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