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