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 2008 The FreeRADIUS server project
21 * Copyright 2008 Alan DeKok <aland@deployingradius.com>
24 #include <freeradius-devel/ident.h>
27 #include <freeradius-devel/radiusd.h>
28 #include <freeradius-devel/radpaths.h>
30 #ifdef HAVE_SYS_SOCKET_H
31 #include <sys/socket.h>
34 #ifdef HAVE_NETINET_IN_H
35 #include <netinet/in.h>
41 #define SUN_LEN(su) (sizeof(*(su)) - sizeof((su)->sun_path) + strlen((su)->sun_path))
49 #ifdef HAVE_SYS_STAT_H
53 #ifdef HAVE_LIBREADLINE
55 #if defined(HAVE_READLINE_READLINE_H)
56 #include <readline/readline.h>
57 #define USE_READLINE (1)
58 #elif defined(HAVE_READLINE_H)
60 #define USE_READLINE (1)
61 #endif /* !defined(HAVE_READLINE_H) */
63 #endif /* HAVE_LIBREADLINE */
65 #ifdef HAVE_READLINE_HISTORY
66 #if defined(HAVE_READLINE_HISTORY_H)
67 #include <readline/history.h>
68 #define USE_READLINE_HISTORY (1)
69 #elif defined(HAVE_HISTORY_H)
71 #define USE_READLINE_HISTORY (1)
72 #endif /* defined(HAVE_READLINE_HISTORY_H) */
74 #endif /* HAVE_READLINE_HISTORY */
77 * For configuration file stuff.
79 char *radius_dir = RADDBDIR;
80 const char *progname = "radmin";
83 * The rest of this is because the conffile.c, etc. assume
84 * they're running inside of the server. And we don't (yet)
85 * have a "libfreeradius-server", or "libfreeradius-util".
88 struct main_config_t mainconfig;
89 char *request_log_file = NULL;
90 char *debug_log_file = NULL;
91 int radius_xlat(UNUSED char *out, UNUSED int outlen, UNUSED const char *fmt,
92 UNUSED REQUEST *request, UNUSED RADIUS_ESCAPE_STRING func)
96 int check_config = FALSE;
98 static FILE *outputfp = NULL;
99 static int echo = FALSE;
100 static const char *secret = "testing123";
102 static int fr_domain_socket(const char *path)
108 struct sockaddr_un saremote;
111 if (len >= sizeof(saremote.sun_path)) {
112 fprintf(stderr, "%s: Path too long in filename\n", progname);
116 if ((sockfd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
117 fprintf(stderr, "%s: Failed creating socket: %s\n",
118 progname, strerror(errno));
122 saremote.sun_family = AF_UNIX;
123 memcpy(saremote.sun_path, path, len + 1); /* SUN_LEN does strlen */
125 socklen = SUN_LEN(&saremote);
127 if (connect(sockfd, (struct sockaddr *)&saremote, socklen) < 0) {
131 fprintf(stderr, "%s: Failed connecting to %s: %s\n",
132 progname, path, strerror(errno));
135 * The file doesn't exist. Tell the user how to
138 if ((stat(path, &buf) < 0) &&
140 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");
150 if ((flags = fcntl(sockfd, F_GETFL, NULL)) < 0) {
151 fprintf(stderr, "%s: Failure getting socket flags: %s",
152 progname, strerror(errno));
158 if( fcntl(sockfd, F_SETFL, flags) < 0) {
159 fprintf(stderr, "%s: Failure setting socket flags: %s",
160 progname, strerror(errno));
170 static int client_socket(const char *server)
174 char *p, buffer[1024];
176 strlcpy(buffer, server, sizeof(buffer));
178 p = strchr(buffer, ':');
180 port = PW_RADMIN_PORT;
186 if (ip_hton(buffer, AF_INET, &ipaddr) < 0) {
187 fprintf(stderr, "%s: Failed looking up host %s: %s\n",
188 progname, buffer, strerror(errno));
192 sockfd = fr_tcp_client_socket(NULL, &ipaddr, port);
194 fprintf(stderr, "%s: Failed opening socket %s: %s\n",
195 progname, server, strerror(errno));
202 static void do_challenge(int sockfd)
206 uint8_t challenge[16];
208 for (total = 0; total < sizeof(challenge); ) {
209 r = read(sockfd, challenge + total, sizeof(challenge) - total);
214 if (errno == ECONNRESET) {
215 fprintf(stderr, "%s: Connection reset",
220 if (errno == EINTR) continue;
222 fprintf(stderr, "%s: Failed reading data: %s\n",
223 progname, strerror(errno));
230 fr_hmac_md5((const uint8_t *) secret, strlen(secret),
231 challenge, sizeof(challenge), challenge);
233 write(sockfd, challenge, sizeof(challenge));
237 static int usage(void)
239 printf("Usage: %s [ args ]\n", progname);
240 printf(" -d raddb_dir Configuration files are in \"raddbdir/*\".\n");
241 printf(" -e command Execute 'command' and then exit.\n");
242 printf(" -E Echo commands as they are being executed.\n");
243 printf(" -f socket_file Open socket_file directly, without reading radius.conf\n");
244 printf(" -h Print usage help information.\n");
245 printf(" -i input_file Read commands from 'input_file'.\n");
246 printf(" -n name Read raddb/name.conf instead of raddb/radiusd.conf\n");
247 printf(" -o output_file Write commands to 'output_file'.\n");
248 printf(" -q Quiet mode.\n");
253 static ssize_t run_command(int sockfd, const char *command,
254 char *buffer, size_t bufsize)
260 fprintf(outputfp, "%s\n", command);
264 * Write the text to the socket.
266 if (write(sockfd, command, strlen(command)) < 0) return -1;
267 if (write(sockfd, "\r\n", 2) < 0) return -1;
275 memset(buffer, 0, bufsize);
282 FD_SET(sockfd, &readfds);
284 rcode = select(sockfd + 1, &readfds, NULL, NULL, NULL);
286 if (errno == EINTR) continue;
288 fprintf(stderr, "%s: Failed selecting: %s\n",
289 progname, strerror(errno));
294 fprintf(stderr, "%s: Server closed the connection.\n",
300 len = recv(sockfd, buffer + size,
301 bufsize - size - 1, MSG_DONTWAIT);
304 * Read one byte at a time (ugh)
306 len = recv(sockfd, buffer + size, 1, 0);
310 * No data: keep looping
312 if ((errno == EAGAIN) || (errno == EINTR)) {
316 fprintf(stderr, "%s: Error reading socket: %s\n",
317 progname, strerror(errno));
320 if (len == 0) return 0; /* clean exit */
326 * There really is a better way of doing this.
328 p = strstr(buffer, "radmin> ");
335 if (p[-1] == '\n') p[-1] = '\0';
341 * Blank prompt. Go get another command.
343 if (!buffer[0]) return 1;
345 buffer[size] = '\0'; /* this is at least right */
350 #define MAX_COMMANDS (4)
352 int main(int argc, char **argv)
354 int argval, quiet = 0;
355 int done_license = 0;
357 uint32_t magic, needed;
360 const char *file = NULL;
361 const char *name = "radiusd";
362 char *p, buffer[65536];
363 const char *input_file = NULL;
364 FILE *inputfp = stdin;
365 const char *output_file = NULL;
366 const char *server = NULL;
368 char *commands[MAX_COMMANDS];
369 int num_commands = -1;
371 outputfp = stdout; /* stdout is not a constant value... */
373 if ((progname = strrchr(argv[0], FR_DIR_SEP)) == NULL)
378 while ((argval = getopt(argc, argv, "d:hi:e:Ef:n:o:qs:S")) != EOF) {
382 fprintf(stderr, "%s: -d and -f cannot be used together.\n", progname);
386 fprintf(stderr, "%s: -d and -s cannot be used together.\n", progname);
393 num_commands++; /* starts at -1 */
394 if (num_commands >= MAX_COMMANDS) {
395 fprintf(stderr, "%s: Too many '-e'\n",
399 commands[num_commands] = optarg;
417 if (strcmp(optarg, "-") != 0) {
428 if (strcmp(optarg, "-") != 0) {
429 output_file = optarg;
440 fprintf(stderr, "%s: -s and -f cannot be used together.\n", progname);
455 CONF_SECTION *cs, *subcs;
457 file = NULL; /* MUST read it from the conffile now */
459 snprintf(buffer, sizeof(buffer), "%s/%s.conf",
462 cs = cf_file_read(buffer);
464 fprintf(stderr, "%s: Errors reading %s\n",
470 while ((subcs = cf_subsection_find_next(cs, subcs, "listen")) != NULL) {
472 CONF_PAIR *cp = cf_pair_find(subcs, "type");
476 value = cf_pair_value(cp);
477 if (!value) continue;
479 if (strcmp(value, "control") != 0) continue;
482 * Now find the socket name (sigh)
484 rcode = cf_item_parse(subcs, "socket",
488 fprintf(stderr, "%s: Failed parsing listen section\n", progname);
493 fprintf(stderr, "%s: No path given for socket\n",
501 fprintf(stderr, "%s: Could not find control socket in %s\n",
508 inputfp = fopen(input_file, "r");
510 fprintf(stderr, "%s: Failed opening %s: %s\n",
511 progname, input_file, strerror(errno));
517 outputfp = fopen(output_file, "w");
519 fprintf(stderr, "%s: Failed creating %s: %s\n",
520 progname, output_file, strerror(errno));
526 * Check if stdin is a TTY only if input is from stdin
528 if (input_file && !quiet && !isatty(STDIN_FILENO)) quiet = 1;
532 #ifdef USE_READLINE_HISTORY
535 rl_bind_key('\t', rl_insert);
542 * FIXME: Get destination from command line, if possible?
544 sockfd = fr_domain_socket(file);
549 sockfd = client_socket(server);
553 * Read initial magic && version information.
555 for (size = 0; size < 8; size += len) {
556 len = read(sockfd, buffer + size, 8 - size);
558 fprintf(stderr, "%s: Error reading initial data from socket: %s\n",
559 progname, strerror(errno));
564 memcpy(&magic, buffer, 4);
565 magic = ntohl(magic);
566 if (magic != 0xf7eead15) {
567 fprintf(stderr, "%s: Socket %s is not FreeRADIUS administration socket\n", progname, file);
571 memcpy(&magic, buffer + 4, 4);
572 magic = ntohl(magic);
580 if (magic != needed) {
581 fprintf(stderr, "%s: Socket version mismatch: Need %d, got %d\n",
582 progname, needed, magic);
586 if (server && secret) do_challenge(sockfd);
591 if (num_commands >= 0) {
594 for (i = 0; i <= num_commands; i++) {
595 size = run_command(sockfd, commands[i],
596 buffer, sizeof(buffer));
597 if (size < 0) exit(1);
600 fputs(buffer, outputfp);
601 fprintf(outputfp, "\n");
608 if (!done_license && !quiet) {
609 printf("radmin " RADIUSD_VERSION " - FreeRADIUS Server administration tool.\n");
610 printf("Copyright (C) 2008-2011 The FreeRADIUS server project and contributors.\n");
611 printf("There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A\n");
612 printf("PARTICULAR PURPOSE.\n");
613 printf("You may redistribute copies of FreeRADIUS under the terms of the\n");
614 printf("GNU General Public License v2.\n");
631 line = readline("radmin> ");
640 #ifdef USE_READLINE_HISTORY
643 } else /* quiet, or no readline */
646 line = fgets(buffer, sizeof(buffer), inputfp);
649 p = strchr(buffer, '\n');
651 fprintf(stderr, "%s: Input line too long\n",
659 * Strip off leading spaces.
661 for (p = line; *p != '\0'; p++) {
677 * Comments: keep going.
684 for (p = line; *p != '\0'; p++) {
685 if ((p[0] == '\r') ||
693 if (strcmp(line, "reconnect") == 0) {
699 if (memcmp(line, "secret ", 7) == 0) {
702 do_challenge(sockfd);
711 if ((strcmp(line, "exit") == 0) ||
712 (strcmp(line, "quit") == 0)) {
716 if (server && !secret) {
717 fprintf(stderr, "ERROR: You must enter 'secret <SECRET>' before running any commands\n");
722 size = run_command(sockfd, line, buffer, sizeof(buffer));
723 if (size <= 0) break; /* error, or clean exit */
725 if (size == 1) continue; /* no output. */
727 fputs(buffer, outputfp);
729 fprintf(outputfp, "\n");
732 fprintf(outputfp, "\n");