]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/main.c
big merge for db frontend and unicode.
[netatalk.git] / etc / afpd / main.c
1 /*
2  * $Id: main.c,v 1.20.4.2.2.1 2003-09-09 16:42:20 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
105 #ifndef NO_DDP
106     asp_kill(sig);
107 #endif /* ! NO_DDP */
108
109     dsi_kill(sig);
110     switch( sig ) {
111     case SIGTERM :
112         LOG(log_info, logtype_afpd, "shutting down on signal %d", sig );
113         break;
114     case SIGUSR1 :
115     case SIGHUP :
116         /* w/ a configuration file, we can force a re-read if we want */
117         nologin++;
118         if (sig == SIGHUP || ((nologin + 1) & 1)) {
119             AFPConfig *config;
120
121             LOG(log_info, logtype_afpd, "re-reading configuration file");
122             for (config = configs; config; config = config->next)
123                 if (config->server_cleanup)
124                     config->server_cleanup(config);
125
126             configfree(configs, NULL);
127             if (!(configs = configinit(&default_options))) {
128                 LOG(log_error, logtype_afpd, "config re-read: no servers configured");
129                 afp_exit(1);
130             }
131             set_fd(Ipc_fd);
132         } else {
133             LOG(log_info, logtype_afpd, "disallowing logins");
134             auth_unload();
135         }
136         if (sig == SIGHUP) {
137             nologin = 0;
138         }
139         break;
140     default :
141         LOG(log_error, logtype_afpd, "afp_goaway: bad signal" );
142     }
143     if ( sig == SIGTERM ) {
144         AFPConfig *config;
145
146         for (config = configs; config; config = config->next)
147             if (config->server_cleanup)
148                 config->server_cleanup(config);
149
150         afp_exit(0);
151     }
152     return;
153 }
154
155 static void child_handler()
156 {
157     server_child_handler(server_children);
158 }
159
160 int main( ac, av )
161 int             ac;
162 char    **av;
163 {
164     AFPConfig           *config;
165     fd_set              rfds;
166     void                *ipc;
167     struct sigaction    sv;
168     sigset_t            sigs;
169
170 #ifdef TRU64
171     argc = ac;
172     argv = av;
173     set_auth_parameters( ac, av );
174 #endif /* TRU64 */
175     fault_setup(NULL);
176
177     afp_options_init(&default_options);
178     if (!afp_options_parse(ac, av, &default_options))
179         exit(1);
180
181     /* Save the user's current umask for use with CNID (and maybe some 
182      * other things, too). */
183     default_options.save_mask = umask( default_options.umask );
184
185     switch(server_lock("afpd", default_options.pidfile,
186                        default_options.flags & OPTION_DEBUG)) {
187     case -1: /* error */
188         exit(1);
189     case 0: /* child */
190         break;
191     default: /* server */
192         exit(0);
193     }
194
195     /* Register CNID  */
196     cnid_init();
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(1);
205     }
206
207     memset(&sv, 0, sizeof(sv));
208     sv.sa_handler = child_handler;
209     sigemptyset( &sv.sa_mask );
210     sv.sa_flags = SA_RESTART;
211     if ( sigaction( SIGCHLD, &sv, 0 ) < 0 ) {
212         LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
213         afp_exit(1);
214     }
215
216     sv.sa_handler = afp_goaway;
217     sigemptyset( &sv.sa_mask );
218     sigaddset(&sv.sa_mask, SIGHUP);
219     sigaddset(&sv.sa_mask, SIGTERM);
220     sigaddset(&sv.sa_mask, SIGUSR1);
221     sv.sa_flags = SA_RESTART;
222     if ( sigaction( SIGUSR1, &sv, 0 ) < 0 ) {
223         LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
224         afp_exit(1);
225     }
226     if ( sigaction( SIGHUP, &sv, 0 ) < 0 ) {
227         LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
228         afp_exit(1);
229     }
230     if ( sigaction( SIGTERM, &sv, 0 ) < 0 ) {
231         LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
232         afp_exit(1);
233     }
234
235     /* afpd.conf: not in config file: lockfile, connections, configfile
236      *            preference: command-line provides defaults.
237      *                        config file over-writes defaults.
238      *
239      * we also need to make sure that killing afpd during startup
240      * won't leave any lingering registered names around.
241      */
242
243     sigemptyset(&sigs);
244     sigaddset(&sigs, SIGHUP);
245     sigaddset(&sigs, SIGTERM);
246     sigprocmask(SIG_BLOCK, &sigs, NULL);
247     if (!(configs = configinit(&default_options))) {
248         LOG(log_error, logtype_afpd, "main: no servers configured: %s\n", strerror(errno));
249         afp_exit(1);
250     }
251     sigprocmask(SIG_UNBLOCK, &sigs, NULL);
252
253     /* watch atp, dsi sockets and ipc parent/child file descriptor. */
254     if ((ipc = server_ipc_create())) {
255         Ipc_fd = server_ipc_parent(ipc);
256     }
257     set_fd(Ipc_fd);
258
259     /* wait for an appleshare connection. parent remains in the loop
260      * while the children get handled by afp_over_{asp,dsi}.  this is
261      * currently vulnerable to a denial-of-service attack if a
262      * connection is made without an actual login attempt being made
263      * afterwards. establishing timeouts for logins is a possible 
264      * solution. */
265     while (1) {
266         rfds = save_rfds;
267         if (select(FD_SETSIZE, &rfds, NULL, NULL, NULL) < 0) {
268             if (errno == EINTR)
269                 continue;
270             LOG(log_error, logtype_afpd, "main: can't wait for input: %s", strerror(errno));
271             break;
272         }
273         if (Ipc_fd >=0 && FD_ISSET(Ipc_fd, &rfds)) {
274             server_ipc_read(server_children);
275        }
276         for (config = configs; config; config = config->next) {
277             if (config->fd < 0)
278                 continue;
279             if (FD_ISSET(config->fd, &rfds))
280                 config->server_start(config, configs, server_children);
281         }
282     }
283
284     return 0;
285 }