oleaut32/tests: Fix test failures and crash on Win9x/WinME.
[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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #include <stdio.h>
23
24 #include "urlmon_main.h"
25 #include "winreg.h"
26 #include "wininet.h"
27
28 #include "wine/debug.h"
29
30 WINE_DEFAULT_DEBUG_CHANNEL(urlmon);
31
32 static const WCHAR currentlevelW[] = {'C','u','r','r','e','n','t','L','e','v','e','l',0};
33 static const WCHAR descriptionW[] = {'D','e','s','c','r','i','p','t','i','o','n',0};
34 static const WCHAR displaynameW[] = {'D','i','s','p','l','a','y','N','a','m','e',0};
35 static const WCHAR fileW[] = {'f','i','l','e',0};
36 static const WCHAR flagsW[] = {'F','l','a','g','s',0};
37 static const WCHAR iconW[] = {'I','c','o','n',0};
38 static const WCHAR minlevelW[] = {'M','i','n','L','e','v','e','l',0};
39 static const WCHAR recommendedlevelW[] = {'R','e','c','o','m','m','e','n','d','e','d',
40                                           'L','e','v','e','l',0};
41
42 /********************************************************************
43  * get_string_from_reg [internal]
44  *
45  * helper to get a string from the reg.
46  *
47  */
48 static void get_string_from_reg(HKEY hcu, HKEY hklm, LPCWSTR name, LPWSTR out, DWORD maxlen)
49 {
50     DWORD type = REG_SZ;
51     DWORD len = maxlen * sizeof(WCHAR);
52     DWORD res;
53
54     res = RegQueryValueExW(hcu, name, NULL, &type, (LPBYTE) out, &len);
55
56     if (res && hklm) {
57         len = maxlen * sizeof(WCHAR);
58         type = REG_SZ;
59         res = RegQueryValueExW(hklm, name, NULL, &type, (LPBYTE) out, &len);
60     }
61
62     if (res) {
63         TRACE("%s failed: %d\n", debugstr_w(name), res);
64         *out = '\0';
65     }
66 }
67
68 /********************************************************************
69  * get_dword_from_reg [internal]
70  *
71  * helper to get a dword from the reg.
72  *
73  */
74 static void get_dword_from_reg(HKEY hcu, HKEY hklm, LPCWSTR name, LPDWORD out)
75 {
76     DWORD type = REG_DWORD;
77     DWORD len = sizeof(DWORD);
78     DWORD res;
79
80     res = RegQueryValueExW(hcu, name, NULL, &type, (LPBYTE) out, &len);
81
82     if (res && hklm) {
83         len = sizeof(DWORD);
84         type = REG_DWORD;
85         res = RegQueryValueExW(hklm, name, NULL, &type, (LPBYTE) out, &len);
86     }
87
88     if (res) {
89         TRACE("%s failed: %d\n", debugstr_w(name), res);
90         *out = 0;
91     }
92 }
93
94 static HRESULT get_zone_from_reg(LPCWSTR schema, DWORD *zone)
95 {
96     DWORD res, size;
97     HKEY hkey;
98
99     static const WCHAR wszZoneMapProtocolKey[] =
100         {'S','o','f','t','w','a','r','e','\\',
101          'M','i','c','r','o','s','o','f','t','\\',
102          'W','i','n','d','o','w','s','\\',
103          'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
104          'I','n','t','e','r','n','e','t',' ','S','e','t','t','i','n','g','s','\\',
105          'Z','o','n','e','M','a','p','\\',
106          'P','r','o','t','o','c','o','l','D','e','f','a','u','l','t','s',0};
107
108     res = RegOpenKeyW(HKEY_CURRENT_USER, wszZoneMapProtocolKey, &hkey);
109     if(res != ERROR_SUCCESS) {
110         ERR("Could not open key %s\n", debugstr_w(wszZoneMapProtocolKey));
111         return E_UNEXPECTED;
112     }
113
114     size = sizeof(DWORD);
115     res = RegQueryValueExW(hkey, schema, NULL, NULL, (PBYTE)zone, &size);
116     RegCloseKey(hkey);
117     if(res == ERROR_SUCCESS)
118         return S_OK;
119
120     res = RegOpenKeyW(HKEY_LOCAL_MACHINE, wszZoneMapProtocolKey, &hkey);
121     if(res != ERROR_SUCCESS) {
122         ERR("Could not open key %s\n", debugstr_w(wszZoneMapProtocolKey));
123         return E_UNEXPECTED;
124     }
125
126     size = sizeof(DWORD);
127     res = RegQueryValueExW(hkey, schema, NULL, NULL, (PBYTE)zone, &size);
128     RegCloseKey(hkey);
129     if(res == ERROR_SUCCESS)
130         return S_OK;
131
132     *zone = 3;
133     return S_OK;
134 }
135
136 static HRESULT map_url_to_zone(LPCWSTR url, DWORD *zone, LPWSTR *ret_url)
137 {
138     LPWSTR secur_url;
139     WCHAR schema[64];
140     DWORD size=0;
141     HRESULT hres;
142
143     secur_url = heap_alloc(INTERNET_MAX_URL_LENGTH*sizeof(WCHAR));
144     *zone = -1;
145
146     hres = CoInternetParseUrl(url, PARSE_SECURITY_URL, 0, secur_url, INTERNET_MAX_URL_LENGTH, &size, 0);
147     if(hres != S_OK)
148         strcpyW(secur_url, url);
149
150     hres = CoInternetParseUrl(secur_url, PARSE_SCHEMA, 0, schema, sizeof(schema)/sizeof(WCHAR), &size, 0);
151     if(FAILED(hres) || !*schema) {
152         heap_free(secur_url);
153         return E_INVALIDARG;
154     }
155
156     /* file protocol is a special case */
157     if(!strcmpW(schema, fileW)) {
158         WCHAR path[MAX_PATH], root[20];
159         WCHAR *ptr;
160
161         hres = CoInternetParseUrl(secur_url, PARSE_PATH_FROM_URL, 0, path,
162                 sizeof(path)/sizeof(WCHAR), &size, 0);
163
164         if(SUCCEEDED(hres) && (ptr = strchrW(path, '\\')) && ptr-path < sizeof(root)/sizeof(WCHAR)) {
165             UINT type;
166
167             memcpy(root, path, (ptr-path)*sizeof(WCHAR));
168             root[ptr-path] = 0;
169
170             type = GetDriveTypeW(root);
171
172             switch(type) {
173             case DRIVE_UNKNOWN:
174             case DRIVE_NO_ROOT_DIR:
175                 break;
176             case DRIVE_REMOVABLE:
177             case DRIVE_FIXED:
178             case DRIVE_CDROM:
179             case DRIVE_RAMDISK:
180                 *zone = 0;
181                 hres = S_OK;
182                 break;
183             case DRIVE_REMOTE:
184                 *zone = 3;
185                 hres = S_OK;
186                 break;
187             default:
188                 FIXME("unsupported drive type %d\n", type);
189             }
190         }
191     }
192
193     if(*zone == -1) {
194         WARN("domains are not yet implemented\n");
195         hres = get_zone_from_reg(schema, zone);
196     }
197
198     if(FAILED(hres) || !ret_url)
199         heap_free(secur_url);
200     else
201         *ret_url = secur_url;
202
203     return hres;
204 }
205
206 static HRESULT open_zone_key(HKEY parent_key, DWORD zone, HKEY *hkey)
207 {
208     static const WCHAR wszZonesKey[] =
209         {'S','o','f','t','w','a','r','e','\\',
210          'M','i','c','r','o','s','o','f','t','\\',
211          'W','i','n','d','o','w','s','\\',
212          'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
213          'I','n','t','e','r','n','e','t',' ','S','e','t','t','i','n','g','s','\\',
214          'Z','o','n','e','s','\\',0};
215     static const WCHAR wszFormat[] = {'%','s','%','l','d',0};
216
217     WCHAR key_name[sizeof(wszZonesKey)/sizeof(WCHAR)+8];
218     DWORD res;
219
220     wsprintfW(key_name, wszFormat, wszZonesKey, zone);
221
222     res = RegOpenKeyW(parent_key, key_name, hkey);
223
224     if(res != ERROR_SUCCESS) {
225         WARN("RegOpenKey failed\n");
226         return E_INVALIDARG;
227     }
228
229     return S_OK;
230 }
231
232 static HRESULT get_action_policy(DWORD zone, DWORD action, BYTE *policy, DWORD size, URLZONEREG zone_reg)
233 {
234     HKEY parent_key;
235     HKEY hkey;
236     LONG res;
237     HRESULT hres;
238
239     switch(action) {
240     case URLACTION_SCRIPT_OVERRIDE_SAFETY:
241         *(DWORD*)policy = URLPOLICY_DISALLOW;
242         return S_OK;
243     }
244
245     switch(zone_reg) {
246     case URLZONEREG_DEFAULT:
247     case URLZONEREG_HKCU:
248         parent_key = HKEY_CURRENT_USER;
249         break;
250     case URLZONEREG_HKLM:
251         parent_key = HKEY_LOCAL_MACHINE;
252         break;
253     default:
254         WARN("Unknown URLZONEREG: %d\n", zone_reg);
255         return E_FAIL;
256     };
257
258     hres = open_zone_key(parent_key, zone, &hkey);
259     if(SUCCEEDED(hres)) {
260         WCHAR action_str[16];
261         DWORD len = size;
262
263         static const WCHAR formatW[] = {'%','X',0};
264
265         wsprintfW(action_str, formatW, action);
266
267         res = RegQueryValueExW(hkey, action_str, NULL, NULL, policy, &len);
268         if(res == ERROR_MORE_DATA) {
269             hres = E_INVALIDARG;
270         }else if(res == ERROR_FILE_NOT_FOUND) {
271             hres = E_FAIL;
272         }else if(res != ERROR_SUCCESS) {
273             ERR("RegQueryValue failed: %d\n", res);
274             hres = E_UNEXPECTED;
275         }
276
277         RegCloseKey(hkey);
278     }
279
280     if(FAILED(hres) && zone_reg == URLZONEREG_DEFAULT)
281         return get_action_policy(zone, action, policy, size, URLZONEREG_HKLM);
282
283     return hres;
284 }
285
286 /***********************************************************************
287  *           InternetSecurityManager implementation
288  *
289  */
290 typedef struct {
291     const IInternetSecurityManagerVtbl* lpInternetSecurityManagerVtbl;
292
293     LONG ref;
294
295     IInternetSecurityMgrSite *mgrsite;
296     IInternetSecurityManager *custom_manager;
297 } SecManagerImpl;
298
299 #define SECMGR_THIS(iface) DEFINE_THIS(SecManagerImpl, InternetSecurityManager, iface)
300
301 static HRESULT WINAPI SecManagerImpl_QueryInterface(IInternetSecurityManager* iface,REFIID riid,void** ppvObject)
302 {
303     SecManagerImpl *This = SECMGR_THIS(iface);
304
305     TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppvObject);
306
307     /* Perform a sanity check on the parameters.*/
308     if ( (This==0) || (ppvObject==0) )
309         return E_INVALIDARG;
310
311     /* Initialize the return parameter */
312     *ppvObject = 0;
313
314     /* Compare the riid with the interface IDs implemented by this object.*/
315     if (IsEqualIID(&IID_IUnknown, riid) ||
316         IsEqualIID(&IID_IInternetSecurityManager, riid))
317         *ppvObject = iface;
318
319     /* Check that we obtained an interface.*/
320     if (!*ppvObject) {
321         WARN("not supported interface %s\n", debugstr_guid(riid));
322         return E_NOINTERFACE;
323     }
324
325     /* Query Interface always increases the reference count by one when it is successful */
326     IInternetSecurityManager_AddRef(iface);
327
328     return S_OK;
329 }
330
331 static ULONG WINAPI SecManagerImpl_AddRef(IInternetSecurityManager* iface)
332 {
333     SecManagerImpl *This = SECMGR_THIS(iface);
334     ULONG refCount = InterlockedIncrement(&This->ref);
335
336     TRACE("(%p) ref=%u\n", This, refCount);
337
338     return refCount;
339 }
340
341 static ULONG WINAPI SecManagerImpl_Release(IInternetSecurityManager* iface)
342 {
343     SecManagerImpl *This = SECMGR_THIS(iface);
344     ULONG refCount = InterlockedDecrement(&This->ref);
345
346     TRACE("(%p) ref=%u\n", This, refCount);
347
348     /* destroy the object if there's no more reference on it */
349     if (!refCount){
350         if(This->mgrsite)
351             IInternetSecurityMgrSite_Release(This->mgrsite);
352         if(This->custom_manager)
353             IInternetSecurityManager_Release(This->custom_manager);
354
355         heap_free(This);
356
357         URLMON_UnlockModule();
358     }
359
360     return refCount;
361 }
362
363 static HRESULT WINAPI SecManagerImpl_SetSecuritySite(IInternetSecurityManager *iface,
364                                                      IInternetSecurityMgrSite *pSite)
365 {
366     SecManagerImpl *This = SECMGR_THIS(iface);
367
368     TRACE("(%p)->(%p)\n", This, pSite);
369
370     if(This->mgrsite)
371         IInternetSecurityMgrSite_Release(This->mgrsite);
372
373     if(This->custom_manager) {
374         IInternetSecurityManager_Release(This->custom_manager);
375         This->custom_manager = NULL;
376     }
377
378     This->mgrsite = pSite;
379
380     if(pSite) {
381         IServiceProvider *servprov;
382         HRESULT hres;
383
384         IInternetSecurityMgrSite_AddRef(pSite);
385
386         hres = IInternetSecurityMgrSite_QueryInterface(pSite, &IID_IServiceProvider,
387                 (void**)&servprov);
388         if(SUCCEEDED(hres)) {
389             IServiceProvider_QueryService(servprov, &SID_SInternetSecurityManager,
390                     &IID_IInternetSecurityManager, (void**)&This->custom_manager);
391             IServiceProvider_Release(servprov);
392         }
393     }
394
395     return S_OK;
396 }
397
398 static HRESULT WINAPI SecManagerImpl_GetSecuritySite(IInternetSecurityManager *iface,
399                                                      IInternetSecurityMgrSite **ppSite)
400 {
401     SecManagerImpl *This = SECMGR_THIS(iface);
402
403     TRACE("(%p)->(%p)\n", This, ppSite);
404
405     if(!ppSite)
406         return E_INVALIDARG;
407
408     if(This->mgrsite)
409         IInternetSecurityMgrSite_AddRef(This->mgrsite);
410
411     *ppSite = This->mgrsite;
412     return S_OK;
413 }
414
415 static HRESULT WINAPI SecManagerImpl_MapUrlToZone(IInternetSecurityManager *iface,
416                                                   LPCWSTR pwszUrl, DWORD *pdwZone,
417                                                   DWORD dwFlags)
418 {
419     SecManagerImpl *This = SECMGR_THIS(iface);
420     HRESULT hres;
421
422     TRACE("(%p)->(%s %p %08x)\n", iface, debugstr_w(pwszUrl), pdwZone, dwFlags);
423
424     if(This->custom_manager) {
425         hres = IInternetSecurityManager_MapUrlToZone(This->custom_manager,
426                 pwszUrl, pdwZone, dwFlags);
427         if(hres != INET_E_DEFAULT_ACTION)
428             return hres;
429     }
430
431     if(!pwszUrl) {
432         *pdwZone = -1;
433         return E_INVALIDARG;
434     }
435
436     if(dwFlags)
437         FIXME("not supported flags: %08x\n", dwFlags);
438
439     return map_url_to_zone(pwszUrl, pdwZone, NULL);
440 }
441
442 static HRESULT WINAPI SecManagerImpl_GetSecurityId(IInternetSecurityManager *iface, 
443         LPCWSTR pwszUrl, BYTE *pbSecurityId, DWORD *pcbSecurityId, DWORD_PTR dwReserved)
444 {
445     SecManagerImpl *This = SECMGR_THIS(iface);
446     LPWSTR url, ptr, ptr2;
447     DWORD zone, len;
448     HRESULT hres;
449
450     static const WCHAR wszFile[] = {'f','i','l','e',':'};
451
452     TRACE("(%p)->(%s %p %p %08lx)\n", iface, debugstr_w(pwszUrl), pbSecurityId,
453           pcbSecurityId, dwReserved);
454
455     if(This->custom_manager) {
456         hres = IInternetSecurityManager_GetSecurityId(This->custom_manager,
457                 pwszUrl, pbSecurityId, pcbSecurityId, dwReserved);
458         if(hres != INET_E_DEFAULT_ACTION)
459             return hres;
460     }
461
462     if(!pwszUrl || !pbSecurityId || !pcbSecurityId)
463         return E_INVALIDARG;
464
465     if(dwReserved)
466         FIXME("dwReserved is not supported\n");
467
468     hres = map_url_to_zone(pwszUrl, &zone, &url);
469     if(FAILED(hres))
470         return hres == 0x80041001 ? E_INVALIDARG : hres;
471
472     /* file protocol is a special case */
473     if(strlenW(url) >= sizeof(wszFile)/sizeof(WCHAR)
474             && !memcmp(url, wszFile, sizeof(wszFile)) && strchrW(url, '\\')) {
475
476         static const BYTE secidFile[] = {'f','i','l','e',':'};
477
478         heap_free(url);
479
480         if(*pcbSecurityId < sizeof(secidFile)+sizeof(zone))
481             return HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
482
483         memcpy(pbSecurityId, secidFile, sizeof(secidFile));
484         *(DWORD*)(pbSecurityId+sizeof(secidFile)) = zone;
485
486         *pcbSecurityId = sizeof(secidFile)+sizeof(zone);
487         return S_OK;
488     }
489
490     ptr = strchrW(url, ':');
491     ptr2 = ++ptr;
492     while(*ptr2 == '/')
493         ptr2++;
494     if(ptr2 != ptr)
495         memmove(ptr, ptr2, (strlenW(ptr2)+1)*sizeof(WCHAR));
496
497     ptr = strchrW(ptr, '/');
498     if(ptr)
499         *ptr = 0;
500
501     len = WideCharToMultiByte(CP_ACP, 0, url, -1, NULL, 0, NULL, NULL)-1;
502
503     if(len+sizeof(DWORD) > *pcbSecurityId) {
504         heap_free(url);
505         return HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
506     }
507
508     WideCharToMultiByte(CP_ACP, 0, url, -1, (LPSTR)pbSecurityId, len, NULL, NULL);
509     heap_free(url);
510
511     *(DWORD*)(pbSecurityId+len) = zone;
512
513     *pcbSecurityId = len+sizeof(DWORD);
514
515     return S_OK;
516 }
517
518
519 static HRESULT WINAPI SecManagerImpl_ProcessUrlAction(IInternetSecurityManager *iface,
520                                                       LPCWSTR pwszUrl, DWORD dwAction,
521                                                       BYTE *pPolicy, DWORD cbPolicy,
522                                                       BYTE *pContext, DWORD cbContext,
523                                                       DWORD dwFlags, DWORD dwReserved)
524 {
525     SecManagerImpl *This = SECMGR_THIS(iface);
526     DWORD zone, policy;
527     HRESULT hres;
528
529     TRACE("(%p)->(%s %08x %p %08x %p %08x %08x %08x)\n", iface, debugstr_w(pwszUrl), dwAction,
530           pPolicy, cbPolicy, pContext, cbContext, dwFlags, dwReserved);
531
532     if(This->custom_manager) {
533         hres = IInternetSecurityManager_ProcessUrlAction(This->custom_manager, pwszUrl, dwAction,
534                 pPolicy, cbPolicy, pContext, cbContext, dwFlags, dwReserved);
535         if(hres != INET_E_DEFAULT_ACTION)
536             return hres;
537     }
538
539     if(pContext || cbContext || dwFlags || dwReserved)
540         FIXME("Unsupported arguments\n");
541
542     if(!pwszUrl)
543         return E_INVALIDARG;
544
545     hres = map_url_to_zone(pwszUrl, &zone, NULL);
546     if(FAILED(hres))
547         return hres;
548
549     hres = get_action_policy(zone, dwAction, (BYTE*)&policy, sizeof(policy), URLZONEREG_DEFAULT);
550     if(FAILED(hres))
551         return hres;
552
553     TRACE("policy %x\n", policy);
554
555     switch(GetUrlPolicyPermissions(policy)) {
556     case URLPOLICY_ALLOW:
557     case URLPOLICY_CHANNEL_SOFTDIST_PRECACHE:
558         return S_OK;
559     case URLPOLICY_DISALLOW:
560         return S_FALSE;
561     case URLPOLICY_QUERY:
562         FIXME("URLPOLICY_QUERY not implemented\n");
563         return E_FAIL;
564     default:
565         FIXME("Not implemented policy %x\n", policy);
566     }
567
568     return E_FAIL;
569 }
570                                                
571
572 static HRESULT WINAPI SecManagerImpl_QueryCustomPolicy(IInternetSecurityManager *iface,
573                                                        LPCWSTR pwszUrl, REFGUID guidKey,
574                                                        BYTE **ppPolicy, DWORD *pcbPolicy,
575                                                        BYTE *pContext, DWORD cbContext,
576                                                        DWORD dwReserved)
577 {
578     SecManagerImpl *This = SECMGR_THIS(iface);
579     HRESULT hres;
580
581     TRACE("(%p)->(%s %s %p %p %p %08x %08x )\n", iface, debugstr_w(pwszUrl), debugstr_guid(guidKey),
582           ppPolicy, pcbPolicy, pContext, cbContext, dwReserved);
583
584     if(This->custom_manager) {
585         hres = IInternetSecurityManager_QueryCustomPolicy(This->custom_manager, pwszUrl, guidKey,
586                 ppPolicy, pcbPolicy, pContext, cbContext, dwReserved);
587         if(hres != INET_E_DEFAULT_ACTION)
588             return hres;
589     }
590
591     FIXME("Default action is not implemented\n");
592     return E_NOTIMPL;
593 }
594
595 static HRESULT WINAPI SecManagerImpl_SetZoneMapping(IInternetSecurityManager *iface,
596                                                     DWORD dwZone, LPCWSTR pwszPattern, DWORD dwFlags)
597 {
598     SecManagerImpl *This = SECMGR_THIS(iface);
599     HRESULT hres;
600
601     TRACE("(%p)->(%08x %s %08x)\n", iface, dwZone, debugstr_w(pwszPattern),dwFlags);
602
603     if(This->custom_manager) {
604         hres = IInternetSecurityManager_SetZoneMapping(This->custom_manager, dwZone,
605                 pwszPattern, dwFlags);
606         if(hres != INET_E_DEFAULT_ACTION)
607             return hres;
608     }
609
610     FIXME("Default action is not implemented\n");
611     return E_NOTIMPL;
612 }
613
614 static HRESULT WINAPI SecManagerImpl_GetZoneMappings(IInternetSecurityManager *iface,
615         DWORD dwZone, IEnumString **ppenumString, DWORD dwFlags)
616 {
617     SecManagerImpl *This = SECMGR_THIS(iface);
618     HRESULT hres;
619
620     TRACE("(%p)->(%08x %p %08x)\n", iface, dwZone, ppenumString,dwFlags);
621
622     if(This->custom_manager) {
623         hres = IInternetSecurityManager_GetZoneMappings(This->custom_manager, dwZone,
624                 ppenumString, dwFlags);
625         if(hres != INET_E_DEFAULT_ACTION)
626             return hres;
627     }
628
629     FIXME("Default action is not implemented\n");
630     return E_NOTIMPL;
631 }
632
633 static const IInternetSecurityManagerVtbl VT_SecManagerImpl =
634 {
635     SecManagerImpl_QueryInterface,
636     SecManagerImpl_AddRef,
637     SecManagerImpl_Release,
638     SecManagerImpl_SetSecuritySite,
639     SecManagerImpl_GetSecuritySite,
640     SecManagerImpl_MapUrlToZone,
641     SecManagerImpl_GetSecurityId,
642     SecManagerImpl_ProcessUrlAction,
643     SecManagerImpl_QueryCustomPolicy,
644     SecManagerImpl_SetZoneMapping,
645     SecManagerImpl_GetZoneMappings
646 };
647
648 HRESULT SecManagerImpl_Construct(IUnknown *pUnkOuter, LPVOID *ppobj)
649 {
650     SecManagerImpl *This;
651
652     TRACE("(%p,%p)\n",pUnkOuter,ppobj);
653     This = heap_alloc(sizeof(*This));
654
655     /* Initialize the virtual function table. */
656     This->lpInternetSecurityManagerVtbl = &VT_SecManagerImpl;
657
658     This->ref = 1;
659     This->mgrsite = NULL;
660     This->custom_manager = NULL;
661
662     *ppobj = This;
663
664     URLMON_LockModule();
665
666     return S_OK;
667 }
668
669 /***********************************************************************
670  *           InternetZoneManager implementation
671  *
672  */
673 typedef struct {
674     const IInternetZoneManagerVtbl* lpVtbl;
675     LONG ref;
676 } ZoneMgrImpl;
677
678 /********************************************************************
679  *      IInternetZoneManager_QueryInterface
680  */
681 static HRESULT WINAPI ZoneMgrImpl_QueryInterface(IInternetZoneManager* iface, REFIID riid, void** ppvObject)
682 {
683     ZoneMgrImpl* This = (ZoneMgrImpl*)iface;
684
685     TRACE("(%p)->(%s,%p)\n", This, debugstr_guid(riid), ppvObject);
686
687     if(!This || !ppvObject)
688         return E_INVALIDARG;
689
690     if(!IsEqualIID(&IID_IUnknown, riid) && !IsEqualIID(&IID_IInternetZoneManager, riid)) {
691         FIXME("Unknown interface: %s\n", debugstr_guid(riid));
692         *ppvObject = NULL;
693         return E_NOINTERFACE;
694     }
695
696     *ppvObject = iface;
697     IInternetZoneManager_AddRef(iface);
698
699     return S_OK;
700 }
701
702 /********************************************************************
703  *      IInternetZoneManager_AddRef
704  */
705 static ULONG WINAPI ZoneMgrImpl_AddRef(IInternetZoneManager* iface)
706 {
707     ZoneMgrImpl* This = (ZoneMgrImpl*)iface;
708     ULONG refCount = InterlockedIncrement(&This->ref);
709
710     TRACE("(%p)->(ref before=%u)\n",This, refCount - 1);
711
712     return refCount;
713 }
714
715 /********************************************************************
716  *      IInternetZoneManager_Release
717  */
718 static ULONG WINAPI ZoneMgrImpl_Release(IInternetZoneManager* iface)
719 {
720     ZoneMgrImpl* This = (ZoneMgrImpl*)iface;
721     ULONG refCount = InterlockedDecrement(&This->ref);
722
723     TRACE("(%p)->(ref before=%u)\n",This, refCount + 1);
724
725     if(!refCount) {
726         heap_free(This);
727         URLMON_UnlockModule();
728     }
729     
730     return refCount;
731 }
732
733 /********************************************************************
734  *      IInternetZoneManager_GetZoneAttributes
735  */
736 static HRESULT WINAPI ZoneMgrImpl_GetZoneAttributes(IInternetZoneManager* iface,
737                                                     DWORD dwZone,
738                                                     ZONEATTRIBUTES* pZoneAttributes)
739 {
740     ZoneMgrImpl* This = (ZoneMgrImpl*)iface;
741     HRESULT hr;
742     HKEY hcu;
743     HKEY hklm = NULL;
744
745     TRACE("(%p)->(%d %p)\n", This, dwZone, pZoneAttributes);
746
747     if (!pZoneAttributes)
748         return E_INVALIDARG;
749
750     hr = open_zone_key(HKEY_CURRENT_USER, dwZone, &hcu);
751     if (FAILED(hr))
752         return S_OK;  /* IE6 and older returned E_FAIL here */
753
754     hr = open_zone_key(HKEY_LOCAL_MACHINE, dwZone, &hklm);
755     if (FAILED(hr))
756         TRACE("Zone %d not in HKLM\n", dwZone);
757
758     get_string_from_reg(hcu, hklm, displaynameW, pZoneAttributes->szDisplayName, MAX_ZONE_PATH);
759     get_string_from_reg(hcu, hklm, descriptionW, pZoneAttributes->szDescription, MAX_ZONE_DESCRIPTION);
760     get_string_from_reg(hcu, hklm, iconW, pZoneAttributes->szIconPath, MAX_ZONE_PATH);
761     get_dword_from_reg(hcu, hklm, minlevelW, &pZoneAttributes->dwTemplateMinLevel);
762     get_dword_from_reg(hcu, hklm, currentlevelW, &pZoneAttributes->dwTemplateCurrentLevel);
763     get_dword_from_reg(hcu, hklm, recommendedlevelW, &pZoneAttributes->dwTemplateRecommended);
764     get_dword_from_reg(hcu, hklm, flagsW, &pZoneAttributes->dwFlags);
765
766     RegCloseKey(hklm);
767     RegCloseKey(hcu);
768     return S_OK;
769 }
770
771 /********************************************************************
772  *      IInternetZoneManager_SetZoneAttributes
773  */
774 static HRESULT WINAPI ZoneMgrImpl_SetZoneAttributes(IInternetZoneManager* iface,
775                                                     DWORD dwZone,
776                                                     ZONEATTRIBUTES* pZoneAttributes)
777 {
778     FIXME("(%p)->(%08x %p) stub\n", iface, dwZone, pZoneAttributes);
779     return E_NOTIMPL;
780 }
781
782 /********************************************************************
783  *      IInternetZoneManager_GetZoneCustomPolicy
784  */
785 static HRESULT WINAPI ZoneMgrImpl_GetZoneCustomPolicy(IInternetZoneManager* iface,
786                                                       DWORD dwZone,
787                                                       REFGUID guidKey,
788                                                       BYTE** ppPolicy,
789                                                       DWORD* pcbPolicy,
790                                                       URLZONEREG ulrZoneReg)
791 {
792     FIXME("(%p)->(%08x %s %p %p %08x) stub\n", iface, dwZone, debugstr_guid(guidKey),
793                                                     ppPolicy, pcbPolicy, ulrZoneReg);
794     return E_NOTIMPL;
795 }
796
797 /********************************************************************
798  *      IInternetZoneManager_SetZoneCustomPolicy
799  */
800 static HRESULT WINAPI ZoneMgrImpl_SetZoneCustomPolicy(IInternetZoneManager* iface,
801                                                       DWORD dwZone,
802                                                       REFGUID guidKey,
803                                                       BYTE* ppPolicy,
804                                                       DWORD cbPolicy,
805                                                       URLZONEREG ulrZoneReg)
806 {
807     FIXME("(%p)->(%08x %s %p %08x %08x) stub\n", iface, dwZone, debugstr_guid(guidKey),
808                                                     ppPolicy, cbPolicy, ulrZoneReg);
809     return E_NOTIMPL;
810 }
811
812 /********************************************************************
813  *      IInternetZoneManager_GetZoneActionPolicy
814  */
815 static HRESULT WINAPI ZoneMgrImpl_GetZoneActionPolicy(IInternetZoneManager* iface,
816         DWORD dwZone, DWORD dwAction, BYTE* pPolicy, DWORD cbPolicy, URLZONEREG urlZoneReg)
817 {
818     TRACE("(%p)->(%d %08x %p %d %d)\n", iface, dwZone, dwAction, pPolicy,
819             cbPolicy, urlZoneReg);
820
821     if(!pPolicy)
822         return E_INVALIDARG;
823
824     return get_action_policy(dwZone, dwAction, pPolicy, cbPolicy, urlZoneReg);
825 }
826
827 /********************************************************************
828  *      IInternetZoneManager_SetZoneActionPolicy
829  */
830 static HRESULT WINAPI ZoneMgrImpl_SetZoneActionPolicy(IInternetZoneManager* iface,
831                                                       DWORD dwZone,
832                                                       DWORD dwAction,
833                                                       BYTE* pPolicy,
834                                                       DWORD cbPolicy,
835                                                       URLZONEREG urlZoneReg)
836 {
837     FIXME("(%p)->(%08x %08x %p %08x %08x) stub\n", iface, dwZone, dwAction, pPolicy,
838                                                        cbPolicy, urlZoneReg);
839     return E_NOTIMPL;
840 }
841
842 /********************************************************************
843  *      IInternetZoneManager_PromptAction
844  */
845 static HRESULT WINAPI ZoneMgrImpl_PromptAction(IInternetZoneManager* iface,
846                                                DWORD dwAction,
847                                                HWND hwndParent,
848                                                LPCWSTR pwszUrl,
849                                                LPCWSTR pwszText,
850                                                DWORD dwPromptFlags)
851 {
852     FIXME("%p %08x %p %s %s %08x\n", iface, dwAction, hwndParent,
853           debugstr_w(pwszUrl), debugstr_w(pwszText), dwPromptFlags );
854     return E_NOTIMPL;
855 }
856
857 /********************************************************************
858  *      IInternetZoneManager_LogAction
859  */
860 static HRESULT WINAPI ZoneMgrImpl_LogAction(IInternetZoneManager* iface,
861                                             DWORD dwAction,
862                                             LPCWSTR pwszUrl,
863                                             LPCWSTR pwszText,
864                                             DWORD dwLogFlags)
865 {
866     FIXME("(%p)->(%08x %s %s %08x) stub\n", iface, dwAction, debugstr_w(pwszUrl),
867                                               debugstr_w(pwszText), dwLogFlags);
868     return E_NOTIMPL;
869 }
870
871 /********************************************************************
872  *      IInternetZoneManager_CreateZoneEnumerator
873  */
874 static HRESULT WINAPI ZoneMgrImpl_CreateZoneEnumerator(IInternetZoneManager* iface,
875                                                        DWORD* pdwEnum,
876                                                        DWORD* pdwCount,
877                                                        DWORD dwFlags)
878 {
879     FIXME("(%p)->(%p %p %08x) stub\n", iface, pdwEnum, pdwCount, dwFlags);
880     return E_NOTIMPL;
881 }
882
883 /********************************************************************
884  *      IInternetZoneManager_GetZoneAt
885  */
886 static HRESULT WINAPI ZoneMgrImpl_GetZoneAt(IInternetZoneManager* iface,
887                                             DWORD dwEnum,
888                                             DWORD dwIndex,
889                                             DWORD* pdwZone)
890 {
891     FIXME("(%p)->(%08x %08x %p) stub\n", iface, dwEnum, dwIndex, pdwZone);
892     return E_NOTIMPL;
893 }
894
895 /********************************************************************
896  *      IInternetZoneManager_DestroyZoneEnumerator
897  */
898 static HRESULT WINAPI ZoneMgrImpl_DestroyZoneEnumerator(IInternetZoneManager* iface,
899                                                         DWORD dwEnum)
900 {
901     FIXME("(%p)->(%08x) stub\n", iface, dwEnum);
902     return E_NOTIMPL;
903 }
904
905 /********************************************************************
906  *      IInternetZoneManager_CopyTemplatePoliciesToZone
907  */
908 static HRESULT WINAPI ZoneMgrImpl_CopyTemplatePoliciesToZone(IInternetZoneManager* iface,
909                                                              DWORD dwTemplate,
910                                                              DWORD dwZone,
911                                                              DWORD dwReserved)
912 {
913     FIXME("(%p)->(%08x %08x %08x) stub\n", iface, dwTemplate, dwZone, dwReserved);
914     return E_NOTIMPL;
915 }
916
917 /********************************************************************
918  *      IInternetZoneManager_Construct
919  */
920 static const IInternetZoneManagerVtbl ZoneMgrImplVtbl = {
921     ZoneMgrImpl_QueryInterface,
922     ZoneMgrImpl_AddRef,
923     ZoneMgrImpl_Release,
924     ZoneMgrImpl_GetZoneAttributes,
925     ZoneMgrImpl_SetZoneAttributes,
926     ZoneMgrImpl_GetZoneCustomPolicy,
927     ZoneMgrImpl_SetZoneCustomPolicy,
928     ZoneMgrImpl_GetZoneActionPolicy,
929     ZoneMgrImpl_SetZoneActionPolicy,
930     ZoneMgrImpl_PromptAction,
931     ZoneMgrImpl_LogAction,
932     ZoneMgrImpl_CreateZoneEnumerator,
933     ZoneMgrImpl_GetZoneAt,
934     ZoneMgrImpl_DestroyZoneEnumerator,
935     ZoneMgrImpl_CopyTemplatePoliciesToZone,
936 };
937
938 HRESULT ZoneMgrImpl_Construct(IUnknown *pUnkOuter, LPVOID *ppobj)
939 {
940     ZoneMgrImpl* ret = heap_alloc(sizeof(ZoneMgrImpl));
941
942     TRACE("(%p %p)\n", pUnkOuter, ppobj);
943     ret->lpVtbl = &ZoneMgrImplVtbl;
944     ret->ref = 1;
945     *ppobj = (IInternetZoneManager*)ret;
946
947     URLMON_LockModule();
948
949     return S_OK;
950 }
951
952 /***********************************************************************
953  *           CoInternetCreateSecurityManager (URLMON.@)
954  *
955  */
956 HRESULT WINAPI CoInternetCreateSecurityManager( IServiceProvider *pSP,
957     IInternetSecurityManager **ppSM, DWORD dwReserved )
958 {
959     TRACE("%p %p %d\n", pSP, ppSM, dwReserved );
960
961     if(pSP)
962         FIXME("pSP not supported\n");
963
964     return SecManagerImpl_Construct(NULL, (void**) ppSM);
965 }
966
967 /********************************************************************
968  *      CoInternetCreateZoneManager (URLMON.@)
969  */
970 HRESULT WINAPI CoInternetCreateZoneManager(IServiceProvider* pSP, IInternetZoneManager** ppZM, DWORD dwReserved)
971 {
972     TRACE("(%p %p %x)\n", pSP, ppZM, dwReserved);
973     return ZoneMgrImpl_Construct(NULL, (void**)ppZM);
974 }