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 * Refuse to open sockets not owned by us.
196 if (buf.st_uid != geteuid()) {
197 radlog(L_ERR, "We do not own %s", path);
201 if (unlink(path) < 0) {
202 radlog(L_ERR, "Failed to delete %s: %s",
203 path, strerror(errno));
208 if (bind(sockfd, (struct sockaddr *)&salocal, socklen) < 0) {
209 radlog(L_ERR, "Failed binding to %s: %s",
210 path, strerror(errno));
216 * FIXME: There's a race condition here. But Linux
217 * doesn't seem to permit fchmod on domain sockets.
219 if (chmod(path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP) < 0) {
220 radlog(L_ERR, "Failed setting permissions on %s: %s",
221 path, strerror(errno));
226 if (listen(sockfd, 8) < 0) {
227 radlog(L_ERR, "Failed listening to %s: %s",
228 path, strerror(errno));
237 if ((flags = fcntl(sockfd, F_GETFL, NULL)) < 0) {
238 radlog(L_ERR, "Failure getting socket flags: %s",
245 if( fcntl(sockfd, F_SETFL, flags) < 0) {
246 radlog(L_ERR, "Failure setting socket flags: %s",
257 static void command_close_socket(rad_listen_t *this)
259 this->status = RAD_LISTEN_STATUS_CLOSED;
262 * This removes the socket from the event fd, so no one
263 * will be calling us any more.
271 static ssize_t cprintf(rad_listen_t *listener, const char *fmt, ...)
278 len = vsnprintf(buffer, sizeof(buffer), fmt, ap);
281 if (listener->status == RAD_LISTEN_STATUS_CLOSED) return 0;
283 len = write(listener->fd, buffer, len);
284 if (len <= 0) command_close_socket(listener);
287 * FIXME: Keep writing until done?
292 static int command_hup(rad_listen_t *listener, int argc, char *argv[])
295 module_instance_t *mi;
298 radius_signal_self(RADIUS_SIGNAL_SELF_HUP);
302 cs = cf_section_find("modules");
305 mi = find_module_instance(cs, argv[0], 0);
307 cprintf(listener, "ERROR: No such module \"%s\"\n", argv[0]);
311 if ((mi->entry->module->type & RLM_TYPE_HUP_SAFE) == 0) {
312 cprintf(listener, "ERROR: Module %s cannot be hup'd\n",
317 if (!module_hup_module(mi->cs, mi, time(NULL))) {
318 cprintf(listener, "ERROR: Failed to reload module\n");
322 return 1; /* success */
325 static int command_terminate(UNUSED rad_listen_t *listener,
326 UNUSED int argc, UNUSED char *argv[])
328 radius_signal_self(RADIUS_SIGNAL_SELF_TERM);
330 return 1; /* success */
333 extern time_t fr_start_time;
335 static int command_uptime(rad_listen_t *listener,
336 UNUSED int argc, UNUSED char *argv[])
340 CTIME_R(&fr_start_time, buffer, sizeof(buffer));
341 cprintf(listener, "Up since %s", buffer); /* no \r\n */
343 return 1; /* success */
346 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";
349 * FIXME: Recurse && indent?
351 static void cprint_conf_parser(rad_listen_t *listener, int indent, CONF_SECTION *cs,
357 const char *name1 = cf_section_name1(cs);
358 const char *name2 = cf_section_name2(cs);
359 const CONF_PARSER *variables = cf_section_parse_table(cs);
363 cprintf(listener, "%.*s%s %s {\n", indent, tabs, name1, name2);
365 cprintf(listener, "%.*s%s {\n", indent, tabs, name1);
373 if (variables) for (i = 0; variables[i].name != NULL; i++) {
375 * No base struct offset, data must be the pointer.
376 * If data doesn't exist, ignore the entry, there
377 * must be something wrong.
380 if (!variables[i].data) {
384 data = variables[i].data;;
386 } else if (variables[i].data) {
387 data = variables[i].data;;
390 data = (((char *)base) + variables[i].offset);
393 switch (variables[i].type) {
395 cprintf(listener, "%.*s%s = ?\n", indent, tabs,
399 case PW_TYPE_INTEGER:
400 cprintf(listener, "%.*s%s = %u\n", indent, tabs,
401 variables[i].name, *(int *) data);
405 inet_ntop(AF_INET, data, buffer, sizeof(buffer));
408 case PW_TYPE_IPV6ADDR:
409 inet_ntop(AF_INET6, data, buffer, sizeof(buffer));
412 case PW_TYPE_BOOLEAN:
413 cprintf(listener, "%.*s%s = %s\n", indent, tabs,
415 ((*(int *) data) == 0) ? "no" : "yes");
418 case PW_TYPE_STRING_PTR:
419 case PW_TYPE_FILENAME:
421 * FIXME: Escape things in the string!
423 if (*(char **) data) {
424 cprintf(listener, "%.*s%s = \"%s\"\n", indent, tabs,
425 variables[i].name, *(char **) data);
427 cprintf(listener, "%.*s%s = \n", indent, tabs,
437 cprintf(listener, "%.*s}\n", indent, tabs);
440 static int command_show_module_config(rad_listen_t *listener, int argc, char *argv[])
443 module_instance_t *mi;
446 cprintf(listener, "ERROR: No module name was given\n");
450 cs = cf_section_find("modules");
453 mi = find_module_instance(cs, argv[0], 0);
455 cprintf(listener, "ERROR: No such module \"%s\"\n", argv[0]);
459 cprint_conf_parser(listener, 0, mi->cs, mi->insthandle);
461 return 1; /* success */
464 static const char *method_names[RLM_COMPONENT_COUNT] = {
476 static int command_show_module_methods(rad_listen_t *listener, int argc, char *argv[])
480 const module_instance_t *mi;
484 cprintf(listener, "ERROR: No module name was given\n");
488 cs = cf_section_find("modules");
491 mi = find_module_instance(cs, argv[0], 0);
493 cprintf(listener, "ERROR: No such module \"%s\"\n", argv[0]);
497 mod = mi->entry->module;
499 for (i = 0; i < RLM_COMPONENT_COUNT; i++) {
500 if (mod->methods[i]) cprintf(listener, "\t%s\n", method_names[i]);
503 return 1; /* success */
507 static int command_show_module_flags(rad_listen_t *listener, int argc, char *argv[])
510 const module_instance_t *mi;
514 cprintf(listener, "ERROR: No module name was given\n");
518 cs = cf_section_find("modules");
521 mi = find_module_instance(cs, argv[0], 0);
523 cprintf(listener, "ERROR: No such module \"%s\"\n", argv[0]);
527 mod = mi->entry->module;
529 if ((mod->type & RLM_TYPE_THREAD_SAFE) != 0)
530 cprintf(listener, "\tthread-safe\n");
533 if ((mod->type & RLM_TYPE_CHECK_CONFIG_SAFE) != 0)
534 cprintf(listener, "\twill-check-config\n");
537 if ((mod->type & RLM_TYPE_HUP_SAFE) != 0)
538 cprintf(listener, "\treload-on-hup\n");
540 return 1; /* success */
545 * Show all loaded modules
547 static int command_show_modules(rad_listen_t *listener, UNUSED int argc, UNUSED char *argv[])
549 CONF_SECTION *cs, *subcs;
551 cs = cf_section_find("modules");
555 while ((subcs = cf_subsection_find_next(cs, subcs, NULL)) != NULL) {
556 const char *name1 = cf_section_name1(subcs);
557 const char *name2 = cf_section_name2(subcs);
559 module_instance_t *mi;
562 mi = find_module_instance(cs, name2, 0);
565 cprintf(listener, "\t%s (%s)\n", name2, name1);
567 mi = find_module_instance(cs, name1, 0);
570 cprintf(listener, "\t%s\n", name1);
574 return 1; /* success */
578 static int command_show_home_servers(rad_listen_t *listener, UNUSED int argc, UNUSED char *argv[])
582 const char *type, *state;
586 for (i = 0; i < 256; i++) {
587 home = home_server_bynumber(i);
591 * Internal "virtual" home server.
593 if (home->ipaddr.af == AF_UNSPEC) continue;
595 if (home->type == HOME_TYPE_AUTH) {
598 } else if (home->type == HOME_TYPE_ACCT) {
603 if (home->state == HOME_STATE_ALIVE) {
606 } else if (home->state == HOME_STATE_ZOMBIE) {
609 } else if (home->state == HOME_STATE_IS_DEAD) {
614 cprintf(listener, "%s\t%d\t%s\t%s\t%d\n",
615 ip_ntoh(&home->ipaddr, buffer, sizeof(buffer)),
616 home->port, type, state,
617 home->currently_outstanding);
624 static int command_show_clients(rad_listen_t *listener, UNUSED int argc, UNUSED char *argv[])
630 for (i = 0; i < 256; i++) {
631 client = client_findbynumber(NULL, i);
634 ip_ntoh(&client->ipaddr, buffer, sizeof(buffer));
636 if (((client->ipaddr.af == AF_INET) &&
637 (client->prefix != 32)) ||
638 ((client->ipaddr.af == AF_INET6) &&
639 (client->prefix != 128))) {
640 cprintf(listener, "\t%s/%d\n", buffer, client->prefix);
642 cprintf(listener, "\t%s\n", buffer);
650 static int command_show_xml(rad_listen_t *listener, UNUSED int argc, UNUSED char *argv[])
653 FILE *fp = fdopen(dup(listener->fd), "a");
656 cprintf(listener, "ERROR: Can't dup %s\n", strerror(errno));
661 cprintf(listener, "ERROR: <reference> is required\n");
665 ci = cf_reference_item(mainconfig.config, mainconfig.config, argv[0]);
667 cprintf(listener, "ERROR: No such item <reference>\n");
671 if (cf_item_is_section(ci)) {
672 cf_section2xml(fp, cf_itemtosection(ci));
674 } else if (cf_item_is_pair(ci)) {
675 cf_pair2xml(fp, cf_itemtopair(ci));
678 cprintf(listener, "ERROR: No such item <reference>\n");
685 return 1; /* success */
688 static int command_show_version(rad_listen_t *listener, UNUSED int argc, UNUSED char *argv[])
690 cprintf(listener, "%s\n", radiusd_version);
694 static int command_debug_level(rad_listen_t *listener, int argc, char *argv[])
699 cprintf(listener, "ERROR: Must specify <number>\n");
703 number = atoi(argv[0]);
704 if ((number < 0) || (number > 4)) {
705 cprintf(listener, "ERROR: <number> must be between 0 and 4\n");
709 fr_debug_flag = debug_flag = number;
714 char *debug_log_file = NULL;
715 static char debug_log_file_buffer[1024];
717 static int command_debug_file(rad_listen_t *listener, int argc, char *argv[])
719 if (debug_flag && mainconfig.radlog_dest == RADLOG_STDOUT) {
720 cprintf(listener, "ERROR: Cannot redirect debug logs to a file when already in debugging mode.\n");
724 if ((argc > 0) && (strchr(argv[0], FR_DIR_SEP) != NULL)) {
725 cprintf(listener, "ERROR: Cannot direct debug logs to absolute path.\n");
728 debug_log_file = NULL;
730 if (argc == 0) return 0;
733 * This looks weird, but it's here to avoid locking
734 * a mutex for every log message.
736 memset(debug_log_file_buffer, 0, sizeof(debug_log_file_buffer));
739 * Debug files always go to the logging directory.
741 snprintf(debug_log_file_buffer, sizeof(debug_log_file_buffer),
742 "%s/%s", radlog_dir, argv[0]);
744 debug_log_file = &debug_log_file_buffer[0];
749 extern char *debug_condition;
750 static int command_debug_condition(UNUSED rad_listen_t *listener, int argc, char *argv[])
753 * Delete old condition.
755 * This is thread-safe because the condition is evaluated
756 * in the main server thread, as is this code.
758 free(debug_condition);
759 debug_condition = NULL;
768 debug_condition = strdup(argv[0]);
773 static int command_show_debug_condition(rad_listen_t *listener,
774 UNUSED int argc, UNUSED char *argv[])
776 if (!debug_condition) return 0;
778 cprintf(listener, "%s\n", debug_condition);
783 static int command_show_debug_file(rad_listen_t *listener,
784 UNUSED int argc, UNUSED char *argv[])
786 if (!debug_log_file) return 0;
788 cprintf(listener, "%s\n", debug_log_file);
793 static int command_show_debug_level(rad_listen_t *listener,
794 UNUSED int argc, UNUSED char *argv[])
796 cprintf(listener, "%d\n", debug_flag);
801 static RADCLIENT *get_client(rad_listen_t *listener, int argc, char *argv[])
805 int proto = IPPROTO_UDP;
808 cprintf(listener, "ERROR: Must specify <ipaddr>\n");
812 if (ip_hton(argv[0], AF_UNSPEC, &ipaddr) < 0) {
813 cprintf(listener, "ERROR: Failed parsing IP address; %s\n",
820 if (strcmp(argv[1], "tcp") == 0) {
823 } else if (strcmp(argv[1], "udp") == 0) {
827 cprintf(listener, "ERROR: Unknown protocol %s. Please use \"udp\" or \"tcp\"\n",
834 client = client_find(NULL, &ipaddr, proto);
836 cprintf(listener, "ERROR: No such client\n");
844 static int command_show_client_config(rad_listen_t *listener, int argc, char *argv[])
849 client = get_client(listener, argc, argv);
854 if (!client->cs) return 1;
856 fp = fdopen(dup(listener->fd), "a");
858 cprintf(listener, "ERROR: Can't dup %s\n", strerror(errno));
862 cf_section2file(fp, client->cs);
869 static home_server *get_home_server(rad_listen_t *listener, int argc, char *argv[])
876 cprintf(listener, "ERROR: Must specify <ipaddr> <port>\n");
880 if (ip_hton(argv[0], AF_UNSPEC, &ipaddr) < 0) {
881 cprintf(listener, "ERROR: Failed parsing IP address; %s\n",
886 port = atoi(argv[1]);
888 home = home_server_find(&ipaddr, port);
890 cprintf(listener, "ERROR: No such home server\n");
897 static int command_show_home_server_config(rad_listen_t *listener, int argc, char *argv[])
902 home = get_home_server(listener, argc, argv);
907 if (!home->cs) return 1;
909 fp = fdopen(dup(listener->fd), "a");
911 cprintf(listener, "ERROR: Can't dup %s\n", strerror(errno));
915 cf_section2file(fp, home->cs);
921 extern void revive_home_server(void *ctx);
922 extern void mark_home_server_dead(home_server *home, struct timeval *when);
924 static int command_set_home_server_state(rad_listen_t *listener, int argc, char *argv[])
929 cprintf(listener, "ERROR: Must specify <ipaddr> <port> <state>\n");
933 home = get_home_server(listener, argc, argv);
938 if (strcmp(argv[2], "alive") == 0) {
939 revive_home_server(home);
941 } else if (strcmp(argv[2], "dead") == 0) {
944 gettimeofday(&now, NULL); /* we do this WAY too ofetn */
945 mark_home_server_dead(home, &now);
948 cprintf(listener, "ERROR: Unknown state \"%s\"\n", argv[2]);
955 static int command_show_home_server_state(rad_listen_t *listener, int argc, char *argv[])
959 home = get_home_server(listener, argc, argv);
964 switch (home->state) {
965 case HOME_STATE_ALIVE:
966 cprintf(listener, "alive\n");
969 case HOME_STATE_IS_DEAD:
970 cprintf(listener, "dead\n");
973 case HOME_STATE_ZOMBIE:
974 cprintf(listener, "zombie\n");
978 cprintf(listener, "unknown\n");
987 * For encode/decode stuff
989 static int null_socket_dencode(UNUSED rad_listen_t *listener, UNUSED REQUEST *request)
994 static int null_socket_send(UNUSED rad_listen_t *listener, REQUEST *request)
1000 output_file = request_data_reference(request, null_socket_send, 0);
1002 radlog(L_ERR, "WARNING: No output file for injected packet %d",
1007 fp = fopen(output_file, "w");
1009 radlog(L_ERR, "Failed to send injected file to %s: %s",
1010 output_file, strerror(errno));
1014 if (request->reply->code != 0) {
1015 const char *what = "reply";
1018 if (request->reply->code < FR_MAX_PACKET_CODE) {
1019 what = fr_packet_codes[request->reply->code];
1022 fprintf(fp, "%s\n", what);
1025 request->radlog(L_DBG, 0, request,
1026 "Injected %s packet to host %s port 0 code=%d, id=%d",
1028 inet_ntop(request->reply->src_ipaddr.af,
1029 &request->reply->src_ipaddr.ipaddr,
1030 buffer, sizeof(buffer)),
1031 request->reply->code, request->reply->id);
1034 for (vp = request->reply->vps; vp != NULL; vp = vp->next) {
1035 vp_prints(buffer, sizeof(buffer), vp);
1036 fprintf(fp, "%s\n", buffer);
1038 request->radlog(L_DBG, 0, request, "\t%s",
1048 static int command_inject_to(rad_listen_t *listener, int argc, char *argv[])
1051 RAD_LISTEN_TYPE type;
1052 fr_command_socket_t *sock = listener->data;
1054 rad_listen_t *found = NULL;
1057 cprintf(listener, "ERROR: Must specify [auth/acct]\n");
1061 if (strcmp(argv[0], "auth") == 0) {
1062 type = RAD_LISTEN_AUTH;
1064 } else if (strcmp(argv[0], "acct") == 0) {
1065 #ifdef WITH_ACCOUNTING
1066 type = RAD_LISTEN_ACCT;
1068 cprintf(listener, "ERROR: This server was built without accounting support.\n");
1073 cprintf(listener, "ERROR: Unknown socket type\n");
1078 cprintf(listener, "ERROR: No <ipaddr> <port> was given\n");
1083 * FIXME: Look for optional arg 4, and bind interface.
1086 if (ip_hton(argv[1], AF_UNSPEC, &ipaddr) < 0) {
1087 cprintf(listener, "ERROR: Failed parsing IP address; %s\n",
1091 port = atoi(argv[2]);
1093 found = listener_find_byipaddr(&ipaddr, port);
1095 cprintf(listener, "ERROR: Could not find matching listener\n");
1099 sock->inject_listener = found;
1100 sock->dst_ipaddr = ipaddr;
1101 sock->dst_port = port;
1106 static int command_inject_from(rad_listen_t *listener, int argc, char *argv[])
1109 fr_command_socket_t *sock = listener->data;
1112 cprintf(listener, "ERROR: No <ipaddr> was given\n");
1116 if (!sock->inject_listener) {
1117 cprintf(listener, "ERROR: You must specify \"inject to\" before using \"inject from\"\n");
1121 sock->src_ipaddr.af = AF_UNSPEC;
1122 if (ip_hton(argv[0], AF_UNSPEC, &sock->src_ipaddr) < 0) {
1123 cprintf(listener, "ERROR: Failed parsing IP address; %s\n",
1128 client = client_listener_find(sock->inject_listener, &sock->src_ipaddr,
1131 cprintf(listener, "ERROR: No such client %s\n", argv[0]);
1134 sock->inject_client = client;
1139 static int command_inject_file(rad_listen_t *listener, int argc, char *argv[])
1141 static int inject_id = 0;
1143 fr_command_socket_t *sock = listener->data;
1145 REQUEST *request = NULL;
1146 RADIUS_PACKET *packet;
1149 RAD_REQUEST_FUNP fun = NULL;
1153 cprintf(listener, "ERROR: You must specify <input-file> <output-file>\n");
1158 * Output files always go to the logging directory.
1160 snprintf(buffer, sizeof(buffer), "%s/%s", radlog_dir, argv[1]);
1162 fp = fopen(argv[0], "r");
1164 cprintf(listener, "ERROR: Failed opening %s: %s\n",
1165 argv[0], strerror(errno));
1169 vp = readvp2(fp, &filedone, "");
1172 cprintf(listener, "ERROR: Failed reading attributes from %s: %s\n",
1173 argv[0], fr_strerror());
1177 fake = rad_malloc(sizeof(*fake));
1178 memcpy(fake, sock->inject_listener, sizeof(*fake));
1181 * Re-write the IO for the listener.
1183 fake->encode = null_socket_dencode;
1184 fake->decode = null_socket_dencode;
1185 fake->send = null_socket_send;
1187 packet = rad_alloc(0);
1188 packet->src_ipaddr = sock->src_ipaddr;
1189 packet->src_port = 0;
1191 packet->dst_ipaddr = sock->dst_ipaddr;
1192 packet->dst_port = sock->dst_port;
1194 packet->id = inject_id++;
1196 if (fake->type == RAD_LISTEN_AUTH) {
1197 packet->code = PW_AUTHENTICATION_REQUEST;
1198 fun = rad_authenticate;
1201 #ifdef WITH_ACCOUNTING
1202 packet->code = PW_ACCOUNTING_REQUEST;
1203 fun = rad_accounting;
1205 cprintf(listener, "ERROR: This server was built without accounting support.\n");
1212 if (!received_request(fake, packet, &request, sock->inject_client)) {
1213 cprintf(listener, "ERROR: Failed to inject request. See log file for details\n");
1220 * Remember what the output file is, and remember to
1221 * delete the fake listener when done.
1223 request_data_add(request, null_socket_send, 0, strdup(buffer), free);
1224 request_data_add(request, null_socket_send, 1, fake, free);
1227 request->radlog(L_DBG, 0, request,
1228 "Injected %s packet from host %s port 0 code=%d, id=%d",
1229 fr_packet_codes[packet->code],
1230 inet_ntop(packet->src_ipaddr.af,
1231 &packet->src_ipaddr.ipaddr,
1232 buffer, sizeof(buffer)),
1233 packet->code, packet->id);
1235 for (vp = packet->vps; vp != NULL; vp = vp->next) {
1236 vp_prints(buffer, sizeof(buffer), vp);
1237 request->radlog(L_DBG, 0, request, "\t%s", buffer);
1242 * And go process it.
1244 thread_pool_addrequest(request, fun);
1250 static fr_command_table_t command_table_inject[] = {
1252 "inject to <ipaddr> <port> - Inject packets to the destination IP and port.",
1253 command_inject_to, NULL },
1256 "inject from <ipaddr> - Inject packets as if they came from <ipaddr>",
1257 command_inject_from, NULL },
1260 "inject file <input-file> <output-file> - Inject packet from input-file>, with results sent to <output-file>",
1261 command_inject_file, NULL },
1263 { NULL, 0, NULL, NULL, NULL }
1266 static fr_command_table_t command_table_debug[] = {
1267 { "condition", FR_WRITE,
1268 "debug condition [condition] - Enable debugging for requests matching [condition]",
1269 command_debug_condition, NULL },
1271 { "level", FR_WRITE,
1272 "debug level <number> - Set debug level to <number>. Higher is more debugging.",
1273 command_debug_level, NULL },
1276 "debug file [filename] - Send all debugging output to [filename]",
1277 command_debug_file, NULL },
1279 { NULL, 0, NULL, NULL, NULL }
1282 static fr_command_table_t command_table_show_debug[] = {
1283 { "condition", FR_READ,
1284 "show debug condition - Shows current debugging condition.",
1285 command_show_debug_condition, NULL },
1288 "show debug level - Shows current debugging level.",
1289 command_show_debug_level, NULL },
1292 "show debug file - Shows current debugging file.",
1293 command_show_debug_file, NULL },
1295 { NULL, 0, NULL, NULL, NULL }
1298 static fr_command_table_t command_table_show_module[] = {
1299 { "config", FR_READ,
1300 "show module config <module> - show configuration for given module",
1301 command_show_module_config, NULL },
1303 "show module flags <module> - show other module properties",
1304 command_show_module_flags, NULL },
1306 "show module list - shows list of loaded modules",
1307 command_show_modules, NULL },
1308 { "methods", FR_READ,
1309 "show module methods <module> - show sections where <module> may be used",
1310 command_show_module_methods, NULL },
1312 { NULL, 0, NULL, NULL, NULL }
1315 static fr_command_table_t command_table_show_client[] = {
1316 { "config", FR_READ,
1317 "show client config <ipaddr> "
1321 "- show configuration for given client",
1322 command_show_client_config, NULL },
1324 "show client list - shows list of global clients",
1325 command_show_clients, NULL },
1327 { NULL, 0, NULL, NULL, NULL }
1331 static fr_command_table_t command_table_show_home[] = {
1332 { "config", FR_READ,
1333 "show home_server config <ipaddr> <port> - show configuration for given home server",
1334 command_show_home_server_config, NULL },
1336 "show home_server list - shows list of home servers",
1337 command_show_home_servers, NULL },
1339 "show home_server state <ipaddr> <port> - shows state of given home server",
1340 command_show_home_server_state, NULL },
1342 { NULL, 0, NULL, NULL, NULL }
1347 static fr_command_table_t command_table_show[] = {
1348 { "client", FR_READ,
1349 "show client <command> - do sub-command of client",
1350 NULL, command_table_show_client },
1352 "show debug <command> - show debug properties",
1353 NULL, command_table_show_debug },
1355 { "home_server", FR_READ,
1356 "show home_server <command> - do sub-command of home_server",
1357 NULL, command_table_show_home },
1359 { "module", FR_READ,
1360 "show module <command> - do sub-command of module",
1361 NULL, command_table_show_module },
1362 { "uptime", FR_READ,
1363 "show uptime - shows time at which server started",
1364 command_uptime, NULL },
1365 { "version", FR_READ,
1366 "show version - Prints version of the running server",
1367 command_show_version, NULL },
1369 "show xml <reference> - Prints out configuration as XML",
1370 command_show_xml, NULL },
1371 { NULL, 0, NULL, NULL, NULL }
1375 static int command_set_module_config(rad_listen_t *listener, int argc, char *argv[])
1380 module_instance_t *mi;
1381 const CONF_PARSER *variables;
1385 cprintf(listener, "ERROR: No module name or variable was given\n");
1389 cs = cf_section_find("modules");
1392 mi = find_module_instance(cs, argv[0], 0);
1394 cprintf(listener, "ERROR: No such module \"%s\"\n", argv[0]);
1398 if ((mi->entry->module->type & RLM_TYPE_HUP_SAFE) == 0) {
1399 cprintf(listener, "ERROR: Cannot change configuration of module as it is cannot be HUP'd.\n");
1403 variables = cf_section_parse_table(mi->cs);
1405 cprintf(listener, "ERROR: Cannot find configuration for module\n");
1410 for (i = 0; variables[i].name != NULL; i++) {
1412 * FIXME: Recurse into sub-types somehow...
1414 if (variables[i].type == PW_TYPE_SUBSECTION) continue;
1416 if (strcmp(variables[i].name, argv[1]) == 0) {
1423 cprintf(listener, "ERROR: No such variable \"%s\"\n", argv[1]);
1427 i = rcode; /* just to be safe */
1430 * It's not part of the dynamic configuration. The module
1431 * needs to re-parse && validate things.
1433 if (variables[i].data) {
1434 cprintf(listener, "ERROR: Variable cannot be dynamically updated\n");
1438 data = ((char *) mi->insthandle) + variables[i].offset;
1440 cp = cf_pair_find(mi->cs, argv[1]);
1444 * Replace the OLD value in the configuration file with
1447 * FIXME: Parse argv[2] depending on it's data type!
1448 * If it's a string, look for leading single/double quotes,
1449 * end then call tokenize functions???
1451 cf_pair_replace(mi->cs, cp, argv[2]);
1453 rcode = cf_item_parse(mi->cs, argv[1], variables[i].type,
1456 cprintf(listener, "ERROR: Failed to parse value\n");
1460 return 1; /* success */
1463 static int command_set_module_status(rad_listen_t *listener, int argc, char *argv[])
1466 module_instance_t *mi;
1469 cprintf(listener, "ERROR: No module name or status was given\n");
1473 cs = cf_section_find("modules");
1476 mi = find_module_instance(cs, argv[0], 0);
1478 cprintf(listener, "ERROR: No such module \"%s\"\n", argv[0]);
1483 if (strcmp(argv[1], "alive") == 0) {
1486 } else if (strcmp(argv[1], "dead") == 0) {
1490 cprintf(listener, "ERROR: Unknown status \"%s\"\n", argv[2]);
1494 return 1; /* success */
1497 static int command_print_stats(rad_listen_t *listener, fr_stats_t *stats,
1500 cprintf(listener, "\trequests\t%d\n", stats->total_requests);
1501 cprintf(listener, "\tresponses\t%d\n", stats->total_responses);
1504 cprintf(listener, "\taccepts\t\t%d\n",
1505 stats->total_access_accepts);
1506 cprintf(listener, "\trejects\t\t%d\n",
1507 stats->total_access_rejects);
1508 cprintf(listener, "\tchallenges\t%d\n",
1509 stats->total_access_challenges);
1512 cprintf(listener, "\tdup\t\t%d\n", stats->total_dup_requests);
1513 cprintf(listener, "\tinvalid\t\t%d\n", stats->total_invalid_requests);
1514 cprintf(listener, "\tmalformed\t%d\n", stats->total_malformed_requests);
1515 cprintf(listener, "\tbad_signature\t%d\n", stats->total_bad_authenticators);
1516 cprintf(listener, "\tdropped\t\t%d\n", stats->total_packets_dropped);
1517 cprintf(listener, "\tunknown_types\t%d\n", stats->total_unknown_types);
1524 static int command_stats_home_server(rad_listen_t *listener, int argc, char *argv[])
1529 cprintf(listener, "ERROR: Must specify [auth/acct] OR <ipaddr> <port>\n");
1534 #ifdef WITH_ACCOUNTING
1535 if (strcmp(argv[0], "acct") == 0) {
1536 return command_print_stats(listener,
1537 &proxy_acct_stats, 0);
1540 if (strcmp(argv[0], "auth") == 0) {
1541 return command_print_stats(listener,
1542 &proxy_auth_stats, 1);
1545 cprintf(listener, "ERROR: Should specify [auth/acct]\n");
1549 home = get_home_server(listener, argc, argv);
1554 command_print_stats(listener, &home->stats,
1555 (home->type == HOME_TYPE_AUTH));
1556 cprintf(listener, "\toutstanding\t%d\n", home->currently_outstanding);
1561 static int command_stats_client(rad_listen_t *listener, int argc, char *argv[])
1567 cprintf(listener, "ERROR: Must specify [auth/acct]\n");
1571 if (strcmp(argv[0], "auth") == 0) {
1574 } else if (strcmp(argv[0], "acct") == 0) {
1575 #ifdef WITH_ACCOUNTING
1578 cprintf(listener, "ERROR: This server was built without accounting support.\n");
1583 cprintf(listener, "ERROR: Unknown statistics type\n");
1588 * Global results for all client.
1591 #ifdef WITH_ACCOUNTING
1593 return command_print_stats(listener,
1594 &radius_acct_stats, auth);
1597 return command_print_stats(listener, &radius_auth_stats, auth);
1600 client = get_client(listener, argc - 1, argv + 1);
1605 #ifdef WITH_ACCOUNTING
1607 return command_print_stats(listener, client->acct, auth);
1611 return command_print_stats(listener, client->auth, auth);
1615 static int command_add_client_file(rad_listen_t *listener, int argc, char *argv[])
1620 cprintf(listener, "ERROR: <file> is required\n");
1625 * Read the file and generate the client.
1627 c = client_read(argv[0], FALSE, FALSE);
1629 cprintf(listener, "ERROR: Unknown error reading client file.\n");
1633 if (!client_add(NULL, c)) {
1634 cprintf(listener, "ERROR: Unknown error inserting new client.\n");
1643 static fr_command_table_t command_table_add_client[] = {
1645 "add client file <filename> - Add new client definition from <filename>",
1646 command_add_client_file, NULL },
1648 { NULL, 0, NULL, NULL, NULL }
1652 static fr_command_table_t command_table_add[] = {
1653 { "client", FR_WRITE,
1654 "add client <command> - Add client configuration commands",
1655 NULL, command_table_add_client },
1657 { NULL, 0, NULL, NULL, NULL }
1662 static fr_command_table_t command_table_set_home[] = {
1663 { "state", FR_WRITE,
1664 "set home_server state <ipaddr> <port> [alive|dead] - set state for given home server",
1665 command_set_home_server_state, NULL },
1667 { NULL, 0, NULL, NULL, NULL }
1671 static fr_command_table_t command_table_set_module[] = {
1672 { "config", FR_WRITE,
1673 "set module config <module> variable value - set configuration for <module>",
1674 command_set_module_config, NULL },
1676 { "status", FR_WRITE,
1677 "set module status [alive|dead] - set the module to be alive or dead (always return \"fail\")",
1678 command_set_module_status, NULL },
1680 { NULL, 0, NULL, NULL, NULL }
1684 static fr_command_table_t command_table_set[] = {
1685 { "module", FR_WRITE,
1686 "set module <command> - set module commands",
1687 NULL, command_table_set_module },
1689 { "home_server", FR_WRITE,
1690 "set home_server <command> - set home server commands",
1691 NULL, command_table_set_home },
1694 { NULL, 0, NULL, NULL, NULL }
1698 static fr_command_table_t command_table_stats[] = {
1699 { "client", FR_READ,
1700 "stats client [auth/acct] <ipaddr> "
1704 "- show statistics for given client, or for all clients (auth or acct)",
1705 command_stats_client, NULL },
1707 { "home_server", FR_READ,
1708 "stats home_server [<ipaddr>/auth/acct] <port> - show statistics for given home server (ipaddr and port), or for all home servers (auth or acct)",
1709 command_stats_home_server, NULL },
1712 { NULL, 0, NULL, NULL, NULL }
1715 static fr_command_table_t command_table[] = {
1716 { "add", FR_WRITE, NULL, NULL, command_table_add },
1717 { "debug", FR_WRITE,
1718 "debug <command> - debugging commands",
1719 NULL, command_table_debug },
1721 "hup [module] - sends a HUP signal to the server, or optionally to one module",
1722 command_hup, NULL },
1723 { "inject", FR_WRITE,
1724 "inject <command> - commands to inject packets into a running server",
1725 NULL, command_table_inject },
1726 { "reconnect", FR_READ,
1727 "reconnect - reconnect to a running server",
1728 NULL, NULL }, /* just here for "help" */
1729 { "terminate", FR_WRITE,
1730 "terminate - terminates the server, and cause it to exit",
1731 command_terminate, NULL },
1732 { "set", FR_WRITE, NULL, NULL, command_table_set },
1733 { "show", FR_READ, NULL, NULL, command_table_show },
1734 { "stats", FR_READ, NULL, NULL, command_table_stats },
1736 { NULL, 0, NULL, NULL, NULL }
1741 * Parse the unix domain sockets.
1743 * FIXME: TCP + SSL, after RadSec is in.
1745 static int command_socket_parse(CONF_SECTION *cs, rad_listen_t *this)
1747 fr_command_socket_t *sock;
1751 if (cf_section_parse(cs, sock, command_config) < 0) {
1755 #if defined(HAVE_GETPEEREID) || defined (SO_PEERCRED)
1756 if (sock->uid_name) {
1759 pw = getpwnam(sock->uid_name);
1761 radlog(L_ERR, "Failed getting uid for %s: %s",
1762 sock->uid_name, strerror(errno));
1766 sock->uid = pw->pw_uid;
1769 if (sock->gid_name) {
1772 gr = getgrnam(sock->gid_name);
1774 radlog(L_ERR, "Failed getting gid for %s: %s",
1775 sock->gid_name, strerror(errno));
1778 sock->gid = gr->gr_gid;
1781 #else /* can't get uid or gid of connecting user */
1783 if (sock->uid_name || sock->gid_name) {
1784 radlog(L_ERR, "System does not support uid or gid authentication for sockets");
1790 if (!sock->mode_name) {
1791 sock->mode = FR_READ;
1793 sock->mode = fr_str2int(mode_names, sock->mode_name, 0);
1795 radlog(L_ERR, "Invalid mode name \"%s\"",
1802 * FIXME: check for absolute pathnames?
1803 * check for uid/gid on the other end...
1806 this->fd = fr_server_domain_socket(sock->path);
1814 static int command_socket_print(rad_listen_t *this, char *buffer, size_t bufsize)
1816 fr_command_socket_t *sock = this->data;
1818 snprintf(buffer, bufsize, "command file %s", sock->path);
1824 * String split routine. Splits an input string IN PLACE
1825 * into pieces, based on spaces.
1827 static int str2argv(char *str, char **argv, int max_argc)
1834 if (argc >= max_argc) return argc;
1837 * Chop out comments early.
1844 while ((*str == ' ') ||
1847 (*str == '\n')) *(str++) = '\0';
1849 if (!*str) return argc;
1851 if ((*str == '\'') || (*str == '"')) {
1855 token = gettoken((const char **) &p, buffer,
1857 if ((token != T_SINGLE_QUOTED_STRING) &&
1858 (token != T_DOUBLE_QUOTED_STRING)) {
1862 len = strlen(buffer);
1863 if (len >= (size_t) (p - str)) {
1867 memcpy(str, buffer, len + 1);
1880 (*str != '\n')) str++;
1886 static void print_help(rad_listen_t *listener,
1887 fr_command_table_t *table, int recursive)
1891 for (i = 0; table[i].command != NULL; i++) {
1892 if (table[i].help) {
1893 cprintf(listener, "%s\n",
1896 cprintf(listener, "%s <command> - do sub-command of %s\n",
1897 table[i].command, table[i].command);
1900 if (recursive && table[i].table) {
1901 print_help(listener, table[i].table, recursive);
1906 #define MAX_ARGV (16)
1909 * Check if an incoming request is "ok"
1911 * It takes packets, not requests. It sees if the packet looks
1912 * OK. If so, it does a number of sanity checks on it.
1914 static int command_domain_recv(rad_listen_t *listener,
1915 UNUSED RAD_REQUEST_FUNP *pfun,
1916 UNUSED REQUEST **prequest)
1921 char *my_argv[MAX_ARGV], **argv;
1922 fr_command_table_t *table;
1923 fr_command_socket_t *co = listener->data;
1932 len = recv(listener->fd, co->buffer + co->offset,
1933 sizeof(co->buffer) - co->offset - 1, 0);
1934 if (len == 0) goto close_socket; /* clean close */
1937 if ((errno == EAGAIN) || (errno == EINTR)) {
1946 if ((co->offset == 0) && (co->buffer[0] == 0x04)) {
1948 command_close_socket(listener);
1953 * See if there are multiple lines in the buffer.
1955 p = co->buffer + co->offset;
1958 for (c = 0; c < len; c++) {
1959 if ((*p == '\r') || (*p == '\n')) {
1964 * FIXME: do real buffering...
1965 * handling of CTRL-C, etc.
1970 * \r \n followed by ASCII...
1981 * Saw CR/LF. Set next element, and exit.
1984 co->next = p - co->buffer;
1988 if (co->offset >= (ssize_t) (sizeof(co->buffer) - 1)) {
1989 radlog(L_ERR, "Line too long!");
1996 argc = str2argv(co->buffer, my_argv, MAX_ARGV);
1997 if (argc == 0) goto do_next; /* empty strings are OK */
2000 cprintf(listener, "ERROR: Failed parsing command.\n");
2006 for (len = 0; len <= co->offset; len++) {
2007 if (co->buffer[len] < 0x20) {
2008 co->buffer[len] = '\0';
2014 * Hard-code exit && quit.
2016 if ((strcmp(argv[0], "exit") == 0) ||
2017 (strcmp(argv[0], "quit") == 0)) goto close_socket;
2021 if (strcmp(argv[0], "login") != 0) {
2022 cprintf(listener, "ERROR: Login required\n");
2027 cprintf(listener, "ERROR: login <user> <password>\n");
2032 * FIXME: Generate && process fake RADIUS request.
2034 if ((strcmp(argv[1], "root") == 0) &&
2035 (strcmp(argv[2], "password") == 0)) {
2036 strlcpy(co->user, argv[1], sizeof(co->user));
2040 cprintf(listener, "ERROR: Login incorrect\n");
2045 table = command_table;
2048 for (i = 0; table[i].command != NULL; i++) {
2049 if (strcmp(table[i].command, argv[0]) == 0) {
2051 * Check permissions.
2053 if (((co->mode & FR_WRITE) == 0) &&
2054 ((table[i].mode & FR_WRITE) != 0)) {
2055 cprintf(listener, "ERROR: You do not have write permission. See \"mode = rw\" in %s\n", co->path);
2059 if (table[i].table) {
2061 * This is the last argument, but
2062 * there's a sub-table. Print help.
2066 table = table[i].table;
2072 table = table[i].table;
2076 if ((argc == 2) && (strcmp(argv[1], "?") == 0)) goto do_help;
2078 if (!table[i].func) {
2079 cprintf(listener, "ERROR: Invalid command\n");
2084 rcode = table[i].func(listener,
2085 argc - 1, argv + 1);
2094 if ((strcmp(argv[0], "help") == 0) ||
2095 (strcmp(argv[0], "?") == 0)) {
2099 if ((argc > 1) && (strcmp(argv[1], "-r") == 0)) {
2105 print_help(listener, table, recursive);
2109 cprintf(listener, "ERROR: Unknown command \"%s\"\n",
2114 cprintf(listener, "radmin> ");
2116 if (co->next <= co->offset) {
2119 memmove(co->buffer, co->buffer + co->next,
2120 co->offset - co->next);
2121 co->offset -= co->next;
2128 static int command_domain_accept(rad_listen_t *listener,
2129 UNUSED RAD_REQUEST_FUNP *pfun,
2130 UNUSED REQUEST **prequest)
2136 struct sockaddr_storage src;
2137 fr_command_socket_t *sock = listener->data;
2139 salen = sizeof(src);
2141 DEBUG2(" ... new connection request on command socket.");
2146 newfd = accept(listener->fd, (struct sockaddr *) &src, &salen);
2149 * Non-blocking sockets must handle this.
2151 if (errno == EWOULDBLOCK) {
2155 DEBUG2(" ... failed to accept connection.");
2160 * Perform user authentication.
2162 if (sock->uid_name || sock->gid_name) {
2166 if (getpeereid(listener->fd, &uid, &gid) < 0) {
2167 radlog(L_ERR, "Failed getting peer credentials for %s: %s",
2168 sock->path, strerror(errno));
2173 if (sock->uid_name && (sock->uid != uid)) {
2174 radlog(L_ERR, "Unauthorized connection to %s from uid %ld",
2175 sock->path, (long int) uid);
2180 if (sock->gid_name && (sock->gid != gid)) {
2181 radlog(L_ERR, "Unauthorized connection to %s from gid %ld",
2182 sock->path, (long int) gid);
2189 * Write 32-bit magic number && version information.
2191 magic = htonl(0xf7eead15);
2192 if (write(newfd, &magic, 4) < 0) {
2193 radlog(L_ERR, "Failed writing initial data to socket: %s",
2198 magic = htonl(1); /* protocol version */
2199 if (write(newfd, &magic, 4) < 0) {
2200 radlog(L_ERR, "Failed writing initial data to socket: %s",
2208 * Add the new listener.
2210 this = listen_alloc(listener->type);
2211 if (!this) return 0;
2214 * Copy everything, including the pointer to the socket
2218 memcpy(this, listener, sizeof(*this));
2219 this->status = RAD_LISTEN_STATUS_INIT;
2221 this->data = sock; /* fix it back */
2224 sock->user[0] = '\0';
2225 sock->path = ((fr_command_socket_t *) listener->data)->path;
2226 sock->mode = ((fr_command_socket_t *) listener->data)->mode;
2229 this->recv = command_domain_recv;
2232 * Tell the event loop that we have a new FD
2241 * Send an authentication response packet
2243 static int command_domain_send(UNUSED rad_listen_t *listener,
2244 UNUSED REQUEST *request)
2250 static int command_socket_encode(UNUSED rad_listen_t *listener,
2251 UNUSED REQUEST *request)
2257 static int command_socket_decode(UNUSED rad_listen_t *listener,
2258 UNUSED REQUEST *request)
2263 #endif /* WITH_COMMAND_SOCKET */