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-2012 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>
30 #include <freeradius-devel/radiusd.h>
31 #include <freeradius-devel/modules.h>
32 #include <freeradius-devel/rad_assert.h>
43 #ifdef HAVE_SYS_WAIT_H
44 # include <sys/wait.h>
47 # define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
50 # define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
56 char const *progname = NULL;
57 char const *radius_dir = NULL;
58 char const *radacct_dir = NULL;
59 char const *radlog_dir = NULL;
60 char const *radlib_dir = NULL;
61 bool log_stripped_names;
62 log_debug_t debug_flag = 0;
63 bool check_config = false;
65 char const *radiusd_version = "FreeRADIUS Version " RADIUSD_VERSION_STRING
66 #ifdef RADIUSD_VERSION_COMMIT
67 " (git #" STRINGIFY(RADIUSD_VERSION_COMMIT) ")"
69 ", for host " HOSTINFO ", built on " __DATE__ " at " __TIME__;
74 * Configuration items.
80 static void usage(int);
82 static void sig_fatal (int);
84 static void sig_hup (int);
90 int main(int argc, char *argv[])
92 int rcode = EXIT_SUCCESS;
95 bool spawn_flag = true;
96 bool write_pid = false;
97 bool display_version = false;
99 int from_child[2] = {-1, -1};
102 * We probably don't want to free the talloc autofree context
103 * directly, so we'll allocate a new context beneath it, and
104 * free that before any leak reports.
106 TALLOC_CTX *autofree = talloc_init("main");
109 * If the server was built with debugging enabled always install
110 * the basic fatal signal handlers.
113 if (fr_fault_setup(getenv("PANIC_ACTION"), argv[0]) < 0) {
114 fr_perror("radiusd");
120 set_auth_parameters(argc,argv);
123 if ((progname = strrchr(argv[0], FR_DIR_SEP)) == NULL)
131 if (WSAStartup(MAKEWORD(2, 0), &wsaData)) {
132 fprintf(stderr, "%s: Unable to initialize socket library.\n", progname);
139 set_radius_dir(autofree, RADIUS_DIR);
142 * Ensure that the configuration is initialized.
144 memset(&main_config, 0, sizeof(main_config));
145 main_config.myip.af = AF_UNSPEC;
146 main_config.port = 0;
147 main_config.name = "radiusd";
148 main_config.daemonize = true;
151 * Don't put output anywhere until we get told a little
154 default_log.dst = L_DST_NULL;
156 main_config.log_file = NULL;
158 /* Process the options. */
159 while ((argval = getopt(argc, argv, "Cd:D:fhi:l:mMn:p:PstvxX")) != EOF) {
165 main_config.daemonize = false;
169 set_radius_dir(autofree, optarg);
173 main_config.dictionary_dir = talloc_typed_strdup(NULL, optarg);
177 main_config.daemonize = false;
185 if (strcmp(optarg, "stdout") == 0) {
188 main_config.log_file = strdup(optarg);
189 default_log.dst = L_DST_FILES;
190 default_log.fd = open(main_config.log_file,
191 O_WRONLY | O_APPEND | O_CREAT, 0640);
192 if (default_log.fd < 0) {
193 fprintf(stderr, "radiusd: Failed to open log file %s: %s\n", main_config.log_file, fr_syserror(errno));
196 fr_log_fp = fdopen(default_log.fd, "a");
200 if (ip_hton(&main_config.myip, AF_UNSPEC, optarg, false) < 0) {
201 fprintf(stderr, "radiusd: Invalid IP Address or hostname \"%s\"\n", optarg);
208 main_config.name = optarg;
212 main_config.debug_memory = true;
216 main_config.memory_report = true;
217 main_config.debug_memory = true;
224 port = strtoul(optarg, 0, 10);
225 if ((port == 0) || (port > UINT16_MAX)) {
226 fprintf(stderr, "radiusd: Invalid port number \"%s\"\n", optarg);
230 main_config.port = (uint16_t) port;
236 /* Force the PID to be written, even in -f mode */
240 case 's': /* Single process mode */
242 main_config.daemonize = false;
245 case 't': /* no child threads */
250 display_version = true;
255 main_config.daemonize = false;
257 main_config.log_auth = true;
258 main_config.log_auth_badpass = true;
259 main_config.log_auth_goodpass = true;
262 default_log.dst = L_DST_STDOUT;
263 default_log.fd = STDOUT_FILENO;
277 * Mismatch between the binary and the libraries it depends on
279 if (fr_check_lib_magic(RADIUSD_MAGIC_NUMBER) < 0) {
280 fr_perror("radiusd");
284 if (rad_check_lib_magic(RADIUSD_MAGIC_NUMBER) < 0) {
289 * Mismatch between build time OpenSSL and linked SSL,
290 * better to die here than segfault later.
292 #ifdef HAVE_OPENSSL_CRYPTO_H
293 if (ssl_check_consistency() < 0) {
298 if (flag && (flag != 0x03)) {
299 fprintf(stderr, "radiusd: The options -i and -p cannot be used individually.\n");
304 * According to the talloc peeps, no two threads
305 * may modify any part of a ctx tree with a common
306 * root without synchronisation.
308 * So we can't run with a null context and threads.
310 if (main_config.memory_report) {
312 fprintf(stderr, "radiusd: The server cannot produce memory reports (-M) in threaded mode\n");
315 talloc_enable_null_tracking();
317 talloc_disable_null_tracking();
321 * Better here, so it doesn't matter whether we get passed
324 if (display_version) {
325 /* Don't print timestamps */
328 default_log.dst = L_DST_STDOUT;
329 default_log.fd = STDOUT_FILENO;
340 * Initialising OpenSSL once, here, is safer than having individual
343 #ifdef HAVE_OPENSSL_CRYPTO_H
348 * Initialize any event loops just enough so module instantiations
349 * can add fd/event to them, but do not start them yet.
351 if (!radius_event_init(autofree)) {
355 /* Read the configuration files, BEFORE doing anything else. */
356 if (main_config_init() < 0) {
360 /* Check for vulnerabilities in the version of libssl were linked against */
361 #ifdef HAVE_OPENSSL_CRYPTO_H
362 if (tls_global_version_check(main_config.allow_vulnerable_openssl) < 0) {
370 if (modules_init(main_config.config) < 0) {
374 /* Set the panic action (if required) */
375 if (main_config.panic_action &&
377 !getenv("PANIC_ACTION") &&
379 (fr_fault_setup(main_config.panic_action, argv[0]) < 0)) {
380 fr_perror("radiusd");
388 * Disconnect from session
390 if (main_config.daemonize) {
395 * Really weird things happen if we leave stdin open and call things like
398 devnull = open("/dev/null", O_RDWR);
400 ERROR("Failed opening /dev/null: %s", fr_syserror(errno));
403 dup2(devnull, STDIN_FILENO);
407 if (pipe(from_child) != 0) {
408 ERROR("Couldn't open pipe for child status: %s", fr_syserror(errno));
414 ERROR("Couldn't fork: %s", fr_syserror(errno));
419 * The parent exits, so the child can run in the background.
421 * As the child can still encounter an error during initialisation
422 * we do a blocking read on a pipe between it and the parent.
424 * Just before entering the event loop the child will send a success
425 * or failure message to the parent, via the pipe.
431 /* So the pipe is correctly widowed if the child exits */
432 close(from_child[1]);
435 * The child writes a 0x01 byte on
436 * success, and closes the pipe on error.
438 if ((read(from_child[0], &ret, 1) < 0)) {
442 /* For cleanliness... */
443 close(from_child[0]);
445 /* Don't turn children into zombies */
447 waitpid(pid, &stat_loc, WNOHANG);
454 /* so the pipe is correctly widowed if the parent exits?! */
455 close(from_child[0]);
463 * Ensure that we're using the CORRECT pid after forking,
464 * NOT the one we started with.
466 radius_pid = getpid();
469 * Redirect stderr/stdout as appropriate.
471 if (radlog_init(&default_log, main_config.daemonize) < 0) {
472 ERROR("%s", fr_strerror());
477 * Start the event loop(s) and threads.
479 radius_event_start(main_config.config, spawn_flag);
482 * Now that we've set everything up, we can install the signal
483 * handlers. Before this, if we get any signal, we don't know
484 * what to do, so we might as well do the default, and die.
487 signal(SIGPIPE, SIG_IGN);
490 if ((fr_set_signal(SIGHUP, sig_hup) < 0) ||
491 (fr_set_signal(SIGTERM, sig_fatal) < 0)) {
492 ERROR("%s", fr_strerror());
497 * If we're debugging, then a CTRL-C will cause the
498 * server to die immediately. Use SIGTERM to shut down
499 * the server cleanly in that case.
501 if (main_config.debug_memory || (debug_flag == 0)) {
502 if ((fr_set_signal(SIGINT, sig_fatal) < 0)
504 || (fr_set_signal(SIGQUIT, sig_fatal) < 0)
507 ERROR("%s", fr_strerror());
513 * Everything seems to have loaded OK, exit gracefully.
516 DEBUG("Configuration appears to be OK");
519 if (main_config.debug_memory) {
527 radius_stats_init(0);
531 * Write the PID always if we're running as a daemon.
533 if (main_config.daemonize) write_pid = true;
536 * Write the PID after we've forked, so that we write the
542 fp = fopen(main_config.pid_file, "w");
545 * FIXME: What about following symlinks,
546 * and having it over-write a normal file?
548 fprintf(fp, "%d\n", (int) radius_pid);
551 ERROR("Failed creating PID file %s: %s\n",
552 main_config.pid_file, fr_syserror(errno));
557 exec_trigger(NULL, NULL, "server.start", false);
560 * Inform the parent (who should still be waiting) that
561 * the rest of initialisation went OK, and that it should
562 * exit with a 0 status. If we don't get this far, then
563 * we just close the pipe on exit, and the parent gets a
566 if (main_config.daemonize) {
567 if (write(from_child[1], "\001", 1) < 0) {
568 WARN("Failed informing parent of successful start: %s",
571 close(from_child[1]);
575 * Clear the libfreeradius error buffer
580 * Process requests until HUP or exit.
582 while ((status = radius_event_process()) == 0x80) {
584 radius_stats_init(1);
589 ERROR("Exiting due to internal error: %s", fr_strerror());
590 rcode = EXIT_FAILURE;
592 INFO("Exiting normally");
595 exec_trigger(NULL, NULL, "server.stop", false);
598 * Ignore the TERM signal: we're
601 signal(SIGTERM, SIG_IGN);
604 * Send a TERM signal to all
605 * associated processes
606 * (including us, which gets
610 if (spawn_flag) kill(-radius_pid, SIGTERM);
614 * We're exiting, so we can delete the PID
615 * file. (If it doesn't exist, we can ignore
616 * the error returned by unlink)
618 if (main_config.daemonize) {
619 unlink(main_config.pid_file);
626 * Detach any modules.
630 xlat_free(); /* modules may have xlat's */
633 * Free the configuration items.
641 #ifdef HAVE_OPENSSL_CRYPTO_H
642 tls_global_cleanup();
646 * So we don't see autofreed memory in the talloc report
648 talloc_free(autofree);
650 if (main_config.memory_report) {
651 INFO("Allocated memory at time of report:");
652 fr_log_talloc_report(NULL);
660 * Display the syntax for starting this program.
662 static void NEVER_RETURNS usage(int status)
664 FILE *output = status?stderr:stdout;
666 fprintf(output, "Usage: %s [options]\n", progname);
667 fprintf(output, "Options:\n");
668 fprintf(output, " -C Check configuration and exit.\n");
669 fprintf(stderr, " -d <raddb> Set configuration directory (defaults to " RADDBDIR ").\n");
670 fprintf(stderr, " -D <dictdir> Set main dictionary directory (defaults to " DICTDIR ").\n");
671 fprintf(output, " -f Run as a foreground process, not a daemon.\n");
672 fprintf(output, " -h Print this help message.\n");
673 fprintf(output, " -i <ipaddr> Listen on ipaddr ONLY.\n");
674 fprintf(output, " -l <log_file> Logging output will be written to this file.\n");
675 fprintf(output, " -m On SIGINT or SIGQUIT exit cleanly instead of immediately.\n");
676 fprintf(output, " -n <name> Read raddb/name.conf instead of raddb/radiusd.conf.\n");
677 fprintf(output, " -p <port> Listen on port ONLY.\n");
678 fprintf(output, " -P Always write out PID, even with -f.\n");
679 fprintf(output, " -s Do not spawn child processes to handle requests.\n");
680 fprintf(output, " -t Disable threads.\n");
681 fprintf(output, " -v Print server version information.\n");
682 fprintf(output, " -X Turn on full debugging.\n");
683 fprintf(output, " -x Turn on additional debugging. (-xx gives more debugging).\n");
689 * We got a fatal signal.
691 static void sig_fatal(int sig)
693 if (getpid() != radius_pid) _exit(sig);
697 radius_signal_self(RADIUS_SIGNAL_SELF_TERM);
704 if (main_config.debug_memory || main_config.memory_report) {
705 radius_signal_self(RADIUS_SIGNAL_SELF_TERM);
717 * We got the hangup signal.
718 * Re-read the configuration files.
720 static void sig_hup(UNUSED int sig)
722 reset_signal(SIGHUP, sig_hup);
724 radius_signal_self(RADIUS_SIGNAL_SELF_HUP);