summaryrefslogblamecommitdiffstats
path: root/private/windows/gina/msgina/lsa.c
blob: 4edbcd896f2571f4f0f6471a21f35f7e3d32dd1f (plain) (tree)
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




























































































































































































































































































































































































































































                                                                                                                                               

/****************************************************************************

   PROGRAM: LSA.C

   PURPOSE: Utility routines that access the LSA.

****************************************************************************/

#include "msgina.h"



// #define DEBUG_LSA

#ifdef DEBUG_LSA
#define VerbosePrint(s) WLPrint(s)
#else
#define VerbosePrint(s)
#endif


/***************************************************************************\
* OpenLsaOnController
*
* Purpose : Attempts to open the Lsa on the specified domain controller.
*           If the open is successful, this routine checks that the
*           controller is still a controller for the specified domain
*
* Notes: If the controller name is NULL, the local Lsa is opened.
*
* Returns : TRUE on success, FALSE on failure.
*
* Notes: Desired access must include POLICY_VIEW_LOCAL_INFO
*
* History:
* 11-03-92 Davidc       Created.
\***************************************************************************/
BOOL
OpenLsaOnController(
    PUNICODE_STRING ControllerName OPTIONAL,
    ACCESS_MASK DesiredAccess,
    PUNICODE_STRING PrimaryDomainName,
    PLSA_HANDLE LsaHandle
    )
{
    NTSTATUS Status, IgnoreStatus;
    OBJECT_ATTRIBUTES ObjectAttributes;
    SECURITY_QUALITY_OF_SERVICE SecurityQualityOfService;
    PPOLICY_PRIMARY_DOMAIN_INFO PrimaryDomainInfo;
    PPOLICY_ACCOUNT_DOMAIN_INFO AccountDomainInfo;
    BOOL Success = FALSE;

    //
    // Attempt to open the Lsa on the controller
    //

    SecurityQualityOfService.Length = sizeof(SECURITY_QUALITY_OF_SERVICE);
    SecurityQualityOfService.ImpersonationLevel = SecurityImpersonation;
    SecurityQualityOfService.ContextTrackingMode = SECURITY_DYNAMIC_TRACKING;
    SecurityQualityOfService.EffectiveOnly = FALSE;

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

    Status = LsaOpenPolicy(
                 ControllerName,
                 &ObjectAttributes,
                 DesiredAccess,
                 LsaHandle
                 );

    if (!NT_SUCCESS(Status)) {
        DebugLog((DEB_TRACE, "Failed to open lsa on <%wZ>, status = 0x%lx\n", ControllerName, Status));
        return(FALSE);
    }

    //
    // Check the controller is actually in this domain
    //

    ASSERT(DesiredAccess & POLICY_VIEW_LOCAL_INFORMATION);

    Status = LsaQueryInformationPolicy(*LsaHandle,
                                       PolicyPrimaryDomainInformation,
                                       (PVOID *)&PrimaryDomainInfo);
    if (!NT_SUCCESS(Status)) {
        DebugLog((DEB_TRACE, "Failed to query primary domain from Lsa on <%wZ>, Status = 0x%lx\n", ControllerName, Status));
    } else {

        //
        // Check the controller is in our domain
        //

        Success = RtlEqualUnicodeString(PrimaryDomainName,
                                        &PrimaryDomainInfo->Name,
                                        TRUE // Case insensitive
                                        );
        if (!Success) {
            DebugLog((DEB_TRACE, "Controller <%wZ> is not in our domain <%wZ>, ignoring it\n", ControllerName, PrimaryDomainName));
        }

        if (Success) {

            //
            // Computer is in the domain, is it still a DC?
            //

            Status = LsaQueryInformationPolicy(*LsaHandle,
                                               PolicyAccountDomainInformation,
                                               (PVOID *)&AccountDomainInfo);

            if ( NT_SUCCESS( Status )) {

                Success = RtlEqualUnicodeString(&AccountDomainInfo->DomainName,
                                                &PrimaryDomainInfo->Name,
                                                TRUE // Case insensitive
                                                );

                IgnoreStatus = LsaFreeMemory(AccountDomainInfo);
                ASSERT(NT_SUCCESS(IgnoreStatus));

            } else {

                Success = FALSE;
            }
        }

        //
        // Free up returned Lsa structure
        //

        IgnoreStatus = LsaFreeMemory(PrimaryDomainInfo);
        ASSERT(NT_SUCCESS(IgnoreStatus));

    }

    //
    // Clean up the Lsa handle on failure
    //

    if (!Success) {

        //
        // The following call may fail if RPC has invalidated the handle
        //

        IgnoreStatus = LsaClose(*LsaHandle);
    }

    return(Success);
}


