2 * Copyright (c) 1990,1993 Regents of The University of Michigan.
3 * All Rights Reserved. See COPYRIGHT.
8 #endif /* HAVE_CONFIG_H */
14 #include <sys/param.h>
17 #include <sys/socket.h>
21 #include <sys/resource.h>
23 #include <atalk/logger.h>
24 #include <atalk/adouble.h>
25 #include <atalk/compat.h>
26 #include <atalk/dsi.h>
27 #include <atalk/afp.h>
28 #include <atalk/paths.h>
29 #include <atalk/util.h>
30 #include <atalk/server_child.h>
31 #include <atalk/server_ipc.h>
32 #include <atalk/errchk.h>
33 #include <atalk/locking.h>
34 #include <atalk/globals.h>
36 #include "event2/event.h"
37 #include "event2/http.h"
38 #include "event2/rpc.h"
40 #include "afp_config.h"
44 #include "afp_zeroconf.h"
47 #include <sys/security.h>
52 static char **argv = NULL;
55 #define AFP_LISTENERS 32
56 #define FDSET_SAFETY 5
58 unsigned char nologin = 0;
59 struct afp_options default_options;
61 static AFPConfig *configs;
62 static server_child *server_children;
63 static sig_atomic_t reloadconfig = 0;
64 static sig_atomic_t gotsigchld = 0;
66 /* Two pointers to dynamic allocated arrays which store pollfds and associated data */
67 static struct pollfd *fdset;
68 static struct polldata *polldata;
69 static int fdset_size; /* current allocated size */
70 static int fdset_used; /* number of used elements */
71 static int disasociated_ipc_fd; /* disasociated sessions uses this fd for IPC */
74 void afp_get_cmdline( int *ac, char ***av)
81 /* This is registered with atexit() */
82 static void afp_exit(void)
84 if (parent_or_child == 0)
85 /* Only do this in the parent */
86 server_unlock(default_options.pidfile);
91 initialize fd set we are waiting for.
93 static void fd_set_listening_sockets(void)
97 for (config = configs; config; config = config->next) {
98 if (config->fd < 0) /* for proxies */
100 fdset_add_fd(default_options.connections + AFP_LISTENERS + FDSET_SAFETY,
110 if (default_options.flags & OPTION_KEEPSESSIONS)
111 fdset_add_fd(default_options.connections + AFP_LISTENERS + FDSET_SAFETY,
121 static void fd_reset_listening_sockets(void)
125 for (config = configs; config; config = config->next) {
126 if (config->fd < 0) /* for proxies */
128 fdset_del_fd(&fdset, &polldata, &fdset_used, &fdset_size, config->fd);
131 if (default_options.flags & OPTION_KEEPSESSIONS)
132 fdset_del_fd(&fdset, &polldata, &fdset_used, &fdset_size, disasociated_ipc_fd);
135 /* ------------------ */
136 static void afp_goaway(int sig)
146 LOG(log_note, logtype_afpd, "AFP Server shutting down on SIGTERM");
149 if (default_options.flags & OPTION_KEEPSESSIONS) {
150 LOG(log_note, logtype_afpd, "AFP Server shutting down on SIGQUIT, NOT disconnecting clients");
152 LOG(log_note, logtype_afpd, "AFP Server shutting down on SIGQUIT");
158 server_child_kill(server_children, CHILD_DSIFORK, sig);
160 for (config = configs; config; config = config->next)
161 if (config->server_cleanup)
162 config->server_cleanup(config);
163 server_unlock(default_options.pidfile);
170 LOG(log_info, logtype_afpd, "disallowing logins");
173 server_child_kill(server_children, CHILD_DSIFORK, sig);
177 /* w/ a configuration file, we can force a re-read if we want */
182 /* w/ a configuration file, we can force a re-read if we want */
187 LOG(log_error, logtype_afpd, "afp_goaway: bad signal" );
192 static void child_handler(void)
199 #define WAIT_ANY (-1)
200 #endif /* ! WAIT_ANY */
202 while ((pid = waitpid(WAIT_ANY, &status, WNOHANG)) > 0) {
203 for (i = 0; i < server_children->nforks; i++) {
204 if ((fd = server_child_remove(server_children, i, pid)) != -1) {
205 fdset_del_fd(&fdset, &polldata, &fdset_used, &fdset_size, fd);
210 if (WIFEXITED(status)) {
211 if (WEXITSTATUS(status))
212 LOG(log_info, logtype_afpd, "child[%d]: exited %d", pid, WEXITSTATUS(status));
214 LOG(log_info, logtype_afpd, "child[%d]: done", pid);
216 if (WIFSIGNALED(status))
217 LOG(log_info, logtype_afpd, "child[%d]: killed by signal %d", pid, WTERMSIG(status));
219 LOG(log_info, logtype_afpd, "child[%d]: died", pid);
224 static int setlimits(void)
228 if (getrlimit(RLIMIT_NOFILE, &rlim) != 0) {
229 LOG(log_warning, logtype_afpd, "setlimits: reading current limits failed: %s", strerror(errno));
232 if (rlim.rlim_cur != RLIM_INFINITY && rlim.rlim_cur < 65535) {
233 rlim.rlim_cur = 65535;
234 if (rlim.rlim_max != RLIM_INFINITY && rlim.rlim_max < 65535)
235 rlim.rlim_max = 65535;
236 if (setrlimit(RLIMIT_NOFILE, &rlim) != 0) {
237 LOG(log_warning, logtype_afpd, "setlimits: increasing limits failed: %s", strerror(errno));
244 int main(int ac, char **av)
256 set_auth_parameters( ac, av );
259 /* Parse argv args and initialize default options */
260 afp_options_init(&default_options);
261 if (!afp_options_parse(ac, av, &default_options))
264 if (check_lockfile("afpd", default_options.pidfile) != 0)
267 if (!(default_options.flags & OPTION_DEBUG) && (daemonize(0, 0) != 0))
270 if (create_lockfile("afpd", default_options.pidfile) != 0)
273 /* Log SIGBUS/SIGSEGV SBT */
276 /* Default log setup: log to syslog */
277 set_processname("afpd");
278 setuplog("default log_note");
280 /* Save the user's current umask */
281 default_options.save_mask = umask( default_options.umask );
285 /* install child handler for asp and dsi. we do this before afp_goaway
286 * as afp_goaway references stuff from here.
287 * XXX: this should really be setup after the initial connections. */
288 if (!(server_children = server_child_alloc(default_options.connections,
290 LOG(log_error, logtype_afpd, "main: server_child alloc: %s", strerror(errno) );
294 memset(&sv, 0, sizeof(sv));
295 /* linux at least up to 2.4.22 send a SIGXFZ for vfat fs,
296 even if the file is open with O_LARGEFILE ! */
298 sv.sa_handler = SIG_IGN;
299 sigemptyset( &sv.sa_mask );
300 if (sigaction(SIGXFSZ, &sv, NULL ) < 0 ) {
301 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
306 sv.sa_handler = afp_goaway; /* handler for all sigs */
308 sigemptyset( &sv.sa_mask );
309 sigaddset(&sv.sa_mask, SIGALRM);
310 sigaddset(&sv.sa_mask, SIGHUP);
311 sigaddset(&sv.sa_mask, SIGTERM);
312 sigaddset(&sv.sa_mask, SIGUSR1);
313 sigaddset(&sv.sa_mask, SIGQUIT);
314 sv.sa_flags = SA_RESTART;
315 if ( sigaction( SIGCHLD, &sv, NULL ) < 0 ) {
316 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
320 sigemptyset( &sv.sa_mask );
321 sigaddset(&sv.sa_mask, SIGALRM);
322 sigaddset(&sv.sa_mask, SIGTERM);
323 sigaddset(&sv.sa_mask, SIGHUP);
324 sigaddset(&sv.sa_mask, SIGCHLD);
325 sigaddset(&sv.sa_mask, SIGQUIT);
326 sv.sa_flags = SA_RESTART;
327 if ( sigaction( SIGUSR1, &sv, NULL ) < 0 ) {
328 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
332 sigemptyset( &sv.sa_mask );
333 sigaddset(&sv.sa_mask, SIGALRM);
334 sigaddset(&sv.sa_mask, SIGTERM);
335 sigaddset(&sv.sa_mask, SIGUSR1);
336 sigaddset(&sv.sa_mask, SIGCHLD);
337 sigaddset(&sv.sa_mask, SIGQUIT);
338 sv.sa_flags = SA_RESTART;
339 if ( sigaction( SIGHUP, &sv, NULL ) < 0 ) {
340 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
345 sigemptyset( &sv.sa_mask );
346 sigaddset(&sv.sa_mask, SIGALRM);
347 sigaddset(&sv.sa_mask, SIGHUP);
348 sigaddset(&sv.sa_mask, SIGUSR1);
349 sigaddset(&sv.sa_mask, SIGCHLD);
350 sigaddset(&sv.sa_mask, SIGQUIT);
351 sv.sa_flags = SA_RESTART;
352 if ( sigaction( SIGTERM, &sv, NULL ) < 0 ) {
353 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
357 sigemptyset( &sv.sa_mask );
358 sigaddset(&sv.sa_mask, SIGALRM);
359 sigaddset(&sv.sa_mask, SIGHUP);
360 sigaddset(&sv.sa_mask, SIGUSR1);
361 sigaddset(&sv.sa_mask, SIGCHLD);
362 sigaddset(&sv.sa_mask, SIGTERM);
363 sv.sa_flags = SA_RESTART;
364 if (sigaction(SIGQUIT, &sv, NULL ) < 0 ) {
365 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
369 /* afpd.conf: not in config file: lockfile, connections, configfile
370 * preference: command-line provides defaults.
371 * config file over-writes defaults.
373 * we also need to make sure that killing afpd during startup
374 * won't leave any lingering registered names around.
378 sigaddset(&sigs, SIGALRM);
379 sigaddset(&sigs, SIGHUP);
380 sigaddset(&sigs, SIGUSR1);
382 /* don't block SIGTERM */
383 sigaddset(&sigs, SIGTERM);
385 sigaddset(&sigs, SIGCHLD);
387 pthread_sigmask(SIG_BLOCK, &sigs, NULL);
388 if (!(configs = configinit(&default_options))) {
389 LOG(log_error, logtype_afpd, "main: no servers configured");
392 pthread_sigmask(SIG_UNBLOCK, &sigs, NULL);
396 if (locktable_init("XXX") != 0)
399 if (rpc_init("127.0.0.1", 4701) != 0)
403 /* watch atp, dsi sockets and ipc parent/child file descriptor. */
405 if (default_options.flags & OPTION_KEEPSESSIONS) {
406 LOG(log_note, logtype_afpd, "Activating continous service");
407 disasociated_ipc_fd = ipc_server_uds(_PATH_AFP_IPC);
410 fd_set_listening_sockets();
416 int fd[2]; /* we only use one, but server_child_add expects [2] */
420 /* wait for an appleshare connection. parent remains in the loop
421 * while the children get handled by afp_over_{asp,dsi}. this is
422 * currently vulnerable to a denial-of-service attack if a
423 * connection is made without an actual login attempt being made
424 * afterwards. establishing timeouts for logins is a possible
427 LOG(log_maxdebug, logtype_afpd, "main: polling %i fds", fdset_used);
428 pthread_sigmask(SIG_UNBLOCK, &sigs, NULL);
429 ret = poll(fdset, fdset_used, -1);
430 pthread_sigmask(SIG_BLOCK, &sigs, NULL);
442 fd_reset_listening_sockets();
444 LOG(log_info, logtype_afpd, "re-reading configuration file");
445 for (config = configs; config; config = config->next)
446 if (config->server_cleanup)
447 config->server_cleanup(config);
449 /* configfree close atp socket used for DDP tickle, there's an issue
451 configfree(configs, NULL);
452 if (!(configs = configinit(&default_options))) {
453 LOG(log_error, logtype_afpd, "config re-read: no servers configured");
457 fd_set_listening_sockets();
471 LOG(log_error, logtype_afpd, "main: can't wait for input: %s", strerror(errno));
475 for (int i = 0; i < fdset_used; i++) {
476 if (fdset[i].revents & (POLLIN | POLLERR | POLLHUP)) {
477 switch (polldata[i].fdtype) {
480 config = (AFPConfig *)polldata[i].data;
481 /* config->server_start is afp_config.c:dsi_start() for DSI */
482 if (child = config->server_start(config, configs, server_children)) {
483 /* Add IPC fd to select fd set */
484 fdset_add_fd(default_options.connections + AFP_LISTENERS + FDSET_SAFETY,
496 child = (afp_child_t *)polldata[i].data;
497 LOG(log_debug, logtype_afpd, "main: IPC request from child[%u]", child->pid);
499 if (ipc_server_read(server_children, child->ipc_fds[0]) != 0) {
500 fdset_del_fd(&fdset, &polldata, &fdset_used, &fdset_size, child->ipc_fds[0]);
501 close(child->ipc_fds[0]);
502 child->ipc_fds[0] = -1;
503 if ((default_options.flags & OPTION_KEEPSESSIONS) && child->disasociated) {
504 LOG(log_note, logtype_afpd, "main: removing reattached child[%u]", child->pid);
505 server_child_remove(server_children, CHILD_DSIFORK, child->pid);
510 case DISASOCIATED_IPC_FD:
511 LOG(log_debug, logtype_afpd, "main: IPC reconnect request");
512 if ((fd[0] = accept(disasociated_ipc_fd, NULL, NULL)) == -1) {
513 LOG(log_error, logtype_afpd, "main: accept: %s", strerror(errno));
516 if (readt(fd[0], &pid, sizeof(pid_t), 0, 1) != sizeof(pid_t)) {
517 LOG(log_error, logtype_afpd, "main: readt: %s", strerror(errno));
521 LOG(log_note, logtype_afpd, "main: IPC reconnect from pid [%u]", pid);
522 if ((child = server_child_add(server_children, CHILD_DSIFORK, pid, fd)) == NULL) {
523 LOG(log_error, logtype_afpd, "main: server_child_add");
527 child->disasociated = 1;
528 fdset_add_fd(default_options.connections + AFP_LISTENERS + FDSET_SAFETY,
539 LOG(log_debug, logtype_afpd, "main: IPC request for unknown type");