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