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