Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1167 lines
33 KiB

  1. /*************************************************************************
  2. *
  3. * security.c
  4. *
  5. * NT Security routines
  6. *
  7. * copyright notice: Copyright 1997, Microsoft
  8. *
  9. *************************************************************************/
  10. #include <nt.h>
  11. #include <ntrtl.h>
  12. #include <nturtl.h>
  13. #include <ntexapi.h>
  14. #include <ntseapi.h>
  15. #include <stdlib.h>
  16. #include <stdio.h>
  17. #include <malloc.h>
  18. #include <windows.h>
  19. #include <string.h>
  20. #include <wcstr.h>
  21. #include "seopaque.h"
  22. #include "sertlp.h"
  23. #include <process.h>
  24. #include <winsta.h>
  25. #include <syslib.h>
  26. #include <lmcons.h> // for DNLEN KLB 09-18-96
  27. #include "security.h"
  28. #if DBG
  29. ULONG
  30. DbgPrint(
  31. PCH Format,
  32. ...
  33. );
  34. #define DBGPRINT(x) DbgPrint x
  35. #if DBGTRACE
  36. #define TRACE0(x) DbgPrint x
  37. #define TRACE1(x) DbgPrint x
  38. #else
  39. #define TRACE0(x)
  40. #define TRACE1(x)
  41. #endif
  42. #else
  43. #define DBGPRINT(x)
  44. #define TRACE0(x)
  45. #define TRACE1(x)
  46. #endif
  47. typedef struct _SIDLIST {
  48. struct _SIDLIST *Next;
  49. USHORT SidCrc;
  50. PWCHAR pAccountName;
  51. PWCHAR pDomainName;
  52. } SIDLIST, *PSIDLIST;
  53. static PSIDLIST SidCache = NULL;
  54. // Computer Name
  55. WCHAR ComputerName[MAX_COMPUTERNAME_LENGTH+1];
  56. // Admins only ACL
  57. PACL pAdminsOnlyAcl = NULL;
  58. DWORD AdminsOnlyAclSize = 0;
  59. //
  60. // Universal well known SIDs
  61. //
  62. PSID SeNullSid;
  63. PSID SeWorldSid;
  64. PSID SeLocalSid;
  65. PSID SeCreatorOwnerSid;
  66. PSID SeCreatorGroupSid;
  67. //
  68. // Sids defined by NT
  69. //
  70. PSID SeNtAuthoritySid;
  71. PSID SeDialupSid;
  72. PSID SeNetworkSid;
  73. PSID SeBatchSid;
  74. PSID SeInteractiveSid;
  75. PSID SeServiceSid;
  76. PSID SeLocalGuestSid;
  77. PSID SeLocalSystemSid;
  78. PSID SeLocalAdminSid;
  79. PSID SeLocalManagerSid;
  80. PSID SeAliasAdminsSid;
  81. PSID SeAliasUsersSid;
  82. PSID SeAliasGuestsSid;
  83. PSID SeAliasPowerUsersSid;
  84. PSID SeAliasAccountOpsSid;
  85. PSID SeAliasSystemOpsSid;
  86. PSID SeAliasPrintOpsSid;
  87. PSID SeAliasBackupOpsSid;
  88. PSID SeAliasReplicatorSid;
  89. static SID_IDENTIFIER_AUTHORITY SepNullSidAuthority = SECURITY_NULL_SID_AUTHORITY;
  90. static SID_IDENTIFIER_AUTHORITY SepWorldSidAuthority = SECURITY_WORLD_SID_AUTHORITY;
  91. static SID_IDENTIFIER_AUTHORITY SepLocalSidAuthority = SECURITY_LOCAL_SID_AUTHORITY;
  92. static SID_IDENTIFIER_AUTHORITY SepCreatorSidAuthority = SECURITY_CREATOR_SID_AUTHORITY;
  93. static SID_IDENTIFIER_AUTHORITY SepNtAuthority = SECURITY_NT_AUTHORITY;
  94. //
  95. // Sid of primary domain, and admin account in that domain
  96. //
  97. PSID SepPrimaryDomainSid;
  98. PSID SepPrimaryDomainAdminSid;
  99. // External data
  100. extern ADMIN_ACCOUNTS AllowAccounts[];
  101. extern DWORD AllowAccountEntries;
  102. extern ACCESS_MASK AllowAccess;
  103. extern ADMIN_ACCOUNTS DenyAccounts[];
  104. extern DWORD DenyAccountEntries;
  105. extern ACCESS_MASK DeniedAccess;
  106. // Forward and external references
  107. BOOLEAN
  108. InitializeBuiltinSids();
  109. BOOL
  110. LoadAccountSids();
  111. BOOL
  112. BuildSecureAcl();
  113. BOOL
  114. xxxLookupBuiltinAccount(
  115. PWCHAR pSystemName,
  116. PWCHAR pAccountName,
  117. PSID *ppSid
  118. );
  119. BOOL
  120. GetCurrentUserSid( PSID *ppSid );
  121. /*****************************************************************************
  122. *
  123. * InitSecurity
  124. *
  125. * Initialize the security package
  126. *
  127. * ENTRY:
  128. * Param1 (input/output)
  129. * Comments
  130. *
  131. * EXIT:
  132. * TRUE - no error
  133. * FALSE - error
  134. *
  135. ****************************************************************************/
  136. BOOL
  137. InitSecurity(
  138. )
  139. {
  140. BOOL rc;
  141. DWORD Size = sizeof(ComputerName)/sizeof(WCHAR);
  142. rc = GetComputerNameW( ComputerName, &Size );
  143. if( !rc ) {
  144. DBGPRINT(("***ERROR*** %d Could not get computer name\n",GetLastError() ));
  145. return( FALSE );
  146. }
  147. rc = InitializeBuiltinSids();
  148. if( !rc ) {
  149. DBGPRINT(("***ERROR*** Some built-in accounts not loaded\n"));
  150. return( FALSE );
  151. }
  152. rc = LoadAccountSids();
  153. if( !rc ) {
  154. DBGPRINT(("***ERROR*** Some accounts not loaded\n"));
  155. return( FALSE );
  156. }
  157. rc = BuildSecureAcl();
  158. if( !rc ) {
  159. DBGPRINT(("***ERROR*** Could not build ACL\n"));
  160. return( FALSE );
  161. }
  162. return( TRUE );
  163. }
  164. /*****************************************************************************
  165. *
  166. * LoadAccountSids
  167. *
  168. * Load the list of account access SIDS.
  169. *
  170. *
  171. * ENTRY:
  172. * Param1 (input/output)
  173. * Comments
  174. *
  175. * EXIT:
  176. * STATUS_SUCCESS - no error
  177. *
  178. ****************************************************************************/
  179. BOOL
  180. LoadAccountSids()
  181. {
  182. BOOL rc, Final;
  183. NTSTATUS Status;
  184. SID_IDENTIFIER_AUTHORITY gSystemSidAuthority = SECURITY_NT_AUTHORITY;
  185. Final = TRUE;
  186. //
  187. // Get the SID's for all of the allow accounts
  188. //
  189. //
  190. // Get the SID of the built-in Administrators group
  191. //
  192. Status = RtlAllocateAndInitializeSid(
  193. &gSystemSidAuthority,
  194. 2,
  195. SECURITY_BUILTIN_DOMAIN_RID,
  196. DOMAIN_ALIAS_RID_ADMINS,
  197. 0,0,0,0,0,0,
  198. &(AllowAccounts[ADMIN_ACCOUNT].pSid));
  199. if (!NT_SUCCESS(Status)) {
  200. DBGPRINT(("SYSLIB: Couldn't allocate Administrators SID (0x%x)\n", Status ));
  201. Final = FALSE;
  202. }
  203. //
  204. // SYSTEM
  205. //
  206. Status = RtlAllocateAndInitializeSid(
  207. &gSystemSidAuthority,
  208. 1,
  209. SECURITY_LOCAL_SYSTEM_RID,
  210. 0,0,0,0,0,0,0,
  211. &(AllowAccounts[SYSTEM_ACCOUNT].pSid));
  212. if (!NT_SUCCESS(Status)) {
  213. DBGPRINT(("SYSLIB: Couldn't allocate SYSTEM SID (0x%x)\n", Status ));
  214. Final = FALSE;
  215. }
  216. //
  217. // Get the current user's Sid
  218. //
  219. //First, see if we run in SYSTEM context.
  220. //If we do, get current user SID from TERMSRV, otherwise use a user SID from process token.
  221. Final = FALSE;
  222. {
  223. HANDLE hToken;
  224. PTOKEN_USER pTokenUser = NULL;
  225. DWORD dwInfoLength = 0;
  226. if(OpenProcessToken(GetCurrentProcess(),TOKEN_QUERY,&hToken))
  227. {
  228. GetTokenInformation(hToken,TokenUser,pTokenUser,dwInfoLength,&dwInfoLength);
  229. if(dwInfoLength)
  230. {
  231. pTokenUser = (PTOKEN_USER)LocalAlloc(LPTR,dwInfoLength);
  232. if(pTokenUser)
  233. {
  234. if(GetTokenInformation(hToken,TokenUser,pTokenUser,dwInfoLength,&dwInfoLength))
  235. {
  236. if(EqualSid(AllowAccounts[SYSTEM_ACCOUNT].pSid,pTokenUser->User.Sid))
  237. {
  238. rc = xxxLookupAccountName(
  239. ComputerName,
  240. CURRENT_USER,
  241. &(AllowAccounts[USER_ACCOUNT].pSid)
  242. );
  243. if ( !rc )
  244. {
  245. // It might be a special builtin account
  246. rc = xxxLookupBuiltinAccount(
  247. ComputerName,
  248. CURRENT_USER,
  249. &(AllowAccounts[USER_ACCOUNT].pSid)
  250. );
  251. if( !rc )
  252. {
  253. DBGPRINT(("***WARNING*** Could not find SID for current user account, Error %d\n", GetLastError() ));
  254. }
  255. }
  256. if( rc )
  257. {
  258. Final = TRUE;
  259. }
  260. }
  261. else
  262. {
  263. DWORD dwSidLength = GetLengthSid(pTokenUser->User.Sid);
  264. AllowAccounts[USER_ACCOUNT].pSid = (PSID)LocalAlloc(LPTR,dwSidLength);
  265. if(AllowAccounts[USER_ACCOUNT].pSid)
  266. {
  267. CopyMemory(AllowAccounts[USER_ACCOUNT].pSid,pTokenUser->User.Sid,dwSidLength);
  268. Final = TRUE;
  269. }
  270. }
  271. }
  272. LocalFree(pTokenUser);
  273. }
  274. }
  275. CloseHandle(hToken);
  276. }
  277. }
  278. #ifdef notdef
  279. //
  280. // Lookup the SID's for all of the deny accounts
  281. //
  282. for( Index = 0; Index < DenyAccountEntries; Index++ ) {
  283. p = &DenyAccounts[Index];
  284. rc = xxxLookupAccountName(
  285. ComputerName,
  286. p->Name,
  287. &p->pSid
  288. );
  289. if( !rc ) {
  290. // It might be a special builtin account
  291. rc = xxxLookupBuiltinAccount(
  292. ComputerName,
  293. p->Name,
  294. &p->pSid
  295. );
  296. if( !rc ) {
  297. DBGPRINT(("***WARNING*** Could not find SID for account %ws Error %d\n",p->Name,GetLastError() ));
  298. Final = FALSE;
  299. }
  300. }
  301. }
  302. #endif
  303. // Lower level function outputs if any accounts are invalid
  304. return( Final );
  305. }
  306. /*****************************************************************************
  307. *
  308. * IsAllowSid
  309. *
  310. * Returns whether the supplied SID is in the allow group
  311. *
  312. *
  313. * ENTRY:
  314. * Param1 (input/output)
  315. * Comments
  316. *
  317. * EXIT:
  318. * STATUS_SUCCESS - no error
  319. *
  320. ****************************************************************************/
  321. BOOL
  322. IsAllowSid(
  323. PSID pSid
  324. )
  325. {
  326. DWORD Index;
  327. PADMIN_ACCOUNTS p;
  328. BOOL AllowSid = FALSE;
  329. for( Index = 0; Index < AllowAccountEntries; Index++ ) {
  330. p = &AllowAccounts[Index];
  331. if( p->pSid && EqualSid( pSid, p->pSid ) ) {
  332. // The Sid is for an allowed account
  333. AllowSid = TRUE;
  334. break;
  335. }
  336. }
  337. return( AllowSid );
  338. }
  339. /*****************************************************************************
  340. *
  341. * xxxLookupAccountName
  342. *
  343. * Wrapper to lookup the SID for a given account name
  344. *
  345. * Returns a pointer to the SID in newly allocated memory
  346. *
  347. * ENTRY:
  348. * Param1 (input/output)
  349. * Comments
  350. *
  351. * EXIT:
  352. * STATUS_SUCCESS - no error
  353. *
  354. ****************************************************************************/
  355. BOOL
  356. xxxLookupAccountName(
  357. PWCHAR pSystemName,
  358. PWCHAR pAccountName,
  359. PSID *ppSid
  360. )
  361. {
  362. BOOL rc;
  363. DWORD Size, DomainSize, Error;
  364. SID_NAME_USE Type;
  365. PWCHAR pDomain = NULL;
  366. PSID pSid = NULL;
  367. WCHAR Buf;
  368. WCHAR pCurrentUser[MAX_ACCOUNT_NAME]; // KLB 09-16-96
  369. DWORD dwCurrentUser = MAX_ACCOUNT_NAME; // KLB 09-16-96
  370. WCHAR pAccountToLookup[MAX_ACCOUNT_NAME+DNLEN+1]; // KLB 09-18-96
  371. LPWSTR pLogon32DomainName = NULL; // KLB 09-19-96
  372. WINSTATIONINFORMATION WinInfo; // KLB 09-30-96
  373. ULONG ReturnLength; // KLB 09-30-96
  374. Size = 0;
  375. DomainSize = 0;
  376. /*
  377. * Open the WinStation and Query its information.
  378. */
  379. memset( &WinInfo, 0, sizeof(WINSTATIONINFORMATION) );
  380. rc = WinStationQueryInformation( SERVERNAME_CURRENT,
  381. LOGONID_CURRENT,
  382. WinStationInformation,
  383. (PVOID)&WinInfo,
  384. sizeof(WINSTATIONINFORMATION),
  385. &ReturnLength );
  386. if ( !rc ) {
  387. DBGPRINT(("error querying winstation information %d.\n", GetLastError() ));
  388. return( FALSE );
  389. }
  390. if( ReturnLength != sizeof(WINSTATIONINFORMATION) ) {
  391. DBGPRINT(("winstation info version mismatch!\n"));
  392. return( FALSE );
  393. }
  394. if (lstrcmpi(pAccountName,CURRENT_USER) == 0) {
  395. lstrcpy( pAccountToLookup, WinInfo.Domain );
  396. lstrcat( pAccountToLookup, L"\\" );
  397. lstrcat( pAccountToLookup, WinInfo.UserName );
  398. } else {
  399. lstrcpy( pAccountToLookup, pAccountName );
  400. }
  401. DBGPRINT(( "looking up account %ws\n", pAccountToLookup ));
  402. rc = LookupAccountNameW(
  403. pSystemName,
  404. pAccountToLookup, // KLB 09-16-96
  405. &Buf, // pSid
  406. &Size,
  407. &Buf, // pDomain
  408. &DomainSize,
  409. &Type
  410. );
  411. if( rc ) {
  412. DBGPRINT(("Internal error on account name %ws\n",pAccountToLookup));
  413. return( FALSE );
  414. }
  415. else {
  416. Error = GetLastError();
  417. if( Error != ERROR_INSUFFICIENT_BUFFER ) {
  418. DBGPRINT(("***ERROR*** %d looking up SID for account name %ws skipped without processing!\n",Error,pAccountToLookup));
  419. return( FALSE );
  420. }
  421. pSid = LocalAlloc( LMEM_FIXED, Size );
  422. if( pSid == NULL ) {
  423. DBGPRINT(("***ERROR*** Out of memory, skipped account %ws without processing!\n",pAccountToLookup));
  424. return( FALSE );
  425. }
  426. pDomain = LocalAlloc( LMEM_FIXED, DomainSize*sizeof(WCHAR) );
  427. if( pDomain == NULL ) {
  428. DBGPRINT(("***ERROR*** Out of memory, skipped account %ws without processing!\n",pAccountToLookup));
  429. LocalFree( pSid );
  430. return( FALSE );
  431. }
  432. rc = LookupAccountNameW(
  433. pSystemName,
  434. pAccountToLookup,
  435. pSid,
  436. &Size,
  437. pDomain,
  438. &DomainSize,
  439. &Type
  440. );
  441. if( !rc ) {
  442. DBGPRINT(("***ERROR*** %d looking up SID for account name %ws skipped without processing!\n",Error,pAccountToLookup));
  443. LocalFree( pSid );
  444. LocalFree( pDomain );
  445. return( FALSE );
  446. }
  447. *ppSid = pSid;
  448. LocalFree( pDomain );
  449. DBGPRINT(( "leaving xxxLookupAccountName(); pSid is okay, returning TRUE.\n" ));
  450. return( TRUE );
  451. }
  452. }
  453. /*****************************************************************************
  454. *
  455. * InitializeBuiltinSids
  456. *
  457. * Initialize the built in SIDS
  458. *
  459. * ENTRY:
  460. * Param1 (input/output)
  461. * Comments
  462. *
  463. * EXIT:
  464. * STATUS_SUCCESS - no error
  465. *
  466. ****************************************************************************/
  467. BOOLEAN
  468. InitializeBuiltinSids()
  469. {
  470. ULONG SidWithZeroSubAuthorities;
  471. ULONG SidWithOneSubAuthority;
  472. ULONG SidWithTwoSubAuthorities;
  473. ULONG SidWithThreeSubAuthorities;
  474. SID_IDENTIFIER_AUTHORITY NullSidAuthority;
  475. SID_IDENTIFIER_AUTHORITY WorldSidAuthority;
  476. SID_IDENTIFIER_AUTHORITY LocalSidAuthority;
  477. SID_IDENTIFIER_AUTHORITY CreatorSidAuthority;
  478. SID_IDENTIFIER_AUTHORITY SeNtAuthority;
  479. NullSidAuthority = SepNullSidAuthority;
  480. WorldSidAuthority = SepWorldSidAuthority;
  481. LocalSidAuthority = SepLocalSidAuthority;
  482. CreatorSidAuthority = SepCreatorSidAuthority;
  483. SeNtAuthority = SepNtAuthority;
  484. //
  485. // The following SID sizes need to be allocated
  486. //
  487. SidWithZeroSubAuthorities = RtlLengthRequiredSid( 0 );
  488. SidWithOneSubAuthority = RtlLengthRequiredSid( 1 );
  489. SidWithTwoSubAuthorities = RtlLengthRequiredSid( 2 );
  490. SidWithThreeSubAuthorities = RtlLengthRequiredSid( 3 );
  491. //
  492. // Allocate and initialize the universal SIDs
  493. //
  494. SeNullSid = (PSID)RtlAllocateHeap(RtlProcessHeap(), 0, SidWithOneSubAuthority);
  495. SeWorldSid = (PSID)RtlAllocateHeap(RtlProcessHeap(), 0, SidWithOneSubAuthority);
  496. SeLocalSid = (PSID)RtlAllocateHeap(RtlProcessHeap(), 0, SidWithOneSubAuthority);
  497. SeCreatorOwnerSid = (PSID)RtlAllocateHeap(RtlProcessHeap(), 0, SidWithOneSubAuthority);
  498. SeCreatorGroupSid = (PSID)RtlAllocateHeap(RtlProcessHeap(), 0, SidWithOneSubAuthority);
  499. //
  500. // Fail initialization if we didn't get enough memory for the universal
  501. // SIDs.
  502. //
  503. if ( (SeNullSid == NULL) ||
  504. (SeWorldSid == NULL) ||
  505. (SeLocalSid == NULL) ||
  506. (SeCreatorOwnerSid == NULL) ||
  507. (SeCreatorGroupSid == NULL)
  508. ) {
  509. return( FALSE );
  510. }
  511. RtlInitializeSid( SeNullSid, &NullSidAuthority, 1 );
  512. RtlInitializeSid( SeWorldSid, &WorldSidAuthority, 1 );
  513. RtlInitializeSid( SeLocalSid, &LocalSidAuthority, 1 );
  514. RtlInitializeSid( SeCreatorOwnerSid, &CreatorSidAuthority, 1 );
  515. RtlInitializeSid( SeCreatorGroupSid, &CreatorSidAuthority, 1 );
  516. *(RtlSubAuthoritySid( SeNullSid, 0 )) = SECURITY_NULL_RID;
  517. *(RtlSubAuthoritySid( SeWorldSid, 0 )) = SECURITY_WORLD_RID;
  518. *(RtlSubAuthoritySid( SeLocalSid, 0 )) = SECURITY_LOCAL_RID;
  519. *(RtlSubAuthoritySid( SeCreatorOwnerSid, 0 )) = SECURITY_CREATOR_OWNER_RID;
  520. *(RtlSubAuthoritySid( SeCreatorGroupSid, 0 )) = SECURITY_CREATOR_GROUP_RID;
  521. //
  522. // Allocate and initialize the NT defined SIDs
  523. //
  524. SeNtAuthoritySid = (PSID)RtlAllocateHeap(RtlProcessHeap(), 0, SidWithZeroSubAuthorities);
  525. SeDialupSid = (PSID)RtlAllocateHeap(RtlProcessHeap(), 0, SidWithOneSubAuthority);
  526. SeNetworkSid = (PSID)RtlAllocateHeap(RtlProcessHeap(), 0, SidWithOneSubAuthority);
  527. SeBatchSid = (PSID)RtlAllocateHeap(RtlProcessHeap(), 0, SidWithOneSubAuthority);
  528. SeInteractiveSid = (PSID)RtlAllocateHeap(RtlProcessHeap(), 0, SidWithOneSubAuthority);
  529. SeServiceSid = (PSID)RtlAllocateHeap(RtlProcessHeap(), 0, SidWithOneSubAuthority);
  530. SeLocalGuestSid = (PSID)RtlAllocateHeap(RtlProcessHeap(), 0, SidWithOneSubAuthority);
  531. SeLocalSystemSid = (PSID)RtlAllocateHeap(RtlProcessHeap(), 0, SidWithOneSubAuthority);
  532. SeLocalAdminSid = (PSID)RtlAllocateHeap(RtlProcessHeap(), 0, SidWithOneSubAuthority);
  533. SeLocalManagerSid = (PSID)RtlAllocateHeap(RtlProcessHeap(), 0, SidWithOneSubAuthority);
  534. SeAliasAdminsSid = (PSID)RtlAllocateHeap(RtlProcessHeap(), 0, SidWithTwoSubAuthorities);
  535. SeAliasUsersSid = (PSID)RtlAllocateHeap(RtlProcessHeap(), 0, SidWithTwoSubAuthorities);
  536. SeAliasGuestsSid = (PSID)RtlAllocateHeap(RtlProcessHeap(), 0, SidWithTwoSubAuthorities);
  537. SeAliasPowerUsersSid = (PSID)RtlAllocateHeap(RtlProcessHeap(), 0, SidWithTwoSubAuthorities);
  538. SeAliasAccountOpsSid = (PSID)RtlAllocateHeap(RtlProcessHeap(), 0, SidWithTwoSubAuthorities);
  539. SeAliasSystemOpsSid = (PSID)RtlAllocateHeap(RtlProcessHeap(), 0, SidWithTwoSubAuthorities);
  540. SeAliasPrintOpsSid = (PSID)RtlAllocateHeap(RtlProcessHeap(), 0, SidWithTwoSubAuthorities);
  541. SeAliasBackupOpsSid = (PSID)RtlAllocateHeap(RtlProcessHeap(), 0, SidWithTwoSubAuthorities);
  542. SeAliasReplicatorSid = (PSID)RtlAllocateHeap(RtlProcessHeap(), 0, SidWithTwoSubAuthorities);
  543. //
  544. // Fail initialization if we didn't get enough memory for the NT SIDs.
  545. //
  546. if ( (SeNtAuthoritySid == NULL) ||
  547. (SeDialupSid == NULL) ||
  548. (SeNetworkSid == NULL) ||
  549. (SeBatchSid == NULL) ||
  550. (SeInteractiveSid == NULL) ||
  551. (SeServiceSid == NULL) ||
  552. (SeLocalGuestSid == NULL) ||
  553. (SeLocalSystemSid == NULL) ||
  554. (SeLocalAdminSid == NULL) ||
  555. (SeLocalManagerSid == NULL) ||
  556. (SeAliasAdminsSid == NULL) ||
  557. (SeAliasUsersSid == NULL) ||
  558. (SeAliasGuestsSid == NULL) ||
  559. (SeAliasPowerUsersSid == NULL) ||
  560. (SeAliasAccountOpsSid == NULL) ||
  561. (SeAliasSystemOpsSid == NULL) ||
  562. (SeAliasReplicatorSid == NULL) ||
  563. (SeAliasPrintOpsSid == NULL) ||
  564. (SeAliasBackupOpsSid == NULL)
  565. ) {
  566. return( FALSE );
  567. }
  568. RtlInitializeSid( SeNtAuthoritySid, &SeNtAuthority, 0 );
  569. RtlInitializeSid( SeDialupSid, &SeNtAuthority, 1 );
  570. RtlInitializeSid( SeNetworkSid, &SeNtAuthority, 1 );
  571. RtlInitializeSid( SeBatchSid, &SeNtAuthority, 1 );
  572. RtlInitializeSid( SeInteractiveSid, &SeNtAuthority, 1 );
  573. RtlInitializeSid( SeServiceSid, &SeNtAuthority, 1 );
  574. RtlInitializeSid( SeLocalGuestSid, &SeNtAuthority, 1 );
  575. RtlInitializeSid( SeLocalSystemSid, &SeNtAuthority, 1 );
  576. RtlInitializeSid( SeLocalAdminSid, &SeNtAuthority, 1 );
  577. RtlInitializeSid( SeLocalManagerSid, &SeNtAuthority, 1 );
  578. RtlInitializeSid( SeAliasAdminsSid, &SeNtAuthority, 2);
  579. RtlInitializeSid( SeAliasUsersSid, &SeNtAuthority, 2);
  580. RtlInitializeSid( SeAliasGuestsSid, &SeNtAuthority, 2);
  581. RtlInitializeSid( SeAliasPowerUsersSid, &SeNtAuthority, 2);
  582. RtlInitializeSid( SeAliasAccountOpsSid, &SeNtAuthority, 2);
  583. RtlInitializeSid( SeAliasSystemOpsSid, &SeNtAuthority, 2);
  584. RtlInitializeSid( SeAliasPrintOpsSid, &SeNtAuthority, 2);
  585. RtlInitializeSid( SeAliasBackupOpsSid, &SeNtAuthority, 2);
  586. RtlInitializeSid( SeAliasReplicatorSid, &SeNtAuthority, 2);
  587. *(RtlSubAuthoritySid( SeDialupSid, 0 )) = SECURITY_DIALUP_RID;
  588. *(RtlSubAuthoritySid( SeNetworkSid, 0 )) = SECURITY_NETWORK_RID;
  589. *(RtlSubAuthoritySid( SeBatchSid, 0 )) = SECURITY_BATCH_RID;
  590. *(RtlSubAuthoritySid( SeInteractiveSid, 0 )) = SECURITY_INTERACTIVE_RID;
  591. *(RtlSubAuthoritySid( SeServiceSid, 0 )) = SECURITY_SERVICE_RID;
  592. // *(RtlSubAuthoritySid( SeLocalGuestSid, 0 )) = SECURITY_LOCAL_GUESTS_RID;
  593. *(RtlSubAuthoritySid( SeLocalSystemSid, 0 )) = SECURITY_LOCAL_SYSTEM_RID;
  594. // *(RtlSubAuthoritySid( SeLocalAdminSid, 0 )) = SECURITY_LOCAL_ADMIN_RID;
  595. // *(RtlSubAuthoritySid( SeLocalManagerSid, 0 )) = SECURITY_LOCAL_MANAGER_RID;
  596. *(RtlSubAuthoritySid( SeAliasAdminsSid, 0 )) = SECURITY_BUILTIN_DOMAIN_RID;
  597. *(RtlSubAuthoritySid( SeAliasUsersSid, 0 )) = SECURITY_BUILTIN_DOMAIN_RID;
  598. *(RtlSubAuthoritySid( SeAliasGuestsSid, 0 )) = SECURITY_BUILTIN_DOMAIN_RID;
  599. *(RtlSubAuthoritySid( SeAliasPowerUsersSid, 0 )) = SECURITY_BUILTIN_DOMAIN_RID;
  600. *(RtlSubAuthoritySid( SeAliasAccountOpsSid, 0 )) = SECURITY_BUILTIN_DOMAIN_RID;
  601. *(RtlSubAuthoritySid( SeAliasSystemOpsSid, 0 )) = SECURITY_BUILTIN_DOMAIN_RID;
  602. *(RtlSubAuthoritySid( SeAliasPrintOpsSid, 0 )) = SECURITY_BUILTIN_DOMAIN_RID;
  603. *(RtlSubAuthoritySid( SeAliasBackupOpsSid, 0 )) = SECURITY_BUILTIN_DOMAIN_RID;
  604. *(RtlSubAuthoritySid( SeAliasReplicatorSid, 0 )) = SECURITY_BUILTIN_DOMAIN_RID;
  605. *(RtlSubAuthoritySid( SeAliasAdminsSid, 1 )) = DOMAIN_ALIAS_RID_ADMINS;
  606. *(RtlSubAuthoritySid( SeAliasUsersSid, 1 )) = DOMAIN_ALIAS_RID_USERS;
  607. *(RtlSubAuthoritySid( SeAliasGuestsSid, 1 )) = DOMAIN_ALIAS_RID_GUESTS;
  608. *(RtlSubAuthoritySid( SeAliasPowerUsersSid, 1 )) = DOMAIN_ALIAS_RID_POWER_USERS;
  609. *(RtlSubAuthoritySid( SeAliasAccountOpsSid, 1 )) = DOMAIN_ALIAS_RID_ACCOUNT_OPS;
  610. *(RtlSubAuthoritySid( SeAliasSystemOpsSid, 1 )) = DOMAIN_ALIAS_RID_SYSTEM_OPS;
  611. *(RtlSubAuthoritySid( SeAliasPrintOpsSid, 1 )) = DOMAIN_ALIAS_RID_PRINT_OPS;
  612. *(RtlSubAuthoritySid( SeAliasBackupOpsSid, 1 )) = DOMAIN_ALIAS_RID_BACKUP_OPS;
  613. *(RtlSubAuthoritySid( SeAliasReplicatorSid, 1 )) = DOMAIN_ALIAS_RID_REPLICATOR;
  614. return( TRUE );
  615. }
  616. /*****************************************************************************
  617. *
  618. * xxxLookupBuiltinAccount
  619. *
  620. * Wrapper to lookup the SID for a given built in account name
  621. *
  622. * Returns a pointer to the SID in newly allocated memory
  623. *
  624. * ENTRY:
  625. * Param1 (input/output)
  626. * Comments
  627. *
  628. * EXIT:
  629. * STATUS_SUCCESS - no error
  630. *
  631. ****************************************************************************/
  632. BOOL
  633. xxxLookupBuiltinAccount(
  634. PWCHAR pSystemName,
  635. PWCHAR pAccountName,
  636. PSID *ppSid
  637. )
  638. {
  639. USHORT SidLen;
  640. SID_NAME_USE Type;
  641. PSID pSid = NULL;
  642. if( _wcsicmp( L"NULL_SID", pAccountName ) == 0 ) {
  643. pSid = SeNullSid;
  644. Type = SidTypeInvalid;
  645. }
  646. if( pSid == NULL ) {
  647. SetLastError( ERROR_NONE_MAPPED );
  648. return( FALSE );
  649. }
  650. SidLen = (USHORT)GetLengthSid( pSid );
  651. *ppSid = LocalAlloc(LMEM_FIXED, SidLen );
  652. if( *ppSid == NULL ) {
  653. SetLastError( ERROR_INSUFFICIENT_BUFFER );
  654. return( FALSE );
  655. }
  656. RtlMoveMemory( *ppSid, pSid, SidLen );
  657. return( TRUE );
  658. #ifdef notdef
  659. if( EqualSid( SeNullSid, pSid ) ) {
  660. pName = L"NULL_SID";
  661. Type = SidTypeInvalid;
  662. }
  663. if( EqualSid( SeWorldSid, pSid ) ) {
  664. pName = L"WORLD";
  665. Type = SidTypeWellKnownGroup;
  666. }
  667. if( EqualSid( SeLocalSid, pSid ) ) {
  668. pName = L"LOCAL_ACCOUNT";
  669. Type = SidTypeWellKnownGroup;
  670. }
  671. if( EqualSid( SeCreatorOwnerSid, pSid ) ) {
  672. pName = L"CREATOR_OWNER";
  673. Type = SidTypeWellKnownGroup;
  674. }
  675. if( EqualSid( SeCreatorGroupSid, pSid ) ) {
  676. pName = L"CREATOR_GROUP";
  677. Type = SidTypeWellKnownGroup;
  678. }
  679. //
  680. // Look at the NT SIDS
  681. //
  682. if( EqualSid( SeNtAuthoritySid, pSid ) ) {
  683. pName = L"NTAUTHORITY";
  684. Type = SidTypeWellKnownGroup;
  685. }
  686. if( EqualSid( SeDialupSid, pSid ) ) {
  687. pName = L"DIALUP";
  688. Type = SidTypeWellKnownGroup;
  689. }
  690. if( EqualSid( SeNetworkSid, pSid ) ) {
  691. pName = L"NETWORK";
  692. Type = SidTypeWellKnownGroup;
  693. }
  694. if( EqualSid( SeBatchSid, pSid ) ) {
  695. pName = L"BATCH";
  696. Type = SidTypeWellKnownGroup;
  697. }
  698. if( EqualSid( SeInteractiveSid, pSid ) ) {
  699. pName = L"INTERACTIVE";
  700. Type = SidTypeWellKnownGroup;
  701. }
  702. if( EqualSid( SeServiceSid, pSid ) ) {
  703. pName = L"SERVICE";
  704. Type = SidTypeWellKnownGroup;
  705. }
  706. if( EqualSid( SeLocalGuestSid, pSid ) ) {
  707. pName = L"LOCALGUEST";
  708. Type = SidTypeWellKnownGroup;
  709. }
  710. if( EqualSid( SeLocalSystemSid, pSid ) ) {
  711. pName = L"LOCALSYSTEM";
  712. Type = SidTypeWellKnownGroup;
  713. }
  714. if( EqualSid( SeLocalAdminSid, pSid ) ) {
  715. pName = L"LOCALADMIN";
  716. Type = SidTypeWellKnownGroup;
  717. }
  718. if( EqualSid( SeLocalManagerSid, pSid ) ) {
  719. pName = L"LOCALMANAGER";
  720. Type = SidTypeWellKnownGroup;
  721. }
  722. if( EqualSid( SeAliasAdminsSid, pSid ) ) {
  723. pName = L"ALIAS_ADMINS";
  724. Type = SidTypeAlias;
  725. }
  726. if( EqualSid( SeAliasUsersSid, pSid ) ) {
  727. pName = L"ALIAS_USERS";
  728. Type = SidTypeAlias;
  729. }
  730. if( EqualSid( SeAliasGuestsSid, pSid ) ) {
  731. pName = L"ALIAS_GUESTS";
  732. Type = SidTypeAlias;
  733. }
  734. if( EqualSid( SeAliasPowerUsersSid, pSid ) ) {
  735. pName = L"ALIAS_POWERUSERS";
  736. Type = SidTypeAlias;
  737. }
  738. if( EqualSid( SeAliasAccountOpsSid, pSid ) ) {
  739. pName = L"ALIAS_ACCOUNT_OPS";
  740. Type = SidTypeAlias;
  741. }
  742. if( EqualSid( SeAliasSystemOpsSid, pSid ) ) {
  743. pName = L"ALIAS_SYSTEM_OPS";
  744. Type = SidTypeAlias;
  745. }
  746. if( EqualSid( SeAliasPrintOpsSid, pSid ) ) {
  747. pName = L"ALIAS_PRINT_OPS";
  748. Type = SidTypeAlias;
  749. }
  750. if( EqualSid( SeAliasBackupOpsSid, pSid ) ) {
  751. pName = L"ALIAS_BACKUP_OPS";
  752. Type = SidTypeAlias;
  753. }
  754. if( EqualSid( SeAliasReplicatorSid, pSid ) ) {
  755. pName = L"ALIAS_REPLICATOR";
  756. Type = SidTypeAlias;
  757. }
  758. #endif
  759. }
  760. /*****************************************************************************
  761. *
  762. * GetSecureAcl
  763. *
  764. * Comment
  765. *
  766. * ENTRY:
  767. * Param1 (input/output)
  768. * Comments
  769. *
  770. * EXIT:
  771. * STATUS_SUCCESS - no error
  772. *
  773. ****************************************************************************/
  774. PACL
  775. GetSecureAcl()
  776. {
  777. return( pAdminsOnlyAcl );
  778. }
  779. /*****************************************************************************
  780. *
  781. * BuildSecureAcl
  782. *
  783. * Comment
  784. *
  785. * ENTRY:
  786. * Param1 (input/output)
  787. * Comments
  788. *
  789. * EXIT:
  790. * STATUS_SUCCESS - no error
  791. *
  792. ****************************************************************************/
  793. BOOL
  794. BuildSecureAcl()
  795. {
  796. BOOL rc;
  797. DWORD Index, Error;
  798. PADMIN_ACCOUNTS p;
  799. DWORD AclSize;
  800. PACL pAcl = NULL;
  801. PACCESS_ALLOWED_ACE pAce = NULL;
  802. /*
  803. * Calculate the ACL size
  804. */
  805. AclSize = sizeof(ACL);
  806. //
  807. // Reserve memory for the deny ACE's
  808. //
  809. for( Index = 0; Index < DenyAccountEntries; Index++ ) {
  810. p = &DenyAccounts[Index];
  811. if( p->pSid ) {
  812. AclSize += sizeof(ACCESS_DENIED_ACE);
  813. AclSize += (GetLengthSid( p->pSid ) - sizeof(DWORD));
  814. }
  815. }
  816. //
  817. // Reserve memory for the allowed ACE's
  818. //
  819. for( Index = 0; Index < AllowAccountEntries; Index++ ) {
  820. p = &AllowAccounts[Index];
  821. if( p->pSid ) {
  822. AclSize += sizeof(ACCESS_ALLOWED_ACE);
  823. AclSize += (GetLengthSid( p->pSid ) - sizeof(DWORD));
  824. }
  825. }
  826. pAcl = LocalAlloc( LMEM_FIXED, AclSize );
  827. if( pAcl == NULL ) {
  828. DBGPRINT(("Could not allocate memory\n"));
  829. return( FALSE );
  830. }
  831. rc = InitializeAcl(
  832. pAcl,
  833. AclSize,
  834. ACL_REVISION
  835. );
  836. if( !rc ) {
  837. DBGPRINT(("Error %d InitializeAcl\n",GetLastError()));
  838. LocalFree( pAcl );
  839. return( FALSE );
  840. }
  841. /*
  842. * Add a deny all ACE for each account in the deny list
  843. */
  844. for( Index = 0; Index < DenyAccountEntries; Index++ ) {
  845. p = &DenyAccounts[Index];
  846. rc = AddAccessDeniedAce(
  847. pAcl,
  848. ACL_REVISION,
  849. FILE_ALL_ACCESS,
  850. p->pSid
  851. );
  852. if( !rc ) {
  853. Error = GetLastError();
  854. DBGPRINT(("***ERROR*** adding deny ACE %d for account %ws\n",Error,p->Name));
  855. }
  856. }
  857. /*
  858. * Add an allow all ACE for each account in the allow list
  859. */
  860. for( Index = 0; Index < AllowAccountEntries; Index++ ) {
  861. p = &AllowAccounts[Index];
  862. rc = AddAccessAllowedAce(
  863. pAcl,
  864. ACL_REVISION,
  865. FILE_ALL_ACCESS,
  866. p->pSid
  867. );
  868. if( !rc ) {
  869. Error = GetLastError();
  870. DBGPRINT(("***ERROR*** adding allow ACE %d for account %ws\n",Error,p->Name));
  871. }
  872. }
  873. pAdminsOnlyAcl = pAcl;
  874. AdminsOnlyAclSize = AclSize;
  875. //
  876. // Put the inherit flags on the ACE's
  877. //
  878. for( Index=0; Index < pAcl->AceCount; Index++ ) {
  879. rc = GetAce( pAcl, Index, (PVOID)&pAce );
  880. if( !rc ) {
  881. continue;
  882. }
  883. pAce->Header.AceFlags |= (CONTAINER_INHERIT_ACE | OBJECT_INHERIT_ACE);
  884. }
  885. return( TRUE );
  886. }
  887. /*****************************************************************************
  888. *
  889. * GetLocalAdminSid
  890. *
  891. * Comment
  892. *
  893. * ENTRY:
  894. * Param1 (input/output)
  895. * Comments
  896. *
  897. * EXIT:
  898. * STATUS_SUCCESS - no error
  899. *
  900. ****************************************************************************/
  901. PSID
  902. GetLocalAdminSid()
  903. {
  904. return( SeLocalAdminSid );
  905. }
  906. /*****************************************************************************
  907. *
  908. * GetAdminSid
  909. *
  910. * Comment
  911. *
  912. * ENTRY:
  913. * Param1 (input/output)
  914. * Comments
  915. *
  916. * EXIT:
  917. *
  918. ****************************************************************************/
  919. PSID
  920. GetAdminSid()
  921. {
  922. return( AllowAccounts[ADMIN_ACCOUNT].pSid );
  923. }
  924. /*****************************************************************************
  925. *
  926. * GetLocalAdminGroupSid
  927. *
  928. * Comment
  929. *
  930. * ENTRY:
  931. * Param1 (input/output)
  932. * Comments
  933. *
  934. * EXIT:
  935. * STATUS_SUCCESS - no error
  936. *
  937. ****************************************************************************/
  938. PSID
  939. GetLocalAdminGroupSid()
  940. {
  941. return( SeAliasAdminsSid );
  942. }
  943. /*****************************************************************************
  944. *
  945. * GetLocalAdminGroupSid
  946. *
  947. * Comment
  948. *
  949. * ENTRY:
  950. * Param1 (input/output)
  951. * Comments
  952. *
  953. * EXIT:
  954. * STATUS_SUCCESS - no error
  955. *
  956. ****************************************************************************/
  957. BOOL CheckUserSid()
  958. {
  959. BOOL rc;
  960. PSID pSid;
  961. PSID pPrevSid;
  962. rc = xxxLookupAccountName(
  963. ComputerName,
  964. AllowAccounts[USER_ACCOUNT].Name,
  965. &pSid
  966. );
  967. if( !rc ) {
  968. // It might be a special builtin account
  969. rc = xxxLookupBuiltinAccount(
  970. ComputerName,
  971. AllowAccounts[USER_ACCOUNT].Name,
  972. &pSid
  973. );
  974. }
  975. pPrevSid = AllowAccounts[USER_ACCOUNT].pSid;
  976. if (rc && (!pPrevSid || !EqualSid( pSid, pPrevSid ))) {
  977. AllowAccounts[USER_ACCOUNT].pSid = pSid;
  978. if (pAdminsOnlyAcl) {
  979. LocalFree( pAdminsOnlyAcl );
  980. pAdminsOnlyAcl = NULL;
  981. AdminsOnlyAclSize = 0;
  982. }
  983. rc = BuildSecureAcl();
  984. }
  985. return(rc);
  986. }