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