]> 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     memset(&sv, 0, sizeof(sv));    
183 #ifdef AFP3x
184     /* linux at least up to 2.4.22 send a SIGXFZ for vfat fs,
185        even if the file is open with O_LARGEFILE ! */
186 #ifdef SIGXFSZ
187     sv.sa_handler = SIG_IGN;
188     sigemptyset( &sv.sa_mask );
189     if (sigaction(SIGXFSZ, &sv, NULL ) < 0 ) {
190         LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
191         afp_exit(EXITERR_SYS);
192     }
193 #endif
194 #endif
195     
196     sv.sa_handler = child_handler;
197     sigemptyset( &sv.sa_mask );
198     sigaddset(&sv.sa_mask, SIGALRM);
199     sigaddset(&sv.sa_mask, SIGHUP);
200     sigaddset(&sv.sa_mask, SIGTERM);
201     sigaddset(&sv.sa_mask, SIGUSR1);
202     
203     sv.sa_flags = SA_RESTART;
204     if ( sigaction( SIGCHLD, &sv, NULL ) < 0 ) {
205         LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
206         afp_exit(EXITERR_SYS);
207     }
208
209     sv.sa_handler = afp_goaway;
210     sigemptyset( &sv.sa_mask );
211     sigaddset(&sv.sa_mask, SIGALRM);
212     sigaddset(&sv.sa_mask, SIGTERM);
213     sigaddset(&sv.sa_mask, SIGHUP);
214     sigaddset(&sv.sa_mask, SIGCHLD);
215     sv.sa_flags = SA_RESTART;
216     if ( sigaction( SIGUSR1, &sv, NULL ) < 0 ) {
217         LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
218         afp_exit(EXITERR_SYS);
219     }
220
221     sigemptyset( &sv.sa_mask );
222     sigaddset(&sv.sa_mask, SIGALRM);
223     sigaddset(&sv.sa_mask, SIGTERM);
224     sigaddset(&sv.sa_mask, SIGUSR1);
225     sigaddset(&sv.sa_mask, SIGCHLD);
226     sv.sa_flags = SA_RESTART;
227     if ( sigaction( SIGHUP, &sv, NULL ) < 0 ) {
228         LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
229         afp_exit(EXITERR_SYS);
230     }
231
232
233     sigemptyset( &sv.sa_mask );
234     sigaddset(&sv.sa_mask, SIGALRM);
235     sigaddset(&sv.sa_mask, SIGHUP);
236     sigaddset(&sv.sa_mask, SIGUSR1);
237     sigaddset(&sv.sa_mask, SIGCHLD);
238     sv.sa_flags = SA_RESTART;
239     if ( sigaction( SIGTERM, &sv, NULL ) < 0 ) {
240         LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
241         afp_exit(EXITERR_SYS);
242     }
243
244     /* afpd.conf: not in config file: lockfile, connections, configfile
245      *            preference: command-line provides defaults.
246      *                        config file over-writes defaults.
247      *
248      * we also need to make sure that killing afpd during startup
249      * won't leave any lingering registered names around.
250      */
251
252     sigemptyset(&sigs);
253     sigaddset(&sigs, SIGALRM);
254     sigaddset(&sigs, SIGHUP);
255     sigaddset(&sigs, SIGUSR1);
256 #if 0
257     /* don't block SIGTERM */
258     sigaddset(&sigs, SIGTERM);
259 #endif
260     sigaddset(&sigs, SIGCHLD);
261
262     pthread_sigmask(SIG_BLOCK, &sigs, NULL);
263     if (!(configs = configinit(&default_options))) {
264         LOG(log_error, logtype_afpd, "main: no servers configured");
265         afp_exit(EXITERR_CONF);
266     }
267     pthread_sigmask(SIG_UNBLOCK, &sigs, NULL);
268
269     /* Register CNID  */
270     cnid_init();
271
272     /* watch atp, dsi sockets and ipc parent/child file descriptor. */
273     if ((ipc = server_ipc_create())) {
274         Ipc_fd = server_ipc_parent(ipc);
275     }
276     set_fd(Ipc_fd);
277
278     /* wait for an appleshare connection. parent remains in the loop
279      * while the children get handled by afp_over_{asp,dsi}.  this is
280      * currently vulnerable to a denial-of-service attack if a
281      * connection is made without an actual login attempt being made
282      * afterwards. establishing timeouts for logins is a possible 
283      * solution. */
284     while (1) {
285         rfds = save_rfds;
286         pthread_sigmask(SIG_UNBLOCK, &sigs, NULL);
287         ret = select(FD_SETSIZE, &rfds, NULL, NULL, NULL);
288         pthread_sigmask(SIG_BLOCK, &sigs, NULL);
289         int saveerrno = errno;
290
291         if (reloadconfig) {
292             nologin++;
293             auth_unload();
294             AFPConfig *config;
295
296             LOG(log_info, logtype_afpd, "re-reading configuration file");
297             for (config = configs; config; config = config->next)
298                 if (config->server_cleanup)
299                     config->server_cleanup(config);
300
301             /* configfree close atp socket used for DDP tickle, there's an issue
302              * with atp tid. */
303             configfree(configs, NULL);
304             if (!(configs = configinit(&default_options))) {
305                 LOG(log_error, logtype_afpd, "config re-read: no servers configured");
306                 afp_exit(EXITERR_CONF);
307             }
308             set_fd(Ipc_fd);
309             nologin = 0;
310             reloadconfig = 0;
311             errno = saveerrno;
312         }
313         
314         if (ret < 0) {
315             if (errno == EINTR)
316                 continue;
317             LOG(log_error, logtype_afpd, "main: can't wait for input: %s", strerror(errno));
318             break;
319         }
320         if (Ipc_fd >=0 && FD_ISSET(Ipc_fd, &rfds)) {
321             server_ipc_read(server_children);
322         }
323         for (config = configs; config; config = config->next) {
324             if (config->fd < 0)
325                 continue;
326             if (FD_ISSET(config->fd, &rfds)) {
327                 config->server_start(config, configs, server_children);
328             }
329         }
330     }
331
332     return 0;
333 }