2 Unix SMB/CIFS implementation.
3 Test suite for libnet calls.
5 Copyright (C) Rafal Szczesniak 2006
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 3 of the License, or
10 (at your option) any later version.
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.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "lib/cmdline/popt_common.h"
24 #include "lib/events/events.h"
25 #include "auth/credentials/credentials.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 #include "libcli/security/security.h"
30 #include "librpc/rpc/dcerpc.h"
31 #include "torture/torture.h"
32 #include "torture/rpc/rpc.h"
35 static BOOL test_opendomain_samr(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
36 struct policy_handle *handle, struct lsa_String *domname,
37 uint32_t *access_mask, struct dom_sid **sid)
40 struct policy_handle h, domain_handle;
41 struct samr_Connect r1;
42 struct samr_LookupDomain r2;
43 struct samr_OpenDomain r3;
45 printf("connecting\n");
47 *access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
49 r1.in.system_name = 0;
50 r1.in.access_mask = *access_mask;
51 r1.out.connect_handle = &h;
53 status = dcerpc_samr_Connect(p, mem_ctx, &r1);
54 if (!NT_STATUS_IS_OK(status)) {
55 printf("Connect failed - %s\n", nt_errstr(status));
59 r2.in.connect_handle = &h;
60 r2.in.domain_name = domname;
62 printf("domain lookup on %s\n", domname->string);
64 status = dcerpc_samr_LookupDomain(p, mem_ctx, &r2);
65 if (!NT_STATUS_IS_OK(status)) {
66 printf("LookupDomain failed - %s\n", nt_errstr(status));
70 r3.in.connect_handle = &h;
71 r3.in.access_mask = *access_mask;
72 r3.in.sid = *sid = r2.out.sid;
73 r3.out.domain_handle = &domain_handle;
75 printf("opening domain\n");
77 status = dcerpc_samr_OpenDomain(p, mem_ctx, &r3);
78 if (!NT_STATUS_IS_OK(status)) {
79 printf("OpenDomain failed - %s\n", nt_errstr(status));
82 *handle = domain_handle;
89 static BOOL test_opendomain_lsa(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
90 struct policy_handle *handle, struct lsa_String *domname,
91 uint32_t *access_mask)
94 struct lsa_OpenPolicy2 open;
95 struct lsa_ObjectAttribute attr;
96 struct lsa_QosInfo qos;
98 *access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
104 qos.impersonation_level = 2;
105 qos.context_mode = 1;
106 qos.effective_only = 0;
110 open.in.system_name = domname->string;
111 open.in.attr = &attr;
112 open.in.access_mask = *access_mask;
113 open.out.handle = handle;
115 status = dcerpc_lsa_OpenPolicy2(p, mem_ctx, &open);
116 if (!NT_STATUS_IS_OK(status)) {
123 bool torture_domain_open_lsa(struct torture_context *torture)
127 struct libnet_context *ctx;
128 struct libnet_DomainOpen r;
129 struct lsa_Close lsa_close;
130 struct policy_handle h;
131 const char *domain_name;
133 /* we're accessing domain controller so the domain name should be
134 passed (it's going to be resolved to dc name and address) instead
135 of specific server name. */
136 domain_name = lp_workgroup();
138 ctx = libnet_context_init(NULL);
140 d_printf("failed to create libnet context\n");
144 ctx->cred = cmdline_credentials;
147 r.in.type = DOMAIN_LSA;
148 r.in.domain_name = domain_name;
149 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
151 status = libnet_DomainOpen(ctx, torture, &r);
152 if (!NT_STATUS_IS_OK(status)) {
153 d_printf("failed to open domain on lsa service: %s\n", nt_errstr(status));
158 ZERO_STRUCT(lsa_close);
159 lsa_close.in.handle = &ctx->lsa.handle;
160 lsa_close.out.handle = &h;
162 status = dcerpc_lsa_Close(ctx->lsa.pipe, ctx, &lsa_close);
163 if (!NT_STATUS_IS_OK(status)) {
164 d_printf("failed to close domain on lsa service: %s\n", nt_errstr(status));
174 BOOL torture_domain_close_lsa(struct torture_context *torture)
178 TALLOC_CTX *mem_ctx=NULL;
179 struct libnet_context *ctx;
180 struct lsa_String domain_name;
181 struct dcerpc_binding *binding;
182 uint32_t access_mask;
183 struct policy_handle h;
184 struct dcerpc_pipe *p;
185 struct libnet_DomainClose r;
187 status = torture_rpc_binding(torture, &binding);
188 if (!NT_STATUS_IS_OK(status)) {
192 ctx = libnet_context_init(NULL);
194 d_printf("failed to create libnet context\n");
199 ctx->cred = cmdline_credentials;
201 mem_ctx = talloc_init("torture_domain_close_lsa");
202 status = dcerpc_pipe_connect_b(mem_ctx, &p, binding, &ndr_table_lsarpc,
203 cmdline_credentials, NULL);
204 if (!NT_STATUS_IS_OK(status)) {
205 d_printf("failed to connect to server: %s\n", nt_errstr(status));
210 domain_name.string = lp_workgroup();
212 if (!test_opendomain_lsa(p, torture, &h, &domain_name, &access_mask)) {
213 d_printf("failed to open domain on lsa service\n");
219 ctx->lsa.name = domain_name.string;
220 ctx->lsa.access_mask = access_mask;
222 /* we have to use pipe's event context, otherwise the call will
224 ctx->event_ctx = p->conn->event_ctx;
227 r.in.type = DOMAIN_LSA;
228 r.in.domain_name = domain_name.string;
230 status = libnet_DomainClose(ctx, mem_ctx, &r);
231 if (!NT_STATUS_IS_OK(status)) {
237 talloc_free(mem_ctx);
243 BOOL torture_domain_open_samr(struct torture_context *torture)
246 struct libnet_context *ctx;
247 struct event_context *evt_ctx=NULL;
249 struct policy_handle domain_handle, handle;
250 struct libnet_DomainOpen io;
252 const char *domain_name;
255 mem_ctx = talloc_init("test_domainopen_lsa");
257 ctx = libnet_context_init(evt_ctx);
258 ctx->cred = cmdline_credentials;
260 /* we're accessing domain controller so the domain name should be
261 passed (it's going to be resolved to dc name and address) instead
262 of specific server name. */
263 domain_name = lp_workgroup();
266 * Testing synchronous version
268 printf("opening domain\n");
270 io.in.type = DOMAIN_SAMR;
271 io.in.domain_name = domain_name;
272 io.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
274 status = libnet_DomainOpen(ctx, mem_ctx, &io);
275 if (!NT_STATUS_IS_OK(status)) {
276 printf("Composite domain open failed - %s\n", nt_errstr(status));
281 domain_handle = ctx->samr.handle;
283 r.in.handle = &domain_handle;
284 r.out.handle = &handle;
286 printf("closing domain handle\n");
288 status = dcerpc_samr_Close(ctx->samr.pipe, mem_ctx, &r);
289 if (!NT_STATUS_IS_OK(status)) {
290 printf("Close failed - %s\n", nt_errstr(status));
296 talloc_free(mem_ctx);
303 BOOL torture_domain_close_samr(struct torture_context *torture)
307 TALLOC_CTX *mem_ctx = NULL;
308 struct libnet_context *ctx;
309 struct lsa_String domain_name;
310 struct dcerpc_binding *binding;
311 uint32_t access_mask;
312 struct policy_handle h;
313 struct dcerpc_pipe *p;
314 struct libnet_DomainClose r;
317 status = torture_rpc_binding(torture, &binding);
318 if (!NT_STATUS_IS_OK(status)) {
322 ctx = libnet_context_init(NULL);
324 d_printf("failed to create libnet context\n");
329 ctx->cred = cmdline_credentials;
331 mem_ctx = talloc_init("torture_domain_close_samr");
332 status = dcerpc_pipe_connect_b(mem_ctx, &p, binding, &ndr_table_samr,
334 if (!NT_STATUS_IS_OK(status)) {
335 d_printf("failed to connect to server: %s\n", nt_errstr(status));
340 domain_name.string = talloc_strdup(mem_ctx, lp_workgroup());
342 if (!test_opendomain_samr(p, torture, &h, &domain_name, &access_mask, &sid)) {
343 d_printf("failed to open domain on samr service\n");
349 ctx->samr.name = talloc_steal(ctx, domain_name.string);
350 ctx->samr.access_mask = access_mask;
351 ctx->samr.handle = h;
352 ctx->samr.sid = talloc_steal(ctx, sid);
353 /* we have to use pipe's event context, otherwise the call will
354 hang indefinitely - this wouldn't be the case if pipe was opened
355 by means of libnet call */
356 ctx->event_ctx = p->conn->event_ctx;
359 r.in.type = DOMAIN_SAMR;
360 r.in.domain_name = domain_name.string;
362 status = libnet_DomainClose(ctx, mem_ctx, &r);
363 if (!NT_STATUS_IS_OK(status)) {
369 talloc_free(mem_ctx);
375 BOOL torture_domain_list(struct torture_context *torture)
379 TALLOC_CTX *mem_ctx = NULL;
380 struct dcerpc_binding *binding;
381 struct libnet_context *ctx;
382 struct libnet_DomainList r;
385 status = torture_rpc_binding(torture, &binding);
386 if (!NT_STATUS_IS_OK(status)) {
390 ctx = libnet_context_init(NULL);
392 d_printf("failed to create libnet context\n");
397 ctx->cred = cmdline_credentials;
399 mem_ctx = talloc_init("torture_domain_close_samr");
402 * querying the domain list using default buffer size
406 r.in.hostname = binding->host;
408 status = libnet_DomainList(ctx, mem_ctx, &r);
409 if (!NT_STATUS_IS_OK(status)) {
414 d_printf("Received list or domains (everything in one piece):\n");
416 for (i = 0; i < r.out.count; i++) {
417 d_printf("Name[%d]: %s\n", i, r.out.domains[i].name);
421 * querying the domain list using specified (much smaller) buffer size
424 ctx->samr.buf_size = 32;
427 r.in.hostname = binding->host;
429 status = libnet_DomainList(ctx, mem_ctx, &r);
430 if (!NT_STATUS_IS_OK(status)) {
435 d_printf("Received list or domains (collected in more than one round):\n");
437 for (i = 0; i < r.out.count; i++) {
438 d_printf("Name[%d]: %s\n", i, r.out.domains[i].name);
442 d_printf("\nStatus: %s\n", nt_errstr(status));
444 talloc_free(mem_ctx);