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