2 * radiusd.c Main loop of the radius server.
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 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>
28 #include <freeradius-devel/ident.h>
31 #include <freeradius-devel/radiusd.h>
32 #include <freeradius-devel/modules.h>
33 #include <freeradius-devel/rad_assert.h>
46 #ifdef HAVE_SYS_WAIT_H
47 # include <sys/wait.h>
50 # define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
53 # define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
56 #ifdef HAVE_OPENSSL_CRYPTO_H
57 #include <openssl/ssl.h>
63 const char *progname = NULL;
64 const char *radius_dir = NULL;
65 const char *radacct_dir = NULL;
66 const char *radlog_dir = NULL;
67 const char *radlib_dir = NULL;
68 int log_stripped_names;
70 int check_config = FALSE;
72 const char *radiusd_version = "FreeRADIUS Version " RADIUSD_VERSION_STRING
73 #ifdef RADIUSD_VERSION_COMMIT
74 " (git #" RADIUSD_VERSION_COMMIT ")"
76 ", for host " HOSTINFO ", built on " __DATE__ " at " __TIME__;
81 * Configuration items.
87 static void usage(int);
89 static void sig_fatal (int);
91 static void sig_hup (int);
97 int main(int argc, char *argv[])
101 int spawn_flag = TRUE;
102 int dont_fork = FALSE;
104 int from_child[2] = {-1, -1};
109 * If the server was built with debugging enabled always install
110 * the basic fatal signal handlers.
113 fr_fault_setup(getenv("PANIC_ACTION"), argv[0]);
116 #ifdef HAVE_SIGACTION
117 struct sigaction act;
121 set_auth_parameters(argc,argv);
124 if ((progname = strrchr(argv[0], FR_DIR_SEP)) == NULL)
132 if (WSAStartup(MAKEWORD(2, 0), &wsaData)) {
133 fprintf(stderr, "%s: Unable to initialize socket library.\n");
141 radius_dir = strdup(RADIUS_DIR);
144 * Ensure that the configuration is initialized.
146 memset(&mainconfig, 0, sizeof(mainconfig));
147 mainconfig.myip.af = AF_UNSPEC;
148 mainconfig.port = -1;
149 mainconfig.name = "radiusd";
151 #ifdef HAVE_SIGACTION
152 memset(&act, 0, sizeof(act));
154 sigemptyset( &act.sa_mask ) ;
158 * Don't put output anywhere until we get told a little
161 mainconfig.radlog_dest = RADLOG_NULL;
162 mainconfig.radlog_fd = -1;
163 mainconfig.log_file = NULL;
165 /* Process the options. */
166 while ((argval = getopt(argc, argv, "Cd:fhi:l:mn:p:stvxX")) != EOF) {
176 if (radius_dir) free(radius_dir);
177 radius_dir = strdup(optarg);
189 if (strcmp(optarg, "stdout") == 0) {
192 mainconfig.log_file = strdup(optarg);
193 mainconfig.radlog_dest = RADLOG_FILES;
194 mainconfig.radlog_fd = open(mainconfig.log_file,
195 O_WRONLY | O_APPEND | O_CREAT, 0640);
196 if (mainconfig.radlog_fd < 0) {
197 fprintf(stderr, "radiusd: Failed to open log file %s: %s\n", mainconfig.log_file, strerror(errno));
200 fr_log_fp = fdopen(mainconfig.radlog_fd, "a");
204 if (ip_hton(optarg, AF_UNSPEC, &mainconfig.myip) < 0) {
205 fprintf(stderr, "radiusd: Invalid IP Address or hostname \"%s\"\n", optarg);
212 mainconfig.name = optarg;
216 mainconfig.debug_memory = 1;
220 mainconfig.port = atoi(optarg);
221 if ((mainconfig.port <= 0) ||
222 (mainconfig.port >= 65536)) {
223 fprintf(stderr, "radiusd: Invalid port number %s\n", optarg);
229 case 's': /* Single process mode */
234 case 't': /* no child threads */
239 /* Don't print timestamps */
242 mainconfig.radlog_dest = RADLOG_STDOUT;
243 mainconfig.radlog_fd = STDOUT_FILENO;
251 mainconfig.log_auth = TRUE;
252 mainconfig.log_auth_badpass = TRUE;
253 mainconfig.log_auth_goodpass = TRUE;
256 mainconfig.radlog_dest = RADLOG_STDOUT;
257 mainconfig.radlog_fd = STDOUT_FILENO;
270 if (flag && (flag != 0x03)) {
271 fprintf(stderr, "radiusd: The options -i and -p cannot be used individually.\n");
279 /* Read the configuration files, BEFORE doing anything else. */
280 if (read_mainconfig(0) < 0) {
284 #ifdef HAVE_OPENSSL_CRYPTO_H
286 * Initialize the OpenSSL library before calling any of its
290 SSL_load_error_strings();
293 * Mismatch between build time OpenSSL and linked SSL,
294 * better to die here than segfault later.
296 if (ssl_check_version(mainconfig.allow_vulnerable_openssl) < 0) {
301 /* Load the modules AFTER doing SSL checks */
302 if (setup_modules(FALSE, mainconfig.config) < 0) {
306 /* Set the panic action (if required) */
307 if (mainconfig.panic_action &&
309 !getenv("PANIC_ACTION") &&
311 (fr_fault_setup(mainconfig.panic_action, argv[0]) < 0)) {
317 devnull = open("/dev/null", O_RDWR);
319 radlog(L_ERR|L_CONS, "Failed opening /dev/null: %s\n", strerror(errno));
324 * Disconnect from session
326 if (dont_fork == FALSE) {
330 * Really weird things happen if we leave stdin open and call
331 * things like system() later.
333 if (dont_fork == 0) {
334 dup2(devnull, STDIN_FILENO);
337 if (pipe(from_child) != 0) {
338 radlog(L_ERR, "Couldn't open pipe for child status: %s", strerror(errno));
344 radlog(L_ERR, "Couldn't fork: %s", strerror(errno));
349 * The parent exits, so the child can run in the background.
351 * As the child can still encounter an error during initialisation
352 * we do a blocking read on a pipe between it and the parent.
354 * Just before entering the event loop the child will
360 /* So the pipe is correctly widowed if the child exits */
361 close(from_child[1]);
363 if ((read(from_child[0], &ret, 1) < 0)) {
367 /* For cleanliness... */
368 close(from_child[0]);
370 /* Don't turn children into zombies */
372 waitpid(pid, &stat_loc, WNOHANG);
379 /* so the pipe is correctly widowed if the parent exits?! */
380 close(from_child[0]);
388 * Ensure that we're using the CORRECT pid after forking,
389 * NOT the one we started with.
391 radius_pid = getpid();
394 * STDOUT & STDERR go to /dev/null, unless we have "-x",
395 * then STDOUT & STDERR go to the "-l log" destination.
397 * The complexity here is because "-l log" can go to
398 * STDOUT or STDERR, too.
400 if (mainconfig.radlog_dest == RADLOG_STDOUT) {
402 mainconfig.radlog_fd = STDOUT_FILENO;
405 * If we're debugging, allow STDERR to go to
406 * STDOUT too, for executed programs,
409 dup2(STDOUT_FILENO, STDERR_FILENO);
411 dup2(devnull, STDERR_FILENO);
414 } else if (mainconfig.radlog_dest == RADLOG_STDERR) {
416 mainconfig.radlog_fd = STDERR_FILENO;
419 * If we're debugging, allow STDOUT to go to
420 * STDERR too, for executed programs,
423 dup2(STDERR_FILENO, STDOUT_FILENO);
425 dup2(devnull, STDOUT_FILENO);
428 } else if (mainconfig.radlog_dest == RADLOG_SYSLOG) {
430 * Discard STDOUT and STDERR no matter what the
431 * status of debugging. Syslog isn't a file
432 * descriptor, so we can't use it.
434 dup2(devnull, STDOUT_FILENO);
435 dup2(devnull, STDERR_FILENO);
437 } else if (debug_flag) {
439 * If we're debugging, allow STDOUT and STDERR to
440 * go to the log file.
442 dup2(mainconfig.radlog_fd, STDOUT_FILENO);
443 dup2(mainconfig.radlog_fd, STDERR_FILENO);
447 * Not debugging, and the log isn't STDOUT or
448 * STDERR. Ensure that we move both of them to
449 * /dev/null, so that the calling terminal can
450 * exit, and the output from executed programs
451 * doesn't pollute STDOUT / STDERR.
453 dup2(devnull, STDOUT_FILENO);
454 dup2(devnull, STDERR_FILENO);
460 * Now we have logging check that the OpenSSL
464 * Initialize the event pool, including threads.
466 radius_event_init(mainconfig.config, spawn_flag);
469 * Now that we've set everything up, we can install the signal
470 * handlers. Before this, if we get any signal, we don't know
471 * what to do, so we might as well do the default, and die.
474 signal(SIGPIPE, SIG_IGN);
476 #ifdef HAVE_SIGACTION
477 act.sa_handler = sig_hup;
478 sigaction(SIGHUP, &act, NULL);
479 act.sa_handler = sig_fatal;
480 sigaction(SIGTERM, &act, NULL);
483 signal(SIGHUP, sig_hup);
485 signal(SIGTERM, sig_fatal);
488 * If we're debugging, then a CTRL-C will cause the
489 * server to die immediately. Use SIGTERM to shut down
490 * the server cleanly in that case.
492 if ((mainconfig.debug_memory == 1) || (debug_flag == 0)) {
493 #ifdef HAVE_SIGACTION
494 act.sa_handler = sig_fatal;
495 sigaction(SIGINT, &act, NULL);
496 sigaction(SIGQUIT, &act, NULL);
498 signal(SIGINT, sig_fatal);
500 signal(SIGQUIT, sig_fatal);
506 * Everything seems to have loaded OK, exit gracefully.
509 DEBUG("Configuration appears to be OK.");
513 radius_stats_init(0);
516 * Only write the PID file if we're running as a daemon.
518 * And write it AFTER we've forked, so that we write the
521 if (dont_fork == FALSE) {
524 fp = fopen(mainconfig.pid_file, "w");
527 * FIXME: What about following symlinks,
528 * and having it over-write a normal file?
530 fprintf(fp, "%d\n", (int) radius_pid);
533 radlog(L_ERR|L_CONS, "Failed creating PID file %s: %s\n",
534 mainconfig.pid_file, strerror(errno));
539 * Inform parent (who should still be waiting) that
540 * the rest of initialisation went OK, and that it
541 * should exit with a 0 status.
543 write(from_child[1], "\001", 1);
544 close(from_child[1]);
548 * Process requests until HUP or exit.
550 while ((rcode = radius_event_process()) == 0x80) {
551 radius_stats_init(1);
556 radlog(L_ERR, "Exiting due to internal error: %s",
560 radlog(L_INFO, "Exiting normally.");
565 * Ignore the TERM signal: we're
568 signal(SIGTERM, SIG_IGN);
571 * Send a TERM signal to all
572 * associated processes
573 * (including us, which gets
577 if (spawn_flag) kill(-radius_pid, SIGTERM);
581 * We're exiting, so we can delete the PID
582 * file. (If it doesn't exist, we can ignore
583 * the error returned by unlink)
585 if (dont_fork == FALSE) {
586 unlink(mainconfig.pid_file);
592 * Detach any modules.
596 xlat_free(); /* modules may have xlat's */
599 * Free the configuration items.
614 * Display the syntax for starting this program.
616 static void NEVER_RETURNS usage(int status)
618 FILE *output = status?stderr:stdout;
621 "Usage: %s [-d db_dir] [-l log_dir] [-i address] [-n name] [-fsvXx]\n", progname);
622 fprintf(output, "Options:\n\n");
623 fprintf(output, " -C Check configuration and exit.\n");
624 fprintf(output, " -d raddb_dir Configuration files are in \"raddbdir/*\".\n");
625 fprintf(output, " -f Run as a foreground process, not a daemon.\n");
626 fprintf(output, " -h Print this help message.\n");
627 fprintf(output, " -i ipaddr Listen on ipaddr ONLY.\n");
628 fprintf(output, " -l log_file Logging output will be written to this file.\n");
629 fprintf(output, " -m On SIGINT or SIGQUIT exit cleanly instead of immediately.\n");
630 fprintf(output, " -n name Read raddb/name.conf instead of raddb/radiusd.conf\n");
631 fprintf(output, " -p port Listen on port ONLY.\n");
632 fprintf(output, " -s Do not spawn child processes to handle requests.\n");
633 fprintf(output, " -t Disable threads.\n");
634 fprintf(output, " -v Print server version information.\n");
635 fprintf(output, " -X Turn on full debugging.\n");
636 fprintf(output, " -x Turn on additional debugging. (-xx gives more debugging).\n");
642 * We got a fatal signal.
644 static void sig_fatal(int sig)
646 if (getpid() != radius_pid) _exit(sig);
650 radius_signal_self(RADIUS_SIGNAL_SELF_TERM);
657 if (mainconfig.debug_memory) {
658 radius_signal_self(RADIUS_SIGNAL_SELF_TERM);
670 * We got the hangup signal.
671 * Re-read the configuration files.
673 static void sig_hup(int sig)
675 sig = sig; /* -Wunused */
677 reset_signal(SIGHUP, sig_hup);
679 radius_signal_self(RADIUS_SIGNAL_SELF_HUP);