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>
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 #ifdef HAVE_READLINE_HISTORY
64 #if defined(HAVE_READLINE_HISTORY_H)
65 #include <readline/history.h>
66 #define USE_READLINE_HISTORY (1)
67 #elif defined(HAVE_HISTORY_H)
69 #define USE_READLINE_HISTORY (1)
70 #endif /* defined(HAVE_READLINE_HISTORY_H) */
72 #endif /* HAVE_READLINE_HISTORY */
74 #endif /* HAVE_LIBREADLINE */
77 * For configuration file stuff.
79 const char *radius_dir = RADDBDIR;
80 const char *progname = "radmin";
81 const char *radmin_version = "radmin version " RADIUSD_VERSION_STRING
82 #ifdef RADIUSD_VERSION_COMMIT
83 " (git #" RADIUSD_VERSION_COMMIT ")"
85 ", built on " __DATE__ " at " __TIME__;
89 * The rest of this is because the conffile.c, etc. assume
90 * they're running inside of the server. And we don't (yet)
91 * have a "libfreeradius-server", or "libfreeradius-util".
94 struct main_config_t mainconfig;
95 char *request_log_file = NULL;
96 char *debug_log_file = NULL;
97 size_t radius_xlat(UNUSED char *out, UNUSED int outlen, UNUSED const char *fmt,
98 UNUSED REQUEST *request,
99 UNUSED RADIUS_ESCAPE_STRING func, UNUSED void *arg)
103 int check_config = FALSE;
105 static FILE *outputfp = NULL;
106 static int echo = FALSE;
107 static const char *secret = "testing123";
109 static int fr_domain_socket(const char *path)
115 struct sockaddr_un saremote;
118 if (len >= sizeof(saremote.sun_path)) {
119 fprintf(stderr, "%s: Path too long in filename\n", progname);
123 if ((sockfd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
124 fprintf(stderr, "%s: Failed creating socket: %s\n",
125 progname, strerror(errno));
129 saremote.sun_family = AF_UNIX;
130 memcpy(saremote.sun_path, path, len + 1); /* SUN_LEN does strlen */
132 socklen = SUN_LEN(&saremote);
134 if (connect(sockfd, (struct sockaddr *)&saremote, socklen) < 0) {
138 fprintf(stderr, "%s: Failed connecting to %s: %s\n",
139 progname, path, strerror(errno));
142 * The file doesn't exist. Tell the user how to
145 if ((stat(path, &buf) < 0) &&
147 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");
157 if ((flags = fcntl(sockfd, F_GETFL, NULL)) < 0) {
158 fprintf(stderr, "%s: Failure getting socket flags: %s",
159 progname, strerror(errno));
165 if( fcntl(sockfd, F_SETFL, flags) < 0) {
166 fprintf(stderr, "%s: Failure setting socket flags: %s",
167 progname, strerror(errno));
177 static int client_socket(const char *server)
181 char *p, buffer[1024];
183 strlcpy(buffer, server, sizeof(buffer));
185 p = strchr(buffer, ':');
187 port = PW_RADMIN_PORT;
193 if (ip_hton(buffer, AF_INET, &ipaddr) < 0) {
194 fprintf(stderr, "%s: Failed looking up host %s: %s\n",
195 progname, buffer, strerror(errno));
199 sockfd = fr_tcp_client_socket(NULL, &ipaddr, port);
201 fprintf(stderr, "%s: Failed opening socket %s: %s\n",
202 progname, server, strerror(errno));
209 static void do_challenge(int sockfd)
213 uint8_t challenge[16];
215 for (total = 0; total < sizeof(challenge); ) {
216 r = read(sockfd, challenge + total, sizeof(challenge) - total);
221 if (errno == ECONNRESET) {
222 fprintf(stderr, "%s: Connection reset",
227 if (errno == EINTR) continue;
229 fprintf(stderr, "%s: Failed reading data: %s\n",
230 progname, strerror(errno));
237 fr_hmac_md5((const uint8_t *) secret, strlen(secret),
238 challenge, sizeof(challenge), challenge);
240 write(sockfd, challenge, sizeof(challenge));
244 static int usage(void)
246 printf("Usage: %s [ args ]\n", progname);
247 printf(" -d raddb_dir Configuration files are in \"raddbdir/*\".\n");
248 printf(" -e command Execute 'command' and then exit.\n");
249 printf(" -E Echo commands as they are being executed.\n");
250 printf(" -f socket_file Open socket_file directly, without reading radius.conf\n");
251 printf(" -h Print usage help information.\n");
252 printf(" -i input_file Read commands from 'input_file'.\n");
253 printf(" -n name Read raddb/name.conf instead of raddb/radiusd.conf\n");
254 printf(" -o output_file Write commands to 'output_file'.\n");
255 printf(" -q Quiet mode.\n");
260 static ssize_t run_command(int sockfd, const char *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, strerror(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, strerror(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)
361 int argval, quiet = 0;
362 int done_license = 0;
364 uint32_t magic, needed;
367 const char *file = NULL;
368 const char *name = "radiusd";
369 char *p, buffer[65536];
370 const char *input_file = NULL;
371 FILE *inputfp = stdin;
372 const char *output_file = NULL;
373 const char *server = NULL;
375 char *commands[MAX_COMMANDS];
376 int num_commands = -1;
378 outputfp = stdout; /* stdout is not a constant value... */
380 if ((progname = strrchr(argv[0], FR_DIR_SEP)) == NULL)
385 while ((argval = getopt(argc, argv, "d:hi:e:Ef:n:o:qs:S")) != EOF) {
389 fprintf(stderr, "%s: -d and -f cannot be used together.\n", progname);
393 fprintf(stderr, "%s: -d and -s cannot be used together.\n", progname);
400 num_commands++; /* starts at -1 */
401 if (num_commands >= MAX_COMMANDS) {
402 fprintf(stderr, "%s: Too many '-e'\n",
406 commands[num_commands] = optarg;
424 if (strcmp(optarg, "-") != 0) {
435 if (strcmp(optarg, "-") != 0) {
436 output_file = optarg;
447 fprintf(stderr, "%s: -s and -f cannot be used together.\n", progname);
462 CONF_SECTION *cs, *subcs;
464 file = NULL; /* MUST read it from the conffile now */
466 snprintf(buffer, sizeof(buffer), "%s/%s.conf",
469 cs = cf_file_read(buffer);
471 fprintf(stderr, "%s: Errors reading or parsing %s\n",
477 while ((subcs = cf_subsection_find_next(cs, subcs, "listen")) != NULL) {
479 CONF_PAIR *cp = cf_pair_find(subcs, "type");
483 value = cf_pair_value(cp);
484 if (!value) continue;
486 if (strcmp(value, "control") != 0) continue;
489 * Now find the socket name (sigh)
491 rcode = cf_item_parse(subcs, "socket",
495 fprintf(stderr, "%s: Failed parsing listen section\n", progname);
500 fprintf(stderr, "%s: No path given for socket\n",
508 fprintf(stderr, "%s: Could not find control socket in %s\n",
515 inputfp = fopen(input_file, "r");
517 fprintf(stderr, "%s: Failed opening %s: %s\n",
518 progname, input_file, strerror(errno));
524 outputfp = fopen(output_file, "w");
526 fprintf(stderr, "%s: Failed creating %s: %s\n",
527 progname, output_file, strerror(errno));
533 * Check if stdin is a TTY only if input is from stdin
535 if (input_file && !quiet && !isatty(STDIN_FILENO)) quiet = 1;
539 #ifdef USE_READLINE_HISTORY
542 rl_bind_key('\t', rl_insert);
549 * FIXME: Get destination from command line, if possible?
551 sockfd = fr_domain_socket(file);
556 sockfd = client_socket(server);
560 * Read initial magic && version information.
562 for (size = 0; size < 8; size += len) {
563 len = read(sockfd, buffer + size, 8 - size);
565 fprintf(stderr, "%s: Error reading initial data from socket: %s\n",
566 progname, strerror(errno));
571 memcpy(&magic, buffer, 4);
572 magic = ntohl(magic);
573 if (magic != 0xf7eead15) {
574 fprintf(stderr, "%s: Socket %s is not FreeRADIUS administration socket\n", progname, file);
578 memcpy(&magic, buffer + 4, 4);
579 magic = ntohl(magic);
587 if (magic != needed) {
588 fprintf(stderr, "%s: Socket version mismatch: Need %d, got %d\n",
589 progname, needed, magic);
593 if (server && secret) do_challenge(sockfd);
598 if (num_commands >= 0) {
601 for (i = 0; i <= num_commands; i++) {
602 size = run_command(sockfd, commands[i],
603 buffer, sizeof(buffer));
604 if (size < 0) exit(1);
607 fputs(buffer, outputfp);
608 fprintf(outputfp, "\n");
615 if (!done_license && !quiet) {
616 printf("%s - FreeRADIUS Server administration tool.\n", radmin_version);
617 printf("Copyright (C) 2008-2012 The FreeRADIUS server project and contributors.\n");
618 printf("There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A\n");
619 printf("PARTICULAR PURPOSE.\n");
620 printf("You may redistribute copies of FreeRADIUS under the terms of the\n");
621 printf("GNU General Public License v2.\n");
638 line = readline("radmin> ");
647 #ifdef USE_READLINE_HISTORY
650 } else /* quiet, or no readline */
653 line = fgets(buffer, sizeof(buffer), inputfp);
656 p = strchr(buffer, '\n');
658 fprintf(stderr, "%s: Input line too long\n",
666 * Strip off leading spaces.
668 for (p = line; *p != '\0'; p++) {
684 * Comments: keep going.
691 for (p = line; *p != '\0'; p++) {
692 if ((p[0] == '\r') ||
700 if (strcmp(line, "reconnect") == 0) {
706 if (memcmp(line, "secret ", 7) == 0) {
709 do_challenge(sockfd);
718 if ((strcmp(line, "exit") == 0) ||
719 (strcmp(line, "quit") == 0)) {
723 if (server && !secret) {
724 fprintf(stderr, "ERROR: You must enter 'secret <SECRET>' before running any commands\n");
729 size = run_command(sockfd, line, buffer, sizeof(buffer));
730 if (size <= 0) break; /* error, or clean exit */
732 if (size == 1) continue; /* no output. */
734 fputs(buffer, outputfp);
736 fprintf(outputfp, "\n");
739 fprintf(outputfp, "\n");