Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

6242 lines
132 KiB

  1. //+--------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1996 - 1999
  5. //
  6. // File: db.cpp
  7. //
  8. // Contents: Cert Server Database interface implementation
  9. //
  10. //---------------------------------------------------------------------------
  11. #include <pch.cpp>
  12. #pragma hdrstop
  13. #include "csprop.h"
  14. #include "db.h"
  15. #include "column.h"
  16. #include "row.h"
  17. #include "view.h"
  18. #include "backup.h"
  19. #include "restore.h"
  20. #include "dbw.h"
  21. #define __dwFILE__ __dwFILE_CERTDB_DB_CPP__
  22. #define SEEKPOS_FIRST 0
  23. #define SEEKPOS_LAST 1
  24. #define SEEKPOS_INDEXFIRST 2
  25. #define SEEKPOS_INDEXLAST 3
  26. LONG g_cCertDB = 0;
  27. LONG g_cCertDBTotal = 0;
  28. LONG g_cXactCommit = 0;
  29. LONG g_cXactAbort = 0;
  30. LONG g_cXactTotal = 0;
  31. char *g_pszDBFile = NULL;
  32. typedef struct _DBJETPARM {
  33. DWORD paramid;
  34. DWORD lParam;
  35. char *pszParam;
  36. BOOL fString;
  37. } DBJETPARM;
  38. DBJETPARM g_aParm[] = {
  39. #define JP_LOGPATH 0
  40. { JET_paramLogFilePath, 0, NULL, TRUE },
  41. #define JP_SYSTEMPATH 1
  42. { JET_paramSystemPath, 0, NULL, TRUE },
  43. #define JP_TEMPPATH 2
  44. { JET_paramTempPath, 0, NULL, TRUE },
  45. #define JP_EVENTSOURCE 3
  46. { JET_paramEventSource, 0, NULL, TRUE },
  47. #define JP_SESSIONMAX 4
  48. { JET_paramMaxSessions, 0, NULL, FALSE },
  49. #define JP_CACHESIZEMIN 5
  50. { JET_paramCacheSizeMin, 64, NULL, FALSE },
  51. #define JP_CACHESIZEMAX 6
  52. { JET_paramCacheSizeMax, 512, NULL, FALSE },
  53. #define JP_VERPAGESMAX 7 // 16k units: 64k per session
  54. { JET_paramMaxVerPages, 4 * DBSESSIONCOUNTDEFAULT, NULL, FALSE },
  55. { JET_paramRecovery, 0, "on", TRUE },
  56. { JET_paramMaxVerPages, 64, NULL, FALSE },
  57. { JET_paramMaxTemporaryTables, 5, NULL, FALSE },
  58. { JET_paramLogBuffers, 41, NULL, FALSE },
  59. { JET_paramLogFileSize, 1024, NULL, FALSE },
  60. { JET_paramAssertAction, JET_AssertBreak, NULL, FALSE },
  61. { JET_paramBaseName, 0, szDBBASENAMEPARM, TRUE } // "edb"
  62. };
  63. #define CDBPARM (sizeof(g_aParm)/sizeof(g_aParm[0]))
  64. VOID
  65. DBFreeParms()
  66. {
  67. if (NULL != g_aParm[JP_LOGPATH].pszParam)
  68. {
  69. LocalFree(g_aParm[JP_LOGPATH].pszParam);
  70. g_aParm[JP_LOGPATH].pszParam = NULL;
  71. }
  72. if (NULL != g_aParm[JP_SYSTEMPATH].pszParam)
  73. {
  74. LocalFree(g_aParm[JP_SYSTEMPATH].pszParam);
  75. g_aParm[JP_SYSTEMPATH].pszParam = NULL;
  76. }
  77. if (NULL != g_aParm[JP_TEMPPATH].pszParam)
  78. {
  79. LocalFree(g_aParm[JP_TEMPPATH].pszParam);
  80. g_aParm[JP_TEMPPATH].pszParam = NULL;
  81. }
  82. if (NULL != g_aParm[JP_EVENTSOURCE].pszParam)
  83. {
  84. LocalFree(g_aParm[JP_EVENTSOURCE].pszParam);
  85. g_aParm[JP_EVENTSOURCE].pszParam = NULL;
  86. }
  87. }
  88. HRESULT
  89. DBInitParms(
  90. IN DWORD cSession,
  91. IN BOOL fCircularLogging,
  92. OPTIONAL IN WCHAR const *pwszEventSource,
  93. OPTIONAL IN WCHAR const *pwszLogDir,
  94. OPTIONAL IN WCHAR const *pwszSystemDir,
  95. OPTIONAL IN WCHAR const *pwszTempDir,
  96. OUT JET_INSTANCE *pInstance)
  97. {
  98. HRESULT hr = E_OUTOFMEMORY;
  99. DBJETPARM const *pjp;
  100. WCHAR awc[MAX_PATH];
  101. DWORD dwPerfTweak = 0;
  102. DBFreeParms();
  103. if (NULL != pwszLogDir)
  104. {
  105. wcscpy(awc, pwszLogDir);
  106. wcscat(awc, L"\\");
  107. if (!ConvertWszToSz(&g_aParm[JP_LOGPATH].pszParam, awc, -1))
  108. {
  109. _JumpError(hr, error, "ConvertWszToSz(LogDir)");
  110. }
  111. }
  112. if (NULL != pwszSystemDir)
  113. {
  114. wcscpy(awc, pwszSystemDir);
  115. wcscat(awc, L"\\");
  116. if (!ConvertWszToSz(&g_aParm[JP_SYSTEMPATH].pszParam, awc, -1))
  117. {
  118. _JumpError(hr, error, "ConvertWszToSz(SystemDir)");
  119. }
  120. }
  121. if (NULL != pwszTempDir)
  122. {
  123. wcscpy(awc, pwszTempDir);
  124. wcscat(awc, L"\\");
  125. if (!ConvertWszToSz(&g_aParm[JP_TEMPPATH].pszParam, awc, -1))
  126. {
  127. _JumpError(hr, error, "ConvertWszToSz(TempDir)");
  128. }
  129. }
  130. if (NULL != pwszEventSource)
  131. {
  132. if (!ConvertWszToSz(
  133. &g_aParm[JP_EVENTSOURCE].pszParam,
  134. pwszEventSource,
  135. -1))
  136. {
  137. _JumpError(hr, error, "ConvertWszToSz(EventSource)");
  138. }
  139. }
  140. g_aParm[JP_SESSIONMAX].lParam = cSession + 1;
  141. if (8 * cSession > g_aParm[JP_CACHESIZEMIN].lParam)
  142. {
  143. g_aParm[JP_CACHESIZEMIN].lParam = 8 * cSession;
  144. }
  145. if (8 * 8 * cSession > g_aParm[JP_CACHESIZEMAX].lParam)
  146. {
  147. g_aParm[JP_CACHESIZEMAX].lParam = 8 * 8 * cSession;
  148. }
  149. if (4 * cSession > g_aParm[JP_VERPAGESMAX].lParam)
  150. {
  151. g_aParm[JP_VERPAGESMAX].lParam = 4 * cSession;
  152. }
  153. for (pjp = g_aParm; pjp < &g_aParm[CDBPARM]; pjp++)
  154. {
  155. if (!pjp->fString || NULL != pjp->pszParam)
  156. {
  157. _dbgJetSetSystemParameter(
  158. pInstance,
  159. 0,
  160. pjp->paramid,
  161. pjp->lParam,
  162. pjp->pszParam);
  163. }
  164. }
  165. if (fCircularLogging)
  166. {
  167. DBGPRINT((DBG_SS_CERTSRV, "Jet: circular logging enabled\n"));
  168. _dbgJetSetSystemParameter(
  169. pInstance,
  170. 0,
  171. JET_paramCircularLog,
  172. TRUE,
  173. NULL);
  174. }
  175. if (S_OK == myGetCertRegDWValue(NULL, NULL, NULL, L"PerfTweak", &dwPerfTweak))
  176. {
  177. if (dwPerfTweak & 0x1)
  178. {
  179. _dbgJetSetSystemParameter(
  180. pInstance,
  181. 0,
  182. JET_paramLogBuffers,
  183. 480, // should be logfilesize (1024k) - 64k, specified in 512b units
  184. NULL);
  185. }
  186. if (dwPerfTweak & 0x2)
  187. {
  188. _dbgJetSetSystemParameter(
  189. pInstance,
  190. 0,
  191. JET_paramCommitDefault,
  192. JET_bitCommitLazyFlush,
  193. NULL);
  194. }
  195. if (dwPerfTweak & 0x4)
  196. {
  197. // real fix is not to set this at all, but setting it to a large number should suffice
  198. _dbgJetSetSystemParameter(
  199. pInstance,
  200. 0,
  201. JET_paramCacheSizeMax,
  202. 512*100, // 100x the size we usually run with
  203. NULL);
  204. }
  205. if (dwPerfTweak & 0x8)
  206. {
  207. _dbgJetSetSystemParameter(
  208. pInstance,
  209. 0,
  210. JET_paramCheckpointDepthMax,
  211. 60 * 1024 * 1024, // 60MB -- triple the size we usually run with (20MB)
  212. NULL);
  213. }
  214. if (dwPerfTweak & 0x10)
  215. {
  216. _dbgJetSetSystemParameter(
  217. pInstance,
  218. 0,
  219. JET_paramLogFileSize,
  220. 16384, // 16x the size we usually run with (1MB)
  221. NULL);
  222. }
  223. if (dwPerfTweak & 0x20)
  224. {
  225. _dbgJetSetSystemParameter(
  226. pInstance,
  227. 0,
  228. JET_paramLogBuffers,
  229. 256, // usually run with 41
  230. NULL);
  231. }
  232. }
  233. hr = S_OK;
  234. error:
  235. return(hr);
  236. }
  237. #if DBG_CERTSRV
  238. WCHAR const *
  239. wszCSFFlags(
  240. IN LONG Flags)
  241. {
  242. static WCHAR s_awc[256];
  243. wsprintf(s_awc, L"{%x", Flags);
  244. if (CSF_INUSE & Flags) dbgcat(s_awc, L"InUse");
  245. if (CSF_READONLY & Flags) dbgcat(s_awc, L"ReadOnly");
  246. if (CSF_CREATE & Flags) dbgcat(s_awc, L"Create");
  247. if (CSF_VIEW & Flags) dbgcat(s_awc, L"View");
  248. if (CSF_VIEWRESET & Flags) dbgcat(s_awc, L"ViewReset");
  249. wcscat(s_awc, L"}");
  250. CSASSERT(wcslen(s_awc) < ARRAYSIZE(s_awc));
  251. return(s_awc);
  252. }
  253. WCHAR const *
  254. wszCSTFlags(
  255. IN LONG Flags)
  256. {
  257. static WCHAR s_awc[256];
  258. wsprintf(s_awc, L"{%x", Flags);
  259. if (CST_SEEKINDEXRANGE & Flags) dbgcat(s_awc, L"IndexRange");
  260. if (CST_SEEKNOTMOVE & Flags) dbgcat(s_awc, L"SeekNotMove");
  261. if (CST_SEEKUSECURRENT & Flags) dbgcat(s_awc, L"UseCurrent");
  262. if (0 == (CST_SEEKUSECURRENT & Flags)) dbgcat(s_awc, L"SkipCurrent");
  263. if (CST_SEEKASCEND & Flags) dbgcat(s_awc, L"Ascend");
  264. if (0 == (CST_SEEKASCEND & Flags)) dbgcat(s_awc, L"Descend");
  265. wcscat(s_awc, L"}");
  266. CSASSERT(wcslen(s_awc) < ARRAYSIZE(s_awc));
  267. return(s_awc);
  268. }
  269. WCHAR const *
  270. wszTable(
  271. IN DWORD dwTable)
  272. {
  273. WCHAR const *pwsz;
  274. switch (dwTable)
  275. {
  276. case TABLE_REQUESTS:
  277. pwsz = wszREQUESTTABLE;
  278. break;
  279. case TABLE_CERTIFICATES:
  280. pwsz = wszCERTIFICATETABLE;
  281. break;
  282. case TABLE_ATTRIBUTES:
  283. pwsz = wszREQUESTATTRIBUTETABLE;
  284. break;
  285. case TABLE_EXTENSIONS:
  286. pwsz = wszCERTIFICATEEXTENSIONTABLE;
  287. break;
  288. case TABLE_CRLS:
  289. pwsz = wszCRLTABLE;
  290. break;
  291. default:
  292. pwsz = L"???";
  293. break;
  294. }
  295. return(pwsz);
  296. }
  297. WCHAR const *
  298. wszSeekOperator(
  299. IN LONG SeekOperator)
  300. {
  301. WCHAR const *pwsz;
  302. static WCHAR s_wszBuf[20];
  303. switch (CVR_SEEK_MASK & SeekOperator)
  304. {
  305. case CVR_SEEK_NONE: pwsz = L"None"; break;
  306. case CVR_SEEK_EQ: pwsz = L"=="; break;
  307. case CVR_SEEK_LT: pwsz = L"<"; break;
  308. case CVR_SEEK_LE: pwsz = L"<="; break;
  309. case CVR_SEEK_GE: pwsz = L">="; break;
  310. case CVR_SEEK_GT: pwsz = L">"; break;
  311. default:
  312. wsprintf(s_wszBuf, L"???=%x", SeekOperator);
  313. pwsz = s_wszBuf;
  314. break;
  315. }
  316. if (s_wszBuf != pwsz && (CVR_SEEK_NODELTA & SeekOperator))
  317. {
  318. wcscpy(s_wszBuf, pwsz);
  319. wcscat(s_wszBuf, L",NoDelta");
  320. pwsz = s_wszBuf;
  321. }
  322. return(pwsz);
  323. }
  324. WCHAR const *
  325. wszSortOperator(
  326. IN LONG SortOrder)
  327. {
  328. WCHAR const *pwsz;
  329. static WCHAR s_wszBuf[20];
  330. switch (SortOrder)
  331. {
  332. case CVR_SORT_NONE: pwsz = L"None"; break;
  333. case CVR_SORT_ASCEND: pwsz = L"Ascend"; break;
  334. case CVR_SORT_DESCEND: pwsz = L"Descend"; break;
  335. default:
  336. wsprintf(s_wszBuf, L"???=%x", SortOrder);
  337. pwsz = s_wszBuf;
  338. break;
  339. }
  340. return(pwsz);
  341. }
  342. VOID
  343. dbDumpFileTime(
  344. IN DWORD dwSubSystemId,
  345. IN CHAR const *pszPrefix,
  346. IN FILETIME const *pft)
  347. {
  348. HRESULT hr;
  349. WCHAR *pwsz;
  350. hr = myGMTFileTimeToWszLocalTime(pft, TRUE, &pwsz);
  351. if (S_OK == hr)
  352. {
  353. DBGPRINT((dwSubSystemId, "%hs%ws\n", pszPrefix, pwsz));
  354. LocalFree(pwsz);
  355. }
  356. }
  357. VOID
  358. dbDumpValue(
  359. IN DWORD dwSubSystemId,
  360. OPTIONAL IN DBTABLE const *pdt,
  361. IN BYTE const *pbValue,
  362. IN DWORD cbValue)
  363. {
  364. if (NULL != pdt && NULL != pbValue && ISTEXTCOLTYP(pdt->dbcoltyp))
  365. {
  366. cbValue += sizeof(WCHAR);
  367. }
  368. if (JET_coltypDateTime == pdt->dbcoltyp && sizeof(FILETIME) == cbValue)
  369. {
  370. dbDumpFileTime(dwSubSystemId, "", (FILETIME const *) pbValue);
  371. }
  372. DBGDUMPHEX((dwSubSystemId, DH_NOADDRESS, pbValue, cbValue));
  373. }
  374. VOID
  375. CCertDB::DumpRestriction(
  376. IN DWORD dwSubSystemId,
  377. IN LONG i,
  378. IN CERTVIEWRESTRICTION const *pcvr)
  379. {
  380. HRESULT hr;
  381. WCHAR wszColumn[20];
  382. DBTABLE const *pdt;
  383. WCHAR const *pwszTable = L"???";
  384. WCHAR const *pwszCol;
  385. hr = _MapPropIdIndex(pcvr->ColumnIndex, &pdt, NULL);
  386. if (S_OK != hr)
  387. {
  388. _PrintError(hr, "_MapPropIdIndex");
  389. wsprintf(wszColumn, L"???=%x", pcvr->ColumnIndex);
  390. pdt = NULL;
  391. pwszCol = wszColumn;
  392. }
  393. else
  394. {
  395. pwszCol = pdt->pwszPropName;
  396. pwszTable = wszTable(pdt->dwTable);
  397. }
  398. DBGPRINT((
  399. dwSubSystemId,
  400. "Restriction[%d]: Col=%ws.%ws\n"
  401. " Seek='%ws' Sort=%ws cb=%x, pb=%x\n",
  402. i,
  403. pwszTable,
  404. pwszCol,
  405. wszSeekOperator(pcvr->SeekOperator),
  406. wszSortOperator(pcvr->SortOrder),
  407. pcvr->cbValue,
  408. pcvr->pbValue));
  409. dbDumpValue(dwSubSystemId, pdt, pcvr->pbValue, pcvr->cbValue);
  410. }
  411. VOID
  412. dbDumpColumn(
  413. IN DWORD dwSubSystemId,
  414. IN DBTABLE const *pdt,
  415. IN BYTE const *pbValue,
  416. IN DWORD cbValue)
  417. {
  418. DBGPRINT((dwSubSystemId, "Column: cb=%x pb=%x\n", cbValue, pbValue));
  419. dbDumpValue(dwSubSystemId, pdt, pbValue, cbValue);
  420. }
  421. DBAUXDATA const *
  422. dbGetAuxTable(
  423. IN CERTSESSION *pcs,
  424. IN JET_TABLEID tableid)
  425. {
  426. DBAUXDATA const *pdbaux;
  427. CSASSERT(IsValidJetTableId(tableid));
  428. if (tableid == pcs->aTable[CSTI_CERTIFICATE].TableId)
  429. {
  430. pdbaux = &g_dbauxCertificates;
  431. }
  432. else if (tableid == pcs->aTable[CSTI_ATTRIBUTE].TableId)
  433. {
  434. pdbaux = &g_dbauxAttributes;
  435. }
  436. else if (tableid == pcs->aTable[CSTI_EXTENSION].TableId)
  437. {
  438. pdbaux = &g_dbauxExtensions;
  439. }
  440. else
  441. {
  442. CSASSERT(tableid == pcs->aTable[CSTI_PRIMARY].TableId);
  443. pdbaux = &g_dbauxRequests;
  444. switch (CSF_TABLEMASK & pcs->SesFlags)
  445. {
  446. case TABLE_CERTIFICATES:
  447. pdbaux = &g_dbauxCertificates;
  448. break;
  449. case TABLE_ATTRIBUTES:
  450. pdbaux = &g_dbauxAttributes;
  451. break;
  452. case TABLE_EXTENSIONS:
  453. pdbaux = &g_dbauxExtensions;
  454. break;
  455. case TABLE_CRLS:
  456. pdbaux = &g_dbauxCRLs;
  457. break;
  458. }
  459. }
  460. return(pdbaux);
  461. }
  462. HRESULT
  463. CCertDB::_DumpRowId(
  464. IN CHAR const *psz,
  465. IN CERTSESSION *pcs,
  466. IN JET_TABLEID tableid)
  467. {
  468. HRESULT hr;
  469. #define DBG_SS_DUMPREQUESTID DBG_SS_CERTDBI
  470. CSASSERT(IsValidJetTableId(tableid));
  471. if (DbgIsSSActive(DBG_SS_DUMPREQUESTID))
  472. {
  473. DWORD cb;
  474. DWORD dwTmp;
  475. DBAUXDATA const *pdbaux = dbGetAuxTable(pcs, tableid);
  476. WCHAR awchr[cwcHRESULTSTRING];
  477. cb = sizeof(dwTmp);
  478. hr = _RetrieveColumn(
  479. pcs,
  480. tableid,
  481. pdbaux->pdtRowId->dbcolumnid,
  482. pdbaux->pdtRowId->dbcoltyp,
  483. &cb,
  484. (BYTE *) &dwTmp);
  485. if (S_OK != hr)
  486. {
  487. DBGPRINT((
  488. DBG_SS_DUMPREQUESTID,
  489. "%hs: %hs.RowId: pcs=%d: %ws\n",
  490. psz,
  491. pdbaux->pszTable,
  492. pcs->RowId,
  493. myHResultToString(awchr, hr)));
  494. _JumpError2(hr, error, "_RetrieveColumn", hr);
  495. }
  496. DBGPRINT((
  497. DBG_SS_DUMPREQUESTID,
  498. "%hs: %hs.RowId: pcs=%d dbcol=%d\n",
  499. psz,
  500. pdbaux->pszTable,
  501. pcs->RowId,
  502. dwTmp));
  503. }
  504. hr = S_OK;
  505. error:
  506. return(hr);
  507. }
  508. HRESULT
  509. CCertDB::_DumpColumn(
  510. IN CHAR const *psz,
  511. IN CERTSESSION *pcs,
  512. IN JET_TABLEID tableid,
  513. IN DBTABLE const *pdt)
  514. {
  515. HRESULT hr;
  516. CSASSERT(IsValidJetTableId(tableid));
  517. CSASSERT(0 != pdt->dbcolumnid);
  518. if (DbgIsSSActive(DBG_SS_CERTDBI))
  519. {
  520. DWORD cb;
  521. BYTE ab[64 * 1024];
  522. DBAUXDATA const *pdbaux = dbGetAuxTable(pcs, tableid);
  523. BOOL fIsText;
  524. cb = sizeof(ab);
  525. hr = _RetrieveColumn(
  526. pcs,
  527. tableid,
  528. pdt->dbcolumnid,
  529. pdt->dbcoltyp,
  530. &cb,
  531. ab);
  532. _JumpIfError(hr, error, "_RetrieveColumn");
  533. fIsText = ISTEXTCOLTYP(pdt->dbcoltyp);
  534. DBGPRINT((
  535. DBG_SS_CERTDBI,
  536. "%hs: _DumpColumn(%hs, %hs): Value:%hs%ws%hs\n",
  537. psz,
  538. pdbaux->pszTable,
  539. pdt->pszFieldName,
  540. fIsText? " '" : "",
  541. fIsText? (WCHAR *) ab : L"",
  542. fIsText? "'" : ""));
  543. dbDumpValue(DBG_SS_CERTDBI, pdt, ab, cb);
  544. }
  545. hr = S_OK;
  546. error:
  547. return(hr);
  548. }
  549. #endif // DBG_CERTSRV
  550. CCertDB::CCertDB()
  551. {
  552. HRESULT hr;
  553. InterlockedIncrement(&g_cCertDB);
  554. InterlockedIncrement(&g_cCertDBTotal);
  555. m_Instance = 0;
  556. m_fDBOpen = FALSE;
  557. m_fDBRestart = FALSE;
  558. m_fPendingShutDown = FALSE;
  559. m_fFoundOldColumns = FALSE;
  560. m_fAddedNewColumns = FALSE;
  561. m_aSession = NULL;
  562. m_cSession = 0;
  563. m_cbPage = 0;
  564. m_cCritSec = 0;
  565. __try
  566. {
  567. InitializeCriticalSection(&m_critsecSession);
  568. m_cCritSec++;
  569. InitializeCriticalSection(&m_critsecAutoIncTables);
  570. m_cCritSec++;
  571. }
  572. __except(hr = myHEXCEPTIONCODE(), EXCEPTION_EXECUTE_HANDLER)
  573. {
  574. }
  575. }
  576. CCertDB::~CCertDB()
  577. {
  578. ShutDown(0);
  579. if (0 < m_cCritSec)
  580. {
  581. DeleteCriticalSection(&m_critsecSession);
  582. if (1 < m_cCritSec)
  583. {
  584. DeleteCriticalSection(&m_critsecAutoIncTables);
  585. }
  586. }
  587. InterlockedDecrement(&g_cCertDB);
  588. }
  589. STDMETHODIMP
  590. CCertDB::Open(
  591. /* [in] */ DWORD Flags,
  592. /* [in] */ DWORD cSession,
  593. /* [in] */ WCHAR const *pwszEventSource,
  594. /* [in] */ WCHAR const *pwszDBFile,
  595. /* [in] */ WCHAR const *pwszLogDir,
  596. /* [in] */ WCHAR const *pwszSystemDir,
  597. /* [in] */ WCHAR const *pwszTempDir)
  598. {
  599. HRESULT hr;
  600. DWORD i;
  601. DBCREATETABLE const *pct;
  602. JET_GRBIT grbit;
  603. DWORD CreateFlags;
  604. CERTSESSION *pcs = NULL;
  605. if (NULL == pwszDBFile ||
  606. NULL == pwszLogDir ||
  607. NULL == pwszSystemDir ||
  608. NULL == pwszTempDir)
  609. {
  610. hr = E_POINTER;
  611. _JumpError(hr, error, "NULL parm");
  612. }
  613. hr = InitGlobalWriterState();
  614. // _JumpIfError(hr, error, "InitGlobalWriterState");
  615. m_fDBOpen = FALSE;
  616. m_fDBRestart = FALSE;
  617. m_fDBReadOnly = (CDBOPEN_READONLY & Flags)? TRUE : FALSE;
  618. CSASSERT(NULL == m_aSession); // code assumes we do not have session
  619. m_cSession = 0;
  620. m_aSession = (CERTSESSION *) LocalAlloc(
  621. LMEM_FIXED | LMEM_ZEROINIT,
  622. cSession * sizeof(m_aSession[0]));
  623. hr = E_OUTOFMEMORY;
  624. if (NULL == m_aSession)
  625. {
  626. _JumpError(hr, error, "LocalAlloc(m_aSession)");
  627. }
  628. for (i = 0; i < cSession; i++)
  629. {
  630. m_aSession[i].SesId = -1;
  631. m_aSession[i].DBId = -1;
  632. }
  633. if (!ConvertWszToSz(&g_pszDBFile, pwszDBFile, -1))
  634. {
  635. _JumpError(hr, error, "ConvertWszToSz(DBFile)");
  636. }
  637. hr = DBInitParms(
  638. cSession,
  639. (CDBOPEN_CIRCULARLOGGING & Flags)? TRUE : FALSE,
  640. pwszEventSource,
  641. pwszLogDir,
  642. pwszSystemDir,
  643. pwszTempDir,
  644. &m_Instance);
  645. _JumpIfError(hr, error, "DBInitParms");
  646. hr = _dbgJetInit(&m_Instance);
  647. if ((HRESULT) JET_errLogFileSizeMismatchDatabasesConsistent == hr ||
  648. (HRESULT) JET_errLogFileSizeMismatch == hr)
  649. {
  650. _PrintError(hr, "JetInit(old log file size)");
  651. _dbgJetSetSystemParameter(
  652. &m_Instance,
  653. 0,
  654. JET_paramLogFileSize,
  655. 1000,
  656. NULL);
  657. hr = _dbgJetInit(&m_Instance);
  658. }
  659. _JumpIfError(
  660. hr,
  661. error,
  662. JET_errFileAccessDenied == hr?
  663. "JetInit(Server already running?)" :
  664. "JetInit(JetSetSystemParameter problem?)");
  665. for (i = 0; i < cSession; i++)
  666. {
  667. hr = _dbgJetBeginSession(m_Instance, &m_aSession[i].SesId, NULL, NULL);
  668. _JumpIfError(hr, error, "_dbgJetBeginSession");
  669. m_cSession++;
  670. if (0 == i)
  671. {
  672. CreateFlags = 0;
  673. grbit = m_fDBReadOnly?
  674. JET_bitDbReadOnly : JET_bitDbDeleteCorruptIndexes;
  675. hr = _dbgJetAttachDatabase(
  676. m_aSession[i].SesId,
  677. g_pszDBFile,
  678. grbit);
  679. if ((HRESULT) JET_errFileNotFound == hr &&
  680. (CDBOPEN_CREATEIFNEEDED & Flags))
  681. {
  682. DBGPRINT((DBG_SS_CERTDB, "Creating Database\n"));
  683. CreateFlags |= CF_DATABASE;
  684. }
  685. else
  686. if ((HRESULT) JET_wrnCorruptIndexDeleted == hr)
  687. {
  688. // Rebuild deleted indexes over Unicode columns...
  689. DBGPRINT((DBG_SS_CERTDB, "Creating Database Indexes\n"));
  690. CreateFlags |= CF_MISSINGINDEXES;
  691. }
  692. else
  693. if ((HRESULT) JET_wrnDatabaseAttached != hr)
  694. {
  695. _JumpIfError(hr, error, "JetAttachDatabase");
  696. }
  697. if (m_fDBReadOnly)
  698. {
  699. if (CreateFlags)
  700. {
  701. hr = E_ACCESSDENIED;
  702. _JumpError(hr, error, "ReadOnly");
  703. }
  704. }
  705. else
  706. {
  707. CreateFlags |= CF_MISSINGTABLES | CF_MISSINGCOLUMNS;
  708. hr = _Create(CreateFlags, g_pszDBFile);
  709. _JumpIfError(hr, error, "_Create");
  710. }
  711. }
  712. hr = _dbgJetOpenDatabase(
  713. m_aSession[i].SesId,
  714. g_pszDBFile,
  715. NULL,
  716. &m_aSession[i].DBId,
  717. 0);
  718. _JumpIfError(hr, error, "JetOpenDatabase");
  719. }
  720. hr = _AllocateSession(&pcs);
  721. _JumpIfError(hr, error, "_AllocateSession");
  722. for (pct = g_actDataBase; NULL != pct->pszTableName; pct++)
  723. {
  724. hr = _BuildColumnIds(pcs, pct->pszTableName, pct->pdt);
  725. _JumpIfError(hr, error, "_BuildColumnIds");
  726. }
  727. if (!m_fDBReadOnly)
  728. {
  729. for (pct = g_actDataBase; NULL != pct->pszTableName; pct++)
  730. {
  731. hr = _ConvertOldColumnData(
  732. pcs,
  733. pct->pszTableName,
  734. pct->pdbaux,
  735. pct->pdt);
  736. _JumpIfError(hr, error, "_ConvertOldColumnData");
  737. }
  738. }
  739. m_fDBOpen = TRUE;
  740. error:
  741. if (NULL != pcs)
  742. {
  743. ReleaseSession(pcs);
  744. }
  745. hr = myJetHResult(hr);
  746. if (S_OK == hr && m_fDBRestart)
  747. {
  748. hr = S_FALSE; // Restart required for DB changes to take effect.
  749. _PrintError(hr, "m_fDBRestart");
  750. }
  751. return(hr);
  752. }
  753. STDMETHODIMP
  754. CCertDB::ShutDown(
  755. /* [in] */ DWORD dwFlags)
  756. {
  757. HRESULT hr;
  758. DWORD i;
  759. if (CDBSHUTDOWN_PENDING == dwFlags)
  760. {
  761. m_fPendingShutDown = TRUE;
  762. goto error;
  763. }
  764. if (NULL != m_aSession)
  765. {
  766. DBGPRINT((DBG_SS_CERTDB, "Database shutdown...\n"));
  767. for (i = 0; i < m_cSession; i++)
  768. {
  769. hr = _dbgJetEndSession(
  770. m_aSession[i].SesId,
  771. JET_bitForceSessionClosed);
  772. _PrintIfError(hr, "JetEndSession");
  773. }
  774. hr = _dbgJetTerm2(m_Instance, JET_bitTermComplete);
  775. DBGPRINT((DBG_SS_CERTDB, "Database shutdown complete\n"));
  776. LocalFree(m_aSession);
  777. m_aSession = NULL;
  778. }
  779. if (NULL != g_pszDBFile)
  780. {
  781. LocalFree(g_pszDBFile);
  782. g_pszDBFile = NULL;
  783. }
  784. DBFreeParms();
  785. UnInitGlobalWriterState();
  786. error:
  787. return(S_OK);
  788. }
  789. HRESULT
  790. CCertDB::BeginTransaction(
  791. IN CERTSESSION *pcs,
  792. IN BOOL fPrepareUpdate)
  793. {
  794. HRESULT hr;
  795. BOOL fTransacted = FALSE;
  796. DWORD i;
  797. if (NULL == pcs)
  798. {
  799. hr = E_POINTER;
  800. _JumpError(hr, error, "NULL parm");
  801. }
  802. if (0 != pcs->cTransact)
  803. {
  804. hr = E_UNEXPECTED;
  805. _JumpError(hr, error, "Nested transaction");
  806. }
  807. CSASSERTTHREAD(pcs);
  808. hr = _dbgJetBeginTransaction(pcs->SesId);
  809. _JumpIfError(hr, error, "JetBeginTransaction");
  810. fTransacted = TRUE;
  811. if (fPrepareUpdate)
  812. {
  813. CSASSERTTHREAD(pcs);
  814. for (i = 0; i < CSTI_MAX; i++)
  815. {
  816. if (IsValidJetTableId(pcs->aTable[i].TableId))
  817. {
  818. hr = _dbgJetPrepareUpdate(
  819. pcs->SesId,
  820. pcs->aTable[i].TableId,
  821. JET_prepReplace);
  822. _JumpIfError(hr, error, "JetPrepareUpdate");
  823. }
  824. }
  825. }
  826. pcs->cTransact++;
  827. InterlockedIncrement(&g_cXactTotal);
  828. hr = S_OK;
  829. error:
  830. if (S_OK != hr && fTransacted)
  831. {
  832. HRESULT hr2;
  833. CSASSERTTHREAD(pcs);
  834. hr2 = _Rollback(pcs);
  835. _PrintIfError(hr2, "_Rollback");
  836. }
  837. return(myJetHResult(hr));
  838. }
  839. HRESULT
  840. CCertDB::CommitTransaction(
  841. IN CERTSESSION *pcs,
  842. IN BOOL fCommit)
  843. {
  844. HRESULT hr;
  845. DWORD i;
  846. if (NULL == pcs)
  847. {
  848. hr = E_POINTER;
  849. _JumpError(hr, error, "NULL parm");
  850. }
  851. CSASSERT(0 != pcs->cTransact);
  852. if (fCommit)
  853. {
  854. if (0 == (CSF_DELETE & pcs->SesFlags))
  855. {
  856. for (i = 0; i < CSTI_MAXDIRECT; i++)
  857. {
  858. if (IsValidJetTableId(pcs->aTable[i].TableId))
  859. {
  860. hr = _UpdateTable(pcs, pcs->aTable[i].TableId);
  861. _JumpIfError(hr, error, "_UpdateTable");
  862. }
  863. }
  864. }
  865. CSASSERTTHREAD(pcs);
  866. hr = _dbgJetCommitTransaction(pcs->SesId, 0);
  867. _JumpIfError(hr, error, "JetCommitTransaction");
  868. }
  869. else
  870. {
  871. hr = _Rollback(pcs);
  872. _JumpIfError(hr, error, "_Rollback");
  873. }
  874. pcs->cTransact--;
  875. if (fCommit)
  876. {
  877. InterlockedIncrement(&g_cXactCommit);
  878. }
  879. else
  880. {
  881. InterlockedIncrement(&g_cXactAbort);
  882. }
  883. error:
  884. return(myJetHResult(hr));
  885. }
  886. HRESULT
  887. CCertDB::_AllocateSession(
  888. OUT CERTSESSION **ppcs)
  889. {
  890. HRESULT hr;
  891. DWORD i;
  892. BOOL fEnterCritSec = FALSE;
  893. CSASSERT(NULL != ppcs);
  894. *ppcs = NULL;
  895. if (0 == m_cCritSec)
  896. {
  897. hr = HRESULT_FROM_WIN32(ERROR_DLL_INIT_FAILED);
  898. _JumpError(hr, error, "InitializeCriticalSection failure");
  899. }
  900. EnterCriticalSection(&m_critsecSession);
  901. fEnterCritSec = TRUE;
  902. for (i = 0; 0 != m_aSession[i].SesFlags; i++)
  903. {
  904. if (i + 1 == m_cSession)
  905. {
  906. hr = CERTSRV_E_NO_DB_SESSIONS;
  907. _JumpIfError(hr, error, "no more sessions");
  908. }
  909. }
  910. *ppcs = &m_aSession[i];
  911. CSASSERT(0 == (*ppcs)->RowId);
  912. (*ppcs)->SesFlags = CSF_INUSE;
  913. (*ppcs)->dwThreadId = GetCurrentThreadId();
  914. ZeroMemory((*ppcs)->aTable, sizeof((*ppcs)->aTable));
  915. hr = S_OK;
  916. error:
  917. if (fEnterCritSec)
  918. {
  919. LeaveCriticalSection(&m_critsecSession);
  920. }
  921. return(hr);
  922. }
  923. HRESULT
  924. CCertDB::_OpenTableRow(
  925. IN CERTSESSION *pcs,
  926. IN DBAUXDATA const *pdbaux,
  927. OPTIONAL IN CERTVIEWRESTRICTION const *pcvr,
  928. OUT CERTSESSIONTABLE *pTable,
  929. OUT DWORD *pdwRowIdMismatch)
  930. {
  931. HRESULT hr;
  932. DWORD dwRowId;
  933. DWORD cb;
  934. CSASSERT(NULL == pTable->TableId);
  935. CSASSERT(0 == pTable->TableFlags);
  936. *pdwRowIdMismatch = 0;
  937. if (CSF_CREATE & pcs->SesFlags)
  938. {
  939. CSASSERT(NULL == pcvr);
  940. CSASSERTTHREAD(pcs);
  941. hr = _dbgJetOpenTable(
  942. pcs->SesId,
  943. pcs->DBId,
  944. pdbaux->pszTable,
  945. NULL,
  946. 0,
  947. 0,
  948. &pTable->TableId);
  949. _JumpIfError(hr, error, "JetOpenTable");
  950. }
  951. else
  952. {
  953. if (NULL == pcvr)
  954. {
  955. hr = E_POINTER;
  956. _JumpError(hr, error, "NULL parm");
  957. }
  958. hr = _OpenTable(pcs, pdbaux, pcvr, pTable);
  959. if (S_FALSE == hr)
  960. {
  961. hr = CERTSRV_E_PROPERTY_EMPTY;
  962. }
  963. _JumpIfError2(hr, error, "_OpenTable", CERTSRV_E_PROPERTY_EMPTY);
  964. }
  965. if (!((CSF_READONLY | CSF_DELETE) & pcs->SesFlags))
  966. {
  967. CSASSERTTHREAD(pcs);
  968. hr = _dbgJetPrepareUpdate(
  969. pcs->SesId,
  970. pTable->TableId,
  971. (CSF_CREATE & pcs->SesFlags)?
  972. JET_prepInsert : JET_prepReplace);
  973. _JumpIfError(hr, error, "JetPrepareUpdate");
  974. }
  975. // Requests table RequestId column is JET_bitColumnAutoincrement.
  976. // Certificates table RequestId column is manually initialized here.
  977. //
  978. // When creating a Certificates table row, the RequestId column must be
  979. // set from pcs->RowId, which must already have been set by first creating
  980. // the Requests table row.
  981. //
  982. // When opening an existing row in either table, just fetch the column.
  983. CSASSERTTHREAD(pcs);
  984. hr = _dbgJetRetrieveColumn(
  985. pcs->SesId,
  986. pTable->TableId,
  987. pdbaux->pdtRowId->dbcolumnid,
  988. &dwRowId,
  989. sizeof(dwRowId),
  990. &cb,
  991. JET_bitRetrieveCopy,
  992. NULL);
  993. if ((HRESULT) JET_wrnColumnNull == hr)
  994. {
  995. hr = CERTSRV_E_PROPERTY_EMPTY;
  996. }
  997. _PrintIfError2(hr, "JetRetrieveColumn", CERTSRV_E_PROPERTY_EMPTY);
  998. if (S_OK != hr || 0 == dwRowId)
  999. {
  1000. CSASSERT(CSF_CREATE & pcs->SesFlags);
  1001. if (0 == (CSF_CREATE & pcs->SesFlags))
  1002. {
  1003. if (S_OK == hr)
  1004. {
  1005. hr = CERTSRV_E_PROPERTY_EMPTY;
  1006. }
  1007. _JumpError(hr, error, "JetRetrieveColumn");
  1008. }
  1009. dwRowId = pcs->RowId;
  1010. hr = _dbgJetSetColumn(
  1011. pcs->SesId,
  1012. pTable->TableId,
  1013. pdbaux->pdtRowId->dbcolumnid,
  1014. &dwRowId,
  1015. sizeof(dwRowId),
  1016. 0,
  1017. NULL);
  1018. _JumpIfError(hr, error, "JetSetColumn");
  1019. }
  1020. else if (0 == pcs->RowId)
  1021. {
  1022. pcs->RowId = dwRowId;
  1023. }
  1024. DBGPRINT((
  1025. DBG_SS_CERTDBI,
  1026. "_OpenTableRow:%hs %hs --> RowId=%d(dwRowId(RetrieveColumn)=%d)\n",
  1027. (CSF_CREATE & pcs->SesFlags)? " (Create)" : "",
  1028. pdbaux->pszTable,
  1029. pcs->RowId,
  1030. dwRowId));
  1031. CSASSERT(0 != pcs->RowId);
  1032. if (pcs->RowId > dwRowId)
  1033. {
  1034. *pdwRowIdMismatch = dwRowId;
  1035. hr = CERTSRV_E_PROPERTY_EMPTY;
  1036. _JumpError(hr, error, "Missing autoincrement RowId");
  1037. }
  1038. CSASSERT(pcs->RowId == dwRowId);
  1039. error:
  1040. if (S_OK != hr)
  1041. {
  1042. if (IsValidJetTableId(pTable->TableId))
  1043. {
  1044. HRESULT hr2;
  1045. CSASSERTTHREAD(pcs);
  1046. hr2 = _dbgJetCloseTable(pcs->SesId, pTable->TableId);
  1047. _PrintIfError(hr2, "JetCloseTable");
  1048. }
  1049. ZeroMemory(pTable, sizeof(*pTable));
  1050. }
  1051. return(myJetHResult(hr));
  1052. }
  1053. HRESULT
  1054. CCertDB::OpenTables(
  1055. IN CERTSESSION *pcs,
  1056. OPTIONAL IN CERTVIEWRESTRICTION const *pcvr)
  1057. {
  1058. HRESULT hr;
  1059. BOOL fCertTableFirst = FALSE;
  1060. BOOL fCertTableLast = FALSE;
  1061. CERTVIEWRESTRICTION cvrRowId;
  1062. CERTVIEWRESTRICTION const *pcvrPrimary;
  1063. CERTVIEWRESTRICTION const *pcvrCertificates;
  1064. BOOL fEnterCritSec = FALSE;
  1065. DBAUXDATA const *pdbauxPrimary;
  1066. if (NULL == pcs)
  1067. {
  1068. hr = E_POINTER;
  1069. _JumpError(hr, error, "NULL parm");
  1070. }
  1071. pcvrPrimary = pcvr;
  1072. pcvrCertificates = NULL;
  1073. pdbauxPrimary = &g_dbauxRequests;
  1074. if (TABLE_REQCERTS == (CSF_TABLEMASK & pcs->SesFlags))
  1075. {
  1076. fCertTableLast = TRUE;
  1077. if (NULL != pcvr)
  1078. {
  1079. cvrRowId.SeekOperator = CVR_SEEK_EQ;
  1080. cvrRowId.SortOrder = CVR_SORT_ASCEND;
  1081. cvrRowId.pbValue = (BYTE *) &pcs->RowId;
  1082. cvrRowId.cbValue = sizeof(pcs->RowId);
  1083. switch (DTI_TABLEMASK & pcvr->ColumnIndex)
  1084. {
  1085. case DTI_REQUESTTABLE:
  1086. pcvrCertificates = &cvrRowId;
  1087. cvrRowId.ColumnIndex = DTI_CERTIFICATETABLE | DTC_REQUESTID;
  1088. break;
  1089. case DTI_CERTIFICATETABLE:
  1090. fCertTableLast = FALSE;
  1091. fCertTableFirst = TRUE;
  1092. pcvrCertificates = pcvr;
  1093. pcvrPrimary = &cvrRowId;
  1094. cvrRowId.ColumnIndex = DTI_REQUESTTABLE | DTR_REQUESTID;
  1095. break;
  1096. default:
  1097. hr = E_INVALIDARG;
  1098. _JumpError(hr, error, "ColumnIndex Table");
  1099. }
  1100. }
  1101. }
  1102. else
  1103. {
  1104. switch (CSF_TABLEMASK & pcs->SesFlags)
  1105. {
  1106. case TABLE_ATTRIBUTES:
  1107. pdbauxPrimary = &g_dbauxAttributes;
  1108. break;
  1109. case TABLE_EXTENSIONS:
  1110. pdbauxPrimary = &g_dbauxExtensions;
  1111. break;
  1112. case TABLE_CRLS:
  1113. pdbauxPrimary = &g_dbauxCRLs;
  1114. break;
  1115. default:
  1116. hr = E_INVALIDARG;
  1117. _JumpError(hr, error, "bad table");
  1118. }
  1119. }
  1120. if (1 >= m_cCritSec)
  1121. {
  1122. hr = HRESULT_FROM_WIN32(ERROR_DLL_INIT_FAILED);
  1123. _JumpError(hr, error, "InitializeCriticalSection failure");
  1124. }
  1125. EnterCriticalSection(&m_critsecAutoIncTables);
  1126. fEnterCritSec = TRUE;
  1127. __try
  1128. {
  1129. DWORD dwRowIdMismatch;
  1130. if (fCertTableFirst)
  1131. {
  1132. hr = _OpenTableRow(
  1133. pcs,
  1134. &g_dbauxCertificates,
  1135. pcvrCertificates,
  1136. &pcs->aTable[CSTI_CERTIFICATE],
  1137. &dwRowIdMismatch);
  1138. _LeaveIfError2(hr, "_OpenTableRow", CERTSRV_E_PROPERTY_EMPTY);
  1139. CSASSERT(0 != pcs->RowId);
  1140. CSASSERT(IsValidJetTableId(pcs->aTable[CSTI_CERTIFICATE].TableId));
  1141. DBGPRINT((
  1142. DBG_SS_CERTDBI,
  1143. "OpenTables: %hs: %ws\n",
  1144. g_dbauxCertificates.pszTable,
  1145. wszCSTFlags(pcs->aTable[CSTI_CERTIFICATE].TableFlags)));
  1146. }
  1147. hr = _OpenTableRow(
  1148. pcs,
  1149. pdbauxPrimary,
  1150. pcvrPrimary,
  1151. &pcs->aTable[CSTI_PRIMARY],
  1152. &dwRowIdMismatch);
  1153. _LeaveIfError2(hr, "_OpenTableRow", CERTSRV_E_PROPERTY_EMPTY);
  1154. CSASSERT(0 != pcs->RowId);
  1155. CSASSERT(IsValidJetTableId(pcs->aTable[CSTI_PRIMARY].TableId));
  1156. DBGPRINT((
  1157. DBG_SS_CERTDBI,
  1158. "OpenTables: %hs: %ws\n",
  1159. g_dbauxRequests.pszTable,
  1160. wszCSTFlags(pcs->aTable[CSTI_PRIMARY].TableFlags)));
  1161. if (fCertTableLast)
  1162. {
  1163. while (TRUE)
  1164. {
  1165. hr = _OpenTableRow(
  1166. pcs,
  1167. &g_dbauxCertificates,
  1168. pcvrCertificates,
  1169. &pcs->aTable[CSTI_CERTIFICATE],
  1170. &dwRowIdMismatch);
  1171. _PrintIfError(hr, "_OpenTableRow");
  1172. if (S_OK == hr || 0 == dwRowIdMismatch)
  1173. {
  1174. break;
  1175. }
  1176. }
  1177. _PrintIfError(hr, "_OpenTableRow");
  1178. //_LeaveIfError(hr, "_OpenTableRow");
  1179. if (S_OK == hr)
  1180. {
  1181. CSASSERT(IsValidJetTableId(pcs->aTable[CSTI_CERTIFICATE].TableId));
  1182. DBGPRINT((
  1183. DBG_SS_CERTDBI,
  1184. "OpenTables: %hs: %ws\n",
  1185. g_dbauxCertificates.pszTable,
  1186. wszCSTFlags(pcs->aTable[CSTI_CERTIFICATE].TableFlags)));
  1187. }
  1188. hr = S_OK;
  1189. }
  1190. }
  1191. __except(hr = myHEXCEPTIONCODE(), EXCEPTION_EXECUTE_HANDLER)
  1192. {
  1193. }
  1194. error:
  1195. if (S_OK != hr)
  1196. {
  1197. __try
  1198. {
  1199. HRESULT hr2;
  1200. DWORD i;
  1201. for (i = 0; i < CSTI_MAX; i++)
  1202. {
  1203. if (NULL != pcs)
  1204. {
  1205. CSASSERTTHREAD(pcs);
  1206. if (IsValidJetTableId(pcs->aTable[i].TableId))
  1207. {
  1208. hr2 = _dbgJetCloseTable(
  1209. pcs->SesId,
  1210. pcs->aTable[i].TableId);
  1211. _PrintIfError(hr2, "JetCloseTable");
  1212. }
  1213. }
  1214. ZeroMemory(&pcs->aTable[i], sizeof(pcs->aTable[i]));
  1215. }
  1216. }
  1217. __except(hr = myHEXCEPTIONCODE(), EXCEPTION_EXECUTE_HANDLER)
  1218. {
  1219. }
  1220. }
  1221. if (fEnterCritSec)
  1222. {
  1223. LeaveCriticalSection(&m_critsecAutoIncTables);
  1224. }
  1225. return(myJetHResult(hr));
  1226. }
  1227. HRESULT
  1228. CCertDB::CloseTables(
  1229. IN CERTSESSION *pcs)
  1230. {
  1231. HRESULT hr = S_OK;
  1232. HRESULT hr2;
  1233. DWORD i;
  1234. if (NULL == pcs)
  1235. {
  1236. hr = E_POINTER;
  1237. _JumpError(hr, error, "NULL parm");
  1238. }
  1239. for (i = 0; i < CSTI_MAX; i++)
  1240. {
  1241. if (IsValidJetTableId(pcs->aTable[i].TableId))
  1242. {
  1243. hr2 = CloseTable(pcs, pcs->aTable[i].TableId);
  1244. _PrintIfError(hr2, "CloseTable");
  1245. if (S_OK == hr)
  1246. {
  1247. hr = hr2;
  1248. }
  1249. }
  1250. }
  1251. error:
  1252. return(myJetHResult(hr));
  1253. }
  1254. HRESULT
  1255. CCertDB::Delete(
  1256. IN CERTSESSION *pcs)
  1257. {
  1258. HRESULT hr = S_OK;
  1259. HRESULT hr2;
  1260. DWORD i;
  1261. if (NULL == pcs)
  1262. {
  1263. hr = E_POINTER;
  1264. _JumpError(hr, error, "NULL parm");
  1265. }
  1266. for (i = 0; i < CSTI_MAXDIRECT; i++)
  1267. {
  1268. if (IsValidJetTableId(pcs->aTable[i].TableId))
  1269. {
  1270. hr2 = _dbgJetDelete(pcs->SesId, pcs->aTable[i].TableId);
  1271. _PrintIfError(hr2, "JetDelete");
  1272. if (S_OK == hr)
  1273. {
  1274. hr = hr2;
  1275. }
  1276. }
  1277. }
  1278. error:
  1279. return(myJetHResult(hr));
  1280. }
  1281. HRESULT
  1282. CCertDB::_UpdateTable(
  1283. IN CERTSESSION *pcs,
  1284. IN JET_TABLEID tableid)
  1285. {
  1286. HRESULT hr;
  1287. CSASSERT(IsValidJetTableId(tableid));
  1288. CSASSERTTHREAD(pcs);
  1289. hr = _dbgJetUpdate(pcs->SesId, tableid, NULL, 0, NULL);
  1290. _JumpIfError(hr, error, "JetUpdate");
  1291. error:
  1292. return(myJetHResult(hr));
  1293. }
  1294. HRESULT
  1295. CCertDB::CloseTable(
  1296. IN CERTSESSION *pcs,
  1297. IN JET_TABLEID tableid)
  1298. {
  1299. HRESULT hr;
  1300. if (NULL == pcs)
  1301. {
  1302. hr = E_POINTER;
  1303. _JumpError(hr, error, "NULL parm");
  1304. }
  1305. CSASSERT(IsValidJetTableId(tableid));
  1306. CSASSERTTHREAD(pcs);
  1307. hr = _dbgJetCloseTable(pcs->SesId, tableid);
  1308. _JumpIfError(hr, error, "JetCloseTable");
  1309. error:
  1310. return(myJetHResult(hr));
  1311. }
  1312. STDMETHODIMP
  1313. CCertDB::OpenRow(
  1314. /* [in] */ DWORD dwFlags,
  1315. /* [in] */ DWORD RowId,
  1316. /* [in] */ WCHAR const *pwszSerialNumberOrCertHash, // OPTIONAL
  1317. /* [out] */ ICertDBRow **pprow)
  1318. {
  1319. HRESULT hr;
  1320. ICertDBRow *prow = NULL;
  1321. DWORD SesFlags = 0;
  1322. BOOL fCreate;
  1323. DWORD i;
  1324. CERTSESSION *pcs = NULL;
  1325. CERTVIEWRESTRICTION cvr;
  1326. CERTVIEWRESTRICTION *pcvr;
  1327. if (NULL == pprow)
  1328. {
  1329. hr = E_POINTER;
  1330. _JumpError(hr, error, "NULL parm");
  1331. }
  1332. *pprow = NULL;
  1333. switch (PROPTABLE_MASK & dwFlags)
  1334. {
  1335. case PROPTABLE_REQCERT:
  1336. SesFlags |= TABLE_REQCERTS;
  1337. cvr.ColumnIndex = DTI_REQUESTTABLE | DTC_REQUESTID;
  1338. break;
  1339. case PROPTABLE_ATTRIBUTE:
  1340. SesFlags |= TABLE_ATTRIBUTES;
  1341. cvr.ColumnIndex = DTI_ATTRIBUTETABLE | DTA_REQUESTID;
  1342. break;
  1343. case PROPTABLE_EXTENSION:
  1344. SesFlags |= TABLE_EXTENSIONS;
  1345. cvr.ColumnIndex = DTI_EXTENSIONTABLE | DTE_REQUESTID;
  1346. break;
  1347. case PROPTABLE_CRL:
  1348. SesFlags |= TABLE_CRLS;
  1349. cvr.ColumnIndex = DTI_CRLTABLE | DTL_ROWID;
  1350. break;
  1351. default:
  1352. hr = E_INVALIDARG;
  1353. _JumpError(hr, error, "bad table");
  1354. }
  1355. if ((PROPOPEN_CERTHASH & dwFlags) && NULL == pwszSerialNumberOrCertHash)
  1356. {
  1357. hr = E_INVALIDARG;
  1358. _JumpError(hr, error, "bad PROPOPEN_CERTHASH");
  1359. }
  1360. if (PROPTABLE_REQCERT != (PROPTABLE_MASK & dwFlags) &&
  1361. NULL != pwszSerialNumberOrCertHash)
  1362. {
  1363. hr = E_INVALIDARG;
  1364. _JumpError(hr, error, "bad pwszSerialNumberOrCertHash");
  1365. }
  1366. if ((PROPOPEN_READONLY | PROPOPEN_DELETE) ==
  1367. ((PROPOPEN_READONLY | PROPOPEN_DELETE) & dwFlags))
  1368. {
  1369. hr = E_INVALIDARG;
  1370. _JumpError(hr, error, "delete + read-only");
  1371. }
  1372. if (0 == RowId && NULL == pwszSerialNumberOrCertHash)
  1373. {
  1374. if ((PROPOPEN_READONLY | PROPOPEN_DELETE) & dwFlags)
  1375. {
  1376. hr = E_INVALIDARG;
  1377. _JumpError(hr, error, "OpenRow: create vs. delete or read-only");
  1378. }
  1379. SesFlags |= CSF_CREATE;
  1380. pcvr = NULL;
  1381. }
  1382. else
  1383. {
  1384. cvr.SeekOperator = CVR_SEEK_EQ;
  1385. cvr.SortOrder = CVR_SORT_ASCEND;
  1386. if (NULL != pwszSerialNumberOrCertHash)
  1387. {
  1388. cvr.ColumnIndex = (PROPOPEN_CERTHASH & dwFlags)?
  1389. (DTI_CERTIFICATETABLE | DTC_CERTIFICATEHASH) :
  1390. (DTI_CERTIFICATETABLE | DTC_CERTIFICATESERIALNUMBER);
  1391. cvr.cbValue = wcslen(pwszSerialNumberOrCertHash) * sizeof(WCHAR);
  1392. cvr.pbValue = (BYTE *) pwszSerialNumberOrCertHash;
  1393. }
  1394. else
  1395. {
  1396. cvr.cbValue = sizeof(RowId);
  1397. cvr.pbValue = (BYTE *) &RowId;
  1398. }
  1399. pcvr = &cvr;
  1400. }
  1401. if (PROPOPEN_READONLY & dwFlags)
  1402. {
  1403. SesFlags |= CSF_READONLY;
  1404. }
  1405. else
  1406. {
  1407. if (PROPOPEN_DELETE & dwFlags)
  1408. {
  1409. SesFlags |= CSF_DELETE;
  1410. }
  1411. if (m_fDBReadOnly)
  1412. {
  1413. hr = E_ACCESSDENIED;
  1414. _JumpError(hr, error, "OpenRow: read-only DB");
  1415. }
  1416. }
  1417. prow = new CCertDBRow;
  1418. if (NULL == prow)
  1419. {
  1420. hr = E_OUTOFMEMORY;
  1421. _JumpError(hr, error, "new CCertDBRow");
  1422. }
  1423. hr = _AllocateSession(&pcs);
  1424. _JumpIfError(hr, error, "_AllocateSession");
  1425. pcs->RowId = RowId;
  1426. pcs->SesFlags |= SesFlags;
  1427. pcs->prow = prow;
  1428. hr = ((CCertDBRow *) prow)->Open(pcs, this, pcvr);
  1429. _JumpIfError2(hr, error, "Open", CERTSRV_E_PROPERTY_EMPTY);
  1430. *pprow = prow;
  1431. prow = NULL;
  1432. pcs = NULL;
  1433. error:
  1434. if (NULL != prow)
  1435. {
  1436. prow->Release();
  1437. }
  1438. if (NULL != pcs)
  1439. {
  1440. ReleaseSession(pcs);
  1441. }
  1442. return(hr);
  1443. }
  1444. STDMETHODIMP
  1445. CCertDB::OpenView(
  1446. /* [in] */ DWORD ccvr,
  1447. /* [in] */ CERTVIEWRESTRICTION const *acvr,
  1448. /* [in] */ DWORD ccolOut,
  1449. /* [in] */ DWORD const *acolOut,
  1450. /* [in] */ DWORD const dwFlags,
  1451. /* [out] */ IEnumCERTDBRESULTROW **ppenum)
  1452. {
  1453. HRESULT hr;
  1454. IEnumCERTDBRESULTROW *penum = NULL;
  1455. BOOL fCreate;
  1456. CERTSESSION *pcs;
  1457. if ((NULL == acvr && 0 != ccvr) || NULL == acolOut || NULL == ppenum)
  1458. {
  1459. hr = E_POINTER;
  1460. _JumpError(hr, error, "NULL parm");
  1461. }
  1462. *ppenum = NULL;
  1463. penum = new CEnumCERTDBRESULTROW(0 != (CDBOPENVIEW_WORKERTHREAD & dwFlags));
  1464. if (NULL == penum)
  1465. {
  1466. hr = E_OUTOFMEMORY;
  1467. _JumpError(hr, error, "new CEnumCERTDBRESULTROW");
  1468. }
  1469. hr = _AllocateSession(&pcs);
  1470. _JumpIfError(hr, error, "_AllocateSession");
  1471. pcs->SesFlags |= CSF_READONLY | CSF_VIEW;
  1472. pcs->pview = penum;
  1473. hr = ((CEnumCERTDBRESULTROW *) penum)->Open(
  1474. pcs,
  1475. this,
  1476. ccvr,
  1477. acvr,
  1478. ccolOut,
  1479. acolOut);
  1480. _JumpIfError(hr, error, "Open");
  1481. *ppenum = penum;
  1482. penum = NULL;
  1483. error:
  1484. if (NULL != penum)
  1485. {
  1486. penum->Release();
  1487. }
  1488. return(hr);
  1489. }
  1490. HRESULT
  1491. CCertDB::OpenBackup(
  1492. /* [in] */ LONG grbitJet,
  1493. /* [out] */ ICertDBBackup **ppBackup)
  1494. {
  1495. HRESULT hr;
  1496. ICertDBBackup *pBackup = NULL;
  1497. CERTSESSION *pcs = NULL;
  1498. if (NULL == ppBackup)
  1499. {
  1500. hr = E_POINTER;
  1501. _JumpError(hr, error, "NULL parm");
  1502. }
  1503. *ppBackup = NULL;
  1504. pBackup = new CCertDBBackup;
  1505. if (NULL == pBackup)
  1506. {
  1507. hr = E_OUTOFMEMORY;
  1508. _JumpError(hr, error, "new CCertDBBackup");
  1509. }
  1510. hr = _AllocateSession(&pcs);
  1511. _JumpIfError(hr, error, "_AllocateSession");
  1512. hr = ((CCertDBBackup *) pBackup)->Open(grbitJet, pcs, this);
  1513. _JumpIfError(hr, error, "Open");
  1514. *ppBackup = pBackup;
  1515. pBackup = NULL;
  1516. pcs = NULL;
  1517. hr = S_OK;
  1518. error:
  1519. if (NULL != pBackup)
  1520. {
  1521. pBackup->Release();
  1522. }
  1523. if (NULL != pcs)
  1524. {
  1525. ReleaseSession(pcs);
  1526. }
  1527. return(hr);
  1528. }
  1529. HRESULT
  1530. CCertDB::ReleaseSession(
  1531. IN CERTSESSION *pcs)
  1532. {
  1533. HRESULT hr = S_OK;
  1534. HRESULT hr2;
  1535. if (NULL == pcs)
  1536. {
  1537. hr = E_POINTER;
  1538. _JumpError(hr, error, "NULL parm");
  1539. }
  1540. CSASSERT(CSF_INUSE & pcs->SesFlags);
  1541. while (0 != pcs->cTransact)
  1542. {
  1543. CSASSERTTHREAD(pcs);
  1544. hr2 = _dbgJetRollback(pcs->SesId, 0);
  1545. if (S_OK == hr)
  1546. {
  1547. hr = hr2;
  1548. }
  1549. _JumpIfError(hr2, loop, "JetRollback");
  1550. DBGPRINT((
  1551. (CSF_READONLY & pcs->SesFlags)? DBG_SS_CERTDBI : DBG_SS_CERTDB,
  1552. "ReleaseSession: Rollback transaction: %x\n",
  1553. pcs->cTransact));
  1554. loop:
  1555. CSASSERT(0 == pcs->cTransact);
  1556. pcs->cTransact--;
  1557. InterlockedIncrement(&g_cXactAbort);
  1558. }
  1559. //EnterCriticalSection(&m_critsecSession);
  1560. pcs->RowId = 0;
  1561. pcs->prow = NULL;
  1562. pcs->SesFlags = 0; // turn off CSF_INUSE -- must be LAST!
  1563. //LeaveCriticalSection(&m_critsecSession);
  1564. error:
  1565. return(myJetHResult(hr));
  1566. }
  1567. HRESULT
  1568. CCertDB::_Rollback(
  1569. IN CERTSESSION *pcs)
  1570. {
  1571. HRESULT hr = S_OK;
  1572. DWORD i;
  1573. if (NULL == pcs)
  1574. {
  1575. hr = E_POINTER;
  1576. _JumpError(hr, error, "NULL parm");
  1577. }
  1578. CSASSERT(CSF_INUSE & pcs->SesFlags);
  1579. for (i = 0; i < CSTI_MAX; i++)
  1580. {
  1581. pcs->aTable[i].TableId = 0;
  1582. }
  1583. CSASSERTTHREAD(pcs);
  1584. hr = _dbgJetRollback(pcs->SesId, 0);
  1585. _JumpIfError(hr, error, "JetRollback");
  1586. error:
  1587. return(myJetHResult(hr));
  1588. }
  1589. HRESULT
  1590. CCertDB::BackupBegin(
  1591. IN LONG grbitJet)
  1592. {
  1593. HRESULT hr;
  1594. hr = _dbgJetBeginExternalBackup(grbitJet);
  1595. _JumpIfError(hr, error, "JetBeginExternalBackup");
  1596. error:
  1597. return(myJetHResult(hr));
  1598. }
  1599. HRESULT
  1600. CCertDB::_BackupGetFileList(
  1601. IN BOOL fDBFiles,
  1602. IN OUT DWORD *pcwcList,
  1603. OUT WCHAR *pwszzList) // OPTIONAL
  1604. {
  1605. HRESULT hr;
  1606. CHAR buf[12];
  1607. CHAR *pszz = buf;
  1608. DWORD cbbuf = ARRAYSIZE(buf);
  1609. DWORD cbActual;
  1610. WCHAR *pwszz = NULL;
  1611. DWORD cwc;
  1612. WCHAR *pwsz;
  1613. DWORD cwcActual;
  1614. while (TRUE)
  1615. {
  1616. if (fDBFiles)
  1617. {
  1618. hr = _dbgJetGetAttachInfo(pszz, cbbuf, &cbActual);
  1619. _JumpIfError(hr, error, "JetGetAttachInfo");
  1620. }
  1621. else
  1622. {
  1623. hr = _dbgJetGetLogInfo(pszz, cbbuf, &cbActual);
  1624. _JumpIfError(hr, error, "JetGetLogInfo");
  1625. }
  1626. if (cbbuf >= cbActual)
  1627. {
  1628. break;
  1629. }
  1630. CSASSERT(buf == pszz);
  1631. pszz = (CHAR *) LocalAlloc(LMEM_FIXED, cbActual);
  1632. if (NULL == pszz)
  1633. {
  1634. hr = E_OUTOFMEMORY;
  1635. _JumpError(hr, error, "LocalAlloc");
  1636. }
  1637. cbbuf = cbActual;
  1638. }
  1639. if (!ConvertSzToWsz(&pwszz, pszz, cbActual))
  1640. {
  1641. hr = E_OUTOFMEMORY;
  1642. _JumpError(hr, error, "ConvertSzToWsz");
  1643. }
  1644. pwsz = pwszz;
  1645. do
  1646. {
  1647. cwc = wcslen(pwsz);
  1648. pwsz += cwc + 1;
  1649. } while (0 != cwc);
  1650. cwc = SAFE_SUBTRACT_POINTERS(pwsz, pwszz); // includes double trailing L'\0's
  1651. if (NULL != pwszzList)
  1652. {
  1653. CopyMemory(pwszzList, pwszz, min(cwc, *pcwcList) * sizeof(WCHAR));
  1654. if (cwc > *pcwcList)
  1655. {
  1656. hr = HRESULT_FROM_WIN32(ERROR_BUFFER_OVERFLOW);
  1657. }
  1658. }
  1659. *pcwcList = cwc;
  1660. _JumpIfError(hr, error, "Buffer Overflow");
  1661. error:
  1662. if (NULL != pszz && buf != pszz)
  1663. {
  1664. LocalFree(pszz);
  1665. }
  1666. if (NULL != pwszz)
  1667. {
  1668. LocalFree(pwszz);
  1669. }
  1670. return(myJetHResult(hr));
  1671. }
  1672. HRESULT
  1673. CCertDB::BackupGetDBFileList(
  1674. IN OUT DWORD *pcwcList,
  1675. OUT WCHAR *pwszzList) // OPTIONAL
  1676. {
  1677. HRESULT hr;
  1678. hr = _BackupGetFileList(TRUE, pcwcList, pwszzList);
  1679. _JumpIfError(hr, error, "_BackupGetFileList");
  1680. error:
  1681. return(hr);
  1682. }
  1683. HRESULT
  1684. CCertDB::BackupGetLogFileList(
  1685. IN OUT DWORD *pcwcList,
  1686. OUT WCHAR *pwszzList) // OPTIONAL
  1687. {
  1688. HRESULT hr;
  1689. hr = _BackupGetFileList(FALSE, pcwcList, pwszzList);
  1690. _JumpIfError(hr, error, "_BackupGetFileList");
  1691. error:
  1692. return(hr);
  1693. }
  1694. HRESULT
  1695. CCertDB::BackupOpenFile(
  1696. IN WCHAR const *pwszFile,
  1697. OUT JET_HANDLE *phFileDB,
  1698. OPTIONAL OUT ULARGE_INTEGER *pliSize)
  1699. {
  1700. HRESULT hr;
  1701. CHAR *pszFile = NULL;
  1702. if (!ConvertWszToSz(&pszFile, pwszFile, -1))
  1703. {
  1704. hr = E_OUTOFMEMORY;
  1705. _JumpError(hr, error, "ConvertWszToSz(pwszFile)");
  1706. }
  1707. hr = _dbgJetOpenFile(
  1708. pszFile,
  1709. phFileDB,
  1710. &pliSize->LowPart,
  1711. &pliSize->HighPart);
  1712. _JumpIfErrorStr(hr, error, "JetOpenFile", pwszFile);
  1713. error:
  1714. if (NULL != pszFile)
  1715. {
  1716. LocalFree(pszFile);
  1717. }
  1718. return(myJetHResult(hr));
  1719. }
  1720. HRESULT
  1721. CCertDB::BackupReadFile(
  1722. IN JET_HANDLE hFileDB,
  1723. OUT BYTE *pb,
  1724. IN DWORD cb,
  1725. OUT DWORD *pcb)
  1726. {
  1727. HRESULT hr;
  1728. BYTE *pbAlloc = NULL;
  1729. BYTE *pbRead;
  1730. if (0 == m_cbPage)
  1731. {
  1732. SYSTEM_INFO si;
  1733. GetSystemInfo(&si);
  1734. m_cbPage = si.dwPageSize;
  1735. }
  1736. if ((m_cbPage - 1) & cb)
  1737. {
  1738. hr = E_INVALIDARG;
  1739. _JumpError(hr, error, "bad read size");
  1740. }
  1741. pbRead = pb;
  1742. // If the caller's buffer is not page aligned, allocate an aligned buffer
  1743. // and copy the data.
  1744. if ((m_cbPage - 1) & (DWORD_PTR) pb)
  1745. {
  1746. pbAlloc = (BYTE *) VirtualAlloc(NULL, cb, MEM_COMMIT, PAGE_READWRITE);
  1747. if (NULL == pbAlloc)
  1748. {
  1749. hr = myHLastError();
  1750. _JumpIfError(hr, error, "VirtualAlloc");
  1751. }
  1752. pbRead = pbAlloc;
  1753. }
  1754. hr = _dbgJetReadFile(hFileDB, pbRead, cb, pcb);
  1755. _JumpIfError(hr, error, "JetReadFile");
  1756. if (NULL != pbAlloc)
  1757. {
  1758. CopyMemory(pb, pbAlloc, *pcb);
  1759. }
  1760. error:
  1761. if (NULL != pbAlloc)
  1762. {
  1763. VirtualFree(pbAlloc, 0, MEM_RELEASE);
  1764. }
  1765. return(myJetHResult(hr));
  1766. }
  1767. HRESULT
  1768. CCertDB::BackupCloseFile(
  1769. IN JET_HANDLE hFileDB)
  1770. {
  1771. HRESULT hr;
  1772. hr = _dbgJetCloseFile(hFileDB);
  1773. _JumpIfError(hr, error, "JetCloseFile");
  1774. error:
  1775. return(myJetHResult(hr));
  1776. }
  1777. HRESULT
  1778. CCertDB::BackupTruncateLog()
  1779. {
  1780. HRESULT hr;
  1781. hr = _dbgJetTruncateLog();
  1782. _JumpIfError(hr, error, "JetTruncateLog");
  1783. error:
  1784. return(myJetHResult(hr));
  1785. }
  1786. HRESULT
  1787. CCertDB::BackupEnd()
  1788. {
  1789. HRESULT hr;
  1790. hr = _dbgJetEndExternalBackup();
  1791. _JumpIfError(hr, error, "JetEndExternalBackup");
  1792. error:
  1793. return(myJetHResult(hr));
  1794. }
  1795. DBTABLE const *
  1796. CCertDB::_MapTable(
  1797. IN WCHAR const *pwszPropName,
  1798. IN DBTABLE const *pdt)
  1799. {
  1800. while (NULL != pdt->pwszPropName)
  1801. {
  1802. if (0 == (DBTF_MISSING & pdt->dwFlags) &&
  1803. (0 == lstrcmpi(pwszPropName, pdt->pwszPropName) ||
  1804. (NULL != pdt->pwszPropNameObjId &&
  1805. 0 == lstrcmpi(pwszPropName, pdt->pwszPropNameObjId))))
  1806. {
  1807. return(pdt);
  1808. }
  1809. pdt++;
  1810. }
  1811. return(NULL);
  1812. }
  1813. HRESULT
  1814. CCertDB::_MapPropIdIndex(
  1815. IN DWORD ColumnIndex,
  1816. OUT DBTABLE const **ppdt,
  1817. OPTIONAL OUT DWORD *pType)
  1818. {
  1819. HRESULT hr;
  1820. DBTABLE const *pdt = NULL;
  1821. DWORD iCol = DTI_COLUMNMASK & ColumnIndex;
  1822. switch (DTI_TABLEMASK & ColumnIndex)
  1823. {
  1824. case DTI_REQUESTTABLE:
  1825. if (DTR_MAX > iCol)
  1826. {
  1827. pdt = g_adtRequests;
  1828. }
  1829. break;
  1830. case DTI_CERTIFICATETABLE:
  1831. if (DTC_MAX > iCol)
  1832. {
  1833. pdt = g_adtCertificates;
  1834. }
  1835. break;
  1836. case DTI_ATTRIBUTETABLE:
  1837. if (DTA_MAX > iCol)
  1838. {
  1839. pdt = g_adtRequestAttributes;
  1840. }
  1841. break;
  1842. case DTI_EXTENSIONTABLE:
  1843. if (DTE_MAX > iCol)
  1844. {
  1845. pdt = g_adtCertExtensions;
  1846. }
  1847. break;
  1848. case DTI_CRLTABLE:
  1849. if (DTL_MAX > iCol)
  1850. {
  1851. pdt = g_adtCRLs;
  1852. }
  1853. break;
  1854. }
  1855. if (NULL == pdt)
  1856. {
  1857. hr = E_INVALIDARG;
  1858. DBGPRINT((
  1859. DBG_SS_CERTDB,
  1860. "_MapPropIdIndex(%x) -> %x\n",
  1861. ColumnIndex,
  1862. hr));
  1863. _JumpError(hr, error, "column index");
  1864. }
  1865. pdt += iCol;
  1866. if (NULL != pType)
  1867. {
  1868. switch (pdt->dbcoltyp)
  1869. {
  1870. case JET_coltypDateTime:
  1871. *pType = PROPTYPE_DATE;
  1872. break;
  1873. case JET_coltypLong:
  1874. *pType = PROPTYPE_LONG;
  1875. break;
  1876. case JET_coltypText:
  1877. case JET_coltypLongText:
  1878. *pType = PROPTYPE_STRING;
  1879. break;
  1880. case JET_coltypLongBinary:
  1881. default:
  1882. *pType = PROPTYPE_BINARY;
  1883. break;
  1884. }
  1885. if (NULL != pdt->pszIndexName &&
  1886. 0 == (DBTF_INDEXREQUESTID & pdt->dwFlags))
  1887. {
  1888. *pType |= PROPFLAGS_INDEXED;
  1889. }
  1890. }
  1891. DBGPRINT((
  1892. DBG_SS_CERTDBI,
  1893. "_MapPropIdIndex(%x) -> %ws.%ws\n",
  1894. ColumnIndex,
  1895. wszTable(pdt->dwTable),
  1896. pdt->pwszPropName));
  1897. hr = S_OK;
  1898. error:
  1899. *ppdt = pdt;
  1900. return(hr);
  1901. }
  1902. HRESULT
  1903. CCertDB::MapPropId(
  1904. IN WCHAR const *pwszPropName,
  1905. IN DWORD dwFlags,
  1906. OUT DBTABLE *pdtOut)
  1907. {
  1908. DBTABLE const *pdt = NULL;
  1909. WCHAR wszPrefix[2 * (sizeof(wszPROPSUBJECTDOT) / sizeof(WCHAR))];
  1910. DWORD dwTable;
  1911. HRESULT hr = S_OK;
  1912. DBTABLE const *pdbTable;
  1913. WCHAR const *pwszStart;
  1914. BOOL fSubject = FALSE;
  1915. BOOL fRequest = FALSE;
  1916. if (NULL == pwszPropName || NULL == pdtOut)
  1917. {
  1918. hr = E_POINTER;
  1919. _JumpError(hr, error, "NULL parm");
  1920. }
  1921. dwTable = PROPTABLE_MASK & dwFlags;
  1922. CSASSERT(
  1923. PROPTABLE_REQUEST == dwTable ||
  1924. PROPTABLE_CERTIFICATE == dwTable ||
  1925. PROPTABLE_CRL == dwTable);
  1926. // Check to see if the request is for L"Subject.".
  1927. pwszStart = pwszPropName;
  1928. if (PROPTABLE_CRL != dwTable)
  1929. {
  1930. while (!fSubject)
  1931. {
  1932. WCHAR const *pwsz;
  1933. pwsz = wcschr(pwszStart, L'.');
  1934. if (NULL == pwsz ||
  1935. pwsz - pwszStart + 2 > sizeof(wszPrefix)/sizeof(WCHAR))
  1936. {
  1937. pwsz = pwszStart;
  1938. break;
  1939. }
  1940. pwsz++; // skip past L'.'
  1941. CopyMemory(
  1942. wszPrefix,
  1943. pwszStart,
  1944. (SAFE_SUBTRACT_POINTERS(pwsz, pwszStart) * sizeof(WCHAR)));
  1945. wszPrefix[pwsz - pwszStart] = L'\0';
  1946. if (!fSubject)
  1947. {
  1948. pwszStart = pwsz;
  1949. if (0 == lstrcmpi(wszPrefix, wszPROPSUBJECTDOT))
  1950. {
  1951. fSubject = TRUE;
  1952. continue;
  1953. }
  1954. else
  1955. if (!fRequest &&
  1956. PROPTABLE_REQUEST == dwTable &&
  1957. 0 == lstrcmpi(wszPrefix, wszPROPREQUESTDOT))
  1958. {
  1959. fRequest = TRUE;
  1960. continue;
  1961. }
  1962. }
  1963. hr = E_INVALIDARG;
  1964. _JumpErrorStr(hr, error, "Invalid prefix", pwszPropName);
  1965. }
  1966. }
  1967. pdbTable = NULL;
  1968. // Search the requested table for a matching property name or property
  1969. // objectid string.
  1970. switch (dwTable)
  1971. {
  1972. case PROPTABLE_REQUEST:
  1973. pdbTable = g_adtRequests;
  1974. break;
  1975. case PROPTABLE_CERTIFICATE:
  1976. pdbTable = g_adtCertificates;
  1977. break;
  1978. case PROPTABLE_CRL:
  1979. pdbTable = g_adtCRLs;
  1980. break;
  1981. }
  1982. CSASSERT(NULL != pdbTable);
  1983. pdt = _MapTable(pwszStart, pdbTable);
  1984. if (NULL == pdt || (fSubject && 0 == (DBTF_SUBJECT & pdt->dwFlags)))
  1985. {
  1986. hr = CERTSRV_E_PROPERTY_EMPTY;
  1987. _JumpErrorStr(
  1988. hr,
  1989. error,
  1990. PROPTABLE_REQUEST == dwTable?
  1991. "unknown Request property" :
  1992. PROPTABLE_CERTIFICATE == dwTable?
  1993. "unknown Certificate property" :
  1994. "unknown CRL property",
  1995. pwszPropName);
  1996. }
  1997. *pdtOut = *pdt; // structure copy
  1998. hr = S_OK;
  1999. error:
  2000. return(hr);
  2001. }
  2002. HRESULT
  2003. CCertDB::TestShutDownState()
  2004. {
  2005. HRESULT hr;
  2006. if (m_fPendingShutDown)
  2007. {
  2008. hr = HRESULT_FROM_WIN32(ERROR_SHUTDOWN_IN_PROGRESS);
  2009. _JumpError(hr, error, "m_fPendingShutDown");
  2010. }
  2011. hr = S_OK;
  2012. error:
  2013. return(hr);
  2014. }
  2015. HRESULT
  2016. CCertDB::SetProperty(
  2017. IN CERTSESSION *pcs,
  2018. IN DBTABLE const *pdt,
  2019. IN DWORD cbProp,
  2020. IN BYTE const *pbProp) // OPTIONAL
  2021. {
  2022. HRESULT hr;
  2023. JET_TABLEID tableid;
  2024. if (NULL == pcs ||
  2025. NULL == pdt ||
  2026. (NULL == pbProp && !ISTEXTCOLTYP(pdt->dbcoltyp)))
  2027. {
  2028. hr = E_POINTER;
  2029. _JumpError(hr, error, "NULL parm");
  2030. }
  2031. DBGPRINT((
  2032. DBG_SS_CERTDBI,
  2033. "SetProperty for %hs into table %d\n",
  2034. pdt->pszFieldName,
  2035. pdt->dwTable));
  2036. if (ISTEXTCOLTYP(pdt->dbcoltyp))
  2037. {
  2038. DBGPRINT((DBG_SS_CERTDBI, "SetProperty setting string %ws\n", pbProp));
  2039. }
  2040. if (JET_coltypDateTime == pdt->dbcoltyp)
  2041. {
  2042. DBGPRINT((
  2043. DBG_SS_CERTDBI,
  2044. "SetProperty setting date: %x:%x\n",
  2045. ((DWORD *) pbProp)[0],
  2046. ((DWORD *) pbProp)[1]));
  2047. }
  2048. switch (pdt->dwTable)
  2049. {
  2050. case TABLE_CRLS:
  2051. case TABLE_REQUESTS:
  2052. tableid = pcs->aTable[CSTI_PRIMARY].TableId;
  2053. break;
  2054. case TABLE_CERTIFICATES:
  2055. tableid = pcs->aTable[CSTI_CERTIFICATE].TableId;
  2056. break;
  2057. default:
  2058. hr = E_INVALIDARG;
  2059. _JumpError(hr, error, "unknown table type");
  2060. }
  2061. CSASSERT(IsValidJetTableId(tableid));
  2062. hr = _SetColumn(
  2063. pcs->SesId,
  2064. tableid,
  2065. pdt->dbcolumnid,
  2066. cbProp,
  2067. pbProp);
  2068. _JumpIfErrorStr(hr, error, "_SetColumn", pdt->pwszPropName);
  2069. error:
  2070. return(myJetHResult(hr));
  2071. }
  2072. HRESULT
  2073. CCertDB::GetProperty(
  2074. IN CERTSESSION *pcs,
  2075. IN DBTABLE const *pdt,
  2076. IN OUT DWORD *pcbProp,
  2077. OUT BYTE *pbProp) // OPTIONAL
  2078. {
  2079. HRESULT hr;
  2080. JET_COLUMNDEF columndef;
  2081. JET_TABLEID tableid;
  2082. if (NULL == pcs || NULL == pdt || NULL == pcbProp)
  2083. {
  2084. hr = E_POINTER;
  2085. _JumpError(hr, error, "NULL parm");
  2086. }
  2087. DBGPRINT((
  2088. DBG_SS_CERTDBI,
  2089. "GetProperty for %hs from table %d\n",
  2090. pdt->pszFieldName,
  2091. pdt->dwTable));
  2092. if ((CSF_TABLEMASK & pcs->SesFlags) != pdt->dwTable)
  2093. {
  2094. if (TABLE_REQCERTS != (CSF_TABLEMASK & pcs->SesFlags) ||
  2095. (TABLE_REQUESTS != pdt->dwTable &&
  2096. TABLE_CERTIFICATES != pdt->dwTable))
  2097. {
  2098. hr = E_INVALIDARG;
  2099. _JumpError(hr, error, "mismatched table");
  2100. }
  2101. }
  2102. if (TABLE_CERTIFICATES == pdt->dwTable)
  2103. {
  2104. tableid = pcs->aTable[CSTI_CERTIFICATE].TableId;
  2105. }
  2106. else
  2107. {
  2108. tableid = pcs->aTable[CSTI_PRIMARY].TableId;
  2109. }
  2110. CSASSERT(IsValidJetTableId(tableid));
  2111. hr = _RetrieveColumn(
  2112. pcs,
  2113. tableid,
  2114. pdt->dbcolumnid,
  2115. pdt->dbcoltyp,
  2116. pcbProp,
  2117. pbProp);
  2118. _JumpIfErrorStr3(
  2119. hr,
  2120. error,
  2121. "_RetrieveColumn",
  2122. pdt->pwszPropName,
  2123. CERTSRV_E_PROPERTY_EMPTY,
  2124. HRESULT_FROM_WIN32(ERROR_BUFFER_OVERFLOW));
  2125. if (ISTEXTCOLTYP(pdt->dbcoltyp))
  2126. {
  2127. DBGPRINT((DBG_SS_CERTDBI, "GetProperty returning string %ws\n", pbProp));
  2128. }
  2129. error:
  2130. return(myJetHResult(hr));
  2131. }
  2132. HRESULT
  2133. CCertDB::CopyRequestNames(
  2134. IN CERTSESSION *pcs)
  2135. {
  2136. HRESULT hr = S_OK;
  2137. DBTABLE dt;
  2138. DWORD cbProp;
  2139. BYTE *pbProp = NULL;
  2140. DWORD i;
  2141. BYTE rgbFastBuf[64];
  2142. if (NULL == pcs)
  2143. {
  2144. hr = E_POINTER;
  2145. _JumpError(hr, error, "NULL parm");
  2146. }
  2147. for (i = 0; NULL != g_dntr[i].pszFieldName; i++)
  2148. {
  2149. hr = MapPropId(g_dntr[i].pwszPropName, PROPTABLE_REQUEST, &dt);
  2150. if (CERTSRV_E_PROPERTY_EMPTY == hr)
  2151. {
  2152. hr = S_OK;
  2153. continue; // Optional column doesn't exist
  2154. }
  2155. _JumpIfError(hr, error, "MapPropId");
  2156. // re-point at fastbuf
  2157. pbProp = rgbFastBuf;
  2158. cbProp = sizeof(rgbFastBuf);
  2159. hr = GetProperty(pcs, &dt, &cbProp, pbProp);
  2160. if (S_OK != hr)
  2161. {
  2162. if (HRESULT_FROM_WIN32(ERROR_BUFFER_OVERFLOW) != hr)
  2163. {
  2164. if (CERTSRV_E_PROPERTY_EMPTY == hr)
  2165. {
  2166. hr = S_OK;
  2167. continue;
  2168. }
  2169. _JumpIfError(hr, error, "GetProperty");
  2170. }
  2171. CSASSERT (ARRAYSIZE(rgbFastBuf) < cbProp);
  2172. DBGPRINT((
  2173. DBG_SS_CERTDB,
  2174. "FastBuf miss: CopyRequestNames(cbProp=%u)\n",
  2175. cbProp));
  2176. pbProp = (BYTE *) LocalAlloc(LMEM_FIXED, cbProp);
  2177. if (NULL == pbProp)
  2178. {
  2179. hr = E_OUTOFMEMORY;
  2180. _JumpError(hr, error, "LocalAlloc");
  2181. }
  2182. hr = GetProperty(pcs, &dt, &cbProp, pbProp);
  2183. _JumpIfError(hr, error, "GetProperty");
  2184. } // have data in hand
  2185. hr = MapPropId(g_dntr[i].pwszPropName, PROPTABLE_CERTIFICATE, &dt);
  2186. _JumpIfError(hr, error, "MapPropId");
  2187. hr = SetProperty(pcs, &dt, cbProp, pbProp);
  2188. _JumpIfError(hr, error, "SetProperty");
  2189. if (NULL != pbProp && rgbFastBuf != pbProp)
  2190. {
  2191. LocalFree(pbProp);
  2192. }
  2193. pbProp = NULL;
  2194. }
  2195. error:
  2196. if (NULL != pbProp && rgbFastBuf != pbProp)
  2197. {
  2198. LocalFree(pbProp);
  2199. }
  2200. return(hr);
  2201. }
  2202. STDMETHODIMP
  2203. CCertDB::EnumCertDBColumn(
  2204. /* [in] */ DWORD dwTable,
  2205. /* [out] */ IEnumCERTDBCOLUMN **ppenum)
  2206. {
  2207. HRESULT hr;
  2208. IEnumCERTDBCOLUMN *penum = NULL;
  2209. if (NULL == ppenum)
  2210. {
  2211. hr = E_POINTER;
  2212. _JumpError(hr, error, "NULL parm");
  2213. }
  2214. *ppenum = NULL;
  2215. penum = new CEnumCERTDBCOLUMN;
  2216. if (NULL == penum)
  2217. {
  2218. hr = E_OUTOFMEMORY;
  2219. _JumpError(hr, error, "new CEnumCERTDBCOLUMN");
  2220. }
  2221. hr = ((CEnumCERTDBCOLUMN *) penum)->Open(dwTable, this);
  2222. _JumpIfError(hr, error, "Open");
  2223. *ppenum = penum;
  2224. hr = S_OK;
  2225. error:
  2226. if (S_OK != hr && NULL != penum)
  2227. {
  2228. penum->Release();
  2229. }
  2230. return(hr);
  2231. }
  2232. STDMETHODIMP
  2233. CCertDB::GetDefaultColumnSet(
  2234. /* [in] */ DWORD iColumnSetDefault,
  2235. /* [in] */ DWORD cColumnIds,
  2236. /* [out] */ DWORD *pcColumnIds,
  2237. /* [out, ref] */ DWORD *pColumnIds) // OPTIONAL
  2238. {
  2239. HRESULT hr;
  2240. DWORD *pcol;
  2241. DWORD ccol;
  2242. if (NULL == pcColumnIds)
  2243. {
  2244. hr = E_POINTER;
  2245. _JumpError(hr, error, "NULL parm");
  2246. }
  2247. switch (iColumnSetDefault)
  2248. {
  2249. case CV_COLUMN_LOG_FAILED_DEFAULT:
  2250. case CV_COLUMN_QUEUE_DEFAULT:
  2251. pcol = g_aColumnViewQueue;
  2252. ccol = g_cColumnViewQueue;
  2253. break;
  2254. case CV_COLUMN_LOG_REVOKED_DEFAULT:
  2255. pcol = g_aColumnViewRevoked;
  2256. ccol = g_cColumnViewRevoked;
  2257. break;
  2258. case CV_COLUMN_LOG_DEFAULT:
  2259. pcol = g_aColumnViewLog;
  2260. ccol = g_cColumnViewLog;
  2261. break;
  2262. case CV_COLUMN_EXTENSION_DEFAULT:
  2263. pcol = g_aColumnViewExtension;
  2264. ccol = g_cColumnViewExtension;
  2265. break;
  2266. case CV_COLUMN_ATTRIBUTE_DEFAULT:
  2267. pcol = g_aColumnViewAttribute;
  2268. ccol = g_cColumnViewAttribute;
  2269. break;
  2270. case CV_COLUMN_CRL_DEFAULT:
  2271. pcol = g_aColumnViewCRL;
  2272. ccol = g_cColumnViewCRL;
  2273. break;
  2274. default:
  2275. hr = E_INVALIDARG;
  2276. _JumpError(hr, error, "iColumnSetDefault");
  2277. }
  2278. *pcColumnIds = ccol;
  2279. hr = S_OK;
  2280. if (NULL != pColumnIds)
  2281. {
  2282. if (ccol > cColumnIds)
  2283. {
  2284. ccol = cColumnIds;
  2285. hr = HRESULT_FROM_WIN32(ERROR_BUFFER_OVERFLOW);
  2286. }
  2287. CopyMemory(pColumnIds, pcol, ccol * sizeof(*pColumnIds));
  2288. }
  2289. error:
  2290. return(hr);
  2291. }
  2292. HRESULT
  2293. CCertDB::GetColumnType(
  2294. IN LONG ColumnIndex,
  2295. OUT DWORD *pType)
  2296. {
  2297. HRESULT hr;
  2298. DBTABLE const *pdt;
  2299. if (NULL == pType)
  2300. {
  2301. hr = E_POINTER;
  2302. _JumpError(hr, error, "NULL parm");
  2303. }
  2304. hr = _MapPropIdIndex(ColumnIndex, &pdt, pType);
  2305. _JumpIfError(hr, error, "_MapPropIdIndex");
  2306. error:
  2307. return(hr);
  2308. }
  2309. HRESULT
  2310. CCertDB::EnumCertDBColumnNext(
  2311. IN DWORD dwTable, // CVRC_TABLE_*
  2312. IN ULONG ielt,
  2313. IN ULONG celt,
  2314. OUT CERTDBCOLUMN *rgelt,
  2315. OUT ULONG *pielt,
  2316. OUT ULONG *pceltFetched)
  2317. {
  2318. HRESULT hr;
  2319. ULONG ieltEnd;
  2320. ULONG ieltMax;
  2321. ULONG TableIndex;
  2322. CERTDBCOLUMN *pelt;
  2323. WCHAR const *pwszPrefix;
  2324. WCHAR const *pwszDisplayName;
  2325. if (NULL == rgelt || NULL == pielt || NULL == pceltFetched)
  2326. {
  2327. hr = E_POINTER;
  2328. _JumpError(hr, error, "NULL parm");
  2329. }
  2330. switch (dwTable)
  2331. {
  2332. case CVRC_TABLE_REQCERT:
  2333. TableIndex = DTI_REQUESTTABLE;
  2334. ieltMax = DTR_MAX + DTC_MAX;
  2335. break;
  2336. case CVRC_TABLE_EXTENSIONS:
  2337. TableIndex = DTI_EXTENSIONTABLE;
  2338. ieltMax = DTE_MAX;
  2339. break;
  2340. case CVRC_TABLE_ATTRIBUTES:
  2341. TableIndex = DTI_ATTRIBUTETABLE;
  2342. ieltMax = DTA_MAX;
  2343. break;
  2344. case CVRC_TABLE_CRL:
  2345. TableIndex = DTI_CRLTABLE;
  2346. ieltMax = DTL_MAX;
  2347. break;
  2348. default:
  2349. hr = E_INVALIDARG;
  2350. _JumpError(hr, error, "Bad table");
  2351. }
  2352. if (ieltMax + ielt < celt)
  2353. {
  2354. celt = ieltMax - ielt;
  2355. }
  2356. ieltEnd = ielt + celt;
  2357. ZeroMemory(rgelt, celt * sizeof(rgelt[0]));
  2358. hr = S_OK;
  2359. for (pelt = rgelt; pelt < &rgelt[celt]; ielt++, pelt++)
  2360. {
  2361. DBTABLE const *pdt;
  2362. ULONG ieltBase = 0;
  2363. if (ieltMax <= ielt)
  2364. {
  2365. if (pelt == rgelt)
  2366. {
  2367. hr = S_FALSE;
  2368. }
  2369. break;
  2370. }
  2371. pwszPrefix = NULL;
  2372. if (CVRC_TABLE_REQCERT == dwTable)
  2373. {
  2374. if (DTR_MAX > ielt)
  2375. {
  2376. pwszPrefix = wszPROPREQUESTDOT;
  2377. TableIndex = DTI_REQUESTTABLE;
  2378. }
  2379. else
  2380. {
  2381. ieltBase = DTR_MAX;
  2382. TableIndex = DTI_CERTIFICATETABLE;
  2383. }
  2384. }
  2385. pelt->Index = TableIndex | (ielt - ieltBase);
  2386. hr = _MapPropIdIndex(pelt->Index, &pdt, &pelt->Type);
  2387. _JumpIfError(hr, error, "_MapPropIdIndex");
  2388. pelt->cbMax = pdt->dwcbMax;
  2389. hr = _DupString(pwszPrefix, pdt->pwszPropName, &pelt->pwszName);
  2390. _JumpIfError(hr, error, "_DupString");
  2391. hr = myGetColumnDisplayName(pelt->pwszName, &pwszDisplayName);
  2392. _PrintIfError(hr, "myGetColumnDisplayName");
  2393. if (S_OK != hr)
  2394. {
  2395. pwszDisplayName = pelt->pwszName;
  2396. }
  2397. hr = _DupString(NULL, pwszDisplayName, &pelt->pwszDisplayName);
  2398. _JumpIfError(hr, error, "_DupString");
  2399. }
  2400. *pceltFetched = SAFE_SUBTRACT_POINTERS(pelt, rgelt);
  2401. *pielt = ielt;
  2402. error:
  2403. if (S_OK != hr && S_FALSE != hr)
  2404. {
  2405. if (NULL != rgelt)
  2406. {
  2407. for (pelt = rgelt; pelt < &rgelt[celt]; pelt++)
  2408. {
  2409. if (NULL != pelt->pwszName)
  2410. {
  2411. CoTaskMemFree(pelt->pwszName);
  2412. pelt->pwszName = NULL;
  2413. }
  2414. if (NULL != pelt->pwszDisplayName)
  2415. {
  2416. CoTaskMemFree(pelt->pwszDisplayName);
  2417. pelt->pwszDisplayName = NULL;
  2418. }
  2419. }
  2420. }
  2421. }
  2422. return(hr);
  2423. }
  2424. HRESULT
  2425. CCertDB::EnumCertDBResultRowNext(
  2426. IN CERTSESSION *pcs,
  2427. IN DWORD ccvr,
  2428. IN CERTVIEWRESTRICTION const *pcvr,
  2429. IN DWORD ccolOut,
  2430. IN DWORD const *acolOut,
  2431. IN LONG cskip,
  2432. IN ULONG celt,
  2433. OUT CERTDBRESULTROW *rgelt,
  2434. OUT ULONG *pceltFetched,
  2435. OUT LONG *pcskipped)
  2436. {
  2437. HRESULT hr;
  2438. DWORD iRow;
  2439. CERTDBRESULTROW *pResultRow;
  2440. LONG cskipped;
  2441. DBGPRINT((
  2442. DBG_SS_CERTDBI,
  2443. "EnumCertDBResultRowNext called: cskip: %d\n", cskip));
  2444. if (NULL == pcvr ||
  2445. NULL == acolOut ||
  2446. NULL == rgelt ||
  2447. NULL == pceltFetched ||
  2448. NULL == pcskipped)
  2449. {
  2450. hr = E_POINTER;
  2451. _JumpError(hr, error, "NULL parm");
  2452. }
  2453. *pcskipped = 0;
  2454. hr = S_OK;
  2455. for (iRow = 0; iRow < celt; iRow++)
  2456. {
  2457. hr = TestShutDownState();
  2458. _JumpIfError(hr, error, "TestShutDownState");
  2459. hr = _GetResultRow(
  2460. pcs,
  2461. ccvr,
  2462. pcvr,
  2463. cskip,
  2464. ccolOut,
  2465. acolOut,
  2466. &rgelt[iRow],
  2467. &cskipped);
  2468. if (S_FALSE == hr)
  2469. {
  2470. *pcskipped += cskipped;
  2471. break;
  2472. }
  2473. DBGPRINT((
  2474. DBG_SS_CERTDBI,
  2475. "EnumCertDBResultRowNext: rowid %u\n", rgelt[iRow].rowid));
  2476. _JumpIfError(hr, error, "_GetResultRow");
  2477. *pcskipped += cskipped;
  2478. cskip = 0;
  2479. }
  2480. *pceltFetched = iRow;
  2481. DBGPRINT((
  2482. DBG_SS_CERTDBI,
  2483. "EnumCertDBResultRowNext: %u rows, hr=%x\n",
  2484. *pceltFetched,
  2485. hr));
  2486. error:
  2487. if (S_OK != hr && S_FALSE != hr)
  2488. {
  2489. ReleaseResultRow(celt, rgelt);
  2490. }
  2491. return(hr);
  2492. }
  2493. HRESULT
  2494. CCertDB::_CompareColumnValue(
  2495. IN CERTSESSION *pcs,
  2496. IN CERTVIEWRESTRICTION const *pcvr)
  2497. {
  2498. HRESULT hr;
  2499. JET_TABLEID tableid;
  2500. DBTABLE const *pdt;
  2501. WCHAR *pwszValue = NULL;
  2502. BOOL fMatch;
  2503. int r;
  2504. BYTE rgbFastBuf[256];
  2505. BYTE *pbProp = rgbFastBuf;
  2506. DWORD cb = sizeof(rgbFastBuf);
  2507. // if SEEK_NONE, short circuit tests
  2508. if (CVR_SEEK_NONE == (CVR_SEEK_MASK & pcvr->SeekOperator))
  2509. {
  2510. return S_OK;
  2511. }
  2512. hr = _MapPropIdIndex(pcvr->ColumnIndex, &pdt, NULL);
  2513. _JumpIfError(hr, error, "_MapPropIdIndex");
  2514. if (TABLE_CERTIFICATES == pdt->dwTable)
  2515. {
  2516. tableid = pcs->aTable[CSTI_CERTIFICATE].TableId;
  2517. }
  2518. else
  2519. {
  2520. tableid = pcs->aTable[CSTI_PRIMARY].TableId;
  2521. }
  2522. CSASSERT(IsValidJetTableId(tableid));
  2523. hr = _RetrieveColumn(
  2524. pcs,
  2525. tableid,
  2526. pdt->dbcolumnid,
  2527. pdt->dbcoltyp,
  2528. &cb,
  2529. rgbFastBuf);
  2530. if (S_OK != hr)
  2531. {
  2532. if (HRESULT_FROM_WIN32(ERROR_BUFFER_OVERFLOW) != hr)
  2533. {
  2534. if (CERTSRV_E_PROPERTY_EMPTY == hr)
  2535. {
  2536. _PrintError2(hr, "_RetrieveColumn", hr);
  2537. hr = S_FALSE;
  2538. }
  2539. _JumpError2(hr, error, "_RetrieveColumn", S_FALSE);
  2540. }
  2541. // buffer not big enough, dyn-alloc
  2542. CSASSERT(ARRAYSIZE(rgbFastBuf) < cb);
  2543. DBGPRINT((
  2544. DBG_SS_CERTDB,
  2545. "FastBuf miss: _CompareColumnValue(cbProp=%u)\n",
  2546. cb));
  2547. pbProp = (BYTE *) LocalAlloc(LMEM_FIXED, cb);
  2548. if (NULL == pbProp)
  2549. {
  2550. hr = E_OUTOFMEMORY;
  2551. _JumpError(hr, error, "LocalAlloc");
  2552. }
  2553. hr = _RetrieveColumn(
  2554. pcs,
  2555. tableid,
  2556. pdt->dbcolumnid,
  2557. pdt->dbcoltyp,
  2558. &cb,
  2559. pbProp);
  2560. _JumpIfError(hr, error, "_RetrieveColumn");
  2561. } // we have data in-hand
  2562. #if DBG_CERTSRV
  2563. DumpRestriction(DBG_SS_CERTDBI, -1, pcvr);
  2564. dbDumpColumn(DBG_SS_CERTDBI, pdt, pbProp, cb);
  2565. #endif
  2566. fMatch = FALSE;
  2567. switch (pdt->dbcoltyp)
  2568. {
  2569. case JET_coltypLong:
  2570. if (cb == pcvr->cbValue && sizeof(LONG) == cb)
  2571. {
  2572. LONG lRestriction;
  2573. LONG lColumn;
  2574. lRestriction = *(LONG *) pcvr->pbValue;
  2575. lColumn = *(LONG *) pbProp;
  2576. switch (CVR_SEEK_MASK & pcvr->SeekOperator)
  2577. {
  2578. case CVR_SEEK_EQ:
  2579. fMatch = lColumn == lRestriction;
  2580. break;
  2581. case CVR_SEEK_LT:
  2582. fMatch = lColumn < lRestriction;
  2583. break;
  2584. case CVR_SEEK_LE:
  2585. fMatch = lColumn <= lRestriction;
  2586. break;
  2587. case CVR_SEEK_GE:
  2588. fMatch = lColumn >= lRestriction;
  2589. break;
  2590. case CVR_SEEK_GT:
  2591. fMatch = lColumn > lRestriction;
  2592. break;
  2593. }
  2594. DBGPRINT((
  2595. DBG_SS_CERTDBI,
  2596. "_CompareColumnValue(lColumn=%x %ws lRestriction=%x) -> fMatch=%x\n",
  2597. lColumn,
  2598. wszSeekOperator(pcvr->SeekOperator),
  2599. lRestriction,
  2600. fMatch));
  2601. }
  2602. break;
  2603. case JET_coltypDateTime:
  2604. if (cb == pcvr->cbValue && sizeof(FILETIME) == cb)
  2605. {
  2606. r = CompareFileTime(
  2607. (FILETIME *) pcvr->pbValue,
  2608. (FILETIME *) pbProp);
  2609. switch (CVR_SEEK_MASK & pcvr->SeekOperator)
  2610. {
  2611. case CVR_SEEK_EQ:
  2612. fMatch = 0 == r;
  2613. break;
  2614. case CVR_SEEK_LT:
  2615. fMatch = 0 < r;
  2616. break;
  2617. case CVR_SEEK_LE:
  2618. fMatch = 0 <= r;
  2619. break;
  2620. case CVR_SEEK_GE:
  2621. fMatch = 0 >= r;
  2622. break;
  2623. case CVR_SEEK_GT:
  2624. fMatch = 0 > r;
  2625. break;
  2626. }
  2627. #if DBG_CERTSRV
  2628. dbDumpFileTime(
  2629. DBG_SS_CERTDBI,
  2630. "Column: ",
  2631. (FILETIME const *) pbProp);
  2632. dbDumpFileTime(
  2633. DBG_SS_CERTDBI,
  2634. "Restriction: ",
  2635. (FILETIME const *) pcvr->pbValue);
  2636. #endif
  2637. DBGPRINT((
  2638. DBG_SS_CERTDBI,
  2639. "_CompareColumnValue(ftColumn=%08x:%08x %ws ftRestriction=%08x:%08x) -> r=%d, fMatch=%x\n",
  2640. ((LARGE_INTEGER *) pbProp)->HighPart,
  2641. ((LARGE_INTEGER *) pbProp)->LowPart,
  2642. wszSeekOperator(pcvr->SeekOperator),
  2643. ((LARGE_INTEGER *) pcvr->pbValue)->HighPart,
  2644. ((LARGE_INTEGER *) pcvr->pbValue)->LowPart,
  2645. r,
  2646. fMatch));
  2647. }
  2648. break;
  2649. case JET_coltypText:
  2650. case JET_coltypLongText:
  2651. CSASSERT(
  2652. (1 + wcslen((WCHAR const *) pcvr->pbValue)) * sizeof(WCHAR) ==
  2653. pcvr->cbValue);
  2654. CSASSERT(wcslen((WCHAR const *) pbProp) * sizeof(WCHAR) == cb);
  2655. r = lstrcmpi((WCHAR const *) pcvr->pbValue, (WCHAR const *) pbProp); //pwszValue
  2656. switch (CVR_SEEK_MASK & pcvr->SeekOperator)
  2657. {
  2658. case CVR_SEEK_EQ:
  2659. fMatch = 0 == r;
  2660. break;
  2661. case CVR_SEEK_LT:
  2662. fMatch = 0 < r;
  2663. break;
  2664. case CVR_SEEK_LE:
  2665. fMatch = 0 <= r;
  2666. break;
  2667. case CVR_SEEK_GE:
  2668. fMatch = 0 >= r;
  2669. break;
  2670. case CVR_SEEK_GT:
  2671. fMatch = 0 > r;
  2672. break;
  2673. }
  2674. DBGPRINT((
  2675. DBG_SS_CERTDBI,
  2676. "_CompareColumnValue(pwszColumn=%ws %ws pwszRestriction=%ws) -> r=%d, fMatch=%x\n",
  2677. pbProp, //pwszValue,
  2678. wszSeekOperator(pcvr->SeekOperator),
  2679. pcvr->pbValue,
  2680. r,
  2681. fMatch));
  2682. break;
  2683. case JET_coltypLongBinary:
  2684. if (CVR_SEEK_EQ != (CVR_SEEK_MASK & pcvr->SeekOperator))
  2685. {
  2686. hr = E_INVALIDARG;
  2687. _JumpError(hr, error, "Bad dbcoltyp");
  2688. }
  2689. fMatch = cb == pcvr->cbValue &&
  2690. 0 == memcmp(pcvr->pbValue, pbProp, cb);
  2691. break;
  2692. default:
  2693. hr = E_INVALIDARG;
  2694. _JumpError(hr, error, "Bad dbcoltyp");
  2695. }
  2696. if (!fMatch)
  2697. {
  2698. hr = S_FALSE;
  2699. _JumpError2(hr, error, "No match", S_FALSE);
  2700. }
  2701. error:
  2702. if (NULL != pwszValue)
  2703. {
  2704. LocalFree(pwszValue);
  2705. }
  2706. if (NULL != pbProp && rgbFastBuf != pbProp)
  2707. {
  2708. LocalFree(pbProp);
  2709. }
  2710. return(hr);
  2711. }
  2712. HRESULT
  2713. CCertDB::_MakeSeekKey(
  2714. IN CERTSESSION *pcs,
  2715. IN JET_TABLEID tableid,
  2716. IN DBTABLE const *pdt,
  2717. IN BYTE const *pbValue,
  2718. IN DWORD cbValue)
  2719. {
  2720. HRESULT hr;
  2721. JET_GRBIT grbitKey = JET_bitNewKey;
  2722. CSASSERT(IsValidJetTableId(tableid));
  2723. if (DBTF_INDEXREQUESTID & pdt->dwFlags)
  2724. {
  2725. CSASSERTTHREAD(pcs);
  2726. hr = _dbgJetMakeKey(
  2727. pcs->SesId,
  2728. tableid,
  2729. &pcs->RowId,
  2730. sizeof(pcs->RowId),
  2731. grbitKey);
  2732. _JumpIfError(hr, error, "JetMakeKey(RowId)");
  2733. DBGPRINT((DBG_SS_CERTDBI, "_MakeSeekKey key(RowId):\n"));
  2734. DBGDUMPHEX((DBG_SS_CERTDBI, DH_NOADDRESS, (BYTE *) &pcs->RowId, sizeof(pcs->RowId)));
  2735. grbitKey = 0;
  2736. }
  2737. CSASSERTTHREAD(pcs);
  2738. hr = _dbgJetMakeKey(pcs->SesId, tableid, pbValue, cbValue, grbitKey);
  2739. _JumpIfErrorStr(hr, error, "JetMakeKey", pdt->pwszPropName);
  2740. DBGPRINT((DBG_SS_CERTDBI, "_MakeSeekKey key:\n"));
  2741. DBGDUMPHEX((DBG_SS_CERTDBI, DH_NOADDRESS, pbValue, cbValue));
  2742. error:
  2743. return(myJetHResult(hr));
  2744. }
  2745. HRESULT
  2746. CCertDB::_SeekTable(
  2747. IN CERTSESSION *pcs,
  2748. IN JET_TABLEID tableid,
  2749. IN CERTVIEWRESTRICTION const *pcvr,
  2750. IN DBTABLE const *pdt,
  2751. IN DWORD dwPosition,
  2752. OUT DWORD *pTableFlags
  2753. DBGPARM(IN DBAUXDATA const *pdbaux))
  2754. {
  2755. HRESULT hr;
  2756. DBSEEKDATA SeekData;
  2757. BYTE *pbValue;
  2758. DWORD cbValue;
  2759. BYTE abRangeKey[JET_cbKeyMost];
  2760. DWORD cbRangeKey;
  2761. *pTableFlags = 0;
  2762. CSASSERT(IsValidJetTableId(tableid));
  2763. hr = _JetSeekFromRestriction(pcvr, dwPosition, &SeekData);
  2764. if (CERTSRV_E_PROPERTY_EMPTY == hr)
  2765. {
  2766. hr = S_FALSE;
  2767. }
  2768. _JumpIfError2(hr, error, "_JetSeekFromRestriction", S_FALSE);
  2769. cbValue = pcvr->cbValue;
  2770. pbValue = pcvr->pbValue;
  2771. if (ISTEXTCOLTYP(pdt->dbcoltyp) &&
  2772. NULL != pbValue &&
  2773. cbValue == -1)
  2774. {
  2775. cbValue = wcslen((WCHAR const *) pbValue) * sizeof(WCHAR);
  2776. }
  2777. // If we need to set an index limit, seek to the limit location, and save
  2778. // a copy of the key until after the initial record is located.
  2779. if (CST_SEEKINDEXRANGE & SeekData.SeekFlags)
  2780. {
  2781. hr = _MakeSeekKey(pcs, tableid, pdt, pbValue, cbValue);
  2782. _JumpIfError(hr, error, "_MakeSeekKey");
  2783. CSASSERTTHREAD(pcs);
  2784. hr = _dbgJetSeek(pcs->SesId, tableid, SeekData.grbitSeekRange);
  2785. if ((HRESULT) JET_errRecordNotFound == hr)
  2786. {
  2787. // No record exists past the data we're interested in.
  2788. // Just use the end of the index as the limit.
  2789. _PrintError2(hr, "JetSeek(Range Limit): no key, index end is limit", hr);
  2790. SeekData.SeekFlags &= ~CST_SEEKINDEXRANGE;
  2791. hr = S_OK;
  2792. }
  2793. else if ((HRESULT) JET_wrnSeekNotEqual == hr)
  2794. {
  2795. _PrintError2(hr, "JetSeek(Range): ignoring key not equal", hr);
  2796. hr = S_OK; // Ignore inexact match when seeking >= or <=
  2797. }
  2798. //_JumpIfError2(hr, error, "JetSeek(IndexRange)", S_FALSE);
  2799. _JumpIfError(hr, error, "JetSeek(IndexRange)");
  2800. }
  2801. // If we found a valid key at the limit location, save it now.
  2802. if (CST_SEEKINDEXRANGE & SeekData.SeekFlags)
  2803. {
  2804. CSASSERTTHREAD(pcs);
  2805. hr = _dbgJetRetrieveKey(
  2806. pcs->SesId,
  2807. tableid,
  2808. abRangeKey,
  2809. ARRAYSIZE(abRangeKey),
  2810. &cbRangeKey,
  2811. 0);
  2812. _JumpIfError(hr, error, "JetRetrieveKey");
  2813. DBGPRINT((DBG_SS_CERTDBI, "RetrieveKey(Range):\n"));
  2814. DBGDUMPHEX((DBG_SS_CERTDBI, DH_NOADDRESS, abRangeKey, cbRangeKey));
  2815. }
  2816. // Locate the initial record: seek to a key or move to one end of the index
  2817. if (CST_SEEKNOTMOVE & SeekData.SeekFlags)
  2818. {
  2819. hr = _MakeSeekKey(pcs, tableid, pdt, pbValue, cbValue);
  2820. _JumpIfError(hr, error, "_MakeSeekKey");
  2821. CSASSERTTHREAD(pcs);
  2822. hr = _dbgJetSeek(pcs->SesId, tableid, SeekData.grbitInitial);
  2823. if ((HRESULT) JET_errRecordNotFound == hr)
  2824. {
  2825. // Routine GetAttribute/Extension call:
  2826. _PrintError2(hr, "JetSeek: Property EMPTY", hr);
  2827. hr = S_FALSE;
  2828. }
  2829. else if ((HRESULT) JET_wrnSeekNotEqual == hr)
  2830. {
  2831. hr = S_OK; // Ignore inexact match when seeking >= or <=
  2832. }
  2833. _JumpIfError2(hr, error, "JetSeek(Initial)", S_FALSE);
  2834. }
  2835. else
  2836. {
  2837. // grbitInitial is a move count here, not a grbit
  2838. CSASSERTTHREAD(pcs);
  2839. hr = _dbgJetMove(pcs->SesId, tableid, SeekData.grbitInitial, 0);
  2840. if ((HRESULT) JET_errNoCurrentRecord == hr)
  2841. {
  2842. // Routine Enumerate call:
  2843. // _JumpIfError(hr, error, "JetMove: no more elements");
  2844. hr = S_FALSE;
  2845. }
  2846. _JumpIfError2(hr, error, "JetMove(End)", S_FALSE);
  2847. // If moving the cursor to the last element, we want to position
  2848. // ourselves to step over the end again (skip the last element).
  2849. //
  2850. // If moving to the first element, we want to position ourselves on the
  2851. // first element and use it before stepping
  2852. if (SEEKPOS_FIRST == dwPosition || SEEKPOS_INDEXFIRST == dwPosition)
  2853. {
  2854. SeekData.SeekFlags |= CST_SEEKUSECURRENT;
  2855. }
  2856. }
  2857. // We're done seeking around; set the index limit from the saved key.
  2858. if (CST_SEEKINDEXRANGE & SeekData.SeekFlags)
  2859. {
  2860. CSASSERTTHREAD(pcs);
  2861. hr = _dbgJetMakeKey(
  2862. pcs->SesId,
  2863. tableid,
  2864. abRangeKey,
  2865. cbRangeKey,
  2866. JET_bitNormalizedKey);
  2867. _JumpIfError(hr, error, "JetMakeKey");
  2868. DBGPRINT((DBG_SS_CERTDBI, "RangeKey:\n"));
  2869. DBGDUMPHEX((DBG_SS_CERTDBI, DH_NOADDRESS, abRangeKey, cbRangeKey));
  2870. CSASSERTTHREAD(pcs);
  2871. hr = _dbgJetSetIndexRange(
  2872. pcs->SesId,
  2873. tableid,
  2874. SeekData.grbitRange);
  2875. if ((HRESULT) JET_errNoCurrentRecord == hr)
  2876. {
  2877. // No records to enumerate:
  2878. _PrintError2(hr, "JetSetIndexRange: no records to enumerate", hr);
  2879. hr = S_FALSE;
  2880. }
  2881. _JumpIfError2(hr, error, "JetSetIndexRange", S_FALSE);
  2882. }
  2883. DBGCODE(_DumpRowId("post-_SeekTable", pcs, tableid));
  2884. DBGCODE(_DumpColumn("post-_SeekTable", pcs, tableid, pdt));
  2885. *pTableFlags = SeekData.SeekFlags;
  2886. error:
  2887. if (S_FALSE == hr)
  2888. {
  2889. DWORD dwPosition2 = dwPosition;
  2890. switch (dwPosition)
  2891. {
  2892. case SEEKPOS_FIRST:
  2893. dwPosition2 = SEEKPOS_INDEXFIRST;
  2894. break;
  2895. case SEEKPOS_LAST:
  2896. dwPosition2 = SEEKPOS_INDEXLAST;
  2897. break;
  2898. }
  2899. if (dwPosition2 != dwPosition)
  2900. {
  2901. hr = _SeekTable(
  2902. pcs,
  2903. tableid,
  2904. pcvr,
  2905. pdt,
  2906. dwPosition2,
  2907. pTableFlags
  2908. DBGPARM(pdbaux));
  2909. _PrintIfError2(hr, "_SeekTable: recurse on index first/last", S_FALSE);
  2910. }
  2911. }
  2912. #if DBG_CERTSRV
  2913. if (S_OK != hr)
  2914. {
  2915. DumpRestriction(DBG_SS_CERTDBI, 0, pcvr);
  2916. }
  2917. #endif
  2918. return(myJetHResult(hr));
  2919. }
  2920. HRESULT
  2921. CCertDB::_MoveTable(
  2922. IN CERTSESSION *pcs,
  2923. IN DWORD ccvr,
  2924. IN CERTVIEWRESTRICTION const *pcvr,
  2925. IN LONG cskip,
  2926. OUT LONG *pcskipped)
  2927. {
  2928. HRESULT hr;
  2929. DWORD cb;
  2930. DBAUXDATA const *pdbaux;
  2931. DBTABLE const *pdt;
  2932. DWORD icvr;
  2933. DWORD SeekFlags;
  2934. LONG lSeek;
  2935. LONG skipIncrement;
  2936. LONG cskipRemain;
  2937. BOOL fHitEnd = FALSE;
  2938. LONG cskippeddummy;
  2939. CERTSESSIONTABLE *pTable;
  2940. CERTSESSIONTABLE *pTable2;
  2941. *pcskipped = 0;
  2942. DBGPRINT((
  2943. DBG_SS_CERTDBI,
  2944. "_MoveTable called(ccvr=%d, cskip=%d, flags=%ws)\n",
  2945. ccvr,
  2946. cskip,
  2947. wszCSFFlags(pcs->SesFlags)));
  2948. hr = _MapPropIdIndex(pcvr->ColumnIndex, &pdt, NULL);
  2949. _JumpIfError(hr, error, "_MapPropIdIndex");
  2950. pTable = &pcs->aTable[CSTI_PRIMARY];
  2951. pTable2 = NULL;
  2952. switch (DTI_TABLEMASK & pcvr->ColumnIndex)
  2953. {
  2954. case DTI_REQUESTTABLE:
  2955. pdbaux = &g_dbauxRequests;
  2956. pTable2 = &pcs->aTable[CSTI_CERTIFICATE];
  2957. break;
  2958. case DTI_CERTIFICATETABLE:
  2959. pdbaux = &g_dbauxCertificates;
  2960. pTable = &pcs->aTable[CSTI_CERTIFICATE];
  2961. pTable2 = &pcs->aTable[CSTI_PRIMARY];
  2962. break;
  2963. case DTI_EXTENSIONTABLE:
  2964. pdbaux = &g_dbauxExtensions;
  2965. break;
  2966. case DTI_ATTRIBUTETABLE:
  2967. pdbaux = &g_dbauxAttributes;
  2968. break;
  2969. case DTI_CRLTABLE:
  2970. pdbaux = &g_dbauxCRLs;
  2971. break;
  2972. default:
  2973. hr = E_INVALIDARG;
  2974. _JumpError(hr, error, "ColumnIndex Table");
  2975. }
  2976. DBGPRINT((
  2977. DBG_SS_CERTDBI,
  2978. "_MoveTable(Table=%hs, TableFlags=%ws)\n",
  2979. pdbaux->pszTable,
  2980. wszCSTFlags(pTable->TableFlags)));
  2981. if (NULL != pTable2 && IsValidJetTableId(pTable2->TableId))
  2982. {
  2983. DBGPRINT((
  2984. DBG_SS_CERTDBI,
  2985. "_MoveTable(Table2=%hs, TableFlags2=%ws)\n",
  2986. &g_dbauxCertificates == pdbaux?
  2987. g_dbauxRequests.pszTable :
  2988. g_dbauxCertificates.pszTable,
  2989. wszCSTFlags(pTable2->TableFlags)));
  2990. }
  2991. switch (pcvr->SortOrder)
  2992. {
  2993. case CVR_SORT_DESCEND:
  2994. lSeek = JET_MovePrevious;
  2995. break;
  2996. case CVR_SORT_NONE:
  2997. default:
  2998. CSASSERT(!"bad pcvr->SortOrder"); // shouldn't get this far
  2999. // FALL THROUGH
  3000. case CVR_SORT_ASCEND:
  3001. lSeek = JET_MoveNext;
  3002. break;
  3003. }
  3004. // Add one to the skip count for the implicit Next operation. Next
  3005. // always moves forward one, even when a negative skip count moves
  3006. // backward. The net result may be a forward or backward skip.
  3007. cskipRemain = cskip + 1;
  3008. skipIncrement = 1;
  3009. if (0 > cskipRemain)
  3010. {
  3011. CSASSERT(JET_MoveNext == -1 * JET_MovePrevious);
  3012. lSeek *= -1; // Seek in opposite direction
  3013. cskipRemain *= -1; // make the skip count positive
  3014. skipIncrement = -1;
  3015. }
  3016. CSASSERT(0 <= cskipRemain);
  3017. while (0 != cskipRemain)
  3018. {
  3019. DBGPRINT((
  3020. DBG_SS_CERTDBI,
  3021. "_MoveTable loop: ccvr=%d, cskipRemain=%d, lSeek=%d, flags=%ws\n",
  3022. ccvr,
  3023. cskipRemain,
  3024. lSeek,
  3025. wszCSFFlags(pcs->SesFlags)));
  3026. DBGCODE(_DumpRowId("_MoveTable(loop top)", pcs, pTable->TableId));
  3027. if (CSF_VIEW & pcs->SesFlags)
  3028. {
  3029. hr = TestShutDownState();
  3030. _JumpIfError(hr, error, "TestShutDownState");
  3031. }
  3032. if (CSF_VIEWRESET & pcs->SesFlags)
  3033. {
  3034. hr = _SeekTable(
  3035. pcs,
  3036. pTable->TableId,
  3037. pcvr,
  3038. pdt,
  3039. SEEKPOS_FIRST,
  3040. &pTable->TableFlags
  3041. DBGPARM(pdbaux));
  3042. _JumpIfError(hr, error, "_SeekTable");
  3043. pcs->SesFlags &= ~CSF_VIEWRESET;
  3044. }
  3045. if (0 == (CST_SEEKUSECURRENT & pTable->TableFlags))
  3046. {
  3047. CSASSERTTHREAD(pcs);
  3048. hr = _dbgJetMove(pcs->SesId, pTable->TableId, lSeek, 0);
  3049. if ((HRESULT) JET_errNoCurrentRecord == hr)
  3050. {
  3051. _PrintIfError2(hr, "JetMove: no more elements", hr);
  3052. if (fHitEnd)
  3053. {
  3054. // we hit the end trying to backstep! We're done
  3055. hr = S_FALSE;
  3056. _JumpError(hr, error, "JetMove: db backstep hit beginning");
  3057. }
  3058. fHitEnd = TRUE;
  3059. // NOTE: Tough case
  3060. //
  3061. // We just hit the end of the database index, which could be a
  3062. // virtual end or the real end. To recover, we call _SeekTable
  3063. // to position ourselves at the last legal element computed by
  3064. // the 1st restriction, then allow this routine to rewind until
  3065. // we position ourselves on the very last legal element as
  3066. // computed by 2nd through Nth restrictions.
  3067. // Routine Seek call to position at end of enumeration
  3068. hr = _SeekTable(
  3069. pcs,
  3070. pTable->TableId,
  3071. pcvr,
  3072. pdt,
  3073. SEEKPOS_LAST, // cursor at end
  3074. &pTable->TableFlags
  3075. DBGPARM(pdbaux));
  3076. _JumpIfError(hr, error, "_SeekTable moving to last elt");
  3077. // now fall through, allow other restrictions to test for 1st
  3078. // valid element
  3079. lSeek *= -1; // Seek in opposite direction
  3080. cskipRemain = 1; // one valid element
  3081. pcskipped = &cskippeddummy; // stop counting skipped rows
  3082. }
  3083. _JumpIfError2(hr, error, "JetMove", S_FALSE);
  3084. DBGCODE(_DumpRowId("_MoveTable(post-move)", pcs, pTable->TableId));
  3085. hr = _CompareColumnValue(pcs, pcvr);
  3086. _JumpIfError2(hr, error, "_CompareColumnValue", S_FALSE);
  3087. }
  3088. pTable->TableFlags &= ~CST_SEEKUSECURRENT;
  3089. // Fetch RowId from the first table, form a key for the second
  3090. // table and seek to the corresponding record in the second table.
  3091. cb = sizeof(pcs->RowId);
  3092. hr = _RetrieveColumn(
  3093. pcs,
  3094. pTable->TableId,
  3095. pdbaux->pdtRowId->dbcolumnid,
  3096. pdbaux->pdtRowId->dbcoltyp,
  3097. &cb,
  3098. (BYTE *) &pcs->RowId);
  3099. _JumpIfError(hr, error, "_RetrieveColumn");
  3100. DBGPRINT((
  3101. DBG_SS_CERTDBI,
  3102. "_MoveTable(Primary) %hs --> RowId=%d\n",
  3103. pdbaux->pszTable,
  3104. pcs->RowId));
  3105. if (NULL != pTable2 && IsValidJetTableId(pTable2->TableId))
  3106. {
  3107. CSASSERTTHREAD(pcs);
  3108. hr = _dbgJetMakeKey(
  3109. pcs->SesId,
  3110. pTable2->TableId,
  3111. &pcs->RowId,
  3112. sizeof(pcs->RowId),
  3113. JET_bitNewKey);
  3114. _JumpIfError(hr, error, "JetMakeKey");
  3115. hr = _dbgJetSeek(pcs->SesId, pTable2->TableId, JET_bitSeekEQ);
  3116. if ((HRESULT) JET_errRecordNotFound == hr)
  3117. {
  3118. // Database is inconsistent
  3119. hr = S_FALSE;
  3120. }
  3121. _JumpIfError2(hr, error, "JetSeek", S_FALSE);
  3122. DBGPRINT((
  3123. DBG_SS_CERTDBI,
  3124. "_MoveTable(Secondary) %hs --> RowId=%d\n",
  3125. &g_dbauxCertificates == pdbaux?
  3126. g_dbauxRequests.pszTable :
  3127. g_dbauxCertificates.pszTable,
  3128. pcs->RowId));
  3129. }
  3130. // Now verify that any addtional restrictions are satisfied
  3131. for (icvr = 1; icvr < ccvr; icvr++)
  3132. {
  3133. #if 0
  3134. printf(
  3135. "RowId=%u, cvr[%u]: seek=%x, *pb=%x\n",
  3136. pcs->RowId,
  3137. icvr,
  3138. pcvr[icvr].SeekOperator,
  3139. *(DWORD *) pcvr[icvr].pbValue);
  3140. #endif
  3141. hr = _CompareColumnValue(pcs, &pcvr[icvr]);
  3142. if (S_FALSE == hr)
  3143. {
  3144. break; // skip row silently
  3145. }
  3146. _JumpIfError(hr, error, "_CompareColumnValue");
  3147. }
  3148. if (icvr >= ccvr)
  3149. {
  3150. *pcskipped += skipIncrement;
  3151. cskipRemain--; // found a matching row
  3152. }
  3153. } // while (cskipRemain)
  3154. error:
  3155. // if we nailed the end and rewound, return failure
  3156. if (fHitEnd && S_OK == hr)
  3157. {
  3158. hr = S_FALSE;
  3159. }
  3160. return(myJetHResult(hr));
  3161. }
  3162. HRESULT
  3163. CCertDB::_GetResultRow(
  3164. IN CERTSESSION *pcs,
  3165. IN DWORD ccvr,
  3166. IN CERTVIEWRESTRICTION const *pcvr,
  3167. IN LONG cskip,
  3168. IN DWORD ccolOut,
  3169. IN DWORD const *acolOut,
  3170. OUT CERTDBRESULTROW *pelt,
  3171. OUT LONG *pcskipped)
  3172. {
  3173. HRESULT hr;
  3174. DWORD iCol;
  3175. BYTE *pbProp = NULL;
  3176. BYTE *pbT;
  3177. DWORD cbAlloc = 0;
  3178. DWORD cbProp;
  3179. WCHAR awc[CCH_DBMAXTEXT_LONG];
  3180. DBGPRINT((
  3181. DBG_SS_CERTDBI,
  3182. "_GetResultRow(ccvr=%d, ccolOut=%d, cskip=%d, flags=%ws)\n",
  3183. ccvr,
  3184. ccolOut,
  3185. cskip,
  3186. wszCSFFlags(pcs->SesFlags)));
  3187. // This may move past the end of the database index entries.
  3188. // In that case, we're positioned at the end of the index.
  3189. hr = _MoveTable(pcs, ccvr, pcvr, cskip, pcskipped);
  3190. _JumpIfError2(hr, error, "_MoveTable", S_FALSE);
  3191. DBGPRINT((DBG_SS_CERTDBI, "_GetResultRow: RowId=%d\n", pcs->RowId));
  3192. pelt->acol = (CERTDBRESULTCOLUMN *) CoTaskMemAlloc(
  3193. ccolOut * sizeof(pelt->acol[0]));
  3194. if (NULL == pelt->acol)
  3195. {
  3196. hr = E_OUTOFMEMORY;
  3197. _JumpError(hr, error, "alloc acol");
  3198. }
  3199. ZeroMemory(pelt->acol, ccolOut * sizeof(pelt->acol[0]));
  3200. pelt->rowid = pcs->RowId;
  3201. pelt->ccol = ccolOut;
  3202. for (iCol = 0; iCol < ccolOut; iCol++)
  3203. {
  3204. DBTABLE const *pdt;
  3205. CERTDBRESULTCOLUMN *pCol;
  3206. pCol = &pelt->acol[iCol];
  3207. pCol->Index = acolOut[iCol];
  3208. hr = _MapPropIdIndex(pCol->Index, &pdt, &pCol->Type);
  3209. _JumpIfError(hr, error, "_MapPropIdIndex");
  3210. while (TRUE)
  3211. {
  3212. cbProp = cbAlloc;
  3213. hr = GetProperty(pcs, pdt, &cbProp, pbProp);
  3214. if (CERTSRV_E_PROPERTY_EMPTY == hr)
  3215. {
  3216. break; // leave 0 size, NULL pointer
  3217. }
  3218. if (HRESULT_FROM_WIN32(ERROR_BUFFER_OVERFLOW) != hr)
  3219. {
  3220. _JumpIfError(hr, error, "GetProperty");
  3221. }
  3222. if (cbAlloc >= cbProp)
  3223. {
  3224. CSASSERT(S_OK == hr);
  3225. CSASSERT(0 != cbProp && NULL != pbProp);
  3226. break; // property value is in cbProp, pbProp
  3227. }
  3228. // Property value is too large for the buffer -- grow it
  3229. if (NULL == pbProp)
  3230. {
  3231. pbT = (BYTE *) LocalAlloc(LMEM_FIXED, cbProp);
  3232. }
  3233. else
  3234. {
  3235. pbT = (BYTE *) LocalReAlloc(pbProp, cbProp, LMEM_MOVEABLE);
  3236. }
  3237. if (NULL == pbT)
  3238. {
  3239. hr = E_OUTOFMEMORY;
  3240. _JumpError(hr, error, "LocalAlloc/LocalReAlloc property");
  3241. }
  3242. pbProp = pbT;
  3243. cbAlloc = cbProp;
  3244. }
  3245. if (CERTSRV_E_PROPERTY_EMPTY != hr)
  3246. {
  3247. BYTE const *pb = pbProp;
  3248. DWORD cwc;
  3249. if (PROPTYPE_STRING == (PROPTYPE_MASK & pCol->Type))
  3250. {
  3251. CSASSERT( *((WCHAR*) &(pbProp[cbProp])) == L'\0');
  3252. cbProp += sizeof(WCHAR); // include NULL term
  3253. }
  3254. pCol->cbValue = cbProp;
  3255. pCol->pbValue = (BYTE *) CoTaskMemAlloc(cbProp);
  3256. if (NULL == pCol->pbValue)
  3257. {
  3258. hr = E_OUTOFMEMORY;
  3259. _JumpError(hr, error, "CoTaskMemAlloc");
  3260. }
  3261. CopyMemory(pCol->pbValue, pb, pCol->cbValue);
  3262. }
  3263. DBGPRINT((
  3264. DBG_SS_CERTDBI,
  3265. "_GetResultRow: fetch %ws.%ws: type=%x cb=%x\n",
  3266. wszTable(pdt->dwTable),
  3267. pdt->pwszPropName,
  3268. pCol->Type,
  3269. pCol->cbValue));
  3270. }
  3271. hr = S_OK;
  3272. error:
  3273. if (NULL != pbProp)
  3274. {
  3275. LocalFree(pbProp);
  3276. }
  3277. return(hr);
  3278. }
  3279. HRESULT
  3280. CCertDB::ReleaseResultRow(
  3281. IN ULONG celt,
  3282. IN OUT CERTDBRESULTROW *rgelt)
  3283. {
  3284. HRESULT hr;
  3285. DWORD iRow;
  3286. DWORD iCol;
  3287. CERTDBRESULTROW *pResultRow;
  3288. if (NULL == rgelt)
  3289. {
  3290. hr = E_POINTER;
  3291. _JumpError(hr, error, "NULL parm");
  3292. }
  3293. for (iRow = 0; iRow < celt; iRow++)
  3294. {
  3295. pResultRow = &rgelt[iRow];
  3296. if (NULL != pResultRow->acol)
  3297. {
  3298. for (iCol = 0; iCol < pResultRow->ccol; iCol++)
  3299. {
  3300. if (NULL != pResultRow->acol[iCol].pbValue)
  3301. {
  3302. CoTaskMemFree(pResultRow->acol[iCol].pbValue);
  3303. pResultRow->acol[iCol].pbValue = NULL;
  3304. }
  3305. }
  3306. CoTaskMemFree(pResultRow->acol);
  3307. pResultRow->acol = NULL;
  3308. }
  3309. pResultRow->ccol = 0;
  3310. }
  3311. hr = S_OK;
  3312. error:
  3313. return(hr);
  3314. }
  3315. HRESULT
  3316. CCertDB::EnumerateSetup(
  3317. IN CERTSESSION *pcs,
  3318. IN OUT DWORD *pFlags,
  3319. OUT JET_TABLEID *ptableid)
  3320. {
  3321. HRESULT hr;
  3322. JET_TABLEID tableid = 0;
  3323. DBAUXDATA const *pdbaux;
  3324. if (NULL == pcs || NULL == ptableid)
  3325. {
  3326. hr = E_POINTER;
  3327. _JumpError(hr, error, "NULL parm");
  3328. }
  3329. switch (*pFlags)
  3330. {
  3331. case CIE_TABLE_ATTRIBUTES:
  3332. pdbaux = &g_dbauxAttributes;
  3333. break;
  3334. case CIE_TABLE_EXTENSIONS:
  3335. pdbaux = &g_dbauxExtensions;
  3336. break;
  3337. default:
  3338. hr = E_INVALIDARG;
  3339. _JumpError(hr, error, "*pFlags");
  3340. }
  3341. CSASSERTTHREAD(pcs);
  3342. hr = _dbgJetOpenTable(
  3343. pcs->SesId,
  3344. pcs->DBId,
  3345. pdbaux->pszTable,
  3346. NULL,
  3347. 0,
  3348. 0,
  3349. &tableid);
  3350. _JumpIfError(hr, error, "JetOpenTable");
  3351. CSASSERTTHREAD(pcs);
  3352. hr = _dbgJetSetCurrentIndex2(
  3353. pcs->SesId,
  3354. tableid,
  3355. pdbaux->pszRowIdIndex,
  3356. JET_bitMoveFirst);
  3357. _JumpIfError(hr, error, "JetSetCurrentIndex2");
  3358. CSASSERTTHREAD(pcs);
  3359. hr = _dbgJetMakeKey(
  3360. pcs->SesId,
  3361. tableid,
  3362. &pcs->RowId,
  3363. sizeof(pcs->RowId),
  3364. JET_bitNewKey);
  3365. _JumpIfError(hr, error, "JetMakeKey");
  3366. *pFlags |= CIE_RESET;
  3367. CSASSERT(IsValidJetTableId(tableid));
  3368. *ptableid = tableid;
  3369. tableid = 0;
  3370. error:
  3371. if (IsValidJetTableId(tableid))
  3372. {
  3373. CSASSERTTHREAD(pcs);
  3374. _dbgJetCloseTable(pcs->SesId, tableid);
  3375. }
  3376. return(myJetHResult(hr));
  3377. }
  3378. HRESULT
  3379. CCertDB::_EnumerateMove(
  3380. IN CERTSESSION *pcs,
  3381. IN OUT DWORD *pFlags,
  3382. IN DBAUXDATA const *pdbaux,
  3383. IN JET_TABLEID tableid,
  3384. IN LONG cskip)
  3385. {
  3386. HRESULT hr;
  3387. DWORD cb;
  3388. DWORD RowId;
  3389. LONG lSeek;
  3390. DBGPRINT((
  3391. DBG_SS_CERTDBI,
  3392. "_EnumerateMove(cskip=%d, flags=%x%hs)\n",
  3393. cskip,
  3394. *pFlags,
  3395. (CIE_RESET & *pFlags)? " Reset" : ""));
  3396. CSASSERT(IsValidJetTableId(tableid));
  3397. if (CIE_RESET & *pFlags)
  3398. {
  3399. CSASSERTTHREAD(pcs);
  3400. hr = _dbgJetSeek(pcs->SesId, tableid, JET_bitSeekEQ);
  3401. if ((HRESULT) JET_errRecordNotFound == hr)
  3402. {
  3403. hr = S_FALSE;
  3404. }
  3405. _JumpIfError2(hr, error, "JetSeek", S_FALSE);
  3406. *pFlags &= ~CIE_RESET;
  3407. }
  3408. else
  3409. {
  3410. // Add one to the skip count for the implicit Next operation. Next
  3411. // always moves forward one, even when a negative skip count moves
  3412. // backward. The net result may be a forward or backward skip.
  3413. cskip++;
  3414. }
  3415. if (0 != cskip)
  3416. {
  3417. lSeek = JET_MoveNext * cskip;
  3418. CSASSERT(JET_MoveNext == -1 * JET_MovePrevious);
  3419. CSASSERTTHREAD(pcs);
  3420. hr = _dbgJetMove(pcs->SesId, tableid, lSeek, 0);
  3421. if ((HRESULT) JET_errNoCurrentRecord == hr)
  3422. {
  3423. hr = S_FALSE;
  3424. }
  3425. _JumpIfError2(hr, error, "JetMove", S_FALSE);
  3426. // Make sure this entry is for the same request:
  3427. cb = sizeof(RowId);
  3428. hr = _RetrieveColumn(
  3429. pcs,
  3430. tableid,
  3431. pdbaux->pdtRowId->dbcolumnid,
  3432. pdbaux->pdtRowId->dbcoltyp,
  3433. &cb,
  3434. (BYTE *) &RowId);
  3435. _JumpIfError(hr, error, "_RetrieveColumn");
  3436. if (RowId != pcs->RowId)
  3437. {
  3438. hr = S_FALSE;
  3439. goto error;
  3440. }
  3441. }
  3442. hr = S_OK;
  3443. error:
  3444. return(myJetHResult(hr));
  3445. }
  3446. HRESULT
  3447. CCertDB::EnumerateNext(
  3448. IN CERTSESSION *pcs,
  3449. IN OUT DWORD *pFlags,
  3450. IN JET_TABLEID tableid,
  3451. IN LONG cskip,
  3452. IN ULONG celt,
  3453. OUT CERTDBNAME *rgelt,
  3454. OUT ULONG *pceltFetched)
  3455. {
  3456. HRESULT hr;
  3457. DWORD cb;
  3458. DWORD cwc;
  3459. DWORD RowId;
  3460. CERTDBNAME *pelt;
  3461. WCHAR wszTmp[MAX_PATH];
  3462. DBAUXDATA const *pdbaux;
  3463. if (NULL == pcs || NULL == rgelt || NULL == pceltFetched)
  3464. {
  3465. hr = E_POINTER;
  3466. _JumpError(hr, error, "NULL parm");
  3467. }
  3468. ZeroMemory(rgelt, celt * sizeof(rgelt[0]));
  3469. CSASSERT(IsValidJetTableId(tableid));
  3470. switch (CIE_TABLE_MASK & *pFlags)
  3471. {
  3472. case CIE_TABLE_ATTRIBUTES:
  3473. pdbaux = &g_dbauxAttributes;
  3474. break;
  3475. case CIE_TABLE_EXTENSIONS:
  3476. pdbaux = &g_dbauxExtensions;
  3477. break;
  3478. default:
  3479. hr = E_INVALIDARG;
  3480. _JumpError(hr, error, "*pFlags");
  3481. }
  3482. hr = S_OK;
  3483. for (pelt = rgelt; pelt < &rgelt[celt]; pelt++)
  3484. {
  3485. hr = _EnumerateMove(pcs, pFlags, pdbaux, tableid, cskip);
  3486. if (S_FALSE == hr)
  3487. {
  3488. break;
  3489. }
  3490. _JumpIfError(hr, error, "_EnumerateMove");
  3491. cskip = 0;
  3492. cb = sizeof(wszTmp);
  3493. hr = _RetrieveColumn(
  3494. pcs,
  3495. tableid,
  3496. pdbaux->pdtName->dbcolumnid,
  3497. pdbaux->pdtName->dbcoltyp,
  3498. &cb,
  3499. (BYTE *) wszTmp);
  3500. _JumpIfError(hr, error, "_RetrieveColumn");
  3501. CSASSERT(0 == (cb % sizeof(WCHAR))); // integer # of wchars
  3502. CSASSERT(L'\0' == wszTmp[cb/sizeof(WCHAR)]); // zero term
  3503. hr = _DupString(NULL, wszTmp, &pelt->pwszName);
  3504. _JumpIfError(hr, error, "_DupString");
  3505. }
  3506. *pceltFetched = SAFE_SUBTRACT_POINTERS(pelt, rgelt);
  3507. error:
  3508. if (S_OK != hr && S_FALSE != hr)
  3509. {
  3510. if (NULL != rgelt)
  3511. {
  3512. for (pelt = rgelt; pelt < &rgelt[celt]; pelt++)
  3513. {
  3514. if (NULL != pelt->pwszName)
  3515. {
  3516. CoTaskMemFree(pelt->pwszName);
  3517. pelt->pwszName = NULL;
  3518. }
  3519. }
  3520. }
  3521. }
  3522. return(myJetHResult(hr));
  3523. }
  3524. HRESULT
  3525. CCertDB::EnumerateClose(
  3526. IN CERTSESSION *pcs,
  3527. IN JET_TABLEID tableid)
  3528. {
  3529. HRESULT hr;
  3530. if (NULL == pcs)
  3531. {
  3532. hr = E_POINTER;
  3533. _JumpError(hr, error, "NULL parm");
  3534. }
  3535. CSASSERT(IsValidJetTableId(tableid));
  3536. CSASSERTTHREAD(pcs);
  3537. hr = _dbgJetCloseTable(pcs->SesId, tableid);
  3538. _JumpIfError(hr, error, "JetCloseTable");
  3539. error:
  3540. return(myJetHResult(hr));
  3541. }
  3542. HRESULT
  3543. CCertDB::_BuildColumnIds(
  3544. IN CERTSESSION *pcs,
  3545. IN CHAR const *pszTableName,
  3546. IN DBTABLE *pdt)
  3547. {
  3548. HRESULT hr;
  3549. JET_TABLEID tableid;
  3550. JET_COLUMNDEF columndef;
  3551. BOOL fOpen = FALSE;
  3552. hr = _dbgJetOpenTable(
  3553. pcs->SesId,
  3554. pcs->DBId,
  3555. pszTableName,
  3556. NULL,
  3557. 0,
  3558. 0,
  3559. &tableid);
  3560. _JumpIfError(hr, error, "JetOpenTable");
  3561. fOpen = TRUE;
  3562. CSASSERT(IsValidJetTableId(tableid));
  3563. for ( ; NULL != pdt->pwszPropName; pdt++)
  3564. {
  3565. hr = _dbgJetGetColumnInfo(
  3566. pcs->SesId,
  3567. pcs->DBId,
  3568. pszTableName,
  3569. pdt->pszFieldName,
  3570. &columndef,
  3571. sizeof(columndef),
  3572. JET_ColInfo);
  3573. if ((HRESULT) JET_errColumnNotFound == hr &&
  3574. (DBTF_SOFTFAIL & pdt->dwFlags))
  3575. {
  3576. pdt->dwFlags |= DBTF_MISSING;
  3577. pdt->dbcolumnid = -1;
  3578. DBGPRINT((
  3579. DBG_SS_CERTDB,
  3580. "_BuildColumnIds: %hs.%hs Ignoring missing column\n",
  3581. pszTableName,
  3582. pdt->pszFieldName));
  3583. hr = S_OK;
  3584. continue;
  3585. }
  3586. _JumpIfError(hr, error, "JetGetColumnInfo");
  3587. pdt->dbcolumnid = columndef.columnid;
  3588. CSASSERT(
  3589. pdt->dbcoltyp == columndef.coltyp ||
  3590. (ISTEXTCOLTYP(pdt->dbcoltyp) && ISTEXTCOLTYP(columndef.coltyp)));
  3591. if (JET_coltypText == pdt->dbcoltyp)
  3592. {
  3593. CSASSERT(pdt->dwcbMax == pdt->dbcolumnMax);
  3594. CSASSERT(0 != pdt->dbcolumnMax);
  3595. CSASSERT(CB_DBMAXTEXT_MAXINTERNAL >= pdt->dbcolumnMax);
  3596. }
  3597. else if (JET_coltypLongText == pdt->dbcoltyp)
  3598. {
  3599. CSASSERT(pdt->dwcbMax == pdt->dbcolumnMax);
  3600. CSASSERT(CB_DBMAXTEXT_MAXINTERNAL < pdt->dbcolumnMax);
  3601. }
  3602. else if (JET_coltypLongBinary == pdt->dbcoltyp)
  3603. {
  3604. CSASSERT(pdt->dwcbMax == pdt->dbcolumnMax);
  3605. CSASSERT(0 != pdt->dbcolumnMax);
  3606. }
  3607. else if (JET_coltypDateTime == pdt->dbcoltyp)
  3608. {
  3609. CSASSERT(sizeof(DATE) == pdt->dwcbMax);
  3610. CSASSERT(0 == pdt->dbcolumnMax);
  3611. }
  3612. else if (JET_coltypLong == pdt->dbcoltyp)
  3613. {
  3614. CSASSERT(sizeof(LONG) == pdt->dwcbMax);
  3615. CSASSERT(0 == pdt->dbcolumnMax);
  3616. }
  3617. else
  3618. {
  3619. DBGPRINT((
  3620. DBG_SS_CERTDB,
  3621. "_BuildColumnIds: %hs.%hs Unknown column type %u\n",
  3622. pszTableName,
  3623. pdt->pszFieldName,
  3624. pdt->dbcoltyp));
  3625. CSASSERT(!"Unknown column type");
  3626. }
  3627. if (pdt->dwcbMax != columndef.cbMax)
  3628. {
  3629. DBGPRINT((
  3630. DBG_SS_CERTDB,
  3631. "_BuildColumnIds: %hs.%hs length %u, expected %u\n",
  3632. pszTableName,
  3633. pdt->pszFieldName,
  3634. columndef.cbMax,
  3635. pdt->dwcbMax));
  3636. // max size can only be increased...
  3637. if (pdt->dwcbMax > columndef.cbMax)
  3638. {
  3639. JET_DDLMAXCOLUMNSIZE jdmcs;
  3640. jdmcs.szTable = const_cast<char *>(pszTableName);
  3641. jdmcs.szColumn = const_cast<char *>(pdt->pszFieldName);
  3642. jdmcs.cbMax = pdt->dwcbMax;
  3643. hr = _dbgJetConvertDDL(
  3644. pcs->SesId,
  3645. pcs->DBId,
  3646. opDDLConvIncreaseMaxColumnSize,
  3647. &jdmcs,
  3648. sizeof(jdmcs));
  3649. _PrintIfError(hr, "JetConvertDDL");
  3650. if (S_OK == hr)
  3651. {
  3652. m_fDBRestart = TRUE;
  3653. }
  3654. }
  3655. }
  3656. pdt->dbcolumnidOld = -1;
  3657. if (chTEXTPREFIX == *pdt->pszFieldName ||
  3658. (DBTF_COLUMNRENAMED & pdt->dwFlags))
  3659. {
  3660. char const *pszFieldName = &pdt->pszFieldName[1];
  3661. if (DBTF_COLUMNRENAMED & pdt->dwFlags)
  3662. {
  3663. pszFieldName += strlen(pszFieldName) + 1;
  3664. }
  3665. CSASSERT(
  3666. chTEXTPREFIX != *pszTableName ||
  3667. ISTEXTCOLTYP(columndef.coltyp));
  3668. hr = _dbgJetGetColumnInfo(
  3669. pcs->SesId,
  3670. pcs->DBId,
  3671. pszTableName,
  3672. pszFieldName,
  3673. &columndef,
  3674. sizeof(columndef),
  3675. JET_ColInfo);
  3676. if (S_OK == hr)
  3677. {
  3678. CSASSERT(
  3679. chTEXTPREFIX != *pszTableName ||
  3680. ISTEXTCOLTYP(columndef.coltyp));
  3681. DBGPRINT((
  3682. DBG_SS_CERTDB,
  3683. "Found Old Column: %hs.%hs: %x\n",
  3684. pszTableName,
  3685. pszFieldName,
  3686. columndef.columnid));
  3687. pdt->dwFlags |= DBTF_OLDCOLUMNID;
  3688. pdt->dbcolumnidOld = columndef.columnid;
  3689. m_fFoundOldColumns = TRUE;
  3690. }
  3691. hr = S_OK;
  3692. }
  3693. }
  3694. error:
  3695. if (fOpen)
  3696. {
  3697. HRESULT hr2;
  3698. hr2 = _dbgJetCloseTable(pcs->SesId, tableid);
  3699. _PrintIfError(hr2, "JetCloseTable");
  3700. if (S_OK == hr)
  3701. {
  3702. hr = hr2;
  3703. }
  3704. }
  3705. return(myJetHResult(hr));
  3706. }
  3707. HRESULT
  3708. CCertDB::_AddKeyLengthColumn(
  3709. IN CERTSESSION *pcs,
  3710. IN JET_TABLEID tableid,
  3711. IN DWORD RowId,
  3712. IN DBTABLE const *pdtPublicKey,
  3713. IN DBTABLE const *pdtPublicKeyAlgorithm,
  3714. IN DBTABLE const *pdtPublicKeyParameters,
  3715. IN DBTABLE const *pdtPublicKeyLength,
  3716. IN DBAUXDATA const *pdbaux,
  3717. IN OUT BYTE **ppbBuf,
  3718. IN OUT DWORD *pcbBuf)
  3719. {
  3720. HRESULT hr;
  3721. DWORD cb;
  3722. DWORD KeyLength;
  3723. CERT_PUBLIC_KEY_INFO PublicKeyInfo;
  3724. ZeroMemory(&PublicKeyInfo, sizeof(PublicKeyInfo));
  3725. DBGPRINT((
  3726. DBG_SS_CERTDBI,
  3727. "Converting %hs[%d].%hs\n",
  3728. pdbaux->pszTable,
  3729. RowId,
  3730. pdtPublicKeyLength->pszFieldName));
  3731. cb = sizeof(KeyLength);
  3732. hr = _RetrieveColumn(
  3733. pcs,
  3734. tableid,
  3735. pdtPublicKeyLength->dbcolumnid,
  3736. pdtPublicKeyLength->dbcoltyp,
  3737. &cb,
  3738. (BYTE *) &KeyLength);
  3739. if (S_OK == hr)
  3740. {
  3741. goto error; // already set -- skip this column
  3742. }
  3743. if (CERTSRV_E_PROPERTY_EMPTY != hr)
  3744. {
  3745. _JumpError(hr, error, "_RetrieveColumn");
  3746. }
  3747. // Fetch the public key algorithm ObjId & copy as ansi to alloc'd memory.
  3748. hr = _RetrieveColumnBuffer(
  3749. pcs,
  3750. tableid,
  3751. pdtPublicKeyAlgorithm->dbcolumnid,
  3752. pdtPublicKeyAlgorithm->dbcoltyp,
  3753. &cb,
  3754. ppbBuf,
  3755. pcbBuf);
  3756. if (CERTSRV_E_PROPERTY_EMPTY == hr)
  3757. {
  3758. hr = S_OK;
  3759. goto error; // No old data -- skip this column
  3760. }
  3761. _JumpIfErrorStr(
  3762. hr,
  3763. error,
  3764. "_RetrieveColumnBuffer",
  3765. pdtPublicKeyAlgorithm->pwszPropName);
  3766. if (!ConvertWszToSz(
  3767. &PublicKeyInfo.Algorithm.pszObjId,
  3768. (WCHAR const *) *ppbBuf,
  3769. -1))
  3770. {
  3771. hr = E_OUTOFMEMORY;
  3772. _JumpError(hr, error, "ConvertWszToSz(LogDir)");
  3773. }
  3774. // Fetch the public key algorithm paramaters, and copy to alloc'd memory.
  3775. hr = _RetrieveColumnBuffer(
  3776. pcs,
  3777. tableid,
  3778. pdtPublicKeyParameters->dbcolumnid,
  3779. pdtPublicKeyParameters->dbcoltyp,
  3780. &PublicKeyInfo.Algorithm.Parameters.cbData,
  3781. ppbBuf,
  3782. pcbBuf);
  3783. if (CERTSRV_E_PROPERTY_EMPTY == hr)
  3784. {
  3785. hr = S_OK;
  3786. goto error; // No old data -- skip this column
  3787. }
  3788. _JumpIfErrorStr(
  3789. hr,
  3790. error,
  3791. "_RetrieveColumnBuffer",
  3792. pdtPublicKeyParameters->pwszPropName);
  3793. PublicKeyInfo.Algorithm.Parameters.pbData = (BYTE *) LocalAlloc(
  3794. LMEM_FIXED,
  3795. PublicKeyInfo.Algorithm.Parameters.cbData);
  3796. if (NULL == PublicKeyInfo.Algorithm.Parameters.pbData)
  3797. {
  3798. hr = E_OUTOFMEMORY;
  3799. _JumpError(hr, error, "ConvertWszToSz(LogDir)");
  3800. }
  3801. CopyMemory(
  3802. PublicKeyInfo.Algorithm.Parameters.pbData,
  3803. *ppbBuf,
  3804. PublicKeyInfo.Algorithm.Parameters.cbData);
  3805. // Fetch the public key, and leave in dynamic buffer.
  3806. hr = _RetrieveColumnBuffer(
  3807. pcs,
  3808. tableid,
  3809. pdtPublicKey->dbcolumnid,
  3810. pdtPublicKey->dbcoltyp,
  3811. &PublicKeyInfo.PublicKey.cbData,
  3812. ppbBuf,
  3813. pcbBuf);
  3814. if (CERTSRV_E_PROPERTY_EMPTY == hr)
  3815. {
  3816. hr = S_OK;
  3817. goto error; // No old data -- skip this column
  3818. }
  3819. _JumpIfErrorStr(
  3820. hr,
  3821. error,
  3822. "_RetrieveColumnBuffer",
  3823. pdtPublicKey->pwszPropName);
  3824. PublicKeyInfo.PublicKey.pbData = *ppbBuf;
  3825. KeyLength = CertGetPublicKeyLength(X509_ASN_ENCODING, &PublicKeyInfo);
  3826. // Store the key length in the new column
  3827. hr = _SetColumn(
  3828. pcs->SesId,
  3829. tableid,
  3830. pdtPublicKeyLength->dbcolumnid,
  3831. sizeof(KeyLength),
  3832. (BYTE const *) &KeyLength);
  3833. _JumpIfErrorStr(hr, error, "_SetColumn", pdtPublicKeyLength->pwszPropName);
  3834. DBGPRINT((
  3835. DBG_SS_CERTDB,
  3836. "Converted %hs[%d].%hs: %u\n",
  3837. pdbaux->pszTable,
  3838. RowId,
  3839. pdtPublicKeyLength->pszFieldName,
  3840. KeyLength));
  3841. error:
  3842. if (NULL != PublicKeyInfo.Algorithm.pszObjId)
  3843. {
  3844. LocalFree(PublicKeyInfo.Algorithm.pszObjId);
  3845. }
  3846. if (NULL != PublicKeyInfo.Algorithm.Parameters.pbData)
  3847. {
  3848. LocalFree(PublicKeyInfo.Algorithm.Parameters.pbData);
  3849. }
  3850. return(hr);
  3851. }
  3852. HRESULT
  3853. CCertDB::_AddCallerName(
  3854. IN CERTSESSION *pcs,
  3855. IN JET_TABLEID tableid,
  3856. IN DWORD RowId,
  3857. IN DBTABLE const *pdtCallerName,
  3858. IN DBTABLE const *pdtRequesterName,
  3859. IN DBAUXDATA const *pdbaux,
  3860. IN OUT BYTE **ppbBuf,
  3861. IN OUT DWORD *pcbBuf)
  3862. {
  3863. HRESULT hr;
  3864. DWORD cb;
  3865. DBGPRINT((
  3866. DBG_SS_CERTDBI,
  3867. "Converting %hs[%d].%hs\n",
  3868. pdbaux->pszTable,
  3869. RowId,
  3870. pdtCallerName->pszFieldName));
  3871. cb = 0;
  3872. hr = _RetrieveColumn(
  3873. pcs,
  3874. tableid,
  3875. pdtCallerName->dbcolumnid,
  3876. pdtCallerName->dbcoltyp,
  3877. &cb,
  3878. NULL);
  3879. if (S_OK == hr)
  3880. {
  3881. goto error; // already set -- skip this column
  3882. }
  3883. if (CERTSRV_E_PROPERTY_EMPTY != hr)
  3884. {
  3885. _JumpError(hr, error, "_RetrieveColumn");
  3886. }
  3887. // Fetch the ReqesterName
  3888. hr = _RetrieveColumnBuffer(
  3889. pcs,
  3890. tableid,
  3891. pdtRequesterName->dbcolumnid,
  3892. pdtRequesterName->dbcoltyp,
  3893. &cb,
  3894. ppbBuf,
  3895. pcbBuf);
  3896. if (CERTSRV_E_PROPERTY_EMPTY == hr)
  3897. {
  3898. hr = S_OK;
  3899. goto error; // No old data -- skip this column
  3900. }
  3901. _JumpIfErrorStr(
  3902. hr,
  3903. error,
  3904. "_RetrieveColumnBuffer",
  3905. pdtRequesterName->pwszPropName);
  3906. // Store the RequesterName as the CallerName in the new column
  3907. hr = _SetColumn(
  3908. pcs->SesId,
  3909. tableid,
  3910. pdtCallerName->dbcolumnid,
  3911. cb,
  3912. *ppbBuf);
  3913. _JumpIfErrorStr(hr, error, "_SetColumn", pdtCallerName->pwszPropName);
  3914. DBGPRINT((
  3915. DBG_SS_CERTDB,
  3916. "Converted %hs[%d].%hs: %ws\n",
  3917. pdbaux->pszTable,
  3918. RowId,
  3919. pdtCallerName->pszFieldName,
  3920. *ppbBuf));
  3921. error:
  3922. return(hr);
  3923. }
  3924. HRESULT
  3925. CCertDB::_ConvertColumnData(
  3926. IN CERTSESSION *pcs,
  3927. IN JET_TABLEID tableid,
  3928. IN DWORD RowId,
  3929. IN DBTABLE const *pdt,
  3930. IN DBAUXDATA const *pdbaux,
  3931. IN OUT BYTE **ppbBuf,
  3932. IN OUT DWORD *pcbBuf)
  3933. {
  3934. HRESULT hr;
  3935. WCHAR *pwszNew = NULL;
  3936. BYTE const *pbNew;
  3937. DWORD cb;
  3938. DBGPRINT((
  3939. DBG_SS_CERTDBI,
  3940. "Converting %hs[%d].%hs:\n",
  3941. pdbaux->pszTable,
  3942. RowId,
  3943. pdt->pszFieldName));
  3944. // Fetch old column. Grows the buffer if necessary.
  3945. hr = _RetrieveColumnBuffer(
  3946. pcs,
  3947. tableid,
  3948. pdt->dbcolumnidOld,
  3949. pdt->dbcoltyp,
  3950. &cb,
  3951. ppbBuf,
  3952. pcbBuf);
  3953. if (CERTSRV_E_PROPERTY_EMPTY == hr)
  3954. {
  3955. hr = S_OK;
  3956. goto error; // No old data -- skip this column
  3957. }
  3958. _JumpIfErrorStr(hr, error, "_RetrieveColumnBuffer", pdt->pwszPropName);
  3959. if (DBTF_COLUMNRENAMED & pdt->dwFlags)
  3960. {
  3961. pbNew = *ppbBuf;
  3962. }
  3963. else
  3964. {
  3965. if (!ConvertSzToWsz(&pwszNew, (char *) *ppbBuf, -1))
  3966. {
  3967. hr = E_OUTOFMEMORY;
  3968. _JumpError(hr, error, "ConvertSzToWsz");
  3969. }
  3970. pbNew = (BYTE const *) pwszNew;
  3971. cb = wcslen(pwszNew) * sizeof(WCHAR);
  3972. }
  3973. // Store the converted string in the Unicode column
  3974. hr = _SetColumn(pcs->SesId, tableid, pdt->dbcolumnid, cb, pbNew);
  3975. _JumpIfErrorStr(hr, error, "_SetColumn", pdt->pwszPropName);
  3976. if (JET_coltypLong != pdt->dbcoltyp)
  3977. {
  3978. // Clear out the old column
  3979. hr = _SetColumn(pcs->SesId, tableid, pdt->dbcolumnidOld, 0, NULL);
  3980. _JumpIfErrorStr(hr, error, "_SetColumn(Clear old)", pdt->pwszPropName);
  3981. }
  3982. DBGPRINT((
  3983. DBG_SS_CERTDB,
  3984. "Converted %hs[%d].%hs: %ws\n",
  3985. pdbaux->pszTable,
  3986. RowId,
  3987. pdt->pszFieldName,
  3988. ISTEXTCOLTYP(pdt->dbcoltyp)? (WCHAR const *) pbNew : L""));
  3989. if (!ISTEXTCOLTYP(pdt->dbcoltyp))
  3990. {
  3991. DBGDUMPHEX((DBG_SS_CERTDB, DH_NOADDRESS, pbNew, cb));
  3992. }
  3993. error:
  3994. if (NULL != pwszNew)
  3995. {
  3996. LocalFree(pwszNew);
  3997. }
  3998. return(hr);
  3999. }
  4000. DBTABLE *
  4001. dbFindColumn(
  4002. IN DBTABLE *adt,
  4003. IN char const *pszFieldName)
  4004. {
  4005. DBTABLE *pdt;
  4006. DBTABLE *pdtRet = NULL;
  4007. for (pdt = adt; NULL != pdt->pwszPropName; pdt++)
  4008. {
  4009. if (0 == _stricmp(pszFieldName, pdt->pszFieldName))
  4010. {
  4011. pdtRet = pdt;
  4012. break;
  4013. }
  4014. }
  4015. return(pdtRet);
  4016. }
  4017. HRESULT
  4018. CCertDB::_ConvertOldColumnData(
  4019. IN CERTSESSION *pcs,
  4020. IN CHAR const *pszTableName,
  4021. IN DBAUXDATA const *pdbaux,
  4022. IN DBTABLE *adt)
  4023. {
  4024. HRESULT hr;
  4025. HRESULT hr2;
  4026. JET_TABLEID tableid;
  4027. JET_COLUMNDEF columndef;
  4028. BOOL fOpen = FALSE;
  4029. BOOL fTransacted = FALSE;
  4030. DBTABLE *pdt;
  4031. DWORD RowId;
  4032. DWORD cb;
  4033. BYTE *pbBuf = NULL;
  4034. DWORD cbBuf = 0;
  4035. BOOL fZeroIssuerNameId = FALSE;
  4036. DWORD IssuerNameId;
  4037. DBTABLE *pdtPublicKeyLength = NULL;
  4038. DBTABLE *pdtPublicKey;
  4039. DBTABLE *pdtPublicKeyAlgorithm;
  4040. DBTABLE *pdtPublicKeyParameters;
  4041. DBTABLE *pdtCallerName = NULL;
  4042. DBTABLE *pdtRequesterName;
  4043. hr = _dbgJetOpenTable(
  4044. pcs->SesId,
  4045. pcs->DBId,
  4046. pszTableName,
  4047. NULL,
  4048. 0,
  4049. 0,
  4050. &tableid);
  4051. _JumpIfError(hr, error, "JetOpenTable");
  4052. fOpen = TRUE;
  4053. // Step through the RowId index for this table.
  4054. CSASSERT(IsValidJetTableId(tableid));
  4055. hr = _dbgJetSetCurrentIndex2(
  4056. pcs->SesId,
  4057. tableid,
  4058. pdbaux->pszRowIdIndex,
  4059. JET_bitMoveFirst);
  4060. _JumpIfError(hr, error, "JetSetCurrentIndex2");
  4061. if (NULL != pdbaux->pdtIssuerNameId)
  4062. {
  4063. cb = sizeof(IssuerNameId);
  4064. hr = _RetrieveColumn(
  4065. pcs,
  4066. tableid,
  4067. pdbaux->pdtIssuerNameId->dbcolumnid,
  4068. pdbaux->pdtIssuerNameId->dbcoltyp,
  4069. &cb,
  4070. (BYTE *) &IssuerNameId);
  4071. if (CERTSRV_E_PROPERTY_EMPTY == hr)
  4072. {
  4073. fZeroIssuerNameId = TRUE;
  4074. }
  4075. else
  4076. {
  4077. // swallow error if empty database
  4078. _PrintIfErrorStr2(
  4079. hr,
  4080. "_RetrieveColumn",
  4081. pdbaux->pdtIssuerNameId->pwszPropName,
  4082. myJetHResult(JET_errNoCurrentRecord));
  4083. }
  4084. }
  4085. pdtPublicKeyLength = dbFindColumn(adt, szPUBLICKEYLENGTH);
  4086. if (NULL != pdtPublicKeyLength)
  4087. {
  4088. pdtPublicKey = dbFindColumn(adt, szPUBLICKEY);
  4089. pdtPublicKeyAlgorithm = dbFindColumn(adt, szPUBLICKEYALGORITHM);
  4090. pdtPublicKeyParameters = dbFindColumn(adt, szPUBLICKEYPARAMS);
  4091. if (NULL == pdtPublicKey ||
  4092. NULL == pdtPublicKeyAlgorithm ||
  4093. NULL == pdtPublicKeyParameters)
  4094. {
  4095. pdtPublicKeyLength = NULL;
  4096. }
  4097. else
  4098. {
  4099. hr = _RetrieveColumn(
  4100. pcs,
  4101. tableid,
  4102. pdtPublicKeyLength->dbcolumnid,
  4103. pdtPublicKeyLength->dbcoltyp,
  4104. &cb,
  4105. NULL);
  4106. if (CERTSRV_E_PROPERTY_EMPTY != hr)
  4107. {
  4108. pdtPublicKeyLength = NULL;
  4109. }
  4110. }
  4111. }
  4112. pdtCallerName = dbFindColumn(adt, szCALLERNAME);
  4113. if (NULL != pdtCallerName)
  4114. {
  4115. pdtRequesterName = dbFindColumn(adt, szREQUESTERNAME);
  4116. if (NULL == pdtRequesterName)
  4117. {
  4118. pdtCallerName = NULL;
  4119. }
  4120. else
  4121. {
  4122. // Update all rows only if the first row's CallerName is empty
  4123. // and the first row's RequesterName is NOT empty.
  4124. hr = _RetrieveColumn(
  4125. pcs,
  4126. tableid,
  4127. pdtCallerName->dbcolumnid,
  4128. pdtCallerName->dbcoltyp,
  4129. &cb,
  4130. NULL);
  4131. if (CERTSRV_E_PROPERTY_EMPTY != hr)
  4132. {
  4133. pdtCallerName = NULL;
  4134. }
  4135. else
  4136. {
  4137. hr = _RetrieveColumn(
  4138. pcs,
  4139. tableid,
  4140. pdtRequesterName->dbcolumnid,
  4141. pdtRequesterName->dbcoltyp,
  4142. &cb,
  4143. NULL);
  4144. if (S_OK != hr)
  4145. {
  4146. pdtCallerName = NULL;
  4147. }
  4148. }
  4149. }
  4150. }
  4151. if (NULL != pdtPublicKeyLength ||
  4152. NULL != pdtCallerName ||
  4153. m_fFoundOldColumns ||
  4154. fZeroIssuerNameId)
  4155. {
  4156. DBGPRINT((DBG_SS_CERTDB, "Updating %hs table.\n", pdbaux->pszTable));
  4157. while (TRUE)
  4158. {
  4159. // Fetch RowId from the table.
  4160. cb = sizeof(pcs->RowId);
  4161. hr = _RetrieveColumn(
  4162. pcs,
  4163. tableid,
  4164. pdbaux->pdtRowId->dbcolumnid,
  4165. pdbaux->pdtRowId->dbcoltyp,
  4166. &cb,
  4167. (BYTE *) &RowId);
  4168. if (S_OK != hr)
  4169. {
  4170. if (myJetHResult(JET_errNoCurrentRecord) == hr)
  4171. {
  4172. hr = S_OK; // Table is empty
  4173. break;
  4174. }
  4175. _JumpError(hr, error, "_RetrieveColumn");
  4176. }
  4177. hr = _dbgJetBeginTransaction(pcs->SesId);
  4178. _JumpIfError(hr, error, "JetBeginTransaction");
  4179. fTransacted = TRUE;
  4180. // Transact each row.
  4181. //
  4182. // If fZeroIssuerNameId, set EMPTY IssuerNameId columns to zero.
  4183. //
  4184. // if the first row's public key length column was empty
  4185. // Read the public key column, compute the size and store it
  4186. //
  4187. // if the first row's CallerName was empty
  4188. // Copy RequesterName to CallerName
  4189. //
  4190. // if m_fFoundOldColumns
  4191. // For each text column, do the following:
  4192. // Retrieve old string from the old column,
  4193. // Convert to Unicode (if old column was Ansi),
  4194. // Write the Unicode string to the new column,
  4195. // Set the old column to NULL.
  4196. hr = _dbgJetPrepareUpdate(
  4197. pcs->SesId,
  4198. tableid,
  4199. JET_prepReplace);
  4200. _JumpIfError(hr, error, "JetPrepareUpdate");
  4201. if (fZeroIssuerNameId)
  4202. {
  4203. cb = sizeof(IssuerNameId);
  4204. hr = _RetrieveColumn(
  4205. pcs,
  4206. tableid,
  4207. pdbaux->pdtIssuerNameId->dbcolumnid,
  4208. pdbaux->pdtIssuerNameId->dbcoltyp,
  4209. &cb,
  4210. (BYTE *) &IssuerNameId);
  4211. if (CERTSRV_E_PROPERTY_EMPTY != hr)
  4212. {
  4213. _JumpIfError(hr, error, "_RetrieveColumn");
  4214. }
  4215. else
  4216. {
  4217. // Only set EMPTY columns!
  4218. IssuerNameId = 0;
  4219. hr = _SetColumn(
  4220. pcs->SesId,
  4221. tableid,
  4222. pdbaux->pdtIssuerNameId->dbcolumnid,
  4223. sizeof(IssuerNameId),
  4224. (BYTE const *) &IssuerNameId);
  4225. _JumpIfError(hr, error, "_SetColumn");
  4226. }
  4227. }
  4228. // Convert old columns first.
  4229. if (m_fFoundOldColumns)
  4230. {
  4231. for (pdt = adt; NULL != pdt->pwszPropName; pdt++)
  4232. {
  4233. if (DBTF_OLDCOLUMNID & pdt->dwFlags)
  4234. {
  4235. hr = _ConvertColumnData(
  4236. pcs,
  4237. tableid,
  4238. RowId,
  4239. pdt,
  4240. pdbaux,
  4241. &pbBuf,
  4242. &cbBuf);
  4243. _JumpIfErrorStr(
  4244. hr,
  4245. error,
  4246. "_ConvertColumnData",
  4247. pdt->pwszPropName);
  4248. }
  4249. }
  4250. }
  4251. // Now compute new columns.
  4252. if (NULL != pdtPublicKeyLength)
  4253. {
  4254. hr = _AddKeyLengthColumn(
  4255. pcs,
  4256. tableid,
  4257. RowId,
  4258. pdtPublicKey,
  4259. pdtPublicKeyAlgorithm,
  4260. pdtPublicKeyParameters,
  4261. pdtPublicKeyLength,
  4262. pdbaux,
  4263. &pbBuf,
  4264. &cbBuf);
  4265. _JumpIfError(hr, error, "_AddKeyLengthColumn");
  4266. }
  4267. if (NULL != pdtCallerName)
  4268. {
  4269. hr = _AddCallerName(
  4270. pcs,
  4271. tableid,
  4272. RowId,
  4273. pdtCallerName,
  4274. pdtRequesterName,
  4275. pdbaux,
  4276. &pbBuf,
  4277. &cbBuf);
  4278. _JumpIfError(hr, error, "_AddCallerName");
  4279. }
  4280. // Done with this row.
  4281. hr = _dbgJetUpdate(pcs->SesId, tableid, NULL, 0, NULL);
  4282. _JumpIfError(hr, error, "JetUpdate");
  4283. hr = _dbgJetCommitTransaction(pcs->SesId, 0);
  4284. _JumpIfError(hr, error, "JetCommitTransaction");
  4285. fTransacted = FALSE;
  4286. hr = _dbgJetMove(pcs->SesId, tableid, JET_MoveNext, 0);
  4287. if ((HRESULT) JET_errNoCurrentRecord == hr)
  4288. {
  4289. hr = S_OK;
  4290. break;
  4291. }
  4292. }
  4293. }
  4294. if (m_fFoundOldColumns)
  4295. {
  4296. hr = _dbgJetBeginTransaction(pcs->SesId);
  4297. _JumpIfError(hr, error, "JetBeginTransaction");
  4298. fTransacted = TRUE;
  4299. for (pdt = adt; NULL != pdt->pwszPropName; pdt++)
  4300. {
  4301. char const *pszFieldName;
  4302. if (0 == (DBTF_OLDCOLUMNID & pdt->dwFlags))
  4303. {
  4304. continue;
  4305. }
  4306. pszFieldName = &pdt->pszFieldName[1];
  4307. if (DBTF_COLUMNRENAMED & pdt->dwFlags)
  4308. {
  4309. pszFieldName += strlen(pszFieldName) + 1;
  4310. }
  4311. DBGPRINT((
  4312. DBG_SS_CERTDB,
  4313. "Deleting column %hs.%hs\n",
  4314. pdbaux->pszTable,
  4315. pszFieldName));
  4316. hr = _dbgJetDeleteColumn(pcs->SesId, tableid, pszFieldName);
  4317. _PrintIfError(hr, "JetDeleteColumn");
  4318. if (JET_errColumnInUse == hr)
  4319. {
  4320. hr = S_OK; // we'll delete the column next time we restart
  4321. }
  4322. _JumpIfError(hr, error, "JetDeleteColumn");
  4323. }
  4324. hr = _dbgJetCommitTransaction(pcs->SesId, 0);
  4325. _JumpIfError(hr, error, "JetCommitTransaction");
  4326. fTransacted = FALSE;
  4327. }
  4328. hr = S_OK;
  4329. error:
  4330. if (NULL != pbBuf)
  4331. {
  4332. LocalFree(pbBuf);
  4333. }
  4334. if (fTransacted)
  4335. {
  4336. hr2 = _Rollback(pcs);
  4337. _PrintIfError(hr2, "_Rollback");
  4338. if (S_OK == hr)
  4339. {
  4340. hr = hr2;
  4341. }
  4342. }
  4343. if (fOpen)
  4344. {
  4345. hr2 = _dbgJetCloseTable(pcs->SesId, tableid);
  4346. _PrintIfError(hr2, "JetCloseTable");
  4347. if (S_OK == hr)
  4348. {
  4349. hr = hr2;
  4350. }
  4351. }
  4352. return(myJetHResult(hr));
  4353. }
  4354. HRESULT
  4355. CCertDB::_SetColumn(
  4356. IN JET_SESID SesId,
  4357. IN JET_TABLEID tableid,
  4358. IN JET_COLUMNID columnid,
  4359. IN DWORD cbProp,
  4360. OPTIONAL IN BYTE const *pbProp)
  4361. {
  4362. HRESULT hr;
  4363. CSASSERT(IsValidJetTableId(tableid));
  4364. hr = _dbgJetSetColumn(SesId, tableid, columnid, pbProp, cbProp, 0, NULL);
  4365. if ((HRESULT) JET_wrnColumnMaxTruncated == hr)
  4366. {
  4367. hr = HRESULT_FROM_WIN32(ERROR_BUFFER_OVERFLOW);
  4368. }
  4369. _JumpIfError(hr, error, "JetSetColumn");
  4370. error:
  4371. return(myJetHResult(hr));
  4372. }
  4373. HRESULT
  4374. CCertDB::SetAttribute(
  4375. IN CERTSESSION *pcs,
  4376. IN WCHAR const *pwszAttributeName,
  4377. IN DWORD cbValue,
  4378. IN BYTE const *pbValue) // OPTIONAL
  4379. {
  4380. return(_SetIndirect(
  4381. pcs,
  4382. &pcs->aTable[CSTI_ATTRIBUTE],
  4383. pwszAttributeName,
  4384. NULL,
  4385. cbValue,
  4386. pbValue));
  4387. }
  4388. HRESULT
  4389. CCertDB::GetAttribute(
  4390. IN CERTSESSION *pcs,
  4391. IN WCHAR const *pwszAttributeName,
  4392. IN OUT DWORD *pcbValue,
  4393. OUT BYTE *pbValue) // OPTIONAL
  4394. {
  4395. return(_GetIndirect(
  4396. pcs,
  4397. &pcs->aTable[CSTI_ATTRIBUTE],
  4398. pwszAttributeName,
  4399. NULL,
  4400. pcbValue,
  4401. pbValue));
  4402. }
  4403. HRESULT
  4404. CCertDB::SetExtension(
  4405. IN CERTSESSION *pcs,
  4406. IN WCHAR const *pwszExtensionName,
  4407. IN DWORD dwExtFlags,
  4408. IN DWORD cbValue,
  4409. IN BYTE const *pbValue) // OPTIONAL
  4410. {
  4411. return(_SetIndirect(
  4412. pcs,
  4413. &pcs->aTable[CSTI_EXTENSION],
  4414. pwszExtensionName,
  4415. &dwExtFlags,
  4416. cbValue,
  4417. pbValue));
  4418. }
  4419. HRESULT
  4420. CCertDB::GetExtension(
  4421. IN CERTSESSION *pcs,
  4422. IN WCHAR const *pwszExtensionName,
  4423. OUT DWORD *pdwExtFlags,
  4424. IN OUT DWORD *pcbValue,
  4425. OUT BYTE *pbValue) // OPTIONAL
  4426. {
  4427. return(_GetIndirect(
  4428. pcs,
  4429. &pcs->aTable[CSTI_EXTENSION],
  4430. pwszExtensionName,
  4431. pdwExtFlags,
  4432. pcbValue,
  4433. pbValue));
  4434. }
  4435. HRESULT
  4436. CCertDB::_JetSeekFromRestriction(
  4437. IN CERTVIEWRESTRICTION const *pcvr,
  4438. IN DWORD dwPosition,
  4439. OUT DBSEEKDATA *pSeekData)
  4440. {
  4441. HRESULT hr;
  4442. BOOL fAscend;
  4443. DBSEEKDATA SeekFirst; // seek to first element matching restriction
  4444. DBSEEKDATA SeekLast; // seek to last element matching restriction
  4445. DBSEEKDATA SeekIndexFirst; // seek to first index element
  4446. DBSEEKDATA SeekIndexLast; // seek to last index element
  4447. DBSEEKDATA *pSeek;
  4448. BOOL fValid;
  4449. // SeekLast.SeekFlags: where to seek to retrieve end-of-range key
  4450. // SeekLast.grbitSeekRange: where to set the cursor initially: move or seek
  4451. // SeekLast.grbitRange: other flags to be ingested while setting range:
  4452. // (bitRange UpperLimit, Inclusive)
  4453. #if DBG_CERTSRV
  4454. DumpRestriction(DBG_SS_CERTDBI, 0, pcvr);
  4455. #endif
  4456. fAscend = (CVR_SORT_DESCEND != pcvr->SortOrder);
  4457. CSASSERT(
  4458. CVR_SORT_NONE == pcvr->SortOrder ||
  4459. CVR_SORT_ASCEND == pcvr->SortOrder ||
  4460. CVR_SORT_DESCEND == pcvr->SortOrder);
  4461. ZeroMemory(&SeekFirst, sizeof(SeekFirst));
  4462. ZeroMemory(&SeekLast, sizeof(SeekLast));
  4463. ZeroMemory(&SeekIndexFirst, sizeof(SeekIndexFirst));
  4464. ZeroMemory(&SeekIndexLast, sizeof(SeekIndexLast));
  4465. switch (CVR_SEEK_MASK & pcvr->SeekOperator)
  4466. {
  4467. case CVR_SEEK_EQ:
  4468. if (fAscend)
  4469. {
  4470. SeekFirst.SeekFlags = CST_SEEKUSECURRENT |
  4471. CST_SEEKNOTMOVE |
  4472. CST_SEEKINDEXRANGE;
  4473. SeekFirst.grbitSeekRange = JET_bitSeekEQ;
  4474. SeekFirst.grbitInitial = JET_bitSeekEQ;
  4475. SeekFirst.grbitRange = JET_bitRangeUpperLimit |
  4476. JET_bitRangeInclusive;
  4477. SeekLast.SeekFlags = CST_SEEKINDEXRANGE;
  4478. SeekLast.grbitSeekRange = JET_bitSeekGT;
  4479. SeekLast.grbitInitial = JET_MovePrevious;
  4480. SeekLast.grbitRange = JET_bitRangeUpperLimit;
  4481. }
  4482. else
  4483. {
  4484. SeekFirst.SeekFlags = CST_SEEKNOTMOVE | CST_SEEKINDEXRANGE;
  4485. SeekFirst.grbitSeekRange = JET_bitSeekEQ;
  4486. SeekFirst.grbitInitial = JET_bitSeekGT;
  4487. SeekFirst.grbitRange = JET_bitRangeInclusive;
  4488. SeekLast.SeekFlags = CST_SEEKINDEXRANGE;
  4489. SeekLast.grbitSeekRange = JET_bitSeekEQ;
  4490. SeekLast.grbitInitial = JET_MovePrevious;
  4491. SeekLast.grbitRange = JET_bitRangeInclusive;
  4492. }
  4493. break;
  4494. case CVR_SEEK_LT:
  4495. if (fAscend)
  4496. {
  4497. SeekFirst.SeekFlags = CST_SEEKUSECURRENT | CST_SEEKINDEXRANGE;
  4498. SeekFirst.grbitSeekRange = JET_bitSeekGE;
  4499. SeekFirst.grbitInitial = JET_MoveFirst;
  4500. SeekFirst.grbitRange = JET_bitRangeUpperLimit;
  4501. SeekLast.SeekFlags = CST_SEEKINDEXRANGE;
  4502. SeekLast.grbitSeekRange = JET_bitSeekGE;
  4503. SeekLast.grbitInitial = JET_MovePrevious;
  4504. SeekLast.grbitRange = JET_bitRangeUpperLimit;
  4505. }
  4506. else
  4507. {
  4508. SeekFirst.SeekFlags = CST_SEEKNOTMOVE;
  4509. SeekFirst.grbitInitial = JET_bitSeekGE;
  4510. //SeekLast.SeekFlags = 0; // not CST_SEEKUSECURRENT
  4511. SeekLast.grbitInitial = JET_MoveFirst;
  4512. SeekIndexFirst.SeekFlags = CST_SEEKUSECURRENT;
  4513. SeekIndexFirst.grbitInitial = JET_MoveLast;
  4514. }
  4515. break;
  4516. case CVR_SEEK_LE:
  4517. if (fAscend)
  4518. {
  4519. SeekFirst.SeekFlags = CST_SEEKUSECURRENT | CST_SEEKINDEXRANGE;
  4520. SeekFirst.grbitSeekRange = JET_bitSeekGT;
  4521. SeekFirst.grbitInitial = JET_MoveFirst;
  4522. SeekFirst.grbitRange = JET_bitRangeUpperLimit;
  4523. SeekLast.SeekFlags = CST_SEEKINDEXRANGE; // !CST_SEEKUSECURRENT
  4524. SeekLast.grbitSeekRange = JET_bitSeekGT;
  4525. SeekLast.grbitInitial = JET_MovePrevious;
  4526. SeekLast.grbitRange = JET_bitRangeUpperLimit;
  4527. }
  4528. else
  4529. {
  4530. SeekFirst.SeekFlags = CST_SEEKNOTMOVE;
  4531. SeekFirst.grbitInitial = JET_bitSeekGT;
  4532. //SeekLast.SeekFlags = 0; // not CST_SEEKUSECURRENT
  4533. SeekLast.grbitInitial = JET_MoveFirst;
  4534. SeekIndexFirst.SeekFlags = CST_SEEKUSECURRENT;
  4535. SeekIndexFirst.grbitInitial = JET_MoveLast;
  4536. }
  4537. break;
  4538. case CVR_SEEK_GE:
  4539. if (fAscend)
  4540. {
  4541. SeekFirst.SeekFlags = CST_SEEKUSECURRENT | CST_SEEKNOTMOVE;
  4542. SeekFirst.grbitInitial = JET_bitSeekGE;
  4543. //SeekLast.SeekFlags = 0; // not CST_SEEKUSECURRENT
  4544. SeekLast.grbitInitial = JET_MoveLast;
  4545. }
  4546. else
  4547. {
  4548. SeekFirst.SeekFlags = CST_SEEKUSECURRENT | CST_SEEKINDEXRANGE;
  4549. SeekFirst.grbitSeekRange = JET_bitSeekLT;
  4550. SeekFirst.grbitInitial = JET_MoveLast;
  4551. // Implied: SeekFirst.grbitRange = JET_bitRangeLowerLimit;
  4552. SeekLast.SeekFlags = CST_SEEKNOTMOVE;
  4553. SeekLast.grbitInitial = JET_bitSeekLT;
  4554. SeekIndexLast.SeekFlags = CST_SEEKUSECURRENT;
  4555. SeekIndexLast.grbitInitial = JET_MoveFirst;
  4556. }
  4557. break;
  4558. case CVR_SEEK_GT:
  4559. if (fAscend)
  4560. {
  4561. SeekFirst.SeekFlags = CST_SEEKUSECURRENT | CST_SEEKNOTMOVE;
  4562. SeekFirst.grbitInitial = JET_bitSeekGT;
  4563. //SeekLast.SeekFlags = 0; // not CST_SEEKUSECURRENT
  4564. SeekLast.grbitInitial = JET_MoveLast;
  4565. }
  4566. else
  4567. {
  4568. SeekFirst.SeekFlags = CST_SEEKUSECURRENT | CST_SEEKINDEXRANGE;
  4569. SeekFirst.grbitSeekRange = JET_bitSeekLE;
  4570. SeekFirst.grbitInitial = JET_MoveLast;
  4571. // Implied: SeekFirst.grbitRange = JET_bitRangeLowerLimit;
  4572. SeekLast.SeekFlags = CST_SEEKNOTMOVE;
  4573. SeekLast.grbitInitial = JET_bitSeekLE;
  4574. SeekIndexLast.SeekFlags = CST_SEEKUSECURRENT;
  4575. SeekIndexLast.grbitInitial = JET_MoveFirst;
  4576. }
  4577. break;
  4578. case CVR_SEEK_NONE:
  4579. if (fAscend)
  4580. {
  4581. SeekFirst.SeekFlags = CST_SEEKUSECURRENT;
  4582. SeekFirst.grbitInitial = JET_MoveFirst;
  4583. //SeekLast.SeekFlags = 0; // not CST_SEEKUSECURRENT
  4584. SeekLast.grbitInitial = JET_MoveLast;
  4585. }
  4586. else
  4587. {
  4588. SeekFirst.SeekFlags = CST_SEEKUSECURRENT;
  4589. SeekFirst.grbitInitial = JET_MoveLast;
  4590. //SeekLast.SeekFlags = 0; // not CST_SEEKUSECURRENT
  4591. SeekLast.grbitInitial = JET_MoveFirst;
  4592. }
  4593. break;
  4594. default:
  4595. CSASSERT(!"bad pcvr->SeekOperator"); // shouldn't get this far
  4596. hr = E_INVALIDARG;
  4597. _JumpError(hr, error, "Seek value");
  4598. }
  4599. fValid = TRUE;
  4600. switch (dwPosition)
  4601. {
  4602. case SEEKPOS_FIRST:
  4603. pSeek = &SeekFirst;
  4604. break;
  4605. case SEEKPOS_LAST:
  4606. pSeek = &SeekLast;
  4607. break;
  4608. case SEEKPOS_INDEXFIRST:
  4609. pSeek = &SeekIndexFirst;
  4610. fValid = 0 != pSeek->SeekFlags;
  4611. break;
  4612. case SEEKPOS_INDEXLAST:
  4613. pSeek = &SeekIndexLast;
  4614. fValid = 0 != pSeek->SeekFlags;
  4615. break;
  4616. default:
  4617. CSASSERT(!"bad dwPosition"); // shouldn't get this far
  4618. hr = E_INVALIDARG;
  4619. _JumpError(hr, error, "dwPosition value");
  4620. }
  4621. if (!fValid)
  4622. {
  4623. // For this SeekOperator, if seeking to the first or last matching
  4624. // restriction failed, there's no point in seeking to the index end.
  4625. hr = CERTSRV_E_PROPERTY_EMPTY;
  4626. _JumpError2(hr, error, "pSeek->SeekFlags", hr);
  4627. }
  4628. *pSeekData = *pSeek; // structure copy
  4629. if (fAscend)
  4630. {
  4631. pSeekData->SeekFlags |= CST_SEEKASCEND;
  4632. }
  4633. hr = S_OK;
  4634. DBGPRINT((
  4635. DBG_SS_CERTDBI,
  4636. "_JetSeekFromRestriction: SeekFlags=%ws, grbitStart=%ws\n",
  4637. wszCSTFlags(pSeekData->SeekFlags),
  4638. (CST_SEEKNOTMOVE & pSeekData->SeekFlags)?
  4639. wszSeekgrbit(pSeekData->grbitInitial) :
  4640. wszMovecrow(pSeekData->grbitInitial)));
  4641. if (CST_SEEKINDEXRANGE & pSeekData->SeekFlags)
  4642. {
  4643. DBGPRINT((
  4644. DBG_SS_CERTDBI,
  4645. "_JetSeekFromRestriction: grbitSeekRange=%ws, grbitRange=%ws\n",
  4646. wszSeekgrbit(pSeekData->grbitSeekRange),
  4647. wszSetIndexRangegrbit(pSeekData->grbitRange)));
  4648. }
  4649. error:
  4650. return(hr);
  4651. }
  4652. HRESULT
  4653. CCertDB::_OpenTable(
  4654. IN CERTSESSION *pcs,
  4655. IN DBAUXDATA const *pdbaux,
  4656. IN CERTVIEWRESTRICTION const *pcvr,
  4657. IN OUT CERTSESSIONTABLE *pTable)
  4658. {
  4659. HRESULT hr;
  4660. DBTABLE const *pdt;
  4661. BOOL fOpened = FALSE;
  4662. hr = _MapPropIdIndex(pcvr->ColumnIndex, &pdt, NULL);
  4663. _JumpIfError(hr, error, "_MapPropIdIndex");
  4664. if (NULL == pdt->pszIndexName)
  4665. {
  4666. hr = E_INVALIDARG;
  4667. _JumpError(hr, error, "Column not indexed");
  4668. }
  4669. if (!IsValidJetTableId(pTable->TableId))
  4670. {
  4671. CSASSERTTHREAD(pcs);
  4672. hr = _dbgJetOpenTable(
  4673. pcs->SesId,
  4674. pcs->DBId,
  4675. pdbaux->pszTable,
  4676. NULL,
  4677. 0,
  4678. 0,
  4679. &pTable->TableId);
  4680. _JumpIfError(hr, error, "JetOpenTable");
  4681. fOpened = TRUE;
  4682. // Find RowId and/or Named column.
  4683. // It's more efficient to pass NULL for primary index name.
  4684. CSASSERTTHREAD(pcs);
  4685. hr = _dbgJetSetCurrentIndex2(
  4686. pcs->SesId,
  4687. pTable->TableId,
  4688. (DBTF_INDEXPRIMARY & pdt->dwFlags)?
  4689. NULL : pdt->pszIndexName,
  4690. JET_bitMoveFirst);
  4691. _JumpIfError(hr, error, "JetSetCurrentIndex2");
  4692. DBGPRINT((
  4693. DBG_SS_CERTDBI,
  4694. "_OpenTable Table=%hs, Index=%hs\n",
  4695. pdbaux->pszTable,
  4696. pdt->pszIndexName));
  4697. }
  4698. hr = _SeekTable(
  4699. pcs,
  4700. pTable->TableId,
  4701. pcvr,
  4702. pdt,
  4703. SEEKPOS_FIRST,
  4704. &pTable->TableFlags
  4705. DBGPARM(pdbaux));
  4706. _JumpIfError2(hr, error, "_SeekTable", S_FALSE);
  4707. error:
  4708. if (S_OK != hr && S_FALSE != hr && fOpened)
  4709. {
  4710. if (IsValidJetTableId(pTable->TableId))
  4711. {
  4712. HRESULT hr2;
  4713. CSASSERTTHREAD(pcs);
  4714. hr2 = _dbgJetCloseTable(pcs->SesId, pTable->TableId);
  4715. _PrintIfError(hr2, "JetCloseTable");
  4716. }
  4717. ZeroMemory(pTable, sizeof(*pTable));
  4718. }
  4719. return(myJetHResult(hr));
  4720. }
  4721. HRESULT
  4722. CCertDB::_SetIndirect(
  4723. IN CERTSESSION *pcs,
  4724. IN OUT CERTSESSIONTABLE *pTable,
  4725. IN WCHAR const *pwszNameValue,
  4726. OPTIONAL IN DWORD const *pdwExtFlags,
  4727. IN DWORD cbValue,
  4728. OPTIONAL IN BYTE const *pbValue)
  4729. {
  4730. HRESULT hr;
  4731. DBAUXDATA const *pdbaux;
  4732. BOOL fExisting = FALSE;
  4733. BOOL fDelete;
  4734. CERTVIEWRESTRICTION cvr;
  4735. if (NULL == pcs || NULL == pwszNameValue)
  4736. {
  4737. hr = E_POINTER;
  4738. _JumpError(hr, error, "NULL parm");
  4739. }
  4740. fDelete = NULL == pbValue;
  4741. if (NULL == pdwExtFlags)
  4742. {
  4743. pdbaux = &g_dbauxAttributes;
  4744. cvr.ColumnIndex = DTI_ATTRIBUTETABLE | DTA_ATTRIBUTENAME;
  4745. }
  4746. else
  4747. {
  4748. if (0 != *pdwExtFlags)
  4749. {
  4750. fDelete = FALSE;
  4751. }
  4752. pdbaux = &g_dbauxExtensions;
  4753. cvr.ColumnIndex = DTI_EXTENSIONTABLE | DTE_EXTENSIONNAME;
  4754. }
  4755. DBGPRINT((
  4756. DBG_SS_CERTDBI,
  4757. "IN: _SetIndirect(%hs.%ws) cb = %x%ws\n",
  4758. pdbaux->pszTable,
  4759. pwszNameValue,
  4760. cbValue,
  4761. fDelete? L" DELETE" : L""));
  4762. cvr.SeekOperator = CVR_SEEK_EQ;
  4763. cvr.SortOrder = CVR_SORT_NONE;
  4764. cvr.cbValue = wcslen(pwszNameValue) * sizeof(WCHAR);
  4765. cvr.pbValue = (BYTE *) pwszNameValue;
  4766. hr = _OpenTable(pcs, pdbaux, &cvr, pTable);
  4767. if (S_FALSE != hr)
  4768. {
  4769. _JumpIfError(hr, error, "_OpenTable");
  4770. fExisting = TRUE;
  4771. }
  4772. _PrintIfError2(hr, "_OpenTable", S_FALSE);
  4773. if (fDelete)
  4774. {
  4775. if (fExisting)
  4776. {
  4777. CSASSERTTHREAD(pcs);
  4778. hr = _dbgJetDelete(pcs->SesId, pTable->TableId);
  4779. _JumpIfError(hr, error, "JetDelete");
  4780. }
  4781. }
  4782. else
  4783. {
  4784. CSASSERTTHREAD(pcs);
  4785. hr = _dbgJetPrepareUpdate(
  4786. pcs->SesId,
  4787. pTable->TableId,
  4788. !fExisting? JET_prepInsert : JET_prepReplace);
  4789. _JumpIfError(hr, error, "JetPrepareUpdate");
  4790. if (!fExisting)
  4791. {
  4792. // No existing row -- insert a new one:
  4793. // Set RowId
  4794. hr = _SetColumn(
  4795. pcs->SesId,
  4796. pTable->TableId,
  4797. pdbaux->pdtRowId->dbcolumnid,
  4798. sizeof(pcs->RowId),
  4799. (BYTE const *) &pcs->RowId);
  4800. _JumpIfError(hr, error, "_SetColumn");
  4801. // Set row's name column
  4802. hr = _SetColumn(
  4803. pcs->SesId,
  4804. pTable->TableId,
  4805. pdbaux->pdtName->dbcolumnid,
  4806. wcslen(pwszNameValue)*sizeof(WCHAR), //cch,
  4807. (BYTE const *) pwszNameValue /*szTmp*/);
  4808. _JumpIfError(hr, error, "_SetColumn");
  4809. }
  4810. if (NULL != pdwExtFlags)
  4811. {
  4812. // Set or update flags
  4813. hr = _SetColumn(
  4814. pcs->SesId,
  4815. pTable->TableId,
  4816. pdbaux->pdtFlags->dbcolumnid,
  4817. sizeof(*pdwExtFlags),
  4818. (BYTE const *) pdwExtFlags);
  4819. _JumpIfError(hr, error, "_SetColumn");
  4820. }
  4821. // Set or update value
  4822. hr = _SetColumn(
  4823. pcs->SesId,
  4824. pTable->TableId,
  4825. pdbaux->pdtValue->dbcolumnid,
  4826. cbValue,
  4827. pbValue);
  4828. _JumpIfError(hr, error, "_SetColumn");
  4829. CSASSERTTHREAD(pcs);
  4830. hr = _dbgJetUpdate(pcs->SesId, pTable->TableId, NULL, 0, NULL);
  4831. _JumpIfError(hr, error, "JetUpdate");
  4832. }
  4833. error:
  4834. return(myJetHResult(hr));
  4835. }
  4836. HRESULT
  4837. CCertDB::_GetIndirect(
  4838. IN CERTSESSION *pcs,
  4839. IN OUT CERTSESSIONTABLE *pTable,
  4840. IN WCHAR const *pwszNameValue,
  4841. OPTIONAL OUT DWORD *pdwExtFlags,
  4842. IN OUT DWORD *pcbValue,
  4843. OPTIONAL OUT BYTE *pbValue)
  4844. {
  4845. HRESULT hr;
  4846. DBAUXDATA const *pdbaux;
  4847. CERTSESSIONTABLE Table;
  4848. CERTVIEWRESTRICTION cvr;
  4849. if (NULL == pcs || NULL == pwszNameValue || NULL == pcbValue)
  4850. {
  4851. hr = E_POINTER;
  4852. _JumpError(hr, error, "NULL parm");
  4853. }
  4854. if (NULL == pdwExtFlags)
  4855. {
  4856. pdbaux = &g_dbauxAttributes;
  4857. cvr.ColumnIndex = DTI_ATTRIBUTETABLE | DTA_ATTRIBUTENAME;
  4858. }
  4859. else
  4860. {
  4861. pdbaux = &g_dbauxExtensions;
  4862. cvr.ColumnIndex = DTI_EXTENSIONTABLE | DTE_EXTENSIONNAME;
  4863. }
  4864. DBGPRINT((
  4865. DBG_SS_CERTDBI,
  4866. "IN: _GetIndirect(%hs.%ws) cb = %x\n",
  4867. pdbaux->pszTable,
  4868. pwszNameValue,
  4869. *pcbValue));
  4870. cvr.SeekOperator = CVR_SEEK_EQ;
  4871. cvr.SortOrder = CVR_SORT_NONE;
  4872. cvr.cbValue = wcslen(pwszNameValue) * sizeof(WCHAR);
  4873. cvr.pbValue = (BYTE *) pwszNameValue;
  4874. hr = _OpenTable(pcs, pdbaux, &cvr, pTable);
  4875. if (S_FALSE == hr)
  4876. {
  4877. hr = CERTSRV_E_PROPERTY_EMPTY;
  4878. }
  4879. _JumpIfError2(hr, error, "_OpenTable", CERTSRV_E_PROPERTY_EMPTY);
  4880. if (NULL != pdwExtFlags)
  4881. {
  4882. DWORD cb;
  4883. // Get flags column
  4884. cb = sizeof(*pdwExtFlags);
  4885. hr = _RetrieveColumn(
  4886. pcs,
  4887. pTable->TableId,
  4888. pdbaux->pdtFlags->dbcolumnid,
  4889. pdbaux->pdtFlags->dbcoltyp,
  4890. &cb,
  4891. (BYTE *) pdwExtFlags);
  4892. _JumpIfError(hr, error, "_RetrieveColumn");
  4893. DBGPRINT((
  4894. DBG_SS_CERTDBI,
  4895. "_GetIndirect(%hs): Flags = %x\n",
  4896. pdbaux->pszTable,
  4897. *pdwExtFlags));
  4898. }
  4899. // Get value column
  4900. hr = _RetrieveColumn(
  4901. pcs,
  4902. pTable->TableId,
  4903. pdbaux->pdtValue->dbcolumnid,
  4904. pdbaux->pdtValue->dbcoltyp,
  4905. pcbValue,
  4906. pbValue);
  4907. if (CERTSRV_E_PROPERTY_EMPTY == hr && NULL != pdwExtFlags)
  4908. {
  4909. // return zero length property value and S_OK, so the caller can see
  4910. // the extension flags.
  4911. *pcbValue = 0;
  4912. hr = S_OK;
  4913. }
  4914. _JumpIfError(hr, error, "_RetrieveColumn");
  4915. DBGPRINT((
  4916. DBG_SS_CERTDBI,
  4917. "OUT: _GetIndirect(%hs.%ws) cb = %x\n",
  4918. pdbaux->pszTable,
  4919. pwszNameValue,
  4920. *pcbValue));
  4921. error:
  4922. return(myJetHResult(hr));
  4923. }
  4924. #define CB_FETCHDELTA 256
  4925. // Fetch a column. Loop if we have to grow the buffer.
  4926. HRESULT
  4927. CCertDB::_RetrieveColumnBuffer(
  4928. IN CERTSESSION *pcs,
  4929. IN JET_TABLEID tableid,
  4930. IN JET_COLUMNID columnid,
  4931. IN JET_COLTYP coltyp,
  4932. OUT DWORD *pcbProp,
  4933. IN OUT BYTE **ppbBuf,
  4934. IN OUT DWORD *pcbBuf)
  4935. {
  4936. HRESULT hr;
  4937. BYTE *pbBuf = *ppbBuf;
  4938. DWORD cbBuf = *pcbBuf;
  4939. DWORD cb;
  4940. cb = cbBuf;
  4941. while (TRUE)
  4942. {
  4943. if (NULL == pbBuf)
  4944. {
  4945. // If cbBuf == 0, allocate CB_FETCHDELTA bytes.
  4946. // Otherwise, allocate column size *plus* CB_FETCHDELTA bytes.
  4947. // Ensures we make no more than two calls to _RetrieveColumn.
  4948. cb += CB_FETCHDELTA;
  4949. pbBuf = (BYTE *) LocalAlloc(LMEM_FIXED, cb);
  4950. if (NULL == pbBuf)
  4951. {
  4952. hr = E_OUTOFMEMORY;
  4953. _JumpError(hr, error, "LocalAlloc");
  4954. }
  4955. DBGPRINT((
  4956. DBG_SS_CERTDB,
  4957. "Grow buffer: %x --> %x\n", cbBuf, cb));
  4958. cbBuf = cb;
  4959. }
  4960. cb = cbBuf;
  4961. hr = _RetrieveColumn(
  4962. pcs,
  4963. tableid,
  4964. columnid,
  4965. coltyp,
  4966. &cb,
  4967. pbBuf);
  4968. if (S_OK == hr)
  4969. {
  4970. *pcbProp = cb;
  4971. break; // data fit in the buffer
  4972. }
  4973. if (HRESULT_FROM_WIN32(ERROR_BUFFER_OVERFLOW) != hr)
  4974. {
  4975. _JumpError2(hr, error, "_RetrieveColumn", CERTSRV_E_PROPERTY_EMPTY);
  4976. }
  4977. // Data won't fit. Grow the buffer.
  4978. CSASSERT(NULL != pbBuf);
  4979. LocalFree(pbBuf);
  4980. pbBuf = NULL;
  4981. }
  4982. CSASSERT(S_OK == hr);
  4983. error:
  4984. *ppbBuf = pbBuf;
  4985. *pcbBuf = cbBuf;
  4986. return(hr);
  4987. }
  4988. HRESULT
  4989. CCertDB::_RetrieveColumn(
  4990. IN CERTSESSION *pcs,
  4991. IN JET_TABLEID tableid,
  4992. IN JET_COLUMNID columnid,
  4993. IN JET_COLTYP coltyp,
  4994. IN OUT DWORD *pcbProp,
  4995. OPTIONAL OUT BYTE *pbProp)
  4996. {
  4997. HRESULT hr;
  4998. DWORD cbActual;
  4999. DWORD cbTotal;
  5000. CSASSERT(IsValidJetTableId(tableid));
  5001. hr = _dbgJetRetrieveColumn(
  5002. pcs->SesId,
  5003. tableid,
  5004. columnid,
  5005. NULL,
  5006. 0,
  5007. &cbActual,
  5008. JET_bitRetrieveCopy,
  5009. NULL);
  5010. if ((HRESULT) JET_wrnColumnNull == hr)
  5011. {
  5012. // Routine GetProperty call:
  5013. // _JumpIfError(hr, error, "JetRetrieveColumn: Property EMPTY");
  5014. hr = CERTSRV_E_PROPERTY_EMPTY;
  5015. goto error;
  5016. }
  5017. if ((HRESULT) JET_wrnBufferTruncated != hr)
  5018. {
  5019. _JumpIfError2(hr, error, "JetRetrieveColumn", JET_errNoCurrentRecord);
  5020. }
  5021. if (cbActual == 0)
  5022. {
  5023. hr = CERTSRV_E_PROPERTY_EMPTY;
  5024. _JumpError(hr, error, "JetRetrieveColumn: cbActual=0: Property EMPTY");
  5025. }
  5026. cbTotal = cbActual;
  5027. if (ISTEXTCOLTYP(coltyp))
  5028. {
  5029. DBGPRINT((DBG_SS_CERTDBI, "Size of text %d\n", cbActual));
  5030. cbTotal += sizeof(WCHAR);
  5031. }
  5032. if (NULL == pbProp || cbTotal > *pcbProp)
  5033. {
  5034. *pcbProp = cbTotal;
  5035. hr = S_OK;
  5036. if (NULL != pbProp)
  5037. {
  5038. hr = HRESULT_FROM_WIN32(ERROR_BUFFER_OVERFLOW);
  5039. //_PrintError(hr, "output buffer too small");
  5040. }
  5041. goto error;
  5042. }
  5043. hr = _dbgJetRetrieveColumn(
  5044. pcs->SesId,
  5045. tableid,
  5046. columnid,
  5047. pbProp,
  5048. cbActual,
  5049. &cbActual,
  5050. JET_bitRetrieveCopy,
  5051. NULL);
  5052. _JumpIfError(hr, error, "JetRetrieveColumn");
  5053. *pcbProp = cbActual;
  5054. if (ISTEXTCOLTYP(coltyp))
  5055. {
  5056. *(WCHAR *) &pbProp[cbActual] = L'\0';
  5057. }
  5058. error:
  5059. return(myJetHResult(hr));
  5060. }
  5061. HRESULT
  5062. CCertDB::_CreateIndex(
  5063. IN CERTSESSION *pcs,
  5064. IN JET_TABLEID tableid,
  5065. IN CHAR const *pszIndexName,
  5066. IN CHAR const *pchKey,
  5067. IN DWORD cbKey,
  5068. IN DWORD flags)
  5069. {
  5070. HRESULT hr;
  5071. CSASSERT(IsValidJetTableId(tableid));
  5072. hr = _dbgJetCreateIndex(
  5073. pcs->SesId,
  5074. tableid,
  5075. pszIndexName,
  5076. flags,
  5077. pchKey,
  5078. cbKey,
  5079. 0); // lDensity %, for splits (use default of 80%)
  5080. _JumpIfError3(
  5081. hr,
  5082. error,
  5083. "JetCreateIndex",
  5084. (HRESULT) JET_errIndexDuplicate,
  5085. (HRESULT) JET_errIndexHasPrimary);
  5086. DBGPRINT((
  5087. DBG_SS_CERTDBI,
  5088. "CreateIndex: %x:%hs idx=%hs len=%x flags=%x\n",
  5089. tableid,
  5090. pszIndexName,
  5091. pchKey,
  5092. cbKey,
  5093. flags));
  5094. error:
  5095. return(myJetHResult(hr));
  5096. }
  5097. HRESULT
  5098. CCertDB::_AddColumn(
  5099. IN CERTSESSION *pcs,
  5100. IN JET_TABLEID tableid,
  5101. IN DBTABLE const *pdt)
  5102. {
  5103. HRESULT hr;
  5104. JET_COLUMNDEF columndef;
  5105. JET_COLUMNID columnid;
  5106. CSASSERT(IsValidJetTableId(tableid));
  5107. ZeroMemory(&columndef, sizeof(columndef));
  5108. columndef.cbStruct = sizeof(columndef);
  5109. columndef.cp = 1200; // unicode (1200) instead of Ascii (1252)
  5110. columndef.langid = 0x409;
  5111. columndef.wCountry = 1;
  5112. columndef.coltyp = pdt->dbcoltyp;
  5113. columndef.cbMax = pdt->dbcolumnMax;
  5114. columndef.grbit = pdt->dbgrbit;
  5115. DBGPRINT((
  5116. DBG_SS_CERTDBI,
  5117. "AddColumn: %x:%hs coltyp=%x cbMax=%x grbit=%x\n",
  5118. tableid,
  5119. pdt->pszFieldName,
  5120. pdt->dbcoltyp,
  5121. pdt->dbcolumnMax,
  5122. pdt->dbgrbit));
  5123. hr = _dbgJetAddColumn(
  5124. pcs->SesId,
  5125. tableid,
  5126. pdt->pszFieldName,
  5127. &columndef,
  5128. NULL,
  5129. 0,
  5130. &columnid);
  5131. CSASSERT(JET_wrnColumnMaxTruncated != hr);
  5132. _JumpIfErrorStr3(
  5133. hr,
  5134. error,
  5135. "JetAddColumn",
  5136. pdt->pwszPropName,
  5137. (HRESULT) JET_errColumnDuplicate,
  5138. (HRESULT) JET_errColumnRedundant);
  5139. error:
  5140. return(myJetHResult(hr));
  5141. }
  5142. HRESULT
  5143. CCertDB::_CreateTable(
  5144. IN DWORD CreateFlags, // CF_*
  5145. IN CERTSESSION *pcs,
  5146. IN DBCREATETABLE const *pct)
  5147. {
  5148. HRESULT hr;
  5149. JET_TABLEID tableid;
  5150. BOOL fTableOpen;
  5151. DWORD dwLength;
  5152. CHAR achCol[MAX_PATH];
  5153. DBTABLE const *pdt;
  5154. DBGPRINT((
  5155. DBG_SS_CERTDBI,
  5156. "_CreateTable(%x, %hs)\n",
  5157. CreateFlags,
  5158. pct->pszTableName));
  5159. fTableOpen = FALSE;
  5160. if (CF_MISSINGTABLES & CreateFlags)
  5161. {
  5162. hr = _dbgJetCreateTable(
  5163. pcs->SesId,
  5164. pcs->DBId,
  5165. pct->pszTableName,
  5166. 4,
  5167. 0,
  5168. &tableid);
  5169. if ((HRESULT) JET_errTableDuplicate != hr)
  5170. {
  5171. _JumpIfError(hr, error, "JetCreateTable");
  5172. if (!(CF_DATABASE & CreateFlags))
  5173. {
  5174. DBGPRINT((
  5175. DBG_SS_CERTDB,
  5176. "Created Missing Table: %hs:%x\n",
  5177. pct->pszTableName,
  5178. tableid));
  5179. }
  5180. hr = _dbgJetCloseTable(pcs->SesId, tableid);
  5181. _JumpIfError(hr, error, "JetCloseTable");
  5182. }
  5183. }
  5184. hr = _dbgJetOpenTable(
  5185. pcs->SesId,
  5186. pcs->DBId,
  5187. pct->pszTableName,
  5188. NULL, // pvParameters
  5189. 0, // cbParameters
  5190. JET_bitTableDenyRead, // grbit
  5191. &tableid);
  5192. _JumpIfError(hr, error, "JetOpenTable");
  5193. fTableOpen = TRUE;
  5194. CSASSERT(IsValidJetTableId(tableid));
  5195. DBGPRINT((DBG_SS_CERTDBI, "OpenTable: %hs: %x\n", pct->pszTableName, tableid));
  5196. if (NULL != pct->pdt)
  5197. {
  5198. HRESULT hrDuplicate;
  5199. HRESULT hrRedundant;
  5200. HRESULT hrHasPrimary;
  5201. if ((CF_DATABASE | CF_MISSINGTABLES | CF_MISSINGCOLUMNS) & CreateFlags)
  5202. {
  5203. hrDuplicate = myJetHResult(JET_errColumnDuplicate);
  5204. hrRedundant = myJetHResult(JET_errColumnRedundant);
  5205. for (pdt = pct->pdt; NULL != pdt->pwszPropName; pdt++)
  5206. {
  5207. hr = _AddColumn(pcs, tableid, pdt);
  5208. if (hrDuplicate == hr || hrRedundant == hr)
  5209. {
  5210. _PrintError2(hr, "_AddColumn", hr);
  5211. hr = S_OK;
  5212. }
  5213. else
  5214. if (S_OK == hr && !(CF_DATABASE & CreateFlags))
  5215. {
  5216. m_fAddedNewColumns = TRUE;
  5217. DBGPRINT((
  5218. DBG_SS_CERTDB,
  5219. "Added Missing Column: %hs.%hs\n",
  5220. pct->pszTableName,
  5221. pdt->pszFieldName));
  5222. }
  5223. _JumpIfErrorStr(hr, error, "_AddColumn", pdt->pwszPropName);
  5224. }
  5225. }
  5226. if ((CF_DATABASE | CF_MISSINGTABLES | CF_MISSINGCOLUMNS | CF_MISSINGINDEXES) & CreateFlags)
  5227. {
  5228. hrDuplicate = myJetHResult(JET_errIndexDuplicate);
  5229. hrHasPrimary = myJetHResult(JET_errIndexHasPrimary);
  5230. for (pdt = pct->pdt; NULL != pdt->pwszPropName; pdt++)
  5231. {
  5232. if (NULL != pdt->pszIndexName)
  5233. {
  5234. DWORD dwCreateIndexFlags = 0;
  5235. char *psz = achCol;
  5236. if (DBTF_INDEXPRIMARY & pdt->dwFlags)
  5237. {
  5238. dwCreateIndexFlags |= JET_bitIndexPrimary;
  5239. }
  5240. if (DBTF_INDEXUNIQUE & pdt->dwFlags)
  5241. {
  5242. dwCreateIndexFlags |= JET_bitIndexUnique;
  5243. }
  5244. if (DBTF_INDEXIGNORENULL & pdt->dwFlags)
  5245. {
  5246. dwCreateIndexFlags |= JET_bitIndexIgnoreNull;
  5247. }
  5248. if (DBTF_INDEXREQUESTID & pdt->dwFlags)
  5249. {
  5250. psz += sprintf(psz, "+%hs", szREQUESTID) + 1;
  5251. }
  5252. psz += sprintf(psz, "+%hs", pdt->pszFieldName) + 1;
  5253. *psz++ = '\0'; // double terminate
  5254. if (ISTEXTCOLTYP(pdt->dbcoltyp))
  5255. {
  5256. // if text field, include 2-byte langid
  5257. *(WORD UNALIGNED *) psz = (WORD) 0x409;
  5258. psz += sizeof(WORD);
  5259. *psz++ = '\0'; // double terminate
  5260. *psz++ = '\0'; // double terminate
  5261. }
  5262. hr = _CreateIndex(
  5263. pcs,
  5264. tableid,
  5265. pdt->pszIndexName,
  5266. achCol,
  5267. SAFE_SUBTRACT_POINTERS(psz, achCol),
  5268. dwCreateIndexFlags);
  5269. if (hrDuplicate == hr ||
  5270. (hrHasPrimary == hr &&
  5271. (DBTF_INDEXPRIMARY & pdt->dwFlags)))
  5272. {
  5273. _PrintError2(hr, "_CreateIndex", hr);
  5274. hr = S_OK;
  5275. }
  5276. else
  5277. if (S_OK == hr && !(CF_DATABASE & CreateFlags))
  5278. {
  5279. DBGPRINT((
  5280. DBG_SS_CERTDB,
  5281. "Added Missing Index: %hs.%hs\n",
  5282. pct->pszTableName,
  5283. pdt->pszIndexName));
  5284. if (chTEXTPREFIX == *pdt->pszIndexName ||
  5285. (DBTF_INDEXRENAMED & pdt->dwFlags))
  5286. {
  5287. char const *pszIndexName = &pdt->pszIndexName[1];
  5288. CSASSERTTHREAD(pcs);
  5289. if (DBTF_INDEXRENAMED & pdt->dwFlags)
  5290. {
  5291. pszIndexName += strlen(pszIndexName) + 1;
  5292. }
  5293. hr = _dbgJetDeleteIndex(
  5294. pcs->SesId,
  5295. tableid,
  5296. pszIndexName);
  5297. _PrintIfError2(hr, "JetDeleteIndex", hr);
  5298. if (S_OK == hr)
  5299. {
  5300. DBGPRINT((
  5301. DBG_SS_CERTDB,
  5302. "Deleted index %hs.%hs\n",
  5303. pct->pszTableName,
  5304. pszIndexName));
  5305. }
  5306. hr = S_OK;
  5307. }
  5308. }
  5309. _JumpIfError(hr, error, "_CreateIndex");
  5310. }
  5311. }
  5312. }
  5313. }
  5314. error:
  5315. if (fTableOpen)
  5316. {
  5317. HRESULT hr2;
  5318. hr2 = _dbgJetCloseTable(pcs->SesId, tableid);
  5319. if (S_OK == hr)
  5320. {
  5321. hr = hr2;
  5322. _JumpIfError(hr, error, "JetCloseTable");
  5323. }
  5324. }
  5325. return(myJetHResult(hr));
  5326. }
  5327. HRESULT
  5328. CCertDB::_Create(
  5329. IN DWORD CreateFlags, // CF_*
  5330. IN CHAR const *pszDataBaseName)
  5331. {
  5332. HRESULT hr;
  5333. DBCREATETABLE const *pct;
  5334. CERTSESSION *pcs = NULL;
  5335. DBGPRINT((
  5336. DBG_SS_CERTDBI,
  5337. "_Create(%x, %hs)\n",
  5338. CreateFlags,
  5339. pszDataBaseName));
  5340. hr = _AllocateSession(&pcs);
  5341. _JumpIfError(hr, error, "_AllocateSession");
  5342. if (CF_DATABASE & CreateFlags)
  5343. {
  5344. hr = _dbgJetCreateDatabase(
  5345. pcs->SesId,
  5346. pszDataBaseName,
  5347. NULL,
  5348. &pcs->DBId,
  5349. 0);
  5350. _JumpIfError(hr, error, "JetCreateDatabase");
  5351. hr = _dbgJetCloseDatabase(pcs->SesId, pcs->DBId, 0);
  5352. _JumpIfError(hr, error, "JetCloseDatabase");
  5353. }
  5354. hr = _dbgJetOpenDatabase(
  5355. pcs->SesId,
  5356. pszDataBaseName,
  5357. NULL,
  5358. &pcs->DBId,
  5359. JET_bitDbExclusive);
  5360. _JumpIfError(hr, error, "JetOpenDatabase");
  5361. hr = _dbgJetBeginTransaction(pcs->SesId);
  5362. _JumpIfError(hr, error, "JetBeginTransaction");
  5363. for (pct = g_actDataBase; NULL != pct->pszTableName; pct++)
  5364. {
  5365. hr = _CreateTable(CreateFlags, pcs, pct);
  5366. _JumpIfError(hr, error, "_CreateTable");
  5367. }
  5368. hr = _dbgJetCommitTransaction(pcs->SesId, 0);
  5369. _JumpIfError(hr, error, "JetCommitTransaction");
  5370. hr = _dbgJetCloseDatabase(pcs->SesId, pcs->DBId, 0);
  5371. _JumpIfError(hr, error, "JetCloseDatabase");
  5372. error:
  5373. if (NULL != pcs)
  5374. {
  5375. ReleaseSession(pcs);
  5376. }
  5377. return(myJetHResult(hr));
  5378. }
  5379. HRESULT
  5380. CCertDB::_DupString(
  5381. OPTIONAL IN WCHAR const *pwszPrefix,
  5382. IN WCHAR const *pwszIn,
  5383. OUT WCHAR **ppwszOut)
  5384. {
  5385. DWORD cbPrefix;
  5386. DWORD cb;
  5387. HRESULT hr;
  5388. cbPrefix = 0;
  5389. if (NULL != pwszPrefix)
  5390. {
  5391. cbPrefix = wcslen(pwszPrefix) * sizeof(WCHAR);
  5392. }
  5393. cb = (wcslen(pwszIn) + 1) * sizeof(WCHAR);
  5394. *ppwszOut = (WCHAR *) CoTaskMemAlloc(cbPrefix + cb);
  5395. if (NULL == *ppwszOut)
  5396. {
  5397. hr = E_OUTOFMEMORY;
  5398. _JumpError(hr, error, "CoTaskMemAlloc");
  5399. }
  5400. if (NULL != pwszPrefix)
  5401. {
  5402. CopyMemory(*ppwszOut, pwszPrefix, cbPrefix);
  5403. }
  5404. CopyMemory((BYTE *) *ppwszOut + cbPrefix, pwszIn, cb);
  5405. hr = S_OK;
  5406. error:
  5407. return(hr);
  5408. }