Source code of Windows XP (NT5)
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.

203 lines
4.8 KiB

  1. //******************************************************************************
  2. //
  3. // TEMPFILT.CPP
  4. //
  5. // Copyright (C) 1996-1999 Microsoft Corporation
  6. //
  7. //******************************************************************************
  8. #include "precomp.h"
  9. #include <stdio.h>
  10. #include "ess.h"
  11. #include "tempfilt.h"
  12. #include <cominit.h>
  13. #include <tkncache.h>
  14. #include <callsec.h>
  15. #include <wbemutil.h>
  16. CTempFilter::CTempFilter(CEssNamespace* pNamespace)
  17. : CGenericFilter(pNamespace), m_wszQueryLanguage(NULL),
  18. m_wszQuery(NULL), m_pSecurity(NULL), m_bInternal( false )
  19. {
  20. }
  21. HRESULT CTempFilter::Initialize( LPCWSTR wszQueryLanguage,
  22. LPCWSTR wszQuery,
  23. long lFlags,
  24. PSID pOwnerSid,
  25. bool bInternal,
  26. IWbemContext* pContext,
  27. IWbemObjectSink* pSink )
  28. {
  29. HRESULT hres;
  30. m_bInternal = bInternal;
  31. hres = CGenericFilter::Create(wszQueryLanguage, wszQuery);
  32. if(FAILED(hres))
  33. return hres;
  34. LPWSTR wszKey = ComputeThisKey();
  35. if(wszKey == NULL)
  36. return WBEM_E_OUT_OF_MEMORY;
  37. CVectorDeleteMe<WCHAR> vdm(wszKey);
  38. if(!(m_isKey = wszKey))
  39. return WBEM_E_OUT_OF_MEMORY;
  40. m_wszQueryLanguage = CloneWstr(wszQueryLanguage);
  41. if(m_wszQueryLanguage == NULL)
  42. return WBEM_E_OUT_OF_MEMORY;
  43. m_wszQuery = CloneWstr(wszQuery);
  44. if(m_wszQuery == NULL)
  45. return WBEM_E_OUT_OF_MEMORY;
  46. //
  47. // if this filter is effectively permanent, that is it is being created
  48. // on behalf of a permanent subscription ( for cross namespace purposes ),
  49. // then we need to propagate the SID of the original subscription.
  50. // For a normal temp filter, we save the call context and use that later
  51. // for checking access.
  52. //
  53. if ( pOwnerSid == NULL )
  54. {
  55. //
  56. // if this call is an on behalf of an internal call, no need to
  57. // check security.
  58. //
  59. if ( !bInternal )
  60. {
  61. WbemCoGetCallContext( IID_IWbemCallSecurity, (void**)&m_pSecurity);
  62. }
  63. }
  64. else
  65. {
  66. int cOwnerSid = GetLengthSid( pOwnerSid );
  67. m_pOwnerSid = new BYTE[cOwnerSid];
  68. if ( m_pOwnerSid == NULL )
  69. {
  70. return WBEM_E_OUT_OF_MEMORY;
  71. }
  72. memcpy( m_pOwnerSid, pOwnerSid, cOwnerSid );
  73. }
  74. return WBEM_S_NO_ERROR;
  75. }
  76. CTempFilter::~CTempFilter()
  77. {
  78. delete [] m_wszQuery;
  79. delete [] m_wszQueryLanguage;
  80. if(m_pSecurity)
  81. m_pSecurity->Release();
  82. }
  83. DELETE_ME LPWSTR CTempFilter::ComputeThisKey()
  84. {
  85. LPWSTR wszKey = _new WCHAR[20];
  86. if ( wszKey )
  87. {
  88. swprintf(wszKey, L"$%p", this);
  89. }
  90. return wszKey;
  91. }
  92. HRESULT CTempFilter::GetCoveringQuery(DELETE_ME LPWSTR& wszQueryLanguage,
  93. DELETE_ME LPWSTR& wszQuery, BOOL& bExact,
  94. DELETE_ME QL_LEVEL_1_RPN_EXPRESSION** ppExp)
  95. {
  96. bExact = TRUE;
  97. wszQueryLanguage = CloneWstr(m_wszQueryLanguage);
  98. if(wszQueryLanguage == NULL)
  99. return WBEM_E_OUT_OF_MEMORY;
  100. wszQuery = CloneWstr(m_wszQuery);
  101. if(wszQuery== NULL)
  102. {
  103. delete [] wszQueryLanguage;
  104. wszQueryLanguage = NULL;
  105. return WBEM_E_OUT_OF_MEMORY;
  106. }
  107. if(ppExp)
  108. {
  109. CTextLexSource src((LPWSTR)wszQuery);
  110. QL1_Parser parser(&src);
  111. int nRes = parser.Parse(ppExp);
  112. if (nRes)
  113. {
  114. delete [] wszQueryLanguage;
  115. delete [] wszQuery;
  116. wszQueryLanguage = NULL;
  117. wszQuery = NULL;
  118. ERRORTRACE((LOG_ESS, "Unable to construct event filter with "
  119. "unparsable "
  120. "query '%S'. The filter is not active\n", wszQuery));
  121. return WBEM_E_UNPARSABLE_QUERY;
  122. }
  123. }
  124. return WBEM_S_NO_ERROR;
  125. }
  126. HRESULT CTempFilter::SetThreadSecurity()
  127. {
  128. HRESULT hr;
  129. if ( m_pSecurity != NULL )
  130. {
  131. IUnknown* pOld;
  132. hr = WbemCoSwitchCallContext( m_pSecurity, &pOld );
  133. }
  134. else
  135. {
  136. hr = WBEM_S_FALSE;
  137. }
  138. return hr;
  139. }
  140. HRESULT CTempFilter::ObtainToken(IWbemToken** ppToken)
  141. {
  142. HRESULT hr;
  143. *ppToken = NULL;
  144. //
  145. // Construct an IWbemToken object to return.
  146. //
  147. if ( m_pSecurity != NULL )
  148. {
  149. CWmiToken* pNewToken = new CWmiToken(m_pSecurity->GetToken());
  150. if ( pNewToken != NULL )
  151. {
  152. hr = pNewToken->QueryInterface(IID_IWbemToken, (void**)ppToken);
  153. }
  154. else
  155. {
  156. hr = WBEM_E_OUT_OF_MEMORY;
  157. }
  158. }
  159. else if ( m_pOwnerSid != NULL )
  160. {
  161. hr = m_pNamespace->GetToken( m_pOwnerSid, ppToken );
  162. }
  163. else if ( m_bInternal )
  164. {
  165. hr = WBEM_S_FALSE;
  166. }
  167. else
  168. {
  169. hr = WBEM_E_FAILED;
  170. }
  171. return hr;
  172. }