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>
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 */
124 LOG(log_note, logtype_afpd, "AFP Server shutting down on SIGTERM");
127 server_child_kill(server_children, CHILD_DSIFORK, sig);
129 for (config = configs; config; config = config->next)
130 if (config->server_cleanup)
131 config->server_cleanup(config);
132 server_unlock(default_options.pidfile);
137 LOG(log_note, logtype_afpd, "AFP Server shutting down on SIGQUIT, NOT disconnecting clients");
139 for (config = configs; config; config = config->next)
140 if (config->server_cleanup)
141 config->server_cleanup(config);
143 server_unlock(default_options.pidfile);
150 LOG(log_info, logtype_afpd, "disallowing logins");
153 server_child_kill(server_children, CHILD_DSIFORK, sig);
157 /* w/ a configuration file, we can force a re-read if we want */
162 LOG(log_error, logtype_afpd, "afp_goaway: bad signal" );
167 static void child_handler(int sig _U_)
174 #define WAIT_ANY (-1)
175 #endif /* ! WAIT_ANY */
177 while ((pid = waitpid(WAIT_ANY, &status, WNOHANG)) > 0) {
178 for (i = 0; i < server_children->nforks; i++) {
179 if ((fd = server_child_remove(server_children, i, pid)) != -1) {
180 fdset_del_fd(&fdset, &polldata, &fdset_used, &fdset_size, fd);
185 if (WIFEXITED(status)) {
186 if (WEXITSTATUS(status))
187 LOG(log_info, logtype_afpd, "child[%d]: exited %d", pid, WEXITSTATUS(status));
189 LOG(log_info, logtype_afpd, "child[%d]: done", pid);
191 if (WIFSIGNALED(status))
192 LOG(log_info, logtype_afpd, "child[%d]: killed by signal %d", pid, WTERMSIG(status));
194 LOG(log_info, logtype_afpd, "child[%d]: died", pid);
199 static int setlimits(void)
203 if (getrlimit(RLIMIT_NOFILE, &rlim) != 0) {
204 LOG(log_error, logtype_afpd, "setlimits: %s", strerror(errno));
207 if (rlim.rlim_cur != RLIM_INFINITY && rlim.rlim_cur < 65535) {
208 rlim.rlim_cur = 65535;
209 if (rlim.rlim_max != RLIM_INFINITY && rlim.rlim_max < 65535)
210 rlim.rlim_max = 65535;
211 if (setrlimit(RLIMIT_NOFILE, &rlim) != 0) {
212 LOG(log_error, logtype_afpd, "setlimits: %s", strerror(errno));
219 int main(int ac, char **av)
231 set_auth_parameters( ac, av );
234 /* Log SIGBUS/SIGSEGV SBT */
237 /* Default log setup: log to syslog */
238 setuplog("default log_note");
240 afp_options_init(&default_options);
241 if (!afp_options_parse(ac, av, &default_options))
244 /* Save the user's current umask for use with CNID (and maybe some
245 * other things, too). */
246 default_options.save_mask = umask( default_options.umask );
248 switch(server_lock("afpd", default_options.pidfile,
249 default_options.flags & OPTION_DEBUG)) {
254 default: /* server */
259 /* install child handler for asp and dsi. we do this before afp_goaway
260 * as afp_goaway references stuff from here.
261 * XXX: this should really be setup after the initial connections. */
262 if (!(server_children = server_child_alloc(default_options.connections,
264 LOG(log_error, logtype_afpd, "main: server_child alloc: %s", strerror(errno) );
268 memset(&sv, 0, sizeof(sv));
269 /* linux at least up to 2.4.22 send a SIGXFZ for vfat fs,
270 even if the file is open with O_LARGEFILE ! */
272 sv.sa_handler = SIG_IGN;
273 sigemptyset( &sv.sa_mask );
274 if (sigaction(SIGXFSZ, &sv, NULL ) < 0 ) {
275 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
280 sv.sa_handler = child_handler;
281 sigemptyset( &sv.sa_mask );
282 sigaddset(&sv.sa_mask, SIGALRM);
283 sigaddset(&sv.sa_mask, SIGHUP);
284 sigaddset(&sv.sa_mask, SIGTERM);
285 sigaddset(&sv.sa_mask, SIGUSR1);
286 sigaddset(&sv.sa_mask, SIGQUIT);
287 sv.sa_flags = SA_RESTART;
288 if ( sigaction( SIGCHLD, &sv, NULL ) < 0 ) {
289 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
293 sv.sa_handler = afp_goaway;
294 sigemptyset( &sv.sa_mask );
295 sigaddset(&sv.sa_mask, SIGALRM);
296 sigaddset(&sv.sa_mask, SIGTERM);
297 sigaddset(&sv.sa_mask, SIGHUP);
298 sigaddset(&sv.sa_mask, SIGCHLD);
299 sigaddset(&sv.sa_mask, SIGQUIT);
300 sv.sa_flags = SA_RESTART;
301 if ( sigaction( SIGUSR1, &sv, NULL ) < 0 ) {
302 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
306 sigemptyset( &sv.sa_mask );
307 sigaddset(&sv.sa_mask, SIGALRM);
308 sigaddset(&sv.sa_mask, SIGTERM);
309 sigaddset(&sv.sa_mask, SIGUSR1);
310 sigaddset(&sv.sa_mask, SIGCHLD);
311 sigaddset(&sv.sa_mask, SIGQUIT);
312 sv.sa_flags = SA_RESTART;
313 if ( sigaction( SIGHUP, &sv, NULL ) < 0 ) {
314 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
319 sigemptyset( &sv.sa_mask );
320 sigaddset(&sv.sa_mask, SIGALRM);
321 sigaddset(&sv.sa_mask, SIGHUP);
322 sigaddset(&sv.sa_mask, SIGUSR1);
323 sigaddset(&sv.sa_mask, SIGCHLD);
324 sigaddset(&sv.sa_mask, SIGQUIT);
325 sv.sa_flags = SA_RESTART;
326 if ( sigaction( SIGTERM, &sv, NULL ) < 0 ) {
327 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
331 sigemptyset( &sv.sa_mask );
332 sigaddset(&sv.sa_mask, SIGALRM);
333 sigaddset(&sv.sa_mask, SIGHUP);
334 sigaddset(&sv.sa_mask, SIGUSR1);
335 sigaddset(&sv.sa_mask, SIGCHLD);
336 sigaddset(&sv.sa_mask, SIGTERM);
337 sv.sa_flags = SA_RESTART;
338 if (sigaction(SIGQUIT, &sv, NULL ) < 0 ) {
339 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
343 /* afpd.conf: not in config file: lockfile, connections, configfile
344 * preference: command-line provides defaults.
345 * config file over-writes defaults.
347 * we also need to make sure that killing afpd during startup
348 * won't leave any lingering registered names around.
352 sigaddset(&sigs, SIGALRM);
353 sigaddset(&sigs, SIGHUP);
354 sigaddset(&sigs, SIGUSR1);
356 /* don't block SIGTERM */
357 sigaddset(&sigs, SIGTERM);
359 sigaddset(&sigs, SIGCHLD);
361 pthread_sigmask(SIG_BLOCK, &sigs, NULL);
362 if (!(configs = configinit(&default_options))) {
363 LOG(log_error, logtype_afpd, "main: no servers configured");
366 pthread_sigmask(SIG_UNBLOCK, &sigs, NULL);
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();
380 /* wait for an appleshare connection. parent remains in the loop
381 * while the children get handled by afp_over_{asp,dsi}. this is
382 * currently vulnerable to a denial-of-service attack if a
383 * connection is made without an actual login attempt being made
384 * afterwards. establishing timeouts for logins is a possible
387 LOG(log_maxdebug, logtype_afpd, "main: polling %i fds", fdset_used);
388 pthread_sigmask(SIG_UNBLOCK, &sigs, NULL);
389 ret = poll(fdset, fdset_used, -1);
390 pthread_sigmask(SIG_BLOCK, &sigs, NULL);
391 int saveerrno = errno;
396 fd_reset_listening_sockets();
398 LOG(log_info, logtype_afpd, "re-reading configuration file");
399 for (config = configs; config; config = config->next)
400 if (config->server_cleanup)
401 config->server_cleanup(config);
403 /* configfree close atp socket used for DDP tickle, there's an issue
405 configfree(configs, NULL);
406 if (!(configs = configinit(&default_options))) {
407 LOG(log_error, logtype_afpd, "config re-read: no servers configured");
411 fd_set_listening_sockets();
425 LOG(log_error, logtype_afpd, "main: can't wait for input: %s", strerror(errno));
429 for (int i = 0; i < fdset_used; i++) {
430 if (fdset[i].revents & POLLIN) {
431 switch (polldata[i].fdtype) {
433 config = (AFPConfig *)polldata[i].data;
434 /* config->server_start is afp_config.c:dsi_start() for DSI */
435 if (child = config->server_start(config, configs, server_children)) {
436 /* Add IPC fd to select fd set */
437 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);
443 if ((ret = ipc_server_read(server_children, child->ipc_fds[0])) == 0) {
444 fdset_del_fd(&fdset, &polldata, &fdset_used, &fdset_size, child->ipc_fds[0]);
445 close(child->ipc_fds[0]);
446 child->ipc_fds[0] = -1;
450 LOG(log_debug, logtype_afpd, "main: IPC request for unknown type");