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