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 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 #include "torture/rpc/torture_rpc.h"
23 #include "torture/libnet/usertest.h"
24 #include "libnet/libnet.h"
25 #include "librpc/gen_ndr/ndr_samr_c.h"
26 #include "param/param.h"
28 #include "torture/libnet/proto.h"
31 static bool test_useradd(struct torture_context
*tctx
,
32 struct dcerpc_pipe
*p
, TALLOC_CTX
*mem_ctx
,
33 struct policy_handle
*domain_handle
,
38 struct libnet_rpc_useradd user
;
40 user
.in
.domain_handle
= *domain_handle
;
41 user
.in
.username
= name
;
43 torture_comment(tctx
, "Testing libnet_rpc_useradd\n");
45 status
= libnet_rpc_useradd(tctx
->ev
, p
->binding_handle
, mem_ctx
, &user
);
46 if (!NT_STATUS_IS_OK(status
)) {
47 torture_comment(tctx
, "Failed to call libnet_rpc_useradd - %s\n", nt_errstr(status
));
55 static bool test_useradd_async(struct torture_context
*tctx
,
56 struct dcerpc_pipe
*p
, TALLOC_CTX
*mem_ctx
,
57 struct policy_handle
*handle
, const char* username
)
60 struct composite_context
*c
;
61 struct libnet_rpc_useradd user
;
63 user
.in
.domain_handle
= *handle
;
64 user
.in
.username
= username
;
66 torture_comment(tctx
, "Testing async libnet_rpc_useradd\n");
68 c
= libnet_rpc_useradd_send(mem_ctx
, tctx
->ev
, p
->binding_handle
,
71 torture_comment(tctx
, "Failed to call async libnet_rpc_useradd\n");
75 status
= libnet_rpc_useradd_recv(c
, mem_ctx
, &user
);
76 if (!NT_STATUS_IS_OK(status
)) {
77 torture_comment(tctx
, "Calling async libnet_rpc_useradd failed - %s\n", nt_errstr(status
));
85 static bool test_usermod(struct torture_context
*tctx
, struct dcerpc_pipe
*p
,
87 struct policy_handle
*handle
, int num_changes
,
88 struct libnet_rpc_usermod
*mod
, char **username
)
90 const char* logon_scripts
[] = { "start_login.cmd", "login.bat", "start.cmd" };
91 const char* home_dirs
[] = { "\\\\srv\\home", "\\\\homesrv\\home\\user", "\\\\pdcsrv\\domain" };
92 const char* home_drives
[] = { "H:", "z:", "I:", "J:", "n:" };
93 const char *homedir
, *homedrive
, *logonscript
;
94 const uint32_t flags
[] = { (ACB_DISABLED
| ACB_NORMAL
| ACB_PW_EXPIRED
),
95 (ACB_NORMAL
| ACB_PWNOEXP
),
96 (ACB_NORMAL
| ACB_PW_EXPIRED
) };
100 enum test_fields testfld
;
104 srandom((unsigned)time(NULL
));
106 mod
->in
.username
= talloc_strdup(mem_ctx
, *username
);
107 mod
->in
.domain_handle
= *handle
;
109 torture_comment(tctx
, "modifying user (%d simultaneous change(s))\n",
112 torture_comment(tctx
, "fields to change: [");
114 for (i
= 0; i
< num_changes
&& i
<= USER_FIELD_LAST
; i
++) {
117 testfld
= (random() % USER_FIELD_LAST
) + 1;
123 continue_if_field_set(mod
->in
.change
.account_name
);
124 mod
->in
.change
.account_name
= talloc_asprintf(mem_ctx
, TEST_CHG_ACCOUNTNAME
,
125 (int)(random() % 100));
126 mod
->in
.change
.fields
|= USERMOD_FIELD_ACCOUNT_NAME
;
127 fldname
= "account_name";
128 *username
= talloc_strdup(mem_ctx
, mod
->in
.change
.account_name
);
132 continue_if_field_set(mod
->in
.change
.full_name
);
133 mod
->in
.change
.full_name
= talloc_asprintf(mem_ctx
, TEST_CHG_FULLNAME
,
134 (int)random(), (int)random());
135 mod
->in
.change
.fields
|= USERMOD_FIELD_FULL_NAME
;
136 fldname
= "full_name";
139 case acct_description
:
140 continue_if_field_set(mod
->in
.change
.description
);
141 mod
->in
.change
.description
= talloc_asprintf(mem_ctx
, TEST_CHG_DESCRIPTION
,
143 mod
->in
.change
.fields
|= USERMOD_FIELD_DESCRIPTION
;
144 fldname
= "description";
147 case acct_home_directory
:
148 continue_if_field_set(mod
->in
.change
.home_directory
);
149 homedir
= home_dirs
[random() % (sizeof(home_dirs
)/sizeof(char*))];
150 mod
->in
.change
.home_directory
= talloc_strdup(mem_ctx
, homedir
);
151 mod
->in
.change
.fields
|= USERMOD_FIELD_HOME_DIRECTORY
;
152 fldname
= "home_directory";
155 case acct_home_drive
:
156 continue_if_field_set(mod
->in
.change
.home_drive
);
157 homedrive
= home_drives
[random() % (sizeof(home_drives
)/sizeof(char*))];
158 mod
->in
.change
.home_drive
= talloc_strdup(mem_ctx
, homedrive
);
159 mod
->in
.change
.fields
|= USERMOD_FIELD_HOME_DRIVE
;
160 fldname
= "home_drive";
164 continue_if_field_set(mod
->in
.change
.comment
);
165 mod
->in
.change
.comment
= talloc_asprintf(mem_ctx
, TEST_CHG_COMMENT
,
167 mod
->in
.change
.fields
|= USERMOD_FIELD_COMMENT
;
171 case acct_logon_script
:
172 continue_if_field_set(mod
->in
.change
.logon_script
);
173 logonscript
= logon_scripts
[random() % (sizeof(logon_scripts
)/sizeof(char*))];
174 mod
->in
.change
.logon_script
= talloc_strdup(mem_ctx
, logonscript
);
175 mod
->in
.change
.fields
|= USERMOD_FIELD_LOGON_SCRIPT
;
176 fldname
= "logon_script";
179 case acct_profile_path
:
180 continue_if_field_set(mod
->in
.change
.profile_path
);
181 mod
->in
.change
.profile_path
= talloc_asprintf(mem_ctx
, TEST_CHG_PROFILEPATH
,
182 (long int)random(), (unsigned int)random());
183 mod
->in
.change
.fields
|= USERMOD_FIELD_PROFILE_PATH
;
184 fldname
= "profile_path";
188 continue_if_field_set(mod
->in
.change
.acct_expiry
);
189 now
= timeval_add(&now
, (random() % (31*24*60*60)), 0);
190 mod
->in
.change
.acct_expiry
= (struct timeval
*)talloc_memdup(mem_ctx
, &now
, sizeof(now
));
191 mod
->in
.change
.fields
|= USERMOD_FIELD_ACCT_EXPIRY
;
192 fldname
= "acct_expiry";
196 continue_if_field_set(mod
->in
.change
.acct_flags
);
197 mod
->in
.change
.acct_flags
= flags
[random() % ARRAY_SIZE(flags
)];
198 mod
->in
.change
.fields
|= USERMOD_FIELD_ACCT_FLAGS
;
199 fldname
= "acct_flags";
203 fldname
= talloc_asprintf(mem_ctx
, "unknown_field (%d)", testfld
);
207 torture_comment(tctx
, ((i
< num_changes
- 1) ? "%s," : "%s"), fldname
);
209 torture_comment(tctx
, "]\n");
211 status
= libnet_rpc_usermod(tctx
->ev
, p
->binding_handle
, mem_ctx
, mod
);
212 torture_assert_ntstatus_ok(tctx
, status
, "Failed to call sync libnet_rpc_usermod");
218 static bool test_userdel(struct torture_context
*tctx
,
219 struct dcerpc_pipe
*p
, TALLOC_CTX
*mem_ctx
,
220 struct policy_handle
*handle
, const char *username
)
223 struct libnet_rpc_userdel user
;
227 user
.in
.domain_handle
= *handle
;
228 user
.in
.username
= username
;
230 status
= libnet_rpc_userdel(tctx
->ev
, p
->binding_handle
, mem_ctx
, &user
);
231 if (!NT_STATUS_IS_OK(status
)) {
232 torture_comment(tctx
, "Failed to call sync libnet_rpc_userdel - %s\n", nt_errstr(status
));
240 #define CMP_LSA_STRING_FLD(fld, flags) \
241 if ((mod->in.change.fields & flags) && \
242 !strequal(i->fld.string, mod->in.change.fld)) { \
243 torture_comment(tctx, "'%s' field does not match\n", #fld); \
244 torture_comment(tctx, "received: '%s'\n", i->fld.string); \
245 torture_comment(tctx, "expected: '%s'\n", mod->in.change.fld); \
250 #define CMP_TIME_FLD(fld, flags) \
251 if (mod->in.change.fields & flags) { \
252 nttime_to_timeval(&t, i->fld); \
253 if (timeval_compare(&t, mod->in.change.fld)) { \
254 torture_comment(tctx, "'%s' field does not match\n", #fld); \
255 torture_comment(tctx, "received: '%s (+%ld us)'\n", \
256 timestring(mem_ctx, t.tv_sec), (long)t.tv_usec); \
257 torture_comment(tctx, "expected: '%s (+%ld us)'\n", \
258 timestring(mem_ctx, mod->in.change.fld->tv_sec), \
259 (long)mod->in.change.fld->tv_usec); \
264 #define CMP_NUM_FLD(fld, flags) \
265 if ((mod->in.change.fields & flags) && \
266 (i->fld != mod->in.change.fld)) { \
267 torture_comment(tctx, "'%s' field does not match\n", #fld); \
268 torture_comment(tctx, "received: '%04x'\n", i->fld); \
269 torture_comment(tctx, "expected: '%04x'\n", mod->in.change.fld); \
274 static bool test_compare(struct torture_context
*tctx
,
275 struct dcerpc_pipe
*p
, TALLOC_CTX
*mem_ctx
,
276 struct policy_handle
*handle
, struct libnet_rpc_usermod
*mod
,
277 const char *username
)
280 struct libnet_rpc_userinfo info
;
281 struct samr_UserInfo21
*i
;
286 info
.in
.username
= username
;
287 info
.in
.domain_handle
= *handle
;
288 info
.in
.level
= 21; /* the most rich infolevel available */
290 status
= libnet_rpc_userinfo(tctx
->ev
, p
->binding_handle
, mem_ctx
, &info
);
291 torture_assert_ntstatus_ok(tctx
, status
, "Failed to call sync libnet_rpc_userinfo");
293 i
= &info
.out
.info
.info21
;
295 CMP_LSA_STRING_FLD(account_name
, USERMOD_FIELD_ACCOUNT_NAME
);
296 CMP_LSA_STRING_FLD(full_name
, USERMOD_FIELD_FULL_NAME
);
297 CMP_LSA_STRING_FLD(description
, USERMOD_FIELD_DESCRIPTION
);
298 CMP_LSA_STRING_FLD(comment
, USERMOD_FIELD_COMMENT
);
299 CMP_LSA_STRING_FLD(logon_script
, USERMOD_FIELD_LOGON_SCRIPT
);
300 CMP_LSA_STRING_FLD(profile_path
, USERMOD_FIELD_PROFILE_PATH
);
301 CMP_LSA_STRING_FLD(home_directory
, USERMOD_FIELD_HOME_DIRECTORY
);
302 CMP_LSA_STRING_FLD(home_drive
, USERMOD_FIELD_HOME_DRIVE
);
303 CMP_TIME_FLD(acct_expiry
, USERMOD_FIELD_ACCT_EXPIRY
);
304 CMP_NUM_FLD(acct_flags
, USERMOD_FIELD_ACCT_FLAGS
)
310 bool torture_useradd(struct torture_context
*torture
)
313 struct dcerpc_pipe
*p
;
314 struct policy_handle h
;
315 struct lsa_String domain_name
;
317 const char *name
= TEST_USERNAME
;
320 struct dcerpc_binding_handle
*b
;
322 mem_ctx
= talloc_init("test_useradd");
324 status
= torture_rpc_connection(torture
,
328 torture_assert_ntstatus_ok(torture
, status
, "RPC connect failed");
329 b
= p
->binding_handle
;
331 domain_name
.string
= lpcfg_workgroup(torture
->lp_ctx
);
332 if (!test_domain_open(torture
, b
, &domain_name
, mem_ctx
, &h
, &sid
)) {
337 if (!test_useradd(torture
, p
, mem_ctx
, &h
, name
)) {
342 if (!test_user_cleanup(torture
, b
, mem_ctx
, &h
, name
)) {
347 if (!test_domain_open(torture
, b
, &domain_name
, mem_ctx
, &h
, &sid
)) {
352 if (!test_useradd_async(torture
, p
, mem_ctx
, &h
, name
)) {
357 if (!test_user_cleanup(torture
, b
, mem_ctx
, &h
, name
)) {
363 talloc_free(mem_ctx
);
368 bool torture_userdel(struct torture_context
*torture
)
371 struct dcerpc_pipe
*p
;
372 struct policy_handle h
;
373 struct lsa_String domain_name
;
376 const char *name
= TEST_USERNAME
;
379 struct dcerpc_binding_handle
*b
;
381 mem_ctx
= talloc_init("test_userdel");
383 status
= torture_rpc_connection(torture
,
387 if (!NT_STATUS_IS_OK(status
)) {
390 b
= p
->binding_handle
;
392 domain_name
.string
= lpcfg_workgroup(torture
->lp_ctx
);
393 if (!test_domain_open(torture
, b
, &domain_name
, mem_ctx
, &h
, &sid
)) {
398 if (!test_user_create(torture
, b
, mem_ctx
, &h
, name
, &rid
)) {
403 if (!test_userdel(torture
, p
, mem_ctx
, &h
, name
)) {
409 talloc_free(mem_ctx
);
414 bool torture_usermod(struct torture_context
*torture
)
417 struct dcerpc_pipe
*p
;
418 struct policy_handle h
;
419 struct lsa_String domain_name
;
426 struct dcerpc_binding_handle
*b
;
428 mem_ctx
= talloc_init("test_userdel");
430 status
= torture_rpc_connection(torture
,
434 torture_assert_ntstatus_ok(torture
, status
, "RPC connect");
435 b
= p
->binding_handle
;
437 domain_name
.string
= lpcfg_workgroup(torture
->lp_ctx
);
438 name
= talloc_strdup(mem_ctx
, TEST_USERNAME
);
440 if (!test_domain_open(torture
, b
, &domain_name
, mem_ctx
, &h
, &sid
)) {
445 if (!test_user_create(torture
, b
, mem_ctx
, &h
, name
, &rid
)) {
450 for (i
= USER_FIELD_FIRST
; i
<= USER_FIELD_LAST
; i
++) {
451 struct libnet_rpc_usermod m
;
453 if (!test_usermod(torture
, p
, mem_ctx
, &h
, i
, &m
, &name
)) {
458 if (!test_compare(torture
, p
, mem_ctx
, &h
, &m
, name
)) {
465 if (!test_user_cleanup(torture
, b
, mem_ctx
, &h
, TEST_USERNAME
)) {
471 talloc_free(mem_ctx
);