]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/main.c
Merge remote-tracking branch 'origin/develop' into spotlight
[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 #include <sys/resource.h>
22
23 #include <atalk/logger.h>
24 #include <atalk/adouble.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/globals.h>
34 #include <atalk/netatalk_conf.h>
35
36 #include "afp_config.h"
37 #include "status.h"
38 #include "fork.h"
39 #include "uam_auth.h"
40 #include "afp_zeroconf.h"
41 #include "afpstats.h"
42
43 #define AFP_LISTENERS 32
44 #define FDSET_SAFETY  5
45
46 unsigned char nologin = 0;
47
48 static AFPObj obj;
49 static server_child_t *server_children;
50 static sig_atomic_t reloadconfig = 0;
51 static sig_atomic_t gotsigchld = 0;
52
53 /* Two pointers to dynamic allocated arrays which store pollfds and associated data */
54 static struct pollfd *fdset;
55 static struct polldata *polldata;
56 static int fdset_size;          /* current allocated size */
57 static int fdset_used;          /* number of used elements */
58
59 static afp_child_t *dsi_start(AFPObj *obj, DSI *dsi, server_child_t *server_children);
60
61 static void afp_exit(int ret)
62 {
63     exit(ret);
64 }
65
66
67 /* ------------------
68    initialize fd set we are waiting for.
69 */
70 static void fd_set_listening_sockets(const AFPObj *config)
71 {
72     DSI *dsi;
73
74     for (dsi = config->dsi; dsi; dsi = dsi->next) {
75         fdset_add_fd(config->options.connections + AFP_LISTENERS + FDSET_SAFETY,
76                      &fdset,
77                      &polldata,
78                      &fdset_used,
79                      &fdset_size,
80                      dsi->serversock,
81                      LISTEN_FD,
82                      dsi);
83     }
84 }
85  
86 static void fd_reset_listening_sockets(const AFPObj *config)
87 {
88     const DSI *dsi;
89
90     for (dsi = config->dsi; dsi; dsi = dsi->next) {
91         fdset_del_fd(&fdset, &polldata, &fdset_used, &fdset_size, dsi->serversock);
92     }
93 }
94
95 /* ------------------ */
96 static void afp_goaway(int sig)
97 {
98     switch( sig ) {
99
100     case SIGTERM:
101     case SIGQUIT:
102         LOG(log_note, logtype_afpd, "AFP Server shutting down");
103         if (server_children)
104             server_child_kill(server_children, SIGTERM);
105         _exit(0);
106         break;
107
108     case SIGUSR1 :
109         nologin++;
110         auth_unload();
111         LOG(log_info, logtype_afpd, "disallowing logins");        
112
113         if (server_children)
114             server_child_kill(server_children, sig);
115         break;
116
117     case SIGHUP :
118         /* w/ a configuration file, we can force a re-read if we want */
119         reloadconfig = 1;
120         break;
121
122     case SIGCHLD:
123         /* w/ a configuration file, we can force a re-read if we want */
124         gotsigchld = 1;
125         break;
126
127     default :
128         LOG(log_error, logtype_afpd, "afp_goaway: bad signal" );
129     }
130     return;
131 }
132
133 static void child_handler(void)
134 {
135     int fd;
136     int status, i;
137     pid_t pid;
138   
139 #ifndef WAIT_ANY
140 #define WAIT_ANY (-1)
141 #endif /* ! WAIT_ANY */
142
143     while ((pid = waitpid(WAIT_ANY, &status, WNOHANG)) > 0) {
144         if ((fd = server_child_remove(server_children, pid)) != -1) {
145             fdset_del_fd(&fdset, &polldata, &fdset_used, &fdset_size, fd);        
146             break;
147         }
148
149         if (WIFEXITED(status)) {
150             if (WEXITSTATUS(status))
151                 LOG(log_info, logtype_afpd, "child[%d]: exited %d", pid, WEXITSTATUS(status));
152             else
153                 LOG(log_info, logtype_afpd, "child[%d]: done", pid);
154         } else {
155             if (WIFSIGNALED(status))
156                 LOG(log_info, logtype_afpd, "child[%d]: killed by signal %d", pid, WTERMSIG(status));
157             else
158                 LOG(log_info, logtype_afpd, "child[%d]: died", pid);
159         }
160     }
161 }
162
163 static int setlimits(void)
164 {
165     struct rlimit rlim;
166
167     if (getrlimit(RLIMIT_NOFILE, &rlim) != 0) {
168         LOG(log_warning, logtype_afpd, "setlimits: reading current limits failed: %s", strerror(errno));
169         return -1;
170     }
171     if (rlim.rlim_cur != RLIM_INFINITY && rlim.rlim_cur < 65535) {
172         rlim.rlim_cur = 65535;
173         if (rlim.rlim_max != RLIM_INFINITY && rlim.rlim_max < 65535)
174             rlim.rlim_max = 65535;
175         if (setrlimit(RLIMIT_NOFILE, &rlim) != 0) {
176             LOG(log_warning, logtype_afpd, "setlimits: increasing limits failed: %s", strerror(errno));
177             return -1;
178         }
179     }
180     return 0;
181 }
182
183 int main(int ac, char **av)
184 {
185     struct sigaction    sv;
186     sigset_t            sigs;
187     int                 ret;
188
189     /* Parse argv args and initialize default options */
190     afp_options_parse_cmdline(&obj, ac, av);
191
192     if (!(obj.cmdlineflags & OPTION_DEBUG) && (daemonize(0, 0) != 0))
193         exit(EXITERR_SYS);
194
195     /* Log SIGBUS/SIGSEGV SBT */
196     fault_setup(NULL);
197
198     if (afp_config_parse(&obj, "afpd") != 0)
199         afp_exit(EXITERR_CONF);
200
201     /* Save the user's current umask */
202     obj.options.save_mask = umask(obj.options.umask);
203
204     /* install child handler for asp and dsi. we do this before afp_goaway
205      * as afp_goaway references stuff from here. 
206      * XXX: this should really be setup after the initial connections. */
207     if (!(server_children = server_child_alloc(obj.options.connections))) {
208         LOG(log_error, logtype_afpd, "main: server_child alloc: %s", strerror(errno) );
209         afp_exit(EXITERR_SYS);
210     }
211     
212     sigemptyset(&sigs);
213     pthread_sigmask(SIG_SETMASK, &sigs, NULL);
214
215     memset(&sv, 0, sizeof(sv));    
216     /* linux at least up to 2.4.22 send a SIGXFZ for vfat fs,
217        even if the file is open with O_LARGEFILE ! */
218 #ifdef SIGXFSZ
219     sv.sa_handler = SIG_IGN;
220     sigemptyset( &sv.sa_mask );
221     if (sigaction(SIGXFSZ, &sv, NULL ) < 0 ) {
222         LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
223         afp_exit(EXITERR_SYS);
224     }
225 #endif
226
227     sv.sa_handler = SIG_IGN;
228     sigemptyset( &sv.sa_mask );
229     if (sigaction(SIGPIPE, &sv, NULL ) < 0 ) {
230         LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
231         afp_exit(EXITERR_SYS);
232     }
233     
234     sv.sa_handler = afp_goaway; /* handler for all sigs */
235
236     sigemptyset( &sv.sa_mask );
237     sigaddset(&sv.sa_mask, SIGALRM);
238     sigaddset(&sv.sa_mask, SIGHUP);
239     sigaddset(&sv.sa_mask, SIGTERM);
240     sigaddset(&sv.sa_mask, SIGUSR1);
241     sigaddset(&sv.sa_mask, SIGQUIT);    
242     sv.sa_flags = SA_RESTART;
243     if ( sigaction( SIGCHLD, &sv, NULL ) < 0 ) {
244         LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
245         afp_exit(EXITERR_SYS);
246     }
247
248     sigemptyset( &sv.sa_mask );
249     sigaddset(&sv.sa_mask, SIGALRM);
250     sigaddset(&sv.sa_mask, SIGTERM);
251     sigaddset(&sv.sa_mask, SIGHUP);
252     sigaddset(&sv.sa_mask, SIGCHLD);
253     sigaddset(&sv.sa_mask, SIGQUIT);
254     sv.sa_flags = SA_RESTART;
255     if ( sigaction( SIGUSR1, &sv, NULL ) < 0 ) {
256         LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
257         afp_exit(EXITERR_SYS);
258     }
259
260     sigemptyset( &sv.sa_mask );
261     sigaddset(&sv.sa_mask, SIGALRM);
262     sigaddset(&sv.sa_mask, SIGTERM);
263     sigaddset(&sv.sa_mask, SIGUSR1);
264     sigaddset(&sv.sa_mask, SIGCHLD);
265     sigaddset(&sv.sa_mask, SIGQUIT);
266     sv.sa_flags = SA_RESTART;
267     if ( sigaction( SIGHUP, &sv, NULL ) < 0 ) {
268         LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
269         afp_exit(EXITERR_SYS);
270     }
271
272     sigemptyset( &sv.sa_mask );
273     sigaddset(&sv.sa_mask, SIGALRM);
274     sigaddset(&sv.sa_mask, SIGHUP);
275     sigaddset(&sv.sa_mask, SIGUSR1);
276     sigaddset(&sv.sa_mask, SIGCHLD);
277     sigaddset(&sv.sa_mask, SIGQUIT);
278     sv.sa_flags = SA_RESTART;
279     if ( sigaction( SIGTERM, &sv, NULL ) < 0 ) {
280         LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
281         afp_exit(EXITERR_SYS);
282     }
283
284     sigemptyset( &sv.sa_mask );
285     sigaddset(&sv.sa_mask, SIGALRM);
286     sigaddset(&sv.sa_mask, SIGHUP);
287     sigaddset(&sv.sa_mask, SIGUSR1);
288     sigaddset(&sv.sa_mask, SIGCHLD);
289     sigaddset(&sv.sa_mask, SIGTERM);
290     sv.sa_flags = SA_RESTART;
291     if (sigaction(SIGQUIT, &sv, NULL ) < 0 ) {
292         LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
293         afp_exit(EXITERR_SYS);
294     }
295
296     /* afp.conf:  not in config file: lockfile, configfile
297      *            preference: command-line provides defaults.
298      *                        config file over-writes defaults.
299      *
300      * we also need to make sure that killing afpd during startup
301      * won't leave any lingering registered names around.
302      */
303
304     sigemptyset(&sigs);
305     sigaddset(&sigs, SIGALRM);
306     sigaddset(&sigs, SIGHUP);
307     sigaddset(&sigs, SIGUSR1);
308 #if 0
309     /* don't block SIGTERM */
310     sigaddset(&sigs, SIGTERM);
311 #endif
312     sigaddset(&sigs, SIGCHLD);
313
314     pthread_sigmask(SIG_BLOCK, &sigs, NULL);
315     if (configinit(&obj) != 0) {
316         LOG(log_error, logtype_afpd, "main: no servers configured");
317         afp_exit(EXITERR_CONF);
318     }
319     pthread_sigmask(SIG_UNBLOCK, &sigs, NULL);
320
321     /* Initialize */
322     cnid_init();
323
324     /* watch atp, dsi sockets and ipc parent/child file descriptor. */
325     fd_set_listening_sockets(&obj);
326
327     /* set limits */
328     (void)setlimits();
329
330 #ifdef HAVE_DBUS_GLIB
331     /* Run dbus AFP statics thread */
332     if (obj.options.flags & OPTION_DBUS_AFPSTATS)
333         (void)afpstats_init(server_children);
334 #endif
335
336     afp_child_t *child;
337     int recon_ipc_fd;
338     pid_t pid;
339     int saveerrno;
340
341     /* wait for an appleshare connection. parent remains in the loop
342      * while the children get handled by afp_over_{asp,dsi}.  this is
343      * currently vulnerable to a denial-of-service attack if a
344      * connection is made without an actual login attempt being made
345      * afterwards. establishing timeouts for logins is a possible 
346      * solution. */
347     while (1) {
348         LOG(log_maxdebug, logtype_afpd, "main: polling %i fds", fdset_used);
349         pthread_sigmask(SIG_UNBLOCK, &sigs, NULL);
350         ret = poll(fdset, fdset_used, -1);
351         pthread_sigmask(SIG_BLOCK, &sigs, NULL);
352         saveerrno = errno;
353
354         if (gotsigchld) {
355             gotsigchld = 0;
356             child_handler();
357             continue;
358         }
359
360         if (reloadconfig) {
361             nologin++;
362
363             fd_reset_listening_sockets(&obj);
364
365             LOG(log_info, logtype_afpd, "re-reading configuration file");
366
367             configfree(&obj, NULL);
368             afp_config_free(&obj);
369
370             if (afp_config_parse(&obj, "afpd") != 0)
371                 afp_exit(EXITERR_CONF);
372
373             if (configinit(&obj) != 0) {
374                 LOG(log_error, logtype_afpd, "config re-read: no servers configured");
375                 afp_exit(EXITERR_CONF);
376             }
377
378             fd_set_listening_sockets(&obj);
379
380             nologin = 0;
381             reloadconfig = 0;
382             errno = saveerrno;
383             continue;
384         }
385
386         if (ret == 0)
387             continue;
388         
389         if (ret < 0) {
390             if (errno == EINTR)
391                 continue;
392             LOG(log_error, logtype_afpd, "main: can't wait for input: %s", strerror(errno));
393             break;
394         }
395
396         for (int i = 0; i < fdset_used; i++) {
397             if (fdset[i].revents & (POLLIN | POLLERR | POLLHUP | POLLNVAL)) {
398                 switch (polldata[i].fdtype) {
399
400                 case LISTEN_FD:
401                     if ((child = dsi_start(&obj, (DSI *)polldata[i].data, server_children))) {
402                         /* Add IPC fd to select fd set */
403                         fdset_add_fd(obj.options.connections + AFP_LISTENERS + FDSET_SAFETY,
404                                      &fdset,
405                                      &polldata,
406                                      &fdset_used,
407                                      &fdset_size,
408                                      child->afpch_ipc_fd,
409                                      IPC_FD,
410                                      child);
411                     }
412                     break;
413
414                 case IPC_FD:
415                     child = (afp_child_t *)polldata[i].data;
416                     LOG(log_debug, logtype_afpd, "main: IPC request from child[%u]", child->afpch_pid);
417
418                     if (ipc_server_read(server_children, child->afpch_ipc_fd) != 0) {
419                         fdset_del_fd(&fdset, &polldata, &fdset_used, &fdset_size, child->afpch_ipc_fd);
420                         close(child->afpch_ipc_fd);
421                         child->afpch_ipc_fd = -1;
422                     }
423                     break;
424
425                 default:
426                     LOG(log_debug, logtype_afpd, "main: IPC request for unknown type");
427                     break;
428                 } /* switch */
429             }  /* if */
430         } /* for (i)*/
431     } /* while (1) */
432
433     return 0;
434 }
435
436 static afp_child_t *dsi_start(AFPObj *obj, DSI *dsi, server_child_t *server_children)
437 {
438     afp_child_t *child = NULL;
439
440     if (dsi_getsession(dsi, server_children, obj->options.tickleval, &child) != 0) {
441         LOG(log_error, logtype_afpd, "dsi_start: session error: %s", strerror(errno));
442         return NULL;
443     }
444
445     /* we've forked. */
446     if (child == NULL) {
447         configfree(obj, dsi);
448         afp_over_dsi(obj); /* start a session */
449         exit (0);
450     }
451
452     return child;
453 }