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/atp.h>
28 #include <atalk/asp.h>
29 #include <atalk/afp.h>
30 #include <atalk/paths.h>
31 #include <atalk/util.h>
32 #include <atalk/server_child.h>
33 #include <atalk/server_ipc.h>
34 #include <atalk/errchk.h>
35 #include <atalk/locking.h>
37 #include "event2/event.h"
38 #include "event2/http.h"
39 #include "event2/rpc.h"
42 #include "afp_config.h"
46 #include "afp_zeroconf.h"
49 #include <sys/security.h>
54 static char **argv = NULL;
57 unsigned char nologin = 0;
59 struct afp_options default_options;
61 static AFPConfig *configs;
62 static server_child *server_children;
63 static sig_atomic_t reloadconfig = 0;
65 /* Two pointers to dynamic allocated arrays which store pollfds and associated data */
66 static struct pollfd *fdset;
67 static struct polldata *polldata;
68 static int fdset_size; /* current allocated size */
69 static int fdset_used; /* number of used elements */
73 void afp_get_cmdline( int *ac, char ***av)
80 /* This is registered with atexit() */
81 static void afp_exit(void)
83 if (parent_or_child == 0)
84 /* Only do this in the parent */
85 server_unlock(default_options.pidfile);
90 initialize fd set we are waiting for.
92 static void fd_set_listening_sockets(void)
96 for (config = configs; config; config = config->next) {
97 if (config->fd < 0) /* for proxies */
99 fdset_add_fd(&fdset, &polldata, &fdset_used, &fdset_size, config->fd, LISTEN_FD, config);
103 static void fd_reset_listening_sockets(void)
107 for (config = configs; config; config = config->next) {
108 if (config->fd < 0) /* for proxies */
110 fdset_del_fd(&fdset, &polldata, &fdset_used, &fdset_size, config->fd);
112 fd_set_listening_sockets();
115 /* ------------------ */
116 static void afp_goaway(int sig)
121 #endif /* ! NO_DDP */
124 server_child_kill(server_children, CHILD_DSIFORK, sig);
129 LOG(log_note, logtype_afpd, "AFP Server shutting down on SIGTERM");
131 for (config = configs; config; config = config->next)
132 if (config->server_cleanup)
133 config->server_cleanup(config);
134 server_unlock(default_options.pidfile);
141 LOG(log_info, logtype_afpd, "disallowing logins");
145 /* w/ a configuration file, we can force a re-read if we want */
150 LOG(log_error, logtype_afpd, "afp_goaway: bad signal" );
155 static void child_handler(int sig _U_)
162 #define WAIT_ANY (-1)
163 #endif /* ! WAIT_ANY */
165 while ((pid = waitpid(WAIT_ANY, &status, WNOHANG)) > 0) {
166 for (i = 0; i < server_children->nforks; i++) {
167 if ((fd = server_child_remove(server_children, i, pid)) != -1) {
168 fdset_del_fd(&fdset, &polldata, &fdset_used, &fdset_size, fd);
173 if (WIFEXITED(status)) {
174 if (WEXITSTATUS(status))
175 LOG(log_info, logtype_afpd, "child[%d]: exited %d", pid, WEXITSTATUS(status));
177 LOG(log_info, logtype_afpd, "child[%d]: done", pid);
179 if (WIFSIGNALED(status))
180 LOG(log_info, logtype_afpd, "child[%d]: killed by signal %d", pid, WTERMSIG(status));
182 LOG(log_info, logtype_afpd, "child[%d]: died", pid);
187 int main(int ac, char **av)
199 set_auth_parameters( ac, av );
202 /* Log SIGBUS/SIGSEGV SBT */
205 /* Default log setup: log to syslog */
206 setuplog("default log_note");
208 afp_options_init(&default_options);
209 if (!afp_options_parse(ac, av, &default_options))
212 /* Save the user's current umask for use with CNID (and maybe some
213 * other things, too). */
214 default_options.save_mask = umask( default_options.umask );
216 switch(server_lock("afpd", default_options.pidfile,
217 default_options.flags & OPTION_DEBUG)) {
222 default: /* server */
227 /* install child handler for asp and dsi. we do this before afp_goaway
228 * as afp_goaway references stuff from here.
229 * XXX: this should really be setup after the initial connections. */
230 if (!(server_children = server_child_alloc(default_options.connections,
232 LOG(log_error, logtype_afpd, "main: server_child alloc: %s", strerror(errno) );
236 memset(&sv, 0, sizeof(sv));
237 /* linux at least up to 2.4.22 send a SIGXFZ for vfat fs,
238 even if the file is open with O_LARGEFILE ! */
240 sv.sa_handler = SIG_IGN;
241 sigemptyset( &sv.sa_mask );
242 if (sigaction(SIGXFSZ, &sv, NULL ) < 0 ) {
243 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
248 sv.sa_handler = child_handler;
249 sigemptyset( &sv.sa_mask );
250 sigaddset(&sv.sa_mask, SIGALRM);
251 sigaddset(&sv.sa_mask, SIGHUP);
252 sigaddset(&sv.sa_mask, SIGTERM);
253 sigaddset(&sv.sa_mask, SIGUSR1);
255 sv.sa_flags = SA_RESTART;
256 if ( sigaction( SIGCHLD, &sv, NULL ) < 0 ) {
257 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
261 sv.sa_handler = afp_goaway;
262 sigemptyset( &sv.sa_mask );
263 sigaddset(&sv.sa_mask, SIGALRM);
264 sigaddset(&sv.sa_mask, SIGTERM);
265 sigaddset(&sv.sa_mask, SIGHUP);
266 sigaddset(&sv.sa_mask, SIGCHLD);
267 sv.sa_flags = SA_RESTART;
268 if ( sigaction( SIGUSR1, &sv, NULL ) < 0 ) {
269 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
273 sigemptyset( &sv.sa_mask );
274 sigaddset(&sv.sa_mask, SIGALRM);
275 sigaddset(&sv.sa_mask, SIGTERM);
276 sigaddset(&sv.sa_mask, SIGUSR1);
277 sigaddset(&sv.sa_mask, SIGCHLD);
278 sv.sa_flags = SA_RESTART;
279 if ( sigaction( SIGHUP, &sv, NULL ) < 0 ) {
280 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
285 sigemptyset( &sv.sa_mask );
286 sigaddset(&sv.sa_mask, SIGALRM);
287 sigaddset(&sv.sa_mask, SIGHUP);
288 sigaddset(&sv.sa_mask, SIGUSR1);
289 sigaddset(&sv.sa_mask, SIGCHLD);
290 sv.sa_flags = SA_RESTART;
291 if ( sigaction( SIGTERM, &sv, NULL ) < 0 ) {
292 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
296 /* afpd.conf: not in config file: lockfile, connections, configfile
297 * preference: command-line provides defaults.
298 * config file over-writes defaults.
300 * we also need to make sure that killing afpd during startup
301 * won't leave any lingering registered names around.
305 sigaddset(&sigs, SIGALRM);
306 sigaddset(&sigs, SIGHUP);
307 sigaddset(&sigs, SIGUSR1);
309 /* don't block SIGTERM */
310 sigaddset(&sigs, SIGTERM);
312 sigaddset(&sigs, SIGCHLD);
314 pthread_sigmask(SIG_BLOCK, &sigs, NULL);
315 if (!(configs = configinit(&default_options))) {
316 LOG(log_error, logtype_afpd, "main: no servers configured");
319 pthread_sigmask(SIG_UNBLOCK, &sigs, NULL);
323 if (locktable_init() != 0)
326 if (rpc_init("127.0.0.1", 4701) != 0)
330 /* watch atp, dsi sockets and ipc parent/child file descriptor. */
331 fd_set_listening_sockets();
335 /* wait for an appleshare connection. parent remains in the loop
336 * while the children get handled by afp_over_{asp,dsi}. this is
337 * currently vulnerable to a denial-of-service attack if a
338 * connection is made without an actual login attempt being made
339 * afterwards. establishing timeouts for logins is a possible
342 LOG(log_maxdebug, logtype_afpd, "main: polling %i fds", fdset_used);
343 pthread_sigmask(SIG_UNBLOCK, &sigs, NULL);
344 ret = poll(fdset, fdset_used, -1);
345 pthread_sigmask(SIG_BLOCK, &sigs, NULL);
346 int saveerrno = errno;
352 LOG(log_info, logtype_afpd, "re-reading configuration file");
353 for (config = configs; config; config = config->next)
354 if (config->server_cleanup)
355 config->server_cleanup(config);
357 /* configfree close atp socket used for DDP tickle, there's an issue
359 configfree(configs, NULL);
360 if (!(configs = configinit(&default_options))) {
361 LOG(log_error, logtype_afpd, "config re-read: no servers configured");
364 fd_reset_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");