2 Unix SMB/CIFS implementation.
3 Test suite for libnet calls.
5 Copyright (C) Rafal Szczesniak 2005
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 2 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, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include "librpc/gen_ndr/ndr_samr.h"
24 #include "libnet/composite.h"
25 #include "libcli/composite/monitor.h"
27 #define TEST_USERNAME "libnetuserinfotest"
29 static BOOL test_opendomain(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
30 struct policy_handle *handle, struct samr_String *domname,
34 struct policy_handle h, domain_handle;
35 struct samr_Connect r1;
36 struct samr_LookupDomain r2;
37 struct samr_OpenDomain r3;
39 printf("connecting\n");
41 r1.in.system_name = 0;
42 r1.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
43 r1.out.connect_handle = &h;
45 status = dcerpc_samr_Connect(p, mem_ctx, &r1);
46 if (!NT_STATUS_IS_OK(status)) {
47 printf("Connect failed - %s\n", nt_errstr(status));
51 r2.in.connect_handle = &h;
52 r2.in.domain_name = domname;
54 printf("domain lookup on %s\n", domname->string);
56 status = dcerpc_samr_LookupDomain(p, mem_ctx, &r2);
57 if (!NT_STATUS_IS_OK(status)) {
58 printf("LookupDomain failed - %s\n", nt_errstr(status));
62 r3.in.connect_handle = &h;
63 r3.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
64 r3.in.sid = r2.out.sid;
65 r3.out.domain_handle = &domain_handle;
67 printf("opening domain\n");
69 status = dcerpc_samr_OpenDomain(p, mem_ctx, &r3);
70 if (!NT_STATUS_IS_OK(status)) {
71 printf("OpenDomain failed - %s\n", nt_errstr(status));
74 *handle = domain_handle;
82 static BOOL test_cleanup(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
83 struct policy_handle *domain_handle, const char *username)
86 struct samr_LookupNames r1;
87 struct samr_OpenUser r2;
88 struct samr_DeleteUser r3;
89 struct samr_String names[2];
91 struct policy_handle user_handle;
93 names[0].string = username;
95 r1.in.domain_handle = domain_handle;
99 printf("user account lookup '%s'\n", username);
101 status = dcerpc_samr_LookupNames(p, mem_ctx, &r1);
102 if (!NT_STATUS_IS_OK(status)) {
103 printf("LookupNames failed - %s\n", nt_errstr(status));
107 rid = r1.out.rids.ids[0];
109 r2.in.domain_handle = domain_handle;
110 r2.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
112 r2.out.user_handle = &user_handle;
114 printf("opening user account\n");
116 status = dcerpc_samr_OpenUser(p, mem_ctx, &r2);
117 if (!NT_STATUS_IS_OK(status)) {
118 printf("OpenUser failed - %s\n", nt_errstr(status));
122 r3.in.user_handle = &user_handle;
123 r3.out.user_handle = &user_handle;
125 printf("deleting user account\n");
127 status = dcerpc_samr_DeleteUser(p, mem_ctx, &r3);
128 if (!NT_STATUS_IS_OK(status)) {
129 printf("DeleteUser failed - %s\n", nt_errstr(status));
137 static BOOL test_create(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
138 struct policy_handle *handle, const char *name, uint32_t *rid)
141 struct samr_String username;
142 struct samr_CreateUser r;
143 struct policy_handle user_handle;
145 username.string = name;
147 r.in.domain_handle = handle;
148 r.in.account_name = &username;
149 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
150 r.out.user_handle = &user_handle;
153 printf("creating user account %s\n", name);
155 status = dcerpc_samr_CreateUser(p, mem_ctx, &r);
156 if (!NT_STATUS_IS_OK(status)) {
157 printf("CreateUser failed - %s\n", nt_errstr(status));
159 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
160 printf("User (%s) already exists - attempting to delete and recreate account again\n", name);
161 if (!test_cleanup(p, mem_ctx, handle, TEST_USERNAME)) {
165 printf("creating user account\n");
167 status = dcerpc_samr_CreateUser(p, mem_ctx, &r);
168 if (!NT_STATUS_IS_OK(status)) {
169 printf("CreateUser failed - %s\n", nt_errstr(status));
181 static BOOL test_userinfo(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
182 struct policy_handle *domain_handle,
183 struct dom_sid2 *domain_sid, const char* user_name,
187 struct rpc_composite_userinfo user;
188 struct dom_sid *user_sid;
190 user_sid = dom_sid_add_rid(mem_ctx, domain_sid, *rid);
192 user.in.domain_handle = *domain_handle;
193 user.in.sid = dom_sid_string(mem_ctx, user_sid);
194 user.in.level = 5; /* this should be extended */
196 printf("Testing sync rpc_composite_userinfo\n");
197 status = rpc_composite_userinfo(p, mem_ctx, &user);
198 if (!NT_STATUS_IS_OK(status)) {
199 printf("Failed to call sync rpc_composite_userinfo - %s\n", nt_errstr(status));
207 static void msg_handler(struct monitor_msg *m)
211 printf("monitor_msg: user opened (rid=%d, access_mask=0x%08x)\n",
212 m->data.rpc_open_user.rid, m->data.rpc_open_user.access_mask);
215 printf("monitor_msg: user queried (level=%d)\n", m->data.rpc_query_user.level);
218 printf("monitor_msg: user closed (rid=%d)\n", m->data.rpc_close_user.rid);
224 static BOOL test_userinfo_async(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
225 struct policy_handle *domain_handle,
226 struct dom_sid2 *domain_sid, const char* user_name,
230 struct composite_context *c;
231 struct rpc_composite_userinfo user;
232 struct dom_sid *user_sid;
234 user_sid = dom_sid_add_rid(mem_ctx, domain_sid, *rid);
236 user.in.domain_handle = *domain_handle;
237 user.in.sid = dom_sid_string(mem_ctx, user_sid);
238 user.in.level = 10; /* this should be extended */
240 printf("Testing async rpc_composite_userinfo\n");
242 c = rpc_composite_userinfo_send(p, &user, msg_handler);
244 printf("Failed to call sync rpc_composite_userinfo_send\n");
248 status = rpc_composite_userinfo_recv(c, mem_ctx, &user);
249 if (!NT_STATUS_IS_OK(status)) {
250 printf("Calling async rpc_composite_userinfo failed - %s\n", nt_errstr(status));
258 BOOL torture_userinfo(void)
262 struct dcerpc_pipe *p;
263 struct dcerpc_binding *b;
266 struct policy_handle h;
267 struct samr_String name;
271 mem_ctx = talloc_init("test_userinfo");
272 binding = lp_parm_string(-1, "torture", "binding");
274 status = torture_rpc_connection(mem_ctx,
278 DCERPC_SAMR_VERSION);
280 if (!NT_STATUS_IS_OK(status)) {
284 name.string = lp_workgroup();
287 * Testing synchronous version
289 if (!test_opendomain(p, mem_ctx, &h, &name, &sid)) {
294 if (!test_create(p, mem_ctx, &h, TEST_USERNAME, &rid)) {
299 if (!test_userinfo(p, mem_ctx, &h, &sid, TEST_USERNAME, &rid)) {
304 if (!test_cleanup(p, mem_ctx, &h, TEST_USERNAME)) {
310 * Testing asynchronous version and monitor messages
312 if (!test_opendomain(p, mem_ctx, &h, &name, &sid)) {
317 if (!test_create(p, mem_ctx, &h, TEST_USERNAME, &rid)) {
322 if (!test_userinfo_async(p, mem_ctx, &h, &sid, TEST_USERNAME, &rid)) {
327 if (!test_cleanup(p, mem_ctx, &h, TEST_USERNAME)) {
333 talloc_free(mem_ctx);