root/source3/passdb/login_cache.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. login_cache_init
  2. login_cache_shutdown
  3. login_cache_read
  4. login_cache_write
  5. login_cache_delentry

   1 /* 
   2    Unix SMB/CIFS implementation.
   3    struct samu local cache for 
   4    Copyright (C) Jim McDonough (jmcd@us.ibm.com) 2004.
   5       
   6    This program is free software; you can redistribute it and/or modify
   7    it under the terms of the GNU General Public License as published by
   8    the Free Software Foundation; either version 3 of the License, or
   9    (at your option) any later version.
  10    
  11    This program is distributed in the hope that it will be useful,
  12    but WITHOUT ANY WARRANTY; without even the implied warranty of
  13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14    GNU General Public License for more details.
  15    
  16    You should have received a copy of the GNU General Public License
  17    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18 */
  19 
  20 #include "includes.h"
  21 
  22 #undef DBGC_CLASS
  23 #define DBGC_CLASS DBGC_PASSDB
  24 
  25 #define LOGIN_CACHE_FILE "login_cache.tdb"
  26 
  27 #define SAM_CACHE_FORMAT "dwwd"
  28 
  29 static TDB_CONTEXT *cache;
  30 
  31 bool login_cache_init(void)
     /* [<][>][^][v][top][bottom][index][help] */
  32 {
  33         char* cache_fname = NULL;
  34         
  35         /* skip file open if it's already opened */
  36         if (cache) return True;
  37 
  38         cache_fname = cache_path(LOGIN_CACHE_FILE);
  39         if (cache_fname == NULL) {
  40                 DEBUG(0, ("Filename allocation failed.\n"));
  41                 return False;
  42         }
  43 
  44         DEBUG(5, ("Opening cache file at %s\n", cache_fname));
  45 
  46         cache = tdb_open_log(cache_fname, 0, TDB_DEFAULT,
  47                              O_RDWR|O_CREAT, 0644);
  48 
  49         if (!cache)
  50                 DEBUG(5, ("Attempt to open %s failed.\n", cache_fname));
  51 
  52         TALLOC_FREE(cache_fname);
  53 
  54         return (cache ? True : False);
  55 }
  56 
  57 bool login_cache_shutdown(void)
     /* [<][>][^][v][top][bottom][index][help] */
  58 {
  59         /* tdb_close routine returns -1 on error */
  60         if (!cache) return False;
  61         DEBUG(5, ("Closing cache file\n"));
  62         return tdb_close(cache) != -1;
  63 }
  64 
  65 /* if we can't read the cache, oh well, no need to return anything */
  66 LOGIN_CACHE * login_cache_read(struct samu *sampass)
     /* [<][>][^][v][top][bottom][index][help] */
  67 {
  68         char *keystr;
  69         TDB_DATA databuf;
  70         LOGIN_CACHE *entry;
  71 
  72         if (!login_cache_init())
  73                 return NULL;
  74 
  75         if (pdb_get_nt_username(sampass) == NULL) {
  76                 return NULL;
  77         }
  78 
  79         keystr = SMB_STRDUP(pdb_get_nt_username(sampass));
  80         if (!keystr || !keystr[0]) {
  81                 SAFE_FREE(keystr);
  82                 return NULL;
  83         }
  84 
  85         DEBUG(7, ("Looking up login cache for user %s\n",
  86                   keystr));
  87         databuf = tdb_fetch_bystring(cache, keystr);
  88         SAFE_FREE(keystr);
  89 
  90         if (!(entry = SMB_MALLOC_P(LOGIN_CACHE))) {
  91                 DEBUG(1, ("Unable to allocate cache entry buffer!\n"));
  92                 SAFE_FREE(databuf.dptr);
  93                 return NULL;
  94         }
  95 
  96         if (tdb_unpack (databuf.dptr, databuf.dsize, SAM_CACHE_FORMAT,
  97                         &entry->entry_timestamp, &entry->acct_ctrl, 
  98                         &entry->bad_password_count, 
  99                         &entry->bad_password_time) == -1) {
 100                 DEBUG(7, ("No cache entry found\n"));
 101                 SAFE_FREE(entry);
 102                 SAFE_FREE(databuf.dptr);
 103                 return NULL;
 104         }
 105 
 106         SAFE_FREE(databuf.dptr);
 107 
 108         DEBUG(5, ("Found login cache entry: timestamp %12u, flags 0x%x, count %d, time %12u\n",
 109                   (unsigned int)entry->entry_timestamp, entry->acct_ctrl, 
 110                   entry->bad_password_count, (unsigned int)entry->bad_password_time));
 111         return entry;
 112 }
 113 
 114 bool login_cache_write(const struct samu *sampass, LOGIN_CACHE entry)
     /* [<][>][^][v][top][bottom][index][help] */
 115 {
 116         char *keystr;
 117         TDB_DATA databuf;
 118         bool ret;
 119 
 120         if (!login_cache_init())
 121                 return False;
 122 
 123         if (pdb_get_nt_username(sampass) == NULL) {
 124                 return False;
 125         }
 126 
 127         keystr = SMB_STRDUP(pdb_get_nt_username(sampass));
 128         if (!keystr || !keystr[0]) {
 129                 SAFE_FREE(keystr);
 130                 return False;
 131         }
 132 
 133         entry.entry_timestamp = time(NULL);
 134 
 135         databuf.dsize = 
 136                 tdb_pack(NULL, 0, SAM_CACHE_FORMAT,
 137                          entry.entry_timestamp,
 138                          entry.acct_ctrl,
 139                          entry.bad_password_count,
 140                          entry.bad_password_time);
 141         databuf.dptr = SMB_MALLOC_ARRAY(uint8, databuf.dsize);
 142         if (!databuf.dptr) {
 143                 SAFE_FREE(keystr);
 144                 return False;
 145         }
 146                          
 147         if (tdb_pack(databuf.dptr, databuf.dsize, SAM_CACHE_FORMAT,
 148                          entry.entry_timestamp,
 149                          entry.acct_ctrl,
 150                          entry.bad_password_count,
 151                          entry.bad_password_time)
 152             != databuf.dsize) {
 153                 SAFE_FREE(keystr);
 154                 SAFE_FREE(databuf.dptr);
 155                 return False;
 156         }
 157 
 158         ret = tdb_store_bystring(cache, keystr, databuf, 0);
 159         SAFE_FREE(keystr);
 160         SAFE_FREE(databuf.dptr);
 161         return ret == 0;
 162 }
 163 
 164 bool login_cache_delentry(const struct samu *sampass)
     /* [<][>][^][v][top][bottom][index][help] */
 165 {
 166         int ret;
 167         char *keystr;
 168         
 169         if (!login_cache_init()) 
 170                 return False;   
 171 
 172         if (pdb_get_nt_username(sampass) == NULL) {
 173                 return False;
 174         }
 175 
 176         keystr = SMB_STRDUP(pdb_get_nt_username(sampass));
 177         if (!keystr || !keystr[0]) {
 178                 SAFE_FREE(keystr);
 179                 return False;
 180         }
 181 
 182         DEBUG(9, ("About to delete entry for %s\n", keystr));
 183         ret = tdb_delete_bystring(cache, keystr);
 184         DEBUG(9, ("tdb_delete returned %d\n", ret));
 185         
 186         SAFE_FREE(keystr);
 187         return ret == 0;
 188 }

/* [<][>][^][v][top][bottom][index][help] */