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>
22 #include <atalk/logger.h>
23 #include <atalk/adouble.h>
24 #include <netatalk/at.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>
35 #include "event2/event.h"
36 #include "event2/http.h"
37 #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 */
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);
101 static void fd_reset_listening_sockets(void)
105 for (config = configs; config; config = config->next) {
106 if (config->fd < 0) /* for proxies */
108 fdset_del_fd(&fdset, &polldata, &fdset_used, &fdset_size, config->fd);
112 /* ------------------ */
113 static void afp_goaway(int sig)
118 LOG(log_note, logtype_afpd, "AFP Server shutting down on SIGTERM");
122 server_child_kill(server_children, CHILD_DSIFORK, sig);
124 for (config = configs; config; config = config->next)
125 if (config->server_cleanup)
126 config->server_cleanup(config);
127 server_unlock(default_options.pidfile);
134 LOG(log_info, logtype_afpd, "disallowing logins");
137 server_child_kill(server_children, CHILD_DSIFORK, sig);
141 /* w/ a configuration file, we can force a re-read if we want */
146 LOG(log_error, logtype_afpd, "afp_goaway: bad signal" );
151 static void child_handler(int sig _U_)
158 #define WAIT_ANY (-1)
159 #endif /* ! WAIT_ANY */
161 while ((pid = waitpid(WAIT_ANY, &status, WNOHANG)) > 0) {
162 for (i = 0; i < server_children->nforks; i++) {
163 if ((fd = server_child_remove(server_children, i, pid)) != -1) {
164 fdset_del_fd(&fdset, &polldata, &fdset_used, &fdset_size, fd);
169 if (WIFEXITED(status)) {
170 if (WEXITSTATUS(status))
171 LOG(log_info, logtype_afpd, "child[%d]: exited %d", pid, WEXITSTATUS(status));
173 LOG(log_info, logtype_afpd, "child[%d]: done", pid);
175 if (WIFSIGNALED(status))
176 LOG(log_info, logtype_afpd, "child[%d]: killed by signal %d", pid, WTERMSIG(status));
178 LOG(log_info, logtype_afpd, "child[%d]: died", pid);
183 int main(int ac, char **av)
195 set_auth_parameters( ac, av );
198 /* Log SIGBUS/SIGSEGV SBT */
201 /* Default log setup: log to syslog */
202 setuplog("default log_note");
204 afp_options_init(&default_options);
205 if (!afp_options_parse(ac, av, &default_options))
208 /* Save the user's current umask for use with CNID (and maybe some
209 * other things, too). */
210 default_options.save_mask = umask( default_options.umask );
212 switch(server_lock("afpd", default_options.pidfile,
213 default_options.flags & OPTION_DEBUG)) {
218 default: /* server */
223 /* install child handler for asp and dsi. we do this before afp_goaway
224 * as afp_goaway references stuff from here.
225 * XXX: this should really be setup after the initial connections. */
226 if (!(server_children = server_child_alloc(default_options.connections,
228 LOG(log_error, logtype_afpd, "main: server_child alloc: %s", strerror(errno) );
232 memset(&sv, 0, sizeof(sv));
233 /* linux at least up to 2.4.22 send a SIGXFZ for vfat fs,
234 even if the file is open with O_LARGEFILE ! */
236 sv.sa_handler = SIG_IGN;
237 sigemptyset( &sv.sa_mask );
238 if (sigaction(SIGXFSZ, &sv, NULL ) < 0 ) {
239 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
244 sv.sa_handler = child_handler;
245 sigemptyset( &sv.sa_mask );
246 sigaddset(&sv.sa_mask, SIGALRM);
247 sigaddset(&sv.sa_mask, SIGHUP);
248 sigaddset(&sv.sa_mask, SIGTERM);
249 sigaddset(&sv.sa_mask, SIGUSR1);
251 sv.sa_flags = SA_RESTART;
252 if ( sigaction( SIGCHLD, &sv, NULL ) < 0 ) {
253 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
257 sv.sa_handler = afp_goaway;
258 sigemptyset( &sv.sa_mask );
259 sigaddset(&sv.sa_mask, SIGALRM);
260 sigaddset(&sv.sa_mask, SIGTERM);
261 sigaddset(&sv.sa_mask, SIGHUP);
262 sigaddset(&sv.sa_mask, SIGCHLD);
263 sv.sa_flags = SA_RESTART;
264 if ( sigaction( SIGUSR1, &sv, NULL ) < 0 ) {
265 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
269 sigemptyset( &sv.sa_mask );
270 sigaddset(&sv.sa_mask, SIGALRM);
271 sigaddset(&sv.sa_mask, SIGTERM);
272 sigaddset(&sv.sa_mask, SIGUSR1);
273 sigaddset(&sv.sa_mask, SIGCHLD);
274 sv.sa_flags = SA_RESTART;
275 if ( sigaction( SIGHUP, &sv, NULL ) < 0 ) {
276 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
281 sigemptyset( &sv.sa_mask );
282 sigaddset(&sv.sa_mask, SIGALRM);
283 sigaddset(&sv.sa_mask, SIGHUP);
284 sigaddset(&sv.sa_mask, SIGUSR1);
285 sigaddset(&sv.sa_mask, SIGCHLD);
286 sv.sa_flags = SA_RESTART;
287 if ( sigaction( SIGTERM, &sv, NULL ) < 0 ) {
288 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
292 /* afpd.conf: not in config file: lockfile, connections, configfile
293 * preference: command-line provides defaults.
294 * config file over-writes defaults.
296 * we also need to make sure that killing afpd during startup
297 * won't leave any lingering registered names around.
301 sigaddset(&sigs, SIGALRM);
302 sigaddset(&sigs, SIGHUP);
303 sigaddset(&sigs, SIGUSR1);
305 /* don't block SIGTERM */
306 sigaddset(&sigs, SIGTERM);
308 sigaddset(&sigs, SIGCHLD);
310 pthread_sigmask(SIG_BLOCK, &sigs, NULL);
311 if (!(configs = configinit(&default_options))) {
312 LOG(log_error, logtype_afpd, "main: no servers configured");
315 pthread_sigmask(SIG_UNBLOCK, &sigs, NULL);
319 if (locktable_init("XXX") != 0)
322 if (rpc_init("127.0.0.1", 4701) != 0)
326 /* watch atp, dsi sockets and ipc parent/child file descriptor. */
327 fd_set_listening_sockets();
331 /* wait for an appleshare connection. parent remains in the loop
332 * while the children get handled by afp_over_{asp,dsi}. this is
333 * currently vulnerable to a denial-of-service attack if a
334 * connection is made without an actual login attempt being made
335 * afterwards. establishing timeouts for logins is a possible
338 LOG(log_maxdebug, logtype_afpd, "main: polling %i fds", fdset_used);
339 pthread_sigmask(SIG_UNBLOCK, &sigs, NULL);
340 ret = poll(fdset, fdset_used, -1);
341 pthread_sigmask(SIG_BLOCK, &sigs, NULL);
342 int saveerrno = errno;
347 fd_reset_listening_sockets();
349 LOG(log_info, logtype_afpd, "re-reading configuration file");
350 for (config = configs; config; config = config->next)
351 if (config->server_cleanup)
352 config->server_cleanup(config);
354 /* configfree close atp socket used for DDP tickle, there's an issue
356 configfree(configs, NULL);
357 if (!(configs = configinit(&default_options))) {
358 LOG(log_error, logtype_afpd, "config re-read: no servers configured");
362 fd_set_listening_sockets();
376 LOG(log_error, logtype_afpd, "main: can't wait for input: %s", strerror(errno));
380 for (int i = 0; i < fdset_used; i++) {
381 if (fdset[i].revents & POLLIN) {
382 switch (polldata[i].fdtype) {
384 config = (AFPConfig *)polldata[i].data;
385 /* config->server_start is afp_config.c:dsi_start() for DSI */
386 if (child = config->server_start(config, configs, server_children)) {
387 /* Add IPC fd to select fd set */
388 fdset_add_fd(&fdset, &polldata, &fdset_used, &fdset_size, child->ipc_fds[0], IPC_FD, child);
392 child = (afp_child_t *)polldata[i].data;
393 LOG(log_debug, logtype_afpd, "main: IPC request from child[%u]", child->pid);
394 if ((ret = ipc_server_read(server_children, child->ipc_fds[0])) == 0) {
395 fdset_del_fd(&fdset, &polldata, &fdset_used, &fdset_size, child->ipc_fds[0]);
396 close(child->ipc_fds[0]);
397 child->ipc_fds[0] = -1;
401 LOG(log_debug, logtype_afpd, "main: IPC request for unknown type");