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.
30 #define DBGC_CLASS DBGC_WINBIND
32 #define WINBINDD_CACHE_VERSION 1
33 #define WINBINDD_CACHE_VERSION_KEYSTR "WINBINDD_CACHE_VERSION"
35 extern struct winbindd_methods reconnect_methods
;
36 extern BOOL opt_nocache
;
38 extern struct winbindd_methods ads_methods
;
40 extern struct winbindd_methods passdb_methods
;
43 * JRA. KEEP THIS LIST UP TO DATE IF YOU ADD CACHE ENTRIES.
44 * Here are the list of entry types that are *not* stored
45 * as form struct cache_entry in the cache.
48 static const char *non_centry_keys
[] = {
53 WINBINDD_CACHE_VERSION_KEYSTR
,
57 /************************************************************************
58 Is this key a non-centry type ?
59 ************************************************************************/
61 static BOOL
is_non_centry_key(TDB_DATA kbuf
)
65 if (kbuf
.dptr
== NULL
|| kbuf
.dsize
== 0) {
68 for (i
= 0; non_centry_keys
[i
] != NULL
; i
++) {
69 size_t namelen
= strlen(non_centry_keys
[i
]);
70 if (kbuf
.dsize
< namelen
) {
73 if (strncmp(non_centry_keys
[i
], (const char *)kbuf
.dptr
, namelen
) == 0) {
80 /* Global online/offline state - False when online. winbindd starts up online
81 and sets this to true if the first query fails and there's an entry in
82 the cache tdb telling us to stay offline. */
84 static BOOL global_winbindd_offline_state
;
86 struct winbind_cache
{
92 uint32 sequence_number
;
97 #define WINBINDD_MAX_CACHE_SIZE (50*1024*1024)
99 static struct winbind_cache
*wcache
;
101 void winbindd_check_cache_size(time_t t
)
103 static time_t last_check_time
;
106 if (last_check_time
== (time_t)0)
109 if (t
- last_check_time
< 60 && t
- last_check_time
> 0)
112 if (wcache
== NULL
|| wcache
->tdb
== NULL
) {
113 DEBUG(0, ("Unable to check size of tdb cache - cache not open !\n"));
117 if (fstat(tdb_fd(wcache
->tdb
), &st
) == -1) {
118 DEBUG(0, ("Unable to check size of tdb cache %s!\n", strerror(errno
) ));
122 if (st
.st_size
> WINBINDD_MAX_CACHE_SIZE
) {
123 DEBUG(10,("flushing cache due to size (%lu) > (%lu)\n",
124 (unsigned long)st
.st_size
,
125 (unsigned long)WINBINDD_MAX_CACHE_SIZE
));
126 wcache_flush_cache();
130 /* get the winbind_cache structure */
131 static struct winbind_cache
*get_cache(struct winbindd_domain
*domain
)
133 struct winbind_cache
*ret
= wcache
;
135 struct winbindd_domain
*our_domain
= domain
;
138 /* We have to know what type of domain we are dealing with first. */
140 if (domain
->internal
) {
141 domain
->backend
= &passdb_methods
;
142 domain
->initialized
= True
;
144 if ( !domain
->initialized
) {
145 init_dc_connection( domain
);
149 OK. listen up becasue I'm only going to say this once.
150 We have the following scenarios to consider
151 (a) trusted AD domains on a Samba DC,
152 (b) trusted AD domains and we are joined to a non-kerberos domain
153 (c) trusted AD domains and we are joined to a kerberos (AD) domain
155 For (a) we can always contact the trusted domain using krb5
156 since we have the domain trust account password
158 For (b) we can only use RPC since we have no way of
159 getting a krb5 ticket in our own domain
161 For (c) we can always use krb5 since we have a kerberos trust
166 if (!domain
->backend
) {
168 /* find our domain first so we can figure out if we
169 are joined to a kerberized domain */
171 if ( !domain
->primary
)
172 our_domain
= find_our_domain();
174 if ( (our_domain
->active_directory
|| IS_DC
) && domain
->active_directory
) {
175 DEBUG(5,("get_cache: Setting ADS methods for domain %s\n", domain
->name
));
176 domain
->backend
= &ads_methods
;
178 #endif /* HAVE_ADS */
179 DEBUG(5,("get_cache: Setting MS-RPC methods for domain %s\n", domain
->name
));
180 domain
->backend
= &reconnect_methods
;
183 #endif /* HAVE_ADS */
189 ret
= SMB_XMALLOC_P(struct winbind_cache
);
193 wcache_flush_cache();
199 free a centry structure
201 static void centry_free(struct cache_entry
*centry
)
205 SAFE_FREE(centry
->data
);
210 pull a uint32 from a cache entry
212 static uint32
centry_uint32(struct cache_entry
*centry
)
215 if (centry
->len
- centry
->ofs
< 4) {
216 DEBUG(0,("centry corruption? needed 4 bytes, have %d\n",
217 centry
->len
- centry
->ofs
));
218 smb_panic("centry_uint32");
220 ret
= IVAL(centry
->data
, centry
->ofs
);
226 pull a uint16 from a cache entry
228 static uint16
centry_uint16(struct cache_entry
*centry
)
231 if (centry
->len
- centry
->ofs
< 2) {
232 DEBUG(0,("centry corruption? needed 2 bytes, have %d\n",
233 centry
->len
- centry
->ofs
));
234 smb_panic("centry_uint16");
236 ret
= CVAL(centry
->data
, centry
->ofs
);
242 pull a uint8 from a cache entry
244 static uint8
centry_uint8(struct cache_entry
*centry
)
247 if (centry
->len
- centry
->ofs
< 1) {
248 DEBUG(0,("centry corruption? needed 1 bytes, have %d\n",
249 centry
->len
- centry
->ofs
));
250 smb_panic("centry_uint32");
252 ret
= CVAL(centry
->data
, centry
->ofs
);
258 pull a NTTIME from a cache entry
260 static NTTIME
centry_nttime(struct cache_entry
*centry
)
263 if (centry
->len
- centry
->ofs
< 8) {
264 DEBUG(0,("centry corruption? needed 8 bytes, have %d\n",
265 centry
->len
- centry
->ofs
));
266 smb_panic("centry_nttime");
268 ret
= IVAL(centry
->data
, centry
->ofs
);
270 ret
+= (uint64_t)IVAL(centry
->data
, centry
->ofs
) << 32;
276 pull a time_t from a cache entry. time_t stored portably as a 64-bit time.
278 static time_t centry_time(struct cache_entry
*centry
)
280 return (time_t)centry_nttime(centry
);
283 /* pull a string from a cache entry, using the supplied
286 static char *centry_string(struct cache_entry
*centry
, TALLOC_CTX
*mem_ctx
)
291 len
= centry_uint8(centry
);
294 /* a deliberate NULL string */
298 if (centry
->len
- centry
->ofs
< len
) {
299 DEBUG(0,("centry corruption? needed %d bytes, have %d\n",
300 len
, centry
->len
- centry
->ofs
));
301 smb_panic("centry_string");
304 ret
= TALLOC_ARRAY(mem_ctx
, char, len
+1);
306 smb_panic("centry_string out of memory\n");
308 memcpy(ret
,centry
->data
+ centry
->ofs
, len
);
314 /* pull a hash16 from a cache entry, using the supplied
317 static char *centry_hash16(struct cache_entry
*centry
, TALLOC_CTX
*mem_ctx
)
322 len
= centry_uint8(centry
);
325 DEBUG(0,("centry corruption? hash len (%u) != 16\n",
330 if (centry
->len
- centry
->ofs
< 16) {
331 DEBUG(0,("centry corruption? needed 16 bytes, have %d\n",
332 centry
->len
- centry
->ofs
));
336 ret
= TALLOC_ARRAY(mem_ctx
, char, 16);
338 smb_panic("centry_hash out of memory\n");
340 memcpy(ret
,centry
->data
+ centry
->ofs
, 16);
345 /* pull a sid from a cache entry, using the supplied
348 static BOOL
centry_sid(struct cache_entry
*centry
, TALLOC_CTX
*mem_ctx
, DOM_SID
*sid
)
351 sid_string
= centry_string(centry
, mem_ctx
);
352 if ((sid_string
== NULL
) || (!string_to_sid(sid
, sid_string
))) {
358 /* the server is considered down if it can't give us a sequence number */
359 static BOOL
wcache_server_down(struct winbindd_domain
*domain
)
366 ret
= (domain
->sequence_number
== DOM_SEQUENCE_NONE
);
369 DEBUG(10,("wcache_server_down: server for Domain %s down\n",
374 static NTSTATUS
fetch_cache_seqnum( struct winbindd_domain
*domain
, time_t now
)
381 DEBUG(10,("fetch_cache_seqnum: tdb == NULL\n"));
382 return NT_STATUS_UNSUCCESSFUL
;
385 fstr_sprintf( key
, "SEQNUM/%s", domain
->name
);
387 data
= tdb_fetch_bystring( wcache
->tdb
, key
);
388 if ( !data
.dptr
|| data
.dsize
!=8 ) {
389 DEBUG(10,("fetch_cache_seqnum: invalid data size key [%s]\n", key
));
390 return NT_STATUS_UNSUCCESSFUL
;
393 domain
->sequence_number
= IVAL(data
.dptr
, 0);
394 domain
->last_seq_check
= IVAL(data
.dptr
, 4);
396 SAFE_FREE(data
.dptr
);
398 /* have we expired? */
400 time_diff
= now
- domain
->last_seq_check
;
401 if ( time_diff
> lp_winbind_cache_time() ) {
402 DEBUG(10,("fetch_cache_seqnum: timeout [%s][%u @ %u]\n",
403 domain
->name
, domain
->sequence_number
,
404 (uint32
)domain
->last_seq_check
));
405 return NT_STATUS_UNSUCCESSFUL
;
408 DEBUG(10,("fetch_cache_seqnum: success [%s][%u @ %u]\n",
409 domain
->name
, domain
->sequence_number
,
410 (uint32
)domain
->last_seq_check
));
415 static NTSTATUS
store_cache_seqnum( struct winbindd_domain
*domain
)
422 DEBUG(10,("store_cache_seqnum: tdb == NULL\n"));
423 return NT_STATUS_UNSUCCESSFUL
;
426 fstr_sprintf( key_str
, "SEQNUM/%s", domain
->name
);
428 SIVAL(buf
, 0, domain
->sequence_number
);
429 SIVAL(buf
, 4, domain
->last_seq_check
);
433 if ( tdb_store_bystring( wcache
->tdb
, key_str
, data
, TDB_REPLACE
) == -1 ) {
434 DEBUG(10,("store_cache_seqnum: tdb_store fail key [%s]\n", key_str
));
435 return NT_STATUS_UNSUCCESSFUL
;
438 DEBUG(10,("store_cache_seqnum: success [%s][%u @ %u]\n",
439 domain
->name
, domain
->sequence_number
,
440 (uint32
)domain
->last_seq_check
));
446 refresh the domain sequence number. If force is True
447 then always refresh it, no matter how recently we fetched it
450 static void refresh_sequence_number(struct winbindd_domain
*domain
, BOOL force
)
454 time_t t
= time(NULL
);
455 unsigned cache_time
= lp_winbind_cache_time();
459 #if 0 /* JERRY -- disable as the default cache time is now 5 minutes */
460 /* trying to reconnect is expensive, don't do it too often */
461 if (domain
->sequence_number
== DOM_SEQUENCE_NONE
) {
466 time_diff
= t
- domain
->last_seq_check
;
468 /* see if we have to refetch the domain sequence number */
469 if (!force
&& (time_diff
< cache_time
)) {
470 DEBUG(10, ("refresh_sequence_number: %s time ok\n", domain
->name
));
474 /* try to get the sequence number from the tdb cache first */
475 /* this will update the timestamp as well */
477 status
= fetch_cache_seqnum( domain
, t
);
478 if ( NT_STATUS_IS_OK(status
) )
481 /* important! make sure that we know if this is a native
482 mode domain or not */
484 status
= domain
->backend
->sequence_number(domain
, &domain
->sequence_number
);
486 /* the above call could have set our domain->backend to NULL when
487 * coming from offline to online mode, make sure to reinitialize the
488 * backend - Guenther */
491 if (!NT_STATUS_IS_OK(status
)) {
492 DEBUG(10,("refresh_sequence_number: failed with %s\n", nt_errstr(status
)));
493 domain
->sequence_number
= DOM_SEQUENCE_NONE
;
496 domain
->last_status
= status
;
497 domain
->last_seq_check
= time(NULL
);
499 /* save the new sequence number ni the cache */
500 store_cache_seqnum( domain
);
503 DEBUG(10, ("refresh_sequence_number: %s seq number is now %d\n",
504 domain
->name
, domain
->sequence_number
));
510 decide if a cache entry has expired
512 static BOOL
centry_expired(struct winbindd_domain
*domain
, const char *keystr
, struct cache_entry
*centry
)
514 /* If we've been told to be offline - stay in that state... */
515 if (lp_winbind_offline_logon() && global_winbindd_offline_state
) {
516 DEBUG(10,("centry_expired: Key %s for domain %s valid as winbindd is globally offline.\n",
517 keystr
, domain
->name
));
521 /* when the domain is offline return the cached entry.
522 * This deals with transient offline states... */
524 if (!domain
->online
) {
525 DEBUG(10,("centry_expired: Key %s for domain %s valid as domain is offline.\n",
526 keystr
, domain
->name
));
530 /* if the server is OK and our cache entry came from when it was down then
531 the entry is invalid */
532 if ((domain
->sequence_number
!= DOM_SEQUENCE_NONE
) &&
533 (centry
->sequence_number
== DOM_SEQUENCE_NONE
)) {
534 DEBUG(10,("centry_expired: Key %s for domain %s invalid sequence.\n",
535 keystr
, domain
->name
));
539 /* if the server is down or the cache entry is not older than the
540 current sequence number then it is OK */
541 if (wcache_server_down(domain
) ||
542 centry
->sequence_number
== domain
->sequence_number
) {
543 DEBUG(10,("centry_expired: Key %s for domain %s is good.\n",
544 keystr
, domain
->name
));
548 DEBUG(10,("centry_expired: Key %s for domain %s expired\n",
549 keystr
, domain
->name
));
555 static struct cache_entry
*wcache_fetch_raw(char *kstr
)
558 struct cache_entry
*centry
;
562 key
.dsize
= strlen(kstr
);
563 data
= tdb_fetch(wcache
->tdb
, key
);
569 centry
= SMB_XMALLOC_P(struct cache_entry
);
570 centry
->data
= (unsigned char *)data
.dptr
;
571 centry
->len
= data
.dsize
;
574 if (centry
->len
< 8) {
575 /* huh? corrupt cache? */
576 DEBUG(10,("wcache_fetch_raw: Corrupt cache for key %s (len < 8) ?\n", kstr
));
581 centry
->status
= NT_STATUS(centry_uint32(centry
));
582 centry
->sequence_number
= centry_uint32(centry
);
588 fetch an entry from the cache, with a varargs key. auto-fetch the sequence
589 number and return status
591 static struct cache_entry
*wcache_fetch(struct winbind_cache
*cache
,
592 struct winbindd_domain
*domain
,
593 const char *format
, ...) PRINTF_ATTRIBUTE(3,4);
594 static struct cache_entry
*wcache_fetch(struct winbind_cache
*cache
,
595 struct winbindd_domain
*domain
,
596 const char *format
, ...)
600 struct cache_entry
*centry
;
606 refresh_sequence_number(domain
, False
);
608 va_start(ap
, format
);
609 smb_xvasprintf(&kstr
, format
, ap
);
612 centry
= wcache_fetch_raw(kstr
);
613 if (centry
== NULL
) {
618 if (centry_expired(domain
, kstr
, centry
)) {
620 DEBUG(10,("wcache_fetch: entry %s expired for domain %s\n",
621 kstr
, domain
->name
));
628 DEBUG(10,("wcache_fetch: returning entry %s for domain %s\n",
629 kstr
, domain
->name
));
635 static void wcache_delete(const char *format
, ...) PRINTF_ATTRIBUTE(1,2);
636 static void wcache_delete(const char *format
, ...)
642 va_start(ap
, format
);
643 smb_xvasprintf(&kstr
, format
, ap
);
647 key
.dsize
= strlen(kstr
);
649 tdb_delete(wcache
->tdb
, key
);
654 make sure we have at least len bytes available in a centry
656 static void centry_expand(struct cache_entry
*centry
, uint32 len
)
658 if (centry
->len
- centry
->ofs
>= len
)
661 centry
->data
= SMB_REALLOC_ARRAY(centry
->data
, unsigned char,
664 DEBUG(0,("out of memory: needed %d bytes in centry_expand\n", centry
->len
));
665 smb_panic("out of memory in centry_expand");
670 push a uint32 into a centry
672 static void centry_put_uint32(struct cache_entry
*centry
, uint32 v
)
674 centry_expand(centry
, 4);
675 SIVAL(centry
->data
, centry
->ofs
, v
);
680 push a uint16 into a centry
682 static void centry_put_uint16(struct cache_entry
*centry
, uint16 v
)
684 centry_expand(centry
, 2);
685 SIVAL(centry
->data
, centry
->ofs
, v
);
690 push a uint8 into a centry
692 static void centry_put_uint8(struct cache_entry
*centry
, uint8 v
)
694 centry_expand(centry
, 1);
695 SCVAL(centry
->data
, centry
->ofs
, v
);
700 push a string into a centry
702 static void centry_put_string(struct cache_entry
*centry
, const char *s
)
707 /* null strings are marked as len 0xFFFF */
708 centry_put_uint8(centry
, 0xFF);
713 /* can't handle more than 254 char strings. Truncating is probably best */
715 DEBUG(10,("centry_put_string: truncating len (%d) to: 254\n", len
));
718 centry_put_uint8(centry
, len
);
719 centry_expand(centry
, len
);
720 memcpy(centry
->data
+ centry
->ofs
, s
, len
);
725 push a 16 byte hash into a centry - treat as 16 byte string.
727 static void centry_put_hash16(struct cache_entry
*centry
, const uint8 val
[16])
729 centry_put_uint8(centry
, 16);
730 centry_expand(centry
, 16);
731 memcpy(centry
->data
+ centry
->ofs
, val
, 16);
735 static void centry_put_sid(struct cache_entry
*centry
, const DOM_SID
*sid
)
738 centry_put_string(centry
, sid_to_string(sid_string
, sid
));
742 push a NTTIME into a centry
744 static void centry_put_nttime(struct cache_entry
*centry
, NTTIME nt
)
746 centry_expand(centry
, 8);
747 SIVAL(centry
->data
, centry
->ofs
, nt
& 0xFFFFFFFF);
749 SIVAL(centry
->data
, centry
->ofs
, nt
>> 32);
754 push a time_t into a centry - use a 64 bit size.
755 NTTIME here is being used as a convenient 64-bit size.
757 static void centry_put_time(struct cache_entry
*centry
, time_t t
)
759 NTTIME nt
= (NTTIME
)t
;
760 centry_put_nttime(centry
, nt
);
764 start a centry for output. When finished, call centry_end()
766 struct cache_entry
*centry_start(struct winbindd_domain
*domain
, NTSTATUS status
)
768 struct cache_entry
*centry
;
773 centry
= SMB_XMALLOC_P(struct cache_entry
);
775 centry
->len
= 8192; /* reasonable default */
776 centry
->data
= SMB_XMALLOC_ARRAY(uint8
, centry
->len
);
778 centry
->sequence_number
= domain
->sequence_number
;
779 centry_put_uint32(centry
, NT_STATUS_V(status
));
780 centry_put_uint32(centry
, centry
->sequence_number
);
785 finish a centry and write it to the tdb
787 static void centry_end(struct cache_entry
*centry
, const char *format
, ...) PRINTF_ATTRIBUTE(2,3);
788 static void centry_end(struct cache_entry
*centry
, const char *format
, ...)
794 va_start(ap
, format
);
795 smb_xvasprintf(&kstr
, format
, ap
);
799 key
.dsize
= strlen(kstr
);
800 data
.dptr
= (char *)centry
->data
;
801 data
.dsize
= centry
->ofs
;
803 tdb_store(wcache
->tdb
, key
, data
, TDB_REPLACE
);
807 static void wcache_save_name_to_sid(struct winbindd_domain
*domain
,
808 NTSTATUS status
, const char *domain_name
,
809 const char *name
, const DOM_SID
*sid
,
810 enum lsa_SidType type
)
812 struct cache_entry
*centry
;
815 centry
= centry_start(domain
, status
);
818 centry_put_uint32(centry
, type
);
819 centry_put_sid(centry
, sid
);
820 fstrcpy(uname
, name
);
822 centry_end(centry
, "NS/%s/%s", domain_name
, uname
);
823 DEBUG(10,("wcache_save_name_to_sid: %s\\%s -> %s\n", domain_name
, uname
,
824 sid_string_static(sid
)));
828 static void wcache_save_sid_to_name(struct winbindd_domain
*domain
, NTSTATUS status
,
829 const DOM_SID
*sid
, const char *domain_name
, const char *name
, enum lsa_SidType type
)
831 struct cache_entry
*centry
;
834 if (is_null_sid(sid
)) {
838 centry
= centry_start(domain
, status
);
841 if (NT_STATUS_IS_OK(status
)) {
842 centry_put_uint32(centry
, type
);
843 centry_put_string(centry
, domain_name
);
844 centry_put_string(centry
, name
);
846 centry_end(centry
, "SN/%s", sid_to_string(sid_string
, sid
));
847 DEBUG(10,("wcache_save_sid_to_name: %s -> %s\n", sid_string
, name
));
852 static void wcache_save_user(struct winbindd_domain
*domain
, NTSTATUS status
, WINBIND_USERINFO
*info
)
854 struct cache_entry
*centry
;
857 if (is_null_sid(&info
->user_sid
)) {
861 centry
= centry_start(domain
, status
);
864 centry_put_string(centry
, info
->acct_name
);
865 centry_put_string(centry
, info
->full_name
);
866 centry_put_string(centry
, info
->homedir
);
867 centry_put_string(centry
, info
->shell
);
868 centry_put_uint32(centry
, info
->primary_gid
);
869 centry_put_sid(centry
, &info
->user_sid
);
870 centry_put_sid(centry
, &info
->group_sid
);
871 centry_end(centry
, "U/%s", sid_to_string(sid_string
, &info
->user_sid
));
872 DEBUG(10,("wcache_save_user: %s (acct_name %s)\n", sid_string
, info
->acct_name
));
876 static void wcache_save_lockout_policy(struct winbindd_domain
*domain
, NTSTATUS status
, SAM_UNK_INFO_12
*lockout_policy
)
878 struct cache_entry
*centry
;
880 centry
= centry_start(domain
, status
);
884 centry_put_nttime(centry
, lockout_policy
->duration
);
885 centry_put_nttime(centry
, lockout_policy
->reset_count
);
886 centry_put_uint16(centry
, lockout_policy
->bad_attempt_lockout
);
888 centry_end(centry
, "LOC_POL/%s", domain
->name
);
890 DEBUG(10,("wcache_save_lockout_policy: %s\n", domain
->name
));
895 static void wcache_save_password_policy(struct winbindd_domain
*domain
, NTSTATUS status
, SAM_UNK_INFO_1
*policy
)
897 struct cache_entry
*centry
;
899 centry
= centry_start(domain
, status
);
903 centry_put_uint16(centry
, policy
->min_length_password
);
904 centry_put_uint16(centry
, policy
->password_history
);
905 centry_put_uint32(centry
, policy
->password_properties
);
906 centry_put_nttime(centry
, policy
->expire
);
907 centry_put_nttime(centry
, policy
->min_passwordage
);
909 centry_end(centry
, "PWD_POL/%s", domain
->name
);
911 DEBUG(10,("wcache_save_password_policy: %s\n", domain
->name
));
916 NTSTATUS
wcache_cached_creds_exist(struct winbindd_domain
*domain
, const DOM_SID
*sid
)
918 struct winbind_cache
*cache
= get_cache(domain
);
924 return NT_STATUS_INTERNAL_DB_ERROR
;
927 if (is_null_sid(sid
)) {
928 return NT_STATUS_INVALID_SID
;
931 if (!(sid_peek_rid(sid
, &rid
)) || (rid
== 0)) {
932 return NT_STATUS_INVALID_SID
;
935 fstr_sprintf(key_str
, "CRED/%s", sid_string_static(sid
));
937 data
= tdb_fetch(cache
->tdb
, make_tdb_data(key_str
, strlen(key_str
)));
939 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
942 SAFE_FREE(data
.dptr
);
946 /* Lookup creds for a SID - copes with old (unsalted) creds as well
947 as new salted ones. */
949 NTSTATUS
wcache_get_creds(struct winbindd_domain
*domain
,
952 const uint8
**cached_nt_pass
,
953 const uint8
**cached_salt
)
955 struct winbind_cache
*cache
= get_cache(domain
);
956 struct cache_entry
*centry
= NULL
;
962 return NT_STATUS_INTERNAL_DB_ERROR
;
965 if (is_null_sid(sid
)) {
966 return NT_STATUS_INVALID_SID
;
969 if (!(sid_peek_rid(sid
, &rid
)) || (rid
== 0)) {
970 return NT_STATUS_INVALID_SID
;
973 /* Try and get a salted cred first. If we can't
974 fall back to an unsalted cred. */
976 centry
= wcache_fetch(cache
, domain
, "CRED/%s", sid_string_static(sid
));
978 DEBUG(10,("wcache_get_creds: entry for [CRED/%s] not found\n",
979 sid_string_static(sid
)));
980 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
983 t
= centry_time(centry
);
985 /* In the salted case this isn't actually the nt_hash itself,
986 but the MD5 of the salt + nt_hash. Let the caller
987 sort this out. It can tell as we only return the cached_salt
988 if we are returning a salted cred. */
990 *cached_nt_pass
= (const uint8
*)centry_hash16(centry
, mem_ctx
);
991 if (*cached_nt_pass
== NULL
) {
992 const char *sidstr
= sid_string_static(sid
);
994 /* Bad (old) cred cache. Delete and pretend we
996 DEBUG(0,("wcache_get_creds: bad entry for [CRED/%s] - deleting\n",
998 wcache_delete("CRED/%s", sidstr
);
1000 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
1003 /* We only have 17 bytes more data in the salted cred case. */
1004 if (centry
->len
- centry
->ofs
== 17) {
1005 *cached_salt
= (const uint8
*)centry_hash16(centry
, mem_ctx
);
1007 *cached_salt
= NULL
;
1011 dump_data(100, (const char *)*cached_nt_pass
, NT_HASH_LEN
);
1013 dump_data(100, (const char *)*cached_salt
, NT_HASH_LEN
);
1016 status
= centry
->status
;
1018 DEBUG(10,("wcache_get_creds: [Cached] - cached creds for user %s status: %s\n",
1019 sid_string_static(sid
), nt_errstr(status
) ));
1021 centry_free(centry
);
1025 /* Store creds for a SID - only writes out new salted ones. */
1027 NTSTATUS
wcache_save_creds(struct winbindd_domain
*domain
,
1028 TALLOC_CTX
*mem_ctx
,
1030 const uint8 nt_pass
[NT_HASH_LEN
])
1032 struct cache_entry
*centry
;
1035 uint8 cred_salt
[NT_HASH_LEN
];
1036 uint8 salted_hash
[NT_HASH_LEN
];
1038 if (is_null_sid(sid
)) {
1039 return NT_STATUS_INVALID_SID
;
1042 if (!(sid_peek_rid(sid
, &rid
)) || (rid
== 0)) {
1043 return NT_STATUS_INVALID_SID
;
1046 centry
= centry_start(domain
, NT_STATUS_OK
);
1048 return NT_STATUS_INTERNAL_DB_ERROR
;
1052 dump_data(100, (const char *)nt_pass
, NT_HASH_LEN
);
1055 centry_put_time(centry
, time(NULL
));
1057 /* Create a salt and then salt the hash. */
1058 generate_random_buffer(cred_salt
, NT_HASH_LEN
);
1059 E_md5hash(cred_salt
, nt_pass
, salted_hash
);
1061 centry_put_hash16(centry
, salted_hash
);
1062 centry_put_hash16(centry
, cred_salt
);
1063 centry_end(centry
, "CRED/%s", sid_to_string(sid_string
, sid
));
1065 DEBUG(10,("wcache_save_creds: %s\n", sid_string
));
1067 centry_free(centry
);
1069 return NT_STATUS_OK
;
1073 /* Query display info. This is the basic user list fn */
1074 static NTSTATUS
query_user_list(struct winbindd_domain
*domain
,
1075 TALLOC_CTX
*mem_ctx
,
1076 uint32
*num_entries
,
1077 WINBIND_USERINFO
**info
)
1079 struct winbind_cache
*cache
= get_cache(domain
);
1080 struct cache_entry
*centry
= NULL
;
1082 unsigned int i
, retry
;
1087 centry
= wcache_fetch(cache
, domain
, "UL/%s", domain
->name
);
1091 *num_entries
= centry_uint32(centry
);
1093 if (*num_entries
== 0)
1096 (*info
) = TALLOC_ARRAY(mem_ctx
, WINBIND_USERINFO
, *num_entries
);
1098 smb_panic("query_user_list out of memory");
1099 for (i
=0; i
<(*num_entries
); i
++) {
1100 (*info
)[i
].acct_name
= centry_string(centry
, mem_ctx
);
1101 (*info
)[i
].full_name
= centry_string(centry
, mem_ctx
);
1102 (*info
)[i
].homedir
= centry_string(centry
, mem_ctx
);
1103 (*info
)[i
].shell
= centry_string(centry
, mem_ctx
);
1104 centry_sid(centry
, mem_ctx
, &(*info
)[i
].user_sid
);
1105 centry_sid(centry
, mem_ctx
, &(*info
)[i
].group_sid
);
1109 status
= centry
->status
;
1111 DEBUG(10,("query_user_list: [Cached] - cached list for domain %s status: %s\n",
1112 domain
->name
, nt_errstr(status
) ));
1114 centry_free(centry
);
1121 /* Return status value returned by seq number check */
1123 if (!NT_STATUS_IS_OK(domain
->last_status
))
1124 return domain
->last_status
;
1126 /* Put the query_user_list() in a retry loop. There appears to be
1127 * some bug either with Windows 2000 or Samba's handling of large
1128 * rpc replies. This manifests itself as sudden disconnection
1129 * at a random point in the enumeration of a large (60k) user list.
1130 * The retry loop simply tries the operation again. )-: It's not
1131 * pretty but an acceptable workaround until we work out what the
1132 * real problem is. */
1137 DEBUG(10,("query_user_list: [Cached] - doing backend query for list for domain %s\n",
1140 status
= domain
->backend
->query_user_list(domain
, mem_ctx
, num_entries
, info
);
1141 if (!NT_STATUS_IS_OK(status
)) {
1142 DEBUG(3, ("query_user_list: returned 0x%08x, "
1143 "retrying\n", NT_STATUS_V(status
)));
1145 if (NT_STATUS_EQUAL(status
, NT_STATUS_UNSUCCESSFUL
)) {
1146 DEBUG(3, ("query_user_list: flushing "
1147 "connection cache\n"));
1148 invalidate_cm_connection(&domain
->conn
);
1151 } while (NT_STATUS_V(status
) == NT_STATUS_V(NT_STATUS_UNSUCCESSFUL
) &&
1155 refresh_sequence_number(domain
, False
);
1156 centry
= centry_start(domain
, status
);
1159 centry_put_uint32(centry
, *num_entries
);
1160 for (i
=0; i
<(*num_entries
); i
++) {
1161 centry_put_string(centry
, (*info
)[i
].acct_name
);
1162 centry_put_string(centry
, (*info
)[i
].full_name
);
1163 centry_put_string(centry
, (*info
)[i
].homedir
);
1164 centry_put_string(centry
, (*info
)[i
].shell
);
1165 centry_put_sid(centry
, &(*info
)[i
].user_sid
);
1166 centry_put_sid(centry
, &(*info
)[i
].group_sid
);
1167 if (domain
->backend
&& domain
->backend
->consistent
) {
1168 /* when the backend is consistent we can pre-prime some mappings */
1169 wcache_save_name_to_sid(domain
, NT_STATUS_OK
,
1171 (*info
)[i
].acct_name
,
1172 &(*info
)[i
].user_sid
,
1174 wcache_save_sid_to_name(domain
, NT_STATUS_OK
,
1175 &(*info
)[i
].user_sid
,
1177 (*info
)[i
].acct_name
,
1179 wcache_save_user(domain
, NT_STATUS_OK
, &(*info
)[i
]);
1182 centry_end(centry
, "UL/%s", domain
->name
);
1183 centry_free(centry
);
1189 /* list all domain groups */
1190 static NTSTATUS
enum_dom_groups(struct winbindd_domain
*domain
,
1191 TALLOC_CTX
*mem_ctx
,
1192 uint32
*num_entries
,
1193 struct acct_info
**info
)
1195 struct winbind_cache
*cache
= get_cache(domain
);
1196 struct cache_entry
*centry
= NULL
;
1203 centry
= wcache_fetch(cache
, domain
, "GL/%s/domain", domain
->name
);
1207 *num_entries
= centry_uint32(centry
);
1209 if (*num_entries
== 0)
1212 (*info
) = TALLOC_ARRAY(mem_ctx
, struct acct_info
, *num_entries
);
1214 smb_panic("enum_dom_groups out of memory");
1215 for (i
=0; i
<(*num_entries
); i
++) {
1216 fstrcpy((*info
)[i
].acct_name
, centry_string(centry
, mem_ctx
));
1217 fstrcpy((*info
)[i
].acct_desc
, centry_string(centry
, mem_ctx
));
1218 (*info
)[i
].rid
= centry_uint32(centry
);
1222 status
= centry
->status
;
1224 DEBUG(10,("enum_dom_groups: [Cached] - cached list for domain %s status: %s\n",
1225 domain
->name
, nt_errstr(status
) ));
1227 centry_free(centry
);
1234 /* Return status value returned by seq number check */
1236 if (!NT_STATUS_IS_OK(domain
->last_status
))
1237 return domain
->last_status
;
1239 DEBUG(10,("enum_dom_groups: [Cached] - doing backend query for list for domain %s\n",
1242 status
= domain
->backend
->enum_dom_groups(domain
, mem_ctx
, num_entries
, info
);
1245 refresh_sequence_number(domain
, False
);
1246 centry
= centry_start(domain
, status
);
1249 centry_put_uint32(centry
, *num_entries
);
1250 for (i
=0; i
<(*num_entries
); i
++) {
1251 centry_put_string(centry
, (*info
)[i
].acct_name
);
1252 centry_put_string(centry
, (*info
)[i
].acct_desc
);
1253 centry_put_uint32(centry
, (*info
)[i
].rid
);
1255 centry_end(centry
, "GL/%s/domain", domain
->name
);
1256 centry_free(centry
);
1262 /* list all domain groups */
1263 static NTSTATUS
enum_local_groups(struct winbindd_domain
*domain
,
1264 TALLOC_CTX
*mem_ctx
,
1265 uint32
*num_entries
,
1266 struct acct_info
**info
)
1268 struct winbind_cache
*cache
= get_cache(domain
);
1269 struct cache_entry
*centry
= NULL
;
1276 centry
= wcache_fetch(cache
, domain
, "GL/%s/local", domain
->name
);
1280 *num_entries
= centry_uint32(centry
);
1282 if (*num_entries
== 0)
1285 (*info
) = TALLOC_ARRAY(mem_ctx
, struct acct_info
, *num_entries
);
1287 smb_panic("enum_dom_groups out of memory");
1288 for (i
=0; i
<(*num_entries
); i
++) {
1289 fstrcpy((*info
)[i
].acct_name
, centry_string(centry
, mem_ctx
));
1290 fstrcpy((*info
)[i
].acct_desc
, centry_string(centry
, mem_ctx
));
1291 (*info
)[i
].rid
= centry_uint32(centry
);
1296 /* If we are returning cached data and the domain controller
1297 is down then we don't know whether the data is up to date
1298 or not. Return NT_STATUS_MORE_PROCESSING_REQUIRED to
1301 if (wcache_server_down(domain
)) {
1302 DEBUG(10, ("enum_local_groups: returning cached user list and server was down\n"));
1303 status
= NT_STATUS_MORE_PROCESSING_REQUIRED
;
1305 status
= centry
->status
;
1307 DEBUG(10,("enum_local_groups: [Cached] - cached list for domain %s status: %s\n",
1308 domain
->name
, nt_errstr(status
) ));
1310 centry_free(centry
);
1317 /* Return status value returned by seq number check */
1319 if (!NT_STATUS_IS_OK(domain
->last_status
))
1320 return domain
->last_status
;
1322 DEBUG(10,("enum_local_groups: [Cached] - doing backend query for list for domain %s\n",
1325 status
= domain
->backend
->enum_local_groups(domain
, mem_ctx
, num_entries
, info
);
1328 refresh_sequence_number(domain
, False
);
1329 centry
= centry_start(domain
, status
);
1332 centry_put_uint32(centry
, *num_entries
);
1333 for (i
=0; i
<(*num_entries
); i
++) {
1334 centry_put_string(centry
, (*info
)[i
].acct_name
);
1335 centry_put_string(centry
, (*info
)[i
].acct_desc
);
1336 centry_put_uint32(centry
, (*info
)[i
].rid
);
1338 centry_end(centry
, "GL/%s/local", domain
->name
);
1339 centry_free(centry
);
1345 /* convert a single name to a sid in a domain */
1346 static NTSTATUS
name_to_sid(struct winbindd_domain
*domain
,
1347 TALLOC_CTX
*mem_ctx
,
1348 const char *domain_name
,
1351 enum lsa_SidType
*type
)
1353 struct winbind_cache
*cache
= get_cache(domain
);
1354 struct cache_entry
*centry
= NULL
;
1361 fstrcpy(uname
, name
);
1363 centry
= wcache_fetch(cache
, domain
, "NS/%s/%s", domain_name
, uname
);
1366 *type
= (enum lsa_SidType
)centry_uint32(centry
);
1367 status
= centry
->status
;
1368 if (NT_STATUS_IS_OK(status
)) {
1369 centry_sid(centry
, mem_ctx
, sid
);
1372 DEBUG(10,("name_to_sid: [Cached] - cached name for domain %s status: %s\n",
1373 domain
->name
, nt_errstr(status
) ));
1375 centry_free(centry
);
1381 /* If the seq number check indicated that there is a problem
1382 * with this DC, then return that status... except for
1383 * access_denied. This is special because the dc may be in
1384 * "restrict anonymous = 1" mode, in which case it will deny
1385 * most unauthenticated operations, but *will* allow the LSA
1386 * name-to-sid that we try as a fallback. */
1388 if (!(NT_STATUS_IS_OK(domain
->last_status
)
1389 || NT_STATUS_EQUAL(domain
->last_status
, NT_STATUS_ACCESS_DENIED
)))
1390 return domain
->last_status
;
1392 DEBUG(10,("name_to_sid: [Cached] - doing backend query for name for domain %s\n",
1395 status
= domain
->backend
->name_to_sid(domain
, mem_ctx
, domain_name
, name
, sid
, type
);
1398 refresh_sequence_number(domain
, False
);
1400 if (domain
->online
&& !is_null_sid(sid
)) {
1401 wcache_save_name_to_sid(domain
, status
, domain_name
, name
, sid
, *type
);
1404 if (NT_STATUS_IS_OK(status
)) {
1405 strupper_m(CONST_DISCARD(char *,domain_name
));
1406 strlower_m(CONST_DISCARD(char *,name
));
1407 wcache_save_sid_to_name(domain
, status
, sid
, domain_name
, name
, *type
);
1413 /* convert a sid to a user or group name. The sid is guaranteed to be in the domain
1415 static NTSTATUS
sid_to_name(struct winbindd_domain
*domain
,
1416 TALLOC_CTX
*mem_ctx
,
1420 enum lsa_SidType
*type
)
1422 struct winbind_cache
*cache
= get_cache(domain
);
1423 struct cache_entry
*centry
= NULL
;
1430 centry
= wcache_fetch(cache
, domain
, "SN/%s", sid_to_string(sid_string
, sid
));
1433 if (NT_STATUS_IS_OK(centry
->status
)) {
1434 *type
= (enum lsa_SidType
)centry_uint32(centry
);
1435 *domain_name
= centry_string(centry
, mem_ctx
);
1436 *name
= centry_string(centry
, mem_ctx
);
1438 status
= centry
->status
;
1440 DEBUG(10,("sid_to_name: [Cached] - cached name for domain %s status: %s\n",
1441 domain
->name
, nt_errstr(status
) ));
1443 centry_free(centry
);
1448 *domain_name
= NULL
;
1450 /* If the seq number check indicated that there is a problem
1451 * with this DC, then return that status... except for
1452 * access_denied. This is special because the dc may be in
1453 * "restrict anonymous = 1" mode, in which case it will deny
1454 * most unauthenticated operations, but *will* allow the LSA
1455 * sid-to-name that we try as a fallback. */
1457 if (!(NT_STATUS_IS_OK(domain
->last_status
)
1458 || NT_STATUS_EQUAL(domain
->last_status
, NT_STATUS_ACCESS_DENIED
)))
1459 return domain
->last_status
;
1461 DEBUG(10,("sid_to_name: [Cached] - doing backend query for name for domain %s\n",
1464 status
= domain
->backend
->sid_to_name(domain
, mem_ctx
, sid
, domain_name
, name
, type
);
1467 refresh_sequence_number(domain
, False
);
1468 wcache_save_sid_to_name(domain
, status
, sid
, *domain_name
, *name
, *type
);
1470 /* We can't save the name to sid mapping here, as with sid history a
1471 * later name2sid would give the wrong sid. */
1476 static NTSTATUS
rids_to_names(struct winbindd_domain
*domain
,
1477 TALLOC_CTX
*mem_ctx
,
1478 const DOM_SID
*domain_sid
,
1483 enum lsa_SidType
**types
)
1485 struct winbind_cache
*cache
= get_cache(domain
);
1487 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
1491 *domain_name
= NULL
;
1499 if (num_rids
== 0) {
1500 return NT_STATUS_OK
;
1503 *names
= TALLOC_ARRAY(mem_ctx
, char *, num_rids
);
1504 *types
= TALLOC_ARRAY(mem_ctx
, enum lsa_SidType
, num_rids
);
1506 if ((*names
== NULL
) || (*types
== NULL
)) {
1507 result
= NT_STATUS_NO_MEMORY
;
1511 have_mapped
= have_unmapped
= False
;
1513 for (i
=0; i
<num_rids
; i
++) {
1515 struct cache_entry
*centry
;
1517 if (!sid_compose(&sid
, domain_sid
, rids
[i
])) {
1518 result
= NT_STATUS_INTERNAL_ERROR
;
1522 centry
= wcache_fetch(cache
, domain
, "SN/%s",
1523 sid_string_static(&sid
));
1528 (*types
)[i
] = SID_NAME_UNKNOWN
;
1529 (*names
)[i
] = talloc_strdup(*names
, "");
1531 if (NT_STATUS_IS_OK(centry
->status
)) {
1534 (*types
)[i
] = (enum lsa_SidType
)centry_uint32(centry
);
1535 dom
= centry_string(centry
, mem_ctx
);
1536 if (*domain_name
== NULL
) {
1541 (*names
)[i
] = centry_string(centry
, *names
);
1543 have_unmapped
= True
;
1546 centry_free(centry
);
1550 return NT_STATUS_NONE_MAPPED
;
1552 if (!have_unmapped
) {
1553 return NT_STATUS_OK
;
1555 return STATUS_SOME_UNMAPPED
;
1559 TALLOC_FREE(*names
);
1560 TALLOC_FREE(*types
);
1562 result
= domain
->backend
->rids_to_names(domain
, mem_ctx
, domain_sid
,
1563 rids
, num_rids
, domain_name
,
1566 if (!NT_STATUS_IS_OK(result
) &&
1567 !NT_STATUS_EQUAL(result
, STATUS_SOME_UNMAPPED
)) {
1571 refresh_sequence_number(domain
, False
);
1573 for (i
=0; i
<num_rids
; i
++) {
1577 if (!sid_compose(&sid
, domain_sid
, rids
[i
])) {
1578 result
= NT_STATUS_INTERNAL_ERROR
;
1582 status
= (*types
)[i
] == SID_NAME_UNKNOWN
?
1583 NT_STATUS_NONE_MAPPED
: NT_STATUS_OK
;
1585 wcache_save_sid_to_name(domain
, status
, &sid
, *domain_name
,
1586 (*names
)[i
], (*types
)[i
]);
1593 TALLOC_FREE(*names
);
1594 TALLOC_FREE(*types
);
1598 /* Lookup user information from a rid */
1599 static NTSTATUS
query_user(struct winbindd_domain
*domain
,
1600 TALLOC_CTX
*mem_ctx
,
1601 const DOM_SID
*user_sid
,
1602 WINBIND_USERINFO
*info
)
1604 struct winbind_cache
*cache
= get_cache(domain
);
1605 struct cache_entry
*centry
= NULL
;
1611 centry
= wcache_fetch(cache
, domain
, "U/%s", sid_string_static(user_sid
));
1613 /* If we have an access denied cache entry and a cached info3 in the
1614 samlogon cache then do a query. This will force the rpc back end
1615 to return the info3 data. */
1617 if (NT_STATUS_V(domain
->last_status
) == NT_STATUS_V(NT_STATUS_ACCESS_DENIED
) &&
1618 netsamlogon_cache_have(user_sid
)) {
1619 DEBUG(10, ("query_user: cached access denied and have cached info3\n"));
1620 domain
->last_status
= NT_STATUS_OK
;
1621 centry_free(centry
);
1628 info
->acct_name
= centry_string(centry
, mem_ctx
);
1629 info
->full_name
= centry_string(centry
, mem_ctx
);
1630 info
->homedir
= centry_string(centry
, mem_ctx
);
1631 info
->shell
= centry_string(centry
, mem_ctx
);
1632 info
->primary_gid
= centry_uint32(centry
);
1633 centry_sid(centry
, mem_ctx
, &info
->user_sid
);
1634 centry_sid(centry
, mem_ctx
, &info
->group_sid
);
1635 status
= centry
->status
;
1637 DEBUG(10,("query_user: [Cached] - cached info for domain %s status: %s\n",
1638 domain
->name
, nt_errstr(status
) ));
1640 centry_free(centry
);
1646 /* Return status value returned by seq number check */
1648 if (!NT_STATUS_IS_OK(domain
->last_status
))
1649 return domain
->last_status
;
1651 DEBUG(10,("query_user: [Cached] - doing backend query for info for domain %s\n",
1654 status
= domain
->backend
->query_user(domain
, mem_ctx
, user_sid
, info
);
1657 refresh_sequence_number(domain
, False
);
1658 wcache_save_user(domain
, status
, info
);
1664 /* Lookup groups a user is a member of. */
1665 static NTSTATUS
lookup_usergroups(struct winbindd_domain
*domain
,
1666 TALLOC_CTX
*mem_ctx
,
1667 const DOM_SID
*user_sid
,
1668 uint32
*num_groups
, DOM_SID
**user_gids
)
1670 struct winbind_cache
*cache
= get_cache(domain
);
1671 struct cache_entry
*centry
= NULL
;
1679 centry
= wcache_fetch(cache
, domain
, "UG/%s", sid_to_string(sid_string
, user_sid
));
1681 /* If we have an access denied cache entry and a cached info3 in the
1682 samlogon cache then do a query. This will force the rpc back end
1683 to return the info3 data. */
1685 if (NT_STATUS_V(domain
->last_status
) == NT_STATUS_V(NT_STATUS_ACCESS_DENIED
) &&
1686 netsamlogon_cache_have(user_sid
)) {
1687 DEBUG(10, ("lookup_usergroups: cached access denied and have cached info3\n"));
1688 domain
->last_status
= NT_STATUS_OK
;
1689 centry_free(centry
);
1696 *num_groups
= centry_uint32(centry
);
1698 if (*num_groups
== 0)
1701 (*user_gids
) = TALLOC_ARRAY(mem_ctx
, DOM_SID
, *num_groups
);
1703 smb_panic("lookup_usergroups out of memory");
1704 for (i
=0; i
<(*num_groups
); i
++) {
1705 centry_sid(centry
, mem_ctx
, &(*user_gids
)[i
]);
1709 status
= centry
->status
;
1711 DEBUG(10,("lookup_usergroups: [Cached] - cached info for domain %s status: %s\n",
1712 domain
->name
, nt_errstr(status
) ));
1714 centry_free(centry
);
1719 (*user_gids
) = NULL
;
1721 /* Return status value returned by seq number check */
1723 if (!NT_STATUS_IS_OK(domain
->last_status
))
1724 return domain
->last_status
;
1726 DEBUG(10,("lookup_usergroups: [Cached] - doing backend query for info for domain %s\n",
1729 status
= domain
->backend
->lookup_usergroups(domain
, mem_ctx
, user_sid
, num_groups
, user_gids
);
1732 refresh_sequence_number(domain
, False
);
1733 centry
= centry_start(domain
, status
);
1736 centry_put_uint32(centry
, *num_groups
);
1737 for (i
=0; i
<(*num_groups
); i
++) {
1738 centry_put_sid(centry
, &(*user_gids
)[i
]);
1740 centry_end(centry
, "UG/%s", sid_to_string(sid_string
, user_sid
));
1741 centry_free(centry
);
1747 static NTSTATUS
lookup_useraliases(struct winbindd_domain
*domain
,
1748 TALLOC_CTX
*mem_ctx
,
1749 uint32 num_sids
, const DOM_SID
*sids
,
1750 uint32
*num_aliases
, uint32
**alias_rids
)
1752 struct winbind_cache
*cache
= get_cache(domain
);
1753 struct cache_entry
*centry
= NULL
;
1755 char *sidlist
= talloc_strdup(mem_ctx
, "");
1761 if (num_sids
== 0) {
1764 return NT_STATUS_OK
;
1767 /* We need to cache indexed by the whole list of SIDs, the aliases
1768 * resulting might come from any of the SIDs. */
1770 for (i
=0; i
<num_sids
; i
++) {
1771 sidlist
= talloc_asprintf(mem_ctx
, "%s/%s", sidlist
,
1772 sid_string_static(&sids
[i
]));
1773 if (sidlist
== NULL
)
1774 return NT_STATUS_NO_MEMORY
;
1777 centry
= wcache_fetch(cache
, domain
, "UA%s", sidlist
);
1782 *num_aliases
= centry_uint32(centry
);
1786 (*alias_rids
) = TALLOC_ARRAY(mem_ctx
, uint32
, *num_aliases
);
1788 if ((*alias_rids
) == NULL
) {
1789 centry_free(centry
);
1790 return NT_STATUS_NO_MEMORY
;
1793 (*alias_rids
) = NULL
;
1796 for (i
=0; i
<(*num_aliases
); i
++)
1797 (*alias_rids
)[i
] = centry_uint32(centry
);
1799 status
= centry
->status
;
1801 DEBUG(10,("lookup_useraliases: [Cached] - cached info for domain: %s "
1802 "status %s\n", domain
->name
, nt_errstr(status
)));
1804 centry_free(centry
);
1809 (*alias_rids
) = NULL
;
1811 if (!NT_STATUS_IS_OK(domain
->last_status
))
1812 return domain
->last_status
;
1814 DEBUG(10,("lookup_usergroups: [Cached] - doing backend query for info "
1815 "for domain %s\n", domain
->name
));
1817 status
= domain
->backend
->lookup_useraliases(domain
, mem_ctx
,
1819 num_aliases
, alias_rids
);
1822 refresh_sequence_number(domain
, False
);
1823 centry
= centry_start(domain
, status
);
1826 centry_put_uint32(centry
, *num_aliases
);
1827 for (i
=0; i
<(*num_aliases
); i
++)
1828 centry_put_uint32(centry
, (*alias_rids
)[i
]);
1829 centry_end(centry
, "UA%s", sidlist
);
1830 centry_free(centry
);
1837 static NTSTATUS
lookup_groupmem(struct winbindd_domain
*domain
,
1838 TALLOC_CTX
*mem_ctx
,
1839 const DOM_SID
*group_sid
, uint32
*num_names
,
1840 DOM_SID
**sid_mem
, char ***names
,
1841 uint32
**name_types
)
1843 struct winbind_cache
*cache
= get_cache(domain
);
1844 struct cache_entry
*centry
= NULL
;
1852 centry
= wcache_fetch(cache
, domain
, "GM/%s", sid_to_string(sid_string
, group_sid
));
1856 *num_names
= centry_uint32(centry
);
1858 if (*num_names
== 0)
1861 (*sid_mem
) = TALLOC_ARRAY(mem_ctx
, DOM_SID
, *num_names
);
1862 (*names
) = TALLOC_ARRAY(mem_ctx
, char *, *num_names
);
1863 (*name_types
) = TALLOC_ARRAY(mem_ctx
, uint32
, *num_names
);
1865 if (! (*sid_mem
) || ! (*names
) || ! (*name_types
)) {
1866 smb_panic("lookup_groupmem out of memory");
1869 for (i
=0; i
<(*num_names
); i
++) {
1870 centry_sid(centry
, mem_ctx
, &(*sid_mem
)[i
]);
1871 (*names
)[i
] = centry_string(centry
, mem_ctx
);
1872 (*name_types
)[i
] = centry_uint32(centry
);
1876 status
= centry
->status
;
1878 DEBUG(10,("lookup_groupmem: [Cached] - cached info for domain %s status: %s\n",
1879 domain
->name
, nt_errstr(status
)));
1881 centry_free(centry
);
1888 (*name_types
) = NULL
;
1890 /* Return status value returned by seq number check */
1892 if (!NT_STATUS_IS_OK(domain
->last_status
))
1893 return domain
->last_status
;
1895 DEBUG(10,("lookup_groupmem: [Cached] - doing backend query for info for domain %s\n",
1898 status
= domain
->backend
->lookup_groupmem(domain
, mem_ctx
, group_sid
, num_names
,
1899 sid_mem
, names
, name_types
);
1902 refresh_sequence_number(domain
, False
);
1903 centry
= centry_start(domain
, status
);
1906 centry_put_uint32(centry
, *num_names
);
1907 for (i
=0; i
<(*num_names
); i
++) {
1908 centry_put_sid(centry
, &(*sid_mem
)[i
]);
1909 centry_put_string(centry
, (*names
)[i
]);
1910 centry_put_uint32(centry
, (*name_types
)[i
]);
1912 centry_end(centry
, "GM/%s", sid_to_string(sid_string
, group_sid
));
1913 centry_free(centry
);
1919 /* find the sequence number for a domain */
1920 static NTSTATUS
sequence_number(struct winbindd_domain
*domain
, uint32
*seq
)
1922 refresh_sequence_number(domain
, False
);
1924 *seq
= domain
->sequence_number
;
1926 return NT_STATUS_OK
;
1929 /* enumerate trusted domains
1930 * (we need to have the list of trustdoms in the cache when we go offline) -
1932 static NTSTATUS
trusted_domains(struct winbindd_domain
*domain
,
1933 TALLOC_CTX
*mem_ctx
,
1934 uint32
*num_domains
,
1939 struct winbind_cache
*cache
= get_cache(domain
);
1940 struct cache_entry
*centry
= NULL
;
1947 centry
= wcache_fetch(cache
, domain
, "TRUSTDOMS/%s", domain
->name
);
1953 *num_domains
= centry_uint32(centry
);
1956 (*names
) = TALLOC_ARRAY(mem_ctx
, char *, *num_domains
);
1957 (*alt_names
) = TALLOC_ARRAY(mem_ctx
, char *, *num_domains
);
1958 (*dom_sids
) = TALLOC_ARRAY(mem_ctx
, DOM_SID
, *num_domains
);
1960 if (! (*dom_sids
) || ! (*names
) || ! (*alt_names
)) {
1961 smb_panic("trusted_domains out of memory");
1965 (*alt_names
) = NULL
;
1969 for (i
=0; i
<(*num_domains
); i
++) {
1970 (*names
)[i
] = centry_string(centry
, mem_ctx
);
1971 (*alt_names
)[i
] = centry_string(centry
, mem_ctx
);
1972 centry_sid(centry
, mem_ctx
, &(*dom_sids
)[i
]);
1975 status
= centry
->status
;
1977 DEBUG(10,("trusted_domains: [Cached] - cached info for domain %s (%d trusts) status: %s\n",
1978 domain
->name
, *num_domains
, nt_errstr(status
) ));
1980 centry_free(centry
);
1987 (*alt_names
) = NULL
;
1989 /* Return status value returned by seq number check */
1991 if (!NT_STATUS_IS_OK(domain
->last_status
))
1992 return domain
->last_status
;
1994 DEBUG(10,("trusted_domains: [Cached] - doing backend query for info for domain %s\n",
1997 status
= domain
->backend
->trusted_domains(domain
, mem_ctx
, num_domains
,
1998 names
, alt_names
, dom_sids
);
2000 /* no trusts gives NT_STATUS_NO_MORE_ENTRIES resetting to NT_STATUS_OK
2001 * so that the generic centry handling still applies correctly -
2004 if (!NT_STATUS_IS_ERR(status
)) {
2005 status
= NT_STATUS_OK
;
2009 refresh_sequence_number(domain
, False
);
2011 centry
= centry_start(domain
, status
);
2015 centry_put_uint32(centry
, *num_domains
);
2017 for (i
=0; i
<(*num_domains
); i
++) {
2018 centry_put_string(centry
, (*names
)[i
]);
2019 centry_put_string(centry
, (*alt_names
)[i
]);
2020 centry_put_sid(centry
, &(*dom_sids
)[i
]);
2023 centry_end(centry
, "TRUSTDOMS/%s", domain
->name
);
2025 centry_free(centry
);
2031 /* get lockout policy */
2032 static NTSTATUS
lockout_policy(struct winbindd_domain
*domain
,
2033 TALLOC_CTX
*mem_ctx
,
2034 SAM_UNK_INFO_12
*policy
){
2035 struct winbind_cache
*cache
= get_cache(domain
);
2036 struct cache_entry
*centry
= NULL
;
2042 centry
= wcache_fetch(cache
, domain
, "LOC_POL/%s", domain
->name
);
2047 policy
->duration
= centry_nttime(centry
);
2048 policy
->reset_count
= centry_nttime(centry
);
2049 policy
->bad_attempt_lockout
= centry_uint16(centry
);
2051 status
= centry
->status
;
2053 DEBUG(10,("lockout_policy: [Cached] - cached info for domain %s status: %s\n",
2054 domain
->name
, nt_errstr(status
) ));
2056 centry_free(centry
);
2060 ZERO_STRUCTP(policy
);
2062 /* Return status value returned by seq number check */
2064 if (!NT_STATUS_IS_OK(domain
->last_status
))
2065 return domain
->last_status
;
2067 DEBUG(10,("lockout_policy: [Cached] - doing backend query for info for domain %s\n",
2070 status
= domain
->backend
->lockout_policy(domain
, mem_ctx
, policy
);
2073 refresh_sequence_number(domain
, False
);
2074 wcache_save_lockout_policy(domain
, status
, policy
);
2079 /* get password policy */
2080 static NTSTATUS
password_policy(struct winbindd_domain
*domain
,
2081 TALLOC_CTX
*mem_ctx
,
2082 SAM_UNK_INFO_1
*policy
)
2084 struct winbind_cache
*cache
= get_cache(domain
);
2085 struct cache_entry
*centry
= NULL
;
2091 centry
= wcache_fetch(cache
, domain
, "PWD_POL/%s", domain
->name
);
2096 policy
->min_length_password
= centry_uint16(centry
);
2097 policy
->password_history
= centry_uint16(centry
);
2098 policy
->password_properties
= centry_uint32(centry
);
2099 policy
->expire
= centry_nttime(centry
);
2100 policy
->min_passwordage
= centry_nttime(centry
);
2102 status
= centry
->status
;
2104 DEBUG(10,("lockout_policy: [Cached] - cached info for domain %s status: %s\n",
2105 domain
->name
, nt_errstr(status
) ));
2107 centry_free(centry
);
2111 ZERO_STRUCTP(policy
);
2113 /* Return status value returned by seq number check */
2115 if (!NT_STATUS_IS_OK(domain
->last_status
))
2116 return domain
->last_status
;
2118 DEBUG(10,("password_policy: [Cached] - doing backend query for info for domain %s\n",
2121 status
= domain
->backend
->password_policy(domain
, mem_ctx
, policy
);
2124 refresh_sequence_number(domain
, False
);
2125 if (NT_STATUS_IS_OK(status
)) {
2126 wcache_save_password_policy(domain
, status
, policy
);
2133 /* Invalidate cached user and group lists coherently */
2135 static int traverse_fn(TDB_CONTEXT
*the_tdb
, TDB_DATA kbuf
, TDB_DATA dbuf
,
2138 if (strncmp(kbuf
.dptr
, "UL/", 3) == 0 ||
2139 strncmp(kbuf
.dptr
, "GL/", 3) == 0)
2140 tdb_delete(the_tdb
, kbuf
);
2145 /* Invalidate the getpwnam and getgroups entries for a winbindd domain */
2147 void wcache_invalidate_samlogon(struct winbindd_domain
*domain
,
2148 NET_USER_INFO_3
*info3
)
2150 struct winbind_cache
*cache
;
2152 /* dont clear cached U/SID and UG/SID entries when we want to logon
2155 if (lp_winbind_offline_logon()) {
2162 cache
= get_cache(domain
);
2163 netsamlogon_clear_cached_user(cache
->tdb
, info3
);
2166 void wcache_invalidate_cache(void)
2168 struct winbindd_domain
*domain
;
2170 for (domain
= domain_list(); domain
; domain
= domain
->next
) {
2171 struct winbind_cache
*cache
= get_cache(domain
);
2173 DEBUG(10, ("wcache_invalidate_cache: invalidating cache "
2174 "entries for %s\n", domain
->name
));
2176 tdb_traverse(cache
->tdb
, traverse_fn
, NULL
);
2180 static BOOL
init_wcache(void)
2182 if (wcache
== NULL
) {
2183 wcache
= SMB_XMALLOC_P(struct winbind_cache
);
2184 ZERO_STRUCTP(wcache
);
2187 if (wcache
->tdb
!= NULL
)
2190 /* when working offline we must not clear the cache on restart */
2191 wcache
->tdb
= tdb_open_log(lock_path("winbindd_cache.tdb"),
2192 WINBINDD_CACHE_TDB_DEFAULT_HASH_SIZE
,
2193 lp_winbind_offline_logon() ? TDB_DEFAULT
: (TDB_DEFAULT
| TDB_CLEAR_IF_FIRST
),
2194 O_RDWR
|O_CREAT
, 0600);
2196 if (wcache
->tdb
== NULL
) {
2197 DEBUG(0,("Failed to open winbindd_cache.tdb!\n"));
2204 /************************************************************************
2205 This is called by the parent to initialize the cache file.
2206 We don't need sophisticated locking here as we know we're the
2208 ************************************************************************/
2210 BOOL
initialize_winbindd_cache(void)
2212 BOOL cache_bad
= True
;
2215 if (!init_wcache()) {
2216 DEBUG(0,("initialize_winbindd_cache: init_wcache failed.\n"));
2220 /* Check version number. */
2221 if (tdb_fetch_uint32(wcache
->tdb
, WINBINDD_CACHE_VERSION_KEYSTR
, &vers
) &&
2222 vers
== WINBINDD_CACHE_VERSION
) {
2227 DEBUG(0,("initialize_winbindd_cache: clearing cache "
2228 "and re-creating with version number %d\n",
2229 WINBINDD_CACHE_VERSION
));
2231 tdb_close(wcache
->tdb
);
2234 if (unlink(lock_path("winbindd_cache.tdb")) == -1) {
2235 DEBUG(0,("initialize_winbindd_cache: unlink %s failed %s ",
2236 lock_path("winbindd_cache.tdb"),
2240 if (!init_wcache()) {
2241 DEBUG(0,("initialize_winbindd_cache: re-initialization "
2242 "init_wcache failed.\n"));
2246 /* Write the version. */
2247 if (!tdb_store_uint32(wcache
->tdb
, WINBINDD_CACHE_VERSION_KEYSTR
, WINBINDD_CACHE_VERSION
)) {
2248 DEBUG(0,("initialize_winbindd_cache: version number store failed %s\n",
2249 tdb_errorstr(wcache
->tdb
) ));
2254 tdb_close(wcache
->tdb
);
2259 void cache_store_response(pid_t pid
, struct winbindd_response
*response
)
2266 DEBUG(10, ("Storing response for pid %d, len %d\n",
2267 pid
, response
->length
));
2269 fstr_sprintf(key_str
, "DR/%d", pid
);
2270 if (tdb_store(wcache
->tdb
, string_tdb_data(key_str
),
2271 make_tdb_data((const char *)response
, sizeof(*response
)),
2275 if (response
->length
== sizeof(*response
))
2278 /* There's extra data */
2280 DEBUG(10, ("Storing extra data: len=%d\n",
2281 (int)(response
->length
- sizeof(*response
))));
2283 fstr_sprintf(key_str
, "DE/%d", pid
);
2284 if (tdb_store(wcache
->tdb
, string_tdb_data(key_str
),
2285 make_tdb_data((const char *)response
->extra_data
.data
,
2286 response
->length
- sizeof(*response
)),
2290 /* We could not store the extra data, make sure the tdb does not
2291 * contain a main record with wrong dangling extra data */
2293 fstr_sprintf(key_str
, "DR/%d", pid
);
2294 tdb_delete(wcache
->tdb
, string_tdb_data(key_str
));
2299 BOOL
cache_retrieve_response(pid_t pid
, struct winbindd_response
* response
)
2307 DEBUG(10, ("Retrieving response for pid %d\n", pid
));
2309 fstr_sprintf(key_str
, "DR/%d", pid
);
2310 data
= tdb_fetch(wcache
->tdb
, string_tdb_data(key_str
));
2312 if (data
.dptr
== NULL
)
2315 if (data
.dsize
!= sizeof(*response
))
2318 memcpy(response
, data
.dptr
, data
.dsize
);
2319 SAFE_FREE(data
.dptr
);
2321 if (response
->length
== sizeof(*response
)) {
2322 response
->extra_data
.data
= NULL
;
2326 /* There's extra data */
2328 DEBUG(10, ("Retrieving extra data length=%d\n",
2329 (int)(response
->length
- sizeof(*response
))));
2331 fstr_sprintf(key_str
, "DE/%d", pid
);
2332 data
= tdb_fetch(wcache
->tdb
, string_tdb_data(key_str
));
2334 if (data
.dptr
== NULL
) {
2335 DEBUG(0, ("Did not find extra data\n"));
2339 if (data
.dsize
!= (response
->length
- sizeof(*response
))) {
2340 DEBUG(0, ("Invalid extra data length: %d\n", (int)data
.dsize
));
2341 SAFE_FREE(data
.dptr
);
2345 dump_data(11, data
.dptr
, data
.dsize
);
2347 response
->extra_data
.data
= data
.dptr
;
2351 void cache_cleanup_response(pid_t pid
)
2358 fstr_sprintf(key_str
, "DR/%d", pid
);
2359 tdb_delete(wcache
->tdb
, string_tdb_data(key_str
));
2361 fstr_sprintf(key_str
, "DE/%d", pid
);
2362 tdb_delete(wcache
->tdb
, string_tdb_data(key_str
));
2368 BOOL
lookup_cached_sid(TALLOC_CTX
*mem_ctx
, const DOM_SID
*sid
,
2369 const char **domain_name
, const char **name
,
2370 enum lsa_SidType
*type
)
2372 struct winbindd_domain
*domain
;
2373 struct winbind_cache
*cache
;
2374 struct cache_entry
*centry
= NULL
;
2377 domain
= find_lookup_domain_from_sid(sid
);
2378 if (domain
== NULL
) {
2382 cache
= get_cache(domain
);
2384 if (cache
->tdb
== NULL
) {
2388 centry
= wcache_fetch(cache
, domain
, "SN/%s", sid_string_static(sid
));
2389 if (centry
== NULL
) {
2393 if (NT_STATUS_IS_OK(centry
->status
)) {
2394 *type
= (enum lsa_SidType
)centry_uint32(centry
);
2395 *domain_name
= centry_string(centry
, mem_ctx
);
2396 *name
= centry_string(centry
, mem_ctx
);
2399 status
= centry
->status
;
2400 centry_free(centry
);
2401 return NT_STATUS_IS_OK(status
);
2404 BOOL
lookup_cached_name(TALLOC_CTX
*mem_ctx
,
2405 const char *domain_name
,
2408 enum lsa_SidType
*type
)
2410 struct winbindd_domain
*domain
;
2411 struct winbind_cache
*cache
;
2412 struct cache_entry
*centry
= NULL
;
2416 domain
= find_lookup_domain_from_name(domain_name
);
2417 if (domain
== NULL
) {
2421 cache
= get_cache(domain
);
2423 if (cache
->tdb
== NULL
) {
2427 fstrcpy(uname
, name
);
2430 centry
= wcache_fetch(cache
, domain
, "NS/%s/%s", domain_name
, uname
);
2431 if (centry
== NULL
) {
2435 if (NT_STATUS_IS_OK(centry
->status
)) {
2436 *type
= (enum lsa_SidType
)centry_uint32(centry
);
2437 centry_sid(centry
, mem_ctx
, sid
);
2440 status
= centry
->status
;
2441 centry_free(centry
);
2443 return NT_STATUS_IS_OK(status
);
2446 void cache_name2sid(struct winbindd_domain
*domain
,
2447 const char *domain_name
, const char *name
,
2448 enum lsa_SidType type
, const DOM_SID
*sid
)
2450 refresh_sequence_number(domain
, False
);
2451 wcache_save_name_to_sid(domain
, NT_STATUS_OK
, domain_name
, name
,
2455 /* delete all centries that don't have NT_STATUS_OK set */
2457 * The original idea that this cache only contains centries has
2458 * been blurred - now other stuff gets put in here. Ensure we
2459 * ignore these things on cleanup.
2462 static int traverse_fn_cleanup(TDB_CONTEXT
*the_tdb
, TDB_DATA kbuf
,
2463 TDB_DATA dbuf
, void *state
)
2465 struct cache_entry
*centry
;
2467 if (is_non_centry_key(kbuf
)) {
2471 centry
= wcache_fetch_raw(kbuf
.dptr
);
2476 if (!NT_STATUS_IS_OK(centry
->status
)) {
2477 DEBUG(10,("deleting centry %s\n", kbuf
.dptr
));
2478 tdb_delete(the_tdb
, kbuf
);
2481 centry_free(centry
);
2485 /* flush the cache */
2486 void wcache_flush_cache(void)
2491 tdb_close(wcache
->tdb
);
2497 /* when working offline we must not clear the cache on restart */
2498 wcache
->tdb
= tdb_open_log(lock_path("winbindd_cache.tdb"),
2499 WINBINDD_CACHE_TDB_DEFAULT_HASH_SIZE
,
2500 lp_winbind_offline_logon() ? TDB_DEFAULT
: (TDB_DEFAULT
| TDB_CLEAR_IF_FIRST
),
2501 O_RDWR
|O_CREAT
, 0600);
2504 DEBUG(0,("Failed to open winbindd_cache.tdb!\n"));
2508 tdb_traverse(wcache
->tdb
, traverse_fn_cleanup
, NULL
);
2510 DEBUG(10,("wcache_flush_cache success\n"));
2513 /* Count cached creds */
2515 static int traverse_fn_cached_creds(TDB_CONTEXT
*the_tdb
, TDB_DATA kbuf
, TDB_DATA dbuf
,
2518 int *cred_count
= (int*)state
;
2520 if (strncmp(kbuf
.dptr
, "CRED/", 5) == 0) {
2526 NTSTATUS
wcache_count_cached_creds(struct winbindd_domain
*domain
, int *count
)
2528 struct winbind_cache
*cache
= get_cache(domain
);
2533 return NT_STATUS_INTERNAL_DB_ERROR
;
2536 tdb_traverse(cache
->tdb
, traverse_fn_cached_creds
, (void *)count
);
2538 return NT_STATUS_OK
;
2542 struct cred_list
*prev
, *next
;
2547 static struct cred_list
*wcache_cred_list
;
2549 static int traverse_fn_get_credlist(TDB_CONTEXT
*the_tdb
, TDB_DATA kbuf
, TDB_DATA dbuf
,
2552 struct cred_list
*cred
;
2554 if (strncmp(kbuf
.dptr
, "CRED/", 5) == 0) {
2556 cred
= SMB_MALLOC_P(struct cred_list
);
2558 DEBUG(0,("traverse_fn_remove_first_creds: failed to malloc new entry for list\n"));
2564 /* save a copy of the key */
2566 fstrcpy(cred
->name
, kbuf
.dptr
);
2567 DLIST_ADD(wcache_cred_list
, cred
);
2573 NTSTATUS
wcache_remove_oldest_cached_creds(struct winbindd_domain
*domain
, const DOM_SID
*sid
)
2575 struct winbind_cache
*cache
= get_cache(domain
);
2578 struct cred_list
*cred
, *oldest
= NULL
;
2581 return NT_STATUS_INTERNAL_DB_ERROR
;
2584 /* we possibly already have an entry */
2585 if (sid
&& NT_STATUS_IS_OK(wcache_cached_creds_exist(domain
, sid
))) {
2589 DEBUG(11,("we already have an entry, deleting that\n"));
2591 fstr_sprintf(key_str
, "CRED/%s", sid_string_static(sid
));
2593 tdb_delete(cache
->tdb
, string_tdb_data(key_str
));
2595 return NT_STATUS_OK
;
2598 ret
= tdb_traverse(cache
->tdb
, traverse_fn_get_credlist
, NULL
);
2600 return NT_STATUS_OK
;
2601 } else if ((ret
== -1) || (wcache_cred_list
== NULL
)) {
2602 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
2605 ZERO_STRUCTP(oldest
);
2607 for (cred
= wcache_cred_list
; cred
; cred
= cred
->next
) {
2612 data
= tdb_fetch(cache
->tdb
, make_tdb_data(cred
->name
, strlen(cred
->name
)));
2614 DEBUG(10,("wcache_remove_oldest_cached_creds: entry for [%s] not found\n",
2616 status
= NT_STATUS_OBJECT_NAME_NOT_FOUND
;
2620 t
= IVAL(data
.dptr
, 0);
2621 SAFE_FREE(data
.dptr
);
2624 oldest
= SMB_MALLOC_P(struct cred_list
);
2625 if (oldest
== NULL
) {
2626 status
= NT_STATUS_NO_MEMORY
;
2630 fstrcpy(oldest
->name
, cred
->name
);
2631 oldest
->created
= t
;
2635 if (t
< oldest
->created
) {
2636 fstrcpy(oldest
->name
, cred
->name
);
2637 oldest
->created
= t
;
2641 if (tdb_delete(cache
->tdb
, string_tdb_data(oldest
->name
)) == 0) {
2642 status
= NT_STATUS_OK
;
2644 status
= NT_STATUS_UNSUCCESSFUL
;
2647 SAFE_FREE(wcache_cred_list
);
2653 /* Change the global online/offline state. */
2654 BOOL
set_global_winbindd_state_offline(void)
2658 DEBUG(10,("set_global_winbindd_state_offline: offline requested.\n"));
2660 /* Only go offline if someone has created
2661 the key "WINBINDD_OFFLINE" in the cache tdb. */
2663 if (wcache
== NULL
|| wcache
->tdb
== NULL
) {
2664 DEBUG(10,("set_global_winbindd_state_offline: wcache not open yet.\n"));
2668 if (!lp_winbind_offline_logon()) {
2669 DEBUG(10,("set_global_winbindd_state_offline: rejecting.\n"));
2673 if (global_winbindd_offline_state
) {
2674 /* Already offline. */
2678 data
= tdb_fetch_bystring( wcache
->tdb
, "WINBINDD_OFFLINE" );
2680 if (!data
.dptr
|| data
.dsize
!= 4) {
2681 DEBUG(10,("set_global_winbindd_state_offline: offline state not set.\n"));
2682 SAFE_FREE(data
.dptr
);
2685 DEBUG(10,("set_global_winbindd_state_offline: offline state set.\n"));
2686 global_winbindd_offline_state
= True
;
2687 SAFE_FREE(data
.dptr
);
2692 void set_global_winbindd_state_online(void)
2694 DEBUG(10,("set_global_winbindd_state_online: online requested.\n"));
2696 if (!lp_winbind_offline_logon()) {
2697 DEBUG(10,("set_global_winbindd_state_online: rejecting.\n"));
2701 if (!global_winbindd_offline_state
) {
2702 /* Already online. */
2705 global_winbindd_offline_state
= False
;
2711 /* Ensure there is no key "WINBINDD_OFFLINE" in the cache tdb. */
2712 tdb_delete_bystring(wcache
->tdb
, "WINBINDD_OFFLINE");
2715 BOOL
get_global_winbindd_state_offline(void)
2717 return global_winbindd_offline_state
;
2720 /* the cache backend methods are exposed via this structure */
2721 struct winbindd_methods cache_methods
= {