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.

117 lines
4.0 KiB

  1. //Copyright (c) Microsoft Corporation. All rights reserved.
  2. #include <windows.h>
  3. #include "zone.h"
  4. #include <urlmon.h>
  5. int __cdecl IsTrustedServer( LPWSTR szServer, LPWSTR szZoneName, DWORD dwZoneNameLen, DWORD *pdwZonePolicy )
  6. {
  7. int iRetVal = FALSE;
  8. if( !szServer || !szZoneName || !pdwZonePolicy )
  9. {
  10. goto IsTargetServerAbort0;
  11. }
  12. if( IsTargetServerSafeOnProtocol( szServer, szZoneName, dwZoneNameLen, pdwZonePolicy, PROTOCOL_PREFIX_TELNET ) )
  13. {
  14. //Should we be getting the name if given ip( and viceversa) to check for zones as well? What happens in the presence of DHCP?
  15. //Do we need to check for http://machine as well?
  16. iRetVal = TRUE;
  17. }
  18. IsTargetServerAbort0:
  19. return iRetVal;
  20. }
  21. int __cdecl IsTargetServerSafeOnProtocol( LPWSTR szServer, LPWSTR szZoneName, DWORD dwZoneNameLen, DWORD *pdwZonePolicy, LPWSTR szProtocol )
  22. {
  23. MULTI_QI qiSecurityMgr[] = {{ &IID_IInternetSecurityManager, NULL, S_OK }};
  24. MULTI_QI qiZoneMgr[] = {{ &IID_IInternetZoneManager, NULL, S_OK }};
  25. IInternetSecurityManager *pSecurityMgr = NULL;
  26. IInternetZoneManager *pZoneMgr = NULL;
  27. ZONEATTRIBUTES zaAttribs;
  28. DWORD dwTargetServerZone = 0;
  29. HRESULT hr = S_FALSE;
  30. int iRetVal = FALSE;
  31. LPWSTR lpszTargetServer = NULL;
  32. DWORD dwSize = 0;
  33. bool bCoInit = false;
  34. if( !szServer || !szZoneName || !szProtocol || !pdwZonePolicy )
  35. {
  36. goto IsTargetServerSafeOnProtocol0;
  37. }
  38. hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
  39. if( !SUCCEEDED( hr ) )
  40. {
  41. goto IsTargetServerSafeOnProtocol0;
  42. }
  43. bCoInit = true;
  44. hr = CoCreateInstanceEx(CLSID_InternetSecurityManager, NULL, CLSCTX_INPROC_SERVER, NULL,
  45. sizeof(qiSecurityMgr) / sizeof(MULTI_QI), qiSecurityMgr );
  46. if( !SUCCEEDED( hr ) || !SUCCEEDED(qiSecurityMgr[INDEX_SEC_MGR].hr) ||
  47. !(pSecurityMgr=(IInternetSecurityManager*)qiSecurityMgr[INDEX_SEC_MGR].pItf))
  48. {
  49. goto IsTargetServerSafeOnProtocol0;
  50. }
  51. dwSize = ( wcslen( szServer ) + wcslen( szProtocol ) + 1 ) ;
  52. lpszTargetServer = (WCHAR * )malloc( dwSize * sizeof( WCHAR ));
  53. if( !lpszTargetServer )
  54. {
  55. goto IsTargetServerSafeOnProtocol1;
  56. }
  57. wcsncpy( lpszTargetServer, szProtocol, dwSize - 1 );
  58. lpszTargetServer[dwSize - 1] = L'\0';
  59. wcsncat( lpszTargetServer, szServer, (dwSize - wcslen(lpszTargetServer) -1));
  60. hr = pSecurityMgr->MapUrlToZone(lpszTargetServer, &dwTargetServerZone, 0);
  61. if( !SUCCEEDED(hr) )
  62. {
  63. goto IsTargetServerSafeOnProtocol2;
  64. }
  65. hr = CoCreateInstanceEx(CLSID_InternetZoneManager, NULL, CLSCTX_INPROC_SERVER, NULL,
  66. sizeof(qiZoneMgr) / sizeof(MULTI_QI), qiZoneMgr );
  67. if( !SUCCEEDED( hr ) || !SUCCEEDED(qiZoneMgr[INDEX_ZONE_MGR].hr) ||
  68. !(pZoneMgr=(IInternetZoneManager*)qiZoneMgr[INDEX_ZONE_MGR].pItf) )
  69. {
  70. goto IsTargetServerSafeOnProtocol2;
  71. }
  72. hr = pZoneMgr->GetZoneAttributes( dwTargetServerZone, &zaAttribs );
  73. if( SUCCEEDED( hr ) )
  74. {
  75. wcsncpy( szZoneName, zaAttribs.szDisplayName, MIN( ( wcslen( zaAttribs.szDisplayName ) + 1 ), dwZoneNameLen ) );
  76. }
  77. hr = pZoneMgr->GetZoneActionPolicy( dwTargetServerZone, URLACTION_CREDENTIALS_USE,
  78. (BYTE*)pdwZonePolicy, sizeof( *pdwZonePolicy), URLZONEREG_DEFAULT );
  79. if( !SUCCEEDED( hr ) )
  80. {
  81. goto IsTargetServerSafeOnProtocol3;
  82. }
  83. if((URLPOLICY_CREDENTIALS_SILENT_LOGON_OK == *pdwZonePolicy ) ||
  84. (URLPOLICY_CREDENTIALS_CONDITIONAL_PROMPT == *pdwZonePolicy && URLZONE_INTRANET == dwTargetServerZone ) )
  85. {
  86. iRetVal = TRUE;
  87. }
  88. IsTargetServerSafeOnProtocol3:
  89. pZoneMgr->Release();
  90. IsTargetServerSafeOnProtocol2:
  91. free( lpszTargetServer );
  92. IsTargetServerSafeOnProtocol1:
  93. pSecurityMgr->Release();
  94. IsTargetServerSafeOnProtocol0:
  95. if(bCoInit)
  96. {
  97. CoUninitialize();
  98. }
  99. return iRetVal;
  100. }