Updated copyright year
[freeradius.git] / src / main / radmin.c
1 /*
2  * radmin.c     RADIUS Administration tool.
3  *
4  * Version:     $Id$
5  *
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.
10  *
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.
15  *
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
19  *
20  * Copyright 2008   The FreeRADIUS server project
21  * Copyright 2008   Alan DeKok <aland@deployingradius.com>
22  */
23
24 #include <freeradius-devel/ident.h>
25 RCSID("$Id$")
26
27 #include <freeradius-devel/radiusd.h>
28 #include <freeradius-devel/radpaths.h>
29
30 #ifdef HAVE_SYS_SOCKET_H
31 #include <sys/socket.h>
32 #endif
33
34 #ifdef HAVE_SYS_UN_H
35 #include <sys/un.h>
36 #ifndef SUN_LEN
37 #define SUN_LEN(su)  (sizeof(*(su)) - sizeof((su)->sun_path) + strlen((su)->sun_path))
38 #endif
39 #endif
40
41 #ifdef HAVE_GETOPT_H
42 #include <getopt.h>
43 #endif
44
45 #ifdef HAVE_SYS_STAT_H
46 #include <sys/stat.h>
47 #endif
48
49 #ifdef HAVE_LIBREADLINE
50
51 #if defined(HAVE_READLINE_READLINE_H)
52 #include <readline/readline.h>
53 #define USE_READLINE (1)
54 #elif defined(HAVE_READLINE_H)
55 #include <readline.h>
56 #define USE_READLINE (1)
57 #endif /* !defined(HAVE_READLINE_H) */
58
59 #endif /* HAVE_LIBREADLINE */
60
61 #ifdef HAVE_READLINE_HISTORY
62 #if defined(HAVE_READLINE_HISTORY_H)
63 #include <readline/history.h>
64 #define USE_READLINE_HISTORY (1)
65 #elif defined(HAVE_HISTORY_H)
66 #include <history.h>
67 #define USE_READLINE_HISTORY (1)
68 #endif /* defined(HAVE_READLINE_HISTORY_H) */
69
70 #endif /* HAVE_READLINE_HISTORY */
71
72 /*
73  *      For configuration file stuff.
74  */
75 char *radius_dir = RADDBDIR;
76 const char *progname = "radmin";
77
78 /*
79  *      The rest of this is because the conffile.c, etc. assume
80  *      they're running inside of the server.  And we don't (yet)
81  *      have a "libfreeradius-server", or "libfreeradius-util".
82  */
83 int debug_flag = 0;
84 struct main_config_t mainconfig;
85 char *request_log_file = NULL;
86 char *debug_log_file = NULL;
87 int radius_xlat(UNUSED char *out, UNUSED int outlen, UNUSED const char *fmt,
88                 UNUSED REQUEST *request, UNUSED RADIUS_ESCAPE_STRING func)
89 {
90         return -1;
91 }
92 int check_config = FALSE;
93
94 static FILE *outputfp = NULL;
95 static int echo = FALSE;
96
97 static int fr_domain_socket(const char *path)
98 {
99         int sockfd = -1;
100 #ifdef HAVE_SYS_UN_H
101         size_t len;
102         socklen_t socklen;
103         struct sockaddr_un saremote;
104
105         len = strlen(path);
106         if (len >= sizeof(saremote.sun_path)) {
107                 fprintf(stderr, "%s: Path too long in filename\n", progname);
108                 return -1;
109         }
110
111         if ((sockfd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
112                 fprintf(stderr, "%s: Failed creating socket: %s\n",
113                         progname, strerror(errno));
114                 return -1;
115         }
116
117         saremote.sun_family = AF_UNIX;
118         memcpy(saremote.sun_path, path, len + 1); /* SUN_LEN does strlen */
119         
120         socklen = SUN_LEN(&saremote);
121
122         if (connect(sockfd, (struct sockaddr *)&saremote, socklen) < 0) {
123                 struct stat buf;
124
125                 close(sockfd);
126                 fprintf(stderr, "%s: Failed connecting to %s: %s\n",
127                         progname, path, strerror(errno));
128
129                 /*
130                  *      The file doesn't exist.  Tell the user how to
131                  *      fix it.
132                  */
133                 if ((stat(path, &buf) < 0) &&
134                     (errno == ENOENT)) {
135                         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");
136                 }
137
138                 return -1;
139         }
140
141 #ifdef O_NONBLOCK
142         {
143                 int flags;
144                 
145                 if ((flags = fcntl(sockfd, F_GETFL, NULL)) < 0)  {
146                         fprintf(stderr, "%s: Failure getting socket flags: %s",
147                                 progname, strerror(errno));
148                         close(sockfd);
149                         return -1;
150                 }
151                 
152                 flags |= O_NONBLOCK;
153                 if( fcntl(sockfd, F_SETFL, flags) < 0) {
154                         fprintf(stderr, "%s: Failure setting socket flags: %s",
155                                 progname, strerror(errno));
156                         close(sockfd);
157                         return -1;
158                 }
159         }
160 #endif
161 #endif
162         return sockfd;
163 }
164
165 static int usage(void)
166 {
167         printf("Usage: %s [ args ]\n", progname);
168         printf("  -d raddb_dir    Configuration files are in \"raddbdir/*\".\n");
169         printf("  -e command      Execute 'command' and then exit.\n");
170         printf("  -E              Echo commands as they are being executed.\n");
171         printf("  -f socket_file  Open socket_file directly, without reading radius.conf\n");
172         printf("  -i input_file   Read commands from 'input_file'.\n");
173         printf("  -n name         Read raddb/name.conf instead of raddb/radiusd.conf\n");
174         printf("  -o output_file  Write commands to 'output_file'.\n");
175         printf("  -q              Quiet mode.\n");
176
177         exit(1);
178 }
179
180 static ssize_t run_command(int sockfd, const char *command,
181                            char *buffer, size_t bufsize)
182 {
183         char *p;
184         ssize_t size, len;
185
186         if (echo) {
187                 fprintf(outputfp, "%s\n", command);
188         }
189
190         /*
191          *      Write the text to the socket.
192          */
193         if (write(sockfd, command, strlen(command)) < 0) return -1;
194         if (write(sockfd, "\r\n", 2) < 0) return -1;
195
196         /*
197          *      Read the response
198          */
199         size = 0;
200         buffer[0] = '\0';
201
202         memset(buffer, 0, bufsize);
203
204         while (1) {
205                 int rcode;
206                 fd_set readfds;
207
208                 FD_ZERO(&readfds);
209                 FD_SET(sockfd, &readfds);
210
211                 rcode = select(sockfd + 1, &readfds, NULL, NULL, NULL);
212                 if (rcode < 0) {
213                         if (errno == EINTR) continue;
214
215                         fprintf(stderr, "%s: Failed selecting: %s\n",
216                                 progname, strerror(errno));
217                         exit(1);
218                 }
219
220                 if (rcode == 0) {
221                         fprintf(stderr, "%s: Server closed the connection.\n",
222                                 progname);
223                         exit(1);
224                 }
225
226 #ifdef MSG_DONTWAIT
227                 len = recv(sockfd, buffer + size,
228                            bufsize - size - 1, MSG_DONTWAIT);
229 #else
230                 /*
231                  *      Read one byte at a time (ugh)
232                  */
233                 len = recv(sockfd, buffer + size, 1, 0);
234 #endif
235                 if (len < 0) {
236                         /*
237                          *      No data: keep looping
238                          */
239                         if ((errno == EAGAIN) || (errno == EINTR)) {
240                                 continue;
241                         }
242
243                         fprintf(stderr, "%s: Error reading socket: %s\n",
244                                 progname, strerror(errno));
245                         exit(1);
246                 }
247                 if (len == 0) return 0; /* clean exit */
248
249                 size += len;
250                 buffer[size] = '\0';
251
252                 /*
253                  *      There really is a better way of doing this.
254                  */
255                 p = strstr(buffer, "radmin> ");
256                 if (p &&
257                     ((p == buffer) || 
258                      (p[-1] == '\n') ||
259                      (p[-1] == '\r'))) {
260                         *p = '\0';
261
262                         if (p[-1] == '\n') p[-1] = '\0';
263                         break;
264                 }
265         }
266
267         /*
268          *      Blank prompt.  Go get another command.
269          */
270         if (!buffer[0]) return 1;
271
272         buffer[size] = '\0'; /* this is at least right */
273
274         return 2;
275 }
276
277 #define MAX_COMMANDS (4)
278
279 int main(int argc, char **argv)
280 {
281         int argval, quiet = 0;
282         int done_license = 0;
283         int sockfd;
284         uint32_t magic;
285         char *line = NULL;
286         ssize_t len, size;
287         const char *file = NULL;
288         const char *name = "radiusd";
289         char *p, buffer[65536];
290         const char *input_file = NULL;
291         FILE *inputfp = stdin;
292         const char *output_file = NULL;
293         
294         char *commands[MAX_COMMANDS];
295         int num_commands = -1;
296
297         outputfp = stdout;      /* stdout is not a constant value... */
298
299         if ((progname = strrchr(argv[0], FR_DIR_SEP)) == NULL)
300                 progname = argv[0];
301         else
302                 progname++;
303
304         while ((argval = getopt(argc, argv, "d:hi:e:Ef:n:o:q")) != EOF) {
305                 switch(argval) {
306                 case 'd':
307                         if (file) {
308                                 fprintf(stderr, "%s: -d and -f cannot be used together.\n", progname);
309                                 exit(1);
310                         }
311                         radius_dir = optarg;
312                         break;
313
314                 case 'e':
315                         num_commands++; /* starts at -1 */
316                         if (num_commands >= MAX_COMMANDS) {
317                                 fprintf(stderr, "%s: Too many '-e'\n",
318                                         progname);
319                                 exit(1);
320                         }
321                         commands[num_commands] = optarg;
322                         break;
323
324                 case 'E':
325                         echo = TRUE;
326                         break;
327
328                 case 'f':
329                         radius_dir = NULL;
330                         file = optarg;
331                         break;
332
333                 default:
334                 case 'h':
335                         usage();
336                         break;
337
338                 case 'i':
339                         if (strcmp(optarg, "-") != 0) {
340                                 input_file = optarg;
341                         }
342                         quiet = 1;
343                         break;
344
345                 case 'n':
346                         name = optarg;
347                         break;
348
349                 case 'o':
350                         if (strcmp(optarg, "-") != 0) {
351                                 output_file = optarg;
352                         }
353                         quiet = 1;
354                         break;
355
356                 case 'q':
357                         quiet = 1;
358                         break;
359                 }
360         }
361
362         if (radius_dir) {
363                 int rcode;
364                 CONF_SECTION *cs, *subcs;
365
366                 file = NULL;    /* MUST read it from the conffile now */
367
368                 snprintf(buffer, sizeof(buffer), "%s/%s.conf",
369                          radius_dir, name);
370
371                 cs = cf_file_read(buffer);
372                 if (!cs) {
373                         fprintf(stderr, "%s: Errors reading %s\n",
374                                 progname, buffer);
375                         exit(1);
376                 }
377
378                 subcs = NULL;
379                 while ((subcs = cf_subsection_find_next(cs, subcs, "listen")) != NULL) {
380                         const char *value;
381                         CONF_PAIR *cp = cf_pair_find(subcs, "type");
382                         
383                         if (!cp) continue;
384
385                         value = cf_pair_value(cp);
386                         if (!value) continue;
387
388                         if (strcmp(value, "control") != 0) continue;
389
390                         /*
391                          *      Now find the socket name (sigh)
392                          */
393                         rcode = cf_item_parse(subcs, "socket",
394                                               PW_TYPE_STRING_PTR,
395                                               &file, NULL);
396                         if (rcode < 0) {
397                                 fprintf(stderr, "%s: Failed parsing listen section\n", progname);
398                                 exit(1);
399                         }
400
401                         if (!file) {
402                                 fprintf(stderr, "%s: No path given for socket\n",
403                                         progname);
404                                 exit(1);
405                         }
406                         break;
407                 }
408
409                 if (!file) {
410                         fprintf(stderr, "%s: Could not find control socket in %s\n",
411                                 progname, buffer);
412                         exit(1);
413                 }
414         }
415
416         if (input_file) {
417                 inputfp = fopen(input_file, "r");
418                 if (!inputfp) {
419                         fprintf(stderr, "%s: Failed opening %s: %s\n",
420                                 progname, input_file, strerror(errno));
421                         exit(1);
422                 }
423         }
424
425         if (output_file) {
426                 outputfp = fopen(output_file, "w");
427                 if (!outputfp) {
428                         fprintf(stderr, "%s: Failed creating %s: %s\n",
429                                 progname, output_file, strerror(errno));
430                         exit(1);
431                 }
432         }
433
434         /*
435          *      Check if stdin is a TTY only if input is from stdin
436          */
437         if (input_file && !quiet && !isatty(STDIN_FILENO)) quiet = 1;
438
439 #ifdef USE_READLINE
440         if (!quiet) {
441 #ifdef USE_READLINE_HISTORY
442                 using_history();
443 #endif
444                 rl_bind_key('\t', rl_insert);
445         }
446 #endif
447
448  reconnect:
449         /*
450          *      FIXME: Get destination from command line, if possible?
451          */
452         sockfd = fr_domain_socket(file);
453         if (sockfd < 0) {
454                 exit(1);
455         }
456
457         /*
458          *      Read initial magic && version information.
459          */
460         for (size = 0; size < 8; size += len) {
461                 len = read(sockfd, buffer + size, 8 - size);
462                 if (len < 0) {
463                         fprintf(stderr, "%s: Error reading initial data from socket: %s\n",
464                                 progname, strerror(errno));
465                         exit(1);
466                 }
467         }
468
469         memcpy(&magic, buffer, 4);
470         magic = ntohl(magic);
471         if (magic != 0xf7eead15) {
472                 fprintf(stderr, "%s: Socket %s is not FreeRADIUS administration socket\n", progname, file);
473                 exit(1);
474         }
475         
476         memcpy(&magic, buffer + 4, 4);
477         magic = ntohl(magic);
478         if (magic != 1) {
479                 fprintf(stderr, "%s: Socket version mismatch: Need 1, got %d\n",
480                         progname, magic);
481                 exit(1);
482         }       
483
484         /*
485          *      Run one command.
486          */
487         if (num_commands >= 0) {
488                 int i;
489
490                 for (i = 0; i <= num_commands; i++) {
491                         size = run_command(sockfd, commands[i],
492                                            buffer, sizeof(buffer));
493                         if (size < 0) exit(1);
494                         
495                         if (buffer[0]) {
496                                 fputs(buffer, outputfp);
497                                 fprintf(outputfp, "\n");
498                                 fflush(outputfp);
499                         }
500                 }
501                 exit(0);
502         }
503
504         if (!done_license && !quiet) {
505                 printf("radmin " RADIUSD_VERSION " - FreeRADIUS Server administration tool.\n");
506                 printf("Copyright (C) 2008-2011 The FreeRADIUS server project and contributors.\n");
507                 printf("There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A\n");
508                 printf("PARTICULAR PURPOSE.\n");
509                 printf("You may redistribute copies of FreeRADIUS under the terms of the\n");
510                 printf("GNU General Public License v2.\n");
511
512                 done_license = 1;
513         }
514
515         /*
516          *      FIXME: Do login?
517          */
518
519         while (1) {
520 #ifndef USE_READLINE
521                 if (!quiet) {
522                         printf("radmin> ");
523                         fflush(stdout);
524                 }
525 #else
526                 if (!quiet) {
527                         line = readline("radmin> ");
528                         
529                         if (!line) break;
530                         
531                         if (!*line) {
532                                 free(line);
533                                 continue;
534                         }
535                         
536 #ifdef USE_READLINE_HISTORY
537                         add_history(line);
538 #endif
539                 } else          /* quiet, or no readline */
540 #endif
541                 {
542                         line = fgets(buffer, sizeof(buffer), inputfp);
543                         if (!line) break;
544
545                         p = strchr(buffer, '\n');
546                         if (!p) {
547                                 fprintf(stderr, "%s: Input line too long\n",
548                                         progname);
549                                 exit(1);
550                         }
551                         
552                         *p = '\0';
553
554                         /*
555                          *      Strip off leading spaces.
556                          */
557                         for (p = line; *p != '\0'; p++) {
558                                 if ((p[0] == ' ') ||
559                                     (p[0] == '\t')) {
560                                         line = p + 1;
561                                         continue;
562                                 }
563
564                                 if (p[0] == '#') {
565                                         line = NULL;
566                                         break;
567                                 }
568
569                                 break;
570                         }
571
572                         /*
573                          *      Comments: keep going.
574                          */
575                         if (!line) continue;
576
577                         /*
578                          *      Strip off CR / LF
579                          */
580                         for (p = line; *p != '\0'; p++) {
581                                 if ((p[0] == '\r') ||
582                                     (p[0] == '\n')) {
583                                         p[0] = '\0';
584                                         break;
585                                 }
586                         }
587                 }
588
589                 if (strcmp(line, "reconnect") == 0) {
590                         close(sockfd);
591                         line = NULL;
592                         goto reconnect;
593                 }
594
595                 /*
596                  *      Exit, done, etc.
597                  */
598                 if ((strcmp(line, "exit") == 0) ||
599                     (strcmp(line, "quit") == 0)) {
600                         break;
601                 }
602
603                 size = run_command(sockfd, line, buffer, sizeof(buffer));
604                 if (size <= 0) break; /* error, or clean exit */
605
606                 if (size == 1) continue; /* no output. */
607
608                 fputs(buffer, outputfp);
609                 fflush(outputfp);
610                 fprintf(outputfp, "\n");
611         }
612
613         fprintf(outputfp, "\n");
614
615         return 0;
616 }
617