2 Unix SMB/CIFS implementation.
4 Winbind cache backend functions
6 Copyright (C) Andrew Tridgell 2001
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #define DBGC_CLASS DBGC_WINBIND
28 struct winbind_cache
{
34 uint32 sequence_number
;
39 #define WINBINDD_MAX_CACHE_SIZE (50*1024*1024)
41 static struct winbind_cache
*wcache
;
44 void wcache_flush_cache(void)
46 extern BOOL opt_nocache
;
51 tdb_close(wcache
->tdb
);
57 wcache
->tdb
= tdb_open_log(lock_path("winbindd_cache.tdb"), 5000,
58 TDB_CLEAR_IF_FIRST
, O_RDWR
|O_CREAT
, 0600);
61 DEBUG(0,("Failed to open winbindd_cache.tdb!\n"));
63 DEBUG(10,("wcache_flush_cache success\n"));
66 void winbindd_check_cache_size(time_t t
)
68 static time_t last_check_time
;
71 if (last_check_time
== (time_t)0)
74 if (t
- last_check_time
< 60 && t
- last_check_time
> 0)
77 if (wcache
== NULL
|| wcache
->tdb
== NULL
) {
78 DEBUG(0, ("Unable to check size of tdb cache - cache not open !\n"));
82 if (fstat(wcache
->tdb
->fd
, &st
) == -1) {
83 DEBUG(0, ("Unable to check size of tdb cache %s!\n", strerror(errno
) ));
87 if (st
.st_size
> WINBINDD_MAX_CACHE_SIZE
) {
88 DEBUG(10,("flushing cache due to size (%lu) > (%lu)\n",
89 (unsigned long)st
.st_size
,
90 (unsigned long)WINBINDD_MAX_CACHE_SIZE
));
95 /* get the winbind_cache structure */
96 static struct winbind_cache
*get_cache(struct winbindd_domain
*domain
)
98 struct winbind_cache
*ret
= wcache
;
100 if (!domain
->backend
) {
101 extern struct winbindd_methods msrpc_methods
;
102 switch (lp_security()) {
105 extern struct winbindd_methods ads_methods
;
106 domain
->backend
= &ads_methods
;
111 domain
->backend
= &msrpc_methods
;
118 ret
= smb_xmalloc(sizeof(*ret
));
122 wcache_flush_cache();
128 free a centry structure
130 static void centry_free(struct cache_entry
*centry
)
134 SAFE_FREE(centry
->data
);
139 pull a uint32 from a cache entry
141 static uint32
centry_uint32(struct cache_entry
*centry
)
144 if (centry
->len
- centry
->ofs
< 4) {
145 DEBUG(0,("centry corruption? needed 4 bytes, have %d\n",
146 centry
->len
- centry
->ofs
));
147 smb_panic("centry_uint32");
149 ret
= IVAL(centry
->data
, centry
->ofs
);
155 pull a uint8 from a cache entry
157 static uint8
centry_uint8(struct cache_entry
*centry
)
160 if (centry
->len
- centry
->ofs
< 1) {
161 DEBUG(0,("centry corruption? needed 1 bytes, have %d\n",
162 centry
->len
- centry
->ofs
));
163 smb_panic("centry_uint32");
165 ret
= CVAL(centry
->data
, centry
->ofs
);
170 /* pull a string from a cache entry, using the supplied
173 static char *centry_string(struct cache_entry
*centry
, TALLOC_CTX
*mem_ctx
)
178 len
= centry_uint8(centry
);
181 /* a deliberate NULL string */
185 if (centry
->len
- centry
->ofs
< len
) {
186 DEBUG(0,("centry corruption? needed %d bytes, have %d\n",
187 len
, centry
->len
- centry
->ofs
));
188 smb_panic("centry_string");
191 ret
= talloc(mem_ctx
, len
+1);
193 smb_panic("centry_string out of memory\n");
195 memcpy(ret
,centry
->data
+ centry
->ofs
, len
);
201 /* pull a string from a cache entry, using the supplied
204 static DOM_SID
*centry_sid(struct cache_entry
*centry
, TALLOC_CTX
*mem_ctx
)
209 sid
= talloc(mem_ctx
, sizeof(*sid
));
213 sid_string
= centry_string(centry
, mem_ctx
);
214 if (!string_to_sid(sid
, sid_string
)) {
220 /* the server is considered down if it can't give us a sequence number */
221 static BOOL
wcache_server_down(struct winbindd_domain
*domain
)
228 ret
= (domain
->sequence_number
== DOM_SEQUENCE_NONE
);
231 DEBUG(10,("wcache_server_down: server for Domain %s down\n",
236 static NTSTATUS
fetch_cache_seqnum( struct winbindd_domain
*domain
, time_t now
)
243 DEBUG(10,("fetch_cache_seqnum: tdb == NULL\n"));
244 return NT_STATUS_UNSUCCESSFUL
;
247 snprintf( key
, sizeof(key
), "SEQNUM/%s", domain
->name
);
249 data
= tdb_fetch_by_string( wcache
->tdb
, key
);
250 if ( !data
.dptr
|| data
.dsize
!=8 ) {
251 DEBUG(10,("fetch_cache_seqnum: invalid data size key [%s]\n", key
));
252 return NT_STATUS_UNSUCCESSFUL
;
255 domain
->sequence_number
= IVAL(data
.dptr
, 0);
256 domain
->last_seq_check
= IVAL(data
.dptr
, 4);
258 /* have we expired? */
260 time_diff
= now
- domain
->last_seq_check
;
261 if ( time_diff
> lp_winbind_cache_time() ) {
262 DEBUG(10,("fetch_cache_seqnum: timeout [%s][%u @ %u]\n",
263 domain
->name
, domain
->sequence_number
,
264 (uint32
)domain
->last_seq_check
));
265 return NT_STATUS_UNSUCCESSFUL
;
268 DEBUG(10,("fetch_cache_seqnum: success [%s][%u @ %u]\n",
269 domain
->name
, domain
->sequence_number
,
270 (uint32
)domain
->last_seq_check
));
275 static NTSTATUS
store_cache_seqnum( struct winbindd_domain
*domain
)
282 DEBUG(10,("store_cache_seqnum: tdb == NULL\n"));
283 return NT_STATUS_UNSUCCESSFUL
;
286 snprintf( key_str
, sizeof(key_str
), "SEQNUM/%s", domain
->name
);
288 key
.dsize
= strlen(key_str
)+1;
290 SIVAL(buf
, 0, domain
->sequence_number
);
291 SIVAL(buf
, 4, domain
->last_seq_check
);
295 if ( tdb_store( wcache
->tdb
, key
, data
, TDB_REPLACE
) == -1 ) {
296 DEBUG(10,("store_cache_seqnum: tdb_store fail key [%s]\n", key_str
));
297 return NT_STATUS_UNSUCCESSFUL
;
300 DEBUG(10,("store_cache_seqnum: success [%s][%u @ %u]\n",
301 domain
->name
, domain
->sequence_number
,
302 (uint32
)domain
->last_seq_check
));
308 refresh the domain sequence number. If force is True
309 then always refresh it, no matter how recently we fetched it
312 static void refresh_sequence_number(struct winbindd_domain
*domain
, BOOL force
)
316 time_t t
= time(NULL
);
317 unsigned cache_time
= lp_winbind_cache_time();
319 /* trying to reconnect is expensive, don't do it too often */
320 if (domain
->sequence_number
== DOM_SEQUENCE_NONE
) {
324 time_diff
= t
- domain
->last_seq_check
;
326 /* see if we have to refetch the domain sequence number */
327 if (!force
&& (time_diff
< cache_time
)) {
328 DEBUG(10, ("refresh_sequence_number: %s time ok\n", domain
->name
));
332 /* try to get the sequence number from the tdb cache first */
333 /* this will update the timestamp as well */
335 status
= fetch_cache_seqnum( domain
, t
);
336 if ( NT_STATUS_IS_OK(status
) )
339 status
= domain
->backend
->sequence_number(domain
, &domain
->sequence_number
);
341 if (!NT_STATUS_IS_OK(status
)) {
342 domain
->sequence_number
= DOM_SEQUENCE_NONE
;
345 domain
->last_seq_check
= time(NULL
);
347 /* save the new sequence number ni the cache */
348 store_cache_seqnum( domain
);
351 DEBUG(10, ("refresh_sequence_number: %s seq number is now %d\n",
352 domain
->name
, domain
->sequence_number
));
358 decide if a cache entry has expired
360 static BOOL
centry_expired(struct winbindd_domain
*domain
, const char *keystr
, struct cache_entry
*centry
)
362 /* if the server is OK and our cache entry came from when it was down then
363 the entry is invalid */
364 if (domain
->sequence_number
!= DOM_SEQUENCE_NONE
&&
365 centry
->sequence_number
== DOM_SEQUENCE_NONE
) {
366 DEBUG(10,("centry_expired: Key %s for domain %s invalid sequence.\n",
367 keystr
, domain
->name
));
371 /* if the server is down or the cache entry is not older than the
372 current sequence number then it is OK */
373 if (wcache_server_down(domain
) ||
374 centry
->sequence_number
== domain
->sequence_number
) {
375 DEBUG(10,("centry_expired: Key %s for domain %s is good.\n",
376 keystr
, domain
->name
));
380 DEBUG(10,("centry_expired: Key %s for domain %s expired\n",
381 keystr
, domain
->name
));
388 fetch an entry from the cache, with a varargs key. auto-fetch the sequence
389 number and return status
391 static struct cache_entry
*wcache_fetch(struct winbind_cache
*cache
,
392 struct winbindd_domain
*domain
,
393 const char *format
, ...) PRINTF_ATTRIBUTE(3,4);
394 static struct cache_entry
*wcache_fetch(struct winbind_cache
*cache
,
395 struct winbindd_domain
*domain
,
396 const char *format
, ...)
401 struct cache_entry
*centry
;
404 refresh_sequence_number(domain
, False
);
406 va_start(ap
, format
);
407 smb_xvasprintf(&kstr
, format
, ap
);
411 key
.dsize
= strlen(kstr
);
412 data
= tdb_fetch(wcache
->tdb
, key
);
419 centry
= smb_xmalloc(sizeof(*centry
));
420 centry
->data
= data
.dptr
;
421 centry
->len
= data
.dsize
;
424 if (centry
->len
< 8) {
425 /* huh? corrupt cache? */
426 DEBUG(10,("wcache_fetch: Corrupt cache for key %s domain %s (len < 8) ?\n",
427 kstr
, domain
->name
));
433 centry
->status
= NT_STATUS(centry_uint32(centry
));
434 centry
->sequence_number
= centry_uint32(centry
);
436 if (centry_expired(domain
, kstr
, centry
)) {
437 extern BOOL opt_dual_daemon
;
439 DEBUG(10,("wcache_fetch: entry %s expired for domain %s\n",
440 kstr
, domain
->name
));
442 if (opt_dual_daemon
) {
443 extern BOOL background_process
;
444 background_process
= True
;
445 DEBUG(10,("wcache_fetch: background processing expired entry %s for domain %s\n",
446 kstr
, domain
->name
));
454 DEBUG(10,("wcache_fetch: returning entry %s for domain %s\n",
455 kstr
, domain
->name
));
462 make sure we have at least len bytes available in a centry
464 static void centry_expand(struct cache_entry
*centry
, uint32 len
)
467 if (centry
->len
- centry
->ofs
>= len
)
470 p
= realloc(centry
->data
, centry
->len
);
472 DEBUG(0,("out of memory: needed %d bytes in centry_expand\n", centry
->len
));
473 smb_panic("out of memory in centry_expand");
479 push a uint32 into a centry
481 static void centry_put_uint32(struct cache_entry
*centry
, uint32 v
)
483 centry_expand(centry
, 4);
484 SIVAL(centry
->data
, centry
->ofs
, v
);
489 push a uint8 into a centry
491 static void centry_put_uint8(struct cache_entry
*centry
, uint8 v
)
493 centry_expand(centry
, 1);
494 SCVAL(centry
->data
, centry
->ofs
, v
);
499 push a string into a centry
501 static void centry_put_string(struct cache_entry
*centry
, const char *s
)
506 /* null strings are marked as len 0xFFFF */
507 centry_put_uint8(centry
, 0xFF);
512 /* can't handle more than 254 char strings. Truncating is probably best */
515 centry_put_uint8(centry
, len
);
516 centry_expand(centry
, len
);
517 memcpy(centry
->data
+ centry
->ofs
, s
, len
);
521 static void centry_put_sid(struct cache_entry
*centry
, const DOM_SID
*sid
)
524 centry_put_string(centry
, sid_to_string(sid_string
, sid
));
528 start a centry for output. When finished, call centry_end()
530 struct cache_entry
*centry_start(struct winbindd_domain
*domain
, NTSTATUS status
)
532 struct cache_entry
*centry
;
537 centry
= smb_xmalloc(sizeof(*centry
));
539 centry
->len
= 8192; /* reasonable default */
540 centry
->data
= smb_xmalloc(centry
->len
);
542 centry
->sequence_number
= domain
->sequence_number
;
543 centry_put_uint32(centry
, NT_STATUS_V(status
));
544 centry_put_uint32(centry
, centry
->sequence_number
);
549 finish a centry and write it to the tdb
551 static void centry_end(struct cache_entry
*centry
, const char *format
, ...) PRINTF_ATTRIBUTE(2,3);
552 static void centry_end(struct cache_entry
*centry
, const char *format
, ...)
558 va_start(ap
, format
);
559 smb_xvasprintf(&kstr
, format
, ap
);
563 key
.dsize
= strlen(kstr
);
564 data
.dptr
= centry
->data
;
565 data
.dsize
= centry
->ofs
;
567 tdb_store(wcache
->tdb
, key
, data
, TDB_REPLACE
);
571 static void wcache_save_name_to_sid(struct winbindd_domain
*domain
,
573 const char *name
, DOM_SID
*sid
,
574 enum SID_NAME_USE type
)
576 struct cache_entry
*centry
;
580 centry
= centry_start(domain
, status
);
583 centry_put_sid(centry
, sid
);
584 fstrcpy(uname
, name
);
586 centry_end(centry
, "NS/%s", sid_to_string(sid_string
, sid
));
587 DEBUG(10,("wcache_save_name_to_sid: %s -> %s\n", uname
, sid_string
));
591 static void wcache_save_sid_to_name(struct winbindd_domain
*domain
, NTSTATUS status
,
592 DOM_SID
*sid
, const char *name
, enum SID_NAME_USE type
)
594 struct cache_entry
*centry
;
597 centry
= centry_start(domain
, status
);
600 if (NT_STATUS_IS_OK(status
)) {
601 centry_put_uint32(centry
, type
);
602 centry_put_string(centry
, name
);
604 centry_end(centry
, "SN/%s", sid_to_string(sid_string
, sid
));
605 DEBUG(10,("wcache_save_sid_to_name: %s -> %s\n", sid_string
, name
));
610 static void wcache_save_user(struct winbindd_domain
*domain
, NTSTATUS status
, WINBIND_USERINFO
*info
)
612 struct cache_entry
*centry
;
615 centry
= centry_start(domain
, status
);
618 centry_put_string(centry
, info
->acct_name
);
619 centry_put_string(centry
, info
->full_name
);
620 centry_put_sid(centry
, info
->user_sid
);
621 centry_put_sid(centry
, info
->group_sid
);
622 centry_end(centry
, "U/%s", sid_to_string(sid_string
, info
->user_sid
));
623 DEBUG(10,("wcache_save_user: %s (acct_name %s)\n", sid_string
, info
->acct_name
));
628 /* Query display info. This is the basic user list fn */
629 static NTSTATUS
query_user_list(struct winbindd_domain
*domain
,
632 WINBIND_USERINFO
**info
)
634 struct winbind_cache
*cache
= get_cache(domain
);
635 struct cache_entry
*centry
= NULL
;
642 centry
= wcache_fetch(cache
, domain
, "UL/%s", domain
->name
);
646 *num_entries
= centry_uint32(centry
);
648 if (*num_entries
== 0)
651 (*info
) = talloc(mem_ctx
, sizeof(**info
) * (*num_entries
));
653 smb_panic("query_user_list out of memory");
654 for (i
=0; i
<(*num_entries
); i
++) {
655 (*info
)[i
].acct_name
= centry_string(centry
, mem_ctx
);
656 (*info
)[i
].full_name
= centry_string(centry
, mem_ctx
);
657 (*info
)[i
].user_sid
= centry_sid(centry
, mem_ctx
);
658 (*info
)[i
].group_sid
= centry_sid(centry
, mem_ctx
);
662 status
= centry
->status
;
664 DEBUG(10,("query_user_list: [Cached] - cached list for domain %s status %s\n",
665 domain
->name
, get_friendly_nt_error_msg(status
) ));
674 if (wcache_server_down(domain
)) {
675 return NT_STATUS_SERVER_DISABLED
;
678 DEBUG(10,("query_user_list: [Cached] - doing backend query for list for domain %s\n",
681 status
= domain
->backend
->query_user_list(domain
, mem_ctx
, num_entries
, info
);
684 refresh_sequence_number(domain
, False
);
685 centry
= centry_start(domain
, status
);
688 centry_put_uint32(centry
, *num_entries
);
689 for (i
=0; i
<(*num_entries
); i
++) {
690 centry_put_string(centry
, (*info
)[i
].acct_name
);
691 centry_put_string(centry
, (*info
)[i
].full_name
);
692 centry_put_sid(centry
, (*info
)[i
].user_sid
);
693 centry_put_sid(centry
, (*info
)[i
].group_sid
);
694 if (domain
->backend
->consistent
) {
695 /* when the backend is consistent we can pre-prime some mappings */
696 wcache_save_name_to_sid(domain
, NT_STATUS_OK
,
697 (*info
)[i
].acct_name
,
700 wcache_save_sid_to_name(domain
, NT_STATUS_OK
,
702 (*info
)[i
].acct_name
,
704 wcache_save_user(domain
, NT_STATUS_OK
, &(*info
)[i
]);
707 centry_end(centry
, "UL/%s", domain
->name
);
714 /* list all domain groups */
715 static NTSTATUS
enum_dom_groups(struct winbindd_domain
*domain
,
718 struct acct_info
**info
)
720 struct winbind_cache
*cache
= get_cache(domain
);
721 struct cache_entry
*centry
= NULL
;
728 centry
= wcache_fetch(cache
, domain
, "GL/%s/domain", domain
->name
);
732 *num_entries
= centry_uint32(centry
);
734 if (*num_entries
== 0)
737 (*info
) = talloc(mem_ctx
, sizeof(**info
) * (*num_entries
));
739 smb_panic("enum_dom_groups out of memory");
740 for (i
=0; i
<(*num_entries
); i
++) {
741 fstrcpy((*info
)[i
].acct_name
, centry_string(centry
, mem_ctx
));
742 fstrcpy((*info
)[i
].acct_desc
, centry_string(centry
, mem_ctx
));
743 (*info
)[i
].rid
= centry_uint32(centry
);
747 status
= centry
->status
;
749 DEBUG(10,("enum_dom_groups: [Cached] - cached list for domain %s status %s\n",
750 domain
->name
, get_friendly_nt_error_msg(status
) ));
759 if (wcache_server_down(domain
)) {
760 return NT_STATUS_SERVER_DISABLED
;
763 DEBUG(10,("enum_dom_groups: [Cached] - doing backend query for list for domain %s\n",
766 status
= domain
->backend
->enum_dom_groups(domain
, mem_ctx
, num_entries
, info
);
769 refresh_sequence_number(domain
, False
);
770 centry
= centry_start(domain
, status
);
773 centry_put_uint32(centry
, *num_entries
);
774 for (i
=0; i
<(*num_entries
); i
++) {
775 centry_put_string(centry
, (*info
)[i
].acct_name
);
776 centry_put_string(centry
, (*info
)[i
].acct_desc
);
777 centry_put_uint32(centry
, (*info
)[i
].rid
);
779 centry_end(centry
, "GL/%s/domain", domain
->name
);
786 /* list all domain groups */
787 static NTSTATUS
enum_local_groups(struct winbindd_domain
*domain
,
790 struct acct_info
**info
)
792 struct winbind_cache
*cache
= get_cache(domain
);
793 struct cache_entry
*centry
= NULL
;
800 centry
= wcache_fetch(cache
, domain
, "GL/%s/local", domain
->name
);
804 *num_entries
= centry_uint32(centry
);
806 if (*num_entries
== 0)
809 (*info
) = talloc(mem_ctx
, sizeof(**info
) * (*num_entries
));
811 smb_panic("enum_dom_groups out of memory");
812 for (i
=0; i
<(*num_entries
); i
++) {
813 fstrcpy((*info
)[i
].acct_name
, centry_string(centry
, mem_ctx
));
814 fstrcpy((*info
)[i
].acct_desc
, centry_string(centry
, mem_ctx
));
815 (*info
)[i
].rid
= centry_uint32(centry
);
820 /* If we are returning cached data and the domain controller
821 is down then we don't know whether the data is up to date
822 or not. Return NT_STATUS_MORE_PROCESSING_REQUIRED to
825 if (wcache_server_down(domain
)) {
826 DEBUG(10, ("enum_local_groups: returning cached user list and server was down\n"));
827 status
= NT_STATUS_MORE_PROCESSING_REQUIRED
;
829 status
= centry
->status
;
831 DEBUG(10,("enum_local_groups: [Cached] - cached list for domain %s status %s\n",
832 domain
->name
, get_friendly_nt_error_msg(status
) ));
841 if (wcache_server_down(domain
)) {
842 return NT_STATUS_SERVER_DISABLED
;
845 DEBUG(10,("enum_local_groups: [Cached] - doing backend query for list for domain %s\n",
848 status
= domain
->backend
->enum_local_groups(domain
, mem_ctx
, num_entries
, info
);
851 refresh_sequence_number(domain
, False
);
852 centry
= centry_start(domain
, status
);
855 centry_put_uint32(centry
, *num_entries
);
856 for (i
=0; i
<(*num_entries
); i
++) {
857 centry_put_string(centry
, (*info
)[i
].acct_name
);
858 centry_put_string(centry
, (*info
)[i
].acct_desc
);
859 centry_put_uint32(centry
, (*info
)[i
].rid
);
861 centry_end(centry
, "GL/%s/local", domain
->name
);
868 /* convert a single name to a sid in a domain */
869 static NTSTATUS
name_to_sid(struct winbindd_domain
*domain
,
873 enum SID_NAME_USE
*type
)
875 struct winbind_cache
*cache
= get_cache(domain
);
876 struct cache_entry
*centry
= NULL
;
881 if (!cache
->tdb
) goto do_query
;
883 fstrcpy(uname
, name
);
885 centry
= wcache_fetch(cache
, domain
, "NS/%s/%s", domain
->name
, uname
);
888 *type
= centry_uint32(centry
);
889 sid2
= centry_sid(centry
, mem_ctx
);
896 status
= centry
->status
;
898 DEBUG(10,("name_to_sid: [Cached] - cached name for domain %s status %s\n",
899 domain
->name
, get_friendly_nt_error_msg(status
) ));
907 if (wcache_server_down(domain
)) {
908 return NT_STATUS_SERVER_DISABLED
;
911 DEBUG(10,("name_to_sid: [Cached] - doing backend query for name for domain %s\n",
914 status
= domain
->backend
->name_to_sid(domain
, mem_ctx
, name
, sid
, type
);
917 wcache_save_name_to_sid(domain
, status
, name
, sid
, *type
);
919 /* We can't save the sid to name mapping as we don't know the
920 correct case of the name without looking it up */
925 /* convert a sid to a user or group name. The sid is guaranteed to be in the domain
927 static NTSTATUS
sid_to_name(struct winbindd_domain
*domain
,
931 enum SID_NAME_USE
*type
)
933 struct winbind_cache
*cache
= get_cache(domain
);
934 struct cache_entry
*centry
= NULL
;
941 centry
= wcache_fetch(cache
, domain
, "SN/%s", sid_to_string(sid_string
, sid
));
944 if (NT_STATUS_IS_OK(centry
->status
)) {
945 *type
= centry_uint32(centry
);
946 *name
= centry_string(centry
, mem_ctx
);
948 status
= centry
->status
;
950 DEBUG(10,("sid_to_name: [Cached] - cached name for domain %s status %s\n",
951 domain
->name
, get_friendly_nt_error_msg(status
) ));
959 if (wcache_server_down(domain
)) {
960 return NT_STATUS_SERVER_DISABLED
;
963 DEBUG(10,("sid_to_name: [Cached] - doing backend query for name for domain %s\n",
966 status
= domain
->backend
->sid_to_name(domain
, mem_ctx
, sid
, name
, type
);
969 refresh_sequence_number(domain
, False
);
970 wcache_save_sid_to_name(domain
, status
, sid
, *name
, *type
);
971 wcache_save_name_to_sid(domain
, status
, *name
, sid
, *type
);
977 /* Lookup user information from a rid */
978 static NTSTATUS
query_user(struct winbindd_domain
*domain
,
981 WINBIND_USERINFO
*info
)
983 struct winbind_cache
*cache
= get_cache(domain
);
984 struct cache_entry
*centry
= NULL
;
991 centry
= wcache_fetch(cache
, domain
, "U/%s", sid_to_string(sid_string
, user_sid
));
995 info
->acct_name
= centry_string(centry
, mem_ctx
);
996 info
->full_name
= centry_string(centry
, mem_ctx
);
997 info
->user_sid
= centry_sid(centry
, mem_ctx
);
998 info
->group_sid
= centry_sid(centry
, mem_ctx
);
999 status
= centry
->status
;
1001 DEBUG(10,("query_user: [Cached] - cached info for domain %s status %s\n",
1002 domain
->name
, get_friendly_nt_error_msg(status
) ));
1004 centry_free(centry
);
1010 if (wcache_server_down(domain
)) {
1011 return NT_STATUS_SERVER_DISABLED
;
1014 DEBUG(10,("sid_to_name: [Cached] - doing backend query for info for domain %s\n",
1017 status
= domain
->backend
->query_user(domain
, mem_ctx
, user_sid
, info
);
1020 refresh_sequence_number(domain
, False
);
1021 wcache_save_user(domain
, status
, info
);
1027 /* Lookup groups a user is a member of. */
1028 static NTSTATUS
lookup_usergroups(struct winbindd_domain
*domain
,
1029 TALLOC_CTX
*mem_ctx
,
1031 uint32
*num_groups
, DOM_SID
***user_gids
)
1033 struct winbind_cache
*cache
= get_cache(domain
);
1034 struct cache_entry
*centry
= NULL
;
1042 centry
= wcache_fetch(cache
, domain
, "UG/%s", sid_to_string(sid_string
, user_sid
));
1046 *num_groups
= centry_uint32(centry
);
1048 if (*num_groups
== 0)
1051 (*user_gids
) = talloc(mem_ctx
, sizeof(**user_gids
) * (*num_groups
));
1053 smb_panic("lookup_usergroups out of memory");
1054 for (i
=0; i
<(*num_groups
); i
++) {
1055 (*user_gids
)[i
] = centry_sid(centry
, mem_ctx
);
1059 status
= centry
->status
;
1061 DEBUG(10,("lookup_usergroups: [Cached] - cached info for domain %s status %s\n",
1062 domain
->name
, get_friendly_nt_error_msg(status
) ));
1064 centry_free(centry
);
1069 (*user_gids
) = NULL
;
1071 if (wcache_server_down(domain
)) {
1072 return NT_STATUS_SERVER_DISABLED
;
1075 DEBUG(10,("lookup_usergroups: [Cached] - doing backend query for info for domain %s\n",
1078 status
= domain
->backend
->lookup_usergroups(domain
, mem_ctx
, user_sid
, num_groups
, user_gids
);
1081 refresh_sequence_number(domain
, False
);
1082 centry
= centry_start(domain
, status
);
1085 centry_put_uint32(centry
, *num_groups
);
1086 for (i
=0; i
<(*num_groups
); i
++) {
1087 centry_put_sid(centry
, (*user_gids
)[i
]);
1089 centry_end(centry
, "UG/%s", sid_to_string(sid_string
, user_sid
));
1090 centry_free(centry
);
1097 static NTSTATUS
lookup_groupmem(struct winbindd_domain
*domain
,
1098 TALLOC_CTX
*mem_ctx
,
1099 DOM_SID
*group_sid
, uint32
*num_names
,
1100 DOM_SID
***sid_mem
, char ***names
,
1101 uint32
**name_types
)
1103 struct winbind_cache
*cache
= get_cache(domain
);
1104 struct cache_entry
*centry
= NULL
;
1112 centry
= wcache_fetch(cache
, domain
, "GM/%s", sid_to_string(sid_string
, group_sid
));
1116 *num_names
= centry_uint32(centry
);
1118 if (*num_names
== 0)
1121 (*sid_mem
) = talloc(mem_ctx
, sizeof(**sid_mem
) * (*num_names
));
1122 (*names
) = talloc(mem_ctx
, sizeof(**names
) * (*num_names
));
1123 (*name_types
) = talloc(mem_ctx
, sizeof(**name_types
) * (*num_names
));
1125 if (! (*sid_mem
) || ! (*names
) || ! (*name_types
)) {
1126 smb_panic("lookup_groupmem out of memory");
1129 for (i
=0; i
<(*num_names
); i
++) {
1130 (*sid_mem
)[i
] = centry_sid(centry
, mem_ctx
);
1131 (*names
)[i
] = centry_string(centry
, mem_ctx
);
1132 (*name_types
)[i
] = centry_uint32(centry
);
1136 status
= centry
->status
;
1138 DEBUG(10,("lookup_groupmem: [Cached] - cached info for domain %s status %s\n",
1139 domain
->name
, get_friendly_nt_error_msg(status
) ));
1141 centry_free(centry
);
1148 (*name_types
) = NULL
;
1151 if (wcache_server_down(domain
)) {
1152 return NT_STATUS_SERVER_DISABLED
;
1155 DEBUG(10,("lookup_groupmem: [Cached] - doing backend query for info for domain %s\n",
1158 status
= domain
->backend
->lookup_groupmem(domain
, mem_ctx
, group_sid
, num_names
,
1159 sid_mem
, names
, name_types
);
1162 refresh_sequence_number(domain
, False
);
1163 centry
= centry_start(domain
, status
);
1166 centry_put_uint32(centry
, *num_names
);
1167 for (i
=0; i
<(*num_names
); i
++) {
1168 centry_put_sid(centry
, (*sid_mem
)[i
]);
1169 centry_put_string(centry
, (*names
)[i
]);
1170 centry_put_uint32(centry
, (*name_types
)[i
]);
1172 centry_end(centry
, "GM/%s", sid_to_string(sid_string
, group_sid
));
1173 centry_free(centry
);
1179 /* find the sequence number for a domain */
1180 static NTSTATUS
sequence_number(struct winbindd_domain
*domain
, uint32
*seq
)
1182 refresh_sequence_number(domain
, False
);
1184 *seq
= domain
->sequence_number
;
1186 return NT_STATUS_OK
;
1189 /* enumerate trusted domains */
1190 static NTSTATUS
trusted_domains(struct winbindd_domain
*domain
,
1191 TALLOC_CTX
*mem_ctx
,
1192 uint32
*num_domains
,
1199 DEBUG(10,("trusted_domains: [Cached] - doing backend query for info for domain %s\n",
1202 /* we don't cache this call */
1203 return domain
->backend
->trusted_domains(domain
, mem_ctx
, num_domains
,
1204 names
, alt_names
, dom_sids
);
1207 /* find the domain sid */
1208 static NTSTATUS
domain_sid(struct winbindd_domain
*domain
, DOM_SID
*sid
)
1212 DEBUG(10,("domain_sid: [Cached] - doing backend query for info for domain %s\n",
1215 /* we don't cache this call */
1216 return domain
->backend
->domain_sid(domain
, sid
);
1219 /* find the alternate names for the domain, if any */
1220 static NTSTATUS
alternate_name(struct winbindd_domain
*domain
)
1224 DEBUG(10,("alternate_name: [Cached] - doing backend query for info for domain %s\n",
1227 /* we don't cache this call */
1228 return domain
->backend
->alternate_name(domain
);
1231 /* the ADS backend methods are exposed via this structure */
1232 struct winbindd_methods cache_methods
= {