Release alpha15.
[nivanova/samba-autobuild/.git] / source3 / passdb / login_cache.c
1 /* 
2    Unix SMB/CIFS implementation.
3    struct samu local cache for 
4    Copyright (C) Jim McDonough (jmcd@us.ibm.com) 2004.
5
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.
10
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.
15
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/>.
18 */
19
20 #include "includes.h"
21 #include "system/filesys.h"
22 #include "passdb.h"
23
24 #undef DBGC_CLASS
25 #define DBGC_CLASS DBGC_PASSDB
26
27 #define LOGIN_CACHE_FILE "login_cache.tdb"
28
29 #define SAM_CACHE_FORMAT "dwwd"
30
31 static TDB_CONTEXT *cache;
32
33 bool login_cache_init(void)
34 {
35         char* cache_fname = NULL;
36
37         /* skip file open if it's already opened */
38         if (cache) return True;
39
40         cache_fname = cache_path(LOGIN_CACHE_FILE);
41         if (cache_fname == NULL) {
42                 DEBUG(0, ("Filename allocation failed.\n"));
43                 return False;
44         }
45
46         DEBUG(5, ("Opening cache file at %s\n", cache_fname));
47
48         cache = tdb_open_log(cache_fname, 0, TDB_DEFAULT,
49                              O_RDWR|O_CREAT, 0644);
50
51         if (!cache)
52                 DEBUG(5, ("Attempt to open %s failed.\n", cache_fname));
53
54         TALLOC_FREE(cache_fname);
55
56         return (cache ? True : False);
57 }
58
59 bool login_cache_shutdown(void)
60 {
61         /* tdb_close routine returns -1 on error */
62         if (!cache) return False;
63         DEBUG(5, ("Closing cache file\n"));
64         return tdb_close(cache) != -1;
65 }
66
67 /* if we can't read the cache, oh well, no need to return anything */
68 bool login_cache_read(struct samu *sampass, struct login_cache *entry)
69 {
70         char *keystr;
71         TDB_DATA databuf;
72         uint32_t entry_timestamp = 0, bad_password_time = 0;
73         uint16_t acct_ctrl;
74
75         if (!login_cache_init()) {
76                 return false;
77         }
78
79         if (pdb_get_nt_username(sampass) == NULL) {
80                 return false;
81         }
82
83         keystr = SMB_STRDUP(pdb_get_nt_username(sampass));
84         if (!keystr || !keystr[0]) {
85                 SAFE_FREE(keystr);
86                 return false;
87         }
88
89         DEBUG(7, ("Looking up login cache for user %s\n",
90                   keystr));
91         databuf = tdb_fetch_bystring(cache, keystr);
92         SAFE_FREE(keystr);
93
94         ZERO_STRUCTP(entry);
95
96         if (tdb_unpack (databuf.dptr, databuf.dsize, SAM_CACHE_FORMAT,
97                         &entry_timestamp,
98                         &acct_ctrl,
99                         &entry->bad_password_count,
100                         &bad_password_time) == -1) {
101                 DEBUG(7, ("No cache entry found\n"));
102                 SAFE_FREE(databuf.dptr);
103                 return false;
104         }
105
106         /*
107          * Deal with 32-bit acct_ctrl. In the tdb we only store 16-bit
108          * ("w" in SAM_CACHE_FORMAT). Fixes bug 7253.
109          */
110         entry->acct_ctrl = acct_ctrl;
111
112         /* Deal with possible 64-bit time_t. */
113         entry->entry_timestamp = (time_t)entry_timestamp;
114         entry->bad_password_time = (time_t)bad_password_time;
115
116         SAFE_FREE(databuf.dptr);
117
118         DEBUG(5, ("Found login cache entry: timestamp %12u, flags 0x%x, count %d, time %12u\n",
119                   (unsigned int)entry->entry_timestamp, entry->acct_ctrl, 
120                   entry->bad_password_count, (unsigned int)entry->bad_password_time));
121         return true;
122 }
123
124 bool login_cache_write(const struct samu *sampass,
125                        const struct login_cache *entry)
126 {
127         char *keystr;
128         TDB_DATA databuf;
129         bool ret;
130         uint32_t entry_timestamp;
131         uint32_t bad_password_time = entry->bad_password_time;
132
133         if (!login_cache_init())
134                 return False;
135
136         if (pdb_get_nt_username(sampass) == NULL) {
137                 return False;
138         }
139
140         keystr = SMB_STRDUP(pdb_get_nt_username(sampass));
141         if (!keystr || !keystr[0]) {
142                 SAFE_FREE(keystr);
143                 return False;
144         }
145
146         entry_timestamp = (uint32_t)time(NULL);
147
148         databuf.dsize = 
149                 tdb_pack(NULL, 0, SAM_CACHE_FORMAT,
150                          entry_timestamp,
151                          entry->acct_ctrl,
152                          entry->bad_password_count,
153                          bad_password_time);
154         databuf.dptr = SMB_MALLOC_ARRAY(uint8, databuf.dsize);
155         if (!databuf.dptr) {
156                 SAFE_FREE(keystr);
157                 return False;
158         }
159
160         if (tdb_pack(databuf.dptr, databuf.dsize, SAM_CACHE_FORMAT,
161                          entry_timestamp,
162                          entry->acct_ctrl,
163                          entry->bad_password_count,
164                          bad_password_time)
165             != databuf.dsize) {
166                 SAFE_FREE(keystr);
167                 SAFE_FREE(databuf.dptr);
168                 return False;
169         }
170
171         ret = tdb_store_bystring(cache, keystr, databuf, 0);
172         SAFE_FREE(keystr);
173         SAFE_FREE(databuf.dptr);
174         return ret == 0;
175 }
176
177 bool login_cache_delentry(const struct samu *sampass)
178 {
179         int ret;
180         char *keystr;
181
182         if (!login_cache_init()) 
183                 return False;   
184
185         if (pdb_get_nt_username(sampass) == NULL) {
186                 return False;
187         }
188
189         keystr = SMB_STRDUP(pdb_get_nt_username(sampass));
190         if (!keystr || !keystr[0]) {
191                 SAFE_FREE(keystr);
192                 return False;
193         }
194
195         DEBUG(9, ("About to delete entry for %s\n", keystr));
196         ret = tdb_delete_bystring(cache, keystr);
197         DEBUG(9, ("tdb_delete returned %d\n", ret));
198
199         SAFE_FREE(keystr);
200         return ret == 0;
201 }