Make winbindd_cli_state->response a pointer instead of a struct member
[Samba.git] / source3 / winbindd / winbindd_async.c
blob56175e2659866e23a7f7ce4c4e67fb0708fd41e6
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 static 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 if 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 /* This is the first callback after enumerating users/groups from a domain */
457 static void listent_recv(TALLOC_CTX *mem_ctx, bool success,
458 struct winbindd_response *response,
459 void *c, void *private_data)
461 void (*cont)(void *priv, bool succ, fstring dom_name, char *data) =
462 (void (*)(void *, bool, fstring, char*))c;
464 if (!success || response->result != WINBINDD_OK) {
465 DEBUG(5, ("list_ent() failed!\n"));
466 cont(private_data, False, response->data.name.dom_name, NULL);
467 return;
470 cont(private_data, True, response->data.name.dom_name,
471 (char *)response->extra_data.data);
474 /* Request the name of all users/groups in a single domain */
475 void winbindd_listent_async(TALLOC_CTX *mem_ctx,
476 struct winbindd_domain *domain,
477 void (*cont)(void *private_data, bool success,
478 fstring dom_name, char* extra_data),
479 void *private_data, enum ent_type type)
481 struct winbindd_request request;
483 ZERO_STRUCT(request);
484 if (type == LIST_USERS)
485 request.cmd = WINBINDD_LIST_USERS;
486 else if (type == LIST_GROUPS)
487 request.cmd = WINBINDD_LIST_GROUPS;
489 do_async_domain(mem_ctx, domain, &request, listent_recv,
490 (void *)cont, private_data);
493 enum winbindd_result winbindd_dual_list_users(struct winbindd_domain *domain,
494 struct winbindd_cli_state *state)
496 WINBIND_USERINFO *info;
497 NTSTATUS status;
498 struct winbindd_methods *methods;
499 uint32 num_entries = 0;
500 char *extra_data;
501 uint32_t extra_data_len = 0, i;
503 /* Must copy domain into response first for debugging in parent */
504 fstrcpy(state->response->data.name.dom_name, domain->name);
506 /* Query user info */
507 methods = domain->methods;
508 status = methods->query_user_list(domain, state->mem_ctx,
509 &num_entries, &info);
511 if (!NT_STATUS_IS_OK(status))
512 return WINBINDD_ERROR;
514 if (num_entries == 0)
515 return WINBINDD_OK;
517 /* Allocate some memory for extra data. Note that we limit
518 account names to sizeof(fstring) = 256 characters.
519 +1 for the ',' between group names */
520 extra_data = talloc_array(state->mem_ctx, char,
521 (sizeof(fstring) + 1) * num_entries);
523 if (!extra_data) {
524 DEBUG(0,("failed to enlarge buffer!\n"));
525 return WINBINDD_ERROR;
528 /* Pack user list into extra data fields */
529 for (i = 0; i < num_entries; i++) {
530 fstring acct_name, name;
532 if (info[i].acct_name == NULL)
533 fstrcpy(acct_name, "");
534 else
535 fstrcpy(acct_name, info[i].acct_name);
537 fill_domain_username(name, domain->name, acct_name, True);
538 /* Append to extra data */
539 memcpy(&extra_data[extra_data_len], name, strlen(name));
540 extra_data_len += strlen(name);
541 extra_data[extra_data_len++] = ',';
544 /* Assign extra_data fields in response structure */
545 if (extra_data) {
546 /* remove trailing ',' */
547 extra_data[extra_data_len - 1] = '\0';
548 state->response->extra_data.data = extra_data;
549 state->response->length += extra_data_len;
552 return WINBINDD_OK;
555 enum winbindd_result winbindd_dual_list_groups(struct winbindd_domain *domain,
556 struct winbindd_cli_state *state)
558 struct getent_state groups;
559 char *extra_data;
560 uint32_t extra_data_len = 0, i;
562 ZERO_STRUCT(groups);
564 /* Must copy domain into response first for debugging in parent */
565 fstrcpy(state->response->data.name.dom_name, domain->name);
566 fstrcpy(groups.domain_name, domain->name);
568 /* Get list of sam groups */
569 if (!get_sam_group_entries(&groups)) {
570 /* this domain is empty or in an error state */
571 return WINBINDD_ERROR;
574 /* Allocate some memory for extra data. Note that we limit
575 account names to sizeof(fstring) = 256 characters.
576 +1 for the ',' between group names */
577 extra_data = talloc_array(
578 state->mem_ctx, char,
579 (sizeof(fstring) + 1) * groups.num_sam_entries);
581 if (!extra_data) {
582 DEBUG(0,("failed to enlarge buffer!\n"));
583 SAFE_FREE(groups.sam_entries);
584 return WINBINDD_ERROR;
587 /* Pack group list into extra data fields */
588 for (i = 0; i < groups.num_sam_entries; i++) {
589 char *group_name = ((struct acct_info *)
590 groups.sam_entries)[i].acct_name;
591 fstring name;
593 fill_domain_username(name, domain->name, group_name, True);
594 /* Append to extra data */
595 memcpy(&extra_data[extra_data_len], name, strlen(name));
596 extra_data_len += strlen(name);
597 extra_data[extra_data_len++] = ',';
600 SAFE_FREE(groups.sam_entries);
602 /* Assign extra_data fields in response structure */
603 if (extra_data) {
604 /* remove trailing ',' */
605 extra_data[extra_data_len - 1] = '\0';
606 state->response->extra_data.data = extra_data;
607 state->response->length += extra_data_len;
610 return WINBINDD_OK;
613 bool print_sidlist(TALLOC_CTX *mem_ctx, const DOM_SID *sids,
614 size_t num_sids, char **result, ssize_t *len)
616 size_t i;
617 size_t buflen = 0;
619 *len = 0;
620 *result = NULL;
621 for (i=0; i<num_sids; i++) {
622 fstring tmp;
623 sprintf_append(mem_ctx, result, len, &buflen,
624 "%s\n", sid_to_fstring(tmp, &sids[i]));
627 if ((num_sids != 0) && (*result == NULL)) {
628 return False;
631 return True;
634 bool parse_sidlist(TALLOC_CTX *mem_ctx, char *sidstr,
635 DOM_SID **sids, size_t *num_sids)
637 char *p, *q;
639 p = sidstr;
640 if (p == NULL)
641 return False;
643 while (p[0] != '\0') {
644 DOM_SID sid;
645 q = strchr(p, '\n');
646 if (q == NULL) {
647 DEBUG(0, ("Got invalid sidstr: %s\n", p));
648 return False;
650 *q = '\0';
651 q += 1;
652 if (!string_to_sid(&sid, p)) {
653 DEBUG(0, ("Could not parse sid %s\n", p));
654 return False;
656 if (!NT_STATUS_IS_OK(add_sid_to_array(mem_ctx, &sid, sids,
657 num_sids)))
659 return False;
661 p = q;
663 return True;
666 static bool parse_ridlist(TALLOC_CTX *mem_ctx, char *ridstr,
667 uint32 **rids, size_t *num_rids)
669 char *p;
671 p = ridstr;
672 if (p == NULL)
673 return False;
675 while (p[0] != '\0') {
676 uint32 rid;
677 char *q;
678 rid = strtoul(p, &q, 10);
679 if (*q != '\n') {
680 DEBUG(0, ("Got invalid ridstr: %s\n", p));
681 return False;
683 p = q+1;
684 ADD_TO_ARRAY(mem_ctx, uint32, rid, rids, num_rids);
686 return True;
689 enum winbindd_result winbindd_dual_lookuprids(struct winbindd_domain *domain,
690 struct winbindd_cli_state *state)
692 uint32 *rids = NULL;
693 size_t i, buflen, num_rids = 0;
694 ssize_t len;
695 DOM_SID domain_sid;
696 char *domain_name;
697 char **names;
698 enum lsa_SidType *types;
699 NTSTATUS status;
700 char *result;
702 DEBUG(10, ("Looking up RIDs for domain %s (%s)\n",
703 state->request->domain_name,
704 state->request->data.sid));
706 if (!parse_ridlist(state->mem_ctx, state->request->extra_data.data,
707 &rids, &num_rids)) {
708 DEBUG(5, ("Could not parse ridlist\n"));
709 return WINBINDD_ERROR;
712 if (!string_to_sid(&domain_sid, state->request->data.sid)) {
713 DEBUG(5, ("Could not parse domain sid %s\n",
714 state->request->data.sid));
715 return WINBINDD_ERROR;
718 status = domain->methods->rids_to_names(domain, state->mem_ctx,
719 &domain_sid, rids, num_rids,
720 &domain_name,
721 &names, &types);
723 if (!NT_STATUS_IS_OK(status) &&
724 !NT_STATUS_EQUAL(status, STATUS_SOME_UNMAPPED)) {
725 return WINBINDD_ERROR;
728 len = 0;
729 buflen = 0;
730 result = NULL;
732 for (i=0; i<num_rids; i++) {
733 sprintf_append(state->mem_ctx, &result, &len, &buflen,
734 "%d %s\n", types[i], names[i]);
737 fstrcpy(state->response->data.domain_name, domain_name);
739 if (result != NULL) {
740 state->response->extra_data.data = result;
741 state->response->length += len+1;
744 return WINBINDD_OK;
747 static void getsidaliases_recv(TALLOC_CTX *mem_ctx, bool success,
748 struct winbindd_response *response,
749 void *c, void *private_data)
751 void (*cont)(void *priv, bool succ,
752 DOM_SID *aliases, size_t num_aliases) =
753 (void (*)(void *, bool, DOM_SID *, size_t))c;
754 char *aliases_str;
755 DOM_SID *sids = NULL;
756 size_t num_sids = 0;
758 if (!success) {
759 DEBUG(5, ("Could not trigger getsidaliases\n"));
760 cont(private_data, success, NULL, 0);
761 return;
764 if (response->result != WINBINDD_OK) {
765 DEBUG(5, ("getsidaliases returned an error\n"));
766 cont(private_data, False, NULL, 0);
767 return;
770 aliases_str = (char *)response->extra_data.data;
772 if (aliases_str == NULL) {
773 DEBUG(10, ("getsidaliases return 0 SIDs\n"));
774 cont(private_data, True, NULL, 0);
775 return;
778 if (!parse_sidlist(mem_ctx, aliases_str, &sids, &num_sids)) {
779 DEBUG(0, ("Could not parse sids\n"));
780 cont(private_data, False, NULL, 0);
781 return;
784 cont(private_data, True, sids, num_sids);
787 void winbindd_getsidaliases_async(struct winbindd_domain *domain,
788 TALLOC_CTX *mem_ctx,
789 const DOM_SID *sids, size_t num_sids,
790 void (*cont)(void *private_data,
791 bool success,
792 const DOM_SID *aliases,
793 size_t num_aliases),
794 void *private_data)
796 struct winbindd_request request;
797 char *sidstr = NULL;
798 ssize_t len;
800 if (num_sids == 0) {
801 cont(private_data, True, NULL, 0);
802 return;
805 if (!print_sidlist(mem_ctx, sids, num_sids, &sidstr, &len)) {
806 cont(private_data, False, NULL, 0);
807 return;
810 ZERO_STRUCT(request);
811 request.cmd = WINBINDD_DUAL_GETSIDALIASES;
812 request.extra_len = len;
813 request.extra_data.data = sidstr;
815 do_async_domain(mem_ctx, domain, &request, getsidaliases_recv,
816 (void *)cont, private_data);
819 struct gettoken_state {
820 TALLOC_CTX *mem_ctx;
821 DOM_SID user_sid;
822 struct winbindd_domain *alias_domain;
823 struct winbindd_domain *local_alias_domain;
824 struct winbindd_domain *builtin_domain;
825 DOM_SID *sids;
826 size_t num_sids;
827 void (*cont)(void *private_data, bool success, DOM_SID *sids, size_t num_sids);
828 void *private_data;
831 static void gettoken_recvdomgroups(TALLOC_CTX *mem_ctx, bool success,
832 struct winbindd_response *response,
833 void *c, void *private_data);
834 static void gettoken_recvaliases(void *private_data, bool success,
835 const DOM_SID *aliases,
836 size_t num_aliases);
839 void winbindd_gettoken_async(TALLOC_CTX *mem_ctx, const DOM_SID *user_sid,
840 void (*cont)(void *private_data, bool success,
841 DOM_SID *sids, size_t num_sids),
842 void *private_data)
844 struct winbindd_domain *domain;
845 struct winbindd_request request;
846 struct gettoken_state *state;
848 state = TALLOC_ZERO_P(mem_ctx, struct gettoken_state);
849 if (state == NULL) {
850 DEBUG(0, ("talloc failed\n"));
851 cont(private_data, False, NULL, 0);
852 return;
855 state->mem_ctx = mem_ctx;
856 sid_copy(&state->user_sid, user_sid);
857 state->alias_domain = find_our_domain();
858 state->local_alias_domain = find_domain_from_name( get_global_sam_name() );
859 state->builtin_domain = find_builtin_domain();
860 state->cont = cont;
861 state->private_data = private_data;
863 domain = find_domain_from_sid_noinit(user_sid);
864 if (domain == NULL) {
865 DEBUG(5, ("Could not find domain from SID %s\n",
866 sid_string_dbg(user_sid)));
867 cont(private_data, False, NULL, 0);
868 return;
871 ZERO_STRUCT(request);
872 request.cmd = WINBINDD_GETUSERDOMGROUPS;
873 sid_to_fstring(request.data.sid, user_sid);
875 do_async_domain(mem_ctx, domain, &request, gettoken_recvdomgroups,
876 NULL, state);
879 static void gettoken_recvdomgroups(TALLOC_CTX *mem_ctx, bool success,
880 struct winbindd_response *response,
881 void *c, void *private_data)
883 struct gettoken_state *state =
884 talloc_get_type_abort(private_data, struct gettoken_state);
885 char *sids_str;
887 if (!success) {
888 DEBUG(10, ("Could not get domain groups\n"));
889 state->cont(state->private_data, False, NULL, 0);
890 return;
893 sids_str = (char *)response->extra_data.data;
895 if (sids_str == NULL) {
896 /* This could be normal if we are dealing with a
897 local user and local groups */
899 if ( !sid_check_is_in_our_domain( &state->user_sid ) ) {
900 DEBUG(10, ("Received no domain groups\n"));
901 state->cont(state->private_data, True, NULL, 0);
902 return;
906 state->sids = NULL;
907 state->num_sids = 0;
909 if (!NT_STATUS_IS_OK(add_sid_to_array(mem_ctx, &state->user_sid,
910 &state->sids, &state->num_sids)))
912 DEBUG(0, ("Out of memory\n"));
913 state->cont(state->private_data, False, NULL, 0);
914 return;
917 if (sids_str && !parse_sidlist(mem_ctx, sids_str, &state->sids,
918 &state->num_sids)) {
919 DEBUG(0, ("Could not parse sids\n"));
920 state->cont(state->private_data, False, NULL, 0);
921 return;
924 if (state->alias_domain == NULL) {
925 DEBUG(10, ("Don't expand domain local groups\n"));
926 state->cont(state->private_data, True, state->sids,
927 state->num_sids);
928 return;
931 winbindd_getsidaliases_async(state->alias_domain, mem_ctx,
932 state->sids, state->num_sids,
933 gettoken_recvaliases, state);
936 static void gettoken_recvaliases(void *private_data, bool success,
937 const DOM_SID *aliases,
938 size_t num_aliases)
940 struct gettoken_state *state = (struct gettoken_state *)private_data;
941 size_t i;
943 if (!success) {
944 DEBUG(10, ("Could not receive domain local groups\n"));
945 state->cont(state->private_data, False, NULL, 0);
946 return;
949 for (i=0; i<num_aliases; i++) {
950 if (!NT_STATUS_IS_OK(add_sid_to_array(state->mem_ctx,
951 &aliases[i],
952 &state->sids,
953 &state->num_sids)))
955 DEBUG(0, ("Out of memory\n"));
956 state->cont(state->private_data, False, NULL, 0);
957 return;
961 if (state->local_alias_domain != NULL) {
962 struct winbindd_domain *local_domain = state->local_alias_domain;
963 DEBUG(10, ("Expanding our own local groups\n"));
964 state->local_alias_domain = NULL;
965 winbindd_getsidaliases_async(local_domain, state->mem_ctx,
966 state->sids, state->num_sids,
967 gettoken_recvaliases, state);
968 return;
971 if (state->builtin_domain != NULL) {
972 struct winbindd_domain *builtin_domain = state->builtin_domain;
973 DEBUG(10, ("Expanding our own BUILTIN groups\n"));
974 state->builtin_domain = NULL;
975 winbindd_getsidaliases_async(builtin_domain, state->mem_ctx,
976 state->sids, state->num_sids,
977 gettoken_recvaliases, state);
978 return;
981 state->cont(state->private_data, True, state->sids, state->num_sids);
984 static void query_user_recv(TALLOC_CTX *mem_ctx, bool success,
985 struct winbindd_response *response,
986 void *c, void *private_data)
988 void (*cont)(void *priv, bool succ, const char *acct_name,
989 const char *full_name, const char *homedir,
990 const char *shell, uint32 gid, uint32 group_rid) =
991 (void (*)(void *, bool, const char *, const char *,
992 const char *, const char *, uint32, uint32))c;
994 if (!success) {
995 DEBUG(5, ("Could not trigger query_user\n"));
996 cont(private_data, False, NULL, NULL, NULL, NULL, -1, -1);
997 return;
1000 if (response->result != WINBINDD_OK) {
1001 DEBUG(5, ("query_user returned an error\n"));
1002 cont(private_data, False, NULL, NULL, NULL, NULL, -1, -1);
1003 return;
1006 cont(private_data, True, response->data.user_info.acct_name,
1007 response->data.user_info.full_name,
1008 response->data.user_info.homedir,
1009 response->data.user_info.shell,
1010 response->data.user_info.primary_gid,
1011 response->data.user_info.group_rid);
1014 void query_user_async(TALLOC_CTX *mem_ctx, struct winbindd_domain *domain,
1015 const DOM_SID *sid,
1016 void (*cont)(void *private_data, bool success,
1017 const char *acct_name,
1018 const char *full_name,
1019 const char *homedir,
1020 const char *shell,
1021 gid_t gid,
1022 uint32 group_rid),
1023 void *private_data)
1025 struct winbindd_request request;
1026 ZERO_STRUCT(request);
1027 request.cmd = WINBINDD_DUAL_USERINFO;
1028 sid_to_fstring(request.data.sid, sid);
1029 do_async_domain(mem_ctx, domain, &request, query_user_recv,
1030 (void *)cont, private_data);
1033 enum winbindd_result winbindd_dual_ping(struct winbindd_domain *domain,
1034 struct winbindd_cli_state *state)
1036 return WINBINDD_OK;