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>
41 #include <atalk/globals.h>
50 #endif /* FORCE_UIDGID */
53 #define SOL_TCP IPPROTO_TCP
57 * We generally pass this from afp_over_dsi to all afp_* funcs, so it should already be
58 * available everywhere. Unfortunately some funcs (eg acltoownermode) need acces to it
59 * but are deeply nested in the function chain with the caller already without acces to it.
60 * Changing this would require adding a reference to the caller which itself might be
61 * called in many places (eg acltoownermode is called from accessmode).
62 * The only sane way out is providing a copy of it here:
64 AFPObj *AFPobj = NULL;
75 * - indexed just by taking DSIreqID mod REPLAYCACHE_SIZE
77 static rc_elem_t replaycache[REPLAYCACHE_SIZE];
79 static sigjmp_buf recon_jmp;
80 static void afp_dsi_close(AFPObj *obj)
82 DSI *dsi = obj->handle;
88 /* we may have been called from a signal handler caught when afpd was running
89 * as uid 0, that's the wrong user for volume's prexec_close scripts if any,
90 * restore our login user
92 if (geteuid() != obj->uid) {
93 if (seteuid( obj->uid ) < 0) {
94 LOG(log_error, logtype_afpd, "can't seteuid(%u) back %s: uid: %u, euid: %u",
95 obj->uid, strerror(errno), getuid(), geteuid());
103 /* Block sigs, PAM/systemd/whoever might send us a SIG??? in (*obj->logout)() -> pam_close_session() */
105 pthread_sigmask(SIG_BLOCK, &sigs, NULL);
109 LOG(log_note, logtype_afpd, "AFP statistics: %.2f KB read, %.2f KB written",
110 dsi->read_count/1024.0, dsi->write_count/1024.0);
116 /* -------------------------------
118 * a little bit of code duplication.
120 static void afp_dsi_die(int sig)
122 DSI *dsi = (DSI *)AFPobj->handle;
124 if (dsi->flags & DSI_RECONINPROG) {
125 /* Primary reconnect succeeded, got SIGTERM from afpd parent */
126 dsi->flags &= ~DSI_RECONINPROG;
127 return; /* this returns to afp_disconnect */
130 if (dsi->flags & DSI_DISCONNECTED) {
131 LOG(log_note, logtype_afpd, "Disconnected session terminating");
135 dsi_attention(AFPobj->handle, AFPATTN_SHUTDOWN);
136 afp_dsi_close(AFPobj);
137 if (sig) /* if no signal, assume dieing because logins are disabled &
138 don't log it (maintenance mode)*/
139 LOG(log_info, logtype_afpd, "Connection terminated");
140 if (sig == SIGTERM || sig == SIGALRM) {
148 /* SIGQUIT handler */
149 static void ipc_reconnect_handler(int sig _U_)
151 DSI *dsi = (DSI *)AFPobj->handle;
153 if (reconnect_ipc(AFPobj) != 0) {
154 LOG(log_error, logtype_afpd, "ipc_reconnect_handler: failed IPC reconnect");
155 afp_dsi_close(AFPobj);
159 if (ipc_child_write(AFPobj->ipc_fd, IPC_GETSESSION, AFPobj->sinfo.clientid_len, AFPobj->sinfo.clientid) != 0) {
160 LOG(log_error, logtype_afpd, "ipc_reconnect_handler: failed IPC ID resend");
161 afp_dsi_close(AFPobj);
164 LOG(log_note, logtype_afpd, "ipc_reconnect_handler: IPC reconnect done");
167 /* SIGURG handler (primary reconnect) */
168 static void afp_dsi_transfer_session(int sig _U_)
172 DSI *dsi = (DSI *)AFPobj->handle;
174 LOG(log_debug, logtype_afpd, "afp_dsi_transfer_session: got SIGURG, trying to receive session");
176 if (readt(AFPobj->ipc_fd, &dsiID, 2, 0, 2) != 2) {
177 LOG(log_error, logtype_afpd, "afp_dsi_transfer_session: couldn't receive DSI id, goodbye");
178 afp_dsi_close(AFPobj);
182 if ((socket = recv_fd(AFPobj->ipc_fd, 1)) == -1) {
183 LOG(log_error, logtype_afpd, "afp_dsi_transfer_session: couldn't receive session fd, goodbye");
184 afp_dsi_close(AFPobj);
188 LOG(log_debug, logtype_afpd, "afp_dsi_transfer_session: received socket fd: %i", socket);
190 dsi->proto_close(dsi);
191 dsi->socket = socket;
192 dsi->flags = DSI_RECONSOCKET;
194 dsi->eof = dsi->start = dsi->buffer;
196 dsi->header.dsi_requestID = dsiID;
197 dsi->header.dsi_command = DSIFUNC_CMD;
200 * The session transfer happens in the middle of FPDisconnect old session, thus we
201 * have to send the reply now.
203 if (!dsi_cmdreply(dsi, AFP_OK)) {
204 LOG(log_error, logtype_afpd, "dsi_cmdreply: %s", strerror(errno) );
205 afp_dsi_close(AFPobj);
209 LOG(log_note, logtype_afpd, "afp_dsi_transfer_session: succesfull primary reconnect");
211 * Now returning from this signal handler return to dsi_receive which should start
212 * reading/continuing from the connected socket that was passed via the parent from
213 * another session. The parent will terminate that session.
215 siglongjmp(recon_jmp, 1);
218 /* ------------------- */
219 static void afp_dsi_timedown(int sig _U_)
223 DSI *dsi = (DSI *)AFPobj->handle;
224 dsi->flags |= DSI_DIE;
225 /* shutdown and don't reconnect. server going down in 5 minutes. */
226 setmessage("The server is going down for maintenance.");
227 if (dsi_attention(AFPobj->handle, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
228 AFPATTN_MESG | AFPATTN_TIME(5)) < 0) {
229 DSI *dsi = (DSI *)AFPobj->handle;
230 dsi->down_request = 1;
233 it.it_interval.tv_sec = 0;
234 it.it_interval.tv_usec = 0;
235 it.it_value.tv_sec = 300;
236 it.it_value.tv_usec = 0;
238 if ( setitimer( ITIMER_REAL, &it, NULL ) < 0 ) {
239 LOG(log_error, logtype_afpd, "afp_timedown: setitimer: %s", strerror(errno) );
240 afp_dsi_die(EXITERR_SYS);
242 memset(&sv, 0, sizeof(sv));
243 sv.sa_handler = afp_dsi_die;
244 sigemptyset( &sv.sa_mask );
245 sigaddset(&sv.sa_mask, SIGHUP);
246 sigaddset(&sv.sa_mask, SIGTERM);
247 sv.sa_flags = SA_RESTART;
248 if ( sigaction( SIGALRM, &sv, NULL ) < 0 ) {
249 LOG(log_error, logtype_afpd, "afp_timedown: sigaction: %s", strerror(errno) );
250 afp_dsi_die(EXITERR_SYS);
254 sv.sa_handler = SIG_IGN;
255 sigemptyset( &sv.sa_mask );
256 sv.sa_flags = SA_RESTART;
257 if ( sigaction( SIGUSR1, &sv, NULL ) < 0 ) {
258 LOG(log_error, logtype_afpd, "afp_timedown: sigaction SIGHUP: %s", strerror(errno) );
259 afp_dsi_die(EXITERR_SYS);
263 /* ---------------------------------
264 * SIGHUP reload configuration file
266 volatile int reload_request = 0;
268 static void afp_dsi_reload(int sig _U_)
273 /* ---------------------------------
274 * SIGINT: enable max_debug LOGging
276 static volatile sig_atomic_t debug_request = 0;
278 static void afp_dsi_debug(int sig _U_)
283 /* ---------------------- */
284 static void afp_dsi_getmesg (int sig _U_)
286 DSI *dsi = (DSI *)AFPobj->handle;
288 dsi->msg_request = 1;
289 if (dsi_attention(AFPobj->handle, AFPATTN_MESG | AFPATTN_TIME(5)) < 0)
290 dsi->msg_request = 2;
293 static void alarm_handler(int sig _U_)
296 DSI *dsi = (DSI *)AFPobj->handle;
298 /* we have to restart the timer because some libraries may use alarm() */
299 setitimer(ITIMER_REAL, &dsi->timer, NULL);
301 /* we got some traffic from the client since the previous timer tick. */
302 if ((dsi->flags & DSI_DATA)) {
303 dsi->flags &= ~DSI_DATA;
308 LOG(log_maxdebug, logtype_afpd, "alarm: tickles: %u, flags: %s|%s|%s|%s|%s|%s|%s|%s|%s",
310 (dsi->flags & DSI_DATA) ? "DSI_DATA" : "-",
311 (dsi->flags & DSI_RUNNING) ? "DSI_RUNNING" : "-",
312 (dsi->flags & DSI_SLEEPING) ? "DSI_SLEEPING" : "-",
313 (dsi->flags & DSI_EXTSLEEP) ? "DSI_EXTSLEEP" : "-",
314 (dsi->flags & DSI_DISCONNECTED) ? "DSI_DISCONNECTED" : "-",
315 (dsi->flags & DSI_DIE) ? "DSI_DIE" : "-",
316 (dsi->flags & DSI_NOREPLY) ? "DSI_NOREPLY" : "-",
317 (dsi->flags & DSI_RECONSOCKET) ? "DSI_RECONSOCKET" : "-",
318 (dsi->flags & DSI_RECONINPROG) ? "DSI_RECONINPROG" : "-");
320 if (dsi->flags & DSI_SLEEPING) {
321 if (dsi->tickle > AFPobj->options.sleep) {
322 LOG(log_note, logtype_afpd, "afp_alarm: sleep time ended");
323 afp_dsi_die(EXITERR_CLNT);
328 if (dsi->flags & DSI_DISCONNECTED) {
329 if (geteuid() == 0) {
330 LOG(log_note, logtype_afpd, "afp_alarm: unauthenticated user, connection problem");
331 afp_dsi_die(EXITERR_CLNT);
333 if (dsi->tickle > AFPobj->options.disconnected) {
334 LOG(log_error, logtype_afpd, "afp_alarm: reconnect timer expired, goodbye");
335 afp_dsi_die(EXITERR_CLNT);
340 /* if we're in the midst of processing something, don't die. */
341 if (dsi->tickle >= AFPobj->options.timeout) {
342 LOG(log_error, logtype_afpd, "afp_alarm: child timed out, entering disconnected state");
343 if (dsi_disconnect(dsi) != 0)
344 afp_dsi_die(EXITERR_CLNT);
348 if ((err = pollvoltime(AFPobj)) == 0)
349 LOG(log_debug, logtype_afpd, "afp_alarm: sending DSI tickle");
350 err = dsi_tickle(AFPobj->handle);
352 if (geteuid() == 0) {
353 LOG(log_note, logtype_afpd, "afp_alarm: unauthenticated user, connection problem");
354 afp_dsi_die(EXITERR_CLNT);
356 LOG(log_error, logtype_afpd, "afp_alarm: connection problem, entering disconnected state");
357 if (dsi_disconnect(dsi) != 0)
358 afp_dsi_die(EXITERR_CLNT);
363 if dsi->in_write is set attention, tickle (and close?) msg
364 aren't sent. We don't care about tickle
366 static void pending_request(DSI *dsi)
368 /* send pending attention */
370 /* read msg if any, it could be done in afp_getsrvrmesg */
371 if (dsi->msg_request) {
372 if (dsi->msg_request == 2) {
373 /* didn't send it in signal handler */
374 dsi_attention(AFPobj->handle, AFPATTN_MESG | AFPATTN_TIME(5));
376 dsi->msg_request = 0;
379 if (dsi->down_request) {
380 dsi->down_request = 0;
381 dsi_attention(AFPobj->handle, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
382 AFPATTN_MESG | AFPATTN_TIME(5));
386 void afp_over_dsi_sighandlers(AFPObj *obj)
388 DSI *dsi = (DSI *) obj->handle;
389 struct sigaction action;
391 memset(&action, 0, sizeof(action));
392 sigfillset(&action.sa_mask);
393 action.sa_flags = SA_RESTART;
396 action.sa_handler = afp_dsi_reload;
397 if ( sigaction( SIGHUP, &action, NULL ) < 0 ) {
398 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
399 afp_dsi_die(EXITERR_SYS);
403 action.sa_handler = afp_dsi_transfer_session;
404 if ( sigaction( SIGURG, &action, NULL ) < 0 ) {
405 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
406 afp_dsi_die(EXITERR_SYS);
409 /* install SIGTERM */
410 action.sa_handler = afp_dsi_die;
411 if ( sigaction( SIGTERM, &action, NULL ) < 0 ) {
412 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
413 afp_dsi_die(EXITERR_SYS);
416 /* install SIGQUIT */
417 action.sa_handler = ipc_reconnect_handler;
418 if ( sigaction(SIGQUIT, &action, NULL ) < 0 ) {
419 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
420 afp_dsi_die(EXITERR_SYS);
423 /* SIGUSR2 - server message support */
424 action.sa_handler = afp_dsi_getmesg;
425 if ( sigaction( SIGUSR2, &action, NULL) < 0 ) {
426 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
427 afp_dsi_die(EXITERR_SYS);
430 /* SIGUSR1 - set down in 5 minutes */
431 action.sa_handler = afp_dsi_timedown;
432 action.sa_flags = SA_RESTART;
433 if ( sigaction( SIGUSR1, &action, NULL) < 0 ) {
434 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
435 afp_dsi_die(EXITERR_SYS);
438 /* SIGINT - enable max_debug LOGging to /tmp/afpd.PID.XXXXXX */
439 action.sa_handler = afp_dsi_debug;
440 if ( sigaction( SIGINT, &action, NULL) < 0 ) {
441 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
442 afp_dsi_die(EXITERR_SYS);
446 /* SIGALRM - tickle handler */
447 action.sa_handler = alarm_handler;
448 if ((sigaction(SIGALRM, &action, NULL) < 0) ||
449 (setitimer(ITIMER_REAL, &dsi->timer, NULL) < 0)) {
450 afp_dsi_die(EXITERR_SYS);
452 #endif /* DEBUGGING */
455 /* -------------------------------------------
456 afp over dsi. this never returns.
458 void afp_over_dsi(AFPObj *obj)
460 DSI *dsi = (DSI *) obj->handle;
466 obj->exit = afp_dsi_die;
467 obj->reply = (int (*)()) dsi_cmdreply;
468 obj->attention = (int (*)(void *, AFPUserBytes)) dsi_attention;
471 afp_over_dsi_sighandlers(obj);
473 if (dircache_init(obj->options.dircachesize) != 0)
474 afp_dsi_die(EXITERR_SYS);
476 /* set TCP snd/rcv buf */
477 if (obj->options.tcp_rcvbuf) {
478 if (setsockopt(dsi->socket,
481 &obj->options.tcp_rcvbuf,
482 sizeof(obj->options.tcp_rcvbuf)) != 0) {
483 LOG(log_error, logtype_dsi, "afp_over_dsi: setsockopt(SO_RCVBUF): %s", strerror(errno));
486 if (obj->options.tcp_sndbuf) {
487 if (setsockopt(dsi->socket,
490 &obj->options.tcp_sndbuf,
491 sizeof(obj->options.tcp_sndbuf)) != 0) {
492 LOG(log_error, logtype_dsi, "afp_over_dsi: setsockopt(SO_SNDBUF): %s", strerror(errno));
496 /* set TCP_NODELAY */
498 setsockopt(dsi->socket, SOL_TCP, TCP_NODELAY, &flag, sizeof(flag));
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);
572 setuplog("default log_note");
577 sprintf(logstr, "default log_maxdebug /tmp/afpd.%u.XXXXXX", getpid());
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 (char *)&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();
643 /* bring everything back to old euid, egid */
645 restore_uidgid ( &obj->uidgid );
646 #endif /* FORCE_UIDGID */
647 dsi->flags &= ~DSI_RUNNING;
649 /* Add result to the AFP replay cache */
650 replaycache[rc_idx].DSIreqID = dsi->clientID;
651 replaycache[rc_idx].AFPcommand = function;
652 replaycache[rc_idx].result = err;
654 LOG(log_error, logtype_afpd, "bad function %X", function);
660 /* single shot toggle that gets set by dsi_readinit. */
661 if (dsi->flags & DSI_NOREPLY) {
662 dsi->flags &= ~DSI_NOREPLY;
664 } else if (!dsi_cmdreply(dsi, err)) {
665 LOG(log_error, logtype_afpd, "dsi_cmdreply(%d): %s", dsi->socket, strerror(errno) );
666 if (dsi_disconnect(dsi) != 0)
667 afp_dsi_die(EXITERR_CLNT);
671 case DSIFUNC_WRITE: /* FPWrite and FPAddIcon */
672 function = (u_char) dsi->commands[0];
673 if ( afp_switch[ function ] != NULL ) {
674 dsi->datalen = DSI_DATASIZ;
675 dsi->flags |= DSI_RUNNING;
677 LOG(log_debug, logtype_afpd, "<== Start AFP command: %s", AfpNum2name(function));
679 err = (*afp_switch[function])(obj,
680 (char *)&dsi->commands, dsi->cmdlen,
681 (char *)&dsi->data, &dsi->datalen);
683 LOG(log_debug, logtype_afpd, "==> Finished AFP command: %s -> %s",
684 AfpNum2name(function), AfpErr2name(err));
686 dsi->flags &= ~DSI_RUNNING;
688 /* bring everything back to old euid, egid */
690 restore_uidgid ( &obj->uidgid );
691 #endif /* FORCE_UIDGID */
693 LOG(log_error, logtype_afpd, "(write) bad function %x", function);
698 if (!dsi_wrtreply(dsi, err)) {
699 LOG(log_error, logtype_afpd, "dsi_wrtreply: %s", strerror(errno) );
700 if (dsi_disconnect(dsi) != 0)
701 afp_dsi_die(EXITERR_CLNT);
705 case DSIFUNC_ATTN: /* attention replies */
708 /* error. this usually implies a mismatch of some kind
709 * between server and client. if things are correct,
710 * we need to flush the rest of the packet if necessary. */
712 LOG(log_info, logtype_afpd,"afp_dsi: spurious command %d", cmd);
713 dsi_writeinit(dsi, dsi->data, DSI_DATASIZ);
717 pending_request(dsi);
719 fce_pending_events(obj);
723 afp_dsi_die(EXITERR_CLNT);