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;
637 unsigned int i, retry;
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 /* Put the query_user_list() in a retry loop. There appears to be
679 * some bug either with Windows 2000 or Samba's handling of large
680 * rpc replies. This manifests itself as sudden disconnection
681 * at a random point in the enumeration of a large (60k) user list.
682 * The retry loop simply tries the operation again. )-: It's not
683 * pretty but an acceptable workaround until we work out what the
684 * real problem is. */
689 DEBUG(10,("query_user_list: [Cached] - doing backend query for list for domain %s\n",
692 status = domain->backend->query_user_list(domain, mem_ctx, num_entries, info);
693 if (!NT_STATUS_IS_OK(status))
694 DEBUG(3, ("query_user_list: returned 0x%08x, retrying\n", NT_STATUS_V(status)));
695 if (NT_STATUS_V(status) == NT_STATUS_V(NT_STATUS_UNSUCCESSFUL)) {
696 DEBUG(3, ("query_user_list: flushing connection cache\n"));
700 } while (NT_STATUS_V(status) == NT_STATUS_V(NT_STATUS_UNSUCCESSFUL) && (retry++ < 5));
703 refresh_sequence_number(domain, False);
704 centry = centry_start(domain, status);
707 centry_put_uint32(centry, *num_entries);
708 for (i=0; i<(*num_entries); i++) {
709 centry_put_string(centry, (*info)[i].acct_name);
710 centry_put_string(centry, (*info)[i].full_name);
711 centry_put_sid(centry, (*info)[i].user_sid);
712 centry_put_sid(centry, (*info)[i].group_sid);
713 if (domain->backend->consistent) {
714 /* when the backend is consistent we can pre-prime some mappings */
715 wcache_save_name_to_sid(domain, NT_STATUS_OK,
716 (*info)[i].acct_name,
719 wcache_save_sid_to_name(domain, NT_STATUS_OK,
721 (*info)[i].acct_name,
723 wcache_save_user(domain, NT_STATUS_OK, &(*info)[i]);
726 centry_end(centry, "UL/%s", domain->name);
733 /* list all domain groups */
734 static NTSTATUS enum_dom_groups(struct winbindd_domain *domain,
737 struct acct_info **info)
739 struct winbind_cache *cache = get_cache(domain);
740 struct cache_entry *centry = NULL;
747 centry = wcache_fetch(cache, domain, "GL/%s/domain", domain->name);
751 *num_entries = centry_uint32(centry);
753 if (*num_entries == 0)
756 (*info) = talloc(mem_ctx, sizeof(**info) * (*num_entries));
758 smb_panic("enum_dom_groups out of memory");
759 for (i=0; i<(*num_entries); i++) {
760 fstrcpy((*info)[i].acct_name, centry_string(centry, mem_ctx));
761 fstrcpy((*info)[i].acct_desc, centry_string(centry, mem_ctx));
762 (*info)[i].rid = centry_uint32(centry);
766 status = centry->status;
768 DEBUG(10,("enum_dom_groups: [Cached] - cached list for domain %s status %s\n",
769 domain->name, get_friendly_nt_error_msg(status) ));
778 if (wcache_server_down(domain)) {
779 return NT_STATUS_SERVER_DISABLED;
782 DEBUG(10,("enum_dom_groups: [Cached] - doing backend query for list for domain %s\n",
785 status = domain->backend->enum_dom_groups(domain, mem_ctx, num_entries, info);
788 refresh_sequence_number(domain, False);
789 centry = centry_start(domain, status);
792 centry_put_uint32(centry, *num_entries);
793 for (i=0; i<(*num_entries); i++) {
794 centry_put_string(centry, (*info)[i].acct_name);
795 centry_put_string(centry, (*info)[i].acct_desc);
796 centry_put_uint32(centry, (*info)[i].rid);
798 centry_end(centry, "GL/%s/domain", domain->name);
805 /* list all domain groups */
806 static NTSTATUS enum_local_groups(struct winbindd_domain *domain,
809 struct acct_info **info)
811 struct winbind_cache *cache = get_cache(domain);
812 struct cache_entry *centry = NULL;
819 centry = wcache_fetch(cache, domain, "GL/%s/local", domain->name);
823 *num_entries = centry_uint32(centry);
825 if (*num_entries == 0)
828 (*info) = talloc(mem_ctx, sizeof(**info) * (*num_entries));
830 smb_panic("enum_dom_groups out of memory");
831 for (i=0; i<(*num_entries); i++) {
832 fstrcpy((*info)[i].acct_name, centry_string(centry, mem_ctx));
833 fstrcpy((*info)[i].acct_desc, centry_string(centry, mem_ctx));
834 (*info)[i].rid = centry_uint32(centry);
839 /* If we are returning cached data and the domain controller
840 is down then we don't know whether the data is up to date
841 or not. Return NT_STATUS_MORE_PROCESSING_REQUIRED to
844 if (wcache_server_down(domain)) {
845 DEBUG(10, ("enum_local_groups: returning cached user list and server was down\n"));
846 status = NT_STATUS_MORE_PROCESSING_REQUIRED;
848 status = centry->status;
850 DEBUG(10,("enum_local_groups: [Cached] - cached list for domain %s status %s\n",
851 domain->name, get_friendly_nt_error_msg(status) ));
860 if (wcache_server_down(domain)) {
861 return NT_STATUS_SERVER_DISABLED;
864 DEBUG(10,("enum_local_groups: [Cached] - doing backend query for list for domain %s\n",
867 status = domain->backend->enum_local_groups(domain, mem_ctx, num_entries, info);
870 refresh_sequence_number(domain, False);
871 centry = centry_start(domain, status);
874 centry_put_uint32(centry, *num_entries);
875 for (i=0; i<(*num_entries); i++) {
876 centry_put_string(centry, (*info)[i].acct_name);
877 centry_put_string(centry, (*info)[i].acct_desc);
878 centry_put_uint32(centry, (*info)[i].rid);
880 centry_end(centry, "GL/%s/local", domain->name);
887 /* convert a single name to a sid in a domain */
888 static NTSTATUS name_to_sid(struct winbindd_domain *domain,
892 enum SID_NAME_USE *type)
894 struct winbind_cache *cache = get_cache(domain);
895 struct cache_entry *centry = NULL;
903 fstrcpy(uname, name);
905 centry = wcache_fetch(cache, domain, "NS/%s/%s", domain->name, uname);
908 *type = centry_uint32(centry);
909 sid2 = centry_sid(centry, mem_ctx);
916 status = centry->status;
918 DEBUG(10,("name_to_sid: [Cached] - cached name for domain %s status %s\n",
919 domain->name, get_friendly_nt_error_msg(status) ));
927 if (wcache_server_down(domain)) {
928 return NT_STATUS_SERVER_DISABLED;
931 DEBUG(10,("name_to_sid: [Cached] - doing backend query for name for domain %s\n",
934 status = domain->backend->name_to_sid(domain, mem_ctx, name, sid, type);
937 wcache_save_name_to_sid(domain, status, name, sid, *type);
939 /* We can't save the sid to name mapping as we don't know the
940 correct case of the name without looking it up */
945 /* convert a sid to a user or group name. The sid is guaranteed to be in the domain
947 static NTSTATUS sid_to_name(struct winbindd_domain *domain,
951 enum SID_NAME_USE *type)
953 struct winbind_cache *cache = get_cache(domain);
954 struct cache_entry *centry = NULL;
961 centry = wcache_fetch(cache, domain, "SN/%s", sid_to_string(sid_string, sid));
964 if (NT_STATUS_IS_OK(centry->status)) {
965 *type = centry_uint32(centry);
966 *name = centry_string(centry, mem_ctx);
968 status = centry->status;
970 DEBUG(10,("sid_to_name: [Cached] - cached name for domain %s status %s\n",
971 domain->name, get_friendly_nt_error_msg(status) ));
979 if (wcache_server_down(domain)) {
980 return NT_STATUS_SERVER_DISABLED;
983 DEBUG(10,("sid_to_name: [Cached] - doing backend query for name for domain %s\n",
986 status = domain->backend->sid_to_name(domain, mem_ctx, sid, name, type);
989 refresh_sequence_number(domain, False);
990 wcache_save_sid_to_name(domain, status, sid, *name, *type);
991 wcache_save_name_to_sid(domain, status, *name, sid, *type);
997 /* Lookup user information from a rid */
998 static NTSTATUS query_user(struct winbindd_domain *domain,
1001 WINBIND_USERINFO *info)
1003 struct winbind_cache *cache = get_cache(domain);
1004 struct cache_entry *centry = NULL;
1011 centry = wcache_fetch(cache, domain, "U/%s", sid_to_string(sid_string, user_sid));
1015 info->acct_name = centry_string(centry, mem_ctx);
1016 info->full_name = centry_string(centry, mem_ctx);
1017 info->user_sid = centry_sid(centry, mem_ctx);
1018 info->group_sid = centry_sid(centry, mem_ctx);
1019 status = centry->status;
1021 DEBUG(10,("query_user: [Cached] - cached info for domain %s status %s\n",
1022 domain->name, get_friendly_nt_error_msg(status) ));
1024 centry_free(centry);
1030 if (wcache_server_down(domain)) {
1031 return NT_STATUS_SERVER_DISABLED;
1034 DEBUG(10,("sid_to_name: [Cached] - doing backend query for info for domain %s\n",
1037 status = domain->backend->query_user(domain, mem_ctx, user_sid, info);
1040 refresh_sequence_number(domain, False);
1041 wcache_save_user(domain, status, info);
1047 /* Lookup groups a user is a member of. */
1048 static NTSTATUS lookup_usergroups(struct winbindd_domain *domain,
1049 TALLOC_CTX *mem_ctx,
1051 uint32 *num_groups, DOM_SID ***user_gids)
1053 struct winbind_cache *cache = get_cache(domain);
1054 struct cache_entry *centry = NULL;
1062 centry = wcache_fetch(cache, domain, "UG/%s", sid_to_string(sid_string, user_sid));
1066 *num_groups = centry_uint32(centry);
1068 if (*num_groups == 0)
1071 (*user_gids) = talloc(mem_ctx, sizeof(**user_gids) * (*num_groups));
1073 smb_panic("lookup_usergroups out of memory");
1074 for (i=0; i<(*num_groups); i++) {
1075 (*user_gids)[i] = centry_sid(centry, mem_ctx);
1079 status = centry->status;
1081 DEBUG(10,("lookup_usergroups: [Cached] - cached info for domain %s status %s\n",
1082 domain->name, get_friendly_nt_error_msg(status) ));
1084 centry_free(centry);
1089 (*user_gids) = NULL;
1091 if (wcache_server_down(domain)) {
1092 return NT_STATUS_SERVER_DISABLED;
1095 DEBUG(10,("lookup_usergroups: [Cached] - doing backend query for info for domain %s\n",
1098 status = domain->backend->lookup_usergroups(domain, mem_ctx, user_sid, num_groups, user_gids);
1101 refresh_sequence_number(domain, False);
1102 centry = centry_start(domain, status);
1105 centry_put_uint32(centry, *num_groups);
1106 for (i=0; i<(*num_groups); i++) {
1107 centry_put_sid(centry, (*user_gids)[i]);
1109 centry_end(centry, "UG/%s", sid_to_string(sid_string, user_sid));
1110 centry_free(centry);
1117 static NTSTATUS lookup_groupmem(struct winbindd_domain *domain,
1118 TALLOC_CTX *mem_ctx,
1119 DOM_SID *group_sid, uint32 *num_names,
1120 DOM_SID ***sid_mem, char ***names,
1121 uint32 **name_types)
1123 struct winbind_cache *cache = get_cache(domain);
1124 struct cache_entry *centry = NULL;
1132 centry = wcache_fetch(cache, domain, "GM/%s", sid_to_string(sid_string, group_sid));
1136 *num_names = centry_uint32(centry);
1138 if (*num_names == 0)
1141 (*sid_mem) = talloc(mem_ctx, sizeof(**sid_mem) * (*num_names));
1142 (*names) = talloc(mem_ctx, sizeof(**names) * (*num_names));
1143 (*name_types) = talloc(mem_ctx, sizeof(**name_types) * (*num_names));
1145 if (! (*sid_mem) || ! (*names) || ! (*name_types)) {
1146 smb_panic("lookup_groupmem out of memory");
1149 for (i=0; i<(*num_names); i++) {
1150 (*sid_mem)[i] = centry_sid(centry, mem_ctx);
1151 (*names)[i] = centry_string(centry, mem_ctx);
1152 (*name_types)[i] = centry_uint32(centry);
1156 status = centry->status;
1158 DEBUG(10,("lookup_groupmem: [Cached] - cached info for domain %s status %s\n",
1159 domain->name, get_friendly_nt_error_msg(status) ));
1161 centry_free(centry);
1168 (*name_types) = NULL;
1171 if (wcache_server_down(domain)) {
1172 return NT_STATUS_SERVER_DISABLED;
1175 DEBUG(10,("lookup_groupmem: [Cached] - doing backend query for info for domain %s\n",
1178 status = domain->backend->lookup_groupmem(domain, mem_ctx, group_sid, num_names,
1179 sid_mem, names, name_types);
1182 refresh_sequence_number(domain, False);
1183 centry = centry_start(domain, status);
1186 centry_put_uint32(centry, *num_names);
1187 for (i=0; i<(*num_names); i++) {
1188 centry_put_sid(centry, (*sid_mem)[i]);
1189 centry_put_string(centry, (*names)[i]);
1190 centry_put_uint32(centry, (*name_types)[i]);
1192 centry_end(centry, "GM/%s", sid_to_string(sid_string, group_sid));
1193 centry_free(centry);
1199 /* find the sequence number for a domain */
1200 static NTSTATUS sequence_number(struct winbindd_domain *domain, uint32 *seq)
1202 refresh_sequence_number(domain, False);
1204 *seq = domain->sequence_number;
1206 return NT_STATUS_OK;
1209 /* enumerate trusted domains */
1210 static NTSTATUS trusted_domains(struct winbindd_domain *domain,
1211 TALLOC_CTX *mem_ctx,
1212 uint32 *num_domains,
1219 DEBUG(10,("trusted_domains: [Cached] - doing backend query for info for domain %s\n",
1222 /* we don't cache this call */
1223 return domain->backend->trusted_domains(domain, mem_ctx, num_domains,
1224 names, alt_names, dom_sids);
1227 /* find the domain sid */
1228 static NTSTATUS domain_sid(struct winbindd_domain *domain, DOM_SID *sid)
1232 DEBUG(10,("domain_sid: [Cached] - doing backend query for info for domain %s\n",
1235 /* we don't cache this call */
1236 return domain->backend->domain_sid(domain, sid);
1239 /* find the alternate names for the domain, if any */
1240 static NTSTATUS alternate_name(struct winbindd_domain *domain)
1244 DEBUG(10,("alternate_name: [Cached] - doing backend query for info for domain %s\n",
1247 /* we don't cache this call */
1248 return domain->backend->alternate_name(domain);
1251 /* the ADS backend methods are exposed via this structure */
1252 struct winbindd_methods cache_methods = {