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 unsigned char nologin = 0;
56 struct afp_options default_options;
57 static AFPConfig *configs;
58 static server_child *server_children;
59 static sig_atomic_t reloadconfig = 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(&fdset, &polldata, &fdset_used, &fdset_size, config->fd, LISTEN_FD, config);
97 fdset_add_fd(&fdset, &polldata, &fdset_used, &fdset_size, disasociated_ipc_fd, DISASOCIATED_IPC_FD, NULL);
100 static void fd_reset_listening_sockets(void)
104 for (config = configs; config; config = config->next) {
105 if (config->fd < 0) /* for proxies */
107 fdset_del_fd(&fdset, &polldata, &fdset_used, &fdset_size, config->fd);
109 fdset_del_fd(&fdset, &polldata, &fdset_used, &fdset_size, disasociated_ipc_fd);
112 /* ------------------ */
113 static void afp_goaway(int sig)
119 #endif /* ! NO_DDP */
127 LOG(log_note, logtype_afpd, "AFP Server shutting down on SIGTERM");
130 LOG(log_note, logtype_afpd, "AFP Server shutting down on SIGQUIT, NOT disconnecting clients");
134 server_child_kill(server_children, CHILD_DSIFORK, sig);
136 for (config = configs; config; config = config->next)
137 if (config->server_cleanup)
138 config->server_cleanup(config);
139 server_unlock(default_options.pidfile);
146 LOG(log_info, logtype_afpd, "disallowing logins");
149 server_child_kill(server_children, CHILD_DSIFORK, sig);
153 /* w/ a configuration file, we can force a re-read if we want */
158 LOG(log_error, logtype_afpd, "afp_goaway: bad signal" );
163 static void child_handler(int sig _U_)
170 #define WAIT_ANY (-1)
171 #endif /* ! WAIT_ANY */
173 while ((pid = waitpid(WAIT_ANY, &status, WNOHANG)) > 0) {
174 for (i = 0; i < server_children->nforks; i++) {
175 if ((fd = server_child_remove(server_children, i, pid)) != -1) {
176 fdset_del_fd(&fdset, &polldata, &fdset_used, &fdset_size, fd);
181 if (WIFEXITED(status)) {
182 if (WEXITSTATUS(status))
183 LOG(log_info, logtype_afpd, "child[%d]: exited %d", pid, WEXITSTATUS(status));
185 LOG(log_info, logtype_afpd, "child[%d]: done", pid);
187 if (WIFSIGNALED(status))
188 LOG(log_info, logtype_afpd, "child[%d]: killed by signal %d", pid, WTERMSIG(status));
190 LOG(log_info, logtype_afpd, "child[%d]: died", pid);
195 static int setlimits(void)
199 if (getrlimit(RLIMIT_NOFILE, &rlim) != 0) {
200 LOG(log_error, logtype_afpd, "setlimits: %s", strerror(errno));
203 if (rlim.rlim_cur != RLIM_INFINITY && rlim.rlim_cur < 65535) {
204 rlim.rlim_cur = 65535;
205 if (rlim.rlim_max != RLIM_INFINITY && rlim.rlim_max < 65535)
206 rlim.rlim_max = 65535;
207 if (setrlimit(RLIMIT_NOFILE, &rlim) != 0) {
208 LOG(log_error, logtype_afpd, "setlimits: %s", strerror(errno));
215 int main(int ac, char **av)
227 set_auth_parameters( ac, av );
230 /* Log SIGBUS/SIGSEGV SBT */
233 /* Default log setup: log to syslog */
234 setuplog("default log_note");
236 afp_options_init(&default_options);
237 if (!afp_options_parse(ac, av, &default_options))
240 /* Save the user's current umask for use with CNID (and maybe some
241 * other things, too). */
242 default_options.save_mask = umask( default_options.umask );
244 switch(server_lock("afpd", default_options.pidfile,
245 default_options.flags & OPTION_DEBUG)) {
250 default: /* server */
255 /* install child handler for asp and dsi. we do this before afp_goaway
256 * as afp_goaway references stuff from here.
257 * XXX: this should really be setup after the initial connections. */
258 if (!(server_children = server_child_alloc(default_options.connections,
260 LOG(log_error, logtype_afpd, "main: server_child alloc: %s", strerror(errno) );
264 memset(&sv, 0, sizeof(sv));
265 /* linux at least up to 2.4.22 send a SIGXFZ for vfat fs,
266 even if the file is open with O_LARGEFILE ! */
268 sv.sa_handler = SIG_IGN;
269 sigemptyset( &sv.sa_mask );
270 if (sigaction(SIGXFSZ, &sv, NULL ) < 0 ) {
271 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
276 sv.sa_handler = child_handler;
277 sigemptyset( &sv.sa_mask );
278 sigaddset(&sv.sa_mask, SIGALRM);
279 sigaddset(&sv.sa_mask, SIGHUP);
280 sigaddset(&sv.sa_mask, SIGTERM);
281 sigaddset(&sv.sa_mask, SIGUSR1);
282 sigaddset(&sv.sa_mask, SIGQUIT);
283 sv.sa_flags = SA_RESTART;
284 if ( sigaction( SIGCHLD, &sv, NULL ) < 0 ) {
285 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
289 sv.sa_handler = afp_goaway;
290 sigemptyset( &sv.sa_mask );
291 sigaddset(&sv.sa_mask, SIGALRM);
292 sigaddset(&sv.sa_mask, SIGTERM);
293 sigaddset(&sv.sa_mask, SIGHUP);
294 sigaddset(&sv.sa_mask, SIGCHLD);
295 sigaddset(&sv.sa_mask, SIGQUIT);
296 sv.sa_flags = SA_RESTART;
297 if ( sigaction( SIGUSR1, &sv, NULL ) < 0 ) {
298 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
302 sigemptyset( &sv.sa_mask );
303 sigaddset(&sv.sa_mask, SIGALRM);
304 sigaddset(&sv.sa_mask, SIGTERM);
305 sigaddset(&sv.sa_mask, SIGUSR1);
306 sigaddset(&sv.sa_mask, SIGCHLD);
307 sigaddset(&sv.sa_mask, SIGQUIT);
308 sv.sa_flags = SA_RESTART;
309 if ( sigaction( SIGHUP, &sv, NULL ) < 0 ) {
310 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, SIGHUP);
318 sigaddset(&sv.sa_mask, SIGUSR1);
319 sigaddset(&sv.sa_mask, SIGCHLD);
320 sigaddset(&sv.sa_mask, SIGQUIT);
321 sv.sa_flags = SA_RESTART;
322 if ( sigaction( SIGTERM, &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, SIGHUP);
330 sigaddset(&sv.sa_mask, SIGUSR1);
331 sigaddset(&sv.sa_mask, SIGCHLD);
332 sigaddset(&sv.sa_mask, SIGTERM);
333 sv.sa_flags = SA_RESTART;
334 if (sigaction(SIGQUIT, &sv, NULL ) < 0 ) {
335 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
339 /* afpd.conf: not in config file: lockfile, connections, configfile
340 * preference: command-line provides defaults.
341 * config file over-writes defaults.
343 * we also need to make sure that killing afpd during startup
344 * won't leave any lingering registered names around.
348 sigaddset(&sigs, SIGALRM);
349 sigaddset(&sigs, SIGHUP);
350 sigaddset(&sigs, SIGUSR1);
352 /* don't block SIGTERM */
353 sigaddset(&sigs, SIGTERM);
355 sigaddset(&sigs, SIGCHLD);
357 pthread_sigmask(SIG_BLOCK, &sigs, NULL);
358 if (!(configs = configinit(&default_options))) {
359 LOG(log_error, logtype_afpd, "main: no servers configured");
362 pthread_sigmask(SIG_UNBLOCK, &sigs, NULL);
367 /* watch atp, dsi sockets and ipc parent/child file descriptor. */
368 disasociated_ipc_fd = ipc_server_uds(_PATH_AFP_IPC);
369 fd_set_listening_sockets();
375 int fd[2]; /* we only use one, but server_child_add expects [2] */
378 /* wait for an appleshare connection. parent remains in the loop
379 * while the children get handled by afp_over_{asp,dsi}. this is
380 * currently vulnerable to a denial-of-service attack if a
381 * connection is made without an actual login attempt being made
382 * afterwards. establishing timeouts for logins is a possible
385 LOG(log_maxdebug, logtype_afpd, "main: polling %i fds", fdset_used);
386 pthread_sigmask(SIG_UNBLOCK, &sigs, NULL);
387 ret = poll(fdset, fdset_used, -1);
388 pthread_sigmask(SIG_BLOCK, &sigs, NULL);
389 int saveerrno = errno;
394 fd_reset_listening_sockets();
396 LOG(log_info, logtype_afpd, "re-reading configuration file");
397 for (config = configs; config; config = config->next)
398 if (config->server_cleanup)
399 config->server_cleanup(config);
401 /* configfree close atp socket used for DDP tickle, there's an issue
403 configfree(configs, NULL);
404 if (!(configs = configinit(&default_options))) {
405 LOG(log_error, logtype_afpd, "config re-read: no servers configured");
409 fd_set_listening_sockets();
423 LOG(log_error, logtype_afpd, "main: can't wait for input: %s", strerror(errno));
427 for (int i = 0; i < fdset_used; i++) {
428 if (fdset[i].revents & (POLLIN | POLLERR | POLLHUP)) {
429 switch (polldata[i].fdtype) {
432 config = (AFPConfig *)polldata[i].data;
433 /* config->server_start is afp_config.c:dsi_start() for DSI */
434 if (child = config->server_start(config, configs, server_children)) {
435 /* Add IPC fd to select fd set */
436 fdset_add_fd(&fdset, &polldata, &fdset_used, &fdset_size, child->ipc_fds[0], IPC_FD, child);
441 child = (afp_child_t *)polldata[i].data;
442 LOG(log_debug, logtype_afpd, "main: IPC request from child[%u]", child->pid);
444 if (ipc_server_read(server_children, child->ipc_fds[0]) != 0) {
445 fdset_del_fd(&fdset, &polldata, &fdset_used, &fdset_size, child->ipc_fds[0]);
446 close(child->ipc_fds[0]);
447 child->ipc_fds[0] = -1;
448 if (child->disasociated) {
449 LOG(log_note, logtype_afpd, "main: removing reattached child[%u]", child->pid);
450 server_child_remove(server_children, CHILD_DSIFORK, child->pid);
455 case DISASOCIATED_IPC_FD:
456 LOG(log_debug, logtype_afpd, "main: IPC reconnect request");
457 if ((fd[0] = accept(disasociated_ipc_fd, NULL, NULL)) == -1) {
458 LOG(log_error, logtype_afpd, "main: accept: %s", strerror(errno));
461 if (readt(fd[0], &pid, sizeof(pid_t), 0, 1) != sizeof(pid_t)) {
462 LOG(log_error, logtype_afpd, "main: readt: %s", strerror(errno));
466 LOG(log_note, logtype_afpd, "main: IPC reconnect from pid [%u]", pid);
467 if ((child = server_child_add(server_children, CHILD_DSIFORK, pid, fd)) == NULL) {
468 LOG(log_error, logtype_afpd, "main: server_child_add");
472 child->disasociated = 1;
473 fdset_add_fd(&fdset, &polldata, &fdset_used, &fdset_size, fd[0], IPC_FD, child);
477 LOG(log_debug, logtype_afpd, "main: IPC request for unknown type");