1 | /*
|
---|
2 | Unix SMB/CIFS implementation.
|
---|
3 |
|
---|
4 | Copyright (C) Volker Lendecke 2005
|
---|
5 |
|
---|
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.
|
---|
10 |
|
---|
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.
|
---|
15 |
|
---|
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/>.
|
---|
18 | */
|
---|
19 | /*
|
---|
20 | a composite API for finding a DC and its name
|
---|
21 | */
|
---|
22 |
|
---|
23 | #include "includes.h"
|
---|
24 | #include <tevent.h>
|
---|
25 | #include "libcli/composite/composite.h"
|
---|
26 | #include "winbind/wb_async_helpers.h"
|
---|
27 |
|
---|
28 | #include "libcli/security/security.h"
|
---|
29 | #include "librpc/gen_ndr/ndr_lsa_c.h"
|
---|
30 | #include "librpc/gen_ndr/ndr_samr_c.h"
|
---|
31 |
|
---|
32 |
|
---|
33 | struct lsa_lookupsids_state {
|
---|
34 | struct composite_context *ctx;
|
---|
35 | uint32_t num_sids;
|
---|
36 | struct lsa_LookupSids r;
|
---|
37 | struct lsa_SidArray sids;
|
---|
38 | struct lsa_TransNameArray names;
|
---|
39 | struct lsa_RefDomainList *domains;
|
---|
40 | uint32_t count;
|
---|
41 | struct wb_sid_object **result;
|
---|
42 | };
|
---|
43 |
|
---|
44 | static void lsa_lookupsids_recv_names(struct tevent_req *subreq);
|
---|
45 |
|
---|
46 | struct composite_context *wb_lsa_lookupsids_send(TALLOC_CTX *mem_ctx,
|
---|
47 | struct dcerpc_pipe *lsa_pipe,
|
---|
48 | struct policy_handle *handle,
|
---|
49 | uint32_t num_sids,
|
---|
50 | const struct dom_sid **sids)
|
---|
51 | {
|
---|
52 | struct composite_context *result;
|
---|
53 | struct lsa_lookupsids_state *state;
|
---|
54 | uint32_t i;
|
---|
55 | struct tevent_req *subreq;
|
---|
56 |
|
---|
57 | result = composite_create(mem_ctx, lsa_pipe->conn->event_ctx);
|
---|
58 | if (result == NULL) goto failed;
|
---|
59 |
|
---|
60 | state = talloc(result, struct lsa_lookupsids_state);
|
---|
61 | if (state == NULL) goto failed;
|
---|
62 | result->private_data = state;
|
---|
63 | state->ctx = result;
|
---|
64 |
|
---|
65 | state->sids.num_sids = num_sids;
|
---|
66 | state->sids.sids = talloc_array(state, struct lsa_SidPtr, num_sids);
|
---|
67 | if (state->sids.sids == NULL) goto failed;
|
---|
68 |
|
---|
69 | for (i=0; i<num_sids; i++) {
|
---|
70 | state->sids.sids[i].sid = dom_sid_dup(state->sids.sids,
|
---|
71 | sids[i]);
|
---|
72 | if (state->sids.sids[i].sid == NULL) goto failed;
|
---|
73 | }
|
---|
74 |
|
---|
75 | state->domains = talloc(state, struct lsa_RefDomainList);
|
---|
76 | if (state->domains == NULL) goto failed;
|
---|
77 |
|
---|
78 | state->count = 0;
|
---|
79 | state->num_sids = num_sids;
|
---|
80 | state->names.count = 0;
|
---|
81 | state->names.names = NULL;
|
---|
82 |
|
---|
83 | state->r.in.handle = handle;
|
---|
84 | state->r.in.sids = &state->sids;
|
---|
85 | state->r.in.names = &state->names;
|
---|
86 | state->r.in.level = 1;
|
---|
87 | state->r.in.count = &state->count;
|
---|
88 | state->r.out.names = &state->names;
|
---|
89 | state->r.out.count = &state->count;
|
---|
90 | state->r.out.domains = &state->domains;
|
---|
91 |
|
---|
92 | subreq = dcerpc_lsa_LookupSids_r_send(state,
|
---|
93 | result->event_ctx,
|
---|
94 | lsa_pipe->binding_handle,
|
---|
95 | &state->r);
|
---|
96 | if (subreq == NULL) goto failed;
|
---|
97 | tevent_req_set_callback(subreq, lsa_lookupsids_recv_names, state);
|
---|
98 |
|
---|
99 | return result;
|
---|
100 |
|
---|
101 | failed:
|
---|
102 | talloc_free(result);
|
---|
103 | return NULL;
|
---|
104 | }
|
---|
105 |
|
---|
106 | static void lsa_lookupsids_recv_names(struct tevent_req *subreq)
|
---|
107 | {
|
---|
108 | struct lsa_lookupsids_state *state =
|
---|
109 | tevent_req_callback_data(subreq,
|
---|
110 | struct lsa_lookupsids_state);
|
---|
111 | uint32_t i;
|
---|
112 |
|
---|
113 | state->ctx->status = dcerpc_lsa_LookupSids_r_recv(subreq, state);
|
---|
114 | TALLOC_FREE(subreq);
|
---|
115 | if (!composite_is_ok(state->ctx)) return;
|
---|
116 | state->ctx->status = state->r.out.result;
|
---|
117 | if (!NT_STATUS_IS_OK(state->ctx->status) &&
|
---|
118 | !NT_STATUS_EQUAL(state->ctx->status, STATUS_SOME_UNMAPPED)) {
|
---|
119 | composite_error(state->ctx, state->ctx->status);
|
---|
120 | return;
|
---|
121 | }
|
---|
122 |
|
---|
123 | if (state->names.count != state->num_sids) {
|
---|
124 | composite_error(state->ctx,
|
---|
125 | NT_STATUS_INVALID_NETWORK_RESPONSE);
|
---|
126 | return;
|
---|
127 | }
|
---|
128 |
|
---|
129 | state->result = talloc_array(state, struct wb_sid_object *,
|
---|
130 | state->num_sids);
|
---|
131 | if (composite_nomem(state->result, state->ctx)) return;
|
---|
132 |
|
---|
133 | for (i=0; i<state->num_sids; i++) {
|
---|
134 | struct lsa_TranslatedName *name =
|
---|
135 | &state->r.out.names->names[i];
|
---|
136 | struct lsa_DomainInfo *dom;
|
---|
137 | struct lsa_RefDomainList *domains =
|
---|
138 | state->domains;
|
---|
139 |
|
---|
140 | state->result[i] = talloc_zero(state->result,
|
---|
141 | struct wb_sid_object);
|
---|
142 | if (composite_nomem(state->result[i], state->ctx)) return;
|
---|
143 |
|
---|
144 | state->result[i]->type = name->sid_type;
|
---|
145 | if (state->result[i]->type == SID_NAME_UNKNOWN) {
|
---|
146 | continue;
|
---|
147 | }
|
---|
148 |
|
---|
149 | if (domains == NULL) {
|
---|
150 | composite_error(state->ctx,
|
---|
151 | NT_STATUS_INVALID_NETWORK_RESPONSE);
|
---|
152 | return;
|
---|
153 | }
|
---|
154 | if (name->sid_index >= domains->count) {
|
---|
155 | composite_error(state->ctx,
|
---|
156 | NT_STATUS_INVALID_NETWORK_RESPONSE);
|
---|
157 | return;
|
---|
158 | }
|
---|
159 |
|
---|
160 | dom = &domains->domains[name->sid_index];
|
---|
161 | state->result[i]->domain = talloc_reference(state->result[i],
|
---|
162 | dom->name.string);
|
---|
163 | if ((name->sid_type == SID_NAME_DOMAIN) ||
|
---|
164 | (name->name.string == NULL)) {
|
---|
165 | state->result[i]->name =
|
---|
166 | talloc_strdup(state->result[i], "");
|
---|
167 | } else {
|
---|
168 | state->result[i]->name =
|
---|
169 | talloc_steal(state->result[i],
|
---|
170 | name->name.string);
|
---|
171 | }
|
---|
172 |
|
---|
173 | if (composite_nomem(state->result[i]->name, state->ctx)) {
|
---|
174 | return;
|
---|
175 | }
|
---|
176 | }
|
---|
177 |
|
---|
178 | composite_done(state->ctx);
|
---|
179 | }
|
---|
180 |
|
---|
181 | NTSTATUS wb_lsa_lookupsids_recv(struct composite_context *c,
|
---|
182 | TALLOC_CTX *mem_ctx,
|
---|
183 | struct wb_sid_object ***names)
|
---|
184 | {
|
---|
185 | NTSTATUS status = composite_wait(c);
|
---|
186 | if (NT_STATUS_IS_OK(status)) {
|
---|
187 | struct lsa_lookupsids_state *state =
|
---|
188 | talloc_get_type(c->private_data,
|
---|
189 | struct lsa_lookupsids_state);
|
---|
190 | *names = talloc_steal(mem_ctx, state->result);
|
---|
191 | }
|
---|
192 | talloc_free(c);
|
---|
193 | return status;
|
---|
194 | }
|
---|
195 |
|
---|
196 |
|
---|
197 | struct lsa_lookupnames_state {
|
---|
198 | struct composite_context *ctx;
|
---|
199 | uint32_t num_names;
|
---|
200 | struct lsa_LookupNames r;
|
---|
201 | struct lsa_TransSidArray sids;
|
---|
202 | struct lsa_RefDomainList *domains;
|
---|
203 | uint32_t count;
|
---|
204 | struct wb_sid_object **result;
|
---|
205 | };
|
---|
206 |
|
---|
207 | static void lsa_lookupnames_recv_sids(struct tevent_req *subreq);
|
---|
208 |
|
---|
209 | struct composite_context *wb_lsa_lookupnames_send(TALLOC_CTX *mem_ctx,
|
---|
210 | struct dcerpc_pipe *lsa_pipe,
|
---|
211 | struct policy_handle *handle,
|
---|
212 | uint32_t num_names,
|
---|
213 | const char **names)
|
---|
214 | {
|
---|
215 | struct composite_context *result;
|
---|
216 | struct lsa_lookupnames_state *state;
|
---|
217 | struct tevent_req *subreq;
|
---|
218 |
|
---|
219 | struct lsa_String *lsa_names;
|
---|
220 | uint32_t i;
|
---|
221 |
|
---|
222 | result = composite_create(mem_ctx, lsa_pipe->conn->event_ctx);
|
---|
223 | if (result == NULL) goto failed;
|
---|
224 |
|
---|
225 | state = talloc(result, struct lsa_lookupnames_state);
|
---|
226 | if (state == NULL) goto failed;
|
---|
227 | result->private_data = state;
|
---|
228 | state->ctx = result;
|
---|
229 |
|
---|
230 | state->sids.count = 0;
|
---|
231 | state->sids.sids = NULL;
|
---|
232 | state->num_names = num_names;
|
---|
233 | state->count = 0;
|
---|
234 |
|
---|
235 | lsa_names = talloc_array(state, struct lsa_String, num_names);
|
---|
236 | if (lsa_names == NULL) goto failed;
|
---|
237 |
|
---|
238 | for (i=0; i<num_names; i++) {
|
---|
239 | lsa_names[i].string = names[i];
|
---|
240 | }
|
---|
241 |
|
---|
242 | state->domains = talloc(state, struct lsa_RefDomainList);
|
---|
243 | if (state->domains == NULL) goto failed;
|
---|
244 |
|
---|
245 | state->r.in.handle = handle;
|
---|
246 | state->r.in.num_names = num_names;
|
---|
247 | state->r.in.names = lsa_names;
|
---|
248 | state->r.in.sids = &state->sids;
|
---|
249 | state->r.in.level = 1;
|
---|
250 | state->r.in.count = &state->count;
|
---|
251 | state->r.out.count = &state->count;
|
---|
252 | state->r.out.sids = &state->sids;
|
---|
253 | state->r.out.domains = &state->domains;
|
---|
254 |
|
---|
255 | subreq = dcerpc_lsa_LookupNames_r_send(state,
|
---|
256 | result->event_ctx,
|
---|
257 | lsa_pipe->binding_handle,
|
---|
258 | &state->r);
|
---|
259 | if (subreq == NULL) goto failed;
|
---|
260 | tevent_req_set_callback(subreq, lsa_lookupnames_recv_sids, state);
|
---|
261 |
|
---|
262 | return result;
|
---|
263 |
|
---|
264 | failed:
|
---|
265 | talloc_free(result);
|
---|
266 | return NULL;
|
---|
267 | }
|
---|
268 |
|
---|
269 | static void lsa_lookupnames_recv_sids(struct tevent_req *subreq)
|
---|
270 | {
|
---|
271 | struct lsa_lookupnames_state *state =
|
---|
272 | tevent_req_callback_data(subreq,
|
---|
273 | struct lsa_lookupnames_state);
|
---|
274 | uint32_t i;
|
---|
275 |
|
---|
276 | state->ctx->status = dcerpc_lsa_LookupNames_r_recv(subreq, state);
|
---|
277 | TALLOC_FREE(subreq);
|
---|
278 | if (!composite_is_ok(state->ctx)) return;
|
---|
279 | state->ctx->status = state->r.out.result;
|
---|
280 | if (!NT_STATUS_IS_OK(state->ctx->status) &&
|
---|
281 | !NT_STATUS_EQUAL(state->ctx->status, STATUS_SOME_UNMAPPED)) {
|
---|
282 | composite_error(state->ctx, state->ctx->status);
|
---|
283 | return;
|
---|
284 | }
|
---|
285 |
|
---|
286 | if (state->sids.count != state->num_names) {
|
---|
287 | composite_error(state->ctx,
|
---|
288 | NT_STATUS_INVALID_NETWORK_RESPONSE);
|
---|
289 | return;
|
---|
290 | }
|
---|
291 |
|
---|
292 | state->result = talloc_array(state, struct wb_sid_object *,
|
---|
293 | state->num_names);
|
---|
294 | if (composite_nomem(state->result, state->ctx)) return;
|
---|
295 |
|
---|
296 | for (i=0; i<state->num_names; i++) {
|
---|
297 | struct lsa_TranslatedSid *sid = &state->r.out.sids->sids[i];
|
---|
298 | struct lsa_RefDomainList *domains = state->domains;
|
---|
299 | struct lsa_DomainInfo *dom;
|
---|
300 |
|
---|
301 | state->result[i] = talloc_zero(state->result,
|
---|
302 | struct wb_sid_object);
|
---|
303 | if (composite_nomem(state->result[i], state->ctx)) return;
|
---|
304 |
|
---|
305 | state->result[i]->type = sid->sid_type;
|
---|
306 | if (state->result[i]->type == SID_NAME_UNKNOWN) {
|
---|
307 | continue;
|
---|
308 | }
|
---|
309 |
|
---|
310 | if (domains == NULL) {
|
---|
311 | composite_error(state->ctx,
|
---|
312 | NT_STATUS_INVALID_NETWORK_RESPONSE);
|
---|
313 | return;
|
---|
314 | }
|
---|
315 | if (sid->sid_index >= domains->count) {
|
---|
316 | composite_error(state->ctx,
|
---|
317 | NT_STATUS_INVALID_NETWORK_RESPONSE);
|
---|
318 | return;
|
---|
319 | }
|
---|
320 |
|
---|
321 | dom = &domains->domains[sid->sid_index];
|
---|
322 |
|
---|
323 | state->result[i]->sid = dom_sid_add_rid(state->result[i],
|
---|
324 | dom->sid, sid->rid);
|
---|
325 | }
|
---|
326 |
|
---|
327 | composite_done(state->ctx);
|
---|
328 | }
|
---|
329 |
|
---|
330 | NTSTATUS wb_lsa_lookupnames_recv(struct composite_context *c,
|
---|
331 | TALLOC_CTX *mem_ctx,
|
---|
332 | struct wb_sid_object ***sids)
|
---|
333 | {
|
---|
334 | NTSTATUS status = composite_wait(c);
|
---|
335 | if (NT_STATUS_IS_OK(status)) {
|
---|
336 | struct lsa_lookupnames_state *state =
|
---|
337 | talloc_get_type(c->private_data,
|
---|
338 | struct lsa_lookupnames_state);
|
---|
339 | *sids = talloc_steal(mem_ctx, state->result);
|
---|
340 | }
|
---|
341 | talloc_free(c);
|
---|
342 | return status;
|
---|
343 | }
|
---|
344 | struct samr_getuserdomgroups_state {
|
---|
345 | struct composite_context *ctx;
|
---|
346 | struct dcerpc_pipe *samr_pipe;
|
---|
347 |
|
---|
348 | uint32_t num_rids;
|
---|
349 | uint32_t *rids;
|
---|
350 |
|
---|
351 | struct samr_RidWithAttributeArray *rid_array;
|
---|
352 |
|
---|
353 | struct policy_handle *user_handle;
|
---|
354 | struct samr_OpenUser o;
|
---|
355 | struct samr_GetGroupsForUser g;
|
---|
356 | struct samr_Close c;
|
---|
357 | };
|
---|
358 |
|
---|
359 | static void samr_usergroups_recv_open(struct tevent_req *subreq);
|
---|
360 | static void samr_usergroups_recv_groups(struct tevent_req *subreq);
|
---|
361 | static void samr_usergroups_recv_close(struct tevent_req *subreq);
|
---|
362 |
|
---|
363 | struct composite_context *wb_samr_userdomgroups_send(TALLOC_CTX *mem_ctx,
|
---|
364 | struct dcerpc_pipe *samr_pipe,
|
---|
365 | struct policy_handle *domain_handle,
|
---|
366 | uint32_t rid)
|
---|
367 | {
|
---|
368 | struct composite_context *result;
|
---|
369 | struct samr_getuserdomgroups_state *state;
|
---|
370 | struct tevent_req *subreq;
|
---|
371 |
|
---|
372 | result = composite_create(mem_ctx, samr_pipe->conn->event_ctx);
|
---|
373 | if (result == NULL) goto failed;
|
---|
374 |
|
---|
375 | state = talloc(result, struct samr_getuserdomgroups_state);
|
---|
376 | if (state == NULL) goto failed;
|
---|
377 | result->private_data = state;
|
---|
378 | state->ctx = result;
|
---|
379 |
|
---|
380 | state->samr_pipe = samr_pipe;
|
---|
381 |
|
---|
382 | state->user_handle = talloc(state, struct policy_handle);
|
---|
383 | if (state->user_handle == NULL) goto failed;
|
---|
384 |
|
---|
385 | state->o.in.domain_handle = domain_handle;
|
---|
386 | state->o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
|
---|
387 | state->o.in.rid = rid;
|
---|
388 | state->o.out.user_handle = state->user_handle;
|
---|
389 |
|
---|
390 | subreq = dcerpc_samr_OpenUser_r_send(state,
|
---|
391 | result->event_ctx,
|
---|
392 | state->samr_pipe->binding_handle,
|
---|
393 | &state->o);
|
---|
394 | if (subreq == NULL) goto failed;
|
---|
395 | tevent_req_set_callback(subreq, samr_usergroups_recv_open, state);
|
---|
396 |
|
---|
397 | return result;
|
---|
398 |
|
---|
399 | failed:
|
---|
400 | talloc_free(result);
|
---|
401 | return NULL;
|
---|
402 | }
|
---|
403 |
|
---|
404 | static void samr_usergroups_recv_open(struct tevent_req *subreq)
|
---|
405 | {
|
---|
406 | struct samr_getuserdomgroups_state *state =
|
---|
407 | tevent_req_callback_data(subreq,
|
---|
408 | struct samr_getuserdomgroups_state);
|
---|
409 |
|
---|
410 | state->ctx->status = dcerpc_samr_OpenUser_r_recv(subreq, state);
|
---|
411 | TALLOC_FREE(subreq);
|
---|
412 | if (!composite_is_ok(state->ctx)) return;
|
---|
413 | state->ctx->status = state->o.out.result;
|
---|
414 | if (!composite_is_ok(state->ctx)) return;
|
---|
415 |
|
---|
416 | state->g.in.user_handle = state->user_handle;
|
---|
417 | state->g.out.rids = &state->rid_array;
|
---|
418 |
|
---|
419 | subreq = dcerpc_samr_GetGroupsForUser_r_send(state,
|
---|
420 | state->ctx->event_ctx,
|
---|
421 | state->samr_pipe->binding_handle,
|
---|
422 | &state->g);
|
---|
423 | if (composite_nomem(subreq, state->ctx)) return;
|
---|
424 | tevent_req_set_callback(subreq, samr_usergroups_recv_groups, state);
|
---|
425 | }
|
---|
426 |
|
---|
427 | static void samr_usergroups_recv_groups(struct tevent_req *subreq)
|
---|
428 | {
|
---|
429 | struct samr_getuserdomgroups_state *state =
|
---|
430 | tevent_req_callback_data(subreq,
|
---|
431 | struct samr_getuserdomgroups_state);
|
---|
432 |
|
---|
433 | state->ctx->status = dcerpc_samr_GetGroupsForUser_r_recv(subreq, state);
|
---|
434 | TALLOC_FREE(subreq);
|
---|
435 | if (!composite_is_ok(state->ctx)) return;
|
---|
436 | state->ctx->status = state->g.out.result;
|
---|
437 | if (!composite_is_ok(state->ctx)) return;
|
---|
438 |
|
---|
439 | state->c.in.handle = state->user_handle;
|
---|
440 | state->c.out.handle = state->user_handle;
|
---|
441 |
|
---|
442 | subreq = dcerpc_samr_Close_r_send(state,
|
---|
443 | state->ctx->event_ctx,
|
---|
444 | state->samr_pipe->binding_handle,
|
---|
445 | &state->c);
|
---|
446 | if (composite_nomem(subreq, state->ctx)) return;
|
---|
447 | tevent_req_set_callback(subreq, samr_usergroups_recv_close, state);
|
---|
448 | }
|
---|
449 |
|
---|
450 | static void samr_usergroups_recv_close(struct tevent_req *subreq)
|
---|
451 | {
|
---|
452 | struct samr_getuserdomgroups_state *state =
|
---|
453 | tevent_req_callback_data(subreq,
|
---|
454 | struct samr_getuserdomgroups_state);
|
---|
455 |
|
---|
456 | state->ctx->status = dcerpc_samr_Close_r_recv(subreq, state);
|
---|
457 | TALLOC_FREE(subreq);
|
---|
458 | if (!composite_is_ok(state->ctx)) return;
|
---|
459 | state->ctx->status = state->c.out.result;
|
---|
460 | if (!composite_is_ok(state->ctx)) return;
|
---|
461 |
|
---|
462 | composite_done(state->ctx);
|
---|
463 | }
|
---|
464 |
|
---|
465 | NTSTATUS wb_samr_userdomgroups_recv(struct composite_context *ctx,
|
---|
466 | TALLOC_CTX *mem_ctx,
|
---|
467 | uint32_t *num_rids, uint32_t **rids)
|
---|
468 | {
|
---|
469 | struct samr_getuserdomgroups_state *state =
|
---|
470 | talloc_get_type(ctx->private_data,
|
---|
471 | struct samr_getuserdomgroups_state);
|
---|
472 |
|
---|
473 | uint32_t i;
|
---|
474 | NTSTATUS status = composite_wait(ctx);
|
---|
475 | if (!NT_STATUS_IS_OK(status)) goto done;
|
---|
476 |
|
---|
477 | *num_rids = state->rid_array->count;
|
---|
478 | *rids = talloc_array(mem_ctx, uint32_t, *num_rids);
|
---|
479 | if (*rids == NULL) {
|
---|
480 | status = NT_STATUS_NO_MEMORY;
|
---|
481 | goto done;
|
---|
482 | }
|
---|
483 |
|
---|
484 | for (i=0; i<*num_rids; i++) {
|
---|
485 | (*rids)[i] = state->rid_array->rids[i].rid;
|
---|
486 | }
|
---|
487 |
|
---|
488 | done:
|
---|
489 | talloc_free(ctx);
|
---|
490 | return status;
|
---|
491 | }
|
---|