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