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>
34 #include <atalk/netatalk_conf.h>
36 #include "afp_config.h"
40 #include "afp_zeroconf.h"
42 #define AFP_LISTENERS 32
43 #define FDSET_SAFETY 5
45 unsigned char nologin = 0;
48 static server_child *server_children;
49 static sig_atomic_t reloadconfig = 0;
50 static sig_atomic_t gotsigchld = 0;
52 /* Two pointers to dynamic allocated arrays which store pollfds and associated data */
53 static struct pollfd *fdset;
54 static struct polldata *polldata;
55 static int fdset_size; /* current allocated size */
56 static int fdset_used; /* number of used elements */
57 static int disasociated_ipc_fd; /* disasociated sessions uses this fd for IPC */
59 static afp_child_t *dsi_start(AFPObj *obj, DSI *dsi, server_child *server_children);
61 /* This is registered with atexit() */
62 static void afp_exit(void)
64 if (parent_or_child == 0)
65 /* Only do this in the parent */
66 server_unlock(_PATH_AFPDLOCK);
71 initialize fd set we are waiting for.
73 static void fd_set_listening_sockets(const AFPObj *config)
77 for (dsi = config->dsi; dsi; dsi = dsi->next) {
78 fdset_add_fd(config->options.connections + AFP_LISTENERS + FDSET_SAFETY,
88 if (config->options.flags & OPTION_KEEPSESSIONS)
89 fdset_add_fd(config->options.connections + AFP_LISTENERS + FDSET_SAFETY,
99 static void fd_reset_listening_sockets(const AFPObj *config)
103 for (dsi = config->dsi; dsi; dsi = dsi->next) {
104 fdset_del_fd(&fdset, &polldata, &fdset_used, &fdset_size, dsi->serversock);
107 if (config->options.flags & OPTION_KEEPSESSIONS)
108 fdset_del_fd(&fdset, &polldata, &fdset_used, &fdset_size, disasociated_ipc_fd);
111 /* ------------------ */
112 static void afp_goaway(int sig)
120 LOG(log_note, logtype_afpd, "AFP Server shutting down on SIGTERM");
123 if (obj.options.flags & OPTION_KEEPSESSIONS) {
124 LOG(log_note, logtype_afpd, "AFP Server shutting down on SIGQUIT, NOT disconnecting clients");
126 LOG(log_note, logtype_afpd, "AFP Server shutting down on SIGQUIT");
132 server_child_kill(server_children, CHILD_DSIFORK, sig);
134 server_unlock(_PATH_AFPDLOCK);
141 LOG(log_info, logtype_afpd, "disallowing logins");
144 server_child_kill(server_children, CHILD_DSIFORK, sig);
148 /* w/ a configuration file, we can force a re-read if we want */
153 /* w/ a configuration file, we can force a re-read if we want */
158 LOG(log_error, logtype_afpd, "afp_goaway: bad signal" );
163 static void child_handler(void)
170 #define WAIT_ANY (-1)
171 #endif /* ! WAIT_ANY */
173 while ((pid = waitpid(WAIT_ANY, &status, WNOHANG)) > 0) {
174 for (i = 0; i < server_children->nforks; i++) {
175 if ((fd = server_child_remove(server_children, i, pid)) != -1) {
176 fdset_del_fd(&fdset, &polldata, &fdset_used, &fdset_size, fd);
181 if (WIFEXITED(status)) {
182 if (WEXITSTATUS(status))
183 LOG(log_info, logtype_afpd, "child[%d]: exited %d", pid, WEXITSTATUS(status));
185 LOG(log_info, logtype_afpd, "child[%d]: done", pid);
187 if (WIFSIGNALED(status))
188 LOG(log_info, logtype_afpd, "child[%d]: killed by signal %d", pid, WTERMSIG(status));
190 LOG(log_info, logtype_afpd, "child[%d]: died", pid);
195 static int setlimits(void)
199 if (getrlimit(RLIMIT_NOFILE, &rlim) != 0) {
200 LOG(log_warning, logtype_afpd, "setlimits: reading current limits failed: %s", strerror(errno));
203 if (rlim.rlim_cur != RLIM_INFINITY && rlim.rlim_cur < 65535) {
204 rlim.rlim_cur = 65535;
205 if (rlim.rlim_max != RLIM_INFINITY && rlim.rlim_max < 65535)
206 rlim.rlim_max = 65535;
207 if (setrlimit(RLIMIT_NOFILE, &rlim) != 0) {
208 LOG(log_warning, logtype_afpd, "setlimits: increasing limits failed: %s", strerror(errno));
215 int main(int ac, char **av)
223 /* Parse argv args and initialize default options */
224 afp_options_parse_cmdline(&obj, ac, av);
226 if (check_lockfile("afpd", _PATH_AFPDLOCK) != 0)
229 if (!(obj.options.flags & OPTION_DEBUG) && (daemonize(0, 0) != 0))
232 if (create_lockfile("afpd", _PATH_AFPDLOCK) != 0)
235 /* Log SIGBUS/SIGSEGV SBT */
239 if (afp_config_parse(&obj) != 0)
242 set_processname("afpd");
243 setuplog(obj.options.logconfig, obj.options.logfile);
245 /* Save the user's current umask */
246 obj.options.save_mask = umask(obj.options.umask);
248 /* install child handler for asp and dsi. we do this before afp_goaway
249 * as afp_goaway references stuff from here.
250 * XXX: this should really be setup after the initial connections. */
251 if (!(server_children = server_child_alloc(obj.options.connections, CHILD_NFORKS))) {
252 LOG(log_error, logtype_afpd, "main: server_child alloc: %s", strerror(errno) );
256 memset(&sv, 0, sizeof(sv));
257 /* linux at least up to 2.4.22 send a SIGXFZ for vfat fs,
258 even if the file is open with O_LARGEFILE ! */
260 sv.sa_handler = SIG_IGN;
261 sigemptyset( &sv.sa_mask );
262 if (sigaction(SIGXFSZ, &sv, NULL ) < 0 ) {
263 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
268 sv.sa_handler = afp_goaway; /* handler for all sigs */
270 sigemptyset( &sv.sa_mask );
271 sigaddset(&sv.sa_mask, SIGALRM);
272 sigaddset(&sv.sa_mask, SIGHUP);
273 sigaddset(&sv.sa_mask, SIGTERM);
274 sigaddset(&sv.sa_mask, SIGUSR1);
275 sigaddset(&sv.sa_mask, SIGQUIT);
276 sv.sa_flags = SA_RESTART;
277 if ( sigaction( SIGCHLD, &sv, NULL ) < 0 ) {
278 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
282 sigemptyset( &sv.sa_mask );
283 sigaddset(&sv.sa_mask, SIGALRM);
284 sigaddset(&sv.sa_mask, SIGTERM);
285 sigaddset(&sv.sa_mask, SIGHUP);
286 sigaddset(&sv.sa_mask, SIGCHLD);
287 sigaddset(&sv.sa_mask, SIGQUIT);
288 sv.sa_flags = SA_RESTART;
289 if ( sigaction( SIGUSR1, &sv, NULL ) < 0 ) {
290 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
294 sigemptyset( &sv.sa_mask );
295 sigaddset(&sv.sa_mask, SIGALRM);
296 sigaddset(&sv.sa_mask, SIGTERM);
297 sigaddset(&sv.sa_mask, SIGUSR1);
298 sigaddset(&sv.sa_mask, SIGCHLD);
299 sigaddset(&sv.sa_mask, SIGQUIT);
300 sv.sa_flags = SA_RESTART;
301 if ( sigaction( SIGHUP, &sv, NULL ) < 0 ) {
302 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
306 sigemptyset( &sv.sa_mask );
307 sigaddset(&sv.sa_mask, SIGALRM);
308 sigaddset(&sv.sa_mask, SIGHUP);
309 sigaddset(&sv.sa_mask, SIGUSR1);
310 sigaddset(&sv.sa_mask, SIGCHLD);
311 sigaddset(&sv.sa_mask, SIGQUIT);
312 sv.sa_flags = SA_RESTART;
313 if ( sigaction( SIGTERM, &sv, NULL ) < 0 ) {
314 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
318 sigemptyset( &sv.sa_mask );
319 sigaddset(&sv.sa_mask, SIGALRM);
320 sigaddset(&sv.sa_mask, SIGHUP);
321 sigaddset(&sv.sa_mask, SIGUSR1);
322 sigaddset(&sv.sa_mask, SIGCHLD);
323 sigaddset(&sv.sa_mask, SIGTERM);
324 sv.sa_flags = SA_RESTART;
325 if (sigaction(SIGQUIT, &sv, NULL ) < 0 ) {
326 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
330 /* afpd.conf: not in config file: lockfile, connections, configfile
331 * preference: command-line provides defaults.
332 * config file over-writes defaults.
334 * we also need to make sure that killing afpd during startup
335 * won't leave any lingering registered names around.
339 sigaddset(&sigs, SIGALRM);
340 sigaddset(&sigs, SIGHUP);
341 sigaddset(&sigs, SIGUSR1);
343 /* don't block SIGTERM */
344 sigaddset(&sigs, SIGTERM);
346 sigaddset(&sigs, SIGCHLD);
348 pthread_sigmask(SIG_BLOCK, &sigs, NULL);
349 if (configinit(&obj) != 0) {
350 LOG(log_error, logtype_afpd, "main: no servers configured");
353 pthread_sigmask(SIG_UNBLOCK, &sigs, NULL);
358 /* watch atp, dsi sockets and ipc parent/child file descriptor. */
360 if (obj.options.flags & OPTION_KEEPSESSIONS) {
361 LOG(log_note, logtype_afpd, "Activating continous service");
362 disasociated_ipc_fd = ipc_server_uds(_PATH_AFP_IPC);
365 fd_set_listening_sockets(&obj);
371 int fd[2]; /* we only use one, but server_child_add expects [2] */
375 /* wait for an appleshare connection. parent remains in the loop
376 * while the children get handled by afp_over_{asp,dsi}. this is
377 * currently vulnerable to a denial-of-service attack if a
378 * connection is made without an actual login attempt being made
379 * afterwards. establishing timeouts for logins is a possible
382 LOG(log_maxdebug, logtype_afpd, "main: polling %i fds", fdset_used);
383 pthread_sigmask(SIG_UNBLOCK, &sigs, NULL);
384 ret = poll(fdset, fdset_used, -1);
385 pthread_sigmask(SIG_BLOCK, &sigs, NULL);
397 fd_reset_listening_sockets(&obj);
399 LOG(log_info, logtype_afpd, "re-reading configuration file");
401 configfree(&obj, NULL);
402 if (configinit(&obj) != 0) {
403 LOG(log_error, logtype_afpd, "config re-read: no servers configured");
407 fd_set_listening_sockets(&obj);
421 LOG(log_error, logtype_afpd, "main: can't wait for input: %s", strerror(errno));
425 for (int i = 0; i < fdset_used; i++) {
426 if (fdset[i].revents & (POLLIN | POLLERR | POLLHUP)) {
427 switch (polldata[i].fdtype) {
430 if (child = dsi_start(&obj, (DSI *)polldata[i].data, server_children)) {
431 /* Add IPC fd to select fd set */
432 fdset_add_fd(obj.options.connections + AFP_LISTENERS + FDSET_SAFETY,
444 child = (afp_child_t *)polldata[i].data;
445 LOG(log_debug, logtype_afpd, "main: IPC request from child[%u]", child->pid);
447 if (ipc_server_read(server_children, child->ipc_fds[0]) != 0) {
448 fdset_del_fd(&fdset, &polldata, &fdset_used, &fdset_size, child->ipc_fds[0]);
449 close(child->ipc_fds[0]);
450 child->ipc_fds[0] = -1;
451 if ((obj.options.flags & OPTION_KEEPSESSIONS) && child->disasociated) {
452 LOG(log_note, logtype_afpd, "main: removing reattached child[%u]", child->pid);
453 server_child_remove(server_children, CHILD_DSIFORK, child->pid);
458 case DISASOCIATED_IPC_FD:
459 LOG(log_debug, logtype_afpd, "main: IPC reconnect request");
460 if ((fd[0] = accept(disasociated_ipc_fd, NULL, NULL)) == -1) {
461 LOG(log_error, logtype_afpd, "main: accept: %s", strerror(errno));
464 if (readt(fd[0], &pid, sizeof(pid_t), 0, 1) != sizeof(pid_t)) {
465 LOG(log_error, logtype_afpd, "main: readt: %s", strerror(errno));
469 LOG(log_note, logtype_afpd, "main: IPC reconnect from pid [%u]", pid);
470 if ((child = server_child_add(server_children, CHILD_DSIFORK, pid, fd)) == NULL) {
471 LOG(log_error, logtype_afpd, "main: server_child_add");
475 child->disasociated = 1;
476 fdset_add_fd(obj.options.connections + AFP_LISTENERS + FDSET_SAFETY,
487 LOG(log_debug, logtype_afpd, "main: IPC request for unknown type");
497 static afp_child_t *dsi_start(AFPObj *obj, DSI *dsi, server_child *server_children)
499 afp_child_t *child = NULL;
501 if (!(child = dsi_getsession(dsi,
503 obj->options.tickleval))) {
504 LOG(log_error, logtype_afpd, "dsi_start: session error: %s", strerror(errno));
509 if (parent_or_child == 1) {
510 configfree(obj, dsi);
511 obj->ipc_fd = child->ipc_fds[1];
512 close(child->ipc_fds[0]); /* Close parent IPC fd */
514 afp_over_dsi(obj); /* start a session */