2 Unix SMB/CIFS implementation.
4 Async helpers for blocking functions
6 Copyright (C) Volker Lendecke 2005
8 The helpers always consist of three functions:
10 * A request setup function that takes the necessary parameters together
11 with a continuation function that is to be called upon completion
13 * A private continuation function that is internal only. This is to be
14 called by the lower-level functions in do_async(). Its only task is to
15 properly call the continuation function named above.
17 * A worker function that is called inside the appropriate child process.
19 This program is free software; you can redistribute it and/or modify
20 it under the terms of the GNU General Public License as published by
21 the Free Software Foundation; either version 2 of the License, or
22 (at your option) any later version.
24 This program is distributed in the hope that it will be useful,
25 but WITHOUT ANY WARRANTY; without even the implied warranty of
26 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 GNU General Public License for more details.
29 You should have received a copy of the GNU General Public License
30 along with this program; if not, write to the Free Software
31 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
38 #define DBGC_CLASS DBGC_WINBIND
40 struct do_async_state
{
42 struct winbindd_request request
;
43 struct winbindd_response response
;
44 void (*cont
)(TALLOC_CTX
*mem_ctx
,
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 static 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_P(mem_ctx
, struct do_async_state
);
71 DEBUG(0, ("talloc failed\n"));
72 cont(mem_ctx
, False
, NULL
, c
, private_data
);
76 state
->mem_ctx
= mem_ctx
;
77 state
->request
= *request
;
78 state
->request
.length
= sizeof(state
->request
);
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_P(mem_ctx
, struct do_async_state
);
98 DEBUG(0, ("talloc failed\n"));
99 cont(mem_ctx
, False
, NULL
, c
, private_data
);
103 state
->mem_ctx
= mem_ctx
;
104 state
->request
= *request
;
105 state
->request
.length
= sizeof(state
->request
);
108 state
->private_data
= private_data
;
110 async_domain_request(mem_ctx
, domain
, &state
->request
,
111 &state
->response
, do_async_recv
, state
);
114 static void idmap_set_mapping_recv(TALLOC_CTX
*mem_ctx
, BOOL success
,
115 struct winbindd_response
*response
,
116 void *c
, void *private_data
)
118 void (*cont
)(void *priv
, BOOL succ
) = c
;
121 DEBUG(5, ("Could not trigger idmap_set_mapping\n"));
122 cont(private_data
, False
);
126 if (response
->result
!= WINBINDD_OK
) {
127 DEBUG(5, ("idmap_set_mapping returned an error\n"));
128 cont(private_data
, False
);
132 cont(private_data
, True
);
135 void idmap_set_mapping_async(TALLOC_CTX
*mem_ctx
, const DOM_SID
*sid
,
136 unid_t id
, int id_type
,
137 void (*cont
)(void *private_data
, BOOL success
),
140 struct winbindd_request request
;
141 ZERO_STRUCT(request
);
142 request
.cmd
= WINBINDD_DUAL_IDMAPSET
;
143 if (id_type
== ID_USERID
)
144 request
.data
.dual_idmapset
.uid
= id
.uid
;
146 request
.data
.dual_idmapset
.gid
= id
.gid
;
147 request
.data
.dual_idmapset
.type
= id_type
;
148 sid_to_string(request
.data
.dual_idmapset
.sid
, sid
);
150 do_async(mem_ctx
, idmap_child(), &request
, idmap_set_mapping_recv
,
154 enum winbindd_result
winbindd_dual_idmapset(struct winbindd_domain
*domain
,
155 struct winbindd_cli_state
*state
)
161 DEBUG(3, ("[%5lu]: dual_idmapset\n", (unsigned long)state
->pid
));
163 if (!string_to_sid(&sid
, state
->request
.data
.dual_idmapset
.sid
))
164 return WINBINDD_ERROR
;
166 if (state
->request
.data
.dual_idmapset
.type
== ID_USERID
)
167 id
.uid
= state
->request
.data
.dual_idmapset
.uid
;
169 id
.gid
= state
->request
.data
.dual_idmapset
.gid
;
171 result
= idmap_set_mapping(&sid
, id
,
172 state
->request
.data
.dual_idmapset
.type
);
173 return NT_STATUS_IS_OK(result
) ? WINBINDD_OK
: WINBINDD_ERROR
;
176 static void idmap_sid2uid_recv(TALLOC_CTX
*mem_ctx
, BOOL success
,
177 struct winbindd_response
*response
,
178 void *c
, void *private_data
);
180 void idmap_sid2uid_async(TALLOC_CTX
*mem_ctx
, const DOM_SID
*sid
, BOOL alloc
,
181 void (*cont
)(void *private_data
, BOOL success
, uid_t uid
),
184 struct winbindd_request request
;
185 ZERO_STRUCT(request
);
186 request
.cmd
= WINBINDD_DUAL_SID2UID
;
187 sid_to_string(request
.data
.dual_sid2id
.sid
, sid
);
188 request
.data
.dual_sid2id
.alloc
= alloc
;
189 do_async(mem_ctx
, idmap_child(), &request
, idmap_sid2uid_recv
,
193 enum winbindd_result
winbindd_dual_sid2uid(struct winbindd_domain
*domain
,
194 struct winbindd_cli_state
*state
)
199 DEBUG(3, ("[%5lu]: sid to uid %s\n", (unsigned long)state
->pid
,
200 state
->request
.data
.dual_sid2id
.sid
));
202 if (!string_to_sid(&sid
, state
->request
.data
.dual_sid2id
.sid
)) {
203 DEBUG(1, ("Could not get convert sid %s from string\n",
204 state
->request
.data
.dual_sid2id
.sid
));
205 return WINBINDD_ERROR
;
208 /* Find uid for this sid and return it, possibly ask the slow remote
211 result
= idmap_sid_to_uid(&sid
, &(state
->response
.data
.uid
),
212 state
->request
.data
.dual_sid2id
.alloc
?
215 return NT_STATUS_IS_OK(result
) ? WINBINDD_OK
: WINBINDD_ERROR
;
218 static void idmap_sid2uid_recv(TALLOC_CTX
*mem_ctx
, BOOL success
,
219 struct winbindd_response
*response
,
220 void *c
, void *private_data
)
222 void (*cont
)(void *priv
, BOOL succ
, uid_t uid
) = c
;
225 DEBUG(5, ("Could not trigger sid2uid\n"));
226 cont(private_data
, False
, 0);
230 if (response
->result
!= WINBINDD_OK
) {
231 DEBUG(5, ("sid2uid returned an error\n"));
232 cont(private_data
, False
, 0);
236 cont(private_data
, True
, response
->data
.uid
);
239 static void uid2name_recv(TALLOC_CTX
*mem_ctx
, BOOL success
,
240 struct winbindd_response
*response
,
241 void *c
, void *private_data
);
243 void winbindd_uid2name_async(TALLOC_CTX
*mem_ctx
, uid_t uid
,
244 void (*cont
)(void *private_data
, BOOL success
,
248 struct winbindd_request request
;
249 ZERO_STRUCT(request
);
250 request
.cmd
= WINBINDD_DUAL_UID2NAME
;
251 request
.data
.uid
= uid
;
252 do_async(mem_ctx
, idmap_child(), &request
, uid2name_recv
,
256 enum winbindd_result
winbindd_dual_uid2name(struct winbindd_domain
*domain
,
257 struct winbindd_cli_state
*state
)
261 DEBUG(3, ("[%5lu]: uid2name %lu\n", (unsigned long)state
->pid
,
262 (unsigned long)state
->request
.data
.uid
));
264 pw
= getpwuid(state
->request
.data
.uid
);
266 DEBUG(5, ("User %lu not found\n",
267 (unsigned long)state
->request
.data
.uid
));
268 return WINBINDD_ERROR
;
271 fstrcpy(state
->response
.data
.name
.name
, pw
->pw_name
);
275 static void uid2name_recv(TALLOC_CTX
*mem_ctx
, BOOL success
,
276 struct winbindd_response
*response
,
277 void *c
, void *private_data
)
279 void (*cont
)(void *priv
, BOOL succ
, const char *name
) = c
;
282 DEBUG(5, ("Could not trigger uid2name\n"));
283 cont(private_data
, False
, NULL
);
287 if (response
->result
!= WINBINDD_OK
) {
288 DEBUG(5, ("uid2name returned an error\n"));
289 cont(private_data
, False
, NULL
);
293 cont(private_data
, True
, response
->data
.name
.name
);
296 static void name2uid_recv(TALLOC_CTX
*mem_ctx
, BOOL success
,
297 struct winbindd_response
*response
,
298 void *c
, void *private_data
);
300 static void winbindd_name2uid_async(TALLOC_CTX
*mem_ctx
, const char *name
,
301 void (*cont
)(void *private_data
, BOOL success
,
305 struct winbindd_request request
;
306 ZERO_STRUCT(request
);
307 request
.cmd
= WINBINDD_DUAL_NAME2UID
;
308 fstrcpy(request
.data
.username
, name
);
309 do_async(mem_ctx
, idmap_child(), &request
, name2uid_recv
,
313 enum winbindd_result
winbindd_dual_name2uid(struct winbindd_domain
*domain
,
314 struct winbindd_cli_state
*state
)
318 /* Ensure null termination */
319 state
->request
.data
.username
320 [sizeof(state
->request
.data
.username
)-1] = '\0';
322 DEBUG(3, ("[%5lu]: name2uid %s\n", (unsigned long)state
->pid
,
323 state
->request
.data
.username
));
325 pw
= getpwnam(state
->request
.data
.username
);
327 return WINBINDD_ERROR
;
330 state
->response
.data
.uid
= pw
->pw_uid
;
334 static void name2uid_recv(TALLOC_CTX
*mem_ctx
, BOOL success
,
335 struct winbindd_response
*response
,
336 void *c
, void *private_data
)
338 void (*cont
)(void *priv
, BOOL succ
, uid_t uid
) = c
;
341 DEBUG(5, ("Could not trigger name2uid\n"));
342 cont(private_data
, False
, 0);
346 if (response
->result
!= WINBINDD_OK
) {
347 DEBUG(5, ("name2uid returned an error\n"));
348 cont(private_data
, False
, 0);
352 cont(private_data
, True
, response
->data
.uid
);
355 static void idmap_sid2gid_recv(TALLOC_CTX
*mem_ctx
, BOOL success
,
356 struct winbindd_response
*response
,
357 void *c
, void *private_data
);
359 void idmap_sid2gid_async(TALLOC_CTX
*mem_ctx
, const DOM_SID
*sid
, BOOL alloc
,
360 void (*cont
)(void *private_data
, BOOL success
, gid_t gid
),
363 struct winbindd_request request
;
364 ZERO_STRUCT(request
);
365 request
.cmd
= WINBINDD_DUAL_SID2GID
;
366 sid_to_string(request
.data
.dual_sid2id
.sid
, sid
);
367 request
.data
.dual_sid2id
.alloc
= alloc
;
368 do_async(mem_ctx
, idmap_child(), &request
, idmap_sid2gid_recv
,
372 enum winbindd_result
winbindd_dual_sid2gid(struct winbindd_domain
*domain
,
373 struct winbindd_cli_state
*state
)
378 DEBUG(3, ("[%5lu]: sid to gid %s\n", (unsigned long)state
->pid
,
379 state
->request
.data
.dual_sid2id
.sid
));
381 if (!string_to_sid(&sid
, state
->request
.data
.dual_sid2id
.sid
)) {
382 DEBUG(1, ("Could not get convert sid %s from string\n",
383 state
->request
.data
.dual_sid2id
.sid
));
384 return WINBINDD_ERROR
;
387 /* Find gid for this sid and return it, possibly ask the slow remote
390 result
= idmap_sid_to_gid(&sid
, &(state
->response
.data
.gid
),
391 state
->request
.data
.dual_sid2id
.alloc
?
394 return NT_STATUS_IS_OK(result
) ? WINBINDD_OK
: WINBINDD_ERROR
;
397 static void idmap_sid2gid_recv(TALLOC_CTX
*mem_ctx
, BOOL success
,
398 struct winbindd_response
*response
,
399 void *c
, void *private_data
)
401 void (*cont
)(void *priv
, BOOL succ
, gid_t gid
) = c
;
404 DEBUG(5, ("Could not trigger sid2gid\n"));
405 cont(private_data
, False
, 0);
409 if (response
->result
!= WINBINDD_OK
) {
410 DEBUG(5, ("sid2gid returned an error\n"));
411 cont(private_data
, False
, 0);
415 cont(private_data
, True
, response
->data
.gid
);
418 static void gid2name_recv(TALLOC_CTX
*mem_ctx
, BOOL success
,
419 struct winbindd_response
*response
,
420 void *c
, void *private_data
)
422 void (*cont
)(void *priv
, BOOL succ
, const char *name
) = c
;
425 DEBUG(5, ("Could not trigger gid2name\n"));
426 cont(private_data
, False
, NULL
);
430 if (response
->result
!= WINBINDD_OK
) {
431 DEBUG(5, ("gid2name returned an error\n"));
432 cont(private_data
, False
, NULL
);
436 cont(private_data
, True
, response
->data
.name
.name
);
439 void winbindd_gid2name_async(TALLOC_CTX
*mem_ctx
, gid_t gid
,
440 void (*cont
)(void *private_data
, BOOL success
,
444 struct winbindd_request request
;
445 ZERO_STRUCT(request
);
446 request
.cmd
= WINBINDD_DUAL_GID2NAME
;
447 request
.data
.gid
= gid
;
448 do_async(mem_ctx
, idmap_child(), &request
, gid2name_recv
,
452 enum winbindd_result
winbindd_dual_gid2name(struct winbindd_domain
*domain
,
453 struct winbindd_cli_state
*state
)
457 DEBUG(3, ("[%5lu]: gid2name %lu\n", (unsigned long)state
->pid
,
458 (unsigned long)state
->request
.data
.gid
));
460 gr
= getgrgid(state
->request
.data
.gid
);
462 return WINBINDD_ERROR
;
464 fstrcpy(state
->response
.data
.name
.name
, gr
->gr_name
);
468 static void name2gid_recv(TALLOC_CTX
*mem_ctx
, BOOL success
,
469 struct winbindd_response
*response
,
470 void *c
, void *private_data
);
472 static void winbindd_name2gid_async(TALLOC_CTX
*mem_ctx
, const char *name
,
473 void (*cont
)(void *private_data
, BOOL success
,
477 struct winbindd_request request
;
478 ZERO_STRUCT(request
);
479 request
.cmd
= WINBINDD_DUAL_NAME2GID
;
480 fstrcpy(request
.data
.groupname
, name
);
481 do_async(mem_ctx
, idmap_child(), &request
, name2gid_recv
,
485 enum winbindd_result
winbindd_dual_name2gid(struct winbindd_domain
*domain
,
486 struct winbindd_cli_state
*state
)
490 /* Ensure null termination */
491 state
->request
.data
.groupname
492 [sizeof(state
->request
.data
.groupname
)-1] = '\0';
494 DEBUG(3, ("[%5lu]: name2gid %s\n", (unsigned long)state
->pid
,
495 state
->request
.data
.groupname
));
497 gr
= getgrnam(state
->request
.data
.groupname
);
499 return WINBINDD_ERROR
;
502 state
->response
.data
.gid
= gr
->gr_gid
;
506 static void name2gid_recv(TALLOC_CTX
*mem_ctx
, BOOL success
,
507 struct winbindd_response
*response
,
508 void *c
, void *private_data
)
510 void (*cont
)(void *priv
, BOOL succ
, gid_t gid
) = c
;
513 DEBUG(5, ("Could not trigger name2gid\n"));
514 cont(private_data
, False
, 0);
518 if (response
->result
!= WINBINDD_OK
) {
519 DEBUG(5, ("name2gid returned an error\n"));
520 cont(private_data
, False
, 0);
524 cont(private_data
, True
, response
->data
.gid
);
528 static void lookupsid_recv(TALLOC_CTX
*mem_ctx
, BOOL success
,
529 struct winbindd_response
*response
,
530 void *c
, void *private_data
)
532 void (*cont
)(void *priv
, BOOL succ
, const char *dom_name
,
533 const char *name
, enum SID_NAME_USE type
) = c
;
536 DEBUG(5, ("Could not trigger lookupsid\n"));
537 cont(private_data
, False
, NULL
, NULL
, SID_NAME_UNKNOWN
);
541 if (response
->result
!= WINBINDD_OK
) {
542 DEBUG(5, ("lookupsid returned an error\n"));
543 cont(private_data
, False
, NULL
, NULL
, SID_NAME_UNKNOWN
);
547 cont(private_data
, True
, response
->data
.name
.dom_name
,
548 response
->data
.name
.name
, response
->data
.name
.type
);
551 void winbindd_lookupsid_async(TALLOC_CTX
*mem_ctx
, const DOM_SID
*sid
,
552 void (*cont
)(void *private_data
, BOOL success
,
553 const char *dom_name
,
555 enum SID_NAME_USE type
),
558 struct winbindd_domain
*domain
;
559 struct winbindd_request request
;
561 domain
= find_lookup_domain_from_sid(sid
);
562 if (domain
== NULL
) {
563 DEBUG(5, ("Could not find domain for sid %s\n",
564 sid_string_static(sid
)));
565 cont(private_data
, False
, NULL
, NULL
, SID_NAME_UNKNOWN
);
569 ZERO_STRUCT(request
);
570 request
.cmd
= WINBINDD_LOOKUPSID
;
571 fstrcpy(request
.data
.sid
, sid_string_static(sid
));
573 do_async_domain(mem_ctx
, domain
, &request
, lookupsid_recv
,
577 enum winbindd_result
winbindd_dual_lookupsid(struct winbindd_domain
*domain
,
578 struct winbindd_cli_state
*state
)
580 enum SID_NAME_USE type
;
585 /* Ensure null termination */
586 state
->request
.data
.sid
[sizeof(state
->request
.data
.sid
)-1]='\0';
588 DEBUG(3, ("[%5lu]: lookupsid %s\n", (unsigned long)state
->pid
,
589 state
->request
.data
.sid
));
591 /* Lookup sid from PDC using lsa_lookup_sids() */
593 if (!string_to_sid(&sid
, state
->request
.data
.sid
)) {
594 DEBUG(5, ("%s not a SID\n", state
->request
.data
.sid
));
595 return WINBINDD_ERROR
;
600 if (!winbindd_lookup_name_by_sid(state
->mem_ctx
, &sid
, dom_name
, name
,
602 return WINBINDD_ERROR
;
605 fstrcpy(state
->response
.data
.name
.dom_name
, dom_name
);
606 fstrcpy(state
->response
.data
.name
.name
, name
);
607 state
->response
.data
.name
.type
= type
;
612 static void lookupname_recv(TALLOC_CTX
*mem_ctx
, BOOL success
,
613 struct winbindd_response
*response
,
614 void *c
, void *private_data
)
616 void (*cont
)(void *priv
, BOOL succ
, const DOM_SID
*sid
,
617 enum SID_NAME_USE type
) = c
;
621 DEBUG(5, ("Could not trigger lookup_name\n"));
622 cont(private_data
, False
, NULL
, SID_NAME_UNKNOWN
);
626 if (response
->result
!= WINBINDD_OK
) {
627 DEBUG(5, ("lookup_name returned an error\n"));
628 cont(private_data
, False
, NULL
, SID_NAME_UNKNOWN
);
632 if (!string_to_sid(&sid
, response
->data
.sid
.sid
)) {
633 DEBUG(0, ("Could not convert string %s to sid\n",
634 response
->data
.sid
.sid
));
635 cont(private_data
, False
, NULL
, SID_NAME_UNKNOWN
);
639 cont(private_data
, True
, &sid
, response
->data
.sid
.type
);
642 void winbindd_lookupname_async(TALLOC_CTX
*mem_ctx
, const char *dom_name
,
644 void (*cont
)(void *private_data
, BOOL success
,
646 enum SID_NAME_USE type
),
649 struct winbindd_request request
;
650 struct winbindd_domain
*domain
;
652 domain
= find_lookup_domain_from_name(dom_name
);
654 if (domain
== NULL
) {
655 DEBUG(5, ("Could not find domain for name %s\n", dom_name
));
656 cont(private_data
, False
, NULL
, SID_NAME_UNKNOWN
);
660 ZERO_STRUCT(request
);
661 request
.cmd
= WINBINDD_LOOKUPNAME
;
662 fstrcpy(request
.data
.name
.dom_name
, dom_name
);
663 fstrcpy(request
.data
.name
.name
, name
);
665 do_async_domain(mem_ctx
, domain
, &request
, lookupname_recv
,
669 enum winbindd_result
winbindd_dual_lookupname(struct winbindd_domain
*domain
,
670 struct winbindd_cli_state
*state
)
672 enum SID_NAME_USE type
;
673 char *name_domain
, *name_user
;
677 /* Ensure null termination */
678 state
->request
.data
.sid
[sizeof(state
->request
.data
.name
.dom_name
)-1]='\0';
680 /* Ensure null termination */
681 state
->request
.data
.sid
[sizeof(state
->request
.data
.name
.name
)-1]='\0';
683 /* cope with the name being a fully qualified name */
684 p
= strstr(state
->request
.data
.name
.name
, lp_winbind_separator());
687 name_domain
= state
->request
.data
.name
.name
;
690 name_domain
= state
->request
.data
.name
.dom_name
;
691 name_user
= state
->request
.data
.name
.name
;
694 DEBUG(3, ("[%5lu]: lookupname %s%s%s\n", (unsigned long)state
->pid
,
695 name_domain
, lp_winbind_separator(), name_user
));
697 /* Lookup name from PDC using lsa_lookup_names() */
698 if (!winbindd_lookup_sid_by_name(state
->mem_ctx
, domain
, name_domain
,
699 name_user
, &sid
, &type
)) {
700 return WINBINDD_ERROR
;
703 sid_to_string(state
->response
.data
.sid
.sid
, &sid
);
704 state
->response
.data
.sid
.type
= type
;
709 BOOL
print_sidlist(TALLOC_CTX
*mem_ctx
, const DOM_SID
*sids
,
710 int num_sids
, char **result
, ssize_t
*len
)
717 for (i
=0; i
<num_sids
; i
++) {
718 sprintf_append(mem_ctx
, result
, len
, &buflen
,
719 "%s\n", sid_string_static(&sids
[i
]));
722 if ((num_sids
!= 0) && (*result
== NULL
)) {
729 BOOL
parse_sidlist(TALLOC_CTX
*mem_ctx
, char *sidstr
,
730 DOM_SID
**sids
, int *num_sids
)
738 while (p
[0] != '\0') {
742 DEBUG(0, ("Got invalid sidstr: %s\n", p
));
747 if (!string_to_sid(&sid
, p
)) {
748 DEBUG(0, ("Could not parse sid %s\n", p
));
751 add_sid_to_array(mem_ctx
, &sid
, sids
, num_sids
);
757 BOOL
print_ridlist(TALLOC_CTX
*mem_ctx
, uint32
*rids
, int num_rids
,
758 char **result
, ssize_t
*len
)
765 for (i
=0; i
<num_rids
; i
++) {
766 sprintf_append(mem_ctx
, result
, len
, &buflen
,
770 if ((num_rids
!= 0) && (*result
== NULL
)) {
777 BOOL
parse_ridlist(TALLOC_CTX
*mem_ctx
, char *ridstr
,
778 uint32
**sids
, int *num_rids
)
786 while (p
[0] != '\0') {
789 rid
= strtoul(p
, &q
, 10);
791 DEBUG(0, ("Got invalid ridstr: %s\n", p
));
795 ADD_TO_ARRAY(mem_ctx
, uint32
, rid
, sids
, num_rids
);
800 static void getsidaliases_recv(TALLOC_CTX
*mem_ctx
, BOOL success
,
801 struct winbindd_response
*response
,
802 void *c
, void *private_data
)
804 void (*cont
)(void *priv
, BOOL succ
,
805 DOM_SID
*aliases
, int num_aliases
) = c
;
807 DOM_SID
*sids
= NULL
;
811 DEBUG(5, ("Could not trigger getsidaliases\n"));
812 cont(private_data
, success
, NULL
, 0);
816 if (response
->result
!= WINBINDD_OK
) {
817 DEBUG(5, ("getsidaliases returned an error\n"));
818 cont(private_data
, False
, NULL
, 0);
822 aliases_str
= response
->extra_data
;
824 if (aliases_str
== NULL
) {
825 DEBUG(10, ("getsidaliases return 0 SIDs\n"));
826 cont(private_data
, True
, NULL
, 0);
830 if (!parse_sidlist(mem_ctx
, aliases_str
, &sids
, &num_sids
)) {
831 DEBUG(0, ("Could not parse sids\n"));
832 cont(private_data
, False
, NULL
, 0);
836 SAFE_FREE(response
->extra_data
);
838 cont(private_data
, True
, sids
, num_sids
);
841 void winbindd_getsidaliases_async(struct winbindd_domain
*domain
,
843 const DOM_SID
*sids
, int num_sids
,
844 void (*cont
)(void *private_data
,
846 const DOM_SID
*aliases
,
850 struct winbindd_request request
;
855 cont(private_data
, True
, NULL
, 0);
859 if (!print_sidlist(mem_ctx
, sids
, num_sids
, &sidstr
, &len
)) {
860 cont(private_data
, False
, NULL
, 0);
864 ZERO_STRUCT(request
);
865 request
.cmd
= WINBINDD_DUAL_GETSIDALIASES
;
866 request
.extra_len
= len
;
867 request
.extra_data
= sidstr
;
869 do_async_domain(mem_ctx
, domain
, &request
, getsidaliases_recv
,
873 enum winbindd_result
winbindd_dual_getsidaliases(struct winbindd_domain
*domain
,
874 struct winbindd_cli_state
*state
)
876 DOM_SID
*sids
= NULL
;
884 DEBUG(3, ("[%5lu]: getsidaliases\n", (unsigned long)state
->pid
));
886 sidstr
= state
->request
.extra_data
;
888 sidstr
= talloc_strdup(state
->mem_ctx
, "\n"); /* No SID */
890 DEBUG(10, ("Sidlist: %s\n", sidstr
));
892 if (!parse_sidlist(state
->mem_ctx
, sidstr
, &sids
, &num_sids
)) {
893 DEBUG(0, ("Could not parse SID list: %s\n", sidstr
));
894 return WINBINDD_ERROR
;
900 result
= domain
->methods
->lookup_useraliases(domain
,
906 if (!NT_STATUS_IS_OK(result
)) {
907 DEBUG(3, ("Could not lookup_useraliases: %s\n",
909 return WINBINDD_ERROR
;
915 DEBUG(10, ("Got %d aliases\n", num_aliases
));
917 for (i
=0; i
<num_aliases
; i
++) {
919 DEBUGADD(10, (" rid %d\n", alias_rids
[i
]));
920 sid_copy(&sid
, &domain
->sid
);
921 sid_append_rid(&sid
, alias_rids
[i
]);
922 add_sid_to_array(state
->mem_ctx
, &sid
, &sids
, &num_sids
);
925 if (!print_sidlist(NULL
, sids
, num_sids
,
926 (char **)&state
->response
.extra_data
, &len
)) {
927 DEBUG(0, ("Could not print_sidlist\n"));
928 return WINBINDD_ERROR
;
931 if (state
->response
.extra_data
!= NULL
) {
932 DEBUG(10, ("aliases_list: %s\n",
933 (char *)state
->response
.extra_data
));
934 state
->response
.length
+= len
+1;
940 struct gettoken_state
{
943 struct winbindd_domain
*alias_domain
;
944 struct winbindd_domain
*builtin_domain
;
947 void (*cont
)(void *private_data
, BOOL success
, DOM_SID
*sids
, int num_sids
);
951 static void gettoken_recvdomgroups(TALLOC_CTX
*mem_ctx
, BOOL success
,
952 struct winbindd_response
*response
,
953 void *c
, void *private_data
);
954 static void gettoken_recvaliases(void *private_data
, BOOL success
,
955 const DOM_SID
*aliases
,
959 void winbindd_gettoken_async(TALLOC_CTX
*mem_ctx
, const DOM_SID
*user_sid
,
960 void (*cont
)(void *private_data
, BOOL success
,
961 DOM_SID
*sids
, int num_sids
),
964 struct winbindd_domain
*domain
;
965 struct winbindd_request request
;
966 struct gettoken_state
*state
;
968 state
= TALLOC_P(mem_ctx
, struct gettoken_state
);
970 DEBUG(0, ("talloc failed\n"));
971 cont(private_data
, False
, NULL
, 0);
975 state
->mem_ctx
= mem_ctx
;
976 sid_copy(&state
->user_sid
, user_sid
);
977 state
->alias_domain
= find_our_domain();
978 state
->builtin_domain
= find_builtin_domain();
980 state
->private_data
= private_data
;
982 domain
= find_domain_from_sid_noinit(user_sid
);
983 if (domain
== NULL
) {
984 DEBUG(5, ("Could not find domain from SID %s\n",
985 sid_string_static(user_sid
)));
986 cont(private_data
, False
, NULL
, 0);
990 ZERO_STRUCT(request
);
991 request
.cmd
= WINBINDD_GETUSERDOMGROUPS
;
992 fstrcpy(request
.data
.sid
, sid_string_static(user_sid
));
994 do_async_domain(mem_ctx
, domain
, &request
, gettoken_recvdomgroups
,
998 static void gettoken_recvdomgroups(TALLOC_CTX
*mem_ctx
, BOOL success
,
999 struct winbindd_response
*response
,
1000 void *c
, void *private_data
)
1002 struct gettoken_state
*state
=
1003 talloc_get_type_abort(private_data
, struct gettoken_state
);
1007 DEBUG(10, ("Could not get domain groups\n"));
1008 state
->cont(state
->private_data
, False
, NULL
, 0);
1012 sids_str
= response
->extra_data
;
1014 if (sids_str
== NULL
) {
1015 DEBUG(10, ("Received no domain groups\n"));
1016 state
->cont(state
->private_data
, True
, NULL
, 0);
1021 state
->num_sids
= 0;
1023 add_sid_to_array(mem_ctx
, &state
->user_sid
, &state
->sids
,
1026 if (!parse_sidlist(mem_ctx
, sids_str
, &state
->sids
,
1027 &state
->num_sids
)) {
1028 DEBUG(0, ("Could not parse sids\n"));
1029 state
->cont(state
->private_data
, False
, NULL
, 0);
1033 SAFE_FREE(response
->extra_data
);
1035 if (state
->alias_domain
== NULL
) {
1036 DEBUG(10, ("Don't expand domain local groups\n"));
1037 state
->cont(state
->private_data
, True
, state
->sids
,
1042 winbindd_getsidaliases_async(state
->alias_domain
, mem_ctx
,
1043 state
->sids
, state
->num_sids
,
1044 gettoken_recvaliases
, state
);
1047 static void gettoken_recvaliases(void *private_data
, BOOL success
,
1048 const DOM_SID
*aliases
,
1051 struct gettoken_state
*state
= private_data
;
1055 DEBUG(10, ("Could not receive domain local groups\n"));
1056 state
->cont(state
->private_data
, False
, NULL
, 0);
1060 for (i
=0; i
<num_aliases
; i
++)
1061 add_sid_to_array(state
->mem_ctx
, &aliases
[i
],
1062 &state
->sids
, &state
->num_sids
);
1064 if (state
->builtin_domain
!= NULL
) {
1065 struct winbindd_domain
*builtin_domain
= state
->builtin_domain
;
1066 DEBUG(10, ("Expanding our own local groups\n"));
1067 state
->builtin_domain
= NULL
;
1068 winbindd_getsidaliases_async(builtin_domain
, state
->mem_ctx
,
1069 state
->sids
, state
->num_sids
,
1070 gettoken_recvaliases
, state
);
1074 state
->cont(state
->private_data
, True
, state
->sids
, state
->num_sids
);
1077 struct sid2uid_state
{
1078 TALLOC_CTX
*mem_ctx
;
1082 void (*cont
)(void *private_data
, BOOL success
, uid_t uid
);
1086 static void sid2uid_lookup_sid_recv(void *private_data
, BOOL success
,
1087 const char *dom_name
, const char *name
,
1088 enum SID_NAME_USE type
);
1089 static void sid2uid_noalloc_recv(void *private_data
, BOOL success
, uid_t uid
);
1090 static void sid2uid_alloc_recv(void *private_data
, BOOL success
, uid_t uid
);
1091 static void sid2uid_name2uid_recv(void *private_data
, BOOL success
, uid_t uid
);
1092 static void sid2uid_set_mapping_recv(void *private_data
, BOOL success
);
1094 void winbindd_sid2uid_async(TALLOC_CTX
*mem_ctx
, const DOM_SID
*sid
,
1095 void (*cont
)(void *private_data
, BOOL success
,
1099 struct sid2uid_state
*state
;
1103 if (idmap_proxyonly()) {
1104 DEBUG(10, ("idmap proxy only\n"));
1105 cont(private_data
, False
, 0);
1109 /* Query only the local tdb, everything else might possibly block */
1111 result
= idmap_sid_to_uid(sid
, &uid
, ID_QUERY_ONLY
|ID_CACHE_ONLY
);
1113 if (NT_STATUS_IS_OK(result
)) {
1114 cont(private_data
, True
, uid
);
1118 state
= TALLOC_P(mem_ctx
, struct sid2uid_state
);
1119 if (state
== NULL
) {
1120 DEBUG(0, ("talloc failed\n"));
1121 cont(private_data
, False
, 0);
1125 state
->mem_ctx
= mem_ctx
;
1128 state
->private_data
= private_data
;
1130 /* Let's see if it's really a user before allocating a uid */
1132 winbindd_lookupsid_async(mem_ctx
, sid
, sid2uid_lookup_sid_recv
, state
);
1135 static void sid2uid_lookup_sid_recv(void *private_data
, BOOL success
,
1136 const char *dom_name
, const char *name
,
1137 enum SID_NAME_USE type
)
1139 struct sid2uid_state
*state
=
1140 talloc_get_type_abort(private_data
, struct sid2uid_state
);
1143 DEBUG(5, ("Could not trigger lookup_sid\n"));
1144 state
->cont(state
->private_data
, False
, 0);
1148 if ((type
!= SID_NAME_USER
) && (type
!= SID_NAME_COMPUTER
)) {
1149 DEBUG(5, ("SID is not a user\n"));
1150 state
->cont(state
->private_data
, False
, 0);
1154 state
->username
= talloc_strdup(state
->mem_ctx
, name
);
1156 /* Ask the possibly blocking remote IDMAP */
1158 idmap_sid2uid_async(state
->mem_ctx
, &state
->sid
, False
,
1159 sid2uid_noalloc_recv
, state
);
1162 static void sid2uid_noalloc_recv(void *private_data
, BOOL success
, uid_t uid
)
1164 struct sid2uid_state
*state
=
1165 talloc_get_type_abort(private_data
, struct sid2uid_state
);
1168 DEBUG(10, ("found uid for sid %s in remote backend\n",
1169 sid_string_static(&state
->sid
)));
1170 state
->cont(state
->private_data
, True
, uid
);
1174 if (lp_winbind_trusted_domains_only() &&
1175 (sid_compare_domain(&state
->sid
, &find_our_domain()->sid
) == 0)) {
1176 DEBUG(10, ("Trying to go via nss\n"));
1177 winbindd_name2uid_async(state
->mem_ctx
, state
->username
,
1178 sid2uid_name2uid_recv
, state
);
1182 /* To be done: Here we're going to try the unixinfo pipe */
1184 /* Now allocate a uid */
1186 idmap_sid2uid_async(state
->mem_ctx
, &state
->sid
, True
,
1187 sid2uid_alloc_recv
, state
);
1190 static void sid2uid_alloc_recv(void *private_data
, BOOL success
, uid_t uid
)
1192 struct sid2uid_state
*state
=
1193 talloc_get_type_abort(private_data
, struct sid2uid_state
);
1196 DEBUG(5, ("Could not allocate uid\n"));
1197 state
->cont(state
->private_data
, False
, 0);
1201 state
->cont(state
->private_data
, True
, uid
);
1204 static void sid2uid_name2uid_recv(void *private_data
, BOOL success
, uid_t uid
)
1206 struct sid2uid_state
*state
=
1207 talloc_get_type_abort(private_data
, struct sid2uid_state
);
1211 DEBUG(5, ("Could not find uid for name %s\n",
1213 state
->cont(state
->private_data
, False
, 0);
1220 idmap_set_mapping_async(state
->mem_ctx
, &state
->sid
, id
, ID_USERID
,
1221 sid2uid_set_mapping_recv
, state
);
1224 static void sid2uid_set_mapping_recv(void *private_data
, BOOL success
)
1226 struct sid2uid_state
*state
=
1227 talloc_get_type_abort(private_data
, struct sid2uid_state
);
1230 DEBUG(5, ("Could not set ID mapping for sid %s\n",
1231 sid_string_static(&state
->sid
)));
1232 state
->cont(state
->private_data
, False
, 0);
1236 state
->cont(state
->private_data
, True
, state
->uid
);
1239 struct sid2gid_state
{
1240 TALLOC_CTX
*mem_ctx
;
1244 void (*cont
)(void *private_data
, BOOL success
, gid_t gid
);
1248 static void sid2gid_lookup_sid_recv(void *private_data
, BOOL success
,
1249 const char *dom_name
, const char *name
,
1250 enum SID_NAME_USE type
);
1251 static void sid2gid_noalloc_recv(void *private_data
, BOOL success
, gid_t gid
);
1252 static void sid2gid_alloc_recv(void *private_data
, BOOL success
, gid_t gid
);
1253 static void sid2gid_name2gid_recv(void *private_data
, BOOL success
, gid_t gid
);
1254 static void sid2gid_set_mapping_recv(void *private_data
, BOOL success
);
1256 void winbindd_sid2gid_async(TALLOC_CTX
*mem_ctx
, const DOM_SID
*sid
,
1257 void (*cont
)(void *private_data
, BOOL success
,
1261 struct sid2gid_state
*state
;
1265 if (idmap_proxyonly()) {
1266 DEBUG(10, ("idmap proxy only\n"));
1267 cont(private_data
, False
, 0);
1271 /* Query only the local tdb, everything else might possibly block */
1273 result
= idmap_sid_to_gid(sid
, &gid
, ID_QUERY_ONLY
|ID_CACHE_ONLY
);
1275 if (NT_STATUS_IS_OK(result
)) {
1276 cont(private_data
, True
, gid
);
1280 state
= TALLOC_P(mem_ctx
, struct sid2gid_state
);
1281 if (state
== NULL
) {
1282 DEBUG(0, ("talloc failed\n"));
1283 cont(private_data
, False
, 0);
1287 state
->mem_ctx
= mem_ctx
;
1290 state
->private_data
= private_data
;
1292 /* Let's see if it's really a user before allocating a gid */
1294 winbindd_lookupsid_async(mem_ctx
, sid
, sid2gid_lookup_sid_recv
, state
);
1297 static void sid2gid_lookup_sid_recv(void *private_data
, BOOL success
,
1298 const char *dom_name
, const char *name
,
1299 enum SID_NAME_USE type
)
1301 struct sid2gid_state
*state
=
1302 talloc_get_type_abort(private_data
, struct sid2gid_state
);
1305 DEBUG(5, ("Could not trigger lookup_sid\n"));
1306 state
->cont(state
->private_data
, False
, 0);
1310 if (((type
!= SID_NAME_DOM_GRP
) && (type
!= SID_NAME_ALIAS
) &&
1311 (type
!= SID_NAME_WKN_GRP
))) {
1312 DEBUG(5, ("SID is not a group\n"));
1313 state
->cont(state
->private_data
, False
, 0);
1317 state
->groupname
= talloc_strdup(state
->mem_ctx
, name
);
1319 /* Ask the possibly blocking remote IDMAP and allocate */
1321 idmap_sid2gid_async(state
->mem_ctx
, &state
->sid
, False
,
1322 sid2gid_noalloc_recv
, state
);
1325 static void sid2gid_noalloc_recv(void *private_data
, BOOL success
, gid_t gid
)
1327 struct sid2gid_state
*state
=
1328 talloc_get_type_abort(private_data
, struct sid2gid_state
);
1331 DEBUG(10, ("found gid for sid %s in remote backend\n",
1332 sid_string_static(&state
->sid
)));
1333 state
->cont(state
->private_data
, True
, gid
);
1337 if (lp_winbind_trusted_domains_only() &&
1338 (sid_compare_domain(&state
->sid
, &find_our_domain()->sid
) == 0)) {
1339 DEBUG(10, ("Trying to go via nss\n"));
1340 winbindd_name2gid_async(state
->mem_ctx
, state
->groupname
,
1341 sid2gid_name2gid_recv
, state
);
1345 /* To be done: Here we're going to try the unixinfo pipe */
1347 /* Now allocate a gid */
1349 idmap_sid2gid_async(state
->mem_ctx
, &state
->sid
, True
,
1350 sid2gid_alloc_recv
, state
);
1353 static void sid2gid_alloc_recv(void *private_data
, BOOL success
, gid_t gid
)
1355 struct sid2gid_state
*state
=
1356 talloc_get_type_abort(private_data
, struct sid2gid_state
);
1359 DEBUG(5, ("Could not allocate gid\n"));
1360 state
->cont(state
->private_data
, False
, 0);
1364 state
->cont(state
->private_data
, True
, gid
);
1367 static void sid2gid_name2gid_recv(void *private_data
, BOOL success
, gid_t gid
)
1369 struct sid2gid_state
*state
=
1370 talloc_get_type_abort(private_data
, struct sid2gid_state
);
1374 DEBUG(5, ("Could not find gid for name %s\n",
1376 state
->cont(state
->private_data
, False
, 0);
1383 idmap_set_mapping_async(state
->mem_ctx
, &state
->sid
, id
, ID_GROUPID
,
1384 sid2gid_set_mapping_recv
, state
);
1387 static void sid2gid_set_mapping_recv(void *private_data
, BOOL success
)
1389 struct sid2gid_state
*state
=
1390 talloc_get_type_abort(private_data
, struct sid2gid_state
);
1393 DEBUG(5, ("Could not set ID mapping for sid %s\n",
1394 sid_string_static(&state
->sid
)));
1395 state
->cont(state
->private_data
, False
, 0);
1399 state
->cont(state
->private_data
, True
, state
->gid
);
1402 static void query_user_recv(TALLOC_CTX
*mem_ctx
, BOOL success
,
1403 struct winbindd_response
*response
,
1404 void *c
, void *private_data
)
1406 void (*cont
)(void *priv
, BOOL succ
, const char *acct_name
,
1407 const char *full_name
, const char *homedir
,
1408 const char *shell
, uint32 group_rid
) = c
;
1411 DEBUG(5, ("Could not trigger query_user\n"));
1412 cont(private_data
, False
, NULL
, NULL
, NULL
, NULL
, -1);
1416 cont(private_data
, True
, response
->data
.user_info
.acct_name
,
1417 response
->data
.user_info
.full_name
,
1418 response
->data
.user_info
.homedir
,
1419 response
->data
.user_info
.shell
,
1420 response
->data
.user_info
.group_rid
);
1423 void query_user_async(TALLOC_CTX
*mem_ctx
, struct winbindd_domain
*domain
,
1425 void (*cont
)(void *private_data
, BOOL success
,
1426 const char *acct_name
,
1427 const char *full_name
,
1428 const char *homedir
,
1433 struct winbindd_request request
;
1434 ZERO_STRUCT(request
);
1435 request
.cmd
= WINBINDD_DUAL_USERINFO
;
1436 sid_to_string(request
.data
.sid
, sid
);
1437 do_async_domain(mem_ctx
, domain
, &request
, query_user_recv
,
1438 cont
, private_data
);