winbindd backends can now be marked "consistent" or "inconsistent"
[abartlet/samba.git/.git] / source3 / nsswitch / winbindd_cache.c
1 /* 
2    Unix SMB/Netbios implementation.
3
4    Winbind cache backend functions
5
6    Copyright (C) Andrew Tridgell 2001
7    
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.
12    
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.
17    
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.
21 */
22
23 #include "winbindd.h"
24
25 struct winbind_cache {
26         struct winbindd_methods *backend;
27         TDB_CONTEXT *tdb;
28 };
29
30 struct cache_entry {
31         NTSTATUS status;
32         uint32 sequence_number;
33         uint8 *data;
34         uint32 len, ofs;
35 };
36
37 static struct winbind_cache *wcache;
38
39 /* flush the cache */
40 void wcache_flush_cache(void)
41 {
42         extern BOOL opt_nocache;
43
44         if (!wcache) return;
45         if (wcache->tdb) {
46                 tdb_close(wcache->tdb);
47                 wcache->tdb = NULL;
48         }
49         if (opt_nocache) return;
50
51         wcache->tdb = tdb_open_log(lock_path("winbindd_cache.tdb"), 5000, 
52                                    TDB_NOLOCK, O_RDWR | O_CREAT | O_TRUNC, 0600);
53
54         if (!wcache->tdb) {
55                 DEBUG(0,("Failed to open winbindd_cache.tdb!\n"));
56         }
57 }
58
59 /* get the winbind_cache structure */
60 static struct winbind_cache *get_cache(struct winbindd_domain *domain)
61 {
62         extern struct winbindd_methods msrpc_methods;
63         struct winbind_cache *ret = wcache;
64
65         if (ret) return ret;
66         
67         ret = smb_xmalloc(sizeof(*ret));
68         ZERO_STRUCTP(ret);
69         switch (lp_security()) {
70 #ifdef HAVE_ADS
71         case SEC_ADS: {
72                 extern struct winbindd_methods ads_methods;
73                 ret->backend = &ads_methods;
74                 break;
75         }
76 #endif
77         default:
78                 ret->backend = &msrpc_methods;
79         }
80
81         wcache = ret;
82         wcache_flush_cache();
83
84         return ret;
85 }
86
87 /*
88   free a centry structure
89 */
90 static void centry_free(struct cache_entry *centry)
91 {
92         if (!centry) return;
93         SAFE_FREE(centry->data);
94         free(centry);
95 }
96
97
98 /*
99   pull a uint32 from a cache entry 
100 */
101 static uint32 centry_uint32(struct cache_entry *centry)
102 {
103         uint32 ret;
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");
108         }
109         ret = IVAL(centry->data, centry->ofs);
110         centry->ofs += 4;
111         return ret;
112 }
113
114 /*
115   pull a uint8 from a cache entry 
116 */
117 static uint8 centry_uint8(struct cache_entry *centry)
118 {
119         uint8 ret;
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");
124         }
125         ret = CVAL(centry->data, centry->ofs);
126         centry->ofs += 1;
127         return ret;
128 }
129
130 /* pull a string from a cache entry, using the supplied
131    talloc context 
132 */
133 static char *centry_string(struct cache_entry *centry, TALLOC_CTX *mem_ctx)
134 {
135         uint32 len;
136         char *ret;
137
138         len = centry_uint8(centry);
139
140         if (len == 0xFF) {
141                 /* a deliberate NULL string */
142                 return NULL;
143         }
144
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");
149         }
150
151         ret = talloc(mem_ctx, len+1);
152         if (!ret) {
153                 smb_panic("centry_string out of memory\n");
154         }
155         memcpy(ret,centry->data + centry->ofs, len);
156         ret[len] = 0;
157         centry->ofs += len;
158         return ret;
159 }
160
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)
163 {
164         if (!wcache->tdb) return False;
165         return (domain->sequence_number == DOM_SEQUENCE_NONE);
166 }
167
168
169 /*
170   refresh the domain sequence number. If force is True
171   then always refresh it, no matter how recently we fetched it
172 */
173 static void refresh_sequence_number(struct winbindd_domain *domain, BOOL force)
174 {
175         NTSTATUS status;
176
177         /* see if we have to refetch the domain sequence number */
178         if (!force && (time(NULL) - domain->last_seq_check < lp_winbind_cache_time())) {
179                 return;
180         }
181
182         status = wcache->backend->sequence_number(domain, &domain->sequence_number);
183
184         if (!NT_STATUS_IS_OK(status)) {
185                 domain->sequence_number = DOM_SEQUENCE_NONE;
186         }
187
188         domain->last_seq_check = time(NULL);
189 }
190
191 /*
192   decide if a cache entry has expired
193 */
194 static BOOL centry_expired(struct winbindd_domain *domain, struct cache_entry *centry)
195 {
196         /* if the server is OK and our cache entry came from when it was down then
197            the entry is invalid */
198         if (domain->sequence_number != DOM_SEQUENCE_NONE && 
199             centry->sequence_number == DOM_SEQUENCE_NONE) {
200                 return True;
201         }
202
203         /* if the server is down or the cache entry is not older than the
204            current sequence number then it is OK */
205         if (wcache_server_down(domain) || 
206             centry->sequence_number >= domain->sequence_number) {
207                 return False;
208         }
209
210         /* it's expired */
211         return True;
212 }
213
214 /*
215   fetch an entry from the cache, with a varargs key. auto-fetch the sequence
216   number and return status
217 */
218 static struct cache_entry *wcache_fetch(struct winbind_cache *cache, 
219                                         struct winbindd_domain *domain,
220                                         const char *format, ...)
221 {
222         va_list ap;
223         char *kstr;
224         TDB_DATA data;
225         struct cache_entry *centry;
226         TDB_DATA key;
227
228         refresh_sequence_number(domain, False);
229
230         va_start(ap, format);
231         smb_xvasprintf(&kstr, format, ap);
232         va_end(ap);
233         
234         key.dptr = kstr;
235         key.dsize = strlen(kstr);
236         data = tdb_fetch(wcache->tdb, key);
237         free(kstr);
238         if (!data.dptr) {
239                 /* a cache miss */
240                 return NULL;
241         }
242
243         centry = smb_xmalloc(sizeof(*centry));
244         centry->data = data.dptr;
245         centry->len = data.dsize;
246         centry->ofs = 0;
247
248         if (centry->len < 8) {
249                 /* huh? corrupt cache? */
250                 centry_free(centry);
251                 return NULL;
252         }
253         
254         centry->status = NT_STATUS(centry_uint32(centry));
255         centry->sequence_number = centry_uint32(centry);
256
257         if (centry_expired(domain, centry)) {
258                 centry_free(centry);
259                 return NULL;
260         }
261
262         return centry;
263 }
264
265 /*
266   make sure we have at least len bytes available in a centry 
267 */
268 static void centry_expand(struct cache_entry *centry, uint32 len)
269 {
270         uint8 *p;
271         if (centry->len - centry->ofs >= len) return;
272         centry->len *= 2;
273         p = realloc(centry->data, centry->len);
274         if (!p) {
275                 DEBUG(0,("out of memory: needed %d bytes in centry_expand\n", centry->len));
276                 smb_panic("out of memory in centry_expand");
277         }
278         centry->data = p;
279 }
280
281 /*
282   push a uint32 into a centry 
283 */
284 static void centry_put_uint32(struct cache_entry *centry, uint32 v)
285 {
286         centry_expand(centry, 4);
287         SIVAL(centry->data, centry->ofs, v);
288         centry->ofs += 4;
289 }
290
291 /*
292   push a uint8 into a centry 
293 */
294 static void centry_put_uint8(struct cache_entry *centry, uint8 v)
295 {
296         centry_expand(centry, 1);
297         SCVAL(centry->data, centry->ofs, v);
298         centry->ofs += 1;
299 }
300
301 /* 
302    push a string into a centry 
303  */
304 static void centry_put_string(struct cache_entry *centry, const char *s)
305 {
306         int len;
307
308         if (!s) {
309                 /* null strings are marked as len 0xFFFF */
310                 centry_put_uint8(centry, 0xFF);
311                 return;
312         }
313
314         len = strlen(s);
315         /* can't handle more than 254 char strings. Truncating is probably best */
316         if (len > 254) len = 254;
317         centry_put_uint8(centry, len);
318         centry_expand(centry, len);
319         memcpy(centry->data + centry->ofs, s, len);
320         centry->ofs += len;
321 }
322
323 /*
324   start a centry for output. When finished, call centry_end()
325 */
326 struct cache_entry *centry_start(struct winbindd_domain *domain, NTSTATUS status)
327 {
328         struct cache_entry *centry;
329
330         if (!wcache->tdb) return NULL;
331
332         centry = smb_xmalloc(sizeof(*centry));
333
334         centry->len = 8192; /* reasonable default */
335         centry->data = smb_xmalloc(centry->len);
336         centry->ofs = 0;
337         centry->sequence_number = domain->sequence_number;
338         centry_put_uint32(centry, NT_STATUS_V(status));
339         centry_put_uint32(centry, centry->sequence_number);
340         return centry;
341 }
342
343 /*
344   finish a centry and write it to the tdb
345 */
346 static void centry_end(struct cache_entry *centry, const char *format, ...)
347 {
348         va_list ap;
349         char *kstr;
350         TDB_DATA key, data;
351
352         va_start(ap, format);
353         smb_xvasprintf(&kstr, format, ap);
354         va_end(ap);
355
356         key.dptr = kstr;
357         key.dsize = strlen(kstr);
358         data.dptr = centry->data;
359         data.dsize = centry->ofs;
360
361         tdb_store(wcache->tdb, key, data, TDB_REPLACE);
362         free(kstr);
363 }
364
365 /* form a name with the domain part stuck on the front */
366 static char *prepend_domain(struct winbindd_domain *domain, const char *name)
367 {
368         static fstring s;
369         snprintf(s, sizeof(s), "%s%s%s", domain->name, lp_winbind_separator(), name);
370         return s;
371 }
372
373 /* form a sid from the domain plus rid */
374 static DOM_SID *form_sid(struct winbindd_domain *domain, uint32 rid)
375 {
376         static DOM_SID sid;
377         sid_copy(&sid, &domain->sid);
378         sid_append_rid(&sid, rid);
379         return &sid;
380 }
381
382 static void wcache_save_name_to_sid(struct winbindd_domain *domain, NTSTATUS status, 
383                                     const char *name, DOM_SID *sid, enum SID_NAME_USE type)
384 {
385         struct cache_entry *centry;
386         uint32 len;
387
388         centry = centry_start(domain, status);
389         if (!centry) return;
390         len = sid_size(sid);
391         centry_expand(centry, len);
392         centry_put_uint32(centry, type);
393         sid_linearize(centry->data + centry->ofs, len, sid);
394         centry->ofs += len;
395         centry_end(centry, "NS/%s/%s", domain->name, name);
396         centry_free(centry);
397 }
398
399 static void wcache_save_sid_to_name(struct winbindd_domain *domain, NTSTATUS status, 
400                                     DOM_SID *sid, const char *name, enum SID_NAME_USE type, uint32 rid)
401 {
402         struct cache_entry *centry;
403
404         centry = centry_start(domain, status);
405         if (!centry) return;
406         if (NT_STATUS_IS_OK(status)) {
407                 centry_put_uint32(centry, type);
408                 centry_put_string(centry, name);
409         }
410         centry_end(centry, "SN/%s/%d", domain->name, rid);
411         centry_free(centry);
412 }
413
414
415 static void wcache_save_user(struct winbindd_domain *domain, NTSTATUS status, WINBIND_USERINFO *info)
416 {
417         struct cache_entry *centry;
418
419         centry = centry_start(domain, status);
420         if (!centry) return;
421         centry_put_string(centry, info->acct_name);
422         centry_put_string(centry, info->full_name);
423         centry_put_uint32(centry, info->user_rid);
424         centry_put_uint32(centry, info->group_rid);
425         centry_end(centry, "U/%s/%x", domain->name, info->user_rid);
426         centry_free(centry);
427 }
428
429
430 /* Query display info. This is the basic user list fn */
431 static NTSTATUS query_user_list(struct winbindd_domain *domain,
432                                 TALLOC_CTX *mem_ctx,
433                                 uint32 *start_ndx, uint32 *num_entries, 
434                                 WINBIND_USERINFO **info)
435 {
436         struct winbind_cache *cache = get_cache(domain);
437         struct cache_entry *centry = NULL;
438         NTSTATUS status;
439         int i;
440
441         if (!cache->tdb) goto do_query;
442
443         centry = wcache_fetch(cache, domain, "UL/%s/%d", domain->name, *start_ndx);
444         if (!centry) goto do_query;
445
446         *num_entries = centry_uint32(centry);
447         
448         if (*num_entries == 0) goto do_cached;
449
450         (*info) = talloc(mem_ctx, sizeof(**info) * (*num_entries));
451         if (! (*info)) smb_panic("query_user_list out of memory");
452         for (i=0; i<(*num_entries); i++) {
453                 (*info)[i].acct_name = centry_string(centry, mem_ctx);
454                 (*info)[i].full_name = centry_string(centry, mem_ctx);
455                 (*info)[i].user_rid = centry_uint32(centry);
456                 (*info)[i].group_rid = centry_uint32(centry);
457         }
458
459 do_cached:      
460         status = centry->status;
461         centry_free(centry);
462         return status;
463
464 do_query:
465         if (wcache_server_down(domain)) {
466                 *num_entries = 0;
467                 return NT_STATUS_SERVER_DISABLED;
468         }
469
470         status = cache->backend->query_user_list(domain, mem_ctx, start_ndx, num_entries, info);
471
472         /* and save it */
473         refresh_sequence_number(domain, True);
474         centry = centry_start(domain, status);
475         if (!centry) goto skip_save;
476         centry_put_uint32(centry, *num_entries);
477         for (i=0; i<(*num_entries); i++) {
478                 centry_put_string(centry, (*info)[i].acct_name);
479                 centry_put_string(centry, (*info)[i].full_name);
480                 centry_put_uint32(centry, (*info)[i].user_rid);
481                 centry_put_uint32(centry, (*info)[i].group_rid);
482                 if (cache->backend->consistent) {
483                         /* when the backend is consistent we can pre-prime some mappings */
484                         wcache_save_name_to_sid(domain, NT_STATUS_OK, 
485                                                 prepend_domain(domain, (*info)[i].acct_name), 
486                                                 form_sid(domain, (*info)[i].user_rid),
487                                                 SID_NAME_USER);
488                         wcache_save_sid_to_name(domain, NT_STATUS_OK, 
489                                                 form_sid(domain, (*info)[i].user_rid),
490                                                 prepend_domain(domain, (*info)[i].acct_name), 
491                                                 SID_NAME_USER, (*info)[i].user_rid);
492                         wcache_save_user(domain, NT_STATUS_OK, &(*info)[i]);
493                 }
494         }       
495         centry_end(centry, "UL/%s/%d", domain->name, *start_ndx);
496         centry_free(centry);
497
498 skip_save:
499         return status;
500 }
501
502 /* list all domain groups */
503 static NTSTATUS enum_dom_groups(struct winbindd_domain *domain,
504                                 TALLOC_CTX *mem_ctx,
505                                 uint32 *start_ndx, uint32 *num_entries, 
506                                 struct acct_info **info)
507 {
508         struct winbind_cache *cache = get_cache(domain);
509         struct cache_entry *centry = NULL;
510         NTSTATUS status;
511         int i;
512
513         if (!cache->tdb) goto do_query;
514
515         centry = wcache_fetch(cache, domain, "GL/%s/%d", domain->name, *start_ndx);
516         if (!centry) goto do_query;
517
518         *num_entries = centry_uint32(centry);
519         
520         if (*num_entries == 0) goto do_cached;
521
522         (*info) = talloc(mem_ctx, sizeof(**info) * (*num_entries));
523         if (! (*info)) smb_panic("enum_dom_groups out of memory");
524         for (i=0; i<(*num_entries); i++) {
525                 fstrcpy((*info)[i].acct_name, centry_string(centry, mem_ctx));
526                 fstrcpy((*info)[i].acct_desc, centry_string(centry, mem_ctx));
527                 (*info)[i].rid = centry_uint32(centry);
528         }
529
530 do_cached:      
531         status = centry->status;
532         centry_free(centry);
533         return status;
534
535 do_query:
536         if (wcache_server_down(domain)) {
537                 *num_entries = 0;
538                 return NT_STATUS_SERVER_DISABLED;
539         }
540
541         status = cache->backend->enum_dom_groups(domain, mem_ctx, start_ndx, num_entries, info);
542
543         /* and save it */
544         refresh_sequence_number(domain, True);
545         centry = centry_start(domain, status);
546         if (!centry) goto skip_save;
547         centry_put_uint32(centry, *num_entries);
548         for (i=0; i<(*num_entries); i++) {
549                 centry_put_string(centry, (*info)[i].acct_name);
550                 centry_put_string(centry, (*info)[i].acct_desc);
551                 centry_put_uint32(centry, (*info)[i].rid);
552         }       
553         centry_end(centry, "GL/%s/%d", domain->name, *start_ndx);
554         centry_free(centry);
555
556 skip_save:
557         return status;
558 }
559
560
561 /* convert a single name to a sid in a domain */
562 static NTSTATUS name_to_sid(struct winbindd_domain *domain,
563                             const char *name,
564                             DOM_SID *sid,
565                             enum SID_NAME_USE *type)
566 {
567         struct winbind_cache *cache = get_cache(domain);
568         struct cache_entry *centry = NULL;
569         NTSTATUS status;
570
571         if (!cache->tdb) goto do_query;
572
573         centry = wcache_fetch(cache, domain, "NS/%s/%s", domain->name, name);
574         if (!centry) goto do_query;
575         *type = centry_uint32(centry);
576         sid_parse(centry->data + centry->ofs, centry->len - centry->ofs, sid);
577
578         status = centry->status;
579         centry_free(centry);
580         return status;
581
582 do_query:
583         if (wcache_server_down(domain)) {
584                 return NT_STATUS_SERVER_DISABLED;
585         }
586         status = cache->backend->name_to_sid(domain, name, sid, type);
587
588         /* and save it */
589         wcache_save_name_to_sid(domain, status, name, sid, *type);
590
591         return status;
592 }
593
594 /* convert a sid to a user or group name. The sid is guaranteed to be in the domain
595    given */
596 static NTSTATUS sid_to_name(struct winbindd_domain *domain,
597                             TALLOC_CTX *mem_ctx,
598                             DOM_SID *sid,
599                             char **name,
600                             enum SID_NAME_USE *type)
601 {
602         struct winbind_cache *cache = get_cache(domain);
603         struct cache_entry *centry = NULL;
604         NTSTATUS status;
605         uint32 rid = 0;
606
607         sid_peek_rid(sid, &rid);
608
609         if (!cache->tdb) goto do_query;
610
611         centry = wcache_fetch(cache, domain, "SN/%s/%d", domain->name, rid);
612         if (!centry) goto do_query;
613         if (NT_STATUS_IS_OK(centry->status)) {
614                 *type = centry_uint32(centry);
615                 *name = centry_string(centry, mem_ctx);
616         }
617         status = centry->status;
618         centry_free(centry);
619         return status;
620
621 do_query:
622         if (wcache_server_down(domain)) {
623                 return NT_STATUS_SERVER_DISABLED;
624         }
625         status = cache->backend->sid_to_name(domain, mem_ctx, sid, name, type);
626
627         /* and save it */
628         refresh_sequence_number(domain, True);
629         wcache_save_sid_to_name(domain, status, sid, *name, *type, rid);
630
631         return status;
632 }
633
634
635 /* Lookup user information from a rid */
636 static NTSTATUS query_user(struct winbindd_domain *domain, 
637                            TALLOC_CTX *mem_ctx, 
638                            uint32 user_rid, 
639                            WINBIND_USERINFO *info)
640 {
641         struct winbind_cache *cache = get_cache(domain);
642         struct cache_entry *centry = NULL;
643         NTSTATUS status;
644
645         if (!cache->tdb) goto do_query;
646
647         centry = wcache_fetch(cache, domain, "U/%s/%x", domain->name, user_rid);
648         if (!centry) goto do_query;
649
650         info->acct_name = centry_string(centry, mem_ctx);
651         info->full_name = centry_string(centry, mem_ctx);
652         info->user_rid = centry_uint32(centry);
653         info->group_rid = centry_uint32(centry);
654         status = centry->status;
655         centry_free(centry);
656         return status;
657
658 do_query:
659         if (wcache_server_down(domain)) {
660                 return NT_STATUS_SERVER_DISABLED;
661         }
662         status = cache->backend->query_user(domain, mem_ctx, user_rid, info);
663
664         /* and save it */
665         refresh_sequence_number(domain, True);
666         wcache_save_user(domain, status, info);
667
668         return status;
669 }
670
671
672 /* Lookup groups a user is a member of. */
673 static NTSTATUS lookup_usergroups(struct winbindd_domain *domain,
674                                   TALLOC_CTX *mem_ctx,
675                                   uint32 user_rid, 
676                                   uint32 *num_groups, uint32 **user_gids)
677 {
678         struct winbind_cache *cache = get_cache(domain);
679         struct cache_entry *centry = NULL;
680         NTSTATUS status;
681         int i;
682
683         if (!cache->tdb) goto do_query;
684
685         centry = wcache_fetch(cache, domain, "UG/%s/%x", domain->name, user_rid);
686         if (!centry) goto do_query;
687
688         *num_groups = centry_uint32(centry);
689         
690         if (*num_groups == 0) goto do_cached;
691
692         (*user_gids) = talloc(mem_ctx, sizeof(**user_gids) * (*num_groups));
693         if (! (*user_gids)) smb_panic("lookup_usergroups out of memory");
694         for (i=0; i<(*num_groups); i++) {
695                 (*user_gids)[i] = centry_uint32(centry);
696         }
697
698 do_cached:      
699         status = centry->status;
700         centry_free(centry);
701         return status;
702
703 do_query:
704         if (wcache_server_down(domain)) {
705                 (*num_groups) = 0;
706                 return NT_STATUS_SERVER_DISABLED;
707         }
708         status = cache->backend->lookup_usergroups(domain, mem_ctx, user_rid, num_groups, user_gids);
709
710         /* and save it */
711         refresh_sequence_number(domain, True);
712         centry = centry_start(domain, status);
713         if (!centry) goto skip_save;
714         centry_put_uint32(centry, *num_groups);
715         for (i=0; i<(*num_groups); i++) {
716                 centry_put_uint32(centry, (*user_gids)[i]);
717         }       
718         centry_end(centry, "UG/%s/%x", domain->name, user_rid);
719         centry_free(centry);
720
721 skip_save:
722         return status;
723 }
724
725
726 static NTSTATUS lookup_groupmem(struct winbindd_domain *domain,
727                                 TALLOC_CTX *mem_ctx,
728                                 uint32 group_rid, uint32 *num_names, 
729                                 uint32 **rid_mem, char ***names, 
730                                 uint32 **name_types)
731 {
732         struct winbind_cache *cache = get_cache(domain);
733         struct cache_entry *centry = NULL;
734         NTSTATUS status;
735         int i;
736
737         if (!cache->tdb) goto do_query;
738
739         centry = wcache_fetch(cache, domain, "GM/%s/%x", domain->name, group_rid);
740         if (!centry) goto do_query;
741
742         *num_names = centry_uint32(centry);
743         
744         if (*num_names == 0) goto do_cached;
745
746         (*rid_mem) = talloc(mem_ctx, sizeof(**rid_mem) * (*num_names));
747         (*names) = talloc(mem_ctx, sizeof(**names) * (*num_names));
748         (*name_types) = talloc(mem_ctx, sizeof(**name_types) * (*num_names));
749
750         if (! (*rid_mem) || ! (*names) || ! (*name_types)) {
751                 smb_panic("lookup_groupmem out of memory");
752         }
753
754         for (i=0; i<(*num_names); i++) {
755                 (*rid_mem)[i] = centry_uint32(centry);
756                 (*names)[i] = centry_string(centry, mem_ctx);
757                 (*name_types)[i] = centry_uint32(centry);
758         }
759
760 do_cached:      
761         status = centry->status;
762         centry_free(centry);
763         return status;
764
765 do_query:
766         if (wcache_server_down(domain)) {
767                 (*num_names) = 0;
768                 return NT_STATUS_SERVER_DISABLED;
769         }
770         status = cache->backend->lookup_groupmem(domain, mem_ctx, group_rid, num_names, 
771                                                  rid_mem, names, name_types);
772
773         /* and save it */
774         refresh_sequence_number(domain, True);
775         centry = centry_start(domain, status);
776         if (!centry) goto skip_save;
777         centry_put_uint32(centry, *num_names);
778         for (i=0; i<(*num_names); i++) {
779                 centry_put_uint32(centry, (*rid_mem)[i]);
780                 centry_put_string(centry, (*names)[i]);
781                 centry_put_uint32(centry, (*name_types)[i]);
782         }       
783         centry_end(centry, "GM/%s/%x", domain->name, group_rid);
784         centry_free(centry);
785
786 skip_save:
787         return status;
788 }
789
790 /* find the sequence number for a domain */
791 static NTSTATUS sequence_number(struct winbindd_domain *domain, uint32 *seq)
792 {
793         refresh_sequence_number(domain, False);
794
795         *seq = domain->sequence_number;
796
797         return NT_STATUS_OK;
798 }
799
800 /* enumerate trusted domains */
801 static NTSTATUS trusted_domains(struct winbindd_domain *domain,
802                                 TALLOC_CTX *mem_ctx,
803                                 uint32 *num_domains,
804                                 char ***names,
805                                 DOM_SID **dom_sids)
806 {
807         struct winbind_cache *cache = get_cache(domain);
808
809         /* we don't cache this call */
810         return cache->backend->trusted_domains(domain, mem_ctx, num_domains, 
811                                                names, dom_sids);
812 }
813
814 /* find the domain sid */
815 static NTSTATUS domain_sid(struct winbindd_domain *domain, DOM_SID *sid)
816 {
817         struct winbind_cache *cache = get_cache(domain);
818
819         /* we don't cache this call */
820         return cache->backend->domain_sid(domain, sid);
821 }
822
823 /* the ADS backend methods are exposed via this structure */
824 struct winbindd_methods cache_methods = {
825         True,
826         query_user_list,
827         enum_dom_groups,
828         name_to_sid,
829         sid_to_name,
830         query_user,
831         lookup_usergroups,
832         lookup_groupmem,
833         sequence_number,
834         trusted_domains,
835         domain_sid
836 };
837
838