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>
42 #ifdef HAVE_LIBREADLINE
43 #if defined(HAVE_READLINE_READLINE_H)
44 #include <readline/readline.h>
45 #elif defined(HAVE_READLINE_H)
47 #else /* !defined(HAVE_READLINE_H) */
48 extern char *readline ();
49 #endif /* !defined(HAVE_READLINE_H) */
51 #else /* !defined(HAVE_READLINE_READLINE_H) */
53 #endif /* HAVE_LIBREADLINE */
55 #ifdef HAVE_READLINE_HISTORY
56 #if defined(HAVE_READLINE_HISTORY_H)
57 #include <readline/history.h>
58 #elif defined(HAVE_HISTORY_H)
60 #else /* !defined(HAVE_HISTORY_H) */
61 extern void add_history ();
62 extern int write_history ();
63 extern int read_history ();
64 #endif /* defined(HAVE_READLINE_HISTORY_H) */
66 #endif /* HAVE_READLINE_HISTORY */
69 * For configuration file stuff.
71 const char *radius_dir = RADDBDIR;
72 const char *progname = "radmin";
75 * The rest of this is because the conffile.c, etc. assume
76 * they're running inside of the server. And we don't (yet)
77 * have a "libfreeradius-server", or "libfreeradius-util".
80 struct main_config_t mainconfig;
81 char *request_log_file = NULL;
82 char *debug_log_file = NULL;
83 int radius_xlat(UNUSED char *out, UNUSED int outlen, UNUSED const char *fmt,
84 UNUSED REQUEST *request, UNUSED RADIUS_ESCAPE_STRING func)
89 static int fr_domain_socket(const char *path)
95 struct sockaddr_un saremote;
98 if (len >= sizeof(saremote.sun_path)) {
99 fprintf(stderr, "%s: Path too long in filename\n", progname);
103 if ((sockfd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
104 fprintf(stderr, "%s: Failed creating socket: %s\n",
105 progname, strerror(errno));
109 saremote.sun_family = AF_UNIX;
110 memcpy(saremote.sun_path, path, len); /* not zero terminated */
112 socklen = sizeof(saremote.sun_family) + len;
114 if (connect(sockfd, (struct sockaddr *)&saremote, socklen) < 0) {
115 fprintf(stderr, "%s: Failed connecting to %s: %s\n",
116 progname, path, strerror(errno));
125 if ((flags = fcntl(sockfd, F_GETFL, NULL)) < 0) {
126 fprintf(stderr, "%s: Failure getting socket flags: %s",
127 progname, strerror(errno));
133 if( fcntl(sockfd, F_SETFL, flags) < 0) {
134 fprintf(stderr, "%s: Failure setting socket flags: %s",
135 progname, strerror(errno));
145 static int usage(void)
147 printf("Usage: %s [ args ]\n", progname);
148 printf(" -d raddb_dir Configuration files are in \"raddbdir/*\".\n");
149 printf(" -e command Execute 'command' and then exit.\n");
150 printf(" -f socket_file Open socket_file directly, without reading radius.conf\n");
151 printf(" -i input_file Read commands from 'input_file'.\n");
152 printf(" -n name Read raddb/name.conf instead of raddb/radiusd.conf\n");
153 printf(" -o output_file Write commands to 'output_file'.\n");
154 printf(" -q Quiet mode.\n");
159 static ssize_t run_command(int sockfd, const char *command,
160 char *buffer, size_t bufsize)
167 * Write the text to the socket.
169 if (write(sockfd, command, strlen(command)) < 0) return -1;
170 if (write(sockfd, "\r\n", 2) < 0) return -1;
178 memset(buffer, 0, bufsize);
185 FD_SET(sockfd, &readfds);
187 rcode = select(sockfd + 1, &readfds, NULL, NULL, NULL);
189 if (errno == EINTR) continue;
191 fprintf(stderr, "%s: Failed selecting: %s\n",
192 progname, strerror(errno));
197 len = recv(sockfd, buffer + size,
198 bufsize - size - 1, MSG_DONTWAIT);
201 * Read one byte at a time (ugh)
203 len = recv(sockfd, buffer + size, 1, 0);
207 * No data: keep looping
209 if ((errno == EAGAIN) || (errno == EINTR)) {
213 fprintf(stderr, "%s: Error reading socket: %s\n",
214 progname, strerror(errno));
217 if (len == 0) return 0; /* clean exit */
223 * There really is a better way of doing this.
225 p = strstr(buffer, "radmin> ");
232 if (p[-1] == '\n') p[-1] = '\0';
240 * Blank prompt. Go get another command.
242 if (!buffer[0]) return 1;
244 buffer[size] = '\0'; /* this is at least right */
250 int main(int argc, char **argv)
252 int argval, quiet = 0;
253 int done_license = 0;
258 const char *file = NULL;
259 const char *name = "radiusd";
260 char *p, buffer[65536];
261 const char *input_file = NULL;
262 FILE *inputfp = stdin;
263 const char *output_file = NULL;
264 FILE *outputfp = stdout;
266 if ((progname = strrchr(argv[0], FR_DIR_SEP)) == NULL)
271 while ((argval = getopt(argc, argv, "d:hi:e:f:n:o:q")) != EOF) {
275 fprintf(stderr, "%s: -d and -f cannot be used together.\n", progname);
296 if (strcmp(optarg, "-") != 0) {
307 if (strcmp(optarg, "-") != 0) {
308 output_file = optarg;
321 CONF_SECTION *cs, *subcs;
323 file = NULL; /* MUST read it from the conffile now */
325 snprintf(buffer, sizeof(buffer), "%s/%s.conf",
328 cs = cf_file_read(buffer);
330 fprintf(stderr, "%s: Errors reading %s\n",
336 while ((subcs = cf_subsection_find_next(cs, subcs, "listen")) != NULL) {
338 CONF_PAIR *cp = cf_pair_find(subcs, "type");
342 value = cf_pair_value(cp);
343 if (!value) continue;
345 if (strcmp(value, "control") != 0) continue;
348 * Now find the socket name (sigh)
350 rcode = cf_item_parse(subcs, "socket",
354 fprintf(stderr, "%s: Failed parsing listen section\n", progname);
359 fprintf(stderr, "%s: No path given for socket\n",
367 fprintf(stderr, "%s: Could not find control socket in %s\n",
374 inputfp = fopen(input_file, "r");
376 fprintf(stderr, "%s: Failed opening %s: %s\n",
377 progname, input_file, strerror(errno));
383 outputfp = fopen(output_file, "w");
385 fprintf(stderr, "%s: Failed creating %s: %s\n",
386 progname, output_file, strerror(errno));
392 * Check if stdin is a TTY only if input is from stdin
394 if (input_file && !quiet && !isatty(STDIN_FILENO)) quiet = 1;
396 #ifdef HAVE_LIBREADLINE
399 rl_bind_key('\t', rl_insert);
405 * FIXME: Get destination from command line, if possible?
407 sockfd = fr_domain_socket(file);
413 * Read initial magic && version information.
415 for (size = 0; size < 8; size += len) {
416 len = read(sockfd, buffer + size, 8 - size);
418 fprintf(stderr, "%s: Error reading initial data from socket: %s\n",
419 progname, strerror(errno));
424 memcpy(&magic, buffer, 4);
425 magic = ntohl(magic);
426 if (magic != 0xf7eead15) {
427 fprintf(stderr, "%s: Socket %s is not FreeRADIUS administration socket\n", progname, file);
431 memcpy(&magic, buffer + 4, 4);
432 magic = ntohl(magic);
434 fprintf(stderr, "%s: Socket version mismatch: Need 1, got %d\n",
443 size = run_command(sockfd, line, buffer, sizeof(buffer));
444 if (size < 0) exit(1);
445 if ((size == 0) || (size == 1)) exit(0);
447 fputs(buffer, outputfp);
448 fprintf(outputfp, "\n");
453 if (!done_license && !quiet) {
454 printf("radmin " RADIUSD_VERSION " - FreeRADIUS Server administration tool.\n");
455 printf("Copyright (C) 2008 The FreeRADIUS server project and contributors.\n");
456 printf("There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A\n");
457 printf("PARTICULAR PURPOSE.\n");
458 printf("You may redistribute copies of FreeRADIUS under the terms of the\n");
459 printf("GNU General Public License v2.\n");
469 #ifndef HAVE_LIBREADLINE
476 line = readline("radmin> ");
486 } else /* quiet, or no readline */
489 line = fgets(buffer, sizeof(buffer), inputfp);
492 p = strchr(buffer, '\n');
494 fprintf(stderr, "%s: Input line too long\n",
501 for (p = line; *p != '\0'; p++) {
515 * Comments: keep going.
520 if (strcmp(line, "reconnect") == 0) {
529 if ((strcmp(line, "exit") == 0) ||
530 (strcmp(line, "quit") == 0)) {
534 size = run_command(sockfd, line, buffer, sizeof(buffer));
535 if (size <= 0) break; /* error, or clean exit */
537 if (size == 1) continue; /* no output. */
539 fputs(buffer, outputfp);
541 fprintf(outputfp, "\n");
544 fprintf(outputfp, "\n");