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