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