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