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/autoconf.h>
35 #ifdef HAVE_NETINET_IN_H
36 # include <netinet/in.h>
54 #ifdef HAVE_SYS_SELECT_H
55 # include <sys/select.h>
58 #ifdef HAVE_SYS_WAIT_H
59 # include <sys/wait.h>
62 # define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
65 # define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
68 #include <freeradius-devel/radiusd.h>
69 #include <freeradius-devel/rad_assert.h>
70 #include <freeradius-devel/modules.h>
71 #include <freeradius-devel/request_list.h>
72 #include <freeradius-devel/radius_snmp.h>
74 #define SLEEP_FOREVER (65536)
79 const char *progname = NULL;
80 const char *radius_dir = NULL;
81 const char *radacct_dir = NULL;
82 const char *radlog_dir = NULL;
83 const char *radlib_dir = NULL;
84 int log_stripped_names;
86 int log_auth_detail = FALSE;
87 int need_reload = FALSE;
89 const char *radiusd_version = "FreeRADIUS Version " RADIUSD_VERSION ", for host " HOSTINFO ", built on " __DATE__ " at " __TIME__;
92 static pid_t radius_pid;
95 * Configuration items.
97 static int do_exit = 0;
102 static void usage(int);
104 static void sig_fatal (int);
105 static void sig_hup (int);
110 int main(int argc, char *argv[])
113 unsigned char buffer[4096];
119 int sleep_time = SLEEP_FOREVER;
120 int spawn_flag = TRUE;
121 int dont_fork = FALSE;
122 int sig_hup_block = FALSE;
123 time_t last_cleaned_lists = 0;
125 #ifdef HAVE_SIGACTION
126 struct sigaction act;
128 rad_listen_t *listener;
131 set_auth_parameters(argc,argv);
134 if ((progname = strrchr(argv[0], '/')) == NULL)
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.radiusd_conf = strdup("radiusd.conf");
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_fd = -1;
162 mainconfig.log_file = NULL;
164 /* Process the options. */
165 while ((argval = getopt(argc, argv, "Aa:bcd:fg:hi:l:n:p:sSvxXyz")) != EOF) {
170 log_auth_detail = TRUE;
174 if (radacct_dir) free(radacct_dir);
175 radacct_dir = strdup(optarg);
179 /* ignore for backwards compatibility with Cistron */
183 if (radius_dir) free(radius_dir);
184 radius_dir = strdup(optarg);
196 if (ip_hton(optarg, AF_UNSPEC, &mainconfig.myip) < 0) {
197 fprintf(stderr, "radiusd: Invalid IP Address or hostname \"%s\"\n", optarg);
203 if ((strcmp(optarg, "stdout") == 0) ||
204 (strcmp(optarg, "stderr") == 0) ||
205 (strcmp(optarg, "syslog") == 0)) {
206 fprintf(stderr, "radiusd: -l %s is unsupported. Use log_destination in radiusd.conf\n", optarg);
209 if (radlog_dir) free(radlog_dir);
210 radlog_dir = strdup(optarg);
214 fprintf(stderr, "radiusd: -g is unsupported. Use log_destination in radiusd.conf.\n");
219 if ((strchr(optarg, '/') != NULL) ||
220 (strchr(optarg, '.') != NULL) ||
221 (strlen(optarg) > 45)) usage(1);
223 snprintf(buffer, sizeof(buffer), "%s.conf",
225 if (mainconfig.radiusd_conf)
226 free(mainconfig.radiusd_conf);
227 mainconfig.radiusd_conf = strdup(buffer);
231 log_stripped_names++;
235 mainconfig.port = atoi(optarg);
236 if ((mainconfig.port <= 0) ||
237 (mainconfig.port >= 65536)) {
238 fprintf(stderr, "radiusd: Invalid port number %s\n", optarg);
243 case 's': /* Single process mode */
253 * BIG debugging mode for users who are
254 * TOO LAZY to type '-sfxxyz -l stdout' themselves.
260 mainconfig.log_auth = TRUE;
261 mainconfig.log_auth_badpass = TRUE;
262 mainconfig.log_auth_goodpass = TRUE;
263 mainconfig.radlog_dest = RADLOG_STDOUT;
264 mainconfig.radlog_fd = STDOUT_FILENO;
272 mainconfig.log_auth = TRUE;
273 mainconfig.log_auth_badpass = TRUE;
277 mainconfig.log_auth_badpass = TRUE;
278 mainconfig.log_auth_goodpass = TRUE;
287 /* Read the configuration files, BEFORE doing anything else. */
288 if (read_mainconfig(0) < 0) {
293 * Disconnect from session
295 if (debug_flag == 0 && dont_fork == FALSE) {
298 radlog(L_ERR|L_CONS, "Couldn't fork");
303 * The parent exits, so the child can run in the background.
314 * If we're NOT debugging, trap fatal signals, so we can
315 * easily clean up after ourselves.
317 * If we ARE debugging, don't trap them, so we can
320 if ((mainconfig.allow_core_dumps == FALSE) && (debug_flag == 0)) {
322 #ifdef HAVE_SIGACTION
323 act.sa_handler = sig_fatal;
324 sigaction(SIGSEGV, &act, NULL);
326 signal(SIGSEGV, sig_fatal);
332 if (mainconfig.do_snmp) radius_snmp_init();
336 * Ensure that we're using the CORRECT pid after forking,
337 * NOT the one we started with.
339 radius_pid = getpid();
343 * Only write the PID file if we're running as a daemon.
345 * And write it AFTER we've forked, so that we write the
348 if (dont_fork == FALSE) {
351 fp = fopen(mainconfig.pid_file, "w");
354 * FIXME: What about following symlinks,
355 * and having it over-write a normal file?
357 fprintf(fp, "%d\n", (int) radius_pid);
360 radlog(L_ERR|L_CONS, "Failed creating PID file %s: %s\n",
361 mainconfig.pid_file, strerror(errno));
367 * If we're running as a daemon, close the default file
368 * descriptors, AFTER forking.
373 devnull = open("/dev/null", O_RDWR);
375 radlog(L_ERR|L_CONS, "Failed opening /dev/null: %s\n",
379 dup2(devnull, STDIN_FILENO);
380 if (mainconfig.radlog_dest == RADLOG_STDOUT) {
381 mainconfig.radlog_fd = dup(STDOUT_FILENO);
383 dup2(devnull, STDOUT_FILENO);
384 if (mainconfig.radlog_dest == RADLOG_STDERR) {
385 mainconfig.radlog_fd = dup(STDERR_FILENO);
387 dup2(devnull, STDERR_FILENO);
392 * It's called the thread pool, but it does a little
395 thread_pool_init(spawn_flag);
398 * Use linebuffered or unbuffered stdout if
399 * the debug flag is on.
401 if (debug_flag == TRUE)
405 * Print out which ports we're listening on.
407 for (listener = mainconfig.listen;
409 listener = listener->next) {
410 listener->print(listener, buffer, sizeof(buffer));
411 switch (listener->type) {
412 case RAD_LISTEN_AUTH:
413 DEBUG("Listening on authentication address %s", buffer);
416 case RAD_LISTEN_ACCT:
417 DEBUG("Listening on accounting address %s", buffer);
420 case RAD_LISTEN_PROXY:
421 DEBUG("Listening on proxy address %s", buffer);
424 case RAD_LISTEN_DETAIL:
425 DEBUG("Listening on detail file %s", buffer);
434 * Now that we've set everything up, we can install the signal
435 * handlers. Before this, if we get any signal, we don't know
436 * what to do, so we might as well do the default, and die.
438 signal(SIGPIPE, SIG_IGN);
439 #ifdef HAVE_SIGACTION
440 act.sa_handler = sig_hup;
441 sigaction(SIGHUP, &act, NULL);
442 act.sa_handler = sig_fatal;
443 sigaction(SIGTERM, &act, NULL);
445 signal(SIGHUP, sig_hup);
446 signal(SIGTERM, sig_fatal);
449 * If we're debugging, then a CTRL-C will cause the
450 * server to die immediately. Use SIGTERM to shut down
451 * the server cleanly in that case.
453 if (debug_flag == 0) {
454 #ifdef HAVE_SIGACTION
455 act.sa_handler = sig_fatal;
456 sigaction(SIGINT, &act, NULL);
457 sigaction(SIGQUIT, &act, NULL);
459 signal(SIGINT, sig_fatal);
460 signal(SIGQUIT, sig_fatal);
464 radlog(L_INFO, "Ready to process requests.");
467 * Receive user requests
471 * If we've been told to exit, then do so,
472 * even if we have data waiting.
478 * Ignore the TERM signal: we're about
481 signal(SIGTERM, SIG_IGN);
484 * Send a TERM signal to all associated
485 * processes (including us, which gets
488 kill(-radius_pid, SIGTERM);
491 * FIXME: Kill child threads, and
496 * Detach any modules.
501 * FIXME: clean up any active REQUEST
506 * We're exiting, so we can delete the PID
507 * file. (If it doesn't exist, we can ignore
508 * the error returned by unlink)
510 if (dont_fork == FALSE) {
511 unlink(mainconfig.pid_file);
515 * Free the configuration items.
520 * SIGTERM gets do_exit=0,
521 * and we want to exit cleanly.
523 * Other signals make us exit
524 * with an error status.
530 #ifdef HAVE_PTHREAD_H
532 * Threads: wait for all threads to stop
533 * processing before re-loading the
534 * config, so we don't pull the rug out
538 if (spawn_flag) for(;;) {
540 * Block until there are '0' threads
541 * with a REQUEST handle.
543 sig_hup_block = TRUE;
544 if( (total_active_threads() == 0) ||
546 sig_hup_block = FALSE;
553 if (read_mainconfig(TRUE) < 0) {
558 radlog(L_INFO, "Ready to process requests.");
565 * Loop over all the listening FD's.
567 for (listener = mainconfig.listen;
569 listener = listener->next) {
570 if (listener->fd < 0) continue;
572 FD_SET(listener->fd, &readfds);
573 if (listener->fd > max_fd) max_fd = listener->fd;
577 if (mainconfig.do_snmp &&
578 (rad_snmp.smux_fd >= 0)) {
579 FD_SET(rad_snmp.smux_fd, &readfds);
580 if (rad_snmp.smux_fd > max_fd) max_fd = rad_snmp.smux_fd;
584 if (sleep_time == SLEEP_FOREVER) {
585 DEBUG2("Nothing to do. Sleeping until we see a request.");
586 status = select(max_fd + 1, &readfds, NULL, NULL, NULL);
590 DEBUG2("Waking up in %d seconds...", sleep_time);
592 tv.tv_sec = sleep_time;
594 status = select(max_fd + 1, &readfds, NULL, NULL, &tv);
598 * On interrupts, we clean up the request
599 * list. We then continue with the loop,
600 * so that if we're supposed to exit,
601 * then the code at the start of the loop
602 * catches that, and exits.
604 if (errno == EINTR) {
605 #ifdef MEMORY_USE_DEBUGGING
607 * Run the server in debugging mode,
608 * without threads, and give it a
609 * SIGHUP. It will clean up after
610 * itself, and any memory left over
611 * should be allocated by C libraries,
622 radlog(L_ERR, "Unexpected error in select(): %s",
627 time_now = time(NULL);
628 #ifndef HAVE_PTHREAD_H
630 * If there are no child threads, then there may
631 * be child processes. In that case, wait for
632 * their exit status, and throw that exit status
633 * away. This helps get rid of zxombie children.
635 while (waitpid(-1, &argval, WNOHANG) > 0) {
641 * Loop over the open socket FD's, reading any data.
643 for (listener = mainconfig.listen;
645 listener = listener->next) {
646 RAD_REQUEST_FUNP fun;
648 if ((listener->fd >= 0) &&
649 !FD_ISSET(listener->fd, &readfds))
653 * Receive the packet.
655 if (sig_hup_block != FALSE) {
660 * Do per-socket receive processing of the
663 if (!listener->recv(listener, &fun, &request)) {
668 * Drop the request into the thread pool,
669 * and let the thread pool take care of
670 * doing something with it.
672 if (!thread_pool_addrequest(request, fun)) {
674 * FIXME: Maybe just drop
675 * the packet on the floor?
677 request_reject(request, REQUEST_FAIL_NO_THREADS);
678 request->finished = TRUE;
680 } /* loop over listening sockets*/
683 if (mainconfig.do_snmp) {
685 * After handling all authentication/accounting
686 * requests, THEN process any pending SMUX/SNMP
689 * Note that the handling is done in the main server,
690 * which probably isn't a Good Thing. It really
691 * should be wrapped, and handled in a thread pool.
693 if ((rad_snmp.smux_fd >= 0) &&
694 FD_ISSET(rad_snmp.smux_fd, &readfds) &&
695 (rad_snmp.smux_event == SMUX_READ)) {
700 * If we've got to re-connect, then do so now,
701 * before calling select again.
703 if (rad_snmp.smux_event == SMUX_CONNECT) {
710 * Loop through the request lists once per
711 * second, to clean up old requests.
713 if (last_cleaned_lists != time_now) {
714 last_cleaned_lists = time_now;
716 DEBUG2("--- Walking the entire request list ---");
717 sleep_time = SLEEP_FOREVER;
718 for (listener = mainconfig.listen;
720 listener = listener->next) {
723 next = listener->update(listener, time_now);
724 if (next < sleep_time) {
730 #ifdef HAVE_PTHREAD_H
733 * Only clean the thread pool if we're spawning
737 thread_pool_clean(time_now);
745 * Display the syntax for starting this program.
747 static void NEVER_RETURNS usage(int status)
749 FILE *output = status?stderr:stdout;
752 "Usage: %s [-a acct_dir] [-d db_dir] [-l log_dir] [-i address] [-AcfnsSvXxyz]\n", progname);
753 fprintf(output, "Options:\n\n");
754 fprintf(output, " -a acct_dir use accounting directory 'acct_dir'.\n");
755 fprintf(output, " -A Log auth detail.\n");
756 fprintf(output, " -d raddb_dir Configuration files are in \"raddbdir/*\".\n");
757 fprintf(output, " -f Run as a foreground process, not a daemon.\n");
758 fprintf(output, " -h Print this help message.\n");
759 fprintf(output, " -i ipaddr Listen on ipaddr ONLY\n");
760 fprintf(output, " -l log_dir Log file is \"log_dir/radius.log\" (not used in debug mode)\n");
761 fprintf(output, " -p port Listen on port ONLY\n");
762 fprintf(output, " -s Do not spawn child processes to handle requests.\n");
763 fprintf(output, " -S Log stripped names.\n");
764 fprintf(output, " -v Print server version information.\n");
765 fprintf(output, " -X Turn on full debugging.\n");
766 fprintf(output, " -x Turn on additional debugging. (-xx gives more debugging).\n");
767 fprintf(output, " -y Log authentication failures, with password.\n");
768 fprintf(output, " -z Log authentication successes, with password.\n");
774 * We got a fatal signal.
776 static void sig_fatal(int sig)
780 /* We can't really do anything intelligent here so just die */
793 * We got the hangup signal.
794 * Re-read the configuration files.
797 static void sig_hup(int sig)
799 sig = sig; /* -Wunused */
801 reset_signal(SIGHUP, sig_hup);
804 * Only do the reload if we're the main server, both
805 * for processes, and for threads.
807 if (getpid() == radius_pid) {
811 if (mainconfig.do_snmp) {
812 rad_snmp.smux_failures = 0;
813 rad_snmp.smux_event = SMUX_CONNECT;