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_hwm_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_hwm\n"));
368 cont(private_data, False);
372 if (response->result != WINBINDD_OK) {
373 DEBUG(5, ("idmap_set_hwm returned an error\n"));
374 cont(private_data, False);
378 cont(private_data, True);
381 void winbindd_set_hwm_async(TALLOC_CTX *mem_ctx, const struct unixid *xid,
382 void (*cont)(void *private_data, bool success),
385 struct winbindd_request request;
386 ZERO_STRUCT(request);
387 request.cmd = WINBINDD_DUAL_SET_HWM;
388 request.data.dual_idmapset.id = xid->id;
389 request.data.dual_idmapset.type = xid->type;
391 do_async(mem_ctx, idmap_child(), &request, winbindd_set_hwm_recv,
392 (void *)cont, private_data);
395 enum winbindd_result winbindd_dual_set_hwm(struct winbindd_domain *domain,
396 struct winbindd_cli_state *state)
401 DEBUG(3, ("[%5lu]: dual_set_hwm\n", (unsigned long)state->pid));
403 xid.id = state->request.data.dual_idmapset.id;
404 xid.type = state->request.data.dual_idmapset.type;
408 result = idmap_set_uid_hwm(&xid);
411 result = idmap_set_gid_hwm(&xid);
414 return WINBINDD_ERROR;
416 return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
419 static void winbindd_sid2uid_recv(TALLOC_CTX *mem_ctx, bool success,
420 struct winbindd_ndr_call *c,
425 void (*cont)(void *priv, bool succ, uid_t) =
426 (void (*)(void *, bool, uid_t))_cont;
427 struct winbind_get_idmap *r =
428 talloc_get_type_abort(_r, struct winbind_get_idmap);
432 DEBUG(5, ("Could not get_idmap(sid2uid)\n"));
434 cont(private_data, false, (uid_t)-1);
438 if (r->out.result != WINBIND_STATUS_OK) {
439 DEBUG(5, ("get_idmap(sid2uid) returned an error:0x%08X\n",
442 cont(private_data, false, (uid_t)-1);
446 if (r->out.rep->uid > UINT32_MAX) {
447 DEBUG(1, ("get_idmap(sid2uid) returned a 64bit uid %llu\n",
448 (unsigned long long)r->out.rep->uid));
450 cont(private_data, false, (uid_t)-1);
454 uid = r->out.rep->uid;
456 cont(private_data, true, uid);
459 void winbindd_sid2uid_async(TALLOC_CTX *mem_ctx, const DOM_SID *sid,
460 void (*cont)(void *private_data, bool success, uid_t uid),
463 struct winbind_get_idmap *r = NULL;
465 DEBUG(7,("winbindd_sid2uid_async: Resolving %s to a uid\n",
466 sid_string_tos(sid)));
468 r = TALLOC_P(mem_ctx, struct winbind_get_idmap);
470 r->in.level = TALLOC_P(r, enum winbind_get_idmap_level);
471 if (!r->in.level) goto nomem;
473 *r->in.level = WINBIND_IDMAP_LEVEL_SID_TO_UID;
474 r->in.req.sid = discard_const(sid);
476 do_async_ndr(mem_ctx, idmap_child(),
477 NDR_WINBIND_GET_IDMAP, r,
478 winbindd_sid2uid_recv, r,
479 (void *)cont, private_data);
483 cont(private_data, false, (uid_t)-1);
487 static void winbindd_sid2gid_recv(TALLOC_CTX *mem_ctx, bool success,
488 struct winbindd_ndr_call *c,
493 void (*cont)(void *priv, bool succ, gid_t) =
494 (void (*)(void *, bool, gid_t))_cont;
495 struct winbind_get_idmap *r =
496 talloc_get_type_abort(_r, struct winbind_get_idmap);
500 DEBUG(5, ("Could not get_idmap(sid2gid)\n"));
502 cont(private_data, false, (gid_t)-1);
506 if (r->out.result != WINBIND_STATUS_OK) {
507 DEBUG(5, ("get_idmap(sid2gid) returned an error:0x%08X\n",
510 cont(private_data, false, (gid_t)-1);
514 if (r->out.rep->gid > UINT32_MAX) {
515 DEBUG(1, ("get_idmap(sid2gid) returned a 64bit gid %llu\n",
516 (unsigned long long)r->out.rep->gid));
518 cont(private_data, false, (gid_t)-1);
522 gid = r->out.rep->gid;
524 cont(private_data, true, gid);
527 void winbindd_sid2gid_async(TALLOC_CTX *mem_ctx, const DOM_SID *sid,
528 void (*cont)(void *private_data, bool success, gid_t gid),
531 struct winbind_get_idmap *r = NULL;
533 DEBUG(7,("winbindd_sid2gid_async: Resolving %s to a gid\n",
534 sid_string_tos(sid)));
536 r = TALLOC_P(mem_ctx, struct winbind_get_idmap);
538 r->in.level = TALLOC_P(r, enum winbind_get_idmap_level);
539 if (!r->in.level) goto nomem;
541 *r->in.level = WINBIND_IDMAP_LEVEL_SID_TO_GID;
542 r->in.req.sid = discard_const(sid);
544 do_async_ndr(mem_ctx, idmap_child(),
545 NDR_WINBIND_GET_IDMAP, r,
546 winbindd_sid2gid_recv, r,
547 (void *)cont, private_data);
551 cont(private_data, false, (gid_t)-1);
555 static void winbindd_uid2sid_recv(TALLOC_CTX *mem_ctx, bool success,
556 struct winbindd_ndr_call *c,
561 void (*cont)(void *priv, bool succ, const char *sid) =
562 (void (*)(void *, bool, const char *))_cont;
563 struct winbind_get_idmap *r =
564 talloc_get_type_abort(_r, struct winbind_get_idmap);
568 DEBUG(5, ("Could not get_idmap(uid2sid)\n"));
570 cont(private_data, false, NULL);
574 if (r->out.result != WINBIND_STATUS_OK) {
575 DEBUG(5, ("get_idmap(uid2sid) returned an error:0x%08X\n",
578 cont(private_data, false, NULL);
582 sid = dom_sid_string(mem_ctx, r->out.rep->sid);
585 cont(private_data, false, NULL);
590 cont(private_data, true, sid);
593 void winbindd_uid2sid_async(TALLOC_CTX *mem_ctx, uid_t uid,
594 void (*cont)(void *private_data, bool success, const char *sid),
597 struct winbind_get_idmap *r = NULL;
599 DEBUG(7,("winbindd_uid2sid_async: Resolving %u to a sid\n",
602 r = TALLOC_P(mem_ctx, struct winbind_get_idmap);
604 r->in.level = TALLOC_P(r, enum winbind_get_idmap_level);
605 if (!r->in.level) goto nomem;
607 *r->in.level = WINBIND_IDMAP_LEVEL_UID_TO_SID;
610 do_async_ndr(mem_ctx, idmap_child(),
611 NDR_WINBIND_GET_IDMAP, r,
612 winbindd_uid2sid_recv, r,
613 (void *)cont, private_data);
617 cont(private_data, false, NULL);
621 static void winbindd_gid2sid_recv(TALLOC_CTX *mem_ctx, bool success,
622 struct winbindd_ndr_call *c,
627 void (*cont)(void *priv, bool succ, const char *sid) =
628 (void (*)(void *, bool, const char *))_cont;
629 struct winbind_get_idmap *r =
630 talloc_get_type_abort(_r, struct winbind_get_idmap);
634 DEBUG(5, ("Could not get_idmap(gid2sid)\n"));
636 cont(private_data, false, NULL);
640 if (r->out.result != WINBIND_STATUS_OK) {
641 DEBUG(5, ("get_idmap(gid2sid) returned an error:0x%08X\n",
644 cont(private_data, false, NULL);
648 sid = dom_sid_string(mem_ctx, r->out.rep->sid);
651 cont(private_data, false, NULL);
656 cont(private_data, true, sid);
659 void winbindd_gid2sid_async(TALLOC_CTX *mem_ctx, gid_t gid,
660 void (*cont)(void *private_data, bool success, const char *sid),
663 struct winbind_get_idmap *r = NULL;
665 DEBUG(7,("winbindd_gid2sid_async: Resolving %u to a sid\n",
668 r = TALLOC_P(mem_ctx, struct winbind_get_idmap);
670 r->in.level = TALLOC_P(r, enum winbind_get_idmap_level);
671 if (!r->in.level) goto nomem;
673 *r->in.level = WINBIND_IDMAP_LEVEL_GID_TO_SID;
676 do_async_ndr(mem_ctx, idmap_child(),
677 NDR_WINBIND_GET_IDMAP, r,
678 winbindd_gid2sid_recv, r,
679 (void *)cont, private_data);
683 cont(private_data, false, NULL);
687 static const struct winbindd_child_dispatch_table idmap_dispatch_table[] = {
689 .name = "DUAL_SET_HWMS",
690 .struct_cmd = WINBINDD_DUAL_SET_HWM,
691 .struct_fn = winbindd_dual_set_hwm,
693 .name = "NDR_WINBIND_GET_IDMAP",
694 .ndr_opnum = NDR_WINBIND_GET_IDMAP,
695 .ndr_fn = winbindd_ndr_child_get_idmap,
697 .name = "NDR_WINBIND_SET_IDMAP",
698 .ndr_opnum = NDR_WINBIND_SET_IDMAP,
699 .ndr_fn = winbindd_ndr_child_set_idmap,