Make the logic a lot clearer and fix the comment to match.
[Samba/fernandojvsilva.git] / source4 / libnet / libnet_domain.c
blob43a6a0e10b52f67b42e05409655d0df7fb467bc7
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 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);
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 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;
78 /* send request */
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);
87 /**
88 * Stage 0.5 (optional): Close existing (in libnet context) domain
89 * handle
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;
104 if (s->monitor_fn) {
105 struct monitor_msg msg;
107 msg.type = mon_SamrClose;
108 msg.data = NULL;
109 msg.data_size = 0;
110 s->monitor_fn(&msg);
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;
123 /* send request */
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;
149 if (s->monitor_fn) {
150 struct monitor_msg msg;
152 msg.type = mon_SamrConnect;
153 msg.data = NULL;
154 msg.data_size = 0;
155 s->monitor_fn(&msg);
158 r = &s->lookup;
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);
189 if (s->monitor_fn) {
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);
198 s->monitor_fn(&msg);
201 r = &s->open;
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);
209 return;
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;
240 if (s->monitor_fn) {
241 struct monitor_msg msg;
243 msg.type = mon_SamrOpenDomain;
244 msg.data = NULL;
245 msg.data_size = 0;
246 s->monitor_fn(&msg);
249 composite_done(c);
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;
276 c->private_data = s;
277 s->monitor_fn = monitor;
279 s->ctx = ctx;
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);
297 return 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 */
307 composite_done(c);
308 return c;
310 } else {
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);
321 return 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;
330 /* send request */
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);
336 return 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)
353 NTSTATUS status;
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
364 libnet functions */
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;
372 talloc_free(c);
373 return status;
377 struct domain_open_lsa_state {
378 const char *name;
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;
420 c->private_data = s;
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;
425 s->ctx = ctx;
427 /* check, if there's lsa pipe opened already, before opening a handle */
428 if (ctx->lsa.pipe == NULL) {
430 ZERO_STRUCT(s->rpcconn);
432 /* attempting to connect a domain controller */
433 s->rpcconn.level = LIBNET_RPC_CONNECT_DC;
434 s->rpcconn.in.name = talloc_strdup(c, io->in.domain_name);
435 s->rpcconn.in.dcerpc_iface = &ndr_table_lsarpc;
437 /* send rpc pipe connect request */
438 rpcconn_req = libnet_RpcConnect_send(ctx, c, &s->rpcconn, s->monitor_fn);
439 if (composite_nomem(rpcconn_req, c)) return c;
441 composite_continue(c, rpcconn_req, continue_rpc_connect_lsa, c);
442 return c;
445 s->pipe = ctx->lsa.pipe;
447 /* preparing parameters for lsa_OpenPolicy2 rpc call */
448 s->openpol.in.system_name = s->name;
449 s->openpol.in.access_mask = s->access_mask;
450 s->openpol.in.attr = talloc_zero(c, struct lsa_ObjectAttribute);
452 qos = talloc_zero(c, struct lsa_QosInfo);
453 qos->len = 0;
454 qos->impersonation_level = 2;
455 qos->context_mode = 1;
456 qos->effective_only = 0;
458 s->openpol.in.attr->sec_qos = qos;
459 s->openpol.out.handle = &s->handle;
461 /* send rpc request */
462 openpol_req = dcerpc_lsa_OpenPolicy2_send(s->pipe, c, &s->openpol);
463 if (composite_nomem(openpol_req, c)) return c;
465 composite_continue_rpc(c, openpol_req, continue_lsa_policy_open, c);
466 return c;
471 Stage 0.5 (optional): Rpc pipe connected, send lsa open policy request
473 static void continue_rpc_connect_lsa(struct composite_context *ctx)
475 struct composite_context *c;
476 struct domain_open_lsa_state *s;
477 struct lsa_QosInfo *qos;
478 struct rpc_request *openpol_req;
480 c = talloc_get_type(ctx->async.private_data, struct composite_context);
481 s = talloc_get_type(c->private_data, struct domain_open_lsa_state);
483 /* receive rpc connection */
484 c->status = libnet_RpcConnect_recv(ctx, s->ctx, c, &s->rpcconn);
485 if (!composite_is_ok(c)) return;
487 /* RpcConnect function leaves the pipe in libnet context,
488 so get it from there */
489 s->pipe = s->ctx->lsa.pipe;
491 /* prepare lsa_OpenPolicy2 call */
492 s->openpol.in.system_name = s->name;
493 s->openpol.in.access_mask = s->access_mask;
494 s->openpol.in.attr = talloc_zero(c, struct lsa_ObjectAttribute);
496 qos = talloc_zero(c, struct lsa_QosInfo);
497 qos->len = 0;
498 qos->impersonation_level = 2;
499 qos->context_mode = 1;
500 qos->effective_only = 0;
502 s->openpol.in.attr->sec_qos = qos;
503 s->openpol.out.handle = &s->handle;
505 /* send rpc request */
506 openpol_req = dcerpc_lsa_OpenPolicy2_send(s->pipe, c, &s->openpol);
507 if (composite_nomem(openpol_req, c)) return;
509 composite_continue_rpc(c, openpol_req, continue_lsa_policy_open, c);
514 Stage 1: Lsa policy opened - we're done, if successfully
516 static void continue_lsa_policy_open(struct rpc_request *req)
518 struct composite_context *c;
519 struct domain_open_lsa_state *s;
521 c = talloc_get_type(req->async.private_data, struct composite_context);
522 s = talloc_get_type(c->private_data, struct domain_open_lsa_state);
524 c->status = dcerpc_ndr_request_recv(req);
525 if (!composite_is_ok(c)) return;
527 if (s->monitor_fn) {
528 struct monitor_msg msg;
530 msg.type = mon_LsaOpenPolicy;
531 msg.data = NULL;
532 msg.data_size = 0;
533 s->monitor_fn(&msg);
536 composite_done(c);
541 * Receives result of asynchronous DomainOpenLsa call
543 * @param c composite context returned by asynchronous DomainOpenLsa call
544 * @param ctx initialised libnet context
545 * @param mem_ctx memory context of the call
546 * @param io pointer to results (and arguments) of the call
547 * @return nt status code of execution
550 NTSTATUS libnet_DomainOpenLsa_recv(struct composite_context *c, struct libnet_context *ctx,
551 TALLOC_CTX *mem_ctx, struct libnet_DomainOpen *io)
553 NTSTATUS status;
554 struct domain_open_lsa_state *s;
556 status = composite_wait(c);
558 if (NT_STATUS_IS_OK(status) && io) {
559 /* everything went fine - get the results and
560 return the error string */
561 s = talloc_get_type(c->private_data, struct domain_open_lsa_state);
562 io->out.domain_handle = s->handle;
564 ctx->lsa.handle = s->handle;
565 ctx->lsa.name = talloc_steal(ctx, s->name);
566 ctx->lsa.access_mask = s->access_mask;
568 io->out.error_string = talloc_strdup(mem_ctx, "Success");
570 } else if (!NT_STATUS_IS_OK(status)) {
571 /* there was an error, so provide nt status code description */
572 io->out.error_string = talloc_asprintf(mem_ctx,
573 "Failed to open domain: %s",
574 nt_errstr(status));
577 talloc_free(c);
578 return status;
583 * Sends a request to open a domain in desired service
585 * @param ctx initalised libnet context
586 * @param io arguments and results of the call
587 * @param monitor pointer to monitor function that is passed monitor message
590 struct composite_context* libnet_DomainOpen_send(struct libnet_context *ctx,
591 struct libnet_DomainOpen *io,
592 void (*monitor)(struct monitor_msg*))
594 struct composite_context *c;
596 switch (io->in.type) {
597 case DOMAIN_LSA:
598 /* reques to open a policy handle on \pipe\lsarpc */
599 c = libnet_DomainOpenLsa_send(ctx, io, monitor);
600 break;
602 case DOMAIN_SAMR:
603 default:
604 /* request to open a domain policy handle on \pipe\samr */
605 c = libnet_DomainOpenSamr_send(ctx, io, monitor);
606 break;
609 return c;
614 * Receive result of domain open request
616 * @param c composite context returned by DomainOpen_send function
617 * @param ctx initialised libnet context
618 * @param mem_ctx memory context of the call
619 * @param io results and arguments of the call
622 NTSTATUS libnet_DomainOpen_recv(struct composite_context *c, struct libnet_context *ctx,
623 TALLOC_CTX *mem_ctx, struct libnet_DomainOpen *io)
625 NTSTATUS status;
627 switch (io->in.type) {
628 case DOMAIN_LSA:
629 status = libnet_DomainOpenLsa_recv(c, ctx, mem_ctx, io);
630 break;
632 case DOMAIN_SAMR:
633 default:
634 status = libnet_DomainOpenSamr_recv(c, ctx, mem_ctx, io);
635 break;
638 return status;
643 * Synchronous version of DomainOpen call
645 * @param ctx initialised libnet context
646 * @param mem_ctx memory context for the call
647 * @param io arguments and results of the call
648 * @return nt status code of execution
651 NTSTATUS libnet_DomainOpen(struct libnet_context *ctx,
652 TALLOC_CTX *mem_ctx,
653 struct libnet_DomainOpen *io)
655 struct composite_context *c = libnet_DomainOpen_send(ctx, io, NULL);
656 return libnet_DomainOpen_recv(c, ctx, mem_ctx, io);
660 struct domain_close_lsa_state {
661 struct dcerpc_pipe *pipe;
662 struct lsa_Close close;
663 struct policy_handle handle;
665 void (*monitor_fn)(struct monitor_msg*);
669 static void continue_lsa_close(struct rpc_request *req);
672 struct composite_context* libnet_DomainCloseLsa_send(struct libnet_context *ctx,
673 struct libnet_DomainClose *io,
674 void (*monitor)(struct monitor_msg*))
676 struct composite_context *c;
677 struct domain_close_lsa_state *s;
678 struct rpc_request *close_req;
680 /* composite context and state structure allocation */
681 c = composite_create(ctx, ctx->event_ctx);
682 if (c == NULL) return c;
684 s = talloc_zero(c, struct domain_close_lsa_state);
685 if (composite_nomem(s, c)) return c;
687 c->private_data = s;
688 s->monitor_fn = monitor;
690 /* TODO: check if lsa pipe pointer is non-null */
692 if (!strequal(ctx->lsa.name, io->in.domain_name)) {
693 composite_error(c, NT_STATUS_INVALID_PARAMETER);
694 return c;
697 /* get opened lsarpc pipe pointer */
698 s->pipe = ctx->lsa.pipe;
700 /* prepare close handle call arguments */
701 s->close.in.handle = &ctx->lsa.handle;
702 s->close.out.handle = &s->handle;
704 /* send the request */
705 close_req = dcerpc_lsa_Close_send(s->pipe, c, &s->close);
706 if (composite_nomem(close_req, c)) return c;
708 composite_continue_rpc(c, close_req, continue_lsa_close, c);
709 return c;
714 Stage 1: Receive result of lsa close call
716 static void continue_lsa_close(struct rpc_request *req)
718 struct composite_context *c;
719 struct domain_close_lsa_state *s;
721 c = talloc_get_type(req->async.private_data, struct composite_context);
722 s = talloc_get_type(c->private_data, struct domain_close_lsa_state);
724 c->status = dcerpc_ndr_request_recv(req);
725 if (!composite_is_ok(c)) return;
727 if (s->monitor_fn) {
728 struct monitor_msg msg;
730 msg.type = mon_LsaClose;
731 msg.data = NULL;
732 msg.data_size = 0;
733 s->monitor_fn(&msg);
736 composite_done(c);
740 NTSTATUS libnet_DomainCloseLsa_recv(struct composite_context *c, struct libnet_context *ctx,
741 TALLOC_CTX *mem_ctx, struct libnet_DomainClose *io)
743 NTSTATUS status;
745 status = composite_wait(c);
747 if (NT_STATUS_IS_OK(status) && io) {
748 /* policy handle closed successfully */
750 ctx->lsa.name = NULL;
751 ZERO_STRUCT(ctx->lsa.handle);
753 io->out.error_string = talloc_asprintf(mem_ctx, "Success");
755 } else if (!NT_STATUS_IS_OK(status)) {
756 /* there was an error, so return description of the status code */
757 io->out.error_string = talloc_asprintf(mem_ctx, "Error: %s", nt_errstr(status));
760 talloc_free(c);
761 return status;
765 struct domain_close_samr_state {
766 struct samr_Close close;
767 struct policy_handle handle;
769 void (*monitor_fn)(struct monitor_msg*);
773 static void continue_samr_close(struct rpc_request *req);
776 struct composite_context* libnet_DomainCloseSamr_send(struct libnet_context *ctx,
777 struct libnet_DomainClose *io,
778 void (*monitor)(struct monitor_msg*))
780 struct composite_context *c;
781 struct domain_close_samr_state *s;
782 struct rpc_request *close_req;
784 /* composite context and state structure allocation */
785 c = composite_create(ctx, ctx->event_ctx);
786 if (c == NULL) return c;
788 s = talloc_zero(c, struct domain_close_samr_state);
789 if (composite_nomem(s, c)) return c;
791 c->private_data = s;
792 s->monitor_fn = monitor;
794 /* TODO: check if samr pipe pointer is non-null */
796 if (!strequal(ctx->samr.name, io->in.domain_name)) {
797 composite_error(c, NT_STATUS_INVALID_PARAMETER);
798 return c;
801 /* prepare close domain handle call arguments */
802 ZERO_STRUCT(s->close);
803 s->close.in.handle = &ctx->samr.handle;
804 s->close.out.handle = &s->handle;
806 /* send the request */
807 close_req = dcerpc_samr_Close_send(ctx->samr.pipe, ctx, &s->close);
808 if (composite_nomem(close_req, c)) return c;
810 composite_continue_rpc(c, close_req, continue_samr_close, c);
811 return c;
816 Stage 1: Receive result of samr close call
818 static void continue_samr_close(struct rpc_request *req)
820 struct composite_context *c;
821 struct domain_close_samr_state *s;
823 c = talloc_get_type(req->async.private_data, struct composite_context);
824 s = talloc_get_type(c->private_data, struct domain_close_samr_state);
826 c->status = dcerpc_ndr_request_recv(req);
827 if (!composite_is_ok(c)) return;
829 if (s->monitor_fn) {
830 struct monitor_msg msg;
832 msg.type = mon_SamrClose;
833 msg.data = NULL;
834 msg.data_size = 0;
835 s->monitor_fn(&msg);
838 composite_done(c);
842 NTSTATUS libnet_DomainCloseSamr_recv(struct composite_context *c, struct libnet_context *ctx,
843 TALLOC_CTX *mem_ctx, struct libnet_DomainClose *io)
845 NTSTATUS status;
847 status = composite_wait(c);
849 if (NT_STATUS_IS_OK(status) && io) {
850 /* domain policy handle closed successfully */
852 ZERO_STRUCT(ctx->samr.handle);
853 talloc_free(discard_const_p(char, ctx->samr.name));
854 talloc_free(ctx->samr.sid);
855 ctx->samr.name = NULL;
856 ctx->samr.sid = NULL;
858 io->out.error_string = talloc_asprintf(mem_ctx, "Success");
860 } else if (!NT_STATUS_IS_OK(status)) {
861 /* there was an error, so return description of the status code */
862 io->out.error_string = talloc_asprintf(mem_ctx, "Error: %s", nt_errstr(status));
865 talloc_free(c);
866 return status;
870 struct composite_context* libnet_DomainClose_send(struct libnet_context *ctx,
871 struct libnet_DomainClose *io,
872 void (*monitor)(struct monitor_msg*))
874 struct composite_context *c;
876 switch (io->in.type) {
877 case DOMAIN_LSA:
878 /* request to close policy handle on \pipe\lsarpc */
879 c = libnet_DomainCloseLsa_send(ctx, io, monitor);
880 break;
882 case DOMAIN_SAMR:
883 default:
884 /* request to close domain policy handle on \pipe\samr */
885 c = libnet_DomainCloseSamr_send(ctx, io, monitor);
886 break;
889 return c;
893 NTSTATUS libnet_DomainClose_recv(struct composite_context *c, struct libnet_context *ctx,
894 TALLOC_CTX *mem_ctx, struct libnet_DomainClose *io)
896 NTSTATUS status;
898 switch (io->in.type) {
899 case DOMAIN_LSA:
900 /* receive result of closing lsa policy handle */
901 status = libnet_DomainCloseLsa_recv(c, ctx, mem_ctx, io);
902 break;
904 case DOMAIN_SAMR:
905 default:
906 /* receive result of closing samr domain policy handle */
907 status = libnet_DomainCloseSamr_recv(c, ctx, mem_ctx, io);
908 break;
911 return status;
915 NTSTATUS libnet_DomainClose(struct libnet_context *ctx, TALLOC_CTX *mem_ctx,
916 struct libnet_DomainClose *io)
918 struct composite_context *c;
920 c = libnet_DomainClose_send(ctx, io, NULL);
921 return libnet_DomainClose_recv(c, ctx, mem_ctx, io);
925 struct domain_list_state {
926 struct libnet_context *ctx;
927 struct libnet_RpcConnect rpcconn;
928 struct samr_Connect samrconn;
929 struct samr_EnumDomains enumdom;
930 struct samr_Close samrclose;
931 const char *hostname;
932 struct policy_handle connect_handle;
933 int buf_size;
934 struct domainlist *domains;
935 uint32_t resume_handle;
936 uint32_t count;
938 void (*monitor_fn)(struct monitor_msg*);
942 static void continue_rpc_connect(struct composite_context *c);
943 static void continue_samr_connect(struct rpc_request *c);
944 static void continue_samr_enum_domains(struct rpc_request *req);
945 static void continue_samr_close_handle(struct rpc_request *req);
947 static struct domainlist* get_domain_list(TALLOC_CTX *mem_ctx, struct domain_list_state *s);
951 Stage 1: Receive connected rpc pipe and send connection
952 request to SAMR service
954 static void continue_rpc_connect(struct composite_context *ctx)
956 struct composite_context *c;
957 struct domain_list_state *s;
958 struct rpc_request *samrconn_req;
960 c = talloc_get_type(ctx->async.private_data, struct composite_context);
961 s = talloc_get_type(c->private_data, struct domain_list_state);
963 c->status = libnet_RpcConnect_recv(ctx, s->ctx, c, &s->rpcconn);
964 if (!composite_is_ok(c)) return;
966 s->samrconn.in.system_name = 0;
967 s->samrconn.in.access_mask = SEC_GENERIC_READ; /* should be enough */
968 s->samrconn.out.connect_handle = &s->connect_handle;
970 samrconn_req = dcerpc_samr_Connect_send(s->ctx->samr.pipe, c, &s->samrconn);
971 if (composite_nomem(samrconn_req, c)) return;
973 composite_continue_rpc(c, samrconn_req, continue_samr_connect, c);
978 Stage 2: Receive policy handle to the connected SAMR service and issue
979 a request to enumerate domain databases available
981 static void continue_samr_connect(struct rpc_request *req)
983 struct composite_context *c;
984 struct domain_list_state *s;
985 struct rpc_request *enumdom_req;
987 c = talloc_get_type(req->async.private_data, struct composite_context);
988 s = talloc_get_type(c->private_data, struct domain_list_state);
990 c->status = dcerpc_ndr_request_recv(req);
991 if (!composite_is_ok(c)) return;
993 if (s->monitor_fn) {
994 struct monitor_msg msg;
996 msg.type = mon_SamrConnect;
997 msg.data = NULL;
998 msg.data_size = 0;
999 s->monitor_fn(&msg);
1002 s->enumdom.in.connect_handle = &s->connect_handle;
1003 s->enumdom.in.resume_handle = &s->resume_handle;
1004 s->enumdom.in.buf_size = s->buf_size;
1005 s->enumdom.out.resume_handle = &s->resume_handle;
1006 s->enumdom.out.num_entries = talloc(s, uint32_t);
1007 if (composite_nomem(s->enumdom.out.num_entries, c)) return;
1008 s->enumdom.out.sam = talloc(s, struct samr_SamArray *);
1009 if (composite_nomem(s->enumdom.out.sam, c)) return;
1011 enumdom_req = dcerpc_samr_EnumDomains_send(s->ctx->samr.pipe, c, &s->enumdom);
1012 if (composite_nomem(enumdom_req, c)) return;
1014 composite_continue_rpc(c, enumdom_req, continue_samr_enum_domains, c);
1019 Stage 3: Receive domain names available and repeat the request
1020 enumeration is not complete yet. Close samr connection handle
1021 upon completion.
1023 static void continue_samr_enum_domains(struct rpc_request *req)
1025 struct composite_context *c;
1026 struct domain_list_state *s;
1027 struct rpc_request *enumdom_req;
1028 struct rpc_request *samrclose_req;
1030 c = talloc_get_type(req->async.private_data, struct composite_context);
1031 s = talloc_get_type(c->private_data, struct domain_list_state);
1033 c->status = dcerpc_ndr_request_recv(req);
1034 if (!composite_is_ok(c)) return;
1036 if (s->monitor_fn) {
1037 struct monitor_msg msg;
1039 msg.type = mon_SamrEnumDomains;
1040 msg.data = NULL;
1041 msg.data_size = 0;
1042 s->monitor_fn(&msg);
1045 if (NT_STATUS_IS_OK(s->enumdom.out.result)) {
1047 s->domains = get_domain_list(c, s);
1049 } else if (NT_STATUS_EQUAL(s->enumdom.out.result, STATUS_MORE_ENTRIES)) {
1051 s->domains = get_domain_list(c, s);
1053 /* prepare next round of enumeration */
1054 s->enumdom.in.connect_handle = &s->connect_handle;
1055 s->enumdom.in.resume_handle = &s->resume_handle;
1056 s->enumdom.in.buf_size = s->ctx->samr.buf_size;
1057 s->enumdom.out.resume_handle = &s->resume_handle;
1059 /* send the request */
1060 enumdom_req = dcerpc_samr_EnumDomains_send(s->ctx->samr.pipe, c, &s->enumdom);
1061 if (composite_nomem(enumdom_req, c)) return;
1063 composite_continue_rpc(c, enumdom_req, continue_samr_enum_domains, c);
1065 } else {
1066 composite_error(c, s->enumdom.out.result);
1067 return;
1070 /* close samr connection handle */
1071 s->samrclose.in.handle = &s->connect_handle;
1072 s->samrclose.out.handle = &s->connect_handle;
1074 /* send the request */
1075 samrclose_req = dcerpc_samr_Close_send(s->ctx->samr.pipe, c, &s->samrclose);
1076 if (composite_nomem(samrclose_req, c)) return;
1078 composite_continue_rpc(c, samrclose_req, continue_samr_close_handle, c);
1083 Stage 4: Receive result of closing samr connection handle.
1085 static void continue_samr_close_handle(struct rpc_request *req)
1087 struct composite_context *c;
1088 struct domain_list_state *s;
1090 c = talloc_get_type(req->async.private_data, struct composite_context);
1091 s = talloc_get_type(c->private_data, struct domain_list_state);
1093 c->status = dcerpc_ndr_request_recv(req);
1094 if (!composite_is_ok(c)) return;
1096 if (s->monitor_fn) {
1097 struct monitor_msg msg;
1099 msg.type = mon_SamrClose;
1100 msg.data = NULL;
1101 msg.data_size = 0;
1102 s->monitor_fn(&msg);
1105 /* did everything go fine ? */
1106 if (!NT_STATUS_IS_OK(s->samrclose.out.result)) {
1107 composite_error(c, s->samrclose.out.result);
1110 composite_done(c);
1115 Utility function to copy domain names from result of samr_EnumDomains call
1117 static struct domainlist* get_domain_list(TALLOC_CTX *mem_ctx, struct domain_list_state *s)
1119 int i;
1120 if (mem_ctx == NULL || s == NULL) return NULL;
1122 /* prepare domains array */
1123 if (s->domains == NULL) {
1124 s->domains = talloc_array(mem_ctx, struct domainlist,
1125 *s->enumdom.out.num_entries);
1126 } else {
1127 s->domains = talloc_realloc(mem_ctx, s->domains, struct domainlist,
1128 s->count + *s->enumdom.out.num_entries);
1131 /* copy domain names returned from samr_EnumDomains call */
1132 for (i = s->count; i < s->count + *s->enumdom.out.num_entries; i++)
1134 struct lsa_String *domain_name = &(*s->enumdom.out.sam)->entries[i - s->count].name;
1136 /* strdup name as a child of allocated array to make it follow the array
1137 in case of talloc_steal or talloc_free */
1138 s->domains[i].name = talloc_strdup(s->domains, domain_name->string);
1139 s->domains[i].sid = NULL; /* this is to be filled out later */
1142 /* number of entries returned (domains enumerated) */
1143 s->count += *s->enumdom.out.num_entries;
1145 return s->domains;
1150 * Sends a request to list domains on given host
1152 * @param ctx initalised libnet context
1153 * @param mem_ctx memory context
1154 * @param io arguments and results of the call
1155 * @param monitor pointer to monitor function that is passed monitor messages
1158 struct composite_context* libnet_DomainList_send(struct libnet_context *ctx,
1159 TALLOC_CTX *mem_ctx,
1160 struct libnet_DomainList *io,
1161 void (*monitor)(struct monitor_msg*))
1163 struct composite_context *c;
1164 struct domain_list_state *s;
1165 struct composite_context *rpcconn_req;
1166 struct rpc_request *samrconn_req;
1168 /* composite context and state structure allocation */
1169 c = composite_create(ctx, ctx->event_ctx);
1170 if (c == NULL) return c;
1172 s = talloc_zero(c, struct domain_list_state);
1173 if (composite_nomem(s, c)) return c;
1175 c->private_data = s;
1176 s->monitor_fn = monitor;
1178 s->ctx = ctx;
1179 s->hostname = talloc_strdup(c, io->in.hostname);
1180 if (composite_nomem(s->hostname, c)) return c;
1182 /* check whether samr pipe has already been opened */
1183 if (ctx->samr.pipe == NULL) {
1184 ZERO_STRUCT(s->rpcconn);
1186 /* prepare rpc connect call */
1187 s->rpcconn.level = LIBNET_RPC_CONNECT_SERVER;
1188 s->rpcconn.in.name = s->hostname;
1189 s->rpcconn.in.dcerpc_iface = &ndr_table_samr;
1191 rpcconn_req = libnet_RpcConnect_send(ctx, c, &s->rpcconn, s->monitor_fn);
1192 if (composite_nomem(rpcconn_req, c)) return c;
1194 composite_continue(c, rpcconn_req, continue_rpc_connect, c);
1196 } else {
1197 /* prepare samr_Connect call */
1198 s->samrconn.in.system_name = 0;
1199 s->samrconn.in.access_mask = SEC_GENERIC_READ;
1200 s->samrconn.out.connect_handle = &s->connect_handle;
1202 samrconn_req = dcerpc_samr_Connect_send(s->ctx->samr.pipe, c, &s->samrconn);
1203 if (composite_nomem(samrconn_req, c)) return c;
1205 composite_continue_rpc(c, samrconn_req, continue_samr_connect, c);
1208 return c;
1213 * Receive result of domain list request
1215 * @param c composite context returned by DomainList_send function
1216 * @param ctx initialised libnet context
1217 * @param mem_ctx memory context of the call
1218 * @param io results and arguments of the call
1221 NTSTATUS libnet_DomainList_recv(struct composite_context *c, struct libnet_context *ctx,
1222 TALLOC_CTX *mem_ctx, struct libnet_DomainList *io)
1224 NTSTATUS status;
1225 struct domain_list_state *s;
1227 status = composite_wait(c);
1229 s = talloc_get_type(c->private_data, struct domain_list_state);
1231 if (NT_STATUS_IS_OK(status) && ctx && mem_ctx && io) {
1232 /* fetch the results to be returned by io structure */
1233 io->out.count = s->count;
1234 io->out.domains = talloc_steal(mem_ctx, s->domains);
1235 io->out.error_string = talloc_asprintf(mem_ctx, "Success");
1237 } else if (!NT_STATUS_IS_OK(status)) {
1238 /* there was an error, so return description of the status code */
1239 io->out.error_string = talloc_asprintf(mem_ctx, "Error: %s", nt_errstr(status));
1242 talloc_free(c);
1243 return status;
1248 * Synchronous version of DomainList call
1250 * @param ctx initialised libnet context
1251 * @param mem_ctx memory context for the call
1252 * @param io arguments and results of the call
1253 * @return nt status code of execution
1256 NTSTATUS libnet_DomainList(struct libnet_context *ctx, TALLOC_CTX *mem_ctx,
1257 struct libnet_DomainList *io)
1259 struct composite_context *c;
1261 c = libnet_DomainList_send(ctx, mem_ctx, io, NULL);
1262 return libnet_DomainList_recv(c, ctx, mem_ctx, io);