r19787: get winbindd compiling
[Samba.git] / source / nsswitch / winbindd_async.c
blobb2f234c47400ce448a7606c75cefc4de820b86f9
1 /*
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.
35 #include "includes.h"
36 #include "winbindd.h"
38 #undef DBGC_CLASS
39 #define DBGC_CLASS DBGC_WINBIND
41 struct do_async_state {
42 TALLOC_CTX *mem_ctx;
43 struct winbindd_request request;
44 struct winbindd_response response;
45 void (*cont)(TALLOC_CTX *mem_ctx,
46 BOOL success,
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);
71 if (state == NULL) {
72 DEBUG(0, ("talloc failed\n"));
73 cont(mem_ctx, False, NULL, c, private_data);
74 return;
77 state->mem_ctx = mem_ctx;
78 state->request = *request;
79 state->request.length = sizeof(state->request);
80 state->cont = cont;
81 state->c = c;
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);
98 if (state == NULL) {
99 DEBUG(0, ("talloc failed\n"));
100 cont(mem_ctx, False, NULL, c, private_data);
101 return;
104 state->mem_ctx = mem_ctx;
105 state->request = *request;
106 state->request.length = sizeof(state->request);
107 state->cont = cont;
108 state->c = c;
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 idmap_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;
121 if (!success) {
122 DEBUG(5, ("Could not trigger idmap_set_mapping\n"));
123 cont(private_data, False);
124 return;
127 if (response->result != WINBINDD_OK) {
128 DEBUG(5, ("idmap_set_mapping returned an error\n"));
129 cont(private_data, False);
130 return;
133 cont(private_data, True);
136 void idmap_set_mapping_async(TALLOC_CTX *mem_ctx, const DOM_SID *sid,
137 unid_t id, int id_type,
138 void (*cont)(void *private_data, BOOL success),
139 void *private_data)
141 struct winbindd_request request;
142 ZERO_STRUCT(request);
143 request.cmd = WINBINDD_DUAL_IDMAPSET;
144 if (id_type == ID_USERID)
145 request.data.dual_idmapset.uid = id.uid;
146 else
147 request.data.dual_idmapset.gid = id.gid;
148 request.data.dual_idmapset.type = id_type;
149 sid_to_string(request.data.dual_idmapset.sid, sid);
151 do_async(mem_ctx, idmap_child(), &request, idmap_set_mapping_recv,
152 (void *)cont, private_data);
155 enum winbindd_result winbindd_dual_idmapset(struct winbindd_domain *domain,
156 struct winbindd_cli_state *state)
158 DOM_SID sid;
159 unid_t id;
160 NTSTATUS result;
162 DEBUG(3, ("[%5lu]: dual_idmapset\n", (unsigned long)state->pid));
164 if (!string_to_sid(&sid, state->request.data.dual_idmapset.sid))
165 return WINBINDD_ERROR;
167 if (state->request.data.dual_idmapset.type == ID_USERID)
168 id.uid = state->request.data.dual_idmapset.uid;
169 else
170 id.gid = state->request.data.dual_idmapset.gid;
172 result = idmap_set_mapping(
173 &sid, id,
174 (enum idmap_type)state->request.data.dual_idmapset.type);
175 return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
178 static void idmap_sid2uid_recv(TALLOC_CTX *mem_ctx, BOOL success,
179 struct winbindd_response *response,
180 void *c, void *private_data);
182 void idmap_sid2uid_async(TALLOC_CTX *mem_ctx, const DOM_SID *sid, BOOL alloc,
183 void (*cont)(void *private_data, BOOL success, uid_t uid),
184 void *private_data)
186 struct winbindd_request request;
187 ZERO_STRUCT(request);
188 request.cmd = WINBINDD_DUAL_SID2UID;
189 sid_to_string(request.data.dual_sid2id.sid, sid);
190 request.data.dual_sid2id.alloc = alloc;
191 do_async(mem_ctx, idmap_child(), &request, idmap_sid2uid_recv,
192 (void *)cont, private_data);
195 enum winbindd_result winbindd_dual_sid2uid(struct winbindd_domain *domain,
196 struct winbindd_cli_state *state)
198 DOM_SID sid;
199 NTSTATUS result;
201 DEBUG(3, ("[%5lu]: sid to uid %s\n", (unsigned long)state->pid,
202 state->request.data.dual_sid2id.sid));
204 if (!string_to_sid(&sid, state->request.data.dual_sid2id.sid)) {
205 DEBUG(1, ("Could not get convert sid %s from string\n",
206 state->request.data.dual_sid2id.sid));
207 return WINBINDD_ERROR;
210 /* Find uid for this sid and return it, possibly ask the slow remote
211 * idmap */
213 result = idmap_sid_to_uid(&sid, &(state->response.data.uid),
214 state->request.data.dual_sid2id.alloc ?
215 0 : IDMAP_FLAG_QUERY_ONLY);
217 return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
220 static void idmap_sid2uid_recv(TALLOC_CTX *mem_ctx, BOOL success,
221 struct winbindd_response *response,
222 void *c, void *private_data)
224 void (*cont)(void *priv, BOOL succ, uid_t uid) =
225 (void (*)(void *, BOOL, uid_t))c;
227 if (!success) {
228 DEBUG(5, ("Could not trigger sid2uid\n"));
229 cont(private_data, False, 0);
230 return;
233 if (response->result != WINBINDD_OK) {
234 DEBUG(5, ("sid2uid returned an error\n"));
235 cont(private_data, False, 0);
236 return;
239 cont(private_data, True, response->data.uid);
242 static void uid2name_recv(TALLOC_CTX *mem_ctx, BOOL success,
243 struct winbindd_response *response,
244 void *c, void *private_data);
246 void winbindd_uid2name_async(TALLOC_CTX *mem_ctx, uid_t uid,
247 void (*cont)(void *private_data, BOOL success,
248 const char *name),
249 void *private_data)
251 struct winbindd_request request;
252 ZERO_STRUCT(request);
253 request.cmd = WINBINDD_DUAL_UID2NAME;
254 request.data.uid = uid;
255 do_async(mem_ctx, idmap_child(), &request, uid2name_recv,
256 (void *)cont, private_data);
259 enum winbindd_result winbindd_dual_uid2name(struct winbindd_domain *domain,
260 struct winbindd_cli_state *state)
262 struct passwd *pw;
264 DEBUG(3, ("[%5lu]: uid2name %lu\n", (unsigned long)state->pid,
265 (unsigned long)state->request.data.uid));
267 pw = getpwuid(state->request.data.uid);
268 if (pw == NULL) {
269 DEBUG(5, ("User %lu not found\n",
270 (unsigned long)state->request.data.uid));
271 return WINBINDD_ERROR;
274 fstrcpy(state->response.data.name.name, pw->pw_name);
275 return WINBINDD_OK;
278 static void uid2name_recv(TALLOC_CTX *mem_ctx, BOOL success,
279 struct winbindd_response *response,
280 void *c, void *private_data)
282 void (*cont)(void *priv, BOOL succ, const char *name) =
283 (void (*)(void *, BOOL, const char *))c;
285 if (!success) {
286 DEBUG(5, ("Could not trigger uid2name\n"));
287 cont(private_data, False, NULL);
288 return;
291 if (response->result != WINBINDD_OK) {
292 DEBUG(5, ("uid2name returned an error\n"));
293 cont(private_data, False, NULL);
294 return;
297 cont(private_data, True, response->data.name.name);
300 static void name2uid_recv(TALLOC_CTX *mem_ctx, BOOL success,
301 struct winbindd_response *response,
302 void *c, void *private_data);
304 static void winbindd_name2uid_async(TALLOC_CTX *mem_ctx, const char *name,
305 void (*cont)(void *private_data, BOOL success,
306 uid_t uid),
307 void *private_data)
309 struct winbindd_request request;
310 ZERO_STRUCT(request);
311 request.cmd = WINBINDD_DUAL_NAME2UID;
312 fstrcpy(request.data.username, name);
313 do_async(mem_ctx, idmap_child(), &request, name2uid_recv,
314 (void *)cont, private_data);
317 enum winbindd_result winbindd_dual_name2uid(struct winbindd_domain *domain,
318 struct winbindd_cli_state *state)
320 struct passwd *pw;
322 /* Ensure null termination */
323 state->request.data.username
324 [sizeof(state->request.data.username)-1] = '\0';
326 DEBUG(3, ("[%5lu]: name2uid %s\n", (unsigned long)state->pid,
327 state->request.data.username));
329 pw = getpwnam(state->request.data.username);
330 if (pw == NULL) {
331 return WINBINDD_ERROR;
334 state->response.data.uid = pw->pw_uid;
335 return WINBINDD_OK;
338 static void name2uid_recv(TALLOC_CTX *mem_ctx, BOOL success,
339 struct winbindd_response *response,
340 void *c, void *private_data)
342 void (*cont)(void *priv, BOOL succ, uid_t uid) =
343 (void (*)(void *, BOOL, uid_t))c;
345 if (!success) {
346 DEBUG(5, ("Could not trigger name2uid\n"));
347 cont(private_data, False, 0);
348 return;
351 if (response->result != WINBINDD_OK) {
352 DEBUG(5, ("name2uid returned an error\n"));
353 cont(private_data, False, 0);
354 return;
357 cont(private_data, True, response->data.uid);
360 static void idmap_sid2gid_recv(TALLOC_CTX *mem_ctx, BOOL success,
361 struct winbindd_response *response,
362 void *c, void *private_data);
364 void idmap_sid2gid_async(TALLOC_CTX *mem_ctx, const DOM_SID *sid, BOOL alloc,
365 void (*cont)(void *private_data, BOOL success, gid_t gid),
366 void *private_data)
368 struct winbindd_request request;
369 ZERO_STRUCT(request);
370 request.cmd = WINBINDD_DUAL_SID2GID;
371 sid_to_string(request.data.dual_sid2id.sid, sid);
373 DEBUG(7,("idmap_sid2gid_async: Resolving %s to a gid\n",
374 request.data.dual_sid2id.sid));
376 request.data.dual_sid2id.alloc = alloc;
377 do_async(mem_ctx, idmap_child(), &request, idmap_sid2gid_recv,
378 (void *)cont, private_data);
381 enum winbindd_result winbindd_dual_sid2gid(struct winbindd_domain *domain,
382 struct winbindd_cli_state *state)
384 DOM_SID sid;
385 NTSTATUS result;
387 DEBUG(3, ("[%5lu]: sid to gid %s\n", (unsigned long)state->pid,
388 state->request.data.dual_sid2id.sid));
390 if (!string_to_sid(&sid, state->request.data.dual_sid2id.sid)) {
391 DEBUG(1, ("Could not get convert sid %s from string\n",
392 state->request.data.dual_sid2id.sid));
393 return WINBINDD_ERROR;
396 /* Find gid for this sid and return it, possibly ask the slow remote
397 * idmap */
399 result = idmap_sid_to_gid(&sid, &state->response.data.gid,
400 state->request.data.dual_sid2id.alloc ?
401 0 : IDMAP_FLAG_QUERY_ONLY);
403 /* If the lookup failed, the perhaps we need to look
404 at the passdb for local groups */
406 if ( !NT_STATUS_IS_OK(result) ) {
407 if ( sid_to_gid( &sid, &(state->response.data.gid) ) ) {
408 result = NT_STATUS_OK;
412 return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
415 static void idmap_sid2gid_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, gid_t gid) =
420 (void (*)(void *, BOOL, gid_t))c;
422 if (!success) {
423 DEBUG(5, ("Could not trigger sid2gid\n"));
424 cont(private_data, False, 0);
425 return;
428 if (response->result != WINBINDD_OK) {
429 DEBUG(5, ("sid2gid returned an error\n"));
430 cont(private_data, False, 0);
431 return;
434 cont(private_data, True, response->data.gid);
437 static void gid2name_recv(TALLOC_CTX *mem_ctx, BOOL success,
438 struct winbindd_response *response,
439 void *c, void *private_data)
441 void (*cont)(void *priv, BOOL succ, const char *name) =
442 (void (*)(void *, BOOL, const char *))c;
444 if (!success) {
445 DEBUG(5, ("Could not trigger gid2name\n"));
446 cont(private_data, False, NULL);
447 return;
450 if (response->result != WINBINDD_OK) {
451 DEBUG(5, ("gid2name returned an error\n"));
452 cont(private_data, False, NULL);
453 return;
456 cont(private_data, True, response->data.name.name);
459 void winbindd_gid2name_async(TALLOC_CTX *mem_ctx, gid_t gid,
460 void (*cont)(void *private_data, BOOL success,
461 const char *name),
462 void *private_data)
464 struct winbindd_request request;
465 ZERO_STRUCT(request);
466 request.cmd = WINBINDD_DUAL_GID2NAME;
467 request.data.gid = gid;
468 do_async(mem_ctx, idmap_child(), &request, gid2name_recv,
469 (void *)cont, private_data);
472 enum winbindd_result winbindd_dual_gid2name(struct winbindd_domain *domain,
473 struct winbindd_cli_state *state)
475 struct group *gr;
477 DEBUG(3, ("[%5lu]: gid2name %lu\n", (unsigned long)state->pid,
478 (unsigned long)state->request.data.gid));
480 gr = getgrgid(state->request.data.gid);
481 if (gr == NULL)
482 return WINBINDD_ERROR;
484 fstrcpy(state->response.data.name.name, gr->gr_name);
485 return WINBINDD_OK;
488 static void name2gid_recv(TALLOC_CTX *mem_ctx, BOOL success,
489 struct winbindd_response *response,
490 void *c, void *private_data);
492 static void winbindd_name2gid_async(TALLOC_CTX *mem_ctx, const char *name,
493 void (*cont)(void *private_data, BOOL success,
494 gid_t gid),
495 void *private_data)
497 struct winbindd_request request;
498 ZERO_STRUCT(request);
499 request.cmd = WINBINDD_DUAL_NAME2GID;
500 fstrcpy(request.data.groupname, name);
501 do_async(mem_ctx, idmap_child(), &request, name2gid_recv,
502 (void *)cont, private_data);
505 enum winbindd_result winbindd_dual_name2gid(struct winbindd_domain *domain,
506 struct winbindd_cli_state *state)
508 struct group *gr;
510 /* Ensure null termination */
511 state->request.data.groupname
512 [sizeof(state->request.data.groupname)-1] = '\0';
514 DEBUG(3, ("[%5lu]: name2gid %s\n", (unsigned long)state->pid,
515 state->request.data.groupname));
517 gr = getgrnam(state->request.data.groupname);
518 if (gr == NULL) {
519 return WINBINDD_ERROR;
522 state->response.data.gid = gr->gr_gid;
523 return WINBINDD_OK;
526 static void name2gid_recv(TALLOC_CTX *mem_ctx, BOOL success,
527 struct winbindd_response *response,
528 void *c, void *private_data)
530 void (*cont)(void *priv, BOOL succ, gid_t gid) =
531 (void (*)(void *, BOOL, gid_t))c;
533 if (!success) {
534 DEBUG(5, ("Could not trigger name2gid\n"));
535 cont(private_data, False, 0);
536 return;
539 if (response->result != WINBINDD_OK) {
540 DEBUG(5, ("name2gid returned an error\n"));
541 cont(private_data, False, 0);
542 return;
545 cont(private_data, True, response->data.gid);
549 static void lookupsid_recv(TALLOC_CTX *mem_ctx, BOOL success,
550 struct winbindd_response *response,
551 void *c, void *private_data)
553 void (*cont)(void *priv, BOOL succ, const char *dom_name,
554 const char *name, enum SID_NAME_USE type) =
555 (void (*)(void *, BOOL, const char *, const char *,
556 enum SID_NAME_USE))c;
558 if (!success) {
559 DEBUG(5, ("Could not trigger lookupsid\n"));
560 cont(private_data, False, NULL, NULL, SID_NAME_UNKNOWN);
561 return;
564 if (response->result != WINBINDD_OK) {
565 DEBUG(5, ("lookupsid returned an error\n"));
566 cont(private_data, False, NULL, NULL, SID_NAME_UNKNOWN);
567 return;
570 cont(private_data, True, response->data.name.dom_name,
571 response->data.name.name,
572 (enum SID_NAME_USE)response->data.name.type);
575 void winbindd_lookupsid_async(TALLOC_CTX *mem_ctx, const DOM_SID *sid,
576 void (*cont)(void *private_data, BOOL success,
577 const char *dom_name,
578 const char *name,
579 enum SID_NAME_USE type),
580 void *private_data)
582 struct winbindd_domain *domain;
583 struct winbindd_request request;
585 domain = find_lookup_domain_from_sid(sid);
586 if (domain == NULL) {
587 DEBUG(5, ("Could not find domain for sid %s\n",
588 sid_string_static(sid)));
589 cont(private_data, False, NULL, NULL, SID_NAME_UNKNOWN);
590 return;
593 ZERO_STRUCT(request);
594 request.cmd = WINBINDD_LOOKUPSID;
595 fstrcpy(request.data.sid, sid_string_static(sid));
597 do_async_domain(mem_ctx, domain, &request, lookupsid_recv,
598 (void *)cont, private_data);
601 enum winbindd_result winbindd_dual_lookupsid(struct winbindd_domain *domain,
602 struct winbindd_cli_state *state)
604 enum SID_NAME_USE type;
605 DOM_SID sid;
606 fstring name;
607 fstring dom_name;
609 /* Ensure null termination */
610 state->request.data.sid[sizeof(state->request.data.sid)-1]='\0';
612 DEBUG(3, ("[%5lu]: lookupsid %s\n", (unsigned long)state->pid,
613 state->request.data.sid));
615 /* Lookup sid from PDC using lsa_lookup_sids() */
617 if (!string_to_sid(&sid, state->request.data.sid)) {
618 DEBUG(5, ("%s not a SID\n", state->request.data.sid));
619 return WINBINDD_ERROR;
622 /* Lookup the sid */
624 if (!winbindd_lookup_name_by_sid(state->mem_ctx, &sid, dom_name, name,
625 &type)) {
626 return WINBINDD_ERROR;
629 fstrcpy(state->response.data.name.dom_name, dom_name);
630 fstrcpy(state->response.data.name.name, name);
631 state->response.data.name.type = type;
633 return WINBINDD_OK;
636 static void lookupname_recv(TALLOC_CTX *mem_ctx, BOOL success,
637 struct winbindd_response *response,
638 void *c, void *private_data)
640 void (*cont)(void *priv, BOOL succ, const DOM_SID *sid,
641 enum SID_NAME_USE type) =
642 (void (*)(void *, BOOL, const DOM_SID *, enum SID_NAME_USE))c;
643 DOM_SID sid;
645 if (!success) {
646 DEBUG(5, ("Could not trigger lookup_name\n"));
647 cont(private_data, False, NULL, SID_NAME_UNKNOWN);
648 return;
651 if (response->result != WINBINDD_OK) {
652 DEBUG(5, ("lookup_name returned an error\n"));
653 cont(private_data, False, NULL, SID_NAME_UNKNOWN);
654 return;
657 if (!string_to_sid(&sid, response->data.sid.sid)) {
658 DEBUG(0, ("Could not convert string %s to sid\n",
659 response->data.sid.sid));
660 cont(private_data, False, NULL, SID_NAME_UNKNOWN);
661 return;
664 cont(private_data, True, &sid,
665 (enum SID_NAME_USE)response->data.sid.type);
668 void winbindd_lookupname_async(TALLOC_CTX *mem_ctx, const char *dom_name,
669 const char *name,
670 void (*cont)(void *private_data, BOOL success,
671 const DOM_SID *sid,
672 enum SID_NAME_USE type),
673 void *private_data)
675 struct winbindd_request request;
676 struct winbindd_domain *domain;
678 domain = find_lookup_domain_from_name(dom_name);
680 if (domain == NULL) {
681 DEBUG(5, ("Could not find domain for name %s\n", dom_name));
682 cont(private_data, False, NULL, SID_NAME_UNKNOWN);
683 return;
686 ZERO_STRUCT(request);
687 request.cmd = WINBINDD_LOOKUPNAME;
688 fstrcpy(request.data.name.dom_name, dom_name);
689 fstrcpy(request.data.name.name, name);
691 do_async_domain(mem_ctx, domain, &request, lookupname_recv,
692 (void *)cont, private_data);
695 enum winbindd_result winbindd_dual_lookupname(struct winbindd_domain *domain,
696 struct winbindd_cli_state *state)
698 enum SID_NAME_USE type;
699 char *name_domain, *name_user;
700 DOM_SID sid;
701 char *p;
703 /* Ensure null termination */
704 state->request.data.sid[sizeof(state->request.data.name.dom_name)-1]='\0';
706 /* Ensure null termination */
707 state->request.data.sid[sizeof(state->request.data.name.name)-1]='\0';
709 /* cope with the name being a fully qualified name */
710 p = strstr(state->request.data.name.name, lp_winbind_separator());
711 if (p) {
712 *p = 0;
713 name_domain = state->request.data.name.name;
714 name_user = p+1;
715 } else {
716 name_domain = state->request.data.name.dom_name;
717 name_user = state->request.data.name.name;
720 DEBUG(3, ("[%5lu]: lookupname %s%s%s\n", (unsigned long)state->pid,
721 name_domain, lp_winbind_separator(), name_user));
723 /* Lookup name from PDC using lsa_lookup_names() */
724 if (!winbindd_lookup_sid_by_name(state->mem_ctx, domain, name_domain,
725 name_user, &sid, &type)) {
726 return WINBINDD_ERROR;
729 sid_to_string(state->response.data.sid.sid, &sid);
730 state->response.data.sid.type = type;
732 return WINBINDD_OK;
735 BOOL print_sidlist(TALLOC_CTX *mem_ctx, const DOM_SID *sids,
736 size_t num_sids, char **result, ssize_t *len)
738 size_t i;
739 size_t buflen = 0;
741 *len = 0;
742 *result = NULL;
743 for (i=0; i<num_sids; i++) {
744 sprintf_append(mem_ctx, result, len, &buflen,
745 "%s\n", sid_string_static(&sids[i]));
748 if ((num_sids != 0) && (*result == NULL)) {
749 return False;
752 return True;
755 static BOOL parse_sidlist(TALLOC_CTX *mem_ctx, char *sidstr,
756 DOM_SID **sids, size_t *num_sids)
758 char *p, *q;
760 p = sidstr;
761 if (p == NULL)
762 return False;
764 while (p[0] != '\0') {
765 DOM_SID sid;
766 q = strchr(p, '\n');
767 if (q == NULL) {
768 DEBUG(0, ("Got invalid sidstr: %s\n", p));
769 return False;
771 *q = '\0';
772 q += 1;
773 if (!string_to_sid(&sid, p)) {
774 DEBUG(0, ("Could not parse sid %s\n", p));
775 return False;
777 add_sid_to_array(mem_ctx, &sid, sids, num_sids);
778 p = q;
780 return True;
783 static BOOL parse_ridlist(TALLOC_CTX *mem_ctx, char *ridstr,
784 uint32 **rids, size_t *num_rids)
786 char *p;
788 p = ridstr;
789 if (p == NULL)
790 return False;
792 while (p[0] != '\0') {
793 uint32 rid;
794 char *q;
795 rid = strtoul(p, &q, 10);
796 if (*q != '\n') {
797 DEBUG(0, ("Got invalid ridstr: %s\n", p));
798 return False;
800 p = q+1;
801 ADD_TO_ARRAY(mem_ctx, uint32, rid, rids, num_rids);
803 return True;
806 enum winbindd_result winbindd_dual_lookuprids(struct winbindd_domain *domain,
807 struct winbindd_cli_state *state)
809 uint32 *rids = NULL;
810 size_t i, buflen, num_rids = 0;
811 ssize_t len;
812 DOM_SID domain_sid;
813 char *domain_name;
814 char **names;
815 enum SID_NAME_USE *types;
816 NTSTATUS status;
817 char *result;
819 DEBUG(10, ("Looking up RIDs for domain %s (%s)\n",
820 state->request.domain_name,
821 state->request.data.sid));
823 if (!parse_ridlist(state->mem_ctx, state->request.extra_data.data,
824 &rids, &num_rids)) {
825 DEBUG(5, ("Could not parse ridlist\n"));
826 return WINBINDD_ERROR;
829 if (!string_to_sid(&domain_sid, state->request.data.sid)) {
830 DEBUG(5, ("Could not parse domain sid %s\n",
831 state->request.data.sid));
832 return WINBINDD_ERROR;
835 status = domain->methods->rids_to_names(domain, state->mem_ctx,
836 &domain_sid, rids, num_rids,
837 &domain_name,
838 &names, &types);
840 if (!NT_STATUS_IS_OK(status) &&
841 !NT_STATUS_EQUAL(status, STATUS_SOME_UNMAPPED)) {
842 return WINBINDD_ERROR;
845 len = 0;
846 buflen = 0;
847 result = NULL;
849 for (i=0; i<num_rids; i++) {
850 sprintf_append(state->mem_ctx, &result, &len, &buflen,
851 "%d %s\n", types[i], names[i]);
854 fstrcpy(state->response.data.domain_name, domain_name);
856 if (result != NULL) {
857 state->response.extra_data.data = SMB_STRDUP(result);
858 state->response.length += len+1;
861 return WINBINDD_OK;
864 static void getsidaliases_recv(TALLOC_CTX *mem_ctx, BOOL success,
865 struct winbindd_response *response,
866 void *c, void *private_data)
868 void (*cont)(void *priv, BOOL succ,
869 DOM_SID *aliases, size_t num_aliases) =
870 (void (*)(void *, BOOL, DOM_SID *, size_t))c;
871 char *aliases_str;
872 DOM_SID *sids = NULL;
873 size_t num_sids = 0;
875 if (!success) {
876 DEBUG(5, ("Could not trigger getsidaliases\n"));
877 cont(private_data, success, NULL, 0);
878 return;
881 if (response->result != WINBINDD_OK) {
882 DEBUG(5, ("getsidaliases returned an error\n"));
883 cont(private_data, False, NULL, 0);
884 return;
887 aliases_str = (char *)response->extra_data.data;
889 if (aliases_str == NULL) {
890 DEBUG(10, ("getsidaliases return 0 SIDs\n"));
891 cont(private_data, True, NULL, 0);
892 return;
895 if (!parse_sidlist(mem_ctx, aliases_str, &sids, &num_sids)) {
896 DEBUG(0, ("Could not parse sids\n"));
897 cont(private_data, False, NULL, 0);
898 return;
901 SAFE_FREE(response->extra_data.data);
903 cont(private_data, True, sids, num_sids);
906 void winbindd_getsidaliases_async(struct winbindd_domain *domain,
907 TALLOC_CTX *mem_ctx,
908 const DOM_SID *sids, size_t num_sids,
909 void (*cont)(void *private_data,
910 BOOL success,
911 const DOM_SID *aliases,
912 size_t num_aliases),
913 void *private_data)
915 struct winbindd_request request;
916 char *sidstr = NULL;
917 ssize_t len;
919 if (num_sids == 0) {
920 cont(private_data, True, NULL, 0);
921 return;
924 if (!print_sidlist(mem_ctx, sids, num_sids, &sidstr, &len)) {
925 cont(private_data, False, NULL, 0);
926 return;
929 ZERO_STRUCT(request);
930 request.cmd = WINBINDD_DUAL_GETSIDALIASES;
931 request.extra_len = len;
932 request.extra_data.data = sidstr;
934 do_async_domain(mem_ctx, domain, &request, getsidaliases_recv,
935 (void *)cont, private_data);
938 enum winbindd_result winbindd_dual_getsidaliases(struct winbindd_domain *domain,
939 struct winbindd_cli_state *state)
941 DOM_SID *sids = NULL;
942 size_t num_sids = 0;
943 char *sidstr;
944 ssize_t len;
945 size_t i;
946 uint32 num_aliases;
947 uint32 *alias_rids;
948 NTSTATUS result;
950 DEBUG(3, ("[%5lu]: getsidaliases\n", (unsigned long)state->pid));
952 sidstr = state->request.extra_data.data;
953 if (sidstr == NULL)
954 sidstr = talloc_strdup(state->mem_ctx, "\n"); /* No SID */
956 DEBUG(10, ("Sidlist: %s\n", sidstr));
958 if (!parse_sidlist(state->mem_ctx, sidstr, &sids, &num_sids)) {
959 DEBUG(0, ("Could not parse SID list: %s\n", sidstr));
960 return WINBINDD_ERROR;
963 num_aliases = 0;
964 alias_rids = NULL;
966 result = domain->methods->lookup_useraliases(domain,
967 state->mem_ctx,
968 num_sids, sids,
969 &num_aliases,
970 &alias_rids);
972 if (!NT_STATUS_IS_OK(result)) {
973 DEBUG(3, ("Could not lookup_useraliases: %s\n",
974 nt_errstr(result)));
975 return WINBINDD_ERROR;
978 num_sids = 0;
979 sids = NULL;
981 DEBUG(10, ("Got %d aliases\n", num_aliases));
983 for (i=0; i<num_aliases; i++) {
984 DOM_SID sid;
985 DEBUGADD(10, (" rid %d\n", alias_rids[i]));
986 sid_copy(&sid, &domain->sid);
987 sid_append_rid(&sid, alias_rids[i]);
988 add_sid_to_array(state->mem_ctx, &sid, &sids, &num_sids);
992 if (!print_sidlist(NULL, sids, num_sids, &sidstr, &len)) {
993 DEBUG(0, ("Could not print_sidlist\n"));
994 state->response.extra_data.data = NULL;
995 return WINBINDD_ERROR;
998 state->response.extra_data.data = sidstr;
1000 if (state->response.extra_data.data != NULL) {
1001 DEBUG(10, ("aliases_list: %s\n",
1002 (char *)state->response.extra_data.data));
1003 state->response.length += len+1;
1006 return WINBINDD_OK;
1009 struct gettoken_state {
1010 TALLOC_CTX *mem_ctx;
1011 DOM_SID user_sid;
1012 struct winbindd_domain *alias_domain;
1013 struct winbindd_domain *local_alias_domain;
1014 struct winbindd_domain *builtin_domain;
1015 DOM_SID *sids;
1016 size_t num_sids;
1017 void (*cont)(void *private_data, BOOL success, DOM_SID *sids, size_t num_sids);
1018 void *private_data;
1021 static void gettoken_recvdomgroups(TALLOC_CTX *mem_ctx, BOOL success,
1022 struct winbindd_response *response,
1023 void *c, void *private_data);
1024 static void gettoken_recvaliases(void *private_data, BOOL success,
1025 const DOM_SID *aliases,
1026 size_t num_aliases);
1029 void winbindd_gettoken_async(TALLOC_CTX *mem_ctx, const DOM_SID *user_sid,
1030 void (*cont)(void *private_data, BOOL success,
1031 DOM_SID *sids, size_t num_sids),
1032 void *private_data)
1034 struct winbindd_domain *domain;
1035 struct winbindd_request request;
1036 struct gettoken_state *state;
1038 state = TALLOC_ZERO_P(mem_ctx, struct gettoken_state);
1039 if (state == NULL) {
1040 DEBUG(0, ("talloc failed\n"));
1041 cont(private_data, False, NULL, 0);
1042 return;
1045 state->mem_ctx = mem_ctx;
1046 sid_copy(&state->user_sid, user_sid);
1047 state->alias_domain = find_our_domain();
1048 state->local_alias_domain = find_domain_from_name( get_global_sam_name() );
1049 state->builtin_domain = find_builtin_domain();
1050 state->cont = cont;
1051 state->private_data = private_data;
1053 domain = find_domain_from_sid_noinit(user_sid);
1054 if (domain == NULL) {
1055 DEBUG(5, ("Could not find domain from SID %s\n",
1056 sid_string_static(user_sid)));
1057 cont(private_data, False, NULL, 0);
1058 return;
1061 ZERO_STRUCT(request);
1062 request.cmd = WINBINDD_GETUSERDOMGROUPS;
1063 fstrcpy(request.data.sid, sid_string_static(user_sid));
1065 do_async_domain(mem_ctx, domain, &request, gettoken_recvdomgroups,
1066 NULL, state);
1069 static void gettoken_recvdomgroups(TALLOC_CTX *mem_ctx, BOOL success,
1070 struct winbindd_response *response,
1071 void *c, void *private_data)
1073 struct gettoken_state *state =
1074 talloc_get_type_abort(private_data, struct gettoken_state);
1075 char *sids_str;
1077 if (!success) {
1078 DEBUG(10, ("Could not get domain groups\n"));
1079 state->cont(state->private_data, False, NULL, 0);
1080 return;
1083 sids_str = (char *)response->extra_data.data;
1085 if (sids_str == NULL) {
1086 /* This could be normal if we are dealing with a
1087 local user and local groups */
1089 if ( !sid_check_is_in_our_domain( &state->user_sid ) ) {
1090 DEBUG(10, ("Received no domain groups\n"));
1091 state->cont(state->private_data, True, NULL, 0);
1092 return;
1096 state->sids = NULL;
1097 state->num_sids = 0;
1099 add_sid_to_array(mem_ctx, &state->user_sid, &state->sids,
1100 &state->num_sids);
1102 if (sids_str && !parse_sidlist(mem_ctx, sids_str, &state->sids,
1103 &state->num_sids)) {
1104 DEBUG(0, ("Could not parse sids\n"));
1105 state->cont(state->private_data, False, NULL, 0);
1106 return;
1109 SAFE_FREE(response->extra_data.data);
1111 if (state->alias_domain == NULL) {
1112 DEBUG(10, ("Don't expand domain local groups\n"));
1113 state->cont(state->private_data, True, state->sids,
1114 state->num_sids);
1115 return;
1118 winbindd_getsidaliases_async(state->alias_domain, mem_ctx,
1119 state->sids, state->num_sids,
1120 gettoken_recvaliases, state);
1123 static void gettoken_recvaliases(void *private_data, BOOL success,
1124 const DOM_SID *aliases,
1125 size_t num_aliases)
1127 struct gettoken_state *state = (struct gettoken_state *)private_data;
1128 size_t i;
1130 if (!success) {
1131 DEBUG(10, ("Could not receive domain local groups\n"));
1132 state->cont(state->private_data, False, NULL, 0);
1133 return;
1136 for (i=0; i<num_aliases; i++)
1137 add_sid_to_array(state->mem_ctx, &aliases[i],
1138 &state->sids, &state->num_sids);
1140 if (state->local_alias_domain != NULL) {
1141 struct winbindd_domain *local_domain = state->local_alias_domain;
1142 DEBUG(10, ("Expanding our own local groups\n"));
1143 state->local_alias_domain = NULL;
1144 winbindd_getsidaliases_async(local_domain, state->mem_ctx,
1145 state->sids, state->num_sids,
1146 gettoken_recvaliases, state);
1147 return;
1150 if (state->builtin_domain != NULL) {
1151 struct winbindd_domain *builtin_domain = state->builtin_domain;
1152 DEBUG(10, ("Expanding our own BUILTIN groups\n"));
1153 state->builtin_domain = NULL;
1154 winbindd_getsidaliases_async(builtin_domain, state->mem_ctx,
1155 state->sids, state->num_sids,
1156 gettoken_recvaliases, state);
1157 return;
1160 state->cont(state->private_data, True, state->sids, state->num_sids);
1163 struct sid2uid_state {
1164 TALLOC_CTX *mem_ctx;
1165 DOM_SID sid;
1166 char *username;
1167 uid_t uid;
1168 void (*cont)(void *private_data, BOOL success, uid_t uid);
1169 void *private_data;
1172 static void sid2uid_lookup_sid_recv(void *private_data, BOOL success,
1173 const char *dom_name, const char *name,
1174 enum SID_NAME_USE type);
1175 static void sid2uid_noalloc_recv(void *private_data, BOOL success, uid_t uid);
1176 static void sid2uid_alloc_recv(void *private_data, BOOL success, uid_t uid);
1177 static void sid2uid_name2uid_recv(void *private_data, BOOL success, uid_t uid);
1178 static void sid2uid_set_mapping_recv(void *private_data, BOOL success);
1180 void winbindd_sid2uid_async(TALLOC_CTX *mem_ctx, const DOM_SID *sid,
1181 void (*cont)(void *private_data, BOOL success,
1182 uid_t uid),
1183 void *private_data)
1185 struct sid2uid_state *state;
1186 NTSTATUS result;
1187 uid_t uid;
1189 if (idmap_proxyonly()) {
1190 DEBUG(10, ("idmap proxy only\n"));
1191 cont(private_data, False, 0);
1192 return;
1195 /* Query only the local tdb, everything else might possibly block */
1197 result = idmap_sid_to_uid(sid, &uid, IDMAP_FLAG_QUERY_ONLY|IDMAP_FLAG_CACHE_ONLY);
1199 if (NT_STATUS_IS_OK(result)) {
1200 cont(private_data, True, uid);
1201 return;
1204 state = TALLOC_ZERO_P(mem_ctx, struct sid2uid_state);
1205 if (state == NULL) {
1206 DEBUG(0, ("talloc failed\n"));
1207 cont(private_data, False, 0);
1208 return;
1211 state->mem_ctx = mem_ctx;
1212 state->sid = *sid;
1213 state->cont = cont;
1214 state->private_data = private_data;
1216 /* Let's see if it's really a user before allocating a uid */
1218 winbindd_lookupsid_async(mem_ctx, sid, sid2uid_lookup_sid_recv, state);
1221 static void sid2uid_lookup_sid_recv(void *private_data, BOOL success,
1222 const char *dom_name, const char *name,
1223 enum SID_NAME_USE type)
1225 struct sid2uid_state *state =
1226 talloc_get_type_abort(private_data, struct sid2uid_state);
1228 if (!success) {
1229 DEBUG(5, ("Could not trigger lookup_sid\n"));
1230 state->cont(state->private_data, False, 0);
1231 return;
1234 if ((type != SID_NAME_USER) && (type != SID_NAME_COMPUTER)) {
1235 DEBUG(5, ("SID is not a user\n"));
1236 state->cont(state->private_data, False, 0);
1237 return;
1240 state->username = talloc_strdup(state->mem_ctx, name);
1242 /* Ask the possibly blocking remote IDMAP */
1244 idmap_sid2uid_async(state->mem_ctx, &state->sid, False,
1245 sid2uid_noalloc_recv, state);
1248 static void sid2uid_noalloc_recv(void *private_data, BOOL success, uid_t uid)
1250 struct sid2uid_state *state =
1251 talloc_get_type_abort(private_data, struct sid2uid_state);
1253 if (success) {
1254 DEBUG(10, ("found uid for sid %s in remote backend\n",
1255 sid_string_static(&state->sid)));
1256 state->cont(state->private_data, True, uid);
1257 return;
1260 if (lp_winbind_trusted_domains_only() &&
1261 (sid_compare_domain(&state->sid, &find_our_domain()->sid) == 0)) {
1262 DEBUG(10, ("Trying to go via nss\n"));
1263 winbindd_name2uid_async(state->mem_ctx, state->username,
1264 sid2uid_name2uid_recv, state);
1265 return;
1268 /* To be done: Here we're going to try the unixinfo pipe */
1270 /* Now allocate a uid */
1272 idmap_sid2uid_async(state->mem_ctx, &state->sid, True,
1273 sid2uid_alloc_recv, state);
1276 static void sid2uid_alloc_recv(void *private_data, BOOL success, uid_t uid)
1278 struct sid2uid_state *state =
1279 talloc_get_type_abort(private_data, struct sid2uid_state);
1281 if (!success) {
1282 DEBUG(5, ("Could not allocate uid\n"));
1283 state->cont(state->private_data, False, 0);
1284 return;
1287 state->cont(state->private_data, True, uid);
1290 static void sid2uid_name2uid_recv(void *private_data, BOOL success, uid_t uid)
1292 struct sid2uid_state *state =
1293 talloc_get_type_abort(private_data, struct sid2uid_state);
1294 unid_t id;
1296 if (!success) {
1297 DEBUG(5, ("Could not find uid for name %s\n",
1298 state->username));
1299 state->cont(state->private_data, False, 0);
1300 return;
1303 state->uid = uid;
1305 id.uid = uid;
1306 idmap_set_mapping_async(state->mem_ctx, &state->sid, id, ID_USERID,
1307 sid2uid_set_mapping_recv, state);
1310 static void sid2uid_set_mapping_recv(void *private_data, BOOL success)
1312 struct sid2uid_state *state =
1313 talloc_get_type_abort(private_data, struct sid2uid_state);
1315 if (!success) {
1316 DEBUG(5, ("Could not set ID mapping for sid %s\n",
1317 sid_string_static(&state->sid)));
1318 state->cont(state->private_data, False, 0);
1319 return;
1322 state->cont(state->private_data, True, state->uid);
1325 struct sid2gid_state {
1326 TALLOC_CTX *mem_ctx;
1327 DOM_SID sid;
1328 char *groupname;
1329 gid_t gid;
1330 void (*cont)(void *private_data, BOOL success, gid_t gid);
1331 void *private_data;
1334 static void sid2gid_lookup_sid_recv(void *private_data, BOOL success,
1335 const char *dom_name, const char *name,
1336 enum SID_NAME_USE type);
1337 static void sid2gid_noalloc_recv(void *private_data, BOOL success, gid_t gid);
1338 static void sid2gid_alloc_recv(void *private_data, BOOL success, gid_t gid);
1339 static void sid2gid_name2gid_recv(void *private_data, BOOL success, gid_t gid);
1340 static void sid2gid_set_mapping_recv(void *private_data, BOOL success);
1342 void winbindd_sid2gid_async(TALLOC_CTX *mem_ctx, const DOM_SID *sid,
1343 void (*cont)(void *private_data, BOOL success,
1344 gid_t gid),
1345 void *private_data)
1347 struct sid2gid_state *state;
1348 NTSTATUS result;
1349 gid_t gid;
1351 if (idmap_proxyonly()) {
1352 DEBUG(10, ("idmap proxy only\n"));
1353 cont(private_data, False, 0);
1354 return;
1357 /* Query only the local tdb, everything else might possibly block */
1359 result = idmap_sid_to_gid(sid, &gid, IDMAP_FLAG_QUERY_ONLY|IDMAP_FLAG_CACHE_ONLY);
1361 if (NT_STATUS_IS_OK(result)) {
1362 cont(private_data, True, gid);
1363 return;
1366 state = TALLOC_ZERO_P(mem_ctx, struct sid2gid_state);
1367 if (state == NULL) {
1368 DEBUG(0, ("talloc failed\n"));
1369 cont(private_data, False, 0);
1370 return;
1373 state->mem_ctx = mem_ctx;
1374 state->sid = *sid;
1375 state->cont = cont;
1376 state->private_data = private_data;
1378 /* Let's see if it's really a user before allocating a gid */
1380 winbindd_lookupsid_async(mem_ctx, sid, sid2gid_lookup_sid_recv, state);
1383 static void sid2gid_lookup_sid_recv(void *private_data, BOOL success,
1384 const char *dom_name, const char *name,
1385 enum SID_NAME_USE type)
1387 struct sid2gid_state *state =
1388 talloc_get_type_abort(private_data, struct sid2gid_state);
1390 if (!success) {
1391 DEBUG(5, ("Could not trigger lookup_sid\n"));
1392 state->cont(state->private_data, False, 0);
1393 return;
1396 if (((type != SID_NAME_DOM_GRP) && (type != SID_NAME_ALIAS) &&
1397 (type != SID_NAME_WKN_GRP))) {
1398 DEBUG(5, ("SID is not a group\n"));
1399 state->cont(state->private_data, False, 0);
1400 return;
1403 state->groupname = talloc_strdup(state->mem_ctx, name);
1405 /* Ask the possibly blocking remote IDMAP and allocate */
1407 idmap_sid2gid_async(state->mem_ctx, &state->sid, False,
1408 sid2gid_noalloc_recv, state);
1411 static void sid2gid_noalloc_recv(void *private_data, BOOL success, gid_t gid)
1413 struct sid2gid_state *state =
1414 talloc_get_type_abort(private_data, struct sid2gid_state);
1416 if (success) {
1417 DEBUG(10, ("found gid for sid %s in remote backend\n",
1418 sid_string_static(&state->sid)));
1419 state->cont(state->private_data, True, gid);
1420 return;
1423 if (lp_winbind_trusted_domains_only() &&
1424 (sid_compare_domain(&state->sid, &find_our_domain()->sid) == 0)) {
1425 DEBUG(10, ("Trying to go via nss\n"));
1426 winbindd_name2gid_async(state->mem_ctx, state->groupname,
1427 sid2gid_name2gid_recv, state);
1428 return;
1431 /* To be done: Here we're going to try the unixinfo pipe */
1433 /* Now allocate a gid */
1435 idmap_sid2gid_async(state->mem_ctx, &state->sid, True,
1436 sid2gid_alloc_recv, state);
1439 static void sid2gid_alloc_recv(void *private_data, BOOL success, gid_t gid)
1441 struct sid2gid_state *state =
1442 talloc_get_type_abort(private_data, struct sid2gid_state);
1444 if (!success) {
1445 DEBUG(5, ("Could not allocate gid\n"));
1446 state->cont(state->private_data, False, 0);
1447 return;
1450 state->cont(state->private_data, True, gid);
1453 static void sid2gid_name2gid_recv(void *private_data, BOOL success, gid_t gid)
1455 struct sid2gid_state *state =
1456 talloc_get_type_abort(private_data, struct sid2gid_state);
1457 unid_t id;
1459 if (!success) {
1460 DEBUG(5, ("Could not find gid for name %s\n",
1461 state->groupname));
1462 state->cont(state->private_data, False, 0);
1463 return;
1466 state->gid = gid;
1468 id.gid = gid;
1469 idmap_set_mapping_async(state->mem_ctx, &state->sid, id, ID_GROUPID,
1470 sid2gid_set_mapping_recv, state);
1473 static void sid2gid_set_mapping_recv(void *private_data, BOOL success)
1475 struct sid2gid_state *state =
1476 talloc_get_type_abort(private_data, struct sid2gid_state);
1478 if (!success) {
1479 DEBUG(5, ("Could not set ID mapping for sid %s\n",
1480 sid_string_static(&state->sid)));
1481 state->cont(state->private_data, False, 0);
1482 return;
1485 state->cont(state->private_data, True, state->gid);
1488 static void query_user_recv(TALLOC_CTX *mem_ctx, BOOL success,
1489 struct winbindd_response *response,
1490 void *c, void *private_data)
1492 void (*cont)(void *priv, BOOL succ, const char *acct_name,
1493 const char *full_name, const char *homedir,
1494 const char *shell, uint32 group_rid) =
1495 (void (*)(void *, BOOL, const char *, const char *,
1496 const char *, const char *, uint32))c;
1498 if (!success) {
1499 DEBUG(5, ("Could not trigger query_user\n"));
1500 cont(private_data, False, NULL, NULL, NULL, NULL, -1);
1501 return;
1504 cont(private_data, True, response->data.user_info.acct_name,
1505 response->data.user_info.full_name,
1506 response->data.user_info.homedir,
1507 response->data.user_info.shell,
1508 response->data.user_info.group_rid);
1511 void query_user_async(TALLOC_CTX *mem_ctx, struct winbindd_domain *domain,
1512 const DOM_SID *sid,
1513 void (*cont)(void *private_data, BOOL success,
1514 const char *acct_name,
1515 const char *full_name,
1516 const char *homedir,
1517 const char *shell,
1518 uint32 group_rid),
1519 void *private_data)
1521 struct winbindd_request request;
1522 ZERO_STRUCT(request);
1523 request.cmd = WINBINDD_DUAL_USERINFO;
1524 sid_to_string(request.data.sid, sid);
1525 do_async_domain(mem_ctx, domain, &request, query_user_recv,
1526 (void *)cont, private_data);
1529 /* The following uid2sid/gid2sid functions has been contributed by
1530 * Keith Reynolds <Keith.Reynolds@centrify.com> */
1532 static void winbindd_uid2sid_recv(TALLOC_CTX *mem_ctx, BOOL success,
1533 struct winbindd_response *response,
1534 void *c, void *private_data)
1536 void (*cont)(void *priv, BOOL succ, const char *sid) =
1537 (void (*)(void *, BOOL, const char *))c;
1539 if (!success) {
1540 DEBUG(5, ("Could not trigger uid2sid\n"));
1541 cont(private_data, False, NULL);
1542 return;
1545 if (response->result != WINBINDD_OK) {
1546 DEBUG(5, ("uid2sid returned an error\n"));
1547 cont(private_data, False, NULL);
1548 return;
1551 cont(private_data, True, response->data.sid.sid);
1554 void winbindd_uid2sid_async(TALLOC_CTX *mem_ctx, uid_t uid,
1555 void (*cont)(void *private_data, BOOL success, const char *sid),
1556 void *private_data)
1558 struct winbindd_request request;
1560 ZERO_STRUCT(request);
1561 request.cmd = WINBINDD_DUAL_UID2SID;
1562 request.data.uid = uid;
1563 do_async(mem_ctx, idmap_child(), &request, winbindd_uid2sid_recv,
1564 (void *)cont, private_data);
1567 enum winbindd_result winbindd_dual_uid2sid(struct winbindd_domain *domain,
1568 struct winbindd_cli_state *state)
1570 DOM_SID sid;
1571 NTSTATUS result;
1573 DEBUG(3,("[%5lu]: uid to sid %lu\n",
1574 (unsigned long)state->pid,
1575 (unsigned long) state->request.data.uid));
1577 /* Find sid for this uid and return it, possibly ask the slow remote idmap */
1578 result = idmap_uid_to_sid(&sid, state->request.data.uid, IDMAP_FLAG_NONE);
1580 if (NT_STATUS_IS_OK(result)) {
1581 sid_to_string(state->response.data.sid.sid, &sid);
1582 state->response.data.sid.type = SID_NAME_USER;
1583 return WINBINDD_OK;
1586 return WINBINDD_ERROR;
1589 static void winbindd_gid2sid_recv(TALLOC_CTX *mem_ctx, BOOL success,
1590 struct winbindd_response *response,
1591 void *c, void *private_data)
1593 void (*cont)(void *priv, BOOL succ, const char *sid) =
1594 (void (*)(void *, BOOL, const char *))c;
1596 if (!success) {
1597 DEBUG(5, ("Could not trigger gid2sid\n"));
1598 cont(private_data, False, NULL);
1599 return;
1602 if (response->result != WINBINDD_OK) {
1603 DEBUG(5, ("gid2sid returned an error\n"));
1604 cont(private_data, False, NULL);
1605 return;
1608 cont(private_data, True, response->data.sid.sid);
1611 void winbindd_gid2sid_async(TALLOC_CTX *mem_ctx, gid_t gid,
1612 void (*cont)(void *private_data, BOOL success, const char *sid),
1613 void *private_data)
1615 struct winbindd_request request;
1617 ZERO_STRUCT(request);
1618 request.cmd = WINBINDD_DUAL_GID2SID;
1619 request.data.gid = gid;
1620 do_async(mem_ctx, idmap_child(), &request, winbindd_gid2sid_recv,
1621 (void *)cont, private_data);
1624 enum winbindd_result winbindd_dual_gid2sid(struct winbindd_domain *domain,
1625 struct winbindd_cli_state *state)
1627 DOM_SID sid;
1628 NTSTATUS result;
1630 DEBUG(3,("[%5lu]: gid %lu to sid\n",
1631 (unsigned long)state->pid,
1632 (unsigned long) state->request.data.gid));
1634 /* Find sid for this gid and return it, possibly ask the slow remote idmap */
1635 result = idmap_gid_to_sid(&sid, state->request.data.gid, IDMAP_FLAG_NONE);
1637 if (NT_STATUS_IS_OK(result)) {
1638 sid_to_string(state->response.data.sid.sid, &sid);
1639 DEBUG(10, ("[%5lu]: retrieved sid: %s\n",
1640 (unsigned long)state->pid,
1641 state->response.data.sid.sid));
1642 state->response.data.sid.type = SID_NAME_DOM_GRP;
1643 return WINBINDD_OK;
1646 return WINBINDD_ERROR;