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