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 SIGTERM, PAM might send us a SIGTERM in (*obj->logout)() -> pam_close_session() */
104 pthread_sigmask(SIG_BLOCK, &sigs, NULL);
106 sigaddset(&sigs, SIGTERM);
110 LOG(log_note, logtype_afpd, "AFP statistics: %.2f KB read, %.2f KB written",
111 dsi->read_count/1024.0, dsi->write_count/1024.0);
117 /* -------------------------------
119 * a little bit of code duplication.
121 static void afp_dsi_die(int sig)
123 DSI *dsi = (DSI *)AFPobj->handle;
125 if (dsi->flags & DSI_RECONINPROG) {
126 /* Primary reconnect succeeded, got SIGTERM from afpd parent */
127 dsi->flags &= ~DSI_RECONINPROG;
128 return; /* this returns to afp_disconnect */
131 if (dsi->flags & DSI_DISCONNECTED) {
132 LOG(log_note, logtype_afpd, "Disconnected session terminating");
136 dsi_attention(AFPobj->handle, AFPATTN_SHUTDOWN);
137 afp_dsi_close(AFPobj);
138 if (sig) /* if no signal, assume dieing because logins are disabled &
139 don't log it (maintenance mode)*/
140 LOG(log_info, logtype_afpd, "Connection terminated");
141 if (sig == SIGTERM || sig == SIGALRM) {
149 /* SIGQUIT handler */
150 static void ipc_reconnect_handler(int sig _U_)
152 DSI *dsi = (DSI *)AFPobj->handle;
154 if (reconnect_ipc(AFPobj) != 0) {
155 LOG(log_error, logtype_afpd, "ipc_reconnect_handler: failed IPC reconnect");
156 afp_dsi_close(AFPobj);
160 if (ipc_child_write(AFPobj->ipc_fd, IPC_GETSESSION, AFPobj->sinfo.clientid_len, AFPobj->sinfo.clientid) != 0) {
161 LOG(log_error, logtype_afpd, "ipc_reconnect_handler: failed IPC ID resend");
162 afp_dsi_close(AFPobj);
165 LOG(log_note, logtype_afpd, "ipc_reconnect_handler: IPC reconnect done");
168 /* SIGURG handler (primary reconnect) */
169 static void afp_dsi_transfer_session(int sig _U_)
173 DSI *dsi = (DSI *)AFPobj->handle;
175 LOG(log_debug, logtype_afpd, "afp_dsi_transfer_session: got SIGURG, trying to receive session");
177 if (readt(AFPobj->ipc_fd, &dsiID, 2, 0, 2) != 2) {
178 LOG(log_error, logtype_afpd, "afp_dsi_transfer_session: couldn't receive DSI id, goodbye");
179 afp_dsi_close(AFPobj);
183 if ((socket = recv_fd(AFPobj->ipc_fd, 1)) == -1) {
184 LOG(log_error, logtype_afpd, "afp_dsi_transfer_session: couldn't receive session fd, goodbye");
185 afp_dsi_close(AFPobj);
189 LOG(log_debug, logtype_afpd, "afp_dsi_transfer_session: received socket fd: %i", socket);
191 dsi->proto_close(dsi);
192 dsi->socket = socket;
193 dsi->flags = DSI_RECONSOCKET;
195 dsi->eof = dsi->start = dsi->buffer;
197 dsi->header.dsi_requestID = dsiID;
198 dsi->header.dsi_command = DSIFUNC_CMD;
201 * The session transfer happens in the middle of FPDisconnect old session, thus we
202 * have to send the reply now.
204 if (!dsi_cmdreply(dsi, AFP_OK)) {
205 LOG(log_error, logtype_afpd, "dsi_cmdreply: %s", strerror(errno) );
206 afp_dsi_close(AFPobj);
210 LOG(log_note, logtype_afpd, "afp_dsi_transfer_session: succesfull primary reconnect");
212 * Now returning from this signal handler return to dsi_receive which should start
213 * reading/continuing from the connected socket that was passed via the parent from
214 * another session. The parent will terminate that session.
216 siglongjmp(recon_jmp, 1);
219 /* ------------------- */
220 static void afp_dsi_timedown(int sig _U_)
224 DSI *dsi = (DSI *)AFPobj->handle;
225 dsi->flags |= DSI_DIE;
226 /* shutdown and don't reconnect. server going down in 5 minutes. */
227 setmessage("The server is going down for maintenance.");
228 if (dsi_attention(AFPobj->handle, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
229 AFPATTN_MESG | AFPATTN_TIME(5)) < 0) {
230 DSI *dsi = (DSI *)AFPobj->handle;
231 dsi->down_request = 1;
234 it.it_interval.tv_sec = 0;
235 it.it_interval.tv_usec = 0;
236 it.it_value.tv_sec = 300;
237 it.it_value.tv_usec = 0;
239 if ( setitimer( ITIMER_REAL, &it, NULL ) < 0 ) {
240 LOG(log_error, logtype_afpd, "afp_timedown: setitimer: %s", strerror(errno) );
241 afp_dsi_die(EXITERR_SYS);
243 memset(&sv, 0, sizeof(sv));
244 sv.sa_handler = afp_dsi_die;
245 sigemptyset( &sv.sa_mask );
246 sigaddset(&sv.sa_mask, SIGHUP);
247 sigaddset(&sv.sa_mask, SIGTERM);
248 sv.sa_flags = SA_RESTART;
249 if ( sigaction( SIGALRM, &sv, NULL ) < 0 ) {
250 LOG(log_error, logtype_afpd, "afp_timedown: sigaction: %s", strerror(errno) );
251 afp_dsi_die(EXITERR_SYS);
255 sv.sa_handler = SIG_IGN;
256 sigemptyset( &sv.sa_mask );
257 sv.sa_flags = SA_RESTART;
258 if ( sigaction( SIGUSR1, &sv, NULL ) < 0 ) {
259 LOG(log_error, logtype_afpd, "afp_timedown: sigaction SIGHUP: %s", strerror(errno) );
260 afp_dsi_die(EXITERR_SYS);
264 /* ---------------------------------
265 * SIGHUP reload configuration file
267 volatile int reload_request = 0;
269 static void afp_dsi_reload(int sig _U_)
274 /* ---------------------------------
275 * SIGINT: enable max_debug LOGging
277 static volatile sig_atomic_t debug_request = 0;
279 static void afp_dsi_debug(int sig _U_)
284 /* ---------------------- */
285 static void afp_dsi_getmesg (int sig _U_)
287 DSI *dsi = (DSI *)AFPobj->handle;
289 dsi->msg_request = 1;
290 if (dsi_attention(AFPobj->handle, AFPATTN_MESG | AFPATTN_TIME(5)) < 0)
291 dsi->msg_request = 2;
294 static void alarm_handler(int sig _U_)
297 DSI *dsi = (DSI *)AFPobj->handle;
299 /* we have to restart the timer because some libraries may use alarm() */
300 setitimer(ITIMER_REAL, &dsi->timer, NULL);
302 /* we got some traffic from the client since the previous timer tick. */
303 if ((dsi->flags & DSI_DATA)) {
304 dsi->flags &= ~DSI_DATA;
309 LOG(log_maxdebug, logtype_afpd, "alarm: tickles: %u, flags: %s|%s|%s|%s|%s|%s|%s|%s|%s",
311 (dsi->flags & DSI_DATA) ? "DSI_DATA" : "-",
312 (dsi->flags & DSI_RUNNING) ? "DSI_RUNNING" : "-",
313 (dsi->flags & DSI_SLEEPING) ? "DSI_SLEEPING" : "-",
314 (dsi->flags & DSI_EXTSLEEP) ? "DSI_EXTSLEEP" : "-",
315 (dsi->flags & DSI_DISCONNECTED) ? "DSI_DISCONNECTED" : "-",
316 (dsi->flags & DSI_DIE) ? "DSI_DIE" : "-",
317 (dsi->flags & DSI_NOREPLY) ? "DSI_NOREPLY" : "-",
318 (dsi->flags & DSI_RECONSOCKET) ? "DSI_RECONSOCKET" : "-",
319 (dsi->flags & DSI_RECONINPROG) ? "DSI_RECONINPROG" : "-");
321 if (dsi->flags & DSI_SLEEPING) {
322 if (dsi->tickle > AFPobj->options.sleep) {
323 LOG(log_note, logtype_afpd, "afp_alarm: sleep time ended");
324 afp_dsi_die(EXITERR_CLNT);
329 if (dsi->flags & DSI_DISCONNECTED) {
330 if (geteuid() == 0) {
331 LOG(log_note, logtype_afpd, "afp_alarm: unauthenticated user, connection problem");
332 afp_dsi_die(EXITERR_CLNT);
334 if (dsi->tickle > AFPobj->options.disconnected) {
335 LOG(log_error, logtype_afpd, "afp_alarm: reconnect timer expired, goodbye");
336 afp_dsi_die(EXITERR_CLNT);
341 /* if we're in the midst of processing something, don't die. */
342 if (dsi->tickle >= AFPobj->options.timeout) {
343 LOG(log_error, logtype_afpd, "afp_alarm: child timed out, entering disconnected state");
344 if (dsi_disconnect(dsi) != 0)
345 afp_dsi_die(EXITERR_CLNT);
349 if ((err = pollvoltime(AFPobj)) == 0)
350 LOG(log_debug, logtype_afpd, "afp_alarm: sending DSI tickle");
351 err = dsi_tickle(AFPobj->handle);
353 if (geteuid() == 0) {
354 LOG(log_note, logtype_afpd, "afp_alarm: unauthenticated user, connection problem");
355 afp_dsi_die(EXITERR_CLNT);
357 LOG(log_error, logtype_afpd, "afp_alarm: connection problem, entering disconnected state");
358 if (dsi_disconnect(dsi) != 0)
359 afp_dsi_die(EXITERR_CLNT);
364 if dsi->in_write is set attention, tickle (and close?) msg
365 aren't sent. We don't care about tickle
367 static void pending_request(DSI *dsi)
369 /* send pending attention */
371 /* read msg if any, it could be done in afp_getsrvrmesg */
372 if (dsi->msg_request) {
373 if (dsi->msg_request == 2) {
374 /* didn't send it in signal handler */
375 dsi_attention(AFPobj->handle, AFPATTN_MESG | AFPATTN_TIME(5));
377 dsi->msg_request = 0;
380 if (dsi->down_request) {
381 dsi->down_request = 0;
382 dsi_attention(AFPobj->handle, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
383 AFPATTN_MESG | AFPATTN_TIME(5));
387 /* -------------------------------------------
388 afp over dsi. this never returns.
390 void afp_over_dsi(AFPObj *obj)
392 DSI *dsi = (DSI *) obj->handle;
396 struct sigaction action;
399 obj->exit = afp_dsi_die;
400 obj->reply = (int (*)()) dsi_cmdreply;
401 obj->attention = (int (*)(void *, AFPUserBytes)) dsi_attention;
404 memset(&action, 0, sizeof(action));
405 sigfillset(&action.sa_mask);
406 action.sa_flags = SA_RESTART;
409 action.sa_handler = afp_dsi_reload;
410 if ( sigaction( SIGHUP, &action, NULL ) < 0 ) {
411 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
412 afp_dsi_die(EXITERR_SYS);
416 action.sa_handler = afp_dsi_transfer_session;
417 if ( sigaction( SIGURG, &action, NULL ) < 0 ) {
418 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
419 afp_dsi_die(EXITERR_SYS);
422 /* install SIGTERM */
423 action.sa_handler = afp_dsi_die;
424 if ( sigaction( SIGTERM, &action, NULL ) < 0 ) {
425 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
426 afp_dsi_die(EXITERR_SYS);
429 /* install SIGQUIT */
430 action.sa_handler = ipc_reconnect_handler;
431 if ( sigaction(SIGQUIT, &action, NULL ) < 0 ) {
432 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
433 afp_dsi_die(EXITERR_SYS);
436 /* SIGUSR2 - server message support */
437 action.sa_handler = afp_dsi_getmesg;
438 if ( sigaction( SIGUSR2, &action, NULL) < 0 ) {
439 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
440 afp_dsi_die(EXITERR_SYS);
443 /* SIGUSR1 - set down in 5 minutes */
444 action.sa_handler = afp_dsi_timedown;
445 action.sa_flags = SA_RESTART;
446 if ( sigaction( SIGUSR1, &action, NULL) < 0 ) {
447 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
448 afp_dsi_die(EXITERR_SYS);
451 /* SIGINT - enable max_debug LOGging to /tmp/afpd.PID.XXXXXX */
452 action.sa_handler = afp_dsi_debug;
453 if ( sigaction( SIGINT, &action, NULL) < 0 ) {
454 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
455 afp_dsi_die(EXITERR_SYS);
459 /* SIGALRM - tickle handler */
460 action.sa_handler = alarm_handler;
461 if ((sigaction(SIGALRM, &action, NULL) < 0) ||
462 (setitimer(ITIMER_REAL, &dsi->timer, NULL) < 0)) {
463 afp_dsi_die(EXITERR_SYS);
465 #endif /* DEBUGGING */
467 if (dircache_init(obj->options.dircachesize) != 0)
468 afp_dsi_die(EXITERR_SYS);
470 /* set TCP snd/rcv buf */
471 if (obj->options.tcp_rcvbuf) {
472 if (setsockopt(dsi->socket,
475 &obj->options.tcp_rcvbuf,
476 sizeof(obj->options.tcp_rcvbuf)) != 0) {
477 LOG(log_error, logtype_dsi, "afp_over_dsi: setsockopt(SO_RCVBUF): %s", strerror(errno));
480 if (obj->options.tcp_sndbuf) {
481 if (setsockopt(dsi->socket,
484 &obj->options.tcp_sndbuf,
485 sizeof(obj->options.tcp_sndbuf)) != 0) {
486 LOG(log_error, logtype_dsi, "afp_over_dsi: setsockopt(SO_SNDBUF): %s", strerror(errno));
490 /* set TCP_NODELAY */
492 setsockopt(dsi->socket, SOL_TCP, TCP_NODELAY, &flag, sizeof(flag));
494 /* get stuck here until the end */
496 if (sigsetjmp(recon_jmp, 1) != 0)
497 /* returning from SIGALARM handler for a primary reconnect */
500 /* Blocking read on the network socket */
501 cmd = dsi_stream_receive(dsi);
504 /* cmd == 0 is the error condition */
505 if (dsi->flags & DSI_RECONSOCKET) {
506 /* we just got a reconnect so we immediately try again to receive on the new fd */
507 dsi->flags &= ~DSI_RECONSOCKET;
511 /* the client sometimes logs out (afp_logout) but doesn't close the DSI session */
512 if (dsi->flags & DSI_AFP_LOGGED_OUT) {
513 LOG(log_note, logtype_afpd, "afp_over_dsi: client logged out, terminating DSI session");
519 /* got ECONNRESET in read from client => exit*/
520 if (dsi->flags & DSI_GOT_ECONNRESET) {
521 LOG(log_note, logtype_afpd, "afp_over_dsi: client connection reset");
527 if (dsi->flags & DSI_RECONINPROG) {
528 LOG(log_note, logtype_afpd, "afp_over_dsi: failed reconnect");
533 /* Some error on the client connection, enter disconnected state */
534 if (dsi_disconnect(dsi) != 0)
535 afp_dsi_die(EXITERR_CLNT);
537 while (dsi->flags & DSI_DISCONNECTED)
538 pause(); /* gets interrupted by SIGALARM or SIGURG tickle */
539 continue; /* continue receiving until disconnect timer expires
540 * or a primary reconnect succeeds */
543 if (!(dsi->flags & DSI_EXTSLEEP) && (dsi->flags & DSI_SLEEPING)) {
544 LOG(log_debug, logtype_afpd, "afp_over_dsi: got data, ending normal sleep");
545 dsi->flags &= ~DSI_SLEEPING;
549 if (reload_request) {
551 load_volumes(AFPobj);
554 /* The first SIGINT enables debugging, the next restores the config */
556 static int debugging = 0;
563 if (obj->options.logconfig)
564 setuplog(obj->options.logconfig);
566 setuplog("default log_note");
571 sprintf(logstr, "default log_maxdebug /tmp/afpd.%u.XXXXXX", getpid());
577 dsi->flags |= DSI_DATA;
583 LOG(log_debug, logtype_afpd, "DSI: close session request");
585 LOG(log_note, logtype_afpd, "done");
589 dsi->flags &= ~DSI_DATA; /* thats no data in the sense we use it in alarm_handler */
590 LOG(log_debug, logtype_afpd, "DSI: client tickle");
591 /* timer is not every 30 seconds anymore, so we don't get killed on the client side. */
592 if ((dsi->flags & DSI_DIE))
599 if ( flushfork( writtenfork ) < 0 ) {
600 LOG(log_error, logtype_afpd, "main flushfork: %s", strerror(errno) );
606 function = (u_char) dsi->commands[0];
608 /* AFP replay cache */
609 rc_idx = dsi->clientID % REPLAYCACHE_SIZE;
610 LOG(log_debug, logtype_dsi, "DSI request ID: %u", dsi->clientID);
612 if (replaycache[rc_idx].DSIreqID == dsi->clientID
613 && replaycache[rc_idx].AFPcommand == function) {
614 LOG(log_note, logtype_afpd, "AFP Replay Cache match: id: %u / cmd: %s",
615 dsi->clientID, AfpNum2name(function));
616 err = replaycache[rc_idx].result;
617 /* AFP replay cache end */
619 /* send off an afp command. in a couple cases, we take advantage
620 * of the fact that we're a stream-based protocol. */
621 if (afp_switch[function]) {
622 dsi->datalen = DSI_DATASIZ;
623 dsi->flags |= DSI_RUNNING;
625 LOG(log_debug, logtype_afpd, "<== Start AFP command: %s", AfpNum2name(function));
627 err = (*afp_switch[function])(obj,
628 (char *)&dsi->commands, dsi->cmdlen,
629 (char *)&dsi->data, &dsi->datalen);
631 LOG(log_debug, logtype_afpd, "==> Finished AFP command: %s -> %s",
632 AfpNum2name(function), AfpErr2name(err));
634 dir_free_invalid_q();
637 /* bring everything back to old euid, egid */
639 restore_uidgid ( &obj->uidgid );
640 #endif /* FORCE_UIDGID */
641 dsi->flags &= ~DSI_RUNNING;
643 /* Add result to the AFP replay cache */
644 replaycache[rc_idx].DSIreqID = dsi->clientID;
645 replaycache[rc_idx].AFPcommand = function;
646 replaycache[rc_idx].result = err;
648 LOG(log_error, logtype_afpd, "bad function %X", function);
654 /* single shot toggle that gets set by dsi_readinit. */
655 if (dsi->flags & DSI_NOREPLY) {
656 dsi->flags &= ~DSI_NOREPLY;
658 } else if (!dsi_cmdreply(dsi, err)) {
659 LOG(log_error, logtype_afpd, "dsi_cmdreply(%d): %s", dsi->socket, strerror(errno) );
660 if (dsi_disconnect(dsi) != 0)
661 afp_dsi_die(EXITERR_CLNT);
665 case DSIFUNC_WRITE: /* FPWrite and FPAddIcon */
666 function = (u_char) dsi->commands[0];
667 if ( afp_switch[ function ] != NULL ) {
668 dsi->datalen = DSI_DATASIZ;
669 dsi->flags |= DSI_RUNNING;
671 LOG(log_debug, logtype_afpd, "<== Start AFP command: %s", AfpNum2name(function));
673 err = (*afp_switch[function])(obj,
674 (char *)&dsi->commands, dsi->cmdlen,
675 (char *)&dsi->data, &dsi->datalen);
677 LOG(log_debug, logtype_afpd, "==> Finished AFP command: %s -> %s",
678 AfpNum2name(function), AfpErr2name(err));
680 dsi->flags &= ~DSI_RUNNING;
682 /* bring everything back to old euid, egid */
684 restore_uidgid ( &obj->uidgid );
685 #endif /* FORCE_UIDGID */
687 LOG(log_error, logtype_afpd, "(write) bad function %x", function);
692 if (!dsi_wrtreply(dsi, err)) {
693 LOG(log_error, logtype_afpd, "dsi_wrtreply: %s", strerror(errno) );
694 if (dsi_disconnect(dsi) != 0)
695 afp_dsi_die(EXITERR_CLNT);
699 case DSIFUNC_ATTN: /* attention replies */
702 /* error. this usually implies a mismatch of some kind
703 * between server and client. if things are correct,
704 * we need to flush the rest of the packet if necessary. */
706 LOG(log_info, logtype_afpd,"afp_dsi: spurious command %d", cmd);
707 dsi_writeinit(dsi, dsi->data, DSI_DATASIZ);
711 pending_request(dsi);
713 fce_pending_events(obj);
717 afp_dsi_die(EXITERR_CLNT);