r25568: move idmap related functions into their own file.
[samba.git] / source3 / winbindd / winbindd_idmap.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 static void winbindd_set_mapping_recv(TALLOC_CTX *mem_ctx, BOOL success,
41                                    struct winbindd_response *response,
42                                    void *c, void *private_data)
43 {
44         void (*cont)(void *priv, BOOL succ) = (void (*)(void *, BOOL))c;
45
46         if (!success) {
47                 DEBUG(5, ("Could not trigger idmap_set_mapping\n"));
48                 cont(private_data, False);
49                 return;
50         }
51
52         if (response->result != WINBINDD_OK) {
53                 DEBUG(5, ("idmap_set_mapping returned an error\n"));
54                 cont(private_data, False);
55                 return;
56         }
57
58         cont(private_data, True);
59 }
60
61 void winbindd_set_mapping_async(TALLOC_CTX *mem_ctx, const struct id_map *map,
62                              void (*cont)(void *private_data, BOOL success),
63                              void *private_data)
64 {
65         struct winbindd_request request;
66         ZERO_STRUCT(request);
67         request.cmd = WINBINDD_DUAL_SET_MAPPING;
68         request.data.dual_idmapset.id = map->xid.id;
69         request.data.dual_idmapset.type = map->xid.type;
70         sid_to_string(request.data.dual_idmapset.sid, map->sid);
71
72         do_async(mem_ctx, idmap_child(), &request, winbindd_set_mapping_recv,
73                  (void *)cont, private_data);
74 }
75
76 enum winbindd_result winbindd_dual_set_mapping(struct winbindd_domain *domain,
77                                             struct winbindd_cli_state *state)
78 {
79         struct id_map map;
80         DOM_SID sid;
81         NTSTATUS result;
82
83         DEBUG(3, ("[%5lu]: dual_idmapset\n", (unsigned long)state->pid));
84
85         if (!string_to_sid(&sid, state->request.data.dual_idmapset.sid))
86                 return WINBINDD_ERROR;
87
88         map.sid = &sid;
89         map.xid.id = state->request.data.dual_idmapset.id;
90         map.xid.type = state->request.data.dual_idmapset.type;
91         map.status = ID_MAPPED;
92
93         result = idmap_set_mapping(&map);
94         return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
95 }
96
97 static void winbindd_set_hwm_recv(TALLOC_CTX *mem_ctx, BOOL success,
98                                    struct winbindd_response *response,
99                                    void *c, void *private_data)
100 {
101         void (*cont)(void *priv, BOOL succ) = (void (*)(void *, BOOL))c;
102
103         if (!success) {
104                 DEBUG(5, ("Could not trigger idmap_set_hwm\n"));
105                 cont(private_data, False);
106                 return;
107         }
108
109         if (response->result != WINBINDD_OK) {
110                 DEBUG(5, ("idmap_set_hwm returned an error\n"));
111                 cont(private_data, False);
112                 return;
113         }
114
115         cont(private_data, True);
116 }
117
118 void winbindd_set_hwm_async(TALLOC_CTX *mem_ctx, const struct unixid *xid,
119                              void (*cont)(void *private_data, BOOL success),
120                              void *private_data)
121 {
122         struct winbindd_request request;
123         ZERO_STRUCT(request);
124         request.cmd = WINBINDD_DUAL_SET_HWM;
125         request.data.dual_idmapset.id = xid->id;
126         request.data.dual_idmapset.type = xid->type;
127
128         do_async(mem_ctx, idmap_child(), &request, winbindd_set_hwm_recv,
129                  (void *)cont, private_data);
130 }
131
132 enum winbindd_result winbindd_dual_set_hwm(struct winbindd_domain *domain,
133                                             struct winbindd_cli_state *state)
134 {
135         struct unixid xid;
136         NTSTATUS result;
137
138         DEBUG(3, ("[%5lu]: dual_set_hwm\n", (unsigned long)state->pid));
139
140         xid.id = state->request.data.dual_idmapset.id;
141         xid.type = state->request.data.dual_idmapset.type;
142
143         switch (xid.type) {
144         case ID_TYPE_UID:
145                 result = idmap_set_uid_hwm(&xid);
146                 break;
147         case ID_TYPE_GID:
148                 result = idmap_set_gid_hwm(&xid);
149                 break;
150         default:
151                 return WINBINDD_ERROR;
152         }
153         return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
154 }
155
156 static void winbindd_sids2xids_recv(TALLOC_CTX *mem_ctx, BOOL success,
157                                struct winbindd_response *response,
158                                void *c, void *private_data)
159 {
160         void (*cont)(void *priv, BOOL succ, void *, int) =
161                 (void (*)(void *, BOOL, void *, int))c;
162
163         if (!success) {
164                 DEBUG(5, ("Could not trigger sids2xids\n"));
165                 cont(private_data, False, NULL, 0);
166                 return;
167         }
168
169         if (response->result != WINBINDD_OK) {
170                 DEBUG(5, ("sids2xids returned an error\n"));
171                 cont(private_data, False, NULL, 0);
172                 return;
173         }
174
175         cont(private_data, True, response->extra_data.data, response->length - sizeof(response));
176 }
177
178 void winbindd_sids2xids_async(TALLOC_CTX *mem_ctx, void *sids, int size,
179                          void (*cont)(void *private_data, BOOL success, void *data, int len),
180                          void *private_data)
181 {
182         struct winbindd_request request;
183         ZERO_STRUCT(request);
184         request.cmd = WINBINDD_DUAL_SIDS2XIDS;
185         request.extra_data.data = (char *)sids;
186         request.extra_len = size;
187         do_async(mem_ctx, idmap_child(), &request, winbindd_sids2xids_recv,
188                  (void *)cont, private_data);
189 }
190
191 enum winbindd_result winbindd_dual_sids2xids(struct winbindd_domain *domain,
192                                            struct winbindd_cli_state *state)
193 {
194         DOM_SID *sids;
195         struct unixid *xids;
196         struct id_map **ids;
197         NTSTATUS result;
198         int num, i;
199
200         DEBUG(3, ("[%5lu]: sids to unix ids\n", (unsigned long)state->pid));
201
202         if (state->request.extra_len == 0) {
203                 DEBUG(0, ("Invalid buffer size!\n"));
204                 return WINBINDD_ERROR;
205         }
206
207         sids = (DOM_SID *)state->request.extra_data.data;
208         num = state->request.extra_len / sizeof(DOM_SID);
209
210         ids = TALLOC_ZERO_ARRAY(state->mem_ctx, struct id_map *, num + 1);
211         if ( ! ids) {
212                 DEBUG(0, ("Out of memory!\n"));
213                 return WINBINDD_ERROR;
214         }
215         for (i = 0; i < num; i++) {
216                 ids[i] = TALLOC_P(ids, struct id_map);
217                 if ( ! ids[i]) {
218                         DEBUG(0, ("Out of memory!\n"));
219                         talloc_free(ids);
220                         return WINBINDD_ERROR;
221                 }
222                 ids[i]->sid = &sids[i];
223         }
224
225         result = idmap_sids_to_unixids(ids);
226
227         if (NT_STATUS_IS_OK(result)) {
228
229                 xids = SMB_MALLOC_ARRAY(struct unixid, num);
230                 if ( ! xids) {
231                         DEBUG(0, ("Out of memory!\n"));
232                         talloc_free(ids);
233                         return WINBINDD_ERROR;
234                 }
235
236                 for (i = 0; i < num; i++) {
237                         if (ids[i]->status == ID_MAPPED) {
238                                 xids[i].type = ids[i]->xid.type;
239                                 xids[i].id = ids[i]->xid.id;
240                         } else {
241                                 xids[i].type = -1;
242                         }
243                 }
244
245                 state->response.length = sizeof(state->response) + (sizeof(struct unixid) * num);
246                 state->response.extra_data.data = xids;
247
248         } else {
249                 DEBUG (2, ("idmap_sids_to_unixids returned an error: 0x%08x\n", NT_STATUS_V(result)));
250                 talloc_free(ids);
251                 return WINBINDD_ERROR;
252         }
253
254         talloc_free(ids);
255         return WINBINDD_OK;
256 }
257
258 static void winbindd_sid2uid_recv(TALLOC_CTX *mem_ctx, BOOL success,
259                                struct winbindd_response *response,
260                                void *c, void *private_data)
261 {
262         void (*cont)(void *priv, BOOL succ, uid_t uid) =
263                 (void (*)(void *, BOOL, uid_t))c;
264
265         if (!success) {
266                 DEBUG(5, ("Could not trigger sid2uid\n"));
267                 cont(private_data, False, 0);
268                 return;
269         }
270
271         if (response->result != WINBINDD_OK) {
272                 DEBUG(5, ("sid2uid returned an error\n"));
273                 cont(private_data, False, 0);
274                 return;
275         }
276
277         cont(private_data, True, response->data.uid);
278 }
279
280 void winbindd_sid2uid_async(TALLOC_CTX *mem_ctx, const DOM_SID *sid,
281                          void (*cont)(void *private_data, BOOL success, uid_t uid),
282                          void *private_data)
283 {
284         struct winbindd_request request;
285         ZERO_STRUCT(request);
286         request.cmd = WINBINDD_DUAL_SID2UID;
287         sid_to_string(request.data.dual_sid2id.sid, sid);
288         do_async(mem_ctx, idmap_child(), &request, winbindd_sid2uid_recv,
289                  (void *)cont, private_data);
290 }
291
292 enum winbindd_result winbindd_dual_sid2uid(struct winbindd_domain *domain,
293                                            struct winbindd_cli_state *state)
294 {
295         DOM_SID sid;
296         NTSTATUS result;
297
298         DEBUG(3, ("[%5lu]: sid to uid %s\n", (unsigned long)state->pid,
299                   state->request.data.dual_sid2id.sid));
300
301         if (!string_to_sid(&sid, state->request.data.dual_sid2id.sid)) {
302                 DEBUG(1, ("Could not get convert sid %s from string\n",
303                           state->request.data.dual_sid2id.sid));
304                 return WINBINDD_ERROR;
305         }
306
307         /* Find uid for this sid and return it, possibly ask the slow remote idmap */
308
309         result = idmap_sid_to_uid(&sid, &(state->response.data.uid));
310
311         return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
312 }
313
314 #if 0   /* not used */
315 static void uid2name_recv(TALLOC_CTX *mem_ctx, BOOL success,
316                           struct winbindd_response *response,
317                           void *c, void *private_data);
318
319 void winbindd_uid2name_async(TALLOC_CTX *mem_ctx, uid_t uid,
320                              void (*cont)(void *private_data, BOOL success,
321                                           const char *name),
322                              void *private_data)
323 {
324         struct winbindd_request request;
325         ZERO_STRUCT(request);
326         request.cmd = WINBINDD_DUAL_UID2NAME;
327         request.data.uid = uid;
328         do_async(mem_ctx, idmap_child(), &request, uid2name_recv,
329                  (void *)cont, private_data);
330 }
331 #endif  /* not used */
332
333 enum winbindd_result winbindd_dual_uid2name(struct winbindd_domain *domain,
334                                             struct winbindd_cli_state *state)
335 {
336         struct passwd *pw;
337
338         DEBUG(3, ("[%5lu]: uid2name %lu\n", (unsigned long)state->pid,
339                   (unsigned long)state->request.data.uid));
340
341         pw = getpwuid(state->request.data.uid);
342         if (pw == NULL) {
343                 DEBUG(5, ("User %lu not found\n",
344                           (unsigned long)state->request.data.uid));
345                 return WINBINDD_ERROR;
346         }
347
348         fstrcpy(state->response.data.name.name, pw->pw_name);
349         return WINBINDD_OK;
350 }
351
352 #if 0   /* not used */
353 static void uid2name_recv(TALLOC_CTX *mem_ctx, BOOL success,
354                           struct winbindd_response *response,
355                           void *c, void *private_data)
356 {
357         void (*cont)(void *priv, BOOL succ, const char *name) =
358                 (void (*)(void *, BOOL, const char *))c;
359
360         if (!success) {
361                 DEBUG(5, ("Could not trigger uid2name\n"));
362                 cont(private_data, False, NULL);
363                 return;
364         }
365
366         if (response->result != WINBINDD_OK) {
367                 DEBUG(5, ("uid2name returned an error\n"));
368                 cont(private_data, False, NULL);
369                 return;
370         }
371
372         cont(private_data, True, response->data.name.name);
373 }
374
375 static void name2uid_recv(TALLOC_CTX *mem_ctx, BOOL success,
376                           struct winbindd_response *response,
377                           void *c, void *private_data);
378
379 static void winbindd_name2uid_async(TALLOC_CTX *mem_ctx, const char *name,
380                                     void (*cont)(void *private_data, BOOL success,
381                                                  uid_t uid),
382                                     void *private_data)
383 {
384         struct winbindd_request request;
385         ZERO_STRUCT(request);
386         request.cmd = WINBINDD_DUAL_NAME2UID;
387         fstrcpy(request.data.username, name);
388         do_async(mem_ctx, idmap_child(), &request, name2uid_recv,
389                  (void *)cont, private_data);
390 }
391 #endif  /* not used */
392
393 enum winbindd_result winbindd_dual_name2uid(struct winbindd_domain *domain,
394                                             struct winbindd_cli_state *state)
395 {
396         struct passwd *pw;
397
398         /* Ensure null termination */
399         state->request.data.username
400                 [sizeof(state->request.data.username)-1] = '\0';
401
402         DEBUG(3, ("[%5lu]: name2uid %s\n", (unsigned long)state->pid,
403                   state->request.data.username));
404
405         pw = getpwnam(state->request.data.username);
406         if (pw == NULL) {
407                 return WINBINDD_ERROR;
408         }
409
410         state->response.data.uid = pw->pw_uid;
411         return WINBINDD_OK;
412 }
413
414 #if 0   /* not used */
415 static void name2uid_recv(TALLOC_CTX *mem_ctx, BOOL success,
416                           struct winbindd_response *response,
417                           void *c, void *private_data)
418 {
419         void (*cont)(void *priv, BOOL succ, uid_t uid) =
420                 (void (*)(void *, BOOL, uid_t))c;
421
422         if (!success) {
423                 DEBUG(5, ("Could not trigger name2uid\n"));
424                 cont(private_data, False, 0);
425                 return;
426         }
427
428         if (response->result != WINBINDD_OK) {
429                 DEBUG(5, ("name2uid returned an error\n"));
430                 cont(private_data, False, 0);
431                 return;
432         }
433
434         cont(private_data, True, response->data.uid);
435 }
436 #endif  /* not used */
437
438 static void winbindd_sid2gid_recv(TALLOC_CTX *mem_ctx, BOOL success,
439                                struct winbindd_response *response,
440                                void *c, void *private_data)
441 {
442         void (*cont)(void *priv, BOOL succ, gid_t gid) =
443                 (void (*)(void *, BOOL, gid_t))c;
444
445         if (!success) {
446                 DEBUG(5, ("Could not trigger sid2gid\n"));
447                 cont(private_data, False, 0);
448                 return;
449         }
450
451         if (response->result != WINBINDD_OK) {
452                 DEBUG(5, ("sid2gid returned an error\n"));
453                 cont(private_data, False, 0);
454                 return;
455         }
456
457         cont(private_data, True, response->data.gid);
458 }
459
460 void winbindd_sid2gid_async(TALLOC_CTX *mem_ctx, const DOM_SID *sid,
461                          void (*cont)(void *private_data, BOOL success, gid_t gid),
462                          void *private_data)
463 {
464         struct winbindd_request request;
465         ZERO_STRUCT(request);
466         request.cmd = WINBINDD_DUAL_SID2GID;
467         sid_to_string(request.data.dual_sid2id.sid, sid);
468
469         DEBUG(7,("winbindd_sid2gid_async: Resolving %s to a gid\n",
470                 request.data.dual_sid2id.sid));
471
472         do_async(mem_ctx, idmap_child(), &request, winbindd_sid2gid_recv,
473                  (void *)cont, private_data);
474 }
475
476 enum winbindd_result winbindd_dual_sid2gid(struct winbindd_domain *domain,
477                                            struct winbindd_cli_state *state)
478 {
479         DOM_SID sid;
480         NTSTATUS result;
481
482         DEBUG(3, ("[%5lu]: sid to gid %s\n", (unsigned long)state->pid,
483                   state->request.data.dual_sid2id.sid));
484
485         if (!string_to_sid(&sid, state->request.data.dual_sid2id.sid)) {
486                 DEBUG(1, ("Could not get convert sid %s from string\n",
487                           state->request.data.dual_sid2id.sid));
488                 return WINBINDD_ERROR;
489         }
490
491         /* Find gid for this sid and return it, possibly ask the slow remote idmap */
492
493         result = idmap_sid_to_gid(&sid, &state->response.data.gid);
494
495         DEBUG(10, ("winbindd_dual_sid2gid: 0x%08x - %s - %u\n", NT_STATUS_V(result), sid_string_static(&sid), state->response.data.gid));
496
497         return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
498 }
499
500 static void gid2name_recv(TALLOC_CTX *mem_ctx, BOOL success,
501                           struct winbindd_response *response,
502                           void *c, void *private_data)
503 {
504         void (*cont)(void *priv, BOOL succ, const char *name) =
505                 (void (*)(void *, BOOL, const char *))c;
506
507         if (!success) {
508                 DEBUG(5, ("Could not trigger gid2name\n"));
509                 cont(private_data, False, NULL);
510                 return;
511         }
512
513         if (response->result != WINBINDD_OK) {
514                 DEBUG(5, ("gid2name returned an error\n"));
515                 cont(private_data, False, NULL);
516                 return;
517         }
518
519         cont(private_data, True, response->data.name.name);
520 }
521
522 void winbindd_gid2name_async(TALLOC_CTX *mem_ctx, gid_t gid,
523                              void (*cont)(void *private_data, BOOL success,
524                                           const char *name),
525                              void *private_data)
526 {
527         struct winbindd_request request;
528         ZERO_STRUCT(request);
529         request.cmd = WINBINDD_DUAL_GID2NAME;
530         request.data.gid = gid;
531         do_async(mem_ctx, idmap_child(), &request, gid2name_recv,
532                  (void *)cont, private_data);
533 }
534
535 enum winbindd_result winbindd_dual_gid2name(struct winbindd_domain *domain,
536                                             struct winbindd_cli_state *state)
537 {
538         struct group *gr;
539
540         DEBUG(3, ("[%5lu]: gid2name %lu\n", (unsigned long)state->pid,
541                   (unsigned long)state->request.data.gid));
542
543         gr = getgrgid(state->request.data.gid);
544         if (gr == NULL)
545                 return WINBINDD_ERROR;
546
547         fstrcpy(state->response.data.name.name, gr->gr_name);
548         return WINBINDD_OK;
549 }
550
551 #if 0   /* not used */
552 static void name2gid_recv(TALLOC_CTX *mem_ctx, BOOL success,
553                           struct winbindd_response *response,
554                           void *c, void *private_data);
555
556 static void winbindd_name2gid_async(TALLOC_CTX *mem_ctx, const char *name,
557                                     void (*cont)(void *private_data, BOOL success,
558                                                  gid_t gid),
559                                     void *private_data)
560 {
561         struct winbindd_request request;
562         ZERO_STRUCT(request);
563         request.cmd = WINBINDD_DUAL_NAME2GID;
564         fstrcpy(request.data.groupname, name);
565         do_async(mem_ctx, idmap_child(), &request, name2gid_recv,
566                  (void *)cont, private_data);
567 }
568 #endif  /* not used */
569
570 enum winbindd_result winbindd_dual_name2gid(struct winbindd_domain *domain,
571                                             struct winbindd_cli_state *state)
572 {
573         struct group *gr;
574
575         /* Ensure null termination */
576         state->request.data.groupname
577                 [sizeof(state->request.data.groupname)-1] = '\0';
578
579         DEBUG(3, ("[%5lu]: name2gid %s\n", (unsigned long)state->pid,
580                   state->request.data.groupname));
581
582         gr = getgrnam(state->request.data.groupname);
583         if (gr == NULL) {
584                 return WINBINDD_ERROR;
585         }
586
587         state->response.data.gid = gr->gr_gid;
588         return WINBINDD_OK;
589 }
590
591 #if 0   /* not used */
592 static void name2gid_recv(TALLOC_CTX *mem_ctx, BOOL success,
593                           struct winbindd_response *response,
594                           void *c, void *private_data)
595 {
596         void (*cont)(void *priv, BOOL succ, gid_t gid) =
597                 (void (*)(void *, BOOL, gid_t))c;
598
599         if (!success) {
600                 DEBUG(5, ("Could not trigger name2gid\n"));
601                 cont(private_data, False, 0);
602                 return;
603         }
604
605         if (response->result != WINBINDD_OK) {
606                 DEBUG(5, ("name2gid returned an error\n"));
607                 cont(private_data, False, 0);
608                 return;
609         }
610
611         cont(private_data, True, response->data.gid);
612 }
613 #endif  /* not used */
614
615 /* The following uid2sid/gid2sid functions has been contributed by
616  * Keith Reynolds <Keith.Reynolds@centrify.com> */
617
618 static void winbindd_uid2sid_recv(TALLOC_CTX *mem_ctx, BOOL success,
619                                   struct winbindd_response *response,
620                                   void *c, void *private_data)
621 {
622         void (*cont)(void *priv, BOOL succ, const char *sid) =
623                 (void (*)(void *, BOOL, const char *))c;
624
625         if (!success) {
626                 DEBUG(5, ("Could not trigger uid2sid\n"));
627                 cont(private_data, False, NULL);
628                 return;
629         }
630
631         if (response->result != WINBINDD_OK) {
632                 DEBUG(5, ("uid2sid returned an error\n"));
633                 cont(private_data, False, NULL);
634                 return;
635         }
636
637         cont(private_data, True, response->data.sid.sid);
638 }
639
640 void winbindd_uid2sid_async(TALLOC_CTX *mem_ctx, uid_t uid,
641                             void (*cont)(void *private_data, BOOL success, const char *sid),
642                             void *private_data)
643 {
644         struct winbindd_request request;
645
646         ZERO_STRUCT(request);
647         request.cmd = WINBINDD_DUAL_UID2SID;
648         request.data.uid = uid;
649         do_async(mem_ctx, idmap_child(), &request, winbindd_uid2sid_recv,
650                  (void *)cont, private_data);
651 }
652
653 enum winbindd_result winbindd_dual_uid2sid(struct winbindd_domain *domain,
654                                            struct winbindd_cli_state *state)
655 {
656         DOM_SID sid;
657         NTSTATUS result;
658
659         DEBUG(3,("[%5lu]: uid to sid %lu\n",
660                  (unsigned long)state->pid,
661                  (unsigned long) state->request.data.uid));
662
663         /* Find sid for this uid and return it, possibly ask the slow remote idmap */
664         result = idmap_uid_to_sid(&sid, state->request.data.uid);
665
666         if (NT_STATUS_IS_OK(result)) {
667                 sid_to_string(state->response.data.sid.sid, &sid);
668                 state->response.data.sid.type = SID_NAME_USER;
669                 return WINBINDD_OK;
670         }
671
672         return WINBINDD_ERROR;
673 }
674
675 static void winbindd_gid2sid_recv(TALLOC_CTX *mem_ctx, BOOL success,
676                                   struct winbindd_response *response,
677                                   void *c, void *private_data)
678 {
679         void (*cont)(void *priv, BOOL succ, const char *sid) =
680                 (void (*)(void *, BOOL, const char *))c;
681
682         if (!success) {
683                 DEBUG(5, ("Could not trigger gid2sid\n"));
684                 cont(private_data, False, NULL);
685                 return;
686         }
687
688         if (response->result != WINBINDD_OK) {
689                 DEBUG(5, ("gid2sid returned an error\n"));
690                 cont(private_data, False, NULL);
691                 return;
692         }
693
694         cont(private_data, True, response->data.sid.sid);
695 }
696
697 void winbindd_gid2sid_async(TALLOC_CTX *mem_ctx, gid_t gid,
698                             void (*cont)(void *private_data, BOOL success, const char *sid),
699                             void *private_data)
700 {
701         struct winbindd_request request;
702
703         ZERO_STRUCT(request);
704         request.cmd = WINBINDD_DUAL_GID2SID;
705         request.data.gid = gid;
706         do_async(mem_ctx, idmap_child(), &request, winbindd_gid2sid_recv,
707                  (void *)cont, private_data);
708 }
709
710 enum winbindd_result winbindd_dual_gid2sid(struct winbindd_domain *domain,
711                                            struct winbindd_cli_state *state)
712 {
713         DOM_SID sid;
714         NTSTATUS result;
715
716         DEBUG(3,("[%5lu]: gid %lu to sid\n",
717                 (unsigned long)state->pid,
718                 (unsigned long) state->request.data.gid));
719
720         /* Find sid for this gid and return it, possibly ask the slow remote idmap */
721         result = idmap_gid_to_sid(&sid, state->request.data.gid);
722
723         if (NT_STATUS_IS_OK(result)) {
724                 sid_to_string(state->response.data.sid.sid, &sid);
725                 DEBUG(10, ("[%5lu]: retrieved sid: %s\n",
726                            (unsigned long)state->pid,
727                            state->response.data.sid.sid));
728                 state->response.data.sid.type = SID_NAME_DOM_GRP;
729                 return WINBINDD_OK;
730         }
731
732         return WINBINDD_ERROR;
733 }
734
735 static void winbindd_dump_id_maps_recv(TALLOC_CTX *mem_ctx, BOOL success,
736                                struct winbindd_response *response,
737                                void *c, void *private_data)
738 {
739         void (*cont)(void *priv, BOOL succ) =
740                 (void (*)(void *, BOOL))c;
741
742         if (!success) {
743                 DEBUG(5, ("Could not trigger a map dump\n"));
744                 cont(private_data, False);
745                 return;
746         }
747
748         if (response->result != WINBINDD_OK) {
749                 DEBUG(5, ("idmap dump maps returned an error\n"));
750                 cont(private_data, False);
751                 return;
752         }
753
754         cont(private_data, True);
755 }
756
757 void winbindd_dump_maps_async(TALLOC_CTX *mem_ctx, void *data, int size,
758                          void (*cont)(void *private_data, BOOL success),
759                          void *private_data)
760 {
761         struct winbindd_request request;
762         ZERO_STRUCT(request);
763         request.cmd = WINBINDD_DUAL_DUMP_MAPS;
764         request.extra_data.data = (char *)data;
765         request.extra_len = size;
766         do_async(mem_ctx, idmap_child(), &request, winbindd_dump_id_maps_recv,
767                  (void *)cont, private_data);
768 }
769
770 enum winbindd_result winbindd_dual_dump_maps(struct winbindd_domain *domain,
771                                            struct winbindd_cli_state *state)
772 {
773         DEBUG(3, ("[%5lu]: dual dump maps\n", (unsigned long)state->pid));
774
775         idmap_dump_maps((char *)state->request.extra_data.data);
776
777         return WINBINDD_OK;
778 }