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>
42 #include <atalk/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 /* SIGURG handler (primary reconnect) */
145 static void afp_dsi_transfer_session(int sig _U_)
149 DSI *dsi = (DSI *)AFPobj->dsi;
151 LOG(log_debug, logtype_afpd, "afp_dsi_transfer_session: got SIGURG, trying to receive session");
153 if (readt(AFPobj->ipc_fd, &dsiID, 2, 0, 2) != 2) {
154 LOG(log_error, logtype_afpd, "afp_dsi_transfer_session: couldn't receive DSI id, goodbye");
155 afp_dsi_close(AFPobj);
159 if ((socket = recv_fd(AFPobj->ipc_fd, 1)) == -1) {
160 LOG(log_error, logtype_afpd, "afp_dsi_transfer_session: couldn't receive session fd, goodbye");
161 afp_dsi_close(AFPobj);
165 LOG(log_debug, logtype_afpd, "afp_dsi_transfer_session: received socket fd: %i", socket);
167 dsi->proto_close(dsi);
168 dsi->socket = socket;
169 dsi->flags = DSI_RECONSOCKET;
171 dsi->eof = dsi->start = dsi->buffer;
173 dsi->header.dsi_requestID = dsiID;
174 dsi->header.dsi_command = DSIFUNC_CMD;
177 * The session transfer happens in the middle of FPDisconnect old session, thus we
178 * have to send the reply now.
180 if (!dsi_cmdreply(dsi, AFP_OK)) {
181 LOG(log_error, logtype_afpd, "dsi_cmdreply: %s", strerror(errno) );
182 afp_dsi_close(AFPobj);
186 LOG(log_note, logtype_afpd, "afp_dsi_transfer_session: succesfull primary reconnect");
188 * Now returning from this signal handler return to dsi_receive which should start
189 * reading/continuing from the connected socket that was passed via the parent from
190 * another session. The parent will terminate that session.
192 siglongjmp(recon_jmp, 1);
195 /* ------------------- */
196 static void afp_dsi_timedown(int sig _U_)
200 DSI *dsi = (DSI *)AFPobj->dsi;
201 dsi->flags |= DSI_DIE;
202 /* shutdown and don't reconnect. server going down in 5 minutes. */
203 setmessage("The server is going down for maintenance.");
204 if (dsi_attention(AFPobj->dsi, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
205 AFPATTN_MESG | AFPATTN_TIME(5)) < 0) {
206 DSI *dsi = (DSI *)AFPobj->dsi;
207 dsi->down_request = 1;
210 it.it_interval.tv_sec = 0;
211 it.it_interval.tv_usec = 0;
212 it.it_value.tv_sec = 300;
213 it.it_value.tv_usec = 0;
215 if ( setitimer( ITIMER_REAL, &it, NULL ) < 0 ) {
216 LOG(log_error, logtype_afpd, "afp_timedown: setitimer: %s", strerror(errno) );
217 afp_dsi_die(EXITERR_SYS);
219 memset(&sv, 0, sizeof(sv));
220 sv.sa_handler = afp_dsi_die;
221 sigemptyset( &sv.sa_mask );
222 sigaddset(&sv.sa_mask, SIGHUP);
223 sigaddset(&sv.sa_mask, SIGTERM);
224 sv.sa_flags = SA_RESTART;
225 if ( sigaction( SIGALRM, &sv, NULL ) < 0 ) {
226 LOG(log_error, logtype_afpd, "afp_timedown: sigaction: %s", strerror(errno) );
227 afp_dsi_die(EXITERR_SYS);
231 sv.sa_handler = SIG_IGN;
232 sigemptyset( &sv.sa_mask );
233 sv.sa_flags = SA_RESTART;
234 if ( sigaction( SIGUSR1, &sv, NULL ) < 0 ) {
235 LOG(log_error, logtype_afpd, "afp_timedown: sigaction SIGHUP: %s", strerror(errno) );
236 afp_dsi_die(EXITERR_SYS);
240 /* ---------------------------------
241 * SIGHUP reload configuration file
243 volatile int reload_request = 0;
245 static void afp_dsi_reload(int sig _U_)
250 /* ---------------------------------
251 * SIGINT: enable max_debug LOGging
253 static volatile sig_atomic_t debug_request = 0;
255 static void afp_dsi_debug(int sig _U_)
260 /* ---------------------- */
261 static void afp_dsi_getmesg (int sig _U_)
263 DSI *dsi = (DSI *)AFPobj->dsi;
265 dsi->msg_request = 1;
266 if (dsi_attention(AFPobj->dsi, AFPATTN_MESG | AFPATTN_TIME(5)) < 0)
267 dsi->msg_request = 2;
270 static void alarm_handler(int sig _U_)
273 DSI *dsi = (DSI *)AFPobj->dsi;
275 /* we have to restart the timer because some libraries may use alarm() */
276 setitimer(ITIMER_REAL, &dsi->timer, NULL);
278 /* we got some traffic from the client since the previous timer tick. */
279 if ((dsi->flags & DSI_DATA)) {
280 dsi->flags &= ~DSI_DATA;
285 LOG(log_maxdebug, logtype_afpd, "alarm: tickles: %u, flags: %s|%s|%s|%s|%s|%s|%s|%s|%s",
287 (dsi->flags & DSI_DATA) ? "DSI_DATA" : "-",
288 (dsi->flags & DSI_RUNNING) ? "DSI_RUNNING" : "-",
289 (dsi->flags & DSI_SLEEPING) ? "DSI_SLEEPING" : "-",
290 (dsi->flags & DSI_EXTSLEEP) ? "DSI_EXTSLEEP" : "-",
291 (dsi->flags & DSI_DISCONNECTED) ? "DSI_DISCONNECTED" : "-",
292 (dsi->flags & DSI_DIE) ? "DSI_DIE" : "-",
293 (dsi->flags & DSI_NOREPLY) ? "DSI_NOREPLY" : "-",
294 (dsi->flags & DSI_RECONSOCKET) ? "DSI_RECONSOCKET" : "-",
295 (dsi->flags & DSI_RECONINPROG) ? "DSI_RECONINPROG" : "-");
297 if (dsi->flags & DSI_SLEEPING) {
298 if (dsi->tickle > AFPobj->options.sleep) {
299 LOG(log_note, logtype_afpd, "afp_alarm: sleep time ended");
300 afp_dsi_die(EXITERR_CLNT);
305 if (dsi->flags & DSI_DISCONNECTED) {
306 if (geteuid() == 0) {
307 LOG(log_note, logtype_afpd, "afp_alarm: unauthenticated user, connection problem");
308 afp_dsi_die(EXITERR_CLNT);
310 if (dsi->tickle > AFPobj->options.disconnected) {
311 LOG(log_error, logtype_afpd, "afp_alarm: reconnect timer expired, goodbye");
312 afp_dsi_die(EXITERR_CLNT);
317 /* if we're in the midst of processing something, don't die. */
318 if (dsi->tickle >= AFPobj->options.timeout) {
319 LOG(log_error, logtype_afpd, "afp_alarm: child timed out, entering disconnected state");
320 if (dsi_disconnect(dsi) != 0)
321 afp_dsi_die(EXITERR_CLNT);
325 if ((err = pollvoltime(AFPobj)) == 0) {
326 LOG(log_debug, logtype_afpd, "afp_alarm: sending DSI tickle");
327 err = dsi_tickle(AFPobj->dsi);
330 if (geteuid() == 0) {
331 LOG(log_note, logtype_afpd, "afp_alarm: unauthenticated user, connection problem");
332 afp_dsi_die(EXITERR_CLNT);
334 LOG(log_error, logtype_afpd, "afp_alarm: connection problem, entering disconnected state");
335 if (dsi_disconnect(dsi) != 0)
336 afp_dsi_die(EXITERR_CLNT);
341 if dsi->in_write is set attention, tickle (and close?) msg
342 aren't sent. We don't care about tickle
344 static void pending_request(DSI *dsi)
346 /* send pending attention */
348 /* read msg if any, it could be done in afp_getsrvrmesg */
349 if (dsi->msg_request) {
350 if (dsi->msg_request == 2) {
351 /* didn't send it in signal handler */
352 dsi_attention(AFPobj->dsi, AFPATTN_MESG | AFPATTN_TIME(5));
354 dsi->msg_request = 0;
357 if (dsi->down_request) {
358 dsi->down_request = 0;
359 dsi_attention(AFPobj->dsi, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
360 AFPATTN_MESG | AFPATTN_TIME(5));
364 void afp_over_dsi_sighandlers(AFPObj *obj)
366 DSI *dsi = (DSI *) obj->dsi;
367 struct sigaction action;
369 memset(&action, 0, sizeof(action));
370 sigfillset(&action.sa_mask);
371 action.sa_flags = SA_RESTART;
374 action.sa_handler = afp_dsi_reload;
375 if ( sigaction( SIGHUP, &action, NULL ) < 0 ) {
376 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
377 afp_dsi_die(EXITERR_SYS);
381 action.sa_handler = afp_dsi_transfer_session;
382 if ( sigaction( SIGURG, &action, NULL ) < 0 ) {
383 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
384 afp_dsi_die(EXITERR_SYS);
387 /* install SIGTERM */
388 action.sa_handler = afp_dsi_die;
389 if ( sigaction( SIGTERM, &action, NULL ) < 0 ) {
390 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
391 afp_dsi_die(EXITERR_SYS);
394 /* install SIGQUIT */
395 action.sa_handler = afp_dsi_die;
396 if ( sigaction(SIGQUIT, &action, NULL ) < 0 ) {
397 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
398 afp_dsi_die(EXITERR_SYS);
401 /* SIGUSR2 - server message support */
402 action.sa_handler = afp_dsi_getmesg;
403 if ( sigaction( SIGUSR2, &action, NULL) < 0 ) {
404 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
405 afp_dsi_die(EXITERR_SYS);
408 /* SIGUSR1 - set down in 5 minutes */
409 action.sa_handler = afp_dsi_timedown;
410 action.sa_flags = SA_RESTART;
411 if ( sigaction( SIGUSR1, &action, NULL) < 0 ) {
412 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
413 afp_dsi_die(EXITERR_SYS);
416 /* SIGINT - enable max_debug LOGging to /tmp/afpd.PID.XXXXXX */
417 action.sa_handler = afp_dsi_debug;
418 if ( sigaction( SIGINT, &action, NULL) < 0 ) {
419 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
420 afp_dsi_die(EXITERR_SYS);
424 /* SIGALRM - tickle handler */
425 action.sa_handler = alarm_handler;
426 if ((sigaction(SIGALRM, &action, NULL) < 0) ||
427 (setitimer(ITIMER_REAL, &dsi->timer, NULL) < 0)) {
428 afp_dsi_die(EXITERR_SYS);
430 #endif /* DEBUGGING */
433 /* -------------------------------------------
434 afp over dsi. this never returns.
436 void afp_over_dsi(AFPObj *obj)
438 DSI *dsi = (DSI *) obj->dsi;
444 obj->exit = afp_dsi_die;
445 obj->reply = (int (*)()) dsi_cmdreply;
446 obj->attention = (int (*)(void *, AFPUserBytes)) dsi_attention;
449 afp_over_dsi_sighandlers(obj);
451 if (dircache_init(obj->options.dircachesize) != 0)
452 afp_dsi_die(EXITERR_SYS);
454 /* set TCP snd/rcv buf */
455 if (obj->options.tcp_rcvbuf) {
456 if (setsockopt(dsi->socket,
459 &obj->options.tcp_rcvbuf,
460 sizeof(obj->options.tcp_rcvbuf)) != 0) {
461 LOG(log_error, logtype_dsi, "afp_over_dsi: setsockopt(SO_RCVBUF): %s", strerror(errno));
464 if (obj->options.tcp_sndbuf) {
465 if (setsockopt(dsi->socket,
468 &obj->options.tcp_sndbuf,
469 sizeof(obj->options.tcp_sndbuf)) != 0) {
470 LOG(log_error, logtype_dsi, "afp_over_dsi: setsockopt(SO_SNDBUF): %s", strerror(errno));
474 /* set TCP_NODELAY */
476 setsockopt(dsi->socket, SOL_TCP, TCP_NODELAY, &flag, sizeof(flag));
478 /* Initialize Spotlight */
479 if ((obj->options.flags & OPTION_SPOTLIGHT) && (obj->options.slmod_path))
480 sl_mod_load(obj->options.slmod_path);
482 ipc_child_state(obj, DSI_RUNNING);
484 /* get stuck here until the end */
486 if (sigsetjmp(recon_jmp, 1) != 0)
487 /* returning from SIGALARM handler for a primary reconnect */
490 /* Blocking read on the network socket */
491 cmd = dsi_stream_receive(dsi);
494 /* cmd == 0 is the error condition */
495 if (dsi->flags & DSI_RECONSOCKET) {
496 /* we just got a reconnect so we immediately try again to receive on the new fd */
497 dsi->flags &= ~DSI_RECONSOCKET;
501 /* the client sometimes logs out (afp_logout) but doesn't close the DSI session */
502 if (dsi->flags & DSI_AFP_LOGGED_OUT) {
503 LOG(log_note, logtype_afpd, "afp_over_dsi: client logged out, terminating DSI session");
508 if (dsi->flags & DSI_RECONINPROG) {
509 LOG(log_note, logtype_afpd, "afp_over_dsi: failed reconnect");
514 /* Some error on the client connection, enter disconnected state */
515 if (dsi_disconnect(dsi) != 0)
516 afp_dsi_die(EXITERR_CLNT);
518 ipc_child_state(obj, DSI_DISCONNECTED);
520 while (dsi->flags & DSI_DISCONNECTED)
521 pause(); /* gets interrupted by SIGALARM or SIGURG tickle */
522 ipc_child_state(obj, DSI_RUNNING);
523 continue; /* continue receiving until disconnect timer expires
524 * or a primary reconnect succeeds */
527 if (!(dsi->flags & DSI_EXTSLEEP) && (dsi->flags & DSI_SLEEPING)) {
528 LOG(log_debug, logtype_afpd, "afp_over_dsi: got data, ending normal sleep");
529 dsi->flags &= ~DSI_SLEEPING;
531 ipc_child_state(obj, DSI_RUNNING);
534 if (reload_request) {
536 load_volumes(AFPobj);
539 /* The first SIGINT enables debugging, the next restores the config */
541 static int debugging = 0;
548 if (obj->options.logconfig)
549 setuplog(obj->options.logconfig, obj->options.logfile);
551 setuplog("default:note", NULL);
556 sprintf(logstr, "/tmp/afpd.%u.XXXXXX", getpid());
557 setuplog("default:maxdebug", logstr);
562 dsi->flags |= DSI_DATA;
568 LOG(log_debug, logtype_afpd, "DSI: close session request");
570 LOG(log_note, logtype_afpd, "done");
574 dsi->flags &= ~DSI_DATA; /* thats no data in the sense we use it in alarm_handler */
575 LOG(log_debug, logtype_afpd, "DSI: client tickle");
576 /* timer is not every 30 seconds anymore, so we don't get killed on the client side. */
577 if ((dsi->flags & DSI_DIE))
584 if ( flushfork( writtenfork ) < 0 ) {
585 LOG(log_error, logtype_afpd, "main flushfork: %s", strerror(errno) );
591 function = (u_char) dsi->commands[0];
593 /* AFP replay cache */
594 rc_idx = dsi->clientID % REPLAYCACHE_SIZE;
595 LOG(log_debug, logtype_dsi, "DSI request ID: %u", dsi->clientID);
597 if (replaycache[rc_idx].DSIreqID == dsi->clientID
598 && replaycache[rc_idx].AFPcommand == function) {
599 LOG(log_note, logtype_afpd, "AFP Replay Cache match: id: %u / cmd: %s",
600 dsi->clientID, AfpNum2name(function));
601 err = replaycache[rc_idx].result;
602 /* AFP replay cache end */
604 /* send off an afp command. in a couple cases, we take advantage
605 * of the fact that we're a stream-based protocol. */
606 if (afp_switch[function]) {
607 dsi->datalen = DSI_DATASIZ;
608 dsi->flags |= DSI_RUNNING;
610 LOG(log_debug, logtype_afpd, "<== Start AFP command: %s", AfpNum2name(function));
612 AFP_AFPFUNC_START(function, (char *)AfpNum2name(function));
613 err = (*afp_switch[function])(obj,
614 (char *)dsi->commands, dsi->cmdlen,
615 (char *)&dsi->data, &dsi->datalen);
617 AFP_AFPFUNC_DONE(function, (char *)AfpNum2name(function));
618 LOG(log_debug, logtype_afpd, "==> Finished AFP command: %s -> %s",
619 AfpNum2name(function), AfpErr2name(err));
621 dir_free_invalid_q();
623 dsi->flags &= ~DSI_RUNNING;
625 /* Add result to the AFP replay cache */
626 replaycache[rc_idx].DSIreqID = dsi->clientID;
627 replaycache[rc_idx].AFPcommand = function;
628 replaycache[rc_idx].result = err;
630 LOG(log_error, logtype_afpd, "bad function %X", function);
636 /* single shot toggle that gets set by dsi_readinit. */
637 if (dsi->flags & DSI_NOREPLY) {
638 dsi->flags &= ~DSI_NOREPLY;
640 } else if (!dsi_cmdreply(dsi, err)) {
641 LOG(log_error, logtype_afpd, "dsi_cmdreply(%d): %s", dsi->socket, strerror(errno) );
642 if (dsi_disconnect(dsi) != 0)
643 afp_dsi_die(EXITERR_CLNT);
647 case DSIFUNC_WRITE: /* FPWrite and FPAddIcon */
648 function = (u_char) dsi->commands[0];
649 if ( afp_switch[ function ] != NULL ) {
650 dsi->datalen = DSI_DATASIZ;
651 dsi->flags |= DSI_RUNNING;
653 LOG(log_debug, logtype_afpd, "<== Start AFP command: %s", AfpNum2name(function));
655 AFP_AFPFUNC_START(function, (char *)AfpNum2name(function));
657 err = (*afp_switch[function])(obj,
658 (char *)dsi->commands, dsi->cmdlen,
659 (char *)&dsi->data, &dsi->datalen);
661 AFP_AFPFUNC_DONE(function, (char *)AfpNum2name(function));
663 LOG(log_debug, logtype_afpd, "==> Finished AFP command: %s -> %s",
664 AfpNum2name(function), AfpErr2name(err));
666 dsi->flags &= ~DSI_RUNNING;
668 LOG(log_error, logtype_afpd, "(write) bad function %x", function);
673 if (!dsi_wrtreply(dsi, err)) {
674 LOG(log_error, logtype_afpd, "dsi_wrtreply: %s", strerror(errno) );
675 if (dsi_disconnect(dsi) != 0)
676 afp_dsi_die(EXITERR_CLNT);
680 case DSIFUNC_ATTN: /* attention replies */
683 /* error. this usually implies a mismatch of some kind
684 * between server and client. if things are correct,
685 * we need to flush the rest of the packet if necessary. */
687 LOG(log_info, logtype_afpd,"afp_dsi: spurious command %d", cmd);
688 dsi_writeinit(dsi, dsi->data, DSI_DATASIZ);
692 pending_request(dsi);
694 fce_pending_events(obj);
698 afp_dsi_die(EXITERR_CLNT);