2 Unix SMB/CIFS implementation.
3 struct samu local cache for
4 Copyright (C) Jim McDonough (jmcd@us.ibm.com) 2004.
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.
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.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #define DBGC_CLASS DBGC_PASSDB
26 #define LOGIN_CACHE_FILE "login_cache.tdb"
28 #define SAM_CACHE_FORMAT "dwwd"
30 static TDB_CONTEXT *cache;
32 BOOL login_cache_init(void)
34 char* cache_fname = NULL;
36 /* skip file open if it's already opened */
37 if (cache) return True;
39 asprintf(&cache_fname, "%s/%s", lp_lockdir(), LOGIN_CACHE_FILE);
41 DEBUG(5, ("Opening cache file at %s\n", cache_fname));
43 DEBUG(0, ("Filename allocation failed.\n"));
47 cache = tdb_open_log(cache_fname, 0, TDB_DEFAULT,
48 O_RDWR|O_CREAT, 0644);
51 DEBUG(5, ("Attempt to open %s failed.\n", cache_fname));
53 SAFE_FREE(cache_fname);
55 return (cache ? True : False);
58 BOOL login_cache_shutdown(void)
60 /* tdb_close routine returns -1 on error */
61 if (!cache) return False;
62 DEBUG(5, ("Closing cache file\n"));
63 return tdb_close(cache) != -1;
66 /* if we can't read the cache, oh well, no need to return anything */
67 LOGIN_CACHE * login_cache_read(struct samu *sampass)
73 if (!login_cache_init())
76 if (pdb_get_nt_username(sampass) == NULL) {
80 keystr = SMB_STRDUP(pdb_get_nt_username(sampass));
81 if (!keystr || !keystr[0]) {
86 DEBUG(7, ("Looking up login cache for user %s\n",
88 databuf = tdb_fetch_bystring(cache, keystr);
91 if (!(entry = SMB_MALLOC_P(LOGIN_CACHE))) {
92 DEBUG(1, ("Unable to allocate cache entry buffer!\n"));
93 SAFE_FREE(databuf.dptr);
97 if (tdb_unpack (databuf.dptr, databuf.dsize, SAM_CACHE_FORMAT,
98 &entry->entry_timestamp, &entry->acct_ctrl,
99 &entry->bad_password_count,
100 &entry->bad_password_time) == -1) {
101 DEBUG(7, ("No cache entry found\n"));
103 SAFE_FREE(databuf.dptr);
107 SAFE_FREE(databuf.dptr);
109 DEBUG(5, ("Found login cache entry: timestamp %12u, flags 0x%x, count %d, time %12u\n",
110 (unsigned int)entry->entry_timestamp, entry->acct_ctrl,
111 entry->bad_password_count, (unsigned int)entry->bad_password_time));
115 BOOL login_cache_write(const struct samu *sampass, LOGIN_CACHE entry)
121 if (!login_cache_init())
124 if (pdb_get_nt_username(sampass) == NULL) {
128 keystr = SMB_STRDUP(pdb_get_nt_username(sampass));
129 if (!keystr || !keystr[0]) {
134 entry.entry_timestamp = time(NULL);
137 tdb_pack(NULL, 0, SAM_CACHE_FORMAT,
138 entry.entry_timestamp,
140 entry.bad_password_count,
141 entry.bad_password_time);
142 databuf.dptr = SMB_MALLOC_ARRAY(uint8, databuf.dsize);
148 if (tdb_pack(databuf.dptr, databuf.dsize, SAM_CACHE_FORMAT,
149 entry.entry_timestamp,
151 entry.bad_password_count,
152 entry.bad_password_time)
155 SAFE_FREE(databuf.dptr);
159 ret = tdb_store_bystring(cache, keystr, databuf, 0);
161 SAFE_FREE(databuf.dptr);
165 BOOL login_cache_delentry(const struct samu *sampass)
170 if (!login_cache_init())
173 if (pdb_get_nt_username(sampass) == NULL) {
177 keystr = SMB_STRDUP(pdb_get_nt_username(sampass));
178 if (!keystr || !keystr[0]) {
183 DEBUG(9, ("About to delete entry for %s\n", keystr));
184 ret = tdb_delete_bystring(cache, keystr);
185 DEBUG(9, ("tdb_delete returned %d\n", ret));