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 main_config_t main_config;
82 static bool echo = false;
83 static char const *secret = "testing123";
92 pid_t rad_waitpid(pid_t pid, int *status)
94 return waitpid(pid, status, 0);
98 static void NEVER_RETURNS usage(int status)
100 FILE *output = status ? stderr : stdout;
101 fprintf(output, "Usage: %s [ args ]\n", progname);
102 fprintf(output, " -d raddb_dir Configuration files are in \"raddbdir/*\".\n");
103 fprintf(stderr, " -D <dictdir> Set main dictionary directory (defaults to " DICTDIR ").\n");
104 fprintf(output, " -e command Execute 'command' and then exit.\n");
105 fprintf(output, " -E Echo commands as they are being executed.\n");
106 fprintf(output, " -f socket_file Open socket_file directly, without reading radius.conf\n");
107 fprintf(output, " -h Print usage help information.\n");
108 fprintf(output, " -i input_file Read commands from 'input_file'.\n");
109 fprintf(output, " -n name Read raddb/name.conf instead of raddb/radiusd.conf\n");
110 fprintf(output, " -q Quiet mode.\n");
115 static int client_socket(char const *server)
120 char *p, buffer[1024];
122 strlcpy(buffer, server, sizeof(buffer));
124 p = strchr(buffer, ':');
126 port = PW_RADMIN_PORT;
132 if (ip_hton(&ipaddr, AF_INET, buffer, false) < 0) {
133 fprintf(stderr, "%s: Failed looking up host %s: %s\n",
134 progname, buffer, fr_syserror(errno));
138 sockfd = fr_socket_client_tcp(NULL, &ipaddr, port, false);
140 fprintf(stderr, "%s: Failed opening socket %s: %s\n",
141 progname, server, fr_syserror(errno));
148 static ssize_t do_challenge(int sockfd)
151 fr_channel_type_t channel;
152 uint8_t challenge[16];
157 * When connecting over a socket, the server challenges us.
159 r = fr_channel_read(sockfd, &channel, challenge, sizeof(challenge));
160 if (r <= 0) return r;
162 if ((r != 16) || (channel != FR_CHANNEL_AUTH_CHALLENGE)) {
163 fprintf(stderr, "%s: Failed to read challenge.\n",
168 fr_hmac_md5(challenge, (uint8_t const *) secret, strlen(secret),
169 challenge, sizeof(challenge));
171 r = fr_channel_write(sockfd, FR_CHANNEL_AUTH_RESPONSE, challenge, sizeof(challenge));
172 if (r <= 0) return r;
175 * If the server doesn't like us, he just closes the
176 * socket. So we don't look for an ACK.
184 * Returns -1 on error. 0 on connection failed. +1 on OK.
186 static ssize_t run_command(int sockfd, char const *command,
187 char *buffer, size_t bufsize)
191 fr_channel_type_t channel;
194 fprintf(stdout, "%s\n", command);
198 * Write the text to the socket.
200 r = fr_channel_write(sockfd, FR_CHANNEL_STDIN, command, strlen(command));
201 if (r <= 0) return r;
204 r = fr_channel_read(sockfd, &channel, buffer, bufsize - 1);
205 if (r <= 0) return r;
207 buffer[r] = '\0'; /* for C strings */
210 case FR_CHANNEL_STDOUT:
211 fprintf(stdout, "%s", buffer);
214 case FR_CHANNEL_STDERR:
215 fprintf(stderr, "ERROR: %s", buffer);
218 case FR_CHANNEL_CMD_STATUS:
221 memcpy(&status, buffer, sizeof(status));
222 status = ntohl(status);
226 fprintf(stderr, "Unexpected response\n");
231 /* never gets here */
234 static int do_connect(int *out, char const *file, char const *server)
238 fr_channel_type_t channel;
244 * Close stale file descriptors
253 * FIXME: Get destination from command line, if possible?
255 sockfd = fr_socket_client_unix(file, false);
258 if (errno == ENOENT) {
259 fprintf(stderr, "Perhaps you need to run the commands:");
260 fprintf(stderr, "\tcd /etc/raddb\n");
261 fprintf(stderr, "\tln -s sites-available/control-socket "
262 "sites-enabled/control-socket\n");
263 fprintf(stderr, "and then re-start the server?\n");
268 sockfd = client_socket(server);
272 * Only works for BSD, but Linux allows us
273 * to mask SIGPIPE, so that's fine.
279 setsockopt(sockfd, SOL_SOCKET, SO_NOSIGPIPE, (void *)&set, sizeof(int));
284 * Set up the initial header data.
287 magic = htonl(magic);
288 memcpy(buffer, &magic, sizeof(magic));
289 memset(buffer + sizeof(magic), 0, sizeof(magic));
291 r = fr_channel_write(sockfd, FR_CHANNEL_INIT_ACK, buffer, 8);
294 fprintf(stderr, "%s: Error in socket: %s\n",
295 progname, fr_syserror(errno));
300 r = fr_channel_read(sockfd, &channel, buffer + 8, 8);
301 if (r <= 0) goto do_close;
303 if ((r != 8) || (channel != FR_CHANNEL_INIT_ACK) ||
304 (memcmp(buffer, buffer + 8, 8) != 0)) {
305 fprintf(stderr, "%s: Incompatible versions\n", progname);
310 if (server && secret) {
311 r = do_challenge(sockfd);
312 if (r <= 0) goto do_close;
320 #define MAX_COMMANDS (4)
322 int main(int argc, char **argv)
329 char const *file = NULL;
330 char const *name = "radiusd";
331 char *p, buffer[65536];
332 char const *input_file = NULL;
333 FILE *inputfp = stdin;
334 char const *server = NULL;
336 char const *radius_dir = RADIUS_DIR;
337 char const *dict_dir = DICTDIR;
339 char *commands[MAX_COMMANDS];
340 int num_commands = -1;
342 int exit_status = EXIT_SUCCESS;
345 if (fr_fault_setup(getenv("PANIC_ACTION"), argv[0]) < 0) {
351 talloc_set_log_stderr();
353 if ((progname = strrchr(argv[0], FR_DIR_SEP)) == NULL) {
359 while ((argval = getopt(argc, argv, "d:D:hi:e:Ef:n:qs:S")) != EOF) {
363 fprintf(stderr, "%s: -d and -f cannot be used together.\n", progname);
367 fprintf(stderr, "%s: -d and -s cannot be used together.\n", progname);
378 num_commands++; /* starts at -1 */
379 if (num_commands >= MAX_COMMANDS) {
380 fprintf(stderr, "%s: Too many '-e'\n",
385 commands[num_commands] = optarg;
399 usage(0); /* never returns */
402 if (strcmp(optarg, "-") != 0) {
418 fprintf(stderr, "%s: -s and -f cannot be used together.\n", progname);
432 * Mismatch between the binary and the libraries it depends on
434 if (fr_check_lib_magic(RADIUSD_MAGIC_NUMBER) < 0) {
441 CONF_SECTION *cs, *subcs;
444 char const *uid_name = NULL;
445 char const *gid_name = NULL;
449 file = NULL; /* MUST read it from the conffile now */
451 snprintf(buffer, sizeof(buffer), "%s/%s.conf", radius_dir, name);
454 * Need to read in the dictionaries, else we may get
455 * validation errors when we try and parse the config.
457 if (dict_init(dict_dir, RADIUS_DICTIONARY) < 0) {
462 if (dict_read(radius_dir, RADIUS_DICTIONARY) == -1) {
467 cs = cf_section_alloc(NULL, "main", NULL);
470 if (cf_file_read(cs, buffer) < 0) {
471 fprintf(stderr, "%s: Errors reading or parsing %s\n", progname, buffer);
480 while ((subcs = cf_subsection_find_next(cs, subcs, "listen")) != NULL) {
482 CONF_PAIR *cp = cf_pair_find(subcs, "type");
486 value = cf_pair_value(cp);
487 if (!value) continue;
489 if (strcmp(value, "control") != 0) continue;
492 * Now find the socket name (sigh)
494 rcode = cf_item_parse(subcs, "socket", FR_ITEM_POINTER(PW_TYPE_STRING, &file), NULL);
496 fprintf(stderr, "%s: Failed parsing listen section 'socket'\n", progname);
501 fprintf(stderr, "%s: No path given for socket\n", progname);
506 * If we're root, just use the first one we find
513 rcode = cf_item_parse(subcs, "uid", FR_ITEM_POINTER(PW_TYPE_STRING, &uid_name), NULL);
515 fprintf(stderr, "%s: Failed parsing listen section 'uid'\n", progname);
519 if (!uid_name) break;
521 pwd = getpwnam(uid_name);
523 fprintf(stderr, "%s: Failed getting UID for user %s: %s\n", progname, uid_name, strerror(errno));
527 if (uid != pwd->pw_uid) continue;
529 rcode = cf_item_parse(subcs, "gid", FR_ITEM_POINTER(PW_TYPE_STRING, &gid_name), NULL);
531 fprintf(stderr, "%s: Failed parsing listen section 'gid'\n", progname);
535 if (!gid_name) break;
537 grp = getgrnam(gid_name);
539 fprintf(stderr, "%s: Failed getting GID for group %s: %s\n", progname, gid_name, strerror(errno));
543 if (gid != grp->gr_gid) continue;
549 fprintf(stderr, "%s: Could not find control socket in %s\n", progname, buffer);
555 inputfp = fopen(input_file, "r");
557 fprintf(stderr, "%s: Failed opening %s: %s\n", progname, input_file, fr_syserror(errno));
562 if (!file && !server) {
563 fprintf(stderr, "%s: Must use one of '-d' or '-f' or '-s'\n",
569 * Check if stdin is a TTY only if input is from stdin
571 if (input_file && !quiet && !isatty(STDIN_FILENO)) quiet = true;
575 #ifdef USE_READLINE_HISTORY
578 rl_bind_key('\t', rl_insert);
583 * Prevent SIGPIPEs from terminating the process
585 signal(SIGPIPE, SIG_IGN);
587 if (do_connect(&sockfd, file, server) < 0) exit(1);
592 if (num_commands >= 0) {
595 for (i = 0; i <= num_commands; i++) {
596 len = run_command(sockfd, commands[i], buffer, sizeof(buffer));
597 if (len < 0) exit(1);
599 if (len == FR_CHANNEL_FAIL) exit_status = EXIT_FAILURE;
605 printf("%s - FreeRADIUS Server administration tool.\n", radmin_version);
606 printf("Copyright (C) 2008-2015 The FreeRADIUS server project and contributors.\n");
607 printf("There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A\n");
608 printf("PARTICULAR PURPOSE.\n");
609 printf("You may redistribute copies of FreeRADIUS under the terms of the\n");
610 printf("GNU General Public License v2.\n");
627 line = readline("radmin> ");
636 #ifdef USE_READLINE_HISTORY
639 } else /* quiet, or no readline */
642 line = fgets(buffer, sizeof(buffer), inputfp);
645 p = strchr(buffer, '\n');
647 fprintf(stderr, "%s: Input line too long\n",
655 * Strip off leading spaces.
657 for (p = line; *p != '\0'; p++) {
673 * Comments: keep going.
680 for (p = line; *p != '\0'; p++) {
681 if ((p[0] == '\r') ||
689 if (strcmp(line, "reconnect") == 0) {
690 if (do_connect(&sockfd, file, server) < 0) exit(1);
695 if (memcmp(line, "secret ", 7) == 0) {
698 do_challenge(sockfd);
707 if ((strcmp(line, "exit") == 0) ||
708 (strcmp(line, "quit") == 0)) {
712 if (server && !secret) {
713 fprintf(stderr, "ERROR: You must enter 'secret <SECRET>' before running any commands\n");
720 len = run_command(sockfd, line, buffer, sizeof(buffer));
722 if (!quiet) fprintf(stderr, "... reconnecting ...\n");
724 if (do_connect(&sockfd, file, server) < 0) {
729 if (retries < 2) goto retry;
731 fprintf(stderr, "Failed to connect to server\n");
734 } else if (len == FR_CHANNEL_SUCCESS) {
737 } else if (len == FR_CHANNEL_FAIL) {
738 exit_status = EXIT_FAILURE;
742 fprintf(stdout, "\n");
744 if (inputfp != stdin) fclose(inputfp);