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>
31 #include <sys/types.h>
34 #include <atalk/logger.h>
35 #include <atalk/dsi.h>
36 #include <atalk/compat.h>
37 #include <atalk/util.h>
38 #include <atalk/uuid.h>
39 #include <atalk/paths.h>
40 #include <atalk/server_ipc.h>
41 #include <atalk/fce_api.h>
42 #include <atalk/globals.h>
43 #include <atalk/netatalk_conf.h>
44 #include <atalk/spotlight.h>
52 #define SOL_TCP IPPROTO_TCP
56 * We generally pass this from afp_over_dsi to all afp_* funcs, so it should already be
57 * available everywhere. Unfortunately some funcs (eg acltoownermode) need acces to it
58 * but are deeply nested in the function chain with the caller already without acces to it.
59 * Changing this would require adding a reference to the caller which itself might be
60 * called in many places (eg acltoownermode is called from accessmode).
61 * The only sane way out is providing a copy of it here:
63 AFPObj *AFPobj = NULL;
74 * - indexed just by taking DSIreqID mod REPLAYCACHE_SIZE
76 static rc_elem_t replaycache[REPLAYCACHE_SIZE];
78 static sigjmp_buf recon_jmp;
79 static void afp_dsi_close(AFPObj *obj)
87 /* we may have been called from a signal handler caught when afpd was running
88 * as uid 0, that's the wrong user for volume's prexec_close scripts if any,
89 * restore our login user
91 if (geteuid() != obj->uid) {
92 if (seteuid( obj->uid ) < 0) {
93 LOG(log_error, logtype_afpd, "can't seteuid(%u) back %s: uid: %u, euid: %u",
94 obj->uid, strerror(errno), getuid(), geteuid());
101 /* Block sigs, PAM/systemd/whoever might send us a SIG??? in (*obj->logout)() -> pam_close_session() */
103 pthread_sigmask(SIG_BLOCK, &sigs, NULL);
107 LOG(log_note, logtype_afpd, "AFP statistics: %.2f KB read, %.2f KB written",
108 dsi->read_count/1024.0, dsi->write_count/1024.0);
114 /* -------------------------------
116 * a little bit of code duplication.
118 static void afp_dsi_die(int sig)
120 DSI *dsi = (DSI *)AFPobj->dsi;
122 if (dsi->flags & DSI_RECONINPROG) {
123 /* Primary reconnect succeeded, got SIGTERM from afpd parent */
124 dsi->flags &= ~DSI_RECONINPROG;
125 return; /* this returns to afp_disconnect */
128 if (dsi->flags & DSI_DISCONNECTED) {
129 LOG(log_note, logtype_afpd, "Disconnected session terminating");
133 dsi_attention(AFPobj->dsi, AFPATTN_SHUTDOWN);
134 afp_dsi_close(AFPobj);
135 if (sig) /* if no signal, assume dieing because logins are disabled &
136 don't log it (maintenance mode)*/
137 LOG(log_info, logtype_afpd, "Connection terminated");
138 if (sig == SIGTERM || sig == SIGALRM) {
146 /* SIGURG handler (primary reconnect) */
147 static void afp_dsi_transfer_session(int sig _U_)
151 DSI *dsi = (DSI *)AFPobj->dsi;
153 LOG(log_debug, logtype_afpd, "afp_dsi_transfer_session: got SIGURG, trying to receive session");
155 if (readt(AFPobj->ipc_fd, &dsiID, 2, 0, 2) != 2) {
156 LOG(log_error, logtype_afpd, "afp_dsi_transfer_session: couldn't receive DSI id, goodbye");
157 afp_dsi_close(AFPobj);
161 if ((socket = recv_fd(AFPobj->ipc_fd, 1)) == -1) {
162 LOG(log_error, logtype_afpd, "afp_dsi_transfer_session: couldn't receive session fd, goodbye");
163 afp_dsi_close(AFPobj);
167 LOG(log_debug, logtype_afpd, "afp_dsi_transfer_session: received socket fd: %i", socket);
169 dsi->proto_close(dsi);
170 dsi->socket = socket;
171 dsi->flags = DSI_RECONSOCKET;
173 dsi->eof = dsi->start = dsi->buffer;
175 dsi->header.dsi_requestID = dsiID;
176 dsi->header.dsi_command = DSIFUNC_CMD;
179 * The session transfer happens in the middle of FPDisconnect old session, thus we
180 * have to send the reply now.
182 if (!dsi_cmdreply(dsi, AFP_OK)) {
183 LOG(log_error, logtype_afpd, "dsi_cmdreply: %s", strerror(errno) );
184 afp_dsi_close(AFPobj);
188 LOG(log_note, logtype_afpd, "afp_dsi_transfer_session: succesfull primary reconnect");
190 * Now returning from this signal handler return to dsi_receive which should start
191 * reading/continuing from the connected socket that was passed via the parent from
192 * another session. The parent will terminate that session.
194 siglongjmp(recon_jmp, 1);
197 /* ------------------- */
198 static void afp_dsi_timedown(int sig _U_)
202 DSI *dsi = (DSI *)AFPobj->dsi;
203 dsi->flags |= DSI_DIE;
204 /* shutdown and don't reconnect. server going down in 5 minutes. */
205 setmessage("The server is going down for maintenance.");
206 if (dsi_attention(AFPobj->dsi, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
207 AFPATTN_MESG | AFPATTN_TIME(5)) < 0) {
208 DSI *dsi = (DSI *)AFPobj->dsi;
209 dsi->down_request = 1;
212 it.it_interval.tv_sec = 0;
213 it.it_interval.tv_usec = 0;
214 it.it_value.tv_sec = 300;
215 it.it_value.tv_usec = 0;
217 if ( setitimer( ITIMER_REAL, &it, NULL ) < 0 ) {
218 LOG(log_error, logtype_afpd, "afp_timedown: setitimer: %s", strerror(errno) );
219 afp_dsi_die(EXITERR_SYS);
221 memset(&sv, 0, sizeof(sv));
222 sv.sa_handler = afp_dsi_die;
223 sigemptyset( &sv.sa_mask );
224 sigaddset(&sv.sa_mask, SIGHUP);
225 sigaddset(&sv.sa_mask, SIGTERM);
226 sv.sa_flags = SA_RESTART;
227 if ( sigaction( SIGALRM, &sv, NULL ) < 0 ) {
228 LOG(log_error, logtype_afpd, "afp_timedown: sigaction: %s", strerror(errno) );
229 afp_dsi_die(EXITERR_SYS);
233 sv.sa_handler = SIG_IGN;
234 sigemptyset( &sv.sa_mask );
235 sv.sa_flags = SA_RESTART;
236 if ( sigaction( SIGUSR1, &sv, NULL ) < 0 ) {
237 LOG(log_error, logtype_afpd, "afp_timedown: sigaction SIGHUP: %s", strerror(errno) );
238 afp_dsi_die(EXITERR_SYS);
242 /* ---------------------------------
243 * SIGHUP reload configuration file
245 volatile int reload_request = 0;
247 static void afp_dsi_reload(int sig _U_)
252 /* ---------------------------------
253 * SIGINT: enable max_debug LOGging
255 static volatile sig_atomic_t debug_request = 0;
257 static void afp_dsi_debug(int sig _U_)
262 /* ---------------------- */
263 static void afp_dsi_getmesg (int sig _U_)
265 DSI *dsi = (DSI *)AFPobj->dsi;
267 dsi->msg_request = 1;
268 if (dsi_attention(AFPobj->dsi, AFPATTN_MESG | AFPATTN_TIME(5)) < 0)
269 dsi->msg_request = 2;
272 static void alarm_handler(int sig _U_)
275 DSI *dsi = (DSI *)AFPobj->dsi;
277 /* we have to restart the timer because some libraries may use alarm() */
278 setitimer(ITIMER_REAL, &dsi->timer, NULL);
280 /* we got some traffic from the client since the previous timer tick. */
281 if ((dsi->flags & DSI_DATA)) {
282 dsi->flags &= ~DSI_DATA;
287 LOG(log_maxdebug, logtype_afpd, "alarm: tickles: %u, flags: %s|%s|%s|%s|%s|%s|%s|%s|%s",
289 (dsi->flags & DSI_DATA) ? "DSI_DATA" : "-",
290 (dsi->flags & DSI_RUNNING) ? "DSI_RUNNING" : "-",
291 (dsi->flags & DSI_SLEEPING) ? "DSI_SLEEPING" : "-",
292 (dsi->flags & DSI_EXTSLEEP) ? "DSI_EXTSLEEP" : "-",
293 (dsi->flags & DSI_DISCONNECTED) ? "DSI_DISCONNECTED" : "-",
294 (dsi->flags & DSI_DIE) ? "DSI_DIE" : "-",
295 (dsi->flags & DSI_NOREPLY) ? "DSI_NOREPLY" : "-",
296 (dsi->flags & DSI_RECONSOCKET) ? "DSI_RECONSOCKET" : "-",
297 (dsi->flags & DSI_RECONINPROG) ? "DSI_RECONINPROG" : "-");
299 if (dsi->flags & DSI_SLEEPING) {
300 if (dsi->tickle > AFPobj->options.sleep) {
301 LOG(log_note, logtype_afpd, "afp_alarm: sleep time ended");
302 afp_dsi_die(EXITERR_CLNT);
307 if (dsi->flags & DSI_DISCONNECTED) {
308 if (geteuid() == 0) {
309 LOG(log_note, logtype_afpd, "afp_alarm: unauthenticated user, connection problem");
310 afp_dsi_die(EXITERR_CLNT);
312 if (dsi->tickle > AFPobj->options.disconnected) {
313 LOG(log_error, logtype_afpd, "afp_alarm: reconnect timer expired, goodbye");
314 afp_dsi_die(EXITERR_CLNT);
319 /* if we're in the midst of processing something, don't die. */
320 if (dsi->tickle >= AFPobj->options.timeout) {
321 LOG(log_error, logtype_afpd, "afp_alarm: child timed out, entering disconnected state");
322 if (dsi_disconnect(dsi) != 0)
323 afp_dsi_die(EXITERR_CLNT);
327 if ((err = pollvoltime(AFPobj)) == 0) {
328 LOG(log_debug, logtype_afpd, "afp_alarm: sending DSI tickle");
329 err = dsi_tickle(AFPobj->dsi);
332 if (geteuid() == 0) {
333 LOG(log_note, logtype_afpd, "afp_alarm: unauthenticated user, connection problem");
334 afp_dsi_die(EXITERR_CLNT);
336 LOG(log_error, logtype_afpd, "afp_alarm: connection problem, entering disconnected state");
337 if (dsi_disconnect(dsi) != 0)
338 afp_dsi_die(EXITERR_CLNT);
342 static void child_handler(int sig _U_)
348 if dsi->in_write is set attention, tickle (and close?) msg
349 aren't sent. We don't care about tickle
351 static void pending_request(DSI *dsi)
353 /* send pending attention */
355 /* read msg if any, it could be done in afp_getsrvrmesg */
356 if (dsi->msg_request) {
357 if (dsi->msg_request == 2) {
358 /* didn't send it in signal handler */
359 dsi_attention(AFPobj->dsi, AFPATTN_MESG | AFPATTN_TIME(5));
361 dsi->msg_request = 0;
364 if (dsi->down_request) {
365 dsi->down_request = 0;
366 dsi_attention(AFPobj->dsi, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
367 AFPATTN_MESG | AFPATTN_TIME(5));
371 void afp_over_dsi_sighandlers(AFPObj *obj)
373 DSI *dsi = (DSI *) obj->dsi;
374 struct sigaction action;
376 memset(&action, 0, sizeof(action));
377 sigfillset(&action.sa_mask);
378 action.sa_flags = SA_RESTART;
381 action.sa_handler = afp_dsi_reload;
382 if ( sigaction( SIGHUP, &action, NULL ) < 0 ) {
383 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
384 afp_dsi_die(EXITERR_SYS);
388 action.sa_handler = afp_dsi_transfer_session;
389 if ( sigaction( SIGURG, &action, NULL ) < 0 ) {
390 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
391 afp_dsi_die(EXITERR_SYS);
394 /* install SIGTERM */
395 action.sa_handler = afp_dsi_die;
396 if ( sigaction( SIGTERM, &action, NULL ) < 0 ) {
397 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
398 afp_dsi_die(EXITERR_SYS);
401 /* install SIGQUIT */
402 action.sa_handler = afp_dsi_die;
403 if ( sigaction(SIGQUIT, &action, NULL ) < 0 ) {
404 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
405 afp_dsi_die(EXITERR_SYS);
408 /* SIGUSR2 - server message support */
409 action.sa_handler = afp_dsi_getmesg;
410 if ( sigaction( SIGUSR2, &action, NULL) < 0 ) {
411 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
412 afp_dsi_die(EXITERR_SYS);
415 /* SIGUSR1 - set down in 5 minutes */
416 action.sa_handler = afp_dsi_timedown;
417 action.sa_flags = SA_RESTART;
418 if ( sigaction( SIGUSR1, &action, NULL) < 0 ) {
419 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
420 afp_dsi_die(EXITERR_SYS);
423 /* SIGINT - enable max_debug LOGging to /tmp/afpd.PID.XXXXXX */
424 action.sa_handler = afp_dsi_debug;
425 if ( sigaction( SIGINT, &action, NULL) < 0 ) {
426 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
427 afp_dsi_die(EXITERR_SYS);
431 /* SIGALRM - tickle handler */
432 action.sa_handler = alarm_handler;
433 if ((sigaction(SIGALRM, &action, NULL) < 0) ||
434 (setitimer(ITIMER_REAL, &dsi->timer, NULL) < 0)) {
435 afp_dsi_die(EXITERR_SYS);
437 #endif /* DEBUGGING */
440 action.sa_handler = child_handler;
442 /* this enhancement simplifies things for Solaris, it also improves performance */
443 action.sa_flags |= SA_NOCLDWAIT;
445 if (sigaction(SIGCLD, &action, NULL) < 0 ) {
446 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
447 afp_dsi_die(EXITERR_SYS);
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) && (obj->options.slmod_path))
500 ipc_child_state(obj, DSI_RUNNING);
502 /* get stuck here until the end */
504 if (sigsetjmp(recon_jmp, 1) != 0)
505 /* returning from SIGALARM handler for a primary reconnect */
508 /* Blocking read on the network socket */
509 cmd = dsi_stream_receive(dsi);
512 /* cmd == 0 is the error condition */
513 if (dsi->flags & DSI_RECONSOCKET) {
514 /* we just got a reconnect so we immediately try again to receive on the new fd */
515 dsi->flags &= ~DSI_RECONSOCKET;
519 /* the client sometimes logs out (afp_logout) but doesn't close the DSI session */
520 if (dsi->flags & DSI_AFP_LOGGED_OUT) {
521 LOG(log_note, logtype_afpd, "afp_over_dsi: client logged out, terminating DSI session");
526 if (dsi->flags & DSI_RECONINPROG) {
527 LOG(log_note, logtype_afpd, "afp_over_dsi: failed reconnect");
532 /* Some error on the client connection, enter disconnected state */
533 if (dsi_disconnect(dsi) != 0)
534 afp_dsi_die(EXITERR_CLNT);
536 ipc_child_state(obj, DSI_DISCONNECTED);
538 while (dsi->flags & DSI_DISCONNECTED)
539 pause(); /* gets interrupted by SIGALARM or SIGURG tickle */
540 ipc_child_state(obj, DSI_RUNNING);
541 continue; /* continue receiving until disconnect timer expires
542 * or a primary reconnect succeeds */
545 if (!(dsi->flags & DSI_EXTSLEEP) && (dsi->flags & DSI_SLEEPING)) {
546 LOG(log_debug, logtype_afpd, "afp_over_dsi: got data, ending normal sleep");
547 dsi->flags &= ~DSI_SLEEPING;
549 ipc_child_state(obj, DSI_RUNNING);
552 if (reload_request) {
554 load_volumes(AFPobj, lv_none);
557 /* The first SIGINT enables debugging, the next restores the config */
559 static int debugging = 0;
566 if (obj->options.logconfig)
567 setuplog(obj->options.logconfig, obj->options.logfile);
569 setuplog("default:note", NULL);
574 sprintf(logstr, "/tmp/afpd.%u.XXXXXX", getpid());
575 setuplog("default:maxdebug", logstr);
580 dsi->flags |= DSI_DATA;
586 LOG(log_debug, logtype_afpd, "DSI: close session request");
588 LOG(log_note, logtype_afpd, "done");
592 dsi->flags &= ~DSI_DATA; /* thats no data in the sense we use it in alarm_handler */
593 LOG(log_debug, logtype_afpd, "DSI: client tickle");
594 /* timer is not every 30 seconds anymore, so we don't get killed on the client side. */
595 if ((dsi->flags & DSI_DIE))
602 if ( flushfork( writtenfork ) < 0 ) {
603 LOG(log_error, logtype_afpd, "main flushfork: %s", strerror(errno) );
609 function = (u_char) dsi->commands[0];
611 /* AFP replay cache */
612 rc_idx = dsi->clientID % REPLAYCACHE_SIZE;
613 LOG(log_debug, logtype_dsi, "DSI request ID: %u", dsi->clientID);
615 if (replaycache[rc_idx].DSIreqID == dsi->clientID
616 && replaycache[rc_idx].AFPcommand == function) {
617 LOG(log_note, logtype_afpd, "AFP Replay Cache match: id: %u / cmd: %s",
618 dsi->clientID, AfpNum2name(function));
619 err = replaycache[rc_idx].result;
620 /* AFP replay cache end */
622 /* send off an afp command. in a couple cases, we take advantage
623 * of the fact that we're a stream-based protocol. */
624 if (afp_switch[function]) {
625 dsi->datalen = DSI_DATASIZ;
626 dsi->flags |= DSI_RUNNING;
628 LOG(log_debug, logtype_afpd, "<== Start AFP command: %s", AfpNum2name(function));
630 AFP_AFPFUNC_START(function, (char *)AfpNum2name(function));
631 err = (*afp_switch[function])(obj,
632 (char *)dsi->commands, dsi->cmdlen,
633 (char *)&dsi->data, &dsi->datalen);
635 AFP_AFPFUNC_DONE(function, (char *)AfpNum2name(function));
636 LOG(log_debug, logtype_afpd, "==> Finished AFP command: %s -> %s",
637 AfpNum2name(function), AfpErr2name(err));
639 dir_free_invalid_q();
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 AFP_AFPFUNC_START(function, (char *)AfpNum2name(function));
675 err = (*afp_switch[function])(obj,
676 (char *)dsi->commands, dsi->cmdlen,
677 (char *)&dsi->data, &dsi->datalen);
679 AFP_AFPFUNC_DONE(function, (char *)AfpNum2name(function));
681 LOG(log_debug, logtype_afpd, "==> Finished AFP command: %s -> %s",
682 AfpNum2name(function), AfpErr2name(err));
684 dsi->flags &= ~DSI_RUNNING;
686 LOG(log_error, logtype_afpd, "(write) bad function %x", function);
691 if (!dsi_wrtreply(dsi, err)) {
692 LOG(log_error, logtype_afpd, "dsi_wrtreply: %s", strerror(errno) );
693 if (dsi_disconnect(dsi) != 0)
694 afp_dsi_die(EXITERR_CLNT);
698 case DSIFUNC_ATTN: /* attention replies */
701 /* error. this usually implies a mismatch of some kind
702 * between server and client. if things are correct,
703 * we need to flush the rest of the packet if necessary. */
705 LOG(log_info, logtype_afpd,"afp_dsi: spurious command %d", cmd);
706 dsi_writeinit(dsi, dsi->data, DSI_DATASIZ);
710 pending_request(dsi);
712 fce_pending_events(obj);
716 afp_dsi_die(EXITERR_CLNT);