jscript: Added Date.setYear implementation.
[wine] / dlls / mshtml / secmgr.c
1 /*
2  * Copyright 2009 Jacek Caban for CodeWeavers
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18
19 #include "config.h"
20
21 #include <stdarg.h>
22 #include <stdio.h>
23
24 #define COBJMACROS
25
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winuser.h"
29 #include "ole2.h"
30 #include "objsafe.h"
31 #include "activscp.h"
32
33 #include "wine/debug.h"
34
35 #include "mshtml_private.h"
36
37 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
38
39 static const WCHAR about_blankW[] = {'a','b','o','u','t',':','b','l','a','n','k',0};
40
41 /* Defined as extern in urlmon.idl, but not exported by uuid.lib */
42 const GUID GUID_CUSTOM_CONFIRMOBJECTSAFETY =
43     {0x10200490,0xfa38,0x11d0,{0xac,0x0e,0x00,0xa0,0xc9,0xf,0xff,0xc0}};
44
45 #define HOSTSECMGR_THIS(iface) DEFINE_THIS(HTMLDocumentNode, IInternetHostSecurityManager, iface)
46
47 static HRESULT WINAPI InternetHostSecurityManager_QueryInterface(IInternetHostSecurityManager *iface, REFIID riid, void **ppv)
48 {
49     HTMLDocumentNode *This = HOSTSECMGR_THIS(iface);
50     return IHTMLDOMNode_QueryInterface(HTMLDOMNODE(&This->node), riid, ppv);
51 }
52
53 static ULONG WINAPI InternetHostSecurityManager_AddRef(IInternetHostSecurityManager *iface)
54 {
55     HTMLDocumentNode *This = HOSTSECMGR_THIS(iface);
56     return IHTMLDOMNode_AddRef(HTMLDOMNODE(&This->node));
57 }
58
59 static ULONG WINAPI InternetHostSecurityManager_Release(IInternetHostSecurityManager *iface)
60 {
61     HTMLDocumentNode *This = HOSTSECMGR_THIS(iface);
62     return IHTMLDOMNode_Release(HTMLDOMNODE(&This->node));
63 }
64
65 static HRESULT WINAPI InternetHostSecurityManager_GetSecurityId(IInternetHostSecurityManager *iface,  BYTE *pbSecurityId,
66         DWORD *pcbSecurityId, DWORD_PTR dwReserved)
67 {
68     HTMLDocumentNode *This = HOSTSECMGR_THIS(iface);
69     FIXME("(%p)->(%p %p %lx)\n", This, pbSecurityId, pcbSecurityId, dwReserved);
70     return E_NOTIMPL;
71 }
72
73 static HRESULT WINAPI InternetHostSecurityManager_ProcessUrlAction(IInternetHostSecurityManager *iface, DWORD dwAction,
74         BYTE *pPolicy, DWORD cbPolicy, BYTE *pContext, DWORD cbContext, DWORD dwFlags, DWORD dwReserved)
75 {
76     HTMLDocumentNode *This = HOSTSECMGR_THIS(iface);
77     const WCHAR *url;
78
79     TRACE("(%p)->(%d %p %d %p %d %x %x)\n", This, dwAction, pPolicy, cbPolicy, pContext, cbContext, dwFlags, dwReserved);
80
81     url = This->basedoc.window->url ? This->basedoc.window->url : about_blankW;
82
83     return IInternetSecurityManager_ProcessUrlAction(This->secmgr, url, dwAction, pPolicy, cbPolicy,
84             pContext, cbContext, dwFlags, dwReserved);
85 }
86
87 static HRESULT confirm_safety_load(HTMLDocumentNode *This, struct CONFIRMSAFETY *cs, DWORD *ret)
88 {
89     IObjectSafety *obj_safety;
90     HRESULT hres;
91
92     hres = IUnknown_QueryInterface(cs->pUnk, &IID_IObjectSafety, (void**)&obj_safety);
93     if(SUCCEEDED(hres)) {
94         hres = IObjectSafety_SetInterfaceSafetyOptions(obj_safety, &IID_IDispatch,
95                 INTERFACESAFE_FOR_UNTRUSTED_DATA, INTERFACESAFE_FOR_UNTRUSTED_DATA);
96         IObjectSafety_Release(obj_safety);
97         *ret = SUCCEEDED(hres) ? URLPOLICY_ALLOW : URLPOLICY_DISALLOW;
98     }else {
99         CATID init_catid = CATID_SafeForInitializing;
100
101         hres = ICatInformation_IsClassOfCategories(This->catmgr, &cs->clsid, 1, &init_catid, 0, NULL);
102         if(FAILED(hres))
103             return hres;
104
105         *ret = hres == S_OK ? URLPOLICY_ALLOW : URLPOLICY_DISALLOW;
106     }
107
108     return S_OK;
109 }
110
111 static HRESULT confirm_safety(HTMLDocumentNode *This, const WCHAR *url, struct CONFIRMSAFETY *cs, DWORD *ret)
112 {
113     DWORD policy, enabled_opts, supported_opts;
114     IObjectSafety *obj_safety;
115     HRESULT hres;
116
117     TRACE("%s %p %s\n", debugstr_w(url), cs->pUnk, debugstr_guid(&cs->clsid));
118
119     /* FIXME: Check URLACTION_ACTIVEX_OVERRIDE_SCRIPT_SAFETY */
120
121     hres = IInternetSecurityManager_ProcessUrlAction(This->secmgr, url, URLACTION_SCRIPT_SAFE_ACTIVEX,
122             (BYTE*)&policy, sizeof(policy), NULL, 0, 0, 0);
123     if(FAILED(hres) || policy != URLPOLICY_ALLOW) {
124         *ret = URLPOLICY_DISALLOW;
125         return S_OK;
126     }
127
128     hres = IUnknown_QueryInterface(cs->pUnk, &IID_IObjectSafety, (void**)&obj_safety);
129     if(SUCCEEDED(hres)) {
130         hres = IObjectSafety_GetInterfaceSafetyOptions(obj_safety, &IID_IDispatchEx, &supported_opts, &enabled_opts);
131         if(FAILED(hres))
132             supported_opts = 0;
133
134         enabled_opts = INTERFACESAFE_FOR_UNTRUSTED_CALLER;
135         if(supported_opts & INTERFACE_USES_SECURITY_MANAGER)
136             enabled_opts |= INTERFACE_USES_SECURITY_MANAGER;
137
138         hres = IObjectSafety_SetInterfaceSafetyOptions(obj_safety, &IID_IDispatchEx, enabled_opts, enabled_opts);
139         if(FAILED(hres)) {
140             enabled_opts &= ~INTERFACE_USES_SECURITY_MANAGER;
141             hres = IObjectSafety_SetInterfaceSafetyOptions(obj_safety, &IID_IDispatch, enabled_opts, enabled_opts);
142         }
143         IObjectSafety_Release(obj_safety);
144
145         if(FAILED(hres)) {
146             *ret = URLPOLICY_DISALLOW;
147             return S_OK;
148         }
149     }else {
150         CATID scripting_catid = CATID_SafeForScripting;
151
152         if(!This->catmgr) {
153             hres = CoCreateInstance(&CLSID_StdComponentCategoriesMgr, NULL, CLSCTX_INPROC_SERVER,
154                     &IID_ICatInformation, (void**)&This->catmgr);
155             if(FAILED(hres))
156                 return hres;
157         }
158
159         hres = ICatInformation_IsClassOfCategories(This->catmgr, &cs->clsid, 1, &scripting_catid, 0, NULL);
160         if(FAILED(hres))
161             return hres;
162
163         if(hres != S_OK) {
164             *ret = URLPOLICY_DISALLOW;
165             return S_OK;
166         }
167     }
168
169     if(cs->dwFlags & CONFIRMSAFETYACTION_LOADOBJECT)
170         return confirm_safety_load(This, cs, ret);
171
172     *ret = URLPOLICY_ALLOW;
173     return S_OK;
174 }
175
176 static HRESULT WINAPI InternetHostSecurityManager_QueryCustomPolicy(IInternetHostSecurityManager *iface, REFGUID guidKey,
177         BYTE **ppPolicy, DWORD *pcbPolicy, BYTE *pContext, DWORD cbContext, DWORD dwReserved)
178 {
179     HTMLDocumentNode *This = HOSTSECMGR_THIS(iface);
180     const WCHAR *url;
181     HRESULT hres;
182
183     TRACE("(%p)->(%s %p %p %p %d %x)\n", This, debugstr_guid(guidKey), ppPolicy, pcbPolicy, pContext, cbContext, dwReserved);
184
185     url = This->basedoc.window->url ? This->basedoc.window->url : about_blankW;
186
187     hres = IInternetSecurityManager_QueryCustomPolicy(This->secmgr, url, guidKey, ppPolicy, pcbPolicy,
188             pContext, cbContext, dwReserved);
189     if(hres != HRESULT_FROM_WIN32(ERROR_NOT_FOUND))
190         return hres;
191
192     if(IsEqualGUID(&GUID_CUSTOM_CONFIRMOBJECTSAFETY, guidKey)) {
193         IActiveScript *active_script;
194         struct CONFIRMSAFETY *cs;
195         DWORD policy;
196
197         if(cbContext != sizeof(struct CONFIRMSAFETY)) {
198             FIXME("wrong context size\n");
199             return E_FAIL;
200         }
201
202         cs = (struct CONFIRMSAFETY*)pContext;
203         TRACE("cs = {%s %p %x}\n", debugstr_guid(&cs->clsid), cs->pUnk, cs->dwFlags);
204
205         hres = IUnknown_QueryInterface(cs->pUnk, &IID_IActiveScript, (void**)&active_script);
206         if(SUCCEEDED(hres)) {
207             FIXME("Got IAciveScript iface\n");
208             IActiveScript_Release(active_script);
209             return E_FAIL;
210         }
211
212         hres = confirm_safety(This, url, cs, &policy);
213         if(FAILED(hres))
214             return hres;
215
216         *ppPolicy = CoTaskMemAlloc(sizeof(policy));
217         if(!*ppPolicy)
218             return E_OUTOFMEMORY;
219
220         *(DWORD*)*ppPolicy = policy;
221         *pcbPolicy = sizeof(policy);
222         TRACE("policy %x\n", policy);
223         return S_OK;
224     }
225
226     FIXME("Unknown guidKey %s\n", debugstr_guid(guidKey));
227     return hres;
228 }
229
230 #undef HOSTSECMGR_THIS
231
232 static const IInternetHostSecurityManagerVtbl InternetHostSecurityManagerVtbl = {
233     InternetHostSecurityManager_QueryInterface,
234     InternetHostSecurityManager_AddRef,
235     InternetHostSecurityManager_Release,
236     InternetHostSecurityManager_GetSecurityId,
237     InternetHostSecurityManager_ProcessUrlAction,
238     InternetHostSecurityManager_QueryCustomPolicy
239 };
240
241 void HTMLDocumentNode_SecMgr_Init(HTMLDocumentNode *This)
242 {
243     This->lpIInternetHostSecurityManagerVtbl = &InternetHostSecurityManagerVtbl;
244 }