2 Unix SMB/CIFS implementation.
4 Copyright (C) Rafal Szczesniak 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/>.
21 a composite function for domain handling on samr and lsa pipes
25 #include "libcli/composite/composite.h"
26 #include "libnet/libnet.h"
27 #include "librpc/gen_ndr/ndr_samr_c.h"
28 #include "librpc/gen_ndr/ndr_lsa_c.h"
31 struct domain_open_samr_state
{
32 struct libnet_context
*ctx
;
33 struct dcerpc_pipe
*pipe
;
34 struct libnet_RpcConnect rpcconn
;
35 struct samr_Connect connect
;
36 struct samr_LookupDomain lookup
;
37 struct samr_OpenDomain open
;
38 struct samr_Close close
;
39 struct lsa_String domain_name
;
41 struct policy_handle connect_handle
;
42 struct policy_handle domain_handle
;
43 struct dom_sid2
*domain_sid
;
45 /* information about the progress */
46 void (*monitor_fn
)(struct monitor_msg
*);
50 static void continue_domain_open_close(struct rpc_request
*req
);
51 static void continue_domain_open_connect(struct rpc_request
*req
);
52 static void continue_domain_open_lookup(struct rpc_request
*req
);
53 static void continue_domain_open_open(struct rpc_request
*req
);
57 * Stage 0.5 (optional): Connect to samr rpc pipe
59 static void continue_domain_open_rpc_connect(struct composite_context
*ctx
)
61 struct composite_context
*c
;
62 struct domain_open_samr_state
*s
;
63 struct rpc_request
*conn_req
;
65 c
= talloc_get_type(ctx
->async
.private_data
, struct composite_context
);
66 s
= talloc_get_type(c
->private_data
, struct domain_open_samr_state
);
68 c
->status
= libnet_RpcConnect_recv(ctx
, s
->ctx
, c
, &s
->rpcconn
);
69 if (!composite_is_ok(c
)) return;
71 s
->pipe
= s
->rpcconn
.out
.dcerpc_pipe
;
73 /* preparing parameters for samr_Connect rpc call */
74 s
->connect
.in
.system_name
= 0;
75 s
->connect
.in
.access_mask
= s
->access_mask
;
76 s
->connect
.out
.connect_handle
= &s
->connect_handle
;
79 conn_req
= dcerpc_samr_Connect_send(s
->pipe
, c
, &s
->connect
);
80 if (composite_nomem(conn_req
, c
)) return;
82 /* callback handler */
83 composite_continue_rpc(c
, conn_req
, continue_domain_open_connect
, c
);
88 * Stage 0.5 (optional): Close existing (in libnet context) domain
91 static void continue_domain_open_close(struct rpc_request
*req
)
93 struct composite_context
*c
;
94 struct domain_open_samr_state
*s
;
95 struct rpc_request
*conn_req
;
97 c
= talloc_get_type(req
->async
.private_data
, struct composite_context
);
98 s
= talloc_get_type(c
->private_data
, struct domain_open_samr_state
);
100 /* receive samr_Close reply */
101 c
->status
= dcerpc_ndr_request_recv(req
);
102 if (!composite_is_ok(c
)) return;
105 struct monitor_msg msg
;
107 msg
.type
= mon_SamrClose
;
113 /* reset domain handle and associated data in libnet_context */
114 s
->ctx
->samr
.name
= NULL
;
115 s
->ctx
->samr
.access_mask
= 0;
116 ZERO_STRUCT(s
->ctx
->samr
.handle
);
118 /* preparing parameters for samr_Connect rpc call */
119 s
->connect
.in
.system_name
= 0;
120 s
->connect
.in
.access_mask
= s
->access_mask
;
121 s
->connect
.out
.connect_handle
= &s
->connect_handle
;
124 conn_req
= dcerpc_samr_Connect_send(s
->pipe
, c
, &s
->connect
);
125 if (composite_nomem(conn_req
, c
)) return;
127 /* callback handler */
128 composite_continue_rpc(c
, conn_req
, continue_domain_open_connect
, c
);
133 * Stage 1: Connect to SAM server.
135 static void continue_domain_open_connect(struct rpc_request
*req
)
137 struct composite_context
*c
;
138 struct domain_open_samr_state
*s
;
139 struct rpc_request
*lookup_req
;
140 struct samr_LookupDomain
*r
;
142 c
= talloc_get_type(req
->async
.private_data
, struct composite_context
);
143 s
= talloc_get_type(c
->private_data
, struct domain_open_samr_state
);
145 /* receive samr_Connect reply */
146 c
->status
= dcerpc_ndr_request_recv(req
);
147 if (!composite_is_ok(c
)) return;
150 struct monitor_msg msg
;
152 msg
.type
= mon_SamrConnect
;
160 /* prepare for samr_LookupDomain call */
161 r
->in
.connect_handle
= &s
->connect_handle
;
162 r
->in
.domain_name
= &s
->domain_name
;
163 r
->out
.sid
= talloc(s
, struct dom_sid2
*);
164 if (composite_nomem(r
->out
.sid
, c
)) return;
166 lookup_req
= dcerpc_samr_LookupDomain_send(s
->pipe
, c
, r
);
167 if (composite_nomem(lookup_req
, c
)) return;
169 composite_continue_rpc(c
, lookup_req
, continue_domain_open_lookup
, c
);
174 * Stage 2: Lookup domain by name.
176 static void continue_domain_open_lookup(struct rpc_request
*req
)
178 struct composite_context
*c
;
179 struct domain_open_samr_state
*s
;
180 struct rpc_request
*opendom_req
;
181 struct samr_OpenDomain
*r
;
183 c
= talloc_get_type(req
->async
.private_data
, struct composite_context
);
184 s
= talloc_get_type(c
->private_data
, struct domain_open_samr_state
);
186 /* receive samr_LookupDomain reply */
187 c
->status
= dcerpc_ndr_request_recv(req
);
190 struct monitor_msg msg
;
191 struct msg_rpc_lookup_domain data
;
193 data
.domain_name
= s
->domain_name
.string
;
195 msg
.type
= mon_SamrLookupDomain
;
196 msg
.data
= (void*)&data
;
197 msg
.data_size
= sizeof(data
);
203 /* check the rpc layer status */
204 if (!composite_is_ok(c
));
206 /* check the rpc call itself status */
207 if (!NT_STATUS_IS_OK(s
->lookup
.out
.result
)) {
208 composite_error(c
, s
->lookup
.out
.result
);
212 /* prepare for samr_OpenDomain call */
213 r
->in
.connect_handle
= &s
->connect_handle
;
214 r
->in
.access_mask
= SEC_FLAG_MAXIMUM_ALLOWED
;
215 r
->in
.sid
= *s
->lookup
.out
.sid
;
216 r
->out
.domain_handle
= &s
->domain_handle
;
218 opendom_req
= dcerpc_samr_OpenDomain_send(s
->pipe
, c
, r
);
219 if (composite_nomem(opendom_req
, c
)) return;
221 composite_continue_rpc(c
, opendom_req
, continue_domain_open_open
, c
);
226 * Stage 3: Open domain.
228 static void continue_domain_open_open(struct rpc_request
*req
)
230 struct composite_context
*c
;
231 struct domain_open_samr_state
*s
;
233 c
= talloc_get_type(req
->async
.private_data
, struct composite_context
);
234 s
= talloc_get_type(c
->private_data
, struct domain_open_samr_state
);
236 /* receive samr_OpenDomain reply */
237 c
->status
= dcerpc_ndr_request_recv(req
);
238 if (!composite_is_ok(c
)) return;
241 struct monitor_msg msg
;
243 msg
.type
= mon_SamrOpenDomain
;
254 * Sends asynchronous DomainOpenSamr request
256 * @param ctx initialised libnet context
257 * @param io arguments and results of the call
258 * @param monitor pointer to monitor function that is passed monitor message
261 struct composite_context
*libnet_DomainOpenSamr_send(struct libnet_context
*ctx
,
262 struct libnet_DomainOpen
*io
,
263 void (*monitor
)(struct monitor_msg
*))
265 struct composite_context
*c
;
266 struct domain_open_samr_state
*s
;
267 struct composite_context
*rpcconn_req
;
268 struct rpc_request
*close_req
, *conn_req
;
270 c
= composite_create(ctx
, ctx
->event_ctx
);
271 if (c
== NULL
) return NULL
;
273 s
= talloc_zero(c
, struct domain_open_samr_state
);
274 if (composite_nomem(s
, c
)) return c
;
277 s
->monitor_fn
= monitor
;
280 s
->pipe
= ctx
->samr
.pipe
;
281 s
->access_mask
= io
->in
.access_mask
;
282 s
->domain_name
.string
= talloc_strdup(c
, io
->in
.domain_name
);
284 /* check, if there's samr pipe opened already, before opening a domain */
285 if (ctx
->samr
.pipe
== NULL
) {
287 /* attempting to connect a domain controller */
288 s
->rpcconn
.level
= LIBNET_RPC_CONNECT_DC
;
289 s
->rpcconn
.in
.name
= io
->in
.domain_name
;
290 s
->rpcconn
.in
.dcerpc_iface
= &ndr_table_samr
;
292 /* send rpc pipe connect request */
293 rpcconn_req
= libnet_RpcConnect_send(ctx
, c
, &s
->rpcconn
, s
->monitor_fn
);
294 if (composite_nomem(rpcconn_req
, c
)) return c
;
296 composite_continue(c
, rpcconn_req
, continue_domain_open_rpc_connect
, c
);
300 /* libnet context's domain handle is not empty, so check out what
301 was opened first, before doing anything */
302 if (!policy_handle_empty(&ctx
->samr
.handle
)) {
303 if (strequal(ctx
->samr
.name
, io
->in
.domain_name
) &&
304 ctx
->samr
.access_mask
== io
->in
.access_mask
) {
306 /* this domain is already opened */
311 /* another domain or access rights have been
312 requested - close the existing handle first */
313 s
->close
.in
.handle
= &ctx
->samr
.handle
;
315 /* send request to close domain handle */
316 close_req
= dcerpc_samr_Close_send(s
->pipe
, c
, &s
->close
);
317 if (composite_nomem(close_req
, c
)) return c
;
319 /* callback handler */
320 composite_continue_rpc(c
, close_req
, continue_domain_open_close
, c
);
325 /* preparing parameters for samr_Connect rpc call */
326 s
->connect
.in
.system_name
= 0;
327 s
->connect
.in
.access_mask
= s
->access_mask
;
328 s
->connect
.out
.connect_handle
= &s
->connect_handle
;
331 conn_req
= dcerpc_samr_Connect_send(s
->pipe
, c
, &s
->connect
);
332 if (composite_nomem(conn_req
, c
)) return c
;
334 /* callback handler */
335 composite_continue_rpc(c
, conn_req
, continue_domain_open_connect
, c
);
341 * Waits for and receives result of asynchronous DomainOpenSamr call
343 * @param c composite context returned by asynchronous DomainOpen call
344 * @param ctx initialised libnet context
345 * @param mem_ctx memory context of the call
346 * @param io pointer to results (and arguments) of the call
347 * @return nt status code of execution
350 NTSTATUS
libnet_DomainOpenSamr_recv(struct composite_context
*c
, struct libnet_context
*ctx
,
351 TALLOC_CTX
*mem_ctx
, struct libnet_DomainOpen
*io
)
354 struct domain_open_samr_state
*s
;
356 /* wait for results of sending request */
357 status
= composite_wait(c
);
359 if (NT_STATUS_IS_OK(status
) && io
) {
360 s
= talloc_get_type(c
->private_data
, struct domain_open_samr_state
);
361 io
->out
.domain_handle
= s
->domain_handle
;
363 /* store the resulting handle and related data for use by other
365 ctx
->samr
.connect_handle
= s
->connect_handle
;
366 ctx
->samr
.handle
= s
->domain_handle
;
367 ctx
->samr
.sid
= talloc_steal(ctx
, *s
->lookup
.out
.sid
);
368 ctx
->samr
.name
= talloc_steal(ctx
, s
->domain_name
.string
);
369 ctx
->samr
.access_mask
= s
->access_mask
;
377 struct domain_open_lsa_state
{
379 uint32_t access_mask
;
380 struct libnet_context
*ctx
;
381 struct libnet_RpcConnect rpcconn
;
382 struct lsa_OpenPolicy2 openpol
;
383 struct policy_handle handle
;
384 struct dcerpc_pipe
*pipe
;
386 /* information about the progress */
387 void (*monitor_fn
)(struct monitor_msg
*);
391 static void continue_rpc_connect_lsa(struct composite_context
*ctx
);
392 static void continue_lsa_policy_open(struct rpc_request
*req
);
396 * Sends asynchronous DomainOpenLsa request
398 * @param ctx initialised libnet context
399 * @param io arguments and results of the call
400 * @param monitor pointer to monitor function that is passed monitor message
403 struct composite_context
* libnet_DomainOpenLsa_send(struct libnet_context
*ctx
,
404 struct libnet_DomainOpen
*io
,
405 void (*monitor
)(struct monitor_msg
*))
407 struct composite_context
*c
;
408 struct domain_open_lsa_state
*s
;
409 struct composite_context
*rpcconn_req
;
410 struct rpc_request
*openpol_req
;
411 struct lsa_QosInfo
*qos
;
413 /* create composite context and state */
414 c
= composite_create(ctx
, ctx
->event_ctx
);
415 if (c
== NULL
) return c
;
417 s
= talloc_zero(c
, struct domain_open_lsa_state
);
418 if (composite_nomem(s
, c
)) return c
;
422 /* store arguments in the state structure */
423 s
->name
= talloc_strdup(c
, io
->in
.domain_name
);
424 s
->access_mask
= io
->in
.access_mask
;
427 /* check, if there's lsa pipe opened already, before opening a handle */
428 if (ctx
->lsa
.pipe
== NULL
) {
430 /* attempting to connect a domain controller */
431 s
->rpcconn
.level
= LIBNET_RPC_CONNECT_DC
;
432 s
->rpcconn
.in
.name
= talloc_strdup(c
, io
->in
.domain_name
);
433 s
->rpcconn
.in
.dcerpc_iface
= &ndr_table_lsarpc
;
435 /* send rpc pipe connect request */
436 rpcconn_req
= libnet_RpcConnect_send(ctx
, c
, &s
->rpcconn
, s
->monitor_fn
);
437 if (composite_nomem(rpcconn_req
, c
)) return c
;
439 composite_continue(c
, rpcconn_req
, continue_rpc_connect_lsa
, c
);
443 s
->pipe
= ctx
->lsa
.pipe
;
445 /* preparing parameters for lsa_OpenPolicy2 rpc call */
446 s
->openpol
.in
.system_name
= s
->name
;
447 s
->openpol
.in
.access_mask
= s
->access_mask
;
448 s
->openpol
.in
.attr
= talloc_zero(c
, struct lsa_ObjectAttribute
);
450 qos
= talloc_zero(c
, struct lsa_QosInfo
);
452 qos
->impersonation_level
= 2;
453 qos
->context_mode
= 1;
454 qos
->effective_only
= 0;
456 s
->openpol
.in
.attr
->sec_qos
= qos
;
457 s
->openpol
.out
.handle
= &s
->handle
;
459 /* send rpc request */
460 openpol_req
= dcerpc_lsa_OpenPolicy2_send(s
->pipe
, c
, &s
->openpol
);
461 if (composite_nomem(openpol_req
, c
)) return c
;
463 composite_continue_rpc(c
, openpol_req
, continue_lsa_policy_open
, c
);
469 Stage 0.5 (optional): Rpc pipe connected, send lsa open policy request
471 static void continue_rpc_connect_lsa(struct composite_context
*ctx
)
473 struct composite_context
*c
;
474 struct domain_open_lsa_state
*s
;
475 struct lsa_QosInfo
*qos
;
476 struct rpc_request
*openpol_req
;
478 c
= talloc_get_type(ctx
->async
.private_data
, struct composite_context
);
479 s
= talloc_get_type(c
->private_data
, struct domain_open_lsa_state
);
481 /* receive rpc connection */
482 c
->status
= libnet_RpcConnect_recv(ctx
, s
->ctx
, c
, &s
->rpcconn
);
483 if (!composite_is_ok(c
)) return;
485 /* RpcConnect function leaves the pipe in libnet context,
486 so get it from there */
487 s
->pipe
= s
->ctx
->lsa
.pipe
;
489 /* prepare lsa_OpenPolicy2 call */
490 s
->openpol
.in
.system_name
= s
->name
;
491 s
->openpol
.in
.access_mask
= s
->access_mask
;
492 s
->openpol
.in
.attr
= talloc_zero(c
, struct lsa_ObjectAttribute
);
494 qos
= talloc_zero(c
, struct lsa_QosInfo
);
496 qos
->impersonation_level
= 2;
497 qos
->context_mode
= 1;
498 qos
->effective_only
= 0;
500 s
->openpol
.in
.attr
->sec_qos
= qos
;
501 s
->openpol
.out
.handle
= &s
->handle
;
503 /* send rpc request */
504 openpol_req
= dcerpc_lsa_OpenPolicy2_send(s
->pipe
, c
, &s
->openpol
);
505 if (composite_nomem(openpol_req
, c
)) return;
507 composite_continue_rpc(c
, openpol_req
, continue_lsa_policy_open
, c
);
512 Stage 1: Lsa policy opened - we're done, if successfully
514 static void continue_lsa_policy_open(struct rpc_request
*req
)
516 struct composite_context
*c
;
517 struct domain_open_lsa_state
*s
;
519 c
= talloc_get_type(req
->async
.private_data
, struct composite_context
);
520 s
= talloc_get_type(c
->private_data
, struct domain_open_lsa_state
);
522 c
->status
= dcerpc_ndr_request_recv(req
);
523 if (!composite_is_ok(c
)) return;
526 struct monitor_msg msg
;
528 msg
.type
= mon_LsaOpenPolicy
;
539 * Receives result of asynchronous DomainOpenLsa call
541 * @param c composite context returned by asynchronous DomainOpenLsa call
542 * @param ctx initialised libnet context
543 * @param mem_ctx memory context of the call
544 * @param io pointer to results (and arguments) of the call
545 * @return nt status code of execution
548 NTSTATUS
libnet_DomainOpenLsa_recv(struct composite_context
*c
, struct libnet_context
*ctx
,
549 TALLOC_CTX
*mem_ctx
, struct libnet_DomainOpen
*io
)
552 struct domain_open_lsa_state
*s
;
554 status
= composite_wait(c
);
556 if (NT_STATUS_IS_OK(status
) && io
) {
557 /* everything went fine - get the results and
558 return the error string */
559 s
= talloc_get_type(c
->private_data
, struct domain_open_lsa_state
);
560 io
->out
.domain_handle
= s
->handle
;
562 ctx
->lsa
.handle
= s
->handle
;
563 ctx
->lsa
.name
= talloc_steal(ctx
, s
->name
);
564 ctx
->lsa
.access_mask
= s
->access_mask
;
566 io
->out
.error_string
= talloc_strdup(mem_ctx
, "Success");
568 } else if (!NT_STATUS_IS_OK(status
)) {
569 /* there was an error, so provide nt status code description */
570 io
->out
.error_string
= talloc_asprintf(mem_ctx
,
571 "Failed to open domain: %s",
581 * Sends a request to open a domain in desired service
583 * @param ctx initalised libnet context
584 * @param io arguments and results of the call
585 * @param monitor pointer to monitor function that is passed monitor message
588 struct composite_context
* libnet_DomainOpen_send(struct libnet_context
*ctx
,
589 struct libnet_DomainOpen
*io
,
590 void (*monitor
)(struct monitor_msg
*))
592 struct composite_context
*c
;
594 switch (io
->in
.type
) {
596 /* reques to open a policy handle on \pipe\lsarpc */
597 c
= libnet_DomainOpenLsa_send(ctx
, io
, monitor
);
602 /* request to open a domain policy handle on \pipe\samr */
603 c
= libnet_DomainOpenSamr_send(ctx
, io
, monitor
);
612 * Receive result of domain open request
614 * @param c composite context returned by DomainOpen_send function
615 * @param ctx initialised libnet context
616 * @param mem_ctx memory context of the call
617 * @param io results and arguments of the call
620 NTSTATUS
libnet_DomainOpen_recv(struct composite_context
*c
, struct libnet_context
*ctx
,
621 TALLOC_CTX
*mem_ctx
, struct libnet_DomainOpen
*io
)
625 switch (io
->in
.type
) {
627 status
= libnet_DomainOpenLsa_recv(c
, ctx
, mem_ctx
, io
);
632 status
= libnet_DomainOpenSamr_recv(c
, ctx
, mem_ctx
, io
);
641 * Synchronous version of DomainOpen call
643 * @param ctx initialised libnet context
644 * @param mem_ctx memory context for the call
645 * @param io arguments and results of the call
646 * @return nt status code of execution
649 NTSTATUS
libnet_DomainOpen(struct libnet_context
*ctx
,
651 struct libnet_DomainOpen
*io
)
653 struct composite_context
*c
= libnet_DomainOpen_send(ctx
, io
, NULL
);
654 return libnet_DomainOpen_recv(c
, ctx
, mem_ctx
, io
);
658 struct domain_close_lsa_state
{
659 struct dcerpc_pipe
*pipe
;
660 struct lsa_Close close
;
661 struct policy_handle handle
;
663 void (*monitor_fn
)(struct monitor_msg
*);
667 static void continue_lsa_close(struct rpc_request
*req
);
670 struct composite_context
* libnet_DomainCloseLsa_send(struct libnet_context
*ctx
,
671 struct libnet_DomainClose
*io
,
672 void (*monitor
)(struct monitor_msg
*))
674 struct composite_context
*c
;
675 struct domain_close_lsa_state
*s
;
676 struct rpc_request
*close_req
;
678 /* composite context and state structure allocation */
679 c
= composite_create(ctx
, ctx
->event_ctx
);
680 if (c
== NULL
) return c
;
682 s
= talloc_zero(c
, struct domain_close_lsa_state
);
683 if (composite_nomem(s
, c
)) return c
;
686 s
->monitor_fn
= monitor
;
688 /* TODO: check if lsa pipe pointer is non-null */
690 if (!strequal(ctx
->lsa
.name
, io
->in
.domain_name
)) {
691 composite_error(c
, NT_STATUS_INVALID_PARAMETER
);
695 /* get opened lsarpc pipe pointer */
696 s
->pipe
= ctx
->lsa
.pipe
;
698 /* prepare close handle call arguments */
699 s
->close
.in
.handle
= &ctx
->lsa
.handle
;
700 s
->close
.out
.handle
= &s
->handle
;
702 /* send the request */
703 close_req
= dcerpc_lsa_Close_send(s
->pipe
, c
, &s
->close
);
704 if (composite_nomem(close_req
, c
)) return c
;
706 composite_continue_rpc(c
, close_req
, continue_lsa_close
, c
);
712 Stage 1: Receive result of lsa close call
714 static void continue_lsa_close(struct rpc_request
*req
)
716 struct composite_context
*c
;
717 struct domain_close_lsa_state
*s
;
719 c
= talloc_get_type(req
->async
.private_data
, struct composite_context
);
720 s
= talloc_get_type(c
->private_data
, struct domain_close_lsa_state
);
722 c
->status
= dcerpc_ndr_request_recv(req
);
723 if (!composite_is_ok(c
)) return;
726 struct monitor_msg msg
;
728 msg
.type
= mon_LsaClose
;
738 NTSTATUS
libnet_DomainCloseLsa_recv(struct composite_context
*c
, struct libnet_context
*ctx
,
739 TALLOC_CTX
*mem_ctx
, struct libnet_DomainClose
*io
)
743 status
= composite_wait(c
);
745 if (NT_STATUS_IS_OK(status
) && io
) {
746 /* policy handle closed successfully */
748 ctx
->lsa
.name
= NULL
;
749 ZERO_STRUCT(ctx
->lsa
.handle
);
751 io
->out
.error_string
= talloc_asprintf(mem_ctx
, "Success");
753 } else if (!NT_STATUS_IS_OK(status
)) {
754 /* there was an error, so return description of the status code */
755 io
->out
.error_string
= talloc_asprintf(mem_ctx
, "Error: %s", nt_errstr(status
));
763 struct domain_close_samr_state
{
764 struct samr_Close close
;
765 struct policy_handle handle
;
767 void (*monitor_fn
)(struct monitor_msg
*);
771 static void continue_samr_close(struct rpc_request
*req
);
774 struct composite_context
* libnet_DomainCloseSamr_send(struct libnet_context
*ctx
,
775 struct libnet_DomainClose
*io
,
776 void (*monitor
)(struct monitor_msg
*))
778 struct composite_context
*c
;
779 struct domain_close_samr_state
*s
;
780 struct rpc_request
*close_req
;
782 /* composite context and state structure allocation */
783 c
= composite_create(ctx
, ctx
->event_ctx
);
784 if (c
== NULL
) return c
;
786 s
= talloc_zero(c
, struct domain_close_samr_state
);
787 if (composite_nomem(s
, c
)) return c
;
790 s
->monitor_fn
= monitor
;
792 /* TODO: check if samr pipe pointer is non-null */
794 if (!strequal(ctx
->samr
.name
, io
->in
.domain_name
)) {
795 composite_error(c
, NT_STATUS_INVALID_PARAMETER
);
799 /* prepare close domain handle call arguments */
800 ZERO_STRUCT(s
->close
);
801 s
->close
.in
.handle
= &ctx
->samr
.handle
;
802 s
->close
.out
.handle
= &s
->handle
;
804 /* send the request */
805 close_req
= dcerpc_samr_Close_send(ctx
->samr
.pipe
, ctx
, &s
->close
);
806 if (composite_nomem(close_req
, c
)) return c
;
808 composite_continue_rpc(c
, close_req
, continue_samr_close
, c
);
814 Stage 1: Receive result of samr close call
816 static void continue_samr_close(struct rpc_request
*req
)
818 struct composite_context
*c
;
819 struct domain_close_samr_state
*s
;
821 c
= talloc_get_type(req
->async
.private_data
, struct composite_context
);
822 s
= talloc_get_type(c
->private_data
, struct domain_close_samr_state
);
824 c
->status
= dcerpc_ndr_request_recv(req
);
825 if (!composite_is_ok(c
)) return;
828 struct monitor_msg msg
;
830 msg
.type
= mon_SamrClose
;
840 NTSTATUS
libnet_DomainCloseSamr_recv(struct composite_context
*c
, struct libnet_context
*ctx
,
841 TALLOC_CTX
*mem_ctx
, struct libnet_DomainClose
*io
)
845 status
= composite_wait(c
);
847 if (NT_STATUS_IS_OK(status
) && io
) {
848 /* domain policy handle closed successfully */
850 ZERO_STRUCT(ctx
->samr
.handle
);
851 talloc_free(discard_const_p(char, ctx
->samr
.name
));
852 talloc_free(ctx
->samr
.sid
);
853 ctx
->samr
.name
= NULL
;
854 ctx
->samr
.sid
= NULL
;
856 io
->out
.error_string
= talloc_asprintf(mem_ctx
, "Success");
858 } else if (!NT_STATUS_IS_OK(status
)) {
859 /* there was an error, so return description of the status code */
860 io
->out
.error_string
= talloc_asprintf(mem_ctx
, "Error: %s", nt_errstr(status
));
868 struct composite_context
* libnet_DomainClose_send(struct libnet_context
*ctx
,
869 struct libnet_DomainClose
*io
,
870 void (*monitor
)(struct monitor_msg
*))
872 struct composite_context
*c
;
874 switch (io
->in
.type
) {
876 /* request to close policy handle on \pipe\lsarpc */
877 c
= libnet_DomainCloseLsa_send(ctx
, io
, monitor
);
882 /* request to close domain policy handle on \pipe\samr */
883 c
= libnet_DomainCloseSamr_send(ctx
, io
, monitor
);
891 NTSTATUS
libnet_DomainClose_recv(struct composite_context
*c
, struct libnet_context
*ctx
,
892 TALLOC_CTX
*mem_ctx
, struct libnet_DomainClose
*io
)
896 switch (io
->in
.type
) {
898 /* receive result of closing lsa policy handle */
899 status
= libnet_DomainCloseLsa_recv(c
, ctx
, mem_ctx
, io
);
904 /* receive result of closing samr domain policy handle */
905 status
= libnet_DomainCloseSamr_recv(c
, ctx
, mem_ctx
, io
);
913 NTSTATUS
libnet_DomainClose(struct libnet_context
*ctx
, TALLOC_CTX
*mem_ctx
,
914 struct libnet_DomainClose
*io
)
916 struct composite_context
*c
;
918 c
= libnet_DomainClose_send(ctx
, io
, NULL
);
919 return libnet_DomainClose_recv(c
, ctx
, mem_ctx
, io
);
923 struct domain_list_state
{
924 struct libnet_context
*ctx
;
925 struct libnet_RpcConnect rpcconn
;
926 struct samr_Connect samrconn
;
927 struct samr_EnumDomains enumdom
;
928 struct samr_Close samrclose
;
929 const char *hostname
;
930 struct policy_handle connect_handle
;
932 struct domainlist
*domains
;
933 uint32_t resume_handle
;
936 void (*monitor_fn
)(struct monitor_msg
*);
940 static void continue_rpc_connect(struct composite_context
*c
);
941 static void continue_samr_connect(struct rpc_request
*c
);
942 static void continue_samr_enum_domains(struct rpc_request
*req
);
943 static void continue_samr_close_handle(struct rpc_request
*req
);
945 static struct domainlist
* get_domain_list(TALLOC_CTX
*mem_ctx
, struct domain_list_state
*s
);
949 Stage 1: Receive connected rpc pipe and send connection
950 request to SAMR service
952 static void continue_rpc_connect(struct composite_context
*ctx
)
954 struct composite_context
*c
;
955 struct domain_list_state
*s
;
956 struct rpc_request
*samrconn_req
;
958 c
= talloc_get_type(ctx
->async
.private_data
, struct composite_context
);
959 s
= talloc_get_type(c
->private_data
, struct domain_list_state
);
961 c
->status
= libnet_RpcConnect_recv(ctx
, s
->ctx
, c
, &s
->rpcconn
);
962 if (!composite_is_ok(c
)) return;
964 s
->samrconn
.in
.system_name
= 0;
965 s
->samrconn
.in
.access_mask
= SEC_GENERIC_READ
; /* should be enough */
966 s
->samrconn
.out
.connect_handle
= &s
->connect_handle
;
968 samrconn_req
= dcerpc_samr_Connect_send(s
->ctx
->samr
.pipe
, c
, &s
->samrconn
);
969 if (composite_nomem(samrconn_req
, c
)) return;
971 composite_continue_rpc(c
, samrconn_req
, continue_samr_connect
, c
);
976 Stage 2: Receive policy handle to the connected SAMR service and issue
977 a request to enumerate domain databases available
979 static void continue_samr_connect(struct rpc_request
*req
)
981 struct composite_context
*c
;
982 struct domain_list_state
*s
;
983 struct rpc_request
*enumdom_req
;
985 c
= talloc_get_type(req
->async
.private_data
, struct composite_context
);
986 s
= talloc_get_type(c
->private_data
, struct domain_list_state
);
988 c
->status
= dcerpc_ndr_request_recv(req
);
989 if (!composite_is_ok(c
)) return;
992 struct monitor_msg msg
;
994 msg
.type
= mon_SamrConnect
;
1000 s
->enumdom
.in
.connect_handle
= &s
->connect_handle
;
1001 s
->enumdom
.in
.resume_handle
= &s
->resume_handle
;
1002 s
->enumdom
.in
.buf_size
= s
->buf_size
;
1003 s
->enumdom
.out
.resume_handle
= &s
->resume_handle
;
1004 s
->enumdom
.out
.num_entries
= talloc(s
, uint32_t);
1005 if (composite_nomem(s
->enumdom
.out
.num_entries
, c
)) return;
1006 s
->enumdom
.out
.sam
= talloc(s
, struct samr_SamArray
*);
1007 if (composite_nomem(s
->enumdom
.out
.sam
, c
)) return;
1009 enumdom_req
= dcerpc_samr_EnumDomains_send(s
->ctx
->samr
.pipe
, c
, &s
->enumdom
);
1010 if (composite_nomem(enumdom_req
, c
)) return;
1012 composite_continue_rpc(c
, enumdom_req
, continue_samr_enum_domains
, c
);
1017 Stage 3: Receive domain names available and repeat the request
1018 enumeration is not complete yet. Close samr connection handle
1021 static void continue_samr_enum_domains(struct rpc_request
*req
)
1023 struct composite_context
*c
;
1024 struct domain_list_state
*s
;
1025 struct rpc_request
*enumdom_req
;
1026 struct rpc_request
*samrclose_req
;
1028 c
= talloc_get_type(req
->async
.private_data
, struct composite_context
);
1029 s
= talloc_get_type(c
->private_data
, struct domain_list_state
);
1031 c
->status
= dcerpc_ndr_request_recv(req
);
1032 if (!composite_is_ok(c
)) return;
1034 if (s
->monitor_fn
) {
1035 struct monitor_msg msg
;
1037 msg
.type
= mon_SamrEnumDomains
;
1040 s
->monitor_fn(&msg
);
1043 if (NT_STATUS_IS_OK(s
->enumdom
.out
.result
)) {
1045 s
->domains
= get_domain_list(c
, s
);
1047 } else if (NT_STATUS_EQUAL(s
->enumdom
.out
.result
, STATUS_MORE_ENTRIES
)) {
1049 s
->domains
= get_domain_list(c
, s
);
1051 /* prepare next round of enumeration */
1052 s
->enumdom
.in
.connect_handle
= &s
->connect_handle
;
1053 s
->enumdom
.in
.resume_handle
= &s
->resume_handle
;
1054 s
->enumdom
.in
.buf_size
= s
->ctx
->samr
.buf_size
;
1055 s
->enumdom
.out
.resume_handle
= &s
->resume_handle
;
1057 /* send the request */
1058 enumdom_req
= dcerpc_samr_EnumDomains_send(s
->ctx
->samr
.pipe
, c
, &s
->enumdom
);
1059 if (composite_nomem(enumdom_req
, c
)) return;
1061 composite_continue_rpc(c
, enumdom_req
, continue_samr_enum_domains
, c
);
1064 composite_error(c
, s
->enumdom
.out
.result
);
1068 /* close samr connection handle */
1069 s
->samrclose
.in
.handle
= &s
->connect_handle
;
1070 s
->samrclose
.out
.handle
= &s
->connect_handle
;
1072 /* send the request */
1073 samrclose_req
= dcerpc_samr_Close_send(s
->ctx
->samr
.pipe
, c
, &s
->samrclose
);
1074 if (composite_nomem(samrclose_req
, c
)) return;
1076 composite_continue_rpc(c
, samrclose_req
, continue_samr_close_handle
, c
);
1081 Stage 4: Receive result of closing samr connection handle.
1083 static void continue_samr_close_handle(struct rpc_request
*req
)
1085 struct composite_context
*c
;
1086 struct domain_list_state
*s
;
1088 c
= talloc_get_type(req
->async
.private_data
, struct composite_context
);
1089 s
= talloc_get_type(c
->private_data
, struct domain_list_state
);
1091 c
->status
= dcerpc_ndr_request_recv(req
);
1092 if (!composite_is_ok(c
)) return;
1094 if (s
->monitor_fn
) {
1095 struct monitor_msg msg
;
1097 msg
.type
= mon_SamrClose
;
1100 s
->monitor_fn(&msg
);
1103 /* did everything go fine ? */
1104 if (!NT_STATUS_IS_OK(s
->samrclose
.out
.result
)) {
1105 composite_error(c
, s
->samrclose
.out
.result
);
1113 Utility function to copy domain names from result of samr_EnumDomains call
1115 static struct domainlist
* get_domain_list(TALLOC_CTX
*mem_ctx
, struct domain_list_state
*s
)
1118 if (mem_ctx
== NULL
|| s
== NULL
) return NULL
;
1120 /* prepare domains array */
1121 if (s
->domains
== NULL
) {
1122 s
->domains
= talloc_array(mem_ctx
, struct domainlist
,
1123 *s
->enumdom
.out
.num_entries
);
1125 s
->domains
= talloc_realloc(mem_ctx
, s
->domains
, struct domainlist
,
1126 s
->count
+ *s
->enumdom
.out
.num_entries
);
1129 /* copy domain names returned from samr_EnumDomains call */
1130 for (i
= s
->count
; i
< s
->count
+ *s
->enumdom
.out
.num_entries
; i
++)
1132 struct lsa_String
*domain_name
= &(*s
->enumdom
.out
.sam
)->entries
[i
- s
->count
].name
;
1134 /* strdup name as a child of allocated array to make it follow the array
1135 in case of talloc_steal or talloc_free */
1136 s
->domains
[i
].name
= talloc_strdup(s
->domains
, domain_name
->string
);
1137 s
->domains
[i
].sid
= NULL
; /* this is to be filled out later */
1140 /* number of entries returned (domains enumerated) */
1141 s
->count
+= *s
->enumdom
.out
.num_entries
;
1148 * Sends a request to list domains on given host
1150 * @param ctx initalised libnet context
1151 * @param mem_ctx memory context
1152 * @param io arguments and results of the call
1153 * @param monitor pointer to monitor function that is passed monitor messages
1156 struct composite_context
* libnet_DomainList_send(struct libnet_context
*ctx
,
1157 TALLOC_CTX
*mem_ctx
,
1158 struct libnet_DomainList
*io
,
1159 void (*monitor
)(struct monitor_msg
*))
1161 struct composite_context
*c
;
1162 struct domain_list_state
*s
;
1163 struct composite_context
*rpcconn_req
;
1164 struct rpc_request
*samrconn_req
;
1166 /* composite context and state structure allocation */
1167 c
= composite_create(ctx
, ctx
->event_ctx
);
1168 if (c
== NULL
) return c
;
1170 s
= talloc_zero(c
, struct domain_list_state
);
1171 if (composite_nomem(s
, c
)) return c
;
1173 c
->private_data
= s
;
1174 s
->monitor_fn
= monitor
;
1177 s
->hostname
= talloc_strdup(c
, io
->in
.hostname
);
1178 if (composite_nomem(s
->hostname
, c
)) return c
;
1180 /* check whether samr pipe has already been opened */
1181 if (ctx
->samr
.pipe
== NULL
) {
1182 /* prepare rpc connect call */
1183 s
->rpcconn
.level
= LIBNET_RPC_CONNECT_SERVER
;
1184 s
->rpcconn
.in
.name
= s
->hostname
;
1185 s
->rpcconn
.in
.dcerpc_iface
= &ndr_table_samr
;
1187 rpcconn_req
= libnet_RpcConnect_send(ctx
, c
, &s
->rpcconn
, s
->monitor_fn
);
1188 if (composite_nomem(rpcconn_req
, c
)) return c
;
1190 composite_continue(c
, rpcconn_req
, continue_rpc_connect
, c
);
1193 /* prepare samr_Connect call */
1194 s
->samrconn
.in
.system_name
= 0;
1195 s
->samrconn
.in
.access_mask
= SEC_GENERIC_READ
;
1196 s
->samrconn
.out
.connect_handle
= &s
->connect_handle
;
1198 samrconn_req
= dcerpc_samr_Connect_send(s
->ctx
->samr
.pipe
, c
, &s
->samrconn
);
1199 if (composite_nomem(samrconn_req
, c
)) return c
;
1201 composite_continue_rpc(c
, samrconn_req
, continue_samr_connect
, c
);
1209 * Receive result of domain list request
1211 * @param c composite context returned by DomainList_send function
1212 * @param ctx initialised libnet context
1213 * @param mem_ctx memory context of the call
1214 * @param io results and arguments of the call
1217 NTSTATUS
libnet_DomainList_recv(struct composite_context
*c
, struct libnet_context
*ctx
,
1218 TALLOC_CTX
*mem_ctx
, struct libnet_DomainList
*io
)
1221 struct domain_list_state
*s
;
1223 status
= composite_wait(c
);
1225 s
= talloc_get_type(c
->private_data
, struct domain_list_state
);
1227 if (NT_STATUS_IS_OK(status
) && ctx
&& mem_ctx
&& io
) {
1228 /* fetch the results to be returned by io structure */
1229 io
->out
.count
= s
->count
;
1230 io
->out
.domains
= talloc_steal(mem_ctx
, s
->domains
);
1231 io
->out
.error_string
= talloc_asprintf(mem_ctx
, "Success");
1233 } else if (!NT_STATUS_IS_OK(status
)) {
1234 /* there was an error, so return description of the status code */
1235 io
->out
.error_string
= talloc_asprintf(mem_ctx
, "Error: %s", nt_errstr(status
));
1244 * Synchronous version of DomainList call
1246 * @param ctx initialised libnet context
1247 * @param mem_ctx memory context for the call
1248 * @param io arguments and results of the call
1249 * @return nt status code of execution
1252 NTSTATUS
libnet_DomainList(struct libnet_context
*ctx
, TALLOC_CTX
*mem_ctx
,
1253 struct libnet_DomainList
*io
)
1255 struct composite_context
*c
;
1257 c
= libnet_DomainList_send(ctx
, mem_ctx
, io
, NULL
);
1258 return libnet_DomainList_recv(c
, ctx
, mem_ctx
, io
);