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>
44 #endif /* FORCE_UIDGID */
46 #define CHILD_DIE (1 << 0)
47 #define CHILD_RUNNING (1 << 1)
48 #define CHILD_SLEEPING (1 << 2)
49 #define CHILD_DATA (1 << 3)
52 * We generally pass this from afp_over_dsi to all afp_* funcs, so it should already be
53 * available everywhere. Unfortunately some funcs (eg acltoownermode) need acces to it
54 * but are deeply nested in the function chain with the caller already without acces to it.
55 * Changing this would require adding a reference to the caller which itself might be
56 * called in many places (eg acltoownermode is called from accessmode).
57 * The only sane way out is providing a copy of it here:
59 AFPObj *AFPobj = NULL;
68 static void afp_dsi_close(AFPObj *obj)
70 DSI *dsi = obj->handle;
72 /* we may have been called from a signal handler caught when afpd was running
73 * as uid 0, that's the wrong user for volume's prexec_close scripts if any,
74 * restore our login user
76 if (seteuid( obj->uid ) < 0) {
77 LOG(log_error, logtype_afpd, "can't seteuid back %s", strerror(errno));
84 LOG(log_info, logtype_afpd, "%.2fKB read, %.2fKB written",
85 dsi->read_count/1024.0, dsi->write_count/1024.0);
90 /* -------------------------------
92 * a little bit of code duplication.
94 static void afp_dsi_die(int sig)
96 static volatile int in_handler;
101 /* it's not atomic but we don't care because it's an exit function
102 * ie if a signal is received here, between the test and the affectation,
103 * it will not return.
107 dsi_attention(child.obj->handle, AFPATTN_SHUTDOWN);
108 afp_dsi_close(child.obj);
109 if (sig) /* if no signal, assume dieing because logins are disabled &
110 don't log it (maintenance mode)*/
111 LOG(log_info, logtype_afpd, "Connection terminated");
112 if (sig == SIGTERM || sig == SIGALRM) {
121 static void afp_dsi_sleep(void)
123 child.flags |= CHILD_SLEEPING;
124 dsi_sleep(child.obj->handle, 1);
127 /* ------------------- */
128 static void afp_dsi_timedown(int sig _U_)
133 child.flags |= CHILD_DIE;
134 /* shutdown and don't reconnect. server going down in 5 minutes. */
135 setmessage("The server is going down for maintenance.");
136 if (dsi_attention(child.obj->handle, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
137 AFPATTN_MESG | AFPATTN_TIME(5)) < 0) {
138 DSI *dsi = (DSI *) child.obj->handle;
139 dsi->down_request = 1;
142 it.it_interval.tv_sec = 0;
143 it.it_interval.tv_usec = 0;
144 it.it_value.tv_sec = 300;
145 it.it_value.tv_usec = 0;
147 if ( setitimer( ITIMER_REAL, &it, NULL ) < 0 ) {
148 LOG(log_error, logtype_afpd, "afp_timedown: setitimer: %s", strerror(errno) );
149 afp_dsi_die(EXITERR_SYS);
151 memset(&sv, 0, sizeof(sv));
152 sv.sa_handler = afp_dsi_die;
153 sigemptyset( &sv.sa_mask );
154 sigaddset(&sv.sa_mask, SIGHUP);
155 sigaddset(&sv.sa_mask, SIGTERM);
156 sv.sa_flags = SA_RESTART;
157 if ( sigaction( SIGALRM, &sv, NULL ) < 0 ) {
158 LOG(log_error, logtype_afpd, "afp_timedown: sigaction: %s", strerror(errno) );
159 afp_dsi_die(EXITERR_SYS);
163 sv.sa_handler = SIG_IGN;
164 sigemptyset( &sv.sa_mask );
165 sv.sa_flags = SA_RESTART;
166 if ( sigaction( SIGUSR1, &sv, NULL ) < 0 ) {
167 LOG(log_error, logtype_afpd, "afp_timedown: sigaction SIGHUP: %s", strerror(errno) );
168 afp_dsi_die(EXITERR_SYS);
172 /* ---------------------------------
173 * SIGHUP reload configuration file
174 * FIXME here or we wait ?
176 volatile int reload_request = 0;
178 static void afp_dsi_reload(int sig _U_)
183 /* ---------------------------------
184 * SIGINT: enable max_debug LOGging
186 static volatile sig_atomic_t debug_request = 0;
188 static void afp_dsi_debug(int sig _U_)
193 /* ---------------------- */
195 static void afp_dsi_getmesg (int sig _U_)
197 DSI *dsi = (DSI *) child.obj->handle;
199 dsi->msg_request = 1;
200 if (dsi_attention(child.obj->handle, AFPATTN_MESG | AFPATTN_TIME(5)) < 0)
201 dsi->msg_request = 2;
203 #endif /* SERVERTEXT */
205 static void alarm_handler(int sig _U_)
208 DSI *dsi = (DSI *) child.obj->handle;
210 /* we have to restart the timer because some libraries
212 setitimer(ITIMER_REAL, &dsi->timer, NULL);
214 /* we got some traffic from the client since the previous timer
216 if ((child.flags & CHILD_DATA)) {
217 child.flags &= ~CHILD_DATA;
221 /* if we're in the midst of processing something,
223 if ((child.flags & CHILD_SLEEPING) && child.tickle++ < child.obj->options.sleep) {
227 if ((child.flags & CHILD_RUNNING) || (child.tickle++ < child.obj->options.timeout)) {
228 if (!(err = pollvoltime(child.obj)))
229 err = dsi_tickle(child.obj->handle);
231 afp_dsi_die(EXITERR_CLNT);
233 } else { /* didn't receive a tickle. close connection */
234 LOG(log_error, logtype_afpd, "afp_alarm: child timed out");
235 afp_dsi_die(EXITERR_CLNT);
240 if dsi->in_write is set attention, tickle (and close?) msg
241 aren't sent. We don't care about tickle
243 static void pending_request(DSI *dsi)
245 /* send pending attention */
247 /* read msg if any, it could be done in afp_getsrvrmesg */
248 if (dsi->msg_request) {
249 if (dsi->msg_request == 2) {
250 /* didn't send it in signal handler */
251 dsi_attention(child.obj->handle, AFPATTN_MESG | AFPATTN_TIME(5));
253 dsi->msg_request = 0;
254 readmessage(child.obj);
256 if (dsi->down_request) {
257 dsi->down_request = 0;
258 dsi_attention(child.obj->handle, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
259 AFPATTN_MESG | AFPATTN_TIME(5));
263 /* -------------------------------------------
264 afp over dsi. this never returns.
266 void afp_over_dsi(AFPObj *obj)
268 DSI *dsi = (DSI *) obj->handle;
271 struct sigaction action;
274 obj->exit = afp_dsi_die;
275 obj->reply = (int (*)()) dsi_cmdreply;
276 obj->attention = (int (*)(void *, AFPUserBytes)) dsi_attention;
278 obj->sleep = afp_dsi_sleep;
280 child.tickle = child.flags = 0;
282 memset(&action, 0, sizeof(action));
285 action.sa_handler = afp_dsi_reload;
286 sigemptyset( &action.sa_mask );
287 sigaddset(&action.sa_mask, SIGALRM);
288 sigaddset(&action.sa_mask, SIGTERM);
289 sigaddset(&action.sa_mask, SIGUSR1);
290 sigaddset(&action.sa_mask, SIGINT);
292 sigaddset(&action.sa_mask, SIGUSR2);
294 action.sa_flags = SA_RESTART;
295 if ( sigaction( SIGHUP, &action, NULL ) < 0 ) {
296 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
297 afp_dsi_die(EXITERR_SYS);
300 /* install SIGTERM */
301 action.sa_handler = afp_dsi_die;
302 sigemptyset( &action.sa_mask );
303 sigaddset(&action.sa_mask, SIGALRM);
304 sigaddset(&action.sa_mask, SIGHUP);
305 sigaddset(&action.sa_mask, SIGUSR1);
306 sigaddset(&action.sa_mask, SIGINT);
308 sigaddset(&action.sa_mask, SIGUSR2);
310 action.sa_flags = SA_RESTART;
311 if ( sigaction( SIGTERM, &action, NULL ) < 0 ) {
312 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
313 afp_dsi_die(EXITERR_SYS);
317 /* Added for server message support */
318 action.sa_handler = afp_dsi_getmesg;
319 sigemptyset( &action.sa_mask );
320 sigaddset(&action.sa_mask, SIGALRM);
321 sigaddset(&action.sa_mask, SIGTERM);
322 sigaddset(&action.sa_mask, SIGUSR1);
323 sigaddset(&action.sa_mask, SIGHUP);
324 sigaddset(&action.sa_mask, SIGINT);
325 action.sa_flags = SA_RESTART;
326 if ( sigaction( SIGUSR2, &action, NULL) < 0 ) {
327 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
328 afp_dsi_die(EXITERR_SYS);
330 #endif /* SERVERTEXT */
332 /* SIGUSR1 - set down in 5 minutes */
333 action.sa_handler = afp_dsi_timedown;
334 sigemptyset( &action.sa_mask );
335 sigaddset(&action.sa_mask, SIGALRM);
336 sigaddset(&action.sa_mask, SIGHUP);
337 sigaddset(&action.sa_mask, SIGTERM);
338 sigaddset(&action.sa_mask, SIGINT);
340 sigaddset(&action.sa_mask, SIGUSR2);
342 action.sa_flags = SA_RESTART;
343 if ( sigaction( SIGUSR1, &action, NULL) < 0 ) {
344 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
345 afp_dsi_die(EXITERR_SYS);
348 /* SIGINT - enable max_debug LOGging to /tmp/afpd.PID.XXXXXX */
349 action.sa_handler = afp_dsi_debug;
350 sigfillset( &action.sa_mask );
351 action.sa_flags = SA_RESTART;
352 if ( sigaction( SIGINT, &action, NULL) < 0 ) {
353 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
354 afp_dsi_die(EXITERR_SYS);
359 action.sa_handler = alarm_handler;
360 sigemptyset(&action.sa_mask);
361 sigaddset(&action.sa_mask, SIGHUP);
362 sigaddset(&action.sa_mask, SIGTERM);
363 sigaddset(&action.sa_mask, SIGUSR1);
364 sigaddset(&action.sa_mask, SIGINT);
366 sigaddset(&action.sa_mask, SIGUSR2);
368 action.sa_flags = SA_RESTART;
369 if ((sigaction(SIGALRM, &action, NULL) < 0) ||
370 (setitimer(ITIMER_REAL, &dsi->timer, NULL) < 0)) {
371 afp_dsi_die(EXITERR_SYS);
373 #endif /* DEBUGGING */
375 /* get stuck here until the end */
376 while ((cmd = dsi_receive(dsi))) {
378 child.flags &= ~CHILD_SLEEPING;
379 dsi_sleep(dsi, 0); /* wake up */
381 if (reload_request) {
383 load_volumes(child.obj);
390 /* The first SIGINT enables debugging, the second one kills us */
391 action.sa_handler = afp_dsi_die;
392 sigfillset( &action.sa_mask );
393 action.sa_flags = SA_RESTART;
394 if ( sigaction( SIGINT, &action, NULL ) < 0 ) {
395 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
396 afp_dsi_die(EXITERR_SYS);
399 sprintf(logstr, "default log_maxdebug /tmp/afpd.%u.XXXXXX", getpid());
403 if (cmd == DSIFUNC_TICKLE) {
404 /* timer is not every 30 seconds anymore, so we don't get killed on the client side. */
405 if ((child.flags & CHILD_DIE))
407 pending_request(dsi);
411 child.flags |= CHILD_DATA;
415 LOG(log_info, logtype_afpd, "done");
422 if ( flushfork( writtenfork ) < 0 ) {
423 LOG(log_error, logtype_afpd, "main flushfork: %s", strerror(errno) );
429 function = (u_char) dsi->commands[0];
431 /* send off an afp command. in a couple cases, we take advantage
432 * of the fact that we're a stream-based protocol. */
433 if (afp_switch[function]) {
434 dsi->datalen = DSI_DATASIZ;
435 child.flags |= CHILD_RUNNING;
437 LOG(log_debug, logtype_afpd, "<== Start AFP command: %s", AfpNum2name(function));
439 err = (*afp_switch[function])(obj,
440 (char *)&dsi->commands, dsi->cmdlen,
441 (char *)&dsi->data, &dsi->datalen);
443 LOG(log_debug, logtype_afpd, "==> Finished AFP command: %s -> %s",
444 AfpNum2name(function), AfpErr2name(err));
446 /* bring everything back to old euid, egid */
448 restore_uidgid ( &obj->uidgid );
449 #endif /* FORCE_UIDGID */
450 child.flags &= ~CHILD_RUNNING;
452 LOG(log_error, logtype_afpd, "bad function %X", function);
457 /* single shot toggle that gets set by dsi_readinit. */
463 if (!dsi_cmdreply(dsi, err)) {
464 LOG(log_error, logtype_afpd, "dsi_cmdreply(%d): %s", dsi->socket, strerror(errno) );
465 afp_dsi_die(EXITERR_CLNT);
469 case DSIFUNC_WRITE: /* FPWrite and FPAddIcon */
470 function = (u_char) dsi->commands[0];
471 if ( afp_switch[ function ] != NULL ) {
472 dsi->datalen = DSI_DATASIZ;
473 child.flags |= CHILD_RUNNING;
475 LOG(log_debug, logtype_afpd, "<== Start AFP command: %s", AfpNum2name(function));
477 err = (*afp_switch[function])(obj,
478 (char *)&dsi->commands, dsi->cmdlen,
479 (char *)&dsi->data, &dsi->datalen);
481 LOG(log_debug, logtype_afpd, "==> Finished AFP command: %s -> %s",
482 AfpNum2name(function), AfpErr2name(err));
484 child.flags &= ~CHILD_RUNNING;
486 /* bring everything back to old euid, egid */
488 restore_uidgid ( &obj->uidgid );
489 #endif /* FORCE_UIDGID */
491 LOG(log_error, logtype_afpd, "(write) bad function %x", function);
496 if (!dsi_wrtreply(dsi, err)) {
497 LOG(log_error, logtype_afpd, "dsi_wrtreply: %s", strerror(errno) );
498 afp_dsi_die(EXITERR_CLNT);
502 case DSIFUNC_ATTN: /* attention replies */
505 /* error. this usually implies a mismatch of some kind
506 * between server and client. if things are correct,
507 * we need to flush the rest of the packet if necessary. */
509 LOG(log_info, logtype_afpd,"afp_dsi: spurious command %d", cmd);
510 dsi_writeinit(dsi, dsi->data, DSI_DATASIZ);
514 pending_request(dsi);
518 afp_dsi_die(EXITERR_CLNT);