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>
31 #define SUN_LEN(su) (sizeof(*(su)) - sizeof((su)->sun_path) + strlen((su)->sun_path))
39 #ifdef HAVE_SYS_STAT_H
43 #ifdef HAVE_LIBREADLINE
45 #if defined(HAVE_READLINE_READLINE_H)
46 #include <readline/readline.h>
47 #define USE_READLINE (1)
48 #elif defined(HAVE_READLINE_H)
50 #define USE_READLINE (1)
51 #endif /* !defined(HAVE_READLINE_H) */
53 #ifdef HAVE_READLINE_HISTORY
54 #if defined(HAVE_READLINE_HISTORY_H)
55 #include <readline/history.h>
56 #define USE_READLINE_HISTORY (1)
57 #elif defined(HAVE_HISTORY_H)
59 #define USE_READLINE_HISTORY (1)
60 #endif /* defined(HAVE_READLINE_HISTORY_H) */
62 #endif /* HAVE_READLINE_HISTORY */
64 #endif /* HAVE_LIBREADLINE */
67 * For configuration file stuff.
69 char const *progname = "radmin";
70 char const *radmin_version = "radmin version " RADIUSD_VERSION_STRING
71 #ifdef RADIUSD_VERSION_COMMIT
72 " (git #" STRINGIFY(RADIUSD_VERSION_COMMIT) ")"
74 ", built on " __DATE__ " at " __TIME__;
78 * The rest of this is because the conffile.c, etc. assume
79 * they're running inside of the server. And we don't (yet)
80 * have a "libfreeradius-server", or "libfreeradius-util".
82 log_debug_t debug_flag = 0;
83 struct main_config_t main_config;
85 bool check_config = false;
87 static FILE *outputfp = NULL;
88 static bool echo = false;
89 static char const *secret = "testing123";
98 pid_t rad_waitpid(pid_t pid, int *status)
100 return waitpid(pid, status, 0);
104 static void NEVER_RETURNS usage(int status)
106 FILE *output = status ? stderr : stdout;
107 fprintf(output, "Usage: %s [ args ]\n", progname);
108 fprintf(output, " -d raddb_dir Configuration files are in \"raddbdir/*\".\n");
109 fprintf(stderr, " -D <dictdir> Set main dictionary directory (defaults to " DICTDIR ").\n");
110 fprintf(output, " -e command Execute 'command' and then exit.\n");
111 fprintf(output, " -E Echo commands as they are being executed.\n");
112 fprintf(output, " -f socket_file Open socket_file directly, without reading radius.conf\n");
113 fprintf(output, " -h Print usage help information.\n");
114 fprintf(output, " -i input_file Read commands from 'input_file'.\n");
115 fprintf(output, " -n name Read raddb/name.conf instead of raddb/radiusd.conf\n");
116 fprintf(output, " -o output_file Write commands to 'output_file'.\n");
117 fprintf(output, " -q Quiet mode.\n");
122 static int fr_domain_socket(char const *path)
128 struct sockaddr_un saremote;
131 if (len >= sizeof(saremote.sun_path)) {
132 fprintf(stderr, "%s: Path too long in filename\n", progname);
136 if ((sockfd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
137 fprintf(stderr, "%s: Failed creating socket: %s\n",
138 progname, fr_syserror(errno));
142 saremote.sun_family = AF_UNIX;
143 memcpy(saremote.sun_path, path, len + 1); /* SUN_LEN does strlen */
145 socklen = SUN_LEN(&saremote);
147 if (connect(sockfd, (struct sockaddr *)&saremote, socklen) < 0) {
151 fprintf(stderr, "%s: Failed connecting to %s: %s\n",
152 progname, path, fr_syserror(errno));
155 * The file doesn't exist. Tell the user how to
158 if ((stat(path, &buf) < 0) &&
160 fprintf(stderr, " Perhaps you need to run the commands:\n\tcd /etc/raddb\n\tln -s sites-available/control-socket sites-enabled/control-socket\n and then re-start the server?\n");
170 if ((flags = fcntl(sockfd, F_GETFL, NULL)) < 0) {
171 fprintf(stderr, "%s: Failure getting socket flags: %s",
172 progname, fr_syserror(errno));
178 if( fcntl(sockfd, F_SETFL, flags) < 0) {
179 fprintf(stderr, "%s: Failure setting socket flags: %s",
180 progname, fr_syserror(errno));
190 static int client_socket(char const *server)
195 char *p, buffer[1024];
197 strlcpy(buffer, server, sizeof(buffer));
199 p = strchr(buffer, ':');
201 port = PW_RADMIN_PORT;
207 if (ip_hton(buffer, AF_INET, &ipaddr) < 0) {
208 fprintf(stderr, "%s: Failed looking up host %s: %s\n",
209 progname, buffer, fr_syserror(errno));
213 sockfd = fr_tcp_client_socket(NULL, &ipaddr, port);
215 fprintf(stderr, "%s: Failed opening socket %s: %s\n",
216 progname, server, fr_syserror(errno));
223 static void do_challenge(int sockfd)
227 uint8_t challenge[16];
229 for (total = 0; total < sizeof(challenge); ) {
230 r = read(sockfd, challenge + total, sizeof(challenge) - total);
235 if (errno == ECONNRESET) {
236 fprintf(stderr, "%s: Connection reset",
241 if (errno == EINTR) continue;
243 fprintf(stderr, "%s: Failed reading data: %s\n",
244 progname, fr_syserror(errno));
251 fr_hmac_md5((uint8_t const *) secret, strlen(secret),
252 challenge, sizeof(challenge), challenge);
254 if (write(sockfd, challenge, sizeof(challenge)) < 0) {
255 fprintf(stderr, "%s: Failed writing challenge data: %s\n",
256 progname, fr_syserror(errno));
260 static ssize_t run_command(int sockfd, char const *command,
261 char *buffer, size_t bufsize)
267 fprintf(outputfp, "%s\n", command);
271 * Write the text to the socket.
273 if (write(sockfd, command, strlen(command)) < 0) return -1;
274 if (write(sockfd, "\r\n", 2) < 0) return -1;
282 memset(buffer, 0, bufsize);
289 FD_SET(sockfd, &readfds);
291 rcode = select(sockfd + 1, &readfds, NULL, NULL, NULL);
293 if (errno == EINTR) continue;
295 fprintf(stderr, "%s: Failed selecting: %s\n",
296 progname, fr_syserror(errno));
301 fprintf(stderr, "%s: Server closed the connection.\n",
307 len = recv(sockfd, buffer + size,
308 bufsize - size - 1, MSG_DONTWAIT);
311 * Read one byte at a time (ugh)
313 len = recv(sockfd, buffer + size, 1, 0);
317 * No data: keep looping
319 if ((errno == EAGAIN) || (errno == EINTR)) {
323 fprintf(stderr, "%s: Error reading socket: %s\n",
324 progname, fr_syserror(errno));
327 if (len == 0) return 0; /* clean exit */
333 * There really is a better way of doing this.
335 p = strstr(buffer, "radmin> ");
342 if (p[-1] == '\n') p[-1] = '\0';
348 * Blank prompt. Go get another command.
350 if (!buffer[0]) return 1;
352 buffer[size] = '\0'; /* this is at least right */
357 #define MAX_COMMANDS (4)
359 int main(int argc, char **argv)
363 bool done_license = false;
365 uint32_t magic, needed;
368 char const *file = NULL;
369 char const *name = "radiusd";
370 char *p, buffer[65536];
371 char const *input_file = NULL;
372 FILE *inputfp = stdin;
373 char const *output_file = NULL;
374 char const *server = NULL;
376 char const *radius_dir = RADIUS_DIR;
377 char const *dict_dir = DICTDIR;
379 char *commands[MAX_COMMANDS];
380 int num_commands = -1;
383 if (fr_fault_setup(getenv("PANIC_ACTION"), argv[0]) < 0) {
389 talloc_set_log_stderr();
391 outputfp = stdout; /* stdout is not a constant value... */
393 if ((progname = strrchr(argv[0], FR_DIR_SEP)) == NULL) {
399 while ((argval = getopt(argc, argv, "d:D:hi:e:Ef:n:o:qs:S")) != EOF) {
403 fprintf(stderr, "%s: -d and -f cannot be used together.\n", progname);
407 fprintf(stderr, "%s: -d and -s cannot be used together.\n", progname);
418 num_commands++; /* starts at -1 */
419 if (num_commands >= MAX_COMMANDS) {
420 fprintf(stderr, "%s: Too many '-e'\n",
424 commands[num_commands] = optarg;
442 if (strcmp(optarg, "-") != 0) {
453 if (strcmp(optarg, "-") != 0) {
454 output_file = optarg;
465 fprintf(stderr, "%s: -s and -f cannot be used together.\n", progname);
479 * Mismatch between the binary and the libraries it depends on
481 if (fr_check_lib_magic(RADIUSD_MAGIC_NUMBER) < 0) {
488 CONF_SECTION *cs, *subcs;
490 file = NULL; /* MUST read it from the conffile now */
492 snprintf(buffer, sizeof(buffer), "%s/%s.conf", radius_dir, name);
495 * Need to read in the dictionaries, else we may get
496 * validation errors when we try and parse the config.
498 if (dict_init(dict_dir, RADIUS_DICTIONARY) < 0) {
503 if (dict_read(radius_dir, RADIUS_DICTIONARY) == -1) {
508 cs = cf_file_read(buffer);
510 fprintf(stderr, "%s: Errors reading or parsing %s\n", progname, buffer);
515 while ((subcs = cf_subsection_find_next(cs, subcs, "listen")) != NULL) {
517 CONF_PAIR *cp = cf_pair_find(subcs, "type");
521 value = cf_pair_value(cp);
522 if (!value) continue;
524 if (strcmp(value, "control") != 0) continue;
527 * Now find the socket name (sigh)
529 rcode = cf_item_parse(subcs, "socket", FR_ITEM_POINTER(PW_TYPE_STRING, &file), NULL);
531 fprintf(stderr, "%s: Failed parsing listen section\n", progname);
536 fprintf(stderr, "%s: No path given for socket\n", progname);
543 fprintf(stderr, "%s: Could not find control socket in %s\n", progname, buffer);
549 inputfp = fopen(input_file, "r");
551 fprintf(stderr, "%s: Failed opening %s: %s\n", progname, input_file, fr_syserror(errno));
557 outputfp = fopen(output_file, "w");
559 fprintf(stderr, "%s: Failed creating %s: %s\n", progname, output_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);
587 * FIXME: Get destination from command line, if possible?
589 sockfd = fr_domain_socket(file);
594 sockfd = client_socket(server);
598 * Read initial magic && version information.
600 for (size = 0; size < 8; size += len) {
601 len = read(sockfd, buffer + size, 8 - size);
603 fprintf(stderr, "%s: Error reading initial data from socket: %s\n",
604 progname, fr_syserror(errno));
609 memcpy(&magic, buffer, 4);
610 magic = ntohl(magic);
611 if (magic != 0xf7eead15) {
612 fprintf(stderr, "%s: Socket %s is not FreeRADIUS administration socket\n", progname, file);
616 memcpy(&magic, buffer + 4, 4);
617 magic = ntohl(magic);
625 if (magic != needed) {
626 fprintf(stderr, "%s: Socket version mismatch: Need %d, got %d\n",
627 progname, needed, magic);
631 if (server && secret) do_challenge(sockfd);
636 if (num_commands >= 0) {
639 for (i = 0; i <= num_commands; i++) {
640 size = run_command(sockfd, commands[i],
641 buffer, sizeof(buffer));
642 if (size < 0) exit(1);
645 fputs(buffer, outputfp);
646 fprintf(outputfp, "\n");
653 if (!done_license && !quiet) {
654 printf("%s - FreeRADIUS Server administration tool.\n", radmin_version);
655 printf("Copyright (C) 2008-2014 The FreeRADIUS server project and contributors.\n");
656 printf("There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A\n");
657 printf("PARTICULAR PURPOSE.\n");
658 printf("You may redistribute copies of FreeRADIUS under the terms of the\n");
659 printf("GNU General Public License v2.\n");
676 line = readline("radmin> ");
685 #ifdef USE_READLINE_HISTORY
688 } else /* quiet, or no readline */
691 line = fgets(buffer, sizeof(buffer), inputfp);
694 p = strchr(buffer, '\n');
696 fprintf(stderr, "%s: Input line too long\n",
704 * Strip off leading spaces.
706 for (p = line; *p != '\0'; p++) {
722 * Comments: keep going.
729 for (p = line; *p != '\0'; p++) {
730 if ((p[0] == '\r') ||
738 if (strcmp(line, "reconnect") == 0) {
744 if (memcmp(line, "secret ", 7) == 0) {
747 do_challenge(sockfd);
756 if ((strcmp(line, "exit") == 0) ||
757 (strcmp(line, "quit") == 0)) {
761 if (server && !secret) {
762 fprintf(stderr, "ERROR: You must enter 'secret <SECRET>' before running any commands\n");
767 size = run_command(sockfd, line, buffer, sizeof(buffer));
768 if (size <= 0) break; /* error, or clean exit */
770 if (size == 1) continue; /* no output. */
772 fputs(buffer, outputfp);
774 fprintf(outputfp, "\n");
777 fprintf(outputfp, "\n");