r18105: Fix samr domain open, and implement samr domain close test.
[sfrench/samba-autobuild/.git] / source4 / torture / libnet / libnet_domain.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Test suite for libnet calls.
4
5    Copyright (C) Rafal Szczesniak 2006
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22
23 #include "includes.h"
24 #include "lib/cmdline/popt_common.h"
25 #include "lib/events/events.h"
26 #include "auth/credentials/credentials.h"
27 #include "libnet/libnet.h"
28 #include "librpc/gen_ndr/ndr_samr_c.h"
29 #include "librpc/gen_ndr/ndr_lsa_c.h"
30 #include "libcli/security/security.h"
31 #include "librpc/rpc/dcerpc.h"
32 #include "torture/torture.h"
33 #include "torture/rpc/rpc.h"
34
35
36 static BOOL test_opendomain_samr(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
37                                  struct policy_handle *handle, struct lsa_String *domname,
38                                  uint32_t *access_mask)
39 {
40         NTSTATUS status;
41         struct policy_handle h, domain_handle;
42         struct samr_Connect r1;
43         struct samr_LookupDomain r2;
44         struct samr_OpenDomain r3;
45         
46         printf("connecting\n");
47
48         *access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
49         
50         r1.in.system_name = 0;
51         r1.in.access_mask = *access_mask;
52         r1.out.connect_handle = &h;
53         
54         status = dcerpc_samr_Connect(p, mem_ctx, &r1);
55         if (!NT_STATUS_IS_OK(status)) {
56                 printf("Connect failed - %s\n", nt_errstr(status));
57                 return False;
58         }
59         
60         r2.in.connect_handle = &h;
61         r2.in.domain_name = domname;
62
63         printf("domain lookup on %s\n", domname->string);
64
65         status = dcerpc_samr_LookupDomain(p, mem_ctx, &r2);
66         if (!NT_STATUS_IS_OK(status)) {
67                 printf("LookupDomain failed - %s\n", nt_errstr(status));
68                 return False;
69         }
70
71         r3.in.connect_handle = &h;
72         r3.in.access_mask = *access_mask;
73         r3.in.sid = r2.out.sid;
74         r3.out.domain_handle = &domain_handle;
75
76         printf("opening domain\n");
77
78         status = dcerpc_samr_OpenDomain(p, mem_ctx, &r3);
79         if (!NT_STATUS_IS_OK(status)) {
80                 printf("OpenDomain failed - %s\n", nt_errstr(status));
81                 return False;
82         } else {
83                 *handle = domain_handle;
84         }
85
86         return True;
87 }
88
89
90 static BOOL test_opendomain_lsa(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
91                                 struct policy_handle *handle, struct lsa_String *domname,
92                                 uint32_t *access_mask)
93 {
94         NTSTATUS status;
95         struct lsa_OpenPolicy2 open;
96         struct lsa_ObjectAttribute attr;
97         struct lsa_QosInfo qos;
98
99         *access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
100
101         ZERO_STRUCT(attr);
102         ZERO_STRUCT(qos);
103
104         qos.len                 = 0;
105         qos.impersonation_level = 2;
106         qos.context_mode        = 1;
107         qos.effective_only      = 0;
108         
109         attr.sec_qos = &qos;
110
111         open.in.system_name = domname->string;
112         open.in.attr        = &attr;
113         open.in.access_mask = *access_mask;
114         open.out.handle     = handle;
115         
116         status = dcerpc_lsa_OpenPolicy2(p, mem_ctx, &open);
117         if (!NT_STATUS_IS_OK(status)) {
118                 return False;
119         }
120
121         return True;
122 }
123
124
125 BOOL torture_domain_open_lsa(struct torture_context *torture)
126 {
127         NTSTATUS status;
128         BOOL ret = True;
129         struct libnet_context *ctx;
130         struct libnet_DomainOpen r;
131         struct lsa_Close close;
132         struct dcerpc_binding *binding;
133         struct policy_handle h;
134         const char *bindstr;
135         
136         bindstr = lp_parm_string(-1, "torture", "binding");
137         status = dcerpc_parse_binding(torture, bindstr, &binding);
138         if (!NT_STATUS_IS_OK(status)) {
139                 d_printf("failed to parse binding string\n");
140                 return False;
141         }
142
143         ctx = libnet_context_init(NULL);
144         if (ctx == NULL) {
145                 d_printf("failed to create libnet context\n");
146                 return False;
147         }
148
149         ctx->cred = cmdline_credentials;
150
151         ZERO_STRUCT(r);
152         r.in.type = DOMAIN_LSA;
153         r.in.domain_name = binding->host;
154         r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
155
156         status = libnet_DomainOpen(ctx, torture, &r);
157         if (!NT_STATUS_IS_OK(status)) {
158                 d_printf("failed to open domain on lsa service: %s\n", nt_errstr(status));
159                 ret = False;
160                 goto done;
161         }
162
163         ZERO_STRUCT(close);
164         close.in.handle  = &ctx->lsa.handle;
165         close.out.handle = &h;
166         
167         status = dcerpc_lsa_Close(ctx->lsa.pipe, ctx, &close);
168         if (!NT_STATUS_IS_OK(status)) {
169                 d_printf("failed to close domain on lsa service: %s\n", nt_errstr(status));
170                 ret = False;
171         }
172
173 done:
174         talloc_free(ctx);
175         return ret;
176 }
177
178
179 BOOL torture_domain_close_lsa(struct torture_context *torture)
180 {
181         BOOL ret = True;
182         NTSTATUS status;
183         TALLOC_CTX *mem_ctx;
184         struct libnet_context *ctx;
185         struct lsa_String domain_name;
186         struct dcerpc_binding *binding;
187         const char *bindstr;
188         uint32_t access_mask;
189         struct policy_handle h;
190         struct dcerpc_pipe *p;
191         struct libnet_DomainClose r;
192
193         bindstr = lp_parm_string(-1, "torture", "binding");
194         status = dcerpc_parse_binding(torture, bindstr, &binding);
195         if (!NT_STATUS_IS_OK(status)) {
196                 d_printf("failed to parse binding string\n");
197                 return False;
198         }
199
200         ctx = libnet_context_init(NULL);
201         if (ctx == NULL) {
202                 d_printf("failed to create libnet context\n");
203                 ret = False;
204                 goto done;
205         }
206
207         ctx->cred = cmdline_credentials;
208
209         mem_ctx = talloc_init("torture_domain_close_lsa");
210         status = dcerpc_pipe_connect(mem_ctx, &p, bindstr, &dcerpc_table_lsarpc,
211                                      cmdline_credentials, NULL);
212         if (!NT_STATUS_IS_OK(status)) {
213                 d_printf("failed to connect to server %s: %s\n", bindstr,
214                          nt_errstr(status));
215                 ret = False;
216                 goto done;
217         }
218
219         domain_name.string = lp_workgroup();
220         
221         if (!test_opendomain_lsa(p, torture, &h, &domain_name, &access_mask)) {
222                 d_printf("failed to open domain on lsa service\n");
223                 ret = False;
224                 goto done;
225         }
226         
227         ctx->lsa.pipe        = p;
228         ctx->lsa.name        = domain_name.string;
229         ctx->lsa.access_mask = access_mask;
230         ctx->lsa.handle      = h;
231         /* we have to use pipe's event context, otherwise the call will
232            hang indefinately */
233         ctx->event_ctx       = p->conn->event_ctx;
234
235         ZERO_STRUCT(r);
236         r.in.type = DOMAIN_LSA;
237         r.in.domain_name = domain_name.string;
238         
239         status = libnet_DomainClose(ctx, mem_ctx, &r);
240         if (!NT_STATUS_IS_OK(status)) {
241                 ret = False;
242                 goto done;
243         }
244
245 done:
246         talloc_free(mem_ctx);
247         talloc_free(ctx);
248         return ret;
249 }
250
251
252 BOOL torture_domain_open_samr(struct torture_context *torture)
253 {
254         NTSTATUS status;
255         const char *binding;
256         struct libnet_context *ctx;
257         struct event_context *evt_ctx;
258         TALLOC_CTX *mem_ctx;
259         struct policy_handle domain_handle, handle;
260         struct lsa_String name;
261         struct libnet_DomainOpen io;
262         struct samr_Close r;
263         BOOL ret = True;
264
265         mem_ctx = talloc_init("test_domainopen_lsa");
266         binding = lp_parm_string(-1, "torture", "binding");
267
268         ctx = libnet_context_init(evt_ctx);
269         ctx->cred = cmdline_credentials;
270
271         name.string = lp_workgroup();
272
273         /*
274          * Testing synchronous version
275          */
276         printf("opening domain\n");
277         
278         io.in.type         = DOMAIN_SAMR;
279         io.in.domain_name  = name.string;
280         io.in.access_mask  = SEC_FLAG_MAXIMUM_ALLOWED;
281
282         status = libnet_DomainOpen(ctx, mem_ctx, &io);
283         if (!NT_STATUS_IS_OK(status)) {
284                 printf("Composite domain open failed - %s\n", nt_errstr(status));
285                 ret = False;
286                 goto done;
287         }
288
289         domain_handle = ctx->samr.handle;
290
291         r.in.handle   = &domain_handle;
292         r.out.handle  = &handle;
293         
294         printf("closing domain handle\n");
295         
296         status = dcerpc_samr_Close(ctx->samr.pipe, mem_ctx, &r);
297         if (!NT_STATUS_IS_OK(status)) {
298                 printf("Close failed - %s\n", nt_errstr(status));
299                 ret = False;
300                 goto done;
301         }
302
303 done:
304         talloc_free(mem_ctx);
305         talloc_free(ctx);
306
307         return ret;
308 }
309
310
311 BOOL torture_domain_close_samr(struct torture_context *torture)
312 {
313         BOOL ret = True;
314         NTSTATUS status;
315         TALLOC_CTX *mem_ctx;
316         struct libnet_context *ctx;
317         struct lsa_String domain_name;
318         struct dcerpc_binding *binding;
319         const char *bindstr;
320         uint32_t access_mask;
321         struct policy_handle h;
322         struct dcerpc_pipe *p;
323         struct libnet_DomainClose r;
324
325         bindstr = lp_parm_string(-1, "torture", "binding");
326         status = dcerpc_parse_binding(torture, bindstr, &binding);
327         if (!NT_STATUS_IS_OK(status)) {
328                 d_printf("failed to parse binding string\n");
329                 return False;
330         }
331
332         ctx = libnet_context_init(NULL);
333         if (ctx == NULL) {
334                 d_printf("failed to create libnet context\n");
335                 ret = False;
336                 goto done;
337         }
338
339         ctx->cred = cmdline_credentials;
340
341         mem_ctx = talloc_init("torture_domain_close_samr");
342         status = dcerpc_pipe_connect(mem_ctx, &p, bindstr, &dcerpc_table_samr,
343                                      cmdline_credentials, NULL);
344         if (!NT_STATUS_IS_OK(status)) {
345                 d_printf("failed to connect to server %s: %s\n", bindstr,
346                          nt_errstr(status));
347                 ret = False;
348                 goto done;
349         }
350
351         domain_name.string = lp_workgroup();
352         
353         if (!test_opendomain_samr(p, torture, &h, &domain_name, &access_mask)) {
354                 d_printf("failed to open domain on samr service\n");
355                 ret = False;
356                 goto done;
357         }
358         
359         ctx->samr.pipe        = p;
360         ctx->samr.name        = domain_name.string;
361         ctx->samr.access_mask = access_mask;
362         ctx->samr.handle      = h;
363         /* we have to use pipe's event context, otherwise the call will
364            hang indefinately */
365         ctx->event_ctx       = p->conn->event_ctx;
366
367         ZERO_STRUCT(r);
368         r.in.type = DOMAIN_SAMR;
369         r.in.domain_name = domain_name.string;
370         
371         status = libnet_DomainClose(ctx, mem_ctx, &r);
372         if (!NT_STATUS_IS_OK(status)) {
373                 ret = False;
374                 goto done;
375         }
376
377 done:
378         talloc_free(mem_ctx);
379         talloc_free(ctx);
380         return ret;
381 }