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.

258 lines
7.6 KiB

  1. #include "stdinc.h"
  2. #include "idp.h"
  3. #include "sxsapi.h"
  4. #include "sxsid.h"
  5. #define IFNTFAILED_EXIT(q) do { status = (q); if (!NT_SUCCESS(status)) goto Exit; } while (0)
  6. NTSTATUS
  7. RtlSxspMapAssemblyIdentityToPolicyIdentity(
  8. ULONG Flags,
  9. PCASSEMBLY_IDENTITY AssemblyIdentity,
  10. PASSEMBLY_IDENTITY *PolicyIdentity
  11. )
  12. {
  13. NTSTATUS status = STATUS_SUCCESS;
  14. PCWSTR pszTemp;
  15. SIZE_T cchTemp;
  16. PASSEMBLY_IDENTITY NewIdentity = NULL;
  17. RTL_UNICODE_STRING_BUFFER NameBuffer;
  18. UCHAR wchNameBuffer[200];
  19. static const UNICODE_STRING strTemp = { 7, 7, L"Policy" };
  20. BOOLEAN fFirst;
  21. const BOOLEAN fOmitEntireVersion = ((Flags & SXSP_MAP_ASSEMBLY_IDENTITY_TO_POLICY_IDENTITY_FLAG_OMIT_ENTIRE_VERSION) != 0);
  22. PolicyIdentity = NULL;
  23. PARAMETER_CHECK((Flags & ~(SXSP_MAP_ASSEMBLY_IDENTITY_TO_POLICY_IDENTITY_FLAG_OMIT_ENTIRE_VERSION)) == 0);
  24. PARAMETER_CHECK(AssemblyIdentity != 0);
  25. PARAMETER_CHECK(PolicyIdentity != NULL);
  26. IFNTFAILED_EXIT(RtlSxspGetAssemblyIdentityAttributeValue(
  27. SXSP_GET_ASSEMBLY_IDENTITY_ATTRIBUTE_VALUE_FLAG_NOT_FOUND_RETURNS_NULL,
  28. AssemblyIdentity,
  29. &s_IdentityAttribute_type,
  30. &pszTemp,
  31. &cchTemp));
  32. PARAMETER_CHECK(
  33. (cchTemp == 5) &&
  34. (RtlSxspCompareStrings(
  35. pszTemp,
  36. 5,
  37. L"win32",
  38. 5,
  39. TRUE) == 0));
  40. RtlInitUnicodeStringBuffer(&NameBuffer, wchNameBuffer, sizeof(wchNameBuffer));
  41. // Ok, we know we have a win32 assembly reference. Let's change the type to win32-policy
  42. IFNTFAILED_EXIT(RtlSxsDuplicateAssemblyIdentity(
  43. 0,
  44. AssemblyIdentity,
  45. &NewIdentity));
  46. IFNTFAILED_EXIT(RtlSxspSetAssemblyIdentityAttributeValue(
  47. SXSP_SET_ASSEMBLY_IDENTITY_ATTRIBUTE_VALUE_FLAG_OVERWRITE_EXISTING,
  48. NewIdentity,
  49. &s_IdentityAttribute_type,
  50. L"win32-policy",
  51. 12));
  52. IFNTFAILED_EXIT(RtlAssignUnicodeStringBuffer(&NameBuffer, &strTemp));
  53. if (!fOmitEntireVersion)
  54. {
  55. IFNTFAILED_EXIT(RtlSxspGetAssemblyIdentityAttributeValue(
  56. 0,
  57. AssemblyIdentity,
  58. &s_IdentityAttribute_version,
  59. &pszTemp,
  60. &cchTemp));
  61. fFirst = TRUE;
  62. while (cchTemp != 0)
  63. {
  64. if (pszTemp[--cchTemp] == L'.')
  65. {
  66. if (!fFirst)
  67. break;
  68. fFirst = FALSE;
  69. }
  70. }
  71. // This should not be zero; someone prior to this should have validated the version format
  72. // to include three dots.
  73. if (cchTemp == 0) {
  74. status = STATUS_INTERNAL_ERROR;
  75. goto Exit;
  76. }
  77. IFNTFAILED_EXIT(RtlEnsureUnicodeStringBufferSizeBytes(
  78. &NameBuffer,
  79. NameBuffer.String.Length + (sizeof(WCHAR) * (cchTemp + 1))
  80. ));
  81. IFNTFAILED_EXIT(RtlAppendUnicodeToString(
  82. &NameBuffer.String,
  83. pszTemp));
  84. }
  85. IFNTFAILED_EXIT(RtlSxspGetAssemblyIdentityAttributeValue(
  86. 0,
  87. AssemblyIdentity,
  88. &s_IdentityAttribute_name,
  89. &pszTemp,
  90. &cchTemp));
  91. IFNTFAILED_EXIT(RtlEnsureUnicodeStringBufferSizeBytes(
  92. &NameBuffer,
  93. NameBuffer.String.Length + (sizeof(WCHAR) * (cchTemp))));
  94. IFNTFAILED_EXIT(RtlAppendUnicodeToString(
  95. &NameBuffer.String,
  96. pszTemp));
  97. IFNTFAILED_EXIT(
  98. RtlSxspSetAssemblyIdentityAttributeValue(
  99. SXSP_SET_ASSEMBLY_IDENTITY_ATTRIBUTE_VALUE_FLAG_OVERWRITE_EXISTING,
  100. NewIdentity,
  101. &s_IdentityAttribute_name,
  102. NameBuffer.String.Buffer,
  103. NameBuffer.String.Length));
  104. // finally we whack the version...
  105. IFNTFAILED_EXIT(
  106. RtlSxspRemoveAssemblyIdentityAttribute(
  107. SXSP_REMOVE_ASSEMBLY_IDENTITY_ATTRIBUTE_FLAG_NOT_FOUND_SUCCEEDS,
  108. NewIdentity,
  109. &s_IdentityAttribute_version));
  110. *PolicyIdentity = NewIdentity;
  111. NewIdentity = NULL;
  112. Exit:
  113. if (NewIdentity != NULL)
  114. {
  115. RtlSxsDestroyAssemblyIdentity(NewIdentity);
  116. NewIdentity = NULL;
  117. }
  118. return status;
  119. }
  120. /*
  121. BOOL
  122. RtlSxspGenerateTextuallyEncodedPolicyIdentityFromAssemblyIdentity(
  123. ULONG Flags,
  124. PCASSEMBLY_IDENTITY AssemblyIdentity,
  125. CBaseStringBuffer &rbuffEncodedIdentity,
  126. PASSEMBLY_IDENTITY *PolicyIdentityOut
  127. )
  128. {
  129. BOOLEAN fSuccess = FALSE;
  130. FN_TRACE_WIN32(fSuccess);
  131. PASSEMBLY_IDENTITY PolicyIdentity = NULL;
  132. SIZE_T EncodedIdentityBytes = 0;
  133. CStringBufferAccessor acc;
  134. ULONG dwMapFlags = 0;
  135. SIZE_T BytesWritten;
  136. if (PolicyIdentityOut != NULL)
  137. *PolicyIdentityOut = NULL;
  138. PARAMETER_CHECK((Flags & ~(SXSP_GENERATE_TEXTUALLY_ENCODED_POLICY_IDENTITY_FROM_ASSEMBLY_IDENTITY_FLAG_OMIT_ENTIRE_VERSION)) == 0);
  139. PARAMETER_CHECK(AssemblyIdentity != NULL);
  140. if (Flags & SXSP_GENERATE_TEXTUALLY_ENCODED_POLICY_IDENTITY_FROM_ASSEMBLY_IDENTITY_FLAG_OMIT_ENTIRE_VERSION)
  141. dwMapFlags |= SXSP_MAP_ASSEMBLY_IDENTITY_TO_POLICY_IDENTITY_FLAG_OMIT_ENTIRE_VERSION;
  142. IFNTFAILED_EXIT(RtlSxspMapAssemblyIdentityToPolicyIdentity(dwMapFlags, AssemblyIdentity, PolicyIdentity));
  143. IFNTFAILED_EXIT(
  144. RtlSxsComputeAssemblyIdentityEncodedSize(
  145. 0,
  146. PolicyIdentity,
  147. NULL,
  148. SXS_ASSEMBLY_IDENTITY_ENCODING_DEFAULTGROUP_TEXTUAL,
  149. &EncodedIdentityBytes));
  150. INTERNAL_ERROR_CHECK((EncodedIdentityBytes % sizeof(WCHAR)) == 0);
  151. IFNTFAILED_EXIT(rbuffEncodedIdentity.Win32ResizeBuffer((EncodedIdentityBytes / sizeof(WCHAR)) + 1, eDoNotPreserveBufferContents));
  152. acc.Attach(&rbuffEncodedIdentity);
  153. IFNTFAILED_EXIT(
  154. RtlSxsEncodeAssemblyIdentity(
  155. 0,
  156. PolicyIdentity,
  157. NULL,
  158. SXS_ASSEMBLY_IDENTITY_ENCODING_DEFAULTGROUP_TEXTUAL,
  159. acc.GetBufferCb(),
  160. acc.GetBufferPtr(),
  161. &BytesWritten));
  162. INTERNAL_ERROR_CHECK((BytesWritten % sizeof(WCHAR)) == 0);
  163. INTERNAL_ERROR_CHECK(BytesWritten <= EncodedIdentityBytes);
  164. acc.GetBufferPtr()[BytesWritten / sizeof(WCHAR)] = L'\0';
  165. acc.Detach();
  166. if (PolicyIdentityOut != NULL)
  167. {
  168. *PolicyIdentityOut = PolicyIdentity;
  169. PolicyIdentity = NULL; // so we don't try to clean it up in the exit path
  170. }
  171. fSuccess = TRUE;
  172. Exit:
  173. if (PolicyIdentity != NULL)
  174. SxsDestroyAssemblyIdentity(PolicyIdentity);
  175. return fSuccess;
  176. }
  177. */
  178. //
  179. // the difference between this func and SxsHashAssemblyIdentity() is that for policy,
  180. // version should not be calcaulated as part of hash
  181. //
  182. NTSTATUS
  183. RtlSxspHashAssemblyIdentityForPolicy(
  184. IN ULONG dwFlags,
  185. IN PCASSEMBLY_IDENTITY AssemblyIdentity,
  186. OUT ULONG *pulIdentityHash)
  187. {
  188. NTSTATUS status = STATUS_SUCCESS;
  189. PASSEMBLY_IDENTITY pAssemblyIdentity = NULL;
  190. IFNTFAILED_EXIT(RtlSxsDuplicateAssemblyIdentity(
  191. SXS_DUPLICATE_ASSEMBLY_IDENTITY_FLAG_FREEZE,
  192. AssemblyIdentity,
  193. &pAssemblyIdentity));
  194. IFNTFAILED_EXIT(RtlSxspRemoveAssemblyIdentityAttribute(
  195. SXSP_REMOVE_ASSEMBLY_IDENTITY_ATTRIBUTE_FLAG_NOT_FOUND_SUCCEEDS,
  196. pAssemblyIdentity,
  197. &s_IdentityAttribute_version));
  198. IFNTFAILED_EXIT(RtlSxsHashAssemblyIdentity(0, pAssemblyIdentity, pulIdentityHash));
  199. Exit:
  200. if (pAssemblyIdentity != NULL)
  201. RtlSxsDestroyAssemblyIdentity(pAssemblyIdentity);
  202. return status;
  203. }