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/locking.h>
43 * We generally pass this from afp_over_dsi to all afp_* funcs, so it should already be
44 * available everywhere. Unfortunately some funcs (eg acltoownermode) need acces to it
45 * but are deeply nested in the function chain with the caller already without acces to it.
46 * Changing this would require adding a reference to the caller which itself might be
47 * called in many places (eg acltoownermode is called from accessmode).
48 * The only sane way out is providing a copy of it here:
50 AFPObj *AFPobj = NULL;
61 * - indexed just by taking DSIreqID mod REPLAYCACHE_SIZE
63 static rc_elem_t replaycache[REPLAYCACHE_SIZE];
65 static sigjmp_buf recon_jmp;
66 static void afp_dsi_close(AFPObj *obj)
68 DSI *dsi = obj->handle;
73 /* we may have been called from a signal handler caught when afpd was running
74 * as uid 0, that's the wrong user for volume's prexec_close scripts if any,
75 * restore our login user
77 if (geteuid() != obj->uid) {
78 if (seteuid( obj->uid ) < 0) {
79 LOG(log_error, logtype_afpd, "can't seteuid(%u) back %s: uid: %u, euid: %u",
80 obj->uid, strerror(errno), getuid(), geteuid());
89 LOG(log_note, logtype_afpd, "AFP statistics: %.2f KB read, %.2f KB written",
90 dsi->read_count/1024.0, dsi->write_count/1024.0);
96 /* -------------------------------
98 * a little bit of code duplication.
100 static void afp_dsi_die(int sig)
102 DSI *dsi = (DSI *)AFPobj->handle;
104 if (dsi->flags & DSI_RECONINPROG) {
105 /* Primary reconnect succeeded, got SIGTERM from afpd parent */
106 dsi->flags &= ~DSI_RECONINPROG;
107 return; /* this returns to afp_disconnect */
110 if (dsi->flags & DSI_DISCONNECTED) {
111 LOG(log_note, logtype_afpd, "Disconnected session terminating");
115 dsi_attention(AFPobj->handle, AFPATTN_SHUTDOWN);
116 afp_dsi_close(AFPobj);
117 if (sig) /* if no signal, assume dieing because logins are disabled &
118 don't log it (maintenance mode)*/
119 LOG(log_info, logtype_afpd, "Connection terminated");
120 if (sig == SIGTERM || sig == SIGALRM) {
128 /* SIGURG handler (primary reconnect) */
129 static void afp_dsi_transfer_session(int sig _U_)
133 DSI *dsi = (DSI *)AFPobj->handle;
135 LOG(log_debug, logtype_afpd, "afp_dsi_transfer_session: got SIGURG, trying to receive session");
137 if (readt(AFPobj->ipc_fd, &dsiID, 2, 0, 2) != 2) {
138 LOG(log_error, logtype_afpd, "afp_dsi_transfer_session: couldn't receive DSI id, goodbye");
139 afp_dsi_close(AFPobj);
143 if ((socket = recv_fd(AFPobj->ipc_fd, 1)) == -1) {
144 LOG(log_error, logtype_afpd, "afp_dsi_transfer_session: couldn't receive session fd, goodbye");
145 afp_dsi_close(AFPobj);
149 LOG(log_debug, logtype_afpd, "afp_dsi_transfer_session: received socket fd: %i", socket);
151 dsi->proto_close(dsi);
152 dsi->socket = socket;
153 dsi->flags = DSI_RECONSOCKET;
155 dsi->eof = dsi->start = dsi->buffer;
157 dsi->header.dsi_requestID = dsiID;
158 dsi->header.dsi_command = DSIFUNC_CMD;
161 * The session transfer happens in the middle of FPDisconnect old session, thus we
162 * have to send the reply now.
164 if (!dsi_cmdreply(dsi, AFP_OK)) {
165 LOG(log_error, logtype_afpd, "dsi_cmdreply: %s", strerror(errno) );
166 afp_dsi_close(AFPobj);
170 LOG(log_note, logtype_afpd, "afp_dsi_transfer_session: succesfull primary reconnect");
172 * Now returning from this signal handler return to dsi_receive which should start
173 * reading/continuing from the connected socket that was passed via the parent from
174 * another session. The parent will terminate that session.
176 siglongjmp(recon_jmp, 1);
179 /* ------------------- */
180 static void afp_dsi_timedown(int sig _U_)
184 DSI *dsi = (DSI *)AFPobj->handle;
185 dsi->flags |= DSI_DIE;
186 /* shutdown and don't reconnect. server going down in 5 minutes. */
187 setmessage("The server is going down for maintenance.");
188 if (dsi_attention(AFPobj->handle, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
189 AFPATTN_MESG | AFPATTN_TIME(5)) < 0) {
190 DSI *dsi = (DSI *)AFPobj->handle;
191 dsi->down_request = 1;
194 it.it_interval.tv_sec = 0;
195 it.it_interval.tv_usec = 0;
196 it.it_value.tv_sec = 300;
197 it.it_value.tv_usec = 0;
199 if ( setitimer( ITIMER_REAL, &it, NULL ) < 0 ) {
200 LOG(log_error, logtype_afpd, "afp_timedown: setitimer: %s", strerror(errno) );
201 afp_dsi_die(EXITERR_SYS);
203 memset(&sv, 0, sizeof(sv));
204 sv.sa_handler = afp_dsi_die;
205 sigemptyset( &sv.sa_mask );
206 sigaddset(&sv.sa_mask, SIGHUP);
207 sigaddset(&sv.sa_mask, SIGTERM);
208 sv.sa_flags = SA_RESTART;
209 if ( sigaction( SIGALRM, &sv, NULL ) < 0 ) {
210 LOG(log_error, logtype_afpd, "afp_timedown: sigaction: %s", strerror(errno) );
211 afp_dsi_die(EXITERR_SYS);
215 sv.sa_handler = SIG_IGN;
216 sigemptyset( &sv.sa_mask );
217 sv.sa_flags = SA_RESTART;
218 if ( sigaction( SIGUSR1, &sv, NULL ) < 0 ) {
219 LOG(log_error, logtype_afpd, "afp_timedown: sigaction SIGHUP: %s", strerror(errno) );
220 afp_dsi_die(EXITERR_SYS);
224 /* ---------------------------------
225 * SIGHUP reload configuration file
227 volatile int reload_request = 0;
229 static void afp_dsi_reload(int sig _U_)
234 /* ---------------------------------
235 * SIGINT: enable max_debug LOGging
237 static volatile sig_atomic_t debug_request = 0;
239 static void afp_dsi_debug(int sig _U_)
244 /* ---------------------- */
245 static void afp_dsi_getmesg (int sig _U_)
247 DSI *dsi = (DSI *)AFPobj->handle;
249 dsi->msg_request = 1;
250 if (dsi_attention(AFPobj->handle, AFPATTN_MESG | AFPATTN_TIME(5)) < 0)
251 dsi->msg_request = 2;
254 static void alarm_handler(int sig _U_)
257 DSI *dsi = (DSI *)AFPobj->handle;
259 /* we have to restart the timer because some libraries may use alarm() */
260 setitimer(ITIMER_REAL, &dsi->timer, NULL);
262 /* we got some traffic from the client since the previous timer tick. */
263 if ((dsi->flags & DSI_DATA)) {
264 dsi->flags &= ~DSI_DATA;
269 LOG(log_maxdebug, logtype_afpd, "alarm: tickles: %u, flags: %s|%s|%s|%s|%s|%s|%s|%s|%s",
271 (dsi->flags & DSI_DATA) ? "DSI_DATA" : "-",
272 (dsi->flags & DSI_RUNNING) ? "DSI_RUNNING" : "-",
273 (dsi->flags & DSI_SLEEPING) ? "DSI_SLEEPING" : "-",
274 (dsi->flags & DSI_EXTSLEEP) ? "DSI_EXTSLEEP" : "-",
275 (dsi->flags & DSI_DISCONNECTED) ? "DSI_DISCONNECTED" : "-",
276 (dsi->flags & DSI_DIE) ? "DSI_DIE" : "-",
277 (dsi->flags & DSI_NOREPLY) ? "DSI_NOREPLY" : "-",
278 (dsi->flags & DSI_RECONSOCKET) ? "DSI_RECONSOCKET" : "-",
279 (dsi->flags & DSI_RECONINPROG) ? "DSI_RECONINPROG" : "-");
281 if (dsi->flags & DSI_SLEEPING) {
282 if (dsi->tickle > AFPobj->options.sleep) {
283 LOG(log_note, logtype_afpd, "afp_alarm: sleep time ended");
284 afp_dsi_die(EXITERR_CLNT);
289 if (dsi->flags & DSI_DISCONNECTED) {
290 if (dsi->tickle > AFPobj->options.disconnected) {
291 LOG(log_error, logtype_afpd, "afp_alarm: reconnect timer expired, goodbye");
292 afp_dsi_die(EXITERR_CLNT);
297 /* if we're in the midst of processing something, don't die. */
298 if ( !(dsi->flags & DSI_RUNNING) && (dsi->tickle >= AFPobj->options.timeout)) {
299 LOG(log_error, logtype_afpd, "afp_alarm: child timed out, entering disconnected state");
300 dsi->proto_close(dsi);
301 dsi->flags |= DSI_DISCONNECTED;
305 if ((err = pollvoltime(AFPobj)) == 0)
306 LOG(log_debug, logtype_afpd, "afp_alarm: sending DSI tickle");
307 err = dsi_tickle(AFPobj->handle);
309 LOG(log_error, logtype_afpd, "afp_alarm: connection problem, entering disconnected state");
310 dsi->proto_close(dsi);
311 dsi->flags |= DSI_DISCONNECTED;
316 if dsi->in_write is set attention, tickle (and close?) msg
317 aren't sent. We don't care about tickle
319 static void pending_request(DSI *dsi)
321 /* send pending attention */
323 /* read msg if any, it could be done in afp_getsrvrmesg */
324 if (dsi->msg_request) {
325 if (dsi->msg_request == 2) {
326 /* didn't send it in signal handler */
327 dsi_attention(AFPobj->handle, AFPATTN_MESG | AFPATTN_TIME(5));
329 dsi->msg_request = 0;
332 if (dsi->down_request) {
333 dsi->down_request = 0;
334 dsi_attention(AFPobj->handle, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
335 AFPATTN_MESG | AFPATTN_TIME(5));
339 /* -------------------------------------------
340 afp over dsi. this never returns.
342 void afp_over_dsi(AFPObj *obj)
344 DSI *dsi = (DSI *) obj->handle;
348 struct sigaction action;
351 obj->exit = afp_dsi_die;
352 obj->reply = (int (*)()) dsi_cmdreply;
353 obj->attention = (int (*)(void *, AFPUserBytes)) dsi_attention;
356 memset(&action, 0, sizeof(action));
359 action.sa_handler = afp_dsi_reload;
360 sigemptyset( &action.sa_mask );
361 sigaddset(&action.sa_mask, SIGALRM);
362 sigaddset(&action.sa_mask, SIGTERM);
363 sigaddset(&action.sa_mask, SIGUSR1);
364 sigaddset(&action.sa_mask, SIGINT);
365 sigaddset(&action.sa_mask, SIGUSR2);
366 action.sa_flags = SA_RESTART;
367 if ( sigaction( SIGHUP, &action, NULL ) < 0 ) {
368 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
369 afp_dsi_die(EXITERR_SYS);
373 action.sa_handler = afp_dsi_transfer_session;
374 sigemptyset( &action.sa_mask );
375 sigaddset(&action.sa_mask, SIGALRM);
376 sigaddset(&action.sa_mask, SIGTERM);
377 sigaddset(&action.sa_mask, SIGUSR1);
378 sigaddset(&action.sa_mask, SIGINT);
379 sigaddset(&action.sa_mask, SIGUSR2);
380 action.sa_flags = SA_RESTART;
381 if ( sigaction( SIGURG, &action, NULL ) < 0 ) {
382 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
383 afp_dsi_die(EXITERR_SYS);
386 /* install SIGTERM */
387 action.sa_handler = afp_dsi_die;
388 sigemptyset( &action.sa_mask );
389 sigaddset(&action.sa_mask, SIGALRM);
390 sigaddset(&action.sa_mask, SIGHUP);
391 sigaddset(&action.sa_mask, SIGUSR1);
392 sigaddset(&action.sa_mask, SIGINT);
393 sigaddset(&action.sa_mask, SIGUSR2);
394 action.sa_flags = SA_RESTART;
395 if ( sigaction( SIGTERM, &action, NULL ) < 0 ) {
396 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
397 afp_dsi_die(EXITERR_SYS);
400 /* Added for server message support */
401 action.sa_handler = afp_dsi_getmesg;
402 sigemptyset( &action.sa_mask );
403 sigaddset(&action.sa_mask, SIGALRM);
404 sigaddset(&action.sa_mask, SIGTERM);
405 sigaddset(&action.sa_mask, SIGUSR1);
406 sigaddset(&action.sa_mask, SIGHUP);
407 sigaddset(&action.sa_mask, SIGINT);
408 action.sa_flags = SA_RESTART;
409 if ( sigaction( SIGUSR2, &action, NULL) < 0 ) {
410 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
411 afp_dsi_die(EXITERR_SYS);
414 /* SIGUSR1 - set down in 5 minutes */
415 action.sa_handler = afp_dsi_timedown;
416 sigemptyset( &action.sa_mask );
417 sigaddset(&action.sa_mask, SIGALRM);
418 sigaddset(&action.sa_mask, SIGHUP);
419 sigaddset(&action.sa_mask, SIGTERM);
420 sigaddset(&action.sa_mask, SIGINT);
421 sigaddset(&action.sa_mask, SIGUSR2);
422 action.sa_flags = SA_RESTART;
423 if ( sigaction( SIGUSR1, &action, NULL) < 0 ) {
424 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
425 afp_dsi_die(EXITERR_SYS);
428 /* SIGINT - enable max_debug LOGging to /tmp/afpd.PID.XXXXXX */
429 action.sa_handler = afp_dsi_debug;
430 sigfillset( &action.sa_mask );
431 action.sa_flags = SA_RESTART;
432 if ( sigaction( SIGINT, &action, NULL) < 0 ) {
433 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
434 afp_dsi_die(EXITERR_SYS);
439 action.sa_handler = alarm_handler;
440 sigemptyset(&action.sa_mask);
441 sigaddset(&action.sa_mask, SIGHUP);
442 sigaddset(&action.sa_mask, SIGTERM);
443 sigaddset(&action.sa_mask, SIGUSR1);
444 sigaddset(&action.sa_mask, SIGINT);
445 sigaddset(&action.sa_mask, SIGUSR2);
446 action.sa_flags = SA_RESTART;
447 if ((sigaction(SIGALRM, &action, NULL) < 0) ||
448 (setitimer(ITIMER_REAL, &dsi->timer, NULL) < 0)) {
449 afp_dsi_die(EXITERR_SYS);
451 #endif /* DEBUGGING */
453 if (dircache_init(obj->options.dircachesize) != 0)
454 afp_dsi_die(EXITERR_SYS);
456 /* set TCP snd/rcv buf */
457 if (obj->options.tcp_rcvbuf) {
458 if (setsockopt(dsi->socket,
461 &obj->options.tcp_rcvbuf,
462 sizeof(obj->options.tcp_rcvbuf)) != 0) {
463 LOG(log_error, logtype_dsi, "afp_over_dsi: setsockopt(SO_RCVBUF): %s", strerror(errno));
466 if (obj->options.tcp_sndbuf) {
467 if (setsockopt(dsi->socket,
470 &obj->options.tcp_sndbuf,
471 sizeof(obj->options.tcp_sndbuf)) != 0) {
472 LOG(log_error, logtype_dsi, "afp_over_dsi: setsockopt(SO_SNDBUF): %s", strerror(errno));
476 /* get stuck here until the end */
478 if (sigsetjmp(recon_jmp, 1) != 0)
479 /* returning from SIGALARM handler for a primary reconnect */
482 /* Blocking read on the network socket */
483 cmd = dsi_receive(dsi);
486 /* cmd == 0 is the error condition */
487 if (dsi->flags & DSI_RECONSOCKET) {
488 /* we just got a reconnect so we immediately try again to receive on the new fd */
489 dsi->flags &= ~DSI_RECONSOCKET;
492 /* Some error on the client connection, enter disconnected state */
493 dsi->flags |= DSI_DISCONNECTED;
495 /* the client sometimes logs out (afp_logout) but doesn't close the DSI session */
496 if (dsi->flags & DSI_AFP_LOGGED_OUT) {
501 pause(); /* gets interrupted by SIGALARM or SIGURG tickle */
502 continue; /* continue receiving until disconnect timer expires
503 * or a primary reconnect succeeds */
506 if (!(dsi->flags & DSI_EXTSLEEP) && (dsi->flags & DSI_SLEEPING)) {
507 LOG(log_debug, logtype_afpd, "afp_over_dsi: got data, ending normal sleep");
508 dsi->flags &= ~DSI_SLEEPING;
512 if (reload_request) {
514 load_volumes(AFPobj);
517 /* The first SIGINT enables debugging, the next restores the config */
519 static int debugging = 0;
525 if (obj->options.logconfig)
526 setuplog(obj->options.logconfig);
528 setuplog("default log_note");
533 sprintf(logstr, "default log_maxdebug /tmp/afpd.%u.XXXXXX", getpid());
539 dsi->flags |= DSI_DATA;
545 LOG(log_debug, logtype_afpd, "DSI: close session request");
547 LOG(log_note, logtype_afpd, "done");
551 dsi->flags &= ~DSI_DATA; /* thats no data in the sense we use it in alarm_handler */
552 LOG(log_debug, logtype_afpd, "DSI: client tickle");
553 /* timer is not every 30 seconds anymore, so we don't get killed on the client side. */
554 if ((dsi->flags & DSI_DIE))
561 if ( flushfork( writtenfork ) < 0 ) {
562 LOG(log_error, logtype_afpd, "main flushfork: %s", strerror(errno) );
568 function = (u_char) dsi->commands[0];
570 /* AFP replay cache */
571 rc_idx = dsi->clientID % REPLAYCACHE_SIZE;
572 LOG(log_debug, logtype_afpd, "DSI request ID: %u", dsi->clientID);
574 if (replaycache[rc_idx].DSIreqID == dsi->clientID
575 && replaycache[rc_idx].AFPcommand == function) {
576 LOG(log_note, logtype_afpd, "AFP Replay Cache match: id: %u / cmd: %s",
577 dsi->clientID, AfpNum2name(function));
578 err = replaycache[rc_idx].result;
579 /* AFP replay cache end */
581 /* send off an afp command. in a couple cases, we take advantage
582 * of the fact that we're a stream-based protocol. */
583 if (afp_switch[function]) {
584 dsi->datalen = DSI_DATASIZ;
585 dsi->flags |= DSI_RUNNING;
587 LOG(log_debug, logtype_afpd, "<== Start AFP command: %s", AfpNum2name(function));
589 err = (*afp_switch[function])(obj,
590 (char *)&dsi->commands, dsi->cmdlen,
591 (char *)&dsi->data, &dsi->datalen);
593 LOG(log_debug, logtype_afpd, "==> Finished AFP command: %s -> %s",
594 AfpNum2name(function), AfpErr2name(err));
596 dir_free_invalid_q();
598 dsi->flags &= ~DSI_RUNNING;
600 /* Add result to the AFP replay cache */
601 replaycache[rc_idx].DSIreqID = dsi->clientID;
602 replaycache[rc_idx].AFPcommand = function;
603 replaycache[rc_idx].result = err;
605 LOG(log_error, logtype_afpd, "bad function %X", function);
611 /* single shot toggle that gets set by dsi_readinit. */
612 if (dsi->flags & DSI_NOREPLY) {
613 dsi->flags &= ~DSI_NOREPLY;
617 if (!dsi_cmdreply(dsi, err)) {
618 LOG(log_error, logtype_afpd, "dsi_cmdreply(%d): %s", dsi->socket, strerror(errno) );
619 dsi->flags |= DSI_DISCONNECTED;
623 case DSIFUNC_WRITE: /* FPWrite and FPAddIcon */
624 function = (u_char) dsi->commands[0];
625 if ( afp_switch[ function ] != NULL ) {
626 dsi->datalen = DSI_DATASIZ;
627 dsi->flags |= DSI_RUNNING;
629 LOG(log_debug, logtype_afpd, "<== Start AFP command: %s", AfpNum2name(function));
631 err = (*afp_switch[function])(obj,
632 (char *)&dsi->commands, dsi->cmdlen,
633 (char *)&dsi->data, &dsi->datalen);
635 LOG(log_debug, logtype_afpd, "==> Finished AFP command: %s -> %s",
636 AfpNum2name(function), AfpErr2name(err));
638 dsi->flags &= ~DSI_RUNNING;
640 LOG(log_error, logtype_afpd, "(write) bad function %x", function);
645 if (!dsi_wrtreply(dsi, err)) {
646 LOG(log_error, logtype_afpd, "dsi_wrtreply: %s", strerror(errno) );
647 dsi->flags |= DSI_DISCONNECTED;
651 case DSIFUNC_ATTN: /* attention replies */
654 /* error. this usually implies a mismatch of some kind
655 * between server and client. if things are correct,
656 * we need to flush the rest of the packet if necessary. */
658 LOG(log_info, logtype_afpd,"afp_dsi: spurious command %d", cmd);
659 dsi_writeinit(dsi, dsi->data, DSI_DATASIZ);
663 pending_request(dsi);
667 afp_dsi_die(EXITERR_CLNT);