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.

3533 lines
96 KiB

  1. /*--
  2. Copyright (c) 1999 Microsoft Corporation
  3. Module Name:
  4. sdbinst.cpp
  5. Abstract:
  6. installs custom SDB files into AppPatch\Custom, and adds registry entries to point
  7. to them
  8. Author:
  9. dmunsil 12/29/2000
  10. Revision History:
  11. Many people contributed over time.
  12. (in alphabetical order: clupu, dmunsil, rparsons, vadimb)
  13. Notes:
  14. --*/
  15. #define _UNICODE
  16. #define WIN
  17. #define FLAT_32
  18. #define TRUE_IF_WIN32 1
  19. #include <nt.h>
  20. #include <ntrtl.h>
  21. #include <nturtl.h>
  22. #define _WINDOWS
  23. #include <windows.h>
  24. #include <shellapi.h>
  25. #include <stdio.h>
  26. #include <stddef.h>
  27. #include <stdlib.h>
  28. #include <assert.h>
  29. #include <tchar.h>
  30. #include <aclapi.h>
  31. #include "resource.h"
  32. extern "C" {
  33. #include "shimdb.h"
  34. }
  35. //
  36. // DJM - turning off strsafe deprecation for now, but all of these deprecated functions
  37. // should be replaced.
  38. //
  39. #include <strsafe.h>
  40. BOOL g_bQuiet;
  41. BOOL g_bWin2K;
  42. WCHAR g_wszCustom[MAX_PATH];
  43. BOOL g_bAllowPatches = FALSE;
  44. HINSTANCE g_hInst;
  45. HANDLE g_hLogFile = INVALID_HANDLE_VALUE;
  46. typedef enum _INSTALL_MODE {
  47. MODE_INSTALL,
  48. MODE_UNINSTALL,
  49. MODE_CLEANUP,
  50. MODE_CONVERT_FORMAT_NEW,
  51. MODE_CONVERT_FORMAT_OLD
  52. } INSTALL_MODE;
  53. #define UNINSTALL_KEY_PATH L"Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\"
  54. #define APPCOMPAT_KEY L"System\\CurrentControlSet\\Control\\Session Manager\\AppCompatibility"
  55. DWORD g_dwWow64Key = (DWORD)-1;
  56. UINT
  57. SdbGetWindowsDirectory(
  58. LPWSTR pwszBuffer,
  59. DWORD dwSize
  60. )
  61. {
  62. pwszBuffer[0] = 0;
  63. UINT unRet = GetSystemWindowsDirectoryW(pwszBuffer, dwSize);
  64. if (!unRet) {
  65. goto out;
  66. }
  67. if (unRet >= dwSize) {
  68. unRet++;
  69. goto out;
  70. }
  71. //
  72. // throw a backslash on there if it's not already there
  73. //
  74. if (pwszBuffer[unRet - 1] != L'\\') {
  75. pwszBuffer[unRet] = L'\\';
  76. unRet++;
  77. pwszBuffer[unRet] = 0;
  78. }
  79. out:
  80. return unRet;
  81. }
  82. void
  83. __cdecl
  84. vPrintError(
  85. UINT unRes,
  86. ...
  87. )
  88. {
  89. WCHAR szT[1024];
  90. WCHAR wszFormat[1024];
  91. WCHAR wszCaption[1024];
  92. va_list arglist;
  93. if (!g_bQuiet) {
  94. if (!LoadStringW(g_hInst, IDS_APP_ERROR_TITLE, wszCaption, ARRAYSIZE(wszCaption))) {
  95. return;
  96. }
  97. if (LoadStringW(g_hInst, unRes, wszFormat, ARRAYSIZE(wszFormat))) {
  98. va_start(arglist, unRes);
  99. StringCchVPrintfW(szT, ARRAYSIZE(szT), wszFormat, arglist);
  100. va_end(arglist);
  101. MessageBoxW(NULL, szT, wszCaption, MB_OK | MB_ICONWARNING);
  102. }
  103. }
  104. }
  105. void
  106. __cdecl
  107. vPrintMessage(
  108. UINT unRes,
  109. ...
  110. )
  111. {
  112. WCHAR szT[1024];
  113. WCHAR wszFormat[1024];
  114. WCHAR wszCaption[1024];
  115. va_list arglist;
  116. if (!g_bQuiet) {
  117. if (!LoadStringW(g_hInst, IDS_APP_TITLE, wszCaption, ARRAYSIZE(wszCaption))) {
  118. return;
  119. }
  120. if (LoadStringW(g_hInst, unRes, wszFormat, ARRAYSIZE(wszFormat))) {
  121. va_start(arglist, unRes);
  122. StringCchVPrintfW(szT, ARRAYSIZE(szT), wszFormat, arglist);
  123. va_end(arglist);
  124. MessageBoxW(NULL, szT, wszCaption, MB_OK | MB_ICONINFORMATION);
  125. }
  126. }
  127. }
  128. void
  129. __cdecl
  130. vLogMessage(
  131. LPCSTR pwszFormat,
  132. ...
  133. )
  134. {
  135. CHAR szT[1024];
  136. va_list arglist;
  137. int nLength;
  138. va_start(arglist, pwszFormat);
  139. StringCchVPrintfA(szT, CHARCOUNT(szT), pwszFormat, arglist);
  140. nLength = strlen(szT);
  141. va_end(arglist);
  142. if (g_hLogFile != INVALID_HANDLE_VALUE) {
  143. DWORD dwWritten;
  144. WriteFile(g_hLogFile, (LPVOID)szT, (DWORD)nLength, &dwWritten, NULL);
  145. }
  146. OutputDebugStringA(szT);
  147. }
  148. DWORD
  149. GetWow64Flag(
  150. void
  151. )
  152. {
  153. if (g_dwWow64Key == (DWORD)-1) {
  154. if (g_bWin2K) {
  155. g_dwWow64Key = 0; // no flag since there is no wow64 on win2k
  156. } else {
  157. g_dwWow64Key = KEY_WOW64_64KEY;
  158. }
  159. }
  160. return g_dwWow64Key;
  161. }
  162. VOID
  163. OpenLogFile(
  164. VOID
  165. )
  166. {
  167. WCHAR wszLogFile[MAX_PATH];
  168. SdbGetWindowsDirectory(wszLogFile, ARRAYSIZE(wszLogFile));
  169. StringCchCatW(wszLogFile, ARRAYSIZE(wszLogFile), L"AppPatch\\SdbInst.Log");
  170. g_hLogFile = CreateFileW(wszLogFile,
  171. GENERIC_WRITE,
  172. FILE_SHARE_READ,
  173. NULL,
  174. CREATE_ALWAYS,
  175. FILE_ATTRIBUTE_NORMAL,
  176. NULL);
  177. }
  178. VOID
  179. CloseLogFile(
  180. VOID
  181. )
  182. {
  183. if (g_hLogFile != INVALID_HANDLE_VALUE) {
  184. CloseHandle(g_hLogFile);
  185. }
  186. g_hLogFile = INVALID_HANDLE_VALUE;
  187. }
  188. void
  189. vPrintHelp(
  190. WCHAR* szAppName
  191. )
  192. {
  193. vPrintMessage(IDS_HELP_TEXT, szAppName);
  194. }
  195. typedef void (CALLBACK *pfn_ShimFlushCache)(HWND, HINSTANCE, LPSTR, int);
  196. void
  197. vFlushCache(
  198. void
  199. )
  200. {
  201. HMODULE hAppHelp;
  202. pfn_ShimFlushCache pShimFlushCache;
  203. hAppHelp = LoadLibraryW(L"apphelp.dll");
  204. if (hAppHelp) {
  205. pShimFlushCache = (pfn_ShimFlushCache)GetProcAddress(hAppHelp, "ShimFlushCache");
  206. if (pShimFlushCache) {
  207. pShimFlushCache(NULL, NULL, NULL, 0);
  208. }
  209. }
  210. }
  211. BOOL
  212. bSearchGroupForSID(
  213. DWORD dwGroup,
  214. BOOL* pfIsMember
  215. )
  216. {
  217. PSID pSID = NULL;
  218. SID_IDENTIFIER_AUTHORITY SIDAuth = SECURITY_NT_AUTHORITY;
  219. BOOL fRes = TRUE;
  220. if (!AllocateAndInitializeSid(&SIDAuth,
  221. 2,
  222. SECURITY_BUILTIN_DOMAIN_RID,
  223. dwGroup,
  224. 0,
  225. 0,
  226. 0,
  227. 0,
  228. 0,
  229. 0,
  230. &pSID)) {
  231. return FALSE;
  232. }
  233. if (!pSID) {
  234. return FALSE;
  235. }
  236. if (!CheckTokenMembership(NULL, pSID, pfIsMember)) {
  237. fRes = FALSE;
  238. }
  239. FreeSid(pSID);
  240. return fRes;
  241. }
  242. BOOL
  243. bCanRun(
  244. void
  245. )
  246. {
  247. BOOL fIsAdmin;
  248. if (!bSearchGroupForSID(DOMAIN_ALIAS_RID_ADMINS, &fIsAdmin))
  249. {
  250. return FALSE;
  251. }
  252. return fIsAdmin;
  253. }
  254. WCHAR*
  255. wszGetFileFromPath(
  256. WCHAR* wszPath
  257. )
  258. {
  259. WCHAR* szTemp = wcsrchr(wszPath, L'\\');
  260. if (szTemp) {
  261. return szTemp + 1;
  262. }
  263. return NULL;
  264. }
  265. BOOL
  266. bIsAlreadyInstalled(
  267. WCHAR* wszPath
  268. )
  269. {
  270. DWORD dwCustomLen;
  271. DWORD dwInputLen;
  272. DWORD dwPos;
  273. dwCustomLen = wcslen(g_wszCustom);
  274. dwInputLen = wcslen(wszPath);
  275. if (_wcsnicmp(wszPath, g_wszCustom, dwCustomLen) != 0) {
  276. //
  277. // it's not in the custom directory
  278. //
  279. return FALSE;
  280. }
  281. for (dwPos = dwCustomLen; dwPos < dwInputLen; ++dwPos) {
  282. if (wszPath[dwPos] == L'\\') {
  283. //
  284. // it's in a subdirectory of Custom,
  285. //
  286. return FALSE;
  287. }
  288. }
  289. return TRUE;
  290. }
  291. BOOL
  292. bGuidToPath(
  293. GUID* pGuid,
  294. WCHAR* wszPath,
  295. DWORD dwPathSize
  296. )
  297. {
  298. UNICODE_STRING ustrGuid;
  299. HRESULT hr1, hr2, hr3;
  300. if (!NT_SUCCESS(RtlStringFromGUID(*pGuid, &ustrGuid))) {
  301. return FALSE;
  302. }
  303. hr1 = StringCchCopyW(wszPath, dwPathSize, g_wszCustom);
  304. hr2 = StringCchCatW(wszPath, dwPathSize, ustrGuid.Buffer);
  305. hr3 = StringCchCatW(wszPath, dwPathSize, L".sdb");
  306. RtlFreeUnicodeString(&ustrGuid);
  307. if (FAILED(hr1) || FAILED(hr2) || FAILED(hr3)) {
  308. return FALSE;
  309. }
  310. return TRUE;
  311. }
  312. BOOL
  313. bGetGuid(
  314. WCHAR* wszSDB,
  315. GUID* pGuid
  316. )
  317. {
  318. PDB pdb = NULL;
  319. TAGID tiDatabase;
  320. TAGID tiID;
  321. BOOL bRet = FALSE;
  322. pdb = SdbOpenDatabase(wszSDB, DOS_PATH);
  323. if (!pdb) {
  324. vPrintError(IDS_UNABLE_TO_OPEN_FILE, wszSDB);
  325. bRet = FALSE;
  326. goto out;
  327. }
  328. tiDatabase = SdbFindFirstTag(pdb, TAGID_ROOT, TAG_DATABASE);
  329. if (!tiDatabase) {
  330. vPrintError(IDS_NO_DB_TAG, wszSDB);
  331. bRet = FALSE;
  332. goto out;
  333. }
  334. ZeroMemory(pGuid, sizeof(GUID));
  335. tiID = SdbFindFirstTag(pdb, tiDatabase, TAG_DATABASE_ID);
  336. if (tiID) {
  337. if (SdbReadBinaryTag(pdb, tiID, (PBYTE)pGuid, sizeof(GUID))) {
  338. bRet = TRUE;
  339. }
  340. }
  341. if (!bRet) {
  342. vPrintError(IDS_NO_DB_ID, wszSDB);
  343. }
  344. out:
  345. if (pdb) {
  346. SdbCloseDatabase(pdb);
  347. pdb = NULL;
  348. }
  349. return bRet;
  350. }
  351. typedef enum _TIME_COMPARE {
  352. FILE_NEWER,
  353. FILE_SAME,
  354. FILE_OLDER
  355. } TIME_COMPARE;
  356. BOOL
  357. bOldSdbInstalled(
  358. WCHAR* wszPath,
  359. WCHAR* wszOldPath,
  360. DWORD dwOldPathSize
  361. )
  362. {
  363. WIN32_FIND_DATAW FindData;
  364. GUID guidMain;
  365. BOOL bRet = FALSE;
  366. HANDLE hFind;
  367. //
  368. // get the guid from the DB we're installing
  369. //
  370. if (!bGetGuid(wszPath, &guidMain)) {
  371. //
  372. // there's no info in this DB, so no way to tell.
  373. //
  374. return FALSE;
  375. }
  376. //
  377. // get the path to the current file
  378. //
  379. if (!bGuidToPath(&guidMain, wszOldPath, dwOldPathSize)) {
  380. //
  381. // couldn't convert to path
  382. //
  383. return FALSE;
  384. }
  385. //
  386. // check to see if the file exists
  387. //
  388. hFind = FindFirstFileW(wszOldPath, &FindData);
  389. if (hFind != INVALID_HANDLE_VALUE) {
  390. //
  391. // yup
  392. //
  393. bRet = TRUE;
  394. FindClose(hFind);
  395. }
  396. return bRet;
  397. }
  398. BOOL
  399. IsKnownDatabaseGUID(
  400. GUID* pGuid
  401. )
  402. {
  403. const GUID* rgpGUID[] = {
  404. &GUID_SYSMAIN_SDB,
  405. &GUID_APPHELP_SDB,
  406. &GUID_SYSTEST_SDB,
  407. &GUID_DRVMAIN_SDB,
  408. &GUID_MSIMAIN_SDB
  409. };
  410. int i;
  411. for (i = 0; i < ARRAYSIZE(rgpGUID); ++i) {
  412. if (*rgpGUID[i] == *pGuid) {
  413. return TRUE;
  414. }
  415. }
  416. return FALSE;
  417. }
  418. BOOL
  419. DatabaseContainsPatch(
  420. WCHAR* wszSDB
  421. )
  422. {
  423. PDB pdb = NULL;
  424. TAGID tiDatabase = TAGID_NULL;
  425. TAGID tiLibrary = TAGID_NULL;
  426. TAGID tiPatch = TAGID_NULL;
  427. BOOL bRet = FALSE;
  428. pdb = SdbOpenDatabase(wszSDB, DOS_PATH);
  429. if (!pdb) {
  430. vPrintError(IDS_UNABLE_TO_OPEN_FILE, wszSDB);
  431. bRet = FALSE;
  432. goto out;
  433. }
  434. tiDatabase = SdbFindFirstTag(pdb, TAGID_ROOT, TAG_DATABASE);
  435. if (!tiDatabase) {
  436. vPrintError(IDS_NO_DB_TAG, wszSDB);
  437. bRet = FALSE;
  438. goto out;
  439. }
  440. tiLibrary = SdbFindFirstTag(pdb, tiDatabase, TAG_LIBRARY);
  441. if (!tiLibrary) {
  442. //
  443. // this isn't an error -- no library just means no patches
  444. //
  445. bRet = FALSE;
  446. goto out;
  447. }
  448. tiPatch = SdbFindFirstTag(pdb, tiLibrary, TAG_PATCH);
  449. if (tiPatch) {
  450. bRet = TRUE;
  451. } else {
  452. bRet = FALSE;
  453. }
  454. out:
  455. if (pdb) {
  456. SdbCloseDatabase(pdb);
  457. pdb = NULL;
  458. }
  459. return bRet;
  460. }
  461. BOOL
  462. bGetInternalNameAndID(
  463. WCHAR* wszSDB,
  464. WCHAR* wszInternalName,
  465. DWORD dwInternalNameSize,
  466. GUID* pGuid
  467. )
  468. {
  469. PDB pdb = NULL;
  470. TAGID tiDatabase;
  471. TAGID tiName;
  472. TAGID tiID;
  473. BOOL bRet = FALSE;
  474. WCHAR* wszTemp;
  475. pdb = SdbOpenDatabase(wszSDB, DOS_PATH);
  476. if (!pdb) {
  477. vPrintError(IDS_UNABLE_TO_OPEN_FILE, wszSDB);
  478. bRet = FALSE;
  479. goto out;
  480. }
  481. tiDatabase = SdbFindFirstTag(pdb, TAGID_ROOT, TAG_DATABASE);
  482. if (!tiDatabase) {
  483. vPrintError(IDS_NO_DB_TAG, wszSDB);
  484. bRet = FALSE;
  485. goto out;
  486. }
  487. tiName = SdbFindFirstTag(pdb, tiDatabase, TAG_NAME);
  488. if (tiName) {
  489. wszTemp = SdbGetStringTagPtr(pdb, tiName);
  490. }
  491. if (wszTemp) {
  492. StringCchCopyW(wszInternalName, dwInternalNameSize, wszTemp);
  493. } else {
  494. wszInternalName[0] = 0;
  495. }
  496. ZeroMemory(pGuid, sizeof(GUID));
  497. tiID = SdbFindFirstTag(pdb, tiDatabase, TAG_DATABASE_ID);
  498. if (!tiID) {
  499. bRet = FALSE;
  500. goto out;
  501. }
  502. if (!SdbReadBinaryTag(pdb, tiID, (PBYTE)pGuid, sizeof(GUID))) {
  503. bRet = FALSE;
  504. goto out;
  505. }
  506. bRet = TRUE;
  507. out:
  508. if (pdb) {
  509. SdbCloseDatabase(pdb);
  510. pdb = NULL;
  511. }
  512. return bRet;
  513. }
  514. BOOL
  515. bFriendlyNameToFile(
  516. WCHAR* wszFriendlyName,
  517. WCHAR* wszFile,
  518. DWORD dwFileSize,
  519. WCHAR* wszPath,
  520. DWORD dwPathSize
  521. )
  522. {
  523. WCHAR wszSearchPath[MAX_PATH];
  524. WIN32_FIND_DATAW FindData;
  525. BOOL bRet = FALSE;
  526. WCHAR wszInternalTemp[256];
  527. WCHAR wszFileTemp[MAX_PATH];
  528. GUID guidTemp;
  529. HANDLE hFind;
  530. StringCchCopyW(wszSearchPath, ARRAYSIZE(wszSearchPath), g_wszCustom);
  531. StringCchCatW(wszSearchPath, ARRAYSIZE(wszSearchPath), L"*.sdb");
  532. hFind = FindFirstFileW(wszSearchPath, &FindData);
  533. if (hFind == INVALID_HANDLE_VALUE) {
  534. return FALSE;
  535. }
  536. while (hFind != INVALID_HANDLE_VALUE) {
  537. StringCchCopyW(wszFileTemp, ARRAYSIZE(wszFileTemp), g_wszCustom);
  538. StringCchCatW(wszFileTemp, ARRAYSIZE(wszFileTemp), FindData.cFileName);
  539. if (!bGetInternalNameAndID(wszFileTemp, wszInternalTemp, ARRAYSIZE(wszInternalTemp), &guidTemp)) {
  540. goto nextFile;
  541. }
  542. if (_wcsicmp(wszInternalTemp, wszFriendlyName) == 0) {
  543. bRet = TRUE;
  544. StringCchCopyW(wszFile, dwFileSize, FindData.cFileName);
  545. StringCchCopyW(wszPath, dwPathSize, wszFileTemp);
  546. FindClose(hFind);
  547. break;
  548. }
  549. nextFile:
  550. if (!FindNextFileW(hFind, &FindData)) {
  551. FindClose(hFind);
  552. hFind = INVALID_HANDLE_VALUE;
  553. }
  554. }
  555. return bRet;
  556. }
  557. BOOL
  558. bFindInstallName(
  559. WCHAR* wszPath,
  560. WCHAR* wszInstallPath,
  561. DWORD dwInstallPathSize
  562. )
  563. {
  564. GUID guidMain;
  565. //
  566. // get the guid from the DB we're installing
  567. //
  568. if (!bGetGuid(wszPath, &guidMain)) {
  569. //
  570. // there's no info in this DB, so no way to tell.
  571. //
  572. return FALSE;
  573. }
  574. //
  575. // get the path to the current file
  576. //
  577. if (!bGuidToPath(&guidMain, wszInstallPath, dwInstallPathSize)) {
  578. //
  579. // couldn't convert to path
  580. //
  581. return FALSE;
  582. }
  583. return TRUE;
  584. }
  585. //
  586. // this function is necessary because RegDeleteKey doesn't work right with
  587. // a 32-bit app deleting 64-bit reg keys
  588. //
  589. LONG
  590. LocalRegDeleteKeyW (
  591. IN HKEY hKey,
  592. IN LPCWSTR lpSubKey
  593. )
  594. {
  595. LONG lRes;
  596. HKEY hSubKey = NULL;
  597. lRes = RegOpenKeyExW(hKey,
  598. lpSubKey,
  599. 0,
  600. KEY_ALL_ACCESS|GetWow64Flag(),
  601. &hSubKey);
  602. if (lRes != ERROR_SUCCESS) {
  603. return lRes;
  604. }
  605. lRes = NtDeleteKey(hSubKey);
  606. RegCloseKey(hSubKey);
  607. return lRes;
  608. }
  609. VOID
  610. InstallW2KData(
  611. WCHAR* pszEntryName,
  612. LPCWSTR pszGuidDB
  613. )
  614. {
  615. HKEY hKey;
  616. WCHAR wszRegPath[MAX_PATH * 2];
  617. DWORD dwDisposition, cbData;
  618. LONG lResult = 0;
  619. BYTE data[16] = {0x0c, 0, 0, 0, 0, 0, 0, 0,
  620. 0x06, 0, 0, 0, 0, 0, 0, 0};
  621. HRESULT hr;
  622. //
  623. // This is Windows 2000 - attempt to add custom SDB specific data.
  624. //
  625. hr = StringCchPrintfW(wszRegPath, ARRAYSIZE(wszRegPath), L"%s\\%s", APPCOMPAT_KEY, pszEntryName);
  626. if (FAILED(hr)) {
  627. vPrintError(IDS_BUFFER_TOO_SMALL);
  628. return;
  629. }
  630. lResult = RegCreateKeyExW(HKEY_LOCAL_MACHINE,
  631. wszRegPath,
  632. 0,
  633. NULL,
  634. 0,
  635. KEY_SET_VALUE,
  636. NULL,
  637. &hKey,
  638. &dwDisposition);
  639. if (ERROR_SUCCESS != lResult) {
  640. if (ERROR_ACCESS_DENIED == lResult) {
  641. vPrintError(IDS_NEED_INSTALL_PERMISSION);
  642. return;
  643. } else {
  644. vPrintError(IDS_CANT_CREATE_REG_KEY, pszEntryName);
  645. return;
  646. }
  647. }
  648. //
  649. // Set the registry values.
  650. //
  651. lResult = RegSetValueExW(hKey,
  652. pszGuidDB,
  653. 0,
  654. REG_BINARY,
  655. data,
  656. sizeof(data));
  657. if (ERROR_SUCCESS != lResult) {
  658. RegCloseKey(hKey);
  659. if (ERROR_ACCESS_DENIED == lResult) {
  660. vPrintError(IDS_NEED_INSTALL_PERMISSION);
  661. } else {
  662. vPrintError(IDS_CANT_SET_REG_VALUE, pszEntryName);
  663. }
  664. return;
  665. }
  666. data[0] = 0;
  667. hr = StringCchPrintfW(wszRegPath, ARRAYSIZE(wszRegPath), L"DllPatch-%s", pszGuidDB);
  668. if (FAILED(hr)) {
  669. vPrintError(IDS_BUFFER_TOO_SMALL);
  670. return;
  671. }
  672. lResult = RegSetValueExW(hKey,
  673. wszRegPath,
  674. 0,
  675. REG_SZ,
  676. data,
  677. 2 * sizeof(WCHAR));
  678. if (ERROR_SUCCESS != lResult) {
  679. RegCloseKey(hKey);
  680. if (ERROR_ACCESS_DENIED == lResult) {
  681. vPrintError(IDS_NEED_INSTALL_PERMISSION);
  682. } else {
  683. vPrintError(IDS_CANT_SET_REG_VALUE, pszEntryName);
  684. }
  685. return;
  686. }
  687. RegCloseKey(hKey);
  688. }
  689. VOID
  690. RemoveW2KData(
  691. WCHAR* pszEntryName,
  692. LPCWSTR pszGuidDB
  693. )
  694. {
  695. HKEY hKey;
  696. WCHAR wszRegPath[MAX_PATH];
  697. LONG lResult = 0;
  698. DWORD dwValues;
  699. HRESULT hr;
  700. //
  701. // This is Windows 2000 - attempt to remove custom SDB specific data.
  702. //
  703. hr = StringCchPrintfW(wszRegPath, ARRAYSIZE(wszRegPath), L"%s\\%s", APPCOMPAT_KEY, pszEntryName);
  704. if (FAILED(hr)) {
  705. vPrintError(IDS_BUFFER_TOO_SMALL);
  706. return;
  707. }
  708. lResult = RegOpenKeyExW(HKEY_LOCAL_MACHINE,
  709. wszRegPath,
  710. 0,
  711. KEY_ALL_ACCESS|GetWow64Flag(),
  712. &hKey);
  713. if (ERROR_SUCCESS != lResult) {
  714. if (ERROR_ACCESS_DENIED == lResult) {
  715. vPrintError(IDS_NEED_INSTALL_PERMISSION);
  716. return;
  717. } else {
  718. vPrintError(IDS_CANT_OPEN_REG_KEY, wszRegPath);
  719. return;
  720. }
  721. }
  722. RegDeleteValueW(hKey, pszGuidDB);
  723. hr = StringCchPrintfW(wszRegPath, ARRAYSIZE(wszRegPath), L"DllPatch-%s", pszGuidDB);
  724. if (FAILED(hr)) {
  725. vPrintError(IDS_BUFFER_TOO_SMALL);
  726. return;
  727. }
  728. RegDeleteValueW(hKey, wszRegPath);
  729. //
  730. // Figure out if we should delete the key, if there aren't any more values left
  731. //
  732. lResult = RegQueryInfoKey(hKey,
  733. NULL,
  734. NULL,
  735. NULL,
  736. NULL,
  737. NULL,
  738. NULL,
  739. &dwValues,
  740. NULL,
  741. NULL,
  742. NULL,
  743. NULL);
  744. RegCloseKey(hKey);
  745. hKey = NULL;
  746. if (dwValues != 0) {
  747. return;
  748. }
  749. lResult = RegOpenKeyExW(HKEY_LOCAL_MACHINE,
  750. APPCOMPAT_KEY,
  751. 0,
  752. KEY_ALL_ACCESS|GetWow64Flag(),
  753. &hKey);
  754. if (ERROR_SUCCESS == lResult) {
  755. lResult = LocalRegDeleteKeyW(hKey, pszEntryName);
  756. }
  757. if (lResult != ERROR_SUCCESS) {
  758. vPrintError(IDS_CANT_DELETE_REG_KEY, pszEntryName, APPCOMPAT_KEY);
  759. }
  760. RegCloseKey(hKey);
  761. }
  762. // Caller is responsible for freeing the memory using delete [].
  763. LPWSTR
  764. ExpandItem(
  765. LPCWSTR pwszItem
  766. )
  767. {
  768. LPWSTR pwszItemExpand = NULL;
  769. // Get the required length.
  770. DWORD dwLenExpand = ExpandEnvironmentStringsW(pwszItem, NULL, 0);
  771. if (!dwLenExpand)
  772. {
  773. goto err;
  774. }
  775. //
  776. // Make room for "\\?\"
  777. //
  778. dwLenExpand += 4;
  779. pwszItemExpand = new WCHAR [dwLenExpand];
  780. if (!pwszItemExpand)
  781. {
  782. goto err;
  783. }
  784. LPWSTR pwszTemp = pwszItemExpand;
  785. DWORD dwTemp = dwLenExpand;
  786. StringCchCopyW(pwszItemExpand, dwLenExpand, L"\\\\?\\");
  787. pwszTemp += 4;
  788. dwTemp -= 4;
  789. if (!ExpandEnvironmentStringsW(pwszItem, pwszTemp, dwTemp))
  790. {
  791. goto err;
  792. }
  793. return pwszItemExpand;
  794. err:
  795. if (pwszItemExpand) {
  796. delete [] pwszItemExpand;
  797. }
  798. return NULL;
  799. }
  800. DWORD
  801. GiveUsersWriteAccess(
  802. LPWSTR pwszDir
  803. )
  804. {
  805. DWORD dwRes;
  806. EXPLICIT_ACCESS ea;
  807. PACL pOldDACL;
  808. PACL pNewDACL = NULL;
  809. PSECURITY_DESCRIPTOR pSD = NULL;
  810. SID_IDENTIFIER_AUTHORITY SIDAuth = SECURITY_NT_AUTHORITY;
  811. PSID pUsersSID = NULL;
  812. dwRes = GetNamedSecurityInfoW(pwszDir,
  813. SE_FILE_OBJECT,
  814. DACL_SECURITY_INFORMATION,
  815. NULL,
  816. NULL,
  817. &pOldDACL,
  818. NULL,
  819. &pSD);
  820. if (ERROR_SUCCESS != dwRes) {
  821. goto Cleanup;
  822. }
  823. if (!AllocateAndInitializeSid(&SIDAuth,
  824. 2,
  825. SECURITY_BUILTIN_DOMAIN_RID,
  826. DOMAIN_ALIAS_RID_USERS,
  827. 0,
  828. 0,
  829. 0,
  830. 0,
  831. 0,
  832. 0,
  833. &pUsersSID) ) {
  834. dwRes = ERROR_NOT_ENOUGH_MEMORY;
  835. goto Cleanup;
  836. }
  837. //
  838. // Initialize an EXPLICIT_ACCESS structure for the new ACE.
  839. //
  840. ZeroMemory(&ea, sizeof(EXPLICIT_ACCESS));
  841. ea.grfAccessPermissions = FILE_GENERIC_WRITE | FILE_GENERIC_READ | DELETE;
  842. ea.grfAccessMode = GRANT_ACCESS;
  843. ea.grfInheritance = SUB_CONTAINERS_AND_OBJECTS_INHERIT;
  844. ea.Trustee.TrusteeForm = TRUSTEE_IS_SID;
  845. ea.Trustee.TrusteeType = TRUSTEE_IS_GROUP;
  846. ea.Trustee.ptstrName = (LPTSTR)pUsersSID;
  847. //
  848. // Create a new ACL that merges the new ACE
  849. // into the existing DACL.
  850. //
  851. dwRes = SetEntriesInAcl(1, &ea, pOldDACL, &pNewDACL);
  852. if (ERROR_SUCCESS != dwRes) {
  853. goto Cleanup;
  854. }
  855. dwRes = SetNamedSecurityInfoW(pwszDir,
  856. SE_FILE_OBJECT,
  857. DACL_SECURITY_INFORMATION,
  858. NULL,
  859. NULL,
  860. pNewDACL,
  861. NULL);
  862. if (ERROR_SUCCESS != dwRes) {
  863. goto Cleanup;
  864. }
  865. Cleanup:
  866. if (pSD) {
  867. LocalFree(pSD);
  868. }
  869. if (pUsersSID) {
  870. FreeSid(pUsersSID);
  871. }
  872. if (pNewDACL) {
  873. LocalFree(pNewDACL);
  874. }
  875. return dwRes;
  876. }
  877. BOOL
  878. SetupLUAAllUserDir(
  879. LPCWSTR pwszAllUserDir
  880. )
  881. {
  882. BOOL bRes = FALSE;
  883. LPWSTR pwszExpandedDir = ExpandItem(pwszAllUserDir);
  884. if (!pwszExpandedDir) {
  885. vPrintError(IDS_CANT_EXPAND_DIR, pwszAllUserDir);
  886. return FALSE;
  887. }
  888. //
  889. // Create the directory if it doesn't already exist.
  890. //
  891. DWORD dwAttributes = GetFileAttributesW(pwszExpandedDir);
  892. if (dwAttributes != -1) {
  893. if (!(dwAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
  894. vPrintError(IDS_OBJECT_ALREADY_EXISTS, pwszExpandedDir);
  895. goto Cleanup;
  896. }
  897. } else {
  898. if (!CreateDirectoryW(pwszExpandedDir, NULL)) {
  899. vPrintError(IDS_CANT_CREATE_DIRECTORY, pwszExpandedDir, GetLastError());
  900. goto Cleanup;
  901. }
  902. }
  903. //
  904. // Give the Users group full control access (power users can already modify
  905. // files in this directory).
  906. //
  907. if (GiveUsersWriteAccess((LPWSTR)pwszExpandedDir) != ERROR_SUCCESS) {
  908. vPrintError(IDS_CANT_SET_ACLS, pwszExpandedDir);
  909. goto Cleanup;
  910. }
  911. bRes = TRUE;
  912. Cleanup:
  913. delete [] pwszExpandedDir;
  914. return bRes;
  915. }
  916. BOOL
  917. ProcessLUAAction(
  918. PDB pdb,
  919. TAGID tiExe
  920. )
  921. {
  922. LPWSTR szAllUserDir = NULL;
  923. //
  924. // See if this EXE has an ACTION node. Currently only EXEs shimmed with the LUA
  925. // shims have ACTION nodes.
  926. //
  927. TAGID tiAction = SdbFindFirstTag(pdb, tiExe, TAG_ACTION);
  928. if (tiAction) {
  929. //
  930. // the ACTION node in the EXE shimmed with LUA looks like this:
  931. //
  932. // <ACTION NAME="REDIRECT" TYPE="ChangeACLs">
  933. // <DATA NAME="AllUserDir" VALUETYPE="STRING"
  934. // VALUE="%ALLUSERSPROFILE%\Application Data\Fireworks 3"/>
  935. // </ACTION>
  936. //
  937. TAGID tiName, tiType, tiData, tiValue;
  938. LPWSTR szName, szType, szData;
  939. if ((tiName = SdbFindFirstTag(pdb, tiAction, TAG_NAME)) &&
  940. (szName = SdbGetStringTagPtr(pdb, tiName))) {
  941. if (!wcscmp(szName, L"REDIRECT")) {
  942. if ((tiType = SdbFindFirstTag(pdb, tiAction, TAG_ACTION_TYPE)) &&
  943. (szType = SdbGetStringTagPtr(pdb, tiType))) {
  944. if (!wcscmp(szType, L"ChangeACLs")) {
  945. if ((tiData = SdbFindFirstTag(pdb, tiAction, TAG_DATA)) &&
  946. (tiValue = SdbFindFirstTag(pdb, tiData, TAG_DATA_STRING)) &&
  947. (szAllUserDir = SdbGetStringTagPtr(pdb, tiValue))) {
  948. if (!SetupLUAAllUserDir(szAllUserDir)) {
  949. return FALSE;
  950. }
  951. }
  952. }
  953. }
  954. }
  955. }
  956. }
  957. return TRUE;
  958. }
  959. // buffer size is in characters (unicode)
  960. BOOL
  961. InstallSdbEntry(
  962. WCHAR* szEntryName, // entry name (foo.exe or layer name)
  963. LPCWSTR pszGuidDB, // guid database id in string format
  964. ULONGLONG ullSdbTimeStamp, // representation of a timestamp
  965. BOOL bLayer // true if layer name
  966. )
  967. {
  968. LONG lRes;
  969. WCHAR szRegPath[MAX_PATH * 2]; // MAX_PATH for the reg path, and MAX_PATH for the exe name
  970. WCHAR szDBName[MAX_PATH]; // this is used in older (win2k) versions
  971. HRESULT hr;
  972. BOOL bReturn = FALSE;
  973. HKEY hKey = NULL;
  974. StringCchCopyW(szDBName, ARRAYSIZE(szDBName), pszGuidDB);
  975. StringCchCatW(szDBName, ARRAYSIZE(szDBName), L".sdb");
  976. pszGuidDB = szDBName;
  977. //
  978. // If this is Win2K, add data to the AppCompatibility key.
  979. //
  980. if (g_bWin2K) {
  981. InstallW2KData(szEntryName, pszGuidDB);
  982. }
  983. // else we have a string
  984. hr = StringCchPrintfW(szRegPath,
  985. ARRAYSIZE(szRegPath),
  986. (bLayer ? L"%s\\Layers\\%s": L"%s\\%s"),
  987. APPCOMPAT_KEY_PATH_CUSTOM_W,
  988. szEntryName);
  989. if (FAILED(hr)) {
  990. // error
  991. vPrintError(IDS_BUFFER_TOO_SMALL);
  992. goto HandleError;
  993. }
  994. lRes = RegCreateKeyExW(HKEY_LOCAL_MACHINE,
  995. szRegPath,
  996. 0,
  997. NULL,
  998. REG_OPTION_NON_VOLATILE,
  999. KEY_ALL_ACCESS|GetWow64Flag(),
  1000. NULL,
  1001. &hKey,
  1002. NULL);
  1003. //
  1004. // on install, we want to quit if we hit an error.
  1005. // BUGBUG - should we undo whatever we've already completed?
  1006. //
  1007. if (lRes != ERROR_SUCCESS) {
  1008. vPrintError(IDS_CANT_CREATE_REG_KEY, szRegPath);
  1009. goto HandleError;
  1010. }
  1011. lRes = RegSetValueExW(hKey,
  1012. pszGuidDB,
  1013. 0,
  1014. REG_QWORD,
  1015. (PBYTE)&ullSdbTimeStamp,
  1016. sizeof(ullSdbTimeStamp));
  1017. if (lRes != ERROR_SUCCESS) {
  1018. vPrintError(IDS_CANT_CREATE_VALUE, szRegPath);
  1019. goto HandleError;
  1020. }
  1021. bReturn = TRUE;
  1022. HandleError:
  1023. if (hKey != NULL) {
  1024. RegCloseKey(hKey);
  1025. }
  1026. return bReturn;
  1027. }
  1028. BOOL
  1029. UninstallSdbEntry(
  1030. WCHAR* szEntryName, // foo.exe or layer name
  1031. LPCWSTR pszGuidDB, // guid (database id) in string format
  1032. BOOL bLayer // true is layer
  1033. )
  1034. {
  1035. LONG lRes;
  1036. WCHAR szRegPath[MAX_PATH * 2]; // MAX_PATH for the reg path, and MAX_PATH for the exe name
  1037. WCHAR szDBName[MAX_PATH];
  1038. HRESULT hr;
  1039. BOOL bReturn = FALSE;
  1040. HKEY hKey = NULL;
  1041. DWORD dwValues;
  1042. WCHAR szOldInstallName[MAX_PATH];
  1043. StringCchCopyW(szDBName, ARRAYSIZE(szDBName), pszGuidDB);
  1044. StringCchCatW(szDBName, ARRAYSIZE(szDBName), L".sdb");
  1045. pszGuidDB = szDBName;
  1046. if (g_bWin2K) {
  1047. RemoveW2KData(szEntryName, pszGuidDB);
  1048. }
  1049. hr = StringCchPrintfW(szRegPath,
  1050. ARRAYSIZE(szRegPath),
  1051. (bLayer ? L"%s\\Layers\\%s": L"%s\\%s"),
  1052. APPCOMPAT_KEY_PATH_CUSTOM_W,
  1053. szEntryName);
  1054. if (FAILED(hr)) {
  1055. // error
  1056. vPrintError(IDS_BUFFER_TOO_SMALL);
  1057. goto Out;
  1058. }
  1059. lRes = RegOpenKeyExW(HKEY_LOCAL_MACHINE,
  1060. szRegPath,
  1061. 0,
  1062. KEY_ALL_ACCESS|GetWow64Flag(),
  1063. &hKey);
  1064. //
  1065. // if we fail to open a key on uninstall, keep going, so
  1066. // hopefully we can get as much uninstalled as possible.
  1067. //
  1068. if (lRes != ERROR_SUCCESS) {
  1069. if (lRes == ERROR_ACCESS_DENIED) {
  1070. vPrintError(IDS_NEED_UNINSTALL_PERMISSION);
  1071. goto HandleError;
  1072. } else {
  1073. //
  1074. // DO NOT report an error - this key might have been cleaned up during the
  1075. // previous path, such as when identical exe names appear in the same db
  1076. // for instance, two setup.exe's -- the first pass will clean up the key,
  1077. // second path will fail to open them right here
  1078. //
  1079. // vPrintError(IDS_CANT_OPEN_REG_KEY, szRegPath);
  1080. goto Out;
  1081. }
  1082. }
  1083. lRes = RegDeleteValueW(hKey, pszGuidDB);
  1084. if (lRes != ERROR_SUCCESS) {
  1085. if (lRes == ERROR_ACCESS_DENIED) {
  1086. vPrintError(IDS_NEED_UNINSTALL_PERMISSION);
  1087. goto HandleError; // fatal error
  1088. } else {
  1089. //
  1090. // bugbug - pszSdbInstallName
  1091. //
  1092. if (lRes == ERROR_FILE_NOT_FOUND) {
  1093. WCHAR wszOldFormat[MAX_PATH];
  1094. //
  1095. // aha, value's not there, try old format
  1096. //
  1097. StringCchCopyW(wszOldFormat, ARRAYSIZE(wszOldFormat), pszGuidDB);
  1098. StringCchCatW(wszOldFormat, ARRAYSIZE(wszOldFormat), L".sdb");
  1099. lRes = RegDeleteValueW(hKey, wszOldFormat);
  1100. }
  1101. if (lRes != ERROR_SUCCESS) {
  1102. vPrintError(IDS_CANT_DELETE_REG_VALUE, pszGuidDB, szRegPath);
  1103. }
  1104. }
  1105. }
  1106. //
  1107. // figure out if we should delete the key, if there aren't any more values left
  1108. //
  1109. lRes = RegQueryInfoKey(hKey,
  1110. NULL,
  1111. NULL,
  1112. NULL,
  1113. NULL,
  1114. NULL,
  1115. NULL,
  1116. &dwValues,
  1117. NULL,
  1118. NULL,
  1119. NULL,
  1120. NULL);
  1121. if (dwValues == 0) {
  1122. RegCloseKey(hKey);
  1123. hKey = NULL;
  1124. hr = StringCchPrintfW(szRegPath,
  1125. sizeof(szRegPath)/sizeof(szRegPath[0]),
  1126. (bLayer ? L"%s\\Layers": L"%s"),
  1127. APPCOMPAT_KEY_PATH_CUSTOM_W);
  1128. if (FAILED(hr)) {
  1129. // error
  1130. vPrintError(IDS_BUFFER_TOO_SMALL);
  1131. goto Out;
  1132. }
  1133. lRes = RegOpenKeyExW(HKEY_LOCAL_MACHINE, szRegPath, 0, KEY_WRITE|GetWow64Flag(), &hKey);
  1134. if (lRes != ERROR_SUCCESS) {
  1135. vPrintError(IDS_CANT_OPEN_REG_KEY, szRegPath);
  1136. goto Out;
  1137. }
  1138. lRes = LocalRegDeleteKeyW(hKey, szEntryName);
  1139. if (lRes != ERROR_SUCCESS) {
  1140. vPrintError(IDS_CANT_DELETE_REG_KEY, szEntryName, szRegPath);
  1141. }
  1142. }
  1143. Out:
  1144. bReturn = TRUE;
  1145. HandleError:
  1146. if (hKey != NULL) {
  1147. RegCloseKey(hKey);
  1148. }
  1149. return bReturn;
  1150. }
  1151. NTSTATUS
  1152. SDBAPI
  1153. FindCharInUnicodeString(
  1154. ULONG Flags,
  1155. PCUNICODE_STRING StringToSearch,
  1156. PCUNICODE_STRING CharSet,
  1157. USHORT* NonInclusivePrefixLength
  1158. )
  1159. {
  1160. LPCWSTR pch;
  1161. //
  1162. // implement only the case when we move backward
  1163. //
  1164. if (Flags != RTL_FIND_CHAR_IN_UNICODE_STRING_START_AT_END) {
  1165. return STATUS_NOT_IMPLEMENTED;
  1166. }
  1167. pch = StringToSearch->Buffer + StringToSearch->Length / sizeof(WCHAR);
  1168. while (pch >= StringToSearch->Buffer) {
  1169. if (_tcschr(CharSet->Buffer, *pch)) {
  1170. //
  1171. // got the char
  1172. //
  1173. if (NonInclusivePrefixLength) {
  1174. *NonInclusivePrefixLength = (USHORT)(pch - StringToSearch->Buffer) * sizeof(WCHAR);
  1175. }
  1176. return STATUS_SUCCESS;
  1177. }
  1178. pch--;
  1179. }
  1180. //
  1181. // We haven't found it. Return failure.
  1182. //
  1183. return STATUS_NOT_FOUND;
  1184. }
  1185. //
  1186. // Database list entry
  1187. // Used to represent a particular installed database
  1188. //
  1189. typedef struct tagSDBLISTENTRY {
  1190. LIST_ENTRY ListEntry; // link list stuff
  1191. ULONGLONG ullTimeStamp; // database install timestamp
  1192. GUID guidDB; // database guid
  1193. WCHAR szTimeStamp[32]; // time stamp in string form
  1194. WCHAR szGuidDB[64]; // guid in string form
  1195. WCHAR szDatabasePath[1]; // database path - we store only the name
  1196. } SDBLISTENTRY, *PSDBLISTENTRY;
  1197. /*++
  1198. AddSdbListEntry
  1199. Adds a particular database to the list of installed sdbs (maintained internally)
  1200. parses database path to retrieve database name
  1201. [in out] pHeadList - pointer to the associated list head for the installed sdbs
  1202. [in] guidDB - database guid
  1203. [in] TimeStamp - database time stamp
  1204. [in] pszDatabasePath - final database path
  1205. returns true if success
  1206. --*/
  1207. BOOL
  1208. AddSdbListEntry(
  1209. PLIST_ENTRY pHeadList,
  1210. GUID& guidDB,
  1211. ULONGLONG& TimeStamp,
  1212. LPCWSTR pszDatabasePath
  1213. )
  1214. {
  1215. //
  1216. // out of database path, recover the database name
  1217. //
  1218. UNICODE_STRING ustrPath = { 0 };
  1219. USHORT uPrefix;
  1220. UNICODE_STRING ustrPathSep = RTL_CONSTANT_STRING(L"\\/");
  1221. NTSTATUS Status;
  1222. UNICODE_STRING ustrGUID = { 0 };
  1223. if (pszDatabasePath != NULL) {
  1224. RtlInitUnicodeString(&ustrPath, pszDatabasePath);
  1225. Status = FindCharInUnicodeString(RTL_FIND_CHAR_IN_UNICODE_STRING_START_AT_END,
  1226. &ustrPath,
  1227. &ustrPathSep,
  1228. &uPrefix);
  1229. if (NT_SUCCESS(Status) && (uPrefix + sizeof(WCHAR)) < ustrPath.Length) {
  1230. //
  1231. // uPrefix is number of character preceding the one we found not including it
  1232. //
  1233. ustrPath.Buffer += uPrefix / sizeof(WCHAR) + 1;
  1234. ustrPath.Length -= (uPrefix + sizeof(WCHAR));
  1235. ustrPath.MaximumLength -= (uPrefix + sizeof(WCHAR));
  1236. }
  1237. //
  1238. // at this point ustrPath has just the filename -- this is what we shall use
  1239. //
  1240. }
  1241. PBYTE Buffer = new BYTE[sizeof(SDBLISTENTRY) + ustrPath.Length];
  1242. if (Buffer == NULL) {
  1243. vLogMessage("[AddSdbListEntry] Failed to allocate 0x%lx bytes\n",
  1244. sizeof(SDBLISTENTRY) + ustrPath.Length);
  1245. return FALSE;
  1246. }
  1247. PSDBLISTENTRY pSdbEntry = (PSDBLISTENTRY)Buffer;
  1248. pSdbEntry->guidDB = guidDB;
  1249. pSdbEntry->ullTimeStamp = TimeStamp;
  1250. Status = RtlStringFromGUID(guidDB, &ustrGUID);
  1251. if (!NT_SUCCESS(Status)) {
  1252. //
  1253. // we can't convert guid to string? memory allocation failure
  1254. //
  1255. vLogMessage("[AddSdbListEntry] Failed to convert guid to string Status 0x%lx\n",
  1256. Status);
  1257. delete[] Buffer;
  1258. return FALSE;
  1259. }
  1260. RtlCopyMemory(&pSdbEntry->szGuidDB[0], &ustrGUID.Buffer[0], ustrGUID.Length);
  1261. pSdbEntry->szGuidDB[ustrGUID.Length/sizeof(WCHAR)] = L'\0';
  1262. RtlFreeUnicodeString(&ustrGUID);
  1263. StringCchPrintfW(pSdbEntry->szTimeStamp, ARRAYSIZE(pSdbEntry->szTimeStamp), L"%.16I64X", TimeStamp);
  1264. RtlCopyMemory(&pSdbEntry->szDatabasePath[0], &ustrPath.Buffer[0], ustrPath.Length);
  1265. pSdbEntry->szDatabasePath[ustrPath.Length / sizeof(WCHAR)] = L'\0';
  1266. InsertHeadList(pHeadList, &pSdbEntry->ListEntry);
  1267. return TRUE;
  1268. }
  1269. //
  1270. // only pGuidDB OR pwszGuid is allowed
  1271. //
  1272. /*++
  1273. FindSdbListEntry
  1274. Finds and returns an sdb list entry given a guid (in string or binary form)
  1275. Whenever possible pwszGuid is used (if it's supplied). If pwszGuid happens to be
  1276. an arbitrary filename -- it is assumed that it's the name of an installed sdb file
  1277. as registered.
  1278. [in] pHeadList - list of the installed sdbs
  1279. [in] pwszGuid - guid or guid.sdb
  1280. [out] ppSdbListEntry - if found, this receives a pointer to sdb list entry
  1281. [in] pGuidDB - guid in binary form
  1282. returns true if matching database has been located in the list
  1283. --*/
  1284. BOOL
  1285. FindSdbListEntry(
  1286. PLIST_ENTRY pHeadList,
  1287. LPCWSTR pwszGuid, // guid, possibly with trailing '.sdb'
  1288. PSDBLISTENTRY* ppSdbListEntry,
  1289. GUID* pGuidDB // guid
  1290. )
  1291. {
  1292. UNICODE_STRING ustrDot = RTL_CONSTANT_STRING(L".");
  1293. UNICODE_STRING ustrPath;
  1294. USHORT uPrefix;
  1295. NTSTATUS Status;
  1296. PLIST_ENTRY pEntry;
  1297. PSDBLISTENTRY pSdbEntry;
  1298. GUID guidDB;
  1299. BOOL bGuidSearch = TRUE;
  1300. BOOL bFound = FALSE;
  1301. LPCWSTR pch;
  1302. if (pGuidDB == NULL) {
  1303. RtlInitUnicodeString(&ustrPath, pwszGuid);
  1304. Status = FindCharInUnicodeString(RTL_FIND_CHAR_IN_UNICODE_STRING_START_AT_END,
  1305. &ustrPath,
  1306. &ustrDot,
  1307. &uPrefix);
  1308. if (NT_SUCCESS(Status)) {
  1309. //
  1310. // uPrefix is number of character preceding the one we found not including it
  1311. //
  1312. ustrPath.Length = uPrefix;
  1313. }
  1314. //
  1315. // convert to guid, but check first
  1316. //
  1317. pch = pwszGuid + wcsspn(pwszGuid, L" \t");
  1318. if (*pch != L'{') { // not a guid, why convert ?
  1319. bGuidSearch = FALSE;
  1320. } else {
  1321. Status = RtlGUIDFromString(&ustrPath, &guidDB);
  1322. if (!NT_SUCCESS(Status)) {
  1323. //
  1324. // failed, so use database path instead
  1325. //
  1326. bGuidSearch = FALSE;
  1327. }
  1328. }
  1329. } else {
  1330. guidDB = *pGuidDB; // guid search only
  1331. }
  1332. pEntry = pHeadList->Flink;
  1333. while (pEntry != pHeadList && !bFound) {
  1334. //
  1335. // convert entry by subtracting the offset of the list entry
  1336. //
  1337. pSdbEntry = (PSDBLISTENTRY)((PBYTE)pEntry - OFFSETOF(SDBLISTENTRY, ListEntry));
  1338. //
  1339. // compare db guids or paths
  1340. //
  1341. if (bGuidSearch) {
  1342. bFound = RtlEqualMemory(&pSdbEntry->guidDB, &guidDB, sizeof(GUID));
  1343. } else {
  1344. bFound = !_wcsicmp(pSdbEntry->szDatabasePath, pwszGuid);
  1345. }
  1346. pEntry = pEntry->Flink;
  1347. }
  1348. //
  1349. // we have found an entry ? return it -- note that pEntry would have advanced while pSdbEntry
  1350. // still points to the entry we have found
  1351. //
  1352. if (bFound) {
  1353. *ppSdbListEntry = pSdbEntry;
  1354. }
  1355. return bFound;
  1356. }
  1357. /*++
  1358. CleanupSdbList
  1359. Performs cleanup for the installed sdb list
  1360. returns nothing
  1361. --*/
  1362. VOID
  1363. CleanupSdbList(
  1364. PLIST_ENTRY pSdbListHead
  1365. )
  1366. {
  1367. PLIST_ENTRY pEntry;
  1368. PSDBLISTENTRY pSdbEntry;
  1369. PBYTE Buffer;
  1370. pEntry = pSdbListHead->Flink;
  1371. if (pEntry == NULL) {
  1372. return;
  1373. }
  1374. while (pEntry != pSdbListHead) {
  1375. pSdbEntry = (PSDBLISTENTRY)((PBYTE)pEntry - OFFSETOF(SDBLISTENTRY, ListEntry));
  1376. pEntry = pEntry->Flink;
  1377. Buffer = (PBYTE)pSdbEntry;
  1378. delete[] Buffer;
  1379. }
  1380. }
  1381. /*++
  1382. ConvertInstalledSdbsToNewFormat
  1383. Converts installed sdbs to new format, which involves storing (or verifying) the
  1384. timestamp for each installed sdb file. This function also builds a list of sdbs
  1385. used elsewhere
  1386. [in] hKey - a key handle for hklm/..../InstalledSdb
  1387. [in out] pSdbListHead - list head for the installed sdbs
  1388. returns true if successful
  1389. --*/
  1390. BOOL
  1391. ConvertInstalledSdbsToNewFormat(
  1392. HKEY hKey, // hklm/.../InstalledSdb
  1393. PLIST_ENTRY pSdbListHead // we fill this list with our sdbs for later
  1394. )
  1395. {
  1396. DWORD dwIndex = 0;
  1397. WCHAR szSubKeyName[MAX_PATH];
  1398. PWCHAR pwszKeyName;
  1399. DWORD dwBufferSize;
  1400. FILETIME ftLastWriteTime;
  1401. HKEY hKeyEntry = NULL;
  1402. LONG lResult;
  1403. ULARGE_INTEGER liTimeStamp;
  1404. UNICODE_STRING ustrGuid;
  1405. GUID guidDB;
  1406. NTSTATUS Status;
  1407. WCHAR szDatabasePath[MAX_PATH];
  1408. PWCHAR pszDatabasePath;
  1409. DWORD dwType;
  1410. BOOL bSuccess = TRUE;
  1411. while (TRUE) {
  1412. dwBufferSize = sizeof(szSubKeyName)/sizeof(szSubKeyName[0]);
  1413. lResult = RegEnumKeyExW(hKey,
  1414. dwIndex,
  1415. szSubKeyName,
  1416. &dwBufferSize,
  1417. NULL, NULL, NULL,
  1418. &ftLastWriteTime);
  1419. ++dwIndex;
  1420. if (lResult != ERROR_SUCCESS) {
  1421. //
  1422. // done if no more keys, else some sort of error
  1423. // bugbug
  1424. //
  1425. if (lResult == ERROR_NO_MORE_ITEMS) {
  1426. //
  1427. // we are done, clean
  1428. //
  1429. break;
  1430. }
  1431. //
  1432. // this is unexpected
  1433. //
  1434. vLogMessage("[ConvertInstalledSdbsToNewFormat] RegEnumKeyExW for index 0x%lx returned unexpected error 0x%lx\n",
  1435. dwIndex, lResult);
  1436. break;
  1437. }
  1438. RtlInitUnicodeString(&ustrGuid, szSubKeyName);
  1439. Status = RtlGUIDFromString(&ustrGuid, &guidDB);
  1440. if (!NT_SUCCESS(Status)) {
  1441. //
  1442. // BUGBUG - failed to convert the guid (subkey name!)
  1443. // extraneous entry, log warning
  1444. //
  1445. vLogMessage("[ConvertInstalledSdbsToNewFormat] Failed to convert string to guid for \"%ls\" status 0x%lx\n",
  1446. szSubKeyName, Status);
  1447. continue;
  1448. }
  1449. //
  1450. // for this db entry we have to set the timestamp
  1451. //
  1452. lResult = RegOpenKeyExW(hKey,
  1453. szSubKeyName,
  1454. 0,
  1455. KEY_READ|KEY_WRITE|GetWow64Flag(),
  1456. &hKeyEntry);
  1457. if (lResult != ERROR_SUCCESS) {
  1458. //
  1459. // bad error ?
  1460. // BUGBUG
  1461. vLogMessage("[ConvertInstalledSdbsToNewFormat] Failed to open subkey \"%ls\" error 0x%lx\n",
  1462. szSubKeyName, lResult);
  1463. continue;
  1464. }
  1465. //
  1466. // now check the value
  1467. //
  1468. dwBufferSize = sizeof(liTimeStamp.QuadPart);
  1469. lResult = RegQueryValueExW(hKeyEntry,
  1470. L"DatabaseInstallTimeStamp",
  1471. NULL,
  1472. &dwType,
  1473. (PBYTE)&liTimeStamp.QuadPart,
  1474. &dwBufferSize);
  1475. if (lResult != ERROR_SUCCESS || dwType != REG_BINARY) {
  1476. //
  1477. // we may either have this value already -- if not, set it up now
  1478. //
  1479. liTimeStamp.LowPart = ftLastWriteTime.dwLowDateTime;
  1480. liTimeStamp.HighPart = ftLastWriteTime.dwHighDateTime;
  1481. vLogMessage("[Info] Database \"%ls\" receives timestamp \"%.16I64X\"\n",
  1482. szSubKeyName, liTimeStamp.QuadPart);
  1483. lResult = RegSetValueExW(hKeyEntry,
  1484. L"DatabaseInstallTimeStamp",
  1485. 0,
  1486. REG_BINARY,
  1487. (PBYTE)&liTimeStamp.QuadPart,
  1488. sizeof(liTimeStamp.QuadPart));
  1489. if (lResult != ERROR_SUCCESS) {
  1490. //
  1491. // error, ignore for now
  1492. //
  1493. vLogMessage("[ConvertInstalledSdbsToNewFormat] Failed to set timestamp value for database \"%ls\" value \"%.16I64X\" error 0x%lx\n",
  1494. szSubKeyName, liTimeStamp.QuadPart, lResult);
  1495. }
  1496. }
  1497. //
  1498. // at this point we have :
  1499. // sdb guid (in szSubKeyName)
  1500. // time stamp in liTimeStamp
  1501. //
  1502. //
  1503. // query also database path
  1504. //
  1505. pszDatabasePath = &szDatabasePath[0];
  1506. dwBufferSize = sizeof(szDatabasePath);
  1507. lResult = RegQueryValueExW(hKeyEntry,
  1508. L"DatabasePath",
  1509. NULL,
  1510. &dwType,
  1511. (PBYTE)pszDatabasePath,
  1512. &dwBufferSize);
  1513. if (lResult != ERROR_SUCCESS || dwType != REG_SZ) {
  1514. //
  1515. // no database path
  1516. // warn basically corrupt database path
  1517. //
  1518. vLogMessage("[ConvertInstalledSdbsToNewFormat] Failed to query database path for \"%s\" error 0x%lx\n", szSubKeyName, lResult);
  1519. pszDatabasePath = NULL;
  1520. }
  1521. //
  1522. // optional check: we can check here whether the sdb file does exist
  1523. //
  1524. //
  1525. // add this sdb to our cache
  1526. //
  1527. if (!AddSdbListEntry(pSdbListHead, guidDB, liTimeStamp.QuadPart, pszDatabasePath)) {
  1528. //
  1529. // failed to add list entry - we cannot continue
  1530. //
  1531. bSuccess = FALSE;
  1532. break;
  1533. }
  1534. RegCloseKey(hKeyEntry);
  1535. hKeyEntry = NULL;
  1536. }
  1537. if (hKeyEntry != NULL) {
  1538. RegCloseKey(hKeyEntry);
  1539. }
  1540. //
  1541. // we are done converting entries -- and we have also collected cache of sdb info
  1542. //
  1543. return bSuccess;
  1544. }
  1545. //
  1546. // this stucture is used to cache values associated with any particular entry (exe)
  1547. //
  1548. typedef struct tagSDBVALUEENTRY {
  1549. LIST_ENTRY ListEntry; // link
  1550. PSDBLISTENTRY pSdbEntry; // this entry belongs to this database
  1551. WCHAR szValueName[1]; // value name as we got it from registry
  1552. } SDBVALUEENTRY, *PSDBVALUEENTRY;
  1553. /*++
  1554. AddValueEntry
  1555. Adds an new link list element to the list of values
  1556. [in out] pValueListHead - link list of values
  1557. [in] pSdbEntry - pointer to a cached entry from sdb list
  1558. [in] pwszValueName - value name as we got it from the db (something like {guid} or {guid}.sdb)
  1559. returns true if successful
  1560. --*/
  1561. BOOL
  1562. AddValueEntry(
  1563. PLIST_ENTRY pValueListHead,
  1564. PSDBLISTENTRY pSdbEntry,
  1565. LPCWSTR pwszValueName
  1566. )
  1567. {
  1568. PSDBVALUEENTRY pValueEntry;
  1569. PBYTE Buffer;
  1570. DWORD dwSize;
  1571. dwSize = sizeof(SDBVALUEENTRY) + wcslen(pwszValueName) * sizeof(WCHAR);
  1572. Buffer = new BYTE[dwSize];
  1573. if (Buffer == NULL) {
  1574. //
  1575. // out of memory
  1576. //
  1577. vLogMessage("[AddValueEntry] Failed to allocate buffer for %ls 0x%lx bytes\n",
  1578. pwszValueName, dwSize);
  1579. return FALSE;
  1580. }
  1581. pValueEntry = (PSDBVALUEENTRY)Buffer;
  1582. pValueEntry->pSdbEntry = pSdbEntry;
  1583. StringCchCopyW(pValueEntry->szValueName, (dwSize - sizeof(SDBVALUEENTRY) + sizeof(WCHAR)), pwszValueName);
  1584. InsertHeadList(pValueListHead, &pValueEntry->ListEntry);
  1585. return TRUE;
  1586. }
  1587. /*++
  1588. WriteEntryValue
  1589. Writes value for a particular entry (exe or layer name), deletes old value associated with
  1590. this particular database for this exe (or layer)
  1591. [in] hKey - handle for an entry (for instance
  1592. hklm/Software/Microsoft/Windows NT/CurrentVersion/AppcompatFlags/Custom/Notepad.exe)
  1593. [in] pValueEntry - pointer to a value entry element from the value list
  1594. [in] bWriteNewFormat - whether we are asked to write new or old format
  1595. returns true if successful
  1596. --*/
  1597. BOOL
  1598. WriteEntryValue(
  1599. HKEY hKey,
  1600. PSDBVALUEENTRY pValueEntry,
  1601. BOOL bWriteNewFormat // if true -- write new format else old format
  1602. )
  1603. {
  1604. LONG lResult;
  1605. BOOL bSuccess = FALSE;
  1606. LPCWSTR pValueName;
  1607. if (bWriteNewFormat) {
  1608. pValueName = pValueEntry->pSdbEntry->szGuidDB;
  1609. lResult = RegSetValueExW(hKey,
  1610. pValueName,
  1611. 0,
  1612. REG_QWORD,
  1613. (PBYTE)&pValueEntry->pSdbEntry->ullTimeStamp,
  1614. sizeof(pValueEntry->pSdbEntry->ullTimeStamp));
  1615. if (lResult != ERROR_SUCCESS) {
  1616. //
  1617. // we can't do this entry ?
  1618. //
  1619. vLogMessage("[WriteEntryValue] Failed to write qword value \"%ls\"=\"%.16I64X\" error 0x%lx\n",
  1620. pValueEntry->pSdbEntry->szGuidDB, pValueEntry->pSdbEntry->ullTimeStamp, lResult);
  1621. goto cleanup;
  1622. }
  1623. //
  1624. // nuke old entry
  1625. //
  1626. } else {
  1627. //
  1628. // old style format please
  1629. //
  1630. pValueName = pValueEntry->pSdbEntry->szDatabasePath;
  1631. lResult = RegSetValueExW(hKey,
  1632. pValueName,
  1633. 0,
  1634. REG_SZ,
  1635. (PBYTE)L"",
  1636. sizeof(WCHAR));
  1637. if (lResult != ERROR_SUCCESS) {
  1638. //
  1639. // trouble -- error
  1640. //
  1641. vLogMessage("[WriteEntryValue] Failed to write string value \"%ls\" error 0x%lx\n",
  1642. pValueEntry->pSdbEntry->szDatabasePath, lResult);
  1643. goto cleanup;
  1644. }
  1645. }
  1646. //
  1647. // if we are here -- success, check to see if we can delete the old value
  1648. //
  1649. if (_wcsicmp(pValueEntry->szValueName, pValueName) != 0) {
  1650. lResult = RegDeleteValueW(hKey, pValueEntry->szValueName);
  1651. if (lResult != ERROR_SUCCESS) {
  1652. vLogMessage("[WriteEntryValue] Failed to delete value \"%ls\" error 0x%lx\n",
  1653. pValueEntry->szValueName, lResult);
  1654. }
  1655. }
  1656. bSuccess = TRUE;
  1657. cleanup:
  1658. return bSuccess;
  1659. }
  1660. /*++
  1661. ConvertEntryToNewFormat
  1662. Converts a particular entry (layer or exe)
  1663. [in] hKeyParent - key handle for a parent key (for instance
  1664. hklm/Software/Microsoft/Windows NT/CurrentVersion/AppcompatFlags/Custom when
  1665. pwszEntryName == "Notepad.exe" or
  1666. hklm/Software/Microsoft/Windows NT/CurrentVersion/AppcompatFlags/Custom/Layers when
  1667. pwszEntryName == "RunLayer"
  1668. [in] pwszEntryName - Either exe name or layer name
  1669. [in] pSdbListHead - cached list of installed databases
  1670. [in] bNewFormat - whether to use new or old format
  1671. returns true if successful
  1672. --*/
  1673. BOOL
  1674. ConvertEntryToNewFormat(
  1675. HKEY hKeyParent,
  1676. LPCWSTR pwszEntryName,
  1677. PLIST_ENTRY pSdbListHead,
  1678. BOOL bConvertToNewFormat // true if converting to new format, false if reverting
  1679. )
  1680. {
  1681. LONG lResult;
  1682. DWORD dwValues;
  1683. DWORD dwMaxValueNameLen;
  1684. DWORD dwMaxValueLen;
  1685. DWORD dwType;
  1686. DWORD dwValueNameSize;
  1687. DWORD dwValueSize;
  1688. LPWSTR pwszValueName = NULL;
  1689. LPBYTE pValue = NULL;
  1690. PSDBLISTENTRY pSdbEntry;
  1691. DWORD dwIndex;
  1692. LIST_ENTRY ValueList = { 0 };
  1693. PSDBVALUEENTRY pValueEntry;
  1694. PLIST_ENTRY pValueList;
  1695. PBYTE Buffer;
  1696. BOOL bSuccess = FALSE;
  1697. HKEY hKey = NULL;
  1698. //
  1699. // loop through values, for each value - find sdb and write out new entry
  1700. // then delete old entry
  1701. //
  1702. lResult = RegOpenKeyExW(hKeyParent,
  1703. pwszEntryName,
  1704. 0,
  1705. KEY_READ|KEY_WRITE|GetWow64Flag(),
  1706. &hKey);
  1707. if (lResult != ERROR_SUCCESS) {
  1708. vLogMessage("[ConvertEntryToNewFormat] Failed to open key \"%ls\" error 0x%lx\n",
  1709. pwszEntryName, lResult);
  1710. goto cleanup;
  1711. }
  1712. lResult = RegQueryInfoKeyW(hKey,
  1713. NULL, NULL, // class/class buffer
  1714. NULL, // reserved
  1715. NULL, NULL, // subkeys/max subkey length
  1716. NULL, // max class len
  1717. &dwValues, // value count
  1718. &dwMaxValueNameLen,
  1719. &dwMaxValueLen,
  1720. NULL, NULL);
  1721. if (lResult != ERROR_SUCCESS) {
  1722. //
  1723. // failed to query the key, very bad
  1724. // bugbug
  1725. vLogMessage("[ConvertEntryToNewFormat] Failed to query key information \"%ls\" error 0x%lx\n",
  1726. pwszEntryName, lResult);
  1727. goto cleanup;
  1728. }
  1729. //
  1730. // allocate buffers
  1731. //
  1732. pwszValueName = new WCHAR[dwMaxValueNameLen + 1];
  1733. pValue = new BYTE[dwMaxValueLen];
  1734. if (pValue == NULL || pwszValueName == NULL) {
  1735. //
  1736. // bugbug
  1737. //
  1738. vLogMessage("[ConvertEntryToNewFormat] Failed to allocate memory buffer entry \"%ls\" (0x%lx, 0x%lx)\n",
  1739. pwszEntryName, dwMaxValueNameLen, dwMaxValueLen);
  1740. goto cleanup;
  1741. }
  1742. InitializeListHead(&ValueList);
  1743. //
  1744. // we have dwValues -- the count of values
  1745. //
  1746. for (dwIndex = 0; dwIndex < dwValues; ++dwIndex) {
  1747. dwValueNameSize = dwMaxValueNameLen + 1;
  1748. dwValueSize = dwMaxValueLen;
  1749. lResult = RegEnumValueW(hKey,
  1750. dwIndex,
  1751. pwszValueName,
  1752. &dwValueNameSize,
  1753. NULL,
  1754. &dwType,
  1755. (PBYTE)pValue,
  1756. &dwValueSize);
  1757. //
  1758. // check if we are successful
  1759. //
  1760. if (lResult != ERROR_SUCCESS) {
  1761. if (lResult == ERROR_NO_MORE_ITEMS) {
  1762. //
  1763. // oops -- we ran out of values!!! Unexpected, but ok
  1764. //
  1765. vLogMessage("[ConvertEntryToNewFormat] RegEnumValue unexpectedly reports no more items for \"%ls\" index 0x%lx\n",
  1766. pwszEntryName, dwIndex);
  1767. break;
  1768. }
  1769. //
  1770. // log error and continue
  1771. //
  1772. vLogMessage("[ConvertEntryToNewFormat] RegEnumValue failed for \"%ls\" index 0x%lx error 0x%lx\n",
  1773. pwszEntryName, dwIndex, lResult);
  1774. continue;
  1775. }
  1776. if (bConvertToNewFormat) {
  1777. if (dwType != REG_SZ) {
  1778. //
  1779. // bad entry for sure -- this could be a new entry
  1780. // log warning
  1781. //
  1782. if (dwType == REG_QWORD || (dwType == REG_BINARY && dwValueSize == sizeof(ULONGLONG))) {
  1783. //
  1784. // new style entry ?
  1785. //
  1786. if (wcsrchr(pwszValueName, L'.') == NULL &&
  1787. *pwszValueName == L'{' &&
  1788. *(pwszValueName + wcslen(pwszValueName) - 1) == L'}') {
  1789. vLogMessage("[Info] Entry \"%ls\" value \"%ls\" already in new format.\n",
  1790. pwszEntryName, pwszValueName);
  1791. continue;
  1792. }
  1793. }
  1794. //
  1795. // very likely - some entry we do not understand
  1796. //
  1797. vLogMessage("[ConvertEntryToNewFormat] Bad value type (0x%lx) for entry \"%ls\" value \"%ls\" index 0x%lx\n",
  1798. dwType, pwszEntryName, pwszValueName, dwIndex);
  1799. continue;
  1800. }
  1801. //
  1802. // search by pwszValueName (which happens to be the GUID.sdb)
  1803. // this may be any kind of a string -- not nec. guid
  1804. //
  1805. if (!FindSdbListEntry(pSdbListHead, pwszValueName, &pSdbEntry, NULL)) {
  1806. //
  1807. // error - sdb not found!
  1808. //
  1809. vLogMessage("[ConvertEntryToNewFormat] Failed to find database \"%ls\" for entry \"%ls\" index 0x%lx\n",
  1810. pwszValueName, pwszEntryName, dwIndex);
  1811. continue;
  1812. }
  1813. } else {
  1814. //
  1815. // check the type first, if this is a new style entry - this will be bin
  1816. //
  1817. if (dwType == REG_SZ &&
  1818. wcsrchr(pwszValueName, L'.') != NULL &&
  1819. *(LPCWSTR)pValue == L'\0') {
  1820. vLogMessage("[Info] Entry \"%ls\" value \"%ls\" is already in required (old) format.\n",
  1821. pwszEntryName, pwszValueName);
  1822. continue;
  1823. }
  1824. if (dwType != REG_QWORD &&
  1825. (dwType != REG_BINARY || dwValueSize < sizeof(ULONGLONG))) {
  1826. //
  1827. // error -- we don't know what this entry is, go to the next one
  1828. // print warning actually
  1829. //
  1830. vLogMessage("[ConvertEntryToNewFormat] Bad value type (0x%lx) or size (0x%lx) for entry \"%ls\" value \"%ls\" index 0x%lx\n",
  1831. dwType, dwValueSize, pwszEntryName, pwszValueName, dwIndex);
  1832. continue;
  1833. }
  1834. if (!FindSdbListEntry(pSdbListHead, pwszValueName, &pSdbEntry, NULL)) {
  1835. //
  1836. // we're in trouble -- an entry has no registered database
  1837. //
  1838. vLogMessage("[ConvertEntryToNewFormat] Failed to find database for value \"%ls\" for entry \"%ls\" index 0x%lx\n",
  1839. pwszValueName, pwszEntryName, dwIndex);
  1840. continue;
  1841. }
  1842. }
  1843. //
  1844. // we have found entry and we're ready to write it out, queue it up
  1845. //
  1846. if (!AddValueEntry(&ValueList, pSdbEntry, pwszValueName)) {
  1847. //
  1848. // bugbug can't add value entry
  1849. //
  1850. vLogMessage("[ConvertEntryToNewFormat] Failed to add value \"%ls\" for entry \"%ls\" index 0x%lx\n",
  1851. pwszValueName, pwszEntryName, dwIndex);
  1852. goto cleanup;
  1853. }
  1854. }
  1855. //
  1856. // we have gone through all the values, write loop
  1857. //
  1858. bSuccess = TRUE;
  1859. pValueList = ValueList.Flink;
  1860. while (pValueList != &ValueList) {
  1861. pValueEntry = (PSDBVALUEENTRY)((PBYTE)pValueList - OFFSETOF(SDBVALUEENTRY, ListEntry));
  1862. //
  1863. // we can point to the next entry now
  1864. //
  1865. if (!WriteEntryValue(hKey, pValueEntry, bConvertToNewFormat)) {
  1866. //
  1867. // error, can't convert entry
  1868. // continue though so that we cleanout the list
  1869. vLogMessage("[ConvertEntryToNewFormat] Failed to write value for entry \"%ls\"\n",
  1870. pwszEntryName);
  1871. }
  1872. pValueList = pValueList->Flink;
  1873. }
  1874. cleanup:
  1875. if (ValueList.Flink) {
  1876. pValueList = ValueList.Flink;
  1877. while (pValueList != &ValueList) {
  1878. Buffer = (PBYTE)pValueList - OFFSETOF(SDBVALUEENTRY, ListEntry);
  1879. pValueList = pValueList->Flink;
  1880. delete[] Buffer;
  1881. }
  1882. }
  1883. if (hKey != NULL) {
  1884. RegCloseKey(hKey);
  1885. }
  1886. if (pwszValueName != NULL) {
  1887. delete[] pwszValueName;
  1888. }
  1889. if (pValue != NULL) {
  1890. delete[] pValue;
  1891. }
  1892. return bSuccess;
  1893. }
  1894. /*++
  1895. ConvertFormat
  1896. This function handles format conversions
  1897. [in] bConvertToNewFormat - true if conversion old->new, false otherwise
  1898. returns true if success
  1899. --*/
  1900. BOOL
  1901. ConvertFormat(
  1902. BOOL bConvertToNewFormat
  1903. )
  1904. {
  1905. LIST_ENTRY SdbList = { 0 }; // installed sdbs cache
  1906. HKEY hKey;
  1907. LONG lResult;
  1908. DWORD dwIndex;
  1909. WCHAR szSubKeyName[MAX_PATH];
  1910. DWORD dwBufferSize;
  1911. WCHAR szKeyPath[MAX_PATH];
  1912. BOOL bSuccess = FALSE;
  1913. //
  1914. // first convert installed sdbs
  1915. // open installed sdb key
  1916. //
  1917. lResult = RegOpenKeyExW(HKEY_LOCAL_MACHINE,
  1918. APPCOMPAT_KEY_PATH_INSTALLEDSDB_W, // path to InstalledSDB
  1919. 0,
  1920. KEY_READ|KEY_WRITE|GetWow64Flag(),
  1921. &hKey);
  1922. if (lResult != ERROR_SUCCESS) {
  1923. //
  1924. // perhaps no dbs are installed ?
  1925. //
  1926. if (lResult == ERROR_FILE_NOT_FOUND) {
  1927. //
  1928. // no installed sdbs -- no problem
  1929. //
  1930. vLogMessage("[ConvertFormat] No Installed sdbs found\n");
  1931. return TRUE;
  1932. }
  1933. //
  1934. // some sort of error has occured
  1935. //
  1936. vLogMessage("[ConvertFormat] Failed to open key \"%ls\" Error 0x%lx\n",
  1937. APPCOMPAT_KEY_PATH_INSTALLEDSDB_W, lResult);
  1938. return FALSE;
  1939. }
  1940. //
  1941. // note that ConvertInstalledSdbsToNewFormat works properly for both install and uninstall cases
  1942. //
  1943. InitializeListHead(&SdbList);
  1944. if (!ConvertInstalledSdbsToNewFormat(hKey, &SdbList)) {
  1945. goto cleanup;
  1946. }
  1947. // done with Installed sdbs
  1948. RegCloseKey(hKey);
  1949. hKey = NULL;
  1950. //
  1951. // next up is entry conversion -- first enum exes, then layers
  1952. //
  1953. lResult = RegOpenKeyExW(HKEY_LOCAL_MACHINE,
  1954. APPCOMPAT_KEY_PATH_CUSTOM_W,
  1955. 0,
  1956. KEY_READ|KEY_WRITE|GetWow64Flag(),
  1957. &hKey);
  1958. if (lResult != ERROR_SUCCESS) {
  1959. //
  1960. // what is this?
  1961. //
  1962. if (lResult == ERROR_FILE_NOT_FOUND && !IsListEmpty(&SdbList)) {
  1963. vLogMessage("[ConvertFormat] Failed to open \"%ls\" - check consistency\n",
  1964. APPCOMPAT_KEY_PATH_CUSTOM_W);
  1965. } else {
  1966. vLogMessage("[ConvertFormat] Failed to open \"%ls\" error 0x%lx\n",
  1967. APPCOMPAT_KEY_PATH_CUSTOM_W, lResult);
  1968. }
  1969. goto cleanup;
  1970. }
  1971. dwIndex = 0;
  1972. while (TRUE) {
  1973. dwBufferSize = sizeof(szSubKeyName)/sizeof(szSubKeyName[0]);
  1974. lResult = RegEnumKeyExW(hKey,
  1975. dwIndex,
  1976. szSubKeyName,
  1977. &dwBufferSize,
  1978. NULL, NULL, NULL,
  1979. NULL);
  1980. ++dwIndex;
  1981. if (lResult != ERROR_SUCCESS) {
  1982. if (lResult == ERROR_NO_MORE_ITEMS) {
  1983. break;
  1984. }
  1985. //
  1986. // some sort of error, log and continue
  1987. //
  1988. vLogMessage("[ConvertFormat] RegEnumKey (entries) returned error for index 0x%lx error 0x%lx\n",
  1989. dwIndex, lResult);
  1990. break;
  1991. }
  1992. //
  1993. // skip layers for now
  1994. //
  1995. if (!_wcsicmp(szSubKeyName, L"Layers")) {
  1996. continue;
  1997. }
  1998. // for each of these -- call fixup function
  1999. if (!ConvertEntryToNewFormat(hKey, szSubKeyName, &SdbList, bConvertToNewFormat)) {
  2000. vLogMessage("[ConvertFormat] Failed to convert entry \"%ls\"\n", szSubKeyName);
  2001. }
  2002. }
  2003. RegCloseKey(hKey);
  2004. hKey = NULL;
  2005. //
  2006. // next up - layers
  2007. //
  2008. StringCchCopyW(szKeyPath, ARRAYSIZE(szKeyPath), APPCOMPAT_KEY_PATH_CUSTOM_W);
  2009. StringCchCatW(szKeyPath, ARRAYSIZE(szKeyPath), L"\\Layers");
  2010. //
  2011. // open and enum layers
  2012. //
  2013. lResult = RegOpenKeyExW(HKEY_LOCAL_MACHINE,
  2014. szKeyPath,
  2015. 0,
  2016. KEY_READ|KEY_WRITE|GetWow64Flag(),
  2017. &hKey);
  2018. if (lResult != ERROR_SUCCESS) {
  2019. // maybe dead ?
  2020. if (lResult == ERROR_FILE_NOT_FOUND) {
  2021. //
  2022. // it's ok, maybe we have none of those ?
  2023. //
  2024. vLogMessage("[ConvertFormat] No layers found\n");
  2025. goto ConvertComplete;
  2026. }
  2027. vLogMessage("[ConvertFormat] Failed to open \"%ls\" error 0x%lx\n", szKeyPath, lResult);
  2028. goto cleanup;
  2029. }
  2030. dwIndex = 0;
  2031. while (TRUE) {
  2032. dwBufferSize = sizeof(szSubKeyName)/sizeof(szSubKeyName[0]);
  2033. lResult = RegEnumKeyExW(hKey,
  2034. dwIndex,
  2035. szSubKeyName,
  2036. &dwBufferSize,
  2037. NULL, NULL, NULL,
  2038. NULL);
  2039. ++dwIndex;
  2040. if (lResult != ERROR_SUCCESS) {
  2041. // check if this was the last entry
  2042. if (lResult == ERROR_NO_MORE_ITEMS) {
  2043. // clean break
  2044. break;
  2045. }
  2046. // some sort of error, log and continue
  2047. vLogMessage("[ConvertFormat] RegEnumKey (layers) returned error for index 0x%lx error 0x%lx\n",
  2048. dwIndex, lResult);
  2049. break;
  2050. }
  2051. // for each of these -- call fixup function
  2052. if (!ConvertEntryToNewFormat(hKey, szSubKeyName, &SdbList, bConvertToNewFormat)) {
  2053. vLogMessage("[ConvertFormat] Failed to convert entry \"%ls\"\n", szSubKeyName);
  2054. }
  2055. }
  2056. RegCloseKey(hKey);
  2057. hKey = NULL;
  2058. ConvertComplete:
  2059. bSuccess = TRUE;
  2060. cleanup:
  2061. if (hKey != NULL) {
  2062. RegCloseKey(hKey);
  2063. }
  2064. //
  2065. // free SdbList
  2066. //
  2067. CleanupSdbList(&SdbList);
  2068. return bSuccess;
  2069. }
  2070. BOOL
  2071. ProcessMSIPackages(
  2072. PDB pdb,
  2073. TAGID tiDatabase,
  2074. LPCWSTR pszGuidDB,
  2075. ULONGLONG ullSdbTimeStamp,
  2076. INSTALL_MODE eMode)
  2077. {
  2078. TAGID tiMsiPackage;
  2079. TAGID tiMsiPackageID;
  2080. GUID* pGuidID;
  2081. GUID GuidID;
  2082. WCHAR szRegPath[MAX_PATH];
  2083. BOOL bReturn = TRUE;
  2084. WCHAR wszGuid[64];
  2085. UNICODE_STRING ustrGuid = { 0 };
  2086. tiMsiPackage = SdbFindFirstTag(pdb, tiDatabase, TAG_MSI_PACKAGE);
  2087. while (tiMsiPackage && bReturn) {
  2088. //
  2089. // we have a package, extract/find TAG_MSI_PACKAGE_ID
  2090. //
  2091. tiMsiPackageID = SdbFindFirstTag(pdb, tiMsiPackage, TAG_MSI_PACKAGE_ID);
  2092. if (!tiMsiPackageID) {
  2093. if (eMode == MODE_CLEANUP || eMode == MODE_UNINSTALL) {
  2094. goto NextPackage;
  2095. } else {
  2096. vPrintError(IDS_MISSING_PACKAGE_ID);
  2097. bReturn = FALSE;
  2098. break;
  2099. }
  2100. }
  2101. pGuidID = (GUID*)SdbGetBinaryTagData(pdb, tiMsiPackageID);
  2102. if (pGuidID == NULL) {
  2103. if (eMode == MODE_CLEANUP || eMode == MODE_UNINSTALL) {
  2104. goto NextPackage;
  2105. } else {
  2106. vPrintError(IDS_MISSING_PACKAGE_ID);
  2107. bReturn = FALSE;
  2108. break;
  2109. }
  2110. }
  2111. RtlCopyMemory(&GuidID, pGuidID, sizeof(GUID));
  2112. if (!NT_SUCCESS(RtlStringFromGUID(GuidID, &ustrGuid))) {
  2113. vPrintError(IDS_GUID_BAD_FORMAT);
  2114. bReturn = FALSE;
  2115. break;
  2116. }
  2117. RtlCopyMemory(wszGuid, ustrGuid.Buffer, ustrGuid.Length);
  2118. wszGuid[ustrGuid.Length / sizeof(WCHAR)] = TEXT('\0');
  2119. if (eMode == MODE_INSTALL) {
  2120. bReturn = InstallSdbEntry(wszGuid, pszGuidDB, ullSdbTimeStamp, FALSE);
  2121. } else {
  2122. bReturn = UninstallSdbEntry(wszGuid, pszGuidDB, FALSE);
  2123. }
  2124. RtlFreeUnicodeString(&ustrGuid);
  2125. NextPackage:
  2126. tiMsiPackage = SdbFindNextTag(pdb, tiDatabase, tiMsiPackage);
  2127. }
  2128. return bReturn;
  2129. }
  2130. #define MAX_FRIENDLY_NAME_LEN 256
  2131. BOOL
  2132. bHandleInstall(
  2133. WCHAR* wszSdbPath,
  2134. INSTALL_MODE eMode,
  2135. WCHAR* wszSdbInstallPath,
  2136. DWORD dwSdbInstallPathSize
  2137. )
  2138. {
  2139. PDB pdb = NULL;
  2140. int i;
  2141. WCHAR wszSdbName[MAX_PATH];
  2142. WCHAR wszSdbInstallName[MAX_PATH];
  2143. HKEY hKey = NULL;
  2144. LONG lRes;
  2145. TAGID tiDatabase, tiExe, tiLayer;
  2146. TAGID tiDBName = TAGID_NULL;
  2147. WCHAR* pszDBName = NULL;
  2148. WCHAR wszFriendlyName[MAX_FRIENDLY_NAME_LEN];
  2149. WCHAR* wszTemp;
  2150. GUID guidDB;
  2151. NTSTATUS Status;
  2152. FILETIME SystemTime;
  2153. BOOL bRet = TRUE;
  2154. UNICODE_STRING ustrGUID;
  2155. ULARGE_INTEGER TimeStamp = { 0 };
  2156. //
  2157. // determine the timestamp (for the install case)
  2158. //
  2159. if (eMode == MODE_INSTALL) {
  2160. GetSystemTimeAsFileTime(&SystemTime);
  2161. TimeStamp.LowPart = SystemTime.dwLowDateTime;
  2162. TimeStamp.HighPart = SystemTime.dwHighDateTime;
  2163. }
  2164. assert(wszSdbPath && wszSdbInstallPath);
  2165. if (!wszSdbPath || !wszSdbInstallPath) {
  2166. bRet = FALSE;
  2167. goto quickOut;
  2168. }
  2169. ZeroMemory(wszFriendlyName, sizeof(wszFriendlyName));
  2170. //
  2171. // get the full path from the file name
  2172. //
  2173. wszTemp = wszGetFileFromPath(wszSdbPath);
  2174. if (!wszTemp) {
  2175. vPrintMessage(IDS_UNABLE_TO_GET_FILE);
  2176. bRet = FALSE;
  2177. goto quickOut;
  2178. }
  2179. StringCchCopyW(wszSdbName, ARRAYSIZE(wszSdbName), wszTemp);
  2180. if (wcscmp(wszSdbName, L"sysmain.sdb") == 0) {
  2181. vPrintError(IDS_CANT_INSTALL_SYS);
  2182. bRet = FALSE;
  2183. goto quickOut;
  2184. }
  2185. if (GetFileAttributesW(wszSdbPath) != -1 && bIsAlreadyInstalled(wszSdbPath)) {
  2186. if (eMode == MODE_INSTALL) {
  2187. //
  2188. // they asked us to install, it's installed, so we're done
  2189. //
  2190. vPrintMessage(IDS_ALREADY_INSTALLED, wszSdbPath);
  2191. goto quickOut;
  2192. }
  2193. } else {
  2194. if (eMode == MODE_UNINSTALL) {
  2195. //
  2196. // they asked us to uninstall, it's not installed, so we're done
  2197. //
  2198. vPrintMessage(IDS_NOT_INSTALLED, wszSdbPath);
  2199. goto quickOut;
  2200. }
  2201. }
  2202. if (eMode == MODE_INSTALL) {
  2203. //
  2204. // find out what file name we're going to use for installing
  2205. //
  2206. if (!bFindInstallName(wszSdbPath, wszSdbInstallPath, dwSdbInstallPathSize)) {
  2207. bRet = FALSE;
  2208. goto quickOut;
  2209. }
  2210. } else if (eMode == MODE_CLEANUP) {
  2211. //
  2212. // we're cleaning up a bad install, so we need to get the install name from the
  2213. // install path
  2214. //
  2215. wszTemp = wszGetFileFromPath(wszSdbInstallPath);
  2216. if (!wszTemp) {
  2217. vPrintMessage(IDS_UNABLE_TO_GET_FILE);
  2218. bRet = FALSE;
  2219. goto quickOut;
  2220. }
  2221. } else {
  2222. //
  2223. // we're uninstalling, so the install name is the given name
  2224. // and the install path is the given path
  2225. //
  2226. StringCchCopyW(wszSdbInstallPath, dwSdbInstallPathSize, wszSdbPath);
  2227. }
  2228. //
  2229. // try to get the guid for later
  2230. //
  2231. if (!bGetGuid(wszSdbPath, &guidDB)) {
  2232. bRet = FALSE;
  2233. goto out;
  2234. }
  2235. //
  2236. // check whether the guid is coopted from one of the known databases
  2237. //
  2238. if (IsKnownDatabaseGUID(&guidDB)) {
  2239. vPrintError(IDS_CANT_INSTALL_SYS);
  2240. bRet = FALSE;
  2241. goto quickOut;
  2242. }
  2243. //
  2244. // in all cases, install name is the db GUID
  2245. //
  2246. Status = RtlStringFromGUID(guidDB, &ustrGUID);
  2247. if (!NT_SUCCESS(Status)) {
  2248. bRet = FALSE;
  2249. goto out;
  2250. }
  2251. RtlCopyMemory(wszSdbInstallName, ustrGUID.Buffer, ustrGUID.Length);
  2252. wszSdbInstallName[ustrGUID.Length/sizeof(WCHAR)] = L'\0';
  2253. RtlFreeUnicodeString(&ustrGUID);
  2254. //
  2255. // if we're installing, make sure the root tags are in place
  2256. //
  2257. if (eMode == MODE_INSTALL) {
  2258. lRes = RegCreateKeyExW(HKEY_LOCAL_MACHINE,
  2259. APPCOMPAT_KEY_PATH_W,
  2260. 0,
  2261. NULL,
  2262. 0,
  2263. KEY_ALL_ACCESS|GetWow64Flag(),
  2264. NULL,
  2265. &hKey,
  2266. NULL);
  2267. if (lRes != ERROR_SUCCESS) {
  2268. if (lRes == ERROR_ACCESS_DENIED) {
  2269. vPrintError(IDS_NEED_INSTALL_PERMISSION);
  2270. } else {
  2271. vPrintError(IDS_CANT_CREATE_REG_KEY, APPCOMPAT_KEY_PATH_W);
  2272. }
  2273. bRet = FALSE;
  2274. goto out;
  2275. }
  2276. RegCloseKey(hKey);
  2277. hKey = NULL;
  2278. lRes = RegCreateKeyExW(HKEY_LOCAL_MACHINE,
  2279. APPCOMPAT_KEY_PATH_CUSTOM_W,
  2280. 0,
  2281. NULL,
  2282. 0,
  2283. KEY_ALL_ACCESS|GetWow64Flag(),
  2284. NULL,
  2285. &hKey,
  2286. NULL);
  2287. if (lRes != ERROR_SUCCESS) {
  2288. vPrintError(IDS_CANT_CREATE_REG_KEY, APPCOMPAT_KEY_PATH_CUSTOM_W);
  2289. bRet = FALSE;
  2290. goto out;
  2291. }
  2292. RegCloseKey(hKey);
  2293. hKey = NULL;
  2294. }
  2295. // Open the DB.
  2296. pdb = SdbOpenDatabase(wszSdbPath, DOS_PATH);
  2297. if (pdb == NULL) {
  2298. vPrintError(IDS_UNABLE_TO_OPEN_FILE, wszSdbPath);
  2299. bRet = FALSE;
  2300. goto out;
  2301. }
  2302. tiDatabase = SdbFindFirstTag(pdb, TAGID_ROOT, TAG_DATABASE);
  2303. if (!tiDatabase) {
  2304. vPrintError(IDS_NO_DB_TAG, wszSdbPath);
  2305. bRet = FALSE;
  2306. goto out;
  2307. }
  2308. //
  2309. // get the friendly name of the database
  2310. //
  2311. tiDBName = SdbFindFirstTag(pdb, tiDatabase, TAG_NAME);
  2312. if (tiDBName) {
  2313. pszDBName = SdbGetStringTagPtr(pdb, tiDBName);
  2314. }
  2315. //
  2316. // if we don't find a friendly name, use the SDB file name
  2317. //
  2318. if (pszDBName) {
  2319. StringCchCopyW(wszFriendlyName, ARRAYSIZE(wszFriendlyName), pszDBName);
  2320. } else {
  2321. StringCchCopyW(wszFriendlyName, ARRAYSIZE(wszFriendlyName), wszSdbName);
  2322. }
  2323. tiExe = SdbFindFirstTag(pdb, tiDatabase, TAG_EXE);
  2324. while (tiExe) {
  2325. WCHAR szRegPath[MAX_PATH];
  2326. TAGID tiName;
  2327. WCHAR *szName;
  2328. TAGID tiTemp;
  2329. tiName = SdbFindFirstTag(pdb, tiExe, TAG_NAME);
  2330. if (!tiName) {
  2331. bRet = FALSE;
  2332. if (eMode == MODE_CLEANUP || eMode == MODE_UNINSTALL) {
  2333. goto nextExe;
  2334. } else {
  2335. vPrintError(IDS_NO_EXE_NAME);
  2336. goto quickOut;
  2337. }
  2338. }
  2339. szName = SdbGetStringTagPtr(pdb, tiName);
  2340. if (!szName) {
  2341. bRet = FALSE;
  2342. if (eMode == MODE_CLEANUP || eMode == MODE_UNINSTALL) {
  2343. goto nextExe;
  2344. } else {
  2345. vPrintError(IDS_NO_EXE_NAME_PTR);
  2346. goto quickOut;
  2347. }
  2348. }
  2349. if (eMode == MODE_INSTALL) {
  2350. if (!InstallSdbEntry(szName, wszSdbInstallName, TimeStamp.QuadPart, FALSE) ||
  2351. !ProcessLUAAction(pdb, tiExe)) {
  2352. bRet = FALSE;
  2353. goto out;
  2354. }
  2355. } else {
  2356. if (!UninstallSdbEntry(szName, wszSdbInstallName, FALSE)) {
  2357. goto quickOut;
  2358. }
  2359. }
  2360. nextExe:
  2361. tiTemp = tiExe;
  2362. tiExe = SdbFindNextTag(pdb, tiDatabase, tiExe);
  2363. //
  2364. // fallback to ensure we don't get caught in a loop because of a quirk in
  2365. // sdbapi and a corrupt database. If we get handed back the same TAGID as before,
  2366. // get out.
  2367. //
  2368. if (tiExe == tiTemp) {
  2369. break;
  2370. }
  2371. }
  2372. //
  2373. // Loop through the published layers
  2374. //
  2375. tiLayer = SdbFindFirstTag(pdb, tiDatabase, TAG_LAYER);
  2376. while (tiLayer) {
  2377. WCHAR szRegPath[MAX_PATH];
  2378. TAGID tiName;
  2379. WCHAR* szName;
  2380. TAGID tiTemp;
  2381. tiName = SdbFindFirstTag(pdb, tiLayer, TAG_NAME);
  2382. if (!tiName) {
  2383. bRet = FALSE;
  2384. if (eMode == MODE_CLEANUP || eMode == MODE_UNINSTALL) {
  2385. goto nextLayer;
  2386. } else {
  2387. vPrintError(IDS_NO_EXE_NAME);
  2388. goto quickOut;
  2389. }
  2390. }
  2391. szName = SdbGetStringTagPtr(pdb, tiName);
  2392. if (!szName) {
  2393. bRet = FALSE;
  2394. if (eMode == MODE_CLEANUP || eMode == MODE_UNINSTALL) {
  2395. goto nextLayer;
  2396. } else {
  2397. vPrintError(IDS_NO_EXE_NAME_PTR);
  2398. goto quickOut;
  2399. }
  2400. }
  2401. if (eMode == MODE_INSTALL) {
  2402. if (!InstallSdbEntry(szName, wszSdbInstallName, TimeStamp.QuadPart, TRUE)) {
  2403. bRet = FALSE;
  2404. goto out;
  2405. }
  2406. } else {
  2407. if (!UninstallSdbEntry(szName, wszSdbInstallName, TRUE)) {
  2408. goto quickOut;
  2409. }
  2410. }
  2411. nextLayer:
  2412. tiTemp = tiLayer;
  2413. tiLayer = SdbFindNextTag(pdb, tiDatabase, tiLayer);
  2414. //
  2415. // fallback to ensure we don't get caught in a loop because of a quirk in
  2416. // sdbapi and a corrupt database. If we get handed back the same TAGID as before,
  2417. // get out.
  2418. //
  2419. if (tiLayer == tiTemp) {
  2420. break;
  2421. }
  2422. }
  2423. if (!ProcessMSIPackages(pdb, tiDatabase, wszSdbInstallName, TimeStamp.QuadPart, eMode)) {
  2424. bRet = FALSE;
  2425. goto quickOut;
  2426. }
  2427. if (pdb) {
  2428. SdbCloseDatabase(pdb);
  2429. pdb = NULL;
  2430. }
  2431. //
  2432. // now that we've handled the registry keys, copy the file
  2433. //
  2434. if (eMode == MODE_INSTALL) {
  2435. //
  2436. // ensure the directory exists
  2437. //
  2438. CreateDirectoryW(g_wszCustom, NULL);
  2439. if (!CopyFileW(wszSdbPath, wszSdbInstallPath, TRUE)) {
  2440. vPrintError(IDS_CANT_COPY_FILE, wszSdbInstallPath);
  2441. bRet = FALSE;
  2442. goto out;
  2443. }
  2444. } else {
  2445. //
  2446. // ensure that we don't fail because of read-only files
  2447. //
  2448. SetFileAttributesW(wszSdbInstallPath, FILE_ATTRIBUTE_NORMAL);
  2449. if (!DeleteFileW(wszSdbInstallPath)) {
  2450. vPrintError(IDS_CANT_DELETE_FILE, wszSdbInstallPath);
  2451. bRet = FALSE;
  2452. }
  2453. }
  2454. //
  2455. // set up or delete the uninstall registry keys
  2456. //
  2457. if (eMode == MODE_INSTALL) {
  2458. WCHAR wszSDBInstPath[MAX_PATH];
  2459. WCHAR wszUninstallPath[MAX_PATH];
  2460. WCHAR wszUninstallString[MAX_PATH * 2 + 10];
  2461. HRESULT hr;
  2462. //
  2463. // goofball hack required because of crazy redirection strategy on IA64
  2464. //
  2465. wszSDBInstPath[0] = 0;
  2466. SdbGetWindowsDirectory(wszSDBInstPath, ARRAYSIZE(wszSDBInstPath));
  2467. #if defined(_WIN64)
  2468. StringCchCatW(wszSDBInstPath, ARRAYSIZE(wszSDBInstPath), L"System32\\sdbinst.exe");
  2469. #else
  2470. StringCchCatW(wszSDBInstPath, ARRAYSIZE(wszSDBInstPath), L"SysWow64\\sdbinst.exe");
  2471. #endif
  2472. if (GetFileAttributesW(wszSDBInstPath) == -1) {
  2473. //
  2474. // there's no SysWow64 directory, so we'll just use system32
  2475. //
  2476. wszSDBInstPath[0] = 0;
  2477. SdbGetWindowsDirectory(wszSDBInstPath, ARRAYSIZE(wszSDBInstPath));
  2478. StringCchCatW(wszSDBInstPath, ARRAYSIZE(wszSDBInstPath), L"system32\\sdbinst.exe");
  2479. }
  2480. StringCchCopyW(wszUninstallPath, ARRAYSIZE(wszUninstallPath), UNINSTALL_KEY_PATH);
  2481. StringCchCatW(wszUninstallPath, ARRAYSIZE(wszUninstallPath), wszSdbInstallName);
  2482. StringCchCatW(wszUninstallPath, ARRAYSIZE(wszUninstallPath), L".sdb");
  2483. lRes = RegCreateKeyExW(HKEY_LOCAL_MACHINE,
  2484. wszUninstallPath,
  2485. 0,
  2486. NULL,
  2487. REG_OPTION_NON_VOLATILE,
  2488. KEY_ALL_ACCESS|GetWow64Flag(),
  2489. NULL,
  2490. &hKey,
  2491. NULL);
  2492. if (lRes != ERROR_SUCCESS) {
  2493. vPrintError(IDS_CANT_CREATE_REG_KEY, wszUninstallPath);
  2494. bRet = FALSE;
  2495. goto out;
  2496. }
  2497. lRes = RegSetValueExW(hKey,
  2498. L"DisplayName",
  2499. 0,
  2500. REG_SZ,
  2501. (PBYTE)wszFriendlyName,
  2502. (wcslen(wszFriendlyName) + 1) * sizeof(WCHAR));
  2503. if (lRes != ERROR_SUCCESS) {
  2504. vPrintError(IDS_CANT_CREATE_VALUE, wszUninstallPath);
  2505. bRet = FALSE;
  2506. goto out;
  2507. }
  2508. hr = StringCchPrintfW(wszUninstallString, ARRAYSIZE(wszUninstallString), L"%s -u \"%s\"", wszSDBInstPath, wszSdbInstallPath);
  2509. if (FAILED(hr)) {
  2510. vPrintError(IDS_CANT_CREATE_VALUE, wszUninstallPath);
  2511. bRet = FALSE;
  2512. goto out;
  2513. }
  2514. lRes = RegSetValueExW(hKey, L"UninstallString", 0, REG_SZ,
  2515. (PBYTE)wszUninstallString, (wcslen(wszUninstallString) + 1) * sizeof(WCHAR));
  2516. if (lRes != ERROR_SUCCESS) {
  2517. vPrintError(IDS_CANT_CREATE_VALUE, wszUninstallPath);
  2518. bRet = FALSE;
  2519. goto out;
  2520. }
  2521. RegCloseKey(hKey);
  2522. hKey = NULL;
  2523. } else {
  2524. WCHAR wszUninstallPath[MAX_PATH];
  2525. lRes = RegOpenKeyExW(HKEY_LOCAL_MACHINE, UNINSTALL_KEY_PATH, 0, KEY_ALL_ACCESS|GetWow64Flag(), &hKey);
  2526. if (lRes != ERROR_SUCCESS) {
  2527. vPrintError(IDS_CANT_OPEN_REG_KEY, UNINSTALL_KEY_PATH);
  2528. bRet = FALSE;
  2529. goto out;
  2530. }
  2531. //
  2532. // create sdb path name
  2533. //
  2534. StringCchCopyW(wszUninstallPath, ARRAYSIZE(wszUninstallPath), wszSdbInstallName);
  2535. StringCchCatW(wszUninstallPath, ARRAYSIZE(wszUninstallPath), L".sdb");
  2536. lRes = LocalRegDeleteKeyW(hKey, wszUninstallPath);
  2537. if (lRes != ERROR_SUCCESS) {
  2538. vPrintError(IDS_CANT_DELETE_REG_KEY, wszSdbInstallName, UNINSTALL_KEY_PATH);
  2539. }
  2540. RegCloseKey(hKey);
  2541. hKey = NULL;
  2542. }
  2543. //
  2544. // register or unregister the DB
  2545. //
  2546. if (eMode == MODE_INSTALL) {
  2547. if (!SdbRegisterDatabaseEx(wszSdbInstallPath, SDB_DATABASE_SHIM, &TimeStamp.QuadPart)) {
  2548. vPrintError(IDS_CANT_REGISTER_DB, wszFriendlyName);
  2549. bRet = FALSE;
  2550. goto out;
  2551. }
  2552. } else {
  2553. if (!SdbUnregisterDatabase(&guidDB)) {
  2554. vPrintError(IDS_CANT_UNREGISTER_DB, wszFriendlyName);
  2555. }
  2556. }
  2557. if (eMode == MODE_INSTALL) {
  2558. vPrintMessage(IDS_INSTALL_COMPLETE, wszFriendlyName);
  2559. } else {
  2560. vPrintMessage(IDS_UNINSTALL_COMPLETE, wszFriendlyName);
  2561. }
  2562. out:
  2563. //
  2564. // always silently delete the file on uninstall, whether we failed to remove the
  2565. // registry entries or not.
  2566. //
  2567. if (eMode != MODE_INSTALL) {
  2568. //
  2569. // need to make sure the pdb is closed before deleting it.
  2570. //
  2571. if (pdb) {
  2572. SdbCloseDatabase(pdb);
  2573. pdb = NULL;
  2574. }
  2575. //
  2576. // ensure that we don't fail because of read-only files
  2577. //
  2578. SetFileAttributesW(wszSdbInstallPath, FILE_ATTRIBUTE_NORMAL);
  2579. DeleteFileW(wszSdbInstallPath);
  2580. }
  2581. quickOut:
  2582. //
  2583. // these cleanup steps are not strictly necessary, as they'll be cleaned up
  2584. // on exit anyway. But what the heck.
  2585. //
  2586. if (pdb) {
  2587. SdbCloseDatabase(pdb);
  2588. pdb = NULL;
  2589. }
  2590. if (hKey) {
  2591. RegCloseKey(hKey);
  2592. hKey = NULL;
  2593. }
  2594. return bRet;
  2595. }
  2596. extern "C" int APIENTRY
  2597. wWinMain(
  2598. HINSTANCE hInstance,
  2599. HINSTANCE hPrevInstance,
  2600. LPWSTR lpCmdLine,
  2601. int nCmdShow
  2602. )
  2603. {
  2604. int i;
  2605. int nReturn = 0;
  2606. WCHAR wszSdbName[MAX_PATH];
  2607. WCHAR wszSdbPath[MAX_PATH];
  2608. WCHAR wszSdbInstallPath[MAX_PATH];
  2609. WCHAR wszOldSdbPath[MAX_PATH];
  2610. TAGID tiDBName = TAGID_NULL;
  2611. WCHAR* pszDBName = NULL;
  2612. WCHAR wszFriendlyName[256];
  2613. WCHAR wszGuid[100];
  2614. OSVERSIONINFO osvi;
  2615. LPWSTR szCommandLine;
  2616. LPWSTR* argv;
  2617. int argc;
  2618. INSTALL_MODE eMode;
  2619. HRESULT hr;
  2620. DWORD dwLen;
  2621. g_hInst = hInstance;
  2622. //
  2623. // check to make sure the user is an administrator
  2624. //
  2625. if (!bCanRun()) {
  2626. vPrintError(IDS_NEED_INSTALL_PERMISSION);
  2627. return 1;
  2628. }
  2629. //
  2630. // init custom directory
  2631. //
  2632. g_wszCustom[0] = 0;
  2633. SdbGetWindowsDirectory(g_wszCustom, ARRAYSIZE(g_wszCustom));
  2634. StringCchCatW(g_wszCustom, ARRAYSIZE(g_wszCustom), L"AppPatch\\Custom\\");
  2635. CreateDirectoryW(g_wszCustom, NULL);
  2636. #if defined(_WIN64)
  2637. StringCchCatW(g_wszCustom, ARRAYSIZE(g_wszCustom), L"IA64\\");
  2638. CreateDirectoryW(g_wszCustom, NULL);
  2639. #endif // _WIN64
  2640. RtlZeroMemory(&osvi, sizeof(OSVERSIONINFO));
  2641. osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
  2642. GetVersionEx(&osvi);
  2643. if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 0) {
  2644. g_bWin2K = TRUE;
  2645. }
  2646. //
  2647. // Note that this memory isn't freed because it will automatically
  2648. // be freed on exit anyway, and there are a lot of exit cases from this application
  2649. //
  2650. szCommandLine = GetCommandLineW();
  2651. argv = CommandLineToArgvW(szCommandLine, &argc);
  2652. if (!argv) {
  2653. vPrintError(IDS_CANT_GET_ARGS);
  2654. return 1;
  2655. }
  2656. if (argc < 2) {
  2657. vPrintHelp(argv[0]);
  2658. return 0;
  2659. }
  2660. g_bQuiet = FALSE;
  2661. eMode = MODE_INSTALL;
  2662. wszSdbName[0] = 0;
  2663. wszGuid[0] = 0;
  2664. wszFriendlyName[0] = 0;
  2665. for (i = 1; i < argc; ++i) {
  2666. if (argv[i][0] == L'-' || argv[i][0] == L'/') {
  2667. switch (tolower(argv[i][1])) {
  2668. case L'?':
  2669. vPrintHelp(argv[0]);
  2670. return 0;
  2671. break;
  2672. case L'c':
  2673. //
  2674. // convert entries to new format
  2675. //
  2676. eMode = MODE_CONVERT_FORMAT_NEW;
  2677. break;
  2678. case L'g':
  2679. i++;
  2680. if (i >= argc) {
  2681. vPrintError(IDS_NEED_ARG, argv[i-1]);
  2682. vPrintHelp(argv[0]);
  2683. return 1;
  2684. }
  2685. eMode = MODE_UNINSTALL;
  2686. hr = StringCchCopyW(wszGuid, ARRAYSIZE(wszGuid), argv[i]);
  2687. if (FAILED(hr)) {
  2688. vPrintError(IDS_ARG_TOO_LONG);
  2689. vPrintHelp(argv[0]);
  2690. return 1;
  2691. }
  2692. break;
  2693. case L'n':
  2694. i++;
  2695. if (i >= argc) {
  2696. vPrintError(IDS_NEED_ARG, argv[i-1]);
  2697. vPrintHelp(argv[0]);
  2698. return 1;
  2699. }
  2700. eMode = MODE_UNINSTALL;
  2701. hr = StringCchCopyW(wszFriendlyName, ARRAYSIZE(wszFriendlyName), argv[i]);
  2702. if (FAILED(hr)) {
  2703. vPrintError(IDS_ARG_TOO_LONG);
  2704. vPrintHelp(argv[0]);
  2705. return 1;
  2706. }
  2707. break;
  2708. case L'p':
  2709. g_bAllowPatches = TRUE;
  2710. break;
  2711. case L'r':
  2712. //
  2713. // revert to old format
  2714. //
  2715. eMode = MODE_CONVERT_FORMAT_OLD;
  2716. break;
  2717. case L'q':
  2718. g_bQuiet = TRUE;
  2719. break;
  2720. case L'u':
  2721. eMode = MODE_UNINSTALL;
  2722. break;
  2723. default:
  2724. vPrintError(IDS_INVALID_SWITCH, argv[i]);
  2725. vPrintHelp(argv[0]);
  2726. return 1;
  2727. }
  2728. } else {
  2729. if (wszSdbName[0]) {
  2730. vPrintError(IDS_TOO_MANY_ARGS);
  2731. vPrintHelp(argv[0]);
  2732. return 1;
  2733. }
  2734. hr = StringCchCopyW(wszSdbName, ARRAYSIZE(wszSdbName), argv[i]);
  2735. if (FAILED(hr)) {
  2736. vPrintError(IDS_ARG_TOO_LONG);
  2737. vPrintHelp(argv[0]);
  2738. return 1;
  2739. }
  2740. }
  2741. }
  2742. //
  2743. // check if we are running in a special 'setup' mode (converting or reverting the entries)
  2744. //
  2745. if (eMode == MODE_CONVERT_FORMAT_NEW || eMode == MODE_CONVERT_FORMAT_OLD) {
  2746. OpenLogFile();
  2747. if (!ConvertFormat(eMode == MODE_CONVERT_FORMAT_NEW)) {
  2748. nReturn = 1;
  2749. }
  2750. CloseLogFile();
  2751. return nReturn;
  2752. }
  2753. if (eMode == MODE_INSTALL && !wszSdbName[0]) {
  2754. vPrintError(IDS_MUST_SPECIFY_SDB);
  2755. vPrintHelp(argv[0]);
  2756. return 1;
  2757. }
  2758. if (eMode == MODE_UNINSTALL && !wszSdbName[0] && !wszGuid[0] && !wszFriendlyName[0]) {
  2759. vPrintError(IDS_MUST_SPECIFY_SDB);
  2760. vPrintHelp(argv[0]);
  2761. return 1;
  2762. }
  2763. if (wszSdbName[0]) {
  2764. if (wszSdbName[1] == L':' || wszSdbName[1] == L'\\') {
  2765. //
  2766. // this is a full path name, so just copy it
  2767. //
  2768. hr = StringCchCopyW(wszSdbPath, ARRAYSIZE(wszSdbPath), wszSdbName);
  2769. if (FAILED(hr)) {
  2770. vPrintError(IDS_ARG_TOO_LONG);
  2771. vPrintHelp(argv[0]);
  2772. return 1;
  2773. }
  2774. } else {
  2775. DWORD dwRet;
  2776. //
  2777. // this is a relative path name, so get the full one
  2778. //
  2779. if (!_wfullpath(wszSdbPath, wszSdbName, ARRAYSIZE(wszSdbPath))) {
  2780. vPrintError(IDS_CANT_GET_FULL_PATH);
  2781. return 1;
  2782. }
  2783. }
  2784. }
  2785. //
  2786. // First, get the real file name from other params, if necessary
  2787. //
  2788. if (eMode == MODE_UNINSTALL) {
  2789. if (wszGuid[0]) {
  2790. DWORD dwLen = wcslen(wszGuid);
  2791. if (dwLen != 38 || wszGuid[0] != L'{' || wszGuid[dwLen - 1] != L'}' ||
  2792. wszGuid[9] != L'-' || wszGuid[14] != L'-' || wszGuid[19] != L'-' ||
  2793. wszGuid[24] != L'-') {
  2794. vPrintError(IDS_GUID_BAD_FORMAT);
  2795. return 1;
  2796. }
  2797. StringCchCopyW(wszSdbName, ARRAYSIZE(wszSdbName), wszGuid);
  2798. StringCchCatW(wszSdbName, ARRAYSIZE(wszSdbName), L".sdb");
  2799. StringCchCopyW(wszSdbPath, ARRAYSIZE(wszSdbPath), g_wszCustom);
  2800. StringCchCatW(wszSdbPath, ARRAYSIZE(wszSdbPath), wszSdbName);
  2801. } else if (wszFriendlyName[0]) {
  2802. if (!bFriendlyNameToFile(wszFriendlyName, wszSdbName, ARRAYSIZE(wszSdbName), wszSdbPath, ARRAYSIZE(wszSdbPath))) {
  2803. vPrintError(IDS_NO_FRIENDLY_NAME, wszFriendlyName);
  2804. return 1;
  2805. }
  2806. } else {
  2807. if (!bIsAlreadyInstalled(wszSdbPath)) {
  2808. WCHAR wszSdbPathTemp[MAX_PATH];
  2809. //
  2810. // they're not giving us an installed file, so get the GUID and convert to a file
  2811. //
  2812. if (!bFindInstallName(wszSdbPath, wszSdbPathTemp, ARRAYSIZE(wszSdbPathTemp))) {
  2813. return 1;
  2814. }
  2815. StringCchCopyW(wszSdbName, ARRAYSIZE(wszSdbName), wszSdbPathTemp); // name and path are the same
  2816. StringCchCopyW(wszSdbPath, ARRAYSIZE(wszSdbPath), wszSdbPathTemp);
  2817. }
  2818. }
  2819. }
  2820. if (eMode == MODE_INSTALL &&
  2821. GetFileAttributesW(wszSdbPath) != -1 &&
  2822. bIsAlreadyInstalled(wszSdbPath)) {
  2823. //
  2824. // they asked us to install, it's installed, so we're done
  2825. //
  2826. vPrintMessage(IDS_ALREADY_INSTALLED, wszSdbPath);
  2827. goto quickOut;
  2828. }
  2829. if (eMode == MODE_UNINSTALL && GetFileAttributesW(wszSdbPath) == -1) {
  2830. //
  2831. // they asked us to uninstall, it's not installed, so we're done
  2832. //
  2833. vPrintMessage(IDS_NOT_INSTALLED, wszSdbName);
  2834. goto quickOut;
  2835. }
  2836. if (eMode == MODE_INSTALL && DatabaseContainsPatch(wszSdbPath) && !g_bAllowPatches) {
  2837. //
  2838. // we can't install because the SDB contains a patch and the user hasn't authorized it.
  2839. //
  2840. vPrintMessage(IDS_NO_PATCHES_ALLOWED);
  2841. goto quickOut;
  2842. }
  2843. if (eMode == MODE_INSTALL && bOldSdbInstalled(wszSdbPath, wszOldSdbPath, ARRAYSIZE(wszOldSdbPath))) {
  2844. //
  2845. // we should ask if we're going to uninstall the old one,
  2846. // unless we're in quiet mode.
  2847. //
  2848. int nRet;
  2849. WCHAR wszCaption[1024];
  2850. WCHAR wszText[1024];
  2851. if (g_bQuiet) {
  2852. nRet = IDYES;
  2853. } else {
  2854. if (!LoadStringW(g_hInst, IDS_APP_TITLE, wszCaption, ARRAYSIZE(wszCaption))) {
  2855. return 1;
  2856. }
  2857. if (!LoadStringW(g_hInst, IDS_FOUND_SAME_ID, wszText, ARRAYSIZE(wszText))) {
  2858. return 1;
  2859. }
  2860. nRet = MessageBoxW(NULL,
  2861. wszText,
  2862. wszCaption,
  2863. MB_YESNO | MB_ICONQUESTION);
  2864. }
  2865. if (nRet == IDNO) {
  2866. return 0;
  2867. } else if (nRet == IDYES) {
  2868. if (!bHandleInstall(wszOldSdbPath, MODE_UNINSTALL, wszSdbInstallPath, ARRAYSIZE(wszSdbInstallPath))) {
  2869. vPrintError(IDS_FAILED_UNINSTALL);
  2870. return 1;
  2871. }
  2872. }
  2873. }
  2874. wszSdbInstallPath[0] = 0;
  2875. if (!bHandleInstall(wszSdbPath, eMode, wszSdbInstallPath, ARRAYSIZE(wszSdbInstallPath))) {
  2876. if (eMode == MODE_INSTALL) {
  2877. //
  2878. // we need to clean up; the install failed.
  2879. //
  2880. g_bQuiet = TRUE;
  2881. bHandleInstall(wszSdbPath, MODE_CLEANUP, wszSdbInstallPath, ARRAYSIZE(wszSdbInstallPath));
  2882. }
  2883. nReturn = 1;
  2884. }
  2885. //
  2886. // no matter what happens, flush the cache
  2887. //
  2888. vFlushCache();
  2889. quickOut:
  2890. return nReturn;
  2891. }