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>
47 #include "spotlight.h"
50 #define SOL_TCP IPPROTO_TCP
54 * We generally pass this from afp_over_dsi to all afp_* funcs, so it should already be
55 * available everywhere. Unfortunately some funcs (eg acltoownermode) need acces to it
56 * but are deeply nested in the function chain with the caller already without acces to it.
57 * Changing this would require adding a reference to the caller which itself might be
58 * called in many places (eg acltoownermode is called from accessmode).
59 * The only sane way out is providing a copy of it here:
61 AFPObj *AFPobj = NULL;
72 * - indexed just by taking DSIreqID mod REPLAYCACHE_SIZE
74 static rc_elem_t replaycache[REPLAYCACHE_SIZE];
76 static sigjmp_buf recon_jmp;
77 static void afp_dsi_close(AFPObj *obj)
85 /* we may have been called from a signal handler caught when afpd was running
86 * as uid 0, that's the wrong user for volume's prexec_close scripts if any,
87 * restore our login user
89 if (geteuid() != obj->uid) {
90 if (seteuid( obj->uid ) < 0) {
91 LOG(log_error, logtype_afpd, "can't seteuid(%u) back %s: uid: %u, euid: %u",
92 obj->uid, strerror(errno), getuid(), geteuid());
99 /* Block sigs, PAM/systemd/whoever might send us a SIG??? in (*obj->logout)() -> pam_close_session() */
101 pthread_sigmask(SIG_BLOCK, &sigs, NULL);
105 LOG(log_note, logtype_afpd, "AFP statistics: %.2f KB read, %.2f KB written",
106 dsi->read_count/1024.0, dsi->write_count/1024.0);
112 /* -------------------------------
114 * a little bit of code duplication.
116 static void afp_dsi_die(int sig)
118 DSI *dsi = (DSI *)AFPobj->dsi;
120 if (dsi->flags & DSI_RECONINPROG) {
121 /* Primary reconnect succeeded, got SIGTERM from afpd parent */
122 dsi->flags &= ~DSI_RECONINPROG;
123 return; /* this returns to afp_disconnect */
126 if (dsi->flags & DSI_DISCONNECTED) {
127 LOG(log_note, logtype_afpd, "Disconnected session terminating");
131 dsi_attention(AFPobj->dsi, AFPATTN_SHUTDOWN);
132 afp_dsi_close(AFPobj);
133 if (sig) /* if no signal, assume dieing because logins are disabled &
134 don't log it (maintenance mode)*/
135 LOG(log_info, logtype_afpd, "Connection terminated");
136 if (sig == SIGTERM || sig == SIGALRM) {
144 /* SIGQUIT handler */
145 static void ipc_reconnect_handler(int sig _U_)
147 DSI *dsi = (DSI *)AFPobj->dsi;
149 if (reconnect_ipc(AFPobj) != 0) {
150 LOG(log_error, logtype_afpd, "ipc_reconnect_handler: failed IPC reconnect");
151 afp_dsi_close(AFPobj);
155 if (ipc_child_write(AFPobj->ipc_fd, IPC_GETSESSION, AFPobj->sinfo.clientid_len, AFPobj->sinfo.clientid) != 0) {
156 LOG(log_error, logtype_afpd, "ipc_reconnect_handler: failed IPC ID resend");
157 afp_dsi_close(AFPobj);
160 LOG(log_note, logtype_afpd, "ipc_reconnect_handler: IPC reconnect done");
163 /* SIGURG handler (primary reconnect) */
164 static void afp_dsi_transfer_session(int sig _U_)
168 DSI *dsi = (DSI *)AFPobj->dsi;
170 LOG(log_debug, logtype_afpd, "afp_dsi_transfer_session: got SIGURG, trying to receive session");
172 if (readt(AFPobj->ipc_fd, &dsiID, 2, 0, 2) != 2) {
173 LOG(log_error, logtype_afpd, "afp_dsi_transfer_session: couldn't receive DSI id, goodbye");
174 afp_dsi_close(AFPobj);
178 if ((socket = recv_fd(AFPobj->ipc_fd, 1)) == -1) {
179 LOG(log_error, logtype_afpd, "afp_dsi_transfer_session: couldn't receive session fd, goodbye");
180 afp_dsi_close(AFPobj);
184 LOG(log_debug, logtype_afpd, "afp_dsi_transfer_session: received socket fd: %i", socket);
186 dsi->proto_close(dsi);
187 dsi->socket = socket;
188 dsi->flags = DSI_RECONSOCKET;
190 dsi->eof = dsi->start = dsi->buffer;
192 dsi->header.dsi_requestID = dsiID;
193 dsi->header.dsi_command = DSIFUNC_CMD;
196 * The session transfer happens in the middle of FPDisconnect old session, thus we
197 * have to send the reply now.
199 if (!dsi_cmdreply(dsi, AFP_OK)) {
200 LOG(log_error, logtype_afpd, "dsi_cmdreply: %s", strerror(errno) );
201 afp_dsi_close(AFPobj);
205 LOG(log_note, logtype_afpd, "afp_dsi_transfer_session: succesfull primary reconnect");
207 * Now returning from this signal handler return to dsi_receive which should start
208 * reading/continuing from the connected socket that was passed via the parent from
209 * another session. The parent will terminate that session.
211 siglongjmp(recon_jmp, 1);
214 /* ------------------- */
215 static void afp_dsi_timedown(int sig _U_)
219 DSI *dsi = (DSI *)AFPobj->dsi;
220 dsi->flags |= DSI_DIE;
221 /* shutdown and don't reconnect. server going down in 5 minutes. */
222 setmessage("The server is going down for maintenance.");
223 if (dsi_attention(AFPobj->dsi, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
224 AFPATTN_MESG | AFPATTN_TIME(5)) < 0) {
225 DSI *dsi = (DSI *)AFPobj->dsi;
226 dsi->down_request = 1;
229 it.it_interval.tv_sec = 0;
230 it.it_interval.tv_usec = 0;
231 it.it_value.tv_sec = 300;
232 it.it_value.tv_usec = 0;
234 if ( setitimer( ITIMER_REAL, &it, NULL ) < 0 ) {
235 LOG(log_error, logtype_afpd, "afp_timedown: setitimer: %s", strerror(errno) );
236 afp_dsi_die(EXITERR_SYS);
238 memset(&sv, 0, sizeof(sv));
239 sv.sa_handler = afp_dsi_die;
240 sigemptyset( &sv.sa_mask );
241 sigaddset(&sv.sa_mask, SIGHUP);
242 sigaddset(&sv.sa_mask, SIGTERM);
243 sv.sa_flags = SA_RESTART;
244 if ( sigaction( SIGALRM, &sv, NULL ) < 0 ) {
245 LOG(log_error, logtype_afpd, "afp_timedown: sigaction: %s", strerror(errno) );
246 afp_dsi_die(EXITERR_SYS);
250 sv.sa_handler = SIG_IGN;
251 sigemptyset( &sv.sa_mask );
252 sv.sa_flags = SA_RESTART;
253 if ( sigaction( SIGUSR1, &sv, NULL ) < 0 ) {
254 LOG(log_error, logtype_afpd, "afp_timedown: sigaction SIGHUP: %s", strerror(errno) );
255 afp_dsi_die(EXITERR_SYS);
259 /* ---------------------------------
260 * SIGHUP reload configuration file
262 volatile int reload_request = 0;
264 static void afp_dsi_reload(int sig _U_)
269 /* ---------------------------------
270 * SIGINT: enable max_debug LOGging
272 static volatile sig_atomic_t debug_request = 0;
274 static void afp_dsi_debug(int sig _U_)
279 /* ---------------------- */
280 static void afp_dsi_getmesg (int sig _U_)
282 DSI *dsi = (DSI *)AFPobj->dsi;
284 dsi->msg_request = 1;
285 if (dsi_attention(AFPobj->dsi, AFPATTN_MESG | AFPATTN_TIME(5)) < 0)
286 dsi->msg_request = 2;
289 static void alarm_handler(int sig _U_)
292 DSI *dsi = (DSI *)AFPobj->dsi;
294 /* we have to restart the timer because some libraries may use alarm() */
295 setitimer(ITIMER_REAL, &dsi->timer, NULL);
297 /* we got some traffic from the client since the previous timer tick. */
298 if ((dsi->flags & DSI_DATA)) {
299 dsi->flags &= ~DSI_DATA;
304 LOG(log_maxdebug, logtype_afpd, "alarm: tickles: %u, flags: %s|%s|%s|%s|%s|%s|%s|%s|%s",
306 (dsi->flags & DSI_DATA) ? "DSI_DATA" : "-",
307 (dsi->flags & DSI_RUNNING) ? "DSI_RUNNING" : "-",
308 (dsi->flags & DSI_SLEEPING) ? "DSI_SLEEPING" : "-",
309 (dsi->flags & DSI_EXTSLEEP) ? "DSI_EXTSLEEP" : "-",
310 (dsi->flags & DSI_DISCONNECTED) ? "DSI_DISCONNECTED" : "-",
311 (dsi->flags & DSI_DIE) ? "DSI_DIE" : "-",
312 (dsi->flags & DSI_NOREPLY) ? "DSI_NOREPLY" : "-",
313 (dsi->flags & DSI_RECONSOCKET) ? "DSI_RECONSOCKET" : "-",
314 (dsi->flags & DSI_RECONINPROG) ? "DSI_RECONINPROG" : "-");
316 if (dsi->flags & DSI_SLEEPING) {
317 if (dsi->tickle > AFPobj->options.sleep) {
318 LOG(log_note, logtype_afpd, "afp_alarm: sleep time ended");
319 afp_dsi_die(EXITERR_CLNT);
324 if (dsi->flags & DSI_DISCONNECTED) {
325 if (geteuid() == 0) {
326 LOG(log_note, logtype_afpd, "afp_alarm: unauthenticated user, connection problem");
327 afp_dsi_die(EXITERR_CLNT);
329 if (dsi->tickle > AFPobj->options.disconnected) {
330 LOG(log_error, logtype_afpd, "afp_alarm: reconnect timer expired, goodbye");
331 afp_dsi_die(EXITERR_CLNT);
336 /* if we're in the midst of processing something, don't die. */
337 if (dsi->tickle >= AFPobj->options.timeout) {
338 LOG(log_error, logtype_afpd, "afp_alarm: child timed out, entering disconnected state");
339 if (dsi_disconnect(dsi) != 0)
340 afp_dsi_die(EXITERR_CLNT);
344 if ((err = pollvoltime(AFPobj)) == 0)
345 LOG(log_debug, logtype_afpd, "afp_alarm: sending DSI tickle");
346 err = dsi_tickle(AFPobj->dsi);
348 if (geteuid() == 0) {
349 LOG(log_note, logtype_afpd, "afp_alarm: unauthenticated user, connection problem");
350 afp_dsi_die(EXITERR_CLNT);
352 LOG(log_error, logtype_afpd, "afp_alarm: connection problem, entering disconnected state");
353 if (dsi_disconnect(dsi) != 0)
354 afp_dsi_die(EXITERR_CLNT);
359 if dsi->in_write is set attention, tickle (and close?) msg
360 aren't sent. We don't care about tickle
362 static void pending_request(DSI *dsi)
364 /* send pending attention */
366 /* read msg if any, it could be done in afp_getsrvrmesg */
367 if (dsi->msg_request) {
368 if (dsi->msg_request == 2) {
369 /* didn't send it in signal handler */
370 dsi_attention(AFPobj->dsi, AFPATTN_MESG | AFPATTN_TIME(5));
372 dsi->msg_request = 0;
375 if (dsi->down_request) {
376 dsi->down_request = 0;
377 dsi_attention(AFPobj->dsi, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
378 AFPATTN_MESG | AFPATTN_TIME(5));
382 void afp_over_dsi_sighandlers(AFPObj *obj)
384 DSI *dsi = (DSI *) obj->dsi;
385 struct sigaction action;
387 memset(&action, 0, sizeof(action));
388 sigfillset(&action.sa_mask);
389 action.sa_flags = SA_RESTART;
392 action.sa_handler = afp_dsi_reload;
393 if ( sigaction( SIGHUP, &action, NULL ) < 0 ) {
394 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
395 afp_dsi_die(EXITERR_SYS);
399 action.sa_handler = afp_dsi_transfer_session;
400 if ( sigaction( SIGURG, &action, NULL ) < 0 ) {
401 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
402 afp_dsi_die(EXITERR_SYS);
405 /* install SIGTERM */
406 action.sa_handler = afp_dsi_die;
407 if ( sigaction( SIGTERM, &action, NULL ) < 0 ) {
408 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
409 afp_dsi_die(EXITERR_SYS);
412 /* install SIGQUIT */
413 action.sa_handler = ipc_reconnect_handler;
414 if ( sigaction(SIGQUIT, &action, NULL ) < 0 ) {
415 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
416 afp_dsi_die(EXITERR_SYS);
419 /* SIGUSR2 - server message support */
420 action.sa_handler = afp_dsi_getmesg;
421 if ( sigaction( SIGUSR2, &action, NULL) < 0 ) {
422 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
423 afp_dsi_die(EXITERR_SYS);
426 /* SIGUSR1 - set down in 5 minutes */
427 action.sa_handler = afp_dsi_timedown;
428 action.sa_flags = SA_RESTART;
429 if ( sigaction( SIGUSR1, &action, NULL) < 0 ) {
430 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
431 afp_dsi_die(EXITERR_SYS);
434 /* SIGINT - enable max_debug LOGging to /tmp/afpd.PID.XXXXXX */
435 action.sa_handler = afp_dsi_debug;
436 if ( sigaction( SIGINT, &action, NULL) < 0 ) {
437 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
438 afp_dsi_die(EXITERR_SYS);
442 /* SIGALRM - tickle handler */
443 action.sa_handler = alarm_handler;
444 if ((sigaction(SIGALRM, &action, NULL) < 0) ||
445 (setitimer(ITIMER_REAL, &dsi->timer, NULL) < 0)) {
446 afp_dsi_die(EXITERR_SYS);
448 #endif /* DEBUGGING */
451 /* -------------------------------------------
452 afp over dsi. this never returns.
454 void afp_over_dsi(AFPObj *obj)
456 DSI *dsi = (DSI *) obj->dsi;
462 obj->exit = afp_dsi_die;
463 obj->reply = (int (*)()) dsi_cmdreply;
464 obj->attention = (int (*)(void *, AFPUserBytes)) dsi_attention;
467 afp_over_dsi_sighandlers(obj);
469 if (dircache_init(obj->options.dircachesize) != 0)
470 afp_dsi_die(EXITERR_SYS);
472 /* set TCP snd/rcv buf */
473 if (obj->options.tcp_rcvbuf) {
474 if (setsockopt(dsi->socket,
477 &obj->options.tcp_rcvbuf,
478 sizeof(obj->options.tcp_rcvbuf)) != 0) {
479 LOG(log_error, logtype_dsi, "afp_over_dsi: setsockopt(SO_RCVBUF): %s", strerror(errno));
482 if (obj->options.tcp_sndbuf) {
483 if (setsockopt(dsi->socket,
486 &obj->options.tcp_sndbuf,
487 sizeof(obj->options.tcp_sndbuf)) != 0) {
488 LOG(log_error, logtype_dsi, "afp_over_dsi: setsockopt(SO_SNDBUF): %s", strerror(errno));
492 /* set TCP_NODELAY */
494 setsockopt(dsi->socket, SOL_TCP, TCP_NODELAY, &flag, sizeof(flag));
496 /* Initialize Spotlight */
497 if (obj->options.flags & OPTION_SPOTLIGHT)
498 sl_mod_load(_PATH_AFPDUAMPATH "mod_spotlight.so");
500 /* get stuck here until the end */
502 if (sigsetjmp(recon_jmp, 1) != 0)
503 /* returning from SIGALARM handler for a primary reconnect */
506 /* Blocking read on the network socket */
507 cmd = dsi_stream_receive(dsi);
510 /* cmd == 0 is the error condition */
511 if (dsi->flags & DSI_RECONSOCKET) {
512 /* we just got a reconnect so we immediately try again to receive on the new fd */
513 dsi->flags &= ~DSI_RECONSOCKET;
517 /* the client sometimes logs out (afp_logout) but doesn't close the DSI session */
518 if (dsi->flags & DSI_AFP_LOGGED_OUT) {
519 LOG(log_note, logtype_afpd, "afp_over_dsi: client logged out, terminating DSI session");
525 /* got ECONNRESET in read from client => exit*/
526 if (dsi->flags & DSI_GOT_ECONNRESET) {
527 LOG(log_note, logtype_afpd, "afp_over_dsi: client connection reset");
533 if (dsi->flags & DSI_RECONINPROG) {
534 LOG(log_note, logtype_afpd, "afp_over_dsi: failed reconnect");
539 /* Some error on the client connection, enter disconnected state */
540 if (dsi_disconnect(dsi) != 0)
541 afp_dsi_die(EXITERR_CLNT);
543 while (dsi->flags & DSI_DISCONNECTED)
544 pause(); /* gets interrupted by SIGALARM or SIGURG tickle */
545 continue; /* continue receiving until disconnect timer expires
546 * or a primary reconnect succeeds */
549 if (!(dsi->flags & DSI_EXTSLEEP) && (dsi->flags & DSI_SLEEPING)) {
550 LOG(log_debug, logtype_afpd, "afp_over_dsi: got data, ending normal sleep");
551 dsi->flags &= ~DSI_SLEEPING;
555 if (reload_request) {
557 load_volumes(AFPobj);
560 /* The first SIGINT enables debugging, the next restores the config */
562 static int debugging = 0;
569 if (obj->options.logconfig)
570 setuplog(obj->options.logconfig, obj->options.logfile);
572 setuplog("default:note", NULL);
577 sprintf(logstr, "/tmp/afpd.%u.XXXXXX", getpid());
578 setuplog("default:maxdebug", logstr);
583 dsi->flags |= DSI_DATA;
589 LOG(log_debug, logtype_afpd, "DSI: close session request");
591 LOG(log_note, logtype_afpd, "done");
595 dsi->flags &= ~DSI_DATA; /* thats no data in the sense we use it in alarm_handler */
596 LOG(log_debug, logtype_afpd, "DSI: client tickle");
597 /* timer is not every 30 seconds anymore, so we don't get killed on the client side. */
598 if ((dsi->flags & DSI_DIE))
605 if ( flushfork( writtenfork ) < 0 ) {
606 LOG(log_error, logtype_afpd, "main flushfork: %s", strerror(errno) );
612 function = (u_char) dsi->commands[0];
614 /* AFP replay cache */
615 rc_idx = dsi->clientID % REPLAYCACHE_SIZE;
616 LOG(log_debug, logtype_dsi, "DSI request ID: %u", dsi->clientID);
618 if (replaycache[rc_idx].DSIreqID == dsi->clientID
619 && replaycache[rc_idx].AFPcommand == function) {
620 LOG(log_note, logtype_afpd, "AFP Replay Cache match: id: %u / cmd: %s",
621 dsi->clientID, AfpNum2name(function));
622 err = replaycache[rc_idx].result;
623 /* AFP replay cache end */
625 /* send off an afp command. in a couple cases, we take advantage
626 * of the fact that we're a stream-based protocol. */
627 if (afp_switch[function]) {
628 dsi->datalen = DSI_DATASIZ;
629 dsi->flags |= DSI_RUNNING;
631 LOG(log_debug, logtype_afpd, "<== Start AFP command: %s", AfpNum2name(function));
633 err = (*afp_switch[function])(obj,
634 dsi->commands, dsi->cmdlen,
635 (char *)&dsi->data, &dsi->datalen);
637 LOG(log_debug, logtype_afpd, "==> Finished AFP command: %s -> %s",
638 AfpNum2name(function), AfpErr2name(err));
640 dir_free_invalid_q();
642 dsi->flags &= ~DSI_RUNNING;
644 /* Add result to the AFP replay cache */
645 replaycache[rc_idx].DSIreqID = dsi->clientID;
646 replaycache[rc_idx].AFPcommand = function;
647 replaycache[rc_idx].result = err;
649 LOG(log_error, logtype_afpd, "bad function %X", function);
655 /* single shot toggle that gets set by dsi_readinit. */
656 if (dsi->flags & DSI_NOREPLY) {
657 dsi->flags &= ~DSI_NOREPLY;
659 } else if (!dsi_cmdreply(dsi, err)) {
660 LOG(log_error, logtype_afpd, "dsi_cmdreply(%d): %s", dsi->socket, strerror(errno) );
661 if (dsi_disconnect(dsi) != 0)
662 afp_dsi_die(EXITERR_CLNT);
666 case DSIFUNC_WRITE: /* FPWrite and FPAddIcon */
667 function = (u_char) dsi->commands[0];
668 if ( afp_switch[ function ] != NULL ) {
669 dsi->datalen = DSI_DATASIZ;
670 dsi->flags |= DSI_RUNNING;
672 LOG(log_debug, logtype_afpd, "<== Start AFP command: %s", AfpNum2name(function));
674 err = (*afp_switch[function])(obj,
675 dsi->commands, dsi->cmdlen,
676 (char *)&dsi->data, &dsi->datalen);
678 LOG(log_debug, logtype_afpd, "==> Finished AFP command: %s -> %s",
679 AfpNum2name(function), AfpErr2name(err));
681 dsi->flags &= ~DSI_RUNNING;
683 LOG(log_error, logtype_afpd, "(write) bad function %x", function);
688 if (!dsi_wrtreply(dsi, err)) {
689 LOG(log_error, logtype_afpd, "dsi_wrtreply: %s", strerror(errno) );
690 if (dsi_disconnect(dsi) != 0)
691 afp_dsi_die(EXITERR_CLNT);
695 case DSIFUNC_ATTN: /* attention replies */
698 /* error. this usually implies a mismatch of some kind
699 * between server and client. if things are correct,
700 * we need to flush the rest of the packet if necessary. */
702 LOG(log_info, logtype_afpd,"afp_dsi: spurious command %d", cmd);
703 dsi_writeinit(dsi, dsi->data, DSI_DATASIZ);
707 pending_request(dsi);
709 fce_pending_events(obj);
713 afp_dsi_die(EXITERR_CLNT);