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"
43 #include "afp_zeroconf.h"
46 #include <sys/security.h>
51 static char **argv = NULL;
54 unsigned char nologin = 0;
56 struct afp_options default_options;
57 static AFPConfig *configs;
58 static server_child *server_children;
59 static fd_set save_rfds;
60 static int Ipc_fd = -1;
61 static sig_atomic_t reloadconfig = 0;
64 void afp_get_cmdline( int *ac, char ***av)
71 static void afp_exit(const int i)
73 server_unlock(default_options.pidfile);
78 initialize fd set we are waiting for.
80 static void set_fd(int ipc_fd)
85 for (config = configs; config; config = config->next) {
86 if (config->fd < 0) /* for proxies */
88 FD_SET(config->fd, &save_rfds);
91 FD_SET(ipc_fd, &save_rfds);
95 /* ------------------ */
96 static void afp_goaway(int sig)
101 #endif /* ! NO_DDP */
107 LOG(log_info, logtype_afpd, "shutting down on signal %d", sig );
109 for (config = configs; config; config = config->next)
110 if (config->server_cleanup)
111 config->server_cleanup(config);
118 LOG(log_info, logtype_afpd, "disallowing logins");
122 /* w/ a configuration file, we can force a re-read if we want */
127 LOG(log_error, logtype_afpd, "afp_goaway: bad signal" );
132 static void child_handler(int sig _U_)
134 server_child_handler(server_children);
137 int main(int ac, char **av)
149 set_auth_parameters( ac, av );
155 afp_options_init(&default_options);
156 if (!afp_options_parse(ac, av, &default_options))
159 /* Save the user's current umask for use with CNID (and maybe some
160 * other things, too). */
161 default_options.save_mask = umask( default_options.umask );
163 switch(server_lock("afpd", default_options.pidfile,
164 default_options.flags & OPTION_DEBUG)) {
169 default: /* server */
173 /* install child handler for asp and dsi. we do this before afp_goaway
174 * as afp_goaway references stuff from here.
175 * XXX: this should really be setup after the initial connections. */
176 if (!(server_children = server_child_alloc(default_options.connections,
178 LOG(log_error, logtype_afpd, "main: server_child alloc: %s", strerror(errno) );
179 afp_exit(EXITERR_SYS);
183 /* linux at least up to 2.4.22 send a SIGXFZ for vfat fs,
184 even if the file is open with O_LARGEFILE ! */
186 signal(SIGXFSZ , SIG_IGN);
190 memset(&sv, 0, sizeof(sv));
191 sv.sa_handler = child_handler;
192 sigemptyset( &sv.sa_mask );
193 sigaddset(&sv.sa_mask, SIGALRM);
194 sigaddset(&sv.sa_mask, SIGHUP);
195 sigaddset(&sv.sa_mask, SIGTERM);
196 sigaddset(&sv.sa_mask, SIGUSR1);
198 sv.sa_flags = SA_RESTART;
199 if ( sigaction( SIGCHLD, &sv, NULL ) < 0 ) {
200 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
201 afp_exit(EXITERR_SYS);
204 sv.sa_handler = afp_goaway;
205 sigemptyset( &sv.sa_mask );
206 sigaddset(&sv.sa_mask, SIGALRM);
207 sigaddset(&sv.sa_mask, SIGTERM);
208 sigaddset(&sv.sa_mask, SIGHUP);
209 sigaddset(&sv.sa_mask, SIGCHLD);
210 sv.sa_flags = SA_RESTART;
211 if ( sigaction( SIGUSR1, &sv, NULL ) < 0 ) {
212 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
213 afp_exit(EXITERR_SYS);
216 sigemptyset( &sv.sa_mask );
217 sigaddset(&sv.sa_mask, SIGALRM);
218 sigaddset(&sv.sa_mask, SIGTERM);
219 sigaddset(&sv.sa_mask, SIGUSR1);
220 sigaddset(&sv.sa_mask, SIGCHLD);
221 sv.sa_flags = SA_RESTART;
222 if ( sigaction( SIGHUP, &sv, NULL ) < 0 ) {
223 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
224 afp_exit(EXITERR_SYS);
228 sigemptyset( &sv.sa_mask );
229 sigaddset(&sv.sa_mask, SIGALRM);
230 sigaddset(&sv.sa_mask, SIGHUP);
231 sigaddset(&sv.sa_mask, SIGUSR1);
232 sigaddset(&sv.sa_mask, SIGCHLD);
233 sv.sa_flags = SA_RESTART;
234 if ( sigaction( SIGTERM, &sv, NULL ) < 0 ) {
235 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
236 afp_exit(EXITERR_SYS);
239 /* afpd.conf: not in config file: lockfile, connections, configfile
240 * preference: command-line provides defaults.
241 * config file over-writes defaults.
243 * we also need to make sure that killing afpd during startup
244 * won't leave any lingering registered names around.
248 sigaddset(&sigs, SIGALRM);
249 sigaddset(&sigs, SIGHUP);
250 sigaddset(&sigs, SIGUSR1);
252 /* don't block SIGTERM */
253 sigaddset(&sigs, SIGTERM);
255 sigaddset(&sigs, SIGCHLD);
257 pthread_sigmask(SIG_BLOCK, &sigs, NULL);
258 if (!(configs = configinit(&default_options))) {
259 LOG(log_error, logtype_afpd, "main: no servers configured");
260 afp_exit(EXITERR_CONF);
262 pthread_sigmask(SIG_UNBLOCK, &sigs, NULL);
267 /* watch atp, dsi sockets and ipc parent/child file descriptor. */
268 if ((ipc = server_ipc_create())) {
269 Ipc_fd = server_ipc_parent(ipc);
273 /* wait for an appleshare connection. parent remains in the loop
274 * while the children get handled by afp_over_{asp,dsi}. this is
275 * currently vulnerable to a denial-of-service attack if a
276 * connection is made without an actual login attempt being made
277 * afterwards. establishing timeouts for logins is a possible
281 pthread_sigmask(SIG_UNBLOCK, &sigs, NULL);
282 ret = select(FD_SETSIZE, &rfds, NULL, NULL, NULL);
283 pthread_sigmask(SIG_BLOCK, &sigs, NULL);
284 int saveerrno = errno;
291 LOG(log_info, logtype_afpd, "re-reading configuration file");
292 for (config = configs; config; config = config->next)
293 if (config->server_cleanup)
294 config->server_cleanup(config);
296 /* configfree close atp socket used for DDP tickle, there's an issue
298 configfree(configs, NULL);
299 if (!(configs = configinit(&default_options))) {
300 LOG(log_error, logtype_afpd, "config re-read: no servers configured");
301 afp_exit(EXITERR_CONF);
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);