e8bcf8f87b98435e35ec6a59ebffcc2b4765b4cf
[freeradius.git] / src / main / radiusd.c
1 /*
2  * radiusd.c    Main loop of the radius server.
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 2000-2004,2006  The FreeRADIUS server project
21  * Copyright 1999,2000  Miquel van Smoorenburg <miquels@cistron.nl>
22  * Copyright 2000  Alan DeKok <aland@ox.org>
23  * Copyright 2000  Alan Curry <pacman-radius@cqc.com>
24  * Copyright 2000  Jeff Carneal <jeff@apex.net>
25  * Copyright 2000  Chad Miller <cmiller@surfsouth.com>
26  */
27
28 #include <freeradius-devel/ident.h>
29 RCSID("$Id$")
30
31 #include <freeradius-devel/radiusd.h>
32 #include <freeradius-devel/radius_snmp.h>
33 #include <freeradius-devel/rad_assert.h>
34
35 #include <sys/file.h>
36
37 #include <fcntl.h>
38 #include <ctype.h>
39
40 #include <signal.h>
41
42 #ifdef HAVE_GETOPT_H
43 #       include <getopt.h>
44 #endif
45
46 #ifdef HAVE_SYS_WAIT_H
47 #       include <sys/wait.h>
48 #endif
49 #ifndef WEXITSTATUS
50 #       define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
51 #endif
52 #ifndef WIFEXITED
53 #       define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
54 #endif
55
56 #ifndef HAVE_PTHREAD_H
57 #define thread_pool_lock(_x)
58 #define thread_pool_unlock(_x)
59 #endif
60
61 /*
62  *  Global variables.
63  */
64 const char *progname = NULL;
65 const char *radius_dir = NULL;
66 const char *radacct_dir = NULL;
67 const char *radlog_dir = NULL;
68 const char *radlib_dir = NULL;
69 int log_stripped_names;
70 int debug_flag = 0;
71 int log_auth_detail = FALSE;
72
73 const char *radiusd_version = "FreeRADIUS Version " RADIUSD_VERSION ", for host " HOSTINFO ", built on " __DATE__ " at " __TIME__;
74
75 time_t time_now;
76 pid_t radius_pid;
77
78 static int debug_memory = 0;
79
80 /*
81  *  Configuration items.
82  */
83
84 /*
85  *      Static functions.
86  */
87 static void usage(int);
88
89 static void sig_fatal (int);
90 static void sig_hup (int);
91
92 /*
93  *      The main guy.
94  */
95 int main(int argc, char *argv[])
96 {
97         int rcode;
98         unsigned char buffer[4096];
99         int argval;
100         int spawn_flag = TRUE;
101         int dont_fork = FALSE;
102         int flag = 0;
103
104 #ifdef HAVE_SIGACTION
105         struct sigaction act;
106 #endif
107
108 #ifdef OSFC2
109         set_auth_parameters(argc,argv);
110 #endif
111
112         if ((progname = strrchr(argv[0], '/')) == NULL)
113                 progname = argv[0];
114         else
115                 progname++;
116
117         debug_flag = 0;
118         spawn_flag = TRUE;
119         radius_dir = strdup(RADIUS_DIR);
120
121         /*
122          *      Ensure that the configuration is initialized.
123          */
124         memset(&mainconfig, 0, sizeof(mainconfig));
125         mainconfig.myip.af = AF_UNSPEC;
126         mainconfig.port = -1;
127         mainconfig.radiusd_conf = strdup("radiusd.conf");
128
129 #ifdef HAVE_SIGACTION
130         memset(&act, 0, sizeof(act));
131         act.sa_flags = 0 ;
132         sigemptyset( &act.sa_mask ) ;
133 #endif
134
135         /*
136          *      Don't put output anywhere until we get told a little
137          *      more.
138          */
139         mainconfig.radlog_fd = -1;
140         mainconfig.log_file = NULL;
141
142         /*  Process the options.  */
143         while ((argval = getopt(argc, argv, "Aa:bcd:fg:hi:l:mn:p:sSvxXyz")) != EOF) {
144
145                 switch(argval) {
146
147                         case 'A':
148                                 log_auth_detail = TRUE;
149                                 break;
150
151                         case 'a':
152                                 if (radacct_dir) free(radacct_dir);
153                                 radacct_dir = strdup(optarg);
154                                 break;
155
156                         case 'c':
157                                 /* ignore for backwards compatibility with Cistron */
158                                 break;
159
160                         case 'd':
161                                 if (radius_dir) free(radius_dir);
162                                 radius_dir = strdup(optarg);
163                                 break;
164
165                         case 'f':
166                                 dont_fork = TRUE;
167                                 break;
168
169                         case 'h':
170                                 usage(0);
171                                 break;
172
173                         case 'i':
174                                 if (ip_hton(optarg, AF_UNSPEC, &mainconfig.myip) < 0) {
175                                         fprintf(stderr, "radiusd: Invalid IP Address or hostname \"%s\"\n", optarg);
176                                         exit(1);
177                                 }
178                                 flag |= 1;
179                                 break;
180
181                         case 'l':
182                                 if ((strcmp(optarg, "stdout") == 0) ||
183                                     (strcmp(optarg, "stderr") == 0) ||
184                                     (strcmp(optarg, "syslog") == 0)) {
185                                         fprintf(stderr, "radiusd: -l %s is unsupported.  Use log_destination in radiusd.conf\n", optarg);
186                                         exit(1);
187                                 }
188                                 if (radlog_dir) free(radlog_dir);
189                                 radlog_dir = strdup(optarg);
190                                 break;
191
192                         case 'g':
193                                 fprintf(stderr, "radiusd: -g is unsupported.  Use log_destination in radiusd.conf.\n");
194                                 exit(1);
195                                 break;
196
197                         case 'm':
198                                 debug_memory = 1;
199                                 break;
200
201                         case 'n':
202                                 if ((strchr(optarg, '/') != NULL) ||
203                                     (strchr(optarg, '.') != NULL) ||
204                                     (strlen(optarg) > 45)) usage(1);
205
206                                 snprintf(buffer, sizeof(buffer), "%s.conf",
207                                          optarg);
208                                 if (mainconfig.radiusd_conf)
209                                         free(mainconfig.radiusd_conf);
210                                 mainconfig.radiusd_conf = strdup(buffer);
211                                 break;
212
213                         case 'S':
214                                 log_stripped_names++;
215                                 break;
216
217                         case 'p':
218                                 mainconfig.port = atoi(optarg);
219                                 if ((mainconfig.port <= 0) ||
220                                     (mainconfig.port >= 65536)) {
221                                         fprintf(stderr, "radiusd: Invalid port number %s\n", optarg);
222                                         exit(1);
223                                 }
224                                 flag |= 2;
225                                 break;
226
227                         case 's':       /* Single process mode */
228                                 spawn_flag = FALSE;
229                                 dont_fork = TRUE;
230                                 break;
231
232                         case 'v':
233                                 version();
234                                 break;
235
236                                 /*
237                                  *  BIG debugging mode for users who are
238                                  *  TOO LAZY to type '-sfxxyz -l stdout' themselves.
239                                  */
240                         case 'X':
241                                 spawn_flag = FALSE;
242                                 dont_fork = TRUE;
243                                 debug_flag += 2;
244                                 mainconfig.log_auth = TRUE;
245                                 mainconfig.log_auth_badpass = TRUE;
246                                 mainconfig.log_auth_goodpass = TRUE;
247                                 mainconfig.radlog_dest = RADLOG_STDOUT;
248                                 mainconfig.radlog_fd = STDOUT_FILENO;
249                                 break;
250
251                         case 'x':
252                                 debug_flag++;
253                                 break;
254
255                         case 'y':
256                                 mainconfig.log_auth = TRUE;
257                                 mainconfig.log_auth_badpass = TRUE;
258                                 break;
259
260                         case 'z':
261                                 mainconfig.log_auth_badpass = TRUE;
262                                 mainconfig.log_auth_goodpass = TRUE;
263                                 break;
264
265                         default:
266                                 usage(1);
267                                 break;
268                 }
269         }
270
271         if (flag && (flag != 0x03)) {
272                 fprintf(stderr, "radiusd: The options -i and -p cannot be used individually.\n");
273                 exit(1);
274         }
275
276         if (debug_flag) {
277                 radlog(L_INFO, "%s", radiusd_version);
278                 radlog(L_INFO, "Copyright (C) 2000-2007 The FreeRADIUS server project.\n");
279                 radlog(L_INFO, "There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A\n");
280                 radlog(L_INFO, "PARTICULAR PURPOSE.\n");
281                 radlog(L_INFO, "You may redistribute copies of FreeRADIUS under the terms of the\n");
282                 radlog(L_INFO, "GNU General Public License.\n");
283                 fflush(NULL);
284         }
285
286         /*  Read the configuration files, BEFORE doing anything else.  */
287         if (read_mainconfig(0) < 0) {
288                 exit(1);
289         }
290
291         /*
292          *  Disconnect from session
293          */
294         if (debug_flag == 0 && dont_fork == FALSE) {
295                 pid_t pid = fork();
296
297                 if (pid < 0) {
298                         radlog(L_ERR, "Couldn't fork: %s", strerror(errno));
299                         exit(1);
300                 }
301
302                 /*
303                  *  The parent exits, so the child can run in the background.
304                  */
305                 if (pid > 0) {
306                         exit(0);
307                 }
308 #ifdef HAVE_SETSID
309                 setsid();
310 #endif
311         }
312
313         /*
314          *      If we're NOT debugging, trap fatal signals, so we can
315          *      easily clean up after ourselves.
316          *
317          *      If we ARE debugging, don't trap them, so we can
318          *      dump core.
319          */
320         if ((mainconfig.allow_core_dumps == FALSE) && (debug_flag == 0)) {
321 #ifdef SIGSEGV
322 #ifdef HAVE_SIGACTION
323                 act.sa_handler = sig_fatal;
324                 sigaction(SIGSEGV, &act, NULL);
325 #else
326                 signal(SIGSEGV, sig_fatal);
327 #endif
328 #endif
329         }
330
331         /*
332          *  Ensure that we're using the CORRECT pid after forking,
333          *  NOT the one we started with.
334          */
335         radius_pid = getpid();
336
337         /*
338          *  Only write the PID file if we're running as a daemon.
339          *
340          *  And write it AFTER we've forked, so that we write the
341          *  correct PID.
342          */
343         if (dont_fork == FALSE) {
344                 FILE *fp;
345
346                 fp = fopen(mainconfig.pid_file, "w");
347                 if (fp != NULL) {
348                         /*
349                          *      FIXME: What about following symlinks,
350                          *      and having it over-write a normal file?
351                          */
352                         fprintf(fp, "%d\n", (int) radius_pid);
353                         fclose(fp);
354                 } else {
355                         radlog(L_ERR|L_CONS, "Failed creating PID file %s: %s\n",
356                                mainconfig.pid_file, strerror(errno));
357                         exit(1);
358                 }
359         }
360
361         /*
362          *      If we're running as a daemon, close the default file
363          *      descriptors, AFTER forking.
364          */
365         if (!debug_flag) {
366                 int devnull;
367
368                 devnull = open("/dev/null", O_RDWR);
369                 if (devnull < 0) {
370                         radlog(L_ERR|L_CONS, "Failed opening /dev/null: %s\n",
371                                strerror(errno));
372                         exit(1);
373                 }
374                 dup2(devnull, STDIN_FILENO);
375                 if (mainconfig.radlog_dest == RADLOG_STDOUT) {
376                         mainconfig.radlog_fd = dup(STDOUT_FILENO);
377                 }
378                 dup2(devnull, STDOUT_FILENO);
379                 if (mainconfig.radlog_dest == RADLOG_STDERR) {
380                         mainconfig.radlog_fd = dup(STDERR_FILENO);
381                 }
382                 dup2(devnull, STDERR_FILENO);
383                 close(devnull);
384         }
385
386         /*
387          *      It's called the thread pool, but it does a little
388          *      more than that.
389          */
390         radius_event_init(mainconfig.config, spawn_flag);
391
392         /*
393          *  Use linebuffered or unbuffered stdout if
394          *  the debug flag is on.
395          */
396         if (debug_flag == TRUE)
397                 setlinebuf(stdout);
398
399         /*
400          *      Now that we've set everything up, we can install the signal
401          *      handlers.  Before this, if we get any signal, we don't know
402          *      what to do, so we might as well do the default, and die.
403          */
404         signal(SIGPIPE, SIG_IGN);
405 #ifdef HAVE_SIGACTION
406         act.sa_handler = sig_hup;
407         sigaction(SIGHUP, &act, NULL);
408         act.sa_handler = sig_fatal;
409         sigaction(SIGTERM, &act, NULL);
410 #else
411         signal(SIGHUP, sig_hup);
412         signal(SIGTERM, sig_fatal);
413 #endif
414         /*
415          *      If we're debugging, then a CTRL-C will cause the
416          *      server to die immediately.  Use SIGTERM to shut down
417          *      the server cleanly in that case.
418          */
419         if ((debug_memory == 1) || (debug_flag == 0)) {
420 #ifdef HAVE_SIGACTION
421                 act.sa_handler = sig_fatal;
422                 sigaction(SIGINT, &act, NULL);
423                 sigaction(SIGQUIT, &act, NULL);
424 #else
425                 signal(SIGINT, sig_fatal);
426                 signal(SIGQUIT, sig_fatal);
427 #endif
428         }
429
430         /*
431          *      Process requests until HUP or exit.
432          */
433         while ((rcode = radius_event_process()) == 0x80) {
434                 thread_pool_lock();
435                 /*
436                  *      Reload anything that can safely be reloaded.
437                  */
438                 DEBUG("HUP support not available.");
439
440                 thread_pool_unlock();
441         }
442         
443         DEBUG("Exiting...");
444         
445         /*
446          *      Ignore the TERM signal: we're
447          *      about to die.
448          */
449         signal(SIGTERM, SIG_IGN);
450         
451         /*
452          *      Send a TERM signal to all
453          *      associated processes
454          *      (including us, which gets
455          *      ignored.)
456          */
457         kill(-radius_pid, SIGTERM);
458         
459         /*
460          *      We're exiting, so we can delete the PID
461          *      file.  (If it doesn't exist, we can ignore
462          *      the error returned by unlink)
463          */
464         if (dont_fork == FALSE) {
465                 unlink(mainconfig.pid_file);
466         }
467                 
468         radius_event_free();
469         
470         /*
471          *      Free the configuration items.
472          */
473         free_mainconfig();
474         
475         /*
476          *      Detach any modules.
477          */
478         detach_modules();
479         
480         free(radius_dir);
481                 
482         return (rcode - 1);
483 }
484
485
486 /*
487  *  Display the syntax for starting this program.
488  */
489 static void NEVER_RETURNS usage(int status)
490 {
491         FILE *output = status?stderr:stdout;
492
493         fprintf(output,
494                         "Usage: %s [-a acct_dir] [-d db_dir] [-l log_dir] [-i address] [-AcfnsSvXxyz]\n", progname);
495         fprintf(output, "Options:\n\n");
496         fprintf(output, "  -a acct_dir     use accounting directory 'acct_dir'.\n");
497         fprintf(output, "  -A              Log auth detail.\n");
498         fprintf(output, "  -d raddb_dir    Configuration files are in \"raddbdir/*\".\n");
499         fprintf(output, "  -f              Run as a foreground process, not a daemon.\n");
500         fprintf(output, "  -h              Print this help message.\n");
501         fprintf(output, "  -i ipaddr       Listen on ipaddr ONLY\n");
502         fprintf(output, "  -l log_dir      Log file is \"log_dir/radius.log\" (not used in debug mode)\n");
503         fprintf(output, "  -p port         Listen on port ONLY\n");
504         fprintf(output, "  -s              Do not spawn child processes to handle requests.\n");
505         fprintf(output, "  -S              Log stripped names.\n");
506         fprintf(output, "  -v              Print server version information.\n");
507         fprintf(output, "  -X              Turn on full debugging.\n");
508         fprintf(output, "  -x              Turn on additional debugging. (-xx gives more debugging).\n");
509         fprintf(output, "  -y              Log authentication failures, with password.\n");
510         fprintf(output, "  -z              Log authentication successes, with password.\n");
511         exit(status);
512 }
513
514
515 /*
516  *      We got a fatal signal.
517  */
518 static void sig_fatal(int sig)
519 {
520         switch(sig) {
521                 case SIGSEGV:
522                         /*
523                          *      We can't really do anything
524                          *      intelligent here so just die
525                          */
526                         _exit(1);
527
528                 case SIGTERM:
529                         radius_signal_self(RADIUS_SIGNAL_SELF_TERM);
530                         break;
531
532                 case SIGINT:
533                 case SIGQUIT:
534                         if (debug_memory) {
535                                 radius_signal_self(RADIUS_SIGNAL_SELF_TERM);
536                                 break;
537                         }
538                         /* FALL-THROUGH */
539
540                 default:
541                         radius_signal_self(RADIUS_SIGNAL_SELF_EXIT);
542                         break;
543         }
544 }
545
546
547 /*
548  *  We got the hangup signal.
549  *  Re-read the configuration files.
550  */
551 static void sig_hup(int sig)
552 {
553         sig = sig; /* -Wunused */
554
555         reset_signal(SIGHUP, sig_hup);
556
557         radius_signal_self(RADIUS_SIGNAL_SELF_HUP);
558 }