2 * $Id: main.c,v 1.10 2001-06-25 15:18:01 rufustfirefly Exp $
4 * Copyright (c) 1990,1993 Regents of The University of Michigan.
5 * All Rights Reserved. See COPYRIGHT.
10 #endif /* HAVE_CONFIG_H */
17 #endif /* HAVE_UNISTD_H */
20 #endif /* HAVE_FCNTL_H */
23 #include <sys/types.h>
25 #include <sys/param.h>
27 #include <sys/syslog.h>
29 #include <sys/socket.h>
33 #include <netatalk/endian.h>
34 #include <netatalk/at.h>
35 #include <atalk/compat.h>
36 #include <atalk/dsi.h>
37 #include <atalk/atp.h>
38 #include <atalk/asp.h>
39 #include <atalk/afp.h>
40 #include <atalk/adouble.h>
41 #include <atalk/paths.h>
42 #include <atalk/util.h>
43 #include <atalk/server_child.h>
46 #include "afp_config.h"
52 #include <sys/security.h>
57 static char **argv = NULL;
61 #include "parse_mtab.h"
64 unsigned char nologin = 0;
66 struct afp_options default_options;
67 static AFPConfig *configs;
68 static server_child *server_children;
69 static fd_set save_rfds;
72 void afp_get_cmdline( int *ac, char ***av)
79 static void afp_exit(const int i)
81 server_unlock(default_options.pidfile);
85 static void afp_goaway(int sig)
93 syslog( LOG_INFO, "shutting down on signal %d", sig );
96 /* w/ a configuration file, we can force a re-read if we want */
98 if ((nologin + 1) & 1) {
101 syslog(LOG_INFO, "re-reading configuration file");
102 for (config = configs; config; config = config->next)
103 if (config->server_cleanup)
104 config->server_cleanup(config);
106 configfree(configs, NULL);
107 if (!(configs = configinit(&default_options))) {
108 syslog(LOG_ERR, "config re-read: no servers configured");
112 for (config = configs; config; config = config->next) {
115 FD_SET(config->fd, &save_rfds);
118 syslog(LOG_INFO, "disallowing logins");
123 syslog( LOG_ERR, "afp_goaway: bad signal" );
125 if ( sig == SIGTERM ) {
128 for (config = configs; config; config = config->next)
129 if (config->server_cleanup)
130 config->server_cleanup(config);
137 static void child_handler()
139 server_child_handler(server_children);
154 set_auth_parameters( ac, av );
157 umask( 0 ); /* so inherited file permissions work right */
159 afp_options_init(&default_options);
160 if (!afp_options_parse(ac, av, &default_options))
163 switch(server_lock("afpd", default_options.pidfile,
164 default_options.flags & OPTION_DEBUG)) {
169 default: /* server */
174 /* if we are going to use afpd.mtab, load the file */
175 afpd_mount_table = afpd_mtab_parse ( AFPD_MTAB_FILE );
176 #endif /* DID_MTAB */
178 /* install child handler for asp and dsi. we do this before afp_goaway
179 * as afp_goaway references stuff from here.
180 * XXX: this should really be setup after the initial connections. */
181 if (!(server_children = server_child_alloc(default_options.connections,
183 syslog(LOG_ERR, "main: server_child alloc: %m");
187 memset(&sv, 0, sizeof(sv));
188 sv.sa_handler = child_handler;
189 sigemptyset( &sv.sa_mask );
190 sv.sa_flags = SA_RESTART;
191 if ( sigaction( SIGCHLD, &sv, 0 ) < 0 ) {
192 syslog( LOG_ERR, "main: sigaction: %m" );
196 sv.sa_handler = afp_goaway;
197 sigemptyset( &sv.sa_mask );
198 sigaddset(&sv.sa_mask, SIGHUP);
199 sigaddset(&sv.sa_mask, SIGTERM);
200 sv.sa_flags = SA_RESTART;
201 if ( sigaction( SIGHUP, &sv, 0 ) < 0 ) {
202 syslog( LOG_ERR, "main: sigaction: %m" );
205 if ( sigaction( SIGTERM, &sv, 0 ) < 0 ) {
206 syslog( LOG_ERR, "main: sigaction: %m" );
210 /* afpd.conf: not in config file: lockfile, connections, configfile
211 * preference: command-line provides defaults.
212 * config file over-writes defaults.
214 * we also need to make sure that killing afpd during startup
215 * won't leave any lingering registered names around.
219 sigaddset(&sigs, SIGHUP);
220 sigaddset(&sigs, SIGTERM);
221 sigprocmask(SIG_BLOCK, &sigs, NULL);
222 if (!(configs = configinit(&default_options))) {
223 syslog(LOG_ERR, "main: no servers configured: %m\n");
226 sigprocmask(SIG_UNBLOCK, &sigs, NULL);
228 /* watch atp and dsi sockets. */
230 for (config = configs; config; config = config->next) {
231 if (config->fd < 0) /* for proxies */
233 FD_SET(config->fd, &save_rfds);
236 /* wait for an appleshare connection. parent remains in the loop
237 * while the children get handled by afp_over_{asp,dsi}. this is
238 * currently vulnerable to a denial-of-service attack if a
239 * connection is made without an actual login attempt being made
240 * afterwards. establishing timeouts for logins is a possible
244 if (select(FD_SETSIZE, &rfds, NULL, NULL, NULL) < 0) {
247 syslog(LOG_ERR, "main: can't wait for input: %m");
251 for (config = configs; config; config = config->next) {
254 if (FD_ISSET(config->fd, &rfds))
255 config->server_start(config, configs, server_children);