2 * $Id: main.c,v 1.19 2002-08-31 08:09:06 jmarcus Exp $
4 * Copyright (c) 1990,1993 Regents of The University of Michigan.
5 * All Rights Reserved. See COPYRIGHT.
10 #endif /* HAVE_CONFIG_H */
17 #endif /* HAVE_UNISTD_H */
20 #endif /* HAVE_FCNTL_H */
23 #include <sys/types.h>
25 #include <sys/param.h>
27 #include <atalk/logger.h>
29 #include <sys/socket.h>
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>
46 #include "afp_config.h"
52 #include <sys/security.h>
57 static char **argv = NULL;
61 #include "parse_mtab.h"
64 unsigned char nologin = 0;
67 /* global mount table; afpd_st_cnid uses this to lookup the right entry. */
68 static struct afpd_mount_table *afpd_mount_table = NULL;
71 struct afp_options default_options;
72 static AFPConfig *configs;
73 static server_child *server_children;
74 static fd_set save_rfds;
77 void afp_get_cmdline( int *ac, char ***av)
84 static void afp_exit(const int i)
86 server_unlock(default_options.pidfile);
90 static void afp_goaway(int sig)
98 LOG(log_info, logtype_afpd, "shutting down on signal %d", sig );
101 /* w/ a configuration file, we can force a re-read if we want */
103 if ((nologin + 1) & 1) {
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);
111 configfree(configs, NULL);
112 if (!(configs = configinit(&default_options))) {
113 LOG(log_error, logtype_afpd, "config re-read: no servers configured");
117 for (config = configs; config; config = config->next) {
120 FD_SET(config->fd, &save_rfds);
123 LOG(log_info, logtype_afpd, "disallowing logins");
128 LOG(log_error, logtype_afpd, "afp_goaway: bad signal" );
130 if ( sig == SIGTERM ) {
133 for (config = configs; config; config = config->next)
134 if (config->server_cleanup)
135 config->server_cleanup(config);
142 static void child_handler()
144 server_child_handler(server_children);
159 set_auth_parameters( ac, av );
162 afp_options_init(&default_options);
163 if (!afp_options_parse(ac, av, &default_options))
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 );
170 switch(server_lock("afpd", default_options.pidfile,
171 default_options.flags & OPTION_DEBUG)) {
176 default: /* server */
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 */
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,
190 LOG(log_error, logtype_afpd, "main: server_child alloc: %s", strerror(errno) );
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) );
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) );
212 if ( sigaction( SIGTERM, &sv, 0 ) < 0 ) {
213 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
217 /* afpd.conf: not in config file: lockfile, connections, configfile
218 * preference: command-line provides defaults.
219 * config file over-writes defaults.
221 * we also need to make sure that killing afpd during startup
222 * won't leave any lingering registered names around.
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));
233 sigprocmask(SIG_UNBLOCK, &sigs, NULL);
235 /* watch atp and dsi sockets. */
237 for (config = configs; config; config = config->next) {
238 if (config->fd < 0) /* for proxies */
240 FD_SET(config->fd, &save_rfds);
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
251 if (select(FD_SETSIZE, &rfds, NULL, NULL, NULL) < 0) {
254 LOG(log_error, logtype_afpd, "main: can't wait for input: %s", strerror(errno));
258 for (config = configs; config; config = config->next) {
261 if (FD_ISSET(config->fd, &rfds))
262 config->server_start(config, configs, server_children);