summaryrefslogtreecommitdiffstats
path: root/private/lsa/server/dbsamtst.c
blob: ef68b8201f07725f355fd55c0544434d869a6064 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
/*++

Copyright (c) 1991  Microsoft Corporation

Module Name:

    dbsamtst.c

Abstract:

    LSA Database - SAM Database load testing code

    When enabled, this code provides a mechanism for stress loading of the
    SAM database with a large number of accounts.  The load executes in the
    context of lsass.exe, similar to how it does in replication.

Author:

    Scott Birrell       (ScottBi)      January 10, 1992

Environment:

Revision History:

--*/

#include "lsasrvp.h"
#include <lsaisrv.h>
#include "dbp.h"


//
// Uncomment the define LSA_SAM_ACCOUNTS_DOMAIN_TEST to enable the
// code needed for the ctsamdb test program.  Recompile dbsamtst.c,
// dbpolicy.c.  rebuild lsasrv.dll and nmake UMTYPE=console UMTEST=ctsamdb.
//

#ifdef LSA_SAM_ACCOUNTS_DOMAIN_TEST

//
// Global data needed by test
//

//
// Random Unicode text Buffer (gets updated pseudo-randomly during run)
//

#define LSAP_DB_TEST_TEXT_BUFF_CHAR_LENGTH     ((ULONG) 1024)

#define LSAP_DB_TEST_TEXT_BUFF_BYTE_LENGTH      \
    (LSAP_DB_TEST_TEXT_BUFF_CHAR_LENGTH * sizeof (WCHAR))

static PWSTR TextBuffer = L"ABCDEFGHIJKLMNOPQRSTUVWXYZAABBCC"
                          L"DDEEFFGGHHIIJJKKLLMMNNOOPPQQRRSS"
                          L"TTUUVVWWXXYYZZZYXWVUTSRQPONMLKJI"
                          L"HGFEDCBAMEJHCKZXHDKCXJHCKXSKLCJJ"
                          L"QKXJHDHNSLCJIFUENSGXCJCVKLSJKSHD"
                          L"QXKXJHDGHASOESDGPMSDVJKSDNJCKACJ"
                          L"AOJSDVJKJASDHABJKACSBNVAJKAJSKDV"
                          L"AJLSBSDJBVAJKLQWDQBVAKLJSCVAKLDD"
                          L"OJWQEFBNQGOTQJWDBVBJOQWDBXQPJJPB"
                          L"QWERTYUIOPASDFGHJKLZXCVBNMQUFIEJ"
                          L"ARWFUITYBNOMVJGBLJHKGJKGKGTJHKHS"
                          L"AOPDEBLFPYVQRUCVKJGRKSDBKGKWPOID"
                          L"MBCZLADJGFDOYEJHLSDYQWHLSOQLFHBF"
                          L"MCNXBZLGHKFDKDHSAQTWYEUTODMKCVJW"
                          L"QOPKIJUNYBTYVTCRCXEXECDWZQGGICRT"
                          L"ZWRFXCERYHVFYJNGHJNMBGTYVFJNMJJK";

//
// Random Unicode text Buffer backup (used for reset)
//

static PWSTR TextBuffBak = L"ABCDEFGHIJKLMNOPQRSTUVWXYZAABBCC"
                           L"DDEEFFGGHHIIJJKKLLMMNNOOPPQQRRSS"
                           L"TTUUVVWWXXYYZZZYXWVUTSRQPONMLKJI"
                           L"HGFEDCBAMEJHCKZXHDKCXJHCKXSKLCJJ"
                           L"QKXJHDHNSLCJIFUENSGXCJCVKLSJKSHD"
                           L"QXKXJHDGHASOESDGPMSDVJKSDNJCKACJ"
                           L"AOJSDVJKJASDHABJKACSBNVAJKAJSKDV"
                           L"AJLSBSDJBVAJKLQWDQBVAKLJSCVAKLDD"
                           L"OJWQEFBNQGOTQJWDBVBJOQWDBXQPJJPB"
                           L"QWERTYUIOPASDFGHJKLZXCVBNMQUFIEJ"
                           L"ARWFUITYBNOMVJGBLJHKGJKGKGTJHKHS"
                           L"AOPDEBLFPYVQRUCVKJGRKSDBKGKWPOID"
                           L"MBCZLADJGFDOYEJHLSDYQWHLSOQLFHBF"
                           L"MCNXBZLGHKFDKDHSAQTWYEUTODMKCVJW"
                           L"QOPKIJUNYBTYVTCRCXEXECDWZQGGICRT"
                           L"ZWRFXCERYHVFYJNGHJNMBGTYVFJNMJJK";

