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.

377 lines
9.1 KiB

  1. #include <windows.h>
  2. #include <wbemcli.h>
  3. #include <wbemprov.h>
  4. #include <stdio.h>
  5. #include <commain.h>
  6. #include <clsfac.h>
  7. #include <wbemcomn.h>
  8. #include <ql.h>
  9. #include <sync.h>
  10. #include <Dsrole.h>
  11. #include "utility.h"
  12. #include "PolicMan.h"
  13. #include "PolicTempl.h"
  14. #include "PolicSOM.h"
  15. #include "PolicType.h"
  16. #include "PolicRange.h"
  17. #include "PolicGPO.h"
  18. #include "PolicStatus.h"
  19. #include <tchar.h>
  20. #define REG_RUN_KEY L"Software\\Microsoft\\Windows\\CurrentVersion\\Run"
  21. class CMyServer : public CComServer
  22. {
  23. public:
  24. CMyServer(void) { InitGlobalNames(); }
  25. ~CMyServer(void) { FreeGlobalNames(); }
  26. HRESULT Initialize()
  27. {
  28. AddClassInfo(CLSID_PolicySOM,
  29. new CClassFactory<CPolicySOM>(GetLifeControl()),
  30. _T("WMI Policy SOM Provider"), TRUE);
  31. AddClassInfo(CLSID_PolicyStatus,
  32. new CClassFactory<CPolicyStatus>(GetLifeControl()),
  33. _T("WMI Policy Status Provider"), TRUE);
  34. return S_OK;
  35. }
  36. HRESULT InitializeCom()
  37. {
  38. return CoInitializeEx(NULL, COINIT_MULTITHREADED);
  39. }
  40. /*
  41. void Register(void)
  42. {
  43. wchar_t
  44. swKeyValue[] = L"RUNDLL32.EXE %systemroot%\\system32\\wbem\\policman.dll,CreateADContainers",
  45. swExpandedValue[512],
  46. swRunOnceKey[] = REG_RUN_KEY ;
  47. HKEY
  48. hkRunOnce;
  49. LONG
  50. lReturnCode;
  51. lReturnCode = ExpandEnvironmentStrings(swKeyValue, swExpandedValue, 512);
  52. lReturnCode = RegOpenKeyEx(HKEY_LOCAL_MACHINE, swRunOnceKey, 0, KEY_SET_VALUE, &hkRunOnce);
  53. if(ERROR_SUCCESS != lReturnCode)
  54. {
  55. // error
  56. }
  57. lReturnCode = RegSetValueEx(hkRunOnce, L"PolicMan", 0, REG_EXPAND_SZ, (BYTE *)swExpandedValue,
  58. (lstrlen(swExpandedValue)+1) * sizeof(wchar_t));
  59. if(ERROR_SUCCESS != lReturnCode)
  60. {
  61. // error
  62. }
  63. RegCloseKey(hkRunOnce);
  64. }
  65. */
  66. } Server;
  67. HRESULT GetOrCreateObj(CComQIPtr<IADsContainer, &IID_IADsContainer> &pIADsContainer_In,
  68. CComBSTR &bstrObjName,
  69. CComQIPtr<IADsContainer, &IID_IADsContainer> &pIADsContainer_Out)
  70. {
  71. HRESULT
  72. hres = WBEM_E_FAILED;
  73. CComQIPtr<IDispatch, &IID_IDispatch>
  74. pDisp;
  75. CComQIPtr<IDirectoryObject, &IID_IDirectoryObject>
  76. pDirectoryObj;
  77. CComQIPtr<IADsObjectOptions, &IID_IADsObjectOptions>
  78. pADsObjectOptions;
  79. CComVariant
  80. vSecurityOptions;
  81. ADSVALUE
  82. AdsValue[1];
  83. ADS_ATTR_INFO
  84. attrInfo[] = { { L"ntSecurityDescriptor", ADS_ATTR_UPDATE, ADSTYPE_NT_SECURITY_DESCRIPTOR, &AdsValue[0], 1} };
  85. CNtSecurityDescriptor
  86. cSD;
  87. DWORD
  88. dwModified;
  89. ADS_OBJECT_INFO
  90. *pADsInfo = NULL;
  91. if(NULL == pIADsContainer_In.p) return WBEM_E_FAILED;
  92. // **** get/create object
  93. hres = pIADsContainer_In->GetObject(L"Container", bstrObjName, &pDisp);
  94. if(FAILED(hres) || (NULL == pDisp.p))
  95. {
  96. CComQIPtr<IADs, &IID_IADs>
  97. pIADs;
  98. hres = pIADsContainer_In->Create(L"Container", bstrObjName, &pDisp);
  99. if(FAILED(hres) || (NULL == pDisp.p))
  100. {
  101. ERRORTRACE((LOG_ESS, "POLICMAN: (Container Creation) Could create container %S : 0x%x\n", (BSTR)bstrObjName, hres));
  102. return hres;
  103. }
  104. // **** write object to AD
  105. pIADs = pDisp;
  106. hres = pIADs->SetInfo();
  107. if(FAILED(hres))
  108. {
  109. ERRORTRACE((LOG_ESS, "POLICMAN: (Container Creation) Could write container %S to DS : 0x%x\n", (BSTR)bstrObjName, hres));
  110. return hres;
  111. }
  112. }
  113. // **** set object security option
  114. pADsObjectOptions = pDisp;
  115. vSecurityOptions = (ADS_SECURITY_INFO_OWNER | ADS_SECURITY_INFO_DACL);
  116. hres = pADsObjectOptions->SetOption(ADS_OPTION_SECURITY_MASK, vSecurityOptions);
  117. if(FAILED(hres))
  118. {
  119. ERRORTRACE((LOG_ESS, "POLICMAN: (Container Creation) Could set security options on object : 0x%x\n", hres));
  120. return hres;
  121. }
  122. // **** create security descriptor
  123. hres = CreateDefaultSecurityDescriptor(cSD);
  124. if(FAILED(hres))
  125. {
  126. ERRORTRACE((LOG_ESS, "POLICMAN: (Container Creation) Could create security descriptor : 0x%x\n", hres));
  127. return hres;
  128. }
  129. // **** set object security descriptor
  130. AdsValue[0].dwType = ADSTYPE_NT_SECURITY_DESCRIPTOR;
  131. AdsValue[0].SecurityDescriptor.dwLength = cSD.GetSize();
  132. AdsValue[0].SecurityDescriptor.lpValue = (LPBYTE)cSD.GetPtr();
  133. pDirectoryObj = pDisp;
  134. hres = pDirectoryObj->SetObjectAttributes(attrInfo, 1, &dwModified);
  135. if(FAILED(hres))
  136. {
  137. ERRORTRACE((LOG_ESS, "POLICMAN: (Container Creation) Could set security on object : 0x%x\n", hres));
  138. return hres;
  139. }
  140. pIADsContainer_Out = pDirectoryObj;
  141. return WBEM_S_NO_ERROR;
  142. }
  143. #define SYSTEM_PATH L"LDAP://CN=System,"
  144. #define WMIPOLICY_PATH L"CN=WMIPolicy"
  145. #define TEMPLATE_PATH L"CN=PolicyTemplate"
  146. #define TYPE_PATH L"CN=PolicyType"
  147. #define GPO_PATH L"CN=WMIGPO"
  148. #define SOM_PATH L"CN=SOM"
  149. HRESULT InScopeOfCOM_CreateADContainers(void)
  150. {
  151. HRESULT
  152. hres = WBEM_E_FAILED;
  153. PDSROLE_PRIMARY_DOMAIN_INFO_BASIC
  154. pBasic;
  155. CComPtr<IADs>
  156. pRootDSE;
  157. CComQIPtr<IADs, &IID_IADs>
  158. pObj;
  159. CComQIPtr<IADsContainer, &IID_IADsContainer>
  160. pWMIPolicyObj,
  161. pSystemObj,
  162. pADsContainer;
  163. CComVariant
  164. vDomainName;
  165. CComBSTR
  166. bstrSystemPath(SYSTEM_PATH),
  167. bstrWMIPolicy(WMIPOLICY_PATH),
  168. bstrTemplate(TEMPLATE_PATH),
  169. bstrType(TYPE_PATH),
  170. bstrSom(SOM_PATH),
  171. bstrGPO(GPO_PATH);
  172. // **** delay until AD is up and running
  173. DWORD
  174. dwResult = DsRoleGetPrimaryDomainInformation(NULL, DsRolePrimaryDomainInfoBasic,
  175. (PBYTE *)&pBasic);
  176. if(dwResult == ERROR_SUCCESS)
  177. {
  178. // **** Check if this is a DC
  179. if((pBasic->MachineRole == DsRole_RoleBackupDomainController) ||
  180. (pBasic->MachineRole == DsRole_RolePrimaryDomainController))
  181. {
  182. HANDLE
  183. hEvent;
  184. hEvent = OpenEvent(EVENT_ALL_ACCESS, FALSE, TEXT("NtdsDelayedStartupCompletedEvent") );
  185. if(hEvent) {
  186. WaitForSingleObject(hEvent, 50000);
  187. CloseHandle (hEvent);
  188. }
  189. }
  190. }
  191. // **** get LDAP name of domain controller
  192. hres = ADsGetObject(L"LDAP://rootDSE", IID_IADs, (void**)&pRootDSE);
  193. if(FAILED(hres))
  194. {
  195. ERRORTRACE((LOG_ESS, "POLICMAN: (Container Creation) Could not get pointer to LDAP://rootDSE : 0x%x\n", hres));
  196. return hres;
  197. }
  198. else
  199. {
  200. hres = pRootDSE->Get(L"defaultNamingContext", &vDomainName);
  201. if(FAILED(hres) || (V_VT(&vDomainName) != VT_BSTR) || (V_BSTR(&vDomainName) == NULL))
  202. {
  203. ERRORTRACE((LOG_ESS, "POLICMAN: (Container Creation) could not get defaultNamingContext : 0x%x\n", hres));
  204. return hres;
  205. }
  206. bstrSystemPath.Append(vDomainName.bstrVal);
  207. }
  208. // **** get system path
  209. hres = ADsGetObject(bstrSystemPath, IID_IADsContainer, (void **)&pSystemObj);
  210. if (FAILED(hres))
  211. {
  212. ERRORTRACE((LOG_ESS, "POLICMAN: (Container Creation) Could not get pointer to %S : 0x%x\n", (BSTR)bstrSystemPath, hres));
  213. return hres;
  214. }
  215. // **** get/create WMIPolicy containers
  216. hres = GetOrCreateObj(pSystemObj, bstrWMIPolicy, pWMIPolicyObj);
  217. if(FAILED(hres))
  218. {
  219. ERRORTRACE((LOG_ESS, "POLICMAN: (Container Creation) Could not create/get pointer to %S : 0x%x\n", (BSTR)bstrWMIPolicy, hres));
  220. return hres;
  221. }
  222. else
  223. {
  224. hres = GetOrCreateObj(pWMIPolicyObj, bstrTemplate, pADsContainer);
  225. if(FAILED(hres))
  226. {
  227. ERRORTRACE((LOG_ESS, "POLICMAN: (Container Creation) Could not create/get pointer to %S : 0x%x\n", (BSTR)bstrTemplate, hres));
  228. return hres;
  229. }
  230. hres = GetOrCreateObj(pWMIPolicyObj, bstrType, pADsContainer);
  231. if(FAILED(hres))
  232. {
  233. ERRORTRACE((LOG_ESS, "POLICMAN: (Container Creation) Could not create/get pointer to %S : 0x%x\n", (BSTR)bstrType, hres));
  234. return hres;
  235. }
  236. hres = GetOrCreateObj(pWMIPolicyObj, bstrSom, pADsContainer);
  237. if(FAILED(hres))
  238. {
  239. ERRORTRACE((LOG_ESS, "POLICMAN: (Container Creation) Could not create/get pointer to %S : 0x%x\n", (BSTR)bstrSom, hres));
  240. return hres;
  241. }
  242. hres = GetOrCreateObj(pWMIPolicyObj, bstrGPO, pADsContainer);
  243. if(FAILED(hres))
  244. {
  245. ERRORTRACE((LOG_ESS, "POLICMAN: (Container Creation) Could not create/get pointer to %S : 0x%x\n", (BSTR)bstrGPO, hres));
  246. return hres;
  247. }
  248. }
  249. return S_OK;
  250. }
  251. extern "C" STDAPI CreateADContainers(void)
  252. {
  253. HRESULT
  254. hres = WBEM_E_FAILED;
  255. // **** init process context
  256. CoInitialize(NULL);
  257. CoInitializeSecurity (NULL, -1, NULL, NULL,
  258. RPC_C_AUTHN_LEVEL_NONE, RPC_C_IMP_LEVEL_IMPERSONATE, NULL,
  259. EOAC_NONE, NULL);
  260. try
  261. {
  262. hres = InScopeOfCOM_CreateADContainers();
  263. }
  264. catch(...)
  265. {
  266. // **** error
  267. return WBEM_E_FAILED;
  268. }
  269. // **** if we returned successfully, then remove the run key
  270. if(SUCCEEDED(hres))
  271. {
  272. wchar_t
  273. swKeyValue[] = L"RUNDLL32.EXE %systemroot%\\system32\\wbem\\policman.dll,CreateADContainers",
  274. swExpandedKeyValue[512],
  275. swRunOnceKey[] = REG_RUN_KEY ;
  276. HKEY
  277. hkRunOnce;
  278. LONG
  279. lReturnCode;
  280. lReturnCode = RegOpenKeyEx(HKEY_LOCAL_MACHINE, swRunOnceKey, 0, KEY_SET_VALUE, &hkRunOnce);
  281. if(ERROR_SUCCESS != lReturnCode)
  282. {
  283. // error
  284. }
  285. lReturnCode = RegDeleteValue(hkRunOnce, L"PolicMan");
  286. if(ERROR_SUCCESS != lReturnCode)
  287. {
  288. // error
  289. }
  290. }
  291. // **** cleanup and shutdown
  292. CoUninitialize();
  293. return S_OK;
  294. }