2 * Copyright (c) 1990,1993 Regents of The University of Michigan.
3 * All Rights Reserved. See COPYRIGHT.
17 #include <sys/types.h>
19 #include <sys/param.h>
21 #include <sys/syslog.h>
23 #include <sys/socket.h>
27 #include <netatalk/endian.h>
28 #include <netatalk/at.h>
29 #include <atalk/compat.h>
30 #include <atalk/dsi.h>
31 #include <atalk/atp.h>
32 #include <atalk/asp.h>
33 #include <atalk/afp.h>
34 #include <atalk/adouble.h>
35 #include <atalk/paths.h>
36 #include <atalk/util.h>
37 #include <atalk/server_child.h>
40 #include "afp_config.h"
45 unsigned char nologin = 0;
47 static struct afp_options default_options;
48 static AFPConfig *configs;
49 static server_child *server_children;
50 static fd_set save_rfds;
53 int addr_net, addr_node, addr_uid;
57 static void afp_exit(const int i)
59 server_unlock(default_options.pidfile);
63 static void afp_goaway(int sig)
71 syslog( LOG_INFO, "shutting down on signal %d", sig );
74 /* w/ a configuration file, we can force a re-read if we want */
76 if ((nologin + 1) & 1) {
79 syslog(LOG_INFO, "re-reading configuration file");
80 for (config = configs; config; config = config->next)
81 if (config->server_cleanup)
82 config->server_cleanup(config);
84 configfree(configs, NULL);
85 if (!(configs = configinit(&default_options))) {
86 syslog(LOG_ERR, "config re-read: no servers configured");
90 for (config = configs; config; config = config->next) {
93 FD_SET(config->fd, &save_rfds);
96 syslog(LOG_INFO, "disallowing logins");
101 syslog( LOG_ERR, "afp_goaway: bad signal" );
103 if ( sig == SIGTERM ) {
106 for (config = configs; config; config = config->next)
107 if (config->server_cleanup)
108 config->server_cleanup(config);
115 static void child_handler()
117 server_child_handler(server_children);
129 umask( 0 ); /* so inherited file permissions work right */
131 afp_options_init(&default_options);
132 if (!afp_options_parse(ac, av, &default_options))
135 switch(server_lock("afpd", default_options.pidfile,
136 default_options.flags & OPTION_DEBUG)) {
141 default: /* server */
145 /* install child handler for asp and dsi. we do this before afp_goaway
146 * as afp_goaway references stuff from here.
147 * XXX: this should really be setup after the initial connections. */
148 if (!(server_children = server_child_alloc(default_options.connections,
150 syslog(LOG_ERR, "main: server_child alloc: %m");
154 memset(&sv, 0, sizeof(sv));
155 sv.sa_handler = child_handler;
156 sigemptyset( &sv.sa_mask );
157 sv.sa_flags = SA_RESTART;
158 if ( sigaction( SIGCHLD, &sv, 0 ) < 0 ) {
159 syslog( LOG_ERR, "main: sigaction: %m" );
163 sv.sa_handler = afp_goaway;
164 sigemptyset( &sv.sa_mask );
165 sigaddset(&sv.sa_mask, SIGHUP);
166 sigaddset(&sv.sa_mask, SIGTERM);
167 sv.sa_flags = SA_RESTART;
168 if ( sigaction( SIGHUP, &sv, 0 ) < 0 ) {
169 syslog( LOG_ERR, "main: sigaction: %m" );
172 if ( sigaction( SIGTERM, &sv, 0 ) < 0 ) {
173 syslog( LOG_ERR, "main: sigaction: %m" );
177 /* afpd.conf: not in config file: lockfile, connections, configfile
178 * preference: command-line provides defaults.
179 * config file over-writes defaults.
181 * we also need to make sure that killing afpd during startup
182 * won't leave any lingering registered names around.
186 sigaddset(&sigs, SIGHUP);
187 sigaddset(&sigs, SIGTERM);
188 sigprocmask(SIG_BLOCK, &sigs, NULL);
189 if (!(configs = configinit(&default_options))) {
190 syslog(LOG_ERR, "main: no servers configured: %m\n");
193 sigprocmask(SIG_UNBLOCK, &sigs, NULL);
195 /* watch atp and dsi sockets. */
197 for (config = configs; config; config = config->next) {
198 if (config->fd < 0) /* for proxies */
200 FD_SET(config->fd, &save_rfds);
203 /* wait for an appleshare connection. parent remains in the loop
204 * while the children get handled by afp_over_{asp,dsi}. this is
205 * currently vulnerable to a denial-of-service attack if a
206 * connection is made without an actual login attempt being made
207 * afterwards. establishing timeouts for logins is a possible
211 if (select(FD_SETSIZE, &rfds, NULL, NULL, NULL) < 0) {
214 syslog(LOG_ERR, "main: can't wait for input: %m");
218 for (config = configs; config; config = config->next) {
221 if (FD_ISSET(config->fd, &rfds))
222 config->server_start(config, configs, server_children);