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 2 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, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 a composite API for finding a DC and its name
25 #include "libcli/composite/composite.h"
26 #include "libcli/smb_composite/smb_composite.h"
27 #include "winbind/wb_async_helpers.h"
28 #include "winbind/wb_server.h"
29 #include "smbd/service_stream.h"
31 #include "librpc/gen_ndr/nbt.h"
32 #include "librpc/gen_ndr/samr.h"
33 #include "lib/messaging/irpc.h"
34 #include "librpc/gen_ndr/irpc.h"
35 #include "librpc/gen_ndr/ndr_irpc.h"
36 #include "libcli/raw/libcliraw.h"
37 #include "librpc/gen_ndr/ndr_netlogon.h"
38 #include "librpc/gen_ndr/ndr_lsa.h"
39 #include "libcli/auth/credentials.h"
41 struct finddcs_state {
42 struct composite_context *ctx;
43 struct messaging_context *msg_ctx;
45 const char *domain_name;
46 const struct dom_sid *domain_sid;
48 struct nbtd_getdcname r;
51 struct nbt_dc_name *dcs;
54 static void finddcs_resolve(struct composite_context *ctx);
55 static void finddcs_getdc(struct irpc_request *ireq);
57 struct composite_context *wb_finddcs_send(const char *domain_name,
58 const struct dom_sid *domain_sid,
59 struct event_context *event_ctx,
60 struct messaging_context *msg_ctx)
62 struct composite_context *result, *ctx;
63 struct finddcs_state *state;
66 result = talloc_zero(NULL, struct composite_context);
67 if (result == NULL) goto failed;
68 result->state = COMPOSITE_STATE_IN_PROGRESS;
69 result->async.fn = NULL;
70 result->event_ctx = event_ctx;
72 state = talloc(result, struct finddcs_state);
73 if (state == NULL) goto failed;
75 result->private_data = state;
77 state->domain_name = talloc_strdup(state, domain_name);
78 if (state->domain_name == NULL) goto failed;
79 state->domain_sid = dom_sid_dup(state, domain_sid);
80 if (state->domain_sid == NULL) goto failed;
81 state->msg_ctx = msg_ctx;
83 make_nbt_name(&name, state->domain_name, 0x1c);
84 ctx = resolve_name_send(&name, result->event_ctx,
85 lp_name_resolve_order());
87 if (ctx == NULL) goto failed;
88 ctx->async.fn = finddcs_resolve;
89 ctx->async.private_data = state;
98 static void finddcs_resolve(struct composite_context *ctx)
100 struct finddcs_state *state =
101 talloc_get_type(ctx->async.private_data, struct finddcs_state);
102 struct irpc_request *ireq;
103 uint32_t *nbt_servers;
106 state->ctx->status = resolve_name_recv(ctx, state, &address);
107 if (!composite_is_ok(state->ctx)) return;
110 state->dcs = talloc_array(state, struct nbt_dc_name, state->num_dcs);
111 if (composite_nomem(state->dcs, state->ctx)) return;
113 state->dcs[0].address = talloc_steal(state->dcs, address);
115 nbt_servers = irpc_servers_byname(state->msg_ctx, "nbt_server");
116 if ((nbt_servers == NULL) || (nbt_servers[0] == 0)) {
117 composite_error(state->ctx, NT_STATUS_NO_LOGON_SERVERS);
121 state->r.in.domainname = state->domain_name;
122 state->r.in.ip_address = state->dcs[0].address;
123 state->r.in.my_computername = lp_netbios_name();
124 state->r.in.my_accountname = talloc_asprintf(state, "%s$",
126 if (composite_nomem(state->r.in.my_accountname, state->ctx)) return;
127 state->r.in.account_control = ACB_WSTRUST;
128 state->r.in.domain_sid = dom_sid_dup(state, state->domain_sid);
129 if (composite_nomem(state->r.in.domain_sid, state->ctx)) return;
131 ireq = irpc_call_send(state->msg_ctx, nbt_servers[0],
132 &dcerpc_table_irpc, DCERPC_NBTD_GETDCNAME,
134 composite_continue_irpc(state->ctx, ireq, finddcs_getdc, state);
137 static void finddcs_getdc(struct irpc_request *ireq)
139 struct finddcs_state *state =
140 talloc_get_type(ireq->async.private, struct finddcs_state);
142 state->ctx->status = irpc_call_recv(ireq);
144 if (!composite_is_ok(state->ctx)) return;
146 state->dcs[0].name = talloc_steal(state->dcs, state->r.out.dcname);
147 composite_done(state->ctx);
150 NTSTATUS wb_finddcs_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
151 int *num_dcs, struct nbt_dc_name **dcs)
153 NTSTATUS status =composite_wait(c);
154 if (NT_STATUS_IS_OK(status)) {
155 struct finddcs_state *state =
156 talloc_get_type(c->private_data, struct finddcs_state);
157 *num_dcs = state->num_dcs;
158 *dcs = talloc_steal(mem_ctx, state->dcs);
164 NTSTATUS wb_finddcs(const char *domain_name, const struct dom_sid *domain_sid,
165 struct event_context *event_ctx,
166 struct messaging_context *msg_ctx,
168 int *num_dcs, struct nbt_dc_name **dcs)
170 struct composite_context *c = wb_finddcs_send(domain_name, domain_sid,
172 return wb_finddcs_recv(c, mem_ctx, num_dcs, dcs);
175 struct get_schannel_creds_state {
176 struct composite_context *ctx;
177 struct cli_credentials *wks_creds;
178 struct dcerpc_pipe *p;
179 struct netr_ServerReqChallenge r;
181 struct creds_CredentialState *creds_state;
182 struct netr_Credential netr_cred;
183 uint32_t negotiate_flags;
184 struct netr_ServerAuthenticate2 a;
187 static void get_schannel_creds_recv_auth(struct rpc_request *req);
188 static void get_schannel_creds_recv_chal(struct rpc_request *req);
189 static void get_schannel_creds_recv_pipe(struct composite_context *ctx);
191 struct composite_context *wb_get_schannel_creds_send(struct cli_credentials *wks_creds,
192 struct smbcli_tree *tree,
193 struct event_context *ev)
195 struct composite_context *result, *ctx;
196 struct get_schannel_creds_state *state;
198 result = talloc_zero(NULL, struct composite_context);
199 if (result == NULL) goto failed;
200 result->state = COMPOSITE_STATE_IN_PROGRESS;
201 result->async.fn = NULL;
202 result->event_ctx = ev;
204 state = talloc(result, struct get_schannel_creds_state);
205 if (state == NULL) goto failed;
206 result->private_data = state;
209 state->wks_creds = wks_creds;
211 state->p = dcerpc_pipe_init(state, ev);
212 if (state->p == NULL) goto failed;
214 ctx = dcerpc_pipe_open_smb_send(state->p->conn, tree, "\\netlogon");
215 if (ctx == NULL) goto failed;
217 ctx->async.fn = get_schannel_creds_recv_pipe;
218 ctx->async.private_data = state;
226 static void get_schannel_creds_recv_pipe(struct composite_context *ctx)
228 struct get_schannel_creds_state *state =
229 talloc_get_type(ctx->async.private_data,
230 struct get_schannel_creds_state);
231 struct rpc_request *req;
233 state->ctx->status = dcerpc_pipe_open_smb_recv(ctx);
234 if (!composite_is_ok(state->ctx)) return;
236 state->ctx->status = dcerpc_bind_auth_none(state->p,
237 DCERPC_NETLOGON_UUID,
238 DCERPC_NETLOGON_VERSION);
239 if (!composite_is_ok(state->ctx)) return;
241 state->r.in.computer_name =
242 cli_credentials_get_workstation(state->wks_creds);
243 state->r.in.server_name =
244 talloc_asprintf(state, "\\\\%s",
245 dcerpc_server_name(state->p));
246 if (composite_nomem(state->r.in.server_name, state->ctx)) return;
248 state->r.in.credentials = talloc(state, struct netr_Credential);
249 if (composite_nomem(state->r.in.credentials, state->ctx)) return;
251 state->r.out.credentials = talloc(state, struct netr_Credential);
252 if (composite_nomem(state->r.out.credentials, state->ctx)) return;
254 generate_random_buffer(state->r.in.credentials->data,
255 sizeof(state->r.in.credentials->data));
257 req = dcerpc_netr_ServerReqChallenge_send(state->p, state, &state->r);
258 composite_continue_rpc(state->ctx, req,
259 get_schannel_creds_recv_chal, state);
262 static void get_schannel_creds_recv_chal(struct rpc_request *req)
264 struct get_schannel_creds_state *state =
265 talloc_get_type(req->async.private,
266 struct get_schannel_creds_state);
267 const struct samr_Password *mach_pwd;
269 state->ctx->status = dcerpc_ndr_request_recv(req);
270 if (!composite_is_ok(state->ctx)) return;
271 state->ctx->status = state->r.out.result;
272 if (!composite_is_ok(state->ctx)) return;
274 state->creds_state = talloc(state, struct creds_CredentialState);
275 if (composite_nomem(state->creds_state, state->ctx)) return;
277 mach_pwd = cli_credentials_get_nt_hash(state->wks_creds, state);
278 if (composite_nomem(mach_pwd, state->ctx)) return;
280 state->negotiate_flags = NETLOGON_NEG_AUTH2_FLAGS;
282 creds_client_init(state->creds_state, state->r.in.credentials,
283 state->r.out.credentials, mach_pwd,
284 &state->netr_cred, state->negotiate_flags);
286 state->a.in.server_name =
287 talloc_reference(state, state->r.in.server_name);
288 state->a.in.account_name =
289 cli_credentials_get_username(state->wks_creds);
290 state->a.in.secure_channel_type =
291 cli_credentials_get_secure_channel_type(state->wks_creds);
292 state->a.in.computer_name =
293 cli_credentials_get_workstation(state->wks_creds);
294 state->a.in.negotiate_flags = &state->negotiate_flags;
295 state->a.out.negotiate_flags = &state->negotiate_flags;
296 state->a.in.credentials = &state->netr_cred;
297 state->a.out.credentials = &state->netr_cred;
299 req = dcerpc_netr_ServerAuthenticate2_send(state->p, state, &state->a);
300 composite_continue_rpc(state->ctx, req,
301 get_schannel_creds_recv_auth, state);
304 static void get_schannel_creds_recv_auth(struct rpc_request *req)
306 struct get_schannel_creds_state *state =
307 talloc_get_type(req->async.private,
308 struct get_schannel_creds_state);
310 state->ctx->status = dcerpc_ndr_request_recv(req);
311 if (!NT_STATUS_IS_OK(state->ctx->status)) goto done;
312 state->ctx->status = state->a.out.result;
313 if (!NT_STATUS_IS_OK(state->ctx->status)) goto done;
315 if (!creds_client_check(state->creds_state,
316 state->a.out.credentials)) {
317 DEBUG(5, ("Server got us invalid creds\n"));
318 state->ctx->status = NT_STATUS_UNSUCCESSFUL;
322 cli_credentials_set_netlogon_creds(state->wks_creds,
325 state->ctx->state = COMPOSITE_STATE_DONE;
328 if (!NT_STATUS_IS_OK(state->ctx->status)) {
329 state->ctx->state = COMPOSITE_STATE_ERROR;
331 if ((state->ctx->state >= COMPOSITE_STATE_DONE) &&
332 (state->ctx->async.fn != NULL)) {
333 state->ctx->async.fn(state->ctx);
337 NTSTATUS wb_get_schannel_creds_recv(struct composite_context *c,
339 struct dcerpc_pipe **netlogon_pipe)
341 NTSTATUS status = composite_wait(c);
342 if (NT_STATUS_IS_OK(status)) {
343 struct get_schannel_creds_state *state =
344 talloc_get_type(c->private_data,
345 struct get_schannel_creds_state);
346 *netlogon_pipe = talloc_steal(mem_ctx, state->p);
352 NTSTATUS wb_get_schannel_creds(struct cli_credentials *wks_creds,
353 struct smbcli_tree *tree,
354 struct event_context *event_ctx,
356 struct dcerpc_pipe **netlogon_pipe)
358 struct composite_context *c =
359 wb_get_schannel_creds_send(wks_creds, tree, event_ctx);
360 return wb_get_schannel_creds_recv(c, mem_ctx, netlogon_pipe);
363 struct lsa_lookupnames_state {
364 struct composite_context *ctx;
366 struct lsa_LookupNames r;
367 struct lsa_TransSidArray sids;
369 struct wb_sid_object **result;
372 static void lsa_lookupnames_recv_sids(struct rpc_request *req);
374 struct composite_context *wb_lsa_lookupnames_send(struct dcerpc_pipe *lsa_pipe,
375 struct policy_handle *handle,
379 struct composite_context *result;
380 struct rpc_request *req;
381 struct lsa_lookupnames_state *state;
383 struct lsa_String *lsa_names;
386 result = talloc_zero(NULL, struct composite_context);
387 if (result == NULL) goto failed;
388 result->state = COMPOSITE_STATE_IN_PROGRESS;
389 result->async.fn = NULL;
390 result->event_ctx = lsa_pipe->conn->event_ctx;
392 state = talloc(result, struct lsa_lookupnames_state);
393 if (state == NULL) goto failed;
394 result->private_data = state;
397 state->sids.count = 0;
398 state->sids.sids = NULL;
399 state->num_names = num_names;
402 lsa_names = talloc_array(state, struct lsa_String, num_names);
403 if (lsa_names == NULL) goto failed;
405 for (i=0; i<num_names; i++) {
406 lsa_names[i].string = names[i];
409 state->r.in.handle = handle;
410 state->r.in.num_names = num_names;
411 state->r.in.names = lsa_names;
412 state->r.in.sids = &state->sids;
413 state->r.in.level = 1;
414 state->r.in.count = &state->count;
415 state->r.out.count = &state->count;
416 state->r.out.sids = &state->sids;
418 req = dcerpc_lsa_LookupNames_send(lsa_pipe, state, &state->r);
419 if (req == NULL) goto failed;
421 req->async.callback = lsa_lookupnames_recv_sids;
422 req->async.private = state;
430 static void lsa_lookupnames_recv_sids(struct rpc_request *req)
432 struct lsa_lookupnames_state *state =
433 talloc_get_type(req->async.private,
434 struct lsa_lookupnames_state);
437 state->ctx->status = dcerpc_ndr_request_recv(req);
438 if (!composite_is_ok(state->ctx)) return;
439 state->ctx->status = state->r.out.result;
440 if (!NT_STATUS_IS_OK(state->ctx->status) &&
441 !NT_STATUS_EQUAL(state->ctx->status, STATUS_SOME_UNMAPPED)) {
442 composite_error(state->ctx, state->ctx->status);
446 state->result = talloc_array(state, struct wb_sid_object *,
448 if (composite_nomem(state->result, state->ctx)) return;
450 for (i=0; i<state->num_names; i++) {
451 struct lsa_TranslatedSid *sid = &state->r.out.sids->sids[i];
452 struct lsa_TrustInformation *dom;
454 state->result[i] = talloc_zero(state->result,
455 struct wb_sid_object);
456 if (composite_nomem(state->result[i], state->ctx)) return;
458 state->result[i]->type = sid->sid_type;
459 if (state->result[i]->type == SID_NAME_UNKNOWN) {
463 if (sid->sid_index >= state->r.out.domains->count) {
464 composite_error(state->ctx,
465 NT_STATUS_INVALID_PARAMETER);
469 dom = &state->r.out.domains->domains[sid->sid_index];
471 state->result[i]->sid = dom_sid_add_rid(state->result[i],
475 composite_done(state->ctx);
478 NTSTATUS wb_lsa_lookupnames_recv(struct composite_context *c,
480 struct wb_sid_object ***sids)
482 NTSTATUS status = composite_wait(c);
483 if (NT_STATUS_IS_OK(status)) {
484 struct lsa_lookupnames_state *state =
485 talloc_get_type(c->private_data,
486 struct lsa_lookupnames_state);
487 *sids = talloc_steal(mem_ctx, state->result);
493 NTSTATUS wb_lsa_lookupnames(struct dcerpc_pipe *lsa_pipe,
494 struct policy_handle *handle,
495 int num_names, const char **names,
497 struct wb_sid_object ***sids)
499 struct composite_context *c =
500 wb_lsa_lookupnames_send(lsa_pipe, handle, num_names, names);
501 return wb_lsa_lookupnames_recv(c, mem_ctx, sids);
504 struct cmd_checkmachacc_state {
505 struct composite_context *ctx;
506 struct wbsrv_call *call;
507 struct wbsrv_domain *domain;
510 static void cmd_checkmachacc_recv_init(struct composite_context *ctx);
512 struct composite_context *wb_cmd_checkmachacc_send(struct wbsrv_call *call)
514 struct composite_context *result, *ctx;
515 struct cmd_checkmachacc_state *state;
516 struct wbsrv_service *service = call->wbconn->listen_socket->service;
518 result = talloc(call, struct composite_context);
519 if (result == NULL) goto failed;
520 result->state = COMPOSITE_STATE_IN_PROGRESS;
521 result->async.fn = NULL;
522 result->event_ctx = call->event_ctx;
524 state = talloc(result, struct cmd_checkmachacc_state);
525 if (state == NULL) goto failed;
527 result->private_data = state;
530 state->domain = service->domains;
532 ctx = wb_init_domain_send(state->domain, result->event_ctx,
533 call->wbconn->conn->msg_ctx);
534 if (ctx == NULL) goto failed;
535 ctx->async.fn = cmd_checkmachacc_recv_init;
536 ctx->async.private_data = state;
545 static void cmd_checkmachacc_recv_init(struct composite_context *ctx)
547 struct cmd_checkmachacc_state *state =
548 talloc_get_type(ctx->async.private_data,
549 struct cmd_checkmachacc_state);
551 state->ctx->status = wb_init_domain_recv(ctx);
552 if (!composite_is_ok(state->ctx)) return;
554 composite_done(state->ctx);
557 NTSTATUS wb_cmd_checkmachacc_recv(struct composite_context *c)
559 NTSTATUS status = composite_wait(c);
564 NTSTATUS wb_cmd_checkmachacc(struct wbsrv_call *call)
566 struct composite_context *c = wb_cmd_checkmachacc_send(call);
567 return wb_cmd_checkmachacc_recv(c);
570 static void composite_netr_LogonSamLogon_recv_rpc(struct rpc_request *req);
572 struct composite_context *composite_netr_LogonSamLogon_send(struct dcerpc_pipe *p,
574 struct netr_LogonSamLogon *r)
576 struct composite_context *result;
577 struct rpc_request *req;
579 result = talloc(mem_ctx, struct composite_context);
580 if (result == NULL) goto failed;
581 result->state = COMPOSITE_STATE_IN_PROGRESS;
582 result->async.fn = NULL;
583 result->event_ctx = p->conn->event_ctx;
585 req = dcerpc_netr_LogonSamLogon_send(p, mem_ctx, r);
586 if (req == NULL) goto failed;
587 req->async.callback = composite_netr_LogonSamLogon_recv_rpc;
588 req->async.private = result;
596 static void composite_netr_LogonSamLogon_recv_rpc(struct rpc_request *req)
598 struct composite_context *ctx =
599 talloc_get_type(req->async.private, struct composite_context);
601 ctx->status = dcerpc_ndr_request_recv(req);
602 if (!composite_is_ok(ctx)) return;
606 NTSTATUS composite_netr_LogonSamLogon_recv(struct composite_context *ctx)
608 NTSTATUS status = composite_wait(ctx);