src/server/daemon/auth.c

changeset 67
50505dc3f8a6
parent 66
74babc0082b7
child 77
f1cff81e425a
--- a/src/server/daemon/auth.c	Sun May 26 12:12:07 2013 +0200
+++ b/src/server/daemon/auth.c	Sun May 26 22:05:41 2013 +0200
@@ -70,6 +70,7 @@
     time_t now = time(NULL);
     size_t slot = mapkey.hash%cache.size;
     
+    User *u = NULL;
     pthread_mutex_lock(&auth_cache_mutex);
     
     UserCacheElm *elm = cache.map[slot];
@@ -80,44 +81,36 @@
     if(elm) {
         // compare the key data to be sure it is the correct user
         int n = (mapkey.len > elm->key.len) ? elm->key.len : mapkey.len;
-        if (memcmp(elm->key.data, mapkey.data, n)) {
-            free(key);
-            pthread_mutex_unlock(&auth_cache_mutex);
-            return NULL;
+        if (!memcmp(elm->key.data, mapkey.data, n)) {
+            // elm is now the correct UserCacheElm
+            // TODO: use configuration for expire time
+            if(now - elm->created > 120) {
+                // cached user expired
+                // remove all users from the list from the first to this one
+                UserCacheElm *e = cache.head;
+                while(e) {
+                    if(e == elm) {
+                        break;
+                    }
+                    UserCacheElm *n = e->next_user;
+                    auth_cache_remove_from_map(e);
+                    e = n;
+                }
+                cache.head = elm->next_user;
+                if(cache.trail == elm) {
+                    cache.trail = NULL;
+                }
+                auth_cache_remove_from_map(elm);
+                u = NULL;
+            } else {
+                u = (User*)elm->user;
+            }
         }
-    } else {
-        free(key);
-        pthread_mutex_unlock(&auth_cache_mutex);
-        return NULL;
-    }
-    
-    // elm is now the correct UserCacheElm
-    // TODO: use configuration for expire time
-    if(now - elm->created > 120) {
-        // cached user expired
-        // remove all users from the list from the first to this one
-        UserCacheElm *e = cache.head;
-        while(e) {
-            if(e == elm) {
-                break;
-            }
-            UserCacheElm *n = e->next_user;
-            auth_cache_remove_from_map(e);
-            e = n;
-        }
-        cache.head = elm->next_user;
-        if(cache.trail == elm) {
-            cache.trail = NULL;
-        }
-        auth_cache_remove_from_map(elm);
-        free(key);
-        pthread_mutex_unlock(&auth_cache_mutex);
-        return NULL;
     }
     
     pthread_mutex_unlock(&auth_cache_mutex);
     free(key);
-    return (User*)elm->user;
+    return u;
 }
 
 void auth_cache_add(

mercurial