objectclass -> objectClass
[Samba/gebeck_regimport.git] / source3 / winbindd / winbindd_async.c
blob2ff5ef230d393877dfb77c81ee263a1de012f899
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 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/>.
34 #include "includes.h"
35 #include "winbindd.h"
37 #undef DBGC_CLASS
38 #define DBGC_CLASS DBGC_WINBIND
40 struct do_async_state {
41 TALLOC_CTX *mem_ctx;
42 struct winbindd_request request;
43 struct winbindd_response response;
44 void (*cont)(TALLOC_CTX *mem_ctx,
45 bool success,
46 struct winbindd_response *response,
47 void *c, void *private_data);
48 void *c, *private_data;
51 static void do_async_recv(void *private_data, bool success)
53 struct do_async_state *state =
54 talloc_get_type_abort(private_data, struct do_async_state);
56 state->cont(state->mem_ctx, success, &state->response,
57 state->c, state->private_data);
60 void do_async(TALLOC_CTX *mem_ctx, struct winbindd_child *child,
61 const struct winbindd_request *request,
62 void (*cont)(TALLOC_CTX *mem_ctx, bool success,
63 struct winbindd_response *response,
64 void *c, void *private_data),
65 void *c, void *private_data)
67 struct do_async_state *state;
69 state = TALLOC_ZERO_P(mem_ctx, struct do_async_state);
70 if (state == NULL) {
71 DEBUG(0, ("talloc failed\n"));
72 cont(mem_ctx, False, NULL, c, private_data);
73 return;
76 state->mem_ctx = mem_ctx;
77 state->request = *request;
78 state->request.length = sizeof(state->request);
79 state->cont = cont;
80 state->c = c;
81 state->private_data = private_data;
83 async_request(mem_ctx, child, &state->request,
84 &state->response, do_async_recv, state);
87 void do_async_domain(TALLOC_CTX *mem_ctx, struct winbindd_domain *domain,
88 const struct winbindd_request *request,
89 void (*cont)(TALLOC_CTX *mem_ctx, bool success,
90 struct winbindd_response *response,
91 void *c, void *private_data),
92 void *c, void *private_data)
94 struct do_async_state *state;
96 state = TALLOC_ZERO_P(mem_ctx, struct do_async_state);
97 if (state == NULL) {
98 DEBUG(0, ("talloc failed\n"));
99 cont(mem_ctx, False, NULL, c, private_data);
100 return;
103 state->mem_ctx = mem_ctx;
104 state->request = *request;
105 state->request.length = sizeof(state->request);
106 state->cont = cont;
107 state->c = c;
108 state->private_data = private_data;
110 async_domain_request(mem_ctx, domain, &state->request,
111 &state->response, do_async_recv, state);
114 struct lookupsid_state {
115 DOM_SID sid;
116 void *caller_private_data;
120 static void lookupsid_recv2(TALLOC_CTX *mem_ctx, bool success,
121 struct winbindd_response *response,
122 void *c, void *private_data)
124 void (*cont)(void *priv, bool succ, const char *dom_name,
125 const char *name, enum lsa_SidType type) =
126 (void (*)(void *, bool, const char *, const char *,
127 enum lsa_SidType))c;
128 struct lookupsid_state *s = talloc_get_type_abort(private_data,
129 struct lookupsid_state);
131 if (!success) {
132 DEBUG(5, ("Could not trigger lookupsid\n"));
133 cont(s->caller_private_data, False, NULL, NULL, SID_NAME_UNKNOWN);
134 return;
137 if (response->result != WINBINDD_OK) {
138 DEBUG(5, ("lookupsid (forest root) returned an error\n"));
139 cont(s->caller_private_data, False, NULL, NULL, SID_NAME_UNKNOWN);
140 return;
143 cont(s->caller_private_data, True, response->data.name.dom_name,
144 response->data.name.name,
145 (enum lsa_SidType)response->data.name.type);
148 static void lookupsid_recv(TALLOC_CTX *mem_ctx, bool success,
149 struct winbindd_response *response,
150 void *c, void *private_data)
152 void (*cont)(void *priv, bool succ, const char *dom_name,
153 const char *name, enum lsa_SidType type) =
154 (void (*)(void *, bool, const char *, const char *,
155 enum lsa_SidType))c;
156 struct lookupsid_state *s = talloc_get_type_abort(private_data,
157 struct lookupsid_state);
159 if (!success) {
160 DEBUG(5, ("Could not trigger lookupsid\n"));
161 cont(s->caller_private_data, False, NULL, NULL, SID_NAME_UNKNOWN);
162 return;
165 if (response->result != WINBINDD_OK) {
166 /* Try again using the forest root */
167 struct winbindd_domain *root_domain = find_root_domain();
168 struct winbindd_request request;
170 if ( !root_domain ) {
171 DEBUG(5,("lookupsid_recv: unable to determine forest root\n"));
172 cont(s->caller_private_data, False, NULL, NULL, SID_NAME_UNKNOWN);
173 return;
176 ZERO_STRUCT(request);
177 request.cmd = WINBINDD_LOOKUPSID;
178 sid_to_fstring(request.data.sid, &s->sid);
180 do_async_domain(mem_ctx, root_domain, &request, lookupsid_recv2,
181 (void *)cont, s);
183 return;
186 cont(s->caller_private_data, True, response->data.name.dom_name,
187 response->data.name.name,
188 (enum lsa_SidType)response->data.name.type);
191 void winbindd_lookupsid_async(TALLOC_CTX *mem_ctx, const DOM_SID *sid,
192 void (*cont)(void *private_data, bool success,
193 const char *dom_name,
194 const char *name,
195 enum lsa_SidType type),
196 void *private_data)
198 struct winbindd_domain *domain;
199 struct winbindd_request request;
200 struct lookupsid_state *s;
202 domain = find_lookup_domain_from_sid(sid);
203 if (domain == NULL) {
204 DEBUG(5, ("Could not find domain for sid %s\n",
205 sid_string_dbg(sid)));
206 cont(private_data, False, NULL, NULL, SID_NAME_UNKNOWN);
207 return;
210 ZERO_STRUCT(request);
211 request.cmd = WINBINDD_LOOKUPSID;
212 sid_to_fstring(request.data.sid, sid);
214 if ( (s = TALLOC_ZERO_P(mem_ctx, struct lookupsid_state)) == NULL ) {
215 DEBUG(0, ("winbindd_lookupsid_async: talloc failed\n"));
216 cont(private_data, False, NULL, NULL, SID_NAME_UNKNOWN);
217 return;
220 sid_copy( &s->sid, sid );
221 s->caller_private_data = private_data;
223 do_async_domain(mem_ctx, domain, &request, lookupsid_recv,
224 (void *)cont, s);
227 enum winbindd_result winbindd_dual_lookupsid(struct winbindd_domain *domain,
228 struct winbindd_cli_state *state)
230 enum lsa_SidType type;
231 DOM_SID sid;
232 char *name;
233 char *dom_name;
235 /* Ensure null termination */
236 state->request.data.sid[sizeof(state->request.data.sid)-1]='\0';
238 DEBUG(3, ("[%5lu]: lookupsid %s\n", (unsigned long)state->pid,
239 state->request.data.sid));
241 /* Lookup sid from PDC using lsa_lookup_sids() */
243 if (!string_to_sid(&sid, state->request.data.sid)) {
244 DEBUG(5, ("%s not a SID\n", state->request.data.sid));
245 return WINBINDD_ERROR;
248 /* Lookup the sid */
250 if (!winbindd_lookup_name_by_sid(state->mem_ctx, domain, &sid,
251 &dom_name, &name, &type))
253 TALLOC_FREE(dom_name);
254 TALLOC_FREE(name);
255 return WINBINDD_ERROR;
258 fstrcpy(state->response.data.name.dom_name, dom_name);
259 fstrcpy(state->response.data.name.name, name);
260 state->response.data.name.type = type;
262 TALLOC_FREE(dom_name);
263 TALLOC_FREE(name);
264 return WINBINDD_OK;
267 /********************************************************************
268 This is the second callback after contacting the forest root
269 ********************************************************************/
271 struct lookupname_state {
272 char *dom_name;
273 char *name;
274 void *caller_private_data;
278 static void lookupname_recv2(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 DOM_SID *sid,
283 enum lsa_SidType type) =
284 (void (*)(void *, bool, const DOM_SID *, enum lsa_SidType))c;
285 DOM_SID sid;
286 struct lookupname_state *s = talloc_get_type_abort( private_data,
287 struct lookupname_state );
289 if (!success) {
290 DEBUG(5, ("Could not trigger lookup_name\n"));
291 cont(s->caller_private_data, False, NULL, SID_NAME_UNKNOWN);
292 return;
295 if (response->result != WINBINDD_OK) {
296 DEBUG(5, ("lookup_name returned an error\n"));
297 cont(s->caller_private_data, False, NULL, SID_NAME_UNKNOWN);
298 return;
301 if (!string_to_sid(&sid, response->data.sid.sid)) {
302 DEBUG(0, ("Could not convert string %s to sid\n",
303 response->data.sid.sid));
304 cont(s->caller_private_data, False, NULL, SID_NAME_UNKNOWN);
305 return;
308 cont(s->caller_private_data, True, &sid,
309 (enum lsa_SidType)response->data.sid.type);
312 /********************************************************************
313 This is the first callback after contacting our own domain
314 ********************************************************************/
316 static void lookupname_recv(TALLOC_CTX *mem_ctx, bool success,
317 struct winbindd_response *response,
318 void *c, void *private_data)
320 void (*cont)(void *priv, bool succ, const DOM_SID *sid,
321 enum lsa_SidType type) =
322 (void (*)(void *, bool, const DOM_SID *, enum lsa_SidType))c;
323 DOM_SID sid;
324 struct lookupname_state *s = talloc_get_type_abort( private_data,
325 struct lookupname_state );
327 if (!success) {
328 DEBUG(5, ("lookupname_recv: lookup_name() failed!\n"));
329 cont(s->caller_private_data, False, NULL, SID_NAME_UNKNOWN);
330 return;
333 if (response->result != WINBINDD_OK) {
334 /* Try again using the forest root */
335 struct winbindd_domain *root_domain = find_root_domain();
336 struct winbindd_request request;
338 if ( !root_domain ) {
339 DEBUG(5,("lookupname_recv: unable to determine forest root\n"));
340 cont(s->caller_private_data, False, NULL, SID_NAME_UNKNOWN);
341 return;
344 ZERO_STRUCT(request);
345 request.cmd = WINBINDD_LOOKUPNAME;
347 fstrcpy( request.data.name.dom_name, s->dom_name );
348 fstrcpy( request.data.name.name, s->name );
350 do_async_domain(mem_ctx, root_domain, &request, lookupname_recv2,
351 (void *)cont, s);
353 return;
356 if (!string_to_sid(&sid, response->data.sid.sid)) {
357 DEBUG(0, ("Could not convert string %s to sid\n",
358 response->data.sid.sid));
359 cont(s->caller_private_data, False, NULL, SID_NAME_UNKNOWN);
360 return;
363 cont(s->caller_private_data, True, &sid,
364 (enum lsa_SidType)response->data.sid.type);
367 /********************************************************************
368 The lookup name call first contacts a DC in its own domain
369 and fallbacks to contact a DC in the forest in our domain doesn't
370 know the name.
371 ********************************************************************/
373 void winbindd_lookupname_async(TALLOC_CTX *mem_ctx,
374 const char *dom_name, const char *name,
375 void (*cont)(void *private_data, bool success,
376 const DOM_SID *sid,
377 enum lsa_SidType type),
378 enum winbindd_cmd orig_cmd,
379 void *private_data)
381 struct winbindd_request request;
382 struct winbindd_domain *domain;
383 struct lookupname_state *s;
385 if ( (domain = find_lookup_domain_from_name(dom_name)) == NULL ) {
386 DEBUG(5, ("Could not find domain for name '%s'\n", dom_name));
387 cont(private_data, False, NULL, SID_NAME_UNKNOWN);
388 return;
391 ZERO_STRUCT(request);
392 request.cmd = WINBINDD_LOOKUPNAME;
393 request.original_cmd = orig_cmd;
394 fstrcpy(request.data.name.dom_name, dom_name);
395 fstrcpy(request.data.name.name, name);
397 if ( (s = TALLOC_ZERO_P(mem_ctx, struct lookupname_state)) == NULL ) {
398 DEBUG(0, ("winbindd_lookupname_async: talloc failed\n"));
399 cont(private_data, False, NULL, SID_NAME_UNKNOWN);
400 return;
403 s->dom_name = talloc_strdup( s, dom_name );
404 s->name = talloc_strdup( s, name );
405 if (!s->dom_name || !s->name) {
406 cont(private_data, False, NULL, SID_NAME_UNKNOWN);
407 return;
410 s->caller_private_data = private_data;
412 do_async_domain(mem_ctx, domain, &request, lookupname_recv,
413 (void *)cont, s);
416 enum winbindd_result winbindd_dual_lookupname(struct winbindd_domain *domain,
417 struct winbindd_cli_state *state)
419 enum lsa_SidType type;
420 char *name_domain, *name_user;
421 DOM_SID sid;
422 char *p;
424 /* Ensure null termination */
425 state->request.data.name.dom_name[sizeof(state->request.data.name.dom_name)-1]='\0';
427 /* Ensure null termination */
428 state->request.data.name.name[sizeof(state->request.data.name.name)-1]='\0';
430 /* cope with the name being a fully qualified name */
431 p = strstr(state->request.data.name.name, lp_winbind_separator());
432 if (p) {
433 *p = 0;
434 name_domain = state->request.data.name.name;
435 name_user = p+1;
436 } else {
437 name_domain = state->request.data.name.dom_name;
438 name_user = state->request.data.name.name;
441 DEBUG(3, ("[%5lu]: lookupname %s%s%s\n", (unsigned long)state->pid,
442 name_domain, lp_winbind_separator(), name_user));
444 /* Lookup name from DC using lsa_lookup_names() */
445 if (!winbindd_lookup_sid_by_name(state->mem_ctx, state->request.original_cmd, domain, name_domain,
446 name_user, &sid, &type)) {
447 return WINBINDD_ERROR;
450 sid_to_fstring(state->response.data.sid.sid, &sid);
451 state->response.data.sid.type = type;
453 return WINBINDD_OK;
456 bool print_sidlist(TALLOC_CTX *mem_ctx, const DOM_SID *sids,
457 size_t num_sids, char **result, ssize_t *len)
459 size_t i;
460 size_t buflen = 0;
462 *len = 0;
463 *result = NULL;
464 for (i=0; i<num_sids; i++) {
465 fstring tmp;
466 sprintf_append(mem_ctx, result, len, &buflen,
467 "%s\n", sid_to_fstring(tmp, &sids[i]));
470 if ((num_sids != 0) && (*result == NULL)) {
471 return False;
474 return True;
477 static bool parse_sidlist(TALLOC_CTX *mem_ctx, char *sidstr,
478 DOM_SID **sids, size_t *num_sids)
480 char *p, *q;
482 p = sidstr;
483 if (p == NULL)
484 return False;
486 while (p[0] != '\0') {
487 DOM_SID sid;
488 q = strchr(p, '\n');
489 if (q == NULL) {
490 DEBUG(0, ("Got invalid sidstr: %s\n", p));
491 return False;
493 *q = '\0';
494 q += 1;
495 if (!string_to_sid(&sid, p)) {
496 DEBUG(0, ("Could not parse sid %s\n", p));
497 return False;
499 if (!NT_STATUS_IS_OK(add_sid_to_array(mem_ctx, &sid, sids,
500 num_sids)))
502 return False;
504 p = q;
506 return True;
509 static bool parse_ridlist(TALLOC_CTX *mem_ctx, char *ridstr,
510 uint32 **rids, size_t *num_rids)
512 char *p;
514 p = ridstr;
515 if (p == NULL)
516 return False;
518 while (p[0] != '\0') {
519 uint32 rid;
520 char *q;
521 rid = strtoul(p, &q, 10);
522 if (*q != '\n') {
523 DEBUG(0, ("Got invalid ridstr: %s\n", p));
524 return False;
526 p = q+1;
527 ADD_TO_ARRAY(mem_ctx, uint32, rid, rids, num_rids);
529 return True;
532 enum winbindd_result winbindd_dual_lookuprids(struct winbindd_domain *domain,
533 struct winbindd_cli_state *state)
535 uint32 *rids = NULL;
536 size_t i, buflen, num_rids = 0;
537 ssize_t len;
538 DOM_SID domain_sid;
539 char *domain_name;
540 char **names;
541 enum lsa_SidType *types;
542 NTSTATUS status;
543 char *result;
545 DEBUG(10, ("Looking up RIDs for domain %s (%s)\n",
546 state->request.domain_name,
547 state->request.data.sid));
549 if (!parse_ridlist(state->mem_ctx, state->request.extra_data.data,
550 &rids, &num_rids)) {
551 DEBUG(5, ("Could not parse ridlist\n"));
552 return WINBINDD_ERROR;
555 if (!string_to_sid(&domain_sid, state->request.data.sid)) {
556 DEBUG(5, ("Could not parse domain sid %s\n",
557 state->request.data.sid));
558 return WINBINDD_ERROR;
561 status = domain->methods->rids_to_names(domain, state->mem_ctx,
562 &domain_sid, rids, num_rids,
563 &domain_name,
564 &names, &types);
566 if (!NT_STATUS_IS_OK(status) &&
567 !NT_STATUS_EQUAL(status, STATUS_SOME_UNMAPPED)) {
568 return WINBINDD_ERROR;
571 len = 0;
572 buflen = 0;
573 result = NULL;
575 for (i=0; i<num_rids; i++) {
576 sprintf_append(state->mem_ctx, &result, &len, &buflen,
577 "%d %s\n", types[i], names[i]);
580 fstrcpy(state->response.data.domain_name, domain_name);
582 if (result != NULL) {
583 state->response.extra_data.data = SMB_STRDUP(result);
584 if (!state->response.extra_data.data) {
585 return WINBINDD_ERROR;
587 state->response.length += len+1;
590 return WINBINDD_OK;
593 static void getsidaliases_recv(TALLOC_CTX *mem_ctx, bool success,
594 struct winbindd_response *response,
595 void *c, void *private_data)
597 void (*cont)(void *priv, bool succ,
598 DOM_SID *aliases, size_t num_aliases) =
599 (void (*)(void *, bool, DOM_SID *, size_t))c;
600 char *aliases_str;
601 DOM_SID *sids = NULL;
602 size_t num_sids = 0;
604 if (!success) {
605 DEBUG(5, ("Could not trigger getsidaliases\n"));
606 cont(private_data, success, NULL, 0);
607 return;
610 if (response->result != WINBINDD_OK) {
611 DEBUG(5, ("getsidaliases returned an error\n"));
612 cont(private_data, False, NULL, 0);
613 return;
616 aliases_str = (char *)response->extra_data.data;
618 if (aliases_str == NULL) {
619 DEBUG(10, ("getsidaliases return 0 SIDs\n"));
620 cont(private_data, True, NULL, 0);
621 return;
624 if (!parse_sidlist(mem_ctx, aliases_str, &sids, &num_sids)) {
625 DEBUG(0, ("Could not parse sids\n"));
626 cont(private_data, False, NULL, 0);
627 return;
630 SAFE_FREE(response->extra_data.data);
632 cont(private_data, True, sids, num_sids);
635 void winbindd_getsidaliases_async(struct winbindd_domain *domain,
636 TALLOC_CTX *mem_ctx,
637 const DOM_SID *sids, size_t num_sids,
638 void (*cont)(void *private_data,
639 bool success,
640 const DOM_SID *aliases,
641 size_t num_aliases),
642 void *private_data)
644 struct winbindd_request request;
645 char *sidstr = NULL;
646 ssize_t len;
648 if (num_sids == 0) {
649 cont(private_data, True, NULL, 0);
650 return;
653 if (!print_sidlist(mem_ctx, sids, num_sids, &sidstr, &len)) {
654 cont(private_data, False, NULL, 0);
655 return;
658 ZERO_STRUCT(request);
659 request.cmd = WINBINDD_DUAL_GETSIDALIASES;
660 request.extra_len = len;
661 request.extra_data.data = sidstr;
663 do_async_domain(mem_ctx, domain, &request, getsidaliases_recv,
664 (void *)cont, private_data);
667 enum winbindd_result winbindd_dual_getsidaliases(struct winbindd_domain *domain,
668 struct winbindd_cli_state *state)
670 DOM_SID *sids = NULL;
671 size_t num_sids = 0;
672 char *sidstr = NULL;
673 ssize_t len;
674 size_t i;
675 uint32 num_aliases;
676 uint32 *alias_rids;
677 NTSTATUS result;
679 DEBUG(3, ("[%5lu]: getsidaliases\n", (unsigned long)state->pid));
681 sidstr = state->request.extra_data.data;
682 if (sidstr == NULL) {
683 sidstr = talloc_strdup(state->mem_ctx, "\n"); /* No SID */
684 if (!sidstr) {
685 DEBUG(0, ("Out of memory\n"));
686 return WINBINDD_ERROR;
690 DEBUG(10, ("Sidlist: %s\n", sidstr));
692 if (!parse_sidlist(state->mem_ctx, sidstr, &sids, &num_sids)) {
693 DEBUG(0, ("Could not parse SID list: %s\n", sidstr));
694 return WINBINDD_ERROR;
697 num_aliases = 0;
698 alias_rids = NULL;
700 result = domain->methods->lookup_useraliases(domain,
701 state->mem_ctx,
702 num_sids, sids,
703 &num_aliases,
704 &alias_rids);
706 if (!NT_STATUS_IS_OK(result)) {
707 DEBUG(3, ("Could not lookup_useraliases: %s\n",
708 nt_errstr(result)));
709 return WINBINDD_ERROR;
712 num_sids = 0;
713 sids = NULL;
714 sidstr = NULL;
716 DEBUG(10, ("Got %d aliases\n", num_aliases));
718 for (i=0; i<num_aliases; i++) {
719 DOM_SID sid;
720 DEBUGADD(10, (" rid %d\n", alias_rids[i]));
721 sid_copy(&sid, &domain->sid);
722 sid_append_rid(&sid, alias_rids[i]);
723 result = add_sid_to_array(state->mem_ctx, &sid, &sids,
724 &num_sids);
725 if (!NT_STATUS_IS_OK(result)) {
726 return WINBINDD_ERROR;
731 if (!print_sidlist(state->mem_ctx, sids, num_sids, &sidstr, &len)) {
732 DEBUG(0, ("Could not print_sidlist\n"));
733 state->response.extra_data.data = NULL;
734 return WINBINDD_ERROR;
737 state->response.extra_data.data = NULL;
739 if (sidstr) {
740 state->response.extra_data.data = SMB_STRDUP(sidstr);
741 if (!state->response.extra_data.data) {
742 DEBUG(0, ("Out of memory\n"));
743 return WINBINDD_ERROR;
745 DEBUG(10, ("aliases_list: %s\n",
746 (char *)state->response.extra_data.data));
747 state->response.length += len+1;
750 return WINBINDD_OK;
753 struct gettoken_state {
754 TALLOC_CTX *mem_ctx;
755 DOM_SID user_sid;
756 struct winbindd_domain *alias_domain;
757 struct winbindd_domain *local_alias_domain;
758 struct winbindd_domain *builtin_domain;
759 DOM_SID *sids;
760 size_t num_sids;
761 void (*cont)(void *private_data, bool success, DOM_SID *sids, size_t num_sids);
762 void *private_data;
765 static void gettoken_recvdomgroups(TALLOC_CTX *mem_ctx, bool success,
766 struct winbindd_response *response,
767 void *c, void *private_data);
768 static void gettoken_recvaliases(void *private_data, bool success,
769 const DOM_SID *aliases,
770 size_t num_aliases);
773 void winbindd_gettoken_async(TALLOC_CTX *mem_ctx, const DOM_SID *user_sid,
774 void (*cont)(void *private_data, bool success,
775 DOM_SID *sids, size_t num_sids),
776 void *private_data)
778 struct winbindd_domain *domain;
779 struct winbindd_request request;
780 struct gettoken_state *state;
782 state = TALLOC_ZERO_P(mem_ctx, struct gettoken_state);
783 if (state == NULL) {
784 DEBUG(0, ("talloc failed\n"));
785 cont(private_data, False, NULL, 0);
786 return;
789 state->mem_ctx = mem_ctx;
790 sid_copy(&state->user_sid, user_sid);
791 state->alias_domain = find_our_domain();
792 state->local_alias_domain = find_domain_from_name( get_global_sam_name() );
793 state->builtin_domain = find_builtin_domain();
794 state->cont = cont;
795 state->private_data = private_data;
797 domain = find_domain_from_sid_noinit(user_sid);
798 if (domain == NULL) {
799 DEBUG(5, ("Could not find domain from SID %s\n",
800 sid_string_dbg(user_sid)));
801 cont(private_data, False, NULL, 0);
802 return;
805 ZERO_STRUCT(request);
806 request.cmd = WINBINDD_GETUSERDOMGROUPS;
807 sid_to_fstring(request.data.sid, user_sid);
809 do_async_domain(mem_ctx, domain, &request, gettoken_recvdomgroups,
810 NULL, state);
813 static void gettoken_recvdomgroups(TALLOC_CTX *mem_ctx, bool success,
814 struct winbindd_response *response,
815 void *c, void *private_data)
817 struct gettoken_state *state =
818 talloc_get_type_abort(private_data, struct gettoken_state);
819 char *sids_str;
821 if (!success) {
822 DEBUG(10, ("Could not get domain groups\n"));
823 state->cont(state->private_data, False, NULL, 0);
824 return;
827 sids_str = (char *)response->extra_data.data;
829 if (sids_str == NULL) {
830 /* This could be normal if we are dealing with a
831 local user and local groups */
833 if ( !sid_check_is_in_our_domain( &state->user_sid ) ) {
834 DEBUG(10, ("Received no domain groups\n"));
835 state->cont(state->private_data, True, NULL, 0);
836 return;
840 state->sids = NULL;
841 state->num_sids = 0;
843 if (!NT_STATUS_IS_OK(add_sid_to_array(mem_ctx, &state->user_sid,
844 &state->sids, &state->num_sids)))
846 DEBUG(0, ("Out of memory\n"));
847 state->cont(state->private_data, False, NULL, 0);
848 return;
851 if (sids_str && !parse_sidlist(mem_ctx, sids_str, &state->sids,
852 &state->num_sids)) {
853 DEBUG(0, ("Could not parse sids\n"));
854 state->cont(state->private_data, False, NULL, 0);
855 return;
858 SAFE_FREE(response->extra_data.data);
860 if (state->alias_domain == NULL) {
861 DEBUG(10, ("Don't expand domain local groups\n"));
862 state->cont(state->private_data, True, state->sids,
863 state->num_sids);
864 return;
867 winbindd_getsidaliases_async(state->alias_domain, mem_ctx,
868 state->sids, state->num_sids,
869 gettoken_recvaliases, state);
872 static void gettoken_recvaliases(void *private_data, bool success,
873 const DOM_SID *aliases,
874 size_t num_aliases)
876 struct gettoken_state *state = (struct gettoken_state *)private_data;
877 size_t i;
879 if (!success) {
880 DEBUG(10, ("Could not receive domain local groups\n"));
881 state->cont(state->private_data, False, NULL, 0);
882 return;
885 for (i=0; i<num_aliases; i++) {
886 if (!NT_STATUS_IS_OK(add_sid_to_array(state->mem_ctx,
887 &aliases[i],
888 &state->sids,
889 &state->num_sids)))
891 DEBUG(0, ("Out of memory\n"));
892 state->cont(state->private_data, False, NULL, 0);
893 return;
897 if (state->local_alias_domain != NULL) {
898 struct winbindd_domain *local_domain = state->local_alias_domain;
899 DEBUG(10, ("Expanding our own local groups\n"));
900 state->local_alias_domain = NULL;
901 winbindd_getsidaliases_async(local_domain, state->mem_ctx,
902 state->sids, state->num_sids,
903 gettoken_recvaliases, state);
904 return;
907 if (state->builtin_domain != NULL) {
908 struct winbindd_domain *builtin_domain = state->builtin_domain;
909 DEBUG(10, ("Expanding our own BUILTIN groups\n"));
910 state->builtin_domain = NULL;
911 winbindd_getsidaliases_async(builtin_domain, state->mem_ctx,
912 state->sids, state->num_sids,
913 gettoken_recvaliases, state);
914 return;
917 state->cont(state->private_data, True, state->sids, state->num_sids);
920 static void query_user_recv(TALLOC_CTX *mem_ctx, bool success,
921 struct winbindd_response *response,
922 void *c, void *private_data)
924 void (*cont)(void *priv, bool succ, const char *acct_name,
925 const char *full_name, const char *homedir,
926 const char *shell, uint32 gid, uint32 group_rid) =
927 (void (*)(void *, bool, const char *, const char *,
928 const char *, const char *, uint32, uint32))c;
930 if (!success) {
931 DEBUG(5, ("Could not trigger query_user\n"));
932 cont(private_data, False, NULL, NULL, NULL, NULL, -1, -1);
933 return;
936 if (response->result != WINBINDD_OK) {
937 DEBUG(5, ("query_user returned an error\n"));
938 cont(private_data, False, NULL, NULL, NULL, NULL, -1, -1);
939 return;
942 cont(private_data, True, response->data.user_info.acct_name,
943 response->data.user_info.full_name,
944 response->data.user_info.homedir,
945 response->data.user_info.shell,
946 response->data.user_info.primary_gid,
947 response->data.user_info.group_rid);
950 void query_user_async(TALLOC_CTX *mem_ctx, struct winbindd_domain *domain,
951 const DOM_SID *sid,
952 void (*cont)(void *private_data, bool success,
953 const char *acct_name,
954 const char *full_name,
955 const char *homedir,
956 const char *shell,
957 gid_t gid,
958 uint32 group_rid),
959 void *private_data)
961 struct winbindd_request request;
962 ZERO_STRUCT(request);
963 request.cmd = WINBINDD_DUAL_USERINFO;
964 sid_to_fstring(request.data.sid, sid);
965 do_async_domain(mem_ctx, domain, &request, query_user_recv,
966 (void *)cont, private_data);