]> arthur.barton.de Git - netatalk.git/blobdiff - etc/afpd/afp_dsi.c
AFP replay cache, first shot
[netatalk.git] / etc / afpd / afp_dsi.c
index a58e6c874bf96722989ac449490ff7d504cd163f..c90c8a70f50364e5f9e957a181c98735b9db0858 100644 (file)
@@ -1,6 +1,4 @@
 /*
- * $Id: afp_dsi.c,v 1.51 2010-02-16 02:37:38 didg Exp $
- *
  * Copyright (c) 1999 Adrian Sun (asun@zoology.washington.edu)
  * Copyright (c) 1990,1993 Regents of The University of Michigan.
  * All Rights Reserved.  See COPYRIGHT.
 #include "switch.h"
 #include "auth.h"
 #include "fork.h"
+#include "dircache.h"
 
 #ifdef FORCE_UIDGID
 #warning UIDGID
 #include "uid.h"
 #endif /* FORCE_UIDGID */
 
-#define CHILD_DIE         (1 << 0)
-#define CHILD_RUNNING     (1 << 1)
-#define CHILD_SLEEPING    (1 << 2)
-#define CHILD_DATA        (1 << 3)
+#define CHILD_DIE          (1 << 0)
+#define CHILD_RUNNING      (1 << 1)
+#define CHILD_SLEEPING     (1 << 2)
+#define CHILD_DATA         (1 << 3)
+#define CHILD_DISCONNECTED (1 << 4)
+
+/* 
+ * We generally pass this from afp_over_dsi to all afp_* funcs, so it should already be
+ * available everywhere. Unfortunately some funcs (eg acltoownermode) need acces to it
+ * but are deeply nested in the function chain with the caller already without acces to it.
+ * Changing this would require adding a reference to the caller which itself might be
+ * called in many places (eg acltoownermode is called from accessmode).
+ * The only sane way out is providing a copy of it here:
+ */
+AFPObj *AFPobj = NULL;
 
 static struct {
     AFPObj *obj;
@@ -54,25 +64,45 @@ static struct {
     int tickle;
 } child;
 
+typedef struct {
+    uint16_t DSIreqID;
+    uint8_t  AFPcommand;
+    uint32_t result;
+} rc_elem_t;
+
+/*
+ * AFP replay cache:
+ * - fix sized array
+ * - indexed just by taking DSIreqID mod REPLAYCACHE_SIZE
+ */
+rc_elem_t replaycache[REPLAYCACHE_SIZE];
 
 static void afp_dsi_close(AFPObj *obj)
 {
     DSI *dsi = obj->handle;
 
+    close(obj->ipc_fd);
+    obj->ipc_fd = -1;
+
     /* we may have been called from a signal handler caught when afpd was running
      * as uid 0, that's the wrong user for volume's prexec_close scripts if any,
      * restore our login user
      */
-    if (seteuid( obj->uid ) < 0) {
-        LOG(log_error, logtype_afpd, "can't seteuid back %s", strerror(errno));
-        exit(EXITERR_SYS);
+    if (geteuid() != obj->uid) {
+        if (seteuid( obj->uid ) < 0) {
+            LOG(log_error, logtype_afpd, "can't seteuid(%u) back %s: uid: %u, euid: %u", 
+                obj->uid, strerror(errno), getuid(), geteuid());
+            exit(EXITERR_SYS);
+        }
     }
+
     close_all_vol();
     if (obj->logout)
         (*obj->logout)();
 
-    LOG(log_info, logtype_afpd, "%.2fKB read, %.2fKB written",
+    LOG(log_info, logtype_afpd, "AFP statistics: %.2f KB read, %.2f KB written",
         dsi->read_count/1024.0, dsi->write_count/1024.0);
+    log_dircache_stat();
 
     dsi_close(dsi);
 }
@@ -107,6 +137,49 @@ static volatile int in_handler;
     }
 }
 
+static void afp_dsi_transfer_session(int sig _U_)
+{
+    uint16_t dsiID;
+    int socket;
+    DSI *dsi = (DSI *)child.obj->handle;
+
+    LOG(log_note, logtype_afpd, "afp_dsi_transfer_session: got SIGURG, trying to receive session fd");
+
+    if (readt(child.obj->ipc_fd, &dsiID, 2, 0, 2) != 2) {
+        LOG(log_error, logtype_afpd, "afp_dsi_transfer_session: couldn't receive DSI id, goodbye");
+        afp_dsi_close(child.obj);
+        exit(EXITERR_SYS);
+    }
+
+    if ((socket = recv_fd(child.obj->ipc_fd, 1)) == -1) {
+        LOG(log_error, logtype_afpd, "afp_dsi_transfer_session: couldn't receive session fd, goodbye");
+        afp_dsi_close(child.obj);
+        exit(EXITERR_SYS);
+    }
+
+    close(dsi->socket);
+    dsi->socket = socket;
+    dsi->header.dsi_requestID = dsiID;
+    dsi->header.dsi_len = 0;
+    dsi->header.dsi_code = AFP_OK;
+    dsi->header.dsi_command = DSIFUNC_CMD;
+    dsi->header.dsi_flags = DSIFL_REPLY;
+
+    if (!dsi_cmdreply(dsi, AFP_OK)) {
+        LOG(log_error, logtype_afpd, "dsi_cmdreply: %s", strerror(errno) );
+        afp_dsi_close(child.obj);
+        exit(EXITERR_CLNT);
+    }
+
+
+    LOG(log_note, logtype_afpd, "afp_dsi_transfer_session: succesfull primary reconnect");
+    /* 
+     * Now returning from this signal handler return to dsi_receive which should start
+     * reading/continuing from the connected socket that was passed via the parent from
+     * another session. The parent will terminate that session.
+     */
+}
+
 /* */
 static void afp_dsi_sleep(void)
 {
@@ -161,8 +234,7 @@ static void afp_dsi_timedown(int sig _U_)
 
 /* ---------------------------------
  * SIGHUP reload configuration file
- * FIXME here or we wait ?
-*/
+ */
 volatile int reload_request = 0;
 
 static void afp_dsi_reload(int sig _U_)
@@ -197,15 +269,20 @@ static void alarm_handler(int sig _U_)
     int err;
     DSI *dsi = (DSI *) child.obj->handle;
 
-    /* we have to restart the timer because some libraries 
-     * may use alarm() */
+    if (child.flags & CHILD_DISCONNECTED) {
+        LOG(log_note, logtype_afpd, "afp_alarm: no reconnect within 10 hours, goodbye");
+        afp_dsi_die(EXITERR_CLNT);
+    }
+
+    /* we have to restart the timer because some libraries may use alarm() */
     setitimer(ITIMER_REAL, &dsi->timer, NULL);
 
     /* we got some traffic from the client since the previous timer 
      * tick. */
     if ((child.flags & CHILD_DATA)) {
         child.flags &= ~CHILD_DATA;
-        return;
+        child.flags &= ~CHILD_DISCONNECTED;
+       return;
     }
 
     /* if we're in the midst of processing something,
@@ -219,10 +296,11 @@ static void alarm_handler(int sig _U_)
             err = dsi_tickle(child.obj->handle);
         if (err <= 0) 
             afp_dsi_die(EXITERR_CLNT);
-        
-    } else { /* didn't receive a tickle. close connection */
-        LOG(log_error, logtype_afpd, "afp_alarm: child timed out");
-        afp_dsi_die(EXITERR_CLNT);
+    } else { /* didn't receive a tickle, enter disconnected state */
+        LOG(log_error, logtype_afpd, "afp_alarm: child timed out, entering disconnected state");
+        struct itimerval t = {{0, 60 * 60 * 10}, {0, 0}}; /* 10 hours */
+        setitimer(ITIMER_REAL, &t, NULL);
+        child.flags |= CHILD_DISCONNECTED;
     }
 }
 
