]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/main.c
Reworked logger
[netatalk.git] / etc / afpd / main.c
1 /*
2  * $Id: main.c,v 1.24 2009-02-27 09:14:40 franklahm 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()
151 {
152     server_child_handler(server_children);
153 }
154
155 int main( ac, av )
156 int             ac;
157 char    **av;
158 {
159     AFPConfig           *config;
160     fd_set              rfds;
161     void                *ipc;
162     struct sigaction    sv;
163     sigset_t            sigs;
164     int                 ret;
165
166 #ifdef TRU64
167     argc = ac;
168     argv = av;
169     set_auth_parameters( ac, av );
170 #endif /* TRU64 */
171
172 #ifdef DEBUG1
173     fault_setup(NULL);
174 #endif
175     afp_options_init(&default_options);
176     if (!afp_options_parse(ac, av, &default_options))
177         exit(EXITERR_CONF);
178
179     /* Save the user's current umask for use with CNID (and maybe some 
180      * other things, too). */
181     default_options.save_mask = umask( default_options.umask );
182
183     switch(server_lock("afpd", default_options.pidfile,
184                        default_options.flags & OPTION_DEBUG)) {
185     case -1: /* error */
186         exit(EXITERR_SYS);
187     case 0: /* child */
188         break;
189     default: /* server */
190         exit(0);
191     }
192
193 #if 0
194     /* Register CNID  */
195     cnid_init();
196 #endif
197
198     /* install child handler for asp and dsi. we do this before afp_goaway
199      * as afp_goaway references stuff from here. 
200      * XXX: this should really be setup after the initial connections. */
201     if (!(server_children = server_child_alloc(default_options.connections,
202                             CHILD_NFORKS))) {
203         LOG(log_error, logtype_afpd, "main: server_child alloc: %s", strerror(errno) );
204         afp_exit(EXITERR_SYS);
205     }
206     
207 #ifdef AFP3x
208     /* linux at least up to 2.4.22 send a SIGXFZ for vfat fs,
209        even if the file is open with O_LARGEFILE ! */
210 #ifdef SIGXFSZ
211     signal(SIGXFSZ , SIG_IGN); 
212 #endif
213 #endif    
214     
215     memset(&sv, 0, sizeof(sv));
216     sv.sa_handler = child_handler;
217     sigemptyset( &sv.sa_mask );
218     sigaddset(&sv.sa_mask, SIGALRM);
219     sigaddset(&sv.sa_mask, SIGHUP);
220     sigaddset(&sv.sa_mask, SIGTERM);
221     sigaddset(&sv.sa_mask, SIGUSR1);
222     
223     sv.sa_flags = SA_RESTART;
224     if ( sigaction( SIGCHLD, &sv, 0 ) < 0 ) {
225         LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
226         afp_exit(EXITERR_SYS);
227     }
228
229     sv.sa_handler = afp_goaway;
230     sigemptyset( &sv.sa_mask );
231     sigaddset(&sv.sa_mask, SIGALRM);
232     sigaddset(&sv.sa_mask, SIGTERM);
233     sigaddset(&sv.sa_mask, SIGHUP);
234     sigaddset(&sv.sa_mask, SIGCHLD);
235     sv.sa_flags = SA_RESTART;
236     if ( sigaction( SIGUSR1, &sv, 0 ) < 0 ) {
237         LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
238         afp_exit(EXITERR_SYS);
239     }
240
241     sigemptyset( &sv.sa_mask );
242     sigaddset(&sv.sa_mask, SIGALRM);
243     sigaddset(&sv.sa_mask, SIGTERM);
244     sigaddset(&sv.sa_mask, SIGUSR1);
245     sigaddset(&sv.sa_mask, SIGCHLD);
246     sv.sa_flags = SA_RESTART;
247     if ( sigaction( SIGHUP, &sv, 0 ) < 0 ) {
248         LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
249         afp_exit(EXITERR_SYS);
250     }
251
252
253     sigemptyset( &sv.sa_mask );
254     sigaddset(&sv.sa_mask, SIGALRM);
255     sigaddset(&sv.sa_mask, SIGHUP);
256     sigaddset(&sv.sa_mask, SIGUSR1);
257     sigaddset(&sv.sa_mask, SIGCHLD);
258     sv.sa_flags = SA_RESTART;
259     if ( sigaction( SIGTERM, &sv, 0 ) < 0 ) {
260         LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
261         afp_exit(EXITERR_SYS);
262     }
263
264     /* afpd.conf: not in config file: lockfile, connections, configfile
265      *            preference: command-line provides defaults.
266      *                        config file over-writes defaults.
267      *
268      * we also need to make sure that killing afpd during startup
269      * won't leave any lingering registered names around.
270      */
271
272     sigemptyset(&sigs);
273     sigaddset(&sigs, SIGALRM);
274     sigaddset(&sigs, SIGHUP);
275     sigaddset(&sigs, SIGUSR1);
276 #if 0
277     /* don't block SIGTERM */
278     sigaddset(&sigs, SIGTERM);
279 #endif
280     sigaddset(&sigs, SIGCHLD);
281
282     sigprocmask(SIG_BLOCK, &sigs, NULL);
283     if (!(configs = configinit(&default_options))) {
284         LOG(log_error, logtype_afpd, "main: no servers configured");
285         afp_exit(EXITERR_CONF);
286     }
287     sigprocmask(SIG_UNBLOCK, &sigs, NULL);
288
289     /* Register CNID  */
290     cnid_init();
291
292     /* watch atp, dsi sockets and ipc parent/child file descriptor. */
293     if ((ipc = server_ipc_create())) {
294         Ipc_fd = server_ipc_parent(ipc);
295     }
296     set_fd(Ipc_fd);
297
298     /* wait for an appleshare connection. parent remains in the loop
299      * while the children get handled by afp_over_{asp,dsi}.  this is
300      * currently vulnerable to a denial-of-service attack if a
301      * connection is made without an actual login attempt being made
302      * afterwards. establishing timeouts for logins is a possible 
303      * solution. */
304     while (1) {
305         rfds = save_rfds;
306         sigprocmask(SIG_UNBLOCK, &sigs, NULL);
307         ret = select(FD_SETSIZE, &rfds, NULL, NULL, NULL);
308         sigprocmask(SIG_BLOCK, &sigs, NULL);
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 }