]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/main.c
AFP 3.x add sleep timeout, add disconnect old session. Björn Fernhomberg and me
[netatalk.git] / etc / afpd / main.c
1 /*
2  * $Id: main.c,v 1.21 2003-05-16 15:29:27 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 #ifdef HAVE_UNISTD_H
16 #include <unistd.h>
17 #endif /* HAVE_UNISTD_H */
18 #ifdef HAVE_FCNTL_H
19 #include <fcntl.h>
20 #endif /* HAVE_FCNTL_H */
21 #include <signal.h>
22
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <sys/param.h>
26 #include <sys/uio.h>
27 #include <atalk/logger.h>
28 #include <sys/time.h>
29 #include <sys/socket.h>
30
31 #include <errno.h>
32
33 #include <netatalk/endian.h>
34 #include <netatalk/at.h>
35 #include <atalk/compat.h>
36 #include <atalk/dsi.h>
37 #include <atalk/atp.h>
38 #include <atalk/asp.h>
39 #include <atalk/afp.h>
40 #include <atalk/adouble.h>
41 #include <atalk/paths.h>
42 #include <atalk/util.h>
43 #include <atalk/server_child.h>
44 #include <atalk/server_ipc.h>
45
46 #include "globals.h"
47 #include "afp_config.h"
48 #include "status.h"
49 #include "fork.h"
50 #include "uam_auth.h"
51
52 #ifdef TRU64
53 #include <sys/security.h>
54 #include <prot.h>
55 #include <sia.h>
56
57 static int argc = 0;
58 static char **argv = NULL;
59 #endif /* TRU64 */
60
61 unsigned char   nologin = 0;
62
63 struct afp_options default_options;
64 static AFPConfig *configs;
65 static server_child *server_children;
66 static fd_set save_rfds;
67 static int    Ipc_fd = -1;
68
69 #ifdef TRU64
70 void afp_get_cmdline( int *ac, char ***av)
71 {
72     *ac = argc;
73     *av = argv;
74 }
75 #endif /* TRU64 */
76
77 static void afp_exit(const int i)
78 {
79     server_unlock(default_options.pidfile);
80     exit(i);
81 }
82
83 /* ------------------ 
84    initialize fd set we are waiting for.
85 */
86 static void set_fd(int ipc_fd)
87 {
88     AFPConfig   *config;
89     
90     FD_ZERO(&save_rfds);
91     for (config = configs; config; config = config->next) {
92         if (config->fd < 0) /* for proxies */
93             continue;
94         FD_SET(config->fd, &save_rfds);
95     }
96     if (ipc_fd >= 0) {
97         FD_SET(ipc_fd, &save_rfds);
98     }
99 }
100
101 /* ------------------ */
102 static void afp_goaway(int sig)
103 {
104 #ifndef NO_DDP
105     asp_kill(sig);
106 #endif /* ! NO_DDP */
107     dsi_kill(sig);
108     switch( sig ) {
109     case SIGTERM :
110         LOG(log_info, logtype_afpd, "shutting down on signal %d", sig );
111         break;
112     case SIGHUP :
113         /* w/ a configuration file, we can force a re-read if we want */
114         nologin++;
115         if ((nologin + 1) & 1) {
116             AFPConfig *config;
117
118             LOG(log_info, logtype_afpd, "re-reading configuration file");
119             for (config = configs; config; config = config->next)
120                 if (config->server_cleanup)
121                     config->server_cleanup(config);
122
123             configfree(configs, NULL);
124             if (!(configs = configinit(&default_options))) {
125                 LOG(log_error, logtype_afpd, "config re-read: no servers configured");
126                 afp_exit(1);
127             }
128             set_fd(Ipc_fd);
129         } else {
130             LOG(log_info, logtype_afpd, "disallowing logins");
131             auth_unload();
132         }
133         break;
134     default :
135         LOG(log_error, logtype_afpd, "afp_goaway: bad signal" );
136     }
137     if ( sig == SIGTERM ) {
138         AFPConfig *config;
139
140         for (config = configs; config; config = config->next)
141             if (config->server_cleanup)
142                 config->server_cleanup(config);
143
144         afp_exit(0);
145     }
146     return;
147 }
148
149 static void child_handler()
150 {
151     server_child_handler(server_children);
152 }
153
154 int main( ac, av )
155 int             ac;
156 char    **av;
157 {
158     AFPConfig           *config;
159     fd_set              rfds;
160     void                *ipc;
161     struct sigaction    sv;
162     sigset_t            sigs;
163
164 #ifdef TRU64
165     argc = ac;
166     argv = av;
167     set_auth_parameters( ac, av );
168 #endif /* TRU64 */
169
170     afp_options_init(&default_options);
171     if (!afp_options_parse(ac, av, &default_options))
172         exit(1);
173
174     /* Save the user's current umask for use with CNID (and maybe some 
175      * other things, too). */
176     default_options.save_mask = umask( default_options.umask );
177
178     switch(server_lock("afpd", default_options.pidfile,
179                        default_options.flags & OPTION_DEBUG)) {
180     case -1: /* error */
181         exit(1);
182     case 0: /* child */
183         break;
184     default: /* server */
185         exit(0);
186     }
187
188     /* install child handler for asp and dsi. we do this before afp_goaway
189      * as afp_goaway references stuff from here. 
190      * XXX: this should really be setup after the initial connections. */
191     if (!(server_children = server_child_alloc(default_options.connections,
192                             CHILD_NFORKS))) {
193         LOG(log_error, logtype_afpd, "main: server_child alloc: %s", strerror(errno) );
194         afp_exit(1);
195     }
196
197     memset(&sv, 0, sizeof(sv));
198     sv.sa_handler = child_handler;
199     sigemptyset( &sv.sa_mask );
200     sv.sa_flags = SA_RESTART;
201     if ( sigaction( SIGCHLD, &sv, 0 ) < 0 ) {
202         LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
203         afp_exit(1);
204     }
205
206     sv.sa_handler = afp_goaway;
207     sigemptyset( &sv.sa_mask );
208     sigaddset(&sv.sa_mask, SIGHUP);
209     sigaddset(&sv.sa_mask, SIGTERM);
210     sv.sa_flags = SA_RESTART;
211     if ( sigaction( SIGHUP, &sv, 0 ) < 0 ) {
212         LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
213         afp_exit(1);
214     }
215     if ( sigaction( SIGTERM, &sv, 0 ) < 0 ) {
216         LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
217         afp_exit(1);
218     }
219
220     /* afpd.conf: not in config file: lockfile, connections, configfile
221      *            preference: command-line provides defaults.
222      *                        config file over-writes defaults.
223      *
224      * we also need to make sure that killing afpd during startup
225      * won't leave any lingering registered names around.
226      */
227
228     sigemptyset(&sigs);
229     sigaddset(&sigs, SIGHUP);
230     sigaddset(&sigs, SIGTERM);
231     sigprocmask(SIG_BLOCK, &sigs, NULL);
232     if (!(configs = configinit(&default_options))) {
233         LOG(log_error, logtype_afpd, "main: no servers configured: %s\n", strerror(errno));
234         afp_exit(1);
235     }
236     sigprocmask(SIG_UNBLOCK, &sigs, NULL);
237
238     /* watch atp, dsi sockets and ipc parent/child file descriptor. */
239     if ((ipc = server_ipc_create())) {
240         Ipc_fd = server_ipc_parent(ipc);
241     }
242     set_fd(Ipc_fd);
243
244     /* wait for an appleshare connection. parent remains in the loop
245      * while the children get handled by afp_over_{asp,dsi}.  this is
246      * currently vulnerable to a denial-of-service attack if a
247      * connection is made without an actual login attempt being made
248      * afterwards. establishing timeouts for logins is a possible 
249      * solution. */
250     while (1) {
251         rfds = save_rfds;
252         if (select(FD_SETSIZE, &rfds, NULL, NULL, NULL) < 0) {
253             if (errno == EINTR)
254                 continue;
255             LOG(log_error, logtype_afpd, "main: can't wait for input: %s", strerror(errno));
256             break;
257         }
258         if (Ipc_fd >=0 && FD_ISSET(Ipc_fd, &rfds)) {
259             server_ipc_read(server_children);
260         }
261         for (config = configs; config; config = config->next) {
262             if (config->fd < 0)
263                 continue;
264             if (FD_ISSET(config->fd, &rfds))
265                 config->server_start(config, configs, server_children);
266         }
267     }
268
269     return 0;
270 }