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 winbindd_set_mapping_recv(TALLOC_CTX *mem_ctx, bool success,
227 struct winbindd_response *response,
228 void *c, void *private_data)
230 void (*cont)(void *priv, bool succ) = (void (*)(void *, bool))c;
233 DEBUG(5, ("Could not trigger idmap_set_mapping\n"));
234 cont(private_data, False);
238 if (response->result != WINBINDD_OK) {
239 DEBUG(5, ("idmap_set_mapping returned an error\n"));
240 cont(private_data, False);
244 cont(private_data, True);
247 void winbindd_set_mapping_async(TALLOC_CTX *mem_ctx, const struct id_map *map,
248 void (*cont)(void *private_data, bool success),
251 struct winbindd_request request;
252 ZERO_STRUCT(request);
253 request.cmd = WINBINDD_DUAL_SET_MAPPING;
254 request.data.dual_idmapset.id = map->xid.id;
255 request.data.dual_idmapset.type = map->xid.type;
256 sid_to_fstring(request.data.dual_idmapset.sid, map->sid);
258 do_async(mem_ctx, idmap_child(), &request, winbindd_set_mapping_recv,
259 (void *)cont, private_data);
262 enum winbindd_result winbindd_dual_set_mapping(struct winbindd_domain *domain,
263 struct winbindd_cli_state *state)
269 DEBUG(3, ("[%5lu]: dual_idmapset\n", (unsigned long)state->pid));
271 if (!string_to_sid(&sid, state->request.data.dual_idmapset.sid))
272 return WINBINDD_ERROR;
275 map.xid.id = state->request.data.dual_idmapset.id;
276 map.xid.type = state->request.data.dual_idmapset.type;
277 map.status = ID_MAPPED;
279 result = idmap_set_mapping(&map);
280 return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
283 static void winbindd_set_hwm_recv(TALLOC_CTX *mem_ctx, bool success,
284 struct winbindd_response *response,
285 void *c, void *private_data)
287 void (*cont)(void *priv, bool succ) = (void (*)(void *, bool))c;
290 DEBUG(5, ("Could not trigger idmap_set_hwm\n"));
291 cont(private_data, False);
295 if (response->result != WINBINDD_OK) {
296 DEBUG(5, ("idmap_set_hwm returned an error\n"));
297 cont(private_data, False);
301 cont(private_data, True);
304 void winbindd_set_hwm_async(TALLOC_CTX *mem_ctx, const struct unixid *xid,
305 void (*cont)(void *private_data, bool success),
308 struct winbindd_request request;
309 ZERO_STRUCT(request);
310 request.cmd = WINBINDD_DUAL_SET_HWM;
311 request.data.dual_idmapset.id = xid->id;
312 request.data.dual_idmapset.type = xid->type;
314 do_async(mem_ctx, idmap_child(), &request, winbindd_set_hwm_recv,
315 (void *)cont, private_data);
318 enum winbindd_result winbindd_dual_set_hwm(struct winbindd_domain *domain,
319 struct winbindd_cli_state *state)
324 DEBUG(3, ("[%5lu]: dual_set_hwm\n", (unsigned long)state->pid));
326 xid.id = state->request.data.dual_idmapset.id;
327 xid.type = state->request.data.dual_idmapset.type;
331 result = idmap_set_uid_hwm(&xid);
334 result = idmap_set_gid_hwm(&xid);
337 return WINBINDD_ERROR;
339 return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
342 static void winbindd_sid2uid_recv(TALLOC_CTX *mem_ctx, bool success,
343 struct winbindd_ndr_call *c,
348 void (*cont)(void *priv, bool succ, uid_t) =
349 (void (*)(void *, bool, uid_t))_cont;
350 struct winbind_get_idmap *r =
351 talloc_get_type_abort(_r, struct winbind_get_idmap);
355 DEBUG(5, ("Could not get_idmap(sid2uid)\n"));
357 cont(private_data, false, (uid_t)-1);
361 if (r->out.result != WINBIND_STATUS_OK) {
362 DEBUG(5, ("get_idmap(sid2uid) returned an error:0x%08X\n",
365 cont(private_data, false, (uid_t)-1);
369 if (r->out.rep->uid > UINT32_MAX) {
370 DEBUG(1, ("get_idmap(sid2uid) returned a 64bit uid %llu\n",
371 (unsigned long long)r->out.rep->uid));
373 cont(private_data, false, (uid_t)-1);
377 uid = r->out.rep->uid;
379 cont(private_data, true, uid);
382 void winbindd_sid2uid_async(TALLOC_CTX *mem_ctx, const DOM_SID *sid,
383 void (*cont)(void *private_data, bool success, uid_t uid),
386 struct winbind_get_idmap *r = NULL;
388 DEBUG(7,("winbindd_sid2uid_async: Resolving %s to a uid\n",
389 sid_string_tos(sid)));
391 r = TALLOC_P(mem_ctx, struct winbind_get_idmap);
393 r->in.level = TALLOC_P(r, enum winbind_get_idmap_level);
394 if (!r->in.level) goto nomem;
396 *r->in.level = WINBIND_IDMAP_LEVEL_SID_TO_UID;
397 r->in.req.sid = discard_const(sid);
399 do_async_ndr(mem_ctx, idmap_child(),
400 NDR_WINBIND_GET_IDMAP, r,
401 winbindd_sid2uid_recv, r,
402 (void *)cont, private_data);
406 cont(private_data, false, (uid_t)-1);
410 static void winbindd_sid2gid_recv(TALLOC_CTX *mem_ctx, bool success,
411 struct winbindd_response *response,
412 void *c, void *private_data)
414 void (*cont)(void *priv, bool succ, gid_t gid) =
415 (void (*)(void *, bool, gid_t))c;
418 DEBUG(5, ("Could not trigger sid2gid\n"));
419 cont(private_data, False, 0);
423 if (response->result != WINBINDD_OK) {
424 DEBUG(5, ("sid2gid returned an error\n"));
425 cont(private_data, False, 0);
429 cont(private_data, True, response->data.gid);
432 void winbindd_sid2gid_async(TALLOC_CTX *mem_ctx, const DOM_SID *sid,
433 void (*cont)(void *private_data, bool success, gid_t gid),
436 struct winbindd_request request;
437 ZERO_STRUCT(request);
438 request.cmd = WINBINDD_DUAL_SID2GID;
439 sid_to_fstring(request.data.dual_sid2id.sid, sid);
441 DEBUG(7,("winbindd_sid2gid_async: Resolving %s to a gid\n",
442 request.data.dual_sid2id.sid));
444 do_async(mem_ctx, idmap_child(), &request, winbindd_sid2gid_recv,
445 (void *)cont, private_data);
448 enum winbindd_result winbindd_dual_sid2gid(struct winbindd_domain *domain,
449 struct winbindd_cli_state *state)
454 DEBUG(3, ("[%5lu]: sid to gid %s\n", (unsigned long)state->pid,
455 state->request.data.dual_sid2id.sid));
457 if (!string_to_sid(&sid, state->request.data.dual_sid2id.sid)) {
458 DEBUG(1, ("Could not get convert sid %s from string\n",
459 state->request.data.dual_sid2id.sid));
460 return WINBINDD_ERROR;
463 /* Find gid for this sid and return it, possibly ask the slow remote idmap */
465 result = idmap_sid_to_gid(&sid, &state->response.data.gid);
467 DEBUG(10, ("winbindd_dual_sid2gid: 0x%08x - %s - %u\n",
468 NT_STATUS_V(result), sid_string_dbg(&sid),
469 state->response.data.gid));
471 return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
474 /* The following uid2sid/gid2sid functions has been contributed by
475 * Keith Reynolds <Keith.Reynolds@centrify.com> */
477 static void winbindd_uid2sid_recv(TALLOC_CTX *mem_ctx, bool success,
478 struct winbindd_response *response,
479 void *c, void *private_data)
481 void (*cont)(void *priv, bool succ, const char *sid) =
482 (void (*)(void *, bool, const char *))c;
485 DEBUG(5, ("Could not trigger uid2sid\n"));
486 cont(private_data, False, NULL);
490 if (response->result != WINBINDD_OK) {
491 DEBUG(5, ("uid2sid returned an error\n"));
492 cont(private_data, False, NULL);
496 cont(private_data, True, response->data.sid.sid);
499 void winbindd_uid2sid_async(TALLOC_CTX *mem_ctx, uid_t uid,
500 void (*cont)(void *private_data, bool success, const char *sid),
503 struct winbindd_request request;
505 ZERO_STRUCT(request);
506 request.cmd = WINBINDD_DUAL_UID2SID;
507 request.data.uid = uid;
508 do_async(mem_ctx, idmap_child(), &request, winbindd_uid2sid_recv,
509 (void *)cont, private_data);
512 enum winbindd_result winbindd_dual_uid2sid(struct winbindd_domain *domain,
513 struct winbindd_cli_state *state)
518 DEBUG(3,("[%5lu]: uid to sid %lu\n",
519 (unsigned long)state->pid,
520 (unsigned long) state->request.data.uid));
522 /* Find sid for this uid and return it, possibly ask the slow remote idmap */
523 result = idmap_uid_to_sid(&sid, state->request.data.uid);
525 if (NT_STATUS_IS_OK(result)) {
526 sid_to_fstring(state->response.data.sid.sid, &sid);
527 state->response.data.sid.type = SID_NAME_USER;
531 return WINBINDD_ERROR;
534 static void winbindd_gid2sid_recv(TALLOC_CTX *mem_ctx, bool success,
535 struct winbindd_response *response,
536 void *c, void *private_data)
538 void (*cont)(void *priv, bool succ, const char *sid) =
539 (void (*)(void *, bool, const char *))c;
542 DEBUG(5, ("Could not trigger gid2sid\n"));
543 cont(private_data, False, NULL);
547 if (response->result != WINBINDD_OK) {
548 DEBUG(5, ("gid2sid returned an error\n"));
549 cont(private_data, False, NULL);
553 cont(private_data, True, response->data.sid.sid);
556 void winbindd_gid2sid_async(TALLOC_CTX *mem_ctx, gid_t gid,
557 void (*cont)(void *private_data, bool success, const char *sid),
560 struct winbindd_request request;
562 ZERO_STRUCT(request);
563 request.cmd = WINBINDD_DUAL_GID2SID;
564 request.data.gid = gid;
565 do_async(mem_ctx, idmap_child(), &request, winbindd_gid2sid_recv,
566 (void *)cont, private_data);
569 enum winbindd_result winbindd_dual_gid2sid(struct winbindd_domain *domain,
570 struct winbindd_cli_state *state)
575 DEBUG(3,("[%5lu]: gid %lu to sid\n",
576 (unsigned long)state->pid,
577 (unsigned long) state->request.data.gid));
579 /* Find sid for this gid and return it, possibly ask the slow remote idmap */
580 result = idmap_gid_to_sid(&sid, state->request.data.gid);
582 if (NT_STATUS_IS_OK(result)) {
583 sid_to_fstring(state->response.data.sid.sid, &sid);
584 DEBUG(10, ("[%5lu]: retrieved sid: %s\n",
585 (unsigned long)state->pid,
586 state->response.data.sid.sid));
587 state->response.data.sid.type = SID_NAME_DOM_GRP;
591 return WINBINDD_ERROR;
594 static const struct winbindd_child_dispatch_table idmap_dispatch_table[] = {
596 .name = "DUAL_SID2GID",
597 .struct_cmd = WINBINDD_DUAL_SID2GID,
598 .struct_fn = winbindd_dual_sid2gid,
600 .name = "DUAL_UID2SID",
601 .struct_cmd = WINBINDD_DUAL_UID2SID,
602 .struct_fn = winbindd_dual_uid2sid,
604 .name = "DUAL_GID2SID",
605 .struct_cmd = WINBINDD_DUAL_GID2SID,
606 .struct_fn = winbindd_dual_gid2sid,
608 .name = "DUAL_SET_MAPPING",
609 .struct_cmd = WINBINDD_DUAL_SET_MAPPING,
610 .struct_fn = winbindd_dual_set_mapping,
612 .name = "DUAL_SET_HWMS",
613 .struct_cmd = WINBINDD_DUAL_SET_HWM,
614 .struct_fn = winbindd_dual_set_hwm,
616 .name = "ALLOCATE_UID",
617 .struct_cmd = WINBINDD_ALLOCATE_UID,
618 .struct_fn = winbindd_dual_allocate_uid,
620 .name = "ALLOCATE_GID",
621 .struct_cmd = WINBINDD_ALLOCATE_GID,
622 .struct_fn = winbindd_dual_allocate_gid,
624 .name = "NDR_WINBIND_GET_IDMAP",
625 .ndr_opnum = NDR_WINBIND_GET_IDMAP,
626 .ndr_fn = winbindd_ndr_child_get_idmap,