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