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