r22542: Move over to using the _strict varients of the talloc
[metze/samba/wip.git] / source3 / nsswitch / 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 2 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, write to the Free Software
32    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
33 */
34
35 #include "includes.h"
36 #include "winbindd.h"
37
38 #undef DBGC_CLASS
39 #define DBGC_CLASS DBGC_WINBIND
40
41 struct do_async_state {
42         TALLOC_CTX *mem_ctx;
43         struct winbindd_request request;
44         struct winbindd_response response;
45         void (*cont)(TALLOC_CTX *mem_ctx,
46                      BOOL success,
47                      struct winbindd_response *response,
48                      void *c, void *private_data);
49         void *c, *private_data;
50 };
51
52 static void do_async_recv(void *private_data, BOOL success)
53 {
54         struct do_async_state *state =
55                 talloc_get_type_abort(private_data, struct do_async_state);
56
57         state->cont(state->mem_ctx, success, &state->response,
58                     state->c, state->private_data);
59 }
60
61 static void do_async(TALLOC_CTX *mem_ctx, struct winbindd_child *child,
62                      const struct winbindd_request *request,
63                      void (*cont)(TALLOC_CTX *mem_ctx, BOOL success,
64                                   struct winbindd_response *response,
65                                   void *c, void *private_data),
66                      void *c, void *private_data)
67 {
68         struct do_async_state *state;
69
70         state = TALLOC_ZERO_P(mem_ctx, struct do_async_state);
71         if (state == NULL) {
72                 DEBUG(0, ("talloc failed\n"));
73                 cont(mem_ctx, False, NULL, c, private_data);
74                 return;
75         }
76
77         state->mem_ctx = mem_ctx;
78         state->request = *request;
79         state->request.length = sizeof(state->request);
80         state->cont = cont;
81         state->c = c;
82         state->private_data = private_data;
83
84         async_request(mem_ctx, child, &state->request,
85                       &state->response, do_async_recv, state);
86 }
87
88 void do_async_domain(TALLOC_CTX *mem_ctx, struct winbindd_domain *domain,
89                      const struct winbindd_request *request,
90                      void (*cont)(TALLOC_CTX *mem_ctx, BOOL success,
91                                   struct winbindd_response *response,
92                                   void *c, void *private_data),
93                      void *c, void *private_data)
94 {
95         struct do_async_state *state;
96
97         state = TALLOC_ZERO_P(mem_ctx, struct do_async_state);
98         if (state == NULL) {
99                 DEBUG(0, ("talloc failed\n"));
100                 cont(mem_ctx, False, NULL, c, private_data);
101                 return;
102         }
103
104         state->mem_ctx = mem_ctx;
105         state->request = *request;
106         state->request.length = sizeof(state->request);
107         state->cont = cont;
108         state->c = c;
109         state->private_data = private_data;
110
111         async_domain_request(mem_ctx, domain, &state->request,
112                              &state->response, do_async_recv, state);
113 }
114
115 static void winbindd_set_mapping_recv(TALLOC_CTX *mem_ctx, BOOL success,
116                                    struct winbindd_response *response,
117                                    void *c, void *private_data)
118 {
119         void (*cont)(void *priv, BOOL succ) = (void (*)(void *, BOOL))c;
120
121         if (!success) {
122                 DEBUG(5, ("Could not trigger idmap_set_mapping\n"));
123                 cont(private_data, False);
124                 return;
125         }
126
127         if (response->result != WINBINDD_OK) {
128                 DEBUG(5, ("idmap_set_mapping returned an error\n"));
129                 cont(private_data, False);
130                 return;
131         }
132
133         cont(private_data, True);
134 }
135
136 void winbindd_set_mapping_async(TALLOC_CTX *mem_ctx, const struct id_map *map,
137                              void (*cont)(void *private_data, BOOL success),
138                              void *private_data)
139 {
140         struct winbindd_request request;
141         ZERO_STRUCT(request);
142         request.cmd = WINBINDD_DUAL_SET_MAPPING;
143         request.data.dual_idmapset.id = map->xid.id;
144         request.data.dual_idmapset.type = map->xid.type;
145         sid_to_string(request.data.dual_idmapset.sid, map->sid);
146
147         do_async(mem_ctx, idmap_child(), &request, winbindd_set_mapping_recv,
148                  (void *)cont, private_data);
149 }
150
151 enum winbindd_result winbindd_dual_set_mapping(struct winbindd_domain *domain,
152                                             struct winbindd_cli_state *state)
153 {
154         struct id_map map;
155         DOM_SID sid;
156         NTSTATUS result;
157
158         DEBUG(3, ("[%5lu]: dual_idmapset\n", (unsigned long)state->pid));
159
160         if (!string_to_sid(&sid, state->request.data.dual_idmapset.sid))
161                 return WINBINDD_ERROR;
162
163         map.sid = &sid;
164         map.xid.id = state->request.data.dual_idmapset.id;
165         map.xid.type = state->request.data.dual_idmapset.type;
166
167         result = idmap_set_mapping(&map);
168         return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
169 }
170
171 static void winbindd_set_hwm_recv(TALLOC_CTX *mem_ctx, BOOL success,
172                                    struct winbindd_response *response,
173                                    void *c, void *private_data)
174 {
175         void (*cont)(void *priv, BOOL succ) = (void (*)(void *, BOOL))c;
176
177         if (!success) {
178                 DEBUG(5, ("Could not trigger idmap_set_hwm\n"));
179                 cont(private_data, False);
180                 return;
181         }
182
183         if (response->result != WINBINDD_OK) {
184                 DEBUG(5, ("idmap_set_hwm returned an error\n"));
185                 cont(private_data, False);
186                 return;
187         }
188
189         cont(private_data, True);
190 }
191
192 void winbindd_set_hwm_async(TALLOC_CTX *mem_ctx, const struct unixid *xid,
193                              void (*cont)(void *private_data, BOOL success),
194                              void *private_data)
195 {
196         struct winbindd_request request;
197         ZERO_STRUCT(request);
198         request.cmd = WINBINDD_DUAL_SET_HWM;
199         request.data.dual_idmapset.id = xid->id;
200         request.data.dual_idmapset.type = xid->type;
201
202         do_async(mem_ctx, idmap_child(), &request, winbindd_set_hwm_recv,
203                  (void *)cont, private_data);
204 }
205
206 enum winbindd_result winbindd_dual_set_hwm(struct winbindd_domain *domain,
207                                             struct winbindd_cli_state *state)
208 {
209         struct unixid xid;
210         NTSTATUS result;
211
212         DEBUG(3, ("[%5lu]: dual_set_hwm\n", (unsigned long)state->pid));
213
214         xid.id = state->request.data.dual_idmapset.id;
215         xid.type = state->request.data.dual_idmapset.type;
216
217         switch (xid.type) {
218         case ID_TYPE_UID:
219                 result = idmap_set_uid_hwm(&xid);
220                 break;
221         case ID_TYPE_GID:
222                 result = idmap_set_gid_hwm(&xid);
223                 break;
224         default:
225                 return WINBINDD_ERROR;
226         }
227         return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
228 }
229
230 static void winbindd_sids2xids_recv(TALLOC_CTX *mem_ctx, BOOL success,
231                                struct winbindd_response *response,
232                                void *c, void *private_data)
233 {
234         void (*cont)(void *priv, BOOL succ, void *, int) =
235                 (void (*)(void *, BOOL, void *, int))c;
236
237         if (!success) {
238                 DEBUG(5, ("Could not trigger sids2xids\n"));
239                 cont(private_data, False, NULL, 0);
240                 return;
241         }
242
243         if (response->result != WINBINDD_OK) {
244                 DEBUG(5, ("sids2xids returned an error\n"));
245                 cont(private_data, False, NULL, 0);
246                 return;
247         }
248
249         cont(private_data, True, response->extra_data.data, response->length - sizeof(response));
250 }
251                          
252 void winbindd_sids2xids_async(TALLOC_CTX *mem_ctx, void *sids, int size,
253                          void (*cont)(void *private_data, BOOL success, void *data, int len),
254                          void *private_data)
255 {
256         struct winbindd_request request;
257         ZERO_STRUCT(request);
258         request.cmd = WINBINDD_DUAL_SIDS2XIDS;
259         request.extra_data.data = (char *)sids;
260         request.extra_len = size;
261         do_async(mem_ctx, idmap_child(), &request, winbindd_sids2xids_recv,
262                  (void *)cont, private_data);
263 }
264
265 enum winbindd_result winbindd_dual_sids2xids(struct winbindd_domain *domain,
266                                            struct winbindd_cli_state *state)
267 {
268         DOM_SID *sids;
269         struct unixid *xids;
270         struct id_map **ids;
271         NTSTATUS result;
272         int num, i;
273
274         DEBUG(3, ("[%5lu]: sids to unix ids\n", (unsigned long)state->pid));
275
276         sids = (DOM_SID *)state->request.extra_data.data;
277         num = state->request.extra_len / sizeof(DOM_SID);
278
279         ids = TALLOC_ZERO_ARRAY(state->mem_ctx, struct id_map *, num + 1);
280         if ( ! ids) {
281                 DEBUG(0, ("Out of memory!\n"));
282                 return WINBINDD_ERROR;
283         }
284         for (i = 0; i < num; i++) {
285                 ids[i] = TALLOC_P(ids, struct id_map);
286                 if ( ! ids[i]) {
287                         DEBUG(0, ("Out of memory!\n"));
288                         talloc_free(ids);
289                         return WINBINDD_ERROR;
290                 }
291                 ids[i]->sid = &sids[i];
292         }
293
294         result = idmap_sids_to_unixids(ids);
295
296         if (NT_STATUS_IS_OK(result)) {
297
298                 xids = SMB_MALLOC_ARRAY(struct unixid, num);
299                 if ( ! xids) {
300                         DEBUG(0, ("Out of memory!\n"));
301                         talloc_free(ids);
302                         return WINBINDD_ERROR;
303                 }
304                 
305                 for (i = 0; i < num; i++) {
306                         if (ids[i]->status == ID_MAPPED) {
307                                 xids[i].type = ids[i]->xid.type;
308                                 xids[i].id = ids[i]->xid.id;
309                         } else {
310                                 xids[i].type = -1;
311                         }
312                 }
313
314                 state->response.length = sizeof(state->response) + (sizeof(struct unixid) * num);
315                 state->response.extra_data.data = xids;
316
317         } else {
318                 DEBUG (2, ("idmap_sids_to_unixids returned an error: 0x%08x\n", NT_STATUS_V(result)));
319                 talloc_free(ids);
320                 return WINBINDD_ERROR;
321         }
322
323         talloc_free(ids);
324         return WINBINDD_OK;
325 }
326
327 static void winbindd_sid2uid_recv(TALLOC_CTX *mem_ctx, BOOL success,
328                                struct winbindd_response *response,
329                                void *c, void *private_data)
330 {
331         void (*cont)(void *priv, BOOL succ, uid_t uid) =
332                 (void (*)(void *, BOOL, uid_t))c;
333
334         if (!success) {
335                 DEBUG(5, ("Could not trigger sid2uid\n"));
336                 cont(private_data, False, 0);
337                 return;
338         }
339
340         if (response->result != WINBINDD_OK) {
341                 DEBUG(5, ("sid2uid returned an error\n"));
342                 cont(private_data, False, 0);
343                 return;
344         }
345
346         cont(private_data, True, response->data.uid);
347 }
348                          
349 void winbindd_sid2uid_async(TALLOC_CTX *mem_ctx, const DOM_SID *sid,
350                          void (*cont)(void *private_data, BOOL success, uid_t uid),
351                          void *private_data)
352 {
353         struct winbindd_request request;
354         ZERO_STRUCT(request);
355         request.cmd = WINBINDD_DUAL_SID2UID;
356         sid_to_string(request.data.dual_sid2id.sid, sid);
357         do_async(mem_ctx, idmap_child(), &request, winbindd_sid2uid_recv,
358                  (void *)cont, private_data);
359 }
360
361 enum winbindd_result winbindd_dual_sid2uid(struct winbindd_domain *domain,
362                                            struct winbindd_cli_state *state)
363 {
364         DOM_SID sid;
365         NTSTATUS result;
366
367         DEBUG(3, ("[%5lu]: sid to uid %s\n", (unsigned long)state->pid,
368                   state->request.data.dual_sid2id.sid));
369
370         if (!string_to_sid(&sid, state->request.data.dual_sid2id.sid)) {
371                 DEBUG(1, ("Could not get convert sid %s from string\n",
372                           state->request.data.dual_sid2id.sid));
373                 return WINBINDD_ERROR;
374         }
375
376         /* Find uid for this sid and return it, possibly ask the slow remote idmap */
377
378         result = idmap_sid_to_uid(&sid, &(state->response.data.uid));
379
380         return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
381 }
382
383 #if 0   /* not used */
384 static void uid2name_recv(TALLOC_CTX *mem_ctx, BOOL success,
385                           struct winbindd_response *response,
386                           void *c, void *private_data);
387
388 void winbindd_uid2name_async(TALLOC_CTX *mem_ctx, uid_t uid,
389                              void (*cont)(void *private_data, BOOL success,
390                                           const char *name),
391                              void *private_data)
392 {
393         struct winbindd_request request;
394         ZERO_STRUCT(request);
395         request.cmd = WINBINDD_DUAL_UID2NAME;
396         request.data.uid = uid;
397         do_async(mem_ctx, idmap_child(), &request, uid2name_recv,
398                  (void *)cont, private_data);
399 }
400 #endif  /* not used */
401
402 enum winbindd_result winbindd_dual_uid2name(struct winbindd_domain *domain,
403                                             struct winbindd_cli_state *state)
404 {
405         struct passwd *pw;
406
407         DEBUG(3, ("[%5lu]: uid2name %lu\n", (unsigned long)state->pid, 
408                   (unsigned long)state->request.data.uid));
409
410         pw = getpwuid(state->request.data.uid);
411         if (pw == NULL) {
412                 DEBUG(5, ("User %lu not found\n",
413                           (unsigned long)state->request.data.uid));
414                 return WINBINDD_ERROR;
415         }
416
417         fstrcpy(state->response.data.name.name, pw->pw_name);
418         return WINBINDD_OK;
419 }
420
421 #if 0   /* not used */
422 static void uid2name_recv(TALLOC_CTX *mem_ctx, BOOL success,
423                           struct winbindd_response *response,
424                           void *c, void *private_data)
425 {
426         void (*cont)(void *priv, BOOL succ, const char *name) =
427                 (void (*)(void *, BOOL, const char *))c;
428
429         if (!success) {
430                 DEBUG(5, ("Could not trigger uid2name\n"));
431                 cont(private_data, False, NULL);
432                 return;
433         }
434
435         if (response->result != WINBINDD_OK) {
436                 DEBUG(5, ("uid2name returned an error\n"));
437                 cont(private_data, False, NULL);
438                 return;
439         }
440
441         cont(private_data, True, response->data.name.name);
442 }
443
444 static void name2uid_recv(TALLOC_CTX *mem_ctx, BOOL success,
445                           struct winbindd_response *response,
446                           void *c, void *private_data);
447
448 static void winbindd_name2uid_async(TALLOC_CTX *mem_ctx, const char *name,
449                                     void (*cont)(void *private_data, BOOL success,
450                                                  uid_t uid),
451                                     void *private_data)
452 {
453         struct winbindd_request request;
454         ZERO_STRUCT(request);
455         request.cmd = WINBINDD_DUAL_NAME2UID;
456         fstrcpy(request.data.username, name);
457         do_async(mem_ctx, idmap_child(), &request, name2uid_recv,
458                  (void *)cont, private_data);
459 }
460 #endif  /* not used */
461
462 enum winbindd_result winbindd_dual_name2uid(struct winbindd_domain *domain,
463                                             struct winbindd_cli_state *state)
464 {
465         struct passwd *pw;
466
467         /* Ensure null termination */
468         state->request.data.username
469                 [sizeof(state->request.data.username)-1] = '\0';
470
471         DEBUG(3, ("[%5lu]: name2uid %s\n", (unsigned long)state->pid, 
472                   state->request.data.username));
473
474         pw = getpwnam(state->request.data.username);
475         if (pw == NULL) {
476                 return WINBINDD_ERROR;
477         }
478
479         state->response.data.uid = pw->pw_uid;
480         return WINBINDD_OK;
481 }
482
483 #if 0   /* not used */
484 static void name2uid_recv(TALLOC_CTX *mem_ctx, BOOL success,
485                           struct winbindd_response *response,
486                           void *c, void *private_data)
487 {
488         void (*cont)(void *priv, BOOL succ, uid_t uid) =
489                 (void (*)(void *, BOOL, uid_t))c;
490
491         if (!success) {
492                 DEBUG(5, ("Could not trigger name2uid\n"));
493                 cont(private_data, False, 0);
494                 return;
495         }
496
497         if (response->result != WINBINDD_OK) {
498                 DEBUG(5, ("name2uid returned an error\n"));
499                 cont(private_data, False, 0);
500                 return;
501         }
502
503         cont(private_data, True, response->data.uid);
504 }
505 #endif  /* not used */
506
507 static void winbindd_sid2gid_recv(TALLOC_CTX *mem_ctx, BOOL success,
508                                struct winbindd_response *response,
509                                void *c, void *private_data)
510 {
511         void (*cont)(void *priv, BOOL succ, gid_t gid) =
512                 (void (*)(void *, BOOL, gid_t))c;
513
514         if (!success) {
515                 DEBUG(5, ("Could not trigger sid2gid\n"));
516                 cont(private_data, False, 0);
517                 return;
518         }
519
520         if (response->result != WINBINDD_OK) {
521                 DEBUG(5, ("sid2gid returned an error\n"));
522                 cont(private_data, False, 0);
523                 return;
524         }
525
526         cont(private_data, True, response->data.gid);
527 }
528                          
529 void winbindd_sid2gid_async(TALLOC_CTX *mem_ctx, const DOM_SID *sid,
530                          void (*cont)(void *private_data, BOOL success, gid_t gid),
531                          void *private_data)
532 {
533         struct winbindd_request request;
534         ZERO_STRUCT(request);
535         request.cmd = WINBINDD_DUAL_SID2GID;
536         sid_to_string(request.data.dual_sid2id.sid, sid);
537
538         DEBUG(7,("winbindd_sid2gid_async: Resolving %s to a gid\n", 
539                 request.data.dual_sid2id.sid));
540
541         do_async(mem_ctx, idmap_child(), &request, winbindd_sid2gid_recv,
542                  (void *)cont, private_data);
543 }
544
545 enum winbindd_result winbindd_dual_sid2gid(struct winbindd_domain *domain,
546                                            struct winbindd_cli_state *state)
547 {
548         DOM_SID sid;
549         NTSTATUS result;
550
551         DEBUG(3, ("[%5lu]: sid to gid %s\n", (unsigned long)state->pid,
552                   state->request.data.dual_sid2id.sid));
553
554         if (!string_to_sid(&sid, state->request.data.dual_sid2id.sid)) {
555                 DEBUG(1, ("Could not get convert sid %s from string\n",
556                           state->request.data.dual_sid2id.sid));
557                 return WINBINDD_ERROR;
558         }
559
560         /* Find gid for this sid and return it, possibly ask the slow remote idmap */
561
562         result = idmap_sid_to_gid(&sid, &state->response.data.gid);
563         
564         DEBUG(10, ("winbindd_dual_sid2gid: 0x%08x - %s - %u\n", NT_STATUS_V(result), sid_string_static(&sid), state->response.data.gid));
565
566         return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
567 }
568
569 static void gid2name_recv(TALLOC_CTX *mem_ctx, BOOL success,
570                           struct winbindd_response *response,
571                           void *c, void *private_data)
572 {
573         void (*cont)(void *priv, BOOL succ, const char *name) =
574                 (void (*)(void *, BOOL, const char *))c;
575
576         if (!success) {
577                 DEBUG(5, ("Could not trigger gid2name\n"));
578                 cont(private_data, False, NULL);
579                 return;
580         }
581
582         if (response->result != WINBINDD_OK) {
583                 DEBUG(5, ("gid2name returned an error\n"));
584                 cont(private_data, False, NULL);
585                 return;
586         }
587
588         cont(private_data, True, response->data.name.name);
589 }
590
591 void winbindd_gid2name_async(TALLOC_CTX *mem_ctx, gid_t gid,
592                              void (*cont)(void *private_data, BOOL success,
593                                           const char *name),
594                              void *private_data)
595 {
596         struct winbindd_request request;
597         ZERO_STRUCT(request);
598         request.cmd = WINBINDD_DUAL_GID2NAME;
599         request.data.gid = gid;
600         do_async(mem_ctx, idmap_child(), &request, gid2name_recv,
601                  (void *)cont, private_data);
602 }
603
604 enum winbindd_result winbindd_dual_gid2name(struct winbindd_domain *domain,
605                                             struct winbindd_cli_state *state)
606 {
607         struct group *gr;
608
609         DEBUG(3, ("[%5lu]: gid2name %lu\n", (unsigned long)state->pid, 
610                   (unsigned long)state->request.data.gid));
611
612         gr = getgrgid(state->request.data.gid);
613         if (gr == NULL)
614                 return WINBINDD_ERROR;
615
616         fstrcpy(state->response.data.name.name, gr->gr_name);
617         return WINBINDD_OK;
618 }
619
620 #if 0   /* not used */
621 static void name2gid_recv(TALLOC_CTX *mem_ctx, BOOL success,
622                           struct winbindd_response *response,
623                           void *c, void *private_data);
624
625 static void winbindd_name2gid_async(TALLOC_CTX *mem_ctx, const char *name,
626                                     void (*cont)(void *private_data, BOOL success,
627                                                  gid_t gid),
628                                     void *private_data)
629 {
630         struct winbindd_request request;
631         ZERO_STRUCT(request);
632         request.cmd = WINBINDD_DUAL_NAME2GID;
633         fstrcpy(request.data.groupname, name);
634         do_async(mem_ctx, idmap_child(), &request, name2gid_recv,
635                  (void *)cont, private_data);
636 }
637 #endif  /* not used */
638
639 enum winbindd_result winbindd_dual_name2gid(struct winbindd_domain *domain,
640                                             struct winbindd_cli_state *state)
641 {
642         struct group *gr;
643
644         /* Ensure null termination */
645         state->request.data.groupname
646                 [sizeof(state->request.data.groupname)-1] = '\0';
647
648         DEBUG(3, ("[%5lu]: name2gid %s\n", (unsigned long)state->pid, 
649                   state->request.data.groupname));
650
651         gr = getgrnam(state->request.data.groupname);
652         if (gr == NULL) {
653                 return WINBINDD_ERROR;
654         }
655
656         state->response.data.gid = gr->gr_gid;
657         return WINBINDD_OK;
658 }
659
660 #if 0   /* not used */
661 static void name2gid_recv(TALLOC_CTX *mem_ctx, BOOL success,
662                           struct winbindd_response *response,
663                           void *c, void *private_data)
664 {
665         void (*cont)(void *priv, BOOL succ, gid_t gid) =
666                 (void (*)(void *, BOOL, gid_t))c;
667
668         if (!success) {
669                 DEBUG(5, ("Could not trigger name2gid\n"));
670                 cont(private_data, False, 0);
671                 return;
672         }
673
674         if (response->result != WINBINDD_OK) {
675                 DEBUG(5, ("name2gid returned an error\n"));
676                 cont(private_data, False, 0);
677                 return;
678         }
679
680         cont(private_data, True, response->data.gid);
681 }
682 #endif  /* not used */
683
684 static void lookupsid_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, const char *dom_name,
689                      const char *name, enum lsa_SidType type) =
690                 (void (*)(void *, BOOL, const char *, const char *,
691                           enum lsa_SidType))c;
692
693         if (!success) {
694                 DEBUG(5, ("Could not trigger lookupsid\n"));
695                 cont(private_data, False, NULL, NULL, SID_NAME_UNKNOWN);
696                 return;
697         }
698
699         if (response->result != WINBINDD_OK) {
700                 DEBUG(5, ("lookupsid returned an error\n"));
701                 cont(private_data, False, NULL, NULL, SID_NAME_UNKNOWN);
702                 return;
703         }
704
705         cont(private_data, True, response->data.name.dom_name,
706              response->data.name.name,
707              (enum lsa_SidType)response->data.name.type);
708 }
709
710 void winbindd_lookupsid_async(TALLOC_CTX *mem_ctx, const DOM_SID *sid,
711                               void (*cont)(void *private_data, BOOL success,
712                                            const char *dom_name,
713                                            const char *name,
714                                            enum lsa_SidType type),
715                               void *private_data)
716 {
717         struct winbindd_domain *domain;
718         struct winbindd_request request;
719
720         domain = find_lookup_domain_from_sid(sid);
721         if (domain == NULL) {
722                 DEBUG(5, ("Could not find domain for sid %s\n",
723                           sid_string_static(sid)));
724                 cont(private_data, False, NULL, NULL, SID_NAME_UNKNOWN);
725                 return;
726         }
727
728         ZERO_STRUCT(request);
729         request.cmd = WINBINDD_LOOKUPSID;
730         fstrcpy(request.data.sid, sid_string_static(sid));
731
732         do_async_domain(mem_ctx, domain, &request, lookupsid_recv,
733                         (void *)cont, private_data);
734 }
735
736 enum winbindd_result winbindd_dual_lookupsid(struct winbindd_domain *domain,
737                                              struct winbindd_cli_state *state)
738 {
739         enum lsa_SidType type;
740         DOM_SID sid;
741         char *name;
742         char *dom_name;
743
744         /* Ensure null termination */
745         state->request.data.sid[sizeof(state->request.data.sid)-1]='\0';
746
747         DEBUG(3, ("[%5lu]: lookupsid %s\n", (unsigned long)state->pid, 
748                   state->request.data.sid));
749
750         /* Lookup sid from PDC using lsa_lookup_sids() */
751
752         if (!string_to_sid(&sid, state->request.data.sid)) {
753                 DEBUG(5, ("%s not a SID\n", state->request.data.sid));
754                 return WINBINDD_ERROR;
755         }
756
757         /* Lookup the sid */
758
759         if (!winbindd_lookup_name_by_sid(state->mem_ctx, &sid, &dom_name, &name,
760                                          &type)) {
761                 TALLOC_FREE(dom_name);
762                 TALLOC_FREE(name);
763                 return WINBINDD_ERROR;
764         }
765
766         fstrcpy(state->response.data.name.dom_name, dom_name);
767         fstrcpy(state->response.data.name.name, name);
768         state->response.data.name.type = type;
769
770         TALLOC_FREE(dom_name);
771         TALLOC_FREE(name);
772         return WINBINDD_OK;
773 }
774
775 /********************************************************************
776  This is the second callback after contacting the forest root
777 ********************************************************************/
778
779 static void lookupname_recv2(TALLOC_CTX *mem_ctx, BOOL success,
780                             struct winbindd_response *response,
781                             void *c, void *private_data)
782 {
783         void (*cont)(void *priv, BOOL succ, const DOM_SID *sid,
784                      enum lsa_SidType type) =
785                 (void (*)(void *, BOOL, const DOM_SID *, enum lsa_SidType))c;
786         DOM_SID sid;
787
788         if (!success) {
789                 DEBUG(5, ("Could not trigger lookup_name\n"));
790                 cont(private_data, False, NULL, SID_NAME_UNKNOWN);
791                 return;
792         }
793
794         if (response->result != WINBINDD_OK) {
795                 DEBUG(5, ("lookup_name returned an error\n"));
796                 cont(private_data, False, NULL, SID_NAME_UNKNOWN);
797                 return;
798         }
799
800         if (!string_to_sid(&sid, response->data.sid.sid)) {
801                 DEBUG(0, ("Could not convert string %s to sid\n",
802                           response->data.sid.sid));
803                 cont(private_data, False, NULL, SID_NAME_UNKNOWN);
804                 return;
805         }
806
807         cont(private_data, True, &sid,
808              (enum lsa_SidType)response->data.sid.type);
809 }
810
811 /********************************************************************
812  This is the first callback after contacting our own domain 
813 ********************************************************************/
814
815 static void lookupname_recv(TALLOC_CTX *mem_ctx, BOOL success,
816                             struct winbindd_response *response,
817                             void *c, void *private_data)
818 {
819         void (*cont)(void *priv, BOOL succ, const DOM_SID *sid,
820                      enum lsa_SidType type) =
821                 (void (*)(void *, BOOL, const DOM_SID *, enum lsa_SidType))c;
822         DOM_SID sid;
823
824         if (!success) {
825                 DEBUG(5, ("lookupname_recv: lookup_name() failed!\n"));
826                 cont(private_data, False, NULL, SID_NAME_UNKNOWN);
827                 return;
828         }
829
830         if (response->result != WINBINDD_OK) {
831                 /* Try again using the forest root */
832                 struct winbindd_domain *root_domain = find_root_domain();
833                 struct winbindd_cli_state *state = (struct winbindd_cli_state*)private_data;            
834                 struct winbindd_request request;                
835                 char *name_domain, *name_account;
836                 
837                 if ( !root_domain ) {
838                         DEBUG(5,("lookupname_recv: unable to determine forest root\n"));
839                         cont(private_data, False, NULL, SID_NAME_UNKNOWN);
840                         return;
841                 }
842
843                 name_domain  = state->request.data.name.dom_name;
844                 name_account = state->request.data.name.name;   
845
846                 ZERO_STRUCT(request);
847                 request.cmd = WINBINDD_LOOKUPNAME;
848                 fstrcpy(request.data.name.dom_name, name_domain);
849                 fstrcpy(request.data.name.name, name_account);
850
851                 do_async_domain(mem_ctx, root_domain, &request, lookupname_recv2,
852                                 (void *)cont, private_data);
853
854                 return;
855         }
856
857         if (!string_to_sid(&sid, response->data.sid.sid)) {
858                 DEBUG(0, ("Could not convert string %s to sid\n",
859                           response->data.sid.sid));
860                 cont(private_data, False, NULL, SID_NAME_UNKNOWN);
861                 return;
862         }
863
864         cont(private_data, True, &sid,
865              (enum lsa_SidType)response->data.sid.type);
866 }
867
868 /********************************************************************
869  The lookup name call first contacts a DC in its own domain
870  and fallbacks to contact a DC in the forest in our domain doesn't
871  know the name.
872 ********************************************************************/
873
874 void winbindd_lookupname_async(TALLOC_CTX *mem_ctx,
875                                const char *dom_name, const char *name,
876                                void (*cont)(void *private_data, BOOL success,
877                                             const DOM_SID *sid,
878                                             enum lsa_SidType type),
879                                void *private_data)
880 {
881         struct winbindd_request request;
882         struct winbindd_domain *domain;
883
884         if ( (domain = find_lookup_domain_from_name(dom_name)) == NULL ) {
885                 DEBUG(5, ("Could not find domain for name %s\n", dom_name));
886                 cont(private_data, False, NULL, SID_NAME_UNKNOWN);
887                 return;
888         }
889
890         ZERO_STRUCT(request);
891         request.cmd = WINBINDD_LOOKUPNAME;
892         fstrcpy(request.data.name.dom_name, dom_name);
893         fstrcpy(request.data.name.name, name);
894
895         do_async_domain(mem_ctx, domain, &request, lookupname_recv,
896                         (void *)cont, private_data);
897 }
898
899 enum winbindd_result winbindd_dual_lookupname(struct winbindd_domain *domain,
900                                               struct winbindd_cli_state *state)
901 {
902         enum lsa_SidType type;
903         char *name_domain, *name_user;
904         DOM_SID sid;
905         char *p;
906
907         /* Ensure null termination */
908         state->request.data.name.dom_name[sizeof(state->request.data.name.dom_name)-1]='\0';
909
910         /* Ensure null termination */
911         state->request.data.name.name[sizeof(state->request.data.name.name)-1]='\0';
912
913         /* cope with the name being a fully qualified name */
914         p = strstr(state->request.data.name.name, lp_winbind_separator());
915         if (p) {
916                 *p = 0;
917                 name_domain = state->request.data.name.name;
918                 name_user = p+1;
919         } else {
920                 name_domain = state->request.data.name.dom_name;
921                 name_user = state->request.data.name.name;
922         }
923
924         DEBUG(3, ("[%5lu]: lookupname %s%s%s\n", (unsigned long)state->pid,
925                   name_domain, lp_winbind_separator(), name_user));
926
927         /* Lookup name from DC using lsa_lookup_names() */
928         if (!winbindd_lookup_sid_by_name(state->mem_ctx, domain, name_domain,
929                                          name_user, &sid, &type)) {
930                 return WINBINDD_ERROR;
931         }
932
933         sid_to_string(state->response.data.sid.sid, &sid);
934         state->response.data.sid.type = type;
935
936         return WINBINDD_OK;
937 }
938
939 BOOL print_sidlist(TALLOC_CTX *mem_ctx, const DOM_SID *sids,
940                    size_t num_sids, char **result, ssize_t *len)
941 {
942         size_t i;
943         size_t buflen = 0;
944
945         *len = 0;
946         *result = NULL;
947         for (i=0; i<num_sids; i++) {
948                 sprintf_append(mem_ctx, result, len, &buflen,
949                                "%s\n", sid_string_static(&sids[i]));
950         }
951
952         if ((num_sids != 0) && (*result == NULL)) {
953                 return False;
954         }
955
956         return True;
957 }
958
959 static BOOL parse_sidlist(TALLOC_CTX *mem_ctx, char *sidstr,
960                           DOM_SID **sids, size_t *num_sids)
961 {
962         char *p, *q;
963
964         p = sidstr;
965         if (p == NULL)
966                 return False;
967
968         while (p[0] != '\0') {
969                 DOM_SID sid;
970                 q = strchr(p, '\n');
971                 if (q == NULL) {
972                         DEBUG(0, ("Got invalid sidstr: %s\n", p));
973                         return False;
974                 }
975                 *q = '\0';
976                 q += 1;
977                 if (!string_to_sid(&sid, p)) {
978                         DEBUG(0, ("Could not parse sid %s\n", p));
979                         return False;
980                 }
981                 if (!add_sid_to_array(mem_ctx, &sid, sids, num_sids)) {
982                         return False;
983                 }
984                 p = q;
985         }
986         return True;
987 }
988
989 static BOOL parse_ridlist(TALLOC_CTX *mem_ctx, char *ridstr,
990                           uint32 **rids, size_t *num_rids)
991 {
992         char *p;
993
994         p = ridstr;
995         if (p == NULL)
996                 return False;
997
998         while (p[0] != '\0') {
999                 uint32 rid;
1000                 char *q;
1001                 rid = strtoul(p, &q, 10);
1002                 if (*q != '\n') {
1003                         DEBUG(0, ("Got invalid ridstr: %s\n", p));
1004                         return False;
1005                 }
1006                 p = q+1;
1007                 ADD_TO_ARRAY(mem_ctx, uint32, rid, rids, num_rids);
1008         }
1009         return True;
1010 }
1011
1012 enum winbindd_result winbindd_dual_lookuprids(struct winbindd_domain *domain,
1013                                               struct winbindd_cli_state *state)
1014 {
1015         uint32 *rids = NULL;
1016         size_t i, buflen, num_rids = 0;
1017         ssize_t len;
1018         DOM_SID domain_sid;
1019         char *domain_name;
1020         char **names;
1021         enum lsa_SidType *types;
1022         NTSTATUS status;
1023         char *result;
1024
1025         DEBUG(10, ("Looking up RIDs for domain %s (%s)\n",
1026                    state->request.domain_name,
1027                    state->request.data.sid));
1028
1029         if (!parse_ridlist(state->mem_ctx, state->request.extra_data.data,
1030                            &rids, &num_rids)) {
1031                 DEBUG(5, ("Could not parse ridlist\n"));
1032                 return WINBINDD_ERROR;
1033         }
1034
1035         if (!string_to_sid(&domain_sid, state->request.data.sid)) {
1036                 DEBUG(5, ("Could not parse domain sid %s\n",
1037                           state->request.data.sid));
1038                 return WINBINDD_ERROR;
1039         }
1040
1041         status = domain->methods->rids_to_names(domain, state->mem_ctx,
1042                                                 &domain_sid, rids, num_rids,
1043                                                 &domain_name,
1044                                                 &names, &types);
1045
1046         if (!NT_STATUS_IS_OK(status) &&
1047             !NT_STATUS_EQUAL(status, STATUS_SOME_UNMAPPED)) {
1048                 return WINBINDD_ERROR;
1049         }
1050
1051         len = 0;
1052         buflen = 0;
1053         result = NULL;
1054
1055         for (i=0; i<num_rids; i++) {
1056                 sprintf_append(state->mem_ctx, &result, &len, &buflen,
1057                                "%d %s\n", types[i], names[i]);
1058         }
1059
1060         fstrcpy(state->response.data.domain_name, domain_name);
1061
1062         if (result != NULL) {
1063                 state->response.extra_data.data = SMB_STRDUP(result);
1064                 if (!state->response.extra_data.data) {
1065                         return WINBINDD_ERROR;
1066                 }
1067                 state->response.length += len+1;
1068         }
1069
1070         return WINBINDD_OK;
1071 }
1072
1073 static void getsidaliases_recv(TALLOC_CTX *mem_ctx, BOOL success,
1074                                struct winbindd_response *response,
1075                                void *c, void *private_data)
1076 {
1077         void (*cont)(void *priv, BOOL succ,
1078                      DOM_SID *aliases, size_t num_aliases) =
1079                 (void (*)(void *, BOOL, DOM_SID *, size_t))c;
1080         char *aliases_str;
1081         DOM_SID *sids = NULL;
1082         size_t num_sids = 0;
1083
1084         if (!success) {
1085                 DEBUG(5, ("Could not trigger getsidaliases\n"));
1086                 cont(private_data, success, NULL, 0);
1087                 return;
1088         }
1089
1090         if (response->result != WINBINDD_OK) {
1091                 DEBUG(5, ("getsidaliases returned an error\n"));
1092                 cont(private_data, False, NULL, 0);
1093                 return;
1094         }
1095
1096         aliases_str = (char *)response->extra_data.data;
1097
1098         if (aliases_str == NULL) {
1099                 DEBUG(10, ("getsidaliases return 0 SIDs\n"));
1100                 cont(private_data, True, NULL, 0);
1101                 return;
1102         }
1103
1104         if (!parse_sidlist(mem_ctx, aliases_str, &sids, &num_sids)) {
1105                 DEBUG(0, ("Could not parse sids\n"));
1106                 cont(private_data, False, NULL, 0);
1107                 return;
1108         }
1109
1110         SAFE_FREE(response->extra_data.data);
1111
1112         cont(private_data, True, sids, num_sids);
1113 }
1114
1115 void winbindd_getsidaliases_async(struct winbindd_domain *domain,
1116                                   TALLOC_CTX *mem_ctx,
1117                                   const DOM_SID *sids, size_t num_sids,
1118                                   void (*cont)(void *private_data,
1119                                                BOOL success,
1120                                                const DOM_SID *aliases,
1121                                                size_t num_aliases),
1122                                   void *private_data)
1123 {
1124         struct winbindd_request request;
1125         char *sidstr = NULL;
1126         ssize_t len;
1127
1128         if (num_sids == 0) {
1129                 cont(private_data, True, NULL, 0);
1130                 return;
1131         }
1132
1133         if (!print_sidlist(mem_ctx, sids, num_sids, &sidstr, &len)) {
1134                 cont(private_data, False, NULL, 0);
1135                 return;
1136         }
1137
1138         ZERO_STRUCT(request);
1139         request.cmd = WINBINDD_DUAL_GETSIDALIASES;
1140         request.extra_len = len;
1141         request.extra_data.data = sidstr;
1142
1143         do_async_domain(mem_ctx, domain, &request, getsidaliases_recv,
1144                         (void *)cont, private_data);
1145 }
1146
1147 enum winbindd_result winbindd_dual_getsidaliases(struct winbindd_domain *domain,
1148                                                  struct winbindd_cli_state *state)
1149 {
1150         DOM_SID *sids = NULL;
1151         size_t num_sids = 0;
1152         char *sidstr = NULL;
1153         ssize_t len;
1154         size_t i;
1155         uint32 num_aliases;
1156         uint32 *alias_rids;
1157         NTSTATUS result;
1158
1159         DEBUG(3, ("[%5lu]: getsidaliases\n", (unsigned long)state->pid));
1160
1161         sidstr = state->request.extra_data.data;
1162         if (sidstr == NULL) {
1163                 sidstr = talloc_strdup(state->mem_ctx, "\n"); /* No SID */
1164                 if (!sidstr) {
1165                         DEBUG(0, ("Out of memory\n"));
1166                         return WINBINDD_ERROR;
1167                 }
1168         }
1169
1170         DEBUG(10, ("Sidlist: %s\n", sidstr));
1171
1172         if (!parse_sidlist(state->mem_ctx, sidstr, &sids, &num_sids)) {
1173                 DEBUG(0, ("Could not parse SID list: %s\n", sidstr));
1174                 return WINBINDD_ERROR;
1175         }
1176
1177         num_aliases = 0;
1178         alias_rids = NULL;
1179
1180         result = domain->methods->lookup_useraliases(domain,
1181                                                      state->mem_ctx,
1182                                                      num_sids, sids,
1183                                                      &num_aliases,
1184                                                      &alias_rids);
1185
1186         if (!NT_STATUS_IS_OK(result)) {
1187                 DEBUG(3, ("Could not lookup_useraliases: %s\n",
1188                           nt_errstr(result)));
1189                 return WINBINDD_ERROR;
1190         }
1191
1192         num_sids = 0;
1193         sids = NULL;
1194         sidstr = NULL;
1195
1196         DEBUG(10, ("Got %d aliases\n", num_aliases));
1197
1198         for (i=0; i<num_aliases; i++) {
1199                 DOM_SID sid;
1200                 DEBUGADD(10, (" rid %d\n", alias_rids[i]));
1201                 sid_copy(&sid, &domain->sid);
1202                 sid_append_rid(&sid, alias_rids[i]);
1203                 if (!add_sid_to_array(state->mem_ctx, &sid, &sids, &num_sids)) {
1204                         return WINBINDD_ERROR;
1205                 }
1206         }
1207
1208
1209         if (!print_sidlist(state->mem_ctx, sids, num_sids, &sidstr, &len)) {
1210                 DEBUG(0, ("Could not print_sidlist\n"));
1211                 state->response.extra_data.data = NULL;
1212                 return WINBINDD_ERROR;
1213         }
1214
1215         state->response.extra_data.data = NULL;
1216
1217         if (sidstr) {
1218                 state->response.extra_data.data = SMB_STRDUP(sidstr);
1219                 if (!state->response.extra_data.data) {
1220                         DEBUG(0, ("Out of memory\n"));
1221                         return WINBINDD_ERROR;
1222                 }
1223                 DEBUG(10, ("aliases_list: %s\n",
1224                            (char *)state->response.extra_data.data));
1225                 state->response.length += len+1;
1226         }
1227         
1228         return WINBINDD_OK;
1229 }
1230
1231 struct gettoken_state {
1232         TALLOC_CTX *mem_ctx;
1233         DOM_SID user_sid;
1234         struct winbindd_domain *alias_domain;
1235         struct winbindd_domain *local_alias_domain;
1236         struct winbindd_domain *builtin_domain;
1237         DOM_SID *sids;
1238         size_t num_sids;
1239         void (*cont)(void *private_data, BOOL success, DOM_SID *sids, size_t num_sids);
1240         void *private_data;
1241 };
1242
1243 static void gettoken_recvdomgroups(TALLOC_CTX *mem_ctx, BOOL success,
1244                                    struct winbindd_response *response,
1245                                    void *c, void *private_data);
1246 static void gettoken_recvaliases(void *private_data, BOOL success,
1247                                  const DOM_SID *aliases,
1248                                  size_t num_aliases);
1249                                  
1250
1251 void winbindd_gettoken_async(TALLOC_CTX *mem_ctx, const DOM_SID *user_sid,
1252                              void (*cont)(void *private_data, BOOL success,
1253                                           DOM_SID *sids, size_t num_sids),
1254                              void *private_data)
1255 {
1256         struct winbindd_domain *domain;
1257         struct winbindd_request request;
1258         struct gettoken_state *state;
1259
1260         state = TALLOC_ZERO_P(mem_ctx, struct gettoken_state);
1261         if (state == NULL) {
1262                 DEBUG(0, ("talloc failed\n"));
1263                 cont(private_data, False, NULL, 0);
1264                 return;
1265         }
1266
1267         state->mem_ctx = mem_ctx;
1268         sid_copy(&state->user_sid, user_sid);
1269         state->alias_domain = find_our_domain();
1270         state->local_alias_domain = find_domain_from_name( get_global_sam_name() );
1271         state->builtin_domain = find_builtin_domain();
1272         state->cont = cont;
1273         state->private_data = private_data;
1274
1275         domain = find_domain_from_sid_noinit(user_sid);
1276         if (domain == NULL) {
1277                 DEBUG(5, ("Could not find domain from SID %s\n",
1278                           sid_string_static(user_sid)));
1279                 cont(private_data, False, NULL, 0);
1280                 return;
1281         }
1282
1283         ZERO_STRUCT(request);
1284         request.cmd = WINBINDD_GETUSERDOMGROUPS;
1285         fstrcpy(request.data.sid, sid_string_static(user_sid));
1286
1287         do_async_domain(mem_ctx, domain, &request, gettoken_recvdomgroups,
1288                         NULL, state);
1289 }
1290
1291 static void gettoken_recvdomgroups(TALLOC_CTX *mem_ctx, BOOL success,
1292                                    struct winbindd_response *response,
1293                                    void *c, void *private_data)
1294 {
1295         struct gettoken_state *state =
1296                 talloc_get_type_abort(private_data, struct gettoken_state);
1297         char *sids_str;
1298         
1299         if (!success) {
1300                 DEBUG(10, ("Could not get domain groups\n"));
1301                 state->cont(state->private_data, False, NULL, 0);
1302                 return;
1303         }
1304
1305         sids_str = (char *)response->extra_data.data;
1306
1307         if (sids_str == NULL) {
1308                 /* This could be normal if we are dealing with a
1309                    local user and local groups */
1310
1311                 if ( !sid_check_is_in_our_domain( &state->user_sid ) ) {
1312                         DEBUG(10, ("Received no domain groups\n"));
1313                         state->cont(state->private_data, True, NULL, 0);
1314                         return;
1315                 }
1316         }
1317
1318         state->sids = NULL;
1319         state->num_sids = 0;
1320
1321         if (!add_sid_to_array(mem_ctx, &state->user_sid, &state->sids,
1322                          &state->num_sids)) {
1323                 DEBUG(0, ("Out of memory\n"));
1324                 state->cont(state->private_data, False, NULL, 0);
1325                 return;
1326         }
1327
1328         if (sids_str && !parse_sidlist(mem_ctx, sids_str, &state->sids,
1329                            &state->num_sids)) {
1330                 DEBUG(0, ("Could not parse sids\n"));
1331                 state->cont(state->private_data, False, NULL, 0);
1332                 return;
1333         }
1334
1335         SAFE_FREE(response->extra_data.data);
1336
1337         if (state->alias_domain == NULL) {
1338                 DEBUG(10, ("Don't expand domain local groups\n"));
1339                 state->cont(state->private_data, True, state->sids,
1340                             state->num_sids);
1341                 return;
1342         }
1343
1344         winbindd_getsidaliases_async(state->alias_domain, mem_ctx,
1345                                      state->sids, state->num_sids,
1346                                      gettoken_recvaliases, state);
1347 }
1348
1349 static void gettoken_recvaliases(void *private_data, BOOL success,
1350                                  const DOM_SID *aliases,
1351                                  size_t num_aliases)
1352 {
1353         struct gettoken_state *state = (struct gettoken_state *)private_data;
1354         size_t i;
1355
1356         if (!success) {
1357                 DEBUG(10, ("Could not receive domain local groups\n"));
1358                 state->cont(state->private_data, False, NULL, 0);
1359                 return;
1360         }
1361
1362         for (i=0; i<num_aliases; i++) {
1363                 if (!add_sid_to_array(state->mem_ctx, &aliases[i],
1364                                  &state->sids, &state->num_sids)) {
1365                         DEBUG(0, ("Out of memory\n"));
1366                         state->cont(state->private_data, False, NULL, 0);
1367                         return;
1368                 }
1369         }
1370
1371         if (state->local_alias_domain != NULL) {
1372                 struct winbindd_domain *local_domain = state->local_alias_domain;
1373                 DEBUG(10, ("Expanding our own local groups\n"));
1374                 state->local_alias_domain = NULL;
1375                 winbindd_getsidaliases_async(local_domain, state->mem_ctx,
1376                                              state->sids, state->num_sids,
1377                                              gettoken_recvaliases, state);
1378                 return;
1379         }
1380
1381         if (state->builtin_domain != NULL) {
1382                 struct winbindd_domain *builtin_domain = state->builtin_domain;
1383                 DEBUG(10, ("Expanding our own BUILTIN groups\n"));
1384                 state->builtin_domain = NULL;
1385                 winbindd_getsidaliases_async(builtin_domain, state->mem_ctx,
1386                                              state->sids, state->num_sids,
1387                                              gettoken_recvaliases, state);
1388                 return;
1389         }
1390
1391         state->cont(state->private_data, True, state->sids, state->num_sids);
1392 }
1393
1394 static void query_user_recv(TALLOC_CTX *mem_ctx, BOOL success,
1395                             struct winbindd_response *response,
1396                             void *c, void *private_data)
1397 {
1398         void (*cont)(void *priv, BOOL succ, const char *acct_name,
1399                      const char *full_name, const char *homedir, 
1400                      const char *shell, uint32 gid, uint32 group_rid) =
1401                 (void (*)(void *, BOOL, const char *, const char *,
1402                           const char *, const char *, uint32, uint32))c;
1403
1404         if (!success) {
1405                 DEBUG(5, ("Could not trigger query_user\n"));
1406                 cont(private_data, False, NULL, NULL, NULL, NULL, -1, -1);
1407                 return;
1408         }
1409
1410         cont(private_data, True, response->data.user_info.acct_name,
1411              response->data.user_info.full_name,
1412              response->data.user_info.homedir,
1413              response->data.user_info.shell,
1414              response->data.user_info.primary_gid,
1415              response->data.user_info.group_rid);
1416 }
1417
1418 void query_user_async(TALLOC_CTX *mem_ctx, struct winbindd_domain *domain,
1419                       const DOM_SID *sid,
1420                       void (*cont)(void *private_data, BOOL success,
1421                                    const char *acct_name,
1422                                    const char *full_name,
1423                                    const char *homedir,
1424                                    const char *shell,
1425                                    gid_t gid,
1426                                    uint32 group_rid),
1427                       void *private_data)
1428 {
1429         struct winbindd_request request;
1430         ZERO_STRUCT(request);
1431         request.cmd = WINBINDD_DUAL_USERINFO;
1432         sid_to_string(request.data.sid, sid);
1433         do_async_domain(mem_ctx, domain, &request, query_user_recv,
1434                         (void *)cont, private_data);
1435 }
1436
1437 /* The following uid2sid/gid2sid functions has been contributed by
1438  * Keith Reynolds <Keith.Reynolds@centrify.com> */
1439
1440 static void winbindd_uid2sid_recv(TALLOC_CTX *mem_ctx, BOOL success,
1441                                   struct winbindd_response *response,
1442                                   void *c, void *private_data)
1443 {
1444         void (*cont)(void *priv, BOOL succ, const char *sid) =
1445                 (void (*)(void *, BOOL, const char *))c;
1446
1447         if (!success) {
1448                 DEBUG(5, ("Could not trigger uid2sid\n"));
1449                 cont(private_data, False, NULL);
1450                 return;
1451         }
1452
1453         if (response->result != WINBINDD_OK) {
1454                 DEBUG(5, ("uid2sid returned an error\n"));
1455                 cont(private_data, False, NULL);
1456                 return;
1457         }
1458
1459         cont(private_data, True, response->data.sid.sid);
1460 }
1461
1462 void winbindd_uid2sid_async(TALLOC_CTX *mem_ctx, uid_t uid,
1463                             void (*cont)(void *private_data, BOOL success, const char *sid),
1464                             void *private_data)
1465 {
1466         struct winbindd_request request;
1467
1468         ZERO_STRUCT(request);
1469         request.cmd = WINBINDD_DUAL_UID2SID;
1470         request.data.uid = uid;
1471         do_async(mem_ctx, idmap_child(), &request, winbindd_uid2sid_recv,
1472                  (void *)cont, private_data);
1473 }
1474
1475 enum winbindd_result winbindd_dual_uid2sid(struct winbindd_domain *domain,
1476                                            struct winbindd_cli_state *state)
1477 {
1478         DOM_SID sid;
1479         NTSTATUS result;
1480
1481         DEBUG(3,("[%5lu]: uid to sid %lu\n",
1482                  (unsigned long)state->pid,
1483                  (unsigned long) state->request.data.uid));
1484
1485         /* Find sid for this uid and return it, possibly ask the slow remote idmap */
1486         result = idmap_uid_to_sid(&sid, state->request.data.uid);
1487
1488         if (NT_STATUS_IS_OK(result)) {
1489                 sid_to_string(state->response.data.sid.sid, &sid);
1490                 state->response.data.sid.type = SID_NAME_USER;
1491                 return WINBINDD_OK;
1492         }
1493
1494         return WINBINDD_ERROR;
1495 }
1496
1497 static void winbindd_gid2sid_recv(TALLOC_CTX *mem_ctx, BOOL success,
1498                                   struct winbindd_response *response,
1499                                   void *c, void *private_data)
1500 {
1501         void (*cont)(void *priv, BOOL succ, const char *sid) =
1502                 (void (*)(void *, BOOL, const char *))c;
1503
1504         if (!success) {
1505                 DEBUG(5, ("Could not trigger gid2sid\n"));
1506                 cont(private_data, False, NULL);
1507                 return;
1508         }
1509
1510         if (response->result != WINBINDD_OK) {
1511                 DEBUG(5, ("gid2sid returned an error\n"));
1512                 cont(private_data, False, NULL);
1513                 return;
1514         }
1515
1516         cont(private_data, True, response->data.sid.sid);
1517 }
1518
1519 void winbindd_gid2sid_async(TALLOC_CTX *mem_ctx, gid_t gid,
1520                             void (*cont)(void *private_data, BOOL success, const char *sid),
1521                             void *private_data)
1522 {
1523         struct winbindd_request request;
1524
1525         ZERO_STRUCT(request);
1526         request.cmd = WINBINDD_DUAL_GID2SID;
1527         request.data.gid = gid;
1528         do_async(mem_ctx, idmap_child(), &request, winbindd_gid2sid_recv,
1529                  (void *)cont, private_data);
1530 }
1531
1532 enum winbindd_result winbindd_dual_gid2sid(struct winbindd_domain *domain,
1533                                            struct winbindd_cli_state *state)
1534 {
1535         DOM_SID sid;
1536         NTSTATUS result;
1537
1538         DEBUG(3,("[%5lu]: gid %lu to sid\n",
1539                 (unsigned long)state->pid,
1540                 (unsigned long) state->request.data.gid));
1541
1542         /* Find sid for this gid and return it, possibly ask the slow remote idmap */
1543         result = idmap_gid_to_sid(&sid, state->request.data.gid);
1544
1545         if (NT_STATUS_IS_OK(result)) {
1546                 sid_to_string(state->response.data.sid.sid, &sid);
1547                 DEBUG(10, ("[%5lu]: retrieved sid: %s\n",
1548                            (unsigned long)state->pid,
1549                            state->response.data.sid.sid));
1550                 state->response.data.sid.type = SID_NAME_DOM_GRP;
1551                 return WINBINDD_OK;
1552         }
1553
1554         return WINBINDD_ERROR;
1555 }
1556
1557 static void winbindd_dump_id_maps_recv(TALLOC_CTX *mem_ctx, BOOL success,
1558                                struct winbindd_response *response,
1559                                void *c, void *private_data)
1560 {
1561         void (*cont)(void *priv, BOOL succ) =
1562                 (void (*)(void *, BOOL))c;
1563
1564         if (!success) {
1565                 DEBUG(5, ("Could not trigger a map dump\n"));
1566                 cont(private_data, False);
1567                 return;
1568         }
1569
1570         if (response->result != WINBINDD_OK) {
1571                 DEBUG(5, ("idmap dump maps returned an error\n"));
1572                 cont(private_data, False);
1573                 return;
1574         }
1575
1576         cont(private_data, True);
1577 }
1578                          
1579 void winbindd_dump_maps_async(TALLOC_CTX *mem_ctx, void *data, int size,
1580                          void (*cont)(void *private_data, BOOL success),
1581                          void *private_data)
1582 {
1583         struct winbindd_request request;
1584         ZERO_STRUCT(request);
1585         request.cmd = WINBINDD_DUAL_DUMP_MAPS;
1586         request.extra_data.data = (char *)data;
1587         request.extra_len = size;
1588         do_async(mem_ctx, idmap_child(), &request, winbindd_dump_id_maps_recv,
1589                  (void *)cont, private_data);
1590 }
1591
1592 enum winbindd_result winbindd_dual_dump_maps(struct winbindd_domain *domain,
1593                                            struct winbindd_cli_state *state)
1594 {
1595         DEBUG(3, ("[%5lu]: dual dump maps\n", (unsigned long)state->pid));
1596
1597         idmap_dump_maps((char *)state->request.extra_data.data);
1598
1599         return WINBINDD_OK;
1600 }
1601