r19592: a simple test for libnet_UserList
[ira/wip.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 2 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, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23 #include "system/time.h"
24 #include "lib/cmdline/popt_common.h"
25 #include "libnet/libnet.h"
26 #include "librpc/gen_ndr/ndr_samr_c.h"
27 #include "librpc/gen_ndr/ndr_lsa_c.h"
28 #include "torture/torture.h"
29 #include "torture/rpc/rpc.h"
30 #include "torture/libnet/usertest.h"
31
32
33 static BOOL test_cleanup(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
34                          struct policy_handle *domain_handle, const char *username)
35 {
36         NTSTATUS status;
37         struct samr_LookupNames r1;
38         struct samr_OpenUser r2;
39         struct samr_DeleteUser r3;
40         struct lsa_String names[2];
41         uint32_t rid;
42         struct policy_handle user_handle;
43
44         names[0].string = username;
45
46         r1.in.domain_handle  = domain_handle;
47         r1.in.num_names      = 1;
48         r1.in.names          = names;
49         
50         printf("user account lookup '%s'\n", username);
51
52         status = dcerpc_samr_LookupNames(p, mem_ctx, &r1);
53         if (!NT_STATUS_IS_OK(status)) {
54                 printf("LookupNames failed - %s\n", nt_errstr(status));
55                 return False;
56         }
57
58         rid = r1.out.rids.ids[0];
59         
60         r2.in.domain_handle  = domain_handle;
61         r2.in.access_mask    = SEC_FLAG_MAXIMUM_ALLOWED;
62         r2.in.rid            = rid;
63         r2.out.user_handle   = &user_handle;
64
65         printf("opening user account\n");
66
67         status = dcerpc_samr_OpenUser(p, mem_ctx, &r2);
68         if (!NT_STATUS_IS_OK(status)) {
69                 printf("OpenUser failed - %s\n", nt_errstr(status));
70                 return False;
71         }
72
73         r3.in.user_handle  = &user_handle;
74         r3.out.user_handle = &user_handle;
75
76         printf("deleting user account\n");
77         
78         status = dcerpc_samr_DeleteUser(p, mem_ctx, &r3);
79         if (!NT_STATUS_IS_OK(status)) {
80                 printf("DeleteUser failed - %s\n", nt_errstr(status));
81                 return False;
82         }
83
84         return True;
85 }
86
87
88 static BOOL test_opendomain(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
89                             struct policy_handle *handle, struct lsa_String *domname)
90 {
91         NTSTATUS status;
92         struct policy_handle h, domain_handle;
93         struct samr_Connect r1;
94         struct samr_LookupDomain r2;
95         struct samr_OpenDomain r3;
96         
97         printf("connecting\n");
98         
99         r1.in.system_name = 0;
100         r1.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
101         r1.out.connect_handle = &h;
102         
103         status = dcerpc_samr_Connect(p, mem_ctx, &r1);
104         if (!NT_STATUS_IS_OK(status)) {
105                 printf("Connect failed - %s\n", nt_errstr(status));
106                 return False;
107         }
108         
109         r2.in.connect_handle = &h;
110         r2.in.domain_name = domname;
111
112         printf("domain lookup on %s\n", domname->string);
113
114         status = dcerpc_samr_LookupDomain(p, mem_ctx, &r2);
115         if (!NT_STATUS_IS_OK(status)) {
116                 printf("LookupDomain failed - %s\n", nt_errstr(status));
117                 return False;
118         }
119
120         r3.in.connect_handle = &h;
121         r3.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
122         r3.in.sid = r2.out.sid;
123         r3.out.domain_handle = &domain_handle;
124
125         printf("opening domain\n");
126
127         status = dcerpc_samr_OpenDomain(p, mem_ctx, &r3);
128         if (!NT_STATUS_IS_OK(status)) {
129                 printf("OpenDomain failed - %s\n", nt_errstr(status));
130                 return False;
131         } else {
132                 *handle = domain_handle;
133         }
134
135         return True;
136 }
137
138
139 static BOOL test_samr_close(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
140                             struct policy_handle *domain_handle)
141 {
142         NTSTATUS status;
143         struct samr_Close r;
144   
145         r.in.handle = domain_handle;
146         r.out.handle = domain_handle;
147
148         status = dcerpc_samr_Close(p, mem_ctx, &r);
149         if (!NT_STATUS_IS_OK(status)) {
150                 printf("Close samr domain failed - %s\n", nt_errstr(status));
151                 return False;
152         }
153         
154         return True;
155 }
156
157
158 static BOOL test_lsa_close(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
159                            struct policy_handle *domain_handle)
160 {
161         NTSTATUS status;
162         struct lsa_Close r;
163
164         r.in.handle = domain_handle;
165         r.out.handle = domain_handle;
166         
167         status = dcerpc_lsa_Close(p, mem_ctx, &r);
168         if (!NT_STATUS_IS_OK(status)) {
169                 printf("Close lsa domain failed - %s\n", nt_errstr(status));
170                 return False;
171         }
172
173         return True;
174 }
175
176
177 static BOOL test_createuser(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
178                             struct policy_handle *handle, const char* user)
179 {
180         NTSTATUS status;
181         struct policy_handle user_handle;
182         struct lsa_String username;
183         struct samr_CreateUser r1;
184         struct samr_Close r2;
185         uint32_t user_rid;
186
187         username.string = user;
188         
189         r1.in.domain_handle = handle;
190         r1.in.account_name = &username;
191         r1.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
192         r1.out.user_handle = &user_handle;
193         r1.out.rid = &user_rid;
194
195         printf("creating user '%s'\n", username.string);
196         
197         status = dcerpc_samr_CreateUser(p, mem_ctx, &r1);
198         if (!NT_STATUS_IS_OK(status)) {
199                 printf("CreateUser failed - %s\n", nt_errstr(status));
200
201                 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
202                         printf("User (%s) already exists - attempting to delete and recreate account again\n", user);
203                         if (!test_cleanup(p, mem_ctx, handle, TEST_USERNAME)) {
204                                 return False;
205                         }
206
207                         printf("creating user account\n");
208                         
209                         status = dcerpc_samr_CreateUser(p, mem_ctx, &r1);
210                         if (!NT_STATUS_IS_OK(status)) {
211                                 printf("CreateUser failed - %s\n", nt_errstr(status));
212                                 return False;
213                         }
214                         return True;
215                 }               
216                 return False;
217         }
218
219         r2.in.handle = &user_handle;
220         r2.out.handle = &user_handle;
221         
222         printf("closing user '%s'\n", username.string);
223
224         status = dcerpc_samr_Close(p, mem_ctx, &r2);
225         if (!NT_STATUS_IS_OK(status)) {
226                 printf("Close failed - %s\n", nt_errstr(status));
227                 return False;
228         }
229
230         return True;
231 }
232
233
234 BOOL torture_createuser(struct torture_context *torture)
235 {
236         NTSTATUS status;
237         const char *binding;
238         TALLOC_CTX *mem_ctx;
239         struct libnet_context *ctx;
240         struct libnet_CreateUser req;
241         BOOL ret = True;
242
243         mem_ctx = talloc_init("test_createuser");
244         binding = torture_setting_string(torture, "binding", NULL);
245
246         ctx = libnet_context_init(NULL);
247         ctx->cred = cmdline_credentials;
248
249         req.in.user_name = TEST_USERNAME;
250         req.in.domain_name = lp_workgroup();
251         req.out.error_string = NULL;
252
253         status = libnet_CreateUser(ctx, mem_ctx, &req);
254         if (!NT_STATUS_IS_OK(status)) {
255                 printf("libnet_CreateUser call failed: %s\n", nt_errstr(status));
256                 ret = False;
257                 goto done;
258         }
259
260         if (!test_cleanup(ctx->samr.pipe, mem_ctx, &ctx->samr.handle, TEST_USERNAME)) {
261                 printf("cleanup failed\n");
262                 ret = False;
263                 goto done;
264         }
265
266         if (!test_samr_close(ctx->samr.pipe, mem_ctx, &ctx->samr.handle)) {
267                 printf("domain close failed\n");
268                 ret = False;
269         }
270
271 done:
272         talloc_free(ctx);
273         talloc_free(mem_ctx);
274         return ret;
275 }
276
277
278 BOOL torture_deleteuser(struct torture_context *torture)
279 {
280         NTSTATUS status;
281         const char *binding;
282         struct dcerpc_pipe *p;
283         TALLOC_CTX *prep_mem_ctx, *mem_ctx;
284         struct policy_handle h;
285         struct lsa_String domain_name;
286         const char *name = TEST_USERNAME;
287         struct libnet_context *ctx;
288         struct libnet_DeleteUser req;
289         BOOL ret = True;
290
291         prep_mem_ctx = talloc_init("prepare test_deleteuser");
292         binding = torture_setting_string(torture, "binding", NULL);
293
294         ctx = libnet_context_init(NULL);
295         ctx->cred = cmdline_credentials;
296
297         req.in.user_name = TEST_USERNAME;
298         req.in.domain_name = lp_workgroup();
299
300         status = torture_rpc_connection(prep_mem_ctx,
301                                         &p,
302                                         &dcerpc_table_samr);
303         if (!NT_STATUS_IS_OK(status)) {
304                 ret = False;
305                 goto done;
306         }
307
308         domain_name.string = lp_workgroup();
309         if (!test_opendomain(p, prep_mem_ctx, &h, &domain_name)) {
310                 ret = False;
311                 goto done;
312         }
313
314         if (!test_createuser(p, prep_mem_ctx, &h, name)) {
315                 ret = False;
316                 goto done;
317         }
318
319         mem_ctx = talloc_init("test_deleteuser");
320
321         status = libnet_DeleteUser(ctx, mem_ctx, &req);
322         if (!NT_STATUS_IS_OK(status)) {
323                 printf("libnet_DeleteUser call failed: %s\n", nt_errstr(status));
324                 ret = False;
325         }
326
327         talloc_free(mem_ctx);
328
329 done:
330         talloc_free(ctx);
331         talloc_free(prep_mem_ctx);
332         return ret;
333 }
334
335
336 /*
337   Generate testing set of random changes
338 */
339
340 static void set_test_changes(TALLOC_CTX *mem_ctx, struct libnet_ModifyUser *r,
341                              int num_changes, char **user_name, enum test_fields req_change)
342 {
343         const char* logon_scripts[] = { "start_login.cmd", "login.bat", "start.cmd" };
344         const char* home_dirs[] = { "\\\\srv\\home", "\\\\homesrv\\home\\user", "\\\\pdcsrv\\domain" };
345         const char* home_drives[] = { "H:", "z:", "I:", "J:", "n:" };
346         const char *homedir, *homedrive, *logonscript;
347         struct timeval now;
348         int i, testfld;
349
350         srandom((unsigned)time(NULL));
351
352         printf("Fields to change: [");
353
354         for (i = 0; i < num_changes && i < FIELDS_NUM; i++) {
355                 const char *fldname;
356
357                 testfld = (req_change == none) ? (random() % FIELDS_NUM) : req_change;
358
359                 /* get one in case we hit time field this time */
360                 gettimeofday(&now, NULL);
361                 
362                 switch (testfld) {
363                 case account_name:
364                         continue_if_field_set(r->in.account_name);
365                         r->in.account_name = talloc_asprintf(mem_ctx, TEST_CHG_ACCOUNTNAME,
366                                                              (int)(random() % 100));
367                         fldname = "account_name";
368                         
369                         /* update the test's user name in case it's about to change */
370                         *user_name = talloc_strdup(mem_ctx, r->in.account_name);
371                         break;
372
373                 case full_name:
374                         continue_if_field_set(r->in.full_name);
375                         r->in.full_name = talloc_asprintf(mem_ctx, TEST_CHG_FULLNAME,
376                                                           (unsigned int)random(), (unsigned int)random());
377                         fldname = "full_name";
378                         break;
379
380                 case description:
381                         continue_if_field_set(r->in.description);
382                         r->in.description = talloc_asprintf(mem_ctx, TEST_CHG_DESCRIPTION,
383                                                             (long)random());
384                         fldname = "description";
385                         break;
386
387                 case home_directory:
388                         continue_if_field_set(r->in.home_directory);
389                         homedir = home_dirs[random() % (sizeof(home_dirs)/sizeof(char*))];
390                         r->in.home_directory = talloc_strdup(mem_ctx, homedir);
391                         fldname = "home_dir";
392                         break;
393
394                 case home_drive:
395                         continue_if_field_set(r->in.home_drive);
396                         homedrive = home_drives[random() % (sizeof(home_drives)/sizeof(char*))];
397                         r->in.home_drive = talloc_strdup(mem_ctx, homedrive);
398                         fldname = "home_drive";
399                         break;
400
401                 case comment:
402                         continue_if_field_set(r->in.comment);
403                         r->in.comment = talloc_asprintf(mem_ctx, TEST_CHG_COMMENT,
404                                                         (unsigned long)random(), (unsigned long)random());
405                         fldname = "comment";
406                         break;
407
408                 case logon_script:
409                         continue_if_field_set(r->in.logon_script);
410                         logonscript = logon_scripts[random() % (sizeof(logon_scripts)/sizeof(char*))];
411                         r->in.logon_script = talloc_strdup(mem_ctx, logonscript);
412                         fldname = "logon_script";
413                         break;
414                         
415                 case profile_path:
416                         continue_if_field_set(r->in.profile_path);
417                         r->in.profile_path = talloc_asprintf(mem_ctx, TEST_CHG_PROFILEPATH,
418                                                              (unsigned long)random(), (unsigned int)random());
419                         fldname = "profile_path";
420                         break;
421
422                 case acct_expiry:
423                         continue_if_field_set(r->in.acct_expiry);
424                         now = timeval_add(&now, (random() % (31*24*60*60)), 0);
425                         r->in.acct_expiry = talloc_memdup(mem_ctx, &now, sizeof(now));
426                         fldname = "acct_expiry";
427                         break;
428
429                 default:
430                         fldname = "unknown_field";
431                 }
432                 
433                 printf(((i < num_changes - 1) ? "%s," : "%s"), fldname);
434
435                 /* disable requested field (it's supposed to be the only one used) */
436                 if (req_change != none) req_change = none;
437         }
438
439         printf("]\n");
440 }
441
442
443 #define TEST_STR_FLD(fld) \
444         if (!strequal(req.in.fld, user_req.out.fld)) { \
445                 printf("failed to change '%s'\n", #fld); \
446                 ret = False; \
447                 goto cleanup; \
448         }
449
450 #define TEST_TIME_FLD(fld) \
451         if (timeval_compare(req.in.fld, user_req.out.fld)) { \
452                 printf("failed to change '%s'\n", #fld); \
453                 ret = False; \
454                 goto cleanup; \
455         }
456
457 #define TEST_NUM_FLD(fld) \
458         if (req.in.fld != user_req.out.fld) { \
459                 printf("failed to change '%s'\n", #fld); \
460                 ret = False; \
461                 goto cleanup; \
462         }
463
464
465 BOOL torture_modifyuser(struct torture_context *torture)
466 {
467         NTSTATUS status;
468         const char *binding;
469         struct dcerpc_binding *bind;
470         struct dcerpc_pipe *p;
471         TALLOC_CTX *prep_mem_ctx, *mem_ctx;
472         struct policy_handle h;
473         struct lsa_String domain_name;
474         char *name;
475         struct libnet_context *ctx;
476         struct libnet_ModifyUser req;
477         struct libnet_UserInfo user_req;
478         int fld;
479         BOOL ret = True;
480
481         prep_mem_ctx = talloc_init("prepare test_deleteuser");
482         binding = torture_setting_string(torture, "binding", NULL);
483
484         ctx = libnet_context_init(NULL);
485         ctx->cred = cmdline_credentials;
486
487         status = torture_rpc_connection(prep_mem_ctx,
488                                         &p,
489                                         &dcerpc_table_samr);
490         if (!NT_STATUS_IS_OK(status)) {
491                 ret = False;
492                 goto done;
493         }
494
495         name = talloc_strdup(prep_mem_ctx, TEST_USERNAME);
496
497         domain_name.string = lp_workgroup();
498         if (!test_opendomain(p, prep_mem_ctx, &h, &domain_name)) {
499                 ret = False;
500                 goto done;
501         }
502
503         if (!test_createuser(p, prep_mem_ctx, &h, name)) {
504                 ret = False;
505                 goto done;
506         }
507
508         mem_ctx = talloc_init("test_modifyuser");
509
510         status = dcerpc_parse_binding(mem_ctx, binding, &bind);
511         if (!NT_STATUS_IS_OK(status)) {
512                 ret = False;
513                 goto done;
514         }
515
516         printf("Testing change of all fields - each single one in turn\n");
517
518         for (fld = 1; fld < FIELDS_NUM - 1; fld++) {
519                 ZERO_STRUCT(req);
520                 req.in.domain_name = lp_workgroup();
521                 req.in.user_name = name;
522
523                 set_test_changes(mem_ctx, &req, 1, &name, fld);
524
525                 status = libnet_ModifyUser(ctx, mem_ctx, &req);
526                 if (!NT_STATUS_IS_OK(status)) {
527                         printf("libnet_ModifyUser call failed: %s\n", nt_errstr(status));
528                         ret = False;
529                         continue;
530                 }
531
532                 ZERO_STRUCT(user_req);
533                 user_req.in.domain_name = lp_workgroup();
534                 user_req.in.user_name = name;
535
536                 status = libnet_UserInfo(ctx, mem_ctx, &user_req);
537                 if (!NT_STATUS_IS_OK(status)) {
538                         printf("libnet_UserInfo call failed: %s\n", nt_errstr(status));
539                         continue;
540                 }
541
542                 switch (fld) {
543                 case account_name: TEST_STR_FLD(account_name);
544                         break;
545                 case full_name: TEST_STR_FLD(full_name);
546                         break;
547                 case comment: TEST_STR_FLD(comment);
548                         break;
549                 case description: TEST_STR_FLD(description);
550                         break;
551                 case home_directory: TEST_STR_FLD(home_directory);
552                         break;
553                 case home_drive: TEST_STR_FLD(home_drive);
554                         break;
555                 case logon_script: TEST_STR_FLD(logon_script);
556                         break;
557                 case profile_path: TEST_STR_FLD(profile_path);
558                         break;
559                 case acct_expiry: TEST_TIME_FLD(acct_expiry);
560                         break;
561                 case acct_flags: TEST_NUM_FLD(acct_flags);
562                         break;
563                 default:
564                         break;
565                 }
566
567                 if (fld == account_name) {
568                         /* restore original testing username - it's useful when test fails
569                            because it prevents from problems with recreating account */
570                         ZERO_STRUCT(req);
571                         req.in.domain_name = lp_workgroup();
572                         req.in.user_name = name;
573                         req.in.account_name = TEST_USERNAME;
574                         
575                         status = libnet_ModifyUser(ctx, mem_ctx, &req);
576                         if (!NT_STATUS_IS_OK(status)) {
577                                 printf("libnet_ModifyUser call failed: %s\n", nt_errstr(status));
578                                 talloc_free(mem_ctx);
579                                 ret = False;
580                                 goto done;
581                         }
582                         
583                         name = talloc_strdup(mem_ctx, TEST_USERNAME);
584                 }
585         }
586
587 cleanup:
588         if (!test_cleanup(ctx->samr.pipe, mem_ctx, &ctx->samr.handle, name)) {
589                 printf("cleanup failed\n");
590                 ret = False;
591                 goto done;
592         }
593
594         if (!test_samr_close(ctx->samr.pipe, mem_ctx, &ctx->samr.handle)) {
595                 printf("domain close failed\n");
596                 ret = False;
597         }
598
599         talloc_free(mem_ctx);
600
601 done:
602         talloc_free(ctx);
603         talloc_free(prep_mem_ctx);
604         return ret;
605 }
606
607
608 BOOL torture_userinfo_api(struct torture_context *torture)
609 {
610         const char *name = TEST_USERNAME;
611         const char *binding;
612         BOOL ret = True;
613         NTSTATUS status;
614         TALLOC_CTX *mem_ctx = NULL, *prep_mem_ctx;
615         struct libnet_context *ctx;
616         struct dcerpc_pipe *p;
617         struct policy_handle h;
618         struct lsa_String domain_name;
619         struct libnet_UserInfo req;
620
621         prep_mem_ctx = talloc_init("prepare torture user info");
622         binding = torture_setting_string(torture, "binding", NULL);
623
624         ctx = libnet_context_init(NULL);
625         ctx->cred = cmdline_credentials;
626
627         status = torture_rpc_connection(prep_mem_ctx,
628                                         &p,
629                                         &dcerpc_table_samr);
630         if (!NT_STATUS_IS_OK(status)) {
631                 return False;
632         }
633
634         domain_name.string = lp_workgroup();
635         if (!test_opendomain(p, prep_mem_ctx, &h, &domain_name)) {
636                 ret = False;
637                 goto done;
638         }
639
640         if (!test_createuser(p, prep_mem_ctx, &h, name)) {
641                 ret = False;
642                 goto done;
643         }
644
645         mem_ctx = talloc_init("torture user info");
646
647         ZERO_STRUCT(req);
648         
649         req.in.domain_name = domain_name.string;
650         req.in.user_name   = name;
651
652         status = libnet_UserInfo(ctx, mem_ctx, &req);
653         if (!NT_STATUS_IS_OK(status)) {
654                 printf("libnet_UserInfo call failed: %s\n", nt_errstr(status));
655                 ret = False;
656                 talloc_free(mem_ctx);
657                 goto done;
658         }
659
660         if (!test_cleanup(ctx->samr.pipe, mem_ctx, &ctx->samr.handle, TEST_USERNAME)) {
661                 printf("cleanup failed\n");
662                 ret = False;
663                 goto done;
664         }
665
666         if (!test_samr_close(ctx->samr.pipe, mem_ctx, &ctx->samr.handle)) {
667                 printf("domain close failed\n");
668                 ret = False;
669         }
670
671         talloc_free(ctx);
672
673 done:
674         talloc_free(mem_ctx);
675         return ret;
676 }
677
678
679 BOOL torture_userlist(struct torture_context *torture)
680 {
681         const char *binding;
682         BOOL ret = True;
683         NTSTATUS status;
684         TALLOC_CTX *mem_ctx = NULL;
685         struct libnet_context *ctx;
686         struct lsa_String domain_name;
687         struct libnet_UserList req;
688
689         binding = torture_setting_string(torture, "binding", NULL);
690
691         ctx = libnet_context_init(NULL);
692         ctx->cred = cmdline_credentials;
693
694         domain_name.string = lp_workgroup();
695         mem_ctx = talloc_init("torture user list");
696
697         ZERO_STRUCT(req);
698         
699         req.in.domain_name = domain_name.string;
700         req.in.page_size   = 30;
701         req.in.restore_index = 0;
702
703         status = libnet_UserList(ctx, mem_ctx, &req);
704         if (!NT_STATUS_IS_OK(status)) {
705                 printf("libnet_UserList call failed: %s\n", nt_errstr(status));
706                 ret = False;
707                 talloc_free(mem_ctx);
708                 goto done;
709         }
710
711         if (!test_lsa_close(ctx->lsa.pipe, mem_ctx, &ctx->lsa.handle)) {
712                 printf("domain close failed\n");
713                 ret = False;
714         }
715
716         talloc_free(ctx);
717
718 done:
719         talloc_free(mem_ctx);
720         return ret;
721 }