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(&obj, ac, av);
225 if (check_lockfile("afpd", _PATH_AFPDLOCK) != 0)
228 if (!(obj.options.flags & OPTION_DEBUG) && (daemonize(0, 0) != 0))
231 if (create_lockfile("afpd", _PATH_AFPDLOCK) != 0)
234 /* Log SIGBUS/SIGSEGV SBT */
238 if (afp_config_parse(&obj) != 0)
241 /* Save the user's current umask */
242 obj.options.save_mask = umask(obj.options.umask);
244 /* install child handler for asp and dsi. we do this before afp_goaway
245 * as afp_goaway references stuff from here.
246 * XXX: this should really be setup after the initial connections. */
247 if (!(server_children = server_child_alloc(obj.options.connections, CHILD_NFORKS))) {
248 LOG(log_error, logtype_afpd, "main: server_child alloc: %s", strerror(errno) );
252 memset(&sv, 0, sizeof(sv));
253 /* linux at least up to 2.4.22 send a SIGXFZ for vfat fs,
254 even if the file is open with O_LARGEFILE ! */
256 sv.sa_handler = SIG_IGN;
257 sigemptyset( &sv.sa_mask );
258 if (sigaction(SIGXFSZ, &sv, NULL ) < 0 ) {
259 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
264 sv.sa_handler = afp_goaway; /* handler for all sigs */
266 sigemptyset( &sv.sa_mask );
267 sigaddset(&sv.sa_mask, SIGALRM);
268 sigaddset(&sv.sa_mask, SIGHUP);
269 sigaddset(&sv.sa_mask, SIGTERM);
270 sigaddset(&sv.sa_mask, SIGUSR1);
271 sigaddset(&sv.sa_mask, SIGQUIT);
272 sv.sa_flags = SA_RESTART;
273 if ( sigaction( SIGCHLD, &sv, NULL ) < 0 ) {
274 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, SIGTERM);
281 sigaddset(&sv.sa_mask, SIGHUP);
282 sigaddset(&sv.sa_mask, SIGCHLD);
283 sigaddset(&sv.sa_mask, SIGQUIT);
284 sv.sa_flags = SA_RESTART;
285 if ( sigaction( SIGUSR1, &sv, NULL ) < 0 ) {
286 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
290 sigemptyset( &sv.sa_mask );
291 sigaddset(&sv.sa_mask, SIGALRM);
292 sigaddset(&sv.sa_mask, SIGTERM);
293 sigaddset(&sv.sa_mask, SIGUSR1);
294 sigaddset(&sv.sa_mask, SIGCHLD);
295 sigaddset(&sv.sa_mask, SIGQUIT);
296 sv.sa_flags = SA_RESTART;
297 if ( sigaction( SIGHUP, &sv, NULL ) < 0 ) {
298 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
302 sigemptyset( &sv.sa_mask );
303 sigaddset(&sv.sa_mask, SIGALRM);
304 sigaddset(&sv.sa_mask, SIGHUP);
305 sigaddset(&sv.sa_mask, SIGUSR1);
306 sigaddset(&sv.sa_mask, SIGCHLD);
307 sigaddset(&sv.sa_mask, SIGQUIT);
308 sv.sa_flags = SA_RESTART;
309 if ( sigaction( SIGTERM, &sv, NULL ) < 0 ) {
310 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
314 sigemptyset( &sv.sa_mask );
315 sigaddset(&sv.sa_mask, SIGALRM);
316 sigaddset(&sv.sa_mask, SIGHUP);
317 sigaddset(&sv.sa_mask, SIGUSR1);
318 sigaddset(&sv.sa_mask, SIGCHLD);
319 sigaddset(&sv.sa_mask, SIGTERM);
320 sv.sa_flags = SA_RESTART;
321 if (sigaction(SIGQUIT, &sv, NULL ) < 0 ) {
322 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
326 /* afpd.conf: not in config file: lockfile, connections, configfile
327 * preference: command-line provides defaults.
328 * config file over-writes defaults.
330 * we also need to make sure that killing afpd during startup
331 * won't leave any lingering registered names around.
335 sigaddset(&sigs, SIGALRM);
336 sigaddset(&sigs, SIGHUP);
337 sigaddset(&sigs, SIGUSR1);
339 /* don't block SIGTERM */
340 sigaddset(&sigs, SIGTERM);
342 sigaddset(&sigs, SIGCHLD);
344 pthread_sigmask(SIG_BLOCK, &sigs, NULL);
345 if (configinit(&obj) != 0) {
346 LOG(log_error, logtype_afpd, "main: no servers configured");
349 pthread_sigmask(SIG_UNBLOCK, &sigs, NULL);
354 /* watch atp, dsi sockets and ipc parent/child file descriptor. */
356 if (obj.options.flags & OPTION_KEEPSESSIONS) {
357 LOG(log_note, logtype_afpd, "Activating continous service");
358 disasociated_ipc_fd = ipc_server_uds(_PATH_AFP_IPC);
361 fd_set_listening_sockets(&obj);
367 int fd[2]; /* we only use one, but server_child_add expects [2] */
371 /* wait for an appleshare connection. parent remains in the loop
372 * while the children get handled by afp_over_{asp,dsi}. this is
373 * currently vulnerable to a denial-of-service attack if a
374 * connection is made without an actual login attempt being made
375 * afterwards. establishing timeouts for logins is a possible
378 LOG(log_maxdebug, logtype_afpd, "main: polling %i fds", fdset_used);
379 pthread_sigmask(SIG_UNBLOCK, &sigs, NULL);
380 ret = poll(fdset, fdset_used, -1);
381 pthread_sigmask(SIG_BLOCK, &sigs, NULL);
393 fd_reset_listening_sockets(&obj);
395 LOG(log_info, logtype_afpd, "re-reading configuration file");
397 configfree(&obj, NULL);
398 if (configinit(&obj) != 0) {
399 LOG(log_error, logtype_afpd, "config re-read: no servers configured");
403 fd_set_listening_sockets(&obj);
417 LOG(log_error, logtype_afpd, "main: can't wait for input: %s", strerror(errno));
421 for (int i = 0; i < fdset_used; i++) {
422 if (fdset[i].revents & (POLLIN | POLLERR | POLLHUP)) {
423 switch (polldata[i].fdtype) {
426 if (child = dsi_start(&obj, (DSI *)polldata[i].data, server_children)) {
427 /* Add IPC fd to select fd set */
428 fdset_add_fd(obj.options.connections + AFP_LISTENERS + FDSET_SAFETY,
440 child = (afp_child_t *)polldata[i].data;
441 LOG(log_debug, logtype_afpd, "main: IPC request from child[%u]", child->pid);
443 if (ipc_server_read(server_children, child->ipc_fds[0]) != 0) {
444 fdset_del_fd(&fdset, &polldata, &fdset_used, &fdset_size, child->ipc_fds[0]);
445 close(child->ipc_fds[0]);
446 child->ipc_fds[0] = -1;
447 if ((obj.options.flags & OPTION_KEEPSESSIONS) && child->disasociated) {
448 LOG(log_note, logtype_afpd, "main: removing reattached child[%u]", child->pid);
449 server_child_remove(server_children, CHILD_DSIFORK, child->pid);
454 case DISASOCIATED_IPC_FD:
455 LOG(log_debug, logtype_afpd, "main: IPC reconnect request");
456 if ((fd[0] = accept(disasociated_ipc_fd, NULL, NULL)) == -1) {
457 LOG(log_error, logtype_afpd, "main: accept: %s", strerror(errno));
460 if (readt(fd[0], &pid, sizeof(pid_t), 0, 1) != sizeof(pid_t)) {
461 LOG(log_error, logtype_afpd, "main: readt: %s", strerror(errno));
465 LOG(log_note, logtype_afpd, "main: IPC reconnect from pid [%u]", pid);
466 if ((child = server_child_add(server_children, CHILD_DSIFORK, pid, fd)) == NULL) {
467 LOG(log_error, logtype_afpd, "main: server_child_add");
471 child->disasociated = 1;
472 fdset_add_fd(obj.options.connections + AFP_LISTENERS + FDSET_SAFETY,
483 LOG(log_debug, logtype_afpd, "main: IPC request for unknown type");
493 static afp_child_t *dsi_start(AFPObj *obj, DSI *dsi, server_child *server_children)
495 afp_child_t *child = NULL;
497 if (!(child = dsi_getsession(dsi,
499 obj->options.tickleval))) {
500 LOG(log_error, logtype_afpd, "dsi_start: session error: %s", strerror(errno));
505 if (parent_or_child == 1) {
506 configfree(obj, dsi);
507 obj->ipc_fd = child->ipc_fds[1];
508 close(child->ipc_fds[0]); /* Close parent IPC fd */
510 afp_over_dsi(obj); /* start a session */