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