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/rpc.h"
23 #include "torture/libnet/usertest.h"
24 #include "libnet/libnet.h"
25 #include "librpc/gen_ndr/ndr_samr_c.h"
28 static BOOL test_opendomain(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
29 struct policy_handle *handle, struct lsa_String *domname)
32 struct policy_handle h, domain_handle;
33 struct samr_Connect r1;
34 struct samr_LookupDomain r2;
35 struct samr_OpenDomain r3;
37 printf("connecting\n");
39 r1.in.system_name = 0;
40 r1.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
41 r1.out.connect_handle = &h;
43 status = dcerpc_samr_Connect(p, mem_ctx, &r1);
44 if (!NT_STATUS_IS_OK(status)) {
45 printf("Connect failed - %s\n", nt_errstr(status));
49 r2.in.connect_handle = &h;
50 r2.in.domain_name = domname;
52 printf("domain lookup on %s\n", domname->string);
54 status = dcerpc_samr_LookupDomain(p, mem_ctx, &r2);
55 if (!NT_STATUS_IS_OK(status)) {
56 printf("LookupDomain failed - %s\n", nt_errstr(status));
60 r3.in.connect_handle = &h;
61 r3.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
62 r3.in.sid = r2.out.sid;
63 r3.out.domain_handle = &domain_handle;
65 printf("opening domain\n");
67 status = dcerpc_samr_OpenDomain(p, mem_ctx, &r3);
68 if (!NT_STATUS_IS_OK(status)) {
69 printf("OpenDomain failed - %s\n", nt_errstr(status));
72 *handle = domain_handle;
79 static BOOL test_useradd(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
80 struct policy_handle *domain_handle,
85 struct libnet_rpc_useradd user;
87 user.in.domain_handle = *domain_handle;
88 user.in.username = name;
90 printf("Testing libnet_rpc_useradd\n");
92 status = libnet_rpc_useradd(p, mem_ctx, &user);
93 if (!NT_STATUS_IS_OK(status)) {
94 printf("Failed to call sync rpc_composite_userinfo - %s\n", nt_errstr(status));
102 static void msg_handler(struct monitor_msg *m)
104 struct msg_rpc_create_user *msg_create;
107 case mon_SamrCreateUser:
108 msg_create = (struct msg_rpc_create_user*)m->data;
109 printf("monitor_msg: user created (rid=%d)\n", msg_create->rid);
115 static BOOL test_useradd_async(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
116 struct policy_handle *handle, const char* username)
119 struct composite_context *c;
120 struct libnet_rpc_useradd user;
122 user.in.domain_handle = *handle;
123 user.in.username = username;
125 printf("Testing async libnet_rpc_useradd\n");
127 c = libnet_rpc_useradd_send(p, &user, msg_handler);
129 printf("Failed to call async libnet_rpc_useradd\n");
133 status = libnet_rpc_useradd_recv(c, mem_ctx, &user);
134 if (!NT_STATUS_IS_OK(status)) {
135 printf("Calling async libnet_rpc_useradd failed - %s\n", nt_errstr(status));
144 static BOOL test_cleanup(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
145 struct policy_handle *domain_handle, const char *username)
148 struct samr_LookupNames r1;
149 struct samr_OpenUser r2;
150 struct samr_DeleteUser r3;
151 struct lsa_String names[2];
153 struct policy_handle user_handle;
155 names[0].string = username;
157 r1.in.domain_handle = domain_handle;
161 printf("user account lookup '%s'\n", username);
163 status = dcerpc_samr_LookupNames(p, mem_ctx, &r1);
164 if (!NT_STATUS_IS_OK(status)) {
165 printf("LookupNames failed - %s\n", nt_errstr(status));
169 rid = r1.out.rids.ids[0];
171 r2.in.domain_handle = domain_handle;
172 r2.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
174 r2.out.user_handle = &user_handle;
176 printf("opening user account\n");
178 status = dcerpc_samr_OpenUser(p, mem_ctx, &r2);
179 if (!NT_STATUS_IS_OK(status)) {
180 printf("OpenUser failed - %s\n", nt_errstr(status));
184 r3.in.user_handle = &user_handle;
185 r3.out.user_handle = &user_handle;
187 printf("deleting user account\n");
189 status = dcerpc_samr_DeleteUser(p, mem_ctx, &r3);
190 if (!NT_STATUS_IS_OK(status)) {
191 printf("DeleteUser failed - %s\n", nt_errstr(status));
199 static BOOL test_createuser(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
200 struct policy_handle *handle, const char* user)
203 struct policy_handle user_handle;
204 struct lsa_String username;
205 struct samr_CreateUser r1;
206 struct samr_Close r2;
209 username.string = user;
211 r1.in.domain_handle = handle;
212 r1.in.account_name = &username;
213 r1.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
214 r1.out.user_handle = &user_handle;
215 r1.out.rid = &user_rid;
217 printf("creating user '%s'\n", username.string);
219 status = dcerpc_samr_CreateUser(p, mem_ctx, &r1);
220 if (!NT_STATUS_IS_OK(status)) {
221 printf("CreateUser failed - %s\n", nt_errstr(status));
223 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
224 printf("User (%s) already exists - attempting to delete and recreate account again\n", user);
225 if (!test_cleanup(p, mem_ctx, handle, TEST_USERNAME)) {
229 printf("creating user account\n");
231 status = dcerpc_samr_CreateUser(p, mem_ctx, &r1);
232 if (!NT_STATUS_IS_OK(status)) {
233 printf("CreateUser failed - %s\n", nt_errstr(status));
241 r2.in.handle = &user_handle;
242 r2.out.handle = &user_handle;
244 printf("closing user '%s'\n", username.string);
246 status = dcerpc_samr_Close(p, mem_ctx, &r2);
247 if (!NT_STATUS_IS_OK(status)) {
248 printf("Close failed - %s\n", nt_errstr(status));
256 static BOOL test_usermod(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
257 struct policy_handle *handle, int num_changes,
258 struct libnet_rpc_usermod *mod, char **username)
260 const char* logon_scripts[] = { "start_login.cmd", "login.bat", "start.cmd" };
261 const char* home_dirs[] = { "\\\\srv\\home", "\\\\homesrv\\home\\user", "\\\\pdcsrv\\domain" };
262 const char* home_drives[] = { "H:", "z:", "I:", "J:", "n:" };
263 const char *homedir, *homedrive, *logonscript;
264 const uint32_t flags[] = { (ACB_DISABLED | ACB_NORMAL),
265 (ACB_NORMAL | ACB_PWNOEXP),
270 enum test_fields testfld;
274 srandom((unsigned)time(NULL));
276 mod->in.username = talloc_strdup(mem_ctx, *username);
277 mod->in.domain_handle = *handle;
279 printf("modifying user (%d simultaneous change(s))\n", num_changes);
281 printf("fields to change: [");
283 for (i = 0; i < num_changes && i < FIELDS_NUM - 1; i++) {
286 testfld = (random() % (FIELDS_NUM - 1)) + 1;
288 gettimeofday(&now, NULL);
292 continue_if_field_set(mod->in.change.account_name);
293 mod->in.change.account_name = talloc_asprintf(mem_ctx, TEST_CHG_ACCOUNTNAME,
294 (int)(random() % 100));
295 mod->in.change.fields |= USERMOD_FIELD_ACCOUNT_NAME;
296 fldname = "account_name";
297 *username = talloc_strdup(mem_ctx, mod->in.change.account_name);
301 continue_if_field_set(mod->in.change.full_name);
302 mod->in.change.full_name = talloc_asprintf(mem_ctx, TEST_CHG_FULLNAME,
303 (int)random(), (int)random());
304 mod->in.change.fields |= USERMOD_FIELD_FULL_NAME;
305 fldname = "full_name";
309 continue_if_field_set(mod->in.change.description);
310 mod->in.change.description = talloc_asprintf(mem_ctx, TEST_CHG_DESCRIPTION,
312 mod->in.change.fields |= USERMOD_FIELD_DESCRIPTION;
313 fldname = "description";
317 continue_if_field_set(mod->in.change.home_directory);
318 homedir = home_dirs[random() % (sizeof(home_dirs)/sizeof(char*))];
319 mod->in.change.home_directory = talloc_strdup(mem_ctx, homedir);
320 mod->in.change.fields |= USERMOD_FIELD_HOME_DIRECTORY;
321 fldname = "home_directory";
325 continue_if_field_set(mod->in.change.home_drive);
326 homedrive = home_drives[random() % (sizeof(home_drives)/sizeof(char*))];
327 mod->in.change.home_drive = talloc_strdup(mem_ctx, homedrive);
328 mod->in.change.fields |= USERMOD_FIELD_HOME_DRIVE;
329 fldname = "home_drive";
333 continue_if_field_set(mod->in.change.comment);
334 mod->in.change.comment = talloc_asprintf(mem_ctx, TEST_CHG_COMMENT,
336 mod->in.change.fields |= USERMOD_FIELD_COMMENT;
341 continue_if_field_set(mod->in.change.logon_script);
342 logonscript = logon_scripts[random() % (sizeof(logon_scripts)/sizeof(char*))];
343 mod->in.change.logon_script = talloc_strdup(mem_ctx, logonscript);
344 mod->in.change.fields |= USERMOD_FIELD_LOGON_SCRIPT;
345 fldname = "logon_script";
349 continue_if_field_set(mod->in.change.profile_path);
350 mod->in.change.profile_path = talloc_asprintf(mem_ctx, TEST_CHG_PROFILEPATH,
351 (long int)random(), (unsigned int)random());
352 mod->in.change.fields |= USERMOD_FIELD_PROFILE_PATH;
353 fldname = "profile_path";
357 continue_if_field_set(mod->in.change.acct_expiry);
358 now = timeval_add(&now, (random() % (31*24*60*60)), 0);
359 mod->in.change.acct_expiry = talloc_memdup(mem_ctx, &now, sizeof(now));
360 mod->in.change.fields |= USERMOD_FIELD_ACCT_EXPIRY;
361 fldname = "acct_expiry";
365 continue_if_field_set(mod->in.change.acct_flags);
366 mod->in.change.acct_flags = flags[random() % (sizeof(flags)/sizeof(uint32_t))];
367 mod->in.change.fields |= USERMOD_FIELD_ACCT_FLAGS;
368 fldname = "acct_flags";
372 fldname = talloc_asprintf(mem_ctx, "unknown_field (%d)", testfld);
376 printf(((i < num_changes - 1) ? "%s," : "%s"), fldname);
380 status = libnet_rpc_usermod(p, mem_ctx, mod);
381 if (!NT_STATUS_IS_OK(status)) {
382 printf("Failed to call sync libnet_rpc_usermod - %s\n", nt_errstr(status));
390 static BOOL test_userdel(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
391 struct policy_handle *handle, const char *username)
394 struct libnet_rpc_userdel user;
396 user.in.domain_handle = *handle;
397 user.in.username = username;
399 status = libnet_rpc_userdel(p, mem_ctx, &user);
400 if (!NT_STATUS_IS_OK(status)) {
401 printf("Failed to call sync libnet_rpc_userdel - %s\n", nt_errstr(status));
409 #define CMP_LSA_STRING_FLD(fld, flags) \
410 if ((mod->in.change.fields & flags) && \
411 !strequal(i->fld.string, mod->in.change.fld)) { \
412 printf("'%s' field does not match\n", #fld); \
413 printf("received: '%s'\n", i->fld.string); \
414 printf("expected: '%s'\n", mod->in.change.fld); \
419 #define CMP_TIME_FLD(fld, flags) \
420 if (mod->in.change.fields & flags) { \
421 nttime_to_timeval(&t, i->fld); \
422 if (timeval_compare(&t, mod->in.change.fld)) { \
423 printf("'%s' field does not match\n", #fld); \
424 printf("received: '%s (+%ld us)'\n", timestring(mem_ctx, t.tv_sec), t.tv_usec); \
425 printf("expected: '%s (+%ld us)'\n", timestring(mem_ctx, mod->in.change.fld->tv_sec), mod->in.change.fld->tv_usec); \
430 #define CMP_NUM_FLD(fld, flags) \
431 if ((mod->in.change.fields & flags) && \
432 (i->fld != mod->in.change.fld)) { \
433 printf("'%s' field does not match\n", #fld); \
434 printf("received: '%04x'\n", i->fld); \
435 printf("expected: '%04x'\n", mod->in.change.fld); \
440 static BOOL test_compare(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
441 struct policy_handle *handle, struct libnet_rpc_usermod *mod,
442 const char *username)
445 struct libnet_rpc_userinfo info;
446 struct samr_UserInfo21 *i;
451 info.in.username = username;
452 info.in.domain_handle = *handle;
453 info.in.level = 21; /* the most rich infolevel available */
455 status = libnet_rpc_userinfo(p, mem_ctx, &info);
456 if (!NT_STATUS_IS_OK(status)) {
457 printf("Failed to call sync libnet_rpc_userinfo - %s\n", nt_errstr(status));
461 i = &info.out.info.info21;
463 CMP_LSA_STRING_FLD(account_name, USERMOD_FIELD_ACCOUNT_NAME);
464 CMP_LSA_STRING_FLD(full_name, USERMOD_FIELD_FULL_NAME);
465 CMP_LSA_STRING_FLD(description, USERMOD_FIELD_DESCRIPTION);
466 CMP_LSA_STRING_FLD(comment, USERMOD_FIELD_COMMENT);
467 CMP_LSA_STRING_FLD(logon_script, USERMOD_FIELD_LOGON_SCRIPT);
468 CMP_LSA_STRING_FLD(profile_path, USERMOD_FIELD_PROFILE_PATH);
469 CMP_LSA_STRING_FLD(home_directory, USERMOD_FIELD_HOME_DIRECTORY);
470 CMP_LSA_STRING_FLD(home_drive, USERMOD_FIELD_HOME_DRIVE);
471 CMP_TIME_FLD(acct_expiry, USERMOD_FIELD_ACCT_EXPIRY);
472 CMP_NUM_FLD(acct_flags, USERMOD_FIELD_ACCT_FLAGS)
478 BOOL torture_useradd(struct torture_context *torture)
482 struct dcerpc_pipe *p;
483 struct policy_handle h;
484 struct lsa_String domain_name;
485 const char *name = TEST_USERNAME;
489 mem_ctx = talloc_init("test_useradd");
490 binding = torture_setting_string(torture, "binding", NULL);
492 status = torture_rpc_connection(mem_ctx,
496 if (!NT_STATUS_IS_OK(status)) {
500 domain_name.string = lp_workgroup();
501 if (!test_opendomain(p, mem_ctx, &h, &domain_name)) {
506 if (!test_useradd(p, mem_ctx, &h, name)) {
511 if (!test_cleanup(p, mem_ctx, &h, name)) {
516 if (!test_opendomain(p, mem_ctx, &h, &domain_name)) {
521 if (!test_useradd_async(p, mem_ctx, &h, name)) {
526 if (!test_cleanup(p, mem_ctx, &h, name)) {
532 talloc_free(mem_ctx);
537 BOOL torture_userdel(struct torture_context *torture)
541 struct dcerpc_pipe *p;
542 struct policy_handle h;
543 struct lsa_String domain_name;
544 const char *name = TEST_USERNAME;
548 mem_ctx = talloc_init("test_userdel");
549 binding = torture_setting_string(torture, "binding", NULL);
551 status = torture_rpc_connection(mem_ctx,
555 if (!NT_STATUS_IS_OK(status)) {
559 domain_name.string = lp_workgroup();
560 if (!test_opendomain(p, mem_ctx, &h, &domain_name)) {
565 if (!test_createuser(p, mem_ctx, &h, name)) {
570 if (!test_userdel(p, mem_ctx, &h, name)) {
576 talloc_free(mem_ctx);
581 BOOL torture_usermod(struct torture_context *torture)
585 struct dcerpc_pipe *p;
586 struct policy_handle h;
587 struct lsa_String domain_name;
593 mem_ctx = talloc_init("test_userdel");
594 binding = torture_setting_string(torture, "binding", NULL);
596 status = torture_rpc_connection(mem_ctx,
600 if (!NT_STATUS_IS_OK(status)) {
605 domain_name.string = lp_workgroup();
606 name = talloc_strdup(mem_ctx, TEST_USERNAME);
608 if (!test_opendomain(p, mem_ctx, &h, &domain_name)) {
613 if (!test_createuser(p, mem_ctx, &h, name)) {
618 for (i = 1; i < FIELDS_NUM; i++) {
619 struct libnet_rpc_usermod m;
621 if (!test_usermod(p, mem_ctx, &h, i, &m, &name)) {
626 if (!test_compare(p, mem_ctx, &h, &m, name)) {
633 if (!test_cleanup(p, mem_ctx, &h, name)) {
639 talloc_free(mem_ctx);