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