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>
44 #endif /* FORCE_UIDGID */
47 * We generally pass this from afp_over_dsi to all afp_* funcs, so it should already be
48 * available everywhere. Unfortunately some funcs (eg acltoownermode) need acces to it
49 * but are deeply nested in the function chain with the caller already without acces to it.
50 * Changing this would require adding a reference to the caller which itself might be
51 * called in many places (eg acltoownermode is called from accessmode).
52 * The only sane way out is providing a copy of it here:
54 AFPObj *AFPobj = NULL;
65 * - indexed just by taking DSIreqID mod REPLAYCACHE_SIZE
67 static rc_elem_t replaycache[REPLAYCACHE_SIZE];
69 static sigjmp_buf recon_jmp;
70 static int oldsock = -1;
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;
108 static volatile int in_handler;
113 /* it's not atomic but we don't care because it's an exit function
114 * ie if a signal is received here, between the test and the affectation,
115 * it will not return.
119 if (dsi->flags & DSI_RECONINPROG) {
120 /* Primary reconnect succeeded, got SIGTERM from afpd parent */
121 dsi->flags &= ~DSI_RECONINPROG;
122 return; /* this returns to afp_disconnect */
125 if (dsi->flags & DSI_DISCONNECTED) {
126 LOG(log_note, logtype_afpd, "Disconnected session terminating");
130 dsi_attention(AFPobj->handle, AFPATTN_SHUTDOWN);
131 afp_dsi_close(AFPobj);
132 if (sig) /* if no signal, assume dieing because logins are disabled &
133 don't log it (maintenance mode)*/
134 LOG(log_info, logtype_afpd, "Connection terminated");
135 if (sig == SIGTERM || sig == SIGALRM) {
143 /* SIGURG handler (primary reconnect) */
144 static void afp_dsi_transfer_session(int sig _U_)
148 DSI *dsi = (DSI *)AFPobj->handle;
150 LOG(log_note, logtype_afpd, "afp_dsi_transfer_session: got SIGURG, trying to receive session");
152 if (readt(AFPobj->ipc_fd, &dsiID, 2, 0, 2) != 2) {
153 LOG(log_error, logtype_afpd, "afp_dsi_transfer_session: couldn't receive DSI id, goodbye");
154 afp_dsi_close(AFPobj);
158 if ((socket = recv_fd(AFPobj->ipc_fd, 1)) == -1) {
159 LOG(log_error, logtype_afpd, "afp_dsi_transfer_session: couldn't receive session fd, goodbye");
160 afp_dsi_close(AFPobj);
164 LOG(log_note, logtype_afpd, "afp_dsi_transfer_session: received socket fd: %i", socket);
166 dsi->socket = socket;
167 dsi->flags = DSI_RECONSOCKET;
169 dsi->eof = dsi->start = dsi->buffer;
171 dsi->header.dsi_requestID = dsiID;
172 dsi->header.dsi_command = DSIFUNC_CMD;
175 * The session transfer happens in the middle of FPDisconnect old session, thus we
176 * have to send the reply now.
178 if (!dsi_cmdreply(dsi, AFP_OK)) {
179 LOG(log_error, logtype_afpd, "dsi_cmdreply: %s", strerror(errno) );
180 afp_dsi_close(AFPobj);
184 LOG(log_note, logtype_afpd, "afp_dsi_transfer_session: succesfull primary reconnect");
186 * Now returning from this signal handler return to dsi_receive which should start
187 * reading/continuing from the connected socket that was passed via the parent from
188 * another session. The parent will terminate that session.
190 siglongjmp(recon_jmp, 1);
193 /* ------------------- */
194 static void afp_dsi_timedown(int sig _U_)
198 DSI *dsi = (DSI *)AFPobj->handle;
199 dsi->flags |= DSI_DIE;
200 /* shutdown and don't reconnect. server going down in 5 minutes. */
201 setmessage("The server is going down for maintenance.");
202 if (dsi_attention(AFPobj->handle, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
203 AFPATTN_MESG | AFPATTN_TIME(5)) < 0) {
204 DSI *dsi = (DSI *)AFPobj->handle;
205 dsi->down_request = 1;
208 it.it_interval.tv_sec = 0;
209 it.it_interval.tv_usec = 0;
210 it.it_value.tv_sec = 300;
211 it.it_value.tv_usec = 0;
213 if ( setitimer( ITIMER_REAL, &it, NULL ) < 0 ) {
214 LOG(log_error, logtype_afpd, "afp_timedown: setitimer: %s", strerror(errno) );
215 afp_dsi_die(EXITERR_SYS);
217 memset(&sv, 0, sizeof(sv));
218 sv.sa_handler = afp_dsi_die;
219 sigemptyset( &sv.sa_mask );
220 sigaddset(&sv.sa_mask, SIGHUP);
221 sigaddset(&sv.sa_mask, SIGTERM);
222 sv.sa_flags = SA_RESTART;
223 if ( sigaction( SIGALRM, &sv, NULL ) < 0 ) {
224 LOG(log_error, logtype_afpd, "afp_timedown: sigaction: %s", strerror(errno) );
225 afp_dsi_die(EXITERR_SYS);
229 sv.sa_handler = SIG_IGN;
230 sigemptyset( &sv.sa_mask );
231 sv.sa_flags = SA_RESTART;
232 if ( sigaction( SIGUSR1, &sv, NULL ) < 0 ) {
233 LOG(log_error, logtype_afpd, "afp_timedown: sigaction SIGHUP: %s", strerror(errno) );
234 afp_dsi_die(EXITERR_SYS);
238 /* ---------------------------------
239 * SIGHUP reload configuration file
241 volatile int reload_request = 0;
243 static void afp_dsi_reload(int sig _U_)
248 /* ---------------------------------
249 * SIGINT: enable max_debug LOGging
251 static volatile sig_atomic_t debug_request = 0;
253 static void afp_dsi_debug(int sig _U_)
258 /* ---------------------- */
259 static void afp_dsi_getmesg (int sig _U_)
261 DSI *dsi = (DSI *)AFPobj->handle;
263 dsi->msg_request = 1;
264 if (dsi_attention(AFPobj->handle, AFPATTN_MESG | AFPATTN_TIME(5)) < 0)
265 dsi->msg_request = 2;
268 static void alarm_handler(int sig _U_)
271 DSI *dsi = (DSI *)AFPobj->handle;
273 /* we have to restart the timer because some libraries may use alarm() */
274 setitimer(ITIMER_REAL, &dsi->timer, NULL);
276 /* we got some traffic from the client since the previous timer tick. */
277 if ((dsi->flags & DSI_DATA)) {
278 dsi->flags &= ~DSI_DATA;
283 LOG(log_maxdebug, logtype_afpd, "alarm: tickles: %u, flags: %s|%s|%s|%s|%s|%s|%s|%s|%s",
285 (dsi->flags & DSI_DATA) ? "DSI_DATA" : "-",
286 (dsi->flags & DSI_RUNNING) ? "DSI_RUNNING" : "-",
287 (dsi->flags & DSI_SLEEPING) ? "DSI_SLEEPING" : "-",
288 (dsi->flags & DSI_EXTSLEEP) ? "DSI_EXTSLEEP" : "-",
289 (dsi->flags & DSI_DISCONNECTED) ? "DSI_DISCONNECTED" : "-",
290 (dsi->flags & DSI_DIE) ? "DSI_DIE" : "-",
291 (dsi->flags & DSI_NOREPLY) ? "DSI_NOREPLY" : "-",
292 (dsi->flags & DSI_RECONSOCKET) ? "DSI_RECONSOCKET" : "-",
293 (dsi->flags & DSI_RECONINPROG) ? "DSI_RECONINPROG" : "-");
295 if (dsi->flags & DSI_SLEEPING) {
296 if (dsi->tickle > AFPobj->options.sleep) {
297 LOG(log_error, logtype_afpd, "afp_alarm: sleep time ended");
298 afp_dsi_die(EXITERR_CLNT);
303 if (dsi->flags & DSI_DISCONNECTED) {
304 if (dsi->tickle > AFPobj->options.disconnected) {
305 LOG(log_error, logtype_afpd, "afp_alarm: no reconnect within 10 minutes, goodbye");
306 afp_dsi_die(EXITERR_CLNT);
311 /* if we're in the midst of processing something, don't die. */
312 if ( !(dsi->flags & DSI_RUNNING) && (dsi->tickle >= AFPobj->options.timeout)) {
313 LOG(log_error, logtype_afpd, "afp_alarm: child timed out, entering disconnected state");
314 dsi->proto_close(dsi);
315 dsi->flags |= DSI_DISCONNECTED;
319 if ((err = pollvoltime(AFPobj)) == 0)
320 err = dsi_tickle(AFPobj->handle);
322 LOG(log_error, logtype_afpd, "afp_alarm: connection problem, entering disconnected state");
323 dsi->flags |= DSI_DISCONNECTED;
328 if dsi->in_write is set attention, tickle (and close?) msg
329 aren't sent. We don't care about tickle
331 static void pending_request(DSI *dsi)
333 /* send pending attention */
335 /* read msg if any, it could be done in afp_getsrvrmesg */
336 if (dsi->msg_request) {
337 if (dsi->msg_request == 2) {
338 /* didn't send it in signal handler */
339 dsi_attention(AFPobj->handle, AFPATTN_MESG | AFPATTN_TIME(5));
341 dsi->msg_request = 0;
344 if (dsi->down_request) {
345 dsi->down_request = 0;
346 dsi_attention(AFPobj->handle, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
347 AFPATTN_MESG | AFPATTN_TIME(5));
351 /* -------------------------------------------
352 afp over dsi. this never returns.
354 void afp_over_dsi(AFPObj *obj)
356 DSI *dsi = (DSI *) obj->handle;
360 struct sigaction action;
363 obj->exit = afp_dsi_die;
364 obj->reply = (int (*)()) dsi_cmdreply;
365 obj->attention = (int (*)(void *, AFPUserBytes)) dsi_attention;
368 memset(&action, 0, sizeof(action));
371 action.sa_handler = afp_dsi_reload;
372 sigemptyset( &action.sa_mask );
373 sigaddset(&action.sa_mask, SIGALRM);
374 sigaddset(&action.sa_mask, SIGTERM);
375 sigaddset(&action.sa_mask, SIGUSR1);
376 sigaddset(&action.sa_mask, SIGINT);
377 sigaddset(&action.sa_mask, SIGUSR2);
378 action.sa_flags = SA_RESTART;
379 if ( sigaction( SIGHUP, &action, NULL ) < 0 ) {
380 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
381 afp_dsi_die(EXITERR_SYS);
385 action.sa_handler = afp_dsi_transfer_session;
386 sigemptyset( &action.sa_mask );
387 sigaddset(&action.sa_mask, SIGALRM);
388 sigaddset(&action.sa_mask, SIGTERM);
389 sigaddset(&action.sa_mask, SIGUSR1);
390 sigaddset(&action.sa_mask, SIGINT);
391 sigaddset(&action.sa_mask, SIGUSR2);
392 action.sa_flags = SA_RESTART;
393 if ( sigaction( SIGURG, &action, NULL ) < 0 ) {
394 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
395 afp_dsi_die(EXITERR_SYS);
398 /* install SIGTERM */
399 action.sa_handler = afp_dsi_die;
400 sigemptyset( &action.sa_mask );
401 sigaddset(&action.sa_mask, SIGALRM);
402 sigaddset(&action.sa_mask, SIGHUP);
403 sigaddset(&action.sa_mask, SIGUSR1);
404 sigaddset(&action.sa_mask, SIGINT);
405 sigaddset(&action.sa_mask, SIGUSR2);
406 action.sa_flags = SA_RESTART;
407 if ( sigaction( SIGTERM, &action, NULL ) < 0 ) {
408 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
409 afp_dsi_die(EXITERR_SYS);
412 /* Added for server message support */
413 action.sa_handler = afp_dsi_getmesg;
414 sigemptyset( &action.sa_mask );
415 sigaddset(&action.sa_mask, SIGALRM);
416 sigaddset(&action.sa_mask, SIGTERM);
417 sigaddset(&action.sa_mask, SIGUSR1);
418 sigaddset(&action.sa_mask, SIGHUP);
419 sigaddset(&action.sa_mask, SIGINT);
420 action.sa_flags = SA_RESTART;
421 if ( sigaction( SIGUSR2, &action, NULL) < 0 ) {
422 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
423 afp_dsi_die(EXITERR_SYS);
426 /* SIGUSR1 - set down in 5 minutes */
427 action.sa_handler = afp_dsi_timedown;
428 sigemptyset( &action.sa_mask );
429 sigaddset(&action.sa_mask, SIGALRM);
430 sigaddset(&action.sa_mask, SIGHUP);
431 sigaddset(&action.sa_mask, SIGTERM);
432 sigaddset(&action.sa_mask, SIGINT);
433 sigaddset(&action.sa_mask, SIGUSR2);
434 action.sa_flags = SA_RESTART;
435 if ( sigaction( SIGUSR1, &action, NULL) < 0 ) {
436 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
437 afp_dsi_die(EXITERR_SYS);
440 /* SIGINT - enable max_debug LOGging to /tmp/afpd.PID.XXXXXX */
441 action.sa_handler = afp_dsi_debug;
442 sigfillset( &action.sa_mask );
443 action.sa_flags = SA_RESTART;
444 if ( sigaction( SIGINT, &action, NULL) < 0 ) {
445 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
446 afp_dsi_die(EXITERR_SYS);
451 action.sa_handler = alarm_handler;
452 sigemptyset(&action.sa_mask);
453 sigaddset(&action.sa_mask, SIGHUP);
454 sigaddset(&action.sa_mask, SIGTERM);
455 sigaddset(&action.sa_mask, SIGUSR1);
456 sigaddset(&action.sa_mask, SIGINT);
457 sigaddset(&action.sa_mask, SIGUSR2);
458 action.sa_flags = SA_RESTART;
459 if ((sigaction(SIGALRM, &action, NULL) < 0) ||
460 (setitimer(ITIMER_REAL, &dsi->timer, NULL) < 0)) {
461 afp_dsi_die(EXITERR_SYS);
463 #endif /* DEBUGGING */
465 if (dircache_init(obj->options.dircachesize) != 0)
466 afp_dsi_die(EXITERR_SYS);
468 /* get stuck here until the end */
470 if (sigsetjmp(recon_jmp, 1) != 0)
471 /* returning from SIGALARM handler for a primary reconnect */
474 /* Blocking read on the network socket */
475 cmd = dsi_receive(dsi);
478 /* cmd == 0 is the error condition */
479 if (dsi->flags & DSI_RECONSOCKET) {
480 /* we just got a reconnect so we immediately try again to receive on the new fd */
481 dsi->flags &= ~DSI_RECONSOCKET;
484 /* Some error on the client connection, enter disconnected state */
485 dsi->flags |= DSI_DISCONNECTED;
486 pause(); /* gets interrupted by SIGALARM or SIGURG tickle */
487 continue; /* continue receiving until disconnect timer expires
488 * or a primary reconnect succeeds */
491 /* Now, after successfully reading from the new socket after a reconnect *
492 * we can close the old socket without taking the risk of confusing the client */
498 if (!(dsi->flags & DSI_EXTSLEEP)) {
499 LOG(log_debug, logtype_afpd, "afp_over_dsi: got data, ending normal sleep");
500 dsi->flags &= ~DSI_SLEEPING;
504 if (reload_request) {
506 load_volumes(AFPobj);
511 /* The first SIGINT enables debugging, the next restores the config */
513 static int debugging = 0;
517 if (obj->options.logconfig)
518 setuplog(obj->options.logconfig);
520 setuplog("default log_note");
525 sprintf(logstr, "default log_maxdebug /tmp/afpd.%u.XXXXXX", getpid());
530 if (cmd == DSIFUNC_TICKLE) {
531 /* timer is not every 30 seconds anymore, so we don't get killed on the client side. */
532 if ((dsi->flags & DSI_DIE))
534 pending_request(dsi);
538 dsi->flags |= DSI_DATA;
542 LOG(log_note, logtype_afpd, "done");
549 if ( flushfork( writtenfork ) < 0 ) {
550 LOG(log_error, logtype_afpd, "main flushfork: %s", strerror(errno) );
556 function = (u_char) dsi->commands[0];
558 /* AFP replay cache */
559 rc_idx = REPLAYCACHE_SIZE % dsi->clientID;
560 LOG(log_debug, logtype_afpd, "DSI request ID: %u", dsi->clientID);
562 if (replaycache[rc_idx].DSIreqID == dsi->clientID
563 && replaycache[rc_idx].AFPcommand == function) {
564 LOG(log_debug, logtype_afpd, "AFP Replay Cache match: id: %u / cmd: %s",
565 dsi->clientID, AfpNum2name(function));
566 err = replaycache[rc_idx].result;
567 /* AFP replay cache end */
569 /* send off an afp command. in a couple cases, we take advantage
570 * of the fact that we're a stream-based protocol. */
571 if (afp_switch[function]) {
572 dsi->datalen = DSI_DATASIZ;
573 dsi->flags |= DSI_RUNNING;
575 LOG(log_debug, logtype_afpd, "<== Start AFP command: %s", AfpNum2name(function));
577 err = (*afp_switch[function])(obj,
578 (char *)&dsi->commands, dsi->cmdlen,
579 (char *)&dsi->data, &dsi->datalen);
581 LOG(log_debug, logtype_afpd, "==> Finished AFP command: %s -> %s",
582 AfpNum2name(function), AfpErr2name(err));
584 dir_free_invalid_q();
587 /* bring everything back to old euid, egid */
589 restore_uidgid ( &obj->uidgid );
590 #endif /* FORCE_UIDGID */
591 dsi->flags &= ~DSI_RUNNING;
593 /* Add result to the AFP replay cache */
594 replaycache[rc_idx].DSIreqID = dsi->clientID;
595 replaycache[rc_idx].AFPcommand = function;
596 replaycache[rc_idx].result = err;
598 LOG(log_error, logtype_afpd, "bad function %X", function);
604 /* single shot toggle that gets set by dsi_readinit. */
605 if (dsi->flags & DSI_NOREPLY) {
606 dsi->flags &= ~DSI_NOREPLY;
610 if (!dsi_cmdreply(dsi, err)) {
611 LOG(log_error, logtype_afpd, "dsi_cmdreply(%d): %s", dsi->socket, strerror(errno) );
612 dsi->flags |= DSI_DISCONNECTED;
616 case DSIFUNC_WRITE: /* FPWrite and FPAddIcon */
617 function = (u_char) dsi->commands[0];
618 if ( afp_switch[ function ] != NULL ) {
619 dsi->datalen = DSI_DATASIZ;
620 dsi->flags |= DSI_RUNNING;
622 LOG(log_debug, logtype_afpd, "<== Start AFP command: %s", AfpNum2name(function));
624 err = (*afp_switch[function])(obj,
625 (char *)&dsi->commands, dsi->cmdlen,
626 (char *)&dsi->data, &dsi->datalen);
628 LOG(log_debug, logtype_afpd, "==> Finished AFP command: %s -> %s",
629 AfpNum2name(function), AfpErr2name(err));
631 dsi->flags &= ~DSI_RUNNING;
633 /* bring everything back to old euid, egid */
635 restore_uidgid ( &obj->uidgid );
636 #endif /* FORCE_UIDGID */
638 LOG(log_error, logtype_afpd, "(write) bad function %x", function);
643 if (!dsi_wrtreply(dsi, err)) {
644 LOG(log_error, logtype_afpd, "dsi_wrtreply: %s", strerror(errno) );
645 dsi->flags |= DSI_DISCONNECTED;
649 case DSIFUNC_ATTN: /* attention replies */
652 /* error. this usually implies a mismatch of some kind
653 * between server and client. if things are correct,
654 * we need to flush the rest of the packet if necessary. */
656 LOG(log_info, logtype_afpd,"afp_dsi: spurious command %d", cmd);
657 dsi_writeinit(dsi, dsi->data, DSI_DATASIZ);
661 pending_request(dsi);
665 afp_dsi_die(EXITERR_CLNT);