@@ -256,10 +334,12 @@ static void pending_request(DSI *dsi)
 void afp_over_dsi(AFPObj *obj)
 {
     DSI *dsi = (DSI *) obj->handle;
+    int rc_idx;
     u_int32_t err, cmd;
     u_int8_t function;
     struct sigaction action;
 
+    AFPobj = obj;
     obj->exit = afp_dsi_die;
     obj->reply = (int (*)()) dsi_cmdreply;
     obj->attention = (int (*)(void *, AFPUserBytes)) dsi_attention;
@@ -286,6 +366,22 @@ void afp_over_dsi(AFPObj *obj)
         afp_dsi_die(EXITERR_SYS);
     }
 
+    /* install SIGURG */
+    action.sa_handler = afp_dsi_transfer_session;
+    sigemptyset( &action.sa_mask );
+    sigaddset(&action.sa_mask, SIGALRM);
+    sigaddset(&action.sa_mask, SIGTERM);
+    sigaddset(&action.sa_mask, SIGUSR1);
+    sigaddset(&action.sa_mask, SIGINT);
+#ifdef SERVERTEXT
+    sigaddset(&action.sa_mask, SIGUSR2);
+#endif    
+    action.sa_flags = SA_RESTART;
+    if ( sigaction( SIGURG, &action, NULL ) < 0 ) {
+        LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
+        afp_dsi_die(EXITERR_SYS);
+    }
+
     /* install SIGTERM */
     action.sa_handler = afp_dsi_die;
     sigemptyset( &action.sa_mask );
@@ -361,6 +457,9 @@ void afp_over_dsi(AFPObj *obj)
     }
 #endif /* DEBUGGING */
 
