2 * Copyright (c) 1999 Adrian Sun (asun@zoology.washington.edu)
3 * Copyright (c) 1990,1993 Regents of The University of Michigan.
4 * All Rights Reserved. See COPYRIGHT.
6 * modified from main.c. this handles afp over tcp.
11 #endif /* HAVE_CONFIG_H */
20 #endif /* HAVE_UNISTD_H */
21 #include <sys/socket.h>
23 #ifdef HAVE_SYS_STAT_H
25 #endif /* HAVE_SYS_STAT_H */
26 #include <netinet/in.h>
27 #include <arpa/inet.h>
28 #include <atalk/logger.h>
31 #include <atalk/dsi.h>
32 #include <atalk/compat.h>
33 #include <atalk/util.h>
34 #include <atalk/uuid.h>
45 #endif /* FORCE_UIDGID */
48 * We generally pass this from afp_over_dsi to all afp_* funcs, so it should already be
49 * available everywhere. Unfortunately some funcs (eg acltoownermode) need acces to it
50 * but are deeply nested in the function chain with the caller already without acces to it.
51 * Changing this would require adding a reference to the caller which itself might be
52 * called in many places (eg acltoownermode is called from accessmode).
53 * The only sane way out is providing a copy of it here:
55 AFPObj *AFPobj = NULL;
66 * - indexed just by taking DSIreqID mod REPLAYCACHE_SIZE
68 static rc_elem_t replaycache[REPLAYCACHE_SIZE];
70 static sigjmp_buf recon_jmp;
71 static void afp_dsi_close(AFPObj *obj)
73 DSI *dsi = obj->handle;
78 /* we may have been called from a signal handler caught when afpd was running
79 * as uid 0, that's the wrong user for volume's prexec_close scripts if any,
80 * restore our login user
82 if (geteuid() != obj->uid) {
83 if (seteuid( obj->uid ) < 0) {
84 LOG(log_error, logtype_afpd, "can't seteuid(%u) back %s: uid: %u, euid: %u",
85 obj->uid, strerror(errno), getuid(), geteuid());
94 LOG(log_note, logtype_afpd, "AFP statistics: %.2f KB read, %.2f KB written",
95 dsi->read_count/1024.0, dsi->write_count/1024.0);
101 /* -------------------------------
103 * a little bit of code duplication.
105 static void afp_dsi_die(int sig)
107 DSI *dsi = (DSI *)AFPobj->handle;
109 if (dsi->flags & DSI_RECONINPROG) {
110 /* Primary reconnect succeeded, got SIGTERM from afpd parent */
111 dsi->flags &= ~DSI_RECONINPROG;
112 return; /* this returns to afp_disconnect */
115 if (dsi->flags & DSI_DISCONNECTED) {
116 LOG(log_note, logtype_afpd, "Disconnected session terminating");
120 dsi_attention(AFPobj->handle, AFPATTN_SHUTDOWN);
121 afp_dsi_close(AFPobj);
122 if (sig) /* if no signal, assume dieing because logins are disabled &
123 don't log it (maintenance mode)*/
124 LOG(log_info, logtype_afpd, "Connection terminated");
125 if (sig == SIGTERM || sig == SIGALRM) {
133 /* SIGURG handler (primary reconnect) */
134 static void afp_dsi_transfer_session(int sig _U_)
138 DSI *dsi = (DSI *)AFPobj->handle;
140 LOG(log_debug, logtype_afpd, "afp_dsi_transfer_session: got SIGURG, trying to receive session");
142 if (readt(AFPobj->ipc_fd, &dsiID, 2, 0, 2) != 2) {
143 LOG(log_error, logtype_afpd, "afp_dsi_transfer_session: couldn't receive DSI id, goodbye");
144 afp_dsi_close(AFPobj);
148 if ((socket = recv_fd(AFPobj->ipc_fd, 1)) == -1) {
149 LOG(log_error, logtype_afpd, "afp_dsi_transfer_session: couldn't receive session fd, goodbye");
150 afp_dsi_close(AFPobj);
154 LOG(log_debug, logtype_afpd, "afp_dsi_transfer_session: received socket fd: %i", socket);
156 dsi->proto_close(dsi);
157 dsi->socket = socket;
158 dsi->flags = DSI_RECONSOCKET;
160 dsi->eof = dsi->start = dsi->buffer;
162 dsi->header.dsi_requestID = dsiID;
163 dsi->header.dsi_command = DSIFUNC_CMD;
166 * The session transfer happens in the middle of FPDisconnect old session, thus we
167 * have to send the reply now.
169 if (!dsi_cmdreply(dsi, AFP_OK)) {
170 LOG(log_error, logtype_afpd, "dsi_cmdreply: %s", strerror(errno) );
171 afp_dsi_close(AFPobj);
175 LOG(log_note, logtype_afpd, "afp_dsi_transfer_session: succesfull primary reconnect");
177 * Now returning from this signal handler return to dsi_receive which should start
178 * reading/continuing from the connected socket that was passed via the parent from
179 * another session. The parent will terminate that session.
181 siglongjmp(recon_jmp, 1);
184 /* ------------------- */
185 static void afp_dsi_timedown(int sig _U_)
189 DSI *dsi = (DSI *)AFPobj->handle;
190 dsi->flags |= DSI_DIE;
191 /* shutdown and don't reconnect. server going down in 5 minutes. */
192 setmessage("The server is going down for maintenance.");
193 if (dsi_attention(AFPobj->handle, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
194 AFPATTN_MESG | AFPATTN_TIME(5)) < 0) {
195 DSI *dsi = (DSI *)AFPobj->handle;
196 dsi->down_request = 1;
199 it.it_interval.tv_sec = 0;
200 it.it_interval.tv_usec = 0;
201 it.it_value.tv_sec = 300;
202 it.it_value.tv_usec = 0;
204 if ( setitimer( ITIMER_REAL, &it, NULL ) < 0 ) {
205 LOG(log_error, logtype_afpd, "afp_timedown: setitimer: %s", strerror(errno) );
206 afp_dsi_die(EXITERR_SYS);
208 memset(&sv, 0, sizeof(sv));
209 sv.sa_handler = afp_dsi_die;
210 sigemptyset( &sv.sa_mask );
211 sigaddset(&sv.sa_mask, SIGHUP);
212 sigaddset(&sv.sa_mask, SIGTERM);
213 sv.sa_flags = SA_RESTART;
214 if ( sigaction( SIGALRM, &sv, NULL ) < 0 ) {
215 LOG(log_error, logtype_afpd, "afp_timedown: sigaction: %s", strerror(errno) );
216 afp_dsi_die(EXITERR_SYS);
220 sv.sa_handler = SIG_IGN;
221 sigemptyset( &sv.sa_mask );
222 sv.sa_flags = SA_RESTART;
223 if ( sigaction( SIGUSR1, &sv, NULL ) < 0 ) {
224 LOG(log_error, logtype_afpd, "afp_timedown: sigaction SIGHUP: %s", strerror(errno) );
225 afp_dsi_die(EXITERR_SYS);
229 /* ---------------------------------
230 * SIGHUP reload configuration file
232 volatile int reload_request = 0;
234 static void afp_dsi_reload(int sig _U_)
239 /* ---------------------------------
240 * SIGINT: enable max_debug LOGging
242 static volatile sig_atomic_t debug_request = 0;
244 static void afp_dsi_debug(int sig _U_)
249 /* ---------------------- */
250 static void afp_dsi_getmesg (int sig _U_)
252 DSI *dsi = (DSI *)AFPobj->handle;
254 dsi->msg_request = 1;
255 if (dsi_attention(AFPobj->handle, AFPATTN_MESG | AFPATTN_TIME(5)) < 0)
256 dsi->msg_request = 2;
259 static void alarm_handler(int sig _U_)
262 DSI *dsi = (DSI *)AFPobj->handle;
264 /* we have to restart the timer because some libraries may use alarm() */
265 setitimer(ITIMER_REAL, &dsi->timer, NULL);
267 /* we got some traffic from the client since the previous timer tick. */
268 if ((dsi->flags & DSI_DATA)) {
269 dsi->flags &= ~DSI_DATA;
274 LOG(log_maxdebug, logtype_afpd, "alarm: tickles: %u, flags: %s|%s|%s|%s|%s|%s|%s|%s|%s",
276 (dsi->flags & DSI_DATA) ? "DSI_DATA" : "-",
277 (dsi->flags & DSI_RUNNING) ? "DSI_RUNNING" : "-",
278 (dsi->flags & DSI_SLEEPING) ? "DSI_SLEEPING" : "-",
279 (dsi->flags & DSI_EXTSLEEP) ? "DSI_EXTSLEEP" : "-",
280 (dsi->flags & DSI_DISCONNECTED) ? "DSI_DISCONNECTED" : "-",
281 (dsi->flags & DSI_DIE) ? "DSI_DIE" : "-",
282 (dsi->flags & DSI_NOREPLY) ? "DSI_NOREPLY" : "-",
283 (dsi->flags & DSI_RECONSOCKET) ? "DSI_RECONSOCKET" : "-",
284 (dsi->flags & DSI_RECONINPROG) ? "DSI_RECONINPROG" : "-");
286 if (dsi->flags & DSI_SLEEPING) {
287 if (dsi->tickle > AFPobj->options.sleep) {
288 LOG(log_note, logtype_afpd, "afp_alarm: sleep time ended");
289 afp_dsi_die(EXITERR_CLNT);
294 if (dsi->flags & DSI_DISCONNECTED) {
295 if (dsi->tickle > AFPobj->options.disconnected) {
296 LOG(log_error, logtype_afpd, "afp_alarm: reconnect timer expired, goodbye");
297 afp_dsi_die(EXITERR_CLNT);
302 /* if we're in the midst of processing something, don't die. */
303 if ( !(dsi->flags & DSI_RUNNING) && (dsi->tickle >= AFPobj->options.timeout)) {
304 LOG(log_error, logtype_afpd, "afp_alarm: child timed out, entering disconnected state");
305 (void)dsi_disconnect(dsi);
309 if ((err = pollvoltime(AFPobj)) == 0)
310 LOG(log_debug, logtype_afpd, "afp_alarm: sending DSI tickle");
311 err = dsi_tickle(AFPobj->handle);
313 LOG(log_error, logtype_afpd, "afp_alarm: connection problem, entering disconnected state");
314 (void)dsi_disconnect(dsi);
319 if dsi->in_write is set attention, tickle (and close?) msg
320 aren't sent. We don't care about tickle
322 static void pending_request(DSI *dsi)
324 /* send pending attention */
326 /* read msg if any, it could be done in afp_getsrvrmesg */
327 if (dsi->msg_request) {
328 if (dsi->msg_request == 2) {
329 /* didn't send it in signal handler */
330 dsi_attention(AFPobj->handle, AFPATTN_MESG | AFPATTN_TIME(5));
332 dsi->msg_request = 0;
335 if (dsi->down_request) {
336 dsi->down_request = 0;
337 dsi_attention(AFPobj->handle, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
338 AFPATTN_MESG | AFPATTN_TIME(5));
342 /* -------------------------------------------
343 afp over dsi. this never returns.
345 void afp_over_dsi(AFPObj *obj)
347 DSI *dsi = (DSI *) obj->handle;
351 struct sigaction action;
354 obj->exit = afp_dsi_die;
355 obj->reply = (int (*)()) dsi_cmdreply;
356 obj->attention = (int (*)(void *, AFPUserBytes)) dsi_attention;
359 memset(&action, 0, sizeof(action));
362 action.sa_handler = afp_dsi_reload;
363 sigemptyset( &action.sa_mask );
364 sigaddset(&action.sa_mask, SIGALRM);
365 sigaddset(&action.sa_mask, SIGTERM);
366 sigaddset(&action.sa_mask, SIGUSR1);
367 sigaddset(&action.sa_mask, SIGINT);
368 sigaddset(&action.sa_mask, SIGUSR2);
369 action.sa_flags = SA_RESTART;
370 if ( sigaction( SIGHUP, &action, NULL ) < 0 ) {
371 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
372 afp_dsi_die(EXITERR_SYS);
376 action.sa_handler = afp_dsi_transfer_session;
377 sigemptyset( &action.sa_mask );
378 sigaddset(&action.sa_mask, SIGALRM);
379 sigaddset(&action.sa_mask, SIGTERM);
380 sigaddset(&action.sa_mask, SIGUSR1);
381 sigaddset(&action.sa_mask, SIGINT);
382 sigaddset(&action.sa_mask, SIGUSR2);
383 action.sa_flags = SA_RESTART;
384 if ( sigaction( SIGURG, &action, NULL ) < 0 ) {
385 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
386 afp_dsi_die(EXITERR_SYS);
389 /* install SIGTERM */
390 action.sa_handler = afp_dsi_die;
391 sigemptyset( &action.sa_mask );
392 sigaddset(&action.sa_mask, SIGALRM);
393 sigaddset(&action.sa_mask, SIGHUP);
394 sigaddset(&action.sa_mask, SIGUSR1);
395 sigaddset(&action.sa_mask, SIGINT);
396 sigaddset(&action.sa_mask, SIGUSR2);
397 action.sa_flags = SA_RESTART;
398 if ( sigaction( SIGTERM, &action, NULL ) < 0 ) {
399 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
400 afp_dsi_die(EXITERR_SYS);
403 /* Added for server message support */
404 action.sa_handler = afp_dsi_getmesg;
405 sigemptyset( &action.sa_mask );
406 sigaddset(&action.sa_mask, SIGALRM);
407 sigaddset(&action.sa_mask, SIGTERM);
408 sigaddset(&action.sa_mask, SIGUSR1);
409 sigaddset(&action.sa_mask, SIGHUP);
410 sigaddset(&action.sa_mask, SIGINT);
411 action.sa_flags = SA_RESTART;
412 if ( sigaction( SIGUSR2, &action, NULL) < 0 ) {
413 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
414 afp_dsi_die(EXITERR_SYS);
417 /* SIGUSR1 - set down in 5 minutes */
418 action.sa_handler = afp_dsi_timedown;
419 sigemptyset( &action.sa_mask );
420 sigaddset(&action.sa_mask, SIGALRM);
421 sigaddset(&action.sa_mask, SIGHUP);
422 sigaddset(&action.sa_mask, SIGTERM);
423 sigaddset(&action.sa_mask, SIGINT);
424 sigaddset(&action.sa_mask, SIGUSR2);
425 action.sa_flags = SA_RESTART;
426 if ( sigaction( SIGUSR1, &action, NULL) < 0 ) {
427 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
428 afp_dsi_die(EXITERR_SYS);
431 /* SIGINT - enable max_debug LOGging to /tmp/afpd.PID.XXXXXX */
432 action.sa_handler = afp_dsi_debug;
433 sigfillset( &action.sa_mask );
434 action.sa_flags = SA_RESTART;
435 if ( sigaction( SIGINT, &action, NULL) < 0 ) {
436 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
437 afp_dsi_die(EXITERR_SYS);
442 action.sa_handler = alarm_handler;
443 sigemptyset(&action.sa_mask);
444 sigaddset(&action.sa_mask, SIGHUP);
445 sigaddset(&action.sa_mask, SIGTERM);
446 sigaddset(&action.sa_mask, SIGUSR1);
447 sigaddset(&action.sa_mask, SIGINT);
448 sigaddset(&action.sa_mask, SIGUSR2);
449 action.sa_flags = SA_RESTART;
450 if ((sigaction(SIGALRM, &action, NULL) < 0) ||
451 (setitimer(ITIMER_REAL, &dsi->timer, NULL) < 0)) {
452 afp_dsi_die(EXITERR_SYS);
454 #endif /* DEBUGGING */
456 if (dircache_init(obj->options.dircachesize) != 0)
457 afp_dsi_die(EXITERR_SYS);
459 /* set TCP snd/rcv buf */
460 if (obj->options.tcp_rcvbuf) {
461 if (setsockopt(dsi->socket,
464 &obj->options.tcp_rcvbuf,
465 sizeof(obj->options.tcp_rcvbuf)) != 0) {
466 LOG(log_error, logtype_dsi, "afp_over_dsi: setsockopt(SO_RCVBUF): %s", strerror(errno));
469 if (obj->options.tcp_sndbuf) {
470 if (setsockopt(dsi->socket,
473 &obj->options.tcp_sndbuf,
474 sizeof(obj->options.tcp_sndbuf)) != 0) {
475 LOG(log_error, logtype_dsi, "afp_over_dsi: setsockopt(SO_SNDBUF): %s", strerror(errno));
479 /* get stuck here until the end */
481 if (sigsetjmp(recon_jmp, 1) != 0)
482 /* returning from SIGALARM handler for a primary reconnect */
485 /* Blocking read on the network socket */
486 cmd = dsi_receive(dsi);
489 /* cmd == 0 is the error condition */
490 if (dsi->flags & DSI_RECONSOCKET) {
491 /* we just got a reconnect so we immediately try again to receive on the new fd */
492 dsi->flags &= ~DSI_RECONSOCKET;
495 /* Some error on the client connection, enter disconnected state */
496 (void)dsi_disconnect(dsi);
498 /* the client sometimes logs out (afp_logout) but doesn't close the DSI session */
499 if (dsi->flags & DSI_AFP_LOGGED_OUT) {
504 pause(); /* gets interrupted by SIGALARM or SIGURG tickle */
505 continue; /* continue receiving until disconnect timer expires
506 * or a primary reconnect succeeds */
509 if (!(dsi->flags & DSI_EXTSLEEP) && (dsi->flags & DSI_SLEEPING)) {
510 LOG(log_debug, logtype_afpd, "afp_over_dsi: got data, ending normal sleep");
511 dsi->flags &= ~DSI_SLEEPING;
515 if (reload_request) {
517 load_volumes(AFPobj);
520 /* The first SIGINT enables debugging, the next restores the config */
522 static int debugging = 0;
529 if (obj->options.logconfig)
530 setuplog(obj->options.logconfig);
532 setuplog("default log_note");
537 sprintf(logstr, "default log_maxdebug /tmp/afpd.%u.XXXXXX", getpid());
543 dsi->flags |= DSI_DATA;
549 LOG(log_debug, logtype_afpd, "DSI: close session request");
551 LOG(log_note, logtype_afpd, "done");
555 dsi->flags &= ~DSI_DATA; /* thats no data in the sense we use it in alarm_handler */
556 LOG(log_debug, logtype_afpd, "DSI: client tickle");
557 /* timer is not every 30 seconds anymore, so we don't get killed on the client side. */
558 if ((dsi->flags & DSI_DIE))
565 if ( flushfork( writtenfork ) < 0 ) {
566 LOG(log_error, logtype_afpd, "main flushfork: %s", strerror(errno) );
572 function = (u_char) dsi->commands[0];
574 /* AFP replay cache */
575 rc_idx = dsi->clientID % REPLAYCACHE_SIZE;
576 LOG(log_debug, logtype_afpd, "DSI request ID: %u", dsi->clientID);
578 if (replaycache[rc_idx].DSIreqID == dsi->clientID
579 && replaycache[rc_idx].AFPcommand == function) {
580 LOG(log_note, logtype_afpd, "AFP Replay Cache match: id: %u / cmd: %s",
581 dsi->clientID, AfpNum2name(function));
582 err = replaycache[rc_idx].result;
583 /* AFP replay cache end */
585 /* send off an afp command. in a couple cases, we take advantage
586 * of the fact that we're a stream-based protocol. */
587 if (afp_switch[function]) {
588 dsi->datalen = DSI_DATASIZ;
589 dsi->flags |= DSI_RUNNING;
591 LOG(log_debug, logtype_afpd, "<== Start AFP command: %s", AfpNum2name(function));
593 err = (*afp_switch[function])(obj,
594 (char *)&dsi->commands, dsi->cmdlen,
595 (char *)&dsi->data, &dsi->datalen);
597 LOG(log_debug, logtype_afpd, "==> Finished AFP command: %s -> %s",
598 AfpNum2name(function), AfpErr2name(err));
600 dir_free_invalid_q();
603 /* bring everything back to old euid, egid */
605 restore_uidgid ( &obj->uidgid );
606 #endif /* FORCE_UIDGID */
607 dsi->flags &= ~DSI_RUNNING;
609 /* Add result to the AFP replay cache */
610 replaycache[rc_idx].DSIreqID = dsi->clientID;
611 replaycache[rc_idx].AFPcommand = function;
612 replaycache[rc_idx].result = err;
614 LOG(log_error, logtype_afpd, "bad function %X", function);
620 /* single shot toggle that gets set by dsi_readinit. */
621 if (dsi->flags & DSI_NOREPLY) {
622 dsi->flags &= ~DSI_NOREPLY;
626 if (!dsi_cmdreply(dsi, err)) {
627 LOG(log_error, logtype_afpd, "dsi_cmdreply(%d): %s", dsi->socket, strerror(errno) );
628 (void)dsi_disconnect(dsi);
632 case DSIFUNC_WRITE: /* FPWrite and FPAddIcon */
633 function = (u_char) dsi->commands[0];
634 if ( afp_switch[ function ] != NULL ) {
635 dsi->datalen = DSI_DATASIZ;
636 dsi->flags |= DSI_RUNNING;
638 LOG(log_debug, logtype_afpd, "<== Start AFP command: %s", AfpNum2name(function));
640 err = (*afp_switch[function])(obj,
641 (char *)&dsi->commands, dsi->cmdlen,
642 (char *)&dsi->data, &dsi->datalen);
644 LOG(log_debug, logtype_afpd, "==> Finished AFP command: %s -> %s",
645 AfpNum2name(function), AfpErr2name(err));
647 dsi->flags &= ~DSI_RUNNING;
649 /* bring everything back to old euid, egid */
651 restore_uidgid ( &obj->uidgid );
652 #endif /* FORCE_UIDGID */
654 LOG(log_error, logtype_afpd, "(write) bad function %x", function);
659 if (!dsi_wrtreply(dsi, err)) {
660 LOG(log_error, logtype_afpd, "dsi_wrtreply: %s", strerror(errno) );
661 (void)dsi_disconnect(dsi);
665 case DSIFUNC_ATTN: /* attention replies */
668 /* error. this usually implies a mismatch of some kind
669 * between server and client. if things are correct,
670 * we need to flush the rest of the packet if necessary. */
672 LOG(log_info, logtype_afpd,"afp_dsi: spurious command %d", cmd);
673 dsi_writeinit(dsi, dsi->data, DSI_DATASIZ);
677 pending_request(dsi);
681 afp_dsi_die(EXITERR_CLNT);