removed bogus prepend_domain() call which was screwing up getpwuid()
[samba.git] / source3 / nsswitch / winbindd_cache.c
1 /* 
2    Unix SMB/CIFS 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         unsigned time_diff;
177
178         time_diff = time(NULL) - domain->last_seq_check;
179
180         /* see if we have to refetch the domain sequence number */
181         if (!force && (time_diff < lp_winbind_cache_time())) {
182                 return;
183         }
184
185         status = wcache->backend->sequence_number(domain, &domain->sequence_number);
186
187         if (!NT_STATUS_IS_OK(status)) {
188                 domain->sequence_number = DOM_SEQUENCE_NONE;
189         }
190
191         domain->last_seq_check = time(NULL);
192 }
193
194 /*
195   decide if a cache entry has expired
196 */
197 static BOOL centry_expired(struct winbindd_domain *domain, struct cache_entry *centry)
198 {
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) {
203                 return True;
204         }
205
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) {
210                 return False;
211         }
212
213         /* it's expired */
214         return True;
215 }
216
217 /*
218   fetch an entry from the cache, with a varargs key. auto-fetch the sequence
219   number and return status
220 */
221 static struct cache_entry *wcache_fetch(struct winbind_cache *cache, 
222                                         struct winbindd_domain *domain,
223                                         const char *format, ...)
224 {
225         va_list ap;
226         char *kstr;
227         TDB_DATA data;
228         struct cache_entry *centry;
229         TDB_DATA key;
230
231         refresh_sequence_number(domain, False);
232
233         va_start(ap, format);
234         smb_xvasprintf(&kstr, format, ap);
235         va_end(ap);
236         
237         key.dptr = kstr;
238         key.dsize = strlen(kstr);
239         data = tdb_fetch(wcache->tdb, key);
240         free(kstr);
241         if (!data.dptr) {
242                 /* a cache miss */
243                 return NULL;
244         }
245
246         centry = smb_xmalloc(sizeof(*centry));
247         centry->data = data.dptr;
248         centry->len = data.dsize;
249         centry->ofs = 0;
250
251         if (centry->len < 8) {
252                 /* huh? corrupt cache? */
253                 centry_free(centry);
254                 return NULL;
255         }
256         
257         centry->status = NT_STATUS(centry_uint32(centry));
258         centry->sequence_number = centry_uint32(centry);
259
260         if (centry_expired(domain, centry)) {
261                 centry_free(centry);
262                 return NULL;
263         }
264
265         return centry;
266 }
267
268 /*
269   make sure we have at least len bytes available in a centry 
270 */
271 static void centry_expand(struct cache_entry *centry, uint32 len)
272 {
273         uint8 *p;
274         if (centry->len - centry->ofs >= len) return;
275         centry->len *= 2;
276         p = realloc(centry->data, centry->len);
277         if (!p) {
278                 DEBUG(0,("out of memory: needed %d bytes in centry_expand\n", centry->len));
279                 smb_panic("out of memory in centry_expand");
280         }
281         centry->data = p;
282 }
283
284 /*
285   push a uint32 into a centry 
286 */
287 static void centry_put_uint32(struct cache_entry *centry, uint32 v)
288 {
289         centry_expand(centry, 4);
290         SIVAL(centry->data, centry->ofs, v);
291         centry->ofs += 4;
292 }
293
294 /*
295   push a uint8 into a centry 
296 */
297 static void centry_put_uint8(struct cache_entry *centry, uint8 v)
298 {
299         centry_expand(centry, 1);
300         SCVAL(centry->data, centry->ofs, v);
301         centry->ofs += 1;
302 }
303
304 /* 
305    push a string into a centry 
306  */
307 static void centry_put_string(struct cache_entry *centry, const char *s)
308 {
309         int len;
310
311         if (!s) {
312                 /* null strings are marked as len 0xFFFF */
313                 centry_put_uint8(centry, 0xFF);
314                 return;
315         }
316
317         len = strlen(s);
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);
323         centry->ofs += len;
324 }
325
326 /*
327   start a centry for output. When finished, call centry_end()
328 */
329 struct cache_entry *centry_start(struct winbindd_domain *domain, NTSTATUS status)
330 {
331         struct cache_entry *centry;
332
333         if (!wcache->tdb) return NULL;
334
335         centry = smb_xmalloc(sizeof(*centry));
336
337         centry->len = 8192; /* reasonable default */
338         centry->data = smb_xmalloc(centry->len);
339         centry->ofs = 0;
340         centry->sequence_number = domain->sequence_number;
341         centry_put_uint32(centry, NT_STATUS_V(status));
342         centry_put_uint32(centry, centry->sequence_number);
343         return centry;
344 }
345
346 /*
347   finish a centry and write it to the tdb
348 */
349 static void centry_end(struct cache_entry *centry, const char *format, ...)
350 {
351         va_list ap;
352         char *kstr;
353         TDB_DATA key, data;
354
355         va_start(ap, format);
356         smb_xvasprintf(&kstr, format, ap);
357         va_end(ap);
358
359         key.dptr = kstr;
360         key.dsize = strlen(kstr);
361         data.dptr = centry->data;
362         data.dsize = centry->ofs;
363
364         tdb_store(wcache->tdb, key, data, TDB_REPLACE);
365         free(kstr);
366 }
367
368 /* form a sid from the domain plus rid */
369 static DOM_SID *form_sid(struct winbindd_domain *domain, uint32 rid)
370 {
371         static DOM_SID sid;
372         sid_copy(&sid, &domain->sid);
373         sid_append_rid(&sid, rid);
374         return &sid;
375 }
376
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)
379 {
380         struct cache_entry *centry;
381         uint32 len;
382
383         centry = centry_start(domain, status);
384         if (!centry) return;
385         len = sid_size(sid);
386         centry_expand(centry, len);
387         centry_put_uint32(centry, type);
388         sid_linearize(centry->data + centry->ofs, len, sid);
389         centry->ofs += len;
390         centry_end(centry, "NS/%s/%s", domain->name, name);
391         centry_free(centry);
392 }
393
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)
396 {
397         struct cache_entry *centry;
398
399         centry = centry_start(domain, status);
400         if (!centry) return;
401         if (NT_STATUS_IS_OK(status)) {
402                 centry_put_uint32(centry, type);
403                 centry_put_string(centry, name);
404         }
405         centry_end(centry, "SN/%s/%d", domain->name, rid);
406         centry_free(centry);
407 }
408
409
410 static void wcache_save_user(struct winbindd_domain *domain, NTSTATUS status, WINBIND_USERINFO *info)
411 {
412         struct cache_entry *centry;
413
414         centry = centry_start(domain, status);
415         if (!centry) return;
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);
421         centry_free(centry);
422 }
423
424
425 /* Query display info. This is the basic user list fn */
426 static NTSTATUS query_user_list(struct winbindd_domain *domain,
427                                 TALLOC_CTX *mem_ctx,
428                                 uint32 *num_entries, 
429                                 WINBIND_USERINFO **info)
430 {
431         struct winbind_cache *cache = get_cache(domain);
432         struct cache_entry *centry = NULL;
433         NTSTATUS status;
434         int i;
435
436         if (!cache->tdb) goto do_query;
437
438         centry = wcache_fetch(cache, domain, "UL/%s", domain->name);
439         if (!centry) goto do_query;
440
441         *num_entries = centry_uint32(centry);
442         
443         if (*num_entries == 0) goto do_cached;
444
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);
452         }
453
454 do_cached:      
455         status = centry->status;
456         centry_free(centry);
457         return status;
458
459 do_query:
460         *num_entries = 0;
461         *info = NULL;
462
463         if (wcache_server_down(domain)) {
464                 return NT_STATUS_SERVER_DISABLED;
465         }
466
467         status = cache->backend->query_user_list(domain, mem_ctx, num_entries, info);
468
469         /* and save it */
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),
484                                                 SID_NAME_USER);
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]);
490                 }
491         }       
492         centry_end(centry, "UL/%s", domain->name);
493         centry_free(centry);
494
495 skip_save:
496         return status;
497 }
498
499 /* list all domain groups */
500 static NTSTATUS enum_dom_groups(struct winbindd_domain *domain,
501                                 TALLOC_CTX *mem_ctx,
502                                 uint32 *num_entries, 
503                                 struct acct_info **info)
504 {
505         struct winbind_cache *cache = get_cache(domain);
506         struct cache_entry *centry = NULL;
507         NTSTATUS status;
508         int i;
509
510         if (!cache->tdb) goto do_query;
511
512         centry = wcache_fetch(cache, domain, "GL/%s", domain->name);
513         if (!centry) goto do_query;
514
515         *num_entries = centry_uint32(centry);
516         
517         if (*num_entries == 0) goto do_cached;
518
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);
525         }
526
527 do_cached:      
528         status = centry->status;
529         centry_free(centry);
530         return status;
531
532 do_query:
533         *num_entries = 0;
534         *info = NULL;
535
536         if (wcache_server_down(domain)) {
537                 return NT_STATUS_SERVER_DISABLED;
538         }
539
540         status = cache->backend->enum_dom_groups(domain, mem_ctx, num_entries, info);
541
542         /* and save it */
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);
551         }       
552         centry_end(centry, "GL/%s", domain->name);
553         centry_free(centry);
554
555 skip_save:
556         return status;
557 }
558
559
560 /* convert a single name to a sid in a domain */
561 static NTSTATUS name_to_sid(struct winbindd_domain *domain,
562                             const char *name,
563                             DOM_SID *sid,
564                             enum SID_NAME_USE *type)
565 {
566         struct winbind_cache *cache = get_cache(domain);
567         struct cache_entry *centry = NULL;
568         NTSTATUS status;
569
570         if (!cache->tdb) goto do_query;
571
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);
576
577         status = centry->status;
578         centry_free(centry);
579         return status;
580
581 do_query:
582         ZERO_STRUCTP(sid);
583
584         if (wcache_server_down(domain)) {
585                 return NT_STATUS_SERVER_DISABLED;
586         }
587         status = cache->backend->name_to_sid(domain, name, sid, type);
588
589         /* and save it */
590         wcache_save_name_to_sid(domain, status, name, sid, *type);
591
592         return status;
593 }
594
595 /* convert a sid to a user or group name. The sid is guaranteed to be in the domain
596    given */
597 static NTSTATUS sid_to_name(struct winbindd_domain *domain,
598                             TALLOC_CTX *mem_ctx,
599                             DOM_SID *sid,
600                             char **name,
601                             enum SID_NAME_USE *type)
602 {
603         struct winbind_cache *cache = get_cache(domain);
604         struct cache_entry *centry = NULL;
605         NTSTATUS status;
606         uint32 rid = 0;
607
608         sid_peek_rid(sid, &rid);
609
610         if (!cache->tdb) goto do_query;
611
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);
617         }
618         status = centry->status;
619         centry_free(centry);
620         return status;
621
622 do_query:
623         *name = NULL;
624
625         if (wcache_server_down(domain)) {
626                 return NT_STATUS_SERVER_DISABLED;
627         }
628         status = cache->backend->sid_to_name(domain, mem_ctx, sid, name, type);
629
630         /* and save it */
631         refresh_sequence_number(domain, True);
632         wcache_save_sid_to_name(domain, status, sid, *name, *type, rid);
633
634         return status;
635 }
636
637
638 /* Lookup user information from a rid */
639 static NTSTATUS query_user(struct winbindd_domain *domain, 
640                            TALLOC_CTX *mem_ctx, 
641                            uint32 user_rid, 
642                            WINBIND_USERINFO *info)
643 {
644         struct winbind_cache *cache = get_cache(domain);
645         struct cache_entry *centry = NULL;
646         NTSTATUS status;
647
648         if (!cache->tdb) goto do_query;
649
650         centry = wcache_fetch(cache, domain, "U/%s/%x", domain->name, user_rid);
651         if (!centry) goto do_query;
652
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;
658         centry_free(centry);
659         return status;
660
661 do_query:
662         ZERO_STRUCTP(info);
663
664         if (wcache_server_down(domain)) {
665                 return NT_STATUS_SERVER_DISABLED;
666         }
667         
668         status = cache->backend->query_user(domain, mem_ctx, user_rid, info);
669
670         /* and save it */
671         refresh_sequence_number(domain, True);
672         wcache_save_user(domain, status, info);
673
674         return status;
675 }
676
677
678 /* Lookup groups a user is a member of. */
679 static NTSTATUS lookup_usergroups(struct winbindd_domain *domain,
680                                   TALLOC_CTX *mem_ctx,
681                                   uint32 user_rid, 
682                                   uint32 *num_groups, uint32 **user_gids)
683 {
684         struct winbind_cache *cache = get_cache(domain);
685         struct cache_entry *centry = NULL;
686         NTSTATUS status;
687         int i;
688
689         if (!cache->tdb) goto do_query;
690
691         centry = wcache_fetch(cache, domain, "UG/%s/%x", domain->name, user_rid);
692         if (!centry) goto do_query;
693
694         *num_groups = centry_uint32(centry);
695         
696         if (*num_groups == 0) goto do_cached;
697
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);
702         }
703
704 do_cached:      
705         status = centry->status;
706         centry_free(centry);
707         return status;
708
709 do_query:
710         (*num_groups) = 0;
711         (*user_gids) = NULL;
712
713         if (wcache_server_down(domain)) {
714                 return NT_STATUS_SERVER_DISABLED;
715         }
716         status = cache->backend->lookup_usergroups(domain, mem_ctx, user_rid, num_groups, user_gids);
717
718         /* and save it */
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]);
725         }       
726         centry_end(centry, "UG/%s/%x", domain->name, user_rid);
727         centry_free(centry);
728
729 skip_save:
730         return status;
731 }
732
733
734 static NTSTATUS lookup_groupmem(struct winbindd_domain *domain,
735                                 TALLOC_CTX *mem_ctx,
736                                 uint32 group_rid, uint32 *num_names, 
737                                 uint32 **rid_mem, char ***names, 
738                                 uint32 **name_types)
739 {
740         struct winbind_cache *cache = get_cache(domain);
741         struct cache_entry *centry = NULL;
742         NTSTATUS status;
743         int i;
744
745         if (!cache->tdb) goto do_query;
746
747         centry = wcache_fetch(cache, domain, "GM/%s/%x", domain->name, group_rid);
748         if (!centry) goto do_query;
749
750         *num_names = centry_uint32(centry);
751         
752         if (*num_names == 0) goto do_cached;
753
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));
757
758         if (! (*rid_mem) || ! (*names) || ! (*name_types)) {
759                 smb_panic("lookup_groupmem out of memory");
760         }
761
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);
766         }
767
768 do_cached:      
769         status = centry->status;
770         centry_free(centry);
771         return status;
772
773 do_query:
774         (*num_names) = 0;
775         (*rid_mem) = NULL;
776         (*names) = NULL;
777         (*name_types) = NULL;
778         
779
780         if (wcache_server_down(domain)) {
781                 return NT_STATUS_SERVER_DISABLED;
782         }
783         status = cache->backend->lookup_groupmem(domain, mem_ctx, group_rid, num_names, 
784                                                  rid_mem, names, name_types);
785
786         /* and save it */
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]);
795         }       
796         centry_end(centry, "GM/%s/%x", domain->name, group_rid);
797         centry_free(centry);
798
799 skip_save:
800         return status;
801 }
802
803 /* find the sequence number for a domain */
804 static NTSTATUS sequence_number(struct winbindd_domain *domain, uint32 *seq)
805 {
806         refresh_sequence_number(domain, False);
807
808         *seq = domain->sequence_number;
809
810         return NT_STATUS_OK;
811 }
812
813 /* enumerate trusted domains */
814 static NTSTATUS trusted_domains(struct winbindd_domain *domain,
815                                 TALLOC_CTX *mem_ctx,
816                                 uint32 *num_domains,
817                                 char ***names,
818                                 DOM_SID **dom_sids)
819 {
820         struct winbind_cache *cache = get_cache(domain);
821
822         /* we don't cache this call */
823         return cache->backend->trusted_domains(domain, mem_ctx, num_domains, 
824                                                names, dom_sids);
825 }
826
827 /* find the domain sid */
828 static NTSTATUS domain_sid(struct winbindd_domain *domain, DOM_SID *sid)
829 {
830         struct winbind_cache *cache = get_cache(domain);
831
832         /* we don't cache this call */
833         return cache->backend->domain_sid(domain, sid);
834 }
835
836 /* the ADS backend methods are exposed via this structure */
837 struct winbindd_methods cache_methods = {
838         True,
839         query_user_list,
840         enum_dom_groups,
841         name_to_sid,
842         sid_to_name,
843         query_user,
844         lookup_usergroups,
845         lookup_groupmem,
846         sequence_number,
847         trusted_domains,
848         domain_sid
849 };
850
851