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