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.

4919 lines
110 KiB

  1. //+--------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1996 - 1999
  5. //
  6. // File: admin.cpp
  7. //
  8. // Contents: Implementation of DCOM object for RPC services
  9. //
  10. // History: July-97 xtan created
  11. //
  12. //---------------------------------------------------------------------------
  13. #include <pch.cpp>
  14. #pragma hdrstop
  15. #include <accctrl.h>
  16. #include "csdisp.h"
  17. #include "csprop.h"
  18. #include "cscom.h"
  19. #include "certlog.h"
  20. #include "certsrvd.h"
  21. #include "admin.h"
  22. #include "resource.h"
  23. #include "dbtable.h"
  24. #include "elog.h"
  25. #define __dwFILE__ __dwFILE_CERTSRV_ADMIN_CPP__
  26. // Global variables
  27. long g_cAdminComponents = 0; // Count of active components
  28. long g_cAdminServerLocks = 0; // Count of locks
  29. DWORD g_dwAdminRegister = 0;
  30. IClassFactory* g_pIAdminFactory = NULL;
  31. extern HWND g_hwndMain;
  32. #ifdef DBG_CERTSRV_DEBUG_PRINT
  33. DWORD s_ssAdmin = DBG_SS_CERTSRVI;
  34. #endif
  35. using namespace CertSrv;
  36. // Admin component
  37. // begin implementing cert admin services
  38. HRESULT
  39. AdminGetIndexedCRL(
  40. /* [unique][string][in] */ const wchar_t __RPC_FAR *pwszAuthority,
  41. /* [in] */ DWORD CertIndex, // -1: current CA cert
  42. /* [in] */ DWORD Flags, // CA_CRL_*
  43. /* [ref][out] */ CERTTRANSBLOB __RPC_FAR *pctbCRL)
  44. {
  45. HRESULT hr;
  46. CRL_CONTEXT const *pCRL = NULL;
  47. CAuditEvent audit(0, g_dwAuditFilter);
  48. DWORD State = 0;
  49. pctbCRL->pb = NULL;
  50. pctbCRL->cb = 0;
  51. hr = CertSrvEnterServer(&State);
  52. _JumpIfError(hr, error, "CertSrvEnterServer");
  53. hr = CheckAuthorityName(pwszAuthority);
  54. _JumpIfError(hr, error, "No authority name");
  55. __try
  56. {
  57. hr = audit.AccessCheck(
  58. CA_ACCESS_ALLREADROLES,
  59. audit.m_gcNoAuditSuccess | audit.m_gcNoAuditFailure);
  60. _LeaveIfError(hr, "CAuditEvent::AccessCheck");
  61. switch (Flags)
  62. {
  63. case CA_CRL_BASE:
  64. case CA_CRL_DELTA:
  65. break;
  66. default:
  67. hr = E_INVALIDARG;
  68. _LeaveError(hr, "Flags");
  69. }
  70. // get the requested CRL:
  71. hr = CRLGetCRL(CertIndex, CA_CRL_DELTA == Flags, &pCRL, NULL);
  72. _LeaveIfError(hr, "CRLGetCRL");
  73. pctbCRL->cb = pCRL->cbCrlEncoded;
  74. pctbCRL->pb = (BYTE *) MIDL_user_allocate(pCRL->cbCrlEncoded);
  75. if (NULL == pctbCRL->pb)
  76. {
  77. hr = E_OUTOFMEMORY;
  78. _LeaveError(hr, "MIDL_user_allocate");
  79. }
  80. CopyMemory(pctbCRL->pb, pCRL->pbCrlEncoded, pCRL->cbCrlEncoded);
  81. myRegisterMemFree(pctbCRL->pb, CSM_MIDLUSERALLOC);
  82. }
  83. __except(hr = myHEXCEPTIONCODE(), EXCEPTION_EXECUTE_HANDLER)
  84. {
  85. _PrintError(hr, "Exception");
  86. }
  87. error:
  88. if (NULL != pCRL)
  89. {
  90. CertFreeCRLContext(pCRL);
  91. }
  92. CertSrvExitServer(State);
  93. CSASSERT(S_OK == hr || FAILED(hr));
  94. return(hr);
  95. }
  96. STDMETHODIMP
  97. CCertAdminD::GetCRL(
  98. /* [unique][string][in] */ const wchar_t __RPC_FAR *pwszAuthority,
  99. /* [ref][out] */ CERTTRANSBLOB __RPC_FAR *pctbCRL)
  100. {
  101. HRESULT hr;
  102. // Just get current base CRL:
  103. hr = AdminGetIndexedCRL(pwszAuthority, MAXDWORD, CA_CRL_BASE, pctbCRL);
  104. _JumpIfError(hr, error, "AdminGetIndexedCRL");
  105. error:
  106. CSASSERT(S_OK == hr || FAILED(hr));
  107. return(hr);
  108. }
  109. STDMETHODIMP
  110. CCertAdminD::GetArchivedKey(
  111. /* [unique][string][in] */ const wchar_t __RPC_FAR *pwszAuthority,
  112. /* [in] */ DWORD dwRequestId,
  113. /* [ref][out] */ CERTTRANSBLOB __RPC_FAR *pctbArchivedKey)
  114. {
  115. HRESULT hr;
  116. CAuditEvent audit(SE_AUDITID_CERTSRV_GETARCHIVEDKEY, g_dwAuditFilter);
  117. DWORD State = 0;
  118. pctbArchivedKey->pb = NULL;
  119. pctbArchivedKey->cb = 0;
  120. hr = CertSrvEnterServer(&State);
  121. _JumpIfError(hr, error, "CertSrvEnterServer");
  122. hr = CheckAuthorityName(pwszAuthority);
  123. _JumpIfError(hr, error, "No authority name");
  124. __try
  125. {
  126. hr = audit.AddData(dwRequestId); // %1 request ID
  127. _LeaveIfError(hr, "CAuditEvent::AddData");
  128. hr = audit.AccessCheck(
  129. CA_ACCESS_OFFICER,
  130. audit.m_gcNoAuditSuccess);
  131. _LeaveIfError(hr, "CAuditEvent::AccessCheck");
  132. hr = CheckOfficerRights(dwRequestId, audit);
  133. _LeaveIfError(hr, "CheckOfficerRights");
  134. hr = PKCSGetArchivedKey(
  135. dwRequestId,
  136. &pctbArchivedKey->pb,
  137. &pctbArchivedKey->cb);
  138. _LeaveIfError(hr, "PKCSGetArchivedKey");
  139. myRegisterMemFree(pctbArchivedKey->pb, CSM_COTASKALLOC);
  140. hr = audit.CachedGenerateAudit();
  141. _LeaveIfError(hr, "CAuditEvent::CachedGenerateAudit");
  142. }
  143. __except(hr = myHEXCEPTIONCODE(), EXCEPTION_EXECUTE_HANDLER)
  144. {
  145. _PrintError(hr, "Exception");
  146. }
  147. error:
  148. CertSrvExitServer(State);
  149. CSASSERT(S_OK == hr || FAILED(hr));
  150. return(hr);
  151. }
  152. STDMETHODIMP
  153. CCertAdminD::GetCAProperty(
  154. IN wchar_t const *pwszAuthority,
  155. IN LONG PropId, // CR_PROP_*
  156. IN LONG PropIndex,
  157. IN LONG PropType, // PROPTYPE_*
  158. OUT CERTTRANSBLOB *pctbPropertyValue)
  159. {
  160. HRESULT hr;
  161. DWORD State = 0;
  162. DBGPRINT((
  163. s_ssAdmin,
  164. "CCertAdminD::GetCAProperty(tid=%d, this=%x)\n",
  165. GetCurrentThreadId(),
  166. this));
  167. hr = CertSrvEnterServer(&State);
  168. _JumpIfError(hr, error, "CertSrvEnterServer");
  169. hr = CheckAuthorityName(pwszAuthority);
  170. _JumpIfError(hr, error, "No authority name");
  171. __try
  172. {
  173. CAuditEvent audit(0, g_dwAuditFilter);
  174. hr = audit.AccessCheck(
  175. CA_ACCESS_ALLREADROLES,
  176. audit.m_gcNoAuditSuccess |
  177. audit.m_gcNoAuditFailure);
  178. _LeaveIfError(hr, "CAuditEvent::AccessCheck");
  179. hr = RequestGetCAProperty(
  180. PropId,
  181. PropIndex,
  182. PropType,
  183. pctbPropertyValue);
  184. _LeaveIfError(hr, "RequestGetCAProperty");
  185. }
  186. __except(hr = myHEXCEPTIONCODE(), EXCEPTION_EXECUTE_HANDLER)
  187. {
  188. _PrintError(hr, "Exception");
  189. }
  190. error:
  191. CertSrvExitServer(State);
  192. CSASSERT(S_OK == hr || FAILED(hr));
  193. return(hr);
  194. }
  195. STDMETHODIMP
  196. CCertAdminD::SetCAProperty(
  197. IN wchar_t const *pwszAuthority,
  198. IN LONG PropId, // CR_PROP_*
  199. IN LONG PropIndex,
  200. IN LONG PropType, // PROPTYPE_*
  201. OUT CERTTRANSBLOB *pctbPropertyValue)
  202. {
  203. HRESULT hr;
  204. DBGPRINT((
  205. s_ssAdmin,
  206. "CCertAdminD::SetCAProperty(tid=%d, this=%x)\n",
  207. GetCurrentThreadId(),
  208. this));
  209. hr = RequestSetCAProperty(
  210. pwszAuthority,
  211. PropId,
  212. PropIndex,
  213. PropType,
  214. pctbPropertyValue);
  215. _JumpIfError(hr, error, "RequestSetCAProperty");
  216. error:
  217. return(hr);
  218. }
  219. STDMETHODIMP
  220. CCertAdminD::GetCAPropertyInfo(
  221. IN wchar_t const *pwszAuthority,
  222. OUT LONG *pcProperty,
  223. OUT CERTTRANSBLOB *pctbPropInfo)
  224. {
  225. HRESULT hr;
  226. DWORD State = 0;
  227. DBGPRINT((
  228. s_ssAdmin,
  229. "CCertAdminD::GetCAPropertyInfo(tid=%d, this=%x)\n",
  230. GetCurrentThreadId(),
  231. this));
  232. hr = CertSrvEnterServer(&State);
  233. _JumpIfError(hr, error, "CertSrvEnterServer");
  234. hr = CheckAuthorityName(pwszAuthority);
  235. _JumpIfError(hr, error, "No authority name");
  236. __try
  237. {
  238. CAuditEvent audit(0, g_dwAuditFilter);
  239. hr = audit.AccessCheck(
  240. CA_ACCESS_ALLREADROLES,
  241. audit.m_gcNoAuditSuccess |
  242. audit.m_gcNoAuditFailure);
  243. _LeaveIfError(hr, "CAuditEvent::AccessCheck");
  244. hr = RequestGetCAPropertyInfo(
  245. pcProperty,
  246. pctbPropInfo);
  247. _LeaveIfError(hr, "RequestGetCAPropertyInfo");
  248. }
  249. __except(hr = myHEXCEPTIONCODE(), EXCEPTION_EXECUTE_HANDLER)
  250. {
  251. _PrintError(hr, "Exception");
  252. }
  253. error:
  254. CertSrvExitServer(State);
  255. CSASSERT(S_OK == hr || FAILED(hr));
  256. return(hr);
  257. }
  258. STDMETHODIMP
  259. CCertAdminD::PublishCRL(
  260. /* [unique][string][in] */ const wchar_t __RPC_FAR *pwszAuthority,
  261. /* [in] */ FILETIME NextUpdate)
  262. {
  263. HRESULT hr;
  264. // CA_CRL_BASE implies CA_CRL_DELTA when delta CRLs are enabled.
  265. hr = PublishCRLs(pwszAuthority, NextUpdate, CA_CRL_BASE);
  266. _JumpError(hr, error, "PublishCRLs");
  267. error:
  268. CSASSERT(S_OK == hr || FAILED(hr));
  269. return(hr);
  270. }
  271. STDMETHODIMP
  272. CCertAdminD::PublishCRLs(
  273. /* [unique][string][in] */ const wchar_t __RPC_FAR *pwszAuthority,
  274. /* [in] */ FILETIME NextUpdate,
  275. /* [in] */ DWORD Flags) // CA_CRL_*
  276. {
  277. HRESULT hr;
  278. BOOL fRetry = FALSE;
  279. BOOL fForceRepublishCRL;
  280. BOOL fShadowDelta = FALSE;
  281. WCHAR *pwszUserName = NULL;
  282. CAuditEvent audit(SE_AUDITID_CERTSRV_PUBLISHCRL, g_dwAuditFilter);
  283. DWORD State = 0;
  284. DBGPRINT((
  285. s_ssAdmin,
  286. "CCertAdminD::PublishCRL(tid=%d, this=%x)\n",
  287. GetCurrentThreadId(),
  288. this));
  289. hr = CertSrvEnterServer(&State);
  290. _JumpIfError(hr, error, "CertSrvEnterServer");
  291. hr = CheckAuthorityName(pwszAuthority);
  292. _JumpIfError(hr, error, "No authority name");
  293. __try
  294. {
  295. HRESULT hrPublish;
  296. hr = audit.AddData(NextUpdate); // %1 next update
  297. _LeaveIfError(hr, "AddData");
  298. hr = audit.AddData(
  299. (CA_CRL_BASE & Flags)? true : false); // %2 publish base
  300. _LeaveIfError(hr, "AddData");
  301. hr = audit.AddData(
  302. (CA_CRL_DELTA & Flags)? true : false); // %3 publish delta
  303. _LeaveIfError(hr, "AddData");
  304. hr = audit.AccessCheck(
  305. CA_ACCESS_ADMIN,
  306. audit.m_gcAuditSuccessOrFailure);
  307. _LeaveIfError(hr, "CAuditEvent::AccessCheck");
  308. switch (~CA_CRL_REPUBLISH & Flags)
  309. {
  310. case CA_CRL_BASE:
  311. break;
  312. case CA_CRL_DELTA:
  313. if (g_fDeltaCRLPublishDisabled)
  314. {
  315. fShadowDelta = TRUE;
  316. }
  317. break;
  318. case CA_CRL_BASE | CA_CRL_DELTA:
  319. if (g_fDeltaCRLPublishDisabled)
  320. {
  321. hr = E_INVALIDARG;
  322. _LeaveError(hr, "Delta CRLs disabled");
  323. }
  324. break;
  325. default:
  326. hr = E_INVALIDARG;
  327. _LeaveError(hr, "Flags");
  328. }
  329. fForceRepublishCRL = (CA_CRL_REPUBLISH & Flags)? TRUE : FALSE;
  330. hr = GetClientUserName(NULL, &pwszUserName, NULL);
  331. _LeaveIfError(hr, "GetClientUserName");
  332. hr = CRLPublishCRLs(
  333. !fForceRepublishCRL, // fRebuildCRL
  334. fForceRepublishCRL, // fForceRepublish
  335. pwszUserName,
  336. CA_CRL_DELTA == (~CA_CRL_REPUBLISH & Flags), // fDeltaOnly
  337. fShadowDelta,
  338. NextUpdate,
  339. &fRetry,
  340. &hrPublish);
  341. _LeaveIfError(hr, "CRLPublishCRLs");
  342. hr = hrPublish;
  343. _LeaveIfError(hr, "CRLPublishCRLs(hrPublish)");
  344. }
  345. __except(hr = myHEXCEPTIONCODE(), EXCEPTION_EXECUTE_HANDLER)
  346. {
  347. _PrintError(hr, "Exception");
  348. }
  349. error:
  350. if (NULL != pwszUserName)
  351. {
  352. LocalFree(pwszUserName);
  353. }
  354. CertSrvExitServer(State);
  355. CSASSERT(S_OK == hr || FAILED(hr));
  356. return(hr);
  357. }
  358. STDMETHODIMP
  359. CCertAdminD::SetExtension(
  360. /* [unique][string][in] */ const wchar_t __RPC_FAR *pwszAuthority,
  361. /* [in] */ DWORD dwRequestId,
  362. /* [unique][string][in] */ const wchar_t __RPC_FAR *pwszExtensionName,
  363. /* [in] */ DWORD dwType,
  364. /* [in] */ DWORD dwFlags,
  365. /* [ref][in] */ CERTTRANSBLOB __RPC_FAR *pctbValue)
  366. {
  367. HRESULT hr;
  368. ICertDBRow *prow = NULL;
  369. CAuditEvent audit(SE_AUDITID_CERTSRV_SETEXTENSION, g_dwAuditFilter);
  370. DWORD State = 0;
  371. BOOL fCommitted = FALSE;
  372. DBGPRINT((
  373. s_ssAdmin,
  374. "CCertAdminD::SetExtension(tid=%d, this=%x)\n",
  375. GetCurrentThreadId(),
  376. this));
  377. hr = CertSrvEnterServer(&State);
  378. _JumpIfError(hr, error, "CertSrvEnterServer");
  379. hr = CheckAuthorityName(pwszAuthority);
  380. _JumpIfError(hr, error, "No authority name");
  381. __try
  382. {
  383. hr = audit.AddData(dwRequestId); // %1 Request ID
  384. _LeaveIfError(hr, "AddData");
  385. hr = audit.AddData(pwszExtensionName); // %2 name
  386. _LeaveIfError(hr, "AddData");
  387. hr = audit.AddData(dwType); // %3 type
  388. _LeaveIfError(hr, "AddData");
  389. hr = audit.AddData(dwFlags); // %4 flags
  390. _LeaveIfError(hr, "AddData");
  391. hr = audit.AddData(pctbValue->pb, pctbValue->cb); // %5 data
  392. _LeaveIfError(hr, "AddData");
  393. hr = audit.AccessCheck(
  394. CA_ACCESS_OFFICER,
  395. audit.m_gcNoAuditSuccess);
  396. _LeaveIfError(hr, "CAuditEvent::AccessCheck");
  397. hr = CheckOfficerRights(dwRequestId, audit);
  398. _LeaveIfError(hr, "CheckOfficerRights");
  399. hr = g_pCertDB->OpenRow(PROPTABLE_REQCERT, dwRequestId, NULL, &prow);
  400. _LeaveIfError(hr, "OpenRow");
  401. hr = CoreValidateRequestId(prow, DB_DISP_PENDING);
  402. if (S_OK != hr)
  403. {
  404. hr = myHError(hr);
  405. _LeaveError(hr, "CoreValidateRequestId");
  406. }
  407. hr = PropSetExtension(
  408. prow,
  409. PROPCALLER_ADMIN | (PROPTYPE_MASK & dwType),
  410. pwszExtensionName,
  411. EXTENSION_ORIGIN_ADMIN |
  412. (EXTENSION_POLICY_MASK & dwFlags),
  413. pctbValue->cb,
  414. pctbValue->pb);
  415. _LeaveIfError(hr, "PropSetExtension");
  416. hr = prow->CommitTransaction(TRUE);
  417. _LeaveIfError(hr, "CommitTransaction");
  418. fCommitted = TRUE;
  419. hr = audit.CachedGenerateAudit();
  420. _LeaveIfError(hr, "CAuditEvent::CachedGenerateAudit");
  421. }
  422. __except(hr = myHEXCEPTIONCODE(), EXCEPTION_EXECUTE_HANDLER)
  423. {
  424. _PrintError(hr, "Exception");
  425. }
  426. error:
  427. if (NULL != prow)
  428. {
  429. if (S_OK != hr && !fCommitted)
  430. {
  431. HRESULT hr2 = prow->CommitTransaction(FALSE);
  432. _PrintIfError(hr2, "CommitTransaction");
  433. }
  434. prow->Release();
  435. }
  436. CertSrvExitServer(State);
  437. CSASSERT(S_OK == hr || FAILED(hr));
  438. return(hr);
  439. }
  440. STDMETHODIMP
  441. CCertAdminD::SetAttributes(
  442. /* [unique][string][in] */ const wchar_t __RPC_FAR *pwszAuthority,
  443. /* [in] */ DWORD dwRequestId,
  444. /* [unique][string][in] */ const wchar_t __RPC_FAR *pwszAttributes)
  445. {
  446. HRESULT hr;
  447. ICertDBRow *prow = NULL;
  448. CAuditEvent audit(SE_AUDITID_CERTSRV_SETATTRIBUTES, g_dwAuditFilter);
  449. DWORD State = 0;
  450. BOOL fCommitted = FALSE;
  451. DBGPRINT((
  452. s_ssAdmin,
  453. "CCertAdminD::SetAttributes(tid=%d, this=%x)\n",
  454. GetCurrentThreadId(),
  455. this));
  456. hr = CertSrvEnterServer(&State);
  457. _JumpIfError(hr, error, "CertSrvEnterServer");
  458. hr = CheckAuthorityName(pwszAuthority);
  459. _JumpIfError(hr, error, "No authority name");
  460. __try
  461. {
  462. hr = audit.AddData(dwRequestId); // %1 request ID
  463. _LeaveIfError(hr, "AddData");
  464. hr = audit.AddData(pwszAttributes); // %2 attributes
  465. _LeaveIfError(hr, "AddData");
  466. hr = audit.AccessCheck(
  467. CA_ACCESS_OFFICER,
  468. audit.m_gcNoAuditSuccess);
  469. _LeaveIfError(hr, "CAuditEvent::AccessCheck");
  470. hr = CheckOfficerRights(dwRequestId, audit);
  471. _LeaveIfError(hr, "CheckOfficerRights");
  472. hr = g_pCertDB->OpenRow(PROPTABLE_REQCERT, dwRequestId, NULL, &prow);
  473. _LeaveIfError(hr, "OpenRow");
  474. hr = CoreValidateRequestId(prow, DB_DISP_PENDING);
  475. if (S_OK != hr)
  476. {
  477. hr = myHError(hr);
  478. _LeaveError(hr, "CoreValidateRequestId");
  479. }
  480. if (NULL == pwszAttributes)
  481. {
  482. hr = E_INVALIDARG;
  483. _LeaveError(hr, "pwszAttributes NULL");
  484. }
  485. hr = PKCSParseAttributes(
  486. prow,
  487. pwszAttributes,
  488. FALSE,
  489. PROPTABLE_CERTIFICATE,
  490. NULL);
  491. if (S_OK != hr)
  492. {
  493. hr = myHError(hr);
  494. _LeaveError(hr, "PKCSParseAttributes");
  495. }
  496. hr = prow->CommitTransaction(TRUE);
  497. _LeaveIfError(hr, "CommitTransaction");
  498. fCommitted = TRUE;
  499. hr = audit.CachedGenerateAudit();
  500. _LeaveIfError(hr, "CAuditEvent::CachedGenerateAudit");
  501. }
  502. __except(hr = myHEXCEPTIONCODE(), EXCEPTION_EXECUTE_HANDLER)
  503. {
  504. _PrintError(hr, "Exception");
  505. }
  506. error:
  507. if (NULL != prow)
  508. {
  509. if (S_OK != hr && !fCommitted)
  510. {
  511. HRESULT hr2 = prow->CommitTransaction(FALSE);
  512. _PrintIfError(hr2, "CommitTransaction");
  513. }
  514. prow->Release();
  515. }
  516. CertSrvExitServer(State);
  517. CSASSERT(S_OK == hr || FAILED(hr));
  518. return(hr);
  519. }
  520. STDMETHODIMP
  521. CCertAdminD::DenyRequest(
  522. /* [unique][string][in] */ const wchar_t __RPC_FAR *pwszAuthority,
  523. /* [in] */ DWORD dwRequestId)
  524. {
  525. HRESULT hr;
  526. DWORD Disposition;
  527. WCHAR *pwszUserName = NULL;
  528. CERTSRV_COM_CONTEXT ComContext;
  529. DWORD dwComContextIndex = MAXDWORD;
  530. CERTSRV_RESULT_CONTEXT Result;
  531. CAuditEvent audit(SE_AUDITID_CERTSRV_DENYREQUEST, g_dwAuditFilter);
  532. DWORD State = 0;
  533. ZeroMemory(&ComContext, sizeof(ComContext));
  534. DBGPRINT((
  535. s_ssAdmin,
  536. "CCertAdminD::DenyRequest(tid=%d, this=%x)\n",
  537. GetCurrentThreadId(),
  538. this));
  539. hr = CertSrvEnterServer(&State);
  540. _JumpIfError(hr, error, "CertSrvEnterServer");
  541. hr = CheckAuthorityName(pwszAuthority);
  542. _JumpIfError(hr, error, "No Authority Name");
  543. hr = RegisterComContext(&ComContext, &dwComContextIndex);
  544. _JumpIfError(hr, error, "RegisterComContext");
  545. ZeroMemory(&Result, sizeof(Result));
  546. Result.pdwRequestId = &dwRequestId;
  547. Result.pdwDisposition = &Disposition;
  548. __try
  549. {
  550. hr = audit.AddData(dwRequestId); // %1 request ID
  551. _LeaveIfError(hr, "AddData");
  552. hr = audit.AccessCheck(
  553. CA_ACCESS_OFFICER,
  554. audit.m_gcNoAuditSuccess);
  555. _LeaveIfError(hr, "CAuditEvent::AccessCheck");
  556. hr = CheckOfficerRights(dwRequestId, audit);
  557. _LeaveIfError(hr, "CheckOfficerRights");
  558. hr = GetClientUserName(NULL, &pwszUserName, NULL);
  559. _LeaveIfError(hr, "GetClientUserName");
  560. hr = CoreProcessRequest(
  561. CR_IN_DENY, // dwFlags
  562. pwszUserName,
  563. 0, // cbRequest
  564. NULL, // pbRequest
  565. NULL, // pwszAttributes
  566. NULL, // pwszSerialNumber
  567. dwComContextIndex,
  568. dwRequestId,
  569. &Result);
  570. if (S_OK != hr)
  571. {
  572. hr = myHError(hr);
  573. _LeaveError(hr, "CoreProcessRequest");
  574. }
  575. if (FAILED(Disposition))
  576. {
  577. hr = (HRESULT) Disposition;
  578. _LeaveError(hr, "CoreProcessRequest(Disposition)");
  579. }
  580. hr = audit.CachedGenerateAudit();
  581. _LeaveIfError(hr, "CAuditEvent::CachedGenerateAudit");
  582. }
  583. __except(hr = myHEXCEPTIONCODE(), EXCEPTION_EXECUTE_HANDLER)
  584. {
  585. _PrintError(hr, "Exception");
  586. }
  587. error:
  588. if (NULL != pwszUserName)
  589. {
  590. LocalFree(pwszUserName);
  591. }
  592. if (MAXDWORD != dwComContextIndex)
  593. {
  594. UnregisterComContext(&ComContext, dwComContextIndex);
  595. }
  596. CertSrvExitServer(State);
  597. CSASSERT(S_OK == hr || FAILED(hr));
  598. return(hr);
  599. }
  600. STDMETHODIMP
  601. CCertAdminD::ResubmitRequest(
  602. /* [unique][string][in] */ const wchar_t __RPC_FAR *pwszAuthority,
  603. /* [in] */ DWORD dwRequestId,
  604. /* [out] */ DWORD __RPC_FAR *pdwDisposition)
  605. {
  606. HRESULT hr;
  607. WCHAR *pwszUserName = NULL;
  608. CERTSRV_COM_CONTEXT ComContext;
  609. DWORD dwComContextIndex = MAXDWORD;
  610. CERTSRV_RESULT_CONTEXT Result;
  611. CAuditEvent audit(SE_AUDITID_CERTSRV_RESUBMITREQUEST, g_dwAuditFilter);
  612. DWORD State = 0;
  613. ZeroMemory(&ComContext, sizeof(ComContext));
  614. DBGPRINT((
  615. s_ssAdmin,
  616. "CCertAdminD::ResubmitRequest(tid=%d, this=%x)\n",
  617. GetCurrentThreadId(),
  618. this));
  619. hr = CertSrvEnterServer(&State);
  620. _JumpIfError(hr, error, "CertSrvEnterServer");
  621. hr = CheckAuthorityName(pwszAuthority);
  622. _JumpIfError(hr, error, "No authority name");
  623. hr = RegisterComContext(&ComContext, &dwComContextIndex);
  624. _JumpIfError(hr, error, "RegisterComContext");
  625. __try
  626. {
  627. hr = audit.AddData(dwRequestId); // %1 request ID
  628. _LeaveIfError(hr, "AddData");
  629. hr = audit.AccessCheck(
  630. CA_ACCESS_OFFICER,
  631. audit.m_gcNoAuditSuccess);
  632. _LeaveIfError(hr, "CAuditEvent::AccessCheck");
  633. hr = CheckOfficerRights(dwRequestId, audit);
  634. _LeaveIfError(hr, "CheckOfficerRights");
  635. hr = GetClientUserName(NULL, &pwszUserName, NULL);
  636. _LeaveIfError(hr, "GetClientUserName");
  637. ComContext.fInRequestGroup = MAXDWORD; // mark value invalid
  638. ZeroMemory(&Result, sizeof(Result));
  639. Result.pdwRequestId = &dwRequestId;
  640. Result.pdwDisposition = pdwDisposition;
  641. hr = CoreProcessRequest(
  642. CR_IN_RESUBMIT, // dwFlags
  643. pwszUserName, // pwszUserName
  644. 0, // cbRequest
  645. NULL, // pbRequest
  646. NULL, // pwszAttributes
  647. NULL, // pwszSerialNumber
  648. dwComContextIndex,
  649. dwRequestId,
  650. &Result);
  651. if (S_OK != hr)
  652. {
  653. hr = myHError(hr);
  654. _LeaveError(hr, "CoreProcessRequest");
  655. }
  656. hr = audit.CachedGenerateAudit();
  657. _LeaveIfError(hr, "CAuditEvent::CachedGenerateAudit");
  658. }
  659. __except(hr = myHEXCEPTIONCODE(), EXCEPTION_EXECUTE_HANDLER)
  660. {
  661. _PrintError(hr, "Exception");
  662. }
  663. error:
  664. if (NULL != pwszUserName)
  665. {
  666. LocalFree(pwszUserName);
  667. }
  668. if (NULL != ComContext.hAccessToken)
  669. {
  670. __try
  671. {
  672. CloseHandle(ComContext.hAccessToken);
  673. }
  674. __except(hr = myHEXCEPTIONCODE(), EXCEPTION_EXECUTE_HANDLER)
  675. {
  676. _PrintError(hr, "Exception");
  677. }
  678. }
  679. if (MAXDWORD != dwComContextIndex)
  680. {
  681. UnregisterComContext(&ComContext, dwComContextIndex);
  682. }
  683. CertSrvExitServer(State);
  684. CSASSERT(S_OK == hr || FAILED(hr));
  685. return(hr);
  686. }
  687. STDMETHODIMP
  688. CCertAdminD::EnumViewColumn(
  689. /* [ref][in] */ wchar_t const *pwszAuthority,
  690. /* [in] */ DWORD iColumn,
  691. /* [in] */ DWORD cColumn,
  692. /* [out] */ DWORD *pcColumn,
  693. /* [ref][out] */ CERTTRANSBLOB __RPC_FAR *pctbColumnInfo) // CoTaskMem*
  694. {
  695. HRESULT hr;
  696. hr = EnumViewColumnTable(
  697. pwszAuthority,
  698. CVRC_TABLE_REQCERT,
  699. iColumn,
  700. cColumn,
  701. pcColumn,
  702. pctbColumnInfo); // CoTaskMem*
  703. _JumpIfError(hr, error, "EnumViewColumnTable");
  704. error:
  705. CSASSERT(S_OK == hr || S_FALSE == hr || FAILED(hr));
  706. return(hr);
  707. }
  708. STDMETHODIMP
  709. CCertAdminD::EnumViewColumnTable(
  710. /* [ref][in] */ wchar_t const *pwszAuthority,
  711. /* [in] */ DWORD iTable,
  712. /* [in] */ DWORD iColumn,
  713. /* [in] */ DWORD cColumn,
  714. /* [out] */ DWORD *pcColumn,
  715. /* [ref][out] */ CERTTRANSBLOB __RPC_FAR *pctbColumnInfo) // CoTaskMem*
  716. {
  717. HRESULT hr;
  718. LONG iColumnCurrent;
  719. CERTDBCOLUMN *rgColumn = NULL;
  720. CERTDBCOLUMN *pColumn;
  721. CERTDBCOLUMN *pColumnEnd;
  722. CERTTRANSDBCOLUMN *rgtColumnOut = NULL;
  723. CERTTRANSDBCOLUMN *ptColumn;
  724. DWORD cColumnFetched;
  725. DWORD cb;
  726. DWORD State = 0;
  727. DBGPRINT((
  728. s_ssAdmin,
  729. "CCertAdminD::EnumViewColumnTable(tid=%d, this=%x, icol=%d, ccol=%d)\n",
  730. GetCurrentThreadId(),
  731. this,
  732. iColumn,
  733. cColumn));
  734. pctbColumnInfo->cb = 0;
  735. pctbColumnInfo->pb = NULL;
  736. hr = CertSrvEnterServer(&State);
  737. _JumpIfError(hr, error, "CertSrvEnterServer");
  738. hr = CheckAuthorityName(pwszAuthority);
  739. _JumpIfError(hr, error, "No authority name");
  740. __try
  741. {
  742. if (NULL == m_pEnumCol || iTable != m_iTableEnum)
  743. {
  744. if (NULL != m_pEnumCol)
  745. {
  746. m_pEnumCol->Release();
  747. m_pEnumCol = NULL;
  748. }
  749. hr = g_pCertDB->EnumCertDBColumn(iTable, &m_pEnumCol);
  750. _LeaveIfError(hr, "EnumCertDBColumn");
  751. m_iTableEnum = iTable;
  752. }
  753. rgColumn = (CERTDBCOLUMN *) LocalAlloc(
  754. LMEM_FIXED | LMEM_ZEROINIT,
  755. cColumn * sizeof(rgColumn[0]));
  756. if (NULL == rgColumn)
  757. {
  758. hr = E_OUTOFMEMORY;
  759. _LeaveError(hr, "Alloc rgColumn");
  760. }
  761. hr = m_pEnumCol->Skip(0, &iColumnCurrent);
  762. _LeaveIfError(hr, "Skip");
  763. if (iColumnCurrent != (LONG) iColumn)
  764. {
  765. hr = m_pEnumCol->Skip(
  766. (LONG) iColumn - iColumnCurrent,
  767. &iColumnCurrent);
  768. _LeaveIfError(hr, "Skip");
  769. CSASSERT((LONG) iColumn == iColumnCurrent);
  770. }
  771. hr = m_pEnumCol->Next(cColumn, rgColumn, &cColumnFetched);
  772. if (S_FALSE != hr)
  773. {
  774. _LeaveIfError(hr, "Next");
  775. }
  776. DBGPRINT((
  777. DBG_SS_CERTSRVI,
  778. "EnumViewColumnTable: cColumnFetched=%d\n",
  779. cColumnFetched));
  780. cb = cColumnFetched * sizeof(rgtColumnOut[0]);
  781. pColumnEnd = &rgColumn[cColumnFetched];
  782. for (pColumn = rgColumn; pColumn < pColumnEnd; pColumn++)
  783. {
  784. cb += DWORDROUND((wcslen(pColumn->pwszName) + 1) * sizeof(WCHAR));
  785. cb += DWORDROUND((wcslen(pColumn->pwszDisplayName) + 1) * sizeof(WCHAR));
  786. }
  787. rgtColumnOut = (CERTTRANSDBCOLUMN *) MIDL_user_allocate(cb);
  788. if (NULL == rgtColumnOut)
  789. {
  790. hr = E_OUTOFMEMORY;
  791. _LeaveError(hr, "MIDL_user_allocate rgtColumnOut");
  792. }
  793. ZeroMemory(rgtColumnOut, cb);
  794. pctbColumnInfo->cb = cb;
  795. cb = cColumnFetched * sizeof(rgtColumnOut[0]);
  796. pColumnEnd = &rgColumn[cColumnFetched];
  797. ptColumn = rgtColumnOut;
  798. for (pColumn = rgColumn; pColumn < pColumnEnd; ptColumn++, pColumn++)
  799. {
  800. DWORD cbT;
  801. ptColumn->Type = pColumn->Type;
  802. ptColumn->Index = pColumn->Index;
  803. ptColumn->cbMax = pColumn->cbMax;
  804. DBGPRINT((
  805. DBG_SS_CERTSRVI,
  806. "EnumViewColumnTable: ielt=%d idx=%x \"%ws\"\n",
  807. iColumn + (ptColumn - rgtColumnOut),
  808. ptColumn->Index,
  809. pColumn->pwszName));
  810. cbT = (wcslen(pColumn->pwszName) + 1) * sizeof(WCHAR);
  811. CopyMemory(Add2Ptr(rgtColumnOut, cb), pColumn->pwszName, cbT);
  812. ptColumn->obwszName = cb;
  813. cb += DWORDROUND(cbT);
  814. cbT = (wcslen(pColumn->pwszDisplayName) + 1) * sizeof(WCHAR);
  815. CopyMemory(Add2Ptr(rgtColumnOut, cb), pColumn->pwszDisplayName, cbT);
  816. ptColumn->obwszDisplayName = cb;
  817. cb += DWORDROUND(cbT);
  818. }
  819. CSASSERT(cb == pctbColumnInfo->cb);
  820. pctbColumnInfo->pb = (BYTE *) rgtColumnOut;
  821. rgtColumnOut = NULL;
  822. *pcColumn = cColumnFetched;
  823. myRegisterMemFree(pctbColumnInfo->pb, CSM_MIDLUSERALLOC);
  824. }
  825. __except(hr = myHEXCEPTIONCODE(), EXCEPTION_EXECUTE_HANDLER)
  826. {
  827. _PrintError(hr, "Exception");
  828. }
  829. error:
  830. if (NULL != rgColumn)
  831. {
  832. pColumnEnd = &rgColumn[cColumn];
  833. for (pColumn = rgColumn; pColumn < pColumnEnd; pColumn++)
  834. {
  835. if (NULL != pColumn->pwszName)
  836. {
  837. CoTaskMemFree(pColumn->pwszName);
  838. }
  839. if (NULL != pColumn->pwszDisplayName)
  840. {
  841. CoTaskMemFree(pColumn->pwszDisplayName);
  842. }
  843. }
  844. LocalFree(rgColumn);
  845. }
  846. if (NULL != rgtColumnOut)
  847. {
  848. MIDL_user_free(rgtColumnOut);
  849. }
  850. DBGPRINT((
  851. DBG_SS_CERTSRVI,
  852. "EnumViewColumnTable: icol=%d, ccol=%d, ccolout=%d, hr=%x\n",
  853. iColumn,
  854. cColumn,
  855. *pcColumn,
  856. hr));
  857. CertSrvExitServer(State);
  858. CSASSERT(S_OK == hr || S_FALSE == hr || FAILED(hr));
  859. return(hr);
  860. }
  861. HRESULT
  862. CCertAdminD::GetViewDefaultColumnSet(
  863. IN wchar_t const *pwszAuthority,
  864. IN DWORD iColumnSetDefault,
  865. OUT DWORD *pcColumn,
  866. OUT CERTTRANSBLOB *ptbColumnInfo) // CoTaskMem*
  867. {
  868. HRESULT hr;
  869. DWORD ccol;
  870. DWORD State = 0;
  871. DBGPRINT((
  872. s_ssAdmin,
  873. "CCertAdminD::GetViewDefaultColumnSet(tid=%d, this=%x, icolset=%d)\n",
  874. GetCurrentThreadId(),
  875. this,
  876. iColumnSetDefault));
  877. hr = CertSrvEnterServer(&State);
  878. _JumpIfError(hr, error, "CertSrvEnterServer");
  879. hr = CheckAuthorityName(pwszAuthority);
  880. _JumpIfError(hr, error, "No authority name");
  881. __try
  882. {
  883. {
  884. CAuditEvent audit(0, g_dwAuditFilter);
  885. hr = audit.AccessCheck(
  886. CA_ACCESS_ALLREADROLES,
  887. audit.m_gcNoAuditSuccess | audit.m_gcNoAuditFailure);
  888. _LeaveIfError(hr, "CAuditEvent::AccessCheck");
  889. }
  890. hr = g_pCertDB->GetDefaultColumnSet(iColumnSetDefault, 0, &ccol, NULL);
  891. _LeaveIfError(hr, "GetDefaultColumnSet");
  892. ptbColumnInfo->cb = ccol * sizeof(DWORD);
  893. ptbColumnInfo->pb = (BYTE *) MIDL_user_allocate(ptbColumnInfo->cb);
  894. if (NULL == ptbColumnInfo->pb)
  895. {
  896. hr = E_OUTOFMEMORY;
  897. _LeaveError(hr, "MIDL_user_allocate");
  898. }
  899. myRegisterMemFree(ptbColumnInfo->pb, CSM_MIDLUSERALLOC);
  900. hr = g_pCertDB->GetDefaultColumnSet(
  901. iColumnSetDefault,
  902. ccol,
  903. pcColumn,
  904. (DWORD *) ptbColumnInfo->pb);
  905. _LeaveIfError(hr, "GetDefaultColumnSet");
  906. }
  907. __except(hr = myHEXCEPTIONCODE(), EXCEPTION_EXECUTE_HANDLER)
  908. {
  909. _PrintError(hr, "Exception");
  910. }
  911. error:
  912. DBGPRINT((
  913. S_OK == hr? DBG_SS_CERTSRVI : DBG_SS_CERTSRV,
  914. "GetViewDefaultColumnSet: icolset=%d, ccolout=%d, hr=%x\n",
  915. iColumnSetDefault,
  916. *pcColumn,
  917. hr));
  918. CertSrvExitServer(State);
  919. CSASSERT(S_OK == hr || FAILED(hr));
  920. return(hr);
  921. }
  922. HRESULT
  923. CCertAdminD::_EnumAttributes(
  924. IN ICertDBRow *prow,
  925. IN CERTDBNAME *adbn,
  926. IN DWORD celt,
  927. OUT CERTTRANSBLOB *pctbOut) // CoTaskMem*
  928. {
  929. HRESULT hr;
  930. DWORD i;
  931. DWORD cb;
  932. DWORD cbT;
  933. CERTTRANSDBATTRIBUTE *pteltOut;
  934. BYTE *pbOut;
  935. BYTE *pbOutEnd;
  936. DWORD State = 0;
  937. CSASSERT(NULL == pctbOut->pb);
  938. hr = CertSrvEnterServer(&State);
  939. _JumpIfError(hr, error, "CertSrvEnterServer");
  940. cb = sizeof(*pteltOut) * celt;
  941. for (i = 0; i < celt; i++)
  942. {
  943. cb += (wcslen(adbn[i].pwszName) + 1) * sizeof(WCHAR);
  944. cb = DWORDROUND(cb);
  945. cbT = 0;
  946. hr = prow->GetProperty(
  947. adbn[i].pwszName,
  948. PROPTYPE_STRING |
  949. PROPCALLER_ADMIN |
  950. PROPTABLE_ATTRIBUTE,
  951. &cbT,
  952. NULL);
  953. _JumpIfError(hr, error, "GetProperty(NULL)");
  954. cb += DWORDROUND(cbT);
  955. }
  956. pctbOut->pb = (BYTE *) MIDL_user_allocate(cb);
  957. if (NULL == pctbOut->pb)
  958. {
  959. hr = E_OUTOFMEMORY;
  960. _JumpError(hr, error, "MIDL_user_allocate out data");
  961. }
  962. pctbOut->cb = cb;
  963. pteltOut = (CERTTRANSDBATTRIBUTE *) pctbOut->pb;
  964. pbOut = (BYTE *) &pteltOut[celt];
  965. pbOutEnd = &pctbOut->pb[pctbOut->cb];
  966. for (i = 0; i < celt; i++)
  967. {
  968. cbT = (wcslen(adbn[i].pwszName) + 1) * sizeof(WCHAR);
  969. CopyMemory(pbOut, adbn[i].pwszName, cbT);
  970. pteltOut->obwszName = SAFE_SUBTRACT_POINTERS(pbOut, pctbOut->pb);
  971. pbOut += DWORDROUND(cbT);
  972. cbT = SAFE_SUBTRACT_POINTERS(pbOutEnd, pbOut);
  973. hr = prow->GetProperty(
  974. adbn[i].pwszName,
  975. PROPTYPE_STRING |
  976. PROPCALLER_ADMIN |
  977. PROPTABLE_ATTRIBUTE,
  978. &cbT,
  979. pbOut);
  980. _JumpIfError(hr, error, "GetProperty(pbOut)");
  981. CSASSERT(wcslen((WCHAR const *) pbOut) * sizeof(WCHAR) == cbT);
  982. pteltOut->obwszValue = SAFE_SUBTRACT_POINTERS(pbOut, pctbOut->pb);
  983. pbOut += DWORDROUND(cbT + sizeof(WCHAR));
  984. pteltOut++;
  985. }
  986. CSASSERT(pbOut == pbOutEnd);
  987. hr = S_OK;
  988. error:
  989. if (S_OK != hr && NULL != pctbOut->pb)
  990. {
  991. MIDL_user_free(pctbOut->pb);
  992. pctbOut->pb = NULL;
  993. }
  994. CertSrvExitServer(State);
  995. return(hr);
  996. }
  997. HRESULT
  998. CCertAdminD::_EnumExtensions(
  999. IN ICertDBRow *prow,
  1000. IN CERTDBNAME *adbn,
  1001. IN DWORD celt,
  1002. OUT CERTTRANSBLOB *pctbOut) // CoTaskMem*
  1003. {
  1004. HRESULT hr;
  1005. DWORD i;
  1006. DWORD cb;
  1007. DWORD cbT;
  1008. DWORD ExtFlags;
  1009. CERTTRANSDBEXTENSION *pteltOut;
  1010. BYTE *pbOut;
  1011. BYTE *pbOutEnd;
  1012. DWORD State = 0;
  1013. CSASSERT(NULL == pctbOut->pb);
  1014. hr = CertSrvEnterServer(&State);
  1015. _JumpIfError(hr, error, "CertSrvEnterServer");
  1016. cb = sizeof(*pteltOut) * celt;
  1017. for (i = 0; i < celt; i++)
  1018. {
  1019. cb += (wcslen(adbn[i].pwszName) + 1) * sizeof(WCHAR);
  1020. cb = DWORDROUND(cb);
  1021. cbT = 0;
  1022. hr = prow->GetExtension(
  1023. adbn[i].pwszName,
  1024. &ExtFlags,
  1025. &cbT,
  1026. NULL);
  1027. _JumpIfError(hr, error, "GetExtension(NULL)");
  1028. cb += DWORDROUND(cbT);
  1029. }
  1030. pctbOut->pb = (BYTE *) MIDL_user_allocate(cb);
  1031. if (NULL == pctbOut->pb)
  1032. {
  1033. hr = E_OUTOFMEMORY;
  1034. _JumpError(hr, error, "MIDL_user_allocate out data");
  1035. }
  1036. pctbOut->cb = cb;
  1037. pteltOut = (CERTTRANSDBEXTENSION *) pctbOut->pb;
  1038. pbOut = (BYTE *) &pteltOut[celt];
  1039. pbOutEnd = &pctbOut->pb[pctbOut->cb];
  1040. for (i = 0; i < celt; i++)
  1041. {
  1042. cbT = (wcslen(adbn[i].pwszName) + 1) * sizeof(WCHAR);
  1043. CopyMemory(pbOut, adbn[i].pwszName, cbT);
  1044. pteltOut->obwszName = SAFE_SUBTRACT_POINTERS(pbOut, pctbOut->pb);
  1045. pbOut += DWORDROUND(cbT);
  1046. cbT = SAFE_SUBTRACT_POINTERS(pbOutEnd, pbOut);
  1047. hr = prow->GetExtension(
  1048. adbn[i].pwszName,
  1049. (DWORD *) &pteltOut->ExtFlags,
  1050. &cbT,
  1051. pbOut);
  1052. _JumpIfError(hr, error, "GetExtension(pbOut)");
  1053. pteltOut->cbValue = cbT;
  1054. pteltOut->obValue = SAFE_SUBTRACT_POINTERS(pbOut, pctbOut->pb);
  1055. pbOut += DWORDROUND(cbT);
  1056. pteltOut++;
  1057. }
  1058. CSASSERT(pbOut == pbOutEnd);
  1059. hr = S_OK;
  1060. error:
  1061. if (S_OK != hr && NULL != pctbOut->pb)
  1062. {
  1063. MIDL_user_free(pctbOut->pb);
  1064. pctbOut->pb = NULL;
  1065. }
  1066. CertSrvExitServer(State);
  1067. return(hr);
  1068. }
  1069. STDMETHODIMP
  1070. CCertAdminD::EnumAttributesOrExtensions(
  1071. IN wchar_t const *pwszAuthority,
  1072. IN DWORD RowId,
  1073. IN DWORD Flags,
  1074. OPTIONAL IN wchar_t const *pwszLast,
  1075. IN DWORD celt,
  1076. OUT DWORD *pceltFetched,
  1077. OUT CERTTRANSBLOB *pctbOut) // CoTaskMem*
  1078. {
  1079. HRESULT hr;
  1080. ICertDBRow *prow = NULL;
  1081. IEnumCERTDBNAME *penum = NULL;
  1082. DWORD EnumFlags;
  1083. CERTDBNAME *adbn = NULL;
  1084. DWORD celtFetched;
  1085. DWORD i;
  1086. DWORD j;
  1087. DWORD State = 0;
  1088. DBGPRINT((
  1089. s_ssAdmin,
  1090. "CCertAdminD::EnumAttributesOrExtensions(tid=%d, this=%x)\n",
  1091. GetCurrentThreadId(),
  1092. this));
  1093. hr = CertSrvEnterServer(&State);
  1094. _JumpIfError(hr, error, "CertSrvEnterServer");
  1095. hr = CheckAuthorityName(pwszAuthority);
  1096. _JumpIfError(hr, error, "No authority name");
  1097. DBGPRINT((
  1098. DBG_SS_CERTSRVI,
  1099. "EnumAttributesOrExtensions(row=%d, flags=0x%x, last=%ws, celt=%d)\n",
  1100. RowId,
  1101. Flags,
  1102. pwszLast,
  1103. celt));
  1104. __try
  1105. {
  1106. pctbOut->pb = NULL;
  1107. {
  1108. CAuditEvent audit(0, g_dwAuditFilter);
  1109. hr = audit.AccessCheck(
  1110. CA_ACCESS_ALLREADROLES,
  1111. audit.m_gcNoAuditSuccess | audit.m_gcNoAuditFailure);
  1112. _LeaveIfError(hr, "CAuditEvent::AccessCheck");
  1113. }
  1114. if (0 >= RowId)
  1115. {
  1116. hr = E_INVALIDARG;
  1117. _LeaveError(hr, "RowId");
  1118. }
  1119. switch (Flags)
  1120. {
  1121. case CDBENUM_ATTRIBUTES:
  1122. EnumFlags = CIE_TABLE_ATTRIBUTES;
  1123. break;
  1124. case CDBENUM_EXTENSIONS:
  1125. EnumFlags = CIE_TABLE_EXTENSIONS;
  1126. break;
  1127. default:
  1128. hr = E_INVALIDARG;
  1129. _LeaveError(hr, "Flags");
  1130. }
  1131. hr = g_pCertDB->OpenRow(
  1132. PROPOPEN_READONLY | PROPTABLE_REQCERT,
  1133. RowId,
  1134. NULL,
  1135. &prow);
  1136. _LeaveIfError(hr, "OpenRow(RowId)");
  1137. hr = prow->EnumCertDBName(EnumFlags, &penum);
  1138. _LeaveIfError(hr, "EnumCertDBName");
  1139. adbn = (CERTDBNAME *) LocalAlloc(
  1140. LMEM_FIXED | LMEM_ZEROINIT,
  1141. sizeof(adbn[0]) * celt);
  1142. if (NULL == adbn)
  1143. {
  1144. hr = E_OUTOFMEMORY;
  1145. _LeaveError(hr, "Alloc string pointers");
  1146. }
  1147. // If specified, skip entries up to and including the last key.
  1148. if (NULL != pwszLast)
  1149. {
  1150. int r;
  1151. do
  1152. {
  1153. hr = penum->Next(1, &adbn[0], &celtFetched);
  1154. if (S_FALSE == hr)
  1155. {
  1156. hr = E_INVALIDARG;
  1157. _PrintError(hr, "pwszLast missing");
  1158. }
  1159. _LeaveIfError(hr, "Next");
  1160. r = lstrcmpi(pwszLast, adbn[0].pwszName);
  1161. LocalFree(adbn[0].pwszName);
  1162. adbn[0].pwszName = NULL;
  1163. } while (0 != r);
  1164. }
  1165. hr = penum->Next(celt, adbn, &celtFetched);
  1166. if (S_FALSE != hr)
  1167. {
  1168. _LeaveIfError(hr, "Next");
  1169. }
  1170. if (CIE_TABLE_ATTRIBUTES == EnumFlags)
  1171. {
  1172. hr = _EnumAttributes(prow, adbn, celtFetched, pctbOut);
  1173. _LeaveIfError(hr, "_EnumAttributes");
  1174. }
  1175. else
  1176. {
  1177. hr = _EnumExtensions(prow, adbn, celtFetched, pctbOut);
  1178. _LeaveIfError(hr, "_EnumExtensions");
  1179. }
  1180. myRegisterMemFree(pctbOut->pb, CSM_MIDLUSERALLOC);
  1181. *pceltFetched = celtFetched;
  1182. hr = S_OK;
  1183. if (celt > celtFetched)
  1184. {
  1185. hr = S_FALSE;
  1186. }
  1187. }
  1188. __except(hr = myHEXCEPTIONCODE(), EXCEPTION_EXECUTE_HANDLER)
  1189. {
  1190. _PrintError(hr, "Exception");
  1191. }
  1192. error:
  1193. if (NULL != adbn)
  1194. {
  1195. for (i = 0; i < celt; i++)
  1196. {
  1197. if (NULL != adbn[i].pwszName)
  1198. {
  1199. MIDL_user_free(adbn[i].pwszName);
  1200. }
  1201. }
  1202. LocalFree(adbn);
  1203. }
  1204. if (NULL != penum)
  1205. {
  1206. penum->Release();
  1207. }
  1208. if (NULL != prow)
  1209. {
  1210. prow->Release();
  1211. }
  1212. DBGPRINT((
  1213. DBG_SS_CERTSRVI,
  1214. "EnumAttributesOrExtensions: celtFetched=%d, hr=%x\n",
  1215. *pceltFetched,
  1216. hr));
  1217. CertSrvExitServer(State);
  1218. CSASSERT(S_OK == hr || S_FALSE == hr || FAILED(hr));
  1219. return(hr);
  1220. }
  1221. STDMETHODIMP
  1222. CCertAdminD::OpenView(
  1223. IN wchar_t const *pwszAuthority,
  1224. IN DWORD ccvr,
  1225. IN CERTVIEWRESTRICTION const *acvr,
  1226. IN DWORD ccolOut,
  1227. IN DWORD const *acolOut,
  1228. IN DWORD ielt,
  1229. IN DWORD celt,
  1230. OUT DWORD *pceltFetched,
  1231. OUT CERTTRANSBLOB *pctbResultRows) // CoTaskMem*
  1232. {
  1233. HRESULT hr;
  1234. IEnumCERTDBRESULTROW *pview = NULL;
  1235. DWORD State = 0;
  1236. DBGPRINT((
  1237. s_ssAdmin,
  1238. "CCertAdminD::OpenView(tid=%d, this=%x)\n",
  1239. GetCurrentThreadId(),
  1240. this));
  1241. hr = CertSrvEnterServer(&State);
  1242. _JumpIfError(hr, error, "CertSrvEnterServer");
  1243. hr = CheckAuthorityName(pwszAuthority);
  1244. _JumpIfError(hr, error, "No authority name");
  1245. DBGPRINT((
  1246. DBG_SS_CERTSRVI,
  1247. "================================================================\n"));
  1248. DBGPRINT((
  1249. DBG_SS_CERTSRVI,
  1250. "OpenView(ccvr=%d, ccolOut=%d, celt=%d)\n",
  1251. ccvr,
  1252. ccolOut,
  1253. celt));
  1254. __try
  1255. {
  1256. pctbResultRows->pb = NULL;
  1257. {
  1258. CAuditEvent audit(0, g_dwAuditFilter);
  1259. hr = audit.AccessCheck(
  1260. CA_ACCESS_ALLREADROLES,
  1261. audit.m_gcNoAuditSuccess | audit.m_gcNoAuditFailure);
  1262. _LeaveIfError(hr, "CAuditEvent::AccessCheck");
  1263. }
  1264. if (NULL != m_pView)
  1265. {
  1266. hr = E_UNEXPECTED;
  1267. _LeaveError(hr, "Has View");
  1268. }
  1269. hr = g_pCertDB->OpenView(
  1270. ccvr,
  1271. acvr,
  1272. ccolOut,
  1273. acolOut,
  1274. CDBOPENVIEW_WORKERTHREAD,
  1275. &pview);
  1276. _LeaveIfError(hr, "OpenView");
  1277. hr = _EnumViewNext(pview, ielt, celt, pceltFetched, pctbResultRows);
  1278. if (S_FALSE != hr)
  1279. {
  1280. _LeaveIfError(hr, "_EnumViewNext");
  1281. }
  1282. m_pView = pview;
  1283. pview = NULL;
  1284. myRegisterMemFree(pctbResultRows->pb, CSM_MIDLUSERALLOC);
  1285. }
  1286. __except(hr = myHEXCEPTIONCODE(), EXCEPTION_EXECUTE_HANDLER)
  1287. {
  1288. _PrintError(hr, "Exception");
  1289. }
  1290. error:
  1291. if (NULL != pview)
  1292. {
  1293. pview->Release();
  1294. }
  1295. DBGPRINT((
  1296. DBG_SS_CERTSRVI,
  1297. "OpenView: celtFetched=%d, hr=%x\n",
  1298. *pceltFetched,
  1299. hr));
  1300. CertSrvExitServer(State);
  1301. CSASSERT(S_OK == hr || S_FALSE == hr || FAILED(hr));
  1302. return(hr);
  1303. }
  1304. STDMETHODIMP
  1305. CCertAdminD::EnumView(
  1306. IN wchar_t const *pwszAuthority,
  1307. IN DWORD ielt,
  1308. IN DWORD celt,
  1309. OUT DWORD *pceltFetched,
  1310. OUT CERTTRANSBLOB *pctbResultRows) // CoTaskMem*
  1311. {
  1312. HRESULT hr;
  1313. DWORD State = 0;
  1314. DBGPRINT((
  1315. s_ssAdmin,
  1316. "CCertAdminD::EnumView(tid=%d, this=%x)\n",
  1317. GetCurrentThreadId(),
  1318. this));
  1319. hr = CertSrvEnterServer(&State);
  1320. _JumpIfError(hr, error, "CertSrvEnterServer");
  1321. hr = CheckAuthorityName(pwszAuthority);
  1322. _JumpIfError(hr, error, "No authority name");
  1323. DBGPRINT((DBG_SS_CERTSRVI, "EnumView(ielt=%d, celt=%d)\n", ielt, celt));
  1324. __try
  1325. {
  1326. if (NULL == m_pView)
  1327. {
  1328. hr = E_UNEXPECTED;
  1329. _LeaveError(hr, "No View");
  1330. }
  1331. hr = _EnumViewNext(
  1332. m_pView,
  1333. ielt,
  1334. celt,
  1335. pceltFetched,
  1336. pctbResultRows);
  1337. if (S_FALSE != hr)
  1338. {
  1339. _LeaveIfError(hr, "_EnumViewNext");
  1340. }
  1341. myRegisterMemFree(pctbResultRows->pb, CSM_MIDLUSERALLOC);
  1342. }
  1343. __except(hr = myHEXCEPTIONCODE(), EXCEPTION_EXECUTE_HANDLER)
  1344. {
  1345. _PrintError(hr, "Exception");
  1346. }
  1347. error:
  1348. DBGPRINT((
  1349. DBG_SS_CERTSRVI,
  1350. "EnumView: celtFetched=%d, hr=%x\n",
  1351. *pceltFetched,
  1352. hr));
  1353. CertSrvExitServer(State);
  1354. CSASSERT(S_OK == hr || S_FALSE == hr || FAILED(hr));
  1355. return(hr);
  1356. }
  1357. HRESULT
  1358. CCertAdminD::_EnumViewNext(
  1359. IN IEnumCERTDBRESULTROW *pview,
  1360. IN DWORD ielt,
  1361. IN DWORD celt,
  1362. OUT DWORD *pceltFetched,
  1363. OUT CERTTRANSBLOB *pctbResultRows) // CoTaskMem
  1364. {
  1365. HRESULT hr;
  1366. BOOL fNoMore = FALSE;
  1367. BOOL fFetched = FALSE;
  1368. DWORD cb;
  1369. DWORD cbT;
  1370. DWORD cColTotal;
  1371. CERTDBRESULTROW *aelt = NULL;
  1372. CERTDBRESULTROW *pelt;
  1373. CERTDBRESULTROW *peltEnd;
  1374. CERTDBRESULTCOLUMN *pcol;
  1375. CERTDBRESULTCOLUMN *pcolEnd;
  1376. CERTTRANSDBRESULTROW *pteltOut;
  1377. CERTTRANSDBRESULTCOLUMN *ptcolOut;
  1378. BYTE *pbOut;
  1379. DWORD ieltLast;
  1380. DWORD State = 0;
  1381. if(1<InterlockedIncrement(&m_cNext))
  1382. {
  1383. hr = E_UNEXPECTED;
  1384. _JumpError(hr, error, "Calls from multiple threads on the same view object");
  1385. }
  1386. hr = CertSrvEnterServer(&State);
  1387. _JumpIfError(hr, error, "CertSrvEnterServer");
  1388. DBGPRINT((DBG_SS_CERTSRVI, "_EnumViewNext(ielt=%d celt=%d)\n", ielt, celt));
  1389. aelt = (CERTDBRESULTROW *) LocalAlloc(LMEM_FIXED, celt * sizeof(aelt[0]));
  1390. if (NULL == aelt)
  1391. {
  1392. hr = E_OUTOFMEMORY;
  1393. _JumpError(hr, error, "Alloc result rows");
  1394. }
  1395. hr = pview->Skip(0, (LONG *) &ieltLast);
  1396. _JumpIfError(hr, error, "Skip");
  1397. if (ielt != ieltLast + 1)
  1398. {
  1399. DBGPRINT((
  1400. DBG_SS_CERTSRVI, "_EnumViewNext! ieltLast=%d cskip=%d\n",
  1401. ieltLast,
  1402. ielt - ieltLast));
  1403. hr = pview->Skip(ielt - (ieltLast + 1), (LONG *) &ieltLast);
  1404. _JumpIfError(hr, error, "Skip");
  1405. DBGPRINT((
  1406. DBG_SS_CERTSRVI, "_EnumViewNext! ielt after skip=%d\n",
  1407. ieltLast));
  1408. }
  1409. hr = pview->Next(celt, aelt, pceltFetched);
  1410. if (S_FALSE == hr)
  1411. {
  1412. fNoMore = TRUE;
  1413. }
  1414. else
  1415. {
  1416. _JumpIfError(hr, error, "Next");
  1417. }
  1418. fFetched = TRUE;
  1419. DBGPRINT((
  1420. DBG_SS_CERTSRVI,
  1421. "_EnumViewNext! celtFetched=%d\n",
  1422. *pceltFetched));
  1423. cb = *pceltFetched * sizeof(*pteltOut);
  1424. if (fNoMore)
  1425. {
  1426. cb += sizeof(*pteltOut);
  1427. }
  1428. cColTotal = 0;
  1429. peltEnd = &aelt[*pceltFetched];
  1430. for (pelt = aelt; pelt < peltEnd; pelt++)
  1431. {
  1432. cColTotal += pelt->ccol;
  1433. cb += pelt->ccol * sizeof(*ptcolOut);
  1434. pcolEnd = &pelt->acol[pelt->ccol];
  1435. for (pcol = pelt->acol; pcol < pcolEnd; pcol++)
  1436. {
  1437. CSASSERT(DWORDROUND(cb) == cb);
  1438. if (NULL != pcol->pbValue)
  1439. {
  1440. if ((DTI_REQUESTTABLE | DTR_REQUESTRAWARCHIVEDKEY) ==
  1441. pcol->Index)
  1442. {
  1443. cb += sizeof(DWORD);
  1444. }
  1445. else
  1446. {
  1447. cb += DWORDROUND(pcol->cbValue);
  1448. }
  1449. }
  1450. }
  1451. }
  1452. pctbResultRows->pb = (BYTE *) MIDL_user_allocate(cb);
  1453. if (NULL == pctbResultRows->pb)
  1454. {
  1455. hr = E_OUTOFMEMORY;
  1456. _JumpError(hr, error, "MIDL_user_allocate result rows");
  1457. }
  1458. pctbResultRows->cb = cb;
  1459. ZeroMemory(pctbResultRows->pb, pctbResultRows->cb);
  1460. pbOut = pctbResultRows->pb;
  1461. DBGPRINT((
  1462. DBG_SS_CERTSRVI,
  1463. "_EnumViewNext! Result Row data cb=0x%x @%x\n",
  1464. pctbResultRows->cb,
  1465. pctbResultRows->pb));
  1466. for (pelt = aelt; pelt < peltEnd; pelt++)
  1467. {
  1468. pteltOut = (CERTTRANSDBRESULTROW *) pbOut;
  1469. pbOut += sizeof(*pteltOut);
  1470. ptcolOut = (CERTTRANSDBRESULTCOLUMN *) pbOut;
  1471. pbOut += pelt->ccol * sizeof(*ptcolOut);
  1472. pteltOut->rowid = pelt->rowid;
  1473. pteltOut->ccol = pelt->ccol;
  1474. pcolEnd = &pelt->acol[pelt->ccol];
  1475. for (pcol = pelt->acol; pcol < pcolEnd; pcol++, ptcolOut++)
  1476. {
  1477. ptcolOut->Type = pcol->Type;
  1478. ptcolOut->Index = pcol->Index;
  1479. if (NULL != pcol->pbValue)
  1480. {
  1481. if ((DTI_REQUESTTABLE | DTR_REQUESTRAWARCHIVEDKEY) ==
  1482. ptcolOut->Index)
  1483. {
  1484. cbT = sizeof(BYTE);
  1485. CSASSERT(0 == *(DWORD *) pbOut);
  1486. }
  1487. else
  1488. {
  1489. cbT = pcol->cbValue;
  1490. CopyMemory(pbOut, pcol->pbValue, cbT);
  1491. }
  1492. ptcolOut->cbValue = cbT;
  1493. ptcolOut->obValue = SAFE_SUBTRACT_POINTERS(pbOut, (BYTE *) pteltOut);
  1494. pbOut += DWORDROUND(cbT);
  1495. }
  1496. }
  1497. pteltOut->cbrow = SAFE_SUBTRACT_POINTERS(pbOut, (BYTE *) pteltOut);
  1498. }
  1499. // if past the end or at end of rowset, write an extra record containimg
  1500. // the maximum element count.
  1501. if (fNoMore)
  1502. {
  1503. pteltOut = (CERTTRANSDBRESULTROW *) pbOut;
  1504. pbOut += sizeof(*pteltOut);
  1505. pteltOut->rowid = pelt->rowid;
  1506. pteltOut->ccol = pelt->ccol;
  1507. pteltOut->cbrow = SAFE_SUBTRACT_POINTERS(pbOut, (BYTE *) pteltOut);
  1508. CSASSERT(pteltOut->rowid == ~pteltOut->ccol);
  1509. DBGPRINT((
  1510. DBG_SS_CERTSRVI,
  1511. "_EnumViewNext! celtMax=%d\n",
  1512. pteltOut->rowid));
  1513. }
  1514. DBGPRINT((
  1515. DBG_SS_CERTSRVI,
  1516. "_EnumViewNext! pbOut=%x/%x\n",
  1517. pbOut,
  1518. &pctbResultRows->pb[pctbResultRows->cb]));
  1519. CSASSERT(&pctbResultRows->pb[pctbResultRows->cb] == pbOut);
  1520. if (fNoMore)
  1521. {
  1522. hr = S_FALSE;
  1523. }
  1524. error:
  1525. DBGPRINT((
  1526. DBG_SS_CERTSRVI,
  1527. "_EnumViewNext: celtFetched=%d, hr=%x\n",
  1528. *pceltFetched,
  1529. hr));
  1530. if (fFetched)
  1531. {
  1532. HRESULT hr2;
  1533. hr2 = pview->ReleaseResultRow(*pceltFetched, aelt);
  1534. _PrintIfError(hr2, "ReleaseResultRow");
  1535. }
  1536. if (NULL != aelt)
  1537. {
  1538. LocalFree(aelt);
  1539. }
  1540. CertSrvExitServer(State);
  1541. InterlockedDecrement(&m_cNext);
  1542. return(hr);
  1543. }
  1544. STDMETHODIMP
  1545. CCertAdminD::CloseView(
  1546. IN wchar_t const *pwszAuthority)
  1547. {
  1548. HRESULT hr;
  1549. DWORD State = 0;
  1550. DBGPRINT((
  1551. s_ssAdmin,
  1552. "CCertAdminD::CloseView(tid=%d, this=%x)\n",
  1553. GetCurrentThreadId(),
  1554. this));
  1555. hr = CertSrvEnterServer(&State);
  1556. _JumpIfError(hr, error, "CertSrvEnterServer");
  1557. hr = CheckAuthorityName(pwszAuthority);
  1558. _JumpIfError(hr, error, "No authority name");
  1559. __try
  1560. {
  1561. if (NULL == m_pView)
  1562. {
  1563. hr = E_UNEXPECTED;
  1564. _LeaveError(hr, "No View");
  1565. }
  1566. m_pView->Release();
  1567. m_pView = NULL;
  1568. hr = S_OK;
  1569. }
  1570. __except(hr = myHEXCEPTIONCODE(), EXCEPTION_EXECUTE_HANDLER)
  1571. {
  1572. _PrintError(hr, "Exception");
  1573. }
  1574. error:
  1575. CertSrvExitServer(State);
  1576. CSASSERT(S_OK == hr || FAILED(hr));
  1577. return(hr);
  1578. }
  1579. STDMETHODIMP
  1580. CCertAdminD::RevokeCertificate(
  1581. /* [unique][in] */ USHORT const __RPC_FAR *pwszAuthority,
  1582. /* [in, string, unique] */ USHORT const __RPC_FAR *pwszSerialNumber,
  1583. /* [in] */ DWORD Reason,
  1584. /* [in] */ FILETIME FileTime)
  1585. {
  1586. HRESULT hr;
  1587. DWORD ReqId;
  1588. DWORD cbProp;
  1589. DWORD Disposition;
  1590. DWORD OldReason;
  1591. ICertDBRow *prow = NULL;
  1592. WCHAR const *pwszDisposition = NULL;
  1593. WCHAR const *pwszDispT;
  1594. BOOL fUnRevoke = FALSE;
  1595. BOOL fRevokeOnHold = FALSE;
  1596. WCHAR *pwszUserName = NULL;
  1597. CAuditEvent audit(SE_AUDITID_CERTSRV_REVOKECERT, g_dwAuditFilter);
  1598. LPWSTR pwszRequesterName = NULL;
  1599. DWORD State = 0;
  1600. BOOL fCommitted = FALSE;
  1601. DBGPRINT((
  1602. s_ssAdmin,
  1603. "CCertAdminD::RevokeCertificate(tid=%d, this=%x)\n",
  1604. GetCurrentThreadId(),
  1605. this));
  1606. hr = CertSrvEnterServer(&State);
  1607. _JumpIfError(hr, error, "CertSrvEnterServer");
  1608. hr = CheckAuthorityName(pwszAuthority);
  1609. _JumpIfError(hr, error, "No authority name");
  1610. __try
  1611. {
  1612. hr = audit.AddData(pwszSerialNumber); // %1 serial no.
  1613. _LeaveIfError(hr, "CAuditEvent::AddData");
  1614. hr = audit.AddData(Reason); // %2 reason
  1615. _LeaveIfError(hr, "CAuditEvent::AddData");
  1616. hr = audit.AccessCheck(
  1617. CA_ACCESS_OFFICER,
  1618. audit.m_gcNoAuditSuccess);
  1619. _LeaveIfError(hr, "CAuditEvent::AccessCheck");
  1620. switch (Reason)
  1621. {
  1622. case MAXDWORD:
  1623. fUnRevoke = TRUE;
  1624. break;
  1625. case CRL_REASON_CERTIFICATE_HOLD:
  1626. fRevokeOnHold = TRUE;
  1627. break;
  1628. case CRL_REASON_UNSPECIFIED:
  1629. case CRL_REASON_KEY_COMPROMISE:
  1630. case CRL_REASON_CA_COMPROMISE:
  1631. case CRL_REASON_AFFILIATION_CHANGED:
  1632. case CRL_REASON_SUPERSEDED:
  1633. case CRL_REASON_CESSATION_OF_OPERATION:
  1634. case CRL_REASON_REMOVE_FROM_CRL:
  1635. break;
  1636. default:
  1637. hr = E_INVALIDARG;
  1638. _LeaveError(hr, "Reason parameter");
  1639. }
  1640. hr = g_pCertDB->OpenRow(PROPTABLE_REQCERT, 0, pwszSerialNumber, &prow);
  1641. if (S_OK != hr)
  1642. {
  1643. if (CERTSRV_E_PROPERTY_EMPTY == hr)
  1644. {
  1645. hr = E_INVALIDARG; // Invalid Serial Number
  1646. }
  1647. _LeaveErrorStr(hr, "OpenRow", pwszSerialNumber);
  1648. }
  1649. hr = PKCSGetProperty(
  1650. prow,
  1651. g_wszPropRequesterName,
  1652. PROPTYPE_STRING | PROPCALLER_SERVER | PROPTABLE_REQUEST,
  1653. NULL,
  1654. (BYTE **) &pwszRequesterName);
  1655. if (CERTSRV_E_PROPERTY_EMPTY != hr)
  1656. {
  1657. _LeaveIfErrorStr(hr, "PKCSGetProperty", g_wszPropRequesterName);
  1658. }
  1659. hr = CheckOfficerRights(pwszRequesterName, audit);
  1660. _LeaveIfError(hr, "CheckOfficerRights");
  1661. cbProp = sizeof(Disposition);
  1662. hr = prow->GetProperty(
  1663. g_wszPropRequestDisposition,
  1664. PROPTYPE_LONG | PROPCALLER_SERVER | PROPTABLE_REQUEST,
  1665. &cbProp,
  1666. (BYTE *) &Disposition);
  1667. _LeaveIfError(hr, "GetProperty");
  1668. hr = HRESULT_FROM_WIN32(ERROR_INVALID_DATA);
  1669. switch (Disposition)
  1670. {
  1671. HRESULT hr2;
  1672. case DB_DISP_CA_CERT:
  1673. if (!IsRootCA(g_CAType))
  1674. {
  1675. _LeaveError(hr, "non-root CA");
  1676. }
  1677. // FALLTHROUGH
  1678. case DB_DISP_ISSUED:
  1679. case DB_DISP_REVOKED:
  1680. cbProp = sizeof(OldReason);
  1681. hr2 = prow->GetProperty(
  1682. g_wszPropRequestRevokedReason,
  1683. PROPTYPE_LONG | PROPCALLER_SERVER | PROPTABLE_REQUEST,
  1684. &cbProp,
  1685. (BYTE *) &OldReason);
  1686. // Converted MDB databases have UNrevoked rows' RevokedReason
  1687. // column set to zero (CRL_REASON_UNSPECIFIED).
  1688. if (S_OK != hr2 ||
  1689. (DB_DISP_ISSUED == Disposition &&
  1690. CRL_REASON_UNSPECIFIED == OldReason))
  1691. {
  1692. OldReason = MAXDWORD;
  1693. }
  1694. if (fRevokeOnHold &&
  1695. MAXDWORD != OldReason &&
  1696. CRL_REASON_CERTIFICATE_HOLD != OldReason)
  1697. {
  1698. _LeaveError(hr, "already revoked: not on hold");
  1699. }
  1700. if (fUnRevoke && CRL_REASON_CERTIFICATE_HOLD != OldReason)
  1701. {
  1702. _LeaveError(hr, "unrevoke: not on hold");
  1703. }
  1704. break;
  1705. default:
  1706. _LeaveError(hr, "invalid disposition");
  1707. }
  1708. hr = PropSetRequestTimeProperty(prow, g_wszPropRequestRevokedWhen);
  1709. if (S_OK != hr)
  1710. {
  1711. hr = myHError(hr);
  1712. _LeaveError(hr, "PropSetRequestTimeProperty");
  1713. }
  1714. hr = prow->SetProperty(
  1715. g_wszPropRequestRevokedEffectiveWhen,
  1716. PROPTYPE_DATE | PROPCALLER_SERVER | PROPTABLE_REQUEST,
  1717. sizeof(FileTime),
  1718. (BYTE const *) &FileTime);
  1719. _LeaveIfError(hr, "SetProperty");
  1720. hr = prow->SetProperty(
  1721. g_wszPropRequestRevokedReason,
  1722. PROPTYPE_LONG | PROPCALLER_SERVER | PROPTABLE_REQUEST,
  1723. sizeof(Reason),
  1724. (BYTE const *) &Reason);
  1725. _LeaveIfError(hr, "SetProperty");
  1726. hr = GetClientUserName(NULL, &pwszUserName, NULL);
  1727. _LeaveIfError(hr, "GetClientUserName");
  1728. pwszDispT = fUnRevoke? g_pwszUnrevokedBy : g_pwszRevokedBy;
  1729. pwszDisposition = CoreBuildDispositionString(
  1730. pwszDispT,
  1731. pwszUserName,
  1732. NULL,
  1733. NULL,
  1734. S_OK,
  1735. FALSE);
  1736. if (NULL == pwszDisposition)
  1737. {
  1738. pwszDisposition = pwszDispT;
  1739. }
  1740. hr = prow->SetProperty(
  1741. g_wszPropRequestDispositionMessage,
  1742. PROPTYPE_STRING | PROPCALLER_SERVER | PROPTABLE_REQUEST,
  1743. MAXDWORD,
  1744. (BYTE const *) pwszDisposition);
  1745. _LeaveIfError(hr, "SetProperty");
  1746. if (DB_DISP_CA_CERT != Disposition)
  1747. {
  1748. Disposition = fUnRevoke? DB_DISP_ISSUED : DB_DISP_REVOKED;
  1749. hr = prow->SetProperty(
  1750. g_wszPropRequestDisposition,
  1751. PROPTYPE_LONG | PROPCALLER_SERVER | PROPTABLE_REQUEST,
  1752. sizeof(Disposition),
  1753. (BYTE const *) &Disposition);
  1754. _LeaveIfError(hr, "SetProperty");
  1755. }
  1756. hr = prow->CommitTransaction(TRUE);
  1757. _LeaveIfError(hr, "CommitTransaction");
  1758. fCommitted = TRUE;
  1759. hr = audit.CachedGenerateAudit();
  1760. _LeaveIfError(hr, "CAuditEvent::CachedGenerateAudit");
  1761. prow->GetRowId(&ReqId);
  1762. ExitNotify(EXITEVENT_CERTREVOKED, ReqId, MAXDWORD);
  1763. CoreLogRequestStatus(
  1764. prow,
  1765. MSG_DN_CERT_REVOKED,
  1766. hr,
  1767. pwszDisposition);
  1768. }
  1769. __except(hr = myHEXCEPTIONCODE(), EXCEPTION_EXECUTE_HANDLER)
  1770. {
  1771. _PrintError(hr, "Exception");
  1772. }
  1773. error:
  1774. if (NULL != pwszUserName)
  1775. {
  1776. LocalFree(pwszUserName);
  1777. }
  1778. if (NULL != pwszRequesterName)
  1779. {
  1780. LocalFree(pwszRequesterName);
  1781. }
  1782. if (NULL != pwszDisposition && pwszDisposition != g_pwszRevokedBy)
  1783. {
  1784. LocalFree(const_cast<WCHAR *>(pwszDisposition));
  1785. }
  1786. if (NULL != prow)
  1787. {
  1788. if (S_OK != hr && !fCommitted)
  1789. {
  1790. HRESULT hr2 = prow->CommitTransaction(FALSE);
  1791. _PrintIfError(hr2, "CommitTransaction");
  1792. }
  1793. prow->Release();
  1794. }
  1795. CertSrvExitServer(State);
  1796. CSASSERT(S_OK == hr || FAILED(hr));
  1797. return(hr);
  1798. }
  1799. STDMETHODIMP
  1800. CCertAdminD::IsValidCertificate(
  1801. /* [unique][string][in] */ const wchar_t __RPC_FAR *pwszAuthority,
  1802. /* [unique][string][in] */ const wchar_t __RPC_FAR *pwszSerialNumber,
  1803. /* [out] */ LONG __RPC_FAR *pRevocationReason,
  1804. /* [out] */ LONG __RPC_FAR *pDisposition)
  1805. {
  1806. HRESULT hr;
  1807. DWORD State = 0;
  1808. DBGPRINT((
  1809. s_ssAdmin,
  1810. "CCertAdminD::IsValidCertificate(tid=%d, this=%x, serial=%ws)\n",
  1811. GetCurrentThreadId(),
  1812. this,
  1813. pwszSerialNumber));
  1814. hr = CertSrvEnterServer(&State);
  1815. _JumpIfError(hr, error, "CertSrvEnterServer");
  1816. hr = CheckAuthorityName(pwszAuthority);
  1817. _JumpIfError(hr, error, "No authority name");
  1818. __try
  1819. {
  1820. CAuditEvent audit(0, g_dwAuditFilter);
  1821. hr = audit.AccessCheck(
  1822. CA_ACCESS_ALLREADROLES,
  1823. audit.m_gcNoAuditSuccess | audit.m_gcNoAuditFailure);
  1824. _LeaveIfError(hr, "CAuditEvent::AccessCheck");
  1825. hr = PKCSIsRevoked(
  1826. 0,
  1827. pwszSerialNumber,
  1828. pRevocationReason,
  1829. pDisposition);
  1830. _LeaveIfError(hr, "PKCSIsRevoked");
  1831. }
  1832. __except(hr = myHEXCEPTIONCODE(), EXCEPTION_EXECUTE_HANDLER)
  1833. {
  1834. _PrintError(hr, "Exception");
  1835. }
  1836. DBGPRINT((
  1837. s_ssAdmin,
  1838. "CCertAdminD::IsValidCertificate(serial=%ws) --> %x, Reason=%u Disposition=%u\n",
  1839. pwszSerialNumber,
  1840. hr,
  1841. *pRevocationReason,
  1842. *pDisposition));
  1843. error:
  1844. CertSrvExitServer(State);
  1845. CSASSERT(S_OK == hr || FAILED(hr));
  1846. return(hr);
  1847. }
  1848. STDMETHODIMP
  1849. CCertAdminD::ServerControl(
  1850. IN wchar_t const *pwszAuthority,
  1851. IN DWORD dwControlFlags,
  1852. OUT CERTTRANSBLOB *pctbOut)
  1853. {
  1854. HRESULT hr;
  1855. BOOL fBackupAccess = FALSE;
  1856. CAuditEvent audit(SE_AUDITID_CERTSRV_SHUTDOWN, g_dwAuditFilter);
  1857. DWORD State = 0;
  1858. DBGPRINT((
  1859. s_ssAdmin,
  1860. "CCertAdminD::ServerControl(tid=%d, this=%x, Flags=0x%x)\n",
  1861. GetCurrentThreadId(),
  1862. this,
  1863. dwControlFlags));
  1864. hr = CertSrvEnterServer(&State);
  1865. _JumpIfError(hr, error, "CertSrvEnterServer");
  1866. hr = CheckAuthorityName(pwszAuthority, true); //allow empty name
  1867. _JumpIfError(hr, error, "CheckAuthorityName");
  1868. switch (dwControlFlags)
  1869. {
  1870. case CSCONTROL_SUSPEND:
  1871. case CSCONTROL_RESTART:
  1872. fBackupAccess = TRUE;
  1873. break;
  1874. case CSCONTROL_SHUTDOWN:
  1875. break;
  1876. default:
  1877. hr = E_INVALIDARG;
  1878. _JumpError(hr, error, "bad control flags");
  1879. }
  1880. __try
  1881. {
  1882. hr = audit.AccessCheck(
  1883. fBackupAccess?CA_ACCESS_OPERATOR:CA_ACCESS_ADMIN,
  1884. audit.m_gcAuditSuccessOrFailure);
  1885. _LeaveIfError(
  1886. hr,
  1887. fBackupAccess?
  1888. "CAuditEvent::AccessCheck backup":
  1889. "CAuditEvent::AccessCheck admin");
  1890. switch (dwControlFlags)
  1891. {
  1892. case CSCONTROL_SHUTDOWN:
  1893. myRegisterMemFree(this, CSM_NEW | CSM_GLOBALDESTRUCTOR);
  1894. hr = CertSrvLockServer(&State);
  1895. _JumpIfError(hr, error, "CertSrvLockServer");
  1896. // have message loop run shutdown code
  1897. SendMessage(g_hwndMain, WM_STOPSERVER, 0, 0);
  1898. // post, don't wait for shutdown
  1899. PostMessage(g_hwndMain, WM_SYNC_CLOSING_THREADS, 0, 0);
  1900. break;
  1901. }
  1902. }
  1903. __except(hr = myHEXCEPTIONCODE(), EXCEPTION_EXECUTE_HANDLER)
  1904. {
  1905. _PrintError(hr, "Exception");
  1906. }
  1907. error:
  1908. CertSrvExitServer(State);
  1909. CSASSERT(S_OK == hr || FAILED(hr));
  1910. return(hr);
  1911. }
  1912. HRESULT
  1913. CCertAdminD::_Ping(
  1914. IN WCHAR const *pwszAuthority)
  1915. {
  1916. HRESULT hr;
  1917. DWORD State = 0;
  1918. hr = CertSrvEnterServer(&State);
  1919. _JumpIfError(hr, error, "CertSrvEnterServer");
  1920. hr = CheckAuthorityName(pwszAuthority, true); //allow empty name
  1921. _JumpIfError(hr, error, "CheckAuthorityName");
  1922. __try
  1923. {
  1924. CAuditEvent audit(0, g_dwAuditFilter);
  1925. hr = audit.AccessCheck(
  1926. CA_ACCESS_ADMIN,
  1927. audit.m_gcNoAuditSuccess | audit.m_gcNoAuditFailure);
  1928. _LeaveIfError(hr, "CAuditEvent::AccessCheck");
  1929. myRegisterMemDump();
  1930. }
  1931. __except(hr = myHEXCEPTIONCODE(), EXCEPTION_EXECUTE_HANDLER)
  1932. {
  1933. _PrintError(hr, "Exception");
  1934. }
  1935. error:
  1936. CertSrvExitServer(State);
  1937. CSASSERT(S_OK == hr || FAILED(hr));
  1938. return(hr);
  1939. }
  1940. STDMETHODIMP
  1941. CCertAdminD::Ping(
  1942. IN WCHAR const *pwszAuthority)
  1943. {
  1944. HRESULT hr;
  1945. DBGPRINT((
  1946. s_ssAdmin,
  1947. "CCertAdminD::Ping(tid=%d, this=%x)\n",
  1948. GetCurrentThreadId(),
  1949. this));
  1950. hr = _Ping(pwszAuthority);
  1951. _JumpIfError(hr, error, "_Ping");
  1952. error:
  1953. CSASSERT(S_OK == hr || FAILED(hr));
  1954. return(hr);
  1955. }
  1956. STDMETHODIMP
  1957. CCertAdminD::Ping2(
  1958. IN WCHAR const *pwszAuthority)
  1959. {
  1960. HRESULT hr;
  1961. DBGPRINT((
  1962. s_ssAdmin,
  1963. "CCertAdminD::Ping2(tid=%d, this=%x)\n",
  1964. GetCurrentThreadId(),
  1965. this));
  1966. hr = _Ping(pwszAuthority);
  1967. _JumpIfError(hr, error, "_Ping");
  1968. error:
  1969. CSASSERT(S_OK == hr || FAILED(hr));
  1970. return(hr);
  1971. }
  1972. STDMETHODIMP
  1973. CCertAdminD::GetServerState(
  1974. IN WCHAR const *pwszAuthority,
  1975. OUT DWORD *pdwState)
  1976. {
  1977. HRESULT hr;
  1978. DWORD State = 0;
  1979. DBGPRINT((
  1980. s_ssAdmin,
  1981. "CCertAdminD::GetServerState(tid=%d, this=%x)\n",
  1982. GetCurrentThreadId(),
  1983. this));
  1984. hr = CertSrvEnterServer(&State);
  1985. _JumpIfError(hr, error, "CertSrvEnterServer");
  1986. hr = CheckAuthorityName(pwszAuthority, true); //allow empty name
  1987. _JumpIfError(hr, error, "CheckAuthorityName");
  1988. __try
  1989. {
  1990. *pdwState = 0;
  1991. {
  1992. CAuditEvent audit(0, g_dwAuditFilter);
  1993. hr = audit.AccessCheck(
  1994. CA_ACCESS_ALLREADROLES,
  1995. audit.m_gcNoAuditSuccess | audit.m_gcNoAuditFailure);
  1996. _LeaveIfError(hr, "CAuditEvent::AccessCheck");
  1997. }
  1998. *pdwState = 1;
  1999. }
  2000. __except(hr = myHEXCEPTIONCODE(), EXCEPTION_EXECUTE_HANDLER)
  2001. {
  2002. _PrintError(hr, "Exception");
  2003. }
  2004. error:
  2005. CertSrvExitServer(State);
  2006. CSASSERT(S_OK == hr || FAILED(hr));
  2007. return(hr);
  2008. }
  2009. STDMETHODIMP
  2010. CCertAdminD::BackupPrepare(
  2011. IN WCHAR const *pwszAuthority,
  2012. IN unsigned long grbitJet,
  2013. IN unsigned long dwBackupFlags,
  2014. IN WCHAR const *pwszBackupAnnotation,
  2015. IN DWORD dwClientIdentifier)
  2016. {
  2017. HRESULT hr;
  2018. CertSrv::CAuditEvent audit(SE_AUDITID_CERTSRV_BACKUPSTART,g_dwAuditFilter);
  2019. DWORD State = 0;
  2020. DBGPRINT((
  2021. s_ssAdmin,
  2022. "CCertAdminD::BackupPrepare(tid=%d, this=%x)\n",
  2023. GetCurrentThreadId(),
  2024. this));
  2025. hr = CertSrvEnterServer(&State);
  2026. _JumpIfError(hr, error, "CertSrvEnterServer");
  2027. hr = CheckAuthorityName(pwszAuthority, true); //allow empty name
  2028. _JumpIfError(hr, error, "CheckAuthorityName");
  2029. __try
  2030. {
  2031. hr = audit.AddData(dwBackupFlags); //%1 backup type
  2032. _LeaveIfError(hr, "CAuditEvent::AddData");
  2033. hr = audit.AccessCheck(
  2034. CA_ACCESS_OPERATOR,
  2035. audit.m_gcAuditSuccessOrFailure);
  2036. _LeaveIfError(hr, "CAuditEvent::AccessCheck");
  2037. if (NULL != m_pBackup)
  2038. {
  2039. hr = E_UNEXPECTED;
  2040. _LeaveError(hr, "Has Backup");
  2041. }
  2042. hr = g_pCertDB->OpenBackup(grbitJet, &m_pBackup);
  2043. _LeaveIfError(hr, "OpenBackup");
  2044. m_grbitBackup = grbitJet;
  2045. }
  2046. __except(hr = myHEXCEPTIONCODE(), EXCEPTION_EXECUTE_HANDLER)
  2047. {
  2048. _PrintError(hr, "Exception");
  2049. }
  2050. error:
  2051. CertSrvExitServer(State);
  2052. CSASSERT(S_OK == hr || FAILED(hr));
  2053. return(hr);
  2054. }
  2055. STDMETHODIMP
  2056. CCertAdminD::BackupEnd()
  2057. {
  2058. HRESULT hr;
  2059. CertSrv::CAuditEvent audit(SE_AUDITID_CERTSRV_BACKUPEND,g_dwAuditFilter);
  2060. DWORD State = 0;
  2061. DBGPRINT((
  2062. s_ssAdmin,
  2063. "CCertAdminD::BackupEnd(tid=%d, this=%x)\n",
  2064. GetCurrentThreadId(),
  2065. this));
  2066. hr = CertSrvEnterServer(&State);
  2067. _JumpIfError(hr, error, "CertSrvEnterServer");
  2068. __try
  2069. {
  2070. hr = audit.AccessCheck(
  2071. CA_ACCESS_OPERATOR,
  2072. audit.m_gcAuditSuccessOrFailure);
  2073. _LeaveIfError(hr, "CAuditEvent::AccessCheck");
  2074. if (NULL == m_pBackup)
  2075. {
  2076. hr = E_UNEXPECTED;
  2077. _LeaveError(hr, "No backup");
  2078. }
  2079. m_pBackup->Release();
  2080. m_pBackup = NULL;
  2081. hr = S_OK;
  2082. }
  2083. __except(hr = myHEXCEPTIONCODE(), EXCEPTION_EXECUTE_HANDLER)
  2084. {
  2085. _PrintError(hr, "Exception");
  2086. }
  2087. error:
  2088. CertSrvExitServer(State);
  2089. CSASSERT(S_OK == hr || FAILED(hr));
  2090. return(hr);
  2091. }
  2092. HRESULT
  2093. CCertAdminD::_GetDynamicFileList(
  2094. IN OUT DWORD *pcwcList,
  2095. OPTIONAL OUT WCHAR *pwszzList)
  2096. {
  2097. HRESULT hr = S_OK;
  2098. HRESULT hr2;
  2099. DWORD iCert;
  2100. DWORD iDelta;
  2101. DWORD iDeltaMax;
  2102. DWORD cwc;
  2103. DWORD cwcRemain;
  2104. DWORD cwcTotal;
  2105. WCHAR const * const *papwszSrc;
  2106. WCHAR const * const *ppwsz;
  2107. DWORD State = 0;
  2108. hr = CertSrvEnterServer(&State);
  2109. _JumpIfError(hr, error, "CertSrvEnterServer");
  2110. cwcRemain = *pcwcList;
  2111. cwcTotal = 0;
  2112. iDeltaMax = g_fDeltaCRLPublishDisabled? 0 : 1;
  2113. for (iCert = 0; iCert < g_cCACerts; iCert++)
  2114. {
  2115. for (iDelta = 0; iDelta <= iDeltaMax; iDelta++)
  2116. {
  2117. hr2 = PKCSGetCRLList(0 != iDelta, iCert, &papwszSrc);
  2118. if (S_OK != hr2)
  2119. {
  2120. _PrintError2(hr2, "PKCSGetCRLList", hr2);
  2121. continue;
  2122. }
  2123. for (ppwsz = papwszSrc; NULL != *ppwsz; ppwsz++)
  2124. {
  2125. WCHAR const *pwsz = *ppwsz;
  2126. // Just return local full path files:
  2127. if (iswalpha(pwsz[0]) && L':' == pwsz[1] && L'\\' == pwsz[2])
  2128. {
  2129. cwc = wcslen(pwsz) + 1;
  2130. if (NULL != pwszzList)
  2131. {
  2132. DWORD cwcT;
  2133. cwcT = min(cwc, cwcRemain);
  2134. CopyMemory(pwszzList, *ppwsz, cwcT * sizeof(WCHAR));
  2135. pwszzList += cwcT;
  2136. if (cwc > cwcT)
  2137. {
  2138. hr = HRESULT_FROM_WIN32(ERROR_BUFFER_OVERFLOW);
  2139. pwszzList = NULL;
  2140. }
  2141. cwcRemain -= cwcT;
  2142. }
  2143. cwcTotal += cwc;
  2144. }
  2145. }
  2146. }
  2147. }
  2148. // append an extra trailing L'\0'
  2149. if (NULL != pwszzList)
  2150. {
  2151. if (1 <= cwcRemain)
  2152. {
  2153. *pwszzList = L'\0';
  2154. }
  2155. else
  2156. {
  2157. hr = HRESULT_FROM_WIN32(ERROR_BUFFER_OVERFLOW);
  2158. }
  2159. }
  2160. cwcTotal++;
  2161. *pcwcList = cwcTotal;
  2162. _JumpIfError(hr, error, "Buffer Overflow");
  2163. error:
  2164. CertSrvExitServer(State);
  2165. return(hr);
  2166. }
  2167. typedef struct _DBTAG
  2168. {
  2169. WCHAR const *pwszPath;
  2170. WCHAR wcFileType;
  2171. } DBTAG;
  2172. DBTAG g_adbtag[] = {
  2173. { g_wszDatabase, CSBFT_CERTSERVER_DATABASE },
  2174. { g_wszLogDir, CSBFT_LOG_DIR },
  2175. { g_wszSystemDir, CSBFT_CHECKPOINT_DIR },
  2176. };
  2177. CSBFT
  2178. BftClassify(
  2179. IN WCHAR const *pwszFileName)
  2180. {
  2181. WCHAR *pwszPath = NULL;
  2182. WCHAR const *pwszExt;
  2183. WCHAR *pwsz;
  2184. DWORD i;
  2185. CSBFT bft;
  2186. // Do the easy cases first.
  2187. pwszExt = wcsrchr(pwszFileName, L'.');
  2188. if (NULL != pwszExt)
  2189. {
  2190. if (0 == lstrcmpi(pwszExt, L".pat"))
  2191. {
  2192. bft = CSBFT_PATCH_FILE;
  2193. goto done;
  2194. }
  2195. if (0 == lstrcmpi(pwszExt, L".log"))
  2196. {
  2197. bft = CSBFT_LOG;
  2198. goto done;
  2199. }
  2200. if (0 == lstrcmpi(pwszExt, L".edb"))
  2201. {
  2202. // It's a database. Find out which database it is.
  2203. for (i = 0; i < ARRAYSIZE(g_adbtag); i++)
  2204. {
  2205. bft = g_adbtag[i].wcFileType;
  2206. if ((bft & CSBFT_DATABASE_DIRECTORY) &&
  2207. 0 == lstrcmpi(g_adbtag[i].pwszPath, pwszFileName))
  2208. {
  2209. goto done;
  2210. }
  2211. }
  2212. }
  2213. }
  2214. // Ok, I give up. We don't know anything about this file at all;
  2215. // try to figure out what we can tell the caller about it.
  2216. pwszPath = (WCHAR *) LocalAlloc(
  2217. LMEM_FIXED,
  2218. (wcslen(pwszFileName) + 1) * sizeof(WCHAR));
  2219. if (NULL != pwszPath)
  2220. {
  2221. wcscpy(pwszPath, pwszFileName);
  2222. pwsz = wcsrchr(pwszPath, L'\\');
  2223. if (NULL != pwsz)
  2224. {
  2225. *pwsz = L'\0'; // truncate to directory path
  2226. }
  2227. for (i = 0; i < ARRAYSIZE(g_adbtag); i++)
  2228. {
  2229. bft = g_adbtag[i].wcFileType;
  2230. if (bft & CSBFT_DIRECTORY)
  2231. {
  2232. // If this file's directory matches the directory we're
  2233. // looking at, we know where it needs to go on the restore.
  2234. if (0 == lstrcmpi(g_adbtag[i].pwszPath, pwszPath))
  2235. {
  2236. goto done;
  2237. }
  2238. }
  2239. }
  2240. }
  2241. bft = CSBFT_UNKNOWN;
  2242. done:
  2243. if (NULL != pwszPath)
  2244. {
  2245. LocalFree(pwszPath);
  2246. }
  2247. return(bft);
  2248. }
  2249. HRESULT
  2250. CCertAdminD::_GetDatabaseLocations(
  2251. IN OUT DWORD *pcwcList,
  2252. OPTIONAL OUT WCHAR *pwszzList)
  2253. {
  2254. HRESULT hr = S_OK;
  2255. DWORD cwc;
  2256. DWORD cwcRemain;
  2257. WCHAR *pwcRemain;
  2258. DWORD i;
  2259. DWORD State = 0;
  2260. hr = CertSrvEnterServer(&State);
  2261. _JumpIfError(hr, error, "CertSrvEnterServer");
  2262. cwcRemain = *pcwcList;
  2263. pwcRemain = pwszzList;
  2264. cwc = 1;
  2265. for (i = 0; i < ARRAYSIZE(g_adbtag); i++)
  2266. {
  2267. DWORD cwcT;
  2268. cwcT = wcslen(g_adbtag[i].pwszPath) + 1;
  2269. cwc += 1 + cwcT;
  2270. if (NULL != pwcRemain && 0 < cwcRemain)
  2271. {
  2272. *pwcRemain++ = g_adbtag[i].wcFileType;
  2273. cwcRemain--;
  2274. if (cwcT > cwcRemain)
  2275. {
  2276. cwcT = cwcRemain;
  2277. }
  2278. CopyMemory(pwcRemain, g_adbtag[i].pwszPath, cwcT * sizeof(WCHAR));
  2279. pwcRemain += cwcT;
  2280. cwcRemain -= cwcT;
  2281. }
  2282. }
  2283. if (NULL != pwcRemain)
  2284. {
  2285. if (0 < cwcRemain)
  2286. {
  2287. *pwcRemain = L'\0';
  2288. }
  2289. if (cwc > *pcwcList)
  2290. {
  2291. hr = HRESULT_FROM_WIN32(ERROR_BUFFER_OVERFLOW);
  2292. }
  2293. }
  2294. *pcwcList = cwc;
  2295. _JumpIfError(hr, error, "Buffer Overflow");
  2296. error:
  2297. CertSrvExitServer(State);
  2298. return(hr);
  2299. }
  2300. STDMETHODIMP
  2301. CCertAdminD::RestoreGetDatabaseLocations(
  2302. OUT WCHAR **ppwszDatabaseLocations,
  2303. OUT LONG *pcwcPaths)
  2304. {
  2305. HRESULT hr;
  2306. DWORD State = 0;
  2307. DBGPRINT((
  2308. s_ssAdmin,
  2309. "CCertAdminD::RestoreGetDatabaseLocations(tid=%d, this=%x)\n",
  2310. GetCurrentThreadId(),
  2311. this));
  2312. hr = CertSrvEnterServer(&State);
  2313. _JumpIfError(hr, error, "CertSrvEnterServer");
  2314. __try
  2315. {
  2316. hr = _BackupGetFileList(MAXDWORD, ppwszDatabaseLocations, pcwcPaths);
  2317. _LeaveIfError(hr, "_BackupGetFileList");
  2318. myRegisterMemFree(*ppwszDatabaseLocations, CSM_MIDLUSERALLOC);
  2319. }
  2320. __except(hr = myHEXCEPTIONCODE(), EXCEPTION_EXECUTE_HANDLER)
  2321. {
  2322. _PrintError(hr, "Exception");
  2323. }
  2324. error:
  2325. CertSrvExitServer(State);
  2326. CSASSERT(S_OK == hr || FAILED(hr));
  2327. return(hr);
  2328. }
  2329. // Convert UNC path to local full path, as in:
  2330. // \\server\c$\foo... --> c:\foo...
  2331. // Note the server name need not match the current server name.
  2332. HRESULT
  2333. ConvertUNCToLocalPath(
  2334. IN WCHAR const *pwszPath,
  2335. OUT WCHAR **ppwszPathLocal) // LocalAlloc
  2336. {
  2337. HRESULT hr = HRESULT_FROM_WIN32(ERROR_INVALID_DATA);
  2338. WCHAR const *pwc;
  2339. *ppwszPathLocal = NULL;
  2340. if (L'\\' != pwszPath[0] || L'\\' != pwszPath[1])
  2341. {
  2342. _JumpError(hr, error, "not a UNC path");
  2343. }
  2344. pwc = wcschr(&pwszPath[2], L'\\');
  2345. if (NULL == pwc || !iswalpha(pwc[1]) || L'$' != pwc[2] || L'\\' != pwc[3])
  2346. {
  2347. _JumpError(hr, error, "bad-UNC path");
  2348. }
  2349. pwc++;
  2350. *ppwszPathLocal = (WCHAR *) LocalAlloc(
  2351. LMEM_FIXED,
  2352. (wcslen(pwc) + 1) * sizeof(WCHAR));
  2353. if (NULL == *ppwszPathLocal)
  2354. {
  2355. hr = E_OUTOFMEMORY;
  2356. _JumpError(hr, error, "LocalAlloc");
  2357. }
  2358. wcscpy(*ppwszPathLocal, pwc);
  2359. CSASSERT(L'$' == (*ppwszPathLocal)[1]);
  2360. (*ppwszPathLocal)[1] = L':';
  2361. hr = S_OK;
  2362. error:
  2363. return(hr);
  2364. }
  2365. // Convert local possibly annotated full paths to possibly annotated UNC, as:
  2366. // [CSBFT_*]c:\foo... --> [CSBFT_*]\\server\c$\foo...
  2367. HRESULT
  2368. ConvertLocalPathsToMungedUNC(
  2369. IN WCHAR const *pwszzFiles,
  2370. IN BOOL fAnnotated, // TRUE if already annotated
  2371. IN WCHAR wcFileType, // else Annotation WCHAR (if not L'\0')
  2372. OUT DWORD *pcwc,
  2373. OUT WCHAR **ppwszzFilesUNC) // MIDL_user_allocate
  2374. {
  2375. HRESULT hr;
  2376. DWORD cwc;
  2377. WCHAR const *pwsz;
  2378. WCHAR *pwszDst;
  2379. DWORD cfiles = 0;
  2380. WCHAR *pwszzFilesUNC = NULL;
  2381. *pcwc = 0;
  2382. for (pwsz = pwszzFiles; L'\0' != *pwsz; pwsz += wcslen(pwsz) + 1)
  2383. {
  2384. if (fAnnotated)
  2385. {
  2386. pwsz++;
  2387. }
  2388. if (!iswalpha(pwsz[0]) || L':' != pwsz[1] || L'\\' != pwsz[2])
  2389. {
  2390. hr = HRESULT_FROM_WIN32(ERROR_INVALID_DATA);
  2391. _JumpError(hr, error, "non-local path");
  2392. }
  2393. cfiles++;
  2394. }
  2395. cwc = SAFE_SUBTRACT_POINTERS(pwsz, pwszzFiles) + 1;
  2396. cwc += cfiles * (2 + wcslen(g_pwszServerName) + 1);
  2397. if (!fAnnotated && 0 != wcFileType)
  2398. {
  2399. cwc += cfiles; // Add munged CSBFT_* character
  2400. }
  2401. pwszzFilesUNC = (WCHAR *) MIDL_user_allocate(cwc * sizeof(WCHAR));
  2402. if (NULL == pwszzFilesUNC)
  2403. {
  2404. hr = E_OUTOFMEMORY;
  2405. _JumpError(hr, error, "MIDL_user_allocate pwszzFiles");
  2406. }
  2407. pwszDst = pwszzFilesUNC;
  2408. for (pwsz = pwszzFiles; L'\0' != *pwsz; pwsz += wcslen(pwsz) + 1)
  2409. {
  2410. if (fAnnotated)
  2411. {
  2412. *pwszDst++ = *pwsz++; // "CSBFT"
  2413. }
  2414. else
  2415. if (0 != wcFileType)
  2416. {
  2417. *pwszDst++ = BftClassify(pwsz); // "CSBFT"
  2418. }
  2419. wcscpy(pwszDst, L"\\\\"); // "[CSBFT]\\"
  2420. wcscat(pwszDst, g_pwszServerName); // "[CSBFT]\\server"
  2421. pwszDst += wcslen(pwszDst);
  2422. *pwszDst++ = L'\\'; // "[CSBFT]\\server\"
  2423. *pwszDst++ = *pwsz++; // "[CSBFT]\\server\c"
  2424. *pwszDst++ = L'$'; // "[CSBFT]\\server\c$"
  2425. pwsz++; // skip colon
  2426. wcscpy(pwszDst, pwsz); // "[CSBFT]\\server\c$\foo..."
  2427. pwszDst += wcslen(pwszDst) + 1;
  2428. }
  2429. *pwszDst = L'\0';
  2430. CSASSERT(SAFE_SUBTRACT_POINTERS(pwszDst, pwszzFilesUNC) + 1 == cwc);
  2431. *pcwc = cwc;
  2432. *ppwszzFilesUNC = pwszzFilesUNC;
  2433. hr = S_OK;
  2434. error:
  2435. return(hr);
  2436. }
  2437. HRESULT
  2438. CCertAdminD::_BackupGetFileList(
  2439. IN DWORD dwFileType,
  2440. OUT WCHAR **ppwszzFiles, // CoTaskMem*
  2441. OUT LONG *pcwcFiles)
  2442. {
  2443. HRESULT hr;
  2444. WCHAR *pwszzFiles = NULL;
  2445. WCHAR *pwszzFilesUNC = NULL;
  2446. DWORD cwcFiles = 0;
  2447. DWORD cwc;
  2448. BOOL fAnnotated = FALSE;
  2449. DWORD State = 0;
  2450. *ppwszzFiles = NULL;
  2451. *pcwcFiles = 0;
  2452. hr = CertSrvEnterServer(&State);
  2453. _JumpIfError(hr, error, "CertSrvEnterServer");
  2454. if (NULL == m_pBackup && MAXDWORD != dwFileType && 0 != dwFileType)
  2455. {
  2456. hr = E_UNEXPECTED;
  2457. _JumpIfError(hr, error, "No backup");
  2458. }
  2459. while (TRUE)
  2460. {
  2461. cwc = cwcFiles;
  2462. if (CSBFT_CERTSERVER_DATABASE == dwFileType)
  2463. {
  2464. hr = m_pBackup->GetDBFileList(&cwc, pwszzFiles);
  2465. _JumpIfError(hr, error, "GetDBFileList");
  2466. }
  2467. else if (CSBFT_LOG == dwFileType)
  2468. {
  2469. hr = m_pBackup->GetLogFileList(&cwc, pwszzFiles);
  2470. _JumpIfError(hr, error, "GetLogFileList");
  2471. }
  2472. else if (MAXDWORD == dwFileType)
  2473. {
  2474. hr = _GetDatabaseLocations(&cwc, pwszzFiles);
  2475. _JumpIfError(hr, error, "_GetDatabaseLocations");
  2476. fAnnotated = TRUE;
  2477. }
  2478. else if (0 == dwFileType)
  2479. {
  2480. hr = _GetDynamicFileList(&cwc, pwszzFiles);
  2481. _JumpIfError(hr, error, "_GetDynamicFileList");
  2482. }
  2483. else
  2484. {
  2485. CSASSERT(!"bad FileListtype");
  2486. }
  2487. if (NULL != pwszzFiles)
  2488. {
  2489. break;
  2490. }
  2491. pwszzFiles = (WCHAR *) LocalAlloc(LMEM_FIXED, cwc * sizeof(WCHAR));
  2492. if (NULL == pwszzFiles)
  2493. {
  2494. hr = E_OUTOFMEMORY;
  2495. _JumpError(hr, error, "LocalAlloc pwszzFiles");
  2496. }
  2497. cwcFiles = cwc;
  2498. }
  2499. hr = ConvertLocalPathsToMungedUNC(
  2500. pwszzFiles,
  2501. fAnnotated,
  2502. (WCHAR) dwFileType,
  2503. &cwc,
  2504. &pwszzFilesUNC);
  2505. _JumpIfError(hr, error, "ConvertLocalPathsToMungedUNC");
  2506. *ppwszzFiles = pwszzFilesUNC;
  2507. *pcwcFiles = cwc;
  2508. pwszzFilesUNC = NULL;
  2509. error:
  2510. if (NULL != pwszzFilesUNC)
  2511. {
  2512. MIDL_user_free(pwszzFilesUNC);
  2513. }
  2514. if (NULL != pwszzFiles)
  2515. {
  2516. LocalFree(pwszzFiles);
  2517. }
  2518. CertSrvExitServer(State);
  2519. return(hr);
  2520. }
  2521. STDMETHODIMP
  2522. CCertAdminD::BackupGetAttachmentInformation(
  2523. OUT WCHAR **ppwszzDBFiles,
  2524. OUT LONG *pcwcDBFiles)
  2525. {
  2526. HRESULT hr;
  2527. DWORD State = 0;
  2528. DBGPRINT((
  2529. s_ssAdmin,
  2530. "CCertAdminD::BackupGetAttachmentInformation(tid=%d, this=%x)\n",
  2531. GetCurrentThreadId(),
  2532. this));
  2533. hr = CertSrvEnterServer(&State);
  2534. _JumpIfError(hr, error, "CertSrvEnterServer");
  2535. __try
  2536. {
  2537. hr = _BackupGetFileList(
  2538. CSBFT_CERTSERVER_DATABASE,
  2539. ppwszzDBFiles,
  2540. pcwcDBFiles);
  2541. _LeaveIfError(hr, "_BackupGetFileList");
  2542. myRegisterMemFree(*ppwszzDBFiles, CSM_MIDLUSERALLOC);
  2543. }
  2544. __except(hr = myHEXCEPTIONCODE(), EXCEPTION_EXECUTE_HANDLER)
  2545. {
  2546. _PrintError(hr, "Exception");
  2547. }
  2548. error:
  2549. CertSrvExitServer(State);
  2550. CSASSERT(S_OK == hr || FAILED(hr));
  2551. return(hr);
  2552. }
  2553. STDMETHODIMP
  2554. CCertAdminD::BackupGetBackupLogs(
  2555. OUT WCHAR **ppwszzLogFiles,
  2556. OUT LONG *pcwcLogFiles)
  2557. {
  2558. HRESULT hr;
  2559. DWORD State = 0;
  2560. DBGPRINT((
  2561. s_ssAdmin,
  2562. "CCertAdminD::BackupGetBackupLogs(tid=%d, this=%x)\n",
  2563. GetCurrentThreadId(),
  2564. this));
  2565. hr = CertSrvEnterServer(&State);
  2566. _JumpIfError(hr, error, "CertSrvEnterServer");
  2567. __try
  2568. {
  2569. hr = _BackupGetFileList(CSBFT_LOG, ppwszzLogFiles, pcwcLogFiles);
  2570. _LeaveIfError(hr, "_BackupGetFileList");
  2571. myRegisterMemFree(*ppwszzLogFiles, CSM_MIDLUSERALLOC);
  2572. }
  2573. __except(hr = myHEXCEPTIONCODE(), EXCEPTION_EXECUTE_HANDLER)
  2574. {
  2575. _PrintError(hr, "Exception");
  2576. }
  2577. error:
  2578. CertSrvExitServer(State);
  2579. CSASSERT(S_OK == hr || FAILED(hr));
  2580. return(hr);
  2581. }
  2582. STDMETHODIMP
  2583. CCertAdminD::BackupGetDynamicFiles(
  2584. OUT WCHAR **ppwszzFiles,
  2585. OUT LONG *pcwcFiles)
  2586. {
  2587. HRESULT hr;
  2588. DWORD State = 0;
  2589. DBGPRINT((
  2590. s_ssAdmin,
  2591. "CCertAdminD::BackupGetDynamicFiles(tid=%d, this=%x)\n",
  2592. GetCurrentThreadId(),
  2593. this));
  2594. hr = CertSrvEnterServer(&State);
  2595. _JumpIfError(hr, error, "CertSrvEnterServer");
  2596. __try
  2597. {
  2598. hr = _BackupGetFileList(0, ppwszzFiles, pcwcFiles);
  2599. _LeaveIfError(hr, "_BackupGetFileList");
  2600. myRegisterMemFree(*ppwszzFiles, CSM_MIDLUSERALLOC);
  2601. }
  2602. __except(hr = myHEXCEPTIONCODE(), EXCEPTION_EXECUTE_HANDLER)
  2603. {
  2604. _PrintError(hr, "Exception");
  2605. }
  2606. error:
  2607. CertSrvExitServer(State);
  2608. CSASSERT(S_OK == hr || FAILED(hr));
  2609. return(hr);
  2610. }
  2611. STDMETHODIMP
  2612. CCertAdminD::BackupOpenFile(
  2613. IN WCHAR const *pwszPath,
  2614. OUT unsigned hyper *pliLength)
  2615. {
  2616. HRESULT hr;
  2617. WCHAR *pwszPathLocal = NULL;
  2618. DWORD State = 0;
  2619. DBGPRINT((
  2620. s_ssAdmin,
  2621. "CCertAdminD::BackupOpenFile(tid=%d, this=%x)\n",
  2622. GetCurrentThreadId(),
  2623. this));
  2624. hr = CertSrvEnterServer(&State);
  2625. _JumpIfError(hr, error, "CertSrvEnterServer");
  2626. __try
  2627. {
  2628. if (NULL == m_pBackup)
  2629. {
  2630. hr = E_UNEXPECTED;
  2631. _LeaveIfError(hr, "No backup");
  2632. }
  2633. hr = ConvertUNCToLocalPath(pwszPath, &pwszPathLocal);
  2634. _LeaveIfError(hr, "ConvertUNCToLocalPath");
  2635. hr = m_pBackup->OpenFile(pwszPathLocal, (ULARGE_INTEGER *) pliLength);
  2636. _LeaveIfError(hr, "OpenFile");
  2637. }
  2638. __except(hr = myHEXCEPTIONCODE(), EXCEPTION_EXECUTE_HANDLER)
  2639. {
  2640. _PrintError(hr, "Exception");
  2641. }
  2642. if (NULL != pwszPathLocal)
  2643. {
  2644. LocalFree(pwszPathLocal);
  2645. }
  2646. error:
  2647. CertSrvExitServer(State);
  2648. CSASSERT(S_OK == hr || FAILED(hr));
  2649. return(hr);
  2650. }
  2651. STDMETHODIMP
  2652. CCertAdminD::BackupReadFile(
  2653. OUT BYTE *pbBuffer,
  2654. IN LONG cbBuffer,
  2655. OUT LONG *pcbRead)
  2656. {
  2657. HRESULT hr;
  2658. DWORD State = 0;
  2659. DBGPRINT((
  2660. s_ssAdmin,
  2661. "CCertAdminD::BackupReadFile(tid=%d, this=%x)\n",
  2662. GetCurrentThreadId(),
  2663. this));
  2664. hr = CertSrvEnterServer(&State);
  2665. _JumpIfError(hr, error, "CertSrvEnterServer");
  2666. __try
  2667. {
  2668. if (NULL == m_pBackup)
  2669. {
  2670. hr = E_UNEXPECTED;
  2671. _LeaveIfError(hr, "No backup");
  2672. }
  2673. *pcbRead = cbBuffer;
  2674. hr = m_pBackup->ReadFile((DWORD *) pcbRead, pbBuffer);
  2675. _LeaveIfError(hr, "ReadFile");
  2676. }
  2677. __except(hr = myHEXCEPTIONCODE(), EXCEPTION_EXECUTE_HANDLER)
  2678. {
  2679. _PrintError(hr, "Exception");
  2680. }
  2681. error:
  2682. CertSrvExitServer(State);
  2683. CSASSERT(S_OK == hr || FAILED(hr));
  2684. return(hr);
  2685. }
  2686. STDMETHODIMP
  2687. CCertAdminD::BackupCloseFile()
  2688. {
  2689. HRESULT hr;
  2690. DWORD State = 0;
  2691. DBGPRINT((
  2692. s_ssAdmin,
  2693. "CCertAdminD::BackupCloseFile(tid=%d, this=%x)\n",
  2694. GetCurrentThreadId(),
  2695. this));
  2696. hr = CertSrvEnterServer(&State);
  2697. _JumpIfError(hr, error, "CertSrvEnterServer");
  2698. __try
  2699. {
  2700. if (NULL == m_pBackup)
  2701. {
  2702. hr = E_UNEXPECTED;
  2703. _LeaveIfError(hr, "No backup");
  2704. }
  2705. hr = m_pBackup->CloseFile();
  2706. _LeaveIfError(hr, "CloseFile");
  2707. }
  2708. __except(hr = myHEXCEPTIONCODE(), EXCEPTION_EXECUTE_HANDLER)
  2709. {
  2710. _PrintError(hr, "Exception");
  2711. }
  2712. error:
  2713. CertSrvExitServer(State);
  2714. CSASSERT(S_OK == hr || FAILED(hr));
  2715. return(hr);
  2716. }
  2717. STDMETHODIMP
  2718. CCertAdminD::BackupTruncateLogs()
  2719. {
  2720. HRESULT hr;
  2721. DWORD State = 0;
  2722. DBGPRINT((
  2723. s_ssAdmin,
  2724. "CCertAdminD::BackupTruncateLogs(tid=%d, this=%x)\n",
  2725. GetCurrentThreadId(),
  2726. this));
  2727. hr = CertSrvEnterServer(&State);
  2728. _JumpIfError(hr, error, "CertSrvEnterServer");
  2729. __try
  2730. {
  2731. WCHAR *apwsz[1];
  2732. if (NULL == m_pBackup)
  2733. {
  2734. hr = E_UNEXPECTED;
  2735. _LeaveIfError(hr, "No backup");
  2736. }
  2737. hr = m_pBackup->TruncateLog();
  2738. _LeaveIfError(hr, "TruncateLog");
  2739. apwsz[0] = wszREGDBLASTINCREMENTALBACKUP;
  2740. hr = CertSrvSetRegistryFileTimeValue(
  2741. TRUE,
  2742. (JET_bitBackupIncremental & m_grbitBackup)?
  2743. wszREGDBLASTINCREMENTALBACKUP :
  2744. wszREGDBLASTFULLBACKUP,
  2745. (DWORD)((JET_bitBackupIncremental & m_grbitBackup)?
  2746. 0 : ARRAYSIZE(apwsz)),
  2747. apwsz);
  2748. _PrintIfError(hr, "CertSrvSetRegistryFileTimeValue");
  2749. hr = S_OK;
  2750. }
  2751. __except(hr = myHEXCEPTIONCODE(), EXCEPTION_EXECUTE_HANDLER)
  2752. {
  2753. _PrintError(hr, "Exception");
  2754. }
  2755. error:
  2756. CertSrvExitServer(State);
  2757. CSASSERT(S_OK == hr || FAILED(hr));
  2758. return(hr);
  2759. }
  2760. STDMETHODIMP
  2761. CCertAdminD::ImportCertificate(
  2762. IN wchar_t const *pwszAuthority,
  2763. IN CERTTRANSBLOB *pctbCertificate,
  2764. IN LONG Flags,
  2765. OUT LONG *pRequestId)
  2766. {
  2767. HRESULT hr;
  2768. ICertDBRow *prow = NULL;
  2769. CERT_CONTEXT const *pCert = NULL;
  2770. WCHAR *pwszUserName = NULL;
  2771. BOOL fAllowed = FALSE;
  2772. CACTX *pCAContext;
  2773. CAuditEvent audit(SE_AUDITID_CERTSRV_IMPORTCERT, g_dwAuditFilter);
  2774. DWORD State = 0;
  2775. BOOL fCommitted = FALSE;
  2776. DWORD Disposition;
  2777. BYTE abHash[CBMAX_CRYPT_HASH_LEN];
  2778. DWORD cbHash;
  2779. BSTR strHash = NULL;
  2780. DBGPRINT((
  2781. s_ssAdmin,
  2782. "CCertAdminD::ImportCertificate(tid=%d, this=%x, cb=%x)\n",
  2783. GetCurrentThreadId(),
  2784. this,
  2785. pctbCertificate->cb));
  2786. hr = CertSrvEnterServer(&State);
  2787. _JumpIfError(hr, error, "CertSrvEnterServer");
  2788. if (~(ICF_ALLOWFOREIGN | CR_IN_ENCODEMASK) & Flags)
  2789. {
  2790. hr = E_INVALIDARG;
  2791. _JumpError(hr, error, "Flags");
  2792. }
  2793. if ((ICF_ALLOWFOREIGN & Flags) &&
  2794. 0 == (KRAF_ENABLEFOREIGN & g_KRAFlags))
  2795. {
  2796. hr = E_INVALIDARG;
  2797. _JumpError(hr, error, "Foreign disabled");
  2798. }
  2799. hr = CheckAuthorityName(pwszAuthority);
  2800. _JumpIfError(hr, error, "No authority name");
  2801. __try
  2802. {
  2803. hr = audit.AddData(
  2804. pctbCertificate->pb,
  2805. pctbCertificate->cb); // %1 Certificate
  2806. _LeaveIfError(hr, "CAuditEvent::AddData");
  2807. hr = audit.AddData((DWORD)0); // %2 dummy request ID, if access check fails
  2808. // and a deny event is generated, we need the
  2809. // right number of audit arguments
  2810. _LeaveIfError(hr, "CAuditEvent::AddData");
  2811. hr = audit.AccessCheck(
  2812. CA_ACCESS_OFFICER,
  2813. audit.m_gcNoAuditSuccess);
  2814. _LeaveIfError(hr, "CAuditEvent::AccessCheck");
  2815. pCert = CertCreateCertificateContext(
  2816. X509_ASN_ENCODING,
  2817. pctbCertificate->pb,
  2818. pctbCertificate->cb);
  2819. if (NULL == pCert)
  2820. {
  2821. hr = HRESULT_FROM_WIN32(ERROR_INVALID_DATA);
  2822. _LeaveError(hr, "CertCreateCertificateContext");
  2823. }
  2824. // Be sure we issued this certificate before adding it to the database.
  2825. Disposition = DB_DISP_ISSUED;
  2826. hr = PKCSVerifyIssuedCertificate(pCert, &pCAContext);
  2827. if (S_OK != hr)
  2828. {
  2829. _PrintError2(hr, "PKCSVerifyIssuedCertificate", NTE_BAD_SIGNATURE);
  2830. if (0 == (ICF_ALLOWFOREIGN & Flags))
  2831. {
  2832. _LeaveError2(
  2833. hr,
  2834. "PKCSVerifyIssuedCertificate",
  2835. NTE_BAD_SIGNATURE);
  2836. }
  2837. Disposition = DB_DISP_FOREIGN;
  2838. pCAContext = NULL;
  2839. }
  2840. cbHash = sizeof(abHash);
  2841. if (!CertGetCertificateContextProperty(
  2842. pCert,
  2843. CERT_SHA1_HASH_PROP_ID,
  2844. abHash,
  2845. &cbHash))
  2846. {
  2847. hr = myHLastError();
  2848. _LeaveError(hr, "CertGetCertificateContextProperty");
  2849. }
  2850. hr = MultiByteIntegerToBstr(TRUE, cbHash, abHash, &strHash);
  2851. _LeaveIfError(hr, "MultiByteIntegerToBstr");
  2852. hr = g_pCertDB->OpenRow(
  2853. PROPOPEN_READONLY |
  2854. PROPOPEN_CERTHASH |
  2855. PROPTABLE_REQCERT,
  2856. 0, // RequestId
  2857. strHash,
  2858. &prow);
  2859. if (CERTSRV_E_PROPERTY_EMPTY != hr)
  2860. {
  2861. _LeaveIfErrorStr(hr, "OpenRow", strHash);
  2862. fCommitted = TRUE; // open for read-only: skip rollback
  2863. hr = HRESULT_FROM_WIN32(ERROR_OBJECT_ALREADY_EXISTS);
  2864. _LeaveErrorStr2(
  2865. hr,
  2866. "Cert exists",
  2867. strHash,
  2868. HRESULT_FROM_WIN32(ERROR_OBJECT_ALREADY_EXISTS));
  2869. }
  2870. // okay, we've got valid data. Time to write to the Database.
  2871. hr = g_pCertDB->OpenRow(PROPTABLE_REQCERT, 0, NULL, &prow);
  2872. _LeaveIfError(hr, "OpenRow");
  2873. // set request id
  2874. hr = prow->GetRowId((DWORD *) pRequestId);
  2875. _LeaveIfError(hr, "GetRowId");
  2876. hr = GetClientUserName(NULL, &pwszUserName, NULL);
  2877. _LeaveIfError(hr, "GetClientUserName");
  2878. hr = prow->SetProperty(
  2879. g_wszPropRequesterName,
  2880. PROPTYPE_STRING | PROPCALLER_SERVER | PROPTABLE_REQUEST,
  2881. MAXDWORD,
  2882. (BYTE const *) pwszUserName);
  2883. _LeaveIfError(hr, "SetProperty(requester)");
  2884. hr = prow->SetProperty(
  2885. g_wszPropCallerName,
  2886. PROPTYPE_STRING | PROPCALLER_SERVER | PROPTABLE_REQUEST,
  2887. MAXDWORD,
  2888. (BYTE const *) pwszUserName);
  2889. _LeaveIfError(hr, "SetProperty(caller)");
  2890. hr = PKCSParseImportedCertificate(
  2891. Disposition,
  2892. prow,
  2893. pCAContext,
  2894. pCert);
  2895. _LeaveIfError(hr, "PKCSParseImportedCertificate");
  2896. hr = prow->CommitTransaction(TRUE);
  2897. _LeaveIfError(hr, "CommitTransaction");
  2898. fCommitted = TRUE;
  2899. audit.DeleteLastData(); // remove dummy request ID added above
  2900. hr = audit.AddData((DWORD) *pRequestId); // %2 request ID
  2901. _LeaveIfError(hr, "CAuditEvent::AddData");
  2902. hr = audit.CachedGenerateAudit();
  2903. _LeaveIfError(hr, "CAuditEvent::CachedGenerateAudit");
  2904. }
  2905. __except(hr = myHEXCEPTIONCODE(), EXCEPTION_EXECUTE_HANDLER)
  2906. {
  2907. _PrintError(hr, "Exception");
  2908. }
  2909. error:
  2910. if (NULL != strHash)
  2911. {
  2912. SysFreeString(strHash);
  2913. }
  2914. if (NULL != pwszUserName)
  2915. {
  2916. LocalFree(pwszUserName);
  2917. }
  2918. if (NULL != pCert)
  2919. {
  2920. CertFreeCertificateContext(pCert);
  2921. }
  2922. if (NULL != prow)
  2923. {
  2924. if (S_OK != hr && !fCommitted)
  2925. {
  2926. HRESULT hr2 = prow->CommitTransaction(FALSE);
  2927. _PrintIfError(hr2, "CommitTransaction");
  2928. }
  2929. prow->Release();
  2930. }
  2931. CertSrvExitServer(State);
  2932. CSASSERT(S_OK == hr || FAILED(hr));
  2933. return(hr);
  2934. }
  2935. STDMETHODIMP
  2936. CCertAdminD::ImportKey(
  2937. IN wchar_t const *pwszAuthority,
  2938. IN DWORD RequestId,
  2939. IN wchar_t const *pwszCertHash,
  2940. IN DWORD Flags,
  2941. IN CERTTRANSBLOB *pctbKey)
  2942. {
  2943. HRESULT hr;
  2944. ICertDBRow *prow = NULL;
  2945. CAuditEvent audit(SE_AUDITID_CERTSRV_IMPORTKEY, g_dwAuditFilter);
  2946. DWORD State = 0;
  2947. BOOL fCommitted = FALSE;
  2948. BYTE *pbCert = NULL;
  2949. DWORD cbCert;
  2950. CERT_CONTEXT const *pCert = NULL;
  2951. DBGPRINT((
  2952. s_ssAdmin,
  2953. "CCertAdminD::ImportKey(tid=%d, this=%x, cb=%x)\n",
  2954. GetCurrentThreadId(),
  2955. this,
  2956. pctbKey->cb));
  2957. hr = CertSrvEnterServer(&State);
  2958. _JumpIfError(hr, error, "CertSrvEnterServer");
  2959. hr = CheckAuthorityName(pwszAuthority);
  2960. _JumpIfError(hr, error, "No authority name");
  2961. if (~(IKF_OVERWRITE | CR_IN_ENCODEMASK) & Flags)
  2962. {
  2963. hr = E_INVALIDARG;
  2964. _JumpError(hr, error, "Flags");
  2965. }
  2966. __try
  2967. {
  2968. CRYPT_ATTR_BLOB BlobEncrypted;
  2969. DWORD cb;
  2970. hr = audit.AddData(RequestId); // %1 request ID
  2971. _LeaveIfError(hr, "AddData");
  2972. hr = audit.AccessCheck(
  2973. CA_ACCESS_OFFICER,
  2974. audit.m_gcNoAuditSuccess);
  2975. _LeaveIfError(hr, "CAuditEvent::AccessCheck");
  2976. if (MAXDWORD == RequestId)
  2977. {
  2978. RequestId = 0;
  2979. }
  2980. if (0 == RequestId && NULL == pwszCertHash)
  2981. {
  2982. hr = E_INVALIDARG;
  2983. _LeaveError(hr, "pwszCertHash NULL");
  2984. }
  2985. hr = g_pCertDB->OpenRow(
  2986. PROPTABLE_REQCERT |
  2987. (NULL != pwszCertHash? PROPOPEN_CERTHASH : 0),
  2988. RequestId,
  2989. pwszCertHash,
  2990. &prow);
  2991. _LeaveIfErrorStr(hr, "OpenRow", pwszCertHash);
  2992. BlobEncrypted.cbData = pctbKey->cb;
  2993. BlobEncrypted.pbData = pctbKey->pb;
  2994. cb = 0;
  2995. hr = prow->GetProperty(
  2996. g_wszPropRequestRawArchivedKey,
  2997. PROPTYPE_BINARY |
  2998. PROPCALLER_SERVER |
  2999. PROPTABLE_REQUEST,
  3000. &cb,
  3001. NULL);
  3002. if (CERTSRV_E_PROPERTY_EMPTY != hr)
  3003. {
  3004. _LeaveIfErrorStr(hr, "OpenRow", pwszCertHash);
  3005. }
  3006. hr = PKCSGetProperty(
  3007. prow,
  3008. g_wszPropRawCertificate,
  3009. PROPTYPE_BINARY | PROPCALLER_SERVER | PROPTABLE_CERTIFICATE,
  3010. &cbCert,
  3011. (BYTE **) &pbCert);
  3012. _LeaveIfError(hr, "PKCSGetProperty(cert)");
  3013. pCert = CertCreateCertificateContext(X509_ASN_ENCODING, pbCert, cbCert);
  3014. if (NULL == pCert)
  3015. {
  3016. hr = HRESULT_FROM_WIN32(ERROR_INVALID_DATA);
  3017. _LeaveError(hr, "CertCreateCertificateContext");
  3018. }
  3019. hr = PKCSArchivePrivateKey(
  3020. prow,
  3021. CERT_V1 == pCert->pCertInfo->dwVersion,
  3022. (IKF_OVERWRITE & Flags)? TRUE : FALSE,
  3023. &BlobEncrypted,
  3024. NULL);
  3025. _LeaveIfError2(
  3026. hr,
  3027. "PKCSArchivePrivateKey",
  3028. HRESULT_FROM_WIN32(ERROR_OBJECT_ALREADY_EXISTS));
  3029. hr = prow->CommitTransaction(TRUE);
  3030. _LeaveIfError(hr, "CommitTransaction");
  3031. fCommitted = TRUE;
  3032. hr = audit.CachedGenerateAudit();
  3033. _LeaveIfError(hr, "CAuditEvent::CachedGenerateAudit");
  3034. }
  3035. __except(hr = myHEXCEPTIONCODE(), EXCEPTION_EXECUTE_HANDLER)
  3036. {
  3037. _PrintError(hr, "Exception");
  3038. }
  3039. error:
  3040. if (NULL != pbCert)
  3041. {
  3042. LocalFree(pbCert);
  3043. }
  3044. if (NULL != pCert)
  3045. {
  3046. CertFreeCertificateContext(pCert);
  3047. }
  3048. if (NULL != prow)
  3049. {
  3050. if (S_OK != hr && !fCommitted)
  3051. {
  3052. HRESULT hr2 = prow->CommitTransaction(FALSE);
  3053. _PrintIfError(hr2, "CommitTransaction");
  3054. }
  3055. prow->Release();
  3056. }
  3057. CertSrvExitServer(State);
  3058. CSASSERT(S_OK == hr || FAILED(hr));
  3059. return(hr);
  3060. }
  3061. STDMETHODIMP
  3062. CCertAdminD::GetCASecurity(
  3063. IN WCHAR const *pwszAuthority,
  3064. OUT CERTTRANSBLOB *pctbSD) // CoTaskMem*
  3065. {
  3066. HRESULT hr;
  3067. PSECURITY_DESCRIPTOR pSD = NULL;
  3068. CAuditEvent audit(0, g_dwAuditFilter);
  3069. DWORD State = 0;
  3070. // init
  3071. pctbSD->pb = NULL;
  3072. pctbSD->cb = 0;
  3073. DBGPRINT((
  3074. s_ssAdmin,
  3075. "CCertAdminD::GetCASecurity(tid=%d, this=%x)\n",
  3076. GetCurrentThreadId(),
  3077. this));
  3078. hr = CertSrvEnterServer(&State);
  3079. _JumpIfError(hr, error, "CertSrvEnterServer");
  3080. hr = CheckAuthorityName(pwszAuthority);
  3081. _JumpIfError(hr, error, "CheckAuthorityName");
  3082. __try
  3083. {
  3084. hr = audit.AccessCheck(
  3085. CA_ACCESS_ALLREADROLES,
  3086. audit.m_gcNoAuditSuccess | audit.m_gcNoAuditFailure);
  3087. _LeaveIfError(hr, "CAuditEvent::AccessCheck");
  3088. // get current SD:
  3089. hr = g_CASD.LockGet(&pSD); // no free
  3090. _LeaveIfError(hr, "CProtectedSecurityDescriptor::LockGet");
  3091. pctbSD->cb = GetSecurityDescriptorLength(pSD);
  3092. pctbSD->pb = (BYTE *) MIDL_user_allocate(pctbSD->cb);
  3093. if (NULL == pctbSD->pb)
  3094. {
  3095. hr = E_OUTOFMEMORY;
  3096. _LeaveError(hr, "MIDL_user_allocate");
  3097. }
  3098. myRegisterMemFree(pctbSD->pb, CSM_MIDLUSERALLOC);
  3099. CopyMemory(pctbSD->pb, pSD, pctbSD->cb);
  3100. hr = g_CASD.Unlock();
  3101. _LeaveIfError(hr, "CProtectedSecurityDescriptor::Unlock");
  3102. }
  3103. __except(hr = myHEXCEPTIONCODE(), EXCEPTION_EXECUTE_HANDLER)
  3104. {
  3105. _PrintError(hr, "Exception");
  3106. }
  3107. error:
  3108. CertSrvExitServer(State);
  3109. return hr;
  3110. }
  3111. STDMETHODIMP
  3112. CCertAdminD::SetCASecurity(
  3113. IN WCHAR const *pwszAuthority,
  3114. IN CERTTRANSBLOB *pctbSD)
  3115. {
  3116. HRESULT hr;
  3117. PSECURITY_DESCRIPTOR pSD = (PSECURITY_DESCRIPTOR) pctbSD->pb;
  3118. LPWSTR pwszSD = NULL;
  3119. CAuditEvent audit(SE_AUDITID_CERTSRV_SETSECURITY, g_dwAuditFilter);
  3120. DWORD State = 0;
  3121. DBGPRINT((
  3122. s_ssAdmin,
  3123. "CCertAdminD::SetCASecurity(tid=%d, this=%x)\n",
  3124. GetCurrentThreadId(),
  3125. this));
  3126. hr = CertSrvEnterServer(&State);
  3127. _JumpIfError(hr, error, "CertSrvEnterServer");
  3128. hr = CheckAuthorityName(pwszAuthority);
  3129. _JumpIfError(hr, error, "CheckAuthorityName");
  3130. __try
  3131. {
  3132. hr = audit.AddData(pctbSD->pb, pctbSD->cb); // %1 dump permissions as blob, we
  3133. // don't want to parse the blob unless
  3134. // access check succeeds
  3135. _LeaveIfError(hr, "CAuditEvent::AddData");
  3136. hr = audit.AccessCheck(
  3137. CA_ACCESS_ADMIN,
  3138. audit.m_gcNoAuditSuccess);
  3139. _LeaveIfError(hr, "CAuditEvent::AccessCheck");
  3140. hr = CCertificateAuthoritySD::ConvertToString(pSD, pwszSD);
  3141. _LeaveIfError(hr, "CAuditEvent::ConvertToString");
  3142. audit.DeleteLastData(); // remove permissions blob to add a human friendly SD dump
  3143. hr = audit.AddData(pwszSD);
  3144. _LeaveIfError(hr, "CAuditEvent::AddData");
  3145. hr = g_CASD.Set(pSD, g_fUseDS?true:false);
  3146. _LeaveIfError(hr, "CProtectedSecurityDescriptor::Set");
  3147. if (g_OfficerRightsSD.IsEnabled())
  3148. {
  3149. // adjust officer rights to match new CA SD; persistently save it
  3150. hr = g_OfficerRightsSD.Adjust(pSD);
  3151. _LeaveIfError(hr, "CProtectedSecurityDescriptor::Adjust");
  3152. hr = g_OfficerRightsSD.Save();
  3153. _LeaveIfError(hr, "CProtectedSecurityDescriptor::Save");
  3154. }
  3155. hr = g_CASD.Save();
  3156. _LeaveIfError(hr, "CProtectedSecurityDescriptor::Save");
  3157. hr = audit.CachedGenerateAudit();
  3158. _LeaveIfError(hr, "CAuditEvent::CachedGenerateAudit");
  3159. }
  3160. __except(hr = myHEXCEPTIONCODE(), EXCEPTION_EXECUTE_HANDLER)
  3161. {
  3162. _PrintError(hr, "Exception");
  3163. }
  3164. error:
  3165. if (HRESULT_FROM_WIN32(ERROR_CAN_NOT_COMPLETE) == hr)
  3166. {
  3167. LogEventString(
  3168. EVENTLOG_ERROR_TYPE,
  3169. MSG_E_CANNOT_WRITE_TO_DS,
  3170. g_wszCommonName);
  3171. }
  3172. else
  3173. {
  3174. if(S_OK != hr)
  3175. {
  3176. LogEventHResult(
  3177. EVENTLOG_ERROR_TYPE,
  3178. MSG_E_CANNOT_SET_PERMISSIONS,
  3179. hr);
  3180. }
  3181. }
  3182. LOCAL_FREE(pwszSD);
  3183. CertSrvExitServer(State);
  3184. return hr;
  3185. }
  3186. // Constructor
  3187. CCertAdminD::CCertAdminD() : m_cRef(1), m_cNext(0)
  3188. {
  3189. InterlockedIncrement(&g_cAdminComponents);
  3190. m_pEnumCol = NULL;
  3191. m_pView = NULL;
  3192. m_pBackup = NULL;
  3193. }
  3194. // Destructor
  3195. CCertAdminD::~CCertAdminD()
  3196. {
  3197. InterlockedDecrement(&g_cAdminComponents);
  3198. if (NULL != m_pEnumCol)
  3199. {
  3200. m_pEnumCol->Release();
  3201. m_pEnumCol = NULL;
  3202. }
  3203. if (NULL != m_pView)
  3204. {
  3205. m_pView->Release();
  3206. m_pView = NULL;
  3207. }
  3208. if (NULL != m_pBackup)
  3209. {
  3210. m_pBackup->Release();
  3211. m_pBackup = NULL;
  3212. }
  3213. }
  3214. // IUnknown implementation
  3215. STDMETHODIMP
  3216. CCertAdminD::QueryInterface(const IID& iid, void** ppv)
  3217. {
  3218. if (iid == IID_IUnknown)
  3219. {
  3220. *ppv = static_cast<ICertAdminD *>(this);
  3221. }
  3222. else if (iid == IID_ICertAdminD)
  3223. {
  3224. *ppv = static_cast<ICertAdminD *>(this);
  3225. }
  3226. else if (iid == IID_ICertAdminD2)
  3227. {
  3228. *ppv = static_cast<ICertAdminD2 *>(this);
  3229. }
  3230. else
  3231. {
  3232. *ppv = NULL;
  3233. return(E_NOINTERFACE);
  3234. }
  3235. reinterpret_cast<IUnknown *>(*ppv)->AddRef();
  3236. return(S_OK);
  3237. }
  3238. ULONG STDMETHODCALLTYPE
  3239. CCertAdminD::AddRef()
  3240. {
  3241. return(InterlockedIncrement(&m_cRef));
  3242. }
  3243. ULONG STDMETHODCALLTYPE
  3244. CCertAdminD::Release()
  3245. {
  3246. ULONG cRef = InterlockedDecrement(&m_cRef);
  3247. if (0 == cRef)
  3248. {
  3249. delete this;
  3250. }
  3251. return(cRef);
  3252. }
  3253. CAdminFactory::~CAdminFactory()
  3254. {
  3255. if (m_cRef != 0)
  3256. {
  3257. DBGPRINT((
  3258. DBG_SS_CERTSRV,
  3259. "CAdminFactory has %d instances left over\n",
  3260. m_cRef));
  3261. }
  3262. }
  3263. // Class factory IUnknown implementation
  3264. STDMETHODIMP
  3265. CAdminFactory::QueryInterface(const IID& iid, void** ppv)
  3266. {
  3267. if ((iid == IID_IUnknown) || (iid == IID_IClassFactory))
  3268. {
  3269. *ppv = static_cast<IClassFactory*>(this);
  3270. }
  3271. else
  3272. {
  3273. *ppv = NULL;
  3274. return(E_NOINTERFACE);
  3275. }
  3276. reinterpret_cast<IUnknown *>(*ppv)->AddRef();
  3277. return(S_OK);
  3278. }
  3279. ULONG STDMETHODCALLTYPE
  3280. CAdminFactory::AddRef()
  3281. {
  3282. return(InterlockedIncrement(&m_cRef));
  3283. }
  3284. ULONG STDMETHODCALLTYPE
  3285. CAdminFactory::Release()
  3286. {
  3287. ULONG cRef = InterlockedDecrement(&m_cRef);
  3288. if (0 == cRef)
  3289. {
  3290. delete this;
  3291. return(0);
  3292. }
  3293. return(cRef);
  3294. }
  3295. // IClassFactory implementation
  3296. STDMETHODIMP
  3297. CAdminFactory::CreateInstance(
  3298. IUnknown *pUnknownOuter,
  3299. const IID& iid,
  3300. void **ppv)
  3301. {
  3302. HRESULT hr;
  3303. CCertAdminD *pA;
  3304. // Cannot aggregate.
  3305. if (pUnknownOuter != NULL)
  3306. {
  3307. hr = CLASS_E_NOAGGREGATION;
  3308. _JumpError(hr, error, "pUnknownOuter");
  3309. }
  3310. // Create component.
  3311. pA = new CCertAdminD;
  3312. if (pA == NULL)
  3313. {
  3314. hr = E_OUTOFMEMORY;
  3315. _JumpError(hr, error, "out of memory");
  3316. }
  3317. // Get the requested interface.
  3318. hr = pA->QueryInterface(iid, ppv);
  3319. // Release the IUnknown pointer.
  3320. // (If QueryInterface failed, component will delete itself.)
  3321. pA->Release();
  3322. error:
  3323. CSASSERT(S_OK == hr || FAILED(hr));
  3324. return(hr);
  3325. }
  3326. // LockServer
  3327. STDMETHODIMP
  3328. CAdminFactory::LockServer(
  3329. BOOL bLock)
  3330. {
  3331. if (bLock)
  3332. {
  3333. InterlockedIncrement(&g_cAdminServerLocks);
  3334. }
  3335. else
  3336. {
  3337. InterlockedDecrement(&g_cAdminServerLocks);
  3338. }
  3339. return(S_OK);
  3340. }
  3341. STDMETHODIMP
  3342. CAdminFactory::CanUnloadNow()
  3343. {
  3344. if (g_cAdminComponents || g_cAdminServerLocks)
  3345. {
  3346. return(S_FALSE);
  3347. }
  3348. return(S_OK);
  3349. }
  3350. STDMETHODIMP
  3351. CAdminFactory::StartFactory()
  3352. {
  3353. HRESULT hr;
  3354. g_pIAdminFactory = new CAdminFactory();
  3355. if (NULL == g_pIAdminFactory)
  3356. {
  3357. hr = E_OUTOFMEMORY;
  3358. _JumpError(hr, error, "alloc CAdminFactory");
  3359. }
  3360. hr = CoRegisterClassObject(
  3361. CLSID_CCertAdminD,
  3362. static_cast<IUnknown *>(g_pIAdminFactory),
  3363. CLSCTX_LOCAL_SERVER,
  3364. REGCLS_MULTIPLEUSE,
  3365. &g_dwAdminRegister);
  3366. _JumpIfError(hr, error, "CoRegisterClassObject");
  3367. error:
  3368. if (S_OK != hr)
  3369. {
  3370. CAdminFactory::StopFactory();
  3371. }
  3372. CSASSERT(S_OK == hr || FAILED(hr));
  3373. return(hr);
  3374. }
  3375. VOID
  3376. CAdminFactory::StopFactory()
  3377. {
  3378. HRESULT hr;
  3379. if (0 != g_dwAdminRegister)
  3380. {
  3381. hr = CoRevokeClassObject(g_dwAdminRegister);
  3382. _PrintIfError(hr, "CoRevokeClassObject");
  3383. g_dwAdminRegister = 0;
  3384. }
  3385. if (NULL != g_pIAdminFactory)
  3386. {
  3387. g_pIAdminFactory->Release();
  3388. g_pIAdminFactory = NULL;
  3389. }
  3390. }
  3391. STDMETHODIMP
  3392. CCertAdminD::GetAuditFilter(
  3393. IN wchar_t const *pwszAuthority,
  3394. OUT DWORD *pdwFilter)
  3395. {
  3396. HRESULT hr;
  3397. DWORD State = 0;
  3398. CAuditEvent audit(0, g_dwAuditFilter);
  3399. *pdwFilter = 0;
  3400. if (!g_fAdvancedServer)
  3401. {
  3402. hr = HRESULT_FROM_WIN32(ERROR_CALL_NOT_IMPLEMENTED);
  3403. _JumpError(hr, error, "g_fAdvancedServer");
  3404. }
  3405. hr = CertSrvEnterServer(&State);
  3406. _JumpIfError(hr, error, "CertSrvEnterServer");
  3407. hr = CheckAuthorityName(pwszAuthority);
  3408. _JumpIfError(hr, error, "No authority name");
  3409. __try
  3410. {
  3411. hr = audit.AccessCheck(
  3412. CA_ACCESS_ALLREADROLES,
  3413. audit.m_gcNoAuditSuccess | audit.m_gcNoAuditFailure);
  3414. _LeaveIfError(hr, "CAuditEvent::AccessCheck");
  3415. *pdwFilter = g_dwAuditFilter;
  3416. }
  3417. __except(hr = myHEXCEPTIONCODE(), EXCEPTION_EXECUTE_HANDLER)
  3418. {
  3419. _PrintError(hr, "Exception");
  3420. }
  3421. error:
  3422. CertSrvExitServer(State);
  3423. return(hr);
  3424. }
  3425. STDMETHODIMP
  3426. CCertAdminD::SetAuditFilter(
  3427. IN wchar_t const *pwszAuthority,
  3428. IN DWORD dwFilter)
  3429. {
  3430. HRESULT hr;
  3431. CAuditEvent audit(SE_AUDITID_CERTSRV_SETAUDITFILTER, g_dwAuditFilter);
  3432. DWORD State = 0;
  3433. if (!g_fAdvancedServer)
  3434. {
  3435. hr = HRESULT_FROM_WIN32(ERROR_CALL_NOT_IMPLEMENTED);
  3436. _JumpError(hr, error, "g_fAdvancedServer");
  3437. }
  3438. hr = CertSrvEnterServer(&State);
  3439. _JumpIfError(hr, error, "CertSrvEnterServer");
  3440. hr = CheckAuthorityName(pwszAuthority);
  3441. _JumpIfError(hr, error, "No authority name");
  3442. __try
  3443. {
  3444. hr = audit.AddData(dwFilter); // %1 filter
  3445. _LeaveIfError(hr, "AddParam");
  3446. hr = audit.AccessCheck(
  3447. CA_ACCESS_AUDITOR,
  3448. audit.m_gcAuditSuccessOrFailure);
  3449. _LeaveIfError(hr, "CAuditEvent::AccessCheck");
  3450. // save the audit filter using a dummy audit object
  3451. {
  3452. CAuditEvent dummyaudit(0, dwFilter);
  3453. hr = dummyaudit.SaveFilter(g_wszSanitizedName);
  3454. _LeaveIfError(hr, "CAuditEvent::SaveFilter");
  3455. }
  3456. g_dwAuditFilter = dwFilter;
  3457. // we can't catch service start/stop events generated
  3458. // by SCM, so we need to update the SACL on the service
  3459. hr = UpdateServiceSacl(g_dwAuditFilter&AUDIT_FILTER_STARTSTOP);
  3460. _LeaveIfError(hr, "UpdateServiceSacl");
  3461. }
  3462. __except(hr = myHEXCEPTIONCODE(), EXCEPTION_EXECUTE_HANDLER)
  3463. {
  3464. _PrintError(hr, "Exception");
  3465. }
  3466. error:
  3467. CertSrvExitServer(State);
  3468. return(hr);
  3469. }
  3470. STDMETHODIMP
  3471. CCertAdminD::GetOfficerRights(
  3472. IN wchar_t const *pwszAuthority,
  3473. OUT BOOL *pfEnabled,
  3474. OUT CERTTRANSBLOB *pctbSD)
  3475. {
  3476. HRESULT hr;
  3477. PSECURITY_DESCRIPTOR pSD = NULL;
  3478. CAuditEvent audit(0, g_dwAuditFilter);
  3479. DWORD State = 0;
  3480. pctbSD->pb = NULL;
  3481. pctbSD->cb = 0;
  3482. DBGPRINT((
  3483. s_ssAdmin,
  3484. "CCertAdminD::GetOfficerRights(tid=%d, this=%x)\n",
  3485. GetCurrentThreadId(),
  3486. this));
  3487. if (!g_fAdvancedServer)
  3488. {
  3489. hr = HRESULT_FROM_WIN32(ERROR_CALL_NOT_IMPLEMENTED);
  3490. _JumpError(hr, error, "g_fAdvancedServer");
  3491. }
  3492. hr = CertSrvEnterServer(&State);
  3493. _JumpIfError(hr, error, "CertSrvEnterServer");
  3494. hr = CheckAuthorityName(pwszAuthority);
  3495. _JumpIfError(hr, error, "CheckAuthorityName");
  3496. __try
  3497. {
  3498. hr = audit.AccessCheck(
  3499. CA_ACCESS_ALLREADROLES,
  3500. audit.m_gcNoAuditSuccess | audit.m_gcNoAuditFailure);
  3501. _LeaveIfError(hr, "CAuditEvent::AccessCheck");
  3502. *pfEnabled = g_OfficerRightsSD.IsEnabled();
  3503. // return the security descriptor only if the feature is enabled
  3504. if (g_OfficerRightsSD.IsEnabled())
  3505. {
  3506. // get current SD:
  3507. hr = g_OfficerRightsSD.LockGet(&pSD); // no free
  3508. _LeaveIfError(hr, "CProtectedSecurityDescriptor::LockGet");
  3509. pctbSD->cb = GetSecurityDescriptorLength(pSD);
  3510. pctbSD->pb = (BYTE *) MIDL_user_allocate(pctbSD->cb);
  3511. if (NULL == pctbSD->pb)
  3512. {
  3513. hr = E_OUTOFMEMORY;
  3514. _LeaveError(hr, "MIDL_user_allocate");
  3515. }
  3516. myRegisterMemFree(pctbSD->pb, CSM_MIDLUSERALLOC);
  3517. CopyMemory(pctbSD->pb, pSD, pctbSD->cb);
  3518. hr = g_OfficerRightsSD.Unlock();
  3519. _LeaveIfError(hr, "CProtectedSecurityDescriptor::Unlock");
  3520. }
  3521. }
  3522. __except(hr = myHEXCEPTIONCODE(), EXCEPTION_EXECUTE_HANDLER)
  3523. {
  3524. _PrintError(hr, "Exception");
  3525. }
  3526. error:
  3527. CertSrvExitServer(State);
  3528. return hr;
  3529. }
  3530. STDMETHODIMP
  3531. CCertAdminD::SetOfficerRights(
  3532. IN wchar_t const *pwszAuthority,
  3533. IN BOOL fEnable,
  3534. IN CERTTRANSBLOB *pctbSD)
  3535. {
  3536. HRESULT hr;
  3537. PSECURITY_DESCRIPTOR pNewOfficerSD = (PSECURITY_DESCRIPTOR) pctbSD->pb;
  3538. PSECURITY_DESCRIPTOR pCASD = NULL;
  3539. LPWSTR pwszSD = NULL;
  3540. CAuditEvent audit(SE_AUDITID_CERTSRV_SETOFFICERRIGHTS, g_dwAuditFilter);
  3541. DWORD State = 0;
  3542. DBGPRINT((
  3543. s_ssAdmin,
  3544. "CCertAdminD::SetOfficerRights(tid=%d, this=%x)\n",
  3545. GetCurrentThreadId(),
  3546. this));
  3547. if (!g_fAdvancedServer)
  3548. {
  3549. hr = HRESULT_FROM_WIN32(ERROR_CALL_NOT_IMPLEMENTED);
  3550. _JumpError(hr, error, "g_fAdvancedServer");
  3551. }
  3552. hr = CertSrvEnterServer(&State);
  3553. _JumpIfError(hr, error, "CertSrvEnterServer");
  3554. hr = CheckAuthorityName(pwszAuthority);
  3555. _JumpIfError(hr, error, "CheckAuthorityName");
  3556. __try
  3557. {
  3558. hr = audit.AddData(fEnable?true:false); // %1 Enable restrictions?
  3559. _LeaveIfError(hr, "CAuditEvent::AddData");
  3560. if(fEnable)
  3561. {
  3562. hr = audit.AddData(pctbSD->pb, pctbSD->cb); // %2 new permissions; add as
  3563. // blob, we don't convert to string
  3564. // unless access check passes
  3565. _LeaveIfError(hr, "CAuditEvent::AddData");
  3566. }
  3567. else
  3568. {
  3569. hr = audit.AddData(L""); // %2 no permissions if disabling
  3570. // the officer restrictions
  3571. _LeaveIfError(hr, "CAuditEvent::AddData");
  3572. }
  3573. hr = audit.AccessCheck(
  3574. CA_ACCESS_ADMIN,
  3575. audit.m_gcNoAuditSuccess);
  3576. _LeaveIfError(hr, "CAuditEvent::AccessCheck");
  3577. g_OfficerRightsSD.SetEnable(fEnable);
  3578. // ignore new security descriptor if asked to turn officer rights off
  3579. if (fEnable)
  3580. {
  3581. hr = g_CASD.LockGet(&pCASD); // no free
  3582. _LeaveIfError(hr, "CProtectedSecurityDescriptor::LockGet");
  3583. // adjust new officer rights based on the CA SD and set the
  3584. // officer rights SD to the new SD
  3585. hr = g_OfficerRightsSD.Merge(pNewOfficerSD, pCASD);
  3586. _LeaveIfError(hr, "COfficerRightsSD::Merge");
  3587. hr = g_CASD.Unlock();
  3588. _LeaveIfError(hr, "CProtectedSecurityDescriptor::Unlock");
  3589. }
  3590. // persistent save to registry
  3591. hr = g_OfficerRightsSD.Save();
  3592. _LeaveIfError(hr, "CProtectedSecurityDescriptor::Save");
  3593. if(fEnable)
  3594. {
  3595. hr = COfficerRightsSD::ConvertToString(pNewOfficerSD, pwszSD);
  3596. _LeaveIfError(hr, "COfficerRightsSD::ConvertToString");
  3597. audit.DeleteLastData(); // remove permissions blob
  3598. hr = audit.AddData(pwszSD); // %2 add human-friend permissions string
  3599. _LeaveIfError(hr, "CAuditEvent::AddData");
  3600. }
  3601. hr = audit.CachedGenerateAudit();
  3602. _LeaveIfError(hr, "CAuditEvent::CachedGenerateAudit");
  3603. }
  3604. __except(hr = myHEXCEPTIONCODE(), EXCEPTION_EXECUTE_HANDLER)
  3605. {
  3606. _PrintError(hr, "Exception");
  3607. }
  3608. error:
  3609. LOCAL_FREE(pwszSD);
  3610. CertSrvExitServer(State);
  3611. return hr;
  3612. }
  3613. STDMETHODIMP
  3614. CCertAdminD::GetConfigEntry(
  3615. wchar_t const *pwszAuthority,
  3616. wchar_t const *pwszNodePath,
  3617. wchar_t const *pwszEntry,
  3618. VARIANT *pVariant)
  3619. {
  3620. HRESULT hr;
  3621. CAuditEvent audit(0, g_dwAuditFilter);
  3622. DWORD State = 0;
  3623. DBGPRINT((
  3624. s_ssAdmin,
  3625. "CCertAdminD::GetConfigEntry(tid=%d, this=%x)\n",
  3626. GetCurrentThreadId(),
  3627. this));
  3628. hr = CertSrvEnterServer(&State);
  3629. _JumpIfError(hr, error, "CertSrvEnterServer");
  3630. hr = CheckAuthorityName(pwszAuthority, true); // allow empty/null name
  3631. _JumpIfError(hr, error, "CheckAuthorityName");
  3632. __try
  3633. {
  3634. hr = audit.AccessCheck(
  3635. CA_ACCESS_ALLREADROLES,
  3636. audit.m_gcNoAuditSuccess | audit.m_gcNoAuditFailure);
  3637. _LeaveIfError(hr, "CAuditEvent::AccessCheck");
  3638. hr = g_ConfigStorage.GetEntry(
  3639. EmptyString(pwszAuthority)?
  3640. NULL : g_wszSanitizedName, // allow empty/null name
  3641. pwszNodePath,
  3642. pwszEntry,
  3643. pVariant);
  3644. _LeaveIfError2(
  3645. hr,
  3646. "CConfigStorage::GetConfigEntry",
  3647. HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND));
  3648. myRegisterMemFree(pVariant, CSM_VARIANT);
  3649. }
  3650. __except(hr = myHEXCEPTIONCODE(), EXCEPTION_EXECUTE_HANDLER)
  3651. {
  3652. _PrintError(hr, "Exception");
  3653. }
  3654. error:
  3655. CertSrvExitServer(State);
  3656. return hr;
  3657. }
  3658. STDMETHODIMP
  3659. CCertAdminD::SetConfigEntry(
  3660. wchar_t const *pwszAuthority,
  3661. wchar_t const *pwszNodePath,
  3662. wchar_t const *pwszEntry,
  3663. VARIANT *pVariant)
  3664. {
  3665. HRESULT hr;
  3666. CAuditEvent audit(SE_AUDITID_CERTSRV_SETCONFIGENTRY, g_dwAuditFilter);
  3667. DWORD State = 0;
  3668. DBGPRINT((
  3669. s_ssAdmin,
  3670. "CCertAdminD::SetConfigEntry(tid=%d, this=%x)\n",
  3671. GetCurrentThreadId(),
  3672. this));
  3673. hr = CertSrvEnterServer(&State);
  3674. _JumpIfError(hr, error, "CertSrvEnterServer");
  3675. hr = CheckAuthorityName(pwszAuthority, true); // allow empty/null name
  3676. _JumpIfError(hr, error, "CheckAuthorityName");
  3677. hr = audit.AddData(pwszNodePath); // %1 node
  3678. _JumpIfError(hr, error, "CAuditEvent::AddData");
  3679. hr = audit.AddData(pwszEntry); // %2 entry
  3680. _JumpIfError(hr, error, "CAuditEvent::AddData");
  3681. hr = audit.AddData(L""); // %3 empty data, we don't process the variant
  3682. // unless the access check passes
  3683. _JumpIfError(hr, error, "CAuditEvent::AddData");
  3684. __try
  3685. {
  3686. hr = audit.AccessCheck(
  3687. CA_ACCESS_ADMIN,
  3688. audit.m_gcNoAuditSuccess);
  3689. _LeaveIfError(hr, "CAuditEvent::AccessCheck");
  3690. hr = g_ConfigStorage.SetEntry(
  3691. EmptyString(pwszAuthority)?
  3692. NULL : g_wszSanitizedName, // allow empty/null name
  3693. pwszNodePath,
  3694. pwszEntry,
  3695. pVariant);
  3696. _LeaveIfError(hr, "CConfigStorage::SetConfigEntry");
  3697. // postpone adding the actual data to allow set entry to validate it
  3698. audit.DeleteLastData();
  3699. hr = audit.AddData(
  3700. pVariant, // %3 value
  3701. true); // true means convert % chars found in strings to %% (bug# 326248)
  3702. _LeaveIfError(hr, "CAuditEvent::AddData");
  3703. hr = audit.CachedGenerateAudit();
  3704. _LeaveIfError(hr, "CAuditEvent::CachedGenerateAudit");
  3705. }
  3706. __except(hr = myHEXCEPTIONCODE(), EXCEPTION_EXECUTE_HANDLER)
  3707. {
  3708. _PrintError(hr, "Exception");
  3709. }
  3710. error:
  3711. CertSrvExitServer(State);
  3712. return hr;
  3713. }
  3714. STDMETHODIMP
  3715. CCertAdminD::GetMyRoles(
  3716. IN wchar_t const *pwszAuthority,
  3717. OUT LONG *pdwRoles)
  3718. {
  3719. HRESULT hr;
  3720. CAuditEvent audit(0, g_dwAuditFilter);
  3721. DWORD dwRoles = 0;
  3722. DWORD State = 0;
  3723. *pdwRoles = 0;
  3724. hr = CertSrvEnterServer(&State);
  3725. _JumpIfError(hr, error, "CertSrvEnterServer");
  3726. hr = CheckAuthorityName(pwszAuthority);
  3727. _JumpIfError(hr, error, "No authority name");
  3728. __try
  3729. {
  3730. hr = audit.GetMyRoles(&dwRoles);
  3731. _LeaveIfError(hr, "CAuditEvent::GetMyRoles");
  3732. *pdwRoles = dwRoles;
  3733. }
  3734. __except(hr = myHEXCEPTIONCODE(), EXCEPTION_EXECUTE_HANDLER)
  3735. {
  3736. _PrintError(hr, "Exception");
  3737. }
  3738. error:
  3739. CertSrvExitServer(State);
  3740. return(hr);
  3741. }
  3742. HRESULT
  3743. adminDeleteRow(
  3744. IN DWORD dwRowId,
  3745. IN DWORD dwPropTable)
  3746. {
  3747. HRESULT hr;
  3748. ICertDBRow *prow = NULL;
  3749. BOOL fCommitted = FALSE;
  3750. hr = g_pCertDB->OpenRow(
  3751. PROPOPEN_DELETE | dwPropTable,
  3752. dwRowId,
  3753. NULL,
  3754. &prow);
  3755. _JumpIfError2(hr, error, "OpenRow", CERTSRV_E_PROPERTY_EMPTY);
  3756. hr = prow->Delete();
  3757. _JumpIfError(hr, error, "Delete");
  3758. hr = prow->CommitTransaction(TRUE);
  3759. _JumpIfError(hr, error, "CommitTransaction");
  3760. fCommitted = TRUE;
  3761. error:
  3762. if (NULL != prow)
  3763. {
  3764. if (S_OK != hr && !fCommitted)
  3765. {
  3766. HRESULT hr2 = prow->CommitTransaction(FALSE);
  3767. _PrintIfError(hr2, "CommitTransaction");
  3768. }
  3769. prow->Release();
  3770. }
  3771. return(hr);
  3772. }
  3773. HRESULT
  3774. adminDeleteByRowId(
  3775. IN DWORD dwRowId,
  3776. IN DWORD dwPropTable,
  3777. OUT LONG *pcDeleted)
  3778. {
  3779. HRESULT hr;
  3780. LONG cDeleted = 0;
  3781. LONG cDeletedExt = 0;
  3782. LONG cDeletedAttr = 0;
  3783. *pcDeleted = 0;
  3784. if (PROPTABLE_REQCERT == dwPropTable)
  3785. {
  3786. hr = adminDeleteByRowId(dwRowId, PROPTABLE_EXTENSION, &cDeletedExt);
  3787. _JumpIfError(hr, error, "adminDeleteByRowId(ext)");
  3788. DBGPRINT((
  3789. DBG_SS_CERTSRV,
  3790. "adminDeleteByRowId(Rowid=%u) deleted %u extension rows\n",
  3791. dwRowId,
  3792. cDeletedExt));
  3793. hr = adminDeleteByRowId(dwRowId, PROPTABLE_ATTRIBUTE, &cDeletedAttr);
  3794. _JumpIfError(hr, error, "adminDeleteByRowId(attrib)");
  3795. DBGPRINT((
  3796. DBG_SS_CERTSRV,
  3797. "adminDeleteByRowId(Rowid=%u) deleted %u attribute rows\n",
  3798. dwRowId,
  3799. cDeletedAttr));
  3800. }
  3801. while (TRUE)
  3802. {
  3803. hr = adminDeleteRow(dwRowId, dwPropTable);
  3804. if (CERTSRV_E_PROPERTY_EMPTY == hr)
  3805. {
  3806. break;
  3807. }
  3808. _JumpIfError(hr, error, "adminDeleteByRowId");
  3809. cDeleted++;
  3810. }
  3811. if (0 == cDeleted && 0 != (cDeletedExt + cDeletedAttr))
  3812. {
  3813. cDeleted++;
  3814. }
  3815. hr = S_OK;
  3816. error:
  3817. *pcDeleted += cDeleted;
  3818. return(hr);
  3819. }
  3820. #define ICOLDEL_DATE 0
  3821. #define ICOLDEL_DISPOSITION 1
  3822. HRESULT
  3823. adminDeleteRowsFromQuery(
  3824. IN DWORD dwPropTable,
  3825. IN DWORD DateColumn,
  3826. IN DWORD DispositionColumn,
  3827. IN BOOL fRequest,
  3828. IN FILETIME const *pft,
  3829. OUT LONG *pcDeleted)
  3830. {
  3831. HRESULT hr;
  3832. CERTVIEWRESTRICTION acvr[1];
  3833. CERTVIEWRESTRICTION *pcvr;
  3834. IEnumCERTDBRESULTROW *pView = NULL;
  3835. DWORD celtFetched;
  3836. DWORD i;
  3837. BOOL fEnd;
  3838. CERTDBRESULTROW aResult[10];
  3839. BOOL fResultActive = FALSE;
  3840. DWORD acol[2];
  3841. DWORD ccol;
  3842. DWORD cDeleted = 0;
  3843. *pcDeleted = 0;
  3844. // Set up restrictions as follows:
  3845. pcvr = acvr;
  3846. // DateColumn < *pft
  3847. pcvr->ColumnIndex = DateColumn;
  3848. pcvr->SeekOperator = CVR_SEEK_LT;
  3849. pcvr->SortOrder = CVR_SORT_ASCEND;
  3850. pcvr->pbValue = (BYTE *) pft;
  3851. pcvr->cbValue = sizeof(*pft);
  3852. pcvr++;
  3853. CSASSERT(ARRAYSIZE(acvr) == SAFE_SUBTRACT_POINTERS(pcvr, acvr));
  3854. ccol = 0;
  3855. acol[ccol++] = DateColumn;
  3856. if (0 != DispositionColumn)
  3857. {
  3858. acol[ccol++] = DispositionColumn;
  3859. }
  3860. hr = g_pCertDB->OpenView(
  3861. ARRAYSIZE(acvr),
  3862. acvr,
  3863. ccol,
  3864. acol,
  3865. 0, // no worker thread
  3866. &pView);
  3867. _JumpIfError(hr, error, "OpenView");
  3868. fEnd = FALSE;
  3869. while (!fEnd)
  3870. {
  3871. hr = pView->Next(ARRAYSIZE(aResult), aResult, &celtFetched);
  3872. if (S_FALSE == hr)
  3873. {
  3874. fEnd = TRUE;
  3875. if (0 == celtFetched)
  3876. {
  3877. break;
  3878. }
  3879. hr = S_OK;
  3880. }
  3881. _JumpIfError(hr, error, "Next");
  3882. fResultActive = TRUE;
  3883. CSASSERT(ARRAYSIZE(aResult) >= celtFetched);
  3884. for (i = 0; i < celtFetched; i++)
  3885. {
  3886. BOOL fDelete = TRUE;
  3887. CERTDBRESULTROW *pResult = &aResult[i];
  3888. CSASSERT(ccol == pResult->ccol);
  3889. if (0 != DispositionColumn)
  3890. {
  3891. DWORD Disposition;
  3892. CSASSERT(NULL != pResult->acol[ICOLDEL_DISPOSITION].pbValue);
  3893. CSASSERT(PROPTYPE_LONG == (PROPTYPE_MASK & pResult->acol[ICOLDEL_DISPOSITION].Type));
  3894. CSASSERT(sizeof(Disposition) == pResult->acol[ICOLDEL_DISPOSITION].cbValue);
  3895. Disposition = *(DWORD *) pResult->acol[ICOLDEL_DISPOSITION].pbValue;
  3896. if (fRequest)
  3897. {
  3898. // Delete only pending and failed requests
  3899. if (DB_DISP_PENDING != Disposition &&
  3900. DB_DISP_LOG_FAILED_MIN > Disposition)
  3901. {
  3902. fDelete = FALSE;
  3903. }
  3904. }
  3905. else
  3906. {
  3907. // Delete only issued and revoked certs
  3908. if (DB_DISP_LOG_MIN > Disposition ||
  3909. DB_DISP_LOG_FAILED_MIN <= Disposition)
  3910. {
  3911. fDelete = FALSE;
  3912. }
  3913. }
  3914. }
  3915. CSASSERT(PROPTYPE_DATE == (PROPTYPE_MASK & pResult->acol[ICOLDEL_DATE].Type));
  3916. // If the date column is missing, delete the row.
  3917. #ifdef DBG_CERTSRV_DEBUG_PRINT
  3918. if (NULL != pResult->acol[ICOLDEL_DATE].pbValue &&
  3919. sizeof(FILETIME) == pResult->acol[ICOLDEL_DATE].cbValue)
  3920. {
  3921. WCHAR *pwszTime = NULL;
  3922. myGMTFileTimeToWszLocalTime(
  3923. (FILETIME *) pResult->acol[ICOLDEL_DATE].pbValue,
  3924. TRUE,
  3925. &pwszTime);
  3926. DBGPRINT((
  3927. DBG_SS_CERTSRV,
  3928. "adminDeleteRowsFromQuery(%ws)\n",
  3929. pwszTime));
  3930. if (NULL != pwszTime)
  3931. {
  3932. LocalFree(pwszTime);
  3933. }
  3934. }
  3935. #endif // DBG_CERTSRV_DEBUG_PRINT
  3936. if (fDelete)
  3937. {
  3938. LONG cDelT;
  3939. hr = adminDeleteByRowId(pResult->rowid, dwPropTable, &cDelT);
  3940. _JumpIfError(hr, error, "adminDeleteByRowId");
  3941. DBGPRINT((
  3942. DBG_SS_CERTSRV,
  3943. "adminDeleteByRowId(Rowid=%u) deleted %u Query rows\n",
  3944. pResult->rowid,
  3945. cDelT));
  3946. cDeleted += cDelT;
  3947. }
  3948. }
  3949. pView->ReleaseResultRow(celtFetched, aResult);
  3950. fResultActive = FALSE;
  3951. }
  3952. hr = S_OK;
  3953. error:
  3954. *pcDeleted = cDeleted;
  3955. if (NULL != pView)
  3956. {
  3957. if (fResultActive)
  3958. {
  3959. pView->ReleaseResultRow(celtFetched, aResult);
  3960. }
  3961. pView->Release();
  3962. }
  3963. return(hr);
  3964. }
  3965. #undef ICOLDEL_DATE
  3966. #undef ICOLDEL_DISPOSITION
  3967. STDMETHODIMP
  3968. CCertAdminD::DeleteRow(
  3969. IN wchar_t const *pwszAuthority,
  3970. IN DWORD dwFlags, // CDR_*
  3971. IN FILETIME FileTime,
  3972. IN DWORD dwTable, // CVRC_TABLE_*
  3973. IN DWORD dwRowId,
  3974. OUT LONG *pcDeleted)
  3975. {
  3976. HRESULT hr;
  3977. DWORD dwPropTable;
  3978. CAuditEvent audit(SE_AUDITID_CERTSRV_DELETEROW, g_dwAuditFilter);
  3979. DWORD DateColumn;
  3980. DWORD DispositionColumn;
  3981. BOOL fRequest;
  3982. DWORD State = 0;
  3983. *pcDeleted = 0;
  3984. hr = CertSrvEnterServer(&State);
  3985. _JumpIfError(hr, error, "CertSrvEnterServer");
  3986. hr = CheckAuthorityName(pwszAuthority);
  3987. _JumpIfError(hr, error, "No authority name");
  3988. __try
  3989. {
  3990. hr = audit.AddData(dwTable); // %1 table ID
  3991. _JumpIfError(hr, error, "CAuditEvent::AddData");
  3992. if (0 == dwRowId)
  3993. {
  3994. hr = audit.AddData(FileTime); // %2 filter (time)
  3995. _JumpIfError(hr, error, "CAuditEvent::AddData");
  3996. hr = audit.AddData((DWORD)0); // %3 rows deleted
  3997. _JumpIfError(hr, error, "CAuditEvent::AddData");
  3998. // bulk deletion -- must be local admin
  3999. hr = audit.AccessCheck(
  4000. CA_ACCESS_LOCALADMIN,
  4001. audit.m_gcNoAuditSuccess);
  4002. _LeaveIfError(hr, "CAuditEvent::AccessCheck");
  4003. }
  4004. else
  4005. {
  4006. hr = audit.AddData(dwRowId); // %2 filter (request ID)
  4007. _JumpIfError(hr, error, "CAuditEvent::AddData");
  4008. hr = audit.AddData((DWORD)0); // %3 rows deleted
  4009. _JumpIfError(hr, error, "CAuditEvent::AddData");
  4010. // individual deletion -- CA admin suffices
  4011. hr = audit.AccessCheck(
  4012. CA_ACCESS_ADMIN,
  4013. audit.m_gcNoAuditSuccess);
  4014. _LeaveIfError(hr, "CAuditEvent::AccessCheck");
  4015. }
  4016. hr = E_INVALIDARG;
  4017. if ((0 == FileTime.dwLowDateTime && 0 == FileTime.dwHighDateTime) ^
  4018. (0 != dwRowId))
  4019. {
  4020. _LeaveError(hr, "row OR date required");
  4021. }
  4022. DateColumn = 0;
  4023. DispositionColumn = 0;
  4024. fRequest = FALSE;
  4025. switch (dwTable)
  4026. {
  4027. case CVRC_TABLE_REQCERT:
  4028. dwPropTable = PROPTABLE_REQCERT;
  4029. switch (dwFlags)
  4030. {
  4031. case CDR_EXPIRED:
  4032. DateColumn = DTI_CERTIFICATETABLE | DTC_CERTIFICATENOTAFTERDATE;
  4033. DispositionColumn = DTI_REQUESTTABLE | DTR_REQUESTDISPOSITION;
  4034. break;
  4035. case CDR_REQUEST_LAST_CHANGED:
  4036. DateColumn = DTI_REQUESTTABLE | DTR_REQUESTRESOLVEDWHEN;
  4037. DispositionColumn = DTI_REQUESTTABLE | DTR_REQUESTDISPOSITION;
  4038. fRequest = TRUE;
  4039. break;
  4040. case 0:
  4041. break;
  4042. default:
  4043. _LeaveError(hr, "dwFlags");
  4044. break;
  4045. }
  4046. break;
  4047. case CVRC_TABLE_EXTENSIONS:
  4048. if (0 == dwRowId)
  4049. {
  4050. _LeaveError(hr, "no date field in Extension table");
  4051. }
  4052. if (0 != dwFlags)
  4053. {
  4054. _LeaveError(hr, "dwFlags");
  4055. }
  4056. dwPropTable = PROPTABLE_EXTENSION;
  4057. break;
  4058. case CVRC_TABLE_ATTRIBUTES:
  4059. if (0 == dwRowId)
  4060. {
  4061. _LeaveError(hr, "no date field in Request Attribute table");
  4062. }
  4063. if (0 != dwFlags)
  4064. {
  4065. _LeaveError(hr, "dwFlags");
  4066. }
  4067. dwPropTable = PROPTABLE_ATTRIBUTE;
  4068. break;
  4069. case CVRC_TABLE_CRL:
  4070. dwPropTable = PROPTABLE_CRL;
  4071. switch (dwFlags)
  4072. {
  4073. case CDR_EXPIRED:
  4074. DateColumn = DTI_CERTIFICATETABLE | DTC_CERTIFICATENOTAFTERDATE;
  4075. break;
  4076. case 0:
  4077. break;
  4078. default:
  4079. _LeaveError(hr, "dwFlags");
  4080. break;
  4081. }
  4082. DateColumn = DTI_CRLTABLE | DTL_NEXTUPDATEDATE;
  4083. break;
  4084. default:
  4085. _LeaveError(hr, "dwTable");
  4086. }
  4087. if (0 != dwRowId)
  4088. {
  4089. hr = adminDeleteByRowId(dwRowId, dwPropTable, pcDeleted);
  4090. _LeaveIfError(hr, "adminDeleteByRowId");
  4091. DBGPRINT((
  4092. DBG_SS_CERTSRV,
  4093. "adminDeleteByRowId(Rowid=%u) deleted %u rows\n",
  4094. dwRowId,
  4095. *pcDeleted));
  4096. }
  4097. else
  4098. {
  4099. CSASSERT(0 != DateColumn);
  4100. hr = adminDeleteRowsFromQuery(
  4101. dwPropTable,
  4102. DateColumn,
  4103. DispositionColumn,
  4104. fRequest,
  4105. &FileTime,
  4106. pcDeleted);
  4107. _LeaveIfError(hr, "adminDeleteRowsFromQuery");
  4108. }
  4109. audit.DeleteLastData();
  4110. hr = audit.AddData((DWORD)*pcDeleted); // %3 rows deleted
  4111. _JumpIfError(hr, error, "CAuditEvent::AddData");
  4112. hr = audit.CachedGenerateAudit();
  4113. _JumpIfError(hr, error, "CAuditEvent::CachedGenerateAudit");
  4114. }
  4115. __except(hr = myHEXCEPTIONCODE(), EXCEPTION_EXECUTE_HANDLER)
  4116. {
  4117. _PrintError(hr, "Exception");
  4118. }
  4119. error:
  4120. CertSrvExitServer(State);
  4121. return(hr);
  4122. }