Source code of Windows XP (NT5)
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.

148 lines
4.0 KiB

  1. //+----------------------------------------------------------------------------
  2. //
  3. // Copyright (C) 1996, Microsoft Corporation
  4. //
  5. // File: security.cxx
  6. //
  7. // Contents: Worker routines to check whether the calling thread has
  8. // admin access on this machine.
  9. //
  10. // Classes:
  11. //
  12. // Functions: InitializeSecurity
  13. // AccessCheckRpcClient
  14. //
  15. // History: Aug 14, 1996 Milans created
  16. //
  17. //-----------------------------------------------------------------------------
  18. #include <headers.hxx>
  19. #pragma hdrstop
  20. static SECURITY_DESCRIPTOR AdminSecurityDesc;
  21. static GENERIC_MAPPING AdminGenericMapping = {
  22. STANDARD_RIGHTS_READ, // Generic read
  23. STANDARD_RIGHTS_WRITE, // Generic write
  24. STANDARD_RIGHTS_EXECUTE, // Generic execute
  25. STANDARD_RIGHTS_READ | // Generic all
  26. STANDARD_RIGHTS_WRITE |
  27. STANDARD_RIGHTS_EXECUTE
  28. };
  29. //+----------------------------------------------------------------------------
  30. //
  31. // Function: InitializeSecurity
  32. //
  33. // Synopsis: Initializes data needed to check the access rights of callers
  34. // of the NetDfs APIs
  35. //
  36. // Arguments: None
  37. //
  38. // Returns: TRUE if successful, FALSE otherwise.
  39. //
  40. //-----------------------------------------------------------------------------
  41. BOOL DfsInitializeSecurity()
  42. {
  43. static PSID AdminSid;
  44. static PACL AdminAcl;
  45. NTSTATUS status;
  46. ULONG cbAcl;
  47. SID_IDENTIFIER_AUTHORITY ntAuthority = SECURITY_NT_AUTHORITY;
  48. status = RtlAllocateAndInitializeSid(
  49. &ntAuthority,
  50. 2,
  51. SECURITY_BUILTIN_DOMAIN_RID,
  52. DOMAIN_ALIAS_RID_ADMINS,
  53. 0,0,0,0,0,0,
  54. &AdminSid);
  55. if (!NT_SUCCESS(status))
  56. return( FALSE );
  57. cbAcl = sizeof(ACL) + sizeof(ACCESS_ALLOWED_ACE) + GetLengthSid(AdminSid);
  58. AdminAcl = (PACL) new BYTE[ cbAcl ];
  59. if (AdminAcl == NULL)
  60. return(FALSE);
  61. if (!InitializeAcl(AdminAcl, cbAcl, ACL_REVISION))
  62. return( FALSE );
  63. if (!AddAccessAllowedAce(AdminAcl, ACL_REVISION, STANDARD_RIGHTS_WRITE, AdminSid))
  64. return( FALSE );
  65. if (!InitializeSecurityDescriptor(&AdminSecurityDesc, SECURITY_DESCRIPTOR_REVISION))
  66. return( FALSE );
  67. if (!SetSecurityDescriptorOwner(&AdminSecurityDesc, AdminSid, FALSE))
  68. return( FALSE );
  69. if (!SetSecurityDescriptorGroup(&AdminSecurityDesc, AdminSid, FALSE))
  70. return( FALSE );
  71. if (!SetSecurityDescriptorDacl(&AdminSecurityDesc, TRUE, AdminAcl, FALSE))
  72. return( FALSE );
  73. return( TRUE );
  74. }
  75. //+----------------------------------------------------------------------------
  76. //
  77. // Function: AccessCheckRpcClient
  78. //
  79. // Synopsis: Called by an RPC server thread. This routine will check if
  80. // the client making the RPC call has rights to do so.
  81. //
  82. // Arguments: None, but the callers thread context needs to be that of an
  83. // RPC server thread
  84. //
  85. // Returns: TRUE if client has rights to make Dfs admin calls.
  86. //
  87. //-----------------------------------------------------------------------------
  88. BOOL AccessCheckRpcClient()
  89. {
  90. BOOL accessGranted = FALSE;
  91. DWORD grantedAccess;
  92. HANDLE clientToken = NULL;
  93. BYTE privilegeSet[500]; // Large buffer
  94. DWORD privilegeSetSize = sizeof(privilegeSet);
  95. DWORD dwErr;
  96. if (RpcImpersonateClient(NULL) != ERROR_SUCCESS)
  97. return( FALSE );
  98. if (OpenThreadToken(GetCurrentThread(), TOKEN_QUERY, TRUE, &clientToken)) {
  99. (void) AccessCheck(
  100. &AdminSecurityDesc,
  101. clientToken,
  102. STANDARD_RIGHTS_WRITE,
  103. &AdminGenericMapping,
  104. (PPRIVILEGE_SET) privilegeSet,
  105. &privilegeSetSize,
  106. &grantedAccess,
  107. &accessGranted);
  108. dwErr = GetLastError();
  109. }
  110. RpcRevertToSelf();
  111. if (clientToken != NULL)
  112. CloseHandle( clientToken );
  113. return( accessGranted );
  114. }