2 * Copyright (c) 1990,1993 Regents of The University of Michigan.
3 * All Rights Reserved. See COPYRIGHT.
8 #endif /* HAVE_CONFIG_H */
15 #include <sys/param.h>
17 #include <atalk/logger.h>
19 #include <sys/socket.h>
23 #include <atalk/adouble.h>
25 #include <netatalk/at.h>
26 #include <atalk/compat.h>
27 #include <atalk/dsi.h>
28 #include <atalk/atp.h>
29 #include <atalk/asp.h>
30 #include <atalk/afp.h>
31 #include <atalk/paths.h>
32 #include <atalk/util.h>
33 #include <atalk/server_child.h>
34 #include <atalk/server_ipc.h>
37 #include "afp_config.h"
41 #include "afp_zeroconf.h"
44 #include <sys/security.h>
49 static char **argv = NULL;
52 unsigned char nologin = 0;
54 struct afp_options default_options;
55 static AFPConfig *configs;
56 static server_child *server_children;
57 static fd_set save_rfds;
58 static int Ipc_fd = -1;
59 static sig_atomic_t reloadconfig = 0;
62 void afp_get_cmdline( int *ac, char ***av)
69 static void afp_exit(const int i)
71 server_unlock(default_options.pidfile);
76 initialize fd set we are waiting for.
78 static void set_fd(int ipc_fd)
83 for (config = configs; config; config = config->next) {
84 if (config->fd < 0) /* for proxies */
86 FD_SET(config->fd, &save_rfds);
89 FD_SET(ipc_fd, &save_rfds);
93 /* ------------------ */
94 static void afp_goaway(int sig)
105 LOG(log_note, logtype_afpd, "AFP Server shutting down on SIGTERM");
107 for (config = configs; config; config = config->next)
108 if (config->server_cleanup)
109 config->server_cleanup(config);
116 LOG(log_info, logtype_afpd, "disallowing logins");
120 /* w/ a configuration file, we can force a re-read if we want */
125 LOG(log_error, logtype_afpd, "afp_goaway: bad signal" );
130 static void child_handler(int sig _U_)
132 server_child_handler(server_children);
135 int main(int ac, char **av)
147 set_auth_parameters( ac, av );
150 /* Log SIGBUS/SIGSEGV SBT */
153 afp_options_init(&default_options);
154 if (!afp_options_parse(ac, av, &default_options))
157 /* Save the user's current umask for use with CNID (and maybe some
158 * other things, too). */
159 default_options.save_mask = umask( default_options.umask );
161 switch(server_lock("afpd", default_options.pidfile,
162 default_options.flags & OPTION_DEBUG)) {
167 default: /* server */
171 /* install child handler for asp and dsi. we do this before afp_goaway
172 * as afp_goaway references stuff from here.
173 * XXX: this should really be setup after the initial connections. */
174 if (!(server_children = server_child_alloc(default_options.connections,
176 LOG(log_error, logtype_afpd, "main: server_child alloc: %s", strerror(errno) );
177 afp_exit(EXITERR_SYS);
180 memset(&sv, 0, sizeof(sv));
182 /* linux at least up to 2.4.22 send a SIGXFZ for vfat fs,
183 even if the file is open with O_LARGEFILE ! */
185 sv.sa_handler = SIG_IGN;
186 sigemptyset( &sv.sa_mask );
187 if (sigaction(SIGXFSZ, &sv, NULL ) < 0 ) {
188 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
189 afp_exit(EXITERR_SYS);
194 sv.sa_handler = child_handler;
195 sigemptyset( &sv.sa_mask );
196 sigaddset(&sv.sa_mask, SIGALRM);
197 sigaddset(&sv.sa_mask, SIGHUP);
198 sigaddset(&sv.sa_mask, SIGTERM);
199 sigaddset(&sv.sa_mask, SIGUSR1);
201 sv.sa_flags = SA_RESTART;
202 if ( sigaction( SIGCHLD, &sv, NULL ) < 0 ) {
203 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
204 afp_exit(EXITERR_SYS);
207 sv.sa_handler = afp_goaway;
208 sigemptyset( &sv.sa_mask );
209 sigaddset(&sv.sa_mask, SIGALRM);
210 sigaddset(&sv.sa_mask, SIGTERM);
211 sigaddset(&sv.sa_mask, SIGHUP);
212 sigaddset(&sv.sa_mask, SIGCHLD);
213 sv.sa_flags = SA_RESTART;
214 if ( sigaction( SIGUSR1, &sv, NULL ) < 0 ) {
215 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
216 afp_exit(EXITERR_SYS);
219 sigemptyset( &sv.sa_mask );
220 sigaddset(&sv.sa_mask, SIGALRM);
221 sigaddset(&sv.sa_mask, SIGTERM);
222 sigaddset(&sv.sa_mask, SIGUSR1);
223 sigaddset(&sv.sa_mask, SIGCHLD);
224 sv.sa_flags = SA_RESTART;
225 if ( sigaction( SIGHUP, &sv, NULL ) < 0 ) {
226 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
227 afp_exit(EXITERR_SYS);
231 sigemptyset( &sv.sa_mask );
232 sigaddset(&sv.sa_mask, SIGALRM);
233 sigaddset(&sv.sa_mask, SIGHUP);
234 sigaddset(&sv.sa_mask, SIGUSR1);
235 sigaddset(&sv.sa_mask, SIGCHLD);
236 sv.sa_flags = SA_RESTART;
237 if ( sigaction( SIGTERM, &sv, NULL ) < 0 ) {
238 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
239 afp_exit(EXITERR_SYS);
242 /* afpd.conf: not in config file: lockfile, connections, configfile
243 * preference: command-line provides defaults.
244 * config file over-writes defaults.
246 * we also need to make sure that killing afpd during startup
247 * won't leave any lingering registered names around.
251 sigaddset(&sigs, SIGALRM);
252 sigaddset(&sigs, SIGHUP);
253 sigaddset(&sigs, SIGUSR1);
255 /* don't block SIGTERM */
256 sigaddset(&sigs, SIGTERM);
258 sigaddset(&sigs, SIGCHLD);
260 pthread_sigmask(SIG_BLOCK, &sigs, NULL);
261 if (!(configs = configinit(&default_options))) {
262 LOG(log_error, logtype_afpd, "main: no servers configured");
263 afp_exit(EXITERR_CONF);
265 pthread_sigmask(SIG_UNBLOCK, &sigs, NULL);
270 /* watch atp, dsi sockets and ipc parent/child file descriptor. */
271 if ((ipc = server_ipc_create())) {
272 Ipc_fd = server_ipc_parent(ipc);
276 /* wait for an appleshare connection. parent remains in the loop
277 * while the children get handled by afp_over_{asp,dsi}. this is
278 * currently vulnerable to a denial-of-service attack if a
279 * connection is made without an actual login attempt being made
280 * afterwards. establishing timeouts for logins is a possible
284 pthread_sigmask(SIG_UNBLOCK, &sigs, NULL);
285 ret = select(FD_SETSIZE, &rfds, NULL, NULL, NULL);
286 pthread_sigmask(SIG_BLOCK, &sigs, NULL);
287 int saveerrno = errno;
294 LOG(log_info, logtype_afpd, "re-reading configuration file");
295 for (config = configs; config; config = config->next)
296 if (config->server_cleanup)
297 config->server_cleanup(config);
299 /* configfree close atp socket used for DDP tickle, there's an issue
301 configfree(configs, NULL);
302 if (!(configs = configinit(&default_options))) {
303 LOG(log_error, logtype_afpd, "config re-read: no servers configured");
304 afp_exit(EXITERR_CONF);
315 LOG(log_error, logtype_afpd, "main: can't wait for input: %s", strerror(errno));
318 if (Ipc_fd >=0 && FD_ISSET(Ipc_fd, &rfds)) {
319 server_ipc_read(server_children);
321 for (config = configs; config; config = config->next) {
324 if (FD_ISSET(config->fd, &rfds)) {
325 config->server_start(config, configs, server_children);