0dca1519ded24c59c2d1b6d71ed5cf992bb8d6c0
[gd/samba-autobuild/.git] / source4 / torture / libnet / groupinfo.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 #include "includes.h"
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"
26
27 #define TEST_GROUPNAME  "libnetgroupinfotest"
28
29
30 static BOOL test_opendomain(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
31                             struct policy_handle *handle, struct lsa_String *domname,
32                             struct dom_sid2 *sid)
33 {
34         NTSTATUS status;
35         struct policy_handle h, domain_handle;
36         struct samr_Connect r1;
37         struct samr_LookupDomain r2;
38         struct samr_OpenDomain r3;
39         
40         printf("connecting\n");
41         
42         r1.in.system_name = 0;
43         r1.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
44         r1.out.connect_handle = &h;
45         
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));
49                 return False;
50         }
51         
52         r2.in.connect_handle = &h;
53         r2.in.domain_name = domname;
54
55         printf("domain lookup on %s\n", domname->string);
56
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));
60                 return False;
61         }
62
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;
67
68         printf("opening domain\n");
69
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));
73                 return False;
74         } else {
75                 *handle = domain_handle;
76         }
77
78         *sid = *r2.out.sid;
79         return True;
80 }
81
82
83 static BOOL test_cleanup(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
84                          struct policy_handle *domain_handle, const char *groupname)
85 {
86         NTSTATUS status;
87         struct samr_LookupNames r1;
88         struct samr_OpenGroup r2;
89         struct samr_DeleteDomainGroup r3;
90         struct lsa_String names[2];
91         uint32_t rid;
92         struct policy_handle group_handle;
93
94         names[0].string = groupname;
95
96         r1.in.domain_handle  = domain_handle;
97         r1.in.num_names      = 1;
98         r1.in.names          = names;
99         
100         printf("group account lookup '%s'\n", groupname);
101
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));
105                 return False;
106         }
107
108         rid = r1.out.rids.ids[0];
109         
110         r2.in.domain_handle  = domain_handle;
111         r2.in.access_mask    = SEC_FLAG_MAXIMUM_ALLOWED;
112         r2.in.rid            = rid;
113         r2.out.group_handle  = &group_handle;
114
115         printf("opening group account\n");
116
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));
120                 return False;
121         }
122
123         r3.in.group_handle  = &group_handle;
124         r3.out.group_handle = &group_handle;
125
126         printf("deleting group account\n");
127         
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));
131                 return False;
132         }
133         
134         return True;
135 }
136
137
138 static BOOL test_create(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
139                         struct policy_handle *handle, const char *name, uint32_t *rid)
140 {
141         NTSTATUS status;
142         struct lsa_String groupname;
143         struct samr_CreateDomainGroup r;
144         struct policy_handle group_handle;
145         
146         groupname.string = name;
147         
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;
152         r.out.rid           = rid;
153
154         printf("creating group account %s\n", name);
155
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));
159
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)) {
163                                 return False;
164                         }
165
166                         printf("creating group account\n");
167                         
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));
171                                 return False;
172                         }
173                         return True;
174                 }
175                 return False;
176         }
177
178         return True;
179 }
180
181
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,
185                            uint32_t *rid)
186 {
187         const uint16_t level = 5;
188         NTSTATUS status;
189         struct libnet_rpc_groupinfo group;
190         struct dom_sid *group_sid;
191         
192         group_sid = dom_sid_add_rid(mem_ctx, domain_sid, *rid);
193         
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 */
197
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));
202                 return False;
203         }
204
205         ZERO_STRUCT(group);
206
207         group.in.domain_handle  = *domain_handle;
208         group.in.sid            = NULL;
209         group.in.groupname      = TEST_GROUPNAME;
210         group.in.level          = level;
211
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));
216                 return False;
217         }
218
219         return True;
220 }
221
222
223 BOOL torture_groupinfo(struct torture_context *torture)
224 {
225         NTSTATUS status;
226         struct dcerpc_pipe *p;
227         TALLOC_CTX *mem_ctx;
228         BOOL ret = True;
229         struct policy_handle h;
230         struct lsa_String name;
231         struct dom_sid2 sid;
232         uint32_t rid;
233
234         mem_ctx = talloc_init("test_userinfo");
235
236         status = torture_rpc_connection(torture, 
237                                         &p,
238                                         &ndr_table_samr);
239         
240         if (!NT_STATUS_IS_OK(status)) {
241                 return False;
242         }
243
244         name.string = lp_workgroup();
245
246         /*
247          * Testing synchronous version
248          */
249         if (!test_opendomain(p, mem_ctx, &h, &name, &sid)) {
250                 ret = False;
251                 goto done;
252         }
253
254         if (!test_create(p, mem_ctx, &h, TEST_GROUPNAME, &rid)) {
255                 ret = False;
256                 goto done;
257         }
258
259         if (!test_groupinfo(p, mem_ctx, &h, &sid, TEST_GROUPNAME, &rid)) {
260                 ret = False;
261                 goto done;
262         }
263
264         if (!test_cleanup(p, mem_ctx, &h, TEST_GROUPNAME)) {
265                 ret = False;
266                 goto done;
267         }
268
269 done:
270         talloc_free(mem_ctx);
271
272         return ret;
273 }