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.

426 lines
10 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 <assert.h>
  14. #include "celib.h"
  15. #include "module.h"
  16. #include "exit.h"
  17. extern HINSTANCE g_hInstance;
  18. INT_PTR CALLBACK ExitSQLDlgProc(
  19. HWND hwndDlg,
  20. UINT uMsg,
  21. WPARAM wParam,
  22. LPARAM lParam);
  23. HRESULT
  24. GetMachineFromConfig(
  25. IN WCHAR const *pwszConfig,
  26. OUT WCHAR **ppwszMachine)
  27. {
  28. HRESULT hr;
  29. WCHAR *pwszMachine = NULL;
  30. WCHAR const *pwsz;
  31. DWORD cwc;
  32. if (NULL != ppwszMachine)
  33. {
  34. *ppwszMachine = NULL;
  35. }
  36. while (L'\\' == *pwszConfig)
  37. {
  38. pwszConfig++;
  39. }
  40. pwsz = wcschr(pwszConfig, L'\\');
  41. if (NULL != pwsz)
  42. {
  43. cwc = SAFE_SUBTRACT_POINTERS(pwsz, pwszConfig);
  44. }
  45. else
  46. {
  47. cwc = wcslen(pwszConfig);
  48. }
  49. pwszMachine = (WCHAR *) LocalAlloc(LMEM_FIXED, (cwc + 1) * sizeof(WCHAR));
  50. if (NULL == pwszMachine)
  51. {
  52. hr = E_OUTOFMEMORY;
  53. _JumpError(hr, error, "LocalAlloc");
  54. }
  55. CopyMemory(pwszMachine, pwszConfig, cwc * sizeof(WCHAR));
  56. pwszMachine[cwc] = L'\0';
  57. if (NULL != ppwszMachine)
  58. {
  59. *ppwszMachine = pwszMachine;
  60. pwszMachine = NULL;
  61. }
  62. hr = S_OK;
  63. error:
  64. if (NULL != pwszMachine)
  65. {
  66. LocalFree(pwszMachine);
  67. }
  68. return(hr);
  69. }
  70. STDMETHODIMP
  71. CCertManageExitModuleSQLSample::GetProperty(
  72. /* [in] */ const BSTR strConfig,
  73. /* [in] */ BSTR strStorageLocation,
  74. /* [in] */ BSTR strPropertyName,
  75. /* [in] */ LONG Flags,
  76. /* [retval][out] */ VARIANT __RPC_FAR *pvarProperty)
  77. {
  78. LPWSTR szStr = NULL;
  79. if (pvarProperty == NULL)
  80. return E_POINTER;
  81. VariantInit(pvarProperty);
  82. if (strPropertyName == NULL)
  83. return S_FALSE;
  84. if (0 == _wcsicmp(strPropertyName, wszCMM_PROP_NAME))
  85. szStr = wsz_SAMPLE_NAME;
  86. else if (0 == _wcsicmp(strPropertyName, wszCMM_PROP_DESCRIPTION))
  87. szStr = wsz_SAMPLE_DESCRIPTION;
  88. else if (0 == _wcsicmp(strPropertyName, wszCMM_PROP_COPYRIGHT))
  89. szStr = wsz_SAMPLE_COPYRIGHT;
  90. else if (0 == _wcsicmp(strPropertyName, wszCMM_PROP_FILEVER))
  91. szStr = wsz_SAMPLE_FILEVER;
  92. else if (0 == _wcsicmp(strPropertyName, wszCMM_PROP_PRODUCTVER))
  93. szStr = wsz_SAMPLE_PRODUCTVER;
  94. else
  95. return S_FALSE;
  96. pvarProperty->bstrVal = SysAllocString(szStr);
  97. if (NULL == pvarProperty->bstrVal)
  98. return E_OUTOFMEMORY;
  99. pvarProperty->vt = VT_BSTR;
  100. return S_OK;
  101. }
  102. STDMETHODIMP
  103. CCertManageExitModuleSQLSample::SetProperty(
  104. /* [in] */ const BSTR strConfig,
  105. /* [in] */ BSTR strStorageLocation,
  106. /* [in] */ BSTR strPropertyName,
  107. /* [in] */ LONG Flags,
  108. /* [in] */ VARIANT const __RPC_FAR *pvarProperty)
  109. {
  110. if (0 == lstrcmpi(strPropertyName, wszCMM_PROP_DISPLAY_HWND))
  111. {
  112. if (pvarProperty->vt != VT_BSTR)
  113. return E_INVALIDARG;
  114. if (SysStringByteLen(pvarProperty->bstrVal) != sizeof(HWND))
  115. return E_INVALIDARG;
  116. // the value is stored as bytes in the bstr itself, not the bstr ptr
  117. m_hWnd = *(HWND*)pvarProperty->bstrVal;
  118. return S_OK;
  119. }
  120. return S_FALSE;
  121. }
  122. typedef struct _EXITSQL_CONFIGSTRUCT
  123. {
  124. HKEY hkeyStorageLocation;
  125. LONG Flags;
  126. BOOL fPageModified;
  127. } EXITSQL_CONFIGSTRUCT, *PEXITSQL_CONFIGSTRUCT;
  128. STDMETHODIMP
  129. CCertManageExitModuleSQLSample::Configure(
  130. /* [in] */ const BSTR strConfig,
  131. /* [in] */ BSTR strStorageLocation,
  132. /* [in] */ LONG Flags)
  133. {
  134. HRESULT hr;
  135. LPWSTR szMachine = NULL;
  136. HKEY hkeyHKLM = NULL;
  137. DWORD dwDisposition;
  138. EXITSQL_CONFIGSTRUCT sConfig;
  139. ZeroMemory(&sConfig, sizeof(EXITSQL_CONFIGSTRUCT));
  140. hr = GetMachineFromConfig(strConfig, &szMachine);
  141. _JumpIfError(hr, Ret, "GetMachineFromConfig");
  142. // UNDONE: only do this if remote
  143. hr = RegConnectRegistry(
  144. szMachine,
  145. HKEY_LOCAL_MACHINE,
  146. &hkeyHKLM);
  147. _JumpIfError(hr, Ret, "RegConnectRegistry");
  148. // open storage location: write perms if possible
  149. hr = RegCreateKeyEx(
  150. hkeyHKLM,
  151. strStorageLocation,
  152. 0,
  153. NULL,
  154. 0,
  155. KEY_READ | KEY_WRITE,
  156. NULL,
  157. &sConfig.hkeyStorageLocation,
  158. &dwDisposition);
  159. if (hr != S_OK)
  160. {
  161. hr = RegOpenKeyEx(
  162. hkeyHKLM,
  163. strStorageLocation,
  164. 0,
  165. KEY_READ, // fallback: read-only
  166. &sConfig.hkeyStorageLocation);
  167. if (hr != ERROR_SUCCESS)
  168. goto Ret;
  169. }
  170. sConfig.Flags = Flags;
  171. PROPSHEETPAGE page;
  172. ZeroMemory(&page, sizeof(PROPSHEETPAGE));
  173. page.dwSize = sizeof(PROPSHEETPAGE);
  174. page.dwFlags = PSP_DEFAULT;
  175. page.hInstance = g_hInstance;
  176. page.lParam = (LPARAM)&sConfig;
  177. page.pszTemplate = MAKEINTRESOURCE(IDD_EXITSQL_PROPERTIES);
  178. page.pfnDlgProc = ExitSQLDlgProc;
  179. PROPSHEETHEADER sSheet;
  180. ZeroMemory(&sSheet, sizeof(PROPSHEETHEADER));
  181. sSheet.dwSize = sizeof(PROPSHEETHEADER);
  182. sSheet.dwFlags = PSH_PROPSHEETPAGE | PSH_PROPTITLE;
  183. sSheet.hwndParent = m_hWnd;
  184. sSheet.hInstance = g_hInstance;
  185. sSheet.pszCaption = L"";
  186. sSheet.nPages = 1;
  187. sSheet.ppsp = &page;
  188. // finally, invoke the modal sheet
  189. INT_PTR iRet;
  190. iRet = ::PropertySheet(&sSheet);
  191. if ((iRet > 0) && (sConfig.fPageModified)) // successful modification
  192. {
  193. MessageBoxW(NULL, L"This action requires the Certificate Service to be restarted before taking effect.", L"Applying ExitSQL Settings", MB_OK|MB_ICONINFORMATION);
  194. }
  195. Ret:
  196. if (sConfig.hkeyStorageLocation)
  197. RegCloseKey(sConfig.hkeyStorageLocation);
  198. if (szMachine)
  199. LocalFree(szMachine);
  200. if (hkeyHKLM)
  201. RegCloseKey(hkeyHKLM);
  202. return S_OK;
  203. }
  204. INT_PTR CALLBACK ExitSQLDlgProc(
  205. HWND hwndDlg,
  206. UINT uMsg,
  207. WPARAM wParam,
  208. LPARAM lParam)
  209. {
  210. EXITSQL_CONFIGSTRUCT* psConfig;
  211. BOOL fReturn = FALSE;
  212. HRESULT hr;
  213. switch(uMsg)
  214. {
  215. case WM_INITDIALOG:
  216. {
  217. PROPSHEETPAGE* ps = (PROPSHEETPAGE *) lParam;
  218. psConfig = (EXITSQL_CONFIGSTRUCT*)ps->lParam;
  219. SetWindowLongPtr(hwndDlg, GWLP_USERDATA, (LPARAM)psConfig);
  220. BYTE pbTmp[MAX_PATH*sizeof(WCHAR)];
  221. DWORD cbTmp = MAX_PATH*sizeof(WCHAR);
  222. DWORD dwType;
  223. // dsn
  224. hr = RegQueryValueEx(
  225. psConfig->hkeyStorageLocation,
  226. wszREG_EXITSQL_DSN,
  227. 0,
  228. &dwType,
  229. pbTmp,
  230. &cbTmp);
  231. if ((hr != ERROR_SUCCESS) || (dwType != REG_SZ))
  232. break;
  233. SetDlgItemText(hwndDlg, IDC_EDIT_DSN, (LPWSTR)pbTmp);
  234. ((WCHAR*)pbTmp)[0] = L'\0';
  235. cbTmp = MAX_PATH*sizeof(WCHAR);
  236. // username
  237. hr = RegQueryValueEx(
  238. psConfig->hkeyStorageLocation,
  239. wszREG_EXITSQL_USER,
  240. 0,
  241. &dwType,
  242. pbTmp,
  243. &cbTmp);
  244. if ((hr != ERROR_SUCCESS) || (dwType != REG_SZ))
  245. break;
  246. SetDlgItemText(hwndDlg, IDC_EDIT_USER, (LPWSTR)pbTmp);
  247. ((WCHAR*)pbTmp)[0] = L'\0';
  248. cbTmp = MAX_PATH*sizeof(WCHAR);
  249. // password
  250. hr = RegQueryValueEx(
  251. psConfig->hkeyStorageLocation,
  252. wszREG_EXITSQL_PASSWORD,
  253. 0,
  254. &dwType,
  255. pbTmp,
  256. &cbTmp);
  257. if ((hr != ERROR_SUCCESS) || (dwType != REG_SZ))
  258. break;
  259. SetDlgItemText(hwndDlg, IDC_EDIT_PASSWORD, (LPWSTR)pbTmp);
  260. ((WCHAR*)pbTmp)[0] = L'\0';
  261. cbTmp = MAX_PATH*sizeof(WCHAR);
  262. psConfig->fPageModified = FALSE;
  263. // no other work to be done
  264. fReturn = TRUE;
  265. break;
  266. }
  267. case WM_COMMAND:
  268. switch (LOWORD(wParam))
  269. {
  270. case IDC_EDIT_DSN:
  271. case IDC_EDIT_USER:
  272. case IDC_EDIT_PASSWORD:
  273. if (HIWORD(wParam) == EN_CHANGE)
  274. {
  275. // grab our LParam
  276. psConfig = (EXITSQL_CONFIGSTRUCT*)GetWindowLongPtr(hwndDlg, GWLP_USERDATA);
  277. if (psConfig == NULL)
  278. break;
  279. psConfig->fPageModified = TRUE;
  280. }
  281. break;
  282. default:
  283. break;
  284. }
  285. break;
  286. case WM_NOTIFY:
  287. switch( ((LPNMHDR)lParam) -> code)
  288. {
  289. case PSN_APPLY:
  290. {
  291. hr = S_OK;
  292. // grab our LParam
  293. psConfig = (EXITSQL_CONFIGSTRUCT*)GetWindowLongPtr(hwndDlg, GWLP_USERDATA);
  294. if (psConfig == NULL)
  295. break;
  296. WCHAR szTmp[MAX_PATH];
  297. GetDlgItemText(hwndDlg, IDC_EDIT_DSN, szTmp, sizeof(szTmp));
  298. hr = RegSetValueEx(
  299. psConfig->hkeyStorageLocation,
  300. wszREG_EXITSQL_DSN,
  301. 0,
  302. REG_SZ,
  303. (PBYTE)&szTmp,
  304. (wcslen(szTmp)+1) * sizeof(WCHAR) );
  305. if (hr != ERROR_SUCCESS)
  306. goto savefailure;
  307. GetDlgItemText(hwndDlg, IDC_EDIT_USER, szTmp, sizeof(szTmp));
  308. hr = RegSetValueEx(
  309. psConfig->hkeyStorageLocation,
  310. wszREG_EXITSQL_USER,
  311. 0,
  312. REG_SZ,
  313. (PBYTE)&szTmp,
  314. (wcslen(szTmp)+1) * sizeof(WCHAR) );
  315. if (hr != ERROR_SUCCESS)
  316. goto savefailure;
  317. GetDlgItemText(hwndDlg, IDC_EDIT_PASSWORD, szTmp, sizeof(szTmp));
  318. hr = RegSetValueEx(
  319. psConfig->hkeyStorageLocation,
  320. wszREG_EXITSQL_PASSWORD,
  321. 0,
  322. REG_SZ,
  323. (PBYTE)&szTmp,
  324. (wcslen(szTmp)+1) * sizeof(WCHAR) );
  325. if (hr != ERROR_SUCCESS)
  326. goto savefailure;
  327. savefailure:
  328. if (hr != ERROR_SUCCESS)
  329. {
  330. MessageBoxW(NULL, L"The settings could not be saved.", L"Applying ExitSQL Settings", MB_OK|MB_ICONINFORMATION);
  331. psConfig->fPageModified = FALSE;
  332. }
  333. }
  334. break;
  335. default:
  336. break;
  337. }
  338. break;
  339. default:
  340. break;
  341. }
  342. return fReturn;
  343. }