2 * $Id: afp_dsi.c,v 1.53 2010/03/30 12:55:26 franklahm Exp $
4 * Copyright (c) 1999 Adrian Sun (asun@zoology.washington.edu)
5 * Copyright (c) 1990,1993 Regents of The University of Michigan.
6 * All Rights Reserved. See COPYRIGHT.
8 * modified from main.c. this handles afp over tcp.
13 #endif /* HAVE_CONFIG_H */
22 #endif /* HAVE_UNISTD_H */
23 #include <sys/socket.h>
25 #ifdef HAVE_SYS_STAT_H
27 #endif /* HAVE_SYS_STAT_H */
28 #include <netinet/in.h>
29 #include <arpa/inet.h>
30 #include <atalk/logger.h>
32 #include <atalk/dsi.h>
33 #include <atalk/compat.h>
34 #include <atalk/util.h>
45 #endif /* FORCE_UIDGID */
47 #define CHILD_DIE (1 << 0)
48 #define CHILD_RUNNING (1 << 1)
49 #define CHILD_SLEEPING (1 << 2)
50 #define CHILD_DATA (1 << 3)
53 * We generally pass this from afp_over_dsi to all afp_* funcs, so it should already be
54 * available everywhere. Unfortunately some funcs (eg acltoownermode) need acces to it
55 * but are deeply nested in the function chain with the caller already without acces to it.
56 * Changing this would require adding a reference to the caller which itself might be
57 * called in many places (eg acltoownermode is called from accessmode).
58 * The only sane way out is providing a copy of it here:
60 AFPObj *AFPobj = NULL;
69 static void afp_dsi_close(AFPObj *obj)
71 DSI *dsi = obj->handle;
73 /* we may have been called from a signal handler caught when afpd was running
74 * as uid 0, that's the wrong user for volume's prexec_close scripts if any,
75 * restore our login user
77 if (seteuid( obj->uid ) < 0) {
78 LOG(log_error, logtype_afpd, "can't seteuid back %s", strerror(errno));
85 LOG(log_info, logtype_afpd, "%.2fKB read, %.2fKB written",
86 dsi->read_count/1024.0, dsi->write_count/1024.0);
91 /* -------------------------------
93 * a little bit of code duplication.
95 static void afp_dsi_die(int sig)
97 static volatile int in_handler;
102 /* it's not atomic but we don't care because it's an exit function
103 * ie if a signal is received here, between the test and the affectation,
104 * it will not return.
108 dsi_attention(child.obj->handle, AFPATTN_SHUTDOWN);
109 afp_dsi_close(child.obj);
110 if (sig) /* if no signal, assume dieing because logins are disabled &
111 don't log it (maintenance mode)*/
112 LOG(log_info, logtype_afpd, "Connection terminated");
113 if (sig == SIGTERM || sig == SIGALRM) {
122 static void afp_dsi_sleep(void)
124 child.flags |= CHILD_SLEEPING;
125 dsi_sleep(child.obj->handle, 1);
128 /* ------------------- */
129 static void afp_dsi_timedown(int sig _U_)
134 child.flags |= CHILD_DIE;
135 /* shutdown and don't reconnect. server going down in 5 minutes. */
136 setmessage("The server is going down for maintenance.");
137 if (dsi_attention(child.obj->handle, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
138 AFPATTN_MESG | AFPATTN_TIME(5)) < 0) {
139 DSI *dsi = (DSI *) child.obj->handle;
140 dsi->down_request = 1;
143 it.it_interval.tv_sec = 0;
144 it.it_interval.tv_usec = 0;
145 it.it_value.tv_sec = 300;
146 it.it_value.tv_usec = 0;
148 if ( setitimer( ITIMER_REAL, &it, NULL ) < 0 ) {
149 LOG(log_error, logtype_afpd, "afp_timedown: setitimer: %s", strerror(errno) );
150 afp_dsi_die(EXITERR_SYS);
152 memset(&sv, 0, sizeof(sv));
153 sv.sa_handler = afp_dsi_die;
154 sigemptyset( &sv.sa_mask );
155 sigaddset(&sv.sa_mask, SIGHUP);
156 sigaddset(&sv.sa_mask, SIGTERM);
157 sv.sa_flags = SA_RESTART;
158 if ( sigaction( SIGALRM, &sv, NULL ) < 0 ) {
159 LOG(log_error, logtype_afpd, "afp_timedown: sigaction: %s", strerror(errno) );
160 afp_dsi_die(EXITERR_SYS);
164 sv.sa_handler = SIG_IGN;
165 sigemptyset( &sv.sa_mask );
166 sv.sa_flags = SA_RESTART;
167 if ( sigaction( SIGUSR1, &sv, NULL ) < 0 ) {
168 LOG(log_error, logtype_afpd, "afp_timedown: sigaction SIGHUP: %s", strerror(errno) );
169 afp_dsi_die(EXITERR_SYS);
173 /* ---------------------------------
174 * SIGHUP reload configuration file
175 * FIXME here or we wait ?
177 volatile int reload_request = 0;
179 static void afp_dsi_reload(int sig _U_)
184 /* ---------------------------------
185 * SIGINT: enable max_debug LOGging
187 static volatile sig_atomic_t debug_request = 0;
189 static void afp_dsi_debug(int sig _U_)
194 /* ---------------------- */
196 static void afp_dsi_getmesg (int sig _U_)
198 DSI *dsi = (DSI *) child.obj->handle;
200 dsi->msg_request = 1;
201 if (dsi_attention(child.obj->handle, AFPATTN_MESG | AFPATTN_TIME(5)) < 0)
202 dsi->msg_request = 2;
204 #endif /* SERVERTEXT */
206 static void alarm_handler(int sig _U_)
209 DSI *dsi = (DSI *) child.obj->handle;
211 /* we have to restart the timer because some libraries
213 setitimer(ITIMER_REAL, &dsi->timer, NULL);
215 /* we got some traffic from the client since the previous timer
217 if ((child.flags & CHILD_DATA)) {
218 child.flags &= ~CHILD_DATA;
222 /* if we're in the midst of processing something,
224 if ((child.flags & CHILD_SLEEPING) && child.tickle++ < child.obj->options.sleep) {
228 if ((child.flags & CHILD_RUNNING) || (child.tickle++ < child.obj->options.timeout)) {
229 if (!(err = pollvoltime(child.obj)))
230 err = dsi_tickle(child.obj->handle);
232 afp_dsi_die(EXITERR_CLNT);
234 } else { /* didn't receive a tickle. close connection */
235 LOG(log_error, logtype_afpd, "afp_alarm: child timed out");
236 afp_dsi_die(EXITERR_CLNT);
241 if dsi->in_write is set attention, tickle (and close?) msg
242 aren't sent. We don't care about tickle
244 static void pending_request(DSI *dsi)
246 /* send pending attention */
248 /* read msg if any, it could be done in afp_getsrvrmesg */
249 if (dsi->msg_request) {
250 if (dsi->msg_request == 2) {
251 /* didn't send it in signal handler */
252 dsi_attention(child.obj->handle, AFPATTN_MESG | AFPATTN_TIME(5));
254 dsi->msg_request = 0;
255 readmessage(child.obj);
257 if (dsi->down_request) {
258 dsi->down_request = 0;
259 dsi_attention(child.obj->handle, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
260 AFPATTN_MESG | AFPATTN_TIME(5));
264 /* -------------------------------------------
265 afp over dsi. this never returns.
267 void afp_over_dsi(AFPObj *obj)
269 DSI *dsi = (DSI *) obj->handle;
272 struct sigaction action;
275 obj->exit = afp_dsi_die;
276 obj->reply = (int (*)()) dsi_cmdreply;
277 obj->attention = (int (*)(void *, AFPUserBytes)) dsi_attention;
279 obj->sleep = afp_dsi_sleep;
281 child.tickle = child.flags = 0;
283 memset(&action, 0, sizeof(action));
286 action.sa_handler = afp_dsi_reload;
287 sigemptyset( &action.sa_mask );
288 sigaddset(&action.sa_mask, SIGALRM);
289 sigaddset(&action.sa_mask, SIGTERM);
290 sigaddset(&action.sa_mask, SIGUSR1);
291 sigaddset(&action.sa_mask, SIGINT);
293 sigaddset(&action.sa_mask, SIGUSR2);
295 action.sa_flags = SA_RESTART;
296 if ( sigaction( SIGHUP, &action, NULL ) < 0 ) {
297 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
298 afp_dsi_die(EXITERR_SYS);
301 /* install SIGTERM */
302 action.sa_handler = afp_dsi_die;
303 sigemptyset( &action.sa_mask );
304 sigaddset(&action.sa_mask, SIGALRM);
305 sigaddset(&action.sa_mask, SIGHUP);
306 sigaddset(&action.sa_mask, SIGUSR1);
307 sigaddset(&action.sa_mask, SIGINT);
309 sigaddset(&action.sa_mask, SIGUSR2);
311 action.sa_flags = SA_RESTART;
312 if ( sigaction( SIGTERM, &action, NULL ) < 0 ) {
313 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
314 afp_dsi_die(EXITERR_SYS);
318 /* Added for server message support */
319 action.sa_handler = afp_dsi_getmesg;
320 sigemptyset( &action.sa_mask );
321 sigaddset(&action.sa_mask, SIGALRM);
322 sigaddset(&action.sa_mask, SIGTERM);
323 sigaddset(&action.sa_mask, SIGUSR1);
324 sigaddset(&action.sa_mask, SIGHUP);
325 sigaddset(&action.sa_mask, SIGINT);
326 action.sa_flags = SA_RESTART;
327 if ( sigaction( SIGUSR2, &action, NULL) < 0 ) {
328 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
329 afp_dsi_die(EXITERR_SYS);
331 #endif /* SERVERTEXT */
333 /* SIGUSR1 - set down in 5 minutes */
334 action.sa_handler = afp_dsi_timedown;
335 sigemptyset( &action.sa_mask );
336 sigaddset(&action.sa_mask, SIGALRM);
337 sigaddset(&action.sa_mask, SIGHUP);
338 sigaddset(&action.sa_mask, SIGTERM);
339 sigaddset(&action.sa_mask, SIGINT);
341 sigaddset(&action.sa_mask, SIGUSR2);
343 action.sa_flags = SA_RESTART;
344 if ( sigaction( SIGUSR1, &action, NULL) < 0 ) {
345 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
346 afp_dsi_die(EXITERR_SYS);
349 /* SIGINT - enable max_debug LOGging to /tmp/afpd.PID.XXXXXX */
350 action.sa_handler = afp_dsi_debug;
351 sigfillset( &action.sa_mask );
352 action.sa_flags = SA_RESTART;
353 if ( sigaction( SIGINT, &action, NULL) < 0 ) {
354 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
355 afp_dsi_die(EXITERR_SYS);
360 action.sa_handler = alarm_handler;
361 sigemptyset(&action.sa_mask);
362 sigaddset(&action.sa_mask, SIGHUP);
363 sigaddset(&action.sa_mask, SIGTERM);
364 sigaddset(&action.sa_mask, SIGUSR1);
365 sigaddset(&action.sa_mask, SIGINT);
367 sigaddset(&action.sa_mask, SIGUSR2);
369 action.sa_flags = SA_RESTART;
370 if ((sigaction(SIGALRM, &action, NULL) < 0) ||
371 (setitimer(ITIMER_REAL, &dsi->timer, NULL) < 0)) {
372 afp_dsi_die(EXITERR_SYS);
374 #endif /* DEBUGGING */
376 if (dircache_init(obj->options.dircachesize) != 0)
377 afp_dsi_die(EXITERR_SYS);
379 /* get stuck here until the end */
380 while ((cmd = dsi_receive(dsi))) {
382 child.flags &= ~CHILD_SLEEPING;
383 dsi_sleep(dsi, 0); /* wake up */
385 if (reload_request) {
387 load_volumes(child.obj);
395 /* The first SIGINT enables debugging, the second one kills us */
396 action.sa_handler = afp_dsi_die;
397 sigfillset( &action.sa_mask );
398 action.sa_flags = SA_RESTART;
399 if ( sigaction( SIGINT, &action, NULL ) < 0 ) {
400 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
401 afp_dsi_die(EXITERR_SYS);
404 sprintf(logstr, "default log_maxdebug /tmp/afpd.%u.XXXXXX", getpid());
408 if (cmd == DSIFUNC_TICKLE) {
409 /* timer is not every 30 seconds anymore, so we don't get killed on the client side. */
410 if ((child.flags & CHILD_DIE))
412 pending_request(dsi);
416 child.flags |= CHILD_DATA;
420 LOG(log_info, logtype_afpd, "done");
427 if ( flushfork( writtenfork ) < 0 ) {
428 LOG(log_error, logtype_afpd, "main flushfork: %s", strerror(errno) );
434 function = (u_char) dsi->commands[0];
436 /* send off an afp command. in a couple cases, we take advantage
437 * of the fact that we're a stream-based protocol. */
438 if (afp_switch[function]) {
439 dsi->datalen = DSI_DATASIZ;
440 child.flags |= CHILD_RUNNING;
442 LOG(log_debug, logtype_afpd, "<== Start AFP command: %s", AfpNum2name(function));
444 err = (*afp_switch[function])(obj,
445 (char *)&dsi->commands, dsi->cmdlen,
446 (char *)&dsi->data, &dsi->datalen);
448 LOG(log_debug, logtype_afpd, "==> Finished AFP command: %s -> %s",
449 AfpNum2name(function), AfpErr2name(err));
451 /* bring everything back to old euid, egid */
453 restore_uidgid ( &obj->uidgid );
454 #endif /* FORCE_UIDGID */
455 child.flags &= ~CHILD_RUNNING;
457 LOG(log_error, logtype_afpd, "bad function %X", function);
462 /* single shot toggle that gets set by dsi_readinit. */
468 if (!dsi_cmdreply(dsi, err)) {
469 LOG(log_error, logtype_afpd, "dsi_cmdreply(%d): %s", dsi->socket, strerror(errno) );
470 afp_dsi_die(EXITERR_CLNT);
474 case DSIFUNC_WRITE: /* FPWrite and FPAddIcon */
475 function = (u_char) dsi->commands[0];
476 if ( afp_switch[ function ] != NULL ) {
477 dsi->datalen = DSI_DATASIZ;
478 child.flags |= CHILD_RUNNING;
480 LOG(log_debug, logtype_afpd, "<== Start AFP command: %s", AfpNum2name(function));
482 err = (*afp_switch[function])(obj,
483 (char *)&dsi->commands, dsi->cmdlen,
484 (char *)&dsi->data, &dsi->datalen);
486 LOG(log_debug, logtype_afpd, "==> Finished AFP command: %s -> %s",
487 AfpNum2name(function), AfpErr2name(err));
489 child.flags &= ~CHILD_RUNNING;
491 /* bring everything back to old euid, egid */
493 restore_uidgid ( &obj->uidgid );
494 #endif /* FORCE_UIDGID */
496 LOG(log_error, logtype_afpd, "(write) bad function %x", function);
501 if (!dsi_wrtreply(dsi, err)) {
502 LOG(log_error, logtype_afpd, "dsi_wrtreply: %s", strerror(errno) );
503 afp_dsi_die(EXITERR_CLNT);
507 case DSIFUNC_ATTN: /* attention replies */
510 /* error. this usually implies a mismatch of some kind
511 * between server and client. if things are correct,
512 * we need to flush the rest of the packet if necessary. */
514 LOG(log_info, logtype_afpd,"afp_dsi: spurious command %d", cmd);
515 dsi_writeinit(dsi, dsi->data, DSI_DATASIZ);
519 pending_request(dsi);
523 afp_dsi_die(EXITERR_CLNT);