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>
21 #include <sys/resource.h>
23 #include <atalk/logger.h>
24 #include <atalk/adouble.h>
25 #include <atalk/compat.h>
26 #include <atalk/dsi.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/globals.h>
34 #include <atalk/netatalk_conf.h>
36 #include "afp_config.h"
40 #include "afp_zeroconf.h"
43 #define ASEV_THRESHHOLD 10
45 unsigned char nologin = 0;
48 static server_child_t *server_children;
49 static sig_atomic_t reloadconfig = 0;
50 static sig_atomic_t gotsigchld = 0;
51 static struct asev *asev;
53 static afp_child_t *dsi_start(AFPObj *obj, DSI *dsi, server_child_t *server_children);
55 static void afp_exit(int ret)
62 initialize fd set we are waiting for.
64 static bool init_listening_sockets(const AFPObj *config)
69 for (numlisteners = 0, dsi = config->dsi; dsi; dsi = dsi->next) {
73 asev = asev_init(config->options.connections + numlisteners + ASEV_THRESHHOLD);
78 for (dsi = config->dsi; dsi; dsi = dsi->next) {
79 if (!(asev_add_fd(asev, dsi->serversock, LISTEN_FD, dsi))) {
87 static bool reset_listening_sockets(const AFPObj *config)
91 for (dsi = config->dsi; dsi; dsi = dsi->next) {
92 if (!(asev_del_fd(asev, dsi->serversock))) {
99 /* ------------------ */
100 static void afp_goaway(int sig)
106 LOG(log_note, logtype_afpd, "AFP Server shutting down");
108 server_child_kill(server_children, SIGTERM);
115 LOG(log_info, logtype_afpd, "disallowing logins");
118 server_child_kill(server_children, sig);
122 /* w/ a configuration file, we can force a re-read if we want */
127 /* w/ a configuration file, we can force a re-read if we want */
132 LOG(log_error, logtype_afpd, "afp_goaway: bad signal" );
137 static void child_handler(void)
144 #define WAIT_ANY (-1)
145 #endif /* ! WAIT_ANY */
147 while ((pid = waitpid(WAIT_ANY, &status, WNOHANG)) > 0) {
148 if (WIFEXITED(status)) {
149 if (WEXITSTATUS(status))
150 LOG(log_info, logtype_afpd, "child[%d]: exited %d", pid, WEXITSTATUS(status));
152 LOG(log_info, logtype_afpd, "child[%d]: done", pid);
154 if (WIFSIGNALED(status))
155 LOG(log_info, logtype_afpd, "child[%d]: killed by signal %d", pid, WTERMSIG(status));
157 LOG(log_info, logtype_afpd, "child[%d]: died", pid);
160 fd = server_child_remove(server_children, pid);
164 if (!(asev_del_fd(asev, fd))) {
165 LOG(log_error, logtype_afpd, "child[%d]: asev_del_fd: %d", pid, fd);
170 static int setlimits(void)
174 if (getrlimit(RLIMIT_NOFILE, &rlim) != 0) {
175 LOG(log_warning, logtype_afpd, "setlimits: reading current limits failed: %s", strerror(errno));
178 if (rlim.rlim_cur != RLIM_INFINITY && rlim.rlim_cur < 65535) {
179 rlim.rlim_cur = 65535;
180 if (rlim.rlim_max != RLIM_INFINITY && rlim.rlim_max < 65535)
181 rlim.rlim_max = 65535;
182 if (setrlimit(RLIMIT_NOFILE, &rlim) != 0) {
183 LOG(log_warning, logtype_afpd, "setlimits: increasing limits failed: %s", strerror(errno));
190 int main(int ac, char **av)
196 /* Parse argv args and initialize default options */
197 afp_options_parse_cmdline(&obj, ac, av);
199 if (!(obj.cmdlineflags & OPTION_DEBUG) && (daemonize(0, 0) != 0))
202 /* Log SIGBUS/SIGSEGV SBT */
205 if (afp_config_parse(&obj, "afpd") != 0)
206 afp_exit(EXITERR_CONF);
208 /* Save the user's current umask */
209 obj.options.save_mask = umask(obj.options.umask);
211 /* install child handler for asp and dsi. we do this before afp_goaway
212 * as afp_goaway references stuff from here.
213 * XXX: this should really be setup after the initial connections. */
214 if (!(server_children = server_child_alloc(obj.options.connections))) {
215 LOG(log_error, logtype_afpd, "main: server_child alloc: %s", strerror(errno) );
216 afp_exit(EXITERR_SYS);
220 pthread_sigmask(SIG_SETMASK, &sigs, NULL);
222 memset(&sv, 0, sizeof(sv));
223 /* linux at least up to 2.4.22 send a SIGXFZ for vfat fs,
224 even if the file is open with O_LARGEFILE ! */
226 sv.sa_handler = SIG_IGN;
227 sigemptyset( &sv.sa_mask );
228 if (sigaction(SIGXFSZ, &sv, NULL ) < 0 ) {
229 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
230 afp_exit(EXITERR_SYS);
234 sv.sa_handler = SIG_IGN;
235 sigemptyset( &sv.sa_mask );
236 if (sigaction(SIGPIPE, &sv, NULL ) < 0 ) {
237 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
238 afp_exit(EXITERR_SYS);
241 sv.sa_handler = afp_goaway; /* handler for all sigs */
243 sigemptyset( &sv.sa_mask );
244 sigaddset(&sv.sa_mask, SIGALRM);
245 sigaddset(&sv.sa_mask, SIGHUP);
246 sigaddset(&sv.sa_mask, SIGTERM);
247 sigaddset(&sv.sa_mask, SIGUSR1);
248 sigaddset(&sv.sa_mask, SIGQUIT);
249 sv.sa_flags = SA_RESTART;
250 if ( sigaction( SIGCHLD, &sv, NULL ) < 0 ) {
251 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
252 afp_exit(EXITERR_SYS);
255 sigemptyset( &sv.sa_mask );
256 sigaddset(&sv.sa_mask, SIGALRM);
257 sigaddset(&sv.sa_mask, SIGTERM);
258 sigaddset(&sv.sa_mask, SIGHUP);
259 sigaddset(&sv.sa_mask, SIGCHLD);
260 sigaddset(&sv.sa_mask, SIGQUIT);
261 sv.sa_flags = SA_RESTART;
262 if ( sigaction( SIGUSR1, &sv, NULL ) < 0 ) {
263 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
264 afp_exit(EXITERR_SYS);
267 sigemptyset( &sv.sa_mask );
268 sigaddset(&sv.sa_mask, SIGALRM);
269 sigaddset(&sv.sa_mask, SIGTERM);
270 sigaddset(&sv.sa_mask, SIGUSR1);
271 sigaddset(&sv.sa_mask, SIGCHLD);
272 sigaddset(&sv.sa_mask, SIGQUIT);
273 sv.sa_flags = SA_RESTART;
274 if ( sigaction( SIGHUP, &sv, NULL ) < 0 ) {
275 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
276 afp_exit(EXITERR_SYS);
279 sigemptyset( &sv.sa_mask );
280 sigaddset(&sv.sa_mask, SIGALRM);
281 sigaddset(&sv.sa_mask, SIGHUP);
282 sigaddset(&sv.sa_mask, SIGUSR1);
283 sigaddset(&sv.sa_mask, SIGCHLD);
284 sigaddset(&sv.sa_mask, SIGQUIT);
285 sv.sa_flags = SA_RESTART;
286 if ( sigaction( SIGTERM, &sv, NULL ) < 0 ) {
287 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
288 afp_exit(EXITERR_SYS);
291 sigemptyset( &sv.sa_mask );
292 sigaddset(&sv.sa_mask, SIGALRM);
293 sigaddset(&sv.sa_mask, SIGHUP);
294 sigaddset(&sv.sa_mask, SIGUSR1);
295 sigaddset(&sv.sa_mask, SIGCHLD);
296 sigaddset(&sv.sa_mask, SIGTERM);
297 sv.sa_flags = SA_RESTART;
298 if (sigaction(SIGQUIT, &sv, NULL ) < 0 ) {
299 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
300 afp_exit(EXITERR_SYS);
303 /* afp.conf: not in config file: lockfile, configfile
304 * preference: command-line provides defaults.
305 * config file over-writes defaults.
307 * we also need to make sure that killing afpd during startup
308 * won't leave any lingering registered names around.
312 sigaddset(&sigs, SIGALRM);
313 sigaddset(&sigs, SIGHUP);
314 sigaddset(&sigs, SIGUSR1);
316 /* don't block SIGTERM */
317 sigaddset(&sigs, SIGTERM);
319 sigaddset(&sigs, SIGCHLD);
321 pthread_sigmask(SIG_BLOCK, &sigs, NULL);
322 #ifdef HAVE_DBUS_GLIB
323 /* Run dbus AFP statics thread */
324 if (obj.options.flags & OPTION_DBUS_AFPSTATS)
325 (void)afpstats_init(server_children);
327 if (configinit(&obj) != 0) {
328 LOG(log_error, logtype_afpd, "main: no servers configured");
329 afp_exit(EXITERR_CONF);
331 pthread_sigmask(SIG_UNBLOCK, &sigs, NULL);
336 /* watch atp, dsi sockets and ipc parent/child file descriptor. */
337 if (!(init_listening_sockets(&obj))) {
338 LOG(log_error, logtype_afpd, "main: couldn't initialize socket handler");
339 afp_exit(EXITERR_CONF);
348 /* wait for an appleshare connection. parent remains in the loop
349 * while the children get handled by afp_over_{asp,dsi}. this is
350 * currently vulnerable to a denial-of-service attack if a
351 * connection is made without an actual login attempt being made
352 * afterwards. establishing timeouts for logins is a possible
355 pthread_sigmask(SIG_UNBLOCK, &sigs, NULL);
356 ret = poll(asev->fdset, asev->used, -1);
357 pthread_sigmask(SIG_BLOCK, &sigs, NULL);
369 if (!(reset_listening_sockets(&obj))) {
370 LOG(log_error, logtype_afpd, "main: reset socket handlers");
371 afp_exit(EXITERR_CONF);
374 LOG(log_info, logtype_afpd, "re-reading configuration file");
376 configfree(&obj, NULL);
377 afp_config_free(&obj);
379 if (afp_config_parse(&obj, "afpd") != 0)
380 afp_exit(EXITERR_CONF);
382 if (configinit(&obj) != 0) {
383 LOG(log_error, logtype_afpd, "config re-read: no servers configured");
384 afp_exit(EXITERR_CONF);
387 if (!(init_listening_sockets(&obj))) {
388 LOG(log_error, logtype_afpd, "main: couldn't initialize socket handler");
389 afp_exit(EXITERR_CONF);
404 LOG(log_error, logtype_afpd, "main: can't wait for input: %s", strerror(errno));
408 for (int i = 0; i < asev->used; i++) {
409 if (asev->fdset[i].revents & (POLLIN | POLLERR | POLLHUP | POLLNVAL)) {
410 switch (asev->data[i].fdtype) {
413 if ((child = dsi_start(&obj, (DSI *)(asev->data[i].private), server_children))) {
414 if (!(asev_add_fd(asev, child->afpch_ipc_fd, IPC_FD, child))) {
415 LOG(log_error, logtype_afpd, "out of asev slots");
418 * Close IPC fd here and mark it as unused
420 close(child->afpch_ipc_fd);
421 child->afpch_ipc_fd = -1;
424 * Being unfriendly here, but we really
425 * want to get rid of it. The 'child'
426 * handle gets cleaned up in the SIGCLD
429 kill(child->afpch_pid, SIGKILL);
435 child = (afp_child_t *)(asev->data[i].private);
436 LOG(log_debug, logtype_afpd, "main: IPC request from child[%u]", child->afpch_pid);
438 if (ipc_server_read(server_children, child->afpch_ipc_fd) != 0) {
439 if (!(asev_del_fd(asev, child->afpch_ipc_fd))) {
440 LOG(log_error, logtype_afpd, "child[%u]: no IPC fd");
442 close(child->afpch_ipc_fd);
443 child->afpch_ipc_fd = -1;
448 LOG(log_debug, logtype_afpd, "main: IPC request for unknown type");
458 static afp_child_t *dsi_start(AFPObj *obj, DSI *dsi, server_child_t *server_children)
460 afp_child_t *child = NULL;
462 if (dsi_getsession(dsi, server_children, obj->options.tickleval, &child) != 0) {
463 LOG(log_error, logtype_afpd, "dsi_start: session error: %s", strerror(errno));
469 configfree(obj, dsi);
470 afp_over_dsi(obj); /* start a session */