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;
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());
103 LOG(log_note, logtype_afpd, "AFP statistics: %.2f KB read, %.2f KB written",
104 dsi->read_count/1024.0, dsi->write_count/1024.0);
110 /* -------------------------------
112 * a little bit of code duplication.
114 static void afp_dsi_die(int sig)
116 DSI *dsi = (DSI *)AFPobj->handle;
118 if (dsi->flags & DSI_RECONINPROG) {
119 /* Primary reconnect succeeded, got SIGTERM from afpd parent */
120 dsi->flags &= ~DSI_RECONINPROG;
121 return; /* this returns to afp_disconnect */
124 if (dsi->flags & DSI_DISCONNECTED) {
125 LOG(log_note, logtype_afpd, "Disconnected session terminating");
129 dsi_attention(AFPobj->handle, AFPATTN_SHUTDOWN);
130 afp_dsi_close(AFPobj);
131 if (sig) /* if no signal, assume dieing because logins are disabled &
132 don't log it (maintenance mode)*/
133 LOG(log_info, logtype_afpd, "Connection terminated");
134 if (sig == SIGTERM || sig == SIGALRM) {
142 /* SIGQUIT handler */
143 static void ipc_reconnect_handler(int sig _U_)
145 DSI *dsi = (DSI *)AFPobj->handle;
147 if (reconnect_ipc(AFPobj) != 0) {
148 LOG(log_error, logtype_afpd, "ipc_reconnect_handler: failed IPC reconnect");
149 afp_dsi_close(AFPobj);
153 if (ipc_child_write(AFPobj->ipc_fd, IPC_GETSESSION, AFPobj->sinfo.clientid_len, AFPobj->sinfo.clientid) != 0) {
154 LOG(log_error, logtype_afpd, "ipc_reconnect_handler: failed IPC ID resend");
155 afp_dsi_close(AFPobj);
158 LOG(log_note, logtype_afpd, "ipc_reconnect_handler: IPC reconnect done");
161 /* SIGURG handler (primary reconnect) */
162 static void afp_dsi_transfer_session(int sig _U_)
166 DSI *dsi = (DSI *)AFPobj->handle;
168 LOG(log_debug, logtype_afpd, "afp_dsi_transfer_session: got SIGURG, trying to receive session");
170 if (readt(AFPobj->ipc_fd, &dsiID, 2, 0, 2) != 2) {
171 LOG(log_error, logtype_afpd, "afp_dsi_transfer_session: couldn't receive DSI id, goodbye");
172 afp_dsi_close(AFPobj);
176 if ((socket = recv_fd(AFPobj->ipc_fd, 1)) == -1) {
177 LOG(log_error, logtype_afpd, "afp_dsi_transfer_session: couldn't receive session fd, goodbye");
178 afp_dsi_close(AFPobj);
182 LOG(log_debug, logtype_afpd, "afp_dsi_transfer_session: received socket fd: %i", socket);
184 dsi->proto_close(dsi);
185 dsi->socket = socket;
186 dsi->flags = DSI_RECONSOCKET;
188 dsi->eof = dsi->start = dsi->buffer;
190 dsi->header.dsi_requestID = dsiID;
191 dsi->header.dsi_command = DSIFUNC_CMD;
194 * The session transfer happens in the middle of FPDisconnect old session, thus we
195 * have to send the reply now.
197 if (!dsi_cmdreply(dsi, AFP_OK)) {
198 LOG(log_error, logtype_afpd, "dsi_cmdreply: %s", strerror(errno) );
199 afp_dsi_close(AFPobj);
203 LOG(log_note, logtype_afpd, "afp_dsi_transfer_session: succesfull primary reconnect");
205 * Now returning from this signal handler return to dsi_receive which should start
206 * reading/continuing from the connected socket that was passed via the parent from
207 * another session. The parent will terminate that session.
209 siglongjmp(recon_jmp, 1);
212 /* ------------------- */
213 static void afp_dsi_timedown(int sig _U_)
217 DSI *dsi = (DSI *)AFPobj->handle;
218 dsi->flags |= DSI_DIE;
219 /* shutdown and don't reconnect. server going down in 5 minutes. */
220 setmessage("The server is going down for maintenance.");
221 if (dsi_attention(AFPobj->handle, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
222 AFPATTN_MESG | AFPATTN_TIME(5)) < 0) {
223 DSI *dsi = (DSI *)AFPobj->handle;
224 dsi->down_request = 1;
227 it.it_interval.tv_sec = 0;
228 it.it_interval.tv_usec = 0;
229 it.it_value.tv_sec = 300;
230 it.it_value.tv_usec = 0;
232 if ( setitimer( ITIMER_REAL, &it, NULL ) < 0 ) {
233 LOG(log_error, logtype_afpd, "afp_timedown: setitimer: %s", strerror(errno) );
234 afp_dsi_die(EXITERR_SYS);
236 memset(&sv, 0, sizeof(sv));
237 sv.sa_handler = afp_dsi_die;
238 sigemptyset( &sv.sa_mask );
239 sigaddset(&sv.sa_mask, SIGHUP);
240 sigaddset(&sv.sa_mask, SIGTERM);
241 sv.sa_flags = SA_RESTART;
242 if ( sigaction( SIGALRM, &sv, NULL ) < 0 ) {
243 LOG(log_error, logtype_afpd, "afp_timedown: sigaction: %s", strerror(errno) );
244 afp_dsi_die(EXITERR_SYS);
248 sv.sa_handler = SIG_IGN;
249 sigemptyset( &sv.sa_mask );
250 sv.sa_flags = SA_RESTART;
251 if ( sigaction( SIGUSR1, &sv, NULL ) < 0 ) {
252 LOG(log_error, logtype_afpd, "afp_timedown: sigaction SIGHUP: %s", strerror(errno) );
253 afp_dsi_die(EXITERR_SYS);
257 /* ---------------------------------
258 * SIGHUP reload configuration file
260 volatile int reload_request = 0;
262 static void afp_dsi_reload(int sig _U_)
267 /* ---------------------------------
268 * SIGINT: enable max_debug LOGging
270 static volatile sig_atomic_t debug_request = 0;
272 static void afp_dsi_debug(int sig _U_)
277 /* ---------------------- */
278 static void afp_dsi_getmesg (int sig _U_)
280 DSI *dsi = (DSI *)AFPobj->handle;
282 dsi->msg_request = 1;
283 if (dsi_attention(AFPobj->handle, AFPATTN_MESG | AFPATTN_TIME(5)) < 0)
284 dsi->msg_request = 2;
287 static void alarm_handler(int sig _U_)
290 DSI *dsi = (DSI *)AFPobj->handle;
292 /* we have to restart the timer because some libraries may use alarm() */
293 setitimer(ITIMER_REAL, &dsi->timer, NULL);
295 /* we got some traffic from the client since the previous timer tick. */
296 if ((dsi->flags & DSI_DATA)) {
297 dsi->flags &= ~DSI_DATA;
302 LOG(log_maxdebug, logtype_afpd, "alarm: tickles: %u, flags: %s|%s|%s|%s|%s|%s|%s|%s|%s",
304 (dsi->flags & DSI_DATA) ? "DSI_DATA" : "-",
305 (dsi->flags & DSI_RUNNING) ? "DSI_RUNNING" : "-",
306 (dsi->flags & DSI_SLEEPING) ? "DSI_SLEEPING" : "-",
307 (dsi->flags & DSI_EXTSLEEP) ? "DSI_EXTSLEEP" : "-",
308 (dsi->flags & DSI_DISCONNECTED) ? "DSI_DISCONNECTED" : "-",
309 (dsi->flags & DSI_DIE) ? "DSI_DIE" : "-",
310 (dsi->flags & DSI_NOREPLY) ? "DSI_NOREPLY" : "-",
311 (dsi->flags & DSI_RECONSOCKET) ? "DSI_RECONSOCKET" : "-",
312 (dsi->flags & DSI_RECONINPROG) ? "DSI_RECONINPROG" : "-");
314 if (dsi->flags & DSI_SLEEPING) {
315 if (dsi->tickle > AFPobj->options.sleep) {
316 LOG(log_note, logtype_afpd, "afp_alarm: sleep time ended");
317 afp_dsi_die(EXITERR_CLNT);
322 if (dsi->flags & DSI_DISCONNECTED) {
323 if (geteuid() == 0) {
324 LOG(log_note, logtype_afpd, "afp_alarm: unauthenticated user, connection problem");
325 afp_dsi_die(EXITERR_CLNT);
327 if (dsi->tickle > AFPobj->options.disconnected) {
328 LOG(log_error, logtype_afpd, "afp_alarm: reconnect timer expired, goodbye");
329 afp_dsi_die(EXITERR_CLNT);
334 /* if we're in the midst of processing something, don't die. */
335 if (dsi->tickle >= AFPobj->options.timeout) {
336 LOG(log_error, logtype_afpd, "afp_alarm: child timed out, entering disconnected state");
337 if (dsi_disconnect(dsi) != 0)
338 afp_dsi_die(EXITERR_CLNT);
342 if ((err = pollvoltime(AFPobj)) == 0)
343 LOG(log_debug, logtype_afpd, "afp_alarm: sending DSI tickle");
344 err = dsi_tickle(AFPobj->handle);
346 if (geteuid() == 0) {
347 LOG(log_note, logtype_afpd, "afp_alarm: unauthenticated user, connection problem");
348 afp_dsi_die(EXITERR_CLNT);
350 LOG(log_error, logtype_afpd, "afp_alarm: connection problem, entering disconnected state");
351 if (dsi_disconnect(dsi) != 0)
352 afp_dsi_die(EXITERR_CLNT);
357 if dsi->in_write is set attention, tickle (and close?) msg
358 aren't sent. We don't care about tickle
360 static void pending_request(DSI *dsi)
362 /* send pending attention */
364 /* read msg if any, it could be done in afp_getsrvrmesg */
365 if (dsi->msg_request) {
366 if (dsi->msg_request == 2) {
367 /* didn't send it in signal handler */
368 dsi_attention(AFPobj->handle, AFPATTN_MESG | AFPATTN_TIME(5));
370 dsi->msg_request = 0;
373 if (dsi->down_request) {
374 dsi->down_request = 0;
375 dsi_attention(AFPobj->handle, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
376 AFPATTN_MESG | AFPATTN_TIME(5));
380 /* -------------------------------------------
381 afp over dsi. this never returns.
383 void afp_over_dsi(AFPObj *obj)
385 DSI *dsi = (DSI *) obj->handle;
389 struct sigaction action;
393 obj->exit = afp_dsi_die;
394 obj->reply = (int (*)()) dsi_cmdreply;
395 obj->attention = (int (*)(void *, AFPUserBytes)) dsi_attention;
398 memset(&action, 0, sizeof(action));
399 sigfillset(&action.sa_mask);
400 action.sa_flags = SA_RESTART;
403 action.sa_handler = afp_dsi_reload;
404 if ( sigaction( SIGHUP, &action, NULL ) < 0 ) {
405 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
406 afp_dsi_die(EXITERR_SYS);
410 action.sa_handler = afp_dsi_transfer_session;
411 if ( sigaction( SIGURG, &action, NULL ) < 0 ) {
412 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
413 afp_dsi_die(EXITERR_SYS);
416 /* install SIGTERM */
417 action.sa_handler = afp_dsi_die;
418 if ( sigaction( SIGTERM, &action, NULL ) < 0 ) {
419 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
420 afp_dsi_die(EXITERR_SYS);
423 /* install SIGQUIT */
424 action.sa_handler = ipc_reconnect_handler;
425 if ( sigaction(SIGQUIT, &action, NULL ) < 0 ) {
426 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
427 afp_dsi_die(EXITERR_SYS);
430 /* SIGUSR2 - server message support */
431 action.sa_handler = afp_dsi_getmesg;
432 if ( sigaction( SIGUSR2, &action, NULL) < 0 ) {
433 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
434 afp_dsi_die(EXITERR_SYS);
437 /* SIGUSR1 - set down in 5 minutes */
438 action.sa_handler = afp_dsi_timedown;
439 action.sa_flags = SA_RESTART;
440 if ( sigaction( SIGUSR1, &action, NULL) < 0 ) {
441 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
442 afp_dsi_die(EXITERR_SYS);
445 /* SIGINT - enable max_debug LOGging to /tmp/afpd.PID.XXXXXX */
446 action.sa_handler = afp_dsi_debug;
447 if ( sigaction( SIGINT, &action, NULL) < 0 ) {
448 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
449 afp_dsi_die(EXITERR_SYS);
453 /* SIGALRM - tickle handler */
454 action.sa_handler = alarm_handler;
455 if ((sigaction(SIGALRM, &action, NULL) < 0) ||
456 (setitimer(ITIMER_REAL, &dsi->timer, NULL) < 0)) {
457 afp_dsi_die(EXITERR_SYS);
459 #endif /* DEBUGGING */
461 if (dircache_init(obj->options.dircachesize) != 0)
462 afp_dsi_die(EXITERR_SYS);
464 /* set TCP snd/rcv buf */
465 if (obj->options.tcp_rcvbuf) {
466 if (setsockopt(dsi->socket,
469 &obj->options.tcp_rcvbuf,
470 sizeof(obj->options.tcp_rcvbuf)) != 0) {
471 LOG(log_error, logtype_dsi, "afp_over_dsi: setsockopt(SO_RCVBUF): %s", strerror(errno));
474 if (obj->options.tcp_sndbuf) {
475 if (setsockopt(dsi->socket,
478 &obj->options.tcp_sndbuf,
479 sizeof(obj->options.tcp_sndbuf)) != 0) {
480 LOG(log_error, logtype_dsi, "afp_over_dsi: setsockopt(SO_SNDBUF): %s", strerror(errno));
484 /* set TCP_NODELAY */
486 setsockopt(dsi->socket, SOL_TCP, TCP_NODELAY, &flag, sizeof(flag));
488 /* get stuck here until the end */
490 if (sigsetjmp(recon_jmp, 1) != 0)
491 /* returning from SIGALARM handler for a primary reconnect */
494 /* Blocking read on the network socket */
495 cmd = dsi_stream_receive(dsi);
498 /* cmd == 0 is the error condition */
499 if (dsi->flags & DSI_RECONSOCKET) {
500 /* we just got a reconnect so we immediately try again to receive on the new fd */
501 dsi->flags &= ~DSI_RECONSOCKET;
505 /* the client sometimes logs out (afp_logout) but doesn't close the DSI session */
506 if (dsi->flags & DSI_AFP_LOGGED_OUT) {
507 LOG(log_note, logtype_afpd, "afp_over_dsi: client logged out, terminating DSI session");
513 /* got ECONNRESET in read from client => exit*/
514 if (dsi->flags & DSI_GOT_ECONNRESET) {
515 LOG(log_note, logtype_afpd, "afp_over_dsi: client connection reset");
521 if (dsi->flags & DSI_RECONINPROG) {
522 LOG(log_note, logtype_afpd, "afp_over_dsi: failed reconnect");
527 /* Some error on the client connection, enter disconnected state */
528 if (dsi_disconnect(dsi) != 0)
529 afp_dsi_die(EXITERR_CLNT);
531 while (dsi->flags & DSI_DISCONNECTED)
532 pause(); /* gets interrupted by SIGALARM or SIGURG tickle */
533 continue; /* continue receiving until disconnect timer expires
534 * or a primary reconnect succeeds */
537 if (!(dsi->flags & DSI_EXTSLEEP) && (dsi->flags & DSI_SLEEPING)) {
538 LOG(log_debug, logtype_afpd, "afp_over_dsi: got data, ending normal sleep");
539 dsi->flags &= ~DSI_SLEEPING;
543 if (reload_request) {
545 load_volumes(AFPobj);
548 /* The first SIGINT enables debugging, the next restores the config */
550 static int debugging = 0;
557 if (obj->options.logconfig)
558 setuplog(obj->options.logconfig);
560 setuplog("default log_note");
565 sprintf(logstr, "default log_maxdebug /tmp/afpd.%u.XXXXXX", getpid());
571 dsi->flags |= DSI_DATA;
577 LOG(log_debug, logtype_afpd, "DSI: close session request");
579 LOG(log_note, logtype_afpd, "done");
583 dsi->flags &= ~DSI_DATA; /* thats no data in the sense we use it in alarm_handler */
584 LOG(log_debug, logtype_afpd, "DSI: client tickle");
585 /* timer is not every 30 seconds anymore, so we don't get killed on the client side. */
586 if ((dsi->flags & DSI_DIE))
593 if ( flushfork( writtenfork ) < 0 ) {
594 LOG(log_error, logtype_afpd, "main flushfork: %s", strerror(errno) );
600 function = (u_char) dsi->commands[0];
602 /* AFP replay cache */
603 rc_idx = dsi->clientID % REPLAYCACHE_SIZE;
604 LOG(log_debug, logtype_dsi, "DSI request ID: %u", dsi->clientID);
606 if (replaycache[rc_idx].DSIreqID == dsi->clientID
607 && replaycache[rc_idx].AFPcommand == function) {
608 LOG(log_note, logtype_afpd, "AFP Replay Cache match: id: %u / cmd: %s",
609 dsi->clientID, AfpNum2name(function));
610 err = replaycache[rc_idx].result;
611 /* AFP replay cache end */
613 /* send off an afp command. in a couple cases, we take advantage
614 * of the fact that we're a stream-based protocol. */
615 if (afp_switch[function]) {
616 dsi->datalen = DSI_DATASIZ;
617 dsi->flags |= DSI_RUNNING;
619 LOG(log_debug, logtype_afpd, "<== Start AFP command: %s", AfpNum2name(function));
621 err = (*afp_switch[function])(obj,
622 (char *)&dsi->commands, dsi->cmdlen,
623 (char *)&dsi->data, &dsi->datalen);
625 LOG(log_debug, logtype_afpd, "==> Finished AFP command: %s -> %s",
626 AfpNum2name(function), AfpErr2name(err));
628 dir_free_invalid_q();
631 /* bring everything back to old euid, egid */
633 restore_uidgid ( &obj->uidgid );
634 #endif /* FORCE_UIDGID */
635 dsi->flags &= ~DSI_RUNNING;
637 /* Add result to the AFP replay cache */
638 replaycache[rc_idx].DSIreqID = dsi->clientID;
639 replaycache[rc_idx].AFPcommand = function;
640 replaycache[rc_idx].result = err;
642 LOG(log_error, logtype_afpd, "bad function %X", function);
648 /* single shot toggle that gets set by dsi_readinit. */
649 if (dsi->flags & DSI_NOREPLY) {
650 dsi->flags &= ~DSI_NOREPLY;
652 } else if (!dsi_cmdreply(dsi, err)) {
653 LOG(log_error, logtype_afpd, "dsi_cmdreply(%d): %s", dsi->socket, strerror(errno) );
654 if (dsi_disconnect(dsi) != 0)
655 afp_dsi_die(EXITERR_CLNT);
659 case DSIFUNC_WRITE: /* FPWrite and FPAddIcon */
660 function = (u_char) dsi->commands[0];
661 if ( afp_switch[ function ] != NULL ) {
662 dsi->datalen = DSI_DATASIZ;
663 dsi->flags |= DSI_RUNNING;
665 LOG(log_debug, logtype_afpd, "<== Start AFP command: %s", AfpNum2name(function));
667 err = (*afp_switch[function])(obj,
668 (char *)&dsi->commands, dsi->cmdlen,
669 (char *)&dsi->data, &dsi->datalen);
671 LOG(log_debug, logtype_afpd, "==> Finished AFP command: %s -> %s",
672 AfpNum2name(function), AfpErr2name(err));
674 dsi->flags &= ~DSI_RUNNING;
676 /* bring everything back to old euid, egid */
678 restore_uidgid ( &obj->uidgid );
679 #endif /* FORCE_UIDGID */
681 LOG(log_error, logtype_afpd, "(write) bad function %x", function);
686 if (!dsi_wrtreply(dsi, err)) {
687 LOG(log_error, logtype_afpd, "dsi_wrtreply: %s", strerror(errno) );
688 if (dsi_disconnect(dsi) != 0)
689 afp_dsi_die(EXITERR_CLNT);
693 case DSIFUNC_ATTN: /* attention replies */
696 /* error. this usually implies a mismatch of some kind
697 * between server and client. if things are correct,
698 * we need to flush the rest of the packet if necessary. */
700 LOG(log_info, logtype_afpd,"afp_dsi: spurious command %d", cmd);
701 dsi_writeinit(dsi, dsi->data, DSI_DATASIZ);
705 pending_request(dsi);
707 fce_pending_events(obj);
711 afp_dsi_die(EXITERR_CLNT);