s3: Cache the username map in gencache
authorVolker Lendecke <vl@samba.org>
Fri, 9 Apr 2010 15:19:13 +0000 (17:19 +0200)
committerVolker Lendecke <vl@samba.org>
Tue, 13 Apr 2010 06:57:28 +0000 (08:57 +0200)
This is for uses with a heavy-weight username map script

source3/include/proto.h
source3/param/loadparm.c
source3/smbd/map_username.c

index 1802558fbcfcfe9fdb72c82b3d48cac55060b23d..0ec3cc082c0ab81117d5fd634990aba49a959c0b 100644 (file)
@@ -3976,6 +3976,7 @@ char *lp_addmachine_script(void);
 char *lp_shutdown_script(void);
 char *lp_abort_shutdown_script(void);
 char *lp_username_map_script(void);
+int lp_username_map_cache_time(void);
 char *lp_check_password_script(void);
 char *lp_wins_hook(void);
 const char *lp_template_homedir(void);
index 5eadcaf3c52307e8ced0f427b10871a913bafede..92b4967f22e6152d7e6ebf076d10f1402cd3beb3 100644 (file)
@@ -177,6 +177,7 @@ struct global {
        char *szShutdownScript;
        char *szAbortShutdownScript;
        char *szUsernameMapScript;
+       int iUsernameMapCacheTime;
        char *szCheckPasswordScript;
        char *szWINSHook;
        char *szUtmpDir;
@@ -3226,6 +3227,15 @@ static struct parm_struct parm_table[] = {
                .enum_list      = NULL,
                .flags          = FLAG_ADVANCED,
        },
+       {
+               .label          = "username map cache time",
+               .type           = P_INTEGER,
+               .p_class        = P_GLOBAL,
+               .ptr            = &Globals.iUsernameMapCacheTime,
+               .special        = NULL,
+               .enum_list      = NULL,
+               .flags          = FLAG_ADVANCED,
+       },
        {
                .label          = "logon script",
                .type           = P_STRING,
@@ -5398,6 +5408,7 @@ FN_GLOBAL_STRING(lp_addmachine_script, &Globals.szAddMachineScript)
 FN_GLOBAL_STRING(lp_shutdown_script, &Globals.szShutdownScript)
 FN_GLOBAL_STRING(lp_abort_shutdown_script, &Globals.szAbortShutdownScript)
 FN_GLOBAL_STRING(lp_username_map_script, &Globals.szUsernameMapScript)
+FN_GLOBAL_INTEGER(lp_username_map_cache_time, &Globals.iUsernameMapCacheTime)
 
 FN_GLOBAL_STRING(lp_check_password_script, &Globals.szCheckPasswordScript)
 
index 0165bef40268b2cec386342720048fbeae3ba1b5..9b24d8fa4ed907d4cb58b3c7ddbd9b434e1a18f2 100644 (file)
@@ -76,6 +76,48 @@ static char *skip_space(char *s)
        return s;
 }
 
+static bool fetch_map_from_gencache(fstring user)
+{
+       char *key, *value;
+       bool found;
+
+       if (lp_username_map_cache_time() == 0) {
+               return false;
+       }
+
+       key = talloc_asprintf_strupper_m(talloc_tos(), "USERNAME_MAP/%s",
+                                        user);
+       if (key == NULL) {
+               return false;
+       }
+       found = gencache_get(key, &value, NULL);
+       TALLOC_FREE(key);
+       if (!found) {
+               return false;
+       }
+       fstrcpy(user, value);
+       SAFE_FREE(value);
+       return true;
+}
+
+static void store_map_in_gencache(const char *from, const char *to)
+{
+       char *key;
+       int cache_time = lp_username_map_cache_time();
+
+       if (cache_time == 0) {
+               return;
+       }
+
+       key = talloc_asprintf_strupper_m(talloc_tos(), "USERNAME_MAP/%s",
+                                        from);
+        if (key == NULL) {
+                return;
+        }
+       gencache_set(key, to, cache_time + time(NULL));
+       TALLOC_FREE(key);
+}
+
 bool map_username(fstring user)
 {
        XFILE *f;
@@ -97,6 +139,10 @@ bool map_username(fstring user)
                return true;
        }
 
+       if (fetch_map_from_gencache(user)) {
+               return true;
+       }
+
        /* first try the username map script */
 
        if ( *cmd ) {
@@ -134,6 +180,7 @@ bool map_username(fstring user)
                if (numlines && qlines) {
                        DEBUG(3,("Mapped user %s to %s\n", user, qlines[0] ));
                        set_last_from_to(user, qlines[0]);
+                       store_map_in_gencache(user, qlines[0]);
                        fstrcpy( user, qlines[0] );
                }
 
@@ -197,6 +244,7 @@ bool map_username(fstring user)
                        mapped_user = True;
 
                        set_last_from_to(user, unixname);
+                       store_map_in_gencache(user, unixname);
                        fstrcpy( user, unixname );
 
                        if ( return_if_mapped ) {
@@ -217,6 +265,7 @@ bool map_username(fstring user)
         */
 
        set_last_from_to(user, user);
+       store_map_in_gencache(user, user);
 
        return mapped_user;
 }