r7415: * big change -- volker's new async winbindd from trunk
[Samba/gbeck.git] / source / nsswitch / winbindd_cache.c
blob90ccb43a6ec1cea78b43396dc553b8ca95234273
1 /*
2 Unix SMB/CIFS implementation.
4 Winbind cache backend functions
6 Copyright (C) Andrew Tridgell 2001
7 Copyright (C) Gerald Carter 2003
8 Copyright (C) Volker Lendecke 2005
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include "includes.h"
27 #include "winbindd.h"
29 #undef DBGC_CLASS
30 #define DBGC_CLASS DBGC_WINBIND
32 struct winbind_cache {
33 TDB_CONTEXT *tdb;
36 struct cache_entry {
37 NTSTATUS status;
38 uint32 sequence_number;
39 uint8 *data;
40 uint32 len, ofs;
43 #define WINBINDD_MAX_CACHE_SIZE (50*1024*1024)
45 static struct winbind_cache *wcache;
47 /* flush the cache */
48 void wcache_flush_cache(void)
50 extern BOOL opt_nocache;
52 if (!wcache)
53 return;
54 if (wcache->tdb) {
55 tdb_close(wcache->tdb);
56 wcache->tdb = NULL;
58 if (opt_nocache)
59 return;
61 wcache->tdb = tdb_open_log(lock_path("winbindd_cache.tdb"), 5000,
62 TDB_CLEAR_IF_FIRST, O_RDWR|O_CREAT, 0600);
64 if (!wcache->tdb) {
65 DEBUG(0,("Failed to open winbindd_cache.tdb!\n"));
67 DEBUG(10,("wcache_flush_cache success\n"));
70 void winbindd_check_cache_size(time_t t)
72 static time_t last_check_time;
73 struct stat st;
75 if (last_check_time == (time_t)0)
76 last_check_time = t;
78 if (t - last_check_time < 60 && t - last_check_time > 0)
79 return;
81 if (wcache == NULL || wcache->tdb == NULL) {
82 DEBUG(0, ("Unable to check size of tdb cache - cache not open !\n"));
83 return;
86 if (fstat(wcache->tdb->fd, &st) == -1) {
87 DEBUG(0, ("Unable to check size of tdb cache %s!\n", strerror(errno) ));
88 return;
91 if (st.st_size > WINBINDD_MAX_CACHE_SIZE) {
92 DEBUG(10,("flushing cache due to size (%lu) > (%lu)\n",
93 (unsigned long)st.st_size,
94 (unsigned long)WINBINDD_MAX_CACHE_SIZE));
95 wcache_flush_cache();
99 /* get the winbind_cache structure */
100 static struct winbind_cache *get_cache(struct winbindd_domain *domain)
102 struct winbind_cache *ret = wcache;
104 if (!domain->backend) {
105 extern struct winbindd_methods reconnect_methods;
106 switch (lp_security()) {
107 #ifdef HAVE_ADS
108 case SEC_ADS: {
109 extern struct winbindd_methods ads_methods;
110 /* always obey the lp_security parameter for our domain */
111 if (domain->primary) {
112 domain->backend = &ads_methods;
113 break;
116 /* only use ADS for native modes at the momment.
117 The problem is the correct detection of mixed
118 mode domains from NT4 BDC's --jerry */
120 if ( domain->native_mode ) {
121 DEBUG(5,("get_cache: Setting ADS methods for domain %s\n",
122 domain->name));
123 domain->backend = &ads_methods;
124 break;
127 /* fall through */
129 #endif
130 default:
131 DEBUG(5,("get_cache: Setting MS-RPC methods for domain %s\n",
132 domain->name));
133 domain->backend = &reconnect_methods;
137 if (ret)
138 return ret;
140 ret = SMB_XMALLOC_P(struct winbind_cache);
141 ZERO_STRUCTP(ret);
143 wcache = ret;
144 wcache_flush_cache();
146 return ret;
150 free a centry structure
152 static void centry_free(struct cache_entry *centry)
154 if (!centry)
155 return;
156 SAFE_FREE(centry->data);
157 free(centry);
161 pull a uint32 from a cache entry
163 static uint32 centry_uint32(struct cache_entry *centry)
165 uint32 ret;
166 if (centry->len - centry->ofs < 4) {
167 DEBUG(0,("centry corruption? needed 4 bytes, have %d\n",
168 centry->len - centry->ofs));
169 smb_panic("centry_uint32");
171 ret = IVAL(centry->data, centry->ofs);
172 centry->ofs += 4;
173 return ret;
177 pull a uint8 from a cache entry
179 static uint8 centry_uint8(struct cache_entry *centry)
181 uint8 ret;
182 if (centry->len - centry->ofs < 1) {
183 DEBUG(0,("centry corruption? needed 1 bytes, have %d\n",
184 centry->len - centry->ofs));
185 smb_panic("centry_uint32");
187 ret = CVAL(centry->data, centry->ofs);
188 centry->ofs += 1;
189 return ret;
192 /* pull a string from a cache entry, using the supplied
193 talloc context
195 static char *centry_string(struct cache_entry *centry, TALLOC_CTX *mem_ctx)
197 uint32 len;
198 char *ret;
200 len = centry_uint8(centry);
202 if (len == 0xFF) {
203 /* a deliberate NULL string */
204 return NULL;
207 if (centry->len - centry->ofs < len) {
208 DEBUG(0,("centry corruption? needed %d bytes, have %d\n",
209 len, centry->len - centry->ofs));
210 smb_panic("centry_string");
213 if (mem_ctx != NULL)
214 ret = TALLOC(mem_ctx, len+1);
215 else
216 ret = SMB_MALLOC(len+1);
217 if (!ret) {
218 smb_panic("centry_string out of memory\n");
220 memcpy(ret,centry->data + centry->ofs, len);
221 ret[len] = 0;
222 centry->ofs += len;
223 return ret;
226 /* pull a string from a cache entry, using the supplied
227 talloc context
229 static BOOL centry_sid(struct cache_entry *centry, DOM_SID *sid)
231 char *sid_string;
232 sid_string = centry_string(centry, NULL);
233 if (!string_to_sid(sid, sid_string)) {
234 return False;
236 SAFE_FREE(sid_string);
237 return True;
240 /* the server is considered down if it can't give us a sequence number */
241 static BOOL wcache_server_down(struct winbindd_domain *domain)
243 BOOL ret;
245 if (!wcache->tdb)
246 return False;
248 ret = (domain->sequence_number == DOM_SEQUENCE_NONE);
250 if (ret)
251 DEBUG(10,("wcache_server_down: server for Domain %s down\n",
252 domain->name ));
253 return ret;
256 static NTSTATUS fetch_cache_seqnum( struct winbindd_domain *domain, time_t now )
258 TDB_DATA data;
259 fstring key;
260 uint32 time_diff;
262 if (!wcache->tdb) {
263 DEBUG(10,("fetch_cache_seqnum: tdb == NULL\n"));
264 return NT_STATUS_UNSUCCESSFUL;
267 fstr_sprintf( key, "SEQNUM/%s", domain->name );
269 data = tdb_fetch_bystring( wcache->tdb, key );
270 if ( !data.dptr || data.dsize!=8 ) {
271 DEBUG(10,("fetch_cache_seqnum: invalid data size key [%s]\n", key ));
272 return NT_STATUS_UNSUCCESSFUL;
275 domain->sequence_number = IVAL(data.dptr, 0);
276 domain->last_seq_check = IVAL(data.dptr, 4);
278 SAFE_FREE(data.dptr);
280 /* have we expired? */
282 time_diff = now - domain->last_seq_check;
283 if ( time_diff > lp_winbind_cache_time() ) {
284 DEBUG(10,("fetch_cache_seqnum: timeout [%s][%u @ %u]\n",
285 domain->name, domain->sequence_number,
286 (uint32)domain->last_seq_check));
287 return NT_STATUS_UNSUCCESSFUL;
290 DEBUG(10,("fetch_cache_seqnum: success [%s][%u @ %u]\n",
291 domain->name, domain->sequence_number,
292 (uint32)domain->last_seq_check));
294 return NT_STATUS_OK;
297 static NTSTATUS store_cache_seqnum( struct winbindd_domain *domain )
299 TDB_DATA data, key;
300 fstring key_str;
301 char buf[8];
303 if (!wcache->tdb) {
304 DEBUG(10,("store_cache_seqnum: tdb == NULL\n"));
305 return NT_STATUS_UNSUCCESSFUL;
308 fstr_sprintf( key_str, "SEQNUM/%s", domain->name );
309 key.dptr = key_str;
310 key.dsize = strlen(key_str)+1;
312 SIVAL(buf, 0, domain->sequence_number);
313 SIVAL(buf, 4, domain->last_seq_check);
314 data.dptr = buf;
315 data.dsize = 8;
317 if ( tdb_store( wcache->tdb, key, data, TDB_REPLACE) == -1 ) {
318 DEBUG(10,("store_cache_seqnum: tdb_store fail key [%s]\n", key_str ));
319 return NT_STATUS_UNSUCCESSFUL;
322 DEBUG(10,("store_cache_seqnum: success [%s][%u @ %u]\n",
323 domain->name, domain->sequence_number,
324 (uint32)domain->last_seq_check));
326 return NT_STATUS_OK;
330 refresh the domain sequence number. If force is True
331 then always refresh it, no matter how recently we fetched it
334 static void refresh_sequence_number(struct winbindd_domain *domain, BOOL force)
336 NTSTATUS status;
337 unsigned time_diff;
338 time_t t = time(NULL);
339 unsigned cache_time = lp_winbind_cache_time();
341 get_cache( domain );
343 #if 0 /* JERRY -- disable as the default cache time is now 5 minutes */
344 /* trying to reconnect is expensive, don't do it too often */
345 if (domain->sequence_number == DOM_SEQUENCE_NONE) {
346 cache_time *= 8;
348 #endif
350 time_diff = t - domain->last_seq_check;
352 /* see if we have to refetch the domain sequence number */
353 if (!force && (time_diff < cache_time)) {
354 DEBUG(10, ("refresh_sequence_number: %s time ok\n", domain->name));
355 goto done;
358 /* try to get the sequence number from the tdb cache first */
359 /* this will update the timestamp as well */
361 status = fetch_cache_seqnum( domain, t );
362 if ( NT_STATUS_IS_OK(status) )
363 goto done;
365 /* important! make sure that we know if this is a native
366 mode domain or not */
368 if ( !domain->initialized )
369 set_dc_type_and_flags( domain );
371 status = domain->backend->sequence_number(domain, &domain->sequence_number);
373 if (!NT_STATUS_IS_OK(status)) {
374 domain->sequence_number = DOM_SEQUENCE_NONE;
377 domain->last_status = status;
378 domain->last_seq_check = time(NULL);
380 /* save the new sequence number ni the cache */
381 store_cache_seqnum( domain );
383 done:
384 DEBUG(10, ("refresh_sequence_number: %s seq number is now %d\n",
385 domain->name, domain->sequence_number));
387 return;
391 decide if a cache entry has expired
393 static BOOL centry_expired(struct winbindd_domain *domain, const char *keystr, struct cache_entry *centry)
395 /* if the server is OK and our cache entry came from when it was down then
396 the entry is invalid */
397 if (domain->sequence_number != DOM_SEQUENCE_NONE &&
398 centry->sequence_number == DOM_SEQUENCE_NONE) {
399 DEBUG(10,("centry_expired: Key %s for domain %s invalid sequence.\n",
400 keystr, domain->name ));
401 return True;
404 /* if the server is down or the cache entry is not older than the
405 current sequence number then it is OK */
406 if (wcache_server_down(domain) ||
407 centry->sequence_number == domain->sequence_number) {
408 DEBUG(10,("centry_expired: Key %s for domain %s is good.\n",
409 keystr, domain->name ));
410 return False;
413 DEBUG(10,("centry_expired: Key %s for domain %s expired\n",
414 keystr, domain->name ));
416 /* it's expired */
417 return True;
421 fetch an entry from the cache, with a varargs key. auto-fetch the sequence
422 number and return status
424 static struct cache_entry *wcache_fetch(struct winbind_cache *cache,
425 struct winbindd_domain *domain,
426 const char *format, ...) PRINTF_ATTRIBUTE(3,4);
427 static struct cache_entry *wcache_fetch(struct winbind_cache *cache,
428 struct winbindd_domain *domain,
429 const char *format, ...)
431 va_list ap;
432 char *kstr;
433 TDB_DATA data;
434 struct cache_entry *centry;
435 TDB_DATA key;
437 refresh_sequence_number(domain, False);
439 va_start(ap, format);
440 smb_xvasprintf(&kstr, format, ap);
441 va_end(ap);
443 key.dptr = kstr;
444 key.dsize = strlen(kstr);
445 data = tdb_fetch(wcache->tdb, key);
446 if (!data.dptr) {
447 /* a cache miss */
448 free(kstr);
449 return NULL;
452 centry = SMB_XMALLOC_P(struct cache_entry);
453 centry->data = (unsigned char *)data.dptr;
454 centry->len = data.dsize;
455 centry->ofs = 0;
457 if (centry->len < 8) {
458 /* huh? corrupt cache? */
459 DEBUG(10,("wcache_fetch: Corrupt cache for key %s domain %s (len < 8) ?\n",
460 kstr, domain->name ));
461 centry_free(centry);
462 free(kstr);
463 return NULL;
466 centry->status = NT_STATUS(centry_uint32(centry));
467 centry->sequence_number = centry_uint32(centry);
469 if (centry_expired(domain, kstr, centry)) {
470 extern BOOL opt_dual_daemon;
472 DEBUG(10,("wcache_fetch: entry %s expired for domain %s\n",
473 kstr, domain->name ));
475 if (opt_dual_daemon) {
476 extern BOOL background_process;
477 background_process = True;
478 DEBUG(10,("wcache_fetch: background processing expired entry %s for domain %s\n",
479 kstr, domain->name ));
480 } else {
481 centry_free(centry);
482 free(kstr);
483 return NULL;
487 DEBUG(10,("wcache_fetch: returning entry %s for domain %s\n",
488 kstr, domain->name ));
490 free(kstr);
491 return centry;
495 make sure we have at least len bytes available in a centry
497 static void centry_expand(struct cache_entry *centry, uint32 len)
499 uint8 *p;
500 if (centry->len - centry->ofs >= len)
501 return;
502 centry->len *= 2;
503 p = SMB_REALLOC(centry->data, centry->len);
504 if (!p) {
505 DEBUG(0,("out of memory: needed %d bytes in centry_expand\n", centry->len));
506 smb_panic("out of memory in centry_expand");
508 centry->data = p;
512 push a uint32 into a centry
514 static void centry_put_uint32(struct cache_entry *centry, uint32 v)
516 centry_expand(centry, 4);
517 SIVAL(centry->data, centry->ofs, v);
518 centry->ofs += 4;
522 push a uint8 into a centry
524 static void centry_put_uint8(struct cache_entry *centry, uint8 v)
526 centry_expand(centry, 1);
527 SCVAL(centry->data, centry->ofs, v);
528 centry->ofs += 1;
532 push a string into a centry
534 static void centry_put_string(struct cache_entry *centry, const char *s)
536 int len;
538 if (!s) {
539 /* null strings are marked as len 0xFFFF */
540 centry_put_uint8(centry, 0xFF);
541 return;
544 len = strlen(s);
545 /* can't handle more than 254 char strings. Truncating is probably best */
546 if (len > 254)
547 len = 254;
548 centry_put_uint8(centry, len);
549 centry_expand(centry, len);
550 memcpy(centry->data + centry->ofs, s, len);
551 centry->ofs += len;
554 static void centry_put_sid(struct cache_entry *centry, const DOM_SID *sid)
556 fstring sid_string;
557 centry_put_string(centry, sid_to_string(sid_string, sid));
561 start a centry for output. When finished, call centry_end()
563 struct cache_entry *centry_start(struct winbindd_domain *domain, NTSTATUS status)
565 struct cache_entry *centry;
567 if (!wcache->tdb)
568 return NULL;
570 centry = SMB_XMALLOC_P(struct cache_entry);
572 centry->len = 8192; /* reasonable default */
573 centry->data = SMB_XMALLOC_ARRAY(char, centry->len);
574 centry->ofs = 0;
575 centry->sequence_number = domain->sequence_number;
576 centry_put_uint32(centry, NT_STATUS_V(status));
577 centry_put_uint32(centry, centry->sequence_number);
578 return centry;
582 finish a centry and write it to the tdb
584 static void centry_end(struct cache_entry *centry, const char *format, ...) PRINTF_ATTRIBUTE(2,3);
585 static void centry_end(struct cache_entry *centry, const char *format, ...)
587 va_list ap;
588 char *kstr;
589 TDB_DATA key, data;
591 va_start(ap, format);
592 smb_xvasprintf(&kstr, format, ap);
593 va_end(ap);
595 key.dptr = kstr;
596 key.dsize = strlen(kstr);
597 data.dptr = (char *)centry->data;
598 data.dsize = centry->ofs;
600 tdb_store(wcache->tdb, key, data, TDB_REPLACE);
601 free(kstr);
604 static void wcache_save_name_to_sid(struct winbindd_domain *domain,
605 NTSTATUS status, const char *domain_name,
606 const char *name, const DOM_SID *sid,
607 enum SID_NAME_USE type)
609 struct cache_entry *centry;
610 fstring uname;
612 centry = centry_start(domain, status);
613 if (!centry)
614 return;
615 centry_put_uint32(centry, type);
616 centry_put_sid(centry, sid);
617 fstrcpy(uname, name);
618 strupper_m(uname);
619 centry_end(centry, "NS/%s/%s", domain_name, uname);
620 DEBUG(10,("wcache_save_name_to_sid: %s -> %s\n", uname,
621 sid_string_static(sid)));
622 centry_free(centry);
625 static void wcache_save_sid_to_name(struct winbindd_domain *domain, NTSTATUS status,
626 const DOM_SID *sid, const char *domain_name, const char *name, enum SID_NAME_USE type)
628 struct cache_entry *centry;
629 fstring sid_string;
631 centry = centry_start(domain, status);
632 if (!centry)
633 return;
634 if (NT_STATUS_IS_OK(status)) {
635 centry_put_uint32(centry, type);
636 centry_put_string(centry, domain_name);
637 centry_put_string(centry, name);
639 centry_end(centry, "SN/%s", sid_to_string(sid_string, sid));
640 DEBUG(10,("wcache_save_sid_to_name: %s -> %s\n", sid_string, name));
641 centry_free(centry);
645 static void wcache_save_user(struct winbindd_domain *domain, NTSTATUS status, WINBIND_USERINFO *info)
647 struct cache_entry *centry;
648 fstring sid_string;
650 centry = centry_start(domain, status);
651 if (!centry)
652 return;
653 centry_put_string(centry, info->acct_name);
654 centry_put_string(centry, info->full_name);
655 centry_put_sid(centry, &info->user_sid);
656 centry_put_sid(centry, &info->group_sid);
657 centry_end(centry, "U/%s", sid_to_string(sid_string, &info->user_sid));
658 DEBUG(10,("wcache_save_user: %s (acct_name %s)\n", sid_string, info->acct_name));
659 centry_free(centry);
663 /* Query display info. This is the basic user list fn */
664 static NTSTATUS query_user_list(struct winbindd_domain *domain,
665 TALLOC_CTX *mem_ctx,
666 uint32 *num_entries,
667 WINBIND_USERINFO **info)
669 struct winbind_cache *cache = get_cache(domain);
670 struct cache_entry *centry = NULL;
671 NTSTATUS status;
672 unsigned int i, retry;
674 if (!cache->tdb)
675 goto do_query;
677 centry = wcache_fetch(cache, domain, "UL/%s", domain->name);
678 if (!centry)
679 goto do_query;
681 *num_entries = centry_uint32(centry);
683 if (*num_entries == 0)
684 goto do_cached;
686 (*info) = TALLOC_ARRAY(mem_ctx, WINBIND_USERINFO, *num_entries);
687 if (! (*info))
688 smb_panic("query_user_list out of memory");
689 for (i=0; i<(*num_entries); i++) {
690 (*info)[i].acct_name = centry_string(centry, mem_ctx);
691 (*info)[i].full_name = centry_string(centry, mem_ctx);
692 centry_sid(centry, &(*info)[i].user_sid);
693 centry_sid(centry, &(*info)[i].group_sid);
696 do_cached:
697 status = centry->status;
699 DEBUG(10,("query_user_list: [Cached] - cached list for domain %s status %s\n",
700 domain->name, get_friendly_nt_error_msg(status) ));
702 centry_free(centry);
703 return status;
705 do_query:
706 *num_entries = 0;
707 *info = NULL;
709 /* Return status value returned by seq number check */
711 if (!NT_STATUS_IS_OK(domain->last_status))
712 return domain->last_status;
714 /* Put the query_user_list() in a retry loop. There appears to be
715 * some bug either with Windows 2000 or Samba's handling of large
716 * rpc replies. This manifests itself as sudden disconnection
717 * at a random point in the enumeration of a large (60k) user list.
718 * The retry loop simply tries the operation again. )-: It's not
719 * pretty but an acceptable workaround until we work out what the
720 * real problem is. */
722 retry = 0;
723 do {
725 DEBUG(10,("query_user_list: [Cached] - doing backend query for list for domain %s\n",
726 domain->name ));
728 status = domain->backend->query_user_list(domain, mem_ctx, num_entries, info);
729 if (!NT_STATUS_IS_OK(status))
730 DEBUG(3, ("query_user_list: returned 0x%08x, "
731 "retrying\n", NT_STATUS_V(status)));
732 if (NT_STATUS_EQUAL(status, NT_STATUS_UNSUCCESSFUL)) {
733 DEBUG(3, ("query_user_list: flushing "
734 "connection cache\n"));
735 invalidate_cm_connection(&domain->conn);
738 } while (NT_STATUS_V(status) == NT_STATUS_V(NT_STATUS_UNSUCCESSFUL) &&
739 (retry++ < 5));
741 /* and save it */
742 refresh_sequence_number(domain, False);
743 centry = centry_start(domain, status);
744 if (!centry)
745 goto skip_save;
746 centry_put_uint32(centry, *num_entries);
747 for (i=0; i<(*num_entries); i++) {
748 centry_put_string(centry, (*info)[i].acct_name);
749 centry_put_string(centry, (*info)[i].full_name);
750 centry_put_sid(centry, &(*info)[i].user_sid);
751 centry_put_sid(centry, &(*info)[i].group_sid);
752 if (domain->backend->consistent) {
753 /* when the backend is consistent we can pre-prime some mappings */
754 wcache_save_name_to_sid(domain, NT_STATUS_OK,
755 domain->name,
756 (*info)[i].acct_name,
757 &(*info)[i].user_sid,
758 SID_NAME_USER);
759 wcache_save_sid_to_name(domain, NT_STATUS_OK,
760 &(*info)[i].user_sid,
761 domain->name,
762 (*info)[i].acct_name,
763 SID_NAME_USER);
764 wcache_save_user(domain, NT_STATUS_OK, &(*info)[i]);
767 centry_end(centry, "UL/%s", domain->name);
768 centry_free(centry);
770 skip_save:
771 return status;
774 /* list all domain groups */
775 static NTSTATUS enum_dom_groups(struct winbindd_domain *domain,
776 TALLOC_CTX *mem_ctx,
777 uint32 *num_entries,
778 struct acct_info **info)
780 struct winbind_cache *cache = get_cache(domain);
781 struct cache_entry *centry = NULL;
782 NTSTATUS status;
783 unsigned int i;
785 if (!cache->tdb)
786 goto do_query;
788 centry = wcache_fetch(cache, domain, "GL/%s/domain", domain->name);
789 if (!centry)
790 goto do_query;
792 *num_entries = centry_uint32(centry);
794 if (*num_entries == 0)
795 goto do_cached;
797 (*info) = TALLOC_ARRAY(mem_ctx, struct acct_info, *num_entries);
798 if (! (*info))
799 smb_panic("enum_dom_groups out of memory");
800 for (i=0; i<(*num_entries); i++) {
801 fstrcpy((*info)[i].acct_name, centry_string(centry, mem_ctx));
802 fstrcpy((*info)[i].acct_desc, centry_string(centry, mem_ctx));
803 (*info)[i].rid = centry_uint32(centry);
806 do_cached:
807 status = centry->status;
809 DEBUG(10,("enum_dom_groups: [Cached] - cached list for domain %s status %s\n",
810 domain->name, get_friendly_nt_error_msg(status) ));
812 centry_free(centry);
813 return status;
815 do_query:
816 *num_entries = 0;
817 *info = NULL;
819 /* Return status value returned by seq number check */
821 if (!NT_STATUS_IS_OK(domain->last_status))
822 return domain->last_status;
824 DEBUG(10,("enum_dom_groups: [Cached] - doing backend query for list for domain %s\n",
825 domain->name ));
827 status = domain->backend->enum_dom_groups(domain, mem_ctx, num_entries, info);
829 /* and save it */
830 refresh_sequence_number(domain, False);
831 centry = centry_start(domain, status);
832 if (!centry)
833 goto skip_save;
834 centry_put_uint32(centry, *num_entries);
835 for (i=0; i<(*num_entries); i++) {
836 centry_put_string(centry, (*info)[i].acct_name);
837 centry_put_string(centry, (*info)[i].acct_desc);
838 centry_put_uint32(centry, (*info)[i].rid);
840 centry_end(centry, "GL/%s/domain", domain->name);
841 centry_free(centry);
843 skip_save:
844 return status;
847 /* list all domain groups */
848 static NTSTATUS enum_local_groups(struct winbindd_domain *domain,
849 TALLOC_CTX *mem_ctx,
850 uint32 *num_entries,
851 struct acct_info **info)
853 struct winbind_cache *cache = get_cache(domain);
854 struct cache_entry *centry = NULL;
855 NTSTATUS status;
856 unsigned int i;
858 if (!cache->tdb)
859 goto do_query;
861 centry = wcache_fetch(cache, domain, "GL/%s/local", domain->name);
862 if (!centry)
863 goto do_query;
865 *num_entries = centry_uint32(centry);
867 if (*num_entries == 0)
868 goto do_cached;
870 (*info) = TALLOC_ARRAY(mem_ctx, struct acct_info, *num_entries);
871 if (! (*info))
872 smb_panic("enum_dom_groups out of memory");
873 for (i=0; i<(*num_entries); i++) {
874 fstrcpy((*info)[i].acct_name, centry_string(centry, mem_ctx));
875 fstrcpy((*info)[i].acct_desc, centry_string(centry, mem_ctx));
876 (*info)[i].rid = centry_uint32(centry);
879 do_cached:
881 /* If we are returning cached data and the domain controller
882 is down then we don't know whether the data is up to date
883 or not. Return NT_STATUS_MORE_PROCESSING_REQUIRED to
884 indicate this. */
886 if (wcache_server_down(domain)) {
887 DEBUG(10, ("enum_local_groups: returning cached user list and server was down\n"));
888 status = NT_STATUS_MORE_PROCESSING_REQUIRED;
889 } else
890 status = centry->status;
892 DEBUG(10,("enum_local_groups: [Cached] - cached list for domain %s status %s\n",
893 domain->name, get_friendly_nt_error_msg(status) ));
895 centry_free(centry);
896 return status;
898 do_query:
899 *num_entries = 0;
900 *info = NULL;
902 /* Return status value returned by seq number check */
904 if (!NT_STATUS_IS_OK(domain->last_status))
905 return domain->last_status;
907 DEBUG(10,("enum_local_groups: [Cached] - doing backend query for list for domain %s\n",
908 domain->name ));
910 status = domain->backend->enum_local_groups(domain, mem_ctx, num_entries, info);
912 /* and save it */
913 refresh_sequence_number(domain, False);
914 centry = centry_start(domain, status);
915 if (!centry)
916 goto skip_save;
917 centry_put_uint32(centry, *num_entries);
918 for (i=0; i<(*num_entries); i++) {
919 centry_put_string(centry, (*info)[i].acct_name);
920 centry_put_string(centry, (*info)[i].acct_desc);
921 centry_put_uint32(centry, (*info)[i].rid);
923 centry_end(centry, "GL/%s/local", domain->name);
924 centry_free(centry);
926 skip_save:
927 return status;
930 /* convert a single name to a sid in a domain */
931 static NTSTATUS name_to_sid(struct winbindd_domain *domain,
932 TALLOC_CTX *mem_ctx,
933 const char *domain_name,
934 const char *name,
935 DOM_SID *sid,
936 enum SID_NAME_USE *type)
938 struct winbind_cache *cache = get_cache(domain);
939 struct cache_entry *centry = NULL;
940 NTSTATUS status;
941 fstring uname;
943 if (!cache->tdb)
944 goto do_query;
946 fstrcpy(uname, name);
947 strupper_m(uname);
948 centry = wcache_fetch(cache, domain, "NS/%s/%s", domain_name, uname);
949 if (!centry)
950 goto do_query;
951 *type = (enum SID_NAME_USE)centry_uint32(centry);
952 centry_sid(centry, sid);
953 status = centry->status;
955 DEBUG(10,("name_to_sid: [Cached] - cached name for domain %s status %s\n",
956 domain->name, get_friendly_nt_error_msg(status) ));
958 centry_free(centry);
959 return status;
961 do_query:
962 ZERO_STRUCTP(sid);
964 /* If the seq number check indicated that there is a problem
965 * with this DC, then return that status... except for
966 * access_denied. This is special because the dc may be in
967 * "restrict anonymous = 1" mode, in which case it will deny
968 * most unauthenticated operations, but *will* allow the LSA
969 * name-to-sid that we try as a fallback. */
971 if (!(NT_STATUS_IS_OK(domain->last_status)
972 || NT_STATUS_EQUAL(domain->last_status, NT_STATUS_ACCESS_DENIED)))
973 return domain->last_status;
975 DEBUG(10,("name_to_sid: [Cached] - doing backend query for name for domain %s\n",
976 domain->name ));
978 status = domain->backend->name_to_sid(domain, mem_ctx, domain_name, name, sid, type);
980 /* and save it */
981 wcache_save_name_to_sid(domain, status, domain_name, name, sid, *type);
983 /* We can't save the sid to name mapping as we don't know the
984 correct case of the name without looking it up */
986 return status;
989 /* convert a sid to a user or group name. The sid is guaranteed to be in the domain
990 given */
991 static NTSTATUS sid_to_name(struct winbindd_domain *domain,
992 TALLOC_CTX *mem_ctx,
993 const DOM_SID *sid,
994 char **domain_name,
995 char **name,
996 enum SID_NAME_USE *type)
998 struct winbind_cache *cache = get_cache(domain);
999 struct cache_entry *centry = NULL;
1000 NTSTATUS status;
1001 fstring sid_string;
1003 if (!cache->tdb)
1004 goto do_query;
1006 centry = wcache_fetch(cache, domain, "SN/%s", sid_to_string(sid_string, sid));
1007 if (!centry)
1008 goto do_query;
1009 if (NT_STATUS_IS_OK(centry->status)) {
1010 *type = (enum SID_NAME_USE)centry_uint32(centry);
1011 *domain_name = centry_string(centry, mem_ctx);
1012 *name = centry_string(centry, mem_ctx);
1014 status = centry->status;
1016 DEBUG(10,("sid_to_name: [Cached] - cached name for domain %s status %s\n",
1017 domain->name, get_friendly_nt_error_msg(status) ));
1019 centry_free(centry);
1020 return status;
1022 do_query:
1023 *name = NULL;
1024 *domain_name = NULL;
1026 /* If the seq number check indicated that there is a problem
1027 * with this DC, then return that status... except for
1028 * access_denied. This is special because the dc may be in
1029 * "restrict anonymous = 1" mode, in which case it will deny
1030 * most unauthenticated operations, but *will* allow the LSA
1031 * sid-to-name that we try as a fallback. */
1033 if (!(NT_STATUS_IS_OK(domain->last_status)
1034 || NT_STATUS_EQUAL(domain->last_status, NT_STATUS_ACCESS_DENIED)))
1035 return domain->last_status;
1037 DEBUG(10,("sid_to_name: [Cached] - doing backend query for name for domain %s\n",
1038 domain->name ));
1040 status = domain->backend->sid_to_name(domain, mem_ctx, sid, domain_name, name, type);
1042 /* and save it */
1043 refresh_sequence_number(domain, False);
1044 wcache_save_sid_to_name(domain, status, sid, *domain_name, *name, *type);
1046 /* We can't save the name to sid mapping here, as with sid history a
1047 * later name2sid would give the wrong sid. */
1049 return status;
1053 /* Lookup user information from a rid */
1054 static NTSTATUS query_user(struct winbindd_domain *domain,
1055 TALLOC_CTX *mem_ctx,
1056 const DOM_SID *user_sid,
1057 WINBIND_USERINFO *info)
1059 struct winbind_cache *cache = get_cache(domain);
1060 struct cache_entry *centry = NULL;
1061 NTSTATUS status;
1063 if (!cache->tdb)
1064 goto do_query;
1066 centry = wcache_fetch(cache, domain, "U/%s", sid_string_static(user_sid));
1068 /* If we have an access denied cache entry and a cached info3 in the
1069 samlogon cache then do a query. This will force the rpc back end
1070 to return the info3 data. */
1072 if (NT_STATUS_V(domain->last_status) == NT_STATUS_V(NT_STATUS_ACCESS_DENIED) &&
1073 netsamlogon_cache_have(user_sid)) {
1074 DEBUG(10, ("query_user: cached access denied and have cached info3\n"));
1075 domain->last_status = NT_STATUS_OK;
1076 centry_free(centry);
1077 goto do_query;
1080 if (!centry)
1081 goto do_query;
1083 info->acct_name = centry_string(centry, mem_ctx);
1084 info->full_name = centry_string(centry, mem_ctx);
1085 centry_sid(centry, &info->user_sid);
1086 centry_sid(centry, &info->group_sid);
1087 status = centry->status;
1089 DEBUG(10,("query_user: [Cached] - cached info for domain %s status %s\n",
1090 domain->name, get_friendly_nt_error_msg(status) ));
1092 centry_free(centry);
1093 return status;
1095 do_query:
1096 ZERO_STRUCTP(info);
1098 /* Return status value returned by seq number check */
1100 if (!NT_STATUS_IS_OK(domain->last_status))
1101 return domain->last_status;
1103 DEBUG(10,("sid_to_name: [Cached] - doing backend query for info for domain %s\n",
1104 domain->name ));
1106 status = domain->backend->query_user(domain, mem_ctx, user_sid, info);
1108 /* and save it */
1109 refresh_sequence_number(domain, False);
1110 wcache_save_user(domain, status, info);
1112 return status;
1116 /* Lookup groups a user is a member of. */
1117 static NTSTATUS lookup_usergroups(struct winbindd_domain *domain,
1118 TALLOC_CTX *mem_ctx,
1119 const DOM_SID *user_sid,
1120 uint32 *num_groups, DOM_SID **user_gids)
1122 struct winbind_cache *cache = get_cache(domain);
1123 struct cache_entry *centry = NULL;
1124 NTSTATUS status;
1125 unsigned int i;
1126 fstring sid_string;
1128 if (!cache->tdb)
1129 goto do_query;
1131 centry = wcache_fetch(cache, domain, "UG/%s", sid_to_string(sid_string, user_sid));
1133 /* If we have an access denied cache entry and a cached info3 in the
1134 samlogon cache then do a query. This will force the rpc back end
1135 to return the info3 data. */
1137 if (NT_STATUS_V(domain->last_status) == NT_STATUS_V(NT_STATUS_ACCESS_DENIED) &&
1138 netsamlogon_cache_have(user_sid)) {
1139 DEBUG(10, ("query_user: cached access denied and have cached info3\n"));
1140 domain->last_status = NT_STATUS_OK;
1141 centry_free(centry);
1142 goto do_query;
1145 if (!centry)
1146 goto do_query;
1148 *num_groups = centry_uint32(centry);
1150 if (*num_groups == 0)
1151 goto do_cached;
1153 (*user_gids) = TALLOC_ARRAY(mem_ctx, DOM_SID, *num_groups);
1154 if (! (*user_gids))
1155 smb_panic("lookup_usergroups out of memory");
1156 for (i=0; i<(*num_groups); i++) {
1157 centry_sid(centry, &(*user_gids)[i]);
1160 do_cached:
1161 status = centry->status;
1163 DEBUG(10,("lookup_usergroups: [Cached] - cached info for domain %s status %s\n",
1164 domain->name, get_friendly_nt_error_msg(status) ));
1166 centry_free(centry);
1167 return status;
1169 do_query:
1170 (*num_groups) = 0;
1171 (*user_gids) = NULL;
1173 /* Return status value returned by seq number check */
1175 if (!NT_STATUS_IS_OK(domain->last_status))
1176 return domain->last_status;
1178 DEBUG(10,("lookup_usergroups: [Cached] - doing backend query for info for domain %s\n",
1179 domain->name ));
1181 status = domain->backend->lookup_usergroups(domain, mem_ctx, user_sid, num_groups, user_gids);
1183 /* and save it */
1184 refresh_sequence_number(domain, False);
1185 centry = centry_start(domain, status);
1186 if (!centry)
1187 goto skip_save;
1188 centry_put_uint32(centry, *num_groups);
1189 for (i=0; i<(*num_groups); i++) {
1190 centry_put_sid(centry, &(*user_gids)[i]);
1192 centry_end(centry, "UG/%s", sid_to_string(sid_string, user_sid));
1193 centry_free(centry);
1195 skip_save:
1196 return status;
1199 static NTSTATUS lookup_useraliases(struct winbindd_domain *domain,
1200 TALLOC_CTX *mem_ctx,
1201 uint32 num_sids, const DOM_SID *sids,
1202 uint32 *num_aliases, uint32 **alias_rids)
1204 struct winbind_cache *cache = get_cache(domain);
1205 struct cache_entry *centry = NULL;
1206 NTSTATUS status;
1207 char *sidlist = talloc_strdup(mem_ctx, "");
1208 int i;
1210 if (!cache->tdb)
1211 goto do_query;
1213 if (num_sids == 0) {
1214 *num_aliases = 0;
1215 *alias_rids = NULL;
1216 return NT_STATUS_OK;
1219 /* We need to cache indexed by the whole list of SIDs, the aliases
1220 * resulting might come from any of the SIDs. */
1222 for (i=0; i<num_sids; i++) {
1223 sidlist = talloc_asprintf(mem_ctx, "%s/%s", sidlist,
1224 sid_string_static(&sids[i]));
1225 if (sidlist == NULL)
1226 return NT_STATUS_NO_MEMORY;
1229 centry = wcache_fetch(cache, domain, "UA%s", sidlist);
1231 if (!centry)
1232 goto do_query;
1234 *num_aliases = centry_uint32(centry);
1235 *alias_rids = NULL;
1237 (*alias_rids) = TALLOC_ARRAY(mem_ctx, uint32, *num_aliases);
1239 if ((*num_aliases != 0) && ((*alias_rids) == NULL))
1240 return NT_STATUS_NO_MEMORY;
1242 for (i=0; i<(*num_aliases); i++)
1243 (*alias_rids)[i] = centry_uint32(centry);
1245 status = centry->status;
1247 DEBUG(10,("lookup_useraliases: [Cached] - cached info for domain %s "
1248 "status %s\n", domain->name,
1249 get_friendly_nt_error_msg(status)));
1251 centry_free(centry);
1252 return status;
1254 do_query:
1255 (*num_aliases) = 0;
1256 (*alias_rids) = NULL;
1258 if (!NT_STATUS_IS_OK(domain->last_status))
1259 return domain->last_status;
1261 DEBUG(10,("lookup_usergroups: [Cached] - doing backend query for info "
1262 "for domain %s\n", domain->name ));
1264 status = domain->backend->lookup_useraliases(domain, mem_ctx,
1265 num_sids, sids,
1266 num_aliases, alias_rids);
1268 /* and save it */
1269 refresh_sequence_number(domain, False);
1270 centry = centry_start(domain, status);
1271 if (!centry)
1272 goto skip_save;
1273 centry_put_uint32(centry, *num_aliases);
1274 for (i=0; i<(*num_aliases); i++)
1275 centry_put_uint32(centry, (*alias_rids)[i]);
1276 centry_end(centry, "UA%s", sidlist);
1277 centry_free(centry);
1279 skip_save:
1280 return status;
1284 static NTSTATUS lookup_groupmem(struct winbindd_domain *domain,
1285 TALLOC_CTX *mem_ctx,
1286 const DOM_SID *group_sid, uint32 *num_names,
1287 DOM_SID **sid_mem, char ***names,
1288 uint32 **name_types)
1290 struct winbind_cache *cache = get_cache(domain);
1291 struct cache_entry *centry = NULL;
1292 NTSTATUS status;
1293 unsigned int i;
1294 fstring sid_string;
1296 if (!cache->tdb)
1297 goto do_query;
1299 centry = wcache_fetch(cache, domain, "GM/%s", sid_to_string(sid_string, group_sid));
1300 if (!centry)
1301 goto do_query;
1303 *num_names = centry_uint32(centry);
1305 if (*num_names == 0)
1306 goto do_cached;
1308 (*sid_mem) = TALLOC_ARRAY(mem_ctx, DOM_SID, *num_names);
1309 (*names) = TALLOC_ARRAY(mem_ctx, char *, *num_names);
1310 (*name_types) = TALLOC_ARRAY(mem_ctx, uint32, *num_names);
1312 if (! (*sid_mem) || ! (*names) || ! (*name_types)) {
1313 smb_panic("lookup_groupmem out of memory");
1316 for (i=0; i<(*num_names); i++) {
1317 centry_sid(centry, &(*sid_mem)[i]);
1318 (*names)[i] = centry_string(centry, mem_ctx);
1319 (*name_types)[i] = centry_uint32(centry);
1322 do_cached:
1323 status = centry->status;
1325 DEBUG(10,("lookup_groupmem: [Cached] - cached info for domain %s status %s\n",
1326 domain->name, get_friendly_nt_error_msg(status) ));
1328 centry_free(centry);
1329 return status;
1331 do_query:
1332 (*num_names) = 0;
1333 (*sid_mem) = NULL;
1334 (*names) = NULL;
1335 (*name_types) = NULL;
1337 /* Return status value returned by seq number check */
1339 if (!NT_STATUS_IS_OK(domain->last_status))
1340 return domain->last_status;
1342 DEBUG(10,("lookup_groupmem: [Cached] - doing backend query for info for domain %s\n",
1343 domain->name ));
1345 status = domain->backend->lookup_groupmem(domain, mem_ctx, group_sid, num_names,
1346 sid_mem, names, name_types);
1348 /* and save it */
1349 refresh_sequence_number(domain, False);
1350 centry = centry_start(domain, status);
1351 if (!centry)
1352 goto skip_save;
1353 centry_put_uint32(centry, *num_names);
1354 for (i=0; i<(*num_names); i++) {
1355 centry_put_sid(centry, &(*sid_mem)[i]);
1356 centry_put_string(centry, (*names)[i]);
1357 centry_put_uint32(centry, (*name_types)[i]);
1359 centry_end(centry, "GM/%s", sid_to_string(sid_string, group_sid));
1360 centry_free(centry);
1362 skip_save:
1363 return status;
1366 /* find the sequence number for a domain */
1367 static NTSTATUS sequence_number(struct winbindd_domain *domain, uint32 *seq)
1369 refresh_sequence_number(domain, False);
1371 *seq = domain->sequence_number;
1373 return NT_STATUS_OK;
1376 /* enumerate trusted domains */
1377 static NTSTATUS trusted_domains(struct winbindd_domain *domain,
1378 TALLOC_CTX *mem_ctx,
1379 uint32 *num_domains,
1380 char ***names,
1381 char ***alt_names,
1382 DOM_SID **dom_sids)
1384 get_cache(domain);
1386 DEBUG(10,("trusted_domains: [Cached] - doing backend query for info for domain %s\n",
1387 domain->name ));
1389 /* we don't cache this call */
1390 return domain->backend->trusted_domains(domain, mem_ctx, num_domains,
1391 names, alt_names, dom_sids);
1394 /* find the alternate names for the domain, if any */
1395 static NTSTATUS alternate_name(struct winbindd_domain *domain)
1397 get_cache(domain);
1399 DEBUG(10,("alternate_name: [Cached] - doing backend query for info for domain %s\n",
1400 domain->name ));
1402 /* we don't cache this call */
1403 return domain->backend->alternate_name(domain);
1406 /* Invalidate cached user and group lists coherently */
1408 static int traverse_fn(TDB_CONTEXT *the_tdb, TDB_DATA kbuf, TDB_DATA dbuf,
1409 void *state)
1411 if (strncmp(kbuf.dptr, "UL/", 3) == 0 ||
1412 strncmp(kbuf.dptr, "GL/", 3) == 0)
1413 tdb_delete(the_tdb, kbuf);
1415 return 0;
1418 /* Invalidate the getpwnam and getgroups entries for a winbindd domain */
1420 void wcache_invalidate_samlogon(struct winbindd_domain *domain,
1421 NET_USER_INFO_3 *info3)
1423 struct winbind_cache *cache;
1425 if (!domain)
1426 return;
1428 cache = get_cache(domain);
1429 netsamlogon_clear_cached_user(cache->tdb, info3);
1432 void wcache_invalidate_cache(void)
1434 struct winbindd_domain *domain;
1436 for (domain = domain_list(); domain; domain = domain->next) {
1437 struct winbind_cache *cache = get_cache(domain);
1439 DEBUG(10, ("wcache_invalidate_cache: invalidating cache "
1440 "entries for %s\n", domain->name));
1441 if (cache)
1442 tdb_traverse(cache->tdb, traverse_fn, NULL);
1446 /* the ADS backend methods are exposed via this structure */
1447 struct winbindd_methods cache_methods = {
1448 True,
1449 query_user_list,
1450 enum_dom_groups,
1451 enum_local_groups,
1452 name_to_sid,
1453 sid_to_name,
1454 query_user,
1455 lookup_usergroups,
1456 lookup_useraliases,
1457 lookup_groupmem,
1458 sequence_number,
1459 trusted_domains,
1460 alternate_name
1463 static BOOL init_wcache(void)
1465 if (wcache == NULL) {
1466 wcache = SMB_XMALLOC_P(struct winbind_cache);
1467 ZERO_STRUCTP(wcache);
1470 if (wcache->tdb != NULL)
1471 return True;
1473 wcache->tdb = tdb_open_log(lock_path("winbindd_cache.tdb"), 5000,
1474 TDB_CLEAR_IF_FIRST, O_RDWR|O_CREAT, 0600);
1476 if (wcache->tdb == NULL) {
1477 DEBUG(0,("Failed to open winbindd_cache.tdb!\n"));
1478 return False;
1481 return True;
1484 void cache_store_response(pid_t pid, struct winbindd_response *response)
1486 fstring key_str;
1488 if (!init_wcache())
1489 return;
1491 DEBUG(10, ("Storing response for pid %d, len %d\n",
1492 pid, response->length));
1494 fstr_sprintf(key_str, "DR/%d", pid);
1495 if (tdb_store(wcache->tdb, string_tdb_data(key_str),
1496 make_tdb_data((void *)response, sizeof(*response)),
1497 TDB_REPLACE) == -1)
1498 return;
1500 if (response->length == sizeof(*response))
1501 return;
1503 /* There's extra data */
1505 DEBUG(10, ("Storing extra data: len=%d\n",
1506 response->length - sizeof(*response)));
1508 fstr_sprintf(key_str, "DE/%d", pid);
1509 if (tdb_store(wcache->tdb, string_tdb_data(key_str),
1510 make_tdb_data(response->extra_data,
1511 response->length - sizeof(*response)),
1512 TDB_REPLACE) == 0)
1513 return;
1515 /* We could not store the extra data, make sure the tdb does not
1516 * contain a main record with wrong dangling extra data */
1518 fstr_sprintf(key_str, "DR/%d", pid);
1519 tdb_delete(wcache->tdb, string_tdb_data(key_str));
1521 return;
1524 BOOL cache_retrieve_response(pid_t pid, struct winbindd_response * response)
1526 TDB_DATA data;
1527 fstring key_str;
1529 if (!init_wcache())
1530 return False;
1532 DEBUG(10, ("Retrieving response for pid %d\n", pid));
1534 fstr_sprintf(key_str, "DR/%d", pid);
1535 data = tdb_fetch(wcache->tdb, string_tdb_data(key_str));
1537 if (data.dptr == NULL)
1538 return False;
1540 if (data.dsize != sizeof(*response))
1541 return False;
1543 memcpy(response, data.dptr, data.dsize);
1544 SAFE_FREE(data.dptr);
1546 if (response->length == sizeof(*response)) {
1547 response->extra_data = NULL;
1548 return True;
1551 /* There's extra data */
1553 DEBUG(10, ("Retrieving extra data length=%d\n",
1554 response->length - sizeof(*response)));
1556 fstr_sprintf(key_str, "DE/%d", pid);
1557 data = tdb_fetch(wcache->tdb, string_tdb_data(key_str));
1559 if (data.dptr == NULL) {
1560 DEBUG(0, ("Did not find extra data\n"));
1561 return False;
1564 if (data.dsize != (response->length - sizeof(*response))) {
1565 DEBUG(0, ("Invalid extra data length: %d\n", data.dsize));
1566 SAFE_FREE(data.dptr);
1567 return False;
1570 response->extra_data = data.dptr;
1571 return True;
1574 char *cache_store_request_data(TALLOC_CTX *mem_ctx, char *request_string)
1576 int i;
1578 if (!init_wcache())
1579 return NULL;
1581 for (i=0; i<2; i++) {
1582 char *key = talloc_strdup(mem_ctx, generate_random_str(16));
1583 if (key == NULL)
1584 return NULL;
1585 DEBUG(10, ("Storing request key %s\n", key));
1586 if (tdb_store_bystring(wcache->tdb, key,
1587 string_tdb_data(request_string),
1588 TDB_INSERT) == 0)
1589 return key;
1591 return NULL;
1594 char *cache_retrieve_request_data(TALLOC_CTX *mem_ctx, char *key)
1596 TDB_DATA data;
1597 char *result = NULL;
1599 if (!init_wcache())
1600 return NULL;
1602 DEBUG(10, ("Retrieving key %s\n", key));
1604 data = tdb_fetch_bystring(wcache->tdb, key);
1605 if (data.dptr == NULL)
1606 return NULL;
1608 if (strnlen(data.dptr, data.dsize) != (data.dsize)) {
1609 DEBUG(0, ("Received invalid request string\n"));
1610 goto done;
1612 result = TALLOC_ARRAY(mem_ctx, char, data.dsize+1);
1613 if (result != NULL) {
1614 memcpy(result, data.dptr, data.dsize);
1615 result[data.dsize] = '\0';
1617 if (tdb_delete_bystring(wcache->tdb, key) != 0) {
1618 DEBUG(0, ("Could not delete key %s\n", key));
1619 result = NULL;
1621 done:
1622 SAFE_FREE(data.dptr);
1623 return result;