2 * command.c Command socket processing.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 * Copyright 2008 The FreeRADIUS server project
21 * Copyright 2008 Alan DeKok <aland@deployingradius.com>
24 #ifdef WITH_COMMAND_SOCKET
26 #include <freeradius-devel/modpriv.h>
27 #include <freeradius-devel/conffile.h>
28 #include <freeradius-devel/stats.h>
29 #include <freeradius-devel/realms.h>
34 #define SUN_LEN(su) (sizeof(*(su)) - sizeof((su)->sun_path) + strlen((su)->sun_path))
38 #ifdef HAVE_SYS_STAT_H
50 typedef struct fr_command_table_t fr_command_table_t;
52 typedef int (*fr_command_func_t)(rad_listen_t *, int, char *argv[]);
57 struct fr_command_table_t {
59 int mode; /* read/write */
61 fr_command_func_t func;
62 fr_command_table_t *table;
65 #define COMMAND_BUFFER_SIZE (1024)
67 typedef struct fr_command_socket_t {
78 * The next few entries handle fake packets injected by
81 fr_ipaddr_t src_ipaddr; /* src_port is always 0 */
82 fr_ipaddr_t dst_ipaddr;
84 rad_listen_t *inject_listener;
85 RADCLIENT *inject_client;
88 * The next few entries do buffer management.
92 char buffer[COMMAND_BUFFER_SIZE];
93 } fr_command_socket_t;
95 static const CONF_PARSER command_config[] = {
96 { "socket", PW_TYPE_STRING_PTR,
97 offsetof(fr_command_socket_t, path), NULL, "${run_dir}/radiusd.sock"},
98 { "uid", PW_TYPE_STRING_PTR,
99 offsetof(fr_command_socket_t, uid_name), NULL, NULL},
100 { "gid", PW_TYPE_STRING_PTR,
101 offsetof(fr_command_socket_t, gid_name), NULL, NULL},
102 { "mode", PW_TYPE_STRING_PTR,
103 offsetof(fr_command_socket_t, mode_name), NULL, NULL},
105 { NULL, -1, 0, NULL, NULL } /* end the list */
108 static FR_NAME_NUMBER mode_names[] = {
110 { "read-only", FR_READ },
111 { "read-write", FR_READ | FR_WRITE },
112 { "rw", FR_READ | FR_WRITE },
117 static ssize_t cprintf(rad_listen_t *listener, const char *fmt, ...)
119 __attribute__ ((format (printf, 2, 3)))
123 #ifndef HAVE_GETPEEREID
124 static int getpeereid(int s, uid_t *euid, gid_t *egid)
130 socklen_t cl = sizeof(cr);
132 if (getsockopt(s, SOL_SOCKET, SO_PEERCRED, &cr, &cl) < 0) {
139 #endif /* SO_PEERCRED */
141 #endif /* HAVE_GETPEEREID */
144 static int fr_server_domain_socket(const char *path)
149 struct sockaddr_un salocal;
153 if (len >= sizeof(salocal.sun_path)) {
154 radlog(L_ERR, "Path too long in socket filename.");
158 if ((sockfd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
159 radlog(L_ERR, "Failed creating socket: %s",
164 memset(&salocal, 0, sizeof(salocal));
165 salocal.sun_family = AF_UNIX;
166 memcpy(salocal.sun_path, path, len + 1); /* SUN_LEN does strlen */
168 socklen = SUN_LEN(&salocal);
173 if (stat(path, &buf) < 0) {
174 if (errno != ENOENT) {
175 radlog(L_ERR, "Failed to stat %s: %s",
176 path, strerror(errno));
181 * FIXME: Check the enclosing directory?
183 } else { /* it exists */
184 if (!S_ISREG(buf.st_mode)
186 && !S_ISSOCK(buf.st_mode)
189 radlog(L_ERR, "Cannot turn %s into socket", path);
194 * In daemon mode, check the ownership. If in
195 * debug mode, ignore ownership if we're root.
197 if ((debug_flag == 0) || (geteuid() != 0)) {
200 * Refuse to open sockets not owned by us.
202 if (buf.st_uid != geteuid()) {
203 radlog(L_ERR, "We do not own %s", path);
208 * In debug mode as root, leave the file
209 * there. Otherwise, it will be owned by
210 * "root", which makes it difficult to
211 * open the file as user "radiusd".
213 if (unlink(path) < 0) {
214 radlog(L_ERR, "Failed to delete %s: %s",
215 path, strerror(errno));
221 if (bind(sockfd, (struct sockaddr *)&salocal, socklen) < 0) {
222 radlog(L_ERR, "Failed binding to %s: %s",
223 path, strerror(errno));
229 * FIXME: There's a race condition here. But Linux
230 * doesn't seem to permit fchmod on domain sockets.
232 if (chmod(path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP) < 0) {
233 radlog(L_ERR, "Failed setting permissions on %s: %s",
234 path, strerror(errno));
239 if (listen(sockfd, 8) < 0) {
240 radlog(L_ERR, "Failed listening to %s: %s",
241 path, strerror(errno));
250 if ((flags = fcntl(sockfd, F_GETFL, NULL)) < 0) {
251 radlog(L_ERR, "Failure getting socket flags: %s",
258 if( fcntl(sockfd, F_SETFL, flags) < 0) {
259 radlog(L_ERR, "Failure setting socket flags: %s",
270 static void command_close_socket(rad_listen_t *this)
272 this->status = RAD_LISTEN_STATUS_CLOSED;
275 * This removes the socket from the event fd, so no one
276 * will be calling us any more.
282 static ssize_t cprintf(rad_listen_t *listener, const char *fmt, ...)
289 len = vsnprintf(buffer, sizeof(buffer), fmt, ap);
292 if (listener->status == RAD_LISTEN_STATUS_CLOSED) return 0;
294 len = write(listener->fd, buffer, len);
295 if (len <= 0) command_close_socket(listener);
298 * FIXME: Keep writing until done?
303 static int command_hup(rad_listen_t *listener, int argc, char *argv[])
306 module_instance_t *mi;
309 radius_signal_self(RADIUS_SIGNAL_SELF_HUP);
313 cs = cf_section_find("modules");
316 mi = find_module_instance(cs, argv[0], 0);
318 cprintf(listener, "ERROR: No such module \"%s\"\n", argv[0]);
322 if ((mi->entry->module->type & RLM_TYPE_HUP_SAFE) == 0) {
323 cprintf(listener, "ERROR: Module %s cannot be hup'd\n",
328 if (!module_hup_module(mi->cs, mi, time(NULL))) {
329 cprintf(listener, "ERROR: Failed to reload module\n");
333 return 1; /* success */
336 static int command_terminate(UNUSED rad_listen_t *listener,
337 UNUSED int argc, UNUSED char *argv[])
339 radius_signal_self(RADIUS_SIGNAL_SELF_TERM);
341 return 1; /* success */
344 extern time_t fr_start_time;
346 static int command_uptime(rad_listen_t *listener,
347 UNUSED int argc, UNUSED char *argv[])
351 CTIME_R(&fr_start_time, buffer, sizeof(buffer));
352 cprintf(listener, "Up since %s", buffer); /* no \r\n */
354 return 1; /* success */
357 static const char *tabs = "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
360 * FIXME: Recurse && indent?
362 static void cprint_conf_parser(rad_listen_t *listener, int indent, CONF_SECTION *cs,
368 const char *name1 = cf_section_name1(cs);
369 const char *name2 = cf_section_name2(cs);
370 const CONF_PARSER *variables = cf_section_parse_table(cs);
374 cprintf(listener, "%.*s%s %s {\n", indent, tabs, name1, name2);
376 cprintf(listener, "%.*s%s {\n", indent, tabs, name1);
384 if (variables) for (i = 0; variables[i].name != NULL; i++) {
386 * No base struct offset, data must be the pointer.
387 * If data doesn't exist, ignore the entry, there
388 * must be something wrong.
391 if (!variables[i].data) {
395 data = variables[i].data;;
397 } else if (variables[i].data) {
398 data = variables[i].data;;
401 data = (((const char *)base) + variables[i].offset);
404 switch (variables[i].type) {
406 cprintf(listener, "%.*s%s = ?\n", indent, tabs,
410 case PW_TYPE_INTEGER:
411 cprintf(listener, "%.*s%s = %u\n", indent, tabs,
412 variables[i].name, *(const int *) data);
416 inet_ntop(AF_INET, data, buffer, sizeof(buffer));
419 case PW_TYPE_IPV6ADDR:
420 inet_ntop(AF_INET6, data, buffer, sizeof(buffer));
423 case PW_TYPE_BOOLEAN:
424 cprintf(listener, "%.*s%s = %s\n", indent, tabs,
426 ((*(const int *) data) == 0) ? "no" : "yes");
429 case PW_TYPE_STRING_PTR:
430 case PW_TYPE_FILENAME:
432 * FIXME: Escape things in the string!
434 if (*(const char * const *) data) {
435 cprintf(listener, "%.*s%s = \"%s\"\n", indent, tabs,
436 variables[i].name, *(const char * const *) data);
438 cprintf(listener, "%.*s%s = \n", indent, tabs,
448 cprintf(listener, "%.*s}\n", indent, tabs);
451 static int command_show_module_config(rad_listen_t *listener, int argc, char *argv[])
454 module_instance_t *mi;
457 cprintf(listener, "ERROR: No module name was given\n");
461 cs = cf_section_find("modules");
464 mi = find_module_instance(cs, argv[0], 0);
466 cprintf(listener, "ERROR: No such module \"%s\"\n", argv[0]);
470 cprint_conf_parser(listener, 0, mi->cs, mi->insthandle);
472 return 1; /* success */
475 static const char *method_names[RLM_COMPONENT_COUNT] = {
487 static int command_show_module_methods(rad_listen_t *listener, int argc, char *argv[])
491 const module_instance_t *mi;
495 cprintf(listener, "ERROR: No module name was given\n");
499 cs = cf_section_find("modules");
502 mi = find_module_instance(cs, argv[0], 0);
504 cprintf(listener, "ERROR: No such module \"%s\"\n", argv[0]);
508 mod = mi->entry->module;
510 for (i = 0; i < RLM_COMPONENT_COUNT; i++) {
511 if (mod->methods[i]) cprintf(listener, "\t%s\n", method_names[i]);
514 return 1; /* success */
518 static int command_show_module_flags(rad_listen_t *listener, int argc, char *argv[])
521 const module_instance_t *mi;
525 cprintf(listener, "ERROR: No module name was given\n");
529 cs = cf_section_find("modules");
532 mi = find_module_instance(cs, argv[0], 0);
534 cprintf(listener, "ERROR: No such module \"%s\"\n", argv[0]);
538 mod = mi->entry->module;
540 if ((mod->type & RLM_TYPE_THREAD_SAFE) != 0)
541 cprintf(listener, "\tthread-safe\n");
544 if ((mod->type & RLM_TYPE_CHECK_CONFIG_SAFE) != 0)
545 cprintf(listener, "\twill-check-config\n");
548 if ((mod->type & RLM_TYPE_HUP_SAFE) != 0)
549 cprintf(listener, "\treload-on-hup\n");
551 return 1; /* success */
556 * Show all loaded modules
558 static int command_show_modules(rad_listen_t *listener, UNUSED int argc, UNUSED char *argv[])
560 CONF_SECTION *cs, *subcs;
562 cs = cf_section_find("modules");
566 while ((subcs = cf_subsection_find_next(cs, subcs, NULL)) != NULL) {
567 const char *name1 = cf_section_name1(subcs);
568 const char *name2 = cf_section_name2(subcs);
570 module_instance_t *mi;
573 mi = find_module_instance(cs, name2, 0);
576 cprintf(listener, "\t%s (%s)\n", name2, name1);
578 mi = find_module_instance(cs, name1, 0);
581 cprintf(listener, "\t%s\n", name1);
585 return 1; /* success */
589 static int command_show_home_servers(rad_listen_t *listener, UNUSED int argc, UNUSED char *argv[])
593 const char *type, *state, *proto;
597 for (i = 0; i < 256; i++) {
598 home = home_server_bynumber(i);
602 * Internal "virtual" home server.
604 if (home->ipaddr.af == AF_UNSPEC) continue;
606 if (home->type == HOME_TYPE_AUTH) {
609 } else if (home->type == HOME_TYPE_ACCT) {
614 if (home->proto == IPPROTO_UDP) {
618 else if (home->proto == IPPROTO_TCP) {
624 if (home->state == HOME_STATE_ALIVE) {
627 } else if (home->state == HOME_STATE_ZOMBIE) {
630 } else if (home->state == HOME_STATE_IS_DEAD) {
635 cprintf(listener, "%s\t%d\t%s\t%s\t%s\t%d\n",
636 ip_ntoh(&home->ipaddr, buffer, sizeof(buffer)),
637 home->port, proto, type, state,
638 home->currently_outstanding);
645 static int command_show_clients(rad_listen_t *listener, UNUSED int argc, UNUSED char *argv[])
651 for (i = 0; i < 256; i++) {
652 client = client_findbynumber(NULL, i);
655 ip_ntoh(&client->ipaddr, buffer, sizeof(buffer));
657 if (((client->ipaddr.af == AF_INET) &&
658 (client->prefix != 32)) ||
659 ((client->ipaddr.af == AF_INET6) &&
660 (client->prefix != 128))) {
661 cprintf(listener, "\t%s/%d\n", buffer, client->prefix);
663 cprintf(listener, "\t%s\n", buffer);
671 static int command_show_xml(rad_listen_t *listener, UNUSED int argc, UNUSED char *argv[])
674 FILE *fp = fdopen(dup(listener->fd), "a");
677 cprintf(listener, "ERROR: Can't dup %s\n", strerror(errno));
682 cprintf(listener, "ERROR: <reference> is required\n");
686 ci = cf_reference_item(mainconfig.config, mainconfig.config, argv[0]);
688 cprintf(listener, "ERROR: No such item <reference>\n");
692 if (cf_item_is_section(ci)) {
693 cf_section2xml(fp, cf_itemtosection(ci));
695 } else if (cf_item_is_pair(ci)) {
696 cf_pair2xml(fp, cf_itemtopair(ci));
699 cprintf(listener, "ERROR: No such item <reference>\n");
706 return 1; /* success */
709 static int command_show_version(rad_listen_t *listener, UNUSED int argc, UNUSED char *argv[])
711 cprintf(listener, "%s\n", radiusd_version);
715 static int command_debug_level(rad_listen_t *listener, int argc, char *argv[])
720 cprintf(listener, "ERROR: Must specify <number>\n");
724 number = atoi(argv[0]);
725 if ((number < 0) || (number > 4)) {
726 cprintf(listener, "ERROR: <number> must be between 0 and 4\n");
730 fr_debug_flag = debug_flag = number;
735 char *debug_log_file = NULL;
736 static char debug_log_file_buffer[1024];
738 static int command_debug_file(rad_listen_t *listener, int argc, char *argv[])
740 if (debug_flag && mainconfig.radlog_dest == RADLOG_STDOUT) {
741 cprintf(listener, "ERROR: Cannot redirect debug logs to a file when already in debugging mode.\n");
745 if ((argc > 0) && (strchr(argv[0], FR_DIR_SEP) != NULL)) {
746 cprintf(listener, "ERROR: Cannot direct debug logs to absolute path.\n");
749 debug_log_file = NULL;
751 if (argc == 0) return 0;
754 * This looks weird, but it's here to avoid locking
755 * a mutex for every log message.
757 memset(debug_log_file_buffer, 0, sizeof(debug_log_file_buffer));
760 * Debug files always go to the logging directory.
762 snprintf(debug_log_file_buffer, sizeof(debug_log_file_buffer),
763 "%s/%s", radlog_dir, argv[0]);
765 debug_log_file = &debug_log_file_buffer[0];
770 extern char *debug_condition;
771 static int command_debug_condition(UNUSED rad_listen_t *listener, int argc, char *argv[])
774 * Delete old condition.
776 * This is thread-safe because the condition is evaluated
777 * in the main server thread, as is this code.
779 free(debug_condition);
780 debug_condition = NULL;
789 debug_condition = strdup(argv[0]);
794 static int command_show_debug_condition(rad_listen_t *listener,
795 UNUSED int argc, UNUSED char *argv[])
797 if (!debug_condition) return 0;
799 cprintf(listener, "%s\n", debug_condition);
804 static int command_show_debug_file(rad_listen_t *listener,
805 UNUSED int argc, UNUSED char *argv[])
807 if (!debug_log_file) return 0;
809 cprintf(listener, "%s\n", debug_log_file);
814 static int command_show_debug_level(rad_listen_t *listener,
815 UNUSED int argc, UNUSED char *argv[])
817 cprintf(listener, "%d\n", debug_flag);
822 static RADCLIENT *get_client(rad_listen_t *listener, int argc, char *argv[])
826 int proto = IPPROTO_UDP;
829 cprintf(listener, "ERROR: Must specify <ipaddr>\n");
833 if (ip_hton(argv[0], AF_UNSPEC, &ipaddr) < 0) {
834 cprintf(listener, "ERROR: Failed parsing IP address; %s\n",
841 if (strcmp(argv[1], "tcp") == 0) {
844 } else if (strcmp(argv[1], "udp") == 0) {
848 cprintf(listener, "ERROR: Unknown protocol %s. Please use \"udp\" or \"tcp\"\n",
855 client = client_find(NULL, &ipaddr, proto);
857 cprintf(listener, "ERROR: No such client\n");
865 static int command_show_client_config(rad_listen_t *listener, int argc, char *argv[])
870 client = get_client(listener, argc, argv);
875 if (!client->cs) return 1;
877 fp = fdopen(dup(listener->fd), "a");
879 cprintf(listener, "ERROR: Can't dup %s\n", strerror(errno));
883 cf_section2file(fp, client->cs);
890 static home_server *get_home_server(rad_listen_t *listener, int argc,
891 char *argv[], int *last)
895 int proto = IPPROTO_UDP;
899 cprintf(listener, "ERROR: Must specify <ipaddr> <port> [proto]\n");
903 if (ip_hton(argv[0], AF_UNSPEC, &ipaddr) < 0) {
904 cprintf(listener, "ERROR: Failed parsing IP address; %s\n",
909 port = atoi(argv[1]);
913 if (strcmp(argv[2], "udp") == 0) {
918 if (strcmp(argv[2], "tcp") == 0) {
925 home = home_server_find(&ipaddr, port, proto);
927 cprintf(listener, "ERROR: No such home server\n");
934 static int command_show_home_server_config(rad_listen_t *listener, int argc, char *argv[])
939 home = get_home_server(listener, argc, argv, NULL);
944 if (!home->cs) return 1;
946 fp = fdopen(dup(listener->fd), "a");
948 cprintf(listener, "ERROR: Can't dup %s\n", strerror(errno));
952 cf_section2file(fp, home->cs);
958 extern void revive_home_server(void *ctx);
959 extern void mark_home_server_dead(home_server *home, struct timeval *when);
961 static int command_set_home_server_state(rad_listen_t *listener, int argc, char *argv[])
967 cprintf(listener, "ERROR: Must specify <ipaddr> <port> [proto] <state>\n");
971 home = get_home_server(listener, argc, argv, &last);
976 if (strcmp(argv[last], "alive") == 0) {
977 revive_home_server(home);
979 } else if (strcmp(argv[last], "dead") == 0) {
982 gettimeofday(&now, NULL); /* we do this WAY too ofetn */
983 mark_home_server_dead(home, &now);
986 cprintf(listener, "ERROR: Unknown state \"%s\"\n", argv[last]);
993 static int command_show_home_server_state(rad_listen_t *listener, int argc, char *argv[])
997 home = get_home_server(listener, argc, argv, NULL);
1002 switch (home->state) {
1003 case HOME_STATE_ALIVE:
1004 cprintf(listener, "alive\n");
1007 case HOME_STATE_IS_DEAD:
1008 cprintf(listener, "dead\n");
1011 case HOME_STATE_ZOMBIE:
1012 cprintf(listener, "zombie\n");
1016 cprintf(listener, "unknown\n");
1025 * For encode/decode stuff
1027 static int null_socket_dencode(UNUSED rad_listen_t *listener, UNUSED REQUEST *request)
1032 static int null_socket_send(UNUSED rad_listen_t *listener, REQUEST *request)
1038 output_file = request_data_reference(request, null_socket_send, 0);
1040 radlog(L_ERR, "WARNING: No output file for injected packet %d",
1045 fp = fopen(output_file, "w");
1047 radlog(L_ERR, "Failed to send injected file to %s: %s",
1048 output_file, strerror(errno));
1052 if (request->reply->code != 0) {
1053 const char *what = "reply";
1056 if (request->reply->code < FR_MAX_PACKET_CODE) {
1057 what = fr_packet_codes[request->reply->code];
1060 fprintf(fp, "%s\n", what);
1063 request->radlog(L_DBG, 0, request,
1064 "Injected %s packet to host %s port 0 code=%d, id=%d",
1066 inet_ntop(request->reply->src_ipaddr.af,
1067 &request->reply->src_ipaddr.ipaddr,
1068 buffer, sizeof(buffer)),
1069 request->reply->code, request->reply->id);
1072 for (vp = request->reply->vps; vp != NULL; vp = vp->next) {
1073 vp_prints(buffer, sizeof(buffer), vp);
1074 fprintf(fp, "%s\n", buffer);
1076 request->radlog(L_DBG, 0, request, "\t%s",
1086 static int command_inject_to(rad_listen_t *listener, int argc, char *argv[])
1089 RAD_LISTEN_TYPE type;
1090 fr_command_socket_t *sock = listener->data;
1092 rad_listen_t *found = NULL;
1095 cprintf(listener, "ERROR: Must specify [auth/acct]\n");
1099 if (strcmp(argv[0], "auth") == 0) {
1100 type = RAD_LISTEN_AUTH;
1102 } else if (strcmp(argv[0], "acct") == 0) {
1103 #ifdef WITH_ACCOUNTING
1104 type = RAD_LISTEN_ACCT;
1106 cprintf(listener, "ERROR: This server was built without accounting support.\n");
1111 cprintf(listener, "ERROR: Unknown socket type\n");
1116 cprintf(listener, "ERROR: No <ipaddr> <port> was given\n");
1121 * FIXME: Look for optional arg 4, and bind interface.
1124 if (ip_hton(argv[1], AF_UNSPEC, &ipaddr) < 0) {
1125 cprintf(listener, "ERROR: Failed parsing IP address; %s\n",
1129 port = atoi(argv[2]);
1131 found = listener_find_byipaddr(&ipaddr, port);
1133 cprintf(listener, "ERROR: Could not find matching listener\n");
1137 sock->inject_listener = found;
1138 sock->dst_ipaddr = ipaddr;
1139 sock->dst_port = port;
1144 static int command_inject_from(rad_listen_t *listener, int argc, char *argv[])
1147 fr_command_socket_t *sock = listener->data;
1150 cprintf(listener, "ERROR: No <ipaddr> was given\n");
1154 if (!sock->inject_listener) {
1155 cprintf(listener, "ERROR: You must specify \"inject to\" before using \"inject from\"\n");
1159 sock->src_ipaddr.af = AF_UNSPEC;
1160 if (ip_hton(argv[0], AF_UNSPEC, &sock->src_ipaddr) < 0) {
1161 cprintf(listener, "ERROR: Failed parsing IP address; %s\n",
1166 client = client_listener_find(sock->inject_listener, &sock->src_ipaddr,
1169 cprintf(listener, "ERROR: No such client %s\n", argv[0]);
1172 sock->inject_client = client;
1177 static int command_inject_file(rad_listen_t *listener, int argc, char *argv[])
1179 static int inject_id = 0;
1181 fr_command_socket_t *sock = listener->data;
1183 REQUEST *request = NULL;
1184 RADIUS_PACKET *packet;
1187 RAD_REQUEST_FUNP fun = NULL;
1191 cprintf(listener, "ERROR: You must specify <input-file> <output-file>\n");
1196 * Output files always go to the logging directory.
1198 snprintf(buffer, sizeof(buffer), "%s/%s", radlog_dir, argv[1]);
1200 fp = fopen(argv[0], "r");
1202 cprintf(listener, "ERROR: Failed opening %s: %s\n",
1203 argv[0], strerror(errno));
1207 vp = readvp2(fp, &filedone, "");
1210 cprintf(listener, "ERROR: Failed reading attributes from %s: %s\n",
1211 argv[0], fr_strerror());
1215 fake = rad_malloc(sizeof(*fake));
1216 memcpy(fake, sock->inject_listener, sizeof(*fake));
1219 * Re-write the IO for the listener.
1221 fake->encode = null_socket_dencode;
1222 fake->decode = null_socket_dencode;
1223 fake->send = null_socket_send;
1225 packet = rad_alloc(0);
1226 packet->src_ipaddr = sock->src_ipaddr;
1227 packet->src_port = 0;
1229 packet->dst_ipaddr = sock->dst_ipaddr;
1230 packet->dst_port = sock->dst_port;
1232 packet->id = inject_id++;
1234 if (fake->type == RAD_LISTEN_AUTH) {
1235 packet->code = PW_AUTHENTICATION_REQUEST;
1236 fun = rad_authenticate;
1239 #ifdef WITH_ACCOUNTING
1240 packet->code = PW_ACCOUNTING_REQUEST;
1241 fun = rad_accounting;
1243 cprintf(listener, "ERROR: This server was built without accounting support.\n");
1250 if (!received_request(fake, packet, &request, sock->inject_client)) {
1251 cprintf(listener, "ERROR: Failed to inject request. See log file for details\n");
1258 * Remember what the output file is, and remember to
1259 * delete the fake listener when done.
1261 request_data_add(request, null_socket_send, 0, strdup(buffer), free);
1262 request_data_add(request, null_socket_send, 1, fake, free);
1265 request->radlog(L_DBG, 0, request,
1266 "Injected %s packet from host %s port 0 code=%d, id=%d",
1267 fr_packet_codes[packet->code],
1268 inet_ntop(packet->src_ipaddr.af,
1269 &packet->src_ipaddr.ipaddr,
1270 buffer, sizeof(buffer)),
1271 packet->code, packet->id);
1273 for (vp = packet->vps; vp != NULL; vp = vp->next) {
1274 vp_prints(buffer, sizeof(buffer), vp);
1275 request->radlog(L_DBG, 0, request, "\t%s", buffer);
1280 * And go process it.
1282 thread_pool_addrequest(request, fun);
1288 static fr_command_table_t command_table_inject[] = {
1290 "inject to <ipaddr> <port> - Inject packets to the destination IP and port.",
1291 command_inject_to, NULL },
1294 "inject from <ipaddr> - Inject packets as if they came from <ipaddr>",
1295 command_inject_from, NULL },
1298 "inject file <input-file> <output-file> - Inject packet from input-file>, with results sent to <output-file>",
1299 command_inject_file, NULL },
1301 { NULL, 0, NULL, NULL, NULL }
1304 static fr_command_table_t command_table_debug[] = {
1305 { "condition", FR_WRITE,
1306 "debug condition [condition] - Enable debugging for requests matching [condition]",
1307 command_debug_condition, NULL },
1309 { "level", FR_WRITE,
1310 "debug level <number> - Set debug level to <number>. Higher is more debugging.",
1311 command_debug_level, NULL },
1314 "debug file [filename] - Send all debugging output to [filename]",
1315 command_debug_file, NULL },
1317 { NULL, 0, NULL, NULL, NULL }
1320 static fr_command_table_t command_table_show_debug[] = {
1321 { "condition", FR_READ,
1322 "show debug condition - Shows current debugging condition.",
1323 command_show_debug_condition, NULL },
1326 "show debug level - Shows current debugging level.",
1327 command_show_debug_level, NULL },
1330 "show debug file - Shows current debugging file.",
1331 command_show_debug_file, NULL },
1333 { NULL, 0, NULL, NULL, NULL }
1336 static fr_command_table_t command_table_show_module[] = {
1337 { "config", FR_READ,
1338 "show module config <module> - show configuration for given module",
1339 command_show_module_config, NULL },
1341 "show module flags <module> - show other module properties",
1342 command_show_module_flags, NULL },
1344 "show module list - shows list of loaded modules",
1345 command_show_modules, NULL },
1346 { "methods", FR_READ,
1347 "show module methods <module> - show sections where <module> may be used",
1348 command_show_module_methods, NULL },
1350 { NULL, 0, NULL, NULL, NULL }
1353 static fr_command_table_t command_table_show_client[] = {
1354 { "config", FR_READ,
1355 "show client config <ipaddr> "
1359 "- show configuration for given client",
1360 command_show_client_config, NULL },
1362 "show client list - shows list of global clients",
1363 command_show_clients, NULL },
1365 { NULL, 0, NULL, NULL, NULL }
1369 static fr_command_table_t command_table_show_home[] = {
1370 { "config", FR_READ,
1371 "show home_server config <ipaddr> <port> [proto] - show configuration for given home server",
1372 command_show_home_server_config, NULL },
1374 "show home_server list - shows list of home servers",
1375 command_show_home_servers, NULL },
1377 "show home_server state <ipaddr> <port> [proto] - shows state of given home server",
1378 command_show_home_server_state, NULL },
1380 { NULL, 0, NULL, NULL, NULL }
1385 static fr_command_table_t command_table_show[] = {
1386 { "client", FR_READ,
1387 "show client <command> - do sub-command of client",
1388 NULL, command_table_show_client },
1390 "show debug <command> - show debug properties",
1391 NULL, command_table_show_debug },
1393 { "home_server", FR_READ,
1394 "show home_server <command> - do sub-command of home_server",
1395 NULL, command_table_show_home },
1397 { "module", FR_READ,
1398 "show module <command> - do sub-command of module",
1399 NULL, command_table_show_module },
1400 { "uptime", FR_READ,
1401 "show uptime - shows time at which server started",
1402 command_uptime, NULL },
1403 { "version", FR_READ,
1404 "show version - Prints version of the running server",
1405 command_show_version, NULL },
1407 "show xml <reference> - Prints out configuration as XML",
1408 command_show_xml, NULL },
1409 { NULL, 0, NULL, NULL, NULL }
1413 static int command_set_module_config(rad_listen_t *listener, int argc, char *argv[])
1418 module_instance_t *mi;
1419 const CONF_PARSER *variables;
1423 cprintf(listener, "ERROR: No module name or variable was given\n");
1427 cs = cf_section_find("modules");
1430 mi = find_module_instance(cs, argv[0], 0);
1432 cprintf(listener, "ERROR: No such module \"%s\"\n", argv[0]);
1436 if ((mi->entry->module->type & RLM_TYPE_HUP_SAFE) == 0) {
1437 cprintf(listener, "ERROR: Cannot change configuration of module as it is cannot be HUP'd.\n");
1441 variables = cf_section_parse_table(mi->cs);
1443 cprintf(listener, "ERROR: Cannot find configuration for module\n");
1448 for (i = 0; variables[i].name != NULL; i++) {
1450 * FIXME: Recurse into sub-types somehow...
1452 if (variables[i].type == PW_TYPE_SUBSECTION) continue;
1454 if (strcmp(variables[i].name, argv[1]) == 0) {
1461 cprintf(listener, "ERROR: No such variable \"%s\"\n", argv[1]);
1465 i = rcode; /* just to be safe */
1468 * It's not part of the dynamic configuration. The module
1469 * needs to re-parse && validate things.
1471 if (variables[i].data) {
1472 cprintf(listener, "ERROR: Variable cannot be dynamically updated\n");
1476 data = ((char *) mi->insthandle) + variables[i].offset;
1478 cp = cf_pair_find(mi->cs, argv[1]);
1482 * Replace the OLD value in the configuration file with
1485 * FIXME: Parse argv[2] depending on it's data type!
1486 * If it's a string, look for leading single/double quotes,
1487 * end then call tokenize functions???
1489 cf_pair_replace(mi->cs, cp, argv[2]);
1491 rcode = cf_item_parse(mi->cs, argv[1], variables[i].type,
1494 cprintf(listener, "ERROR: Failed to parse value\n");
1498 return 1; /* success */
1501 static int command_set_module_status(rad_listen_t *listener, int argc, char *argv[])
1504 module_instance_t *mi;
1507 cprintf(listener, "ERROR: No module name or status was given\n");
1511 cs = cf_section_find("modules");
1514 mi = find_module_instance(cs, argv[0], 0);
1516 cprintf(listener, "ERROR: No such module \"%s\"\n", argv[0]);
1521 if (strcmp(argv[1], "alive") == 0) {
1524 } else if (strcmp(argv[1], "dead") == 0) {
1528 cprintf(listener, "ERROR: Unknown status \"%s\"\n", argv[2]);
1532 return 1; /* success */
1535 static int command_print_stats(rad_listen_t *listener, fr_stats_t *stats,
1538 cprintf(listener, "\trequests\t%u\n", stats->total_requests);
1539 cprintf(listener, "\tresponses\t%u\n", stats->total_responses);
1542 cprintf(listener, "\taccepts\t\t%u\n",
1543 stats->total_access_accepts);
1544 cprintf(listener, "\trejects\t\t%u\n",
1545 stats->total_access_rejects);
1546 cprintf(listener, "\tchallenges\t%u\n",
1547 stats->total_access_challenges);
1550 cprintf(listener, "\tdup\t\t%u\n", stats->total_dup_requests);
1551 cprintf(listener, "\tinvalid\t\t%u\n", stats->total_invalid_requests);
1552 cprintf(listener, "\tmalformed\t%u\n", stats->total_malformed_requests);
1553 cprintf(listener, "\tbad_signature\t%u\n", stats->total_bad_authenticators);
1554 cprintf(listener, "\tdropped\t\t%u\n", stats->total_packets_dropped);
1555 cprintf(listener, "\tunknown_types\t%u\n", stats->total_unknown_types);
1562 static FR_NAME_NUMBER state_names[] = {
1563 { "unopened", STATE_UNOPENED },
1564 { "unlocked", STATE_UNLOCKED },
1565 { "header", STATE_HEADER },
1566 { "reading", STATE_READING },
1567 { "queued", STATE_QUEUED },
1568 { "running", STATE_RUNNING },
1569 { "no-reply", STATE_NO_REPLY },
1570 { "replied", STATE_REPLIED },
1575 static int command_stats_detail(rad_listen_t *listener, int argc, char *argv[])
1578 listen_detail_t *data;
1582 cprintf(listener, "ERROR: Must specify <filename>\n");
1587 for (this = mainconfig.listen; this != NULL; this = this->next) {
1588 if (this->type != RAD_LISTEN_DETAIL) continue;
1591 if (strcmp(argv[1], data->filename) != 0) continue;
1596 cprintf(listener, "\tstate\t%s\n",
1597 fr_int2str(state_names, data->state, "?"));
1599 if ((data->state == STATE_UNOPENED) ||
1600 (data->state == STATE_UNLOCKED)) {
1605 * Race conditions: file might not exist.
1607 if (stat(data->filename_work, &buf) < 0) {
1608 cprintf(listener, "packets\t0\n");
1609 cprintf(listener, "tries\t0\n");
1610 cprintf(listener, "offset\t0\n");
1611 cprintf(listener, "size\t0\n");
1615 cprintf(listener, "packets\t%d\n", data->packets);
1616 cprintf(listener, "tries\t%d\n", data->tries);
1617 cprintf(listener, "offset\t%u\n", (unsigned int) data->offset);
1618 cprintf(listener, "size\t%u\n", (unsigned int) buf.st_size);
1625 static int command_stats_home_server(rad_listen_t *listener, int argc, char *argv[])
1630 cprintf(listener, "ERROR: Must specify [auth/acct] OR <ipaddr> <port>\n");
1635 #ifdef WITH_ACCOUNTING
1636 if (strcmp(argv[0], "acct") == 0) {
1637 return command_print_stats(listener,
1638 &proxy_acct_stats, 0);
1641 if (strcmp(argv[0], "auth") == 0) {
1642 return command_print_stats(listener,
1643 &proxy_auth_stats, 1);
1646 cprintf(listener, "ERROR: Should specify [auth/acct]\n");
1650 home = get_home_server(listener, argc, argv, NULL);
1655 command_print_stats(listener, &home->stats,
1656 (home->type == HOME_TYPE_AUTH));
1657 cprintf(listener, "\toutstanding\t%d\n", home->currently_outstanding);
1662 static int command_stats_client(rad_listen_t *listener, int argc, char *argv[])
1668 cprintf(listener, "ERROR: Must specify [auth/acct]\n");
1672 if (strcmp(argv[0], "auth") == 0) {
1675 } else if (strcmp(argv[0], "acct") == 0) {
1676 #ifdef WITH_ACCOUNTING
1679 cprintf(listener, "ERROR: This server was built without accounting support.\n");
1684 cprintf(listener, "ERROR: Unknown statistics type\n");
1689 * Global results for all client.
1692 #ifdef WITH_ACCOUNTING
1694 return command_print_stats(listener,
1695 &radius_acct_stats, auth);
1698 return command_print_stats(listener, &radius_auth_stats, auth);
1701 client = get_client(listener, argc - 1, argv + 1);
1706 #ifdef WITH_ACCOUNTING
1708 return command_print_stats(listener, client->acct, auth);
1712 return command_print_stats(listener, client->auth, auth);
1716 static int command_add_client_file(rad_listen_t *listener, int argc, char *argv[])
1721 cprintf(listener, "ERROR: <file> is required\n");
1726 * Read the file and generate the client.
1728 c = client_read(argv[0], FALSE, FALSE);
1730 cprintf(listener, "ERROR: Unknown error reading client file.\n");
1734 if (!client_add(NULL, c)) {
1735 cprintf(listener, "ERROR: Unknown error inserting new client.\n");
1744 static fr_command_table_t command_table_add_client[] = {
1746 "add client file <filename> - Add new client definition from <filename>",
1747 command_add_client_file, NULL },
1749 { NULL, 0, NULL, NULL, NULL }
1753 static fr_command_table_t command_table_add[] = {
1754 { "client", FR_WRITE,
1755 "add client <command> - Add client configuration commands",
1756 NULL, command_table_add_client },
1758 { NULL, 0, NULL, NULL, NULL }
1763 static fr_command_table_t command_table_set_home[] = {
1764 { "state", FR_WRITE,
1765 "set home_server state <ipaddr> <port> [proto] [alive|dead] - set state for given home server",
1766 command_set_home_server_state, NULL },
1768 { NULL, 0, NULL, NULL, NULL }
1772 static fr_command_table_t command_table_set_module[] = {
1773 { "config", FR_WRITE,
1774 "set module config <module> variable value - set configuration for <module>",
1775 command_set_module_config, NULL },
1777 { "status", FR_WRITE,
1778 "set module status [alive|dead] - set the module to be alive or dead (always return \"fail\")",
1779 command_set_module_status, NULL },
1781 { NULL, 0, NULL, NULL, NULL }
1785 static fr_command_table_t command_table_set[] = {
1786 { "module", FR_WRITE,
1787 "set module <command> - set module commands",
1788 NULL, command_table_set_module },
1790 { "home_server", FR_WRITE,
1791 "set home_server <command> - set home server commands",
1792 NULL, command_table_set_home },
1795 { NULL, 0, NULL, NULL, NULL }
1799 static fr_command_table_t command_table_stats[] = {
1800 { "client", FR_READ,
1801 "stats client [auth/acct] <ipaddr> "
1805 "- show statistics for given client, or for all clients (auth or acct)",
1806 command_stats_client, NULL },
1808 { "home_server", FR_READ,
1809 "stats home_server [<ipaddr>/auth/acct] <port> - show statistics for given home server (ipaddr and port), or for all home servers (auth or acct)",
1810 command_stats_home_server, NULL },
1814 { "detail", FR_READ,
1815 "stats detail <filename> - show statistics for the given detail file",
1816 command_stats_detail, NULL },
1819 { NULL, 0, NULL, NULL, NULL }
1822 static fr_command_table_t command_table[] = {
1823 { "add", FR_WRITE, NULL, NULL, command_table_add },
1824 { "debug", FR_WRITE,
1825 "debug <command> - debugging commands",
1826 NULL, command_table_debug },
1828 "hup [module] - sends a HUP signal to the server, or optionally to one module",
1829 command_hup, NULL },
1830 { "inject", FR_WRITE,
1831 "inject <command> - commands to inject packets into a running server",
1832 NULL, command_table_inject },
1833 { "reconnect", FR_READ,
1834 "reconnect - reconnect to a running server",
1835 NULL, NULL }, /* just here for "help" */
1836 { "terminate", FR_WRITE,
1837 "terminate - terminates the server, and cause it to exit",
1838 command_terminate, NULL },
1839 { "set", FR_WRITE, NULL, NULL, command_table_set },
1840 { "show", FR_READ, NULL, NULL, command_table_show },
1841 { "stats", FR_READ, NULL, NULL, command_table_stats },
1843 { NULL, 0, NULL, NULL, NULL }
1848 * Parse the unix domain sockets.
1850 * FIXME: TCP + SSL, after RadSec is in.
1852 static int command_socket_parse(CONF_SECTION *cs, rad_listen_t *this)
1854 fr_command_socket_t *sock;
1858 if (cf_section_parse(cs, sock, command_config) < 0) {
1862 #if defined(HAVE_GETPEEREID) || defined (SO_PEERCRED)
1863 if (sock->uid_name) {
1866 pw = getpwnam(sock->uid_name);
1868 radlog(L_ERR, "Failed getting uid for %s: %s",
1869 sock->uid_name, strerror(errno));
1873 sock->uid = pw->pw_uid;
1876 if (sock->gid_name) {
1879 gr = getgrnam(sock->gid_name);
1881 radlog(L_ERR, "Failed getting gid for %s: %s",
1882 sock->gid_name, strerror(errno));
1885 sock->gid = gr->gr_gid;
1888 #else /* can't get uid or gid of connecting user */
1890 if (sock->uid_name || sock->gid_name) {
1891 radlog(L_ERR, "System does not support uid or gid authentication for sockets");
1897 if (!sock->mode_name) {
1898 sock->mode = FR_READ;
1900 sock->mode = fr_str2int(mode_names, sock->mode_name, 0);
1902 radlog(L_ERR, "Invalid mode name \"%s\"",
1909 * FIXME: check for absolute pathnames?
1910 * check for uid/gid on the other end...
1913 this->fd = fr_server_domain_socket(sock->path);
1921 static int command_socket_print(rad_listen_t *this, char *buffer, size_t bufsize)
1923 fr_command_socket_t *sock = this->data;
1925 snprintf(buffer, bufsize, "command file %s", sock->path);
1931 * String split routine. Splits an input string IN PLACE
1932 * into pieces, based on spaces.
1934 static int str2argv(char *str, char **argv, int max_argc)
1941 if (argc >= max_argc) return argc;
1944 * Chop out comments early.
1951 while ((*str == ' ') ||
1954 (*str == '\n')) *(str++) = '\0';
1956 if (!*str) return argc;
1958 if ((*str == '\'') || (*str == '"')) {
1962 token = gettoken((const char **) &p, buffer,
1964 if ((token != T_SINGLE_QUOTED_STRING) &&
1965 (token != T_DOUBLE_QUOTED_STRING)) {
1969 len = strlen(buffer);
1970 if (len >= (size_t) (p - str)) {
1974 memcpy(str, buffer, len + 1);
1987 (*str != '\n')) str++;
1993 static void print_help(rad_listen_t *listener,
1994 fr_command_table_t *table, int recursive)
1998 for (i = 0; table[i].command != NULL; i++) {
1999 if (table[i].help) {
2000 cprintf(listener, "%s\n",
2003 cprintf(listener, "%s <command> - do sub-command of %s\n",
2004 table[i].command, table[i].command);
2007 if (recursive && table[i].table) {
2008 print_help(listener, table[i].table, recursive);
2013 #define MAX_ARGV (16)
2016 * Check if an incoming request is "ok"
2018 * It takes packets, not requests. It sees if the packet looks
2019 * OK. If so, it does a number of sanity checks on it.
2021 static int command_domain_recv(rad_listen_t *listener,
2022 UNUSED RAD_REQUEST_FUNP *pfun,
2023 UNUSED REQUEST **prequest)
2028 char *my_argv[MAX_ARGV], **argv;
2029 fr_command_table_t *table;
2030 fr_command_socket_t *co = listener->data;
2039 len = recv(listener->fd, co->buffer + co->offset,
2040 sizeof(co->buffer) - co->offset - 1, 0);
2041 if (len == 0) goto close_socket; /* clean close */
2044 if ((errno == EAGAIN) || (errno == EINTR)) {
2053 if ((co->offset == 0) && (co->buffer[0] == 0x04)) {
2055 command_close_socket(listener);
2060 * See if there are multiple lines in the buffer.
2062 p = co->buffer + co->offset;
2065 for (c = 0; c < len; c++) {
2066 if ((*p == '\r') || (*p == '\n')) {
2071 * FIXME: do real buffering...
2072 * handling of CTRL-C, etc.
2077 * \r \n followed by ASCII...
2088 * Saw CR/LF. Set next element, and exit.
2091 co->next = p - co->buffer;
2095 if (co->offset >= (ssize_t) (sizeof(co->buffer) - 1)) {
2096 radlog(L_ERR, "Line too long!");
2103 DEBUG("radmin> %s", co->buffer);
2105 argc = str2argv(co->buffer, my_argv, MAX_ARGV);
2106 if (argc == 0) goto do_next; /* empty strings are OK */
2109 cprintf(listener, "ERROR: Failed parsing command.\n");
2115 for (len = 0; len <= co->offset; len++) {
2116 if (co->buffer[len] < 0x20) {
2117 co->buffer[len] = '\0';
2123 * Hard-code exit && quit.
2125 if ((strcmp(argv[0], "exit") == 0) ||
2126 (strcmp(argv[0], "quit") == 0)) goto close_socket;
2130 if (strcmp(argv[0], "login") != 0) {
2131 cprintf(listener, "ERROR: Login required\n");
2136 cprintf(listener, "ERROR: login <user> <password>\n");
2141 * FIXME: Generate && process fake RADIUS request.
2143 if ((strcmp(argv[1], "root") == 0) &&
2144 (strcmp(argv[2], "password") == 0)) {
2145 strlcpy(co->user, argv[1], sizeof(co->user));
2149 cprintf(listener, "ERROR: Login incorrect\n");
2154 table = command_table;
2157 for (i = 0; table[i].command != NULL; i++) {
2158 if (strcmp(table[i].command, argv[0]) == 0) {
2160 * Check permissions.
2162 if (((co->mode & FR_WRITE) == 0) &&
2163 ((table[i].mode & FR_WRITE) != 0)) {
2164 cprintf(listener, "ERROR: You do not have write permission. See \"mode = rw\" in %s\n", co->path);
2168 if (table[i].table) {
2170 * This is the last argument, but
2171 * there's a sub-table. Print help.
2175 table = table[i].table;
2181 table = table[i].table;
2185 if ((argc == 2) && (strcmp(argv[1], "?") == 0)) goto do_help;
2187 if (!table[i].func) {
2188 cprintf(listener, "ERROR: Invalid command\n");
2193 rcode = table[i].func(listener,
2194 argc - 1, argv + 1);
2203 if ((strcmp(argv[0], "help") == 0) ||
2204 (strcmp(argv[0], "?") == 0)) {
2208 if ((argc > 1) && (strcmp(argv[1], "-r") == 0)) {
2214 print_help(listener, table, recursive);
2218 cprintf(listener, "ERROR: Unknown command \"%s\"\n",
2223 cprintf(listener, "radmin> ");
2225 if (co->next <= co->offset) {
2228 memmove(co->buffer, co->buffer + co->next,
2229 co->offset - co->next);
2230 co->offset -= co->next;
2237 static int command_domain_accept(rad_listen_t *listener,
2238 UNUSED RAD_REQUEST_FUNP *pfun,
2239 UNUSED REQUEST **prequest)
2245 struct sockaddr_storage src;
2246 fr_command_socket_t *sock = listener->data;
2248 salen = sizeof(src);
2250 DEBUG2(" ... new connection request on command socket.");
2255 newfd = accept(listener->fd, (struct sockaddr *) &src, &salen);
2258 * Non-blocking sockets must handle this.
2260 if (errno == EWOULDBLOCK) {
2264 DEBUG2(" ... failed to accept connection.");
2269 * Perform user authentication.
2271 if (sock->uid_name || sock->gid_name) {
2275 if (getpeereid(listener->fd, &uid, &gid) < 0) {
2276 radlog(L_ERR, "Failed getting peer credentials for %s: %s",
2277 sock->path, strerror(errno));
2282 if (sock->uid_name && (sock->uid != uid)) {
2283 radlog(L_ERR, "Unauthorized connection to %s from uid %ld",
2284 sock->path, (long int) uid);
2289 if (sock->gid_name && (sock->gid != gid)) {
2290 radlog(L_ERR, "Unauthorized connection to %s from gid %ld",
2291 sock->path, (long int) gid);
2298 * Write 32-bit magic number && version information.
2300 magic = htonl(0xf7eead15);
2301 if (write(newfd, &magic, 4) < 0) {
2302 radlog(L_ERR, "Failed writing initial data to socket: %s",
2307 magic = htonl(1); /* protocol version */
2308 if (write(newfd, &magic, 4) < 0) {
2309 radlog(L_ERR, "Failed writing initial data to socket: %s",
2317 * Add the new listener.
2319 this = listen_alloc(listener->type);
2320 if (!this) return 0;
2323 * Copy everything, including the pointer to the socket
2327 memcpy(this, listener, sizeof(*this));
2328 this->status = RAD_LISTEN_STATUS_INIT;
2330 this->data = sock; /* fix it back */
2333 sock->user[0] = '\0';
2334 sock->path = ((fr_command_socket_t *) listener->data)->path;
2335 sock->mode = ((fr_command_socket_t *) listener->data)->mode;
2338 this->recv = command_domain_recv;
2341 * Tell the event loop that we have a new FD
2350 * Delete the socket for safety. If the server isn't running,
2351 * the socket shouldn't exist.
2353 static void command_socket_free(rad_listen_t *listener)
2355 fr_command_socket_t *sock = listener->data;
2357 if (sock->path) unlink(sock->path);
2361 * Send an authentication response packet
2363 static int command_domain_send(UNUSED rad_listen_t *listener,
2364 UNUSED REQUEST *request)
2370 static int command_socket_encode(UNUSED rad_listen_t *listener,
2371 UNUSED REQUEST *request)
2377 static int command_socket_decode(UNUSED rad_listen_t *listener,
2378 UNUSED REQUEST *request)
2383 #endif /* WITH_COMMAND_SOCKET */