]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/main.c
8c3f43ad6fe03826356c5df894102f9907944499
[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
15 #include <sys/param.h>
16 #include <sys/uio.h>
17 #include <atalk/logger.h>
18 #include <sys/time.h>
19 #include <sys/socket.h>
20 #include <sys/poll.h>
21 #include <errno.h>
22 #include <sys/wait.h>
23 #include <sys/resource.h>
24
25 #include <atalk/adouble.h>
26
27 #include <netatalk/at.h>
28 #include <atalk/compat.h>
29 #include <atalk/dsi.h>
30 #include <atalk/atp.h>
31 #include <atalk/asp.h>
32 #include <atalk/afp.h>
33 #include <atalk/paths.h>
34 #include <atalk/util.h>
35 #include <atalk/server_child.h>
36 #include <atalk/server_ipc.h>
37 #include <atalk/globals.h>
38
39 #include "afp_config.h"
40 #include "status.h"
41 #include "fork.h"
42 #include "uam_auth.h"
43 #include "afp_zeroconf.h"
44
45 #ifdef TRU64
46 #include <sys/security.h>
47 #include <prot.h>
48 #include <sia.h>
49
50 static int argc = 0;
51 static char **argv = NULL;
52 #endif /* TRU64 */
53
54 unsigned char   nologin = 0;
55 struct afp_options default_options;
56 static AFPConfig *configs;
57 static server_child *server_children;
58 static sig_atomic_t reloadconfig = 0;
59 static sig_atomic_t gotsigchld = 0;
60
61 /* Two pointers to dynamic allocated arrays which store pollfds and associated data */
62 static struct pollfd *fdset;
63 static struct polldata *polldata;
64 static int fdset_size;          /* current allocated size */
65 static int fdset_used;          /* number of used elements */
66 static int disasociated_ipc_fd; /* disasociated sessions uses this fd for IPC */
67
68 #ifdef TRU64
69 void afp_get_cmdline( int *ac, char ***av)
70 {
71     *ac = argc;
72     *av = argv;
73 }
74 #endif /* TRU64 */
75
76 /* This is registered with atexit() */
77 static void afp_exit(void)
78 {
79     if (parent_or_child == 0)
80         /* Only do this in the parent */
81         server_unlock(default_options.pidfile);
82 }
83
84
85 /* ------------------
86    initialize fd set we are waiting for.
87 */
88 static void fd_set_listening_sockets(void)
89 {
90     AFPConfig   *config;
91
92     for (config = configs; config; config = config->next) {
93         if (config->fd < 0) /* for proxies */
94             continue;
95         fdset_add_fd(&fdset, &polldata, &fdset_used, &fdset_size, config->fd, LISTEN_FD, config);
96     }
97
98     if (default_options.flags & OPTION_KEEPSESSIONS) {
99         LOG(log_note, logtype_afpd, "Activating continous service");
100         fdset_add_fd(&fdset, &polldata, &fdset_used, &fdset_size, disasociated_ipc_fd, DISASOCIATED_IPC_FD, NULL);
101     }
102 }
103  
104 static void fd_reset_listening_sockets(void)
105 {
106     AFPConfig   *config;
107
108     for (config = configs; config; config = config->next) {
109         if (config->fd < 0) /* for proxies */
110             continue;
111         fdset_del_fd(&fdset, &polldata, &fdset_used, &fdset_size, config->fd);
112     }
113
114     if (default_options.flags & OPTION_KEEPSESSIONS)
115         fdset_del_fd(&fdset, &polldata, &fdset_used, &fdset_size, disasociated_ipc_fd);
116 }
117
118 /* ------------------ */
119 static void afp_goaway(int sig)
120 {
121         AFPConfig *config;
122
123 #ifndef NO_DDP
124     asp_kill(sig);
125 #endif /* ! NO_DDP */
126
127     switch( sig ) {
128
129     case SIGTERM:
130     case SIGQUIT:
131         switch (sig) {
132         case SIGTERM:
133             LOG(log_note, logtype_afpd, "AFP Server shutting down on SIGTERM");
134             break;
135         case SIGQUIT:
136             if (default_options.flags & OPTION_KEEPSESSIONS) {
137                 LOG(log_note, logtype_afpd, "AFP Server shutting down on SIGQUIT, NOT disconnecting clients");
138             } else {
139                 LOG(log_note, logtype_afpd, "AFP Server shutting down on SIGQUIT");
140                 sig = SIGTERM;
141             }
142             break;
143         }
144         if (server_children)
145             server_child_kill(server_children, CHILD_DSIFORK, sig);
146
147         for (config = configs; config; config = config->next)
148             if (config->server_cleanup)
149                 config->server_cleanup(config);
150         server_unlock(default_options.pidfile);
151         exit(0);
152         break;
153
154     case SIGUSR1 :
155         nologin++;
156         auth_unload();
157         LOG(log_info, logtype_afpd, "disallowing logins");        
158
159         if (server_children)
160             server_child_kill(server_children, CHILD_DSIFORK, sig);
161         break;
162
163     case SIGHUP :
164         /* w/ a configuration file, we can force a re-read if we want */
165         reloadconfig = 1;
166         break;
167
168     case SIGCHLD:
169         /* w/ a configuration file, we can force a re-read if we want */
170         gotsigchld = 1;
171         break;
172
173     default :
174         LOG(log_error, logtype_afpd, "afp_goaway: bad signal" );
175     }
176     return;
177 }
178
179 static void child_handler(void)
180 {
181     int fd;
182     int status, i;
183     pid_t pid;
184   
185 #ifndef WAIT_ANY
186 #define WAIT_ANY (-1)
187 #endif /* ! WAIT_ANY */
188
189     while ((pid = waitpid(WAIT_ANY, &status, WNOHANG)) > 0) {
190         for (i = 0; i < server_children->nforks; i++) {
191             if ((fd = server_child_remove(server_children, i, pid)) != -1) {
192                 fdset_del_fd(&fdset, &polldata, &fdset_used, &fdset_size, fd);        
193                 break;
194             }
195         }
196
197         if (WIFEXITED(status)) {
198             if (WEXITSTATUS(status))
199                 LOG(log_info, logtype_afpd, "child[%d]: exited %d", pid, WEXITSTATUS(status));
200             else
201                 LOG(log_info, logtype_afpd, "child[%d]: done", pid);
202         } else {
203             if (WIFSIGNALED(status))
204                 LOG(log_info, logtype_afpd, "child[%d]: killed by signal %d", pid, WTERMSIG(status));
205             else
206                 LOG(log_info, logtype_afpd, "child[%d]: died", pid);
207         }
208     }
209 }
210
211 static int setlimits(void)
212 {
213     struct rlimit rlim;
214
215     if (getrlimit(RLIMIT_NOFILE, &rlim) != 0) {
216         LOG(log_error, logtype_afpd, "setlimits: %s", strerror(errno));
217         exit(1);
218     }
219     if (rlim.rlim_cur != RLIM_INFINITY && rlim.rlim_cur < 65535) {
220         rlim.rlim_cur = 65535;
221         if (rlim.rlim_max != RLIM_INFINITY && rlim.rlim_max < 65535)
222             rlim.rlim_max = 65535;
223         if (setrlimit(RLIMIT_NOFILE, &rlim) != 0) {
224             LOG(log_error, logtype_afpd, "setlimits: %s", strerror(errno));
225             exit(1);
226         }
227     }
228     return 0;
229 }
230
231 int main(int ac, char **av)
232 {
233     AFPConfig           *config;
234     fd_set              rfds;
235     void                *ipc;
236     struct sigaction    sv;
237     sigset_t            sigs;
238     int                 ret;
239
240 #ifdef TRU64
241     argc = ac;
242     argv = av;
243     set_auth_parameters( ac, av );
244 #endif /* TRU64 */
245
246     /* Log SIGBUS/SIGSEGV SBT */
247     fault_setup(NULL);
248
249     /* Default log setup: log to syslog */
250     setuplog("default log_note");
251
252     afp_options_init(&default_options);
253     if (!afp_options_parse(ac, av, &default_options))
254         exit(EXITERR_CONF);
255
256     /* Save the user's current umask for use with CNID (and maybe some 
257      * other things, too). */
258     default_options.save_mask = umask( default_options.umask );
259
260     switch(server_lock("afpd", default_options.pidfile,
261                        default_options.flags & OPTION_DEBUG)) {
262     case -1: /* error */
263         exit(EXITERR_SYS);
264     case 0: /* child */
265         break;
266     default: /* server */
267         exit(0);
268     }
269     atexit(afp_exit);
270
271     /* install child handler for asp and dsi. we do this before afp_goaway
272      * as afp_goaway references stuff from here. 
273      * XXX: this should really be setup after the initial connections. */
274     if (!(server_children = server_child_alloc(default_options.connections,
275                             CHILD_NFORKS))) {
276         LOG(log_error, logtype_afpd, "main: server_child alloc: %s", strerror(errno) );
277         exit(EXITERR_SYS);
278     }
279
280     memset(&sv, 0, sizeof(sv));    
281     /* linux at least up to 2.4.22 send a SIGXFZ for vfat fs,
282        even if the file is open with O_LARGEFILE ! */
283 #ifdef SIGXFSZ
284     sv.sa_handler = SIG_IGN;
285     sigemptyset( &sv.sa_mask );
286     if (sigaction(SIGXFSZ, &sv, NULL ) < 0 ) {
287         LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
288         exit(EXITERR_SYS);
289     }
290 #endif
291     
292     sv.sa_handler = afp_goaway; /* handler for all sigs */
293
294     sigemptyset( &sv.sa_mask );
295     sigaddset(&sv.sa_mask, SIGALRM);
296     sigaddset(&sv.sa_mask, SIGHUP);
297     sigaddset(&sv.sa_mask, SIGTERM);
298     sigaddset(&sv.sa_mask, SIGUSR1);
299     sigaddset(&sv.sa_mask, SIGQUIT);    
300     sv.sa_flags = SA_RESTART;
301     if ( sigaction( SIGCHLD, &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, SIGTERM);
309     sigaddset(&sv.sa_mask, SIGHUP);
310     sigaddset(&sv.sa_mask, SIGCHLD);
311     sigaddset(&sv.sa_mask, SIGQUIT);
312     sv.sa_flags = SA_RESTART;
313     if ( sigaction( SIGUSR1, &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, SIGTERM);
321     sigaddset(&sv.sa_mask, SIGUSR1);
322     sigaddset(&sv.sa_mask, SIGCHLD);
323     sigaddset(&sv.sa_mask, SIGQUIT);
324     sv.sa_flags = SA_RESTART;
325     if ( sigaction( SIGHUP, &sv, NULL ) < 0 ) {
326         LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
327         exit(EXITERR_SYS);
328     }
329
330
331     sigemptyset( &sv.sa_mask );
332     sigaddset(&sv.sa_mask, SIGALRM);
333     sigaddset(&sv.sa_mask, SIGHUP);
334     sigaddset(&sv.sa_mask, SIGUSR1);
335     sigaddset(&sv.sa_mask, SIGCHLD);
336     sigaddset(&sv.sa_mask, SIGQUIT);
337     sv.sa_flags = SA_RESTART;
338     if ( sigaction( SIGTERM, &sv, NULL ) < 0 ) {
339         LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
340         exit(EXITERR_SYS);
341     }
342
343     sigemptyset( &sv.sa_mask );
344     sigaddset(&sv.sa_mask, SIGALRM);
345     sigaddset(&sv.sa_mask, SIGHUP);
346     sigaddset(&sv.sa_mask, SIGUSR1);
347     sigaddset(&sv.sa_mask, SIGCHLD);
348     sigaddset(&sv.sa_mask, SIGTERM);
349     sv.sa_flags = SA_RESTART;
350     if (sigaction(SIGQUIT, &sv, NULL ) < 0 ) {
351         LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
352         exit(EXITERR_SYS);
353     }
354
355     /* afpd.conf: not in config file: lockfile, connections, configfile
356      *            preference: command-line provides defaults.
357      *                        config file over-writes defaults.
358      *
359      * we also need to make sure that killing afpd during startup
360      * won't leave any lingering registered names around.
361      */
362
363     sigemptyset(&sigs);
364     sigaddset(&sigs, SIGALRM);
365     sigaddset(&sigs, SIGHUP);
366     sigaddset(&sigs, SIGUSR1);
367 #if 0
368     /* don't block SIGTERM */
369     sigaddset(&sigs, SIGTERM);
370 #endif
371     sigaddset(&sigs, SIGCHLD);
372
373     pthread_sigmask(SIG_BLOCK, &sigs, NULL);
374     if (!(configs = configinit(&default_options))) {
375         LOG(log_error, logtype_afpd, "main: no servers configured");
376         exit(EXITERR_CONF);
377     }
378     pthread_sigmask(SIG_UNBLOCK, &sigs, NULL);
379
380     /* Register CNID  */
381     cnid_init();
382
383     /* watch atp, dsi sockets and ipc parent/child file descriptor. */
384     disasociated_ipc_fd = ipc_server_uds(_PATH_AFP_IPC);
385     fd_set_listening_sockets();
386
387     /* set limits */
388     (void)setlimits();
389
390     afp_child_t *child;
391     int fd[2];  /* we only use one, but server_child_add expects [2] */
392     pid_t pid;
393     int saveerrno;
394
395     /* wait for an appleshare connection. parent remains in the loop
396      * while the children get handled by afp_over_{asp,dsi}.  this is
397      * currently vulnerable to a denial-of-service attack if a
398      * connection is made without an actual login attempt being made
399      * afterwards. establishing timeouts for logins is a possible 
400      * solution. */
401     while (1) {
402         LOG(log_maxdebug, logtype_afpd, "main: polling %i fds", fdset_used);
403         pthread_sigmask(SIG_UNBLOCK, &sigs, NULL);
404         ret = poll(fdset, fdset_used, -1);
405         pthread_sigmask(SIG_BLOCK, &sigs, NULL);
406         saveerrno = errno;
407
408         if (gotsigchld) {
409             gotsigchld = 0;
410             child_handler();
411             continue;
412         }
413
414         if (reloadconfig) {
415             nologin++;
416             auth_unload();
417             fd_reset_listening_sockets();
418
419             LOG(log_info, logtype_afpd, "re-reading configuration file");
420             for (config = configs; config; config = config->next)
421                 if (config->server_cleanup)
422                     config->server_cleanup(config);
423
424             /* configfree close atp socket used for DDP tickle, there's an issue
425              * with atp tid. */
426             configfree(configs, NULL);
427             if (!(configs = configinit(&default_options))) {
428                 LOG(log_error, logtype_afpd, "config re-read: no servers configured");
429                 exit(EXITERR_CONF);
430             }
431
432             fd_set_listening_sockets();
433
434             nologin = 0;
435             reloadconfig = 0;
436             errno = saveerrno;
437             continue;
438         }
439
440         if (ret == 0)
441             continue;
442         
443         if (ret < 0) {
444             if (errno == EINTR)
445                 continue;
446             LOG(log_error, logtype_afpd, "main: can't wait for input: %s", strerror(errno));
447             break;
448         }
449
450         for (int i = 0; i < fdset_used; i++) {
451             if (fdset[i].revents & (POLLIN | POLLERR | POLLHUP)) {
452                 switch (polldata[i].fdtype) {
453
454                 case LISTEN_FD:
455                     config = (AFPConfig *)polldata[i].data;
456                     /* config->server_start is afp_config.c:dsi_start() for DSI */
457                     if (child = config->server_start(config, configs, server_children)) {
458                         /* Add IPC fd to select fd set */
459                         fdset_add_fd(&fdset, &polldata, &fdset_used, &fdset_size, child->ipc_fds[0], IPC_FD, child);
460                     }
461                     break;
462
463                 case IPC_FD:
464                     child = (afp_child_t *)polldata[i].data;
465                     LOG(log_debug, logtype_afpd, "main: IPC request from child[%u]", child->pid);
466
467                     if (ipc_server_read(server_children, child->ipc_fds[0]) != 0) {
468                         fdset_del_fd(&fdset, &polldata, &fdset_used, &fdset_size, child->ipc_fds[0]);
469                         close(child->ipc_fds[0]);
470                         child->ipc_fds[0] = -1;
471                         if ((default_options.flags & OPTION_KEEPSESSIONS) && child->disasociated) {
472                             LOG(log_note, logtype_afpd, "main: removing reattached child[%u]", child->pid);
473                             server_child_remove(server_children, CHILD_DSIFORK, child->pid);
474                         }
475                     }
476                     break;
477
478                 case DISASOCIATED_IPC_FD:
479                     LOG(log_debug, logtype_afpd, "main: IPC reconnect request");
480                     if ((fd[0] = accept(disasociated_ipc_fd, NULL, NULL)) == -1) {
481                         LOG(log_error, logtype_afpd, "main: accept: %s", strerror(errno));
482                         break;
483                     }
484                     if (readt(fd[0], &pid, sizeof(pid_t), 0, 1) != sizeof(pid_t)) {
485                         LOG(log_error, logtype_afpd, "main: readt: %s", strerror(errno));
486                         close(fd[0]);
487                         break;
488                     }
489                     LOG(log_note, logtype_afpd, "main: IPC reconnect from pid [%u]", pid);
490                     if ((child = server_child_add(server_children, CHILD_DSIFORK, pid, fd)) == NULL) {
491                         LOG(log_error, logtype_afpd, "main: server_child_add");
492                         close(fd[0]);
493                         break;
494                     }
495                     child->disasociated = 1;
496                     fdset_add_fd(&fdset, &polldata, &fdset_used, &fdset_size, fd[0], IPC_FD, child);
497                     break;
498
499                 default:
500                     LOG(log_debug, logtype_afpd, "main: IPC request for unknown type");
501                     break;
502                 } /* switch */
503             }  /* if */
504         } /* for (i)*/
505     } /* while (1) */
506
507     return 0;
508 }