2 * Copyright (c) 1990,1993 Regents of The University of Michigan.
3 * All Rights Reserved. See COPYRIGHT.
8 #endif /* HAVE_CONFIG_H */
14 #include <sys/param.h>
17 #include <sys/socket.h>
20 #include <atalk/logger.h>
21 #include <atalk/adouble.h>
22 #include <netatalk/at.h>
23 #include <atalk/compat.h>
24 #include <atalk/dsi.h>
25 #include <atalk/atp.h>
26 #include <atalk/asp.h>
27 #include <atalk/afp.h>
28 #include <atalk/paths.h>
29 #include <atalk/util.h>
30 #include <atalk/server_child.h>
31 #include <atalk/server_ipc.h>
32 #include <atalk/errchk.h>
33 #include <atalk/locking.h>
35 #include "event2/event.h"
36 #include "event2/http.h"
37 #include "event2/rpc.h"
40 #include "afp_config.h"
44 #include "afp_zeroconf.h"
47 #include <sys/security.h>
52 static char **argv = NULL;
55 unsigned char nologin = 0;
57 struct afp_options default_options;
59 static AFPConfig *configs;
60 static server_child *server_children;
61 static fd_set save_rfds;
62 static int Ipc_fd = -1;
63 static sig_atomic_t reloadconfig = 0;
66 void afp_get_cmdline( int *ac, char ***av)
73 static void afp_exit(const int i)
75 server_unlock(default_options.pidfile);
80 initialize fd set we are waiting for.
82 static void set_fd(int ipc_fd)
87 for (config = configs; config; config = config->next) {
88 if (config->fd < 0) /* for proxies */
90 FD_SET(config->fd, &save_rfds);
93 FD_SET(ipc_fd, &save_rfds);
97 /* ------------------ */
98 static void afp_goaway(int sig)
103 #endif /* ! NO_DDP */
109 LOG(log_note, logtype_afpd, "AFP Server shutting down on SIGTERM");
111 for (config = configs; config; config = config->next)
112 if (config->server_cleanup)
113 config->server_cleanup(config);
120 LOG(log_info, logtype_afpd, "disallowing logins");
124 /* w/ a configuration file, we can force a re-read if we want */
129 LOG(log_error, logtype_afpd, "afp_goaway: bad signal" );
134 static void child_handler(int sig _U_)
136 server_child_handler(server_children);
139 int main(int ac, char **av)
151 set_auth_parameters( ac, av );
154 /* Log SIGBUS/SIGSEGV SBT */
157 /* Default log setup: log to syslog */
158 setuplog("default log_note");
160 afp_options_init(&default_options);
161 if (!afp_options_parse(ac, av, &default_options))
164 /* Save the user's current umask for use with CNID (and maybe some
165 * other things, too). */
166 default_options.save_mask = umask( default_options.umask );
168 switch(server_lock("afpd", default_options.pidfile,
169 default_options.flags & OPTION_DEBUG)) {
174 default: /* server */
178 /* install child handler for asp and dsi. we do this before afp_goaway
179 * as afp_goaway references stuff from here.
180 * XXX: this should really be setup after the initial connections. */
181 if (!(server_children = server_child_alloc(default_options.connections,
183 LOG(log_error, logtype_afpd, "main: server_child alloc: %s", strerror(errno) );
184 afp_exit(EXITERR_SYS);
187 memset(&sv, 0, sizeof(sv));
189 /* linux at least up to 2.4.22 send a SIGXFZ for vfat fs,
190 even if the file is open with O_LARGEFILE ! */
192 sv.sa_handler = SIG_IGN;
193 sigemptyset( &sv.sa_mask );
194 if (sigaction(SIGXFSZ, &sv, NULL ) < 0 ) {
195 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
196 afp_exit(EXITERR_SYS);
201 sv.sa_handler = child_handler;
202 sigemptyset( &sv.sa_mask );
203 sigaddset(&sv.sa_mask, SIGALRM);
204 sigaddset(&sv.sa_mask, SIGHUP);
205 sigaddset(&sv.sa_mask, SIGTERM);
206 sigaddset(&sv.sa_mask, SIGUSR1);
208 sv.sa_flags = SA_RESTART;
209 if ( sigaction( SIGCHLD, &sv, NULL ) < 0 ) {
210 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
211 afp_exit(EXITERR_SYS);
214 sv.sa_handler = afp_goaway;
215 sigemptyset( &sv.sa_mask );
216 sigaddset(&sv.sa_mask, SIGALRM);
217 sigaddset(&sv.sa_mask, SIGTERM);
218 sigaddset(&sv.sa_mask, SIGHUP);
219 sigaddset(&sv.sa_mask, SIGCHLD);
220 sv.sa_flags = SA_RESTART;
221 if ( sigaction( SIGUSR1, &sv, NULL ) < 0 ) {
222 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
223 afp_exit(EXITERR_SYS);
226 sigemptyset( &sv.sa_mask );
227 sigaddset(&sv.sa_mask, SIGALRM);
228 sigaddset(&sv.sa_mask, SIGTERM);
229 sigaddset(&sv.sa_mask, SIGUSR1);
230 sigaddset(&sv.sa_mask, SIGCHLD);
231 sv.sa_flags = SA_RESTART;
232 if ( sigaction( SIGHUP, &sv, NULL ) < 0 ) {
233 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
234 afp_exit(EXITERR_SYS);
238 sigemptyset( &sv.sa_mask );
239 sigaddset(&sv.sa_mask, SIGALRM);
240 sigaddset(&sv.sa_mask, SIGHUP);
241 sigaddset(&sv.sa_mask, SIGUSR1);
242 sigaddset(&sv.sa_mask, SIGCHLD);
243 sv.sa_flags = SA_RESTART;
244 if ( sigaction( SIGTERM, &sv, NULL ) < 0 ) {
245 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
246 afp_exit(EXITERR_SYS);
249 /* afpd.conf: not in config file: lockfile, connections, configfile
250 * preference: command-line provides defaults.
251 * config file over-writes defaults.
253 * we also need to make sure that killing afpd during startup
254 * won't leave any lingering registered names around.
258 sigaddset(&sigs, SIGALRM);
259 sigaddset(&sigs, SIGHUP);
260 sigaddset(&sigs, SIGUSR1);
262 /* don't block SIGTERM */
263 sigaddset(&sigs, SIGTERM);
265 sigaddset(&sigs, SIGCHLD);
267 pthread_sigmask(SIG_BLOCK, &sigs, NULL);
268 if (!(configs = configinit(&default_options))) {
269 LOG(log_error, logtype_afpd, "main: no servers configured");
270 afp_exit(EXITERR_CONF);
272 pthread_sigmask(SIG_UNBLOCK, &sigs, NULL);
276 if (rpc_init("127.0.0.1", 4701) != 0)
277 afp_exit(EXITERR_SYS);
279 /* watch atp, dsi sockets and ipc parent/child file descriptor. */
280 if ((ipc = server_ipc_create())) {
281 Ipc_fd = server_ipc_parent(ipc);
285 /* wait for an appleshare connection. parent remains in the loop
286 * while the children get handled by afp_over_{asp,dsi}. this is
287 * currently vulnerable to a denial-of-service attack if a
288 * connection is made without an actual login attempt being made
289 * afterwards. establishing timeouts for logins is a possible
293 pthread_sigmask(SIG_UNBLOCK, &sigs, NULL);
294 ret = select(FD_SETSIZE, &rfds, NULL, NULL, NULL);
295 pthread_sigmask(SIG_BLOCK, &sigs, NULL);
296 int saveerrno = errno;
303 LOG(log_info, logtype_afpd, "re-reading configuration file");
304 for (config = configs; config; config = config->next)
305 if (config->server_cleanup)
306 config->server_cleanup(config);
308 /* configfree close atp socket used for DDP tickle, there's an issue
310 configfree(configs, NULL);
311 if (!(configs = configinit(&default_options))) {
312 LOG(log_error, logtype_afpd, "config re-read: no servers configured");
313 afp_exit(EXITERR_CONF);
324 LOG(log_error, logtype_afpd, "main: can't wait for input: %s", strerror(errno));
327 if (Ipc_fd >=0 && FD_ISSET(Ipc_fd, &rfds)) {
328 server_ipc_read(server_children);
330 for (config = configs; config; config = config->next) {
333 if (FD_ISSET(config->fd, &rfds)) {
334 config->server_start(config, configs, server_children);