]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/main.c
Merge branch-2-1
[netatalk.git] / etc / afpd / main.c
1 /*
2  * $Id: main.c,v 1.26 2009-10-14 02:24:05 didg Exp $
3  *
4  * Copyright (c) 1990,1993 Regents of The University of Michigan.
5  * All Rights Reserved.  See COPYRIGHT.
6  */
7
8 #ifdef HAVE_CONFIG_H
9 #include "config.h"
10 #endif /* HAVE_CONFIG_H */
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <signal.h>
16
17 #include <sys/param.h>
18 #include <sys/uio.h>
19 #include <atalk/logger.h>
20 #include <sys/time.h>
21 #include <sys/socket.h>
22
23 #include <errno.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 fd_set save_rfds;
60 static int    Ipc_fd = -1;
61 static sig_atomic_t reloadconfig = 0;
62
63 #ifdef TRU64
64 void afp_get_cmdline( int *ac, char ***av)
65 {
66     *ac = argc;
67     *av = argv;
68 }
69 #endif /* TRU64 */
70
71 static void afp_exit(const int i)
72 {
73     server_unlock(default_options.pidfile);
74     exit(i);
75 }
76
77 /* ------------------
78    initialize fd set we are waiting for.
79 */
80 static void set_fd(int ipc_fd)
81 {
82     AFPConfig   *config;
83
84     FD_ZERO(&save_rfds);
85     for (config = configs; config; config = config->next) {
86         if (config->fd < 0) /* for proxies */
87             continue;
88         FD_SET(config->fd, &save_rfds);
89     }
90     if (ipc_fd >= 0) {
91         FD_SET(ipc_fd, &save_rfds);
92     }
93 }
94  
95 /* ------------------ */
96 static void afp_goaway(int sig)
97 {
98
99 #ifndef NO_DDP
100     asp_kill(sig);
101 #endif /* ! NO_DDP */
102
103     dsi_kill(sig);
104     switch( sig ) {
105
106     case SIGTERM :
107         LOG(log_info, logtype_afpd, "shutting down on signal %d", sig );
108         AFPConfig *config;
109         for (config = configs; config; config = config->next)
110             if (config->server_cleanup)
111                 config->server_cleanup(config);
112         afp_exit(0);
113         break;
114
115     case SIGUSR1 :
116         nologin++;
117         auth_unload();
118         LOG(log_info, logtype_afpd, "disallowing logins");        
119         break;
120
121     case SIGHUP :
122         /* w/ a configuration file, we can force a re-read if we want */
123         reloadconfig = 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(int sig _U_)
133 {
134     server_child_handler(server_children);
135 }
136
137 int main(int ac, char **av)
138 {
139     AFPConfig           *config;
140     fd_set              rfds;
141     void                *ipc;
142     struct sigaction    sv;
143     sigset_t            sigs;
144     int                 ret;
145
146 #ifdef TRU64
147     argc = ac;
148     argv = av;
149     set_auth_parameters( ac, av );
150 #endif /* TRU64 */
151
152 #ifdef DEBUG1
153     fault_setup(NULL);
154 #endif
155     afp_options_init(&default_options);
156     if (!afp_options_parse(ac, av, &default_options))
157         exit(EXITERR_CONF);
158
159     /* Save the user's current umask for use with CNID (and maybe some 
160      * other things, too). */
161     default_options.save_mask = umask( default_options.umask );
162
163     switch(server_lock("afpd", default_options.pidfile,
164                        default_options.flags & OPTION_DEBUG)) {
165     case -1: /* error */
166         exit(EXITERR_SYS);
167     case 0: /* child */
168         break;
169     default: /* server */
170         exit(0);
171     }
172
173     /* install child handler for asp and dsi. we do this before afp_goaway
174      * as afp_goaway references stuff from here. 
175      * XXX: this should really be setup after the initial connections. */
176     if (!(server_children = server_child_alloc(default_options.connections,
177                             CHILD_NFORKS))) {
178         LOG(log_error, logtype_afpd, "main: server_child alloc: %s", strerror(errno) );
179         afp_exit(EXITERR_SYS);
180     }
181     
182 #ifdef AFP3x
183     /* linux at least up to 2.4.22 send a SIGXFZ for vfat fs,
184        even if the file is open with O_LARGEFILE ! */
185 #ifdef SIGXFSZ
186     signal(SIGXFSZ , SIG_IGN); 
187 #endif
188 #endif    
189     
190     memset(&sv, 0, sizeof(sv));
191     sv.sa_handler = child_handler;
192     sigemptyset( &sv.sa_mask );
193     sigaddset(&sv.sa_mask, SIGALRM);
194     sigaddset(&sv.sa_mask, SIGHUP);
195     sigaddset(&sv.sa_mask, SIGTERM);
196     sigaddset(&sv.sa_mask, SIGUSR1);
197     
198     sv.sa_flags = SA_RESTART;
199     if ( sigaction( SIGCHLD, &sv, NULL ) < 0 ) {
200         LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
201         afp_exit(EXITERR_SYS);
202     }
203
204     sv.sa_handler = afp_goaway;
205     sigemptyset( &sv.sa_mask );
206     sigaddset(&sv.sa_mask, SIGALRM);
207     sigaddset(&sv.sa_mask, SIGTERM);
208     sigaddset(&sv.sa_mask, SIGHUP);
209     sigaddset(&sv.sa_mask, SIGCHLD);
210     sv.sa_flags = SA_RESTART;
211     if ( sigaction( SIGUSR1, &sv, NULL ) < 0 ) {
212         LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
213         afp_exit(EXITERR_SYS);
214     }
215
216     sigemptyset( &sv.sa_mask );
217     sigaddset(&sv.sa_mask, SIGALRM);
218     sigaddset(&sv.sa_mask, SIGTERM);
219     sigaddset(&sv.sa_mask, SIGUSR1);
220     sigaddset(&sv.sa_mask, SIGCHLD);
221     sv.sa_flags = SA_RESTART;
222     if ( sigaction( SIGHUP, &sv, NULL ) < 0 ) {
223         LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
224         afp_exit(EXITERR_SYS);
225     }
226
227
228     sigemptyset( &sv.sa_mask );
229     sigaddset(&sv.sa_mask, SIGALRM);
230     sigaddset(&sv.sa_mask, SIGHUP);
231     sigaddset(&sv.sa_mask, SIGUSR1);
232     sigaddset(&sv.sa_mask, SIGCHLD);
233     sv.sa_flags = SA_RESTART;
234     if ( sigaction( SIGTERM, &sv, NULL ) < 0 ) {
235         LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
236         afp_exit(EXITERR_SYS);
237     }
238
239     /* afpd.conf: not in config file: lockfile, connections, configfile
240      *            preference: command-line provides defaults.
241      *                        config file over-writes defaults.
242      *
243      * we also need to make sure that killing afpd during startup
244      * won't leave any lingering registered names around.
245      */
246
247     sigemptyset(&sigs);
248     sigaddset(&sigs, SIGALRM);
249     sigaddset(&sigs, SIGHUP);
250     sigaddset(&sigs, SIGUSR1);
251 #if 0
252     /* don't block SIGTERM */
253     sigaddset(&sigs, SIGTERM);
254 #endif
255     sigaddset(&sigs, SIGCHLD);
256
257     pthread_sigmask(SIG_BLOCK, &sigs, NULL);
258     if (!(configs = configinit(&default_options))) {
259         LOG(log_error, logtype_afpd, "main: no servers configured");
260         afp_exit(EXITERR_CONF);
261     }
262     pthread_sigmask(SIG_UNBLOCK, &sigs, NULL);
263
264     /* Register CNID  */
265     cnid_init();
266
267     /* watch atp, dsi sockets and ipc parent/child file descriptor. */
268     if ((ipc = server_ipc_create())) {
269         Ipc_fd = server_ipc_parent(ipc);
270     }
271     set_fd(Ipc_fd);
272
273     /* wait for an appleshare connection. parent remains in the loop
274      * while the children get handled by afp_over_{asp,dsi}.  this is
275      * currently vulnerable to a denial-of-service attack if a
276      * connection is made without an actual login attempt being made
277      * afterwards. establishing timeouts for logins is a possible 
278      * solution. */
279     while (1) {
280         rfds = save_rfds;
281         pthread_sigmask(SIG_UNBLOCK, &sigs, NULL);
282         ret = select(FD_SETSIZE, &rfds, NULL, NULL, NULL);
283         pthread_sigmask(SIG_BLOCK, &sigs, NULL);
284         int saveerrno = errno;
285
286         if (reloadconfig) {
287             nologin++;
288             auth_unload();
289             AFPConfig *config;
290
291             LOG(log_info, logtype_afpd, "re-reading configuration file");
292             for (config = configs; config; config = config->next)
293                 if (config->server_cleanup)
294                     config->server_cleanup(config);
295
296             /* configfree close atp socket used for DDP tickle, there's an issue
297              * with atp tid. */
298             configfree(configs, NULL);
299             if (!(configs = configinit(&default_options))) {
300                 LOG(log_error, logtype_afpd, "config re-read: no servers configured");
301                 afp_exit(EXITERR_CONF);
302             }
303             set_fd(Ipc_fd);
304             nologin = 0;
305             reloadconfig = 0;
306             errno = saveerrno;
307         }
308         
309         if (ret < 0) {
310             if (errno == EINTR)
311                 continue;
312             LOG(log_error, logtype_afpd, "main: can't wait for input: %s", strerror(errno));
313             break;
314         }
315         if (Ipc_fd >=0 && FD_ISSET(Ipc_fd, &rfds)) {
316             server_ipc_read(server_children);
317         }
318         for (config = configs; config; config = config->next) {
319             if (config->fd < 0)
320                 continue;
321             if (FD_ISSET(config->fd, &rfds)) {
322                 config->server_start(config, configs, server_children);
323             }
324         }
325     }
326
327     return 0;
328 }