2 Unix SMB/Netbios 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.
25 struct winbind_cache {
26 struct winbindd_methods *backend;
32 uint32 sequence_number;
37 static struct winbind_cache *wcache;
40 void wcache_flush_cache(void)
42 extern BOOL opt_nocache;
46 tdb_close(wcache->tdb);
49 if (opt_nocache) return;
51 wcache->tdb = tdb_open_log(lock_path("winbindd_cache.tdb"), 5000,
52 TDB_NOLOCK, O_RDWR | O_CREAT | O_TRUNC, 0600);
55 DEBUG(0,("Failed to open winbindd_cache.tdb!\n"));
59 /* get the winbind_cache structure */
60 static struct winbind_cache *get_cache(struct winbindd_domain *domain)
62 extern struct winbindd_methods msrpc_methods;
63 struct winbind_cache *ret = wcache;
67 ret = smb_xmalloc(sizeof(*ret));
69 switch (lp_security()) {
72 extern struct winbindd_methods ads_methods;
73 ret->backend = &ads_methods;
78 ret->backend = &msrpc_methods;
88 free a centry structure
90 static void centry_free(struct cache_entry *centry)
93 SAFE_FREE(centry->data);
99 pull a uint32 from a cache entry
101 static uint32 centry_uint32(struct cache_entry *centry)
104 if (centry->len - centry->ofs < 4) {
105 DEBUG(0,("centry corruption? needed 4 bytes, have %d\n",
106 centry->len - centry->ofs));
107 smb_panic("centry_uint32");
109 ret = IVAL(centry->data, centry->ofs);
115 pull a uint8 from a cache entry
117 static uint8 centry_uint8(struct cache_entry *centry)
120 if (centry->len - centry->ofs < 1) {
121 DEBUG(0,("centry corruption? needed 1 bytes, have %d\n",
122 centry->len - centry->ofs));
123 smb_panic("centry_uint32");
125 ret = CVAL(centry->data, centry->ofs);
130 /* pull a string from a cache entry, using the supplied
133 static char *centry_string(struct cache_entry *centry, TALLOC_CTX *mem_ctx)
138 len = centry_uint8(centry);
141 /* a deliberate NULL string */
145 if (centry->len - centry->ofs < len) {
146 DEBUG(0,("centry corruption? needed %d bytes, have %d\n",
147 len, centry->len - centry->ofs));
148 smb_panic("centry_string");
151 ret = talloc(mem_ctx, len+1);
153 smb_panic("centry_string out of memory\n");
155 memcpy(ret,centry->data + centry->ofs, len);
161 /* the server is considered down if it can't give us a sequence number */
162 static BOOL wcache_server_down(struct winbindd_domain *domain)
164 if (!wcache->tdb) return False;
165 return (domain->sequence_number == DOM_SEQUENCE_NONE);
170 refresh the domain sequence number. If force is True
171 then always refresh it, no matter how recently we fetched it
173 static void refresh_sequence_number(struct winbindd_domain *domain, BOOL force)
178 time_diff = time(NULL) - domain->last_seq_check;
180 /* see if we have to refetch the domain sequence number */
181 if (!force && (time_diff < lp_winbind_cache_time())) {
185 status = wcache->backend->sequence_number(domain, &domain->sequence_number);
187 if (!NT_STATUS_IS_OK(status)) {
188 domain->sequence_number = DOM_SEQUENCE_NONE;
191 domain->last_seq_check = time(NULL);
195 decide if a cache entry has expired
197 static BOOL centry_expired(struct winbindd_domain *domain, struct cache_entry *centry)
199 /* if the server is OK and our cache entry came from when it was down then
200 the entry is invalid */
201 if (domain->sequence_number != DOM_SEQUENCE_NONE &&
202 centry->sequence_number == DOM_SEQUENCE_NONE) {
206 /* if the server is down or the cache entry is not older than the
207 current sequence number then it is OK */
208 if (wcache_server_down(domain) ||
209 centry->sequence_number == domain->sequence_number) {
218 fetch an entry from the cache, with a varargs key. auto-fetch the sequence
219 number and return status
221 static struct cache_entry *wcache_fetch(struct winbind_cache *cache,
222 struct winbindd_domain *domain,
223 const char *format, ...)
228 struct cache_entry *centry;
231 refresh_sequence_number(domain, False);
233 va_start(ap, format);
234 smb_xvasprintf(&kstr, format, ap);
238 key.dsize = strlen(kstr);
239 data = tdb_fetch(wcache->tdb, key);
246 centry = smb_xmalloc(sizeof(*centry));
247 centry->data = data.dptr;
248 centry->len = data.dsize;
251 if (centry->len < 8) {
252 /* huh? corrupt cache? */
257 centry->status = NT_STATUS(centry_uint32(centry));
258 centry->sequence_number = centry_uint32(centry);
260 if (centry_expired(domain, centry)) {
269 make sure we have at least len bytes available in a centry
271 static void centry_expand(struct cache_entry *centry, uint32 len)
274 if (centry->len - centry->ofs >= len) return;
276 p = realloc(centry->data, centry->len);
278 DEBUG(0,("out of memory: needed %d bytes in centry_expand\n", centry->len));
279 smb_panic("out of memory in centry_expand");
285 push a uint32 into a centry
287 static void centry_put_uint32(struct cache_entry *centry, uint32 v)
289 centry_expand(centry, 4);
290 SIVAL(centry->data, centry->ofs, v);
295 push a uint8 into a centry
297 static void centry_put_uint8(struct cache_entry *centry, uint8 v)
299 centry_expand(centry, 1);
300 SCVAL(centry->data, centry->ofs, v);
305 push a string into a centry
307 static void centry_put_string(struct cache_entry *centry, const char *s)
312 /* null strings are marked as len 0xFFFF */
313 centry_put_uint8(centry, 0xFF);
318 /* can't handle more than 254 char strings. Truncating is probably best */
319 if (len > 254) len = 254;
320 centry_put_uint8(centry, len);
321 centry_expand(centry, len);
322 memcpy(centry->data + centry->ofs, s, len);
327 start a centry for output. When finished, call centry_end()
329 struct cache_entry *centry_start(struct winbindd_domain *domain, NTSTATUS status)
331 struct cache_entry *centry;
333 if (!wcache->tdb) return NULL;
335 centry = smb_xmalloc(sizeof(*centry));
337 centry->len = 8192; /* reasonable default */
338 centry->data = smb_xmalloc(centry->len);
340 centry->sequence_number = domain->sequence_number;
341 centry_put_uint32(centry, NT_STATUS_V(status));
342 centry_put_uint32(centry, centry->sequence_number);
347 finish a centry and write it to the tdb
349 static void centry_end(struct cache_entry *centry, const char *format, ...)
355 va_start(ap, format);
356 smb_xvasprintf(&kstr, format, ap);
360 key.dsize = strlen(kstr);
361 data.dptr = centry->data;
362 data.dsize = centry->ofs;
364 tdb_store(wcache->tdb, key, data, TDB_REPLACE);
368 /* form a name with the domain part stuck on the front */
369 static char *prepend_domain(struct winbindd_domain *domain, const char *name)
372 snprintf(s, sizeof(s), "%s%s%s", domain->name, lp_winbind_separator(), name);
376 /* form a sid from the domain plus rid */
377 static DOM_SID *form_sid(struct winbindd_domain *domain, uint32 rid)
380 sid_copy(&sid, &domain->sid);
381 sid_append_rid(&sid, rid);
385 static void wcache_save_name_to_sid(struct winbindd_domain *domain, NTSTATUS status,
386 const char *name, DOM_SID *sid, enum SID_NAME_USE type)
388 struct cache_entry *centry;
391 centry = centry_start(domain, status);
394 centry_expand(centry, len);
395 centry_put_uint32(centry, type);
396 sid_linearize(centry->data + centry->ofs, len, sid);
398 centry_end(centry, "NS/%s/%s", domain->name, name);
402 static void wcache_save_sid_to_name(struct winbindd_domain *domain, NTSTATUS status,
403 DOM_SID *sid, const char *name, enum SID_NAME_USE type, uint32 rid)
405 struct cache_entry *centry;
407 centry = centry_start(domain, status);
409 if (NT_STATUS_IS_OK(status)) {
410 centry_put_uint32(centry, type);
411 centry_put_string(centry, name);
413 centry_end(centry, "SN/%s/%d", domain->name, rid);
418 static void wcache_save_user(struct winbindd_domain *domain, NTSTATUS status, WINBIND_USERINFO *info)
420 struct cache_entry *centry;
422 centry = centry_start(domain, status);
424 centry_put_string(centry, info->acct_name);
425 centry_put_string(centry, info->full_name);
426 centry_put_uint32(centry, info->user_rid);
427 centry_put_uint32(centry, info->group_rid);
428 centry_end(centry, "U/%s/%x", domain->name, info->user_rid);
433 /* Query display info. This is the basic user list fn */
434 static NTSTATUS query_user_list(struct winbindd_domain *domain,
437 WINBIND_USERINFO **info)
439 struct winbind_cache *cache = get_cache(domain);
440 struct cache_entry *centry = NULL;
444 if (!cache->tdb) goto do_query;
446 centry = wcache_fetch(cache, domain, "UL/%s", domain->name);
447 if (!centry) goto do_query;
449 *num_entries = centry_uint32(centry);
451 if (*num_entries == 0) goto do_cached;
453 (*info) = talloc(mem_ctx, sizeof(**info) * (*num_entries));
454 if (! (*info)) smb_panic("query_user_list out of memory");
455 for (i=0; i<(*num_entries); i++) {
456 (*info)[i].acct_name = centry_string(centry, mem_ctx);
457 (*info)[i].full_name = centry_string(centry, mem_ctx);
458 (*info)[i].user_rid = centry_uint32(centry);
459 (*info)[i].group_rid = centry_uint32(centry);
463 status = centry->status;
471 if (wcache_server_down(domain)) {
472 return NT_STATUS_SERVER_DISABLED;
475 status = cache->backend->query_user_list(domain, mem_ctx, num_entries, info);
478 refresh_sequence_number(domain, True);
479 centry = centry_start(domain, status);
480 if (!centry) goto skip_save;
481 centry_put_uint32(centry, *num_entries);
482 for (i=0; i<(*num_entries); i++) {
483 centry_put_string(centry, (*info)[i].acct_name);
484 centry_put_string(centry, (*info)[i].full_name);
485 centry_put_uint32(centry, (*info)[i].user_rid);
486 centry_put_uint32(centry, (*info)[i].group_rid);
487 if (cache->backend->consistent) {
488 /* when the backend is consistent we can pre-prime some mappings */
489 wcache_save_name_to_sid(domain, NT_STATUS_OK,
490 prepend_domain(domain, (*info)[i].acct_name),
491 form_sid(domain, (*info)[i].user_rid),
493 wcache_save_sid_to_name(domain, NT_STATUS_OK,
494 form_sid(domain, (*info)[i].user_rid),
495 prepend_domain(domain, (*info)[i].acct_name),
496 SID_NAME_USER, (*info)[i].user_rid);
497 wcache_save_user(domain, NT_STATUS_OK, &(*info)[i]);
500 centry_end(centry, "UL/%s", domain->name);
507 /* list all domain groups */
508 static NTSTATUS enum_dom_groups(struct winbindd_domain *domain,
511 struct acct_info **info)
513 struct winbind_cache *cache = get_cache(domain);
514 struct cache_entry *centry = NULL;
518 if (!cache->tdb) goto do_query;
520 centry = wcache_fetch(cache, domain, "GL/%s", domain->name);
521 if (!centry) goto do_query;
523 *num_entries = centry_uint32(centry);
525 if (*num_entries == 0) goto do_cached;
527 (*info) = talloc(mem_ctx, sizeof(**info) * (*num_entries));
528 if (! (*info)) smb_panic("enum_dom_groups out of memory");
529 for (i=0; i<(*num_entries); i++) {
530 fstrcpy((*info)[i].acct_name, centry_string(centry, mem_ctx));
531 fstrcpy((*info)[i].acct_desc, centry_string(centry, mem_ctx));
532 (*info)[i].rid = centry_uint32(centry);
536 status = centry->status;
544 if (wcache_server_down(domain)) {
545 return NT_STATUS_SERVER_DISABLED;
548 status = cache->backend->enum_dom_groups(domain, mem_ctx, num_entries, info);
551 refresh_sequence_number(domain, True);
552 centry = centry_start(domain, status);
553 if (!centry) goto skip_save;
554 centry_put_uint32(centry, *num_entries);
555 for (i=0; i<(*num_entries); i++) {
556 centry_put_string(centry, (*info)[i].acct_name);
557 centry_put_string(centry, (*info)[i].acct_desc);
558 centry_put_uint32(centry, (*info)[i].rid);
560 centry_end(centry, "GL/%s", domain->name);
568 /* convert a single name to a sid in a domain */
569 static NTSTATUS name_to_sid(struct winbindd_domain *domain,
572 enum SID_NAME_USE *type)
574 struct winbind_cache *cache = get_cache(domain);
575 struct cache_entry *centry = NULL;
578 if (!cache->tdb) goto do_query;
580 centry = wcache_fetch(cache, domain, "NS/%s/%s", domain->name, name);
581 if (!centry) goto do_query;
582 *type = centry_uint32(centry);
583 sid_parse(centry->data + centry->ofs, centry->len - centry->ofs, sid);
585 status = centry->status;
592 if (wcache_server_down(domain)) {
593 return NT_STATUS_SERVER_DISABLED;
595 status = cache->backend->name_to_sid(domain, name, sid, type);
598 wcache_save_name_to_sid(domain, status, name, sid, *type);
603 /* convert a sid to a user or group name. The sid is guaranteed to be in the domain
605 static NTSTATUS sid_to_name(struct winbindd_domain *domain,
609 enum SID_NAME_USE *type)
611 struct winbind_cache *cache = get_cache(domain);
612 struct cache_entry *centry = NULL;
616 sid_peek_rid(sid, &rid);
618 if (!cache->tdb) goto do_query;
620 centry = wcache_fetch(cache, domain, "SN/%s/%d", domain->name, rid);
621 if (!centry) goto do_query;
622 if (NT_STATUS_IS_OK(centry->status)) {
623 *type = centry_uint32(centry);
624 *name = centry_string(centry, mem_ctx);
626 status = centry->status;
633 if (wcache_server_down(domain)) {
634 return NT_STATUS_SERVER_DISABLED;
636 status = cache->backend->sid_to_name(domain, mem_ctx, sid, name, type);
639 refresh_sequence_number(domain, True);
640 wcache_save_sid_to_name(domain, status, sid, *name, *type, rid);
646 /* Lookup user information from a rid */
647 static NTSTATUS query_user(struct winbindd_domain *domain,
650 WINBIND_USERINFO *info)
652 struct winbind_cache *cache = get_cache(domain);
653 struct cache_entry *centry = NULL;
656 if (!cache->tdb) goto do_query;
658 centry = wcache_fetch(cache, domain, "U/%s/%x", domain->name, user_rid);
659 if (!centry) goto do_query;
661 info->acct_name = centry_string(centry, mem_ctx);
662 info->full_name = centry_string(centry, mem_ctx);
663 info->user_rid = centry_uint32(centry);
664 info->group_rid = centry_uint32(centry);
665 status = centry->status;
672 if (wcache_server_down(domain)) {
673 return NT_STATUS_SERVER_DISABLED;
676 status = cache->backend->query_user(domain, mem_ctx, user_rid, info);
679 refresh_sequence_number(domain, True);
680 wcache_save_user(domain, status, info);
686 /* Lookup groups a user is a member of. */
687 static NTSTATUS lookup_usergroups(struct winbindd_domain *domain,
690 uint32 *num_groups, uint32 **user_gids)
692 struct winbind_cache *cache = get_cache(domain);
693 struct cache_entry *centry = NULL;
697 if (!cache->tdb) goto do_query;
699 centry = wcache_fetch(cache, domain, "UG/%s/%x", domain->name, user_rid);
700 if (!centry) goto do_query;
702 *num_groups = centry_uint32(centry);
704 if (*num_groups == 0) goto do_cached;
706 (*user_gids) = talloc(mem_ctx, sizeof(**user_gids) * (*num_groups));
707 if (! (*user_gids)) smb_panic("lookup_usergroups out of memory");
708 for (i=0; i<(*num_groups); i++) {
709 (*user_gids)[i] = centry_uint32(centry);
713 status = centry->status;
721 if (wcache_server_down(domain)) {
722 return NT_STATUS_SERVER_DISABLED;
724 status = cache->backend->lookup_usergroups(domain, mem_ctx, user_rid, num_groups, user_gids);
727 refresh_sequence_number(domain, True);
728 centry = centry_start(domain, status);
729 if (!centry) goto skip_save;
730 centry_put_uint32(centry, *num_groups);
731 for (i=0; i<(*num_groups); i++) {
732 centry_put_uint32(centry, (*user_gids)[i]);
734 centry_end(centry, "UG/%s/%x", domain->name, user_rid);
742 static NTSTATUS lookup_groupmem(struct winbindd_domain *domain,
744 uint32 group_rid, uint32 *num_names,
745 uint32 **rid_mem, char ***names,
748 struct winbind_cache *cache = get_cache(domain);
749 struct cache_entry *centry = NULL;
753 if (!cache->tdb) goto do_query;
755 centry = wcache_fetch(cache, domain, "GM/%s/%x", domain->name, group_rid);
756 if (!centry) goto do_query;
758 *num_names = centry_uint32(centry);
760 if (*num_names == 0) goto do_cached;
762 (*rid_mem) = talloc(mem_ctx, sizeof(**rid_mem) * (*num_names));
763 (*names) = talloc(mem_ctx, sizeof(**names) * (*num_names));
764 (*name_types) = talloc(mem_ctx, sizeof(**name_types) * (*num_names));
766 if (! (*rid_mem) || ! (*names) || ! (*name_types)) {
767 smb_panic("lookup_groupmem out of memory");
770 for (i=0; i<(*num_names); i++) {
771 (*rid_mem)[i] = centry_uint32(centry);
772 (*names)[i] = centry_string(centry, mem_ctx);
773 (*name_types)[i] = centry_uint32(centry);
777 status = centry->status;
785 (*name_types) = NULL;
788 if (wcache_server_down(domain)) {
789 return NT_STATUS_SERVER_DISABLED;
791 status = cache->backend->lookup_groupmem(domain, mem_ctx, group_rid, num_names,
792 rid_mem, names, name_types);
795 refresh_sequence_number(domain, True);
796 centry = centry_start(domain, status);
797 if (!centry) goto skip_save;
798 centry_put_uint32(centry, *num_names);
799 for (i=0; i<(*num_names); i++) {
800 centry_put_uint32(centry, (*rid_mem)[i]);
801 centry_put_string(centry, (*names)[i]);
802 centry_put_uint32(centry, (*name_types)[i]);
804 centry_end(centry, "GM/%s/%x", domain->name, group_rid);
811 /* find the sequence number for a domain */
812 static NTSTATUS sequence_number(struct winbindd_domain *domain, uint32 *seq)
814 refresh_sequence_number(domain, False);
816 *seq = domain->sequence_number;
821 /* enumerate trusted domains */
822 static NTSTATUS trusted_domains(struct winbindd_domain *domain,
828 struct winbind_cache *cache = get_cache(domain);
830 /* we don't cache this call */
831 return cache->backend->trusted_domains(domain, mem_ctx, num_domains,
835 /* find the domain sid */
836 static NTSTATUS domain_sid(struct winbindd_domain *domain, DOM_SID *sid)
838 struct winbind_cache *cache = get_cache(domain);
840 /* we don't cache this call */
841 return cache->backend->domain_sid(domain, sid);
844 /* the ADS backend methods are exposed via this structure */
845 struct winbindd_methods cache_methods = {