+    if (dircache_init(obj->options.dircachesize) != 0)
+        afp_dsi_die(EXITERR_SYS);
+
     /* get stuck here until the end */
     while ((cmd = dsi_receive(dsi))) {
         child.tickle = 0;
@@ -370,23 +469,27 @@ void afp_over_dsi(AFPObj *obj)
         if (reload_request) {
             reload_request = 0;
             load_volumes(child.obj);
+            dircache_dump();
+            log_dircache_stat();
         }
 
+        /* The first SIGINT enables debugging, the next restores the config */
         if (debug_request) {
-            char logstr[50];
+            static int debugging = 0;
             debug_request = 0;
 
-            /* The first SIGINT enables debugging, the second one kills us */
-            action.sa_handler = afp_dsi_die;
-            sigfillset( &action.sa_mask );
-            action.sa_flags = SA_RESTART;
-            if ( sigaction( SIGINT, &action, NULL ) < 0 ) {
-                LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
-                afp_dsi_die(EXITERR_SYS);
+            if (debugging) {
+                if (obj->options.logconfig)
+                    setuplog(obj->options.logconfig);
+                else
+                    setuplog("default log_note");
+                debugging = 0;
+            } else {
+                char logstr[50];
+                debugging = 1;
+                sprintf(logstr, "default log_maxdebug /tmp/afpd.%u.XXXXXX", getpid());
+                setuplog(logstr);
             }
-
-            sprintf(logstr, "default log_maxdebug /tmp/afpd.%u.XXXXXX", getpid());
-            setuplog(logstr);
         }
 
         if (cmd == DSIFUNC_TICKLE) {
@@ -417,30 +520,50 @@ void afp_over_dsi(AFPObj *obj)
 
             function = (u_char) dsi->commands[0];
 
-            /* send off an afp command. in a couple cases, we take advantage
-             * of the fact that we're a stream-based protocol. */
-            if (afp_switch[function]) {
-                dsi->datalen = DSI_DATASIZ;
-                child.flags |= CHILD_RUNNING;
+            /* AFP replay cache */
+            rc_idx = REPLAYCACHE_SIZE % dsi->clientID;
+            LOG(log_debug, logtype_afpd, "DSI request ID: %u", dsi->clientID);
 
-                LOG(log_debug, logtype_afpd, "<== Start AFP command: %s", AfpNum2name(function));
+            if (replaycache[rc_idx].DSIreqID == dsi->clientID
+                && replaycache[rc_idx].AFPcommand == function) {
+                LOG(log_debug, logtype_afpd, "AFP Replay Cache match: id: %u / cmd: %s",
+                    dsi->clientID, AfpNum2name(function));
+                err = replaycache[rc_idx].result;
+            /* AFP replay cache end */
+            } else {
+                /* send off an afp command. in a couple cases, we take advantage
+                 * of the fact that we're a stream-based protocol. */
+                if (afp_switch[function]) {
+                    dsi->datalen = DSI_DATASIZ;
+                    child.flags |= CHILD_RUNNING;
 
-                err = (*afp_switch[function])(obj,
-                                              (char *)&dsi->commands, dsi->cmdlen,
-                                              (char *)&dsi->data, &dsi->datalen);
+                    LOG(log_debug, logtype_afpd, "<== Start AFP command: %s", AfpNum2name(function));
+
+                    err = (*afp_switch[function])(obj,
+                                                  (char *)&dsi->commands, dsi->cmdlen,
+                                                  (char *)&dsi->data, &dsi->datalen);
+
+                    LOG(log_debug, logtype_afpd, "==> Finished AFP command: %s -> %s",
+                        AfpNum2name(function), AfpErr2name(err));
+
+                    dir_free_invalid_q();
 
-                LOG(log_debug, logtype_afpd, "==> Finished AFP command: %s -> %s",
-                    AfpNum2name(function), AfpErr2name(err));
 #ifdef FORCE_UIDGID
-               /* bring everything back to old euid, egid */
-                if (obj->force_uid)
-                   restore_uidgid ( &obj->uidgid );
+                    /* bring everything back to old euid, egid */
+                    if (obj->force_uid)
+                        restore_uidgid ( &obj->uidgid );
 #endif /* FORCE_UIDGID */
-                child.flags &= ~CHILD_RUNNING;
-            } else {
-                LOG(log_error, logtype_afpd, "bad function %X", function);
-                dsi->datalen = 0;
-                err = AFPERR_NOOP;
+                    child.flags &= ~CHILD_RUNNING;
+
+                    /* Add result to the AFP replay cache */
+                    replaycache[rc_idx].DSIreqID = dsi->clientID;
+                    replaycache[rc_idx].AFPcommand = function;
+                    replaycache[rc_idx].result = err;
+                } else {
+                    LOG(log_error, logtype_afpd, "bad function %X", function);
+                    dsi->datalen = 0;
+                    err = AFPERR_NOOP;
+                }
             }
 
             /* single shot toggle that gets set by dsi_readinit. */
@@ -460,9 +583,16 @@ void afp_over_dsi(AFPObj *obj)
             if ( afp_switch[ function ] != NULL ) {
                 dsi->datalen = DSI_DATASIZ;
                 child.flags |= CHILD_RUNNING;
+
+                LOG(log_debug, logtype_afpd, "<== Start AFP command: %s", AfpNum2name(function));
+
                 err = (*afp_switch[function])(obj,
                                               (char *)&dsi->commands, dsi->cmdlen,
                                               (char *)&dsi->data, &dsi->datalen);
+
+                LOG(log_debug, logtype_afpd, "==> Finished AFP command: %s -> %s",
+                    AfpNum2name(function), AfpErr2name(err));
+
                 child.flags &= ~CHILD_RUNNING;
 #ifdef FORCE_UIDGID
                /* bring everything back to old euid, egid */