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.

596 lines
14 KiB

  1. #ifndef __WBEM_UTILITY__H_
  2. #define __WBEM_UTILITY__H_
  3. #include "iads.h"
  4. #undef _ASSERT
  5. #include <wbemcli.h>
  6. #include <wbemprov.h>
  7. #include <wbemcomn.h>
  8. #include <atlbase.h>
  9. #include <activeds.h>
  10. #include <WinNTSec.h>
  11. // **** default min, max and default
  12. #define SINT_MIN 0xffffffff
  13. #define SINT_MAX 0xefffffff
  14. #define SINT_DEFAULT 0x0
  15. #define UINT_MIN 0x0
  16. #define UINT_MAX 0xffffffff
  17. #define UINT_DEFAULT 0x0
  18. #define REAL_MIN (double)(0xffffffffffffffff)
  19. #define REAL_MAX (double)(0xefffffffffffffff)
  20. #define REAL_DEFAULT 0x0
  21. // retrieve namespace with given name
  22. HRESULT GetNamespace(BSTR namespaceName, IWbemServices*& pNamespace, bool bInProc);
  23. // make sure that the ID property of pObj has a value
  24. // will generate GUID if not
  25. // assumes property is a BSTR!
  26. // if pName == NULL, assumes property name is "ID"
  27. // returns WBEM_S_NO_ERROR if ID generated
  28. // WBEM_S_FALSE if no ID generated (already has a value)
  29. // WBEM_E_NOT_FOUND if ID property is not
  30. // some error if error of some sort
  31. HRESULT EnsureID(IWbemClassObject* pObj, WCHAR* pName);
  32. // init ADS_ATTR_INFO structure
  33. void Init_AdsAttrInfo(ADS_ATTR_INFO *pAdsAttrInfo,
  34. LPWSTR bstrName,
  35. DWORD control,
  36. ADSTYPE type,
  37. PADSVALUE pVals,
  38. DWORD nVals);
  39. template <class ElementType, VARTYPE vt> class SafeArray
  40. {
  41. private:
  42. long
  43. m_IndexMin,
  44. m_IndexMax,
  45. m_Size;
  46. ElementType
  47. *m_pElementType;
  48. SAFEARRAY
  49. *m_pSafeArray;
  50. void _EmptyInit(void)
  51. {
  52. m_IndexMin = -1;
  53. m_IndexMax = -1;
  54. m_Size = 0;
  55. m_pElementType = NULL;
  56. m_pSafeArray = NULL;
  57. }
  58. void _ArrayInit(void)
  59. {
  60. HRESULT hres;
  61. hres = SafeArrayGetUBound(m_pSafeArray, 1, &m_IndexMax);
  62. if(FAILED(hres))
  63. return;
  64. hres = SafeArrayGetLBound(m_pSafeArray, 1, &m_IndexMin);
  65. if(FAILED(hres))
  66. return;
  67. m_Size = m_IndexMax - m_IndexMin + 1;
  68. m_pElementType = NULL;
  69. hres = SafeArrayAccessData(m_pSafeArray, (void **)&m_pElementType);
  70. if(FAILED(hres))
  71. return;
  72. }
  73. public:
  74. SafeArray(void)
  75. {
  76. _EmptyInit();
  77. }
  78. // **** empty array creation
  79. SafeArray(long LowerBound, long ArgSize)
  80. {
  81. HRESULT hres;
  82. SAFEARRAYBOUND
  83. arrayBounds[1];
  84. // **** check for valid size
  85. if(LowerBound < 0)
  86. return;
  87. if(ArgSize < 0)
  88. return;
  89. // **** create empty array
  90. if(0 == ArgSize)
  91. {
  92. _EmptyInit();
  93. }
  94. else
  95. {
  96. arrayBounds[0].lLbound = LowerBound;
  97. arrayBounds[0].cElements = ArgSize;
  98. m_pSafeArray = SafeArrayCreate(vt, 1, arrayBounds);
  99. if(NULL == m_pSafeArray)
  100. return;
  101. _ArrayInit();
  102. }
  103. }
  104. // **** creation/init SafeArray from VARIANT
  105. SafeArray(VARIANT *v)
  106. {
  107. HRESULT hres;
  108. // **** first ensure that the contained object is valid
  109. if(NULL == v)
  110. return;
  111. if((V_VT(v) == VT_NULL) || (V_VT(v) == VT_EMPTY))
  112. {
  113. _EmptyInit();
  114. }
  115. else
  116. {
  117. if(V_VT(v) != (VT_ARRAY | vt))
  118. return;
  119. // **** copy to SafeArray contents of variant
  120. m_pSafeArray = NULL;
  121. hres = SafeArrayCopy(V_ARRAY(v), &m_pSafeArray);
  122. if(FAILED(hres))
  123. return;
  124. _ArrayInit();
  125. }
  126. }
  127. SafeArray &operator =(const VARIANT *Source)
  128. {
  129. HRESULT hres;
  130. // **** first ensure that the contained object is valid
  131. if((NULL == Source) || (V_VT(Source) != (VT_ARRAY | vt)))
  132. return *this;
  133. // **** get SAFEARRAY dimensions
  134. if(NULL != m_pSafeArray)
  135. {
  136. hres = SafeArrayUnaccessData(m_pSafeArray);
  137. hres = SafeArrayDestroy(m_pSafeArray);
  138. }
  139. hres = SafeArrayCopy(V_ARRAY(Source), &m_pSafeArray);
  140. if(FAILED(hres))
  141. {
  142. _EmptyInit();
  143. return *this;
  144. }
  145. _ArrayInit();
  146. return *this;
  147. }
  148. // **** creation/init SafeArray from SafeArray
  149. SafeArray(const SafeArray &Source)
  150. {
  151. HRESULT hres;
  152. if(NULL == Source.m_pSafeArray)
  153. {
  154. _EmptyInit();
  155. }
  156. else
  157. {
  158. // **** copy to SafeArray contents of variant
  159. m_pSafeArray = NULL;
  160. hres = SafeArrayCopy(Source.m_pSafeArray, &m_pSafeArray);
  161. if(FAILED(hres))
  162. return;
  163. _ArrayInit();
  164. }
  165. }
  166. SafeArray &operator = (const SafeArray &Source)
  167. {
  168. HRESULT hres;
  169. if(NULL == Source.m_pSafeArray)
  170. {
  171. _EmptyInit();
  172. }
  173. else
  174. {
  175. if(NULL != m_pSafeArray)
  176. {
  177. hres = SafeArrayUnaccessData(m_pSafeArray);
  178. }
  179. // **** copy to SafeArray contents of variant
  180. hres = SafeArrayCopy(Source.m_pSafeArray, &m_pSafeArray);
  181. if(FAILED(hres))
  182. return;
  183. _ArrayInit();
  184. }
  185. return *this;
  186. }
  187. ~SafeArray(void)
  188. {
  189. if(NULL != m_pSafeArray)
  190. {
  191. SafeArrayUnaccessData(m_pSafeArray);
  192. SafeArrayDestroy(m_pSafeArray);
  193. }
  194. _EmptyInit();
  195. }
  196. // **** Misc functions
  197. VARTYPE Type(void) { return vt; }
  198. long Size(void) { return m_Size; }
  199. long IndexMin(void) { return m_IndexMin; }
  200. long IndexMax(void) { return m_IndexMax; }
  201. SAFEARRAY *Data(void)
  202. {
  203. HRESULT hres;
  204. SAFEARRAY *pArrayCopy = NULL;
  205. if(NULL != m_pSafeArray)
  206. {
  207. hres = SafeArrayCopy(m_pSafeArray, &pArrayCopy);
  208. if(FAILED(hres))
  209. return NULL;
  210. }
  211. return pArrayCopy;
  212. }
  213. long ReDim(long LowerBound, long ArgSize)
  214. {
  215. HRESULT hres;
  216. SAFEARRAYBOUND arrayBounds[1];
  217. // **** check to see that Index is within range
  218. if((LowerBound != m_IndexMin) || ((LowerBound + ArgSize - 1) != m_IndexMax))
  219. {
  220. m_IndexMin = LowerBound;
  221. if(ArgSize < 1)
  222. {
  223. if(NULL != m_pSafeArray)
  224. {
  225. SafeArrayUnaccessData(m_pSafeArray);
  226. SafeArrayDestroy(m_pSafeArray);
  227. }
  228. _EmptyInit();
  229. }
  230. else
  231. {
  232. m_IndexMax = LowerBound + ArgSize - 1;
  233. m_Size = m_IndexMax - m_IndexMin + 1;
  234. arrayBounds[0].lLbound = LowerBound;
  235. arrayBounds[0].cElements = m_Size;
  236. if(NULL == m_pSafeArray)
  237. {
  238. m_pSafeArray = SafeArrayCreate(vt, 1, arrayBounds);
  239. }
  240. else
  241. {
  242. hres = SafeArrayUnaccessData(m_pSafeArray);
  243. hres = SafeArrayRedim(m_pSafeArray, arrayBounds);
  244. }
  245. m_pElementType = NULL;
  246. hres = SafeArrayAccessData(m_pSafeArray, (void **)&m_pElementType);
  247. }
  248. }
  249. // **** return reference to Index element
  250. return m_Size;
  251. }
  252. ElementType& operator[](long Index)
  253. {
  254. // **** return reference to Index element
  255. return m_pElementType[Index];
  256. }
  257. };
  258. template<class T> struct ADsStruct
  259. {
  260. T *m_pADsMem;
  261. ADsStruct(void)
  262. { m_pADsMem = NULL; }
  263. ~ADsStruct(void)
  264. { if(NULL != m_pADsMem) FreeADsMem(m_pADsMem); }
  265. operator T*(void) const { return m_pADsMem; }
  266. T** operator&(void) { return &m_pADsMem; }
  267. bool operator==(T *x) { return m_pADsMem == x; }
  268. bool operator!=(T *x) { return m_pADsMem != x; }
  269. T* & operator->(void) { return m_pADsMem; }
  270. // T* operator+(unsigned long x) { return m_pADsMem + x; }
  271. ADsStruct<T>& operator=(ADsStruct<T> &x)
  272. { return; }
  273. ADsStruct<T>& operator=(T* x)
  274. { if(NULL != m_pADsMem) FreeADsMem(m_pADsMem); m_pADsMem = x; return *this; }
  275. };
  276. template<class T> struct AutoDelete
  277. {
  278. T **m_pTheThingToDelete;
  279. AutoDelete(void)
  280. { m_pTheThingToDelete = NULL; }
  281. AutoDelete(T **x)
  282. { m_pTheThingToDelete = x; }
  283. ~AutoDelete(void)
  284. { if((NULL != m_pTheThingToDelete) && (NULL != *m_pTheThingToDelete)) delete *m_pTheThingToDelete;}
  285. };
  286. class ADsObjAutoDelete
  287. {
  288. CComPtr<IADsDeleteOps>
  289. m_pDelObj;
  290. public:
  291. HRESULT Attach(IDispatch *pDisp)
  292. { return pDisp->QueryInterface(IID_IADsDeleteOps, (void**)&m_pDelObj); }
  293. void Detach(void)
  294. { if(m_pDelObj != NULL) m_pDelObj = NULL; }
  295. ~ADsObjAutoDelete(void)
  296. { if(m_pDelObj != NULL) m_pDelObj->DeleteObject(0); }
  297. };
  298. extern BSTR
  299. // **** misc names
  300. g_bstrEmptyString,
  301. g_bstrEmptyDate,
  302. // **** AD schema names
  303. g_bstrADAuthor,
  304. g_bstrADChangeDate,
  305. g_bstrADClassDefinition,
  306. g_bstrADCreationDate,
  307. g_bstrADDescription,
  308. g_bstrADIntDefault,
  309. g_bstrADInt8Default,
  310. g_bstrADID,
  311. g_bstrADIntMax,
  312. g_bstrADInt8Max,
  313. g_bstrADIntMin,
  314. g_bstrADInt8Min,
  315. g_bstrADIntValidValues,
  316. g_bstrADName,
  317. g_bstrADNormalizedClass,
  318. g_bstrADObjectClass,
  319. g_bstrADParam2,
  320. g_bstrADPolicyType,
  321. g_bstrADPropertyName,
  322. g_bstrADQuery,
  323. g_bstrADQueryLanguage,
  324. g_bstrADStringDefault,
  325. g_bstrADStringValidValues,
  326. g_bstrADSourceOrganization,
  327. g_bstrADTargetClass,
  328. g_bstrADTargetNameSpace,
  329. g_bstrADTargetObject,
  330. g_bstrADTargetPath,
  331. g_bstrADTargetType,
  332. // **** AD class names
  333. g_bstrADClassMergeablePolicy,
  334. g_bstrADClassRangeParam,
  335. g_bstrADClassRangeSint32,
  336. g_bstrADClassRangeUint32,
  337. g_bstrADClassRangeReal,
  338. g_bstrADClassParamUnknown,
  339. g_bstrADClassSetSint32,
  340. g_bstrADClassSetUint32,
  341. g_bstrADClassSetString,
  342. g_bstrADClassSimplePolicy,
  343. g_bstrADClassRule,
  344. g_bstrADClassSom,
  345. g_bstrADClassPolicyType,
  346. g_bstrADClassWMIGPO,
  347. // **** CIM schema names
  348. g_bstrAuthor,
  349. g_bstrChangeDate,
  350. g_bstrClassDefinition,
  351. g_bstrCreationDate,
  352. g_bstrDefault,
  353. g_bstrDescription;
  354. extern BSTR
  355. g_bstrDsPath,
  356. g_bstrDomain,
  357. g_bstrID,
  358. g_bstrMax,
  359. g_bstrMin,
  360. g_bstrName,
  361. g_bstrPolicyType,
  362. g_bstrPropertyName,
  363. g_bstrQuery,
  364. g_bstrQueryLanguage,
  365. g_bstrRangeSettings,
  366. g_bstrRules,
  367. g_bstrSourceOrganization,
  368. g_bstrTargetClass,
  369. g_bstrTargetNameSpace,
  370. g_bstrTargetObject,
  371. g_bstrTargetPath,
  372. g_bstrTargetType,
  373. g_bstrValidValues,
  374. // **** CIM class names
  375. g_bstrClassMergeablePolicy,
  376. g_bstrClassRangeParam,
  377. g_bstrClassRangeSint32,
  378. g_bstrClassRangeUint32,
  379. g_bstrClassRangeReal,
  380. g_bstrClassSetSint32,
  381. g_bstrClassSetUint32,
  382. g_bstrClassSetString,
  383. g_bstrClassSimplePolicy,
  384. g_bstrClassRule,
  385. g_bstrClassSom,
  386. g_bstrClassSomFilterStatus,
  387. g_bstrClassPolicyType,
  388. g_bstrClassWMIGPO,
  389. // **** CIM standard names
  390. g_bstrMISCclassSchema,
  391. g_bstrMISCContainer,
  392. g_bstrMISCschemaNamingContext,
  393. g_bstrMISCdefaultNamingContext,
  394. g_bstrMISCWQL,
  395. g_bstrMISCQuery;
  396. void InitGlobalNames(void);
  397. void FreeGlobalNames(void);
  398. HRESULT DomainNameFromDistName(CComBSTR &DomainName, CComBSTR &DistName);
  399. HRESULT DistNameFromDomainName(CComBSTR &DomainName, CComBSTR &DistName);
  400. HRESULT ADSIToWMIErrorCodes(HRESULT hresAD);
  401. IsEmpty(VARIANT &);
  402. typedef HRESULT (*functTyp)(IWbemClassObject * *,
  403. IDirectoryObject*,
  404. IWbemServices*);
  405. class CPolicySOM;
  406. HRESULT ExecuteWQLQuery(CPolicySOM *,
  407. wchar_t *wcsWQLStmt,
  408. IWbemObjectSink *pResponseHandler,
  409. IWbemServices *pWbemServices,
  410. BSTR bstrADClassName,
  411. functTyp pf_ADToCIM);
  412. // **** PolicyTemplate
  413. HRESULT Policy_CIMToAD(long, IWbemClassObject *, IDirectoryObject *pDestContainer);
  414. HRESULT Policy_ADToCIM(IWbemClassObject **, IDirectoryObject *, IWbemServices *);
  415. HRESULT Policy_Merge(SafeArray<IUnknown*, VT_UNKNOWN> &PolicyArray,
  416. CComPtr<IWbemClassObject> &pMergedPolicy,
  417. IWbemServices *pDestCIM);
  418. // **** RangeSint32
  419. HRESULT Range_Sint32_Verify(IWbemClassObject*);
  420. HRESULT Range_Sint32_ADToCIM(IWbemClassObject * *, IDirectorySearch*, ADS_SEARCH_HANDLE, IWbemServices*);
  421. HRESULT Range_Sint32_Merge(SafeArray<IUnknown *, VT_UNKNOWN> &, CComPtr<IWbemClassObject> &, int &);
  422. // **** RangeUint32
  423. HRESULT Range_Uint32_Verify(IWbemClassObject*);
  424. HRESULT Range_Uint32_ADToCIM(IWbemClassObject * *, IDirectorySearch*, ADS_SEARCH_HANDLE, IWbemServices*);
  425. HRESULT Range_Uint32_Merge(SafeArray<IUnknown *, VT_UNKNOWN> &, CComPtr<IWbemClassObject> &, int &);
  426. // **** RangeReal
  427. HRESULT Range_Real_Verify(IWbemClassObject*);
  428. HRESULT Range_Real_ADToCIM(IWbemClassObject * *, IDirectorySearch*, ADS_SEARCH_HANDLE, IWbemServices*);
  429. HRESULT Range_Real_Merge(SafeArray<IUnknown *, VT_UNKNOWN> &, CComPtr<IWbemClassObject> &, int &);
  430. // **** SetSint32
  431. HRESULT Set_Sint32_Verify(IWbemClassObject*);
  432. HRESULT Set_Sint32_ADToCIM(IWbemClassObject * *, IDirectorySearch*, ADS_SEARCH_HANDLE, IWbemServices*);
  433. HRESULT Set_Sint32_Merge(SafeArray<IUnknown *, VT_UNKNOWN> &, CComPtr<IWbemClassObject> &, int &);
  434. // **** SetUint32
  435. HRESULT Set_Uint32_Verify(IWbemClassObject*);
  436. HRESULT Set_Uint32_ADToCIM(IWbemClassObject * *, IDirectorySearch*, ADS_SEARCH_HANDLE, IWbemServices*);
  437. HRESULT Set_Uint32_Merge(SafeArray<IUnknown *, VT_UNKNOWN> &, CComPtr<IWbemClassObject> &, int &);
  438. // **** SetString
  439. HRESULT Set_String_Verify(IWbemClassObject*);
  440. HRESULT Set_String_ADToCIM(IWbemClassObject * *, IDirectorySearch*, ADS_SEARCH_HANDLE, IWbemServices*);
  441. HRESULT Set_String_Merge(SafeArray<IUnknown *, VT_UNKNOWN> &, CComPtr<IWbemClassObject> &, int &);
  442. // **** ParamUnknown
  443. HRESULT Param_Unknown_Verify(IWbemClassObject*);
  444. HRESULT Param_Unknown_ADToCIM(IWbemClassObject * *, IDirectorySearch*, ADS_SEARCH_HANDLE*, IWbemServices*);
  445. // **** SOM
  446. HRESULT Som_CIMToAD(IWbemClassObject *pSrcPolicyObj, IDirectoryObject *pDestContainer, long lFlags);
  447. HRESULT Som_ADToCIM(IWbemClassObject **, IDirectoryObject *pSrcPolicyObj, IWbemServices *pDestCIM);
  448. // **** WMIGPO
  449. HRESULT WMIGPO_CIMToAD(IWbemClassObject *pSrcPolicyObj, IDirectoryObject *pDestContainer, long lFlags);
  450. HRESULT WMIGPO_ADToCIM(IWbemClassObject **, IDirectoryObject *pSrcPolicyObj, IWbemServices *pDestCIM);
  451. // **** PolicyType
  452. HRESULT PolicyType_CIMToAD(IWbemClassObject *pSrcPolicyObj, IDirectoryObject *pDestContainer);
  453. HRESULT PolicyType_ADToCIM(IWbemClassObject**, IDirectoryObject *pSrcPolicyObj, IWbemServices *pDestCIM);
  454. // **** Security Manipulation
  455. HRESULT CreateDefaultSecurityDescriptor(CNtSecurityDescriptor& cSD);
  456. HRESULT GetOwnerSecurityDescriptor(CNtSecurityDescriptor& SD);
  457. PSECURITY_DESCRIPTOR GetADSecurityDescriptor(IDirectoryObject *pIDirectoryObject);
  458. #endif __WBEM_UTILITY__H_