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