r560: Fix bugzilla 1279: cannot control individual print jobs using cups
[Samba/gebeck_regimport.git] / source3 / passdb / login_cache.c
blobfc05122ccaf6c41937055bd1a0da5a907a3de8c8
1 /*
2 Unix SMB/CIFS implementation.
3 SAM_ACCOUNT 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 2 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, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include "includes.h"
23 #undef DBGC_CLASS
24 #define DBGC_CLASS DBGC_PASSDB
26 #define LOGIN_CACHE_FILE "login_cache.tdb"
28 #define SAM_CACHE_FORMAT "dwwd"
30 static TDB_CONTEXT *cache;
32 BOOL login_cache_init(void)
34 char* cache_fname = NULL;
36 /* skip file open if it's already opened */
37 if (cache) return True;
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;
47 cache = tdb_open_log(cache_fname, 0, TDB_DEFAULT,
48 O_RDWR|O_CREAT, 0644);
50 if (!cache)
51 DEBUG(5, ("Attempt to open %s failed.\n", cache_fname));
53 SAFE_FREE(cache_fname);
55 return (cache ? True : False);
58 BOOL login_cache_shutdown(void)
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;
66 /* if we can't read the cache, oh well, no need to return anything */
67 LOGIN_CACHE * login_cache_read(SAM_ACCOUNT *sampass)
69 TDB_DATA keybuf, databuf;
70 LOGIN_CACHE *entry;
72 if (!login_cache_init())
73 return NULL;
75 keybuf.dptr = strdup(pdb_get_nt_username(sampass));
76 if (!keybuf.dptr || !strlen(keybuf.dptr)) {
77 SAFE_FREE(keybuf.dptr);
78 return NULL;
80 keybuf.dsize = strlen(keybuf.dptr) + 1;
82 DEBUG(7, ("Looking up login cache for user %s\n",
83 keybuf.dptr));
84 databuf = tdb_fetch(cache, keybuf);
85 SAFE_FREE(keybuf.dptr);
87 if (!(entry = malloc(sizeof(LOGIN_CACHE)))) {
88 DEBUG(1, ("Unable to allocate cache entry buffer!\n"));
89 SAFE_FREE(databuf.dptr);
90 return NULL;
93 if (tdb_unpack (databuf.dptr, databuf.dsize, SAM_CACHE_FORMAT,
94 &entry->entry_timestamp, &entry->acct_ctrl,
95 &entry->bad_password_count,
96 &entry->bad_password_time) == -1) {
97 DEBUG(7, ("No cache entry found\n"));
98 SAFE_FREE(databuf.dptr);
99 return NULL;
102 DEBUG(5, ("Found login cache entry: timestamp %12u, flags 0x%x, count %d, time %12u\n",
103 (unsigned int)entry->entry_timestamp, entry->acct_ctrl,
104 entry->bad_password_count, (unsigned int)entry->bad_password_time));
105 return entry;
108 BOOL login_cache_write(const SAM_ACCOUNT *sampass, LOGIN_CACHE entry)
111 TDB_DATA keybuf, databuf;
112 BOOL ret;
114 if (!login_cache_init())
115 return False;
117 keybuf.dptr = strdup(pdb_get_nt_username(sampass));
118 if (!keybuf.dptr || !strlen(keybuf.dptr)) {
119 SAFE_FREE(keybuf.dptr);
120 return False;
122 keybuf.dsize = strlen(keybuf.dptr) + 1;
124 entry.entry_timestamp = time(NULL);
126 databuf.dsize =
127 tdb_pack(NULL, 0, SAM_CACHE_FORMAT,
128 entry.entry_timestamp,
129 entry.acct_ctrl,
130 entry.bad_password_count,
131 entry.bad_password_time);
132 databuf.dptr = malloc(databuf.dsize);
133 if (!databuf.dptr) {
134 SAFE_FREE(keybuf.dptr);
135 return False;
138 if (tdb_pack(databuf.dptr, databuf.dsize, SAM_CACHE_FORMAT,
139 entry.entry_timestamp,
140 entry.acct_ctrl,
141 entry.bad_password_count,
142 entry.bad_password_time)
143 != databuf.dsize) {
144 SAFE_FREE(keybuf.dptr);
145 SAFE_FREE(databuf.dptr);
146 return False;
149 ret = tdb_store(cache, keybuf, databuf, 0);
150 SAFE_FREE(keybuf.dptr);
151 SAFE_FREE(databuf.dptr);
152 return ret == 0;
155 BOOL login_cache_delentry(const SAM_ACCOUNT *sampass)
157 int ret;
158 TDB_DATA keybuf;
160 if (!login_cache_init())
161 return False;
163 keybuf.dptr = strdup(pdb_get_nt_username(sampass));
164 if (!keybuf.dptr || !strlen(keybuf.dptr)) {
165 SAFE_FREE(keybuf.dptr);
166 return False;
168 keybuf.dsize = strlen(keybuf.dptr) + 1;
169 DEBUG(9, ("About to delete entry for %s\n", keybuf.dptr));
170 ret = tdb_delete(cache, keybuf);
171 DEBUG(9, ("tdb_delete returned %d\n", ret));
173 SAFE_FREE(keybuf.dptr);
174 return ret == 0;