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
13 KiB

  1. // test harness for WMI GPO subsubsystem
  2. // hardcoded for the hhancedom domain in microsoft.com
  3. #include <windows.h>
  4. #include <initguid.h>
  5. #include <prsht.h>
  6. #include <wbemidl.h>
  7. #include <gpedit.h>
  8. #include <stdio.h>
  9. #define LINK_TARGET L"LDAP://DC=EssCool,DC=com"
  10. #define DOMAIN_NAME L"LDAP://DC=EssCool,DC=com"
  11. // {AAEAE720-0328-4763-8ECB-23422EDE2DB5}
  12. const CLSID CLSID_CSE =
  13. { 0xaaeae720, 0x328, 0x4763, { 0x8e, 0xcb, 0x23, 0x42, 0x2e, 0xde, 0x2d, 0xb5 } };
  14. // TODO: attempt to create namespace if not available.
  15. HRESULT GetNamespace(BSTR namespaceName, IWbemServices*& pNamespace)
  16. {
  17. HRESULT hr = WBEM_E_FAILED;
  18. IWbemLocator* pLoc = NULL;
  19. if (FAILED(hr = CoCreateInstance(CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER, IID_IWbemLocator, (LPVOID*) &pLoc)))
  20. printf("Could not create wbem locator (0x%08X)\n", hr);
  21. else
  22. {
  23. if (SUCCEEDED(hr = pLoc->ConnectServer(namespaceName, NULL,NULL, 0,0,0,0,&pNamespace)))
  24. printf("Retrieved %S namespace\n", namespaceName);
  25. else
  26. printf("ConnectServer(%s) failed (0x%08X)\n", namespaceName, hr);
  27. pLoc->Release();
  28. }
  29. return hr;
  30. }
  31. HRESULT GetPolicyNamespace(IWbemServices*& pPolicyNamespace)
  32. {
  33. HRESULT hr;
  34. BSTR bstr = SysAllocString(L"\\\\.\\ROOT\\POLICY");
  35. if (bstr)
  36. {
  37. hr = GetNamespace(bstr, pPolicyNamespace);
  38. SysFreeString(bstr);
  39. }
  40. else
  41. hr = WBEM_E_OUT_OF_MEMORY;
  42. if (FAILED(hr))
  43. printf("Failed to retrieve policy namespace (0x%08X)\n", hr);
  44. return hr;
  45. }
  46. HRESULT DeleteGPO(WCHAR *name)
  47. {
  48. LPGROUPPOLICYOBJECT pGPO = NULL;
  49. HRESULT hr;
  50. if (SUCCEEDED(hr = DeleteGPOLink(name,LINK_TARGET)))
  51. printf("deleted link\n");
  52. else
  53. printf("DeleteGPOLink failed with 0x%x.\n", hr);
  54. if (FAILED(hr = CoCreateInstance(CLSID_GroupPolicyObject, NULL,
  55. CLSCTX_SERVER, IID_IGroupPolicyObject,
  56. (void **)&pGPO)))
  57. printf("CoCreateInstance failed with 0x%x.\n", hr);
  58. else
  59. {
  60. if (FAILED(hr = pGPO->OpenDSGPO(name, 0)))
  61. printf("OpenDSGPO failed with 0x%x.\n", hr);
  62. else
  63. if (FAILED(hr = pGPO->Delete()))
  64. printf("Delete failed with 0x%x.\n", hr);
  65. else
  66. printf("Deleted %S\n", name);
  67. pGPO->Release();
  68. }
  69. return hr;
  70. }
  71. // communicate with the Group Policy Infrastructure which will
  72. // create a GPO object & return to us the path of the container
  73. // into which we should write our object
  74. HRESULT CreateGPO(const WCHAR* name, WCHAR* domain, WCHAR* szPath)
  75. {
  76. LPGROUPPOLICYOBJECT pGPO = NULL;
  77. HRESULT hr;
  78. if (FAILED(hr = CoCreateInstance(CLSID_GroupPolicyObject, NULL,
  79. CLSCTX_SERVER, IID_IGroupPolicyObject,
  80. (void **)&pGPO)))
  81. printf("CoCreateInstance failed with 0x%x.\n", hr);
  82. else
  83. {
  84. if (FAILED(hr = pGPO->New(domain, (WCHAR*)name, 0)))
  85. printf("New failed with 0x%x.\n", hr);
  86. else
  87. if (FAILED(hr= pGPO->GetDSPath(GPO_SECTION_MACHINE, szPath, MAX_PATH)))
  88. printf("GetDSPath failed with 0x%x.\n", hr);
  89. else
  90. {
  91. printf("\nGPO machine path: %S\n\n", szPath);
  92. // pGPO->GetDSPath(GPO_SECTION_USER, szPath, MAX_PATH);
  93. // printf("GPO user path: %S\n\n", szPath);
  94. WCHAR rootPath[MAX_PATH];
  95. if (FAILED(hr = pGPO->GetPath(rootPath, MAX_PATH)))
  96. printf("GetPath failed with 0x%x.\n", hr);
  97. else
  98. {
  99. printf("GPO root path: %S\n\n", rootPath);
  100. if (FAILED(hr = CreateGPOLink(rootPath, domain, FALSE)))
  101. printf("CreateGPOLink failed with 0x%x.\n", hr);
  102. else
  103. if (FAILED(hr = pGPO->Save(TRUE, TRUE, (struct _GUID *)&CLSID_CSE, (struct _GUID *)&CLSID_CSE) ))
  104. printf("Save failed with 0x%x.\n", hr);
  105. }
  106. }
  107. pGPO->Release();
  108. }
  109. return hr;
  110. }
  111. HRESULT PutRangeParams(IWbemServices* pPolicyNamespace, IWbemClassObject* pTemplate)
  112. {
  113. HRESULT hr = WBEM_S_NO_ERROR;
  114. SAFEARRAYBOUND
  115. arrayBounds;
  116. arrayBounds.lLbound = 0;
  117. arrayBounds.cElements = 1;
  118. SAFEARRAY* psa = SafeArrayCreate(VT_UNKNOWN, 1, &arrayBounds);
  119. if (!psa)
  120. {
  121. printf("Failed to create safe array\n");
  122. return WBEM_E_OUT_OF_MEMORY;
  123. }
  124. IWbemClassObject* pClass = NULL;
  125. BSTR bustard = SysAllocString(L"MSFT_SintRangeParam");
  126. if (SUCCEEDED(hr = pPolicyNamespace->GetObject(bustard, 0, NULL, &pClass, NULL)))
  127. {
  128. IWbemClassObject* pRange = NULL;
  129. if (FAILED(hr = pClass->SpawnInstance(0, &pRange)))
  130. {
  131. printf("pClass->SpawnInstance failed, 0x%08X\n", hr);
  132. return hr;
  133. }
  134. VARIANT v;
  135. VariantInit(&v);
  136. v.vt = VT_BSTR;
  137. v.bstrVal = SysAllocString(L"ID");
  138. pRange->Put(L"PropertyName",0,&v,NULL);
  139. VariantClear(&v);
  140. v.vt = VT_BSTR;
  141. v.bstrVal = SysAllocString(L"SINT32");
  142. pRange->Put(L"TargetClass",0,&v,NULL);
  143. VariantClear(&v);
  144. v.vt = VT_I4;
  145. v.lVal = 5;
  146. pRange->Put(L"Default",0,&v,NULL);
  147. VariantClear(&v);
  148. v.vt = VT_I4;
  149. v.lVal = CIM_SINT32;
  150. pRange->Put(L"TargetType",0,&v,NULL);
  151. v.vt = VT_UNKNOWN | VT_ARRAY;
  152. v.parray = psa;
  153. long index = 0;
  154. SafeArrayPutElement(psa, &index, pRange);
  155. hr = pTemplate->Put(L"RangeSettings", 0, &v, NULL);
  156. pRange->Release();
  157. pClass->Release();
  158. }
  159. else
  160. printf("Failed to retrieve MSFT_SintRangeParam, 0x%08X\n", hr);
  161. printf("PutRangeParams returning 0x%08X\n", hr);
  162. return hr;
  163. }
  164. // create template based on object
  165. // write it to ds, return key string
  166. HRESULT CreatePolicyTemplate(IWbemServices* pPolicyNamespace, WCHAR* keyString)
  167. {
  168. HRESULT hr = WBEM_E_FAILED;
  169. IWbemClassObject* pTemplateTemplate = NULL;
  170. BSTR bstr = SysAllocString(L"MSFT_MergeablePolicyTemplate");
  171. if (FAILED(hr = pPolicyNamespace->GetObject(bstr,0,NULL,&pTemplateTemplate,NULL)))
  172. printf("GetObject on MSFT_MergeablePolicyTemplate failed 0x%08X\n", hr);
  173. else
  174. {
  175. printf("Retrieved MSFT_MergeablePolicyTemplate\n");
  176. IWbemClassObject* pTemplate = NULL;
  177. if (FAILED(hr = pTemplateTemplate->SpawnInstance(0, &pTemplate)))
  178. printf("SpawnInstance on MSFT_MergeablePolicyTemplate failed 0x%08X\n", hr);
  179. else
  180. {
  181. printf("SpawnInstance on MSFT_MergeablePolicyTemplate Succeeded\n");
  182. VARIANT v;
  183. VariantInit(&v);
  184. v.vt = VT_BSTR;
  185. GUID guid;
  186. CoCreateGuid(&guid);
  187. WCHAR guidStr[128];
  188. StringFromGUID2(guid, guidStr, 128);
  189. v.bstrVal = SysAllocString(guidStr);
  190. pTemplate->Put(L"ID", 0, &v, NULL);
  191. SysFreeString(v.bstrVal);
  192. v.bstrVal = SysAllocString(L"LOCAL");
  193. pTemplate->Put(L"DsContext", 0, &v, NULL);
  194. SysFreeString(v.bstrVal);
  195. v.bstrVal = SysAllocString(L"root\\policy");
  196. pTemplate->Put(L"TargetNamespace", 0, &v, NULL);
  197. SysFreeString(v.bstrVal);
  198. v.bstrVal = SysAllocString(L"ModemSetting");
  199. pTemplate->Put(L"TargetClass", 0, &v, NULL);
  200. SysFreeString(v.bstrVal);
  201. v.bstrVal = SysAllocString(L"Description");
  202. pTemplate->Put(L"Description", 0, &v, NULL);
  203. SysFreeString(v.bstrVal);
  204. v.bstrVal = SysAllocString(L"ModemSetting.id=5");
  205. pTemplate->Put(L"TargetPath", 0, &v, NULL);
  206. SysFreeString(v.bstrVal);
  207. v.bstrVal = SysAllocString(L"None whatsoever");
  208. pTemplate->Put(L"SourceOrganization", 0, &v, NULL);
  209. SysFreeString(v.bstrVal);
  210. v.bstrVal = SysAllocString(L"20000101000000.000000-480");
  211. pTemplate->Put(L"ChangeDate", 0, &v, NULL);
  212. pTemplate->Put(L"CreationDate", 0, &v, NULL);
  213. SysFreeString(v.bstrVal);
  214. v.bstrVal = SysAllocString(L"Joe Bob");
  215. pTemplate->Put(L"Name", 0, &v, NULL);
  216. SysFreeString(v.bstrVal);
  217. v.bstrVal = SysAllocString(L"Joe Jack");
  218. pTemplate->Put(L"Author", 0, &v, NULL);
  219. SysFreeString(v.bstrVal);
  220. hr = PutRangeParams(pPolicyNamespace, pTemplate);
  221. if (SUCCEEDED(hr))
  222. {
  223. pTemplate->Get(L"__RELPATH", 0, &v, NULL, NULL);
  224. wcscpy(keyString, v.bstrVal);
  225. VariantClear(&v);
  226. if (SUCCEEDED(hr = pPolicyNamespace->PutInstance(pTemplate, WBEM_FLAG_USE_AMENDED_QUALIFIERS | WBEM_FLAG_CREATE_OR_UPDATE, NULL, NULL)))
  227. printf("Successfully put %S\n", keyString);
  228. else
  229. printf("PutInstance on MSFT_MergeablePolicyTemplate failed 0x%08X\n", hr);
  230. }
  231. pTemplate->Release();
  232. }
  233. pTemplateTemplate->Release();
  234. }
  235. SysFreeString(bstr);
  236. return hr;
  237. }
  238. // write WMIGPO object to DS
  239. // to path specified in szPath
  240. // containing keystring
  241. HRESULT WriteWMIGPO(IWbemServices* pPolicyNamespace, const WCHAR* szPath, const WCHAR* keyString)
  242. {
  243. HRESULT hr = WBEM_E_FAILED;
  244. IWbemClassObject* pWmiGpoClass = NULL;
  245. BSTR bstr = SysAllocString(L"MSFT_WMIGPO");
  246. if (FAILED(hr = pPolicyNamespace->GetObject(bstr,WBEM_FLAG_USE_AMENDED_QUALIFIERS,NULL,&pWmiGpoClass,NULL)))
  247. printf("GetObject on MSFT_WMIGPO failed 0x%08X\n", hr);
  248. else
  249. {
  250. IWbemClassObject* pWmiGpo = NULL;
  251. if (FAILED(hr = pWmiGpoClass->SpawnInstance(0, &pWmiGpo)))
  252. printf("SpawnInstance on MSFT_WMIGPO failed 0x%08X\n", hr);
  253. else
  254. {
  255. VARIANT v;
  256. VariantInit(&v);
  257. v.vt = VT_BSTR;
  258. v.bstrVal = SysAllocString(szPath);
  259. pWmiGpo->Put(L"DsPath", 0, &v, NULL);
  260. VariantClear(&v);
  261. v.vt = VT_BSTR | VT_ARRAY;
  262. SAFEARRAYBOUND
  263. arrayBounds;
  264. SAFEARRAY* pArray = NULL;
  265. arrayBounds.lLbound = 0;
  266. arrayBounds.cElements = 1;
  267. long index = 0;
  268. pArray = SafeArrayCreate(VT_BSTR, 1, &arrayBounds);
  269. SafeArrayPutElement(pArray, &index, SysAllocString(keyString));
  270. v.parray = pArray;
  271. pWmiGpo->Put(L"PolicyTemplate", 0, &v, NULL);
  272. SafeArrayDestroy(pArray);
  273. if (SUCCEEDED(hr = pPolicyNamespace->PutInstance(pWmiGpo, WBEM_FLAG_USE_AMENDED_QUALIFIERS | WBEM_FLAG_CREATE_OR_UPDATE, NULL, NULL)))
  274. printf("Successfully put MSFT_WMIGPO\n");
  275. else
  276. printf("PutInstance on MSFT_WMIGPO failed 0x%08X\n", hr);
  277. pWmiGpo->Release();
  278. }
  279. pWmiGpoClass->Release();
  280. }
  281. SysFreeString(bstr);
  282. return hr;
  283. }
  284. int __cdecl wmain(int argc, WCHAR *argv[])
  285. {
  286. CoInitialize(NULL);
  287. CoInitializeSecurity (NULL, -1, NULL, NULL,
  288. RPC_C_AUTHN_LEVEL_NONE, RPC_C_IMP_LEVEL_IMPERSONATE, NULL,
  289. EOAC_NONE, NULL);
  290. /**********************
  291. // create deleter with this...
  292. if (argc==2)
  293. DeleteGPO(argv[1]);
  294. else
  295. printf("one and only one argument, please - that's the path to the root of the gpo\n");
  296. ******************/
  297. WCHAR szPath[MAX_PATH];
  298. IWbemServices* pPolicyNamespace = NULL;
  299. if (argc==3)
  300. {
  301. if (SUCCEEDED(GetPolicyNamespace(pPolicyNamespace))
  302. &&
  303. SUCCEEDED(CreateGPO(L"WMI Test Policy", argv[1], szPath)))
  304. WriteWMIGPO(pPolicyNamespace, szPath, argv[2]);
  305. if (pPolicyNamespace)
  306. pPolicyNamespace->Release();
  307. }
  308. else if (argc==4)
  309. {
  310. if (SUCCEEDED(GetPolicyNamespace(pPolicyNamespace))
  311. &&
  312. SUCCEEDED(CreateGPO(argv[3], argv[1], szPath)))
  313. WriteWMIGPO(pPolicyNamespace, szPath, argv[2]);
  314. if (pPolicyNamespace)
  315. pPolicyNamespace->Release();
  316. }
  317. else
  318. printf("\nUSAGE:\n\n Loader [domain path] [Policy Template Path] <optional policy name>\n\nEXAMPLE (line breaks to improve readability):\n\n Loader LDAP://DC=EssCool,DC=com\n MSFT_MergeablePolicyTemplate.DsContext=\\\"LOCAL\\\",ID=\\\"{BA34...3471}\\\"\n MyPolicy\n");
  319. /*********************
  320. hard coded version
  321. WCHAR keyString[MAX_PATH];
  322. if (SUCCEEDED(GetPolicyNamespace(pPolicyNamespace))
  323. &&
  324. SUCCEEDED(CreatePolicyTemplate(pPolicyNamespace, keyString))
  325. &&
  326. SUCCEEDED(CreateGPO(L"fribbert", szPath))
  327. )
  328. WriteWMIGPO(pPolicyNamespace, szPath, keyString);
  329. if (pPolicyNamespace)
  330. pPolicyNamespace->Release();
  331. *************************/
  332. CoUninitialize();
  333. return 0;
  334. }