Fix bug #7669.
[Samba.git] / source / passdb / login_cache.c
bloba0d78063fcec1833b5e7b3c9e042fe3437d4a923
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 if (asprintf(&cache_fname, "%s/%s", lp_lockdir(), LOGIN_CACHE_FILE) == -1) {
39 DEBUG(0, ("Filename allocation failed.\n"));
40 return False;
43 DEBUG(5, ("Opening cache file at %s\n", cache_fname));
45 cache = tdb_open_log(cache_fname, 0, TDB_DEFAULT,
46 O_RDWR|O_CREAT, 0644);
48 if (!cache)
49 DEBUG(5, ("Attempt to open %s failed.\n", cache_fname));
51 SAFE_FREE(cache_fname);
53 return (cache ? True : False);
56 bool login_cache_shutdown(void)
58 /* tdb_close routine returns -1 on error */
59 if (!cache) return False;
60 DEBUG(5, ("Closing cache file\n"));
61 return tdb_close(cache) != -1;
64 /* if we can't read the cache, oh well, no need to return anything */
65 LOGIN_CACHE * login_cache_read(struct samu *sampass)
67 char *keystr;
68 TDB_DATA databuf;
69 LOGIN_CACHE *entry;
70 uint32_t entry_timestamp = 0, bad_password_time = 0;
72 if (!login_cache_init())
73 return NULL;
75 if (pdb_get_nt_username(sampass) == NULL) {
76 return NULL;
79 keystr = SMB_STRDUP(pdb_get_nt_username(sampass));
80 if (!keystr || !keystr[0]) {
81 SAFE_FREE(keystr);
82 return NULL;
85 DEBUG(7, ("Looking up login cache for user %s\n",
86 keystr));
87 databuf = tdb_fetch_bystring(cache, keystr);
88 SAFE_FREE(keystr);
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;
95 ZERO_STRUCTP(entry);
97 if (tdb_unpack (databuf.dptr, databuf.dsize, SAM_CACHE_FORMAT,
98 &entry_timestamp,
99 &entry->acct_ctrl,
100 &entry->bad_password_count,
101 &bad_password_time) == -1) {
102 DEBUG(7, ("No cache entry found\n"));
103 SAFE_FREE(entry);
104 SAFE_FREE(databuf.dptr);
105 return NULL;
108 /* Deal with possible 64-bit time_t. */
109 entry->entry_timestamp = (time_t)entry_timestamp;
110 entry->bad_password_time = (time_t)bad_password_time;
112 SAFE_FREE(databuf.dptr);
114 DEBUG(5, ("Found login cache entry: timestamp %12u, flags 0x%x, count %d, time %12u\n",
115 (unsigned int)entry->entry_timestamp, entry->acct_ctrl,
116 entry->bad_password_count, (unsigned int)entry->bad_password_time));
117 return entry;
120 bool login_cache_write(const struct samu *sampass, LOGIN_CACHE entry)
122 char *keystr;
123 TDB_DATA databuf;
124 bool ret;
125 uint32_t entry_timestamp;
126 uint32_t bad_password_time = (uint32_t)entry.bad_password_time;
128 if (!login_cache_init())
129 return False;
131 if (pdb_get_nt_username(sampass) == NULL) {
132 return False;
135 keystr = SMB_STRDUP(pdb_get_nt_username(sampass));
136 if (!keystr || !keystr[0]) {
137 SAFE_FREE(keystr);
138 return False;
141 entry_timestamp = (uint32_t)time(NULL);
143 databuf.dsize =
144 tdb_pack(NULL, 0, SAM_CACHE_FORMAT,
145 entry_timestamp,
146 entry.acct_ctrl,
147 entry.bad_password_count,
148 bad_password_time);
149 databuf.dptr = SMB_MALLOC_ARRAY(uint8, databuf.dsize);
150 if (!databuf.dptr) {
151 SAFE_FREE(keystr);
152 return False;
155 if (tdb_pack(databuf.dptr, databuf.dsize, SAM_CACHE_FORMAT,
156 entry_timestamp,
157 entry.acct_ctrl,
158 entry.bad_password_count,
159 bad_password_time)
160 != databuf.dsize) {
161 SAFE_FREE(keystr);
162 SAFE_FREE(databuf.dptr);
163 return False;
166 ret = tdb_store_bystring(cache, keystr, databuf, 0);
167 SAFE_FREE(keystr);
168 SAFE_FREE(databuf.dptr);
169 return ret == 0;
172 bool login_cache_delentry(const struct samu *sampass)
174 int ret;
175 char *keystr;
177 if (!login_cache_init())
178 return False;
180 if (pdb_get_nt_username(sampass) == NULL) {
181 return False;
184 keystr = SMB_STRDUP(pdb_get_nt_username(sampass));
185 if (!keystr || !keystr[0]) {
186 SAFE_FREE(keystr);
187 return False;
190 DEBUG(9, ("About to delete entry for %s\n", keystr));
191 ret = tdb_delete_bystring(cache, keystr);
192 DEBUG(9, ("tdb_delete returned %d\n", ret));
194 SAFE_FREE(keystr);
195 return ret == 0;