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.

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