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 DSI *dsi = (DSI *)AFPobj->dsi;
148 if (reconnect_ipc(AFPobj) != 0) {
149 LOG(log_error, logtype_afpd, "ipc_reconnect_handler: failed IPC reconnect");
150 afp_dsi_close(AFPobj);
154 if (ipc_child_write(AFPobj->ipc_fd, IPC_GETSESSION, AFPobj->sinfo.clientid_len, AFPobj->sinfo.clientid) != 0) {
155 LOG(log_error, logtype_afpd, "ipc_reconnect_handler: failed IPC ID resend");
156 afp_dsi_close(AFPobj);
159 LOG(log_note, logtype_afpd, "ipc_reconnect_handler: IPC reconnect done");
162 /* SIGURG handler (primary reconnect) */
163 static void afp_dsi_transfer_session(int sig _U_)
167 DSI *dsi = (DSI *)AFPobj->dsi;
169 LOG(log_debug, logtype_afpd, "afp_dsi_transfer_session: got SIGURG, trying to receive session");
171 if (readt(AFPobj->ipc_fd, &dsiID, 2, 0, 2) != 2) {
172 LOG(log_error, logtype_afpd, "afp_dsi_transfer_session: couldn't receive DSI id, goodbye");
173 afp_dsi_close(AFPobj);
177 if ((socket = recv_fd(AFPobj->ipc_fd, 1)) == -1) {
178 LOG(log_error, logtype_afpd, "afp_dsi_transfer_session: couldn't receive session fd, goodbye");
179 afp_dsi_close(AFPobj);
183 LOG(log_debug, logtype_afpd, "afp_dsi_transfer_session: received socket fd: %i", socket);
185 dsi->proto_close(dsi);
186 dsi->socket = socket;
187 dsi->flags = DSI_RECONSOCKET;
189 dsi->eof = dsi->start = dsi->buffer;
191 dsi->header.dsi_requestID = dsiID;
192 dsi->header.dsi_command = DSIFUNC_CMD;
195 * The session transfer happens in the middle of FPDisconnect old session, thus we
196 * have to send the reply now.
198 if (!dsi_cmdreply(dsi, AFP_OK)) {
199 LOG(log_error, logtype_afpd, "dsi_cmdreply: %s", strerror(errno) );
200 afp_dsi_close(AFPobj);
204 LOG(log_note, logtype_afpd, "afp_dsi_transfer_session: succesfull primary reconnect");
206 * Now returning from this signal handler return to dsi_receive which should start
207 * reading/continuing from the connected socket that was passed via the parent from
208 * another session. The parent will terminate that session.
210 siglongjmp(recon_jmp, 1);
213 /* ------------------- */
214 static void afp_dsi_timedown(int sig _U_)
218 DSI *dsi = (DSI *)AFPobj->dsi;
219 dsi->flags |= DSI_DIE;
220 /* shutdown and don't reconnect. server going down in 5 minutes. */
221 setmessage("The server is going down for maintenance.");
222 if (dsi_attention(AFPobj->dsi, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
223 AFPATTN_MESG | AFPATTN_TIME(5)) < 0) {
224 DSI *dsi = (DSI *)AFPobj->dsi;
225 dsi->down_request = 1;
228 it.it_interval.tv_sec = 0;
229 it.it_interval.tv_usec = 0;
230 it.it_value.tv_sec = 300;
231 it.it_value.tv_usec = 0;
233 if ( setitimer( ITIMER_REAL, &it, NULL ) < 0 ) {
234 LOG(log_error, logtype_afpd, "afp_timedown: setitimer: %s", strerror(errno) );
235 afp_dsi_die(EXITERR_SYS);
237 memset(&sv, 0, sizeof(sv));
238 sv.sa_handler = afp_dsi_die;
239 sigemptyset( &sv.sa_mask );
240 sigaddset(&sv.sa_mask, SIGHUP);
241 sigaddset(&sv.sa_mask, SIGTERM);
242 sv.sa_flags = SA_RESTART;
243 if ( sigaction( SIGALRM, &sv, NULL ) < 0 ) {
244 LOG(log_error, logtype_afpd, "afp_timedown: sigaction: %s", strerror(errno) );
245 afp_dsi_die(EXITERR_SYS);
249 sv.sa_handler = SIG_IGN;
250 sigemptyset( &sv.sa_mask );
251 sv.sa_flags = SA_RESTART;
252 if ( sigaction( SIGUSR1, &sv, NULL ) < 0 ) {
253 LOG(log_error, logtype_afpd, "afp_timedown: sigaction SIGHUP: %s", strerror(errno) );
254 afp_dsi_die(EXITERR_SYS);
258 /* ---------------------------------
259 * SIGHUP reload configuration file
261 volatile int reload_request = 0;
263 static void afp_dsi_reload(int sig _U_)
268 /* ---------------------------------
269 * SIGINT: enable max_debug LOGging
271 static volatile sig_atomic_t debug_request = 0;
273 static void afp_dsi_debug(int sig _U_)
278 /* ---------------------- */
279 static void afp_dsi_getmesg (int sig _U_)
281 DSI *dsi = (DSI *)AFPobj->dsi;
283 dsi->msg_request = 1;
284 if (dsi_attention(AFPobj->dsi, AFPATTN_MESG | AFPATTN_TIME(5)) < 0)
285 dsi->msg_request = 2;
288 static void alarm_handler(int sig _U_)
291 DSI *dsi = (DSI *)AFPobj->dsi;
293 /* we have to restart the timer because some libraries may use alarm() */
294 setitimer(ITIMER_REAL, &dsi->timer, NULL);
296 /* we got some traffic from the client since the previous timer tick. */
297 if ((dsi->flags & DSI_DATA)) {
298 dsi->flags &= ~DSI_DATA;
303 LOG(log_maxdebug, logtype_afpd, "alarm: tickles: %u, flags: %s|%s|%s|%s|%s|%s|%s|%s|%s",
305 (dsi->flags & DSI_DATA) ? "DSI_DATA" : "-",
306 (dsi->flags & DSI_RUNNING) ? "DSI_RUNNING" : "-",
307 (dsi->flags & DSI_SLEEPING) ? "DSI_SLEEPING" : "-",
308 (dsi->flags & DSI_EXTSLEEP) ? "DSI_EXTSLEEP" : "-",
309 (dsi->flags & DSI_DISCONNECTED) ? "DSI_DISCONNECTED" : "-",
310 (dsi->flags & DSI_DIE) ? "DSI_DIE" : "-",
311 (dsi->flags & DSI_NOREPLY) ? "DSI_NOREPLY" : "-",
312 (dsi->flags & DSI_RECONSOCKET) ? "DSI_RECONSOCKET" : "-",
313 (dsi->flags & DSI_RECONINPROG) ? "DSI_RECONINPROG" : "-");
315 if (dsi->flags & DSI_SLEEPING) {
316 if (dsi->tickle > AFPobj->options.sleep) {
317 LOG(log_note, logtype_afpd, "afp_alarm: sleep time ended");
318 afp_dsi_die(EXITERR_CLNT);
323 if (dsi->flags & DSI_DISCONNECTED) {
324 if (geteuid() == 0) {
325 LOG(log_note, logtype_afpd, "afp_alarm: unauthenticated user, connection problem");
326 afp_dsi_die(EXITERR_CLNT);
328 if (dsi->tickle > AFPobj->options.disconnected) {
329 LOG(log_error, logtype_afpd, "afp_alarm: reconnect timer expired, goodbye");
330 afp_dsi_die(EXITERR_CLNT);
335 /* if we're in the midst of processing something, don't die. */
336 if (dsi->tickle >= AFPobj->options.timeout) {
337 LOG(log_error, logtype_afpd, "afp_alarm: child timed out, entering disconnected state");
338 if (dsi_disconnect(dsi) != 0)
339 afp_dsi_die(EXITERR_CLNT);
343 if ((err = pollvoltime(AFPobj)) == 0)
344 LOG(log_debug, logtype_afpd, "afp_alarm: sending DSI tickle");
345 err = dsi_tickle(AFPobj->dsi);
347 if (geteuid() == 0) {
348 LOG(log_note, logtype_afpd, "afp_alarm: unauthenticated user, connection problem");
349 afp_dsi_die(EXITERR_CLNT);
351 LOG(log_error, logtype_afpd, "afp_alarm: connection problem, entering disconnected state");
352 if (dsi_disconnect(dsi) != 0)
353 afp_dsi_die(EXITERR_CLNT);
358 if dsi->in_write is set attention, tickle (and close?) msg
359 aren't sent. We don't care about tickle
361 static void pending_request(DSI *dsi)
363 /* send pending attention */
365 /* read msg if any, it could be done in afp_getsrvrmesg */
366 if (dsi->msg_request) {
367 if (dsi->msg_request == 2) {
368 /* didn't send it in signal handler */
369 dsi_attention(AFPobj->dsi, AFPATTN_MESG | AFPATTN_TIME(5));
371 dsi->msg_request = 0;
374 if (dsi->down_request) {
375 dsi->down_request = 0;
376 dsi_attention(AFPobj->dsi, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
377 AFPATTN_MESG | AFPATTN_TIME(5));
381 void afp_over_dsi_sighandlers(AFPObj *obj)
383 DSI *dsi = (DSI *) obj->dsi;
384 struct sigaction action;
386 memset(&action, 0, sizeof(action));
387 sigfillset(&action.sa_mask);
388 action.sa_flags = SA_RESTART;
391 action.sa_handler = afp_dsi_reload;
392 if ( sigaction( SIGHUP, &action, NULL ) < 0 ) {
393 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
394 afp_dsi_die(EXITERR_SYS);
398 action.sa_handler = afp_dsi_transfer_session;
399 if ( sigaction( SIGURG, &action, NULL ) < 0 ) {
400 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
401 afp_dsi_die(EXITERR_SYS);
404 /* install SIGTERM */
405 action.sa_handler = afp_dsi_die;
406 if ( sigaction( SIGTERM, &action, NULL ) < 0 ) {
407 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
408 afp_dsi_die(EXITERR_SYS);
411 /* install SIGQUIT */
412 action.sa_handler = ipc_reconnect_handler;
413 if ( sigaction(SIGQUIT, &action, NULL ) < 0 ) {
414 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
415 afp_dsi_die(EXITERR_SYS);
418 /* SIGUSR2 - server message support */
419 action.sa_handler = afp_dsi_getmesg;
420 if ( sigaction( SIGUSR2, &action, NULL) < 0 ) {
421 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
422 afp_dsi_die(EXITERR_SYS);
425 /* SIGUSR1 - set down in 5 minutes */
426 action.sa_handler = afp_dsi_timedown;
427 action.sa_flags = SA_RESTART;
428 if ( sigaction( SIGUSR1, &action, NULL) < 0 ) {
429 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
430 afp_dsi_die(EXITERR_SYS);
433 /* SIGINT - enable max_debug LOGging to /tmp/afpd.PID.XXXXXX */
434 action.sa_handler = afp_dsi_debug;
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);
441 /* SIGALRM - tickle handler */
442 action.sa_handler = alarm_handler;
443 if ((sigaction(SIGALRM, &action, NULL) < 0) ||
444 (setitimer(ITIMER_REAL, &dsi->timer, NULL) < 0)) {
445 afp_dsi_die(EXITERR_SYS);
447 #endif /* DEBUGGING */
450 /* -------------------------------------------
451 afp over dsi. this never returns.
453 void afp_over_dsi(AFPObj *obj)
455 DSI *dsi = (DSI *) obj->dsi;
461 obj->exit = afp_dsi_die;
462 obj->reply = (int (*)()) dsi_cmdreply;
463 obj->attention = (int (*)(void *, AFPUserBytes)) dsi_attention;
466 afp_over_dsi_sighandlers(obj);
468 if (dircache_init(obj->options.dircachesize) != 0)
469 afp_dsi_die(EXITERR_SYS);
471 /* set TCP snd/rcv buf */
472 if (obj->options.tcp_rcvbuf) {
473 if (setsockopt(dsi->socket,
476 &obj->options.tcp_rcvbuf,
477 sizeof(obj->options.tcp_rcvbuf)) != 0) {
478 LOG(log_error, logtype_dsi, "afp_over_dsi: setsockopt(SO_RCVBUF): %s", strerror(errno));
481 if (obj->options.tcp_sndbuf) {
482 if (setsockopt(dsi->socket,
485 &obj->options.tcp_sndbuf,
486 sizeof(obj->options.tcp_sndbuf)) != 0) {
487 LOG(log_error, logtype_dsi, "afp_over_dsi: setsockopt(SO_SNDBUF): %s", strerror(errno));
491 /* set TCP_NODELAY */
493 setsockopt(dsi->socket, SOL_TCP, TCP_NODELAY, &flag, sizeof(flag));
495 /* get stuck here until the end */
497 if (sigsetjmp(recon_jmp, 1) != 0)
498 /* returning from SIGALARM handler for a primary reconnect */
501 /* Blocking read on the network socket */
502 cmd = dsi_stream_receive(dsi);
505 /* cmd == 0 is the error condition */
506 if (dsi->flags & DSI_RECONSOCKET) {
507 /* we just got a reconnect so we immediately try again to receive on the new fd */
508 dsi->flags &= ~DSI_RECONSOCKET;
512 /* the client sometimes logs out (afp_logout) but doesn't close the DSI session */
513 if (dsi->flags & DSI_AFP_LOGGED_OUT) {
514 LOG(log_note, logtype_afpd, "afp_over_dsi: client logged out, terminating DSI session");
520 /* got ECONNRESET in read from client => exit*/
521 if (dsi->flags & DSI_GOT_ECONNRESET) {
522 LOG(log_note, logtype_afpd, "afp_over_dsi: client connection reset");
528 if (dsi->flags & DSI_RECONINPROG) {
529 LOG(log_note, logtype_afpd, "afp_over_dsi: failed reconnect");
534 /* Some error on the client connection, enter disconnected state */
535 if (dsi_disconnect(dsi) != 0)
536 afp_dsi_die(EXITERR_CLNT);
538 while (dsi->flags & DSI_DISCONNECTED)
539 pause(); /* gets interrupted by SIGALARM or SIGURG tickle */
540 continue; /* continue receiving until disconnect timer expires
541 * or a primary reconnect succeeds */
544 if (!(dsi->flags & DSI_EXTSLEEP) && (dsi->flags & DSI_SLEEPING)) {
545 LOG(log_debug, logtype_afpd, "afp_over_dsi: got data, ending normal sleep");
546 dsi->flags &= ~DSI_SLEEPING;
550 if (reload_request) {
552 load_volumes(AFPobj);
555 /* The first SIGINT enables debugging, the next restores the config */
557 static int debugging = 0;
564 if (obj->options.logconfig)
565 setuplog(obj->options.logconfig, obj->options.logfile);
567 setuplog("default:note", NULL);
572 sprintf(logstr, "/tmp/afpd.%u.XXXXXX", getpid());
573 setuplog("default:maxdebug", logstr);
578 dsi->flags |= DSI_DATA;
584 LOG(log_debug, logtype_afpd, "DSI: close session request");
586 LOG(log_note, logtype_afpd, "done");
590 dsi->flags &= ~DSI_DATA; /* thats no data in the sense we use it in alarm_handler */
591 LOG(log_debug, logtype_afpd, "DSI: client tickle");
592 /* timer is not every 30 seconds anymore, so we don't get killed on the client side. */
593 if ((dsi->flags & DSI_DIE))
600 if ( flushfork( writtenfork ) < 0 ) {
601 LOG(log_error, logtype_afpd, "main flushfork: %s", strerror(errno) );
607 function = (u_char) dsi->commands[0];
609 /* AFP replay cache */
610 rc_idx = dsi->clientID % REPLAYCACHE_SIZE;
611 LOG(log_debug, logtype_dsi, "DSI request ID: %u", dsi->clientID);
613 if (replaycache[rc_idx].DSIreqID == dsi->clientID
614 && replaycache[rc_idx].AFPcommand == function) {
615 LOG(log_note, logtype_afpd, "AFP Replay Cache match: id: %u / cmd: %s",
616 dsi->clientID, AfpNum2name(function));
617 err = replaycache[rc_idx].result;
618 /* AFP replay cache end */
620 /* send off an afp command. in a couple cases, we take advantage
621 * of the fact that we're a stream-based protocol. */
622 if (afp_switch[function]) {
623 dsi->datalen = DSI_DATASIZ;
624 dsi->flags |= DSI_RUNNING;
626 LOG(log_debug, logtype_afpd, "<== Start AFP command: %s", AfpNum2name(function));
628 err = (*afp_switch[function])(obj,
629 dsi->commands, dsi->cmdlen,
630 (char *)&dsi->data, &dsi->datalen);
632 LOG(log_debug, logtype_afpd, "==> Finished AFP command: %s -> %s",
633 AfpNum2name(function), AfpErr2name(err));
635 dir_free_invalid_q();
637 dsi->flags &= ~DSI_RUNNING;
639 /* Add result to the AFP replay cache */
640 replaycache[rc_idx].DSIreqID = dsi->clientID;
641 replaycache[rc_idx].AFPcommand = function;
642 replaycache[rc_idx].result = err;
644 LOG(log_error, logtype_afpd, "bad function %X", function);
650 /* single shot toggle that gets set by dsi_readinit. */
651 if (dsi->flags & DSI_NOREPLY) {
652 dsi->flags &= ~DSI_NOREPLY;
654 } else if (!dsi_cmdreply(dsi, err)) {
655 LOG(log_error, logtype_afpd, "dsi_cmdreply(%d): %s", dsi->socket, strerror(errno) );
656 if (dsi_disconnect(dsi) != 0)
657 afp_dsi_die(EXITERR_CLNT);
661 case DSIFUNC_WRITE: /* FPWrite and FPAddIcon */
662 function = (u_char) dsi->commands[0];
663 if ( afp_switch[ function ] != NULL ) {
664 dsi->datalen = DSI_DATASIZ;
665 dsi->flags |= DSI_RUNNING;
667 LOG(log_debug, logtype_afpd, "<== Start AFP command: %s", AfpNum2name(function));
669 err = (*afp_switch[function])(obj,
670 dsi->commands, dsi->cmdlen,
671 (char *)&dsi->data, &dsi->datalen);
673 LOG(log_debug, logtype_afpd, "==> Finished AFP command: %s -> %s",
674 AfpNum2name(function), AfpErr2name(err));
676 dsi->flags &= ~DSI_RUNNING;
678 LOG(log_error, logtype_afpd, "(write) bad function %x", function);
683 if (!dsi_wrtreply(dsi, err)) {
684 LOG(log_error, logtype_afpd, "dsi_wrtreply: %s", strerror(errno) );
685 if (dsi_disconnect(dsi) != 0)
686 afp_dsi_die(EXITERR_CLNT);
690 case DSIFUNC_ATTN: /* attention replies */
693 /* error. this usually implies a mismatch of some kind
694 * between server and client. if things are correct,
695 * we need to flush the rest of the packet if necessary. */
697 LOG(log_info, logtype_afpd,"afp_dsi: spurious command %d", cmd);
698 dsi_writeinit(dsi, dsi->data, DSI_DATASIZ);
702 pending_request(dsi);
704 fce_pending_events(obj);
708 afp_dsi_die(EXITERR_CLNT);