2 * Copyright (c) 1990,1993 Regents of The University of Michigan.
3 * All Rights Reserved. See COPYRIGHT.
13 #include <sys/types.h>
15 #include <sys/param.h>
17 #include <sys/syslog.h>
19 #include <sys/socket.h>
23 #include <netatalk/endian.h>
24 #include <netatalk/at.h>
25 #include <atalk/compat.h>
26 #include <atalk/dsi.h>
27 #include <atalk/atp.h>
28 #include <atalk/asp.h>
29 #include <atalk/afp.h>
30 #include <atalk/adouble.h>
31 #include <atalk/paths.h>
32 #include <atalk/util.h>
33 #include <atalk/server_child.h>
41 unsigned char nologin = 0;
43 static struct afp_options default_options;
44 static AFPConfig *configs;
45 static server_child *server_children;
46 static fd_set save_rfds;
49 int addr_net, addr_node, addr_uid;
53 static void afp_exit(const int i)
55 server_unlock(default_options.pidfile);
59 static void afp_goaway(int sig)
67 syslog( LOG_INFO, "shutting down on signal %d", sig );
70 /* w/ a configuration file, we can force a re-read if we want */
72 if ((nologin + 1) & 1) {
75 syslog(LOG_INFO, "re-reading configuration file");
76 for (config = configs; config; config = config->next)
77 if (config->server_cleanup)
78 config->server_cleanup(config);
80 configfree(configs, NULL);
81 if (!(configs = configinit(&default_options))) {
82 syslog(LOG_ERR, "config re-read: no servers configured");
86 for (config = configs; config; config = config->next) {
89 FD_SET(config->fd, &save_rfds);
92 syslog(LOG_INFO, "disallowing logins");
97 syslog( LOG_ERR, "afp_goaway: bad signal" );
99 if ( sig == SIGTERM ) {
102 for (config = configs; config; config = config->next)
103 if (config->server_cleanup)
104 config->server_cleanup(config);
111 static void child_handler()
113 server_child_handler(server_children);
125 umask( 0 ); /* so inherited file permissions work right */
127 afp_options_init(&default_options);
128 if (!afp_options_parse(ac, av, &default_options))
131 switch(server_lock("afpd", default_options.pidfile,
132 default_options.flags & OPTION_DEBUG)) {
137 default: /* server */
141 /* install child handler for asp and dsi. we do this before afp_goaway
142 * as afp_goaway references stuff from here.
143 * XXX: this should really be setup after the initial connections. */
144 if (!(server_children = server_child_alloc(default_options.connections,
146 syslog(LOG_ERR, "main: server_child alloc: %m");
150 memset(&sv, 0, sizeof(sv));
151 sv.sa_handler = child_handler;
152 sigemptyset( &sv.sa_mask );
153 sv.sa_flags = SA_RESTART;
154 if ( sigaction( SIGCHLD, &sv, 0 ) < 0 ) {
155 syslog( LOG_ERR, "main: sigaction: %m" );
159 sv.sa_handler = afp_goaway;
160 sigemptyset( &sv.sa_mask );
161 sigaddset(&sv.sa_mask, SIGHUP);
162 sigaddset(&sv.sa_mask, SIGTERM);
163 sv.sa_flags = SA_RESTART;
164 if ( sigaction( SIGHUP, &sv, 0 ) < 0 ) {
165 syslog( LOG_ERR, "main: sigaction: %m" );
168 if ( sigaction( SIGTERM, &sv, 0 ) < 0 ) {
169 syslog( LOG_ERR, "main: sigaction: %m" );
173 /* afpd.conf: not in config file: lockfile, connections, configfile
174 * preference: command-line provides defaults.
175 * config file over-writes defaults.
177 * we also need to make sure that killing afpd during startup
178 * won't leave any lingering registered names around.
182 sigaddset(&sigs, SIGHUP);
183 sigaddset(&sigs, SIGTERM);
184 sigprocmask(SIG_BLOCK, &sigs, NULL);
185 if (!(configs = configinit(&default_options))) {
186 syslog(LOG_ERR, "main: no servers configured: %m\n");
189 sigprocmask(SIG_UNBLOCK, &sigs, NULL);
191 /* watch atp and dsi sockets. */
193 for (config = configs; config; config = config->next) {
194 if (config->fd < 0) /* for proxies */
196 FD_SET(config->fd, &save_rfds);
199 /* wait for an appleshare connection. parent remains in the loop
200 * while the children get handled by afp_over_{asp,dsi}. this is
201 * currently vulnerable to a denial-of-service attack if a
202 * connection is made without an actual login attempt being made
203 * afterwards. establishing timeouts for logins is a possible
207 if (select(FD_SETSIZE, &rfds, NULL, NULL, NULL) < 0) {
210 syslog(LOG_ERR, "main: can't wait for input: %m");
214 for (config = configs; config; config = config->next) {
217 if (FD_ISSET(config->fd, &rfds))
218 config->server_start(config, configs, server_children);