selftest: Use standard subunit commands for progress reporting.
[Samba/eduardoll.git] / source3 / passdb / login_cache.c
blobcf6c796321cb094fdf1e754bbb05591e849eaa95
1 /*
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, see <http://www.gnu.org/licenses/>.
20 #include "includes.h"
22 #undef DBGC_CLASS
23 #define DBGC_CLASS DBGC_PASSDB
25 #define LOGIN_CACHE_FILE "login_cache.tdb"
27 #define SAM_CACHE_FORMAT "dwwd"
29 static TDB_CONTEXT *cache;
31 bool login_cache_init(void)
33 char* cache_fname = NULL;
35 /* skip file open if it's already opened */
36 if (cache) return True;
38 cache_fname = cache_path(LOGIN_CACHE_FILE);
39 if (cache_fname == NULL) {
40 DEBUG(0, ("Filename allocation failed.\n"));
41 return False;
44 DEBUG(5, ("Opening cache file at %s\n", cache_fname));
46 cache = tdb_open_log(cache_fname, 0, TDB_DEFAULT,
47 O_RDWR|O_CREAT, 0644);
49 if (!cache)
50 DEBUG(5, ("Attempt to open %s failed.\n", cache_fname));
52 TALLOC_FREE(cache_fname);
54 return (cache ? True : False);
57 bool login_cache_shutdown(void)
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;
65 /* if we can't read the cache, oh well, no need to return anything */
66 bool login_cache_read(struct samu *sampass, struct login_cache *entry)
68 char *keystr;
69 TDB_DATA databuf;
70 uint32_t entry_timestamp = 0, bad_password_time = 0;
72 if (!login_cache_init()) {
73 return false;
76 if (pdb_get_nt_username(sampass) == NULL) {
77 return false;
80 keystr = SMB_STRDUP(pdb_get_nt_username(sampass));
81 if (!keystr || !keystr[0]) {
82 SAFE_FREE(keystr);
83 return false;
86 DEBUG(7, ("Looking up login cache for user %s\n",
87 keystr));
88 databuf = tdb_fetch_bystring(cache, keystr);
89 SAFE_FREE(keystr);
91 ZERO_STRUCTP(entry);
93 if (tdb_unpack (databuf.dptr, databuf.dsize, SAM_CACHE_FORMAT,
94 &entry_timestamp,
95 &entry->acct_ctrl,
96 &entry->bad_password_count,
97 &bad_password_time) == -1) {
98 DEBUG(7, ("No cache entry found\n"));
99 SAFE_FREE(databuf.dptr);
100 return false;
103 /* Deal with possible 64-bit time_t. */
104 entry->entry_timestamp = (time_t)entry_timestamp;
105 entry->bad_password_time = (time_t)bad_password_time;
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));
112 return true;
115 bool login_cache_write(const struct samu *sampass,
116 const struct login_cache *entry)
118 char *keystr;
119 TDB_DATA databuf;
120 bool ret;
121 uint32_t entry_timestamp;
122 uint32_t bad_password_time = entry->bad_password_time;
124 if (!login_cache_init())
125 return False;
127 if (pdb_get_nt_username(sampass) == NULL) {
128 return False;
131 keystr = SMB_STRDUP(pdb_get_nt_username(sampass));
132 if (!keystr || !keystr[0]) {
133 SAFE_FREE(keystr);
134 return False;
137 entry_timestamp = (uint32_t)time(NULL);
139 databuf.dsize =
140 tdb_pack(NULL, 0, SAM_CACHE_FORMAT,
141 entry_timestamp,
142 entry->acct_ctrl,
143 entry->bad_password_count,
144 bad_password_time);
145 databuf.dptr = SMB_MALLOC_ARRAY(uint8, databuf.dsize);
146 if (!databuf.dptr) {
147 SAFE_FREE(keystr);
148 return False;
151 if (tdb_pack(databuf.dptr, databuf.dsize, SAM_CACHE_FORMAT,
152 entry_timestamp,
153 entry->acct_ctrl,
154 entry->bad_password_count,
155 bad_password_time)
156 != databuf.dsize) {
157 SAFE_FREE(keystr);
158 SAFE_FREE(databuf.dptr);
159 return False;
162 ret = tdb_store_bystring(cache, keystr, databuf, 0);
163 SAFE_FREE(keystr);
164 SAFE_FREE(databuf.dptr);
165 return ret == 0;
168 bool login_cache_delentry(const struct samu *sampass)
170 int ret;
171 char *keystr;
173 if (!login_cache_init())
174 return False;
176 if (pdb_get_nt_username(sampass) == NULL) {
177 return False;
180 keystr = SMB_STRDUP(pdb_get_nt_username(sampass));
181 if (!keystr || !keystr[0]) {
182 SAFE_FREE(keystr);
183 return False;
186 DEBUG(9, ("About to delete entry for %s\n", keystr));
187 ret = tdb_delete_bystring(cache, keystr);
188 DEBUG(9, ("tdb_delete returned %d\n", ret));
190 SAFE_FREE(keystr);
191 return ret == 0;