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 tevent_req
*subreq
);
51 static void continue_domain_open_connect(struct tevent_req
*subreq
);
52 static void continue_domain_open_lookup(struct tevent_req
*subreq
);
53 static void continue_domain_open_open(struct tevent_req
*subreq
);
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 tevent_req
*subreq
;
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 subreq
= dcerpc_samr_Connect_r_send(s
, c
->event_ctx
,
80 s
->pipe
->binding_handle
,
82 if (composite_nomem(subreq
, c
)) return;
84 /* callback handler */
85 tevent_req_set_callback(subreq
, continue_domain_open_connect
, c
);
90 * Stage 0.5 (optional): Close existing (in libnet context) domain
93 static void continue_domain_open_close(struct tevent_req
*subreq
)
95 struct composite_context
*c
;
96 struct domain_open_samr_state
*s
;
98 c
= tevent_req_callback_data(subreq
, struct composite_context
);
99 s
= talloc_get_type(c
->private_data
, struct domain_open_samr_state
);
101 /* receive samr_Close reply */
102 c
->status
= dcerpc_samr_Close_r_recv(subreq
, s
);
104 if (!composite_is_ok(c
)) return;
107 struct monitor_msg msg
;
109 msg
.type
= mon_SamrClose
;
115 /* reset domain handle and associated data in libnet_context */
116 s
->ctx
->samr
.name
= NULL
;
117 s
->ctx
->samr
.access_mask
= 0;
118 ZERO_STRUCT(s
->ctx
->samr
.handle
);
120 /* preparing parameters for samr_Connect rpc call */
121 s
->connect
.in
.system_name
= 0;
122 s
->connect
.in
.access_mask
= s
->access_mask
;
123 s
->connect
.out
.connect_handle
= &s
->connect_handle
;
126 subreq
= dcerpc_samr_Connect_r_send(s
, c
->event_ctx
,
127 s
->pipe
->binding_handle
,
129 if (composite_nomem(subreq
, c
)) return;
131 /* callback handler */
132 tevent_req_set_callback(subreq
, continue_domain_open_connect
, c
);
137 * Stage 1: Connect to SAM server.
139 static void continue_domain_open_connect(struct tevent_req
*subreq
)
141 struct composite_context
*c
;
142 struct domain_open_samr_state
*s
;
143 struct samr_LookupDomain
*r
;
145 c
= tevent_req_callback_data(subreq
, struct composite_context
);
146 s
= talloc_get_type(c
->private_data
, struct domain_open_samr_state
);
148 /* receive samr_Connect reply */
149 c
->status
= dcerpc_samr_Connect_r_recv(subreq
, s
);
151 if (!composite_is_ok(c
)) return;
154 struct monitor_msg msg
;
156 msg
.type
= mon_SamrConnect
;
164 /* prepare for samr_LookupDomain call */
165 r
->in
.connect_handle
= &s
->connect_handle
;
166 r
->in
.domain_name
= &s
->domain_name
;
167 r
->out
.sid
= talloc(s
, struct dom_sid2
*);
168 if (composite_nomem(r
->out
.sid
, c
)) return;
170 subreq
= dcerpc_samr_LookupDomain_r_send(s
, c
->event_ctx
,
171 s
->pipe
->binding_handle
,
173 if (composite_nomem(subreq
, c
)) return;
175 tevent_req_set_callback(subreq
, continue_domain_open_lookup
, c
);
180 * Stage 2: Lookup domain by name.
182 static void continue_domain_open_lookup(struct tevent_req
*subreq
)
184 struct composite_context
*c
;
185 struct domain_open_samr_state
*s
;
186 struct samr_OpenDomain
*r
;
188 c
= tevent_req_callback_data(subreq
, struct composite_context
);
189 s
= talloc_get_type(c
->private_data
, struct domain_open_samr_state
);
191 /* receive samr_LookupDomain reply */
192 c
->status
= dcerpc_samr_LookupDomain_r_recv(subreq
, s
);
196 struct monitor_msg msg
;
197 struct msg_rpc_lookup_domain data
;
199 data
.domain_name
= s
->domain_name
.string
;
201 msg
.type
= mon_SamrLookupDomain
;
202 msg
.data
= (void*)&data
;
203 msg
.data_size
= sizeof(data
);
209 /* check the rpc layer status */
210 if (!composite_is_ok(c
));
212 /* check the rpc call itself status */
213 if (!NT_STATUS_IS_OK(s
->lookup
.out
.result
)) {
214 composite_error(c
, s
->lookup
.out
.result
);
218 /* prepare for samr_OpenDomain call */
219 r
->in
.connect_handle
= &s
->connect_handle
;
220 r
->in
.access_mask
= SEC_FLAG_MAXIMUM_ALLOWED
;
221 r
->in
.sid
= *s
->lookup
.out
.sid
;
222 r
->out
.domain_handle
= &s
->domain_handle
;
224 subreq
= dcerpc_samr_OpenDomain_r_send(s
, c
->event_ctx
,
225 s
->pipe
->binding_handle
,
227 if (composite_nomem(subreq
, c
)) return;
229 tevent_req_set_callback(subreq
, continue_domain_open_open
, c
);
234 * Stage 3: Open domain.
236 static void continue_domain_open_open(struct tevent_req
*subreq
)
238 struct composite_context
*c
;
239 struct domain_open_samr_state
*s
;
241 c
= tevent_req_callback_data(subreq
, struct composite_context
);
242 s
= talloc_get_type(c
->private_data
, struct domain_open_samr_state
);
244 /* receive samr_OpenDomain reply */
245 c
->status
= dcerpc_samr_OpenDomain_r_recv(subreq
, s
);
247 if (!composite_is_ok(c
)) return;
250 struct monitor_msg msg
;
252 msg
.type
= mon_SamrOpenDomain
;
263 * Sends asynchronous DomainOpenSamr request
265 * @param ctx initialised libnet context
266 * @param io arguments and results of the call
267 * @param monitor pointer to monitor function that is passed monitor message
270 struct composite_context
*libnet_DomainOpenSamr_send(struct libnet_context
*ctx
,
271 struct libnet_DomainOpen
*io
,
272 void (*monitor
)(struct monitor_msg
*))
274 struct composite_context
*c
;
275 struct domain_open_samr_state
*s
;
276 struct composite_context
*rpcconn_req
;
277 struct tevent_req
*subreq
;
279 c
= composite_create(ctx
, ctx
->event_ctx
);
280 if (c
== NULL
) return NULL
;
282 s
= talloc_zero(c
, struct domain_open_samr_state
);
283 if (composite_nomem(s
, c
)) return c
;
286 s
->monitor_fn
= monitor
;
289 s
->pipe
= ctx
->samr
.pipe
;
290 s
->access_mask
= io
->in
.access_mask
;
291 s
->domain_name
.string
= talloc_strdup(c
, io
->in
.domain_name
);
293 /* check, if there's samr pipe opened already, before opening a domain */
294 if (ctx
->samr
.pipe
== NULL
) {
296 /* attempting to connect a domain controller */
297 s
->rpcconn
.level
= LIBNET_RPC_CONNECT_DC
;
298 s
->rpcconn
.in
.name
= io
->in
.domain_name
;
299 s
->rpcconn
.in
.dcerpc_iface
= &ndr_table_samr
;
301 /* send rpc pipe connect request */
302 rpcconn_req
= libnet_RpcConnect_send(ctx
, c
, &s
->rpcconn
, s
->monitor_fn
);
303 if (composite_nomem(rpcconn_req
, c
)) return c
;
305 composite_continue(c
, rpcconn_req
, continue_domain_open_rpc_connect
, c
);
309 /* libnet context's domain handle is not empty, so check out what
310 was opened first, before doing anything */
311 if (!policy_handle_empty(&ctx
->samr
.handle
)) {
312 if (strequal(ctx
->samr
.name
, io
->in
.domain_name
) &&
313 ctx
->samr
.access_mask
== io
->in
.access_mask
) {
315 /* this domain is already opened */
320 /* another domain or access rights have been
321 requested - close the existing handle first */
322 s
->close
.in
.handle
= &ctx
->samr
.handle
;
324 /* send request to close domain handle */
325 subreq
= dcerpc_samr_Close_r_send(s
, c
->event_ctx
,
326 s
->pipe
->binding_handle
,
328 if (composite_nomem(subreq
, c
)) return c
;
330 /* callback handler */
331 tevent_req_set_callback(subreq
, continue_domain_open_close
, c
);
336 /* preparing parameters for samr_Connect rpc call */
337 s
->connect
.in
.system_name
= 0;
338 s
->connect
.in
.access_mask
= s
->access_mask
;
339 s
->connect
.out
.connect_handle
= &s
->connect_handle
;
342 subreq
= dcerpc_samr_Connect_r_send(s
, c
->event_ctx
,
343 s
->pipe
->binding_handle
,
345 if (composite_nomem(subreq
, c
)) return c
;
347 /* callback handler */
348 tevent_req_set_callback(subreq
, continue_domain_open_connect
, c
);
354 * Waits for and receives result of asynchronous DomainOpenSamr call
356 * @param c composite context returned by asynchronous DomainOpen call
357 * @param ctx initialised libnet context
358 * @param mem_ctx memory context of the call
359 * @param io pointer to results (and arguments) of the call
360 * @return nt status code of execution
363 NTSTATUS
libnet_DomainOpenSamr_recv(struct composite_context
*c
, struct libnet_context
*ctx
,
364 TALLOC_CTX
*mem_ctx
, struct libnet_DomainOpen
*io
)
367 struct domain_open_samr_state
*s
;
369 /* wait for results of sending request */
370 status
= composite_wait(c
);
372 if (NT_STATUS_IS_OK(status
) && io
) {
373 s
= talloc_get_type(c
->private_data
, struct domain_open_samr_state
);
374 io
->out
.domain_handle
= s
->domain_handle
;
376 /* store the resulting handle and related data for use by other
378 ctx
->samr
.connect_handle
= s
->connect_handle
;
379 ctx
->samr
.handle
= s
->domain_handle
;
380 ctx
->samr
.sid
= talloc_steal(ctx
, *s
->lookup
.out
.sid
);
381 ctx
->samr
.name
= talloc_steal(ctx
, s
->domain_name
.string
);
382 ctx
->samr
.access_mask
= s
->access_mask
;
390 struct domain_open_lsa_state
{
392 uint32_t access_mask
;
393 struct libnet_context
*ctx
;
394 struct libnet_RpcConnect rpcconn
;
395 struct lsa_OpenPolicy2 openpol
;
396 struct policy_handle handle
;
397 struct dcerpc_pipe
*pipe
;
399 /* information about the progress */
400 void (*monitor_fn
)(struct monitor_msg
*);
404 static void continue_rpc_connect_lsa(struct composite_context
*ctx
);
405 static void continue_lsa_policy_open(struct tevent_req
*subreq
);
409 * Sends asynchronous DomainOpenLsa request
411 * @param ctx initialised libnet context
412 * @param io arguments and results of the call
413 * @param monitor pointer to monitor function that is passed monitor message
416 struct composite_context
* libnet_DomainOpenLsa_send(struct libnet_context
*ctx
,
417 struct libnet_DomainOpen
*io
,
418 void (*monitor
)(struct monitor_msg
*))
420 struct composite_context
*c
;
421 struct domain_open_lsa_state
*s
;
422 struct composite_context
*rpcconn_req
;
423 struct tevent_req
*subreq
;
424 struct lsa_QosInfo
*qos
;
426 /* create composite context and state */
427 c
= composite_create(ctx
, ctx
->event_ctx
);
428 if (c
== NULL
) return c
;
430 s
= talloc_zero(c
, struct domain_open_lsa_state
);
431 if (composite_nomem(s
, c
)) return c
;
435 /* store arguments in the state structure */
436 s
->name
= talloc_strdup(c
, io
->in
.domain_name
);
437 s
->access_mask
= io
->in
.access_mask
;
440 /* check, if there's lsa pipe opened already, before opening a handle */
441 if (ctx
->lsa
.pipe
== NULL
) {
443 ZERO_STRUCT(s
->rpcconn
);
445 /* attempting to connect a domain controller */
446 s
->rpcconn
.level
= LIBNET_RPC_CONNECT_DC
;
447 s
->rpcconn
.in
.name
= talloc_strdup(c
, io
->in
.domain_name
);
448 s
->rpcconn
.in
.dcerpc_iface
= &ndr_table_lsarpc
;
450 /* send rpc pipe connect request */
451 rpcconn_req
= libnet_RpcConnect_send(ctx
, c
, &s
->rpcconn
, s
->monitor_fn
);
452 if (composite_nomem(rpcconn_req
, c
)) return c
;
454 composite_continue(c
, rpcconn_req
, continue_rpc_connect_lsa
, c
);
458 s
->pipe
= ctx
->lsa
.pipe
;
460 /* preparing parameters for lsa_OpenPolicy2 rpc call */
461 s
->openpol
.in
.system_name
= s
->name
;
462 s
->openpol
.in
.access_mask
= s
->access_mask
;
463 s
->openpol
.in
.attr
= talloc_zero(c
, struct lsa_ObjectAttribute
);
465 qos
= talloc_zero(c
, struct lsa_QosInfo
);
467 qos
->impersonation_level
= 2;
468 qos
->context_mode
= 1;
469 qos
->effective_only
= 0;
471 s
->openpol
.in
.attr
->sec_qos
= qos
;
472 s
->openpol
.out
.handle
= &s
->handle
;
474 /* send rpc request */
475 subreq
= dcerpc_lsa_OpenPolicy2_r_send(s
, c
->event_ctx
,
476 s
->pipe
->binding_handle
,
478 if (composite_nomem(subreq
, c
)) return c
;
480 tevent_req_set_callback(subreq
, continue_lsa_policy_open
, c
);
486 Stage 0.5 (optional): Rpc pipe connected, send lsa open policy request
488 static void continue_rpc_connect_lsa(struct composite_context
*ctx
)
490 struct composite_context
*c
;
491 struct domain_open_lsa_state
*s
;
492 struct lsa_QosInfo
*qos
;
493 struct tevent_req
*subreq
;
495 c
= talloc_get_type(ctx
->async
.private_data
, struct composite_context
);
496 s
= talloc_get_type(c
->private_data
, struct domain_open_lsa_state
);
498 /* receive rpc connection */
499 c
->status
= libnet_RpcConnect_recv(ctx
, s
->ctx
, c
, &s
->rpcconn
);
500 if (!composite_is_ok(c
)) return;
502 /* RpcConnect function leaves the pipe in libnet context,
503 so get it from there */
504 s
->pipe
= s
->ctx
->lsa
.pipe
;
506 /* prepare lsa_OpenPolicy2 call */
507 s
->openpol
.in
.system_name
= s
->name
;
508 s
->openpol
.in
.access_mask
= s
->access_mask
;
509 s
->openpol
.in
.attr
= talloc_zero(c
, struct lsa_ObjectAttribute
);
511 qos
= talloc_zero(c
, struct lsa_QosInfo
);
513 qos
->impersonation_level
= 2;
514 qos
->context_mode
= 1;
515 qos
->effective_only
= 0;
517 s
->openpol
.in
.attr
->sec_qos
= qos
;
518 s
->openpol
.out
.handle
= &s
->handle
;
520 /* send rpc request */
521 subreq
= dcerpc_lsa_OpenPolicy2_r_send(s
, c
->event_ctx
,
522 s
->pipe
->binding_handle
,
524 if (composite_nomem(subreq
, c
)) return;
526 tevent_req_set_callback(subreq
, continue_lsa_policy_open
, c
);
531 Stage 1: Lsa policy opened - we're done, if successfully
533 static void continue_lsa_policy_open(struct tevent_req
*subreq
)
535 struct composite_context
*c
;
536 struct domain_open_lsa_state
*s
;
538 c
= tevent_req_callback_data(subreq
, struct composite_context
);
539 s
= talloc_get_type(c
->private_data
, struct domain_open_lsa_state
);
541 c
->status
= dcerpc_lsa_OpenPolicy2_r_recv(subreq
, s
);
543 if (!composite_is_ok(c
)) return;
546 struct monitor_msg msg
;
548 msg
.type
= mon_LsaOpenPolicy
;
559 * Receives result of asynchronous DomainOpenLsa call
561 * @param c composite context returned by asynchronous DomainOpenLsa call
562 * @param ctx initialised libnet context
563 * @param mem_ctx memory context of the call
564 * @param io pointer to results (and arguments) of the call
565 * @return nt status code of execution
568 NTSTATUS
libnet_DomainOpenLsa_recv(struct composite_context
*c
, struct libnet_context
*ctx
,
569 TALLOC_CTX
*mem_ctx
, struct libnet_DomainOpen
*io
)
572 struct domain_open_lsa_state
*s
;
574 status
= composite_wait(c
);
576 if (NT_STATUS_IS_OK(status
) && io
) {
577 /* everything went fine - get the results and
578 return the error string */
579 s
= talloc_get_type(c
->private_data
, struct domain_open_lsa_state
);
580 io
->out
.domain_handle
= s
->handle
;
582 ctx
->lsa
.handle
= s
->handle
;
583 ctx
->lsa
.name
= talloc_steal(ctx
, s
->name
);
584 ctx
->lsa
.access_mask
= s
->access_mask
;
586 io
->out
.error_string
= talloc_strdup(mem_ctx
, "Success");
588 } else if (!NT_STATUS_IS_OK(status
)) {
589 /* there was an error, so provide nt status code description */
590 io
->out
.error_string
= talloc_asprintf(mem_ctx
,
591 "Failed to open domain: %s",
601 * Sends a request to open a domain in desired service
603 * @param ctx initalised libnet context
604 * @param io arguments and results of the call
605 * @param monitor pointer to monitor function that is passed monitor message
608 struct composite_context
* libnet_DomainOpen_send(struct libnet_context
*ctx
,
609 struct libnet_DomainOpen
*io
,
610 void (*monitor
)(struct monitor_msg
*))
612 struct composite_context
*c
;
614 switch (io
->in
.type
) {
616 /* reques to open a policy handle on \pipe\lsarpc */
617 c
= libnet_DomainOpenLsa_send(ctx
, io
, monitor
);
622 /* request to open a domain policy handle on \pipe\samr */
623 c
= libnet_DomainOpenSamr_send(ctx
, io
, monitor
);
632 * Receive result of domain open request
634 * @param c composite context returned by DomainOpen_send function
635 * @param ctx initialised libnet context
636 * @param mem_ctx memory context of the call
637 * @param io results and arguments of the call
640 NTSTATUS
libnet_DomainOpen_recv(struct composite_context
*c
, struct libnet_context
*ctx
,
641 TALLOC_CTX
*mem_ctx
, struct libnet_DomainOpen
*io
)
645 switch (io
->in
.type
) {
647 status
= libnet_DomainOpenLsa_recv(c
, ctx
, mem_ctx
, io
);
652 status
= libnet_DomainOpenSamr_recv(c
, ctx
, mem_ctx
, io
);
661 * Synchronous version of DomainOpen call
663 * @param ctx initialised libnet context
664 * @param mem_ctx memory context for the call
665 * @param io arguments and results of the call
666 * @return nt status code of execution
669 NTSTATUS
libnet_DomainOpen(struct libnet_context
*ctx
,
671 struct libnet_DomainOpen
*io
)
673 struct composite_context
*c
= libnet_DomainOpen_send(ctx
, io
, NULL
);
674 return libnet_DomainOpen_recv(c
, ctx
, mem_ctx
, io
);
678 struct domain_close_lsa_state
{
679 struct dcerpc_pipe
*pipe
;
680 struct lsa_Close close
;
681 struct policy_handle handle
;
683 void (*monitor_fn
)(struct monitor_msg
*);
687 static void continue_lsa_close(struct tevent_req
*subreq
);
690 struct composite_context
* libnet_DomainCloseLsa_send(struct libnet_context
*ctx
,
691 struct libnet_DomainClose
*io
,
692 void (*monitor
)(struct monitor_msg
*))
694 struct composite_context
*c
;
695 struct domain_close_lsa_state
*s
;
696 struct tevent_req
*subreq
;
698 /* composite context and state structure allocation */
699 c
= composite_create(ctx
, ctx
->event_ctx
);
700 if (c
== NULL
) return c
;
702 s
= talloc_zero(c
, struct domain_close_lsa_state
);
703 if (composite_nomem(s
, c
)) return c
;
706 s
->monitor_fn
= monitor
;
708 /* TODO: check if lsa pipe pointer is non-null */
710 if (!strequal(ctx
->lsa
.name
, io
->in
.domain_name
)) {
711 composite_error(c
, NT_STATUS_INVALID_PARAMETER
);
715 /* get opened lsarpc pipe pointer */
716 s
->pipe
= ctx
->lsa
.pipe
;
718 /* prepare close handle call arguments */
719 s
->close
.in
.handle
= &ctx
->lsa
.handle
;
720 s
->close
.out
.handle
= &s
->handle
;
722 /* send the request */
723 subreq
= dcerpc_lsa_Close_r_send(s
, c
->event_ctx
,
724 s
->pipe
->binding_handle
,
726 if (composite_nomem(subreq
, c
)) return c
;
728 tevent_req_set_callback(subreq
, continue_lsa_close
, c
);
734 Stage 1: Receive result of lsa close call
736 static void continue_lsa_close(struct tevent_req
*subreq
)
738 struct composite_context
*c
;
739 struct domain_close_lsa_state
*s
;
741 c
= tevent_req_callback_data(subreq
, struct composite_context
);
742 s
= talloc_get_type(c
->private_data
, struct domain_close_lsa_state
);
744 c
->status
= dcerpc_lsa_Close_r_recv(subreq
, s
);
746 if (!composite_is_ok(c
)) return;
749 struct monitor_msg msg
;
751 msg
.type
= mon_LsaClose
;
761 NTSTATUS
libnet_DomainCloseLsa_recv(struct composite_context
*c
, struct libnet_context
*ctx
,
762 TALLOC_CTX
*mem_ctx
, struct libnet_DomainClose
*io
)
766 status
= composite_wait(c
);
768 if (NT_STATUS_IS_OK(status
) && io
) {
769 /* policy handle closed successfully */
771 ctx
->lsa
.name
= NULL
;
772 ZERO_STRUCT(ctx
->lsa
.handle
);
774 io
->out
.error_string
= talloc_asprintf(mem_ctx
, "Success");
776 } else if (!NT_STATUS_IS_OK(status
)) {
777 /* there was an error, so return description of the status code */
778 io
->out
.error_string
= talloc_asprintf(mem_ctx
, "Error: %s", nt_errstr(status
));
786 struct domain_close_samr_state
{
787 struct samr_Close close
;
788 struct policy_handle handle
;
790 void (*monitor_fn
)(struct monitor_msg
*);
794 static void continue_samr_close(struct tevent_req
*subreq
);
797 struct composite_context
* libnet_DomainCloseSamr_send(struct libnet_context
*ctx
,
798 struct libnet_DomainClose
*io
,
799 void (*monitor
)(struct monitor_msg
*))
801 struct composite_context
*c
;
802 struct domain_close_samr_state
*s
;
803 struct tevent_req
*subreq
;
805 /* composite context and state structure allocation */
806 c
= composite_create(ctx
, ctx
->event_ctx
);
807 if (c
== NULL
) return c
;
809 s
= talloc_zero(c
, struct domain_close_samr_state
);
810 if (composite_nomem(s
, c
)) return c
;
813 s
->monitor_fn
= monitor
;
815 /* TODO: check if samr pipe pointer is non-null */
817 if (!strequal(ctx
->samr
.name
, io
->in
.domain_name
)) {
818 composite_error(c
, NT_STATUS_INVALID_PARAMETER
);
822 /* prepare close domain handle call arguments */
823 ZERO_STRUCT(s
->close
);
824 s
->close
.in
.handle
= &ctx
->samr
.handle
;
825 s
->close
.out
.handle
= &s
->handle
;
827 /* send the request */
828 subreq
= dcerpc_samr_Close_r_send(s
, c
->event_ctx
,
829 ctx
->samr
.pipe
->binding_handle
,
831 if (composite_nomem(subreq
, c
)) return c
;
833 tevent_req_set_callback(subreq
, continue_samr_close
, c
);
839 Stage 1: Receive result of samr close call
841 static void continue_samr_close(struct tevent_req
*subreq
)
843 struct composite_context
*c
;
844 struct domain_close_samr_state
*s
;
846 c
= tevent_req_callback_data(subreq
, struct composite_context
);
847 s
= talloc_get_type(c
->private_data
, struct domain_close_samr_state
);
849 c
->status
= dcerpc_samr_Close_r_recv(subreq
, s
);
851 if (!composite_is_ok(c
)) return;
854 struct monitor_msg msg
;
856 msg
.type
= mon_SamrClose
;
866 NTSTATUS
libnet_DomainCloseSamr_recv(struct composite_context
*c
, struct libnet_context
*ctx
,
867 TALLOC_CTX
*mem_ctx
, struct libnet_DomainClose
*io
)
871 status
= composite_wait(c
);
873 if (NT_STATUS_IS_OK(status
) && io
) {
874 /* domain policy handle closed successfully */
876 ZERO_STRUCT(ctx
->samr
.handle
);
877 talloc_free(discard_const_p(char, ctx
->samr
.name
));
878 talloc_free(ctx
->samr
.sid
);
879 ctx
->samr
.name
= NULL
;
880 ctx
->samr
.sid
= NULL
;
882 io
->out
.error_string
= talloc_asprintf(mem_ctx
, "Success");
884 } else if (!NT_STATUS_IS_OK(status
)) {
885 /* there was an error, so return description of the status code */
886 io
->out
.error_string
= talloc_asprintf(mem_ctx
, "Error: %s", nt_errstr(status
));
894 struct composite_context
* libnet_DomainClose_send(struct libnet_context
*ctx
,
895 struct libnet_DomainClose
*io
,
896 void (*monitor
)(struct monitor_msg
*))
898 struct composite_context
*c
;
900 switch (io
->in
.type
) {
902 /* request to close policy handle on \pipe\lsarpc */
903 c
= libnet_DomainCloseLsa_send(ctx
, io
, monitor
);
908 /* request to close domain policy handle on \pipe\samr */
909 c
= libnet_DomainCloseSamr_send(ctx
, io
, monitor
);
917 NTSTATUS
libnet_DomainClose_recv(struct composite_context
*c
, struct libnet_context
*ctx
,
918 TALLOC_CTX
*mem_ctx
, struct libnet_DomainClose
*io
)
922 switch (io
->in
.type
) {
924 /* receive result of closing lsa policy handle */
925 status
= libnet_DomainCloseLsa_recv(c
, ctx
, mem_ctx
, io
);
930 /* receive result of closing samr domain policy handle */
931 status
= libnet_DomainCloseSamr_recv(c
, ctx
, mem_ctx
, io
);
939 NTSTATUS
libnet_DomainClose(struct libnet_context
*ctx
, TALLOC_CTX
*mem_ctx
,
940 struct libnet_DomainClose
*io
)
942 struct composite_context
*c
;
944 c
= libnet_DomainClose_send(ctx
, io
, NULL
);
945 return libnet_DomainClose_recv(c
, ctx
, mem_ctx
, io
);
949 struct domain_list_state
{
950 struct libnet_context
*ctx
;
951 struct libnet_RpcConnect rpcconn
;
952 struct samr_Connect samrconn
;
953 struct samr_EnumDomains enumdom
;
954 struct samr_Close samrclose
;
955 const char *hostname
;
956 struct policy_handle connect_handle
;
958 struct domainlist
*domains
;
959 uint32_t resume_handle
;
962 void (*monitor_fn
)(struct monitor_msg
*);
966 static void continue_rpc_connect(struct composite_context
*c
);
967 static void continue_samr_connect(struct tevent_req
*subreq
);
968 static void continue_samr_enum_domains(struct tevent_req
*subreq
);
969 static void continue_samr_close_handle(struct tevent_req
*subreq
);
971 static struct domainlist
* get_domain_list(TALLOC_CTX
*mem_ctx
, struct domain_list_state
*s
);
975 Stage 1: Receive connected rpc pipe and send connection
976 request to SAMR service
978 static void continue_rpc_connect(struct composite_context
*ctx
)
980 struct composite_context
*c
;
981 struct domain_list_state
*s
;
982 struct tevent_req
*subreq
;
984 c
= talloc_get_type(ctx
->async
.private_data
, struct composite_context
);
985 s
= talloc_get_type(c
->private_data
, struct domain_list_state
);
987 c
->status
= libnet_RpcConnect_recv(ctx
, s
->ctx
, c
, &s
->rpcconn
);
988 if (!composite_is_ok(c
)) return;
990 s
->samrconn
.in
.system_name
= 0;
991 s
->samrconn
.in
.access_mask
= SEC_GENERIC_READ
; /* should be enough */
992 s
->samrconn
.out
.connect_handle
= &s
->connect_handle
;
994 subreq
= dcerpc_samr_Connect_r_send(s
, c
->event_ctx
,
995 s
->ctx
->samr
.pipe
->binding_handle
,
997 if (composite_nomem(subreq
, c
)) return;
999 tevent_req_set_callback(subreq
, continue_samr_connect
, c
);
1004 Stage 2: Receive policy handle to the connected SAMR service and issue
1005 a request to enumerate domain databases available
1007 static void continue_samr_connect(struct tevent_req
*subreq
)
1009 struct composite_context
*c
;
1010 struct domain_list_state
*s
;
1012 c
= tevent_req_callback_data(subreq
, struct composite_context
);
1013 s
= talloc_get_type(c
->private_data
, struct domain_list_state
);
1015 c
->status
= dcerpc_samr_Connect_r_recv(subreq
, s
);
1016 TALLOC_FREE(subreq
);
1017 if (!composite_is_ok(c
)) return;
1019 if (s
->monitor_fn
) {
1020 struct monitor_msg msg
;
1022 msg
.type
= mon_SamrConnect
;
1025 s
->monitor_fn(&msg
);
1028 s
->enumdom
.in
.connect_handle
= &s
->connect_handle
;
1029 s
->enumdom
.in
.resume_handle
= &s
->resume_handle
;
1030 s
->enumdom
.in
.buf_size
= s
->buf_size
;
1031 s
->enumdom
.out
.resume_handle
= &s
->resume_handle
;
1032 s
->enumdom
.out
.num_entries
= talloc(s
, uint32_t);
1033 if (composite_nomem(s
->enumdom
.out
.num_entries
, c
)) return;
1034 s
->enumdom
.out
.sam
= talloc(s
, struct samr_SamArray
*);
1035 if (composite_nomem(s
->enumdom
.out
.sam
, c
)) return;
1037 subreq
= dcerpc_samr_EnumDomains_r_send(s
, c
->event_ctx
,
1038 s
->ctx
->samr
.pipe
->binding_handle
,
1040 if (composite_nomem(subreq
, c
)) return;
1042 tevent_req_set_callback(subreq
, continue_samr_enum_domains
, c
);
1047 Stage 3: Receive domain names available and repeat the request
1048 enumeration is not complete yet. Close samr connection handle
1051 static void continue_samr_enum_domains(struct tevent_req
*subreq
)
1053 struct composite_context
*c
;
1054 struct domain_list_state
*s
;
1056 c
= tevent_req_callback_data(subreq
, struct composite_context
);
1057 s
= talloc_get_type(c
->private_data
, struct domain_list_state
);
1059 c
->status
= dcerpc_samr_EnumDomains_r_recv(subreq
, s
);
1060 TALLOC_FREE(subreq
);
1061 if (!composite_is_ok(c
)) return;
1063 if (s
->monitor_fn
) {
1064 struct monitor_msg msg
;
1066 msg
.type
= mon_SamrEnumDomains
;
1069 s
->monitor_fn(&msg
);
1072 if (NT_STATUS_IS_OK(s
->enumdom
.out
.result
)) {
1074 s
->domains
= get_domain_list(c
, s
);
1076 } else if (NT_STATUS_EQUAL(s
->enumdom
.out
.result
, STATUS_MORE_ENTRIES
)) {
1078 s
->domains
= get_domain_list(c
, s
);
1080 /* prepare next round of enumeration */
1081 s
->enumdom
.in
.connect_handle
= &s
->connect_handle
;
1082 s
->enumdom
.in
.resume_handle
= &s
->resume_handle
;
1083 s
->enumdom
.in
.buf_size
= s
->ctx
->samr
.buf_size
;
1084 s
->enumdom
.out
.resume_handle
= &s
->resume_handle
;
1086 /* send the request */
1087 subreq
= dcerpc_samr_EnumDomains_r_send(s
, c
->event_ctx
,
1088 s
->ctx
->samr
.pipe
->binding_handle
,
1090 if (composite_nomem(subreq
, c
)) return;
1092 tevent_req_set_callback(subreq
, continue_samr_enum_domains
, c
);
1095 composite_error(c
, s
->enumdom
.out
.result
);
1099 /* close samr connection handle */
1100 s
->samrclose
.in
.handle
= &s
->connect_handle
;
1101 s
->samrclose
.out
.handle
= &s
->connect_handle
;
1103 /* send the request */
1104 subreq
= dcerpc_samr_Close_r_send(s
, c
->event_ctx
,
1105 s
->ctx
->samr
.pipe
->binding_handle
,
1107 if (composite_nomem(subreq
, c
)) return;
1109 tevent_req_set_callback(subreq
, continue_samr_close_handle
, c
);
1114 Stage 4: Receive result of closing samr connection handle.
1116 static void continue_samr_close_handle(struct tevent_req
*subreq
)
1118 struct composite_context
*c
;
1119 struct domain_list_state
*s
;
1121 c
= tevent_req_callback_data(subreq
, struct composite_context
);
1122 s
= talloc_get_type(c
->private_data
, struct domain_list_state
);
1124 c
->status
= dcerpc_samr_Close_r_recv(subreq
, s
);
1125 TALLOC_FREE(subreq
);
1126 if (!composite_is_ok(c
)) return;
1128 if (s
->monitor_fn
) {
1129 struct monitor_msg msg
;
1131 msg
.type
= mon_SamrClose
;
1134 s
->monitor_fn(&msg
);
1137 /* did everything go fine ? */
1138 if (!NT_STATUS_IS_OK(s
->samrclose
.out
.result
)) {
1139 composite_error(c
, s
->samrclose
.out
.result
);
1147 Utility function to copy domain names from result of samr_EnumDomains call
1149 static struct domainlist
* get_domain_list(TALLOC_CTX
*mem_ctx
, struct domain_list_state
*s
)
1152 if (mem_ctx
== NULL
|| s
== NULL
) return NULL
;
1154 /* prepare domains array */
1155 if (s
->domains
== NULL
) {
1156 s
->domains
= talloc_array(mem_ctx
, struct domainlist
,
1157 *s
->enumdom
.out
.num_entries
);
1159 s
->domains
= talloc_realloc(mem_ctx
, s
->domains
, struct domainlist
,
1160 s
->count
+ *s
->enumdom
.out
.num_entries
);
1163 /* copy domain names returned from samr_EnumDomains call */
1164 for (i
= s
->count
; i
< s
->count
+ *s
->enumdom
.out
.num_entries
; i
++)
1166 struct lsa_String
*domain_name
= &(*s
->enumdom
.out
.sam
)->entries
[i
- s
->count
].name
;
1168 /* strdup name as a child of allocated array to make it follow the array
1169 in case of talloc_steal or talloc_free */
1170 s
->domains
[i
].name
= talloc_strdup(s
->domains
, domain_name
->string
);
1171 s
->domains
[i
].sid
= NULL
; /* this is to be filled out later */
1174 /* number of entries returned (domains enumerated) */
1175 s
->count
+= *s
->enumdom
.out
.num_entries
;
1182 * Sends a request to list domains on given host
1184 * @param ctx initalised libnet context
1185 * @param mem_ctx memory context
1186 * @param io arguments and results of the call
1187 * @param monitor pointer to monitor function that is passed monitor messages
1190 struct composite_context
* libnet_DomainList_send(struct libnet_context
*ctx
,
1191 TALLOC_CTX
*mem_ctx
,
1192 struct libnet_DomainList
*io
,
1193 void (*monitor
)(struct monitor_msg
*))
1195 struct composite_context
*c
;
1196 struct domain_list_state
*s
;
1197 struct composite_context
*rpcconn_req
;
1198 struct tevent_req
*subreq
;
1200 /* composite context and state structure allocation */
1201 c
= composite_create(ctx
, ctx
->event_ctx
);
1202 if (c
== NULL
) return c
;
1204 s
= talloc_zero(c
, struct domain_list_state
);
1205 if (composite_nomem(s
, c
)) return c
;
1207 c
->private_data
= s
;
1208 s
->monitor_fn
= monitor
;
1211 s
->hostname
= talloc_strdup(c
, io
->in
.hostname
);
1212 if (composite_nomem(s
->hostname
, c
)) return c
;
1214 /* check whether samr pipe has already been opened */
1215 if (ctx
->samr
.pipe
== NULL
) {
1216 ZERO_STRUCT(s
->rpcconn
);
1218 /* prepare rpc connect call */
1219 s
->rpcconn
.level
= LIBNET_RPC_CONNECT_SERVER
;
1220 s
->rpcconn
.in
.name
= s
->hostname
;
1221 s
->rpcconn
.in
.dcerpc_iface
= &ndr_table_samr
;
1223 rpcconn_req
= libnet_RpcConnect_send(ctx
, c
, &s
->rpcconn
, s
->monitor_fn
);
1224 if (composite_nomem(rpcconn_req
, c
)) return c
;
1226 composite_continue(c
, rpcconn_req
, continue_rpc_connect
, c
);
1229 /* prepare samr_Connect call */
1230 s
->samrconn
.in
.system_name
= 0;
1231 s
->samrconn
.in
.access_mask
= SEC_GENERIC_READ
;
1232 s
->samrconn
.out
.connect_handle
= &s
->connect_handle
;
1234 subreq
= dcerpc_samr_Connect_r_send(s
, c
->event_ctx
,
1235 s
->ctx
->samr
.pipe
->binding_handle
,
1237 if (composite_nomem(subreq
, c
)) return c
;
1239 tevent_req_set_callback(subreq
, continue_samr_connect
, c
);
1247 * Receive result of domain list request
1249 * @param c composite context returned by DomainList_send function
1250 * @param ctx initialised libnet context
1251 * @param mem_ctx memory context of the call
1252 * @param io results and arguments of the call
1255 NTSTATUS
libnet_DomainList_recv(struct composite_context
*c
, struct libnet_context
*ctx
,
1256 TALLOC_CTX
*mem_ctx
, struct libnet_DomainList
*io
)
1259 struct domain_list_state
*s
;
1261 status
= composite_wait(c
);
1263 s
= talloc_get_type(c
->private_data
, struct domain_list_state
);
1265 if (NT_STATUS_IS_OK(status
) && ctx
&& mem_ctx
&& io
) {
1266 /* fetch the results to be returned by io structure */
1267 io
->out
.count
= s
->count
;
1268 io
->out
.domains
= talloc_steal(mem_ctx
, s
->domains
);
1269 io
->out
.error_string
= talloc_asprintf(mem_ctx
, "Success");
1271 } else if (!NT_STATUS_IS_OK(status
)) {
1272 /* there was an error, so return description of the status code */
1273 io
->out
.error_string
= talloc_asprintf(mem_ctx
, "Error: %s", nt_errstr(status
));
1282 * Synchronous version of DomainList call
1284 * @param ctx initialised libnet context
1285 * @param mem_ctx memory context for the call
1286 * @param io arguments and results of the call
1287 * @return nt status code of execution
1290 NTSTATUS
libnet_DomainList(struct libnet_context
*ctx
, TALLOC_CTX
*mem_ctx
,
1291 struct libnet_DomainList
*io
)
1293 struct composite_context
*c
;
1295 c
= libnet_DomainList_send(ctx
, mem_ctx
, io
, NULL
);
1296 return libnet_DomainList_recv(c
, ctx
, mem_ctx
, io
);