summaryrefslogtreecommitdiffstats
path: root/private/newsam/client/tchgpwd.c
blob: 5177e05dc179f11b1475c97487a0a0f9b5bda1b3 (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
// Test changing a SAM password



///////////////////////////////////////////////////////////////////////////////
//                                                                           //
// Includes                                                                  //
//                                                                           //
///////////////////////////////////////////////////////////////////////////////

#include <stdio.h>
#include <nt.h>
#include <ntsam.h>
#include <ntsamp.h>
#include <ntlsa.h>
#include <ntrpcp.h>     // prototypes for MIDL user functions
#include <seopaque.h>
#include <string.h>


VOID
main (argc, argv)
int argc;
char **argv;

{
    NTSTATUS                    Status;
    OBJECT_ATTRIBUTES           ObjectAttributes;
    SECURITY_QUALITY_OF_SERVICE SecurityQos;
    PSID                        DomainSid = NULL;
    PULONG                      UserId = NULL;
    PSID_NAME_USE               NameUse = NULL;
    SAM_HANDLE                  SamHandle = NULL;
    SAM_HANDLE                  DomainHandle = NULL;
    SAM_HANDLE                  UserHandle = NULL;
    WCHAR                       UserNameBuffer[80];
    WCHAR                       OldPasswordBuffer[80];
    WCHAR                       NewPasswordBuffer[80];
    UNICODE_STRING              UserName;
    UNICODE_STRING              Domain;
    UNICODE_STRING              OldPassword;
    UNICODE_STRING              NewPassword;
    ANSI_STRING                 AnsiString;

    UserName.Buffer = UserNameBuffer;
    UserName.MaximumLength = sizeof(UserNameBuffer);

    OldPassword.Buffer = OldPasswordBuffer;
    OldPassword.MaximumLength = sizeof(OldPasswordBuffer);

    NewPassword.Buffer = NewPasswordBuffer;
    NewPassword.MaximumLength = sizeof(NewPasswordBuffer);

    RtlInitUnicodeString(&Domain, L"Account");


    RtlInitAnsiString(&AnsiString, argv[1]);
    RtlAnsiStringToUnicodeString(&UserName, &AnsiString, FALSE);

    if (*(argv[2]) == '-') {
        *(argv[2]) = 0;
    }
    RtlInitAnsiString(&AnsiString, argv[2]);
    RtlAnsiStringToUnicodeString(&OldPassword, &AnsiString, FALSE);

    if (*(argv[3]) == '-') {
        *(argv[3]) = 0;
    }
    RtlInitAnsiString(&AnsiString, argv[3]);
    RtlAnsiStringToUnicodeString(&NewPassword, &AnsiString, FALSE);

    //
    // Setup ObjectAttributes for SamConnect call.
    //

    InitializeObjectAttributes(&ObjectAttributes, NULL, 0, 0, NULL);
    ObjectAttributes.SecurityQualityOfService = &SecurityQos;

    SecurityQos.Length = sizeof(SecurityQos);
    SecurityQos.ImpersonationLevel = SecurityIdentification;
    SecurityQos.ContextTrackingMode = SECURITY_STATIC_TRACKING;
    SecurityQos.EffectiveOnly = FALSE;

    Status = SamConnect(
                 NULL,
                 &SamHandle,
                 GENERIC_EXECUTE,
                 &ObjectAttributes
                 );

    if ( !NT_SUCCESS(Status) ) {
        DbgPrint("MspChangePasswordSam: SamConnect failed, status %8.8x\n", Status);
        goto Cleanup;
    }


    Status = SamLookupDomainInSamServer(
                 SamHandle,
                 &Domain,
                 &DomainSid
                 );

    if ( !NT_SUCCESS(Status) ) {
        DbgPrint("MspChangePasswordSam: Cannot find account domain, status %8.8x\n", Status);
        Status = STATUS_CANT_ACCESS_DOMAIN_INFO;
        goto Cleanup;
    }

    Status = SamOpenDomain(
                 SamHandle,
                 GENERIC_EXECUTE,
                 DomainSid,
                 &DomainHandle
                 );

    if ( !NT_SUCCESS(Status) ) {
        DbgPrint("MspChangePasswordSam: Cannot open account domain, status %8.8x\n", Status);
        Status = STATUS_CANT_ACCESS_DOMAIN_INFO;
        goto Cleanup;
    }

    Status = SamLookupNamesInDomain(
                 DomainHandle,
                 1,
                 &UserName,
                 &UserId,
                 &NameUse
                 );

    if ( !NT_SUCCESS(Status) ) {
        DbgPrint("MspChangePasswordSam: Cannot lookup user %wZ, status %8.8x\n", &UserName, Status);
        goto Cleanup;
    }

    Status = SamOpenUser(
                 DomainHandle,
                 USER_CHANGE_PASSWORD,
                 *UserId,
                 &UserHandle
                 );

    if ( !NT_SUCCESS(Status) ) {
        DbgPrint("MspChangePasswordSam: Cannot open user %wZ, status %8.8x\n",
                 &UserName, Status);
        goto Cleanup;
    }

    Status = SamChangePasswordUser(
                 UserHandle,
                 &OldPassword,
                 &NewPassword
                 );

    if ( !NT_SUCCESS(Status) ) {
        DbgPrint("MspChangePasswordSam: Failed to change user password, status %8.8x\n", Status);
    }


Cleanup:

    //
    // Free DomainSid if used.
    //

    if (DomainSid) {
        SamFreeMemory(DomainSid);
    }

    //
    // Free UserId if used.
    //

    if (UserId) {
        SamFreeMemory(UserId);
    }

    //
    // Free NameUse if used.
    //

    if (NameUse) {
        SamFreeMemory(NameUse);
    }

    //
    // Close UserHandle if open.
    //

    if (UserHandle) {
        SamCloseHandle(UserHandle);
    }

    //
    // Close DomainHandle if open.
    //

    if (DomainHandle) {
        SamCloseHandle(DomainHandle);
    }

    //
    // Close SamHandle if open.
    //

    if (SamHandle) {
        SamCloseHandle(SamHandle);
    }

}