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;
46 names[0].string = groupname;
48 r1.in.domain_handle = domain_handle;
52 printf("group account lookup '%s'\n", groupname);
54 status = dcerpc_samr_LookupNames(p, mem_ctx, &r1);
55 if (!NT_STATUS_IS_OK(status)) {
56 printf("LookupNames failed - %s\n", nt_errstr(status));
60 rid = r1.out.rids.ids[0];
62 r2.in.domain_handle = domain_handle;
63 r2.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
65 r2.out.group_handle = &group_handle;
67 printf("opening group account\n");
69 status = dcerpc_samr_OpenGroup(p, mem_ctx, &r2);
70 if (!NT_STATUS_IS_OK(status)) {
71 printf("OpenGroup failed - %s\n", nt_errstr(status));
75 r3.in.group_handle = &group_handle;
76 r3.out.group_handle = &group_handle;
78 printf("deleting group account\n");
80 status = dcerpc_samr_DeleteDomainGroup(p, mem_ctx, &r3);
81 if (!NT_STATUS_IS_OK(status)) {
82 printf("DeleteGroup failed - %s\n", nt_errstr(status));
90 static BOOL test_creategroup(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
91 struct policy_handle *handle, const char *name)
94 struct lsa_String groupname;
95 struct samr_CreateDomainGroup r;
96 struct policy_handle group_handle;
99 groupname.string = name;
101 r.in.domain_handle = handle;
102 r.in.name = &groupname;
103 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
104 r.out.group_handle = &group_handle;
105 r.out.rid = &group_rid;
107 printf("creating group account %s\n", name);
109 status = dcerpc_samr_CreateDomainGroup(p, mem_ctx, &r);
110 if (!NT_STATUS_IS_OK(status)) {
111 printf("CreateGroup failed - %s\n", nt_errstr(status));
113 if (NT_STATUS_EQUAL(status, NT_STATUS_GROUP_EXISTS)) {
114 printf("Group (%s) already exists - attempting to delete and recreate group again\n", name);
115 if (!test_cleanup(p, mem_ctx, handle, TEST_GROUPNAME)) {
119 printf("creating group account\n");
121 status = dcerpc_samr_CreateDomainGroup(p, mem_ctx, &r);
122 if (!NT_STATUS_IS_OK(status)) {
123 printf("CreateGroup failed - %s\n", nt_errstr(status));
135 static BOOL test_opendomain(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
136 struct policy_handle *handle, struct lsa_String *domname)
139 struct policy_handle h, domain_handle;
140 struct samr_Connect r1;
141 struct samr_LookupDomain r2;
142 struct samr_OpenDomain r3;
144 printf("connecting\n");
146 r1.in.system_name = 0;
147 r1.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
148 r1.out.connect_handle = &h;
150 status = dcerpc_samr_Connect(p, mem_ctx, &r1);
151 if (!NT_STATUS_IS_OK(status)) {
152 printf("Connect failed - %s\n", nt_errstr(status));
156 r2.in.connect_handle = &h;
157 r2.in.domain_name = domname;
159 printf("domain lookup on %s\n", domname->string);
161 status = dcerpc_samr_LookupDomain(p, mem_ctx, &r2);
162 if (!NT_STATUS_IS_OK(status)) {
163 printf("LookupDomain failed - %s\n", nt_errstr(status));
167 r3.in.connect_handle = &h;
168 r3.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
169 r3.in.sid = r2.out.sid;
170 r3.out.domain_handle = &domain_handle;
172 printf("opening domain\n");
174 status = dcerpc_samr_OpenDomain(p, mem_ctx, &r3);
175 if (!NT_STATUS_IS_OK(status)) {
176 printf("OpenDomain failed - %s\n", nt_errstr(status));
179 *handle = domain_handle;
186 static BOOL test_samr_close(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
187 struct policy_handle *domain_handle)
192 r.in.handle = domain_handle;
193 r.out.handle = domain_handle;
195 status = dcerpc_samr_Close(p, mem_ctx, &r);
196 if (!NT_STATUS_IS_OK(status)) {
197 printf("Close samr domain failed - %s\n", nt_errstr(status));
205 BOOL torture_groupinfo_api(struct torture_context *torture)
207 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");
219 ctx = libnet_context_init(NULL);
220 ctx->cred = cmdline_credentials;
222 status = torture_rpc_connection(torture,
225 if (!NT_STATUS_IS_OK(status)) {
229 domain_name.string = lp_workgroup();
230 if (!test_opendomain(p, prep_mem_ctx, &h, &domain_name)) {
235 if (!test_creategroup(p, prep_mem_ctx, &h, name)) {
240 mem_ctx = talloc_init("torture group info");
244 req.in.domain_name = domain_name.string;
245 req.in.group_name = name;
247 status = libnet_GroupInfo(ctx, mem_ctx, &req);
248 if (!NT_STATUS_IS_OK(status)) {
249 printf("libnet_GroupInfo call failed: %s\n", nt_errstr(status));
251 talloc_free(mem_ctx);
255 if (!test_cleanup(ctx->samr.pipe, mem_ctx, &ctx->samr.handle, TEST_GROUPNAME)) {
256 printf("cleanup failed\n");
261 if (!test_samr_close(ctx->samr.pipe, mem_ctx, &ctx->samr.handle)) {
262 printf("domain close failed\n");
269 talloc_free(mem_ctx);