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.

382 lines
12 KiB

  1. // This is a part of the Microsoft Management Console.
  2. // Copyright (C) Microsoft Corporation, 1995 - 1999
  3. // All rights reserved.
  4. //
  5. // This source code is only intended as a supplement to the
  6. // Microsoft Management Console and related
  7. // electronic documentation provided with the interfaces.
  8. // You will need the NT SUR Beta 2 SDK or VC 4.2 in order to build this
  9. // project. This is because you will need MIDL 3.00.15 or higher and new
  10. // headers and libs. If you have VC 4.2 installed, then everything should
  11. // already be configured correctly.
  12. // Note: Proxy/Stub Information
  13. // To build a separate proxy/stub DLL,
  14. // run nmake -f Snapinps.mak in the project directory.
  15. #include "stdafx.h"
  16. #define myHLastError GetLastError
  17. #include "csresstr.h"
  18. CComModule _Module;
  19. HINSTANCE g_hInstance = NULL;
  20. BEGIN_OBJECT_MAP(ObjectMap)
  21. OBJECT_ENTRY(CLSID_CAPolicyExtensionSnapIn, CComponentDataPolicySettings)
  22. OBJECT_ENTRY(CLSID_CACertificateTemplateManager, CComponentDataGPEExtension)
  23. OBJECT_ENTRY(CLSID_CAPolicyAbout, CCAPolicyAboutImpl)
  24. OBJECT_ENTRY(CLSID_CertTypeAbout, CCertTypeAboutImpl)
  25. OBJECT_ENTRY(CLSID_CertTypeShellExt, CCertTypeShlExt)
  26. OBJECT_ENTRY(CLSID_CAShellExt, CCAShellExt)
  27. END_OBJECT_MAP()
  28. STDAPI UnregisterGPECertTemplates(void);
  29. BOOL WINAPI DllMain(
  30. HINSTANCE hinstDLL, // handle to DLL module
  31. DWORD dwReason, // reason for calling function
  32. LPVOID lpvReserved)
  33. {
  34. switch (dwReason)
  35. {
  36. case DLL_PROCESS_ATTACH:
  37. {
  38. g_hInstance = hinstDLL;
  39. myVerifyResourceStrings(hinstDLL);
  40. _Module.Init(ObjectMap, hinstDLL);
  41. DisableThreadLibraryCalls(hinstDLL);
  42. break;
  43. }
  44. case DLL_PROCESS_DETACH:
  45. {
  46. _Module.Term();
  47. DEBUG_VERIFY_INSTANCE_COUNT(CSnapin);
  48. DEBUG_VERIFY_INSTANCE_COUNT(CComponentDataImpl);
  49. break;
  50. }
  51. default:
  52. break;
  53. }
  54. return TRUE;
  55. }
  56. /////////////////////////////////////////////////////////////////////////////
  57. // Used to determine whether the DLL can be unloaded by OLE
  58. STDAPI DllCanUnloadNow(void)
  59. {
  60. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  61. return (_Module.GetLockCount() == 0) ? S_OK : S_FALSE;
  62. }
  63. /////////////////////////////////////////////////////////////////////////////
  64. // Returns a class factory to create an object of the requested type
  65. STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID* ppv)
  66. {
  67. return _Module.GetClassObject(rclsid, riid, ppv);
  68. }
  69. /////////////////////////////////////////////////////////////////////////////
  70. // DllRegisterServer - Adds entries to the system registry
  71. STDAPI DllRegisterServer(void)
  72. {
  73. HRESULT hResult = S_OK;
  74. CString cstrSubKey;
  75. DWORD dwDisp;
  76. LONG lResult;
  77. HKEY hKey;
  78. CString cstrSnapInKey;
  79. CString cstrCAPolicyAboutKey;
  80. CString cstrSnapInName;
  81. LPWSTR pszTmp;
  82. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  83. cstrSnapInName.LoadString(IDS_SNAPIN_NAME);
  84. // be sure this is alloced
  85. if (NULL == (pszTmp = cstrSubKey.GetBuffer(MAX_PATH)))
  86. return SELFREG_E_CLASS;
  87. StringFromGUID2( CLSID_CAPolicyExtensionSnapIn,
  88. pszTmp,
  89. MAX_PATH);
  90. cstrSnapInKey = cstrSubKey;
  91. // be sure this is alloced
  92. if (NULL == (pszTmp = cstrSubKey.GetBuffer(MAX_PATH)))
  93. return SELFREG_E_CLASS;
  94. StringFromGUID2( CLSID_CAPolicyAbout,
  95. pszTmp,
  96. MAX_PATH);
  97. cstrCAPolicyAboutKey = cstrSubKey;
  98. //
  99. // Register Policy Extensions SnapIn with MMC
  100. //
  101. cstrSubKey.Format(L"Software\\Microsoft\\MMC\\SnapIns\\%s", (LPCWSTR)cstrSnapInKey);
  102. lResult = RegCreateKeyEx (HKEY_LOCAL_MACHINE, cstrSubKey, 0, NULL,
  103. REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL,
  104. &hKey, &dwDisp);
  105. if (lResult != ERROR_SUCCESS) {
  106. return SELFREG_E_CLASS;
  107. }
  108. RegSetValueEx (hKey, L"NameString", 0, REG_SZ, (LPBYTE)((LPCTSTR)cstrSnapInName),
  109. (cstrSnapInName.GetLength() + 1) * sizeof(WCHAR));
  110. RegSetValueEx (hKey, L"About", 0, REG_SZ, (LPBYTE)((LPCTSTR)cstrCAPolicyAboutKey),
  111. (cstrCAPolicyAboutKey.GetLength() + 1) * sizeof(WCHAR));
  112. RegCloseKey (hKey);
  113. cstrSubKey.Format(L"Software\\Microsoft\\MMC\\SnapIns\\%s\\NodeTypes", (LPCWSTR)cstrSnapInKey);
  114. lResult = RegCreateKeyEx (HKEY_LOCAL_MACHINE, cstrSubKey, 0, NULL,
  115. REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL,
  116. &hKey, &dwDisp);
  117. if (lResult != ERROR_SUCCESS) {
  118. return SELFREG_E_CLASS;
  119. }
  120. RegCloseKey (hKey);
  121. cstrSubKey.Format(L"Software\\Microsoft\\MMC\\SnapIns\\%s\\NodeTypes\\%s", (LPCWSTR)cstrSnapInKey, cszNodeTypePolicySettings);
  122. lResult = RegCreateKeyEx (HKEY_LOCAL_MACHINE, cstrSubKey, 0, NULL,
  123. REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL,
  124. &hKey, &dwDisp);
  125. if (lResult != ERROR_SUCCESS) {
  126. return SELFREG_E_CLASS;
  127. }
  128. RegCloseKey (hKey);
  129. //
  130. // Register Policy Settings in the NodeTypes key
  131. //
  132. cstrSubKey.Format(L"Software\\Microsoft\\MMC\\NodeTypes\\%s", cszNodeTypePolicySettings);
  133. lResult = RegCreateKeyEx (HKEY_LOCAL_MACHINE, cstrSubKey, 0, NULL,
  134. REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL,
  135. &hKey, &dwDisp);
  136. if (lResult != ERROR_SUCCESS) {
  137. return SELFREG_E_CLASS;
  138. }
  139. RegSetValueEx (hKey, NULL, 0, REG_SZ, (LPBYTE)((LPCTSTR)cstrSnapInName),
  140. (cstrSnapInName.GetLength() + 1) * sizeof(WCHAR));
  141. RegCloseKey (hKey);
  142. //
  143. // register as an extension under the CA snapin
  144. //
  145. cstrSubKey.Format(L"Software\\Microsoft\\MMC\\NodeTypes\\%s\\Extensions\\NameSpace", cszCAManagerParentNodeID);
  146. lResult = RegCreateKeyEx (HKEY_LOCAL_MACHINE, cstrSubKey, 0, NULL,
  147. REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL,
  148. &hKey, &dwDisp);
  149. if (lResult != ERROR_SUCCESS) {
  150. return SELFREG_E_CLASS;
  151. }
  152. RegSetValueEx (hKey, cstrSnapInKey, 0, REG_SZ, (LPBYTE)((LPCTSTR)cstrSnapInName),
  153. (cstrSnapInName.GetLength() + 1) * sizeof(WCHAR));
  154. RegCloseKey (hKey);
  155. // kill beta2 GPT cert type editing
  156. UnregisterGPECertTemplates();
  157. // registers object, typelib and all interfaces in typelib
  158. return _Module.RegisterServer(FALSE);
  159. //return S_OK;
  160. }
  161. /////////////////////////////////////////////////////////////////////////////
  162. // DllUnregisterServer - Removes entries from the system registry
  163. STDAPI DllUnregisterServer(void)
  164. {
  165. CString cstrSubKey;
  166. LONG lResult;
  167. HKEY hKey;
  168. WCHAR szSnapInKey[50];
  169. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  170. _Module.UnregisterServer();
  171. StringFromGUID2 (CLSID_CAPolicyExtensionSnapIn, szSnapInKey, 50);
  172. cstrSubKey.Format(L"Software\\Microsoft\\MMC\\NodeTypes\\%s\\Extensions\\NameSpace", cszCAManagerParentNodeID);
  173. lResult = RegOpenKeyEx (HKEY_LOCAL_MACHINE, cstrSubKey, 0,
  174. KEY_WRITE, &hKey);
  175. if (lResult == ERROR_SUCCESS) {
  176. RegDeleteValue (hKey, szSnapInKey);
  177. RegCloseKey (hKey);
  178. }
  179. cstrSubKey.Format(L"Software\\Microsoft\\MMC\\NodeTypes\\%s\\Extensions\\NameSpace", cszNodeTypePolicySettings);
  180. RegDeleteKey (HKEY_LOCAL_MACHINE, cstrSubKey);
  181. cstrSubKey.Format(L"Software\\Microsoft\\MMC\\NodeTypes\\%s", cszNodeTypePolicySettings);
  182. RegDeleteKey (HKEY_LOCAL_MACHINE, cstrSubKey);
  183. cstrSubKey.Format(L"Software\\Microsoft\\MMC\\SnapIns\\%s\\NodeTypes\\%s", szSnapInKey, cszNodeTypePolicySettings);
  184. RegDeleteKey (HKEY_LOCAL_MACHINE, cstrSubKey);
  185. cstrSubKey.Format(L"Software\\Microsoft\\MMC\\SnapIns\\%s\\NodeTypes", szSnapInKey);
  186. RegDeleteKey (HKEY_LOCAL_MACHINE, cstrSubKey);
  187. cstrSubKey.Format(L"Software\\Microsoft\\MMC\\SnapIns\\%s", szSnapInKey);
  188. RegDeleteKey (HKEY_LOCAL_MACHINE, cstrSubKey);
  189. // kill beta2 GPT cert type editing
  190. UnregisterGPECertTemplates();
  191. return S_OK;
  192. }
  193. /////////////////////////////////////////////////////////////////////////////
  194. // UnregisterGPECertTemplates - Removes GPECertTemplateEditing from the registry
  195. STDAPI UnregisterGPECertTemplates(void)
  196. {
  197. CString cstrSubKey;
  198. LONG lResult;
  199. HKEY hKey;
  200. WCHAR szSnapInKeyForGPT[50];
  201. WCHAR szSnapInKey[50];
  202. StringFromGUID2 (CLSID_CACertificateTemplateManager, szSnapInKeyForGPT, 50);
  203. StringFromGUID2 (CLSID_CAPolicyExtensionSnapIn, szSnapInKey, 50);
  204. cstrSubKey.Format(L"Software\\Microsoft\\MMC\\NodeTypes\\%s\\Extensions\\NameSpace", cszSCEParentNodeIDUSER);
  205. lResult = RegOpenKeyEx (HKEY_LOCAL_MACHINE, cstrSubKey, 0,
  206. KEY_WRITE, &hKey);
  207. if (lResult == ERROR_SUCCESS) {
  208. RegDeleteValue (hKey, szSnapInKeyForGPT);
  209. RegCloseKey (hKey);
  210. }
  211. cstrSubKey.Format(L"Software\\Microsoft\\MMC\\NodeTypes\\%s\\Extensions\\NameSpace", cszSCEParentNodeIDCOMPUTER);
  212. lResult = RegOpenKeyEx (HKEY_LOCAL_MACHINE, cstrSubKey, 0,
  213. KEY_WRITE, &hKey);
  214. if (lResult == ERROR_SUCCESS) {
  215. RegDeleteValue (hKey, szSnapInKeyForGPT);
  216. RegCloseKey (hKey);
  217. }
  218. cstrSubKey.Format(L"Software\\Microsoft\\MMC\\NodeTypes\\%s", cszNodeTypeCertificateTemplate);
  219. RegDeleteKey (HKEY_LOCAL_MACHINE, cstrSubKey);
  220. cstrSubKey.Format(L"Software\\Microsoft\\MMC\\SnapIns\\%s\\NodeTypes\\%s", (LPCWSTR)szSnapInKey, cszNodeTypeCertificateTemplate);
  221. RegDeleteKey (HKEY_LOCAL_MACHINE, cstrSubKey);
  222. cstrSubKey.Format(L"Software\\Microsoft\\MMC\\SnapIns\\%s\\NodeTypes", (LPCWSTR)szSnapInKeyForGPT);
  223. RegDeleteKey (HKEY_LOCAL_MACHINE, cstrSubKey);
  224. cstrSubKey.Format(L"Software\\Microsoft\\MMC\\SnapIns\\%s", (LPCWSTR)szSnapInKeyForGPT);
  225. RegDeleteKey (HKEY_LOCAL_MACHINE, cstrSubKey);
  226. return S_OK;
  227. }
  228. VOID Usage()
  229. {
  230. CString cstrDllInstallUsageText;
  231. CString cstrDllInstallUsageTitle;
  232. cstrDllInstallUsageText.LoadString(IDS_DLL_INSTALL_USAGE_TEXT);
  233. cstrDllInstallUsageTitle.LoadString(IDS_DLL_INSTALL_USAGE_TITLE);
  234. MessageBox(NULL, cstrDllInstallUsageText, cstrDllInstallUsageTitle, MB_OK);
  235. }
  236. STDAPI DllInstall(BOOL bInstall, LPCWSTR pszCmdLine)
  237. {
  238. DWORD dwDisp;
  239. LONG lResult;
  240. HKEY hKey;
  241. LPCWSTR wszCurrentCmd = pszCmdLine;
  242. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  243. BOOL fEnableTypeEditing = FALSE;
  244. // parse the cmd line
  245. while(wszCurrentCmd && *wszCurrentCmd)
  246. {
  247. while(*wszCurrentCmd == L' ')
  248. wszCurrentCmd++;
  249. if(*wszCurrentCmd == 0)
  250. break;
  251. switch(*wszCurrentCmd++)
  252. {
  253. case L'?':
  254. Usage();
  255. return S_OK;
  256. case L'c':
  257. fEnableTypeEditing = TRUE;
  258. break;
  259. }
  260. }
  261. //
  262. // Register Certificate Templates in the NodeTypes key
  263. //
  264. if(fEnableTypeEditing)
  265. {
  266. DWORD dwEnable = bInstall;
  267. lResult = RegCreateKeyEx (HKEY_CURRENT_USER, L"Software\\Microsoft\\Cryptography\\CertificateTemplateCache", 0, NULL,
  268. REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL,
  269. &hKey, &dwDisp);
  270. if (lResult != ERROR_SUCCESS) {
  271. return SELFREG_E_CLASS;
  272. }
  273. RegSetValueEx (hKey,
  274. REGSZ_ENABLE_CERTTYPE_EDITING,
  275. 0,
  276. REG_DWORD,
  277. (LPBYTE)(&dwEnable),
  278. sizeof(dwEnable));
  279. RegCloseKey (hKey);
  280. }
  281. return S_OK;
  282. }