c2man: Fix .spec file flag recognition.
[wine/testsucceed.git] / dlls / urlmon / sec_mgr.c
blob562680fc7e61a820f72142f5c96b99639fa7fa48
1 /*
2 * Internet Security and Zone Manager
4 * Copyright (c) 2004 Huw D M Davies
5 * Copyright 2004 Jacek Caban
6 * Copyright 2009 Detlef Riekenberg
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.
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.
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
23 #include <stdio.h>
25 #include "urlmon_main.h"
26 #include "winreg.h"
27 #include "wininet.h"
29 #define NO_SHLWAPI_REG
30 #include "shlwapi.h"
32 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(urlmon);
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};
52 /********************************************************************
53 * get_string_from_reg [internal]
55 * helper to get a string from the reg.
58 static void get_string_from_reg(HKEY hcu, HKEY hklm, LPCWSTR name, LPWSTR out, DWORD maxlen)
60 DWORD type = REG_SZ;
61 DWORD len = maxlen * sizeof(WCHAR);
62 DWORD res;
64 res = RegQueryValueExW(hcu, name, NULL, &type, (LPBYTE) out, &len);
66 if (res && hklm) {
67 len = maxlen * sizeof(WCHAR);
68 type = REG_SZ;
69 res = RegQueryValueExW(hklm, name, NULL, &type, (LPBYTE) out, &len);
72 if (res) {
73 TRACE("%s failed: %d\n", debugstr_w(name), res);
74 *out = '\0';
78 /********************************************************************
79 * get_dword_from_reg [internal]
81 * helper to get a dword from the reg.
84 static void get_dword_from_reg(HKEY hcu, HKEY hklm, LPCWSTR name, LPDWORD out)
86 DWORD type = REG_DWORD;
87 DWORD len = sizeof(DWORD);
88 DWORD res;
90 res = RegQueryValueExW(hcu, name, NULL, &type, (LPBYTE) out, &len);
92 if (res && hklm) {
93 len = sizeof(DWORD);
94 type = REG_DWORD;
95 res = RegQueryValueExW(hklm, name, NULL, &type, (LPBYTE) out, &len);
98 if (res) {
99 TRACE("%s failed: %d\n", debugstr_w(name), res);
100 *out = 0;
104 static HRESULT get_zone_from_reg(LPCWSTR schema, DWORD *zone)
106 DWORD res, size;
107 HKEY hkey;
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};
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;
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;
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;
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;
142 *zone = 3;
143 return S_OK;
146 static HRESULT map_url_to_zone(LPCWSTR url, DWORD *zone, LPWSTR *ret_url)
148 LPWSTR secur_url;
149 WCHAR schema[64];
150 DWORD size=0;
151 HRESULT hres;
153 *zone = URLZONE_INVALID;
155 hres = CoInternetGetSecurityUrl(url, &secur_url, PSU_SECURITY_URL_ONLY, 0);
156 if(hres != S_OK) {
157 size = strlenW(url)*sizeof(WCHAR);
159 secur_url = heap_alloc(size);
160 if(!secur_url)
161 return E_OUTOFMEMORY;
163 memcpy(secur_url, url, size);
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;
172 /* file protocol is a special case */
173 if(!strcmpW(schema, fileW)) {
174 WCHAR path[MAX_PATH], root[20];
175 WCHAR *ptr;
177 hres = CoInternetParseUrl(secur_url, PARSE_PATH_FROM_URL, 0, path,
178 sizeof(path)/sizeof(WCHAR), &size, 0);
180 if(SUCCEEDED(hres) && (ptr = strchrW(path, '\\')) && ptr-path < sizeof(root)/sizeof(WCHAR)) {
181 UINT type;
183 memcpy(root, path, (ptr-path)*sizeof(WCHAR));
184 root[ptr-path] = 0;
186 type = GetDriveTypeW(root);
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);
209 if(*zone == URLZONE_INVALID) {
210 WARN("domains are not yet implemented\n");
211 hres = get_zone_from_reg(schema, zone);
214 if(FAILED(hres) || !ret_url)
215 heap_free(secur_url);
216 else
217 *ret_url = secur_url;
219 return hres;
222 static HRESULT open_zone_key(HKEY parent_key, DWORD zone, HKEY *hkey)
224 static const WCHAR wszFormat[] = {'%','s','%','u',0};
226 WCHAR key_name[sizeof(wszZonesKey)/sizeof(WCHAR)+12];
227 DWORD res;
229 wsprintfW(key_name, wszFormat, wszZonesKey, zone);
231 res = RegOpenKeyW(parent_key, key_name, hkey);
233 if(res != ERROR_SUCCESS) {
234 WARN("RegOpenKey failed\n");
235 return E_INVALIDARG;
238 return S_OK;
241 static HRESULT get_action_policy(DWORD zone, DWORD action, BYTE *policy, DWORD size, URLZONEREG zone_reg)
243 HKEY parent_key;
244 HKEY hkey;
245 LONG res;
246 HRESULT hres;
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;
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;
268 hres = open_zone_key(parent_key, zone, &hkey);
269 if(SUCCEEDED(hres)) {
270 WCHAR action_str[16];
271 DWORD len = size;
273 static const WCHAR formatW[] = {'%','X',0};
275 wsprintfW(action_str, formatW, action);
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;
287 RegCloseKey(hkey);
290 if(FAILED(hres) && zone_reg == URLZONEREG_DEFAULT)
291 return get_action_policy(zone, action, policy, size, URLZONEREG_HKLM);
293 return hres;
296 /***********************************************************************
297 * InternetSecurityManager implementation
300 typedef struct {
301 IInternetSecurityManager IInternetSecurityManager_iface;
303 LONG ref;
305 IInternetSecurityMgrSite *mgrsite;
306 IInternetSecurityManager *custom_manager;
307 } SecManagerImpl;
309 static inline SecManagerImpl *impl_from_IInternetSecurityManager(IInternetSecurityManager *iface)
311 return CONTAINING_RECORD(iface, SecManagerImpl, IInternetSecurityManager_iface);
314 static HRESULT WINAPI SecManagerImpl_QueryInterface(IInternetSecurityManager* iface,REFIID riid,void** ppvObject)
316 SecManagerImpl *This = impl_from_IInternetSecurityManager(iface);
318 TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppvObject);
320 /* Perform a sanity check on the parameters.*/
321 if ( (This==0) || (ppvObject==0) )
322 return E_INVALIDARG;
324 /* Initialize the return parameter */
325 *ppvObject = 0;
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;
332 /* Check that we obtained an interface.*/
333 if (!*ppvObject) {
334 WARN("not supported interface %s\n", debugstr_guid(riid));
335 return E_NOINTERFACE;
338 /* Query Interface always increases the reference count by one when it is successful */
339 IInternetSecurityManager_AddRef(iface);
341 return S_OK;
344 static ULONG WINAPI SecManagerImpl_AddRef(IInternetSecurityManager* iface)
346 SecManagerImpl *This = impl_from_IInternetSecurityManager(iface);
347 ULONG refCount = InterlockedIncrement(&This->ref);
349 TRACE("(%p) ref=%u\n", This, refCount);
351 return refCount;
354 static ULONG WINAPI SecManagerImpl_Release(IInternetSecurityManager* iface)
356 SecManagerImpl *This = impl_from_IInternetSecurityManager(iface);
357 ULONG refCount = InterlockedDecrement(&This->ref);
359 TRACE("(%p) ref=%u\n", This, refCount);
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);
368 heap_free(This);
370 URLMON_UnlockModule();
373 return refCount;
376 static HRESULT WINAPI SecManagerImpl_SetSecuritySite(IInternetSecurityManager *iface,
377 IInternetSecurityMgrSite *pSite)
379 SecManagerImpl *This = impl_from_IInternetSecurityManager(iface);
381 TRACE("(%p)->(%p)\n", This, pSite);
383 if(This->mgrsite)
384 IInternetSecurityMgrSite_Release(This->mgrsite);
386 if(This->custom_manager) {
387 IInternetSecurityManager_Release(This->custom_manager);
388 This->custom_manager = NULL;
391 This->mgrsite = pSite;
393 if(pSite) {
394 IServiceProvider *servprov;
395 HRESULT hres;
397 IInternetSecurityMgrSite_AddRef(pSite);
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);
408 return S_OK;
411 static HRESULT WINAPI SecManagerImpl_GetSecuritySite(IInternetSecurityManager *iface,
412 IInternetSecurityMgrSite **ppSite)
414 SecManagerImpl *This = impl_from_IInternetSecurityManager(iface);
416 TRACE("(%p)->(%p)\n", This, ppSite);
418 if(!ppSite)
419 return E_INVALIDARG;
421 if(This->mgrsite)
422 IInternetSecurityMgrSite_AddRef(This->mgrsite);
424 *ppSite = This->mgrsite;
425 return S_OK;
428 static HRESULT WINAPI SecManagerImpl_MapUrlToZone(IInternetSecurityManager *iface,
429 LPCWSTR pwszUrl, DWORD *pdwZone,
430 DWORD dwFlags)
432 SecManagerImpl *This = impl_from_IInternetSecurityManager(iface);
433 HRESULT hres;
435 TRACE("(%p)->(%s %p %08x)\n", iface, debugstr_w(pwszUrl), pdwZone, dwFlags);
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;
444 if(!pwszUrl) {
445 *pdwZone = URLZONE_INVALID;
446 return E_INVALIDARG;
449 if(dwFlags)
450 FIXME("not supported flags: %08x\n", dwFlags);
452 return map_url_to_zone(pwszUrl, pdwZone, NULL);
455 static HRESULT WINAPI SecManagerImpl_GetSecurityId(IInternetSecurityManager *iface,
456 LPCWSTR pwszUrl, BYTE *pbSecurityId, DWORD *pcbSecurityId, DWORD_PTR dwReserved)
458 SecManagerImpl *This = impl_from_IInternetSecurityManager(iface);
459 LPWSTR url, ptr, ptr2;
460 DWORD zone, len;
461 HRESULT hres;
463 static const WCHAR wszFile[] = {'f','i','l','e',':'};
465 TRACE("(%p)->(%s %p %p %08lx)\n", iface, debugstr_w(pwszUrl), pbSecurityId,
466 pcbSecurityId, dwReserved);
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;
475 if(!pwszUrl || !pbSecurityId || !pcbSecurityId)
476 return E_INVALIDARG;
478 if(dwReserved)
479 FIXME("dwReserved is not supported\n");
481 hres = map_url_to_zone(pwszUrl, &zone, &url);
482 if(FAILED(hres))
483 return hres == 0x80041001 ? E_INVALIDARG : hres;
485 /* file protocol is a special case */
486 if(strlenW(url) >= sizeof(wszFile)/sizeof(WCHAR)
487 && !memcmp(url, wszFile, sizeof(wszFile)) && strchrW(url, '\\')) {
489 static const BYTE secidFile[] = {'f','i','l','e',':'};
491 heap_free(url);
493 if(*pcbSecurityId < sizeof(secidFile)+sizeof(zone))
494 return HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
496 memcpy(pbSecurityId, secidFile, sizeof(secidFile));
497 *(DWORD*)(pbSecurityId+sizeof(secidFile)) = zone;
499 *pcbSecurityId = sizeof(secidFile)+sizeof(zone);
500 return S_OK;
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));
510 ptr = strchrW(ptr, '/');
511 if(ptr)
512 *ptr = 0;
514 len = WideCharToMultiByte(CP_ACP, 0, url, -1, NULL, 0, NULL, NULL)-1;
516 if(len+sizeof(DWORD) > *pcbSecurityId) {
517 heap_free(url);
518 return HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
521 WideCharToMultiByte(CP_ACP, 0, url, -1, (LPSTR)pbSecurityId, len, NULL, NULL);
522 heap_free(url);
524 *(DWORD*)(pbSecurityId+len) = zone;
526 *pcbSecurityId = len+sizeof(DWORD);
528 return S_OK;
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)
538 SecManagerImpl *This = impl_from_IInternetSecurityManager(iface);
539 DWORD zone, policy;
540 HRESULT hres;
542 TRACE("(%p)->(%s %08x %p %08x %p %08x %08x %08x)\n", iface, debugstr_w(pwszUrl), dwAction,
543 pPolicy, cbPolicy, pContext, cbContext, dwFlags, dwReserved);
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;
552 if(dwFlags || dwReserved)
553 FIXME("Unsupported arguments\n");
555 if(!pwszUrl)
556 return E_INVALIDARG;
558 hres = map_url_to_zone(pwszUrl, &zone, NULL);
559 if(FAILED(hres))
560 return hres;
562 hres = get_action_policy(zone, dwAction, (BYTE*)&policy, sizeof(policy), URLZONEREG_DEFAULT);
563 if(FAILED(hres))
564 return hres;
566 TRACE("policy %x\n", policy);
567 if(cbPolicy >= sizeof(DWORD))
568 *(DWORD*)pPolicy = policy;
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);
583 return E_FAIL;
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)
593 SecManagerImpl *This = impl_from_IInternetSecurityManager(iface);
594 HRESULT hres;
596 TRACE("(%p)->(%s %s %p %p %p %08x %08x )\n", iface, debugstr_w(pwszUrl), debugstr_guid(guidKey),
597 ppPolicy, pcbPolicy, pContext, cbContext, dwReserved);
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;
606 WARN("Unknown guidKey %s\n", debugstr_guid(guidKey));
607 return HRESULT_FROM_WIN32(ERROR_NOT_FOUND);
610 static HRESULT WINAPI SecManagerImpl_SetZoneMapping(IInternetSecurityManager *iface,
611 DWORD dwZone, LPCWSTR pwszPattern, DWORD dwFlags)
613 SecManagerImpl *This = impl_from_IInternetSecurityManager(iface);
614 HRESULT hres;
616 TRACE("(%p)->(%08x %s %08x)\n", iface, dwZone, debugstr_w(pwszPattern),dwFlags);
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;
625 FIXME("Default action is not implemented\n");
626 return E_NOTIMPL;
629 static HRESULT WINAPI SecManagerImpl_GetZoneMappings(IInternetSecurityManager *iface,
630 DWORD dwZone, IEnumString **ppenumString, DWORD dwFlags)
632 SecManagerImpl *This = impl_from_IInternetSecurityManager(iface);
633 HRESULT hres;
635 TRACE("(%p)->(%08x %p %08x)\n", iface, dwZone, ppenumString,dwFlags);
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;
644 FIXME("Default action is not implemented\n");
645 return E_NOTIMPL;
648 static const IInternetSecurityManagerVtbl VT_SecManagerImpl =
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
663 HRESULT SecManagerImpl_Construct(IUnknown *pUnkOuter, LPVOID *ppobj)
665 SecManagerImpl *This;
667 TRACE("(%p,%p)\n",pUnkOuter,ppobj);
668 This = heap_alloc(sizeof(*This));
670 /* Initialize the virtual function table. */
671 This->IInternetSecurityManager_iface.lpVtbl = &VT_SecManagerImpl;
673 This->ref = 1;
674 This->mgrsite = NULL;
675 This->custom_manager = NULL;
677 *ppobj = This;
679 URLMON_LockModule();
681 return S_OK;
684 /***********************************************************************
685 * InternetZoneManager implementation
688 typedef struct {
689 IInternetZoneManagerEx2 IInternetZoneManagerEx2_iface;
690 LONG ref;
691 LPDWORD *zonemaps;
692 DWORD zonemap_count;
693 } ZoneMgrImpl;
695 static inline ZoneMgrImpl *impl_from_IInternetZoneManagerEx2(IInternetZoneManagerEx2 *iface)
697 return CONTAINING_RECORD(iface, ZoneMgrImpl, IInternetZoneManagerEx2_iface);
701 /***********************************************************************
702 * build_zonemap_from_reg [internal]
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]
707 static LPDWORD build_zonemap_from_reg(void)
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;
718 res = RegOpenKeyW(HKEY_CURRENT_USER, wszZonesKey, &hkey);
719 if (res)
720 return NULL;
722 data = heap_alloc(allocated * sizeof(DWORD));
723 if (!data)
724 goto cleanup;
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);
731 if (!res) {
732 used++;
733 if (used == allocated) {
734 LPDWORD new_data;
736 allocated *= 2;
737 new_data = heap_realloc_zero(data, allocated * sizeof(DWORD));
738 if (!new_data)
739 goto cleanup;
741 data = new_data;
743 data[used] = atoiW(name);
746 if (used) {
747 RegCloseKey(hkey);
748 data[0] = used;
749 return data;
752 cleanup:
753 /* something failed */
754 RegCloseKey(hkey);
755 heap_free(data);
756 return NULL;
759 /********************************************************************
760 * IInternetZoneManager_QueryInterface
762 static HRESULT WINAPI ZoneMgrImpl_QueryInterface(IInternetZoneManagerEx2* iface, REFIID riid, void** ppvObject)
764 ZoneMgrImpl* This = impl_from_IInternetZoneManagerEx2(iface);
766 TRACE("(%p)->(%s,%p)\n", This, debugstr_guid(riid), ppvObject);
768 if(!This || !ppvObject)
769 return E_INVALIDARG;
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);
780 else
782 FIXME("Unknown interface: %s\n", debugstr_guid(riid));
783 *ppvObject = NULL;
784 return E_NOINTERFACE;
787 *ppvObject = iface;
788 IInternetZoneManager_AddRef(iface);
789 return S_OK;
792 /********************************************************************
793 * IInternetZoneManager_AddRef
795 static ULONG WINAPI ZoneMgrImpl_AddRef(IInternetZoneManagerEx2* iface)
797 ZoneMgrImpl* This = impl_from_IInternetZoneManagerEx2(iface);
798 ULONG refCount = InterlockedIncrement(&This->ref);
800 TRACE("(%p)->(ref before=%u)\n",This, refCount - 1);
802 return refCount;
805 /********************************************************************
806 * IInternetZoneManager_Release
808 static ULONG WINAPI ZoneMgrImpl_Release(IInternetZoneManagerEx2* iface)
810 ZoneMgrImpl* This = impl_from_IInternetZoneManagerEx2(iface);
811 ULONG refCount = InterlockedDecrement(&This->ref);
813 TRACE("(%p)->(ref before=%u)\n",This, refCount + 1);
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();
822 return refCount;
825 /********************************************************************
826 * IInternetZoneManager_GetZoneAttributes
828 static HRESULT WINAPI ZoneMgrImpl_GetZoneAttributes(IInternetZoneManagerEx2* iface,
829 DWORD dwZone,
830 ZONEATTRIBUTES* pZoneAttributes)
832 ZoneMgrImpl* This = impl_from_IInternetZoneManagerEx2(iface);
833 HRESULT hr;
834 HKEY hcu;
835 HKEY hklm = NULL;
837 TRACE("(%p)->(%d %p)\n", This, dwZone, pZoneAttributes);
839 if (!pZoneAttributes)
840 return E_INVALIDARG;
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 */
846 hr = open_zone_key(HKEY_LOCAL_MACHINE, dwZone, &hklm);
847 if (FAILED(hr))
848 TRACE("Zone %d not in HKLM\n", dwZone);
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);
858 RegCloseKey(hklm);
859 RegCloseKey(hcu);
860 return S_OK;
863 /********************************************************************
864 * IInternetZoneManager_SetZoneAttributes
866 static HRESULT WINAPI ZoneMgrImpl_SetZoneAttributes(IInternetZoneManagerEx2* iface,
867 DWORD dwZone,
868 ZONEATTRIBUTES* pZoneAttributes)
870 FIXME("(%p)->(%08x %p) stub\n", iface, dwZone, pZoneAttributes);
871 return E_NOTIMPL;
874 /********************************************************************
875 * IInternetZoneManager_GetZoneCustomPolicy
877 static HRESULT WINAPI ZoneMgrImpl_GetZoneCustomPolicy(IInternetZoneManagerEx2* iface,
878 DWORD dwZone,
879 REFGUID guidKey,
880 BYTE** ppPolicy,
881 DWORD* pcbPolicy,
882 URLZONEREG ulrZoneReg)
884 FIXME("(%p)->(%08x %s %p %p %08x) stub\n", iface, dwZone, debugstr_guid(guidKey),
885 ppPolicy, pcbPolicy, ulrZoneReg);
886 return E_NOTIMPL;
889 /********************************************************************
890 * IInternetZoneManager_SetZoneCustomPolicy
892 static HRESULT WINAPI ZoneMgrImpl_SetZoneCustomPolicy(IInternetZoneManagerEx2* iface,
893 DWORD dwZone,
894 REFGUID guidKey,
895 BYTE* ppPolicy,
896 DWORD cbPolicy,
897 URLZONEREG ulrZoneReg)
899 FIXME("(%p)->(%08x %s %p %08x %08x) stub\n", iface, dwZone, debugstr_guid(guidKey),
900 ppPolicy, cbPolicy, ulrZoneReg);
901 return E_NOTIMPL;
904 /********************************************************************
905 * IInternetZoneManager_GetZoneActionPolicy
907 static HRESULT WINAPI ZoneMgrImpl_GetZoneActionPolicy(IInternetZoneManagerEx2* iface,
908 DWORD dwZone, DWORD dwAction, BYTE* pPolicy, DWORD cbPolicy, URLZONEREG urlZoneReg)
910 TRACE("(%p)->(%d %08x %p %d %d)\n", iface, dwZone, dwAction, pPolicy,
911 cbPolicy, urlZoneReg);
913 if(!pPolicy)
914 return E_INVALIDARG;
916 return get_action_policy(dwZone, dwAction, pPolicy, cbPolicy, urlZoneReg);
919 /********************************************************************
920 * IInternetZoneManager_SetZoneActionPolicy
922 static HRESULT WINAPI ZoneMgrImpl_SetZoneActionPolicy(IInternetZoneManagerEx2* iface,
923 DWORD dwZone,
924 DWORD dwAction,
925 BYTE* pPolicy,
926 DWORD cbPolicy,
927 URLZONEREG urlZoneReg)
929 FIXME("(%p)->(%08x %08x %p %08x %08x) stub\n", iface, dwZone, dwAction, pPolicy,
930 cbPolicy, urlZoneReg);
931 return E_NOTIMPL;
934 /********************************************************************
935 * IInternetZoneManager_PromptAction
937 static HRESULT WINAPI ZoneMgrImpl_PromptAction(IInternetZoneManagerEx2* iface,
938 DWORD dwAction,
939 HWND hwndParent,
940 LPCWSTR pwszUrl,
941 LPCWSTR pwszText,
942 DWORD dwPromptFlags)
944 FIXME("%p %08x %p %s %s %08x\n", iface, dwAction, hwndParent,
945 debugstr_w(pwszUrl), debugstr_w(pwszText), dwPromptFlags );
946 return E_NOTIMPL;
949 /********************************************************************
950 * IInternetZoneManager_LogAction
952 static HRESULT WINAPI ZoneMgrImpl_LogAction(IInternetZoneManagerEx2* iface,
953 DWORD dwAction,
954 LPCWSTR pwszUrl,
955 LPCWSTR pwszText,
956 DWORD dwLogFlags)
958 FIXME("(%p)->(%08x %s %s %08x) stub\n", iface, dwAction, debugstr_w(pwszUrl),
959 debugstr_w(pwszText), dwLogFlags);
960 return E_NOTIMPL;
963 /********************************************************************
964 * IInternetZoneManager_CreateZoneEnumerator
966 static HRESULT WINAPI ZoneMgrImpl_CreateZoneEnumerator(IInternetZoneManagerEx2* iface,
967 DWORD* pdwEnum,
968 DWORD* pdwCount,
969 DWORD dwFlags)
971 ZoneMgrImpl* This = impl_from_IInternetZoneManagerEx2(iface);
972 LPDWORD * new_maps;
973 LPDWORD data;
974 DWORD i;
976 TRACE("(%p)->(%p, %p, 0x%08x)\n", This, pdwEnum, pdwCount, dwFlags);
977 if (!pdwEnum || !pdwCount || (dwFlags != 0))
978 return E_INVALIDARG;
980 data = build_zonemap_from_reg();
981 TRACE("found %d zones\n", data ? data[0] : -1);
983 if (!data)
984 return E_FAIL;
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;
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;
1001 else
1003 This->zonemap_count = 2;
1004 new_maps = heap_alloc_zero(This->zonemap_count * sizeof(LPDWORD));
1007 if (!new_maps) {
1008 heap_free(data);
1009 return E_FAIL;
1011 This->zonemaps = new_maps;
1012 This->zonemaps[i] = data;
1013 *pdwEnum = i;
1014 *pdwCount = data[0];
1015 return S_OK;
1018 /********************************************************************
1019 * IInternetZoneManager_GetZoneAt
1021 static HRESULT WINAPI ZoneMgrImpl_GetZoneAt(IInternetZoneManagerEx2* iface,
1022 DWORD dwEnum,
1023 DWORD dwIndex,
1024 DWORD* pdwZone)
1026 ZoneMgrImpl* This = impl_from_IInternetZoneManagerEx2(iface);
1027 LPDWORD data;
1029 TRACE("(%p)->(0x%08x, %d, %p)\n", This, dwEnum, dwIndex, pdwZone);
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;
1040 return E_INVALIDARG;
1043 /********************************************************************
1044 * IInternetZoneManager_DestroyZoneEnumerator
1046 static HRESULT WINAPI ZoneMgrImpl_DestroyZoneEnumerator(IInternetZoneManagerEx2* iface,
1047 DWORD dwEnum)
1049 ZoneMgrImpl* This = impl_from_IInternetZoneManagerEx2(iface);
1050 LPDWORD data;
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;
1061 return E_INVALIDARG;
1064 /********************************************************************
1065 * IInternetZoneManager_CopyTemplatePoliciesToZone
1067 static HRESULT WINAPI ZoneMgrImpl_CopyTemplatePoliciesToZone(IInternetZoneManagerEx2* iface,
1068 DWORD dwTemplate,
1069 DWORD dwZone,
1070 DWORD dwReserved)
1072 FIXME("(%p)->(%08x %08x %08x) stub\n", iface, dwTemplate, dwZone, dwReserved);
1073 return E_NOTIMPL;
1076 /********************************************************************
1077 * IInternetZoneManagerEx_GetZoneActionPolicyEx
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)
1087 TRACE("(%p)->(%d, 0x%x, %p, %d, %d, 0x%x)\n", iface, dwZone,
1088 dwAction, pPolicy, cbPolicy, urlZoneReg, dwFlags);
1090 if(!pPolicy)
1091 return E_INVALIDARG;
1093 if (dwFlags)
1094 FIXME("dwFlags 0x%x ignored\n", dwFlags);
1096 return get_action_policy(dwZone, dwAction, pPolicy, cbPolicy, urlZoneReg);
1099 /********************************************************************
1100 * IInternetZoneManagerEx_SetZoneActionPolicyEx
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)
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;
1115 /********************************************************************
1116 * IInternetZoneManagerEx2_GetZoneAttributesEx
1118 static HRESULT WINAPI ZoneMgrImpl_GetZoneAttributesEx(IInternetZoneManagerEx2* iface,
1119 DWORD dwZone,
1120 ZONEATTRIBUTES* pZoneAttributes,
1121 DWORD dwFlags)
1123 TRACE("(%p)->(%d, %p, 0x%x)\n", iface, dwZone, pZoneAttributes, dwFlags);
1125 if (dwFlags)
1126 FIXME("dwFlags 0x%x ignored\n", dwFlags);
1128 return IInternetZoneManager_GetZoneAttributes(iface, dwZone, pZoneAttributes);
1132 /********************************************************************
1133 * IInternetZoneManagerEx2_GetZoneSecurityState
1135 static HRESULT WINAPI ZoneMgrImpl_GetZoneSecurityState(IInternetZoneManagerEx2* iface,
1136 DWORD dwZoneIndex,
1137 BOOL fRespectPolicy,
1138 LPDWORD pdwState,
1139 BOOL *pfPolicyEncountered)
1141 FIXME("(%p)->(%d, %d, %p, %p) stub\n", iface, dwZoneIndex, fRespectPolicy,
1142 pdwState, pfPolicyEncountered);
1144 *pdwState = SECURITY_IE_STATE_GREEN;
1146 if (pfPolicyEncountered)
1147 *pfPolicyEncountered = FALSE;
1149 return S_OK;
1152 /********************************************************************
1153 * IInternetZoneManagerEx2_GetIESecurityState
1155 static HRESULT WINAPI ZoneMgrImpl_GetIESecurityState(IInternetZoneManagerEx2* iface,
1156 BOOL fRespectPolicy,
1157 LPDWORD pdwState,
1158 BOOL *pfPolicyEncountered,
1159 BOOL fNoCache)
1161 FIXME("(%p)->(%d, %p, %p, %d) stub\n", iface, fRespectPolicy, pdwState,
1162 pfPolicyEncountered, fNoCache);
1164 *pdwState = SECURITY_IE_STATE_GREEN;
1166 if (pfPolicyEncountered)
1167 *pfPolicyEncountered = FALSE;
1169 return S_OK;
1172 /********************************************************************
1173 * IInternetZoneManagerEx2_FixInsecureSettings
1175 static HRESULT WINAPI ZoneMgrImpl_FixInsecureSettings(IInternetZoneManagerEx2* iface)
1177 FIXME("(%p) stub\n", iface);
1178 return S_OK;
1181 /********************************************************************
1182 * IInternetZoneManager_Construct
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,
1211 HRESULT ZoneMgrImpl_Construct(IUnknown *pUnkOuter, LPVOID *ppobj)
1213 ZoneMgrImpl* ret = heap_alloc_zero(sizeof(ZoneMgrImpl));
1215 TRACE("(%p %p)\n", pUnkOuter, ppobj);
1216 ret->IInternetZoneManagerEx2_iface.lpVtbl = &ZoneMgrImplVtbl;
1217 ret->ref = 1;
1218 *ppobj = (IInternetZoneManagerEx*)ret;
1220 URLMON_LockModule();
1222 return S_OK;
1225 /***********************************************************************
1226 * CoInternetCreateSecurityManager (URLMON.@)
1229 HRESULT WINAPI CoInternetCreateSecurityManager( IServiceProvider *pSP,
1230 IInternetSecurityManager **ppSM, DWORD dwReserved )
1232 TRACE("%p %p %d\n", pSP, ppSM, dwReserved );
1234 if(pSP)
1235 FIXME("pSP not supported\n");
1237 return SecManagerImpl_Construct(NULL, (void**) ppSM);
1240 /********************************************************************
1241 * CoInternetCreateZoneManager (URLMON.@)
1243 HRESULT WINAPI CoInternetCreateZoneManager(IServiceProvider* pSP, IInternetZoneManager** ppZM, DWORD dwReserved)
1245 TRACE("(%p %p %x)\n", pSP, ppZM, dwReserved);
1246 return ZoneMgrImpl_Construct(NULL, (void**)ppZM);
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;
1255 while(1) {
1256 TRACE("parsing %s\n", debugstr_w(url));
1258 protocol_info = get_protocol_info(url);
1259 if(!protocol_info)
1260 break;
1262 size = strlenW(url)+1;
1263 new_url = CoTaskMemAlloc(size*sizeof(WCHAR));
1264 if(!new_url) {
1265 hres = E_OUTOFMEMORY;
1266 break;
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;
1277 tmp = CoTaskMemRealloc(new_url, new_size*sizeof(WCHAR));
1278 if(!tmp) {
1279 hres = E_OUTOFMEMORY;
1280 break;
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;
1291 if(parse_hres != S_OK || !strcmpW(url, new_url))
1292 break;
1294 CoTaskMemFree(alloc_url);
1295 url = alloc_url = new_url;
1296 new_url = NULL;
1299 CoTaskMemFree(new_url);
1301 if(hres != S_OK) {
1302 WARN("failed: %08x\n", hres);
1303 CoTaskMemFree(alloc_url);
1304 return hres;
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;
1326 }else {
1327 hres = E_UNEXPECTED;
1331 if(hres == S_OK && parse_hres == S_OK) {
1332 CoTaskMemFree(alloc_url);
1333 url = alloc_url = new_url;
1334 new_url = NULL;
1337 CoTaskMemFree(new_url);
1338 }else {
1339 hres = E_OUTOFMEMORY;
1341 IInternetProtocolInfo_Release(protocol_info);
1344 if(FAILED(hres)) {
1345 WARN("failed %08x\n", hres);
1346 CoTaskMemFree(alloc_url);
1347 return hres;
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));
1358 *result = alloc_url;
1359 return S_OK;
1362 /********************************************************************
1363 * CoInternetGetSecurityUrl (URLMON.@)
1365 HRESULT WINAPI CoInternetGetSecurityUrl(LPCWSTR pwzUrl, LPWSTR *ppwzSecUrl, PSUACTION psuAction, DWORD dwReserved)
1367 WCHAR *secure_url;
1368 HRESULT hres;
1370 TRACE("(%p,%p,%u,%u)\n", pwzUrl, ppwzSecUrl, psuAction, dwReserved);
1372 hres = parse_security_url(pwzUrl, psuAction, &secure_url);
1373 if(FAILED(hres))
1374 return hres;
1376 if(psuAction != PSU_SECURITY_URL_ONLY) {
1377 PARSEDURLW parsed_url = { sizeof(parsed_url) };
1378 DWORD size;
1380 /* FIXME: Use helpers from uri.c */
1381 if(SUCCEEDED(ParseURLW(secure_url, &parsed_url))) {
1382 WCHAR *new_url;
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;
1400 secure_url = new_url;
1405 *ppwzSecUrl = secure_url;
1406 return S_OK;
1409 /********************************************************************
1410 * CoInternetGetSecurityUrlEx (URLMON.@)
1412 HRESULT WINAPI CoInternetGetSecurityUrlEx(IUri *pUri, IUri **ppSecUri, PSUACTION psuAction, DWORD_PTR dwReserved)
1414 URL_SCHEME scheme_type;
1415 BSTR secure_uri;
1416 WCHAR *ret_url;
1417 HRESULT hres;
1419 TRACE("(%p,%p,%u,%u)\n", pUri, ppSecUri, psuAction, (DWORD)dwReserved);
1421 if(!pUri || !ppSecUri)
1422 return E_INVALIDARG;
1424 hres = IUri_GetDisplayUri(pUri, &secure_uri);
1425 if(FAILED(hres))
1426 return hres;
1428 hres = parse_security_url(secure_uri, psuAction, &ret_url);
1429 SysFreeString(secure_uri);
1430 if(FAILED(hres))
1431 return hres;
1433 hres = CreateUri(ret_url, Uri_CREATE_ALLOW_IMPLICIT_WILDCARD_SCHEME, 0, ppSecUri);
1434 if(FAILED(hres)) {
1435 CoTaskMemFree(ret_url);
1436 return hres;
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;
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;
1450 if(SUCCEEDED(hres))
1451 hres = CreateUri(ret_url, Uri_CREATE_ALLOW_IMPLICIT_WILDCARD_SCHEME, 0, ppSecUri);
1452 CoTaskMemFree(ret_url);
1453 return hres;