Samba 3: added Samba 3.0.24 sources
[tomato.git] / release / src / router / samba3 / source / nsswitch / winbindd_cache.c
blobcdb86f9f61ba17dbd194cd1872114c20ba39b5eb
1 /*
2 Unix SMB/CIFS implementation.
4 Winbind cache backend functions
6 Copyright (C) Andrew Tridgell 2001
7 Copyright (C) Gerald Carter 2003
8 Copyright (C) Volker Lendecke 2005
9 Copyright (C) Guenther Deschner 2005
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include "includes.h"
27 #include "winbindd.h"
29 #undef DBGC_CLASS
30 #define DBGC_CLASS DBGC_WINBIND
32 /* Global online/offline state - False when online. winbindd starts up online
33 and sets this to true if the first query fails and there's an entry in
34 the cache tdb telling us to stay offline. */
36 static BOOL global_winbindd_offline_state;
38 struct winbind_cache {
39 TDB_CONTEXT *tdb;
42 struct cache_entry {
43 NTSTATUS status;
44 uint32 sequence_number;
45 uint8 *data;
46 uint32 len, ofs;
49 #define WINBINDD_MAX_CACHE_SIZE (50*1024*1024)
51 static struct winbind_cache *wcache;
53 void winbindd_check_cache_size(time_t t)
55 static time_t last_check_time;
56 struct stat st;
58 if (last_check_time == (time_t)0)
59 last_check_time = t;
61 if (t - last_check_time < 60 && t - last_check_time > 0)
62 return;
64 if (wcache == NULL || wcache->tdb == NULL) {
65 DEBUG(0, ("Unable to check size of tdb cache - cache not open !\n"));
66 return;
69 if (fstat(wcache->tdb->fd, &st) == -1) {
70 DEBUG(0, ("Unable to check size of tdb cache %s!\n", strerror(errno) ));
71 return;
74 if (st.st_size > WINBINDD_MAX_CACHE_SIZE) {
75 DEBUG(10,("flushing cache due to size (%lu) > (%lu)\n",
76 (unsigned long)st.st_size,
77 (unsigned long)WINBINDD_MAX_CACHE_SIZE));
78 wcache_flush_cache();
82 /* get the winbind_cache structure */
83 static struct winbind_cache *get_cache(struct winbindd_domain *domain)
85 struct winbind_cache *ret = wcache;
86 #ifdef HAVE_ADS
87 struct winbindd_domain *our_domain = domain;
88 #endif
90 /* we have to know what type of domain we are dealing with first */
92 if ( !domain->initialized )
93 set_dc_type_and_flags( domain );
95 /*
96 OK. listen up becasue I'm only going to say this once.
97 We have the following scenarios to consider
98 (a) trusted AD domains on a Samba DC,
99 (b) trusted AD domains and we are joined to a non-kerberos domain
100 (c) trusted AD domains and we are joined to a kerberos (AD) domain
102 For (a) we can always contact the trusted domain using krb5
103 since we have the domain trust account password
105 For (b) we can only use RPC since we have no way of
106 getting a krb5 ticket in our own domain
108 For (c) we can always use krb5 since we have a kerberos trust
110 --jerry
113 if (!domain->backend) {
114 extern struct winbindd_methods reconnect_methods;
115 #ifdef HAVE_ADS
116 extern struct winbindd_methods ads_methods;
118 /* find our domain first so we can figure out if we
119 are joined to a kerberized domain */
121 if ( !domain->primary )
122 our_domain = find_our_domain();
124 if ( (our_domain->active_directory || IS_DC) && domain->active_directory ) {
125 DEBUG(5,("get_cache: Setting ADS methods for domain %s\n", domain->name));
126 domain->backend = &ads_methods;
127 } else {
128 #endif /* HAVE_ADS */
129 DEBUG(5,("get_cache: Setting MS-RPC methods for domain %s\n", domain->name));
130 domain->backend = &reconnect_methods;
131 #ifdef HAVE_ADS
133 #endif /* HAVE_ADS */
136 if (ret)
137 return ret;
139 ret = SMB_XMALLOC_P(struct winbind_cache);
140 ZERO_STRUCTP(ret);
142 wcache = ret;
143 wcache_flush_cache();
145 return ret;
149 free a centry structure
151 static void centry_free(struct cache_entry *centry)
153 if (!centry)
154 return;
155 SAFE_FREE(centry->data);
156 free(centry);
160 pull a uint32 from a cache entry
162 static uint32 centry_uint32(struct cache_entry *centry)
164 uint32 ret;
165 if (centry->len - centry->ofs < 4) {
166 DEBUG(0,("centry corruption? needed 4 bytes, have %d\n",
167 centry->len - centry->ofs));
168 smb_panic("centry_uint32");
170 ret = IVAL(centry->data, centry->ofs);
171 centry->ofs += 4;
172 return ret;
176 pull a uint16 from a cache entry
178 static uint16 centry_uint16(struct cache_entry *centry)
180 uint16 ret;
181 if (centry->len - centry->ofs < 2) {
182 DEBUG(0,("centry corruption? needed 2 bytes, have %d\n",
183 centry->len - centry->ofs));
184 smb_panic("centry_uint16");
186 ret = CVAL(centry->data, centry->ofs);
187 centry->ofs += 2;
188 return ret;
192 pull a uint8 from a cache entry
194 static uint8 centry_uint8(struct cache_entry *centry)
196 uint8 ret;
197 if (centry->len - centry->ofs < 1) {
198 DEBUG(0,("centry corruption? needed 1 bytes, have %d\n",
199 centry->len - centry->ofs));
200 smb_panic("centry_uint32");
202 ret = CVAL(centry->data, centry->ofs);
203 centry->ofs += 1;
204 return ret;
208 pull a NTTIME from a cache entry
210 static NTTIME centry_nttime(struct cache_entry *centry)
212 NTTIME ret;
213 if (centry->len - centry->ofs < 8) {
214 DEBUG(0,("centry corruption? needed 8 bytes, have %d\n",
215 centry->len - centry->ofs));
216 smb_panic("centry_nttime");
218 ret.low = IVAL(centry->data, centry->ofs);
219 centry->ofs += 4;
220 ret.high = IVAL(centry->data, centry->ofs);
221 centry->ofs += 4;
222 return ret;
226 pull a time_t from a cache entry
228 static time_t centry_time(struct cache_entry *centry)
230 time_t ret;
231 if (centry->len - centry->ofs < sizeof(time_t)) {
232 DEBUG(0,("centry corruption? needed %u bytes, have %u\n",
233 (unsigned int)sizeof(time_t), (unsigned int)(centry->len - centry->ofs)));
234 smb_panic("centry_time");
236 ret = IVAL(centry->data, centry->ofs); /* FIXME: correct ? */
237 centry->ofs += sizeof(time_t);
238 return ret;
241 /* pull a string from a cache entry, using the supplied
242 talloc context
244 static char *centry_string(struct cache_entry *centry, TALLOC_CTX *mem_ctx)
246 uint32 len;
247 char *ret;
249 len = centry_uint8(centry);
251 if (len == 0xFF) {
252 /* a deliberate NULL string */
253 return NULL;
256 if (centry->len - centry->ofs < len) {
257 DEBUG(0,("centry corruption? needed %d bytes, have %d\n",
258 len, centry->len - centry->ofs));
259 smb_panic("centry_string");
262 ret = TALLOC(mem_ctx, len+1);
263 if (!ret) {
264 smb_panic("centry_string out of memory\n");
266 memcpy(ret,centry->data + centry->ofs, len);
267 ret[len] = 0;
268 centry->ofs += len;
269 return ret;
272 /* pull a hash16 from a cache entry, using the supplied
273 talloc context
275 static char *centry_hash16(struct cache_entry *centry, TALLOC_CTX *mem_ctx)
277 uint32 len;
278 char *ret;
280 len = centry_uint8(centry);
282 if (len != 16) {
283 DEBUG(0,("centry corruption? hash len (%u) != 16\n",
284 len ));
285 smb_panic("centry_hash16");
288 if (centry->len - centry->ofs < 16) {
289 DEBUG(0,("centry corruption? needed 16 bytes, have %d\n",
290 centry->len - centry->ofs));
291 smb_panic("centry_hash16");
294 ret = TALLOC_ARRAY(mem_ctx, char, 16);
295 if (!ret) {
296 smb_panic("centry_hash out of memory\n");
298 memcpy(ret,centry->data + centry->ofs, 16);
299 centry->ofs += 16;
300 return ret;
303 /* pull a sid from a cache entry, using the supplied
304 talloc context
306 static BOOL centry_sid(struct cache_entry *centry, TALLOC_CTX *mem_ctx, DOM_SID *sid)
308 char *sid_string;
309 sid_string = centry_string(centry, mem_ctx);
310 if ((sid_string == NULL) || (!string_to_sid(sid, sid_string))) {
311 return False;
313 return True;
316 /* the server is considered down if it can't give us a sequence number */
317 static BOOL wcache_server_down(struct winbindd_domain *domain)
319 BOOL ret;
321 if (!wcache->tdb)
322 return False;
324 ret = (domain->sequence_number == DOM_SEQUENCE_NONE);
326 if (ret)
327 DEBUG(10,("wcache_server_down: server for Domain %s down\n",
328 domain->name ));
329 return ret;
332 static NTSTATUS fetch_cache_seqnum( struct winbindd_domain *domain, time_t now )
334 TDB_DATA data;
335 fstring key;
336 uint32 time_diff;
338 if (!wcache->tdb) {
339 DEBUG(10,("fetch_cache_seqnum: tdb == NULL\n"));
340 return NT_STATUS_UNSUCCESSFUL;
343 fstr_sprintf( key, "SEQNUM/%s", domain->name );
345 data = tdb_fetch_bystring( wcache->tdb, key );
346 if ( !data.dptr || data.dsize!=8 ) {
347 DEBUG(10,("fetch_cache_seqnum: invalid data size key [%s]\n", key ));
348 return NT_STATUS_UNSUCCESSFUL;
351 domain->sequence_number = IVAL(data.dptr, 0);
352 domain->last_seq_check = IVAL(data.dptr, 4);
354 SAFE_FREE(data.dptr);
356 /* have we expired? */
358 time_diff = now - domain->last_seq_check;
359 if ( time_diff > lp_winbind_cache_time() ) {
360 DEBUG(10,("fetch_cache_seqnum: timeout [%s][%u @ %u]\n",
361 domain->name, domain->sequence_number,
362 (uint32)domain->last_seq_check));
363 return NT_STATUS_UNSUCCESSFUL;
366 DEBUG(10,("fetch_cache_seqnum: success [%s][%u @ %u]\n",
367 domain->name, domain->sequence_number,
368 (uint32)domain->last_seq_check));
370 return NT_STATUS_OK;
373 static NTSTATUS store_cache_seqnum( struct winbindd_domain *domain )
375 TDB_DATA data, key;
376 fstring key_str;
377 char buf[8];
379 if (!wcache->tdb) {
380 DEBUG(10,("store_cache_seqnum: tdb == NULL\n"));
381 return NT_STATUS_UNSUCCESSFUL;
384 fstr_sprintf( key_str, "SEQNUM/%s", domain->name );
385 key.dptr = key_str;
386 key.dsize = strlen(key_str)+1;
388 SIVAL(buf, 0, domain->sequence_number);
389 SIVAL(buf, 4, domain->last_seq_check);
390 data.dptr = buf;
391 data.dsize = 8;
393 if ( tdb_store( wcache->tdb, key, data, TDB_REPLACE) == -1 ) {
394 DEBUG(10,("store_cache_seqnum: tdb_store fail key [%s]\n", key_str ));
395 return NT_STATUS_UNSUCCESSFUL;
398 DEBUG(10,("store_cache_seqnum: success [%s][%u @ %u]\n",
399 domain->name, domain->sequence_number,
400 (uint32)domain->last_seq_check));
402 return NT_STATUS_OK;
406 refresh the domain sequence number. If force is True
407 then always refresh it, no matter how recently we fetched it
410 static void refresh_sequence_number(struct winbindd_domain *domain, BOOL force)
412 NTSTATUS status;
413 unsigned time_diff;
414 time_t t = time(NULL);
415 unsigned cache_time = lp_winbind_cache_time();
417 get_cache( domain );
419 #if 0 /* JERRY -- disable as the default cache time is now 5 minutes */
420 /* trying to reconnect is expensive, don't do it too often */
421 if (domain->sequence_number == DOM_SEQUENCE_NONE) {
422 cache_time *= 8;
424 #endif
426 time_diff = t - domain->last_seq_check;
428 /* see if we have to refetch the domain sequence number */
429 if (!force && (time_diff < cache_time)) {
430 DEBUG(10, ("refresh_sequence_number: %s time ok\n", domain->name));
431 goto done;
434 /* try to get the sequence number from the tdb cache first */
435 /* this will update the timestamp as well */
437 status = fetch_cache_seqnum( domain, t );
438 if ( NT_STATUS_IS_OK(status) )
439 goto done;
441 /* important! make sure that we know if this is a native
442 mode domain or not */
444 status = domain->backend->sequence_number(domain, &domain->sequence_number);
446 if (!NT_STATUS_IS_OK(status)) {
447 DEBUG(10,("refresh_sequence_number: failed with %s\n", nt_errstr(status)));
448 domain->sequence_number = DOM_SEQUENCE_NONE;
451 domain->last_status = status;
452 domain->last_seq_check = time(NULL);
454 /* save the new sequence number ni the cache */
455 store_cache_seqnum( domain );
457 done:
458 DEBUG(10, ("refresh_sequence_number: %s seq number is now %d\n",
459 domain->name, domain->sequence_number));
461 return;
465 decide if a cache entry has expired
467 static BOOL centry_expired(struct winbindd_domain *domain, const char *keystr, struct cache_entry *centry)
469 /* If we've been told to be offline - stay in that state... */
470 if (lp_winbind_offline_logon() && global_winbindd_offline_state) {
471 DEBUG(10,("centry_expired: Key %s for domain %s valid as winbindd is globally offline.\n",
472 keystr, domain->name ));
473 return False;
476 /* when the domain is offline and we havent checked in the last 30
477 * seconds if it has become online again, return the cached entry.
478 * This deals with transient offline states... */
480 if (!domain->online &&
481 !NT_STATUS_IS_OK(check_negative_conn_cache(domain->name, domain->dcname))) {
482 DEBUG(10,("centry_expired: Key %s for domain %s valid as domain is offline.\n",
483 keystr, domain->name ));
484 return False;
487 /* if the server is OK and our cache entry came from when it was down then
488 the entry is invalid */
489 if ((domain->sequence_number != DOM_SEQUENCE_NONE) &&
490 (centry->sequence_number == DOM_SEQUENCE_NONE)) {
491 DEBUG(10,("centry_expired: Key %s for domain %s invalid sequence.\n",
492 keystr, domain->name ));
493 return True;
496 /* if the server is down or the cache entry is not older than the
497 current sequence number then it is OK */
498 if (wcache_server_down(domain) ||
499 centry->sequence_number == domain->sequence_number) {
500 DEBUG(10,("centry_expired: Key %s for domain %s is good.\n",
501 keystr, domain->name ));
502 return False;
505 DEBUG(10,("centry_expired: Key %s for domain %s expired\n",
506 keystr, domain->name ));
508 /* it's expired */
509 return True;
512 static struct cache_entry *wcache_fetch_raw(char *kstr)
514 TDB_DATA data;
515 struct cache_entry *centry;
516 TDB_DATA key;
518 key.dptr = kstr;
519 key.dsize = strlen(kstr);
520 data = tdb_fetch(wcache->tdb, key);
521 if (!data.dptr) {
522 /* a cache miss */
523 return NULL;
526 centry = SMB_XMALLOC_P(struct cache_entry);
527 centry->data = (unsigned char *)data.dptr;
528 centry->len = data.dsize;
529 centry->ofs = 0;
531 if (centry->len < 8) {
532 /* huh? corrupt cache? */
533 DEBUG(10,("wcache_fetch_raw: Corrupt cache for key %s (len < 8) ?\n", kstr));
534 centry_free(centry);
535 return NULL;
538 centry->status = NT_STATUS(centry_uint32(centry));
539 centry->sequence_number = centry_uint32(centry);
541 return centry;
545 fetch an entry from the cache, with a varargs key. auto-fetch the sequence
546 number and return status
548 static struct cache_entry *wcache_fetch(struct winbind_cache *cache,
549 struct winbindd_domain *domain,
550 const char *format, ...) PRINTF_ATTRIBUTE(3,4);
551 static struct cache_entry *wcache_fetch(struct winbind_cache *cache,
552 struct winbindd_domain *domain,
553 const char *format, ...)
555 va_list ap;
556 char *kstr;
557 struct cache_entry *centry;
559 extern BOOL opt_nocache;
561 if (opt_nocache) {
562 return NULL;
565 refresh_sequence_number(domain, False);
567 va_start(ap, format);
568 smb_xvasprintf(&kstr, format, ap);
569 va_end(ap);
571 centry = wcache_fetch_raw(kstr);
572 if (centry == NULL) {
573 free(kstr);
574 return NULL;
577 if (centry_expired(domain, kstr, centry)) {
579 DEBUG(10,("wcache_fetch: entry %s expired for domain %s\n",
580 kstr, domain->name ));
582 centry_free(centry);
583 free(kstr);
584 return NULL;
587 DEBUG(10,("wcache_fetch: returning entry %s for domain %s\n",
588 kstr, domain->name ));
590 free(kstr);
591 return centry;
595 make sure we have at least len bytes available in a centry
597 static void centry_expand(struct cache_entry *centry, uint32 len)
599 if (centry->len - centry->ofs >= len)
600 return;
601 centry->len *= 2;
602 centry->data = SMB_REALLOC(centry->data, centry->len);
603 if (!centry->data) {
604 DEBUG(0,("out of memory: needed %d bytes in centry_expand\n", centry->len));
605 smb_panic("out of memory in centry_expand");
610 push a uint32 into a centry
612 static void centry_put_uint32(struct cache_entry *centry, uint32 v)
614 centry_expand(centry, 4);
615 SIVAL(centry->data, centry->ofs, v);
616 centry->ofs += 4;
620 push a uint16 into a centry
622 static void centry_put_uint16(struct cache_entry *centry, uint16 v)
624 centry_expand(centry, 2);
625 SIVAL(centry->data, centry->ofs, v);
626 centry->ofs += 2;
630 push a uint8 into a centry
632 static void centry_put_uint8(struct cache_entry *centry, uint8 v)
634 centry_expand(centry, 1);
635 SCVAL(centry->data, centry->ofs, v);
636 centry->ofs += 1;
640 push a string into a centry
642 static void centry_put_string(struct cache_entry *centry, const char *s)
644 int len;
646 if (!s) {
647 /* null strings are marked as len 0xFFFF */
648 centry_put_uint8(centry, 0xFF);
649 return;
652 len = strlen(s);
653 /* can't handle more than 254 char strings. Truncating is probably best */
654 if (len > 254) {
655 DEBUG(10,("centry_put_string: truncating len (%d) to: 254\n", len));
656 len = 254;
658 centry_put_uint8(centry, len);
659 centry_expand(centry, len);
660 memcpy(centry->data + centry->ofs, s, len);
661 centry->ofs += len;
665 push a 16 byte hash into a centry - treat as 16 byte string.
667 static void centry_put_hash16(struct cache_entry *centry, const uint8 val[16])
669 centry_put_uint8(centry, 16);
670 centry_expand(centry, 16);
671 memcpy(centry->data + centry->ofs, val, 16);
672 centry->ofs += 16;
675 static void centry_put_sid(struct cache_entry *centry, const DOM_SID *sid)
677 fstring sid_string;
678 centry_put_string(centry, sid_to_string(sid_string, sid));
682 push a NTTIME into a centry
684 static void centry_put_nttime(struct cache_entry *centry, NTTIME nt)
686 centry_expand(centry, 8);
687 SIVAL(centry->data, centry->ofs, nt.low);
688 centry->ofs += 4;
689 SIVAL(centry->data, centry->ofs, nt.high);
690 centry->ofs += 4;
694 push a time_t into a centry
696 static void centry_put_time(struct cache_entry *centry, time_t t)
698 centry_expand(centry, sizeof(time_t));
699 SIVAL(centry->data, centry->ofs, t); /* FIXME: is this correct ?? */
700 centry->ofs += sizeof(time_t);
704 start a centry for output. When finished, call centry_end()
706 struct cache_entry *centry_start(struct winbindd_domain *domain, NTSTATUS status)
708 struct cache_entry *centry;
710 if (!wcache->tdb)
711 return NULL;
713 centry = SMB_XMALLOC_P(struct cache_entry);
715 centry->len = 8192; /* reasonable default */
716 centry->data = SMB_XMALLOC_ARRAY(uint8, centry->len);
717 centry->ofs = 0;
718 centry->sequence_number = domain->sequence_number;
719 centry_put_uint32(centry, NT_STATUS_V(status));
720 centry_put_uint32(centry, centry->sequence_number);
721 return centry;
725 finish a centry and write it to the tdb
727 static void centry_end(struct cache_entry *centry, const char *format, ...) PRINTF_ATTRIBUTE(2,3);
728 static void centry_end(struct cache_entry *centry, const char *format, ...)
730 va_list ap;
731 char *kstr;
732 TDB_DATA key, data;
734 va_start(ap, format);
735 smb_xvasprintf(&kstr, format, ap);
736 va_end(ap);
738 key.dptr = kstr;
739 key.dsize = strlen(kstr);
740 data.dptr = (char *)centry->data;
741 data.dsize = centry->ofs;
743 tdb_store(wcache->tdb, key, data, TDB_REPLACE);
744 free(kstr);
747 static void wcache_save_name_to_sid(struct winbindd_domain *domain,
748 NTSTATUS status, const char *domain_name,
749 const char *name, const DOM_SID *sid,
750 enum SID_NAME_USE type)
752 struct cache_entry *centry;
753 fstring uname;
755 centry = centry_start(domain, status);
756 if (!centry)
757 return;
758 centry_put_uint32(centry, type);
759 centry_put_sid(centry, sid);
760 fstrcpy(uname, name);
761 strupper_m(uname);
762 centry_end(centry, "NS/%s/%s", domain_name, uname);
763 DEBUG(10,("wcache_save_name_to_sid: %s\\%s -> %s\n", domain_name, uname,
764 sid_string_static(sid)));
765 centry_free(centry);
768 static void wcache_save_sid_to_name(struct winbindd_domain *domain, NTSTATUS status,
769 const DOM_SID *sid, const char *domain_name, const char *name, enum SID_NAME_USE type)
771 struct cache_entry *centry;
772 fstring sid_string;
774 if (is_null_sid(sid)) {
775 return;
778 centry = centry_start(domain, status);
779 if (!centry)
780 return;
781 if (NT_STATUS_IS_OK(status)) {
782 centry_put_uint32(centry, type);
783 centry_put_string(centry, domain_name);
784 centry_put_string(centry, name);
786 centry_end(centry, "SN/%s", sid_to_string(sid_string, sid));
787 DEBUG(10,("wcache_save_sid_to_name: %s -> %s\n", sid_string, name));
788 centry_free(centry);
792 static void wcache_save_user(struct winbindd_domain *domain, NTSTATUS status, WINBIND_USERINFO *info)
794 struct cache_entry *centry;
795 fstring sid_string;
797 if (is_null_sid(&info->user_sid)) {
798 return;
801 centry = centry_start(domain, status);
802 if (!centry)
803 return;
804 centry_put_string(centry, info->acct_name);
805 centry_put_string(centry, info->full_name);
806 centry_put_string(centry, info->homedir);
807 centry_put_string(centry, info->shell);
808 centry_put_sid(centry, &info->user_sid);
809 centry_put_sid(centry, &info->group_sid);
810 centry_end(centry, "U/%s", sid_to_string(sid_string, &info->user_sid));
811 DEBUG(10,("wcache_save_user: %s (acct_name %s)\n", sid_string, info->acct_name));
812 centry_free(centry);
815 static void wcache_save_lockout_policy(struct winbindd_domain *domain, NTSTATUS status, SAM_UNK_INFO_12 *lockout_policy)
817 struct cache_entry *centry;
819 centry = centry_start(domain, status);
820 if (!centry)
821 return;
823 centry_put_nttime(centry, lockout_policy->duration);
824 centry_put_nttime(centry, lockout_policy->reset_count);
825 centry_put_uint16(centry, lockout_policy->bad_attempt_lockout);
827 centry_end(centry, "LOC_POL/%s", domain->name);
829 DEBUG(10,("wcache_save_lockout_policy: %s\n", domain->name));
831 centry_free(centry);
834 static void wcache_save_password_policy(struct winbindd_domain *domain, NTSTATUS status, SAM_UNK_INFO_1 *policy)
836 struct cache_entry *centry;
838 centry = centry_start(domain, status);
839 if (!centry)
840 return;
842 centry_put_uint16(centry, policy->min_length_password);
843 centry_put_uint16(centry, policy->password_history);
844 centry_put_uint32(centry, policy->password_properties);
845 centry_put_nttime(centry, policy->expire);
846 centry_put_nttime(centry, policy->min_passwordage);
848 centry_end(centry, "PWD_POL/%s", domain->name);
850 DEBUG(10,("wcache_save_password_policy: %s\n", domain->name));
852 centry_free(centry);
855 NTSTATUS wcache_cached_creds_exist(struct winbindd_domain *domain, const DOM_SID *sid)
857 struct winbind_cache *cache = get_cache(domain);
858 TDB_DATA data;
859 fstring key_str;
860 uint32 rid;
862 if (!cache->tdb) {
863 return NT_STATUS_INTERNAL_DB_ERROR;
866 if (is_null_sid(sid)) {
867 return NT_STATUS_INVALID_SID;
870 if (!(sid_peek_rid(sid, &rid)) || (rid == 0)) {
871 return NT_STATUS_INVALID_SID;
874 fstr_sprintf(key_str, "CRED/%s", sid_string_static(sid));
876 data = tdb_fetch(cache->tdb, make_tdb_data(key_str, strlen(key_str)));
877 if (!data.dptr) {
878 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
881 SAFE_FREE(data.dptr);
882 return NT_STATUS_OK;
885 /* Lookup creds for a SID */
886 NTSTATUS wcache_get_creds(struct winbindd_domain *domain,
887 TALLOC_CTX *mem_ctx,
888 const DOM_SID *sid,
889 const uint8 **cached_nt_pass)
891 struct winbind_cache *cache = get_cache(domain);
892 struct cache_entry *centry = NULL;
893 NTSTATUS status;
894 time_t t;
895 uint32 rid;
897 if (!cache->tdb) {
898 return NT_STATUS_INTERNAL_DB_ERROR;
901 if (is_null_sid(sid)) {
902 return NT_STATUS_INVALID_SID;
905 if (!(sid_peek_rid(sid, &rid)) || (rid == 0)) {
906 return NT_STATUS_INVALID_SID;
909 centry = wcache_fetch(cache, domain, "CRED/%s", sid_string_static(sid));
911 if (!centry) {
912 DEBUG(10,("wcache_get_creds: entry for [CRED/%s] not found\n",
913 sid_string_static(sid)));
914 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
917 t = centry_time(centry);
918 *cached_nt_pass = (const uint8 *)centry_hash16(centry, mem_ctx);
920 #if DEBUG_PASSWORD
921 dump_data(100, (const char *)cached_nt_pass, NT_HASH_LEN);
922 #endif
923 status = centry->status;
925 DEBUG(10,("wcache_get_creds: [Cached] - cached creds for user %s status: %s\n",
926 sid_string_static(sid), nt_errstr(status) ));
928 centry_free(centry);
929 return status;
932 NTSTATUS wcache_save_creds(struct winbindd_domain *domain,
933 TALLOC_CTX *mem_ctx,
934 const DOM_SID *sid,
935 const uint8 nt_pass[NT_HASH_LEN])
937 struct cache_entry *centry;
938 fstring sid_string;
939 uint32 rid;
941 if (is_null_sid(sid)) {
942 return NT_STATUS_INVALID_SID;
945 if (!(sid_peek_rid(sid, &rid)) || (rid == 0)) {
946 return NT_STATUS_INVALID_SID;
949 centry = centry_start(domain, NT_STATUS_OK);
950 if (!centry) {
951 return NT_STATUS_INTERNAL_DB_ERROR;
954 #if DEBUG_PASSWORD
955 dump_data(100, (const char *)nt_pass, NT_HASH_LEN);
956 #endif
958 centry_put_time(centry, time(NULL));
959 centry_put_hash16(centry, nt_pass);
960 centry_end(centry, "CRED/%s", sid_to_string(sid_string, sid));
962 DEBUG(10,("wcache_save_creds: %s\n", sid_string));
964 centry_free(centry);
966 return NT_STATUS_OK;
970 /* Query display info. This is the basic user list fn */
971 static NTSTATUS query_user_list(struct winbindd_domain *domain,
972 TALLOC_CTX *mem_ctx,
973 uint32 *num_entries,
974 WINBIND_USERINFO **info)
976 struct winbind_cache *cache = get_cache(domain);
977 struct cache_entry *centry = NULL;
978 NTSTATUS status;
979 unsigned int i, retry;
981 if (!cache->tdb)
982 goto do_query;
984 centry = wcache_fetch(cache, domain, "UL/%s", domain->name);
985 if (!centry)
986 goto do_query;
988 *num_entries = centry_uint32(centry);
990 if (*num_entries == 0)
991 goto do_cached;
993 (*info) = TALLOC_ARRAY(mem_ctx, WINBIND_USERINFO, *num_entries);
994 if (! (*info))
995 smb_panic("query_user_list out of memory");
996 for (i=0; i<(*num_entries); i++) {
997 (*info)[i].acct_name = centry_string(centry, mem_ctx);
998 (*info)[i].full_name = centry_string(centry, mem_ctx);
999 (*info)[i].homedir = centry_string(centry, mem_ctx);
1000 (*info)[i].shell = centry_string(centry, mem_ctx);
1001 centry_sid(centry, mem_ctx, &(*info)[i].user_sid);
1002 centry_sid(centry, mem_ctx, &(*info)[i].group_sid);
1005 do_cached:
1006 status = centry->status;
1008 DEBUG(10,("query_user_list: [Cached] - cached list for domain %s status: %s\n",
1009 domain->name, nt_errstr(status) ));
1011 centry_free(centry);
1012 return status;
1014 do_query:
1015 *num_entries = 0;
1016 *info = NULL;
1018 /* Return status value returned by seq number check */
1020 if (!NT_STATUS_IS_OK(domain->last_status))
1021 return domain->last_status;
1023 /* Put the query_user_list() in a retry loop. There appears to be
1024 * some bug either with Windows 2000 or Samba's handling of large
1025 * rpc replies. This manifests itself as sudden disconnection
1026 * at a random point in the enumeration of a large (60k) user list.
1027 * The retry loop simply tries the operation again. )-: It's not
1028 * pretty but an acceptable workaround until we work out what the
1029 * real problem is. */
1031 retry = 0;
1032 do {
1034 DEBUG(10,("query_user_list: [Cached] - doing backend query for list for domain %s\n",
1035 domain->name ));
1037 status = domain->backend->query_user_list(domain, mem_ctx, num_entries, info);
1038 if (!NT_STATUS_IS_OK(status))
1039 DEBUG(3, ("query_user_list: returned 0x%08x, "
1040 "retrying\n", NT_STATUS_V(status)));
1041 if (NT_STATUS_EQUAL(status, NT_STATUS_UNSUCCESSFUL)) {
1042 DEBUG(3, ("query_user_list: flushing "
1043 "connection cache\n"));
1044 invalidate_cm_connection(&domain->conn);
1047 } while (NT_STATUS_V(status) == NT_STATUS_V(NT_STATUS_UNSUCCESSFUL) &&
1048 (retry++ < 5));
1050 /* and save it */
1051 refresh_sequence_number(domain, False);
1052 centry = centry_start(domain, status);
1053 if (!centry)
1054 goto skip_save;
1055 centry_put_uint32(centry, *num_entries);
1056 for (i=0; i<(*num_entries); i++) {
1057 centry_put_string(centry, (*info)[i].acct_name);
1058 centry_put_string(centry, (*info)[i].full_name);
1059 centry_put_string(centry, (*info)[i].homedir);
1060 centry_put_string(centry, (*info)[i].shell);
1061 centry_put_sid(centry, &(*info)[i].user_sid);
1062 centry_put_sid(centry, &(*info)[i].group_sid);
1063 if (domain->backend->consistent) {
1064 /* when the backend is consistent we can pre-prime some mappings */
1065 wcache_save_name_to_sid(domain, NT_STATUS_OK,
1066 domain->name,
1067 (*info)[i].acct_name,
1068 &(*info)[i].user_sid,
1069 SID_NAME_USER);
1070 wcache_save_sid_to_name(domain, NT_STATUS_OK,
1071 &(*info)[i].user_sid,
1072 domain->name,
1073 (*info)[i].acct_name,
1074 SID_NAME_USER);
1075 wcache_save_user(domain, NT_STATUS_OK, &(*info)[i]);
1078 centry_end(centry, "UL/%s", domain->name);
1079 centry_free(centry);
1081 skip_save:
1082 return status;
1085 /* list all domain groups */
1086 static NTSTATUS enum_dom_groups(struct winbindd_domain *domain,
1087 TALLOC_CTX *mem_ctx,
1088 uint32 *num_entries,
1089 struct acct_info **info)
1091 struct winbind_cache *cache = get_cache(domain);
1092 struct cache_entry *centry = NULL;
1093 NTSTATUS status;
1094 unsigned int i;
1096 if (!cache->tdb)
1097 goto do_query;
1099 centry = wcache_fetch(cache, domain, "GL/%s/domain", domain->name);
1100 if (!centry)
1101 goto do_query;
1103 *num_entries = centry_uint32(centry);
1105 if (*num_entries == 0)
1106 goto do_cached;
1108 (*info) = TALLOC_ARRAY(mem_ctx, struct acct_info, *num_entries);
1109 if (! (*info))
1110 smb_panic("enum_dom_groups out of memory");
1111 for (i=0; i<(*num_entries); i++) {
1112 fstrcpy((*info)[i].acct_name, centry_string(centry, mem_ctx));
1113 fstrcpy((*info)[i].acct_desc, centry_string(centry, mem_ctx));
1114 (*info)[i].rid = centry_uint32(centry);
1117 do_cached:
1118 status = centry->status;
1120 DEBUG(10,("enum_dom_groups: [Cached] - cached list for domain %s status: %s\n",
1121 domain->name, nt_errstr(status) ));
1123 centry_free(centry);
1124 return status;
1126 do_query:
1127 *num_entries = 0;
1128 *info = NULL;
1130 /* Return status value returned by seq number check */
1132 if (!NT_STATUS_IS_OK(domain->last_status))
1133 return domain->last_status;
1135 DEBUG(10,("enum_dom_groups: [Cached] - doing backend query for list for domain %s\n",
1136 domain->name ));
1138 status = domain->backend->enum_dom_groups(domain, mem_ctx, num_entries, info);
1140 /* and save it */
1141 refresh_sequence_number(domain, False);
1142 centry = centry_start(domain, status);
1143 if (!centry)
1144 goto skip_save;
1145 centry_put_uint32(centry, *num_entries);
1146 for (i=0; i<(*num_entries); i++) {
1147 centry_put_string(centry, (*info)[i].acct_name);
1148 centry_put_string(centry, (*info)[i].acct_desc);
1149 centry_put_uint32(centry, (*info)[i].rid);
1151 centry_end(centry, "GL/%s/domain", domain->name);
1152 centry_free(centry);
1154 skip_save:
1155 return status;
1158 /* list all domain groups */
1159 static NTSTATUS enum_local_groups(struct winbindd_domain *domain,
1160 TALLOC_CTX *mem_ctx,
1161 uint32 *num_entries,
1162 struct acct_info **info)
1164 struct winbind_cache *cache = get_cache(domain);
1165 struct cache_entry *centry = NULL;
1166 NTSTATUS status;
1167 unsigned int i;
1169 if (!cache->tdb)
1170 goto do_query;
1172 centry = wcache_fetch(cache, domain, "GL/%s/local", domain->name);
1173 if (!centry)
1174 goto do_query;
1176 *num_entries = centry_uint32(centry);
1178 if (*num_entries == 0)
1179 goto do_cached;
1181 (*info) = TALLOC_ARRAY(mem_ctx, struct acct_info, *num_entries);
1182 if (! (*info))
1183 smb_panic("enum_dom_groups out of memory");
1184 for (i=0; i<(*num_entries); i++) {
1185 fstrcpy((*info)[i].acct_name, centry_string(centry, mem_ctx));
1186 fstrcpy((*info)[i].acct_desc, centry_string(centry, mem_ctx));
1187 (*info)[i].rid = centry_uint32(centry);
1190 do_cached:
1192 /* If we are returning cached data and the domain controller
1193 is down then we don't know whether the data is up to date
1194 or not. Return NT_STATUS_MORE_PROCESSING_REQUIRED to
1195 indicate this. */
1197 if (wcache_server_down(domain)) {
1198 DEBUG(10, ("enum_local_groups: returning cached user list and server was down\n"));
1199 status = NT_STATUS_MORE_PROCESSING_REQUIRED;
1200 } else
1201 status = centry->status;
1203 DEBUG(10,("enum_local_groups: [Cached] - cached list for domain %s status: %s\n",
1204 domain->name, nt_errstr(status) ));
1206 centry_free(centry);
1207 return status;
1209 do_query:
1210 *num_entries = 0;
1211 *info = NULL;
1213 /* Return status value returned by seq number check */
1215 if (!NT_STATUS_IS_OK(domain->last_status))
1216 return domain->last_status;
1218 DEBUG(10,("enum_local_groups: [Cached] - doing backend query for list for domain %s\n",
1219 domain->name ));
1221 status = domain->backend->enum_local_groups(domain, mem_ctx, num_entries, info);
1223 /* and save it */
1224 refresh_sequence_number(domain, False);
1225 centry = centry_start(domain, status);
1226 if (!centry)
1227 goto skip_save;
1228 centry_put_uint32(centry, *num_entries);
1229 for (i=0; i<(*num_entries); i++) {
1230 centry_put_string(centry, (*info)[i].acct_name);
1231 centry_put_string(centry, (*info)[i].acct_desc);
1232 centry_put_uint32(centry, (*info)[i].rid);
1234 centry_end(centry, "GL/%s/local", domain->name);
1235 centry_free(centry);
1237 skip_save:
1238 return status;
1241 /* convert a single name to a sid in a domain */
1242 static NTSTATUS name_to_sid(struct winbindd_domain *domain,
1243 TALLOC_CTX *mem_ctx,
1244 const char *domain_name,
1245 const char *name,
1246 DOM_SID *sid,
1247 enum SID_NAME_USE *type)
1249 struct winbind_cache *cache = get_cache(domain);
1250 struct cache_entry *centry = NULL;
1251 NTSTATUS status;
1252 fstring uname;
1254 if (!cache->tdb)
1255 goto do_query;
1257 fstrcpy(uname, name);
1258 strupper_m(uname);
1259 centry = wcache_fetch(cache, domain, "NS/%s/%s", domain_name, uname);
1260 if (!centry)
1261 goto do_query;
1262 *type = (enum SID_NAME_USE)centry_uint32(centry);
1263 status = centry->status;
1264 if (NT_STATUS_IS_OK(status)) {
1265 centry_sid(centry, mem_ctx, sid);
1268 DEBUG(10,("name_to_sid: [Cached] - cached name for domain %s status: %s\n",
1269 domain->name, nt_errstr(status) ));
1271 centry_free(centry);
1272 return status;
1274 do_query:
1275 ZERO_STRUCTP(sid);
1277 /* If the seq number check indicated that there is a problem
1278 * with this DC, then return that status... except for
1279 * access_denied. This is special because the dc may be in
1280 * "restrict anonymous = 1" mode, in which case it will deny
1281 * most unauthenticated operations, but *will* allow the LSA
1282 * name-to-sid that we try as a fallback. */
1284 if (!(NT_STATUS_IS_OK(domain->last_status)
1285 || NT_STATUS_EQUAL(domain->last_status, NT_STATUS_ACCESS_DENIED)))
1286 return domain->last_status;
1288 DEBUG(10,("name_to_sid: [Cached] - doing backend query for name for domain %s\n",
1289 domain->name ));
1291 status = domain->backend->name_to_sid(domain, mem_ctx, domain_name, name, sid, type);
1293 /* and save it */
1294 refresh_sequence_number(domain, False);
1296 if (domain->online && !is_null_sid(sid)) {
1297 wcache_save_name_to_sid(domain, status, domain_name, name, sid, *type);
1300 if (NT_STATUS_IS_OK(status)) {
1301 strupper_m(CONST_DISCARD(char *,domain_name));
1302 strlower_m(CONST_DISCARD(char *,name));
1303 wcache_save_sid_to_name(domain, status, sid, domain_name, name, *type);
1306 return status;
1309 /* convert a sid to a user or group name. The sid is guaranteed to be in the domain
1310 given */
1311 static NTSTATUS sid_to_name(struct winbindd_domain *domain,
1312 TALLOC_CTX *mem_ctx,
1313 const DOM_SID *sid,
1314 char **domain_name,
1315 char **name,
1316 enum SID_NAME_USE *type)
1318 struct winbind_cache *cache = get_cache(domain);
1319 struct cache_entry *centry = NULL;
1320 NTSTATUS status;
1321 fstring sid_string;
1323 if (!cache->tdb)
1324 goto do_query;
1326 centry = wcache_fetch(cache, domain, "SN/%s", sid_to_string(sid_string, sid));
1327 if (!centry)
1328 goto do_query;
1329 if (NT_STATUS_IS_OK(centry->status)) {
1330 *type = (enum SID_NAME_USE)centry_uint32(centry);
1331 *domain_name = centry_string(centry, mem_ctx);
1332 *name = centry_string(centry, mem_ctx);
1334 status = centry->status;
1336 DEBUG(10,("sid_to_name: [Cached] - cached name for domain %s status: %s\n",
1337 domain->name, nt_errstr(status) ));
1339 centry_free(centry);
1340 return status;
1342 do_query:
1343 *name = NULL;
1344 *domain_name = NULL;
1346 /* If the seq number check indicated that there is a problem
1347 * with this DC, then return that status... except for
1348 * access_denied. This is special because the dc may be in
1349 * "restrict anonymous = 1" mode, in which case it will deny
1350 * most unauthenticated operations, but *will* allow the LSA
1351 * sid-to-name that we try as a fallback. */
1353 if (!(NT_STATUS_IS_OK(domain->last_status)
1354 || NT_STATUS_EQUAL(domain->last_status, NT_STATUS_ACCESS_DENIED)))
1355 return domain->last_status;
1357 DEBUG(10,("sid_to_name: [Cached] - doing backend query for name for domain %s\n",
1358 domain->name ));
1360 status = domain->backend->sid_to_name(domain, mem_ctx, sid, domain_name, name, type);
1362 /* and save it */
1363 refresh_sequence_number(domain, False);
1364 wcache_save_sid_to_name(domain, status, sid, *domain_name, *name, *type);
1366 /* We can't save the name to sid mapping here, as with sid history a
1367 * later name2sid would give the wrong sid. */
1369 return status;
1372 /* Lookup user information from a rid */
1373 static NTSTATUS query_user(struct winbindd_domain *domain,
1374 TALLOC_CTX *mem_ctx,
1375 const DOM_SID *user_sid,
1376 WINBIND_USERINFO *info)
1378 struct winbind_cache *cache = get_cache(domain);
1379 struct cache_entry *centry = NULL;
1380 NTSTATUS status;
1382 if (!cache->tdb)
1383 goto do_query;
1385 centry = wcache_fetch(cache, domain, "U/%s", sid_string_static(user_sid));
1387 /* If we have an access denied cache entry and a cached info3 in the
1388 samlogon cache then do a query. This will force the rpc back end
1389 to return the info3 data. */
1391 if (NT_STATUS_V(domain->last_status) == NT_STATUS_V(NT_STATUS_ACCESS_DENIED) &&
1392 netsamlogon_cache_have(user_sid)) {
1393 DEBUG(10, ("query_user: cached access denied and have cached info3\n"));
1394 domain->last_status = NT_STATUS_OK;
1395 centry_free(centry);
1396 goto do_query;
1399 if (!centry)
1400 goto do_query;
1402 info->acct_name = centry_string(centry, mem_ctx);
1403 info->full_name = centry_string(centry, mem_ctx);
1404 info->homedir = centry_string(centry, mem_ctx);
1405 info->shell = centry_string(centry, mem_ctx);
1406 centry_sid(centry, mem_ctx, &info->user_sid);
1407 centry_sid(centry, mem_ctx, &info->group_sid);
1408 status = centry->status;
1410 DEBUG(10,("query_user: [Cached] - cached info for domain %s status: %s\n",
1411 domain->name, nt_errstr(status) ));
1413 centry_free(centry);
1414 return status;
1416 do_query:
1417 ZERO_STRUCTP(info);
1419 /* Return status value returned by seq number check */
1421 if (!NT_STATUS_IS_OK(domain->last_status))
1422 return domain->last_status;
1424 DEBUG(10,("sid_to_name: [Cached] - doing backend query for info for domain %s\n",
1425 domain->name ));
1427 status = domain->backend->query_user(domain, mem_ctx, user_sid, info);
1429 /* and save it */
1430 refresh_sequence_number(domain, False);
1431 wcache_save_user(domain, status, info);
1433 return status;
1437 /* Lookup groups a user is a member of. */
1438 static NTSTATUS lookup_usergroups(struct winbindd_domain *domain,
1439 TALLOC_CTX *mem_ctx,
1440 const DOM_SID *user_sid,
1441 uint32 *num_groups, DOM_SID **user_gids)
1443 struct winbind_cache *cache = get_cache(domain);
1444 struct cache_entry *centry = NULL;
1445 NTSTATUS status;
1446 unsigned int i;
1447 fstring sid_string;
1449 if (!cache->tdb)
1450 goto do_query;
1452 centry = wcache_fetch(cache, domain, "UG/%s", sid_to_string(sid_string, user_sid));
1454 /* If we have an access denied cache entry and a cached info3 in the
1455 samlogon cache then do a query. This will force the rpc back end
1456 to return the info3 data. */
1458 if (NT_STATUS_V(domain->last_status) == NT_STATUS_V(NT_STATUS_ACCESS_DENIED) &&
1459 netsamlogon_cache_have(user_sid)) {
1460 DEBUG(10, ("lookup_usergroups: cached access denied and have cached info3\n"));
1461 domain->last_status = NT_STATUS_OK;
1462 centry_free(centry);
1463 goto do_query;
1466 if (!centry)
1467 goto do_query;
1469 *num_groups = centry_uint32(centry);
1471 if (*num_groups == 0)
1472 goto do_cached;
1474 (*user_gids) = TALLOC_ARRAY(mem_ctx, DOM_SID, *num_groups);
1475 if (! (*user_gids))
1476 smb_panic("lookup_usergroups out of memory");
1477 for (i=0; i<(*num_groups); i++) {
1478 centry_sid(centry, mem_ctx, &(*user_gids)[i]);
1481 do_cached:
1482 status = centry->status;
1484 DEBUG(10,("lookup_usergroups: [Cached] - cached info for domain %s status: %s\n",
1485 domain->name, nt_errstr(status) ));
1487 centry_free(centry);
1488 return status;
1490 do_query:
1491 (*num_groups) = 0;
1492 (*user_gids) = NULL;
1494 /* Return status value returned by seq number check */
1496 if (!NT_STATUS_IS_OK(domain->last_status))
1497 return domain->last_status;
1499 DEBUG(10,("lookup_usergroups: [Cached] - doing backend query for info for domain %s\n",
1500 domain->name ));
1502 status = domain->backend->lookup_usergroups(domain, mem_ctx, user_sid, num_groups, user_gids);
1504 /* and save it */
1505 refresh_sequence_number(domain, False);
1506 centry = centry_start(domain, status);
1507 if (!centry)
1508 goto skip_save;
1509 centry_put_uint32(centry, *num_groups);
1510 for (i=0; i<(*num_groups); i++) {
1511 centry_put_sid(centry, &(*user_gids)[i]);
1513 centry_end(centry, "UG/%s", sid_to_string(sid_string, user_sid));
1514 centry_free(centry);
1516 skip_save:
1517 return status;
1520 static NTSTATUS lookup_useraliases(struct winbindd_domain *domain,
1521 TALLOC_CTX *mem_ctx,
1522 uint32 num_sids, const DOM_SID *sids,
1523 uint32 *num_aliases, uint32 **alias_rids)
1525 struct winbind_cache *cache = get_cache(domain);
1526 struct cache_entry *centry = NULL;
1527 NTSTATUS status;
1528 char *sidlist = talloc_strdup(mem_ctx, "");
1529 int i;
1531 if (!cache->tdb)
1532 goto do_query;
1534 if (num_sids == 0) {
1535 *num_aliases = 0;
1536 *alias_rids = NULL;
1537 return NT_STATUS_OK;
1540 /* We need to cache indexed by the whole list of SIDs, the aliases
1541 * resulting might come from any of the SIDs. */
1543 for (i=0; i<num_sids; i++) {
1544 sidlist = talloc_asprintf(mem_ctx, "%s/%s", sidlist,
1545 sid_string_static(&sids[i]));
1546 if (sidlist == NULL)
1547 return NT_STATUS_NO_MEMORY;
1550 centry = wcache_fetch(cache, domain, "UA%s", sidlist);
1552 if (!centry)
1553 goto do_query;
1555 *num_aliases = centry_uint32(centry);
1556 *alias_rids = NULL;
1558 (*alias_rids) = TALLOC_ARRAY(mem_ctx, uint32, *num_aliases);
1560 if ((*num_aliases != 0) && ((*alias_rids) == NULL)) {
1561 centry_free(centry);
1562 return NT_STATUS_NO_MEMORY;
1565 for (i=0; i<(*num_aliases); i++)
1566 (*alias_rids)[i] = centry_uint32(centry);
1568 status = centry->status;
1570 DEBUG(10,("lookup_useraliases: [Cached] - cached info for domain: %s "
1571 "status %s\n", domain->name, nt_errstr(status)));
1573 centry_free(centry);
1574 return status;
1576 do_query:
1577 (*num_aliases) = 0;
1578 (*alias_rids) = NULL;
1580 if (!NT_STATUS_IS_OK(domain->last_status))
1581 return domain->last_status;
1583 DEBUG(10,("lookup_usergroups: [Cached] - doing backend query for info "
1584 "for domain %s\n", domain->name ));
1586 status = domain->backend->lookup_useraliases(domain, mem_ctx,
1587 num_sids, sids,
1588 num_aliases, alias_rids);
1590 /* and save it */
1591 refresh_sequence_number(domain, False);
1592 centry = centry_start(domain, status);
1593 if (!centry)
1594 goto skip_save;
1595 centry_put_uint32(centry, *num_aliases);
1596 for (i=0; i<(*num_aliases); i++)
1597 centry_put_uint32(centry, (*alias_rids)[i]);
1598 centry_end(centry, "UA%s", sidlist);
1599 centry_free(centry);
1601 skip_save:
1602 return status;
1606 static NTSTATUS lookup_groupmem(struct winbindd_domain *domain,
1607 TALLOC_CTX *mem_ctx,
1608 const DOM_SID *group_sid, uint32 *num_names,
1609 DOM_SID **sid_mem, char ***names,
1610 uint32 **name_types)
1612 struct winbind_cache *cache = get_cache(domain);
1613 struct cache_entry *centry = NULL;
1614 NTSTATUS status;
1615 unsigned int i;
1616 fstring sid_string;
1618 if (!cache->tdb)
1619 goto do_query;
1621 centry = wcache_fetch(cache, domain, "GM/%s", sid_to_string(sid_string, group_sid));
1622 if (!centry)
1623 goto do_query;
1625 *num_names = centry_uint32(centry);
1627 if (*num_names == 0)
1628 goto do_cached;
1630 (*sid_mem) = TALLOC_ARRAY(mem_ctx, DOM_SID, *num_names);
1631 (*names) = TALLOC_ARRAY(mem_ctx, char *, *num_names);
1632 (*name_types) = TALLOC_ARRAY(mem_ctx, uint32, *num_names);
1634 if (! (*sid_mem) || ! (*names) || ! (*name_types)) {
1635 smb_panic("lookup_groupmem out of memory");
1638 for (i=0; i<(*num_names); i++) {
1639 centry_sid(centry, mem_ctx, &(*sid_mem)[i]);
1640 (*names)[i] = centry_string(centry, mem_ctx);
1641 (*name_types)[i] = centry_uint32(centry);
1644 do_cached:
1645 status = centry->status;
1647 DEBUG(10,("lookup_groupmem: [Cached] - cached info for domain %s status: %s\n",
1648 domain->name, nt_errstr(status)));
1650 centry_free(centry);
1651 return status;
1653 do_query:
1654 (*num_names) = 0;
1655 (*sid_mem) = NULL;
1656 (*names) = NULL;
1657 (*name_types) = NULL;
1659 /* Return status value returned by seq number check */
1661 if (!NT_STATUS_IS_OK(domain->last_status))
1662 return domain->last_status;
1664 DEBUG(10,("lookup_groupmem: [Cached] - doing backend query for info for domain %s\n",
1665 domain->name ));
1667 status = domain->backend->lookup_groupmem(domain, mem_ctx, group_sid, num_names,
1668 sid_mem, names, name_types);
1670 /* and save it */
1671 refresh_sequence_number(domain, False);
1672 centry = centry_start(domain, status);
1673 if (!centry)
1674 goto skip_save;
1675 centry_put_uint32(centry, *num_names);
1676 for (i=0; i<(*num_names); i++) {
1677 centry_put_sid(centry, &(*sid_mem)[i]);
1678 centry_put_string(centry, (*names)[i]);
1679 centry_put_uint32(centry, (*name_types)[i]);
1681 centry_end(centry, "GM/%s", sid_to_string(sid_string, group_sid));
1682 centry_free(centry);
1684 skip_save:
1685 return status;
1688 /* find the sequence number for a domain */
1689 static NTSTATUS sequence_number(struct winbindd_domain *domain, uint32 *seq)
1691 refresh_sequence_number(domain, False);
1693 *seq = domain->sequence_number;
1695 return NT_STATUS_OK;
1698 /* enumerate trusted domains
1699 * (we need to have the list of trustdoms in the cache when we go offline) -
1700 * Guenther */
1701 static NTSTATUS trusted_domains(struct winbindd_domain *domain,
1702 TALLOC_CTX *mem_ctx,
1703 uint32 *num_domains,
1704 char ***names,
1705 char ***alt_names,
1706 DOM_SID **dom_sids)
1708 struct winbind_cache *cache = get_cache(domain);
1709 struct cache_entry *centry = NULL;
1710 NTSTATUS status;
1711 int i;
1713 if (!cache->tdb)
1714 goto do_query;
1716 centry = wcache_fetch(cache, domain, "TRUSTDOMS/%s", domain->name);
1718 if (!centry) {
1719 goto do_query;
1722 *num_domains = centry_uint32(centry);
1724 (*names) = TALLOC_ARRAY(mem_ctx, char *, *num_domains);
1725 (*alt_names) = TALLOC_ARRAY(mem_ctx, char *, *num_domains);
1726 (*dom_sids) = TALLOC_ARRAY(mem_ctx, DOM_SID, *num_domains);
1728 if (! (*dom_sids) || ! (*names) || ! (*alt_names)) {
1729 smb_panic("trusted_domains out of memory");
1732 for (i=0; i<(*num_domains); i++) {
1733 (*names)[i] = centry_string(centry, mem_ctx);
1734 (*alt_names)[i] = centry_string(centry, mem_ctx);
1735 centry_sid(centry, mem_ctx, &(*dom_sids)[i]);
1738 status = centry->status;
1740 DEBUG(10,("trusted_domains: [Cached] - cached info for domain %s (%d trusts) status: %s\n",
1741 domain->name, *num_domains, nt_errstr(status) ));
1743 centry_free(centry);
1744 return status;
1746 do_query:
1747 (*num_domains) = 0;
1748 (*dom_sids) = NULL;
1749 (*names) = NULL;
1750 (*alt_names) = NULL;
1752 /* Return status value returned by seq number check */
1754 if (!NT_STATUS_IS_OK(domain->last_status))
1755 return domain->last_status;
1757 DEBUG(10,("trusted_domains: [Cached] - doing backend query for info for domain %s\n",
1758 domain->name ));
1760 status = domain->backend->trusted_domains(domain, mem_ctx, num_domains,
1761 names, alt_names, dom_sids);
1763 /* no trusts gives NT_STATUS_NO_MORE_ENTRIES resetting to NT_STATUS_OK
1764 * so that the generic centry handling still applies correctly -
1765 * Guenther*/
1767 if (!NT_STATUS_IS_ERR(status)) {
1768 status = NT_STATUS_OK;
1771 /* and save it */
1772 refresh_sequence_number(domain, False);
1774 centry = centry_start(domain, status);
1775 if (!centry)
1776 goto skip_save;
1778 centry_put_uint32(centry, *num_domains);
1780 for (i=0; i<(*num_domains); i++) {
1781 centry_put_string(centry, (*names)[i]);
1782 centry_put_string(centry, (*alt_names)[i]);
1783 centry_put_sid(centry, &(*dom_sids)[i]);
1786 centry_end(centry, "TRUSTDOMS/%s", domain->name);
1788 centry_free(centry);
1790 skip_save:
1791 return status;
1794 /* get lockout policy */
1795 static NTSTATUS lockout_policy(struct winbindd_domain *domain,
1796 TALLOC_CTX *mem_ctx,
1797 SAM_UNK_INFO_12 *policy){
1798 struct winbind_cache *cache = get_cache(domain);
1799 struct cache_entry *centry = NULL;
1800 NTSTATUS status;
1802 if (!cache->tdb)
1803 goto do_query;
1805 centry = wcache_fetch(cache, domain, "LOC_POL/%s", domain->name);
1807 if (!centry)
1808 goto do_query;
1810 policy->duration = centry_nttime(centry);
1811 policy->reset_count = centry_nttime(centry);
1812 policy->bad_attempt_lockout = centry_uint16(centry);
1814 status = centry->status;
1816 DEBUG(10,("lockout_policy: [Cached] - cached info for domain %s status: %s\n",
1817 domain->name, nt_errstr(status) ));
1819 centry_free(centry);
1820 return status;
1822 do_query:
1823 ZERO_STRUCTP(policy);
1825 /* Return status value returned by seq number check */
1827 if (!NT_STATUS_IS_OK(domain->last_status))
1828 return domain->last_status;
1830 DEBUG(10,("lockout_policy: [Cached] - doing backend query for info for domain %s\n",
1831 domain->name ));
1833 status = domain->backend->lockout_policy(domain, mem_ctx, policy);
1835 /* and save it */
1836 refresh_sequence_number(domain, False);
1837 wcache_save_lockout_policy(domain, status, policy);
1839 return status;
1842 /* get password policy */
1843 static NTSTATUS password_policy(struct winbindd_domain *domain,
1844 TALLOC_CTX *mem_ctx,
1845 SAM_UNK_INFO_1 *policy)
1847 struct winbind_cache *cache = get_cache(domain);
1848 struct cache_entry *centry = NULL;
1849 NTSTATUS status;
1851 if (!cache->tdb)
1852 goto do_query;
1854 centry = wcache_fetch(cache, domain, "PWD_POL/%s", domain->name);
1856 if (!centry)
1857 goto do_query;
1859 policy->min_length_password = centry_uint16(centry);
1860 policy->password_history = centry_uint16(centry);
1861 policy->password_properties = centry_uint32(centry);
1862 policy->expire = centry_nttime(centry);
1863 policy->min_passwordage = centry_nttime(centry);
1865 status = centry->status;
1867 DEBUG(10,("lockout_policy: [Cached] - cached info for domain %s status: %s\n",
1868 domain->name, nt_errstr(status) ));
1870 centry_free(centry);
1871 return status;
1873 do_query:
1874 ZERO_STRUCTP(policy);
1876 /* Return status value returned by seq number check */
1878 if (!NT_STATUS_IS_OK(domain->last_status))
1879 return domain->last_status;
1881 DEBUG(10,("password_policy: [Cached] - doing backend query for info for domain %s\n",
1882 domain->name ));
1884 status = domain->backend->password_policy(domain, mem_ctx, policy);
1886 /* and save it */
1887 refresh_sequence_number(domain, False);
1888 wcache_save_password_policy(domain, status, policy);
1890 return status;
1894 /* Invalidate cached user and group lists coherently */
1896 static int traverse_fn(TDB_CONTEXT *the_tdb, TDB_DATA kbuf, TDB_DATA dbuf,
1897 void *state)
1899 if (strncmp(kbuf.dptr, "UL/", 3) == 0 ||
1900 strncmp(kbuf.dptr, "GL/", 3) == 0)
1901 tdb_delete(the_tdb, kbuf);
1903 return 0;
1906 /* Invalidate the getpwnam and getgroups entries for a winbindd domain */
1908 void wcache_invalidate_samlogon(struct winbindd_domain *domain,
1909 NET_USER_INFO_3 *info3)
1911 struct winbind_cache *cache;
1913 if (!domain)
1914 return;
1916 cache = get_cache(domain);
1917 netsamlogon_clear_cached_user(cache->tdb, info3);
1920 void wcache_invalidate_cache(void)
1922 struct winbindd_domain *domain;
1924 for (domain = domain_list(); domain; domain = domain->next) {
1925 struct winbind_cache *cache = get_cache(domain);
1927 DEBUG(10, ("wcache_invalidate_cache: invalidating cache "
1928 "entries for %s\n", domain->name));
1929 if (cache)
1930 tdb_traverse(cache->tdb, traverse_fn, NULL);
1934 static BOOL init_wcache(void)
1936 if (wcache == NULL) {
1937 wcache = SMB_XMALLOC_P(struct winbind_cache);
1938 ZERO_STRUCTP(wcache);
1941 if (wcache->tdb != NULL)
1942 return True;
1944 /* when working offline we must not clear the cache on restart */
1945 wcache->tdb = tdb_open_log(lock_path("winbindd_cache.tdb"),
1946 WINBINDD_CACHE_TDB_DEFAULT_HASH_SIZE,
1947 lp_winbind_offline_logon() ? TDB_DEFAULT : (TDB_DEFAULT | TDB_CLEAR_IF_FIRST),
1948 O_RDWR|O_CREAT, 0600);
1950 if (wcache->tdb == NULL) {
1951 DEBUG(0,("Failed to open winbindd_cache.tdb!\n"));
1952 return False;
1955 return True;
1958 void cache_store_response(pid_t pid, struct winbindd_response *response)
1960 fstring key_str;
1962 if (!init_wcache())
1963 return;
1965 DEBUG(10, ("Storing response for pid %d, len %d\n",
1966 pid, response->length));
1968 fstr_sprintf(key_str, "DR/%d", pid);
1969 if (tdb_store(wcache->tdb, string_tdb_data(key_str),
1970 make_tdb_data((void *)response, sizeof(*response)),
1971 TDB_REPLACE) == -1)
1972 return;
1974 if (response->length == sizeof(*response))
1975 return;
1977 /* There's extra data */
1979 DEBUG(10, ("Storing extra data: len=%d\n",
1980 (int)(response->length - sizeof(*response))));
1982 fstr_sprintf(key_str, "DE/%d", pid);
1983 if (tdb_store(wcache->tdb, string_tdb_data(key_str),
1984 make_tdb_data(response->extra_data.data,
1985 response->length - sizeof(*response)),
1986 TDB_REPLACE) == 0)
1987 return;
1989 /* We could not store the extra data, make sure the tdb does not
1990 * contain a main record with wrong dangling extra data */
1992 fstr_sprintf(key_str, "DR/%d", pid);
1993 tdb_delete(wcache->tdb, string_tdb_data(key_str));
1995 return;
1998 BOOL cache_retrieve_response(pid_t pid, struct winbindd_response * response)
2000 TDB_DATA data;
2001 fstring key_str;
2003 if (!init_wcache())
2004 return False;
2006 DEBUG(10, ("Retrieving response for pid %d\n", pid));
2008 fstr_sprintf(key_str, "DR/%d", pid);
2009 data = tdb_fetch(wcache->tdb, string_tdb_data(key_str));
2011 if (data.dptr == NULL)
2012 return False;
2014 if (data.dsize != sizeof(*response))
2015 return False;
2017 memcpy(response, data.dptr, data.dsize);
2018 SAFE_FREE(data.dptr);
2020 if (response->length == sizeof(*response)) {
2021 response->extra_data.data = NULL;
2022 return True;
2025 /* There's extra data */
2027 DEBUG(10, ("Retrieving extra data length=%d\n",
2028 (int)(response->length - sizeof(*response))));
2030 fstr_sprintf(key_str, "DE/%d", pid);
2031 data = tdb_fetch(wcache->tdb, string_tdb_data(key_str));
2033 if (data.dptr == NULL) {
2034 DEBUG(0, ("Did not find extra data\n"));
2035 return False;
2038 if (data.dsize != (response->length - sizeof(*response))) {
2039 DEBUG(0, ("Invalid extra data length: %d\n", (int)data.dsize));
2040 SAFE_FREE(data.dptr);
2041 return False;
2044 dump_data(11, data.dptr, data.dsize);
2046 response->extra_data.data = data.dptr;
2047 return True;
2050 void cache_cleanup_response(pid_t pid)
2052 fstring key_str;
2054 if (!init_wcache())
2055 return;
2057 fstr_sprintf(key_str, "DR/%d", pid);
2058 tdb_delete(wcache->tdb, string_tdb_data(key_str));
2060 fstr_sprintf(key_str, "DE/%d", pid);
2061 tdb_delete(wcache->tdb, string_tdb_data(key_str));
2063 return;
2067 BOOL lookup_cached_sid(TALLOC_CTX *mem_ctx, const DOM_SID *sid,
2068 const char **domain_name, const char **name,
2069 enum SID_NAME_USE *type)
2071 struct winbindd_domain *domain;
2072 struct winbind_cache *cache;
2073 struct cache_entry *centry = NULL;
2074 NTSTATUS status;
2076 domain = find_lookup_domain_from_sid(sid);
2077 if (domain == NULL) {
2078 return False;
2081 cache = get_cache(domain);
2083 if (cache->tdb == NULL) {
2084 return False;
2087 centry = wcache_fetch(cache, domain, "SN/%s", sid_string_static(sid));
2088 if (centry == NULL) {
2089 return False;
2092 if (NT_STATUS_IS_OK(centry->status)) {
2093 *type = (enum SID_NAME_USE)centry_uint32(centry);
2094 *domain_name = centry_string(centry, mem_ctx);
2095 *name = centry_string(centry, mem_ctx);
2098 status = centry->status;
2099 centry_free(centry);
2100 return NT_STATUS_IS_OK(status);
2103 BOOL lookup_cached_name(TALLOC_CTX *mem_ctx,
2104 const char *domain_name,
2105 const char *name,
2106 DOM_SID *sid,
2107 enum SID_NAME_USE *type)
2109 struct winbindd_domain *domain;
2110 struct winbind_cache *cache;
2111 struct cache_entry *centry = NULL;
2112 NTSTATUS status;
2113 fstring uname;
2115 domain = find_lookup_domain_from_name(domain_name);
2116 if (domain == NULL) {
2117 return False;
2120 cache = get_cache(domain);
2122 if (cache->tdb == NULL) {
2123 return False;
2126 fstrcpy(uname, name);
2127 strupper_m(uname);
2129 centry = wcache_fetch(cache, domain, "NS/%s/%s", domain_name, uname);
2130 if (centry == NULL) {
2131 return False;
2134 if (NT_STATUS_IS_OK(centry->status)) {
2135 *type = (enum SID_NAME_USE)centry_uint32(centry);
2136 centry_sid(centry, mem_ctx, sid);
2139 status = centry->status;
2140 centry_free(centry);
2142 return NT_STATUS_IS_OK(status);
2145 void cache_name2sid(struct winbindd_domain *domain,
2146 const char *domain_name, const char *name,
2147 enum SID_NAME_USE type, const DOM_SID *sid)
2149 refresh_sequence_number(domain, False);
2150 wcache_save_name_to_sid(domain, NT_STATUS_OK, domain_name, name,
2151 sid, type);
2154 /* delete all centries that don't have NT_STATUS_OK set */
2155 static int traverse_fn_cleanup(TDB_CONTEXT *the_tdb, TDB_DATA kbuf,
2156 TDB_DATA dbuf, void *state)
2158 struct cache_entry *centry;
2160 centry = wcache_fetch_raw(kbuf.dptr);
2161 if (!centry) {
2162 return 0;
2165 if (!NT_STATUS_IS_OK(centry->status)) {
2166 DEBUG(10,("deleting centry %s\n", kbuf.dptr));
2167 tdb_delete(the_tdb, kbuf);
2170 centry_free(centry);
2171 return 0;
2174 /* flush the cache */
2175 void wcache_flush_cache(void)
2177 extern BOOL opt_nocache;
2179 if (!wcache)
2180 return;
2181 if (wcache->tdb) {
2182 tdb_close(wcache->tdb);
2183 wcache->tdb = NULL;
2185 if (opt_nocache)
2186 return;
2188 /* when working offline we must not clear the cache on restart */
2189 wcache->tdb = tdb_open_log(lock_path("winbindd_cache.tdb"),
2190 WINBINDD_CACHE_TDB_DEFAULT_HASH_SIZE,
2191 lp_winbind_offline_logon() ? TDB_DEFAULT : (TDB_DEFAULT | TDB_CLEAR_IF_FIRST),
2192 O_RDWR|O_CREAT, 0600);
2194 if (!wcache->tdb) {
2195 DEBUG(0,("Failed to open winbindd_cache.tdb!\n"));
2196 return;
2199 tdb_traverse(wcache->tdb, traverse_fn_cleanup, NULL);
2201 DEBUG(10,("wcache_flush_cache success\n"));
2204 /* Count cached creds */
2206 static int traverse_fn_cached_creds(TDB_CONTEXT *the_tdb, TDB_DATA kbuf, TDB_DATA dbuf,
2207 void *state)
2209 int *cred_count = (int*)state;
2211 if (strncmp(kbuf.dptr, "CRED/", 5) == 0) {
2212 (*cred_count)++;
2214 return 0;
2217 NTSTATUS wcache_count_cached_creds(struct winbindd_domain *domain, int *count)
2219 struct winbind_cache *cache = get_cache(domain);
2221 *count = 0;
2223 if (!cache->tdb) {
2224 return NT_STATUS_INTERNAL_DB_ERROR;
2227 tdb_traverse(cache->tdb, traverse_fn_cached_creds, (void *)count);
2229 return NT_STATUS_OK;
2232 struct cred_list {
2233 struct cred_list *prev, *next;
2234 TDB_DATA key;
2235 fstring name;
2236 time_t created;
2238 static struct cred_list *wcache_cred_list;
2240 static int traverse_fn_get_credlist(TDB_CONTEXT *the_tdb, TDB_DATA kbuf, TDB_DATA dbuf,
2241 void *state)
2243 struct cred_list *cred;
2245 if (strncmp(kbuf.dptr, "CRED/", 5) == 0) {
2247 cred = SMB_MALLOC_P(struct cred_list);
2248 if (cred == NULL) {
2249 DEBUG(0,("traverse_fn_remove_first_creds: failed to malloc new entry for list\n"));
2250 return -1;
2253 ZERO_STRUCTP(cred);
2255 /* save a copy of the key */
2257 fstrcpy(cred->name, kbuf.dptr);
2258 DLIST_ADD(wcache_cred_list, cred);
2261 return 0;
2264 NTSTATUS wcache_remove_oldest_cached_creds(struct winbindd_domain *domain, const DOM_SID *sid)
2266 struct winbind_cache *cache = get_cache(domain);
2267 NTSTATUS status;
2268 int ret;
2269 struct cred_list *cred, *oldest = NULL;
2271 if (!cache->tdb) {
2272 return NT_STATUS_INTERNAL_DB_ERROR;
2275 /* we possibly already have an entry */
2276 if (sid && NT_STATUS_IS_OK(wcache_cached_creds_exist(domain, sid))) {
2278 fstring key_str;
2280 DEBUG(11,("we already have an entry, deleting that\n"));
2282 fstr_sprintf(key_str, "CRED/%s", sid_string_static(sid));
2284 tdb_delete(cache->tdb, string_tdb_data(key_str));
2286 return NT_STATUS_OK;
2289 ret = tdb_traverse(cache->tdb, traverse_fn_get_credlist, NULL);
2290 if (ret == 0) {
2291 return NT_STATUS_OK;
2292 } else if ((ret == -1) || (wcache_cred_list == NULL)) {
2293 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2296 ZERO_STRUCTP(oldest);
2298 for (cred = wcache_cred_list; cred; cred = cred->next) {
2300 TDB_DATA data;
2301 time_t t;
2303 data = tdb_fetch(cache->tdb, make_tdb_data(cred->name, strlen(cred->name)));
2304 if (!data.dptr) {
2305 DEBUG(10,("wcache_remove_oldest_cached_creds: entry for [%s] not found\n",
2306 cred->name));
2307 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
2308 goto done;
2311 t = IVAL(data.dptr, 0);
2312 SAFE_FREE(data.dptr);
2314 if (!oldest) {
2315 oldest = SMB_MALLOC_P(struct cred_list);
2316 if (oldest == NULL) {
2317 status = NT_STATUS_NO_MEMORY;
2318 goto done;
2321 fstrcpy(oldest->name, cred->name);
2322 oldest->created = t;
2323 continue;
2326 if (t < oldest->created) {
2327 fstrcpy(oldest->name, cred->name);
2328 oldest->created = t;
2332 if (tdb_delete(cache->tdb, string_tdb_data(oldest->name)) == 0) {
2333 status = NT_STATUS_OK;
2334 } else {
2335 status = NT_STATUS_UNSUCCESSFUL;
2337 done:
2338 SAFE_FREE(wcache_cred_list);
2339 SAFE_FREE(oldest);
2341 return status;
2344 /* Change the global online/offline state. */
2345 BOOL set_global_winbindd_state_offline(void)
2347 TDB_DATA data;
2348 int err;
2350 DEBUG(10,("set_global_winbindd_state_offline: offline requested.\n"));
2352 /* Only go offline if someone has created
2353 the key "WINBINDD_OFFLINE" in the cache tdb. */
2355 if (wcache == NULL || wcache->tdb == NULL) {
2356 DEBUG(10,("set_global_winbindd_state_offline: wcache not open yet.\n"));
2357 return False;
2360 if (!lp_winbind_offline_logon()) {
2361 DEBUG(10,("set_global_winbindd_state_offline: rejecting.\n"));
2362 return False;
2365 if (global_winbindd_offline_state) {
2366 /* Already offline. */
2367 return True;
2370 wcache->tdb->ecode = 0;
2372 data = tdb_fetch_bystring( wcache->tdb, "WINBINDD_OFFLINE" );
2374 /* As this is a key with no data we don't need to free, we
2375 check for existence by looking at tdb_err. */
2377 err = tdb_error(wcache->tdb);
2379 if (err == TDB_ERR_NOEXIST) {
2380 DEBUG(10,("set_global_winbindd_state_offline: offline state not set.\n"));
2381 return False;
2382 } else {
2383 DEBUG(10,("set_global_winbindd_state_offline: offline state set.\n"));
2384 global_winbindd_offline_state = True;
2385 return True;
2389 void set_global_winbindd_state_online(void)
2391 DEBUG(10,("set_global_winbindd_state_online: online requested.\n"));
2393 if (!lp_winbind_offline_logon()) {
2394 DEBUG(10,("set_global_winbindd_state_online: rejecting.\n"));
2395 return;
2398 if (!global_winbindd_offline_state) {
2399 /* Already online. */
2400 return;
2402 global_winbindd_offline_state = False;
2404 if (!wcache->tdb) {
2405 return;
2408 /* Ensure there is no key "WINBINDD_OFFLINE" in the cache tdb. */
2409 tdb_delete_bystring(wcache->tdb, "WINBINDD_OFFLINE");
2412 BOOL get_global_winbindd_state_online(void)
2414 return global_winbindd_offline_state;
2417 /* the cache backend methods are exposed via this structure */
2418 struct winbindd_methods cache_methods = {
2419 True,
2420 query_user_list,
2421 enum_dom_groups,
2422 enum_local_groups,
2423 name_to_sid,
2424 sid_to_name,
2425 query_user,
2426 lookup_usergroups,
2427 lookup_useraliases,
2428 lookup_groupmem,
2429 sequence_number,
2430 lockout_policy,
2431 password_policy,
2432 trusted_domains