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