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"
26 #include "param/param.h"
28 #include "torture/libnet/utils.h"
31 static bool test_useradd(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
32 struct policy_handle *domain_handle,
37 struct libnet_rpc_useradd user;
39 user.in.domain_handle = *domain_handle;
40 user.in.username = name;
42 printf("Testing libnet_rpc_useradd\n");
44 status = libnet_rpc_useradd(p, mem_ctx, &user);
45 if (!NT_STATUS_IS_OK(status)) {
46 printf("Failed to call libnet_rpc_useradd - %s\n", nt_errstr(status));
54 static bool test_useradd_async(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
55 struct policy_handle *handle, const char* username)
58 struct composite_context *c;
59 struct libnet_rpc_useradd user;
61 user.in.domain_handle = *handle;
62 user.in.username = username;
64 printf("Testing async libnet_rpc_useradd\n");
66 c = libnet_rpc_useradd_send(p, &user, msg_handler);
68 printf("Failed to call async libnet_rpc_useradd\n");
72 status = libnet_rpc_useradd_recv(c, mem_ctx, &user);
73 if (!NT_STATUS_IS_OK(status)) {
74 printf("Calling async libnet_rpc_useradd failed - %s\n", nt_errstr(status));
82 static bool test_usermod(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
83 struct policy_handle *handle, int num_changes,
84 struct libnet_rpc_usermod *mod, char **username)
86 const char* logon_scripts[] = { "start_login.cmd", "login.bat", "start.cmd" };
87 const char* home_dirs[] = { "\\\\srv\\home", "\\\\homesrv\\home\\user", "\\\\pdcsrv\\domain" };
88 const char* home_drives[] = { "H:", "z:", "I:", "J:", "n:" };
89 const char *homedir, *homedrive, *logonscript;
90 const uint32_t flags[] = { (ACB_DISABLED | ACB_NORMAL),
91 (ACB_NORMAL | ACB_PWNOEXP),
96 enum test_fields testfld;
100 srandom((unsigned)time(NULL));
102 mod->in.username = talloc_strdup(mem_ctx, *username);
103 mod->in.domain_handle = *handle;
105 printf("modifying user (%d simultaneous change(s))\n", num_changes);
107 printf("fields to change: [");
109 for (i = 0; i < num_changes && i < FIELDS_NUM - 1; i++) {
112 testfld = (random() % (FIELDS_NUM - 1)) + 1;
114 gettimeofday(&now, NULL);
118 continue_if_field_set(mod->in.change.account_name);
119 mod->in.change.account_name = talloc_asprintf(mem_ctx, TEST_CHG_ACCOUNTNAME,
120 (int)(random() % 100));
121 mod->in.change.fields |= USERMOD_FIELD_ACCOUNT_NAME;
122 fldname = "account_name";
123 *username = talloc_strdup(mem_ctx, mod->in.change.account_name);
127 continue_if_field_set(mod->in.change.full_name);
128 mod->in.change.full_name = talloc_asprintf(mem_ctx, TEST_CHG_FULLNAME,
129 (int)random(), (int)random());
130 mod->in.change.fields |= USERMOD_FIELD_FULL_NAME;
131 fldname = "full_name";
135 continue_if_field_set(mod->in.change.description);
136 mod->in.change.description = talloc_asprintf(mem_ctx, TEST_CHG_DESCRIPTION,
138 mod->in.change.fields |= USERMOD_FIELD_DESCRIPTION;
139 fldname = "description";
143 continue_if_field_set(mod->in.change.home_directory);
144 homedir = home_dirs[random() % (sizeof(home_dirs)/sizeof(char*))];
145 mod->in.change.home_directory = talloc_strdup(mem_ctx, homedir);
146 mod->in.change.fields |= USERMOD_FIELD_HOME_DIRECTORY;
147 fldname = "home_directory";
151 continue_if_field_set(mod->in.change.home_drive);
152 homedrive = home_drives[random() % (sizeof(home_drives)/sizeof(char*))];
153 mod->in.change.home_drive = talloc_strdup(mem_ctx, homedrive);
154 mod->in.change.fields |= USERMOD_FIELD_HOME_DRIVE;
155 fldname = "home_drive";
159 continue_if_field_set(mod->in.change.comment);
160 mod->in.change.comment = talloc_asprintf(mem_ctx, TEST_CHG_COMMENT,
162 mod->in.change.fields |= USERMOD_FIELD_COMMENT;
167 continue_if_field_set(mod->in.change.logon_script);
168 logonscript = logon_scripts[random() % (sizeof(logon_scripts)/sizeof(char*))];
169 mod->in.change.logon_script = talloc_strdup(mem_ctx, logonscript);
170 mod->in.change.fields |= USERMOD_FIELD_LOGON_SCRIPT;
171 fldname = "logon_script";
175 continue_if_field_set(mod->in.change.profile_path);
176 mod->in.change.profile_path = talloc_asprintf(mem_ctx, TEST_CHG_PROFILEPATH,
177 (long int)random(), (unsigned int)random());
178 mod->in.change.fields |= USERMOD_FIELD_PROFILE_PATH;
179 fldname = "profile_path";
183 continue_if_field_set(mod->in.change.acct_expiry);
184 now = timeval_add(&now, (random() % (31*24*60*60)), 0);
185 mod->in.change.acct_expiry = (struct timeval *)talloc_memdup(mem_ctx, &now, sizeof(now));
186 mod->in.change.fields |= USERMOD_FIELD_ACCT_EXPIRY;
187 fldname = "acct_expiry";
191 continue_if_field_set(mod->in.change.acct_flags);
192 mod->in.change.acct_flags = flags[random() % ARRAY_SIZE(flags)];
193 mod->in.change.fields |= USERMOD_FIELD_ACCT_FLAGS;
194 fldname = "acct_flags";
198 fldname = talloc_asprintf(mem_ctx, "unknown_field (%d)", testfld);
202 printf(((i < num_changes - 1) ? "%s," : "%s"), fldname);
206 status = libnet_rpc_usermod(p, mem_ctx, mod);
207 if (!NT_STATUS_IS_OK(status)) {
208 printf("Failed to call sync libnet_rpc_usermod - %s\n", nt_errstr(status));
216 static bool test_userdel(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
217 struct policy_handle *handle, const char *username)
220 struct libnet_rpc_userdel user;
222 user.in.domain_handle = *handle;
223 user.in.username = username;
225 status = libnet_rpc_userdel(p, mem_ctx, &user);
226 if (!NT_STATUS_IS_OK(status)) {
227 printf("Failed to call sync libnet_rpc_userdel - %s\n", nt_errstr(status));
235 #define CMP_LSA_STRING_FLD(fld, flags) \
236 if ((mod->in.change.fields & flags) && \
237 !strequal(i->fld.string, mod->in.change.fld)) { \
238 printf("'%s' field does not match\n", #fld); \
239 printf("received: '%s'\n", i->fld.string); \
240 printf("expected: '%s'\n", mod->in.change.fld); \
245 #define CMP_TIME_FLD(fld, flags) \
246 if (mod->in.change.fields & flags) { \
247 nttime_to_timeval(&t, i->fld); \
248 if (timeval_compare(&t, mod->in.change.fld)) { \
249 printf("'%s' field does not match\n", #fld); \
250 printf("received: '%s (+%ld us)'\n", \
251 timestring(mem_ctx, t.tv_sec), t.tv_usec); \
252 printf("expected: '%s (+%ld us)'\n", \
253 timestring(mem_ctx, mod->in.change.fld->tv_sec), \
254 mod->in.change.fld->tv_usec); \
259 #define CMP_NUM_FLD(fld, flags) \
260 if ((mod->in.change.fields & flags) && \
261 (i->fld != mod->in.change.fld)) { \
262 printf("'%s' field does not match\n", #fld); \
263 printf("received: '%04x'\n", i->fld); \
264 printf("expected: '%04x'\n", mod->in.change.fld); \
269 static bool test_compare(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
270 struct policy_handle *handle, struct libnet_rpc_usermod *mod,
271 const char *username)
274 struct libnet_rpc_userinfo info;
275 struct samr_UserInfo21 *i;
280 info.in.username = username;
281 info.in.domain_handle = *handle;
282 info.in.level = 21; /* the most rich infolevel available */
284 status = libnet_rpc_userinfo(p, mem_ctx, &info);
285 if (!NT_STATUS_IS_OK(status)) {
286 printf("Failed to call sync libnet_rpc_userinfo - %s\n", nt_errstr(status));
290 i = &info.out.info.info21;
292 CMP_LSA_STRING_FLD(account_name, USERMOD_FIELD_ACCOUNT_NAME);
293 CMP_LSA_STRING_FLD(full_name, USERMOD_FIELD_FULL_NAME);
294 CMP_LSA_STRING_FLD(description, USERMOD_FIELD_DESCRIPTION);
295 CMP_LSA_STRING_FLD(comment, USERMOD_FIELD_COMMENT);
296 CMP_LSA_STRING_FLD(logon_script, USERMOD_FIELD_LOGON_SCRIPT);
297 CMP_LSA_STRING_FLD(profile_path, USERMOD_FIELD_PROFILE_PATH);
298 CMP_LSA_STRING_FLD(home_directory, USERMOD_FIELD_HOME_DIRECTORY);
299 CMP_LSA_STRING_FLD(home_drive, USERMOD_FIELD_HOME_DRIVE);
300 CMP_TIME_FLD(acct_expiry, USERMOD_FIELD_ACCT_EXPIRY);
301 CMP_NUM_FLD(acct_flags, USERMOD_FIELD_ACCT_FLAGS)
307 bool torture_useradd(struct torture_context *torture)
310 struct dcerpc_pipe *p;
311 struct policy_handle h;
312 struct lsa_String domain_name;
314 const char *name = TEST_USERNAME;
318 mem_ctx = talloc_init("test_useradd");
320 status = torture_rpc_connection(torture,
324 if (!NT_STATUS_IS_OK(status)) {
328 domain_name.string = lp_workgroup(torture->lp_ctx);
329 if (!test_opendomain(p, mem_ctx, &h, &domain_name, &sid)) {
334 if (!test_useradd(p, mem_ctx, &h, name)) {
339 if (!test_user_cleanup(p, mem_ctx, &h, name)) {
344 if (!test_opendomain(p, mem_ctx, &h, &domain_name, &sid)) {
349 if (!test_useradd_async(p, mem_ctx, &h, name)) {
354 if (!test_user_cleanup(p, mem_ctx, &h, name)) {
360 talloc_free(mem_ctx);
365 bool torture_userdel(struct torture_context *torture)
368 struct dcerpc_pipe *p;
369 struct policy_handle h;
370 struct lsa_String domain_name;
373 const char *name = TEST_USERNAME;
377 mem_ctx = talloc_init("test_userdel");
379 status = torture_rpc_connection(torture,
383 if (!NT_STATUS_IS_OK(status)) {
387 domain_name.string = lp_workgroup(torture->lp_ctx);
388 if (!test_opendomain(p, mem_ctx, &h, &domain_name, &sid)) {
393 if (!test_user_create(p, mem_ctx, &h, name, &rid)) {
398 if (!test_userdel(p, mem_ctx, &h, name)) {
404 talloc_free(mem_ctx);
409 bool torture_usermod(struct torture_context *torture)
412 struct dcerpc_pipe *p;
413 struct policy_handle h;
414 struct lsa_String domain_name;
422 mem_ctx = talloc_init("test_userdel");
424 status = torture_rpc_connection(torture,
428 if (!NT_STATUS_IS_OK(status)) {
433 domain_name.string = lp_workgroup(torture->lp_ctx);
434 name = talloc_strdup(mem_ctx, TEST_USERNAME);
436 if (!test_opendomain(p, mem_ctx, &h, &domain_name, &sid)) {
441 if (!test_user_create(p, mem_ctx, &h, name, &rid)) {
446 for (i = 1; i < FIELDS_NUM; i++) {
447 struct libnet_rpc_usermod m;
449 if (!test_usermod(p, mem_ctx, &h, i, &m, &name)) {
454 if (!test_compare(p, mem_ctx, &h, &m, name)) {
461 if (!test_user_cleanup(p, mem_ctx, &h, name)) {
467 talloc_free(mem_ctx);