s4/test: Remove duplicated test_opendomain() functionality
[kamenim/samba.git] / source4 / torture / libnet / libnet_group.c
1 /*
2    Unix SMB/CIFS implementation.
3    Test suite for libnet calls.
4
5    Copyright (C) Rafal Szczesniak  2007
6
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.
11
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.
16
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/>.
19 */
20
21
22 #include "includes.h"
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/rpc/torture_rpc.h"
28 #include "torture/libnet/proto.h"
29 #include "param/param.h"
30
31
32 #define TEST_GROUPNAME  "libnetgrouptest"
33
34
35 static bool test_cleanup(struct torture_context *tctx,
36                          struct dcerpc_binding_handle *b, TALLOC_CTX *mem_ctx,
37                          struct policy_handle *domain_handle, const char *groupname)
38 {
39         struct samr_LookupNames r1;
40         struct samr_OpenGroup r2;
41         struct samr_DeleteDomainGroup r3;
42         struct lsa_String names[2];
43         uint32_t rid;
44         struct policy_handle group_handle;
45         struct samr_Ids rids, types;
46
47         names[0].string = groupname;
48
49         r1.in.domain_handle  = domain_handle;
50         r1.in.num_names      = 1;
51         r1.in.names          = names;
52         r1.out.rids          = &rids;
53         r1.out.types         = &types;
54
55         torture_comment(tctx, "group account lookup '%s'\n", groupname);
56
57         torture_assert_ntstatus_ok(tctx,
58                 dcerpc_samr_LookupNames_r(b, mem_ctx, &r1),
59                 "LookupNames failed");
60         torture_assert_ntstatus_ok(tctx, r1.out.result,
61                 "LookupNames failed");
62
63         rid = r1.out.rids->ids[0];
64
65         r2.in.domain_handle  = domain_handle;
66         r2.in.access_mask    = SEC_FLAG_MAXIMUM_ALLOWED;
67         r2.in.rid            = rid;
68         r2.out.group_handle  = &group_handle;
69
70         torture_comment(tctx, "opening group account\n");
71
72         torture_assert_ntstatus_ok(tctx,
73                 dcerpc_samr_OpenGroup_r(b, mem_ctx, &r2),
74                 "OpenGroup failed");
75         torture_assert_ntstatus_ok(tctx, r2.out.result,
76                 "OpenGroup failed");
77
78         r3.in.group_handle  = &group_handle;
79         r3.out.group_handle = &group_handle;
80
81         torture_comment(tctx, "deleting group account\n");
82
83         torture_assert_ntstatus_ok(tctx,
84                 dcerpc_samr_DeleteDomainGroup_r(b, mem_ctx, &r3),
85                 "DeleteGroup failed");
86         torture_assert_ntstatus_ok(tctx, r3.out.result,
87                 "DeleteGroup failed");
88
89         return true;
90 }
91
92
93 static bool test_creategroup(struct torture_context *tctx,
94                              struct dcerpc_binding_handle *b, TALLOC_CTX *mem_ctx,
95                              struct policy_handle *handle, const char *name)
96 {
97         struct lsa_String groupname;
98         struct samr_CreateDomainGroup r;
99         struct policy_handle group_handle;
100         uint32_t group_rid;
101
102         groupname.string = name;
103
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;
109
110         torture_comment(tctx, "creating group account %s\n", name);
111
112         torture_assert_ntstatus_ok(tctx,
113                 dcerpc_samr_CreateDomainGroup_r(b, mem_ctx, &r),
114                 "CreateGroup failed");
115
116         if (!NT_STATUS_IS_OK(r.out.result)) {
117                 torture_comment(tctx, "CreateGroup failed - %s\n", nt_errstr(r.out.result));
118
119                 if (NT_STATUS_EQUAL(r.out.result, NT_STATUS_GROUP_EXISTS)) {
120                         torture_comment(tctx, "Group (%s) already exists - attempting to delete and recreate group again\n", name);
121                         if (!test_cleanup(tctx, b, mem_ctx, handle, TEST_GROUPNAME)) {
122                                 return false;
123                         }
124
125                         torture_comment(tctx, "creating group account\n");
126
127                         torture_assert_ntstatus_ok(tctx,
128                                 dcerpc_samr_CreateDomainGroup_r(b, mem_ctx, &r),
129                                 "CreateGroup failed");
130                         torture_assert_ntstatus_ok(tctx, r.out.result,
131                                 "CreateGroup failed");
132
133                         return true;
134                 }
135                 return false;
136         }
137
138         return true;
139 }
140
141
142 static bool test_lsa_close(struct torture_context *tctx,
143                            struct dcerpc_binding_handle *b, TALLOC_CTX *mem_ctx,
144                            struct policy_handle *domain_handle)
145 {
146         struct lsa_Close r;
147
148         r.in.handle = domain_handle;
149         r.out.handle = domain_handle;
150
151         torture_assert_ntstatus_ok(tctx,
152                 dcerpc_lsa_Close_r(b, mem_ctx, &r),
153                 "Close lsa domain failed");
154         torture_assert_ntstatus_ok(tctx, r.out.result,
155                 "Close lsa domain failed");
156
157         return true;
158 }
159
160
161 bool torture_groupinfo_api(struct torture_context *torture)
162 {
163         const char *name = TEST_GROUPNAME;
164         bool ret = true;
165         NTSTATUS status;
166         TALLOC_CTX *mem_ctx = NULL, *prep_mem_ctx;
167         struct libnet_context *ctx;
168         struct dcerpc_pipe *p;
169         struct policy_handle h;
170         struct lsa_String domain_name;
171         struct libnet_GroupInfo req;
172
173         prep_mem_ctx = talloc_init("prepare torture group info");
174
175         ctx = libnet_context_init(torture->ev, torture->lp_ctx);
176         ctx->cred = cmdline_credentials;
177
178         status = torture_rpc_connection(torture,
179                                         &p,
180                                         &ndr_table_samr);
181         if (!NT_STATUS_IS_OK(status)) {
182                 return false;
183         }
184
185         domain_name.string = lp_workgroup(torture->lp_ctx);
186         if (!test_domain_open(torture, p->binding_handle, &domain_name, prep_mem_ctx, &h, NULL)) {
187                 ret = false;
188                 goto done;
189         }
190
191         if (!test_creategroup(torture, p->binding_handle, prep_mem_ctx, &h, name)) {
192                 ret = false;
193                 goto done;
194         }
195
196         mem_ctx = talloc_init("torture group info");
197
198         ZERO_STRUCT(req);
199
200         req.in.domain_name = domain_name.string;
201         req.in.level = GROUP_INFO_BY_NAME;
202         req.in.data.group_name = name;
203
204         status = libnet_GroupInfo(ctx, mem_ctx, &req);
205         if (!NT_STATUS_IS_OK(status)) {
206                 torture_comment(torture, "libnet_GroupInfo call failed: %s\n", nt_errstr(status));
207                 ret = false;
208                 goto done;
209         }
210
211         if (!test_cleanup(torture, ctx->samr.pipe->binding_handle, mem_ctx, &ctx->samr.handle, TEST_GROUPNAME)) {
212                 torture_comment(torture, "cleanup failed\n");
213                 ret = false;
214                 goto done;
215         }
216
217         if (!test_samr_close_handle(torture,
218                                     ctx->samr.pipe->binding_handle, mem_ctx, &ctx->samr.handle)) {
219                 torture_comment(torture, "domain close failed\n");
220                 ret = false;
221         }
222
223         talloc_free(ctx);
224
225 done:
226         talloc_free(mem_ctx);
227         return ret;
228 }
229
230
231 bool torture_grouplist(struct torture_context *torture)
232 {
233         bool ret = true;
234         NTSTATUS status;
235         TALLOC_CTX *mem_ctx = NULL;
236         struct libnet_context *ctx;
237         struct lsa_String domain_name;
238         struct libnet_GroupList req;
239         int i;
240
241         ctx = libnet_context_init(torture->ev, torture->lp_ctx);
242         ctx->cred = cmdline_credentials;
243
244         domain_name.string = lp_workgroup(torture->lp_ctx);
245         mem_ctx = talloc_init("torture group list");
246
247         ZERO_STRUCT(req);
248
249         torture_comment(torture, "listing group accounts:\n");
250
251         do {
252                 req.in.domain_name  = domain_name.string;
253                 req.in.page_size    = 128;
254                 req.in.resume_index = req.out.resume_index;
255
256                 status = libnet_GroupList(ctx, mem_ctx, &req);
257                 if (!NT_STATUS_IS_OK(status) &&
258                     !NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) break;
259
260                 for (i = 0; i < req.out.count; i++) {
261                         torture_comment(torture, "\tgroup: %s, sid=%s\n",
262                                req.out.groups[i].groupname, req.out.groups[i].sid);
263                 }
264
265         } while (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES));
266
267         if (!(NT_STATUS_IS_OK(status) ||
268               NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES))) {
269                 torture_comment(torture, "libnet_GroupList call failed: %s\n", nt_errstr(status));
270                 ret = false;
271                 goto done;
272         }
273
274         if (!test_samr_close_handle(torture,
275                                     ctx->samr.pipe->binding_handle, mem_ctx, &ctx->samr.handle)) {
276                 torture_comment(torture, "domain close failed\n");
277                 ret = false;
278         }
279
280         if (!test_lsa_close(torture, ctx->lsa.pipe->binding_handle, mem_ctx, &ctx->lsa.handle)) {
281                 torture_comment(torture, "lsa domain close failed\n");
282                 ret = false;
283         }
284
285         talloc_free(ctx);
286
287 done:
288         talloc_free(mem_ctx);
289         return ret;
290 }
291
292
293 bool torture_creategroup(struct torture_context *torture)
294 {
295         bool ret = true;
296         NTSTATUS status;
297         TALLOC_CTX *mem_ctx = NULL;
298         struct libnet_context *ctx;
299         struct libnet_CreateGroup req;
300
301         mem_ctx = talloc_init("test_creategroup");
302
303         ctx = libnet_context_init(torture->ev, torture->lp_ctx);
304         ctx->cred = cmdline_credentials;
305
306         req.in.group_name = TEST_GROUPNAME;
307         req.in.domain_name = lp_workgroup(torture->lp_ctx);
308         req.out.error_string = NULL;
309
310         status = libnet_CreateGroup(ctx, mem_ctx, &req);
311         if (!NT_STATUS_IS_OK(status)) {
312                 torture_comment(torture, "libnet_CreateGroup call failed: %s\n", nt_errstr(status));
313                 ret = false;
314                 goto done;
315         }
316
317         if (!test_cleanup(torture, ctx->samr.pipe->binding_handle, mem_ctx, &ctx->samr.handle, TEST_GROUPNAME)) {
318                 torture_comment(torture, "cleanup failed\n");
319                 ret = false;
320                 goto done;
321         }
322
323         if (!test_samr_close_handle(torture,
324                                     ctx->samr.pipe->binding_handle, mem_ctx, &ctx->samr.handle)) {
325                 torture_comment(torture, "domain close failed\n");
326                 ret = false;
327         }
328
329 done:
330         talloc_free(ctx);
331         talloc_free(mem_ctx);
332         return ret;
333 }