0d8249f9f3aa5bdd66f863016d0074d432d14323
[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_lsa_close(struct torture_context *tctx,
36                            struct dcerpc_binding_handle *b, TALLOC_CTX *mem_ctx,
37                            struct policy_handle *domain_handle)
38 {
39         struct lsa_Close r;
40
41         r.in.handle = domain_handle;
42         r.out.handle = domain_handle;
43
44         torture_assert_ntstatus_ok(tctx,
45                 dcerpc_lsa_Close_r(b, mem_ctx, &r),
46                 "Close lsa domain failed");
47         torture_assert_ntstatus_ok(tctx, r.out.result,
48                 "Close lsa domain failed");
49
50         return true;
51 }
52
53
54 bool torture_groupinfo_api(struct torture_context *torture)
55 {
56         const char *name = TEST_GROUPNAME;
57         bool ret = true;
58         NTSTATUS status;
59         TALLOC_CTX *mem_ctx = NULL, *prep_mem_ctx;
60         struct libnet_context *ctx = NULL;
61         struct dcerpc_pipe *p;
62         struct policy_handle h;
63         struct lsa_String domain_name;
64         struct libnet_GroupInfo req;
65
66         prep_mem_ctx = talloc_init("prepare torture group info");
67
68         status = torture_rpc_connection(torture,
69                                         &p,
70                                         &ndr_table_samr);
71         if (!NT_STATUS_IS_OK(status)) {
72                 return false;
73         }
74
75         domain_name.string = lp_workgroup(torture->lp_ctx);
76         if (!test_domain_open(torture, p->binding_handle, &domain_name, prep_mem_ctx, &h, NULL)) {
77                 ret = false;
78                 goto done;
79         }
80
81         if (!test_group_create(torture, p->binding_handle, prep_mem_ctx, &h, name, NULL)) {
82                 ret = false;
83                 goto done;
84         }
85
86         mem_ctx = talloc_init("torture group info");
87
88         if (!test_libnet_context_init(torture, true, &ctx)) {
89                 return false;
90         }
91
92         ZERO_STRUCT(req);
93
94         req.in.domain_name = domain_name.string;
95         req.in.level = GROUP_INFO_BY_NAME;
96         req.in.data.group_name = name;
97
98         status = libnet_GroupInfo(ctx, mem_ctx, &req);
99         if (!NT_STATUS_IS_OK(status)) {
100                 torture_comment(torture, "libnet_GroupInfo call failed: %s\n", nt_errstr(status));
101                 ret = false;
102                 goto done;
103         }
104
105         if (!test_group_cleanup(torture, ctx->samr.pipe->binding_handle, mem_ctx, &ctx->samr.handle, TEST_GROUPNAME)) {
106                 torture_comment(torture, "cleanup failed\n");
107                 ret = false;
108                 goto done;
109         }
110
111         if (!test_samr_close_handle(torture,
112                                     ctx->samr.pipe->binding_handle, mem_ctx, &ctx->samr.handle)) {
113                 torture_comment(torture, "domain close failed\n");
114                 ret = false;
115         }
116
117 done:
118         talloc_free(ctx);
119         talloc_free(mem_ctx);
120         return ret;
121 }
122
123
124 bool torture_grouplist(struct torture_context *torture)
125 {
126         bool ret = true;
127         NTSTATUS status;
128         TALLOC_CTX *mem_ctx = NULL;
129         struct libnet_context *ctx;
130         struct lsa_String domain_name;
131         struct libnet_GroupList req;
132         int i;
133
134         ctx = libnet_context_init(torture->ev, torture->lp_ctx);
135         ctx->cred = cmdline_credentials;
136
137         domain_name.string = lp_workgroup(torture->lp_ctx);
138         mem_ctx = talloc_init("torture group list");
139
140         ZERO_STRUCT(req);
141
142         torture_comment(torture, "listing group accounts:\n");
143
144         do {
145                 req.in.domain_name  = domain_name.string;
146                 req.in.page_size    = 128;
147                 req.in.resume_index = req.out.resume_index;
148
149                 status = libnet_GroupList(ctx, mem_ctx, &req);
150                 if (!NT_STATUS_IS_OK(status) &&
151                     !NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) break;
152
153                 for (i = 0; i < req.out.count; i++) {
154                         torture_comment(torture, "\tgroup: %s, sid=%s\n",
155                                req.out.groups[i].groupname, req.out.groups[i].sid);
156                 }
157
158         } while (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES));
159
160         if (!(NT_STATUS_IS_OK(status) ||
161               NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES))) {
162                 torture_comment(torture, "libnet_GroupList call failed: %s\n", nt_errstr(status));
163                 ret = false;
164                 goto done;
165         }
166
167         if (!test_samr_close_handle(torture,
168                                     ctx->samr.pipe->binding_handle, mem_ctx, &ctx->samr.handle)) {
169                 torture_comment(torture, "domain close failed\n");
170                 ret = false;
171         }
172
173         if (!test_lsa_close(torture, ctx->lsa.pipe->binding_handle, mem_ctx, &ctx->lsa.handle)) {
174                 torture_comment(torture, "lsa domain close failed\n");
175                 ret = false;
176         }
177
178         talloc_free(ctx);
179
180 done:
181         talloc_free(mem_ctx);
182         return ret;
183 }
184
185
186 bool torture_creategroup(struct torture_context *torture)
187 {
188         bool ret = true;
189         NTSTATUS status;
190         TALLOC_CTX *mem_ctx = NULL;
191         struct libnet_context *ctx;
192         struct libnet_CreateGroup req;
193
194         mem_ctx = talloc_init("test_creategroup");
195
196         ctx = libnet_context_init(torture->ev, torture->lp_ctx);
197         ctx->cred = cmdline_credentials;
198
199         req.in.group_name = TEST_GROUPNAME;
200         req.in.domain_name = lp_workgroup(torture->lp_ctx);
201         req.out.error_string = NULL;
202
203         status = libnet_CreateGroup(ctx, mem_ctx, &req);
204         if (!NT_STATUS_IS_OK(status)) {
205                 torture_comment(torture, "libnet_CreateGroup call failed: %s\n", nt_errstr(status));
206                 ret = false;
207                 goto done;
208         }
209
210         if (!test_group_cleanup(torture, ctx->samr.pipe->binding_handle, mem_ctx, &ctx->samr.handle, TEST_GROUPNAME)) {
211                 torture_comment(torture, "cleanup failed\n");
212                 ret = false;
213                 goto done;
214         }
215
216         if (!test_samr_close_handle(torture,
217                                     ctx->samr.pipe->binding_handle, mem_ctx, &ctx->samr.handle)) {
218                 torture_comment(torture, "domain close failed\n");
219                 ret = false;
220         }
221
222 done:
223         talloc_free(ctx);
224         talloc_free(mem_ctx);
225         return ret;
226 }