Make WINBINDD_LIST_GROUPS handler asynchronous.
[samba.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                 sid_to_fstring(request.data.sid, &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_dbg(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         sid_to_fstring(request.data.sid, 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         if (!success) {
290                 DEBUG(5, ("Could not trigger lookup_name\n"));
291                 cont(s->caller_private_data, False, NULL, SID_NAME_UNKNOWN);
292                 return;
293         }
294
295         if (response->result != WINBINDD_OK) {
296                 DEBUG(5, ("lookup_name returned an error\n"));
297                 cont(s->caller_private_data, False, NULL, SID_NAME_UNKNOWN);
298                 return;
299         }
300
301         if (!string_to_sid(&sid, response->data.sid.sid)) {
302                 DEBUG(0, ("Could not convert string %s to sid\n",
303                           response->data.sid.sid));
304                 cont(s->caller_private_data, False, NULL, SID_NAME_UNKNOWN);
305                 return;
306         }
307
308         cont(s->caller_private_data, True, &sid,
309              (enum lsa_SidType)response->data.sid.type);
310 }
311
312 /********************************************************************
313  This is the first callback after contacting our own domain
314 ********************************************************************/
315
316 static void lookupname_recv(TALLOC_CTX *mem_ctx, bool success,
317                             struct winbindd_response *response,
318                             void *c, void *private_data)
319 {
320         void (*cont)(void *priv, bool succ, const DOM_SID *sid,
321                      enum lsa_SidType type) =
322                 (void (*)(void *, bool, const DOM_SID *, enum lsa_SidType))c;
323         DOM_SID sid;
324         struct lookupname_state *s = talloc_get_type_abort( private_data,
325                                                             struct lookupname_state );  
326
327         if (!success) {
328                 DEBUG(5, ("lookupname_recv: lookup_name() failed!\n"));
329                 cont(s->caller_private_data, False, NULL, SID_NAME_UNKNOWN);
330                 return;
331         }
332
333         if (response->result != WINBINDD_OK) {
334                 /* Try again using the forest root */
335                 struct winbindd_domain *root_domain = find_root_domain();
336                 struct winbindd_request request;
337
338                 if ( !root_domain ) {
339                         DEBUG(5,("lookupname_recv: unable to determine forest root\n"));
340                         cont(s->caller_private_data, False, NULL, SID_NAME_UNKNOWN);
341                         return;
342                 }
343
344                 ZERO_STRUCT(request);
345                 request.cmd = WINBINDD_LOOKUPNAME;
346
347                 fstrcpy( request.data.name.dom_name, s->dom_name );
348                 fstrcpy( request.data.name.name, s->name );
349
350                 do_async_domain(mem_ctx, root_domain, &request, lookupname_recv2,
351                                 (void *)cont, s);
352
353                 return;
354         }
355
356         if (!string_to_sid(&sid, response->data.sid.sid)) {
357                 DEBUG(0, ("Could not convert string %s to sid\n",
358                           response->data.sid.sid));
359                 cont(s->caller_private_data, False, NULL, SID_NAME_UNKNOWN);
360                 return;
361         }
362
363         cont(s->caller_private_data, True, &sid,
364              (enum lsa_SidType)response->data.sid.type);
365 }
366
367 /********************************************************************
368  The lookup name call first contacts a DC in its own domain
369  and fallbacks to contact a DC in the forest in our domain doesn't
370  know the name.
371 ********************************************************************/
372
373 void winbindd_lookupname_async(TALLOC_CTX *mem_ctx,
374                                const char *dom_name, const char *name,
375                                void (*cont)(void *private_data, bool success,
376                                             const DOM_SID *sid,
377                                             enum lsa_SidType type),
378                                enum winbindd_cmd orig_cmd,
379                                void *private_data)
380 {
381         struct winbindd_request request;
382         struct winbindd_domain *domain;
383         struct lookupname_state *s;
384
385         if ( (domain = find_lookup_domain_from_name(dom_name)) == NULL ) {
386                 DEBUG(5, ("Could not find domain for name '%s'\n", dom_name));
387                 cont(private_data, False, NULL, SID_NAME_UNKNOWN);
388                 return;
389         }
390
391         ZERO_STRUCT(request);
392         request.cmd = WINBINDD_LOOKUPNAME;
393         request.original_cmd = orig_cmd;
394         fstrcpy(request.data.name.dom_name, dom_name);
395         fstrcpy(request.data.name.name, name);
396
397         if ( (s = TALLOC_ZERO_P(mem_ctx, struct lookupname_state)) == NULL ) {
398                 DEBUG(0, ("winbindd_lookupname_async: talloc failed\n"));
399                 cont(private_data, False, NULL, SID_NAME_UNKNOWN);
400                 return;
401         }
402
403         s->dom_name = talloc_strdup( s, dom_name );
404         s->name     = talloc_strdup( s, name );
405         if (!s->dom_name || !s->name) {
406                 cont(private_data, False, NULL, SID_NAME_UNKNOWN);
407                 return;
408         }
409
410         s->caller_private_data = private_data;
411
412         do_async_domain(mem_ctx, domain, &request, lookupname_recv,
413                         (void *)cont, s);
414 }
415
416 enum winbindd_result winbindd_dual_lookupname(struct winbindd_domain *domain,
417                                               struct winbindd_cli_state *state)
418 {
419         enum lsa_SidType type;
420         char *name_domain, *name_user;
421         DOM_SID sid;
422         char *p;
423
424         /* Ensure null termination */
425         state->request.data.name.dom_name[sizeof(state->request.data.name.dom_name)-1]='\0';
426
427         /* Ensure null termination */
428         state->request.data.name.name[sizeof(state->request.data.name.name)-1]='\0';
429
430         /* cope with the name being a fully qualified name */
431         p = strstr(state->request.data.name.name, lp_winbind_separator());
432         if (p) {
433                 *p = 0;
434                 name_domain = state->request.data.name.name;
435                 name_user = p+1;
436         } else {
437                 name_domain = state->request.data.name.dom_name;
438                 name_user = state->request.data.name.name;
439         }
440
441         DEBUG(3, ("[%5lu]: lookupname %s%s%s\n", (unsigned long)state->pid,
442                   name_domain, lp_winbind_separator(), name_user));
443
444         /* Lookup name from DC using lsa_lookup_names() */
445         if (!winbindd_lookup_sid_by_name(state->mem_ctx, state->request.original_cmd, domain, name_domain,
446                                          name_user, &sid, &type)) {
447                 return WINBINDD_ERROR;
448         }
449
450         sid_to_fstring(state->response.data.sid.sid, &sid);
451         state->response.data.sid.type = type;
452
453         return WINBINDD_OK;
454 }
455
456 /* This is the first callback after enumerating groups from a domain */
457 static void listgroups_recv(TALLOC_CTX *mem_ctx, bool success,
458                             struct winbindd_response *response,
459                             void *c, void *private_data)
460 {
461         void (*cont)(void *priv, bool succ, fstring dom_name, char *data) =
462                 (void (*)(void *, bool, fstring, char*))c;
463
464         if (!success || response->result != WINBINDD_OK) {
465                 DEBUG(5, ("list_groups() failed!\n"));
466                 cont(private_data, False, response->data.name.dom_name, NULL);
467                 return;
468         }
469
470         cont(private_data, True, response->data.name.dom_name,
471              response->extra_data.data);
472
473         SAFE_FREE(response->extra_data.data);
474 }
475
476 /* Request the name of all groups in a single domain */
477 void winbindd_listgroups_async(TALLOC_CTX *mem_ctx,
478                                struct winbindd_domain *domain,
479                                void (*cont)(void *private_data, bool success,
480                                      fstring dom_name, char* extra_data),
481                                void *private_data)
482 {
483         struct winbindd_request request;
484
485         ZERO_STRUCT(request);
486         request.cmd = WINBINDD_LIST_GROUPS;
487
488         do_async_domain(mem_ctx, domain, &request, listgroups_recv,
489                         (void *)cont, private_data);
490 }
491
492 enum winbindd_result winbindd_dual_list_groups(struct winbindd_domain *domain,
493                                                struct winbindd_cli_state *state)
494 {
495         struct getent_state groups = {};
496         char *extra_data = NULL;
497         unsigned int extra_data_len = 0, i;
498
499         /* Must copy domain into response first for bookeeping in parent */
500         fstrcpy(state->response.data.name.dom_name, domain->name);
501         fstrcpy(groups.domain_name, domain->name);
502
503         /* Get list of sam groups */
504         if (!get_sam_group_entries(&groups)) {
505                 /* this domain is empty or in an error state */
506                 return WINBINDD_ERROR;
507         }
508
509         /* Allocate some memory for extra data.  Note that we limit
510            account names to sizeof(fstring) = 256 characters.
511            +1 for the ',' between group names */
512         extra_data = (char *)SMB_REALLOC(extra_data,
513                 (sizeof(fstring) + 1) * groups.num_sam_entries);
514
515         if (!extra_data) {
516                 DEBUG(0,("failed to enlarge buffer!\n"));
517                 SAFE_FREE(groups.sam_entries);
518                 return WINBINDD_ERROR;
519         }
520
521         /* Pack group list into extra data fields */
522         for (i = 0; i < groups.num_sam_entries; i++) {
523                 char *group_name = ((struct acct_info *)
524                                     groups.sam_entries)[i].acct_name;
525                 fstring name;
526
527                 fill_domain_username(name, domain->name, group_name, True);
528                 /* Append to extra data */
529                 memcpy(&extra_data[extra_data_len], name, strlen(name));
530                 extra_data_len += strlen(name);
531                 extra_data[extra_data_len++] = ',';
532         }
533
534         SAFE_FREE(groups.sam_entries);
535
536         /* Assign extra_data fields in response structure */
537         if (extra_data) {
538                 /* remove trailing ',' */
539                 extra_data[extra_data_len - 1] = '\0';
540                 state->response.extra_data.data = extra_data;
541                 state->response.length += extra_data_len;
542         }
543
544         return WINBINDD_OK;
545 }
546
547 bool print_sidlist(TALLOC_CTX *mem_ctx, const DOM_SID *sids,
548                    size_t num_sids, char **result, ssize_t *len)
549 {
550         size_t i;
551         size_t buflen = 0;
552
553         *len = 0;
554         *result = NULL;
555         for (i=0; i<num_sids; i++) {
556                 fstring tmp;
557                 sprintf_append(mem_ctx, result, len, &buflen,
558                                "%s\n", sid_to_fstring(tmp, &sids[i]));
559         }
560
561         if ((num_sids != 0) && (*result == NULL)) {
562                 return False;
563         }
564
565         return True;
566 }
567
568 static bool parse_sidlist(TALLOC_CTX *mem_ctx, char *sidstr,
569                           DOM_SID **sids, size_t *num_sids)
570 {
571         char *p, *q;
572
573         p = sidstr;
574         if (p == NULL)
575                 return False;
576
577         while (p[0] != '\0') {
578                 DOM_SID sid;
579                 q = strchr(p, '\n');
580                 if (q == NULL) {
581                         DEBUG(0, ("Got invalid sidstr: %s\n", p));
582                         return False;
583                 }
584                 *q = '\0';
585                 q += 1;
586                 if (!string_to_sid(&sid, p)) {
587                         DEBUG(0, ("Could not parse sid %s\n", p));
588                         return False;
589                 }
590                 if (!NT_STATUS_IS_OK(add_sid_to_array(mem_ctx, &sid, sids,
591                                                       num_sids)))
592                 {
593                         return False;
594                 }
595                 p = q;
596         }
597         return True;
598 }
599
600 static bool parse_ridlist(TALLOC_CTX *mem_ctx, char *ridstr,
601                           uint32 **rids, size_t *num_rids)
602 {
603         char *p;
604
605         p = ridstr;
606         if (p == NULL)
607                 return False;
608
609         while (p[0] != '\0') {
610                 uint32 rid;
611                 char *q;
612                 rid = strtoul(p, &q, 10);
613                 if (*q != '\n') {
614                         DEBUG(0, ("Got invalid ridstr: %s\n", p));
615                         return False;
616                 }
617                 p = q+1;
618                 ADD_TO_ARRAY(mem_ctx, uint32, rid, rids, num_rids);
619         }
620         return True;
621 }
622
623 enum winbindd_result winbindd_dual_lookuprids(struct winbindd_domain *domain,
624                                               struct winbindd_cli_state *state)
625 {
626         uint32 *rids = NULL;
627         size_t i, buflen, num_rids = 0;
628         ssize_t len;
629         DOM_SID domain_sid;
630         char *domain_name;
631         char **names;
632         enum lsa_SidType *types;
633         NTSTATUS status;
634         char *result;
635
636         DEBUG(10, ("Looking up RIDs for domain %s (%s)\n",
637                    state->request.domain_name,
638                    state->request.data.sid));
639
640         if (!parse_ridlist(state->mem_ctx, state->request.extra_data.data,
641                            &rids, &num_rids)) {
642                 DEBUG(5, ("Could not parse ridlist\n"));
643                 return WINBINDD_ERROR;
644         }
645
646         if (!string_to_sid(&domain_sid, state->request.data.sid)) {
647                 DEBUG(5, ("Could not parse domain sid %s\n",
648                           state->request.data.sid));
649                 return WINBINDD_ERROR;
650         }
651
652         status = domain->methods->rids_to_names(domain, state->mem_ctx,
653                                                 &domain_sid, rids, num_rids,
654                                                 &domain_name,
655                                                 &names, &types);
656
657         if (!NT_STATUS_IS_OK(status) &&
658             !NT_STATUS_EQUAL(status, STATUS_SOME_UNMAPPED)) {
659                 return WINBINDD_ERROR;
660         }
661
662         len = 0;
663         buflen = 0;
664         result = NULL;
665
666         for (i=0; i<num_rids; i++) {
667                 sprintf_append(state->mem_ctx, &result, &len, &buflen,
668                                "%d %s\n", types[i], names[i]);
669         }
670
671         fstrcpy(state->response.data.domain_name, domain_name);
672
673         if (result != NULL) {
674                 state->response.extra_data.data = SMB_STRDUP(result);
675                 if (!state->response.extra_data.data) {
676                         return WINBINDD_ERROR;
677                 }
678                 state->response.length += len+1;
679         }
680
681         return WINBINDD_OK;
682 }
683
684 static void getsidaliases_recv(TALLOC_CTX *mem_ctx, bool success,
685                                struct winbindd_response *response,
686                                void *c, void *private_data)
687 {
688         void (*cont)(void *priv, bool succ,
689                      DOM_SID *aliases, size_t num_aliases) =
690                 (void (*)(void *, bool, DOM_SID *, size_t))c;
691         char *aliases_str;
692         DOM_SID *sids = NULL;
693         size_t num_sids = 0;
694
695         if (!success) {
696                 DEBUG(5, ("Could not trigger getsidaliases\n"));
697                 cont(private_data, success, NULL, 0);
698                 return;
699         }
700
701         if (response->result != WINBINDD_OK) {
702                 DEBUG(5, ("getsidaliases returned an error\n"));
703                 cont(private_data, False, NULL, 0);
704                 return;
705         }
706
707         aliases_str = (char *)response->extra_data.data;
708
709         if (aliases_str == NULL) {
710                 DEBUG(10, ("getsidaliases return 0 SIDs\n"));
711                 cont(private_data, True, NULL, 0);
712                 return;
713         }
714
715         if (!parse_sidlist(mem_ctx, aliases_str, &sids, &num_sids)) {
716                 DEBUG(0, ("Could not parse sids\n"));
717                 cont(private_data, False, NULL, 0);
718                 return;
719         }
720
721         SAFE_FREE(response->extra_data.data);
722
723         cont(private_data, True, sids, num_sids);
724 }
725
726 void winbindd_getsidaliases_async(struct winbindd_domain *domain,
727                                   TALLOC_CTX *mem_ctx,
728                                   const DOM_SID *sids, size_t num_sids,
729                                   void (*cont)(void *private_data,
730                                                bool success,
731                                                const DOM_SID *aliases,
732                                                size_t num_aliases),
733                                   void *private_data)
734 {
735         struct winbindd_request request;
736         char *sidstr = NULL;
737         ssize_t len;
738
739         if (num_sids == 0) {
740                 cont(private_data, True, NULL, 0);
741                 return;
742         }
743
744         if (!print_sidlist(mem_ctx, sids, num_sids, &sidstr, &len)) {
745                 cont(private_data, False, NULL, 0);
746                 return;
747         }
748
749         ZERO_STRUCT(request);
750         request.cmd = WINBINDD_DUAL_GETSIDALIASES;
751         request.extra_len = len;
752         request.extra_data.data = sidstr;
753
754         do_async_domain(mem_ctx, domain, &request, getsidaliases_recv,
755                         (void *)cont, private_data);
756 }
757
758 enum winbindd_result winbindd_dual_getsidaliases(struct winbindd_domain *domain,
759                                                  struct winbindd_cli_state *state)
760 {
761         DOM_SID *sids = NULL;
762         size_t num_sids = 0;
763         char *sidstr = NULL;
764         ssize_t len;
765         size_t i;
766         uint32 num_aliases;
767         uint32 *alias_rids;
768         NTSTATUS result;
769
770         DEBUG(3, ("[%5lu]: getsidaliases\n", (unsigned long)state->pid));
771
772         sidstr = state->request.extra_data.data;
773         if (sidstr == NULL) {
774                 sidstr = talloc_strdup(state->mem_ctx, "\n"); /* No SID */
775                 if (!sidstr) {
776                         DEBUG(0, ("Out of memory\n"));
777                         return WINBINDD_ERROR;
778                 }
779         }
780
781         DEBUG(10, ("Sidlist: %s\n", sidstr));
782
783         if (!parse_sidlist(state->mem_ctx, sidstr, &sids, &num_sids)) {
784                 DEBUG(0, ("Could not parse SID list: %s\n", sidstr));
785                 return WINBINDD_ERROR;
786         }
787
788         num_aliases = 0;
789         alias_rids = NULL;
790
791         result = domain->methods->lookup_useraliases(domain,
792                                                      state->mem_ctx,
793                                                      num_sids, sids,
794                                                      &num_aliases,
795                                                      &alias_rids);
796
797         if (!NT_STATUS_IS_OK(result)) {
798                 DEBUG(3, ("Could not lookup_useraliases: %s\n",
799                           nt_errstr(result)));
800                 return WINBINDD_ERROR;
801         }
802
803         num_sids = 0;
804         sids = NULL;
805         sidstr = NULL;
806
807         DEBUG(10, ("Got %d aliases\n", num_aliases));
808
809         for (i=0; i<num_aliases; i++) {
810                 DOM_SID sid;
811                 DEBUGADD(10, (" rid %d\n", alias_rids[i]));
812                 sid_copy(&sid, &domain->sid);
813                 sid_append_rid(&sid, alias_rids[i]);
814                 result = add_sid_to_array(state->mem_ctx, &sid, &sids,
815                                           &num_sids);
816                 if (!NT_STATUS_IS_OK(result)) {
817                         return WINBINDD_ERROR;
818                 }
819         }
820
821
822         if (!print_sidlist(state->mem_ctx, sids, num_sids, &sidstr, &len)) {
823                 DEBUG(0, ("Could not print_sidlist\n"));
824                 state->response.extra_data.data = NULL;
825                 return WINBINDD_ERROR;
826         }
827
828         state->response.extra_data.data = NULL;
829
830         if (sidstr) {
831                 state->response.extra_data.data = SMB_STRDUP(sidstr);
832                 if (!state->response.extra_data.data) {
833                         DEBUG(0, ("Out of memory\n"));
834                         return WINBINDD_ERROR;
835                 }
836                 DEBUG(10, ("aliases_list: %s\n",
837                            (char *)state->response.extra_data.data));
838                 state->response.length += len+1;
839         }
840         
841         return WINBINDD_OK;
842 }
843
844 struct gettoken_state {
845         TALLOC_CTX *mem_ctx;
846         DOM_SID user_sid;
847         struct winbindd_domain *alias_domain;
848         struct winbindd_domain *local_alias_domain;
849         struct winbindd_domain *builtin_domain;
850         DOM_SID *sids;
851         size_t num_sids;
852         void (*cont)(void *private_data, bool success, DOM_SID *sids, size_t num_sids);
853         void *private_data;
854 };
855
856 static void gettoken_recvdomgroups(TALLOC_CTX *mem_ctx, bool success,
857                                    struct winbindd_response *response,
858                                    void *c, void *private_data);
859 static void gettoken_recvaliases(void *private_data, bool success,
860                                  const DOM_SID *aliases,
861                                  size_t num_aliases);
862                                  
863
864 void winbindd_gettoken_async(TALLOC_CTX *mem_ctx, const DOM_SID *user_sid,
865                              void (*cont)(void *private_data, bool success,
866                                           DOM_SID *sids, size_t num_sids),
867                              void *private_data)
868 {
869         struct winbindd_domain *domain;
870         struct winbindd_request request;
871         struct gettoken_state *state;
872
873         state = TALLOC_ZERO_P(mem_ctx, struct gettoken_state);
874         if (state == NULL) {
875                 DEBUG(0, ("talloc failed\n"));
876                 cont(private_data, False, NULL, 0);
877                 return;
878         }
879
880         state->mem_ctx = mem_ctx;
881         sid_copy(&state->user_sid, user_sid);
882         state->alias_domain = find_our_domain();
883         state->local_alias_domain = find_domain_from_name( get_global_sam_name() );
884         state->builtin_domain = find_builtin_domain();
885         state->cont = cont;
886         state->private_data = private_data;
887
888         domain = find_domain_from_sid_noinit(user_sid);
889         if (domain == NULL) {
890                 DEBUG(5, ("Could not find domain from SID %s\n",
891                           sid_string_dbg(user_sid)));
892                 cont(private_data, False, NULL, 0);
893                 return;
894         }
895
896         ZERO_STRUCT(request);
897         request.cmd = WINBINDD_GETUSERDOMGROUPS;
898         sid_to_fstring(request.data.sid, user_sid);
899
900         do_async_domain(mem_ctx, domain, &request, gettoken_recvdomgroups,
901                         NULL, state);
902 }
903
904 static void gettoken_recvdomgroups(TALLOC_CTX *mem_ctx, bool success,
905                                    struct winbindd_response *response,
906                                    void *c, void *private_data)
907 {
908         struct gettoken_state *state =
909                 talloc_get_type_abort(private_data, struct gettoken_state);
910         char *sids_str;
911         
912         if (!success) {
913                 DEBUG(10, ("Could not get domain groups\n"));
914                 state->cont(state->private_data, False, NULL, 0);
915                 return;
916         }
917
918         sids_str = (char *)response->extra_data.data;
919
920         if (sids_str == NULL) {
921                 /* This could be normal if we are dealing with a
922                    local user and local groups */
923
924                 if ( !sid_check_is_in_our_domain( &state->user_sid ) ) {
925                         DEBUG(10, ("Received no domain groups\n"));
926                         state->cont(state->private_data, True, NULL, 0);
927                         return;
928                 }
929         }
930
931         state->sids = NULL;
932         state->num_sids = 0;
933
934         if (!NT_STATUS_IS_OK(add_sid_to_array(mem_ctx, &state->user_sid,
935                                               &state->sids, &state->num_sids)))
936         {
937                 DEBUG(0, ("Out of memory\n"));
938                 state->cont(state->private_data, False, NULL, 0);
939                 return;
940         }
941
942         if (sids_str && !parse_sidlist(mem_ctx, sids_str, &state->sids,
943                            &state->num_sids)) {
944                 DEBUG(0, ("Could not parse sids\n"));
945                 state->cont(state->private_data, False, NULL, 0);
946                 return;
947         }
948
949         SAFE_FREE(response->extra_data.data);
950
951         if (state->alias_domain == NULL) {
952                 DEBUG(10, ("Don't expand domain local groups\n"));
953                 state->cont(state->private_data, True, state->sids,
954                             state->num_sids);
955                 return;
956         }
957
958         winbindd_getsidaliases_async(state->alias_domain, mem_ctx,
959                                      state->sids, state->num_sids,
960                                      gettoken_recvaliases, state);
961 }
962
963 static void gettoken_recvaliases(void *private_data, bool success,
964                                  const DOM_SID *aliases,
965                                  size_t num_aliases)
966 {
967         struct gettoken_state *state = (struct gettoken_state *)private_data;
968         size_t i;
969
970         if (!success) {
971                 DEBUG(10, ("Could not receive domain local groups\n"));
972                 state->cont(state->private_data, False, NULL, 0);
973                 return;
974         }
975
976         for (i=0; i<num_aliases; i++) {
977                 if (!NT_STATUS_IS_OK(add_sid_to_array(state->mem_ctx,
978                                                       &aliases[i],
979                                                       &state->sids,
980                                                       &state->num_sids)))
981                 {
982                         DEBUG(0, ("Out of memory\n"));
983                         state->cont(state->private_data, False, NULL, 0);
984                         return;
985                 }
986         }
987
988         if (state->local_alias_domain != NULL) {
989                 struct winbindd_domain *local_domain = state->local_alias_domain;
990                 DEBUG(10, ("Expanding our own local groups\n"));
991                 state->local_alias_domain = NULL;
992                 winbindd_getsidaliases_async(local_domain, state->mem_ctx,
993                                              state->sids, state->num_sids,
994                                              gettoken_recvaliases, state);
995                 return;
996         }
997
998         if (state->builtin_domain != NULL) {
999                 struct winbindd_domain *builtin_domain = state->builtin_domain;
1000                 DEBUG(10, ("Expanding our own BUILTIN groups\n"));
1001                 state->builtin_domain = NULL;
1002                 winbindd_getsidaliases_async(builtin_domain, state->mem_ctx,
1003                                              state->sids, state->num_sids,
1004                                              gettoken_recvaliases, state);
1005                 return;
1006         }
1007
1008         state->cont(state->private_data, True, state->sids, state->num_sids);
1009 }
1010
1011 static void query_user_recv(TALLOC_CTX *mem_ctx, bool success,
1012                             struct winbindd_response *response,
1013                             void *c, void *private_data)
1014 {
1015         void (*cont)(void *priv, bool succ, const char *acct_name,
1016                      const char *full_name, const char *homedir, 
1017                      const char *shell, uint32 gid, uint32 group_rid) =
1018                 (void (*)(void *, bool, const char *, const char *,
1019                           const char *, const char *, uint32, uint32))c;
1020
1021         if (!success) {
1022                 DEBUG(5, ("Could not trigger query_user\n"));
1023                 cont(private_data, False, NULL, NULL, NULL, NULL, -1, -1);
1024                 return;
1025         }
1026
1027         if (response->result != WINBINDD_OK) {
1028                 DEBUG(5, ("query_user returned an error\n"));
1029                 cont(private_data, False, NULL, NULL, NULL, NULL, -1, -1);
1030                 return;
1031         }
1032
1033         cont(private_data, True, response->data.user_info.acct_name,
1034              response->data.user_info.full_name,
1035              response->data.user_info.homedir,
1036              response->data.user_info.shell,
1037              response->data.user_info.primary_gid,
1038              response->data.user_info.group_rid);
1039 }
1040
1041 void query_user_async(TALLOC_CTX *mem_ctx, struct winbindd_domain *domain,
1042                       const DOM_SID *sid,
1043                       void (*cont)(void *private_data, bool success,
1044                                    const char *acct_name,
1045                                    const char *full_name,
1046                                    const char *homedir,
1047                                    const char *shell,
1048                                    gid_t gid,
1049                                    uint32 group_rid),
1050                       void *private_data)
1051 {
1052         struct winbindd_request request;
1053         ZERO_STRUCT(request);
1054         request.cmd = WINBINDD_DUAL_USERINFO;
1055         sid_to_fstring(request.data.sid, sid);
1056         do_async_domain(mem_ctx, domain, &request, query_user_recv,
1057                         (void *)cont, private_data);
1058 }