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>
31 #include <atalk/logger.h>
32 #include <atalk/dsi.h>
33 #include <atalk/compat.h>
34 #include <atalk/util.h>
35 #include <atalk/locking.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>
48 * We generally pass this from afp_over_dsi to all afp_* funcs, so it should already be
49 * available everywhere. Unfortunately some funcs (eg acltoownermode) need acces to it
50 * but are deeply nested in the function chain with the caller already without acces to it.
51 * Changing this would require adding a reference to the caller which itself might be
52 * called in many places (eg acltoownermode is called from accessmode).
53 * The only sane way out is providing a copy of it here:
55 AFPObj *AFPobj = NULL;
66 * - indexed just by taking DSIreqID mod REPLAYCACHE_SIZE
68 static rc_elem_t replaycache[REPLAYCACHE_SIZE];
70 static sigjmp_buf recon_jmp;
71 static void afp_dsi_close(AFPObj *obj)
73 DSI *dsi = obj->handle;
78 /* we may have been called from a signal handler caught when afpd was running
79 * as uid 0, that's the wrong user for volume's prexec_close scripts if any,
80 * restore our login user
82 if (geteuid() != obj->uid) {
83 if (seteuid( obj->uid ) < 0) {
84 LOG(log_error, logtype_afpd, "can't seteuid(%u) back %s: uid: %u, euid: %u",
85 obj->uid, strerror(errno), getuid(), geteuid());
94 LOG(log_note, logtype_afpd, "AFP statistics: %.2f KB read, %.2f KB written",
95 dsi->read_count/1024.0, dsi->write_count/1024.0);
101 /* -------------------------------
103 * a little bit of code duplication.
105 static void afp_dsi_die(int sig)
107 DSI *dsi = (DSI *)AFPobj->handle;
109 if (dsi->flags & DSI_RECONINPROG) {
110 /* Primary reconnect succeeded, got SIGTERM from afpd parent */
111 dsi->flags &= ~DSI_RECONINPROG;
112 return; /* this returns to afp_disconnect */
115 if (dsi->flags & DSI_DISCONNECTED) {
116 LOG(log_note, logtype_afpd, "Disconnected session terminating");
120 dsi_attention(AFPobj->handle, AFPATTN_SHUTDOWN);
121 afp_dsi_close(AFPobj);
122 if (sig) /* if no signal, assume dieing because logins are disabled &
123 don't log it (maintenance mode)*/
124 LOG(log_info, logtype_afpd, "Connection terminated");
125 if (sig == SIGTERM || sig == SIGALRM) {
133 /* SIGQUIT handler */
134 static void ipc_reconnect_handler(int sig _U_)
136 DSI *dsi = (DSI *)AFPobj->handle;
138 if (reconnect_ipc(AFPobj) != 0) {
139 LOG(log_error, logtype_afpd, "ipc_reconnect_handler: failed IPC reconnect");
140 afp_dsi_close(AFPobj);
144 if (ipc_child_write(AFPobj->ipc_fd, IPC_GETSESSION, AFPobj->sinfo.clientid_len, AFPobj->sinfo.clientid) != 0) {
145 LOG(log_error, logtype_afpd, "ipc_reconnect_handler: failed IPC ID resend");
146 afp_dsi_close(AFPobj);
149 LOG(log_note, logtype_afpd, "ipc_reconnect_handler: IPC reconnect done");
152 /* SIGURG handler (primary reconnect) */
153 static void afp_dsi_transfer_session(int sig _U_)
157 DSI *dsi = (DSI *)AFPobj->handle;
159 LOG(log_debug, logtype_afpd, "afp_dsi_transfer_session: got SIGURG, trying to receive session");
161 if (readt(AFPobj->ipc_fd, &dsiID, 2, 0, 2) != 2) {
162 LOG(log_error, logtype_afpd, "afp_dsi_transfer_session: couldn't receive DSI id, goodbye");
163 afp_dsi_close(AFPobj);
167 if ((socket = recv_fd(AFPobj->ipc_fd, 1)) == -1) {
168 LOG(log_error, logtype_afpd, "afp_dsi_transfer_session: couldn't receive session fd, goodbye");
169 afp_dsi_close(AFPobj);
173 LOG(log_debug, logtype_afpd, "afp_dsi_transfer_session: received socket fd: %i", socket);
175 dsi->proto_close(dsi);
176 dsi->socket = socket;
177 dsi->flags = DSI_RECONSOCKET;
179 dsi->eof = dsi->start = dsi->buffer;
181 dsi->header.dsi_requestID = dsiID;
182 dsi->header.dsi_command = DSIFUNC_CMD;
185 * The session transfer happens in the middle of FPDisconnect old session, thus we
186 * have to send the reply now.
188 if (!dsi_cmdreply(dsi, AFP_OK)) {
189 LOG(log_error, logtype_afpd, "dsi_cmdreply: %s", strerror(errno) );
190 afp_dsi_close(AFPobj);
194 LOG(log_note, logtype_afpd, "afp_dsi_transfer_session: succesfull primary reconnect");
196 * Now returning from this signal handler return to dsi_receive which should start
197 * reading/continuing from the connected socket that was passed via the parent from
198 * another session. The parent will terminate that session.
200 siglongjmp(recon_jmp, 1);
203 /* ------------------- */
204 static void afp_dsi_timedown(int sig _U_)
208 DSI *dsi = (DSI *)AFPobj->handle;
209 dsi->flags |= DSI_DIE;
210 /* shutdown and don't reconnect. server going down in 5 minutes. */
211 setmessage("The server is going down for maintenance.");
212 if (dsi_attention(AFPobj->handle, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
213 AFPATTN_MESG | AFPATTN_TIME(5)) < 0) {
214 DSI *dsi = (DSI *)AFPobj->handle;
215 dsi->down_request = 1;
218 it.it_interval.tv_sec = 0;
219 it.it_interval.tv_usec = 0;
220 it.it_value.tv_sec = 300;
221 it.it_value.tv_usec = 0;
223 if ( setitimer( ITIMER_REAL, &it, NULL ) < 0 ) {
224 LOG(log_error, logtype_afpd, "afp_timedown: setitimer: %s", strerror(errno) );
225 afp_dsi_die(EXITERR_SYS);
227 memset(&sv, 0, sizeof(sv));
228 sv.sa_handler = afp_dsi_die;
229 sigemptyset( &sv.sa_mask );
230 sigaddset(&sv.sa_mask, SIGHUP);
231 sigaddset(&sv.sa_mask, SIGTERM);
232 sv.sa_flags = SA_RESTART;
233 if ( sigaction( SIGALRM, &sv, NULL ) < 0 ) {
234 LOG(log_error, logtype_afpd, "afp_timedown: sigaction: %s", strerror(errno) );
235 afp_dsi_die(EXITERR_SYS);
239 sv.sa_handler = SIG_IGN;
240 sigemptyset( &sv.sa_mask );
241 sv.sa_flags = SA_RESTART;
242 if ( sigaction( SIGUSR1, &sv, NULL ) < 0 ) {
243 LOG(log_error, logtype_afpd, "afp_timedown: sigaction SIGHUP: %s", strerror(errno) );
244 afp_dsi_die(EXITERR_SYS);
248 /* ---------------------------------
249 * SIGHUP reload configuration file
251 volatile int reload_request = 0;
253 static void afp_dsi_reload(int sig _U_)
258 /* ---------------------------------
259 * SIGINT: enable max_debug LOGging
261 static volatile sig_atomic_t debug_request = 0;
263 static void afp_dsi_debug(int sig _U_)
268 /* ---------------------- */
269 static void afp_dsi_getmesg (int sig _U_)
271 DSI *dsi = (DSI *)AFPobj->handle;
273 dsi->msg_request = 1;
274 if (dsi_attention(AFPobj->handle, AFPATTN_MESG | AFPATTN_TIME(5)) < 0)
275 dsi->msg_request = 2;
278 static void alarm_handler(int sig _U_)
281 DSI *dsi = (DSI *)AFPobj->handle;
283 /* we have to restart the timer because some libraries may use alarm() */
284 setitimer(ITIMER_REAL, &dsi->timer, NULL);
286 /* we got some traffic from the client since the previous timer tick. */
287 if ((dsi->flags & DSI_DATA)) {
288 dsi->flags &= ~DSI_DATA;
293 LOG(log_maxdebug, logtype_afpd, "alarm: tickles: %u, flags: %s|%s|%s|%s|%s|%s|%s|%s|%s",
295 (dsi->flags & DSI_DATA) ? "DSI_DATA" : "-",
296 (dsi->flags & DSI_RUNNING) ? "DSI_RUNNING" : "-",
297 (dsi->flags & DSI_SLEEPING) ? "DSI_SLEEPING" : "-",
298 (dsi->flags & DSI_EXTSLEEP) ? "DSI_EXTSLEEP" : "-",
299 (dsi->flags & DSI_DISCONNECTED) ? "DSI_DISCONNECTED" : "-",
300 (dsi->flags & DSI_DIE) ? "DSI_DIE" : "-",
301 (dsi->flags & DSI_NOREPLY) ? "DSI_NOREPLY" : "-",
302 (dsi->flags & DSI_RECONSOCKET) ? "DSI_RECONSOCKET" : "-",
303 (dsi->flags & DSI_RECONINPROG) ? "DSI_RECONINPROG" : "-");
305 if (dsi->flags & DSI_SLEEPING) {
306 if (dsi->tickle > AFPobj->options.sleep) {
307 LOG(log_note, logtype_afpd, "afp_alarm: sleep time ended");
308 afp_dsi_die(EXITERR_CLNT);
313 if (dsi->flags & DSI_DISCONNECTED) {
314 if (geteuid() == 0) {
315 LOG(log_note, logtype_afpd, "afp_alarm: unauthenticated user, connection problem");
316 afp_dsi_die(EXITERR_CLNT);
318 if (dsi->tickle > AFPobj->options.disconnected) {
319 LOG(log_error, logtype_afpd, "afp_alarm: reconnect timer expired, goodbye");
320 afp_dsi_die(EXITERR_CLNT);
325 /* if we're in the midst of processing something, don't die. */
326 if (dsi->tickle >= AFPobj->options.timeout) {
327 LOG(log_error, logtype_afpd, "afp_alarm: child timed out, entering disconnected state");
328 if (dsi_disconnect(dsi) != 0)
329 afp_dsi_die(EXITERR_CLNT);
333 if ((err = pollvoltime(AFPobj)) == 0)
334 LOG(log_debug, logtype_afpd, "afp_alarm: sending DSI tickle");
335 err = dsi_tickle(AFPobj->handle);
337 if (geteuid() == 0) {
338 LOG(log_note, logtype_afpd, "afp_alarm: unauthenticated user, connection problem");
339 afp_dsi_die(EXITERR_CLNT);
341 LOG(log_error, logtype_afpd, "afp_alarm: connection problem, entering disconnected state");
342 if (dsi_disconnect(dsi) != 0)
343 afp_dsi_die(EXITERR_CLNT);
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->handle, AFPATTN_MESG | AFPATTN_TIME(5));
361 dsi->msg_request = 0;
364 if (dsi->down_request) {
365 dsi->down_request = 0;
366 dsi_attention(AFPobj->handle, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
367 AFPATTN_MESG | AFPATTN_TIME(5));
371 /* -------------------------------------------
372 afp over dsi. this never returns.
374 void afp_over_dsi(AFPObj *obj)
376 DSI *dsi = (DSI *) obj->handle;
380 struct sigaction action;
384 obj->exit = afp_dsi_die;
385 obj->reply = (int (*)()) dsi_cmdreply;
386 obj->attention = (int (*)(void *, AFPUserBytes)) dsi_attention;
389 memset(&action, 0, sizeof(action));
390 sigfillset(&action.sa_mask);
391 action.sa_flags = SA_RESTART;
394 action.sa_handler = afp_dsi_reload;
395 if ( sigaction( SIGHUP, &action, NULL ) < 0 ) {
396 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
397 afp_dsi_die(EXITERR_SYS);
401 action.sa_handler = afp_dsi_transfer_session;
402 if ( sigaction( SIGURG, &action, NULL ) < 0 ) {
403 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
404 afp_dsi_die(EXITERR_SYS);
407 /* install SIGTERM */
408 action.sa_handler = afp_dsi_die;
409 if ( sigaction( SIGTERM, &action, NULL ) < 0 ) {
410 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
411 afp_dsi_die(EXITERR_SYS);
414 /* install SIGQUIT */
415 action.sa_handler = ipc_reconnect_handler;
416 if ( sigaction(SIGQUIT, &action, NULL ) < 0 ) {
417 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
418 afp_dsi_die(EXITERR_SYS);
421 /* SIGUSR2 - server message support */
422 action.sa_handler = afp_dsi_getmesg;
423 if ( sigaction( SIGUSR2, &action, NULL) < 0 ) {
424 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
425 afp_dsi_die(EXITERR_SYS);
428 /* SIGUSR1 - set down in 5 minutes */
429 action.sa_handler = afp_dsi_timedown;
430 action.sa_flags = SA_RESTART;
431 if ( sigaction( SIGUSR1, &action, NULL) < 0 ) {
432 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
433 afp_dsi_die(EXITERR_SYS);
436 /* SIGINT - enable max_debug LOGging to /tmp/afpd.PID.XXXXXX */
437 action.sa_handler = afp_dsi_debug;
438 if ( sigaction( SIGINT, &action, NULL) < 0 ) {
439 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
440 afp_dsi_die(EXITERR_SYS);
444 /* SIGALRM - tickle handler */
445 action.sa_handler = alarm_handler;
446 if ((sigaction(SIGALRM, &action, NULL) < 0) ||
447 (setitimer(ITIMER_REAL, &dsi->timer, NULL) < 0)) {
448 afp_dsi_die(EXITERR_SYS);
450 #endif /* DEBUGGING */
452 if (dircache_init(obj->options.dircachesize) != 0)
453 afp_dsi_die(EXITERR_SYS);
455 /* set TCP snd/rcv buf */
456 if (obj->options.tcp_rcvbuf) {
457 if (setsockopt(dsi->socket,
460 &obj->options.tcp_rcvbuf,
461 sizeof(obj->options.tcp_rcvbuf)) != 0) {
462 LOG(log_error, logtype_dsi, "afp_over_dsi: setsockopt(SO_RCVBUF): %s", strerror(errno));
465 if (obj->options.tcp_sndbuf) {
466 if (setsockopt(dsi->socket,
469 &obj->options.tcp_sndbuf,
470 sizeof(obj->options.tcp_sndbuf)) != 0) {
471 LOG(log_error, logtype_dsi, "afp_over_dsi: setsockopt(SO_SNDBUF): %s", strerror(errno));
475 /* get stuck here until the end */
477 if (sigsetjmp(recon_jmp, 1) != 0)
478 /* returning from SIGALARM handler for a primary reconnect */
481 /* Blocking read on the network socket */
482 cmd = dsi_receive(dsi);
485 /* cmd == 0 is the error condition */
486 if (dsi->flags & DSI_RECONSOCKET) {
487 /* we just got a reconnect so we immediately try again to receive on the new fd */
488 dsi->flags &= ~DSI_RECONSOCKET;
492 /* the client sometimes logs out (afp_logout) but doesn't close the DSI session */
493 if (dsi->flags & DSI_AFP_LOGGED_OUT) {
494 LOG(log_note, logtype_afpd, "afp_over_dsi: client logged out, terminating DSI session");
500 /* got ECONNRESET in read from client => exit*/
501 if (dsi->flags & DSI_GOT_ECONNRESET) {
502 LOG(log_note, logtype_afpd, "afp_over_dsi: client connection reset");
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 while (dsi->flags & DSI_DISCONNECTED)
519 pause(); /* gets interrupted by SIGALARM or SIGURG tickle */
520 continue; /* continue receiving until disconnect timer expires
521 * or a primary reconnect succeeds */
524 if (!(dsi->flags & DSI_EXTSLEEP) && (dsi->flags & DSI_SLEEPING)) {
525 LOG(log_debug, logtype_afpd, "afp_over_dsi: got data, ending normal sleep");
526 dsi->flags &= ~DSI_SLEEPING;
530 if (reload_request) {
532 load_volumes(AFPobj);
535 /* The first SIGINT enables debugging, the next restores the config */
537 static int debugging = 0;
544 if (obj->options.logconfig)
545 setuplog(obj->options.logconfig);
547 setuplog("default log_note");
552 sprintf(logstr, "default log_maxdebug /tmp/afpd.%u.XXXXXX", getpid());
558 dsi->flags |= DSI_DATA;
564 LOG(log_debug, logtype_afpd, "DSI: close session request");
566 LOG(log_note, logtype_afpd, "done");
570 dsi->flags &= ~DSI_DATA; /* thats no data in the sense we use it in alarm_handler */
571 LOG(log_debug, logtype_afpd, "DSI: client tickle");
572 /* timer is not every 30 seconds anymore, so we don't get killed on the client side. */
573 if ((dsi->flags & DSI_DIE))
580 if ( flushfork( writtenfork ) < 0 ) {
581 LOG(log_error, logtype_afpd, "main flushfork: %s", strerror(errno) );
587 function = (u_char) dsi->commands[0];
589 /* AFP replay cache */
590 rc_idx = dsi->clientID % REPLAYCACHE_SIZE;
591 LOG(log_debug, logtype_afpd, "DSI request ID: %u", dsi->clientID);
593 if (replaycache[rc_idx].DSIreqID == dsi->clientID
594 && replaycache[rc_idx].AFPcommand == function) {
595 LOG(log_note, logtype_afpd, "AFP Replay Cache match: id: %u / cmd: %s",
596 dsi->clientID, AfpNum2name(function));
597 err = replaycache[rc_idx].result;
598 /* AFP replay cache end */
600 /* send off an afp command. in a couple cases, we take advantage
601 * of the fact that we're a stream-based protocol. */
602 if (afp_switch[function]) {
603 dsi->datalen = DSI_DATASIZ;
604 dsi->flags |= DSI_RUNNING;
606 LOG(log_debug, logtype_afpd, "<== Start AFP command: %s", AfpNum2name(function));
608 err = (*afp_switch[function])(obj,
609 (char *)&dsi->commands, dsi->cmdlen,
610 (char *)&dsi->data, &dsi->datalen);
612 LOG(log_debug, logtype_afpd, "==> Finished AFP command: %s -> %s",
613 AfpNum2name(function), AfpErr2name(err));
615 dir_free_invalid_q();
617 dsi->flags &= ~DSI_RUNNING;
619 /* Add result to the AFP replay cache */
620 replaycache[rc_idx].DSIreqID = dsi->clientID;
621 replaycache[rc_idx].AFPcommand = function;
622 replaycache[rc_idx].result = err;
624 LOG(log_error, logtype_afpd, "bad function %X", function);
630 /* single shot toggle that gets set by dsi_readinit. */
631 if (dsi->flags & DSI_NOREPLY) {
632 dsi->flags &= ~DSI_NOREPLY;
636 if (!dsi_cmdreply(dsi, err)) {
637 LOG(log_error, logtype_afpd, "dsi_cmdreply(%d): %s", dsi->socket, strerror(errno) );
638 if (dsi_disconnect(dsi) != 0)
639 afp_dsi_die(EXITERR_CLNT);
643 case DSIFUNC_WRITE: /* FPWrite and FPAddIcon */
644 function = (u_char) dsi->commands[0];
645 if ( afp_switch[ function ] != NULL ) {
646 dsi->datalen = DSI_DATASIZ;
647 dsi->flags |= DSI_RUNNING;
649 LOG(log_debug, logtype_afpd, "<== Start AFP command: %s", AfpNum2name(function));
651 err = (*afp_switch[function])(obj,
652 (char *)&dsi->commands, dsi->cmdlen,
653 (char *)&dsi->data, &dsi->datalen);
655 LOG(log_debug, logtype_afpd, "==> Finished AFP command: %s -> %s",
656 AfpNum2name(function), AfpErr2name(err));
658 dsi->flags &= ~DSI_RUNNING;
660 LOG(log_error, logtype_afpd, "(write) bad function %x", function);
665 if (!dsi_wrtreply(dsi, err)) {
666 LOG(log_error, logtype_afpd, "dsi_wrtreply: %s", strerror(errno) );
667 if (dsi_disconnect(dsi) != 0)
668 afp_dsi_die(EXITERR_CLNT);
672 case DSIFUNC_ATTN: /* attention replies */
675 /* error. this usually implies a mismatch of some kind
676 * between server and client. if things are correct,
677 * we need to flush the rest of the packet if necessary. */
679 LOG(log_info, logtype_afpd,"afp_dsi: spurious command %d", cmd);
680 dsi_writeinit(dsi, dsi->data, DSI_DATASIZ);
684 pending_request(dsi);
686 fce_pending_events(obj);
690 afp_dsi_die(EXITERR_CLNT);