2 Unix SMB/CIFS implementation.
4 Copyright (C) Volker Lendecke 2005
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 a composite API for finding a DC and its name
25 #include "libcli/composite/composite.h"
26 #include "winbind/wb_async_helpers.h"
28 #include "libcli/security/security.h"
29 #include "librpc/gen_ndr/ndr_lsa_c.h"
30 #include "librpc/gen_ndr/ndr_samr_c.h"
32 #include "winbind/wb_helper.h"
35 struct lsa_lookupsids_state
{
36 struct composite_context
*ctx
;
38 struct lsa_LookupSids r
;
39 struct lsa_SidArray sids
;
40 struct lsa_TransNameArray names
;
41 struct lsa_RefDomainList
*domains
;
43 struct wb_sid_object
**result
;
46 static void lsa_lookupsids_recv_names(struct tevent_req
*subreq
);
48 struct composite_context
*wb_lsa_lookupsids_send(TALLOC_CTX
*mem_ctx
,
49 struct dcerpc_pipe
*lsa_pipe
,
50 struct policy_handle
*handle
,
52 const struct dom_sid
**sids
)
54 struct composite_context
*result
;
55 struct lsa_lookupsids_state
*state
;
57 struct tevent_req
*subreq
;
59 result
= composite_create(mem_ctx
, lsa_pipe
->conn
->event_ctx
);
60 if (result
== NULL
) goto failed
;
62 state
= talloc(result
, struct lsa_lookupsids_state
);
63 if (state
== NULL
) goto failed
;
64 result
->private_data
= state
;
67 state
->sids
.num_sids
= num_sids
;
68 state
->sids
.sids
= talloc_array(state
, struct lsa_SidPtr
, num_sids
);
69 if (state
->sids
.sids
== NULL
) goto failed
;
71 for (i
=0; i
<num_sids
; i
++) {
72 state
->sids
.sids
[i
].sid
= dom_sid_dup(state
->sids
.sids
,
74 if (state
->sids
.sids
[i
].sid
== NULL
) goto failed
;
77 state
->domains
= talloc(state
, struct lsa_RefDomainList
);
78 if (state
->domains
== NULL
) goto failed
;
81 state
->num_sids
= num_sids
;
82 state
->names
.count
= 0;
83 state
->names
.names
= NULL
;
85 state
->r
.in
.handle
= handle
;
86 state
->r
.in
.sids
= &state
->sids
;
87 state
->r
.in
.names
= &state
->names
;
88 state
->r
.in
.level
= 1;
89 state
->r
.in
.count
= &state
->count
;
90 state
->r
.out
.names
= &state
->names
;
91 state
->r
.out
.count
= &state
->count
;
92 state
->r
.out
.domains
= &state
->domains
;
94 subreq
= dcerpc_lsa_LookupSids_r_send(state
,
96 lsa_pipe
->binding_handle
,
98 if (subreq
== NULL
) goto failed
;
99 tevent_req_set_callback(subreq
, lsa_lookupsids_recv_names
, state
);
108 static void lsa_lookupsids_recv_names(struct tevent_req
*subreq
)
110 struct lsa_lookupsids_state
*state
=
111 tevent_req_callback_data(subreq
,
112 struct lsa_lookupsids_state
);
115 state
->ctx
->status
= dcerpc_lsa_LookupSids_r_recv(subreq
, state
);
117 if (!composite_is_ok(state
->ctx
)) return;
118 state
->ctx
->status
= state
->r
.out
.result
;
119 if (!NT_STATUS_IS_OK(state
->ctx
->status
) &&
120 !NT_STATUS_EQUAL(state
->ctx
->status
, STATUS_SOME_UNMAPPED
)) {
121 composite_error(state
->ctx
, state
->ctx
->status
);
125 state
->result
= talloc_array(state
, struct wb_sid_object
*,
127 if (composite_nomem(state
->result
, state
->ctx
)) return;
129 for (i
=0; i
<state
->num_sids
; i
++) {
130 struct lsa_TranslatedName
*name
=
131 &state
->r
.out
.names
->names
[i
];
132 struct lsa_DomainInfo
*dom
;
133 struct lsa_RefDomainList
*domains
=
136 state
->result
[i
] = talloc_zero(state
->result
,
137 struct wb_sid_object
);
138 if (composite_nomem(state
->result
[i
], state
->ctx
)) return;
140 state
->result
[i
]->type
= name
->sid_type
;
141 if (state
->result
[i
]->type
== SID_NAME_UNKNOWN
) {
145 if (name
->sid_index
>= domains
->count
) {
146 composite_error(state
->ctx
,
147 NT_STATUS_INVALID_PARAMETER
);
151 dom
= &domains
->domains
[name
->sid_index
];
152 state
->result
[i
]->domain
= talloc_reference(state
->result
[i
],
154 if ((name
->sid_type
== SID_NAME_DOMAIN
) ||
155 (name
->name
.string
== NULL
)) {
156 state
->result
[i
]->name
=
157 talloc_strdup(state
->result
[i
], "");
159 state
->result
[i
]->name
=
160 talloc_steal(state
->result
[i
],
164 if (composite_nomem(state
->result
[i
]->name
, state
->ctx
)) {
169 composite_done(state
->ctx
);
172 NTSTATUS
wb_lsa_lookupsids_recv(struct composite_context
*c
,
174 struct wb_sid_object
***names
)
176 NTSTATUS status
= composite_wait(c
);
177 if (NT_STATUS_IS_OK(status
)) {
178 struct lsa_lookupsids_state
*state
=
179 talloc_get_type(c
->private_data
,
180 struct lsa_lookupsids_state
);
181 *names
= talloc_steal(mem_ctx
, state
->result
);
188 struct lsa_lookupnames_state
{
189 struct composite_context
*ctx
;
191 struct lsa_LookupNames r
;
192 struct lsa_TransSidArray sids
;
193 struct lsa_RefDomainList
*domains
;
195 struct wb_sid_object
**result
;
198 static void lsa_lookupnames_recv_sids(struct tevent_req
*subreq
);
200 struct composite_context
*wb_lsa_lookupnames_send(TALLOC_CTX
*mem_ctx
,
201 struct dcerpc_pipe
*lsa_pipe
,
202 struct policy_handle
*handle
,
206 struct composite_context
*result
;
207 struct lsa_lookupnames_state
*state
;
208 struct tevent_req
*subreq
;
210 struct lsa_String
*lsa_names
;
213 result
= composite_create(mem_ctx
, lsa_pipe
->conn
->event_ctx
);
214 if (result
== NULL
) goto failed
;
216 state
= talloc(result
, struct lsa_lookupnames_state
);
217 if (state
== NULL
) goto failed
;
218 result
->private_data
= state
;
221 state
->sids
.count
= 0;
222 state
->sids
.sids
= NULL
;
223 state
->num_names
= num_names
;
226 lsa_names
= talloc_array(state
, struct lsa_String
, num_names
);
227 if (lsa_names
== NULL
) goto failed
;
229 for (i
=0; i
<num_names
; i
++) {
230 lsa_names
[i
].string
= names
[i
];
233 state
->domains
= talloc(state
, struct lsa_RefDomainList
);
234 if (state
->domains
== NULL
) goto failed
;
236 state
->r
.in
.handle
= handle
;
237 state
->r
.in
.num_names
= num_names
;
238 state
->r
.in
.names
= lsa_names
;
239 state
->r
.in
.sids
= &state
->sids
;
240 state
->r
.in
.level
= 1;
241 state
->r
.in
.count
= &state
->count
;
242 state
->r
.out
.count
= &state
->count
;
243 state
->r
.out
.sids
= &state
->sids
;
244 state
->r
.out
.domains
= &state
->domains
;
246 subreq
= dcerpc_lsa_LookupNames_r_send(state
,
248 lsa_pipe
->binding_handle
,
250 if (subreq
== NULL
) goto failed
;
251 tevent_req_set_callback(subreq
, lsa_lookupnames_recv_sids
, state
);
260 static void lsa_lookupnames_recv_sids(struct tevent_req
*subreq
)
262 struct lsa_lookupnames_state
*state
=
263 tevent_req_callback_data(subreq
,
264 struct lsa_lookupnames_state
);
267 state
->ctx
->status
= dcerpc_lsa_LookupNames_r_recv(subreq
, state
);
269 if (!composite_is_ok(state
->ctx
)) return;
270 state
->ctx
->status
= state
->r
.out
.result
;
271 if (!NT_STATUS_IS_OK(state
->ctx
->status
) &&
272 !NT_STATUS_EQUAL(state
->ctx
->status
, STATUS_SOME_UNMAPPED
)) {
273 composite_error(state
->ctx
, state
->ctx
->status
);
277 state
->result
= talloc_array(state
, struct wb_sid_object
*,
279 if (composite_nomem(state
->result
, state
->ctx
)) return;
281 for (i
=0; i
<state
->num_names
; i
++) {
282 struct lsa_TranslatedSid
*sid
= &state
->r
.out
.sids
->sids
[i
];
283 struct lsa_RefDomainList
*domains
= state
->domains
;
284 struct lsa_DomainInfo
*dom
;
286 state
->result
[i
] = talloc_zero(state
->result
,
287 struct wb_sid_object
);
288 if (composite_nomem(state
->result
[i
], state
->ctx
)) return;
290 state
->result
[i
]->type
= sid
->sid_type
;
291 if (state
->result
[i
]->type
== SID_NAME_UNKNOWN
) {
295 if (sid
->sid_index
>= domains
->count
) {
296 composite_error(state
->ctx
,
297 NT_STATUS_INVALID_PARAMETER
);
301 dom
= &domains
->domains
[sid
->sid_index
];
303 state
->result
[i
]->sid
= dom_sid_add_rid(state
->result
[i
],
307 composite_done(state
->ctx
);
310 NTSTATUS
wb_lsa_lookupnames_recv(struct composite_context
*c
,
312 struct wb_sid_object
***sids
)
314 NTSTATUS status
= composite_wait(c
);
315 if (NT_STATUS_IS_OK(status
)) {
316 struct lsa_lookupnames_state
*state
=
317 talloc_get_type(c
->private_data
,
318 struct lsa_lookupnames_state
);
319 *sids
= talloc_steal(mem_ctx
, state
->result
);
324 struct samr_getuserdomgroups_state
{
325 struct composite_context
*ctx
;
326 struct dcerpc_pipe
*samr_pipe
;
331 struct samr_RidWithAttributeArray
*rid_array
;
333 struct policy_handle
*user_handle
;
334 struct samr_OpenUser o
;
335 struct samr_GetGroupsForUser g
;
339 static void samr_usergroups_recv_open(struct tevent_req
*subreq
);
340 static void samr_usergroups_recv_groups(struct tevent_req
*subreq
);
341 static void samr_usergroups_recv_close(struct tevent_req
*subreq
);
343 struct composite_context
*wb_samr_userdomgroups_send(TALLOC_CTX
*mem_ctx
,
344 struct dcerpc_pipe
*samr_pipe
,
345 struct policy_handle
*domain_handle
,
348 struct composite_context
*result
;
349 struct samr_getuserdomgroups_state
*state
;
350 struct tevent_req
*subreq
;
352 result
= composite_create(mem_ctx
, samr_pipe
->conn
->event_ctx
);
353 if (result
== NULL
) goto failed
;
355 state
= talloc(result
, struct samr_getuserdomgroups_state
);
356 if (state
== NULL
) goto failed
;
357 result
->private_data
= state
;
360 state
->samr_pipe
= samr_pipe
;
362 state
->user_handle
= talloc(state
, struct policy_handle
);
363 if (state
->user_handle
== NULL
) goto failed
;
365 state
->o
.in
.domain_handle
= domain_handle
;
366 state
->o
.in
.access_mask
= SEC_FLAG_MAXIMUM_ALLOWED
;
367 state
->o
.in
.rid
= rid
;
368 state
->o
.out
.user_handle
= state
->user_handle
;
370 subreq
= dcerpc_samr_OpenUser_r_send(state
,
372 state
->samr_pipe
->binding_handle
,
374 if (subreq
== NULL
) goto failed
;
375 tevent_req_set_callback(subreq
, samr_usergroups_recv_open
, state
);
384 static void samr_usergroups_recv_open(struct tevent_req
*subreq
)
386 struct samr_getuserdomgroups_state
*state
=
387 tevent_req_callback_data(subreq
,
388 struct samr_getuserdomgroups_state
);
390 state
->ctx
->status
= dcerpc_samr_OpenUser_r_recv(subreq
, state
);
392 if (!composite_is_ok(state
->ctx
)) return;
393 state
->ctx
->status
= state
->o
.out
.result
;
394 if (!composite_is_ok(state
->ctx
)) return;
396 state
->g
.in
.user_handle
= state
->user_handle
;
397 state
->g
.out
.rids
= &state
->rid_array
;
399 subreq
= dcerpc_samr_GetGroupsForUser_r_send(state
,
400 state
->ctx
->event_ctx
,
401 state
->samr_pipe
->binding_handle
,
403 if (composite_nomem(subreq
, state
->ctx
)) return;
404 tevent_req_set_callback(subreq
, samr_usergroups_recv_groups
, state
);
407 static void samr_usergroups_recv_groups(struct tevent_req
*subreq
)
409 struct samr_getuserdomgroups_state
*state
=
410 tevent_req_callback_data(subreq
,
411 struct samr_getuserdomgroups_state
);
413 state
->ctx
->status
= dcerpc_samr_GetGroupsForUser_r_recv(subreq
, state
);
415 if (!composite_is_ok(state
->ctx
)) return;
416 state
->ctx
->status
= state
->g
.out
.result
;
417 if (!composite_is_ok(state
->ctx
)) return;
419 state
->c
.in
.handle
= state
->user_handle
;
420 state
->c
.out
.handle
= state
->user_handle
;
422 subreq
= dcerpc_samr_Close_r_send(state
,
423 state
->ctx
->event_ctx
,
424 state
->samr_pipe
->binding_handle
,
426 if (composite_nomem(subreq
, state
->ctx
)) return;
427 tevent_req_set_callback(subreq
, samr_usergroups_recv_close
, state
);
430 static void samr_usergroups_recv_close(struct tevent_req
*subreq
)
432 struct samr_getuserdomgroups_state
*state
=
433 tevent_req_callback_data(subreq
,
434 struct samr_getuserdomgroups_state
);
436 state
->ctx
->status
= dcerpc_samr_Close_r_recv(subreq
, state
);
438 if (!composite_is_ok(state
->ctx
)) return;
439 state
->ctx
->status
= state
->c
.out
.result
;
440 if (!composite_is_ok(state
->ctx
)) return;
442 composite_done(state
->ctx
);
445 NTSTATUS
wb_samr_userdomgroups_recv(struct composite_context
*ctx
,
447 uint32_t *num_rids
, uint32_t **rids
)
449 struct samr_getuserdomgroups_state
*state
=
450 talloc_get_type(ctx
->private_data
,
451 struct samr_getuserdomgroups_state
);
454 NTSTATUS status
= composite_wait(ctx
);
455 if (!NT_STATUS_IS_OK(status
)) goto done
;
457 *num_rids
= state
->rid_array
->count
;
458 *rids
= talloc_array(mem_ctx
, uint32_t, *num_rids
);
460 status
= NT_STATUS_NO_MEMORY
;
464 for (i
=0; i
<*num_rids
; i
++) {
465 (*rids
)[i
] = state
->rid_array
->rids
[i
].rid
;