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 /* SIGURG handler (primary reconnect) */
144 static void afp_dsi_transfer_session(int sig _U_)
148 DSI *dsi = (DSI *)AFPobj->dsi;
150 LOG(log_debug, logtype_afpd, "afp_dsi_transfer_session: got SIGURG, trying to receive session");
152 if (readt(AFPobj->ipc_fd, &dsiID, 2, 0, 2) != 2) {
153 LOG(log_error, logtype_afpd, "afp_dsi_transfer_session: couldn't receive DSI id, goodbye");
154 afp_dsi_close(AFPobj);
158 if ((socket = recv_fd(AFPobj->ipc_fd, 1)) == -1) {
159 LOG(log_error, logtype_afpd, "afp_dsi_transfer_session: couldn't receive session fd, goodbye");
160 afp_dsi_close(AFPobj);
164 LOG(log_debug, logtype_afpd, "afp_dsi_transfer_session: received socket fd: %i", socket);
166 dsi->proto_close(dsi);
167 dsi->socket = socket;
168 dsi->flags = DSI_RECONSOCKET;
170 dsi->eof = dsi->start = dsi->buffer;
172 dsi->header.dsi_requestID = dsiID;
173 dsi->header.dsi_command = DSIFUNC_CMD;
176 * The session transfer happens in the middle of FPDisconnect old session, thus we
177 * have to send the reply now.
179 if (!dsi_cmdreply(dsi, AFP_OK)) {
180 LOG(log_error, logtype_afpd, "dsi_cmdreply: %s", strerror(errno) );
181 afp_dsi_close(AFPobj);
185 LOG(log_note, logtype_afpd, "afp_dsi_transfer_session: succesfull primary reconnect");
187 * Now returning from this signal handler return to dsi_receive which should start
188 * reading/continuing from the connected socket that was passed via the parent from
189 * another session. The parent will terminate that session.
191 siglongjmp(recon_jmp, 1);
194 /* ------------------- */
195 static void afp_dsi_timedown(int sig _U_)
199 DSI *dsi = (DSI *)AFPobj->dsi;
200 dsi->flags |= DSI_DIE;
201 /* shutdown and don't reconnect. server going down in 5 minutes. */
202 setmessage("The server is going down for maintenance.");
203 if (dsi_attention(AFPobj->dsi, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
204 AFPATTN_MESG | AFPATTN_TIME(5)) < 0) {
205 DSI *dsi = (DSI *)AFPobj->dsi;
206 dsi->down_request = 1;
209 it.it_interval.tv_sec = 0;
210 it.it_interval.tv_usec = 0;
211 it.it_value.tv_sec = 300;
212 it.it_value.tv_usec = 0;
214 if ( setitimer( ITIMER_REAL, &it, NULL ) < 0 ) {
215 LOG(log_error, logtype_afpd, "afp_timedown: setitimer: %s", strerror(errno) );
216 afp_dsi_die(EXITERR_SYS);
218 memset(&sv, 0, sizeof(sv));
219 sv.sa_handler = afp_dsi_die;
220 sigemptyset( &sv.sa_mask );
221 sigaddset(&sv.sa_mask, SIGHUP);
222 sigaddset(&sv.sa_mask, SIGTERM);
223 sv.sa_flags = SA_RESTART;
224 if ( sigaction( SIGALRM, &sv, NULL ) < 0 ) {
225 LOG(log_error, logtype_afpd, "afp_timedown: sigaction: %s", strerror(errno) );
226 afp_dsi_die(EXITERR_SYS);
230 sv.sa_handler = SIG_IGN;
231 sigemptyset( &sv.sa_mask );
232 sv.sa_flags = SA_RESTART;
233 if ( sigaction( SIGUSR1, &sv, NULL ) < 0 ) {
234 LOG(log_error, logtype_afpd, "afp_timedown: sigaction SIGHUP: %s", strerror(errno) );
235 afp_dsi_die(EXITERR_SYS);
239 /* ---------------------------------
240 * SIGHUP reload configuration file
242 volatile int reload_request = 0;
244 static void afp_dsi_reload(int sig _U_)
249 /* ---------------------------------
250 * SIGINT: enable max_debug LOGging
252 static volatile sig_atomic_t debug_request = 0;
254 static void afp_dsi_debug(int sig _U_)
259 /* ---------------------- */
260 static void afp_dsi_getmesg (int sig _U_)
262 DSI *dsi = (DSI *)AFPobj->dsi;
264 dsi->msg_request = 1;
265 if (dsi_attention(AFPobj->dsi, AFPATTN_MESG | AFPATTN_TIME(5)) < 0)
266 dsi->msg_request = 2;
269 static void alarm_handler(int sig _U_)
272 DSI *dsi = (DSI *)AFPobj->dsi;
274 /* we have to restart the timer because some libraries may use alarm() */
275 setitimer(ITIMER_REAL, &dsi->timer, NULL);
277 /* we got some traffic from the client since the previous timer tick. */
278 if ((dsi->flags & DSI_DATA)) {
279 dsi->flags &= ~DSI_DATA;
284 LOG(log_maxdebug, logtype_afpd, "alarm: tickles: %u, flags: %s|%s|%s|%s|%s|%s|%s|%s|%s",
286 (dsi->flags & DSI_DATA) ? "DSI_DATA" : "-",
287 (dsi->flags & DSI_RUNNING) ? "DSI_RUNNING" : "-",
288 (dsi->flags & DSI_SLEEPING) ? "DSI_SLEEPING" : "-",
289 (dsi->flags & DSI_EXTSLEEP) ? "DSI_EXTSLEEP" : "-",
290 (dsi->flags & DSI_DISCONNECTED) ? "DSI_DISCONNECTED" : "-",
291 (dsi->flags & DSI_DIE) ? "DSI_DIE" : "-",
292 (dsi->flags & DSI_NOREPLY) ? "DSI_NOREPLY" : "-",
293 (dsi->flags & DSI_RECONSOCKET) ? "DSI_RECONSOCKET" : "-",
294 (dsi->flags & DSI_RECONINPROG) ? "DSI_RECONINPROG" : "-");
296 if (dsi->flags & DSI_SLEEPING) {
297 if (dsi->tickle > AFPobj->options.sleep) {
298 LOG(log_note, logtype_afpd, "afp_alarm: sleep time ended");
299 afp_dsi_die(EXITERR_CLNT);
304 if (dsi->flags & DSI_DISCONNECTED) {
305 if (geteuid() == 0) {
306 LOG(log_note, logtype_afpd, "afp_alarm: unauthenticated user, connection problem");
307 afp_dsi_die(EXITERR_CLNT);
309 if (dsi->tickle > AFPobj->options.disconnected) {
310 LOG(log_error, logtype_afpd, "afp_alarm: reconnect timer expired, goodbye");
311 afp_dsi_die(EXITERR_CLNT);
316 /* if we're in the midst of processing something, don't die. */
317 if (dsi->tickle >= AFPobj->options.timeout) {
318 LOG(log_error, logtype_afpd, "afp_alarm: child timed out, entering disconnected state");
319 if (dsi_disconnect(dsi) != 0)
320 afp_dsi_die(EXITERR_CLNT);
324 if ((err = pollvoltime(AFPobj)) == 0)
325 LOG(log_debug, logtype_afpd, "afp_alarm: sending DSI tickle");
326 err = dsi_tickle(AFPobj->dsi);
328 if (geteuid() == 0) {
329 LOG(log_note, logtype_afpd, "afp_alarm: unauthenticated user, connection problem");
330 afp_dsi_die(EXITERR_CLNT);
332 LOG(log_error, logtype_afpd, "afp_alarm: connection problem, entering disconnected state");
333 if (dsi_disconnect(dsi) != 0)
334 afp_dsi_die(EXITERR_CLNT);
339 if dsi->in_write is set attention, tickle (and close?) msg
340 aren't sent. We don't care about tickle
342 static void pending_request(DSI *dsi)
344 /* send pending attention */
346 /* read msg if any, it could be done in afp_getsrvrmesg */
347 if (dsi->msg_request) {
348 if (dsi->msg_request == 2) {
349 /* didn't send it in signal handler */
350 dsi_attention(AFPobj->dsi, AFPATTN_MESG | AFPATTN_TIME(5));
352 dsi->msg_request = 0;
355 if (dsi->down_request) {
356 dsi->down_request = 0;
357 dsi_attention(AFPobj->dsi, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
358 AFPATTN_MESG | AFPATTN_TIME(5));
362 void afp_over_dsi_sighandlers(AFPObj *obj)
364 DSI *dsi = (DSI *) obj->dsi;
365 struct sigaction action;
367 memset(&action, 0, sizeof(action));
368 sigfillset(&action.sa_mask);
369 action.sa_flags = SA_RESTART;
372 action.sa_handler = afp_dsi_reload;
373 if ( sigaction( SIGHUP, &action, NULL ) < 0 ) {
374 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
375 afp_dsi_die(EXITERR_SYS);
379 action.sa_handler = afp_dsi_transfer_session;
380 if ( sigaction( SIGURG, &action, NULL ) < 0 ) {
381 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
382 afp_dsi_die(EXITERR_SYS);
385 /* install SIGTERM */
386 action.sa_handler = afp_dsi_die;
387 if ( sigaction( SIGTERM, &action, NULL ) < 0 ) {
388 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
389 afp_dsi_die(EXITERR_SYS);
392 /* install SIGQUIT */
393 action.sa_handler = afp_dsi_die;
394 if ( sigaction(SIGQUIT, &action, NULL ) < 0 ) {
395 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
396 afp_dsi_die(EXITERR_SYS);
399 /* SIGUSR2 - server message support */
400 action.sa_handler = afp_dsi_getmesg;
401 if ( sigaction( SIGUSR2, &action, NULL) < 0 ) {
402 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
403 afp_dsi_die(EXITERR_SYS);
406 /* SIGUSR1 - set down in 5 minutes */
407 action.sa_handler = afp_dsi_timedown;
408 action.sa_flags = SA_RESTART;
409 if ( sigaction( SIGUSR1, &action, NULL) < 0 ) {
410 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
411 afp_dsi_die(EXITERR_SYS);
414 /* SIGINT - enable max_debug LOGging to /tmp/afpd.PID.XXXXXX */
415 action.sa_handler = afp_dsi_debug;
416 if ( sigaction( SIGINT, &action, NULL) < 0 ) {
417 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
418 afp_dsi_die(EXITERR_SYS);
422 /* SIGALRM - tickle handler */
423 action.sa_handler = alarm_handler;
424 if ((sigaction(SIGALRM, &action, NULL) < 0) ||
425 (setitimer(ITIMER_REAL, &dsi->timer, NULL) < 0)) {
426 afp_dsi_die(EXITERR_SYS);
428 #endif /* DEBUGGING */
431 /* -------------------------------------------
432 afp over dsi. this never returns.
434 void afp_over_dsi(AFPObj *obj)
436 DSI *dsi = (DSI *) obj->dsi;
442 obj->exit = afp_dsi_die;
443 obj->reply = (int (*)()) dsi_cmdreply;
444 obj->attention = (int (*)(void *, AFPUserBytes)) dsi_attention;
447 afp_over_dsi_sighandlers(obj);
449 if (dircache_init(obj->options.dircachesize) != 0)
450 afp_dsi_die(EXITERR_SYS);
452 /* set TCP snd/rcv buf */
453 if (obj->options.tcp_rcvbuf) {
454 if (setsockopt(dsi->socket,
457 &obj->options.tcp_rcvbuf,
458 sizeof(obj->options.tcp_rcvbuf)) != 0) {
459 LOG(log_error, logtype_dsi, "afp_over_dsi: setsockopt(SO_RCVBUF): %s", strerror(errno));
462 if (obj->options.tcp_sndbuf) {
463 if (setsockopt(dsi->socket,
466 &obj->options.tcp_sndbuf,
467 sizeof(obj->options.tcp_sndbuf)) != 0) {
468 LOG(log_error, logtype_dsi, "afp_over_dsi: setsockopt(SO_SNDBUF): %s", strerror(errno));
472 /* set TCP_NODELAY */
474 setsockopt(dsi->socket, SOL_TCP, TCP_NODELAY, &flag, sizeof(flag));
476 ipc_child_state(obj, DSI_RUNNING);
478 /* get stuck here until the end */
480 if (sigsetjmp(recon_jmp, 1) != 0)
481 /* returning from SIGALARM handler for a primary reconnect */
484 /* Blocking read on the network socket */
485 cmd = dsi_stream_receive(dsi);
488 /* cmd == 0 is the error condition */
489 if (dsi->flags & DSI_RECONSOCKET) {
490 /* we just got a reconnect so we immediately try again to receive on the new fd */
491 dsi->flags &= ~DSI_RECONSOCKET;
495 /* the client sometimes logs out (afp_logout) but doesn't close the DSI session */
496 if (dsi->flags & DSI_AFP_LOGGED_OUT) {
497 LOG(log_note, logtype_afpd, "afp_over_dsi: client logged out, terminating DSI session");
502 if (dsi->flags & DSI_RECONINPROG) {
503 LOG(log_note, logtype_afpd, "afp_over_dsi: failed reconnect");
508 /* Some error on the client connection, enter disconnected state */
509 if (dsi_disconnect(dsi) != 0)
510 afp_dsi_die(EXITERR_CLNT);
512 ipc_child_state(obj, DSI_DISCONNECTED);
514 while (dsi->flags & DSI_DISCONNECTED)
515 pause(); /* gets interrupted by SIGALARM or SIGURG tickle */
516 ipc_child_state(obj, DSI_RUNNING);
517 continue; /* continue receiving until disconnect timer expires
518 * or a primary reconnect succeeds */
521 if (!(dsi->flags & DSI_EXTSLEEP) && (dsi->flags & DSI_SLEEPING)) {
522 LOG(log_debug, logtype_afpd, "afp_over_dsi: got data, ending normal sleep");
523 dsi->flags &= ~DSI_SLEEPING;
525 ipc_child_state(obj, DSI_RUNNING);
528 if (reload_request) {
530 load_volumes(AFPobj);
533 /* The first SIGINT enables debugging, the next restores the config */
535 static int debugging = 0;
542 if (obj->options.logconfig)
543 setuplog(obj->options.logconfig, obj->options.logfile);
545 setuplog("default:note", NULL);
550 sprintf(logstr, "/tmp/afpd.%u.XXXXXX", getpid());
551 setuplog("default:maxdebug", logstr);
556 dsi->flags |= DSI_DATA;
562 LOG(log_debug, logtype_afpd, "DSI: close session request");
564 LOG(log_note, logtype_afpd, "done");
568 dsi->flags &= ~DSI_DATA; /* thats no data in the sense we use it in alarm_handler */
569 LOG(log_debug, logtype_afpd, "DSI: client tickle");
570 /* timer is not every 30 seconds anymore, so we don't get killed on the client side. */
571 if ((dsi->flags & DSI_DIE))
578 if ( flushfork( writtenfork ) < 0 ) {
579 LOG(log_error, logtype_afpd, "main flushfork: %s", strerror(errno) );
585 function = (u_char) dsi->commands[0];
587 /* AFP replay cache */
588 rc_idx = dsi->clientID % REPLAYCACHE_SIZE;
589 LOG(log_debug, logtype_dsi, "DSI request ID: %u", dsi->clientID);
591 if (replaycache[rc_idx].DSIreqID == dsi->clientID
592 && replaycache[rc_idx].AFPcommand == function) {
593 LOG(log_note, logtype_afpd, "AFP Replay Cache match: id: %u / cmd: %s",
594 dsi->clientID, AfpNum2name(function));
595 err = replaycache[rc_idx].result;
596 /* AFP replay cache end */
598 /* send off an afp command. in a couple cases, we take advantage
599 * of the fact that we're a stream-based protocol. */
600 if (afp_switch[function]) {
601 dsi->datalen = DSI_DATASIZ;
602 dsi->flags |= DSI_RUNNING;
604 LOG(log_debug, logtype_afpd, "<== Start AFP command: %s", AfpNum2name(function));
606 AFP_AFPFUNC_START(function, (char *)AfpNum2name(function));
607 err = (*afp_switch[function])(obj,
608 (char *)dsi->commands, dsi->cmdlen,
609 (char *)&dsi->data, &dsi->datalen);
611 AFP_AFPFUNC_DONE(function, (char *)AfpNum2name(function));
612 LOG(log_debug, logtype_afpd, "==> Finished AFP command: %s -> %s",
613 AfpNum2name(function), AfpErr2name(err));
615 dir_free_invalid_q();
617 dsi->flags &= ~DSI_RUNNING;
619 /* Add result to the AFP replay cache */
620 replaycache[rc_idx].DSIreqID = dsi->clientID;
621 replaycache[rc_idx].AFPcommand = function;
622 replaycache[rc_idx].result = err;
624 LOG(log_error, logtype_afpd, "bad function %X", function);
630 /* single shot toggle that gets set by dsi_readinit. */
631 if (dsi->flags & DSI_NOREPLY) {
632 dsi->flags &= ~DSI_NOREPLY;
634 } else if (!dsi_cmdreply(dsi, err)) {
635 LOG(log_error, logtype_afpd, "dsi_cmdreply(%d): %s", dsi->socket, strerror(errno) );
636 if (dsi_disconnect(dsi) != 0)
637 afp_dsi_die(EXITERR_CLNT);
641 case DSIFUNC_WRITE: /* FPWrite and FPAddIcon */
642 function = (u_char) dsi->commands[0];
643 if ( afp_switch[ function ] != NULL ) {
644 dsi->datalen = DSI_DATASIZ;
645 dsi->flags |= DSI_RUNNING;
647 LOG(log_debug, logtype_afpd, "<== Start AFP command: %s", AfpNum2name(function));
649 AFP_AFPFUNC_START(function, (char *)AfpNum2name(function));
651 err = (*afp_switch[function])(obj,
652 (char *)dsi->commands, dsi->cmdlen,
653 (char *)&dsi->data, &dsi->datalen);
655 AFP_AFPFUNC_DONE(function, (char *)AfpNum2name(function));
657 LOG(log_debug, logtype_afpd, "==> Finished AFP command: %s -> %s",
658 AfpNum2name(function), AfpErr2name(err));
660 dsi->flags &= ~DSI_RUNNING;
662 LOG(log_error, logtype_afpd, "(write) bad function %x", function);
667 if (!dsi_wrtreply(dsi, err)) {
668 LOG(log_error, logtype_afpd, "dsi_wrtreply: %s", strerror(errno) );
669 if (dsi_disconnect(dsi) != 0)
670 afp_dsi_die(EXITERR_CLNT);
674 case DSIFUNC_ATTN: /* attention replies */
677 /* error. this usually implies a mismatch of some kind
678 * between server and client. if things are correct,
679 * we need to flush the rest of the packet if necessary. */
681 LOG(log_info, logtype_afpd,"afp_dsi: spurious command %d", cmd);
682 dsi_writeinit(dsi, dsi->data, DSI_DATASIZ);
686 pending_request(dsi);
688 fce_pending_events(obj);
692 afp_dsi_die(EXITERR_CLNT);