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