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/globals.h>
35 #include "afp_config.h"
39 #include "afp_zeroconf.h"
42 #include <sys/security.h>
47 static char **argv = NULL;
50 #define AFP_LISTENERS 32
51 #define FDSET_SAFETY 5
53 unsigned char nologin = 0;
54 struct afp_options default_options;
56 static AFPConfig *configs;
57 static server_child *server_children;
58 static sig_atomic_t reloadconfig = 0;
59 static sig_atomic_t gotsigchld = 0;
61 /* Two pointers to dynamic allocated arrays which store pollfds and associated data */
62 static struct pollfd *fdset;
63 static struct polldata *polldata;
64 static int fdset_size; /* current allocated size */
65 static int fdset_used; /* number of used elements */
66 static int disasociated_ipc_fd; /* disasociated sessions uses this fd for IPC */
69 void afp_get_cmdline( int *ac, char ***av)
76 /* This is registered with atexit() */
77 static void afp_exit(void)
79 if (parent_or_child == 0)
80 /* Only do this in the parent */
81 server_unlock(default_options.pidfile);
86 initialize fd set we are waiting for.
88 static void fd_set_listening_sockets(void)
92 for (config = configs; config; config = config->next) {
93 if (config->fd < 0) /* for proxies */
95 fdset_add_fd(default_options.connections + AFP_LISTENERS + FDSET_SAFETY,
105 if (default_options.flags & OPTION_KEEPSESSIONS)
106 fdset_add_fd(default_options.connections + AFP_LISTENERS + FDSET_SAFETY,
116 static void fd_reset_listening_sockets(void)
120 for (config = configs; config; config = config->next) {
121 if (config->fd < 0) /* for proxies */
123 fdset_del_fd(&fdset, &polldata, &fdset_used, &fdset_size, config->fd);
126 if (default_options.flags & OPTION_KEEPSESSIONS)
127 fdset_del_fd(&fdset, &polldata, &fdset_used, &fdset_size, disasociated_ipc_fd);
130 /* ------------------ */
131 static void afp_goaway(int sig)
141 LOG(log_note, logtype_afpd, "AFP Server shutting down on SIGTERM");
144 if (default_options.flags & OPTION_KEEPSESSIONS) {
145 LOG(log_note, logtype_afpd, "AFP Server shutting down on SIGQUIT, NOT disconnecting clients");
147 LOG(log_note, logtype_afpd, "AFP Server shutting down on SIGQUIT");
153 server_child_kill(server_children, CHILD_DSIFORK, sig);
155 for (config = configs; config; config = config->next)
156 if (config->server_cleanup)
157 config->server_cleanup(config);
158 server_unlock(default_options.pidfile);
165 LOG(log_info, logtype_afpd, "disallowing logins");
168 server_child_kill(server_children, CHILD_DSIFORK, sig);
172 /* w/ a configuration file, we can force a re-read if we want */
177 /* w/ a configuration file, we can force a re-read if we want */
182 LOG(log_error, logtype_afpd, "afp_goaway: bad signal" );
187 static void child_handler(void)
194 #define WAIT_ANY (-1)
195 #endif /* ! WAIT_ANY */
197 while ((pid = waitpid(WAIT_ANY, &status, WNOHANG)) > 0) {
198 for (i = 0; i < server_children->nforks; i++) {
199 if ((fd = server_child_remove(server_children, i, pid)) != -1) {
200 fdset_del_fd(&fdset, &polldata, &fdset_used, &fdset_size, fd);
205 if (WIFEXITED(status)) {
206 if (WEXITSTATUS(status))
207 LOG(log_info, logtype_afpd, "child[%d]: exited %d", pid, WEXITSTATUS(status));
209 LOG(log_info, logtype_afpd, "child[%d]: done", pid);
211 if (WIFSIGNALED(status))
212 LOG(log_info, logtype_afpd, "child[%d]: killed by signal %d", pid, WTERMSIG(status));
214 LOG(log_info, logtype_afpd, "child[%d]: died", pid);
219 static int setlimits(void)
223 if (getrlimit(RLIMIT_NOFILE, &rlim) != 0) {
224 LOG(log_warning, logtype_afpd, "setlimits: reading current limits failed: %s", strerror(errno));
227 if (rlim.rlim_cur != RLIM_INFINITY && rlim.rlim_cur < 65535) {
228 rlim.rlim_cur = 65535;
229 if (rlim.rlim_max != RLIM_INFINITY && rlim.rlim_max < 65535)
230 rlim.rlim_max = 65535;
231 if (setrlimit(RLIMIT_NOFILE, &rlim) != 0) {
232 LOG(log_warning, logtype_afpd, "setlimits: increasing limits failed: %s", strerror(errno));
239 int main(int ac, char **av)
251 set_auth_parameters( ac, av );
254 /* Parse argv args and initialize default options */
255 afp_options_init(&default_options);
256 if (!afp_options_parse(ac, av, &default_options))
259 if (check_lockfile("afpd", default_options.pidfile) != 0)
262 if (!(default_options.flags & OPTION_DEBUG) && (daemonize(0, 0) != 0))
265 if (create_lockfile("afpd", default_options.pidfile) != 0)
268 /* Log SIGBUS/SIGSEGV SBT */
271 /* Default log setup: log to syslog */
272 set_processname("afpd");
273 setuplog("default log_note");
275 /* Save the user's current umask */
276 default_options.save_mask = umask( default_options.umask );
280 /* install child handler for asp and dsi. we do this before afp_goaway
281 * as afp_goaway references stuff from here.
282 * XXX: this should really be setup after the initial connections. */
283 if (!(server_children = server_child_alloc(default_options.connections,
285 LOG(log_error, logtype_afpd, "main: server_child alloc: %s", strerror(errno) );
289 memset(&sv, 0, sizeof(sv));
290 /* linux at least up to 2.4.22 send a SIGXFZ for vfat fs,
291 even if the file is open with O_LARGEFILE ! */
293 sv.sa_handler = SIG_IGN;
294 sigemptyset( &sv.sa_mask );
295 if (sigaction(SIGXFSZ, &sv, NULL ) < 0 ) {
296 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
301 sv.sa_handler = afp_goaway; /* handler for all sigs */
303 sigemptyset( &sv.sa_mask );
304 sigaddset(&sv.sa_mask, SIGALRM);
305 sigaddset(&sv.sa_mask, SIGHUP);
306 sigaddset(&sv.sa_mask, SIGTERM);
307 sigaddset(&sv.sa_mask, SIGUSR1);
308 sigaddset(&sv.sa_mask, SIGQUIT);
309 sv.sa_flags = SA_RESTART;
310 if ( sigaction( SIGCHLD, &sv, NULL ) < 0 ) {
311 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
315 sigemptyset( &sv.sa_mask );
316 sigaddset(&sv.sa_mask, SIGALRM);
317 sigaddset(&sv.sa_mask, SIGTERM);
318 sigaddset(&sv.sa_mask, SIGHUP);
319 sigaddset(&sv.sa_mask, SIGCHLD);
320 sigaddset(&sv.sa_mask, SIGQUIT);
321 sv.sa_flags = SA_RESTART;
322 if ( sigaction( SIGUSR1, &sv, NULL ) < 0 ) {
323 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
327 sigemptyset( &sv.sa_mask );
328 sigaddset(&sv.sa_mask, SIGALRM);
329 sigaddset(&sv.sa_mask, SIGTERM);
330 sigaddset(&sv.sa_mask, SIGUSR1);
331 sigaddset(&sv.sa_mask, SIGCHLD);
332 sigaddset(&sv.sa_mask, SIGQUIT);
333 sv.sa_flags = SA_RESTART;
334 if ( sigaction( SIGHUP, &sv, NULL ) < 0 ) {
335 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
340 sigemptyset( &sv.sa_mask );
341 sigaddset(&sv.sa_mask, SIGALRM);
342 sigaddset(&sv.sa_mask, SIGHUP);
343 sigaddset(&sv.sa_mask, SIGUSR1);
344 sigaddset(&sv.sa_mask, SIGCHLD);
345 sigaddset(&sv.sa_mask, SIGQUIT);
346 sv.sa_flags = SA_RESTART;
347 if ( sigaction( SIGTERM, &sv, NULL ) < 0 ) {
348 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
352 sigemptyset( &sv.sa_mask );
353 sigaddset(&sv.sa_mask, SIGALRM);
354 sigaddset(&sv.sa_mask, SIGHUP);
355 sigaddset(&sv.sa_mask, SIGUSR1);
356 sigaddset(&sv.sa_mask, SIGCHLD);
357 sigaddset(&sv.sa_mask, SIGTERM);
358 sv.sa_flags = SA_RESTART;
359 if (sigaction(SIGQUIT, &sv, NULL ) < 0 ) {
360 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
364 /* afpd.conf: not in config file: lockfile, connections, configfile
365 * preference: command-line provides defaults.
366 * config file over-writes defaults.
368 * we also need to make sure that killing afpd during startup
369 * won't leave any lingering registered names around.
373 sigaddset(&sigs, SIGALRM);
374 sigaddset(&sigs, SIGHUP);
375 sigaddset(&sigs, SIGUSR1);
377 /* don't block SIGTERM */
378 sigaddset(&sigs, SIGTERM);
380 sigaddset(&sigs, SIGCHLD);
382 pthread_sigmask(SIG_BLOCK, &sigs, NULL);
383 if (!(configs = configinit(&default_options))) {
384 LOG(log_error, logtype_afpd, "main: no servers configured");
387 pthread_sigmask(SIG_UNBLOCK, &sigs, NULL);
392 /* watch atp, dsi sockets and ipc parent/child file descriptor. */
394 if (default_options.flags & OPTION_KEEPSESSIONS) {
395 LOG(log_note, logtype_afpd, "Activating continous service");
396 disasociated_ipc_fd = ipc_server_uds(_PATH_AFP_IPC);
399 fd_set_listening_sockets();
405 int fd[2]; /* we only use one, but server_child_add expects [2] */
409 /* wait for an appleshare connection. parent remains in the loop
410 * while the children get handled by afp_over_{asp,dsi}. this is
411 * currently vulnerable to a denial-of-service attack if a
412 * connection is made without an actual login attempt being made
413 * afterwards. establishing timeouts for logins is a possible
416 LOG(log_maxdebug, logtype_afpd, "main: polling %i fds", fdset_used);
417 pthread_sigmask(SIG_UNBLOCK, &sigs, NULL);
418 ret = poll(fdset, fdset_used, -1);
419 pthread_sigmask(SIG_BLOCK, &sigs, NULL);
431 fd_reset_listening_sockets();
433 LOG(log_info, logtype_afpd, "re-reading configuration file");
434 for (config = configs; config; config = config->next)
435 if (config->server_cleanup)
436 config->server_cleanup(config);
438 /* configfree close atp socket used for DDP tickle, there's an issue
440 configfree(configs, NULL);
441 if (!(configs = configinit(&default_options))) {
442 LOG(log_error, logtype_afpd, "config re-read: no servers configured");
446 fd_set_listening_sockets();
460 LOG(log_error, logtype_afpd, "main: can't wait for input: %s", strerror(errno));
464 for (int i = 0; i < fdset_used; i++) {
465 if (fdset[i].revents & (POLLIN | POLLERR | POLLHUP)) {
466 switch (polldata[i].fdtype) {
469 config = (AFPConfig *)polldata[i].data;
470 /* config->server_start is afp_config.c:dsi_start() for DSI */
471 if (child = config->server_start(config, configs, server_children)) {
472 /* Add IPC fd to select fd set */
473 fdset_add_fd(default_options.connections + AFP_LISTENERS + FDSET_SAFETY,
485 child = (afp_child_t *)polldata[i].data;
486 LOG(log_debug, logtype_afpd, "main: IPC request from child[%u]", child->pid);
488 if (ipc_server_read(server_children, child->ipc_fds[0]) != 0) {
489 fdset_del_fd(&fdset, &polldata, &fdset_used, &fdset_size, child->ipc_fds[0]);
490 close(child->ipc_fds[0]);
491 child->ipc_fds[0] = -1;
492 if ((default_options.flags & OPTION_KEEPSESSIONS) && child->disasociated) {
493 LOG(log_note, logtype_afpd, "main: removing reattached child[%u]", child->pid);
494 server_child_remove(server_children, CHILD_DSIFORK, child->pid);
499 case DISASOCIATED_IPC_FD:
500 LOG(log_debug, logtype_afpd, "main: IPC reconnect request");
501 if ((fd[0] = accept(disasociated_ipc_fd, NULL, NULL)) == -1) {
502 LOG(log_error, logtype_afpd, "main: accept: %s", strerror(errno));
505 if (readt(fd[0], &pid, sizeof(pid_t), 0, 1) != sizeof(pid_t)) {
506 LOG(log_error, logtype_afpd, "main: readt: %s", strerror(errno));
510 LOG(log_note, logtype_afpd, "main: IPC reconnect from pid [%u]", pid);
511 if ((child = server_child_add(server_children, CHILD_DSIFORK, pid, fd)) == NULL) {
512 LOG(log_error, logtype_afpd, "main: server_child_add");
516 child->disasociated = 1;
517 fdset_add_fd(default_options.connections + AFP_LISTENERS + FDSET_SAFETY,
528 LOG(log_debug, logtype_afpd, "main: IPC request for unknown type");