/***************************************************************************\
* OpenLsaOnDomain
*
* Purpose : Opens the Lsa on a domain controller in the domain.
*
* Notes: On successful return, the caller should free the ControllerName
*        using RtlFreeUnicodeString()
*
* Returns : ThreadExitCode
*
* History:
* 11-12-92 Davidc       Created.
\***************************************************************************/
BOOL
OpenLsaOnDomain(
    PUNICODE_STRING PrimaryDomainName IN,
    ACCESS_MASK DesiredAccess IN,
    PUNICODE_STRING SuggestedControllerName IN OPTIONAL,
    PUNICODE_STRING ControllerName OUT,
    PLSA_HANDLE ControllerHandle OUT
    )
{
    NT_PRODUCT_TYPE NtProductType;

    //
    // Find out what product we are installed as
    // This always defaults to something useful even on failure
    //

    RtlGetNtProductType(&NtProductType);


    //
    // Prepare for failure
    //

    *ControllerHandle = NULL;
    RtlInitUnicodeString(ControllerName, NULL);

    if (!IsWorkstation(NtProductType)) {

        //
        // LanmanNT machine - controller is local machine
        //

        if (!OpenLsaOnController( NULL,
                                  DesiredAccess,
                                  PrimaryDomainName,
                                  ControllerHandle)) {

            DebugLog((DEB_TRACE, "Failed to open local lsa, desired access = 0x%lx\n", DesiredAccess));
            *ControllerHandle = NULL;
            return(FALSE);

        }
        return(TRUE);

    } else {

        //
        // WinNT machine - try the suggested controller, if that fails
        // use I_NetGetDCList to get controller list and try each one
        // until we open the Lsa successfully.
        //

        DWORD Error;
        LPBYTE DCNameBuffer;
        BOOLEAN Result;
        UNICODE_STRING DCName;


        //
        // Wait for the network to start
        //

        if (!WaitForNetworkToStart(SERVICE_NETLOGON)) {
            DebugLog((DEB_ERROR, "Failed to wait for network to start\n"));
            return(FALSE);
        }

        //
        // Try suggested controller
        // Don't bother if it's the local LSA
        //

        if ( SuggestedControllerName &&
            (SuggestedControllerName->Buffer != NULL) &&
            (SuggestedControllerName->Length != 0) ) {

            if (OpenLsaOnController( SuggestedControllerName,
                                     DesiredAccess,
                                     PrimaryDomainName,
                                     ControllerHandle)) {
                //
                // Success - the suggested controller came up trumps
                //

                if (!DuplicateUnicodeString(ControllerName,
                                            SuggestedControllerName)) {
                    RtlInitUnicodeString(ControllerName, NULL);
                }

                DebugLog((DEB_TRACE, "Successfully opened Lsa on suggested controller\n"));

                return(TRUE);

            } else {

                DebugLog((DEB_TRACE, "Failed to open Lsa on suggested controller <%wZ>, trying other controllers\n", SuggestedControllerName));
                *ControllerHandle = NULL;
            }
        }


        //
        // Go get the list of domain controllers for the domain
        //

        ASSERT(PrimaryDomainName->Length < PrimaryDomainName->MaximumLength);
        PrimaryDomainName->Buffer[ PrimaryDomainName->Length/
                                   sizeof(*(PrimaryDomainName->Buffer)) ] = 0;


        Error = NetGetAnyDCName (
                    NULL,
                    PrimaryDomainName->Buffer,
                    &DCNameBuffer
                    );

        if (Error != ERROR_SUCCESS) {
            DebugLog((DEB_ERROR, "NetGetAnyDCName failed, error = %d\n", Error));
            return(FALSE);
        }

        RtlInitUnicodeString( &DCName, (PCWSTR)DCNameBuffer );

        //
        // Attempt to open the LSA for one of the controllers on the list.
        // For now, just scan the list from the end backwards.  Later,
        // use a more random method.  The active Domain Controller is
        // at the beginning of the list.
        //

        DebugLog((DEB_TRACE, "Trying to open Lsa on controller <%wZ>\n", &DCName));

        Result =  OpenLsaOnController( &DCName,
                                       DesiredAccess,
                                       PrimaryDomainName,
                                       ControllerHandle);

        if ( Result ) {
            if (!DuplicateUnicodeString(ControllerName,
                                        &DCName)) {
                RtlInitUnicodeString(ControllerName, NULL);
            }
        } else {
            DebugLog((DEB_TRACE, "Unable to open Lsa on controller <%s>\n", DCNameBuffer));
        }

        Error = NetApiBufferFree(DCNameBuffer);
        ASSERT( Error == NO_ERROR );

        return( Result );
    }
}



