summaryrefslogblamecommitdiffstats
path: root/private/net/portuas/alias.c
blob: 12ebf38d00aa04ad5568f27d9512584599ed17f6 (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
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





































































































































































































































































































































































































































































































































































































                                                                                        
/*++

Copyright (c) 1992-1993  Microsoft Corporation

Module Name:

    Alias.c

Abstract:

    NT alias functions:
        PortUasAliasSetup() -- one-time init of static data
        PortUasAddUserToAliases()
        PortUasAliasCleanup()

Author:

    John Rogers (JohnRo) 24-Apr-1992

Revision History:

    24-Apr-1992 JohnRo
        Created.
    05-May-1992 JohnRo
        Fixed desired _access parm to SamConnect() and SamOpenAlias().
        Fixed misleading msg when SamOpenDomain() fails.
        Call NetpGetLocalDomainId() instead of NetpGetDomainId() so we can get
        the kind that SamOpenDomain() wants.
        Only _open aliases for this product type.
    09-Jun-1992 JohnRo
        RAID 10139: PortUAS should add to admin group/alias.
        Fixed PortUasAliasSetup's error handling.
    27-Jan-1993 JohnRo
        RAID 8683: PortUAS should set primary group from Mac parms.
        Use NetLib's get product type function.
    01-Jun-1993 JohnRo
        Made changes suggested by PC-LINT 5.0

--*/


// Uncomment this to enable comm ops alias:
//#define USE_COMM_OPS


#include <nt.h>         // NTSTATUS, NT_SUCCESS(), etc.
#include <ntrtl.h>      // (Needed with nt.h and windows.h)
#include <nturtl.h>     // (Needed with ntrtl.h and windows.h)
#include <windows.h>    // LocalFree(), etc.
#include <lmcons.h>

#include <lmaccess.h>   // UF_ equates, LPUSER_INFO_2, etc.
#include <lmerr.h>      // NO_ERROR, NERR_ equates.
#include <netdebug.h>   // DBGSTATIC, NetpAssert(), etc.
#include <netlib.h>     // NetpGetProductType(), NetpMemoryFree().
#include <netlibnt.h>   // NetpNtStatusToApiStatus().
#include <ntsam.h>      // SAM_HANDLE, SamConnect(), etc.
#include <secobj.h>     // NetpDomainIdToSid().
#include <portuasp.h>   // My prototypes, UNEXPECTED_MSG(), PortUasSam*, etc.

#include "nlstxt.h"     // Nls message ID codes.


//#define OUR_ALIAS_DESIRED_ACCESS (ALIAS_ADD_MEMBER | STANDARD_RIGHTS_REQUIRED)
#define OUR_ALIAS_DESIRED_ACCESS  ALIAS_ADD_MEMBER

//#define OUR_GROUP_DESIRED_ACCESS (GROUP_ADD_MEMBER | STANDARD_RIGHTS_REQUIRED)
#define OUR_GROUP_DESIRED_ACCESS  GROUP_ADD_MEMBER


//int      _wcsicmp(const wchar_t *string1, const wchar_t *string2);

//
// State data (misc):
//
DBGSTATIC BOOL PortUasTargetIsLanmanNt;

//
// State data (alias handles and group handles):
//
DBGSTATIC SAM_HANDLE PortUasSamAccountOpsAliasHandle = NULL;
DBGSTATIC SAM_HANDLE PortUasSamAdminsAliasHandle = NULL;
DBGSTATIC SAM_HANDLE PortUasSamAdminsGroupHandle = NULL;
#ifdef USE_COMM_OPS
DBGSTATIC SAM_HANDLE PortUasSamCommOpsAliasHandle = NULL;
#endif
DBGSTATIC SAM_HANDLE PortUasSamPowerUsersAliasHandle = NULL;
DBGSTATIC SAM_HANDLE PortUasSamPrintOpsAliasHandle = NULL;
DBGSTATIC SAM_HANDLE PortUasSamServerOpsAliasHandle = NULL;


NET_API_STATUS
PortUasAliasSetup(
    VOID
    )

/*++

Routine Description:

    PortUas ports account information in a LanMan 2.0 UAS database into
    the SAM database.

Arguments:

    UasPathName - supplies the path name to the UAS database file.

Return Value:

    NET_API_STATUS - NERR_Success if successful, or one of the following:

--*/

{
    NET_API_STATUS  ApiStatus;
    NTSTATUS        NtStatus;
    NT_PRODUCT_TYPE ProductType;

    //
    // Caller should have already set global domain IDs and SAM domain handles.
    //

    NetpAssert( PortUasAccountsDomainId != NULL );
    NetpAssert( PortUasBuiltinDomainId  != NULL );

    NetpAssert( PortUasSamAccountsDomainHandle != NULL );
    NetpAssert( PortUasSamBuiltinDomainHandle  != NULL );

    //
    // Decide on policy: should we combine operator accounts into the
    // "power user" alias (on WindowsNT) or leave them in seperate
    // aliases (on LanManNT)?
    //

    ApiStatus = NetpGetProductType(
            NULL,               // local (no server name)
            &ProductType );     // get result here.
    if (ApiStatus != NO_ERROR) {

        UNEXPECTED_MSG( "NetpGetProductType", ApiStatus );
        goto SetupError;
    }
    if (ProductType == NtProductLanManNt) {
        PortUasTargetIsLanmanNt = TRUE;
    } else {
        PortUasTargetIsLanmanNt = FALSE;
    }
    if (Verbose) {
        DEBUG_MSG( (PREFIX_PORTUAS "Product type is " FORMAT_DWORD
                ", IsLanmanNt is " FORMAT_DWORD ".\n", ProductType,
                (DWORD) PortUasTargetIsLanmanNt) );
    }

    //
    // Open the well-known aliases and/or groups for this product type.
    //
    if (PortUasTargetIsLanmanNt) {

        NtStatus = SamOpenAlias(
                PortUasSamBuiltinDomainHandle,  // aliases live in builtin dom.
                OUR_ALIAS_DESIRED_ACCESS,
                DOMAIN_ALIAS_RID_ACCOUNT_OPS,   // alias ID
                & PortUasSamAccountOpsAliasHandle );  // alias handle
        if ( !NT_SUCCESS( NtStatus ) ) {
            ApiStatus = NetpNtStatusToApiStatus( NtStatus );
            UNEXPECTED_MSG( "SamOpenAlias(account)", ApiStatus );
            goto SetupError;
        }
        NetpAssert( PortUasSamAccountOpsAliasHandle != NULL );

        NtStatus = SamOpenGroup(
                PortUasSamAccountsDomainHandle,   // groups live in accts domain
                OUR_GROUP_DESIRED_ACCESS,
                DOMAIN_GROUP_RID_ADMINS,          // alias ID
                & PortUasSamAdminsGroupHandle );  // alias handle
        if ( !NT_SUCCESS( NtStatus ) ) {
            ApiStatus = NetpNtStatusToApiStatus( NtStatus );
            UNEXPECTED_MSG( "SamOpenGroup(admins global group)", ApiStatus );
            goto SetupError;
        }
        NetpAssert( PortUasSamAdminsGroupHandle != NULL );

        NtStatus = SamOpenAlias(
                PortUasSamBuiltinDomainHandle,
                OUR_ALIAS_DESIRED_ACCESS,
                DOMAIN_ALIAS_RID_PRINT_OPS,   // alias ID
                & PortUasSamPrintOpsAliasHandle );  // alias handle
        if ( !NT_SUCCESS( NtStatus ) ) {
            ApiStatus = NetpNtStatusToApiStatus( NtStatus );
            UNEXPECTED_MSG( "SamOpenAlias(print)", ApiStatus );
            goto SetupError;

        }
        NetpAssert( PortUasSamPrintOpsAliasHandle != NULL );

        NtStatus = SamOpenAlias(
                PortUasSamBuiltinDomainHandle,
                OUR_ALIAS_DESIRED_ACCESS,
                DOMAIN_ALIAS_RID_SYSTEM_OPS,   // alias ID
                & PortUasSamServerOpsAliasHandle );  // alias handle
        if ( !NT_SUCCESS( NtStatus ) ) {
            ApiStatus = NetpNtStatusToApiStatus( NtStatus );
            UNEXPECTED_MSG( "SamOpenAlias(server)", ApiStatus );
            goto SetupError;
        }
        NetpAssert( PortUasSamServerOpsAliasHandle != NULL );

    } else {

        // Target must be Windows/NT (not Lanman/NT), which changes policy.

        NtStatus = SamOpenAlias(
                PortUasSamBuiltinDomainHandle,
                OUR_ALIAS_DESIRED_ACCESS,
                DOMAIN_ALIAS_RID_ADMINS ,   // alias ID
                & PortUasSamAdminsAliasHandle );  // alias handle
        if ( !NT_SUCCESS( NtStatus ) ) {
            ApiStatus = NetpNtStatusToApiStatus( NtStatus );
            UNEXPECTED_MSG( "SamOpenAlias(admins local group)", ApiStatus );
            goto SetupError;
        }
        NetpAssert( PortUasSamAdminsAliasHandle != NULL );

        NtStatus = SamOpenAlias(
                PortUasSamBuiltinDomainHandle,
                OUR_ALIAS_DESIRED_ACCESS,
                DOMAIN_ALIAS_RID_POWER_USERS,   // alias ID
                & PortUasSamPowerUsersAliasHandle );  // alias handle
        if ( !NT_SUCCESS( NtStatus ) ) {
            ApiStatus = NetpNtStatusToApiStatus( NtStatus );
            UNEXPECTED_MSG( "SamOpenAlias(power users)", ApiStatus );
            goto SetupError;
        }
        NetpAssert( PortUasSamPowerUsersAliasHandle != NULL );

    }

    return (NO_ERROR);

SetupError:

    //
    // Come here to process errors.  ApiStatus has already been set.
    //

    NetpAssert( ApiStatus != NO_ERROR );

    (VOID) PortUasAliasCleanup();

    return (ApiStatus);

} // PortUasAliasSetup


DBGSTATIC NET_API_STATUS
PortUasAddUserToAlias(
    IN PSID UserSid,
    IN SAM_HANDLE AliasHandle
    )
{
    NET_API_STATUS ApiStatus;
    NTSTATUS NtStatus;

    NetpAssert( UserSid != NULL );
    NetpAssert( AliasHandle != NULL );

    NtStatus = SamAddMemberToAlias(
            AliasHandle,
            UserSid );

    if ( !NT_SUCCESS( NtStatus ) ) {
        ApiStatus = NetpNtStatusToApiStatus( NtStatus );

        // Allow repeated runs of PortUAS...
        if (ApiStatus == ERROR_MEMBER_IN_ALIAS) {
            return (NO_ERROR);
        }

        UNEXPECTED_MSG( "SamAddMemberToAlias", ApiStatus );
        return (ApiStatus);
    }

    return (NO_ERROR);
}


NET_API_STATUS
PortUasNameToRid(
    IN  LPCTSTR      Name,      // may be user or group name.
    IN  SID_NAME_USE ExpectedType,
    OUT PULONG       UserRid
    )
{
    NET_API_STATUS ApiStatus;
    PSID_NAME_USE  NameUse;
    NTSTATUS       NtStatus;
    UNICODE_STRING UnicodeName;
    PULONG         TempRid;

    NetpAssert( Name != NULL );
    NetpAssert( (*Name) != NULLC );
    NetpAssert( UserRid != NULL );

    NetpAssert( PortUasSamAccountsDomainHandle != NULL );

    //
    // Get a RID for this user name.
    //
    RtlInitUnicodeString(
            & UnicodeName,          // dest (NT struct)
            Name );             // src (null-terminated)

    NtStatus = SamLookupNamesInDomain(
            PortUasSamAccountsDomainHandle,  // users live in accounts domain
            (ULONG) 1,               // only want one name.
            &UnicodeName,            // name (in NT struct)
            &TempRid,                // alloc and set RIDs.
            &NameUse );              // alloc and set name types.

    if ( !NT_SUCCESS( NtStatus ) ) {
        ApiStatus = NetpNtStatusToApiStatus( NtStatus );
        UNEXPECTED_MSG( "SamLookupNamesInDomain(accounts)", ApiStatus );
        DEBUG_MSG( (PREFIX_PORTUAS "SAM lookup(accounts) of "
                FORMAT_LPWSTR " failed.\n", Name) );
        return (ApiStatus);
    }

    NetpAssert( NameUse != NULL );
    NetpAssert( TempRid != NULL );

    *UserRid = *TempRid;

    //
    // Did type user wanted match the actual one?
    //
    if (ExpectedType != *NameUse) {
        NetpAssert( FALSE );                   // BUGBUG: Is this possible?
        ApiStatus = ERROR_INVALID_PARAMETER;   // BUGBUG: A better error code?
        goto Cleanup;
    }

    ApiStatus = NO_ERROR;

Cleanup:
    //
    // Free memory which SAM allocated for us.
    //
    NtStatus = SamFreeMemory( NameUse );
    if ( !NT_SUCCESS( NtStatus ) ) {
        ApiStatus = NetpNtStatusToApiStatus( NtStatus );
        UNEXPECTED_MSG( "SamFreeMemory(use)", ApiStatus );
        return (ApiStatus);
    }

    NtStatus = SamFreeMemory( TempRid );
    if ( !NT_SUCCESS( NtStatus ) ) {
        ApiStatus = NetpNtStatusToApiStatus( NtStatus );
        UNEXPECTED_MSG( "SamFreeMemory(rid)", ApiStatus );
        return (ApiStatus);
    }

    return (ApiStatus);

} // PortUasNameToRid


DBGSTATIC NET_API_STATUS
PortUasUserNameToRidAndSid(
    IN  LPCWSTR UserName,
    OUT PULONG  UserRid,
    OUT PSID *  UserSid
    )
{
    NET_API_STATUS ApiStatus;

    NetpAssert( UserName != NULL );
    NetpAssert( (*UserName) != NULLC );
    NetpAssert( UserRid != NULL );
    NetpAssert( UserSid != NULL );

    NetpAssert( PortUasSamAccountsDomainHandle != NULL );

    //
    // Get a RID for this user name.
    //
    ApiStatus = PortUasNameToRid(
            (LPCTSTR) UserName,
            SidTypeUser,            // expected type
            UserRid );              // set RID for caller.
    if (ApiStatus != NO_ERROR) {
        UNEXPECTED_MSG( "PortUasNameToRid", ApiStatus );
        return (ApiStatus);
    }

    //
    // Convert the RID to a SID.
    //

    ApiStatus = NetpDomainIdToSid(
            PortUasAccountsDomainId,  // user IDs live in accounts domain
            *UserRid,
            UserSid );
    if (ApiStatus != NO_ERROR) {
        UNEXPECTED_MSG( "NetpDomainIdToSid", ApiStatus );
        return (ApiStatus);
    }
    NetpAssert( (*UserSid) != NULL );

    return (NO_ERROR);

} // PortUasUserNameToRidAndSid


NET_API_STATUS
PortUasAddUserToAliases(
    IN LPCWSTR UserName,
    IN DWORD Priv,              // USER_PRIV_ values from lmaccess.h
    IN DWORD AuthFlags          // AF_ values from lmaccess.h
    )
{
    NET_API_STATUS ApiStatus;
    NTSTATUS NtStatus;
    ULONG UserRid;
    PSID UserSid;

    NetpAssert( UserName != NULL );
    NetpAssert( (*UserName) != NULLC );

    //
    // If it's an ordinary user (or guest), ignore it.
    //
    if ( (AuthFlags == 0) && (Priv != USER_PRIV_ADMIN) ) {
        return (NO_ERROR);
    }

    //
    // Convert UserName to UserSid.
    //
    ApiStatus = PortUasUserNameToRidAndSid(
            UserName,
            &UserRid,                  // set RID for this user.
            &UserSid );                // alloc and set ptr for SID.
    if (ApiStatus != NO_ERROR) {
        return (ApiStatus);
    }
    NetpAssert( UserSid != NULL );

    if (Priv == USER_PRIV_ADMIN) {
        //
        // Depending on product type, add to admins local group or admins
        // global group.
        //
        if (PortUasTargetIsLanmanNt) {
            //PROGRESS_MSG( ("Adding user " FORMAT_LPWSTR
            //       " to admins global group...\n", UserName) );
	    (void)NlsPutMsg(STDOUT, PUAS_ADDING_USER_TO_ADMINS_GLOBAL_GROUP, UserName );
            NtStatus = SamAddMemberToGroup(
                    PortUasSamAdminsGroupHandle,
                    UserRid,
                    SE_GROUP_MANDATORY | SE_GROUP_ENABLED_BY_DEFAULT |
                        SE_GROUP_ENABLED );

            if ( !NT_SUCCESS( NtStatus ) ) {
                ApiStatus = NetpNtStatusToApiStatus( NtStatus );

                // Allow repeated runs of PortUAS...
                if (ApiStatus == NERR_UserInGroup) {
                    return (NO_ERROR);
                }

                UNEXPECTED_MSG( "SamAddMemberToGroup", ApiStatus );
                return (ApiStatus);
            }
        } else {
            //PROGRESS_MSG( ("Adding user " FORMAT_LPWSTR
            //       " to admins local group...\n", UserName) );
	    (void)NlsPutMsg(STDOUT, PUAS_ADDING_USER_TO_ADMINS_LOCAL_GROUP, UserName );
            ApiStatus = PortUasAddUserToAlias( UserSid,
                    PortUasSamAdminsAliasHandle );
            if (ApiStatus != NO_ERROR) {
                // Msg already logged.
                return (ApiStatus);
            }
        }
    }

    //
    // Add user (operator) to applicable alias(es).
    //
    if (PortUasTargetIsLanmanNt) {

        if (AuthFlags & AF_OP_ACCOUNTS) {
            //PROGRESS_MSG( ("Adding user " FORMAT_LPWSTR " to acct ops alias.\n",
            //       UserName) );
	    (void)NlsPutMsg(STDOUT, PUAS_ADDING_USER_TO_ACCT_OPS_ALIAS, UserName );
            ApiStatus = PortUasAddUserToAlias( UserSid,
                    PortUasSamAccountOpsAliasHandle );
            if (ApiStatus != NO_ERROR) {
                // Msg already logged.
                return (ApiStatus);
            }
        }

        if (AuthFlags & AF_OP_COMM) {

#ifdef USE_COMM_OPS
            //PROGRESS_MSG( ("Adding user " FORMAT_LPWSTR " to comm ops alias.\n",
            //       UserName) );
	    (void)NlsPutMsg(STDOUT, PUAS_ADDING_USER_TO_COMM_OPS_ALIAS, UserName );
            ApiStatus = PortUasAddUserToAlias( UserSid,
                    PortUasSamCommOpsAliasHandle );
            if (ApiStatus != NO_ERROR) {
                // Msg already logged.
                return (ApiStatus);
            }
#else
            //WARNING_MSG( ("Ignoring comm operator flag for " FORMAT_LPWSTR
            //       ".\n", UserName) );
	    (void)NlsPutMsg(STDOUT,PUAS_IGNORING_COMM_OPERATOR_FLAG, UserName);
#endif
        }

        if (AuthFlags & AF_OP_PRINT) {
            //PROGRESS_MSG( ("Adding user " FORMAT_LPWSTR " to print alias.\n",
            //       UserName) );
	    (void)NlsPutMsg(STDOUT, PUAS_ADDING_USER_TO_PRINT_ALIAS, UserName );
            ApiStatus = PortUasAddUserToAlias( UserSid,
                    PortUasSamPrintOpsAliasHandle );
            if (ApiStatus != NO_ERROR) {
                // Msg already logged.
                return (ApiStatus);
            }
        }

        if (AuthFlags & AF_OP_SERVER) {
            //PROGRESS_MSG( ("Adding user " FORMAT_LPWSTR " to srv ops alias.\n",
            //       UserName) );
	    (void)NlsPutMsg(STDOUT, PUAS_ADDING_USER_TO_SRV_OPS_ALIAS, UserName );
            ApiStatus = PortUasAddUserToAlias( UserSid,
                    PortUasSamServerOpsAliasHandle );
            if (ApiStatus != NO_ERROR) {
                // Msg already logged.
                return (ApiStatus);
            }
        }

    } else {

        ApiStatus = PortUasAddUserToAlias( UserSid,
                PortUasSamPowerUsersAliasHandle );
        if (ApiStatus != NO_ERROR) {
            // Msg already logged.
            return (ApiStatus);
        }
    }

    NetpMemoryFree( UserSid );   // Free mem alloc'ed by PortUasUserNameToSid().

    NetpAssert( ApiStatus == NO_ERROR );
    return (NO_ERROR);
}


NET_API_STATUS
PortUasAliasCleanup(
    VOID
    )
{

    CLOSE_SAM_HANDLE( PortUasSamAdminsAliasHandle );
    CLOSE_SAM_HANDLE( PortUasSamAdminsGroupHandle );
    CLOSE_SAM_HANDLE( PortUasSamAccountOpsAliasHandle );
#ifdef USE_COMM_OPS
    CLOSE_SAM_HANDLE( PortUasSamCommOpsAliasHandle );
#endif
    CLOSE_SAM_HANDLE( PortUasSamPowerUsersAliasHandle );
    CLOSE_SAM_HANDLE( PortUasSamPrintOpsAliasHandle );
    CLOSE_SAM_HANDLE( PortUasSamServerOpsAliasHandle );

    return (NO_ERROR);

} // PortUasAliasCleanup