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.

224 lines
5.0 KiB

  1. /*++
  2. Copyright (C) 1996-2001 Microsoft Corporation
  3. Module Name:
  4. Abstract:
  5. History:
  6. --*/
  7. #include "precomp.h"
  8. #include <sspi.h>
  9. #include <secext.h>
  10. #include <wmimsg.h>
  11. #include <comutl.h>
  12. #include "rpcctx.h"
  13. HRESULT RpcResToWmiRes( RPC_STATUS stat, HRESULT hrDefault )
  14. {
  15. //
  16. // override the default error code here if a more specific one can be
  17. // determined
  18. //
  19. switch( stat )
  20. {
  21. case EPT_S_NOT_REGISTERED :
  22. return WMIMSG_E_TARGETNOTLISTENING;
  23. case ERROR_ACCESS_DENIED :
  24. return WBEM_E_ACCESS_DENIED;
  25. };
  26. return hrDefault == S_OK ? HRESULT_FROM_WIN32(stat) : hrDefault;
  27. }
  28. /******************************************************/
  29. /* MIDL allocate and free */
  30. /******************************************************/
  31. void __RPC_FAR * __RPC_USER midl_user_allocate(size_t len)
  32. {
  33. return( new BYTE[len] );
  34. }
  35. void __RPC_USER midl_user_free(void __RPC_FAR * ptr)
  36. {
  37. delete [] ptr;
  38. }
  39. /***************************************************************************
  40. CMsgRpcRcvrCtx
  41. ****************************************************************************/
  42. STDMETHODIMP CMsgRpcRcvrCtx::GetTimeSent( SYSTEMTIME* pTime )
  43. {
  44. *pTime = *m_pHdr->GetTimeSent();
  45. return S_OK;
  46. }
  47. STDMETHODIMP CMsgRpcRcvrCtx::GetSendingMachine( WCHAR* awchMachine,
  48. ULONG cMachine,
  49. ULONG* pcMachine )
  50. {
  51. LPCWSTR wszSource = m_pHdr->GetSendingMachine();
  52. *pcMachine = wcslen( wszSource ) + 1;
  53. if ( *pcMachine > cMachine )
  54. {
  55. return S_FALSE;
  56. }
  57. StringCchCopyW( awchMachine, cMachine, wszSource );
  58. return S_OK;
  59. }
  60. STDMETHODIMP CMsgRpcRcvrCtx::GetTarget( WCHAR* awchTarget,
  61. ULONG cTarget,
  62. ULONG* pcTarget )
  63. {
  64. *pcTarget = 0;
  65. return WBEM_E_NOT_SUPPORTED;
  66. }
  67. STDMETHODIMP CMsgRpcRcvrCtx::IsSenderAuthenticated()
  68. {
  69. HRESULT hr;
  70. DWORD dwAuthn;
  71. DWORD dwLevel;
  72. RPC_STATUS stat;
  73. stat = RpcBindingInqAuthClient( m_hClient,
  74. NULL,
  75. NULL,
  76. &dwLevel,
  77. &dwAuthn,
  78. NULL );
  79. if ( stat == RPC_S_OK &&
  80. dwAuthn != RPC_C_AUTHN_NONE &&
  81. dwLevel >= RPC_C_AUTHN_LEVEL_PKT_INTEGRITY )
  82. {
  83. hr = WBEM_S_NO_ERROR;
  84. }
  85. else
  86. {
  87. hr = WBEM_S_FALSE;
  88. }
  89. return hr;
  90. }
  91. STDMETHODIMP CMsgRpcRcvrCtx::ImpersonateSender()
  92. {
  93. HRESULT hr;
  94. RPC_STATUS stat = RpcImpersonateClient( m_hClient );
  95. if ( stat == RPC_S_OK )
  96. {
  97. hr = WBEM_S_NO_ERROR;
  98. }
  99. else
  100. {
  101. hr = RpcResToWmiRes( stat, S_OK );
  102. }
  103. return hr;
  104. }
  105. STDMETHODIMP CMsgRpcRcvrCtx::RevertToSelf()
  106. {
  107. HRESULT hr;
  108. RPC_STATUS stat = RpcRevertToSelf();
  109. if ( stat == RPC_S_OK )
  110. {
  111. hr = WBEM_S_NO_ERROR;
  112. }
  113. else
  114. {
  115. hr = RpcResToWmiRes( stat, S_OK );
  116. }
  117. return hr;
  118. }
  119. STDMETHODIMP CMsgRpcRcvrCtx::GetSenderId( PBYTE pchSenderId,
  120. ULONG cSenderId,
  121. ULONG* pcSenderId )
  122. {
  123. HRESULT hr;
  124. RPC_STATUS stat;
  125. *pcSenderId = 0;
  126. stat = RpcImpersonateClient( m_hClient );
  127. if ( stat == RPC_S_OK )
  128. {
  129. HANDLE hToken;
  130. if ( OpenThreadToken( GetCurrentThread(), TOKEN_QUERY, TRUE, &hToken) )
  131. {
  132. //
  133. // use the passed in buffer to get the SID_AND_ATTRIBUTES
  134. //
  135. DWORD dwLen;
  136. if ( GetTokenInformation( hToken,
  137. TokenUser,
  138. pchSenderId,
  139. cSenderId,
  140. &dwLen ) )
  141. {
  142. //
  143. // move the sid to the beginning of the buffer.
  144. //
  145. PSID pSid = PSID_AND_ATTRIBUTES(pchSenderId)->Sid;
  146. *pcSenderId = GetLengthSid( pSid );
  147. memmove( pchSenderId, pSid, *pcSenderId );
  148. hr = WBEM_S_NO_ERROR;
  149. }
  150. else
  151. {
  152. DWORD dwRes = GetLastError();
  153. if ( dwRes == ERROR_MORE_DATA )
  154. {
  155. *pcSenderId = dwLen;
  156. hr = WBEM_S_FALSE;
  157. }
  158. else
  159. {
  160. hr = WMIMSG_E_AUTHFAILURE;
  161. }
  162. }
  163. CloseHandle( hToken );
  164. }
  165. else
  166. {
  167. hr = WMIMSG_E_AUTHFAILURE;
  168. }
  169. RpcRevertToSelf();
  170. }
  171. else
  172. {
  173. hr = RpcResToWmiRes( stat, S_OK );
  174. }
  175. return hr;
  176. }