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>
21 #include <sys/resource.h>
23 #include <atalk/logger.h>
24 #include <atalk/adouble.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/globals.h>
35 #include "afp_config.h"
39 #include "afp_zeroconf.h"
41 #define AFP_LISTENERS 32
42 #define FDSET_SAFETY 5
44 unsigned char nologin = 0;
47 static server_child *server_children;
48 static sig_atomic_t reloadconfig = 0;
49 static sig_atomic_t gotsigchld = 0;
51 /* Two pointers to dynamic allocated arrays which store pollfds and associated data */
52 static struct pollfd *fdset;
53 static struct polldata *polldata;
54 static int fdset_size; /* current allocated size */
55 static int fdset_used; /* number of used elements */
56 static int disasociated_ipc_fd; /* disasociated sessions uses this fd for IPC */
58 static afp_child_t *dsi_start(AFPObj *obj, DSI *dsi, server_child *server_children);
60 /* This is registered with atexit() */
61 static void afp_exit(void)
63 if (parent_or_child == 0)
64 /* Only do this in the parent */
65 server_unlock(_PATH_AFPDLOCK);
70 initialize fd set we are waiting for.
72 static void fd_set_listening_sockets(const AFPObj *config)
76 for (dsi = config->dsi; dsi; dsi = dsi->next) {
77 fdset_add_fd(config->options.connections + AFP_LISTENERS + FDSET_SAFETY,
87 if (config->options.flags & OPTION_KEEPSESSIONS)
88 fdset_add_fd(config->options.connections + AFP_LISTENERS + FDSET_SAFETY,
98 static void fd_reset_listening_sockets(const AFPObj *config)
102 for (dsi = config->dsi; dsi; dsi = dsi->next) {
103 fdset_del_fd(&fdset, &polldata, &fdset_used, &fdset_size, dsi->serversock);
106 if (config->options.flags & OPTION_KEEPSESSIONS)
107 fdset_del_fd(&fdset, &polldata, &fdset_used, &fdset_size, disasociated_ipc_fd);
110 /* ------------------ */
111 static void afp_goaway(int sig)
119 LOG(log_note, logtype_afpd, "AFP Server shutting down on SIGTERM");
122 if (obj.options.flags & OPTION_KEEPSESSIONS) {
123 LOG(log_note, logtype_afpd, "AFP Server shutting down on SIGQUIT, NOT disconnecting clients");
125 LOG(log_note, logtype_afpd, "AFP Server shutting down on SIGQUIT");
131 server_child_kill(server_children, CHILD_DSIFORK, sig);
133 server_unlock(_PATH_AFPDLOCK);
140 LOG(log_info, logtype_afpd, "disallowing logins");
143 server_child_kill(server_children, CHILD_DSIFORK, sig);
147 /* w/ a configuration file, we can force a re-read if we want */
152 /* w/ a configuration file, we can force a re-read if we want */
157 LOG(log_error, logtype_afpd, "afp_goaway: bad signal" );
162 static void child_handler(void)
169 #define WAIT_ANY (-1)
170 #endif /* ! WAIT_ANY */
172 while ((pid = waitpid(WAIT_ANY, &status, WNOHANG)) > 0) {
173 for (i = 0; i < server_children->nforks; i++) {
174 if ((fd = server_child_remove(server_children, i, pid)) != -1) {
175 fdset_del_fd(&fdset, &polldata, &fdset_used, &fdset_size, fd);
180 if (WIFEXITED(status)) {
181 if (WEXITSTATUS(status))
182 LOG(log_info, logtype_afpd, "child[%d]: exited %d", pid, WEXITSTATUS(status));
184 LOG(log_info, logtype_afpd, "child[%d]: done", pid);
186 if (WIFSIGNALED(status))
187 LOG(log_info, logtype_afpd, "child[%d]: killed by signal %d", pid, WTERMSIG(status));
189 LOG(log_info, logtype_afpd, "child[%d]: died", pid);
194 static int setlimits(void)
198 if (getrlimit(RLIMIT_NOFILE, &rlim) != 0) {
199 LOG(log_warning, logtype_afpd, "setlimits: reading current limits failed: %s", strerror(errno));
202 if (rlim.rlim_cur != RLIM_INFINITY && rlim.rlim_cur < 65535) {
203 rlim.rlim_cur = 65535;
204 if (rlim.rlim_max != RLIM_INFINITY && rlim.rlim_max < 65535)
205 rlim.rlim_max = 65535;
206 if (setrlimit(RLIMIT_NOFILE, &rlim) != 0) {
207 LOG(log_warning, logtype_afpd, "setlimits: increasing limits failed: %s", strerror(errno));
214 int main(int ac, char **av)
222 /* Parse argv args and initialize default options */
223 afp_options_parse_cmdline(ac, av);
224 if (afp_config_parse(&obj) != 0)
227 if (check_lockfile("afpd", _PATH_AFPDLOCK) != 0)
230 if (!(obj.options.flags & OPTION_DEBUG) && (daemonize(0, 0) != 0))
233 if (create_lockfile("afpd", _PATH_AFPDLOCK) != 0)
236 /* Log SIGBUS/SIGSEGV SBT */
240 /* Save the user's current umask */
241 obj.options.save_mask = umask(obj.options.umask);
243 /* install child handler for asp and dsi. we do this before afp_goaway
244 * as afp_goaway references stuff from here.
245 * XXX: this should really be setup after the initial connections. */
246 if (!(server_children = server_child_alloc(obj.options.connections, CHILD_NFORKS))) {
247 LOG(log_error, logtype_afpd, "main: server_child alloc: %s", strerror(errno) );
251 memset(&sv, 0, sizeof(sv));
252 /* linux at least up to 2.4.22 send a SIGXFZ for vfat fs,
253 even if the file is open with O_LARGEFILE ! */
255 sv.sa_handler = SIG_IGN;
256 sigemptyset( &sv.sa_mask );
257 if (sigaction(SIGXFSZ, &sv, NULL ) < 0 ) {
258 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
263 sv.sa_handler = afp_goaway; /* handler for all sigs */
265 sigemptyset( &sv.sa_mask );
266 sigaddset(&sv.sa_mask, SIGALRM);
267 sigaddset(&sv.sa_mask, SIGHUP);
268 sigaddset(&sv.sa_mask, SIGTERM);
269 sigaddset(&sv.sa_mask, SIGUSR1);
270 sigaddset(&sv.sa_mask, SIGQUIT);
271 sv.sa_flags = SA_RESTART;
272 if ( sigaction( SIGCHLD, &sv, NULL ) < 0 ) {
273 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
277 sigemptyset( &sv.sa_mask );
278 sigaddset(&sv.sa_mask, SIGALRM);
279 sigaddset(&sv.sa_mask, SIGTERM);
280 sigaddset(&sv.sa_mask, SIGHUP);
281 sigaddset(&sv.sa_mask, SIGCHLD);
282 sigaddset(&sv.sa_mask, SIGQUIT);
283 sv.sa_flags = SA_RESTART;
284 if ( sigaction( SIGUSR1, &sv, NULL ) < 0 ) {
285 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
289 sigemptyset( &sv.sa_mask );
290 sigaddset(&sv.sa_mask, SIGALRM);
291 sigaddset(&sv.sa_mask, SIGTERM);
292 sigaddset(&sv.sa_mask, SIGUSR1);
293 sigaddset(&sv.sa_mask, SIGCHLD);
294 sigaddset(&sv.sa_mask, SIGQUIT);
295 sv.sa_flags = SA_RESTART;
296 if ( sigaction( SIGHUP, &sv, NULL ) < 0 ) {
297 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
301 sigemptyset( &sv.sa_mask );
302 sigaddset(&sv.sa_mask, SIGALRM);
303 sigaddset(&sv.sa_mask, SIGHUP);
304 sigaddset(&sv.sa_mask, SIGUSR1);
305 sigaddset(&sv.sa_mask, SIGCHLD);
306 sigaddset(&sv.sa_mask, SIGQUIT);
307 sv.sa_flags = SA_RESTART;
308 if ( sigaction( SIGTERM, &sv, NULL ) < 0 ) {
309 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
313 sigemptyset( &sv.sa_mask );
314 sigaddset(&sv.sa_mask, SIGALRM);
315 sigaddset(&sv.sa_mask, SIGHUP);
316 sigaddset(&sv.sa_mask, SIGUSR1);
317 sigaddset(&sv.sa_mask, SIGCHLD);
318 sigaddset(&sv.sa_mask, SIGTERM);
319 sv.sa_flags = SA_RESTART;
320 if (sigaction(SIGQUIT, &sv, NULL ) < 0 ) {
321 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
325 /* afpd.conf: not in config file: lockfile, connections, configfile
326 * preference: command-line provides defaults.
327 * config file over-writes defaults.
329 * we also need to make sure that killing afpd during startup
330 * won't leave any lingering registered names around.
334 sigaddset(&sigs, SIGALRM);
335 sigaddset(&sigs, SIGHUP);
336 sigaddset(&sigs, SIGUSR1);
338 /* don't block SIGTERM */
339 sigaddset(&sigs, SIGTERM);
341 sigaddset(&sigs, SIGCHLD);
343 pthread_sigmask(SIG_BLOCK, &sigs, NULL);
344 if (configinit(&obj) != 0) {
345 LOG(log_error, logtype_afpd, "main: no servers configured");
348 pthread_sigmask(SIG_UNBLOCK, &sigs, NULL);
353 /* watch atp, dsi sockets and ipc parent/child file descriptor. */
355 if (obj.options.flags & OPTION_KEEPSESSIONS) {
356 LOG(log_note, logtype_afpd, "Activating continous service");
357 disasociated_ipc_fd = ipc_server_uds(_PATH_AFP_IPC);
360 fd_set_listening_sockets(&obj);
366 int fd[2]; /* we only use one, but server_child_add expects [2] */
370 /* wait for an appleshare connection. parent remains in the loop
371 * while the children get handled by afp_over_{asp,dsi}. this is
372 * currently vulnerable to a denial-of-service attack if a
373 * connection is made without an actual login attempt being made
374 * afterwards. establishing timeouts for logins is a possible
377 LOG(log_maxdebug, logtype_afpd, "main: polling %i fds", fdset_used);
378 pthread_sigmask(SIG_UNBLOCK, &sigs, NULL);
379 ret = poll(fdset, fdset_used, -1);
380 pthread_sigmask(SIG_BLOCK, &sigs, NULL);
392 fd_reset_listening_sockets(&obj);
394 LOG(log_info, logtype_afpd, "re-reading configuration file");
396 configfree(&obj, NULL);
397 if (configinit(&obj) != 0) {
398 LOG(log_error, logtype_afpd, "config re-read: no servers configured");
402 fd_set_listening_sockets(&obj);
416 LOG(log_error, logtype_afpd, "main: can't wait for input: %s", strerror(errno));
420 for (int i = 0; i < fdset_used; i++) {
421 if (fdset[i].revents & (POLLIN | POLLERR | POLLHUP)) {
422 switch (polldata[i].fdtype) {
425 if (child = dsi_start(&obj, (DSI *)polldata[i].data, server_children)) {
426 /* Add IPC fd to select fd set */
427 fdset_add_fd(obj.options.connections + AFP_LISTENERS + FDSET_SAFETY,
439 child = (afp_child_t *)polldata[i].data;
440 LOG(log_debug, logtype_afpd, "main: IPC request from child[%u]", child->pid);
442 if (ipc_server_read(server_children, child->ipc_fds[0]) != 0) {
443 fdset_del_fd(&fdset, &polldata, &fdset_used, &fdset_size, child->ipc_fds[0]);
444 close(child->ipc_fds[0]);
445 child->ipc_fds[0] = -1;
446 if ((obj.options.flags & OPTION_KEEPSESSIONS) && child->disasociated) {
447 LOG(log_note, logtype_afpd, "main: removing reattached child[%u]", child->pid);
448 server_child_remove(server_children, CHILD_DSIFORK, child->pid);
453 case DISASOCIATED_IPC_FD:
454 LOG(log_debug, logtype_afpd, "main: IPC reconnect request");
455 if ((fd[0] = accept(disasociated_ipc_fd, NULL, NULL)) == -1) {
456 LOG(log_error, logtype_afpd, "main: accept: %s", strerror(errno));
459 if (readt(fd[0], &pid, sizeof(pid_t), 0, 1) != sizeof(pid_t)) {
460 LOG(log_error, logtype_afpd, "main: readt: %s", strerror(errno));
464 LOG(log_note, logtype_afpd, "main: IPC reconnect from pid [%u]", pid);
465 if ((child = server_child_add(server_children, CHILD_DSIFORK, pid, fd)) == NULL) {
466 LOG(log_error, logtype_afpd, "main: server_child_add");
470 child->disasociated = 1;
471 fdset_add_fd(obj.options.connections + AFP_LISTENERS + FDSET_SAFETY,
482 LOG(log_debug, logtype_afpd, "main: IPC request for unknown type");
492 static afp_child_t *dsi_start(AFPObj *obj, DSI *dsi, server_child *server_children)
494 afp_child_t *child = NULL;
496 if (!(child = dsi_getsession(dsi,
498 obj->options.tickleval))) {
499 LOG(log_error, logtype_afpd, "dsi_start: session error: %s", strerror(errno));
504 if (parent_or_child == 1) {
505 configfree(obj, dsi);
506 obj->ipc_fd = child->ipc_fds[1];
507 close(child->ipc_fds[0]); /* Close parent IPC fd */
509 afp_over_dsi(obj); /* start a session */