s4-loadparm: 2nd half of lp_ to lpcfg_ conversion
[samba.git] / source4 / torture / libnet / libnet_user.c
1 /*
2    Unix SMB/CIFS implementation.
3    Test suite for libnet calls.
4
5    Copyright (C) Rafal Szczesniak 2005
6
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.
11
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.
16
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/>.
19 */
20
21 #include "includes.h"
22 #include "system/time.h"
23 #include "lib/cmdline/popt_common.h"
24 #include "libnet/libnet.h"
25 #include "librpc/gen_ndr/ndr_samr_c.h"
26 #include "librpc/gen_ndr/ndr_lsa_c.h"
27 #include "torture/rpc/torture_rpc.h"
28 #include "torture/libnet/usertest.h"
29 #include "torture/libnet/proto.h"
30 #include "param/param.h"
31
32
33
34 bool torture_createuser(struct torture_context *torture)
35 {
36         NTSTATUS status;
37         TALLOC_CTX *mem_ctx;
38         struct libnet_context *ctx = NULL;
39         struct libnet_CreateUser req;
40         bool ret = true;
41
42         mem_ctx = talloc_init("test_createuser");
43
44         if (!test_libnet_context_init(torture, true, &ctx)) {
45                 return false;
46         }
47
48         req.in.user_name = TEST_USERNAME;
49         req.in.domain_name = lpcfg_workgroup(torture->lp_ctx);
50         req.out.error_string = NULL;
51
52         status = libnet_CreateUser(ctx, mem_ctx, &req);
53         if (!NT_STATUS_IS_OK(status)) {
54                 torture_comment(torture, "libnet_CreateUser call failed: %s\n", nt_errstr(status));
55                 ret = false;
56                 goto done;
57         }
58
59         if (!test_user_cleanup(torture, ctx->samr.pipe->binding_handle,
60                                mem_ctx, &ctx->samr.handle, TEST_USERNAME)) {
61                 torture_comment(torture, "cleanup failed\n");
62                 ret = false;
63                 goto done;
64         }
65
66         if (!test_samr_close_handle(torture,
67                                     ctx->samr.pipe->binding_handle, mem_ctx, &ctx->samr.handle)) {
68                 torture_comment(torture, "domain close failed\n");
69                 ret = false;
70         }
71
72 done:
73         talloc_free(ctx);
74         talloc_free(mem_ctx);
75         return ret;
76 }
77
78
79 bool torture_deleteuser(struct torture_context *torture)
80 {
81         NTSTATUS status;
82         struct dcerpc_pipe *p;
83         TALLOC_CTX *prep_mem_ctx, *mem_ctx;
84         struct policy_handle h;
85         struct lsa_String domain_name;
86         const char *name = TEST_USERNAME;
87         struct libnet_context *ctx = NULL;
88         struct libnet_DeleteUser req;
89         bool ret = true;
90
91         prep_mem_ctx = talloc_init("prepare test_deleteuser");
92
93         req.in.user_name = TEST_USERNAME;
94         req.in.domain_name = lpcfg_workgroup(torture->lp_ctx);
95
96         status = torture_rpc_connection(torture,
97                                         &p,
98                                         &ndr_table_samr);
99         if (!NT_STATUS_IS_OK(status)) {
100                 ret = false;
101                 goto done;
102         }
103
104         domain_name.string = lpcfg_workgroup(torture->lp_ctx);
105         if (!test_domain_open(torture, p->binding_handle, &domain_name, prep_mem_ctx, &h, NULL)) {
106                 ret = false;
107                 goto done;
108         }
109
110         if (!test_user_create(torture, p->binding_handle, prep_mem_ctx, &h, name, NULL)) {
111                 ret = false;
112                 goto done;
113         }
114
115         mem_ctx = talloc_init("test_deleteuser");
116
117         if (!test_libnet_context_init(torture, true, &ctx)) {
118                 return false;
119         }
120
121         status = libnet_DeleteUser(ctx, mem_ctx, &req);
122         if (!NT_STATUS_IS_OK(status)) {
123                 torture_comment(torture, "libnet_DeleteUser call failed: %s\n", nt_errstr(status));
124                 ret = false;
125         }
126
127         talloc_free(mem_ctx);
128
129 done:
130         talloc_free(ctx);
131         talloc_free(prep_mem_ctx);
132         return ret;
133 }
134
135
136 /*
137   Generate testing set of random changes
138 */
139
140 static void set_test_changes(struct torture_context *tctx,
141                              TALLOC_CTX *mem_ctx, struct libnet_ModifyUser *r,
142                              int num_changes, char **user_name, enum test_fields req_change)
143 {
144         const char* logon_scripts[] = { "start_login.cmd", "login.bat", "start.cmd" };
145         const char* home_dirs[] = { "\\\\srv\\home", "\\\\homesrv\\home\\user", "\\\\pdcsrv\\domain" };
146         const char* home_drives[] = { "H:", "z:", "I:", "J:", "n:" };
147         const uint32_t flags[] = { (ACB_DISABLED | ACB_NORMAL | ACB_PW_EXPIRED),
148                                    (ACB_NORMAL | ACB_PWNOEXP),
149                                    (ACB_NORMAL | ACB_PW_EXPIRED) };
150         const char *homedir, *homedrive, *logonscript;
151         struct timeval now;
152         int i, testfld;
153
154         torture_comment(tctx, "Fields to change: [");
155
156         for (i = 0; i < num_changes && i <= USER_FIELD_LAST; i++) {
157                 const char *fldname;
158
159                 testfld = (req_change == none) ? (random() % USER_FIELD_LAST) + 1 : req_change;
160
161                 /* get one in case we hit time field this time */
162                 gettimeofday(&now, NULL);
163
164                 switch (testfld) {
165                 case acct_name:
166                         continue_if_field_set(r->in.account_name);
167                         r->in.account_name = talloc_asprintf(mem_ctx, TEST_CHG_ACCOUNTNAME,
168                                                              (int)(random() % 100));
169                         fldname = "account_name";
170
171                         /* update the test's user name in case it's about to change */
172                         *user_name = talloc_strdup(mem_ctx, r->in.account_name);
173                         break;
174
175                 case acct_full_name:
176                         continue_if_field_set(r->in.full_name);
177                         r->in.full_name = talloc_asprintf(mem_ctx, TEST_CHG_FULLNAME,
178                                                           (unsigned int)random(), (unsigned int)random());
179                         fldname = "full_name";
180                         break;
181
182                 case acct_description:
183                         continue_if_field_set(r->in.description);
184                         r->in.description = talloc_asprintf(mem_ctx, TEST_CHG_DESCRIPTION,
185                                                             (long)random());
186                         fldname = "description";
187                         break;
188
189                 case acct_home_directory:
190                         continue_if_field_set(r->in.home_directory);
191                         homedir = home_dirs[random() % ARRAY_SIZE(home_dirs)];
192                         r->in.home_directory = talloc_strdup(mem_ctx, homedir);
193                         fldname = "home_dir";
194                         break;
195
196                 case acct_home_drive:
197                         continue_if_field_set(r->in.home_drive);
198                         homedrive = home_drives[random() % ARRAY_SIZE(home_drives)];
199                         r->in.home_drive = talloc_strdup(mem_ctx, homedrive);
200                         fldname = "home_drive";
201                         break;
202
203                 case acct_comment:
204                         continue_if_field_set(r->in.comment);
205                         r->in.comment = talloc_asprintf(mem_ctx, TEST_CHG_COMMENT,
206                                                         (unsigned long)random(), (unsigned long)random());
207                         fldname = "comment";
208                         break;
209
210                 case acct_logon_script:
211                         continue_if_field_set(r->in.logon_script);
212                         logonscript = logon_scripts[random() % ARRAY_SIZE(logon_scripts)];
213                         r->in.logon_script = talloc_strdup(mem_ctx, logonscript);
214                         fldname = "logon_script";
215                         break;
216
217                 case acct_profile_path:
218                         continue_if_field_set(r->in.profile_path);
219                         r->in.profile_path = talloc_asprintf(mem_ctx, TEST_CHG_PROFILEPATH,
220                                                              (unsigned long)random(), (unsigned int)random());
221                         fldname = "profile_path";
222                         break;
223
224                 case acct_expiry:
225                         continue_if_field_set(r->in.acct_expiry);
226                         now = timeval_add(&now, (random() % (31*24*60*60)), 0);
227                         r->in.acct_expiry = (struct timeval *)talloc_memdup(mem_ctx, &now, sizeof(now));
228                         fldname = "acct_expiry";
229                         break;
230
231                 case acct_flags:
232                         continue_if_field_set(r->in.acct_flags);
233                         r->in.acct_flags = flags[random() % ARRAY_SIZE(flags)];
234                         fldname = "acct_flags";
235                         break;
236
237                 default:
238                         fldname = "unknown_field";
239                 }
240
241                 torture_comment(tctx, ((i < num_changes - 1) ? "%s," : "%s"), fldname);
242
243                 /* disable requested field (it's supposed to be the only one used) */
244                 if (req_change != none) req_change = none;
245         }
246
247         torture_comment(tctx, "]\n");
248 }
249
250
251 #define TEST_STR_FLD(fld) \
252         if (!strequal(req.in.fld, user_req.out.fld)) { \
253                 torture_comment(torture, "failed to change '%s'\n", #fld); \
254                 ret = false; \
255                 goto cleanup; \
256         }
257
258 #define TEST_TIME_FLD(fld) \
259         if (timeval_compare(req.in.fld, user_req.out.fld)) { \
260                 torture_comment(torture, "failed to change '%s'\n", #fld); \
261                 ret = false; \
262                 goto cleanup; \
263         }
264
265 #define TEST_NUM_FLD(fld) \
266         if (req.in.fld != user_req.out.fld) { \
267                 torture_comment(torture, "failed to change '%s'\n", #fld); \
268                 ret = false; \
269                 goto cleanup; \
270         }
271
272
273 bool torture_modifyuser(struct torture_context *torture)
274 {
275         NTSTATUS status;
276         struct dcerpc_pipe *p;
277         TALLOC_CTX *prep_mem_ctx;
278         struct policy_handle h;
279         struct lsa_String domain_name;
280         char *name;
281         struct libnet_context *ctx = NULL;
282         struct libnet_ModifyUser req;
283         struct libnet_UserInfo user_req;
284         int fld;
285         bool ret = true;
286         struct dcerpc_binding_handle *b;
287
288         prep_mem_ctx = talloc_init("prepare test_deleteuser");
289
290         status = torture_rpc_connection(torture,
291                                         &p,
292                                         &ndr_table_samr);
293         if (!NT_STATUS_IS_OK(status)) {
294                 ret = false;
295                 goto done;
296         }
297         b = p->binding_handle;
298
299         name = talloc_strdup(prep_mem_ctx, TEST_USERNAME);
300
301         domain_name.string = lpcfg_workgroup(torture->lp_ctx);
302         if (!test_domain_open(torture, b, &domain_name, prep_mem_ctx, &h, NULL)) {
303                 ret = false;
304                 goto done;
305         }
306
307         if (!test_user_create(torture, b, prep_mem_ctx, &h, name, NULL)) {
308                 ret = false;
309                 goto done;
310         }
311
312         torture_comment(torture, "Testing change of all fields - each single one in turn\n");
313
314         if (!test_libnet_context_init(torture, true, &ctx)) {
315                 return false;
316         }
317
318         for (fld = USER_FIELD_FIRST; fld <= USER_FIELD_LAST; fld++) {
319                 ZERO_STRUCT(req);
320                 req.in.domain_name = lpcfg_workgroup(torture->lp_ctx);
321                 req.in.user_name = name;
322
323                 set_test_changes(torture, torture, &req, 1, &name, fld);
324
325                 status = libnet_ModifyUser(ctx, torture, &req);
326                 if (!NT_STATUS_IS_OK(status)) {
327                         torture_comment(torture, "libnet_ModifyUser call failed: %s\n", nt_errstr(status));
328                         ret = false;
329                         continue;
330                 }
331
332                 ZERO_STRUCT(user_req);
333                 user_req.in.domain_name = lpcfg_workgroup(torture->lp_ctx);
334                 user_req.in.data.user_name = name;
335                 user_req.in.level = USER_INFO_BY_NAME;
336
337                 status = libnet_UserInfo(ctx, torture, &user_req);
338                 if (!NT_STATUS_IS_OK(status)) {
339                         torture_comment(torture, "libnet_UserInfo call failed: %s\n", nt_errstr(status));
340                         ret = false;
341                         continue;
342                 }
343
344                 switch (fld) {
345                 case acct_name: TEST_STR_FLD(account_name);
346                         break;
347                 case acct_full_name: TEST_STR_FLD(full_name);
348                         break;
349                 case acct_comment: TEST_STR_FLD(comment);
350                         break;
351                 case acct_description: TEST_STR_FLD(description);
352                         break;
353                 case acct_home_directory: TEST_STR_FLD(home_directory);
354                         break;
355                 case acct_home_drive: TEST_STR_FLD(home_drive);
356                         break;
357                 case acct_logon_script: TEST_STR_FLD(logon_script);
358                         break;
359                 case acct_profile_path: TEST_STR_FLD(profile_path);
360                         break;
361                 case acct_expiry: TEST_TIME_FLD(acct_expiry);
362                         break;
363                 case acct_flags: TEST_NUM_FLD(acct_flags);
364                         break;
365                 default:
366                         break;
367                 }
368         }
369
370 cleanup:
371         if (!test_user_cleanup(torture, ctx->samr.pipe->binding_handle,
372                                torture, &ctx->samr.handle, TEST_USERNAME)) {
373                 torture_comment(torture, "cleanup failed\n");
374                 ret = false;
375                 goto done;
376         }
377
378         if (!test_samr_close_handle(torture,
379                                     ctx->samr.pipe->binding_handle, torture, &ctx->samr.handle)) {
380                 torture_comment(torture, "domain close failed\n");
381                 ret = false;
382         }
383
384 done:
385         talloc_free(ctx);
386         talloc_free(prep_mem_ctx);
387         return ret;
388 }
389
390
391 bool torture_userinfo_api(struct torture_context *torture)
392 {
393         const char *name = TEST_USERNAME;
394         bool ret = true;
395         NTSTATUS status;
396         TALLOC_CTX *mem_ctx = NULL, *prep_mem_ctx;
397         struct libnet_context *ctx = NULL;
398         struct dcerpc_pipe *p;
399         struct policy_handle h;
400         struct lsa_String domain_name;
401         struct libnet_UserInfo req;
402         struct dcerpc_binding_handle *b;
403
404         prep_mem_ctx = talloc_init("prepare torture user info");
405
406         status = torture_rpc_connection(torture,
407                                         &p,
408                                         &ndr_table_samr);
409         if (!NT_STATUS_IS_OK(status)) {
410                 return false;
411         }
412         b = p->binding_handle;
413
414         domain_name.string = lpcfg_workgroup(torture->lp_ctx);
415         if (!test_domain_open(torture, b, &domain_name, prep_mem_ctx, &h, NULL)) {
416                 ret = false;
417                 goto done;
418         }
419
420         if (!test_user_create(torture, b, prep_mem_ctx, &h, name, NULL)) {
421                 ret = false;
422                 goto done;
423         }
424
425         mem_ctx = talloc_init("torture user info");
426
427         if (!test_libnet_context_init(torture, true, &ctx)) {
428                 return false;
429         }
430
431         ZERO_STRUCT(req);
432
433         req.in.domain_name = domain_name.string;
434         req.in.data.user_name   = name;
435         req.in.level = USER_INFO_BY_NAME;
436
437         status = libnet_UserInfo(ctx, mem_ctx, &req);
438         if (!NT_STATUS_IS_OK(status)) {
439                 torture_comment(torture, "libnet_UserInfo call failed: %s\n", nt_errstr(status));
440                 ret = false;
441                 goto done;
442         }
443
444         if (!test_user_cleanup(torture, ctx->samr.pipe->binding_handle,
445                                mem_ctx, &ctx->samr.handle, TEST_USERNAME)) {
446                 torture_comment(torture, "cleanup failed\n");
447                 ret = false;
448                 goto done;
449         }
450
451         if (!test_samr_close_handle(torture,
452                                     ctx->samr.pipe->binding_handle, mem_ctx, &ctx->samr.handle)) {
453                 torture_comment(torture, "domain close failed\n");
454                 ret = false;
455         }
456
457 done:
458         talloc_free(ctx);
459         talloc_free(mem_ctx);
460         return ret;
461 }
462
463
464 bool torture_userlist(struct torture_context *torture)
465 {
466         bool ret = true;
467         NTSTATUS status;
468         TALLOC_CTX *mem_ctx = NULL;
469         struct libnet_context *ctx;
470         struct lsa_String domain_name;
471         struct libnet_UserList req;
472         int i;
473
474         ctx = libnet_context_init(torture->ev, torture->lp_ctx);
475         ctx->cred = cmdline_credentials;
476
477         domain_name.string = lpcfg_workgroup(torture->lp_ctx);
478         mem_ctx = talloc_init("torture user list");
479
480         ZERO_STRUCT(req);
481
482         torture_comment(torture, "listing user accounts:\n");
483
484         do {
485
486                 req.in.domain_name = domain_name.string;
487                 req.in.page_size   = 128;
488                 req.in.resume_index = req.out.resume_index;
489
490                 status = libnet_UserList(ctx, mem_ctx, &req);
491                 if (!NT_STATUS_IS_OK(status) &&
492                     !NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) break;
493
494                 for (i = 0; i < req.out.count; i++) {
495                         torture_comment(torture, "\tuser: %s, sid=%s\n",
496                                         req.out.users[i].username, req.out.users[i].sid);
497                 }
498
499         } while (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES));
500
501         if (!(NT_STATUS_IS_OK(status) ||
502               NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES))) {
503                 torture_comment(torture, "libnet_UserList call failed: %s\n", nt_errstr(status));
504                 ret = false;
505                 goto done;
506         }
507
508         if (!test_samr_close_handle(torture,
509                                     ctx->samr.pipe->binding_handle, mem_ctx, &ctx->samr.handle)) {
510                 torture_comment(torture, "samr domain close failed\n");
511                 ret = false;
512                 goto done;
513         }
514
515         if (!test_lsa_close_handle(torture,
516                                    ctx->lsa.pipe->binding_handle, mem_ctx, &ctx->lsa.handle)) {
517                 torture_comment(torture, "lsa domain close failed\n");
518                 ret = false;
519         }
520
521         talloc_free(ctx);
522
523 done:
524         talloc_free(mem_ctx);
525         return ret;
526 }