r15625: Partial commit of my current work. It makes libnet api functions
[bbaumbach/samba-autobuild/.git] / source4 / libnet / libnet_domain.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    Copyright (C) Rafal Szczesniak 2005
5    
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.
10    
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.
15    
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.
19 */
20
21 /*
22   a composite function for domain handling on samr pipe
23 */
24
25 #include "includes.h"
26 #include "libcli/composite/composite.h"
27 #include "libnet/libnet.h"
28 #include "librpc/gen_ndr/ndr_samr_c.h"
29
30 static void domain_open_handler(struct rpc_request*);
31
32 enum domain_open_stage { DOMOPEN_CONNECT, DOMOPEN_LOOKUP, DOMOPEN_OPEN,
33                          DOMOPEN_CLOSE_EXISTING, DOMOPEN_RPC_CONNECT };
34
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;
47         uint32_t                  access_mask;
48         struct policy_handle      connect_handle;
49         struct policy_handle      domain_handle;
50 };
51
52
53 /**
54  * Stage 0.5 (optional): Connect to samr rpc pipe
55  */
56 static void domain_open_rpc_connect(struct composite_context *ctx)
57 {
58         struct composite_context *c;
59         struct domain_open_state *s;
60
61         c = talloc_get_type(ctx->async.private_data, struct composite_context);
62         s = talloc_get_type(c->private_data, struct domain_open_state);
63
64         c->status = libnet_RpcConnect_recv(ctx, s->ctx, c, &s->rpcconn);
65         if (!composite_is_ok(c)) return;
66
67         s->pipe = s->rpcconn.out.dcerpc_pipe;
68
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;
73
74         /* send request */
75         s->req = dcerpc_samr_Connect_send(s->pipe, c, &s->connect);
76         if (composite_nomem(s->req, c)) return;
77
78         /* callback handler */
79         s->req->async.callback = domain_open_handler;
80         s->req->async.private  = c;
81         s->stage = DOMOPEN_CONNECT;
82 }
83
84
85 /**
86  * Stage 0.5 (optional): Close existing (in libnet context) domain
87  * handle
88  */
89 static NTSTATUS domain_open_close(struct composite_context *c,
90                                   struct domain_open_state *s)
91 {
92         /* receive samr_Close reply */
93         c->status = dcerpc_ndr_request_recv(s->req);
94         NT_STATUS_NOT_OK_RETURN(c->status);
95
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);
100
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;
105         
106         /* send request */
107         s->req = dcerpc_samr_Connect_send(s->pipe, c, &s->connect);
108         if (s->req == NULL) return NT_STATUS_NO_MEMORY;
109
110         /* callback handler */
111         s->req->async.callback = domain_open_handler;
112         s->req->async.private  = c;
113         s->stage = DOMOPEN_CONNECT;
114         
115         return NT_STATUS_OK;
116 }
117
118
119 /**
120  * Stage 1: Connect to SAM server.
121  */
122 static NTSTATUS domain_open_connect(struct composite_context *c,
123                                     struct domain_open_state *s)
124 {
125         struct samr_LookupDomain *r = &s->lookup;
126
127         /* receive samr_Connect reply */
128         c->status = dcerpc_ndr_request_recv(s->req);
129         NT_STATUS_NOT_OK_RETURN(c->status);
130
131         /* prepare for samr_LookupDomain call */
132         r->in.connect_handle = &s->connect_handle;
133         r->in.domain_name    = &s->domain_name;
134
135         s->req = dcerpc_samr_LookupDomain_send(s->pipe, c, r);
136         if (s->req == NULL) goto failure;
137
138         s->req->async.callback = domain_open_handler;
139         s->req->async.private  = c;
140         s->stage = DOMOPEN_LOOKUP;
141
142         return NT_STATUS_OK;
143
144 failure:
145         return NT_STATUS_UNSUCCESSFUL;
146 }
147
148
149 /**
150  * Stage 2: Lookup domain by name.
151  */
152 static NTSTATUS domain_open_lookup(struct composite_context *c,
153                                    struct domain_open_state *s)
154 {
155         struct samr_OpenDomain *r = &s->open;
156
157         /* receive samr_LookupDomain reply */
158         c->status = dcerpc_ndr_request_recv(s->req);
159         NT_STATUS_NOT_OK_RETURN(c->status);
160
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;
166
167         s->req = dcerpc_samr_OpenDomain_send(s->pipe, c, r);
168         if (s->req == NULL) goto failure;
169
170         s->req->async.callback = domain_open_handler;
171         s->req->async.private  = c;
172         s->stage = DOMOPEN_OPEN;
173
174         return NT_STATUS_OK;
175
176 failure:
177         return NT_STATUS_UNSUCCESSFUL;
178 }
179
180
181 /*
182  * Stage 3: Open domain.
183  */
184 static NTSTATUS domain_open_open(struct composite_context *c,
185                                  struct domain_open_state *s)
186 {
187         /* receive samr_OpenDomain reply */
188         c->status = dcerpc_ndr_request_recv(s->req);
189         NT_STATUS_NOT_OK_RETURN(c->status);
190
191         c->state = COMPOSITE_STATE_DONE;
192         
193         return NT_STATUS_OK;
194 }
195
196
197 /**
198  * Event handler for asynchronous request. Handles transition through
199  * intermediate stages of the call.
200  *
201  * @param req rpc call context
202  */
203 static void domain_open_handler(struct rpc_request *req)
204 {
205         struct composite_context *c = req->async.private;
206         struct domain_open_state *s = talloc_get_type(c->private_data, struct domain_open_state);
207
208         /* Stages of the call */
209         switch (s->stage) {
210         case DOMOPEN_CONNECT:
211                 c->status = domain_open_connect(c, s);
212                 break;
213         case DOMOPEN_LOOKUP:
214                 c->status = domain_open_lookup(c, s);
215                 break;
216         case DOMOPEN_OPEN:
217                 c->status = domain_open_open(c, s);
218                 break;
219         case DOMOPEN_CLOSE_EXISTING:
220                 c->status = domain_open_close(c, s);
221                 break;
222         case DOMOPEN_RPC_CONNECT:
223                 /* this state shouldn't be handled here */
224                 c->status = NT_STATUS_UNSUCCESSFUL;
225                 break;
226         }
227
228         if (!NT_STATUS_IS_OK(c->status)) {
229                 c->state = COMPOSITE_STATE_ERROR;
230         }
231
232         if (c->state == COMPOSITE_STATE_DONE) {
233                 composite_done(c);
234         }
235 }
236
237
238 /**
239  * Sends asynchronous domain_open request
240  *
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
244  */
245
246 struct composite_context *libnet_DomainOpen_send(struct libnet_context *ctx,
247                                                  struct libnet_DomainOpen *io,
248                                                  void (*monitor)(struct monitor_msg*))
249 {
250         struct composite_context *c;
251         struct domain_open_state *s;
252
253         c = talloc_zero(ctx, struct composite_context);
254         if (c == NULL) return NULL;
255
256         s = talloc_zero(c, struct domain_open_state);
257         if (composite_nomem(s, c)) return c;
258
259         c->state        = COMPOSITE_STATE_IN_PROGRESS;
260         c->private_data = s;
261         c->event_ctx    = ctx->event_ctx;
262
263         s->ctx                 = ctx;
264         s->pipe                = ctx->samr_pipe;
265         s->access_mask         = io->in.access_mask;
266         s->domain_name.string  = io->in.domain_name;
267
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;
272
273                 s->rpcconn_req = libnet_RpcConnect_send(ctx, c, &s->rpcconn);
274                 if (composite_nomem(s->rpcconn_req, c)) return c;
275
276                 s->rpcconn_req->async.fn = domain_open_rpc_connect;
277                 s->rpcconn_req->async.private_data  = c;
278                 s->stage = DOMOPEN_RPC_CONNECT;
279
280                 return c;
281         }
282
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) {
288
289                         /* this domain is already opened */
290                         composite_done(c);
291                         return c;
292
293                 } else {
294                         /* another domain or access rights have been
295                            requested - close the existing handle first */
296                         s->close.in.handle = &ctx->domain.handle;
297
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;
301
302                         /* callback handler */
303                         s->req->async.callback = domain_open_handler;
304                         s->req->async.private  = c;
305                         s->stage = DOMOPEN_CLOSE_EXISTING;
306
307                         return c;
308                 }
309         }
310
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;
315         
316         /* send request */
317         s->req = dcerpc_samr_Connect_send(s->pipe, c, &s->connect);
318         if (composite_nomem(s->req, c)) return c;
319
320         /* callback handler */
321         s->req->async.callback = domain_open_handler;
322         s->req->async.private  = c;
323         s->stage = DOMOPEN_CONNECT;
324
325         return c;
326 }
327
328
329 /**
330  * Waits for and receives result of asynchronous domain_open call
331  * 
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
337  */
338
339 NTSTATUS libnet_DomainOpen_recv(struct composite_context *c, struct libnet_context *ctx,
340                                 TALLOC_CTX *mem_ctx, struct libnet_DomainOpen *io)
341 {
342         NTSTATUS status;
343         struct domain_open_state *s;
344
345         /* wait for results of sending request */
346         status = composite_wait(c);
347         
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;
351
352                 /* store the resulting handle and related data for use by other
353                    libnet functions */
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;
357         }
358
359         talloc_free(c);
360         return status;
361 }
362
363
364 /**
365  * Synchronous version of domain_open call
366  *
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
371  */
372
373 NTSTATUS libnet_DomainOpen(struct libnet_context *ctx,
374                            TALLOC_CTX *mem_ctx,
375                            struct libnet_DomainOpen *io)
376 {
377         struct composite_context *c = libnet_DomainOpen_send(ctx, io, NULL);
378         return libnet_DomainOpen_recv(c, ctx, mem_ctx, io);
379 }