2 * $Id: main.c,v 1.20.4.2.2.1 2003-09-09 16:42:20 didg 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>
44 #include <atalk/server_ipc.h>
47 #include "afp_config.h"
53 #include <sys/security.h>
58 static char **argv = NULL;
61 unsigned char nologin = 0;
63 struct afp_options default_options;
64 static AFPConfig *configs;
65 static server_child *server_children;
66 static fd_set save_rfds;
67 static int Ipc_fd = -1;
70 void afp_get_cmdline( int *ac, char ***av)
77 static void afp_exit(const int i)
79 server_unlock(default_options.pidfile);
84 initialize fd set we are waiting for.
86 static void set_fd(int ipc_fd)
91 for (config = configs; config; config = config->next) {
92 if (config->fd < 0) /* for proxies */
94 FD_SET(config->fd, &save_rfds);
97 FD_SET(ipc_fd, &save_rfds);
101 /* ------------------ */
102 static void afp_goaway(int sig)
107 #endif /* ! NO_DDP */
112 LOG(log_info, logtype_afpd, "shutting down on signal %d", sig );
116 /* w/ a configuration file, we can force a re-read if we want */
118 if (sig == SIGHUP || ((nologin + 1) & 1)) {
121 LOG(log_info, logtype_afpd, "re-reading configuration file");
122 for (config = configs; config; config = config->next)
123 if (config->server_cleanup)
124 config->server_cleanup(config);
126 configfree(configs, NULL);
127 if (!(configs = configinit(&default_options))) {
128 LOG(log_error, logtype_afpd, "config re-read: no servers configured");
133 LOG(log_info, logtype_afpd, "disallowing logins");
141 LOG(log_error, logtype_afpd, "afp_goaway: bad signal" );
143 if ( sig == SIGTERM ) {
146 for (config = configs; config; config = config->next)
147 if (config->server_cleanup)
148 config->server_cleanup(config);
155 static void child_handler()
157 server_child_handler(server_children);
173 set_auth_parameters( ac, av );
177 afp_options_init(&default_options);
178 if (!afp_options_parse(ac, av, &default_options))
181 /* Save the user's current umask for use with CNID (and maybe some
182 * other things, too). */
183 default_options.save_mask = umask( default_options.umask );
185 switch(server_lock("afpd", default_options.pidfile,
186 default_options.flags & OPTION_DEBUG)) {
191 default: /* server */
198 /* install child handler for asp and dsi. we do this before afp_goaway
199 * as afp_goaway references stuff from here.
200 * XXX: this should really be setup after the initial connections. */
201 if (!(server_children = server_child_alloc(default_options.connections,
203 LOG(log_error, logtype_afpd, "main: server_child alloc: %s", strerror(errno) );
207 memset(&sv, 0, sizeof(sv));
208 sv.sa_handler = child_handler;
209 sigemptyset( &sv.sa_mask );
210 sv.sa_flags = SA_RESTART;
211 if ( sigaction( SIGCHLD, &sv, 0 ) < 0 ) {
212 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
216 sv.sa_handler = afp_goaway;
217 sigemptyset( &sv.sa_mask );
218 sigaddset(&sv.sa_mask, SIGHUP);
219 sigaddset(&sv.sa_mask, SIGTERM);
220 sigaddset(&sv.sa_mask, SIGUSR1);
221 sv.sa_flags = SA_RESTART;
222 if ( sigaction( SIGUSR1, &sv, 0 ) < 0 ) {
223 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
226 if ( sigaction( SIGHUP, &sv, 0 ) < 0 ) {
227 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
230 if ( sigaction( SIGTERM, &sv, 0 ) < 0 ) {
231 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
235 /* afpd.conf: not in config file: lockfile, connections, configfile
236 * preference: command-line provides defaults.
237 * config file over-writes defaults.
239 * we also need to make sure that killing afpd during startup
240 * won't leave any lingering registered names around.
244 sigaddset(&sigs, SIGHUP);
245 sigaddset(&sigs, SIGTERM);
246 sigprocmask(SIG_BLOCK, &sigs, NULL);
247 if (!(configs = configinit(&default_options))) {
248 LOG(log_error, logtype_afpd, "main: no servers configured: %s\n", strerror(errno));
251 sigprocmask(SIG_UNBLOCK, &sigs, NULL);
253 /* watch atp, dsi sockets and ipc parent/child file descriptor. */
254 if ((ipc = server_ipc_create())) {
255 Ipc_fd = server_ipc_parent(ipc);
259 /* wait for an appleshare connection. parent remains in the loop
260 * while the children get handled by afp_over_{asp,dsi}. this is
261 * currently vulnerable to a denial-of-service attack if a
262 * connection is made without an actual login attempt being made
263 * afterwards. establishing timeouts for logins is a possible
267 if (select(FD_SETSIZE, &rfds, NULL, NULL, NULL) < 0) {
270 LOG(log_error, logtype_afpd, "main: can't wait for input: %s", strerror(errno));
273 if (Ipc_fd >=0 && FD_ISSET(Ipc_fd, &rfds)) {
274 server_ipc_read(server_children);
276 for (config = configs; config; config = config->next) {
279 if (FD_ISSET(config->fd, &rfds))
280 config->server_start(config, configs, server_children);