2 * $Id: main.c,v 1.20 2002-10-04 15:15:05 srittau 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 <atalk/logger.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;
60 unsigned char nologin = 0;
62 struct afp_options default_options;
63 static AFPConfig *configs;
64 static server_child *server_children;
65 static fd_set save_rfds;
68 void afp_get_cmdline( int *ac, char ***av)
75 static void afp_exit(const int i)
77 server_unlock(default_options.pidfile);
81 static void afp_goaway(int sig)
89 LOG(log_info, logtype_afpd, "shutting down on signal %d", sig );
92 /* w/ a configuration file, we can force a re-read if we want */
94 if ((nologin + 1) & 1) {
97 LOG(log_info, logtype_afpd, "re-reading configuration file");
98 for (config = configs; config; config = config->next)
99 if (config->server_cleanup)
100 config->server_cleanup(config);
102 configfree(configs, NULL);
103 if (!(configs = configinit(&default_options))) {
104 LOG(log_error, logtype_afpd, "config re-read: no servers configured");
108 for (config = configs; config; config = config->next) {
111 FD_SET(config->fd, &save_rfds);
114 LOG(log_info, logtype_afpd, "disallowing logins");
119 LOG(log_error, logtype_afpd, "afp_goaway: bad signal" );
121 if ( sig == SIGTERM ) {
124 for (config = configs; config; config = config->next)
125 if (config->server_cleanup)
126 config->server_cleanup(config);
133 static void child_handler()
135 server_child_handler(server_children);
150 set_auth_parameters( ac, av );
153 afp_options_init(&default_options);
154 if (!afp_options_parse(ac, av, &default_options))
157 /* Save the user's current umask for use with CNID (and maybe some
158 * other things, too). */
159 default_options.save_mask = umask( default_options.umask );
161 switch(server_lock("afpd", default_options.pidfile,
162 default_options.flags & OPTION_DEBUG)) {
167 default: /* server */
171 /* install child handler for asp and dsi. we do this before afp_goaway
172 * as afp_goaway references stuff from here.
173 * XXX: this should really be setup after the initial connections. */
174 if (!(server_children = server_child_alloc(default_options.connections,
176 LOG(log_error, logtype_afpd, "main: server_child alloc: %s", strerror(errno) );
180 memset(&sv, 0, sizeof(sv));
181 sv.sa_handler = child_handler;
182 sigemptyset( &sv.sa_mask );
183 sv.sa_flags = SA_RESTART;
184 if ( sigaction( SIGCHLD, &sv, 0 ) < 0 ) {
185 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
189 sv.sa_handler = afp_goaway;
190 sigemptyset( &sv.sa_mask );
191 sigaddset(&sv.sa_mask, SIGHUP);
192 sigaddset(&sv.sa_mask, SIGTERM);
193 sv.sa_flags = SA_RESTART;
194 if ( sigaction( SIGHUP, &sv, 0 ) < 0 ) {
195 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
198 if ( sigaction( SIGTERM, &sv, 0 ) < 0 ) {
199 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
203 /* afpd.conf: not in config file: lockfile, connections, configfile
204 * preference: command-line provides defaults.
205 * config file over-writes defaults.
207 * we also need to make sure that killing afpd during startup
208 * won't leave any lingering registered names around.
212 sigaddset(&sigs, SIGHUP);
213 sigaddset(&sigs, SIGTERM);
214 sigprocmask(SIG_BLOCK, &sigs, NULL);
215 if (!(configs = configinit(&default_options))) {
216 LOG(log_error, logtype_afpd, "main: no servers configured: %s\n", strerror(errno));
219 sigprocmask(SIG_UNBLOCK, &sigs, NULL);
221 /* watch atp and dsi sockets. */
223 for (config = configs; config; config = config->next) {
224 if (config->fd < 0) /* for proxies */
226 FD_SET(config->fd, &save_rfds);
229 /* wait for an appleshare connection. parent remains in the loop
230 * while the children get handled by afp_over_{asp,dsi}. this is
231 * currently vulnerable to a denial-of-service attack if a
232 * connection is made without an actual login attempt being made
233 * afterwards. establishing timeouts for logins is a possible
237 if (select(FD_SETSIZE, &rfds, NULL, NULL, NULL) < 0) {
240 LOG(log_error, logtype_afpd, "main: can't wait for input: %s", strerror(errno));
244 for (config = configs; config; config = config->next) {
247 if (FD_ISSET(config->fd, &rfds))
248 config->server_start(config, configs, server_children);