static ULONG Array0Index = 0,
             Array1Index = 0,
             Array2Index = 0,
             Array3Index = 0,
             Array4Index = 0,
             Array5Index = 0;


static ULONG Array0[] = { 0, 44, 88 };
static ULONG Array1[] = { 87,2,45,48,68};
static ULONG Array2[] = { 23,1,43,37,4,16,8 };
static ULONG Array3[] = { 64,5,17,29,18,17,22,56,19,20,6 };
static ULONG Array4[] = { 7,11,97,88,8,5,24,0,99,51,33,20,80 };
static ULONG Array5[] = { 53,56,3,27,29,98,35,47,53,92,89,4,66,34,1,99,3 };


NTSTATUS
LsapDbTestLoadSamAccountsDomain(
    IN PUNICODE_STRING NumberOfAccounts
    );

NTSTATUS
LsapDbTestLoadSamAccountsDomainInitialize(
    IN OUT PUSER_ALL_INFORMATION UserInformation
    );

NTSTATUS
LsapDbTestCreateNextAccountInfo(
    IN OUT PUSER_ALL_INFORMATION UserInformation
    );

NTSTATUS
LsapDbTestGenUnicodeString(
    OUT PUNICODE_STRING OutputString,
    IN ULONG MinimumLength,
    IN ULONG MaximumLength
    );

PWSTR
LsapDbTestGenRandomStringPointer(
    IN ULONG MinLengthToLeaveAtEnd
    );

ULONG
LsapDbTestGenRandomNumber(
    IN ULONG MinimumValue,
    IN ULONG MaximumValue
    );

NTSTATUS
LsapDbTestLoadSamAccountsDomain(
    IN PUNICODE_STRING NumberOfAccounts
    )

/*++

Routine Description:

    This function creates a number of users in the local SAM Accounts Domain,
    or deletes previously created users in the domain.

Arguments:

    AccountCount - Specifies the number of accounts to be created.  If a negative
        number is specified, the accounts are deleted.

Return Values:

    NTSTATUS - Standard Nt Result Code

--*/

