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.

435 lines
11 KiB

  1. /*++
  2. Copyright (c) 2001 Microsoft Corporation
  3. Module Name:
  4. accessck.cxx
  5. Abstract:
  6. CAzBizRuleContext class implementation
  7. Author:
  8. ObjectFame sample code taken from http://support.microsoft.com/support/kb/articles/Q183/6/98.ASP
  9. Cliff Van Dyke (cliffv) 19-July-2001
  10. -- */
  11. #include "pch.hxx"
  12. /////////////////////////
  13. //CAzBizRuleContext
  14. /////////////////////////
  15. //Constructor
  16. CAzBizRuleContext::CAzBizRuleContext()
  17. {
  18. AzPrint((AZD_SCRIPT_MORE, "CAzBizRuleContext\n"));
  19. m_typeInfo = NULL;
  20. m_AcContext = NULL;
  21. // m_theConnection = NULL;
  22. //
  23. // Set the default return values for this access check
  24. //
  25. m_BizRuleResult = NULL;
  26. m_ScriptError = NULL;
  27. m_bCaseSensitive = TRUE;
  28. }
  29. //Destructor
  30. CAzBizRuleContext::~CAzBizRuleContext()
  31. {
  32. if (m_typeInfo != NULL) {
  33. m_typeInfo->Release();
  34. m_typeInfo = NULL;
  35. }
  36. ASSERT( m_AcContext == NULL );
  37. ASSERT( m_BizRuleResult == NULL );
  38. ASSERT( m_ScriptError == NULL );
  39. AzPrint((AZD_SCRIPT_MORE, "~CAzBizRuleContext\n"));
  40. }
  41. //
  42. // Methods to set/get the boolean result of the business rule
  43. //
  44. HRESULT
  45. CAzBizRuleContext::put_BusinessRuleResult(
  46. IN BOOL bResult
  47. )
  48. /*++
  49. Routine Description:
  50. The script calls the SetBusinessRuleResult method to indicate whether the business rule
  51. has decided to grant permission to the user to perform the requested task.
  52. If the script never calls this method, permission is not granted.
  53. On entry, AcContext->ClientContext.CritSect must be locked.
  54. Arguments:
  55. BizRuleResult -- Specifies whether permission is granted. If TRUE, permission is granted.
  56. If FALSE, permission is not granted.
  57. Return Value:
  58. None
  59. --*/
  60. {
  61. AzPrint((AZD_SCRIPT, "CAzBizRuleContext::put_BusinessRuleResult: %ld\n", bResult));
  62. //
  63. // This really can't happen, but ...
  64. //
  65. if ( m_BizRuleResult == NULL || m_AcContext == NULL || m_ScriptError == NULL ) {
  66. ASSERT( FALSE );
  67. return E_FAIL;
  68. }
  69. ASSERT( AzpIsCritsectLocked( &m_AcContext->ClientContext->CritSect ) );
  70. //
  71. // Set the result
  72. //
  73. *m_BizRuleResult = bResult;
  74. return S_OK;
  75. }
  76. HRESULT
  77. CAzBizRuleContext::put_BusinessRuleString(
  78. IN BSTR bstrBusinessRuleString
  79. )
  80. /*++
  81. Routine Description:
  82. The script calls the put_BusinessRuleString method to store a string to be returned
  83. from access check.
  84. If the no script ever calls this method, a zero length string is returned from access check.
  85. On entry, AcContext->ClientContext.CritSect must be locked.
  86. Arguments:
  87. bstrBusinessRuleString -- The string to store
  88. Return Value:
  89. None
  90. --*/
  91. {
  92. HRESULT hr;
  93. DWORD WinStatus;
  94. AZP_STRING CapturedString;
  95. //
  96. // Initialization
  97. //
  98. AzPrint((AZD_SCRIPT, "CAzBizRuleContext::put_BusinessRuleString: %ws\n", bstrBusinessRuleString ));
  99. AzpInitString( &CapturedString, NULL );
  100. //
  101. // This really can't happen, but ...
  102. //
  103. if ( m_BizRuleResult == NULL || m_AcContext == NULL || m_ScriptError == NULL ) {
  104. ASSERT( FALSE );
  105. hr = E_FAIL;
  106. goto Cleanup;
  107. }
  108. ASSERT( AzpIsCritsectLocked( &m_AcContext->ClientContext->CritSect ) );
  109. //
  110. // Capture the input string
  111. //
  112. WinStatus = AzpCaptureString( &CapturedString,
  113. bstrBusinessRuleString,
  114. AZ_MAX_BIZRULE_STRING,
  115. TRUE ); // NULL is OK
  116. if ( WinStatus != NO_ERROR ) {
  117. hr = E_OUTOFMEMORY;
  118. goto Cleanup;
  119. }
  120. //
  121. // Swap the old/new names
  122. //
  123. AzpSwapStrings( &CapturedString, &m_AcContext->BusinessRuleString );
  124. hr = S_OK;
  125. Cleanup:
  126. AzpFreeString( &CapturedString );
  127. return hr;
  128. }
  129. HRESULT
  130. CAzBizRuleContext::get_BusinessRuleString(
  131. OUT BSTR *pbstrBusinessRuleString)
  132. /*++
  133. Routine Description:
  134. The script calls the get_BusinessRuleString method to get a copy of the string to be returned
  135. from access check.
  136. If the no script ever calls this method, a zero length string is returned from access check.
  137. On entry, AcContext->ClientContext.CritSect must be locked.
  138. Arguments:
  139. bstrBusinessRuleString -- A pointer to the string to return
  140. Return Value:
  141. None
  142. --*/
  143. {
  144. LPWSTR TempString;
  145. AzPrint((AZD_SCRIPT, "CAzBizRuleContext::get_BusinessRuleString: %ws\n", m_AcContext->BusinessRuleString.String ));
  146. //
  147. // This really can't happen, but ...
  148. //
  149. if ( m_BizRuleResult == NULL || m_AcContext == NULL || m_ScriptError == NULL ) {
  150. ASSERT( FALSE );
  151. return E_FAIL;
  152. }
  153. ASSERT( AzpIsCritsectLocked( &m_AcContext->ClientContext->CritSect ) );
  154. //
  155. // Set the result
  156. //
  157. if ( m_AcContext->BusinessRuleString.String == NULL ) {
  158. TempString = NULL;
  159. } else {
  160. TempString = SysAllocString( m_AcContext->BusinessRuleString.String );
  161. if ( TempString == NULL ) {
  162. return E_OUTOFMEMORY;
  163. }
  164. }
  165. //
  166. // Return it to the caller
  167. //
  168. *pbstrBusinessRuleString = TempString;
  169. return S_OK;
  170. }
  171. HRESULT
  172. CAzBizRuleContext::GetParameter(
  173. IN BSTR bstrParameterName,
  174. OUT VARIANT *pvarParameterValue )
  175. {
  176. /*++
  177. Routine Description:
  178. The script calls the GetParameter method to get the parameters
  179. passed into IAzClientContext::AccessCheck in Parameters
  180. On entry, AcContext->ClientContext.CritSect must be locked.
  181. Arguments:
  182. bstrParameterName - Specifies the parameter to get. This name must match the name
  183. in one of the elements specified in the ParameterNames array passed to AccessCheck.
  184. pvarParameterValue - Returns a variant containing the corresponding element in the
  185. ParameterValues array passed to AccessCheck.
  186. Return Value:
  187. S_OK: pvarParameterValue was returned successfully
  188. E_INVALIDARG: bstrParameterName did not correspond to a passed in parameter
  189. --*/
  190. HRESULT hr;
  191. VARIANT Name;
  192. VARIANT *ParameterNameInArray;
  193. ULONG ArrayIndex;
  194. AzPrint((AZD_SCRIPT, "CAzBizRuleContext::GetParameter: %ls\n", bstrParameterName ));
  195. //
  196. // This really can't happen, but ...
  197. //
  198. if ( m_BizRuleResult == NULL || m_AcContext == NULL || m_ScriptError == NULL ) {
  199. ASSERT( FALSE );
  200. return E_FAIL;
  201. }
  202. ASSERT( AzpIsCritsectLocked( &m_AcContext->ClientContext->CritSect ) );
  203. //
  204. // Convert name to an easier form to compare
  205. //
  206. VariantInit( &Name );
  207. V_VT(&Name) = VT_BSTR;
  208. V_BSTR(&Name) = bstrParameterName;
  209. //
  210. // We didn't capture the array
  211. // So access it under a try/except
  212. __try {
  213. //
  214. // Do a binary search to find the parameter
  215. //
  216. if (m_bCaseSensitive)
  217. {
  218. ParameterNameInArray = (VARIANT *) bsearch(
  219. &Name,
  220. m_AcContext->ParameterNames,
  221. m_AcContext->ParameterCount,
  222. sizeof(VARIANT),
  223. AzpCompareParameterNames );
  224. }
  225. else
  226. {
  227. ParameterNameInArray = (VARIANT *) bsearch(
  228. &Name,
  229. m_AcContext->ParameterNames,
  230. m_AcContext->ParameterCount,
  231. sizeof(VARIANT),
  232. AzpCaseInsensitiveCompareParameterNames );
  233. }
  234. if ( ParameterNameInArray == NULL ) {
  235. AzPrint((AZD_INVPARM, "CAzBizRuleContext::GetParameter: %ls: Parameter not passed in.\n", bstrParameterName ));
  236. hr = E_INVALIDARG;
  237. goto Cleanup;
  238. }
  239. //
  240. // Return the parameter to the caller
  241. //
  242. ArrayIndex = (ULONG)(ParameterNameInArray - m_AcContext->ParameterNames);
  243. hr = VariantCopy( pvarParameterValue, &m_AcContext->ParameterValues[ArrayIndex] );
  244. if ( FAILED(hr)) {
  245. goto Cleanup;
  246. }
  247. //
  248. // Mark that we've used this parameter
  249. //
  250. if ( !m_AcContext->UsedParameters[ArrayIndex] ) {
  251. m_AcContext->UsedParameters[ArrayIndex] = TRUE;
  252. m_AcContext->UsedParameterCount ++;
  253. }
  254. hr = S_OK;
  255. Cleanup:;
  256. } __except( EXCEPTION_EXECUTE_HANDLER ) {
  257. hr = GetExceptionCode();
  258. AzPrint((AZD_CRITICAL, "GetParameter took an exception: 0x%lx\n", hr));
  259. }
  260. if ( FAILED(hr)) {
  261. *m_ScriptError = hr;
  262. }
  263. return hr;
  264. }
  265. VOID
  266. SetAccessCheckContext(
  267. IN OUT CAzBizRuleContext* BizRuleContext,
  268. IN BOOL bCaseSensitive,
  269. IN PACCESS_CHECK_CONTEXT AcContext,
  270. IN PBOOL BizRuleResult,
  271. IN HRESULT *ScriptError
  272. )
  273. /*++
  274. Routine Description:
  275. The routine tells this object about the current access check context that is running.
  276. The context is used to satisfy queries from the script about the access check being performed.
  277. On entry, AcContext->ClientContext.CritSect must be locked.
  278. Arguments:
  279. BizRuleContext - Pointer to the instance of IAzBizRuleContext to update.
  280. bCaseSensitive - Specify whether the the script is case-sensitive or not.
  281. AcContext - Specifies the context of the accesscheck operation the bizrule is being evaluated for
  282. NULL - sets the AccessCheck object back to an initialized state.
  283. BizRuleResult - Result of the bizrule
  284. NULL - sets the AccessCheck object back to an initialized state.
  285. ScriptError - Status of the script.
  286. Any error generated by the script should be returned here.
  287. NULL - sets the AccessCheck object back to an initialized state.
  288. Return Value:
  289. None
  290. Note:
  291. 1. This function modifies the state of the BizRuleContext object. And the modification
  292. is not protected. The reason this works, and future implementation must keep this
  293. in mind, is that we execute biz rules one after another sequentially. When that
  294. needs to be changed, then the whole biz rule context object needs to be re-written.
  295. 2. This function should really be a member function of CAzBizRuleContext instead of
  296. a friend function. Need to change that in V2.
  297. --*/
  298. {
  299. //
  300. // Initialization
  301. //
  302. AzPrint(( AZD_SCRIPT_MORE, "CAzBizRuleContext::SetAccessCheckContext\n"));
  303. if ( AcContext != NULL ) {
  304. ASSERT( AzpIsCritsectLocked( &AcContext->ClientContext->CritSect ) );
  305. }
  306. //
  307. // Save the pointer to the context
  308. //
  309. ASSERT( BizRuleContext != NULL );
  310. ASSERT( BizRuleContext->m_AcContext == NULL || AcContext == NULL );
  311. ASSERT( BizRuleContext->m_BizRuleResult == NULL || BizRuleResult == NULL );
  312. ASSERT( BizRuleContext->m_ScriptError == NULL || ScriptError == NULL );
  313. BizRuleContext->m_AcContext = AcContext;
  314. BizRuleContext->m_BizRuleResult = BizRuleResult;
  315. BizRuleContext->m_ScriptError = ScriptError;
  316. BizRuleContext->m_bCaseSensitive = bCaseSensitive;
  317. }