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"
26 #define TEST_USERNAME "libnetusermantest"
29 static BOOL test_opendomain(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
30 struct policy_handle *handle, struct samr_String *domname)
33 struct policy_handle h, domain_handle;
34 struct samr_Connect r1;
35 struct samr_LookupDomain r2;
36 struct samr_OpenDomain r3;
38 printf("connecting\n");
40 r1.in.system_name = 0;
41 r1.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
42 r1.out.connect_handle = &h;
44 status = dcerpc_samr_Connect(p, mem_ctx, &r1);
45 if (!NT_STATUS_IS_OK(status)) {
46 printf("Connect failed - %s\n", nt_errstr(status));
50 r2.in.connect_handle = &h;
51 r2.in.domain_name = domname;
53 printf("domain lookup on %s\n", domname->string);
55 status = dcerpc_samr_LookupDomain(p, mem_ctx, &r2);
56 if (!NT_STATUS_IS_OK(status)) {
57 printf("LookupDomain failed - %s\n", nt_errstr(status));
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;
66 printf("opening domain\n");
68 status = dcerpc_samr_OpenDomain(p, mem_ctx, &r3);
69 if (!NT_STATUS_IS_OK(status)) {
70 printf("OpenDomain failed - %s\n", nt_errstr(status));
73 *handle = domain_handle;
80 static BOOL test_useradd(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
81 struct policy_handle *domain_handle,
86 struct rpc_composite_useradd user;
88 user.in.domain_handle = *domain_handle;
89 user.in.username = name;
91 status = rpc_composite_useradd(p, mem_ctx, &user);
92 if (!NT_STATUS_IS_OK(status)) {
93 printf("Failed to call sync rpc_composite_userinfo - %s\n", nt_errstr(status));
101 static BOOL test_cleanup(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
102 struct policy_handle *domain_handle, const char *username)
105 struct samr_LookupNames r1;
106 struct samr_OpenUser r2;
107 struct samr_DeleteUser r3;
108 struct samr_String names[2];
110 struct policy_handle user_handle;
112 names[0].string = username;
114 r1.in.domain_handle = domain_handle;
118 printf("user account lookup '%s'\n", username);
120 status = dcerpc_samr_LookupNames(p, mem_ctx, &r1);
121 if (!NT_STATUS_IS_OK(status)) {
122 printf("LookupNames failed - %s\n", nt_errstr(status));
126 rid = r1.out.rids.ids[0];
128 r2.in.domain_handle = domain_handle;
129 r2.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
131 r2.out.user_handle = &user_handle;
133 printf("opening user account\n");
135 status = dcerpc_samr_OpenUser(p, mem_ctx, &r2);
136 if (!NT_STATUS_IS_OK(status)) {
137 printf("OpenUser failed - %s\n", nt_errstr(status));
141 r3.in.user_handle = &user_handle;
142 r3.out.user_handle = &user_handle;
144 printf("deleting user account\n");
146 status = dcerpc_samr_DeleteUser(p, mem_ctx, &r3);
147 if (!NT_STATUS_IS_OK(status)) {
148 printf("DeleteUser failed - %s\n", nt_errstr(status));
156 static BOOL test_createuser(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
157 struct policy_handle *handle, const char* user)
160 struct policy_handle user_handle;
161 struct samr_String username;
162 struct samr_CreateUser r1;
163 struct samr_Close r2;
166 username.string = user;
168 r1.in.domain_handle = handle;
169 r1.in.account_name = &username;
170 r1.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
171 r1.out.user_handle = &user_handle;
172 r1.out.rid = &user_rid;
174 printf("creating user '%s'\n", username.string);
176 status = dcerpc_samr_CreateUser(p, mem_ctx, &r1);
177 if (!NT_STATUS_IS_OK(status)) {
178 printf("CreateUser failed - %s\n", nt_errstr(status));
180 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
181 printf("User (%s) already exists - attempting to delete and recreate account again\n", user);
182 if (!test_cleanup(p, mem_ctx, handle, TEST_USERNAME)) {
186 printf("creating user account\n");
188 status = dcerpc_samr_CreateUser(p, mem_ctx, &r1);
189 if (!NT_STATUS_IS_OK(status)) {
190 printf("CreateUser failed - %s\n", nt_errstr(status));
198 r2.in.handle = &user_handle;
199 r2.out.handle = &user_handle;
201 printf("closing user '%s'\n", username.string);
203 status = dcerpc_samr_Close(p, mem_ctx, &r2);
204 if (!NT_STATUS_IS_OK(status)) {
205 printf("Close failed - %s\n", nt_errstr(status));
213 static BOOL test_userdel(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
214 struct policy_handle *handle, const char *username)
217 struct rpc_composite_userdel user;
219 user.in.domain_handle = *handle;
220 user.in.username = username;
222 status = rpc_composite_userdel(p, mem_ctx, &user);
223 if (!NT_STATUS_IS_OK(status)) {
224 printf("Failed to call sync rpc_composite_userdel - %s\n", nt_errstr(status));
232 BOOL torture_useradd(void)
236 struct dcerpc_pipe *p;
237 struct policy_handle h;
238 struct samr_String domain_name;
239 char* name = TEST_USERNAME;
243 mem_ctx = talloc_init("test_useradd");
244 binding = lp_parm_string(-1, "torture", "binding");
246 status = torture_rpc_connection(mem_ctx,
250 DCERPC_SAMR_VERSION);
252 if (!NT_STATUS_IS_OK(status)) {
256 domain_name.string = lp_workgroup();
257 if (!test_opendomain(p, mem_ctx, &h, &domain_name)) {
262 if (!test_useradd(p, mem_ctx, &h, name)) {
267 if (!test_cleanup(p, mem_ctx, &h, name)) {
273 talloc_free(mem_ctx);
278 BOOL torture_userdel(void)
282 struct dcerpc_pipe *p;
283 struct policy_handle h;
284 struct samr_String domain_name;
285 char* name = TEST_USERNAME;
289 mem_ctx = talloc_init("test_userdel");
290 binding = lp_parm_string(-1, "torture", "binding");
292 status = torture_rpc_connection(mem_ctx,
296 DCERPC_SAMR_VERSION);
298 if (!NT_STATUS_IS_OK(status)) {
302 domain_name.string = lp_workgroup();
303 if (!test_opendomain(p, mem_ctx, &h, &domain_name)) {
308 if (!test_createuser(p, mem_ctx, &h, name)) {
313 if (!test_userdel(p, mem_ctx, &h, name)) {
319 talloc_free(mem_ctx);