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