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>
24 #include <atalk/adouble.h>
26 #include <netatalk/at.h>
27 #include <atalk/compat.h>
28 #include <atalk/dsi.h>
29 #include <atalk/atp.h>
30 #include <atalk/asp.h>
31 #include <atalk/afp.h>
32 #include <atalk/paths.h>
33 #include <atalk/util.h>
34 #include <atalk/server_child.h>
35 #include <atalk/server_ipc.h>
38 #include "afp_config.h"
42 #include "afp_zeroconf.h"
45 #include <sys/security.h>
50 static char **argv = NULL;
53 unsigned char nologin = 0;
55 struct afp_options default_options;
56 static AFPConfig *configs;
57 static server_child *server_children;
58 static sig_atomic_t reloadconfig = 0;
60 /* Two pointers to dynamic allocated arrays which store pollfds and associated data */
61 static struct pollfd *fdset;
62 static struct polldata *polldata;
63 static int fdset_size; /* current allocated size */
64 static int fdset_used; /* number of used elements */
68 void afp_get_cmdline( int *ac, char ***av)
75 /* This is registered with atexit() */
76 static void afp_exit(void)
78 if (parent_or_child == 0)
79 /* Only do this in the parent */
80 server_unlock(default_options.pidfile);
85 initialize fd set we are waiting for.
87 static void fd_set_listening_sockets(void)
91 for (config = configs; config; config = config->next) {
92 if (config->fd < 0) /* for proxies */
94 fdset_add_fd(&fdset, &polldata, &fdset_used, &fdset_size, config->fd, LISTEN_FD, config);
98 static void fd_reset_listening_sockets(void)
102 for (config = configs; config; config = config->next) {
103 if (config->fd < 0) /* for proxies */
105 fdset_del_fd(&fdset, &polldata, &fdset_used, &fdset_size, config->fd);
107 fd_set_listening_sockets();
110 /* ------------------ */
111 static void afp_goaway(int sig)
116 #endif /* ! NO_DDP */
119 server_child_kill(server_children, CHILD_DSIFORK, sig);
124 LOG(log_note, logtype_afpd, "AFP Server shutting down on SIGTERM");
126 for (config = configs; config; config = config->next)
127 if (config->server_cleanup)
128 config->server_cleanup(config);
129 server_unlock(default_options.pidfile);
136 LOG(log_info, logtype_afpd, "disallowing logins");
140 /* w/ a configuration file, we can force a re-read if we want */
145 LOG(log_error, logtype_afpd, "afp_goaway: bad signal" );
150 static void child_handler(int sig _U_)
157 #define WAIT_ANY (-1)
158 #endif /* ! WAIT_ANY */
160 while ((pid = waitpid(WAIT_ANY, &status, WNOHANG)) > 0) {
161 for (i = 0; i < server_children->nforks; i++) {
162 if ((fd = server_child_remove(server_children, i, pid)) != -1) {
163 fdset_del_fd(&fdset, &polldata, &fdset_used, &fdset_size, fd);
168 if (WIFEXITED(status)) {
169 if (WEXITSTATUS(status))
170 LOG(log_info, logtype_afpd, "child[%d]: exited %d", pid, WEXITSTATUS(status));
172 LOG(log_info, logtype_afpd, "child[%d]: done", pid);
174 if (WIFSIGNALED(status))
175 LOG(log_info, logtype_afpd, "child[%d]: killed by signal %d", pid, WTERMSIG(status));
177 LOG(log_info, logtype_afpd, "child[%d]: died", pid);
182 int main(int ac, char **av)
194 set_auth_parameters( ac, av );
197 /* Log SIGBUS/SIGSEGV SBT */
200 /* Default log setup: log to syslog */
201 setuplog("default log_note");
203 afp_options_init(&default_options);
204 if (!afp_options_parse(ac, av, &default_options))
207 /* Save the user's current umask for use with CNID (and maybe some
208 * other things, too). */
209 default_options.save_mask = umask( default_options.umask );
211 switch(server_lock("afpd", default_options.pidfile,
212 default_options.flags & OPTION_DEBUG)) {
217 default: /* server */
222 /* install child handler for asp and dsi. we do this before afp_goaway
223 * as afp_goaway references stuff from here.
224 * XXX: this should really be setup after the initial connections. */
225 if (!(server_children = server_child_alloc(default_options.connections,
227 LOG(log_error, logtype_afpd, "main: server_child alloc: %s", strerror(errno) );
231 memset(&sv, 0, sizeof(sv));
232 /* linux at least up to 2.4.22 send a SIGXFZ for vfat fs,
233 even if the file is open with O_LARGEFILE ! */
235 sv.sa_handler = SIG_IGN;
236 sigemptyset( &sv.sa_mask );
237 if (sigaction(SIGXFSZ, &sv, NULL ) < 0 ) {
238 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
243 sv.sa_handler = child_handler;
244 sigemptyset( &sv.sa_mask );
245 sigaddset(&sv.sa_mask, SIGALRM);
246 sigaddset(&sv.sa_mask, SIGHUP);
247 sigaddset(&sv.sa_mask, SIGTERM);
248 sigaddset(&sv.sa_mask, SIGUSR1);
250 sv.sa_flags = SA_RESTART;
251 if ( sigaction( SIGCHLD, &sv, NULL ) < 0 ) {
252 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
256 sv.sa_handler = afp_goaway;
257 sigemptyset( &sv.sa_mask );
258 sigaddset(&sv.sa_mask, SIGALRM);
259 sigaddset(&sv.sa_mask, SIGTERM);
260 sigaddset(&sv.sa_mask, SIGHUP);
261 sigaddset(&sv.sa_mask, SIGCHLD);
262 sv.sa_flags = SA_RESTART;
263 if ( sigaction( SIGUSR1, &sv, NULL ) < 0 ) {
264 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
268 sigemptyset( &sv.sa_mask );
269 sigaddset(&sv.sa_mask, SIGALRM);
270 sigaddset(&sv.sa_mask, SIGTERM);
271 sigaddset(&sv.sa_mask, SIGUSR1);
272 sigaddset(&sv.sa_mask, SIGCHLD);
273 sv.sa_flags = SA_RESTART;
274 if ( sigaction( SIGHUP, &sv, NULL ) < 0 ) {
275 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
280 sigemptyset( &sv.sa_mask );
281 sigaddset(&sv.sa_mask, SIGALRM);
282 sigaddset(&sv.sa_mask, SIGHUP);
283 sigaddset(&sv.sa_mask, SIGUSR1);
284 sigaddset(&sv.sa_mask, SIGCHLD);
285 sv.sa_flags = SA_RESTART;
286 if ( sigaction( SIGTERM, &sv, NULL ) < 0 ) {
287 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
291 /* afpd.conf: not in config file: lockfile, connections, configfile
292 * preference: command-line provides defaults.
293 * config file over-writes defaults.
295 * we also need to make sure that killing afpd during startup
296 * won't leave any lingering registered names around.
300 sigaddset(&sigs, SIGALRM);
301 sigaddset(&sigs, SIGHUP);
302 sigaddset(&sigs, SIGUSR1);
304 /* don't block SIGTERM */
305 sigaddset(&sigs, SIGTERM);
307 sigaddset(&sigs, SIGCHLD);
309 pthread_sigmask(SIG_BLOCK, &sigs, NULL);
310 if (!(configs = configinit(&default_options))) {
311 LOG(log_error, logtype_afpd, "main: no servers configured");
314 pthread_sigmask(SIG_UNBLOCK, &sigs, NULL);
319 /* watch atp, dsi sockets and ipc parent/child file descriptor. */
320 fd_set_listening_sockets();
324 /* wait for an appleshare connection. parent remains in the loop
325 * while the children get handled by afp_over_{asp,dsi}. this is
326 * currently vulnerable to a denial-of-service attack if a
327 * connection is made without an actual login attempt being made
328 * afterwards. establishing timeouts for logins is a possible
331 LOG(log_maxdebug, logtype_afpd, "main: polling %i fds", fdset_used);
332 pthread_sigmask(SIG_UNBLOCK, &sigs, NULL);
333 ret = poll(fdset, fdset_used, -1);
334 pthread_sigmask(SIG_BLOCK, &sigs, NULL);
335 int saveerrno = errno;
341 LOG(log_info, logtype_afpd, "re-reading configuration file");
342 for (config = configs; config; config = config->next)
343 if (config->server_cleanup)
344 config->server_cleanup(config);
346 /* configfree close atp socket used for DDP tickle, there's an issue
348 configfree(configs, NULL);
349 if (!(configs = configinit(&default_options))) {
350 LOG(log_error, logtype_afpd, "config re-read: no servers configured");
353 fd_reset_listening_sockets();
365 LOG(log_error, logtype_afpd, "main: can't wait for input: %s", strerror(errno));
369 for (int i = 0; i < fdset_used; i++) {
370 if (fdset[i].revents & POLLIN) {
371 switch (polldata[i].fdtype) {
373 config = (AFPConfig *)polldata[i].data;
374 /* config->server_start is afp_config.c:dsi_start() for DSI */
375 if (child = config->server_start(config, configs, server_children)) {
376 /* Add IPC fd to select fd set */
377 fdset_add_fd(&fdset, &polldata, &fdset_used, &fdset_size, child->ipc_fds[0], IPC_FD, child);
381 child = (afp_child_t *)polldata[i].data;
382 LOG(log_debug, logtype_afpd, "main: IPC request from child[%u]", child->pid);
383 if ((ret = ipc_server_read(server_children, child->ipc_fds[0])) == 0) {
384 fdset_del_fd(&fdset, &polldata, &fdset_used, &fdset_size, child->ipc_fds[0]);
385 close(child->ipc_fds[0]);
386 child->ipc_fds[0] = -1;
390 LOG(log_debug, logtype_afpd, "main: IPC request for unknown type");