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.

356 lines
7.7 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1996 - 1999
  6. //
  7. // File: pkireg.cpp
  8. //
  9. // Contents: Microsoft Internet Security Register
  10. //
  11. // Functions: RegisterCryptoDlls
  12. // CleanupRegistry
  13. //
  14. // *** local functions ***
  15. //
  16. // History: 06-Jun-1997 pberkman created
  17. //
  18. //--------------------------------------------------------------------------
  19. #include "global.hxx"
  20. #include "cryptreg.h"
  21. char *ppszDlls[] =
  22. {
  23. "wintrust.dll",
  24. "mssign32.dll",
  25. "cryptui.dll",
  26. "cryptnet.dll",
  27. "cryptext.dll",
  28. "xenroll.dll",
  29. NULL
  30. };
  31. POLSET psPolicySettings[] =
  32. {
  33. WTPF_IGNOREREVOKATION, FALSE,
  34. WTPF_IGNOREREVOCATIONONTS, TRUE,
  35. WTPF_OFFLINEOK_IND, TRUE,
  36. WTPF_OFFLINEOK_COM, TRUE,
  37. WTPF_OFFLINEOKNBU_IND, TRUE,
  38. WTPF_OFFLINEOKNBU_COM, TRUE,
  39. 0, 0
  40. };
  41. char *ppszOldHKLMRegistryKeys[] =
  42. {
  43. "SOFTWARE\\Microsoft\\Cryptography\\Providers\\Subject",
  44. NULL
  45. };
  46. void DeleteKeys(HKEY hKeyParent, char *pszKey);
  47. #define PKIREG_WINLOGON_EXT_PREFIX \
  48. "Software\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon\\Notify\\"
  49. void RegisterWinlogonExtension(
  50. IN LPCSTR pszSubKey,
  51. IN LPCSTR pszDll,
  52. IN LPCSTR pszProc
  53. )
  54. {
  55. HKEY hKey;
  56. DWORD dwDisposition;
  57. DWORD dwValue;
  58. LPSTR pszKey; // _alloca'ed
  59. DWORD cchKey;
  60. if ( FIsWinNT5() == FALSE )
  61. {
  62. return;
  63. }
  64. cchKey = strlen(PKIREG_WINLOGON_EXT_PREFIX) + strlen(pszSubKey) + 1;
  65. __try {
  66. pszKey = (LPSTR) _alloca(cchKey);
  67. } __except(EXCEPTION_EXECUTE_HANDLER) {
  68. return;
  69. }
  70. strcpy(pszKey, PKIREG_WINLOGON_EXT_PREFIX);
  71. strcat(pszKey, pszSubKey);
  72. if ( RegCreateKeyExA(
  73. HKEY_LOCAL_MACHINE,
  74. pszKey,
  75. 0,
  76. NULL,
  77. 0,
  78. KEY_ALL_ACCESS,
  79. NULL,
  80. &hKey,
  81. &dwDisposition
  82. ) != ERROR_SUCCESS )
  83. {
  84. return;
  85. }
  86. dwValue = 0;
  87. RegSetValueExA( hKey, "Asynchronous", 0, REG_DWORD, (LPBYTE)&dwValue, sizeof( dwValue ) );
  88. RegSetValueExA( hKey, "Impersonate", 0, REG_DWORD, (LPBYTE)&dwValue, sizeof( dwValue ) );
  89. RegSetValueExA( hKey, "DllName", 0, REG_EXPAND_SZ, (LPBYTE) pszDll,
  90. strlen(pszDll) + 1 );
  91. RegSetValueExA( hKey, "Logoff", 0, REG_SZ, (LPBYTE) pszProc,
  92. strlen(pszProc) + 1 );
  93. RegCloseKey( hKey );
  94. }
  95. void RegisterCrypt32EventSource()
  96. {
  97. HKEY hKey;
  98. DWORD dwDisposition;
  99. LPCSTR pszEventMessageFile = "%SystemRoot%\\System32\\crypt32.dll";
  100. DWORD dwTypesSupported;
  101. if ( FIsWinNT5() == FALSE )
  102. {
  103. return;
  104. }
  105. if ( RegCreateKeyExA(
  106. HKEY_LOCAL_MACHINE,
  107. "SYSTEM\\CurrentControlSet\\Services\\Eventlog\\Application\\crypt32",
  108. 0,
  109. NULL,
  110. 0,
  111. KEY_ALL_ACCESS,
  112. NULL,
  113. &hKey,
  114. &dwDisposition
  115. ) != ERROR_SUCCESS )
  116. {
  117. return;
  118. }
  119. RegSetValueExA(
  120. hKey,
  121. "EventMessageFile",
  122. 0,
  123. REG_EXPAND_SZ,
  124. (LPBYTE) pszEventMessageFile,
  125. strlen(pszEventMessageFile) + 1
  126. );
  127. dwTypesSupported = EVENTLOG_ERROR_TYPE | EVENTLOG_WARNING_TYPE |
  128. EVENTLOG_INFORMATION_TYPE;
  129. RegSetValueExA(
  130. hKey,
  131. "TypesSupported",
  132. 0,
  133. REG_DWORD,
  134. (LPBYTE) &dwTypesSupported,
  135. sizeof(DWORD)
  136. );
  137. RegCloseKey( hKey );
  138. }
  139. HRESULT RegisterCryptoDlls(BOOL fSetFlags)
  140. {
  141. char **ppszDll;
  142. BOOL fRet;
  143. fRet = TRUE;
  144. ppszDll = ppszDlls;
  145. while (*ppszDll)
  146. {
  147. fRet &= _LoadAndRegister(*ppszDll, FALSE);
  148. ppszDll++;
  149. }
  150. if (fSetFlags)
  151. {
  152. fRet &= _AdjustPolicyFlags(psPolicySettings);
  153. }
  154. // Unregister previously registered DLL's
  155. // vsrevoke.dll
  156. CryptUnregisterDefaultOIDFunction(
  157. X509_ASN_ENCODING,
  158. CRYPT_OID_VERIFY_REVOCATION_FUNC,
  159. L"vsrevoke.dll"
  160. );
  161. // mscrlrev.dll
  162. CryptUnregisterDefaultOIDFunction(
  163. X509_ASN_ENCODING,
  164. CRYPT_OID_VERIFY_REVOCATION_FUNC,
  165. L"mscrlrev.dll"
  166. );
  167. // msctl.dll
  168. CryptUnregisterDefaultOIDFunction(
  169. X509_ASN_ENCODING,
  170. CRYPT_OID_VERIFY_CTL_USAGE_FUNC,
  171. L"msctl.dll"
  172. );
  173. RegisterWinlogonExtension("crypt32chain", "crypt32.dll",
  174. "ChainWlxLogoffEvent");
  175. RegisterWinlogonExtension("cryptnet", "cryptnet.dll",
  176. "CryptnetWlxLogoffEvent");
  177. RegisterCrypt32EventSource();
  178. return((fRet) ? S_OK : S_FALSE);
  179. }
  180. HRESULT UnregisterCryptoDlls(void)
  181. {
  182. char **ppszDll;
  183. BOOL fRet;
  184. fRet = TRUE;
  185. ppszDll = ppszDlls;
  186. while (*ppszDll)
  187. {
  188. fRet &= _LoadAndRegister(*ppszDll, TRUE);
  189. ppszDll++;
  190. }
  191. return((fRet) ? S_OK : S_FALSE);
  192. }
  193. void CleanupRegistry(void)
  194. {
  195. char **ppszKeys;
  196. ppszKeys = ppszOldHKLMRegistryKeys;
  197. while (*ppszKeys)
  198. {
  199. DeleteKeys(HKEY_LOCAL_MACHINE, *ppszKeys);
  200. ppszKeys++;
  201. }
  202. }
  203. void DeleteKeys(HKEY hKeyParent, char *pszKey)
  204. {
  205. HKEY hKey;
  206. char szSubKey[REG_MAX_KEY_NAME];
  207. if (RegOpenKeyEx(hKeyParent, pszKey, 0, KEY_READ, &hKey) == ERROR_SUCCESS)
  208. {
  209. while (RegEnumKey(hKey, 0, &szSubKey[0], REG_MAX_KEY_NAME) == ERROR_SUCCESS)
  210. {
  211. // WARNING: recursive!
  212. DeleteKeys(hKey, &szSubKey[0]);
  213. }
  214. RegCloseKey(hKey);
  215. RegDeleteKey(hKeyParent, pszKey);
  216. }
  217. }
  218. typedef HRESULT (WINAPI *DllRegisterServer)(void);
  219. BOOL _LoadAndRegister(char *pszDll, BOOL fUnregister)
  220. {
  221. DllRegisterServer pfn;
  222. HINSTANCE hDll;
  223. BOOL fRet;
  224. fRet = TRUE;
  225. if (!(hDll = LoadLibrary(pszDll)))
  226. {
  227. goto LoadLibraryFail;
  228. }
  229. if (!(pfn = (DllRegisterServer)GetProcAddress(hDll, (fUnregister) ? "DllUnregisterServer" : "DllRegisterServer")))
  230. {
  231. goto ProcAddressFail;
  232. }
  233. if ((*pfn)() != S_OK)
  234. {
  235. goto DllRegisterFailed;
  236. }
  237. CommonReturn:
  238. if (hDll)
  239. {
  240. FreeLibrary(hDll);
  241. }
  242. return(fRet);
  243. ErrorReturn:
  244. fRet = FALSE;
  245. goto CommonReturn;
  246. TRACE_ERROR_EX(DBG_SS, LoadLibraryFail);
  247. TRACE_ERROR_EX(DBG_SS, ProcAddressFail);
  248. TRACE_ERROR_EX(DBG_SS, DllRegisterFailed);
  249. }
  250. BOOL _AdjustPolicyFlags(POLSET *pPolSet)
  251. {
  252. DWORD dwPolSettings;
  253. POLSET *pPol;
  254. dwPolSettings = 0;
  255. WintrustGetRegPolicyFlags(&dwPolSettings);
  256. // In WXP, changed to always update the settings
  257. #if 0
  258. //
  259. // only do this if we aren't set yet.
  260. //
  261. if (dwPolSettings != 0)
  262. {
  263. return(TRUE);
  264. }
  265. #endif
  266. pPol = pPolSet;
  267. while (pPol->dwSetting > 0)
  268. {
  269. if (pPol->fOn)
  270. {
  271. dwPolSettings |= pPol->dwSetting;
  272. }
  273. else
  274. {
  275. dwPolSettings &= ~(pPol->dwSetting);
  276. }
  277. pPol++;
  278. }
  279. return(WintrustSetRegPolicyFlags(dwPolSettings));
  280. }