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