Added deprecated flag to variables
[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 #ifdef MSG_DONTWAIT
221                 len = recv(sockfd, buffer + size,
222                            bufsize - size - 1, MSG_DONTWAIT);
223 #else
224                 /*
225                  *      Read one byte at a time (ugh)
226                  */
227                 len = recv(sockfd, buffer + size, 1, 0);
228 #endif
229                 if (len < 0) {
230                         /*
231                          *      No data: keep looping
232                          */
233                         if ((errno == EAGAIN) || (errno == EINTR)) {
234                                 continue;
235                         }
236
237                         fprintf(stderr, "%s: Error reading socket: %s\n",
238                                 progname, strerror(errno));
239                         exit(1);
240                 }
241                 if (len == 0) return 0; /* clean exit */
242
243                 size += len;
244                 buffer[size] = '\0';
245
246                 /*
247                  *      There really is a better way of doing this.
248                  */
249                 p = strstr(buffer, "radmin> ");
250                 if (p &&
251                     ((p == buffer) || 
252                      (p[-1] == '\n') ||
253                      (p[-1] == '\r'))) {
254                         *p = '\0';
255
256                         if (p[-1] == '\n') p[-1] = '\0';
257                         break;
258                 }
259         }
260
261         /*
262          *      Blank prompt.  Go get another command.
263          */
264         if (!buffer[0]) return 1;
265
266         buffer[size] = '\0'; /* this is at least right */
267
268         return 2;
269 }
270
271 #define MAX_COMMANDS (4)
272
273 int main(int argc, char **argv)
274 {
275         int argval, quiet = 0;
276         int done_license = 0;
277         int sockfd;
278         uint32_t magic;
279         char *line = NULL;
280         ssize_t len, size;
281         const char *file = NULL;
282         const char *name = "radiusd";
283         char *p, buffer[65536];
284         const char *input_file = NULL;
285         FILE *inputfp = stdin;
286         const char *output_file = NULL;
287         
288         char *commands[MAX_COMMANDS];
289         int num_commands = -1;
290
291         outputfp = stdout;      /* stdout is not a constant value... */
292
293         if ((progname = strrchr(argv[0], FR_DIR_SEP)) == NULL)
294                 progname = argv[0];
295         else
296                 progname++;
297
298         while ((argval = getopt(argc, argv, "d:hi:e:Ef:n:o:q")) != EOF) {
299                 switch(argval) {
300                 case 'd':
301                         if (file) {
302                                 fprintf(stderr, "%s: -d and -f cannot be used together.\n", progname);
303                                 exit(1);
304                         }
305                         radius_dir = optarg;
306                         break;
307
308                 case 'e':
309                         num_commands++; /* starts at -1 */
310                         if (num_commands >= MAX_COMMANDS) {
311                                 fprintf(stderr, "%s: Too many '-e'\n",
312                                         progname);
313                                 exit(1);
314                         }
315                         commands[num_commands] = optarg;
316                         break;
317
318                 case 'E':
319                         echo = TRUE;
320                         break;
321
322                 case 'f':
323                         radius_dir = NULL;
324                         file = optarg;
325                         break;
326
327                 default:
328                 case 'h':
329                         usage();
330                         break;
331
332                 case 'i':
333                         if (strcmp(optarg, "-") != 0) {
334                                 input_file = optarg;
335                         }
336                         quiet = 1;
337                         break;
338
339                 case 'n':
340                         name = optarg;
341                         break;
342
343                 case 'o':
344                         if (strcmp(optarg, "-") != 0) {
345                                 output_file = optarg;
346                         }
347                         quiet = 1;
348                         break;
349
350                 case 'q':
351                         quiet = 1;
352                         break;
353                 }
354         }
355
356         if (radius_dir) {
357                 int rcode;
358                 CONF_SECTION *cs, *subcs;
359
360                 file = NULL;    /* MUST read it from the conffile now */
361
362                 snprintf(buffer, sizeof(buffer), "%s/%s.conf",
363                          radius_dir, name);
364
365                 cs = cf_file_read(buffer);
366                 if (!cs) {
367                         fprintf(stderr, "%s: Errors reading %s\n",
368                                 progname, buffer);
369                         exit(1);
370                 }
371
372                 subcs = NULL;
373                 while ((subcs = cf_subsection_find_next(cs, subcs, "listen")) != NULL) {
374                         const char *value;
375                         CONF_PAIR *cp = cf_pair_find(subcs, "type");
376                         
377                         if (!cp) continue;
378
379                         value = cf_pair_value(cp);
380                         if (!value) continue;
381
382                         if (strcmp(value, "control") != 0) continue;
383
384                         /*
385                          *      Now find the socket name (sigh)
386                          */
387                         rcode = cf_item_parse(subcs, "socket",
388                                               PW_TYPE_STRING_PTR,
389                                               &file, NULL);
390                         if (rcode < 0) {
391                                 fprintf(stderr, "%s: Failed parsing listen section\n", progname);
392                                 exit(1);
393                         }
394
395                         if (!file) {
396                                 fprintf(stderr, "%s: No path given for socket\n",
397                                         progname);
398                                 exit(1);
399                         }
400                         break;
401                 }
402
403                 if (!file) {
404                         fprintf(stderr, "%s: Could not find control socket in %s\n",
405                                 progname, buffer);
406                         exit(1);
407                 }
408         }
409
410         if (input_file) {
411                 inputfp = fopen(input_file, "r");
412                 if (!inputfp) {
413                         fprintf(stderr, "%s: Failed opening %s: %s\n",
414                                 progname, input_file, strerror(errno));
415                         exit(1);
416                 }
417         }
418
419         if (output_file) {
420                 outputfp = fopen(output_file, "w");
421                 if (!outputfp) {
422                         fprintf(stderr, "%s: Failed creating %s: %s\n",
423                                 progname, output_file, strerror(errno));
424                         exit(1);
425                 }
426         }
427
428         /*
429          *      Check if stdin is a TTY only if input is from stdin
430          */
431         if (input_file && !quiet && !isatty(STDIN_FILENO)) quiet = 1;
432
433 #ifdef USE_READLINE
434         if (!quiet) {
435 #ifdef USE_READLINE_HISTORY
436                 using_history();
437 #endif
438                 rl_bind_key('\t', rl_insert);
439         }
440 #endif
441
442  reconnect:
443         /*
444          *      FIXME: Get destination from command line, if possible?
445          */
446         sockfd = fr_domain_socket(file);
447         if (sockfd < 0) {
448                 exit(1);
449         }
450
451         /*
452          *      Read initial magic && version information.
453          */
454         for (size = 0; size < 8; size += len) {
455                 len = read(sockfd, buffer + size, 8 - size);
456                 if (len < 0) {
457                         fprintf(stderr, "%s: Error reading initial data from socket: %s\n",
458                                 progname, strerror(errno));
459                         exit(1);
460                 }
461         }
462
463         memcpy(&magic, buffer, 4);
464         magic = ntohl(magic);
465         if (magic != 0xf7eead15) {
466                 fprintf(stderr, "%s: Socket %s is not FreeRADIUS administration socket\n", progname, file);
467                 exit(1);
468         }
469         
470         memcpy(&magic, buffer + 4, 4);
471         magic = ntohl(magic);
472         if (magic != 1) {
473                 fprintf(stderr, "%s: Socket version mismatch: Need 1, got %d\n",
474                         progname, magic);
475                 exit(1);
476         }       
477
478         /*
479          *      Run one command.
480          */
481         if (num_commands >= 0) {
482                 int i;
483
484                 for (i = 0; i <= num_commands; i++) {
485                         size = run_command(sockfd, commands[i],
486                                            buffer, sizeof(buffer));
487                         if (size < 0) exit(1);
488                         
489                         if (buffer[0]) {
490                                 fputs(buffer, outputfp);
491                                 fprintf(outputfp, "\n");
492                                 fflush(outputfp);
493                         }
494                 }
495                 exit(0);
496         }
497
498         if (!done_license && !quiet) {
499                 printf("radmin " RADIUSD_VERSION " - FreeRADIUS Server administration tool.\n");
500                 printf("Copyright (C) 2008 The FreeRADIUS server project and contributors.\n");
501                 printf("There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A\n");
502                 printf("PARTICULAR PURPOSE.\n");
503                 printf("You may redistribute copies of FreeRADIUS under the terms of the\n");
504                 printf("GNU General Public License v2.\n");
505
506                 done_license = 1;
507         }
508
509         /*
510          *      FIXME: Do login?
511          */
512
513         while (1) {
514 #ifndef USE_READLINE
515                 if (!quiet) {
516                         printf("radmin> ");
517                         fflush(stdout);
518                 }
519 #else
520                 if (!quiet) {
521                         line = readline("radmin> ");
522                         
523                         if (!line) break;
524                         
525                         if (!*line) {
526                                 free(line);
527                                 continue;
528                         }
529                         
530 #ifdef USE_READLINE_HISTORY
531                         add_history(line);
532 #endif
533                 } else          /* quiet, or no readline */
534 #endif
535                 {
536                         line = fgets(buffer, sizeof(buffer), inputfp);
537                         if (!line) break;
538
539                         p = strchr(buffer, '\n');
540                         if (!p) {
541                                 fprintf(stderr, "%s: Input line too long\n",
542                                         progname);
543                                 exit(1);
544                         }
545                         
546                         *p = '\0';
547
548                         /*
549                          *      Strip off leading spaces.
550                          */
551                         for (p = line; *p != '\0'; p++) {
552                                 if ((p[0] == ' ') ||
553                                     (p[0] == '\t')) {
554                                         line = p + 1;
555                                         continue;
556                                 }
557
558                                 if (p[0] == '#') {
559                                         line = NULL;
560                                         break;
561                                 }
562
563                                 break;
564                         }
565
566                         /*
567                          *      Comments: keep going.
568                          */
569                         if (!line) continue;
570
571                         /*
572                          *      Strip off CR / LF
573                          */
574                         for (p = line; *p != '\0'; p++) {
575                                 if ((p[0] == '\r') ||
576                                     (p[0] == '\n')) {
577                                         p[0] = '\0';
578                                         break;
579                                 }
580                         }
581                 }
582
583                 if (strcmp(line, "reconnect") == 0) {
584                         close(sockfd);
585                         line = NULL;
586                         goto reconnect;
587                 }
588
589                 /*
590                  *      Exit, done, etc.
591                  */
592                 if ((strcmp(line, "exit") == 0) ||
593                     (strcmp(line, "quit") == 0)) {
594                         break;
595                 }
596
597                 size = run_command(sockfd, line, buffer, sizeof(buffer));
598                 if (size <= 0) break; /* error, or clean exit */
599
600                 if (size == 1) continue; /* no output. */
601
602                 fputs(buffer, outputfp);
603                 fflush(outputfp);
604                 fprintf(outputfp, "\n");
605         }
606
607         fprintf(outputfp, "\n");
608
609         return 0;
610 }
611