{
    NTSTATUS Status, EnumerateStatus;
    LONG AccountNumber = 0;
    LONG AccountCount = 0;
    LONG CollisionCount = 0;
    LONG ExistingAccountCount = 0;
    LONG DeletedUserCount = 0;
    ANSI_STRING NumberOfAccountsAnsi;
    PLSAPR_POLICY_ACCOUNT_DOM_INFO PolicyAccountDomainInfo = NULL;
    LSA_TRUST_INFORMATION TrustInformation;
    SAMPR_HANDLE LocalSamServerHandle = NULL;
    USER_ALL_INFORMATION UserInformation;
    SAMPR_HANDLE UserHandle = NULL;
    SAMPR_HANDLE LocalSamDomainHandle = NULL;
    ULONG InitialRelativeId, RelativeId;
    ULONG CountReturned;
    SAM_ENUMERATE_HANDLE EnumerationContext = 0;
    PSAMPR_ENUMERATION_BUFFER RidEnumerationBuffer = NULL;
    ULONG NextAccount;
    ULONG ConflictingAccountRid;
    LARGE_INTEGER StartTime, TimeAfterThis100Users, TimeAfterPrevious100Users;
    LARGE_INTEGER TimeForThis100Users, TimeForThis100UsersInMs, TotalTime, TenThousand;
    

    //
    // Open the local SAM Accounts Domain
    //
    // The Sid and Name of the Account Domain are both configurable, and
    // we need to obtain them from the Policy Object.  Now obtain the
    // Account Domain Sid and Name by querying the appropriate
    // Policy Information Class.
    //

    Status = LsarQueryInformationPolicy(
                 LsapPolicyHandle,
                 PolicyAccountDomainInformation,
                 (PLSAPR_POLICY_INFORMATION *) &PolicyAccountDomainInfo
                 );

    if (!NT_SUCCESS(Status)) {

        KdPrint(("LsarQueryInformationPolicy failed 0x%lx\n", Status));
        goto TestLoadSamAccountsDomainError;
    }

    //
    // Set up the Trust Information structure for the Account Domain.
    //

    TrustInformation.Sid = PolicyAccountDomainInfo->DomainSid;

    RtlCopyMemory(
        &TrustInformation.Name,
        &PolicyAccountDomainInfo->DomainName,
        sizeof (UNICODE_STRING)
        );

    //
    // Connect to the Local Sam.  The LSA server is a trusted client, so we
    // call the internal version of the SamConnect routine.
    //

    Status = SamIConnect( NULL, &LocalSamServerHandle, 0, TRUE );

    if (!NT_SUCCESS(Status)) {

        KdPrint(("SamIConnect failed 0x%lx\n", Status));
        goto TestLoadSamAccountsDomainError;
    }

    //
    // Open the Account Domain.
    //

    Status = SamrOpenDomain(
                 LocalSamServerHandle,
                 DOMAIN_LOOKUP,
                 TrustInformation.Sid,
                 &LocalSamDomainHandle
                 );

    if (!NT_SUCCESS(Status)) {

        KdPrint(("SamrOpenDomain failed 0x%lx\n", Status));
        goto TestLoadSamAccountsDomainError;
    }

    //
    // Now convert the count of accounts from unicode.
    //

    Status = RtlUnicodeStringToAnsiString(
                 &NumberOfAccountsAnsi,
                 NumberOfAccounts,
                 TRUE
                 );

    if (!NT_SUCCESS(Status)) {

        KdPrint(("RtlUnicodeStringToAnsiString failed 0x%lx\n", Status));
        goto TestLoadSamAccountsDomainError;
    }

    AccountCount = (LONG) atoi(NumberOfAccountsAnsi.Buffer);

    //
    // Tke initial relative id is used by both the create and delete
    // account branches so initialize it here.
    //

    InitialRelativeId = (ULONG) 0x00001000L;

    //
    // If the number of accounts is > 0, add the specified number of
    // accounts to the SAM account domain.  If the number of accounts is
    // < 0, delete all accounts from the SAM account s domain.
    //

    if (AccountCount > 0) {

        //
        // Initialize the constant fields in the UserInformation structure
        //

        Status = LsapDbTestLoadSamAccountsDomainInitialize(&UserInformation);

        if (!NT_SUCCESS(Status)) {

            goto TestLoadSamAccountsDomainError;
        }

        KdPrint(("Sam Account Database Load Begins\n"));

        Status = NtQuerySystemTime( &StartTime );

        if (!NT_SUCCESS(Status)) {

            return(Status);
        }

        TimeAfterPrevious100Users = StartTime;

        //
        // Conversion factor for converting 100ns ticks to milliseconds
        //

        TenThousand = RtlConvertUlongToLargeInteger((ULONG) 10000);

        for( AccountNumber = 0; AccountNumber < AccountCount; AccountNumber++) {

            //
            // Generate Information for this Account
            //

            Status = LsapDbTestCreateNextAccountInfo(&UserInformation);

            if (!NT_SUCCESS(Status)) {

                break;
            }

            RelativeId = InitialRelativeId + (ULONG) AccountNumber;

            //
            // Create the account
            //

            ConflictingAccountRid = (ULONG) 0;

            Status = SamICreateAccountByRid(
                         LocalSamDomainHandle,
                         SamObjectUser,
                         RelativeId,
                         (PRPC_UNICODE_STRING) &UserInformation.UserName,
                         USER_ALL_ACCESS,
                         &UserHandle,
                         &ConflictingAccountRid
                         );

            if (!NT_SUCCESS(Status)) {

                if ((Status == STATUS_USER_EXISTS) || (Status == STATUS_OBJECT_TYPE_MISMATCH)) {

                    CollisionCount++;
                    AccountNumber--;
                    if ((CollisionCount%100)==0) {
                        KdPrint(("."));
                    }
                    continue;
                }

                KdPrint(("SamrCreateAccountByRid failed 0x%lx\n", Status));
                break;
            }

            //
            // If an account already existed with the given Rid and had
            // the same account type  and name, it will have been opened.
            // Record the number of occurrences of this.
            //

            if (ConflictingAccountRid == RelativeId) {

                ExistingAccountCount++;
            }

            //
            // Set the information for the account
            //

            UserInformation.UserId = RelativeId;

            Status = SamrSetInformationUser(
                         UserHandle,
                         UserAllInformation,
                         (PSAMPR_USER_INFO_BUFFER) &UserInformation
                         );

            if (!NT_SUCCESS(Status)) {

                KdPrint(("SamrSetInformationUser failed 0x%lx\n", Status));
                break;
            }

            Status = SamrCloseHandle( &UserHandle );

            if ((AccountNumber > 0) && ((AccountNumber % 100) == 0)) {

                Status = NtQuerySystemTime( &TimeAfterThis100Users );

                if (!NT_SUCCESS(Status)) {

                    break;
                }

                TimeForThis100Users.QuadPart = TimeAfterThis100Users.QuadPart -
                                               TimeAfterPrevious100Users.QuadPart;

                TimeForThis100UsersInMs = TimeForThis100Users.QuadPart /
                                          TenThousand.QuadPart;

                TotalTime = TimeAfterThis100Users.QuadPart -
                            StartTime.QuadPart;

                KdPrint(("%d Accounts Created\n", AccountNumber));
                KdPrint(("Last 100 users took %d millisecs to create\n", TimeForThis100UsersInMs.LowPart));

                if (ExistingAccountCount > 0) {

                    KdPrint(("%d Attempts to create non-conflicting existing users\n\n", ExistingAccountCount));
                }

                if (CollisionCount > 0) {

                    KdPrint(("%d Creation conflicts with existing users\n", CollisionCount));
                }

                TimeAfterPrevious100Users = TimeAfterThis100Users;
            }
        }

        KdPrint(("%d Accounts Created\n", AccountNumber));
        KdPrint(("%d existing accounts opened\n", ExistingAccountCount));
        KdPrint(("%d Rid/Name/Type conflicts with existing accounts\n", CollisionCount));
        KdPrint(("\nSam Account Database Load Ends\n"));

    } else if (AccountCount < 0) {

        //
        // Delete the accounts
        //

        KdPrint(("Deleting user accounts with Rid >= 4096 in the local SAM Accounts Domain\n"));

        EnumerateStatus = STATUS_MORE_ENTRIES;

        while(EnumerateStatus == STATUS_MORE_ENTRIES) {

            RidEnumerationBuffer = NULL;

            Status = SamrEnumerateUsersInDomain(
                         LocalSamDomainHandle,
                         &EnumerationContext,
                         USER_NORMAL_ACCOUNT,
                         (PSAMPR_ENUMERATION_BUFFER *) &RidEnumerationBuffer,
                         4096,
                         &CountReturned
                         );

            EnumerateStatus = Status;

            if (!NT_SUCCESS(Status)) {

                KdPrint(("SamrEnumerateUsersInDomain failed 0x%lx\n", Status));
                break;
            }

            if (CountReturned == 0) {

                break;
            }

            for (NextAccount = 0; NextAccount < CountReturned; NextAccount++) {

                RelativeId = RidEnumerationBuffer->Buffer[ NextAccount ].RelativeId;

                //
                // Skip this account if it's not one of ours.  This is not
                // a rigorous check, but works.
                //

                if (RelativeId < InitialRelativeId) {

                    continue;
                }

                Status = SamrOpenUser(
                             LocalSamDomainHandle,
                             DELETE,
                             RelativeId,
                             &UserHandle
                             );

                if (!NT_SUCCESS(Status)) {

                    KdPrint(("SamrOpenUser failed 0x%lx\n", Status));
                    break;
                }

                Status = SamrDeleteUser( &UserHandle );

                if (!NT_SUCCESS(Status)) {

                    if (Status != STATUS_SPECIAL_ACCOUNT) {

                        KdPrint(("SamrDeleteUser failed 0x%lx\n", Status));
                        break;
                    }

                    Status = SamrCloseHandle(&UserHandle);

                    if (!NT_SUCCESS(Status)) {

                        KdPrint(("SamrCloseHandle failed 0x%lx\n", Status));
                        break;
                    }
                }

                DeletedUserCount++;
            }

            if (!NT_SUCCESS(Status)) {

                break;
            }
        }

        if (NT_SUCCESS(Status)) {

            KdPrint(("%d user accounts deleted\n", DeletedUserCount));
            KdPrint(("Deletion of user accounts ends\n"));
        }
    }

TestLoadSamAccountsDomainFinish:

    return(Status);

TestLoadSamAccountsDomainError:

    goto TestLoadSamAccountsDomainFinish;
}


