2 * radmin.c RADIUS Administration tool.
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 2012 The FreeRADIUS server project
21 * Copyright 2012 Alan DeKok <aland@deployingradius.com>
26 #include <freeradius-devel/radiusd.h>
27 #include <freeradius-devel/md5.h>
28 #include <freeradius-devel/channel.h>
37 #ifdef HAVE_SYS_STAT_H
38 # include <sys/stat.h>
41 #ifdef HAVE_LIBREADLINE
43 #if defined(HAVE_READLINE_READLINE_H)
44 # include <readline/readline.h>
45 # define USE_READLINE (1)
46 #elif defined(HAVE_READLINE_H)
47 # include <readline.h>
48 # define USE_READLINE (1)
49 #endif /* !defined(HAVE_READLINE_H) */
51 #ifdef HAVE_READLINE_HISTORY
52 # if defined(HAVE_READLINE_HISTORY_H)
53 # include <readline/history.h>
54 # define USE_READLINE_HISTORY (1)
55 # elif defined(HAVE_HISTORY_H)
57 # define USE_READLINE_HISTORY (1)
58 #endif /* defined(HAVE_READLINE_HISTORY_H) */
60 #endif /* HAVE_READLINE_HISTORY */
62 #endif /* HAVE_LIBREADLINE */
65 * For configuration file stuff.
67 char const *progname = "radmin";
68 static char const *radmin_version = "radmin version " RADIUSD_VERSION_STRING
69 #ifdef RADIUSD_VERSION_COMMIT
70 " (git #" STRINGIFY(RADIUSD_VERSION_COMMIT) ")"
72 ", built on " __DATE__ " at " __TIME__;
76 * The rest of this is because the conffile.c, etc. assume
77 * they're running inside of the server. And we don't (yet)
78 * have a "libfreeradius-server", or "libfreeradius-util".
80 log_lvl_t debug_flag = 0;
81 main_config_t main_config;
83 bool check_config = false;
85 static bool echo = false;
86 static char const *secret = "testing123";
95 pid_t rad_waitpid(pid_t pid, int *status)
97 return waitpid(pid, status, 0);
101 static void NEVER_RETURNS usage(int status)
103 FILE *output = status ? stderr : stdout;
104 fprintf(output, "Usage: %s [ args ]\n", progname);
105 fprintf(output, " -d raddb_dir Configuration files are in \"raddbdir/*\".\n");
106 fprintf(stderr, " -D <dictdir> Set main dictionary directory (defaults to " DICTDIR ").\n");
107 fprintf(output, " -e command Execute 'command' and then exit.\n");
108 fprintf(output, " -E Echo commands as they are being executed.\n");
109 fprintf(output, " -f socket_file Open socket_file directly, without reading radius.conf\n");
110 fprintf(output, " -h Print usage help information.\n");
111 fprintf(output, " -i input_file Read commands from 'input_file'.\n");
112 fprintf(output, " -n name Read raddb/name.conf instead of raddb/radiusd.conf\n");
113 fprintf(output, " -q Quiet mode.\n");
118 static int client_socket(char const *server)
123 char *p, buffer[1024];
125 strlcpy(buffer, server, sizeof(buffer));
127 p = strchr(buffer, ':');
129 port = PW_RADMIN_PORT;
135 if (ip_hton(&ipaddr, AF_INET, buffer, false) < 0) {
136 fprintf(stderr, "%s: Failed looking up host %s: %s\n",
137 progname, buffer, fr_syserror(errno));
141 sockfd = fr_socket_client_tcp(NULL, &ipaddr, port, false);
143 fprintf(stderr, "%s: Failed opening socket %s: %s\n",
144 progname, server, fr_syserror(errno));
151 static ssize_t do_challenge(int sockfd)
154 fr_channel_type_t channel;
155 uint8_t challenge[16];
160 * When connecting over a socket, the server challenges us.
162 r = fr_channel_read(sockfd, &channel, challenge, sizeof(challenge));
163 if (r <= 0) return r;
165 if ((r != 16) || (channel != FR_CHANNEL_AUTH_CHALLENGE)) {
166 fprintf(stderr, "%s: Failed to read challenge.\n",
171 fr_hmac_md5(challenge, (uint8_t const *) secret, strlen(secret),
172 challenge, sizeof(challenge));
174 r = fr_channel_write(sockfd, FR_CHANNEL_AUTH_RESPONSE, challenge, sizeof(challenge));
175 if (r <= 0) return r;
178 * If the server doesn't like us, he just closes the
179 * socket. So we don't look for an ACK.
187 * Returns -1 on error. 0 on connection failed. +1 on OK.
189 static ssize_t run_command(int sockfd, char const *command,
190 char *buffer, size_t bufsize)
194 fr_channel_type_t channel;
197 fprintf(stdout, "%s\n", command);
201 * Write the text to the socket.
203 r = fr_channel_write(sockfd, FR_CHANNEL_STDIN, command, strlen(command));
204 if (r <= 0) return r;
207 r = fr_channel_read(sockfd, &channel, buffer, bufsize - 1);
208 if (r <= 0) return r;
210 buffer[r] = '\0'; /* for C strings */
213 case FR_CHANNEL_STDOUT:
214 fprintf(stdout, "%s", buffer);
217 case FR_CHANNEL_STDERR:
218 fprintf(stderr, "ERROR: %s", buffer);
221 case FR_CHANNEL_CMD_STATUS:
224 memcpy(&status, buffer, sizeof(status));
225 status = ntohl(status);
229 fprintf(stderr, "Unexpected response\n");
234 /* never gets here */
237 static int do_connect(int *out, char const *file, char const *server)
241 fr_channel_type_t channel;
247 * Close stale file descriptors
256 * FIXME: Get destination from command line, if possible?
258 sockfd = fr_socket_client_unix(file, false);
261 if (errno == ENOENT) {
262 fprintf(stderr, "Perhaps you need to run the commands:");
263 fprintf(stderr, "\tcd /etc/raddb\n");
264 fprintf(stderr, "\tln -s sites-available/control-socket "
265 "sites-enabled/control-socket\n");
266 fprintf(stderr, "and then re-start the server?\n");
271 sockfd = client_socket(server);
275 * Only works for BSD, but Linux allows us
276 * to mask SIGPIPE, so that's fine.
282 setsockopt(sockfd, SOL_SOCKET, SO_NOSIGPIPE, (void *)&set, sizeof(int));
287 * Set up the initial header data.
290 magic = htonl(magic);
291 memcpy(buffer, &magic, sizeof(magic));
292 memset(buffer + sizeof(magic), 0, sizeof(magic));
294 r = fr_channel_write(sockfd, FR_CHANNEL_INIT_ACK, buffer, 8);
297 fprintf(stderr, "%s: Error in socket: %s\n",
298 progname, fr_syserror(errno));
303 r = fr_channel_read(sockfd, &channel, buffer + 8, 8);
304 if (r <= 0) goto do_close;
306 if ((r != 8) || (channel != FR_CHANNEL_INIT_ACK) ||
307 (memcmp(buffer, buffer + 8, 8) != 0)) {
308 fprintf(stderr, "%s: Incompatible versions\n", progname);
313 if (server && secret) {
314 r = do_challenge(sockfd);
315 if (r <= 0) goto do_close;
323 #define MAX_COMMANDS (4)
325 int main(int argc, char **argv)
332 char const *file = NULL;
333 char const *name = "radiusd";
334 char *p, buffer[65536];
335 char const *input_file = NULL;
336 FILE *inputfp = stdin;
337 char const *server = NULL;
339 char const *radius_dir = RADIUS_DIR;
340 char const *dict_dir = DICTDIR;
342 char *commands[MAX_COMMANDS];
343 int num_commands = -1;
345 int exit_status = EXIT_SUCCESS;
348 if (fr_fault_setup(getenv("PANIC_ACTION"), argv[0]) < 0) {
354 talloc_set_log_stderr();
356 if ((progname = strrchr(argv[0], FR_DIR_SEP)) == NULL) {
362 while ((argval = getopt(argc, argv, "d:D:hi:e:Ef:n:qs:S")) != EOF) {
366 fprintf(stderr, "%s: -d and -f cannot be used together.\n", progname);
370 fprintf(stderr, "%s: -d and -s cannot be used together.\n", progname);
381 num_commands++; /* starts at -1 */
382 if (num_commands >= MAX_COMMANDS) {
383 fprintf(stderr, "%s: Too many '-e'\n",
388 commands[num_commands] = optarg;
402 usage(0); /* never returns */
405 if (strcmp(optarg, "-") != 0) {
421 fprintf(stderr, "%s: -s and -f cannot be used together.\n", progname);
435 * Mismatch between the binary and the libraries it depends on
437 if (fr_check_lib_magic(RADIUSD_MAGIC_NUMBER) < 0) {
444 CONF_SECTION *cs, *subcs;
447 char const *uid_name = NULL;
448 char const *gid_name = NULL;
452 file = NULL; /* MUST read it from the conffile now */
454 snprintf(buffer, sizeof(buffer), "%s/%s.conf", radius_dir, name);
457 * Need to read in the dictionaries, else we may get
458 * validation errors when we try and parse the config.
460 if (dict_init(dict_dir, RADIUS_DICTIONARY) < 0) {
465 if (dict_read(radius_dir, RADIUS_DICTIONARY) == -1) {
470 cs = cf_section_alloc(NULL, "main", NULL);
473 if (cf_file_read(cs, buffer) < 0) {
474 fprintf(stderr, "%s: Errors reading or parsing %s\n", progname, buffer);
482 while ((subcs = cf_subsection_find_next(cs, subcs, "listen")) != NULL) {
484 CONF_PAIR *cp = cf_pair_find(subcs, "type");
488 value = cf_pair_value(cp);
489 if (!value) continue;
491 if (strcmp(value, "control") != 0) continue;
494 * Now find the socket name (sigh)
496 rcode = cf_item_parse(subcs, "socket", FR_ITEM_POINTER(PW_TYPE_STRING, &file), NULL);
498 fprintf(stderr, "%s: Failed parsing listen section 'socket'\n", progname);
503 fprintf(stderr, "%s: No path given for socket\n", progname);
508 * If we're root, just use the first one we find
515 rcode = cf_item_parse(subcs, "uid", FR_ITEM_POINTER(PW_TYPE_STRING, &uid_name), NULL);
517 fprintf(stderr, "%s: Failed parsing listen section 'uid'\n", progname);
521 if (!uid_name) break;
523 pwd = getpwnam(uid_name);
525 fprintf(stderr, "%s: Failed getting UID for user %s: %s\n", progname, uid_name, strerror(errno));
529 if (uid != pwd->pw_uid) continue;
531 rcode = cf_item_parse(subcs, "gid", FR_ITEM_POINTER(PW_TYPE_STRING, &gid_name), NULL);
533 fprintf(stderr, "%s: Failed parsing listen section 'gid'\n", progname);
537 if (!gid_name) break;
539 grp = getgrnam(gid_name);
541 fprintf(stderr, "%s: Failed getting GID for group %s: %s\n", progname, gid_name, strerror(errno));
545 if (gid != grp->gr_gid) continue;
551 fprintf(stderr, "%s: Could not find control socket in %s\n", progname, buffer);
557 inputfp = fopen(input_file, "r");
559 fprintf(stderr, "%s: Failed opening %s: %s\n", progname, input_file, fr_syserror(errno));
564 if (!file && !server) {
565 fprintf(stderr, "%s: Must use one of '-d' or '-f' or '-s'\n",
571 * Check if stdin is a TTY only if input is from stdin
573 if (input_file && !quiet && !isatty(STDIN_FILENO)) quiet = true;
577 #ifdef USE_READLINE_HISTORY
580 rl_bind_key('\t', rl_insert);
585 * Prevent SIGPIPEs from terminating the process
587 signal(SIGPIPE, SIG_IGN);
589 if (do_connect(&sockfd, file, server) < 0) exit(1);
594 if (num_commands >= 0) {
597 for (i = 0; i <= num_commands; i++) {
598 len = run_command(sockfd, commands[i], buffer, sizeof(buffer));
599 if (len < 0) exit(1);
601 if (len == FR_CHANNEL_FAIL) exit_status = EXIT_FAILURE;
607 printf("%s - FreeRADIUS Server administration tool.\n", radmin_version);
608 printf("Copyright (C) 2008-2015 The FreeRADIUS server project and contributors.\n");
609 printf("There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A\n");
610 printf("PARTICULAR PURPOSE.\n");
611 printf("You may redistribute copies of FreeRADIUS under the terms of the\n");
612 printf("GNU General Public License v2.\n");
629 line = readline("radmin> ");
638 #ifdef USE_READLINE_HISTORY
641 } else /* quiet, or no readline */
644 line = fgets(buffer, sizeof(buffer), inputfp);
647 p = strchr(buffer, '\n');
649 fprintf(stderr, "%s: Input line too long\n",
657 * Strip off leading spaces.
659 for (p = line; *p != '\0'; p++) {
675 * Comments: keep going.
682 for (p = line; *p != '\0'; p++) {
683 if ((p[0] == '\r') ||
691 if (strcmp(line, "reconnect") == 0) {
692 if (do_connect(&sockfd, file, server) < 0) exit(1);
697 if (memcmp(line, "secret ", 7) == 0) {
700 do_challenge(sockfd);
709 if ((strcmp(line, "exit") == 0) ||
710 (strcmp(line, "quit") == 0)) {
714 if (server && !secret) {
715 fprintf(stderr, "ERROR: You must enter 'secret <SECRET>' before running any commands\n");
722 len = run_command(sockfd, line, buffer, sizeof(buffer));
724 if (!quiet) fprintf(stderr, "Reconnecting...");
726 if (do_connect(&sockfd, file, server) < 0) {
731 if (retries < 2) goto retry;
733 fprintf(stderr, "Failed to connect to server\n");
736 } else if (len == FR_CHANNEL_SUCCESS) {
739 } else if (len == FR_CHANNEL_FAIL) {
740 exit_status = EXIT_FAILURE;
744 fprintf(stdout, "\n");
746 if (inputfp != stdin) fclose(inputfp);