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 torture_context *tctx, struct dcerpc_pipe *p,
84 struct policy_handle *handle, int num_changes,
85 struct libnet_rpc_usermod *mod, char **username)
87 const char* logon_scripts[] = { "start_login.cmd", "login.bat", "start.cmd" };
88 const char* home_dirs[] = { "\\\\srv\\home", "\\\\homesrv\\home\\user", "\\\\pdcsrv\\domain" };
89 const char* home_drives[] = { "H:", "z:", "I:", "J:", "n:" };
90 const char *homedir, *homedrive, *logonscript;
91 const uint32_t flags[] = { (ACB_DISABLED | ACB_NORMAL),
92 (ACB_NORMAL | ACB_PWNOEXP),
97 enum test_fields testfld;
101 srandom((unsigned)time(NULL));
103 mod->in.username = talloc_strdup(mem_ctx, *username);
104 mod->in.domain_handle = *handle;
106 torture_comment(tctx, "modifying user (%d simultaneous change(s))\n",
109 torture_comment(tctx, "fields to change: [");
111 for (i = 0; i < num_changes && i < FIELDS_NUM - 1; i++) {
114 testfld = (random() % (FIELDS_NUM - 1)) + 1;
116 gettimeofday(&now, NULL);
120 continue_if_field_set(mod->in.change.account_name);
121 mod->in.change.account_name = talloc_asprintf(mem_ctx, TEST_CHG_ACCOUNTNAME,
122 (int)(random() % 100));
123 mod->in.change.fields |= USERMOD_FIELD_ACCOUNT_NAME;
124 fldname = "account_name";
125 *username = talloc_strdup(mem_ctx, mod->in.change.account_name);
129 continue_if_field_set(mod->in.change.full_name);
130 mod->in.change.full_name = talloc_asprintf(mem_ctx, TEST_CHG_FULLNAME,
131 (int)random(), (int)random());
132 mod->in.change.fields |= USERMOD_FIELD_FULL_NAME;
133 fldname = "full_name";
137 continue_if_field_set(mod->in.change.description);
138 mod->in.change.description = talloc_asprintf(mem_ctx, TEST_CHG_DESCRIPTION,
140 mod->in.change.fields |= USERMOD_FIELD_DESCRIPTION;
141 fldname = "description";
145 continue_if_field_set(mod->in.change.home_directory);
146 homedir = home_dirs[random() % (sizeof(home_dirs)/sizeof(char*))];
147 mod->in.change.home_directory = talloc_strdup(mem_ctx, homedir);
148 mod->in.change.fields |= USERMOD_FIELD_HOME_DIRECTORY;
149 fldname = "home_directory";
153 continue_if_field_set(mod->in.change.home_drive);
154 homedrive = home_drives[random() % (sizeof(home_drives)/sizeof(char*))];
155 mod->in.change.home_drive = talloc_strdup(mem_ctx, homedrive);
156 mod->in.change.fields |= USERMOD_FIELD_HOME_DRIVE;
157 fldname = "home_drive";
161 continue_if_field_set(mod->in.change.comment);
162 mod->in.change.comment = talloc_asprintf(mem_ctx, TEST_CHG_COMMENT,
164 mod->in.change.fields |= USERMOD_FIELD_COMMENT;
169 continue_if_field_set(mod->in.change.logon_script);
170 logonscript = logon_scripts[random() % (sizeof(logon_scripts)/sizeof(char*))];
171 mod->in.change.logon_script = talloc_strdup(mem_ctx, logonscript);
172 mod->in.change.fields |= USERMOD_FIELD_LOGON_SCRIPT;
173 fldname = "logon_script";
177 continue_if_field_set(mod->in.change.profile_path);
178 mod->in.change.profile_path = talloc_asprintf(mem_ctx, TEST_CHG_PROFILEPATH,
179 (long int)random(), (unsigned int)random());
180 mod->in.change.fields |= USERMOD_FIELD_PROFILE_PATH;
181 fldname = "profile_path";
185 continue_if_field_set(mod->in.change.acct_expiry);
186 now = timeval_add(&now, (random() % (31*24*60*60)), 0);
187 mod->in.change.acct_expiry = (struct timeval *)talloc_memdup(mem_ctx, &now, sizeof(now));
188 mod->in.change.fields |= USERMOD_FIELD_ACCT_EXPIRY;
189 fldname = "acct_expiry";
193 continue_if_field_set(mod->in.change.acct_flags);
194 mod->in.change.acct_flags = flags[random() % ARRAY_SIZE(flags)];
195 mod->in.change.fields |= USERMOD_FIELD_ACCT_FLAGS;
196 fldname = "acct_flags";
200 fldname = talloc_asprintf(mem_ctx, "unknown_field (%d)", testfld);
204 printf(((i < num_changes - 1) ? "%s," : "%s"), fldname);
206 torture_comment(tctx, "]\n");
208 status = libnet_rpc_usermod(p, mem_ctx, mod);
209 torture_assert_ntstatus_ok(tctx, status, "Failed to call sync libnet_rpc_usermod");
215 static bool test_userdel(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
216 struct policy_handle *handle, const char *username)
219 struct libnet_rpc_userdel user;
221 user.in.domain_handle = *handle;
222 user.in.username = username;
224 status = libnet_rpc_userdel(p, mem_ctx, &user);
225 if (!NT_STATUS_IS_OK(status)) {
226 printf("Failed to call sync libnet_rpc_userdel - %s\n", nt_errstr(status));
234 #define CMP_LSA_STRING_FLD(fld, flags) \
235 if ((mod->in.change.fields & flags) && \
236 !strequal(i->fld.string, mod->in.change.fld)) { \
237 printf("'%s' field does not match\n", #fld); \
238 printf("received: '%s'\n", i->fld.string); \
239 printf("expected: '%s'\n", mod->in.change.fld); \
244 #define CMP_TIME_FLD(fld, flags) \
245 if (mod->in.change.fields & flags) { \
246 nttime_to_timeval(&t, i->fld); \
247 if (timeval_compare(&t, mod->in.change.fld)) { \
248 printf("'%s' field does not match\n", #fld); \
249 printf("received: '%s (+%ld us)'\n", \
250 timestring(mem_ctx, t.tv_sec), t.tv_usec); \
251 printf("expected: '%s (+%ld us)'\n", \
252 timestring(mem_ctx, mod->in.change.fld->tv_sec), \
253 mod->in.change.fld->tv_usec); \
258 #define CMP_NUM_FLD(fld, flags) \
259 if ((mod->in.change.fields & flags) && \
260 (i->fld != mod->in.change.fld)) { \
261 printf("'%s' field does not match\n", #fld); \
262 printf("received: '%04x'\n", i->fld); \
263 printf("expected: '%04x'\n", mod->in.change.fld); \
268 static bool test_compare(struct torture_context *tctx,
269 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 torture_assert_ntstatus_ok(tctx, status, "Failed to call sync libnet_rpc_userinfo");
287 i = &info.out.info.info21;
289 CMP_LSA_STRING_FLD(account_name, USERMOD_FIELD_ACCOUNT_NAME);
290 CMP_LSA_STRING_FLD(full_name, USERMOD_FIELD_FULL_NAME);
291 CMP_LSA_STRING_FLD(description, USERMOD_FIELD_DESCRIPTION);
292 CMP_LSA_STRING_FLD(comment, USERMOD_FIELD_COMMENT);
293 CMP_LSA_STRING_FLD(logon_script, USERMOD_FIELD_LOGON_SCRIPT);
294 CMP_LSA_STRING_FLD(profile_path, USERMOD_FIELD_PROFILE_PATH);
295 CMP_LSA_STRING_FLD(home_directory, USERMOD_FIELD_HOME_DIRECTORY);
296 CMP_LSA_STRING_FLD(home_drive, USERMOD_FIELD_HOME_DRIVE);
297 CMP_TIME_FLD(acct_expiry, USERMOD_FIELD_ACCT_EXPIRY);
298 CMP_NUM_FLD(acct_flags, USERMOD_FIELD_ACCT_FLAGS)
304 bool torture_useradd(struct torture_context *torture)
307 struct dcerpc_pipe *p;
308 struct policy_handle h;
309 struct lsa_String domain_name;
311 const char *name = TEST_USERNAME;
315 mem_ctx = talloc_init("test_useradd");
317 status = torture_rpc_connection(torture,
321 torture_assert_ntstatus_ok(torture, status, "RPC connect failed");
323 domain_name.string = lp_workgroup(torture->lp_ctx);
324 if (!test_opendomain(torture, p, mem_ctx, &h, &domain_name, &sid)) {
329 if (!test_useradd(p, mem_ctx, &h, name)) {
334 if (!test_user_cleanup(torture, p, mem_ctx, &h, name)) {
339 if (!test_opendomain(torture, p, mem_ctx, &h, &domain_name, &sid)) {
344 if (!test_useradd_async(p, mem_ctx, &h, name)) {
349 if (!test_user_cleanup(torture, p, mem_ctx, &h, name)) {
355 talloc_free(mem_ctx);
360 bool torture_userdel(struct torture_context *torture)
363 struct dcerpc_pipe *p;
364 struct policy_handle h;
365 struct lsa_String domain_name;
368 const char *name = TEST_USERNAME;
372 mem_ctx = talloc_init("test_userdel");
374 status = torture_rpc_connection(torture,
378 if (!NT_STATUS_IS_OK(status)) {
382 domain_name.string = lp_workgroup(torture->lp_ctx);
383 if (!test_opendomain(torture, p, mem_ctx, &h, &domain_name, &sid)) {
388 if (!test_user_create(torture, p, mem_ctx, &h, name, &rid)) {
393 if (!test_userdel(p, mem_ctx, &h, name)) {
399 talloc_free(mem_ctx);
404 bool torture_usermod(struct torture_context *torture)
407 struct dcerpc_pipe *p;
408 struct policy_handle h;
409 struct lsa_String domain_name;
417 mem_ctx = talloc_init("test_userdel");
419 status = torture_rpc_connection(torture,
423 torture_assert_ntstatus_ok(torture, status, "RPC connect");
425 domain_name.string = lp_workgroup(torture->lp_ctx);
426 name = talloc_strdup(mem_ctx, TEST_USERNAME);
428 if (!test_opendomain(torture, p, mem_ctx, &h, &domain_name, &sid)) {
433 if (!test_user_create(torture, p, mem_ctx, &h, name, &rid)) {
438 for (i = 1; i < FIELDS_NUM; i++) {
439 struct libnet_rpc_usermod m;
441 if (!test_usermod(torture, p, mem_ctx, &h, i, &m, &name)) {
446 if (!test_compare(torture, p, mem_ctx, &h, &m, name)) {
453 if (!test_user_cleanup(torture, p, mem_ctx, &h, name)) {
459 talloc_free(mem_ctx);