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 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 sid from the domain plus rid */
369 static DOM_SID *form_sid(struct winbindd_domain *domain, uint32 rid)
372 sid_copy(&sid, &domain->sid);
373 sid_append_rid(&sid, rid);
377 static void wcache_save_name_to_sid(struct winbindd_domain *domain, NTSTATUS status,
378 const char *name, DOM_SID *sid, enum SID_NAME_USE type)
380 struct cache_entry *centry;
383 centry = centry_start(domain, status);
386 centry_expand(centry, len);
387 centry_put_uint32(centry, type);
388 sid_linearize(centry->data + centry->ofs, len, sid);
390 centry_end(centry, "NS/%s/%s", domain->name, name);
394 static void wcache_save_sid_to_name(struct winbindd_domain *domain, NTSTATUS status,
395 DOM_SID *sid, const char *name, enum SID_NAME_USE type, uint32 rid)
397 struct cache_entry *centry;
399 centry = centry_start(domain, status);
401 if (NT_STATUS_IS_OK(status)) {
402 centry_put_uint32(centry, type);
403 centry_put_string(centry, name);
405 centry_end(centry, "SN/%s/%d", domain->name, rid);
410 static void wcache_save_user(struct winbindd_domain *domain, NTSTATUS status, WINBIND_USERINFO *info)
412 struct cache_entry *centry;
414 centry = centry_start(domain, status);
416 centry_put_string(centry, info->acct_name);
417 centry_put_string(centry, info->full_name);
418 centry_put_uint32(centry, info->user_rid);
419 centry_put_uint32(centry, info->group_rid);
420 centry_end(centry, "U/%s/%x", domain->name, info->user_rid);
425 /* Query display info. This is the basic user list fn */
426 static NTSTATUS query_user_list(struct winbindd_domain *domain,
429 WINBIND_USERINFO **info)
431 struct winbind_cache *cache = get_cache(domain);
432 struct cache_entry *centry = NULL;
436 if (!cache->tdb) goto do_query;
438 centry = wcache_fetch(cache, domain, "UL/%s", domain->name);
439 if (!centry) goto do_query;
441 *num_entries = centry_uint32(centry);
443 if (*num_entries == 0) goto do_cached;
445 (*info) = talloc(mem_ctx, sizeof(**info) * (*num_entries));
446 if (! (*info)) smb_panic("query_user_list out of memory");
447 for (i=0; i<(*num_entries); i++) {
448 (*info)[i].acct_name = centry_string(centry, mem_ctx);
449 (*info)[i].full_name = centry_string(centry, mem_ctx);
450 (*info)[i].user_rid = centry_uint32(centry);
451 (*info)[i].group_rid = centry_uint32(centry);
455 status = centry->status;
463 if (wcache_server_down(domain)) {
464 return NT_STATUS_SERVER_DISABLED;
467 status = cache->backend->query_user_list(domain, mem_ctx, num_entries, info);
470 refresh_sequence_number(domain, True);
471 centry = centry_start(domain, status);
472 if (!centry) goto skip_save;
473 centry_put_uint32(centry, *num_entries);
474 for (i=0; i<(*num_entries); i++) {
475 centry_put_string(centry, (*info)[i].acct_name);
476 centry_put_string(centry, (*info)[i].full_name);
477 centry_put_uint32(centry, (*info)[i].user_rid);
478 centry_put_uint32(centry, (*info)[i].group_rid);
479 if (cache->backend->consistent) {
480 /* when the backend is consistent we can pre-prime some mappings */
481 wcache_save_name_to_sid(domain, NT_STATUS_OK,
482 (*info)[i].acct_name,
483 form_sid(domain, (*info)[i].user_rid),
485 wcache_save_sid_to_name(domain, NT_STATUS_OK,
486 form_sid(domain, (*info)[i].user_rid),
487 (*info)[i].acct_name,
488 SID_NAME_USER, (*info)[i].user_rid);
489 wcache_save_user(domain, NT_STATUS_OK, &(*info)[i]);
492 centry_end(centry, "UL/%s", domain->name);
499 /* list all domain groups */
500 static NTSTATUS enum_dom_groups(struct winbindd_domain *domain,
503 struct acct_info **info)
505 struct winbind_cache *cache = get_cache(domain);
506 struct cache_entry *centry = NULL;
510 if (!cache->tdb) goto do_query;
512 centry = wcache_fetch(cache, domain, "GL/%s", domain->name);
513 if (!centry) goto do_query;
515 *num_entries = centry_uint32(centry);
517 if (*num_entries == 0) goto do_cached;
519 (*info) = talloc(mem_ctx, sizeof(**info) * (*num_entries));
520 if (! (*info)) smb_panic("enum_dom_groups out of memory");
521 for (i=0; i<(*num_entries); i++) {
522 fstrcpy((*info)[i].acct_name, centry_string(centry, mem_ctx));
523 fstrcpy((*info)[i].acct_desc, centry_string(centry, mem_ctx));
524 (*info)[i].rid = centry_uint32(centry);
528 status = centry->status;
536 if (wcache_server_down(domain)) {
537 return NT_STATUS_SERVER_DISABLED;
540 status = cache->backend->enum_dom_groups(domain, mem_ctx, num_entries, info);
543 refresh_sequence_number(domain, True);
544 centry = centry_start(domain, status);
545 if (!centry) goto skip_save;
546 centry_put_uint32(centry, *num_entries);
547 for (i=0; i<(*num_entries); i++) {
548 centry_put_string(centry, (*info)[i].acct_name);
549 centry_put_string(centry, (*info)[i].acct_desc);
550 centry_put_uint32(centry, (*info)[i].rid);
552 centry_end(centry, "GL/%s", domain->name);
560 /* convert a single name to a sid in a domain */
561 static NTSTATUS name_to_sid(struct winbindd_domain *domain,
564 enum SID_NAME_USE *type)
566 struct winbind_cache *cache = get_cache(domain);
567 struct cache_entry *centry = NULL;
570 if (!cache->tdb) goto do_query;
572 centry = wcache_fetch(cache, domain, "NS/%s/%s", domain->name, name);
573 if (!centry) goto do_query;
574 *type = centry_uint32(centry);
575 sid_parse(centry->data + centry->ofs, centry->len - centry->ofs, sid);
577 status = centry->status;
584 if (wcache_server_down(domain)) {
585 return NT_STATUS_SERVER_DISABLED;
587 status = cache->backend->name_to_sid(domain, name, sid, type);
590 wcache_save_name_to_sid(domain, status, name, sid, *type);
595 /* convert a sid to a user or group name. The sid is guaranteed to be in the domain
597 static NTSTATUS sid_to_name(struct winbindd_domain *domain,
601 enum SID_NAME_USE *type)
603 struct winbind_cache *cache = get_cache(domain);
604 struct cache_entry *centry = NULL;
608 sid_peek_rid(sid, &rid);
610 if (!cache->tdb) goto do_query;
612 centry = wcache_fetch(cache, domain, "SN/%s/%d", domain->name, rid);
613 if (!centry) goto do_query;
614 if (NT_STATUS_IS_OK(centry->status)) {
615 *type = centry_uint32(centry);
616 *name = centry_string(centry, mem_ctx);
618 status = centry->status;
625 if (wcache_server_down(domain)) {
626 return NT_STATUS_SERVER_DISABLED;
628 status = cache->backend->sid_to_name(domain, mem_ctx, sid, name, type);
631 refresh_sequence_number(domain, True);
632 wcache_save_sid_to_name(domain, status, sid, *name, *type, rid);
638 /* Lookup user information from a rid */
639 static NTSTATUS query_user(struct winbindd_domain *domain,
642 WINBIND_USERINFO *info)
644 struct winbind_cache *cache = get_cache(domain);
645 struct cache_entry *centry = NULL;
648 if (!cache->tdb) goto do_query;
650 centry = wcache_fetch(cache, domain, "U/%s/%x", domain->name, user_rid);
651 if (!centry) goto do_query;
653 info->acct_name = centry_string(centry, mem_ctx);
654 info->full_name = centry_string(centry, mem_ctx);
655 info->user_rid = centry_uint32(centry);
656 info->group_rid = centry_uint32(centry);
657 status = centry->status;
664 if (wcache_server_down(domain)) {
665 return NT_STATUS_SERVER_DISABLED;
668 status = cache->backend->query_user(domain, mem_ctx, user_rid, info);
671 refresh_sequence_number(domain, True);
672 wcache_save_user(domain, status, info);
678 /* Lookup groups a user is a member of. */
679 static NTSTATUS lookup_usergroups(struct winbindd_domain *domain,
682 uint32 *num_groups, uint32 **user_gids)
684 struct winbind_cache *cache = get_cache(domain);
685 struct cache_entry *centry = NULL;
689 if (!cache->tdb) goto do_query;
691 centry = wcache_fetch(cache, domain, "UG/%s/%x", domain->name, user_rid);
692 if (!centry) goto do_query;
694 *num_groups = centry_uint32(centry);
696 if (*num_groups == 0) goto do_cached;
698 (*user_gids) = talloc(mem_ctx, sizeof(**user_gids) * (*num_groups));
699 if (! (*user_gids)) smb_panic("lookup_usergroups out of memory");
700 for (i=0; i<(*num_groups); i++) {
701 (*user_gids)[i] = centry_uint32(centry);
705 status = centry->status;
713 if (wcache_server_down(domain)) {
714 return NT_STATUS_SERVER_DISABLED;
716 status = cache->backend->lookup_usergroups(domain, mem_ctx, user_rid, num_groups, user_gids);
719 refresh_sequence_number(domain, True);
720 centry = centry_start(domain, status);
721 if (!centry) goto skip_save;
722 centry_put_uint32(centry, *num_groups);
723 for (i=0; i<(*num_groups); i++) {
724 centry_put_uint32(centry, (*user_gids)[i]);
726 centry_end(centry, "UG/%s/%x", domain->name, user_rid);
734 static NTSTATUS lookup_groupmem(struct winbindd_domain *domain,
736 uint32 group_rid, uint32 *num_names,
737 uint32 **rid_mem, char ***names,
740 struct winbind_cache *cache = get_cache(domain);
741 struct cache_entry *centry = NULL;
745 if (!cache->tdb) goto do_query;
747 centry = wcache_fetch(cache, domain, "GM/%s/%x", domain->name, group_rid);
748 if (!centry) goto do_query;
750 *num_names = centry_uint32(centry);
752 if (*num_names == 0) goto do_cached;
754 (*rid_mem) = talloc(mem_ctx, sizeof(**rid_mem) * (*num_names));
755 (*names) = talloc(mem_ctx, sizeof(**names) * (*num_names));
756 (*name_types) = talloc(mem_ctx, sizeof(**name_types) * (*num_names));
758 if (! (*rid_mem) || ! (*names) || ! (*name_types)) {
759 smb_panic("lookup_groupmem out of memory");
762 for (i=0; i<(*num_names); i++) {
763 (*rid_mem)[i] = centry_uint32(centry);
764 (*names)[i] = centry_string(centry, mem_ctx);
765 (*name_types)[i] = centry_uint32(centry);
769 status = centry->status;
777 (*name_types) = NULL;
780 if (wcache_server_down(domain)) {
781 return NT_STATUS_SERVER_DISABLED;
783 status = cache->backend->lookup_groupmem(domain, mem_ctx, group_rid, num_names,
784 rid_mem, names, name_types);
787 refresh_sequence_number(domain, True);
788 centry = centry_start(domain, status);
789 if (!centry) goto skip_save;
790 centry_put_uint32(centry, *num_names);
791 for (i=0; i<(*num_names); i++) {
792 centry_put_uint32(centry, (*rid_mem)[i]);
793 centry_put_string(centry, (*names)[i]);
794 centry_put_uint32(centry, (*name_types)[i]);
796 centry_end(centry, "GM/%s/%x", domain->name, group_rid);
803 /* find the sequence number for a domain */
804 static NTSTATUS sequence_number(struct winbindd_domain *domain, uint32 *seq)
806 refresh_sequence_number(domain, False);
808 *seq = domain->sequence_number;
813 /* enumerate trusted domains */
814 static NTSTATUS trusted_domains(struct winbindd_domain *domain,
820 struct winbind_cache *cache = get_cache(domain);
822 /* we don't cache this call */
823 return cache->backend->trusted_domains(domain, mem_ctx, num_domains,
827 /* find the domain sid */
828 static NTSTATUS domain_sid(struct winbindd_domain *domain, DOM_SID *sid)
830 struct winbind_cache *cache = get_cache(domain);
832 /* we don't cache this call */
833 return cache->backend->domain_sid(domain, sid);
836 /* the ADS backend methods are exposed via this structure */
837 struct winbindd_methods cache_methods = {