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/>.
22 #include "torture/rpc/rpc.h"
23 #include "libnet/libnet.h"
24 #include "libcli/security/security.h"
25 #include "librpc/gen_ndr/ndr_samr_c.h"
27 #define TEST_GROUPNAME "libnetgroupinfotest"
30 static BOOL test_opendomain(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
31 struct policy_handle *handle, struct lsa_String *domname,
35 struct policy_handle h, domain_handle;
36 struct samr_Connect r1;
37 struct samr_LookupDomain r2;
38 struct samr_OpenDomain r3;
40 printf("connecting\n");
42 r1.in.system_name = 0;
43 r1.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
44 r1.out.connect_handle = &h;
46 status = dcerpc_samr_Connect(p, mem_ctx, &r1);
47 if (!NT_STATUS_IS_OK(status)) {
48 printf("Connect failed - %s\n", nt_errstr(status));
52 r2.in.connect_handle = &h;
53 r2.in.domain_name = domname;
55 printf("domain lookup on %s\n", domname->string);
57 status = dcerpc_samr_LookupDomain(p, mem_ctx, &r2);
58 if (!NT_STATUS_IS_OK(status)) {
59 printf("LookupDomain failed - %s\n", nt_errstr(status));
63 r3.in.connect_handle = &h;
64 r3.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
65 r3.in.sid = r2.out.sid;
66 r3.out.domain_handle = &domain_handle;
68 printf("opening domain\n");
70 status = dcerpc_samr_OpenDomain(p, mem_ctx, &r3);
71 if (!NT_STATUS_IS_OK(status)) {
72 printf("OpenDomain failed - %s\n", nt_errstr(status));
75 *handle = domain_handle;
83 static BOOL test_cleanup(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
84 struct policy_handle *domain_handle, const char *groupname)
87 struct samr_LookupNames r1;
88 struct samr_OpenGroup r2;
89 struct samr_DeleteDomainGroup r3;
90 struct lsa_String names[2];
92 struct policy_handle group_handle;
94 names[0].string = groupname;
96 r1.in.domain_handle = domain_handle;
100 printf("group account lookup '%s'\n", groupname);
102 status = dcerpc_samr_LookupNames(p, mem_ctx, &r1);
103 if (!NT_STATUS_IS_OK(status)) {
104 printf("LookupNames failed - %s\n", nt_errstr(status));
108 rid = r1.out.rids.ids[0];
110 r2.in.domain_handle = domain_handle;
111 r2.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
113 r2.out.group_handle = &group_handle;
115 printf("opening group account\n");
117 status = dcerpc_samr_OpenGroup(p, mem_ctx, &r2);
118 if (!NT_STATUS_IS_OK(status)) {
119 printf("OpenUser failed - %s\n", nt_errstr(status));
123 r3.in.group_handle = &group_handle;
124 r3.out.group_handle = &group_handle;
126 printf("deleting group account\n");
128 status = dcerpc_samr_DeleteDomainGroup(p, mem_ctx, &r3);
129 if (!NT_STATUS_IS_OK(status)) {
130 printf("DeleteGroup failed - %s\n", nt_errstr(status));
138 static BOOL test_create(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
139 struct policy_handle *handle, const char *name, uint32_t *rid)
142 struct lsa_String groupname;
143 struct samr_CreateDomainGroup r;
144 struct policy_handle group_handle;
146 groupname.string = name;
148 r.in.domain_handle = handle;
149 r.in.name = &groupname;
150 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
151 r.out.group_handle = &group_handle;
154 printf("creating group account %s\n", name);
156 status = dcerpc_samr_CreateDomainGroup(p, mem_ctx, &r);
157 if (!NT_STATUS_IS_OK(status)) {
158 printf("CreateGroup failed - %s\n", nt_errstr(status));
160 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
161 printf("Group (%s) already exists - attempting to delete and recreate account again\n", name);
162 if (!test_cleanup(p, mem_ctx, handle, TEST_GROUPNAME)) {
166 printf("creating group account\n");
168 status = dcerpc_samr_CreateDomainGroup(p, mem_ctx, &r);
169 if (!NT_STATUS_IS_OK(status)) {
170 printf("CreateGroup failed - %s\n", nt_errstr(status));
182 static BOOL test_groupinfo(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
183 struct policy_handle *domain_handle,
184 struct dom_sid2 *domain_sid, const char* group_name,
187 const uint16_t level = 5;
189 struct libnet_rpc_groupinfo group;
190 struct dom_sid *group_sid;
192 group_sid = dom_sid_add_rid(mem_ctx, domain_sid, *rid);
194 group.in.domain_handle = *domain_handle;
195 group.in.sid = dom_sid_string(mem_ctx, group_sid);
196 group.in.level = level; /* this should be extended */
198 printf("Testing sync libnet_rpc_groupinfo (SID argument)\n");
199 status = libnet_rpc_groupinfo(p, mem_ctx, &group);
200 if (!NT_STATUS_IS_OK(status)) {
201 printf("Failed to call sync libnet_rpc_userinfo - %s\n", nt_errstr(status));
207 group.in.domain_handle = *domain_handle;
209 group.in.groupname = TEST_GROUPNAME;
210 group.in.level = level;
212 printf("Testing sync libnet_rpc_groupinfo (groupname argument)\n");
213 status = libnet_rpc_groupinfo(p, mem_ctx, &group);
214 if (!NT_STATUS_IS_OK(status)) {
215 printf("Failed to call sync libnet_rpc_groupinfo - %s\n", nt_errstr(status));
223 BOOL torture_groupinfo(struct torture_context *torture)
226 struct dcerpc_pipe *p;
229 struct policy_handle h;
230 struct lsa_String name;
234 mem_ctx = talloc_init("test_userinfo");
236 status = torture_rpc_connection(torture,
240 if (!NT_STATUS_IS_OK(status)) {
244 name.string = lp_workgroup();
247 * Testing synchronous version
249 if (!test_opendomain(p, mem_ctx, &h, &name, &sid)) {
254 if (!test_create(p, mem_ctx, &h, TEST_GROUPNAME, &rid)) {
259 if (!test_groupinfo(p, mem_ctx, &h, &sid, TEST_GROUPNAME, &rid)) {
264 if (!test_cleanup(p, mem_ctx, &h, TEST_GROUPNAME)) {
270 talloc_free(mem_ctx);