s3 swat: Add support for anti-XSRF token
[Samba.git] / source4 / libnet / libnet_domain.c
blob0fafe7125005e9898b8e5afadf3ba4909ff2a435
1 /*
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
24 #include "includes.h"
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;
40 uint32_t access_mask;
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);
56 /**
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;
78 /* send request */
79 subreq = dcerpc_samr_Connect_r_send(s, c->event_ctx,
80 s->pipe->binding_handle,
81 &s->connect);
82 if (composite_nomem(subreq, c)) return;
84 /* callback handler */
85 tevent_req_set_callback(subreq, continue_domain_open_connect, c);
89 /**
90 * Stage 0.5 (optional): Close existing (in libnet context) domain
91 * handle
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);
103 TALLOC_FREE(subreq);
104 if (!composite_is_ok(c)) return;
106 if (s->monitor_fn) {
107 struct monitor_msg msg;
109 msg.type = mon_SamrClose;
110 msg.data = NULL;
111 msg.data_size = 0;
112 s->monitor_fn(&msg);
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;
125 /* send request */
126 subreq = dcerpc_samr_Connect_r_send(s, c->event_ctx,
127 s->pipe->binding_handle,
128 &s->connect);
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);
150 TALLOC_FREE(subreq);
151 if (!composite_is_ok(c)) return;
153 if (s->monitor_fn) {
154 struct monitor_msg msg;
156 msg.type = mon_SamrConnect;
157 msg.data = NULL;
158 msg.data_size = 0;
159 s->monitor_fn(&msg);
162 r = &s->lookup;
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);
193 TALLOC_FREE(subreq);
195 if (s->monitor_fn) {
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);
204 s->monitor_fn(&msg);
207 r = &s->open;
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);
215 return;
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);
246 TALLOC_FREE(subreq);
247 if (!composite_is_ok(c)) return;
249 if (s->monitor_fn) {
250 struct monitor_msg msg;
252 msg.type = mon_SamrOpenDomain;
253 msg.data = NULL;
254 msg.data_size = 0;
255 s->monitor_fn(&msg);
258 composite_done(c);
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;
285 c->private_data = s;
286 s->monitor_fn = monitor;
288 s->ctx = ctx;
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);
306 return 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 */
316 composite_done(c);
317 return c;
319 } else {
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,
327 &s->close);
328 if (composite_nomem(subreq, c)) return c;
330 /* callback handler */
331 tevent_req_set_callback(subreq, continue_domain_open_close, c);
332 return 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;
341 /* send request */
342 subreq = dcerpc_samr_Connect_r_send(s, c->event_ctx,
343 s->pipe->binding_handle,
344 &s->connect);
345 if (composite_nomem(subreq, c)) return c;
347 /* callback handler */
348 tevent_req_set_callback(subreq, continue_domain_open_connect, c);
349 return 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)
366 NTSTATUS status;
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
377 libnet functions */
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;
385 talloc_free(c);
386 return status;
390 struct domain_open_lsa_state {
391 const char *name;
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;
433 c->private_data = s;
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;
438 s->ctx = ctx;
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);
455 return 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);
466 qos->len = 0;
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,
477 &s->openpol);
478 if (composite_nomem(subreq, c)) return c;
480 tevent_req_set_callback(subreq, continue_lsa_policy_open, c);
481 return 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);
512 qos->len = 0;
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,
523 &s->openpol);
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);
542 TALLOC_FREE(subreq);
543 if (!composite_is_ok(c)) return;
545 if (s->monitor_fn) {
546 struct monitor_msg msg;
548 msg.type = mon_LsaOpenPolicy;
549 msg.data = NULL;
550 msg.data_size = 0;
551 s->monitor_fn(&msg);
554 composite_done(c);
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)
571 NTSTATUS status;
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",
592 nt_errstr(status));
595 talloc_free(c);
596 return status;
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) {
615 case DOMAIN_LSA:
616 /* reques to open a policy handle on \pipe\lsarpc */
617 c = libnet_DomainOpenLsa_send(ctx, io, monitor);
618 break;
620 case DOMAIN_SAMR:
621 default:
622 /* request to open a domain policy handle on \pipe\samr */
623 c = libnet_DomainOpenSamr_send(ctx, io, monitor);
624 break;
627 return c;
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)
643 NTSTATUS status;
645 switch (io->in.type) {
646 case DOMAIN_LSA:
647 status = libnet_DomainOpenLsa_recv(c, ctx, mem_ctx, io);
648 break;
650 case DOMAIN_SAMR:
651 default:
652 status = libnet_DomainOpenSamr_recv(c, ctx, mem_ctx, io);
653 break;
656 return status;
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,
670 TALLOC_CTX *mem_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;
705 c->private_data = s;
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);
712 return c;
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,
725 &s->close);
726 if (composite_nomem(subreq, c)) return c;
728 tevent_req_set_callback(subreq, continue_lsa_close, c);
729 return 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);
745 TALLOC_FREE(subreq);
746 if (!composite_is_ok(c)) return;
748 if (s->monitor_fn) {
749 struct monitor_msg msg;
751 msg.type = mon_LsaClose;
752 msg.data = NULL;
753 msg.data_size = 0;
754 s->monitor_fn(&msg);
757 composite_done(c);
761 NTSTATUS libnet_DomainCloseLsa_recv(struct composite_context *c, struct libnet_context *ctx,
762 TALLOC_CTX *mem_ctx, struct libnet_DomainClose *io)
764 NTSTATUS status;
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));
781 talloc_free(c);
782 return 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;
812 c->private_data = s;
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);
819 return c;
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,
830 &s->close);
831 if (composite_nomem(subreq, c)) return c;
833 tevent_req_set_callback(subreq, continue_samr_close, c);
834 return 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);
850 TALLOC_FREE(subreq);
851 if (!composite_is_ok(c)) return;
853 if (s->monitor_fn) {
854 struct monitor_msg msg;
856 msg.type = mon_SamrClose;
857 msg.data = NULL;
858 msg.data_size = 0;
859 s->monitor_fn(&msg);
862 composite_done(c);
866 NTSTATUS libnet_DomainCloseSamr_recv(struct composite_context *c, struct libnet_context *ctx,
867 TALLOC_CTX *mem_ctx, struct libnet_DomainClose *io)
869 NTSTATUS status;
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));
889 talloc_free(c);
890 return 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) {
901 case DOMAIN_LSA:
902 /* request to close policy handle on \pipe\lsarpc */
903 c = libnet_DomainCloseLsa_send(ctx, io, monitor);
904 break;
906 case DOMAIN_SAMR:
907 default:
908 /* request to close domain policy handle on \pipe\samr */
909 c = libnet_DomainCloseSamr_send(ctx, io, monitor);
910 break;
913 return c;
917 NTSTATUS libnet_DomainClose_recv(struct composite_context *c, struct libnet_context *ctx,
918 TALLOC_CTX *mem_ctx, struct libnet_DomainClose *io)
920 NTSTATUS status;
922 switch (io->in.type) {
923 case DOMAIN_LSA:
924 /* receive result of closing lsa policy handle */
925 status = libnet_DomainCloseLsa_recv(c, ctx, mem_ctx, io);
926 break;
928 case DOMAIN_SAMR:
929 default:
930 /* receive result of closing samr domain policy handle */
931 status = libnet_DomainCloseSamr_recv(c, ctx, mem_ctx, io);
932 break;
935 return status;
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;
957 int buf_size;
958 struct domainlist *domains;
959 uint32_t resume_handle;
960 uint32_t count;
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,
996 &s->samrconn);
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;
1023 msg.data = NULL;
1024 msg.data_size = 0;
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,
1039 &s->enumdom);
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
1049 upon completion.
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;
1067 msg.data = NULL;
1068 msg.data_size = 0;
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,
1089 &s->enumdom);
1090 if (composite_nomem(subreq, c)) return;
1092 tevent_req_set_callback(subreq, continue_samr_enum_domains, c);
1094 } else {
1095 composite_error(c, s->enumdom.out.result);
1096 return;
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,
1106 &s->samrclose);
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;
1132 msg.data = NULL;
1133 msg.data_size = 0;
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);
1142 composite_done(c);
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)
1151 uint32_t i;
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);
1158 } else {
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;
1177 return s->domains;
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;
1210 s->ctx = ctx;
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);
1228 } else {
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,
1236 &s->samrconn);
1237 if (composite_nomem(subreq, c)) return c;
1239 tevent_req_set_callback(subreq, continue_samr_connect, c);
1242 return 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)
1258 NTSTATUS status;
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));
1276 talloc_free(c);
1277 return 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);