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