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