Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

535 lines
15 KiB

  1. //+--------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1996 - 1999
  5. //
  6. // File: module.cpp
  7. //
  8. // Contents: Cert Server Exit Module implementation
  9. //
  10. //---------------------------------------------------------------------------
  11. #include "pch.cpp"
  12. #pragma hdrstop
  13. #include <commctrl.h>
  14. #include "module.h"
  15. #include "exit.h"
  16. #include "cslistvw.h"
  17. #include <ntverp.h>
  18. #include <common.ver>
  19. #include "csdisp.h"
  20. // helpids
  21. #include "csmmchlp.h"
  22. #define __dwFILE__ __dwFILE_EXIT_DEFAULT_MODULE_CPP__
  23. #define ARRAYLEN(x) (sizeof(x) / sizeof((x)[0]))
  24. extern HINSTANCE g_hInstance;
  25. STDMETHODIMP
  26. CCertManageExitModule::GetProperty(
  27. /* [in] */ const BSTR, // strConfig
  28. /* [in] */ BSTR, // strStorageLocation
  29. /* [in] */ BSTR strPropertyName,
  30. /* [in] */ LONG, // dwFlags
  31. /* [retval][out] */ VARIANT __RPC_FAR *pvarProperty)
  32. {
  33. UINT uiStr = 0;
  34. HRESULT hr;
  35. if (NULL == pvarProperty)
  36. {
  37. hr = E_POINTER;
  38. _PrintError(hr, "NULL parm");
  39. return hr;
  40. }
  41. VariantInit(pvarProperty);
  42. if (NULL == strPropertyName)
  43. {
  44. hr = S_FALSE;
  45. _PrintError(hr, "NULL in parm");
  46. return hr;
  47. }
  48. // load string from resource
  49. WCHAR szStr[MAX_PATH];
  50. szStr[0] = L'\0';
  51. if (0 == mylstrcmpiL(strPropertyName, wszCMM_PROP_FILEVER))
  52. {
  53. LPWSTR pwszTmp = NULL;
  54. if (!ConvertSzToWsz(&pwszTmp, VER_FILEVERSION_STR, -1))
  55. return myHLastError();
  56. wcsncpy(szStr, pwszTmp, MAX_PATH);
  57. LocalFree(pwszTmp);
  58. }
  59. else if (0 == mylstrcmpiL(strPropertyName, wszCMM_PROP_PRODUCTVER))
  60. {
  61. LPWSTR pwszTmp = NULL;
  62. if (!ConvertSzToWsz(&pwszTmp, VER_PRODUCTVERSION_STR, -1))
  63. return myHLastError();
  64. wcsncpy(szStr, pwszTmp, MAX_PATH);
  65. LocalFree(pwszTmp);
  66. }
  67. else
  68. {
  69. if (0 == LSTRCMPIS(strPropertyName, wszCMM_PROP_NAME))
  70. uiStr = IDS_MODULE_NAME;
  71. else if (0 == LSTRCMPIS(strPropertyName, wszCMM_PROP_DESCRIPTION))
  72. uiStr = IDS_MODULE_DESCR;
  73. else if (0 == LSTRCMPIS(strPropertyName, wszCMM_PROP_COPYRIGHT))
  74. uiStr = IDS_MODULE_COPYRIGHT;
  75. else
  76. return S_FALSE;
  77. LoadString(g_hInstance, uiStr, szStr, ARRAYLEN(szStr));
  78. }
  79. szStr[MAX_PATH-1] = L'\0';
  80. pvarProperty->bstrVal = SysAllocString(szStr);
  81. if (NULL == pvarProperty->bstrVal)
  82. return E_OUTOFMEMORY;
  83. myRegisterMemFree(pvarProperty->bstrVal, CSM_SYSALLOC); // this mem owned by caller
  84. pvarProperty->vt = VT_BSTR;
  85. return S_OK;
  86. }
  87. STDMETHODIMP
  88. CCertManageExitModule::SetProperty(
  89. /* [in] */ const BSTR, // strConfig
  90. /* [in] */ BSTR, // strStorageLocation
  91. /* [in] */ BSTR strPropertyName,
  92. /* [in] */ LONG, // dwFlags
  93. /* [in] */ VARIANT const __RPC_FAR *pvalProperty)
  94. {
  95. HRESULT hr;
  96. if (NULL == strPropertyName)
  97. {
  98. hr = S_FALSE;
  99. _PrintError(hr, "NULL in parm");
  100. return hr;
  101. }
  102. if (NULL == pvalProperty)
  103. {
  104. hr = E_POINTER;
  105. _PrintError(hr, "NULL parm");
  106. return hr;
  107. }
  108. if (0 == LSTRCMPIS(strPropertyName, wszCMM_PROP_DISPLAY_HWND))
  109. {
  110. if (pvalProperty->vt != VT_BSTR)
  111. return E_INVALIDARG;
  112. if (SysStringByteLen(pvalProperty->bstrVal) != sizeof(HWND))
  113. return E_INVALIDARG;
  114. // the value is stored as bytes in the bstr itself, not the bstr ptr
  115. m_hWnd = *(HWND*)pvalProperty->bstrVal;
  116. return S_OK;
  117. }
  118. return S_FALSE;
  119. }
  120. INT_PTR CALLBACK WizPage2DlgProc(
  121. HWND hwnd,
  122. UINT uMsg,
  123. WPARAM wParam,
  124. LPARAM lParam);
  125. struct EXIT_CONFIGSTRUCT
  126. {
  127. EXIT_CONFIGSTRUCT() :
  128. pstrConfig(NULL),
  129. CAType(ENUM_UNKNOWN_CA),
  130. pCertAdmin(NULL),
  131. fUseDS(FALSE),
  132. Flags(),
  133. dwPageModified(0) {}
  134. ~EXIT_CONFIGSTRUCT()
  135. {
  136. if(pCertAdmin)
  137. {
  138. pCertAdmin->Release();
  139. pCertAdmin = NULL;
  140. }
  141. }
  142. const BSTR* pstrConfig;
  143. CString strSanitizedConfig;
  144. ENUM_CATYPES CAType;
  145. BOOL fUseDS;
  146. ICertAdmin2 *pCertAdmin;
  147. LONG Flags;
  148. DWORD dwPageModified;
  149. };
  150. typedef EXIT_CONFIGSTRUCT *PEXIT_CONFIGSTRUCT;
  151. void MessageBoxWarnReboot(HWND hwndDlg)
  152. {
  153. WCHAR szText[MAX_PATH], szTitle[MAX_PATH];
  154. LoadString(g_hInstance, IDS_MODULE_NAME, szTitle, ARRAYLEN(szTitle));
  155. LoadString(g_hInstance, IDS_WARNING_REBOOT, szText, ARRAYLEN(szText));
  156. MessageBox(hwndDlg, szText, szTitle, MB_OK|MB_ICONINFORMATION);
  157. }
  158. void MessageBoxNoSave(HWND hwndDlg)
  159. {
  160. WCHAR szText[MAX_PATH], szTitle[MAX_PATH];
  161. LoadString(g_hInstance, IDS_MODULE_NAME, szTitle, ARRAYLEN(szTitle));
  162. LoadString(g_hInstance, IDS_WARNING_NOSAVE, szText, ARRAYLEN(szText));
  163. MessageBox(hwndDlg, szText, szTitle, MB_OK|MB_ICONINFORMATION);
  164. }
  165. // dwPageModified
  166. #define PAGE1 (0x1)
  167. #define PAGE2 (0x2)
  168. STDMETHODIMP
  169. CCertManageExitModule::Configure(
  170. /* [in] */ const BSTR strConfig,
  171. /* [in] */ BSTR, // strStorageLocation
  172. /* [in] */ LONG dwFlags)
  173. {
  174. HRESULT hr;
  175. EXIT_CONFIGSTRUCT sConfig;
  176. VARIANT varValue;
  177. VariantInit(&varValue);
  178. ICertServerExit *pServer = NULL;
  179. BOOL fLocal;
  180. LPWSTR szMachine = NULL;
  181. CAutoLPWSTR autoszMachine, autoszCAName, autoszSanitizedCAName;
  182. hr = myIsConfigLocal(strConfig, &szMachine, &fLocal);
  183. _JumpIfError(hr, Ret, "myIsConfigLocal");
  184. // use callbacks for info
  185. hr = GetServerCallbackInterface(&pServer, 0); // no context: 0
  186. _JumpIfError(hr, Ret, "GetServerCallbackInterface");
  187. // we need to find out who we're running under
  188. hr = exitGetProperty(
  189. pServer,
  190. FALSE,
  191. wszPROPCATYPE,
  192. PROPTYPE_LONG,
  193. &varValue);
  194. _JumpIfError(hr, Ret, "exitGetCertificateProperty: wszPROPCATYPE");
  195. hr = GetAdmin(&sConfig.pCertAdmin);
  196. _JumpIfError(hr, Ret, "GetAdmin");
  197. sConfig.CAType = (ENUM_CATYPES)varValue.lVal;
  198. VariantClear(&varValue);
  199. hr = exitGetProperty(
  200. pServer,
  201. FALSE,
  202. wszPROPUSEDS,
  203. PROPTYPE_LONG,
  204. &varValue);
  205. _JumpIfError(hr, Ret, "exitGetCertificateProperty: wszPROPUSEDS");
  206. sConfig.fUseDS = (BOOL)varValue.lVal;
  207. VariantClear(&varValue);
  208. sConfig.pstrConfig = &strConfig;
  209. sConfig.Flags = dwFlags;
  210. hr = mySplitConfigString(
  211. *sConfig.pstrConfig,
  212. &autoszMachine,
  213. &autoszCAName);
  214. _JumpIfErrorStr(hr, Ret, "mySanitizeName", *sConfig.pstrConfig);
  215. hr = mySanitizeName(autoszCAName, &autoszSanitizedCAName);
  216. _JumpIfErrorStr(hr, Ret, "mySanitizeName", autoszCAName);
  217. sConfig.strSanitizedConfig = autoszMachine;
  218. sConfig.strSanitizedConfig += L"\\";
  219. sConfig.strSanitizedConfig += autoszSanitizedCAName;
  220. PROPSHEETPAGE page[1];
  221. ZeroMemory(&page[0], sizeof(PROPSHEETPAGE));
  222. page[0].dwSize = sizeof(PROPSHEETPAGE);
  223. page[0].dwFlags = PSP_DEFAULT;
  224. page[0].hInstance = g_hInstance;
  225. page[0].lParam = (LPARAM)&sConfig;
  226. page[0].pszTemplate = MAKEINTRESOURCE(IDD_EXITPG2);
  227. page[0].pfnDlgProc = WizPage2DlgProc;
  228. PROPSHEETHEADER sSheet;
  229. ZeroMemory(&sSheet, sizeof(PROPSHEETHEADER));
  230. sSheet.dwSize = sizeof(PROPSHEETHEADER);
  231. sSheet.dwFlags = PSH_PROPSHEETPAGE | PSH_PROPTITLE;
  232. sSheet.hwndParent = m_hWnd;
  233. sSheet.pszCaption = MAKEINTRESOURCE(IDS_MODULE_NAME);
  234. sSheet.nPages = ARRAYLEN(page);
  235. sSheet.ppsp = page;
  236. // finally, invoke the modal sheet
  237. INT_PTR iRet;
  238. iRet = ::PropertySheet(&sSheet);
  239. Ret:
  240. if (szMachine)
  241. LocalFree(szMachine);
  242. if (pServer)
  243. pServer->Release();
  244. return S_OK;
  245. }
  246. void mySetModified(HWND hwndPage, EXIT_CONFIGSTRUCT* psConfig)
  247. {
  248. if (psConfig->dwPageModified != 0)
  249. {
  250. PropSheet_Changed( ::GetParent(hwndPage), hwndPage);
  251. }
  252. else
  253. {
  254. PropSheet_UnChanged( ::GetParent(hwndPage), hwndPage);
  255. }
  256. }
  257. INT_PTR CALLBACK WizPage2DlgProc(
  258. HWND hwndDlg,
  259. UINT uMsg,
  260. WPARAM wParam,
  261. LPARAM lParam)
  262. {
  263. EXIT_CONFIGSTRUCT* psConfig;
  264. BOOL fReturn = FALSE;
  265. HRESULT hr;
  266. switch(uMsg)
  267. {
  268. case WM_INITDIALOG:
  269. {
  270. ::SetWindowLong(hwndDlg, GWL_EXSTYLE, ::GetWindowLong(hwndDlg, GWL_EXSTYLE) | WS_EX_CONTEXTHELP);
  271. PROPSHEETPAGE* ps = (PROPSHEETPAGE *) lParam;
  272. psConfig = (EXIT_CONFIGSTRUCT*)ps->lParam;
  273. SetWindowLongPtr(hwndDlg, GWLP_USERDATA, (LPARAM)psConfig);
  274. DWORD dwPublish;
  275. CAutoBSTR bstrConfig, bstrSubkey, bstrValue;
  276. bstrConfig = SysAllocString(psConfig->strSanitizedConfig.GetBuffer());
  277. if(NULL == (BSTR)bstrConfig)
  278. {
  279. hr = E_OUTOFMEMORY;
  280. break;
  281. }
  282. bstrSubkey = SysAllocString(
  283. wszREGKEYEXITMODULES
  284. L"\\"
  285. wszMICROSOFTCERTMODULE_PREFIX
  286. wszCERTEXITMODULE_POSTFIX);
  287. if(NULL == (BSTR)bstrSubkey)
  288. {
  289. hr = E_OUTOFMEMORY;
  290. break;
  291. }
  292. bstrValue = SysAllocString(wszREGCERTPUBLISHFLAGS);
  293. if(NULL == (BSTR)bstrValue)
  294. {
  295. hr = E_OUTOFMEMORY;
  296. break;
  297. }
  298. VARIANT var;
  299. VariantInit(&var);
  300. hr = psConfig->pCertAdmin->GetConfigEntry(
  301. bstrConfig,
  302. bstrSubkey,
  303. bstrValue,
  304. &var);
  305. if(S_OK!=hr)
  306. break;
  307. dwPublish = V_I4(&var);
  308. // if disposition includes Issue
  309. if (dwPublish & EXITPUB_FILE)
  310. {
  311. SendMessage(GetDlgItem(hwndDlg, IDC_CHECK_FILE), BM_SETCHECK, TRUE, BST_CHECKED);
  312. }
  313. if (CMM_READONLY & psConfig->Flags)
  314. {
  315. DBGPRINT((DBG_SS_CERTPOL, "Read-only mode\n"));
  316. EnableWindow(GetDlgItem(hwndDlg, IDC_CHECK_FILE), FALSE);
  317. }
  318. psConfig->dwPageModified &= ~PAGE2; // we're virgin
  319. mySetModified(hwndDlg, psConfig);
  320. // no other work to be done
  321. fReturn = TRUE;
  322. break;
  323. }
  324. case WM_HELP:
  325. {
  326. OnDialogHelp((LPHELPINFO) lParam, CERTMMC_HELPFILENAME, g_aHelpIDs_IDD_EXITPG2);
  327. break;
  328. }
  329. case WM_CONTEXTMENU:
  330. {
  331. OnDialogContextHelp((HWND)wParam, CERTMMC_HELPFILENAME, g_aHelpIDs_IDD_EXITPG2);
  332. break;
  333. }
  334. case WM_NOTIFY:
  335. switch( ((LPNMHDR)lParam) -> code)
  336. {
  337. case PSN_APPLY:
  338. {
  339. // grab our LParam
  340. psConfig = (EXIT_CONFIGSTRUCT*)GetWindowLongPtr(hwndDlg, GWLP_USERDATA);
  341. if (psConfig == NULL)
  342. break;
  343. if (psConfig->dwPageModified & PAGE2)
  344. {
  345. DWORD dwCheckState, dwRequestDisposition=0;
  346. dwCheckState = (DWORD)SendMessage(GetDlgItem(hwndDlg, IDC_CHECK_FILE), BM_GETCHECK, 0, 0);
  347. if (dwCheckState == BST_CHECKED)
  348. dwRequestDisposition |= EXITPUB_FILE;
  349. CAutoBSTR bstrConfig, bstrSubkey, bstrValue;
  350. bstrConfig = SysAllocString(psConfig->strSanitizedConfig.GetBuffer());
  351. if(NULL == (BSTR)bstrConfig)
  352. {
  353. hr = E_OUTOFMEMORY;
  354. break;
  355. }
  356. bstrSubkey = SysAllocString(
  357. wszREGKEYEXITMODULES
  358. L"\\"
  359. wszMICROSOFTCERTMODULE_PREFIX
  360. wszCERTEXITMODULE_POSTFIX);
  361. if(NULL == (BSTR)bstrSubkey)
  362. {
  363. hr = E_OUTOFMEMORY;
  364. break;
  365. }
  366. bstrValue = SysAllocString(wszREGCERTPUBLISHFLAGS);
  367. if(NULL == (BSTR)bstrValue)
  368. {
  369. hr = E_OUTOFMEMORY;
  370. break;
  371. }
  372. VARIANT var;
  373. VariantInit(&var);
  374. V_VT(&var) = VT_I4;
  375. V_I4(&var) = dwRequestDisposition;
  376. hr = psConfig->pCertAdmin->SetConfigEntry(
  377. bstrConfig,
  378. bstrSubkey,
  379. bstrValue,
  380. &var);
  381. if(S_OK!=hr)
  382. {
  383. MessageBoxNoSave(hwndDlg);
  384. }
  385. else
  386. {
  387. MessageBoxWarnReboot(NULL);
  388. psConfig->dwPageModified &= ~PAGE2;
  389. }
  390. }
  391. }
  392. break;
  393. case PSN_RESET:
  394. {
  395. // grab our LParam
  396. psConfig = (EXIT_CONFIGSTRUCT*)GetWindowLongPtr(hwndDlg, GWLP_USERDATA);
  397. if (psConfig == NULL)
  398. break;
  399. psConfig->dwPageModified &= ~PAGE2;
  400. mySetModified(hwndDlg, psConfig);
  401. }
  402. break;
  403. default:
  404. break;
  405. }
  406. break;
  407. case WM_COMMAND:
  408. switch (LOWORD(wParam))
  409. {
  410. case IDC_CHECK_FILE:
  411. {
  412. // grab our LParam
  413. psConfig = (EXIT_CONFIGSTRUCT*)GetWindowLongPtr(hwndDlg, GWLP_USERDATA);
  414. if (psConfig == NULL)
  415. break;
  416. if (BN_CLICKED == HIWORD(wParam))
  417. {
  418. psConfig->dwPageModified |= PAGE2;
  419. mySetModified(hwndDlg, psConfig);
  420. }
  421. }
  422. break;
  423. default:
  424. break;
  425. }
  426. default:
  427. break;
  428. }
  429. return fReturn;
  430. }
  431. HRESULT CCertManageExitModule::GetAdmin(ICertAdmin2 **ppAdmin)
  432. {
  433. HRESULT hr = S_OK, hr1;
  434. BOOL fCoInit = FALSE;
  435. hr1 = CoInitialize(NULL);
  436. if ((S_OK == hr1) || (S_FALSE == hr1))
  437. fCoInit = TRUE;
  438. // create interface, pass back
  439. hr = CoCreateInstance(
  440. CLSID_CCertAdmin,
  441. NULL, // pUnkOuter
  442. CLSCTX_INPROC_SERVER,
  443. IID_ICertAdmin2,
  444. (void **) ppAdmin);
  445. _PrintIfError(hr, "CoCreateInstance");
  446. if (fCoInit)
  447. CoUninitialize();
  448. return hr;
  449. }