s3-passdb: Fix typo in comment.
[Samba.git] / source3 / passdb / login_cache.c
blobeba83ea686c10d29ac634695770af92d865a8a39
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;
71 uint16_t acct_ctrl;
73 if (!login_cache_init()) {
74 return false;
77 if (pdb_get_nt_username(sampass) == NULL) {
78 return false;
81 keystr = SMB_STRDUP(pdb_get_nt_username(sampass));
82 if (!keystr || !keystr[0]) {
83 SAFE_FREE(keystr);
84 return false;
87 DEBUG(7, ("Looking up login cache for user %s\n",
88 keystr));
89 databuf = tdb_fetch_bystring(cache, keystr);
90 SAFE_FREE(keystr);
92 ZERO_STRUCTP(entry);
94 if (tdb_unpack (databuf.dptr, databuf.dsize, SAM_CACHE_FORMAT,
95 &entry_timestamp,
96 &acct_ctrl,
97 &entry->bad_password_count,
98 &bad_password_time) == -1) {
99 DEBUG(7, ("No cache entry found\n"));
100 SAFE_FREE(databuf.dptr);
101 return false;
105 * Deal with 32-bit acct_ctrl. In the tdb we only store 16-bit
106 * ("w" in SAM_CACHE_FORMAT). Fixes bug 7253.
108 entry->acct_ctrl = acct_ctrl;
110 /* Deal with possible 64-bit time_t. */
111 entry->entry_timestamp = (time_t)entry_timestamp;
112 entry->bad_password_time = (time_t)bad_password_time;
114 SAFE_FREE(databuf.dptr);
116 DEBUG(5, ("Found login cache entry: timestamp %12u, flags 0x%x, count %d, time %12u\n",
117 (unsigned int)entry->entry_timestamp, entry->acct_ctrl,
118 entry->bad_password_count, (unsigned int)entry->bad_password_time));
119 return true;
122 bool login_cache_write(const struct samu *sampass,
123 const struct login_cache *entry)
125 char *keystr;
126 TDB_DATA databuf;
127 bool ret;
128 uint32_t entry_timestamp;
129 uint32_t bad_password_time = entry->bad_password_time;
131 if (!login_cache_init())
132 return False;
134 if (pdb_get_nt_username(sampass) == NULL) {
135 return False;
138 keystr = SMB_STRDUP(pdb_get_nt_username(sampass));
139 if (!keystr || !keystr[0]) {
140 SAFE_FREE(keystr);
141 return False;
144 entry_timestamp = (uint32_t)time(NULL);
146 databuf.dsize =
147 tdb_pack(NULL, 0, SAM_CACHE_FORMAT,
148 entry_timestamp,
149 entry->acct_ctrl,
150 entry->bad_password_count,
151 bad_password_time);
152 databuf.dptr = SMB_MALLOC_ARRAY(uint8, databuf.dsize);
153 if (!databuf.dptr) {
154 SAFE_FREE(keystr);
155 return False;
158 if (tdb_pack(databuf.dptr, databuf.dsize, SAM_CACHE_FORMAT,
159 entry_timestamp,
160 entry->acct_ctrl,
161 entry->bad_password_count,
162 bad_password_time)
163 != databuf.dsize) {
164 SAFE_FREE(keystr);
165 SAFE_FREE(databuf.dptr);
166 return False;
169 ret = tdb_store_bystring(cache, keystr, databuf, 0);
170 SAFE_FREE(keystr);
171 SAFE_FREE(databuf.dptr);
172 return ret == 0;
175 bool login_cache_delentry(const struct samu *sampass)
177 int ret;
178 char *keystr;
180 if (!login_cache_init())
181 return False;
183 if (pdb_get_nt_username(sampass) == NULL) {
184 return False;
187 keystr = SMB_STRDUP(pdb_get_nt_username(sampass));
188 if (!keystr || !keystr[0]) {
189 SAFE_FREE(keystr);
190 return False;
193 DEBUG(9, ("About to delete entry for %s\n", keystr));
194 ret = tdb_delete_bystring(cache, keystr);
195 DEBUG(9, ("tdb_delete returned %d\n", ret));
197 SAFE_FREE(keystr);
198 return ret == 0;