r23784: use the GPLv3 boilerplate as recommended by the FSF and the license text
[samba.git] / source3 / passdb / login_cache.c
index 5acec64573bec51c20ccf22cbc2d9d7a9129a1ae..5dce09a0dacc8d29db1950c5688cd61c9b3b6866 100644 (file)
@@ -1,11 +1,11 @@
 /* 
    Unix SMB/CIFS implementation.
-   SAM_ACCOUNT local cache for 
+   struct samu local cache for 
    Copyright (C) Jim McDonough (jmcd@us.ibm.com) 2004.
       
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 2 of the License, or
+   the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
@@ -14,8 +14,7 @@
    GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 #include "includes.h"
@@ -64,27 +63,31 @@ BOOL login_cache_shutdown(void)
 }
 
 /* if we can't read the cache, oh well, no need to return anything */
-LOGIN_CACHE * login_cache_read(SAM_ACCOUNT *sampass)
+LOGIN_CACHE * login_cache_read(struct samu *sampass)
 {
-       TDB_DATA keybuf, databuf;
+       char *keystr;
+       TDB_DATA databuf;
        LOGIN_CACHE *entry;
 
        if (!login_cache_init())
                return NULL;
 
-       keybuf.dptr = strdup(pdb_get_nt_username(sampass));
-       if (!keybuf.dptr || !strlen(keybuf.dptr)) {
-               SAFE_FREE(keybuf.dptr);
+       if (pdb_get_nt_username(sampass) == NULL) {
+               return NULL;
+       }
+
+       keystr = SMB_STRDUP(pdb_get_nt_username(sampass));
+       if (!keystr || !keystr[0]) {
+               SAFE_FREE(keystr);
                return NULL;
        }
-       keybuf.dsize = strlen(keybuf.dptr) + 1;
 
        DEBUG(7, ("Looking up login cache for user %s\n",
-                 keybuf.dptr));
-       databuf = tdb_fetch(cache, keybuf);
-       SAFE_FREE(keybuf.dptr);
+                 keystr));
+       databuf = tdb_fetch_bystring(cache, keystr);
+       SAFE_FREE(keystr);
 
-       if (!(entry = malloc(sizeof(LOGIN_CACHE)))) {
+       if (!(entry = SMB_MALLOC_P(LOGIN_CACHE))) {
                DEBUG(1, ("Unable to allocate cache entry buffer!\n"));
                SAFE_FREE(databuf.dptr);
                return NULL;
@@ -95,29 +98,37 @@ LOGIN_CACHE * login_cache_read(SAM_ACCOUNT *sampass)
                        &entry->bad_password_count, 
                        &entry->bad_password_time) == -1) {
                DEBUG(7, ("No cache entry found\n"));
+               SAFE_FREE(entry);
                SAFE_FREE(databuf.dptr);
                return NULL;
        }
 
+       SAFE_FREE(databuf.dptr);
+
        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;
 }
 
-BOOL login_cache_write(const SAM_ACCOUNT *sampass, LOGIN_CACHE entry)
+BOOL login_cache_write(const struct samu *sampass, LOGIN_CACHE entry)
 {
-
-       TDB_DATA keybuf, databuf;
+       char *keystr;
+       TDB_DATA databuf;
        BOOL ret;
-       
 
-       keybuf.dptr = strdup(pdb_get_nt_username(sampass));
-       if (!keybuf.dptr || !strlen(keybuf.dptr)) {
-               SAFE_FREE(keybuf.dptr);
+       if (!login_cache_init())
+               return False;
+
+       if (pdb_get_nt_username(sampass) == NULL) {
+               return False;
+       }
+
+       keystr = SMB_STRDUP(pdb_get_nt_username(sampass));
+       if (!keystr || !keystr[0]) {
+               SAFE_FREE(keystr);
                return False;
        }
-       keybuf.dsize = strlen(keybuf.dptr) + 1;
 
        entry.entry_timestamp = time(NULL);
 
@@ -127,9 +138,9 @@ BOOL login_cache_write(const SAM_ACCOUNT *sampass, LOGIN_CACHE entry)
                         entry.acct_ctrl,
                         entry.bad_password_count,
                         entry.bad_password_time);
-       databuf.dptr = malloc(databuf.dsize);
+       databuf.dptr = SMB_MALLOC_ARRAY(uint8, databuf.dsize);
        if (!databuf.dptr) {
-               SAFE_FREE(keybuf.dptr);
+               SAFE_FREE(keystr);
                return False;
        }
                         
@@ -139,36 +150,39 @@ BOOL login_cache_write(const SAM_ACCOUNT *sampass, LOGIN_CACHE entry)
                         entry.bad_password_count,
                         entry.bad_password_time)
            != databuf.dsize) {
-               SAFE_FREE(keybuf.dptr);
+               SAFE_FREE(keystr);
                SAFE_FREE(databuf.dptr);
                return False;
        }
 
-       ret = tdb_store(cache, keybuf, databuf, 0);
-       SAFE_FREE(keybuf.dptr);
+       ret = tdb_store_bystring(cache, keystr, databuf, 0);
+       SAFE_FREE(keystr);
        SAFE_FREE(databuf.dptr);
        return ret == 0;
 }
 
-BOOL login_cache_delentry(const SAM_ACCOUNT *sampass)
+BOOL login_cache_delentry(const struct samu *sampass)
 {
        int ret;
-       TDB_DATA keybuf;
+       char *keystr;
        
        if (!login_cache_init()) 
                return False;   
 
-       keybuf.dptr = strdup(pdb_get_nt_username(sampass));
-       if (!keybuf.dptr || !strlen(keybuf.dptr)) {
-               SAFE_FREE(keybuf.dptr);
+       if (pdb_get_nt_username(sampass) == NULL) {
                return False;
        }
-       keybuf.dsize = strlen(keybuf.dptr) + 1;
-       DEBUG(9, ("About to delete entry for %s\n", keybuf.dptr));
-       ret = tdb_delete(cache, keybuf);
+
+       keystr = SMB_STRDUP(pdb_get_nt_username(sampass));
+       if (!keystr || !keystr[0]) {
+               SAFE_FREE(keystr);
+               return False;
+       }
+
+       DEBUG(9, ("About to delete entry for %s\n", keystr));
+       ret = tdb_delete_bystring(cache, keystr);
        DEBUG(9, ("tdb_delete returned %d\n", ret));
        
-       SAFE_FREE(keybuf.dptr);
+       SAFE_FREE(keystr);
        return ret == 0;
 }
-