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.

207 lines
4.8 KiB

  1. #pragma once
  2. #include "pch.h"
  3. #pragma hdrstop
  4. #include "winsock2.h"
  5. #include "util.h"
  6. HRESULT SetUPnPError(LPOLESTR pszError)
  7. {
  8. HRESULT hr = S_OK;
  9. ICreateErrorInfo* pCreateErrorInfo;
  10. hr = CreateErrorInfo(&pCreateErrorInfo);
  11. if(SUCCEEDED(hr))
  12. {
  13. hr = pCreateErrorInfo->SetSource(pszError);
  14. if(SUCCEEDED(hr))
  15. {
  16. IErrorInfo* pErrorInfo;
  17. hr = pCreateErrorInfo->QueryInterface(IID_IErrorInfo, reinterpret_cast<void**>(&pErrorInfo));
  18. if(SUCCEEDED(hr))
  19. {
  20. hr = SetErrorInfo(0, pErrorInfo);
  21. pErrorInfo->Release();
  22. }
  23. }
  24. pCreateErrorInfo->Release();
  25. }
  26. return hr;
  27. }
  28. VOID SetProxyBlanket(IUnknown *pUnk)
  29. {
  30. HRESULT hr;
  31. _ASSERT(pUnk);
  32. hr = CoSetProxyBlanket(
  33. pUnk,
  34. RPC_C_AUTHN_WINNT, // use NT default security
  35. RPC_C_AUTHZ_NONE, // use NT default authentication
  36. NULL, // must be null if default
  37. RPC_C_AUTHN_LEVEL_CALL, // call
  38. RPC_C_IMP_LEVEL_IMPERSONATE,
  39. NULL, // use process token
  40. EOAC_NONE
  41. );
  42. if (SUCCEEDED(hr))
  43. {
  44. IUnknown * pUnkSet = NULL;
  45. hr = pUnk->QueryInterface(&pUnkSet);
  46. if (SUCCEEDED(hr))
  47. {
  48. hr = CoSetProxyBlanket(
  49. pUnkSet,
  50. RPC_C_AUTHN_WINNT, // use NT default security
  51. RPC_C_AUTHZ_NONE, // use NT default authentication
  52. NULL, // must be null if default
  53. RPC_C_AUTHN_LEVEL_CALL, // call
  54. RPC_C_IMP_LEVEL_IMPERSONATE,
  55. NULL, // use process token
  56. EOAC_NONE
  57. );
  58. pUnkSet->Release();
  59. }
  60. }
  61. }
  62. HANDLE CSwitchSecurityContext::m_hImpersonationToken = NULL;
  63. CSwitchSecurityContext::CSwitchSecurityContext( )
  64. {
  65. HRESULT hr = S_OK;
  66. hr = CoImpersonateClient();
  67. if ( SUCCEEDED(hr) )
  68. {
  69. if ( FALSE == SetThreadToken( NULL,
  70. m_hImpersonationToken ) )
  71. {
  72. hr = HRESULT_FROM_WIN32(GetLastError()) ;
  73. CoRevertToSelf();
  74. }
  75. }
  76. _ASSERT( SUCCEEDED(hr) );
  77. }
  78. HRESULT
  79. CSwitchSecurityContext::ObtainImpersonationToken ( )
  80. {
  81. HANDLE hTokenHandleForDuplicate = NULL;
  82. HRESULT hr = S_OK;
  83. if ( TRUE == OpenProcessToken( GetCurrentProcess(),
  84. TOKEN_DUPLICATE | TOKEN_QUERY,
  85. &hTokenHandleForDuplicate ) )
  86. {
  87. if ( FALSE == DuplicateToken( hTokenHandleForDuplicate,
  88. SecurityImpersonation,
  89. &m_hImpersonationToken ))
  90. {
  91. hr = HRESULT_FROM_WIN32( GetLastError() );
  92. }
  93. CloseHandle( hTokenHandleForDuplicate );
  94. }
  95. else
  96. {
  97. hr = HRESULT_FROM_WIN32( GetLastError() );
  98. }
  99. return hr;
  100. }
  101. DWORD WINAPI
  102. INET_ADDR(LPCWSTR szAddressW)
  103. //
  104. // The 3 "." testing is for the sole purpose of preventing computer
  105. // names with digits to be catched appropriately.
  106. // so we strictly assume a valid IP is number.number.number.number
  107. //
  108. {
  109. CHAR szAddressA[16];
  110. int iCount = 0;
  111. LPCWSTR tmp = szAddressW;
  112. // Check wether it is a non-shortcut IPs.
  113. while(tmp = wcschr(tmp, L'.'))
  114. { tmp++; iCount++; }
  115. if ( iCount < 3)
  116. { return INADDR_NONE; }
  117. wcstombs(szAddressA, szAddressW, 16);
  118. return inet_addr(szAddressA);
  119. }
  120. WCHAR * WINAPI
  121. INET_NTOW( ULONG addr )
  122. {
  123. struct in_addr dwAddress;
  124. static WCHAR szAddress[16];
  125. dwAddress.S_un.S_addr = addr;
  126. char* pAddr = inet_ntoa(*(struct in_addr *) &dwAddress);
  127. if (pAddr)
  128. {
  129. // mbstowcs(szAddress, inet_ntoa(*(struct in_addr *)&dwAddress), 16);
  130. MultiByteToWideChar(CP_ACP, 0, pAddr, -1, szAddress, 16);
  131. return szAddress;
  132. }
  133. else
  134. return NULL;
  135. }
  136. WCHAR * WINAPI
  137. INET_NTOW_TS( ULONG addr )
  138. {
  139. WCHAR* retString = NULL;
  140. CHAR* asciiString = NULL;
  141. struct in_addr dwAddress;
  142. dwAddress.S_un.S_addr = addr;
  143. retString = (WCHAR*)CoTaskMemAlloc( 16 * sizeof(WCHAR) );
  144. if ( NULL == retString )
  145. {
  146. return NULL;
  147. }
  148. //
  149. // note that inet_nota is thread safe
  150. // altough it uses static buffer
  151. //
  152. asciiString = inet_ntoa( dwAddress);
  153. if (asciiString != NULL)
  154. {
  155. MultiByteToWideChar(CP_ACP, 0, asciiString, -1, retString, 16);
  156. return retString;
  157. }
  158. return NULL;
  159. }