2 Unix SMB/CIFS implementation.
4 Async helpers for blocking functions
6 Copyright (C) Volker Lendecke 2005
7 Copyright (C) Gerald Carter 2006
9 The helpers always consist of three functions:
11 * A request setup function that takes the necessary parameters together
12 with a continuation function that is to be called upon completion
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.
18 * A worker function that is called inside the appropriate child process.
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.
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.
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/>.
38 #define DBGC_CLASS DBGC_WINBIND
40 static void winbindd_set_mapping_recv(TALLOC_CTX *mem_ctx, BOOL success,
41 struct winbindd_response *response,
42 void *c, void *private_data)
44 void (*cont)(void *priv, BOOL succ) = (void (*)(void *, BOOL))c;
47 DEBUG(5, ("Could not trigger idmap_set_mapping\n"));
48 cont(private_data, False);
52 if (response->result != WINBINDD_OK) {
53 DEBUG(5, ("idmap_set_mapping returned an error\n"));
54 cont(private_data, False);
58 cont(private_data, True);
61 void winbindd_set_mapping_async(TALLOC_CTX *mem_ctx, const struct id_map *map,
62 void (*cont)(void *private_data, BOOL success),
65 struct winbindd_request request;
67 request.cmd = WINBINDD_DUAL_SET_MAPPING;
68 request.data.dual_idmapset.id = map->xid.id;
69 request.data.dual_idmapset.type = map->xid.type;
70 sid_to_string(request.data.dual_idmapset.sid, map->sid);
72 do_async(mem_ctx, idmap_child(), &request, winbindd_set_mapping_recv,
73 (void *)cont, private_data);
76 enum winbindd_result winbindd_dual_set_mapping(struct winbindd_domain *domain,
77 struct winbindd_cli_state *state)
83 DEBUG(3, ("[%5lu]: dual_idmapset\n", (unsigned long)state->pid));
85 if (!string_to_sid(&sid, state->request.data.dual_idmapset.sid))
86 return WINBINDD_ERROR;
89 map.xid.id = state->request.data.dual_idmapset.id;
90 map.xid.type = state->request.data.dual_idmapset.type;
91 map.status = ID_MAPPED;
93 result = idmap_set_mapping(&map);
94 return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
97 static void winbindd_set_hwm_recv(TALLOC_CTX *mem_ctx, BOOL success,
98 struct winbindd_response *response,
99 void *c, void *private_data)
101 void (*cont)(void *priv, BOOL succ) = (void (*)(void *, BOOL))c;
104 DEBUG(5, ("Could not trigger idmap_set_hwm\n"));
105 cont(private_data, False);
109 if (response->result != WINBINDD_OK) {
110 DEBUG(5, ("idmap_set_hwm returned an error\n"));
111 cont(private_data, False);
115 cont(private_data, True);
118 void winbindd_set_hwm_async(TALLOC_CTX *mem_ctx, const struct unixid *xid,
119 void (*cont)(void *private_data, BOOL success),
122 struct winbindd_request request;
123 ZERO_STRUCT(request);
124 request.cmd = WINBINDD_DUAL_SET_HWM;
125 request.data.dual_idmapset.id = xid->id;
126 request.data.dual_idmapset.type = xid->type;
128 do_async(mem_ctx, idmap_child(), &request, winbindd_set_hwm_recv,
129 (void *)cont, private_data);
132 enum winbindd_result winbindd_dual_set_hwm(struct winbindd_domain *domain,
133 struct winbindd_cli_state *state)
138 DEBUG(3, ("[%5lu]: dual_set_hwm\n", (unsigned long)state->pid));
140 xid.id = state->request.data.dual_idmapset.id;
141 xid.type = state->request.data.dual_idmapset.type;
145 result = idmap_set_uid_hwm(&xid);
148 result = idmap_set_gid_hwm(&xid);
151 return WINBINDD_ERROR;
153 return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
156 static void winbindd_sids2xids_recv(TALLOC_CTX *mem_ctx, BOOL success,
157 struct winbindd_response *response,
158 void *c, void *private_data)
160 void (*cont)(void *priv, BOOL succ, void *, int) =
161 (void (*)(void *, BOOL, void *, int))c;
164 DEBUG(5, ("Could not trigger sids2xids\n"));
165 cont(private_data, False, NULL, 0);
169 if (response->result != WINBINDD_OK) {
170 DEBUG(5, ("sids2xids returned an error\n"));
171 cont(private_data, False, NULL, 0);
175 cont(private_data, True, response->extra_data.data, response->length - sizeof(response));
178 void winbindd_sids2xids_async(TALLOC_CTX *mem_ctx, void *sids, int size,
179 void (*cont)(void *private_data, BOOL success, void *data, int len),
182 struct winbindd_request request;
183 ZERO_STRUCT(request);
184 request.cmd = WINBINDD_DUAL_SIDS2XIDS;
185 request.extra_data.data = (char *)sids;
186 request.extra_len = size;
187 do_async(mem_ctx, idmap_child(), &request, winbindd_sids2xids_recv,
188 (void *)cont, private_data);
191 enum winbindd_result winbindd_dual_sids2xids(struct winbindd_domain *domain,
192 struct winbindd_cli_state *state)
200 DEBUG(3, ("[%5lu]: sids to unix ids\n", (unsigned long)state->pid));
202 if (state->request.extra_len == 0) {
203 DEBUG(0, ("Invalid buffer size!\n"));
204 return WINBINDD_ERROR;
207 sids = (DOM_SID *)state->request.extra_data.data;
208 num = state->request.extra_len / sizeof(DOM_SID);
210 ids = TALLOC_ZERO_ARRAY(state->mem_ctx, struct id_map *, num + 1);
212 DEBUG(0, ("Out of memory!\n"));
213 return WINBINDD_ERROR;
215 for (i = 0; i < num; i++) {
216 ids[i] = TALLOC_P(ids, struct id_map);
218 DEBUG(0, ("Out of memory!\n"));
220 return WINBINDD_ERROR;
222 ids[i]->sid = &sids[i];
225 result = idmap_sids_to_unixids(ids);
227 if (NT_STATUS_IS_OK(result)) {
229 xids = SMB_MALLOC_ARRAY(struct unixid, num);
231 DEBUG(0, ("Out of memory!\n"));
233 return WINBINDD_ERROR;
236 for (i = 0; i < num; i++) {
237 if (ids[i]->status == ID_MAPPED) {
238 xids[i].type = ids[i]->xid.type;
239 xids[i].id = ids[i]->xid.id;
245 state->response.length = sizeof(state->response) + (sizeof(struct unixid) * num);
246 state->response.extra_data.data = xids;
249 DEBUG (2, ("idmap_sids_to_unixids returned an error: 0x%08x\n", NT_STATUS_V(result)));
251 return WINBINDD_ERROR;
258 static void winbindd_sid2uid_recv(TALLOC_CTX *mem_ctx, BOOL success,
259 struct winbindd_response *response,
260 void *c, void *private_data)
262 void (*cont)(void *priv, BOOL succ, uid_t uid) =
263 (void (*)(void *, BOOL, uid_t))c;
266 DEBUG(5, ("Could not trigger sid2uid\n"));
267 cont(private_data, False, 0);
271 if (response->result != WINBINDD_OK) {
272 DEBUG(5, ("sid2uid returned an error\n"));
273 cont(private_data, False, 0);
277 cont(private_data, True, response->data.uid);
280 void winbindd_sid2uid_async(TALLOC_CTX *mem_ctx, const DOM_SID *sid,
281 void (*cont)(void *private_data, BOOL success, uid_t uid),
284 struct winbindd_request request;
285 ZERO_STRUCT(request);
286 request.cmd = WINBINDD_DUAL_SID2UID;
287 sid_to_string(request.data.dual_sid2id.sid, sid);
288 do_async(mem_ctx, idmap_child(), &request, winbindd_sid2uid_recv,
289 (void *)cont, private_data);
292 enum winbindd_result winbindd_dual_sid2uid(struct winbindd_domain *domain,
293 struct winbindd_cli_state *state)
298 DEBUG(3, ("[%5lu]: sid to uid %s\n", (unsigned long)state->pid,
299 state->request.data.dual_sid2id.sid));
301 if (!string_to_sid(&sid, state->request.data.dual_sid2id.sid)) {
302 DEBUG(1, ("Could not get convert sid %s from string\n",
303 state->request.data.dual_sid2id.sid));
304 return WINBINDD_ERROR;
307 /* Find uid for this sid and return it, possibly ask the slow remote idmap */
309 result = idmap_sid_to_uid(&sid, &(state->response.data.uid));
311 return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
315 static void uid2name_recv(TALLOC_CTX *mem_ctx, BOOL success,
316 struct winbindd_response *response,
317 void *c, void *private_data);
319 void winbindd_uid2name_async(TALLOC_CTX *mem_ctx, uid_t uid,
320 void (*cont)(void *private_data, BOOL success,
324 struct winbindd_request request;
325 ZERO_STRUCT(request);
326 request.cmd = WINBINDD_DUAL_UID2NAME;
327 request.data.uid = uid;
328 do_async(mem_ctx, idmap_child(), &request, uid2name_recv,
329 (void *)cont, private_data);
331 #endif /* not used */
333 enum winbindd_result winbindd_dual_uid2name(struct winbindd_domain *domain,
334 struct winbindd_cli_state *state)
338 DEBUG(3, ("[%5lu]: uid2name %lu\n", (unsigned long)state->pid,
339 (unsigned long)state->request.data.uid));
341 pw = getpwuid(state->request.data.uid);
343 DEBUG(5, ("User %lu not found\n",
344 (unsigned long)state->request.data.uid));
345 return WINBINDD_ERROR;
348 fstrcpy(state->response.data.name.name, pw->pw_name);
353 static void uid2name_recv(TALLOC_CTX *mem_ctx, BOOL success,
354 struct winbindd_response *response,
355 void *c, void *private_data)
357 void (*cont)(void *priv, BOOL succ, const char *name) =
358 (void (*)(void *, BOOL, const char *))c;
361 DEBUG(5, ("Could not trigger uid2name\n"));
362 cont(private_data, False, NULL);
366 if (response->result != WINBINDD_OK) {
367 DEBUG(5, ("uid2name returned an error\n"));
368 cont(private_data, False, NULL);
372 cont(private_data, True, response->data.name.name);
375 static void name2uid_recv(TALLOC_CTX *mem_ctx, BOOL success,
376 struct winbindd_response *response,
377 void *c, void *private_data);
379 static void winbindd_name2uid_async(TALLOC_CTX *mem_ctx, const char *name,
380 void (*cont)(void *private_data, BOOL success,
384 struct winbindd_request request;
385 ZERO_STRUCT(request);
386 request.cmd = WINBINDD_DUAL_NAME2UID;
387 fstrcpy(request.data.username, name);
388 do_async(mem_ctx, idmap_child(), &request, name2uid_recv,
389 (void *)cont, private_data);
391 #endif /* not used */
393 enum winbindd_result winbindd_dual_name2uid(struct winbindd_domain *domain,
394 struct winbindd_cli_state *state)
398 /* Ensure null termination */
399 state->request.data.username
400 [sizeof(state->request.data.username)-1] = '\0';
402 DEBUG(3, ("[%5lu]: name2uid %s\n", (unsigned long)state->pid,
403 state->request.data.username));
405 pw = getpwnam(state->request.data.username);
407 return WINBINDD_ERROR;
410 state->response.data.uid = pw->pw_uid;
415 static void name2uid_recv(TALLOC_CTX *mem_ctx, BOOL success,
416 struct winbindd_response *response,
417 void *c, void *private_data)
419 void (*cont)(void *priv, BOOL succ, uid_t uid) =
420 (void (*)(void *, BOOL, uid_t))c;
423 DEBUG(5, ("Could not trigger name2uid\n"));
424 cont(private_data, False, 0);
428 if (response->result != WINBINDD_OK) {
429 DEBUG(5, ("name2uid returned an error\n"));
430 cont(private_data, False, 0);
434 cont(private_data, True, response->data.uid);
436 #endif /* not used */
438 static void winbindd_sid2gid_recv(TALLOC_CTX *mem_ctx, BOOL success,
439 struct winbindd_response *response,
440 void *c, void *private_data)
442 void (*cont)(void *priv, BOOL succ, gid_t gid) =
443 (void (*)(void *, BOOL, gid_t))c;
446 DEBUG(5, ("Could not trigger sid2gid\n"));
447 cont(private_data, False, 0);
451 if (response->result != WINBINDD_OK) {
452 DEBUG(5, ("sid2gid returned an error\n"));
453 cont(private_data, False, 0);
457 cont(private_data, True, response->data.gid);
460 void winbindd_sid2gid_async(TALLOC_CTX *mem_ctx, const DOM_SID *sid,
461 void (*cont)(void *private_data, BOOL success, gid_t gid),
464 struct winbindd_request request;
465 ZERO_STRUCT(request);
466 request.cmd = WINBINDD_DUAL_SID2GID;
467 sid_to_string(request.data.dual_sid2id.sid, sid);
469 DEBUG(7,("winbindd_sid2gid_async: Resolving %s to a gid\n",
470 request.data.dual_sid2id.sid));
472 do_async(mem_ctx, idmap_child(), &request, winbindd_sid2gid_recv,
473 (void *)cont, private_data);
476 enum winbindd_result winbindd_dual_sid2gid(struct winbindd_domain *domain,
477 struct winbindd_cli_state *state)
482 DEBUG(3, ("[%5lu]: sid to gid %s\n", (unsigned long)state->pid,
483 state->request.data.dual_sid2id.sid));
485 if (!string_to_sid(&sid, state->request.data.dual_sid2id.sid)) {
486 DEBUG(1, ("Could not get convert sid %s from string\n",
487 state->request.data.dual_sid2id.sid));
488 return WINBINDD_ERROR;
491 /* Find gid for this sid and return it, possibly ask the slow remote idmap */
493 result = idmap_sid_to_gid(&sid, &state->response.data.gid);
495 DEBUG(10, ("winbindd_dual_sid2gid: 0x%08x - %s - %u\n", NT_STATUS_V(result), sid_string_static(&sid), state->response.data.gid));
497 return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
500 static void gid2name_recv(TALLOC_CTX *mem_ctx, BOOL success,
501 struct winbindd_response *response,
502 void *c, void *private_data)
504 void (*cont)(void *priv, BOOL succ, const char *name) =
505 (void (*)(void *, BOOL, const char *))c;
508 DEBUG(5, ("Could not trigger gid2name\n"));
509 cont(private_data, False, NULL);
513 if (response->result != WINBINDD_OK) {
514 DEBUG(5, ("gid2name returned an error\n"));
515 cont(private_data, False, NULL);
519 cont(private_data, True, response->data.name.name);
522 void winbindd_gid2name_async(TALLOC_CTX *mem_ctx, gid_t gid,
523 void (*cont)(void *private_data, BOOL success,
527 struct winbindd_request request;
528 ZERO_STRUCT(request);
529 request.cmd = WINBINDD_DUAL_GID2NAME;
530 request.data.gid = gid;
531 do_async(mem_ctx, idmap_child(), &request, gid2name_recv,
532 (void *)cont, private_data);
535 enum winbindd_result winbindd_dual_gid2name(struct winbindd_domain *domain,
536 struct winbindd_cli_state *state)
540 DEBUG(3, ("[%5lu]: gid2name %lu\n", (unsigned long)state->pid,
541 (unsigned long)state->request.data.gid));
543 gr = getgrgid(state->request.data.gid);
545 return WINBINDD_ERROR;
547 fstrcpy(state->response.data.name.name, gr->gr_name);
552 static void name2gid_recv(TALLOC_CTX *mem_ctx, BOOL success,
553 struct winbindd_response *response,
554 void *c, void *private_data);
556 static void winbindd_name2gid_async(TALLOC_CTX *mem_ctx, const char *name,
557 void (*cont)(void *private_data, BOOL success,
561 struct winbindd_request request;
562 ZERO_STRUCT(request);
563 request.cmd = WINBINDD_DUAL_NAME2GID;
564 fstrcpy(request.data.groupname, name);
565 do_async(mem_ctx, idmap_child(), &request, name2gid_recv,
566 (void *)cont, private_data);
568 #endif /* not used */
570 enum winbindd_result winbindd_dual_name2gid(struct winbindd_domain *domain,
571 struct winbindd_cli_state *state)
575 /* Ensure null termination */
576 state->request.data.groupname
577 [sizeof(state->request.data.groupname)-1] = '\0';
579 DEBUG(3, ("[%5lu]: name2gid %s\n", (unsigned long)state->pid,
580 state->request.data.groupname));
582 gr = getgrnam(state->request.data.groupname);
584 return WINBINDD_ERROR;
587 state->response.data.gid = gr->gr_gid;
592 static void name2gid_recv(TALLOC_CTX *mem_ctx, BOOL success,
593 struct winbindd_response *response,
594 void *c, void *private_data)
596 void (*cont)(void *priv, BOOL succ, gid_t gid) =
597 (void (*)(void *, BOOL, gid_t))c;
600 DEBUG(5, ("Could not trigger name2gid\n"));
601 cont(private_data, False, 0);
605 if (response->result != WINBINDD_OK) {
606 DEBUG(5, ("name2gid returned an error\n"));
607 cont(private_data, False, 0);
611 cont(private_data, True, response->data.gid);
613 #endif /* not used */
615 /* The following uid2sid/gid2sid functions has been contributed by
616 * Keith Reynolds <Keith.Reynolds@centrify.com> */
618 static void winbindd_uid2sid_recv(TALLOC_CTX *mem_ctx, BOOL success,
619 struct winbindd_response *response,
620 void *c, void *private_data)
622 void (*cont)(void *priv, BOOL succ, const char *sid) =
623 (void (*)(void *, BOOL, const char *))c;
626 DEBUG(5, ("Could not trigger uid2sid\n"));
627 cont(private_data, False, NULL);
631 if (response->result != WINBINDD_OK) {
632 DEBUG(5, ("uid2sid returned an error\n"));
633 cont(private_data, False, NULL);
637 cont(private_data, True, response->data.sid.sid);
640 void winbindd_uid2sid_async(TALLOC_CTX *mem_ctx, uid_t uid,
641 void (*cont)(void *private_data, BOOL success, const char *sid),
644 struct winbindd_request request;
646 ZERO_STRUCT(request);
647 request.cmd = WINBINDD_DUAL_UID2SID;
648 request.data.uid = uid;
649 do_async(mem_ctx, idmap_child(), &request, winbindd_uid2sid_recv,
650 (void *)cont, private_data);
653 enum winbindd_result winbindd_dual_uid2sid(struct winbindd_domain *domain,
654 struct winbindd_cli_state *state)
659 DEBUG(3,("[%5lu]: uid to sid %lu\n",
660 (unsigned long)state->pid,
661 (unsigned long) state->request.data.uid));
663 /* Find sid for this uid and return it, possibly ask the slow remote idmap */
664 result = idmap_uid_to_sid(&sid, state->request.data.uid);
666 if (NT_STATUS_IS_OK(result)) {
667 sid_to_string(state->response.data.sid.sid, &sid);
668 state->response.data.sid.type = SID_NAME_USER;
672 return WINBINDD_ERROR;
675 static void winbindd_gid2sid_recv(TALLOC_CTX *mem_ctx, BOOL success,
676 struct winbindd_response *response,
677 void *c, void *private_data)
679 void (*cont)(void *priv, BOOL succ, const char *sid) =
680 (void (*)(void *, BOOL, const char *))c;
683 DEBUG(5, ("Could not trigger gid2sid\n"));
684 cont(private_data, False, NULL);
688 if (response->result != WINBINDD_OK) {
689 DEBUG(5, ("gid2sid returned an error\n"));
690 cont(private_data, False, NULL);
694 cont(private_data, True, response->data.sid.sid);
697 void winbindd_gid2sid_async(TALLOC_CTX *mem_ctx, gid_t gid,
698 void (*cont)(void *private_data, BOOL success, const char *sid),
701 struct winbindd_request request;
703 ZERO_STRUCT(request);
704 request.cmd = WINBINDD_DUAL_GID2SID;
705 request.data.gid = gid;
706 do_async(mem_ctx, idmap_child(), &request, winbindd_gid2sid_recv,
707 (void *)cont, private_data);
710 enum winbindd_result winbindd_dual_gid2sid(struct winbindd_domain *domain,
711 struct winbindd_cli_state *state)
716 DEBUG(3,("[%5lu]: gid %lu to sid\n",
717 (unsigned long)state->pid,
718 (unsigned long) state->request.data.gid));
720 /* Find sid for this gid and return it, possibly ask the slow remote idmap */
721 result = idmap_gid_to_sid(&sid, state->request.data.gid);
723 if (NT_STATUS_IS_OK(result)) {
724 sid_to_string(state->response.data.sid.sid, &sid);
725 DEBUG(10, ("[%5lu]: retrieved sid: %s\n",
726 (unsigned long)state->pid,
727 state->response.data.sid.sid));
728 state->response.data.sid.type = SID_NAME_DOM_GRP;
732 return WINBINDD_ERROR;
735 static void winbindd_dump_id_maps_recv(TALLOC_CTX *mem_ctx, BOOL success,
736 struct winbindd_response *response,
737 void *c, void *private_data)
739 void (*cont)(void *priv, BOOL succ) =
740 (void (*)(void *, BOOL))c;
743 DEBUG(5, ("Could not trigger a map dump\n"));
744 cont(private_data, False);
748 if (response->result != WINBINDD_OK) {
749 DEBUG(5, ("idmap dump maps returned an error\n"));
750 cont(private_data, False);
754 cont(private_data, True);
757 void winbindd_dump_maps_async(TALLOC_CTX *mem_ctx, void *data, int size,
758 void (*cont)(void *private_data, BOOL success),
761 struct winbindd_request request;
762 ZERO_STRUCT(request);
763 request.cmd = WINBINDD_DUAL_DUMP_MAPS;
764 request.extra_data.data = (char *)data;
765 request.extra_len = size;
766 do_async(mem_ctx, idmap_child(), &request, winbindd_dump_id_maps_recv,
767 (void *)cont, private_data);
770 enum winbindd_result winbindd_dual_dump_maps(struct winbindd_domain *domain,
771 struct winbindd_cli_state *state)
773 DEBUG(3, ("[%5lu]: dual dump maps\n", (unsigned long)state->pid));
775 idmap_dump_maps((char *)state->request.extra_data.data);