dpwsockx: Implementation of SPInit
[wine/gsoc_dplay.git] / dlls / urlmon / sec_mgr.c
blob9d0e7994cbfce3df3238dbd32dd1e654e81e85f4
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 secur_url = heap_alloc(INTERNET_MAX_URL_LENGTH*sizeof(WCHAR));
151 *zone = -1;
153 hres = CoInternetParseUrl(url, PARSE_SECURITY_URL, 0, secur_url, INTERNET_MAX_URL_LENGTH, &size, 0);
154 if(hres != S_OK)
155 strcpyW(secur_url, url);
157 hres = CoInternetParseUrl(secur_url, PARSE_SCHEMA, 0, schema, sizeof(schema)/sizeof(WCHAR), &size, 0);
158 if(FAILED(hres) || !*schema) {
159 heap_free(secur_url);
160 return E_INVALIDARG;
163 /* file protocol is a special case */
164 if(!strcmpW(schema, fileW)) {
165 WCHAR path[MAX_PATH], root[20];
166 WCHAR *ptr;
168 hres = CoInternetParseUrl(secur_url, PARSE_PATH_FROM_URL, 0, path,
169 sizeof(path)/sizeof(WCHAR), &size, 0);
171 if(SUCCEEDED(hres) && (ptr = strchrW(path, '\\')) && ptr-path < sizeof(root)/sizeof(WCHAR)) {
172 UINT type;
174 memcpy(root, path, (ptr-path)*sizeof(WCHAR));
175 root[ptr-path] = 0;
177 type = GetDriveTypeW(root);
179 switch(type) {
180 case DRIVE_UNKNOWN:
181 case DRIVE_NO_ROOT_DIR:
182 break;
183 case DRIVE_REMOVABLE:
184 case DRIVE_FIXED:
185 case DRIVE_CDROM:
186 case DRIVE_RAMDISK:
187 *zone = 0;
188 hres = S_OK;
189 break;
190 case DRIVE_REMOTE:
191 *zone = 3;
192 hres = S_OK;
193 break;
194 default:
195 FIXME("unsupported drive type %d\n", type);
200 if(*zone == -1) {
201 WARN("domains are not yet implemented\n");
202 hres = get_zone_from_reg(schema, zone);
205 if(FAILED(hres) || !ret_url)
206 heap_free(secur_url);
207 else
208 *ret_url = secur_url;
210 return hres;
213 static HRESULT open_zone_key(HKEY parent_key, DWORD zone, HKEY *hkey)
215 static const WCHAR wszFormat[] = {'%','s','%','l','d',0};
217 WCHAR key_name[sizeof(wszZonesKey)/sizeof(WCHAR)+8];
218 DWORD res;
220 wsprintfW(key_name, wszFormat, wszZonesKey, zone);
222 res = RegOpenKeyW(parent_key, key_name, hkey);
224 if(res != ERROR_SUCCESS) {
225 WARN("RegOpenKey failed\n");
226 return E_INVALIDARG;
229 return S_OK;
232 static HRESULT get_action_policy(DWORD zone, DWORD action, BYTE *policy, DWORD size, URLZONEREG zone_reg)
234 HKEY parent_key;
235 HKEY hkey;
236 LONG res;
237 HRESULT hres;
239 switch(action) {
240 case URLACTION_SCRIPT_OVERRIDE_SAFETY:
241 *(DWORD*)policy = URLPOLICY_DISALLOW;
242 return S_OK;
245 switch(zone_reg) {
246 case URLZONEREG_DEFAULT:
247 case URLZONEREG_HKCU:
248 parent_key = HKEY_CURRENT_USER;
249 break;
250 case URLZONEREG_HKLM:
251 parent_key = HKEY_LOCAL_MACHINE;
252 break;
253 default:
254 WARN("Unknown URLZONEREG: %d\n", zone_reg);
255 return E_FAIL;
258 hres = open_zone_key(parent_key, zone, &hkey);
259 if(SUCCEEDED(hres)) {
260 WCHAR action_str[16];
261 DWORD len = size;
263 static const WCHAR formatW[] = {'%','X',0};
265 wsprintfW(action_str, formatW, action);
267 res = RegQueryValueExW(hkey, action_str, NULL, NULL, policy, &len);
268 if(res == ERROR_MORE_DATA) {
269 hres = E_INVALIDARG;
270 }else if(res == ERROR_FILE_NOT_FOUND) {
271 hres = E_FAIL;
272 }else if(res != ERROR_SUCCESS) {
273 ERR("RegQueryValue failed: %d\n", res);
274 hres = E_UNEXPECTED;
277 RegCloseKey(hkey);
280 if(FAILED(hres) && zone_reg == URLZONEREG_DEFAULT)
281 return get_action_policy(zone, action, policy, size, URLZONEREG_HKLM);
283 return hres;
286 /***********************************************************************
287 * InternetSecurityManager implementation
290 typedef struct {
291 const IInternetSecurityManagerVtbl* lpInternetSecurityManagerVtbl;
293 LONG ref;
295 IInternetSecurityMgrSite *mgrsite;
296 IInternetSecurityManager *custom_manager;
297 } SecManagerImpl;
299 #define SECMGR_THIS(iface) DEFINE_THIS(SecManagerImpl, InternetSecurityManager, iface)
301 static HRESULT WINAPI SecManagerImpl_QueryInterface(IInternetSecurityManager* iface,REFIID riid,void** ppvObject)
303 SecManagerImpl *This = SECMGR_THIS(iface);
305 TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppvObject);
307 /* Perform a sanity check on the parameters.*/
308 if ( (This==0) || (ppvObject==0) )
309 return E_INVALIDARG;
311 /* Initialize the return parameter */
312 *ppvObject = 0;
314 /* Compare the riid with the interface IDs implemented by this object.*/
315 if (IsEqualIID(&IID_IUnknown, riid) ||
316 IsEqualIID(&IID_IInternetSecurityManager, riid))
317 *ppvObject = iface;
319 /* Check that we obtained an interface.*/
320 if (!*ppvObject) {
321 WARN("not supported interface %s\n", debugstr_guid(riid));
322 return E_NOINTERFACE;
325 /* Query Interface always increases the reference count by one when it is successful */
326 IInternetSecurityManager_AddRef(iface);
328 return S_OK;
331 static ULONG WINAPI SecManagerImpl_AddRef(IInternetSecurityManager* iface)
333 SecManagerImpl *This = SECMGR_THIS(iface);
334 ULONG refCount = InterlockedIncrement(&This->ref);
336 TRACE("(%p) ref=%u\n", This, refCount);
338 return refCount;
341 static ULONG WINAPI SecManagerImpl_Release(IInternetSecurityManager* iface)
343 SecManagerImpl *This = SECMGR_THIS(iface);
344 ULONG refCount = InterlockedDecrement(&This->ref);
346 TRACE("(%p) ref=%u\n", This, refCount);
348 /* destroy the object if there's no more reference on it */
349 if (!refCount){
350 if(This->mgrsite)
351 IInternetSecurityMgrSite_Release(This->mgrsite);
352 if(This->custom_manager)
353 IInternetSecurityManager_Release(This->custom_manager);
355 heap_free(This);
357 URLMON_UnlockModule();
360 return refCount;
363 static HRESULT WINAPI SecManagerImpl_SetSecuritySite(IInternetSecurityManager *iface,
364 IInternetSecurityMgrSite *pSite)
366 SecManagerImpl *This = SECMGR_THIS(iface);
368 TRACE("(%p)->(%p)\n", This, pSite);
370 if(This->mgrsite)
371 IInternetSecurityMgrSite_Release(This->mgrsite);
373 if(This->custom_manager) {
374 IInternetSecurityManager_Release(This->custom_manager);
375 This->custom_manager = NULL;
378 This->mgrsite = pSite;
380 if(pSite) {
381 IServiceProvider *servprov;
382 HRESULT hres;
384 IInternetSecurityMgrSite_AddRef(pSite);
386 hres = IInternetSecurityMgrSite_QueryInterface(pSite, &IID_IServiceProvider,
387 (void**)&servprov);
388 if(SUCCEEDED(hres)) {
389 IServiceProvider_QueryService(servprov, &SID_SInternetSecurityManager,
390 &IID_IInternetSecurityManager, (void**)&This->custom_manager);
391 IServiceProvider_Release(servprov);
395 return S_OK;
398 static HRESULT WINAPI SecManagerImpl_GetSecuritySite(IInternetSecurityManager *iface,
399 IInternetSecurityMgrSite **ppSite)
401 SecManagerImpl *This = SECMGR_THIS(iface);
403 TRACE("(%p)->(%p)\n", This, ppSite);
405 if(!ppSite)
406 return E_INVALIDARG;
408 if(This->mgrsite)
409 IInternetSecurityMgrSite_AddRef(This->mgrsite);
411 *ppSite = This->mgrsite;
412 return S_OK;
415 static HRESULT WINAPI SecManagerImpl_MapUrlToZone(IInternetSecurityManager *iface,
416 LPCWSTR pwszUrl, DWORD *pdwZone,
417 DWORD dwFlags)
419 SecManagerImpl *This = SECMGR_THIS(iface);
420 HRESULT hres;
422 TRACE("(%p)->(%s %p %08x)\n", iface, debugstr_w(pwszUrl), pdwZone, dwFlags);
424 if(This->custom_manager) {
425 hres = IInternetSecurityManager_MapUrlToZone(This->custom_manager,
426 pwszUrl, pdwZone, dwFlags);
427 if(hres != INET_E_DEFAULT_ACTION)
428 return hres;
431 if(!pwszUrl) {
432 *pdwZone = -1;
433 return E_INVALIDARG;
436 if(dwFlags)
437 FIXME("not supported flags: %08x\n", dwFlags);
439 return map_url_to_zone(pwszUrl, pdwZone, NULL);
442 static HRESULT WINAPI SecManagerImpl_GetSecurityId(IInternetSecurityManager *iface,
443 LPCWSTR pwszUrl, BYTE *pbSecurityId, DWORD *pcbSecurityId, DWORD_PTR dwReserved)
445 SecManagerImpl *This = SECMGR_THIS(iface);
446 LPWSTR url, ptr, ptr2;
447 DWORD zone, len;
448 HRESULT hres;
450 static const WCHAR wszFile[] = {'f','i','l','e',':'};
452 TRACE("(%p)->(%s %p %p %08lx)\n", iface, debugstr_w(pwszUrl), pbSecurityId,
453 pcbSecurityId, dwReserved);
455 if(This->custom_manager) {
456 hres = IInternetSecurityManager_GetSecurityId(This->custom_manager,
457 pwszUrl, pbSecurityId, pcbSecurityId, dwReserved);
458 if(hres != INET_E_DEFAULT_ACTION)
459 return hres;
462 if(!pwszUrl || !pbSecurityId || !pcbSecurityId)
463 return E_INVALIDARG;
465 if(dwReserved)
466 FIXME("dwReserved is not supported\n");
468 hres = map_url_to_zone(pwszUrl, &zone, &url);
469 if(FAILED(hres))
470 return hres == 0x80041001 ? E_INVALIDARG : hres;
472 /* file protocol is a special case */
473 if(strlenW(url) >= sizeof(wszFile)/sizeof(WCHAR)
474 && !memcmp(url, wszFile, sizeof(wszFile)) && strchrW(url, '\\')) {
476 static const BYTE secidFile[] = {'f','i','l','e',':'};
478 heap_free(url);
480 if(*pcbSecurityId < sizeof(secidFile)+sizeof(zone))
481 return HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
483 memcpy(pbSecurityId, secidFile, sizeof(secidFile));
484 *(DWORD*)(pbSecurityId+sizeof(secidFile)) = zone;
486 *pcbSecurityId = sizeof(secidFile)+sizeof(zone);
487 return S_OK;
490 ptr = strchrW(url, ':');
491 ptr2 = ++ptr;
492 while(*ptr2 == '/')
493 ptr2++;
494 if(ptr2 != ptr)
495 memmove(ptr, ptr2, (strlenW(ptr2)+1)*sizeof(WCHAR));
497 ptr = strchrW(ptr, '/');
498 if(ptr)
499 *ptr = 0;
501 len = WideCharToMultiByte(CP_ACP, 0, url, -1, NULL, 0, NULL, NULL)-1;
503 if(len+sizeof(DWORD) > *pcbSecurityId) {
504 heap_free(url);
505 return HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
508 WideCharToMultiByte(CP_ACP, 0, url, -1, (LPSTR)pbSecurityId, len, NULL, NULL);
509 heap_free(url);
511 *(DWORD*)(pbSecurityId+len) = zone;
513 *pcbSecurityId = len+sizeof(DWORD);
515 return S_OK;
519 static HRESULT WINAPI SecManagerImpl_ProcessUrlAction(IInternetSecurityManager *iface,
520 LPCWSTR pwszUrl, DWORD dwAction,
521 BYTE *pPolicy, DWORD cbPolicy,
522 BYTE *pContext, DWORD cbContext,
523 DWORD dwFlags, DWORD dwReserved)
525 SecManagerImpl *This = SECMGR_THIS(iface);
526 DWORD zone, policy;
527 HRESULT hres;
529 TRACE("(%p)->(%s %08x %p %08x %p %08x %08x %08x)\n", iface, debugstr_w(pwszUrl), dwAction,
530 pPolicy, cbPolicy, pContext, cbContext, dwFlags, dwReserved);
532 if(This->custom_manager) {
533 hres = IInternetSecurityManager_ProcessUrlAction(This->custom_manager, pwszUrl, dwAction,
534 pPolicy, cbPolicy, pContext, cbContext, dwFlags, dwReserved);
535 if(hres != INET_E_DEFAULT_ACTION)
536 return hres;
539 if(pContext || cbContext || dwFlags || dwReserved)
540 FIXME("Unsupported arguments\n");
542 if(!pwszUrl)
543 return E_INVALIDARG;
545 hres = map_url_to_zone(pwszUrl, &zone, NULL);
546 if(FAILED(hres))
547 return hres;
549 hres = get_action_policy(zone, dwAction, (BYTE*)&policy, sizeof(policy), URLZONEREG_DEFAULT);
550 if(FAILED(hres))
551 return hres;
553 TRACE("policy %x\n", policy);
555 switch(GetUrlPolicyPermissions(policy)) {
556 case URLPOLICY_ALLOW:
557 case URLPOLICY_CHANNEL_SOFTDIST_PRECACHE:
558 return S_OK;
559 case URLPOLICY_DISALLOW:
560 return S_FALSE;
561 case URLPOLICY_QUERY:
562 FIXME("URLPOLICY_QUERY not implemented\n");
563 return E_FAIL;
564 default:
565 FIXME("Not implemented policy %x\n", policy);
568 return E_FAIL;
572 static HRESULT WINAPI SecManagerImpl_QueryCustomPolicy(IInternetSecurityManager *iface,
573 LPCWSTR pwszUrl, REFGUID guidKey,
574 BYTE **ppPolicy, DWORD *pcbPolicy,
575 BYTE *pContext, DWORD cbContext,
576 DWORD dwReserved)
578 SecManagerImpl *This = SECMGR_THIS(iface);
579 HRESULT hres;
581 TRACE("(%p)->(%s %s %p %p %p %08x %08x )\n", iface, debugstr_w(pwszUrl), debugstr_guid(guidKey),
582 ppPolicy, pcbPolicy, pContext, cbContext, dwReserved);
584 if(This->custom_manager) {
585 hres = IInternetSecurityManager_QueryCustomPolicy(This->custom_manager, pwszUrl, guidKey,
586 ppPolicy, pcbPolicy, pContext, cbContext, dwReserved);
587 if(hres != INET_E_DEFAULT_ACTION)
588 return hres;
591 FIXME("Default action is not implemented\n");
592 return E_NOTIMPL;
595 static HRESULT WINAPI SecManagerImpl_SetZoneMapping(IInternetSecurityManager *iface,
596 DWORD dwZone, LPCWSTR pwszPattern, DWORD dwFlags)
598 SecManagerImpl *This = SECMGR_THIS(iface);
599 HRESULT hres;
601 TRACE("(%p)->(%08x %s %08x)\n", iface, dwZone, debugstr_w(pwszPattern),dwFlags);
603 if(This->custom_manager) {
604 hres = IInternetSecurityManager_SetZoneMapping(This->custom_manager, dwZone,
605 pwszPattern, dwFlags);
606 if(hres != INET_E_DEFAULT_ACTION)
607 return hres;
610 FIXME("Default action is not implemented\n");
611 return E_NOTIMPL;
614 static HRESULT WINAPI SecManagerImpl_GetZoneMappings(IInternetSecurityManager *iface,
615 DWORD dwZone, IEnumString **ppenumString, DWORD dwFlags)
617 SecManagerImpl *This = SECMGR_THIS(iface);
618 HRESULT hres;
620 TRACE("(%p)->(%08x %p %08x)\n", iface, dwZone, ppenumString,dwFlags);
622 if(This->custom_manager) {
623 hres = IInternetSecurityManager_GetZoneMappings(This->custom_manager, dwZone,
624 ppenumString, dwFlags);
625 if(hres != INET_E_DEFAULT_ACTION)
626 return hres;
629 FIXME("Default action is not implemented\n");
630 return E_NOTIMPL;
633 static const IInternetSecurityManagerVtbl VT_SecManagerImpl =
635 SecManagerImpl_QueryInterface,
636 SecManagerImpl_AddRef,
637 SecManagerImpl_Release,
638 SecManagerImpl_SetSecuritySite,
639 SecManagerImpl_GetSecuritySite,
640 SecManagerImpl_MapUrlToZone,
641 SecManagerImpl_GetSecurityId,
642 SecManagerImpl_ProcessUrlAction,
643 SecManagerImpl_QueryCustomPolicy,
644 SecManagerImpl_SetZoneMapping,
645 SecManagerImpl_GetZoneMappings
648 HRESULT SecManagerImpl_Construct(IUnknown *pUnkOuter, LPVOID *ppobj)
650 SecManagerImpl *This;
652 TRACE("(%p,%p)\n",pUnkOuter,ppobj);
653 This = heap_alloc(sizeof(*This));
655 /* Initialize the virtual function table. */
656 This->lpInternetSecurityManagerVtbl = &VT_SecManagerImpl;
658 This->ref = 1;
659 This->mgrsite = NULL;
660 This->custom_manager = NULL;
662 *ppobj = This;
664 URLMON_LockModule();
666 return S_OK;
669 /***********************************************************************
670 * InternetZoneManager implementation
673 typedef struct {
674 const IInternetZoneManagerEx2Vtbl* lpVtbl;
675 LONG ref;
676 LPDWORD *zonemaps;
677 DWORD zonemap_count;
678 } ZoneMgrImpl;
681 /***********************************************************************
682 * build_zonemap_from_reg [internal]
684 * Enumerate the Zones in the Registry and return the Zones in a DWORD-array
685 * The number of the Zones is returned in data[0]
687 static LPDWORD build_zonemap_from_reg(void)
689 WCHAR name[32];
690 HKEY hkey;
691 LPDWORD data = NULL;
692 DWORD allocated = 6; /* space for the zonecount and Zone "0" up to Zone "4" */
693 DWORD used = 0;
694 DWORD res;
695 DWORD len;
698 res = RegOpenKeyW(HKEY_CURRENT_USER, wszZonesKey, &hkey);
699 if (res)
700 return NULL;
702 data = heap_alloc(allocated * sizeof(DWORD));
703 if (!data)
704 goto cleanup;
706 while (!res) {
707 name[0] = '\0';
708 len = sizeof(name) / sizeof(name[0]);
709 res = RegEnumKeyExW(hkey, used, name, &len, NULL, NULL, NULL, NULL);
711 if (!res) {
712 used++;
713 if (used == allocated) {
714 LPDWORD new_data;
716 allocated *= 2;
717 new_data = heap_realloc_zero(data, allocated * sizeof(DWORD));
718 if (!new_data)
719 goto cleanup;
721 data = new_data;
723 data[used] = atoiW(name);
726 if (used) {
727 RegCloseKey(hkey);
728 data[0] = used;
729 return data;
732 cleanup:
733 /* something failed */
734 RegCloseKey(hkey);
735 heap_free(data);
736 return NULL;
739 /********************************************************************
740 * IInternetZoneManager_QueryInterface
742 static HRESULT WINAPI ZoneMgrImpl_QueryInterface(IInternetZoneManagerEx2* iface, REFIID riid, void** ppvObject)
744 ZoneMgrImpl* This = (ZoneMgrImpl*)iface;
746 TRACE("(%p)->(%s,%p)\n", This, debugstr_guid(riid), ppvObject);
748 if(!This || !ppvObject)
749 return E_INVALIDARG;
751 if(IsEqualIID(&IID_IUnknown, riid)) {
752 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppvObject);
753 }else if(IsEqualIID(&IID_IInternetZoneManager, riid)) {
754 TRACE("(%p)->(IID_InternetZoneManager %p)\n", This, ppvObject);
755 }else if(IsEqualIID(&IID_IInternetZoneManagerEx, riid)) {
756 TRACE("(%p)->(IID_InternetZoneManagerEx %p)\n", This, ppvObject);
757 }else if(IsEqualIID(&IID_IInternetZoneManagerEx2, riid)) {
758 TRACE("(%p)->(IID_InternetZoneManagerEx2 %p)\n", This, ppvObject);
760 else
762 FIXME("Unknown interface: %s\n", debugstr_guid(riid));
763 *ppvObject = NULL;
764 return E_NOINTERFACE;
767 *ppvObject = iface;
768 IInternetZoneManager_AddRef(iface);
769 return S_OK;
772 /********************************************************************
773 * IInternetZoneManager_AddRef
775 static ULONG WINAPI ZoneMgrImpl_AddRef(IInternetZoneManagerEx2* iface)
777 ZoneMgrImpl* This = (ZoneMgrImpl*)iface;
778 ULONG refCount = InterlockedIncrement(&This->ref);
780 TRACE("(%p)->(ref before=%u)\n",This, refCount - 1);
782 return refCount;
785 /********************************************************************
786 * IInternetZoneManager_Release
788 static ULONG WINAPI ZoneMgrImpl_Release(IInternetZoneManagerEx2* iface)
790 ZoneMgrImpl* This = (ZoneMgrImpl*)iface;
791 ULONG refCount = InterlockedDecrement(&This->ref);
793 TRACE("(%p)->(ref before=%u)\n",This, refCount + 1);
795 if(!refCount) {
796 while (This->zonemap_count) heap_free(This->zonemaps[--This->zonemap_count]);
797 heap_free(This->zonemaps);
798 heap_free(This);
799 URLMON_UnlockModule();
802 return refCount;
805 /********************************************************************
806 * IInternetZoneManager_GetZoneAttributes
808 static HRESULT WINAPI ZoneMgrImpl_GetZoneAttributes(IInternetZoneManagerEx2* iface,
809 DWORD dwZone,
810 ZONEATTRIBUTES* pZoneAttributes)
812 ZoneMgrImpl* This = (ZoneMgrImpl*)iface;
813 HRESULT hr;
814 HKEY hcu;
815 HKEY hklm = NULL;
817 TRACE("(%p)->(%d %p)\n", This, dwZone, pZoneAttributes);
819 if (!pZoneAttributes)
820 return E_INVALIDARG;
822 hr = open_zone_key(HKEY_CURRENT_USER, dwZone, &hcu);
823 if (FAILED(hr))
824 return S_OK; /* IE6 and older returned E_FAIL here */
826 hr = open_zone_key(HKEY_LOCAL_MACHINE, dwZone, &hklm);
827 if (FAILED(hr))
828 TRACE("Zone %d not in HKLM\n", dwZone);
830 get_string_from_reg(hcu, hklm, displaynameW, pZoneAttributes->szDisplayName, MAX_ZONE_PATH);
831 get_string_from_reg(hcu, hklm, descriptionW, pZoneAttributes->szDescription, MAX_ZONE_DESCRIPTION);
832 get_string_from_reg(hcu, hklm, iconW, pZoneAttributes->szIconPath, MAX_ZONE_PATH);
833 get_dword_from_reg(hcu, hklm, minlevelW, &pZoneAttributes->dwTemplateMinLevel);
834 get_dword_from_reg(hcu, hklm, currentlevelW, &pZoneAttributes->dwTemplateCurrentLevel);
835 get_dword_from_reg(hcu, hklm, recommendedlevelW, &pZoneAttributes->dwTemplateRecommended);
836 get_dword_from_reg(hcu, hklm, flagsW, &pZoneAttributes->dwFlags);
838 RegCloseKey(hklm);
839 RegCloseKey(hcu);
840 return S_OK;
843 /********************************************************************
844 * IInternetZoneManager_SetZoneAttributes
846 static HRESULT WINAPI ZoneMgrImpl_SetZoneAttributes(IInternetZoneManagerEx2* iface,
847 DWORD dwZone,
848 ZONEATTRIBUTES* pZoneAttributes)
850 FIXME("(%p)->(%08x %p) stub\n", iface, dwZone, pZoneAttributes);
851 return E_NOTIMPL;
854 /********************************************************************
855 * IInternetZoneManager_GetZoneCustomPolicy
857 static HRESULT WINAPI ZoneMgrImpl_GetZoneCustomPolicy(IInternetZoneManagerEx2* iface,
858 DWORD dwZone,
859 REFGUID guidKey,
860 BYTE** ppPolicy,
861 DWORD* pcbPolicy,
862 URLZONEREG ulrZoneReg)
864 FIXME("(%p)->(%08x %s %p %p %08x) stub\n", iface, dwZone, debugstr_guid(guidKey),
865 ppPolicy, pcbPolicy, ulrZoneReg);
866 return E_NOTIMPL;
869 /********************************************************************
870 * IInternetZoneManager_SetZoneCustomPolicy
872 static HRESULT WINAPI ZoneMgrImpl_SetZoneCustomPolicy(IInternetZoneManagerEx2* iface,
873 DWORD dwZone,
874 REFGUID guidKey,
875 BYTE* ppPolicy,
876 DWORD cbPolicy,
877 URLZONEREG ulrZoneReg)
879 FIXME("(%p)->(%08x %s %p %08x %08x) stub\n", iface, dwZone, debugstr_guid(guidKey),
880 ppPolicy, cbPolicy, ulrZoneReg);
881 return E_NOTIMPL;
884 /********************************************************************
885 * IInternetZoneManager_GetZoneActionPolicy
887 static HRESULT WINAPI ZoneMgrImpl_GetZoneActionPolicy(IInternetZoneManagerEx2* iface,
888 DWORD dwZone, DWORD dwAction, BYTE* pPolicy, DWORD cbPolicy, URLZONEREG urlZoneReg)
890 TRACE("(%p)->(%d %08x %p %d %d)\n", iface, dwZone, dwAction, pPolicy,
891 cbPolicy, urlZoneReg);
893 if(!pPolicy)
894 return E_INVALIDARG;
896 return get_action_policy(dwZone, dwAction, pPolicy, cbPolicy, urlZoneReg);
899 /********************************************************************
900 * IInternetZoneManager_SetZoneActionPolicy
902 static HRESULT WINAPI ZoneMgrImpl_SetZoneActionPolicy(IInternetZoneManagerEx2* iface,
903 DWORD dwZone,
904 DWORD dwAction,
905 BYTE* pPolicy,
906 DWORD cbPolicy,
907 URLZONEREG urlZoneReg)
909 FIXME("(%p)->(%08x %08x %p %08x %08x) stub\n", iface, dwZone, dwAction, pPolicy,
910 cbPolicy, urlZoneReg);
911 return E_NOTIMPL;
914 /********************************************************************
915 * IInternetZoneManager_PromptAction
917 static HRESULT WINAPI ZoneMgrImpl_PromptAction(IInternetZoneManagerEx2* iface,
918 DWORD dwAction,
919 HWND hwndParent,
920 LPCWSTR pwszUrl,
921 LPCWSTR pwszText,
922 DWORD dwPromptFlags)
924 FIXME("%p %08x %p %s %s %08x\n", iface, dwAction, hwndParent,
925 debugstr_w(pwszUrl), debugstr_w(pwszText), dwPromptFlags );
926 return E_NOTIMPL;
929 /********************************************************************
930 * IInternetZoneManager_LogAction
932 static HRESULT WINAPI ZoneMgrImpl_LogAction(IInternetZoneManagerEx2* iface,
933 DWORD dwAction,
934 LPCWSTR pwszUrl,
935 LPCWSTR pwszText,
936 DWORD dwLogFlags)
938 FIXME("(%p)->(%08x %s %s %08x) stub\n", iface, dwAction, debugstr_w(pwszUrl),
939 debugstr_w(pwszText), dwLogFlags);
940 return E_NOTIMPL;
943 /********************************************************************
944 * IInternetZoneManager_CreateZoneEnumerator
946 static HRESULT WINAPI ZoneMgrImpl_CreateZoneEnumerator(IInternetZoneManagerEx2* iface,
947 DWORD* pdwEnum,
948 DWORD* pdwCount,
949 DWORD dwFlags)
951 ZoneMgrImpl* This = (ZoneMgrImpl*)iface;
952 LPDWORD * new_maps;
953 LPDWORD data;
954 DWORD i;
956 TRACE("(%p)->(%p, %p, 0x%08x)\n", This, pdwEnum, pdwCount, dwFlags);
957 if (!pdwEnum || !pdwCount || (dwFlags != 0))
958 return E_INVALIDARG;
960 data = build_zonemap_from_reg();
961 TRACE("found %d zones\n", data ? data[0] : -1);
963 if (!data)
964 return E_FAIL;
966 for (i = 0; i < This->zonemap_count; i++) {
967 if (This->zonemaps && !This->zonemaps[i]) {
968 This->zonemaps[i] = data;
969 *pdwEnum = i;
970 *pdwCount = data[0];
971 return S_OK;
975 if (This->zonemaps) {
976 /* try to double the nr. of pointers in the array */
977 new_maps = heap_realloc_zero(This->zonemaps, This->zonemap_count * 2 * sizeof(LPDWORD));
978 if (new_maps)
979 This->zonemap_count *= 2;
981 else
983 This->zonemap_count = 2;
984 new_maps = heap_alloc_zero(This->zonemap_count * sizeof(LPDWORD));
987 if (!new_maps) {
988 heap_free(data);
989 return E_FAIL;
991 This->zonemaps = new_maps;
992 This->zonemaps[i] = data;
993 *pdwEnum = i;
994 *pdwCount = data[0];
995 return S_OK;
998 /********************************************************************
999 * IInternetZoneManager_GetZoneAt
1001 static HRESULT WINAPI ZoneMgrImpl_GetZoneAt(IInternetZoneManagerEx2* iface,
1002 DWORD dwEnum,
1003 DWORD dwIndex,
1004 DWORD* pdwZone)
1006 ZoneMgrImpl* This = (ZoneMgrImpl*)iface;
1007 LPDWORD data;
1009 TRACE("(%p)->(0x%08x, %d, %p)\n", This, dwEnum, dwIndex, pdwZone);
1011 /* make sure, that dwEnum and dwIndex are in the valid range */
1012 if (dwEnum < This->zonemap_count) {
1013 if ((data = This->zonemaps[dwEnum])) {
1014 if (dwIndex < data[0]) {
1015 *pdwZone = data[dwIndex + 1];
1016 return S_OK;
1020 return E_INVALIDARG;
1023 /********************************************************************
1024 * IInternetZoneManager_DestroyZoneEnumerator
1026 static HRESULT WINAPI ZoneMgrImpl_DestroyZoneEnumerator(IInternetZoneManagerEx2* iface,
1027 DWORD dwEnum)
1029 ZoneMgrImpl* This = (ZoneMgrImpl*)iface;
1030 LPDWORD data;
1032 TRACE("(%p)->(0x%08x)\n", This, dwEnum);
1033 /* make sure, that dwEnum is valid */
1034 if (dwEnum < This->zonemap_count) {
1035 if ((data = This->zonemaps[dwEnum])) {
1036 This->zonemaps[dwEnum] = NULL;
1037 heap_free(data);
1038 return S_OK;
1041 return E_INVALIDARG;
1044 /********************************************************************
1045 * IInternetZoneManager_CopyTemplatePoliciesToZone
1047 static HRESULT WINAPI ZoneMgrImpl_CopyTemplatePoliciesToZone(IInternetZoneManagerEx2* iface,
1048 DWORD dwTemplate,
1049 DWORD dwZone,
1050 DWORD dwReserved)
1052 FIXME("(%p)->(%08x %08x %08x) stub\n", iface, dwTemplate, dwZone, dwReserved);
1053 return E_NOTIMPL;
1056 /********************************************************************
1057 * IInternetZoneManagerEx_GetZoneActionPolicyEx
1059 static HRESULT WINAPI ZoneMgrImpl_GetZoneActionPolicyEx(IInternetZoneManagerEx2* iface,
1060 DWORD dwZone,
1061 DWORD dwAction,
1062 BYTE* pPolicy,
1063 DWORD cbPolicy,
1064 URLZONEREG urlZoneReg,
1065 DWORD dwFlags)
1067 TRACE("(%p)->(%d, 0x%x, %p, %d, %d, 0x%x)\n", iface, dwZone,
1068 dwAction, pPolicy, cbPolicy, urlZoneReg, dwFlags);
1070 if(!pPolicy)
1071 return E_INVALIDARG;
1073 if (dwFlags)
1074 FIXME("dwFlags 0x%x ignored\n", dwFlags);
1076 return get_action_policy(dwZone, dwAction, pPolicy, cbPolicy, urlZoneReg);
1079 /********************************************************************
1080 * IInternetZoneManagerEx_SetZoneActionPolicyEx
1082 static HRESULT WINAPI ZoneMgrImpl_SetZoneActionPolicyEx(IInternetZoneManagerEx2* iface,
1083 DWORD dwZone,
1084 DWORD dwAction,
1085 BYTE* pPolicy,
1086 DWORD cbPolicy,
1087 URLZONEREG urlZoneReg,
1088 DWORD dwFlags)
1090 FIXME("(%p)->(%d, 0x%x, %p, %d, %d, 0x%x) stub\n", iface, dwZone, dwAction, pPolicy,
1091 cbPolicy, urlZoneReg, dwFlags);
1092 return E_NOTIMPL;
1095 /********************************************************************
1096 * IInternetZoneManagerEx2_GetZoneAttributesEx
1098 static HRESULT WINAPI ZoneMgrImpl_GetZoneAttributesEx(IInternetZoneManagerEx2* iface,
1099 DWORD dwZone,
1100 ZONEATTRIBUTES* pZoneAttributes,
1101 DWORD dwFlags)
1103 TRACE("(%p)->(%d, %p, 0x%x)\n", iface, dwZone, pZoneAttributes, dwFlags);
1105 if (dwFlags)
1106 FIXME("dwFlags 0x%x ignored\n", dwFlags);
1108 return IInternetZoneManager_GetZoneAttributes(iface, dwZone, pZoneAttributes);
1112 /********************************************************************
1113 * IInternetZoneManagerEx2_GetZoneSecurityState
1115 static HRESULT WINAPI ZoneMgrImpl_GetZoneSecurityState(IInternetZoneManagerEx2* iface,
1116 DWORD dwZoneIndex,
1117 BOOL fRespectPolicy,
1118 LPDWORD pdwState,
1119 BOOL *pfPolicyEncountered)
1121 FIXME("(%p)->(%d, %d, %p, %p) stub\n", iface, dwZoneIndex, fRespectPolicy,
1122 pdwState, pfPolicyEncountered);
1124 *pdwState = SECURITY_IE_STATE_GREEN;
1126 if (pfPolicyEncountered)
1127 *pfPolicyEncountered = FALSE;
1129 return S_OK;
1132 /********************************************************************
1133 * IInternetZoneManagerEx2_GetIESecurityState
1135 static HRESULT WINAPI ZoneMgrImpl_GetIESecurityState(IInternetZoneManagerEx2* iface,
1136 BOOL fRespectPolicy,
1137 LPDWORD pdwState,
1138 BOOL *pfPolicyEncountered,
1139 BOOL fNoCache)
1141 FIXME("(%p)->(%d, %p, %p, %d) stub\n", iface, fRespectPolicy, pdwState,
1142 pfPolicyEncountered, fNoCache);
1144 *pdwState = SECURITY_IE_STATE_GREEN;
1146 if (pfPolicyEncountered)
1147 *pfPolicyEncountered = FALSE;
1149 return S_OK;
1152 /********************************************************************
1153 * IInternetZoneManagerEx2_FixInsecureSettings
1155 static HRESULT WINAPI ZoneMgrImpl_FixInsecureSettings(IInternetZoneManagerEx2* iface)
1157 FIXME("(%p) stub\n", iface);
1158 return S_OK;
1161 /********************************************************************
1162 * IInternetZoneManager_Construct
1164 static const IInternetZoneManagerEx2Vtbl ZoneMgrImplVtbl = {
1165 ZoneMgrImpl_QueryInterface,
1166 ZoneMgrImpl_AddRef,
1167 ZoneMgrImpl_Release,
1168 /* IInternetZoneManager */
1169 ZoneMgrImpl_GetZoneAttributes,
1170 ZoneMgrImpl_SetZoneAttributes,
1171 ZoneMgrImpl_GetZoneCustomPolicy,
1172 ZoneMgrImpl_SetZoneCustomPolicy,
1173 ZoneMgrImpl_GetZoneActionPolicy,
1174 ZoneMgrImpl_SetZoneActionPolicy,
1175 ZoneMgrImpl_PromptAction,
1176 ZoneMgrImpl_LogAction,
1177 ZoneMgrImpl_CreateZoneEnumerator,
1178 ZoneMgrImpl_GetZoneAt,
1179 ZoneMgrImpl_DestroyZoneEnumerator,
1180 ZoneMgrImpl_CopyTemplatePoliciesToZone,
1181 /* IInternetZoneManagerEx */
1182 ZoneMgrImpl_GetZoneActionPolicyEx,
1183 ZoneMgrImpl_SetZoneActionPolicyEx,
1184 /* IInternetZoneManagerEx2 */
1185 ZoneMgrImpl_GetZoneAttributesEx,
1186 ZoneMgrImpl_GetZoneSecurityState,
1187 ZoneMgrImpl_GetIESecurityState,
1188 ZoneMgrImpl_FixInsecureSettings,
1191 HRESULT ZoneMgrImpl_Construct(IUnknown *pUnkOuter, LPVOID *ppobj)
1193 ZoneMgrImpl* ret = heap_alloc_zero(sizeof(ZoneMgrImpl));
1195 TRACE("(%p %p)\n", pUnkOuter, ppobj);
1196 ret->lpVtbl = &ZoneMgrImplVtbl;
1197 ret->ref = 1;
1198 *ppobj = (IInternetZoneManagerEx*)ret;
1200 URLMON_LockModule();
1202 return S_OK;
1205 /***********************************************************************
1206 * CoInternetCreateSecurityManager (URLMON.@)
1209 HRESULT WINAPI CoInternetCreateSecurityManager( IServiceProvider *pSP,
1210 IInternetSecurityManager **ppSM, DWORD dwReserved )
1212 TRACE("%p %p %d\n", pSP, ppSM, dwReserved );
1214 if(pSP)
1215 FIXME("pSP not supported\n");
1217 return SecManagerImpl_Construct(NULL, (void**) ppSM);
1220 /********************************************************************
1221 * CoInternetCreateZoneManager (URLMON.@)
1223 HRESULT WINAPI CoInternetCreateZoneManager(IServiceProvider* pSP, IInternetZoneManager** ppZM, DWORD dwReserved)
1225 TRACE("(%p %p %x)\n", pSP, ppZM, dwReserved);
1226 return ZoneMgrImpl_Construct(NULL, (void**)ppZM);