LCOV - code coverage report
Current view: top level - source3/passdb - login_cache.c (source / functions) Hit Total Coverage
Test: coverage report for master 98b443d9 Lines: 0 94 0.0 %
Date: 2024-05-31 13:13:24 Functions: 0 5 0.0 %

          Line data    Source code
       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             : #include "system/filesys.h"
      22             : #include "passdb.h"
      23             : #include "util_tdb.h"
      24             : 
      25             : #undef DBGC_CLASS
      26             : #define DBGC_CLASS DBGC_PASSDB
      27             : 
      28             : #define LOGIN_CACHE_FILE "login_cache.tdb"
      29             : 
      30             : #define SAM_CACHE_FORMAT "dwwd"
      31             : 
      32             : static TDB_CONTEXT *cache;
      33             : 
      34           0 : bool login_cache_init(void)
      35             : {
      36           0 :         char* cache_fname = NULL;
      37             : 
      38             :         /* skip file open if it's already opened */
      39           0 :         if (cache) return True;
      40             : 
      41           0 :         cache_fname = cache_path(talloc_tos(), LOGIN_CACHE_FILE);
      42           0 :         if (cache_fname == NULL) {
      43           0 :                 DEBUG(0, ("Filename allocation failed.\n"));
      44           0 :                 return False;
      45             :         }
      46             : 
      47           0 :         DEBUG(5, ("Opening cache file at %s\n", cache_fname));
      48             : 
      49           0 :         cache = tdb_open_log(cache_fname, 0, TDB_DEFAULT,
      50             :                              O_RDWR|O_CREAT, 0644);
      51             : 
      52           0 :         if (!cache)
      53           0 :                 DEBUG(5, ("Attempt to open %s failed.\n", cache_fname));
      54             : 
      55           0 :         TALLOC_FREE(cache_fname);
      56             : 
      57           0 :         return (cache ? True : False);
      58             : }
      59             : 
      60           0 : bool login_cache_shutdown(void)
      61             : {
      62             :         /* tdb_close routine returns non-zero on error */
      63           0 :         if (!cache) return False;
      64           0 :         DEBUG(5, ("Closing cache file\n"));
      65           0 :         return tdb_close(cache) == 0;
      66             : }
      67             : 
      68             : /* if we can't read the cache, oh well, no need to return anything */
      69           0 : bool login_cache_read(struct samu *sampass, struct login_cache *entry)
      70             : {
      71           0 :         char *keystr;
      72           0 :         TDB_DATA databuf;
      73           0 :         uint32_t entry_timestamp = 0, bad_password_time = 0;
      74           0 :         uint16_t acct_ctrl;
      75             : 
      76           0 :         if (!login_cache_init()) {
      77           0 :                 return false;
      78             :         }
      79             : 
      80           0 :         if (pdb_get_nt_username(sampass) == NULL) {
      81           0 :                 return false;
      82             :         }
      83             : 
      84           0 :         keystr = SMB_STRDUP(pdb_get_nt_username(sampass));
      85           0 :         if (!keystr || !keystr[0]) {
      86           0 :                 SAFE_FREE(keystr);
      87           0 :                 return false;
      88             :         }
      89             : 
      90           0 :         DEBUG(7, ("Looking up login cache for user %s\n",
      91             :                   keystr));
      92           0 :         databuf = tdb_fetch_bystring(cache, keystr);
      93           0 :         SAFE_FREE(keystr);
      94             : 
      95           0 :         ZERO_STRUCTP(entry);
      96             : 
      97           0 :         if (tdb_unpack (databuf.dptr, databuf.dsize, SAM_CACHE_FORMAT,
      98             :                         &entry_timestamp,
      99             :                         &acct_ctrl,
     100             :                         &entry->bad_password_count,
     101             :                         &bad_password_time) == -1) {
     102           0 :                 DEBUG(7, ("No cache entry found\n"));
     103           0 :                 SAFE_FREE(databuf.dptr);
     104           0 :                 return false;
     105             :         }
     106             : 
     107             :         /*
     108             :          * Deal with 32-bit acct_ctrl. In the tdb we only store 16-bit
     109             :          * ("w" in SAM_CACHE_FORMAT). Fixes bug 7253.
     110             :          */
     111           0 :         entry->acct_ctrl = acct_ctrl;
     112             : 
     113             :         /* Deal with possible 64-bit time_t. */
     114           0 :         entry->entry_timestamp = (time_t)entry_timestamp;
     115           0 :         entry->bad_password_time = (time_t)bad_password_time;
     116             : 
     117           0 :         SAFE_FREE(databuf.dptr);
     118             : 
     119           0 :         DEBUG(5, ("Found login cache entry: timestamp %12u, flags 0x%x, count %d, time %12u\n",
     120             :                   (unsigned int)entry->entry_timestamp, entry->acct_ctrl, 
     121             :                   entry->bad_password_count, (unsigned int)entry->bad_password_time));
     122           0 :         return true;
     123             : }
     124             : 
     125           0 : bool login_cache_write(const struct samu *sampass,
     126             :                        const struct login_cache *entry)
     127             : {
     128           0 :         char *keystr;
     129           0 :         TDB_DATA databuf;
     130           0 :         bool ret;
     131           0 :         uint32_t entry_timestamp;
     132           0 :         uint32_t bad_password_time = entry->bad_password_time;
     133             : 
     134           0 :         if (!login_cache_init())
     135           0 :                 return False;
     136             : 
     137           0 :         if (pdb_get_nt_username(sampass) == NULL) {
     138           0 :                 return False;
     139             :         }
     140             : 
     141           0 :         keystr = SMB_STRDUP(pdb_get_nt_username(sampass));
     142           0 :         if (!keystr || !keystr[0]) {
     143           0 :                 SAFE_FREE(keystr);
     144           0 :                 return False;
     145             :         }
     146             : 
     147           0 :         entry_timestamp = (uint32_t)time(NULL);
     148             : 
     149           0 :         databuf.dsize = 
     150           0 :                 tdb_pack(NULL, 0, SAM_CACHE_FORMAT,
     151             :                          entry_timestamp,
     152           0 :                          entry->acct_ctrl,
     153           0 :                          entry->bad_password_count,
     154             :                          bad_password_time);
     155           0 :         databuf.dptr = SMB_MALLOC_ARRAY(uint8_t, databuf.dsize);
     156           0 :         if (!databuf.dptr) {
     157           0 :                 SAFE_FREE(keystr);
     158           0 :                 return False;
     159             :         }
     160             : 
     161           0 :         if (tdb_pack(databuf.dptr, databuf.dsize, SAM_CACHE_FORMAT,
     162             :                          entry_timestamp,
     163           0 :                          entry->acct_ctrl,
     164           0 :                          entry->bad_password_count,
     165             :                          bad_password_time)
     166           0 :             != databuf.dsize) {
     167           0 :                 SAFE_FREE(keystr);
     168           0 :                 SAFE_FREE(databuf.dptr);
     169           0 :                 return False;
     170             :         }
     171             : 
     172           0 :         ret = tdb_store_bystring(cache, keystr, databuf, 0);
     173           0 :         SAFE_FREE(keystr);
     174           0 :         SAFE_FREE(databuf.dptr);
     175           0 :         return ret == 0;
     176             : }
     177             : 
     178           0 : bool login_cache_delentry(const struct samu *sampass)
     179             : {
     180           0 :         int ret;
     181           0 :         char *keystr;
     182             : 
     183           0 :         if (!login_cache_init()) 
     184           0 :                 return False;   
     185             : 
     186           0 :         if (pdb_get_nt_username(sampass) == NULL) {
     187           0 :                 return False;
     188             :         }
     189             : 
     190           0 :         keystr = SMB_STRDUP(pdb_get_nt_username(sampass));
     191           0 :         if (!keystr || !keystr[0]) {
     192           0 :                 SAFE_FREE(keystr);
     193           0 :                 return False;
     194             :         }
     195             : 
     196           0 :         DEBUG(9, ("About to delete entry for %s\n", keystr));
     197           0 :         ret = tdb_delete_bystring(cache, keystr);
     198           0 :         DEBUG(9, ("tdb_delete returned %d\n", ret));
     199             : 
     200           0 :         SAFE_FREE(keystr);
     201           0 :         return ret == 0;
     202             : }

Generated by: LCOV version 1.14