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.

3136 lines
88 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1996 - 1999
  6. //
  7. // File: catadnew.cpp
  8. //
  9. // Contents: Microsoft Internet Security Catalog Utilities
  10. //
  11. // Functions: CryptCATAdminAcquireContext
  12. // CryptCATAdminReleaseContext
  13. // CryptCATAdminAddCatalog
  14. // CryptCATAdminRemoveCatalog
  15. // CryptCATAdminEnumCatalogFromHash
  16. // CryptCATCatalogInfoFromContext
  17. // CryptCATAdminReleaseCatalogContext
  18. // CryptCATAdminResolveCatalogPath
  19. // CryptCATAdminPauseServiceForBackup
  20. // CryptCATAdminCalcHashFromFileHandle
  21. // I_CryptCatAdminMigrateToNewCatDB
  22. // CatAdminDllMain
  23. //
  24. // History: 01-Jan-2000 reidk created
  25. //
  26. //--------------------------------------------------------------------------
  27. #include "global.hxx"
  28. #include "cryptreg.h"
  29. #include "wintrust.h"
  30. #include "softpub.h"
  31. #include "eventlst.h"
  32. #include "sipguids.h"
  33. #include "mscat32.h"
  34. #include "catdb.h"
  35. #include "voidlist.h"
  36. #include "catutil.h"
  37. #include "..\..\common\catdbsvc\catdbcli.h"
  38. #include "errlog.h"
  39. #define MAX_HASH_LEN 20
  40. //
  41. // default system guid for apps that just make calls to CryptCATAdminAddCatalog with
  42. // hCatAdmin == NULL...
  43. //
  44. // {127D0A1D-4EF2-11d1-8608-00C04FC295EE}
  45. //
  46. #define DEF_CAT_SUBSYS_ID \
  47. { \
  48. 0x127d0a1d, \
  49. 0x4ef2, \
  50. 0x11d1, \
  51. { 0x86, 0x8, 0x0, 0xc0, 0x4f, 0xc2, 0x95, 0xee } \
  52. }
  53. #define WSZ_CATALOG_FILE_BASE_DIRECTORY L"CatRoot"
  54. #define WSZ_DATABASE_FILE_BASE_DIRECTORY L"CatRoot2"
  55. #define WSZ_REG_FILES_NOT_TO_BACKUP L"System\\CurrentControlSet\\Control\\BackupRestore\\FilesNotToBackup"
  56. #define WSZ_REG_CATALOG_DATABASE_VALUE L"Catalog Database"
  57. #define WSZ_PATH_NOT_TO_BACKUP L"%SystemRoot%\\System32\\CatRoot2\\* /s\0"
  58. static WCHAR *gpwszDatabaseFileBaseDirectory = NULL;
  59. static WCHAR *gpwszCatalogFileBaseDirectory = NULL;
  60. #define WSZ_CATALOG_SUBSYTEM_SEARCH_STRING L"{????????????????????????????????????}"
  61. #define CATADMIN_LOGERR_LASTERR() ErrLog_LogError(NULL, \
  62. ERRLOG_CLIENT_ID_CATADMIN, \
  63. __LINE__, \
  64. 0, \
  65. FALSE, \
  66. FALSE);
  67. #define CATADMIN_SETERR_LOG_RETURN(x, y) SetLastError(x); \
  68. ErrLog_LogError(NULL, \
  69. ERRLOG_CLIENT_ID_CATADMIN, \
  70. __LINE__, \
  71. 0, \
  72. FALSE, \
  73. FALSE); \
  74. goto y;
  75. typedef struct CATALOG_INFO_CONTEXT_
  76. {
  77. HANDLE hMappedFile;
  78. BYTE *pbMappedFile;
  79. WCHAR *pwszCatalogFile;
  80. PCCTL_CONTEXT pCTLContext;
  81. BOOL fResultOfAdd;
  82. } CATALOG_INFO_CONTEXT;
  83. typedef struct CRYPT_CAT_ADMIN_
  84. {
  85. DWORD cbStruct;
  86. BOOL fUseDefSubSysId;
  87. LPWSTR pwszSubSysGUID;
  88. LPWSTR pwszCatalogFileDir; // full path to .cat files
  89. LPWSTR pwszDatabaseFileDir; // full path to CatDB file
  90. DWORD dwLastDBError;
  91. LIST CatalogInfoContextList;
  92. int nOpenCatInfoContexts;
  93. CRITICAL_SECTION CriticalSection;
  94. BOOL fCSInitialized;
  95. BOOL fCSEntered;
  96. HANDLE hClearCacheEvent;
  97. HANDLE hRegisterWaitForClearCache;
  98. BOOL fRegisteredForChangeNotification;
  99. } CRYPT_CAT_ADMIN;
  100. #define CATINFO_CONTEXT_ALLOCATION_SIZE 64
  101. LPWSTR ppwszFilesToDelete[] = {L"hashmast.cbd",
  102. L"hashmast.cbk",
  103. L"catmast.cbd",
  104. L"catmast.cbk",
  105. L"sysmast.cbd",
  106. L"sysmast.cbk"};
  107. #define NUM_FILES_TO_DELETE (sizeof(ppwszFilesToDelete) / \
  108. sizeof(ppwszFilesToDelete[0]))
  109. BOOL
  110. _CatAdminMigrateSingleDatabase(
  111. LPWSTR pwszDatabaseGUID);
  112. BOOL
  113. _CatAdminSetupDefaults(void);
  114. void
  115. _CatAdminCleanupDefaults(void);
  116. BOOL
  117. _CatAdminTimeStampFilesInSync(
  118. LPWSTR pwszDatabaseGUID,
  119. BOOL *pfInSync);
  120. BOOL
  121. _CatAdminRegisterForChangeNotification(
  122. CRYPT_CAT_ADMIN *pCatAdmin
  123. );
  124. BOOL
  125. _CatAdminFreeCachedCatalogs(
  126. CRYPT_CAT_ADMIN *pCatAdmin
  127. );
  128. VOID CALLBACK
  129. _CatAdminWaitOrTimerCallback(
  130. PVOID lpParameter,
  131. BOOLEAN TimerOrWaitFired
  132. );
  133. BOOL
  134. _CatAdminAddCatalogsToCache(
  135. CRYPT_CAT_ADMIN *pCatAdmin,
  136. LPWSTR pwszSubSysGUID,
  137. CRYPT_DATA_BLOB *pCryptDataBlob,
  138. LIST_NODE **ppFirstListNodeAdded
  139. );
  140. BOOL
  141. _CatAdminAddSingleCatalogToCache(
  142. CRYPT_CAT_ADMIN *pCatAdmin,
  143. LPWSTR pwszCatalog,
  144. LIST_NODE **ppListNodeAdded
  145. );
  146. BOOL
  147. _CatAdminMigrateCatalogDatabase(
  148. LPWSTR pwszFrom,
  149. LPWSTR pwszTo
  150. );
  151. void
  152. _CatAdminBToHex (
  153. LPBYTE pbDigest,
  154. DWORD iByte,
  155. LPWSTR pwszHashTag
  156. );
  157. BOOL
  158. _CatAdminCreateHashTag(
  159. BYTE *pbHash,
  160. DWORD cbHash,
  161. LPWSTR *ppwszHashTag,
  162. CRYPT_DATA_BLOB *pCryptDataBlob
  163. );
  164. BOOL
  165. _CatAdminRecursiveCreateDirectory(
  166. IN LPCWSTR pwszDir,
  167. LPSECURITY_ATTRIBUTES lpSecurityAttributes
  168. );
  169. LPWSTR
  170. _CatAdminCreatePath(
  171. IN LPCWSTR pwsz1,
  172. IN LPCWSTR pwsz2,
  173. IN BOOL fAddEndingSlash
  174. );
  175. void __RPC_FAR * __RPC_API MIDL_user_allocate(size_t len)
  176. {
  177. return(LocalAlloc(LMEM_ZEROINIT, len));
  178. }
  179. void __RPC_API MIDL_user_free(void __RPC_FAR * ptr)
  180. {
  181. if (ptr != NULL)
  182. {
  183. LocalFree(ptr);
  184. }
  185. }
  186. //---------------------------------------------------------------------------------------
  187. //
  188. // CryptCATAdminAcquireContext
  189. //
  190. //---------------------------------------------------------------------------------------
  191. BOOL WINAPI
  192. CryptCATAdminAcquireContext_Internal(
  193. HCATADMIN *phCatAdmin,
  194. const GUID *pgSubsystem,
  195. DWORD dwFlags,
  196. BOOL fCalledFromMigrate)
  197. {
  198. GUID gDefault = DEF_CAT_SUBSYS_ID;
  199. const GUID *pgCatroot = &gDefault;
  200. CRYPT_CAT_ADMIN *pCatAdmin = NULL;
  201. BOOL fRet = TRUE;
  202. DWORD dwErr = 0;
  203. WCHAR wszGUID[256];
  204. BOOL fInSync;
  205. //
  206. // Validata parameters
  207. //
  208. if (phCatAdmin == NULL)
  209. {
  210. CATADMIN_SETERR_LOG_RETURN(ERROR_INVALID_PARAMETER, ErrorInvalidParam)
  211. }
  212. *phCatAdmin = NULL;
  213. //
  214. // Allocate a new CatAdmin state struct
  215. //
  216. if (NULL == (pCatAdmin = (CRYPT_CAT_ADMIN *) malloc(sizeof(CRYPT_CAT_ADMIN))))
  217. {
  218. CATADMIN_SETERR_LOG_RETURN(ERROR_NOT_ENOUGH_MEMORY, ErrorMemory)
  219. }
  220. memset(pCatAdmin, 0, sizeof(CRYPT_CAT_ADMIN));
  221. pCatAdmin->cbStruct = sizeof(CRYPT_CAT_ADMIN);
  222. LIST_Initialize(&(pCatAdmin->CatalogInfoContextList));
  223. //
  224. // Check to see if caller specified the Catroot dir to use
  225. //
  226. if (pgSubsystem == NULL)
  227. {
  228. pCatAdmin->fUseDefSubSysId = TRUE;
  229. }
  230. else
  231. {
  232. pgCatroot = pgSubsystem;
  233. }
  234. guid2wstr(pgCatroot, wszGUID);
  235. //
  236. // Initialize the critical section
  237. //
  238. __try
  239. {
  240. InitializeCriticalSection(&(pCatAdmin->CriticalSection));
  241. }
  242. __except(EXCEPTION_EXECUTE_HANDLER)
  243. {
  244. SetLastError(GetExceptionCode());
  245. CATADMIN_LOGERR_LASTERR()
  246. goto ErrorReturn;
  247. }
  248. pCatAdmin->fCSInitialized = TRUE;
  249. pCatAdmin->fCSEntered = FALSE;
  250. //
  251. // Save a copy of the GUID as a string
  252. //
  253. if (NULL == (pCatAdmin->pwszSubSysGUID = (LPWSTR)
  254. malloc((wcslen(wszGUID) + 1) * sizeof(WCHAR))))
  255. {
  256. CATADMIN_SETERR_LOG_RETURN(ERROR_NOT_ENOUGH_MEMORY, ErrorMemory)
  257. }
  258. wcscpy(pCatAdmin->pwszSubSysGUID, wszGUID);
  259. //
  260. // Get the complete paths for the catalog files and the db file
  261. //
  262. if (NULL == (pCatAdmin->pwszCatalogFileDir = _CatAdminCreatePath(
  263. gpwszCatalogFileBaseDirectory,
  264. wszGUID,
  265. TRUE)))
  266. {
  267. CATADMIN_LOGERR_LASTERR()
  268. goto ErrorReturn;
  269. }
  270. if (NULL == (pCatAdmin->pwszDatabaseFileDir = _CatAdminCreatePath(
  271. gpwszDatabaseFileBaseDirectory,
  272. wszGUID,
  273. TRUE)))
  274. {
  275. CATADMIN_LOGERR_LASTERR()
  276. goto ErrorReturn;
  277. }
  278. //
  279. // Make sure catalog file and database file sub-directories exists
  280. //
  281. if (!_CatAdminRecursiveCreateDirectory(
  282. pCatAdmin->pwszCatalogFileDir,
  283. NULL))
  284. {
  285. CATADMIN_LOGERR_LASTERR()
  286. goto ErrorReturn;
  287. }
  288. if (!_CatAdminRecursiveCreateDirectory(
  289. pCatAdmin->pwszDatabaseFileDir,
  290. NULL))
  291. {
  292. CATADMIN_LOGERR_LASTERR()
  293. goto ErrorReturn;
  294. }
  295. //
  296. // Create the event which is notified when the catalog db changes, and register
  297. // a callback for when the event is signaled
  298. //
  299. if (NULL == (pCatAdmin->hClearCacheEvent = CreateEvent(NULL, FALSE, FALSE, NULL)))
  300. {
  301. CATADMIN_LOGERR_LASTERR()
  302. goto ErrorCreateEvent;
  303. }
  304. if (!RegisterWaitForSingleObject(
  305. &(pCatAdmin->hRegisterWaitForClearCache),
  306. pCatAdmin->hClearCacheEvent,
  307. _CatAdminWaitOrTimerCallback,
  308. pCatAdmin,
  309. INFINITE,
  310. WT_TRANSFER_IMPERSONATION))
  311. {
  312. CATADMIN_LOGERR_LASTERR()
  313. goto ErrorRegisterWaitForSingleObject;
  314. }
  315. //
  316. // If we are being called by a real client (not the migrate code) then make sure
  317. // the TimeStamp files are in a consistent state, and if not, migrate (re-add)
  318. // the catalog files for that database
  319. //
  320. if (!fCalledFromMigrate)
  321. {
  322. if (_CatAdminTimeStampFilesInSync(wszGUID, &fInSync))
  323. {
  324. if (!fInSync)
  325. {
  326. //
  327. // FIX FIX - may need to migrate
  328. // all DBs if the wszGUID is DEF_CAT_SUBSYS_ID
  329. //
  330. if (!_CatAdminMigrateSingleDatabase(wszGUID))
  331. {
  332. CATADMIN_LOGERR_LASTERR()
  333. goto ErrorReturn;
  334. }
  335. }
  336. }
  337. else
  338. {
  339. CATADMIN_LOGERR_LASTERR()
  340. goto ErrorReturn;
  341. }
  342. }
  343. //
  344. // NOTE:
  345. // Defer registering with the service for the change notificatation so we
  346. // don't rely on the service during an acquire context
  347. //
  348. *phCatAdmin = (HCATADMIN)pCatAdmin;
  349. CommonReturn:
  350. return(fRet);
  351. ErrorReturn:
  352. if (pCatAdmin != NULL)
  353. {
  354. dwErr = GetLastError();
  355. if (pCatAdmin->hRegisterWaitForClearCache != NULL)
  356. {
  357. UnregisterWaitEx(
  358. pCatAdmin->hRegisterWaitForClearCache,
  359. INVALID_HANDLE_VALUE);
  360. }
  361. // call UnregisterWaitEx before deteling the critical section
  362. // because the cb thread tries to enter it
  363. if (pCatAdmin->fCSInitialized)
  364. {
  365. DeleteCriticalSection(&(pCatAdmin->CriticalSection));
  366. }
  367. if (pCatAdmin->hClearCacheEvent != NULL)
  368. {
  369. CloseHandle(pCatAdmin->hClearCacheEvent);
  370. }
  371. if (pCatAdmin->pwszSubSysGUID != NULL)
  372. {
  373. free(pCatAdmin->pwszSubSysGUID);
  374. }
  375. if (pCatAdmin->pwszCatalogFileDir != NULL)
  376. {
  377. free(pCatAdmin->pwszCatalogFileDir);
  378. }
  379. if (pCatAdmin->pwszDatabaseFileDir != NULL)
  380. {
  381. free(pCatAdmin->pwszDatabaseFileDir);
  382. }
  383. free(pCatAdmin);
  384. SetLastError(dwErr);
  385. }
  386. fRet = FALSE;
  387. goto CommonReturn;
  388. TRACE_ERROR_EX(DBG_SS_TRUST, ErrorMemory)
  389. TRACE_ERROR_EX(DBG_SS_TRUST, ErrorInvalidParam)
  390. TRACE_ERROR_EX(DBG_SS_TRUST, ErrorRegisterWaitForSingleObject)
  391. TRACE_ERROR_EX(DBG_SS_TRUST, ErrorCreateEvent)
  392. }
  393. BOOL WINAPI
  394. CryptCATAdminAcquireContext(
  395. OUT HCATADMIN *phCatAdmin,
  396. IN const GUID *pgSubsystem,
  397. IN DWORD dwFlags)
  398. {
  399. return (CryptCATAdminAcquireContext_Internal(
  400. phCatAdmin,
  401. pgSubsystem,
  402. dwFlags,
  403. FALSE));
  404. }
  405. //---------------------------------------------------------------------------------------
  406. //
  407. // CryptCATAdminReleaseContext
  408. //
  409. //---------------------------------------------------------------------------------------
  410. BOOL WINAPI
  411. CryptCATAdminReleaseContext(
  412. IN HCATADMIN hCatAdmin,
  413. IN DWORD dwFlags)
  414. {
  415. CRYPT_CAT_ADMIN *pCatAdmin = (CRYPT_CAT_ADMIN *)hCatAdmin;
  416. BOOL fRet = TRUE;
  417. //
  418. // Validate input params
  419. //
  420. if ((pCatAdmin == NULL) ||
  421. (pCatAdmin->cbStruct != sizeof(CRYPT_CAT_ADMIN)))
  422. {
  423. CATADMIN_SETERR_LOG_RETURN(ERROR_INVALID_PARAMETER, ErrorInvalidParam)
  424. }
  425. //
  426. // Un-Register for change notifications from DB process
  427. //
  428. // This needs to happen first thing, so that no callbacks
  429. // happen during cleanup
  430. //
  431. if (pCatAdmin->fRegisteredForChangeNotification)
  432. {
  433. Client_SSCatDBRegisterForChangeNotification(
  434. (DWORD_PTR) pCatAdmin->hClearCacheEvent,
  435. 0,
  436. pCatAdmin->pwszSubSysGUID,
  437. TRUE);
  438. }
  439. UnregisterWaitEx(pCatAdmin->hRegisterWaitForClearCache, INVALID_HANDLE_VALUE);
  440. CloseHandle(pCatAdmin->hClearCacheEvent);
  441. _CatAdminFreeCachedCatalogs(pCatAdmin);
  442. free(pCatAdmin->pwszSubSysGUID);
  443. free(pCatAdmin->pwszCatalogFileDir);
  444. free(pCatAdmin->pwszDatabaseFileDir);
  445. DeleteCriticalSection(&(pCatAdmin->CriticalSection));
  446. free(pCatAdmin);
  447. CommonReturn:
  448. return(fRet);
  449. ErrorReturn:
  450. fRet = FALSE;
  451. goto CommonReturn;
  452. TRACE_ERROR_EX(DBG_SS_TRUST, ErrorInvalidParam)
  453. }
  454. //---------------------------------------------------------------------------------------
  455. //
  456. // CryptCATAdminAddCatalog
  457. //
  458. //---------------------------------------------------------------------------------------
  459. HCATINFO WINAPI
  460. CryptCATAdminAddCatalog(
  461. IN HCATADMIN hCatAdmin,
  462. IN WCHAR *pwszCatalogFile,
  463. IN WCHAR *pwszSelectBaseName,
  464. IN DWORD dwFlags)
  465. {
  466. CRYPT_CAT_ADMIN *pCatAdmin = (CRYPT_CAT_ADMIN *)hCatAdmin;
  467. CATALOG_INFO_CONTEXT *pCatInfoContext = NULL;
  468. DWORD dwErr = 0;
  469. LPWSTR pwszCatalogNameUsed = NULL;
  470. LPWSTR pwszCatalogNameUsedCopy = NULL;
  471. LPWSTR pwszFullyQualifiedCatalogFile = NULL;
  472. DWORD dwLength = 0;
  473. LIST_NODE *pListNode = NULL;
  474. WCHAR wszTmp[1];
  475. if ((pCatAdmin == NULL) ||
  476. (pCatAdmin->cbStruct != sizeof(CRYPT_CAT_ADMIN)) ||
  477. (pwszCatalogFile == NULL) ||
  478. (dwFlags != 0))
  479. {
  480. CATADMIN_SETERR_LOG_RETURN(ERROR_INVALID_PARAMETER, ErrorInvalidParam)
  481. }
  482. ErrLog_LogString(NULL, L"Adding Catalog File: ", pwszSelectBaseName, TRUE);
  483. //
  484. // first, check the catalog...
  485. //
  486. if (!(IsCatalogFile(INVALID_HANDLE_VALUE, pwszCatalogFile)))
  487. {
  488. if (GetLastError() == ERROR_FILE_NOT_FOUND)
  489. {
  490. CATADMIN_LOGERR_LASTERR()
  491. goto ErrorReturn;
  492. }
  493. CATADMIN_SETERR_LOG_RETURN(ERROR_BAD_FORMAT, ErrorBadFileFormat)
  494. }
  495. EnterCriticalSection(&(pCatAdmin->CriticalSection));
  496. pCatAdmin->fCSEntered = TRUE;
  497. if (!_CatAdminRegisterForChangeNotification(pCatAdmin))
  498. {
  499. CATADMIN_LOGERR_LASTERR()
  500. goto ErrorReturn;
  501. }
  502. //
  503. // Clear the cache, since doing the add may change things
  504. //
  505. _CatAdminFreeCachedCatalogs(pCatAdmin);
  506. //
  507. // If the file name specified by pwszCatalogFile is not a fully qualified
  508. // path name, we need to build one before calling the service.
  509. //
  510. if ((wcschr(pwszCatalogFile, L'\\') == NULL) &&
  511. (wcschr(pwszCatalogFile, L':') == NULL))
  512. {
  513. dwLength = GetCurrentDirectoryW(1, wszTmp) * sizeof(WCHAR);
  514. if (dwLength == 0)
  515. {
  516. CATADMIN_LOGERR_LASTERR()
  517. goto ErrorReturn;
  518. }
  519. dwLength += (wcslen(pwszCatalogFile) + 1) * sizeof(WCHAR);
  520. if (NULL == (pwszFullyQualifiedCatalogFile = (LPWSTR) malloc(dwLength)))
  521. {
  522. CATADMIN_SETERR_LOG_RETURN(ERROR_NOT_ENOUGH_MEMORY, ErrorMemory)
  523. }
  524. if (!GetCurrentDirectoryW(
  525. dwLength / sizeof(WCHAR),
  526. pwszFullyQualifiedCatalogFile))
  527. {
  528. CATADMIN_LOGERR_LASTERR()
  529. goto ErrorReturn;
  530. }
  531. if ((pwszFullyQualifiedCatalogFile[wcslen(pwszFullyQualifiedCatalogFile) - 1]
  532. != L'\\'))
  533. {
  534. wcscat(pwszFullyQualifiedCatalogFile, L"\\");
  535. }
  536. wcscat(pwszFullyQualifiedCatalogFile, pwszCatalogFile);
  537. }
  538. //
  539. // Call the DB process to add the catalog
  540. //
  541. if (0 != (dwErr = Client_SSCatDBAddCatalog(
  542. 0,
  543. pCatAdmin->pwszSubSysGUID,
  544. (pwszFullyQualifiedCatalogFile != NULL) ?
  545. pwszFullyQualifiedCatalogFile :
  546. pwszCatalogFile,
  547. pwszSelectBaseName,
  548. &pwszCatalogNameUsed)))
  549. {
  550. CATADMIN_SETERR_LOG_RETURN(dwErr, ErrorCatDBProcess)
  551. }
  552. //
  553. // Touch the TimeStamp file
  554. //
  555. TimeStampFile_Touch(pCatAdmin->pwszCatalogFileDir);
  556. //
  557. // create a psuedo list entry, that really isn't part of the list...
  558. // this is so the caller can call CryptCATCatalogInfoFromContext
  559. //
  560. if (NULL == (pwszCatalogNameUsedCopy = (LPWSTR)
  561. malloc((wcslen(pwszCatalogNameUsed) + 1) * sizeof(WCHAR))))
  562. {
  563. CATADMIN_SETERR_LOG_RETURN(ERROR_NOT_ENOUGH_MEMORY, ErrorMemory)
  564. }
  565. wcscpy(pwszCatalogNameUsedCopy, pwszCatalogNameUsed);
  566. if (NULL == (pCatInfoContext = (CATALOG_INFO_CONTEXT *)
  567. malloc(sizeof(CATALOG_INFO_CONTEXT))))
  568. {
  569. CATADMIN_SETERR_LOG_RETURN(ERROR_NOT_ENOUGH_MEMORY, ErrorMemory)
  570. }
  571. memset(pCatInfoContext, 0, sizeof(CATALOG_INFO_CONTEXT));
  572. pCatInfoContext->pwszCatalogFile = pwszCatalogNameUsedCopy;
  573. pCatInfoContext->fResultOfAdd = TRUE;
  574. if (NULL == (pListNode = (LIST_NODE *) malloc(sizeof(LIST_NODE))))
  575. {
  576. CATADMIN_SETERR_LOG_RETURN(ERROR_NOT_ENOUGH_MEMORY, ErrorMemory)
  577. }
  578. memset(pListNode, 0, sizeof(LIST_NODE));
  579. pListNode->pElement = pCatInfoContext;
  580. CommonReturn:
  581. MIDL_user_free(pwszCatalogNameUsed);
  582. if (pwszFullyQualifiedCatalogFile != NULL)
  583. {
  584. free(pwszFullyQualifiedCatalogFile);
  585. }
  586. if ((pCatAdmin != NULL) &&
  587. (pCatAdmin->fCSEntered))
  588. {
  589. pCatAdmin->fCSEntered = FALSE;
  590. LeaveCriticalSection(&(pCatAdmin->CriticalSection));
  591. }
  592. ErrLog_LogString(NULL, L"DONE Adding Catalog File: ", pwszSelectBaseName, TRUE);
  593. return((HCATINFO) pListNode);
  594. ErrorReturn:
  595. if (pwszCatalogNameUsedCopy != NULL)
  596. {
  597. free(pwszCatalogNameUsedCopy);
  598. }
  599. if (pCatInfoContext != NULL)
  600. {
  601. free(pCatInfoContext);
  602. }
  603. goto CommonReturn;
  604. TRACE_ERROR_EX(DBG_SS_TRUST, ErrorInvalidParam)
  605. TRACE_ERROR_EX(DBG_SS_TRUST, ErrorBadFileFormat)
  606. TRACE_ERROR_EX(DBG_SS_TRUST, ErrorCatDBProcess)
  607. TRACE_ERROR_EX(DBG_SS_TRUST, ErrorMemory)
  608. }
  609. //---------------------------------------------------------------------------------------
  610. //
  611. // CryptCATAdminRemoveCatalog
  612. //
  613. //---------------------------------------------------------------------------------------
  614. BOOL WINAPI
  615. CryptCATAdminRemoveCatalog(
  616. IN HCATADMIN hCatAdmin,
  617. IN LPCWSTR pwszCatalogFile,
  618. IN DWORD dwFlags)
  619. {
  620. BOOL fRet = TRUE;
  621. DWORD dwErr = 0;
  622. CRYPT_CAT_ADMIN *pCatAdmin = (CRYPT_CAT_ADMIN *)hCatAdmin;
  623. //
  624. // Call the DB process to delete the catalog
  625. //
  626. if (0 != (dwErr = Client_SSCatDBDeleteCatalog(
  627. 0,
  628. pCatAdmin->pwszSubSysGUID,
  629. pwszCatalogFile)))
  630. {
  631. CATADMIN_SETERR_LOG_RETURN(dwErr, ErrorCatDBProcess)
  632. }
  633. //
  634. // Touch the TimeStamp file
  635. //
  636. TimeStampFile_Touch(pCatAdmin->pwszCatalogFileDir);
  637. CommonReturn:
  638. return(fRet);
  639. ErrorReturn:
  640. fRet = FALSE;
  641. goto CommonReturn;
  642. TRACE_ERROR_EX(DBG_SS_TRUST, ErrorCatDBProcess)
  643. }
  644. //---------------------------------------------------------------------------------------
  645. //
  646. // CryptCATAdminEnumCatalogFromHash
  647. //
  648. //---------------------------------------------------------------------------------------
  649. HCATINFO WINAPI
  650. CryptCATAdminEnumCatalogFromHash(
  651. IN HCATADMIN hCatAdmin,
  652. IN BYTE *pbHash,
  653. IN DWORD cbHash,
  654. IN DWORD dwFlags,
  655. IN HCATINFO *phPrevCatInfo)
  656. {
  657. CRYPT_CAT_ADMIN *pCatAdmin = (CRYPT_CAT_ADMIN *)hCatAdmin;
  658. BOOL fFindFirstOnly;
  659. CRYPT_DATA_BLOB CryptDataBlobHash;
  660. CRYPT_DATA_BLOB CryptDataBlobHashTag;
  661. LPWSTR pwszSearch = NULL;
  662. HANDLE hFindHandle = INVALID_HANDLE_VALUE;
  663. WIN32_FIND_DATAW FindData;
  664. LPWSTR pwszHashTag = NULL;
  665. DWORD dwErr = 0;
  666. LIST_NODE *pPrevListNode = NULL;
  667. LIST_NODE *pListNodeToReturn = NULL;
  668. LIST_NODE *pListNode = NULL;
  669. CATALOG_INFO_CONTEXT *pCatInfoContext = NULL;
  670. //
  671. // Validate input params
  672. //
  673. if ((pCatAdmin == NULL) ||
  674. (pCatAdmin->cbStruct != sizeof(CRYPT_CAT_ADMIN)) ||
  675. (cbHash == 0) ||
  676. (cbHash > MAX_HASH_LEN) ||
  677. (dwFlags != 0))
  678. {
  679. CATADMIN_SETERR_LOG_RETURN(ERROR_INVALID_PARAMETER, ErrorInvalidParam)
  680. }
  681. if (!_CatAdminRegisterForChangeNotification(pCatAdmin))
  682. {
  683. CATADMIN_LOGERR_LASTERR()
  684. goto ErrorReturn;
  685. }
  686. //
  687. // If phPrevCatInfo is NULL then that means the caller is only interested
  688. // in the first catalog that contains the hash, thus no enum state is
  689. // started. If phPrevCatInfo is non NULL, then it contains NULL, or a
  690. // HCATINFO that was returned from a previous call to
  691. // CryptCATAdminEnumCatalogFromHash. If it contains NULL, then this is
  692. // the start of an enum, otherwise it is enuming the next catalog containing
  693. // the hash.
  694. //
  695. if (phPrevCatInfo == NULL)
  696. {
  697. fFindFirstOnly = TRUE;
  698. }
  699. else
  700. {
  701. fFindFirstOnly = FALSE;
  702. pPrevListNode = (LIST_NODE *) *phPrevCatInfo;
  703. }
  704. //
  705. // Only allow one thread to view/modify at a time
  706. //
  707. EnterCriticalSection(&(pCatAdmin->CriticalSection));
  708. pCatAdmin->fCSEntered = TRUE;
  709. __try
  710. {
  711. //
  712. // This data blob is used to do the find in the database
  713. //
  714. CryptDataBlobHash.pbData = pbHash;
  715. CryptDataBlobHash.cbData = cbHash;
  716. //
  717. // Create the tag to be used for calls to CertFindSubjectInSortedCTL
  718. //
  719. if (!_CatAdminCreateHashTag(pbHash, cbHash, &pwszHashTag, &CryptDataBlobHashTag))
  720. {
  721. CATADMIN_LOGERR_LASTERR()
  722. goto ErrorReturn;
  723. }
  724. //
  725. // The enum works as follows:
  726. //
  727. // if enum-state is not being initialized OR this is the first call to start an enum
  728. //
  729. // loop through all currently cached catalogs until a catalog containing the
  730. // the hash is found, and return it
  731. //
  732. // if a catalog was not found in the cache, then call the DB process to try and
  733. // find one
  734. //
  735. // else (enum state has already been started)
  736. //
  737. // loop through currently cached catalogs, starting with the catalog just after
  738. // the current catalog, and until a catalog containing the hash is found
  739. //
  740. if ((fFindFirstOnly) || (pPrevListNode == NULL))
  741. {
  742. pListNode = LIST_GetFirst(&(pCatAdmin->CatalogInfoContextList));
  743. while (pListNode != NULL)
  744. {
  745. pCatInfoContext = (CATALOG_INFO_CONTEXT *) LIST_GetElement(pListNode);
  746. if (CertFindSubjectInSortedCTL(
  747. &CryptDataBlobHashTag,
  748. pCatInfoContext->pCTLContext,
  749. NULL,
  750. NULL,
  751. NULL))
  752. {
  753. pListNodeToReturn = pListNode;
  754. goto CommonReturn;
  755. }
  756. pListNode = LIST_GetNext(pListNode);
  757. }
  758. //
  759. // If we are here, that means we did not find a cached catalog that contained
  760. // the hash, so call the DB process to try and find one or more.
  761. //
  762. // Call the DB process once if we are not using the default sub-system ID,
  763. // otherwise call the DB process once for each sub-system.
  764. if (!pCatAdmin->fUseDefSubSysId)
  765. {
  766. if (_CatAdminAddCatalogsToCache(
  767. pCatAdmin,
  768. pCatAdmin->pwszSubSysGUID,
  769. &CryptDataBlobHash,
  770. &pListNodeToReturn))
  771. {
  772. if (pListNodeToReturn == NULL)
  773. {
  774. SetLastError(ERROR_NOT_FOUND);
  775. //CATADMIN_LOGERR_LASTERR()
  776. goto CatNotFound;
  777. }
  778. goto CommonReturn;
  779. }
  780. else
  781. {
  782. CATADMIN_LOGERR_LASTERR()
  783. goto ErrorReturn;
  784. }
  785. }
  786. else
  787. {
  788. //
  789. // For each subdir, add all the catalogs that contain the hash
  790. //
  791. //
  792. // Create search string to find all subdirs
  793. //
  794. if (NULL == (pwszSearch = _CatAdminCreatePath(
  795. gpwszDatabaseFileBaseDirectory,
  796. WSZ_CATALOG_SUBSYTEM_SEARCH_STRING,
  797. FALSE)))
  798. {
  799. CATADMIN_LOGERR_LASTERR()
  800. goto ErrorReturn;
  801. }
  802. //
  803. // Do the initial find
  804. //
  805. hFindHandle = FindFirstFileU(pwszSearch, &FindData);
  806. if (hFindHandle == INVALID_HANDLE_VALUE)
  807. {
  808. dwErr = GetLastError();
  809. //
  810. // no sub dirs found
  811. //
  812. if ((dwErr == ERROR_NO_MORE_FILES) ||
  813. (dwErr == ERROR_PATH_NOT_FOUND) ||
  814. (dwErr == ERROR_FILE_NOT_FOUND))
  815. {
  816. CATADMIN_SETERR_LOG_RETURN(ERROR_NOT_FOUND, CatNotFound)
  817. }
  818. else
  819. {
  820. goto ErrorFindFirstFile;
  821. }
  822. }
  823. while (1)
  824. {
  825. //
  826. // Only care about directories
  827. //
  828. if (FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
  829. {
  830. //
  831. // Add all the catalogs in this subdir that contain the hash to
  832. // the catalog cache
  833. //
  834. if (!_CatAdminAddCatalogsToCache(
  835. pCatAdmin,
  836. FindData.cFileName,
  837. &CryptDataBlobHash,
  838. (pListNodeToReturn == NULL) ?
  839. &pListNodeToReturn : NULL))
  840. {
  841. CATADMIN_LOGERR_LASTERR()
  842. goto ErrorReturn;
  843. }
  844. }
  845. //
  846. // Get next subdir
  847. //
  848. if (!FindNextFileU(hFindHandle, &FindData))
  849. {
  850. if (GetLastError() == ERROR_NO_MORE_FILES)
  851. {
  852. break;
  853. }
  854. else
  855. {
  856. goto ErrorFindNextFile;
  857. }
  858. }
  859. }
  860. if (pListNodeToReturn == NULL)
  861. {
  862. SetLastError(ERROR_NOT_FOUND);
  863. //CATADMIN_LOGERR_LASTERR()
  864. goto CatNotFound;
  865. }
  866. }
  867. }
  868. else
  869. {
  870. //
  871. // Enum state already started, so just search through the rest of the cached
  872. // catalogs to try and find one that contains the hash
  873. //
  874. pListNode = LIST_GetNext(pPrevListNode);
  875. while (pListNode != NULL)
  876. {
  877. pCatInfoContext = (CATALOG_INFO_CONTEXT *) LIST_GetElement(pListNode);
  878. if (CertFindSubjectInSortedCTL(
  879. &CryptDataBlobHashTag,
  880. pCatInfoContext->pCTLContext,
  881. NULL,
  882. NULL,
  883. NULL))
  884. {
  885. pListNodeToReturn = pListNode;
  886. goto CommonReturn;
  887. }
  888. pListNode = LIST_GetNext(pListNode);
  889. }
  890. //
  891. // If we get here that means no catalog was found
  892. //
  893. SetLastError(ERROR_NOT_FOUND);
  894. }
  895. }
  896. __except(EXCEPTION_EXECUTE_HANDLER)
  897. {
  898. CATADMIN_SETERR_LOG_RETURN(GetExceptionCode(), ErrorException)
  899. }
  900. CommonReturn:
  901. dwErr = GetLastError();
  902. if (pwszHashTag != NULL)
  903. {
  904. free(pwszHashTag);
  905. }
  906. if (pwszSearch != NULL)
  907. {
  908. free(pwszSearch);
  909. }
  910. if (hFindHandle != INVALID_HANDLE_VALUE)
  911. {
  912. FindClose(hFindHandle);
  913. }
  914. if (pListNodeToReturn != NULL)
  915. {
  916. pCatAdmin->nOpenCatInfoContexts++;
  917. }
  918. if (pPrevListNode != NULL)
  919. {
  920. *phPrevCatInfo = NULL;
  921. //
  922. // Decrement, since this is the equivalent of
  923. // calling CryptCATAdminReleaseCatalogContext
  924. //
  925. pCatAdmin->nOpenCatInfoContexts--;
  926. }
  927. if ((pCatAdmin != NULL) &&
  928. (pCatAdmin->fCSEntered))
  929. {
  930. pCatAdmin->fCSEntered = FALSE;
  931. LeaveCriticalSection(&(pCatAdmin->CriticalSection));
  932. }
  933. SetLastError(dwErr);
  934. return((HCATINFO) pListNodeToReturn);
  935. ErrorReturn:
  936. goto CommonReturn;
  937. TRACE_ERROR_EX(DBG_SS_TRUST, ErrorInvalidParam)
  938. TRACE_ERROR_EX(DBG_SS_TRUST, CatNotFound)
  939. TRACE_ERROR_EX(DBG_SS_TRUST, ErrorFindFirstFile)
  940. TRACE_ERROR_EX(DBG_SS_TRUST, ErrorFindNextFile)
  941. TRACE_ERROR_EX(DBG_SS_TRUST, ErrorException)
  942. }
  943. //---------------------------------------------------------------------------------------
  944. //
  945. // CryptCATCatalogInfoFromContext
  946. //
  947. //---------------------------------------------------------------------------------------
  948. BOOL WINAPI
  949. CryptCATCatalogInfoFromContext(
  950. IN HCATINFO hCatInfo,
  951. IN OUT CATALOG_INFO *psCatInfo,
  952. IN DWORD dwFlags)
  953. {
  954. BOOL fRet = TRUE;
  955. LIST_NODE *pListNode = (LIST_NODE *) hCatInfo;
  956. CATALOG_INFO_CONTEXT *pContext = NULL;
  957. if ((pListNode == NULL) || (psCatInfo == NULL))
  958. {
  959. CATADMIN_SETERR_LOG_RETURN(ERROR_INVALID_PARAMETER, ErrorInvalidParam)
  960. }
  961. pContext = (CATALOG_INFO_CONTEXT *) LIST_GetElement(pListNode);
  962. if (pContext->pwszCatalogFile != NULL)
  963. {
  964. if ((wcslen(pContext->pwszCatalogFile) + 1) > MAX_PATH)
  965. {
  966. CATADMIN_SETERR_LOG_RETURN(ERROR_NOT_ENOUGH_MEMORY, ErrorTooLong)
  967. }
  968. wcscpy(psCatInfo->wszCatalogFile, pContext->pwszCatalogFile);
  969. }
  970. CommonReturn:
  971. return(fRet);
  972. ErrorReturn:
  973. fRet = FALSE;
  974. goto CommonReturn;
  975. TRACE_ERROR_EX(DBG_SS_TRUST, ErrorInvalidParam)
  976. TRACE_ERROR_EX(DBG_SS_TRUST, ErrorTooLong)
  977. }
  978. //---------------------------------------------------------------------------------------
  979. //
  980. // CryptCATAdminReleaseCatalogContext
  981. //
  982. //---------------------------------------------------------------------------------------
  983. BOOL WINAPI
  984. CryptCATAdminReleaseCatalogContext(
  985. IN HCATADMIN hCatAdmin,
  986. IN HCATINFO hCatInfo,
  987. IN DWORD dwFlags)
  988. {
  989. BOOL fRet = TRUE;
  990. CRYPT_CAT_ADMIN *pCatAdmin = (CRYPT_CAT_ADMIN *)hCatAdmin;
  991. LIST_NODE *pListNode = (LIST_NODE *) hCatInfo;
  992. CATALOG_INFO_CONTEXT *pCatInfoContext = NULL;
  993. if ((pCatAdmin == NULL) ||
  994. (pCatAdmin->cbStruct != sizeof(CRYPT_CAT_ADMIN)) ||
  995. (pListNode == NULL))
  996. {
  997. CATADMIN_SETERR_LOG_RETURN(ERROR_INVALID_PARAMETER, ErrorInvalidParam)
  998. }
  999. //
  1000. // check to see if this is from and add operation, if so, then clean
  1001. // up allocated memory, otherwise, just decrement ref count
  1002. //
  1003. pCatInfoContext = (CATALOG_INFO_CONTEXT *) LIST_GetElement(pListNode);
  1004. if (pCatInfoContext->fResultOfAdd)
  1005. {
  1006. free(pCatInfoContext->pwszCatalogFile);
  1007. free(pCatInfoContext);
  1008. free(pListNode);
  1009. }
  1010. else
  1011. {
  1012. // FIX FIX - may need to be smarter about this... like verify
  1013. // the node is actually in the list.
  1014. pCatAdmin->nOpenCatInfoContexts--;
  1015. }
  1016. CommonReturn:
  1017. return(fRet);
  1018. ErrorReturn:
  1019. fRet = FALSE;
  1020. goto CommonReturn;
  1021. TRACE_ERROR_EX(DBG_SS_TRUST, ErrorInvalidParam);
  1022. }
  1023. //---------------------------------------------------------------------------------------
  1024. //
  1025. // CryptCATAdminResolveCatalogPath
  1026. //
  1027. //---------------------------------------------------------------------------------------
  1028. BOOL WINAPI
  1029. CryptCATAdminResolveCatalogPath(
  1030. IN HCATADMIN hCatAdmin,
  1031. IN WCHAR *pwszCatalogFile,
  1032. IN OUT CATALOG_INFO *psCatInfo,
  1033. IN DWORD dwFlags)
  1034. {
  1035. BOOL fRet = TRUE;
  1036. CRYPT_CAT_ADMIN *pCatAdmin = (CRYPT_CAT_ADMIN *)hCatAdmin;
  1037. if ((pCatAdmin == NULL) ||
  1038. (pCatAdmin->cbStruct != sizeof(CRYPT_CAT_ADMIN)) ||
  1039. (pwszCatalogFile == NULL) ||
  1040. (psCatInfo == NULL) ||
  1041. (psCatInfo->cbStruct != sizeof(CATALOG_INFO)) ||
  1042. (dwFlags != 0))
  1043. {
  1044. CATADMIN_SETERR_LOG_RETURN(ERROR_INVALID_PARAMETER, ErrorInvalidParam)
  1045. }
  1046. if ((wcslen(pCatAdmin->pwszCatalogFileDir) +
  1047. wcslen(pwszCatalogFile) +
  1048. 1) > MAX_PATH)
  1049. {
  1050. CATADMIN_SETERR_LOG_RETURN(ERROR_NOT_ENOUGH_MEMORY, ErrorTooLong)
  1051. }
  1052. wcscpy(psCatInfo->wszCatalogFile, pCatAdmin->pwszCatalogFileDir);
  1053. wcscat(psCatInfo->wszCatalogFile, pwszCatalogFile);
  1054. CommonReturn:
  1055. return(fRet);
  1056. ErrorReturn:
  1057. fRet = FALSE;
  1058. goto CommonReturn;
  1059. TRACE_ERROR_EX(DBG_SS_TRUST, ErrorInvalidParam)
  1060. TRACE_ERROR_EX(DBG_SS_TRUST, ErrorTooLong)
  1061. }
  1062. //---------------------------------------------------------------------------------------
  1063. //
  1064. // CryptCATAdminPauseServiceForBackup
  1065. //
  1066. //---------------------------------------------------------------------------------------
  1067. BOOL WINAPI
  1068. CryptCATAdminPauseServiceForBackup(
  1069. IN DWORD dwFlags,
  1070. IN BOOL fResume)
  1071. {
  1072. BOOL fRet = TRUE;
  1073. DWORD dwErr = 0;
  1074. //
  1075. // Call the DB process to delete the catalog
  1076. //
  1077. if (0 != (dwErr = Client_SSCatDBPauseResumeService(
  1078. 0,
  1079. fResume)))
  1080. {
  1081. CATADMIN_SETERR_LOG_RETURN(dwErr, ErrorCatDBProcess)
  1082. }
  1083. CommonReturn:
  1084. return(fRet);
  1085. ErrorReturn:
  1086. fRet = FALSE;
  1087. goto CommonReturn;
  1088. TRACE_ERROR_EX(DBG_SS_TRUST, ErrorCatDBProcess)
  1089. }
  1090. //---------------------------------------------------------------------------------------
  1091. //
  1092. // CryptCATAdminCalcHashFromFileHandle
  1093. //
  1094. //---------------------------------------------------------------------------------------
  1095. BOOL WINAPI
  1096. CryptCATAdminCalcHashFromFileHandle(
  1097. IN HANDLE hFile,
  1098. IN OUT DWORD *pcbHash,
  1099. IN BYTE *pbHash,
  1100. IN DWORD dwFlags)
  1101. {
  1102. BYTE *pbRet = NULL;
  1103. SIP_INDIRECT_DATA *pbIndirectData = NULL;
  1104. BOOL fRet;
  1105. GUID gSubject;
  1106. SIP_DISPATCH_INFO sSip;
  1107. if ((hFile == NULL) ||
  1108. (hFile == INVALID_HANDLE_VALUE) ||
  1109. (pcbHash == NULL) ||
  1110. (dwFlags != 0))
  1111. {
  1112. CATADMIN_SETERR_LOG_RETURN(ERROR_INVALID_PARAMETER, InvalidParam)
  1113. }
  1114. if (!CryptSIPRetrieveSubjectGuidForCatalogFile(L"CATADMIN", hFile, &gSubject))
  1115. {
  1116. goto ErrorMemory;
  1117. }
  1118. memset(&sSip, 0x00, sizeof(SIP_DISPATCH_INFO));
  1119. sSip.cbSize = sizeof(SIP_DISPATCH_INFO);
  1120. if (!CryptSIPLoad(&gSubject, 0, &sSip))
  1121. {
  1122. CATADMIN_LOGERR_LASTERR()
  1123. goto SIPLoadError;
  1124. }
  1125. SIP_SUBJECTINFO sSubjInfo;
  1126. DWORD cbIndirectData;
  1127. memset(&sSubjInfo, 0x00, sizeof(SIP_SUBJECTINFO));
  1128. sSubjInfo.cbSize = sizeof(SIP_SUBJECTINFO);
  1129. sSubjInfo.DigestAlgorithm.pszObjId = (char *)CertAlgIdToOID(CALG_SHA1);
  1130. sSubjInfo.dwFlags = SPC_INC_PE_RESOURCES_FLAG |
  1131. SPC_INC_PE_IMPORT_ADDR_TABLE_FLAG |
  1132. MSSIP_FLAGS_PROHIBIT_RESIZE_ON_CREATE;
  1133. sSubjInfo.pgSubjectType = &gSubject;
  1134. sSubjInfo.hFile = hFile;
  1135. sSubjInfo.pwsFileName = L"CATADMIN";
  1136. sSubjInfo.dwEncodingType = PKCS_7_ASN_ENCODING | X509_ASN_ENCODING;
  1137. cbIndirectData = 0;
  1138. sSip.pfCreate(&sSubjInfo, &cbIndirectData, NULL);
  1139. if (cbIndirectData == 0)
  1140. {
  1141. SetLastError(E_NOTIMPL);
  1142. //CATADMIN_LOGERR_LASTERR()
  1143. goto SIPError;
  1144. }
  1145. if (NULL == (pbIndirectData = (SIP_INDIRECT_DATA *) malloc(cbIndirectData)))
  1146. {
  1147. CATADMIN_SETERR_LOG_RETURN(ERROR_NOT_ENOUGH_MEMORY, ErrorMemory)
  1148. }
  1149. if (!(sSip.pfCreate(&sSubjInfo, &cbIndirectData, pbIndirectData)))
  1150. {
  1151. if (GetLastError() == 0)
  1152. {
  1153. SetLastError(ERROR_INVALID_DATA);
  1154. }
  1155. CATADMIN_LOGERR_LASTERR()
  1156. goto SIPError;
  1157. }
  1158. if ((pbIndirectData->Digest.cbData == 0) ||
  1159. (pbIndirectData->Digest.cbData > MAX_HASH_LEN))
  1160. {
  1161. SetLastError( ERROR_INVALID_DATA );
  1162. goto SIPError;
  1163. }
  1164. if (NULL == (pbRet = (BYTE *) malloc(pbIndirectData->Digest.cbData)))
  1165. {
  1166. CATADMIN_SETERR_LOG_RETURN(ERROR_NOT_ENOUGH_MEMORY, ErrorMemory)
  1167. }
  1168. memcpy(pbRet, pbIndirectData->Digest.pbData, pbIndirectData->Digest.cbData);
  1169. fRet = TRUE;
  1170. CommonReturn:
  1171. if (pbRet)
  1172. {
  1173. if (*pcbHash < pbIndirectData->Digest.cbData)
  1174. {
  1175. SetLastError(ERROR_INSUFFICIENT_BUFFER);
  1176. fRet = FALSE;
  1177. }
  1178. else if (pbHash)
  1179. {
  1180. memcpy(pbHash, pbRet, pbIndirectData->Digest.cbData);
  1181. }
  1182. *pcbHash = pbIndirectData->Digest.cbData;
  1183. free(pbRet);
  1184. }
  1185. if (pbIndirectData)
  1186. {
  1187. free(pbIndirectData);
  1188. }
  1189. if ((GetLastError() == ERROR_INSUFFICIENT_BUFFER) &&
  1190. (pbHash == NULL))
  1191. {
  1192. fRet = TRUE;
  1193. }
  1194. return(fRet);
  1195. ErrorReturn:
  1196. free(pbRet);
  1197. fRet = FALSE;
  1198. goto CommonReturn;
  1199. TRACE_ERROR_EX(DBG_SS_TRUST, SIPLoadError)
  1200. TRACE_ERROR_EX(DBG_SS_TRUST, SIPError)
  1201. TRACE_ERROR_EX(DBG_SS_TRUST, InvalidParam)
  1202. TRACE_ERROR_EX(DBG_SS_TRUST, ErrorMemory)
  1203. }
  1204. //---------------------------------------------------------------------------------------
  1205. //
  1206. // I_CryptCatAdminMigrateToNewCatDB
  1207. //
  1208. //---------------------------------------------------------------------------------------
  1209. BOOL WINAPI
  1210. I_CryptCatAdminMigrateToNewCatDB()
  1211. {
  1212. BOOL fRet = TRUE;
  1213. LPWSTR pwszSearchCatDirs = NULL;
  1214. LPWSTR pwszDeleteFile = NULL;
  1215. LPWSTR pwsz = NULL;
  1216. LPWSTR pwszMigrateFromDir = NULL;
  1217. HCATADMIN hCatAdmin = NULL;
  1218. GUID gDefault = DEF_CAT_SUBSYS_ID;
  1219. HANDLE hFindHandleCatDirs = INVALID_HANDLE_VALUE;
  1220. WIN32_FIND_DATAW FindDataCatDirs;
  1221. DWORD dwErr = 0;
  1222. HKEY hKey;
  1223. DWORD dwDisposition;
  1224. int i;
  1225. BOOL fInSync;
  1226. WCHAR wszGUID[256];
  1227. LPWSTR pwszCatalogFileDir = NULL;
  1228. LPWSTR pwszDatabaseFileDir = NULL;
  1229. //
  1230. // FIRST!!
  1231. //
  1232. // Clean up the old reg based catroot entry, and if needed, move
  1233. // the old style catalog database from its old directory to the new directory,
  1234. // then do the migrate from there
  1235. //
  1236. if (RegCreateKeyExU(
  1237. HKEY_LOCAL_MACHINE,
  1238. REG_MACHINE_SETTINGS_KEY,
  1239. 0,
  1240. NULL,
  1241. REG_OPTION_NON_VOLATILE,
  1242. KEY_ALL_ACCESS,
  1243. NULL,
  1244. &hKey,
  1245. &dwDisposition) == ERROR_SUCCESS)
  1246. {
  1247. DWORD dwType;
  1248. DWORD cbSize;
  1249. cbSize = 0;
  1250. RegQueryValueExU(
  1251. hKey,
  1252. WSZ_CATALOG_FILE_BASE_DIRECTORY,
  1253. NULL,
  1254. &dwType,
  1255. NULL,
  1256. &cbSize);
  1257. if (cbSize > 0)
  1258. {
  1259. if (NULL == (pwszMigrateFromDir = (LPWSTR)
  1260. malloc(sizeof(WCHAR) * ((cbSize / sizeof(WCHAR)) + 3))))
  1261. {
  1262. RegCloseKey(hKey);
  1263. CATADMIN_SETERR_LOG_RETURN(ERROR_NOT_ENOUGH_MEMORY, ErrorMemory)
  1264. }
  1265. pwszMigrateFromDir[0] = NULL;
  1266. RegQueryValueExU(
  1267. hKey,
  1268. WSZ_CATALOG_FILE_BASE_DIRECTORY,
  1269. NULL,
  1270. &dwType,
  1271. (BYTE *)pwszMigrateFromDir,
  1272. &cbSize);
  1273. if (!_CatAdminMigrateCatalogDatabase(
  1274. pwszMigrateFromDir,
  1275. gpwszCatalogFileBaseDirectory))
  1276. {
  1277. RegCloseKey(hKey);
  1278. CATADMIN_LOGERR_LASTERR()
  1279. goto ErrorReturn;
  1280. }
  1281. RegDeleteValueU(hKey, WSZ_CATALOG_FILE_BASE_DIRECTORY);
  1282. }
  1283. RegCloseKey(hKey);
  1284. }
  1285. //
  1286. // NOW, that we are in a consistent state
  1287. //
  1288. // For each catalog sub-system, enumerate all catalogs and add them to the
  1289. // new catalog database under the same sub-system GUID.
  1290. //
  1291. //
  1292. // Create search string to find all catalog sub dirs
  1293. //
  1294. if (NULL == (pwszSearchCatDirs = _CatAdminCreatePath(
  1295. gpwszCatalogFileBaseDirectory,
  1296. WSZ_CATALOG_SUBSYTEM_SEARCH_STRING,
  1297. FALSE)))
  1298. {
  1299. CATADMIN_LOGERR_LASTERR()
  1300. goto ErrorReturn;
  1301. }
  1302. //
  1303. // Do the initial find
  1304. //
  1305. hFindHandleCatDirs = FindFirstFileU(pwszSearchCatDirs, &FindDataCatDirs);
  1306. if (hFindHandleCatDirs == INVALID_HANDLE_VALUE)
  1307. {
  1308. //
  1309. // See if a real error occurred, or just no files
  1310. //
  1311. dwErr = GetLastError();
  1312. if ((dwErr == ERROR_NO_MORE_FILES) ||
  1313. (dwErr == ERROR_PATH_NOT_FOUND) ||
  1314. (dwErr == ERROR_FILE_NOT_FOUND))
  1315. {
  1316. //
  1317. // There is nothing to do
  1318. //
  1319. SetLastError(0);
  1320. goto RegKeyAdd;
  1321. }
  1322. else
  1323. {
  1324. CATADMIN_LOGERR_LASTERR()
  1325. goto ErrorFindFirstFile;
  1326. }
  1327. }
  1328. while (1)
  1329. {
  1330. //
  1331. // Only care about directories
  1332. //
  1333. if (FindDataCatDirs.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
  1334. {
  1335. _CatAdminMigrateSingleDatabase(FindDataCatDirs.cFileName);
  1336. }
  1337. //
  1338. // Get rid of old files
  1339. //
  1340. dwErr = GetLastError();
  1341. if (NULL != (pwsz = _CatAdminCreatePath(
  1342. gpwszCatalogFileBaseDirectory,
  1343. FindDataCatDirs.cFileName,
  1344. FALSE)))
  1345. {
  1346. for (i=0; i<NUM_FILES_TO_DELETE; i++)
  1347. {
  1348. if (NULL != (pwszDeleteFile = _CatAdminCreatePath(
  1349. pwsz,
  1350. ppwszFilesToDelete[i],
  1351. FALSE)))
  1352. {
  1353. if (!DeleteFileU(pwszDeleteFile))
  1354. {
  1355. //
  1356. // If delete fails, then log for delete after reboot
  1357. //
  1358. MoveFileExW(pwszDeleteFile, NULL, MOVEFILE_DELAY_UNTIL_REBOOT);
  1359. }
  1360. free(pwszDeleteFile);
  1361. }
  1362. }
  1363. free(pwsz);
  1364. }
  1365. SetLastError(dwErr);
  1366. //
  1367. // Get next subdir
  1368. //
  1369. if (!FindNextFileU(hFindHandleCatDirs, &FindDataCatDirs))
  1370. {
  1371. if (GetLastError() == ERROR_NO_MORE_FILES)
  1372. {
  1373. SetLastError(0);
  1374. break;
  1375. }
  1376. else
  1377. {
  1378. CATADMIN_LOGERR_LASTERR()
  1379. goto ErrorFindNextFile;
  1380. }
  1381. }
  1382. }
  1383. //
  1384. // Get rid of old files
  1385. //
  1386. dwErr = GetLastError();
  1387. for (i=0; i<NUM_FILES_TO_DELETE; i++)
  1388. {
  1389. if (NULL != (pwszDeleteFile = _CatAdminCreatePath(
  1390. gpwszCatalogFileBaseDirectory,
  1391. ppwszFilesToDelete[i],
  1392. FALSE)))
  1393. {
  1394. if (!DeleteFileU(pwszDeleteFile))
  1395. {
  1396. //
  1397. // If delete fails, then log for delete after reboot
  1398. //
  1399. MoveFileExW(pwszDeleteFile, NULL, MOVEFILE_DELAY_UNTIL_REBOOT);
  1400. }
  1401. free(pwszDeleteFile);
  1402. }
  1403. }
  1404. SetLastError(dwErr);
  1405. RegKeyAdd:
  1406. //
  1407. // Set reg key so backup does not backup the catroot2 directory
  1408. // which contains jet db files
  1409. //
  1410. if (RegCreateKeyExW(
  1411. HKEY_LOCAL_MACHINE,
  1412. WSZ_REG_FILES_NOT_TO_BACKUP,
  1413. 0,
  1414. NULL,
  1415. REG_OPTION_NON_VOLATILE,
  1416. KEY_ALL_ACCESS,
  1417. NULL,
  1418. &hKey,
  1419. &dwDisposition) == ERROR_SUCCESS)
  1420. {
  1421. if (RegSetValueExW(
  1422. hKey,
  1423. WSZ_REG_CATALOG_DATABASE_VALUE,
  1424. 0,
  1425. REG_MULTI_SZ,
  1426. (BYTE *) WSZ_PATH_NOT_TO_BACKUP,
  1427. (wcslen(WSZ_PATH_NOT_TO_BACKUP) + 2) * sizeof(WCHAR)) != ERROR_SUCCESS)
  1428. {
  1429. CATADMIN_LOGERR_LASTERR()
  1430. }
  1431. RegCloseKey(hKey);
  1432. }
  1433. else
  1434. {
  1435. CATADMIN_LOGERR_LASTERR()
  1436. }
  1437. //
  1438. // Force the default DB to be created
  1439. //
  1440. if (CryptCATAdminAcquireContext_Internal(
  1441. &hCatAdmin,
  1442. &gDefault,
  1443. NULL,
  1444. TRUE))
  1445. {
  1446. BYTE rgHash[20] = {0};
  1447. HCATINFO hCatInfo = NULL;
  1448. hCatInfo = CryptCATAdminEnumCatalogFromHash(
  1449. hCatAdmin,
  1450. rgHash,
  1451. 20,
  1452. 0,
  1453. NULL);
  1454. if (hCatInfo != NULL)
  1455. {
  1456. CryptCATAdminReleaseCatalogContext(hCatAdmin, hCatInfo, 0);
  1457. }
  1458. CryptCATAdminReleaseContext(hCatAdmin, 0);
  1459. //
  1460. // Need to create the timestamp files if they don't exist
  1461. //
  1462. guid2wstr(&gDefault, wszGUID);
  1463. //
  1464. // Construct full subdir path to Catalog files TimeStamp location
  1465. //
  1466. if (NULL == (pwszCatalogFileDir = _CatAdminCreatePath(
  1467. gpwszCatalogFileBaseDirectory,
  1468. wszGUID,
  1469. FALSE)))
  1470. {
  1471. CATADMIN_LOGERR_LASTERR()
  1472. goto CommonReturn; // non fatal for the function, so don't error out
  1473. }
  1474. //
  1475. // Construct full subdir path to Database files TimeStamp location
  1476. //
  1477. if (NULL == (pwszDatabaseFileDir = _CatAdminCreatePath(
  1478. gpwszDatabaseFileBaseDirectory,
  1479. wszGUID,
  1480. FALSE)))
  1481. {
  1482. CATADMIN_LOGERR_LASTERR()
  1483. goto CommonReturn; // non fatal for the function, so don't error out
  1484. }
  1485. //
  1486. // See if they are in sync (if they don't exist, that equals out of sync)
  1487. //
  1488. if (TimeStampFile_InSync(
  1489. pwszCatalogFileDir,
  1490. pwszDatabaseFileDir,
  1491. &fInSync))
  1492. {
  1493. if (!fInSync)
  1494. {
  1495. TimeStampFile_Touch(pwszCatalogFileDir);
  1496. TimeStampFile_Touch(pwszDatabaseFileDir);
  1497. }
  1498. }
  1499. else
  1500. {
  1501. CATADMIN_LOGERR_LASTERR()
  1502. }
  1503. }
  1504. else
  1505. {
  1506. CATADMIN_LOGERR_LASTERR()
  1507. }
  1508. CommonReturn:
  1509. dwErr = GetLastError();
  1510. if (pwszMigrateFromDir != NULL)
  1511. {
  1512. free(pwszMigrateFromDir);
  1513. }
  1514. if (pwszSearchCatDirs != NULL)
  1515. {
  1516. free(pwszSearchCatDirs);
  1517. }
  1518. if (hFindHandleCatDirs != INVALID_HANDLE_VALUE)
  1519. {
  1520. FindClose(hFindHandleCatDirs);
  1521. }
  1522. if (pwszCatalogFileDir != NULL)
  1523. {
  1524. free(pwszCatalogFileDir);
  1525. }
  1526. if (pwszDatabaseFileDir != NULL)
  1527. {
  1528. free(pwszDatabaseFileDir);
  1529. }
  1530. SetLastError(dwErr);
  1531. return(fRet);
  1532. ErrorReturn:
  1533. fRet = FALSE;
  1534. goto CommonReturn;
  1535. TRACE_ERROR_EX(DBG_SS_TRUST, ErrorMemory);
  1536. TRACE_ERROR_EX(DBG_SS_TRUST, ErrorFindFirstFile)
  1537. TRACE_ERROR_EX(DBG_SS_TRUST, ErrorFindNextFile)
  1538. }
  1539. //---------------------------------------------------------------------------------------
  1540. //
  1541. // _CatAdminMigrateSingleDatabase
  1542. //
  1543. //---------------------------------------------------------------------------------------
  1544. BOOL
  1545. _CatAdminMigrateSingleDatabase(
  1546. LPWSTR pwszDatabaseGUID)
  1547. {
  1548. BOOL fRet = TRUE;
  1549. LPWSTR pwszCatalogFile = NULL;
  1550. LPWSTR pwszSearchCatalogsInDir = NULL;
  1551. HANDLE hFindHandleCatalogsInDir = INVALID_HANDLE_VALUE;
  1552. WIN32_FIND_DATAW FindDataCatalogsInDir;
  1553. GUID guid;
  1554. HCATINFO hCatInfo = NULL;
  1555. HCATADMIN hCatAdmin = NULL;
  1556. DWORD dwErr = 0;
  1557. LPWSTR pwszSubDir = NULL;
  1558. LPWSTR pwszTempDir = NULL;
  1559. LPWSTR pwszTempCatalogFile = NULL;
  1560. //
  1561. // Acquire the catadmin context to add the catalog files to
  1562. //
  1563. if (!wstr2guid(pwszDatabaseGUID, &guid))
  1564. {
  1565. CATADMIN_LOGERR_LASTERR()
  1566. goto ErrorReturn;
  1567. }
  1568. if (!CryptCATAdminAcquireContext_Internal(&hCatAdmin, &guid, NULL, TRUE))
  1569. {
  1570. CATADMIN_LOGERR_LASTERR()
  1571. goto ErrorReturn;
  1572. }
  1573. //
  1574. // Construct full subdir path so we can search for all cat files
  1575. //
  1576. if (NULL == (pwszSubDir = _CatAdminCreatePath(
  1577. gpwszCatalogFileBaseDirectory,
  1578. pwszDatabaseGUID,
  1579. FALSE)))
  1580. {
  1581. CATADMIN_LOGERR_LASTERR()
  1582. goto ErrorReturn;
  1583. }
  1584. //
  1585. // Construct temp directory path, and create the directory to back it
  1586. //
  1587. if (NULL == (pwszTempDir = _CatAdminCreatePath(
  1588. pwszSubDir,
  1589. L"TempDir",
  1590. FALSE)))
  1591. {
  1592. CATADMIN_LOGERR_LASTERR()
  1593. goto ErrorReturn;
  1594. }
  1595. if (!_CatAdminRecursiveCreateDirectory(
  1596. pwszTempDir,
  1597. NULL))
  1598. {
  1599. CATADMIN_LOGERR_LASTERR()
  1600. goto ErrorReturn;
  1601. }
  1602. //
  1603. // Construct the search string
  1604. //
  1605. if (NULL == (pwszSearchCatalogsInDir = _CatAdminCreatePath(
  1606. pwszSubDir,
  1607. L"*",
  1608. FALSE)))
  1609. {
  1610. CATADMIN_LOGERR_LASTERR()
  1611. goto ErrorReturn;
  1612. }
  1613. //
  1614. // First copy all the catalogs to a temp directory, then add each catalog
  1615. // to the database from the temporary location
  1616. //
  1617. //
  1618. // Copy each file
  1619. //
  1620. memset(&FindDataCatalogsInDir, 0, sizeof(FindDataCatalogsInDir));
  1621. hFindHandleCatalogsInDir = FindFirstFileU(
  1622. pwszSearchCatalogsInDir,
  1623. &FindDataCatalogsInDir);
  1624. if (hFindHandleCatalogsInDir == INVALID_HANDLE_VALUE)
  1625. {
  1626. dwErr = GetLastError();
  1627. //
  1628. // no files found
  1629. //
  1630. if ((dwErr == ERROR_NO_MORE_FILES) ||
  1631. (dwErr == ERROR_FILE_NOT_FOUND))
  1632. {
  1633. SetLastError(0);
  1634. }
  1635. else
  1636. {
  1637. CATADMIN_LOGERR_LASTERR()
  1638. goto ErrorFindFirstFile;
  1639. }
  1640. }
  1641. else
  1642. {
  1643. while (1)
  1644. {
  1645. //
  1646. // Only care about files
  1647. //
  1648. if (!(FindDataCatalogsInDir.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
  1649. {
  1650. //
  1651. // Construct fully qualified path name to catalog file
  1652. //
  1653. if (NULL == (pwszCatalogFile = _CatAdminCreatePath(
  1654. pwszSubDir,
  1655. FindDataCatalogsInDir.cFileName,
  1656. FALSE)))
  1657. {
  1658. CATADMIN_LOGERR_LASTERR()
  1659. goto ErrorReturn;
  1660. }
  1661. //
  1662. // Verify that this is a catalog and then copy it to the temp dir
  1663. // which is where it will be installed from
  1664. //
  1665. if (IsCatalogFile(NULL, pwszCatalogFile))
  1666. {
  1667. if (NULL == (pwszTempCatalogFile = _CatAdminCreatePath(
  1668. pwszTempDir,
  1669. FindDataCatalogsInDir.cFileName,
  1670. FALSE)))
  1671. {
  1672. CATADMIN_LOGERR_LASTERR()
  1673. goto ErrorReturn;
  1674. }
  1675. if (!CopyFileU(pwszCatalogFile, pwszTempCatalogFile, FALSE))
  1676. {
  1677. CATADMIN_LOGERR_LASTERR()
  1678. goto ErrorReturn;
  1679. }
  1680. free(pwszTempCatalogFile);
  1681. pwszTempCatalogFile = NULL;
  1682. }
  1683. free(pwszCatalogFile);
  1684. pwszCatalogFile = NULL;
  1685. }
  1686. //
  1687. // Get next catalog file
  1688. //
  1689. if (!FindNextFileU(hFindHandleCatalogsInDir, &FindDataCatalogsInDir))
  1690. {
  1691. if (GetLastError() == ERROR_NO_MORE_FILES)
  1692. {
  1693. SetLastError(0);
  1694. break;
  1695. }
  1696. else
  1697. {
  1698. CATADMIN_LOGERR_LASTERR()
  1699. goto ErrorFindNextFile;
  1700. }
  1701. }
  1702. }
  1703. }
  1704. //
  1705. // Free up stuff used for find
  1706. //
  1707. free(pwszSearchCatalogsInDir);
  1708. pwszSearchCatalogsInDir = NULL;
  1709. FindClose(hFindHandleCatalogsInDir);
  1710. hFindHandleCatalogsInDir = INVALID_HANDLE_VALUE;
  1711. memset(&FindDataCatalogsInDir, 0, sizeof(FindDataCatalogsInDir));
  1712. //
  1713. // Construct the new search string which point to the temp dir
  1714. //
  1715. if (NULL == (pwszSearchCatalogsInDir = _CatAdminCreatePath(
  1716. pwszTempDir,
  1717. L"*",
  1718. FALSE)))
  1719. {
  1720. CATADMIN_LOGERR_LASTERR()
  1721. goto ErrorReturn;
  1722. }
  1723. //
  1724. // Add each catalog in the temp dir to the database
  1725. //
  1726. hFindHandleCatalogsInDir = FindFirstFileU(
  1727. pwszSearchCatalogsInDir,
  1728. &FindDataCatalogsInDir);
  1729. if (hFindHandleCatalogsInDir == INVALID_HANDLE_VALUE)
  1730. {
  1731. dwErr = GetLastError();
  1732. //
  1733. // no files found
  1734. //
  1735. if ((dwErr == ERROR_NO_MORE_FILES) ||
  1736. (dwErr == ERROR_FILE_NOT_FOUND))
  1737. {
  1738. SetLastError(0);
  1739. }
  1740. else
  1741. {
  1742. CATADMIN_LOGERR_LASTERR()
  1743. goto ErrorFindFirstFile;
  1744. }
  1745. }
  1746. else
  1747. {
  1748. while (1)
  1749. {
  1750. //
  1751. // Only care about files
  1752. //
  1753. if (!(FindDataCatalogsInDir.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
  1754. {
  1755. //
  1756. // Construct fully qualified path name to catalog file
  1757. //
  1758. if (NULL == (pwszCatalogFile = _CatAdminCreatePath(
  1759. pwszTempDir,
  1760. FindDataCatalogsInDir.cFileName,
  1761. FALSE)))
  1762. {
  1763. CATADMIN_LOGERR_LASTERR()
  1764. goto ErrorReturn;
  1765. }
  1766. hCatInfo = CryptCATAdminAddCatalog(
  1767. hCatAdmin,
  1768. pwszCatalogFile,
  1769. FindDataCatalogsInDir.cFileName,
  1770. NULL);
  1771. if (hCatInfo != NULL)
  1772. {
  1773. CryptCATAdminReleaseCatalogContext(
  1774. hCatAdmin,
  1775. hCatInfo,
  1776. NULL);
  1777. hCatInfo = NULL;
  1778. }
  1779. else
  1780. {
  1781. // Log error
  1782. CATADMIN_LOGERR_LASTERR()
  1783. }
  1784. free(pwszCatalogFile);
  1785. pwszCatalogFile = NULL;
  1786. }
  1787. //
  1788. // Get next catalog file
  1789. //
  1790. if (!FindNextFileU(hFindHandleCatalogsInDir, &FindDataCatalogsInDir))
  1791. {
  1792. if (GetLastError() == ERROR_NO_MORE_FILES)
  1793. {
  1794. SetLastError(0);
  1795. break;
  1796. }
  1797. else
  1798. {
  1799. CATADMIN_LOGERR_LASTERR()
  1800. goto ErrorFindNextFile;
  1801. }
  1802. }
  1803. }
  1804. }
  1805. CommonReturn:
  1806. dwErr = GetLastError();
  1807. if (pwszSubDir != NULL)
  1808. {
  1809. free(pwszSubDir);
  1810. }
  1811. if (pwszCatalogFile != NULL)
  1812. {
  1813. free(pwszCatalogFile);
  1814. }
  1815. if (pwszSearchCatalogsInDir != NULL)
  1816. {
  1817. free(pwszSearchCatalogsInDir);
  1818. }
  1819. if (pwszTempDir != NULL)
  1820. {
  1821. I_RecursiveDeleteDirectory(pwszTempDir);
  1822. free(pwszTempDir);
  1823. }
  1824. if (pwszTempCatalogFile != NULL)
  1825. {
  1826. free(pwszTempCatalogFile);
  1827. }
  1828. if (hFindHandleCatalogsInDir != INVALID_HANDLE_VALUE)
  1829. {
  1830. FindClose(hFindHandleCatalogsInDir);
  1831. }
  1832. if (hCatAdmin != NULL)
  1833. {
  1834. CryptCATAdminReleaseContext(hCatAdmin, NULL);
  1835. }
  1836. SetLastError(dwErr);
  1837. return(fRet);
  1838. ErrorReturn:
  1839. fRet = FALSE;
  1840. goto CommonReturn;
  1841. TRACE_ERROR_EX(DBG_SS_TRUST, ErrorFindFirstFile)
  1842. TRACE_ERROR_EX(DBG_SS_TRUST, ErrorFindNextFile)
  1843. }
  1844. //---------------------------------------------------------------------------------------
  1845. //
  1846. // CatAdminDllMain
  1847. //
  1848. //---------------------------------------------------------------------------------------
  1849. BOOL WINAPI
  1850. CatAdminDllMain(
  1851. HANDLE hInstDLL,
  1852. DWORD fdwReason,
  1853. LPVOID lpvReserved)
  1854. {
  1855. BOOL fRet = TRUE;
  1856. switch (fdwReason)
  1857. {
  1858. case DLL_PROCESS_ATTACH:
  1859. fRet = _CatAdminSetupDefaults();
  1860. break;
  1861. case DLL_PROCESS_DETACH:
  1862. _CatAdminCleanupDefaults();
  1863. break;
  1864. }
  1865. return(fRet);
  1866. }
  1867. //---------------------------------------------------------------------------------------
  1868. //---------------------------------------------------------------------------------------
  1869. // Internal functions
  1870. //---------------------------------------------------------------------------------------
  1871. //---------------------------------------------------------------------------------------
  1872. //---------------------------------------------------------------------------------------
  1873. //
  1874. // _CatAdminSetupDefaults
  1875. //
  1876. //---------------------------------------------------------------------------------------
  1877. BOOL
  1878. _CatAdminSetupDefaults(void)
  1879. {
  1880. BOOL fRet = TRUE;
  1881. WCHAR wszDefaultSystemDir[MAX_PATH + 1];
  1882. //
  1883. // Get System default directory
  1884. //
  1885. wszDefaultSystemDir[0] = NULL;
  1886. if (0 == GetSystemDirectoryW(wszDefaultSystemDir, MAX_PATH))
  1887. {
  1888. CATADMIN_LOGERR_LASTERR()
  1889. goto ErrorSystemError;
  1890. }
  1891. //
  1892. // Get catalog file base directory
  1893. //
  1894. if (NULL == (gpwszCatalogFileBaseDirectory =
  1895. _CatAdminCreatePath(
  1896. wszDefaultSystemDir,
  1897. WSZ_CATALOG_FILE_BASE_DIRECTORY,
  1898. TRUE)))
  1899. {
  1900. CATADMIN_LOGERR_LASTERR()
  1901. goto ErrorReturn;
  1902. }
  1903. //
  1904. // Get database file base directory
  1905. //
  1906. if (NULL == (gpwszDatabaseFileBaseDirectory =
  1907. _CatAdminCreatePath(
  1908. wszDefaultSystemDir,
  1909. WSZ_DATABASE_FILE_BASE_DIRECTORY,
  1910. TRUE)))
  1911. {
  1912. CATADMIN_LOGERR_LASTERR()
  1913. goto ErrorReturn;
  1914. }
  1915. CommonReturn:
  1916. return(fRet);
  1917. ErrorReturn:
  1918. if (gpwszCatalogFileBaseDirectory != NULL)
  1919. {
  1920. free(gpwszCatalogFileBaseDirectory);
  1921. gpwszCatalogFileBaseDirectory = NULL;
  1922. }
  1923. fRet = FALSE;
  1924. goto CommonReturn;
  1925. TRACE_ERROR_EX(DBG_SS_TRUST, ErrorSystemError);
  1926. }
  1927. //---------------------------------------------------------------------------------------
  1928. //
  1929. // _CatAdminCleanupDefaults
  1930. //
  1931. //---------------------------------------------------------------------------------------
  1932. void _CatAdminCleanupDefaults(void)
  1933. {
  1934. if (gpwszCatalogFileBaseDirectory != NULL)
  1935. {
  1936. free(gpwszCatalogFileBaseDirectory);
  1937. gpwszCatalogFileBaseDirectory = NULL;
  1938. }
  1939. if (gpwszDatabaseFileBaseDirectory != NULL)
  1940. {
  1941. free(gpwszDatabaseFileBaseDirectory);
  1942. gpwszDatabaseFileBaseDirectory = NULL;
  1943. }
  1944. }
  1945. //---------------------------------------------------------------------------------------
  1946. //
  1947. // _CatAdminTimeStampFilesInSync
  1948. //
  1949. //---------------------------------------------------------------------------------------
  1950. BOOL
  1951. _CatAdminTimeStampFilesInSync(
  1952. LPWSTR pwszDatabaseGUID,
  1953. BOOL *pfInSync)
  1954. {
  1955. LPWSTR pwszCatalogFileDir = NULL;
  1956. LPWSTR pwszDatabaseFileDir = NULL;
  1957. BOOL fRet = TRUE;
  1958. *pfInSync = FALSE;
  1959. //
  1960. // Construct full subdir path to Catalog files TimeStamp location
  1961. //
  1962. if (NULL == (pwszCatalogFileDir = _CatAdminCreatePath(
  1963. gpwszCatalogFileBaseDirectory,
  1964. pwszDatabaseGUID,
  1965. FALSE)))
  1966. {
  1967. CATADMIN_LOGERR_LASTERR()
  1968. goto ErrorReturn;
  1969. }
  1970. //
  1971. // Construct full subdir path to Database files TimeStamp location
  1972. //
  1973. if (NULL == (pwszDatabaseFileDir = _CatAdminCreatePath(
  1974. gpwszDatabaseFileBaseDirectory,
  1975. pwszDatabaseGUID,
  1976. FALSE)))
  1977. {
  1978. CATADMIN_LOGERR_LASTERR()
  1979. goto ErrorReturn;
  1980. }
  1981. fRet = TimeStampFile_InSync(
  1982. pwszCatalogFileDir,
  1983. pwszDatabaseFileDir,
  1984. pfInSync);
  1985. CommonReturn:
  1986. if (pwszCatalogFileDir != NULL)
  1987. {
  1988. free(pwszCatalogFileDir);
  1989. }
  1990. if (pwszDatabaseFileDir != NULL)
  1991. {
  1992. free(pwszDatabaseFileDir);
  1993. }
  1994. return(fRet);
  1995. ErrorReturn:
  1996. fRet = FALSE;
  1997. goto CommonReturn;
  1998. }
  1999. //---------------------------------------------------------------------------------------
  2000. //
  2001. // _CatAdminRegisterForChangeNotification
  2002. //
  2003. //---------------------------------------------------------------------------------------
  2004. BOOL
  2005. _CatAdminRegisterForChangeNotification(
  2006. CRYPT_CAT_ADMIN *pCatAdmin
  2007. )
  2008. {
  2009. BOOL fRet = TRUE;
  2010. DWORD dwErr = 0;
  2011. //
  2012. // See if already registered
  2013. //
  2014. if (pCatAdmin->fRegisteredForChangeNotification)
  2015. {
  2016. goto CommonReturn;
  2017. }
  2018. //
  2019. // NOTE:
  2020. // Currently the service ignores the pwszSubSysGUID when registering a change
  2021. // notification because it DOES NOT do notifications on a per pwszSubSysDir basis...
  2022. // it really should at some point.
  2023. // When it does start to do notifications on per pwszSubSysGUID this will need to
  2024. // change. CryptCatAdminAcquireContext can be called with a NULL subSysGUID,
  2025. // in which case all SubSysDirs are used, so we would need to register a
  2026. // change notification for all of them.
  2027. //
  2028. //
  2029. // Register the event with the DB process, so the DB process can SetEvent() it
  2030. // when a changed occurs
  2031. //
  2032. if (0 != (dwErr = Client_SSCatDBRegisterForChangeNotification(
  2033. (DWORD_PTR) pCatAdmin->hClearCacheEvent,
  2034. 0,
  2035. pCatAdmin->pwszSubSysGUID,
  2036. FALSE)))
  2037. {
  2038. CATADMIN_SETERR_LOG_RETURN(dwErr, ErrorCatDBProcess)
  2039. }
  2040. pCatAdmin->fRegisteredForChangeNotification = TRUE;
  2041. CommonReturn:
  2042. return fRet;
  2043. ErrorReturn:
  2044. fRet = FALSE;
  2045. goto CommonReturn;
  2046. TRACE_ERROR_EX(DBG_SS_TRUST, ErrorCatDBProcess)
  2047. }
  2048. //---------------------------------------------------------------------------------------
  2049. //
  2050. // _CatAdminFreeCachedCatalogs
  2051. //
  2052. //---------------------------------------------------------------------------------------
  2053. BOOL
  2054. _CatAdminFreeCachedCatalogs(
  2055. CRYPT_CAT_ADMIN *pCatAdmin)
  2056. {
  2057. BOOL fRet = TRUE;
  2058. LIST_NODE *pListNode = NULL;
  2059. CATALOG_INFO_CONTEXT *pCatInfoContext = NULL;
  2060. //
  2061. // NOTE: the caller of this function must have entered the Critical Section for
  2062. // the CatAdminContext
  2063. //
  2064. //
  2065. // Enumerate through all the cached CATALOG_INFO_CONTEXTs and free all the
  2066. // resources for each
  2067. //
  2068. pListNode = LIST_GetFirst(&(pCatAdmin->CatalogInfoContextList));
  2069. while (pListNode != NULL)
  2070. {
  2071. pCatInfoContext = (CATALOG_INFO_CONTEXT *) LIST_GetElement(pListNode);
  2072. free(pCatInfoContext->pwszCatalogFile);
  2073. CertFreeCTLContext(pCatInfoContext->pCTLContext);
  2074. UnmapViewOfFile(pCatInfoContext->pbMappedFile);
  2075. CloseHandle(pCatInfoContext->hMappedFile);
  2076. free(pCatInfoContext);
  2077. pListNode = LIST_GetNext(pListNode);
  2078. }
  2079. LIST_RemoveAll(&(pCatAdmin->CatalogInfoContextList));
  2080. return(fRet);
  2081. }
  2082. //---------------------------------------------------------------------------------------
  2083. //
  2084. // _CatAdminWaitOrTimerCallback
  2085. //
  2086. //---------------------------------------------------------------------------------------
  2087. VOID CALLBACK
  2088. _CatAdminWaitOrTimerCallback(
  2089. PVOID lpParameter,
  2090. BOOLEAN TimerOrWaitFired)
  2091. {
  2092. CRYPT_CAT_ADMIN *pCatAdmin = (CRYPT_CAT_ADMIN *) lpParameter;
  2093. //
  2094. // Enter the CS before wacking anything
  2095. //
  2096. EnterCriticalSection(&(pCatAdmin->CriticalSection));
  2097. pCatAdmin->fCSEntered = TRUE;
  2098. //
  2099. // If there is an open ref count, then we can't clean up
  2100. //
  2101. if (pCatAdmin->nOpenCatInfoContexts != 0)
  2102. {
  2103. pCatAdmin->fCSEntered = FALSE;
  2104. LeaveCriticalSection(&(pCatAdmin->CriticalSection));
  2105. return;
  2106. }
  2107. //
  2108. // Cleanup all the cached CATALOG_INFO_CONTEXTs
  2109. //
  2110. _CatAdminFreeCachedCatalogs(pCatAdmin);
  2111. pCatAdmin->fCSEntered = FALSE;
  2112. LeaveCriticalSection(&(pCatAdmin->CriticalSection));
  2113. }
  2114. //---------------------------------------------------------------------------------------
  2115. //
  2116. // _CatAdminAddCatalogsToCache
  2117. //
  2118. //---------------------------------------------------------------------------------------
  2119. BOOL
  2120. _CatAdminAddCatalogsToCache(
  2121. CRYPT_CAT_ADMIN *pCatAdmin,
  2122. LPWSTR pwszSubSysGUID,
  2123. CRYPT_DATA_BLOB *pCryptDataBlob,
  2124. LIST_NODE **ppFirstListNodeAdded)
  2125. {
  2126. BOOL fRet = TRUE;
  2127. LPWSTR pwszCopy = NULL;
  2128. DWORD i;
  2129. DWORD dwNumCatalogNames = 0;
  2130. LPWSTR *ppwszCatalogNames = NULL;
  2131. DWORD dwErr = 0;
  2132. LIST_NODE *pListNode = NULL;
  2133. LPWSTR pwszSubSysDir = NULL;
  2134. if (ppFirstListNodeAdded != NULL)
  2135. {
  2136. *ppFirstListNodeAdded = NULL;
  2137. }
  2138. if (NULL == (pwszSubSysDir = _CatAdminCreatePath(
  2139. gpwszCatalogFileBaseDirectory,
  2140. pwszSubSysGUID,
  2141. FALSE)))
  2142. {
  2143. CATADMIN_LOGERR_LASTERR()
  2144. goto ErrorReturn;
  2145. }
  2146. //
  2147. // Call DB process and get list of catalogs into ppwszCatalogNames
  2148. //
  2149. // NOTE: the order in which the service adds CatNames to the list results in
  2150. // only the first CatName of the list being guaranteed to contain the
  2151. // hash... all other CatNames may or may not contain the hash. Which
  2152. // is OK because this code only assumes the first CatName contains the
  2153. // hash, and then searches all other CatNames for the hash before returning them.
  2154. //
  2155. if (0 != (dwErr = Client_SSCatDBEnumCatalogs(
  2156. 0,
  2157. pwszSubSysGUID,
  2158. pCryptDataBlob->pbData,
  2159. pCryptDataBlob->cbData,
  2160. &dwNumCatalogNames,
  2161. &ppwszCatalogNames)))
  2162. {
  2163. CATADMIN_SETERR_LOG_RETURN(dwErr, ErrorServiceError)
  2164. }
  2165. //
  2166. // Loop for each catalog and create the CTL context
  2167. //
  2168. for (i=0; i<dwNumCatalogNames; i++)
  2169. {
  2170. //
  2171. // Make a copy of the catalog file name
  2172. //
  2173. if (NULL == (pwszCopy = _CatAdminCreatePath(
  2174. pwszSubSysDir,
  2175. ppwszCatalogNames[i],
  2176. FALSE)))
  2177. {
  2178. CATADMIN_LOGERR_LASTERR()
  2179. goto ErrorReturn;
  2180. }
  2181. if (!_CatAdminAddSingleCatalogToCache(
  2182. pCatAdmin,
  2183. pwszCopy,
  2184. &pListNode))
  2185. {
  2186. //
  2187. // if this isn't the first catalog, then continue since the
  2188. // macro operation may still succeed without the current catalog
  2189. //
  2190. if (i != 0)
  2191. {
  2192. CATADMIN_LOGERR_LASTERR()
  2193. continue;
  2194. }
  2195. CATADMIN_LOGERR_LASTERR()
  2196. goto ErrorReturn;
  2197. }
  2198. //
  2199. // This will only be set for the first catalog added,
  2200. // as per the NOTE above
  2201. //
  2202. if ((ppFirstListNodeAdded != NULL) &&
  2203. (*ppFirstListNodeAdded == NULL))
  2204. {
  2205. *ppFirstListNodeAdded = pListNode;
  2206. }
  2207. }
  2208. CommonReturn:
  2209. if (ppwszCatalogNames != NULL)
  2210. {
  2211. for (i=0; i<dwNumCatalogNames; i++)
  2212. {
  2213. MIDL_user_free(ppwszCatalogNames[i]);
  2214. }
  2215. MIDL_user_free(ppwszCatalogNames);
  2216. }
  2217. if (pwszSubSysDir != NULL)
  2218. {
  2219. free(pwszSubSysDir);
  2220. }
  2221. return(fRet);
  2222. ErrorReturn:
  2223. if (pwszCopy != NULL)
  2224. {
  2225. free(pwszCopy);
  2226. }
  2227. fRet = FALSE;
  2228. goto CommonReturn;
  2229. TRACE_ERROR_EX(DBG_SS_TRUST, ErrorServiceError)
  2230. }
  2231. //---------------------------------------------------------------------------------------
  2232. //
  2233. // _CatAdminAddSingleCatalogToCache
  2234. //
  2235. //---------------------------------------------------------------------------------------
  2236. BOOL
  2237. _CatAdminAddSingleCatalogToCache(
  2238. CRYPT_CAT_ADMIN *pCatAdmin,
  2239. LPWSTR pwszCatalog,
  2240. LIST_NODE **ppListNodeAdded)
  2241. {
  2242. BOOL fRet = TRUE;
  2243. DWORD dwErr = 0;
  2244. LIST_NODE *pListNode = NULL;
  2245. CATALOG_INFO_CONTEXT *pCatInfoContext = NULL;
  2246. CATALOG_INFO_CONTEXT *pCatInfoContextAdd = NULL;
  2247. *ppListNodeAdded = NULL;
  2248. //
  2249. // If there is already a copy of this catalog, then just get out
  2250. //
  2251. pListNode = LIST_GetFirst(&(pCatAdmin->CatalogInfoContextList));
  2252. while (pListNode != NULL)
  2253. {
  2254. pCatInfoContext = (CATALOG_INFO_CONTEXT *) LIST_GetElement(pListNode);
  2255. if (_wcsicmp(pCatInfoContext->pwszCatalogFile, pwszCatalog) == 0)
  2256. {
  2257. *ppListNodeAdded = pListNode;
  2258. goto CommonReturn;
  2259. }
  2260. pListNode = LIST_GetNext(pListNode);
  2261. }
  2262. //
  2263. // Allocate space for a new cached catalog context
  2264. //
  2265. if (NULL == (pCatInfoContextAdd = (CATALOG_INFO_CONTEXT *)
  2266. malloc(sizeof(CATALOG_INFO_CONTEXT))))
  2267. {
  2268. CATADMIN_SETERR_LOG_RETURN(ERROR_NOT_ENOUGH_MEMORY, ErrorMemory)
  2269. }
  2270. memset(pCatInfoContextAdd, 0, sizeof(CATALOG_INFO_CONTEXT));
  2271. pCatInfoContextAdd->fResultOfAdd = FALSE;
  2272. //
  2273. // Open, create a file mapping, and create the CTL context for
  2274. // the catalog file
  2275. //
  2276. if (!CatUtil_CreateCTLContextFromFileName(
  2277. pwszCatalog,
  2278. &pCatInfoContextAdd->hMappedFile,
  2279. &pCatInfoContextAdd->pbMappedFile,
  2280. &pCatInfoContextAdd->pCTLContext,
  2281. TRUE))
  2282. {
  2283. CATADMIN_LOGERR_LASTERR()
  2284. ErrLog_LogString(NULL, L"The following file was not found - ", pwszCatalog, TRUE);
  2285. goto ErrorReturn;
  2286. }
  2287. pCatInfoContextAdd->pwszCatalogFile = pwszCatalog;
  2288. //
  2289. // Add to the list of cached catalog contexts
  2290. //
  2291. if (NULL == (pListNode = LIST_AddTail(
  2292. &(pCatAdmin->CatalogInfoContextList),
  2293. pCatInfoContextAdd)))
  2294. {
  2295. CATADMIN_LOGERR_LASTERR()
  2296. goto ErrorReturn;
  2297. }
  2298. *ppListNodeAdded = pListNode;
  2299. CommonReturn:
  2300. return(fRet);
  2301. ErrorReturn:
  2302. dwErr = GetLastError();
  2303. if (pCatInfoContextAdd != NULL)
  2304. {
  2305. if (pCatInfoContextAdd->pCTLContext != NULL)
  2306. {
  2307. CertFreeCTLContext(pCatInfoContextAdd->pCTLContext);
  2308. }
  2309. if (pCatInfoContextAdd->pbMappedFile != NULL)
  2310. {
  2311. UnmapViewOfFile(pCatInfoContextAdd->pbMappedFile);
  2312. }
  2313. if (pCatInfoContextAdd->hMappedFile != NULL)
  2314. {
  2315. CloseHandle(pCatInfoContextAdd->hMappedFile);
  2316. }
  2317. free(pCatInfoContextAdd);
  2318. }
  2319. SetLastError(dwErr);
  2320. fRet = FALSE;
  2321. goto CommonReturn;
  2322. TRACE_ERROR_EX(DBG_SS_TRUST, ErrorMemory)
  2323. }
  2324. //---------------------------------------------------------------------------------------
  2325. //
  2326. // _CatAdminMigrateCatalogDatabase
  2327. //
  2328. // This migration code deals with very old catalog databases. In the olden days, the
  2329. // catroot dir location could be specified by a particular registry key... that is no
  2330. // longer true. So, if an old system is being upgraded that has the registry key, this
  2331. // code moves all the catalog files from the location specified by the registry key to
  2332. // the %SystemDefaultDir%\Catroot dir. Then it shwacks the registry key.
  2333. //
  2334. //---------------------------------------------------------------------------------------
  2335. BOOL
  2336. _CatAdminMigrateCatalogDatabase(
  2337. LPWSTR pwszFrom,
  2338. LPWSTR pwszTo)
  2339. {
  2340. DWORD dwAttr = 0;
  2341. WCHAR wszFrom[MAX_PATH];
  2342. WCHAR wszTo[MAX_PATH];
  2343. //
  2344. // If they are the same dir then just get out
  2345. //
  2346. if (((wcslen(pwszFrom) + 2) > MAX_PATH) ||
  2347. ((wcslen(pwszTo) + 2) > MAX_PATH))
  2348. {
  2349. return TRUE;
  2350. }
  2351. wcscpy(wszFrom, pwszFrom);
  2352. wcscpy(wszTo, pwszTo);
  2353. if (wszFrom[wcslen(wszFrom) - 1] != L'\\')
  2354. {
  2355. wcscat(wszFrom, L"\\");
  2356. }
  2357. if (wszTo[wcslen(wszTo) - 1] != L'\\')
  2358. {
  2359. wcscat(wszTo, L"\\");
  2360. }
  2361. if (_wcsicmp(wszFrom, wszTo) == 0)
  2362. {
  2363. return TRUE;
  2364. }
  2365. //
  2366. // if the pwszTo dir already exists, then don't do a thing.
  2367. //
  2368. dwAttr = GetFileAttributesU(pwszTo);
  2369. if (INVALID_FILE_ATTRIBUTES != dwAttr)
  2370. {
  2371. if (FILE_ATTRIBUTE_DIRECTORY & dwAttr)
  2372. {
  2373. //
  2374. // dir already exists...
  2375. //
  2376. return TRUE;
  2377. }
  2378. else
  2379. {
  2380. //
  2381. // something exists with pwszTo name, but it isn't a dir
  2382. //
  2383. CATADMIN_LOGERR_LASTERR()
  2384. return FALSE;
  2385. }
  2386. }
  2387. //
  2388. // if the pwszFrom dir does not exist, then don't do a thing.
  2389. //
  2390. dwAttr = GetFileAttributesU(pwszFrom);
  2391. if ((0xFFFFFFFF == dwAttr) || (!(FILE_ATTRIBUTE_DIRECTORY & dwAttr)))
  2392. {
  2393. return TRUE;
  2394. }
  2395. if (!_CatAdminRecursiveCreateDirectory(pwszTo, NULL))
  2396. {
  2397. CATADMIN_LOGERR_LASTERR()
  2398. return FALSE;
  2399. }
  2400. if (!I_RecursiveCopyDirectory(pwszFrom, pwszTo))
  2401. {
  2402. CATADMIN_LOGERR_LASTERR()
  2403. return FALSE;
  2404. }
  2405. //
  2406. // Don't check for error on delete since this operation is NOT mandatory
  2407. //
  2408. I_RecursiveDeleteDirectory(pwszFrom);
  2409. return TRUE;
  2410. }
  2411. //---------------------------------------------------------------------------------------
  2412. //
  2413. // _CatAdminBToHex
  2414. //
  2415. //---------------------------------------------------------------------------------------
  2416. WCHAR rgHexDigit[] = { L'0', L'1', L'2', L'3', L'4', L'5', L'6', L'7',
  2417. L'8', L'9', L'A', L'B', L'C', L'D', L'E', L'F' };
  2418. void
  2419. _CatAdminBToHex (
  2420. LPBYTE pbDigest,
  2421. DWORD iByte,
  2422. LPWSTR pwszHashTag)
  2423. {
  2424. DWORD iTag;
  2425. DWORD iHexDigit1;
  2426. DWORD iHexDigit2;
  2427. iTag = iByte * 2;
  2428. iHexDigit1 = (pbDigest[iByte] & 0xF0) >> 4;
  2429. iHexDigit2 = (pbDigest[iByte] & 0x0F);
  2430. pwszHashTag[iTag] = rgHexDigit[iHexDigit1];
  2431. pwszHashTag[iTag + 1] = rgHexDigit[iHexDigit2];
  2432. }
  2433. //---------------------------------------------------------------------------------------
  2434. //
  2435. // _CatAdminCreateHashTag
  2436. //
  2437. //---------------------------------------------------------------------------------------
  2438. BOOL
  2439. _CatAdminCreateHashTag(
  2440. BYTE *pbHash,
  2441. DWORD cbHash,
  2442. LPWSTR *ppwszHashTag,
  2443. CRYPT_DATA_BLOB *pCryptDataBlob)
  2444. {
  2445. DWORD cwTag;
  2446. DWORD cCount;
  2447. cwTag = ((cbHash * 2) + 1);
  2448. if (NULL == (*ppwszHashTag = (LPWSTR) malloc(cwTag * sizeof(WCHAR))))
  2449. {
  2450. SetLastError(ERROR_NOT_ENOUGH_MEMORY);
  2451. CATADMIN_LOGERR_LASTERR()
  2452. return(FALSE);
  2453. }
  2454. for (cCount = 0; cCount < cbHash; cCount++)
  2455. {
  2456. _CatAdminBToHex(pbHash, cCount, *ppwszHashTag);
  2457. }
  2458. (*ppwszHashTag)[cwTag - 1] = L'\0';
  2459. pCryptDataBlob->pbData = (BYTE *) *ppwszHashTag;
  2460. pCryptDataBlob->cbData = cwTag * sizeof(WCHAR);
  2461. return (TRUE);
  2462. }
  2463. //---------------------------------------------------------------------------------------
  2464. //
  2465. // _CatAdminRecursiveCreateDirectory
  2466. //
  2467. //---------------------------------------------------------------------------------------
  2468. BOOL
  2469. _CatAdminRecursiveCreateDirectory(
  2470. IN LPCWSTR pwszDir,
  2471. LPSECURITY_ATTRIBUTES lpSecurityAttributes
  2472. )
  2473. {
  2474. BOOL fResult;
  2475. DWORD dwAttr;
  2476. DWORD dwErr;
  2477. LPCWSTR pwsz;
  2478. DWORD cch;
  2479. WCHAR wch;
  2480. LPWSTR pwszParent = NULL;
  2481. //
  2482. // if last char is a '\', then just strip it and recurse
  2483. //
  2484. if (pwszDir[wcslen(pwszDir) - 1] == L'\\')
  2485. {
  2486. cch = wcslen(pwszDir);
  2487. if (NULL == (pwszParent = (LPWSTR) malloc(cch * sizeof(WCHAR))))
  2488. {
  2489. CATADMIN_SETERR_LOG_RETURN(ERROR_NOT_ENOUGH_MEMORY, ErrorMemory)
  2490. }
  2491. memcpy(pwszParent, pwszDir, (cch - 1) * sizeof(WCHAR));
  2492. pwszParent[cch - 1] = L'\0';
  2493. fResult = _CatAdminRecursiveCreateDirectory(
  2494. pwszParent,
  2495. lpSecurityAttributes);
  2496. goto CommonReturn;
  2497. }
  2498. //
  2499. // See if dir already exists
  2500. //
  2501. dwAttr = GetFileAttributesU(pwszDir);
  2502. if (0xFFFFFFFF != dwAttr)
  2503. {
  2504. if (FILE_ATTRIBUTE_DIRECTORY & dwAttr)
  2505. {
  2506. return TRUE;
  2507. }
  2508. CATADMIN_LOGERR_LASTERR()
  2509. goto InvalidDirectoryAttr;
  2510. }
  2511. //
  2512. // If it was an error other than file/path not found, error out
  2513. //
  2514. dwErr = GetLastError();
  2515. if (!(ERROR_PATH_NOT_FOUND == dwErr || ERROR_FILE_NOT_FOUND == dwErr))
  2516. {
  2517. CATADMIN_LOGERR_LASTERR()
  2518. goto GetFileAttrError;
  2519. }
  2520. //
  2521. // Try creating the new dir
  2522. //
  2523. if (CreateDirectoryU(
  2524. pwszDir,
  2525. lpSecurityAttributes))
  2526. {
  2527. SetFileAttributesU(pwszDir, FILE_ATTRIBUTE_NORMAL);
  2528. return TRUE;
  2529. }
  2530. dwErr = GetLastError();
  2531. if (!(ERROR_PATH_NOT_FOUND == dwErr || ERROR_FILE_NOT_FOUND == dwErr))
  2532. {
  2533. CATADMIN_LOGERR_LASTERR()
  2534. goto CreateDirectoryError;
  2535. }
  2536. //
  2537. // Peal off the last path name component
  2538. //
  2539. cch = wcslen(pwszDir);
  2540. pwsz = pwszDir + cch;
  2541. while (L'\\' != *pwsz)
  2542. {
  2543. if (pwsz == pwszDir)
  2544. {
  2545. // Path didn't have a \.
  2546. CATADMIN_SETERR_LOG_RETURN(ERROR_BAD_PATHNAME, BadDirectoryPath)
  2547. }
  2548. pwsz--;
  2549. }
  2550. cch = (DWORD)(pwsz - pwszDir);
  2551. if (0 == cch)
  2552. {
  2553. // Detected leading \Path
  2554. CATADMIN_SETERR_LOG_RETURN(ERROR_BAD_PATHNAME, BadDirectoryPath)
  2555. }
  2556. // Check for leading \\ or x:\.
  2557. wch = *(pwsz - 1);
  2558. if ((1 == cch && L'\\' == wch) || (2 == cch && L':' == wch))
  2559. {
  2560. CATADMIN_SETERR_LOG_RETURN(ERROR_BAD_PATHNAME, BadDirectoryPath)
  2561. }
  2562. if (NULL == (pwszParent = (LPWSTR) malloc((cch + 1) * sizeof(WCHAR))))
  2563. {
  2564. CATADMIN_SETERR_LOG_RETURN(ERROR_NOT_ENOUGH_MEMORY, ErrorMemory)
  2565. }
  2566. memcpy(pwszParent, pwszDir, cch * sizeof(WCHAR));
  2567. pwszParent[cch] = L'\0';
  2568. if (!_CatAdminRecursiveCreateDirectory(pwszParent, lpSecurityAttributes))
  2569. {
  2570. CATADMIN_LOGERR_LASTERR()
  2571. goto ErrorReturn;
  2572. }
  2573. if (!CreateDirectoryU(
  2574. pwszDir,
  2575. lpSecurityAttributes))
  2576. {
  2577. CATADMIN_LOGERR_LASTERR()
  2578. goto CreateDirectory2Error;
  2579. }
  2580. SetFileAttributesU(pwszDir, FILE_ATTRIBUTE_NORMAL);
  2581. fResult = TRUE;
  2582. CommonReturn:
  2583. if (pwszParent != NULL)
  2584. {
  2585. free(pwszParent);
  2586. }
  2587. return fResult;
  2588. ErrorReturn:
  2589. fResult = FALSE;
  2590. goto CommonReturn;
  2591. TRACE_ERROR_EX(DBG_SS_TRUST, InvalidDirectoryAttr)
  2592. TRACE_ERROR_EX(DBG_SS_TRUST, GetFileAttrError)
  2593. TRACE_ERROR_EX(DBG_SS_TRUST, CreateDirectoryError)
  2594. TRACE_ERROR_EX(DBG_SS_TRUST, BadDirectoryPath)
  2595. TRACE_ERROR_EX(DBG_SS_TRUST, ErrorMemory)
  2596. TRACE_ERROR_EX(DBG_SS_TRUST, CreateDirectory2Error)
  2597. }
  2598. //---------------------------------------------------------------------------------------
  2599. //
  2600. // _CatAdminCreatePath
  2601. //
  2602. //---------------------------------------------------------------------------------------
  2603. LPWSTR
  2604. _CatAdminCreatePath(
  2605. IN LPCWSTR pwsz1,
  2606. IN LPCWSTR pwsz2,
  2607. IN BOOL fAddEndingSlash
  2608. )
  2609. {
  2610. LPWSTR pwszTemp = NULL;
  2611. int nTotalLen = 0;
  2612. int nLenStr1 = 0;
  2613. //
  2614. // Calculate the length of the resultant string as the sum of the length
  2615. // of pwsz1, length of pwsz2, a NULL char, and a possible extra '\' char
  2616. //
  2617. nLenStr1 = wcslen(pwsz1);
  2618. nTotalLen = nLenStr1 + wcslen(pwsz2) + 2;
  2619. if (fAddEndingSlash)
  2620. {
  2621. nTotalLen++;
  2622. }
  2623. //
  2624. // Allocate the string and copy pwsz1 into the buffer
  2625. //
  2626. if (NULL == (pwszTemp = (LPWSTR) malloc(sizeof(WCHAR) * nTotalLen)))
  2627. {
  2628. CATADMIN_SETERR_LOG_RETURN(ERROR_NOT_ENOUGH_MEMORY, ErrorMemory)
  2629. }
  2630. wcscpy(pwszTemp, pwsz1);
  2631. //
  2632. // Add the extra '\' if needed
  2633. //
  2634. if (pwsz1[nLenStr1 - 1] != L'\\')
  2635. {
  2636. wcscat(pwszTemp, L"\\");
  2637. }
  2638. //
  2639. // Tack on pwsz2
  2640. //
  2641. wcscat(pwszTemp, pwsz2);
  2642. if (fAddEndingSlash)
  2643. {
  2644. wcscat(pwszTemp, L"\\");
  2645. }
  2646. CommonReturn:
  2647. return (pwszTemp);
  2648. ErrorReturn:
  2649. goto CommonReturn;
  2650. TRACE_ERROR_EX(DBG_SS_CATDBSVC, ErrorMemory)
  2651. }
  2652. //
  2653. // Kept so that old dlls linking to this function in wintrust.dll
  2654. // don't get an unresolved external.
  2655. //
  2656. EXTERN_C
  2657. BOOL WINAPI
  2658. CatalogCompactHashDatabase (
  2659. IN LPCWSTR pwszDbLock,
  2660. IN LPCWSTR pwszDbDirectory,
  2661. IN LPCWSTR pwszDbName,
  2662. IN OPTIONAL LPCWSTR pwszUnwantedCatalog
  2663. )
  2664. {
  2665. return (TRUE);
  2666. }