0fafe7125005e9898b8e5afadf3ba4909ff2a435
[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 3 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, see <http://www.gnu.org/licenses/>.
18 */
19
20 /*
21   a composite function for domain handling on samr and lsa pipes
22 */
23
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"
29
30
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;
44
45         /* information about the progress */
46         void (*monitor_fn)(struct monitor_msg*);
47 };
48
49
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);
54
55
56 /**
57  * Stage 0.5 (optional): Connect to samr rpc pipe
58  */
59 static void continue_domain_open_rpc_connect(struct composite_context *ctx)
60 {
61         struct composite_context *c;
62         struct domain_open_samr_state *s;
63         struct tevent_req *subreq;
64
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);
67
68         c->status = libnet_RpcConnect_recv(ctx, s->ctx, c, &s->rpcconn);
69         if (!composite_is_ok(c)) return;
70
71         s->pipe = s->rpcconn.out.dcerpc_pipe;
72
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;
77
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;
83
84         /* callback handler */
85         tevent_req_set_callback(subreq, continue_domain_open_connect, c);
86 }
87
88
89 /**
90  * Stage 0.5 (optional): Close existing (in libnet context) domain
91  * handle
92  */
93 static void continue_domain_open_close(struct tevent_req *subreq)
94 {
95         struct composite_context *c;
96         struct domain_open_samr_state *s;
97
98         c = tevent_req_callback_data(subreq, struct composite_context);
99         s = talloc_get_type(c->private_data, struct domain_open_samr_state);
100
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;
105
106         if (s->monitor_fn) {
107                 struct monitor_msg msg;
108                 
109                 msg.type = mon_SamrClose;
110                 msg.data = NULL;
111                 msg.data_size = 0;
112                 s->monitor_fn(&msg);
113         }
114
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);
119
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;
124         
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;
130
131         /* callback handler */
132         tevent_req_set_callback(subreq, continue_domain_open_connect, c);
133 }
134
135
136 /**
137  * Stage 1: Connect to SAM server.
138  */
139 static void continue_domain_open_connect(struct tevent_req *subreq)
140 {
141         struct composite_context *c;
142         struct domain_open_samr_state *s;
143         struct samr_LookupDomain *r;
144         
145         c = tevent_req_callback_data(subreq, struct composite_context);
146         s = talloc_get_type(c->private_data, struct domain_open_samr_state);
147
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;
152
153         if (s->monitor_fn) {
154                 struct monitor_msg msg;
155
156                 msg.type = mon_SamrConnect;
157                 msg.data = NULL;
158                 msg.data_size = 0;
159                 s->monitor_fn(&msg);
160         }
161
162         r = &s->lookup;
163
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;
169
170         subreq = dcerpc_samr_LookupDomain_r_send(s, c->event_ctx,
171                                                  s->pipe->binding_handle,
172                                                  r);
173         if (composite_nomem(subreq, c)) return;
174
175         tevent_req_set_callback(subreq, continue_domain_open_lookup, c);
176 }
177
178
179 /**
180  * Stage 2: Lookup domain by name.
181  */
182 static void continue_domain_open_lookup(struct tevent_req *subreq)
183 {
184         struct composite_context *c;
185         struct domain_open_samr_state *s;
186         struct samr_OpenDomain *r;
187
188         c = tevent_req_callback_data(subreq, struct composite_context);
189         s = talloc_get_type(c->private_data, struct domain_open_samr_state);
190         
191         /* receive samr_LookupDomain reply */
192         c->status = dcerpc_samr_LookupDomain_r_recv(subreq, s);
193         TALLOC_FREE(subreq);
194
195         if (s->monitor_fn) {
196                 struct monitor_msg msg;
197                 struct msg_rpc_lookup_domain data;
198
199                 data.domain_name = s->domain_name.string;
200
201                 msg.type = mon_SamrLookupDomain;
202                 msg.data = (void*)&data;
203                 msg.data_size = sizeof(data);
204                 s->monitor_fn(&msg);
205         }
206
207         r = &s->open;
208
209         /* check the rpc layer status */
210         if (!composite_is_ok(c));
211
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;
216         }
217
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;
223
224         subreq = dcerpc_samr_OpenDomain_r_send(s, c->event_ctx,
225                                                s->pipe->binding_handle,
226                                                r);
227         if (composite_nomem(subreq, c)) return;
228
229         tevent_req_set_callback(subreq, continue_domain_open_open, c);
230 }
231
232
233 /*
234  * Stage 3: Open domain.
235  */
236 static void continue_domain_open_open(struct tevent_req *subreq)
237 {
238         struct composite_context *c;
239         struct domain_open_samr_state *s;
240
241         c = tevent_req_callback_data(subreq, struct composite_context);
242         s = talloc_get_type(c->private_data, struct domain_open_samr_state);
243
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;
248
249         if (s->monitor_fn) {
250                 struct monitor_msg msg;
251                 
252                 msg.type = mon_SamrOpenDomain;
253                 msg.data = NULL;
254                 msg.data_size = 0;
255                 s->monitor_fn(&msg);
256         }
257
258         composite_done(c);
259 }
260
261
262 /**
263  * Sends asynchronous DomainOpenSamr request
264  *
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
268  */
269
270 struct composite_context *libnet_DomainOpenSamr_send(struct libnet_context *ctx,
271                                                      struct libnet_DomainOpen *io,
272                                                      void (*monitor)(struct monitor_msg*))
273 {
274         struct composite_context *c;
275         struct domain_open_samr_state *s;
276         struct composite_context *rpcconn_req;
277         struct tevent_req *subreq;
278
279         c = composite_create(ctx, ctx->event_ctx);
280         if (c == NULL) return NULL;
281
282         s = talloc_zero(c, struct domain_open_samr_state);
283         if (composite_nomem(s, c)) return c;
284
285         c->private_data = s;
286         s->monitor_fn   = monitor;
287
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);
292
293         /* check, if there's samr pipe opened already, before opening a domain */
294         if (ctx->samr.pipe == NULL) {
295
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;
300                 
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;
304
305                 composite_continue(c, rpcconn_req, continue_domain_open_rpc_connect, c);
306                 return c;
307         }
308
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) {
314
315                         /* this domain is already opened */
316                         composite_done(c);
317                         return c;
318
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;
323
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;
329
330                         /* callback handler */
331                         tevent_req_set_callback(subreq, continue_domain_open_close, c);
332                         return c;
333                 }
334         }
335
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;
340         
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;
346
347         /* callback handler */
348         tevent_req_set_callback(subreq, continue_domain_open_connect, c);
349         return c;
350 }
351
352
353 /**
354  * Waits for and receives result of asynchronous DomainOpenSamr call
355  * 
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
361  */
362
363 NTSTATUS libnet_DomainOpenSamr_recv(struct composite_context *c, struct libnet_context *ctx,
364                                     TALLOC_CTX *mem_ctx, struct libnet_DomainOpen *io)
365 {
366         NTSTATUS status;
367         struct domain_open_samr_state *s;
368
369         /* wait for results of sending request */
370         status = composite_wait(c);
371         
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;
375
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;
383         }
384
385         talloc_free(c);
386         return status;
387 }
388
389
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;
398
399         /* information about the progress */
400         void (*monitor_fn)(struct monitor_msg*);
401 };
402
403
404 static void continue_rpc_connect_lsa(struct composite_context *ctx);
405 static void continue_lsa_policy_open(struct tevent_req *subreq);
406
407
408 /**
409  * Sends asynchronous DomainOpenLsa request
410  *
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
414  */
415
416 struct composite_context* libnet_DomainOpenLsa_send(struct libnet_context *ctx,
417                                                     struct libnet_DomainOpen *io,
418                                                     void (*monitor)(struct monitor_msg*))
419 {
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;
425
426         /* create composite context and state */
427         c = composite_create(ctx, ctx->event_ctx);
428         if (c == NULL) return c;
429
430         s = talloc_zero(c, struct domain_open_lsa_state);
431         if (composite_nomem(s, c)) return c;
432
433         c->private_data = s;
434
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;
439
440         /* check, if there's lsa pipe opened already, before opening a handle */
441         if (ctx->lsa.pipe == NULL) {
442
443                 ZERO_STRUCT(s->rpcconn);
444
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;
449                 
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;
453
454                 composite_continue(c, rpcconn_req, continue_rpc_connect_lsa, c);
455                 return c;
456         }
457
458         s->pipe = ctx->lsa.pipe;
459
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);
464
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;
470
471         s->openpol.in.attr->sec_qos = qos;
472         s->openpol.out.handle       = &s->handle;
473         
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;
479
480         tevent_req_set_callback(subreq, continue_lsa_policy_open, c);
481         return c;
482 }
483
484
485 /*
486   Stage 0.5 (optional): Rpc pipe connected, send lsa open policy request
487  */
488 static void continue_rpc_connect_lsa(struct composite_context *ctx)
489 {
490         struct composite_context *c;
491         struct domain_open_lsa_state *s;
492         struct lsa_QosInfo *qos;
493         struct tevent_req *subreq;
494
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);
497
498         /* receive rpc connection */
499         c->status = libnet_RpcConnect_recv(ctx, s->ctx, c, &s->rpcconn);
500         if (!composite_is_ok(c)) return;
501
502         /* RpcConnect function leaves the pipe in libnet context,
503            so get it from there */
504         s->pipe = s->ctx->lsa.pipe;
505
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);
510
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;
516
517         s->openpol.in.attr->sec_qos = qos;
518         s->openpol.out.handle       = &s->handle;
519
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;
525
526         tevent_req_set_callback(subreq, continue_lsa_policy_open, c);
527 }
528
529
530 /*
531   Stage 1: Lsa policy opened - we're done, if successfully
532  */
533 static void continue_lsa_policy_open(struct tevent_req *subreq)
534 {
535         struct composite_context *c;
536         struct domain_open_lsa_state *s;
537
538         c = tevent_req_callback_data(subreq, struct composite_context);
539         s = talloc_get_type(c->private_data, struct domain_open_lsa_state);
540
541         c->status = dcerpc_lsa_OpenPolicy2_r_recv(subreq, s);
542         TALLOC_FREE(subreq);
543         if (!composite_is_ok(c)) return;
544
545         if (s->monitor_fn) {
546                 struct monitor_msg msg;
547                 
548                 msg.type      = mon_LsaOpenPolicy;
549                 msg.data      = NULL;
550                 msg.data_size = 0;
551                 s->monitor_fn(&msg);
552         }
553
554         composite_done(c);
555 }
556
557
558 /**
559  * Receives result of asynchronous DomainOpenLsa call
560  *
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
566  */
567
568 NTSTATUS libnet_DomainOpenLsa_recv(struct composite_context *c, struct libnet_context *ctx,
569                                    TALLOC_CTX *mem_ctx, struct libnet_DomainOpen *io)
570 {
571         NTSTATUS status;
572         struct domain_open_lsa_state *s;
573
574         status = composite_wait(c);
575
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;
581
582                 ctx->lsa.handle      = s->handle;
583                 ctx->lsa.name        = talloc_steal(ctx, s->name);
584                 ctx->lsa.access_mask = s->access_mask;
585                 
586                 io->out.error_string = talloc_strdup(mem_ctx, "Success");
587
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));
593         }
594
595         talloc_free(c);
596         return status;
597 }
598
599
600 /**
601  * Sends a request to open a domain in desired service
602  *
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
606  */
607
608 struct composite_context* libnet_DomainOpen_send(struct libnet_context *ctx,
609                                                  struct libnet_DomainOpen *io,
610                                                  void (*monitor)(struct monitor_msg*))
611 {
612         struct composite_context *c;
613
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;
619
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;
625         }
626
627         return c;
628 }
629
630
631 /**
632  * Receive result of domain open request
633  *
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
638  */
639
640 NTSTATUS libnet_DomainOpen_recv(struct composite_context *c, struct libnet_context *ctx,
641                                 TALLOC_CTX *mem_ctx, struct libnet_DomainOpen *io)
642 {
643         NTSTATUS status;
644
645         switch (io->in.type) {
646         case DOMAIN_LSA:
647                 status = libnet_DomainOpenLsa_recv(c, ctx, mem_ctx, io);
648                 break;
649
650         case DOMAIN_SAMR:
651         default:
652                 status = libnet_DomainOpenSamr_recv(c, ctx, mem_ctx, io);
653                 break;
654         }
655         
656         return status;
657 }
658
659
660 /**
661  * Synchronous version of DomainOpen call
662  *
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
667  */
668
669 NTSTATUS libnet_DomainOpen(struct libnet_context *ctx,
670                            TALLOC_CTX *mem_ctx,
671                            struct libnet_DomainOpen *io)
672 {
673         struct composite_context *c = libnet_DomainOpen_send(ctx, io, NULL);
674         return libnet_DomainOpen_recv(c, ctx, mem_ctx, io);
675 }
676
677
678 struct domain_close_lsa_state {
679         struct dcerpc_pipe *pipe;
680         struct lsa_Close close;
681         struct policy_handle handle;
682
683         void (*monitor_fn)(struct monitor_msg*);
684 };
685
686
687 static void continue_lsa_close(struct tevent_req *subreq);
688
689
690 struct composite_context* libnet_DomainCloseLsa_send(struct libnet_context *ctx,
691                                                      struct libnet_DomainClose *io,
692                                                      void (*monitor)(struct monitor_msg*))
693 {
694         struct composite_context *c;
695         struct domain_close_lsa_state *s;
696         struct tevent_req *subreq;
697
698         /* composite context and state structure allocation */
699         c = composite_create(ctx, ctx->event_ctx);
700         if (c == NULL) return c;
701
702         s = talloc_zero(c, struct domain_close_lsa_state);
703         if (composite_nomem(s, c)) return c;
704
705         c->private_data = s;
706         s->monitor_fn   = monitor;
707
708         /* TODO: check if lsa pipe pointer is non-null */
709
710         if (!strequal(ctx->lsa.name, io->in.domain_name)) {
711                 composite_error(c, NT_STATUS_INVALID_PARAMETER);
712                 return c;
713         }
714
715         /* get opened lsarpc pipe pointer */
716         s->pipe = ctx->lsa.pipe;
717         
718         /* prepare close handle call arguments */
719         s->close.in.handle  = &ctx->lsa.handle;
720         s->close.out.handle = &s->handle;
721
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;
727
728         tevent_req_set_callback(subreq, continue_lsa_close, c);
729         return c;
730 }
731
732
733 /*
734   Stage 1: Receive result of lsa close call
735 */
736 static void continue_lsa_close(struct tevent_req *subreq)
737 {
738         struct composite_context *c;
739         struct domain_close_lsa_state *s;
740         
741         c = tevent_req_callback_data(subreq, struct composite_context);
742         s = talloc_get_type(c->private_data, struct domain_close_lsa_state);
743
744         c->status = dcerpc_lsa_Close_r_recv(subreq, s);
745         TALLOC_FREE(subreq);
746         if (!composite_is_ok(c)) return;
747
748         if (s->monitor_fn) {
749                 struct monitor_msg msg;
750
751                 msg.type      = mon_LsaClose;
752                 msg.data      = NULL;
753                 msg.data_size = 0;
754                 s->monitor_fn(&msg);
755         }
756
757         composite_done(c);
758 }
759
760
761 NTSTATUS libnet_DomainCloseLsa_recv(struct composite_context *c, struct libnet_context *ctx,
762                                     TALLOC_CTX *mem_ctx, struct libnet_DomainClose *io)
763 {
764         NTSTATUS status;
765
766         status = composite_wait(c);
767
768         if (NT_STATUS_IS_OK(status) && io) {
769                 /* policy handle closed successfully */
770
771                 ctx->lsa.name = NULL;
772                 ZERO_STRUCT(ctx->lsa.handle);
773
774                 io->out.error_string = talloc_asprintf(mem_ctx, "Success");
775
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));
779         }
780
781         talloc_free(c);
782         return status;
783 }
784
785
786 struct domain_close_samr_state {
787         struct samr_Close close;
788         struct policy_handle handle;
789         
790         void (*monitor_fn)(struct monitor_msg*);
791 };
792
793
794 static void continue_samr_close(struct tevent_req *subreq);
795
796
797 struct composite_context* libnet_DomainCloseSamr_send(struct libnet_context *ctx,
798                                                       struct libnet_DomainClose *io,
799                                                       void (*monitor)(struct monitor_msg*))
800 {
801         struct composite_context *c;
802         struct domain_close_samr_state *s;
803         struct tevent_req *subreq;
804
805         /* composite context and state structure allocation */
806         c = composite_create(ctx, ctx->event_ctx);
807         if (c == NULL) return c;
808
809         s = talloc_zero(c, struct domain_close_samr_state);
810         if (composite_nomem(s, c)) return c;
811
812         c->private_data = s;
813         s->monitor_fn   = monitor;
814
815         /* TODO: check if samr pipe pointer is non-null */
816
817         if (!strequal(ctx->samr.name, io->in.domain_name)) {
818                 composite_error(c, NT_STATUS_INVALID_PARAMETER);
819                 return c;
820         }
821
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;
826
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;
832
833         tevent_req_set_callback(subreq, continue_samr_close, c);
834         return c;
835 }
836
837
838 /*
839   Stage 1: Receive result of samr close call
840 */
841 static void continue_samr_close(struct tevent_req *subreq)
842 {
843         struct composite_context *c;
844         struct domain_close_samr_state *s;
845
846         c = tevent_req_callback_data(subreq, struct composite_context);
847         s = talloc_get_type(c->private_data, struct domain_close_samr_state);
848         
849         c->status = dcerpc_samr_Close_r_recv(subreq, s);
850         TALLOC_FREE(subreq);
851         if (!composite_is_ok(c)) return;
852
853         if (s->monitor_fn) {
854                 struct monitor_msg msg;
855                 
856                 msg.type      = mon_SamrClose;
857                 msg.data      = NULL;
858                 msg.data_size = 0;
859                 s->monitor_fn(&msg);
860         }
861         
862         composite_done(c);
863 }
864
865
866 NTSTATUS libnet_DomainCloseSamr_recv(struct composite_context *c, struct libnet_context *ctx,
867                                      TALLOC_CTX *mem_ctx, struct libnet_DomainClose *io)
868 {
869         NTSTATUS status;
870
871         status = composite_wait(c);
872
873         if (NT_STATUS_IS_OK(status) && io) {
874                 /* domain policy handle closed successfully */
875
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;
881
882                 io->out.error_string = talloc_asprintf(mem_ctx, "Success");
883
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));
887         }
888
889         talloc_free(c);
890         return status;
891 }
892
893
894 struct composite_context* libnet_DomainClose_send(struct libnet_context *ctx,
895                                                   struct libnet_DomainClose *io,
896                                                   void (*monitor)(struct monitor_msg*))
897 {
898         struct composite_context *c;
899
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;
905
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;
911         }
912         
913         return c;
914 }
915
916
917 NTSTATUS libnet_DomainClose_recv(struct composite_context *c, struct libnet_context *ctx,
918                                  TALLOC_CTX *mem_ctx, struct libnet_DomainClose *io)
919 {
920         NTSTATUS status;
921
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;
927
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;
933         }
934         
935         return status;
936 }
937
938
939 NTSTATUS libnet_DomainClose(struct libnet_context *ctx, TALLOC_CTX *mem_ctx,
940                             struct libnet_DomainClose *io)
941 {
942         struct composite_context *c;
943         
944         c = libnet_DomainClose_send(ctx, io, NULL);
945         return libnet_DomainClose_recv(c, ctx, mem_ctx, io);
946 }
947
948
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;
961
962         void (*monitor_fn)(struct monitor_msg*);
963 };
964
965
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);
970
971 static struct domainlist* get_domain_list(TALLOC_CTX *mem_ctx, struct domain_list_state *s);
972
973
974 /*
975   Stage 1: Receive connected rpc pipe and send connection
976   request to SAMR service
977 */
978 static void continue_rpc_connect(struct composite_context *ctx)
979 {
980         struct composite_context *c;
981         struct domain_list_state *s;
982         struct tevent_req *subreq;
983
984         c = talloc_get_type(ctx->async.private_data, struct composite_context);
985         s = talloc_get_type(c->private_data, struct domain_list_state);
986         
987         c->status = libnet_RpcConnect_recv(ctx, s->ctx, c, &s->rpcconn);
988         if (!composite_is_ok(c)) return;
989
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;
993
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;
998
999         tevent_req_set_callback(subreq, continue_samr_connect, c);
1000 }
1001
1002
1003 /*
1004   Stage 2: Receive policy handle to the connected SAMR service and issue
1005   a request to enumerate domain databases available
1006 */
1007 static void continue_samr_connect(struct tevent_req *subreq)
1008 {
1009         struct composite_context *c;
1010         struct domain_list_state *s;
1011
1012         c = tevent_req_callback_data(subreq, struct composite_context);
1013         s = talloc_get_type(c->private_data, struct domain_list_state);
1014         
1015         c->status = dcerpc_samr_Connect_r_recv(subreq, s);
1016         TALLOC_FREE(subreq);
1017         if (!composite_is_ok(c)) return;
1018
1019         if (s->monitor_fn) {
1020                 struct monitor_msg msg;
1021                 
1022                 msg.type      = mon_SamrConnect;
1023                 msg.data      = NULL;
1024                 msg.data_size = 0;
1025                 s->monitor_fn(&msg);
1026         }
1027
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;
1036
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;
1041
1042         tevent_req_set_callback(subreq, continue_samr_enum_domains, c);
1043 }
1044
1045
1046 /*
1047   Stage 3: Receive domain names available and repeat the request
1048   enumeration is not complete yet. Close samr connection handle
1049   upon completion.
1050 */
1051 static void continue_samr_enum_domains(struct tevent_req *subreq)
1052 {
1053         struct composite_context *c;
1054         struct domain_list_state *s;
1055
1056         c = tevent_req_callback_data(subreq, struct composite_context);
1057         s = talloc_get_type(c->private_data, struct domain_list_state);
1058         
1059         c->status = dcerpc_samr_EnumDomains_r_recv(subreq, s);
1060         TALLOC_FREE(subreq);
1061         if (!composite_is_ok(c)) return;
1062
1063         if (s->monitor_fn) {
1064                 struct monitor_msg msg;
1065                 
1066                 msg.type      = mon_SamrEnumDomains;
1067                 msg.data      = NULL;
1068                 msg.data_size = 0;
1069                 s->monitor_fn(&msg);
1070         }
1071
1072         if (NT_STATUS_IS_OK(s->enumdom.out.result)) {
1073
1074                 s->domains = get_domain_list(c, s);
1075
1076         } else if (NT_STATUS_EQUAL(s->enumdom.out.result, STATUS_MORE_ENTRIES)) {
1077                 
1078                 s->domains = get_domain_list(c, s);
1079                 
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;
1085
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;
1091
1092                 tevent_req_set_callback(subreq, continue_samr_enum_domains, c);
1093
1094         } else {
1095                 composite_error(c, s->enumdom.out.result);
1096                 return;
1097         }
1098
1099         /* close samr connection handle */
1100         s->samrclose.in.handle  = &s->connect_handle;
1101         s->samrclose.out.handle = &s->connect_handle;
1102         
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;
1108
1109         tevent_req_set_callback(subreq, continue_samr_close_handle, c);
1110 }
1111
1112
1113 /*
1114   Stage 4: Receive result of closing samr connection handle.
1115 */
1116 static void continue_samr_close_handle(struct tevent_req *subreq)
1117 {
1118         struct composite_context *c;
1119         struct domain_list_state *s;
1120
1121         c = tevent_req_callback_data(subreq, struct composite_context);
1122         s = talloc_get_type(c->private_data, struct domain_list_state);
1123
1124         c->status = dcerpc_samr_Close_r_recv(subreq, s);
1125         TALLOC_FREE(subreq);
1126         if (!composite_is_ok(c)) return;
1127
1128         if (s->monitor_fn) {
1129                 struct monitor_msg msg;
1130                 
1131                 msg.type      = mon_SamrClose;
1132                 msg.data      = NULL;
1133                 msg.data_size = 0;
1134                 s->monitor_fn(&msg);
1135         }
1136
1137         /* did everything go fine ? */
1138         if (!NT_STATUS_IS_OK(s->samrclose.out.result)) {
1139                 composite_error(c, s->samrclose.out.result);
1140         }
1141
1142         composite_done(c);
1143 }
1144
1145
1146 /*
1147   Utility function to copy domain names from result of samr_EnumDomains call
1148 */
1149 static struct domainlist* get_domain_list(TALLOC_CTX *mem_ctx, struct domain_list_state *s)
1150 {
1151         uint32_t i;
1152         if (mem_ctx == NULL || s == NULL) return NULL;
1153
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);
1161         }
1162
1163         /* copy domain names returned from samr_EnumDomains call */
1164         for (i = s->count; i < s->count + *s->enumdom.out.num_entries; i++)
1165         {
1166                 struct lsa_String *domain_name = &(*s->enumdom.out.sam)->entries[i - s->count].name;
1167
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 */
1172         }
1173
1174         /* number of entries returned (domains enumerated) */
1175         s->count += *s->enumdom.out.num_entries;
1176         
1177         return s->domains;
1178 }
1179
1180
1181 /**
1182  * Sends a request to list domains on given host
1183  *
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
1188  */
1189
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*))
1194 {
1195         struct composite_context *c;
1196         struct domain_list_state *s;
1197         struct composite_context *rpcconn_req;
1198         struct tevent_req *subreq;
1199
1200         /* composite context and state structure allocation */
1201         c = composite_create(ctx, ctx->event_ctx);
1202         if (c == NULL) return c;
1203
1204         s = talloc_zero(c, struct domain_list_state);
1205         if (composite_nomem(s, c)) return c;
1206
1207         c->private_data = s;
1208         s->monitor_fn   = monitor;
1209
1210         s->ctx      = ctx;
1211         s->hostname = talloc_strdup(c, io->in.hostname);
1212         if (composite_nomem(s->hostname, c)) return c;
1213
1214         /* check whether samr pipe has already been opened */
1215         if (ctx->samr.pipe == NULL) {
1216                 ZERO_STRUCT(s->rpcconn);
1217
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;
1222
1223                 rpcconn_req = libnet_RpcConnect_send(ctx, c, &s->rpcconn, s->monitor_fn);
1224                 if (composite_nomem(rpcconn_req, c)) return c;
1225                 
1226                 composite_continue(c, rpcconn_req, continue_rpc_connect, c);
1227
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;
1233                 
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;
1238
1239                 tevent_req_set_callback(subreq, continue_samr_connect, c);
1240         }
1241
1242         return c;
1243 }
1244
1245
1246 /**
1247  * Receive result of domain list request
1248  *
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
1253  */
1254
1255 NTSTATUS libnet_DomainList_recv(struct composite_context *c, struct libnet_context *ctx,
1256                                 TALLOC_CTX *mem_ctx, struct libnet_DomainList *io)
1257 {
1258         NTSTATUS status;
1259         struct domain_list_state *s;
1260
1261         status = composite_wait(c);
1262
1263         s = talloc_get_type(c->private_data, struct domain_list_state);
1264
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");
1270
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));
1274         }
1275
1276         talloc_free(c);
1277         return status;
1278 }
1279
1280
1281 /**
1282  * Synchronous version of DomainList call
1283  *
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
1288  */
1289
1290 NTSTATUS libnet_DomainList(struct libnet_context *ctx, TALLOC_CTX *mem_ctx,
1291                            struct libnet_DomainList *io)
1292 {
1293         struct composite_context *c;
1294
1295         c = libnet_DomainList_send(ctx, mem_ctx, io, NULL);
1296         return libnet_DomainList_recv(c, ctx, mem_ctx, io);
1297 }