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 2 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, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 a composite function for domain handling on samr pipe
26 #include "libcli/composite/composite.h"
27 #include "libnet/libnet.h"
28 #include "librpc/gen_ndr/ndr_samr_c.h"
30 static void domain_open_handler(struct rpc_request*);
32 enum domain_open_stage { DOMOPEN_CONNECT, DOMOPEN_LOOKUP, DOMOPEN_OPEN,
33 DOMOPEN_CLOSE_EXISTING, DOMOPEN_RPC_CONNECT };
35 struct domain_open_state {
36 enum domain_open_stage stage;
37 struct libnet_context *ctx;
38 struct dcerpc_pipe *pipe;
39 struct rpc_request *req;
40 struct composite_context *rpcconn_req;
41 struct samr_Connect connect;
42 struct samr_LookupDomain lookup;
43 struct samr_OpenDomain open;
44 struct samr_Close close;
45 struct libnet_RpcConnect rpcconn;
46 struct lsa_String domain_name;
48 struct policy_handle connect_handle;
49 struct policy_handle domain_handle;
54 * Stage 0.5 (optional): Connect to samr rpc pipe
56 static void domain_open_rpc_connect(struct composite_context *ctx)
58 struct composite_context *c;
59 struct domain_open_state *s;
61 c = talloc_get_type(ctx->async.private_data, struct composite_context);
62 s = talloc_get_type(c->private_data, struct domain_open_state);
64 c->status = libnet_RpcConnect_recv(ctx, s->ctx, c, &s->rpcconn);
65 if (!composite_is_ok(c)) return;
67 s->pipe = s->rpcconn.out.dcerpc_pipe;
69 /* preparing parameters for samr_Connect rpc call */
70 s->connect.in.system_name = 0;
71 s->connect.in.access_mask = s->access_mask;
72 s->connect.out.connect_handle = &s->connect_handle;
75 s->req = dcerpc_samr_Connect_send(s->pipe, c, &s->connect);
76 if (composite_nomem(s->req, c)) return;
78 /* callback handler */
79 s->req->async.callback = domain_open_handler;
80 s->req->async.private = c;
81 s->stage = DOMOPEN_CONNECT;
86 * Stage 0.5 (optional): Close existing (in libnet context) domain
89 static NTSTATUS domain_open_close(struct composite_context *c,
90 struct domain_open_state *s)
92 /* receive samr_Close reply */
93 c->status = dcerpc_ndr_request_recv(s->req);
94 NT_STATUS_NOT_OK_RETURN(c->status);
96 /* reset domain handle and associated data in libnet_context */
97 s->ctx->domain.name = NULL;
98 s->ctx->domain.access_mask = 0;
99 ZERO_STRUCT(s->ctx->domain.handle);
101 /* preparing parameters for samr_Connect rpc call */
102 s->connect.in.system_name = 0;
103 s->connect.in.access_mask = s->access_mask;
104 s->connect.out.connect_handle = &s->connect_handle;
107 s->req = dcerpc_samr_Connect_send(s->pipe, c, &s->connect);
108 if (s->req == NULL) return NT_STATUS_NO_MEMORY;
110 /* callback handler */
111 s->req->async.callback = domain_open_handler;
112 s->req->async.private = c;
113 s->stage = DOMOPEN_CONNECT;
120 * Stage 1: Connect to SAM server.
122 static NTSTATUS domain_open_connect(struct composite_context *c,
123 struct domain_open_state *s)
125 struct samr_LookupDomain *r = &s->lookup;
127 /* receive samr_Connect reply */
128 c->status = dcerpc_ndr_request_recv(s->req);
129 NT_STATUS_NOT_OK_RETURN(c->status);
131 /* prepare for samr_LookupDomain call */
132 r->in.connect_handle = &s->connect_handle;
133 r->in.domain_name = &s->domain_name;
135 s->req = dcerpc_samr_LookupDomain_send(s->pipe, c, r);
136 if (s->req == NULL) goto failure;
138 s->req->async.callback = domain_open_handler;
139 s->req->async.private = c;
140 s->stage = DOMOPEN_LOOKUP;
145 return NT_STATUS_UNSUCCESSFUL;
150 * Stage 2: Lookup domain by name.
152 static NTSTATUS domain_open_lookup(struct composite_context *c,
153 struct domain_open_state *s)
155 struct samr_OpenDomain *r = &s->open;
157 /* receive samr_LookupDomain reply */
158 c->status = dcerpc_ndr_request_recv(s->req);
159 NT_STATUS_NOT_OK_RETURN(c->status);
161 /* prepare for samr_OpenDomain call */
162 r->in.connect_handle = &s->connect_handle;
163 r->in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
164 r->in.sid = s->lookup.out.sid;
165 r->out.domain_handle = &s->domain_handle;
167 s->req = dcerpc_samr_OpenDomain_send(s->pipe, c, r);
168 if (s->req == NULL) goto failure;
170 s->req->async.callback = domain_open_handler;
171 s->req->async.private = c;
172 s->stage = DOMOPEN_OPEN;
177 return NT_STATUS_UNSUCCESSFUL;
182 * Stage 3: Open domain.
184 static NTSTATUS domain_open_open(struct composite_context *c,
185 struct domain_open_state *s)
187 /* receive samr_OpenDomain reply */
188 c->status = dcerpc_ndr_request_recv(s->req);
189 NT_STATUS_NOT_OK_RETURN(c->status);
191 c->state = COMPOSITE_STATE_DONE;
198 * Event handler for asynchronous request. Handles transition through
199 * intermediate stages of the call.
201 * @param req rpc call context
203 static void domain_open_handler(struct rpc_request *req)
205 struct composite_context *c = req->async.private;
206 struct domain_open_state *s = talloc_get_type(c->private_data, struct domain_open_state);
208 /* Stages of the call */
210 case DOMOPEN_CONNECT:
211 c->status = domain_open_connect(c, s);
214 c->status = domain_open_lookup(c, s);
217 c->status = domain_open_open(c, s);
219 case DOMOPEN_CLOSE_EXISTING:
220 c->status = domain_open_close(c, s);
222 case DOMOPEN_RPC_CONNECT:
223 /* this state shouldn't be handled here */
224 c->status = NT_STATUS_UNSUCCESSFUL;
228 if (!NT_STATUS_IS_OK(c->status)) {
229 c->state = COMPOSITE_STATE_ERROR;
232 if (c->state == COMPOSITE_STATE_DONE) {
239 * Sends asynchronous domain_open request
241 * @param ctx initialised libnet context
242 * @param io arguments and results of the call
243 * @param monitor pointer to monitor function that is passed monitor message
246 struct composite_context *libnet_DomainOpen_send(struct libnet_context *ctx,
247 struct libnet_DomainOpen *io,
248 void (*monitor)(struct monitor_msg*))
250 struct composite_context *c;
251 struct domain_open_state *s;
253 c = talloc_zero(ctx, struct composite_context);
254 if (c == NULL) return NULL;
256 s = talloc_zero(c, struct domain_open_state);
257 if (composite_nomem(s, c)) return c;
259 c->state = COMPOSITE_STATE_IN_PROGRESS;
261 c->event_ctx = ctx->event_ctx;
264 s->pipe = ctx->samr_pipe;
265 s->access_mask = io->in.access_mask;
266 s->domain_name.string = io->in.domain_name;
268 if (ctx->samr_pipe == NULL) {
269 s->rpcconn.level = LIBNET_RPC_CONNECT_DC;
270 s->rpcconn.in.name = io->in.domain_name;
271 s->rpcconn.in.dcerpc_iface = &dcerpc_table_samr;
273 s->rpcconn_req = libnet_RpcConnect_send(ctx, c, &s->rpcconn);
274 if (composite_nomem(s->rpcconn_req, c)) return c;
276 s->rpcconn_req->async.fn = domain_open_rpc_connect;
277 s->rpcconn_req->async.private_data = c;
278 s->stage = DOMOPEN_RPC_CONNECT;
283 /* libnet context's domain handle is not empty, so check out what
284 was opened first, before doing anything */
285 if (!policy_handle_empty(&ctx->domain.handle)) {
286 if (strequal(ctx->domain.name, io->in.domain_name) &&
287 ctx->domain.access_mask == io->in.access_mask) {
289 /* this domain is already opened */
294 /* another domain or access rights have been
295 requested - close the existing handle first */
296 s->close.in.handle = &ctx->domain.handle;
298 /* send request to close domain handle */
299 s->req = dcerpc_samr_Close_send(s->pipe, c, &s->close);
300 if (composite_nomem(s->req, c)) return c;
302 /* callback handler */
303 s->req->async.callback = domain_open_handler;
304 s->req->async.private = c;
305 s->stage = DOMOPEN_CLOSE_EXISTING;
311 /* preparing parameters for samr_Connect rpc call */
312 s->connect.in.system_name = 0;
313 s->connect.in.access_mask = s->access_mask;
314 s->connect.out.connect_handle = &s->connect_handle;
317 s->req = dcerpc_samr_Connect_send(s->pipe, c, &s->connect);
318 if (composite_nomem(s->req, c)) return c;
320 /* callback handler */
321 s->req->async.callback = domain_open_handler;
322 s->req->async.private = c;
323 s->stage = DOMOPEN_CONNECT;
330 * Waits for and receives result of asynchronous domain_open call
332 * @param c composite context returned by asynchronous domain_open call
333 * @param ctx initialised libnet context
334 * @param mem_ctx memory context of the call
335 * @param io pointer to results (and arguments) of the call
336 * @return nt status code of execution
339 NTSTATUS libnet_DomainOpen_recv(struct composite_context *c, struct libnet_context *ctx,
340 TALLOC_CTX *mem_ctx, struct libnet_DomainOpen *io)
343 struct domain_open_state *s;
345 /* wait for results of sending request */
346 status = composite_wait(c);
348 if (NT_STATUS_IS_OK(status) && io) {
349 s = talloc_get_type(c->private_data, struct domain_open_state);
350 io->out.domain_handle = s->domain_handle;
352 /* store the resulting handle and related data for use by other
354 ctx->domain.handle = s->domain_handle;
355 ctx->domain.name = talloc_strdup(ctx, s->domain_name.string);
356 ctx->domain.access_mask = s->access_mask;
365 * Synchronous version of domain_open call
367 * @param ctx initialised libnet context
368 * @param mem_ctx memory context for the call
369 * @param io arguments and results of the call
370 * @return nt status code of execution
373 NTSTATUS libnet_DomainOpen(struct libnet_context *ctx,
375 struct libnet_DomainOpen *io)
377 struct composite_context *c = libnet_DomainOpen_send(ctx, io, NULL);
378 return libnet_DomainOpen_recv(c, ctx, mem_ctx, io);