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