2 Unix SMB/CIFS implementation.
3 Test suite for libnet calls.
5 Copyright (C) Rafal Szczesniak 2007
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 "libnet/libnet.h"
25 #include "librpc/gen_ndr/ndr_samr_c.h"
26 #include "librpc/gen_ndr/ndr_lsa_c.h"
27 #include "torture/torture.h"
28 #include "torture/rpc/rpc.h"
29 #include "param/param.h"
32 #define TEST_GROUPNAME "libnetgrouptest"
35 static bool test_cleanup(struct dcerpc_pipe
*p
, TALLOC_CTX
*mem_ctx
,
36 struct policy_handle
*domain_handle
, const char *groupname
)
39 struct samr_LookupNames r1
;
40 struct samr_OpenGroup r2
;
41 struct samr_DeleteDomainGroup r3
;
42 struct lsa_String names
[2];
44 struct policy_handle group_handle
;
45 struct samr_Ids rids
, types
;
47 names
[0].string
= groupname
;
49 r1
.in
.domain_handle
= domain_handle
;
53 r1
.out
.types
= &types
;
55 printf("group account lookup '%s'\n", groupname
);
57 status
= dcerpc_samr_LookupNames(p
, mem_ctx
, &r1
);
58 if (!NT_STATUS_IS_OK(status
)) {
59 printf("LookupNames failed - %s\n", nt_errstr(status
));
63 rid
= r1
.out
.rids
->ids
[0];
65 r2
.in
.domain_handle
= domain_handle
;
66 r2
.in
.access_mask
= SEC_FLAG_MAXIMUM_ALLOWED
;
68 r2
.out
.group_handle
= &group_handle
;
70 printf("opening group account\n");
72 status
= dcerpc_samr_OpenGroup(p
, mem_ctx
, &r2
);
73 if (!NT_STATUS_IS_OK(status
)) {
74 printf("OpenGroup failed - %s\n", nt_errstr(status
));
78 r3
.in
.group_handle
= &group_handle
;
79 r3
.out
.group_handle
= &group_handle
;
81 printf("deleting group account\n");
83 status
= dcerpc_samr_DeleteDomainGroup(p
, mem_ctx
, &r3
);
84 if (!NT_STATUS_IS_OK(status
)) {
85 printf("DeleteGroup failed - %s\n", nt_errstr(status
));
93 static bool test_creategroup(struct dcerpc_pipe
*p
, TALLOC_CTX
*mem_ctx
,
94 struct policy_handle
*handle
, const char *name
)
97 struct lsa_String groupname
;
98 struct samr_CreateDomainGroup r
;
99 struct policy_handle group_handle
;
102 groupname
.string
= name
;
104 r
.in
.domain_handle
= handle
;
105 r
.in
.name
= &groupname
;
106 r
.in
.access_mask
= SEC_FLAG_MAXIMUM_ALLOWED
;
107 r
.out
.group_handle
= &group_handle
;
108 r
.out
.rid
= &group_rid
;
110 printf("creating group account %s\n", name
);
112 status
= dcerpc_samr_CreateDomainGroup(p
, mem_ctx
, &r
);
113 if (!NT_STATUS_IS_OK(status
)) {
114 printf("CreateGroup failed - %s\n", nt_errstr(status
));
116 if (NT_STATUS_EQUAL(status
, NT_STATUS_GROUP_EXISTS
)) {
117 printf("Group (%s) already exists - attempting to delete and recreate group again\n", name
);
118 if (!test_cleanup(p
, mem_ctx
, handle
, TEST_GROUPNAME
)) {
122 printf("creating group account\n");
124 status
= dcerpc_samr_CreateDomainGroup(p
, mem_ctx
, &r
);
125 if (!NT_STATUS_IS_OK(status
)) {
126 printf("CreateGroup failed - %s\n", nt_errstr(status
));
138 static bool test_opendomain(struct dcerpc_pipe
*p
, TALLOC_CTX
*mem_ctx
,
139 struct policy_handle
*handle
, struct lsa_String
*domname
)
142 struct policy_handle h
, domain_handle
;
143 struct samr_Connect r1
;
144 struct samr_LookupDomain r2
;
145 struct dom_sid2
*sid
= NULL
;
146 struct samr_OpenDomain r3
;
148 printf("connecting\n");
150 r1
.in
.system_name
= 0;
151 r1
.in
.access_mask
= SEC_FLAG_MAXIMUM_ALLOWED
;
152 r1
.out
.connect_handle
= &h
;
154 status
= dcerpc_samr_Connect(p
, mem_ctx
, &r1
);
155 if (!NT_STATUS_IS_OK(status
)) {
156 printf("Connect failed - %s\n", nt_errstr(status
));
160 r2
.in
.connect_handle
= &h
;
161 r2
.in
.domain_name
= domname
;
164 printf("domain lookup on %s\n", domname
->string
);
166 status
= dcerpc_samr_LookupDomain(p
, mem_ctx
, &r2
);
167 if (!NT_STATUS_IS_OK(status
)) {
168 printf("LookupDomain failed - %s\n", nt_errstr(status
));
172 r3
.in
.connect_handle
= &h
;
173 r3
.in
.access_mask
= SEC_FLAG_MAXIMUM_ALLOWED
;
174 r3
.in
.sid
= *r2
.out
.sid
;
175 r3
.out
.domain_handle
= &domain_handle
;
177 printf("opening domain\n");
179 status
= dcerpc_samr_OpenDomain(p
, mem_ctx
, &r3
);
180 if (!NT_STATUS_IS_OK(status
)) {
181 printf("OpenDomain failed - %s\n", nt_errstr(status
));
184 *handle
= domain_handle
;
191 static bool test_samr_close(struct dcerpc_pipe
*p
, TALLOC_CTX
*mem_ctx
,
192 struct policy_handle
*domain_handle
)
197 r
.in
.handle
= domain_handle
;
198 r
.out
.handle
= domain_handle
;
200 status
= dcerpc_samr_Close(p
, mem_ctx
, &r
);
201 if (!NT_STATUS_IS_OK(status
)) {
202 printf("Close samr domain failed - %s\n", nt_errstr(status
));
210 static bool test_lsa_close(struct dcerpc_pipe
*p
, TALLOC_CTX
*mem_ctx
,
211 struct policy_handle
*domain_handle
)
216 r
.in
.handle
= domain_handle
;
217 r
.out
.handle
= domain_handle
;
219 status
= dcerpc_lsa_Close(p
, mem_ctx
, &r
);
220 if (!NT_STATUS_IS_OK(status
)) {
221 printf("Close lsa domain failed - %s\n", nt_errstr(status
));
229 bool torture_groupinfo_api(struct torture_context
*torture
)
231 const char *name
= TEST_GROUPNAME
;
234 TALLOC_CTX
*mem_ctx
= NULL
, *prep_mem_ctx
;
235 struct libnet_context
*ctx
;
236 struct dcerpc_pipe
*p
;
237 struct policy_handle h
;
238 struct lsa_String domain_name
;
239 struct libnet_GroupInfo req
;
241 prep_mem_ctx
= talloc_init("prepare torture group info");
243 ctx
= libnet_context_init(torture
->ev
, torture
->lp_ctx
);
244 ctx
->cred
= cmdline_credentials
;
246 status
= torture_rpc_connection(torture
,
249 if (!NT_STATUS_IS_OK(status
)) {
253 domain_name
.string
= lp_workgroup(torture
->lp_ctx
);
254 if (!test_opendomain(p
, prep_mem_ctx
, &h
, &domain_name
)) {
259 if (!test_creategroup(p
, prep_mem_ctx
, &h
, name
)) {
264 mem_ctx
= talloc_init("torture group info");
268 req
.in
.domain_name
= domain_name
.string
;
269 req
.in
.level
= GROUP_INFO_BY_NAME
;
270 req
.in
.data
.group_name
= name
;
272 status
= libnet_GroupInfo(ctx
, mem_ctx
, &req
);
273 if (!NT_STATUS_IS_OK(status
)) {
274 printf("libnet_GroupInfo call failed: %s\n", nt_errstr(status
));
279 if (!test_cleanup(ctx
->samr
.pipe
, mem_ctx
, &ctx
->samr
.handle
, TEST_GROUPNAME
)) {
280 printf("cleanup failed\n");
285 if (!test_samr_close(ctx
->samr
.pipe
, mem_ctx
, &ctx
->samr
.handle
)) {
286 printf("domain close failed\n");
293 talloc_free(mem_ctx
);
298 bool torture_grouplist(struct torture_context
*torture
)
302 TALLOC_CTX
*mem_ctx
= NULL
;
303 struct libnet_context
*ctx
;
304 struct lsa_String domain_name
;
305 struct libnet_GroupList req
;
308 ctx
= libnet_context_init(torture
->ev
, torture
->lp_ctx
);
309 ctx
->cred
= cmdline_credentials
;
311 domain_name
.string
= lp_workgroup(torture
->lp_ctx
);
312 mem_ctx
= talloc_init("torture group list");
316 printf("listing group accounts:\n");
319 req
.in
.domain_name
= domain_name
.string
;
320 req
.in
.page_size
= 128;
321 req
.in
.resume_index
= req
.out
.resume_index
;
323 status
= libnet_GroupList(ctx
, mem_ctx
, &req
);
324 if (!NT_STATUS_IS_OK(status
) &&
325 !NT_STATUS_EQUAL(status
, STATUS_MORE_ENTRIES
)) break;
327 for (i
= 0; i
< req
.out
.count
; i
++) {
328 printf("\tgroup: %s, sid=%s\n",
329 req
.out
.groups
[i
].groupname
, req
.out
.groups
[i
].sid
);
332 } while (NT_STATUS_EQUAL(status
, STATUS_MORE_ENTRIES
));
334 if (!(NT_STATUS_IS_OK(status
) ||
335 NT_STATUS_EQUAL(status
, NT_STATUS_NO_MORE_ENTRIES
))) {
336 printf("libnet_GroupList call failed: %s\n", nt_errstr(status
));
341 if (!test_samr_close(ctx
->samr
.pipe
, mem_ctx
, &ctx
->samr
.handle
)) {
342 printf("domain close failed\n");
346 if (!test_lsa_close(ctx
->lsa
.pipe
, mem_ctx
, &ctx
->lsa
.handle
)) {
347 printf("lsa domain close failed\n");
354 talloc_free(mem_ctx
);
359 bool torture_creategroup(struct torture_context
*torture
)
363 TALLOC_CTX
*mem_ctx
= NULL
;
364 struct libnet_context
*ctx
;
365 struct libnet_CreateGroup req
;
367 mem_ctx
= talloc_init("test_creategroup");
369 ctx
= libnet_context_init(torture
->ev
, torture
->lp_ctx
);
370 ctx
->cred
= cmdline_credentials
;
372 req
.in
.group_name
= TEST_GROUPNAME
;
373 req
.in
.domain_name
= lp_workgroup(torture
->lp_ctx
);
374 req
.out
.error_string
= NULL
;
376 status
= libnet_CreateGroup(ctx
, mem_ctx
, &req
);
377 if (!NT_STATUS_IS_OK(status
)) {
378 printf("libnet_CreateGroup call failed: %s\n", nt_errstr(status
));
383 if (!test_cleanup(ctx
->samr
.pipe
, mem_ctx
, &ctx
->samr
.handle
, TEST_GROUPNAME
)) {
384 printf("cleanup failed\n");
389 if (!test_samr_close(ctx
->samr
.pipe
, mem_ctx
, &ctx
->samr
.handle
)) {
390 printf("domain close failed\n");
396 talloc_free(mem_ctx
);