]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/main.c
Fix build
[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     obj.argc = ac;
225     obj.argv = av;
226     if (afp_config_parse(&obj) != 0)
227         exit(EXITERR_CONF);
228
229     if (check_lockfile("afpd", _PATH_AFPDLOCK) != 0)
230         exit(EXITERR_SYS);
231
232     if (!(obj.options.flags & OPTION_DEBUG) && (daemonize(0, 0) != 0))
233         exit(EXITERR_SYS);
234
235     if (create_lockfile("afpd", _PATH_AFPDLOCK) != 0)
236         exit(EXITERR_SYS);
237
238     /* Log SIGBUS/SIGSEGV SBT */
239     fault_setup(NULL);
240     atexit(afp_exit);
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         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         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         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         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         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         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         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         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                 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 (parent_or_child == 1) {
507         configfree(obj, dsi);
508         obj->ipc_fd = child->ipc_fds[1];
509         close(child->ipc_fds[0]); /* Close parent IPC fd */
510         free(child);
511         afp_over_dsi(obj); /* start a session */
512         exit (0);
513     }
514
515     return child;
516 }