r24681: add basic test of libnet_GroupInfo function.
[sfrench/samba-autobuild/.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/torture.h"
28 #include "torture/rpc/rpc.h"
29
30
31 #define TEST_GROUPNAME  "libnetgrouptest"
32
33
34 static BOOL test_cleanup(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
35                          struct policy_handle *domain_handle, const char *groupname)
36 {
37         NTSTATUS status;
38         struct samr_LookupNames r1;
39         struct samr_OpenGroup r2;
40         struct samr_DeleteDomainGroup r3;
41         struct lsa_String names[2];
42         uint32_t rid;
43         struct policy_handle group_handle;
44
45         names[0].string = groupname;
46
47         r1.in.domain_handle  = domain_handle;
48         r1.in.num_names      = 1;
49         r1.in.names          = names;
50         
51         printf("group account lookup '%s'\n", groupname);
52
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));
56                 return False;
57         }
58
59         rid = r1.out.rids.ids[0];
60         
61         r2.in.domain_handle  = domain_handle;
62         r2.in.access_mask    = SEC_FLAG_MAXIMUM_ALLOWED;
63         r2.in.rid            = rid;
64         r2.out.group_handle  = &group_handle;
65
66         printf("opening group account\n");
67
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));
71                 return False;
72         }
73
74         r3.in.group_handle  = &group_handle;
75         r3.out.group_handle = &group_handle;
76
77         printf("deleting group account\n");
78         
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));
82                 return False;
83         }
84
85         return True;
86 }
87
88
89 static BOOL test_creategroup(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
90                              struct policy_handle *handle, const char *name)
91 {
92         NTSTATUS status;
93         struct lsa_String groupname;
94         struct samr_CreateDomainGroup r;
95         struct policy_handle group_handle;
96         uint32_t group_rid;
97         
98         groupname.string = name;
99         
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;
105
106         printf("creating group account %s\n", name);
107
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));
111
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)) {
115                                 return False;
116                         }
117
118                         printf("creating group account\n");
119                         
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));
123                                 return False;
124                         }
125                         return True;
126                 }
127                 return False;
128         }
129
130         return True;
131 }
132
133
134 static BOOL test_opendomain(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
135                             struct policy_handle *handle, struct lsa_String *domname)
136 {
137         NTSTATUS status;
138         struct policy_handle h, domain_handle;
139         struct samr_Connect r1;
140         struct samr_LookupDomain r2;
141         struct samr_OpenDomain r3;
142         
143         printf("connecting\n");
144         
145         r1.in.system_name = 0;
146         r1.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
147         r1.out.connect_handle = &h;
148         
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));
152                 return False;
153         }
154         
155         r2.in.connect_handle = &h;
156         r2.in.domain_name = domname;
157
158         printf("domain lookup on %s\n", domname->string);
159
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));
163                 return False;
164         }
165
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;
170
171         printf("opening domain\n");
172
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));
176                 return False;
177         } else {
178                 *handle = domain_handle;
179         }
180
181         return True;
182 }
183
184
185 static BOOL test_samr_close(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
186                             struct policy_handle *domain_handle)
187 {
188         NTSTATUS status;
189         struct samr_Close r;
190   
191         r.in.handle = domain_handle;
192         r.out.handle = domain_handle;
193
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));
197                 return False;
198         }
199         
200         return True;
201 }
202
203
204 BOOL torture_groupinfo_api(struct torture_context *torture)
205 {
206         const char *name = TEST_GROUPNAME;
207         const char *binding;
208         BOOL ret = True;
209         NTSTATUS status;
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;
216
217         prep_mem_ctx = talloc_init("prepare torture group info");
218         binding = torture_setting_string(torture, "binding", NULL);
219
220         ctx = libnet_context_init(NULL);
221         ctx->cred = cmdline_credentials;
222
223         status = torture_rpc_connection(prep_mem_ctx,
224                                         &p,
225                                         &ndr_table_samr);
226         if (!NT_STATUS_IS_OK(status)) {
227                 return False;
228         }
229
230         domain_name.string = lp_workgroup();
231         if (!test_opendomain(p, prep_mem_ctx, &h, &domain_name)) {
232                 ret = False;
233                 goto done;
234         }
235
236         if (!test_creategroup(p, prep_mem_ctx, &h, name)) {
237                 ret = False;
238                 goto done;
239         }
240
241         mem_ctx = talloc_init("torture group info");
242
243         ZERO_STRUCT(req);
244         
245         req.in.domain_name = domain_name.string;
246         req.in.group_name   = name;
247
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));
251                 ret = False;
252                 talloc_free(mem_ctx);
253                 goto done;
254         }
255
256         if (!test_cleanup(ctx->samr.pipe, mem_ctx, &ctx->samr.handle, TEST_GROUPNAME)) {
257                 printf("cleanup failed\n");
258                 ret = False;
259                 goto done;
260         }
261
262         if (!test_samr_close(ctx->samr.pipe, mem_ctx, &ctx->samr.handle)) {
263                 printf("domain close failed\n");
264                 ret = False;
265         }
266
267         talloc_free(ctx);
268
269 done:
270         talloc_free(mem_ctx);
271         return ret;
272 }