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 /* 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
{
44 uint32 sequence_number
;
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
;
58 if (last_check_time
== (time_t)0)
61 if (t
- last_check_time
< 60 && t
- last_check_time
> 0)
64 if (wcache
== NULL
|| wcache
->tdb
== NULL
) {
65 DEBUG(0, ("Unable to check size of tdb cache - cache not open !\n"));
69 if (fstat(wcache
->tdb
->fd
, &st
) == -1) {
70 DEBUG(0, ("Unable to check size of tdb cache %s!\n", strerror(errno
) ));
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
));
82 /* get the winbind_cache structure */
83 static struct winbind_cache
*get_cache(struct winbindd_domain
*domain
)
85 struct winbind_cache
*ret
= wcache
;
86 struct winbindd_domain
*our_domain
= domain
;
88 /* we have to know what type of domain we are dealing with first */
90 if ( !domain
->initialized
)
91 set_dc_type_and_flags( domain
);
94 OK. listen up becasue I'm only going to say this once.
95 We have the following scenarios to consider
96 (a) trusted AD domains on a Samba DC,
97 (b) trusted AD domains and we are joined to a non-kerberos domain
98 (c) trusted AD domains and we are joined to a kerberos (AD) domain
100 For (a) we can always contact the trusted domain using krb5
101 since we have the domain trust account password
103 For (b) we can only use RPC since we have no way of
104 getting a krb5 ticket in our own domain
106 For (c) we can always use krb5 since we have a kerberos trust
111 if (!domain
->backend
) {
112 extern struct winbindd_methods reconnect_methods
;
114 extern struct winbindd_methods ads_methods
;
116 /* find our domain first so we can figure out if we
117 are joined to a kerberized domain */
119 if ( !domain
->primary
)
120 our_domain
= find_our_domain();
122 if ( (our_domain
->active_directory
|| IS_DC
) && domain
->active_directory
) {
123 DEBUG(5,("get_cache: Setting ADS methods for domain %s\n", domain
->name
));
124 domain
->backend
= &ads_methods
;
126 #endif /* HAVE_ADS */
127 DEBUG(5,("get_cache: Setting MS-RPC methods for domain %s\n", domain
->name
));
128 domain
->backend
= &reconnect_methods
;
131 #endif /* HAVE_ADS */
137 ret
= SMB_XMALLOC_P(struct winbind_cache
);
141 wcache_flush_cache();
147 free a centry structure
149 static void centry_free(struct cache_entry
*centry
)
153 SAFE_FREE(centry
->data
);
158 pull a uint32 from a cache entry
160 static uint32
centry_uint32(struct cache_entry
*centry
)
163 if (centry
->len
- centry
->ofs
< 4) {
164 DEBUG(0,("centry corruption? needed 4 bytes, have %d\n",
165 centry
->len
- centry
->ofs
));
166 smb_panic("centry_uint32");
168 ret
= IVAL(centry
->data
, centry
->ofs
);
174 pull a uint16 from a cache entry
176 static uint16
centry_uint16(struct cache_entry
*centry
)
179 if (centry
->len
- centry
->ofs
< 2) {
180 DEBUG(0,("centry corruption? needed 2 bytes, have %d\n",
181 centry
->len
- centry
->ofs
));
182 smb_panic("centry_uint16");
184 ret
= CVAL(centry
->data
, centry
->ofs
);
190 pull a uint8 from a cache entry
192 static uint8
centry_uint8(struct cache_entry
*centry
)
195 if (centry
->len
- centry
->ofs
< 1) {
196 DEBUG(0,("centry corruption? needed 1 bytes, have %d\n",
197 centry
->len
- centry
->ofs
));
198 smb_panic("centry_uint32");
200 ret
= CVAL(centry
->data
, centry
->ofs
);
206 pull a NTTIME from a cache entry
208 static NTTIME
centry_nttime(struct cache_entry
*centry
)
211 if (centry
->len
- centry
->ofs
< 8) {
212 DEBUG(0,("centry corruption? needed 8 bytes, have %d\n",
213 centry
->len
- centry
->ofs
));
214 smb_panic("centry_nttime");
216 ret
.low
= IVAL(centry
->data
, centry
->ofs
);
218 ret
.high
= IVAL(centry
->data
, centry
->ofs
);
224 pull a time_t from a cache entry
226 static time_t centry_time(struct cache_entry
*centry
)
229 if (centry
->len
- centry
->ofs
< sizeof(time_t)) {
230 DEBUG(0,("centry corruption? needed %d bytes, have %d\n",
231 sizeof(time_t), centry
->len
- centry
->ofs
));
232 smb_panic("centry_time");
234 ret
= IVAL(centry
->data
, centry
->ofs
); /* FIXME: correct ? */
235 centry
->ofs
+= sizeof(time_t);
239 /* pull a string from a cache entry, using the supplied
242 static char *centry_string(struct cache_entry
*centry
, TALLOC_CTX
*mem_ctx
)
247 len
= centry_uint8(centry
);
250 /* a deliberate NULL string */
254 if (centry
->len
- centry
->ofs
< len
) {
255 DEBUG(0,("centry corruption? needed %d bytes, have %d\n",
256 len
, centry
->len
- centry
->ofs
));
257 smb_panic("centry_string");
261 ret
= TALLOC(mem_ctx
, len
+1);
263 ret
= SMB_MALLOC(len
+1);
265 smb_panic("centry_string out of memory\n");
267 memcpy(ret
,centry
->data
+ centry
->ofs
, len
);
273 /* pull a string from a cache entry, using the supplied
276 static BOOL
centry_sid(struct cache_entry
*centry
, DOM_SID
*sid
)
279 sid_string
= centry_string(centry
, NULL
);
280 if (!string_to_sid(sid
, sid_string
)) {
283 SAFE_FREE(sid_string
);
287 /* the server is considered down if it can't give us a sequence number */
288 static BOOL
wcache_server_down(struct winbindd_domain
*domain
)
295 ret
= (domain
->sequence_number
== DOM_SEQUENCE_NONE
);
298 DEBUG(10,("wcache_server_down: server for Domain %s down\n",
303 static NTSTATUS
fetch_cache_seqnum( struct winbindd_domain
*domain
, time_t now
)
310 DEBUG(10,("fetch_cache_seqnum: tdb == NULL\n"));
311 return NT_STATUS_UNSUCCESSFUL
;
314 fstr_sprintf( key
, "SEQNUM/%s", domain
->name
);
316 data
= tdb_fetch_bystring( wcache
->tdb
, key
);
317 if ( !data
.dptr
|| data
.dsize
!=8 ) {
318 DEBUG(10,("fetch_cache_seqnum: invalid data size key [%s]\n", key
));
319 return NT_STATUS_UNSUCCESSFUL
;
322 domain
->sequence_number
= IVAL(data
.dptr
, 0);
323 domain
->last_seq_check
= IVAL(data
.dptr
, 4);
325 SAFE_FREE(data
.dptr
);
327 /* have we expired? */
329 time_diff
= now
- domain
->last_seq_check
;
330 if ( time_diff
> lp_winbind_cache_time() ) {
331 DEBUG(10,("fetch_cache_seqnum: timeout [%s][%u @ %u]\n",
332 domain
->name
, domain
->sequence_number
,
333 (uint32
)domain
->last_seq_check
));
334 return NT_STATUS_UNSUCCESSFUL
;
337 DEBUG(10,("fetch_cache_seqnum: success [%s][%u @ %u]\n",
338 domain
->name
, domain
->sequence_number
,
339 (uint32
)domain
->last_seq_check
));
344 static NTSTATUS
store_cache_seqnum( struct winbindd_domain
*domain
)
351 DEBUG(10,("store_cache_seqnum: tdb == NULL\n"));
352 return NT_STATUS_UNSUCCESSFUL
;
355 fstr_sprintf( key_str
, "SEQNUM/%s", domain
->name
);
357 key
.dsize
= strlen(key_str
)+1;
359 SIVAL(buf
, 0, domain
->sequence_number
);
360 SIVAL(buf
, 4, domain
->last_seq_check
);
364 if ( tdb_store( wcache
->tdb
, key
, data
, TDB_REPLACE
) == -1 ) {
365 DEBUG(10,("store_cache_seqnum: tdb_store fail key [%s]\n", key_str
));
366 return NT_STATUS_UNSUCCESSFUL
;
369 DEBUG(10,("store_cache_seqnum: success [%s][%u @ %u]\n",
370 domain
->name
, domain
->sequence_number
,
371 (uint32
)domain
->last_seq_check
));
377 refresh the domain sequence number. If force is True
378 then always refresh it, no matter how recently we fetched it
381 static void refresh_sequence_number(struct winbindd_domain
*domain
, BOOL force
)
385 time_t t
= time(NULL
);
386 unsigned cache_time
= lp_winbind_cache_time();
390 #if 0 /* JERRY -- disable as the default cache time is now 5 minutes */
391 /* trying to reconnect is expensive, don't do it too often */
392 if (domain
->sequence_number
== DOM_SEQUENCE_NONE
) {
397 time_diff
= t
- domain
->last_seq_check
;
399 /* see if we have to refetch the domain sequence number */
400 if (!force
&& (time_diff
< cache_time
)) {
401 DEBUG(10, ("refresh_sequence_number: %s time ok\n", domain
->name
));
405 /* try to get the sequence number from the tdb cache first */
406 /* this will update the timestamp as well */
408 status
= fetch_cache_seqnum( domain
, t
);
409 if ( NT_STATUS_IS_OK(status
) )
412 /* important! make sure that we know if this is a native
413 mode domain or not */
415 status
= domain
->backend
->sequence_number(domain
, &domain
->sequence_number
);
417 if (!NT_STATUS_IS_OK(status
)) {
418 domain
->sequence_number
= DOM_SEQUENCE_NONE
;
421 domain
->last_status
= status
;
422 domain
->last_seq_check
= time(NULL
);
424 /* save the new sequence number ni the cache */
425 store_cache_seqnum( domain
);
428 DEBUG(10, ("refresh_sequence_number: %s seq number is now %d\n",
429 domain
->name
, domain
->sequence_number
));
435 decide if a cache entry has expired
437 static BOOL
centry_expired(struct winbindd_domain
*domain
, const char *keystr
, struct cache_entry
*centry
)
439 /* If we've been told to be offline - stay in that state... */
440 if (lp_winbind_offline_logon() && global_winbindd_offline_state
) {
441 DEBUG(10,("centry_expired: Key %s for domain %s valid as winbindd is globally offline.\n",
442 keystr
, domain
->name
));
446 /* when the domain is offline and we havent checked in the last 30
447 * seconds if it has become online again, return the cached entry.
448 * This deals with transient offline states... */
450 if (!domain
->online
&&
451 !NT_STATUS_IS_OK(check_negative_conn_cache(domain
->name
, domain
->dcname
))) {
452 DEBUG(10,("centry_expired: Key %s for domain %s valid as domain is offline.\n",
453 keystr
, domain
->name
));
457 /* if the server is OK and our cache entry came from when it was down then
458 the entry is invalid */
459 if ((domain
->sequence_number
!= DOM_SEQUENCE_NONE
) &&
460 (centry
->sequence_number
== DOM_SEQUENCE_NONE
)) {
461 DEBUG(10,("centry_expired: Key %s for domain %s invalid sequence.\n",
462 keystr
, domain
->name
));
466 /* if the server is down or the cache entry is not older than the
467 current sequence number then it is OK */
468 if (wcache_server_down(domain
) ||
469 centry
->sequence_number
== domain
->sequence_number
) {
470 DEBUG(10,("centry_expired: Key %s for domain %s is good.\n",
471 keystr
, domain
->name
));
475 DEBUG(10,("centry_expired: Key %s for domain %s expired\n",
476 keystr
, domain
->name
));
482 static struct cache_entry
*wcache_fetch_raw(char *kstr
)
485 struct cache_entry
*centry
;
489 key
.dsize
= strlen(kstr
);
490 data
= tdb_fetch(wcache
->tdb
, key
);
496 centry
= SMB_XMALLOC_P(struct cache_entry
);
497 centry
->data
= (unsigned char *)data
.dptr
;
498 centry
->len
= data
.dsize
;
501 if (centry
->len
< 8) {
502 /* huh? corrupt cache? */
503 DEBUG(10,("wcache_fetch_raw: Corrupt cache for key %s (len < 8) ?\n", kstr
));
508 centry
->status
= NT_STATUS(centry_uint32(centry
));
509 centry
->sequence_number
= centry_uint32(centry
);
515 fetch an entry from the cache, with a varargs key. auto-fetch the sequence
516 number and return status
518 static struct cache_entry
*wcache_fetch(struct winbind_cache
*cache
,
519 struct winbindd_domain
*domain
,
520 const char *format
, ...) PRINTF_ATTRIBUTE(3,4);
521 static struct cache_entry
*wcache_fetch(struct winbind_cache
*cache
,
522 struct winbindd_domain
*domain
,
523 const char *format
, ...)
527 struct cache_entry
*centry
;
529 refresh_sequence_number(domain
, False
);
531 va_start(ap
, format
);
532 smb_xvasprintf(&kstr
, format
, ap
);
535 centry
= wcache_fetch_raw(kstr
);
536 if (centry
== NULL
) {
541 if (centry_expired(domain
, kstr
, centry
)) {
543 DEBUG(10,("wcache_fetch: entry %s expired for domain %s\n",
544 kstr
, domain
->name
));
551 DEBUG(10,("wcache_fetch: returning entry %s for domain %s\n",
552 kstr
, domain
->name
));
559 make sure we have at least len bytes available in a centry
561 static void centry_expand(struct cache_entry
*centry
, uint32 len
)
563 if (centry
->len
- centry
->ofs
>= len
)
566 centry
->data
= SMB_REALLOC(centry
->data
, centry
->len
);
568 DEBUG(0,("out of memory: needed %d bytes in centry_expand\n", centry
->len
));
569 smb_panic("out of memory in centry_expand");
574 push a uint32 into a centry
576 static void centry_put_uint32(struct cache_entry
*centry
, uint32 v
)
578 centry_expand(centry
, 4);
579 SIVAL(centry
->data
, centry
->ofs
, v
);
584 push a uint16 into a centry
586 static void centry_put_uint16(struct cache_entry
*centry
, uint16 v
)
588 centry_expand(centry
, 2);
589 SIVAL(centry
->data
, centry
->ofs
, v
);
594 push a uint8 into a centry
596 static void centry_put_uint8(struct cache_entry
*centry
, uint8 v
)
598 centry_expand(centry
, 1);
599 SCVAL(centry
->data
, centry
->ofs
, v
);
604 push a string into a centry
606 static void centry_put_string(struct cache_entry
*centry
, const char *s
)
611 /* null strings are marked as len 0xFFFF */
612 centry_put_uint8(centry
, 0xFF);
617 /* can't handle more than 254 char strings. Truncating is probably best */
619 DEBUG(10,("centry_put_string: truncating len (%d) to: 254\n", len
));
622 centry_put_uint8(centry
, len
);
623 centry_expand(centry
, len
);
624 memcpy(centry
->data
+ centry
->ofs
, s
, len
);
628 static void centry_put_sid(struct cache_entry
*centry
, const DOM_SID
*sid
)
631 centry_put_string(centry
, sid_to_string(sid_string
, sid
));
635 push a NTTIME into a centry
637 static void centry_put_nttime(struct cache_entry
*centry
, NTTIME nt
)
639 centry_expand(centry
, 8);
640 SIVAL(centry
->data
, centry
->ofs
, nt
.low
);
642 SIVAL(centry
->data
, centry
->ofs
, nt
.high
);
647 push a time_t into a centry
649 static void centry_put_time(struct cache_entry
*centry
, time_t t
)
651 centry_expand(centry
, sizeof(time_t));
652 SIVAL(centry
->data
, centry
->ofs
, t
); /* FIXME: is this correct ?? */
653 centry
->ofs
+= sizeof(time_t);
657 start a centry for output. When finished, call centry_end()
659 struct cache_entry
*centry_start(struct winbindd_domain
*domain
, NTSTATUS status
)
661 struct cache_entry
*centry
;
666 centry
= SMB_XMALLOC_P(struct cache_entry
);
668 centry
->len
= 8192; /* reasonable default */
669 centry
->data
= SMB_XMALLOC_ARRAY(uint8
, centry
->len
);
671 centry
->sequence_number
= domain
->sequence_number
;
672 centry_put_uint32(centry
, NT_STATUS_V(status
));
673 centry_put_uint32(centry
, centry
->sequence_number
);
678 finish a centry and write it to the tdb
680 static void centry_end(struct cache_entry
*centry
, const char *format
, ...) PRINTF_ATTRIBUTE(2,3);
681 static void centry_end(struct cache_entry
*centry
, const char *format
, ...)
687 va_start(ap
, format
);
688 smb_xvasprintf(&kstr
, format
, ap
);
692 key
.dsize
= strlen(kstr
);
693 data
.dptr
= (char *)centry
->data
;
694 data
.dsize
= centry
->ofs
;
696 tdb_store(wcache
->tdb
, key
, data
, TDB_REPLACE
);
700 static void wcache_save_name_to_sid(struct winbindd_domain
*domain
,
701 NTSTATUS status
, const char *domain_name
,
702 const char *name
, const DOM_SID
*sid
,
703 enum SID_NAME_USE type
)
705 struct cache_entry
*centry
;
708 centry
= centry_start(domain
, status
);
711 centry_put_uint32(centry
, type
);
712 centry_put_sid(centry
, sid
);
713 fstrcpy(uname
, name
);
715 centry_end(centry
, "NS/%s/%s", domain_name
, uname
);
716 DEBUG(10,("wcache_save_name_to_sid: %s -> %s\n", uname
,
717 sid_string_static(sid
)));
721 static void wcache_save_sid_to_name(struct winbindd_domain
*domain
, NTSTATUS status
,
722 const DOM_SID
*sid
, const char *domain_name
, const char *name
, enum SID_NAME_USE type
)
724 struct cache_entry
*centry
;
727 centry
= centry_start(domain
, status
);
730 if (NT_STATUS_IS_OK(status
)) {
731 centry_put_uint32(centry
, type
);
732 centry_put_string(centry
, domain_name
);
733 centry_put_string(centry
, name
);
735 centry_end(centry
, "SN/%s", sid_to_string(sid_string
, sid
));
736 DEBUG(10,("wcache_save_sid_to_name: %s -> %s\n", sid_string
, name
));
741 static void wcache_save_user(struct winbindd_domain
*domain
, NTSTATUS status
, WINBIND_USERINFO
*info
)
743 struct cache_entry
*centry
;
746 centry
= centry_start(domain
, status
);
749 centry_put_string(centry
, info
->acct_name
);
750 centry_put_string(centry
, info
->full_name
);
751 centry_put_string(centry
, info
->homedir
);
752 centry_put_string(centry
, info
->shell
);
753 centry_put_sid(centry
, &info
->user_sid
);
754 centry_put_sid(centry
, &info
->group_sid
);
755 centry_end(centry
, "U/%s", sid_to_string(sid_string
, &info
->user_sid
));
756 DEBUG(10,("wcache_save_user: %s (acct_name %s)\n", sid_string
, info
->acct_name
));
760 static void wcache_save_lockout_policy(struct winbindd_domain
*domain
, NTSTATUS status
, SAM_UNK_INFO_12
*lockout_policy
)
762 struct cache_entry
*centry
;
764 centry
= centry_start(domain
, status
);
768 centry_put_nttime(centry
, lockout_policy
->duration
);
769 centry_put_nttime(centry
, lockout_policy
->reset_count
);
770 centry_put_uint16(centry
, lockout_policy
->bad_attempt_lockout
);
772 centry_end(centry
, "LOC_POL/%s", domain
->name
);
774 DEBUG(10,("wcache_save_lockout_policy: %s\n", domain
->name
));
779 static void wcache_save_password_policy(struct winbindd_domain
*domain
, NTSTATUS status
, SAM_UNK_INFO_1
*password_policy
)
781 struct cache_entry
*centry
;
783 centry
= centry_start(domain
, status
);
787 centry_put_uint16(centry
, password_policy
->min_length_password
);
788 centry_put_uint16(centry
, password_policy
->password_history
);
789 centry_put_uint32(centry
, password_policy
->password_properties
);
790 centry_put_nttime(centry
, password_policy
->expire
);
791 centry_put_nttime(centry
, password_policy
->min_passwordage
);
793 centry_end(centry
, "PWD_POL/%s", domain
->name
);
795 DEBUG(10,("wcache_save_password_policy: %s\n", domain
->name
));
800 NTSTATUS
wcache_cached_creds_exist(struct winbindd_domain
*domain
, const DOM_SID
*sid
)
802 struct winbind_cache
*cache
= get_cache(domain
);
807 return NT_STATUS_INTERNAL_DB_ERROR
;
810 fstr_sprintf(key_str
, "CRED/%s", sid_string_static(sid
));
812 data
= tdb_fetch(cache
->tdb
, make_tdb_data(key_str
, strlen(key_str
)));
814 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
820 /* Lookup creds for a SID */
821 NTSTATUS
wcache_get_creds(struct winbindd_domain
*domain
,
824 const uint8
**cached_nt_pass
)
826 struct winbind_cache
*cache
= get_cache(domain
);
827 struct cache_entry
*centry
= NULL
;
832 return NT_STATUS_INTERNAL_DB_ERROR
;
835 centry
= wcache_fetch(cache
, domain
, "CRED/%s", sid_string_static(sid
));
838 DEBUG(10,("wcache_get_creds: entry for [CRED/%s] not found\n",
839 sid_string_static(sid
)));
840 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
843 t
= centry_time(centry
);
844 *cached_nt_pass
= (const uint8
*)centry_string(centry
, mem_ctx
);
846 dump_data(10, (const char *)cached_nt_pass
, NT_HASH_LEN
);
847 status
= centry
->status
;
849 DEBUG(10,("wcache_get_creds: [Cached] - cached creds for user %s status %s\n",
850 sid_string_static(sid
), get_friendly_nt_error_msg(status
) ));
856 NTSTATUS
wcache_save_creds(struct winbindd_domain
*domain
,
859 const uint8 nt_pass
[NT_HASH_LEN
])
861 struct cache_entry
*centry
;
863 NTSTATUS status
= NT_STATUS_OK
; /* ??? */
865 centry
= centry_start(domain
, status
);
867 return NT_STATUS_INTERNAL_DB_ERROR
;
870 dump_data(100, (const char *)nt_pass
, NT_HASH_LEN
);
872 centry_put_time(centry
, time(NULL
));
873 centry_put_string(centry
, (const char *)nt_pass
);
874 centry_end(centry
, "CRED/%s", sid_to_string(sid_string
, sid
));
876 DEBUG(10,("wcache_save_creds: %s\n", sid_string
));
884 /* Query display info. This is the basic user list fn */
885 static NTSTATUS
query_user_list(struct winbindd_domain
*domain
,
888 WINBIND_USERINFO
**info
)
890 struct winbind_cache
*cache
= get_cache(domain
);
891 struct cache_entry
*centry
= NULL
;
893 unsigned int i
, retry
;
898 centry
= wcache_fetch(cache
, domain
, "UL/%s", domain
->name
);
902 *num_entries
= centry_uint32(centry
);
904 if (*num_entries
== 0)
907 (*info
) = TALLOC_ARRAY(mem_ctx
, WINBIND_USERINFO
, *num_entries
);
909 smb_panic("query_user_list out of memory");
910 for (i
=0; i
<(*num_entries
); i
++) {
911 (*info
)[i
].acct_name
= centry_string(centry
, mem_ctx
);
912 (*info
)[i
].full_name
= centry_string(centry
, mem_ctx
);
913 (*info
)[i
].homedir
= centry_string(centry
, mem_ctx
);
914 (*info
)[i
].shell
= centry_string(centry
, mem_ctx
);
915 centry_sid(centry
, &(*info
)[i
].user_sid
);
916 centry_sid(centry
, &(*info
)[i
].group_sid
);
920 status
= centry
->status
;
922 DEBUG(10,("query_user_list: [Cached] - cached list for domain %s status %s\n",
923 domain
->name
, get_friendly_nt_error_msg(status
) ));
932 /* Return status value returned by seq number check */
934 if (!NT_STATUS_IS_OK(domain
->last_status
))
935 return domain
->last_status
;
937 /* Put the query_user_list() in a retry loop. There appears to be
938 * some bug either with Windows 2000 or Samba's handling of large
939 * rpc replies. This manifests itself as sudden disconnection
940 * at a random point in the enumeration of a large (60k) user list.
941 * The retry loop simply tries the operation again. )-: It's not
942 * pretty but an acceptable workaround until we work out what the
943 * real problem is. */
948 DEBUG(10,("query_user_list: [Cached] - doing backend query for list for domain %s\n",
951 status
= domain
->backend
->query_user_list(domain
, mem_ctx
, num_entries
, info
);
952 if (!NT_STATUS_IS_OK(status
))
953 DEBUG(3, ("query_user_list: returned 0x%08x, "
954 "retrying\n", NT_STATUS_V(status
)));
955 if (NT_STATUS_EQUAL(status
, NT_STATUS_UNSUCCESSFUL
)) {
956 DEBUG(3, ("query_user_list: flushing "
957 "connection cache\n"));
958 invalidate_cm_connection(&domain
->conn
);
961 } while (NT_STATUS_V(status
) == NT_STATUS_V(NT_STATUS_UNSUCCESSFUL
) &&
965 refresh_sequence_number(domain
, False
);
966 centry
= centry_start(domain
, status
);
969 centry_put_uint32(centry
, *num_entries
);
970 for (i
=0; i
<(*num_entries
); i
++) {
971 centry_put_string(centry
, (*info
)[i
].acct_name
);
972 centry_put_string(centry
, (*info
)[i
].full_name
);
973 centry_put_string(centry
, (*info
)[i
].homedir
);
974 centry_put_string(centry
, (*info
)[i
].shell
);
975 centry_put_sid(centry
, &(*info
)[i
].user_sid
);
976 centry_put_sid(centry
, &(*info
)[i
].group_sid
);
977 if (domain
->backend
->consistent
) {
978 /* when the backend is consistent we can pre-prime some mappings */
979 wcache_save_name_to_sid(domain
, NT_STATUS_OK
,
981 (*info
)[i
].acct_name
,
982 &(*info
)[i
].user_sid
,
984 wcache_save_sid_to_name(domain
, NT_STATUS_OK
,
985 &(*info
)[i
].user_sid
,
987 (*info
)[i
].acct_name
,
989 wcache_save_user(domain
, NT_STATUS_OK
, &(*info
)[i
]);
992 centry_end(centry
, "UL/%s", domain
->name
);
999 /* list all domain groups */
1000 static NTSTATUS
enum_dom_groups(struct winbindd_domain
*domain
,
1001 TALLOC_CTX
*mem_ctx
,
1002 uint32
*num_entries
,
1003 struct acct_info
**info
)
1005 struct winbind_cache
*cache
= get_cache(domain
);
1006 struct cache_entry
*centry
= NULL
;
1013 centry
= wcache_fetch(cache
, domain
, "GL/%s/domain", domain
->name
);
1017 *num_entries
= centry_uint32(centry
);
1019 if (*num_entries
== 0)
1022 (*info
) = TALLOC_ARRAY(mem_ctx
, struct acct_info
, *num_entries
);
1024 smb_panic("enum_dom_groups out of memory");
1025 for (i
=0; i
<(*num_entries
); i
++) {
1026 fstrcpy((*info
)[i
].acct_name
, centry_string(centry
, mem_ctx
));
1027 fstrcpy((*info
)[i
].acct_desc
, centry_string(centry
, mem_ctx
));
1028 (*info
)[i
].rid
= centry_uint32(centry
);
1032 status
= centry
->status
;
1034 DEBUG(10,("enum_dom_groups: [Cached] - cached list for domain %s status %s\n",
1035 domain
->name
, get_friendly_nt_error_msg(status
) ));
1037 centry_free(centry
);
1044 /* Return status value returned by seq number check */
1046 if (!NT_STATUS_IS_OK(domain
->last_status
))
1047 return domain
->last_status
;
1049 DEBUG(10,("enum_dom_groups: [Cached] - doing backend query for list for domain %s\n",
1052 status
= domain
->backend
->enum_dom_groups(domain
, mem_ctx
, num_entries
, info
);
1055 refresh_sequence_number(domain
, False
);
1056 centry
= centry_start(domain
, status
);
1059 centry_put_uint32(centry
, *num_entries
);
1060 for (i
=0; i
<(*num_entries
); i
++) {
1061 centry_put_string(centry
, (*info
)[i
].acct_name
);
1062 centry_put_string(centry
, (*info
)[i
].acct_desc
);
1063 centry_put_uint32(centry
, (*info
)[i
].rid
);
1065 centry_end(centry
, "GL/%s/domain", domain
->name
);
1066 centry_free(centry
);
1072 /* list all domain groups */
1073 static NTSTATUS
enum_local_groups(struct winbindd_domain
*domain
,
1074 TALLOC_CTX
*mem_ctx
,
1075 uint32
*num_entries
,
1076 struct acct_info
**info
)
1078 struct winbind_cache
*cache
= get_cache(domain
);
1079 struct cache_entry
*centry
= NULL
;
1086 centry
= wcache_fetch(cache
, domain
, "GL/%s/local", domain
->name
);
1090 *num_entries
= centry_uint32(centry
);
1092 if (*num_entries
== 0)
1095 (*info
) = TALLOC_ARRAY(mem_ctx
, struct acct_info
, *num_entries
);
1097 smb_panic("enum_dom_groups out of memory");
1098 for (i
=0; i
<(*num_entries
); i
++) {
1099 fstrcpy((*info
)[i
].acct_name
, centry_string(centry
, mem_ctx
));
1100 fstrcpy((*info
)[i
].acct_desc
, centry_string(centry
, mem_ctx
));
1101 (*info
)[i
].rid
= centry_uint32(centry
);
1106 /* If we are returning cached data and the domain controller
1107 is down then we don't know whether the data is up to date
1108 or not. Return NT_STATUS_MORE_PROCESSING_REQUIRED to
1111 if (wcache_server_down(domain
)) {
1112 DEBUG(10, ("enum_local_groups: returning cached user list and server was down\n"));
1113 status
= NT_STATUS_MORE_PROCESSING_REQUIRED
;
1115 status
= centry
->status
;
1117 DEBUG(10,("enum_local_groups: [Cached] - cached list for domain %s status %s\n",
1118 domain
->name
, get_friendly_nt_error_msg(status
) ));
1120 centry_free(centry
);
1127 /* Return status value returned by seq number check */
1129 if (!NT_STATUS_IS_OK(domain
->last_status
))
1130 return domain
->last_status
;
1132 DEBUG(10,("enum_local_groups: [Cached] - doing backend query for list for domain %s\n",
1135 status
= domain
->backend
->enum_local_groups(domain
, mem_ctx
, num_entries
, info
);
1138 refresh_sequence_number(domain
, False
);
1139 centry
= centry_start(domain
, status
);
1142 centry_put_uint32(centry
, *num_entries
);
1143 for (i
=0; i
<(*num_entries
); i
++) {
1144 centry_put_string(centry
, (*info
)[i
].acct_name
);
1145 centry_put_string(centry
, (*info
)[i
].acct_desc
);
1146 centry_put_uint32(centry
, (*info
)[i
].rid
);
1148 centry_end(centry
, "GL/%s/local", domain
->name
);
1149 centry_free(centry
);
1155 /* convert a single name to a sid in a domain */
1156 static NTSTATUS
name_to_sid(struct winbindd_domain
*domain
,
1157 TALLOC_CTX
*mem_ctx
,
1158 const char *domain_name
,
1161 enum SID_NAME_USE
*type
)
1163 struct winbind_cache
*cache
= get_cache(domain
);
1164 struct cache_entry
*centry
= NULL
;
1171 fstrcpy(uname
, name
);
1173 centry
= wcache_fetch(cache
, domain
, "NS/%s/%s", domain_name
, uname
);
1176 *type
= (enum SID_NAME_USE
)centry_uint32(centry
);
1177 status
= centry
->status
;
1178 if (NT_STATUS_IS_OK(status
)) {
1179 centry_sid(centry
, sid
);
1182 DEBUG(10,("name_to_sid: [Cached] - cached name for domain %s status %s\n",
1183 domain
->name
, get_friendly_nt_error_msg(status
) ));
1185 centry_free(centry
);
1191 /* If the seq number check indicated that there is a problem
1192 * with this DC, then return that status... except for
1193 * access_denied. This is special because the dc may be in
1194 * "restrict anonymous = 1" mode, in which case it will deny
1195 * most unauthenticated operations, but *will* allow the LSA
1196 * name-to-sid that we try as a fallback. */
1198 if (!(NT_STATUS_IS_OK(domain
->last_status
)
1199 || NT_STATUS_EQUAL(domain
->last_status
, NT_STATUS_ACCESS_DENIED
)))
1200 return domain
->last_status
;
1202 DEBUG(10,("name_to_sid: [Cached] - doing backend query for name for domain %s\n",
1205 status
= domain
->backend
->name_to_sid(domain
, mem_ctx
, domain_name
, name
, sid
, type
);
1208 if (domain
->online
|| !is_null_sid(sid
)) {
1209 wcache_save_name_to_sid(domain
, status
, domain_name
, name
, sid
, *type
);
1212 if (NT_STATUS_IS_OK(status
)) {
1213 strupper_m(CONST_DISCARD(char *,domain_name
));
1214 strlower_m(CONST_DISCARD(char *,name
));
1215 wcache_save_sid_to_name(domain
, status
, sid
, domain_name
, name
, *type
);
1221 /* convert a sid to a user or group name. The sid is guaranteed to be in the domain
1223 static NTSTATUS
sid_to_name(struct winbindd_domain
*domain
,
1224 TALLOC_CTX
*mem_ctx
,
1228 enum SID_NAME_USE
*type
)
1230 struct winbind_cache
*cache
= get_cache(domain
);
1231 struct cache_entry
*centry
= NULL
;
1238 centry
= wcache_fetch(cache
, domain
, "SN/%s", sid_to_string(sid_string
, sid
));
1241 if (NT_STATUS_IS_OK(centry
->status
)) {
1242 *type
= (enum SID_NAME_USE
)centry_uint32(centry
);
1243 *domain_name
= centry_string(centry
, mem_ctx
);
1244 *name
= centry_string(centry
, mem_ctx
);
1246 status
= centry
->status
;
1248 DEBUG(10,("sid_to_name: [Cached] - cached name for domain %s status %s\n",
1249 domain
->name
, get_friendly_nt_error_msg(status
) ));
1251 centry_free(centry
);
1256 *domain_name
= NULL
;
1258 /* If the seq number check indicated that there is a problem
1259 * with this DC, then return that status... except for
1260 * access_denied. This is special because the dc may be in
1261 * "restrict anonymous = 1" mode, in which case it will deny
1262 * most unauthenticated operations, but *will* allow the LSA
1263 * sid-to-name that we try as a fallback. */
1265 if (!(NT_STATUS_IS_OK(domain
->last_status
)
1266 || NT_STATUS_EQUAL(domain
->last_status
, NT_STATUS_ACCESS_DENIED
)))
1267 return domain
->last_status
;
1269 DEBUG(10,("sid_to_name: [Cached] - doing backend query for name for domain %s\n",
1272 status
= domain
->backend
->sid_to_name(domain
, mem_ctx
, sid
, domain_name
, name
, type
);
1275 refresh_sequence_number(domain
, False
);
1276 wcache_save_sid_to_name(domain
, status
, sid
, *domain_name
, *name
, *type
);
1278 /* We can't save the name to sid mapping here, as with sid history a
1279 * later name2sid would give the wrong sid. */
1284 /* Lookup user information from a rid */
1285 static NTSTATUS
query_user(struct winbindd_domain
*domain
,
1286 TALLOC_CTX
*mem_ctx
,
1287 const DOM_SID
*user_sid
,
1288 WINBIND_USERINFO
*info
)
1290 struct winbind_cache
*cache
= get_cache(domain
);
1291 struct cache_entry
*centry
= NULL
;
1297 centry
= wcache_fetch(cache
, domain
, "U/%s", sid_string_static(user_sid
));
1299 /* If we have an access denied cache entry and a cached info3 in the
1300 samlogon cache then do a query. This will force the rpc back end
1301 to return the info3 data. */
1303 if (NT_STATUS_V(domain
->last_status
) == NT_STATUS_V(NT_STATUS_ACCESS_DENIED
) &&
1304 netsamlogon_cache_have(user_sid
)) {
1305 DEBUG(10, ("query_user: cached access denied and have cached info3\n"));
1306 domain
->last_status
= NT_STATUS_OK
;
1307 centry_free(centry
);
1314 info
->acct_name
= centry_string(centry
, mem_ctx
);
1315 info
->full_name
= centry_string(centry
, mem_ctx
);
1316 info
->homedir
= centry_string(centry
, mem_ctx
);
1317 info
->shell
= centry_string(centry
, mem_ctx
);
1318 centry_sid(centry
, &info
->user_sid
);
1319 centry_sid(centry
, &info
->group_sid
);
1320 status
= centry
->status
;
1322 DEBUG(10,("query_user: [Cached] - cached info for domain %s status %s\n",
1323 domain
->name
, get_friendly_nt_error_msg(status
) ));
1325 centry_free(centry
);
1331 /* Return status value returned by seq number check */
1333 if (!NT_STATUS_IS_OK(domain
->last_status
))
1334 return domain
->last_status
;
1336 DEBUG(10,("sid_to_name: [Cached] - doing backend query for info for domain %s\n",
1339 status
= domain
->backend
->query_user(domain
, mem_ctx
, user_sid
, info
);
1342 refresh_sequence_number(domain
, False
);
1343 wcache_save_user(domain
, status
, info
);
1349 /* Lookup groups a user is a member of. */
1350 static NTSTATUS
lookup_usergroups(struct winbindd_domain
*domain
,
1351 TALLOC_CTX
*mem_ctx
,
1352 const DOM_SID
*user_sid
,
1353 uint32
*num_groups
, DOM_SID
**user_gids
)
1355 struct winbind_cache
*cache
= get_cache(domain
);
1356 struct cache_entry
*centry
= NULL
;
1364 centry
= wcache_fetch(cache
, domain
, "UG/%s", sid_to_string(sid_string
, user_sid
));
1366 /* If we have an access denied cache entry and a cached info3 in the
1367 samlogon cache then do a query. This will force the rpc back end
1368 to return the info3 data. */
1370 if (NT_STATUS_V(domain
->last_status
) == NT_STATUS_V(NT_STATUS_ACCESS_DENIED
) &&
1371 netsamlogon_cache_have(user_sid
)) {
1372 DEBUG(10, ("lookup_usergroups: cached access denied and have cached info3\n"));
1373 domain
->last_status
= NT_STATUS_OK
;
1374 centry_free(centry
);
1381 *num_groups
= centry_uint32(centry
);
1383 if (*num_groups
== 0)
1386 (*user_gids
) = TALLOC_ARRAY(mem_ctx
, DOM_SID
, *num_groups
);
1388 smb_panic("lookup_usergroups out of memory");
1389 for (i
=0; i
<(*num_groups
); i
++) {
1390 centry_sid(centry
, &(*user_gids
)[i
]);
1394 status
= centry
->status
;
1396 DEBUG(10,("lookup_usergroups: [Cached] - cached info for domain %s status %s\n",
1397 domain
->name
, get_friendly_nt_error_msg(status
) ));
1399 centry_free(centry
);
1404 (*user_gids
) = NULL
;
1406 /* Return status value returned by seq number check */
1408 if (!NT_STATUS_IS_OK(domain
->last_status
))
1409 return domain
->last_status
;
1411 DEBUG(10,("lookup_usergroups: [Cached] - doing backend query for info for domain %s\n",
1414 status
= domain
->backend
->lookup_usergroups(domain
, mem_ctx
, user_sid
, num_groups
, user_gids
);
1417 refresh_sequence_number(domain
, False
);
1418 centry
= centry_start(domain
, status
);
1421 centry_put_uint32(centry
, *num_groups
);
1422 for (i
=0; i
<(*num_groups
); i
++) {
1423 centry_put_sid(centry
, &(*user_gids
)[i
]);
1425 centry_end(centry
, "UG/%s", sid_to_string(sid_string
, user_sid
));
1426 centry_free(centry
);
1432 static NTSTATUS
lookup_useraliases(struct winbindd_domain
*domain
,
1433 TALLOC_CTX
*mem_ctx
,
1434 uint32 num_sids
, const DOM_SID
*sids
,
1435 uint32
*num_aliases
, uint32
**alias_rids
)
1437 struct winbind_cache
*cache
= get_cache(domain
);
1438 struct cache_entry
*centry
= NULL
;
1440 char *sidlist
= talloc_strdup(mem_ctx
, "");
1446 if (num_sids
== 0) {
1449 return NT_STATUS_OK
;
1452 /* We need to cache indexed by the whole list of SIDs, the aliases
1453 * resulting might come from any of the SIDs. */
1455 for (i
=0; i
<num_sids
; i
++) {
1456 sidlist
= talloc_asprintf(mem_ctx
, "%s/%s", sidlist
,
1457 sid_string_static(&sids
[i
]));
1458 if (sidlist
== NULL
)
1459 return NT_STATUS_NO_MEMORY
;
1462 centry
= wcache_fetch(cache
, domain
, "UA%s", sidlist
);
1467 *num_aliases
= centry_uint32(centry
);
1470 (*alias_rids
) = TALLOC_ARRAY(mem_ctx
, uint32
, *num_aliases
);
1472 if ((*num_aliases
!= 0) && ((*alias_rids
) == NULL
)) {
1473 centry_free(centry
);
1474 return NT_STATUS_NO_MEMORY
;
1477 for (i
=0; i
<(*num_aliases
); i
++)
1478 (*alias_rids
)[i
] = centry_uint32(centry
);
1480 status
= centry
->status
;
1482 DEBUG(10,("lookup_useraliases: [Cached] - cached info for domain %s "
1483 "status %s\n", domain
->name
,
1484 get_friendly_nt_error_msg(status
)));
1486 centry_free(centry
);
1491 (*alias_rids
) = NULL
;
1493 if (!NT_STATUS_IS_OK(domain
->last_status
))
1494 return domain
->last_status
;
1496 DEBUG(10,("lookup_usergroups: [Cached] - doing backend query for info "
1497 "for domain %s\n", domain
->name
));
1499 status
= domain
->backend
->lookup_useraliases(domain
, mem_ctx
,
1501 num_aliases
, alias_rids
);
1504 refresh_sequence_number(domain
, False
);
1505 centry
= centry_start(domain
, status
);
1508 centry_put_uint32(centry
, *num_aliases
);
1509 for (i
=0; i
<(*num_aliases
); i
++)
1510 centry_put_uint32(centry
, (*alias_rids
)[i
]);
1511 centry_end(centry
, "UA%s", sidlist
);
1512 centry_free(centry
);
1519 static NTSTATUS
lookup_groupmem(struct winbindd_domain
*domain
,
1520 TALLOC_CTX
*mem_ctx
,
1521 const DOM_SID
*group_sid
, uint32
*num_names
,
1522 DOM_SID
**sid_mem
, char ***names
,
1523 uint32
**name_types
)
1525 struct winbind_cache
*cache
= get_cache(domain
);
1526 struct cache_entry
*centry
= NULL
;
1534 centry
= wcache_fetch(cache
, domain
, "GM/%s", sid_to_string(sid_string
, group_sid
));
1538 *num_names
= centry_uint32(centry
);
1540 if (*num_names
== 0)
1543 (*sid_mem
) = TALLOC_ARRAY(mem_ctx
, DOM_SID
, *num_names
);
1544 (*names
) = TALLOC_ARRAY(mem_ctx
, char *, *num_names
);
1545 (*name_types
) = TALLOC_ARRAY(mem_ctx
, uint32
, *num_names
);
1547 if (! (*sid_mem
) || ! (*names
) || ! (*name_types
)) {
1548 smb_panic("lookup_groupmem out of memory");
1551 for (i
=0; i
<(*num_names
); i
++) {
1552 centry_sid(centry
, &(*sid_mem
)[i
]);
1553 (*names
)[i
] = centry_string(centry
, mem_ctx
);
1554 (*name_types
)[i
] = centry_uint32(centry
);
1558 status
= centry
->status
;
1560 DEBUG(10,("lookup_groupmem: [Cached] - cached info for domain %s status %s\n",
1561 domain
->name
, get_friendly_nt_error_msg(status
) ));
1563 centry_free(centry
);
1570 (*name_types
) = NULL
;
1572 /* Return status value returned by seq number check */
1574 if (!NT_STATUS_IS_OK(domain
->last_status
))
1575 return domain
->last_status
;
1577 DEBUG(10,("lookup_groupmem: [Cached] - doing backend query for info for domain %s\n",
1580 status
= domain
->backend
->lookup_groupmem(domain
, mem_ctx
, group_sid
, num_names
,
1581 sid_mem
, names
, name_types
);
1584 refresh_sequence_number(domain
, False
);
1585 centry
= centry_start(domain
, status
);
1588 centry_put_uint32(centry
, *num_names
);
1589 for (i
=0; i
<(*num_names
); i
++) {
1590 centry_put_sid(centry
, &(*sid_mem
)[i
]);
1591 centry_put_string(centry
, (*names
)[i
]);
1592 centry_put_uint32(centry
, (*name_types
)[i
]);
1594 centry_end(centry
, "GM/%s", sid_to_string(sid_string
, group_sid
));
1595 centry_free(centry
);
1601 /* find the sequence number for a domain */
1602 static NTSTATUS
sequence_number(struct winbindd_domain
*domain
, uint32
*seq
)
1604 refresh_sequence_number(domain
, False
);
1606 *seq
= domain
->sequence_number
;
1608 return NT_STATUS_OK
;
1611 /* enumerate trusted domains
1612 * (we need to have the list of trustdoms in the cache when we go offline) -
1614 static NTSTATUS
trusted_domains(struct winbindd_domain
*domain
,
1615 TALLOC_CTX
*mem_ctx
,
1616 uint32
*num_domains
,
1621 struct winbind_cache
*cache
= get_cache(domain
);
1622 struct cache_entry
*centry
= NULL
;
1629 centry
= wcache_fetch(cache
, domain
, "TRUSTDOMS/%s", domain
->name
);
1635 *num_domains
= centry_uint32(centry
);
1637 (*names
) = TALLOC_ARRAY(mem_ctx
, char *, *num_domains
);
1638 (*alt_names
) = TALLOC_ARRAY(mem_ctx
, char *, *num_domains
);
1639 (*dom_sids
) = TALLOC_ARRAY(mem_ctx
, DOM_SID
, *num_domains
);
1641 if (! (*dom_sids
) || ! (*names
) || ! (*alt_names
)) {
1642 smb_panic("trusted_domains out of memory");
1645 for (i
=0; i
<(*num_domains
); i
++) {
1646 (*names
)[i
] = centry_string(centry
, mem_ctx
);
1647 (*alt_names
)[i
] = centry_string(centry
, mem_ctx
);
1648 centry_sid(centry
, &(*dom_sids
)[i
]);
1651 status
= centry
->status
;
1653 DEBUG(10,("trusted_domains: [Cached] - cached info for domain %s (%d trusts) status %s\n",
1654 domain
->name
, *num_domains
, get_friendly_nt_error_msg(status
) ));
1656 centry_free(centry
);
1663 (*alt_names
) = NULL
;
1665 /* Return status value returned by seq number check */
1667 if (!NT_STATUS_IS_OK(domain
->last_status
))
1668 return domain
->last_status
;
1670 DEBUG(10,("trusted_domains: [Cached] - doing backend query for info for domain %s\n",
1673 status
= domain
->backend
->trusted_domains(domain
, mem_ctx
, num_domains
,
1674 names
, alt_names
, dom_sids
);
1676 /* no trusts gives NT_STATUS_NO_MORE_ENTRIES resetting to NT_STATUS_OK
1677 * so that the generic centry handling still applies correctly -
1680 if (!NT_STATUS_IS_ERR(status
)) {
1681 status
= NT_STATUS_OK
;
1685 refresh_sequence_number(domain
, False
);
1687 centry
= centry_start(domain
, status
);
1691 centry_put_uint32(centry
, *num_domains
);
1693 for (i
=0; i
<(*num_domains
); i
++) {
1694 centry_put_string(centry
, (*names
)[i
]);
1695 centry_put_string(centry
, (*alt_names
)[i
]);
1696 centry_put_sid(centry
, &(*dom_sids
)[i
]);
1699 centry_end(centry
, "TRUSTDOMS/%s", domain
->name
);
1701 centry_free(centry
);
1707 /* get lockout policy */
1708 static NTSTATUS
lockout_policy(struct winbindd_domain
*domain
,
1709 TALLOC_CTX
*mem_ctx
,
1710 SAM_UNK_INFO_12
*lockout_policy
){
1711 struct winbind_cache
*cache
= get_cache(domain
);
1712 struct cache_entry
*centry
= NULL
;
1718 centry
= wcache_fetch(cache
, domain
, "LOC_POL/%s", domain
->name
);
1723 lockout_policy
->duration
= centry_nttime(centry
);
1724 lockout_policy
->reset_count
= centry_nttime(centry
);
1725 lockout_policy
->bad_attempt_lockout
= centry_uint16(centry
);
1727 status
= centry
->status
;
1729 DEBUG(10,("lockout_policy: [Cached] - cached info for domain %s status %s\n",
1730 domain
->name
, get_friendly_nt_error_msg(status
) ));
1732 centry_free(centry
);
1736 ZERO_STRUCTP(lockout_policy
);
1738 /* Return status value returned by seq number check */
1740 if (!NT_STATUS_IS_OK(domain
->last_status
))
1741 return domain
->last_status
;
1743 DEBUG(10,("lockout_policy: [Cached] - doing backend query for info for domain %s\n",
1746 status
= domain
->backend
->lockout_policy(domain
, mem_ctx
, lockout_policy
);
1749 refresh_sequence_number(domain
, False
);
1750 wcache_save_lockout_policy(domain
, status
, lockout_policy
);
1755 /* get password policy */
1756 static NTSTATUS
password_policy(struct winbindd_domain
*domain
,
1757 TALLOC_CTX
*mem_ctx
,
1758 SAM_UNK_INFO_1
*password_policy
)
1760 struct winbind_cache
*cache
= get_cache(domain
);
1761 struct cache_entry
*centry
= NULL
;
1767 centry
= wcache_fetch(cache
, domain
, "PWD_POL/%s", domain
->name
);
1772 password_policy
->min_length_password
= centry_uint16(centry
);
1773 password_policy
->password_history
= centry_uint16(centry
);
1774 password_policy
->password_properties
= centry_uint32(centry
);
1775 password_policy
->expire
= centry_nttime(centry
);
1776 password_policy
->min_passwordage
= centry_nttime(centry
);
1778 status
= centry
->status
;
1780 DEBUG(10,("lockout_policy: [Cached] - cached info for domain %s status %s\n",
1781 domain
->name
, get_friendly_nt_error_msg(status
) ));
1783 centry_free(centry
);
1787 ZERO_STRUCTP(password_policy
);
1789 /* Return status value returned by seq number check */
1791 if (!NT_STATUS_IS_OK(domain
->last_status
))
1792 return domain
->last_status
;
1794 DEBUG(10,("password_policy: [Cached] - doing backend query for info for domain %s\n",
1797 status
= domain
->backend
->password_policy(domain
, mem_ctx
, password_policy
);
1800 refresh_sequence_number(domain
, False
);
1801 wcache_save_password_policy(domain
, status
, password_policy
);
1807 /* Invalidate cached user and group lists coherently */
1809 static int traverse_fn(TDB_CONTEXT
*the_tdb
, TDB_DATA kbuf
, TDB_DATA dbuf
,
1812 if (strncmp(kbuf
.dptr
, "UL/", 3) == 0 ||
1813 strncmp(kbuf
.dptr
, "GL/", 3) == 0)
1814 tdb_delete(the_tdb
, kbuf
);
1819 /* Invalidate the getpwnam and getgroups entries for a winbindd domain */
1821 void wcache_invalidate_samlogon(struct winbindd_domain
*domain
,
1822 NET_USER_INFO_3
*info3
)
1824 struct winbind_cache
*cache
;
1829 cache
= get_cache(domain
);
1830 netsamlogon_clear_cached_user(cache
->tdb
, info3
);
1833 void wcache_invalidate_cache(void)
1835 struct winbindd_domain
*domain
;
1837 for (domain
= domain_list(); domain
; domain
= domain
->next
) {
1838 struct winbind_cache
*cache
= get_cache(domain
);
1840 DEBUG(10, ("wcache_invalidate_cache: invalidating cache "
1841 "entries for %s\n", domain
->name
));
1843 tdb_traverse(cache
->tdb
, traverse_fn
, NULL
);
1847 static BOOL
init_wcache(void)
1849 if (wcache
== NULL
) {
1850 wcache
= SMB_XMALLOC_P(struct winbind_cache
);
1851 ZERO_STRUCTP(wcache
);
1854 if (wcache
->tdb
!= NULL
)
1857 /* when working offline we must not clear the cache on restart */
1858 wcache
->tdb
= tdb_open_log(lock_path("winbindd_cache.tdb"),
1859 WINBINDD_CACHE_TDB_DEFAULT_HASH_SIZE
,
1860 TDB_DEFAULT
/*TDB_CLEAR_IF_FIRST*/, O_RDWR
|O_CREAT
, 0600);
1862 if (wcache
->tdb
== NULL
) {
1863 DEBUG(0,("Failed to open winbindd_cache.tdb!\n"));
1870 void cache_store_response(pid_t pid
, struct winbindd_response
*response
)
1877 DEBUG(10, ("Storing response for pid %d, len %d\n",
1878 pid
, response
->length
));
1880 fstr_sprintf(key_str
, "DR/%d", pid
);
1881 if (tdb_store(wcache
->tdb
, string_tdb_data(key_str
),
1882 make_tdb_data((void *)response
, sizeof(*response
)),
1886 if (response
->length
== sizeof(*response
))
1889 /* There's extra data */
1891 DEBUG(10, ("Storing extra data: len=%d\n",
1892 (int)(response
->length
- sizeof(*response
))));
1894 fstr_sprintf(key_str
, "DE/%d", pid
);
1895 if (tdb_store(wcache
->tdb
, string_tdb_data(key_str
),
1896 make_tdb_data(response
->extra_data
,
1897 response
->length
- sizeof(*response
)),
1901 /* We could not store the extra data, make sure the tdb does not
1902 * contain a main record with wrong dangling extra data */
1904 fstr_sprintf(key_str
, "DR/%d", pid
);
1905 tdb_delete(wcache
->tdb
, string_tdb_data(key_str
));
1910 BOOL
cache_retrieve_response(pid_t pid
, struct winbindd_response
* response
)
1918 DEBUG(10, ("Retrieving response for pid %d\n", pid
));
1920 fstr_sprintf(key_str
, "DR/%d", pid
);
1921 data
= tdb_fetch(wcache
->tdb
, string_tdb_data(key_str
));
1923 if (data
.dptr
== NULL
)
1926 if (data
.dsize
!= sizeof(*response
))
1929 memcpy(response
, data
.dptr
, data
.dsize
);
1930 SAFE_FREE(data
.dptr
);
1932 if (response
->length
== sizeof(*response
)) {
1933 response
->extra_data
= NULL
;
1937 /* There's extra data */
1939 DEBUG(10, ("Retrieving extra data length=%d\n",
1940 (int)(response
->length
- sizeof(*response
))));
1942 fstr_sprintf(key_str
, "DE/%d", pid
);
1943 data
= tdb_fetch(wcache
->tdb
, string_tdb_data(key_str
));
1945 if (data
.dptr
== NULL
) {
1946 DEBUG(0, ("Did not find extra data\n"));
1950 if (data
.dsize
!= (response
->length
- sizeof(*response
))) {
1951 DEBUG(0, ("Invalid extra data length: %d\n", (int)data
.dsize
));
1952 SAFE_FREE(data
.dptr
);
1956 dump_data(11, data
.dptr
, data
.dsize
);
1958 response
->extra_data
= data
.dptr
;
1962 void cache_cleanup_response(pid_t pid
)
1969 fstr_sprintf(key_str
, "DR/%d", pid
);
1970 tdb_delete(wcache
->tdb
, string_tdb_data(key_str
));
1972 fstr_sprintf(key_str
, "DE/%d", pid
);
1973 tdb_delete(wcache
->tdb
, string_tdb_data(key_str
));
1979 BOOL
lookup_cached_sid(TALLOC_CTX
*mem_ctx
, const DOM_SID
*sid
,
1980 const char **domain_name
, const char **name
,
1981 enum SID_NAME_USE
*type
)
1983 struct winbindd_domain
*domain
;
1984 struct winbind_cache
*cache
;
1985 struct cache_entry
*centry
= NULL
;
1988 domain
= find_lookup_domain_from_sid(sid
);
1989 if (domain
== NULL
) {
1993 cache
= get_cache(domain
);
1995 if (cache
->tdb
== NULL
) {
1999 centry
= wcache_fetch(cache
, domain
, "SN/%s", sid_string_static(sid
));
2000 if (centry
== NULL
) {
2004 if (NT_STATUS_IS_OK(centry
->status
)) {
2005 *type
= (enum SID_NAME_USE
)centry_uint32(centry
);
2006 *domain_name
= centry_string(centry
, mem_ctx
);
2007 *name
= centry_string(centry
, mem_ctx
);
2010 status
= centry
->status
;
2011 centry_free(centry
);
2012 return NT_STATUS_IS_OK(status
);
2015 BOOL
lookup_cached_name(TALLOC_CTX
*mem_ctx
,
2016 const char *domain_name
,
2019 enum SID_NAME_USE
*type
)
2021 struct winbindd_domain
*domain
;
2022 struct winbind_cache
*cache
;
2023 struct cache_entry
*centry
= NULL
;
2027 domain
= find_lookup_domain_from_name(domain_name
);
2028 if (domain
== NULL
) {
2032 cache
= get_cache(domain
);
2034 if (cache
->tdb
== NULL
) {
2038 fstrcpy(uname
, name
);
2041 centry
= wcache_fetch(cache
, domain
, "NS/%s/%s", domain_name
, uname
);
2042 if (centry
== NULL
) {
2046 if (NT_STATUS_IS_OK(centry
->status
)) {
2047 *type
= (enum SID_NAME_USE
)centry_uint32(centry
);
2048 centry_sid(centry
, sid
);
2051 status
= centry
->status
;
2052 centry_free(centry
);
2054 return NT_STATUS_IS_OK(status
);
2057 void cache_name2sid(struct winbindd_domain
*domain
,
2058 const char *domain_name
, const char *name
,
2059 enum SID_NAME_USE type
, const DOM_SID
*sid
)
2061 wcache_save_name_to_sid(domain
, NT_STATUS_OK
, domain_name
, name
,
2065 /* delete all centries that don't have NT_STATUS_OK set */
2066 static int traverse_fn_cleanup(TDB_CONTEXT
*the_tdb
, TDB_DATA kbuf
,
2067 TDB_DATA dbuf
, void *state
)
2069 struct cache_entry
*centry
;
2072 if (!snprintf(buf
, kbuf
.dsize
+ 1, "%s", kbuf
.dptr
)) {
2076 centry
= wcache_fetch_raw(buf
);
2081 if (!NT_STATUS_IS_OK(centry
->status
)) {
2082 DEBUG(10,("deleting centry %s\n", buf
));
2083 tdb_delete(the_tdb
, kbuf
);
2086 centry_free(centry
);
2090 /* flush the cache */
2091 void wcache_flush_cache(void)
2093 extern BOOL opt_nocache
;
2098 tdb_close(wcache
->tdb
);
2104 /* when working offline we must not clear the cache on restart */
2105 wcache
->tdb
= tdb_open_log(lock_path("winbindd_cache.tdb"),
2106 WINBINDD_CACHE_TDB_DEFAULT_HASH_SIZE
,
2107 TDB_DEFAULT
/* TDB_CLEAR_IF_FIRST */, O_RDWR
|O_CREAT
, 0600);
2110 DEBUG(0,("Failed to open winbindd_cache.tdb!\n"));
2113 tdb_traverse(wcache
->tdb
, traverse_fn_cleanup
, NULL
);
2115 DEBUG(10,("wcache_flush_cache success\n"));
2118 /* Count cached creds */
2120 static int traverse_fn_cached_creds(TDB_CONTEXT
*the_tdb
, TDB_DATA kbuf
, TDB_DATA dbuf
,
2123 int *cred_count
= (int*)state
;
2125 if (strncmp(kbuf
.dptr
, "CRED/", 5) == 0) {
2131 NTSTATUS
wcache_count_cached_creds(struct winbindd_domain
*domain
, int *count
)
2133 struct winbind_cache
*cache
= get_cache(domain
);
2138 return NT_STATUS_INTERNAL_DB_ERROR
;
2141 tdb_traverse(cache
->tdb
, traverse_fn_cached_creds
, (void *)count
);
2143 return NT_STATUS_OK
;
2147 struct cred_list
*prev
, *next
;
2152 static struct cred_list
*wcache_cred_list
;
2154 static int traverse_fn_get_credlist(TDB_CONTEXT
*the_tdb
, TDB_DATA kbuf
, TDB_DATA dbuf
,
2157 struct cred_list
*cred
;
2159 if (strncmp(kbuf
.dptr
, "CRED/", 5) == 0) {
2161 cred
= SMB_MALLOC_P(struct cred_list
);
2163 DEBUG(0,("traverse_fn_remove_first_creds: failed to malloc new entry for list\n"));
2169 /* save a copy of the key */
2171 fstrcpy(cred
->name
, kbuf
.dptr
);
2172 DLIST_ADD(wcache_cred_list
, cred
);
2178 NTSTATUS
wcache_remove_oldest_cached_creds(struct winbindd_domain
*domain
, const DOM_SID
*sid
)
2180 struct winbind_cache
*cache
= get_cache(domain
);
2183 struct cred_list
*cred
, *oldest
= NULL
;
2186 return NT_STATUS_INTERNAL_DB_ERROR
;
2189 /* we possibly already have an entry */
2190 if (sid
&& NT_STATUS_IS_OK(wcache_cached_creds_exist(domain
, sid
))) {
2194 DEBUG(11,("we already have an entry, deleting that\n"));
2196 fstr_sprintf(key_str
, "CRED/%s", sid_string_static(sid
));
2198 tdb_delete(cache
->tdb
, string_tdb_data(key_str
));
2200 return NT_STATUS_OK
;
2203 ret
= tdb_traverse(cache
->tdb
, traverse_fn_get_credlist
, NULL
);
2205 return NT_STATUS_OK
;
2206 } else if (ret
== -1) {
2207 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
2210 ZERO_STRUCTP(oldest
);
2212 for (cred
= wcache_cred_list
; cred
; cred
= cred
->next
) {
2217 data
= tdb_fetch(cache
->tdb
, make_tdb_data(cred
->name
, strlen(cred
->name
)));
2219 DEBUG(10,("wcache_remove_oldest_cached_creds: entry for [%s] not found\n",
2221 status
= NT_STATUS_OBJECT_NAME_NOT_FOUND
;
2225 t
= IVAL(data
.dptr
, 0);
2226 SAFE_FREE(data
.dptr
);
2229 oldest
= SMB_MALLOC_P(struct cred_list
);
2230 if (oldest
== NULL
) {
2231 status
= NT_STATUS_NO_MEMORY
;
2235 fstrcpy(oldest
->name
, cred
->name
);
2236 oldest
->created
= t
;
2240 if (t
< oldest
->created
) {
2241 fstrcpy(oldest
->name
, cred
->name
);
2242 oldest
->created
= t
;
2246 if (tdb_delete(cache
->tdb
, string_tdb_data(oldest
->name
)) == 0) {
2247 status
= NT_STATUS_OK
;
2249 status
= NT_STATUS_UNSUCCESSFUL
;
2252 SAFE_FREE(wcache_cred_list
);
2258 /* Change the global online/offline state. */
2259 BOOL
set_global_winbindd_state_offline(void)
2264 DEBUG(10,("set_global_winbindd_state_offline: offline requested.\n"));
2266 /* Only go offline if someone has created
2267 the key "WINBINDD_OFFLINE" in the cache tdb. */
2269 if (wcache
== NULL
|| wcache
->tdb
== NULL
) {
2270 DEBUG(10,("set_global_winbindd_state_offline: wcache not open yet.\n"));
2274 if (!lp_winbind_offline_logon()) {
2275 DEBUG(10,("set_global_winbindd_state_offline: rejecting.\n"));
2279 if (global_winbindd_offline_state
) {
2280 /* Already offline. */
2284 wcache
->tdb
->ecode
= 0;
2286 data
= tdb_fetch_bystring( wcache
->tdb
, "WINBINDD_OFFLINE" );
2288 /* As this is a key with no data we don't need to free, we
2289 check for existence by looking at tdb_err. */
2291 err
= tdb_error(wcache
->tdb
);
2293 if (err
== TDB_ERR_NOEXIST
) {
2294 DEBUG(10,("set_global_winbindd_state_offline: offline state not set.\n"));
2297 DEBUG(10,("set_global_winbindd_state_offline: offline state set.\n"));
2298 global_winbindd_offline_state
= True
;
2303 void set_global_winbindd_state_online(void)
2305 DEBUG(10,("set_global_winbindd_state_online: online requested.\n"));
2307 if (!lp_winbind_offline_logon()) {
2308 DEBUG(10,("set_global_winbindd_state_online: rejecting.\n"));
2312 if (!global_winbindd_offline_state
) {
2313 /* Already online. */
2316 global_winbindd_offline_state
= False
;
2322 /* Ensure there is no key "WINBINDD_OFFLINE" in the cache tdb. */
2323 tdb_delete_bystring(wcache
->tdb
, "WINBINDD_OFFLINE");
2326 /* the cache backend methods are exposed via this structure */
2327 struct winbindd_methods cache_methods
= {