]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/main.c
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 #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 = afp_goaway; /* handler for all sigs */
259
260     sigemptyset( &sv.sa_mask );
261     sigaddset(&sv.sa_mask, SIGALRM);
262     sigaddset(&sv.sa_mask, SIGHUP);
263     sigaddset(&sv.sa_mask, SIGTERM);
264     sigaddset(&sv.sa_mask, SIGUSR1);
265     sigaddset(&sv.sa_mask, SIGQUIT);    
266     sv.sa_flags = SA_RESTART;
267     if ( sigaction( SIGCHLD, &sv, NULL ) < 0 ) {
268         LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
269         afp_exit(EXITERR_SYS);
270     }
271
272     sigemptyset( &sv.sa_mask );
273     sigaddset(&sv.sa_mask, SIGALRM);
274     sigaddset(&sv.sa_mask, SIGTERM);
275     sigaddset(&sv.sa_mask, SIGHUP);
276     sigaddset(&sv.sa_mask, SIGCHLD);
277     sigaddset(&sv.sa_mask, SIGQUIT);
278     sv.sa_flags = SA_RESTART;
279     if ( sigaction( SIGUSR1, &sv, NULL ) < 0 ) {
280         LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
281         afp_exit(EXITERR_SYS);
282     }
283
284     sigemptyset( &sv.sa_mask );
285     sigaddset(&sv.sa_mask, SIGALRM);
286     sigaddset(&sv.sa_mask, SIGTERM);
287     sigaddset(&sv.sa_mask, SIGUSR1);
288     sigaddset(&sv.sa_mask, SIGCHLD);
289     sigaddset(&sv.sa_mask, SIGQUIT);
290     sv.sa_flags = SA_RESTART;
291     if ( sigaction( SIGHUP, &sv, NULL ) < 0 ) {
292         LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
293         afp_exit(EXITERR_SYS);
294     }
295
296     sigemptyset( &sv.sa_mask );
297     sigaddset(&sv.sa_mask, SIGALRM);
298     sigaddset(&sv.sa_mask, SIGHUP);
299     sigaddset(&sv.sa_mask, SIGUSR1);
300     sigaddset(&sv.sa_mask, SIGCHLD);
301     sigaddset(&sv.sa_mask, SIGQUIT);
302     sv.sa_flags = SA_RESTART;
303     if ( sigaction( SIGTERM, &sv, NULL ) < 0 ) {
304         LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
305         afp_exit(EXITERR_SYS);
306     }
307
308     sigemptyset( &sv.sa_mask );
309     sigaddset(&sv.sa_mask, SIGALRM);
310     sigaddset(&sv.sa_mask, SIGHUP);
311     sigaddset(&sv.sa_mask, SIGUSR1);
312     sigaddset(&sv.sa_mask, SIGCHLD);
313     sigaddset(&sv.sa_mask, SIGTERM);
314     sv.sa_flags = SA_RESTART;
315     if (sigaction(SIGQUIT, &sv, NULL ) < 0 ) {
316         LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
317         afp_exit(EXITERR_SYS);
318     }
319
320     /* afp.conf:  not in config file: lockfile, configfile
321      *            preference: command-line provides defaults.
322      *                        config file over-writes defaults.
323      *
324      * we also need to make sure that killing afpd during startup
325      * won't leave any lingering registered names around.
326      */
327
328     sigemptyset(&sigs);
329     sigaddset(&sigs, SIGALRM);
330     sigaddset(&sigs, SIGHUP);
331     sigaddset(&sigs, SIGUSR1);
332 #if 0
333     /* don't block SIGTERM */
334     sigaddset(&sigs, SIGTERM);
335 #endif
336     sigaddset(&sigs, SIGCHLD);
337
338     pthread_sigmask(SIG_BLOCK, &sigs, NULL);
339     if (configinit(&obj) != 0) {
340         LOG(log_error, logtype_afpd, "main: no servers configured");
341         afp_exit(EXITERR_CONF);
342     }
343     pthread_sigmask(SIG_UNBLOCK, &sigs, NULL);
344
345     /* Initialize */
346     cnid_init();
347     sl_mod_load(_PATH_AFPDUAMPATH "mod_spotlight.so");
348
349     /* watch atp, dsi sockets and ipc parent/child file descriptor. */
350
351     if (obj.options.flags & OPTION_KEEPSESSIONS) {
352         LOG(log_note, logtype_afpd, "Activating continous service");
353         disasociated_ipc_fd = ipc_server_uds(_PATH_AFP_IPC);
354     }
355
356     fd_set_listening_sockets(&obj);
357
358     /* set limits */
359     (void)setlimits();
360
361     afp_child_t *child;
362     int recon_ipc_fd;
363     pid_t pid;
364     int saveerrno;
365
366     /* wait for an appleshare connection. parent remains in the loop
367      * while the children get handled by afp_over_{asp,dsi}.  this is
368      * currently vulnerable to a denial-of-service attack if a
369      * connection is made without an actual login attempt being made
370      * afterwards. establishing timeouts for logins is a possible 
371      * solution. */
372     while (1) {
373         LOG(log_maxdebug, logtype_afpd, "main: polling %i fds", fdset_used);
374         pthread_sigmask(SIG_UNBLOCK, &sigs, NULL);
375         ret = poll(fdset, fdset_used, -1);
376         pthread_sigmask(SIG_BLOCK, &sigs, NULL);
377         saveerrno = errno;
378
379         if (gotsigchld) {
380             gotsigchld = 0;
381             child_handler();
382             continue;
383         }
384
385         if (reloadconfig) {
386             nologin++;
387             auth_unload();
388             fd_reset_listening_sockets(&obj);
389
390             LOG(log_info, logtype_afpd, "re-reading configuration file");
391
392             configfree(&obj, NULL);
393             if (configinit(&obj) != 0) {
394                 LOG(log_error, logtype_afpd, "config re-read: no servers configured");
395                 afp_exit(EXITERR_CONF);
396             }
397
398             fd_set_listening_sockets(&obj);
399
400             nologin = 0;
401             reloadconfig = 0;
402             errno = saveerrno;
403             continue;
404         }
405
406         if (ret == 0)
407             continue;
408         
409         if (ret < 0) {
410             if (errno == EINTR)
411                 continue;
412             LOG(log_error, logtype_afpd, "main: can't wait for input: %s", strerror(errno));
413             break;
414         }
415
416         for (int i = 0; i < fdset_used; i++) {
417             if (fdset[i].revents & (POLLIN | POLLERR | POLLHUP | POLLNVAL)) {
418                 switch (polldata[i].fdtype) {
419
420                 case LISTEN_FD:
421                     if (child = dsi_start(&obj, (DSI *)polldata[i].data, server_children)) {
422                         /* Add IPC fd to select fd set */
423                         fdset_add_fd(obj.options.connections + AFP_LISTENERS + FDSET_SAFETY,
424                                      &fdset,
425                                      &polldata,
426                                      &fdset_used,
427                                      &fdset_size,
428                                      child->ipc_fd,
429                                      IPC_FD,
430                                      child);
431                     }
432                     break;
433
434                 case IPC_FD:
435                     child = (afp_child_t *)polldata[i].data;
436                     LOG(log_debug, logtype_afpd, "main: IPC request from child[%u]", child->pid);
437
438                     if (ipc_server_read(server_children, child->ipc_fd) != 0) {
439                         fdset_del_fd(&fdset, &polldata, &fdset_used, &fdset_size, child->ipc_fd);
440                         close(child->ipc_fd);
441                         child->ipc_fd = -1;
442                         if ((obj.options.flags & OPTION_KEEPSESSIONS) && child->disasociated) {
443                             LOG(log_note, logtype_afpd, "main: removing reattached child[%u]", child->pid);
444                             server_child_remove(server_children, CHILD_DSIFORK, child->pid);
445                         }
446                     }
447                     break;
448
449                 case DISASOCIATED_IPC_FD:
450                     LOG(log_debug, logtype_afpd, "main: IPC reconnect request");
451                     if ((recon_ipc_fd = accept(disasociated_ipc_fd, NULL, NULL)) == -1) {
452                         LOG(log_error, logtype_afpd, "main: accept: %s", strerror(errno));
453                         break;
454                     }
455                     if (readt(recon_ipc_fd, &pid, sizeof(pid_t), 0, 1) != sizeof(pid_t)) {
456                         LOG(log_error, logtype_afpd, "main: readt: %s", strerror(errno));
457                         close(recon_ipc_fd);
458                         break;
459                     }
460                     LOG(log_note, logtype_afpd, "main: IPC reconnect from pid [%u]", pid);
461
462                     if ((child = server_child_add(server_children, CHILD_DSIFORK, pid, recon_ipc_fd)) == NULL) {
463                         LOG(log_error, logtype_afpd, "main: server_child_add");
464                         close(recon_ipc_fd);
465                         break;
466                     }
467                     child->disasociated = 1;
468                     fdset_add_fd(obj.options.connections + AFP_LISTENERS + FDSET_SAFETY,
469                                  &fdset,
470                                  &polldata,
471                                  &fdset_used,
472                                  &fdset_size,
473                                  recon_ipc_fd,
474                                  IPC_FD,
475                                  child);
476                     break;
477
478                 default:
479                     LOG(log_debug, logtype_afpd, "main: IPC request for unknown type");
480                     break;
481                 } /* switch */
482             }  /* if */
483         } /* for (i)*/
484     } /* while (1) */
485
486     return 0;
487 }
488
489 static afp_child_t *dsi_start(AFPObj *obj, DSI *dsi, server_child *server_children)
490 {
491     afp_child_t *child = NULL;
492
493     if (dsi_getsession(dsi, server_children, obj->options.tickleval, &child) != 0) {
494         LOG(log_error, logtype_afpd, "dsi_start: session error: %s", strerror(errno));
495         return NULL;
496     }
497
498     /* we've forked. */
499     if (child == NULL) {
500         configfree(obj, dsi);
501         afp_over_dsi(obj); /* start a session */
502         exit (0);
503     }
504
505     return child;
506 }