NTSTATUS
LsapDbTestLoadSamAccountsDomainInitialize(
    IN OUT PUSER_ALL_INFORMATION UserInformation
    )

/*++

Routine Description:

    This function initializes the constant values required by the
    SAM Accounts Domain load/update tests.  These values include
    static global data and fixed values within a USER_ALL_INFORMATION
    structure used for creating/updating accounts.

Arguments:

    UserInformation - Points to USER_ALL_INFORMATION structure.

Return Values:

    NTSTATUS - Standard Nt Result Code

--*/

{
    NTSTATUS Status = STATUS_SUCCESS;
    ULONG Index;

    static UCHAR LogonHours[SAM_HOURS_PER_WEEK / 8];

    //
    // Restore the Unicode Text Buffer used for generating random
    // Unicode Strings to its original contents.
    //

    RtlCopyMemory( TextBuffer, TextBuffBak, LSAP_DB_TEST_TEXT_BUFF_CHAR_LENGTH );

    //
    // Reset the random number generator indices
    //

    Array0Index = 0;
    Array1Index = 0;
    Array2Index = 0;
    Array3Index = 0;
    Array4Index = 0;
    Array5Index = 0;

    //
    // Initialize Logon Hours data
    //

    for( Index = 0; Index < (SAM_HOURS_PER_WEEK / 8); Index++) {

        LogonHours[Index] = (UCHAR) 0xff;
    }

    //
    // Initialize constaint values in User Information structure
    //

    UserInformation->LogonCount = 0;
    UserInformation->LogonHours.UnitsPerWeek = SAM_HOURS_PER_WEEK;
    UserInformation->LogonHours.LogonHours = LogonHours;
    UserInformation->BadPasswordCount = 1;
    UserInformation->LogonCount = 1;
    UserInformation->UserAccountControl = USER_NORMAL_ACCOUNT;
    UserInformation->CountryCode = 1;
    UserInformation->CodePage = 850;
    UserInformation->NtPasswordPresent = FALSE;
    UserInformation->LmPasswordPresent = FALSE;
    UserInformation->PasswordExpired = FALSE;
    UserInformation->SecurityDescriptor.SecurityDescriptor = NULL;
    UserInformation->SecurityDescriptor.Length = 0;
    UserInformation->WhichFields =
        (
         USER_ALL_USERNAME |
         USER_ALL_FULLNAME |
         USER_ALL_ADMINCOMMENT |
         USER_ALL_USERCOMMENT |
         USER_ALL_HOMEDIRECTORY |
         USER_ALL_HOMEDIRECTORYDRIVE |
         USER_ALL_SCRIPTPATH |
         USER_ALL_PROFILEPATH |
         USER_ALL_LOGONHOURS |
         USER_ALL_USERACCOUNTCONTROL
        );
    return(Status);
}


