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"
31 #define TEST_GROUPNAME "libnetgrouptest"
34 static BOOL test_cleanup(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
35 struct policy_handle *domain_handle, const char *groupname)
38 struct samr_LookupNames r1;
39 struct samr_OpenGroup r2;
40 struct samr_DeleteDomainGroup r3;
41 struct lsa_String names[2];
43 struct policy_handle group_handle;
45 names[0].string = groupname;
47 r1.in.domain_handle = domain_handle;
51 printf("group account lookup '%s'\n", groupname);
53 status = dcerpc_samr_LookupNames(p, mem_ctx, &r1);
54 if (!NT_STATUS_IS_OK(status)) {
55 printf("LookupNames failed - %s\n", nt_errstr(status));
59 rid = r1.out.rids.ids[0];
61 r2.in.domain_handle = domain_handle;
62 r2.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
64 r2.out.group_handle = &group_handle;
66 printf("opening group account\n");
68 status = dcerpc_samr_OpenGroup(p, mem_ctx, &r2);
69 if (!NT_STATUS_IS_OK(status)) {
70 printf("OpenGroup failed - %s\n", nt_errstr(status));
74 r3.in.group_handle = &group_handle;
75 r3.out.group_handle = &group_handle;
77 printf("deleting group account\n");
79 status = dcerpc_samr_DeleteDomainGroup(p, mem_ctx, &r3);
80 if (!NT_STATUS_IS_OK(status)) {
81 printf("DeleteGroup failed - %s\n", nt_errstr(status));
89 static BOOL test_creategroup(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
90 struct policy_handle *handle, const char *name)
93 struct lsa_String groupname;
94 struct samr_CreateDomainGroup r;
95 struct policy_handle group_handle;
98 groupname.string = name;
100 r.in.domain_handle = handle;
101 r.in.name = &groupname;
102 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
103 r.out.group_handle = &group_handle;
104 r.out.rid = &group_rid;
106 printf("creating group account %s\n", name);
108 status = dcerpc_samr_CreateDomainGroup(p, mem_ctx, &r);
109 if (!NT_STATUS_IS_OK(status)) {
110 printf("CreateGroup failed - %s\n", nt_errstr(status));
112 if (NT_STATUS_EQUAL(status, NT_STATUS_GROUP_EXISTS)) {
113 printf("Group (%s) already exists - attempting to delete and recreate group again\n", name);
114 if (!test_cleanup(p, mem_ctx, handle, TEST_GROUPNAME)) {
118 printf("creating group account\n");
120 status = dcerpc_samr_CreateDomainGroup(p, mem_ctx, &r);
121 if (!NT_STATUS_IS_OK(status)) {
122 printf("CreateGroup failed - %s\n", nt_errstr(status));
134 static BOOL test_opendomain(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
135 struct policy_handle *handle, struct lsa_String *domname)
138 struct policy_handle h, domain_handle;
139 struct samr_Connect r1;
140 struct samr_LookupDomain r2;
141 struct samr_OpenDomain r3;
143 printf("connecting\n");
145 r1.in.system_name = 0;
146 r1.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
147 r1.out.connect_handle = &h;
149 status = dcerpc_samr_Connect(p, mem_ctx, &r1);
150 if (!NT_STATUS_IS_OK(status)) {
151 printf("Connect failed - %s\n", nt_errstr(status));
155 r2.in.connect_handle = &h;
156 r2.in.domain_name = domname;
158 printf("domain lookup on %s\n", domname->string);
160 status = dcerpc_samr_LookupDomain(p, mem_ctx, &r2);
161 if (!NT_STATUS_IS_OK(status)) {
162 printf("LookupDomain failed - %s\n", nt_errstr(status));
166 r3.in.connect_handle = &h;
167 r3.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
168 r3.in.sid = r2.out.sid;
169 r3.out.domain_handle = &domain_handle;
171 printf("opening domain\n");
173 status = dcerpc_samr_OpenDomain(p, mem_ctx, &r3);
174 if (!NT_STATUS_IS_OK(status)) {
175 printf("OpenDomain failed - %s\n", nt_errstr(status));
178 *handle = domain_handle;
185 static BOOL test_samr_close(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
186 struct policy_handle *domain_handle)
191 r.in.handle = domain_handle;
192 r.out.handle = domain_handle;
194 status = dcerpc_samr_Close(p, mem_ctx, &r);
195 if (!NT_STATUS_IS_OK(status)) {
196 printf("Close samr domain failed - %s\n", nt_errstr(status));
204 BOOL torture_groupinfo_api(struct torture_context *torture)
206 const char *name = TEST_GROUPNAME;
210 TALLOC_CTX *mem_ctx = NULL, *prep_mem_ctx;
211 struct libnet_context *ctx;
212 struct dcerpc_pipe *p;
213 struct policy_handle h;
214 struct lsa_String domain_name;
215 struct libnet_GroupInfo req;
217 prep_mem_ctx = talloc_init("prepare torture group info");
218 binding = torture_setting_string(torture, "binding", NULL);
220 ctx = libnet_context_init(NULL);
221 ctx->cred = cmdline_credentials;
223 status = torture_rpc_connection(prep_mem_ctx,
226 if (!NT_STATUS_IS_OK(status)) {
230 domain_name.string = lp_workgroup();
231 if (!test_opendomain(p, prep_mem_ctx, &h, &domain_name)) {
236 if (!test_creategroup(p, prep_mem_ctx, &h, name)) {
241 mem_ctx = talloc_init("torture group info");
245 req.in.domain_name = domain_name.string;
246 req.in.group_name = name;
248 status = libnet_GroupInfo(ctx, mem_ctx, &req);
249 if (!NT_STATUS_IS_OK(status)) {
250 printf("libnet_GroupInfo call failed: %s\n", nt_errstr(status));
252 talloc_free(mem_ctx);
256 if (!test_cleanup(ctx->samr.pipe, mem_ctx, &ctx->samr.handle, TEST_GROUPNAME)) {
257 printf("cleanup failed\n");
262 if (!test_samr_close(ctx->samr.pipe, mem_ctx, &ctx->samr.handle)) {
263 printf("domain close failed\n");
270 talloc_free(mem_ctx);