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"
42 #define AFP_LISTENERS 32
43 #define FDSET_SAFETY 5
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;
52 /* Two pointers to dynamic allocated arrays which store pollfds and associated data */
53 static struct pollfd *fdset;
54 static struct polldata *polldata;
55 static int fdset_size; /* current allocated size */
56 static int fdset_used; /* number of used elements */
58 static afp_child_t *dsi_start(AFPObj *obj, DSI *dsi, server_child_t *server_children);
60 static void afp_exit(int ret)
67 initialize fd set we are waiting for.
69 static void fd_set_listening_sockets(const AFPObj *config)
73 for (dsi = config->dsi; dsi; dsi = dsi->next) {
74 fdset_add_fd(config->options.connections + AFP_LISTENERS + FDSET_SAFETY,
85 static void fd_reset_listening_sockets(const AFPObj *config)
89 for (dsi = config->dsi; dsi; dsi = dsi->next) {
90 fdset_del_fd(&fdset, &polldata, &fdset_used, &fdset_size, dsi->serversock);
94 /* ------------------ */
95 static void afp_goaway(int sig)
101 LOG(log_note, logtype_afpd, "AFP Server shutting down");
103 server_child_kill(server_children, SIGTERM);
110 LOG(log_info, logtype_afpd, "disallowing logins");
113 server_child_kill(server_children, sig);
117 /* w/ a configuration file, we can force a re-read if we want */
122 /* w/ a configuration file, we can force a re-read if we want */
127 LOG(log_error, logtype_afpd, "afp_goaway: bad signal" );
132 static void child_handler(void)
139 #define WAIT_ANY (-1)
140 #endif /* ! WAIT_ANY */
142 while ((pid = waitpid(WAIT_ANY, &status, WNOHANG)) > 0) {
143 if ((fd = server_child_remove(server_children, pid)) != -1) {
144 fdset_del_fd(&fdset, &polldata, &fdset_used, &fdset_size, fd);
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);
162 static int setlimits(void)
166 if (getrlimit(RLIMIT_NOFILE, &rlim) != 0) {
167 LOG(log_warning, logtype_afpd, "setlimits: reading current limits failed: %s", strerror(errno));
170 if (rlim.rlim_cur != RLIM_INFINITY && rlim.rlim_cur < 65535) {
171 rlim.rlim_cur = 65535;
172 if (rlim.rlim_max != RLIM_INFINITY && rlim.rlim_max < 65535)
173 rlim.rlim_max = 65535;
174 if (setrlimit(RLIMIT_NOFILE, &rlim) != 0) {
175 LOG(log_warning, logtype_afpd, "setlimits: increasing limits failed: %s", strerror(errno));
182 int main(int ac, char **av)
188 /* Parse argv args and initialize default options */
189 afp_options_parse_cmdline(&obj, ac, av);
191 if (!(obj.cmdlineflags & OPTION_DEBUG) && (daemonize(0, 0) != 0))
194 /* Log SIGBUS/SIGSEGV SBT */
197 if (afp_config_parse(&obj, "afpd") != 0)
198 afp_exit(EXITERR_CONF);
200 /* Save the user's current umask */
201 obj.options.save_mask = umask(obj.options.umask);
203 /* install child handler for asp and dsi. we do this before afp_goaway
204 * as afp_goaway references stuff from here.
205 * XXX: this should really be setup after the initial connections. */
206 if (!(server_children = server_child_alloc(obj.options.connections))) {
207 LOG(log_error, logtype_afpd, "main: server_child alloc: %s", strerror(errno) );
208 afp_exit(EXITERR_SYS);
212 pthread_sigmask(SIG_SETMASK, &sigs, NULL);
214 memset(&sv, 0, sizeof(sv));
215 /* linux at least up to 2.4.22 send a SIGXFZ for vfat fs,
216 even if the file is open with O_LARGEFILE ! */
218 sv.sa_handler = SIG_IGN;
219 sigemptyset( &sv.sa_mask );
220 if (sigaction(SIGXFSZ, &sv, NULL ) < 0 ) {
221 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
222 afp_exit(EXITERR_SYS);
226 sv.sa_handler = SIG_IGN;
227 sigemptyset( &sv.sa_mask );
228 if (sigaction(SIGPIPE, &sv, NULL ) < 0 ) {
229 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
230 afp_exit(EXITERR_SYS);
233 sv.sa_handler = afp_goaway; /* handler for all sigs */
235 sigemptyset( &sv.sa_mask );
236 sigaddset(&sv.sa_mask, SIGALRM);
237 sigaddset(&sv.sa_mask, SIGHUP);
238 sigaddset(&sv.sa_mask, SIGTERM);
239 sigaddset(&sv.sa_mask, SIGUSR1);
240 sigaddset(&sv.sa_mask, SIGQUIT);
241 sv.sa_flags = SA_RESTART;
242 if ( sigaction( SIGCHLD, &sv, NULL ) < 0 ) {
243 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
244 afp_exit(EXITERR_SYS);
247 sigemptyset( &sv.sa_mask );
248 sigaddset(&sv.sa_mask, SIGALRM);
249 sigaddset(&sv.sa_mask, SIGTERM);
250 sigaddset(&sv.sa_mask, SIGHUP);
251 sigaddset(&sv.sa_mask, SIGCHLD);
252 sigaddset(&sv.sa_mask, SIGQUIT);
253 sv.sa_flags = SA_RESTART;
254 if ( sigaction( SIGUSR1, &sv, NULL ) < 0 ) {
255 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
256 afp_exit(EXITERR_SYS);
259 sigemptyset( &sv.sa_mask );
260 sigaddset(&sv.sa_mask, SIGALRM);
261 sigaddset(&sv.sa_mask, SIGTERM);
262 sigaddset(&sv.sa_mask, SIGUSR1);
263 sigaddset(&sv.sa_mask, SIGCHLD);
264 sigaddset(&sv.sa_mask, SIGQUIT);
265 sv.sa_flags = SA_RESTART;
266 if ( sigaction( SIGHUP, &sv, NULL ) < 0 ) {
267 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
268 afp_exit(EXITERR_SYS);
271 sigemptyset( &sv.sa_mask );
272 sigaddset(&sv.sa_mask, SIGALRM);
273 sigaddset(&sv.sa_mask, SIGHUP);
274 sigaddset(&sv.sa_mask, SIGUSR1);
275 sigaddset(&sv.sa_mask, SIGCHLD);
276 sigaddset(&sv.sa_mask, SIGQUIT);
277 sv.sa_flags = SA_RESTART;
278 if ( sigaction( SIGTERM, &sv, NULL ) < 0 ) {
279 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
280 afp_exit(EXITERR_SYS);
283 sigemptyset( &sv.sa_mask );
284 sigaddset(&sv.sa_mask, SIGALRM);
285 sigaddset(&sv.sa_mask, SIGHUP);
286 sigaddset(&sv.sa_mask, SIGUSR1);
287 sigaddset(&sv.sa_mask, SIGCHLD);
288 sigaddset(&sv.sa_mask, SIGTERM);
289 sv.sa_flags = SA_RESTART;
290 if (sigaction(SIGQUIT, &sv, NULL ) < 0 ) {
291 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
292 afp_exit(EXITERR_SYS);
295 /* afp.conf: not in config file: lockfile, configfile
296 * preference: command-line provides defaults.
297 * config file over-writes defaults.
299 * we also need to make sure that killing afpd during startup
300 * won't leave any lingering registered names around.
304 sigaddset(&sigs, SIGALRM);
305 sigaddset(&sigs, SIGHUP);
306 sigaddset(&sigs, SIGUSR1);
308 /* don't block SIGTERM */
309 sigaddset(&sigs, SIGTERM);
311 sigaddset(&sigs, SIGCHLD);
313 pthread_sigmask(SIG_BLOCK, &sigs, NULL);
314 if (configinit(&obj) != 0) {
315 LOG(log_error, logtype_afpd, "main: no servers configured");
316 afp_exit(EXITERR_CONF);
318 pthread_sigmask(SIG_UNBLOCK, &sigs, NULL);
323 /* watch atp, dsi sockets and ipc parent/child file descriptor. */
324 fd_set_listening_sockets(&obj);
334 /* wait for an appleshare connection. parent remains in the loop
335 * while the children get handled by afp_over_{asp,dsi}. this is
336 * currently vulnerable to a denial-of-service attack if a
337 * connection is made without an actual login attempt being made
338 * afterwards. establishing timeouts for logins is a possible
341 LOG(log_maxdebug, logtype_afpd, "main: polling %i fds", fdset_used);
342 pthread_sigmask(SIG_UNBLOCK, &sigs, NULL);
343 ret = poll(fdset, fdset_used, -1);
344 pthread_sigmask(SIG_BLOCK, &sigs, NULL);
356 fd_reset_listening_sockets(&obj);
358 LOG(log_info, logtype_afpd, "re-reading configuration file");
360 configfree(&obj, NULL);
361 afp_config_free(&obj);
363 if (afp_config_parse(&obj, "afpd") != 0)
364 afp_exit(EXITERR_CONF);
366 if (configinit(&obj) != 0) {
367 LOG(log_error, logtype_afpd, "config re-read: no servers configured");
368 afp_exit(EXITERR_CONF);
371 fd_set_listening_sockets(&obj);
385 LOG(log_error, logtype_afpd, "main: can't wait for input: %s", strerror(errno));
389 for (int i = 0; i < fdset_used; i++) {
390 if (fdset[i].revents & (POLLIN | POLLERR | POLLHUP | POLLNVAL)) {
391 switch (polldata[i].fdtype) {
394 if ((child = dsi_start(&obj, (DSI *)polldata[i].data, server_children))) {
395 /* Add IPC fd to select fd set */
396 fdset_add_fd(obj.options.connections + AFP_LISTENERS + FDSET_SAFETY,
408 child = (afp_child_t *)polldata[i].data;
409 LOG(log_debug, logtype_afpd, "main: IPC request from child[%u]", child->afpch_pid);
411 if (ipc_server_read(server_children, child->afpch_ipc_fd) != 0) {
412 fdset_del_fd(&fdset, &polldata, &fdset_used, &fdset_size, child->afpch_ipc_fd);
413 close(child->afpch_ipc_fd);
414 child->afpch_ipc_fd = -1;
419 LOG(log_debug, logtype_afpd, "main: IPC request for unknown type");
429 static afp_child_t *dsi_start(AFPObj *obj, DSI *dsi, server_child_t *server_children)
431 afp_child_t *child = NULL;
433 if (dsi_getsession(dsi, server_children, obj->options.tickleval, &child) != 0) {
434 LOG(log_error, logtype_afpd, "dsi_start: session error: %s", strerror(errno));
440 configfree(obj, dsi);
441 afp_over_dsi(obj); /* start a session */