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.

244 lines
6.8 KiB

  1. /////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright(C) 1997-1998 Microsoft Corporation all rights reserved.
  4. //
  5. // Module: sdopolicy.cpp
  6. //
  7. // Project: Everest
  8. //
  9. // Description: IAS Server Data Object - Policy Object Implementation
  10. //
  11. // Author: TLP 1/23/98
  12. //
  13. /////////////////////////////////////////////////////////////////////////////
  14. #include "stdafx.h"
  15. #include "sdopolicy.h"
  16. #include "sdohelperfuncs.h"
  17. #include <varvec.h>
  18. #include <time.h>
  19. //////////////////////////////////////////////////////////////////////////////
  20. HRESULT CSdoPolicy::FinalInitialize(
  21. /*[in]*/ bool fInitNew,
  22. /*[in]*/ ISdoMachine* pAttachedMachine
  23. )
  24. {
  25. HRESULT hr = InitializeCollection(
  26. PROPERTY_POLICY_CONDITIONS_COLLECTION,
  27. SDO_PROG_ID_CONDITION,
  28. pAttachedMachine,
  29. NULL
  30. );
  31. if ( SUCCEEDED(hr) )
  32. {
  33. if ( ! fInitNew )
  34. hr = Load();
  35. }
  36. return hr;
  37. }
  38. //////////////////////////////////////////////////////////////////////////////
  39. HRESULT CSdoPolicy::Load()
  40. {
  41. HRESULT hr = LoadProperties();
  42. if ( SUCCEEDED(hr) )
  43. hr = ConditionsFromConstraints();
  44. return hr;
  45. }
  46. //////////////////////////////////////////////////////////////////////////////
  47. HRESULT CSdoPolicy::Save()
  48. {
  49. HRESULT hr = ConstraintsFromConditions();
  50. if ( SUCCEEDED(hr) )
  51. hr = SaveProperties();
  52. return hr;
  53. }
  54. static DWORD dwConditionName;
  55. //////////////////////////////////////////////////////////////////////////////
  56. HRESULT CSdoPolicy::ConditionsFromConstraints() // Invoked by LoadSdo()...
  57. {
  58. // Preliminaries...
  59. //
  60. _variant_t vtConstraints;
  61. HRESULT hr = GetPropertyInternal(PROPERTY_POLICY_CONSTRAINT, &vtConstraints);
  62. if ( FAILED(hr) )
  63. {
  64. IASTracePrintf("Error in Policy SDO - ConditionsFromConstraints() - Could not get policy constraints...");
  65. return E_FAIL;
  66. }
  67. _variant_t vtConditions;
  68. hr = GetPropertyInternal(PROPERTY_POLICY_CONDITIONS_COLLECTION, &vtConditions);
  69. if ( FAILED(hr) )
  70. {
  71. IASTracePrintf("Error in Policy SDO - ConditionsFromConstraints() - Could not get conditions collection...");
  72. return E_FAIL;
  73. }
  74. CComPtr<ISdoCollection> pConditions;
  75. hr = vtConditions.pdispVal->QueryInterface(IID_ISdoCollection, (void**)&pConditions);
  76. if ( FAILED(hr) )
  77. {
  78. IASTracePrintf("Error in Policy SDO - ConditionsFromConstraints() - QueryInterface(ISdoCollection) failed...");
  79. return E_FAIL;
  80. }
  81. // Clear the conditions collection
  82. //
  83. pConditions->RemoveAll();
  84. // hr = S_OK at this point... Short circuit if there are no constraints
  85. //
  86. if ( VT_EMPTY != V_VT(&vtConstraints) )
  87. {
  88. CVariantVector<VARIANT> Constraints(&vtConstraints) ;
  89. ULONG ulCount = 0;
  90. // Build a condition object out of each constraint.
  91. //
  92. CComPtr<IDispatch> pDispatch;
  93. CComPtr<ISdo> pSdo;
  94. _variant_t vtConditionText;
  95. _bstr_t bstrConditionName;
  96. WCHAR szConditionName[MAX_PATH + 1];
  97. dwConditionName = 0;
  98. while ( ulCount < Constraints.size() )
  99. {
  100. // For now we'll generate a name... The conditions collection should go away and
  101. // we should use the policy object constraint property directly.
  102. //
  103. wsprintf(
  104. szConditionName,
  105. _T("Condition%ld"),
  106. dwConditionName++
  107. );
  108. bstrConditionName = szConditionName;
  109. hr = pConditions->Add(bstrConditionName, &pDispatch);
  110. if ( FAILED(hr) )
  111. {
  112. IASTracePrintf("Error in Policy SDO - ConditionsFromConstraints() - Could not update conditions collection...");
  113. break;
  114. }
  115. hr = pDispatch->QueryInterface(IID_ISdo, (void**)&pSdo);
  116. if ( FAILED(hr) )
  117. {
  118. IASTracePrintf("Error in Policy SDO - ConditionsFromConstraints() - QueryInterface() failed...");
  119. break;
  120. }
  121. vtConditionText = Constraints[ulCount];
  122. hr = pSdo->PutProperty(PROPERTY_CONDITION_TEXT, &vtConditionText);
  123. if ( FAILED(hr) )
  124. {
  125. IASTracePrintf("Error in Policy SDO - ConditionsFromConstraints() - PutProperty() failed...");
  126. break;
  127. }
  128. vtConditionText.Clear();
  129. pDispatch.Release();
  130. pSdo.Release();
  131. ulCount++;
  132. }
  133. }
  134. return hr;
  135. }
  136. //////////////////////////////////////////////////////////////////////////////
  137. HRESULT CSdoPolicy::ConstraintsFromConditions()
  138. {
  139. // Preliminaries...
  140. //
  141. PropertyMapIterator p = m_PropertyMap.find(PROPERTY_POLICY_CONSTRAINT);
  142. _ASSERT ( p != m_PropertyMap.end() );
  143. _variant_t vtConditions;
  144. HRESULT hr = GetPropertyInternal(PROPERTY_POLICY_CONDITIONS_COLLECTION, &vtConditions);
  145. if ( FAILED(hr) )
  146. {
  147. IASTracePrintf("Error in Policy SDO - ConstraintsFromConditions() - Could not get conditions collection...");
  148. return hr;
  149. }
  150. CComPtr<ISdoCollection> pConditions;
  151. hr = vtConditions.pdispVal->QueryInterface(IID_ISdoCollection, (void**)&pConditions);
  152. if ( FAILED(hr) )
  153. {
  154. IASTracePrintf("Error in Policy SDO - ConditionsFromConstraints() - QueryInterface(ISdoCollection) failed...");
  155. return hr;
  156. }
  157. LONG lCount;
  158. hr = pConditions->get_Count(&lCount);
  159. _ASSERT( SUCCEEDED(hr) );
  160. if ( FAILED(hr) )
  161. return hr;
  162. // Short circuit if no constraints are present
  163. //
  164. _variant_t vtConstraints;
  165. if ( 0 == lCount )
  166. {
  167. // Put VT_EMPTY into data store to clear out the constraints
  168. //
  169. hr = ((*p).second)->PutValue(&vtConstraints);
  170. if ( FAILED(hr) )
  171. IASTracePrintf("Error in Policy SDO - ConstraintsFromConditions() - PutValue(VT_EMPTY) failed...");
  172. }
  173. else
  174. {
  175. // Create the varvec of Constraints and reset the count
  176. //
  177. CVariantVector<VARIANT> Constraints(&vtConstraints, lCount);
  178. lCount = 0;
  179. // Build the constraints property from the Policy SDO's conditions collection
  180. //
  181. CComPtr<IEnumVARIANT> pEnumConditions;
  182. hr = SDOGetCollectionEnumerator(
  183. static_cast<ISdo*>(this),
  184. PROPERTY_POLICY_CONDITIONS_COLLECTION,
  185. &pEnumConditions
  186. );
  187. CComPtr<ISdo> pSdoCondition;
  188. hr = ::SDONextObjectFromCollection(pEnumConditions, &pSdoCondition);
  189. while ( S_OK == hr )
  190. {
  191. hr = pSdoCondition->GetProperty(PROPERTY_CONDITION_TEXT, &Constraints[lCount]);
  192. if ( FAILED(hr) )
  193. {
  194. IASTracePrintf("Error in Policy SDO - ConstraintsFromConditions() - GetProperty() failed...");
  195. break;
  196. }
  197. lCount++;
  198. pSdoCondition.Release();
  199. hr = ::SDONextObjectFromCollection(pEnumConditions, &pSdoCondition);
  200. }
  201. if ( S_FALSE == hr )
  202. hr = S_OK;
  203. // Update the msNPConstraints property if everything went OK
  204. //
  205. if ( SUCCEEDED(hr) )
  206. {
  207. hr = ((*p).second)->PutValue(&vtConstraints);
  208. if ( FAILED(hr) )
  209. IASTracePrintf("Error in Policy SDO - ConstraintsFromConditions() - PutValue() failed...");
  210. }
  211. }
  212. return hr;
  213. }