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
44 #if defined(HAVE_READLINE_READLINE_H)
45 # include <readline/readline.h>
46 # define USE_READLINE (1)
47 #elif defined(HAVE_READLINE_H)
48 # include <readline.h>
49 # define USE_READLINE (1)
50 #endif /* !defined(HAVE_READLINE_H) */
52 #ifdef HAVE_READLINE_HISTORY
53 # if defined(HAVE_READLINE_HISTORY_H)
54 # include <readline/history.h>
55 # define USE_READLINE_HISTORY (1)
56 # elif defined(HAVE_HISTORY_H)
58 # define USE_READLINE_HISTORY (1)
59 #endif /* defined(HAVE_READLINE_HISTORY_H) */
61 #endif /* HAVE_READLINE_HISTORY */
63 #endif /* HAVE_LIBREADLINE */
66 * For configuration file stuff.
68 static char const *progname = "radmin";
69 static char const *radmin_version = "radmin version " RADIUSD_VERSION_STRING
70 #ifdef RADIUSD_VERSION_COMMIT
71 " (git #" STRINGIFY(RADIUSD_VERSION_COMMIT) ")"
73 ", built on " __DATE__ " at " __TIME__;
77 * The rest of this is because the conffile.c, etc. assume
78 * they're running inside of the server. And we don't (yet)
79 * have a "libfreeradius-server", or "libfreeradius-util".
81 main_config_t main_config;
83 static bool echo = false;
84 static char const *secret = "testing123";
93 pid_t rad_waitpid(pid_t pid, int *status)
95 return waitpid(pid, status, 0);
99 static void NEVER_RETURNS usage(int status)
101 FILE *output = status ? stderr : stdout;
102 fprintf(output, "Usage: %s [ args ]\n", progname);
103 fprintf(output, " -d raddb_dir Configuration files are in \"raddbdir/*\".\n");
104 fprintf(stderr, " -D <dictdir> Set main dictionary directory (defaults to " DICTDIR ").\n");
105 fprintf(output, " -e command Execute 'command' and then exit.\n");
106 fprintf(output, " -E Echo commands as they are being executed.\n");
107 fprintf(output, " -f socket_file Open socket_file directly, without reading radius.conf\n");
108 fprintf(output, " -h Print usage help information.\n");
109 fprintf(output, " -i input_file Read commands from 'input_file'.\n");
110 fprintf(output, " -n name Read raddb/name.conf instead of raddb/radiusd.conf\n");
111 fprintf(output, " -q Quiet mode.\n");
116 static int client_socket(char const *server)
121 char *p, buffer[1024];
123 strlcpy(buffer, server, sizeof(buffer));
125 p = strchr(buffer, ':');
127 port = PW_RADMIN_PORT;
133 if (ip_hton(&ipaddr, AF_INET, buffer, false) < 0) {
134 fprintf(stderr, "%s: Failed looking up host %s: %s\n",
135 progname, buffer, fr_syserror(errno));
139 sockfd = fr_socket_client_tcp(NULL, &ipaddr, port, false);
141 fprintf(stderr, "%s: Failed opening socket %s: %s\n",
142 progname, server, fr_syserror(errno));
149 static ssize_t do_challenge(int sockfd)
152 fr_channel_type_t channel;
153 uint8_t challenge[16];
158 * When connecting over a socket, the server challenges us.
160 r = fr_channel_read(sockfd, &channel, challenge, sizeof(challenge));
161 if (r <= 0) return r;
163 if ((r != 16) || (channel != FR_CHANNEL_AUTH_CHALLENGE)) {
164 fprintf(stderr, "%s: Failed to read challenge.\n",
169 fr_hmac_md5(challenge, (uint8_t const *) secret, strlen(secret),
170 challenge, sizeof(challenge));
172 r = fr_channel_write(sockfd, FR_CHANNEL_AUTH_RESPONSE, challenge, sizeof(challenge));
173 if (r <= 0) return r;
176 * If the server doesn't like us, he just closes the
177 * socket. So we don't look for an ACK.
185 * Returns -1 on error. 0 on connection failed. +1 on OK.
187 static ssize_t run_command(int sockfd, char const *command,
188 char *buffer, size_t bufsize)
192 fr_channel_type_t channel;
195 fprintf(stdout, "%s\n", command);
199 * Write the text to the socket.
201 r = fr_channel_write(sockfd, FR_CHANNEL_STDIN, command, strlen(command));
202 if (r <= 0) return r;
205 r = fr_channel_read(sockfd, &channel, buffer, bufsize - 1);
206 if (r <= 0) return r;
208 buffer[r] = '\0'; /* for C strings */
211 case FR_CHANNEL_STDOUT:
212 fprintf(stdout, "%s", buffer);
215 case FR_CHANNEL_STDERR:
216 fprintf(stderr, "ERROR: %s", buffer);
219 case FR_CHANNEL_CMD_STATUS:
222 memcpy(&status, buffer, sizeof(status));
223 status = ntohl(status);
227 fprintf(stderr, "Unexpected response\n");
232 /* never gets here */
235 static int do_connect(int *out, char const *file, char const *server)
239 fr_channel_type_t channel;
245 * Close stale file descriptors
254 * FIXME: Get destination from command line, if possible?
256 sockfd = fr_socket_client_unix(file, false);
259 if (errno == ENOENT) {
260 fprintf(stderr, "Perhaps you need to run the commands:");
261 fprintf(stderr, "\tcd /etc/raddb\n");
262 fprintf(stderr, "\tln -s sites-available/control-socket "
263 "sites-enabled/control-socket\n");
264 fprintf(stderr, "and then re-start the server?\n");
269 sockfd = client_socket(server);
273 * Only works for BSD, but Linux allows us
274 * to mask SIGPIPE, so that's fine.
280 setsockopt(sockfd, SOL_SOCKET, SO_NOSIGPIPE, (void *)&set, sizeof(int));
285 * Set up the initial header data.
288 magic = htonl(magic);
289 memcpy(buffer, &magic, sizeof(magic));
290 memset(buffer + sizeof(magic), 0, sizeof(magic));
292 r = fr_channel_write(sockfd, FR_CHANNEL_INIT_ACK, buffer, 8);
295 fprintf(stderr, "%s: Error in socket: %s\n",
296 progname, fr_syserror(errno));
301 r = fr_channel_read(sockfd, &channel, buffer + 8, 8);
302 if (r <= 0) goto do_close;
304 if ((r != 8) || (channel != FR_CHANNEL_INIT_ACK) ||
305 (memcmp(buffer, buffer + 8, 8) != 0)) {
306 fprintf(stderr, "%s: Incompatible versions\n", progname);
311 if (server && secret) {
312 r = do_challenge(sockfd);
313 if (r <= 0) goto do_close;
321 #define MAX_COMMANDS (4)
323 int main(int argc, char **argv)
330 char const *file = NULL;
331 char const *name = "radiusd";
332 char *p, buffer[65536];
333 char const *input_file = NULL;
334 FILE *inputfp = stdin;
335 char const *server = NULL;
337 char const *radius_dir = RADIUS_DIR;
338 char const *dict_dir = DICTDIR;
340 char *commands[MAX_COMMANDS];
341 int num_commands = -1;
343 int exit_status = EXIT_SUCCESS;
346 if (fr_fault_setup(getenv("PANIC_ACTION"), argv[0]) < 0) {
352 talloc_set_log_stderr();
354 if ((progname = strrchr(argv[0], FR_DIR_SEP)) == NULL) {
360 while ((argval = getopt(argc, argv, "d:D:hi:e:Ef:n:qs:S")) != EOF) {
364 fprintf(stderr, "%s: -d and -f cannot be used together.\n", progname);
368 fprintf(stderr, "%s: -d and -s cannot be used together.\n", progname);
379 num_commands++; /* starts at -1 */
380 if (num_commands >= MAX_COMMANDS) {
381 fprintf(stderr, "%s: Too many '-e'\n",
386 commands[num_commands] = optarg;
400 usage(0); /* never returns */
403 if (strcmp(optarg, "-") != 0) {
419 fprintf(stderr, "%s: -s and -f cannot be used together.\n", progname);
433 * Mismatch between the binary and the libraries it depends on
435 if (fr_check_lib_magic(RADIUSD_MAGIC_NUMBER) < 0) {
442 CONF_SECTION *cs, *subcs;
445 char const *uid_name = NULL;
446 char const *gid_name = NULL;
450 file = NULL; /* MUST read it from the conffile now */
452 snprintf(buffer, sizeof(buffer), "%s/%s.conf", radius_dir, name);
455 * Need to read in the dictionaries, else we may get
456 * validation errors when we try and parse the config.
458 if (dict_init(dict_dir, RADIUS_DICTIONARY) < 0) {
463 if (dict_read(radius_dir, RADIUS_DICTIONARY) == -1) {
468 cs = cf_section_alloc(NULL, "main", NULL);
471 if (cf_file_read(cs, buffer) < 0) {
472 fprintf(stderr, "%s: Errors reading or parsing %s\n", progname, buffer);
481 while ((subcs = cf_subsection_find_next(cs, subcs, "listen")) != NULL) {
483 CONF_PAIR *cp = cf_pair_find(subcs, "type");
487 value = cf_pair_value(cp);
488 if (!value) continue;
490 if (strcmp(value, "control") != 0) continue;
493 * Now find the socket name (sigh)
495 rcode = cf_item_parse(subcs, "socket", FR_ITEM_POINTER(PW_TYPE_STRING, &file), NULL);
497 fprintf(stderr, "%s: Failed parsing listen section 'socket'\n", progname);
502 fprintf(stderr, "%s: No path given for socket\n", progname);
507 * If we're root, just use the first one we find
514 rcode = cf_item_parse(subcs, "uid", FR_ITEM_POINTER(PW_TYPE_STRING, &uid_name), NULL);
516 fprintf(stderr, "%s: Failed parsing listen section 'uid'\n", progname);
520 if (!uid_name) break;
522 pwd = getpwnam(uid_name);
524 fprintf(stderr, "%s: Failed getting UID for user %s: %s\n", progname, uid_name, strerror(errno));
528 if (uid != pwd->pw_uid) continue;
530 rcode = cf_item_parse(subcs, "gid", FR_ITEM_POINTER(PW_TYPE_STRING, &gid_name), NULL);
532 fprintf(stderr, "%s: Failed parsing listen section 'gid'\n", progname);
536 if (!gid_name) break;
538 grp = getgrnam(gid_name);
540 fprintf(stderr, "%s: Failed getting GID for group %s: %s\n", progname, gid_name, strerror(errno));
544 if (gid != grp->gr_gid) continue;
550 fprintf(stderr, "%s: Could not find control socket in %s\n", progname, buffer);
556 inputfp = fopen(input_file, "r");
558 fprintf(stderr, "%s: Failed opening %s: %s\n", progname, input_file, fr_syserror(errno));
563 if (!file && !server) {
564 fprintf(stderr, "%s: Must use one of '-d' or '-f' or '-s'\n",
570 * Check if stdin is a TTY only if input is from stdin
572 if (input_file && !quiet && !isatty(STDIN_FILENO)) quiet = true;
576 #ifdef USE_READLINE_HISTORY
579 rl_bind_key('\t', rl_insert);
584 * Prevent SIGPIPEs from terminating the process
586 signal(SIGPIPE, SIG_IGN);
588 if (do_connect(&sockfd, file, server) < 0) exit(1);
593 if (num_commands >= 0) {
596 for (i = 0; i <= num_commands; i++) {
597 len = run_command(sockfd, commands[i], buffer, sizeof(buffer));
598 if (len < 0) exit(1);
600 if (len == FR_CHANNEL_FAIL) exit_status = EXIT_FAILURE;
606 printf("%s - FreeRADIUS Server administration tool.\n", radmin_version);
607 printf("Copyright (C) 2008-2017 The FreeRADIUS server project and contributors.\n");
608 printf("There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A\n");
609 printf("PARTICULAR PURPOSE.\n");
610 printf("You may redistribute copies of FreeRADIUS under the terms of the\n");
611 printf("GNU General Public License v2.\n");
628 line = readline("radmin> ");
637 #ifdef USE_READLINE_HISTORY
640 } else /* quiet, or no readline */
643 line = fgets(buffer, sizeof(buffer), inputfp);
646 p = strchr(buffer, '\n');
648 fprintf(stderr, "%s: Input line too long\n",
656 * Strip off leading spaces.
658 for (p = line; *p != '\0'; p++) {
674 * Comments: keep going.
681 for (p = line; *p != '\0'; p++) {
682 if ((p[0] == '\r') ||
690 if (strcmp(line, "reconnect") == 0) {
691 if (do_connect(&sockfd, file, server) < 0) exit(1);
696 if (memcmp(line, "secret ", 7) == 0) {
699 do_challenge(sockfd);
708 if ((strcmp(line, "exit") == 0) ||
709 (strcmp(line, "quit") == 0)) {
713 if (server && !secret) {
714 fprintf(stderr, "ERROR: You must enter 'secret <SECRET>' before running any commands\n");
721 len = run_command(sockfd, line, buffer, sizeof(buffer));
723 if (!quiet) fprintf(stderr, "... reconnecting ...\n");
725 if (do_connect(&sockfd, file, server) < 0) {
730 if (retries < 2) goto retry;
732 fprintf(stderr, "Failed to connect to server\n");
735 } else if (len == FR_CHANNEL_SUCCESS) {
738 } else if (len == FR_CHANNEL_FAIL) {
739 exit_status = EXIT_FAILURE;
743 fprintf(stdout, "\n");
745 if (inputfp != stdin) fclose(inputfp);