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.

554 lines
16 KiB

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