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[])
807 cprintf(listener, "ERROR: Must specify <ipaddr>\n");
811 if (ip_hton(argv[0], AF_UNSPEC, &ipaddr) < 0) {
812 cprintf(listener, "ERROR: Failed parsing IP address; %s\n",
817 client = client_find(NULL, &ipaddr);
819 cprintf(listener, "ERROR: No such client\n");
827 static int command_show_client_config(rad_listen_t *listener, int argc, char *argv[])
832 client = get_client(listener, argc, argv);
837 if (!client->cs) return 1;
839 fp = fdopen(dup(listener->fd), "a");
841 cprintf(listener, "ERROR: Can't dup %s\n", strerror(errno));
845 cf_section2file(fp, client->cs);
852 static home_server *get_home_server(rad_listen_t *listener, int argc, char *argv[])
859 cprintf(listener, "ERROR: Must specify <ipaddr> <port>\n");
863 if (ip_hton(argv[0], AF_UNSPEC, &ipaddr) < 0) {
864 cprintf(listener, "ERROR: Failed parsing IP address; %s\n",
869 port = atoi(argv[1]);
871 home = home_server_find(&ipaddr, port);
873 cprintf(listener, "ERROR: No such home server\n");
880 static int command_show_home_server_config(rad_listen_t *listener, int argc, char *argv[])
885 home = get_home_server(listener, argc, argv);
890 if (!home->cs) return 1;
892 fp = fdopen(dup(listener->fd), "a");
894 cprintf(listener, "ERROR: Can't dup %s\n", strerror(errno));
898 cf_section2file(fp, home->cs);
904 extern void revive_home_server(void *ctx);
905 extern void mark_home_server_dead(home_server *home, struct timeval *when);
907 static int command_set_home_server_state(rad_listen_t *listener, int argc, char *argv[])
912 cprintf(listener, "ERROR: Must specify <ipaddr> <port> <state>\n");
916 home = get_home_server(listener, argc, argv);
921 if (strcmp(argv[2], "alive") == 0) {
922 revive_home_server(home);
924 } else if (strcmp(argv[2], "dead") == 0) {
927 gettimeofday(&now, NULL); /* we do this WAY too ofetn */
928 mark_home_server_dead(home, &now);
931 cprintf(listener, "ERROR: Unknown state \"%s\"\n", argv[2]);
938 static int command_show_home_server_state(rad_listen_t *listener, int argc, char *argv[])
942 home = get_home_server(listener, argc, argv);
947 switch (home->state) {
948 case HOME_STATE_ALIVE:
949 cprintf(listener, "alive\n");
952 case HOME_STATE_IS_DEAD:
953 cprintf(listener, "dead\n");
956 case HOME_STATE_ZOMBIE:
957 cprintf(listener, "zombie\n");
961 cprintf(listener, "unknown\n");
970 * For encode/decode stuff
972 static int null_socket_dencode(UNUSED rad_listen_t *listener, UNUSED REQUEST *request)
977 static int null_socket_send(UNUSED rad_listen_t *listener, REQUEST *request)
983 output_file = request_data_reference(request, null_socket_send, 0);
985 radlog(L_ERR, "WARNING: No output file for injected packet %d",
990 fp = fopen(output_file, "w");
992 radlog(L_ERR, "Failed to send injected file to %s: %s",
993 output_file, strerror(errno));
997 if (request->reply->code != 0) {
998 const char *what = "reply";
1001 if (request->reply->code < FR_MAX_PACKET_CODE) {
1002 what = fr_packet_codes[request->reply->code];
1005 fprintf(fp, "%s\n", what);
1008 request->radlog(L_DBG, 0, request,
1009 "Injected %s packet to host %s port 0 code=%d, id=%d",
1011 inet_ntop(request->reply->src_ipaddr.af,
1012 &request->reply->src_ipaddr.ipaddr,
1013 buffer, sizeof(buffer)),
1014 request->reply->code, request->reply->id);
1017 for (vp = request->reply->vps; vp != NULL; vp = vp->next) {
1018 vp_prints(buffer, sizeof(buffer), vp);
1019 fprintf(fp, "%s\n", buffer);
1021 request->radlog(L_DBG, 0, request, "\t%s",
1031 static int command_inject_to(rad_listen_t *listener, int argc, char *argv[])
1034 RAD_LISTEN_TYPE type;
1035 fr_command_socket_t *sock = listener->data;
1037 rad_listen_t *found = NULL;
1040 cprintf(listener, "ERROR: Must specify [auth/acct]\n");
1044 if (strcmp(argv[0], "auth") == 0) {
1045 type = RAD_LISTEN_AUTH;
1047 } else if (strcmp(argv[0], "acct") == 0) {
1048 #ifdef WITH_ACCOUNTING
1049 type = RAD_LISTEN_ACCT;
1051 cprintf(listener, "ERROR: This server was built without accounting support.\n");
1056 cprintf(listener, "ERROR: Unknown socket type\n");
1061 cprintf(listener, "ERROR: No <ipaddr> <port> was given\n");
1066 * FIXME: Look for optional arg 4, and bind interface.
1069 if (ip_hton(argv[1], AF_UNSPEC, &ipaddr) < 0) {
1070 cprintf(listener, "ERROR: Failed parsing IP address; %s\n",
1074 port = atoi(argv[2]);
1076 found = listener_find_byipaddr(&ipaddr, port);
1078 cprintf(listener, "ERROR: Could not find matching listener\n");
1082 sock->inject_listener = found;
1083 sock->dst_ipaddr = ipaddr;
1084 sock->dst_port = port;
1089 static int command_inject_from(rad_listen_t *listener, int argc, char *argv[])
1092 fr_command_socket_t *sock = listener->data;
1095 cprintf(listener, "ERROR: No <ipaddr> was given\n");
1099 if (!sock->inject_listener) {
1100 cprintf(listener, "ERROR: You must specify \"inject to\" before using \"inject from\"\n");
1104 sock->src_ipaddr.af = AF_UNSPEC;
1105 if (ip_hton(argv[0], AF_UNSPEC, &sock->src_ipaddr) < 0) {
1106 cprintf(listener, "ERROR: Failed parsing IP address; %s\n",
1111 client = client_listener_find(sock->inject_listener, &sock->src_ipaddr,
1114 cprintf(listener, "ERROR: No such client %s\n", argv[0]);
1117 sock->inject_client = client;
1122 static int command_inject_file(rad_listen_t *listener, int argc, char *argv[])
1124 static int inject_id = 0;
1126 fr_command_socket_t *sock = listener->data;
1128 REQUEST *request = NULL;
1129 RADIUS_PACKET *packet;
1132 RAD_REQUEST_FUNP fun = NULL;
1136 cprintf(listener, "ERROR: You must specify <input-file> <output-file>\n");
1141 * Output files always go to the logging directory.
1143 snprintf(buffer, sizeof(buffer), "%s/%s", radlog_dir, argv[1]);
1145 fp = fopen(argv[0], "r");
1147 cprintf(listener, "ERROR: Failed opening %s: %s\n",
1148 argv[0], strerror(errno));
1152 vp = readvp2(fp, &filedone, "");
1155 cprintf(listener, "ERROR: Failed reading attributes from %s: %s\n",
1156 argv[0], fr_strerror());
1160 fake = rad_malloc(sizeof(*fake));
1161 memcpy(fake, sock->inject_listener, sizeof(*fake));
1164 * Re-write the IO for the listener.
1166 fake->encode = null_socket_dencode;
1167 fake->decode = null_socket_dencode;
1168 fake->send = null_socket_send;
1170 packet = rad_alloc(0);
1171 packet->src_ipaddr = sock->src_ipaddr;
1172 packet->src_port = 0;
1174 packet->dst_ipaddr = sock->dst_ipaddr;
1175 packet->dst_port = sock->dst_port;
1177 packet->id = inject_id++;
1179 if (fake->type == RAD_LISTEN_AUTH) {
1180 packet->code = PW_AUTHENTICATION_REQUEST;
1181 fun = rad_authenticate;
1184 #ifdef WITH_ACCOUNTING
1185 packet->code = PW_ACCOUNTING_REQUEST;
1186 fun = rad_accounting;
1188 cprintf(listener, "ERROR: This server was built without accounting support.\n");
1195 if (!received_request(fake, packet, &request, sock->inject_client)) {
1196 cprintf(listener, "ERROR: Failed to inject request. See log file for details\n");
1203 * Remember what the output file is, and remember to
1204 * delete the fake listener when done.
1206 request_data_add(request, null_socket_send, 0, strdup(buffer), free);
1207 request_data_add(request, null_socket_send, 1, fake, free);
1210 request->radlog(L_DBG, 0, request,
1211 "Injected %s packet from host %s port 0 code=%d, id=%d",
1212 fr_packet_codes[packet->code],
1213 inet_ntop(packet->src_ipaddr.af,
1214 &packet->src_ipaddr.ipaddr,
1215 buffer, sizeof(buffer)),
1216 packet->code, packet->id);
1218 for (vp = packet->vps; vp != NULL; vp = vp->next) {
1219 vp_prints(buffer, sizeof(buffer), vp);
1220 request->radlog(L_DBG, 0, request, "\t%s", buffer);
1225 * And go process it.
1227 thread_pool_addrequest(request, fun);
1233 static fr_command_table_t command_table_inject[] = {
1235 "inject to <ipaddr> <port> - Inject packets to the destination IP and port.",
1236 command_inject_to, NULL },
1239 "inject from <ipaddr> - Inject packets as if they came from <ipaddr>",
1240 command_inject_from, NULL },
1243 "inject file <input-file> <output-file> - Inject packet from input-file>, with results sent to <output-file>",
1244 command_inject_file, NULL },
1246 { NULL, 0, NULL, NULL, NULL }
1249 static fr_command_table_t command_table_debug[] = {
1250 { "condition", FR_WRITE,
1251 "debug condition [condition] - Enable debugging for requests matching [condition]",
1252 command_debug_condition, NULL },
1254 { "level", FR_WRITE,
1255 "debug level <number> - Set debug level to <number>. Higher is more debugging.",
1256 command_debug_level, NULL },
1259 "debug file [filename] - Send all debugging output to [filename]",
1260 command_debug_file, NULL },
1262 { NULL, 0, NULL, NULL, NULL }
1265 static fr_command_table_t command_table_show_debug[] = {
1266 { "condition", FR_READ,
1267 "show debug condition - Shows current debugging condition.",
1268 command_show_debug_condition, NULL },
1271 "show debug level - Shows current debugging level.",
1272 command_show_debug_level, NULL },
1275 "show debug file - Shows current debugging file.",
1276 command_show_debug_file, NULL },
1278 { NULL, 0, NULL, NULL, NULL }
1281 static fr_command_table_t command_table_show_module[] = {
1282 { "config", FR_READ,
1283 "show module config <module> - show configuration for given module",
1284 command_show_module_config, NULL },
1286 "show module flags <module> - show other module properties",
1287 command_show_module_flags, NULL },
1289 "show module list - shows list of loaded modules",
1290 command_show_modules, NULL },
1291 { "methods", FR_READ,
1292 "show module methods <module> - show sections where <module> may be used",
1293 command_show_module_methods, NULL },
1295 { NULL, 0, NULL, NULL, NULL }
1298 static fr_command_table_t command_table_show_client[] = {
1299 { "config", FR_READ,
1300 "show client config <ipaddr> - show configuration for given client",
1301 command_show_client_config, NULL },
1303 "show client list - shows list of global clients",
1304 command_show_clients, NULL },
1306 { NULL, 0, NULL, NULL, NULL }
1310 static fr_command_table_t command_table_show_home[] = {
1311 { "config", FR_READ,
1312 "show home_server config <ipaddr> <port> - show configuration for given home server",
1313 command_show_home_server_config, NULL },
1315 "show home_server list - shows list of home servers",
1316 command_show_home_servers, NULL },
1318 "show home_server state <ipaddr> <port> - shows state of given home server",
1319 command_show_home_server_state, NULL },
1321 { NULL, 0, NULL, NULL, NULL }
1326 static fr_command_table_t command_table_show[] = {
1327 { "client", FR_READ,
1328 "show client <command> - do sub-command of client",
1329 NULL, command_table_show_client },
1331 "show debug <command> - show debug properties",
1332 NULL, command_table_show_debug },
1334 { "home_server", FR_READ,
1335 "show home_server <command> - do sub-command of home_server",
1336 NULL, command_table_show_home },
1338 { "module", FR_READ,
1339 "show module <command> - do sub-command of module",
1340 NULL, command_table_show_module },
1341 { "uptime", FR_READ,
1342 "show uptime - shows time at which server started",
1343 command_uptime, NULL },
1344 { "version", FR_READ,
1345 "show version - Prints version of the running server",
1346 command_show_version, NULL },
1348 "show xml <reference> - Prints out configuration as XML",
1349 command_show_xml, NULL },
1350 { NULL, 0, NULL, NULL, NULL }
1354 static int command_set_module_config(rad_listen_t *listener, int argc, char *argv[])
1359 module_instance_t *mi;
1360 const CONF_PARSER *variables;
1364 cprintf(listener, "ERROR: No module name or variable was given\n");
1368 cs = cf_section_find("modules");
1371 mi = find_module_instance(cs, argv[0], 0);
1373 cprintf(listener, "ERROR: No such module \"%s\"\n", argv[0]);
1377 if ((mi->entry->module->type & RLM_TYPE_HUP_SAFE) == 0) {
1378 cprintf(listener, "ERROR: Cannot change configuration of module as it is cannot be HUP'd.\n");
1382 variables = cf_section_parse_table(mi->cs);
1384 cprintf(listener, "ERROR: Cannot find configuration for module\n");
1389 for (i = 0; variables[i].name != NULL; i++) {
1391 * FIXME: Recurse into sub-types somehow...
1393 if (variables[i].type == PW_TYPE_SUBSECTION) continue;
1395 if (strcmp(variables[i].name, argv[1]) == 0) {
1402 cprintf(listener, "ERROR: No such variable \"%s\"\n", argv[1]);
1406 i = rcode; /* just to be safe */
1409 * It's not part of the dynamic configuration. The module
1410 * needs to re-parse && validate things.
1412 if (variables[i].data) {
1413 cprintf(listener, "ERROR: Variable cannot be dynamically updated\n");
1417 data = ((char *) mi->insthandle) + variables[i].offset;
1419 cp = cf_pair_find(mi->cs, argv[1]);
1423 * Replace the OLD value in the configuration file with
1426 * FIXME: Parse argv[2] depending on it's data type!
1427 * If it's a string, look for leading single/double quotes,
1428 * end then call tokenize functions???
1430 cf_pair_replace(mi->cs, cp, argv[2]);
1432 rcode = cf_item_parse(mi->cs, argv[1], variables[i].type,
1435 cprintf(listener, "ERROR: Failed to parse value\n");
1439 return 1; /* success */
1442 static int command_print_stats(rad_listen_t *listener, fr_stats_t *stats,
1445 cprintf(listener, "\trequests\t%d\n", stats->total_requests);
1446 cprintf(listener, "\tresponses\t%d\n", stats->total_responses);
1449 cprintf(listener, "\taccepts\t\t%d\n",
1450 stats->total_access_accepts);
1451 cprintf(listener, "\trejects\t\t%d\n",
1452 stats->total_access_rejects);
1453 cprintf(listener, "\tchallenges\t%d\n",
1454 stats->total_access_challenges);
1457 cprintf(listener, "\tdup\t\t%d\n", stats->total_dup_requests);
1458 cprintf(listener, "\tinvalid\t\t%d\n", stats->total_invalid_requests);
1459 cprintf(listener, "\tmalformed\t%d\n", stats->total_malformed_requests);
1460 cprintf(listener, "\tbad_signature\t%d\n", stats->total_bad_authenticators);
1461 cprintf(listener, "\tdropped\t\t%d\n", stats->total_packets_dropped);
1462 cprintf(listener, "\tunknown_types\t%d\n", stats->total_unknown_types);
1469 static int command_stats_home_server(rad_listen_t *listener, int argc, char *argv[])
1474 cprintf(listener, "ERROR: Must specify [auth/acct] OR <ipaddr> <port>\n");
1479 #ifdef WITH_ACCOUNTING
1480 if (strcmp(argv[0], "acct") == 0) {
1481 return command_print_stats(listener,
1482 &proxy_acct_stats, 0);
1485 if (strcmp(argv[0], "auth") == 0) {
1486 return command_print_stats(listener,
1487 &proxy_auth_stats, 1);
1490 cprintf(listener, "ERROR: Should specify [auth/acct]\n");
1494 home = get_home_server(listener, argc, argv);
1499 command_print_stats(listener, &home->stats,
1500 (home->type == HOME_TYPE_AUTH));
1501 cprintf(listener, "\toutstanding\t%d\n", home->currently_outstanding);
1506 static int command_stats_client(rad_listen_t *listener, int argc, char *argv[])
1512 cprintf(listener, "ERROR: Must specify [auth/acct]\n");
1516 if (strcmp(argv[0], "auth") == 0) {
1519 } else if (strcmp(argv[0], "acct") == 0) {
1520 #ifdef WITH_ACCOUNTING
1523 cprintf(listener, "ERROR: This server was built without accounting support.\n");
1528 cprintf(listener, "ERROR: Unknown statistics type\n");
1533 * Global results for all client.
1536 #ifdef WITH_ACCOUNTING
1538 return command_print_stats(listener,
1539 &radius_acct_stats, auth);
1542 return command_print_stats(listener, &radius_auth_stats, auth);
1545 client = get_client(listener, argc - 1, argv + 1);
1550 #ifdef WITH_ACCOUNTING
1552 return command_print_stats(listener, client->acct, auth);
1556 return command_print_stats(listener, client->auth, auth);
1560 static int command_add_client_file(rad_listen_t *listener, int argc, char *argv[])
1565 cprintf(listener, "ERROR: <file> is required\n");
1570 * Read the file and generate the client.
1572 c = client_read(argv[0], FALSE, FALSE);
1574 cprintf(listener, "ERROR: Unknown error reading client file.\n");
1578 if (!client_add(NULL, c)) {
1579 cprintf(listener, "ERROR: Unknown error inserting new client.\n");
1588 static fr_command_table_t command_table_add_client[] = {
1590 "add client file <filename> - Add new client definition from <filename>",
1591 command_add_client_file, NULL },
1593 { NULL, 0, NULL, NULL, NULL }
1597 static fr_command_table_t command_table_add[] = {
1598 { "client", FR_WRITE,
1599 "add client <command> - Add client configuration commands",
1600 NULL, command_table_add_client },
1602 { NULL, 0, NULL, NULL, NULL }
1607 static fr_command_table_t command_table_set_home[] = {
1608 { "state", FR_WRITE,
1609 "set home_server state <ipaddr> <port> [alive|dead] - set state for given home server",
1610 command_set_home_server_state, NULL },
1612 { NULL, 0, NULL, NULL, NULL }
1616 static fr_command_table_t command_table_set_module[] = {
1617 { "config", FR_WRITE,
1618 "set module config <module> variable value - set configuration for <module>",
1619 command_set_module_config, NULL },
1621 { NULL, 0, NULL, NULL, NULL }
1625 static fr_command_table_t command_table_set[] = {
1626 { "module", FR_WRITE,
1627 "set module <command> - set module commands",
1628 NULL, command_table_set_module },
1630 { "home_server", FR_WRITE,
1631 "set home_server <command> - set home server commands",
1632 NULL, command_table_set_home },
1635 { NULL, 0, NULL, NULL, NULL }
1639 static fr_command_table_t command_table_stats[] = {
1640 { "client", FR_READ,
1641 "stats client [auth/acct] <ipaddr> - show statistics for given client, or for all clients (auth or acct)",
1642 command_stats_client, NULL },
1644 { "home_server", FR_READ,
1645 "stats home_server [<ipaddr>/auth/acct] <port> - show statistics for given home server (ipaddr and port), or for all home servers (auth or acct)",
1646 command_stats_home_server, NULL },
1649 { NULL, 0, NULL, NULL, NULL }
1652 static fr_command_table_t command_table[] = {
1653 { "add", FR_WRITE, NULL, NULL, command_table_add },
1654 { "debug", FR_WRITE,
1655 "debug <command> - debugging commands",
1656 NULL, command_table_debug },
1658 "hup [module] - sends a HUP signal to the server, or optionally to one module",
1659 command_hup, NULL },
1660 { "inject", FR_WRITE,
1661 "inject <command> - commands to inject packets into a running server",
1662 NULL, command_table_inject },
1663 { "reconnect", FR_READ,
1664 "reconnect - reconnect to a running server",
1665 NULL, NULL }, /* just here for "help" */
1666 { "terminate", FR_WRITE,
1667 "terminate - terminates the server, and cause it to exit",
1668 command_terminate, NULL },
1669 { "set", FR_WRITE, NULL, NULL, command_table_set },
1670 { "show", FR_READ, NULL, NULL, command_table_show },
1671 { "stats", FR_READ, NULL, NULL, command_table_stats },
1673 { NULL, 0, NULL, NULL, NULL }
1678 * Parse the unix domain sockets.
1680 * FIXME: TCP + SSL, after RadSec is in.
1682 static int command_socket_parse(CONF_SECTION *cs, rad_listen_t *this)
1684 fr_command_socket_t *sock;
1688 if (cf_section_parse(cs, sock, command_config) < 0) {
1692 #if defined(HAVE_GETPEEREID) || defined (SO_PEERCRED)
1693 if (sock->uid_name) {
1696 pw = getpwnam(sock->uid_name);
1698 radlog(L_ERR, "Failed getting uid for %s: %s",
1699 sock->uid_name, strerror(errno));
1703 sock->uid = pw->pw_uid;
1706 if (sock->gid_name) {
1709 gr = getgrnam(sock->gid_name);
1711 radlog(L_ERR, "Failed getting gid for %s: %s",
1712 sock->gid_name, strerror(errno));
1715 sock->gid = gr->gr_gid;
1718 #else /* can't get uid or gid of connecting user */
1720 if (sock->uid_name || sock->gid_name) {
1721 radlog(L_ERR, "System does not support uid or gid authentication for sockets");
1727 if (!sock->mode_name) {
1728 sock->mode = FR_READ;
1730 sock->mode = fr_str2int(mode_names, sock->mode_name, 0);
1732 radlog(L_ERR, "Invalid mode name \"%s\"",
1739 * FIXME: check for absolute pathnames?
1740 * check for uid/gid on the other end...
1743 this->fd = fr_server_domain_socket(sock->path);
1751 static int command_socket_print(rad_listen_t *this, char *buffer, size_t bufsize)
1753 fr_command_socket_t *sock = this->data;
1755 snprintf(buffer, bufsize, "command file %s", sock->path);
1761 * String split routine. Splits an input string IN PLACE
1762 * into pieces, based on spaces.
1764 static int str2argv(char *str, char **argv, int max_argc)
1771 if (argc >= max_argc) return argc;
1774 * Chop out comments early.
1781 while ((*str == ' ') ||
1784 (*str == '\n')) *(str++) = '\0';
1786 if (!*str) return argc;
1788 if ((*str == '\'') || (*str == '"')) {
1792 token = gettoken((const char **) &p, buffer,
1794 if ((token != T_SINGLE_QUOTED_STRING) &&
1795 (token != T_DOUBLE_QUOTED_STRING)) {
1799 len = strlen(buffer);
1800 if (len >= (size_t) (p - str)) {
1804 memcpy(str, buffer, len + 1);
1817 (*str != '\n')) str++;
1823 static void print_help(rad_listen_t *listener,
1824 fr_command_table_t *table, int recursive)
1828 for (i = 0; table[i].command != NULL; i++) {
1829 if (table[i].help) {
1830 cprintf(listener, "%s\n",
1833 cprintf(listener, "%s <command> - do sub-command of %s\n",
1834 table[i].command, table[i].command);
1837 if (recursive && table[i].table) {
1838 print_help(listener, table[i].table, recursive);
1843 #define MAX_ARGV (16)
1846 * Check if an incoming request is "ok"
1848 * It takes packets, not requests. It sees if the packet looks
1849 * OK. If so, it does a number of sanity checks on it.
1851 static int command_domain_recv(rad_listen_t *listener,
1852 UNUSED RAD_REQUEST_FUNP *pfun,
1853 UNUSED REQUEST **prequest)
1858 char *my_argv[MAX_ARGV], **argv;
1859 fr_command_table_t *table;
1860 fr_command_socket_t *co = listener->data;
1869 len = recv(listener->fd, co->buffer + co->offset,
1870 sizeof(co->buffer) - co->offset - 1, 0);
1871 if (len == 0) goto close_socket; /* clean close */
1874 if ((errno == EAGAIN) || (errno == EINTR)) {
1883 if ((co->offset == 0) && (co->buffer[0] == 0x04)) {
1885 command_close_socket(listener);
1890 * See if there are multiple lines in the buffer.
1892 p = co->buffer + co->offset;
1895 for (c = 0; c < len; c++) {
1896 if ((*p == '\r') || (*p == '\n')) {
1901 * FIXME: do real buffering...
1902 * handling of CTRL-C, etc.
1907 * \r \n followed by ASCII...
1918 * Saw CR/LF. Set next element, and exit.
1921 co->next = p - co->buffer;
1925 if (co->offset >= (ssize_t) (sizeof(co->buffer) - 1)) {
1926 radlog(L_ERR, "Line too long!");
1933 argc = str2argv(co->buffer, my_argv, MAX_ARGV);
1934 if (argc == 0) goto do_next; /* empty strings are OK */
1937 cprintf(listener, "ERROR: Failed parsing command.\n");
1943 for (len = 0; len <= co->offset; len++) {
1944 if (co->buffer[len] < 0x20) {
1945 co->buffer[len] = '\0';
1951 * Hard-code exit && quit.
1953 if ((strcmp(argv[0], "exit") == 0) ||
1954 (strcmp(argv[0], "quit") == 0)) goto close_socket;
1958 if (strcmp(argv[0], "login") != 0) {
1959 cprintf(listener, "ERROR: Login required\n");
1964 cprintf(listener, "ERROR: login <user> <password>\n");
1969 * FIXME: Generate && process fake RADIUS request.
1971 if ((strcmp(argv[1], "root") == 0) &&
1972 (strcmp(argv[2], "password") == 0)) {
1973 strlcpy(co->user, argv[1], sizeof(co->user));
1977 cprintf(listener, "ERROR: Login incorrect\n");
1982 table = command_table;
1985 for (i = 0; table[i].command != NULL; i++) {
1986 if (strcmp(table[i].command, argv[0]) == 0) {
1988 * Check permissions.
1990 if (((co->mode & FR_WRITE) == 0) &&
1991 ((table[i].mode & FR_WRITE) != 0)) {
1992 cprintf(listener, "ERROR: You do not have write permission.\n");
1996 if (table[i].table) {
1998 * This is the last argument, but
1999 * there's a sub-table. Print help.
2003 table = table[i].table;
2009 table = table[i].table;
2013 if (!table[i].func) {
2014 cprintf(listener, "ERROR: Invalid command\n");
2019 rcode = table[i].func(listener,
2020 argc - 1, argv + 1);
2029 if ((strcmp(argv[0], "help") == 0) ||
2030 (strcmp(argv[0], "?") == 0)) {
2034 if ((argc > 1) && (strcmp(argv[1], "-r") == 0)) {
2038 print_help(listener, table, recursive);
2042 cprintf(listener, "ERROR: Unknown command \"%s\"\n",
2047 cprintf(listener, "radmin> ");
2049 if (co->next <= co->offset) {
2052 memmove(co->buffer, co->buffer + co->next,
2053 co->offset - co->next);
2054 co->offset -= co->next;
2061 static int command_domain_accept(rad_listen_t *listener,
2062 UNUSED RAD_REQUEST_FUNP *pfun,
2063 UNUSED REQUEST **prequest)
2069 struct sockaddr_storage src;
2070 fr_command_socket_t *sock = listener->data;
2072 salen = sizeof(src);
2074 DEBUG2(" ... new connection request on command socket.");
2076 newfd = accept(listener->fd, (struct sockaddr *) &src, &salen);
2079 * Non-blocking sockets must handle this.
2081 if (errno == EWOULDBLOCK) {
2085 DEBUG2(" ... failed to accept connection.");
2090 * Perform user authentication.
2092 if (sock->uid_name || sock->gid_name) {
2096 if (getpeereid(listener->fd, &uid, &gid) < 0) {
2097 radlog(L_ERR, "Failed getting peer credentials for %s: %s",
2098 sock->path, strerror(errno));
2103 if (sock->uid_name && (sock->uid != uid)) {
2104 radlog(L_ERR, "Unauthorized connection to %s from uid %ld",
2105 sock->path, (long int) uid);
2110 if (sock->gid_name && (sock->gid != gid)) {
2111 radlog(L_ERR, "Unauthorized connection to %s from gid %ld",
2112 sock->path, (long int) gid);
2119 * Write 32-bit magic number && version information.
2121 magic = htonl(0xf7eead15);
2122 if (write(newfd, &magic, 4) < 0) {
2123 radlog(L_ERR, "Failed writing initial data to socket: %s",
2128 magic = htonl(1); /* protocol version */
2129 if (write(newfd, &magic, 4) < 0) {
2130 radlog(L_ERR, "Failed writing initial data to socket: %s",
2138 * Add the new listener.
2140 this = listen_alloc(listener->type);
2141 if (!this) return -1;
2144 * Copy everything, including the pointer to the socket
2148 memcpy(this, listener, sizeof(*this));
2149 this->status = RAD_LISTEN_STATUS_INIT;
2151 this->data = sock; /* fix it back */
2154 sock->user[0] = '\0';
2155 sock->path = ((fr_command_socket_t *) listener->data)->path;
2156 sock->mode = ((fr_command_socket_t *) listener->data)->mode;
2159 this->recv = command_domain_recv;
2162 * Tell the event loop that we have a new FD
2171 * Send an authentication response packet
2173 static int command_domain_send(UNUSED rad_listen_t *listener,
2174 UNUSED REQUEST *request)
2180 static int command_socket_encode(UNUSED rad_listen_t *listener,
2181 UNUSED REQUEST *request)
2187 static int command_socket_decode(UNUSED rad_listen_t *listener,
2188 UNUSED REQUEST *request)
2193 #endif /* WITH_COMMAND_SOCKET */