7fd3b47826f8d1c941cd342244b8714f05043aa4
[amitay/samba.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 2 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, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include "includes.h"
22
23 #undef DBGC_CLASS
24 #define DBGC_CLASS DBGC_PASSDB
25
26 #define LOGIN_CACHE_FILE "login_cache.tdb"
27
28 #define SAM_CACHE_FORMAT "dwwd"
29
30 static TDB_CONTEXT *cache;
31
32 BOOL login_cache_init(void)
33 {
34         char* cache_fname = NULL;
35         
36         /* skip file open if it's already opened */
37         if (cache) return True;
38
39         asprintf(&cache_fname, "%s/%s", lp_lockdir(), LOGIN_CACHE_FILE);
40         if (cache_fname)
41                 DEBUG(5, ("Opening cache file at %s\n", cache_fname));
42         else {
43                 DEBUG(0, ("Filename allocation failed.\n"));
44                 return False;
45         }
46
47         cache = tdb_open_log(cache_fname, 0, TDB_DEFAULT,
48                              O_RDWR|O_CREAT, 0644);
49
50         if (!cache)
51                 DEBUG(5, ("Attempt to open %s failed.\n", cache_fname));
52
53         SAFE_FREE(cache_fname);
54
55         return (cache ? True : False);
56 }
57
58 BOOL login_cache_shutdown(void)
59 {
60         /* tdb_close routine returns -1 on error */
61         if (!cache) return False;
62         DEBUG(5, ("Closing cache file\n"));
63         return tdb_close(cache) != -1;
64 }
65
66 /* if we can't read the cache, oh well, no need to return anything */
67 LOGIN_CACHE * login_cache_read(struct samu *sampass)
68 {
69         TDB_DATA keybuf, databuf;
70         LOGIN_CACHE *entry;
71
72         if (!login_cache_init())
73                 return NULL;
74
75         if (pdb_get_nt_username(sampass) == NULL) {
76                 return NULL;
77         }
78
79         keybuf.dptr = SMB_STRDUP(pdb_get_nt_username(sampass));
80         if (!keybuf.dptr || !strlen(keybuf.dptr)) {
81                 SAFE_FREE(keybuf.dptr);
82                 return NULL;
83         }
84         keybuf.dsize = strlen(keybuf.dptr) + 1;
85
86         DEBUG(7, ("Looking up login cache for user %s\n",
87                   keybuf.dptr));
88         databuf = tdb_fetch(cache, keybuf);
89         SAFE_FREE(keybuf.dptr);
90
91         if (!(entry = SMB_MALLOC_P(LOGIN_CACHE))) {
92                 DEBUG(1, ("Unable to allocate cache entry buffer!\n"));
93                 SAFE_FREE(databuf.dptr);
94                 return NULL;
95         }
96
97         if (tdb_unpack (databuf.dptr, databuf.dsize, SAM_CACHE_FORMAT,
98                         &entry->entry_timestamp, &entry->acct_ctrl, 
99                         &entry->bad_password_count, 
100                         &entry->bad_password_time) == -1) {
101                 DEBUG(7, ("No cache entry found\n"));
102                 SAFE_FREE(entry);
103                 SAFE_FREE(databuf.dptr);
104                 return NULL;
105         }
106
107         SAFE_FREE(databuf.dptr);
108
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 entry;
113 }
114
115 BOOL login_cache_write(const struct samu *sampass, LOGIN_CACHE entry)
116 {
117
118         TDB_DATA keybuf, databuf;
119         BOOL ret;
120
121         if (!login_cache_init())
122                 return False;
123
124         if (pdb_get_nt_username(sampass) == NULL) {
125                 return False;
126         }
127
128         keybuf.dptr = SMB_STRDUP(pdb_get_nt_username(sampass));
129         if (!keybuf.dptr || !strlen(keybuf.dptr)) {
130                 SAFE_FREE(keybuf.dptr);
131                 return False;
132         }
133         keybuf.dsize = strlen(keybuf.dptr) + 1;
134
135         entry.entry_timestamp = time(NULL);
136
137         databuf.dsize = 
138                 tdb_pack(NULL, 0, SAM_CACHE_FORMAT,
139                          entry.entry_timestamp,
140                          entry.acct_ctrl,
141                          entry.bad_password_count,
142                          entry.bad_password_time);
143         databuf.dptr = SMB_MALLOC_ARRAY(char, databuf.dsize);
144         if (!databuf.dptr) {
145                 SAFE_FREE(keybuf.dptr);
146                 return False;
147         }
148                          
149         if (tdb_pack(databuf.dptr, databuf.dsize, SAM_CACHE_FORMAT,
150                          entry.entry_timestamp,
151                          entry.acct_ctrl,
152                          entry.bad_password_count,
153                          entry.bad_password_time)
154             != databuf.dsize) {
155                 SAFE_FREE(keybuf.dptr);
156                 SAFE_FREE(databuf.dptr);
157                 return False;
158         }
159
160         ret = tdb_store(cache, keybuf, databuf, 0);
161         SAFE_FREE(keybuf.dptr);
162         SAFE_FREE(databuf.dptr);
163         return ret == 0;
164 }
165
166 BOOL login_cache_delentry(const struct samu *sampass)
167 {
168         int ret;
169         TDB_DATA keybuf;
170         
171         if (!login_cache_init()) 
172                 return False;   
173
174         if (pdb_get_nt_username(sampass) == NULL) {
175                 return False;
176         }
177
178         keybuf.dptr = SMB_STRDUP(pdb_get_nt_username(sampass));
179         if (!keybuf.dptr || !strlen(keybuf.dptr)) {
180                 SAFE_FREE(keybuf.dptr);
181                 return False;
182         }
183         keybuf.dsize = strlen(keybuf.dptr) + 1;
184         DEBUG(9, ("About to delete entry for %s\n", keybuf.dptr));
185         ret = tdb_delete(cache, keybuf);
186         DEBUG(9, ("tdb_delete returned %d\n", ret));
187         
188         SAFE_FREE(keybuf.dptr);
189         return ret == 0;
190 }
191