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