]> 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
22 #include <atalk/logger.h>
23 #include <atalk/adouble.h>
24 #include <netatalk/at.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
35 #include "event2/event.h"
36 #include "event2/http.h"
37 #include "event2/rpc.h"
38
39 #include "globals.h"
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
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 }
100  
101 static void fd_reset_listening_sockets(void)
102 {
103     AFPConfig   *config;
104
105     for (config = configs; config; config = config->next) {
106         if (config->fd < 0) /* for proxies */
107             continue;
108         fdset_del_fd(&fdset, &polldata, &fdset_used, &fdset_size, config->fd);
109     }
110 }
111
112 /* ------------------ */
113 static void afp_goaway(int sig)
114 {
115     switch( sig ) {
116
117     case SIGTERM :
118         LOG(log_note, logtype_afpd, "AFP Server shutting down on SIGTERM");
119         AFPConfig *config;
120
121         if (server_children)
122             server_child_kill(server_children, CHILD_DSIFORK, sig);
123
124         for (config = configs; config; config = config->next)
125             if (config->server_cleanup)
126                 config->server_cleanup(config);
127         server_unlock(default_options.pidfile);
128         exit(0);
129         break;
130
131     case SIGUSR1 :
132         nologin++;
133         auth_unload();
134         LOG(log_info, logtype_afpd, "disallowing logins");        
135
136         if (server_children)
137             server_child_kill(server_children, CHILD_DSIFORK, sig);
138         break;
139
140     case SIGHUP :
141         /* w/ a configuration file, we can force a re-read if we want */
142         reloadconfig = 1;
143         break;
144
145     default :
146         LOG(log_error, logtype_afpd, "afp_goaway: bad signal" );
147     }
148     return;
149 }
150
151 static void child_handler(int sig _U_)
152 {
153     int fd;
154     int status, i;
155     pid_t pid;
156   
157 #ifndef WAIT_ANY
158 #define WAIT_ANY (-1)
159 #endif /* ! WAIT_ANY */
160
161     while ((pid = waitpid(WAIT_ANY, &status, WNOHANG)) > 0) {
162         for (i = 0; i < server_children->nforks; i++) {
163             if ((fd = server_child_remove(server_children, i, pid)) != -1) {
164                 fdset_del_fd(&fdset, &polldata, &fdset_used, &fdset_size, fd);        
165                 break;
166             }
167         }
168
169         if (WIFEXITED(status)) {
170             if (WEXITSTATUS(status))
171                 LOG(log_info, logtype_afpd, "child[%d]: exited %d", pid, WEXITSTATUS(status));
172             else
173                 LOG(log_info, logtype_afpd, "child[%d]: done", pid);
174         } else {
175             if (WIFSIGNALED(status))
176                 LOG(log_info, logtype_afpd, "child[%d]: killed by signal %d", pid, WTERMSIG(status));
177             else
178                 LOG(log_info, logtype_afpd, "child[%d]: died", pid);
179         }
180     }
181 }
182
183 int main(int ac, char **av)
184 {
185     AFPConfig           *config;
186     fd_set              rfds;
187     void                *ipc;
188     struct sigaction    sv;
189     sigset_t            sigs;
190     int                 ret;
191
192 #ifdef TRU64
193     argc = ac;
194     argv = av;
195     set_auth_parameters( ac, av );
196 #endif /* TRU64 */
197
198     /* Log SIGBUS/SIGSEGV SBT */
199     fault_setup(NULL);
200
201     /* Default log setup: log to syslog */
202     setuplog("default log_note");
203
204     afp_options_init(&default_options);
205     if (!afp_options_parse(ac, av, &default_options))
206         exit(EXITERR_CONF);
207
208     /* Save the user's current umask for use with CNID (and maybe some 
209      * other things, too). */
210     default_options.save_mask = umask( default_options.umask );
211
212     switch(server_lock("afpd", default_options.pidfile,
213                        default_options.flags & OPTION_DEBUG)) {
214     case -1: /* error */
215         exit(EXITERR_SYS);
216     case 0: /* child */
217         break;
218     default: /* server */
219         exit(0);
220     }
221     atexit(afp_exit);
222
223     /* install child handler for asp and dsi. we do this before afp_goaway
224      * as afp_goaway references stuff from here. 
225      * XXX: this should really be setup after the initial connections. */
226     if (!(server_children = server_child_alloc(default_options.connections,
227                             CHILD_NFORKS))) {
228         LOG(log_error, logtype_afpd, "main: server_child alloc: %s", strerror(errno) );
229         exit(EXITERR_SYS);
230     }
231
232     memset(&sv, 0, sizeof(sv));    
233     /* linux at least up to 2.4.22 send a SIGXFZ for vfat fs,
234        even if the file is open with O_LARGEFILE ! */
235 #ifdef SIGXFSZ
236     sv.sa_handler = SIG_IGN;
237     sigemptyset( &sv.sa_mask );
238     if (sigaction(SIGXFSZ, &sv, NULL ) < 0 ) {
239         LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
240         exit(EXITERR_SYS);
241     }
242 #endif
243     
244     sv.sa_handler = child_handler;
245     sigemptyset( &sv.sa_mask );
246     sigaddset(&sv.sa_mask, SIGALRM);
247     sigaddset(&sv.sa_mask, SIGHUP);
248     sigaddset(&sv.sa_mask, SIGTERM);
249     sigaddset(&sv.sa_mask, SIGUSR1);
250     
251     sv.sa_flags = SA_RESTART;
252     if ( sigaction( SIGCHLD, &sv, NULL ) < 0 ) {
253         LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
254         exit(EXITERR_SYS);
255     }
256
257     sv.sa_handler = afp_goaway;
258     sigemptyset( &sv.sa_mask );
259     sigaddset(&sv.sa_mask, SIGALRM);
260     sigaddset(&sv.sa_mask, SIGTERM);
261     sigaddset(&sv.sa_mask, SIGHUP);
262     sigaddset(&sv.sa_mask, SIGCHLD);
263     sv.sa_flags = SA_RESTART;
264     if ( sigaction( SIGUSR1, &sv, NULL ) < 0 ) {
265         LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
266         exit(EXITERR_SYS);
267     }
268
269     sigemptyset( &sv.sa_mask );
270     sigaddset(&sv.sa_mask, SIGALRM);
271     sigaddset(&sv.sa_mask, SIGTERM);
272     sigaddset(&sv.sa_mask, SIGUSR1);
273     sigaddset(&sv.sa_mask, SIGCHLD);
274     sv.sa_flags = SA_RESTART;
275     if ( sigaction( SIGHUP, &sv, NULL ) < 0 ) {
276         LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
277         exit(EXITERR_SYS);
278     }
279
280
281     sigemptyset( &sv.sa_mask );
282     sigaddset(&sv.sa_mask, SIGALRM);
283     sigaddset(&sv.sa_mask, SIGHUP);
284     sigaddset(&sv.sa_mask, SIGUSR1);
285     sigaddset(&sv.sa_mask, SIGCHLD);
286     sv.sa_flags = SA_RESTART;
287     if ( sigaction( SIGTERM, &sv, NULL ) < 0 ) {
288         LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
289         exit(EXITERR_SYS);
290     }
291
292     /* afpd.conf: not in config file: lockfile, connections, configfile
293      *            preference: command-line provides defaults.
294      *                        config file over-writes defaults.
295      *
296      * we also need to make sure that killing afpd during startup
297      * won't leave any lingering registered names around.
298      */
299
300     sigemptyset(&sigs);
301     sigaddset(&sigs, SIGALRM);
302     sigaddset(&sigs, SIGHUP);
303     sigaddset(&sigs, SIGUSR1);
304 #if 0
305     /* don't block SIGTERM */
306     sigaddset(&sigs, SIGTERM);
307 #endif
308     sigaddset(&sigs, SIGCHLD);
309
310     pthread_sigmask(SIG_BLOCK, &sigs, NULL);
311     if (!(configs = configinit(&default_options))) {
312         LOG(log_error, logtype_afpd, "main: no servers configured");
313         exit(EXITERR_CONF);
314     }
315     pthread_sigmask(SIG_UNBLOCK, &sigs, NULL);
316
317     /* Initialize */
318     cnid_init();
319     if (locktable_init("XXX") != 0)
320         exit(EXITERR_SYS);
321 #if 0
322     if (rpc_init("127.0.0.1", 4701) != 0)
323         exit(EXITERR_SYS);
324 #endif
325     
326     /* watch atp, dsi sockets and ipc parent/child file descriptor. */
327     fd_set_listening_sockets();
328
329     afp_child_t *child;
330
331     /* wait for an appleshare connection. parent remains in the loop
332      * while the children get handled by afp_over_{asp,dsi}.  this is
333      * currently vulnerable to a denial-of-service attack if a
334      * connection is made without an actual login attempt being made
335      * afterwards. establishing timeouts for logins is a possible 
336      * solution. */
337     while (1) {
338         LOG(log_maxdebug, logtype_afpd, "main: polling %i fds", fdset_used);
339         pthread_sigmask(SIG_UNBLOCK, &sigs, NULL);
340         ret = poll(fdset, fdset_used, -1);
341         pthread_sigmask(SIG_BLOCK, &sigs, NULL);
342         int saveerrno = errno;
343
344         if (reloadconfig) {
345             nologin++;
346             auth_unload();
347             fd_reset_listening_sockets();
348
349             LOG(log_info, logtype_afpd, "re-reading configuration file");
350             for (config = configs; config; config = config->next)
351                 if (config->server_cleanup)
352                     config->server_cleanup(config);
353
354             /* configfree close atp socket used for DDP tickle, there's an issue
355              * with atp tid. */
356             configfree(configs, NULL);
357             if (!(configs = configinit(&default_options))) {
358                 LOG(log_error, logtype_afpd, "config re-read: no servers configured");
359                 exit(EXITERR_CONF);
360             }
361
362             fd_set_listening_sockets();
363
364             nologin = 0;
365             reloadconfig = 0;
366             errno = saveerrno;
367             continue;
368         }
369
370         if (ret == 0)
371             continue;
372         
373         if (ret < 0) {
374             if (errno == EINTR)
375                 continue;
376             LOG(log_error, logtype_afpd, "main: can't wait for input: %s", strerror(errno));
377             break;
378         }
379
380         for (int i = 0; i < fdset_used; i++) {
381             if (fdset[i].revents & POLLIN) {
382                 switch (polldata[i].fdtype) {
383                 case LISTEN_FD:
384                     config = (AFPConfig *)polldata[i].data;
385                     /* config->server_start is afp_config.c:dsi_start() for DSI */
386                     if (child = config->server_start(config, configs, server_children)) {
387                         /* Add IPC fd to select fd set */
388                         fdset_add_fd(&fdset, &polldata, &fdset_used, &fdset_size, child->ipc_fds[0], IPC_FD, child);
389                     }
390                     break;
391                 case IPC_FD:
392                     child = (afp_child_t *)polldata[i].data;
393                     LOG(log_debug, logtype_afpd, "main: IPC request from child[%u]", child->pid);
394                     if ((ret = ipc_server_read(server_children, child->ipc_fds[0])) == 0) {
395                         fdset_del_fd(&fdset, &polldata, &fdset_used, &fdset_size, child->ipc_fds[0]);
396                         close(child->ipc_fds[0]);
397                         child->ipc_fds[0] = -1;
398                     }
399                     break;
400                 default:
401                     LOG(log_debug, logtype_afpd, "main: IPC request for unknown type");
402                     break;
403                 } /* switch */
404             }  /* if */
405         } /* for (i)*/
406     } /* while (1) */
407
408     return 0;
409 }