]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/main.c
Merge branch 'develop'
[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 #include "spotlight.h"
42
43 #define AFP_LISTENERS 32
44 #define FDSET_SAFETY  5
45
46 unsigned char nologin = 0;
47
48 static AFPObj obj;
49 static server_child *server_children;
50 static sig_atomic_t reloadconfig = 0;
51 static sig_atomic_t gotsigchld = 0;
52
53 /* Two pointers to dynamic allocated arrays which store pollfds and associated data */
54 static struct pollfd *fdset;
55 static struct polldata *polldata;
56 static int fdset_size;          /* current allocated size */
57 static int fdset_used;          /* number of used elements */
58 static int disasociated_ipc_fd; /* disasociated sessions uses this fd for IPC */
59
60 static afp_child_t *dsi_start(AFPObj *obj, DSI *dsi, server_child *server_children);
61
62 static void afp_exit(int ret)
63 {
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         _exit(0);
133         break;
134
135     case SIGUSR1 :
136         nologin++;
137         auth_unload();
138         LOG(log_info, logtype_afpd, "disallowing logins");        
139
140         if (server_children)
141             server_child_kill(server_children, CHILD_DSIFORK, sig);
142         break;
143
144     case SIGHUP :
145         /* w/ a configuration file, we can force a re-read if we want */
146         reloadconfig = 1;
147         break;
148
149     case SIGCHLD:
150         /* w/ a configuration file, we can force a re-read if we want */
151         gotsigchld = 1;
152         break;
153
154     default :
155         LOG(log_error, logtype_afpd, "afp_goaway: bad signal" );
156     }
157     return;
158 }
159
160 static void child_handler(void)
161 {
162     int fd;
163     int status, i;
164     pid_t pid;
165   
166 #ifndef WAIT_ANY
167 #define WAIT_ANY (-1)
168 #endif /* ! WAIT_ANY */
169
170     while ((pid = waitpid(WAIT_ANY, &status, WNOHANG)) > 0) {
171         for (i = 0; i < server_children->nforks; i++) {
172             if ((fd = server_child_remove(server_children, i, pid)) != -1) {
173                 fdset_del_fd(&fdset, &polldata, &fdset_used, &fdset_size, fd);        
174                 break;
175             }
176         }
177
178         if (WIFEXITED(status)) {
179             if (WEXITSTATUS(status))
180                 LOG(log_info, logtype_afpd, "child[%d]: exited %d", pid, WEXITSTATUS(status));
181             else
182                 LOG(log_info, logtype_afpd, "child[%d]: done", pid);
183         } else {
184             if (WIFSIGNALED(status))
185                 LOG(log_info, logtype_afpd, "child[%d]: killed by signal %d", pid, WTERMSIG(status));
186             else
187                 LOG(log_info, logtype_afpd, "child[%d]: died", pid);
188         }
189     }
190 }
191
192 static int setlimits(void)
193 {
194     struct rlimit rlim;
195
196     if (getrlimit(RLIMIT_NOFILE, &rlim) != 0) {
197         LOG(log_warning, logtype_afpd, "setlimits: reading current limits failed: %s", strerror(errno));
198         return -1;
199     }
200     if (rlim.rlim_cur != RLIM_INFINITY && rlim.rlim_cur < 65535) {
201         rlim.rlim_cur = 65535;
202         if (rlim.rlim_max != RLIM_INFINITY && rlim.rlim_max < 65535)
203             rlim.rlim_max = 65535;
204         if (setrlimit(RLIMIT_NOFILE, &rlim) != 0) {
205             LOG(log_warning, logtype_afpd, "setlimits: increasing limits failed: %s", strerror(errno));
206             return -1;
207         }
208     }
209     return 0;
210 }
211
212 int main(int ac, char **av)
213 {
214     fd_set              rfds;
215     void                *ipc;
216     struct sigaction    sv;
217     sigset_t            sigs;
218     int                 ret;
219
220     /* Parse argv args and initialize default options */
221     afp_options_parse_cmdline(&obj, ac, av);
222
223     if (!(obj.cmdlineflags & OPTION_DEBUG) && (daemonize(0, 0) != 0))
224         exit(EXITERR_SYS);
225
226     /* Log SIGBUS/SIGSEGV SBT */
227     fault_setup(NULL);
228
229     if (afp_config_parse(&obj, "afpd") != 0)
230         afp_exit(EXITERR_CONF);
231
232     /* Save the user's current umask */
233     obj.options.save_mask = umask(obj.options.umask);
234
235     /* install child handler for asp and dsi. we do this before afp_goaway
236      * as afp_goaway references stuff from here. 
237      * XXX: this should really be setup after the initial connections. */
238     if (!(server_children = server_child_alloc(obj.options.connections, CHILD_NFORKS))) {
239         LOG(log_error, logtype_afpd, "main: server_child alloc: %s", strerror(errno) );
240         afp_exit(EXITERR_SYS);
241     }
242     
243     sigemptyset(&sigs);
244     pthread_sigmask(SIG_SETMASK, &sigs, NULL);
245
246     memset(&sv, 0, sizeof(sv));    
247     /* linux at least up to 2.4.22 send a SIGXFZ for vfat fs,
248        even if the file is open with O_LARGEFILE ! */
249 #ifdef SIGXFSZ
250     sv.sa_handler = SIG_IGN;
251     sigemptyset( &sv.sa_mask );
252     if (sigaction(SIGXFSZ, &sv, NULL ) < 0 ) {
253         LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
254         afp_exit(EXITERR_SYS);
255     }
256 #endif
257
258     sv.sa_handler = SIG_IGN;
259     sigemptyset( &sv.sa_mask );
260     if (sigaction(SIGPIPE, &sv, NULL ) < 0 ) {
261         LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
262         afp_exit(EXITERR_SYS);
263     }
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     /* afp.conf:  not in config file: lockfile, 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 recon_ipc_fd;
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
394             fd_reset_listening_sockets(&obj);
395
396             LOG(log_info, logtype_afpd, "re-reading configuration file");
397
398             configfree(&obj, NULL);
399             afp_config_free(&obj);
400
401             if (afp_config_parse(&obj, "afpd") != 0)
402                 afp_exit(EXITERR_CONF);
403
404             if (configinit(&obj) != 0) {
405                 LOG(log_error, logtype_afpd, "config re-read: no servers configured");
406                 afp_exit(EXITERR_CONF);
407             }
408
409             fd_set_listening_sockets(&obj);
410
411             nologin = 0;
412             reloadconfig = 0;
413             errno = saveerrno;
414             continue;
415         }
416
417         if (ret == 0)
418             continue;
419         
420         if (ret < 0) {
421             if (errno == EINTR)
422                 continue;
423             LOG(log_error, logtype_afpd, "main: can't wait for input: %s", strerror(errno));
424             break;
425         }
426
427         for (int i = 0; i < fdset_used; i++) {
428             if (fdset[i].revents & (POLLIN | POLLERR | POLLHUP | POLLNVAL)) {
429                 switch (polldata[i].fdtype) {
430
431                 case LISTEN_FD:
432                     if (child = dsi_start(&obj, (DSI *)polldata[i].data, server_children)) {
433                         /* Add IPC fd to select fd set */
434                         fdset_add_fd(obj.options.connections + AFP_LISTENERS + FDSET_SAFETY,
435                                      &fdset,
436                                      &polldata,
437                                      &fdset_used,
438                                      &fdset_size,
439                                      child->ipc_fd,
440                                      IPC_FD,
441                                      child);
442                     }
443                     break;
444
445                 case IPC_FD:
446                     child = (afp_child_t *)polldata[i].data;
447                     LOG(log_debug, logtype_afpd, "main: IPC request from child[%u]", child->pid);
448
449                     if (ipc_server_read(server_children, child->ipc_fd) != 0) {
450                         fdset_del_fd(&fdset, &polldata, &fdset_used, &fdset_size, child->ipc_fd);
451                         close(child->ipc_fd);
452                         child->ipc_fd = -1;
453                         if ((obj.options.flags & OPTION_KEEPSESSIONS) && child->disasociated) {
454                             LOG(log_note, logtype_afpd, "main: removing reattached child[%u]", child->pid);
455                             server_child_remove(server_children, CHILD_DSIFORK, child->pid);
456                         }
457                     }
458                     break;
459
460                 case DISASOCIATED_IPC_FD:
461                     LOG(log_debug, logtype_afpd, "main: IPC reconnect request");
462                     if ((recon_ipc_fd = accept(disasociated_ipc_fd, NULL, NULL)) == -1) {
463                         LOG(log_error, logtype_afpd, "main: accept: %s", strerror(errno));
464                         break;
465                     }
466                     if (readt(recon_ipc_fd, &pid, sizeof(pid_t), 0, 1) != sizeof(pid_t)) {
467                         LOG(log_error, logtype_afpd, "main: readt: %s", strerror(errno));
468                         close(recon_ipc_fd);
469                         break;
470                     }
471                     LOG(log_note, logtype_afpd, "main: IPC reconnect from pid [%u]", pid);
472
473                     if ((child = server_child_add(server_children, CHILD_DSIFORK, pid, recon_ipc_fd)) == NULL) {
474                         LOG(log_error, logtype_afpd, "main: server_child_add");
475                         close(recon_ipc_fd);
476                         break;
477                     }
478                     child->disasociated = 1;
479                     fdset_add_fd(obj.options.connections + AFP_LISTENERS + FDSET_SAFETY,
480                                  &fdset,
481                                  &polldata,
482                                  &fdset_used,
483                                  &fdset_size,
484                                  recon_ipc_fd,
485                                  IPC_FD,
486                                  child);
487                     break;
488
489                 default:
490                     LOG(log_debug, logtype_afpd, "main: IPC request for unknown type");
491                     break;
492                 } /* switch */
493             }  /* if */
494         } /* for (i)*/
495     } /* while (1) */
496
497     return 0;
498 }
499
500 static afp_child_t *dsi_start(AFPObj *obj, DSI *dsi, server_child *server_children)
501 {
502     afp_child_t *child = NULL;
503
504     if (dsi_getsession(dsi, server_children, obj->options.tickleval, &child) != 0) {
505         LOG(log_error, logtype_afpd, "dsi_start: session error: %s", strerror(errno));
506         return NULL;
507     }
508
509     /* we've forked. */
510     if (child == NULL) {
511         configfree(obj, dsi);
512         afp_over_dsi(obj); /* start a session */
513         exit (0);
514     }
515
516     return child;
517 }