NTSTATUS
LsapDbTestCreateNextAccountInfo(
    IN OUT PUSER_ALL_INFORMATION UserInformation
    )

/*++

Routine Description:

    This function generates the user information for the next user.

Arguments:

    UserInformation - Pointer to User Information structure

Return Values:

    NTSTATUS - Standard Nt Result Code

--*/

{

#define LSAP_TEST_MIN_USERNAME_LENGTH ((ULONG)  0x00000005L)
#define LSAP_TEST_MAX_USERNAME_LENGTH ((ULONG)  0x0000000cL)

#define LSAP_TEST_MIN_FULLNAME_LENGTH ((ULONG)  0x00000003L)
#define LSAP_TEST_MAX_FULLNAME_LENGTH ((ULONG)  0x00000030L)

#define LSAP_TEST_MIN_HOMEDIR_LENGTH ((ULONG)   0x00000003L)
#define LSAP_TEST_MAX_HOMEDIR_LENGTH ((ULONG)   0x00000030L)

#define LSAP_TEST_MIN_SCRIPTPATH_LENGTH ((ULONG)  0x00000003L)
#define LSAP_TEST_MAX_SCRIPTPATH_LENGTH ((ULONG)  0x00000100L)

#define LSAP_TEST_MIN_PROFPATH_LENGTH ((ULONG)  0x00000003L)
#define LSAP_TEST_MAX_PROFPATH_LENGTH ((ULONG)  0x00000100L)

#define LSAP_TEST_MIN_DRIVE_LENGTH ((ULONG)     0x00000001L)
#define LSAP_TEST_MAX_DRIVE_LENGTH ((ULONG)     0x00000001L)

#define LSAP_TEST_MIN_ADMCMT_LENGTH ((ULONG)     0x00000003L)
#define LSAP_TEST_MAX_ADMCMT_LENGTH ((ULONG)     0x00000100L)

#define LSAP_TEST_MIN_WKSTA_LENGTH ((ULONG)     0x00000003L)
#define LSAP_TEST_MAX_WKSTA_LENGTH ((ULONG)     0x00000040L)

#define LSAP_TEST_MIN_USER_COMMENT_LENGTH ((ULONG)     0x00000003L)
#define LSAP_TEST_MAX_USER_COMMENT_LENGTH ((ULONG)     0x00000040L)

#define LSAP_TEST_MIN_PARAMS_LENGTH ((ULONG)     0x00000003L)
#define LSAP_TEST_MAX_PARAMS_LENGTH ((ULONG)     0x00000040L)

#define LSAP_TEST_MIN_PRIVDATA_LENGTH ((ULONG)     0x00000003L)
#define LSAP_TEST_MAX_PRIVDATA_LENGTH ((ULONG)     0x00000040L)

    NTSTATUS Status;

    Status = LsapDbTestGenUnicodeString(
                 &UserInformation->UserName,
                 LSAP_TEST_MIN_USERNAME_LENGTH,
                 LSAP_TEST_MAX_USERNAME_LENGTH
                 );

    if (!NT_SUCCESS(Status)) {

        return(Status);
    }

    Status = LsapDbTestGenUnicodeString(
                 &UserInformation->FullName,
                 LSAP_TEST_MIN_FULLNAME_LENGTH,
                 LSAP_TEST_MAX_FULLNAME_LENGTH
                 );

    if (!NT_SUCCESS(Status)) {

        return(Status);
    }

    Status = LsapDbTestGenUnicodeString(
                 &UserInformation->HomeDirectoryDrive,
                 LSAP_TEST_MIN_DRIVE_LENGTH,
                 LSAP_TEST_MAX_DRIVE_LENGTH
                 );

    if (!NT_SUCCESS(Status)) {

        return(Status);
    }

    Status = LsapDbTestGenUnicodeString(
                 &UserInformation->HomeDirectory,
                 LSAP_TEST_MIN_HOMEDIR_LENGTH,
                 LSAP_TEST_MAX_HOMEDIR_LENGTH
                 );

    if (!NT_SUCCESS(Status)) {

        return(Status);
    }

    Status = LsapDbTestGenUnicodeString(
                 &UserInformation->ScriptPath,
                 LSAP_TEST_MIN_SCRIPTPATH_LENGTH,
                 LSAP_TEST_MAX_SCRIPTPATH_LENGTH
                 );

    if (!NT_SUCCESS(Status)) {

        return(Status);
    }

    Status = LsapDbTestGenUnicodeString(
                 &UserInformation->ProfilePath,
                 LSAP_TEST_MIN_PROFPATH_LENGTH,
                 LSAP_TEST_MAX_PROFPATH_LENGTH
                 );

    if (!NT_SUCCESS(Status)) {

        return(Status);
    }

    Status = LsapDbTestGenUnicodeString(
                 &UserInformation->AdminComment,
                 LSAP_TEST_MIN_ADMCMT_LENGTH,
                 LSAP_TEST_MAX_ADMCMT_LENGTH
                 );

    if (!NT_SUCCESS(Status)) {

        return(Status);
    }

    Status = LsapDbTestGenUnicodeString(
                 &UserInformation->WorkStations,
                 LSAP_TEST_MIN_WKSTA_LENGTH,
                 LSAP_TEST_MAX_WKSTA_LENGTH
                 );

    if (!NT_SUCCESS(Status)) {

        return(Status);
    }

    Status = LsapDbTestGenUnicodeString(
                 &UserInformation->UserComment,
                 LSAP_TEST_MIN_USER_COMMENT_LENGTH,
                 LSAP_TEST_MAX_USER_COMMENT_LENGTH
                 );

    if (!NT_SUCCESS(Status)) {

        return(Status);
    }

    Status = LsapDbTestGenUnicodeString(
                 &UserInformation->Parameters,
                 LSAP_TEST_MIN_PARAMS_LENGTH,
                 LSAP_TEST_MAX_PARAMS_LENGTH
                 );

    if (!NT_SUCCESS(Status)) {

        return(Status);
    }

    Status = LsapDbTestGenUnicodeString(
                 (PUNICODE_STRING) &UserInformation->PrivateData,
                 LSAP_TEST_MIN_PRIVDATA_LENGTH,
                 LSAP_TEST_MAX_PRIVDATA_LENGTH
                 );

    return(Status);
}

