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);
110 fd_set_listening_sockets();
113 /* ------------------ */
114 static void afp_goaway(int sig)
117 server_child_kill(server_children, CHILD_DSIFORK, sig);
122 LOG(log_note, logtype_afpd, "AFP Server shutting down on SIGTERM");
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");
138 /* w/ a configuration file, we can force a re-read if we want */
143 LOG(log_error, logtype_afpd, "afp_goaway: bad signal" );
148 static void child_handler(int sig _U_)
155 #define WAIT_ANY (-1)
156 #endif /* ! WAIT_ANY */
158 while ((pid = waitpid(WAIT_ANY, &status, WNOHANG)) > 0) {
159 for (i = 0; i < server_children->nforks; i++) {
160 if ((fd = server_child_remove(server_children, i, pid)) != -1) {
161 fdset_del_fd(&fdset, &polldata, &fdset_used, &fdset_size, fd);
166 if (WIFEXITED(status)) {
167 if (WEXITSTATUS(status))
168 LOG(log_info, logtype_afpd, "child[%d]: exited %d", pid, WEXITSTATUS(status));
170 LOG(log_info, logtype_afpd, "child[%d]: done", pid);
172 if (WIFSIGNALED(status))
173 LOG(log_info, logtype_afpd, "child[%d]: killed by signal %d", pid, WTERMSIG(status));
175 LOG(log_info, logtype_afpd, "child[%d]: died", pid);
180 int main(int ac, char **av)
192 set_auth_parameters( ac, av );
195 /* Log SIGBUS/SIGSEGV SBT */
198 /* Default log setup: log to syslog */
199 setuplog("default log_note");
201 afp_options_init(&default_options);
202 if (!afp_options_parse(ac, av, &default_options))
205 /* Save the user's current umask for use with CNID (and maybe some
206 * other things, too). */
207 default_options.save_mask = umask( default_options.umask );
209 switch(server_lock("afpd", default_options.pidfile,
210 default_options.flags & OPTION_DEBUG)) {
215 default: /* server */
220 /* install child handler for asp and dsi. we do this before afp_goaway
221 * as afp_goaway references stuff from here.
222 * XXX: this should really be setup after the initial connections. */
223 if (!(server_children = server_child_alloc(default_options.connections,
225 LOG(log_error, logtype_afpd, "main: server_child alloc: %s", strerror(errno) );
229 memset(&sv, 0, sizeof(sv));
230 /* linux at least up to 2.4.22 send a SIGXFZ for vfat fs,
231 even if the file is open with O_LARGEFILE ! */
233 sv.sa_handler = SIG_IGN;
234 sigemptyset( &sv.sa_mask );
235 if (sigaction(SIGXFSZ, &sv, NULL ) < 0 ) {
236 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
241 sv.sa_handler = child_handler;
242 sigemptyset( &sv.sa_mask );
243 sigaddset(&sv.sa_mask, SIGALRM);
244 sigaddset(&sv.sa_mask, SIGHUP);
245 sigaddset(&sv.sa_mask, SIGTERM);
246 sigaddset(&sv.sa_mask, SIGUSR1);
248 sv.sa_flags = SA_RESTART;
249 if ( sigaction( SIGCHLD, &sv, NULL ) < 0 ) {
250 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
254 sv.sa_handler = afp_goaway;
255 sigemptyset( &sv.sa_mask );
256 sigaddset(&sv.sa_mask, SIGALRM);
257 sigaddset(&sv.sa_mask, SIGTERM);
258 sigaddset(&sv.sa_mask, SIGHUP);
259 sigaddset(&sv.sa_mask, SIGCHLD);
260 sv.sa_flags = SA_RESTART;
261 if ( sigaction( SIGUSR1, &sv, NULL ) < 0 ) {
262 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
266 sigemptyset( &sv.sa_mask );
267 sigaddset(&sv.sa_mask, SIGALRM);
268 sigaddset(&sv.sa_mask, SIGTERM);
269 sigaddset(&sv.sa_mask, SIGUSR1);
270 sigaddset(&sv.sa_mask, SIGCHLD);
271 sv.sa_flags = SA_RESTART;
272 if ( sigaction( SIGHUP, &sv, NULL ) < 0 ) {
273 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
278 sigemptyset( &sv.sa_mask );
279 sigaddset(&sv.sa_mask, SIGALRM);
280 sigaddset(&sv.sa_mask, SIGHUP);
281 sigaddset(&sv.sa_mask, SIGUSR1);
282 sigaddset(&sv.sa_mask, SIGCHLD);
283 sv.sa_flags = SA_RESTART;
284 if ( sigaction( SIGTERM, &sv, NULL ) < 0 ) {
285 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
289 /* afpd.conf: not in config file: lockfile, connections, configfile
290 * preference: command-line provides defaults.
291 * config file over-writes defaults.
293 * we also need to make sure that killing afpd during startup
294 * won't leave any lingering registered names around.
298 sigaddset(&sigs, SIGALRM);
299 sigaddset(&sigs, SIGHUP);
300 sigaddset(&sigs, SIGUSR1);
302 /* don't block SIGTERM */
303 sigaddset(&sigs, SIGTERM);
305 sigaddset(&sigs, SIGCHLD);
307 pthread_sigmask(SIG_BLOCK, &sigs, NULL);
308 if (!(configs = configinit(&default_options))) {
309 LOG(log_error, logtype_afpd, "main: no servers configured");
312 pthread_sigmask(SIG_UNBLOCK, &sigs, NULL);
316 if (locktable_init("XXX") != 0)
319 if (rpc_init("127.0.0.1", 4701) != 0)
323 /* watch atp, dsi sockets and ipc parent/child file descriptor. */
324 fd_set_listening_sockets();
328 /* wait for an appleshare connection. parent remains in the loop
329 * while the children get handled by afp_over_{asp,dsi}. this is
330 * currently vulnerable to a denial-of-service attack if a
331 * connection is made without an actual login attempt being made
332 * afterwards. establishing timeouts for logins is a possible
335 LOG(log_maxdebug, logtype_afpd, "main: polling %i fds", fdset_used);
336 pthread_sigmask(SIG_UNBLOCK, &sigs, NULL);
337 ret = poll(fdset, fdset_used, -1);
338 pthread_sigmask(SIG_BLOCK, &sigs, NULL);
339 int saveerrno = errno;
345 LOG(log_info, logtype_afpd, "re-reading configuration file");
346 for (config = configs; config; config = config->next)
347 if (config->server_cleanup)
348 config->server_cleanup(config);
350 /* configfree close atp socket used for DDP tickle, there's an issue
352 configfree(configs, NULL);
353 if (!(configs = configinit(&default_options))) {
354 LOG(log_error, logtype_afpd, "config re-read: no servers configured");
357 fd_reset_listening_sockets();
369 LOG(log_error, logtype_afpd, "main: can't wait for input: %s", strerror(errno));
373 for (int i = 0; i < fdset_used; i++) {
374 if (fdset[i].revents & POLLIN) {
375 switch (polldata[i].fdtype) {
377 config = (AFPConfig *)polldata[i].data;
378 /* config->server_start is afp_config.c:dsi_start() for DSI */
379 if (child = config->server_start(config, configs, server_children)) {
380 /* Add IPC fd to select fd set */
381 fdset_add_fd(&fdset, &polldata, &fdset_used, &fdset_size, child->ipc_fds[0], IPC_FD, child);
385 child = (afp_child_t *)polldata[i].data;
386 LOG(log_debug, logtype_afpd, "main: IPC request from child[%u]", child->pid);
387 if ((ret = ipc_server_read(server_children, child->ipc_fds[0])) == 0) {
388 fdset_del_fd(&fdset, &polldata, &fdset_used, &fdset_size, child->ipc_fds[0]);
389 close(child->ipc_fds[0]);
390 child->ipc_fds[0] = -1;
394 LOG(log_debug, logtype_afpd, "main: IPC request for unknown type");