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