2 * $Id: main.c,v 1.8 2001-06-07 17:18:26 rufustfirefly Exp $
4 * Copyright (c) 1990,1993 Regents of The University of Michigan.
5 * All Rights Reserved. See COPYRIGHT.
19 #include <sys/types.h>
21 #include <sys/param.h>
23 #include <sys/syslog.h>
25 #include <sys/socket.h>
29 #include <netatalk/endian.h>
30 #include <netatalk/at.h>
31 #include <atalk/compat.h>
32 #include <atalk/dsi.h>
33 #include <atalk/atp.h>
34 #include <atalk/asp.h>
35 #include <atalk/afp.h>
36 #include <atalk/adouble.h>
37 #include <atalk/paths.h>
38 #include <atalk/util.h>
39 #include <atalk/server_child.h>
42 #include "afp_config.h"
48 #include <sys/security.h>
54 #include "parse_mtab.h"
57 unsigned char nologin = 0;
59 struct afp_options default_options;
60 static AFPConfig *configs;
61 static server_child *server_children;
62 static fd_set save_rfds;
64 static void afp_exit(const int i)
66 server_unlock(default_options.pidfile);
70 static void afp_goaway(int sig)
78 syslog( LOG_INFO, "shutting down on signal %d", sig );
81 /* w/ a configuration file, we can force a re-read if we want */
83 if ((nologin + 1) & 1) {
86 syslog(LOG_INFO, "re-reading configuration file");
87 for (config = configs; config; config = config->next)
88 if (config->server_cleanup)
89 config->server_cleanup(config);
91 configfree(configs, NULL);
92 if (!(configs = configinit(&default_options))) {
93 syslog(LOG_ERR, "config re-read: no servers configured");
97 for (config = configs; config; config = config->next) {
100 FD_SET(config->fd, &save_rfds);
103 syslog(LOG_INFO, "disallowing logins");
108 syslog( LOG_ERR, "afp_goaway: bad signal" );
110 if ( sig == SIGTERM ) {
113 for (config = configs; config; config = config->next)
114 if (config->server_cleanup)
115 config->server_cleanup(config);
122 static void child_handler()
124 server_child_handler(server_children);
137 set_auth_parameters( ac, av );
140 umask( 0 ); /* so inherited file permissions work right */
142 afp_options_init(&default_options);
143 if (!afp_options_parse(ac, av, &default_options))
146 switch(server_lock("afpd", default_options.pidfile,
147 default_options.flags & OPTION_DEBUG)) {
152 default: /* server */
157 /* if we are going to use afpd.mtab, load the file */
158 afpd_mount_table = afpd_mtab_parse ( AFPD_MTAB_FILE );
159 #endif /* DID_MTAB */
161 /* install child handler for asp and dsi. we do this before afp_goaway
162 * as afp_goaway references stuff from here.
163 * XXX: this should really be setup after the initial connections. */
164 if (!(server_children = server_child_alloc(default_options.connections,
166 syslog(LOG_ERR, "main: server_child alloc: %m");
170 memset(&sv, 0, sizeof(sv));
171 sv.sa_handler = child_handler;
172 sigemptyset( &sv.sa_mask );
173 sv.sa_flags = SA_RESTART;
174 if ( sigaction( SIGCHLD, &sv, 0 ) < 0 ) {
175 syslog( LOG_ERR, "main: sigaction: %m" );
179 sv.sa_handler = afp_goaway;
180 sigemptyset( &sv.sa_mask );
181 sigaddset(&sv.sa_mask, SIGHUP);
182 sigaddset(&sv.sa_mask, SIGTERM);
183 sv.sa_flags = SA_RESTART;
184 if ( sigaction( SIGHUP, &sv, 0 ) < 0 ) {
185 syslog( LOG_ERR, "main: sigaction: %m" );
188 if ( sigaction( SIGTERM, &sv, 0 ) < 0 ) {
189 syslog( LOG_ERR, "main: sigaction: %m" );
193 /* afpd.conf: not in config file: lockfile, connections, configfile
194 * preference: command-line provides defaults.
195 * config file over-writes defaults.
197 * we also need to make sure that killing afpd during startup
198 * won't leave any lingering registered names around.
202 sigaddset(&sigs, SIGHUP);
203 sigaddset(&sigs, SIGTERM);
204 sigprocmask(SIG_BLOCK, &sigs, NULL);
205 if (!(configs = configinit(&default_options))) {
206 syslog(LOG_ERR, "main: no servers configured: %m\n");
209 sigprocmask(SIG_UNBLOCK, &sigs, NULL);
211 /* watch atp and dsi sockets. */
213 for (config = configs; config; config = config->next) {
214 if (config->fd < 0) /* for proxies */
216 FD_SET(config->fd, &save_rfds);
219 /* wait for an appleshare connection. parent remains in the loop
220 * while the children get handled by afp_over_{asp,dsi}. this is
221 * currently vulnerable to a denial-of-service attack if a
222 * connection is made without an actual login attempt being made
223 * afterwards. establishing timeouts for logins is a possible
227 if (select(FD_SETSIZE, &rfds, NULL, NULL, NULL) < 0) {
230 syslog(LOG_ERR, "main: can't wait for input: %m");
234 for (config = configs; config; config = config->next) {
237 if (FD_ISSET(config->fd, &rfds))
238 config->server_start(config, configs, server_children);