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