2 * $Id: main.c,v 1.21 2003-05-16 15:29:27 didg 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>
44 #include <atalk/server_ipc.h>
47 #include "afp_config.h"
53 #include <sys/security.h>
58 static char **argv = NULL;
61 unsigned char nologin = 0;
63 struct afp_options default_options;
64 static AFPConfig *configs;
65 static server_child *server_children;
66 static fd_set save_rfds;
67 static int Ipc_fd = -1;
70 void afp_get_cmdline( int *ac, char ***av)
77 static void afp_exit(const int i)
79 server_unlock(default_options.pidfile);
84 initialize fd set we are waiting for.
86 static void set_fd(int ipc_fd)
91 for (config = configs; config; config = config->next) {
92 if (config->fd < 0) /* for proxies */
94 FD_SET(config->fd, &save_rfds);
97 FD_SET(ipc_fd, &save_rfds);
101 /* ------------------ */
102 static void afp_goaway(int sig)
106 #endif /* ! NO_DDP */
110 LOG(log_info, logtype_afpd, "shutting down on signal %d", sig );
113 /* w/ a configuration file, we can force a re-read if we want */
115 if ((nologin + 1) & 1) {
118 LOG(log_info, logtype_afpd, "re-reading configuration file");
119 for (config = configs; config; config = config->next)
120 if (config->server_cleanup)
121 config->server_cleanup(config);
123 configfree(configs, NULL);
124 if (!(configs = configinit(&default_options))) {
125 LOG(log_error, logtype_afpd, "config re-read: no servers configured");
130 LOG(log_info, logtype_afpd, "disallowing logins");
135 LOG(log_error, logtype_afpd, "afp_goaway: bad signal" );
137 if ( sig == SIGTERM ) {
140 for (config = configs; config; config = config->next)
141 if (config->server_cleanup)
142 config->server_cleanup(config);
149 static void child_handler()
151 server_child_handler(server_children);
167 set_auth_parameters( ac, av );
170 afp_options_init(&default_options);
171 if (!afp_options_parse(ac, av, &default_options))
174 /* Save the user's current umask for use with CNID (and maybe some
175 * other things, too). */
176 default_options.save_mask = umask( default_options.umask );
178 switch(server_lock("afpd", default_options.pidfile,
179 default_options.flags & OPTION_DEBUG)) {
184 default: /* server */
188 /* install child handler for asp and dsi. we do this before afp_goaway
189 * as afp_goaway references stuff from here.
190 * XXX: this should really be setup after the initial connections. */
191 if (!(server_children = server_child_alloc(default_options.connections,
193 LOG(log_error, logtype_afpd, "main: server_child alloc: %s", strerror(errno) );
197 memset(&sv, 0, sizeof(sv));
198 sv.sa_handler = child_handler;
199 sigemptyset( &sv.sa_mask );
200 sv.sa_flags = SA_RESTART;
201 if ( sigaction( SIGCHLD, &sv, 0 ) < 0 ) {
202 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
206 sv.sa_handler = afp_goaway;
207 sigemptyset( &sv.sa_mask );
208 sigaddset(&sv.sa_mask, SIGHUP);
209 sigaddset(&sv.sa_mask, SIGTERM);
210 sv.sa_flags = SA_RESTART;
211 if ( sigaction( SIGHUP, &sv, 0 ) < 0 ) {
212 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
215 if ( sigaction( SIGTERM, &sv, 0 ) < 0 ) {
216 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
220 /* afpd.conf: not in config file: lockfile, connections, configfile
221 * preference: command-line provides defaults.
222 * config file over-writes defaults.
224 * we also need to make sure that killing afpd during startup
225 * won't leave any lingering registered names around.
229 sigaddset(&sigs, SIGHUP);
230 sigaddset(&sigs, SIGTERM);
231 sigprocmask(SIG_BLOCK, &sigs, NULL);
232 if (!(configs = configinit(&default_options))) {
233 LOG(log_error, logtype_afpd, "main: no servers configured: %s\n", strerror(errno));
236 sigprocmask(SIG_UNBLOCK, &sigs, NULL);
238 /* watch atp, dsi sockets and ipc parent/child file descriptor. */
239 if ((ipc = server_ipc_create())) {
240 Ipc_fd = server_ipc_parent(ipc);
244 /* wait for an appleshare connection. parent remains in the loop
245 * while the children get handled by afp_over_{asp,dsi}. this is
246 * currently vulnerable to a denial-of-service attack if a
247 * connection is made without an actual login attempt being made
248 * afterwards. establishing timeouts for logins is a possible
252 if (select(FD_SETSIZE, &rfds, NULL, NULL, NULL) < 0) {
255 LOG(log_error, logtype_afpd, "main: can't wait for input: %s", strerror(errno));
258 if (Ipc_fd >=0 && FD_ISSET(Ipc_fd, &rfds)) {
259 server_ipc_read(server_children);
261 for (config = configs; config; config = config->next) {
264 if (FD_ISSET(config->fd, &rfds))
265 config->server_start(config, configs, server_children);