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