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 <netinet/tcp.h>
28 #include <arpa/inet.h>
32 #include <atalk/logger.h>
33 #include <atalk/dsi.h>
34 #include <atalk/compat.h>
35 #include <atalk/util.h>
36 #include <atalk/uuid.h>
37 #include <atalk/paths.h>
38 #include <atalk/server_ipc.h>
39 #include <atalk/fce_api.h>
40 #include <atalk/globals.h>
41 #include <atalk/netatalk_conf.h>
49 #define SOL_TCP IPPROTO_TCP
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;
71 * - indexed just by taking DSIreqID mod REPLAYCACHE_SIZE
73 static rc_elem_t replaycache[REPLAYCACHE_SIZE];
75 static sigjmp_buf recon_jmp;
76 static void afp_dsi_close(AFPObj *obj)
84 /* we may have been called from a signal handler caught when afpd was running
85 * as uid 0, that's the wrong user for volume's prexec_close scripts if any,
86 * restore our login user
88 if (geteuid() != obj->uid) {
89 if (seteuid( obj->uid ) < 0) {
90 LOG(log_error, logtype_afpd, "can't seteuid(%u) back %s: uid: %u, euid: %u",
91 obj->uid, strerror(errno), getuid(), geteuid());
98 /* Block sigs, PAM/systemd/whoever might send us a SIG??? in (*obj->logout)() -> pam_close_session() */
100 pthread_sigmask(SIG_BLOCK, &sigs, NULL);
104 LOG(log_note, logtype_afpd, "AFP statistics: %.2f KB read, %.2f KB written",
105 dsi->read_count/1024.0, dsi->write_count/1024.0);
111 /* -------------------------------
113 * a little bit of code duplication.
115 static void afp_dsi_die(int sig)
117 DSI *dsi = (DSI *)AFPobj->dsi;
119 if (dsi->flags & DSI_RECONINPROG) {
120 /* Primary reconnect succeeded, got SIGTERM from afpd parent */
121 dsi->flags &= ~DSI_RECONINPROG;
122 return; /* this returns to afp_disconnect */
125 if (dsi->flags & DSI_DISCONNECTED) {
126 LOG(log_note, logtype_afpd, "Disconnected session terminating");
130 dsi_attention(AFPobj->dsi, AFPATTN_SHUTDOWN);
131 afp_dsi_close(AFPobj);
132 if (sig) /* if no signal, assume dieing because logins are disabled &
133 don't log it (maintenance mode)*/
134 LOG(log_info, logtype_afpd, "Connection terminated");
135 if (sig == SIGTERM || sig == SIGALRM) {
143 /* SIGQUIT handler */
144 static void ipc_reconnect_handler(int sig _U_)
146 if (reconnect_ipc(AFPobj) != 0) {
147 LOG(log_error, logtype_afpd, "ipc_reconnect_handler: failed IPC reconnect");
148 afp_dsi_close(AFPobj);
152 if (ipc_child_write(AFPobj->ipc_fd, IPC_GETSESSION, AFPobj->sinfo.clientid_len, AFPobj->sinfo.clientid) != 0) {
153 LOG(log_error, logtype_afpd, "ipc_reconnect_handler: failed IPC ID resend");
154 afp_dsi_close(AFPobj);
157 LOG(log_note, logtype_afpd, "ipc_reconnect_handler: IPC reconnect done");
160 /* SIGURG handler (primary reconnect) */
161 static void afp_dsi_transfer_session(int sig _U_)
165 DSI *dsi = (DSI *)AFPobj->dsi;
167 LOG(log_debug, logtype_afpd, "afp_dsi_transfer_session: got SIGURG, trying to receive session");
169 if (readt(AFPobj->ipc_fd, &dsiID, 2, 0, 2) != 2) {
170 LOG(log_error, logtype_afpd, "afp_dsi_transfer_session: couldn't receive DSI id, goodbye");
171 afp_dsi_close(AFPobj);
175 if ((socket = recv_fd(AFPobj->ipc_fd, 1)) == -1) {
176 LOG(log_error, logtype_afpd, "afp_dsi_transfer_session: couldn't receive session fd, goodbye");
177 afp_dsi_close(AFPobj);
181 LOG(log_debug, logtype_afpd, "afp_dsi_transfer_session: received socket fd: %i", socket);
183 dsi->proto_close(dsi);
184 dsi->socket = socket;
185 dsi->flags = DSI_RECONSOCKET;
187 dsi->eof = dsi->start = dsi->buffer;
189 dsi->header.dsi_requestID = dsiID;
190 dsi->header.dsi_command = DSIFUNC_CMD;
193 * The session transfer happens in the middle of FPDisconnect old session, thus we
194 * have to send the reply now.
196 if (!dsi_cmdreply(dsi, AFP_OK)) {
197 LOG(log_error, logtype_afpd, "dsi_cmdreply: %s", strerror(errno) );
198 afp_dsi_close(AFPobj);
202 LOG(log_note, logtype_afpd, "afp_dsi_transfer_session: succesfull primary reconnect");
204 * Now returning from this signal handler return to dsi_receive which should start
205 * reading/continuing from the connected socket that was passed via the parent from
206 * another session. The parent will terminate that session.
208 siglongjmp(recon_jmp, 1);
211 /* ------------------- */
212 static void afp_dsi_timedown(int sig _U_)
216 DSI *dsi = (DSI *)AFPobj->dsi;
217 dsi->flags |= DSI_DIE;
218 /* shutdown and don't reconnect. server going down in 5 minutes. */
219 setmessage("The server is going down for maintenance.");
220 if (dsi_attention(AFPobj->dsi, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
221 AFPATTN_MESG | AFPATTN_TIME(5)) < 0) {
222 DSI *dsi = (DSI *)AFPobj->dsi;
223 dsi->down_request = 1;
226 it.it_interval.tv_sec = 0;
227 it.it_interval.tv_usec = 0;
228 it.it_value.tv_sec = 300;
229 it.it_value.tv_usec = 0;
231 if ( setitimer( ITIMER_REAL, &it, NULL ) < 0 ) {
232 LOG(log_error, logtype_afpd, "afp_timedown: setitimer: %s", strerror(errno) );
233 afp_dsi_die(EXITERR_SYS);
235 memset(&sv, 0, sizeof(sv));
236 sv.sa_handler = afp_dsi_die;
237 sigemptyset( &sv.sa_mask );
238 sigaddset(&sv.sa_mask, SIGHUP);
239 sigaddset(&sv.sa_mask, SIGTERM);
240 sv.sa_flags = SA_RESTART;
241 if ( sigaction( SIGALRM, &sv, NULL ) < 0 ) {
242 LOG(log_error, logtype_afpd, "afp_timedown: sigaction: %s", strerror(errno) );
243 afp_dsi_die(EXITERR_SYS);
247 sv.sa_handler = SIG_IGN;
248 sigemptyset( &sv.sa_mask );
249 sv.sa_flags = SA_RESTART;
250 if ( sigaction( SIGUSR1, &sv, NULL ) < 0 ) {
251 LOG(log_error, logtype_afpd, "afp_timedown: sigaction SIGHUP: %s", strerror(errno) );
252 afp_dsi_die(EXITERR_SYS);
256 /* ---------------------------------
257 * SIGHUP reload configuration file
259 volatile int reload_request = 0;
261 static void afp_dsi_reload(int sig _U_)
266 /* ---------------------------------
267 * SIGINT: enable max_debug LOGging
269 static volatile sig_atomic_t debug_request = 0;
271 static void afp_dsi_debug(int sig _U_)
276 /* ---------------------- */
277 static void afp_dsi_getmesg (int sig _U_)
279 DSI *dsi = (DSI *)AFPobj->dsi;
281 dsi->msg_request = 1;
282 if (dsi_attention(AFPobj->dsi, AFPATTN_MESG | AFPATTN_TIME(5)) < 0)
283 dsi->msg_request = 2;
286 static void alarm_handler(int sig _U_)
289 DSI *dsi = (DSI *)AFPobj->dsi;
291 /* we have to restart the timer because some libraries may use alarm() */
292 setitimer(ITIMER_REAL, &dsi->timer, NULL);
294 /* we got some traffic from the client since the previous timer tick. */
295 if ((dsi->flags & DSI_DATA)) {
296 dsi->flags &= ~DSI_DATA;
301 LOG(log_maxdebug, logtype_afpd, "alarm: tickles: %u, flags: %s|%s|%s|%s|%s|%s|%s|%s|%s",
303 (dsi->flags & DSI_DATA) ? "DSI_DATA" : "-",
304 (dsi->flags & DSI_RUNNING) ? "DSI_RUNNING" : "-",
305 (dsi->flags & DSI_SLEEPING) ? "DSI_SLEEPING" : "-",
306 (dsi->flags & DSI_EXTSLEEP) ? "DSI_EXTSLEEP" : "-",
307 (dsi->flags & DSI_DISCONNECTED) ? "DSI_DISCONNECTED" : "-",
308 (dsi->flags & DSI_DIE) ? "DSI_DIE" : "-",
309 (dsi->flags & DSI_NOREPLY) ? "DSI_NOREPLY" : "-",
310 (dsi->flags & DSI_RECONSOCKET) ? "DSI_RECONSOCKET" : "-",
311 (dsi->flags & DSI_RECONINPROG) ? "DSI_RECONINPROG" : "-");
313 if (dsi->flags & DSI_SLEEPING) {
314 if (dsi->tickle > AFPobj->options.sleep) {
315 LOG(log_note, logtype_afpd, "afp_alarm: sleep time ended");
316 afp_dsi_die(EXITERR_CLNT);
321 if (dsi->flags & DSI_DISCONNECTED) {
322 if (geteuid() == 0) {
323 LOG(log_note, logtype_afpd, "afp_alarm: unauthenticated user, connection problem");
324 afp_dsi_die(EXITERR_CLNT);
326 if (dsi->tickle > AFPobj->options.disconnected) {
327 LOG(log_error, logtype_afpd, "afp_alarm: reconnect timer expired, goodbye");
328 afp_dsi_die(EXITERR_CLNT);
333 /* if we're in the midst of processing something, don't die. */
334 if (dsi->tickle >= AFPobj->options.timeout) {
335 LOG(log_error, logtype_afpd, "afp_alarm: child timed out, entering disconnected state");
336 if (dsi_disconnect(dsi) != 0)
337 afp_dsi_die(EXITERR_CLNT);
341 if ((err = pollvoltime(AFPobj)) == 0)
342 LOG(log_debug, logtype_afpd, "afp_alarm: sending DSI tickle");
343 err = dsi_tickle(AFPobj->dsi);
345 if (geteuid() == 0) {
346 LOG(log_note, logtype_afpd, "afp_alarm: unauthenticated user, connection problem");
347 afp_dsi_die(EXITERR_CLNT);
349 LOG(log_error, logtype_afpd, "afp_alarm: connection problem, entering disconnected state");
350 if (dsi_disconnect(dsi) != 0)
351 afp_dsi_die(EXITERR_CLNT);
356 if dsi->in_write is set attention, tickle (and close?) msg
357 aren't sent. We don't care about tickle
359 static void pending_request(DSI *dsi)
361 /* send pending attention */
363 /* read msg if any, it could be done in afp_getsrvrmesg */
364 if (dsi->msg_request) {
365 if (dsi->msg_request == 2) {
366 /* didn't send it in signal handler */
367 dsi_attention(AFPobj->dsi, AFPATTN_MESG | AFPATTN_TIME(5));
369 dsi->msg_request = 0;
372 if (dsi->down_request) {
373 dsi->down_request = 0;
374 dsi_attention(AFPobj->dsi, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
375 AFPATTN_MESG | AFPATTN_TIME(5));
379 void afp_over_dsi_sighandlers(AFPObj *obj)
381 DSI *dsi = (DSI *) obj->dsi;
382 struct sigaction action;
384 memset(&action, 0, sizeof(action));
385 sigfillset(&action.sa_mask);
386 action.sa_flags = SA_RESTART;
389 action.sa_handler = afp_dsi_reload;
390 if ( sigaction( SIGHUP, &action, NULL ) < 0 ) {
391 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
392 afp_dsi_die(EXITERR_SYS);
396 action.sa_handler = afp_dsi_transfer_session;
397 if ( sigaction( SIGURG, &action, NULL ) < 0 ) {
398 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
399 afp_dsi_die(EXITERR_SYS);
402 /* install SIGTERM */
403 action.sa_handler = afp_dsi_die;
404 if ( sigaction( SIGTERM, &action, NULL ) < 0 ) {
405 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
406 afp_dsi_die(EXITERR_SYS);
409 /* install SIGQUIT */
410 action.sa_handler = ipc_reconnect_handler;
411 if ( sigaction(SIGQUIT, &action, NULL ) < 0 ) {
412 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
413 afp_dsi_die(EXITERR_SYS);
416 /* SIGUSR2 - server message support */
417 action.sa_handler = afp_dsi_getmesg;
418 if ( sigaction( SIGUSR2, &action, NULL) < 0 ) {
419 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
420 afp_dsi_die(EXITERR_SYS);
423 /* SIGUSR1 - set down in 5 minutes */
424 action.sa_handler = afp_dsi_timedown;
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 if ( sigaction( SIGINT, &action, NULL) < 0 ) {
434 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
435 afp_dsi_die(EXITERR_SYS);
439 /* SIGALRM - tickle handler */
440 action.sa_handler = alarm_handler;
441 if ((sigaction(SIGALRM, &action, NULL) < 0) ||
442 (setitimer(ITIMER_REAL, &dsi->timer, NULL) < 0)) {
443 afp_dsi_die(EXITERR_SYS);
445 #endif /* DEBUGGING */
448 /* -------------------------------------------
449 afp over dsi. this never returns.
451 void afp_over_dsi(AFPObj *obj)
453 DSI *dsi = (DSI *) obj->dsi;
459 obj->exit = afp_dsi_die;
460 obj->reply = (int (*)()) dsi_cmdreply;
461 obj->attention = (int (*)(void *, AFPUserBytes)) dsi_attention;
464 afp_over_dsi_sighandlers(obj);
466 if (dircache_init(obj->options.dircachesize) != 0)
467 afp_dsi_die(EXITERR_SYS);
469 /* set TCP snd/rcv buf */
470 if (obj->options.tcp_rcvbuf) {
471 if (setsockopt(dsi->socket,
474 &obj->options.tcp_rcvbuf,
475 sizeof(obj->options.tcp_rcvbuf)) != 0) {
476 LOG(log_error, logtype_dsi, "afp_over_dsi: setsockopt(SO_RCVBUF): %s", strerror(errno));
479 if (obj->options.tcp_sndbuf) {
480 if (setsockopt(dsi->socket,
483 &obj->options.tcp_sndbuf,
484 sizeof(obj->options.tcp_sndbuf)) != 0) {
485 LOG(log_error, logtype_dsi, "afp_over_dsi: setsockopt(SO_SNDBUF): %s", strerror(errno));
489 /* set TCP_NODELAY */
491 setsockopt(dsi->socket, SOL_TCP, TCP_NODELAY, &flag, sizeof(flag));
493 /* get stuck here until the end */
495 if (sigsetjmp(recon_jmp, 1) != 0)
496 /* returning from SIGALARM handler for a primary reconnect */
499 /* Blocking read on the network socket */
500 cmd = dsi_stream_receive(dsi);
503 /* cmd == 0 is the error condition */
504 if (dsi->flags & DSI_RECONSOCKET) {
505 /* we just got a reconnect so we immediately try again to receive on the new fd */
506 dsi->flags &= ~DSI_RECONSOCKET;
510 /* the client sometimes logs out (afp_logout) but doesn't close the DSI session */
511 if (dsi->flags & DSI_AFP_LOGGED_OUT) {
512 LOG(log_note, logtype_afpd, "afp_over_dsi: client logged out, terminating DSI session");
518 /* got ECONNRESET in read from client => exit*/
519 if (dsi->flags & DSI_GOT_ECONNRESET) {
520 LOG(log_note, logtype_afpd, "afp_over_dsi: client connection reset");
526 if (dsi->flags & DSI_RECONINPROG) {
527 LOG(log_note, logtype_afpd, "afp_over_dsi: failed reconnect");
532 /* Some error on the client connection, enter disconnected state */
533 if (dsi_disconnect(dsi) != 0)
534 afp_dsi_die(EXITERR_CLNT);
536 while (dsi->flags & DSI_DISCONNECTED)
537 pause(); /* gets interrupted by SIGALARM or SIGURG tickle */
538 continue; /* continue receiving until disconnect timer expires
539 * or a primary reconnect succeeds */
542 if (!(dsi->flags & DSI_EXTSLEEP) && (dsi->flags & DSI_SLEEPING)) {
543 LOG(log_debug, logtype_afpd, "afp_over_dsi: got data, ending normal sleep");
544 dsi->flags &= ~DSI_SLEEPING;
548 if (reload_request) {
550 load_volumes(AFPobj);
553 /* The first SIGINT enables debugging, the next restores the config */
555 static int debugging = 0;
562 if (obj->options.logconfig)
563 setuplog(obj->options.logconfig, obj->options.logfile);
565 setuplog("default:note", NULL);
570 sprintf(logstr, "/tmp/afpd.%u.XXXXXX", getpid());
571 setuplog("default:maxdebug", logstr);
576 dsi->flags |= DSI_DATA;
582 LOG(log_debug, logtype_afpd, "DSI: close session request");
584 LOG(log_note, logtype_afpd, "done");
588 dsi->flags &= ~DSI_DATA; /* thats no data in the sense we use it in alarm_handler */
589 LOG(log_debug, logtype_afpd, "DSI: client tickle");
590 /* timer is not every 30 seconds anymore, so we don't get killed on the client side. */
591 if ((dsi->flags & DSI_DIE))
598 if ( flushfork( writtenfork ) < 0 ) {
599 LOG(log_error, logtype_afpd, "main flushfork: %s", strerror(errno) );
605 function = (u_char) dsi->commands[0];
607 /* AFP replay cache */
608 rc_idx = dsi->clientID % REPLAYCACHE_SIZE;
609 LOG(log_debug, logtype_dsi, "DSI request ID: %u", dsi->clientID);
611 if (replaycache[rc_idx].DSIreqID == dsi->clientID
612 && replaycache[rc_idx].AFPcommand == function) {
613 LOG(log_note, logtype_afpd, "AFP Replay Cache match: id: %u / cmd: %s",
614 dsi->clientID, AfpNum2name(function));
615 err = replaycache[rc_idx].result;
616 /* AFP replay cache end */
618 /* send off an afp command. in a couple cases, we take advantage
619 * of the fact that we're a stream-based protocol. */
620 if (afp_switch[function]) {
621 dsi->datalen = DSI_DATASIZ;
622 dsi->flags |= DSI_RUNNING;
624 LOG(log_debug, logtype_afpd, "<== Start AFP command: %s", AfpNum2name(function));
626 err = (*afp_switch[function])(obj,
627 (char *)dsi->commands, dsi->cmdlen,
628 (char *)&dsi->data, &dsi->datalen);
630 LOG(log_debug, logtype_afpd, "==> Finished AFP command: %s -> %s",
631 AfpNum2name(function), AfpErr2name(err));
633 dir_free_invalid_q();
635 dsi->flags &= ~DSI_RUNNING;
637 /* Add result to the AFP replay cache */
638 replaycache[rc_idx].DSIreqID = dsi->clientID;
639 replaycache[rc_idx].AFPcommand = function;
640 replaycache[rc_idx].result = err;
642 LOG(log_error, logtype_afpd, "bad function %X", function);
648 /* single shot toggle that gets set by dsi_readinit. */
649 if (dsi->flags & DSI_NOREPLY) {
650 dsi->flags &= ~DSI_NOREPLY;
652 } else if (!dsi_cmdreply(dsi, err)) {
653 LOG(log_error, logtype_afpd, "dsi_cmdreply(%d): %s", dsi->socket, strerror(errno) );
654 if (dsi_disconnect(dsi) != 0)
655 afp_dsi_die(EXITERR_CLNT);
659 case DSIFUNC_WRITE: /* FPWrite and FPAddIcon */
660 function = (u_char) dsi->commands[0];
661 if ( afp_switch[ function ] != NULL ) {
662 dsi->datalen = DSI_DATASIZ;
663 dsi->flags |= DSI_RUNNING;
665 LOG(log_debug, logtype_afpd, "<== Start AFP command: %s", AfpNum2name(function));
667 err = (*afp_switch[function])(obj,
668 (char *)dsi->commands, dsi->cmdlen,
669 (char *)&dsi->data, &dsi->datalen);
671 LOG(log_debug, logtype_afpd, "==> Finished AFP command: %s -> %s",
672 AfpNum2name(function), AfpErr2name(err));
674 dsi->flags &= ~DSI_RUNNING;
676 LOG(log_error, logtype_afpd, "(write) bad function %x", function);
681 if (!dsi_wrtreply(dsi, err)) {
682 LOG(log_error, logtype_afpd, "dsi_wrtreply: %s", strerror(errno) );
683 if (dsi_disconnect(dsi) != 0)
684 afp_dsi_die(EXITERR_CLNT);
688 case DSIFUNC_ATTN: /* attention replies */
691 /* error. this usually implies a mismatch of some kind
692 * between server and client. if things are correct,
693 * we need to flush the rest of the packet if necessary. */
695 LOG(log_info, logtype_afpd,"afp_dsi: spurious command %d", cmd);
696 dsi_writeinit(dsi, dsi->data, DSI_DATASIZ);
700 pending_request(dsi);
702 fce_pending_events(obj);
706 afp_dsi_die(EXITERR_CLNT);