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 2 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, write to the Free Software
32 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
39 #define DBGC_CLASS DBGC_WINBIND
41 struct do_async_state {
43 struct winbindd_request request;
44 struct winbindd_response response;
45 void (*cont)(TALLOC_CTX *mem_ctx,
47 struct winbindd_response *response,
48 void *c, void *private_data);
49 void *c, *private_data;
52 static void do_async_recv(void *private_data, BOOL success)
54 struct do_async_state *state =
55 talloc_get_type_abort(private_data, struct do_async_state);
57 state->cont(state->mem_ctx, success, &state->response,
58 state->c, state->private_data);
61 static void do_async(TALLOC_CTX *mem_ctx, struct winbindd_child *child,
62 const struct winbindd_request *request,
63 void (*cont)(TALLOC_CTX *mem_ctx, BOOL success,
64 struct winbindd_response *response,
65 void *c, void *private_data),
66 void *c, void *private_data)
68 struct do_async_state *state;
70 state = TALLOC_ZERO_P(mem_ctx, struct do_async_state);
72 DEBUG(0, ("talloc failed\n"));
73 cont(mem_ctx, False, NULL, c, private_data);
77 state->mem_ctx = mem_ctx;
78 state->request = *request;
79 state->request.length = sizeof(state->request);
82 state->private_data = private_data;
84 async_request(mem_ctx, child, &state->request,
85 &state->response, do_async_recv, state);
88 void do_async_domain(TALLOC_CTX *mem_ctx, struct winbindd_domain *domain,
89 const struct winbindd_request *request,
90 void (*cont)(TALLOC_CTX *mem_ctx, BOOL success,
91 struct winbindd_response *response,
92 void *c, void *private_data),
93 void *c, void *private_data)
95 struct do_async_state *state;
97 state = TALLOC_ZERO_P(mem_ctx, struct do_async_state);
99 DEBUG(0, ("talloc failed\n"));
100 cont(mem_ctx, False, NULL, c, private_data);
104 state->mem_ctx = mem_ctx;
105 state->request = *request;
106 state->request.length = sizeof(state->request);
109 state->private_data = private_data;
111 async_domain_request(mem_ctx, domain, &state->request,
112 &state->response, do_async_recv, state);
115 static void winbindd_set_mapping_recv(TALLOC_CTX *mem_ctx, BOOL success,
116 struct winbindd_response *response,
117 void *c, void *private_data)
119 void (*cont)(void *priv, BOOL succ) = (void (*)(void *, BOOL))c;
122 DEBUG(5, ("Could not trigger idmap_set_mapping\n"));
123 cont(private_data, False);
127 if (response->result != WINBINDD_OK) {
128 DEBUG(5, ("idmap_set_mapping returned an error\n"));
129 cont(private_data, False);
133 cont(private_data, True);
136 void winbindd_set_mapping_async(TALLOC_CTX *mem_ctx, const struct id_map *map,
137 void (*cont)(void *private_data, BOOL success),
140 struct winbindd_request request;
141 ZERO_STRUCT(request);
142 request.cmd = WINBINDD_DUAL_SET_MAPPING;
143 request.data.dual_idmapset.id = map->xid.id;
144 request.data.dual_idmapset.type = map->xid.type;
145 sid_to_string(request.data.dual_idmapset.sid, map->sid);
147 do_async(mem_ctx, idmap_child(), &request, winbindd_set_mapping_recv,
148 (void *)cont, private_data);
151 enum winbindd_result winbindd_dual_set_mapping(struct winbindd_domain *domain,
152 struct winbindd_cli_state *state)
158 DEBUG(3, ("[%5lu]: dual_idmapset\n", (unsigned long)state->pid));
160 if (!string_to_sid(&sid, state->request.data.dual_idmapset.sid))
161 return WINBINDD_ERROR;
164 map.xid.id = state->request.data.dual_idmapset.id;
165 map.xid.type = state->request.data.dual_idmapset.type;
167 result = idmap_set_mapping(&map);
168 return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
171 static void winbindd_set_hwm_recv(TALLOC_CTX *mem_ctx, BOOL success,
172 struct winbindd_response *response,
173 void *c, void *private_data)
175 void (*cont)(void *priv, BOOL succ) = (void (*)(void *, BOOL))c;
178 DEBUG(5, ("Could not trigger idmap_set_hwm\n"));
179 cont(private_data, False);
183 if (response->result != WINBINDD_OK) {
184 DEBUG(5, ("idmap_set_hwm returned an error\n"));
185 cont(private_data, False);
189 cont(private_data, True);
192 void winbindd_set_hwm_async(TALLOC_CTX *mem_ctx, const struct unixid *xid,
193 void (*cont)(void *private_data, BOOL success),
196 struct winbindd_request request;
197 ZERO_STRUCT(request);
198 request.cmd = WINBINDD_DUAL_SET_HWM;
199 request.data.dual_idmapset.id = xid->id;
200 request.data.dual_idmapset.type = xid->type;
202 do_async(mem_ctx, idmap_child(), &request, winbindd_set_hwm_recv,
203 (void *)cont, private_data);
206 enum winbindd_result winbindd_dual_set_hwm(struct winbindd_domain *domain,
207 struct winbindd_cli_state *state)
212 DEBUG(3, ("[%5lu]: dual_set_hwm\n", (unsigned long)state->pid));
214 xid.id = state->request.data.dual_idmapset.id;
215 xid.type = state->request.data.dual_idmapset.type;
219 result = idmap_set_uid_hwm(&xid);
222 result = idmap_set_gid_hwm(&xid);
225 return WINBINDD_ERROR;
227 return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
230 static void winbindd_sids2xids_recv(TALLOC_CTX *mem_ctx, BOOL success,
231 struct winbindd_response *response,
232 void *c, void *private_data)
234 void (*cont)(void *priv, BOOL succ, void *, int) =
235 (void (*)(void *, BOOL, void *, int))c;
238 DEBUG(5, ("Could not trigger sids2xids\n"));
239 cont(private_data, False, NULL, 0);
243 if (response->result != WINBINDD_OK) {
244 DEBUG(5, ("sids2xids returned an error\n"));
245 cont(private_data, False, NULL, 0);
249 cont(private_data, True, response->extra_data.data, response->length - sizeof(response));
252 void winbindd_sids2xids_async(TALLOC_CTX *mem_ctx, void *sids, int size,
253 void (*cont)(void *private_data, BOOL success, void *data, int len),
256 struct winbindd_request request;
257 ZERO_STRUCT(request);
258 request.cmd = WINBINDD_DUAL_SIDS2XIDS;
259 request.extra_data.data = sids;
260 request.extra_len = size;
261 do_async(mem_ctx, idmap_child(), &request, winbindd_sids2xids_recv,
262 (void *)cont, private_data);
265 enum winbindd_result winbindd_dual_sids2xids(struct winbindd_domain *domain,
266 struct winbindd_cli_state *state)
274 DEBUG(3, ("[%5lu]: sids to unix ids\n", (unsigned long)state->pid));
276 sids = (DOM_SID *)state->request.extra_data.data;
277 num = state->request.extra_len / sizeof(DOM_SID);
279 ids = talloc_zero_array(state->mem_ctx, struct id_map *, num + 1);
281 DEBUG(0, ("Out of memory!\n"));
282 return WINBINDD_ERROR;
284 for (i = 0; i < num; i++) {
285 ids[i] = talloc(ids, struct id_map);
287 DEBUG(0, ("Out of memory!\n"));
289 return WINBINDD_ERROR;
291 ids[i]->sid = &sids[i];
294 result = idmap_sids_to_unixids(ids);
296 if (NT_STATUS_IS_OK(result)) {
298 xids = SMB_MALLOC_ARRAY(struct unixid, num);
300 DEBUG(0, ("Out of memory!\n"));
302 return WINBINDD_ERROR;
305 for (i = 0; i < num; i++) {
306 if (ids[i]->mapped) {
307 xids[i].type = ids[i]->xid.type;
308 xids[i].id = ids[i]->xid.id;
314 state->response.length = sizeof(state->response) + (sizeof(struct unixid) * num);
315 state->response.extra_data.data = xids;
318 DEBUG (2, ("idmap_sids_to_unixids returned an error: 0x%08x\n", NT_STATUS_V(result)));
320 return WINBINDD_ERROR;
327 static void winbindd_sid2uid_recv(TALLOC_CTX *mem_ctx, BOOL success,
328 struct winbindd_response *response,
329 void *c, void *private_data)
331 void (*cont)(void *priv, BOOL succ, uid_t uid) =
332 (void (*)(void *, BOOL, uid_t))c;
335 DEBUG(5, ("Could not trigger sid2uid\n"));
336 cont(private_data, False, 0);
340 if (response->result != WINBINDD_OK) {
341 DEBUG(5, ("sid2uid returned an error\n"));
342 cont(private_data, False, 0);
346 cont(private_data, True, response->data.uid);
349 void winbindd_sid2uid_async(TALLOC_CTX *mem_ctx, const DOM_SID *sid,
350 void (*cont)(void *private_data, BOOL success, uid_t uid),
353 struct winbindd_request request;
354 ZERO_STRUCT(request);
355 request.cmd = WINBINDD_DUAL_SID2UID;
356 sid_to_string(request.data.dual_sid2id.sid, sid);
357 do_async(mem_ctx, idmap_child(), &request, winbindd_sid2uid_recv,
358 (void *)cont, private_data);
361 enum winbindd_result winbindd_dual_sid2uid(struct winbindd_domain *domain,
362 struct winbindd_cli_state *state)
367 DEBUG(3, ("[%5lu]: sid to uid %s\n", (unsigned long)state->pid,
368 state->request.data.dual_sid2id.sid));
370 if (!string_to_sid(&sid, state->request.data.dual_sid2id.sid)) {
371 DEBUG(1, ("Could not get convert sid %s from string\n",
372 state->request.data.dual_sid2id.sid));
373 return WINBINDD_ERROR;
376 /* Find uid for this sid and return it, possibly ask the slow remote idmap */
378 result = idmap_sid_to_uid(&sid, &(state->response.data.uid));
380 return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
384 static void uid2name_recv(TALLOC_CTX *mem_ctx, BOOL success,
385 struct winbindd_response *response,
386 void *c, void *private_data);
388 void winbindd_uid2name_async(TALLOC_CTX *mem_ctx, uid_t uid,
389 void (*cont)(void *private_data, BOOL success,
393 struct winbindd_request request;
394 ZERO_STRUCT(request);
395 request.cmd = WINBINDD_DUAL_UID2NAME;
396 request.data.uid = uid;
397 do_async(mem_ctx, idmap_child(), &request, uid2name_recv,
398 (void *)cont, private_data);
400 #endif /* not used */
402 enum winbindd_result winbindd_dual_uid2name(struct winbindd_domain *domain,
403 struct winbindd_cli_state *state)
407 DEBUG(3, ("[%5lu]: uid2name %lu\n", (unsigned long)state->pid,
408 (unsigned long)state->request.data.uid));
410 pw = getpwuid(state->request.data.uid);
412 DEBUG(5, ("User %lu not found\n",
413 (unsigned long)state->request.data.uid));
414 return WINBINDD_ERROR;
417 fstrcpy(state->response.data.name.name, pw->pw_name);
422 static void uid2name_recv(TALLOC_CTX *mem_ctx, BOOL success,
423 struct winbindd_response *response,
424 void *c, void *private_data)
426 void (*cont)(void *priv, BOOL succ, const char *name) =
427 (void (*)(void *, BOOL, const char *))c;
430 DEBUG(5, ("Could not trigger uid2name\n"));
431 cont(private_data, False, NULL);
435 if (response->result != WINBINDD_OK) {
436 DEBUG(5, ("uid2name returned an error\n"));
437 cont(private_data, False, NULL);
441 cont(private_data, True, response->data.name.name);
444 static void name2uid_recv(TALLOC_CTX *mem_ctx, BOOL success,
445 struct winbindd_response *response,
446 void *c, void *private_data);
448 static void winbindd_name2uid_async(TALLOC_CTX *mem_ctx, const char *name,
449 void (*cont)(void *private_data, BOOL success,
453 struct winbindd_request request;
454 ZERO_STRUCT(request);
455 request.cmd = WINBINDD_DUAL_NAME2UID;
456 fstrcpy(request.data.username, name);
457 do_async(mem_ctx, idmap_child(), &request, name2uid_recv,
458 (void *)cont, private_data);
460 #endif /* not used */
462 enum winbindd_result winbindd_dual_name2uid(struct winbindd_domain *domain,
463 struct winbindd_cli_state *state)
467 /* Ensure null termination */
468 state->request.data.username
469 [sizeof(state->request.data.username)-1] = '\0';
471 DEBUG(3, ("[%5lu]: name2uid %s\n", (unsigned long)state->pid,
472 state->request.data.username));
474 pw = getpwnam(state->request.data.username);
476 return WINBINDD_ERROR;
479 state->response.data.uid = pw->pw_uid;
484 static void name2uid_recv(TALLOC_CTX *mem_ctx, BOOL success,
485 struct winbindd_response *response,
486 void *c, void *private_data)
488 void (*cont)(void *priv, BOOL succ, uid_t uid) =
489 (void (*)(void *, BOOL, uid_t))c;
492 DEBUG(5, ("Could not trigger name2uid\n"));
493 cont(private_data, False, 0);
497 if (response->result != WINBINDD_OK) {
498 DEBUG(5, ("name2uid returned an error\n"));
499 cont(private_data, False, 0);
503 cont(private_data, True, response->data.uid);
505 #endif /* not used */
507 static void winbindd_sid2gid_recv(TALLOC_CTX *mem_ctx, BOOL success,
508 struct winbindd_response *response,
509 void *c, void *private_data)
511 void (*cont)(void *priv, BOOL succ, gid_t gid) =
512 (void (*)(void *, BOOL, gid_t))c;
515 DEBUG(5, ("Could not trigger sid2gid\n"));
516 cont(private_data, False, 0);
520 if (response->result != WINBINDD_OK) {
521 DEBUG(5, ("sid2gid returned an error\n"));
522 cont(private_data, False, 0);
526 cont(private_data, True, response->data.gid);
529 void winbindd_sid2gid_async(TALLOC_CTX *mem_ctx, const DOM_SID *sid,
530 void (*cont)(void *private_data, BOOL success, gid_t gid),
533 struct winbindd_request request;
534 ZERO_STRUCT(request);
535 request.cmd = WINBINDD_DUAL_SID2GID;
536 sid_to_string(request.data.dual_sid2id.sid, sid);
538 DEBUG(7,("idmap_sid2gid_async: Resolving %s to a gid\n",
539 request.data.dual_sid2id.sid));
541 do_async(mem_ctx, idmap_child(), &request, winbindd_sid2gid_recv,
542 (void *)cont, private_data);
545 enum winbindd_result winbindd_dual_sid2gid(struct winbindd_domain *domain,
546 struct winbindd_cli_state *state)
551 DEBUG(3, ("[%5lu]: sid to gid %s\n", (unsigned long)state->pid,
552 state->request.data.dual_sid2id.sid));
554 if (!string_to_sid(&sid, state->request.data.dual_sid2id.sid)) {
555 DEBUG(1, ("Could not get convert sid %s from string\n",
556 state->request.data.dual_sid2id.sid));
557 return WINBINDD_ERROR;
560 /* Find gid for this sid and return it, possibly ask the slow remote idmap */
562 result = idmap_sid_to_gid(&sid, &state->response.data.gid);
564 DEBUG(10, ("winbindd_dual_sid2gid: 0x%08x - %s - %u\n", NT_STATUS_V(result), sid_string_static(&sid), state->response.data.gid));
566 return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
569 static void gid2name_recv(TALLOC_CTX *mem_ctx, BOOL success,
570 struct winbindd_response *response,
571 void *c, void *private_data)
573 void (*cont)(void *priv, BOOL succ, const char *name) =
574 (void (*)(void *, BOOL, const char *))c;
577 DEBUG(5, ("Could not trigger gid2name\n"));
578 cont(private_data, False, NULL);
582 if (response->result != WINBINDD_OK) {
583 DEBUG(5, ("gid2name returned an error\n"));
584 cont(private_data, False, NULL);
588 cont(private_data, True, response->data.name.name);
591 void winbindd_gid2name_async(TALLOC_CTX *mem_ctx, gid_t gid,
592 void (*cont)(void *private_data, BOOL success,
596 struct winbindd_request request;
597 ZERO_STRUCT(request);
598 request.cmd = WINBINDD_DUAL_GID2NAME;
599 request.data.gid = gid;
600 do_async(mem_ctx, idmap_child(), &request, gid2name_recv,
601 (void *)cont, private_data);
604 enum winbindd_result winbindd_dual_gid2name(struct winbindd_domain *domain,
605 struct winbindd_cli_state *state)
609 DEBUG(3, ("[%5lu]: gid2name %lu\n", (unsigned long)state->pid,
610 (unsigned long)state->request.data.gid));
612 gr = getgrgid(state->request.data.gid);
614 return WINBINDD_ERROR;
616 fstrcpy(state->response.data.name.name, gr->gr_name);
621 static void name2gid_recv(TALLOC_CTX *mem_ctx, BOOL success,
622 struct winbindd_response *response,
623 void *c, void *private_data);
625 static void winbindd_name2gid_async(TALLOC_CTX *mem_ctx, const char *name,
626 void (*cont)(void *private_data, BOOL success,
630 struct winbindd_request request;
631 ZERO_STRUCT(request);
632 request.cmd = WINBINDD_DUAL_NAME2GID;
633 fstrcpy(request.data.groupname, name);
634 do_async(mem_ctx, idmap_child(), &request, name2gid_recv,
635 (void *)cont, private_data);
637 #endif /* not used */
639 enum winbindd_result winbindd_dual_name2gid(struct winbindd_domain *domain,
640 struct winbindd_cli_state *state)
644 /* Ensure null termination */
645 state->request.data.groupname
646 [sizeof(state->request.data.groupname)-1] = '\0';
648 DEBUG(3, ("[%5lu]: name2gid %s\n", (unsigned long)state->pid,
649 state->request.data.groupname));
651 gr = getgrnam(state->request.data.groupname);
653 return WINBINDD_ERROR;
656 state->response.data.gid = gr->gr_gid;
661 static void name2gid_recv(TALLOC_CTX *mem_ctx, BOOL success,
662 struct winbindd_response *response,
663 void *c, void *private_data)
665 void (*cont)(void *priv, BOOL succ, gid_t gid) =
666 (void (*)(void *, BOOL, gid_t))c;
669 DEBUG(5, ("Could not trigger name2gid\n"));
670 cont(private_data, False, 0);
674 if (response->result != WINBINDD_OK) {
675 DEBUG(5, ("name2gid returned an error\n"));
676 cont(private_data, False, 0);
680 cont(private_data, True, response->data.gid);
682 #endif /* not used */
684 static void lookupsid_recv(TALLOC_CTX *mem_ctx, BOOL success,
685 struct winbindd_response *response,
686 void *c, void *private_data)
688 void (*cont)(void *priv, BOOL succ, const char *dom_name,
689 const char *name, enum lsa_SidType type) =
690 (void (*)(void *, BOOL, const char *, const char *,
694 DEBUG(5, ("Could not trigger lookupsid\n"));
695 cont(private_data, False, NULL, NULL, SID_NAME_UNKNOWN);
699 if (response->result != WINBINDD_OK) {
700 DEBUG(5, ("lookupsid returned an error\n"));
701 cont(private_data, False, NULL, NULL, SID_NAME_UNKNOWN);
705 cont(private_data, True, response->data.name.dom_name,
706 response->data.name.name,
707 (enum lsa_SidType)response->data.name.type);
710 void winbindd_lookupsid_async(TALLOC_CTX *mem_ctx, const DOM_SID *sid,
711 void (*cont)(void *private_data, BOOL success,
712 const char *dom_name,
714 enum lsa_SidType type),
717 struct winbindd_domain *domain;
718 struct winbindd_request request;
720 domain = find_lookup_domain_from_sid(sid);
721 if (domain == NULL) {
722 DEBUG(5, ("Could not find domain for sid %s\n",
723 sid_string_static(sid)));
724 cont(private_data, False, NULL, NULL, SID_NAME_UNKNOWN);
728 ZERO_STRUCT(request);
729 request.cmd = WINBINDD_LOOKUPSID;
730 fstrcpy(request.data.sid, sid_string_static(sid));
732 do_async_domain(mem_ctx, domain, &request, lookupsid_recv,
733 (void *)cont, private_data);
736 enum winbindd_result winbindd_dual_lookupsid(struct winbindd_domain *domain,
737 struct winbindd_cli_state *state)
739 enum lsa_SidType type;
744 /* Ensure null termination */
745 state->request.data.sid[sizeof(state->request.data.sid)-1]='\0';
747 DEBUG(3, ("[%5lu]: lookupsid %s\n", (unsigned long)state->pid,
748 state->request.data.sid));
750 /* Lookup sid from PDC using lsa_lookup_sids() */
752 if (!string_to_sid(&sid, state->request.data.sid)) {
753 DEBUG(5, ("%s not a SID\n", state->request.data.sid));
754 return WINBINDD_ERROR;
759 if (!winbindd_lookup_name_by_sid(state->mem_ctx, &sid, &dom_name, &name,
761 TALLOC_FREE(dom_name);
763 return WINBINDD_ERROR;
766 fstrcpy(state->response.data.name.dom_name, dom_name);
767 fstrcpy(state->response.data.name.name, name);
768 state->response.data.name.type = type;
770 TALLOC_FREE(dom_name);
775 static void lookupname_recv(TALLOC_CTX *mem_ctx, BOOL success,
776 struct winbindd_response *response,
777 void *c, void *private_data)
779 void (*cont)(void *priv, BOOL succ, const DOM_SID *sid,
780 enum lsa_SidType type) =
781 (void (*)(void *, BOOL, const DOM_SID *, enum lsa_SidType))c;
785 DEBUG(5, ("Could not trigger lookup_name\n"));
786 cont(private_data, False, NULL, SID_NAME_UNKNOWN);
790 if (response->result != WINBINDD_OK) {
791 DEBUG(5, ("lookup_name returned an error\n"));
792 cont(private_data, False, NULL, SID_NAME_UNKNOWN);
796 if (!string_to_sid(&sid, response->data.sid.sid)) {
797 DEBUG(0, ("Could not convert string %s to sid\n",
798 response->data.sid.sid));
799 cont(private_data, False, NULL, SID_NAME_UNKNOWN);
803 cont(private_data, True, &sid,
804 (enum lsa_SidType)response->data.sid.type);
807 void winbindd_lookupname_async(TALLOC_CTX *mem_ctx, const char *dom_name,
809 void (*cont)(void *private_data, BOOL success,
811 enum lsa_SidType type),
814 struct winbindd_request request;
815 struct winbindd_domain *domain;
817 domain = find_lookup_domain_from_name(dom_name);
819 if (domain == NULL) {
820 DEBUG(5, ("Could not find domain for name %s\n", dom_name));
821 cont(private_data, False, NULL, SID_NAME_UNKNOWN);
825 ZERO_STRUCT(request);
826 request.cmd = WINBINDD_LOOKUPNAME;
827 fstrcpy(request.data.name.dom_name, dom_name);
828 fstrcpy(request.data.name.name, name);
830 do_async_domain(mem_ctx, domain, &request, lookupname_recv,
831 (void *)cont, private_data);
834 enum winbindd_result winbindd_dual_lookupname(struct winbindd_domain *domain,
835 struct winbindd_cli_state *state)
837 enum lsa_SidType type;
838 char *name_domain, *name_user;
842 /* Ensure null termination */
843 state->request.data.sid[sizeof(state->request.data.name.dom_name)-1]='\0';
845 /* Ensure null termination */
846 state->request.data.sid[sizeof(state->request.data.name.name)-1]='\0';
848 /* cope with the name being a fully qualified name */
849 p = strstr(state->request.data.name.name, lp_winbind_separator());
852 name_domain = state->request.data.name.name;
855 name_domain = state->request.data.name.dom_name;
856 name_user = state->request.data.name.name;
859 DEBUG(3, ("[%5lu]: lookupname %s%s%s\n", (unsigned long)state->pid,
860 name_domain, lp_winbind_separator(), name_user));
862 /* Lookup name from PDC using lsa_lookup_names() */
863 if (!winbindd_lookup_sid_by_name(state->mem_ctx, domain, name_domain,
864 name_user, &sid, &type)) {
865 return WINBINDD_ERROR;
868 sid_to_string(state->response.data.sid.sid, &sid);
869 state->response.data.sid.type = type;
874 BOOL print_sidlist(TALLOC_CTX *mem_ctx, const DOM_SID *sids,
875 size_t num_sids, char **result, ssize_t *len)
882 for (i=0; i<num_sids; i++) {
883 sprintf_append(mem_ctx, result, len, &buflen,
884 "%s\n", sid_string_static(&sids[i]));
887 if ((num_sids != 0) && (*result == NULL)) {
894 static BOOL parse_sidlist(TALLOC_CTX *mem_ctx, char *sidstr,
895 DOM_SID **sids, size_t *num_sids)
903 while (p[0] != '\0') {
907 DEBUG(0, ("Got invalid sidstr: %s\n", p));
912 if (!string_to_sid(&sid, p)) {
913 DEBUG(0, ("Could not parse sid %s\n", p));
916 if (!add_sid_to_array(mem_ctx, &sid, sids, num_sids)) {
924 static BOOL parse_ridlist(TALLOC_CTX *mem_ctx, char *ridstr,
925 uint32 **rids, size_t *num_rids)
933 while (p[0] != '\0') {
936 rid = strtoul(p, &q, 10);
938 DEBUG(0, ("Got invalid ridstr: %s\n", p));
942 ADD_TO_ARRAY(mem_ctx, uint32, rid, rids, num_rids);
947 enum winbindd_result winbindd_dual_lookuprids(struct winbindd_domain *domain,
948 struct winbindd_cli_state *state)
951 size_t i, buflen, num_rids = 0;
956 enum lsa_SidType *types;
960 DEBUG(10, ("Looking up RIDs for domain %s (%s)\n",
961 state->request.domain_name,
962 state->request.data.sid));
964 if (!parse_ridlist(state->mem_ctx, state->request.extra_data.data,
966 DEBUG(5, ("Could not parse ridlist\n"));
967 return WINBINDD_ERROR;
970 if (!string_to_sid(&domain_sid, state->request.data.sid)) {
971 DEBUG(5, ("Could not parse domain sid %s\n",
972 state->request.data.sid));
973 return WINBINDD_ERROR;
976 status = domain->methods->rids_to_names(domain, state->mem_ctx,
977 &domain_sid, rids, num_rids,
981 if (!NT_STATUS_IS_OK(status) &&
982 !NT_STATUS_EQUAL(status, STATUS_SOME_UNMAPPED)) {
983 return WINBINDD_ERROR;
990 for (i=0; i<num_rids; i++) {
991 sprintf_append(state->mem_ctx, &result, &len, &buflen,
992 "%d %s\n", types[i], names[i]);
995 fstrcpy(state->response.data.domain_name, domain_name);
997 if (result != NULL) {
998 state->response.extra_data.data = SMB_STRDUP(result);
999 if (!state->response.extra_data.data) {
1000 return WINBINDD_ERROR;
1002 state->response.length += len+1;
1008 static void getsidaliases_recv(TALLOC_CTX *mem_ctx, BOOL success,
1009 struct winbindd_response *response,
1010 void *c, void *private_data)
1012 void (*cont)(void *priv, BOOL succ,
1013 DOM_SID *aliases, size_t num_aliases) =
1014 (void (*)(void *, BOOL, DOM_SID *, size_t))c;
1016 DOM_SID *sids = NULL;
1017 size_t num_sids = 0;
1020 DEBUG(5, ("Could not trigger getsidaliases\n"));
1021 cont(private_data, success, NULL, 0);
1025 if (response->result != WINBINDD_OK) {
1026 DEBUG(5, ("getsidaliases returned an error\n"));
1027 cont(private_data, False, NULL, 0);
1031 aliases_str = (char *)response->extra_data.data;
1033 if (aliases_str == NULL) {
1034 DEBUG(10, ("getsidaliases return 0 SIDs\n"));
1035 cont(private_data, True, NULL, 0);
1039 if (!parse_sidlist(mem_ctx, aliases_str, &sids, &num_sids)) {
1040 DEBUG(0, ("Could not parse sids\n"));
1041 cont(private_data, False, NULL, 0);
1045 SAFE_FREE(response->extra_data.data);
1047 cont(private_data, True, sids, num_sids);
1050 void winbindd_getsidaliases_async(struct winbindd_domain *domain,
1051 TALLOC_CTX *mem_ctx,
1052 const DOM_SID *sids, size_t num_sids,
1053 void (*cont)(void *private_data,
1055 const DOM_SID *aliases,
1056 size_t num_aliases),
1059 struct winbindd_request request;
1060 char *sidstr = NULL;
1063 if (num_sids == 0) {
1064 cont(private_data, True, NULL, 0);
1068 if (!print_sidlist(mem_ctx, sids, num_sids, &sidstr, &len)) {
1069 cont(private_data, False, NULL, 0);
1073 ZERO_STRUCT(request);
1074 request.cmd = WINBINDD_DUAL_GETSIDALIASES;
1075 request.extra_len = len;
1076 request.extra_data.data = sidstr;
1078 do_async_domain(mem_ctx, domain, &request, getsidaliases_recv,
1079 (void *)cont, private_data);
1082 enum winbindd_result winbindd_dual_getsidaliases(struct winbindd_domain *domain,
1083 struct winbindd_cli_state *state)
1085 DOM_SID *sids = NULL;
1086 size_t num_sids = 0;
1094 DEBUG(3, ("[%5lu]: getsidaliases\n", (unsigned long)state->pid));
1096 sidstr = state->request.extra_data.data;
1098 sidstr = talloc_strdup(state->mem_ctx, "\n"); /* No SID */
1100 DEBUG(10, ("Sidlist: %s\n", sidstr));
1102 if (!parse_sidlist(state->mem_ctx, sidstr, &sids, &num_sids)) {
1103 DEBUG(0, ("Could not parse SID list: %s\n", sidstr));
1104 return WINBINDD_ERROR;
1110 result = domain->methods->lookup_useraliases(domain,
1116 if (!NT_STATUS_IS_OK(result)) {
1117 DEBUG(3, ("Could not lookup_useraliases: %s\n",
1118 nt_errstr(result)));
1119 return WINBINDD_ERROR;
1125 DEBUG(10, ("Got %d aliases\n", num_aliases));
1127 for (i=0; i<num_aliases; i++) {
1129 DEBUGADD(10, (" rid %d\n", alias_rids[i]));
1130 sid_copy(&sid, &domain->sid);
1131 sid_append_rid(&sid, alias_rids[i]);
1132 if (!add_sid_to_array(state->mem_ctx, &sid, &sids, &num_sids)) {
1133 return WINBINDD_ERROR;
1138 if (!print_sidlist(state->mem_ctx, sids, num_sids, &sidstr, &len)) {
1139 DEBUG(0, ("Could not print_sidlist\n"));
1140 state->response.extra_data.data = NULL;
1141 return WINBINDD_ERROR;
1144 state->response.extra_data.data = SMB_STRDUP(sidstr);
1146 if (state->response.extra_data.data != NULL) {
1147 DEBUG(10, ("aliases_list: %s\n",
1148 (char *)state->response.extra_data.data));
1149 state->response.length += len+1;
1155 struct gettoken_state {
1156 TALLOC_CTX *mem_ctx;
1158 struct winbindd_domain *alias_domain;
1159 struct winbindd_domain *local_alias_domain;
1160 struct winbindd_domain *builtin_domain;
1163 void (*cont)(void *private_data, BOOL success, DOM_SID *sids, size_t num_sids);
1167 static void gettoken_recvdomgroups(TALLOC_CTX *mem_ctx, BOOL success,
1168 struct winbindd_response *response,
1169 void *c, void *private_data);
1170 static void gettoken_recvaliases(void *private_data, BOOL success,
1171 const DOM_SID *aliases,
1172 size_t num_aliases);
1175 void winbindd_gettoken_async(TALLOC_CTX *mem_ctx, const DOM_SID *user_sid,
1176 void (*cont)(void *private_data, BOOL success,
1177 DOM_SID *sids, size_t num_sids),
1180 struct winbindd_domain *domain;
1181 struct winbindd_request request;
1182 struct gettoken_state *state;
1184 state = TALLOC_ZERO_P(mem_ctx, struct gettoken_state);
1185 if (state == NULL) {
1186 DEBUG(0, ("talloc failed\n"));
1187 cont(private_data, False, NULL, 0);
1191 state->mem_ctx = mem_ctx;
1192 sid_copy(&state->user_sid, user_sid);
1193 state->alias_domain = find_our_domain();
1194 state->local_alias_domain = find_domain_from_name( get_global_sam_name() );
1195 state->builtin_domain = find_builtin_domain();
1197 state->private_data = private_data;
1199 domain = find_domain_from_sid_noinit(user_sid);
1200 if (domain == NULL) {
1201 DEBUG(5, ("Could not find domain from SID %s\n",
1202 sid_string_static(user_sid)));
1203 cont(private_data, False, NULL, 0);
1207 ZERO_STRUCT(request);
1208 request.cmd = WINBINDD_GETUSERDOMGROUPS;
1209 fstrcpy(request.data.sid, sid_string_static(user_sid));
1211 do_async_domain(mem_ctx, domain, &request, gettoken_recvdomgroups,
1215 static void gettoken_recvdomgroups(TALLOC_CTX *mem_ctx, BOOL success,
1216 struct winbindd_response *response,
1217 void *c, void *private_data)
1219 struct gettoken_state *state =
1220 talloc_get_type_abort(private_data, struct gettoken_state);
1224 DEBUG(10, ("Could not get domain groups\n"));
1225 state->cont(state->private_data, False, NULL, 0);
1229 sids_str = (char *)response->extra_data.data;
1231 if (sids_str == NULL) {
1232 /* This could be normal if we are dealing with a
1233 local user and local groups */
1235 if ( !sid_check_is_in_our_domain( &state->user_sid ) ) {
1236 DEBUG(10, ("Received no domain groups\n"));
1237 state->cont(state->private_data, True, NULL, 0);
1243 state->num_sids = 0;
1245 if (!add_sid_to_array(mem_ctx, &state->user_sid, &state->sids,
1246 &state->num_sids)) {
1247 DEBUG(0, ("Out of memory\n"));
1248 state->cont(state->private_data, False, NULL, 0);
1252 if (sids_str && !parse_sidlist(mem_ctx, sids_str, &state->sids,
1253 &state->num_sids)) {
1254 DEBUG(0, ("Could not parse sids\n"));
1255 state->cont(state->private_data, False, NULL, 0);
1259 SAFE_FREE(response->extra_data.data);
1261 if (state->alias_domain == NULL) {
1262 DEBUG(10, ("Don't expand domain local groups\n"));
1263 state->cont(state->private_data, True, state->sids,
1268 winbindd_getsidaliases_async(state->alias_domain, mem_ctx,
1269 state->sids, state->num_sids,
1270 gettoken_recvaliases, state);
1273 static void gettoken_recvaliases(void *private_data, BOOL success,
1274 const DOM_SID *aliases,
1277 struct gettoken_state *state = (struct gettoken_state *)private_data;
1281 DEBUG(10, ("Could not receive domain local groups\n"));
1282 state->cont(state->private_data, False, NULL, 0);
1286 for (i=0; i<num_aliases; i++) {
1287 if (!add_sid_to_array(state->mem_ctx, &aliases[i],
1288 &state->sids, &state->num_sids)) {
1289 DEBUG(0, ("Out of memory\n"));
1290 state->cont(state->private_data, False, NULL, 0);
1295 if (state->local_alias_domain != NULL) {
1296 struct winbindd_domain *local_domain = state->local_alias_domain;
1297 DEBUG(10, ("Expanding our own local groups\n"));
1298 state->local_alias_domain = NULL;
1299 winbindd_getsidaliases_async(local_domain, state->mem_ctx,
1300 state->sids, state->num_sids,
1301 gettoken_recvaliases, state);
1305 if (state->builtin_domain != NULL) {
1306 struct winbindd_domain *builtin_domain = state->builtin_domain;
1307 DEBUG(10, ("Expanding our own BUILTIN groups\n"));
1308 state->builtin_domain = NULL;
1309 winbindd_getsidaliases_async(builtin_domain, state->mem_ctx,
1310 state->sids, state->num_sids,
1311 gettoken_recvaliases, state);
1315 state->cont(state->private_data, True, state->sids, state->num_sids);
1318 static void query_user_recv(TALLOC_CTX *mem_ctx, BOOL success,
1319 struct winbindd_response *response,
1320 void *c, void *private_data)
1322 void (*cont)(void *priv, BOOL succ, const char *acct_name,
1323 const char *full_name, const char *homedir,
1324 const char *shell, uint32 group_rid) =
1325 (void (*)(void *, BOOL, const char *, const char *,
1326 const char *, const char *, uint32))c;
1329 DEBUG(5, ("Could not trigger query_user\n"));
1330 cont(private_data, False, NULL, NULL, NULL, NULL, -1);
1334 cont(private_data, True, response->data.user_info.acct_name,
1335 response->data.user_info.full_name,
1336 response->data.user_info.homedir,
1337 response->data.user_info.shell,
1338 response->data.user_info.group_rid);
1341 void query_user_async(TALLOC_CTX *mem_ctx, struct winbindd_domain *domain,
1343 void (*cont)(void *private_data, BOOL success,
1344 const char *acct_name,
1345 const char *full_name,
1346 const char *homedir,
1351 struct winbindd_request request;
1352 ZERO_STRUCT(request);
1353 request.cmd = WINBINDD_DUAL_USERINFO;
1354 sid_to_string(request.data.sid, sid);
1355 do_async_domain(mem_ctx, domain, &request, query_user_recv,
1356 (void *)cont, private_data);
1359 /* The following uid2sid/gid2sid functions has been contributed by
1360 * Keith Reynolds <Keith.Reynolds@centrify.com> */
1362 static void winbindd_uid2sid_recv(TALLOC_CTX *mem_ctx, BOOL success,
1363 struct winbindd_response *response,
1364 void *c, void *private_data)
1366 void (*cont)(void *priv, BOOL succ, const char *sid) =
1367 (void (*)(void *, BOOL, const char *))c;
1370 DEBUG(5, ("Could not trigger uid2sid\n"));
1371 cont(private_data, False, NULL);
1375 if (response->result != WINBINDD_OK) {
1376 DEBUG(5, ("uid2sid returned an error\n"));
1377 cont(private_data, False, NULL);
1381 cont(private_data, True, response->data.sid.sid);
1384 void winbindd_uid2sid_async(TALLOC_CTX *mem_ctx, uid_t uid,
1385 void (*cont)(void *private_data, BOOL success, const char *sid),
1388 struct winbindd_request request;
1390 ZERO_STRUCT(request);
1391 request.cmd = WINBINDD_DUAL_UID2SID;
1392 request.data.uid = uid;
1393 do_async(mem_ctx, idmap_child(), &request, winbindd_uid2sid_recv,
1394 (void *)cont, private_data);
1397 enum winbindd_result winbindd_dual_uid2sid(struct winbindd_domain *domain,
1398 struct winbindd_cli_state *state)
1403 DEBUG(3,("[%5lu]: uid to sid %lu\n",
1404 (unsigned long)state->pid,
1405 (unsigned long) state->request.data.uid));
1407 /* Find sid for this uid and return it, possibly ask the slow remote idmap */
1408 result = idmap_uid_to_sid(&sid, state->request.data.uid);
1410 if (NT_STATUS_IS_OK(result)) {
1411 sid_to_string(state->response.data.sid.sid, &sid);
1412 state->response.data.sid.type = SID_NAME_USER;
1416 return WINBINDD_ERROR;
1419 static void winbindd_gid2sid_recv(TALLOC_CTX *mem_ctx, BOOL success,
1420 struct winbindd_response *response,
1421 void *c, void *private_data)
1423 void (*cont)(void *priv, BOOL succ, const char *sid) =
1424 (void (*)(void *, BOOL, const char *))c;
1427 DEBUG(5, ("Could not trigger gid2sid\n"));
1428 cont(private_data, False, NULL);
1432 if (response->result != WINBINDD_OK) {
1433 DEBUG(5, ("gid2sid returned an error\n"));
1434 cont(private_data, False, NULL);
1438 cont(private_data, True, response->data.sid.sid);
1441 void winbindd_gid2sid_async(TALLOC_CTX *mem_ctx, gid_t gid,
1442 void (*cont)(void *private_data, BOOL success, const char *sid),
1445 struct winbindd_request request;
1447 ZERO_STRUCT(request);
1448 request.cmd = WINBINDD_DUAL_GID2SID;
1449 request.data.gid = gid;
1450 do_async(mem_ctx, idmap_child(), &request, winbindd_gid2sid_recv,
1451 (void *)cont, private_data);
1454 enum winbindd_result winbindd_dual_gid2sid(struct winbindd_domain *domain,
1455 struct winbindd_cli_state *state)
1460 DEBUG(3,("[%5lu]: gid %lu to sid\n",
1461 (unsigned long)state->pid,
1462 (unsigned long) state->request.data.gid));
1464 /* Find sid for this gid and return it, possibly ask the slow remote idmap */
1465 result = idmap_gid_to_sid(&sid, state->request.data.gid);
1467 if (NT_STATUS_IS_OK(result)) {
1468 sid_to_string(state->response.data.sid.sid, &sid);
1469 DEBUG(10, ("[%5lu]: retrieved sid: %s\n",
1470 (unsigned long)state->pid,
1471 state->response.data.sid.sid));
1472 state->response.data.sid.type = SID_NAME_DOM_GRP;
1476 return WINBINDD_ERROR;
1479 static void winbindd_dump_id_maps_recv(TALLOC_CTX *mem_ctx, BOOL success,
1480 struct winbindd_response *response,
1481 void *c, void *private_data)
1483 void (*cont)(void *priv, BOOL succ) =
1484 (void (*)(void *, BOOL))c;
1487 DEBUG(5, ("Could not trigger a map dump\n"));
1488 cont(private_data, False);
1492 if (response->result != WINBINDD_OK) {
1493 DEBUG(5, ("idmap dump maps returned an error\n"));
1494 cont(private_data, False);
1498 cont(private_data, True);
1501 void winbindd_dump_maps_async(TALLOC_CTX *mem_ctx, void *data, int size,
1502 void (*cont)(void *private_data, BOOL success),
1505 struct winbindd_request request;
1506 ZERO_STRUCT(request);
1507 request.cmd = WINBINDD_DUAL_DUMP_MAPS;
1508 request.extra_data.data = data;
1509 request.extra_len = size;
1510 do_async(mem_ctx, idmap_child(), &request, winbindd_dump_id_maps_recv,
1511 (void *)cont, private_data);
1514 enum winbindd_result winbindd_dual_dump_maps(struct winbindd_domain *domain,
1515 struct winbindd_cli_state *state)
1517 DEBUG(3, ("[%5lu]: dual dump maps\n", (unsigned long)state->pid));
1519 idmap_dump_maps((char *)state->request.extra_data.data);