add winbindd_ndr_child_set_idmap() ...
[metze/samba/wb-ndr.git] / source / 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    Copyright (C) Simo Sorce 2007
9
10    The helpers always consist of three functions:
11
12    * A request setup function that takes the necessary parameters together
13      with a continuation function that is to be called upon completion
14
15    * A private continuation function that is internal only. This is to be
16      called by the lower-level functions in do_async(). Its only task is to
17      properly call the continuation function named above.
18
19    * A worker function that is called inside the appropriate child process.
20
21    This program is free software; you can redistribute it and/or modify
22    it under the terms of the GNU General Public License as published by
23    the Free Software Foundation; either version 3 of the License, or
24    (at your option) any later version.
25
26    This program is distributed in the hope that it will be useful,
27    but WITHOUT ANY WARRANTY; without even the implied warranty of
28    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29    GNU General Public License for more details.
30
31    You should have received a copy of the GNU General Public License
32    along with this program.  If not, see <http://www.gnu.org/licenses/>.
33 */
34
35 #include "includes.h"
36 #include "winbindd.h"
37
38 #undef DBGC_CLASS
39 #define DBGC_CLASS DBGC_WINBIND
40
41 static const struct winbindd_child_dispatch_table idmap_dispatch_table[];
42
43 static struct winbindd_child static_idmap_child;
44
45 void init_idmap_child(void)
46 {
47         setup_child(&static_idmap_child,
48                     idmap_dispatch_table,
49                     "log.winbindd", "idmap");
50 }
51
52 struct winbindd_child *idmap_child(void)
53 {
54         return &static_idmap_child;
55 }
56
57 static void ndr_child_get_idmap_sid2uid(struct winbindd_domain *domain,
58                                         struct winbindd_cli_state *state,
59                                         struct winbind_get_idmap *r)
60 {
61         uid_t uid;
62         NTSTATUS result;
63
64         DEBUG(3, ("sid to uid '%s'\n", sid_string_tos(r->in.req.sid)));
65
66         /* Find uid for this sid and return it, possibly ask the slow remote idmap */
67
68         result = idmap_sid_to_uid(r->in.req.sid, &uid);
69         if (!NT_STATUS_IS_OK(result)) {
70                 DEBUG(1, ("Can't map '%s' to uid: %s\n",
71                         sid_string_tos(r->in.req.sid),
72                         nt_errstr(result)));
73                 r->out.result = WINBIND_STATUS_FOOBAR;
74                 return;
75         }
76
77         DEBUG(10, ("sid %s mapped to uid %u\n",
78                 sid_string_tos(r->in.req.sid), uid));
79
80         r->out.rep->uid = uid;
81         r->out.result = WINBIND_STATUS_OK;
82 }
83
84 static void ndr_child_get_idmap_sid2gid(struct winbindd_domain *domain,
85                                         struct winbindd_cli_state *state,
86                                         struct winbind_get_idmap *r)
87 {
88         gid_t gid;
89         NTSTATUS result;
90
91         DEBUG(3, ("sid to gid '%s'\n", sid_string_tos(r->in.req.sid)));
92
93         /* Find uid for this sid and return it, possibly ask the slow remote idmap */
94
95         result = idmap_sid_to_gid(r->in.req.sid, &gid);
96         if (!NT_STATUS_IS_OK(result)) {
97                 DEBUG(1, ("Can't map '%s' to gid: %s\n",
98                         sid_string_tos(r->in.req.sid),
99                         nt_errstr(result)));
100                 r->out.result = WINBIND_STATUS_FOOBAR;
101                 return;
102         }
103
104         DEBUG(10, ("sid %s mapped to gid %u\n",
105                 sid_string_tos(r->in.req.sid), gid));
106
107         r->out.rep->gid = gid;
108         r->out.result = WINBIND_STATUS_OK;
109 }
110
111 static void ndr_child_get_idmap_uid2sid(struct winbindd_domain *domain,
112                                         struct winbindd_cli_state *state,
113                                         struct winbind_get_idmap *r)
114 {
115         DOM_SID sid;
116         uid_t uid;
117         NTSTATUS result;
118
119         DEBUG(3, ("uid to sid '%llu'\n",
120                 (unsigned long long)r->in.req.uid));
121
122         /* the IDMAP subsystem only knows about uint32_t id's yet */
123         if (r->in.req.uid > UINT32_MAX) {
124                 DEBUG(1, ("Can't map uid '%llu' to sid\n",
125                         (unsigned long long)r->in.req.uid));
126                 r->out.result = WINBIND_STATUS_FOOBAR;
127                 return;
128         }
129
130         /* Find uid for this sid and return it, possibly ask the slow remote idmap */
131
132         uid = r->in.req.uid;
133
134         result = idmap_uid_to_sid(&sid, uid);
135         if (!NT_STATUS_IS_OK(result)) {
136                 DEBUG(1, ("Can't map uid '%u' to sid: %s\n",
137                         uid, nt_errstr(result)));
138                 r->out.result = WINBIND_STATUS_FOOBAR;
139                 return;
140         }
141
142         DEBUG(10, ("uid %u mapped to sid %s\n",
143                 uid, sid_string_tos(&sid)));
144
145         r->out.rep->sid = sid_dup_talloc(r, &sid);
146         if (!r->out.rep->sid) {
147                 r->out.result = WINBIND_STATUS_NO_MEMORY;
148                 return;
149         }
150
151         r->out.result = WINBIND_STATUS_OK;
152 }
153
154 static void ndr_child_get_idmap_gid2sid(struct winbindd_domain *domain,
155                                         struct winbindd_cli_state *state,
156                                         struct winbind_get_idmap *r)
157 {
158         DOM_SID sid;
159         gid_t gid;
160         NTSTATUS result;
161
162         DEBUG(3, ("gid to sid '%llu'\n",
163                 (unsigned long long)r->in.req.gid));
164
165         /* the IDMAP subsystem only knows about uint32_t id's yet */
166         if (r->in.req.gid > UINT32_MAX) {
167                 DEBUG(1, ("Can't map gid '%llu' to sid\n",
168                         (unsigned long long)r->in.req.gid));
169                 r->out.result = WINBIND_STATUS_FOOBAR;
170                 return;
171         }
172
173         /* Find uid for this sid and return it, possibly ask the slow remote idmap */
174         gid = r->in.req.gid;
175
176         result = idmap_gid_to_sid(&sid, gid);
177         if (!NT_STATUS_IS_OK(result)) {
178                 DEBUG(1, ("Can't map gid '%u' to sid: %s\n",
179                         gid, nt_errstr(result)));
180                 r->out.result = WINBIND_STATUS_FOOBAR;
181                 return;
182         }
183
184         DEBUG(10, ("gid %u mapped to sid %s\n",
185                 gid, sid_string_tos(&sid)));
186
187         r->out.rep->sid = sid_dup_talloc(r, &sid);
188         if (!r->out.rep->sid) {
189                 r->out.result = WINBIND_STATUS_NO_MEMORY;
190                 return;
191         }
192
193         r->out.result = WINBIND_STATUS_OK;
194 }
195
196 void winbindd_ndr_child_get_idmap(struct winbindd_domain *domain,
197                                   struct winbindd_cli_state *state)
198 {
199         struct winbind_get_idmap *r;
200
201         r = talloc_get_type_abort(state->c.ndr.r,
202                                   struct winbind_get_idmap);
203
204         switch (*r->in.level) {
205         case WINBIND_IDMAP_LEVEL_SID_TO_UID:
206                 ndr_child_get_idmap_sid2uid(domain, state, r);
207                 return;
208
209         case WINBIND_IDMAP_LEVEL_SID_TO_GID:
210                 ndr_child_get_idmap_sid2gid(domain, state, r);
211                 return;
212
213         case WINBIND_IDMAP_LEVEL_UID_TO_SID:
214                 ndr_child_get_idmap_uid2sid(domain, state, r);
215                 return;
216
217         case WINBIND_IDMAP_LEVEL_GID_TO_SID:
218                 ndr_child_get_idmap_gid2sid(domain, state, r);
219                 return;
220         }
221
222         r->out.result = WINBIND_STATUS_UNKNOWN_LEVEL;
223         return;
224 }
225
226 static void ndr_child_set_idmap_allocate_uid(struct winbindd_domain *domain,
227                                              struct winbindd_cli_state *state,
228                                              struct winbind_set_idmap *r)
229 {
230         NTSTATUS result;
231         struct unixid xid;
232
233         DEBUG(3, ("allocate_uid\n"));
234
235         result = idmap_allocate_uid(&xid);
236         if (!NT_STATUS_IS_OK(result)) {
237                 DEBUG(1,("Can't allocate uid: %s\n",
238                          nt_errstr(result)));
239                 r->out.result = WINBIND_STATUS_FOOBAR;
240                 return;
241         }
242
243         DEBUG(10, ("allocte_uid: %u\n",
244                    xid.id));
245
246         r->out.rep->uid = xid.id;
247
248         r->out.result = WINBIND_STATUS_OK;
249 }
250
251 static void ndr_child_set_idmap_allocate_gid(struct winbindd_domain *domain,
252                                              struct winbindd_cli_state *state,
253                                              struct winbind_set_idmap *r)
254 {
255         NTSTATUS result;
256         struct unixid xid;
257
258         DEBUG(3, ("allocate_gid\n"));
259
260         result = idmap_allocate_gid(&xid);
261         if (!NT_STATUS_IS_OK(result)) {
262                 DEBUG(1,("Can't allocate gid: %s\n",
263                          nt_errstr(result)));
264                 r->out.result = WINBIND_STATUS_FOOBAR;
265                 return;
266         }
267
268         DEBUG(10, ("allocte_gid: %u\n",
269                    xid.id));
270
271         r->out.rep->gid = xid.id;
272
273         r->out.result = WINBIND_STATUS_OK;
274 }
275
276 static void ndr_child_set_idmap_set_mapping(struct winbindd_domain *domain,
277                                             struct winbindd_cli_state *state,
278                                             struct winbind_set_idmap *r)
279 {
280         NTSTATUS result;
281
282         DEBUG(3,("set_mapping: sid %s -> %u type:%u\n",
283                  sid_string_tos(r->in.req.mapping.sid),
284                  r->in.req.mapping.xid.id,
285                  r->in.req.mapping.xid.type));
286
287         result = idmap_set_mapping(&r->in.req.mapping);
288         if (!NT_STATUS_IS_OK(result)) {
289                 DEBUG(1,("Can't set mapping: %s\n",
290                          nt_errstr(result)));
291                 r->out.result = WINBIND_STATUS_FOOBAR;
292                 return;
293         }
294
295         r->out.result = WINBIND_STATUS_OK;
296 }
297
298 static void ndr_child_set_idmap_set_hwm(struct winbindd_domain *domain,
299                                         struct winbindd_cli_state *state,
300                                         struct winbind_set_idmap *r)
301 {
302         NTSTATUS result;
303
304         DEBUG(3,("set_hwm: %u type:%u\n",
305                  r->in.req.hwm.id,
306                  r->in.req.hwm.type));
307
308         switch (r->in.req.hwm.type) {
309         case ID_TYPE_UID:
310                 result = idmap_set_uid_hwm(&r->in.req.hwm);
311                 break;
312         case ID_TYPE_GID:
313                 result = idmap_set_gid_hwm(&r->in.req.hwm);
314                 break;
315         default:
316                 result = NT_STATUS_INVALID_PARAMETER;
317                 break;
318         }
319
320         if (!NT_STATUS_IS_OK(result)) {
321                 DEBUG(1,("Can't set hwm: %s\n",
322                          nt_errstr(result)));
323                 r->out.result = WINBIND_STATUS_FOOBAR;
324                 return;
325         }
326
327         r->out.result = WINBIND_STATUS_OK;
328 }
329
330 void winbindd_ndr_child_set_idmap(struct winbindd_domain *domain,
331                                   struct winbindd_cli_state *state)
332 {
333         struct winbind_set_idmap *r;
334
335         r = talloc_get_type_abort(state->c.ndr.r,
336                                   struct winbind_set_idmap);
337
338         switch (*r->in.level) {
339         case WINBIND_SET_IDMAP_LEVEL_ALLOCATE_UID:
340                 ndr_child_set_idmap_allocate_uid(domain, state, r);
341                 return;
342
343         case WINBIND_SET_IDMAP_LEVEL_ALLOCATE_GID:
344                 ndr_child_set_idmap_allocate_gid(domain, state, r);
345                 return;
346
347         case WINBIND_SET_IDMAP_LEVEL_SET_MAPPING:
348                 ndr_child_set_idmap_set_mapping(domain, state, r);
349                 return;
350
351         case WINBIND_SET_IDMAP_LEVEL_SET_HWM:
352                 ndr_child_set_idmap_set_hwm(domain, state, r);
353                 return;
354         }
355
356         r->out.result = WINBIND_STATUS_UNKNOWN_LEVEL;
357         return;
358 }
359
360 static void winbindd_set_mapping_recv(TALLOC_CTX *mem_ctx, bool success,
361                                    struct winbindd_response *response,
362                                    void *c, void *private_data)
363 {
364         void (*cont)(void *priv, bool succ) = (void (*)(void *, bool))c;
365
366         if (!success) {
367                 DEBUG(5, ("Could not trigger idmap_set_mapping\n"));
368                 cont(private_data, False);
369                 return;
370         }
371
372         if (response->result != WINBINDD_OK) {
373                 DEBUG(5, ("idmap_set_mapping returned an error\n"));
374                 cont(private_data, False);
375                 return;
376         }
377
378         cont(private_data, True);
379 }
380
381 void winbindd_set_mapping_async(TALLOC_CTX *mem_ctx, const struct id_map *map,
382                              void (*cont)(void *private_data, bool success),
383                              void *private_data)
384 {
385         struct winbindd_request request;
386         ZERO_STRUCT(request);
387         request.cmd = WINBINDD_DUAL_SET_MAPPING;
388         request.data.dual_idmapset.id = map->xid.id;
389         request.data.dual_idmapset.type = map->xid.type;
390         sid_to_fstring(request.data.dual_idmapset.sid, map->sid);
391
392         do_async(mem_ctx, idmap_child(), &request, winbindd_set_mapping_recv,
393                  (void *)cont, private_data);
394 }
395
396 enum winbindd_result winbindd_dual_set_mapping(struct winbindd_domain *domain,
397                                             struct winbindd_cli_state *state)
398 {
399         struct id_map map;
400         DOM_SID sid;
401         NTSTATUS result;
402
403         DEBUG(3, ("[%5lu]: dual_idmapset\n", (unsigned long)state->pid));
404
405         if (!string_to_sid(&sid, state->request.data.dual_idmapset.sid))
406                 return WINBINDD_ERROR;
407
408         map.sid = &sid;
409         map.xid.id = state->request.data.dual_idmapset.id;
410         map.xid.type = state->request.data.dual_idmapset.type;
411         map.status = ID_MAPPED;
412
413         result = idmap_set_mapping(&map);
414         return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
415 }
416
417 static void winbindd_set_hwm_recv(TALLOC_CTX *mem_ctx, bool success,
418                                    struct winbindd_response *response,
419                                    void *c, void *private_data)
420 {
421         void (*cont)(void *priv, bool succ) = (void (*)(void *, bool))c;
422
423         if (!success) {
424                 DEBUG(5, ("Could not trigger idmap_set_hwm\n"));
425                 cont(private_data, False);
426                 return;
427         }
428
429         if (response->result != WINBINDD_OK) {
430                 DEBUG(5, ("idmap_set_hwm returned an error\n"));
431                 cont(private_data, False);
432                 return;
433         }
434
435         cont(private_data, True);
436 }
437
438 void winbindd_set_hwm_async(TALLOC_CTX *mem_ctx, const struct unixid *xid,
439                              void (*cont)(void *private_data, bool success),
440                              void *private_data)
441 {
442         struct winbindd_request request;
443         ZERO_STRUCT(request);
444         request.cmd = WINBINDD_DUAL_SET_HWM;
445         request.data.dual_idmapset.id = xid->id;
446         request.data.dual_idmapset.type = xid->type;
447
448         do_async(mem_ctx, idmap_child(), &request, winbindd_set_hwm_recv,
449                  (void *)cont, private_data);
450 }
451
452 enum winbindd_result winbindd_dual_set_hwm(struct winbindd_domain *domain,
453                                             struct winbindd_cli_state *state)
454 {
455         struct unixid xid;
456         NTSTATUS result;
457
458         DEBUG(3, ("[%5lu]: dual_set_hwm\n", (unsigned long)state->pid));
459
460         xid.id = state->request.data.dual_idmapset.id;
461         xid.type = state->request.data.dual_idmapset.type;
462
463         switch (xid.type) {
464         case ID_TYPE_UID:
465                 result = idmap_set_uid_hwm(&xid);
466                 break;
467         case ID_TYPE_GID:
468                 result = idmap_set_gid_hwm(&xid);
469                 break;
470         default:
471                 return WINBINDD_ERROR;
472         }
473         return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
474 }
475
476 static void winbindd_sid2uid_recv(TALLOC_CTX *mem_ctx, bool success,
477                                     struct winbindd_ndr_call *c,
478                                     void *_r,
479                                     void *_cont,
480                                     void *private_data)
481 {
482         void (*cont)(void *priv, bool succ, uid_t) =
483                 (void (*)(void *, bool, uid_t))_cont;
484         struct winbind_get_idmap *r =
485                 talloc_get_type_abort(_r, struct winbind_get_idmap);
486         uid_t uid;
487
488         if (!success) {
489                 DEBUG(5, ("Could not get_idmap(sid2uid)\n"));
490                 TALLOC_FREE(r);
491                 cont(private_data, false, (uid_t)-1);
492                 return;
493         }
494
495         if (r->out.result != WINBIND_STATUS_OK) {
496                 DEBUG(5, ("get_idmap(sid2uid) returned an error:0x%08X\n",
497                         r->out.result));
498                 TALLOC_FREE(r);
499                 cont(private_data, false, (uid_t)-1);
500                 return;
501         }
502
503         if (r->out.rep->uid > UINT32_MAX) {
504                 DEBUG(1, ("get_idmap(sid2uid) returned a 64bit uid %llu\n",
505                         (unsigned long long)r->out.rep->uid));
506                 TALLOC_FREE(r);
507                 cont(private_data, false, (uid_t)-1);
508                 return;
509         }
510
511         uid = r->out.rep->uid;
512         TALLOC_FREE(r);
513         cont(private_data, true, uid);
514 }
515
516 void winbindd_sid2uid_async(TALLOC_CTX *mem_ctx, const DOM_SID *sid,
517                          void (*cont)(void *private_data, bool success, uid_t uid),
518                          void *private_data)
519 {
520         struct winbind_get_idmap *r = NULL;
521
522         DEBUG(7,("winbindd_sid2uid_async: Resolving %s to a uid\n",
523                 sid_string_tos(sid)));
524
525         r = TALLOC_P(mem_ctx, struct winbind_get_idmap);
526         if (!r) goto nomem;
527         r->in.level = TALLOC_P(r, enum winbind_get_idmap_level);
528         if (!r->in.level) goto nomem;
529
530         *r->in.level    = WINBIND_IDMAP_LEVEL_SID_TO_UID;
531         r->in.req.sid   = discard_const(sid);
532
533         do_async_ndr(mem_ctx, idmap_child(),
534                      NDR_WINBIND_GET_IDMAP, r,
535                      winbindd_sid2uid_recv, r,
536                      (void *)cont, private_data);
537         return;
538 nomem:
539         TALLOC_FREE(r);
540         cont(private_data, false, (uid_t)-1);
541         return;
542 }
543
544 static void winbindd_sid2gid_recv(TALLOC_CTX *mem_ctx, bool success,
545                                     struct winbindd_ndr_call *c,
546                                     void *_r,
547                                     void *_cont,
548                                     void *private_data)
549 {
550         void (*cont)(void *priv, bool succ, gid_t) =
551                 (void (*)(void *, bool, gid_t))_cont;
552         struct winbind_get_idmap *r =
553                 talloc_get_type_abort(_r, struct winbind_get_idmap);
554         gid_t gid;
555
556         if (!success) {
557                 DEBUG(5, ("Could not get_idmap(sid2gid)\n"));
558                 TALLOC_FREE(r);
559                 cont(private_data, false, (gid_t)-1);
560                 return;
561         }
562
563         if (r->out.result != WINBIND_STATUS_OK) {
564                 DEBUG(5, ("get_idmap(sid2gid) returned an error:0x%08X\n",
565                         r->out.result));
566                 TALLOC_FREE(r);
567                 cont(private_data, false, (gid_t)-1);
568                 return;
569         }
570
571         if (r->out.rep->gid > UINT32_MAX) {
572                 DEBUG(1, ("get_idmap(sid2gid) returned a 64bit gid %llu\n",
573                         (unsigned long long)r->out.rep->gid));
574                 TALLOC_FREE(r);
575                 cont(private_data, false, (gid_t)-1);
576                 return;
577         }
578
579         gid = r->out.rep->gid;
580         TALLOC_FREE(r);
581         cont(private_data, true, gid);
582 }
583
584 void winbindd_sid2gid_async(TALLOC_CTX *mem_ctx, const DOM_SID *sid,
585                          void (*cont)(void *private_data, bool success, gid_t gid),
586                          void *private_data)
587 {
588         struct winbind_get_idmap *r = NULL;
589
590         DEBUG(7,("winbindd_sid2gid_async: Resolving %s to a gid\n",
591                 sid_string_tos(sid)));
592
593         r = TALLOC_P(mem_ctx, struct winbind_get_idmap);
594         if (!r) goto nomem;
595         r->in.level = TALLOC_P(r, enum winbind_get_idmap_level);
596         if (!r->in.level) goto nomem;
597
598         *r->in.level    = WINBIND_IDMAP_LEVEL_SID_TO_GID;
599         r->in.req.sid   = discard_const(sid);
600
601         do_async_ndr(mem_ctx, idmap_child(),
602                      NDR_WINBIND_GET_IDMAP, r,
603                      winbindd_sid2gid_recv, r,
604                      (void *)cont, private_data);
605         return;
606 nomem:
607         TALLOC_FREE(r);
608         cont(private_data, false, (gid_t)-1);
609         return;
610 }
611
612 static void winbindd_uid2sid_recv(TALLOC_CTX *mem_ctx, bool success,
613                                     struct winbindd_ndr_call *c,
614                                     void *_r,
615                                     void *_cont,
616                                     void *private_data)
617 {
618         void (*cont)(void *priv, bool succ, const char *sid) =
619                 (void (*)(void *, bool, const char *))_cont;
620         struct winbind_get_idmap *r =
621                 talloc_get_type_abort(_r, struct winbind_get_idmap);
622         const char *sid;
623
624         if (!success) {
625                 DEBUG(5, ("Could not get_idmap(uid2sid)\n"));
626                 TALLOC_FREE(r);
627                 cont(private_data, false, NULL);
628                 return;
629         }
630
631         if (r->out.result != WINBIND_STATUS_OK) {
632                 DEBUG(5, ("get_idmap(uid2sid) returned an error:0x%08X\n",
633                         r->out.result));
634                 TALLOC_FREE(r);
635                 cont(private_data, false, NULL);
636                 return;
637         }
638
639         sid = dom_sid_string(mem_ctx, r->out.rep->sid);
640         if (!sid) {
641                 TALLOC_FREE(r);
642                 cont(private_data, false, NULL);
643                 return;
644         }
645
646         TALLOC_FREE(r);
647         cont(private_data, true, sid);
648 }
649
650 void winbindd_uid2sid_async(TALLOC_CTX *mem_ctx, uid_t uid,
651                             void (*cont)(void *private_data, bool success, const char *sid),
652                             void *private_data)
653 {
654         struct winbind_get_idmap *r = NULL;
655
656         DEBUG(7,("winbindd_uid2sid_async: Resolving %u to a sid\n",
657                 uid));
658
659         r = TALLOC_P(mem_ctx, struct winbind_get_idmap);
660         if (!r) goto nomem;
661         r->in.level = TALLOC_P(r, enum winbind_get_idmap_level);
662         if (!r->in.level) goto nomem;
663
664         *r->in.level    = WINBIND_IDMAP_LEVEL_UID_TO_SID;
665         r->in.req.uid   = uid;
666
667         do_async_ndr(mem_ctx, idmap_child(),
668                      NDR_WINBIND_GET_IDMAP, r,
669                      winbindd_uid2sid_recv, r,
670                      (void *)cont, private_data);
671         return;
672 nomem:
673         TALLOC_FREE(r);
674         cont(private_data, false, NULL);
675         return;
676 }
677
678 static void winbindd_gid2sid_recv(TALLOC_CTX *mem_ctx, bool success,
679                                   struct winbindd_ndr_call *c,
680                                   void *_r,
681                                   void *_cont,
682                                   void *private_data)
683 {
684         void (*cont)(void *priv, bool succ, const char *sid) =
685                 (void (*)(void *, bool, const char *))_cont;
686         struct winbind_get_idmap *r =
687                 talloc_get_type_abort(_r, struct winbind_get_idmap);
688         const char *sid;
689
690         if (!success) {
691                 DEBUG(5, ("Could not get_idmap(gid2sid)\n"));
692                 TALLOC_FREE(r);
693                 cont(private_data, false, NULL);
694                 return;
695         }
696
697         if (r->out.result != WINBIND_STATUS_OK) {
698                 DEBUG(5, ("get_idmap(gid2sid) returned an error:0x%08X\n",
699                         r->out.result));
700                 TALLOC_FREE(r);
701                 cont(private_data, false, NULL);
702                 return;
703         }
704
705         sid = dom_sid_string(mem_ctx, r->out.rep->sid);
706         if (!sid) {
707                 TALLOC_FREE(r);
708                 cont(private_data, false, NULL);
709                 return;
710         }
711
712         TALLOC_FREE(r);
713         cont(private_data, true, sid);
714 }
715
716 void winbindd_gid2sid_async(TALLOC_CTX *mem_ctx, gid_t gid,
717                             void (*cont)(void *private_data, bool success, const char *sid),
718                             void *private_data)
719 {
720         struct winbind_get_idmap *r = NULL;
721
722         DEBUG(7,("winbindd_gid2sid_async: Resolving %u to a sid\n",
723                 gid));
724
725         r = TALLOC_P(mem_ctx, struct winbind_get_idmap);
726         if (!r) goto nomem;
727         r->in.level = TALLOC_P(r, enum winbind_get_idmap_level);
728         if (!r->in.level) goto nomem;
729
730         *r->in.level    = WINBIND_IDMAP_LEVEL_GID_TO_SID;
731         r->in.req.gid   = gid;
732
733         do_async_ndr(mem_ctx, idmap_child(),
734                      NDR_WINBIND_GET_IDMAP, r,
735                      winbindd_gid2sid_recv, r,
736                      (void *)cont, private_data);
737         return;
738 nomem:
739         TALLOC_FREE(r);
740         cont(private_data, false, NULL);
741         return;
742 }
743
744 static const struct winbindd_child_dispatch_table idmap_dispatch_table[] = {
745         {
746                 .name           = "DUAL_SET_MAPPING",
747                 .struct_cmd     = WINBINDD_DUAL_SET_MAPPING,
748                 .struct_fn      = winbindd_dual_set_mapping,
749         },{
750                 .name           = "DUAL_SET_HWMS",
751                 .struct_cmd     = WINBINDD_DUAL_SET_HWM,
752                 .struct_fn      = winbindd_dual_set_hwm,
753         },{
754                 .name           = "ALLOCATE_UID",
755                 .struct_cmd     = WINBINDD_ALLOCATE_UID,
756                 .struct_fn      = winbindd_dual_allocate_uid,
757         },{
758                 .name           = "ALLOCATE_GID",
759                 .struct_cmd     = WINBINDD_ALLOCATE_GID,
760                 .struct_fn      = winbindd_dual_allocate_gid,
761         },{
762                 .name           = "NDR_WINBIND_GET_IDMAP",
763                 .ndr_opnum      = NDR_WINBIND_GET_IDMAP,
764                 .ndr_fn         = winbindd_ndr_child_get_idmap,
765         },{
766                 .name           = "NDR_WINBIND_SET_IDMAP",
767                 .ndr_opnum      = NDR_WINBIND_SET_IDMAP,
768                 .ndr_fn         = winbindd_ndr_child_set_idmap,
769         },{
770                 .name           = NULL,
771         }
772 };