urlmon: Fixed some tests on IE11.
[wine/wine-gecko.git] / dlls / urlmon / sec_mgr.c
blobdc57c889666224844d1e547de1af4111605b204b
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
7 * Copyright 2011 Thomas Mullaly for CodeWeavers
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include <stdio.h>
26 #include "urlmon_main.h"
27 #include "winreg.h"
28 #include "wininet.h"
30 #define NO_SHLWAPI_REG
31 #include "shlwapi.h"
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(urlmon);
37 static const WCHAR currentlevelW[] = {'C','u','r','r','e','n','t','L','e','v','e','l',0};
38 static const WCHAR descriptionW[] = {'D','e','s','c','r','i','p','t','i','o','n',0};
39 static const WCHAR displaynameW[] = {'D','i','s','p','l','a','y','N','a','m','e',0};
40 static const WCHAR fileW[] = {'f','i','l','e',0};
41 static const WCHAR flagsW[] = {'F','l','a','g','s',0};
42 static const WCHAR iconW[] = {'I','c','o','n',0};
43 static const WCHAR minlevelW[] = {'M','i','n','L','e','v','e','l',0};
44 static const WCHAR recommendedlevelW[] = {'R','e','c','o','m','m','e','n','d','e','d',
45 'L','e','v','e','l',0};
46 static const WCHAR wszZonesKey[] = {'S','o','f','t','w','a','r','e','\\',
47 'M','i','c','r','o','s','o','f','t','\\',
48 'W','i','n','d','o','w','s','\\',
49 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
50 'I','n','t','e','r','n','e','t',' ','S','e','t','t','i','n','g','s','\\',
51 'Z','o','n','e','s','\\',0};
52 static const WCHAR zone_map_keyW[] = {'S','o','f','t','w','a','r','e','\\',
53 'M','i','c','r','o','s','o','f','t','\\',
54 'W','i','n','d','o','w','s','\\',
55 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
56 'I','n','t','e','r','n','e','t',' ','S','e','t','t','i','n','g','s','\\',
57 'Z','o','n','e','M','a','p',0};
58 static const WCHAR wszZoneMapDomainsKey[] = {'S','o','f','t','w','a','r','e','\\',
59 'M','i','c','r','o','s','o','f','t','\\',
60 'W','i','n','d','o','w','s','\\',
61 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
62 'I','n','t','e','r','n','e','t',' ','S','e','t','t','i','n','g','s','\\',
63 'Z','o','n','e','M','a','p','\\',
64 'D','o','m','a','i','n','s',0};
66 static inline BOOL is_drive_path(const WCHAR *path)
68 return isalphaW(*path) && *(path+1) == ':';
71 /* List of schemes types Windows seems to expect to be hierarchical. */
72 static inline BOOL is_hierarchical_scheme(URL_SCHEME type) {
73 return(type == URL_SCHEME_HTTP || type == URL_SCHEME_FTP ||
74 type == URL_SCHEME_GOPHER || type == URL_SCHEME_NNTP ||
75 type == URL_SCHEME_TELNET || type == URL_SCHEME_WAIS ||
76 type == URL_SCHEME_FILE || type == URL_SCHEME_HTTPS ||
77 type == URL_SCHEME_RES);
80 /********************************************************************
81 * get_string_from_reg [internal]
83 * helper to get a string from the reg.
86 static void get_string_from_reg(HKEY hcu, HKEY hklm, LPCWSTR name, LPWSTR out, DWORD maxlen)
88 DWORD type = REG_SZ;
89 DWORD len = maxlen * sizeof(WCHAR);
90 DWORD res;
92 res = RegQueryValueExW(hcu, name, NULL, &type, (LPBYTE) out, &len);
94 if (res && hklm) {
95 len = maxlen * sizeof(WCHAR);
96 type = REG_SZ;
97 res = RegQueryValueExW(hklm, name, NULL, &type, (LPBYTE) out, &len);
100 if (res) {
101 TRACE("%s failed: %d\n", debugstr_w(name), res);
102 *out = '\0';
106 /********************************************************************
107 * get_dword_from_reg [internal]
109 * helper to get a dword from the reg.
112 static void get_dword_from_reg(HKEY hcu, HKEY hklm, LPCWSTR name, LPDWORD out)
114 DWORD type = REG_DWORD;
115 DWORD len = sizeof(DWORD);
116 DWORD res;
118 res = RegQueryValueExW(hcu, name, NULL, &type, (LPBYTE) out, &len);
120 if (res && hklm) {
121 len = sizeof(DWORD);
122 type = REG_DWORD;
123 res = RegQueryValueExW(hklm, name, NULL, &type, (LPBYTE) out, &len);
126 if (res) {
127 TRACE("%s failed: %d\n", debugstr_w(name), res);
128 *out = 0;
132 static HRESULT get_zone_from_reg(LPCWSTR schema, DWORD *zone)
134 DWORD res, size;
135 HKEY hkey;
137 static const WCHAR wszZoneMapProtocolKey[] =
138 {'S','o','f','t','w','a','r','e','\\',
139 'M','i','c','r','o','s','o','f','t','\\',
140 'W','i','n','d','o','w','s','\\',
141 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
142 'I','n','t','e','r','n','e','t',' ','S','e','t','t','i','n','g','s','\\',
143 'Z','o','n','e','M','a','p','\\',
144 'P','r','o','t','o','c','o','l','D','e','f','a','u','l','t','s',0};
146 res = RegOpenKeyW(HKEY_CURRENT_USER, wszZoneMapProtocolKey, &hkey);
147 if(res != ERROR_SUCCESS) {
148 ERR("Could not open key %s\n", debugstr_w(wszZoneMapProtocolKey));
149 return E_UNEXPECTED;
152 size = sizeof(DWORD);
153 res = RegQueryValueExW(hkey, schema, NULL, NULL, (PBYTE)zone, &size);
154 RegCloseKey(hkey);
155 if(res == ERROR_SUCCESS)
156 return S_OK;
158 res = RegOpenKeyW(HKEY_LOCAL_MACHINE, wszZoneMapProtocolKey, &hkey);
159 if(res != ERROR_SUCCESS) {
160 ERR("Could not open key %s\n", debugstr_w(wszZoneMapProtocolKey));
161 return E_UNEXPECTED;
164 size = sizeof(DWORD);
165 res = RegQueryValueExW(hkey, schema, NULL, NULL, (PBYTE)zone, &size);
166 RegCloseKey(hkey);
167 if(res == ERROR_SUCCESS)
168 return S_OK;
170 *zone = 3;
171 return S_OK;
174 /********************************************************************
175 * matches_domain_pattern [internal]
177 * Checks if the given string matches the specified domain pattern.
179 * This function looks for explicit wildcard domain components iff
180 * they appear at the very beginning of the 'pattern' string
182 * pattern = "*.google.com"
184 static BOOL matches_domain_pattern(LPCWSTR pattern, LPCWSTR str, BOOL implicit_wildcard, LPCWSTR *matched)
186 BOOL matches = FALSE;
187 DWORD pattern_len = strlenW(pattern);
188 DWORD str_len = strlenW(str);
190 TRACE("(%d) Checking if %s matches %s\n", implicit_wildcard, debugstr_w(str), debugstr_w(pattern));
192 *matched = NULL;
193 if(str_len >= pattern_len) {
194 /* Check if there's an explicit wildcard in the pattern. */
195 if(pattern[0] == '*' && pattern[1] == '.') {
196 /* Make sure that 'str' matches the wildcard pattern.
198 * Example:
199 * pattern = "*.google.com"
201 * So in this case 'str' would have to end with ".google.com" in order
202 * to map to this pattern.
204 if(str_len >= pattern_len+1 && !strcmpiW(str+(str_len-pattern_len+1), pattern+1)) {
205 /* Check if there's another '.' inside of the "unmatched" portion
206 * of 'str'.
208 * Example:
209 * pattern = "*.google.com"
210 * str = "test.testing.google.com"
212 * The currently matched portion is ".google.com" in 'str', we need
213 * see if there's a '.' inside of the unmatched portion ("test.testing"), because
214 * if there is and 'implicit_wildcard' isn't set, then this isn't
215 * a match.
217 const WCHAR *ptr;
218 if(str_len > pattern_len+1 && (ptr = memrchrW(str, '.', str_len-pattern_len-2))) {
219 if(implicit_wildcard) {
220 matches = TRUE;
221 *matched = ptr+1;
223 } else {
224 matches = TRUE;
225 *matched = str;
228 } else if(implicit_wildcard && str_len > pattern_len) {
229 /* When the pattern has an implicit wildcard component, it means
230 * that anything goes in 'str' as long as it ends with the pattern
231 * and that the beginning of the match has a '.' before it.
233 * Example:
234 * pattern = "google.com"
235 * str = "www.google.com"
237 * Implicitly matches the pattern, where as:
239 * pattern = "google.com"
240 * str = "wwwgoogle.com"
242 * Doesn't match the pattern.
244 if(str_len > pattern_len) {
245 if(str[str_len-pattern_len-1] == '.' && !strcmpiW(str+(str_len-pattern_len), pattern)) {
246 matches = TRUE;
247 *matched = str+(str_len-pattern_len);
250 } else {
251 /* The pattern doesn't have an implicit wildcard, or an explicit wildcard,
252 * so 'str' has to be an exact match to the 'pattern'.
254 if(!strcmpiW(str, pattern)) {
255 matches = TRUE;
256 *matched = str;
261 if(matches)
262 TRACE("Found a match: matched=%s\n", debugstr_w(*matched));
263 else
264 TRACE("No match found\n");
266 return matches;
269 static BOOL get_zone_for_scheme(HKEY key, LPCWSTR schema, DWORD *zone)
271 static const WCHAR wildcardW[] = {'*',0};
273 DWORD res;
274 DWORD size = sizeof(DWORD);
275 DWORD type;
277 /* See if the key contains a value for the scheme first. */
278 res = RegQueryValueExW(key, schema, NULL, &type, (BYTE*)zone, &size);
279 if(res == ERROR_SUCCESS) {
280 if(type == REG_DWORD)
281 return TRUE;
282 WARN("Unexpected value type %d for value %s, expected REG_DWORD\n", type, debugstr_w(schema));
285 /* Try to get the zone for the wildcard scheme. */
286 size = sizeof(DWORD);
287 res = RegQueryValueExW(key, wildcardW, NULL, &type, (BYTE*)zone, &size);
288 if(res != ERROR_SUCCESS)
289 return FALSE;
291 if(type != REG_DWORD) {
292 WARN("Unexpected value type %d for value %s, expected REG_DWORD\n", type, debugstr_w(wildcardW));
293 return FALSE;
296 return TRUE;
299 /********************************************************************
300 * search_domain_for_zone [internal]
302 * Searches the specified 'domain' registry key to see if 'host' maps into it, or any
303 * of its subdomain registry keys.
305 * Returns S_OK if a match is found, S_FALSE if no matches were found, or an error code.
307 static HRESULT search_domain_for_zone(HKEY domains, LPCWSTR domain, DWORD domain_len, LPCWSTR schema,
308 LPCWSTR host, DWORD host_len, DWORD *zone)
310 BOOL found = FALSE;
311 HKEY domain_key;
312 DWORD res;
313 LPCWSTR matched;
315 if(host_len >= domain_len && matches_domain_pattern(domain, host, TRUE, &matched)) {
316 res = RegOpenKeyW(domains, domain, &domain_key);
317 if(res != ERROR_SUCCESS) {
318 ERR("Failed to open domain key %s: %d\n", debugstr_w(domain), res);
319 return E_UNEXPECTED;
322 if(matched == host)
323 found = get_zone_for_scheme(domain_key, schema, zone);
324 else {
325 INT domain_offset;
326 DWORD subdomain_count, subdomain_len;
327 BOOL check_domain = TRUE;
329 find_domain_name(domain, domain_len, &domain_offset);
331 res = RegQueryInfoKeyW(domain_key, NULL, NULL, NULL, &subdomain_count, &subdomain_len,
332 NULL, NULL, NULL, NULL, NULL, NULL);
333 if(res != ERROR_SUCCESS) {
334 ERR("Unable to query info for key %s: %d\n", debugstr_w(domain), res);
335 RegCloseKey(domain_key);
336 return E_UNEXPECTED;
339 if(subdomain_count) {
340 WCHAR *subdomain;
341 WCHAR *component;
342 DWORD i;
344 subdomain = heap_alloc((subdomain_len+1)*sizeof(WCHAR));
345 if(!subdomain) {
346 RegCloseKey(domain_key);
347 return E_OUTOFMEMORY;
350 component = heap_strndupW(host, matched-host-1);
351 if(!component) {
352 heap_free(subdomain);
353 RegCloseKey(domain_key);
354 return E_OUTOFMEMORY;
357 for(i = 0; i < subdomain_count; ++i) {
358 DWORD len = subdomain_len+1;
359 const WCHAR *sub_matched;
361 res = RegEnumKeyExW(domain_key, i, subdomain, &len, NULL, NULL, NULL, NULL);
362 if(res != ERROR_SUCCESS) {
363 heap_free(component);
364 heap_free(subdomain);
365 RegCloseKey(domain_key);
366 return E_UNEXPECTED;
369 if(matches_domain_pattern(subdomain, component, FALSE, &sub_matched)) {
370 HKEY subdomain_key;
372 res = RegOpenKeyW(domain_key, subdomain, &subdomain_key);
373 if(res != ERROR_SUCCESS) {
374 ERR("Unable to open subdomain key %s of %s: %d\n", debugstr_w(subdomain),
375 debugstr_w(domain), res);
376 heap_free(component);
377 heap_free(subdomain);
378 RegCloseKey(domain_key);
379 return E_UNEXPECTED;
382 found = get_zone_for_scheme(subdomain_key, schema, zone);
383 check_domain = FALSE;
384 RegCloseKey(subdomain_key);
385 break;
388 heap_free(subdomain);
389 heap_free(component);
392 /* There's a chance that 'host' implicitly mapped into 'domain', in
393 * which case we check to see if 'domain' contains zone information.
395 * This can only happen if 'domain' is its own domain name.
396 * Example:
397 * "google.com" (domain name = "google.com")
399 * So if:
400 * host = "www.google.com"
402 * Then host would map directly into the "google.com" domain key.
404 * If 'domain' has more than just its domain name, or it does not
405 * have a domain name, then we don't perform the check. The reason
406 * for this is that these domains don't allow implicit mappings.
407 * Example:
408 * domain = "org" (has no domain name)
409 * host = "www.org"
411 * The mapping would only happen if the "org" key had an explicit subkey
412 * called "www".
414 if(check_domain && !domain_offset && !strchrW(host, matched-host-1))
415 found = get_zone_for_scheme(domain_key, schema, zone);
417 RegCloseKey(domain_key);
420 return found ? S_OK : S_FALSE;
423 static HRESULT search_for_domain_mapping(HKEY domains, LPCWSTR schema, LPCWSTR host, DWORD host_len, DWORD *zone)
425 WCHAR *domain;
426 DWORD domain_count, domain_len, i;
427 DWORD res;
428 HRESULT hres = S_FALSE;
430 res = RegQueryInfoKeyW(domains, NULL, NULL, NULL, &domain_count, &domain_len,
431 NULL, NULL, NULL, NULL, NULL, NULL);
432 if(res != ERROR_SUCCESS) {
433 WARN("Failed to retrieve information about key\n");
434 return E_UNEXPECTED;
437 if(!domain_count)
438 return S_FALSE;
440 domain = heap_alloc((domain_len+1)*sizeof(WCHAR));
441 if(!domain)
442 return E_OUTOFMEMORY;
444 for(i = 0; i < domain_count; ++i) {
445 DWORD len = domain_len+1;
447 res = RegEnumKeyExW(domains, i, domain, &len, NULL, NULL, NULL, NULL);
448 if(res != ERROR_SUCCESS) {
449 heap_free(domain);
450 return E_UNEXPECTED;
453 hres = search_domain_for_zone(domains, domain, len, schema, host, host_len, zone);
454 if(FAILED(hres) || hres == S_OK)
455 break;
458 heap_free(domain);
459 return hres;
462 static HRESULT get_zone_from_domains(IUri *uri, DWORD *zone)
464 HRESULT hres;
465 BSTR host, scheme;
466 DWORD res;
467 HKEY domains;
468 DWORD scheme_type;
470 hres = IUri_GetScheme(uri, &scheme_type);
471 if(FAILED(hres))
472 return hres;
474 /* Windows doesn't play nice with unknown scheme types when it tries
475 * to check if a host name maps into any domains.
477 if(scheme_type == URL_SCHEME_UNKNOWN)
478 return S_FALSE;
480 hres = IUri_GetHost(uri, &host);
481 if(FAILED(hres))
482 return hres;
484 /* Known hierarchical scheme types must have a host. If they don't Windows
485 * assigns URLZONE_INVALID to the zone.
487 if((scheme_type != URL_SCHEME_UNKNOWN && scheme_type != URL_SCHEME_FILE)
488 && is_hierarchical_scheme(scheme_type) && !*host) {
489 *zone = URLZONE_INVALID;
491 SysFreeString(host);
493 /* The MapUrlToZone functions return S_OK when this condition occurs. */
494 return S_OK;
497 hres = IUri_GetSchemeName(uri, &scheme);
498 if(FAILED(hres)) {
499 SysFreeString(host);
500 return hres;
503 /* First try CURRENT_USER. */
504 res = RegOpenKeyW(HKEY_CURRENT_USER, wszZoneMapDomainsKey, &domains);
505 if(res == ERROR_SUCCESS) {
506 hres = search_for_domain_mapping(domains, scheme, host, SysStringLen(host), zone);
507 RegCloseKey(domains);
508 } else
509 WARN("Failed to open HKCU's %s key\n", debugstr_w(wszZoneMapDomainsKey));
511 /* If that doesn't work try LOCAL_MACHINE. */
512 if(hres == S_FALSE) {
513 res = RegOpenKeyW(HKEY_LOCAL_MACHINE, wszZoneMapDomainsKey, &domains);
514 if(res == ERROR_SUCCESS) {
515 hres = search_for_domain_mapping(domains, scheme, host, SysStringLen(host), zone);
516 RegCloseKey(domains);
517 } else
518 WARN("Failed to open HKLM's %s key\n", debugstr_w(wszZoneMapDomainsKey));
521 SysFreeString(host);
522 SysFreeString(scheme);
523 return hres;
526 static HRESULT map_security_uri_to_zone(IUri *uri, DWORD *zone)
528 HRESULT hres;
529 BSTR scheme;
531 *zone = URLZONE_INVALID;
533 hres = IUri_GetSchemeName(uri, &scheme);
534 if(FAILED(hres))
535 return hres;
537 if(!strcmpiW(scheme, fileW)) {
538 BSTR path;
539 WCHAR *ptr, *path_start, root[20];
541 hres = IUri_GetPath(uri, &path);
542 if(FAILED(hres)) {
543 SysFreeString(scheme);
544 return hres;
547 if(*path == '/' && is_drive_path(path+1))
548 path_start = path+1;
549 else
550 path_start = path;
552 if((ptr = strchrW(path_start, ':')) && ptr-path_start+1 < sizeof(root)/sizeof(WCHAR)) {
553 UINT type;
555 memcpy(root, path_start, (ptr-path_start+1)*sizeof(WCHAR));
556 root[ptr-path_start+1] = 0;
558 type = GetDriveTypeW(root);
560 switch(type) {
561 case DRIVE_UNKNOWN:
562 case DRIVE_NO_ROOT_DIR:
563 break;
564 case DRIVE_REMOVABLE:
565 case DRIVE_FIXED:
566 case DRIVE_CDROM:
567 case DRIVE_RAMDISK:
568 *zone = URLZONE_LOCAL_MACHINE;
569 hres = S_OK;
570 break;
571 case DRIVE_REMOTE:
572 *zone = URLZONE_INTERNET;
573 hres = S_OK;
574 break;
575 default:
576 FIXME("unsupported drive type %d\n", type);
579 SysFreeString(path);
582 if(*zone == URLZONE_INVALID) {
583 hres = get_zone_from_domains(uri, zone);
584 if(hres == S_FALSE)
585 hres = get_zone_from_reg(scheme, zone);
588 SysFreeString(scheme);
589 return hres;
592 static HRESULT map_url_to_zone(LPCWSTR url, DWORD *zone, LPWSTR *ret_url)
594 IUri *secur_uri;
595 LPWSTR secur_url;
596 HRESULT hres;
598 *zone = URLZONE_INVALID;
600 hres = CoInternetGetSecurityUrl(url, &secur_url, PSU_SECURITY_URL_ONLY, 0);
601 if(hres != S_OK) {
602 DWORD size = strlenW(url)*sizeof(WCHAR);
604 secur_url = CoTaskMemAlloc(size);
605 if(!secur_url)
606 return E_OUTOFMEMORY;
608 memcpy(secur_url, url, size);
611 hres = CreateUri(secur_url, Uri_CREATE_ALLOW_IMPLICIT_FILE_SCHEME, 0, &secur_uri);
612 if(FAILED(hres)) {
613 CoTaskMemFree(secur_url);
614 return hres;
617 hres = map_security_uri_to_zone(secur_uri, zone);
618 IUri_Release(secur_uri);
620 if(FAILED(hres) || !ret_url)
621 CoTaskMemFree(secur_url);
622 else
623 *ret_url = secur_url;
625 return hres;
628 static HRESULT map_uri_to_zone(IUri *uri, DWORD *zone, IUri **ret_uri)
630 HRESULT hres;
631 IUri *secur_uri;
633 hres = CoInternetGetSecurityUrlEx(uri, &secur_uri, PSU_SECURITY_URL_ONLY, 0);
634 if(FAILED(hres))
635 return hres;
637 hres = map_security_uri_to_zone(secur_uri, zone);
638 if(FAILED(hres) || !ret_uri)
639 IUri_Release(secur_uri);
640 else
641 *ret_uri = secur_uri;
643 return hres;
646 static HRESULT open_zone_key(HKEY parent_key, DWORD zone, HKEY *hkey)
648 static const WCHAR wszFormat[] = {'%','s','%','u',0};
650 WCHAR key_name[sizeof(wszZonesKey)/sizeof(WCHAR)+12];
651 DWORD res;
653 wsprintfW(key_name, wszFormat, wszZonesKey, zone);
655 res = RegOpenKeyW(parent_key, key_name, hkey);
657 if(res != ERROR_SUCCESS) {
658 WARN("RegOpenKey failed\n");
659 return E_INVALIDARG;
662 return S_OK;
665 static HRESULT get_action_policy(DWORD zone, DWORD action, BYTE *policy, DWORD size, URLZONEREG zone_reg)
667 HKEY parent_key;
668 HKEY hkey;
669 LONG res;
670 HRESULT hres;
672 switch(action) {
673 case URLACTION_SCRIPT_OVERRIDE_SAFETY:
674 case URLACTION_ACTIVEX_OVERRIDE_SCRIPT_SAFETY:
675 *(DWORD*)policy = URLPOLICY_DISALLOW;
676 return S_OK;
679 switch(zone_reg) {
680 case URLZONEREG_DEFAULT:
681 case URLZONEREG_HKCU:
682 parent_key = HKEY_CURRENT_USER;
683 break;
684 case URLZONEREG_HKLM:
685 parent_key = HKEY_LOCAL_MACHINE;
686 break;
687 default:
688 WARN("Unknown URLZONEREG: %d\n", zone_reg);
689 return E_FAIL;
692 hres = open_zone_key(parent_key, zone, &hkey);
693 if(SUCCEEDED(hres)) {
694 WCHAR action_str[16];
695 DWORD len = size;
697 static const WCHAR formatW[] = {'%','X',0};
699 wsprintfW(action_str, formatW, action);
701 res = RegQueryValueExW(hkey, action_str, NULL, NULL, policy, &len);
702 if(res == ERROR_MORE_DATA) {
703 hres = E_INVALIDARG;
704 }else if(res == ERROR_FILE_NOT_FOUND) {
705 hres = E_FAIL;
706 }else if(res != ERROR_SUCCESS) {
707 ERR("RegQueryValue failed: %d\n", res);
708 hres = E_UNEXPECTED;
711 RegCloseKey(hkey);
714 if(FAILED(hres) && zone_reg == URLZONEREG_DEFAULT)
715 return get_action_policy(zone, action, policy, size, URLZONEREG_HKLM);
717 return hres;
720 static HRESULT generate_security_id(IUri *uri, BYTE *secid, DWORD *secid_len, DWORD zone)
722 DWORD len;
723 HRESULT hres;
724 DWORD scheme_type;
726 if(zone == URLZONE_INVALID)
727 return E_INVALIDARG;
729 hres = IUri_GetScheme(uri, &scheme_type);
730 if(FAILED(hres))
731 return hres;
733 /* Windows handles opaque URLs differently then hierarchical ones. */
734 if(!is_hierarchical_scheme(scheme_type) && scheme_type != URL_SCHEME_WILDCARD) {
735 BSTR display_uri;
737 hres = IUri_GetDisplayUri(uri, &display_uri);
738 if(FAILED(hres))
739 return hres;
741 len = WideCharToMultiByte(CP_ACP, 0, display_uri, -1, NULL, 0, NULL, NULL)-1;
743 if(len+sizeof(DWORD) > *secid_len) {
744 SysFreeString(display_uri);
745 return HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
748 WideCharToMultiByte(CP_ACP, 0, display_uri, -1, (LPSTR)secid, len, NULL, NULL);
749 SysFreeString(display_uri);
751 *(DWORD*)(secid+len) = zone;
752 } else {
753 BSTR host, scheme;
754 DWORD host_len, scheme_len;
755 BYTE *ptr;
757 hres = IUri_GetHost(uri, &host);
758 if(FAILED(hres))
759 return hres;
761 /* The host can't be empty for Wildcard URIs. */
762 if(scheme_type == URL_SCHEME_WILDCARD && !*host) {
763 SysFreeString(host);
764 return E_INVALIDARG;
767 hres = IUri_GetSchemeName(uri, &scheme);
768 if(FAILED(hres)) {
769 SysFreeString(host);
770 return hres;
773 host_len = WideCharToMultiByte(CP_ACP, 0, host, -1, NULL, 0, NULL, NULL)-1;
774 scheme_len = WideCharToMultiByte(CP_ACP, 0, scheme, -1, NULL, 0, NULL, NULL)-1;
776 len = host_len+scheme_len+sizeof(BYTE);
778 if(len+sizeof(DWORD) > *secid_len) {
779 SysFreeString(host);
780 SysFreeString(scheme);
781 return HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
784 WideCharToMultiByte(CP_ACP, 0, scheme, -1, (LPSTR)secid, len, NULL, NULL);
785 SysFreeString(scheme);
787 ptr = secid+scheme_len;
788 *ptr++ = ':';
790 WideCharToMultiByte(CP_ACP, 0, host, -1, (LPSTR)ptr, host_len, NULL, NULL);
791 SysFreeString(host);
793 ptr += host_len;
795 *(DWORD*)ptr = zone;
798 *secid_len = len+sizeof(DWORD);
800 return S_OK;
803 static HRESULT get_security_id_for_url(LPCWSTR url, BYTE *secid, DWORD *secid_len)
805 HRESULT hres;
806 DWORD zone = URLZONE_INVALID;
807 LPWSTR secur_url = NULL;
808 IUri *uri;
810 hres = map_url_to_zone(url, &zone, &secur_url);
811 if(FAILED(hres))
812 return hres == 0x80041001 ? E_INVALIDARG : hres;
814 hres = CreateUri(secur_url, Uri_CREATE_ALLOW_IMPLICIT_FILE_SCHEME, 0, &uri);
815 CoTaskMemFree(secur_url);
816 if(FAILED(hres))
817 return hres;
819 hres = generate_security_id(uri, secid, secid_len, zone);
820 IUri_Release(uri);
822 return hres;
825 static HRESULT get_security_id_for_uri(IUri *uri, BYTE *secid, DWORD *secid_len)
827 HRESULT hres;
828 IUri *secur_uri;
829 DWORD zone = URLZONE_INVALID;
831 hres = map_uri_to_zone(uri, &zone, &secur_uri);
832 if(FAILED(hres))
833 return hres;
835 hres = generate_security_id(secur_uri, secid, secid_len, zone);
836 IUri_Release(secur_uri);
838 return hres;
841 /***********************************************************************
842 * InternetSecurityManager implementation
845 typedef struct {
846 IInternetSecurityManagerEx2 IInternetSecurityManagerEx2_iface;
848 LONG ref;
850 IInternetSecurityMgrSite *mgrsite;
851 IInternetSecurityManager *custom_manager;
852 } SecManagerImpl;
854 static inline SecManagerImpl *impl_from_IInternetSecurityManagerEx2(IInternetSecurityManagerEx2 *iface)
856 return CONTAINING_RECORD(iface, SecManagerImpl, IInternetSecurityManagerEx2_iface);
859 static HRESULT WINAPI SecManagerImpl_QueryInterface(IInternetSecurityManagerEx2* iface,REFIID riid,void** ppvObject)
861 SecManagerImpl *This = impl_from_IInternetSecurityManagerEx2(iface);
863 TRACE("(%p)->(%s %p)\n",This,debugstr_guid(riid),ppvObject);
865 if(!ppvObject)
866 return E_INVALIDARG;
868 if(IsEqualIID(&IID_IUnknown, riid) ||
869 IsEqualIID(&IID_IInternetSecurityManager, riid) ||
870 IsEqualIID(&IID_IInternetSecurityManagerEx, riid) ||
871 IsEqualIID(&IID_IInternetSecurityManagerEx2, riid)) {
872 *ppvObject = iface;
873 } else {
874 WARN("not supported interface %s\n", debugstr_guid(riid));
875 *ppvObject = NULL;
876 return E_NOINTERFACE;
879 IInternetSecurityManagerEx2_AddRef(iface);
880 return S_OK;
883 static ULONG WINAPI SecManagerImpl_AddRef(IInternetSecurityManagerEx2* iface)
885 SecManagerImpl *This = impl_from_IInternetSecurityManagerEx2(iface);
886 ULONG refCount = InterlockedIncrement(&This->ref);
888 TRACE("(%p) ref=%u\n", This, refCount);
890 return refCount;
893 static ULONG WINAPI SecManagerImpl_Release(IInternetSecurityManagerEx2* iface)
895 SecManagerImpl *This = impl_from_IInternetSecurityManagerEx2(iface);
896 ULONG refCount = InterlockedDecrement(&This->ref);
898 TRACE("(%p) ref=%u\n", This, refCount);
900 /* destroy the object if there's no more reference on it */
901 if (!refCount){
902 if(This->mgrsite)
903 IInternetSecurityMgrSite_Release(This->mgrsite);
904 if(This->custom_manager)
905 IInternetSecurityManager_Release(This->custom_manager);
907 heap_free(This);
909 URLMON_UnlockModule();
912 return refCount;
915 static HRESULT WINAPI SecManagerImpl_SetSecuritySite(IInternetSecurityManagerEx2 *iface,
916 IInternetSecurityMgrSite *pSite)
918 SecManagerImpl *This = impl_from_IInternetSecurityManagerEx2(iface);
920 TRACE("(%p)->(%p)\n", This, pSite);
922 if(This->mgrsite)
923 IInternetSecurityMgrSite_Release(This->mgrsite);
925 if(This->custom_manager) {
926 IInternetSecurityManager_Release(This->custom_manager);
927 This->custom_manager = NULL;
930 This->mgrsite = pSite;
932 if(pSite) {
933 IServiceProvider *servprov;
934 HRESULT hres;
936 IInternetSecurityMgrSite_AddRef(pSite);
938 hres = IInternetSecurityMgrSite_QueryInterface(pSite, &IID_IServiceProvider,
939 (void**)&servprov);
940 if(SUCCEEDED(hres)) {
941 IServiceProvider_QueryService(servprov, &SID_SInternetSecurityManager,
942 &IID_IInternetSecurityManager, (void**)&This->custom_manager);
943 IServiceProvider_Release(servprov);
947 return S_OK;
950 static HRESULT WINAPI SecManagerImpl_GetSecuritySite(IInternetSecurityManagerEx2 *iface,
951 IInternetSecurityMgrSite **ppSite)
953 SecManagerImpl *This = impl_from_IInternetSecurityManagerEx2(iface);
955 TRACE("(%p)->(%p)\n", This, ppSite);
957 if(!ppSite)
958 return E_INVALIDARG;
960 if(This->mgrsite)
961 IInternetSecurityMgrSite_AddRef(This->mgrsite);
963 *ppSite = This->mgrsite;
964 return S_OK;
967 static HRESULT WINAPI SecManagerImpl_MapUrlToZone(IInternetSecurityManagerEx2 *iface,
968 LPCWSTR pwszUrl, DWORD *pdwZone,
969 DWORD dwFlags)
971 SecManagerImpl *This = impl_from_IInternetSecurityManagerEx2(iface);
972 HRESULT hres;
974 TRACE("(%p)->(%s %p %08x)\n", iface, debugstr_w(pwszUrl), pdwZone, dwFlags);
976 if(This->custom_manager) {
977 hres = IInternetSecurityManager_MapUrlToZone(This->custom_manager,
978 pwszUrl, pdwZone, dwFlags);
979 if(hres != INET_E_DEFAULT_ACTION)
980 return hres;
983 if(!pwszUrl) {
984 *pdwZone = URLZONE_INVALID;
985 return E_INVALIDARG;
988 if(dwFlags)
989 FIXME("not supported flags: %08x\n", dwFlags);
991 return map_url_to_zone(pwszUrl, pdwZone, NULL);
994 static HRESULT WINAPI SecManagerImpl_GetSecurityId(IInternetSecurityManagerEx2 *iface,
995 LPCWSTR pwszUrl, BYTE *pbSecurityId, DWORD *pcbSecurityId, DWORD_PTR dwReserved)
997 SecManagerImpl *This = impl_from_IInternetSecurityManagerEx2(iface);
999 TRACE("(%p)->(%s %p %p %08lx)\n", iface, debugstr_w(pwszUrl), pbSecurityId,
1000 pcbSecurityId, dwReserved);
1002 if(This->custom_manager) {
1003 HRESULT hres;
1005 hres = IInternetSecurityManager_GetSecurityId(This->custom_manager,
1006 pwszUrl, pbSecurityId, pcbSecurityId, dwReserved);
1007 if(hres != INET_E_DEFAULT_ACTION)
1008 return hres;
1011 if(!pwszUrl || !pbSecurityId || !pcbSecurityId)
1012 return E_INVALIDARG;
1014 if(dwReserved)
1015 FIXME("dwReserved is not supported\n");
1017 return get_security_id_for_url(pwszUrl, pbSecurityId, pcbSecurityId);
1021 static HRESULT WINAPI SecManagerImpl_ProcessUrlAction(IInternetSecurityManagerEx2 *iface,
1022 LPCWSTR pwszUrl, DWORD dwAction,
1023 BYTE *pPolicy, DWORD cbPolicy,
1024 BYTE *pContext, DWORD cbContext,
1025 DWORD dwFlags, DWORD dwReserved)
1027 SecManagerImpl *This = impl_from_IInternetSecurityManagerEx2(iface);
1028 DWORD zone, policy;
1029 HRESULT hres;
1031 TRACE("(%p)->(%s %08x %p %08x %p %08x %08x %08x)\n", iface, debugstr_w(pwszUrl), dwAction,
1032 pPolicy, cbPolicy, pContext, cbContext, dwFlags, dwReserved);
1034 if(This->custom_manager) {
1035 hres = IInternetSecurityManager_ProcessUrlAction(This->custom_manager, pwszUrl, dwAction,
1036 pPolicy, cbPolicy, pContext, cbContext, dwFlags, dwReserved);
1037 if(hres != INET_E_DEFAULT_ACTION)
1038 return hres;
1041 if(dwFlags || dwReserved)
1042 FIXME("Unsupported arguments\n");
1044 if(!pwszUrl)
1045 return E_INVALIDARG;
1047 hres = map_url_to_zone(pwszUrl, &zone, NULL);
1048 if(FAILED(hres))
1049 return hres;
1051 hres = get_action_policy(zone, dwAction, (BYTE*)&policy, sizeof(policy), URLZONEREG_DEFAULT);
1052 if(FAILED(hres))
1053 return hres;
1055 TRACE("policy %x\n", policy);
1056 if(cbPolicy >= sizeof(DWORD))
1057 *(DWORD*)pPolicy = policy;
1059 switch(GetUrlPolicyPermissions(policy)) {
1060 case URLPOLICY_ALLOW:
1061 case URLPOLICY_CHANNEL_SOFTDIST_PRECACHE:
1062 return S_OK;
1063 case URLPOLICY_DISALLOW:
1064 return S_FALSE;
1065 case URLPOLICY_QUERY:
1066 FIXME("URLPOLICY_QUERY not implemented\n");
1067 return E_FAIL;
1068 default:
1069 FIXME("Not implemented policy %x\n", policy);
1072 return E_FAIL;
1076 static HRESULT WINAPI SecManagerImpl_QueryCustomPolicy(IInternetSecurityManagerEx2 *iface,
1077 LPCWSTR pwszUrl, REFGUID guidKey,
1078 BYTE **ppPolicy, DWORD *pcbPolicy,
1079 BYTE *pContext, DWORD cbContext,
1080 DWORD dwReserved)
1082 SecManagerImpl *This = impl_from_IInternetSecurityManagerEx2(iface);
1083 HRESULT hres;
1085 TRACE("(%p)->(%s %s %p %p %p %08x %08x )\n", iface, debugstr_w(pwszUrl), debugstr_guid(guidKey),
1086 ppPolicy, pcbPolicy, pContext, cbContext, dwReserved);
1088 if(This->custom_manager) {
1089 hres = IInternetSecurityManager_QueryCustomPolicy(This->custom_manager, pwszUrl, guidKey,
1090 ppPolicy, pcbPolicy, pContext, cbContext, dwReserved);
1091 if(hres != INET_E_DEFAULT_ACTION)
1092 return hres;
1095 WARN("Unknown guidKey %s\n", debugstr_guid(guidKey));
1096 return HRESULT_FROM_WIN32(ERROR_NOT_FOUND);
1099 static HRESULT WINAPI SecManagerImpl_SetZoneMapping(IInternetSecurityManagerEx2 *iface,
1100 DWORD dwZone, LPCWSTR pwszPattern, DWORD dwFlags)
1102 SecManagerImpl *This = impl_from_IInternetSecurityManagerEx2(iface);
1103 HRESULT hres;
1105 TRACE("(%p)->(%08x %s %08x)\n", iface, dwZone, debugstr_w(pwszPattern),dwFlags);
1107 if(This->custom_manager) {
1108 hres = IInternetSecurityManager_SetZoneMapping(This->custom_manager, dwZone,
1109 pwszPattern, dwFlags);
1110 if(hres != INET_E_DEFAULT_ACTION)
1111 return hres;
1114 FIXME("Default action is not implemented\n");
1115 return E_NOTIMPL;
1118 static HRESULT WINAPI SecManagerImpl_GetZoneMappings(IInternetSecurityManagerEx2 *iface,
1119 DWORD dwZone, IEnumString **ppenumString, DWORD dwFlags)
1121 SecManagerImpl *This = impl_from_IInternetSecurityManagerEx2(iface);
1122 HRESULT hres;
1124 TRACE("(%p)->(%08x %p %08x)\n", iface, dwZone, ppenumString,dwFlags);
1126 if(This->custom_manager) {
1127 hres = IInternetSecurityManager_GetZoneMappings(This->custom_manager, dwZone,
1128 ppenumString, dwFlags);
1129 if(hres != INET_E_DEFAULT_ACTION)
1130 return hres;
1133 FIXME("Default action is not implemented\n");
1134 return E_NOTIMPL;
1137 static HRESULT WINAPI SecManagerImpl_ProcessUrlActionEx(IInternetSecurityManagerEx2 *iface,
1138 LPCWSTR pwszUrl, DWORD dwAction, BYTE *pPolicy, DWORD cbPolicy, BYTE *pContext, DWORD cbContext,
1139 DWORD dwFlags, DWORD dwReserved, DWORD *pdwOutFlags)
1141 SecManagerImpl *This = impl_from_IInternetSecurityManagerEx2(iface);
1142 FIXME("(%p)->(%s %08x %p %d %p %d %08x %08x %p) stub\n", This, debugstr_w(pwszUrl), dwAction, pPolicy, cbPolicy,
1143 pContext, cbContext, dwFlags, dwReserved, pdwOutFlags);
1144 return E_NOTIMPL;
1147 static HRESULT WINAPI SecManagerImpl_MapUrlToZoneEx2(IInternetSecurityManagerEx2 *iface,
1148 IUri *pUri, DWORD *pdwZone, DWORD dwFlags, LPWSTR *ppwszMappedUrl, DWORD *pdwOutFlags)
1150 SecManagerImpl *This = impl_from_IInternetSecurityManagerEx2(iface);
1152 TRACE("(%p)->(%p %p %08x %p %p)\n", This, pUri, pdwZone, dwFlags, ppwszMappedUrl, pdwOutFlags);
1154 if(This->custom_manager) {
1155 HRESULT hres;
1156 IInternetSecurityManagerEx2 *sec_mgr2;
1158 hres = IInternetSecurityManager_QueryInterface(This->custom_manager, &IID_IInternetSecurityManagerEx2,
1159 (void**)&sec_mgr2);
1160 if(SUCCEEDED(hres)) {
1161 hres = IInternetSecurityManagerEx2_MapUrlToZoneEx2(sec_mgr2, pUri, pdwZone, dwFlags, ppwszMappedUrl, pdwOutFlags);
1162 IInternetSecurityManagerEx2_Release(sec_mgr2);
1163 } else {
1164 BSTR url;
1166 hres = IUri_GetDisplayUri(pUri, &url);
1167 if(FAILED(hres))
1168 return hres;
1170 hres = IInternetSecurityManager_MapUrlToZone(This->custom_manager, url, pdwZone, dwFlags);
1171 SysFreeString(url);
1174 if(hres != INET_E_DEFAULT_ACTION)
1175 return hres;
1178 if(!pdwZone)
1179 return E_INVALIDARG;
1181 if(!pUri) {
1182 *pdwZone = URLZONE_INVALID;
1183 return E_INVALIDARG;
1186 if(dwFlags)
1187 FIXME("Unsupported flags: %08x\n", dwFlags);
1189 return map_uri_to_zone(pUri, pdwZone, NULL);
1192 static HRESULT WINAPI SecManagerImpl_ProcessUrlActionEx2(IInternetSecurityManagerEx2 *iface,
1193 IUri *pUri, DWORD dwAction, BYTE *pPolicy, DWORD cbPolicy, BYTE *pContext, DWORD cbContext,
1194 DWORD dwFlags, DWORD_PTR dwReserved, DWORD *pdwOutFlags)
1196 SecManagerImpl *This = impl_from_IInternetSecurityManagerEx2(iface);
1197 FIXME("(%p)->(%p %08x %p %d %p %d %08x %08x %p) stub\n", This, pUri, dwAction, pPolicy,
1198 cbPolicy, pContext, cbContext, dwFlags, (DWORD)dwReserved, pdwOutFlags);
1199 return E_NOTIMPL;
1202 static HRESULT WINAPI SecManagerImpl_GetSecurityIdEx2(IInternetSecurityManagerEx2 *iface,
1203 IUri *pUri, BYTE *pbSecurityId, DWORD *pcbSecurityId, DWORD_PTR dwReserved)
1205 SecManagerImpl *This = impl_from_IInternetSecurityManagerEx2(iface);
1206 TRACE("(%p)->(%p %p %p %08x) stub\n", This, pUri, pbSecurityId, pcbSecurityId, (DWORD)dwReserved);
1208 if(dwReserved)
1209 FIXME("dwReserved is not supported yet\n");
1211 if(!pUri || !pcbSecurityId || !pbSecurityId)
1212 return E_INVALIDARG;
1214 return get_security_id_for_uri(pUri, pbSecurityId, pcbSecurityId);
1217 static HRESULT WINAPI SecManagerImpl_QueryCustomPolicyEx2(IInternetSecurityManagerEx2 *iface,
1218 IUri *pUri, REFGUID guidKey, BYTE **ppPolicy, DWORD *pcbPolicy, BYTE *pContext,
1219 DWORD cbContext, DWORD_PTR dwReserved)
1221 SecManagerImpl *This = impl_from_IInternetSecurityManagerEx2(iface);
1222 FIXME("(%p)->(%p %s %p %p %p %d %08x) stub\n", This, pUri, debugstr_guid(guidKey), ppPolicy, pcbPolicy,
1223 pContext, cbContext, (DWORD)dwReserved);
1224 return E_NOTIMPL;
1227 static const IInternetSecurityManagerEx2Vtbl VT_SecManagerImpl =
1229 SecManagerImpl_QueryInterface,
1230 SecManagerImpl_AddRef,
1231 SecManagerImpl_Release,
1232 SecManagerImpl_SetSecuritySite,
1233 SecManagerImpl_GetSecuritySite,
1234 SecManagerImpl_MapUrlToZone,
1235 SecManagerImpl_GetSecurityId,
1236 SecManagerImpl_ProcessUrlAction,
1237 SecManagerImpl_QueryCustomPolicy,
1238 SecManagerImpl_SetZoneMapping,
1239 SecManagerImpl_GetZoneMappings,
1240 SecManagerImpl_ProcessUrlActionEx,
1241 SecManagerImpl_MapUrlToZoneEx2,
1242 SecManagerImpl_ProcessUrlActionEx2,
1243 SecManagerImpl_GetSecurityIdEx2,
1244 SecManagerImpl_QueryCustomPolicyEx2
1247 HRESULT SecManagerImpl_Construct(IUnknown *pUnkOuter, LPVOID *ppobj)
1249 SecManagerImpl *This;
1251 TRACE("(%p,%p)\n",pUnkOuter,ppobj);
1252 This = heap_alloc(sizeof(*This));
1254 /* Initialize the virtual function table. */
1255 This->IInternetSecurityManagerEx2_iface.lpVtbl = &VT_SecManagerImpl;
1257 This->ref = 1;
1258 This->mgrsite = NULL;
1259 This->custom_manager = NULL;
1261 *ppobj = This;
1263 URLMON_LockModule();
1265 return S_OK;
1268 /***********************************************************************
1269 * InternetZoneManager implementation
1272 typedef struct {
1273 IInternetZoneManagerEx2 IInternetZoneManagerEx2_iface;
1274 LONG ref;
1275 LPDWORD *zonemaps;
1276 DWORD zonemap_count;
1277 } ZoneMgrImpl;
1279 static inline ZoneMgrImpl *impl_from_IInternetZoneManagerEx2(IInternetZoneManagerEx2 *iface)
1281 return CONTAINING_RECORD(iface, ZoneMgrImpl, IInternetZoneManagerEx2_iface);
1285 /***********************************************************************
1286 * build_zonemap_from_reg [internal]
1288 * Enumerate the Zones in the Registry and return the Zones in a DWORD-array
1289 * The number of the Zones is returned in data[0]
1291 static LPDWORD build_zonemap_from_reg(void)
1293 WCHAR name[32];
1294 HKEY hkey;
1295 LPDWORD data = NULL;
1296 DWORD allocated = 6; /* space for the zonecount and Zone "0" up to Zone "4" */
1297 DWORD used = 0;
1298 DWORD res;
1299 DWORD len;
1302 res = RegOpenKeyW(HKEY_CURRENT_USER, wszZonesKey, &hkey);
1303 if (res)
1304 return NULL;
1306 data = heap_alloc(allocated * sizeof(DWORD));
1307 if (!data)
1308 goto cleanup;
1310 while (!res) {
1311 name[0] = '\0';
1312 len = sizeof(name) / sizeof(name[0]);
1313 res = RegEnumKeyExW(hkey, used, name, &len, NULL, NULL, NULL, NULL);
1315 if (!res) {
1316 used++;
1317 if (used == allocated) {
1318 LPDWORD new_data;
1320 allocated *= 2;
1321 new_data = heap_realloc_zero(data, allocated * sizeof(DWORD));
1322 if (!new_data)
1323 goto cleanup;
1325 data = new_data;
1327 data[used] = atoiW(name);
1330 if (used) {
1331 RegCloseKey(hkey);
1332 data[0] = used;
1333 return data;
1336 cleanup:
1337 /* something failed */
1338 RegCloseKey(hkey);
1339 heap_free(data);
1340 return NULL;
1343 /********************************************************************
1344 * IInternetZoneManager_QueryInterface
1346 static HRESULT WINAPI ZoneMgrImpl_QueryInterface(IInternetZoneManagerEx2* iface, REFIID riid, void** ppvObject)
1348 ZoneMgrImpl* This = impl_from_IInternetZoneManagerEx2(iface);
1350 TRACE("(%p)->(%s,%p)\n", This, debugstr_guid(riid), ppvObject);
1352 if(!This || !ppvObject)
1353 return E_INVALIDARG;
1355 if(IsEqualIID(&IID_IUnknown, riid)) {
1356 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppvObject);
1357 }else if(IsEqualIID(&IID_IInternetZoneManager, riid)) {
1358 TRACE("(%p)->(IID_InternetZoneManager %p)\n", This, ppvObject);
1359 }else if(IsEqualIID(&IID_IInternetZoneManagerEx, riid)) {
1360 TRACE("(%p)->(IID_InternetZoneManagerEx %p)\n", This, ppvObject);
1361 }else if(IsEqualIID(&IID_IInternetZoneManagerEx2, riid)) {
1362 TRACE("(%p)->(IID_InternetZoneManagerEx2 %p)\n", This, ppvObject);
1364 else
1366 FIXME("Unknown interface: %s\n", debugstr_guid(riid));
1367 *ppvObject = NULL;
1368 return E_NOINTERFACE;
1371 *ppvObject = iface;
1372 IInternetZoneManagerEx2_AddRef(iface);
1373 return S_OK;
1376 /********************************************************************
1377 * IInternetZoneManager_AddRef
1379 static ULONG WINAPI ZoneMgrImpl_AddRef(IInternetZoneManagerEx2* iface)
1381 ZoneMgrImpl* This = impl_from_IInternetZoneManagerEx2(iface);
1382 ULONG refCount = InterlockedIncrement(&This->ref);
1384 TRACE("(%p)->(ref before=%u)\n",This, refCount - 1);
1386 return refCount;
1389 /********************************************************************
1390 * IInternetZoneManager_Release
1392 static ULONG WINAPI ZoneMgrImpl_Release(IInternetZoneManagerEx2* iface)
1394 ZoneMgrImpl* This = impl_from_IInternetZoneManagerEx2(iface);
1395 ULONG refCount = InterlockedDecrement(&This->ref);
1397 TRACE("(%p)->(ref before=%u)\n",This, refCount + 1);
1399 if(!refCount) {
1400 while (This->zonemap_count) heap_free(This->zonemaps[--This->zonemap_count]);
1401 heap_free(This->zonemaps);
1402 heap_free(This);
1403 URLMON_UnlockModule();
1406 return refCount;
1409 /********************************************************************
1410 * IInternetZoneManager_GetZoneAttributes
1412 static HRESULT WINAPI ZoneMgrImpl_GetZoneAttributes(IInternetZoneManagerEx2* iface,
1413 DWORD dwZone,
1414 ZONEATTRIBUTES* pZoneAttributes)
1416 ZoneMgrImpl* This = impl_from_IInternetZoneManagerEx2(iface);
1417 HRESULT hr;
1418 HKEY hcu;
1419 HKEY hklm = NULL;
1421 TRACE("(%p)->(%d %p)\n", This, dwZone, pZoneAttributes);
1423 if (!pZoneAttributes)
1424 return E_INVALIDARG;
1426 hr = open_zone_key(HKEY_CURRENT_USER, dwZone, &hcu);
1427 if (FAILED(hr))
1428 return S_OK; /* IE6 and older returned E_FAIL here */
1430 hr = open_zone_key(HKEY_LOCAL_MACHINE, dwZone, &hklm);
1431 if (FAILED(hr))
1432 TRACE("Zone %d not in HKLM\n", dwZone);
1434 get_string_from_reg(hcu, hklm, displaynameW, pZoneAttributes->szDisplayName, MAX_ZONE_PATH);
1435 get_string_from_reg(hcu, hklm, descriptionW, pZoneAttributes->szDescription, MAX_ZONE_DESCRIPTION);
1436 get_string_from_reg(hcu, hklm, iconW, pZoneAttributes->szIconPath, MAX_ZONE_PATH);
1437 get_dword_from_reg(hcu, hklm, minlevelW, &pZoneAttributes->dwTemplateMinLevel);
1438 get_dword_from_reg(hcu, hklm, currentlevelW, &pZoneAttributes->dwTemplateCurrentLevel);
1439 get_dword_from_reg(hcu, hklm, recommendedlevelW, &pZoneAttributes->dwTemplateRecommended);
1440 get_dword_from_reg(hcu, hklm, flagsW, &pZoneAttributes->dwFlags);
1442 RegCloseKey(hklm);
1443 RegCloseKey(hcu);
1444 return S_OK;
1447 /********************************************************************
1448 * IInternetZoneManager_SetZoneAttributes
1450 static HRESULT WINAPI ZoneMgrImpl_SetZoneAttributes(IInternetZoneManagerEx2* iface,
1451 DWORD dwZone,
1452 ZONEATTRIBUTES* pZoneAttributes)
1454 ZoneMgrImpl* This = impl_from_IInternetZoneManagerEx2(iface);
1455 HRESULT hr;
1456 HKEY hcu;
1458 TRACE("(%p)->(%d %p)\n", This, dwZone, pZoneAttributes);
1460 if (!pZoneAttributes)
1461 return E_INVALIDARG;
1463 hr = open_zone_key(HKEY_CURRENT_USER, dwZone, &hcu);
1464 if (FAILED(hr))
1465 return S_OK; /* IE6 returned E_FAIL here */
1467 /* cbSize is ignored */
1468 RegSetValueExW(hcu, displaynameW, 0, REG_SZ, (LPBYTE) pZoneAttributes->szDisplayName,
1469 (lstrlenW(pZoneAttributes->szDisplayName)+1)* sizeof(WCHAR));
1471 RegSetValueExW(hcu, descriptionW, 0, REG_SZ, (LPBYTE) pZoneAttributes->szDescription,
1472 (lstrlenW(pZoneAttributes->szDescription)+1)* sizeof(WCHAR));
1474 RegSetValueExW(hcu, iconW, 0, REG_SZ, (LPBYTE) pZoneAttributes->szIconPath,
1475 (lstrlenW(pZoneAttributes->szIconPath)+1)* sizeof(WCHAR));
1477 RegSetValueExW(hcu, minlevelW, 0, REG_DWORD,
1478 (const BYTE*) &pZoneAttributes->dwTemplateMinLevel, sizeof(DWORD));
1480 RegSetValueExW(hcu, currentlevelW, 0, REG_DWORD,
1481 (const BYTE*) &pZoneAttributes->dwTemplateCurrentLevel, sizeof(DWORD));
1483 RegSetValueExW(hcu, recommendedlevelW, 0, REG_DWORD,
1484 (const BYTE*) &pZoneAttributes->dwTemplateRecommended, sizeof(DWORD));
1486 RegSetValueExW(hcu, flagsW, 0, REG_DWORD, (const BYTE*) &pZoneAttributes->dwFlags, sizeof(DWORD));
1487 RegCloseKey(hcu);
1488 return S_OK;
1492 /********************************************************************
1493 * IInternetZoneManager_GetZoneCustomPolicy
1495 static HRESULT WINAPI ZoneMgrImpl_GetZoneCustomPolicy(IInternetZoneManagerEx2* iface,
1496 DWORD dwZone,
1497 REFGUID guidKey,
1498 BYTE** ppPolicy,
1499 DWORD* pcbPolicy,
1500 URLZONEREG ulrZoneReg)
1502 FIXME("(%p)->(%08x %s %p %p %08x) stub\n", iface, dwZone, debugstr_guid(guidKey),
1503 ppPolicy, pcbPolicy, ulrZoneReg);
1504 return E_NOTIMPL;
1507 /********************************************************************
1508 * IInternetZoneManager_SetZoneCustomPolicy
1510 static HRESULT WINAPI ZoneMgrImpl_SetZoneCustomPolicy(IInternetZoneManagerEx2* iface,
1511 DWORD dwZone,
1512 REFGUID guidKey,
1513 BYTE* ppPolicy,
1514 DWORD cbPolicy,
1515 URLZONEREG ulrZoneReg)
1517 FIXME("(%p)->(%08x %s %p %08x %08x) stub\n", iface, dwZone, debugstr_guid(guidKey),
1518 ppPolicy, cbPolicy, ulrZoneReg);
1519 return E_NOTIMPL;
1522 /********************************************************************
1523 * IInternetZoneManager_GetZoneActionPolicy
1525 static HRESULT WINAPI ZoneMgrImpl_GetZoneActionPolicy(IInternetZoneManagerEx2* iface,
1526 DWORD dwZone, DWORD dwAction, BYTE* pPolicy, DWORD cbPolicy, URLZONEREG urlZoneReg)
1528 TRACE("(%p)->(%d %08x %p %d %d)\n", iface, dwZone, dwAction, pPolicy,
1529 cbPolicy, urlZoneReg);
1531 if(!pPolicy)
1532 return E_INVALIDARG;
1534 return get_action_policy(dwZone, dwAction, pPolicy, cbPolicy, urlZoneReg);
1537 /********************************************************************
1538 * IInternetZoneManager_SetZoneActionPolicy
1540 static HRESULT WINAPI ZoneMgrImpl_SetZoneActionPolicy(IInternetZoneManagerEx2* iface,
1541 DWORD dwZone,
1542 DWORD dwAction,
1543 BYTE* pPolicy,
1544 DWORD cbPolicy,
1545 URLZONEREG urlZoneReg)
1547 FIXME("(%p)->(%08x %08x %p %08x %08x) stub\n", iface, dwZone, dwAction, pPolicy,
1548 cbPolicy, urlZoneReg);
1549 return E_NOTIMPL;
1552 /********************************************************************
1553 * IInternetZoneManager_PromptAction
1555 static HRESULT WINAPI ZoneMgrImpl_PromptAction(IInternetZoneManagerEx2* iface,
1556 DWORD dwAction,
1557 HWND hwndParent,
1558 LPCWSTR pwszUrl,
1559 LPCWSTR pwszText,
1560 DWORD dwPromptFlags)
1562 FIXME("%p %08x %p %s %s %08x\n", iface, dwAction, hwndParent,
1563 debugstr_w(pwszUrl), debugstr_w(pwszText), dwPromptFlags );
1564 return E_NOTIMPL;
1567 /********************************************************************
1568 * IInternetZoneManager_LogAction
1570 static HRESULT WINAPI ZoneMgrImpl_LogAction(IInternetZoneManagerEx2* iface,
1571 DWORD dwAction,
1572 LPCWSTR pwszUrl,
1573 LPCWSTR pwszText,
1574 DWORD dwLogFlags)
1576 FIXME("(%p)->(%08x %s %s %08x) stub\n", iface, dwAction, debugstr_w(pwszUrl),
1577 debugstr_w(pwszText), dwLogFlags);
1578 return E_NOTIMPL;
1581 /********************************************************************
1582 * IInternetZoneManager_CreateZoneEnumerator
1584 static HRESULT WINAPI ZoneMgrImpl_CreateZoneEnumerator(IInternetZoneManagerEx2* iface,
1585 DWORD* pdwEnum,
1586 DWORD* pdwCount,
1587 DWORD dwFlags)
1589 ZoneMgrImpl* This = impl_from_IInternetZoneManagerEx2(iface);
1590 LPDWORD * new_maps;
1591 LPDWORD data;
1592 DWORD i;
1594 TRACE("(%p)->(%p, %p, 0x%08x)\n", This, pdwEnum, pdwCount, dwFlags);
1595 if (!pdwEnum || !pdwCount || (dwFlags != 0))
1596 return E_INVALIDARG;
1598 data = build_zonemap_from_reg();
1599 TRACE("found %d zones\n", data ? data[0] : -1);
1601 if (!data)
1602 return E_FAIL;
1604 for (i = 0; i < This->zonemap_count; i++) {
1605 if (This->zonemaps && !This->zonemaps[i]) {
1606 This->zonemaps[i] = data;
1607 *pdwEnum = i;
1608 *pdwCount = data[0];
1609 return S_OK;
1613 if (This->zonemaps) {
1614 /* try to double the nr. of pointers in the array */
1615 new_maps = heap_realloc_zero(This->zonemaps, This->zonemap_count * 2 * sizeof(LPDWORD));
1616 if (new_maps)
1617 This->zonemap_count *= 2;
1619 else
1621 This->zonemap_count = 2;
1622 new_maps = heap_alloc_zero(This->zonemap_count * sizeof(LPDWORD));
1625 if (!new_maps) {
1626 heap_free(data);
1627 return E_FAIL;
1629 This->zonemaps = new_maps;
1630 This->zonemaps[i] = data;
1631 *pdwEnum = i;
1632 *pdwCount = data[0];
1633 return S_OK;
1636 /********************************************************************
1637 * IInternetZoneManager_GetZoneAt
1639 static HRESULT WINAPI ZoneMgrImpl_GetZoneAt(IInternetZoneManagerEx2* iface,
1640 DWORD dwEnum,
1641 DWORD dwIndex,
1642 DWORD* pdwZone)
1644 ZoneMgrImpl* This = impl_from_IInternetZoneManagerEx2(iface);
1645 LPDWORD data;
1647 TRACE("(%p)->(0x%08x, %d, %p)\n", This, dwEnum, dwIndex, pdwZone);
1649 /* make sure, that dwEnum and dwIndex are in the valid range */
1650 if (dwEnum < This->zonemap_count) {
1651 if ((data = This->zonemaps[dwEnum])) {
1652 if (dwIndex < data[0]) {
1653 *pdwZone = data[dwIndex + 1];
1654 return S_OK;
1658 return E_INVALIDARG;
1661 /********************************************************************
1662 * IInternetZoneManager_DestroyZoneEnumerator
1664 static HRESULT WINAPI ZoneMgrImpl_DestroyZoneEnumerator(IInternetZoneManagerEx2* iface,
1665 DWORD dwEnum)
1667 ZoneMgrImpl* This = impl_from_IInternetZoneManagerEx2(iface);
1668 LPDWORD data;
1670 TRACE("(%p)->(0x%08x)\n", This, dwEnum);
1671 /* make sure, that dwEnum is valid */
1672 if (dwEnum < This->zonemap_count) {
1673 if ((data = This->zonemaps[dwEnum])) {
1674 This->zonemaps[dwEnum] = NULL;
1675 heap_free(data);
1676 return S_OK;
1679 return E_INVALIDARG;
1682 /********************************************************************
1683 * IInternetZoneManager_CopyTemplatePoliciesToZone
1685 static HRESULT WINAPI ZoneMgrImpl_CopyTemplatePoliciesToZone(IInternetZoneManagerEx2* iface,
1686 DWORD dwTemplate,
1687 DWORD dwZone,
1688 DWORD dwReserved)
1690 FIXME("(%p)->(%08x %08x %08x) stub\n", iface, dwTemplate, dwZone, dwReserved);
1691 return E_NOTIMPL;
1694 /********************************************************************
1695 * IInternetZoneManagerEx_GetZoneActionPolicyEx
1697 static HRESULT WINAPI ZoneMgrImpl_GetZoneActionPolicyEx(IInternetZoneManagerEx2* iface,
1698 DWORD dwZone,
1699 DWORD dwAction,
1700 BYTE* pPolicy,
1701 DWORD cbPolicy,
1702 URLZONEREG urlZoneReg,
1703 DWORD dwFlags)
1705 TRACE("(%p)->(%d, 0x%x, %p, %d, %d, 0x%x)\n", iface, dwZone,
1706 dwAction, pPolicy, cbPolicy, urlZoneReg, dwFlags);
1708 if(!pPolicy)
1709 return E_INVALIDARG;
1711 if (dwFlags)
1712 FIXME("dwFlags 0x%x ignored\n", dwFlags);
1714 return get_action_policy(dwZone, dwAction, pPolicy, cbPolicy, urlZoneReg);
1717 /********************************************************************
1718 * IInternetZoneManagerEx_SetZoneActionPolicyEx
1720 static HRESULT WINAPI ZoneMgrImpl_SetZoneActionPolicyEx(IInternetZoneManagerEx2* iface,
1721 DWORD dwZone,
1722 DWORD dwAction,
1723 BYTE* pPolicy,
1724 DWORD cbPolicy,
1725 URLZONEREG urlZoneReg,
1726 DWORD dwFlags)
1728 FIXME("(%p)->(%d, 0x%x, %p, %d, %d, 0x%x) stub\n", iface, dwZone, dwAction, pPolicy,
1729 cbPolicy, urlZoneReg, dwFlags);
1730 return E_NOTIMPL;
1733 /********************************************************************
1734 * IInternetZoneManagerEx2_GetZoneAttributesEx
1736 static HRESULT WINAPI ZoneMgrImpl_GetZoneAttributesEx(IInternetZoneManagerEx2* iface,
1737 DWORD dwZone,
1738 ZONEATTRIBUTES* pZoneAttributes,
1739 DWORD dwFlags)
1741 TRACE("(%p)->(%d, %p, 0x%x)\n", iface, dwZone, pZoneAttributes, dwFlags);
1743 if (dwFlags)
1744 FIXME("dwFlags 0x%x ignored\n", dwFlags);
1746 return IInternetZoneManagerEx2_GetZoneAttributes(iface, dwZone, pZoneAttributes);
1750 /********************************************************************
1751 * IInternetZoneManagerEx2_GetZoneSecurityState
1753 static HRESULT WINAPI ZoneMgrImpl_GetZoneSecurityState(IInternetZoneManagerEx2* iface,
1754 DWORD dwZoneIndex,
1755 BOOL fRespectPolicy,
1756 LPDWORD pdwState,
1757 BOOL *pfPolicyEncountered)
1759 FIXME("(%p)->(%d, %d, %p, %p) stub\n", iface, dwZoneIndex, fRespectPolicy,
1760 pdwState, pfPolicyEncountered);
1762 *pdwState = SECURITY_IE_STATE_GREEN;
1764 if (pfPolicyEncountered)
1765 *pfPolicyEncountered = FALSE;
1767 return S_OK;
1770 /********************************************************************
1771 * IInternetZoneManagerEx2_GetIESecurityState
1773 static HRESULT WINAPI ZoneMgrImpl_GetIESecurityState(IInternetZoneManagerEx2* iface,
1774 BOOL fRespectPolicy,
1775 LPDWORD pdwState,
1776 BOOL *pfPolicyEncountered,
1777 BOOL fNoCache)
1779 FIXME("(%p)->(%d, %p, %p, %d) stub\n", iface, fRespectPolicy, pdwState,
1780 pfPolicyEncountered, fNoCache);
1782 *pdwState = SECURITY_IE_STATE_GREEN;
1784 if (pfPolicyEncountered)
1785 *pfPolicyEncountered = FALSE;
1787 return S_OK;
1790 /********************************************************************
1791 * IInternetZoneManagerEx2_FixInsecureSettings
1793 static HRESULT WINAPI ZoneMgrImpl_FixInsecureSettings(IInternetZoneManagerEx2* iface)
1795 FIXME("(%p) stub\n", iface);
1796 return S_OK;
1799 /********************************************************************
1800 * IInternetZoneManager_Construct
1802 static const IInternetZoneManagerEx2Vtbl ZoneMgrImplVtbl = {
1803 ZoneMgrImpl_QueryInterface,
1804 ZoneMgrImpl_AddRef,
1805 ZoneMgrImpl_Release,
1806 /* IInternetZoneManager */
1807 ZoneMgrImpl_GetZoneAttributes,
1808 ZoneMgrImpl_SetZoneAttributes,
1809 ZoneMgrImpl_GetZoneCustomPolicy,
1810 ZoneMgrImpl_SetZoneCustomPolicy,
1811 ZoneMgrImpl_GetZoneActionPolicy,
1812 ZoneMgrImpl_SetZoneActionPolicy,
1813 ZoneMgrImpl_PromptAction,
1814 ZoneMgrImpl_LogAction,
1815 ZoneMgrImpl_CreateZoneEnumerator,
1816 ZoneMgrImpl_GetZoneAt,
1817 ZoneMgrImpl_DestroyZoneEnumerator,
1818 ZoneMgrImpl_CopyTemplatePoliciesToZone,
1819 /* IInternetZoneManagerEx */
1820 ZoneMgrImpl_GetZoneActionPolicyEx,
1821 ZoneMgrImpl_SetZoneActionPolicyEx,
1822 /* IInternetZoneManagerEx2 */
1823 ZoneMgrImpl_GetZoneAttributesEx,
1824 ZoneMgrImpl_GetZoneSecurityState,
1825 ZoneMgrImpl_GetIESecurityState,
1826 ZoneMgrImpl_FixInsecureSettings,
1829 HRESULT ZoneMgrImpl_Construct(IUnknown *pUnkOuter, LPVOID *ppobj)
1831 ZoneMgrImpl* ret = heap_alloc_zero(sizeof(ZoneMgrImpl));
1833 TRACE("(%p %p)\n", pUnkOuter, ppobj);
1834 ret->IInternetZoneManagerEx2_iface.lpVtbl = &ZoneMgrImplVtbl;
1835 ret->ref = 1;
1836 *ppobj = (IInternetZoneManagerEx*)ret;
1838 URLMON_LockModule();
1840 return S_OK;
1843 /***********************************************************************
1844 * CoInternetCreateSecurityManager (URLMON.@)
1847 HRESULT WINAPI CoInternetCreateSecurityManager( IServiceProvider *pSP,
1848 IInternetSecurityManager **ppSM, DWORD dwReserved )
1850 TRACE("%p %p %d\n", pSP, ppSM, dwReserved );
1852 if(pSP)
1853 FIXME("pSP not supported\n");
1855 return SecManagerImpl_Construct(NULL, (void**) ppSM);
1858 /********************************************************************
1859 * CoInternetCreateZoneManager (URLMON.@)
1861 HRESULT WINAPI CoInternetCreateZoneManager(IServiceProvider* pSP, IInternetZoneManager** ppZM, DWORD dwReserved)
1863 TRACE("(%p %p %x)\n", pSP, ppZM, dwReserved);
1864 return ZoneMgrImpl_Construct(NULL, (void**)ppZM);
1867 static HRESULT parse_security_url(const WCHAR *url, PSUACTION action, WCHAR **result) {
1868 IInternetProtocolInfo *protocol_info;
1869 WCHAR *tmp, *new_url = NULL, *alloc_url = NULL;
1870 DWORD size, new_size;
1871 HRESULT hres = S_OK, parse_hres;
1873 while(1) {
1874 TRACE("parsing %s\n", debugstr_w(url));
1876 protocol_info = get_protocol_info(url);
1877 if(!protocol_info)
1878 break;
1880 size = strlenW(url)+1;
1881 new_url = CoTaskMemAlloc(size*sizeof(WCHAR));
1882 if(!new_url) {
1883 hres = E_OUTOFMEMORY;
1884 break;
1887 new_size = 0;
1888 parse_hres = IInternetProtocolInfo_ParseUrl(protocol_info, url, PARSE_SECURITY_URL, 0, new_url, size, &new_size, 0);
1889 if(parse_hres == S_FALSE) {
1890 if(!new_size) {
1891 hres = E_UNEXPECTED;
1892 break;
1895 tmp = CoTaskMemRealloc(new_url, new_size*sizeof(WCHAR));
1896 if(!tmp) {
1897 hres = E_OUTOFMEMORY;
1898 break;
1900 new_url = tmp;
1901 parse_hres = IInternetProtocolInfo_ParseUrl(protocol_info, url, PARSE_SECURITY_URL, 0, new_url,
1902 new_size, &new_size, 0);
1903 if(parse_hres == S_FALSE) {
1904 hres = E_FAIL;
1905 break;
1909 if(parse_hres != S_OK || !strcmpW(url, new_url))
1910 break;
1912 CoTaskMemFree(alloc_url);
1913 url = alloc_url = new_url;
1914 new_url = NULL;
1917 CoTaskMemFree(new_url);
1919 if(hres != S_OK) {
1920 WARN("failed: %08x\n", hres);
1921 CoTaskMemFree(alloc_url);
1922 return hres;
1925 if(action == PSU_DEFAULT && (protocol_info = get_protocol_info(url))) {
1926 size = strlenW(url)+1;
1927 new_url = CoTaskMemAlloc(size * sizeof(WCHAR));
1928 if(new_url) {
1929 new_size = 0;
1930 parse_hres = IInternetProtocolInfo_ParseUrl(protocol_info, url, PARSE_SECURITY_DOMAIN, 0,
1931 new_url, size, &new_size, 0);
1932 if(parse_hres == S_FALSE) {
1933 if(new_size) {
1934 tmp = CoTaskMemRealloc(new_url, new_size*sizeof(WCHAR));
1935 if(tmp) {
1936 new_url = tmp;
1937 parse_hres = IInternetProtocolInfo_ParseUrl(protocol_info, url, PARSE_SECURITY_DOMAIN, 0, new_url,
1938 new_size, &new_size, 0);
1939 if(parse_hres == S_FALSE)
1940 hres = E_FAIL;
1941 }else {
1942 hres = E_OUTOFMEMORY;
1944 }else {
1945 hres = E_UNEXPECTED;
1949 if(hres == S_OK && parse_hres == S_OK) {
1950 CoTaskMemFree(alloc_url);
1951 url = alloc_url = new_url;
1952 new_url = NULL;
1955 CoTaskMemFree(new_url);
1956 }else {
1957 hres = E_OUTOFMEMORY;
1959 IInternetProtocolInfo_Release(protocol_info);
1962 if(FAILED(hres)) {
1963 WARN("failed %08x\n", hres);
1964 CoTaskMemFree(alloc_url);
1965 return hres;
1968 if(!alloc_url) {
1969 size = strlenW(url)+1;
1970 alloc_url = CoTaskMemAlloc(size * sizeof(WCHAR));
1971 if(!alloc_url)
1972 return E_OUTOFMEMORY;
1973 memcpy(alloc_url, url, size * sizeof(WCHAR));
1976 *result = alloc_url;
1977 return S_OK;
1980 /********************************************************************
1981 * CoInternetGetSecurityUrl (URLMON.@)
1983 HRESULT WINAPI CoInternetGetSecurityUrl(LPCWSTR pwzUrl, LPWSTR *ppwzSecUrl, PSUACTION psuAction, DWORD dwReserved)
1985 WCHAR *secure_url;
1986 HRESULT hres;
1988 TRACE("(%p,%p,%u,%u)\n", pwzUrl, ppwzSecUrl, psuAction, dwReserved);
1990 hres = parse_security_url(pwzUrl, psuAction, &secure_url);
1991 if(FAILED(hres))
1992 return hres;
1994 if(psuAction != PSU_SECURITY_URL_ONLY) {
1995 PARSEDURLW parsed_url = { sizeof(parsed_url) };
1996 DWORD size;
1998 /* FIXME: Use helpers from uri.c */
1999 if(SUCCEEDED(ParseURLW(secure_url, &parsed_url))) {
2000 WCHAR *new_url;
2002 switch(parsed_url.nScheme) {
2003 case URL_SCHEME_FTP:
2004 case URL_SCHEME_HTTP:
2005 case URL_SCHEME_HTTPS:
2006 size = strlenW(secure_url)+1;
2007 new_url = CoTaskMemAlloc(size * sizeof(WCHAR));
2008 if(new_url)
2009 hres = UrlGetPartW(secure_url, new_url, &size, URL_PART_HOSTNAME, URL_PARTFLAG_KEEPSCHEME);
2010 else
2011 hres = E_OUTOFMEMORY;
2012 CoTaskMemFree(secure_url);
2013 if(hres != S_OK) {
2014 WARN("UrlGetPart failed: %08x\n", hres);
2015 CoTaskMemFree(new_url);
2016 return FAILED(hres) ? hres : E_FAIL;
2018 secure_url = new_url;
2023 *ppwzSecUrl = secure_url;
2024 return S_OK;
2027 /********************************************************************
2028 * CoInternetGetSecurityUrlEx (URLMON.@)
2030 HRESULT WINAPI CoInternetGetSecurityUrlEx(IUri *pUri, IUri **ppSecUri, PSUACTION psuAction, DWORD_PTR dwReserved)
2032 URL_SCHEME scheme_type;
2033 BSTR secure_uri;
2034 WCHAR *ret_url;
2035 HRESULT hres;
2037 TRACE("(%p,%p,%u,%u)\n", pUri, ppSecUri, psuAction, (DWORD)dwReserved);
2039 if(!pUri || !ppSecUri)
2040 return E_INVALIDARG;
2042 hres = IUri_GetDisplayUri(pUri, &secure_uri);
2043 if(FAILED(hres))
2044 return hres;
2046 hres = parse_security_url(secure_uri, psuAction, &ret_url);
2047 SysFreeString(secure_uri);
2048 if(FAILED(hres))
2049 return hres;
2051 /* File URIs have to hierarchical. */
2052 hres = IUri_GetScheme(pUri, (DWORD*)&scheme_type);
2053 if(SUCCEEDED(hres) && scheme_type == URL_SCHEME_FILE) {
2054 const WCHAR *tmp = ret_url;
2056 /* Check and see if a "//" is after the scheme name. */
2057 tmp += sizeof(fileW)/sizeof(WCHAR);
2058 if(*tmp != '/' || *(tmp+1) != '/')
2059 hres = E_INVALIDARG;
2062 if(SUCCEEDED(hres))
2063 hres = CreateUri(ret_url, Uri_CREATE_ALLOW_IMPLICIT_WILDCARD_SCHEME, 0, ppSecUri);
2064 CoTaskMemFree(ret_url);
2065 return hres;
2068 /********************************************************************
2069 * CompareSecurityIds (URLMON.@)
2071 HRESULT WINAPI CompareSecurityIds(BYTE *secid1, DWORD size1, BYTE *secid2, DWORD size2, DWORD reserved)
2073 FIXME("(%p %d %p %d %x)\n", secid1, size1, secid2, size2, reserved);
2074 return E_NOTIMPL;
2077 /********************************************************************
2078 * IsInternetESCEnabledLocal (URLMON.108)
2080 * Undocumented, returns if IE is running in Enhanced Security Configuration.
2082 BOOL WINAPI IsInternetESCEnabledLocal(void)
2084 static BOOL esc_initialized, esc_enabled;
2086 TRACE("()\n");
2088 if(!esc_initialized) {
2089 DWORD type, size, val;
2090 HKEY zone_map;
2092 static const WCHAR iehardenW[] = {'I','E','H','a','r','d','e','n',0};
2094 if(RegOpenKeyExW(HKEY_CURRENT_USER, zone_map_keyW, 0, KEY_QUERY_VALUE, &zone_map) == ERROR_SUCCESS) {
2095 size = sizeof(DWORD);
2096 if(RegQueryValueExW(zone_map, iehardenW, NULL, &type, (BYTE*)&val, &size) == ERROR_SUCCESS)
2097 esc_enabled = type == REG_DWORD && val != 0;
2098 RegCloseKey(zone_map);
2100 esc_initialized = TRUE;
2103 return esc_enabled;