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>
37 #define SUN_LEN(su) (sizeof(*(su)) - sizeof((su)->sun_path) + strlen((su)->sun_path))
45 #ifdef HAVE_LIBREADLINE
46 #if defined(HAVE_READLINE_READLINE_H)
47 #include <readline/readline.h>
48 #elif defined(HAVE_READLINE_H)
50 #else /* !defined(HAVE_READLINE_H) */
51 extern char *readline ();
52 #endif /* !defined(HAVE_READLINE_H) */
54 #else /* !defined(HAVE_READLINE_READLINE_H) */
56 #endif /* HAVE_LIBREADLINE */
58 #ifdef HAVE_READLINE_HISTORY
59 #if defined(HAVE_READLINE_HISTORY_H)
60 #include <readline/history.h>
61 #elif defined(HAVE_HISTORY_H)
63 #else /* !defined(HAVE_HISTORY_H) */
64 extern void add_history ();
65 extern int write_history ();
66 extern int read_history ();
67 #endif /* defined(HAVE_READLINE_HISTORY_H) */
69 #endif /* HAVE_READLINE_HISTORY */
72 * For configuration file stuff.
74 const char *radius_dir = RADDBDIR;
75 const char *progname = "radmin";
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".
83 struct main_config_t mainconfig;
84 char *request_log_file = NULL;
85 char *debug_log_file = NULL;
86 int radius_xlat(UNUSED char *out, UNUSED int outlen, UNUSED const char *fmt,
87 UNUSED REQUEST *request, UNUSED RADIUS_ESCAPE_STRING func)
92 static FILE *outputfp = NULL;
93 static int echo = FALSE;
95 static int fr_domain_socket(const char *path)
101 struct sockaddr_un saremote;
104 if (len >= sizeof(saremote.sun_path)) {
105 fprintf(stderr, "%s: Path too long in filename\n", progname);
109 if ((sockfd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
110 fprintf(stderr, "%s: Failed creating socket: %s\n",
111 progname, strerror(errno));
115 saremote.sun_family = AF_UNIX;
116 memcpy(saremote.sun_path, path, len + 1); /* SUN_LEN does strlen */
118 socklen = SUN_LEN(&saremote);
120 if (connect(sockfd, (struct sockaddr *)&saremote, socklen) < 0) {
121 fprintf(stderr, "%s: Failed connecting to %s: %s\n",
122 progname, path, strerror(errno));
131 if ((flags = fcntl(sockfd, F_GETFL, NULL)) < 0) {
132 fprintf(stderr, "%s: Failure getting socket flags: %s",
133 progname, strerror(errno));
139 if( fcntl(sockfd, F_SETFL, flags) < 0) {
140 fprintf(stderr, "%s: Failure setting socket flags: %s",
141 progname, strerror(errno));
151 static int usage(void)
153 printf("Usage: %s [ args ]\n", progname);
154 printf(" -d raddb_dir Configuration files are in \"raddbdir/*\".\n");
155 printf(" -e command Execute 'command' and then exit.\n");
156 printf(" -E Echo commands as they are being executed.\n");
157 printf(" -f socket_file Open socket_file directly, without reading radius.conf\n");
158 printf(" -i input_file Read commands from 'input_file'.\n");
159 printf(" -n name Read raddb/name.conf instead of raddb/radiusd.conf\n");
160 printf(" -o output_file Write commands to 'output_file'.\n");
161 printf(" -q Quiet mode.\n");
166 static ssize_t run_command(int sockfd, const char *command,
167 char *buffer, size_t bufsize)
174 fprintf(outputfp, "%s\n", command);
178 * Write the text to the socket.
180 if (write(sockfd, command, strlen(command)) < 0) return -1;
181 if (write(sockfd, "\r\n", 2) < 0) return -1;
189 memset(buffer, 0, bufsize);
196 FD_SET(sockfd, &readfds);
198 rcode = select(sockfd + 1, &readfds, NULL, NULL, NULL);
200 if (errno == EINTR) continue;
202 fprintf(stderr, "%s: Failed selecting: %s\n",
203 progname, strerror(errno));
208 len = recv(sockfd, buffer + size,
209 bufsize - size - 1, MSG_DONTWAIT);
212 * Read one byte at a time (ugh)
214 len = recv(sockfd, buffer + size, 1, 0);
218 * No data: keep looping
220 if ((errno == EAGAIN) || (errno == EINTR)) {
224 fprintf(stderr, "%s: Error reading socket: %s\n",
225 progname, strerror(errno));
228 if (len == 0) return 0; /* clean exit */
234 * There really is a better way of doing this.
236 p = strstr(buffer, "radmin> ");
243 if (p[-1] == '\n') p[-1] = '\0';
251 * Blank prompt. Go get another command.
253 if (!buffer[0]) return 1;
255 buffer[size] = '\0'; /* this is at least right */
260 #define MAX_COMMANDS (4)
262 int main(int argc, char **argv)
264 int argval, quiet = 0;
265 int done_license = 0;
270 const char *file = NULL;
271 const char *name = "radiusd";
272 char *p, buffer[65536];
273 const char *input_file = NULL;
274 FILE *inputfp = stdin;
275 const char *output_file = NULL;
277 char *commands[MAX_COMMANDS];
278 int num_commands = -1;
280 outputfp = stdout; /* stdout is not a constant value... */
282 if ((progname = strrchr(argv[0], FR_DIR_SEP)) == NULL)
287 while ((argval = getopt(argc, argv, "d:hi:e:Ef:n:o:q")) != EOF) {
291 fprintf(stderr, "%s: -d and -f cannot be used together.\n", progname);
298 num_commands++; /* starts at -1 */
299 if (num_commands >= MAX_COMMANDS) {
300 fprintf(stderr, "%s: Too many '-e'\n",
304 commands[num_commands] = optarg;
322 if (strcmp(optarg, "-") != 0) {
333 if (strcmp(optarg, "-") != 0) {
334 output_file = optarg;
347 CONF_SECTION *cs, *subcs;
349 file = NULL; /* MUST read it from the conffile now */
351 snprintf(buffer, sizeof(buffer), "%s/%s.conf",
354 cs = cf_file_read(buffer);
356 fprintf(stderr, "%s: Errors reading %s\n",
362 while ((subcs = cf_subsection_find_next(cs, subcs, "listen")) != NULL) {
364 CONF_PAIR *cp = cf_pair_find(subcs, "type");
368 value = cf_pair_value(cp);
369 if (!value) continue;
371 if (strcmp(value, "control") != 0) continue;
374 * Now find the socket name (sigh)
376 rcode = cf_item_parse(subcs, "socket",
380 fprintf(stderr, "%s: Failed parsing listen section\n", progname);
385 fprintf(stderr, "%s: No path given for socket\n",
393 fprintf(stderr, "%s: Could not find control socket in %s\n",
400 inputfp = fopen(input_file, "r");
402 fprintf(stderr, "%s: Failed opening %s: %s\n",
403 progname, input_file, strerror(errno));
409 outputfp = fopen(output_file, "w");
411 fprintf(stderr, "%s: Failed creating %s: %s\n",
412 progname, output_file, strerror(errno));
418 * Check if stdin is a TTY only if input is from stdin
420 if (input_file && !quiet && !isatty(STDIN_FILENO)) quiet = 1;
422 #ifdef HAVE_LIBREADLINE
425 rl_bind_key('\t', rl_insert);
431 * FIXME: Get destination from command line, if possible?
433 sockfd = fr_domain_socket(file);
439 * Read initial magic && version information.
441 for (size = 0; size < 8; size += len) {
442 len = read(sockfd, buffer + size, 8 - size);
444 fprintf(stderr, "%s: Error reading initial data from socket: %s\n",
445 progname, strerror(errno));
450 memcpy(&magic, buffer, 4);
451 magic = ntohl(magic);
452 if (magic != 0xf7eead15) {
453 fprintf(stderr, "%s: Socket %s is not FreeRADIUS administration socket\n", progname, file);
457 memcpy(&magic, buffer + 4, 4);
458 magic = ntohl(magic);
460 fprintf(stderr, "%s: Socket version mismatch: Need 1, got %d\n",
468 if (num_commands >= 0) {
471 for (i = 0; i <= num_commands; i++) {
472 size = run_command(sockfd, commands[i],
473 buffer, sizeof(buffer));
474 if (size < 0) exit(1);
476 fputs(buffer, outputfp);
477 fprintf(outputfp, "\n");
483 if (!done_license && !quiet) {
484 printf("radmin " RADIUSD_VERSION " - FreeRADIUS Server administration tool.\n");
485 printf("Copyright (C) 2008 The FreeRADIUS server project and contributors.\n");
486 printf("There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A\n");
487 printf("PARTICULAR PURPOSE.\n");
488 printf("You may redistribute copies of FreeRADIUS under the terms of the\n");
489 printf("GNU General Public License v2.\n");
499 #ifndef HAVE_LIBREADLINE
506 line = readline("radmin> ");
516 } else /* quiet, or no readline */
519 line = fgets(buffer, sizeof(buffer), inputfp);
522 p = strchr(buffer, '\n');
524 fprintf(stderr, "%s: Input line too long\n",
532 * Strip off leading spaces.
534 for (p = line; *p != '\0'; p++) {
550 * Comments: keep going.
557 for (p = line; *p != '\0'; p++) {
558 if ((p[0] == '\r') ||
566 if (strcmp(line, "reconnect") == 0) {
575 if ((strcmp(line, "exit") == 0) ||
576 (strcmp(line, "quit") == 0)) {
580 size = run_command(sockfd, line, buffer, sizeof(buffer));
581 if (size <= 0) break; /* error, or clean exit */
583 if (size == 1) continue; /* no output. */
585 fputs(buffer, outputfp);
587 fprintf(outputfp, "\n");
590 fprintf(outputfp, "\n");