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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * Copyright 2000,2001,2002,2003,2004 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 /* don't look here for the version, run radiusd -v or look in version.c */
29 static const char rcsid[] =
36 #ifdef HAVE_NETINET_IN_H
37 # include <netinet/in.h>
55 #ifdef HAVE_SYS_SELECT_H
56 # include <sys/select.h>
59 #ifdef HAVE_SYS_WAIT_H
60 # include <sys/wait.h>
63 # define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
66 # define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
70 #include "rad_assert.h"
73 #include "request_list.h"
74 #include "radius_snmp.h"
76 #define SLEEP_FOREVER (65536)
81 const char *progname = NULL;
82 const char *radius_dir = NULL;
83 const char *radacct_dir = NULL;
84 const char *radlog_dir = NULL;
85 const char *radlib_dir = NULL;
86 int log_stripped_names;
88 int log_auth_detail = FALSE;
89 int need_reload = FALSE;
90 const char *radiusd_version = "FreeRADIUS Version " RADIUSD_VERSION ", for host " HOSTINFO ", built on " __DATE__ " at " __TIME__;
93 static pid_t radius_pid;
96 * Configuration items.
98 static int do_exit = 0;
103 static void usage(int);
105 static void sig_fatal (int);
106 static void sig_hup (int);
111 int main(int argc, char *argv[])
114 unsigned char buffer[4096];
120 int sleep_time = SLEEP_FOREVER;
121 int spawn_flag = TRUE;
122 int dont_fork = FALSE;
123 int sig_hup_block = FALSE;
124 time_t last_cleaned_lists = 0;
126 #ifdef HAVE_SIGACTION
127 struct sigaction act;
129 rad_listen_t *listener;
132 set_auth_parameters(argc,argv);
135 if ((progname = strrchr(argv[0], '/')) == NULL)
142 radius_dir = strdup(RADIUS_DIR);
145 * Ensure that the configuration is initialized.
147 memset(&mainconfig, 0, sizeof(mainconfig));
148 mainconfig.myip.af = AF_UNSPEC;
149 mainconfig.port = -1;
150 mainconfig.radiusd_conf = "radiusd.conf";
152 #ifdef HAVE_SIGACTION
153 memset(&act, 0, sizeof(act));
155 sigemptyset( &act.sa_mask ) ;
158 /* Process the options. */
159 while ((argval = getopt(argc, argv, "Aa:bcd:fg:hi:l:n:p:sSvxXyz")) != EOF) {
164 log_auth_detail = TRUE;
168 if (radacct_dir) free(radacct_dir);
169 radacct_dir = strdup(optarg);
173 /* ignore for backwards compatibility with Cistron */
177 if (radius_dir) free(radius_dir);
178 radius_dir = strdup(optarg);
190 if (ip_hton(optarg, AF_UNSPEC, &mainconfig.myip) < 0) {
191 fprintf(stderr, "radiusd: Invalid IP Address or hostname \"%s\"\n", optarg);
197 if ((strcmp(optarg, "stdout") == 0) ||
198 (strcmp(optarg, "stderr") == 0) ||
199 (strcmp(optarg, "syslog") == 0)) {
200 fprintf(stderr, "radiusd: -l %s is unsupported. Use log_destination in radiusd.conf\n", optarg);
203 radlog_dir = strdup(optarg);
207 fprintf(stderr, "radiusd: -g is unsupported. Use log_destination in radiusd.conf.\n");
212 if ((strchr(optarg, '/') != NULL) ||
213 (strchr(optarg, '.') != NULL) ||
214 (strlen(optarg) > 45)) usage(1);
216 snprintf(buffer, sizeof(buffer), "%s.conf",
218 mainconfig.radiusd_conf = strdup(buffer);
222 log_stripped_names++;
226 mainconfig.port = atoi(optarg);
227 if ((mainconfig.port <= 0) ||
228 (mainconfig.port >= 65536)) {
229 fprintf(stderr, "radiusd: Invalid port number %s\n", optarg);
234 case 's': /* Single process mode */
244 * BIG debugging mode for users who are
245 * TOO LAZY to type '-sfxxyz -l stdout' themselves.
251 mainconfig.log_auth = TRUE;
252 mainconfig.log_auth_badpass = TRUE;
253 mainconfig.log_auth_goodpass = TRUE;
254 mainconfig.radlog_dest = RADLOG_STDOUT;
262 mainconfig.log_auth = TRUE;
263 mainconfig.log_auth_badpass = TRUE;
267 mainconfig.log_auth_badpass = TRUE;
268 mainconfig.log_auth_goodpass = TRUE;
277 /* Read the configuration files, BEFORE doing anything else. */
278 if (read_mainconfig(0) < 0) {
283 * Disconnect from session
285 if (debug_flag == 0 && dont_fork == FALSE) {
288 radlog(L_ERR|L_CONS, "Couldn't fork");
293 * The parent exits, so the child can run in the background.
304 * If we're NOT debugging, trap fatal signals, so we can
305 * easily clean up after ourselves.
307 * If we ARE debugging, don't trap them, so we can
310 if ((mainconfig.allow_core_dumps == FALSE) && (debug_flag == 0)) {
312 #ifdef HAVE_SIGACTION
313 act.sa_handler = sig_fatal;
314 sigaction(SIGSEGV, &act, NULL);
316 signal(SIGSEGV, sig_fatal);
322 if (mainconfig.do_snmp) radius_snmp_init();
326 * Ensure that we're using the CORRECT pid after forking,
327 * NOT the one we started with.
329 radius_pid = getpid();
333 * Only write the PID file if we're running as a daemon.
335 * And write it AFTER we've forked, so that we write the
338 if (dont_fork == FALSE) {
341 fp = fopen(mainconfig.pid_file, "w");
344 * FIXME: What about following symlinks,
345 * and having it over-write a normal file?
347 fprintf(fp, "%d\n", (int) radius_pid);
350 radlog(L_ERR|L_CONS, "Failed creating PID file %s: %s\n",
351 mainconfig.pid_file, strerror(errno));
357 * If we're running as a daemon, close the default file
358 * descriptors, AFTER forking.
360 mainconfig.radlog_fd = -1;
362 mainconfig.radlog_fd = STDOUT_FILENO;
366 devnull = open("/dev/null", O_RDWR);
368 radlog(L_ERR|L_CONS, "Failed opening /dev/null: %s\n",
372 dup2(devnull, STDIN_FILENO);
373 if (mainconfig.radlog_dest == RADLOG_STDOUT) {
374 mainconfig.radlog_fd = dup(STDOUT_FILENO);
376 dup2(devnull, STDOUT_FILENO);
377 if (mainconfig.radlog_dest == RADLOG_STDERR) {
378 mainconfig.radlog_fd = dup(STDERR_FILENO);
380 dup2(devnull, STDERR_FILENO);
385 * It's called the thread pool, but it does a little
388 thread_pool_init(spawn_flag);
391 * Use linebuffered or unbuffered stdout if
392 * the debug flag is on.
394 if (debug_flag == TRUE)
398 * Print out which ports we're listening on.
400 for (listener = mainconfig.listen;
402 listener = listener->next) {
403 listener->print(listener, buffer, sizeof(buffer));
404 switch (listener->type) {
405 case RAD_LISTEN_AUTH:
406 DEBUG("Listening on authentication address %s", buffer);
409 case RAD_LISTEN_ACCT:
410 DEBUG("Listening on accounting address %s", buffer);
413 case RAD_LISTEN_PROXY:
414 DEBUG("Listening on proxy address %s", buffer);
417 case RAD_LISTEN_DETAIL:
418 DEBUG("Listening on detail file %s", buffer);
427 * Now that we've set everything up, we can install the signal
428 * handlers. Before this, if we get any signal, we don't know
429 * what to do, so we might as well do the default, and die.
431 signal(SIGPIPE, SIG_IGN);
432 #ifdef HAVE_SIGACTION
433 act.sa_handler = sig_hup;
434 sigaction(SIGHUP, &act, NULL);
435 act.sa_handler = sig_fatal;
436 sigaction(SIGTERM, &act, NULL);
438 signal(SIGHUP, sig_hup);
439 signal(SIGTERM, sig_fatal);
442 * If we're debugging, then a CTRL-C will cause the
443 * server to die immediately. Use SIGTERM to shut down
444 * the server cleanly in that case.
446 if (debug_flag == 0) {
447 #ifdef HAVE_SIGACTION
448 act.sa_handler = sig_fatal;
449 sigaction(SIGINT, &act, NULL);
450 sigaction(SIGQUIT, &act, NULL);
452 signal(SIGINT, sig_fatal);
453 signal(SIGQUIT, sig_fatal);
457 radlog(L_INFO, "Ready to process requests.");
460 * Receive user requests
464 * If we've been told to exit, then do so,
465 * even if we have data waiting.
471 * Ignore the TERM signal: we're about
474 signal(SIGTERM, SIG_IGN);
477 * Send a TERM signal to all associated
478 * processes (including us, which gets
481 kill(-radius_pid, SIGTERM);
484 * FIXME: Kill child threads, and
489 * Detach any modules.
494 * FIXME: clean up any active REQUEST
499 * We're exiting, so we can delete the PID
500 * file. (If it doesn't exist, we can ignore
501 * the error returned by unlink)
503 if (dont_fork == FALSE) {
504 unlink(mainconfig.pid_file);
508 * Free the configuration items.
513 * SIGTERM gets do_exit=0,
514 * and we want to exit cleanly.
516 * Other signals make us exit
517 * with an error status.
523 #ifdef HAVE_PTHREAD_H
525 * Threads: wait for all threads to stop
526 * processing before re-loading the
527 * config, so we don't pull the rug out
531 if (!spawn_flag) for(;;) {
533 * Block until there are '0' threads
534 * with a REQUEST handle.
536 sig_hup_block = TRUE;
537 if( (total_active_threads() == 0) ||
539 sig_hup_block = FALSE;
546 if (read_mainconfig(TRUE) < 0) {
551 radlog(L_INFO, "Ready to process requests.");
558 * Loop over all the listening FD's.
560 for (listener = mainconfig.listen;
562 listener = listener->next) {
563 if (listener->fd < 0) continue;
565 FD_SET(listener->fd, &readfds);
566 if (listener->fd > max_fd) max_fd = listener->fd;
570 if (mainconfig.do_snmp &&
571 (rad_snmp.smux_fd >= 0)) {
572 FD_SET(rad_snmp.smux_fd, &readfds);
573 if (rad_snmp.smux_fd > max_fd) max_fd = rad_snmp.smux_fd;
577 if (sleep_time == SLEEP_FOREVER) {
578 DEBUG2("Nothing to do. Sleeping until we see a request.");
579 status = select(max_fd + 1, &readfds, NULL, NULL, NULL);
583 DEBUG2("Waking up in %d seconds...", sleep_time);
585 tv.tv_sec = sleep_time;
587 status = select(max_fd + 1, &readfds, NULL, NULL, &tv);
591 * On interrupts, we clean up the request
592 * list. We then continue with the loop,
593 * so that if we're supposed to exit,
594 * then the code at the start of the loop
595 * catches that, and exits.
597 if (errno == EINTR) {
598 #ifdef MEMORY_USE_DEBUGGING
600 * Run the server in debugging mode,
601 * without threads, and give it a
602 * SIGHUP. It will clean up after
603 * itself, and any memory left over
604 * should be allocated by C libraries,
615 radlog(L_ERR, "Unexpected error in select(): %s",
620 time_now = time(NULL);
621 #ifndef HAVE_PTHREAD_H
623 * If there are no child threads, then there may
624 * be child processes. In that case, wait for
625 * their exit status, and throw that exit status
626 * away. This helps get rid of zxombie children.
628 while (waitpid(-1, &argval, WNOHANG) > 0) {
634 * Loop over the open socket FD's, reading any data.
636 for (listener = mainconfig.listen;
638 listener = listener->next) {
639 RAD_REQUEST_FUNP fun;
641 if ((listener->fd >= 0) &&
642 !FD_ISSET(listener->fd, &readfds))
646 * Receive the packet.
648 if (sig_hup_block != FALSE) {
653 * Do per-socket receive processing of the
656 if (!listener->recv(listener, &fun, &request)) {
661 * Drop the request into the thread pool,
662 * and let the thread pool take care of
663 * doing something with it.
665 if (!thread_pool_addrequest(request, fun)) {
667 * FIXME: Maybe just drop
668 * the packet on the floor?
670 request_reject(request, REQUEST_FAIL_NO_THREADS);
671 request->finished = TRUE;
673 } /* loop over listening sockets*/
676 if (mainconfig.do_snmp) {
678 * After handling all authentication/accounting
679 * requests, THEN process any pending SMUX/SNMP
682 * Note that the handling is done in the main server,
683 * which probably isn't a Good Thing. It really
684 * should be wrapped, and handled in a thread pool.
686 if ((rad_snmp.smux_fd >= 0) &&
687 FD_ISSET(rad_snmp.smux_fd, &readfds) &&
688 (rad_snmp.smux_event == SMUX_READ)) {
693 * If we've got to re-connect, then do so now,
694 * before calling select again.
696 if (rad_snmp.smux_event == SMUX_CONNECT) {
703 * Loop through the request lists once per
704 * second, to clean up old requests.
706 if (last_cleaned_lists != time_now) {
707 last_cleaned_lists = time_now;
709 DEBUG2("--- Walking the entire request list ---");
710 sleep_time = SLEEP_FOREVER;
711 for (listener = mainconfig.listen;
713 listener = listener->next) {
716 next = listener->update(listener, time_now);
717 if (next < sleep_time) {
723 #ifdef HAVE_PTHREAD_H
726 * Only clean the thread pool if we're spawning
730 thread_pool_clean(time_now);
738 * Display the syntax for starting this program.
740 static void usage(int status)
742 FILE *output = status?stderr:stdout;
745 "Usage: %s [-a acct_dir] [-d db_dir] [-l log_dir] [-i address] [-AcfnsSvXxyz]\n", progname);
746 fprintf(output, "Options:\n\n");
747 fprintf(output, " -a acct_dir use accounting directory 'acct_dir'.\n");
748 fprintf(output, " -A Log auth detail.\n");
749 fprintf(output, " -d raddb_dir Configuration files are in \"raddbdir/*\".\n");
750 fprintf(output, " -f Run as a foreground process, not a daemon.\n");
751 fprintf(output, " -h Print this help message.\n");
752 fprintf(output, " -i ipaddr Listen on ipaddr ONLY\n");
753 fprintf(output, " -l log_dir Log file is \"log_dir/radius.log\" (not used in debug mode)\n");
754 fprintf(output, " -p port Listen on port ONLY\n");
755 fprintf(output, " -s Do not spawn child processes to handle requests.\n");
756 fprintf(output, " -S Log stripped names.\n");
757 fprintf(output, " -v Print server version information.\n");
758 fprintf(output, " -X Turn on full debugging.\n");
759 fprintf(output, " -x Turn on additional debugging. (-xx gives more debugging).\n");
760 fprintf(output, " -y Log authentication failures, with password.\n");
761 fprintf(output, " -z Log authentication successes, with password.\n");
767 * We got a fatal signal.
769 static void sig_fatal(int sig)
773 /* We can't really do anything intelligent here so just die */
786 * We got the hangup signal.
787 * Re-read the configuration files.
790 static void sig_hup(int sig)
792 sig = sig; /* -Wunused */
794 reset_signal(SIGHUP, sig_hup);
797 * Only do the reload if we're the main server, both
798 * for processes, and for threads.
800 if (getpid() == radius_pid) {
804 if (mainconfig.do_snmp) {
805 rad_snmp.smux_failures = 0;
806 rad_snmp.smux_event = SMUX_CONNECT;