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