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