urlmon: Added parser for IPv6 addresses.
[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  * Copyright 2009 Detlef Riekenberg
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21  */
22
23 #include <stdio.h>
24
25 #include "urlmon_main.h"
26 #include "winreg.h"
27 #include "wininet.h"
28
29 #include "wine/debug.h"
30
31 WINE_DEFAULT_DEBUG_CHANNEL(urlmon);
32
33 static const WCHAR currentlevelW[] = {'C','u','r','r','e','n','t','L','e','v','e','l',0};
34 static const WCHAR descriptionW[] = {'D','e','s','c','r','i','p','t','i','o','n',0};
35 static const WCHAR displaynameW[] = {'D','i','s','p','l','a','y','N','a','m','e',0};
36 static const WCHAR fileW[] = {'f','i','l','e',0};
37 static const WCHAR flagsW[] = {'F','l','a','g','s',0};
38 static const WCHAR iconW[] = {'I','c','o','n',0};
39 static const WCHAR minlevelW[] = {'M','i','n','L','e','v','e','l',0};
40 static const WCHAR recommendedlevelW[] = {'R','e','c','o','m','m','e','n','d','e','d',
41                                           'L','e','v','e','l',0};
42 static const WCHAR wszZonesKey[] = {'S','o','f','t','w','a','r','e','\\',
43                                     'M','i','c','r','o','s','o','f','t','\\',
44                                     'W','i','n','d','o','w','s','\\',
45                                     'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
46                                     'I','n','t','e','r','n','e','t',' ','S','e','t','t','i','n','g','s','\\',
47                                     'Z','o','n','e','s','\\',0};
48
49 /********************************************************************
50  * get_string_from_reg [internal]
51  *
52  * helper to get a string from the reg.
53  *
54  */
55 static void get_string_from_reg(HKEY hcu, HKEY hklm, LPCWSTR name, LPWSTR out, DWORD maxlen)
56 {
57     DWORD type = REG_SZ;
58     DWORD len = maxlen * sizeof(WCHAR);
59     DWORD res;
60
61     res = RegQueryValueExW(hcu, name, NULL, &type, (LPBYTE) out, &len);
62
63     if (res && hklm) {
64         len = maxlen * sizeof(WCHAR);
65         type = REG_SZ;
66         res = RegQueryValueExW(hklm, name, NULL, &type, (LPBYTE) out, &len);
67     }
68
69     if (res) {
70         TRACE("%s failed: %d\n", debugstr_w(name), res);
71         *out = '\0';
72     }
73 }
74
75 /********************************************************************
76  * get_dword_from_reg [internal]
77  *
78  * helper to get a dword from the reg.
79  *
80  */
81 static void get_dword_from_reg(HKEY hcu, HKEY hklm, LPCWSTR name, LPDWORD out)
82 {
83     DWORD type = REG_DWORD;
84     DWORD len = sizeof(DWORD);
85     DWORD res;
86
87     res = RegQueryValueExW(hcu, name, NULL, &type, (LPBYTE) out, &len);
88
89     if (res && hklm) {
90         len = sizeof(DWORD);
91         type = REG_DWORD;
92         res = RegQueryValueExW(hklm, name, NULL, &type, (LPBYTE) out, &len);
93     }
94
95     if (res) {
96         TRACE("%s failed: %d\n", debugstr_w(name), res);
97         *out = 0;
98     }
99 }
100
101 static HRESULT get_zone_from_reg(LPCWSTR schema, DWORD *zone)
102 {
103     DWORD res, size;
104     HKEY hkey;
105
106     static const WCHAR wszZoneMapProtocolKey[] =
107         {'S','o','f','t','w','a','r','e','\\',
108          'M','i','c','r','o','s','o','f','t','\\',
109          'W','i','n','d','o','w','s','\\',
110          'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
111          'I','n','t','e','r','n','e','t',' ','S','e','t','t','i','n','g','s','\\',
112          'Z','o','n','e','M','a','p','\\',
113          'P','r','o','t','o','c','o','l','D','e','f','a','u','l','t','s',0};
114
115     res = RegOpenKeyW(HKEY_CURRENT_USER, wszZoneMapProtocolKey, &hkey);
116     if(res != ERROR_SUCCESS) {
117         ERR("Could not open key %s\n", debugstr_w(wszZoneMapProtocolKey));
118         return E_UNEXPECTED;
119     }
120
121     size = sizeof(DWORD);
122     res = RegQueryValueExW(hkey, schema, NULL, NULL, (PBYTE)zone, &size);
123     RegCloseKey(hkey);
124     if(res == ERROR_SUCCESS)
125         return S_OK;
126
127     res = RegOpenKeyW(HKEY_LOCAL_MACHINE, wszZoneMapProtocolKey, &hkey);
128     if(res != ERROR_SUCCESS) {
129         ERR("Could not open key %s\n", debugstr_w(wszZoneMapProtocolKey));
130         return E_UNEXPECTED;
131     }
132
133     size = sizeof(DWORD);
134     res = RegQueryValueExW(hkey, schema, NULL, NULL, (PBYTE)zone, &size);
135     RegCloseKey(hkey);
136     if(res == ERROR_SUCCESS)
137         return S_OK;
138
139     *zone = 3;
140     return S_OK;
141 }
142
143 static HRESULT map_url_to_zone(LPCWSTR url, DWORD *zone, LPWSTR *ret_url)
144 {
145     LPWSTR secur_url;
146     WCHAR schema[64];
147     DWORD size=0;
148     HRESULT hres;
149
150     *zone = -1;
151
152     hres = CoInternetGetSecurityUrl(url, &secur_url, PSU_SECURITY_URL_ONLY, 0);
153     if(hres != S_OK) {
154         size = strlenW(url)*sizeof(WCHAR);
155
156         secur_url = heap_alloc(size);
157         if(!secur_url)
158             return E_OUTOFMEMORY;
159
160         memcpy(secur_url, url, size);
161     }
162
163     hres = CoInternetParseUrl(secur_url, PARSE_SCHEMA, 0, schema, sizeof(schema)/sizeof(WCHAR), &size, 0);
164     if(FAILED(hres) || !*schema) {
165         heap_free(secur_url);
166         return E_INVALIDARG;
167     }
168
169     /* file protocol is a special case */
170     if(!strcmpW(schema, fileW)) {
171         WCHAR path[MAX_PATH], root[20];
172         WCHAR *ptr;
173
174         hres = CoInternetParseUrl(secur_url, PARSE_PATH_FROM_URL, 0, path,
175                 sizeof(path)/sizeof(WCHAR), &size, 0);
176
177         if(SUCCEEDED(hres) && (ptr = strchrW(path, '\\')) && ptr-path < sizeof(root)/sizeof(WCHAR)) {
178             UINT type;
179
180             memcpy(root, path, (ptr-path)*sizeof(WCHAR));
181             root[ptr-path] = 0;
182
183             type = GetDriveTypeW(root);
184
185             switch(type) {
186             case DRIVE_UNKNOWN:
187             case DRIVE_NO_ROOT_DIR:
188                 break;
189             case DRIVE_REMOVABLE:
190             case DRIVE_FIXED:
191             case DRIVE_CDROM:
192             case DRIVE_RAMDISK:
193                 *zone = 0;
194                 hres = S_OK;
195                 break;
196             case DRIVE_REMOTE:
197                 *zone = 3;
198                 hres = S_OK;
199                 break;
200             default:
201                 FIXME("unsupported drive type %d\n", type);
202             }
203         }
204     }
205
206     if(*zone == -1) {
207         WARN("domains are not yet implemented\n");
208         hres = get_zone_from_reg(schema, zone);
209     }
210
211     if(FAILED(hres) || !ret_url)
212         heap_free(secur_url);
213     else
214         *ret_url = secur_url;
215
216     return hres;
217 }
218
219 static HRESULT open_zone_key(HKEY parent_key, DWORD zone, HKEY *hkey)
220 {
221     static const WCHAR wszFormat[] = {'%','s','%','l','d',0};
222
223     WCHAR key_name[sizeof(wszZonesKey)/sizeof(WCHAR)+8];
224     DWORD res;
225
226     wsprintfW(key_name, wszFormat, wszZonesKey, zone);
227
228     res = RegOpenKeyW(parent_key, key_name, hkey);
229
230     if(res != ERROR_SUCCESS) {
231         WARN("RegOpenKey failed\n");
232         return E_INVALIDARG;
233     }
234
235     return S_OK;
236 }
237
238 static HRESULT get_action_policy(DWORD zone, DWORD action, BYTE *policy, DWORD size, URLZONEREG zone_reg)
239 {
240     HKEY parent_key;
241     HKEY hkey;
242     LONG res;
243     HRESULT hres;
244
245     switch(action) {
246     case URLACTION_SCRIPT_OVERRIDE_SAFETY:
247     case URLACTION_ACTIVEX_OVERRIDE_SCRIPT_SAFETY:
248         *(DWORD*)policy = URLPOLICY_DISALLOW;
249         return S_OK;
250     }
251
252     switch(zone_reg) {
253     case URLZONEREG_DEFAULT:
254     case URLZONEREG_HKCU:
255         parent_key = HKEY_CURRENT_USER;
256         break;
257     case URLZONEREG_HKLM:
258         parent_key = HKEY_LOCAL_MACHINE;
259         break;
260     default:
261         WARN("Unknown URLZONEREG: %d\n", zone_reg);
262         return E_FAIL;
263     };
264
265     hres = open_zone_key(parent_key, zone, &hkey);
266     if(SUCCEEDED(hres)) {
267         WCHAR action_str[16];
268         DWORD len = size;
269
270         static const WCHAR formatW[] = {'%','X',0};
271
272         wsprintfW(action_str, formatW, action);
273
274         res = RegQueryValueExW(hkey, action_str, NULL, NULL, policy, &len);
275         if(res == ERROR_MORE_DATA) {
276             hres = E_INVALIDARG;
277         }else if(res == ERROR_FILE_NOT_FOUND) {
278             hres = E_FAIL;
279         }else if(res != ERROR_SUCCESS) {
280             ERR("RegQueryValue failed: %d\n", res);
281             hres = E_UNEXPECTED;
282         }
283
284         RegCloseKey(hkey);
285     }
286
287     if(FAILED(hres) && zone_reg == URLZONEREG_DEFAULT)
288         return get_action_policy(zone, action, policy, size, URLZONEREG_HKLM);
289
290     return hres;
291 }
292
293 /***********************************************************************
294  *           InternetSecurityManager implementation
295  *
296  */
297 typedef struct {
298     const IInternetSecurityManagerVtbl* lpInternetSecurityManagerVtbl;
299
300     LONG ref;
301
302     IInternetSecurityMgrSite *mgrsite;
303     IInternetSecurityManager *custom_manager;
304 } SecManagerImpl;
305
306 #define SECMGR_THIS(iface) DEFINE_THIS(SecManagerImpl, InternetSecurityManager, iface)
307
308 static HRESULT WINAPI SecManagerImpl_QueryInterface(IInternetSecurityManager* iface,REFIID riid,void** ppvObject)
309 {
310     SecManagerImpl *This = SECMGR_THIS(iface);
311
312     TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppvObject);
313
314     /* Perform a sanity check on the parameters.*/
315     if ( (This==0) || (ppvObject==0) )
316         return E_INVALIDARG;
317
318     /* Initialize the return parameter */
319     *ppvObject = 0;
320
321     /* Compare the riid with the interface IDs implemented by this object.*/
322     if (IsEqualIID(&IID_IUnknown, riid) ||
323         IsEqualIID(&IID_IInternetSecurityManager, riid))
324         *ppvObject = iface;
325
326     /* Check that we obtained an interface.*/
327     if (!*ppvObject) {
328         WARN("not supported interface %s\n", debugstr_guid(riid));
329         return E_NOINTERFACE;
330     }
331
332     /* Query Interface always increases the reference count by one when it is successful */
333     IInternetSecurityManager_AddRef(iface);
334
335     return S_OK;
336 }
337
338 static ULONG WINAPI SecManagerImpl_AddRef(IInternetSecurityManager* iface)
339 {
340     SecManagerImpl *This = SECMGR_THIS(iface);
341     ULONG refCount = InterlockedIncrement(&This->ref);
342
343     TRACE("(%p) ref=%u\n", This, refCount);
344
345     return refCount;
346 }
347
348 static ULONG WINAPI SecManagerImpl_Release(IInternetSecurityManager* iface)
349 {
350     SecManagerImpl *This = SECMGR_THIS(iface);
351     ULONG refCount = InterlockedDecrement(&This->ref);
352
353     TRACE("(%p) ref=%u\n", This, refCount);
354
355     /* destroy the object if there's no more reference on it */
356     if (!refCount){
357         if(This->mgrsite)
358             IInternetSecurityMgrSite_Release(This->mgrsite);
359         if(This->custom_manager)
360             IInternetSecurityManager_Release(This->custom_manager);
361
362         heap_free(This);
363
364         URLMON_UnlockModule();
365     }
366
367     return refCount;
368 }
369
370 static HRESULT WINAPI SecManagerImpl_SetSecuritySite(IInternetSecurityManager *iface,
371                                                      IInternetSecurityMgrSite *pSite)
372 {
373     SecManagerImpl *This = SECMGR_THIS(iface);
374
375     TRACE("(%p)->(%p)\n", This, pSite);
376
377     if(This->mgrsite)
378         IInternetSecurityMgrSite_Release(This->mgrsite);
379
380     if(This->custom_manager) {
381         IInternetSecurityManager_Release(This->custom_manager);
382         This->custom_manager = NULL;
383     }
384
385     This->mgrsite = pSite;
386
387     if(pSite) {
388         IServiceProvider *servprov;
389         HRESULT hres;
390
391         IInternetSecurityMgrSite_AddRef(pSite);
392
393         hres = IInternetSecurityMgrSite_QueryInterface(pSite, &IID_IServiceProvider,
394                 (void**)&servprov);
395         if(SUCCEEDED(hres)) {
396             IServiceProvider_QueryService(servprov, &SID_SInternetSecurityManager,
397                     &IID_IInternetSecurityManager, (void**)&This->custom_manager);
398             IServiceProvider_Release(servprov);
399         }
400     }
401
402     return S_OK;
403 }
404
405 static HRESULT WINAPI SecManagerImpl_GetSecuritySite(IInternetSecurityManager *iface,
406                                                      IInternetSecurityMgrSite **ppSite)
407 {
408     SecManagerImpl *This = SECMGR_THIS(iface);
409
410     TRACE("(%p)->(%p)\n", This, ppSite);
411
412     if(!ppSite)
413         return E_INVALIDARG;
414
415     if(This->mgrsite)
416         IInternetSecurityMgrSite_AddRef(This->mgrsite);
417
418     *ppSite = This->mgrsite;
419     return S_OK;
420 }
421
422 static HRESULT WINAPI SecManagerImpl_MapUrlToZone(IInternetSecurityManager *iface,
423                                                   LPCWSTR pwszUrl, DWORD *pdwZone,
424                                                   DWORD dwFlags)
425 {
426     SecManagerImpl *This = SECMGR_THIS(iface);
427     HRESULT hres;
428
429     TRACE("(%p)->(%s %p %08x)\n", iface, debugstr_w(pwszUrl), pdwZone, dwFlags);
430
431     if(This->custom_manager) {
432         hres = IInternetSecurityManager_MapUrlToZone(This->custom_manager,
433                 pwszUrl, pdwZone, dwFlags);
434         if(hres != INET_E_DEFAULT_ACTION)
435             return hres;
436     }
437
438     if(!pwszUrl) {
439         *pdwZone = -1;
440         return E_INVALIDARG;
441     }
442
443     if(dwFlags)
444         FIXME("not supported flags: %08x\n", dwFlags);
445
446     return map_url_to_zone(pwszUrl, pdwZone, NULL);
447 }
448
449 static HRESULT WINAPI SecManagerImpl_GetSecurityId(IInternetSecurityManager *iface, 
450         LPCWSTR pwszUrl, BYTE *pbSecurityId, DWORD *pcbSecurityId, DWORD_PTR dwReserved)
451 {
452     SecManagerImpl *This = SECMGR_THIS(iface);
453     LPWSTR url, ptr, ptr2;
454     DWORD zone, len;
455     HRESULT hres;
456
457     static const WCHAR wszFile[] = {'f','i','l','e',':'};
458
459     TRACE("(%p)->(%s %p %p %08lx)\n", iface, debugstr_w(pwszUrl), pbSecurityId,
460           pcbSecurityId, dwReserved);
461
462     if(This->custom_manager) {
463         hres = IInternetSecurityManager_GetSecurityId(This->custom_manager,
464                 pwszUrl, pbSecurityId, pcbSecurityId, dwReserved);
465         if(hres != INET_E_DEFAULT_ACTION)
466             return hres;
467     }
468
469     if(!pwszUrl || !pbSecurityId || !pcbSecurityId)
470         return E_INVALIDARG;
471
472     if(dwReserved)
473         FIXME("dwReserved is not supported\n");
474
475     hres = map_url_to_zone(pwszUrl, &zone, &url);
476     if(FAILED(hres))
477         return hres == 0x80041001 ? E_INVALIDARG : hres;
478
479     /* file protocol is a special case */
480     if(strlenW(url) >= sizeof(wszFile)/sizeof(WCHAR)
481             && !memcmp(url, wszFile, sizeof(wszFile)) && strchrW(url, '\\')) {
482
483         static const BYTE secidFile[] = {'f','i','l','e',':'};
484
485         heap_free(url);
486
487         if(*pcbSecurityId < sizeof(secidFile)+sizeof(zone))
488             return HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
489
490         memcpy(pbSecurityId, secidFile, sizeof(secidFile));
491         *(DWORD*)(pbSecurityId+sizeof(secidFile)) = zone;
492
493         *pcbSecurityId = sizeof(secidFile)+sizeof(zone);
494         return S_OK;
495     }
496
497     ptr = strchrW(url, ':');
498     ptr2 = ++ptr;
499     while(*ptr2 == '/')
500         ptr2++;
501     if(ptr2 != ptr)
502         memmove(ptr, ptr2, (strlenW(ptr2)+1)*sizeof(WCHAR));
503
504     ptr = strchrW(ptr, '/');
505     if(ptr)
506         *ptr = 0;
507
508     len = WideCharToMultiByte(CP_ACP, 0, url, -1, NULL, 0, NULL, NULL)-1;
509
510     if(len+sizeof(DWORD) > *pcbSecurityId) {
511         heap_free(url);
512         return HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
513     }
514
515     WideCharToMultiByte(CP_ACP, 0, url, -1, (LPSTR)pbSecurityId, len, NULL, NULL);
516     heap_free(url);
517
518     *(DWORD*)(pbSecurityId+len) = zone;
519
520     *pcbSecurityId = len+sizeof(DWORD);
521
522     return S_OK;
523 }
524
525
526 static HRESULT WINAPI SecManagerImpl_ProcessUrlAction(IInternetSecurityManager *iface,
527                                                       LPCWSTR pwszUrl, DWORD dwAction,
528                                                       BYTE *pPolicy, DWORD cbPolicy,
529                                                       BYTE *pContext, DWORD cbContext,
530                                                       DWORD dwFlags, DWORD dwReserved)
531 {
532     SecManagerImpl *This = SECMGR_THIS(iface);
533     DWORD zone, policy;
534     HRESULT hres;
535
536     TRACE("(%p)->(%s %08x %p %08x %p %08x %08x %08x)\n", iface, debugstr_w(pwszUrl), dwAction,
537           pPolicy, cbPolicy, pContext, cbContext, dwFlags, dwReserved);
538
539     if(This->custom_manager) {
540         hres = IInternetSecurityManager_ProcessUrlAction(This->custom_manager, pwszUrl, dwAction,
541                 pPolicy, cbPolicy, pContext, cbContext, dwFlags, dwReserved);
542         if(hres != INET_E_DEFAULT_ACTION)
543             return hres;
544     }
545
546     if(dwFlags || dwReserved)
547         FIXME("Unsupported arguments\n");
548
549     if(!pwszUrl)
550         return E_INVALIDARG;
551
552     hres = map_url_to_zone(pwszUrl, &zone, NULL);
553     if(FAILED(hres))
554         return hres;
555
556     hres = get_action_policy(zone, dwAction, (BYTE*)&policy, sizeof(policy), URLZONEREG_DEFAULT);
557     if(FAILED(hres))
558         return hres;
559
560     TRACE("policy %x\n", policy);
561     if(cbPolicy >= sizeof(DWORD))
562         *(DWORD*)pPolicy = policy;
563
564     switch(GetUrlPolicyPermissions(policy)) {
565     case URLPOLICY_ALLOW:
566     case URLPOLICY_CHANNEL_SOFTDIST_PRECACHE:
567         return S_OK;
568     case URLPOLICY_DISALLOW:
569         return S_FALSE;
570     case URLPOLICY_QUERY:
571         FIXME("URLPOLICY_QUERY not implemented\n");
572         return E_FAIL;
573     default:
574         FIXME("Not implemented policy %x\n", policy);
575     }
576
577     return E_FAIL;
578 }
579                                                
580
581 static HRESULT WINAPI SecManagerImpl_QueryCustomPolicy(IInternetSecurityManager *iface,
582                                                        LPCWSTR pwszUrl, REFGUID guidKey,
583                                                        BYTE **ppPolicy, DWORD *pcbPolicy,
584                                                        BYTE *pContext, DWORD cbContext,
585                                                        DWORD dwReserved)
586 {
587     SecManagerImpl *This = SECMGR_THIS(iface);
588     HRESULT hres;
589
590     TRACE("(%p)->(%s %s %p %p %p %08x %08x )\n", iface, debugstr_w(pwszUrl), debugstr_guid(guidKey),
591           ppPolicy, pcbPolicy, pContext, cbContext, dwReserved);
592
593     if(This->custom_manager) {
594         hres = IInternetSecurityManager_QueryCustomPolicy(This->custom_manager, pwszUrl, guidKey,
595                 ppPolicy, pcbPolicy, pContext, cbContext, dwReserved);
596         if(hres != INET_E_DEFAULT_ACTION)
597             return hres;
598     }
599
600     WARN("Unknown guidKey %s\n", debugstr_guid(guidKey));
601     return HRESULT_FROM_WIN32(ERROR_NOT_FOUND);
602 }
603
604 static HRESULT WINAPI SecManagerImpl_SetZoneMapping(IInternetSecurityManager *iface,
605                                                     DWORD dwZone, LPCWSTR pwszPattern, DWORD dwFlags)
606 {
607     SecManagerImpl *This = SECMGR_THIS(iface);
608     HRESULT hres;
609
610     TRACE("(%p)->(%08x %s %08x)\n", iface, dwZone, debugstr_w(pwszPattern),dwFlags);
611
612     if(This->custom_manager) {
613         hres = IInternetSecurityManager_SetZoneMapping(This->custom_manager, dwZone,
614                 pwszPattern, dwFlags);
615         if(hres != INET_E_DEFAULT_ACTION)
616             return hres;
617     }
618
619     FIXME("Default action is not implemented\n");
620     return E_NOTIMPL;
621 }
622
623 static HRESULT WINAPI SecManagerImpl_GetZoneMappings(IInternetSecurityManager *iface,
624         DWORD dwZone, IEnumString **ppenumString, DWORD dwFlags)
625 {
626     SecManagerImpl *This = SECMGR_THIS(iface);
627     HRESULT hres;
628
629     TRACE("(%p)->(%08x %p %08x)\n", iface, dwZone, ppenumString,dwFlags);
630
631     if(This->custom_manager) {
632         hres = IInternetSecurityManager_GetZoneMappings(This->custom_manager, dwZone,
633                 ppenumString, dwFlags);
634         if(hres != INET_E_DEFAULT_ACTION)
635             return hres;
636     }
637
638     FIXME("Default action is not implemented\n");
639     return E_NOTIMPL;
640 }
641
642 static const IInternetSecurityManagerVtbl VT_SecManagerImpl =
643 {
644     SecManagerImpl_QueryInterface,
645     SecManagerImpl_AddRef,
646     SecManagerImpl_Release,
647     SecManagerImpl_SetSecuritySite,
648     SecManagerImpl_GetSecuritySite,
649     SecManagerImpl_MapUrlToZone,
650     SecManagerImpl_GetSecurityId,
651     SecManagerImpl_ProcessUrlAction,
652     SecManagerImpl_QueryCustomPolicy,
653     SecManagerImpl_SetZoneMapping,
654     SecManagerImpl_GetZoneMappings
655 };
656
657 HRESULT SecManagerImpl_Construct(IUnknown *pUnkOuter, LPVOID *ppobj)
658 {
659     SecManagerImpl *This;
660
661     TRACE("(%p,%p)\n",pUnkOuter,ppobj);
662     This = heap_alloc(sizeof(*This));
663
664     /* Initialize the virtual function table. */
665     This->lpInternetSecurityManagerVtbl = &VT_SecManagerImpl;
666
667     This->ref = 1;
668     This->mgrsite = NULL;
669     This->custom_manager = NULL;
670
671     *ppobj = This;
672
673     URLMON_LockModule();
674
675     return S_OK;
676 }
677
678 /***********************************************************************
679  *           InternetZoneManager implementation
680  *
681  */
682 typedef struct {
683     const IInternetZoneManagerEx2Vtbl* lpVtbl;
684     LONG ref;
685     LPDWORD *zonemaps;
686     DWORD zonemap_count;
687 } ZoneMgrImpl;
688
689
690 /***********************************************************************
691  * build_zonemap_from_reg [internal]
692  *
693  * Enumerate the Zones in the Registry and return the Zones in a DWORD-array
694  * The number of the Zones is returned in data[0]
695  */
696 static LPDWORD build_zonemap_from_reg(void)
697 {
698     WCHAR name[32];
699     HKEY hkey;
700     LPDWORD data = NULL;
701     DWORD allocated = 6; /* space for the zonecount and Zone "0" up to Zone "4" */
702     DWORD used = 0;
703     DWORD res;
704     DWORD len;
705
706
707     res = RegOpenKeyW(HKEY_CURRENT_USER, wszZonesKey, &hkey);
708     if (res)
709         return NULL;
710
711     data = heap_alloc(allocated * sizeof(DWORD));
712     if (!data)
713         goto cleanup;
714
715     while (!res) {
716         name[0] = '\0';
717         len = sizeof(name) / sizeof(name[0]);
718         res = RegEnumKeyExW(hkey, used, name, &len, NULL, NULL, NULL, NULL);
719
720         if (!res) {
721             used++;
722             if (used == allocated) {
723                 LPDWORD new_data;
724
725                 allocated *= 2;
726                 new_data = heap_realloc_zero(data, allocated * sizeof(DWORD));
727                 if (!new_data)
728                     goto cleanup;
729
730                 data = new_data;
731             }
732             data[used] = atoiW(name);
733         }
734     }
735     if (used) {
736         RegCloseKey(hkey);
737         data[0] = used;
738         return data;
739     }
740
741 cleanup:
742     /* something failed */
743     RegCloseKey(hkey);
744     heap_free(data);
745     return NULL;
746 }
747
748 /********************************************************************
749  *      IInternetZoneManager_QueryInterface
750  */
751 static HRESULT WINAPI ZoneMgrImpl_QueryInterface(IInternetZoneManagerEx2* iface, REFIID riid, void** ppvObject)
752 {
753     ZoneMgrImpl* This = (ZoneMgrImpl*)iface;
754
755     TRACE("(%p)->(%s,%p)\n", This, debugstr_guid(riid), ppvObject);
756
757     if(!This || !ppvObject)
758         return E_INVALIDARG;
759
760     if(IsEqualIID(&IID_IUnknown, riid)) {
761         TRACE("(%p)->(IID_IUnknown %p)\n", This, ppvObject);
762     }else if(IsEqualIID(&IID_IInternetZoneManager, riid)) {
763         TRACE("(%p)->(IID_InternetZoneManager %p)\n", This, ppvObject);
764     }else if(IsEqualIID(&IID_IInternetZoneManagerEx, riid)) {
765         TRACE("(%p)->(IID_InternetZoneManagerEx %p)\n", This, ppvObject);
766     }else if(IsEqualIID(&IID_IInternetZoneManagerEx2, riid)) {
767         TRACE("(%p)->(IID_InternetZoneManagerEx2 %p)\n", This, ppvObject);
768     }
769     else
770     {
771         FIXME("Unknown interface: %s\n", debugstr_guid(riid));
772         *ppvObject = NULL;
773         return E_NOINTERFACE;
774     }
775
776     *ppvObject = iface;
777     IInternetZoneManager_AddRef(iface);
778     return S_OK;
779 }
780
781 /********************************************************************
782  *      IInternetZoneManager_AddRef
783  */
784 static ULONG WINAPI ZoneMgrImpl_AddRef(IInternetZoneManagerEx2* iface)
785 {
786     ZoneMgrImpl* This = (ZoneMgrImpl*)iface;
787     ULONG refCount = InterlockedIncrement(&This->ref);
788
789     TRACE("(%p)->(ref before=%u)\n",This, refCount - 1);
790
791     return refCount;
792 }
793
794 /********************************************************************
795  *      IInternetZoneManager_Release
796  */
797 static ULONG WINAPI ZoneMgrImpl_Release(IInternetZoneManagerEx2* iface)
798 {
799     ZoneMgrImpl* This = (ZoneMgrImpl*)iface;
800     ULONG refCount = InterlockedDecrement(&This->ref);
801
802     TRACE("(%p)->(ref before=%u)\n",This, refCount + 1);
803
804     if(!refCount) {
805         while (This->zonemap_count) heap_free(This->zonemaps[--This->zonemap_count]);
806         heap_free(This->zonemaps);
807         heap_free(This);
808         URLMON_UnlockModule();
809     }
810     
811     return refCount;
812 }
813
814 /********************************************************************
815  *      IInternetZoneManager_GetZoneAttributes
816  */
817 static HRESULT WINAPI ZoneMgrImpl_GetZoneAttributes(IInternetZoneManagerEx2* iface,
818                                                     DWORD dwZone,
819                                                     ZONEATTRIBUTES* pZoneAttributes)
820 {
821     ZoneMgrImpl* This = (ZoneMgrImpl*)iface;
822     HRESULT hr;
823     HKEY hcu;
824     HKEY hklm = NULL;
825
826     TRACE("(%p)->(%d %p)\n", This, dwZone, pZoneAttributes);
827
828     if (!pZoneAttributes)
829         return E_INVALIDARG;
830
831     hr = open_zone_key(HKEY_CURRENT_USER, dwZone, &hcu);
832     if (FAILED(hr))
833         return S_OK;  /* IE6 and older returned E_FAIL here */
834
835     hr = open_zone_key(HKEY_LOCAL_MACHINE, dwZone, &hklm);
836     if (FAILED(hr))
837         TRACE("Zone %d not in HKLM\n", dwZone);
838
839     get_string_from_reg(hcu, hklm, displaynameW, pZoneAttributes->szDisplayName, MAX_ZONE_PATH);
840     get_string_from_reg(hcu, hklm, descriptionW, pZoneAttributes->szDescription, MAX_ZONE_DESCRIPTION);
841     get_string_from_reg(hcu, hklm, iconW, pZoneAttributes->szIconPath, MAX_ZONE_PATH);
842     get_dword_from_reg(hcu, hklm, minlevelW, &pZoneAttributes->dwTemplateMinLevel);
843     get_dword_from_reg(hcu, hklm, currentlevelW, &pZoneAttributes->dwTemplateCurrentLevel);
844     get_dword_from_reg(hcu, hklm, recommendedlevelW, &pZoneAttributes->dwTemplateRecommended);
845     get_dword_from_reg(hcu, hklm, flagsW, &pZoneAttributes->dwFlags);
846
847     RegCloseKey(hklm);
848     RegCloseKey(hcu);
849     return S_OK;
850 }
851
852 /********************************************************************
853  *      IInternetZoneManager_SetZoneAttributes
854  */
855 static HRESULT WINAPI ZoneMgrImpl_SetZoneAttributes(IInternetZoneManagerEx2* iface,
856                                                     DWORD dwZone,
857                                                     ZONEATTRIBUTES* pZoneAttributes)
858 {
859     FIXME("(%p)->(%08x %p) stub\n", iface, dwZone, pZoneAttributes);
860     return E_NOTIMPL;
861 }
862
863 /********************************************************************
864  *      IInternetZoneManager_GetZoneCustomPolicy
865  */
866 static HRESULT WINAPI ZoneMgrImpl_GetZoneCustomPolicy(IInternetZoneManagerEx2* iface,
867                                                       DWORD dwZone,
868                                                       REFGUID guidKey,
869                                                       BYTE** ppPolicy,
870                                                       DWORD* pcbPolicy,
871                                                       URLZONEREG ulrZoneReg)
872 {
873     FIXME("(%p)->(%08x %s %p %p %08x) stub\n", iface, dwZone, debugstr_guid(guidKey),
874                                                     ppPolicy, pcbPolicy, ulrZoneReg);
875     return E_NOTIMPL;
876 }
877
878 /********************************************************************
879  *      IInternetZoneManager_SetZoneCustomPolicy
880  */
881 static HRESULT WINAPI ZoneMgrImpl_SetZoneCustomPolicy(IInternetZoneManagerEx2* iface,
882                                                       DWORD dwZone,
883                                                       REFGUID guidKey,
884                                                       BYTE* ppPolicy,
885                                                       DWORD cbPolicy,
886                                                       URLZONEREG ulrZoneReg)
887 {
888     FIXME("(%p)->(%08x %s %p %08x %08x) stub\n", iface, dwZone, debugstr_guid(guidKey),
889                                                     ppPolicy, cbPolicy, ulrZoneReg);
890     return E_NOTIMPL;
891 }
892
893 /********************************************************************
894  *      IInternetZoneManager_GetZoneActionPolicy
895  */
896 static HRESULT WINAPI ZoneMgrImpl_GetZoneActionPolicy(IInternetZoneManagerEx2* iface,
897         DWORD dwZone, DWORD dwAction, BYTE* pPolicy, DWORD cbPolicy, URLZONEREG urlZoneReg)
898 {
899     TRACE("(%p)->(%d %08x %p %d %d)\n", iface, dwZone, dwAction, pPolicy,
900             cbPolicy, urlZoneReg);
901
902     if(!pPolicy)
903         return E_INVALIDARG;
904
905     return get_action_policy(dwZone, dwAction, pPolicy, cbPolicy, urlZoneReg);
906 }
907
908 /********************************************************************
909  *      IInternetZoneManager_SetZoneActionPolicy
910  */
911 static HRESULT WINAPI ZoneMgrImpl_SetZoneActionPolicy(IInternetZoneManagerEx2* iface,
912                                                       DWORD dwZone,
913                                                       DWORD dwAction,
914                                                       BYTE* pPolicy,
915                                                       DWORD cbPolicy,
916                                                       URLZONEREG urlZoneReg)
917 {
918     FIXME("(%p)->(%08x %08x %p %08x %08x) stub\n", iface, dwZone, dwAction, pPolicy,
919                                                        cbPolicy, urlZoneReg);
920     return E_NOTIMPL;
921 }
922
923 /********************************************************************
924  *      IInternetZoneManager_PromptAction
925  */
926 static HRESULT WINAPI ZoneMgrImpl_PromptAction(IInternetZoneManagerEx2* iface,
927                                                DWORD dwAction,
928                                                HWND hwndParent,
929                                                LPCWSTR pwszUrl,
930                                                LPCWSTR pwszText,
931                                                DWORD dwPromptFlags)
932 {
933     FIXME("%p %08x %p %s %s %08x\n", iface, dwAction, hwndParent,
934           debugstr_w(pwszUrl), debugstr_w(pwszText), dwPromptFlags );
935     return E_NOTIMPL;
936 }
937
938 /********************************************************************
939  *      IInternetZoneManager_LogAction
940  */
941 static HRESULT WINAPI ZoneMgrImpl_LogAction(IInternetZoneManagerEx2* iface,
942                                             DWORD dwAction,
943                                             LPCWSTR pwszUrl,
944                                             LPCWSTR pwszText,
945                                             DWORD dwLogFlags)
946 {
947     FIXME("(%p)->(%08x %s %s %08x) stub\n", iface, dwAction, debugstr_w(pwszUrl),
948                                               debugstr_w(pwszText), dwLogFlags);
949     return E_NOTIMPL;
950 }
951
952 /********************************************************************
953  *      IInternetZoneManager_CreateZoneEnumerator
954  */
955 static HRESULT WINAPI ZoneMgrImpl_CreateZoneEnumerator(IInternetZoneManagerEx2* iface,
956                                                        DWORD* pdwEnum,
957                                                        DWORD* pdwCount,
958                                                        DWORD dwFlags)
959 {
960     ZoneMgrImpl* This = (ZoneMgrImpl*)iface;
961     LPDWORD * new_maps;
962     LPDWORD data;
963     DWORD i;
964
965     TRACE("(%p)->(%p, %p, 0x%08x)\n", This, pdwEnum, pdwCount, dwFlags);
966     if (!pdwEnum || !pdwCount || (dwFlags != 0))
967         return E_INVALIDARG;
968
969     data = build_zonemap_from_reg();
970     TRACE("found %d zones\n", data ? data[0] : -1);
971
972     if (!data)
973         return E_FAIL;
974
975     for (i = 0; i < This->zonemap_count; i++) {
976         if (This->zonemaps && !This->zonemaps[i]) {
977             This->zonemaps[i] = data;
978             *pdwEnum = i;
979             *pdwCount = data[0];
980             return S_OK;
981         }
982     }
983
984     if (This->zonemaps) {
985         /* try to double the nr. of pointers in the array */
986         new_maps = heap_realloc_zero(This->zonemaps, This->zonemap_count * 2 * sizeof(LPDWORD));
987         if (new_maps)
988             This->zonemap_count *= 2;
989     }
990     else
991     {
992         This->zonemap_count = 2;
993         new_maps = heap_alloc_zero(This->zonemap_count * sizeof(LPDWORD));
994     }
995
996     if (!new_maps) {
997         heap_free(data);
998         return E_FAIL;
999     }
1000     This->zonemaps = new_maps;
1001     This->zonemaps[i] = data;
1002     *pdwEnum = i;
1003     *pdwCount = data[0];
1004     return S_OK;
1005 }
1006
1007 /********************************************************************
1008  *      IInternetZoneManager_GetZoneAt
1009  */
1010 static HRESULT WINAPI ZoneMgrImpl_GetZoneAt(IInternetZoneManagerEx2* iface,
1011                                             DWORD dwEnum,
1012                                             DWORD dwIndex,
1013                                             DWORD* pdwZone)
1014 {
1015     ZoneMgrImpl* This = (ZoneMgrImpl*)iface;
1016     LPDWORD data;
1017
1018     TRACE("(%p)->(0x%08x, %d, %p)\n", This, dwEnum, dwIndex, pdwZone);
1019
1020     /* make sure, that dwEnum and dwIndex are in the valid range */
1021     if (dwEnum < This->zonemap_count) {
1022         if ((data = This->zonemaps[dwEnum])) {
1023             if (dwIndex < data[0]) {
1024                 *pdwZone = data[dwIndex + 1];
1025                 return S_OK;
1026             }
1027         }
1028     }
1029     return E_INVALIDARG;
1030 }
1031
1032 /********************************************************************
1033  *      IInternetZoneManager_DestroyZoneEnumerator
1034  */
1035 static HRESULT WINAPI ZoneMgrImpl_DestroyZoneEnumerator(IInternetZoneManagerEx2* iface,
1036                                                         DWORD dwEnum)
1037 {
1038     ZoneMgrImpl* This = (ZoneMgrImpl*)iface;
1039     LPDWORD data;
1040
1041     TRACE("(%p)->(0x%08x)\n", This, dwEnum);
1042     /* make sure, that dwEnum is valid */
1043     if (dwEnum < This->zonemap_count) {
1044         if ((data = This->zonemaps[dwEnum])) {
1045             This->zonemaps[dwEnum] = NULL;
1046             heap_free(data);
1047             return S_OK;
1048         }
1049     }
1050     return E_INVALIDARG;
1051 }
1052
1053 /********************************************************************
1054  *      IInternetZoneManager_CopyTemplatePoliciesToZone
1055  */
1056 static HRESULT WINAPI ZoneMgrImpl_CopyTemplatePoliciesToZone(IInternetZoneManagerEx2* iface,
1057                                                              DWORD dwTemplate,
1058                                                              DWORD dwZone,
1059                                                              DWORD dwReserved)
1060 {
1061     FIXME("(%p)->(%08x %08x %08x) stub\n", iface, dwTemplate, dwZone, dwReserved);
1062     return E_NOTIMPL;
1063 }
1064
1065 /********************************************************************
1066  *      IInternetZoneManagerEx_GetZoneActionPolicyEx
1067  */
1068 static HRESULT WINAPI ZoneMgrImpl_GetZoneActionPolicyEx(IInternetZoneManagerEx2* iface,
1069                                                         DWORD dwZone,
1070                                                         DWORD dwAction,
1071                                                         BYTE* pPolicy,
1072                                                         DWORD cbPolicy,
1073                                                         URLZONEREG urlZoneReg,
1074                                                         DWORD dwFlags)
1075 {
1076     TRACE("(%p)->(%d, 0x%x, %p, %d, %d, 0x%x)\n", iface, dwZone,
1077             dwAction, pPolicy, cbPolicy, urlZoneReg, dwFlags);
1078
1079     if(!pPolicy)
1080         return E_INVALIDARG;
1081
1082     if (dwFlags)
1083         FIXME("dwFlags 0x%x ignored\n", dwFlags);
1084
1085     return get_action_policy(dwZone, dwAction, pPolicy, cbPolicy, urlZoneReg);
1086 }
1087
1088 /********************************************************************
1089  *      IInternetZoneManagerEx_SetZoneActionPolicyEx
1090  */
1091 static HRESULT WINAPI ZoneMgrImpl_SetZoneActionPolicyEx(IInternetZoneManagerEx2* iface,
1092                                                         DWORD dwZone,
1093                                                         DWORD dwAction,
1094                                                         BYTE* pPolicy,
1095                                                         DWORD cbPolicy,
1096                                                         URLZONEREG urlZoneReg,
1097                                                         DWORD dwFlags)
1098 {
1099     FIXME("(%p)->(%d, 0x%x, %p, %d, %d, 0x%x) stub\n", iface, dwZone, dwAction, pPolicy,
1100                                                        cbPolicy, urlZoneReg, dwFlags);
1101     return E_NOTIMPL;
1102 }
1103
1104 /********************************************************************
1105  *      IInternetZoneManagerEx2_GetZoneAttributesEx
1106  */
1107 static HRESULT WINAPI ZoneMgrImpl_GetZoneAttributesEx(IInternetZoneManagerEx2* iface,
1108                                                       DWORD dwZone,
1109                                                       ZONEATTRIBUTES* pZoneAttributes,
1110                                                       DWORD dwFlags)
1111 {
1112     TRACE("(%p)->(%d, %p, 0x%x)\n", iface, dwZone, pZoneAttributes, dwFlags);
1113
1114     if (dwFlags)
1115         FIXME("dwFlags 0x%x ignored\n", dwFlags);
1116
1117     return IInternetZoneManager_GetZoneAttributes(iface, dwZone, pZoneAttributes);
1118 }
1119
1120
1121 /********************************************************************
1122  *      IInternetZoneManagerEx2_GetZoneSecurityState
1123  */
1124 static HRESULT WINAPI ZoneMgrImpl_GetZoneSecurityState(IInternetZoneManagerEx2* iface,
1125                                                        DWORD dwZoneIndex,
1126                                                        BOOL fRespectPolicy,
1127                                                        LPDWORD pdwState,
1128                                                        BOOL *pfPolicyEncountered)
1129 {
1130     FIXME("(%p)->(%d, %d, %p, %p) stub\n", iface, dwZoneIndex, fRespectPolicy,
1131                                            pdwState, pfPolicyEncountered);
1132
1133     *pdwState = SECURITY_IE_STATE_GREEN;
1134
1135     if (pfPolicyEncountered)
1136         *pfPolicyEncountered = FALSE;
1137
1138     return S_OK;
1139 }
1140
1141 /********************************************************************
1142  *      IInternetZoneManagerEx2_GetIESecurityState
1143  */
1144 static HRESULT WINAPI ZoneMgrImpl_GetIESecurityState(IInternetZoneManagerEx2* iface,
1145                                                      BOOL fRespectPolicy,
1146                                                      LPDWORD pdwState,
1147                                                      BOOL *pfPolicyEncountered,
1148                                                      BOOL fNoCache)
1149 {
1150     FIXME("(%p)->(%d, %p, %p, %d) stub\n", iface, fRespectPolicy, pdwState,
1151                                            pfPolicyEncountered, fNoCache);
1152
1153     *pdwState = SECURITY_IE_STATE_GREEN;
1154
1155     if (pfPolicyEncountered)
1156         *pfPolicyEncountered = FALSE;
1157
1158     return S_OK;
1159 }
1160
1161 /********************************************************************
1162  *      IInternetZoneManagerEx2_FixInsecureSettings
1163  */
1164 static HRESULT WINAPI ZoneMgrImpl_FixInsecureSettings(IInternetZoneManagerEx2* iface)
1165 {
1166     FIXME("(%p) stub\n", iface);
1167     return S_OK;
1168 }
1169
1170 /********************************************************************
1171  *      IInternetZoneManager_Construct
1172  */
1173 static const IInternetZoneManagerEx2Vtbl ZoneMgrImplVtbl = {
1174     ZoneMgrImpl_QueryInterface,
1175     ZoneMgrImpl_AddRef,
1176     ZoneMgrImpl_Release,
1177     /* IInternetZoneManager */
1178     ZoneMgrImpl_GetZoneAttributes,
1179     ZoneMgrImpl_SetZoneAttributes,
1180     ZoneMgrImpl_GetZoneCustomPolicy,
1181     ZoneMgrImpl_SetZoneCustomPolicy,
1182     ZoneMgrImpl_GetZoneActionPolicy,
1183     ZoneMgrImpl_SetZoneActionPolicy,
1184     ZoneMgrImpl_PromptAction,
1185     ZoneMgrImpl_LogAction,
1186     ZoneMgrImpl_CreateZoneEnumerator,
1187     ZoneMgrImpl_GetZoneAt,
1188     ZoneMgrImpl_DestroyZoneEnumerator,
1189     ZoneMgrImpl_CopyTemplatePoliciesToZone,
1190     /* IInternetZoneManagerEx */
1191     ZoneMgrImpl_GetZoneActionPolicyEx,
1192     ZoneMgrImpl_SetZoneActionPolicyEx,
1193     /* IInternetZoneManagerEx2 */
1194     ZoneMgrImpl_GetZoneAttributesEx,
1195     ZoneMgrImpl_GetZoneSecurityState,
1196     ZoneMgrImpl_GetIESecurityState,
1197     ZoneMgrImpl_FixInsecureSettings,
1198 };
1199
1200 HRESULT ZoneMgrImpl_Construct(IUnknown *pUnkOuter, LPVOID *ppobj)
1201 {
1202     ZoneMgrImpl* ret = heap_alloc_zero(sizeof(ZoneMgrImpl));
1203
1204     TRACE("(%p %p)\n", pUnkOuter, ppobj);
1205     ret->lpVtbl = &ZoneMgrImplVtbl;
1206     ret->ref = 1;
1207     *ppobj = (IInternetZoneManagerEx*)ret;
1208
1209     URLMON_LockModule();
1210
1211     return S_OK;
1212 }
1213
1214 /***********************************************************************
1215  *           CoInternetCreateSecurityManager (URLMON.@)
1216  *
1217  */
1218 HRESULT WINAPI CoInternetCreateSecurityManager( IServiceProvider *pSP,
1219     IInternetSecurityManager **ppSM, DWORD dwReserved )
1220 {
1221     TRACE("%p %p %d\n", pSP, ppSM, dwReserved );
1222
1223     if(pSP)
1224         FIXME("pSP not supported\n");
1225
1226     return SecManagerImpl_Construct(NULL, (void**) ppSM);
1227 }
1228
1229 /********************************************************************
1230  *      CoInternetCreateZoneManager (URLMON.@)
1231  */
1232 HRESULT WINAPI CoInternetCreateZoneManager(IServiceProvider* pSP, IInternetZoneManager** ppZM, DWORD dwReserved)
1233 {
1234     TRACE("(%p %p %x)\n", pSP, ppZM, dwReserved);
1235     return ZoneMgrImpl_Construct(NULL, (void**)ppZM);
1236 }
1237
1238 /********************************************************************
1239  *      CoInternetGetSecurityUrl (URLMON.@)
1240  */
1241 HRESULT WINAPI CoInternetGetSecurityUrl(LPCWSTR pwzUrl, LPWSTR *ppwzSecUrl, PSUACTION psuAction, DWORD dwReserved)
1242 {
1243     WCHAR buf1[INTERNET_MAX_URL_LENGTH], buf2[INTERNET_MAX_URL_LENGTH];
1244     LPWSTR url, domain;
1245     DWORD len;
1246     HRESULT hres;
1247
1248     TRACE("(%p,%p,%u,%u)\n", pwzUrl, ppwzSecUrl, psuAction, dwReserved);
1249
1250     url = buf1;
1251     domain = buf2;
1252     strcpyW(url, pwzUrl);
1253
1254     while(1) {
1255         hres = CoInternetParseUrl(url, PARSE_SECURITY_URL, 0, domain, INTERNET_MAX_URL_LENGTH, &len, 0);
1256         if(hres!=S_OK || !strcmpW(url, domain))
1257             break;
1258
1259         if(url == buf1) {
1260             url = buf2;
1261             domain = buf1;
1262         } else {
1263             url = buf1;
1264             domain = buf2;
1265         }
1266     }
1267
1268     if(psuAction==PSU_SECURITY_URL_ONLY) {
1269         len = lstrlenW(url)+1;
1270         *ppwzSecUrl = CoTaskMemAlloc(len*sizeof(WCHAR));
1271         if(!*ppwzSecUrl)
1272             return E_OUTOFMEMORY;
1273
1274         memcpy(*ppwzSecUrl, url, len*sizeof(WCHAR));
1275         return S_OK;
1276     }
1277
1278     hres = CoInternetParseUrl(url, PARSE_SECURITY_DOMAIN, 0, domain,
1279             INTERNET_MAX_URL_LENGTH, &len, 0);
1280     if(SUCCEEDED(hres)) {
1281         len++;
1282         *ppwzSecUrl = CoTaskMemAlloc(len*sizeof(WCHAR));
1283         if(!*ppwzSecUrl)
1284             return E_OUTOFMEMORY;
1285
1286         memcpy(*ppwzSecUrl, domain, len*sizeof(WCHAR));
1287         return S_OK;
1288     }
1289
1290     hres = CoInternetParseUrl(url, PARSE_SCHEMA, 0, domain,
1291             INTERNET_MAX_URL_LENGTH, &len, 0);
1292     if(hres == S_OK){
1293         const WCHAR fileW[] = {'f','i','l','e',0};
1294         if(!strcmpW(domain, fileW)){
1295             hres = CoInternetParseUrl(url, PARSE_ROOTDOCUMENT, 0, domain, INTERNET_MAX_URL_LENGTH, &len, 0);
1296         }else{
1297             domain[len] = ':';
1298             hres = CoInternetParseUrl(url, PARSE_DOMAIN, 0, domain+len+1,
1299                     INTERNET_MAX_URL_LENGTH-len-1, &len, 0);
1300             if(hres == S_OK) {
1301                 len = lstrlenW(domain)+1;
1302                 *ppwzSecUrl = CoTaskMemAlloc(len*sizeof(WCHAR));
1303                 if(!*ppwzSecUrl)
1304                     return E_OUTOFMEMORY;
1305
1306                 memcpy(*ppwzSecUrl, domain, len*sizeof(WCHAR));
1307                 return S_OK;
1308             }
1309         }
1310     }else
1311         return hres;
1312
1313     len = lstrlenW(url)+1;
1314     *ppwzSecUrl = CoTaskMemAlloc(len*sizeof(WCHAR));
1315     if(!*ppwzSecUrl)
1316         return E_OUTOFMEMORY;
1317
1318     memcpy(*ppwzSecUrl, url, len*sizeof(WCHAR));
1319     return S_OK;
1320 }