summaryrefslogtreecommitdiffstats
path: root/private/nw/svcdlls/nwwks/lib/lsa.c
blob: 1dc664aa436e39403a780dc9b8830307eaa63a92 (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
/*++

Copyright (c) 1993  Microsoft Corporation

Module Name:

    lsa.c

Abstract:

    This module provides helpers to call LSA, particularly for
    manipulating secret objects.

Author:

    Rita Wong (ritaw)     22-Apr-1993

--*/

#include <stdlib.h>

#include <nt.h>
#include <ntrtl.h>
#include <nturtl.h>

#include <windef.h>
#include <winerror.h>
#include <winbase.h>

#include <nwlsa.h>

//-------------------------------------------------------------------//
//                                                                   //
// Constants and Macros                                              //
//                                                                   //
//-------------------------------------------------------------------//

#define NW_SECRET_PREFIX               L"_MS_NWCS_"


DWORD
NwOpenPolicy(
    IN ACCESS_MASK DesiredAccess,
    OUT LSA_HANDLE *PolicyHandle
    )
/*++

Routine Description:

    This function gets a handle to the local security policy by calling
    LsaOpenPolicy.

Arguments:

    DesiredAccess - Supplies the desired access to the local security
        policy.

    PolicyHandle - Receives a handle to the opened policy.

Return Value:

    NO_ERROR - Policy handle is returned.

    Error from LSA.

--*/
{
    NTSTATUS ntstatus;
    OBJECT_ATTRIBUTES ObjAttributes;


    //
    // Open a handle to the local security policy.  Initialize the
    // objects attributes structure first.
    //
    InitializeObjectAttributes(
        &ObjAttributes,
        NULL,
        0L,
        NULL,
        NULL
        );

    ntstatus = LsaOpenPolicy(
                   NULL,
                   &ObjAttributes,
                   DesiredAccess,
                   PolicyHandle
                   );

    if (! NT_SUCCESS(ntstatus)) {
        KdPrint(("NWWORKSTATION: LsaOpenPolicy returns %08lx\n",
                 ntstatus));

        return RtlNtStatusToDosError(ntstatus);
    }

    return NO_ERROR;
}


DWORD
NwOpenSecret(
    IN ACCESS_MASK DesiredAccess,
    IN LSA_HANDLE PolicyHandle,
    IN LPWSTR LsaSecretName,
    OUT PLSA_HANDLE SecretHandle
    )
/*++

Routine Description:

    This function opens a handle to the specified secret object.

Arguments:

    DesiredAccess - Supplies the desired access to the secret object.

    PolicyHandle - Supplies a handle to an already opened LSA policy.

    LsaSecretName - Supplies the name of the secret to open.

    SecretHandle - Receives the handle of the opened secret.

Return Value:

    NO_ERROR - Secret handle is returned.

    Error from LSA.

--*/
{
    NTSTATUS ntstatus;
    UNICODE_STRING SecretNameString;


    RtlInitUnicodeString(&SecretNameString, LsaSecretName);

    ntstatus = LsaOpenSecret(
                   PolicyHandle,
                   &SecretNameString,
                   DesiredAccess,
                   SecretHandle
                   );


    if (! NT_SUCCESS(ntstatus)) {
        KdPrint(("NWWORKSTATION: LsaOpenSecret %ws returns %08lx\n",
                 LsaSecretName, ntstatus));

        return RtlNtStatusToDosError(ntstatus);
    }

    return NO_ERROR;
}


DWORD
NwFormSecretName(
    IN  LPWSTR Qualifier,
    OUT LPWSTR *LsaSecretName
    )
/*++

Routine Description:

    This function creates a secret name from the user name.
    It also allocates the buffer to return the created secret name which
    must be freed by the caller using LocalFree when done with it.

Arguments:

    Qualifier - Supplies the qualifier which forms part of part of the secret
        object name we are creating.

    LsaSecretName - Receives a pointer to the buffer which contains the
        secret object name.

Return Value:

    NO_ERROR - Successfully returned secret name.

    ERROR_NOT_ENOUGH_MEMORY - Failed to allocate buffer to hold the secret
        name.

--*/
{
    if ((*LsaSecretName = (LPWSTR)LocalAlloc(
                              0,
                              (wcslen(NW_SECRET_PREFIX) +
                               wcslen(Qualifier) +
                               1) * sizeof(WCHAR)
                              )) == NULL) {

        KdPrint(("NWWORKSTATION: NwFormSecretName: LocalAlloc failed %lu\n",
                 GetLastError()));
        return ERROR_NOT_ENOUGH_MEMORY;
    }

    wcscpy(*LsaSecretName, NW_SECRET_PREFIX);
    wcscat(*LsaSecretName, Qualifier);

    return NO_ERROR;
}



DWORD
NwSetPassword(
    IN LPWSTR Qualifier,
    IN LPWSTR Password
    )
/*++

Routine Description:

    This function opens ( creates one if needed ) the LSA secret object, 
    and sets it with the specified password.

Arguments:

    Qualifier - Supplies the qualifier which forms part of part of the secret
        object name to be created.

    Password - Supplies the user specified password for an account.

Return Value:

    NO_ERROR - Secret object for the password is created and set with new value.

    Error from LSA.

--*/
{
    DWORD status;
    NTSTATUS ntstatus;
    LSA_HANDLE PolicyHandle;

    LSA_HANDLE SecretHandle;
    UNICODE_STRING SecretNameString;
    LPWSTR LsaSecretName;

    UNICODE_STRING NewPasswordString;
    UNICODE_STRING OldPasswordString;


    //
    // Open a handle to the local security policy.
    //
    if ((status = NwOpenPolicy(
                      POLICY_CREATE_SECRET,
                      &PolicyHandle
                      )) != NO_ERROR) {
        KdPrint(("NWWORKSTATION: NwCreatePassword: NwOpenPolicy failed\n"));
        return status;
    }

    //
    // Create the LSA secret object.  But first, form a secret name.
    //
    if ((status = NwFormSecretName(
                      Qualifier,
                      &LsaSecretName
                      )) != NO_ERROR) {
        (void) LsaClose(PolicyHandle);
        return status;
    }

    RtlInitUnicodeString(&SecretNameString, LsaSecretName);

    ntstatus = LsaCreateSecret(
                   PolicyHandle,
                   &SecretNameString,
                   SECRET_SET_VALUE | DELETE,
                   &SecretHandle
                   );


    //
    // note: ignore object already exists error. just use the existing
    //       object. this could be because the user didnt completely cleanup
    //       during deinstall. the unique names makes it unlikely to be a real
    //       collision.
    //
    if ( ntstatus == STATUS_OBJECT_NAME_COLLISION ) {
        ntstatus = NwOpenSecret(
                       SECRET_SET_VALUE,
                       PolicyHandle,
                       LsaSecretName,
                       &SecretHandle
                       );
    }

    //
    // Don't need the name or policy handle anymore.
    //
    (void) LocalFree((HLOCAL) LsaSecretName);
    (void) LsaClose(PolicyHandle);

    if (! NT_SUCCESS(ntstatus)) {

        KdPrint(("NWWORKSTATION: NwCreatePassword: LsaCreateSecret or LsaOpenSecret returned %08lx for %ws\n", ntstatus, LsaSecretName));

        return RtlNtStatusToDosError(ntstatus);
    }
    

    RtlInitUnicodeString(&OldPasswordString, NULL);
    RtlInitUnicodeString(&NewPasswordString, Password);

    ntstatus = LsaSetSecret(
                   SecretHandle,
                   &NewPasswordString,
                   &OldPasswordString
                   );

    if (! NT_SUCCESS(ntstatus)) {
        KdPrint(("NWWORKSTATION NwCreatePassword: LsaSetSecret returned %08lx\n",
                 ntstatus));

        status = RtlNtStatusToDosError(ntstatus);

        //
        // Delete the secret object
        //
        ntstatus = LsaDelete(SecretHandle);

        if (! NT_SUCCESS(ntstatus)) {
            KdPrint(("NWWORKSTATION: NwCreatePassword: LsaDelete to restore back to original returned %08lx\n",
                     ntstatus));
            (void) LsaClose(SecretHandle);
        }

        //
        // Secret handle is closed by LsaDelete if successfully deleted.
        //
        return status;
    }

    (void) LsaClose(SecretHandle);

    return NO_ERROR;
}



DWORD
NwDeletePassword(
    IN LPWSTR Qualifier
    )
/*++

Routine Description:

    This function deletes the LSA secret object whose name is derived
    from the specified Qualifier.

Arguments:

    Qualifier - Supplies the qualifier which forms part of part of the secret
        object name to be deleted.

Return Value:

    NO_ERROR - Secret object for password is deleted.

    Error from LSA.

--*/
{
    DWORD status;
    NTSTATUS ntstatus;

    LSA_HANDLE PolicyHandle;

    LSA_HANDLE SecretHandle;
    LPWSTR LsaSecretName;


    //
    // Open a handle to the local security policy.
    //
    if ((status = NwOpenPolicy(
            POLICY_VIEW_LOCAL_INFORMATION,
            &PolicyHandle
            )) != NO_ERROR) {
        KdPrint(("NWWORKSTATION: NwDeletePassword: NwOpenPolicy failed\n"));
        return status;
    }

    //
    // Get the secret object name from the specified user name.
    //
    if ((status = NwFormSecretName(
                      Qualifier,
                      &LsaSecretName
                      )) != NO_ERROR) {
        (void) LsaClose(PolicyHandle);
        return status;
    }

    status = NwOpenSecret(
                 DELETE,
                 PolicyHandle,
                 LsaSecretName,
                 &SecretHandle
                 );

    //
    // Don't need the name or policy handle anymore.
    //
    (void) LocalFree((HLOCAL) LsaSecretName);
    (void) LsaClose(PolicyHandle);

    if (status != NO_ERROR) {
        KdPrint(("NWWORKSTATION: NwDeletePassword: NwOpenSecret failed\n"));
        return status;
    }

    ntstatus = LsaDelete(SecretHandle);

    if (! NT_SUCCESS(ntstatus)) {
        KdPrint(("NWWORKSTATION: NwDeletePassword: LsaDelete returned %08lx\n",
                 ntstatus));
        (void) LsaClose(SecretHandle);
        return RtlNtStatusToDosError(ntstatus);
    }

    return NO_ERROR;
}



DWORD
NwGetPassword(
    IN  LPWSTR Qualifier,
    OUT PUNICODE_STRING *Password,
    OUT PUNICODE_STRING *OldPassword
    )
/*++

Routine Description:

    This function retrieves the current password and old password
    values from the service secret object given the user name.

Arguments:

    Qualifier - Supplies the qualifier which forms part of the key to the
        secret object name.

    Password - Receives a pointer to the string structure that contains
        the password.

    OldPassword - Receives a pointer to the string structure that
        contains the old password.

Return Value:

    NO_ERROR - Secret object for password is changed to new value.

    Error from LSA.

--*/
{
    DWORD status;
    NTSTATUS ntstatus;

    LSA_HANDLE PolicyHandle;
    LSA_HANDLE SecretHandle;

    LPWSTR LsaSecretName;


    //
    // Open a handle to the local security policy to read the
    // value of the secret.
    //
    if ((status = NwOpenPolicy(
                      POLICY_VIEW_LOCAL_INFORMATION,
                      &PolicyHandle
                      )) != NO_ERROR) {
        return status;
    }

    //
    // Get the secret object name from the specified user name.
    //
    if ((status = NwFormSecretName(
                      Qualifier,
                      &LsaSecretName
                      )) != NO_ERROR) {
        (void) LsaClose(PolicyHandle);
        return status;
    }

    status = NwOpenSecret(
                 SECRET_QUERY_VALUE,
                 PolicyHandle,
                 LsaSecretName,
                 &SecretHandle
                 );

    //
    // Don't need the name or policy handle anymore.
    //
    (void) LocalFree((HLOCAL) LsaSecretName);
    (void) LsaClose(PolicyHandle);

    if (status != NO_ERROR) {
        KdPrint(("NWWORKSTATION: ScGetSecret: ScOpenSecret failed\n"));
        return status;
    }

    //
    // Query the old value of the secret object so that we can
    // we can restore it if we fail to change the password later.
    //
    ntstatus = LsaQuerySecret(
                   SecretHandle,
                   Password,
                   NULL,                   // don't need set time
                   OldPassword,
                   NULL                    // don't need set time
                   );

    (void) LsaClose(SecretHandle);

    if (! NT_SUCCESS(ntstatus)) {
        KdPrint(("NWWORKSTATION: NwGetPassword: LsaQuerySecret for previous values returned %08lx\n",
                 ntstatus));

        return RtlNtStatusToDosError(ntstatus);
    }

    return NO_ERROR;
}