2 * $Id: main.c,v 1.26 2009-10-14 02:24:05 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 #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(int sig _U_)
152 server_child_handler(server_children);
155 int main(int ac, char **av)
167 set_auth_parameters( ac, av );
173 afp_options_init(&default_options);
174 if (!afp_options_parse(ac, av, &default_options))
177 /* Save the user's current umask for use with CNID (and maybe some
178 * other things, too). */
179 default_options.save_mask = umask( default_options.umask );
181 switch(server_lock("afpd", default_options.pidfile,
182 default_options.flags & OPTION_DEBUG)) {
187 default: /* server */
196 /* install child handler for asp and dsi. we do this before afp_goaway
197 * as afp_goaway references stuff from here.
198 * XXX: this should really be setup after the initial connections. */
199 if (!(server_children = server_child_alloc(default_options.connections,
201 LOG(log_error, logtype_afpd, "main: server_child alloc: %s", strerror(errno) );
202 afp_exit(EXITERR_SYS);
206 /* linux at least up to 2.4.22 send a SIGXFZ for vfat fs,
207 even if the file is open with O_LARGEFILE ! */
209 signal(SIGXFSZ , SIG_IGN);
213 memset(&sv, 0, sizeof(sv));
214 sv.sa_handler = child_handler;
215 sigemptyset( &sv.sa_mask );
216 sigaddset(&sv.sa_mask, SIGALRM);
217 sigaddset(&sv.sa_mask, SIGHUP);
218 sigaddset(&sv.sa_mask, SIGTERM);
219 sigaddset(&sv.sa_mask, SIGUSR1);
221 sv.sa_flags = SA_RESTART;
222 if ( sigaction( SIGCHLD, &sv, NULL ) < 0 ) {
223 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
224 afp_exit(EXITERR_SYS);
227 sv.sa_handler = afp_goaway;
228 sigemptyset( &sv.sa_mask );
229 sigaddset(&sv.sa_mask, SIGALRM);
230 sigaddset(&sv.sa_mask, SIGTERM);
231 sigaddset(&sv.sa_mask, SIGHUP);
232 sigaddset(&sv.sa_mask, SIGCHLD);
233 sv.sa_flags = SA_RESTART;
234 if ( sigaction( SIGUSR1, &sv, NULL ) < 0 ) {
235 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
236 afp_exit(EXITERR_SYS);
239 sigemptyset( &sv.sa_mask );
240 sigaddset(&sv.sa_mask, SIGALRM);
241 sigaddset(&sv.sa_mask, SIGTERM);
242 sigaddset(&sv.sa_mask, SIGUSR1);
243 sigaddset(&sv.sa_mask, SIGCHLD);
244 sv.sa_flags = SA_RESTART;
245 if ( sigaction( SIGHUP, &sv, NULL ) < 0 ) {
246 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
247 afp_exit(EXITERR_SYS);
251 sigemptyset( &sv.sa_mask );
252 sigaddset(&sv.sa_mask, SIGALRM);
253 sigaddset(&sv.sa_mask, SIGHUP);
254 sigaddset(&sv.sa_mask, SIGUSR1);
255 sigaddset(&sv.sa_mask, SIGCHLD);
256 sv.sa_flags = SA_RESTART;
257 if ( sigaction( SIGTERM, &sv, NULL ) < 0 ) {
258 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
259 afp_exit(EXITERR_SYS);
262 /* afpd.conf: not in config file: lockfile, connections, configfile
263 * preference: command-line provides defaults.
264 * config file over-writes defaults.
266 * we also need to make sure that killing afpd during startup
267 * won't leave any lingering registered names around.
271 sigaddset(&sigs, SIGALRM);
272 sigaddset(&sigs, SIGHUP);
273 sigaddset(&sigs, SIGUSR1);
275 /* don't block SIGTERM */
276 sigaddset(&sigs, SIGTERM);
278 sigaddset(&sigs, SIGCHLD);
280 sigprocmask(SIG_BLOCK, &sigs, NULL);
281 if (!(configs = configinit(&default_options))) {
282 LOG(log_error, logtype_afpd, "main: no servers configured");
283 afp_exit(EXITERR_CONF);
285 sigprocmask(SIG_UNBLOCK, &sigs, NULL);
290 /* watch atp, dsi sockets and ipc parent/child file descriptor. */
291 if ((ipc = server_ipc_create())) {
292 Ipc_fd = server_ipc_parent(ipc);
296 /* wait for an appleshare connection. parent remains in the loop
297 * while the children get handled by afp_over_{asp,dsi}. this is
298 * currently vulnerable to a denial-of-service attack if a
299 * connection is made without an actual login attempt being made
300 * afterwards. establishing timeouts for logins is a possible
304 sigprocmask(SIG_UNBLOCK, &sigs, NULL);
305 ret = select(FD_SETSIZE, &rfds, NULL, NULL, NULL);
306 sigprocmask(SIG_BLOCK, &sigs, NULL);
310 LOG(log_error, logtype_afpd, "main: can't wait for input: %s", strerror(errno));
313 if (Ipc_fd >=0 && FD_ISSET(Ipc_fd, &rfds)) {
314 server_ipc_read(server_children);
316 for (config = configs; config; config = config->next) {
319 if (FD_ISSET(config->fd, &rfds)) {
320 config->server_start(config, configs, server_children);