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 unsigned char nologin = 0;
57 struct afp_options default_options;
59 static AFPConfig *configs;
60 static server_child *server_children;
61 static sig_atomic_t reloadconfig = 0;
63 /* Two pointers to dynamic allocated arrays which store pollfds and associated data */
64 static struct pollfd *fdset;
65 static struct polldata *polldata;
66 static int fdset_size; /* current allocated size */
67 static int fdset_used; /* number of used elements */
68 static int disasociated_ipc_fd; /* disasociated sessions uses this fd for IPC */
71 void afp_get_cmdline( int *ac, char ***av)
78 /* This is registered with atexit() */
79 static void afp_exit(void)
81 if (parent_or_child == 0)
82 /* Only do this in the parent */
83 server_unlock(default_options.pidfile);
88 initialize fd set we are waiting for.
90 static void fd_set_listening_sockets(void)
94 for (config = configs; config; config = config->next) {
95 if (config->fd < 0) /* for proxies */
97 fdset_add_fd(&fdset, &polldata, &fdset_used, &fdset_size, config->fd, LISTEN_FD, config);
99 fdset_add_fd(&fdset, &polldata, &fdset_used, &fdset_size, disasociated_ipc_fd, DISASOCIATED_IPC_FD, NULL);
102 static void fd_reset_listening_sockets(void)
106 for (config = configs; config; config = config->next) {
107 if (config->fd < 0) /* for proxies */
109 fdset_del_fd(&fdset, &polldata, &fdset_used, &fdset_size, config->fd);
111 fdset_del_fd(&fdset, &polldata, &fdset_used, &fdset_size, disasociated_ipc_fd);
114 /* ------------------ */
115 static void afp_goaway(int sig)
125 LOG(log_note, logtype_afpd, "AFP Server shutting down on SIGTERM");
128 LOG(log_note, logtype_afpd, "AFP Server shutting down on SIGQUIT, NOT disconnecting clients");
132 server_child_kill(server_children, CHILD_DSIFORK, sig);
134 for (config = configs; config; config = config->next)
135 if (config->server_cleanup)
136 config->server_cleanup(config);
137 server_unlock(default_options.pidfile);
144 LOG(log_info, logtype_afpd, "disallowing logins");
147 server_child_kill(server_children, CHILD_DSIFORK, sig);
151 /* w/ a configuration file, we can force a re-read if we want */
156 LOG(log_error, logtype_afpd, "afp_goaway: bad signal" );
161 static void child_handler(int sig _U_)
168 #define WAIT_ANY (-1)
169 #endif /* ! WAIT_ANY */
171 while ((pid = waitpid(WAIT_ANY, &status, WNOHANG)) > 0) {
172 for (i = 0; i < server_children->nforks; i++) {
173 if ((fd = server_child_remove(server_children, i, pid)) != -1) {
174 fdset_del_fd(&fdset, &polldata, &fdset_used, &fdset_size, fd);
179 if (WIFEXITED(status)) {
180 if (WEXITSTATUS(status))
181 LOG(log_info, logtype_afpd, "child[%d]: exited %d", pid, WEXITSTATUS(status));
183 LOG(log_info, logtype_afpd, "child[%d]: done", pid);
185 if (WIFSIGNALED(status))
186 LOG(log_info, logtype_afpd, "child[%d]: killed by signal %d", pid, WTERMSIG(status));
188 LOG(log_info, logtype_afpd, "child[%d]: died", pid);
193 static int setlimits(void)
197 if (getrlimit(RLIMIT_NOFILE, &rlim) != 0) {
198 LOG(log_error, logtype_afpd, "setlimits: %s", strerror(errno));
201 if (rlim.rlim_cur != RLIM_INFINITY && rlim.rlim_cur < 65535) {
202 rlim.rlim_cur = 65535;
203 if (rlim.rlim_max != RLIM_INFINITY && rlim.rlim_max < 65535)
204 rlim.rlim_max = 65535;
205 if (setrlimit(RLIMIT_NOFILE, &rlim) != 0) {
206 LOG(log_error, logtype_afpd, "setlimits: %s", strerror(errno));
213 int main(int ac, char **av)
225 set_auth_parameters( ac, av );
228 /* Log SIGBUS/SIGSEGV SBT */
231 /* Default log setup: log to syslog */
232 setuplog("default log_note");
234 afp_options_init(&default_options);
235 if (!afp_options_parse(ac, av, &default_options))
238 /* Save the user's current umask for use with CNID (and maybe some
239 * other things, too). */
240 default_options.save_mask = umask( default_options.umask );
242 switch(server_lock("afpd", default_options.pidfile,
243 default_options.flags & OPTION_DEBUG)) {
248 default: /* server */
253 /* install child handler for asp and dsi. we do this before afp_goaway
254 * as afp_goaway references stuff from here.
255 * XXX: this should really be setup after the initial connections. */
256 if (!(server_children = server_child_alloc(default_options.connections,
258 LOG(log_error, logtype_afpd, "main: server_child alloc: %s", strerror(errno) );
262 memset(&sv, 0, sizeof(sv));
263 /* linux at least up to 2.4.22 send a SIGXFZ for vfat fs,
264 even if the file is open with O_LARGEFILE ! */
266 sv.sa_handler = SIG_IGN;
267 sigemptyset( &sv.sa_mask );
268 if (sigaction(SIGXFSZ, &sv, NULL ) < 0 ) {
269 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
274 sv.sa_handler = child_handler;
275 sigemptyset( &sv.sa_mask );
276 sigaddset(&sv.sa_mask, SIGALRM);
277 sigaddset(&sv.sa_mask, SIGHUP);
278 sigaddset(&sv.sa_mask, SIGTERM);
279 sigaddset(&sv.sa_mask, SIGUSR1);
280 sigaddset(&sv.sa_mask, SIGQUIT);
281 sv.sa_flags = SA_RESTART;
282 if ( sigaction( SIGCHLD, &sv, NULL ) < 0 ) {
283 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
287 sv.sa_handler = afp_goaway;
288 sigemptyset( &sv.sa_mask );
289 sigaddset(&sv.sa_mask, SIGALRM);
290 sigaddset(&sv.sa_mask, SIGTERM);
291 sigaddset(&sv.sa_mask, SIGHUP);
292 sigaddset(&sv.sa_mask, SIGCHLD);
293 sigaddset(&sv.sa_mask, SIGQUIT);
294 sv.sa_flags = SA_RESTART;
295 if ( sigaction( SIGUSR1, &sv, NULL ) < 0 ) {
296 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
300 sigemptyset( &sv.sa_mask );
301 sigaddset(&sv.sa_mask, SIGALRM);
302 sigaddset(&sv.sa_mask, SIGTERM);
303 sigaddset(&sv.sa_mask, SIGUSR1);
304 sigaddset(&sv.sa_mask, SIGCHLD);
305 sigaddset(&sv.sa_mask, SIGQUIT);
306 sv.sa_flags = SA_RESTART;
307 if ( sigaction( SIGHUP, &sv, NULL ) < 0 ) {
308 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
313 sigemptyset( &sv.sa_mask );
314 sigaddset(&sv.sa_mask, SIGALRM);
315 sigaddset(&sv.sa_mask, SIGHUP);
316 sigaddset(&sv.sa_mask, SIGUSR1);
317 sigaddset(&sv.sa_mask, SIGCHLD);
318 sigaddset(&sv.sa_mask, SIGQUIT);
319 sv.sa_flags = SA_RESTART;
320 if ( sigaction( SIGTERM, &sv, NULL ) < 0 ) {
321 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
325 sigemptyset( &sv.sa_mask );
326 sigaddset(&sv.sa_mask, SIGALRM);
327 sigaddset(&sv.sa_mask, SIGHUP);
328 sigaddset(&sv.sa_mask, SIGUSR1);
329 sigaddset(&sv.sa_mask, SIGCHLD);
330 sigaddset(&sv.sa_mask, SIGTERM);
331 sv.sa_flags = SA_RESTART;
332 if (sigaction(SIGQUIT, &sv, NULL ) < 0 ) {
333 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
337 /* afpd.conf: not in config file: lockfile, connections, configfile
338 * preference: command-line provides defaults.
339 * config file over-writes defaults.
341 * we also need to make sure that killing afpd during startup
342 * won't leave any lingering registered names around.
346 sigaddset(&sigs, SIGALRM);
347 sigaddset(&sigs, SIGHUP);
348 sigaddset(&sigs, SIGUSR1);
350 /* don't block SIGTERM */
351 sigaddset(&sigs, SIGTERM);
353 sigaddset(&sigs, SIGCHLD);
355 pthread_sigmask(SIG_BLOCK, &sigs, NULL);
356 if (!(configs = configinit(&default_options))) {
357 LOG(log_error, logtype_afpd, "main: no servers configured");
360 pthread_sigmask(SIG_UNBLOCK, &sigs, NULL);
364 if (locktable_init("XXX") != 0)
367 if (rpc_init("127.0.0.1", 4701) != 0)
371 /* watch atp, dsi sockets and ipc parent/child file descriptor. */
372 disasociated_ipc_fd = ipc_server_uds(_PATH_AFP_IPC);
373 fd_set_listening_sockets();
379 int fd[2]; /* we only use one, but server_child_add expects [2] */
382 /* wait for an appleshare connection. parent remains in the loop
383 * while the children get handled by afp_over_{asp,dsi}. this is
384 * currently vulnerable to a denial-of-service attack if a
385 * connection is made without an actual login attempt being made
386 * afterwards. establishing timeouts for logins is a possible
389 LOG(log_maxdebug, logtype_afpd, "main: polling %i fds", fdset_used);
390 pthread_sigmask(SIG_UNBLOCK, &sigs, NULL);
391 ret = poll(fdset, fdset_used, -1);
392 pthread_sigmask(SIG_BLOCK, &sigs, NULL);
393 int saveerrno = errno;
398 fd_reset_listening_sockets();
400 LOG(log_info, logtype_afpd, "re-reading configuration file");
401 for (config = configs; config; config = config->next)
402 if (config->server_cleanup)
403 config->server_cleanup(config);
405 /* configfree close atp socket used for DDP tickle, there's an issue
407 configfree(configs, NULL);
408 if (!(configs = configinit(&default_options))) {
409 LOG(log_error, logtype_afpd, "config re-read: no servers configured");
413 fd_set_listening_sockets();
427 LOG(log_error, logtype_afpd, "main: can't wait for input: %s", strerror(errno));
431 for (int i = 0; i < fdset_used; i++) {
432 if (fdset[i].revents & (POLLIN | POLLERR | POLLHUP)) {
433 switch (polldata[i].fdtype) {
436 config = (AFPConfig *)polldata[i].data;
437 /* config->server_start is afp_config.c:dsi_start() for DSI */
438 if (child = config->server_start(config, configs, server_children)) {
439 /* Add IPC fd to select fd set */
440 fdset_add_fd(&fdset, &polldata, &fdset_used, &fdset_size, child->ipc_fds[0], IPC_FD, child);
445 child = (afp_child_t *)polldata[i].data;
446 LOG(log_debug, logtype_afpd, "main: IPC request from child[%u]", child->pid);
448 if ((ret = ipc_server_read(server_children, child->ipc_fds[0])) == 0) {
449 fdset_del_fd(&fdset, &polldata, &fdset_used, &fdset_size, child->ipc_fds[0]);
450 close(child->ipc_fds[0]);
451 child->ipc_fds[0] = -1;
452 if (child->disasociated) {
453 LOG(log_note, logtype_afpd, "main: removing reattached child[%u]", child->pid);
454 server_child_remove(server_children, CHILD_DSIFORK, child->pid);
459 case DISASOCIATED_IPC_FD:
460 LOG(log_debug, logtype_afpd, "main: IPC reconnect request");
461 if ((fd[0] = accept(disasociated_ipc_fd, NULL, NULL)) == -1) {
462 LOG(log_error, logtype_afpd, "main: accept: %s", strerror(errno));
465 if (readt(fd[0], &pid, sizeof(pid_t), 0, 1) != sizeof(pid_t)) {
466 LOG(log_error, logtype_afpd, "main: readt: %s", strerror(errno));
469 LOG(log_note, logtype_afpd, "main: IPC reconnect from [%u]", pid);
470 if ((child = server_child_add(server_children, CHILD_DSIFORK, pid, fd)) == NULL) {
471 LOG(log_error, logtype_afpd, "main: server_child_add");
475 child->disasociated = 1;
476 fdset_add_fd(&fdset, &polldata, &fdset_used, &fdset_size, fd[0], IPC_FD, child);
480 LOG(log_debug, logtype_afpd, "main: IPC request for unknown type");