wer: Add new stubbed wer.dll.
[wine/hramrach.git] / dlls / urlmon / sec_mgr.c
blobf984aa4e90700529028a1eb689fa9b4a91a1be77
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 #include "wine/debug.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(urlmon);
33 static const WCHAR currentlevelW[] = {'C','u','r','r','e','n','t','L','e','v','e','l',0};
34 static const WCHAR descriptionW[] = {'D','e','s','c','r','i','p','t','i','o','n',0};
35 static const WCHAR displaynameW[] = {'D','i','s','p','l','a','y','N','a','m','e',0};
36 static const WCHAR fileW[] = {'f','i','l','e',0};
37 static const WCHAR flagsW[] = {'F','l','a','g','s',0};
38 static const WCHAR iconW[] = {'I','c','o','n',0};
39 static const WCHAR minlevelW[] = {'M','i','n','L','e','v','e','l',0};
40 static const WCHAR recommendedlevelW[] = {'R','e','c','o','m','m','e','n','d','e','d',
41 'L','e','v','e','l',0};
42 static const WCHAR wszZonesKey[] = {'S','o','f','t','w','a','r','e','\\',
43 'M','i','c','r','o','s','o','f','t','\\',
44 'W','i','n','d','o','w','s','\\',
45 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
46 'I','n','t','e','r','n','e','t',' ','S','e','t','t','i','n','g','s','\\',
47 'Z','o','n','e','s','\\',0};
49 /********************************************************************
50 * get_string_from_reg [internal]
52 * helper to get a string from the reg.
55 static void get_string_from_reg(HKEY hcu, HKEY hklm, LPCWSTR name, LPWSTR out, DWORD maxlen)
57 DWORD type = REG_SZ;
58 DWORD len = maxlen * sizeof(WCHAR);
59 DWORD res;
61 res = RegQueryValueExW(hcu, name, NULL, &type, (LPBYTE) out, &len);
63 if (res && hklm) {
64 len = maxlen * sizeof(WCHAR);
65 type = REG_SZ;
66 res = RegQueryValueExW(hklm, name, NULL, &type, (LPBYTE) out, &len);
69 if (res) {
70 TRACE("%s failed: %d\n", debugstr_w(name), res);
71 *out = '\0';
75 /********************************************************************
76 * get_dword_from_reg [internal]
78 * helper to get a dword from the reg.
81 static void get_dword_from_reg(HKEY hcu, HKEY hklm, LPCWSTR name, LPDWORD out)
83 DWORD type = REG_DWORD;
84 DWORD len = sizeof(DWORD);
85 DWORD res;
87 res = RegQueryValueExW(hcu, name, NULL, &type, (LPBYTE) out, &len);
89 if (res && hklm) {
90 len = sizeof(DWORD);
91 type = REG_DWORD;
92 res = RegQueryValueExW(hklm, name, NULL, &type, (LPBYTE) out, &len);
95 if (res) {
96 TRACE("%s failed: %d\n", debugstr_w(name), res);
97 *out = 0;
101 static HRESULT get_zone_from_reg(LPCWSTR schema, DWORD *zone)
103 DWORD res, size;
104 HKEY hkey;
106 static const WCHAR wszZoneMapProtocolKey[] =
107 {'S','o','f','t','w','a','r','e','\\',
108 'M','i','c','r','o','s','o','f','t','\\',
109 'W','i','n','d','o','w','s','\\',
110 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
111 'I','n','t','e','r','n','e','t',' ','S','e','t','t','i','n','g','s','\\',
112 'Z','o','n','e','M','a','p','\\',
113 'P','r','o','t','o','c','o','l','D','e','f','a','u','l','t','s',0};
115 res = RegOpenKeyW(HKEY_CURRENT_USER, wszZoneMapProtocolKey, &hkey);
116 if(res != ERROR_SUCCESS) {
117 ERR("Could not open key %s\n", debugstr_w(wszZoneMapProtocolKey));
118 return E_UNEXPECTED;
121 size = sizeof(DWORD);
122 res = RegQueryValueExW(hkey, schema, NULL, NULL, (PBYTE)zone, &size);
123 RegCloseKey(hkey);
124 if(res == ERROR_SUCCESS)
125 return S_OK;
127 res = RegOpenKeyW(HKEY_LOCAL_MACHINE, wszZoneMapProtocolKey, &hkey);
128 if(res != ERROR_SUCCESS) {
129 ERR("Could not open key %s\n", debugstr_w(wszZoneMapProtocolKey));
130 return E_UNEXPECTED;
133 size = sizeof(DWORD);
134 res = RegQueryValueExW(hkey, schema, NULL, NULL, (PBYTE)zone, &size);
135 RegCloseKey(hkey);
136 if(res == ERROR_SUCCESS)
137 return S_OK;
139 *zone = 3;
140 return S_OK;
143 static HRESULT map_url_to_zone(LPCWSTR url, DWORD *zone, LPWSTR *ret_url)
145 LPWSTR secur_url;
146 WCHAR schema[64];
147 DWORD size=0;
148 HRESULT hres;
150 *zone = -1;
152 hres = CoInternetGetSecurityUrl(url, &secur_url, PSU_SECURITY_URL_ONLY, 0);
153 if(hres != S_OK) {
154 size = strlenW(url)*sizeof(WCHAR);
156 secur_url = heap_alloc(size);
157 if(!secur_url)
158 return E_OUTOFMEMORY;
160 memcpy(secur_url, url, size);
163 hres = CoInternetParseUrl(secur_url, PARSE_SCHEMA, 0, schema, sizeof(schema)/sizeof(WCHAR), &size, 0);
164 if(FAILED(hres) || !*schema) {
165 heap_free(secur_url);
166 return E_INVALIDARG;
169 /* file protocol is a special case */
170 if(!strcmpW(schema, fileW)) {
171 WCHAR path[MAX_PATH], root[20];
172 WCHAR *ptr;
174 hres = CoInternetParseUrl(secur_url, PARSE_PATH_FROM_URL, 0, path,
175 sizeof(path)/sizeof(WCHAR), &size, 0);
177 if(SUCCEEDED(hres) && (ptr = strchrW(path, '\\')) && ptr-path < sizeof(root)/sizeof(WCHAR)) {
178 UINT type;
180 memcpy(root, path, (ptr-path)*sizeof(WCHAR));
181 root[ptr-path] = 0;
183 type = GetDriveTypeW(root);
185 switch(type) {
186 case DRIVE_UNKNOWN:
187 case DRIVE_NO_ROOT_DIR:
188 break;
189 case DRIVE_REMOVABLE:
190 case DRIVE_FIXED:
191 case DRIVE_CDROM:
192 case DRIVE_RAMDISK:
193 *zone = 0;
194 hres = S_OK;
195 break;
196 case DRIVE_REMOTE:
197 *zone = 3;
198 hres = S_OK;
199 break;
200 default:
201 FIXME("unsupported drive type %d\n", type);
206 if(*zone == -1) {
207 WARN("domains are not yet implemented\n");
208 hres = get_zone_from_reg(schema, zone);
211 if(FAILED(hres) || !ret_url)
212 heap_free(secur_url);
213 else
214 *ret_url = secur_url;
216 return hres;
219 static HRESULT open_zone_key(HKEY parent_key, DWORD zone, HKEY *hkey)
221 static const WCHAR wszFormat[] = {'%','s','%','l','d',0};
223 WCHAR key_name[sizeof(wszZonesKey)/sizeof(WCHAR)+8];
224 DWORD res;
226 wsprintfW(key_name, wszFormat, wszZonesKey, zone);
228 res = RegOpenKeyW(parent_key, key_name, hkey);
230 if(res != ERROR_SUCCESS) {
231 WARN("RegOpenKey failed\n");
232 return E_INVALIDARG;
235 return S_OK;
238 static HRESULT get_action_policy(DWORD zone, DWORD action, BYTE *policy, DWORD size, URLZONEREG zone_reg)
240 HKEY parent_key;
241 HKEY hkey;
242 LONG res;
243 HRESULT hres;
245 switch(action) {
246 case URLACTION_SCRIPT_OVERRIDE_SAFETY:
247 case URLACTION_ACTIVEX_OVERRIDE_SCRIPT_SAFETY:
248 *(DWORD*)policy = URLPOLICY_DISALLOW;
249 return S_OK;
252 switch(zone_reg) {
253 case URLZONEREG_DEFAULT:
254 case URLZONEREG_HKCU:
255 parent_key = HKEY_CURRENT_USER;
256 break;
257 case URLZONEREG_HKLM:
258 parent_key = HKEY_LOCAL_MACHINE;
259 break;
260 default:
261 WARN("Unknown URLZONEREG: %d\n", zone_reg);
262 return E_FAIL;
265 hres = open_zone_key(parent_key, zone, &hkey);
266 if(SUCCEEDED(hres)) {
267 WCHAR action_str[16];
268 DWORD len = size;
270 static const WCHAR formatW[] = {'%','X',0};
272 wsprintfW(action_str, formatW, action);
274 res = RegQueryValueExW(hkey, action_str, NULL, NULL, policy, &len);
275 if(res == ERROR_MORE_DATA) {
276 hres = E_INVALIDARG;
277 }else if(res == ERROR_FILE_NOT_FOUND) {
278 hres = E_FAIL;
279 }else if(res != ERROR_SUCCESS) {
280 ERR("RegQueryValue failed: %d\n", res);
281 hres = E_UNEXPECTED;
284 RegCloseKey(hkey);
287 if(FAILED(hres) && zone_reg == URLZONEREG_DEFAULT)
288 return get_action_policy(zone, action, policy, size, URLZONEREG_HKLM);
290 return hres;
293 /***********************************************************************
294 * InternetSecurityManager implementation
297 typedef struct {
298 const IInternetSecurityManagerVtbl* lpInternetSecurityManagerVtbl;
300 LONG ref;
302 IInternetSecurityMgrSite *mgrsite;
303 IInternetSecurityManager *custom_manager;
304 } SecManagerImpl;
306 #define SECMGR_THIS(iface) DEFINE_THIS(SecManagerImpl, InternetSecurityManager, iface)
308 static HRESULT WINAPI SecManagerImpl_QueryInterface(IInternetSecurityManager* iface,REFIID riid,void** ppvObject)
310 SecManagerImpl *This = SECMGR_THIS(iface);
312 TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppvObject);
314 /* Perform a sanity check on the parameters.*/
315 if ( (This==0) || (ppvObject==0) )
316 return E_INVALIDARG;
318 /* Initialize the return parameter */
319 *ppvObject = 0;
321 /* Compare the riid with the interface IDs implemented by this object.*/
322 if (IsEqualIID(&IID_IUnknown, riid) ||
323 IsEqualIID(&IID_IInternetSecurityManager, riid))
324 *ppvObject = iface;
326 /* Check that we obtained an interface.*/
327 if (!*ppvObject) {
328 WARN("not supported interface %s\n", debugstr_guid(riid));
329 return E_NOINTERFACE;
332 /* Query Interface always increases the reference count by one when it is successful */
333 IInternetSecurityManager_AddRef(iface);
335 return S_OK;
338 static ULONG WINAPI SecManagerImpl_AddRef(IInternetSecurityManager* iface)
340 SecManagerImpl *This = SECMGR_THIS(iface);
341 ULONG refCount = InterlockedIncrement(&This->ref);
343 TRACE("(%p) ref=%u\n", This, refCount);
345 return refCount;
348 static ULONG WINAPI SecManagerImpl_Release(IInternetSecurityManager* iface)
350 SecManagerImpl *This = SECMGR_THIS(iface);
351 ULONG refCount = InterlockedDecrement(&This->ref);
353 TRACE("(%p) ref=%u\n", This, refCount);
355 /* destroy the object if there's no more reference on it */
356 if (!refCount){
357 if(This->mgrsite)
358 IInternetSecurityMgrSite_Release(This->mgrsite);
359 if(This->custom_manager)
360 IInternetSecurityManager_Release(This->custom_manager);
362 heap_free(This);
364 URLMON_UnlockModule();
367 return refCount;
370 static HRESULT WINAPI SecManagerImpl_SetSecuritySite(IInternetSecurityManager *iface,
371 IInternetSecurityMgrSite *pSite)
373 SecManagerImpl *This = SECMGR_THIS(iface);
375 TRACE("(%p)->(%p)\n", This, pSite);
377 if(This->mgrsite)
378 IInternetSecurityMgrSite_Release(This->mgrsite);
380 if(This->custom_manager) {
381 IInternetSecurityManager_Release(This->custom_manager);
382 This->custom_manager = NULL;
385 This->mgrsite = pSite;
387 if(pSite) {
388 IServiceProvider *servprov;
389 HRESULT hres;
391 IInternetSecurityMgrSite_AddRef(pSite);
393 hres = IInternetSecurityMgrSite_QueryInterface(pSite, &IID_IServiceProvider,
394 (void**)&servprov);
395 if(SUCCEEDED(hres)) {
396 IServiceProvider_QueryService(servprov, &SID_SInternetSecurityManager,
397 &IID_IInternetSecurityManager, (void**)&This->custom_manager);
398 IServiceProvider_Release(servprov);
402 return S_OK;
405 static HRESULT WINAPI SecManagerImpl_GetSecuritySite(IInternetSecurityManager *iface,
406 IInternetSecurityMgrSite **ppSite)
408 SecManagerImpl *This = SECMGR_THIS(iface);
410 TRACE("(%p)->(%p)\n", This, ppSite);
412 if(!ppSite)
413 return E_INVALIDARG;
415 if(This->mgrsite)
416 IInternetSecurityMgrSite_AddRef(This->mgrsite);
418 *ppSite = This->mgrsite;
419 return S_OK;
422 static HRESULT WINAPI SecManagerImpl_MapUrlToZone(IInternetSecurityManager *iface,
423 LPCWSTR pwszUrl, DWORD *pdwZone,
424 DWORD dwFlags)
426 SecManagerImpl *This = SECMGR_THIS(iface);
427 HRESULT hres;
429 TRACE("(%p)->(%s %p %08x)\n", iface, debugstr_w(pwszUrl), pdwZone, dwFlags);
431 if(This->custom_manager) {
432 hres = IInternetSecurityManager_MapUrlToZone(This->custom_manager,
433 pwszUrl, pdwZone, dwFlags);
434 if(hres != INET_E_DEFAULT_ACTION)
435 return hres;
438 if(!pwszUrl) {
439 *pdwZone = -1;
440 return E_INVALIDARG;
443 if(dwFlags)
444 FIXME("not supported flags: %08x\n", dwFlags);
446 return map_url_to_zone(pwszUrl, pdwZone, NULL);
449 static HRESULT WINAPI SecManagerImpl_GetSecurityId(IInternetSecurityManager *iface,
450 LPCWSTR pwszUrl, BYTE *pbSecurityId, DWORD *pcbSecurityId, DWORD_PTR dwReserved)
452 SecManagerImpl *This = SECMGR_THIS(iface);
453 LPWSTR url, ptr, ptr2;
454 DWORD zone, len;
455 HRESULT hres;
457 static const WCHAR wszFile[] = {'f','i','l','e',':'};
459 TRACE("(%p)->(%s %p %p %08lx)\n", iface, debugstr_w(pwszUrl), pbSecurityId,
460 pcbSecurityId, dwReserved);
462 if(This->custom_manager) {
463 hres = IInternetSecurityManager_GetSecurityId(This->custom_manager,
464 pwszUrl, pbSecurityId, pcbSecurityId, dwReserved);
465 if(hres != INET_E_DEFAULT_ACTION)
466 return hres;
469 if(!pwszUrl || !pbSecurityId || !pcbSecurityId)
470 return E_INVALIDARG;
472 if(dwReserved)
473 FIXME("dwReserved is not supported\n");
475 hres = map_url_to_zone(pwszUrl, &zone, &url);
476 if(FAILED(hres))
477 return hres == 0x80041001 ? E_INVALIDARG : hres;
479 /* file protocol is a special case */
480 if(strlenW(url) >= sizeof(wszFile)/sizeof(WCHAR)
481 && !memcmp(url, wszFile, sizeof(wszFile)) && strchrW(url, '\\')) {
483 static const BYTE secidFile[] = {'f','i','l','e',':'};
485 heap_free(url);
487 if(*pcbSecurityId < sizeof(secidFile)+sizeof(zone))
488 return HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
490 memcpy(pbSecurityId, secidFile, sizeof(secidFile));
491 *(DWORD*)(pbSecurityId+sizeof(secidFile)) = zone;
493 *pcbSecurityId = sizeof(secidFile)+sizeof(zone);
494 return S_OK;
497 ptr = strchrW(url, ':');
498 ptr2 = ++ptr;
499 while(*ptr2 == '/')
500 ptr2++;
501 if(ptr2 != ptr)
502 memmove(ptr, ptr2, (strlenW(ptr2)+1)*sizeof(WCHAR));
504 ptr = strchrW(ptr, '/');
505 if(ptr)
506 *ptr = 0;
508 len = WideCharToMultiByte(CP_ACP, 0, url, -1, NULL, 0, NULL, NULL)-1;
510 if(len+sizeof(DWORD) > *pcbSecurityId) {
511 heap_free(url);
512 return HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
515 WideCharToMultiByte(CP_ACP, 0, url, -1, (LPSTR)pbSecurityId, len, NULL, NULL);
516 heap_free(url);
518 *(DWORD*)(pbSecurityId+len) = zone;
520 *pcbSecurityId = len+sizeof(DWORD);
522 return S_OK;
526 static HRESULT WINAPI SecManagerImpl_ProcessUrlAction(IInternetSecurityManager *iface,
527 LPCWSTR pwszUrl, DWORD dwAction,
528 BYTE *pPolicy, DWORD cbPolicy,
529 BYTE *pContext, DWORD cbContext,
530 DWORD dwFlags, DWORD dwReserved)
532 SecManagerImpl *This = SECMGR_THIS(iface);
533 DWORD zone, policy;
534 HRESULT hres;
536 TRACE("(%p)->(%s %08x %p %08x %p %08x %08x %08x)\n", iface, debugstr_w(pwszUrl), dwAction,
537 pPolicy, cbPolicy, pContext, cbContext, dwFlags, dwReserved);
539 if(This->custom_manager) {
540 hres = IInternetSecurityManager_ProcessUrlAction(This->custom_manager, pwszUrl, dwAction,
541 pPolicy, cbPolicy, pContext, cbContext, dwFlags, dwReserved);
542 if(hres != INET_E_DEFAULT_ACTION)
543 return hres;
546 if(dwFlags || dwReserved)
547 FIXME("Unsupported arguments\n");
549 if(!pwszUrl)
550 return E_INVALIDARG;
552 hres = map_url_to_zone(pwszUrl, &zone, NULL);
553 if(FAILED(hres))
554 return hres;
556 hres = get_action_policy(zone, dwAction, (BYTE*)&policy, sizeof(policy), URLZONEREG_DEFAULT);
557 if(FAILED(hres))
558 return hres;
560 TRACE("policy %x\n", policy);
561 if(cbPolicy >= sizeof(DWORD))
562 *(DWORD*)pPolicy = policy;
564 switch(GetUrlPolicyPermissions(policy)) {
565 case URLPOLICY_ALLOW:
566 case URLPOLICY_CHANNEL_SOFTDIST_PRECACHE:
567 return S_OK;
568 case URLPOLICY_DISALLOW:
569 return S_FALSE;
570 case URLPOLICY_QUERY:
571 FIXME("URLPOLICY_QUERY not implemented\n");
572 return E_FAIL;
573 default:
574 FIXME("Not implemented policy %x\n", policy);
577 return E_FAIL;
581 static HRESULT WINAPI SecManagerImpl_QueryCustomPolicy(IInternetSecurityManager *iface,
582 LPCWSTR pwszUrl, REFGUID guidKey,
583 BYTE **ppPolicy, DWORD *pcbPolicy,
584 BYTE *pContext, DWORD cbContext,
585 DWORD dwReserved)
587 SecManagerImpl *This = SECMGR_THIS(iface);
588 HRESULT hres;
590 TRACE("(%p)->(%s %s %p %p %p %08x %08x )\n", iface, debugstr_w(pwszUrl), debugstr_guid(guidKey),
591 ppPolicy, pcbPolicy, pContext, cbContext, dwReserved);
593 if(This->custom_manager) {
594 hres = IInternetSecurityManager_QueryCustomPolicy(This->custom_manager, pwszUrl, guidKey,
595 ppPolicy, pcbPolicy, pContext, cbContext, dwReserved);
596 if(hres != INET_E_DEFAULT_ACTION)
597 return hres;
600 WARN("Unknown guidKey %s\n", debugstr_guid(guidKey));
601 return HRESULT_FROM_WIN32(ERROR_NOT_FOUND);
604 static HRESULT WINAPI SecManagerImpl_SetZoneMapping(IInternetSecurityManager *iface,
605 DWORD dwZone, LPCWSTR pwszPattern, DWORD dwFlags)
607 SecManagerImpl *This = SECMGR_THIS(iface);
608 HRESULT hres;
610 TRACE("(%p)->(%08x %s %08x)\n", iface, dwZone, debugstr_w(pwszPattern),dwFlags);
612 if(This->custom_manager) {
613 hres = IInternetSecurityManager_SetZoneMapping(This->custom_manager, dwZone,
614 pwszPattern, dwFlags);
615 if(hres != INET_E_DEFAULT_ACTION)
616 return hres;
619 FIXME("Default action is not implemented\n");
620 return E_NOTIMPL;
623 static HRESULT WINAPI SecManagerImpl_GetZoneMappings(IInternetSecurityManager *iface,
624 DWORD dwZone, IEnumString **ppenumString, DWORD dwFlags)
626 SecManagerImpl *This = SECMGR_THIS(iface);
627 HRESULT hres;
629 TRACE("(%p)->(%08x %p %08x)\n", iface, dwZone, ppenumString,dwFlags);
631 if(This->custom_manager) {
632 hres = IInternetSecurityManager_GetZoneMappings(This->custom_manager, dwZone,
633 ppenumString, dwFlags);
634 if(hres != INET_E_DEFAULT_ACTION)
635 return hres;
638 FIXME("Default action is not implemented\n");
639 return E_NOTIMPL;
642 static const IInternetSecurityManagerVtbl VT_SecManagerImpl =
644 SecManagerImpl_QueryInterface,
645 SecManagerImpl_AddRef,
646 SecManagerImpl_Release,
647 SecManagerImpl_SetSecuritySite,
648 SecManagerImpl_GetSecuritySite,
649 SecManagerImpl_MapUrlToZone,
650 SecManagerImpl_GetSecurityId,
651 SecManagerImpl_ProcessUrlAction,
652 SecManagerImpl_QueryCustomPolicy,
653 SecManagerImpl_SetZoneMapping,
654 SecManagerImpl_GetZoneMappings
657 HRESULT SecManagerImpl_Construct(IUnknown *pUnkOuter, LPVOID *ppobj)
659 SecManagerImpl *This;
661 TRACE("(%p,%p)\n",pUnkOuter,ppobj);
662 This = heap_alloc(sizeof(*This));
664 /* Initialize the virtual function table. */
665 This->lpInternetSecurityManagerVtbl = &VT_SecManagerImpl;
667 This->ref = 1;
668 This->mgrsite = NULL;
669 This->custom_manager = NULL;
671 *ppobj = This;
673 URLMON_LockModule();
675 return S_OK;
678 /***********************************************************************
679 * InternetZoneManager implementation
682 typedef struct {
683 const IInternetZoneManagerEx2Vtbl* lpVtbl;
684 LONG ref;
685 LPDWORD *zonemaps;
686 DWORD zonemap_count;
687 } ZoneMgrImpl;
690 /***********************************************************************
691 * build_zonemap_from_reg [internal]
693 * Enumerate the Zones in the Registry and return the Zones in a DWORD-array
694 * The number of the Zones is returned in data[0]
696 static LPDWORD build_zonemap_from_reg(void)
698 WCHAR name[32];
699 HKEY hkey;
700 LPDWORD data = NULL;
701 DWORD allocated = 6; /* space for the zonecount and Zone "0" up to Zone "4" */
702 DWORD used = 0;
703 DWORD res;
704 DWORD len;
707 res = RegOpenKeyW(HKEY_CURRENT_USER, wszZonesKey, &hkey);
708 if (res)
709 return NULL;
711 data = heap_alloc(allocated * sizeof(DWORD));
712 if (!data)
713 goto cleanup;
715 while (!res) {
716 name[0] = '\0';
717 len = sizeof(name) / sizeof(name[0]);
718 res = RegEnumKeyExW(hkey, used, name, &len, NULL, NULL, NULL, NULL);
720 if (!res) {
721 used++;
722 if (used == allocated) {
723 LPDWORD new_data;
725 allocated *= 2;
726 new_data = heap_realloc_zero(data, allocated * sizeof(DWORD));
727 if (!new_data)
728 goto cleanup;
730 data = new_data;
732 data[used] = atoiW(name);
735 if (used) {
736 RegCloseKey(hkey);
737 data[0] = used;
738 return data;
741 cleanup:
742 /* something failed */
743 RegCloseKey(hkey);
744 heap_free(data);
745 return NULL;
748 /********************************************************************
749 * IInternetZoneManager_QueryInterface
751 static HRESULT WINAPI ZoneMgrImpl_QueryInterface(IInternetZoneManagerEx2* iface, REFIID riid, void** ppvObject)
753 ZoneMgrImpl* This = (ZoneMgrImpl*)iface;
755 TRACE("(%p)->(%s,%p)\n", This, debugstr_guid(riid), ppvObject);
757 if(!This || !ppvObject)
758 return E_INVALIDARG;
760 if(IsEqualIID(&IID_IUnknown, riid)) {
761 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppvObject);
762 }else if(IsEqualIID(&IID_IInternetZoneManager, riid)) {
763 TRACE("(%p)->(IID_InternetZoneManager %p)\n", This, ppvObject);
764 }else if(IsEqualIID(&IID_IInternetZoneManagerEx, riid)) {
765 TRACE("(%p)->(IID_InternetZoneManagerEx %p)\n", This, ppvObject);
766 }else if(IsEqualIID(&IID_IInternetZoneManagerEx2, riid)) {
767 TRACE("(%p)->(IID_InternetZoneManagerEx2 %p)\n", This, ppvObject);
769 else
771 FIXME("Unknown interface: %s\n", debugstr_guid(riid));
772 *ppvObject = NULL;
773 return E_NOINTERFACE;
776 *ppvObject = iface;
777 IInternetZoneManager_AddRef(iface);
778 return S_OK;
781 /********************************************************************
782 * IInternetZoneManager_AddRef
784 static ULONG WINAPI ZoneMgrImpl_AddRef(IInternetZoneManagerEx2* iface)
786 ZoneMgrImpl* This = (ZoneMgrImpl*)iface;
787 ULONG refCount = InterlockedIncrement(&This->ref);
789 TRACE("(%p)->(ref before=%u)\n",This, refCount - 1);
791 return refCount;
794 /********************************************************************
795 * IInternetZoneManager_Release
797 static ULONG WINAPI ZoneMgrImpl_Release(IInternetZoneManagerEx2* iface)
799 ZoneMgrImpl* This = (ZoneMgrImpl*)iface;
800 ULONG refCount = InterlockedDecrement(&This->ref);
802 TRACE("(%p)->(ref before=%u)\n",This, refCount + 1);
804 if(!refCount) {
805 while (This->zonemap_count) heap_free(This->zonemaps[--This->zonemap_count]);
806 heap_free(This->zonemaps);
807 heap_free(This);
808 URLMON_UnlockModule();
811 return refCount;
814 /********************************************************************
815 * IInternetZoneManager_GetZoneAttributes
817 static HRESULT WINAPI ZoneMgrImpl_GetZoneAttributes(IInternetZoneManagerEx2* iface,
818 DWORD dwZone,
819 ZONEATTRIBUTES* pZoneAttributes)
821 ZoneMgrImpl* This = (ZoneMgrImpl*)iface;
822 HRESULT hr;
823 HKEY hcu;
824 HKEY hklm = NULL;
826 TRACE("(%p)->(%d %p)\n", This, dwZone, pZoneAttributes);
828 if (!pZoneAttributes)
829 return E_INVALIDARG;
831 hr = open_zone_key(HKEY_CURRENT_USER, dwZone, &hcu);
832 if (FAILED(hr))
833 return S_OK; /* IE6 and older returned E_FAIL here */
835 hr = open_zone_key(HKEY_LOCAL_MACHINE, dwZone, &hklm);
836 if (FAILED(hr))
837 TRACE("Zone %d not in HKLM\n", dwZone);
839 get_string_from_reg(hcu, hklm, displaynameW, pZoneAttributes->szDisplayName, MAX_ZONE_PATH);
840 get_string_from_reg(hcu, hklm, descriptionW, pZoneAttributes->szDescription, MAX_ZONE_DESCRIPTION);
841 get_string_from_reg(hcu, hklm, iconW, pZoneAttributes->szIconPath, MAX_ZONE_PATH);
842 get_dword_from_reg(hcu, hklm, minlevelW, &pZoneAttributes->dwTemplateMinLevel);
843 get_dword_from_reg(hcu, hklm, currentlevelW, &pZoneAttributes->dwTemplateCurrentLevel);
844 get_dword_from_reg(hcu, hklm, recommendedlevelW, &pZoneAttributes->dwTemplateRecommended);
845 get_dword_from_reg(hcu, hklm, flagsW, &pZoneAttributes->dwFlags);
847 RegCloseKey(hklm);
848 RegCloseKey(hcu);
849 return S_OK;
852 /********************************************************************
853 * IInternetZoneManager_SetZoneAttributes
855 static HRESULT WINAPI ZoneMgrImpl_SetZoneAttributes(IInternetZoneManagerEx2* iface,
856 DWORD dwZone,
857 ZONEATTRIBUTES* pZoneAttributes)
859 FIXME("(%p)->(%08x %p) stub\n", iface, dwZone, pZoneAttributes);
860 return E_NOTIMPL;
863 /********************************************************************
864 * IInternetZoneManager_GetZoneCustomPolicy
866 static HRESULT WINAPI ZoneMgrImpl_GetZoneCustomPolicy(IInternetZoneManagerEx2* iface,
867 DWORD dwZone,
868 REFGUID guidKey,
869 BYTE** ppPolicy,
870 DWORD* pcbPolicy,
871 URLZONEREG ulrZoneReg)
873 FIXME("(%p)->(%08x %s %p %p %08x) stub\n", iface, dwZone, debugstr_guid(guidKey),
874 ppPolicy, pcbPolicy, ulrZoneReg);
875 return E_NOTIMPL;
878 /********************************************************************
879 * IInternetZoneManager_SetZoneCustomPolicy
881 static HRESULT WINAPI ZoneMgrImpl_SetZoneCustomPolicy(IInternetZoneManagerEx2* iface,
882 DWORD dwZone,
883 REFGUID guidKey,
884 BYTE* ppPolicy,
885 DWORD cbPolicy,
886 URLZONEREG ulrZoneReg)
888 FIXME("(%p)->(%08x %s %p %08x %08x) stub\n", iface, dwZone, debugstr_guid(guidKey),
889 ppPolicy, cbPolicy, ulrZoneReg);
890 return E_NOTIMPL;
893 /********************************************************************
894 * IInternetZoneManager_GetZoneActionPolicy
896 static HRESULT WINAPI ZoneMgrImpl_GetZoneActionPolicy(IInternetZoneManagerEx2* iface,
897 DWORD dwZone, DWORD dwAction, BYTE* pPolicy, DWORD cbPolicy, URLZONEREG urlZoneReg)
899 TRACE("(%p)->(%d %08x %p %d %d)\n", iface, dwZone, dwAction, pPolicy,
900 cbPolicy, urlZoneReg);
902 if(!pPolicy)
903 return E_INVALIDARG;
905 return get_action_policy(dwZone, dwAction, pPolicy, cbPolicy, urlZoneReg);
908 /********************************************************************
909 * IInternetZoneManager_SetZoneActionPolicy
911 static HRESULT WINAPI ZoneMgrImpl_SetZoneActionPolicy(IInternetZoneManagerEx2* iface,
912 DWORD dwZone,
913 DWORD dwAction,
914 BYTE* pPolicy,
915 DWORD cbPolicy,
916 URLZONEREG urlZoneReg)
918 FIXME("(%p)->(%08x %08x %p %08x %08x) stub\n", iface, dwZone, dwAction, pPolicy,
919 cbPolicy, urlZoneReg);
920 return E_NOTIMPL;
923 /********************************************************************
924 * IInternetZoneManager_PromptAction
926 static HRESULT WINAPI ZoneMgrImpl_PromptAction(IInternetZoneManagerEx2* iface,
927 DWORD dwAction,
928 HWND hwndParent,
929 LPCWSTR pwszUrl,
930 LPCWSTR pwszText,
931 DWORD dwPromptFlags)
933 FIXME("%p %08x %p %s %s %08x\n", iface, dwAction, hwndParent,
934 debugstr_w(pwszUrl), debugstr_w(pwszText), dwPromptFlags );
935 return E_NOTIMPL;
938 /********************************************************************
939 * IInternetZoneManager_LogAction
941 static HRESULT WINAPI ZoneMgrImpl_LogAction(IInternetZoneManagerEx2* iface,
942 DWORD dwAction,
943 LPCWSTR pwszUrl,
944 LPCWSTR pwszText,
945 DWORD dwLogFlags)
947 FIXME("(%p)->(%08x %s %s %08x) stub\n", iface, dwAction, debugstr_w(pwszUrl),
948 debugstr_w(pwszText), dwLogFlags);
949 return E_NOTIMPL;
952 /********************************************************************
953 * IInternetZoneManager_CreateZoneEnumerator
955 static HRESULT WINAPI ZoneMgrImpl_CreateZoneEnumerator(IInternetZoneManagerEx2* iface,
956 DWORD* pdwEnum,
957 DWORD* pdwCount,
958 DWORD dwFlags)
960 ZoneMgrImpl* This = (ZoneMgrImpl*)iface;
961 LPDWORD * new_maps;
962 LPDWORD data;
963 DWORD i;
965 TRACE("(%p)->(%p, %p, 0x%08x)\n", This, pdwEnum, pdwCount, dwFlags);
966 if (!pdwEnum || !pdwCount || (dwFlags != 0))
967 return E_INVALIDARG;
969 data = build_zonemap_from_reg();
970 TRACE("found %d zones\n", data ? data[0] : -1);
972 if (!data)
973 return E_FAIL;
975 for (i = 0; i < This->zonemap_count; i++) {
976 if (This->zonemaps && !This->zonemaps[i]) {
977 This->zonemaps[i] = data;
978 *pdwEnum = i;
979 *pdwCount = data[0];
980 return S_OK;
984 if (This->zonemaps) {
985 /* try to double the nr. of pointers in the array */
986 new_maps = heap_realloc_zero(This->zonemaps, This->zonemap_count * 2 * sizeof(LPDWORD));
987 if (new_maps)
988 This->zonemap_count *= 2;
990 else
992 This->zonemap_count = 2;
993 new_maps = heap_alloc_zero(This->zonemap_count * sizeof(LPDWORD));
996 if (!new_maps) {
997 heap_free(data);
998 return E_FAIL;
1000 This->zonemaps = new_maps;
1001 This->zonemaps[i] = data;
1002 *pdwEnum = i;
1003 *pdwCount = data[0];
1004 return S_OK;
1007 /********************************************************************
1008 * IInternetZoneManager_GetZoneAt
1010 static HRESULT WINAPI ZoneMgrImpl_GetZoneAt(IInternetZoneManagerEx2* iface,
1011 DWORD dwEnum,
1012 DWORD dwIndex,
1013 DWORD* pdwZone)
1015 ZoneMgrImpl* This = (ZoneMgrImpl*)iface;
1016 LPDWORD data;
1018 TRACE("(%p)->(0x%08x, %d, %p)\n", This, dwEnum, dwIndex, pdwZone);
1020 /* make sure, that dwEnum and dwIndex are in the valid range */
1021 if (dwEnum < This->zonemap_count) {
1022 if ((data = This->zonemaps[dwEnum])) {
1023 if (dwIndex < data[0]) {
1024 *pdwZone = data[dwIndex + 1];
1025 return S_OK;
1029 return E_INVALIDARG;
1032 /********************************************************************
1033 * IInternetZoneManager_DestroyZoneEnumerator
1035 static HRESULT WINAPI ZoneMgrImpl_DestroyZoneEnumerator(IInternetZoneManagerEx2* iface,
1036 DWORD dwEnum)
1038 ZoneMgrImpl* This = (ZoneMgrImpl*)iface;
1039 LPDWORD data;
1041 TRACE("(%p)->(0x%08x)\n", This, dwEnum);
1042 /* make sure, that dwEnum is valid */
1043 if (dwEnum < This->zonemap_count) {
1044 if ((data = This->zonemaps[dwEnum])) {
1045 This->zonemaps[dwEnum] = NULL;
1046 heap_free(data);
1047 return S_OK;
1050 return E_INVALIDARG;
1053 /********************************************************************
1054 * IInternetZoneManager_CopyTemplatePoliciesToZone
1056 static HRESULT WINAPI ZoneMgrImpl_CopyTemplatePoliciesToZone(IInternetZoneManagerEx2* iface,
1057 DWORD dwTemplate,
1058 DWORD dwZone,
1059 DWORD dwReserved)
1061 FIXME("(%p)->(%08x %08x %08x) stub\n", iface, dwTemplate, dwZone, dwReserved);
1062 return E_NOTIMPL;
1065 /********************************************************************
1066 * IInternetZoneManagerEx_GetZoneActionPolicyEx
1068 static HRESULT WINAPI ZoneMgrImpl_GetZoneActionPolicyEx(IInternetZoneManagerEx2* iface,
1069 DWORD dwZone,
1070 DWORD dwAction,
1071 BYTE* pPolicy,
1072 DWORD cbPolicy,
1073 URLZONEREG urlZoneReg,
1074 DWORD dwFlags)
1076 TRACE("(%p)->(%d, 0x%x, %p, %d, %d, 0x%x)\n", iface, dwZone,
1077 dwAction, pPolicy, cbPolicy, urlZoneReg, dwFlags);
1079 if(!pPolicy)
1080 return E_INVALIDARG;
1082 if (dwFlags)
1083 FIXME("dwFlags 0x%x ignored\n", dwFlags);
1085 return get_action_policy(dwZone, dwAction, pPolicy, cbPolicy, urlZoneReg);
1088 /********************************************************************
1089 * IInternetZoneManagerEx_SetZoneActionPolicyEx
1091 static HRESULT WINAPI ZoneMgrImpl_SetZoneActionPolicyEx(IInternetZoneManagerEx2* iface,
1092 DWORD dwZone,
1093 DWORD dwAction,
1094 BYTE* pPolicy,
1095 DWORD cbPolicy,
1096 URLZONEREG urlZoneReg,
1097 DWORD dwFlags)
1099 FIXME("(%p)->(%d, 0x%x, %p, %d, %d, 0x%x) stub\n", iface, dwZone, dwAction, pPolicy,
1100 cbPolicy, urlZoneReg, dwFlags);
1101 return E_NOTIMPL;
1104 /********************************************************************
1105 * IInternetZoneManagerEx2_GetZoneAttributesEx
1107 static HRESULT WINAPI ZoneMgrImpl_GetZoneAttributesEx(IInternetZoneManagerEx2* iface,
1108 DWORD dwZone,
1109 ZONEATTRIBUTES* pZoneAttributes,
1110 DWORD dwFlags)
1112 TRACE("(%p)->(%d, %p, 0x%x)\n", iface, dwZone, pZoneAttributes, dwFlags);
1114 if (dwFlags)
1115 FIXME("dwFlags 0x%x ignored\n", dwFlags);
1117 return IInternetZoneManager_GetZoneAttributes(iface, dwZone, pZoneAttributes);
1121 /********************************************************************
1122 * IInternetZoneManagerEx2_GetZoneSecurityState
1124 static HRESULT WINAPI ZoneMgrImpl_GetZoneSecurityState(IInternetZoneManagerEx2* iface,
1125 DWORD dwZoneIndex,
1126 BOOL fRespectPolicy,
1127 LPDWORD pdwState,
1128 BOOL *pfPolicyEncountered)
1130 FIXME("(%p)->(%d, %d, %p, %p) stub\n", iface, dwZoneIndex, fRespectPolicy,
1131 pdwState, pfPolicyEncountered);
1133 *pdwState = SECURITY_IE_STATE_GREEN;
1135 if (pfPolicyEncountered)
1136 *pfPolicyEncountered = FALSE;
1138 return S_OK;
1141 /********************************************************************
1142 * IInternetZoneManagerEx2_GetIESecurityState
1144 static HRESULT WINAPI ZoneMgrImpl_GetIESecurityState(IInternetZoneManagerEx2* iface,
1145 BOOL fRespectPolicy,
1146 LPDWORD pdwState,
1147 BOOL *pfPolicyEncountered,
1148 BOOL fNoCache)
1150 FIXME("(%p)->(%d, %p, %p, %d) stub\n", iface, fRespectPolicy, pdwState,
1151 pfPolicyEncountered, fNoCache);
1153 *pdwState = SECURITY_IE_STATE_GREEN;
1155 if (pfPolicyEncountered)
1156 *pfPolicyEncountered = FALSE;
1158 return S_OK;
1161 /********************************************************************
1162 * IInternetZoneManagerEx2_FixInsecureSettings
1164 static HRESULT WINAPI ZoneMgrImpl_FixInsecureSettings(IInternetZoneManagerEx2* iface)
1166 FIXME("(%p) stub\n", iface);
1167 return S_OK;
1170 /********************************************************************
1171 * IInternetZoneManager_Construct
1173 static const IInternetZoneManagerEx2Vtbl ZoneMgrImplVtbl = {
1174 ZoneMgrImpl_QueryInterface,
1175 ZoneMgrImpl_AddRef,
1176 ZoneMgrImpl_Release,
1177 /* IInternetZoneManager */
1178 ZoneMgrImpl_GetZoneAttributes,
1179 ZoneMgrImpl_SetZoneAttributes,
1180 ZoneMgrImpl_GetZoneCustomPolicy,
1181 ZoneMgrImpl_SetZoneCustomPolicy,
1182 ZoneMgrImpl_GetZoneActionPolicy,
1183 ZoneMgrImpl_SetZoneActionPolicy,
1184 ZoneMgrImpl_PromptAction,
1185 ZoneMgrImpl_LogAction,
1186 ZoneMgrImpl_CreateZoneEnumerator,
1187 ZoneMgrImpl_GetZoneAt,
1188 ZoneMgrImpl_DestroyZoneEnumerator,
1189 ZoneMgrImpl_CopyTemplatePoliciesToZone,
1190 /* IInternetZoneManagerEx */
1191 ZoneMgrImpl_GetZoneActionPolicyEx,
1192 ZoneMgrImpl_SetZoneActionPolicyEx,
1193 /* IInternetZoneManagerEx2 */
1194 ZoneMgrImpl_GetZoneAttributesEx,
1195 ZoneMgrImpl_GetZoneSecurityState,
1196 ZoneMgrImpl_GetIESecurityState,
1197 ZoneMgrImpl_FixInsecureSettings,
1200 HRESULT ZoneMgrImpl_Construct(IUnknown *pUnkOuter, LPVOID *ppobj)
1202 ZoneMgrImpl* ret = heap_alloc_zero(sizeof(ZoneMgrImpl));
1204 TRACE("(%p %p)\n", pUnkOuter, ppobj);
1205 ret->lpVtbl = &ZoneMgrImplVtbl;
1206 ret->ref = 1;
1207 *ppobj = (IInternetZoneManagerEx*)ret;
1209 URLMON_LockModule();
1211 return S_OK;
1214 /***********************************************************************
1215 * CoInternetCreateSecurityManager (URLMON.@)
1218 HRESULT WINAPI CoInternetCreateSecurityManager( IServiceProvider *pSP,
1219 IInternetSecurityManager **ppSM, DWORD dwReserved )
1221 TRACE("%p %p %d\n", pSP, ppSM, dwReserved );
1223 if(pSP)
1224 FIXME("pSP not supported\n");
1226 return SecManagerImpl_Construct(NULL, (void**) ppSM);
1229 /********************************************************************
1230 * CoInternetCreateZoneManager (URLMON.@)
1232 HRESULT WINAPI CoInternetCreateZoneManager(IServiceProvider* pSP, IInternetZoneManager** ppZM, DWORD dwReserved)
1234 TRACE("(%p %p %x)\n", pSP, ppZM, dwReserved);
1235 return ZoneMgrImpl_Construct(NULL, (void**)ppZM);
1238 /********************************************************************
1239 * CoInternetGetSecurityUrl (URLMON.@)
1241 HRESULT WINAPI CoInternetGetSecurityUrl(LPCWSTR pwzUrl, LPWSTR *ppwzSecUrl, PSUACTION psuAction, DWORD dwReserved)
1243 WCHAR buf1[INTERNET_MAX_URL_LENGTH], buf2[INTERNET_MAX_URL_LENGTH];
1244 LPWSTR url, domain;
1245 DWORD len;
1246 HRESULT hres;
1248 TRACE("(%p,%p,%u,%u)\n", pwzUrl, ppwzSecUrl, psuAction, dwReserved);
1250 url = buf1;
1251 domain = buf2;
1252 strcpyW(url, pwzUrl);
1254 while(1) {
1255 hres = CoInternetParseUrl(url, PARSE_SECURITY_URL, 0, domain, INTERNET_MAX_URL_LENGTH, &len, 0);
1256 if(hres!=S_OK || !strcmpW(url, domain))
1257 break;
1259 if(url == buf1) {
1260 url = buf2;
1261 domain = buf1;
1262 } else {
1263 url = buf1;
1264 domain = buf2;
1268 if(psuAction==PSU_SECURITY_URL_ONLY) {
1269 len = lstrlenW(url)+1;
1270 *ppwzSecUrl = CoTaskMemAlloc(len*sizeof(WCHAR));
1271 if(!*ppwzSecUrl)
1272 return E_OUTOFMEMORY;
1274 memcpy(*ppwzSecUrl, url, len*sizeof(WCHAR));
1275 return S_OK;
1278 hres = CoInternetParseUrl(url, PARSE_SECURITY_DOMAIN, 0, domain,
1279 INTERNET_MAX_URL_LENGTH, &len, 0);
1280 if(SUCCEEDED(hres)) {
1281 len++;
1282 *ppwzSecUrl = CoTaskMemAlloc(len*sizeof(WCHAR));
1283 if(!*ppwzSecUrl)
1284 return E_OUTOFMEMORY;
1286 memcpy(*ppwzSecUrl, domain, len*sizeof(WCHAR));
1287 return S_OK;
1290 hres = CoInternetParseUrl(url, PARSE_SCHEMA, 0, domain,
1291 INTERNET_MAX_URL_LENGTH, &len, 0);
1292 if(hres == S_OK){
1293 const WCHAR fileW[] = {'f','i','l','e',0};
1294 if(!strcmpW(domain, fileW)){
1295 hres = CoInternetParseUrl(url, PARSE_ROOTDOCUMENT, 0, domain, INTERNET_MAX_URL_LENGTH, &len, 0);
1296 }else{
1297 domain[len] = ':';
1298 hres = CoInternetParseUrl(url, PARSE_DOMAIN, 0, domain+len+1,
1299 INTERNET_MAX_URL_LENGTH-len-1, &len, 0);
1300 if(hres == S_OK) {
1301 len = lstrlenW(domain)+1;
1302 *ppwzSecUrl = CoTaskMemAlloc(len*sizeof(WCHAR));
1303 if(!*ppwzSecUrl)
1304 return E_OUTOFMEMORY;
1306 memcpy(*ppwzSecUrl, domain, len*sizeof(WCHAR));
1307 return S_OK;
1310 }else
1311 return hres;
1313 len = lstrlenW(url)+1;
1314 *ppwzSecUrl = CoTaskMemAlloc(len*sizeof(WCHAR));
1315 if(!*ppwzSecUrl)
1316 return E_OUTOFMEMORY;
1318 memcpy(*ppwzSecUrl, url, len*sizeof(WCHAR));
1319 return S_OK;