NTSTATUS
LsapDbTestGenUnicodeString(
    OUT PUNICODE_STRING OutputString,
    IN ULONG MinimumLength,
    IN ULONG MaximumLength
    )

/*++

Routine Description:

    This function generates a pseudo-random Unicode String whose length
    lies within the indicated bounds.

Arguments:

    OutputString - Points to output UNICODE_STRING structure to be
        initialized.

    MinimumLength - Minimum length required in wide characters.

    MaximumLength - Maximum length required in wide characters.

Return Values:

    NTSTATUS - Standard Nt Result Code

--*/

{
    NTSTATUS Status = STATUS_SUCCESS;

    OutputString->Length = (USHORT) (sizeof(WCHAR) * LsapDbTestGenRandomNumber(
                                                        MinimumLength,
                                                        MaximumLength
                                                        ));

    OutputString->Buffer = LsapDbTestGenRandomStringPointer(
                               (OutputString->Length / 2)
                               );

    OutputString->MaximumLength = OutputString->Length;

    return(Status);
}

PWSTR
LsapDbTestGenRandomStringPointer(
    IN ULONG MinLengthToLeaveAtEnd
    )

/*++

Routine Description:

    This function returns a random pointer to a location within a global
    block of text.

Arguments:

    MinLengthToLeaveAtEnd - Specifies the minimum length of the string
        in Wide Chars from the returned pointer to the end of the global buffer.

Return Values:

    PUCHAR - Receives a pointer referencing a character within the block
        of text.

--*/

