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"
43 #include <sys/security.h>
48 static char **argv = NULL;
51 unsigned char nologin = 0;
53 struct afp_options default_options;
54 static AFPConfig *configs;
55 static server_child *server_children;
56 static fd_set save_rfds;
57 static int Ipc_fd = -1;
60 void afp_get_cmdline( int *ac, char ***av)
67 static void afp_exit(const int i)
69 server_unlock(default_options.pidfile);
74 initialize fd set we are waiting for.
76 static void set_fd(int ipc_fd)
81 for (config = configs; config; config = config->next) {
82 if (config->fd < 0) /* for proxies */
84 FD_SET(config->fd, &save_rfds);
87 FD_SET(ipc_fd, &save_rfds);
91 /* ------------------ */
92 static void afp_goaway(int sig)
102 LOG(log_note, logtype_afpd, "AFP Server shutting down on SIGTERM");
106 /* w/ a configuration file, we can force a re-read if we want */
109 if (sig == SIGHUP || ((nologin + 1) & 1)) {
112 LOG(log_info, logtype_afpd, "re-reading configuration file");
113 for (config = configs; config; config = config->next)
114 if (config->server_cleanup)
115 config->server_cleanup(config);
117 /* configfree close atp socket used for DDP tickle, there's an issue
120 configfree(configs, NULL);
121 if (!(configs = configinit(&default_options))) {
122 LOG(log_error, logtype_afpd, "config re-read: no servers configured");
123 afp_exit(EXITERR_CONF);
127 LOG(log_info, logtype_afpd, "disallowing logins");
134 LOG(log_error, logtype_afpd, "afp_goaway: bad signal" );
136 if ( sig == SIGTERM ) {
139 for (config = configs; config; config = config->next)
140 if (config->server_cleanup)
141 config->server_cleanup(config);
148 static void child_handler(int sig _U_)
150 server_child_handler(server_children);
153 int main(int ac, char **av)
165 set_auth_parameters( ac, av );
171 afp_options_init(&default_options);
172 if (!afp_options_parse(ac, av, &default_options))
175 /* Save the user's current umask for use with CNID (and maybe some
176 * other things, too). */
177 default_options.save_mask = umask( default_options.umask );
179 switch(server_lock("afpd", default_options.pidfile,
180 default_options.flags & OPTION_DEBUG)) {
185 default: /* server */
189 /* install child handler for asp and dsi. we do this before afp_goaway
190 * as afp_goaway references stuff from here.
191 * XXX: this should really be setup after the initial connections. */
192 if (!(server_children = server_child_alloc(default_options.connections,
194 LOG(log_error, logtype_afpd, "main: server_child alloc: %s", strerror(errno) );
195 afp_exit(EXITERR_SYS);
198 memset(&sv, 0, sizeof(sv));
200 /* linux at least up to 2.4.22 send a SIGXFZ for vfat fs,
201 even if the file is open with O_LARGEFILE ! */
203 sv.sa_handler = SIG_IGN;
204 sigemptyset( &sv.sa_mask );
205 if (sigaction(SIGXFSZ, &sv, NULL ) < 0 ) {
206 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
207 afp_exit(EXITERR_SYS);
212 sv.sa_handler = child_handler;
213 sigemptyset( &sv.sa_mask );
214 sigaddset(&sv.sa_mask, SIGALRM);
215 sigaddset(&sv.sa_mask, SIGHUP);
216 sigaddset(&sv.sa_mask, SIGTERM);
217 sigaddset(&sv.sa_mask, SIGUSR1);
219 sv.sa_flags = SA_RESTART;
220 if ( sigaction( SIGCHLD, &sv, NULL ) < 0 ) {
221 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
222 afp_exit(EXITERR_SYS);
225 sv.sa_handler = afp_goaway;
226 sigemptyset( &sv.sa_mask );
227 sigaddset(&sv.sa_mask, SIGALRM);
228 sigaddset(&sv.sa_mask, SIGTERM);
229 sigaddset(&sv.sa_mask, SIGHUP);
230 sigaddset(&sv.sa_mask, SIGCHLD);
231 sv.sa_flags = SA_RESTART;
232 if ( sigaction( SIGUSR1, &sv, NULL ) < 0 ) {
233 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
234 afp_exit(EXITERR_SYS);
237 sigemptyset( &sv.sa_mask );
238 sigaddset(&sv.sa_mask, SIGALRM);
239 sigaddset(&sv.sa_mask, SIGTERM);
240 sigaddset(&sv.sa_mask, SIGUSR1);
241 sigaddset(&sv.sa_mask, SIGCHLD);
242 sv.sa_flags = SA_RESTART;
243 if ( sigaction( SIGHUP, &sv, NULL ) < 0 ) {
244 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
245 afp_exit(EXITERR_SYS);
249 sigemptyset( &sv.sa_mask );
250 sigaddset(&sv.sa_mask, SIGALRM);
251 sigaddset(&sv.sa_mask, SIGHUP);
252 sigaddset(&sv.sa_mask, SIGUSR1);
253 sigaddset(&sv.sa_mask, SIGCHLD);
254 sv.sa_flags = SA_RESTART;
255 if ( sigaction( SIGTERM, &sv, NULL ) < 0 ) {
256 LOG(log_error, logtype_afpd, "main: sigaction: %s", strerror(errno) );
257 afp_exit(EXITERR_SYS);
260 /* afpd.conf: not in config file: lockfile, connections, configfile
261 * preference: command-line provides defaults.
262 * config file over-writes defaults.
264 * we also need to make sure that killing afpd during startup
265 * won't leave any lingering registered names around.
269 sigaddset(&sigs, SIGALRM);
270 sigaddset(&sigs, SIGHUP);
271 sigaddset(&sigs, SIGUSR1);
273 /* don't block SIGTERM */
274 sigaddset(&sigs, SIGTERM);
276 sigaddset(&sigs, SIGCHLD);
278 sigprocmask(SIG_BLOCK, &sigs, NULL);
279 if (!(configs = configinit(&default_options))) {
280 LOG(log_error, logtype_afpd, "main: no servers configured");
281 afp_exit(EXITERR_CONF);
283 sigprocmask(SIG_UNBLOCK, &sigs, NULL);
288 /* watch atp, dsi sockets and ipc parent/child file descriptor. */
289 if ((ipc = server_ipc_create())) {
290 Ipc_fd = server_ipc_parent(ipc);
294 /* wait for an appleshare connection. parent remains in the loop
295 * while the children get handled by afp_over_{asp,dsi}. this is
296 * currently vulnerable to a denial-of-service attack if a
297 * connection is made without an actual login attempt being made
298 * afterwards. establishing timeouts for logins is a possible
302 sigprocmask(SIG_UNBLOCK, &sigs, NULL);
303 ret = select(FD_SETSIZE, &rfds, NULL, NULL, NULL);
304 sigprocmask(SIG_BLOCK, &sigs, NULL);
308 LOG(log_error, logtype_afpd, "main: can't wait for input: %s", strerror(errno));
311 if (Ipc_fd >=0 && FD_ISSET(Ipc_fd, &rfds)) {
312 server_ipc_read(server_children);
314 for (config = configs; config; config = config->next) {
317 if (FD_ISSET(config->fd, &rfds)) {
318 config->server_start(config, configs, server_children);