2 Unix SMB/CIFS implementation.
4 Copyright (C) Stefan Metzmacher 2004
5 Copyright (C) Rafal Szczesniak 2005
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include "libnet/libnet.h"
24 #include "libcli/libcli.h"
25 #include "libcli/composite/composite.h"
26 #include "librpc/gen_ndr/ndr_lsa_c.h"
29 struct rpc_connect_srv_state {
30 struct libnet_RpcConnect r;
35 static void continue_pipe_connect(struct composite_context *ctx);
39 * Initiates connection to rpc pipe on remote server
41 * @param ctx initialised libnet context
42 * @param mem_ctx memory context of this call
43 * @param r data structure containing necessary parameters and return values
44 * @return nt status of the call
47 static struct composite_context* libnet_RpcConnectSrv_send(struct libnet_context *ctx,
49 struct libnet_RpcConnect *r)
51 struct composite_context *c;
52 struct rpc_connect_srv_state *s;
53 struct composite_context *pipe_connect_req;
55 c = talloc_zero(mem_ctx, struct composite_context);
56 if (c == NULL) return NULL;
58 s = talloc_zero(c, struct rpc_connect_srv_state);
59 if (composite_nomem(s, c)) return c;
61 c->state = COMPOSITE_STATE_IN_PROGRESS;
63 c->event_ctx = ctx->event_ctx;
67 /* prepare binding string */
69 case LIBNET_RPC_CONNECT_DC:
70 case LIBNET_RPC_CONNECT_PDC:
71 case LIBNET_RPC_CONNECT_SERVER:
72 s->binding = talloc_asprintf(s, "ncacn_np:%s", r->in.name);
75 case LIBNET_RPC_CONNECT_BINDING:
76 s->binding = talloc_strdup(s, r->in.binding);
80 /* connect to remote dcerpc pipe */
81 pipe_connect_req = dcerpc_pipe_connect_send(c, &s->r.out.dcerpc_pipe,
82 s->binding, r->in.dcerpc_iface,
83 ctx->cred, c->event_ctx);
84 if (composite_nomem(pipe_connect_req, c)) return c;
86 composite_continue(c, pipe_connect_req, continue_pipe_connect, c);
91 static void continue_pipe_connect(struct composite_context *ctx)
93 struct composite_context *c;
94 struct rpc_connect_srv_state *s;
96 c = talloc_get_type(ctx->async.private_data, struct composite_context);
97 s = talloc_get_type(c->private_data, struct rpc_connect_srv_state);
99 c->status = dcerpc_pipe_connect_recv(ctx, c, &s->r.out.dcerpc_pipe);
100 if (!composite_is_ok(c)) return;
102 s->r.out.error_string = NULL;
108 * Receives result of connection to rpc pipe on remote server
110 * @param c composite context
111 * @param ctx initialised libnet context
112 * @param mem_ctx memory context of this call
113 * @param r data structure containing necessary parameters and return values
114 * @return nt status of rpc connection
117 static NTSTATUS libnet_RpcConnectSrv_recv(struct composite_context *c,
118 struct libnet_context *ctx,
120 struct libnet_RpcConnect *r)
122 struct rpc_connect_srv_state *s;
123 NTSTATUS status = composite_wait(c);
125 if (NT_STATUS_IS_OK(status) && ctx && mem_ctx && r) {
126 s = talloc_get_type(c->private_data, struct rpc_connect_srv_state);
127 r->out.dcerpc_pipe = talloc_steal(mem_ctx, s->r.out.dcerpc_pipe);
128 ctx->pipe = r->out.dcerpc_pipe;
136 struct rpc_connect_dc_state {
137 struct libnet_context *ctx;
138 struct libnet_RpcConnect r;
139 struct libnet_RpcConnect r2;
140 struct libnet_LookupDCs f;
141 const char *connect_name;
145 static void continue_lookup_dc(struct composite_context *ctx);
146 static void continue_rpc_connect(struct composite_context *ctx);
150 * Initiates connection to rpc pipe on domain pdc
152 * @param ctx initialised libnet context
153 * @param mem_ctx memory context of this call
154 * @param r data structure containing necessary parameters and return values
155 * @return composite context of this call
158 static struct composite_context* libnet_RpcConnectDC_send(struct libnet_context *ctx,
160 struct libnet_RpcConnect *r)
162 struct composite_context *c;
163 struct rpc_connect_dc_state *s;
164 struct composite_context *lookup_dc_req;
166 c = talloc_zero(mem_ctx, struct composite_context);
167 if (c == NULL) return NULL;
169 s = talloc_zero(c, struct rpc_connect_dc_state);
170 if (composite_nomem(s, c)) return c;
172 c->state = COMPOSITE_STATE_IN_PROGRESS;
174 c->event_ctx = ctx->event_ctx;
180 case LIBNET_RPC_CONNECT_PDC:
181 s->f.in.name_type = NBT_NAME_PDC;
184 case LIBNET_RPC_CONNECT_DC:
185 s->f.in.name_type = NBT_NAME_LOGON;
191 s->f.in.domain_name = r->in.name;
192 s->f.out.num_dcs = 0;
195 /* find the domain pdc first */
196 lookup_dc_req = libnet_LookupDCs_send(ctx, c, &s->f);
197 if (composite_nomem(lookup_dc_req, c)) return c;
199 composite_continue(c, lookup_dc_req, continue_lookup_dc, c);
204 static void continue_lookup_dc(struct composite_context *ctx)
206 struct composite_context *c;
207 struct rpc_connect_dc_state *s;
208 struct composite_context *rpc_connect_req;
210 c = talloc_get_type(ctx->async.private_data, struct composite_context);
211 s = talloc_get_type(c->private_data, struct rpc_connect_dc_state);
213 c->status = libnet_LookupDCs_recv(ctx, c, &s->f);
214 if (!composite_is_ok(c)) return;
216 /* we might not have got back a name. Fall back to the IP */
217 if (s->f.out.dcs[0].name) {
218 s->connect_name = s->f.out.dcs[0].name;
220 s->connect_name = s->f.out.dcs[0].address;
223 /* ok, pdc has been found so do attempt to rpc connect */
224 s->r2.level = LIBNET_RPC_CONNECT_SERVER;
226 /* this will cause yet another name resolution, but at least
227 * we pass the right name down the stack now */
228 s->r2.in.name = talloc_strdup(c, s->connect_name);
229 s->r2.in.dcerpc_iface = s->r.in.dcerpc_iface;
231 rpc_connect_req = libnet_RpcConnect_send(s->ctx, c, &s->r2);
232 if (composite_nomem(rpc_connect_req, c)) return;
234 composite_continue(c, rpc_connect_req, continue_rpc_connect, c);
238 static void continue_rpc_connect(struct composite_context *ctx)
240 struct composite_context *c;
241 struct rpc_connect_dc_state *s;
243 c = talloc_get_type(ctx->async.private_data, struct composite_context);
244 s = talloc_get_type(c->private_data, struct rpc_connect_dc_state);
246 c->status = libnet_RpcConnect_recv(ctx, s->ctx, c, &s->r2);
248 /* error string is to be passed anyway */
249 s->r.out.error_string = s->r2.out.error_string;
250 if (!composite_is_ok(c)) return;
252 s->r.out.dcerpc_pipe = s->r2.out.dcerpc_pipe;
259 * Receives result of connection to rpc pipe on domain pdc
261 * @param c composite context
262 * @param ctx initialised libnet context
263 * @param mem_ctx memory context of this call
264 * @param r data structure containing necessary parameters and return values
265 * @return nt status of rpc connection
268 static NTSTATUS libnet_RpcConnectDC_recv(struct composite_context *c,
269 struct libnet_context *ctx,
271 struct libnet_RpcConnect *r)
274 struct rpc_connect_dc_state *s;
276 status = composite_wait(c);
278 if (NT_STATUS_IS_OK(status) && ctx && mem_ctx && r) {
279 s = talloc_get_type(c->private_data, struct rpc_connect_dc_state);
280 r->out.dcerpc_pipe = talloc_steal(mem_ctx, s->r.out.dcerpc_pipe);
281 ctx->pipe = r->out.dcerpc_pipe;
291 * Initiates connection to rpc pipe on remote server or pdc
293 * @param ctx initialised libnet context
294 * @param mem_ctx memory context of this call
295 * @param r data structure containing necessary parameters and return values
296 * @return composite context of this call
299 struct composite_context* libnet_RpcConnect_send(struct libnet_context *ctx,
301 struct libnet_RpcConnect *r)
303 struct composite_context *c;
306 case LIBNET_RPC_CONNECT_SERVER:
307 c = libnet_RpcConnectSrv_send(ctx, mem_ctx, r);
310 case LIBNET_RPC_CONNECT_BINDING:
311 c = libnet_RpcConnectSrv_send(ctx, mem_ctx, r);
314 case LIBNET_RPC_CONNECT_PDC:
315 case LIBNET_RPC_CONNECT_DC:
316 c = libnet_RpcConnectDC_send(ctx, mem_ctx, r);
320 c = talloc_zero(mem_ctx, struct composite_context);
321 composite_error(c, NT_STATUS_INVALID_LEVEL);
329 * Receives result of connection to rpc pipe on remote server or pdc
331 * @param c composite context
332 * @param ctx initialised libnet context
333 * @param mem_ctx memory context of this call
334 * @param r data structure containing necessary parameters and return values
335 * @return nt status of rpc connection
338 NTSTATUS libnet_RpcConnect_recv(struct composite_context *c, struct libnet_context *ctx,
339 TALLOC_CTX *mem_ctx, struct libnet_RpcConnect *r)
342 case LIBNET_RPC_CONNECT_SERVER:
343 case LIBNET_RPC_CONNECT_BINDING:
344 return libnet_RpcConnectSrv_recv(c, ctx, mem_ctx, r);
346 case LIBNET_RPC_CONNECT_PDC:
347 case LIBNET_RPC_CONNECT_DC:
348 return libnet_RpcConnectDC_recv(c, ctx, mem_ctx, r);
351 return NT_STATUS_INVALID_LEVEL;
356 NTSTATUS libnet_RpcConnect(struct libnet_context *ctx, TALLOC_CTX *mem_ctx,
357 struct libnet_RpcConnect *r)
359 struct composite_context *c;
361 c = libnet_RpcConnect_send(ctx, mem_ctx, r);
362 return libnet_RpcConnect_recv(c, ctx, mem_ctx, r);
367 * Connects to rpc pipe on remote server or pdc, and returns info on the domain name, domain sid and realm
369 * @param ctx initialised libnet context
370 * @param r data structure containing necessary parameters and return values. Must be a talloc context
371 * @return nt status of the call
374 NTSTATUS libnet_RpcConnectDCInfo(struct libnet_context *ctx,
375 struct libnet_RpcConnectDCInfo *r)
380 struct libnet_RpcConnect *c;
381 struct lsa_ObjectAttribute attr;
382 struct lsa_QosInfo qos;
383 struct lsa_OpenPolicy2 lsa_open_policy;
384 struct policy_handle lsa_p_handle;
385 struct lsa_QueryInfoPolicy2 lsa_query_info2;
386 struct lsa_QueryInfoPolicy lsa_query_info;
388 struct dcerpc_pipe *lsa_pipe;
390 struct dcerpc_binding *final_binding;
391 struct dcerpc_pipe *final_pipe;
393 tmp_ctx = talloc_new(r);
395 r->out.error_string = NULL;
396 return NT_STATUS_NO_MEMORY;
399 c = talloc(tmp_ctx, struct libnet_RpcConnect);
401 r->out.error_string = NULL;
402 talloc_free(tmp_ctx);
403 return NT_STATUS_NO_MEMORY;
407 if (r->level != LIBNET_RPC_CONNECT_BINDING) {
408 c->in.name = r->in.name;
410 c->in.binding = r->in.binding;
413 c->in.dcerpc_iface = &dcerpc_table_lsarpc;
415 /* connect to the LSA pipe of the PDC */
417 status = libnet_RpcConnect(ctx, c, c);
418 if (!NT_STATUS_IS_OK(status)) {
419 if (r->level != LIBNET_RPC_CONNECT_BINDING) {
420 r->out.error_string = talloc_asprintf(r,
421 "Connection to LSA pipe of DC failed: %s",
422 c->out.error_string);
424 r->out.error_string = talloc_asprintf(r,
425 "Connection to LSA pipe with binding '%s' failed: %s",
426 r->in.binding, c->out.error_string);
428 talloc_free(tmp_ctx);
431 lsa_pipe = c->out.dcerpc_pipe;
433 /* Get an LSA policy handle */
435 ZERO_STRUCT(lsa_p_handle);
437 qos.impersonation_level = 2;
438 qos.context_mode = 1;
439 qos.effective_only = 0;
442 attr.root_dir = NULL;
443 attr.object_name = NULL;
445 attr.sec_desc = NULL;
448 lsa_open_policy.in.attr = &attr;
450 lsa_open_policy.in.system_name = talloc_asprintf(tmp_ctx, "\\");
451 if (!lsa_open_policy.in.system_name) {
452 r->out.error_string = NULL;
453 talloc_free(tmp_ctx);
454 return NT_STATUS_NO_MEMORY;
457 lsa_open_policy.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
458 lsa_open_policy.out.handle = &lsa_p_handle;
460 status = dcerpc_lsa_OpenPolicy2(lsa_pipe, tmp_ctx, &lsa_open_policy);
462 /* This now fails on ncacn_ip_tcp against Win2k3 SP1 */
463 if (NT_STATUS_IS_OK(status)) {
464 /* Look to see if this is ADS (a fault indicates NT4 or Samba 3.0) */
466 lsa_query_info2.in.handle = &lsa_p_handle;
467 lsa_query_info2.in.level = LSA_POLICY_INFO_DNS;
469 status = dcerpc_lsa_QueryInfoPolicy2(lsa_pipe, tmp_ctx,
472 if (!NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
473 if (!NT_STATUS_IS_OK(status)) {
474 r->out.error_string = talloc_asprintf(r,
475 "lsa_QueryInfoPolicy2 failed: %s",
477 talloc_free(tmp_ctx);
480 r->out.realm = lsa_query_info2.out.info->dns.dns_domain.string;
481 r->out.guid = talloc(r, struct GUID);
483 r->out.error_string = NULL;
484 return NT_STATUS_NO_MEMORY;
486 *r->out.guid = lsa_query_info2.out.info->dns.domain_guid;
492 /* Grab the domain SID (regardless of the result of the previous call */
494 lsa_query_info.in.handle = &lsa_p_handle;
495 lsa_query_info.in.level = LSA_POLICY_INFO_DOMAIN;
497 status = dcerpc_lsa_QueryInfoPolicy(lsa_pipe, tmp_ctx,
500 if (!NT_STATUS_IS_OK(status)) {
501 r->out.error_string = talloc_asprintf(r,
502 "lsa_QueryInfoPolicy2 failed: %s",
504 talloc_free(tmp_ctx);
508 r->out.domain_sid = lsa_query_info.out.info->domain.sid;
509 r->out.domain_name = lsa_query_info.out.info->domain.name.string;
511 /* Cause the code further down to try this with just SAMR */
512 r->out.domain_sid = NULL;
513 r->out.domain_name = NULL;
517 /* Find the original binding string */
518 final_binding = talloc(tmp_ctx, struct dcerpc_binding);
519 if (!final_binding) {
520 return NT_STATUS_NO_MEMORY;
522 *final_binding = *lsa_pipe->binding;
523 /* Ensure we keep hold of the member elements */
524 talloc_reference(final_binding, lsa_pipe->binding);
526 /* Make binding string for samr, not the other pipe */
527 status = dcerpc_epm_map_binding(tmp_ctx, final_binding,
529 lsa_pipe->conn->event_ctx);
530 if (!NT_STATUS_IS_OK(status)) {
531 r->out.error_string = talloc_asprintf(r,
532 "Failed to map pipe with endpoint mapper - %s",
534 talloc_free(tmp_ctx);
538 /* Now that we have the info setup a final connection to the pipe they wanted */
539 status = dcerpc_secondary_connection(lsa_pipe, &final_pipe, final_binding);
540 if (!NT_STATUS_IS_OK(status)) {
541 r->out.error_string = talloc_asprintf(r,
542 "secondary connection failed: %s",
544 talloc_free(tmp_ctx);
547 r->out.dcerpc_pipe = final_pipe;
549 talloc_steal(r, r->out.realm);
550 talloc_steal(r, r->out.domain_sid);
551 talloc_steal(r, r->out.domain_name);
552 talloc_steal(r, r->out.dcerpc_pipe);
554 /* This should close the LSA pipe, which we don't need now we have the info */
555 talloc_free(tmp_ctx);