{
    WCHAR Temp;

    ULONG Offset = LsapDbTestGenRandomNumber(
                       0,
                       (ULONG) 512 - MinLengthToLeaveAtEnd
                       );

    ULONG Offset1 = LsapDbTestGenRandomNumber(
                       0,
                       (ULONG) 512 - 1
                       );

    ULONG Offset2 = LsapDbTestGenRandomNumber(
                       0,
                       (ULONG) 512 - 1
                       );

    ULONG Offset3 = LsapDbTestGenRandomNumber(
                       0,
                       (ULONG) 512 - 1
                       );

    ULONG Offset4 = LsapDbTestGenRandomNumber(
                       0,
                       (ULONG) 512 - 1
                       );

    //
    // Randomly alter the 2nd to 6th characters of the selected string.
    //

    Temp = TextBuffer[Offset + 1];
    TextBuffer[Offset + 1] = TextBuffer[Offset1];
    TextBuffer[Offset1] = Temp;

    if ((Offset > 0) && (Offset < (ULONG) (512 - 2))) {

        Temp = TextBuffer[Offset + 2];
        TextBuffer[Offset + 2] = TextBuffer[Offset2];
        TextBuffer[Offset2] = Temp;
    }

    if ((Offset > 0) && (Offset < (ULONG) (512 - 3))) {

        Temp = TextBuffer[Offset + 3];
        TextBuffer[Offset + 3] = TextBuffer[Offset3];
        TextBuffer[Offset3] = Temp;
    }

    if ((Offset > 0) && (Offset < (ULONG) (512 - 4))) {

        Temp = TextBuffer[Offset + 4];
        TextBuffer[Offset + 4] = TextBuffer[Offset4];
        TextBuffer[Offset4] = Temp;
    }

    return(&TextBuffer[Offset]);
}


