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.
25 struct winbind_cache {
26 struct winbindd_methods *backend;
32 uint32 sequence_number;
37 #define WINBINDD_MAX_CACHE_SIZE (50*1024*1024)
39 static struct winbind_cache *wcache;
42 void wcache_flush_cache(void)
44 extern BOOL opt_nocache;
48 tdb_close(wcache->tdb);
51 if (opt_nocache) return;
53 wcache->tdb = tdb_open_log(lock_path("winbindd_cache.tdb"), 5000,
54 TDB_DEFAULT, O_RDWR | O_CREAT | O_TRUNC, 0600);
57 DEBUG(0,("Failed to open winbindd_cache.tdb!\n"));
61 void winbindd_check_cache_size(time_t t)
63 static time_t last_check_time;
66 if (last_check_time == (time_t)0)
69 if (t - last_check_time < 60 && t - last_check_time > 0)
72 if (wcache == NULL || wcache->tdb == NULL) {
73 DEBUG(0, ("Unable to check size of tdb cache - cache not open !\n"));
77 if (fstat(wcache->tdb->fd, &st) == -1) {
78 DEBUG(0, ("Unable to check size of tdb cache %s!\n", strerror(errno) ));
82 if (st.st_size > WINBINDD_MAX_CACHE_SIZE) {
83 DEBUG(10,("flushing cache due to size (%lu) > (%lu)\n",
84 (unsigned long)st.st_size,
85 (unsigned long)WINBINDD_MAX_CACHE_SIZE));
90 /* get the winbind_cache structure */
91 static struct winbind_cache *get_cache(struct winbindd_domain *domain)
93 extern struct winbindd_methods msrpc_methods;
94 struct winbind_cache *ret = wcache;
98 ret = smb_xmalloc(sizeof(*ret));
100 switch (lp_security()) {
103 extern struct winbindd_methods ads_methods;
104 ret->backend = &ads_methods;
109 ret->backend = &msrpc_methods;
113 wcache_flush_cache();
119 free a centry structure
121 static void centry_free(struct cache_entry *centry)
124 SAFE_FREE(centry->data);
130 pull a uint32 from a cache entry
132 static uint32 centry_uint32(struct cache_entry *centry)
135 if (centry->len - centry->ofs < 4) {
136 DEBUG(0,("centry corruption? needed 4 bytes, have %d\n",
137 centry->len - centry->ofs));
138 smb_panic("centry_uint32");
140 ret = IVAL(centry->data, centry->ofs);
146 pull a uint8 from a cache entry
148 static uint8 centry_uint8(struct cache_entry *centry)
151 if (centry->len - centry->ofs < 1) {
152 DEBUG(0,("centry corruption? needed 1 bytes, have %d\n",
153 centry->len - centry->ofs));
154 smb_panic("centry_uint32");
156 ret = CVAL(centry->data, centry->ofs);
161 /* pull a string from a cache entry, using the supplied
164 static char *centry_string(struct cache_entry *centry, TALLOC_CTX *mem_ctx)
169 len = centry_uint8(centry);
172 /* a deliberate NULL string */
176 if (centry->len - centry->ofs < len) {
177 DEBUG(0,("centry corruption? needed %d bytes, have %d\n",
178 len, centry->len - centry->ofs));
179 smb_panic("centry_string");
182 ret = talloc(mem_ctx, len+1);
184 smb_panic("centry_string out of memory\n");
186 memcpy(ret,centry->data + centry->ofs, len);
192 /* the server is considered down if it can't give us a sequence number */
193 static BOOL wcache_server_down(struct winbindd_domain *domain)
195 if (!wcache->tdb) return False;
196 return (domain->sequence_number == DOM_SEQUENCE_NONE);
201 refresh the domain sequence number. If force is True
202 then always refresh it, no matter how recently we fetched it
204 static void refresh_sequence_number(struct winbindd_domain *domain, BOOL force)
209 time_diff = time(NULL) - domain->last_seq_check;
211 /* see if we have to refetch the domain sequence number */
212 if (!force && (time_diff < lp_winbind_cache_time())) {
216 status = wcache->backend->sequence_number(domain, &domain->sequence_number);
218 if (!NT_STATUS_IS_OK(status)) {
219 domain->sequence_number = DOM_SEQUENCE_NONE;
222 domain->last_seq_check = time(NULL);
226 decide if a cache entry has expired
228 static BOOL centry_expired(struct winbindd_domain *domain, struct cache_entry *centry)
230 /* if the server is OK and our cache entry came from when it was down then
231 the entry is invalid */
232 if (domain->sequence_number != DOM_SEQUENCE_NONE &&
233 centry->sequence_number == DOM_SEQUENCE_NONE) {
237 /* if the server is down or the cache entry is not older than the
238 current sequence number then it is OK */
239 if (wcache_server_down(domain) ||
240 centry->sequence_number == domain->sequence_number) {
249 fetch an entry from the cache, with a varargs key. auto-fetch the sequence
250 number and return status
252 static struct cache_entry *wcache_fetch(struct winbind_cache *cache,
253 struct winbindd_domain *domain,
254 const char *format, ...)
259 struct cache_entry *centry;
262 refresh_sequence_number(domain, False);
264 va_start(ap, format);
265 smb_xvasprintf(&kstr, format, ap);
269 key.dsize = strlen(kstr);
270 data = tdb_fetch(wcache->tdb, key);
277 centry = smb_xmalloc(sizeof(*centry));
278 centry->data = data.dptr;
279 centry->len = data.dsize;
282 if (centry->len < 8) {
283 /* huh? corrupt cache? */
288 centry->status = NT_STATUS(centry_uint32(centry));
289 centry->sequence_number = centry_uint32(centry);
291 if (centry_expired(domain, centry)) {
300 make sure we have at least len bytes available in a centry
302 static void centry_expand(struct cache_entry *centry, uint32 len)
305 if (centry->len - centry->ofs >= len) return;
307 p = realloc(centry->data, centry->len);
309 DEBUG(0,("out of memory: needed %d bytes in centry_expand\n", centry->len));
310 smb_panic("out of memory in centry_expand");
316 push a uint32 into a centry
318 static void centry_put_uint32(struct cache_entry *centry, uint32 v)
320 centry_expand(centry, 4);
321 SIVAL(centry->data, centry->ofs, v);
326 push a uint8 into a centry
328 static void centry_put_uint8(struct cache_entry *centry, uint8 v)
330 centry_expand(centry, 1);
331 SCVAL(centry->data, centry->ofs, v);
336 push a string into a centry
338 static void centry_put_string(struct cache_entry *centry, const char *s)
343 /* null strings are marked as len 0xFFFF */
344 centry_put_uint8(centry, 0xFF);
349 /* can't handle more than 254 char strings. Truncating is probably best */
350 if (len > 254) len = 254;
351 centry_put_uint8(centry, len);
352 centry_expand(centry, len);
353 memcpy(centry->data + centry->ofs, s, len);
358 start a centry for output. When finished, call centry_end()
360 struct cache_entry *centry_start(struct winbindd_domain *domain, NTSTATUS status)
362 struct cache_entry *centry;
364 if (!wcache->tdb) return NULL;
366 centry = smb_xmalloc(sizeof(*centry));
368 centry->len = 8192; /* reasonable default */
369 centry->data = smb_xmalloc(centry->len);
371 centry->sequence_number = domain->sequence_number;
372 centry_put_uint32(centry, NT_STATUS_V(status));
373 centry_put_uint32(centry, centry->sequence_number);
378 finish a centry and write it to the tdb
380 static void centry_end(struct cache_entry *centry, const char *format, ...)
386 va_start(ap, format);
387 smb_xvasprintf(&kstr, format, ap);
391 key.dsize = strlen(kstr);
392 data.dptr = centry->data;
393 data.dsize = centry->ofs;
395 tdb_store(wcache->tdb, key, data, TDB_REPLACE);
399 /* form a sid from the domain plus rid */
400 static DOM_SID *form_sid(struct winbindd_domain *domain, uint32 rid)
403 sid_copy(&sid, &domain->sid);
404 sid_append_rid(&sid, rid);
408 static void wcache_save_name_to_sid(struct winbindd_domain *domain, NTSTATUS status,
409 const char *name, DOM_SID *sid, enum SID_NAME_USE type)
411 struct cache_entry *centry;
414 centry = centry_start(domain, status);
417 centry_expand(centry, len);
418 centry_put_uint32(centry, type);
419 sid_linearize(centry->data + centry->ofs, len, sid);
421 centry_end(centry, "NS/%s/%s", domain->name, name);
425 static void wcache_save_sid_to_name(struct winbindd_domain *domain, NTSTATUS status,
426 DOM_SID *sid, const char *name, enum SID_NAME_USE type, uint32 rid)
428 struct cache_entry *centry;
430 centry = centry_start(domain, status);
432 if (NT_STATUS_IS_OK(status)) {
433 centry_put_uint32(centry, type);
434 centry_put_string(centry, name);
436 centry_end(centry, "SN/%s/%d", domain->name, rid);
441 static void wcache_save_user(struct winbindd_domain *domain, NTSTATUS status, WINBIND_USERINFO *info)
443 struct cache_entry *centry;
445 centry = centry_start(domain, status);
447 centry_put_string(centry, info->acct_name);
448 centry_put_string(centry, info->full_name);
449 centry_put_uint32(centry, info->user_rid);
450 centry_put_uint32(centry, info->group_rid);
451 centry_end(centry, "U/%s/%x", domain->name, info->user_rid);
456 /* Query display info. This is the basic user list fn */
457 static NTSTATUS query_user_list(struct winbindd_domain *domain,
460 WINBIND_USERINFO **info)
462 struct winbind_cache *cache = get_cache(domain);
463 struct cache_entry *centry = NULL;
467 if (!cache->tdb) goto do_query;
469 centry = wcache_fetch(cache, domain, "UL/%s", domain->name);
470 if (!centry) goto do_query;
472 *num_entries = centry_uint32(centry);
474 if (*num_entries == 0) goto do_cached;
476 (*info) = talloc(mem_ctx, sizeof(**info) * (*num_entries));
477 if (! (*info)) smb_panic("query_user_list out of memory");
478 for (i=0; i<(*num_entries); i++) {
479 (*info)[i].acct_name = centry_string(centry, mem_ctx);
480 (*info)[i].full_name = centry_string(centry, mem_ctx);
481 (*info)[i].user_rid = centry_uint32(centry);
482 (*info)[i].group_rid = centry_uint32(centry);
486 status = centry->status;
494 if (wcache_server_down(domain)) {
495 return NT_STATUS_SERVER_DISABLED;
498 status = cache->backend->query_user_list(domain, mem_ctx, num_entries, info);
501 refresh_sequence_number(domain, True);
502 centry = centry_start(domain, status);
503 if (!centry) goto skip_save;
504 centry_put_uint32(centry, *num_entries);
505 for (i=0; i<(*num_entries); i++) {
506 centry_put_string(centry, (*info)[i].acct_name);
507 centry_put_string(centry, (*info)[i].full_name);
508 centry_put_uint32(centry, (*info)[i].user_rid);
509 centry_put_uint32(centry, (*info)[i].group_rid);
510 if (cache->backend->consistent) {
511 /* when the backend is consistent we can pre-prime some mappings */
512 wcache_save_name_to_sid(domain, NT_STATUS_OK,
513 (*info)[i].acct_name,
514 form_sid(domain, (*info)[i].user_rid),
516 wcache_save_sid_to_name(domain, NT_STATUS_OK,
517 form_sid(domain, (*info)[i].user_rid),
518 (*info)[i].acct_name,
519 SID_NAME_USER, (*info)[i].user_rid);
520 wcache_save_user(domain, NT_STATUS_OK, &(*info)[i]);
523 centry_end(centry, "UL/%s", domain->name);
530 /* list all domain groups */
531 static NTSTATUS enum_dom_groups(struct winbindd_domain *domain,
534 struct acct_info **info)
536 struct winbind_cache *cache = get_cache(domain);
537 struct cache_entry *centry = NULL;
541 if (!cache->tdb) goto do_query;
543 centry = wcache_fetch(cache, domain, "GL/%s", domain->name);
544 if (!centry) goto do_query;
546 *num_entries = centry_uint32(centry);
548 if (*num_entries == 0) goto do_cached;
550 (*info) = talloc(mem_ctx, sizeof(**info) * (*num_entries));
551 if (! (*info)) smb_panic("enum_dom_groups out of memory");
552 for (i=0; i<(*num_entries); i++) {
553 fstrcpy((*info)[i].acct_name, centry_string(centry, mem_ctx));
554 fstrcpy((*info)[i].acct_desc, centry_string(centry, mem_ctx));
555 (*info)[i].rid = centry_uint32(centry);
559 status = centry->status;
567 if (wcache_server_down(domain)) {
568 return NT_STATUS_SERVER_DISABLED;
571 status = cache->backend->enum_dom_groups(domain, mem_ctx, num_entries, info);
574 refresh_sequence_number(domain, True);
575 centry = centry_start(domain, status);
576 if (!centry) goto skip_save;
577 centry_put_uint32(centry, *num_entries);
578 for (i=0; i<(*num_entries); i++) {
579 centry_put_string(centry, (*info)[i].acct_name);
580 centry_put_string(centry, (*info)[i].acct_desc);
581 centry_put_uint32(centry, (*info)[i].rid);
583 centry_end(centry, "GL/%s", domain->name);
591 /* convert a single name to a sid in a domain */
592 static NTSTATUS name_to_sid(struct winbindd_domain *domain,
595 enum SID_NAME_USE *type)
597 struct winbind_cache *cache = get_cache(domain);
598 struct cache_entry *centry = NULL;
601 if (!cache->tdb) goto do_query;
603 centry = wcache_fetch(cache, domain, "NS/%s/%s", domain->name, name);
604 if (!centry) goto do_query;
605 *type = centry_uint32(centry);
606 sid_parse(centry->data + centry->ofs, centry->len - centry->ofs, sid);
608 status = centry->status;
615 if (wcache_server_down(domain)) {
616 return NT_STATUS_SERVER_DISABLED;
618 status = cache->backend->name_to_sid(domain, name, sid, type);
621 wcache_save_name_to_sid(domain, status, name, sid, *type);
626 /* convert a sid to a user or group name. The sid is guaranteed to be in the domain
628 static NTSTATUS sid_to_name(struct winbindd_domain *domain,
632 enum SID_NAME_USE *type)
634 struct winbind_cache *cache = get_cache(domain);
635 struct cache_entry *centry = NULL;
639 sid_peek_rid(sid, &rid);
641 if (!cache->tdb) goto do_query;
643 centry = wcache_fetch(cache, domain, "SN/%s/%d", domain->name, rid);
644 if (!centry) goto do_query;
645 if (NT_STATUS_IS_OK(centry->status)) {
646 *type = centry_uint32(centry);
647 *name = centry_string(centry, mem_ctx);
649 status = centry->status;
656 if (wcache_server_down(domain)) {
657 return NT_STATUS_SERVER_DISABLED;
659 status = cache->backend->sid_to_name(domain, mem_ctx, sid, name, type);
662 refresh_sequence_number(domain, True);
663 wcache_save_sid_to_name(domain, status, sid, *name, *type, rid);
669 /* Lookup user information from a rid */
670 static NTSTATUS query_user(struct winbindd_domain *domain,
673 WINBIND_USERINFO *info)
675 struct winbind_cache *cache = get_cache(domain);
676 struct cache_entry *centry = NULL;
679 if (!cache->tdb) goto do_query;
681 centry = wcache_fetch(cache, domain, "U/%s/%x", domain->name, user_rid);
682 if (!centry) goto do_query;
684 info->acct_name = centry_string(centry, mem_ctx);
685 info->full_name = centry_string(centry, mem_ctx);
686 info->user_rid = centry_uint32(centry);
687 info->group_rid = centry_uint32(centry);
688 status = centry->status;
695 if (wcache_server_down(domain)) {
696 return NT_STATUS_SERVER_DISABLED;
699 status = cache->backend->query_user(domain, mem_ctx, user_rid, info);
702 refresh_sequence_number(domain, True);
703 wcache_save_user(domain, status, info);
709 /* Lookup groups a user is a member of. */
710 static NTSTATUS lookup_usergroups(struct winbindd_domain *domain,
713 uint32 *num_groups, uint32 **user_gids)
715 struct winbind_cache *cache = get_cache(domain);
716 struct cache_entry *centry = NULL;
720 if (!cache->tdb) goto do_query;
722 centry = wcache_fetch(cache, domain, "UG/%s/%x", domain->name, user_rid);
723 if (!centry) goto do_query;
725 *num_groups = centry_uint32(centry);
727 if (*num_groups == 0) goto do_cached;
729 (*user_gids) = talloc(mem_ctx, sizeof(**user_gids) * (*num_groups));
730 if (! (*user_gids)) smb_panic("lookup_usergroups out of memory");
731 for (i=0; i<(*num_groups); i++) {
732 (*user_gids)[i] = centry_uint32(centry);
736 status = centry->status;
744 if (wcache_server_down(domain)) {
745 return NT_STATUS_SERVER_DISABLED;
747 status = cache->backend->lookup_usergroups(domain, mem_ctx, user_rid, num_groups, user_gids);
750 refresh_sequence_number(domain, True);
751 centry = centry_start(domain, status);
752 if (!centry) goto skip_save;
753 centry_put_uint32(centry, *num_groups);
754 for (i=0; i<(*num_groups); i++) {
755 centry_put_uint32(centry, (*user_gids)[i]);
757 centry_end(centry, "UG/%s/%x", domain->name, user_rid);
765 static NTSTATUS lookup_groupmem(struct winbindd_domain *domain,
767 uint32 group_rid, uint32 *num_names,
768 uint32 **rid_mem, char ***names,
771 struct winbind_cache *cache = get_cache(domain);
772 struct cache_entry *centry = NULL;
776 if (!cache->tdb) goto do_query;
778 centry = wcache_fetch(cache, domain, "GM/%s/%x", domain->name, group_rid);
779 if (!centry) goto do_query;
781 *num_names = centry_uint32(centry);
783 if (*num_names == 0) goto do_cached;
785 (*rid_mem) = talloc(mem_ctx, sizeof(**rid_mem) * (*num_names));
786 (*names) = talloc(mem_ctx, sizeof(**names) * (*num_names));
787 (*name_types) = talloc(mem_ctx, sizeof(**name_types) * (*num_names));
789 if (! (*rid_mem) || ! (*names) || ! (*name_types)) {
790 smb_panic("lookup_groupmem out of memory");
793 for (i=0; i<(*num_names); i++) {
794 (*rid_mem)[i] = centry_uint32(centry);
795 (*names)[i] = centry_string(centry, mem_ctx);
796 (*name_types)[i] = centry_uint32(centry);
800 status = centry->status;
808 (*name_types) = NULL;
811 if (wcache_server_down(domain)) {
812 return NT_STATUS_SERVER_DISABLED;
814 status = cache->backend->lookup_groupmem(domain, mem_ctx, group_rid, num_names,
815 rid_mem, names, name_types);
818 refresh_sequence_number(domain, True);
819 centry = centry_start(domain, status);
820 if (!centry) goto skip_save;
821 centry_put_uint32(centry, *num_names);
822 for (i=0; i<(*num_names); i++) {
823 centry_put_uint32(centry, (*rid_mem)[i]);
824 centry_put_string(centry, (*names)[i]);
825 centry_put_uint32(centry, (*name_types)[i]);
827 centry_end(centry, "GM/%s/%x", domain->name, group_rid);
834 /* find the sequence number for a domain */
835 static NTSTATUS sequence_number(struct winbindd_domain *domain, uint32 *seq)
837 refresh_sequence_number(domain, False);
839 *seq = domain->sequence_number;
844 /* enumerate trusted domains */
845 static NTSTATUS trusted_domains(struct winbindd_domain *domain,
851 struct winbind_cache *cache = get_cache(domain);
853 /* we don't cache this call */
854 return cache->backend->trusted_domains(domain, mem_ctx, num_domains,
858 /* find the domain sid */
859 static NTSTATUS domain_sid(struct winbindd_domain *domain, DOM_SID *sid)
861 struct winbind_cache *cache = get_cache(domain);
863 /* we don't cache this call */
864 return cache->backend->domain_sid(domain, sid);
867 /* the ADS backend methods are exposed via this structure */
868 struct winbindd_methods cache_methods = {