]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/main.c
Merge master
[netatalk.git] / etc / afpd / main.c
1 /*
2  * Copyright (c) 1990,1993 Regents of The University of Michigan.
3  * All Rights Reserved.  See COPYRIGHT.
4  */
5
6 #ifdef HAVE_CONFIG_H
7 #include "config.h"
8 #endif /* HAVE_CONFIG_H */
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <signal.h>
14 #include <sys/param.h>
15 #include <sys/uio.h>
16 #include <sys/time.h>
17 #include <sys/socket.h>
18 #include <sys/poll.h>
19 #include <errno.h>
20 #include <sys/wait.h>
21 #include <sys/resource.h>
22
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/locking.h>
34 #include <atalk/globals.h>
35
36 #include "event2/event.h"
37 #include "event2/http.h"
38 #include "event2/rpc.h"
39
40 #include "afp_config.h"
41 #include "status.h"
42 #include "fork.h"
43 #include "uam_auth.h"
44 #include "afp_zeroconf.h"
45
46 #ifdef TRU64
47 #include <sys/security.h>
48 #include <prot.h>
49 #include <sia.h>
50
51 static int argc = 0;
52 static char **argv = NULL;
53 #endif /* TRU64 */
54
55 #define AFP_LISTENERS 32
56 #define FDSET_SAFETY  5
57
58 unsigned char   nologin = 0;
59 struct afp_options default_options;
60
61 static AFPConfig *configs;
62 static server_child *server_children;
63 static sig_atomic_t reloadconfig = 0;
64 static sig_atomic_t gotsigchld = 0;
65
66 /* Two pointers to dynamic allocated arrays which store pollfds and associated data */
67 static struct pollfd *fdset;
68 static struct polldata *polldata;
69 static int fdset_size;          /* current allocated size */
70 static int fdset_used;          /* number of used elements */
71 static int disasociated_ipc_fd; /* disasociated sessions uses this fd for IPC */
72
73 #ifdef TRU64
74 void afp_get_cmdline( int *ac, char ***av)
75 {
76     *ac = argc;
77     *av = argv;
78 }
79 #endif /* TRU64 */
80
81 /* This is registered with atexit() */
82 static void afp_exit(void)
83 {
84     if (parent_or_child == 0)
85         /* Only do this in the parent */
86         server_unlock(default_options.pidfile);
87 }
88
89
90 /* ------------------
91    initialize fd set we are waiting for.
92 */
93 static void fd_set_listening_sockets(void)
94 {
95     AFPConfig   *config;
96
97     for (config = configs; config; config = config->next) {
98         if (config->fd < 0) /* for proxies */
99             continue;
100         fdset_add_fd(default_options.connections + AFP_LISTENERS + FDSET_SAFETY,
101                      &fdset,
102                      &polldata,
103                      &fdset_used,
104                      &fdset_size,
105                      config->fd,
106                      LISTEN_FD,
107                      config);
108     }
109
110     if (default_options.flags & OPTION_KEEPSESSIONS)
111         fdset_add_fd(default_options.connections + AFP_LISTENERS + FDSET_SAFETY,
112                      &fdset,
113                      &polldata,
114                      &fdset_used,
115                      &fdset_size,
116                      disasociated_ipc_fd,
117                      DISASOCIATED_IPC_FD,
118                      NULL);
119 }
120  
121 static void fd_reset_listening_sockets(void)
122 {
123     AFPConfig   *config;
124
125     for (config = configs; config; config = config->next) {
126         if (config->fd < 0) /* for proxies */
127             continue;
128         fdset_del_fd(&fdset, &polldata, &fdset_used, &fdset_size, config->fd);
129     }
130
131     if (default_options.flags & OPTION_KEEPSESSIONS)
132         fdset_del_fd(&fdset, &polldata, &fdset_used, &fdset_size, disasociated_ipc_fd);
133 }
134
135 /* ------------------ */
136 static void afp_goaway(int sig)
137 {
138     AFPConfig *config;
139
140     switch( sig ) {
141
142     case SIGTERM:
143     case SIGQUIT:
144         switch (sig) {
145         case SIGTERM:
146             LOG(log_note, logtype_afpd, "AFP Server shutting down on SIGTERM");
147             break;
148         case SIGQUIT:
149             if (default_options.flags & OPTION_KEEPSESSIONS) {
150                 LOG(log_note, logtype_afpd, "AFP Server shutting down on SIGQUIT, NOT disconnecting clients");
151             } else {
152                 LOG(log_note, logtype_afpd, "AFP Server shutting down on SIGQUIT");
153                 sig = SIGTERM;
154             }
155             break;
156         }
157         if (server_children)
158             server_child_kill(server_children, CHILD_DSIFORK, sig);
159
160         for (config = configs; config; config = config->next)
161             if (config->server_cleanup)
162                 config->server_cleanup(config);
163         server_unlock(default_options.pidfile);
164         exit(0);
165         break;
166
167     case SIGUSR1 :
168         nologin++;
169         auth_unload();
170         LOG(log_info, logtype_afpd, "disallowing logins");        
171
172         if (server_children)
173             server_child_kill(server_children, CHILD_DSIFORK, sig);
174         break;
175
176     case SIGHUP :
177         /* w/ a configuration file, we can force a re-read if we want */
178         reloadconfig = 1;
179         break;
180
181     case SIGCHLD:
182         /* w/ a configuration file, we can force a re-read if we want */
183         gotsigchld = 1;
184         break;
185
186     default :
187         LOG(log_error, logtype_afpd, "afp_goaway: bad signal" );
188     }
189     return;
190 }
191
192 static void child_handler(void)
193 {
194     int fd;
195     int status, i;
196     pid_t pid;
197   
198 #ifndef WAIT_ANY
199 #define WAIT_ANY (-1)
200 #endif /* ! WAIT_ANY */
201
202     while ((pid = waitpid(WAIT_ANY, &status, WNOHANG)) > 0) {
203         for (i = 0; i < server_children->nforks; i++) {
204             if ((fd = server_child_remove(server_children, i, pid)) != -1) {
205                 fdset_del_fd(&fdset, &polldata, &fdset_used, &fdset_size, fd);        
206                 break;
207             }
208         }
209
210         if (WIFEXITED(status)) {
211             if (WEXITSTATUS(status))
212                 LOG(log_info, logtype_afpd, "child[%d]: exited %d", pid, WEXITSTATUS(status));
213             else
214                 LOG(log_info, logtype_afpd, "child[%d]: done", pid);
215         } else {
216             if (WIFSIGNALED(status))
217                 LOG(log_info, logtype_afpd, "child[%d]: killed by signal %d", pid, WTERMSIG(status));
218             else
219                 LOG(log_info, logtype_afpd, "child[%d]: died", pid);
220         }
221     }
222 }
223
224 static int setlimits(void)
225 {
226     struct rlimit rlim;
227
228     if (getrlimit(RLIMIT_NOFILE, &rlim) != 0) {
229         LOG(log_error, logtype_afpd, "setlimits: %s", strerror(errno));
230         exit(1);
231     }
232     if (rlim.rlim_cur != RLIM_INFINITY && rlim.rlim_cur < 65535) {
233         rlim.rlim_cur = 65535;
234         if (rlim.rlim_max != RLIM_INFINITY && rlim.rlim_max < 65535)
235             rlim.rlim_max = 65535;
236         if (setrlimit(RLIMIT_NOFILE, &rlim) != 0) {
237             LOG(log_error, logtype_afpd, "setlimits: %s", strerror(errno));
238             exit(1);
239         }
240     }
241     return 0;
242 }
243
244 int main(int ac, char **av)
245 {
246     AFPConfig           *config;
247     fd_set              rfds;
248     void                *ipc;
249     struct sigaction    sv;
250     sigset_t            sigs;
251     int                 ret;
252
253 #ifdef TRU64
254     argc = ac;
255     argv = av;
256     set_auth_parameters( ac, av );
257 #endif /* TRU64 */
258
259     /* Parse argv args and initialize default options */
260     afp_options_init(&default_options);
261     if (!afp_options_parse(ac, av, &default_options))
262         exit(EXITERR_CONF);
263
264     if (check_lockfile("afpd", default_options.pidfile) != 0)
265         exit(EXITERR_SYS);
266
267     if (!(default_options.flags & OPTION_DEBUG) && (daemonize(0, 0) != 0))
268         exit(EXITERR_SYS);
269
270     if (create_lockfile("afpd", default_options.pidfile) != 0)
271         exit(EXITERR_SYS);
272
273     /* Log SIGBUS/SIGSEGV SBT */
274     fault_setup(NULL);
275
276     /* Default log setup: log to syslog */
277     set_processname("afpd");
278     setuplog("default log_note");
279
280     /* Save the user's current umask */
281     default_options.save_mask = umask( default_options.umask );
282
283     atexit(afp_exit);
284
285     /* install child handler for asp and dsi. we do this before afp_goaway
286      * as afp_goaway references stuff from here. 
287      * XXX: this should really be setup after the initial connections. */
288     if (!(server_children = server_child_alloc(default_options.connections,
289                             CHILD_NFORKS))) {
290         LOG(log_error, logtype_afpd, "main: server_child alloc: %s", strerror(errno) );
291         exit(EXITERR_SYS);
292     }
293
294     memset(&sv, 0, sizeof(sv));    
295     /* linux at least up to 2.4.22 send a SIGXFZ for vfat fs,
296        even if the file is open with O_LARGEFILE ! */
297 #ifdef SIGXFSZ
298     sv.sa_handler = SIG_IGN;
299     sigemptyset( &sv.sa_mask );
300     if (sigaction(SIGXFSZ, &sv, NULL ) < 0 ) {
301         LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
302         exit(EXITERR_SYS);
303     }
304 #endif
305     
306     sv.sa_handler = afp_goaway; /* handler for all sigs */
307
308     sigemptyset( &sv.sa_mask );
309     sigaddset(&sv.sa_mask, SIGALRM);
310     sigaddset(&sv.sa_mask, SIGHUP);
311     sigaddset(&sv.sa_mask, SIGTERM);
312     sigaddset(&sv.sa_mask, SIGUSR1);
313     sigaddset(&sv.sa_mask, SIGQUIT);    
314     sv.sa_flags = SA_RESTART;
315     if ( sigaction( SIGCHLD, &sv, NULL ) < 0 ) {
316         LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
317         exit(EXITERR_SYS);
318     }
319
320     sigemptyset( &sv.sa_mask );
321     sigaddset(&sv.sa_mask, SIGALRM);
322     sigaddset(&sv.sa_mask, SIGTERM);
323     sigaddset(&sv.sa_mask, SIGHUP);
324     sigaddset(&sv.sa_mask, SIGCHLD);
325     sigaddset(&sv.sa_mask, SIGQUIT);
326     sv.sa_flags = SA_RESTART;
327     if ( sigaction( SIGUSR1, &sv, NULL ) < 0 ) {
328         LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
329         exit(EXITERR_SYS);
330     }
331
332     sigemptyset( &sv.sa_mask );
333     sigaddset(&sv.sa_mask, SIGALRM);
334     sigaddset(&sv.sa_mask, SIGTERM);
335     sigaddset(&sv.sa_mask, SIGUSR1);
336     sigaddset(&sv.sa_mask, SIGCHLD);
337     sigaddset(&sv.sa_mask, SIGQUIT);
338     sv.sa_flags = SA_RESTART;
339     if ( sigaction( SIGHUP, &sv, NULL ) < 0 ) {
340         LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
341         exit(EXITERR_SYS);
342     }
343
344
345     sigemptyset( &sv.sa_mask );
346     sigaddset(&sv.sa_mask, SIGALRM);
347     sigaddset(&sv.sa_mask, SIGHUP);
348     sigaddset(&sv.sa_mask, SIGUSR1);
349     sigaddset(&sv.sa_mask, SIGCHLD);
350     sigaddset(&sv.sa_mask, SIGQUIT);
351     sv.sa_flags = SA_RESTART;
352     if ( sigaction( SIGTERM, &sv, NULL ) < 0 ) {
353         LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
354         exit(EXITERR_SYS);
355     }
356
357     sigemptyset( &sv.sa_mask );
358     sigaddset(&sv.sa_mask, SIGALRM);
359     sigaddset(&sv.sa_mask, SIGHUP);
360     sigaddset(&sv.sa_mask, SIGUSR1);
361     sigaddset(&sv.sa_mask, SIGCHLD);
362     sigaddset(&sv.sa_mask, SIGTERM);
363     sv.sa_flags = SA_RESTART;
364     if (sigaction(SIGQUIT, &sv, NULL ) < 0 ) {
365         LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
366         exit(EXITERR_SYS);
367     }
368
369     /* afpd.conf: not in config file: lockfile, connections, configfile
370      *            preference: command-line provides defaults.
371      *                        config file over-writes defaults.
372      *
373      * we also need to make sure that killing afpd during startup
374      * won't leave any lingering registered names around.
375      */
376
377     sigemptyset(&sigs);
378     sigaddset(&sigs, SIGALRM);
379     sigaddset(&sigs, SIGHUP);
380     sigaddset(&sigs, SIGUSR1);
381 #if 0
382     /* don't block SIGTERM */
383     sigaddset(&sigs, SIGTERM);
384 #endif
385     sigaddset(&sigs, SIGCHLD);
386
387     pthread_sigmask(SIG_BLOCK, &sigs, NULL);
388     if (!(configs = configinit(&default_options))) {
389         LOG(log_error, logtype_afpd, "main: no servers configured");
390         exit(EXITERR_CONF);
391     }
392     pthread_sigmask(SIG_UNBLOCK, &sigs, NULL);
393
394     /* Initialize */
395     cnid_init();
396     if (locktable_init("XXX") != 0)
397         exit(EXITERR_SYS);
398 #if 0
399     if (rpc_init("127.0.0.1", 4701) != 0)
400         exit(EXITERR_SYS);
401 #endif
402     
403     /* watch atp, dsi sockets and ipc parent/child file descriptor. */
404
405     if (default_options.flags & OPTION_KEEPSESSIONS) {
406         LOG(log_note, logtype_afpd, "Activating continous service");
407         disasociated_ipc_fd = ipc_server_uds(_PATH_AFP_IPC);
408     }
409
410     fd_set_listening_sockets();
411
412     /* set limits */
413     (void)setlimits();
414
415     afp_child_t *child;
416     int fd[2];  /* we only use one, but server_child_add expects [2] */
417     pid_t pid;
418     int saveerrno;
419
420     /* wait for an appleshare connection. parent remains in the loop
421      * while the children get handled by afp_over_{asp,dsi}.  this is
422      * currently vulnerable to a denial-of-service attack if a
423      * connection is made without an actual login attempt being made
424      * afterwards. establishing timeouts for logins is a possible 
425      * solution. */
426     while (1) {
427         LOG(log_maxdebug, logtype_afpd, "main: polling %i fds", fdset_used);
428         pthread_sigmask(SIG_UNBLOCK, &sigs, NULL);
429         ret = poll(fdset, fdset_used, -1);
430         pthread_sigmask(SIG_BLOCK, &sigs, NULL);
431         saveerrno = errno;
432
433         if (gotsigchld) {
434             gotsigchld = 0;
435             child_handler();
436             continue;
437         }
438
439         if (reloadconfig) {
440             nologin++;
441             auth_unload();
442             fd_reset_listening_sockets();
443
444             LOG(log_info, logtype_afpd, "re-reading configuration file");
445             for (config = configs; config; config = config->next)
446                 if (config->server_cleanup)
447                     config->server_cleanup(config);
448
449             /* configfree close atp socket used for DDP tickle, there's an issue
450              * with atp tid. */
451             configfree(configs, NULL);
452             if (!(configs = configinit(&default_options))) {
453                 LOG(log_error, logtype_afpd, "config re-read: no servers configured");
454                 exit(EXITERR_CONF);
455             }
456
457             fd_set_listening_sockets();
458
459             nologin = 0;
460             reloadconfig = 0;
461             errno = saveerrno;
462             continue;
463         }
464
465         if (ret == 0)
466             continue;
467         
468         if (ret < 0) {
469             if (errno == EINTR)
470                 continue;
471             LOG(log_error, logtype_afpd, "main: can't wait for input: %s", strerror(errno));
472             break;
473         }
474
475         for (int i = 0; i < fdset_used; i++) {
476             if (fdset[i].revents & (POLLIN | POLLERR | POLLHUP)) {
477                 switch (polldata[i].fdtype) {
478
479                 case LISTEN_FD:
480                     config = (AFPConfig *)polldata[i].data;
481                     /* config->server_start is afp_config.c:dsi_start() for DSI */
482                     if (child = config->server_start(config, configs, server_children)) {
483                         /* Add IPC fd to select fd set */
484                         fdset_add_fd(default_options.connections + AFP_LISTENERS + FDSET_SAFETY,
485                                      &fdset,
486                                      &polldata,
487                                      &fdset_used,
488                                      &fdset_size,
489                                      child->ipc_fds[0],
490                                      IPC_FD,
491                                      child);
492                     }
493                     break;
494
495                 case IPC_FD:
496                     child = (afp_child_t *)polldata[i].data;
497                     LOG(log_debug, logtype_afpd, "main: IPC request from child[%u]", child->pid);
498
499                     if (ipc_server_read(server_children, child->ipc_fds[0]) != 0) {
500                         fdset_del_fd(&fdset, &polldata, &fdset_used, &fdset_size, child->ipc_fds[0]);
501                         close(child->ipc_fds[0]);
502                         child->ipc_fds[0] = -1;
503                         if ((default_options.flags & OPTION_KEEPSESSIONS) && child->disasociated) {
504                             LOG(log_note, logtype_afpd, "main: removing reattached child[%u]", child->pid);
505                             server_child_remove(server_children, CHILD_DSIFORK, child->pid);
506                         }
507                     }
508                     break;
509
510                 case DISASOCIATED_IPC_FD:
511                     LOG(log_debug, logtype_afpd, "main: IPC reconnect request");
512                     if ((fd[0] = accept(disasociated_ipc_fd, NULL, NULL)) == -1) {
513                         LOG(log_error, logtype_afpd, "main: accept: %s", strerror(errno));
514                         break;
515                     }
516                     if (readt(fd[0], &pid, sizeof(pid_t), 0, 1) != sizeof(pid_t)) {
517                         LOG(log_error, logtype_afpd, "main: readt: %s", strerror(errno));
518                         close(fd[0]);
519                         break;
520                     }
521                     LOG(log_note, logtype_afpd, "main: IPC reconnect from pid [%u]", pid);
522                     if ((child = server_child_add(server_children, CHILD_DSIFORK, pid, fd)) == NULL) {
523                         LOG(log_error, logtype_afpd, "main: server_child_add");
524                         close(fd[0]);
525                         break;
526                     }
527                     child->disasociated = 1;
528                     fdset_add_fd(default_options.connections + AFP_LISTENERS + FDSET_SAFETY,
529                                  &fdset,
530                                  &polldata,
531                                  &fdset_used,
532                                  &fdset_size,
533                                  fd[0],
534                                  IPC_FD,
535                                  child);
536                     break;
537
538                 default:
539                     LOG(log_debug, logtype_afpd, "main: IPC request for unknown type");
540                     break;
541                 } /* switch */
542             }  /* if */
543         } /* for (i)*/
544     } /* while (1) */
545
546     return 0;
547 }