r25568: move idmap related functions into their own file.
[ira/wip.git] / source3 / winbindd / winbindd_async.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    Async helpers for blocking functions
5
6    Copyright (C) Volker Lendecke 2005
7    Copyright (C) Gerald Carter 2006
8    
9    The helpers always consist of three functions: 
10
11    * A request setup function that takes the necessary parameters together
12      with a continuation function that is to be called upon completion
13
14    * A private continuation function that is internal only. This is to be
15      called by the lower-level functions in do_async(). Its only task is to
16      properly call the continuation function named above.
17
18    * A worker function that is called inside the appropriate child process.
19
20    This program is free software; you can redistribute it and/or modify
21    it under the terms of the GNU General Public License as published by
22    the Free Software Foundation; either version 3 of the License, or
23    (at your option) any later version.
24    
25    This program is distributed in the hope that it will be useful,
26    but WITHOUT ANY WARRANTY; without even the implied warranty of
27    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28    GNU General Public License for more details.
29    
30    You should have received a copy of the GNU General Public License
31    along with this program.  If not, see <http://www.gnu.org/licenses/>.
32 */
33
34 #include "includes.h"
35 #include "winbindd.h"
36
37 #undef DBGC_CLASS
38 #define DBGC_CLASS DBGC_WINBIND
39
40 struct do_async_state {
41         TALLOC_CTX *mem_ctx;
42         struct winbindd_request request;
43         struct winbindd_response response;
44         void (*cont)(TALLOC_CTX *mem_ctx,
45                      BOOL success,
46                      struct winbindd_response *response,
47                      void *c, void *private_data);
48         void *c, *private_data;
49 };
50
51 static void do_async_recv(void *private_data, BOOL success)
52 {
53         struct do_async_state *state =
54                 talloc_get_type_abort(private_data, struct do_async_state);
55
56         state->cont(state->mem_ctx, success, &state->response,
57                     state->c, state->private_data);
58 }
59
60 void do_async(TALLOC_CTX *mem_ctx, struct winbindd_child *child,
61               const struct winbindd_request *request,
62               void (*cont)(TALLOC_CTX *mem_ctx, BOOL success,
63                            struct winbindd_response *response,
64                            void *c, void *private_data),
65               void *c, void *private_data)
66 {
67         struct do_async_state *state;
68
69         state = TALLOC_ZERO_P(mem_ctx, struct do_async_state);
70         if (state == NULL) {
71                 DEBUG(0, ("talloc failed\n"));
72                 cont(mem_ctx, False, NULL, c, private_data);
73                 return;
74         }
75
76         state->mem_ctx = mem_ctx;
77         state->request = *request;
78         state->request.length = sizeof(state->request);
79         state->cont = cont;
80         state->c = c;
81         state->private_data = private_data;
82
83         async_request(mem_ctx, child, &state->request,
84                       &state->response, do_async_recv, state);
85 }
86
87 void do_async_domain(TALLOC_CTX *mem_ctx, struct winbindd_domain *domain,
88                      const struct winbindd_request *request,
89                      void (*cont)(TALLOC_CTX *mem_ctx, BOOL success,
90                                   struct winbindd_response *response,
91                                   void *c, void *private_data),
92                      void *c, void *private_data)
93 {
94         struct do_async_state *state;
95
96         state = TALLOC_ZERO_P(mem_ctx, struct do_async_state);
97         if (state == NULL) {
98                 DEBUG(0, ("talloc failed\n"));
99                 cont(mem_ctx, False, NULL, c, private_data);
100                 return;
101         }
102
103         state->mem_ctx = mem_ctx;
104         state->request = *request;
105         state->request.length = sizeof(state->request);
106         state->cont = cont;
107         state->c = c;
108         state->private_data = private_data;
109
110         async_domain_request(mem_ctx, domain, &state->request,
111                              &state->response, do_async_recv, state);
112 }
113
114 struct lookupsid_state {
115         DOM_SID sid;    
116         void *caller_private_data;
117 };
118
119
120 static void lookupsid_recv2(TALLOC_CTX *mem_ctx, BOOL success,
121                            struct winbindd_response *response,
122                            void *c, void *private_data)
123 {
124         void (*cont)(void *priv, BOOL succ, const char *dom_name,
125                      const char *name, enum lsa_SidType type) =
126                 (void (*)(void *, BOOL, const char *, const char *,
127                           enum lsa_SidType))c;
128         struct lookupsid_state *s = talloc_get_type_abort(private_data, 
129                                                           struct lookupsid_state);
130
131         if (!success) {
132                 DEBUG(5, ("Could not trigger lookupsid\n"));
133                 cont(s->caller_private_data, False, NULL, NULL, SID_NAME_UNKNOWN);
134                 return;
135         }
136
137         if (response->result != WINBINDD_OK) {
138                 DEBUG(5, ("lookupsid (forest root) returned an error\n"));              
139                 cont(s->caller_private_data, False, NULL, NULL, SID_NAME_UNKNOWN);
140                 return;
141         }
142
143         cont(s->caller_private_data, True, response->data.name.dom_name,
144              response->data.name.name,
145              (enum lsa_SidType)response->data.name.type);
146 }
147
148 static void lookupsid_recv(TALLOC_CTX *mem_ctx, BOOL success,
149                            struct winbindd_response *response,
150                            void *c, void *private_data)
151 {
152         void (*cont)(void *priv, BOOL succ, const char *dom_name,
153                      const char *name, enum lsa_SidType type) =
154                 (void (*)(void *, BOOL, const char *, const char *,
155                           enum lsa_SidType))c;
156         struct lookupsid_state *s = talloc_get_type_abort(private_data, 
157                                                           struct lookupsid_state);
158
159         if (!success) {
160                 DEBUG(5, ("Could not trigger lookupsid\n"));
161                 cont(s->caller_private_data, False, NULL, NULL, SID_NAME_UNKNOWN);
162                 return;
163         }
164
165         if (response->result != WINBINDD_OK) {
166                 /* Try again using the forest root */
167                 struct winbindd_domain *root_domain = find_root_domain();
168                 struct winbindd_request request;
169                 
170                 if ( !root_domain ) {
171                         DEBUG(5,("lookupsid_recv: unable to determine forest root\n"));
172                         cont(s->caller_private_data, False, NULL, NULL, SID_NAME_UNKNOWN);
173                         return;
174                 }
175
176                 ZERO_STRUCT(request);
177                 request.cmd = WINBINDD_LOOKUPSID;
178                 fstrcpy(request.data.sid, sid_string_static(&s->sid));
179
180                 do_async_domain(mem_ctx, root_domain, &request, lookupsid_recv2,
181                                 (void *)cont, s);
182
183                 return;
184         }
185
186         cont(s->caller_private_data, True, response->data.name.dom_name,
187              response->data.name.name,
188              (enum lsa_SidType)response->data.name.type);
189 }
190
191 void winbindd_lookupsid_async(TALLOC_CTX *mem_ctx, const DOM_SID *sid,
192                               void (*cont)(void *private_data, BOOL success,
193                                            const char *dom_name,
194                                            const char *name,
195                                            enum lsa_SidType type),
196                               void *private_data)
197 {
198         struct winbindd_domain *domain;
199         struct winbindd_request request;
200         struct lookupsid_state *s;      
201
202         domain = find_lookup_domain_from_sid(sid);
203         if (domain == NULL) {
204                 DEBUG(5, ("Could not find domain for sid %s\n",
205                           sid_string_static(sid)));
206                 cont(private_data, False, NULL, NULL, SID_NAME_UNKNOWN);
207                 return;
208         }
209
210         ZERO_STRUCT(request);
211         request.cmd = WINBINDD_LOOKUPSID;
212         fstrcpy(request.data.sid, sid_string_static(sid));
213
214         if ( (s = TALLOC_ZERO_P(mem_ctx, struct lookupsid_state)) == NULL ) {
215                 DEBUG(0, ("winbindd_lookupsid_async: talloc failed\n"));
216                 cont(private_data, False, NULL, NULL, SID_NAME_UNKNOWN);
217                 return;
218         }
219
220         sid_copy( &s->sid, sid );       
221         s->caller_private_data = private_data;  
222
223         do_async_domain(mem_ctx, domain, &request, lookupsid_recv,
224                         (void *)cont, s);
225 }
226
227 enum winbindd_result winbindd_dual_lookupsid(struct winbindd_domain *domain,
228                                              struct winbindd_cli_state *state)
229 {
230         enum lsa_SidType type;
231         DOM_SID sid;
232         char *name;
233         char *dom_name;
234
235         /* Ensure null termination */
236         state->request.data.sid[sizeof(state->request.data.sid)-1]='\0';
237
238         DEBUG(3, ("[%5lu]: lookupsid %s\n", (unsigned long)state->pid, 
239                   state->request.data.sid));
240
241         /* Lookup sid from PDC using lsa_lookup_sids() */
242
243         if (!string_to_sid(&sid, state->request.data.sid)) {
244                 DEBUG(5, ("%s not a SID\n", state->request.data.sid));
245                 return WINBINDD_ERROR;
246         }
247
248         /* Lookup the sid */
249
250         if (!winbindd_lookup_name_by_sid(state->mem_ctx, domain, &sid, 
251                                          &dom_name, &name, &type)) 
252         {
253                 TALLOC_FREE(dom_name);
254                 TALLOC_FREE(name);
255                 return WINBINDD_ERROR;
256         }
257
258         fstrcpy(state->response.data.name.dom_name, dom_name);
259         fstrcpy(state->response.data.name.name, name);
260         state->response.data.name.type = type;
261
262         TALLOC_FREE(dom_name);
263         TALLOC_FREE(name);
264         return WINBINDD_OK;
265 }
266
267 /********************************************************************
268  This is the second callback after contacting the forest root
269 ********************************************************************/
270
271 struct lookupname_state {
272         char *dom_name;
273         char *name;
274         void *caller_private_data;
275 };
276
277
278 static void lookupname_recv2(TALLOC_CTX *mem_ctx, BOOL success,
279                             struct winbindd_response *response,
280                             void *c, void *private_data)
281 {
282         void (*cont)(void *priv, BOOL succ, const DOM_SID *sid,
283                      enum lsa_SidType type) =
284                 (void (*)(void *, BOOL, const DOM_SID *, enum lsa_SidType))c;
285         DOM_SID sid;
286         struct lookupname_state *s = talloc_get_type_abort( private_data, 
287                                                             struct lookupname_state );
288         
289
290         if (!success) {
291                 DEBUG(5, ("Could not trigger lookup_name\n"));
292                 cont(s->caller_private_data, False, NULL, SID_NAME_UNKNOWN);
293                 return;
294         }
295
296         if (response->result != WINBINDD_OK) {
297                 DEBUG(5, ("lookup_name returned an error\n"));
298                 cont(s->caller_private_data, False, NULL, SID_NAME_UNKNOWN);
299                 return;
300         }
301
302         if (!string_to_sid(&sid, response->data.sid.sid)) {
303                 DEBUG(0, ("Could not convert string %s to sid\n",
304                           response->data.sid.sid));
305                 cont(s->caller_private_data, False, NULL, SID_NAME_UNKNOWN);
306                 return;
307         }
308
309         cont(s->caller_private_data, True, &sid,
310              (enum lsa_SidType)response->data.sid.type);
311 }
312
313 /********************************************************************
314  This is the first callback after contacting our own domain 
315 ********************************************************************/
316
317 static void lookupname_recv(TALLOC_CTX *mem_ctx, BOOL success,
318                             struct winbindd_response *response,
319                             void *c, void *private_data)
320 {
321         void (*cont)(void *priv, BOOL succ, const DOM_SID *sid,
322                      enum lsa_SidType type) =
323                 (void (*)(void *, BOOL, const DOM_SID *, enum lsa_SidType))c;
324         DOM_SID sid;
325         struct lookupname_state *s = talloc_get_type_abort( private_data, 
326                                                             struct lookupname_state );  
327
328         if (!success) {
329                 DEBUG(5, ("lookupname_recv: lookup_name() failed!\n"));
330                 cont(s->caller_private_data, False, NULL, SID_NAME_UNKNOWN);
331                 return;
332         }
333
334         if (response->result != WINBINDD_OK) {
335                 /* Try again using the forest root */
336                 struct winbindd_domain *root_domain = find_root_domain();
337                 struct winbindd_request request;                
338                 
339                 if ( !root_domain ) {
340                         DEBUG(5,("lookupname_recv: unable to determine forest root\n"));
341                         cont(s->caller_private_data, False, NULL, SID_NAME_UNKNOWN);
342                         return;
343                 }
344
345                 ZERO_STRUCT(request);
346                 request.cmd = WINBINDD_LOOKUPNAME;
347
348                 fstrcpy( request.data.name.dom_name, s->dom_name );
349                 fstrcpy( request.data.name.name, s->name );             
350
351                 do_async_domain(mem_ctx, root_domain, &request, lookupname_recv2,
352                                 (void *)cont, s);
353
354                 return;
355         }
356
357         if (!string_to_sid(&sid, response->data.sid.sid)) {
358                 DEBUG(0, ("Could not convert string %s to sid\n",
359                           response->data.sid.sid));
360                 cont(s->caller_private_data, False, NULL, SID_NAME_UNKNOWN);
361                 return;
362         }
363
364         cont(s->caller_private_data, True, &sid,
365              (enum lsa_SidType)response->data.sid.type);
366 }
367
368 /********************************************************************
369  The lookup name call first contacts a DC in its own domain
370  and fallbacks to contact a DC in the forest in our domain doesn't
371  know the name.
372 ********************************************************************/
373
374 void winbindd_lookupname_async(TALLOC_CTX *mem_ctx,
375                                const char *dom_name, const char *name,
376                                void (*cont)(void *private_data, BOOL success,
377                                             const DOM_SID *sid,
378                                             enum lsa_SidType type),
379                                enum winbindd_cmd orig_cmd,
380                                void *private_data)
381 {
382         struct winbindd_request request;
383         struct winbindd_domain *domain;
384         struct lookupname_state *s;     
385
386         if ( (domain = find_lookup_domain_from_name(dom_name)) == NULL ) {
387                 DEBUG(5, ("Could not find domain for name %s\n", dom_name));
388                 cont(private_data, False, NULL, SID_NAME_UNKNOWN);
389                 return;
390         }
391
392         ZERO_STRUCT(request);
393         request.cmd = WINBINDD_LOOKUPNAME;
394         request.original_cmd = orig_cmd;
395         fstrcpy(request.data.name.dom_name, dom_name);
396         fstrcpy(request.data.name.name, name);
397
398         if ( (s = TALLOC_ZERO_P(mem_ctx, struct lookupname_state)) == NULL ) {
399                 DEBUG(0, ("winbindd_lookupname_async: talloc failed\n"));
400                 cont(private_data, False, NULL, SID_NAME_UNKNOWN);
401                 return;
402         }
403
404         s->dom_name = talloc_strdup( s, dom_name );
405         s->name     = talloc_strdup( s, name );
406         s->caller_private_data = private_data;
407
408         do_async_domain(mem_ctx, domain, &request, lookupname_recv,
409                         (void *)cont, s);
410 }
411
412 enum winbindd_result winbindd_dual_lookupname(struct winbindd_domain *domain,
413                                               struct winbindd_cli_state *state)
414 {
415         enum lsa_SidType type;
416         char *name_domain, *name_user;
417         DOM_SID sid;
418         char *p;
419
420         /* Ensure null termination */
421         state->request.data.name.dom_name[sizeof(state->request.data.name.dom_name)-1]='\0';
422
423         /* Ensure null termination */
424         state->request.data.name.name[sizeof(state->request.data.name.name)-1]='\0';
425
426         /* cope with the name being a fully qualified name */
427         p = strstr(state->request.data.name.name, lp_winbind_separator());
428         if (p) {
429                 *p = 0;
430                 name_domain = state->request.data.name.name;
431                 name_user = p+1;
432         } else {
433                 name_domain = state->request.data.name.dom_name;
434                 name_user = state->request.data.name.name;
435         }
436
437         DEBUG(3, ("[%5lu]: lookupname %s%s%s\n", (unsigned long)state->pid,
438                   name_domain, lp_winbind_separator(), name_user));
439
440         /* Lookup name from DC using lsa_lookup_names() */
441         if (!winbindd_lookup_sid_by_name(state->mem_ctx, state->request.original_cmd, domain, name_domain,
442                                          name_user, &sid, &type)) {
443                 return WINBINDD_ERROR;
444         }
445
446         sid_to_string(state->response.data.sid.sid, &sid);
447         state->response.data.sid.type = type;
448
449         return WINBINDD_OK;
450 }
451
452 BOOL print_sidlist(TALLOC_CTX *mem_ctx, const DOM_SID *sids,
453                    size_t num_sids, char **result, ssize_t *len)
454 {
455         size_t i;
456         size_t buflen = 0;
457
458         *len = 0;
459         *result = NULL;
460         for (i=0; i<num_sids; i++) {
461                 sprintf_append(mem_ctx, result, len, &buflen,
462                                "%s\n", sid_string_static(&sids[i]));
463         }
464
465         if ((num_sids != 0) && (*result == NULL)) {
466                 return False;
467         }
468
469         return True;
470 }
471
472 static BOOL parse_sidlist(TALLOC_CTX *mem_ctx, char *sidstr,
473                           DOM_SID **sids, size_t *num_sids)
474 {
475         char *p, *q;
476
477         p = sidstr;
478         if (p == NULL)
479                 return False;
480
481         while (p[0] != '\0') {
482                 DOM_SID sid;
483                 q = strchr(p, '\n');
484                 if (q == NULL) {
485                         DEBUG(0, ("Got invalid sidstr: %s\n", p));
486                         return False;
487                 }
488                 *q = '\0';
489                 q += 1;
490                 if (!string_to_sid(&sid, p)) {
491                         DEBUG(0, ("Could not parse sid %s\n", p));
492                         return False;
493                 }
494                 if (!add_sid_to_array(mem_ctx, &sid, sids, num_sids)) {
495                         return False;
496                 }
497                 p = q;
498         }
499         return True;
500 }
501
502 static BOOL parse_ridlist(TALLOC_CTX *mem_ctx, char *ridstr,
503                           uint32 **rids, size_t *num_rids)
504 {
505         char *p;
506
507         p = ridstr;
508         if (p == NULL)
509                 return False;
510
511         while (p[0] != '\0') {
512                 uint32 rid;
513                 char *q;
514                 rid = strtoul(p, &q, 10);
515                 if (*q != '\n') {
516                         DEBUG(0, ("Got invalid ridstr: %s\n", p));
517                         return False;
518                 }
519                 p = q+1;
520                 ADD_TO_ARRAY(mem_ctx, uint32, rid, rids, num_rids);
521         }
522         return True;
523 }
524
525 enum winbindd_result winbindd_dual_lookuprids(struct winbindd_domain *domain,
526                                               struct winbindd_cli_state *state)
527 {
528         uint32 *rids = NULL;
529         size_t i, buflen, num_rids = 0;
530         ssize_t len;
531         DOM_SID domain_sid;
532         char *domain_name;
533         char **names;
534         enum lsa_SidType *types;
535         NTSTATUS status;
536         char *result;
537
538         DEBUG(10, ("Looking up RIDs for domain %s (%s)\n",
539                    state->request.domain_name,
540                    state->request.data.sid));
541
542         if (!parse_ridlist(state->mem_ctx, state->request.extra_data.data,
543                            &rids, &num_rids)) {
544                 DEBUG(5, ("Could not parse ridlist\n"));
545                 return WINBINDD_ERROR;
546         }
547
548         if (!string_to_sid(&domain_sid, state->request.data.sid)) {
549                 DEBUG(5, ("Could not parse domain sid %s\n",
550                           state->request.data.sid));
551                 return WINBINDD_ERROR;
552         }
553
554         status = domain->methods->rids_to_names(domain, state->mem_ctx,
555                                                 &domain_sid, rids, num_rids,
556                                                 &domain_name,
557                                                 &names, &types);
558
559         if (!NT_STATUS_IS_OK(status) &&
560             !NT_STATUS_EQUAL(status, STATUS_SOME_UNMAPPED)) {
561                 return WINBINDD_ERROR;
562         }
563
564         len = 0;
565         buflen = 0;
566         result = NULL;
567
568         for (i=0; i<num_rids; i++) {
569                 sprintf_append(state->mem_ctx, &result, &len, &buflen,
570                                "%d %s\n", types[i], names[i]);
571         }
572
573         fstrcpy(state->response.data.domain_name, domain_name);
574
575         if (result != NULL) {
576                 state->response.extra_data.data = SMB_STRDUP(result);
577                 if (!state->response.extra_data.data) {
578                         return WINBINDD_ERROR;
579                 }
580                 state->response.length += len+1;
581         }
582
583         return WINBINDD_OK;
584 }
585
586 static void getsidaliases_recv(TALLOC_CTX *mem_ctx, BOOL success,
587                                struct winbindd_response *response,
588                                void *c, void *private_data)
589 {
590         void (*cont)(void *priv, BOOL succ,
591                      DOM_SID *aliases, size_t num_aliases) =
592                 (void (*)(void *, BOOL, DOM_SID *, size_t))c;
593         char *aliases_str;
594         DOM_SID *sids = NULL;
595         size_t num_sids = 0;
596
597         if (!success) {
598                 DEBUG(5, ("Could not trigger getsidaliases\n"));
599                 cont(private_data, success, NULL, 0);
600                 return;
601         }
602
603         if (response->result != WINBINDD_OK) {
604                 DEBUG(5, ("getsidaliases returned an error\n"));
605                 cont(private_data, False, NULL, 0);
606                 return;
607         }
608
609         aliases_str = (char *)response->extra_data.data;
610
611         if (aliases_str == NULL) {
612                 DEBUG(10, ("getsidaliases return 0 SIDs\n"));
613                 cont(private_data, True, NULL, 0);
614                 return;
615         }
616
617         if (!parse_sidlist(mem_ctx, aliases_str, &sids, &num_sids)) {
618                 DEBUG(0, ("Could not parse sids\n"));
619                 cont(private_data, False, NULL, 0);
620                 return;
621         }
622
623         SAFE_FREE(response->extra_data.data);
624
625         cont(private_data, True, sids, num_sids);
626 }
627
628 void winbindd_getsidaliases_async(struct winbindd_domain *domain,
629                                   TALLOC_CTX *mem_ctx,
630                                   const DOM_SID *sids, size_t num_sids,
631                                   void (*cont)(void *private_data,
632                                                BOOL success,
633                                                const DOM_SID *aliases,
634                                                size_t num_aliases),
635                                   void *private_data)
636 {
637         struct winbindd_request request;
638         char *sidstr = NULL;
639         ssize_t len;
640
641         if (num_sids == 0) {
642                 cont(private_data, True, NULL, 0);
643                 return;
644         }
645
646         if (!print_sidlist(mem_ctx, sids, num_sids, &sidstr, &len)) {
647                 cont(private_data, False, NULL, 0);
648                 return;
649         }
650
651         ZERO_STRUCT(request);
652         request.cmd = WINBINDD_DUAL_GETSIDALIASES;
653         request.extra_len = len;
654         request.extra_data.data = sidstr;
655
656         do_async_domain(mem_ctx, domain, &request, getsidaliases_recv,
657                         (void *)cont, private_data);
658 }
659
660 enum winbindd_result winbindd_dual_getsidaliases(struct winbindd_domain *domain,
661                                                  struct winbindd_cli_state *state)
662 {
663         DOM_SID *sids = NULL;
664         size_t num_sids = 0;
665         char *sidstr = NULL;
666         ssize_t len;
667         size_t i;
668         uint32 num_aliases;
669         uint32 *alias_rids;
670         NTSTATUS result;
671
672         DEBUG(3, ("[%5lu]: getsidaliases\n", (unsigned long)state->pid));
673
674         sidstr = state->request.extra_data.data;
675         if (sidstr == NULL) {
676                 sidstr = talloc_strdup(state->mem_ctx, "\n"); /* No SID */
677                 if (!sidstr) {
678                         DEBUG(0, ("Out of memory\n"));
679                         return WINBINDD_ERROR;
680                 }
681         }
682
683         DEBUG(10, ("Sidlist: %s\n", sidstr));
684
685         if (!parse_sidlist(state->mem_ctx, sidstr, &sids, &num_sids)) {
686                 DEBUG(0, ("Could not parse SID list: %s\n", sidstr));
687                 return WINBINDD_ERROR;
688         }
689
690         num_aliases = 0;
691         alias_rids = NULL;
692
693         result = domain->methods->lookup_useraliases(domain,
694                                                      state->mem_ctx,
695                                                      num_sids, sids,
696                                                      &num_aliases,
697                                                      &alias_rids);
698
699         if (!NT_STATUS_IS_OK(result)) {
700                 DEBUG(3, ("Could not lookup_useraliases: %s\n",
701                           nt_errstr(result)));
702                 return WINBINDD_ERROR;
703         }
704
705         num_sids = 0;
706         sids = NULL;
707         sidstr = NULL;
708
709         DEBUG(10, ("Got %d aliases\n", num_aliases));
710
711         for (i=0; i<num_aliases; i++) {
712                 DOM_SID sid;
713                 DEBUGADD(10, (" rid %d\n", alias_rids[i]));
714                 sid_copy(&sid, &domain->sid);
715                 sid_append_rid(&sid, alias_rids[i]);
716                 if (!add_sid_to_array(state->mem_ctx, &sid, &sids, &num_sids)) {
717                         return WINBINDD_ERROR;
718                 }
719         }
720
721
722         if (!print_sidlist(state->mem_ctx, sids, num_sids, &sidstr, &len)) {
723                 DEBUG(0, ("Could not print_sidlist\n"));
724                 state->response.extra_data.data = NULL;
725                 return WINBINDD_ERROR;
726         }
727
728         state->response.extra_data.data = NULL;
729
730         if (sidstr) {
731                 state->response.extra_data.data = SMB_STRDUP(sidstr);
732                 if (!state->response.extra_data.data) {
733                         DEBUG(0, ("Out of memory\n"));
734                         return WINBINDD_ERROR;
735                 }
736                 DEBUG(10, ("aliases_list: %s\n",
737                            (char *)state->response.extra_data.data));
738                 state->response.length += len+1;
739         }
740         
741         return WINBINDD_OK;
742 }
743
744 struct gettoken_state {
745         TALLOC_CTX *mem_ctx;
746         DOM_SID user_sid;
747         struct winbindd_domain *alias_domain;
748         struct winbindd_domain *local_alias_domain;
749         struct winbindd_domain *builtin_domain;
750         DOM_SID *sids;
751         size_t num_sids;
752         void (*cont)(void *private_data, BOOL success, DOM_SID *sids, size_t num_sids);
753         void *private_data;
754 };
755
756 static void gettoken_recvdomgroups(TALLOC_CTX *mem_ctx, BOOL success,
757                                    struct winbindd_response *response,
758                                    void *c, void *private_data);
759 static void gettoken_recvaliases(void *private_data, BOOL success,
760                                  const DOM_SID *aliases,
761                                  size_t num_aliases);
762                                  
763
764 void winbindd_gettoken_async(TALLOC_CTX *mem_ctx, const DOM_SID *user_sid,
765                              void (*cont)(void *private_data, BOOL success,
766                                           DOM_SID *sids, size_t num_sids),
767                              void *private_data)
768 {
769         struct winbindd_domain *domain;
770         struct winbindd_request request;
771         struct gettoken_state *state;
772
773         state = TALLOC_ZERO_P(mem_ctx, struct gettoken_state);
774         if (state == NULL) {
775                 DEBUG(0, ("talloc failed\n"));
776                 cont(private_data, False, NULL, 0);
777                 return;
778         }
779
780         state->mem_ctx = mem_ctx;
781         sid_copy(&state->user_sid, user_sid);
782         state->alias_domain = find_our_domain();
783         state->local_alias_domain = find_domain_from_name( get_global_sam_name() );
784         state->builtin_domain = find_builtin_domain();
785         state->cont = cont;
786         state->private_data = private_data;
787
788         domain = find_domain_from_sid_noinit(user_sid);
789         if (domain == NULL) {
790                 DEBUG(5, ("Could not find domain from SID %s\n",
791                           sid_string_static(user_sid)));
792                 cont(private_data, False, NULL, 0);
793                 return;
794         }
795
796         ZERO_STRUCT(request);
797         request.cmd = WINBINDD_GETUSERDOMGROUPS;
798         fstrcpy(request.data.sid, sid_string_static(user_sid));
799
800         do_async_domain(mem_ctx, domain, &request, gettoken_recvdomgroups,
801                         NULL, state);
802 }
803
804 static void gettoken_recvdomgroups(TALLOC_CTX *mem_ctx, BOOL success,
805                                    struct winbindd_response *response,
806                                    void *c, void *private_data)
807 {
808         struct gettoken_state *state =
809                 talloc_get_type_abort(private_data, struct gettoken_state);
810         char *sids_str;
811         
812         if (!success) {
813                 DEBUG(10, ("Could not get domain groups\n"));
814                 state->cont(state->private_data, False, NULL, 0);
815                 return;
816         }
817
818         sids_str = (char *)response->extra_data.data;
819
820         if (sids_str == NULL) {
821                 /* This could be normal if we are dealing with a
822                    local user and local groups */
823
824                 if ( !sid_check_is_in_our_domain( &state->user_sid ) ) {
825                         DEBUG(10, ("Received no domain groups\n"));
826                         state->cont(state->private_data, True, NULL, 0);
827                         return;
828                 }
829         }
830
831         state->sids = NULL;
832         state->num_sids = 0;
833
834         if (!add_sid_to_array(mem_ctx, &state->user_sid, &state->sids,
835                          &state->num_sids)) {
836                 DEBUG(0, ("Out of memory\n"));
837                 state->cont(state->private_data, False, NULL, 0);
838                 return;
839         }
840
841         if (sids_str && !parse_sidlist(mem_ctx, sids_str, &state->sids,
842                            &state->num_sids)) {
843                 DEBUG(0, ("Could not parse sids\n"));
844                 state->cont(state->private_data, False, NULL, 0);
845                 return;
846         }
847
848         SAFE_FREE(response->extra_data.data);
849
850         if (state->alias_domain == NULL) {
851                 DEBUG(10, ("Don't expand domain local groups\n"));
852                 state->cont(state->private_data, True, state->sids,
853                             state->num_sids);
854                 return;
855         }
856
857         winbindd_getsidaliases_async(state->alias_domain, mem_ctx,
858                                      state->sids, state->num_sids,
859                                      gettoken_recvaliases, state);
860 }
861
862 static void gettoken_recvaliases(void *private_data, BOOL success,
863                                  const DOM_SID *aliases,
864                                  size_t num_aliases)
865 {
866         struct gettoken_state *state = (struct gettoken_state *)private_data;
867         size_t i;
868
869         if (!success) {
870                 DEBUG(10, ("Could not receive domain local groups\n"));
871                 state->cont(state->private_data, False, NULL, 0);
872                 return;
873         }
874
875         for (i=0; i<num_aliases; i++) {
876                 if (!add_sid_to_array(state->mem_ctx, &aliases[i],
877                                  &state->sids, &state->num_sids)) {
878                         DEBUG(0, ("Out of memory\n"));
879                         state->cont(state->private_data, False, NULL, 0);
880                         return;
881                 }
882         }
883
884         if (state->local_alias_domain != NULL) {
885                 struct winbindd_domain *local_domain = state->local_alias_domain;
886                 DEBUG(10, ("Expanding our own local groups\n"));
887                 state->local_alias_domain = NULL;
888                 winbindd_getsidaliases_async(local_domain, state->mem_ctx,
889                                              state->sids, state->num_sids,
890                                              gettoken_recvaliases, state);
891                 return;
892         }
893
894         if (state->builtin_domain != NULL) {
895                 struct winbindd_domain *builtin_domain = state->builtin_domain;
896                 DEBUG(10, ("Expanding our own BUILTIN groups\n"));
897                 state->builtin_domain = NULL;
898                 winbindd_getsidaliases_async(builtin_domain, state->mem_ctx,
899                                              state->sids, state->num_sids,
900                                              gettoken_recvaliases, state);
901                 return;
902         }
903
904         state->cont(state->private_data, True, state->sids, state->num_sids);
905 }
906
907 static void query_user_recv(TALLOC_CTX *mem_ctx, BOOL success,
908                             struct winbindd_response *response,
909                             void *c, void *private_data)
910 {
911         void (*cont)(void *priv, BOOL succ, const char *acct_name,
912                      const char *full_name, const char *homedir, 
913                      const char *shell, uint32 gid, uint32 group_rid) =
914                 (void (*)(void *, BOOL, const char *, const char *,
915                           const char *, const char *, uint32, uint32))c;
916
917         if (!success) {
918                 DEBUG(5, ("Could not trigger query_user\n"));
919                 cont(private_data, False, NULL, NULL, NULL, NULL, -1, -1);
920                 return;
921         }
922
923         if (response->result != WINBINDD_OK) {
924                 DEBUG(5, ("query_user returned an error\n"));
925                 cont(private_data, False, NULL, NULL, NULL, NULL, -1, -1);
926                 return;
927         }
928
929         cont(private_data, True, response->data.user_info.acct_name,
930              response->data.user_info.full_name,
931              response->data.user_info.homedir,
932              response->data.user_info.shell,
933              response->data.user_info.primary_gid,
934              response->data.user_info.group_rid);
935 }
936
937 void query_user_async(TALLOC_CTX *mem_ctx, struct winbindd_domain *domain,
938                       const DOM_SID *sid,
939                       void (*cont)(void *private_data, BOOL success,
940                                    const char *acct_name,
941                                    const char *full_name,
942                                    const char *homedir,
943                                    const char *shell,
944                                    gid_t gid,
945                                    uint32 group_rid),
946                       void *private_data)
947 {
948         struct winbindd_request request;
949         ZERO_STRUCT(request);
950         request.cmd = WINBINDD_DUAL_USERINFO;
951         sid_to_string(request.data.sid, sid);
952         do_async_domain(mem_ctx, domain, &request, query_user_recv,
953                         (void *)cont, private_data);
954 }