/***************************************************************************\
* GetPrimaryDomain
*
* Purpose : Returns the primary domain name for authentication
*
* Returns : TRUE if primary domain exists and returned, otherwise FALSE
*
* The primary domain name should be freed using RtlFreeUnicodeString().
* The primary domain sid should be freed using Free()
*
* History:
* 02-13-92 Davidc       Created.
\***************************************************************************/
BOOL
GetPrimaryDomain(
    PUNICODE_STRING PrimaryDomainName,
    PSID    *PrimaryDomainSid OPTIONAL
    )
{
    NTSTATUS Status, IgnoreStatus;
    OBJECT_ATTRIBUTES ObjectAttributes;
    LSA_HANDLE LsaHandle;
    SECURITY_QUALITY_OF_SERVICE SecurityQualityOfService;
    PPOLICY_PRIMARY_DOMAIN_INFO PrimaryDomainInfo;
    BOOL    PrimaryDomainPresent = FALSE;

    //
    // Set up the Security Quality Of Service
    //

    SecurityQualityOfService.Length = sizeof(SECURITY_QUALITY_OF_SERVICE);
    SecurityQualityOfService.ImpersonationLevel = SecurityImpersonation;
    SecurityQualityOfService.ContextTrackingMode = SECURITY_DYNAMIC_TRACKING;
    SecurityQualityOfService.EffectiveOnly = FALSE;

    //
    // Set up the object attributes to open the Lsa policy object
    //

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

    //
    // Open the local LSA policy object
    //

    Status = LsaOpenPolicy( NULL,
                            &ObjectAttributes,
                            POLICY_VIEW_LOCAL_INFORMATION,
                            &LsaHandle
                          );
    if (!NT_SUCCESS(Status)) {
        DebugLog((DEB_ERROR, "Failed to open local LsaPolicyObject, Status = 0x%lx\n", Status));
        return(FALSE);
    }

    //
    // Get the primary domain info
    //
    Status = LsaQueryInformationPolicy(LsaHandle,
                                       PolicyPrimaryDomainInformation,
                                       (PVOID *)&PrimaryDomainInfo);
    if (!NT_SUCCESS(Status)) {
        DebugLog((DEB_ERROR, "Failed to query primary domain from Lsa, Status = 0x%lx\n", Status));

        IgnoreStatus = LsaClose(LsaHandle);
        ASSERT(NT_SUCCESS(IgnoreStatus));

        return(FALSE);
    }

    //
    // Copy the primary domain name into the return string
    //

    if (PrimaryDomainInfo->Sid != NULL) {

        PrimaryDomainPresent = TRUE;

        if (PrimaryDomainName)
        {

            if (DuplicateUnicodeString(PrimaryDomainName, &(PrimaryDomainInfo->Name))) {

                if (PrimaryDomainSid != NULL) {

                    ULONG SidLength = RtlLengthSid(PrimaryDomainInfo->Sid);

                    *PrimaryDomainSid = Alloc(SidLength);
                    if (*PrimaryDomainSid != NULL) {

                        Status = RtlCopySid(SidLength, *PrimaryDomainSid, PrimaryDomainInfo->Sid);
                        ASSERT(NT_SUCCESS(Status));

                    } else {
                        RtlFreeUnicodeString(PrimaryDomainName);
                        PrimaryDomainPresent = FALSE;
                    }
                }

            } else {
                PrimaryDomainPresent = FALSE;
            }
        }
    }

    //
    // We're finished with the Lsa
    //

    IgnoreStatus = LsaFreeMemory(PrimaryDomainInfo);
    ASSERT(NT_SUCCESS(IgnoreStatus));

    IgnoreStatus = LsaClose(LsaHandle);
    ASSERT(NT_SUCCESS(IgnoreStatus));


    return(PrimaryDomainPresent);
}