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 /* Default log setup: log to syslog */
154 setuplog("default log_note");
156 afp_options_init(&default_options);
157 if (!afp_options_parse(ac, av, &default_options))
160 /* Save the user's current umask for use with CNID (and maybe some
161 * other things, too). */
162 default_options.save_mask = umask( default_options.umask );
164 switch(server_lock("afpd", default_options.pidfile,
165 default_options.flags & OPTION_DEBUG)) {
170 default: /* server */
174 /* install child handler for asp and dsi. we do this before afp_goaway
175 * as afp_goaway references stuff from here.
176 * XXX: this should really be setup after the initial connections. */
177 if (!(server_children = server_child_alloc(default_options.connections,
179 LOG(log_error, logtype_afpd, "main: server_child alloc: %s", strerror(errno) );
180 afp_exit(EXITERR_SYS);
183 memset(&sv, 0, sizeof(sv));
185 /* linux at least up to 2.4.22 send a SIGXFZ for vfat fs,
186 even if the file is open with O_LARGEFILE ! */
188 sv.sa_handler = SIG_IGN;
189 sigemptyset( &sv.sa_mask );
190 if (sigaction(SIGXFSZ, &sv, NULL ) < 0 ) {
191 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
192 afp_exit(EXITERR_SYS);
197 sv.sa_handler = child_handler;
198 sigemptyset( &sv.sa_mask );
199 sigaddset(&sv.sa_mask, SIGALRM);
200 sigaddset(&sv.sa_mask, SIGHUP);
201 sigaddset(&sv.sa_mask, SIGTERM);
202 sigaddset(&sv.sa_mask, SIGUSR1);
204 sv.sa_flags = SA_RESTART;
205 if ( sigaction( SIGCHLD, &sv, NULL ) < 0 ) {
206 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
207 afp_exit(EXITERR_SYS);
210 sv.sa_handler = afp_goaway;
211 sigemptyset( &sv.sa_mask );
212 sigaddset(&sv.sa_mask, SIGALRM);
213 sigaddset(&sv.sa_mask, SIGTERM);
214 sigaddset(&sv.sa_mask, SIGHUP);
215 sigaddset(&sv.sa_mask, SIGCHLD);
216 sv.sa_flags = SA_RESTART;
217 if ( sigaction( SIGUSR1, &sv, NULL ) < 0 ) {
218 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
219 afp_exit(EXITERR_SYS);
222 sigemptyset( &sv.sa_mask );
223 sigaddset(&sv.sa_mask, SIGALRM);
224 sigaddset(&sv.sa_mask, SIGTERM);
225 sigaddset(&sv.sa_mask, SIGUSR1);
226 sigaddset(&sv.sa_mask, SIGCHLD);
227 sv.sa_flags = SA_RESTART;
228 if ( sigaction( SIGHUP, &sv, NULL ) < 0 ) {
229 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
230 afp_exit(EXITERR_SYS);
234 sigemptyset( &sv.sa_mask );
235 sigaddset(&sv.sa_mask, SIGALRM);
236 sigaddset(&sv.sa_mask, SIGHUP);
237 sigaddset(&sv.sa_mask, SIGUSR1);
238 sigaddset(&sv.sa_mask, SIGCHLD);
239 sv.sa_flags = SA_RESTART;
240 if ( sigaction( SIGTERM, &sv, NULL ) < 0 ) {
241 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
242 afp_exit(EXITERR_SYS);
245 /* afpd.conf: not in config file: lockfile, connections, configfile
246 * preference: command-line provides defaults.
247 * config file over-writes defaults.
249 * we also need to make sure that killing afpd during startup
250 * won't leave any lingering registered names around.
254 sigaddset(&sigs, SIGALRM);
255 sigaddset(&sigs, SIGHUP);
256 sigaddset(&sigs, SIGUSR1);
258 /* don't block SIGTERM */
259 sigaddset(&sigs, SIGTERM);
261 sigaddset(&sigs, SIGCHLD);
263 pthread_sigmask(SIG_BLOCK, &sigs, NULL);
264 if (!(configs = configinit(&default_options))) {
265 LOG(log_error, logtype_afpd, "main: no servers configured");
266 afp_exit(EXITERR_CONF);
268 pthread_sigmask(SIG_UNBLOCK, &sigs, NULL);
273 /* watch atp, dsi sockets and ipc parent/child file descriptor. */
274 if ((ipc = server_ipc_create())) {
275 Ipc_fd = server_ipc_parent(ipc);
279 /* wait for an appleshare connection. parent remains in the loop
280 * while the children get handled by afp_over_{asp,dsi}. this is
281 * currently vulnerable to a denial-of-service attack if a
282 * connection is made without an actual login attempt being made
283 * afterwards. establishing timeouts for logins is a possible
287 pthread_sigmask(SIG_UNBLOCK, &sigs, NULL);
288 ret = select(FD_SETSIZE, &rfds, NULL, NULL, NULL);
289 pthread_sigmask(SIG_BLOCK, &sigs, NULL);
290 int saveerrno = errno;
297 LOG(log_info, logtype_afpd, "re-reading configuration file");
298 for (config = configs; config; config = config->next)
299 if (config->server_cleanup)
300 config->server_cleanup(config);
302 /* configfree close atp socket used for DDP tickle, there's an issue
304 configfree(configs, NULL);
305 if (!(configs = configinit(&default_options))) {
306 LOG(log_error, logtype_afpd, "config re-read: no servers configured");
307 afp_exit(EXITERR_CONF);
318 LOG(log_error, logtype_afpd, "main: can't wait for input: %s", strerror(errno));
321 if (Ipc_fd >=0 && FD_ISSET(Ipc_fd, &rfds)) {
322 server_ipc_read(server_children);
324 for (config = configs; config; config = config->next) {
327 if (FD_ISSET(config->fd, &rfds)) {
328 config->server_start(config, configs, server_children);