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