- Added IHttpNegotiate2 interface.
[wine] / dlls / urlmon / sec_mgr.c
1 /*
2  * Internet Security and Zone Manager
3  *
4  * Copyright (c) 2004 Huw D M Davies
5  * Copyright 2004 Jacek Caban
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #include <stdarg.h>
23 #include <stdio.h>
24
25 #define COBJMACROS
26
27 #include "windef.h"
28 #include "winbase.h"
29 #include "winuser.h"
30 #include "wine/debug.h"
31 #include "ole2.h"
32 #include "wine/unicode.h"
33 #include "urlmon.h"
34 #include "urlmon_main.h"
35
36 WINE_DEFAULT_DEBUG_CHANNEL(urlmon);
37
38 /***********************************************************************
39  *           InternetSecurityManager implementation
40  *
41  */
42 typedef struct {
43     const IInternetSecurityManagerVtbl* lpInternetSecurityManagerVtbl;
44
45     LONG ref;
46
47     IInternetSecurityMgrSite *mgrsite;
48     IInternetSecurityManager *custom_manager;
49 } SecManagerImpl;
50
51 #define SECMGR_THIS(iface) DEFINE_THIS(SecManagerImpl, InternetSecurityManager, iface)
52
53 static HRESULT WINAPI SecManagerImpl_QueryInterface(IInternetSecurityManager* iface,REFIID riid,void** ppvObject)
54 {
55     SecManagerImpl *This = SECMGR_THIS(iface);
56
57     TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppvObject);
58
59     /* Perform a sanity check on the parameters.*/
60     if ( (This==0) || (ppvObject==0) )
61         return E_INVALIDARG;
62
63     /* Initialize the return parameter */
64     *ppvObject = 0;
65
66     /* Compare the riid with the interface IDs implemented by this object.*/
67     if (IsEqualIID(&IID_IUnknown, riid) ||
68         IsEqualIID(&IID_IInternetSecurityManager, riid))
69         *ppvObject = iface;
70
71     /* Check that we obtained an interface.*/
72     if (!*ppvObject) {
73         WARN("not supported interface %s\n", debugstr_guid(riid));
74         return E_NOINTERFACE;
75     }
76
77     /* Query Interface always increases the reference count by one when it is successful */
78     IInternetSecurityManager_AddRef(iface);
79
80     return S_OK;
81 }
82
83 static ULONG WINAPI SecManagerImpl_AddRef(IInternetSecurityManager* iface)
84 {
85     SecManagerImpl *This = SECMGR_THIS(iface);
86     ULONG refCount = InterlockedIncrement(&This->ref);
87
88     TRACE("(%p) ref=%lu\n", This, refCount);
89
90     return refCount;
91 }
92
93 static ULONG WINAPI SecManagerImpl_Release(IInternetSecurityManager* iface)
94 {
95     SecManagerImpl *This = SECMGR_THIS(iface);
96     ULONG refCount = InterlockedDecrement(&This->ref);
97
98     TRACE("(%p) ref=%lu\n", This, refCount);
99
100     /* destroy the object if there's no more reference on it */
101     if (!refCount){
102         if(This->mgrsite)
103             IInternetSecurityMgrSite_Release(This->mgrsite);
104         if(This->custom_manager)
105             IInternetSecurityManager_Release(This->custom_manager);
106
107         HeapFree(GetProcessHeap(),0,This);
108
109         URLMON_UnlockModule();
110     }
111
112     return refCount;
113 }
114
115 static HRESULT WINAPI SecManagerImpl_SetSecuritySite(IInternetSecurityManager *iface,
116                                                      IInternetSecurityMgrSite *pSite)
117 {
118     SecManagerImpl *This = SECMGR_THIS(iface);
119
120     TRACE("(%p)->(%p)\n", This, pSite);
121
122     if(This->mgrsite)
123         IInternetSecurityMgrSite_Release(This->mgrsite);
124
125     if(This->custom_manager) {
126         IInternetSecurityManager_Release(This->custom_manager);
127         This->custom_manager = NULL;
128     }
129
130     This->mgrsite = pSite;
131
132     if(pSite) {
133         IServiceProvider *servprov;
134         HRESULT hres;
135
136         IInternetSecurityMgrSite_AddRef(pSite);
137
138         hres = IInternetSecurityMgrSite_QueryInterface(pSite, &IID_IServiceProvider,
139                 (void**)&servprov);
140         if(SUCCEEDED(hres)) {
141             IServiceProvider_QueryService(servprov, &SID_SInternetSecurityManager,
142                     &IID_IInternetSecurityManager, (void**)&This->custom_manager);
143             IServiceProvider_Release(servprov);
144         }
145     }
146
147     return S_OK;
148 }
149
150 static HRESULT WINAPI SecManagerImpl_GetSecuritySite(IInternetSecurityManager *iface,
151                                                      IInternetSecurityMgrSite **ppSite)
152 {
153     SecManagerImpl *This = SECMGR_THIS(iface);
154
155     TRACE("(%p)->(%p)\n", This, ppSite);
156
157     if(!ppSite)
158         return E_INVALIDARG;
159
160     if(This->mgrsite)
161         IInternetSecurityMgrSite_AddRef(This->mgrsite);
162
163     *ppSite = This->mgrsite;
164     return S_OK;
165 }
166
167 static HRESULT WINAPI SecManagerImpl_MapUrlToZone(IInternetSecurityManager *iface,
168                                                   LPCWSTR pwszUrl, DWORD *pdwZone,
169                                                   DWORD dwFlags)
170 {
171     SecManagerImpl *This = SECMGR_THIS(iface);
172     HRESULT hres;
173
174     TRACE("(%p)->(%s %p %08lx)\n", iface, debugstr_w(pwszUrl), pdwZone, dwFlags);
175
176     if(This->custom_manager) {
177         hres = IInternetSecurityManager_MapUrlToZone(This->custom_manager,
178                 pwszUrl, pdwZone, dwFlags);
179         if(hres != INET_E_DEFAULT_ACTION)
180             return hres;
181     }
182
183     FIXME("Default action is not implemented\n");
184     return E_NOTIMPL;
185 }
186
187 static HRESULT WINAPI SecManagerImpl_GetSecurityId(IInternetSecurityManager *iface, 
188                                                    LPCWSTR pwszUrl,
189                                                    BYTE *pbSecurityId, DWORD *pcbSecurityId,
190                                                    DWORD_PTR dwReserved)
191 {
192     SecManagerImpl *This = SECMGR_THIS(iface);
193     HRESULT hres;
194
195     TRACE("(%p)->(%s %p %p %08lx)\n", iface, debugstr_w(pwszUrl), pbSecurityId, pcbSecurityId,
196           dwReserved);
197
198     if(This->custom_manager) {
199         hres = IInternetSecurityManager_GetSecurityId(This->custom_manager,
200                 pwszUrl, pbSecurityId, pcbSecurityId, dwReserved);
201         if(hres != INET_E_DEFAULT_ACTION)
202             return hres;
203     }
204
205     FIXME("Default action is not implemented\n");
206     return E_NOTIMPL;
207 }
208
209
210 static HRESULT WINAPI SecManagerImpl_ProcessUrlAction(IInternetSecurityManager *iface,
211                                                       LPCWSTR pwszUrl, DWORD dwAction,
212                                                       BYTE *pPolicy, DWORD cbPolicy,
213                                                       BYTE *pContext, DWORD cbContext,
214                                                       DWORD dwFlags, DWORD dwReserved)
215 {
216     SecManagerImpl *This = SECMGR_THIS(iface);
217     HRESULT hres;
218
219     TRACE("(%p)->(%s %08lx %p %08lx %p %08lx %08lx %08lx)\n", iface, debugstr_w(pwszUrl), dwAction,
220           pPolicy, cbPolicy, pContext, cbContext, dwFlags, dwReserved);
221
222     if(This->custom_manager) {
223         hres = IInternetSecurityManager_ProcessUrlAction(This->custom_manager, pwszUrl, dwAction,
224                 pPolicy, cbPolicy, pContext, cbContext, dwFlags, dwReserved);
225         if(hres != INET_E_DEFAULT_ACTION)
226             return hres;
227     }
228
229     FIXME("Default action is not implemented\n");
230     return E_NOTIMPL;
231 }
232                                                
233
234 static HRESULT WINAPI SecManagerImpl_QueryCustomPolicy(IInternetSecurityManager *iface,
235                                                        LPCWSTR pwszUrl, REFGUID guidKey,
236                                                        BYTE **ppPolicy, DWORD *pcbPolicy,
237                                                        BYTE *pContext, DWORD cbContext,
238                                                        DWORD dwReserved)
239 {
240     SecManagerImpl *This = SECMGR_THIS(iface);
241     HRESULT hres;
242
243     TRACE("(%p)->(%s %s %p %p %p %08lx %08lx )\n", iface, debugstr_w(pwszUrl), debugstr_guid(guidKey),
244           ppPolicy, pcbPolicy, pContext, cbContext, dwReserved);
245
246     if(This->custom_manager) {
247         hres = IInternetSecurityManager_QueryCustomPolicy(This->custom_manager, pwszUrl, guidKey,
248                 ppPolicy, pcbPolicy, pContext, cbContext, dwReserved);
249         if(hres != INET_E_DEFAULT_ACTION)
250             return hres;
251     }
252
253     FIXME("Default action is not implemented\n");
254     return E_NOTIMPL;
255 }
256
257 static HRESULT WINAPI SecManagerImpl_SetZoneMapping(IInternetSecurityManager *iface,
258                                                     DWORD dwZone, LPCWSTR pwszPattern, DWORD dwFlags)
259 {
260     SecManagerImpl *This = SECMGR_THIS(iface);
261     HRESULT hres;
262
263     TRACE("(%p)->(%08lx %s %08lx)\n", iface, dwZone, debugstr_w(pwszPattern),dwFlags);
264
265     if(This->custom_manager) {
266         hres = IInternetSecurityManager_SetZoneMapping(This->custom_manager, dwZone,
267                 pwszPattern, dwFlags);
268         if(hres != INET_E_DEFAULT_ACTION)
269             return hres;
270     }
271
272     FIXME("Default action is not implemented\n");
273     return E_NOTIMPL;
274 }
275
276 static HRESULT WINAPI SecManagerImpl_GetZoneMappings(IInternetSecurityManager *iface,
277         DWORD dwZone, IEnumString **ppenumString, DWORD dwFlags)
278 {
279     SecManagerImpl *This = SECMGR_THIS(iface);
280     HRESULT hres;
281
282     TRACE("(%p)->(%08lx %p %08lx)\n", iface, dwZone, ppenumString,dwFlags);
283
284     if(This->custom_manager) {
285         hres = IInternetSecurityManager_GetZoneMappings(This->custom_manager, dwZone,
286                 ppenumString, dwFlags);
287         if(hres != INET_E_DEFAULT_ACTION)
288             return hres;
289     }
290
291     FIXME("Default action is not implemented\n");
292     return E_NOTIMPL;
293 }
294
295 static const IInternetSecurityManagerVtbl VT_SecManagerImpl =
296 {
297     SecManagerImpl_QueryInterface,
298     SecManagerImpl_AddRef,
299     SecManagerImpl_Release,
300     SecManagerImpl_SetSecuritySite,
301     SecManagerImpl_GetSecuritySite,
302     SecManagerImpl_MapUrlToZone,
303     SecManagerImpl_GetSecurityId,
304     SecManagerImpl_ProcessUrlAction,
305     SecManagerImpl_QueryCustomPolicy,
306     SecManagerImpl_SetZoneMapping,
307     SecManagerImpl_GetZoneMappings
308 };
309
310 HRESULT SecManagerImpl_Construct(IUnknown *pUnkOuter, LPVOID *ppobj)
311 {
312     SecManagerImpl *This;
313
314     TRACE("(%p,%p)\n",pUnkOuter,ppobj);
315     This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
316
317     /* Initialize the virtual function table. */
318     This->lpInternetSecurityManagerVtbl = &VT_SecManagerImpl;
319
320     This->ref = 1;
321     This->mgrsite = NULL;
322     This->custom_manager = NULL;
323
324     *ppobj = This;
325
326     URLMON_LockModule();
327
328     return S_OK;
329 }
330
331 /***********************************************************************
332  *           InternetZoneManager implementation
333  *
334  */
335 typedef struct {
336     const IInternetZoneManagerVtbl* lpVtbl;
337     LONG ref;
338 } ZoneMgrImpl;
339
340 /********************************************************************
341  *      IInternetZoneManager_QueryInterface
342  */
343 static HRESULT WINAPI ZoneMgrImpl_QueryInterface(IInternetZoneManager* iface, REFIID riid, void** ppvObject)
344 {
345     ZoneMgrImpl* This = (ZoneMgrImpl*)iface;
346
347     TRACE("(%p)->(%s,%p)\n", This, debugstr_guid(riid), ppvObject);
348
349     if(!This || !ppvObject)
350         return E_INVALIDARG;
351
352     if(!IsEqualIID(&IID_IUnknown, riid) && !IsEqualIID(&IID_IInternetZoneManager, riid)) {
353         FIXME("Unknown interface: %s\n", debugstr_guid(riid));
354         *ppvObject = NULL;
355         return E_NOINTERFACE;
356     }
357
358     *ppvObject = iface;
359     IInternetZoneManager_AddRef(iface);
360
361     return S_OK;
362 }
363
364 /********************************************************************
365  *      IInternetZoneManager_AddRef
366  */
367 static ULONG WINAPI ZoneMgrImpl_AddRef(IInternetZoneManager* iface)
368 {
369     ZoneMgrImpl* This = (ZoneMgrImpl*)iface;
370     ULONG refCount = InterlockedIncrement(&This->ref);
371
372     TRACE("(%p)->(ref before=%lu)\n",This, refCount - 1);
373
374     return refCount;
375 }
376
377 /********************************************************************
378  *      IInternetZoneManager_Release
379  */
380 static ULONG WINAPI ZoneMgrImpl_Release(IInternetZoneManager* iface)
381 {
382     ZoneMgrImpl* This = (ZoneMgrImpl*)iface;
383     ULONG refCount = InterlockedDecrement(&This->ref);
384
385     TRACE("(%p)->(ref before=%lu)\n",This, refCount + 1);
386
387     if(!refCount) {
388         HeapFree(GetProcessHeap(), 0, This);
389         URLMON_UnlockModule();
390     }
391     
392     return refCount;
393 }
394
395 /********************************************************************
396  *      IInternetZoneManager_GetZoneAttributes
397  */
398 static HRESULT WINAPI ZoneMgrImpl_GetZoneAttributes(IInternetZoneManager* iface,
399                                                     DWORD dwZone,
400                                                     ZONEATTRIBUTES* pZoneAttributes)
401 {
402     FIXME("(%p)->(%ld %p) stub\n", iface, dwZone, pZoneAttributes);
403     return E_NOTIMPL;
404 }
405
406 /********************************************************************
407  *      IInternetZoneManager_SetZoneAttributes
408  */
409 static HRESULT WINAPI ZoneMgrImpl_SetZoneAttributes(IInternetZoneManager* iface,
410                                                     DWORD dwZone,
411                                                     ZONEATTRIBUTES* pZoneAttributes)
412 {
413     FIXME("(%p)->(%08lx %p) stub\n", iface, dwZone, pZoneAttributes);
414     return E_NOTIMPL;
415 }
416
417 /********************************************************************
418  *      IInternetZoneManager_GetZoneCustomPolicy
419  */
420 static HRESULT WINAPI ZoneMgrImpl_GetZoneCustomPolicy(IInternetZoneManager* iface,
421                                                       DWORD dwZone,
422                                                       REFGUID guidKey,
423                                                       BYTE** ppPolicy,
424                                                       DWORD* pcbPolicy,
425                                                       URLZONEREG ulrZoneReg)
426 {
427     FIXME("(%p)->(%08lx %s %p %p %08x) stub\n", iface, dwZone, debugstr_guid(guidKey),
428                                                     ppPolicy, pcbPolicy, ulrZoneReg);
429     return E_NOTIMPL;
430 }
431
432 /********************************************************************
433  *      IInternetZoneManager_SetZoneCustomPolicy
434  */
435 static HRESULT WINAPI ZoneMgrImpl_SetZoneCustomPolicy(IInternetZoneManager* iface,
436                                                       DWORD dwZone,
437                                                       REFGUID guidKey,
438                                                       BYTE* ppPolicy,
439                                                       DWORD cbPolicy,
440                                                       URLZONEREG ulrZoneReg)
441 {
442     FIXME("(%p)->(%08lx %s %p %08lx %08x) stub\n", iface, dwZone, debugstr_guid(guidKey),
443                                                     ppPolicy, cbPolicy, ulrZoneReg);
444     return E_NOTIMPL;
445 }
446
447 /********************************************************************
448  *      IInternetZoneManager_GetZoneActionPolicy
449  */
450 static HRESULT WINAPI ZoneMgrImpl_GetZoneActionPolicy(IInternetZoneManager* iface,
451                                                       DWORD dwZone,
452                                                       DWORD dwAction,
453                                                       BYTE* pPolicy,
454                                                       DWORD cbPolicy,
455                                                       URLZONEREG urlZoneReg)
456 {
457     FIXME("(%p)->(%08lx %08lx %p %08lx %08x) stub\n", iface, dwZone, dwAction, pPolicy,
458                                                        cbPolicy, urlZoneReg);
459     return E_NOTIMPL;
460 }
461
462 /********************************************************************
463  *      IInternetZoneManager_SetZoneActionPolicy
464  */
465 static HRESULT WINAPI ZoneMgrImpl_SetZoneActionPolicy(IInternetZoneManager* iface,
466                                                       DWORD dwZone,
467                                                       DWORD dwAction,
468                                                       BYTE* pPolicy,
469                                                       DWORD cbPolicy,
470                                                       URLZONEREG urlZoneReg)
471 {
472     FIXME("(%p)->(%08lx %08lx %p %08lx %08x) stub\n", iface, dwZone, dwAction, pPolicy,
473                                                        cbPolicy, urlZoneReg);
474     return E_NOTIMPL;
475 }
476
477 /********************************************************************
478  *      IInternetZoneManager_PromptAction
479  */
480 static HRESULT WINAPI ZoneMgrImpl_PromptAction(IInternetZoneManager* iface,
481                                                DWORD dwAction,
482                                                HWND hwndParent,
483                                                LPCWSTR pwszUrl,
484                                                LPCWSTR pwszText,
485                                                DWORD dwPromptFlags)
486 {
487     FIXME("%p %08lx %p %s %s %08lx\n", iface, dwAction, hwndParent,
488           debugstr_w(pwszUrl), debugstr_w(pwszText), dwPromptFlags );
489     return E_NOTIMPL;
490 }
491
492 /********************************************************************
493  *      IInternetZoneManager_LogAction
494  */
495 static HRESULT WINAPI ZoneMgrImpl_LogAction(IInternetZoneManager* iface,
496                                             DWORD dwAction,
497                                             LPCWSTR pwszUrl,
498                                             LPCWSTR pwszText,
499                                             DWORD dwLogFlags)
500 {
501     FIXME("(%p)->(%08lx %s %s %08lx) stub\n", iface, dwAction, debugstr_w(pwszUrl),
502                                               debugstr_w(pwszText), dwLogFlags);
503     return E_NOTIMPL;
504 }
505
506 /********************************************************************
507  *      IInternetZoneManager_CreateZoneEnumerator
508  */
509 static HRESULT WINAPI ZoneMgrImpl_CreateZoneEnumerator(IInternetZoneManager* iface,
510                                                        DWORD* pdwEnum,
511                                                        DWORD* pdwCount,
512                                                        DWORD dwFlags)
513 {
514     FIXME("(%p)->(%p %p %08lx) stub\n", iface, pdwEnum, pdwCount, dwFlags);
515     return E_NOTIMPL;
516 }
517
518 /********************************************************************
519  *      IInternetZoneManager_GetZoneAt
520  */
521 static HRESULT WINAPI ZoneMgrImpl_GetZoneAt(IInternetZoneManager* iface,
522                                             DWORD dwEnum,
523                                             DWORD dwIndex,
524                                             DWORD* pdwZone)
525 {
526     FIXME("(%p)->(%08lx %08lx %p) stub\n", iface, dwEnum, dwIndex, pdwZone);
527     return E_NOTIMPL;
528 }
529
530 /********************************************************************
531  *      IInternetZoneManager_DestroyZoneEnumerator
532  */
533 static HRESULT WINAPI ZoneMgrImpl_DestroyZoneEnumerator(IInternetZoneManager* iface,
534                                                         DWORD dwEnum)
535 {
536     FIXME("(%p)->(%08lx) stub\n", iface, dwEnum);
537     return E_NOTIMPL;
538 }
539
540 /********************************************************************
541  *      IInternetZoneManager_CopyTemplatePoliciesToZone
542  */
543 static HRESULT WINAPI ZoneMgrImpl_CopyTemplatePoliciesToZone(IInternetZoneManager* iface,
544                                                              DWORD dwTemplate,
545                                                              DWORD dwZone,
546                                                              DWORD dwReserved)
547 {
548     FIXME("(%p)->(%08lx %08lx %08lx) stub\n", iface, dwTemplate, dwZone, dwReserved);
549     return E_NOTIMPL;
550 }
551
552 /********************************************************************
553  *      IInternetZoneManager_Construct
554  */
555 static const IInternetZoneManagerVtbl ZoneMgrImplVtbl = {
556     ZoneMgrImpl_QueryInterface,
557     ZoneMgrImpl_AddRef,
558     ZoneMgrImpl_Release,
559     ZoneMgrImpl_GetZoneAttributes,
560     ZoneMgrImpl_SetZoneAttributes,
561     ZoneMgrImpl_GetZoneCustomPolicy,
562     ZoneMgrImpl_SetZoneCustomPolicy,
563     ZoneMgrImpl_GetZoneActionPolicy,
564     ZoneMgrImpl_SetZoneActionPolicy,
565     ZoneMgrImpl_PromptAction,
566     ZoneMgrImpl_LogAction,
567     ZoneMgrImpl_CreateZoneEnumerator,
568     ZoneMgrImpl_GetZoneAt,
569     ZoneMgrImpl_DestroyZoneEnumerator,
570     ZoneMgrImpl_CopyTemplatePoliciesToZone,
571 };
572
573 HRESULT ZoneMgrImpl_Construct(IUnknown *pUnkOuter, LPVOID *ppobj)
574 {
575     ZoneMgrImpl* ret = HeapAlloc(GetProcessHeap(), 0, sizeof(ZoneMgrImpl));
576
577     TRACE("(%p %p)\n", pUnkOuter, ppobj);
578     ret->lpVtbl = &ZoneMgrImplVtbl;
579     ret->ref = 1;
580     *ppobj = (IInternetZoneManager*)ret;
581
582     URLMON_LockModule();
583
584     return S_OK;
585 }
586
587 /***********************************************************************
588  *           CoInternetCreateSecurityManager (URLMON.@)
589  *
590  */
591 HRESULT WINAPI CoInternetCreateSecurityManager( IServiceProvider *pSP,
592     IInternetSecurityManager **ppSM, DWORD dwReserved )
593 {
594     TRACE("%p %p %ld\n", pSP, ppSM, dwReserved );
595
596     if(pSP)
597         FIXME("pSP not supported\n");
598
599     return SecManagerImpl_Construct(NULL, (void**) ppSM);
600 }
601
602 /********************************************************************
603  *      CoInternetCreateZoneManager (URLMON.@)
604  */
605 HRESULT WINAPI CoInternetCreateZoneManager(IServiceProvider* pSP, IInternetZoneManager** ppZM, DWORD dwReserved)
606 {
607     TRACE("(%p %p %lx)\n", pSP, ppZM, dwReserved);
608     return ZoneMgrImpl_Construct(NULL, (void**)ppZM);
609 }