[s3]vfs_acl_tdb: fix the build.
[ira/wip.git] / source4 / torture / libnet / utils.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  * These are more general use functions shared among the tests.
23  */
24
25 #include "includes.h"
26 #include "torture/rpc/rpc.h"
27 #include "libnet/libnet.h"
28 #include "librpc/gen_ndr/ndr_samr_c.h"
29 #include "torture/libnet/utils.h"
30
31
32 bool test_opendomain(struct torture_context *tctx, 
33                      struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
34                      struct policy_handle *handle, struct lsa_String *domname,
35                      struct dom_sid2 *sid)
36 {
37         NTSTATUS status;
38         struct policy_handle h, domain_handle;
39         struct samr_Connect r1;
40         struct samr_LookupDomain r2;
41         struct samr_OpenDomain r3;
42         
43         torture_comment(tctx, "connecting\n");
44         
45         r1.in.system_name = 0;
46         r1.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
47         r1.out.connect_handle = &h;
48         
49         status = dcerpc_samr_Connect(p, mem_ctx, &r1);
50         torture_assert_ntstatus_ok(tctx, status, "Connect failed");
51         
52         r2.in.connect_handle = &h;
53         r2.in.domain_name = domname;
54
55         torture_comment(tctx, "domain lookup on %s\n", domname->string);
56
57         status = dcerpc_samr_LookupDomain(p, mem_ctx, &r2);
58         torture_assert_ntstatus_ok(tctx, status, "LookupDomain failed");
59
60         r3.in.connect_handle = &h;
61         r3.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
62         r3.in.sid = r2.out.sid;
63         r3.out.domain_handle = &domain_handle;
64
65         torture_comment(tctx, "opening domain\n");
66
67         status = dcerpc_samr_OpenDomain(p, mem_ctx, &r3);
68         torture_assert_ntstatus_ok(tctx, status, "OpenDomain failed");
69         *handle = domain_handle;
70
71         *sid = *r2.out.sid;
72         return true;
73 }
74
75
76 bool test_user_cleanup(struct torture_context *tctx, struct dcerpc_pipe *p, 
77                        TALLOC_CTX *mem_ctx, struct policy_handle *domain_handle,
78                        const char *name)
79 {
80         NTSTATUS status;
81         struct samr_LookupNames r1;
82         struct samr_OpenUser r2;
83         struct samr_DeleteUser r3;
84         struct lsa_String names[2];
85         uint32_t rid;
86         struct policy_handle user_handle;
87
88         names[0].string = name;
89
90         r1.in.domain_handle  = domain_handle;
91         r1.in.num_names      = 1;
92         r1.in.names          = names;
93         
94         torture_comment(tctx, "user account lookup '%s'\n", name);
95
96         status = dcerpc_samr_LookupNames(p, mem_ctx, &r1);
97         torture_assert_ntstatus_ok(tctx, status, "LookupNames failed");
98
99         rid = r1.out.rids.ids[0];
100         
101         r2.in.domain_handle  = domain_handle;
102         r2.in.access_mask    = SEC_FLAG_MAXIMUM_ALLOWED;
103         r2.in.rid            = rid;
104         r2.out.user_handle   = &user_handle;
105
106         torture_comment(tctx, "opening user account\n");
107
108         status = dcerpc_samr_OpenUser(p, mem_ctx, &r2);
109         torture_assert_ntstatus_ok(tctx, status, "OpenUser failed");
110
111         r3.in.user_handle  = &user_handle;
112         r3.out.user_handle = &user_handle;
113
114         torture_comment(tctx, "deleting user account\n");
115         
116         status = dcerpc_samr_DeleteUser(p, mem_ctx, &r3);
117         torture_assert_ntstatus_ok(tctx, status, "DeleteUser failed");
118         
119         return true;
120 }
121
122
123 bool test_user_create(struct torture_context *tctx, 
124                       struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
125                       struct policy_handle *handle, const char *name,
126                       uint32_t *rid)
127 {
128         NTSTATUS status;
129         struct lsa_String username;
130         struct samr_CreateUser r;
131         struct policy_handle user_handle;
132         
133         username.string = name;
134         
135         r.in.domain_handle = handle;
136         r.in.account_name  = &username;
137         r.in.access_mask   = SEC_FLAG_MAXIMUM_ALLOWED;
138         r.out.user_handle  = &user_handle;
139         r.out.rid          = rid;
140
141         torture_comment(tctx, "creating user account %s\n", name);
142
143         status = dcerpc_samr_CreateUser(p, mem_ctx, &r);
144         if (!NT_STATUS_IS_OK(status)) {
145                 printf("CreateUser failed - %s\n", nt_errstr(status));
146
147                 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
148                         torture_comment(tctx, "User (%s) already exists - attempting to delete and recreate account again\n", name);
149                         if (!test_user_cleanup(tctx, p, mem_ctx, handle, name)) {
150                                 return false;
151                         }
152
153                         torture_comment(tctx, "creating user account\n");
154                         
155                         status = dcerpc_samr_CreateUser(p, mem_ctx, &r);
156                         torture_assert_ntstatus_ok(tctx, status, "CreateUser failed");
157                         return true;
158                 }
159                 return false;
160         }
161
162         return true;
163 }
164
165
166 bool test_group_cleanup(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
167                         struct policy_handle *domain_handle,
168                         const char *name)
169 {
170         NTSTATUS status;
171         struct samr_LookupNames r1;
172         struct samr_OpenGroup r2;
173         struct samr_DeleteDomainGroup r3;
174         struct lsa_String names[2];
175         uint32_t rid;
176         struct policy_handle group_handle;
177
178         names[0].string = name;
179
180         r1.in.domain_handle  = domain_handle;
181         r1.in.num_names      = 1;
182         r1.in.names          = names;
183         
184         printf("group account lookup '%s'\n", name);
185
186         status = dcerpc_samr_LookupNames(p, mem_ctx, &r1);
187         if (!NT_STATUS_IS_OK(status)) {
188                 printf("LookupNames failed - %s\n", nt_errstr(status));
189                 return false;
190         }
191
192         rid = r1.out.rids.ids[0];
193         
194         r2.in.domain_handle  = domain_handle;
195         r2.in.access_mask    = SEC_FLAG_MAXIMUM_ALLOWED;
196         r2.in.rid            = rid;
197         r2.out.group_handle  = &group_handle;
198
199         printf("opening group account\n");
200
201         status = dcerpc_samr_OpenGroup(p, mem_ctx, &r2);
202         if (!NT_STATUS_IS_OK(status)) {
203                 printf("OpenGroup failed - %s\n", nt_errstr(status));
204                 return false;
205         }
206
207         r3.in.group_handle  = &group_handle;
208         r3.out.group_handle = &group_handle;
209
210         printf("deleting group account\n");
211         
212         status = dcerpc_samr_DeleteDomainGroup(p, mem_ctx, &r3);
213         if (!NT_STATUS_IS_OK(status)) {
214                 printf("DeleteGroup failed - %s\n", nt_errstr(status));
215                 return false;
216         }
217         
218         return true;
219 }
220
221
222 bool test_group_create(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
223                        struct policy_handle *handle, const char *name,
224                        uint32_t *rid)
225 {
226         NTSTATUS status;
227         struct lsa_String groupname;
228         struct samr_CreateDomainGroup r;
229         struct policy_handle group_handle;
230         
231         groupname.string = name;
232         
233         r.in.domain_handle  = handle;
234         r.in.name           = &groupname;
235         r.in.access_mask    = SEC_FLAG_MAXIMUM_ALLOWED;
236         r.out.group_handle  = &group_handle;
237         r.out.rid           = rid;
238
239         printf("creating group account %s\n", name);
240
241         status = dcerpc_samr_CreateDomainGroup(p, mem_ctx, &r);
242         if (!NT_STATUS_IS_OK(status)) {
243                 printf("CreateGroup failed - %s\n", nt_errstr(status));
244
245                 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
246                         printf("Group (%s) already exists - attempting to delete and recreate account again\n", name);
247                         if (!test_group_cleanup(p, mem_ctx, handle, name)) {
248                                 return false;
249                         }
250
251                         printf("creating group account\n");
252                         
253                         status = dcerpc_samr_CreateDomainGroup(p, mem_ctx, &r);
254                         if (!NT_STATUS_IS_OK(status)) {
255                                 printf("CreateGroup failed - %s\n", nt_errstr(status));
256                                 return false;
257                         }
258                         return true;
259                 }
260                 return false;
261         }
262
263         return true;
264 }
265
266
267 void msg_handler(struct monitor_msg *m)
268 {
269         struct msg_rpc_open_user *msg_open;
270         struct msg_rpc_query_user *msg_query;
271         struct msg_rpc_close_user *msg_close;
272         struct msg_rpc_create_user *msg_create;
273
274         switch (m->type) {
275         case mon_SamrOpenUser:
276                 msg_open = (struct msg_rpc_open_user*)m->data;
277                 printf("monitor_msg: user opened (rid=%d, access_mask=0x%08x)\n",
278                        msg_open->rid, msg_open->access_mask);
279                 break;
280         case mon_SamrQueryUser:
281                 msg_query = (struct msg_rpc_query_user*)m->data;
282                 printf("monitor_msg: user queried (level=%d)\n", msg_query->level);
283                 break;
284         case mon_SamrCloseUser:
285                 msg_close = (struct msg_rpc_close_user*)m->data;
286                 printf("monitor_msg: user closed (rid=%d)\n", msg_close->rid);
287                 break;
288         case mon_SamrCreateUser:
289                 msg_create = (struct msg_rpc_create_user*)m->data;
290                 printf("monitor_msg: user created (rid=%d)\n", msg_create->rid);
291                 break;
292         }
293 }