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