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