2 * $Id: main.c,v 1.24 2009-02-27 09:14:40 franklahm Exp $
4 * Copyright (c) 1990,1993 Regents of The University of Michigan.
5 * All Rights Reserved. See COPYRIGHT.
10 #endif /* HAVE_CONFIG_H */
17 #include <sys/param.h>
19 #include <atalk/logger.h>
21 #include <sys/socket.h>
25 #include <atalk/adouble.h>
27 #include <netatalk/at.h>
28 #include <atalk/compat.h>
29 #include <atalk/dsi.h>
30 #include <atalk/atp.h>
31 #include <atalk/asp.h>
32 #include <atalk/afp.h>
33 #include <atalk/paths.h>
34 #include <atalk/util.h>
35 #include <atalk/server_child.h>
36 #include <atalk/server_ipc.h>
39 #include "afp_config.h"
45 #include <sys/security.h>
50 static char **argv = NULL;
53 unsigned char nologin = 0;
55 struct afp_options default_options;
56 static AFPConfig *configs;
57 static server_child *server_children;
58 static fd_set save_rfds;
59 static int Ipc_fd = -1;
62 void afp_get_cmdline( int *ac, char ***av)
69 static void afp_exit(const int i)
71 server_unlock(default_options.pidfile);
76 initialize fd set we are waiting for.
78 static void set_fd(int ipc_fd)
83 for (config = configs; config; config = config->next) {
84 if (config->fd < 0) /* for proxies */
86 FD_SET(config->fd, &save_rfds);
89 FD_SET(ipc_fd, &save_rfds);
93 /* ------------------ */
94 static void afp_goaway(int sig)
104 LOG(log_info, logtype_afpd, "shutting down on signal %d", sig );
108 /* w/ a configuration file, we can force a re-read if we want */
111 if (sig == SIGHUP || ((nologin + 1) & 1)) {
114 LOG(log_info, logtype_afpd, "re-reading configuration file");
115 for (config = configs; config; config = config->next)
116 if (config->server_cleanup)
117 config->server_cleanup(config);
119 /* configfree close atp socket used for DDP tickle, there's an issue
122 configfree(configs, NULL);
123 if (!(configs = configinit(&default_options))) {
124 LOG(log_error, logtype_afpd, "config re-read: no servers configured");
125 afp_exit(EXITERR_CONF);
129 LOG(log_info, logtype_afpd, "disallowing logins");
136 LOG(log_error, logtype_afpd, "afp_goaway: bad signal" );
138 if ( sig == SIGTERM ) {
141 for (config = configs; config; config = config->next)
142 if (config->server_cleanup)
143 config->server_cleanup(config);
150 static void child_handler()
152 server_child_handler(server_children);
169 set_auth_parameters( ac, av );
175 afp_options_init(&default_options);
176 if (!afp_options_parse(ac, av, &default_options))
179 /* Save the user's current umask for use with CNID (and maybe some
180 * other things, too). */
181 default_options.save_mask = umask( default_options.umask );
183 switch(server_lock("afpd", default_options.pidfile,
184 default_options.flags & OPTION_DEBUG)) {
189 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) );
204 afp_exit(EXITERR_SYS);
208 /* linux at least up to 2.4.22 send a SIGXFZ for vfat fs,
209 even if the file is open with O_LARGEFILE ! */
211 signal(SIGXFSZ , SIG_IGN);
215 memset(&sv, 0, sizeof(sv));
216 sv.sa_handler = child_handler;
217 sigemptyset( &sv.sa_mask );
218 sigaddset(&sv.sa_mask, SIGALRM);
219 sigaddset(&sv.sa_mask, SIGHUP);
220 sigaddset(&sv.sa_mask, SIGTERM);
221 sigaddset(&sv.sa_mask, SIGUSR1);
223 sv.sa_flags = SA_RESTART;
224 if ( sigaction( SIGCHLD, &sv, 0 ) < 0 ) {
225 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
226 afp_exit(EXITERR_SYS);
229 sv.sa_handler = afp_goaway;
230 sigemptyset( &sv.sa_mask );
231 sigaddset(&sv.sa_mask, SIGALRM);
232 sigaddset(&sv.sa_mask, SIGTERM);
233 sigaddset(&sv.sa_mask, SIGHUP);
234 sigaddset(&sv.sa_mask, SIGCHLD);
235 sv.sa_flags = SA_RESTART;
236 if ( sigaction( SIGUSR1, &sv, 0 ) < 0 ) {
237 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
238 afp_exit(EXITERR_SYS);
241 sigemptyset( &sv.sa_mask );
242 sigaddset(&sv.sa_mask, SIGALRM);
243 sigaddset(&sv.sa_mask, SIGTERM);
244 sigaddset(&sv.sa_mask, SIGUSR1);
245 sigaddset(&sv.sa_mask, SIGCHLD);
246 sv.sa_flags = SA_RESTART;
247 if ( sigaction( SIGHUP, &sv, 0 ) < 0 ) {
248 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
249 afp_exit(EXITERR_SYS);
253 sigemptyset( &sv.sa_mask );
254 sigaddset(&sv.sa_mask, SIGALRM);
255 sigaddset(&sv.sa_mask, SIGHUP);
256 sigaddset(&sv.sa_mask, SIGUSR1);
257 sigaddset(&sv.sa_mask, SIGCHLD);
258 sv.sa_flags = SA_RESTART;
259 if ( sigaction( SIGTERM, &sv, 0 ) < 0 ) {
260 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
261 afp_exit(EXITERR_SYS);
264 /* afpd.conf: not in config file: lockfile, connections, configfile
265 * preference: command-line provides defaults.
266 * config file over-writes defaults.
268 * we also need to make sure that killing afpd during startup
269 * won't leave any lingering registered names around.
273 sigaddset(&sigs, SIGALRM);
274 sigaddset(&sigs, SIGHUP);
275 sigaddset(&sigs, SIGUSR1);
277 /* don't block SIGTERM */
278 sigaddset(&sigs, SIGTERM);
280 sigaddset(&sigs, SIGCHLD);
282 sigprocmask(SIG_BLOCK, &sigs, NULL);
283 if (!(configs = configinit(&default_options))) {
284 LOG(log_error, logtype_afpd, "main: no servers configured");
285 afp_exit(EXITERR_CONF);
287 sigprocmask(SIG_UNBLOCK, &sigs, NULL);
292 /* watch atp, dsi sockets and ipc parent/child file descriptor. */
293 if ((ipc = server_ipc_create())) {
294 Ipc_fd = server_ipc_parent(ipc);
298 /* wait for an appleshare connection. parent remains in the loop
299 * while the children get handled by afp_over_{asp,dsi}. this is
300 * currently vulnerable to a denial-of-service attack if a
301 * connection is made without an actual login attempt being made
302 * afterwards. establishing timeouts for logins is a possible
306 sigprocmask(SIG_UNBLOCK, &sigs, NULL);
307 ret = select(FD_SETSIZE, &rfds, NULL, NULL, NULL);
308 sigprocmask(SIG_BLOCK, &sigs, NULL);
312 LOG(log_error, logtype_afpd, "main: can't wait for input: %s", strerror(errno));
315 if (Ipc_fd >=0 && FD_ISSET(Ipc_fd, &rfds)) {
316 server_ipc_read(server_children);
318 for (config = configs; config; config = config->next) {
321 if (FD_ISSET(config->fd, &rfds)) {
322 config->server_start(config, configs, server_children);