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.

357 lines
11 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_CertTypeShellExt, CCertTypeShlExt)
  24. OBJECT_ENTRY(CLSID_CAPolicyAbout, CCAPolicyAboutImpl)
  25. OBJECT_ENTRY(CLSID_CertTypeAbout, CCertTypeAboutImpl)
  26. END_OBJECT_MAP()
  27. STDAPI UnregisterGPECertTemplates(void);
  28. BOOL WINAPI DllMain(
  29. HINSTANCE hinstDLL, // handle to DLL module
  30. DWORD dwReason, // reason for calling function
  31. LPVOID lpvReserved)
  32. {
  33. switch (dwReason)
  34. {
  35. case DLL_PROCESS_ATTACH:
  36. {
  37. g_hInstance = hinstDLL;
  38. myVerifyResourceStrings(hinstDLL);
  39. _Module.Init(ObjectMap, hinstDLL);
  40. DisableThreadLibraryCalls(hinstDLL);
  41. break;
  42. }
  43. case DLL_PROCESS_DETACH:
  44. {
  45. _Module.Term();
  46. DEBUG_VERIFY_INSTANCE_COUNT(CSnapin);
  47. DEBUG_VERIFY_INSTANCE_COUNT(CComponentDataImpl);
  48. break;
  49. }
  50. default:
  51. break;
  52. }
  53. return TRUE;
  54. }
  55. /////////////////////////////////////////////////////////////////////////////
  56. // Used to determine whether the DLL can be unloaded by OLE
  57. STDAPI DllCanUnloadNow(void)
  58. {
  59. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  60. return (_Module.GetLockCount() == 0) ? S_OK : S_FALSE;
  61. }
  62. /////////////////////////////////////////////////////////////////////////////
  63. // Returns a class factory to create an object of the requested type
  64. STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID* ppv)
  65. {
  66. return _Module.GetClassObject(rclsid, riid, ppv);
  67. }
  68. /////////////////////////////////////////////////////////////////////////////
  69. // DllRegisterServer - Adds entries to the system registry
  70. STDAPI DllRegisterServer(void)
  71. {
  72. HRESULT hResult = S_OK;
  73. CString cstrSubKey;
  74. DWORD dwDisp;
  75. LONG lResult;
  76. HKEY hKey;
  77. CString cstrSnapInKey;
  78. CString cstrCAPolicyAboutKey;
  79. CString cstrSnapInName;
  80. CString cstrSnapInNameIndirect;
  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. cstrSnapInNameIndirect.Format(
  99. wszSNAPINNAMESTRINGINDIRECT_TEMPLATE,
  100. wszSNAPIN_FILENAME,
  101. IDS_SNAPIN_NAME);
  102. //
  103. // Register Policy Extensions SnapIn with MMC
  104. //
  105. cstrSubKey.Format(L"Software\\Microsoft\\MMC\\SnapIns\\%ws", (LPCWSTR)cstrSnapInKey);
  106. lResult = RegCreateKeyEx (HKEY_LOCAL_MACHINE, cstrSubKey, 0, NULL,
  107. REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL,
  108. &hKey, &dwDisp);
  109. if (lResult != ERROR_SUCCESS) {
  110. return SELFREG_E_CLASS;
  111. }
  112. RegSetValueEx (hKey, L"NameString", 0, REG_SZ, (LPBYTE)((LPCTSTR)cstrSnapInName),
  113. (cstrSnapInName.GetLength() + 1) * sizeof(WCHAR));
  114. RegSetValueEx (hKey, wszSNAPINNAMESTRINGINDIRECT, 0, REG_SZ,
  115. (LPBYTE)((LPCTSTR)cstrSnapInNameIndirect),
  116. (cstrSnapInNameIndirect.GetLength() + 1) * sizeof(WCHAR));
  117. RegSetValueEx (hKey, L"About", 0, REG_SZ, (LPBYTE)((LPCTSTR)cstrCAPolicyAboutKey),
  118. (cstrCAPolicyAboutKey.GetLength() + 1) * sizeof(WCHAR));
  119. RegCloseKey (hKey);
  120. cstrSubKey.Format(L"Software\\Microsoft\\MMC\\SnapIns\\%ws\\NodeTypes", (LPCWSTR)cstrSnapInKey);
  121. lResult = RegCreateKeyEx (HKEY_LOCAL_MACHINE, cstrSubKey, 0, NULL,
  122. REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL,
  123. &hKey, &dwDisp);
  124. if (lResult != ERROR_SUCCESS) {
  125. return SELFREG_E_CLASS;
  126. }
  127. RegCloseKey (hKey);
  128. cstrSubKey.Format(L"Software\\Microsoft\\MMC\\SnapIns\\%ws\\NodeTypes\\%ws", (LPCWSTR)cstrSnapInKey, cszNodeTypePolicySettings);
  129. lResult = RegCreateKeyEx (HKEY_LOCAL_MACHINE, cstrSubKey, 0, NULL,
  130. REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL,
  131. &hKey, &dwDisp);
  132. if (lResult != ERROR_SUCCESS) {
  133. return SELFREG_E_CLASS;
  134. }
  135. RegCloseKey (hKey);
  136. //
  137. // Register Policy Settings in the NodeTypes key
  138. //
  139. cstrSubKey.Format(L"Software\\Microsoft\\MMC\\NodeTypes\\%ws", cszNodeTypePolicySettings);
  140. lResult = RegCreateKeyEx (HKEY_LOCAL_MACHINE, cstrSubKey, 0, NULL,
  141. REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL,
  142. &hKey, &dwDisp);
  143. if (lResult != ERROR_SUCCESS) {
  144. return SELFREG_E_CLASS;
  145. }
  146. RegSetValueEx (hKey, NULL, 0, REG_SZ, (LPBYTE)((LPCTSTR)cstrSnapInName),
  147. (cstrSnapInName.GetLength() + 1) * sizeof(WCHAR));
  148. RegCloseKey (hKey);
  149. //
  150. // register as an extension under the CA snapin
  151. //
  152. cstrSubKey.Format(L"Software\\Microsoft\\MMC\\NodeTypes\\%ws\\Extensions\\NameSpace", cszCAManagerParentNodeID);
  153. lResult = RegCreateKeyEx (HKEY_LOCAL_MACHINE, cstrSubKey, 0, NULL,
  154. REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL,
  155. &hKey, &dwDisp);
  156. if (lResult != ERROR_SUCCESS) {
  157. return SELFREG_E_CLASS;
  158. }
  159. RegSetValueEx (hKey, cstrSnapInKey, 0, REG_SZ, (LPBYTE)((LPCTSTR)cstrSnapInName),
  160. (cstrSnapInName.GetLength() + 1) * sizeof(WCHAR));
  161. RegCloseKey (hKey);
  162. // kill beta2 GPT cert type editing
  163. UnregisterGPECertTemplates();
  164. // registers object, typelib and all interfaces in typelib
  165. return _Module.RegisterServer(FALSE);
  166. //return S_OK;
  167. }
  168. /////////////////////////////////////////////////////////////////////////////
  169. // DllUnregisterServer - Removes entries from the system registry
  170. STDAPI DllUnregisterServer(void)
  171. {
  172. CString cstrSubKey;
  173. LONG lResult;
  174. HKEY hKey;
  175. WCHAR szSnapInKey[50];
  176. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  177. _Module.UnregisterServer();
  178. StringFromGUID2 (CLSID_CAPolicyExtensionSnapIn, szSnapInKey, 50);
  179. cstrSubKey.Format(L"Software\\Microsoft\\MMC\\NodeTypes\\%ws\\Extensions\\NameSpace", cszCAManagerParentNodeID);
  180. lResult = RegOpenKeyEx (HKEY_LOCAL_MACHINE, cstrSubKey, 0,
  181. KEY_WRITE, &hKey);
  182. if (lResult == ERROR_SUCCESS) {
  183. RegDeleteValue (hKey, szSnapInKey);
  184. RegCloseKey (hKey);
  185. }
  186. cstrSubKey.Format(L"Software\\Microsoft\\MMC\\NodeTypes\\%ws\\Extensions\\NameSpace", cszNodeTypePolicySettings);
  187. RegDeleteKey (HKEY_LOCAL_MACHINE, cstrSubKey);
  188. cstrSubKey.Format(L"Software\\Microsoft\\MMC\\NodeTypes\\%ws", cszNodeTypePolicySettings);
  189. RegDeleteKey (HKEY_LOCAL_MACHINE, cstrSubKey);
  190. cstrSubKey.Format(L"Software\\Microsoft\\MMC\\SnapIns\\%ws\\NodeTypes\\%ws", szSnapInKey, cszNodeTypePolicySettings);
  191. RegDeleteKey (HKEY_LOCAL_MACHINE, cstrSubKey);
  192. cstrSubKey.Format(L"Software\\Microsoft\\MMC\\SnapIns\\%ws\\NodeTypes", szSnapInKey);
  193. RegDeleteKey (HKEY_LOCAL_MACHINE, cstrSubKey);
  194. cstrSubKey.Format(L"Software\\Microsoft\\MMC\\SnapIns\\%ws", szSnapInKey);
  195. RegDeleteKey (HKEY_LOCAL_MACHINE, cstrSubKey);
  196. // kill beta2 GPT cert type editing
  197. UnregisterGPECertTemplates();
  198. return S_OK;
  199. }
  200. /////////////////////////////////////////////////////////////////////////////
  201. // UnregisterGPECertTemplates - Removes GPECertTemplateEditing from the registry
  202. STDAPI UnregisterGPECertTemplates(void)
  203. {
  204. CString cstrSubKey;
  205. LONG lResult;
  206. HKEY hKey;
  207. WCHAR szSnapInKeyForGPT[50];
  208. WCHAR szSnapInKey[50];
  209. StringFromGUID2 (CLSID_CACertificateTemplateManager, szSnapInKeyForGPT, 50);
  210. StringFromGUID2 (CLSID_CAPolicyExtensionSnapIn, szSnapInKey, 50);
  211. cstrSubKey.Format(L"Software\\Microsoft\\MMC\\NodeTypes\\%ws\\Extensions\\NameSpace", cszSCEParentNodeIDUSER);
  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\\%ws\\Extensions\\NameSpace", cszSCEParentNodeIDCOMPUTER);
  219. lResult = RegOpenKeyEx (HKEY_LOCAL_MACHINE, cstrSubKey, 0,
  220. KEY_WRITE, &hKey);
  221. if (lResult == ERROR_SUCCESS) {
  222. RegDeleteValue (hKey, szSnapInKeyForGPT);
  223. RegCloseKey (hKey);
  224. }
  225. cstrSubKey.Format(L"Software\\Microsoft\\MMC\\NodeTypes\\%ws", cszNodeTypeCertificateTemplate);
  226. RegDeleteKey (HKEY_LOCAL_MACHINE, cstrSubKey);
  227. cstrSubKey.Format(L"Software\\Microsoft\\MMC\\SnapIns\\%ws\\NodeTypes\\%ws", (LPCWSTR)szSnapInKey, cszNodeTypeCertificateTemplate);
  228. RegDeleteKey (HKEY_LOCAL_MACHINE, cstrSubKey);
  229. cstrSubKey.Format(L"Software\\Microsoft\\MMC\\SnapIns\\%ws\\NodeTypes", (LPCWSTR)szSnapInKeyForGPT);
  230. RegDeleteKey (HKEY_LOCAL_MACHINE, cstrSubKey);
  231. cstrSubKey.Format(L"Software\\Microsoft\\MMC\\SnapIns\\%ws", (LPCWSTR)szSnapInKeyForGPT);
  232. RegDeleteKey (HKEY_LOCAL_MACHINE, cstrSubKey);
  233. return S_OK;
  234. }
  235. VOID Usage()
  236. {
  237. CString cstrDllInstallUsageText;
  238. CString cstrDllInstallUsageTitle;
  239. cstrDllInstallUsageText.LoadString(IDS_DLL_INSTALL_USAGE_TEXT);
  240. cstrDllInstallUsageTitle.LoadString(IDS_DLL_INSTALL_USAGE_TITLE);
  241. MessageBox(NULL, cstrDllInstallUsageText, cstrDllInstallUsageTitle, MB_OK);
  242. }
  243. STDAPI DllInstall(BOOL bInstall, LPCWSTR pszCmdLine)
  244. {
  245. LPCWSTR wszCurrentCmd = pszCmdLine;
  246. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  247. // parse the cmd line
  248. while(wszCurrentCmd && *wszCurrentCmd)
  249. {
  250. while(*wszCurrentCmd == L' ')
  251. wszCurrentCmd++;
  252. if(*wszCurrentCmd == 0)
  253. break;
  254. switch(*wszCurrentCmd++)
  255. {
  256. case L'?':
  257. Usage();
  258. return S_OK;
  259. }
  260. }
  261. return S_OK;
  262. }