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 */
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);
99 static void fd_reset_listening_sockets(void)
103 for (config = configs; config; config = config->next) {
104 if (config->fd < 0) /* for proxies */
106 fdset_del_fd(&fdset, &polldata, &fdset_used, &fdset_size, config->fd);
110 /* ------------------ */
111 static void afp_goaway(int sig)
117 #endif /* ! NO_DDP */
122 LOG(log_note, logtype_afpd, "AFP Server shutting down on SIGTERM");
125 server_child_kill(server_children, CHILD_DSIFORK, sig);
127 for (config = configs; config; config = config->next)
128 if (config->server_cleanup)
129 config->server_cleanup(config);
130 server_unlock(default_options.pidfile);
135 LOG(log_note, logtype_afpd, "AFP Server shutting down on SIGQUIT, NOT disconnecting clients");
137 for (config = configs; config; config = config->next)
138 if (config->server_cleanup)
139 config->server_cleanup(config);
141 server_unlock(default_options.pidfile);
148 LOG(log_info, logtype_afpd, "disallowing logins");
151 server_child_kill(server_children, CHILD_DSIFORK, sig);
155 /* w/ a configuration file, we can force a re-read if we want */
160 LOG(log_error, logtype_afpd, "afp_goaway: bad signal" );
165 static void child_handler(int sig _U_)
172 #define WAIT_ANY (-1)
173 #endif /* ! WAIT_ANY */
175 while ((pid = waitpid(WAIT_ANY, &status, WNOHANG)) > 0) {
176 for (i = 0; i < server_children->nforks; i++) {
177 if ((fd = server_child_remove(server_children, i, pid)) != -1) {
178 fdset_del_fd(&fdset, &polldata, &fdset_used, &fdset_size, fd);
183 if (WIFEXITED(status)) {
184 if (WEXITSTATUS(status))
185 LOG(log_info, logtype_afpd, "child[%d]: exited %d", pid, WEXITSTATUS(status));
187 LOG(log_info, logtype_afpd, "child[%d]: done", pid);
189 if (WIFSIGNALED(status))
190 LOG(log_info, logtype_afpd, "child[%d]: killed by signal %d", pid, WTERMSIG(status));
192 LOG(log_info, logtype_afpd, "child[%d]: died", pid);
197 static int setlimits(void)
201 if (getrlimit(RLIMIT_NOFILE, &rlim) != 0) {
202 LOG(log_error, logtype_afpd, "setlimits: %s", strerror(errno));
205 if (rlim.rlim_cur != RLIM_INFINITY && rlim.rlim_cur < 65535) {
206 rlim.rlim_cur = 65535;
207 if (rlim.rlim_max != RLIM_INFINITY && rlim.rlim_max < 65535)
208 rlim.rlim_max = 65535;
209 if (setrlimit(RLIMIT_NOFILE, &rlim) != 0) {
210 LOG(log_error, logtype_afpd, "setlimits: %s", strerror(errno));
217 int main(int ac, char **av)
229 set_auth_parameters( ac, av );
232 /* Log SIGBUS/SIGSEGV SBT */
235 /* Default log setup: log to syslog */
236 setuplog("default log_note");
238 afp_options_init(&default_options);
239 if (!afp_options_parse(ac, av, &default_options))
242 /* Save the user's current umask for use with CNID (and maybe some
243 * other things, too). */
244 default_options.save_mask = umask( default_options.umask );
246 switch(server_lock("afpd", default_options.pidfile,
247 default_options.flags & OPTION_DEBUG)) {
252 default: /* server */
257 /* install child handler for asp and dsi. we do this before afp_goaway
258 * as afp_goaway references stuff from here.
259 * XXX: this should really be setup after the initial connections. */
260 if (!(server_children = server_child_alloc(default_options.connections,
262 LOG(log_error, logtype_afpd, "main: server_child alloc: %s", strerror(errno) );
266 memset(&sv, 0, sizeof(sv));
267 /* linux at least up to 2.4.22 send a SIGXFZ for vfat fs,
268 even if the file is open with O_LARGEFILE ! */
270 sv.sa_handler = SIG_IGN;
271 sigemptyset( &sv.sa_mask );
272 if (sigaction(SIGXFSZ, &sv, NULL ) < 0 ) {
273 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
278 sv.sa_handler = child_handler;
279 sigemptyset( &sv.sa_mask );
280 sigaddset(&sv.sa_mask, SIGALRM);
281 sigaddset(&sv.sa_mask, SIGHUP);
282 sigaddset(&sv.sa_mask, SIGTERM);
283 sigaddset(&sv.sa_mask, SIGUSR1);
284 sigaddset(&sv.sa_mask, SIGQUIT);
285 sv.sa_flags = SA_RESTART;
286 if ( sigaction( SIGCHLD, &sv, NULL ) < 0 ) {
287 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
291 sv.sa_handler = afp_goaway;
292 sigemptyset( &sv.sa_mask );
293 sigaddset(&sv.sa_mask, SIGALRM);
294 sigaddset(&sv.sa_mask, SIGTERM);
295 sigaddset(&sv.sa_mask, SIGHUP);
296 sigaddset(&sv.sa_mask, SIGCHLD);
297 sigaddset(&sv.sa_mask, SIGQUIT);
298 sv.sa_flags = SA_RESTART;
299 if ( sigaction( SIGUSR1, &sv, NULL ) < 0 ) {
300 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
304 sigemptyset( &sv.sa_mask );
305 sigaddset(&sv.sa_mask, SIGALRM);
306 sigaddset(&sv.sa_mask, SIGTERM);
307 sigaddset(&sv.sa_mask, SIGUSR1);
308 sigaddset(&sv.sa_mask, SIGCHLD);
309 sigaddset(&sv.sa_mask, SIGQUIT);
310 sv.sa_flags = SA_RESTART;
311 if ( sigaction( SIGHUP, &sv, NULL ) < 0 ) {
312 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
317 sigemptyset( &sv.sa_mask );
318 sigaddset(&sv.sa_mask, SIGALRM);
319 sigaddset(&sv.sa_mask, SIGHUP);
320 sigaddset(&sv.sa_mask, SIGUSR1);
321 sigaddset(&sv.sa_mask, SIGCHLD);
322 sigaddset(&sv.sa_mask, SIGQUIT);
323 sv.sa_flags = SA_RESTART;
324 if ( sigaction( SIGTERM, &sv, NULL ) < 0 ) {
325 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
329 sigemptyset( &sv.sa_mask );
330 sigaddset(&sv.sa_mask, SIGALRM);
331 sigaddset(&sv.sa_mask, SIGHUP);
332 sigaddset(&sv.sa_mask, SIGUSR1);
333 sigaddset(&sv.sa_mask, SIGCHLD);
334 sigaddset(&sv.sa_mask, SIGTERM);
335 sv.sa_flags = SA_RESTART;
336 if (sigaction(SIGQUIT, &sv, NULL ) < 0 ) {
337 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
341 /* afpd.conf: not in config file: lockfile, connections, configfile
342 * preference: command-line provides defaults.
343 * config file over-writes defaults.
345 * we also need to make sure that killing afpd during startup
346 * won't leave any lingering registered names around.
350 sigaddset(&sigs, SIGALRM);
351 sigaddset(&sigs, SIGHUP);
352 sigaddset(&sigs, SIGUSR1);
354 /* don't block SIGTERM */
355 sigaddset(&sigs, SIGTERM);
357 sigaddset(&sigs, SIGCHLD);
359 pthread_sigmask(SIG_BLOCK, &sigs, NULL);
360 if (!(configs = configinit(&default_options))) {
361 LOG(log_error, logtype_afpd, "main: no servers configured");
364 pthread_sigmask(SIG_UNBLOCK, &sigs, NULL);
369 /* watch atp, dsi sockets and ipc parent/child file descriptor. */
370 fd_set_listening_sockets();
377 /* wait for an appleshare connection. parent remains in the loop
378 * while the children get handled by afp_over_{asp,dsi}. this is
379 * currently vulnerable to a denial-of-service attack if a
380 * connection is made without an actual login attempt being made
381 * afterwards. establishing timeouts for logins is a possible
384 LOG(log_maxdebug, logtype_afpd, "main: polling %i fds", fdset_used);
385 pthread_sigmask(SIG_UNBLOCK, &sigs, NULL);
386 ret = poll(fdset, fdset_used, -1);
387 pthread_sigmask(SIG_BLOCK, &sigs, NULL);
388 int saveerrno = errno;
393 fd_reset_listening_sockets();
395 LOG(log_info, logtype_afpd, "re-reading configuration file");
396 for (config = configs; config; config = config->next)
397 if (config->server_cleanup)
398 config->server_cleanup(config);
400 /* configfree close atp socket used for DDP tickle, there's an issue
402 configfree(configs, NULL);
403 if (!(configs = configinit(&default_options))) {
404 LOG(log_error, logtype_afpd, "config re-read: no servers configured");
408 fd_set_listening_sockets();
422 LOG(log_error, logtype_afpd, "main: can't wait for input: %s", strerror(errno));
426 for (int i = 0; i < fdset_used; i++) {
427 if (fdset[i].revents & POLLIN) {
428 switch (polldata[i].fdtype) {
430 config = (AFPConfig *)polldata[i].data;
431 /* config->server_start is afp_config.c:dsi_start() for DSI */
432 if (child = config->server_start(config, configs, server_children)) {
433 /* Add IPC fd to select fd set */
434 fdset_add_fd(&fdset, &polldata, &fdset_used, &fdset_size, child->ipc_fds[0], IPC_FD, child);
438 child = (afp_child_t *)polldata[i].data;
439 LOG(log_debug, logtype_afpd, "main: IPC request from child[%u]", child->pid);
440 if ((ret = ipc_server_read(server_children, child->ipc_fds[0])) == 0) {
441 fdset_del_fd(&fdset, &polldata, &fdset_used, &fdset_size, child->ipc_fds[0]);
442 close(child->ipc_fds[0]);
443 child->ipc_fds[0] = -1;
447 LOG(log_debug, logtype_afpd, "main: IPC request for unknown type");