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.

212 lines
5.3 KiB

  1. /*++
  2. Copyright (c) 2000-2000 Microsoft Corporation
  3. Module Name:
  4. Security.c
  5. Abstract:
  6. This module implements various Security routines used by
  7. the PGM Transport
  8. Author:
  9. Mohammad Shabbir Alam (MAlam) 3-30-2000
  10. Revision History:
  11. --*/
  12. #include "precomp.h"
  13. #ifdef FILE_LOGGING
  14. #include "security.tmh"
  15. #endif // FILE_LOGGING
  16. //******************* Pageable Routine Declarations ****************
  17. #ifdef ALLOC_PRAGMA
  18. #pragma alloc_text(PAGE, PgmBuildAdminSecurityDescriptor)
  19. #pragma alloc_text(PAGE, PgmGetUserInfo)
  20. #endif
  21. //******************* Pageable Routine Declarations ****************
  22. //----------------------------------------------------------------------------
  23. NTSTATUS
  24. PgmBuildAdminSecurityDescriptor(
  25. OUT SECURITY_DESCRIPTOR **ppSecurityDescriptor
  26. )
  27. /*++
  28. Routine Description:
  29. (Lifted from TCP - TcpBuildDeviceAcl)
  30. This routine builds an ACL which gives Administrators, LocalService and NetworkService
  31. principals full access. All other principals have no access.
  32. Arguments:
  33. DeviceAcl - Output pointer to the new ACL.
  34. Return Value:
  35. STATUS_SUCCESS or an appropriate error code.
  36. --*/
  37. {
  38. PGENERIC_MAPPING GenericMapping;
  39. PSID pAdminsSid, pServiceSid, pNetworkSid;
  40. ULONG AclLength;
  41. NTSTATUS Status;
  42. ACCESS_MASK AccessMask = GENERIC_ALL;
  43. PACL pNewAcl, pAclCopy;
  44. PSID pSid;
  45. SID_IDENTIFIER_AUTHORITY Authority = SECURITY_NT_AUTHORITY;
  46. SECURITY_DESCRIPTOR *pSecurityDescriptor;
  47. PAGED_CODE();
  48. if (!(pSid = PgmAllocMem (RtlLengthRequiredSid (3), PGM_TAG('S'))) ||
  49. (!NT_SUCCESS (Status = RtlInitializeSid (pSid, &Authority, 3))))
  50. {
  51. if (pSid)
  52. {
  53. PgmFreeMem (pSid);
  54. }
  55. return (STATUS_INSUFFICIENT_RESOURCES);
  56. }
  57. *RtlSubAuthoritySid (pSid, 0) = SECURITY_BUILTIN_DOMAIN_RID;
  58. *RtlSubAuthoritySid (pSid, 1) = DOMAIN_ALIAS_RID_ADMINS;
  59. *RtlSubAuthoritySid (pSid, 2) = SECURITY_LOCAL_SYSTEM_RID;
  60. ASSERT (RtlValidSid (pSid));
  61. AclLength = sizeof(ACL) +
  62. RtlLengthSid(pSid) +
  63. sizeof(ACCESS_ALLOWED_ACE) -
  64. sizeof(ULONG);
  65. if (!(pNewAcl = PgmAllocMem (AclLength, PGM_TAG('S'))))
  66. {
  67. PgmFreeMem (pSid);
  68. return (STATUS_INSUFFICIENT_RESOURCES);
  69. }
  70. Status = RtlCreateAcl (pNewAcl, AclLength, ACL_REVISION);
  71. if (!NT_SUCCESS(Status))
  72. {
  73. PgmFreeMem (pNewAcl);
  74. PgmFreeMem (pSid);
  75. return (Status);
  76. }
  77. Status = RtlAddAccessAllowedAce (pNewAcl,
  78. ACL_REVISION,
  79. GENERIC_ALL,
  80. pSid);
  81. ASSERT(NT_SUCCESS(Status));
  82. if (!NT_SUCCESS(Status))
  83. {
  84. PgmFreeMem (pNewAcl);
  85. PgmFreeMem (pSid);
  86. return (Status);
  87. }
  88. if (!(pSecurityDescriptor = PgmAllocMem ((sizeof(SECURITY_DESCRIPTOR) + AclLength), PGM_TAG('S'))))
  89. {
  90. PgmFreeMem (pNewAcl);
  91. PgmFreeMem (pSid);
  92. return (STATUS_INSUFFICIENT_RESOURCES);
  93. }
  94. pAclCopy = (PACL) ((PISECURITY_DESCRIPTOR) pSecurityDescriptor+1);
  95. RtlCopyMemory (pAclCopy, pNewAcl, AclLength);
  96. Status = RtlCreateSecurityDescriptor (pSecurityDescriptor, SECURITY_DESCRIPTOR_REVISION);
  97. if (!NT_SUCCESS (Status))
  98. {
  99. PgmFreeMem (pNewAcl);
  100. PgmFreeMem (pSid);
  101. PgmFreeMem (pSecurityDescriptor);
  102. }
  103. Status = RtlSetDaclSecurityDescriptor (pSecurityDescriptor, TRUE, pAclCopy, FALSE);
  104. if (!NT_SUCCESS (Status))
  105. {
  106. PgmFreeMem (pNewAcl);
  107. PgmFreeMem (pSid);
  108. PgmFreeMem (pSecurityDescriptor);
  109. }
  110. PgmFreeMem (pNewAcl);
  111. PgmFreeMem (pSid);
  112. *ppSecurityDescriptor = pSecurityDescriptor;
  113. return (STATUS_SUCCESS);
  114. }
  115. //----------------------------------------------------------------------------
  116. NTSTATUS
  117. PgmGetUserInfo(
  118. IN PIRP pIrp,
  119. IN PIO_STACK_LOCATION pIrpSp,
  120. OUT TOKEN_USER **ppUserId,
  121. OUT BOOLEAN *pfUserIsAdmin
  122. )
  123. {
  124. PACCESS_TOKEN *pAccessToken = NULL;
  125. TOKEN_USER *pUserId = NULL;
  126. BOOLEAN fUserIsAdmin = FALSE;
  127. SECURITY_SUBJECT_CONTEXT *pSubjectContext;
  128. PAGED_CODE();
  129. //
  130. // Get User ID
  131. //
  132. pSubjectContext = &pIrpSp->Parameters.Create.SecurityContext->AccessState->SubjectSecurityContext;
  133. pAccessToken = SeQuerySubjectContextToken (pSubjectContext);
  134. if ((!pAccessToken) ||
  135. (!NT_SUCCESS (SeQueryInformationToken (pAccessToken, TokenUser, &pUserId))))
  136. {
  137. //
  138. // Cannot get the user token
  139. //
  140. *ppUserId = NULL;
  141. *pfUserIsAdmin = FALSE;
  142. return (STATUS_UNSUCCESSFUL);
  143. }
  144. if (ppUserId)
  145. {
  146. *ppUserId = pUserId;
  147. }
  148. else
  149. {
  150. PgmFreeMem (pUserId);
  151. }
  152. if (pfUserIsAdmin)
  153. {
  154. *pfUserIsAdmin = SeTokenIsAdmin (pAccessToken);
  155. }
  156. return (STATUS_SUCCESS);
  157. /*
  158. //
  159. // Got the user SID
  160. //
  161. if (!RtlEqualSid (gpSystemSid, pUserId->User.Sid))
  162. {
  163. fUserIsAdmin = TRUE;
  164. }
  165. PgmFreeMem (pUserId);
  166. return (fUserIsAdmin);
  167. */
  168. }