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