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