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.

889 lines
28 KiB

  1. #undef _WIN32_WINNT
  2. #define _WIN32_WINNT 0x0500
  3. #include <wbemcli.h>
  4. #include <wbemprov.h>
  5. #include <atlbase.h>
  6. #include <stdio.h> // fprintf
  7. #include <stdlib.h>
  8. #include <locale.h>
  9. #include <genlex.h>
  10. #include <qllex.h>
  11. #include <ql.h>
  12. #include <ntdsapi.h>
  13. #include "objpath.h"
  14. #include "iads.h"
  15. #include "adshlp.h"
  16. #include "Utility.h"
  17. #include "policsom.h"
  18. #include <SDDL.H>
  19. BSTR
  20. // **** misc names
  21. g_bstrEmptyString = NULL,
  22. g_bstrEmptyDate = NULL;
  23. // **** AD schema names
  24. BSTR
  25. g_bstrADAuthor = NULL,
  26. g_bstrADChangeDate = NULL,
  27. g_bstrADClassDefinition = NULL,
  28. g_bstrADCreationDate = NULL,
  29. g_bstrADDescription = NULL,
  30. g_bstrADIntDefault = NULL,
  31. g_bstrADInt8Default = NULL,
  32. g_bstrADID = NULL,
  33. g_bstrADIntMax = NULL,
  34. g_bstrADInt8Max = NULL,
  35. g_bstrADIntMin = NULL,
  36. g_bstrADInt8Min = NULL,
  37. g_bstrADIntValidValues = NULL,
  38. g_bstrADName = NULL,
  39. g_bstrADNormalizedClass = NULL,
  40. g_bstrADObjectClass = NULL,
  41. g_bstrADParam2 = NULL,
  42. g_bstrADPolicyType = NULL,
  43. g_bstrADPropertyName = NULL,
  44. g_bstrADQuery = NULL,
  45. g_bstrADQueryLanguage = NULL,
  46. g_bstrADSourceOrganization = NULL,
  47. g_bstrADStringDefault = NULL,
  48. g_bstrADStringValidValues = NULL,
  49. g_bstrADTargetClass = NULL,
  50. g_bstrADTargetNameSpace = NULL,
  51. g_bstrADTargetObject = NULL,
  52. g_bstrADTargetPath = NULL,
  53. g_bstrADTargetType = NULL;
  54. // **** AD class names
  55. BSTR
  56. g_bstrADClassMergeablePolicy = NULL,
  57. g_bstrADClassRangeParam = NULL,
  58. g_bstrADClassRangeSint32 = NULL,
  59. g_bstrADClassRangeUint32 = NULL,
  60. g_bstrADClassRangeReal = NULL,
  61. g_bstrADClassParamUnknown = NULL,
  62. g_bstrADClassSetSint32 = NULL,
  63. g_bstrADClassSetUint32 = NULL,
  64. g_bstrADClassSetString = NULL,
  65. g_bstrADClassSimplePolicy = NULL,
  66. g_bstrADClassRule = NULL,
  67. g_bstrADClassSom = NULL,
  68. g_bstrADClassPolicyType = NULL,
  69. g_bstrADClassWMIGPO = NULL;
  70. // **** CIM schema names
  71. BSTR
  72. g_bstrAuthor = NULL,
  73. g_bstrChangeDate = NULL,
  74. g_bstrClassDefinition = NULL,
  75. g_bstrCreationDate = NULL,
  76. g_bstrDefault = NULL,
  77. g_bstrDescription = NULL,
  78. g_bstrDsPath = NULL,
  79. g_bstrDomain = NULL,
  80. g_bstrID = NULL,
  81. g_bstrMax = NULL,
  82. g_bstrMin = NULL,
  83. g_bstrName = NULL,
  84. g_bstrPolicyType = NULL,
  85. g_bstrPropertyName = NULL,
  86. g_bstrQuery = NULL,
  87. g_bstrQueryLanguage = NULL,
  88. g_bstrRangeSettings = NULL,
  89. g_bstrRules = NULL,
  90. g_bstrSourceOrganization = NULL,
  91. g_bstrTargetClass = NULL,
  92. g_bstrTargetNameSpace = NULL,
  93. g_bstrTargetObject = NULL,
  94. g_bstrTargetPath = NULL,
  95. g_bstrTargetType = NULL,
  96. g_bstrValidValues = NULL;
  97. // **** CIM class names
  98. BSTR
  99. g_bstrClassMergeablePolicy = NULL,
  100. g_bstrClassRangeParam = NULL,
  101. g_bstrClassRangeSint32 = NULL,
  102. g_bstrClassRangeUint32 = NULL,
  103. g_bstrClassRangeReal = NULL,
  104. g_bstrClassSetSint32 = NULL,
  105. g_bstrClassSetUint32 = NULL,
  106. g_bstrClassSetString = NULL,
  107. g_bstrClassSimplePolicy = NULL,
  108. g_bstrClassRule = NULL,
  109. g_bstrClassSom = NULL,
  110. g_bstrClassSomFilterStatus = NULL,
  111. g_bstrClassPolicyType = NULL,
  112. g_bstrClassWMIGPO = NULL;
  113. // **** CIM standard names
  114. BSTR
  115. g_bstrMISCclassSchema = NULL,
  116. g_bstrMISCContainer = NULL,
  117. g_bstrMISCdefaultNamingContext = NULL,
  118. g_bstrMISCschemaNamingContext = NULL,
  119. g_bstrMISCWQL = NULL,
  120. g_bstrMISCQuery = NULL;
  121. void InitGlobalNames(void)
  122. {
  123. // **** misc names
  124. g_bstrEmptyString = SysAllocString(L" ");
  125. g_bstrEmptyDate = SysAllocString(L"00000000000000.000000-000");
  126. // **** AD schema names
  127. g_bstrADAuthor = SysAllocString(L"msWMI-Author");
  128. g_bstrADChangeDate = SysAllocString(L"msWMI-ChangeDate");
  129. g_bstrADClassDefinition = SysAllocString(L"msWMI-ClassDefinition"),
  130. g_bstrADCreationDate = SysAllocString(L"msWMI-CreationDate");
  131. g_bstrADDescription = SysAllocString(L"msWMI-Parm1");
  132. g_bstrADIntDefault = SysAllocString(L"msWMI-IntDefault");
  133. g_bstrADInt8Default = SysAllocString(L"msWMI-Int8Default");
  134. g_bstrADID = SysAllocString(L"msWMI-ID");
  135. g_bstrADIntMax = SysAllocString(L"msWMI-IntMax");
  136. g_bstrADInt8Max = SysAllocString(L"msWMI-Int8Max");
  137. g_bstrADIntMin = SysAllocString(L"msWMI-IntMin");
  138. g_bstrADInt8Min = SysAllocString(L"msWMI-Int8Min");
  139. g_bstrADIntValidValues = SysAllocString(L"msWMI-IntValidValues");
  140. g_bstrADName = SysAllocString(L"msWMI-Name");
  141. g_bstrADNormalizedClass = SysAllocString(L"msWMI-NormalizedClass");
  142. g_bstrADObjectClass = SysAllocString(L"objectClass");
  143. g_bstrADParam2 = SysAllocString(L"msWMI-Parm2");
  144. g_bstrADPolicyType = SysAllocString(L"msWMI-PolicyType");
  145. g_bstrADPropertyName = SysAllocString(L"msWMI-PropertyName");
  146. g_bstrADQuery = SysAllocString(L"msWMI-Query");
  147. g_bstrADQueryLanguage = SysAllocString(L"msWMI-QueryLanguage");
  148. g_bstrADStringDefault = SysAllocString(L"msWMI-StringDefault");
  149. g_bstrADStringValidValues = SysAllocString(L"msWMI-StringValidValues");
  150. g_bstrADSourceOrganization = SysAllocString(L"msWMI-SourceOrganization");
  151. g_bstrADTargetClass = SysAllocString(L"msWMI-TargetClass");
  152. g_bstrADTargetNameSpace = SysAllocString(L"msWMI-TargetNameSpace");
  153. g_bstrADTargetObject = SysAllocString(L"msWMI-TargetObject");
  154. g_bstrADTargetPath = SysAllocString(L"msWMI-TargetPath");
  155. g_bstrADTargetType = SysAllocString(L"msWMI-TargetType");
  156. // **** AD class names
  157. g_bstrADClassMergeablePolicy = SysAllocString(L"msWMI-MergeablePolicyTemplate");
  158. g_bstrADClassRangeParam = SysAllocString(L"msWMI-RangeParam");
  159. g_bstrADClassRangeSint32 = SysAllocString(L"msWMI-IntRangeParam");
  160. g_bstrADClassRangeUint32 = SysAllocString(L"msWMI-UintRangeParam");
  161. g_bstrADClassRangeReal = SysAllocString(L"msWMI-RealRangeParam");
  162. g_bstrADClassParamUnknown = SysAllocString(L"msWMI-UnknownRangeParam");
  163. g_bstrADClassSetSint32 = SysAllocString(L"msWMI-IntSetParam");
  164. g_bstrADClassSetUint32 = SysAllocString(L"msWMI-UintSetParam");
  165. g_bstrADClassSetString = SysAllocString(L"msWMI-StringSetParam");
  166. g_bstrADClassSimplePolicy = SysAllocString(L"msWMI-SimplePolicyTemplate");
  167. g_bstrADClassRule = SysAllocString(L"msWMI-Rule");
  168. g_bstrADClassSom = SysAllocString(L"msWMI-SOM");
  169. g_bstrADClassPolicyType = SysAllocString(L"msWMI-PolicyType");
  170. g_bstrADClassWMIGPO = SysAllocString(L"msWMI-WMIGPO");
  171. // **** CIM Attribute Names
  172. g_bstrAuthor = SysAllocString(L"Author");
  173. g_bstrChangeDate = SysAllocString(L"ChangeDate");
  174. g_bstrClassDefinition = SysAllocString(L"ClassDefinition"),
  175. g_bstrCreationDate = SysAllocString(L"CreationDate");
  176. g_bstrDefault = SysAllocString(L"Default");
  177. g_bstrDescription = SysAllocString(L"Description");
  178. g_bstrDsPath = SysAllocString(L"DsPath");
  179. g_bstrDomain = SysAllocString(L"Domain");
  180. g_bstrID = SysAllocString(L"ID");
  181. g_bstrMax = SysAllocString(L"Max");
  182. g_bstrMin = SysAllocString(L"Min");
  183. g_bstrName = SysAllocString(L"Name");
  184. g_bstrPolicyType = SysAllocString(L"PolicyType");
  185. g_bstrPropertyName = SysAllocString(L"PropertyName");
  186. g_bstrQuery = SysAllocString(L"query");
  187. g_bstrQueryLanguage = SysAllocString(L"QueryLanguage");
  188. g_bstrRangeSettings = SysAllocString(L"RangeSettings");
  189. g_bstrRules = SysAllocString(L"Rules");
  190. g_bstrSourceOrganization = SysAllocString(L"SourceOrganization");
  191. g_bstrTargetClass = SysAllocString(L"TargetClass"),
  192. g_bstrTargetNameSpace = SysAllocString(L"TargetNamespace");
  193. g_bstrTargetObject = SysAllocString(L"TargetObject"),
  194. g_bstrTargetPath = SysAllocString(L"TargetPath"),
  195. g_bstrTargetType = SysAllocString(L"TargetType"),
  196. g_bstrValidValues = SysAllocString(L"ValidValues");
  197. // **** CIM class names
  198. g_bstrClassMergeablePolicy = SysAllocString(L"MSFT_MergeablePolicyTemplate");
  199. g_bstrClassRangeParam = SysAllocString(L"MSFT_RangeParam");
  200. g_bstrClassRangeSint32 = SysAllocString(L"MSFT_SintRangeParam");
  201. g_bstrClassRangeUint32 = SysAllocString(L"MSFT_UintRangeParam");
  202. g_bstrClassRangeReal = SysAllocString(L"MSFT_RealRangeParam");
  203. g_bstrClassSetSint32 = SysAllocString(L"MSFT_SintSetParam");
  204. g_bstrClassSetUint32 = SysAllocString(L"MSFT_UintSetParam");
  205. g_bstrClassSetString = SysAllocString(L"MSFT_StringSetParam");
  206. g_bstrClassSimplePolicy = SysAllocString(L"MSFT_SimplePolicyTemplate");
  207. g_bstrClassRule = SysAllocString(L"MSFT_Rule");
  208. g_bstrClassSom = SysAllocString(L"MSFT_SomFilter");
  209. g_bstrClassSomFilterStatus = SysAllocString(L"MSFT_SomFilterStatus");
  210. g_bstrClassPolicyType = SysAllocString(L"MSFT_PolicyType");
  211. g_bstrClassWMIGPO = SysAllocString(L"MSFT_WMIGPO");
  212. // **** misscelaneious standard names
  213. g_bstrMISCclassSchema = SysAllocString(L"classSchema");
  214. g_bstrMISCContainer = SysAllocString(L"Container"),
  215. g_bstrMISCdefaultNamingContext = SysAllocString(L"defaultNamingContext");
  216. g_bstrMISCschemaNamingContext = SysAllocString(L"schemaNamingContext");
  217. g_bstrMISCWQL = SysAllocString(L"WQL");
  218. g_bstrMISCQuery = SysAllocString(L"Query");
  219. }
  220. void FreeGlobalNames(void)
  221. {
  222. // **** misc names
  223. SysFreeString(g_bstrEmptyString);
  224. SysFreeString(g_bstrEmptyDate);
  225. // **** AD schema names
  226. SysFreeString(g_bstrADAuthor);
  227. SysFreeString(g_bstrADChangeDate);
  228. SysFreeString(g_bstrADClassDefinition);
  229. SysFreeString(g_bstrADCreationDate);
  230. SysFreeString(g_bstrADIntDefault);
  231. SysFreeString(g_bstrADInt8Default);
  232. SysFreeString(g_bstrADID);
  233. SysFreeString(g_bstrADIntMax);
  234. SysFreeString(g_bstrADInt8Max);
  235. SysFreeString(g_bstrADIntMin);
  236. SysFreeString(g_bstrADInt8Min);
  237. SysFreeString(g_bstrADIntValidValues);
  238. SysFreeString(g_bstrADName);
  239. SysFreeString(g_bstrADNormalizedClass);
  240. SysFreeString(g_bstrADObjectClass);
  241. SysFreeString(g_bstrADParam2);
  242. SysFreeString(g_bstrADPolicyType);
  243. SysFreeString(g_bstrADPropertyName);
  244. SysFreeString(g_bstrADQuery);
  245. SysFreeString(g_bstrADQueryLanguage);
  246. SysFreeString(g_bstrADStringDefault);
  247. SysFreeString(g_bstrADStringValidValues);
  248. SysFreeString(g_bstrADSourceOrganization);
  249. SysFreeString(g_bstrADTargetClass);
  250. SysFreeString(g_bstrADTargetNameSpace);
  251. SysFreeString(g_bstrADTargetObject);
  252. SysFreeString(g_bstrADTargetPath);
  253. SysFreeString(g_bstrADTargetType);
  254. // **** AD class names
  255. SysFreeString(g_bstrADClassMergeablePolicy);
  256. SysFreeString(g_bstrADClassRangeParam);
  257. SysFreeString(g_bstrADClassRangeSint32);
  258. SysFreeString(g_bstrADClassRangeUint32);
  259. SysFreeString(g_bstrADClassRangeReal);
  260. SysFreeString(g_bstrADClassParamUnknown);
  261. SysFreeString(g_bstrADClassSetSint32);
  262. SysFreeString(g_bstrADClassSetUint32);
  263. SysFreeString(g_bstrADClassSetString);
  264. SysFreeString(g_bstrADClassSimplePolicy);
  265. SysFreeString(g_bstrADClassRule);
  266. SysFreeString(g_bstrADClassSom);
  267. SysFreeString(g_bstrADClassPolicyType);
  268. SysFreeString(g_bstrADClassWMIGPO);
  269. // **** CIM Attribute Names
  270. SysFreeString(g_bstrAuthor);
  271. SysFreeString(g_bstrChangeDate);
  272. SysFreeString(g_bstrClassDefinition);
  273. SysFreeString(g_bstrCreationDate);
  274. SysFreeString(g_bstrDefault);
  275. SysFreeString(g_bstrDsPath);
  276. SysFreeString(g_bstrDomain);
  277. SysFreeString(g_bstrID);
  278. SysFreeString(g_bstrMax);
  279. SysFreeString(g_bstrMin);
  280. SysFreeString(g_bstrName);
  281. SysFreeString(g_bstrPolicyType);
  282. SysFreeString(g_bstrPropertyName);
  283. SysFreeString(g_bstrQuery);
  284. SysFreeString(g_bstrQueryLanguage);
  285. SysFreeString(g_bstrRangeSettings);
  286. SysFreeString(g_bstrRules);
  287. SysFreeString(g_bstrSourceOrganization);
  288. SysFreeString(g_bstrTargetClass);
  289. SysFreeString(g_bstrTargetNameSpace);
  290. SysFreeString(g_bstrTargetObject);
  291. SysFreeString(g_bstrTargetPath);
  292. SysFreeString(g_bstrTargetType);
  293. SysFreeString(g_bstrValidValues);
  294. // **** CIM class names
  295. SysFreeString(g_bstrClassMergeablePolicy);
  296. SysFreeString(g_bstrClassRangeParam);
  297. SysFreeString(g_bstrClassRangeSint32);
  298. SysFreeString(g_bstrClassRangeUint32);
  299. SysFreeString(g_bstrClassRangeReal);
  300. SysFreeString(g_bstrClassSetSint32);
  301. SysFreeString(g_bstrClassSetUint32);
  302. SysFreeString(g_bstrClassSetString);
  303. SysFreeString(g_bstrClassSimplePolicy);
  304. SysFreeString(g_bstrClassRule);
  305. SysFreeString(g_bstrClassSom);
  306. SysFreeString(g_bstrClassSomFilterStatus);
  307. SysFreeString(g_bstrClassPolicyType);
  308. SysFreeString(g_bstrClassWMIGPO);
  309. // **** CIM standard names
  310. SysFreeString(g_bstrMISCclassSchema);
  311. SysFreeString(g_bstrMISCContainer);
  312. SysFreeString(g_bstrMISCschemaNamingContext);
  313. SysFreeString(g_bstrMISCdefaultNamingContext);
  314. SysFreeString(g_bstrMISCWQL),
  315. SysFreeString(g_bstrMISCQuery);
  316. }
  317. // TODO: attempt to create namespace if not available.
  318. HRESULT GetNamespace(BSTR namespaceName, IWbemServices*& pNamespace, bool bInProc)
  319. {
  320. HRESULT hr = WBEM_E_FAILED;
  321. IWbemLocator* pLoc = NULL;
  322. if (FAILED(hr = CoCreateInstance(bInProc ? CLSID_WbemAdministrativeLocator : CLSID_WbemLocator,
  323. 0, CLSCTX_INPROC_SERVER, IID_IWbemLocator,
  324. (LPVOID*) &pLoc)))
  325. ERRORTRACE((LOG_ESS, "Could not create wbem locator (0x%08X)\n", hr));
  326. else
  327. {
  328. DEBUGTRACE((LOG_ESS, "Created Locator\n"));
  329. if (FAILED(hr = pLoc->ConnectServer(namespaceName, NULL,NULL, 0,0,0,0,&pNamespace)))
  330. ERRORTRACE((LOG_ESS, "ConnectServer(%S) failed (0x%08X)\n", namespaceName, hr));
  331. else
  332. DEBUGTRACE((LOG_ESS, "ConnectServer(%S) succeeded (0x%08X)\n", namespaceName, hr));
  333. pLoc->Release();
  334. }
  335. return hr;
  336. }
  337. // make sure that the ID property of pObj has a value
  338. // assumes property is a BSTR!
  339. // will generate GUID if not
  340. // if pName == NULL, assumes property name is "ID"
  341. // returns WBEM_S_NO_ERROR if ID generated
  342. // WBEM_S_FALSE if no ID generated (already has a value)
  343. // WBEM_E_NOT_FOUND if ID property is not
  344. // some error if error of some sort
  345. HRESULT EnsureID(IWbemClassObject* pObj, WCHAR* pName)
  346. {
  347. HRESULT hr = WBEM_S_NO_ERROR;
  348. WCHAR* pKeyName = (pName == NULL) ? L"ID" : pName;
  349. CComVariant
  350. v;
  351. if (SUCCEEDED(hr = pObj->Get(pKeyName, 0, &v, NULL, NULL)))
  352. {
  353. if ((v.vt == VT_NULL) || (v.bstrVal == NULL))
  354. {
  355. GUID guid;
  356. CoCreateGuid(&guid);
  357. BSTR guidStr = SysAllocStringByteLen(NULL, 129);
  358. if (guidStr == NULL)
  359. hr = WBEM_E_OUT_OF_MEMORY;
  360. else
  361. {
  362. StringFromGUID2(guid, guidStr, 128);
  363. VARIANT v;
  364. VariantInit(&v);
  365. v.vt = VT_BSTR;
  366. v.bstrVal = guidStr;
  367. pObj->Put(pKeyName, 0, &v, NULL);
  368. SysFreeString(guidStr);
  369. }
  370. }
  371. else
  372. {
  373. VariantClear(&v);
  374. hr = (HRESULT)WBEM_S_FALSE;
  375. }
  376. }
  377. return hr;
  378. }
  379. void Init_AdsAttrInfo(ADS_ATTR_INFO *pAdsAttrInfo,
  380. LPWSTR bstrName,
  381. DWORD control,
  382. ADSTYPE type,
  383. PADSVALUE pVals,
  384. DWORD nVals)
  385. {
  386. unsigned long c1;
  387. if(NULL == pAdsAttrInfo) return;
  388. pAdsAttrInfo->pszAttrName = bstrName;
  389. pAdsAttrInfo->dwControlCode = control;
  390. pAdsAttrInfo->dwADsType = type;
  391. if(nVals > 0)
  392. {
  393. pAdsAttrInfo->dwNumValues = nVals;
  394. if(NULL != pVals)
  395. {
  396. pAdsAttrInfo->pADsValues = pVals;
  397. }
  398. else
  399. {
  400. throw L"could not init ADS_ATTR_INFO structure";
  401. }
  402. for(c1 = 0; c1 < nVals; c1++)
  403. {
  404. (pAdsAttrInfo->pADsValues + c1)->dwType = type;
  405. }
  406. }
  407. else
  408. {
  409. pAdsAttrInfo->dwNumValues = 0;
  410. pAdsAttrInfo->pADsValues = NULL;
  411. }
  412. }
  413. HRESULT ADSIToWMIErrorCodes(HRESULT hresAD)
  414. {
  415. // **** first check that we were handed a wbem error
  416. if(((hresAD & 0x800FF000) == 0x80041000) ||
  417. ((hresAD & 0x800FF000) == 0x80042000) ||
  418. ((hresAD & 0x800FF000) == 0x80044000)) return hresAD;
  419. if(hresAD != WBEM_S_NO_ERROR) switch(hresAD)
  420. {
  421. case S_ADS_ERRORSOCCURRED : return WBEM_E_FAILED ;
  422. case S_ADS_NOMORE_ROWS : return WBEM_S_NO_MORE_DATA ;
  423. case S_ADS_NOMORE_COLUMNS : return WBEM_S_NO_MORE_DATA ;
  424. case E_ADS_UNKNOWN_OBJECT : return WBEM_E_NOT_FOUND ;
  425. case E_ADS_OBJECT_EXISTS : return WBEM_E_ALREADY_EXISTS;
  426. case 0x80072014 : return WBEM_E_INVALID_OBJECT; // The requested operation did not
  427. // satisfy one or more constraints
  428. // associated with the class of the object.
  429. case 0x8007202B : return WBEM_E_NOT_AVAILABLE; // A referral was returned from the server.
  430. case 0x80072032 : return WBEM_E_INVALID_SYNTAX; // LDAP_INVALID_DN_SYNTAX
  431. case 0x80070005 : return WBEM_E_ACCESS_DENIED; // LDAP_INSUFFICIENT_RIGHTS
  432. case 0x8007054b : return WBEM_E_NOT_AVAILABLE; // The specified domain either does not exist
  433. // or could not be contacted.
  434. case 0x80072030 : return WBEM_E_NOT_FOUND ; // LDAP_NO_SUCH_OBJECT
  435. case 0x8007200a : return WBEM_E_FAILED; //LDAP_NO_SUCH_ATTRIBUTE
  436. default : return WBEM_E_FAILED;
  437. }
  438. return WBEM_S_NO_ERROR;
  439. }
  440. int IsEmpty(VARIANT &v)
  441. {
  442. if((v.vt == VT_NULL) || (v.vt == VT_EMPTY)) return 1;
  443. if((v.vt == VT_UNKNOWN) || (v.vt == VT_DISPATCH))
  444. { if(NULL == v.punkVal)
  445. return 1;
  446. }
  447. return 0;
  448. }
  449. // Creates the default security descriptor for WMI policy *containers*
  450. // Full Control - Domain & Enterprise Admins & GPO Creator-Owner group
  451. // Read Access - authenticated users
  452. // assumes input pointer is NULL or uninitialized or some such
  453. HRESULT CreateDefaultSecurityDescriptor(CNtSecurityDescriptor& cSD)
  454. {
  455. HRESULT hr = WBEM_S_NO_ERROR;
  456. CNtSid sidWorld(L"Authenticated Users");
  457. CNtSid sidSystem(L"System");
  458. CNtSid sidAdmins(L"Domain Admins");
  459. CNtSid sidEnterpriseAdmins(L"Enterprise Admins");
  460. CNtSid sidAdministrators(L"Administrators");
  461. CNtSid sidGPO(L"Group Policy Creator Owners");
  462. CNtSid sidOwner(L"CREATOR OWNER");
  463. // hmmm - does six aces beat four aces?
  464. // a smith and wesson beats four of a kind...
  465. DWORD full = FULL_CONTROL | DS_GENERIC_ALL;
  466. CNtAce aceEnterpriseAdmins(full, ACCESS_ALLOWED_ACE_TYPE, ADS_ACEFLAG_INHERIT_ACE, sidEnterpriseAdmins);
  467. CNtAce aceOwner(full, ACCESS_ALLOWED_ACE_TYPE, ADS_ACEFLAG_INHERIT_ACE | ADS_ACEFLAG_INHERIT_ONLY_ACE, sidOwner);
  468. CNtAce aceAdminsObject(full, ACCESS_ALLOWED_ACE_TYPE, ADS_ACEFLAG_INHERIT_ACE | ADS_ACEFLAG_INHERIT_ONLY_ACE, sidAdmins);
  469. DWORD write = DS_GENERIC_READ | DS_GENERIC_WRITE | ACTRL_DS_CREATE_CHILD;
  470. CNtAce aceAdmins(write | ACTRL_DS_DELETE_CHILD, ACCESS_ALLOWED_ACE_TYPE, 0, sidAdmins);
  471. CNtAce aceAdministrators(write, ACCESS_ALLOWED_ACE_TYPE, 0, sidAdministrators);
  472. CNtAce aceGPO(write, ACCESS_ALLOWED_ACE_TYPE, 0, sidGPO);
  473. DWORD read = DS_GENERIC_READ;
  474. CNtAce aceWorld(read, ACCESS_ALLOWED_ACE_TYPE, ADS_ACEFLAG_INHERIT_ACE, sidWorld);
  475. CNtAce aceSystem(read, ACCESS_ALLOWED_ACE_TYPE, ADS_ACEFLAG_INHERIT_ACE, sidSystem);
  476. CNtAcl ackl;
  477. if (!ackl.AddAce(&aceWorld)) hr = WBEM_E_FAILED;
  478. if (!ackl.AddAce(&aceSystem)) hr = WBEM_E_FAILED;
  479. if (!ackl.AddAce(&aceAdmins)) hr = WBEM_E_FAILED;
  480. if (!ackl.AddAce(&aceEnterpriseAdmins)) hr = WBEM_E_FAILED;
  481. if (!ackl.AddAce(&aceAdministrators)) hr = WBEM_E_FAILED;
  482. if (!ackl.AddAce(&aceGPO)) hr = WBEM_E_FAILED;
  483. if (!ackl.AddAce(&aceOwner)) hr = WBEM_E_FAILED;
  484. if (!ackl.AddAce(&aceAdminsObject)) hr = WBEM_E_FAILED;
  485. if (!ackl.Resize(CNtAcl::MinimumSize)) hr = WBEM_E_FAILED;
  486. if (!cSD.SetDacl(&ackl)) hr = WBEM_E_FAILED;
  487. if (!cSD.SetOwner(&sidEnterpriseAdmins)) hr = WBEM_E_FAILED;
  488. // prefix: making sure we don't dereference a NULL...
  489. if (cSD.GetPtr() != NULL)
  490. if (!SetSecurityDescriptorControl(cSD.GetPtr(), SE_DACL_PROTECTED, SE_DACL_PROTECTED)) hr = WBEM_E_FAILED;
  491. return hr;
  492. }
  493. // given an input security descriptor
  494. // add owner from thread
  495. HRESULT GetOwnerSecurityDescriptor(CNtSecurityDescriptor& SD)
  496. {
  497. HRESULT hr = WBEM_S_NO_ERROR;
  498. CNtSid sidOwner(CNtSid::CURRENT_THREAD);
  499. if (!SD.SetOwner(&sidOwner)) hr = WBEM_E_FAILED;
  500. /***********************
  501. CNtSid sidOwner(CNtSid::CURRENT_THREAD);
  502. CNtAcl ackl;
  503. if (!SD.GetDacl(ackl)) hr = WBEM_E_FAILED;
  504. if (!SD.SetOwner(&sidOwner)) hr = WBEM_E_FAILED;
  505. if (SUCCEEDED(hr))
  506. {
  507. CNtSid sidGPO(L"Group Policy Creator Owners");
  508. int nAces = ackl.GetNumAces();
  509. CNtAce* pAce = NULL;
  510. CNtSid* pSid = NULL;
  511. // walk through all the aces, find GPO owner creators & kill them
  512. for (int i = 0; i < nAces; i++)
  513. if ((pAce = ackl.GetAce(i)) && (pSid = pAce->GetSid()))
  514. {
  515. if ((*pSid == sidGPO) && (pAce->GetType() == ACCESS_ALLOWED_ACE_TYPE))
  516. {
  517. ackl.DeleteAce(i);
  518. delete pAce;
  519. delete pSid;
  520. break;
  521. }
  522. delete pAce;
  523. delete pSid;
  524. }
  525. else
  526. //
  527. {
  528. hr = WBEM_E_CRITICAL_ERROR;
  529. break;
  530. }
  531. }
  532. if (SUCCEEDED(hr))
  533. {
  534. CNtAce aceOwner(FULL_CONTROL, ACCESS_ALLOWED_ACE_TYPE, 0, sidOwner);
  535. if (ackl.AddAce(&aceOwner) && SD.SetDacl(&ackl))
  536. hr = WBEM_S_NO_ERROR;
  537. else
  538. hr = WBEM_E_FAILED;
  539. }
  540. ******************/
  541. return hr;
  542. }
  543. PSECURITY_DESCRIPTOR GetADSecurityDescriptor(IDirectoryObject *pIDirectoryObject)
  544. {
  545. HRESULT
  546. hres;
  547. LPWSTR
  548. pAttrNames[] = { L"ntSecurityDescriptor" };
  549. ADsStruct<ADS_ATTR_INFO>
  550. pAttrInfo;
  551. PSECURITY_DESCRIPTOR
  552. pSD = NULL;
  553. DWORD
  554. dwReturn,
  555. dwLength;
  556. hres = pIDirectoryObject->GetObjectAttributes(pAttrNames, 1, &pAttrInfo, &dwReturn);
  557. if(SUCCEEDED(hres) && (1 == dwReturn))
  558. {
  559. dwLength = pAttrInfo->pADsValues->SecurityDescriptor.dwLength;
  560. pSD = new BYTE[dwLength];
  561. if(NULL == pSD) return NULL;
  562. ZeroMemory(pSD, dwLength);
  563. memcpy(pSD, pAttrInfo->pADsValues->SecurityDescriptor.lpValue, dwLength);
  564. }
  565. return pSD;
  566. }
  567. #define PATH_LENGTH 1024
  568. HRESULT ExecuteWQLQuery(CPolicySOM *a_pSomObj,
  569. wchar_t *wcsWQLStmt,
  570. IWbemObjectSink *pResponseHandler,
  571. IWbemServices *pWbemServices,
  572. BSTR bstrADClassName,
  573. functTyp pf_ADToCIM)
  574. {
  575. HRESULT
  576. hres = WBEM_E_FAILED;
  577. int
  578. nRes;
  579. QL_LEVEL_1_TOKEN
  580. *pToken = NULL;
  581. CComPtr<IADsContainer>
  582. pADsContainer;
  583. CComQIPtr<IDirectorySearch>
  584. pDirectorySearch;
  585. CComBSTR
  586. bstrLDAPQuery;
  587. wchar_t
  588. objPath[PATH_LENGTH];
  589. ADS_SEARCH_HANDLE
  590. searchHandle;
  591. ADS_SEARCH_COLUMN
  592. searchColumn;
  593. wchar_t
  594. *pszDistName[] = { L"distinguishedName" };
  595. // **** parse WQL expression
  596. CTextLexSource
  597. src(wcsWQLStmt);
  598. QL1_Parser
  599. parser(&src);
  600. QL_LEVEL_1_RPN_EXPRESSION
  601. *pExp = NULL;
  602. AutoDelete<QL_LEVEL_1_RPN_EXPRESSION>
  603. AutoExp(&pExp);
  604. if(nRes = parser.Parse(&pExp))
  605. return WBEM_E_INVALID_QUERY;
  606. // **** find domain attribute
  607. for(int iToken = 0; (iToken < pExp->nNumTokens) && (NULL == pToken); iToken++)
  608. {
  609. pToken = &pExp->pArrayOfTokens[iToken];
  610. if(_wcsicmp(g_bstrDomain, pToken->PropertyName.GetStringAt(pToken->PropertyName.GetNumElements() - 1)))
  611. pToken = NULL;
  612. }
  613. if(NULL == pToken)
  614. return WBEMESS_E_REGISTRATION_TOO_BROAD;
  615. if((QL_LEVEL_1_TOKEN::OP_EXPRESSION != pToken->nTokenType) ||
  616. (QL_LEVEL_1_TOKEN::OP_EQUAL != pToken->nOperator) ||
  617. (TRUE == pToken->m_bPropComp) ||
  618. (VT_BSTR != pToken->vConstValue.vt))
  619. return WBEM_E_INVALID_QUERY;
  620. if((NULL == bstrADClassName) || (NULL == pf_ADToCIM))
  621. return WBEM_E_INVALID_QUERY;
  622. // **** connect to LDAP location
  623. CComBSTR
  624. bstrDomain = pToken->vConstValue.bstrVal;
  625. pADsContainer.Attach(a_pSomObj->GetADServices(bstrDomain, hres));
  626. pDirectorySearch = pADsContainer;
  627. if(FAILED(hres))
  628. {
  629. if(WBEM_E_NOT_FOUND == hres)
  630. return WBEM_S_NO_ERROR;
  631. return hres;
  632. }
  633. else if(pDirectorySearch == NULL)
  634. return WBEM_E_FAILED;
  635. VARIANT v1; v1.bstrVal = (BSTR)bstrDomain; v1.vt = VT_BSTR;
  636. // **** build LDAP query to execute on container pADs
  637. bstrLDAPQuery.Append(L"(objectCategory=");
  638. bstrLDAPQuery.Append(bstrADClassName);
  639. bstrLDAPQuery.Append(L")");
  640. // **** set search preferences
  641. ADS_SEARCHPREF_INFO
  642. SearchPreferences[1];
  643. SearchPreferences[0].dwSearchPref = ADS_SEARCHPREF_PAGESIZE;
  644. SearchPreferences[0].vValue.dwType = ADSTYPE_INTEGER;
  645. SearchPreferences[0].vValue.Integer = 1000;
  646. hres = pDirectorySearch->SetSearchPreference(SearchPreferences, 1);
  647. if(FAILED(hres))
  648. {
  649. ERRORTRACE((LOG_ESS, "POLICMAN: Could not set search preferences, returned error: 0x%08X\n", (BSTR)bstrLDAPQuery, hres));
  650. return ADSIToWMIErrorCodes(hres);
  651. }
  652. // **** execute query
  653. hres = pDirectorySearch->ExecuteSearch(bstrLDAPQuery, pszDistName, 1, &searchHandle);
  654. if(FAILED(hres))
  655. {
  656. ERRORTRACE((LOG_ESS, "POLICMAN: Could execute query: (%s) returned error: 0x%08X\n", (BSTR)bstrLDAPQuery, hres));
  657. return ADSIToWMIErrorCodes(hres);
  658. }
  659. // **** build result list
  660. try
  661. {
  662. while(SUCCEEDED(hres = pDirectorySearch->GetNextRow(searchHandle)) && (S_ADS_NOMORE_ROWS != hres))
  663. {
  664. CComPtr<IDirectoryObject>
  665. pDirectoryObject;
  666. CComPtr<IWbemClassObject>
  667. pWbemClassObject;
  668. // **** get path to object
  669. hres = pDirectorySearch->GetColumn(searchHandle, pszDistName[0], &searchColumn);
  670. if(FAILED(hres)) return ADSIToWMIErrorCodes(hres);
  671. // **** get pointer to object
  672. StringCchCopy(objPath, PATH_LENGTH, L"LDAP://");
  673. StringCchCat(objPath, PATH_LENGTH, bstrDomain);
  674. StringCchCat(objPath, PATH_LENGTH, L"/");
  675. StringCchCat(objPath, PATH_LENGTH, searchColumn.pADsValues->CaseIgnoreString);
  676. pDirectorySearch->FreeColumn(&searchColumn);
  677. hres = ADsOpenObject(objPath,
  678. NULL, NULL,
  679. ADS_SECURE_AUTHENTICATION | ADS_USE_SEALING | ADS_USE_SIGNING,
  680. IID_IDirectoryObject, (void **)&pDirectoryObject);
  681. if(FAILED(hres)) return ADSIToWMIErrorCodes(hres);
  682. hres = pf_ADToCIM(&pWbemClassObject, pDirectoryObject, pWbemServices);
  683. if(FAILED(hres)) return ADSIToWMIErrorCodes(hres);
  684. if(pWbemClassObject == NULL) return WBEM_E_FAILED;
  685. hres = pWbemClassObject->Put(g_bstrDomain, 0, &v1, 0);
  686. hres = pResponseHandler->Indicate(1, &pWbemClassObject);
  687. }
  688. }
  689. catch(long hret)
  690. {
  691. hres = ADSIToWMIErrorCodes(hret);
  692. ERRORTRACE((LOG_ESS, "POLICMAN: Translation of Policy object from AD to WMI generated HRESULT 0x%08X\n", hres));
  693. }
  694. catch(wchar_t *swErrString)
  695. {
  696. ERRORTRACE((LOG_ESS, "POLICMAN: Caught Exception: %S\n", swErrString));
  697. hres = WBEM_E_FAILED;
  698. }
  699. catch(...)
  700. {
  701. ERRORTRACE((LOG_ESS, "POLICMAN: Caught unknown Exception\n"));
  702. hres = WBEM_E_TRANSPORT_FAILURE; // HACK for RC1
  703. }
  704. pDirectorySearch->CloseSearchHandle(searchHandle);
  705. return hres;
  706. }
  707. void LogExtendedADErrorInfo(HRESULT hres)
  708. {
  709. DWORD dwLastError;
  710. WCHAR szErrorBuf[1024], szNameBuf[256];
  711. if(HRESULT_FACILITY(hres) == FACILITY_WIN32)
  712. {
  713. HRESULT hres2;
  714. hres2 = ADsGetLastError(&dwLastError, szErrorBuf, 1023, szNameBuf, 255);
  715. if(SUCCEEDED(hres2))
  716. ERRORTRACE((LOG_ESS, "POLICMAN: Error Code: %d Error Text: %S Provider: %S\n", dwLastError, szErrorBuf, szNameBuf));
  717. else
  718. ERRORTRACE((LOG_ESS, "POLICMAN: Type mismatch on date property\n"));
  719. }
  720. }