]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/main.c
Initialize Spotlight from session child
[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
348     /* watch atp, dsi sockets and ipc parent/child file descriptor. */
349
350     if (obj.options.flags & OPTION_KEEPSESSIONS) {
351         LOG(log_note, logtype_afpd, "Activating continous service");
352         disasociated_ipc_fd = ipc_server_uds(_PATH_AFP_IPC);
353     }
354
355     fd_set_listening_sockets(&obj);
356
357     /* set limits */
358     (void)setlimits();
359
360     afp_child_t *child;
361     int recon_ipc_fd;
362     pid_t pid;
363     int saveerrno;
364
365     /* wait for an appleshare connection. parent remains in the loop
366      * while the children get handled by afp_over_{asp,dsi}.  this is
367      * currently vulnerable to a denial-of-service attack if a
368      * connection is made without an actual login attempt being made
369      * afterwards. establishing timeouts for logins is a possible 
370      * solution. */
371     while (1) {
372         LOG(log_maxdebug, logtype_afpd, "main: polling %i fds", fdset_used);
373         pthread_sigmask(SIG_UNBLOCK, &sigs, NULL);
374         ret = poll(fdset, fdset_used, -1);
375         pthread_sigmask(SIG_BLOCK, &sigs, NULL);
376         saveerrno = errno;
377
378         if (gotsigchld) {
379             gotsigchld = 0;
380             child_handler();
381             continue;
382         }
383
384         if (reloadconfig) {
385             nologin++;
386             auth_unload();
387             fd_reset_listening_sockets(&obj);
388
389             LOG(log_info, logtype_afpd, "re-reading configuration file");
390
391             configfree(&obj, NULL);
392             if (configinit(&obj) != 0) {
393                 LOG(log_error, logtype_afpd, "config re-read: no servers configured");
394                 afp_exit(EXITERR_CONF);
395             }
396
397             fd_set_listening_sockets(&obj);
398
399             nologin = 0;
400             reloadconfig = 0;
401             errno = saveerrno;
402             continue;
403         }
404
405         if (ret == 0)
406             continue;
407         
408         if (ret < 0) {
409             if (errno == EINTR)
410                 continue;
411             LOG(log_error, logtype_afpd, "main: can't wait for input: %s", strerror(errno));
412             break;
413         }
414
415         for (int i = 0; i < fdset_used; i++) {
416             if (fdset[i].revents & (POLLIN | POLLERR | POLLHUP | POLLNVAL)) {
417                 switch (polldata[i].fdtype) {
418
419                 case LISTEN_FD:
420                     if (child = dsi_start(&obj, (DSI *)polldata[i].data, server_children)) {
421                         /* Add IPC fd to select fd set */
422                         fdset_add_fd(obj.options.connections + AFP_LISTENERS + FDSET_SAFETY,
423                                      &fdset,
424                                      &polldata,
425                                      &fdset_used,
426                                      &fdset_size,
427                                      child->ipc_fd,
428                                      IPC_FD,
429                                      child);
430                     }
431                     break;
432
433                 case IPC_FD:
434                     child = (afp_child_t *)polldata[i].data;
435                     LOG(log_debug, logtype_afpd, "main: IPC request from child[%u]", child->pid);
436
437                     if (ipc_server_read(server_children, child->ipc_fd) != 0) {
438                         fdset_del_fd(&fdset, &polldata, &fdset_used, &fdset_size, child->ipc_fd);
439                         close(child->ipc_fd);
440                         child->ipc_fd = -1;
441                         if ((obj.options.flags & OPTION_KEEPSESSIONS) && child->disasociated) {
442                             LOG(log_note, logtype_afpd, "main: removing reattached child[%u]", child->pid);
443                             server_child_remove(server_children, CHILD_DSIFORK, child->pid);
444                         }
445                     }
446                     break;
447
448                 case DISASOCIATED_IPC_FD:
449                     LOG(log_debug, logtype_afpd, "main: IPC reconnect request");
450                     if ((recon_ipc_fd = accept(disasociated_ipc_fd, NULL, NULL)) == -1) {
451                         LOG(log_error, logtype_afpd, "main: accept: %s", strerror(errno));
452                         break;
453                     }
454                     if (readt(recon_ipc_fd, &pid, sizeof(pid_t), 0, 1) != sizeof(pid_t)) {
455                         LOG(log_error, logtype_afpd, "main: readt: %s", strerror(errno));
456                         close(recon_ipc_fd);
457                         break;
458                     }
459                     LOG(log_note, logtype_afpd, "main: IPC reconnect from pid [%u]", pid);
460
461                     if ((child = server_child_add(server_children, CHILD_DSIFORK, pid, recon_ipc_fd)) == NULL) {
462                         LOG(log_error, logtype_afpd, "main: server_child_add");
463                         close(recon_ipc_fd);
464                         break;
465                     }
466                     child->disasociated = 1;
467                     fdset_add_fd(obj.options.connections + AFP_LISTENERS + FDSET_SAFETY,
468                                  &fdset,
469                                  &polldata,
470                                  &fdset_used,
471                                  &fdset_size,
472                                  recon_ipc_fd,
473                                  IPC_FD,
474                                  child);
475                     break;
476
477                 default:
478                     LOG(log_debug, logtype_afpd, "main: IPC request for unknown type");
479                     break;
480                 } /* switch */
481             }  /* if */
482         } /* for (i)*/
483     } /* while (1) */
484
485     return 0;
486 }
487
488 static afp_child_t *dsi_start(AFPObj *obj, DSI *dsi, server_child *server_children)
489 {
490     afp_child_t *child = NULL;
491
492     if (dsi_getsession(dsi, server_children, obj->options.tickleval, &child) != 0) {
493         LOG(log_error, logtype_afpd, "dsi_start: session error: %s", strerror(errno));
494         return NULL;
495     }
496
497     /* we've forked. */
498     if (child == NULL) {
499         configfree(obj, dsi);
500         afp_over_dsi(obj); /* start a session */
501         exit (0);
502     }
503
504     return child;
505 }