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;
48 static void afp_exit(const int i)
50 server_unlock(default_options.pidfile);
54 static void afp_goaway(int sig)
62 syslog( LOG_INFO, "shutting down on signal %d", sig );
65 /* w/ a configuration file, we can force a re-read if we want */
67 if ((nologin + 1) & 1) {
70 syslog(LOG_INFO, "re-reading configuration file");
71 for (config = configs; config; config = config->next)
72 if (config->server_cleanup)
73 config->server_cleanup(config);
75 configfree(configs, NULL);
76 if (!(configs = configinit(&default_options))) {
77 syslog(LOG_ERR, "config re-read: no servers configured");
81 for (config = configs; config; config = config->next) {
84 FD_SET(config->fd, &save_rfds);
87 syslog(LOG_INFO, "disallowing logins");
92 syslog( LOG_ERR, "afp_goaway: bad signal" );
94 if ( sig == SIGTERM ) {
97 for (config = configs; config; config = config->next)
98 if (config->server_cleanup)
99 config->server_cleanup(config);
106 static void child_handler()
108 server_child_handler(server_children);
120 umask( 0 ); /* so inherited file permissions work right */
122 afp_options_init(&default_options);
123 if (!afp_options_parse(ac, av, &default_options))
126 switch(server_lock("afpd", default_options.pidfile,
127 default_options.flags & OPTION_DEBUG)) {
132 default: /* server */
136 /* install child handler for asp and dsi. we do this before afp_goaway
137 * as afp_goaway references stuff from here.
138 * XXX: this should really be setup after the initial connections. */
139 if (!(server_children = server_child_alloc(default_options.connections,
141 syslog(LOG_ERR, "main: server_child alloc: %m");
145 memset(&sv, 0, sizeof(sv));
146 sv.sa_handler = child_handler;
147 sigemptyset( &sv.sa_mask );
148 sv.sa_flags = SA_RESTART;
149 if ( sigaction( SIGCHLD, &sv, 0 ) < 0 ) {
150 syslog( LOG_ERR, "main: sigaction: %m" );
154 sv.sa_handler = afp_goaway;
155 sigemptyset( &sv.sa_mask );
156 sigaddset(&sv.sa_mask, SIGHUP);
157 sigaddset(&sv.sa_mask, SIGTERM);
158 sv.sa_flags = SA_RESTART;
159 if ( sigaction( SIGHUP, &sv, 0 ) < 0 ) {
160 syslog( LOG_ERR, "main: sigaction: %m" );
163 if ( sigaction( SIGTERM, &sv, 0 ) < 0 ) {
164 syslog( LOG_ERR, "main: sigaction: %m" );
168 /* afpd.conf: not in config file: lockfile, connections, configfile
169 * preference: command-line provides defaults.
170 * config file over-writes defaults.
172 * we also need to make sure that killing afpd during startup
173 * won't leave any lingering registered names around.
177 sigaddset(&sigs, SIGHUP);
178 sigaddset(&sigs, SIGTERM);
179 sigprocmask(SIG_BLOCK, &sigs, NULL);
180 if (!(configs = configinit(&default_options))) {
181 syslog(LOG_ERR, "main: no servers configured: %m\n");
184 sigprocmask(SIG_UNBLOCK, &sigs, NULL);
186 /* watch atp and dsi sockets. */
188 for (config = configs; config; config = config->next) {
189 if (config->fd < 0) /* for proxies */
191 FD_SET(config->fd, &save_rfds);
194 /* wait for an appleshare connection. parent remains in the loop
195 * while the children get handled by afp_over_{asp,dsi}. this is
196 * currently vulnerable to a denial-of-service attack if a
197 * connection is made without an actual login attempt being made
198 * afterwards. establishing timeouts for logins is a possible
202 if (select(FD_SETSIZE, &rfds, NULL, NULL, NULL) < 0) {
205 syslog(LOG_ERR, "main: can't wait for input: %m");
209 for (config = configs; config; config = config->next) {
212 if (FD_ISSET(config->fd, &rfds))
213 config->server_start(config, configs, server_children);