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 "torture/rpc/rpc.h"
24 #include "torture/libnet/usertest.h"
25 #include "libnet/libnet.h"
26 #include "librpc/gen_ndr/ndr_samr_c.h"
29 static BOOL
test_opendomain(struct dcerpc_pipe
*p
, TALLOC_CTX
*mem_ctx
,
30 struct policy_handle
*handle
, struct lsa_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 libnet_rpc_useradd user
;
88 user
.in
.domain_handle
= *domain_handle
;
89 user
.in
.username
= name
;
91 printf("Testing libnet_rpc_useradd\n");
93 status
= libnet_rpc_useradd(p
, mem_ctx
, &user
);
94 if (!NT_STATUS_IS_OK(status
)) {
95 printf("Failed to call sync rpc_composite_userinfo - %s\n", nt_errstr(status
));
103 static void msg_handler(struct monitor_msg
*m
)
105 struct msg_rpc_create_user
*msg_create
;
108 case rpc_create_user
:
109 msg_create
= (struct msg_rpc_create_user
*)m
->data
;
110 printf("monitor_msg: user created (rid=%d)\n", msg_create
->rid
);
116 static BOOL
test_useradd_async(struct dcerpc_pipe
*p
, TALLOC_CTX
*mem_ctx
,
117 struct policy_handle
*handle
, const char* username
)
120 struct composite_context
*c
;
121 struct libnet_rpc_useradd user
;
123 user
.in
.domain_handle
= *handle
;
124 user
.in
.username
= username
;
126 printf("Testing async libnet_rpc_useradd\n");
128 c
= libnet_rpc_useradd_send(p
, &user
, msg_handler
);
130 printf("Failed to call async libnet_rpc_useradd\n");
134 status
= libnet_rpc_useradd_recv(c
, mem_ctx
, &user
);
135 if (!NT_STATUS_IS_OK(status
)) {
136 printf("Calling async libnet_rpc_useradd failed - %s\n", nt_errstr(status
));
145 static BOOL
test_cleanup(struct dcerpc_pipe
*p
, TALLOC_CTX
*mem_ctx
,
146 struct policy_handle
*domain_handle
, const char *username
)
149 struct samr_LookupNames r1
;
150 struct samr_OpenUser r2
;
151 struct samr_DeleteUser r3
;
152 struct lsa_String names
[2];
154 struct policy_handle user_handle
;
156 names
[0].string
= username
;
158 r1
.in
.domain_handle
= domain_handle
;
162 printf("user account lookup '%s'\n", username
);
164 status
= dcerpc_samr_LookupNames(p
, mem_ctx
, &r1
);
165 if (!NT_STATUS_IS_OK(status
)) {
166 printf("LookupNames failed - %s\n", nt_errstr(status
));
170 rid
= r1
.out
.rids
.ids
[0];
172 r2
.in
.domain_handle
= domain_handle
;
173 r2
.in
.access_mask
= SEC_FLAG_MAXIMUM_ALLOWED
;
175 r2
.out
.user_handle
= &user_handle
;
177 printf("opening user account\n");
179 status
= dcerpc_samr_OpenUser(p
, mem_ctx
, &r2
);
180 if (!NT_STATUS_IS_OK(status
)) {
181 printf("OpenUser failed - %s\n", nt_errstr(status
));
185 r3
.in
.user_handle
= &user_handle
;
186 r3
.out
.user_handle
= &user_handle
;
188 printf("deleting user account\n");
190 status
= dcerpc_samr_DeleteUser(p
, mem_ctx
, &r3
);
191 if (!NT_STATUS_IS_OK(status
)) {
192 printf("DeleteUser failed - %s\n", nt_errstr(status
));
200 static BOOL
test_createuser(struct dcerpc_pipe
*p
, TALLOC_CTX
*mem_ctx
,
201 struct policy_handle
*handle
, const char* user
)
204 struct policy_handle user_handle
;
205 struct lsa_String username
;
206 struct samr_CreateUser r1
;
207 struct samr_Close r2
;
210 username
.string
= user
;
212 r1
.in
.domain_handle
= handle
;
213 r1
.in
.account_name
= &username
;
214 r1
.in
.access_mask
= SEC_FLAG_MAXIMUM_ALLOWED
;
215 r1
.out
.user_handle
= &user_handle
;
216 r1
.out
.rid
= &user_rid
;
218 printf("creating user '%s'\n", username
.string
);
220 status
= dcerpc_samr_CreateUser(p
, mem_ctx
, &r1
);
221 if (!NT_STATUS_IS_OK(status
)) {
222 printf("CreateUser failed - %s\n", nt_errstr(status
));
224 if (NT_STATUS_EQUAL(status
, NT_STATUS_USER_EXISTS
)) {
225 printf("User (%s) already exists - attempting to delete and recreate account again\n", user
);
226 if (!test_cleanup(p
, mem_ctx
, handle
, TEST_USERNAME
)) {
230 printf("creating user account\n");
232 status
= dcerpc_samr_CreateUser(p
, mem_ctx
, &r1
);
233 if (!NT_STATUS_IS_OK(status
)) {
234 printf("CreateUser failed - %s\n", nt_errstr(status
));
242 r2
.in
.handle
= &user_handle
;
243 r2
.out
.handle
= &user_handle
;
245 printf("closing user '%s'\n", username
.string
);
247 status
= dcerpc_samr_Close(p
, mem_ctx
, &r2
);
248 if (!NT_STATUS_IS_OK(status
)) {
249 printf("Close failed - %s\n", nt_errstr(status
));
257 static BOOL
test_usermod(struct dcerpc_pipe
*p
, TALLOC_CTX
*mem_ctx
,
258 struct policy_handle
*handle
, int num_changes
,
259 struct libnet_rpc_usermod
*mod
, char **username
)
261 const char* logon_scripts
[] = { "start_login.cmd", "login.bat", "start.cmd" };
262 const char* home_dirs
[] = { "\\\\srv\\home", "\\\\homesrv\\home\\user", "\\\\pdcsrv\\domain" };
263 const char* home_drives
[] = { "H:", "z:", "I:", "J:", "n:" };
264 const char *homedir
, *homedrive
, *logonscript
;
265 const uint32_t flags
[] = { (ACB_DISABLED
| ACB_NORMAL
),
266 (ACB_NORMAL
| ACB_PWNOEXP
),
267 (ACB_NORMAL
| ACB_PW_EXPIRED
) };
271 enum test_fields testfld
;
275 srandom((unsigned)time(NULL
));
277 mod
->in
.username
= talloc_strdup(mem_ctx
, *username
);
278 mod
->in
.domain_handle
= *handle
;
280 printf("modifying user (%d simultaneous change(s))\n", num_changes
);
282 printf("fields to change: [");
284 for (i
= 0; i
< num_changes
&& i
< FIELDS_NUM
- 1; i
++) {
287 testfld
= (random() % (FIELDS_NUM
- 1)) + 1;
289 gettimeofday(&now
, NULL
);
293 continue_if_field_set(mod
->in
.change
.account_name
);
294 mod
->in
.change
.account_name
= talloc_asprintf(mem_ctx
, TEST_CHG_ACCOUNTNAME
,
296 mod
->in
.change
.fields
|= USERMOD_FIELD_ACCOUNT_NAME
;
297 fldname
= "account_name";
298 *username
= talloc_strdup(mem_ctx
, mod
->in
.change
.account_name
);
302 continue_if_field_set(mod
->in
.change
.full_name
);
303 mod
->in
.change
.full_name
= talloc_asprintf(mem_ctx
, TEST_CHG_FULLNAME
,
304 (int)random(), (int)random());
305 mod
->in
.change
.fields
|= USERMOD_FIELD_FULL_NAME
;
306 fldname
= "full_name";
310 continue_if_field_set(mod
->in
.change
.description
);
311 mod
->in
.change
.description
= talloc_asprintf(mem_ctx
, TEST_CHG_DESCRIPTION
,
313 mod
->in
.change
.fields
|= USERMOD_FIELD_DESCRIPTION
;
314 fldname
= "description";
318 continue_if_field_set(mod
->in
.change
.home_directory
);
319 homedir
= home_dirs
[random() % (sizeof(home_dirs
)/sizeof(char*))];
320 mod
->in
.change
.home_directory
= talloc_strdup(mem_ctx
, homedir
);
321 mod
->in
.change
.fields
|= USERMOD_FIELD_HOME_DIRECTORY
;
322 fldname
= "home directory";
326 continue_if_field_set(mod
->in
.change
.home_drive
);
327 homedrive
= home_drives
[random() % (sizeof(home_drives
)/sizeof(char*))];
328 mod
->in
.change
.home_drive
= talloc_strdup(mem_ctx
, homedrive
);
329 mod
->in
.change
.fields
|= USERMOD_FIELD_HOME_DRIVE
;
330 fldname
= "home drive";
334 continue_if_field_set(mod
->in
.change
.comment
);
335 mod
->in
.change
.comment
= talloc_asprintf(mem_ctx
, TEST_CHG_COMMENT
,
337 mod
->in
.change
.fields
|= USERMOD_FIELD_COMMENT
;
342 continue_if_field_set(mod
->in
.change
.logon_script
);
343 logonscript
= logon_scripts
[random() % (sizeof(logon_scripts
)/sizeof(char*))];
344 mod
->in
.change
.logon_script
= talloc_strdup(mem_ctx
, logonscript
);
345 mod
->in
.change
.fields
|= USERMOD_FIELD_LOGON_SCRIPT
;
346 fldname
= "logon script";
350 continue_if_field_set(mod
->in
.change
.profile_path
);
351 mod
->in
.change
.profile_path
= talloc_asprintf(mem_ctx
, TEST_CHG_PROFILEPATH
,
352 (long int)random(), (unsigned int)random());
353 mod
->in
.change
.fields
|= USERMOD_FIELD_PROFILE_PATH
;
354 fldname
= "profile path";
358 continue_if_field_set(mod
->in
.change
.acct_expiry
);
359 now
= timeval_add(&now
, (random() % (31*24*60*60)), 0);
360 mod
->in
.change
.acct_expiry
= talloc_memdup(mem_ctx
, &now
, sizeof(now
));
361 mod
->in
.change
.fields
|= USERMOD_FIELD_ACCT_EXPIRY
;
362 fldname
= "acct_expiry";
366 continue_if_field_set(mod
->in
.change
.acct_flags
);
367 mod
->in
.change
.acct_flags
= flags
[random() % (sizeof(flags
)/sizeof(uint32_t))];
368 mod
->in
.change
.fields
|= USERMOD_FIELD_ACCT_EXPIRY
;
369 fldname
= "acct_flags";
373 fldname
= talloc_asprintf(mem_ctx
, "unknown_field (%d)", testfld
);
377 printf(((i
< num_changes
- 1) ? "%s," : "%s"), fldname
);
381 status
= libnet_rpc_usermod(p
, mem_ctx
, mod
);
382 if (!NT_STATUS_IS_OK(status
)) {
383 printf("Failed to call sync libnet_rpc_usermd - %s\n", nt_errstr(status
));
391 static BOOL
test_userdel(struct dcerpc_pipe
*p
, TALLOC_CTX
*mem_ctx
,
392 struct policy_handle
*handle
, const char *username
)
395 struct libnet_rpc_userdel user
;
397 user
.in
.domain_handle
= *handle
;
398 user
.in
.username
= username
;
400 status
= libnet_rpc_userdel(p
, mem_ctx
, &user
);
401 if (!NT_STATUS_IS_OK(status
)) {
402 printf("Failed to call sync libnet_rpc_userdel - %s\n", nt_errstr(status
));
410 #define CMP_LSA_STRING_FLD(fld, flags) \
411 if ((mod->in.change.fields & flags) && \
412 !strequal(i->fld.string, mod->in.change.fld)) { \
413 printf("'%s' field does not match\n", #fld); \
414 printf("received: '%s'\n", i->fld.string); \
415 printf("expected: '%s'\n", mod->in.change.fld); \
420 #define CMP_TIME_FLD(fld, flags) \
421 if (mod->in.change.fields & flags) { \
422 nttime_to_timeval(&t, i->fld); \
423 if (timeval_compare(&t, mod->in.change.fld)) { \
424 printf("'%s' field does not match\n", #fld); \
425 printf("received: '%s (+%ld us)'\n", timestring(mem_ctx, t.tv_sec), t.tv_usec); \
426 printf("expected: '%s (+%ld us)'\n", timestring(mem_ctx, mod->in.change.fld->tv_sec), mod->in.change.fld->tv_usec); \
431 #define CMP_NUM_FLD(fld, flags) \
432 if ((mod->in.change.fields & flags) && \
433 (i->fld != mod->in.change.fld)) { \
434 printf("'%s' field does not match\n", #fld); \
435 printf("received: '%04x'\n", i->fld); \
436 printf("expected: '%04x'\n", mod->in.change.fld); \
441 static BOOL
test_compare(struct dcerpc_pipe
*p
, TALLOC_CTX
*mem_ctx
,
442 struct policy_handle
*handle
, struct libnet_rpc_usermod
*mod
,
443 const char *username
)
446 struct libnet_rpc_userinfo info
;
447 struct samr_UserInfo21
*i
;
452 info
.in
.username
= username
;
453 info
.in
.domain_handle
= *handle
;
454 info
.in
.level
= 21; /* the most rich infolevel available */
456 status
= libnet_rpc_userinfo(p
, mem_ctx
, &info
);
457 if (!NT_STATUS_IS_OK(status
)) {
458 printf("Failed to call sync libnet_rpc_userinfo - %s\n", nt_errstr(status
));
462 i
= &info
.out
.info
.info21
;
464 CMP_LSA_STRING_FLD(account_name
, USERMOD_FIELD_ACCOUNT_NAME
);
465 CMP_LSA_STRING_FLD(full_name
, USERMOD_FIELD_FULL_NAME
);
466 CMP_LSA_STRING_FLD(description
, USERMOD_FIELD_DESCRIPTION
);
467 CMP_LSA_STRING_FLD(comment
, USERMOD_FIELD_COMMENT
);
468 CMP_LSA_STRING_FLD(logon_script
, USERMOD_FIELD_LOGON_SCRIPT
);
469 CMP_LSA_STRING_FLD(profile_path
, USERMOD_FIELD_PROFILE_PATH
);
470 CMP_LSA_STRING_FLD(home_directory
, USERMOD_FIELD_HOME_DIRECTORY
);
471 CMP_LSA_STRING_FLD(home_drive
, USERMOD_FIELD_HOME_DRIVE
);
472 CMP_TIME_FLD(acct_expiry
, USERMOD_FIELD_ACCT_EXPIRY
);
473 CMP_NUM_FLD(acct_flags
, USERMOD_FIELD_ACCT_FLAGS
)
479 BOOL
torture_useradd(struct torture_context
*torture
)
483 struct dcerpc_pipe
*p
;
484 struct policy_handle h
;
485 struct lsa_String domain_name
;
486 const char *name
= TEST_USERNAME
;
490 mem_ctx
= talloc_init("test_useradd");
491 binding
= lp_parm_string(-1, "torture", "binding");
493 status
= torture_rpc_connection(mem_ctx
,
497 if (!NT_STATUS_IS_OK(status
)) {
501 domain_name
.string
= lp_workgroup();
502 if (!test_opendomain(p
, mem_ctx
, &h
, &domain_name
)) {
507 if (!test_useradd(p
, mem_ctx
, &h
, name
)) {
512 if (!test_cleanup(p
, mem_ctx
, &h
, name
)) {
517 if (!test_opendomain(p
, mem_ctx
, &h
, &domain_name
)) {
522 if (!test_useradd_async(p
, mem_ctx
, &h
, name
)) {
527 if (!test_cleanup(p
, mem_ctx
, &h
, name
)) {
533 talloc_free(mem_ctx
);
538 BOOL
torture_userdel(struct torture_context
*torture
)
542 struct dcerpc_pipe
*p
;
543 struct policy_handle h
;
544 struct lsa_String domain_name
;
545 const char *name
= TEST_USERNAME
;
549 mem_ctx
= talloc_init("test_userdel");
550 binding
= lp_parm_string(-1, "torture", "binding");
552 status
= torture_rpc_connection(mem_ctx
,
556 if (!NT_STATUS_IS_OK(status
)) {
560 domain_name
.string
= lp_workgroup();
561 if (!test_opendomain(p
, mem_ctx
, &h
, &domain_name
)) {
566 if (!test_createuser(p
, mem_ctx
, &h
, name
)) {
571 if (!test_userdel(p
, mem_ctx
, &h
, name
)) {
577 talloc_free(mem_ctx
);
582 BOOL
torture_usermod(struct torture_context
*torture
)
586 struct dcerpc_pipe
*p
;
587 struct policy_handle h
;
588 struct lsa_String domain_name
;
594 mem_ctx
= talloc_init("test_userdel");
595 binding
= lp_parm_string(-1, "torture", "binding");
597 status
= torture_rpc_connection(mem_ctx
,
601 if (!NT_STATUS_IS_OK(status
)) {
606 domain_name
.string
= lp_workgroup();
607 name
= talloc_strdup(mem_ctx
, TEST_USERNAME
);
609 if (!test_opendomain(p
, mem_ctx
, &h
, &domain_name
)) {
614 if (!test_createuser(p
, mem_ctx
, &h
, name
)) {
619 for (i
= 1; i
< FIELDS_NUM
; i
++) {
620 struct libnet_rpc_usermod m
;
622 if (!test_usermod(p
, mem_ctx
, &h
, i
, &m
, &name
)) {
627 if (!test_compare(p
, mem_ctx
, &h
, &m
, name
)) {
634 if (!test_cleanup(p
, mem_ctx
, &h
, name
)) {
640 talloc_free(mem_ctx
);