s4/test: remove local impl for open_domain and use common one
[Samba.git] / source4 / torture / libnet / utils.c
blob02baf07046e0dd73c1aae5ba8eba1e228f839b87
1 /*
2 Unix SMB/CIFS implementation.
3 Test suite for libnet calls.
5 Copyright (C) Rafal Szczesniak 2007
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.
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.
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/>.
22 * These are more general use functions shared among the tests.
25 #include "includes.h"
26 #include "torture/rpc/torture_rpc.h"
27 #include "libnet/libnet.h"
28 #include "librpc/gen_ndr/ndr_samr_c.h"
29 #include "torture/libnet/proto.h"
31 /**
32 * Opens handle on Domain using SAMR
34 * @param _domain_handle [out] Ptr to storage to store Domain handle
35 * @param _dom_sid [out] If NULL, Domain SID won't be returned
36 * @return
38 bool test_domain_open(struct torture_context *tctx,
39 struct dcerpc_binding_handle *b,
40 struct lsa_String *domname,
41 TALLOC_CTX *mem_ctx,
42 struct policy_handle *_domain_handle,
43 struct dom_sid2 *_dom_sid)
45 struct policy_handle connect_handle;
46 struct policy_handle domain_handle;
47 struct samr_Connect r1;
48 struct samr_LookupDomain r2;
49 struct dom_sid2 *sid = NULL;
50 struct samr_OpenDomain r3;
52 torture_comment(tctx, "connecting\n");
54 r1.in.system_name = 0;
55 r1.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
56 r1.out.connect_handle = &connect_handle;
58 torture_assert_ntstatus_ok(tctx,
59 dcerpc_samr_Connect_r(b, mem_ctx, &r1),
60 "Connect failed");
61 torture_assert_ntstatus_ok(tctx, r1.out.result,
62 "Connect failed");
64 r2.in.connect_handle = &connect_handle;
65 r2.in.domain_name = domname;
66 r2.out.sid = &sid;
68 torture_comment(tctx, "domain lookup on %s\n", domname->string);
70 torture_assert_ntstatus_ok(tctx,
71 dcerpc_samr_LookupDomain_r(b, mem_ctx, &r2),
72 "LookupDomain failed");
73 torture_assert_ntstatus_ok(tctx, r2.out.result,
74 "LookupDomain failed");
76 r3.in.connect_handle = &connect_handle;
77 r3.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
78 r3.in.sid = *r2.out.sid;
79 r3.out.domain_handle = &domain_handle;
81 torture_comment(tctx, "opening domain %s\n", domname->string);
83 torture_assert_ntstatus_ok(tctx,
84 dcerpc_samr_OpenDomain_r(b, mem_ctx, &r3),
85 "OpenDomain failed");
86 torture_assert_ntstatus_ok(tctx, r3.out.result,
87 "OpenDomain failed");
89 *_domain_handle = domain_handle;
91 if (_dom_sid) {
92 *_dom_sid = **r2.out.sid;
95 /* Close connect_handle, we don't need it anymore */
96 test_samr_close_handle(tctx, b, mem_ctx, &connect_handle);
98 return true;
102 bool test_user_cleanup(struct torture_context *tctx,
103 struct dcerpc_binding_handle *b,
104 TALLOC_CTX *mem_ctx, struct policy_handle *domain_handle,
105 const char *name)
107 struct samr_LookupNames r1;
108 struct samr_OpenUser r2;
109 struct samr_DeleteUser r3;
110 struct lsa_String names[2];
111 uint32_t rid;
112 struct policy_handle user_handle;
113 struct samr_Ids rids, types;
115 names[0].string = name;
117 r1.in.domain_handle = domain_handle;
118 r1.in.num_names = 1;
119 r1.in.names = names;
120 r1.out.rids = &rids;
121 r1.out.types = &types;
123 torture_comment(tctx, "user account lookup '%s'\n", name);
125 torture_assert_ntstatus_ok(tctx,
126 dcerpc_samr_LookupNames_r(b, mem_ctx, &r1),
127 "LookupNames failed");
128 torture_assert_ntstatus_ok(tctx, r1.out.result,
129 "LookupNames failed");
131 rid = r1.out.rids->ids[0];
133 r2.in.domain_handle = domain_handle;
134 r2.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
135 r2.in.rid = rid;
136 r2.out.user_handle = &user_handle;
138 torture_comment(tctx, "opening user account\n");
140 torture_assert_ntstatus_ok(tctx,
141 dcerpc_samr_OpenUser_r(b, mem_ctx, &r2),
142 "OpenUser failed");
143 torture_assert_ntstatus_ok(tctx, r2.out.result,
144 "OpenUser failed");
146 r3.in.user_handle = &user_handle;
147 r3.out.user_handle = &user_handle;
149 torture_comment(tctx, "deleting user account\n");
151 torture_assert_ntstatus_ok(tctx,
152 dcerpc_samr_DeleteUser_r(b, mem_ctx, &r3),
153 "DeleteUser failed");
154 torture_assert_ntstatus_ok(tctx, r3.out.result,
155 "DeleteUser failed");
157 return true;
161 bool test_user_create(struct torture_context *tctx,
162 struct dcerpc_binding_handle *b,
163 TALLOC_CTX *mem_ctx,
164 struct policy_handle *handle, const char *name,
165 uint32_t *rid)
167 struct lsa_String username;
168 struct samr_CreateUser r;
169 struct policy_handle user_handle;
171 username.string = name;
173 r.in.domain_handle = handle;
174 r.in.account_name = &username;
175 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
176 r.out.user_handle = &user_handle;
177 r.out.rid = rid;
179 torture_comment(tctx, "creating user account %s\n", name);
181 torture_assert_ntstatus_ok(tctx,
182 dcerpc_samr_CreateUser_r(b, mem_ctx, &r),
183 "CreateUser failed");
184 if (!NT_STATUS_IS_OK(r.out.result)) {
185 printf("CreateUser failed - %s\n", nt_errstr(r.out.result));
186 if (NT_STATUS_EQUAL(r.out.result, NT_STATUS_USER_EXISTS)) {
187 torture_comment(tctx, "User (%s) already exists - attempting to delete and recreate account again\n", name);
188 if (!test_user_cleanup(tctx, b, mem_ctx, handle, name)) {
189 return false;
192 torture_comment(tctx, "creating user account\n");
194 torture_assert_ntstatus_ok(tctx,
195 dcerpc_samr_CreateUser_r(b, mem_ctx, &r),
196 "CreateUser failed");
197 torture_assert_ntstatus_ok(tctx, r.out.result,
198 "CreateUser failed");
200 return true;
202 return false;
205 return true;
209 bool test_group_cleanup(struct torture_context *tctx,
210 struct dcerpc_binding_handle *b, TALLOC_CTX *mem_ctx,
211 struct policy_handle *domain_handle,
212 const char *name)
214 struct samr_LookupNames r1;
215 struct samr_OpenGroup r2;
216 struct samr_DeleteDomainGroup r3;
217 struct lsa_String names[2];
218 uint32_t rid;
219 struct policy_handle group_handle;
220 struct samr_Ids rids, types;
222 names[0].string = name;
224 r1.in.domain_handle = domain_handle;
225 r1.in.num_names = 1;
226 r1.in.names = names;
227 r1.out.rids = &rids;
228 r1.out.types = &types;
230 torture_comment(tctx, "group account lookup '%s'\n", name);
232 torture_assert_ntstatus_ok(tctx,
233 dcerpc_samr_LookupNames_r(b, mem_ctx, &r1),
234 "LookupNames failed");
235 torture_assert_ntstatus_ok(tctx, r1.out.result,
236 "LookupNames failed");
238 rid = r1.out.rids->ids[0];
240 r2.in.domain_handle = domain_handle;
241 r2.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
242 r2.in.rid = rid;
243 r2.out.group_handle = &group_handle;
245 torture_comment(tctx, "opening group account\n");
247 torture_assert_ntstatus_ok(tctx,
248 dcerpc_samr_OpenGroup_r(b, mem_ctx, &r2),
249 "OpenGroup failed");
250 torture_assert_ntstatus_ok(tctx, r2.out.result,
251 "OpenGroup failed");
253 r3.in.group_handle = &group_handle;
254 r3.out.group_handle = &group_handle;
256 torture_comment(tctx, "deleting group account\n");
258 torture_assert_ntstatus_ok(tctx,
259 dcerpc_samr_DeleteDomainGroup_r(b, mem_ctx, &r3),
260 "DeleteGroup failed");
261 torture_assert_ntstatus_ok(tctx, r3.out.result,
262 "DeleteGroup failed");
264 return true;
268 bool test_group_create(struct torture_context *tctx,
269 struct dcerpc_binding_handle *b, TALLOC_CTX *mem_ctx,
270 struct policy_handle *handle, const char *name,
271 uint32_t *rid)
273 struct lsa_String groupname;
274 struct samr_CreateDomainGroup r;
275 struct policy_handle group_handle;
277 groupname.string = name;
279 r.in.domain_handle = handle;
280 r.in.name = &groupname;
281 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
282 r.out.group_handle = &group_handle;
283 r.out.rid = rid;
285 torture_comment(tctx, "creating group account %s\n", name);
287 torture_assert_ntstatus_ok(tctx,
288 dcerpc_samr_CreateDomainGroup_r(b, mem_ctx, &r),
289 "CreateGroup failed");
290 if (!NT_STATUS_IS_OK(r.out.result)) {
291 torture_comment(tctx, "CreateGroup failed - %s\n", nt_errstr(r.out.result));
293 if (NT_STATUS_EQUAL(r.out.result, NT_STATUS_USER_EXISTS)) {
294 torture_comment(tctx, "Group (%s) already exists - attempting to delete and recreate account again\n", name);
295 if (!test_group_cleanup(tctx, b, mem_ctx, handle, name)) {
296 return false;
299 torture_comment(tctx, "creating group account\n");
301 torture_assert_ntstatus_ok(tctx,
302 dcerpc_samr_CreateDomainGroup_r(b, mem_ctx, &r),
303 "CreateGroup failed");
304 torture_assert_ntstatus_ok(tctx, r.out.result,
305 "CreateGroup failed");
307 return true;
309 return false;
312 return true;
316 * Closes SAMR handle obtained from Connect, Open User/Domain, etc
318 bool test_samr_close_handle(struct torture_context *tctx,
319 struct dcerpc_binding_handle *b,
320 TALLOC_CTX *mem_ctx,
321 struct policy_handle *samr_handle)
323 struct samr_Close r;
325 r.in.handle = samr_handle;
326 r.out.handle = samr_handle;
328 torture_assert_ntstatus_ok(tctx,
329 dcerpc_samr_Close_r(b, mem_ctx, &r),
330 "Close SAMR handle RPC call failed");
331 torture_assert_ntstatus_ok(tctx, r.out.result,
332 "Close SAMR handle failed");
334 return true;
338 void msg_handler(struct monitor_msg *m)
340 struct msg_rpc_open_user *msg_open;
341 struct msg_rpc_query_user *msg_query;
342 struct msg_rpc_close_user *msg_close;
343 struct msg_rpc_create_user *msg_create;
345 switch (m->type) {
346 case mon_SamrOpenUser:
347 msg_open = (struct msg_rpc_open_user*)m->data;
348 printf("monitor_msg: user opened (rid=%d, access_mask=0x%08x)\n",
349 msg_open->rid, msg_open->access_mask);
350 break;
351 case mon_SamrQueryUser:
352 msg_query = (struct msg_rpc_query_user*)m->data;
353 printf("monitor_msg: user queried (level=%d)\n", msg_query->level);
354 break;
355 case mon_SamrCloseUser:
356 msg_close = (struct msg_rpc_close_user*)m->data;
357 printf("monitor_msg: user closed (rid=%d)\n", msg_close->rid);
358 break;
359 case mon_SamrCreateUser:
360 msg_create = (struct msg_rpc_create_user*)m->data;
361 printf("monitor_msg: user created (rid=%d)\n", msg_create->rid);
362 break;