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