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.

1908 lines
74 KiB

  1. /*++
  2. Copyright (c) 2000 Microsoft Corporation
  3. Module Name:
  4. dllredir.cpp
  5. Abstract:
  6. Activation context section contributor for the DLL Redirection section.
  7. Author:
  8. Michael J. Grier (MGrier) 23-Feb-2000
  9. Revision History:
  10. Jay Krell (a-JayK) April 2000 install support
  11. --*/
  12. #include "stdinc.h"
  13. #include <windows.h>
  14. #include "sxsp.h"
  15. #include "FusionEventLog.h"
  16. #include "SxsInstall.h"
  17. #include "DllRedir.h"
  18. #include "CTeeStream.h"
  19. #include "SxsPath.h"
  20. #include "hashfile.h"
  21. #if FUSION_PRECOMPILED_MANIFEST
  22. #define PRECOMPILED_MANIFEST_EXTENSION L".precompiled"
  23. #include "pcmWriterStream.h"
  24. #endif
  25. #include "SxsExceptionHandling.h"
  26. #include "strongname.h"
  27. #include "CSecurityMetaData.h"
  28. #include "cstreamtap.h"
  29. //
  30. // We need to hook this up to the setuplog file functionality.
  31. //
  32. #define SxspInstallPrint FusionpDbgPrint
  33. #define POST_WHISTLER_BETA1 0
  34. //
  35. // This is the default hash algorithm for manifests. If no algorithm
  36. // is specified with hashalg="foo", then it's SHA1.
  37. //
  38. #define FUSION_DEFAULT_HASH_ALGORITHM (CALG_SHA1)
  39. /*-----------------------------------------------------------------------------*/
  40. DECLARE_STD_ATTRIBUTE_NAME_DESCRIPTOR(name);
  41. DECLARE_STD_ATTRIBUTE_NAME_DESCRIPTOR(sourceName);
  42. DECLARE_STD_ATTRIBUTE_NAME_DESCRIPTOR(loadFrom);
  43. DECLARE_STD_ATTRIBUTE_NAME_DESCRIPTOR(hash);
  44. DECLARE_STD_ATTRIBUTE_NAME_DESCRIPTOR(hashalg);
  45. typedef struct _DLL_REDIRECTION_CONTEXT
  46. {
  47. _DLL_REDIRECTION_CONTEXT() { }
  48. } DLL_REDIRECTION_CONTEXT, *PDLL_REDIRECTION_CONTEXT;
  49. typedef struct _DLL_REDIRECTION_ENTRY
  50. {
  51. _DLL_REDIRECTION_ENTRY() :
  52. AssemblyPathIsLoadFrom(false),
  53. PathIncludesBaseName(false)
  54. { }
  55. CStringBuffer AssemblyPathBuffer;
  56. bool AssemblyPathIsLoadFrom; // Set to true when a <file name="x" loadfrom="%windir%\system32\"/> is found
  57. bool PathIncludesBaseName; // Set to true when a <file name="x" loadfrom="%windir%\x.dll"/> is found
  58. CStringBuffer FileNameBuffer;
  59. private:
  60. _DLL_REDIRECTION_ENTRY(const _DLL_REDIRECTION_ENTRY &);
  61. void operator =(const _DLL_REDIRECTION_ENTRY &);
  62. } DLL_REDIRECTION_ENTRY, *PDLL_REDIRECTION_ENTRY;
  63. /*-----------------------------------------------------------------------------*/
  64. CDllRedir::CDllRedir()
  65. : m_SSGenContext(NULL),
  66. m_pRunOnce(NULL)
  67. {
  68. }
  69. /*-----------------------------------------------------------------------------*/
  70. CDllRedir::~CDllRedir()
  71. {
  72. }
  73. /*-----------------------------------------------------------------------------*/
  74. VOID
  75. SxspDllRedirectionContributorCallback(
  76. PACTCTXCTB_CALLBACK_DATA Data
  77. )
  78. {
  79. FN_TRACE();
  80. CDllRedir* pThis = NULL;
  81. switch (Data->Header.Reason)
  82. {
  83. case ACTCTXCTB_CBREASON_ACTCTXGENBEGINNING:
  84. Data->GenBeginning.Success = FALSE;
  85. if (Data->Header.ActCtxGenContext == NULL)
  86. {
  87. IFALLOCFAILED_EXIT(pThis = new CDllRedir);
  88. Data->Header.ActCtxGenContext = pThis;
  89. }
  90. // fall through
  91. default:
  92. pThis = reinterpret_cast<CDllRedir*>(Data->Header.ActCtxGenContext);
  93. pThis->ContributorCallback(Data);
  94. if (Data->Header.Reason == ACTCTXCTB_CBREASON_ACTCTXGENENDED)
  95. FUSION_DELETE_SINGLETON(pThis);
  96. break;
  97. }
  98. Exit:
  99. ;
  100. }
  101. /*-----------------------------------------------------------------------------
  102. This function is called on Win9x if we crash during an install, on the
  103. next login. It deletes temporary files/directories.
  104. -----------------------------------------------------------------------------*/
  105. VOID CALLBACK
  106. SxspRunDllDeleteDirectory(HWND hwnd, HINSTANCE hinst, PSTR lpszCmdLine, int nCmdShow)
  107. {
  108. FN_TRACE_SMART_TLS();
  109. CStringBuffer buffer;
  110. if (buffer.Win32Assign(lpszCmdLine, ::strlen(lpszCmdLine)))
  111. {
  112. SxspDeleteDirectory(buffer);
  113. }
  114. }
  115. /*-----------------------------------------------------------------------------
  116. This function is called on Nt if we crash during an install, on the
  117. next login. It deletes temporary files/directories.
  118. -----------------------------------------------------------------------------*/
  119. VOID CALLBACK
  120. SxspRunDllDeleteDirectoryW(HWND hwnd, HINSTANCE hinst, PWSTR lpszCmdLine, int nCmdShow)
  121. {
  122. FN_TRACE_SMART_TLS();
  123. CStringBuffer buffer;
  124. if (buffer.Win32Assign(lpszCmdLine, ::wcslen(lpszCmdLine)))
  125. {
  126. SxspDeleteDirectory(buffer);
  127. }
  128. }
  129. /*-----------------------------------------------------------------------------
  130. This function sets up state for an upcoming series of installs, installs
  131. of assemblies/files.
  132. -----------------------------------------------------------------------------*/
  133. BOOL
  134. CDllRedir::BeginInstall(
  135. PACTCTXCTB_CALLBACK_DATA Data
  136. )
  137. {
  138. BOOL fSuccess = FALSE;
  139. FN_TRACE_WIN32(fSuccess);
  140. const DWORD dwManifestOperationFlags = Data->Header.ManifestOperationFlags;
  141. const bool fTransactional = (dwManifestOperationFlags & MANIFEST_OPERATION_INSTALL_FLAG_NOT_TRANSACTIONAL) == 0;
  142. CStringBuffer ManifestDirectory;
  143. if (!fTransactional)
  144. {
  145. //
  146. // m_strTempRootSlash is now actually the real root
  147. //
  148. IFW32FALSE_EXIT(::SxspGetAssemblyRootDirectory(m_strTempRootSlash));
  149. m_strTempRootSlash.RemoveTrailingPathSeparators(); // CreateDirectory doesn't like them
  150. // create \winnt\WinSxs, must not delete even on failure
  151. if (::CreateDirectoryW(m_strTempRootSlash, NULL))
  152. {
  153. // We don't care if this fails.
  154. ::SetFileAttributesW(m_strTempRootSlash, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
  155. }
  156. else if (::FusionpGetLastWin32Error() != ERROR_ALREADY_EXISTS)
  157. {
  158. goto Exit;
  159. }
  160. }
  161. else
  162. {
  163. CSmallStringBuffer uidBuffer;
  164. // Create the directory first, not the RunOnce value, in case the directory
  165. // already exists; we don't want to put it in the registry, then crash,
  166. // then end up deleting someone else's stuff.
  167. //
  168. // If we crash between creating the directory and setting the RunOnce value,
  169. // we do leak the directory. Darn. (You should be able to create/open
  170. // with delete on close/exit, then turn that off once you get far enough,
  171. // or in our case, never, and it should be applicable recursively..Win32
  172. // is not yet sufficient.)
  173. IFW32FALSE_EXIT(::SxspCreateWinSxsTempDirectory(m_strTempRootSlash, NULL, &uidBuffer, NULL));
  174. // ok, we created the directory, now make a note in the registry to delete it
  175. // upon login, if we crash
  176. IFALLOCFAILED_EXIT(m_pRunOnce = new CRunOnceDeleteDirectory);
  177. IFW32FALSE_EXIT(m_pRunOnce->Initialize(m_strTempRootSlash, &uidBuffer));
  178. }
  179. // create winnt\winsxs\manifests
  180. IFW32FALSE_EXIT(ManifestDirectory.Win32Assign(m_strTempRootSlash, m_strTempRootSlash.Cch()));
  181. IFW32FALSE_EXIT(ManifestDirectory.Win32AppendPathElement(MANIFEST_ROOT_DIRECTORY_NAME, NUMBER_OF(MANIFEST_ROOT_DIRECTORY_NAME) - 1));
  182. if (CreateDirectoryW(ManifestDirectory, NULL))
  183. {
  184. // We don't care if this fails.
  185. SetFileAttributesW(ManifestDirectory, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
  186. }
  187. else if (::FusionpGetLastWin32Error() != ERROR_ALREADY_EXISTS)
  188. {
  189. goto Exit;
  190. }
  191. IFW32FALSE_EXIT(m_strTempRootSlash.Win32Append(L"\\", 1));
  192. // fix it up..not sure this accomplishes anything..
  193. // if (!ActCtxGenCtx->m_AssemblyRootDirectoryBuffer.Win32Assign(m_strTempRootSlash))
  194. // {
  195. // goto Exit;
  196. // }
  197. fSuccess = TRUE;
  198. Exit:
  199. if (!fSuccess && fTransactional)
  200. {
  201. // rollback, which is not coincidentally identical to EndInstall aborting,
  202. // except that
  203. // here RemoveDirectoryW would be sufficient, there SxspDeleteDirectory is needed
  204. // here, we already know there is an error, and cleanup can't produce another
  205. // there, they have extra logic to progagage errors
  206. // we mask that by preserve LastError since we preserve it ourselves
  207. // and ignore the return value
  208. const DWORD dwLastError = ::FusionpGetLastWin32Error();
  209. const DWORD dwManifestOperationFlags = Data->Header.ManifestOperationFlags;
  210. Data->Header.ManifestOperationFlags |= MANIFEST_OPERATION_INSTALL_FLAG_ABORT;
  211. this->EndInstall(Data);
  212. Data->Header.ManifestOperationFlags = dwManifestOperationFlags; // our caller doesn't like us changing this
  213. ::FusionpSetLastWin32Error(dwLastError);
  214. }
  215. return fSuccess;
  216. }
  217. BOOL
  218. CDllRedir::AttemptInstallPolicies(
  219. const CBaseStringBuffer &m_strTempRootSlash,
  220. const CBaseStringBuffer &moveDestination,
  221. const BOOL fReplaceExisting,
  222. OUT BOOL &fFoundPolicesToInstall
  223. )
  224. {
  225. BOOL fSuccess = FALSE;
  226. FN_TRACE_WIN32(fSuccess);
  227. CFindFile FindPolicies;
  228. CStringBuffer sbSearcher;
  229. CStringBuffer PoliciesRootPath, PoliciesDestinationPath;
  230. DWORD dwAttributes;
  231. WIN32_FIND_DATAW FindPolicyData;
  232. fFoundPolicesToInstall = FALSE;
  233. // This is %installpath%\policies, turn it into %installpath%\policies\*
  234. IFW32FALSE_EXIT(PoliciesRootPath.Win32Assign(m_strTempRootSlash));
  235. IFW32FALSE_EXIT(PoliciesRootPath.Win32AppendPathElement(POLICY_ROOT_DIRECTORY_NAME, NUMBER_OF(POLICY_ROOT_DIRECTORY_NAME) - 1));
  236. IFW32FALSE_EXIT(PoliciesDestinationPath.Win32Assign(moveDestination));
  237. IFW32FALSE_EXIT(PoliciesDestinationPath.Win32AppendPathElement(POLICY_ROOT_DIRECTORY_NAME, NUMBER_OF(POLICY_ROOT_DIRECTORY_NAME) - 1));
  238. dwAttributes = GetFileAttributesW(PoliciesRootPath);
  239. if ((dwAttributes == 0xFFFFFFFF) || ((dwAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0))
  240. {
  241. #if DBG
  242. FusionpDbgPrintEx(FUSION_DBG_LEVEL_INFO,
  243. "SXS: %s() - No policies found (%ls not there), not attempting to install\n",
  244. __FUNCTION__,
  245. static_cast<PCWSTR>(PoliciesRootPath));
  246. #endif
  247. fSuccess = TRUE;
  248. goto Exit;
  249. }
  250. fFoundPolicesToInstall = TRUE;
  251. // Ensure that policies root always exists!
  252. PoliciesDestinationPath.RemoveTrailingPathSeparators();
  253. IFW32FALSE_ORIGINATE_AND_EXIT(
  254. ::CreateDirectoryW(PoliciesDestinationPath, NULL) ||
  255. (::FusionpGetLastWin32Error() == ERROR_ALREADY_EXISTS));
  256. IFW32FALSE_EXIT(sbSearcher.Win32Assign(PoliciesRootPath));
  257. IFW32FALSE_EXIT(sbSearcher.Win32AppendPathElement(L"*", 1));
  258. IFW32FALSE_EXIT(FindPolicies.Win32FindFirstFile(sbSearcher, &FindPolicyData));
  259. do
  260. {
  261. CStringBuffer sbSourcePolicyPath;
  262. CStringBuffer sbDestinationPolicyPath;
  263. if (FusionpIsDotOrDotDot(FindPolicyData.cFileName))
  264. continue;
  265. if ((FindPolicyData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0)
  266. continue;
  267. // Generate %installtemp%\policies\{thisfoundpolicy}
  268. IFW32FALSE_EXIT(sbSourcePolicyPath.Win32Assign(PoliciesRootPath));
  269. IFW32FALSE_EXIT(sbSourcePolicyPath.Win32AppendPathElement(FindPolicyData.cFileName, ::wcslen(FindPolicyData.cFileName)));
  270. IFW32FALSE_EXIT(sbDestinationPolicyPath.Win32Append(PoliciesDestinationPath));
  271. IFW32FALSE_EXIT(sbDestinationPolicyPath.Win32AppendPathElement(FindPolicyData.cFileName, ::wcslen(FindPolicyData.cFileName)));
  272. ::FusionpDbgPrintEx(FUSION_DBG_LEVEL_INFO,
  273. "SXS: %s():Found policy in staging area %ls\n\tMoving to %ls\n",
  274. __FUNCTION__,
  275. static_cast<PCWSTR>(sbSourcePolicyPath),
  276. static_cast<PCWSTR>(sbDestinationPolicyPath));
  277. //
  278. // Ensure that the target path exists
  279. //
  280. IFW32FALSE_ORIGINATE_AND_EXIT(
  281. ::FusionpCreateDirectories(sbDestinationPolicyPath, sbDestinationPolicyPath.Cch()) ||
  282. (::FusionpGetLastWin32Error() == ERROR_ALREADY_EXISTS));
  283. //
  284. // Go copy files from the source path that we've consed up to the
  285. // target path that we've also consed up. Unfortunately, SxspMoveFilesUnderDir
  286. // does not actually return the buffers to the state they were in before
  287. // the call (they leave a trailing slash), so we have to manually use the size
  288. // thingy above (Left(originalsize)) to avoid this.
  289. //
  290. IFW32FALSE_EXIT(SxspMoveFilesUnderDir(
  291. 0,
  292. sbSourcePolicyPath,
  293. sbDestinationPolicyPath,
  294. fReplaceExisting ? MOVEFILE_REPLACE_EXISTING : 0));
  295. }
  296. while(::FindNextFileW(FindPolicies, &FindPolicyData));
  297. if (::FusionpGetLastWin32Error() != ERROR_NO_MORE_FILES)
  298. {
  299. TRACE_WIN32_FAILURE_ORIGINATION(FindNextFileW);
  300. goto Exit;
  301. }
  302. ::SetLastError(ERROR_SUCCESS); // clear LastError
  303. IFW32FALSE_EXIT(FindPolicies.Win32Close());
  304. fSuccess = TRUE;
  305. Exit:
  306. return fSuccess;
  307. }
  308. BOOL
  309. CDllRedir::EndInstall(
  310. PACTCTXCTB_CALLBACK_DATA Data
  311. )
  312. {
  313. BOOL fSuccess = FALSE;
  314. FN_TRACE_WIN32(fSuccess);
  315. /*
  316. 1) Make sure all the queued copies have actually been done.
  317. 2) Enumerate \winnt\winsxs\guid
  318. renaming each to be in \winnt\winsxs
  319. upon rename conflicts
  320. compare all the files in each (by size)
  321. output debug string if mismatch
  322. just leave temp if mismatch (will be cleaned up in common path)
  323. success either way
  324. 3) delete temp; delete runonce value
  325. */
  326. // make sure all the queued copies have actually been done
  327. const DWORD dwManifestOperationFlags = Data->Header.ManifestOperationFlags;
  328. const BOOL fVerify = (dwManifestOperationFlags & MANIFEST_OPERATION_INSTALL_FLAG_NO_VERIFY) == 0;
  329. const BOOL fTransactional = (dwManifestOperationFlags & MANIFEST_OPERATION_INSTALL_FLAG_NOT_TRANSACTIONAL) == 0;
  330. const BOOL fReplaceExisting = (dwManifestOperationFlags & MANIFEST_OPERATION_INSTALL_FLAG_REPLACE_EXISTING) != 0;
  331. const BOOL fAbort = (dwManifestOperationFlags & MANIFEST_OPERATION_INSTALL_FLAG_ABORT) != 0;
  332. BOOL fPoliciesExist = FALSE;
  333. HashValidateResult HashCorrect = HashValidate_OtherProblems;
  334. CFileStream * pLogFileStream = NULL;
  335. if (fAbort)
  336. {
  337. fSuccess = TRUE;
  338. goto Exit;
  339. }
  340. if (fVerify)
  341. {
  342. CQueuedFileCopies::ConstIterator i;
  343. for (i = m_queuedFileCopies.Begin() ; i != m_queuedFileCopies.End() ; ++i)
  344. {
  345. //
  346. // Only bother to check this if we're not in OS-setup mode.
  347. //
  348. if (i->m_bHasHashInfo)
  349. {
  350. IFW32FALSE_EXIT(::SxspCheckHashDuringInstall(i->m_bHasHashInfo, i->m_path, i->m_HashString, i->m_HashAlgorithm, HashCorrect));
  351. if (HashCorrect != HashValidate_Matches)
  352. ORIGINATE_WIN32_FAILURE_AND_EXIT(FileHashDidNotMatchManifest, ERROR_SXS_FILE_HASH_MISMATCH);
  353. }
  354. //
  355. // Otherwise, let's do the simple thing and just make sure the file made it
  356. //
  357. else
  358. {
  359. DWORD dwAttributes = ::GetFileAttributesW(i->m_path);
  360. if (dwAttributes == -1)
  361. {
  362. ::FusionpDbgPrintEx(
  363. FUSION_DBG_LEVEL_ERROR,
  364. "SXS: %s() GetFileAttributesW(%ls)\n",
  365. __FUNCTION__,
  366. static_cast<PCWSTR>(i->m_path));
  367. TRACE_WIN32_FAILURE_ORIGINATION(GetFileAttributesW);
  368. goto Exit;
  369. }
  370. }
  371. }
  372. }
  373. if (fTransactional)
  374. {
  375. CFusionDirectoryDifference directoryDifference;
  376. CFindFile findFile;
  377. CStringBuffer tempStar; // also used for \winnt\winsxs\guid\foo
  378. WIN32_FIND_DATAW findData;
  379. SIZE_T realRootSlashLength = 0; // length of "\winnt\winsxs\"
  380. SIZE_T tempRootSlashLength = 0; // length of "\winnt\winxsx\guid\"
  381. CStringBuffer moveDestination; // \winnt\winsxs\foo
  382. IFW32FALSE_EXIT(::SxspGetAssemblyRootDirectory(moveDestination));
  383. IFW32FALSE_EXIT(moveDestination.Win32EnsureTrailingPathSeparator());
  384. realRootSlashLength = moveDestination.Cch();
  385. // move dirs from "\winnt\winsxs\InstallTemp\123456\" to \winnt\winsxs\x86_bar_1000_0409\"
  386. IFW32FALSE_EXIT(tempStar.Win32Assign(m_strTempRootSlash, m_strTempRootSlash.Cch()));
  387. tempRootSlashLength = tempStar.Cch();
  388. IFW32FALSE_EXIT(tempStar.Win32Append(L"*", 1));
  389. IFW32FALSE_EXIT(findFile.Win32FindFirstFile(tempStar, &findData));
  390. do
  391. {
  392. // skip . and ..
  393. if (::FusionpIsDotOrDotDot(findData.cFileName))
  394. continue;
  395. // there shouldn't be any files, skip them
  396. if ((findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != FILE_ATTRIBUTE_DIRECTORY)
  397. continue;
  398. // skip manifests dir, do it at the second pass
  399. if (_wcsicmp(findData.cFileName, MANIFEST_ROOT_DIRECTORY_NAME) == 0) // in-casesensitive compare
  400. continue;
  401. if (_wcsicmp(findData.cFileName, POLICY_ROOT_DIRECTORY_NAME) == 0)
  402. continue;
  403. moveDestination.Left(realRootSlashLength);
  404. tempStar.Left(tempRootSlashLength);
  405. IFW32FALSE_EXIT(moveDestination.Win32Append(findData.cFileName, ::wcslen(findData.cFileName)));
  406. IFW32FALSE_EXIT(tempStar.Win32Append(findData.cFileName, ::wcslen(findData.cFileName)));
  407. //
  408. // replace existing doesn't work on directories, but we'll give it a shot anyway,
  409. // maybe it'll work in some future better version of Windows..
  410. // and of course, the error when you try this is "access denied" which is
  411. // somewhat unexpected, you have appro access to delete the directory maybe,
  412. // but not replace it.. the ReplaceFile api is also explicitly described
  413. // as for files only
  414. //
  415. IFW32FALSE_EXIT(::SxspInstallMoveFileExW(tempStar, moveDestination, fReplaceExisting? MOVEFILE_REPLACE_EXISTING : 0, TRUE));
  416. } while (::FindNextFileW(findFile, &findData));
  417. if (::FusionpGetLastWin32Error() != ERROR_NO_MORE_FILES)
  418. {
  419. ::FusionpDbgPrintEx(
  420. FUSION_DBG_LEVEL_ERROR,
  421. "SXS.DLL: %s(): FindNextFile() failed:%ld\n",
  422. __FUNCTION__,
  423. ::FusionpGetLastWin32Error());
  424. goto Exit;
  425. }
  426. if (!findFile.Win32Close())
  427. {
  428. ::FusionpDbgPrintEx(
  429. FUSION_DBG_LEVEL_ERROR,
  430. "SXS.DLL: %s(): FindClose() failed:%ld\n",
  431. __FUNCTION__,
  432. ::FusionpGetLastWin32Error());
  433. goto Exit;
  434. }
  435. // Honk off and install polices - fFoundPolicesToInstall will be true if we really found any.
  436. moveDestination.Left(realRootSlashLength);
  437. IFW32FALSE_EXIT(this->AttemptInstallPolicies(m_strTempRootSlash, moveDestination, fReplaceExisting, fPoliciesExist));
  438. // move manifest file from "\winnt\winsxs\InstallTemp\123456\manifests\x86_cards.2000_0409.manifest" to
  439. // \winnt\winsxs\manifests\x86_bar_1000_0406.manifst"
  440. moveDestination.Left(realRootSlashLength);
  441. IFW32FALSE_EXIT(moveDestination.Win32Append(MANIFEST_ROOT_DIRECTORY_NAME, NUMBER_OF(MANIFEST_ROOT_DIRECTORY_NAME) - 1));
  442. IFW32FALSE_EXIT(moveDestination.Win32EnsureTrailingPathSeparator()); //"winnt\winsxs\manifests\"
  443. realRootSlashLength = moveDestination.Cch();
  444. tempStar.Left(tempRootSlashLength);
  445. IFW32FALSE_EXIT(tempStar.Win32Append(MANIFEST_ROOT_DIRECTORY_NAME, NUMBER_OF(MANIFEST_ROOT_DIRECTORY_NAME) - 1));
  446. IFW32FALSE_EXIT(tempStar.Win32EnsureTrailingPathSeparator()); //"winnt\winsxs\InstallTemp\123456\manifests\"
  447. tempRootSlashLength = tempStar.Cch();
  448. IFW32FALSE_EXIT(tempStar.Win32Append(L"*", 1));
  449. IFW32FALSE_EXIT(findFile.Win32FindFirstFile(tempStar, &findData));
  450. do
  451. {
  452. // skip . and ..
  453. if (FusionpIsDotOrDotDot(findData.cFileName))
  454. continue;
  455. // there shouldn't be any directories, skip them
  456. if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
  457. continue;
  458. moveDestination.Left(realRootSlashLength);
  459. tempStar.Left(tempRootSlashLength);
  460. IFW32FALSE_EXIT(moveDestination.Win32Append(findData.cFileName, ::wcslen(findData.cFileName)));
  461. IFW32FALSE_EXIT(tempStar.Win32Append(findData.cFileName, ::wcslen(findData.cFileName)));
  462. IFW32FALSE_EXIT(::SxspInstallMoveFileExW(tempStar, moveDestination, fReplaceExisting ? MOVEFILE_REPLACE_EXISTING : 0, TRUE));
  463. } while (::FindNextFileW(findFile, &findData));
  464. if (::FusionpGetLastWin32Error() != ERROR_NO_MORE_FILES)
  465. {
  466. ::FusionpDbgPrintEx(
  467. FUSION_DBG_LEVEL_ERROR,
  468. "SXS.DLL: %s(): FindNextFile() failed:%ld\n",
  469. __FUNCTION__,
  470. ::FusionpGetLastWin32Error());
  471. goto Exit;
  472. }
  473. if (!findFile.Win32Close())
  474. {
  475. ::FusionpDbgPrintEx(
  476. FUSION_DBG_LEVEL_ERROR,
  477. "SXS.DLL: %s(): FindClose() failed:%ld\n",
  478. __FUNCTION__,
  479. ::FusionpGetLastWin32Error());
  480. goto Exit;
  481. }
  482. }
  483. fSuccess = TRUE;
  484. Exit:
  485. if (pLogFileStream)
  486. {
  487. pLogFileStream->Close(); // ignore the error
  488. FUSION_DELETE_SINGLETON(pLogFileStream);
  489. }
  490. if (fTransactional)
  491. {
  492. DWORD dwLastError = ERROR_SUCCESS;
  493. ASSERT(m_pRunOnce != NULL);
  494. if (!fSuccess)
  495. dwLastError = ::FusionpGetLastWin32Error();
  496. if (!m_strTempRootSlash.IsEmpty())
  497. {
  498. if (!SxspDeleteDirectory(m_strTempRootSlash))
  499. {
  500. FusionpDbgPrintEx(
  501. FUSION_DBG_LEVEL_ERROR,
  502. "SXS.DLL: %s(): SxspDeleteDirectory(%ls) failed:%ld\n",
  503. __FUNCTION__,
  504. static_cast<PCWSTR>(m_strTempRootSlash),
  505. ::FusionpGetLastWin32Error());
  506. if (fSuccess)
  507. {
  508. fSuccess = FALSE;
  509. dwLastError = ::FusionpGetLastWin32Error();
  510. }
  511. // Close instead of Cancel so the delete wil be tried again upon reboot
  512. if (m_pRunOnce != NULL && !m_pRunOnce->Close() && fSuccess)
  513. {
  514. dwLastError = ::FusionpGetLastWin32Error();
  515. fSuccess = FALSE;
  516. }
  517. }
  518. }
  519. if (m_pRunOnce != NULL && !m_pRunOnce->Cancel() && fSuccess)
  520. {
  521. dwLastError = ::FusionpGetLastWin32Error();
  522. fSuccess = FALSE;
  523. }
  524. if (!fSuccess)
  525. ::FusionpSetLastWin32Error(dwLastError);
  526. }
  527. m_pRunOnce = NULL;
  528. return fSuccess;
  529. }
  530. //
  531. // we have to do this in three places, so it is worth the reuse
  532. //
  533. class CMungeFileReadOnlynessAroundReplacement
  534. {
  535. public:
  536. #if POST_WHISTLER_BETA1
  537. CMungeFileReadOnlynessAroundReplacement()
  538. : m_ReplaceExisting(false), m_FileAttributes(SXSP_INVALID_FILE_ATTRIBUTES)
  539. {
  540. }
  541. BOOL Initialize(
  542. const CBaseStringBuffer &rbuff,
  543. BOOL ReplaceExisting
  544. )
  545. {
  546. BOOL Success = FALSE;
  547. FN_TRACE_WIN32(Success);
  548. IFW32FALSE_EXIT(m_FileName.Win32Assign(rbuff));
  549. m_ReplaceExisting = ReplaceExisting;
  550. // deliberately ignore failure from GetFileAttributes
  551. // 1) It's ok if the file doesn't exist
  552. // 2) If there's a more serious problem, we'll hit it again immediately, but
  553. // that does lead to nested retry.
  554. m_FileAttributes = (ReplaceExisting ? ::GetFileAttributesW(FileName) : SXSP_INVALID_FILE_ATTRIBUTES);
  555. if (m_FileAttributes != SXSP_INVALID_FILE_ATTRIBUTES)
  556. ::SetFileAttributesW(FileName, 0);
  557. Success = TRUE;
  558. Exit:
  559. return Success;
  560. }
  561. ~CMungeFileReadOnlynessAroundReplacement()
  562. {
  563. if (m_ReplaceExisting && m_FileAttributes != SXSP_INVALID_FILE_ATTRIBUTES)
  564. {
  565. // error deliberately ignored
  566. SXSP_PRESERVE_LAST_ERROR(::SetFileAttributesW(m_FileName, m_FileAttributes));
  567. }
  568. }
  569. BOOL m_ReplaceExisting;
  570. CUnicodeStringBuffer m_FileName;
  571. DWORD m_FileAttributes;
  572. #else // POST_WHISTLER_BETA1
  573. // simpler code for beta1
  574. BOOL Initialize(
  575. PCWSTR FileName,
  576. BOOL /*ReplaceExisting*/
  577. )
  578. {
  579. // error deliberately ignored
  580. ::SetFileAttributesW(FileName, 0);
  581. return TRUE;
  582. }
  583. #endif // POST_WHISTLER_BETA1
  584. };
  585. BOOL
  586. CDllRedir::InstallCatalog(
  587. DWORD dwManifestOperationFlags,
  588. const CBaseStringBuffer &ManifestSourceBuffer,
  589. const CBaseStringBuffer &ManifestDestinationBuffer,
  590. PCACTCTXCTB_ASSEMBLY_CONTEXT AssemblyContext
  591. )
  592. {
  593. BOOL fSuccess = FALSE;
  594. FN_TRACE_WIN32(fSuccess);
  595. bool fHasCatalog = false;
  596. CMungeFileReadOnlynessAroundReplacement MungeCatalogAttributes;
  597. CStringBuffer CatalogSourceBuffer;
  598. CStringBuffer CatalogDestinationBuffer;
  599. ManifestValidationResult ManifestStatus = ManifestValidate_Unknown;
  600. BOOL fAreWeInOSSetupMode = FALSE;
  601. //
  602. // Determine the possible source and destination of the catalog file. This
  603. // needs to be done, even if we're not explicitly looking for a catalog, since
  604. // our heuristic still needs to check to see if there is one available.
  605. //
  606. IFW32FALSE_EXIT(CatalogDestinationBuffer.Win32Assign(ManifestDestinationBuffer));
  607. IFW32FALSE_EXIT(CatalogDestinationBuffer.Win32ChangePathExtension(FILE_EXTENSION_CATALOG, FILE_EXTENSION_CATALOG_CCH, eAddIfNoExtension));
  608. IFW32FALSE_EXIT(CatalogSourceBuffer.Win32Assign(ManifestSourceBuffer));
  609. IFW32FALSE_EXIT(CatalogSourceBuffer.Win32ChangePathExtension(FILE_EXTENSION_CATALOG, FILE_EXTENSION_CATALOG_CCH, eAddIfNoExtension));
  610. //
  611. // Note: We only attempt to deal with catalogs when there is installation info.
  612. // Even if there was no install data, we don't bother looking to see if there's
  613. // a catalog. Catalogs imply signatures and public key information, and require
  614. // a codebase to be reinstalled from. If you didn't provide such to the installer,
  615. // shame on you.
  616. //
  617. if (AssemblyContext->InstallationInfo != NULL)
  618. {
  619. PSXS_INSTALL_SOURCE_INFO pInfo = static_cast<PSXS_INSTALL_SOURCE_INFO>(AssemblyContext->InstallationInfo);
  620. ::FusionpDbgPrintEx(
  621. FUSION_DBG_LEVEL_INSTALLATION,
  622. "SXS.DLL: %s() found installation info at %p\n"
  623. " pInfo->dwFlags = 0x%08lx\n",
  624. __FUNCTION__, pInfo, (pInfo != NULL) ? pInfo->dwFlags : 0);
  625. //
  626. // Do we explicitly have a catalog?
  627. //
  628. fHasCatalog = ((pInfo->dwFlags & SXSINSTALLSOURCE_HAS_CATALOG) != 0);
  629. if (fHasCatalog)
  630. {
  631. FusionpDbgPrintEx(
  632. FUSION_DBG_LEVEL_INSTALLATION,
  633. "SXS.DLL: Using catalog because install source says that they're supposed to be there.\n");
  634. }
  635. //
  636. // Well, if we didn't, then we still should look.. maybe they forgot the flag.
  637. // But, only look if they don't mind us checking.
  638. //
  639. if (!(pInfo->dwFlags & SXSINSTALLSOURCE_DONT_DETECT_CATALOG) && !fHasCatalog)
  640. IFW32FALSE_EXIT(::SxspDoesFileExist(SXSP_DOES_FILE_EXIST_FLAG_COMPRESSION_AWARE, CatalogSourceBuffer, fHasCatalog));
  641. pInfo->dwFlags |= (fHasCatalog ? SXSINSTALLSOURCE_HAS_CATALOG : 0);
  642. }
  643. //
  644. // We officially no longer allow assemblies without catalogs in them.
  645. //
  646. //
  647. // BUGBUG NTRAID#273193
  648. //
  649. // This must be turned back on for beta2 or beta3!
  650. // - Updated 1/15/2001 (jonwis) : Turning on in winfuse private branch
  651. if (!fHasCatalog)
  652. {
  653. ::FusionpLogError(
  654. MSG_SXS_PUBLIC_ASSEMBLY_REQUIRES_CATALOG_AND_SIGNATURE,
  655. CEventLogString(ManifestSourceBuffer));
  656. ::FusionpSetLastWin32Error(ERROR_SXS_PROTECTION_CATALOG_FILE_MISSING);
  657. goto Exit;
  658. }
  659. //
  660. // If there's a catalog next to the manifest file, we should copy it over as well.
  661. // If the flag is set, and there's no catalog present, then something bad happened
  662. // at some point along the way - fail the installation!
  663. //
  664. if (fHasCatalog)
  665. {
  666. BOOL bInstallCatalogSuccess = FALSE;
  667. //
  668. // Copyfile it over. We do this rather than streaming because we don't
  669. // care about the contents of the catalog, it's binary.
  670. //
  671. IFW32FALSE_EXIT(MungeCatalogAttributes.Initialize(CatalogDestinationBuffer, TRUE));
  672. if (dwManifestOperationFlags & MANIFEST_OPERATION_INSTALL_FLAG_MOVE)
  673. {
  674. bInstallCatalogSuccess = ::SxspInstallDecompressAndMoveFileExW(
  675. CatalogSourceBuffer,
  676. CatalogDestinationBuffer,
  677. (dwManifestOperationFlags & MANIFEST_OPERATION_INSTALL_FLAG_REPLACE_EXISTING) ? MOVEFILE_REPLACE_EXISTING : 0);
  678. }
  679. else
  680. {
  681. bInstallCatalogSuccess =
  682. ::SxspInstallDecompressOrCopyFileW(
  683. CatalogSourceBuffer,
  684. CatalogDestinationBuffer,
  685. (dwManifestOperationFlags & MANIFEST_OPERATION_INSTALL_FLAG_REPLACE_EXISTING) ? FALSE : TRUE); // bFailIfExist == FALSE
  686. }
  687. if (!bInstallCatalogSuccess)
  688. {
  689. #if DBG
  690. ::FusionpDbgPrintEx(
  691. FUSION_DBG_LEVEL_ERROR,
  692. "SXS.DLL:%s Failed copying catalog file from [%ls] to [%ls] - Error was 0x%08x\n",
  693. __FUNCTION__,
  694. static_cast<PCWSTR>(CatalogSourceBuffer),
  695. static_cast<PCWSTR>(CatalogDestinationBuffer),
  696. ::FusionpGetLastWin32Error());
  697. #endif
  698. TRACE_WIN32_FAILURE_ORIGINATION(SxspInstallDecompressOrCopyFileW);
  699. goto Exit;
  700. }
  701. }
  702. //
  703. // If we're in OS-setup mode, then we don't bother to validate this manifest against
  704. // its catalog, instead assuming that the catalogs coming off the CD/installpoint
  705. // are golden. This does not protect us against malicious IT managers, warezer groups
  706. // putting bad bits in their distros, etc. But who cares, right?
  707. //
  708. IFW32FALSE_EXIT(::FusionpAreWeInOSSetupMode(&fAreWeInOSSetupMode));
  709. if (!fAreWeInOSSetupMode && fHasCatalog)
  710. {
  711. ULONG ulCatalogKeyLength;
  712. CPublicKeyInformation CatalogSignerInfo;
  713. CSmallStringBuffer sbStrongNameString;
  714. CSmallStringBuffer sbReferencePublicKeyToken;
  715. BOOL bHasPublicKeyToken = FALSE;
  716. BOOL bStrongNameMatches = FALSE;
  717. CAssemblyReference OurReference;
  718. IFW32FALSE_EXIT(OurReference.Initialize(AssemblyContext->AssemblyIdentity));
  719. IFW32FALSE_EXIT(OurReference.GetPublicKeyToken(&sbReferencePublicKeyToken, bHasPublicKeyToken));
  720. //
  721. // Validate the catalog and manifest, but don't check the strong name
  722. // yet - the file name isn't valid at this point.
  723. //
  724. IFW32FALSE_EXIT(::SxspValidateManifestAgainstCatalog(
  725. ManifestSourceBuffer,
  726. CatalogDestinationBuffer,
  727. ManifestStatus,
  728. MANIFESTVALIDATE_MODE_NO_STRONGNAME));
  729. //
  730. // If there's no catalog, or there is a catalog but it's broken, then
  731. // we need to complain and exit.
  732. //
  733. if (ManifestStatus != ManifestValidate_IsIntact)
  734. {
  735. #if DBG
  736. DWORD dwFileAttributes;
  737. ::FusionpDbgPrintEx(FUSION_DBG_LEVEL_ERROR,
  738. "SXS: ManifestStatus: %s (%lu)\n",
  739. SxspManifestValidationResultToString(ManifestStatus),
  740. static_cast<ULONG>(ManifestStatus));
  741. dwFileAttributes = GetFileAttributesW(ManifestSourceBuffer);
  742. if (dwFileAttributes == INVALID_FILE_ATTRIBUTES)
  743. ::FusionpDbgPrintEx(FUSION_DBG_LEVEL_ERROR,
  744. "SXS: GetFileAttributes(%ls):0x%lx, error:%lu\n",
  745. static_cast<PCWSTR>(ManifestSourceBuffer),
  746. dwFileAttributes,
  747. FusionpGetLastWin32Error());
  748. dwFileAttributes = GetFileAttributesW(CatalogDestinationBuffer);
  749. if (dwFileAttributes == INVALID_FILE_ATTRIBUTES)
  750. ::FusionpDbgPrintEx(FUSION_DBG_LEVEL_ERROR,
  751. "SXS: GetFileAttribtes(%ls):0x%lx, error:%lu\n",
  752. static_cast<PCWSTR>(CatalogDestinationBuffer),
  753. dwFileAttributes,
  754. FusionpGetLastWin32Error());
  755. #endif
  756. ::FusionpLogError(
  757. MSG_SXS_MANIFEST_CATALOG_VERIFY_FAILURE,
  758. CEventLogString(ManifestDestinationBuffer));
  759. ::FusionpSetLastWin32Error(ERROR_SXS_PROTECTION_CATALOG_NOT_VALID);
  760. goto Exit;
  761. }
  762. //
  763. // Get some useful information about the catalog's signer - opens the catalog
  764. // on the installation source.
  765. //
  766. IFW32FALSE_EXIT(CatalogSignerInfo.Initialize(CatalogDestinationBuffer));
  767. IFW32FALSE_EXIT(CatalogSignerInfo.GetPublicKeyBitLength(ulCatalogKeyLength));
  768. //
  769. // Minimally, we need some number of bits in the signing catalog's public key
  770. //
  771. if ((ulCatalogKeyLength < SXS_MINIMAL_SIGNING_KEY_LENGTH) || !bHasPublicKeyToken)
  772. {
  773. CSmallStringBuffer sbSignerName;
  774. IFW32FALSE_EXIT(CatalogSignerInfo.GetSignerNiceName(sbSignerName));
  775. ::FusionpLogError(
  776. MSG_SXS_CATALOG_SIGNER_KEY_TOO_SHORT,
  777. CEventLogString(sbSignerName),
  778. CEventLogString(CatalogSourceBuffer));
  779. goto Exit;
  780. }
  781. // Now compare the public key tokens
  782. IFW32FALSE_EXIT(CatalogSignerInfo.DoesStrongNameMatchSigner(sbReferencePublicKeyToken, bStrongNameMatches));
  783. if (!bStrongNameMatches)
  784. {
  785. CSmallStringBuffer sbSignerName;
  786. IFW32FALSE_EXIT(CatalogSignerInfo.GetSignerNiceName(sbSignerName));
  787. ::FusionpLogError(
  788. MSG_SXS_PUBLIC_KEY_TOKEN_AND_CATALOG_MISMATCH,
  789. CEventLogString(CatalogSourceBuffer),
  790. CEventLogString(sbSignerName),
  791. CEventLogString(sbReferencePublicKeyToken));
  792. goto Exit;
  793. }
  794. }
  795. fSuccess = TRUE;
  796. Exit:
  797. return fSuccess;
  798. }
  799. BOOL
  800. CDllRedir::InstallManifest(
  801. DWORD dwManifestOperationFlags,
  802. PCACTCTXCTB_ASSEMBLY_CONTEXT AssemblyContext
  803. )
  804. {
  805. BOOL fSuccess = FALSE;
  806. FN_TRACE_WIN32(fSuccess);
  807. BOOL fVerify;
  808. BOOL fTransactional;
  809. BOOL fReplaceExisting;
  810. DWORD OpenOrCreateManifestDestination;
  811. CTeeStream* TeeStreamForManifestInstall = NULL;
  812. CFullPathSplitPointers SplitManifestSource;
  813. CMungeFileReadOnlynessAroundReplacement MungeManifestAttributes;
  814. CAssemblyReference TempAssemblyReference;
  815. //
  816. // Windows Setup is restartable, so we must be too when it calls us.
  817. // ReplaceExisting is probably enough to use CREATE_ALWAYS, but lets be safer for
  818. // now and check both weakenings.
  819. //
  820. CStringBuffer ManifestSourceBuffer;
  821. CStringBuffer ManifestDestinationBuffer;
  822. CStringBuffer ManifestFileNameBuffer;
  823. CStringBuffer CatalogSourceBuffer;
  824. CStringBuffer CatalogDestinationBuffer;
  825. fVerify = (dwManifestOperationFlags & MANIFEST_OPERATION_INSTALL_FLAG_NO_VERIFY) == 0;
  826. fTransactional = (dwManifestOperationFlags & MANIFEST_OPERATION_INSTALL_FLAG_NOT_TRANSACTIONAL) == 0;
  827. fReplaceExisting = (dwManifestOperationFlags & MANIFEST_OPERATION_INSTALL_FLAG_REPLACE_EXISTING) != 0;
  828. OpenOrCreateManifestDestination = (fReplaceExisting && !fTransactional) ? CREATE_ALWAYS : CREATE_NEW;
  829. TeeStreamForManifestInstall = reinterpret_cast<CTeeStream*>(AssemblyContext->TeeStreamForManifestInstall);
  830. #if FUSION_PRECOMPILED_MANIFEST
  831. CMungeFileReadOnlynessAroundReplacement MungePrecompiledManifestAttributes;
  832. CPrecompiledManifestWriterStream * pcmWriterStream = reinterpret_cast<CPrecompiledManifestWriterStream *>(AssemblyContext->pcmWriterStream);
  833. #endif
  834. PARAMETER_CHECK(AssemblyContext != NULL);
  835. INTERNAL_ERROR_CHECK(AssemblyContext->TeeStreamForManifestInstall != NULL);
  836. // Get "\windir\winsxs\install\guid\manifests" or Get "\windir\winsxs\install\guid\policies".
  837. IFW32FALSE_EXIT(
  838. ::SxspGenerateSxsPath(
  839. SXSP_GENERATE_SXS_PATH_FLAG_PARTIAL_PATH, // Flags
  840. (AssemblyContext->Flags & ACTCTXCTB_ASSEMBLY_CONTEXT_IS_SYSTEM_POLICY_INSTALLATION)? SXSP_GENERATE_SXS_PATH_PATHTYPE_POLICY : SXSP_GENERATE_SXS_PATH_PATHTYPE_MANIFEST,
  841. m_strTempRootSlash,
  842. m_strTempRootSlash.Cch(),
  843. AssemblyContext->AssemblyIdentity,
  844. ManifestDestinationBuffer));
  845. // remove the trailing slash because CreateDirectory maybe sometimes doesn't like it
  846. ManifestDestinationBuffer.RemoveTrailingPathSeparators();
  847. IFW32FALSE_ORIGINATE_AND_EXIT(
  848. ::CreateDirectoryW(ManifestDestinationBuffer, NULL)
  849. || ::FusionpGetLastWin32Error() == ERROR_ALREADY_EXISTS);
  850. IFW32FALSE_EXIT(ManifestSourceBuffer.Win32Assign(AssemblyContext->ManifestPath, AssemblyContext->ManifestPathCch));
  851. // get "x86_bar_1000_0409"
  852. IFW32FALSE_EXIT(
  853. ::SxspGenerateSxsPath(
  854. SXSP_GENERATE_SXS_PATH_FLAG_OMIT_ROOT
  855. | ((AssemblyContext->Flags & ACTCTXCTB_ASSEMBLY_CONTEXT_IS_SYSTEM_POLICY_INSTALLATION) ? SXSP_GENERATE_SXS_PATH_FLAG_OMIT_VERSION : 0),
  856. SXSP_GENERATE_SXS_PATH_PATHTYPE_ASSEMBLY,
  857. m_strTempRootSlash,
  858. m_strTempRootSlash.Cch(),
  859. AssemblyContext->AssemblyIdentity,
  860. ManifestFileNameBuffer));
  861. // create policies\x86_policy.6.0.Microsoft.windows.cards_pulicKeyToken_en-us_1223423423
  862. IFW32FALSE_EXIT(ManifestDestinationBuffer.Win32AppendPathElement(ManifestFileNameBuffer));
  863. if (AssemblyContext->Flags & ACTCTXCTB_ASSEMBLY_CONTEXT_IS_SYSTEM_POLICY_INSTALLATION)
  864. {
  865. PCWSTR pszVersion = NULL;
  866. SIZE_T VersionCch = 0;
  867. // for policy installation, create a subdir under Policies
  868. IFW32FALSE_ORIGINATE_AND_EXIT(
  869. ::CreateDirectoryW(ManifestDestinationBuffer, NULL)
  870. || ::FusionpGetLastWin32Error() == ERROR_ALREADY_EXISTS);
  871. //generate policy file name, like 1.0.0.0.policy
  872. IFW32FALSE_EXIT(
  873. ::SxspGetAssemblyIdentityAttributeValue(
  874. SXSP_GET_ASSEMBLY_IDENTITY_ATTRIBUTE_VALUE_FLAG_NOT_FOUND_RETURNS_NULL,
  875. AssemblyContext->AssemblyIdentity,
  876. &s_IdentityAttribute_version,
  877. &pszVersion,
  878. &VersionCch));
  879. INTERNAL_ERROR_CHECK(VersionCch != 0);
  880. IFW32FALSE_EXIT(ManifestDestinationBuffer.Win32EnsureTrailingPathSeparator());
  881. IFW32FALSE_EXIT(ManifestDestinationBuffer.Win32Append(pszVersion, VersionCch));
  882. // .policy
  883. IFW32FALSE_EXIT(ManifestDestinationBuffer.Win32Append(ASSEMBLY_POLICY_FILE_NAME_SUFFIX, NUMBER_OF(ASSEMBLY_POLICY_FILE_NAME_SUFFIX) - 1));
  884. }
  885. else
  886. {
  887. // .manifest
  888. ManifestDestinationBuffer.RemoveTrailingPathSeparators();
  889. IFW32FALSE_EXIT(ManifestDestinationBuffer.Win32Append(ASSEMBLY_MANIFEST_FILE_NAME_SUFFIX, NUMBER_OF(ASSEMBLY_MANIFEST_FILE_NAME_SUFFIX) - 1));
  890. }
  891. IFW32FALSE_EXIT(MungeManifestAttributes.Initialize(ManifestDestinationBuffer, fReplaceExisting));
  892. //
  893. // Try installing the catalog that goes with this assembly
  894. //
  895. IFW32FALSE_EXIT(
  896. this->InstallCatalog(
  897. dwManifestOperationFlags,
  898. ManifestSourceBuffer,
  899. ManifestDestinationBuffer,
  900. AssemblyContext));
  901. FusionpDbgPrintEx(
  902. FUSION_DBG_LEVEL_INSTALLATION,
  903. "SXS.DLL: Sinking manifest to \"%S\"\n", static_cast<PCWSTR>(ManifestDestinationBuffer));
  904. //
  905. // Set the manifest sink after trying to install the catalog, so that if there is no
  906. // catalog, we do not install the manifest (we don't always have InstallTemp to rely on).
  907. //
  908. IFW32FALSE_EXIT(TeeStreamForManifestInstall->SetSink(ManifestDestinationBuffer, OpenOrCreateManifestDestination));
  909. #if FUSION_PRECOMPILED_MANIFEST
  910. IFW32FALSE_EXIT(
  911. ManifestDestinationBuffer.Win32ChangePathExtension(
  912. PRECOMPILED_MANIFEST_EXTENSION,
  913. NUMBER_OF(PRECOMPILED_MANIFEST_EXTENSION) - 1,
  914. NULL,
  915. eErrorIfNoExtension));
  916. IFW32FALSE_EXIT(MungePrecompiledManifestAttributes.Initialize(ManifestDestinationBuffer, fReplaceExisting));
  917. IFW32FALSE_EXIT(pcmWriterStream->SetSink(ManifestDestinationBuffer, OpenOrCreateManifestDestination));
  918. #endif
  919. fSuccess = TRUE;
  920. Exit:
  921. return fSuccess;
  922. }
  923. BOOL
  924. CDllRedir::InstallFile(
  925. PACTCTXCTB_CALLBACK_DATA Data,
  926. const CBaseStringBuffer &FileNameBuffer
  927. )
  928. {
  929. BOOL fSuccess = FALSE;
  930. FN_TRACE_WIN32(fSuccess);
  931. CStringBuffer SourceBuffer;
  932. CStringBuffer DestinationBuffer;
  933. SIZE_T DirectoryLength = 0;
  934. CStringBuffer SourceFileNameBuffer;
  935. ULONGLONG SourceFileSize = 0;
  936. bool fFound = false;
  937. SIZE_T cb;
  938. ULONG Disposition = (Data->Header.ManifestOperationFlags & MANIFEST_OPERATION_INSTALL_FLAG_MOVE) ? SXS_INSTALLATION_FILE_COPY_DISPOSITION_PLEASE_MOVE : SXS_INSTALLATION_FILE_COPY_DISPOSITION_PLEASE_COPY;
  939. const DWORD dwManifestOperationFlags = Data->Header.ManifestOperationFlags;
  940. const BOOL fVerify = (dwManifestOperationFlags & MANIFEST_OPERATION_INSTALL_FLAG_NO_VERIFY) == 0;
  941. const BOOL fTransactional = (dwManifestOperationFlags & MANIFEST_OPERATION_INSTALL_FLAG_NOT_TRANSACTIONAL) == 0;
  942. const BOOL fReplaceExisting = (dwManifestOperationFlags & MANIFEST_OPERATION_INSTALL_FLAG_REPLACE_EXISTING) != 0;
  943. ALG_ID HashAlgId = FUSION_DEFAULT_HASH_ALGORITHM;
  944. bool fHasHashData = false;
  945. bool fHasHashAlgName = false;
  946. HashValidateResult HashCorrect = HashValidate_OtherProblems;
  947. CStringBuffer HashDataString;
  948. CSmallStringBuffer HashAlgNiceName;
  949. IFW32FALSE_EXIT(
  950. ::SxspGenerateSxsPath(
  951. 0, // Flags
  952. SXSP_GENERATE_SXS_PATH_PATHTYPE_ASSEMBLY,
  953. m_strTempRootSlash,
  954. m_strTempRootSlash.Cch(),
  955. Data->ElementParsed.AssemblyContext->AssemblyIdentity,
  956. DestinationBuffer));
  957. IFW32FALSE_EXIT(DestinationBuffer.Win32Append(static_cast<PCWSTR>(FileNameBuffer), FileNameBuffer.Cch()));
  958. DirectoryLength = 1 + DestinationBuffer.CchWithoutLastPathElement();
  959. // Take the manifest path, trim back to the directory name and add the file...
  960. IFW32FALSE_EXIT(SourceBuffer.Win32Assign(Data->ElementParsed.AssemblyContext->ManifestPath, Data->ElementParsed.AssemblyContext->ManifestPathCch));
  961. SourceBuffer.RemoveLastPathElement();
  962. IFW32FALSE_EXIT(
  963. ::SxspGetAttributeValue(
  964. 0,
  965. &s_AttributeName_sourceName,
  966. &Data->ElementParsed,
  967. fFound,
  968. sizeof(SourceFileNameBuffer),
  969. &SourceFileNameBuffer,
  970. cb,
  971. NULL,
  972. 0));
  973. PCWSTR SourceFileName;
  974. if (fFound)
  975. SourceFileName = SourceFileNameBuffer;
  976. else
  977. SourceFileName = FileNameBuffer;
  978. // Extract information about the hashing stuff that's included on this node
  979. IFW32FALSE_EXIT(
  980. ::SxspGetAttributeValue(
  981. 0,
  982. &s_AttributeName_hash,
  983. &Data->ElementParsed,
  984. fHasHashData,
  985. sizeof(HashDataString),
  986. &HashDataString,
  987. cb,
  988. NULL,
  989. 0));
  990. IFW32FALSE_EXIT(
  991. ::SxspGetAttributeValue(
  992. 0,
  993. &s_AttributeName_hashalg,
  994. &Data->ElementParsed,
  995. fHasHashAlgName,
  996. sizeof(HashAlgNiceName),
  997. &HashAlgNiceName,
  998. cb,
  999. NULL,
  1000. 0));
  1001. //
  1002. // Neat. Find out what the hash algorithm was.
  1003. //
  1004. if (fHasHashAlgName)
  1005. {
  1006. if (!::SxspHashAlgFromString(HashAlgNiceName, HashAlgId))
  1007. {
  1008. ::FusionpLogError(
  1009. MSG_SXS_INVALID_FILE_HASH_FROM_COPY_CALLBACK,
  1010. CEventLogString(HashAlgNiceName));
  1011. goto Exit;
  1012. }
  1013. }
  1014. else
  1015. {
  1016. HashAlgId = FUSION_DEFAULT_HASH_ALGORITHM;
  1017. }
  1018. IFW32FALSE_EXIT(SourceBuffer.Win32AppendPathElement(SourceFileName, (SourceFileName != NULL) ? ::wcslen(SourceFileName) : 0));
  1019. IFW32FALSE_EXIT(::SxspGetFileSize(SXSP_GET_FILE_SIZE_FLAG_COMPRESSION_AWARE, SourceBuffer, SourceFileSize));
  1020. //
  1021. // And add the file's metadata to the currently running metadata blob
  1022. //
  1023. {
  1024. CSecurityMetaData *pMetaDataObject = reinterpret_cast<CSecurityMetaData*>(Data->Header.InstallationContext->SecurityMetaData);
  1025. if ( pMetaDataObject != NULL )
  1026. {
  1027. CSmallStringBuffer sbuffFileShortName;
  1028. IFW32FALSE_EXIT(sbuffFileShortName.Win32Assign(SourceFileName, ::wcslen(SourceFileName)));
  1029. IFW32FALSE_EXIT(pMetaDataObject->QuickAddFileHash(
  1030. sbuffFileShortName,
  1031. HashAlgId,
  1032. HashDataString));
  1033. }
  1034. }
  1035. if ((Data->Header.InstallationContext != NULL) &&
  1036. (Data->Header.InstallationContext->Callback != NULL))
  1037. {
  1038. Disposition = 0;
  1039. SXS_INSTALLATION_FILE_COPY_CALLBACK_PARAMETERS parameters = {sizeof(parameters)};
  1040. parameters.pvContext = Data->Header.InstallationContext->Context;
  1041. parameters.dwFileFlags = 0;
  1042. parameters.pAlternateSource = NULL; // future IStream
  1043. parameters.pSourceFile = SourceBuffer;
  1044. parameters.pDestinationFile = DestinationBuffer;
  1045. parameters.nFileSize = SourceFileSize;
  1046. parameters.nDisposition = 0;
  1047. IFW32FALSE_EXIT((*Data->Header.InstallationContext->Callback)(&parameters));
  1048. Disposition = parameters.nDisposition;
  1049. }
  1050. switch (Disposition)
  1051. {
  1052. default:
  1053. ::FusionpLogError(
  1054. MSG_SXS_INVALID_DISPOSITION_FROM_FILE_COPY_CALLBACK,
  1055. CEventLogString(SxspInstallDispositionToStringW(Disposition)));
  1056. goto Exit;
  1057. case SXS_INSTALLATION_FILE_COPY_DISPOSITION_FILE_COPIED:
  1058. {
  1059. if (fVerify)
  1060. {
  1061. ULONGLONG DestinationFileSize = 0;
  1062. IFW32FALSE_EXIT(::SxspGetFileSize(0, DestinationBuffer, DestinationFileSize));
  1063. INTERNAL_ERROR_CHECK(SourceFileSize == DestinationFileSize);
  1064. //
  1065. // (jonwis) Add a verification check to make sure that the file copied
  1066. // is really the one that they wanted from the file hash information.
  1067. // Do this only if we're not in OS-setup mode.
  1068. //
  1069. IFW32FALSE_EXIT(::SxspCheckHashDuringInstall(fHasHashData, DestinationBuffer, HashDataString, HashAlgId, HashCorrect));
  1070. if (HashCorrect != HashValidate_Matches)
  1071. ORIGINATE_WIN32_FAILURE_AND_EXIT(FileHashMismatch, ERROR_SXS_FILE_HASH_MISMATCH);
  1072. }
  1073. }
  1074. break;
  1075. case SXS_INSTALLATION_FILE_COPY_DISPOSITION_FILE_QUEUED:
  1076. {
  1077. if (fVerify)
  1078. {
  1079. CFusionFilePathAndSize verifyQueuedFileCopy;
  1080. // Copy our hashing info over. Yes, I really do mean =, not ==.
  1081. if (verifyQueuedFileCopy.m_bHasHashInfo = fHasHashData)
  1082. {
  1083. IFW32FALSE_EXIT(verifyQueuedFileCopy.m_HashString.Win32Assign(HashDataString));
  1084. verifyQueuedFileCopy.m_HashAlgorithm = HashAlgId;
  1085. }
  1086. IFW32FALSE_EXIT(verifyQueuedFileCopy.m_path.Win32Assign(DestinationBuffer));
  1087. verifyQueuedFileCopy.m_size = SourceFileSize;
  1088. IFW32FALSE_EXIT(m_queuedFileCopies.Win32Append(verifyQueuedFileCopy));
  1089. }
  1090. }
  1091. break;
  1092. case SXS_INSTALLATION_FILE_COPY_DISPOSITION_PLEASE_MOVE:
  1093. case SXS_INSTALLATION_FILE_COPY_DISPOSITION_PLEASE_COPY:
  1094. {
  1095. CStringBuffer DestinationDirectory;
  1096. CMungeFileReadOnlynessAroundReplacement MungeFileAttributes;
  1097. IFW32FALSE_EXIT(DestinationDirectory.Win32Assign(DestinationBuffer));
  1098. DestinationDirectory.RemoveLastPathElement();
  1099. IFW32FALSE_EXIT(::FusionpCreateDirectories(DestinationDirectory, DestinationDirectory.Cch()));
  1100. if (Disposition == SXS_INSTALLATION_FILE_COPY_DISPOSITION_PLEASE_COPY)
  1101. {
  1102. DWORD dwLastError;
  1103. IFW32FALSE_EXIT(MungeFileAttributes.Initialize(DestinationBuffer, fReplaceExisting));
  1104. fSuccess = ::SxspInstallDecompressOrCopyFileW(
  1105. SourceBuffer,
  1106. DestinationBuffer,
  1107. !fReplaceExisting); //bFailIfExist
  1108. dwLastError = ::FusionpGetLastWin32Error();
  1109. if ((!fSuccess) && (dwLastError == ERROR_FILE_EXISTS))
  1110. {
  1111. ULONGLONG cbSource, cbDestination;
  1112. if ((!::SxspGetFileSize(SXSP_GET_FILE_SIZE_FLAG_COMPRESSION_AWARE, SourceBuffer, cbSource)) ||
  1113. (!::SxspGetFileSize(0, DestinationBuffer, cbDestination)))
  1114. {
  1115. ::FusionpSetLastWin32Error(dwLastError);
  1116. }
  1117. else if (cbSource == cbDestination)
  1118. {
  1119. fSuccess = TRUE;
  1120. }
  1121. }
  1122. }
  1123. else
  1124. {
  1125. fSuccess = ::SxspInstallMoveFileExW(
  1126. SourceBuffer,
  1127. DestinationBuffer,
  1128. MOVEFILE_COPY_ALLOWED | (fReplaceExisting ? MOVEFILE_REPLACE_EXISTING : 0));
  1129. // move fails on from resource, so general idea: try copy upon move failure
  1130. if (!fSuccess)
  1131. {
  1132. DWORD dwLastError = ::FusionpGetLastWin32Error();
  1133. if ((dwLastError == ERROR_ACCESS_DENIED) ||
  1134. (dwLastError == ERROR_USER_MAPPED_FILE) ||
  1135. (dwLastError == ERROR_SHARING_VIOLATION))
  1136. {
  1137. fSuccess = ::SxspInstallDecompressOrCopyFileW(
  1138. SourceBuffer,
  1139. DestinationBuffer,
  1140. !fReplaceExisting); // bFailIfExist
  1141. }
  1142. }
  1143. }
  1144. if (fSuccess)
  1145. {
  1146. IFW32FALSE_EXIT(::SxspCheckHashDuringInstall(fHasHashData, DestinationBuffer, HashDataString, HashAlgId, HashCorrect));
  1147. if (HashCorrect != HashValidate_Matches)
  1148. ORIGINATE_WIN32_FAILURE_AND_EXIT(FileHashMismatch, ERROR_SXS_FILE_HASH_MISMATCH);
  1149. else
  1150. fSuccess = TRUE;
  1151. goto Exit;
  1152. }
  1153. else
  1154. {
  1155. ULONGLONG iDupFileSize = 0;
  1156. DWORD dwLastError = ::FusionpGetLastWin32Error();
  1157. CStringBuffer renameExistingAway;
  1158. CSmallStringBuffer uidBuffer;
  1159. CFullPathSplitPointers splitExisting;
  1160. bool fFatal =
  1161. (
  1162. dwLastError != ERROR_FILE_EXISTS // !fReplaceExisting
  1163. && dwLastError != ERROR_ALREADY_EXISTS // !fReplaceExisting
  1164. && dwLastError != ERROR_ACCESS_DENIED
  1165. && dwLastError != ERROR_USER_MAPPED_FILE // fReplaceExisting
  1166. && dwLastError != ERROR_SHARING_VIOLATION); // fReplaceExisting
  1167. if (fFatal)
  1168. {
  1169. ::SxspInstallPrint(
  1170. "SxsInstall: Copy/MoveFileW(%ls,%ls) failed %d, %s.\n",
  1171. static_cast<PCWSTR>(SourceBuffer),
  1172. static_cast<PCWSTR>(DestinationBuffer),
  1173. ::FusionpGetLastWin32Error(),
  1174. fFatal ? "fatal" : "not fatal");
  1175. ::FusionpDbgPrintEx(FUSION_DBG_LEVEL_ERROR,
  1176. "SXS.dll: Copy/MoveFileW(%ls,%ls) failed %d, %s.\n",
  1177. static_cast<PCWSTR>(SourceBuffer),
  1178. static_cast<PCWSTR>(DestinationBuffer),
  1179. ::FusionpGetLastWin32Error(),
  1180. fFatal ? "fatal" : "not fatal");
  1181. goto Exit;
  1182. }
  1183. //
  1184. // This could be winlogon (or setup) holding open comctl, so
  1185. // try harder. Move the file away and then copy.
  1186. // Consider ReplaceFile here for atomicity, but ReplaceFile
  1187. // is kind of big and scary and unknown.
  1188. //
  1189. if (fTransactional)
  1190. {
  1191. ::SxspInstallPrint("SxsInstall: Failure to copy file into temp, someone's opening temp?\n");
  1192. }
  1193. if (!splitExisting.Initialize(SourceBuffer))
  1194. {
  1195. goto CheckSizes;
  1196. }
  1197. if (!::SxspCreateWinSxsTempDirectory(renameExistingAway, NULL, &uidBuffer, NULL))
  1198. {
  1199. goto CheckSizes;
  1200. }
  1201. if (!renameExistingAway.Win32AppendPathElement(splitExisting.m_name, (splitExisting.m_name != NULL) ? ::wcslen(splitExisting.m_name) : 0))
  1202. {
  1203. goto CheckSizes;
  1204. }
  1205. if (!::MoveFileExW(DestinationBuffer, renameExistingAway, 0))
  1206. {
  1207. ::SxspInstallPrint(
  1208. "SxsInstall: MoveFileExW(%ls,%ls,0) failed %d.\n",
  1209. static_cast<PCWSTR>(DestinationBuffer),
  1210. static_cast<PCWSTR>(renameExistingAway),
  1211. ::FusionpGetLastWin32Error());
  1212. goto CheckSizes;
  1213. }
  1214. if (!::SxspInstallDecompressOrCopyFileW(
  1215. SourceBuffer,
  1216. DestinationBuffer,
  1217. FALSE))
  1218. {
  1219. ::SxspInstallPrint(
  1220. "SxsInstall: CopyFile(%ls, %ls, TRUE) failed %d.\n",
  1221. static_cast<PCWSTR>(SourceBuffer),
  1222. static_cast<PCWSTR>(DestinationBuffer),
  1223. ::FusionpGetLastWin32Error());
  1224. //roll back
  1225. if (!::MoveFileExW(renameExistingAway, DestinationBuffer, 0))
  1226. {
  1227. ::SxspInstallPrint(
  1228. "SxsInstall: Rollback MoveFileExW(%ls, %ls, 0) failed %d; this is very bad.\n",
  1229. static_cast<PCWSTR>(renameExistingAway),
  1230. static_cast<PCWSTR>(DestinationBuffer),
  1231. ::FusionpGetLastWin32Error()
  1232. );
  1233. }
  1234. goto CheckSizes;
  1235. }
  1236. fSuccess = TRUE;
  1237. goto Exit;
  1238. CheckSizes:
  1239. IFW32FALSE_EXIT(::SxspGetFileSize(0, DestinationBuffer, iDupFileSize));
  1240. if (iDupFileSize != SourceFileSize)
  1241. {
  1242. ::SxspInstallPrint("SxsInstall: " __FUNCTION__ " Error %d encountered, file sizes not the same, assumed equal, propagating error.\n", dwLastError);
  1243. ::FusionpSetLastWin32Error(dwLastError);
  1244. goto Exit;
  1245. }
  1246. ::SxspInstallPrint("SxsInstall: " __FUNCTION__ " Error %d encountered, file sizes the same, assumed equal, claiming success.\n", dwLastError);
  1247. }
  1248. break;
  1249. }
  1250. }
  1251. fSuccess = TRUE;
  1252. Exit:
  1253. return fSuccess;
  1254. }
  1255. VOID
  1256. CDllRedir::ContributorCallback(
  1257. PACTCTXCTB_CALLBACK_DATA Data
  1258. )
  1259. {
  1260. FN_TRACE();
  1261. CDllRedir *pDllRedir = reinterpret_cast<CDllRedir*>(Data->Header.ActCtxGenContext);
  1262. PSTRING_SECTION_GENERATION_CONTEXT SSGenContext = NULL;
  1263. PDLL_REDIRECTION_CONTEXT DllRedirectionContext = NULL;
  1264. PDLL_REDIRECTION_ENTRY Entry = NULL;
  1265. if (pDllRedir != NULL)
  1266. SSGenContext = pDllRedir->m_SSGenContext;
  1267. if (SSGenContext != NULL)
  1268. DllRedirectionContext = (PDLL_REDIRECTION_CONTEXT) ::SxsGetStringSectionGenerationContextCallbackContext(SSGenContext);
  1269. switch (Data->Header.Reason)
  1270. {
  1271. case ACTCTXCTB_CBREASON_PARSEENDING:
  1272. Data->ParseEnding.Success = FALSE;
  1273. /*
  1274. at this point we have enough information to form the install path,
  1275. so get the TeeStream to start writing the manifest to disk
  1276. */
  1277. if (Data->Header.ManifestOperation == MANIFEST_OPERATION_INSTALL)
  1278. IFW32FALSE_EXIT(InstallManifest(Data->Header.ManifestOperationFlags, Data->ParseEnding.AssemblyContext));
  1279. Data->ParseEnding.Success = TRUE;
  1280. break;
  1281. case ACTCTXCTB_CBREASON_PARSEENDED:
  1282. if ( Data->Header.ManifestOperation == MANIFEST_OPERATION_INSTALL )
  1283. {
  1284. PACTCTXCTB_CBPARSEENDED pParseEnded = reinterpret_cast<PACTCTXCTB_CBPARSEENDED>(Data);
  1285. CSecurityMetaData *psmdSecurity =
  1286. reinterpret_cast<CSecurityMetaData*>(pParseEnded->AssemblyContext->SecurityMetaData);
  1287. CTeeStreamWithHash *pTeeStreamWithHash =
  1288. reinterpret_cast<CTeeStreamWithHash*>(pParseEnded->AssemblyContext->TeeStreamForManifestInstall);
  1289. CFusionArray<BYTE> baManifestHashBytes;
  1290. if ( ( psmdSecurity != NULL ) && ( pTeeStreamWithHash != NULL ) )
  1291. {
  1292. IFW32FALSE_EXIT(baManifestHashBytes.Win32Initialize());
  1293. IFW32FALSE_EXIT(pTeeStreamWithHash->GetCryptHash().Win32GetValue(baManifestHashBytes));
  1294. IFW32FALSE_EXIT(psmdSecurity->SetManifestHash( baManifestHashBytes ));
  1295. }
  1296. }
  1297. break;
  1298. case ACTCTXCTB_CBREASON_ACTCTXGENBEGINNING:
  1299. Data->GenBeginning.Success = FALSE;
  1300. if (Data->Header.ManifestOperation == MANIFEST_OPERATION_GENERATE_ACTIVATION_CONTEXT)
  1301. {
  1302. IFALLOCFAILED_EXIT(DllRedirectionContext = new DLL_REDIRECTION_CONTEXT);
  1303. IFW32FALSE_EXIT(::SxsInitStringSectionGenerationContext(
  1304. &m_SSGenContext,
  1305. ACTIVATION_CONTEXT_DATA_DLL_REDIRECTION_FORMAT_WHISTLER,
  1306. TRUE,
  1307. &::SxspDllRedirectionStringSectionGenerationCallback,
  1308. DllRedirectionContext));
  1309. DllRedirectionContext = NULL;
  1310. }
  1311. if (Data->Header.ManifestOperation == MANIFEST_OPERATION_INSTALL)
  1312. {
  1313. IFW32FALSE_EXIT(this->BeginInstall(Data));
  1314. }
  1315. Data->GenBeginning.Success = TRUE;
  1316. break;
  1317. case ACTCTXCTB_CBREASON_ACTCTXGENENDING:
  1318. Data->GenEnding.Success = FALSE;
  1319. if (Data->Header.ManifestOperation == MANIFEST_OPERATION_INSTALL)
  1320. IFW32FALSE_EXIT(this->EndInstall(Data));
  1321. Data->GenEnding.Success = TRUE;
  1322. break;
  1323. case ACTCTXCTB_CBREASON_ACTCTXGENENDED:
  1324. if (m_SSGenContext != NULL)
  1325. ::SxsDestroyStringSectionGenerationContext(m_SSGenContext);
  1326. if (DllRedirectionContext != NULL)
  1327. FUSION_DELETE_SINGLETON(DllRedirectionContext);
  1328. m_SSGenContext = NULL;
  1329. break;
  1330. case ACTCTXCTB_CBREASON_ALLPARSINGDONE:
  1331. Data->AllParsingDone.Success = FALSE;
  1332. if (SSGenContext != NULL)
  1333. IFW32FALSE_EXIT(::SxsDoneModifyingStringSectionGenerationContext(SSGenContext));
  1334. Data->AllParsingDone.Success = TRUE;
  1335. break;
  1336. case ACTCTXCTB_CBREASON_GETSECTIONSIZE:
  1337. Data->GetSectionSize.Success = FALSE;
  1338. INTERNAL_ERROR_CHECK(SSGenContext);
  1339. IFW32FALSE_EXIT(::SxsGetStringSectionGenerationContextSectionSize(SSGenContext, &Data->GetSectionSize.SectionSize));
  1340. Data->GetSectionSize.Success = TRUE;
  1341. break;
  1342. case ACTCTXCTB_CBREASON_ELEMENTPARSED:
  1343. {
  1344. Data->ElementParsed.Success = FALSE;
  1345. ULONG MappedValue = 0;
  1346. bool fFound = false;
  1347. enum MappedValues
  1348. {
  1349. eAssembly,
  1350. eAssemblyFile,
  1351. };
  1352. static const ELEMENT_PATH_MAP_ENTRY s_rgEntries[] =
  1353. {
  1354. { 1, L"urn:schemas-microsoft-com:asm.v1^assembly", NUMBER_OF(L"urn:schemas-microsoft-com:asm.v1^assembly") - 1, eAssembly },
  1355. { 2, L"urn:schemas-microsoft-com:asm.v1^assembly!urn:schemas-microsoft-com:asm.v1^file", NUMBER_OF(L"urn:schemas-microsoft-com:asm.v1^assembly!urn:schemas-microsoft-com:asm.v1^file") - 1, eAssemblyFile },
  1356. };
  1357. IFW32FALSE_EXIT(
  1358. ::SxspProcessElementPathMap(
  1359. Data->ElementParsed.ParseContext,
  1360. s_rgEntries,
  1361. NUMBER_OF(s_rgEntries),
  1362. MappedValue,
  1363. fFound));
  1364. if (fFound)
  1365. {
  1366. switch (MappedValue)
  1367. {
  1368. default:
  1369. INTERNAL_ERROR_CHECK2(
  1370. FALSE,
  1371. "Invalid mapped value returned from SxspProcessElementPathMap()");
  1372. case eAssembly:
  1373. break;
  1374. case eAssemblyFile:
  1375. {
  1376. bool fFound;
  1377. CSmallStringBuffer FileNameBuffer;
  1378. CSmallStringBuffer LoadFromBuffer;
  1379. CSmallStringBuffer HashValueBuffer;
  1380. SIZE_T cb;
  1381. // We look for required attributes etc first so that if we're only parsing, it's
  1382. // common code.
  1383. IFW32FALSE_EXIT(
  1384. ::SxspGetAttributeValue(
  1385. SXSP_GET_ATTRIBUTE_VALUE_FLAG_REQUIRED_ATTRIBUTE,
  1386. &s_AttributeName_name,
  1387. &Data->ElementParsed,
  1388. fFound,
  1389. sizeof(FileNameBuffer),
  1390. &FileNameBuffer,
  1391. cb,
  1392. NULL,
  1393. 0));
  1394. INTERNAL_ERROR_CHECK(fFound);
  1395. //
  1396. // Ensure that the hash string is valid
  1397. //
  1398. IFW32FALSE_EXIT(
  1399. ::SxspGetAttributeValue(
  1400. 0,
  1401. &s_AttributeName_hash,
  1402. &Data->ElementParsed,
  1403. fFound,
  1404. sizeof(HashValueBuffer),
  1405. &HashValueBuffer,
  1406. cb,
  1407. NULL,
  1408. 0));
  1409. //
  1410. // Odd numbers of characters in the hash string will be bad later.
  1411. //
  1412. if (fFound && (HashValueBuffer.Cch() % 2))
  1413. {
  1414. (*Data->ElementParsed.ParseContext->ErrorCallbacks.InvalidAttributeValue)(
  1415. Data->ElementParsed.ParseContext,
  1416. &s_AttributeName_hash);
  1417. ::FusionpSetLastWin32Error(ERROR_SXS_MANIFEST_PARSE_ERROR);
  1418. goto Exit;
  1419. }
  1420. //
  1421. // And that the hash-alg string is valid too
  1422. //
  1423. IFW32FALSE_EXIT(
  1424. ::SxspGetAttributeValue(
  1425. 0,
  1426. &s_AttributeName_hashalg,
  1427. &Data->ElementParsed,
  1428. fFound,
  1429. sizeof(HashValueBuffer),
  1430. &HashValueBuffer,
  1431. cb,
  1432. NULL,
  1433. 0));
  1434. if (fFound)
  1435. {
  1436. ALG_ID aid;
  1437. if (!::SxspHashAlgFromString(HashValueBuffer, aid))
  1438. {
  1439. (*Data->ElementParsed.ParseContext->ErrorCallbacks.InvalidAttributeValue)(
  1440. Data->ElementParsed.ParseContext,
  1441. &s_AttributeName_hashalg);
  1442. ::FusionpSetLastWin32Error(ERROR_SXS_MANIFEST_PARSE_ERROR);
  1443. goto Exit;
  1444. }
  1445. }
  1446. IFW32FALSE_EXIT(
  1447. ::SxspGetAttributeValue(
  1448. 0,
  1449. &s_AttributeName_loadFrom,
  1450. &Data->ElementParsed,
  1451. fFound,
  1452. sizeof(LoadFromBuffer),
  1453. &LoadFromBuffer,
  1454. cb,
  1455. NULL,
  1456. 0));
  1457. if (fFound)
  1458. {
  1459. // We're not allowed to install assemblies that have a loadFrom= and the only
  1460. // manifests with them that we can activate are ones that don't live in the assembly store.
  1461. if ((Data->Header.ManifestOperation == MANIFEST_OPERATION_INSTALL) ||
  1462. ((Data->ElementParsed.AssemblyContext->Flags & ACTCTXCTB_ASSEMBLY_CONTEXT_IS_ROOT_ASSEMBLY) == 0))
  1463. {
  1464. // You can't install an assembly with a loadfrom=foo file; it's only provided for
  1465. // app compat...
  1466. (*Data->ElementParsed.ParseContext->ErrorCallbacks.AttributeNotAllowed)(
  1467. Data->ElementParsed.ParseContext,
  1468. &s_AttributeName_loadFrom);
  1469. ::FusionpSetLastWin32Error(ERROR_SXS_MANIFEST_PARSE_ERROR);
  1470. goto Exit;
  1471. }
  1472. }
  1473. //
  1474. // Always update the file count.
  1475. //
  1476. ASSERT(Data->Header.ActCtxGenContext != NULL);
  1477. if (Data->Header.ActCtxGenContext)
  1478. {
  1479. Data->Header.pOriginalActCtxGenCtx->m_ulFileCount++;
  1480. }
  1481. // If we're installing, call back to the copy function
  1482. if (Data->Header.ManifestOperation == MANIFEST_OPERATION_INSTALL)
  1483. IFW32FALSE_EXIT(this->InstallFile(Data, FileNameBuffer));
  1484. // If we are generating an activation context, add it to the context.
  1485. if (Data->Header.ManifestOperation == MANIFEST_OPERATION_GENERATE_ACTIVATION_CONTEXT)
  1486. {
  1487. IFALLOCFAILED_EXIT(Entry = new DLL_REDIRECTION_ENTRY);
  1488. IFW32FALSE_EXIT(Entry->FileNameBuffer.Win32Assign(FileNameBuffer, FileNameBuffer.Cch()));
  1489. if (LoadFromBuffer.Cch() != 0)
  1490. {
  1491. Entry->AssemblyPathBuffer.Win32Assign(LoadFromBuffer, LoadFromBuffer.Cch());
  1492. Entry->AssemblyPathIsLoadFrom = true;
  1493. // If the value does not end in a slash, we assume it directly refers to
  1494. // a file.
  1495. if (!LoadFromBuffer.HasTrailingPathSeparator())
  1496. Entry->PathIncludesBaseName = true;
  1497. }
  1498. if (!::SxsAddStringToStringSectionGenerationContext(
  1499. (PSTRING_SECTION_GENERATION_CONTEXT) m_SSGenContext,
  1500. Entry->FileNameBuffer,
  1501. Entry->FileNameBuffer.Cch(),
  1502. Entry,
  1503. Data->ElementParsed.AssemblyContext->AssemblyRosterIndex,
  1504. ERROR_SXS_DUPLICATE_DLL_NAME))
  1505. {
  1506. ::FusionpLogError(
  1507. MSG_SXS_DLLREDIR_CONTRIB_ADD_FILE_MAP_ENTRY,
  1508. CUnicodeString(Entry->FileNameBuffer, Entry->FileNameBuffer.Cch()),
  1509. CEventLogLastError());
  1510. goto Exit;
  1511. }
  1512. Entry = NULL;
  1513. }
  1514. }
  1515. break;
  1516. }
  1517. }
  1518. }
  1519. // Everything's groovy!
  1520. Data->ElementParsed.Success = TRUE;
  1521. break;
  1522. case ACTCTXCTB_CBREASON_GETSECTIONDATA:
  1523. Data->GetSectionData.Success = FALSE;
  1524. IFW32FALSE_EXIT(::SxsGetStringSectionGenerationContextSectionData(
  1525. m_SSGenContext,
  1526. Data->GetSectionData.SectionSize,
  1527. Data->GetSectionData.SectionDataStart,
  1528. NULL));
  1529. Data->GetSectionData.Success = TRUE;
  1530. break;
  1531. }
  1532. Exit:
  1533. FUSION_DELETE_SINGLETON(Entry);
  1534. }
  1535. BOOL
  1536. SxspDllRedirectionStringSectionGenerationCallback(
  1537. PVOID Context,
  1538. ULONG Reason,
  1539. PVOID CallbackData
  1540. )
  1541. {
  1542. BOOL fSuccess = FALSE;
  1543. switch (Reason)
  1544. {
  1545. default:
  1546. goto Exit;
  1547. case STRING_SECTION_GENERATION_CONTEXT_CALLBACK_REASON_GETUSERDATASIZE:
  1548. case STRING_SECTION_GENERATION_CONTEXT_CALLBACK_REASON_GETUSERDATA:
  1549. // will use the user data area later to store common paths
  1550. break;
  1551. case STRING_SECTION_GENERATION_CONTEXT_CALLBACK_REASON_ENTRYDELETED:
  1552. {
  1553. PSTRING_SECTION_GENERATION_CONTEXT_CBDATA_ENTRYDELETED CBData =
  1554. (PSTRING_SECTION_GENERATION_CONTEXT_CBDATA_ENTRYDELETED) CallbackData;
  1555. PDLL_REDIRECTION_ENTRY Entry = (PDLL_REDIRECTION_ENTRY) CBData->DataContext;
  1556. FUSION_DELETE_SINGLETON(Entry);
  1557. break;
  1558. }
  1559. case STRING_SECTION_GENERATION_CONTEXT_CALLBACK_REASON_GETDATASIZE:
  1560. {
  1561. PSTRING_SECTION_GENERATION_CONTEXT_CBDATA_GETDATASIZE CBData =
  1562. (PSTRING_SECTION_GENERATION_CONTEXT_CBDATA_GETDATASIZE) CallbackData;
  1563. PDLL_REDIRECTION_ENTRY Entry = (PDLL_REDIRECTION_ENTRY) CBData->DataContext;
  1564. CBData->DataSize = sizeof(ACTIVATION_CONTEXT_DATA_DLL_REDIRECTION);
  1565. if (Entry->AssemblyPathBuffer.Cch() != 0)
  1566. {
  1567. CBData->DataSize += sizeof(ACTIVATION_CONTEXT_DATA_DLL_REDIRECTION_PATH_SEGMENT);
  1568. CBData->DataSize += (Entry->AssemblyPathBuffer.Cch() * sizeof(WCHAR));
  1569. }
  1570. break;
  1571. }
  1572. case STRING_SECTION_GENERATION_CONTEXT_CALLBACK_REASON_GETDATA:
  1573. {
  1574. PSTRING_SECTION_GENERATION_CONTEXT_CBDATA_GETDATA CBData =
  1575. (PSTRING_SECTION_GENERATION_CONTEXT_CBDATA_GETDATA) CallbackData;
  1576. PDLL_REDIRECTION_ENTRY Entry = (PDLL_REDIRECTION_ENTRY) CBData->DataContext;
  1577. PACTIVATION_CONTEXT_DATA_DLL_REDIRECTION Info;
  1578. SIZE_T BytesLeft = CBData->BufferSize;
  1579. SIZE_T BytesWritten = 0;
  1580. PVOID Cursor;
  1581. Info = (PACTIVATION_CONTEXT_DATA_DLL_REDIRECTION) CBData->Buffer;
  1582. Cursor = (PVOID) (Info + 1);
  1583. if (BytesLeft < sizeof(ACTIVATION_CONTEXT_DATA_DLL_REDIRECTION))
  1584. {
  1585. ::FusionpSetLastWin32Error(ERROR_INSUFFICIENT_BUFFER);
  1586. goto Exit;
  1587. }
  1588. BytesWritten += sizeof(ACTIVATION_CONTEXT_DATA_DLL_REDIRECTION);
  1589. BytesLeft -= sizeof(ACTIVATION_CONTEXT_DATA_DLL_REDIRECTION);
  1590. Info->Size = sizeof(ACTIVATION_CONTEXT_DATA_DLL_REDIRECTION);
  1591. Info->Flags = 0;
  1592. Info->TotalPathLength = static_cast<ULONG>(Entry->AssemblyPathBuffer.Cch() * sizeof(WCHAR));
  1593. if (Entry->PathIncludesBaseName)
  1594. Info->Flags |= ACTIVATION_CONTEXT_DATA_DLL_REDIRECTION_PATH_INCLUDES_BASE_NAME;
  1595. if (Entry->AssemblyPathBuffer.Cch() == 0)
  1596. {
  1597. // If there's no path, there's no segments!
  1598. Info->PathSegmentCount = 0;
  1599. Info->PathSegmentOffset = 0;
  1600. Info->Flags |= ACTIVATION_CONTEXT_DATA_DLL_REDIRECTION_PATH_OMITS_ASSEMBLY_ROOT;
  1601. }
  1602. else
  1603. {
  1604. PACTIVATION_CONTEXT_DATA_DLL_REDIRECTION_PATH_SEGMENT Segment;
  1605. Info->PathSegmentCount = 1;
  1606. Info->PathSegmentOffset = static_cast<LONG>(((LONG_PTR) Cursor) - ((LONG_PTR) CBData->SectionHeader));
  1607. // If this is a loadfrom="foo" file and the string contains a %, set the expand flag...
  1608. if ((Entry->AssemblyPathIsLoadFrom) && (Entry->AssemblyPathBuffer.ContainsCharacter(L'%')))
  1609. Info->Flags |= ACTIVATION_CONTEXT_DATA_DLL_REDIRECTION_PATH_EXPAND;
  1610. Segment = (PACTIVATION_CONTEXT_DATA_DLL_REDIRECTION_PATH_SEGMENT) Cursor;
  1611. Cursor = (PVOID) (Segment + 1);
  1612. if (BytesLeft < sizeof(ACTIVATION_CONTEXT_DATA_DLL_REDIRECTION_PATH_SEGMENT))
  1613. {
  1614. ::FusionpSetLastWin32Error(ERROR_INSUFFICIENT_BUFFER);
  1615. goto Exit;
  1616. }
  1617. BytesWritten += sizeof(ACTIVATION_CONTEXT_DATA_DLL_REDIRECTION_PATH_SEGMENT);
  1618. BytesLeft -= sizeof(ACTIVATION_CONTEXT_DATA_DLL_REDIRECTION_PATH_SEGMENT);
  1619. Segment->Length = Info->TotalPathLength;
  1620. Segment->Offset = static_cast<LONG>(((LONG_PTR) Cursor) - ((LONG_PTR) CBData->SectionHeader));
  1621. if (BytesLeft < (Entry->AssemblyPathBuffer.Cch() * sizeof(WCHAR)))
  1622. {
  1623. ::FusionpSetLastWin32Error(ERROR_INSUFFICIENT_BUFFER);
  1624. goto Exit;
  1625. }
  1626. BytesWritten += (Entry->AssemblyPathBuffer.Cch() * sizeof(WCHAR));
  1627. BytesLeft -= (Entry->AssemblyPathBuffer.Cch() * sizeof(WCHAR));
  1628. memcpy(Cursor, static_cast<PCWSTR>(Entry->AssemblyPathBuffer), Entry->AssemblyPathBuffer.Cch() * sizeof(WCHAR));
  1629. }
  1630. CBData->BytesWritten = BytesWritten;
  1631. }
  1632. }
  1633. fSuccess = TRUE;
  1634. Exit:
  1635. return fSuccess;
  1636. }