2 Unix SMB/CIFS implementation.
4 Async helpers for blocking functions
6 Copyright (C) Volker Lendecke 2005
7 Copyright (C) Gerald Carter 2006
8 Copyright (C) Simo Sorce 2007
10 The helpers always consist of three functions:
12 * A request setup function that takes the necessary parameters together
13 with a continuation function that is to be called upon completion
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.
19 * A worker function that is called inside the appropriate child process.
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.
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.
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/>.
39 #define DBGC_CLASS DBGC_WINBIND
41 static const struct winbindd_child_dispatch_table idmap_dispatch_table[];
43 static struct winbindd_child static_idmap_child;
45 void init_idmap_child(void)
47 setup_child(&static_idmap_child,
49 "log.winbindd", "idmap");
52 struct winbindd_child *idmap_child(void)
54 return &static_idmap_child;
57 static void ndr_child_get_idmap_sid2uid(struct winbindd_domain *domain,
58 struct winbindd_cli_state *state,
59 struct winbind_get_idmap *r)
64 DEBUG(3, ("sid to uid '%s'\n", sid_string_tos(r->in.req.sid)));
66 /* Find uid for this sid and return it, possibly ask the slow remote idmap */
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),
73 r->out.result = WINBIND_STATUS_FOOBAR;
77 DEBUG(10, ("sid %s mapped to uid %u\n",
78 sid_string_tos(r->in.req.sid), uid));
80 r->out.rep->uid = uid;
81 r->out.result = WINBIND_STATUS_OK;
84 static void ndr_child_get_idmap_sid2gid(struct winbindd_domain *domain,
85 struct winbindd_cli_state *state,
86 struct winbind_get_idmap *r)
91 DEBUG(3, ("sid to gid '%s'\n", sid_string_tos(r->in.req.sid)));
93 /* Find uid for this sid and return it, possibly ask the slow remote idmap */
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),
100 r->out.result = WINBIND_STATUS_FOOBAR;
104 DEBUG(10, ("sid %s mapped to gid %u\n",
105 sid_string_tos(r->in.req.sid), gid));
107 r->out.rep->gid = gid;
108 r->out.result = WINBIND_STATUS_OK;
111 static void ndr_child_get_idmap_uid2sid(struct winbindd_domain *domain,
112 struct winbindd_cli_state *state,
113 struct winbind_get_idmap *r)
119 DEBUG(3, ("uid to sid '%llu'\n",
120 (unsigned long long)r->in.req.uid));
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;
130 /* Find uid for this sid and return it, possibly ask the slow remote idmap */
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;
142 DEBUG(10, ("uid %u mapped to sid %s\n",
143 uid, sid_string_tos(&sid)));
145 r->out.rep->sid = sid_dup_talloc(r, &sid);
146 if (!r->out.rep->sid) {
147 r->out.result = WINBIND_STATUS_NO_MEMORY;
151 r->out.result = WINBIND_STATUS_OK;
154 static void ndr_child_get_idmap_gid2sid(struct winbindd_domain *domain,
155 struct winbindd_cli_state *state,
156 struct winbind_get_idmap *r)
162 DEBUG(3, ("gid to sid '%llu'\n",
163 (unsigned long long)r->in.req.gid));
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;
173 /* Find uid for this sid and return it, possibly ask the slow remote idmap */
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;
184 DEBUG(10, ("gid %u mapped to sid %s\n",
185 gid, sid_string_tos(&sid)));
187 r->out.rep->sid = sid_dup_talloc(r, &sid);
188 if (!r->out.rep->sid) {
189 r->out.result = WINBIND_STATUS_NO_MEMORY;
193 r->out.result = WINBIND_STATUS_OK;
196 void winbindd_ndr_child_get_idmap(struct winbindd_domain *domain,
197 struct winbindd_cli_state *state)
199 struct winbind_get_idmap *r;
201 r = talloc_get_type_abort(state->c.ndr.r,
202 struct winbind_get_idmap);
204 switch (*r->in.level) {
205 case WINBIND_IDMAP_LEVEL_SID_TO_UID:
206 ndr_child_get_idmap_sid2uid(domain, state, r);
209 case WINBIND_IDMAP_LEVEL_SID_TO_GID:
210 ndr_child_get_idmap_sid2gid(domain, state, r);
213 case WINBIND_IDMAP_LEVEL_UID_TO_SID:
214 ndr_child_get_idmap_uid2sid(domain, state, r);
217 case WINBIND_IDMAP_LEVEL_GID_TO_SID:
218 ndr_child_get_idmap_gid2sid(domain, state, r);
222 r->out.result = WINBIND_STATUS_UNKNOWN_LEVEL;
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)
233 DEBUG(3, ("allocate_uid\n"));
235 result = idmap_allocate_uid(&xid);
236 if (!NT_STATUS_IS_OK(result)) {
237 DEBUG(1,("Can't allocate uid: %s\n",
239 r->out.result = WINBIND_STATUS_FOOBAR;
243 DEBUG(10, ("allocte_uid: %u\n",
246 r->out.rep->uid = xid.id;
248 r->out.result = WINBIND_STATUS_OK;
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)
258 DEBUG(3, ("allocate_gid\n"));
260 result = idmap_allocate_gid(&xid);
261 if (!NT_STATUS_IS_OK(result)) {
262 DEBUG(1,("Can't allocate gid: %s\n",
264 r->out.result = WINBIND_STATUS_FOOBAR;
268 DEBUG(10, ("allocte_gid: %u\n",
271 r->out.rep->gid = xid.id;
273 r->out.result = WINBIND_STATUS_OK;
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)
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));
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",
291 r->out.result = WINBIND_STATUS_FOOBAR;
295 r->out.result = WINBIND_STATUS_OK;
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)
304 DEBUG(3,("set_hwm: %u type:%u\n",
306 r->in.req.hwm.type));
308 switch (r->in.req.hwm.type) {
310 result = idmap_set_uid_hwm(&r->in.req.hwm);
313 result = idmap_set_gid_hwm(&r->in.req.hwm);
316 result = NT_STATUS_INVALID_PARAMETER;
320 if (!NT_STATUS_IS_OK(result)) {
321 DEBUG(1,("Can't set hwm: %s\n",
323 r->out.result = WINBIND_STATUS_FOOBAR;
327 r->out.result = WINBIND_STATUS_OK;
330 void winbindd_ndr_child_set_idmap(struct winbindd_domain *domain,
331 struct winbindd_cli_state *state)
333 struct winbind_set_idmap *r;
335 r = talloc_get_type_abort(state->c.ndr.r,
336 struct winbind_set_idmap);
338 switch (*r->in.level) {
339 case WINBIND_SET_IDMAP_LEVEL_ALLOCATE_UID:
340 ndr_child_set_idmap_allocate_uid(domain, state, r);
343 case WINBIND_SET_IDMAP_LEVEL_ALLOCATE_GID:
344 ndr_child_set_idmap_allocate_gid(domain, state, r);
347 case WINBIND_SET_IDMAP_LEVEL_SET_MAPPING:
348 ndr_child_set_idmap_set_mapping(domain, state, r);
351 case WINBIND_SET_IDMAP_LEVEL_SET_HWM:
352 ndr_child_set_idmap_set_hwm(domain, state, r);
356 r->out.result = WINBIND_STATUS_UNKNOWN_LEVEL;
360 static void winbindd_set_mapping_recv(TALLOC_CTX *mem_ctx, bool success,
361 struct winbindd_response *response,
362 void *c, void *private_data)
364 void (*cont)(void *priv, bool succ) = (void (*)(void *, bool))c;
367 DEBUG(5, ("Could not trigger idmap_set_mapping\n"));
368 cont(private_data, False);
372 if (response->result != WINBINDD_OK) {
373 DEBUG(5, ("idmap_set_mapping returned an error\n"));
374 cont(private_data, False);
378 cont(private_data, True);
381 void winbindd_set_mapping_async(TALLOC_CTX *mem_ctx, const struct id_map *map,
382 void (*cont)(void *private_data, bool success),
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);
392 do_async(mem_ctx, idmap_child(), &request, winbindd_set_mapping_recv,
393 (void *)cont, private_data);
396 enum winbindd_result winbindd_dual_set_mapping(struct winbindd_domain *domain,
397 struct winbindd_cli_state *state)
403 DEBUG(3, ("[%5lu]: dual_idmapset\n", (unsigned long)state->pid));
405 if (!string_to_sid(&sid, state->request.data.dual_idmapset.sid))
406 return WINBINDD_ERROR;
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;
413 result = idmap_set_mapping(&map);
414 return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
417 static void winbindd_set_hwm_recv(TALLOC_CTX *mem_ctx, bool success,
418 struct winbindd_response *response,
419 void *c, void *private_data)
421 void (*cont)(void *priv, bool succ) = (void (*)(void *, bool))c;
424 DEBUG(5, ("Could not trigger idmap_set_hwm\n"));
425 cont(private_data, False);
429 if (response->result != WINBINDD_OK) {
430 DEBUG(5, ("idmap_set_hwm returned an error\n"));
431 cont(private_data, False);
435 cont(private_data, True);
438 void winbindd_set_hwm_async(TALLOC_CTX *mem_ctx, const struct unixid *xid,
439 void (*cont)(void *private_data, bool success),
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;
448 do_async(mem_ctx, idmap_child(), &request, winbindd_set_hwm_recv,
449 (void *)cont, private_data);
452 enum winbindd_result winbindd_dual_set_hwm(struct winbindd_domain *domain,
453 struct winbindd_cli_state *state)
458 DEBUG(3, ("[%5lu]: dual_set_hwm\n", (unsigned long)state->pid));
460 xid.id = state->request.data.dual_idmapset.id;
461 xid.type = state->request.data.dual_idmapset.type;
465 result = idmap_set_uid_hwm(&xid);
468 result = idmap_set_gid_hwm(&xid);
471 return WINBINDD_ERROR;
473 return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
476 static void winbindd_sid2uid_recv(TALLOC_CTX *mem_ctx, bool success,
477 struct winbindd_ndr_call *c,
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);
489 DEBUG(5, ("Could not get_idmap(sid2uid)\n"));
491 cont(private_data, false, (uid_t)-1);
495 if (r->out.result != WINBIND_STATUS_OK) {
496 DEBUG(5, ("get_idmap(sid2uid) returned an error:0x%08X\n",
499 cont(private_data, false, (uid_t)-1);
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));
507 cont(private_data, false, (uid_t)-1);
511 uid = r->out.rep->uid;
513 cont(private_data, true, uid);
516 void winbindd_sid2uid_async(TALLOC_CTX *mem_ctx, const DOM_SID *sid,
517 void (*cont)(void *private_data, bool success, uid_t uid),
520 struct winbind_get_idmap *r = NULL;
522 DEBUG(7,("winbindd_sid2uid_async: Resolving %s to a uid\n",
523 sid_string_tos(sid)));
525 r = TALLOC_P(mem_ctx, struct winbind_get_idmap);
527 r->in.level = TALLOC_P(r, enum winbind_get_idmap_level);
528 if (!r->in.level) goto nomem;
530 *r->in.level = WINBIND_IDMAP_LEVEL_SID_TO_UID;
531 r->in.req.sid = discard_const(sid);
533 do_async_ndr(mem_ctx, idmap_child(),
534 NDR_WINBIND_GET_IDMAP, r,
535 winbindd_sid2uid_recv, r,
536 (void *)cont, private_data);
540 cont(private_data, false, (uid_t)-1);
544 static void winbindd_sid2gid_recv(TALLOC_CTX *mem_ctx, bool success,
545 struct winbindd_ndr_call *c,
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);
557 DEBUG(5, ("Could not get_idmap(sid2gid)\n"));
559 cont(private_data, false, (gid_t)-1);
563 if (r->out.result != WINBIND_STATUS_OK) {
564 DEBUG(5, ("get_idmap(sid2gid) returned an error:0x%08X\n",
567 cont(private_data, false, (gid_t)-1);
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));
575 cont(private_data, false, (gid_t)-1);
579 gid = r->out.rep->gid;
581 cont(private_data, true, gid);
584 void winbindd_sid2gid_async(TALLOC_CTX *mem_ctx, const DOM_SID *sid,
585 void (*cont)(void *private_data, bool success, gid_t gid),
588 struct winbind_get_idmap *r = NULL;
590 DEBUG(7,("winbindd_sid2gid_async: Resolving %s to a gid\n",
591 sid_string_tos(sid)));
593 r = TALLOC_P(mem_ctx, struct winbind_get_idmap);
595 r->in.level = TALLOC_P(r, enum winbind_get_idmap_level);
596 if (!r->in.level) goto nomem;
598 *r->in.level = WINBIND_IDMAP_LEVEL_SID_TO_GID;
599 r->in.req.sid = discard_const(sid);
601 do_async_ndr(mem_ctx, idmap_child(),
602 NDR_WINBIND_GET_IDMAP, r,
603 winbindd_sid2gid_recv, r,
604 (void *)cont, private_data);
608 cont(private_data, false, (gid_t)-1);
612 static void winbindd_uid2sid_recv(TALLOC_CTX *mem_ctx, bool success,
613 struct winbindd_ndr_call *c,
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);
625 DEBUG(5, ("Could not get_idmap(uid2sid)\n"));
627 cont(private_data, false, NULL);
631 if (r->out.result != WINBIND_STATUS_OK) {
632 DEBUG(5, ("get_idmap(uid2sid) returned an error:0x%08X\n",
635 cont(private_data, false, NULL);
639 sid = dom_sid_string(mem_ctx, r->out.rep->sid);
642 cont(private_data, false, NULL);
647 cont(private_data, true, sid);
650 void winbindd_uid2sid_async(TALLOC_CTX *mem_ctx, uid_t uid,
651 void (*cont)(void *private_data, bool success, const char *sid),
654 struct winbind_get_idmap *r = NULL;
656 DEBUG(7,("winbindd_uid2sid_async: Resolving %u to a sid\n",
659 r = TALLOC_P(mem_ctx, struct winbind_get_idmap);
661 r->in.level = TALLOC_P(r, enum winbind_get_idmap_level);
662 if (!r->in.level) goto nomem;
664 *r->in.level = WINBIND_IDMAP_LEVEL_UID_TO_SID;
667 do_async_ndr(mem_ctx, idmap_child(),
668 NDR_WINBIND_GET_IDMAP, r,
669 winbindd_uid2sid_recv, r,
670 (void *)cont, private_data);
674 cont(private_data, false, NULL);
678 static void winbindd_gid2sid_recv(TALLOC_CTX *mem_ctx, bool success,
679 struct winbindd_ndr_call *c,
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);
691 DEBUG(5, ("Could not get_idmap(gid2sid)\n"));
693 cont(private_data, false, NULL);
697 if (r->out.result != WINBIND_STATUS_OK) {
698 DEBUG(5, ("get_idmap(gid2sid) returned an error:0x%08X\n",
701 cont(private_data, false, NULL);
705 sid = dom_sid_string(mem_ctx, r->out.rep->sid);
708 cont(private_data, false, NULL);
713 cont(private_data, true, sid);
716 void winbindd_gid2sid_async(TALLOC_CTX *mem_ctx, gid_t gid,
717 void (*cont)(void *private_data, bool success, const char *sid),
720 struct winbind_get_idmap *r = NULL;
722 DEBUG(7,("winbindd_gid2sid_async: Resolving %u to a sid\n",
725 r = TALLOC_P(mem_ctx, struct winbind_get_idmap);
727 r->in.level = TALLOC_P(r, enum winbind_get_idmap_level);
728 if (!r->in.level) goto nomem;
730 *r->in.level = WINBIND_IDMAP_LEVEL_GID_TO_SID;
733 do_async_ndr(mem_ctx, idmap_child(),
734 NDR_WINBIND_GET_IDMAP, r,
735 winbindd_gid2sid_recv, r,
736 (void *)cont, private_data);
740 cont(private_data, false, NULL);
744 static const struct winbindd_child_dispatch_table idmap_dispatch_table[] = {
746 .name = "DUAL_SET_MAPPING",
747 .struct_cmd = WINBINDD_DUAL_SET_MAPPING,
748 .struct_fn = winbindd_dual_set_mapping,
750 .name = "DUAL_SET_HWMS",
751 .struct_cmd = WINBINDD_DUAL_SET_HWM,
752 .struct_fn = winbindd_dual_set_hwm,
754 .name = "ALLOCATE_UID",
755 .struct_cmd = WINBINDD_ALLOCATE_UID,
756 .struct_fn = winbindd_dual_allocate_uid,
758 .name = "ALLOCATE_GID",
759 .struct_cmd = WINBINDD_ALLOCATE_GID,
760 .struct_fn = winbindd_dual_allocate_gid,
762 .name = "NDR_WINBIND_GET_IDMAP",
763 .ndr_opnum = NDR_WINBIND_GET_IDMAP,
764 .ndr_fn = winbindd_ndr_child_get_idmap,
766 .name = "NDR_WINBIND_SET_IDMAP",
767 .ndr_opnum = NDR_WINBIND_SET_IDMAP,
768 .ndr_fn = winbindd_ndr_child_set_idmap,