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 <arpa/inet.h>
28 #include <atalk/logger.h>
31 #include <atalk/dsi.h>
32 #include <atalk/compat.h>
33 #include <atalk/util.h>
34 #include <atalk/uuid.h>
35 #include <atalk/paths.h>
36 #include <atalk/server_ipc.h>
47 #endif /* FORCE_UIDGID */
50 * We generally pass this from afp_over_dsi to all afp_* funcs, so it should already be
51 * available everywhere. Unfortunately some funcs (eg acltoownermode) need acces to it
52 * but are deeply nested in the function chain with the caller already without acces to it.
53 * Changing this would require adding a reference to the caller which itself might be
54 * called in many places (eg acltoownermode is called from accessmode).
55 * The only sane way out is providing a copy of it here:
57 AFPObj *AFPobj = NULL;
68 * - indexed just by taking DSIreqID mod REPLAYCACHE_SIZE
70 static rc_elem_t replaycache[REPLAYCACHE_SIZE];
72 static sigjmp_buf recon_jmp;
73 static void afp_dsi_close(AFPObj *obj)
75 DSI *dsi = obj->handle;
80 /* we may have been called from a signal handler caught when afpd was running
81 * as uid 0, that's the wrong user for volume's prexec_close scripts if any,
82 * restore our login user
84 if (geteuid() != obj->uid) {
85 if (seteuid( obj->uid ) < 0) {
86 LOG(log_error, logtype_afpd, "can't seteuid(%u) back %s: uid: %u, euid: %u",
87 obj->uid, strerror(errno), getuid(), geteuid());
96 LOG(log_note, logtype_afpd, "AFP statistics: %.2f KB read, %.2f KB written",
97 dsi->read_count/1024.0, dsi->write_count/1024.0);
103 /* -------------------------------
105 * a little bit of code duplication.
107 static void afp_dsi_die(int sig)
109 DSI *dsi = (DSI *)AFPobj->handle;
111 if (dsi->flags & DSI_RECONINPROG) {
112 /* Primary reconnect succeeded, got SIGTERM from afpd parent */
113 dsi->flags &= ~DSI_RECONINPROG;
114 return; /* this returns to afp_disconnect */
117 if (dsi->flags & DSI_DISCONNECTED) {
118 LOG(log_note, logtype_afpd, "Disconnected session terminating");
122 dsi_attention(AFPobj->handle, AFPATTN_SHUTDOWN);
123 afp_dsi_close(AFPobj);
124 if (sig) /* if no signal, assume dieing because logins are disabled &
125 don't log it (maintenance mode)*/
126 LOG(log_info, logtype_afpd, "Connection terminated");
127 if (sig == SIGTERM || sig == SIGALRM) {
135 /* SIGURG handler (primary reconnect) */
136 static void afp_dsi_transfer_session(int sig _U_)
140 DSI *dsi = (DSI *)AFPobj->handle;
142 LOG(log_debug, logtype_afpd, "afp_dsi_transfer_session: got SIGURG, trying to receive session");
144 if (readt(AFPobj->ipc_fd, &dsiID, 2, 0, 2) != 2) {
145 LOG(log_error, logtype_afpd, "afp_dsi_transfer_session: couldn't receive DSI id, goodbye");
146 afp_dsi_close(AFPobj);
150 if ((socket = recv_fd(AFPobj->ipc_fd, 1)) == -1) {
151 LOG(log_error, logtype_afpd, "afp_dsi_transfer_session: couldn't receive session fd, goodbye");
152 afp_dsi_close(AFPobj);
156 LOG(log_debug, logtype_afpd, "afp_dsi_transfer_session: received socket fd: %i", socket);
158 dsi->proto_close(dsi);
159 dsi->socket = socket;
160 dsi->flags = DSI_RECONSOCKET;
162 dsi->eof = dsi->start = dsi->buffer;
164 dsi->header.dsi_requestID = dsiID;
165 dsi->header.dsi_command = DSIFUNC_CMD;
168 * The session transfer happens in the middle of FPDisconnect old session, thus we
169 * have to send the reply now.
171 if (!dsi_cmdreply(dsi, AFP_OK)) {
172 LOG(log_error, logtype_afpd, "dsi_cmdreply: %s", strerror(errno) );
173 afp_dsi_close(AFPobj);
177 LOG(log_note, logtype_afpd, "afp_dsi_transfer_session: succesfull primary reconnect");
179 * Now returning from this signal handler return to dsi_receive which should start
180 * reading/continuing from the connected socket that was passed via the parent from
181 * another session. The parent will terminate that session.
183 siglongjmp(recon_jmp, 1);
186 /* ------------------- */
187 static void afp_dsi_timedown(int sig _U_)
191 DSI *dsi = (DSI *)AFPobj->handle;
192 dsi->flags |= DSI_DIE;
193 /* shutdown and don't reconnect. server going down in 5 minutes. */
194 setmessage("The server is going down for maintenance.");
195 if (dsi_attention(AFPobj->handle, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
196 AFPATTN_MESG | AFPATTN_TIME(5)) < 0) {
197 DSI *dsi = (DSI *)AFPobj->handle;
198 dsi->down_request = 1;
201 it.it_interval.tv_sec = 0;
202 it.it_interval.tv_usec = 0;
203 it.it_value.tv_sec = 300;
204 it.it_value.tv_usec = 0;
206 if ( setitimer( ITIMER_REAL, &it, NULL ) < 0 ) {
207 LOG(log_error, logtype_afpd, "afp_timedown: setitimer: %s", strerror(errno) );
208 afp_dsi_die(EXITERR_SYS);
210 memset(&sv, 0, sizeof(sv));
211 sv.sa_handler = afp_dsi_die;
212 sigemptyset( &sv.sa_mask );
213 sigaddset(&sv.sa_mask, SIGHUP);
214 sigaddset(&sv.sa_mask, SIGTERM);
215 sv.sa_flags = SA_RESTART;
216 if ( sigaction( SIGALRM, &sv, NULL ) < 0 ) {
217 LOG(log_error, logtype_afpd, "afp_timedown: sigaction: %s", strerror(errno) );
218 afp_dsi_die(EXITERR_SYS);
222 sv.sa_handler = SIG_IGN;
223 sigemptyset( &sv.sa_mask );
224 sv.sa_flags = SA_RESTART;
225 if ( sigaction( SIGUSR1, &sv, NULL ) < 0 ) {
226 LOG(log_error, logtype_afpd, "afp_timedown: sigaction SIGHUP: %s", strerror(errno) );
227 afp_dsi_die(EXITERR_SYS);
231 /* ---------------------------------
232 * SIGHUP reload configuration file
234 volatile int reload_request = 0;
236 static void afp_dsi_reload(int sig _U_)
241 /* ---------------------------------
242 * SIGINT: enable max_debug LOGging
244 static volatile sig_atomic_t debug_request = 0;
246 static void afp_dsi_debug(int sig _U_)
251 /* ---------------------- */
252 static void afp_dsi_getmesg (int sig _U_)
254 DSI *dsi = (DSI *)AFPobj->handle;
256 dsi->msg_request = 1;
257 if (dsi_attention(AFPobj->handle, AFPATTN_MESG | AFPATTN_TIME(5)) < 0)
258 dsi->msg_request = 2;
261 static void alarm_handler(int sig _U_)
264 DSI *dsi = (DSI *)AFPobj->handle;
266 /* we have to restart the timer because some libraries may use alarm() */
267 setitimer(ITIMER_REAL, &dsi->timer, NULL);
269 /* we got some traffic from the client since the previous timer tick. */
270 if ((dsi->flags & DSI_DATA)) {
271 dsi->flags &= ~DSI_DATA;
276 LOG(log_maxdebug, logtype_afpd, "alarm: tickles: %u, flags: %s|%s|%s|%s|%s|%s|%s|%s|%s",
278 (dsi->flags & DSI_DATA) ? "DSI_DATA" : "-",
279 (dsi->flags & DSI_RUNNING) ? "DSI_RUNNING" : "-",
280 (dsi->flags & DSI_SLEEPING) ? "DSI_SLEEPING" : "-",
281 (dsi->flags & DSI_EXTSLEEP) ? "DSI_EXTSLEEP" : "-",
282 (dsi->flags & DSI_DISCONNECTED) ? "DSI_DISCONNECTED" : "-",
283 (dsi->flags & DSI_DIE) ? "DSI_DIE" : "-",
284 (dsi->flags & DSI_NOREPLY) ? "DSI_NOREPLY" : "-",
285 (dsi->flags & DSI_RECONSOCKET) ? "DSI_RECONSOCKET" : "-",
286 (dsi->flags & DSI_RECONINPROG) ? "DSI_RECONINPROG" : "-");
288 if (dsi->flags & DSI_SLEEPING) {
289 if (dsi->tickle > AFPobj->options.sleep) {
290 LOG(log_note, logtype_afpd, "afp_alarm: sleep time ended");
291 afp_dsi_die(EXITERR_CLNT);
296 if (dsi->flags & DSI_DISCONNECTED) {
297 if (geteuid() == 0) {
298 LOG(log_note, logtype_afpd, "afp_alarm: unauthenticated user, connection problem");
299 afp_dsi_die(EXITERR_CLNT);
301 if (dsi->tickle > AFPobj->options.disconnected) {
302 LOG(log_error, logtype_afpd, "afp_alarm: reconnect timer expired, goodbye");
303 afp_dsi_die(EXITERR_CLNT);
308 /* if we're in the midst of processing something, don't die. */
309 if ( !(dsi->flags & DSI_RUNNING) && (dsi->tickle >= AFPobj->options.timeout)) {
310 LOG(log_error, logtype_afpd, "afp_alarm: child timed out, entering disconnected state");
311 if (dsi_disconnect(dsi) != 0)
312 afp_dsi_die(EXITERR_CLNT);
316 if ((err = pollvoltime(AFPobj)) == 0)
317 LOG(log_debug, logtype_afpd, "afp_alarm: sending DSI tickle");
318 err = dsi_tickle(AFPobj->handle);
320 if (geteuid() == 0) {
321 LOG(log_note, logtype_afpd, "afp_alarm: unauthenticated user, connection problem");
322 afp_dsi_die(EXITERR_CLNT);
324 LOG(log_error, logtype_afpd, "afp_alarm: connection problem, entering disconnected state");
325 if (dsi_disconnect(dsi) != 0)
326 afp_dsi_die(EXITERR_CLNT);
331 if dsi->in_write is set attention, tickle (and close?) msg
332 aren't sent. We don't care about tickle
334 static void pending_request(DSI *dsi)
336 /* send pending attention */
338 /* read msg if any, it could be done in afp_getsrvrmesg */
339 if (dsi->msg_request) {
340 if (dsi->msg_request == 2) {
341 /* didn't send it in signal handler */
342 dsi_attention(AFPobj->handle, AFPATTN_MESG | AFPATTN_TIME(5));
344 dsi->msg_request = 0;
347 if (dsi->down_request) {
348 dsi->down_request = 0;
349 dsi_attention(AFPobj->handle, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
350 AFPATTN_MESG | AFPATTN_TIME(5));
354 /* -------------------------------------------
355 afp over dsi. this never returns.
357 void afp_over_dsi(AFPObj *obj)
359 DSI *dsi = (DSI *) obj->handle;
363 struct sigaction action;
364 struct pollfd pollfds[1];
367 obj->exit = afp_dsi_die;
368 obj->reply = (int (*)()) dsi_cmdreply;
369 obj->attention = (int (*)(void *, AFPUserBytes)) dsi_attention;
372 pollfds[0].fd = obj->ipc_fd;
373 pollfds[0].events = POLLOUT;
375 memset(&action, 0, sizeof(action));
378 action.sa_handler = afp_dsi_reload;
379 sigemptyset( &action.sa_mask );
380 sigaddset(&action.sa_mask, SIGALRM);
381 sigaddset(&action.sa_mask, SIGTERM);
382 sigaddset(&action.sa_mask, SIGUSR1);
383 sigaddset(&action.sa_mask, SIGINT);
384 sigaddset(&action.sa_mask, SIGUSR2);
385 action.sa_flags = SA_RESTART;
386 if ( sigaction( SIGHUP, &action, NULL ) < 0 ) {
387 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
388 afp_dsi_die(EXITERR_SYS);
392 action.sa_handler = afp_dsi_transfer_session;
393 sigemptyset( &action.sa_mask );
394 sigaddset(&action.sa_mask, SIGALRM);
395 sigaddset(&action.sa_mask, SIGTERM);
396 sigaddset(&action.sa_mask, SIGUSR1);
397 sigaddset(&action.sa_mask, SIGINT);
398 sigaddset(&action.sa_mask, SIGUSR2);
399 action.sa_flags = SA_RESTART;
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 sigemptyset( &action.sa_mask );
408 sigaddset(&action.sa_mask, SIGALRM);
409 sigaddset(&action.sa_mask, SIGHUP);
410 sigaddset(&action.sa_mask, SIGUSR1);
411 sigaddset(&action.sa_mask, SIGINT);
412 sigaddset(&action.sa_mask, SIGUSR2);
413 action.sa_flags = SA_RESTART;
414 if ( sigaction( SIGTERM, &action, NULL ) < 0 ) {
415 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
416 afp_dsi_die(EXITERR_SYS);
419 /* Added for server message support */
420 action.sa_handler = afp_dsi_getmesg;
421 sigemptyset( &action.sa_mask );
422 sigaddset(&action.sa_mask, SIGALRM);
423 sigaddset(&action.sa_mask, SIGTERM);
424 sigaddset(&action.sa_mask, SIGUSR1);
425 sigaddset(&action.sa_mask, SIGHUP);
426 sigaddset(&action.sa_mask, SIGINT);
427 action.sa_flags = SA_RESTART;
428 if ( sigaction( SIGUSR2, &action, NULL) < 0 ) {
429 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
430 afp_dsi_die(EXITERR_SYS);
433 /* SIGUSR1 - set down in 5 minutes */
434 action.sa_handler = afp_dsi_timedown;
435 sigemptyset( &action.sa_mask );
436 sigaddset(&action.sa_mask, SIGALRM);
437 sigaddset(&action.sa_mask, SIGHUP);
438 sigaddset(&action.sa_mask, SIGTERM);
439 sigaddset(&action.sa_mask, SIGINT);
440 sigaddset(&action.sa_mask, SIGUSR2);
441 action.sa_flags = SA_RESTART;
442 if ( sigaction( SIGUSR1, &action, NULL) < 0 ) {
443 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
444 afp_dsi_die(EXITERR_SYS);
447 /* SIGINT - enable max_debug LOGging to /tmp/afpd.PID.XXXXXX */
448 action.sa_handler = afp_dsi_debug;
449 sigfillset( &action.sa_mask );
450 action.sa_flags = SA_RESTART;
451 if ( sigaction( SIGINT, &action, NULL) < 0 ) {
452 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
453 afp_dsi_die(EXITERR_SYS);
458 action.sa_handler = alarm_handler;
459 sigemptyset(&action.sa_mask);
460 sigaddset(&action.sa_mask, SIGHUP);
461 sigaddset(&action.sa_mask, SIGTERM);
462 sigaddset(&action.sa_mask, SIGUSR1);
463 sigaddset(&action.sa_mask, SIGINT);
464 sigaddset(&action.sa_mask, SIGUSR2);
465 action.sa_flags = SA_RESTART;
466 if ((sigaction(SIGALRM, &action, NULL) < 0) ||
467 (setitimer(ITIMER_REAL, &dsi->timer, NULL) < 0)) {
468 afp_dsi_die(EXITERR_SYS);
470 #endif /* DEBUGGING */
472 if (dircache_init(obj->options.dircachesize) != 0)
473 afp_dsi_die(EXITERR_SYS);
475 /* set TCP snd/rcv buf */
476 if (obj->options.tcp_rcvbuf) {
477 if (setsockopt(dsi->socket,
480 &obj->options.tcp_rcvbuf,
481 sizeof(obj->options.tcp_rcvbuf)) != 0) {
482 LOG(log_error, logtype_dsi, "afp_over_dsi: setsockopt(SO_RCVBUF): %s", strerror(errno));
485 if (obj->options.tcp_sndbuf) {
486 if (setsockopt(dsi->socket,
489 &obj->options.tcp_sndbuf,
490 sizeof(obj->options.tcp_sndbuf)) != 0) {
491 LOG(log_error, logtype_dsi, "afp_over_dsi: setsockopt(SO_SNDBUF): %s", strerror(errno));
495 /* get stuck here until the end */
497 if (sigsetjmp(recon_jmp, 1) != 0)
498 /* returning from SIGALARM handler for a primary reconnect */
501 /* Blocking read on the network socket */
502 cmd = dsi_receive(dsi);
505 /* cmd == 0 is the error condition */
506 if (dsi->flags & DSI_RECONSOCKET) {
507 /* we just got a reconnect so we immediately try again to receive on the new fd */
508 dsi->flags &= ~DSI_RECONSOCKET;
511 /* Some error on the client connection, enter disconnected state */
512 if (dsi_disconnect(dsi) != 0)
513 afp_dsi_die(EXITERR_CLNT);
515 /* the client sometimes logs out (afp_logout) but doesn't close the DSI session */
516 if (dsi->flags & DSI_AFP_LOGGED_OUT) {
521 pause(); /* gets interrupted by SIGALARM or SIGURG tickle */
522 continue; /* continue receiving until disconnect timer expires
523 * or a primary reconnect succeeds */
526 static int saved_ipcfd = -1;
527 if (saved_ipcfd == -1)
528 saved_ipcfd = obj->ipc_fd;
529 if (poll(pollfds, 1, 0) == 1) {
530 if (pollfds[0].revents & (POLLHUP | POLLERR)) {
531 if (saved_ipcfd == obj->ipc_fd && getppid() == 1) {
533 sleep(30); /* give it enough time to start */
534 if ((obj->ipc_fd = ipc_client_uds(_PATH_AFP_IPC)) == -1) {
535 LOG(log_error, logtype_afpd, "afp_over_dsi: cant reconnect to master");
536 afp_dsi_die(EXITERR_SYS);
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_afpd, "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;
660 if (!dsi_cmdreply(dsi, err)) {
661 LOG(log_error, logtype_afpd, "dsi_cmdreply(%d): %s", dsi->socket, strerror(errno) );
662 if (dsi_disconnect(dsi) != 0)
663 afp_dsi_die(EXITERR_CLNT);
667 case DSIFUNC_WRITE: /* FPWrite and FPAddIcon */
668 function = (u_char) dsi->commands[0];
669 if ( afp_switch[ function ] != NULL ) {
670 dsi->datalen = DSI_DATASIZ;
671 dsi->flags |= DSI_RUNNING;
673 LOG(log_debug, logtype_afpd, "<== Start AFP command: %s", AfpNum2name(function));
675 err = (*afp_switch[function])(obj,
676 (char *)&dsi->commands, dsi->cmdlen,
677 (char *)&dsi->data, &dsi->datalen);
679 LOG(log_debug, logtype_afpd, "==> Finished AFP command: %s -> %s",
680 AfpNum2name(function), AfpErr2name(err));
682 dsi->flags &= ~DSI_RUNNING;
684 /* bring everything back to old euid, egid */
686 restore_uidgid ( &obj->uidgid );
687 #endif /* FORCE_UIDGID */
689 LOG(log_error, logtype_afpd, "(write) bad function %x", function);
694 if (!dsi_wrtreply(dsi, err)) {
695 LOG(log_error, logtype_afpd, "dsi_wrtreply: %s", strerror(errno) );
696 if (dsi_disconnect(dsi) != 0)
697 afp_dsi_die(EXITERR_CLNT);
701 case DSIFUNC_ATTN: /* attention replies */
704 /* error. this usually implies a mismatch of some kind
705 * between server and client. if things are correct,
706 * we need to flush the rest of the packet if necessary. */
708 LOG(log_info, logtype_afpd,"afp_dsi: spurious command %d", cmd);
709 dsi_writeinit(dsi, dsi->data, DSI_DATASIZ);
713 pending_request(dsi);
717 afp_dsi_die(EXITERR_CLNT);