utils.cpp
Upload User: xhy777
Upload Date: 2007-02-14
Package Size: 24088k
Code Size: 5k
Category:

Windows Kernel

Development Platform:

Visual C++

  1. #include "stdafx.h"
  2. #pragma hdrstop
  3. // This isn't a typical delay load since it's called only if wininet
  4. // is already loaded in memory. Otherwise the call is dropped on the floor.
  5. // Defview did it this way I assume to keep WININET out of first boot time.
  6. BOOL MyInternetSetOption(HANDLE h, DWORD dw1, LPVOID lpv, DWORD dw2)
  7. {
  8.     BOOL bRet = FALSE;
  9.     HMODULE hmod = GetModuleHandle(TEXT("wininet.dll"));
  10.     if (hmod)
  11.     {
  12.         typedef BOOL (*PFNINTERNETSETOPTIONA)(HANDLE h, DWORD dw1, LPVOID lpv, DWORD dw2);
  13.         FARPROC fp = GetProcAddress(hmod, "InternetSetOptionA");
  14.         if (fp)
  15.         {
  16.             bRet = ((PFNINTERNETSETOPTIONA)fp)(h, dw1, lpv, dw2);
  17.         }
  18.     }
  19.     return bRet;
  20. }
  21. // REVIEW: maybe just check (hwnd == GetShellWindow())
  22. STDAPI_(BOOL) IsDesktopWindow(HWND hwnd)
  23. {
  24.     TCHAR szName[80];
  25.     GetClassName(hwnd, szName, ARRAYSIZE(szName));
  26.     if (!lstrcmp(szName, TEXT(STR_DESKTOPCLASS)))
  27.     {
  28.         GetWindowText(hwnd, szName, ARRAYSIZE(szName));
  29.         return !lstrcmp(szName, TEXT("Program Manager"));
  30.     }
  31.     return FALSE;
  32. }
  33. STDAPI GetHTMLDoc2(IUnknown *punk, IHTMLDocument2 **ppHtmlDoc)
  34. {
  35.     *ppHtmlDoc = NULL;
  36.     if (!punk)
  37.         return E_FAIL;
  38.         
  39.     *ppHtmlDoc = NULL;
  40.     //  The window.external, jscript "new ActiveXObject" and the <OBJECT> tag
  41.     //  don't take us down the same road.
  42.     IOleClientSite *pClientSite;
  43.     HRESULT hr = punk->QueryInterface(IID_PPV_ARG(IOleClientSite, &pClientSite));
  44.     if (SUCCEEDED(hr))
  45.     {
  46.         //  <OBJECT> tag path
  47.         IOleContainer *pContainer;
  48.         hr = pClientSite->GetContainer(&pContainer);
  49.         if (SUCCEEDED(hr))
  50.         {
  51.             hr = pContainer->QueryInterface(IID_PPV_ARG(IHTMLDocument2, ppHtmlDoc));
  52.             pContainer->Release();
  53.         }
  54.     
  55.         if (FAILED(hr))
  56.         {
  57.             //  window.external path
  58.             IWebBrowser2 *pWebBrowser2;
  59.             hr = IUnknown_QueryService(pClientSite, SID_SWebBrowserApp, IID_PPV_ARG(IWebBrowser2, &pWebBrowser2));
  60.             if (SUCCEEDED(hr))
  61.             {
  62.                 IDispatch *pDispatch;
  63.                 hr = pWebBrowser2->get_Document(&pDispatch);
  64.                 if (SUCCEEDED(hr))
  65.                 {
  66.                     hr = pDispatch->QueryInterface(IID_PPV_ARG(IHTMLDocument2, ppHtmlDoc));
  67.                     pDispatch->Release();
  68.                 }
  69.                 pWebBrowser2->Release();
  70.             }
  71.         }
  72.         pClientSite->Release();
  73.     }
  74.     else
  75.     {
  76.         //  jscript path
  77.         hr = IUnknown_QueryService(punk, SID_SContainerDispatch, IID_PPV_ARG(IHTMLDocument2, ppHtmlDoc));
  78.     }
  79.     ASSERT(FAILED(hr) || (*ppHtmlDoc));
  80.     return hr;
  81. }
  82. STDAPI LocalZoneCheckPath(LPCWSTR bstrPath)
  83. {
  84.     HRESULT hr = E_ACCESSDENIED;
  85.     if (bstrPath) 
  86.     {
  87.         IInternetSecurityManager *pSecMgr;
  88.         if (SUCCEEDED(CoCreateInstance(CLSID_InternetSecurityManager, 
  89.                                        NULL, CLSCTX_INPROC_SERVER,
  90.                                        IID_IInternetSecurityManager, 
  91.                                        (void **)&pSecMgr))) 
  92.         {
  93.             DWORD dwZoneID = URLZONE_UNTRUSTED;
  94.             if (SUCCEEDED(pSecMgr->MapUrlToZone(bstrPath, &dwZoneID, 0))) 
  95.             {
  96.                 if (dwZoneID == URLZONE_LOCAL_MACHINE)
  97.                     hr = S_OK;
  98.             }       
  99.             pSecMgr->Release();
  100.         }
  101.     } 
  102.     else 
  103.     {
  104.         hr = E_INVALIDARG;
  105.     }
  106.     return hr;
  107. }
  108. STDAPI LocalZoneCheck(IUnknown *punkSite)
  109. {
  110.     HRESULT hr = E_ACCESSDENIED;
  111.     IOleCommandTarget * pct;
  112.     BOOL fTriedBrowser = FALSE;
  113.     //  Return S_FALSE if we don't have a host site since we have no way of doing a 
  114.     //  security check.  This is as far as VB 5.0 apps get.
  115.     if (!punkSite)
  116.         return S_FALSE;
  117.     // Try to find the original template path for zone checking
  118.     if (SUCCEEDED(IUnknown_QueryService(punkSite, SID_DefView, IID_IOleCommandTarget, (void **)&pct)))
  119.     {
  120.         VARIANT vTemplatePath;
  121.         vTemplatePath.vt = VT_EMPTY;
  122.         if (pct->Exec(&CGID_DefView, DVCMDID_GETTEMPLATEDIRNAME, 0, NULL, &vTemplatePath) == S_OK)
  123.         {
  124.             fTriedBrowser = TRUE;
  125.             if (vTemplatePath.vt == VT_BSTR)
  126.                 hr = LocalZoneCheckPath(vTemplatePath.bstrVal);
  127.             // We were able to talk to the browser, so don't fall back on Trident because they may be
  128.             // less secure.
  129.             fTriedBrowser = TRUE;
  130.             VariantClear(&vTemplatePath);
  131.         }
  132.         pct->Release();
  133.     }
  134.     // If this is one of those cases where the browser doesn't exist (AOL, VB, ...) then
  135.     // we will check the scripts security.  If we did ask the browser, don't ask trident
  136.     // because the browser is often more restrictive in some cases.
  137.     if (!fTriedBrowser && (hr != S_OK))
  138.     {
  139.         // Try to use the URL from the document to zone check 
  140.         IHTMLDocument2 *pHtmlDoc;
  141.         if (SUCCEEDED(GetHTMLDoc2(punkSite, &pHtmlDoc)))
  142.         {
  143.             BSTR bstrPath;
  144.             if (SUCCEEDED(pHtmlDoc->get_URL(&bstrPath)))
  145.             {
  146.                 hr = LocalZoneCheckPath(bstrPath);
  147.                 SysFreeString(bstrPath);
  148.             }
  149.             pHtmlDoc->Release();
  150.         }
  151.     }
  152.                             
  153.     return hr;
  154. }