pyldb: Fix CID 1034792 Dereference null return value
[Samba.git] / source3 / passdb / login_cache.c
bloba53fe5b24f7f33dd7f6f0d6dea449fbf8db12322
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"
21 #include "system/filesys.h"
22 #include "passdb.h"
23 #include "util_tdb.h"
25 #undef DBGC_CLASS
26 #define DBGC_CLASS DBGC_PASSDB
28 #define LOGIN_CACHE_FILE "login_cache.tdb"
30 #define SAM_CACHE_FORMAT "dwwd"
32 static TDB_CONTEXT *cache;
34 bool login_cache_init(void)
36 char* cache_fname = NULL;
38 /* skip file open if it's already opened */
39 if (cache) return True;
41 cache_fname = cache_path(LOGIN_CACHE_FILE);
42 if (cache_fname == NULL) {
43 DEBUG(0, ("Filename allocation failed.\n"));
44 return False;
47 DEBUG(5, ("Opening cache file at %s\n", cache_fname));
49 cache = tdb_open_log(cache_fname, 0, TDB_DEFAULT,
50 O_RDWR|O_CREAT, 0644);
52 if (!cache)
53 DEBUG(5, ("Attempt to open %s failed.\n", cache_fname));
55 TALLOC_FREE(cache_fname);
57 return (cache ? True : False);
60 bool login_cache_shutdown(void)
62 /* tdb_close routine returns non-zero on error */
63 if (!cache) return False;
64 DEBUG(5, ("Closing cache file\n"));
65 return tdb_close(cache) == 0;
68 /* if we can't read the cache, oh well, no need to return anything */
69 bool login_cache_read(struct samu *sampass, struct login_cache *entry)
71 char *keystr;
72 TDB_DATA databuf;
73 uint32_t entry_timestamp = 0, bad_password_time = 0;
74 uint16_t acct_ctrl;
76 if (!login_cache_init()) {
77 return false;
80 if (pdb_get_nt_username(sampass) == NULL) {
81 return false;
84 keystr = SMB_STRDUP(pdb_get_nt_username(sampass));
85 if (!keystr || !keystr[0]) {
86 SAFE_FREE(keystr);
87 return false;
90 DEBUG(7, ("Looking up login cache for user %s\n",
91 keystr));
92 databuf = tdb_fetch_bystring(cache, keystr);
93 SAFE_FREE(keystr);
95 ZERO_STRUCTP(entry);
97 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 DEBUG(7, ("No cache entry found\n"));
103 SAFE_FREE(databuf.dptr);
104 return false;
108 * Deal with 32-bit acct_ctrl. In the tdb we only store 16-bit
109 * ("w" in SAM_CACHE_FORMAT). Fixes bug 7253.
111 entry->acct_ctrl = acct_ctrl;
113 /* Deal with possible 64-bit time_t. */
114 entry->entry_timestamp = (time_t)entry_timestamp;
115 entry->bad_password_time = (time_t)bad_password_time;
117 SAFE_FREE(databuf.dptr);
119 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 return true;
125 bool login_cache_write(const struct samu *sampass,
126 const struct login_cache *entry)
128 char *keystr;
129 TDB_DATA databuf;
130 bool ret;
131 uint32_t entry_timestamp;
132 uint32_t bad_password_time = entry->bad_password_time;
134 if (!login_cache_init())
135 return False;
137 if (pdb_get_nt_username(sampass) == NULL) {
138 return False;
141 keystr = SMB_STRDUP(pdb_get_nt_username(sampass));
142 if (!keystr || !keystr[0]) {
143 SAFE_FREE(keystr);
144 return False;
147 entry_timestamp = (uint32_t)time(NULL);
149 databuf.dsize =
150 tdb_pack(NULL, 0, SAM_CACHE_FORMAT,
151 entry_timestamp,
152 entry->acct_ctrl,
153 entry->bad_password_count,
154 bad_password_time);
155 databuf.dptr = SMB_MALLOC_ARRAY(uint8, databuf.dsize);
156 if (!databuf.dptr) {
157 SAFE_FREE(keystr);
158 return False;
161 if (tdb_pack(databuf.dptr, databuf.dsize, SAM_CACHE_FORMAT,
162 entry_timestamp,
163 entry->acct_ctrl,
164 entry->bad_password_count,
165 bad_password_time)
166 != databuf.dsize) {
167 SAFE_FREE(keystr);
168 SAFE_FREE(databuf.dptr);
169 return False;
172 ret = tdb_store_bystring(cache, keystr, databuf, 0);
173 SAFE_FREE(keystr);
174 SAFE_FREE(databuf.dptr);
175 return ret == 0;
178 bool login_cache_delentry(const struct samu *sampass)
180 int ret;
181 char *keystr;
183 if (!login_cache_init())
184 return False;
186 if (pdb_get_nt_username(sampass) == NULL) {
187 return False;
190 keystr = SMB_STRDUP(pdb_get_nt_username(sampass));
191 if (!keystr || !keystr[0]) {
192 SAFE_FREE(keystr);
193 return False;
196 DEBUG(9, ("About to delete entry for %s\n", keystr));
197 ret = tdb_delete_bystring(cache, keystr);
198 DEBUG(9, ("tdb_delete returned %d\n", ret));
200 SAFE_FREE(keystr);
201 return ret == 0;