2 * Copyright (c) 1990,1993 Regents of The University of Michigan.
3 * All Rights Reserved. See COPYRIGHT.
8 #endif /* HAVE_CONFIG_H */
15 #include <sys/param.h>
17 #include <atalk/logger.h>
19 #include <sys/socket.h>
23 #include <sys/resource.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>
37 #include <atalk/globals.h>
39 #include "afp_config.h"
43 #include "afp_zeroconf.h"
46 #include <sys/security.h>
51 static char **argv = NULL;
54 #define AFP_LISTENERS 32
55 #define FDSET_SAFETY 5
57 unsigned char nologin = 0;
58 struct afp_options default_options;
59 static AFPConfig *configs;
60 static server_child *server_children;
61 static sig_atomic_t reloadconfig = 0;
62 static sig_atomic_t gotsigchld = 0;
64 /* Two pointers to dynamic allocated arrays which store pollfds and associated data */
65 static struct pollfd *fdset;
66 static struct polldata *polldata;
67 static int fdset_size; /* current allocated size */
68 static int fdset_used; /* number of used elements */
69 static int disasociated_ipc_fd; /* disasociated sessions uses this fd for IPC */
72 void afp_get_cmdline( int *ac, char ***av)
79 /* This is registered with atexit() */
80 static void afp_exit(void)
82 if (parent_or_child == 0)
83 /* Only do this in the parent */
84 server_unlock(default_options.pidfile);
89 initialize fd set we are waiting for.
91 static void fd_set_listening_sockets(void)
95 for (config = configs; config; config = config->next) {
96 if (config->fd < 0) /* for proxies */
98 fdset_add_fd(default_options.connections + AFP_LISTENERS + FDSET_SAFETY,
108 if (default_options.flags & OPTION_KEEPSESSIONS)
109 fdset_add_fd(default_options.connections + AFP_LISTENERS + FDSET_SAFETY,
119 static void fd_reset_listening_sockets(void)
123 for (config = configs; config; config = config->next) {
124 if (config->fd < 0) /* for proxies */
126 fdset_del_fd(&fdset, &polldata, &fdset_used, &fdset_size, config->fd);
129 if (default_options.flags & OPTION_KEEPSESSIONS)
130 fdset_del_fd(&fdset, &polldata, &fdset_used, &fdset_size, disasociated_ipc_fd);
133 /* ------------------ */
134 static void afp_goaway(int sig)
140 #endif /* ! NO_DDP */
148 LOG(log_note, logtype_afpd, "AFP Server shutting down on SIGTERM");
151 if (default_options.flags & OPTION_KEEPSESSIONS) {
152 LOG(log_note, logtype_afpd, "AFP Server shutting down on SIGQUIT, NOT disconnecting clients");
154 LOG(log_note, logtype_afpd, "AFP Server shutting down on SIGQUIT");
160 server_child_kill(server_children, CHILD_DSIFORK, sig);
162 for (config = configs; config; config = config->next)
163 if (config->server_cleanup)
164 config->server_cleanup(config);
165 server_unlock(default_options.pidfile);
172 LOG(log_info, logtype_afpd, "disallowing logins");
175 server_child_kill(server_children, CHILD_DSIFORK, sig);
179 /* w/ a configuration file, we can force a re-read if we want */
184 /* w/ a configuration file, we can force a re-read if we want */
189 LOG(log_error, logtype_afpd, "afp_goaway: bad signal" );
194 static void child_handler(void)
201 #define WAIT_ANY (-1)
202 #endif /* ! WAIT_ANY */
204 while ((pid = waitpid(WAIT_ANY, &status, WNOHANG)) > 0) {
205 for (i = 0; i < server_children->nforks; i++) {
206 if ((fd = server_child_remove(server_children, i, pid)) != -1) {
207 fdset_del_fd(&fdset, &polldata, &fdset_used, &fdset_size, fd);
212 if (WIFEXITED(status)) {
213 if (WEXITSTATUS(status))
214 LOG(log_info, logtype_afpd, "child[%d]: exited %d", pid, WEXITSTATUS(status));
216 LOG(log_info, logtype_afpd, "child[%d]: done", pid);
218 if (WIFSIGNALED(status))
219 LOG(log_info, logtype_afpd, "child[%d]: killed by signal %d", pid, WTERMSIG(status));
221 LOG(log_info, logtype_afpd, "child[%d]: died", pid);
226 static int setlimits(void)
230 if (getrlimit(RLIMIT_NOFILE, &rlim) != 0) {
231 LOG(log_error, logtype_afpd, "setlimits: %s", strerror(errno));
234 if (rlim.rlim_cur != RLIM_INFINITY && rlim.rlim_cur < 65535) {
235 rlim.rlim_cur = 65535;
236 if (rlim.rlim_max != RLIM_INFINITY && rlim.rlim_max < 65535)
237 rlim.rlim_max = 65535;
238 if (setrlimit(RLIMIT_NOFILE, &rlim) != 0) {
239 LOG(log_error, logtype_afpd, "setlimits: %s", strerror(errno));
246 int main(int ac, char **av)
258 set_auth_parameters( ac, av );
261 /* Parse argv args and initialize default options */
262 afp_options_init(&default_options);
263 if (!afp_options_parse(ac, av, &default_options))
266 if (!(default_options.flags & OPTION_DEBUG) && (daemonize(0, 0) != 0))
269 switch(check_lockfile("afpd", default_options.pidfile)) {
276 /* Log SIGBUS/SIGSEGV SBT */
279 /* Default log setup: log to syslog */
280 set_processname("afpd");
281 setuplog("default log_note");
283 /* Save the user's current umask */
284 default_options.save_mask = umask( default_options.umask );
288 /* install child handler for asp and dsi. we do this before afp_goaway
289 * as afp_goaway references stuff from here.
290 * XXX: this should really be setup after the initial connections. */
291 if (!(server_children = server_child_alloc(default_options.connections,
293 LOG(log_error, logtype_afpd, "main: server_child alloc: %s", strerror(errno) );
297 memset(&sv, 0, sizeof(sv));
298 /* linux at least up to 2.4.22 send a SIGXFZ for vfat fs,
299 even if the file is open with O_LARGEFILE ! */
301 sv.sa_handler = SIG_IGN;
302 sigemptyset( &sv.sa_mask );
303 if (sigaction(SIGXFSZ, &sv, NULL ) < 0 ) {
304 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
309 sv.sa_handler = afp_goaway; /* handler for all sigs */
311 sigemptyset( &sv.sa_mask );
312 sigaddset(&sv.sa_mask, SIGALRM);
313 sigaddset(&sv.sa_mask, SIGHUP);
314 sigaddset(&sv.sa_mask, SIGTERM);
315 sigaddset(&sv.sa_mask, SIGUSR1);
316 sigaddset(&sv.sa_mask, SIGQUIT);
317 sv.sa_flags = SA_RESTART;
318 if ( sigaction( SIGCHLD, &sv, NULL ) < 0 ) {
319 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
323 sigemptyset( &sv.sa_mask );
324 sigaddset(&sv.sa_mask, SIGALRM);
325 sigaddset(&sv.sa_mask, SIGTERM);
326 sigaddset(&sv.sa_mask, SIGHUP);
327 sigaddset(&sv.sa_mask, SIGCHLD);
328 sigaddset(&sv.sa_mask, SIGQUIT);
329 sv.sa_flags = SA_RESTART;
330 if ( sigaction( SIGUSR1, &sv, NULL ) < 0 ) {
331 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
335 sigemptyset( &sv.sa_mask );
336 sigaddset(&sv.sa_mask, SIGALRM);
337 sigaddset(&sv.sa_mask, SIGTERM);
338 sigaddset(&sv.sa_mask, SIGUSR1);
339 sigaddset(&sv.sa_mask, SIGCHLD);
340 sigaddset(&sv.sa_mask, SIGQUIT);
341 sv.sa_flags = SA_RESTART;
342 if ( sigaction( SIGHUP, &sv, NULL ) < 0 ) {
343 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
348 sigemptyset( &sv.sa_mask );
349 sigaddset(&sv.sa_mask, SIGALRM);
350 sigaddset(&sv.sa_mask, SIGHUP);
351 sigaddset(&sv.sa_mask, SIGUSR1);
352 sigaddset(&sv.sa_mask, SIGCHLD);
353 sigaddset(&sv.sa_mask, SIGQUIT);
354 sv.sa_flags = SA_RESTART;
355 if ( sigaction( SIGTERM, &sv, NULL ) < 0 ) {
356 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
360 sigemptyset( &sv.sa_mask );
361 sigaddset(&sv.sa_mask, SIGALRM);
362 sigaddset(&sv.sa_mask, SIGHUP);
363 sigaddset(&sv.sa_mask, SIGUSR1);
364 sigaddset(&sv.sa_mask, SIGCHLD);
365 sigaddset(&sv.sa_mask, SIGTERM);
366 sv.sa_flags = SA_RESTART;
367 if (sigaction(SIGQUIT, &sv, NULL ) < 0 ) {
368 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
372 /* afpd.conf: not in config file: lockfile, connections, configfile
373 * preference: command-line provides defaults.
374 * config file over-writes defaults.
376 * we also need to make sure that killing afpd during startup
377 * won't leave any lingering registered names around.
381 sigaddset(&sigs, SIGALRM);
382 sigaddset(&sigs, SIGHUP);
383 sigaddset(&sigs, SIGUSR1);
385 /* don't block SIGTERM */
386 sigaddset(&sigs, SIGTERM);
388 sigaddset(&sigs, SIGCHLD);
390 pthread_sigmask(SIG_BLOCK, &sigs, NULL);
391 if (!(configs = configinit(&default_options))) {
392 LOG(log_error, logtype_afpd, "main: no servers configured");
395 pthread_sigmask(SIG_UNBLOCK, &sigs, NULL);
400 /* watch atp, dsi sockets and ipc parent/child file descriptor. */
402 if (default_options.flags & OPTION_KEEPSESSIONS) {
403 LOG(log_note, logtype_afpd, "Activating continous service");
404 disasociated_ipc_fd = ipc_server_uds(_PATH_AFP_IPC);
407 fd_set_listening_sockets();
413 int fd[2]; /* we only use one, but server_child_add expects [2] */
417 /* wait for an appleshare connection. parent remains in the loop
418 * while the children get handled by afp_over_{asp,dsi}. this is
419 * currently vulnerable to a denial-of-service attack if a
420 * connection is made without an actual login attempt being made
421 * afterwards. establishing timeouts for logins is a possible
424 LOG(log_maxdebug, logtype_afpd, "main: polling %i fds", fdset_used);
425 pthread_sigmask(SIG_UNBLOCK, &sigs, NULL);
426 ret = poll(fdset, fdset_used, -1);
427 pthread_sigmask(SIG_BLOCK, &sigs, NULL);
439 fd_reset_listening_sockets();
441 LOG(log_info, logtype_afpd, "re-reading configuration file");
442 for (config = configs; config; config = config->next)
443 if (config->server_cleanup)
444 config->server_cleanup(config);
446 /* configfree close atp socket used for DDP tickle, there's an issue
448 configfree(configs, NULL);
449 if (!(configs = configinit(&default_options))) {
450 LOG(log_error, logtype_afpd, "config re-read: no servers configured");
454 fd_set_listening_sockets();
468 LOG(log_error, logtype_afpd, "main: can't wait for input: %s", strerror(errno));
472 for (int i = 0; i < fdset_used; i++) {
473 if (fdset[i].revents & (POLLIN | POLLERR | POLLHUP)) {
474 switch (polldata[i].fdtype) {
477 config = (AFPConfig *)polldata[i].data;
478 /* config->server_start is afp_config.c:dsi_start() for DSI */
479 if (child = config->server_start(config, configs, server_children)) {
480 /* Add IPC fd to select fd set */
481 fdset_add_fd(default_options.connections + AFP_LISTENERS + FDSET_SAFETY,
493 child = (afp_child_t *)polldata[i].data;
494 LOG(log_debug, logtype_afpd, "main: IPC request from child[%u]", child->pid);
496 if (ipc_server_read(server_children, child->ipc_fds[0]) != 0) {
497 fdset_del_fd(&fdset, &polldata, &fdset_used, &fdset_size, child->ipc_fds[0]);
498 close(child->ipc_fds[0]);
499 child->ipc_fds[0] = -1;
500 if ((default_options.flags & OPTION_KEEPSESSIONS) && child->disasociated) {
501 LOG(log_note, logtype_afpd, "main: removing reattached child[%u]", child->pid);
502 server_child_remove(server_children, CHILD_DSIFORK, child->pid);
507 case DISASOCIATED_IPC_FD:
508 LOG(log_debug, logtype_afpd, "main: IPC reconnect request");
509 if ((fd[0] = accept(disasociated_ipc_fd, NULL, NULL)) == -1) {
510 LOG(log_error, logtype_afpd, "main: accept: %s", strerror(errno));
513 if (readt(fd[0], &pid, sizeof(pid_t), 0, 1) != sizeof(pid_t)) {
514 LOG(log_error, logtype_afpd, "main: readt: %s", strerror(errno));
518 LOG(log_note, logtype_afpd, "main: IPC reconnect from pid [%u]", pid);
519 if ((child = server_child_add(server_children, CHILD_DSIFORK, pid, fd)) == NULL) {
520 LOG(log_error, logtype_afpd, "main: server_child_add");
524 child->disasociated = 1;
525 fdset_add_fd(default_options.connections + AFP_LISTENERS + FDSET_SAFETY,
536 LOG(log_debug, logtype_afpd, "main: IPC request for unknown type");