ULONG
LsapDbTestGenRandomNumber(
    IN ULONG MinimumValue,
    IN ULONG MaximumValue
    )

/*++

Routine Description:

    This function generates a pseudo-random unsigned integer lying between
    two values.

Arguments:

    MinimumValue - The minimum value of the random number

    MaximumValue - The maximum value of the random number

--*/

{
    ULONG NextValue, NextValue1, NextValue2;
    ULONG MaxPossValue = 88 + 87 + 43 + 64 + 99 + 99;

    Array0Index++;
    if (Array0Index == sizeof(Array0) / sizeof(ULONG)) {

        Array0Index = 0;
    }

    Array1Index++;
    if (Array1Index == sizeof(Array1) / sizeof(ULONG)) {

        Array1Index = 0;
    }

    Array2Index++;
    if (Array2Index == sizeof(Array2) / sizeof(ULONG)) {

        Array2Index = 0;
    }

    Array3Index++;
    if (Array3Index == sizeof(Array3) / sizeof(ULONG)) {

        Array3Index = 0;
    }

    Array4Index++;
    if (Array4Index == sizeof(Array4) / sizeof(ULONG)) {

        Array4Index = 0;
    }

    Array5Index++;
    if (Array5Index == sizeof(Array5) / sizeof(ULONG)) {

        Array5Index = 0;
    }

    NextValue1 = Array0[Array0Index] +
                 Array1[Array1Index] +
                 Array2[Array2Index] +
                 Array3[Array3Index] +
                 Array4[Array4Index] +
                 Array5[Array5Index];

    Array0Index++;
    if (Array0Index == sizeof(Array0) / sizeof(ULONG)) {

        Array0Index = 0;
    }

    Array1Index++;
    if (Array1Index == sizeof(Array1) / sizeof(ULONG)) {

        Array1Index = 0;
    }

    Array2Index++;
    if (Array2Index == sizeof(Array2) / sizeof(ULONG)) {

        Array2Index = 0;
    }

    Array3Index++;
    if (Array3Index == sizeof(Array3) / sizeof(ULONG)) {

        Array3Index = 0;
    }

    Array4Index++;
    if (Array4Index == sizeof(Array4) / sizeof(ULONG)) {

        Array4Index = 0;
    }

    Array5Index++;
    if (Array5Index == sizeof(Array5) / sizeof(ULONG)) {

        Array5Index = 0;
    }

    NextValue2 = Array0[Array0Index] +
                 Array1[Array1Index] +
                 Array2[Array2Index] +
                 Array3[Array3Index] +
                 Array4[Array4Index] +
                 Array5[Array5Index];

    if (NextValue2 > NextValue1) {

        NextValue = NextValue2 - NextValue1;

    } else {

        NextValue = NextValue1 - NextValue2;
    }

    NextValue = MinimumValue +
                    (MaximumValue - MinimumValue) * NextValue / MaxPossValue;

    return(NextValue);
}

#endif // LSA_SAM_ACCOUNTS_DOMAIN_TEST