s3: Make login_cache_read take a pointer, avoid a malloc
authorVolker Lendecke <vl@samba.org>
Tue, 16 Mar 2010 21:18:52 +0000 (22:18 +0100)
committerVolker Lendecke <vl@samba.org>
Tue, 16 Mar 2010 21:35:41 +0000 (22:35 +0100)
source3/include/proto.h
source3/passdb/login_cache.c
source3/passdb/pdb_ldap.c

index f2ba8e3b24a6aa1a064f754715b4b9e0f87fdcd6..68a3e5cf7b7b867d67d9638bca5ff6cfffc0c763 100644 (file)
@@ -4409,7 +4409,7 @@ char* get_string_param( const char* param );
 
 bool login_cache_init(void);
 bool login_cache_shutdown(void);
-struct login_cache * login_cache_read(struct samu *sampass);
+bool login_cache_read(struct samu *sampass, struct login_cache *entry);
 bool login_cache_write(const struct samu *sampass, struct login_cache entry);
 bool login_cache_delentry(const struct samu *sampass);
 
index 6ac6d52a63810d6748894116367d00226f087a33..2f9f000f11108e2507ff49eef24608dbf9d09c9f 100644 (file)
@@ -63,24 +63,24 @@ bool login_cache_shutdown(void)
 }
 
 /* if we can't read the cache, oh well, no need to return anything */
-struct login_cache * login_cache_read(struct samu *sampass)
+bool login_cache_read(struct samu *sampass, struct login_cache *entry)
 {
        char *keystr;
        TDB_DATA databuf;
-       struct login_cache *entry;
        uint32_t entry_timestamp = 0, bad_password_time = 0;
 
-       if (!login_cache_init())
-               return NULL;
+       if (!login_cache_init()) {
+               return false;
+       }
 
        if (pdb_get_nt_username(sampass) == NULL) {
-               return NULL;
+               return false;
        }
 
        keystr = SMB_STRDUP(pdb_get_nt_username(sampass));
        if (!keystr || !keystr[0]) {
                SAFE_FREE(keystr);
-               return NULL;
+               return false;
        }
 
        DEBUG(7, ("Looking up login cache for user %s\n",
@@ -88,12 +88,6 @@ struct login_cache * login_cache_read(struct samu *sampass)
        databuf = tdb_fetch_bystring(cache, keystr);
        SAFE_FREE(keystr);
 
-       entry = SMB_MALLOC_P(struct login_cache);
-       if (entry == NULL) {
-               DEBUG(1, ("Unable to allocate cache entry buffer!\n"));
-               SAFE_FREE(databuf.dptr);
-               return NULL;
-       }
        ZERO_STRUCTP(entry);
 
        if (tdb_unpack (databuf.dptr, databuf.dsize, SAM_CACHE_FORMAT,
@@ -102,9 +96,8 @@ struct login_cache * login_cache_read(struct samu *sampass)
                        &entry->bad_password_count,
                        &bad_password_time) == -1) {
                DEBUG(7, ("No cache entry found\n"));
-               SAFE_FREE(entry);
                SAFE_FREE(databuf.dptr);
-               return NULL;
+               return false;
        }
 
        /* Deal with possible 64-bit time_t. */
@@ -116,7 +109,7 @@ struct login_cache * login_cache_read(struct samu *sampass)
        DEBUG(5, ("Found login cache entry: timestamp %12u, flags 0x%x, count %d, time %12u\n",
                  (unsigned int)entry->entry_timestamp, entry->acct_ctrl, 
                  entry->bad_password_count, (unsigned int)entry->bad_password_time));
-       return entry;
+       return true;
 }
 
 bool login_cache_write(const struct samu *sampass, struct login_cache entry)
index 1e3413e3817101ca9cb55afee793f3ac722783cc..b3eb37501c04a99197206e318e93a332bce24c2e 100644 (file)
@@ -540,7 +540,7 @@ static bool init_sam_from_ldap(struct ldapsam_privates *ldap_state,
        uint32 hours_len;
        uint8           hours[MAX_HOURS_LEN];
        char *temp = NULL;
-       struct login_cache *cache_entry = NULL;
+       struct login_cache cache_entry;
        uint32          pwHistLen;
        bool expand_explicit = lp_passdb_expand_explicit();
        bool ret = false;
@@ -1120,7 +1120,7 @@ static bool init_sam_from_ldap(struct ldapsam_privates *ldap_state,
        }
 
        /* see if we have newer updates */
-       if (!(cache_entry = login_cache_read(sampass))) {
+       if (!login_cache_read(sampass, &cache_entry)) {
                DEBUG (9, ("No cache entry, bad count = %u, bad time = %u\n",
                           (unsigned int)pdb_get_bad_password_count(sampass),
                           (unsigned int)pdb_get_bad_password_time(sampass)));
@@ -1130,10 +1130,10 @@ static bool init_sam_from_ldap(struct ldapsam_privates *ldap_state,
 
        DEBUG(7, ("ldap time is %u, cache time is %u, bad time = %u\n",
                  (unsigned int)ldap_entry_time,
-                 (unsigned int)cache_entry->entry_timestamp,
-                 (unsigned int)cache_entry->bad_password_time));
+                 (unsigned int)cache_entry.entry_timestamp,
+                 (unsigned int)cache_entry.bad_password_time));
 
-       if (ldap_entry_time > cache_entry->entry_timestamp) {
+       if (ldap_entry_time > cache_entry.entry_timestamp) {
                /* cache is older than directory , so
                   we need to delete the entry but allow the
                   fields to be written out */
@@ -1142,13 +1142,13 @@ static bool init_sam_from_ldap(struct ldapsam_privates *ldap_state,
                /* read cache in */
                pdb_set_acct_ctrl(sampass,
                                  pdb_get_acct_ctrl(sampass) |
-                                 (cache_entry->acct_ctrl & ACB_AUTOLOCK),
+                                 (cache_entry.acct_ctrl & ACB_AUTOLOCK),
                                  PDB_SET);
                pdb_set_bad_password_count(sampass,
-                                          cache_entry->bad_password_count,
+                                          cache_entry.bad_password_count,
                                           PDB_SET);
                pdb_set_bad_password_time(sampass,
-                                         cache_entry->bad_password_time,
+                                         cache_entry.bad_password_time,
                                          PDB_SET);
        }
 
@@ -1157,7 +1157,6 @@ static bool init_sam_from_ldap(struct ldapsam_privates *ldap_state,
   fn_exit:
 
        TALLOC_FREE(ctx);
-       SAFE_FREE(cache_entry);
        return ret;
 }