urlmon: Use the _recalloc function instead of reimplementing it.
[wine.git] / dlls / urlmon / sec_mgr.c
blob82266066e0fe4c50b3966a13e0d026fb7a99a823
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);
36 static const WCHAR wszZonesKey[] =
37 L"Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\Zones\\";
38 static const WCHAR zone_map_keyW[] =
39 L"Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\ZoneMap";
40 static const WCHAR wszZoneMapDomainsKey[] =
41 L"Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\ZoneMap\\Domains";
43 static inline BOOL is_drive_path(const WCHAR *path)
45 return iswalpha(*path) && *(path+1) == ':';
48 /* List of schemes types Windows seems to expect to be hierarchical. */
49 static inline BOOL is_hierarchical_scheme(URL_SCHEME type) {
50 return(type == URL_SCHEME_HTTP || type == URL_SCHEME_FTP ||
51 type == URL_SCHEME_GOPHER || type == URL_SCHEME_NNTP ||
52 type == URL_SCHEME_TELNET || type == URL_SCHEME_WAIS ||
53 type == URL_SCHEME_FILE || type == URL_SCHEME_HTTPS ||
54 type == URL_SCHEME_RES);
57 /********************************************************************
58 * get_string_from_reg [internal]
60 * helper to get a string from the reg.
63 static void get_string_from_reg(HKEY hcu, HKEY hklm, LPCWSTR name, LPWSTR out, DWORD maxlen)
65 DWORD type = REG_SZ;
66 DWORD len = maxlen * sizeof(WCHAR);
67 DWORD res;
69 res = RegQueryValueExW(hcu, name, NULL, &type, (LPBYTE) out, &len);
71 if (res && hklm) {
72 len = maxlen * sizeof(WCHAR);
73 type = REG_SZ;
74 res = RegQueryValueExW(hklm, name, NULL, &type, (LPBYTE) out, &len);
77 if (res) {
78 TRACE("%s failed: %ld\n", debugstr_w(name), res);
79 *out = '\0';
83 /********************************************************************
84 * get_dword_from_reg [internal]
86 * helper to get a dword from the reg.
89 static void get_dword_from_reg(HKEY hcu, HKEY hklm, LPCWSTR name, LPDWORD out)
91 DWORD type = REG_DWORD;
92 DWORD len = sizeof(DWORD);
93 DWORD res;
95 res = RegQueryValueExW(hcu, name, NULL, &type, (LPBYTE) out, &len);
97 if (res && hklm) {
98 len = sizeof(DWORD);
99 type = REG_DWORD;
100 res = RegQueryValueExW(hklm, name, NULL, &type, (LPBYTE) out, &len);
103 if (res) {
104 TRACE("%s failed: %ld\n", debugstr_w(name), res);
105 *out = 0;
109 static HRESULT get_zone_from_reg(LPCWSTR schema, DWORD *zone)
111 DWORD res, size;
112 HKEY hkey;
114 static const WCHAR wszZoneMapProtocolKey[] =
115 L"Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\ZoneMap\\ProtocolDefaults";
117 res = RegOpenKeyW(HKEY_CURRENT_USER, wszZoneMapProtocolKey, &hkey);
118 if(res != ERROR_SUCCESS) {
119 ERR("Could not open key %s\n", debugstr_w(wszZoneMapProtocolKey));
120 return E_UNEXPECTED;
123 size = sizeof(DWORD);
124 res = RegQueryValueExW(hkey, schema, NULL, NULL, (PBYTE)zone, &size);
125 RegCloseKey(hkey);
126 if(res == ERROR_SUCCESS)
127 return S_OK;
129 res = RegOpenKeyW(HKEY_LOCAL_MACHINE, wszZoneMapProtocolKey, &hkey);
130 if(res != ERROR_SUCCESS) {
131 ERR("Could not open key %s\n", debugstr_w(wszZoneMapProtocolKey));
132 return E_UNEXPECTED;
135 size = sizeof(DWORD);
136 res = RegQueryValueExW(hkey, schema, NULL, NULL, (PBYTE)zone, &size);
137 RegCloseKey(hkey);
138 if(res == ERROR_SUCCESS)
139 return S_OK;
141 *zone = 3;
142 return S_OK;
145 /********************************************************************
146 * matches_domain_pattern [internal]
148 * Checks if the given string matches the specified domain pattern.
150 * This function looks for explicit wildcard domain components iff
151 * they appear at the very beginning of the 'pattern' string
153 * pattern = "*.google.com"
155 static BOOL matches_domain_pattern(LPCWSTR pattern, LPCWSTR str, BOOL implicit_wildcard, LPCWSTR *matched)
157 BOOL matches = FALSE;
158 DWORD pattern_len = lstrlenW(pattern);
159 DWORD str_len = lstrlenW(str);
161 TRACE("(%d) Checking if %s matches %s\n", implicit_wildcard, debugstr_w(str), debugstr_w(pattern));
163 *matched = NULL;
164 if(str_len >= pattern_len) {
165 /* Check if there's an explicit wildcard in the pattern. */
166 if(pattern[0] == '*' && pattern[1] == '.') {
167 /* Make sure that 'str' matches the wildcard pattern.
169 * Example:
170 * pattern = "*.google.com"
172 * So in this case 'str' would have to end with ".google.com" in order
173 * to map to this pattern.
175 if(str_len >= pattern_len+1 && !wcsicmp(str+(str_len-pattern_len+1), pattern+1)) {
176 /* Check if there's another '.' inside of the "unmatched" portion
177 * of 'str'.
179 * Example:
180 * pattern = "*.google.com"
181 * str = "test.testing.google.com"
183 * The currently matched portion is ".google.com" in 'str', we need
184 * see if there's a '.' inside of the unmatched portion ("test.testing"), because
185 * if there is and 'implicit_wildcard' isn't set, then this isn't
186 * a match.
188 const WCHAR *ptr;
189 for (ptr = str + str_len - pattern_len; ptr > str; ptr--) if (ptr[-1] == '.') break;
190 if (ptr == str || implicit_wildcard) {
191 matches = TRUE;
192 *matched = ptr;
195 } else if(implicit_wildcard && str_len > pattern_len) {
196 /* When the pattern has an implicit wildcard component, it means
197 * that anything goes in 'str' as long as it ends with the pattern
198 * and that the beginning of the match has a '.' before it.
200 * Example:
201 * pattern = "google.com"
202 * str = "www.google.com"
204 * Implicitly matches the pattern, where as:
206 * pattern = "google.com"
207 * str = "wwwgoogle.com"
209 * Doesn't match the pattern.
211 if(str[str_len-pattern_len-1] == '.' && !wcsicmp(str+(str_len-pattern_len), pattern)) {
212 matches = TRUE;
213 *matched = str+(str_len-pattern_len);
215 } else {
216 /* The pattern doesn't have an implicit wildcard, or an explicit wildcard,
217 * so 'str' has to be an exact match to the 'pattern'.
219 if(!wcsicmp(str, pattern)) {
220 matches = TRUE;
221 *matched = str;
226 if(matches)
227 TRACE("Found a match: matched=%s\n", debugstr_w(*matched));
228 else
229 TRACE("No match found\n");
231 return matches;
234 static BOOL get_zone_for_scheme(HKEY key, LPCWSTR schema, DWORD *zone)
236 DWORD res;
237 DWORD size = sizeof(DWORD);
238 DWORD type;
240 /* See if the key contains a value for the scheme first. */
241 res = RegQueryValueExW(key, schema, NULL, &type, (BYTE*)zone, &size);
242 if(res == ERROR_SUCCESS) {
243 if(type == REG_DWORD)
244 return TRUE;
245 WARN("Unexpected value type %ld for value %s, expected REG_DWORD\n", type, debugstr_w(schema));
248 /* Try to get the zone for the wildcard scheme. */
249 size = sizeof(DWORD);
250 res = RegQueryValueExW(key, L"*", NULL, &type, (BYTE*)zone, &size);
251 if(res != ERROR_SUCCESS)
252 return FALSE;
254 if(type != REG_DWORD) {
255 WARN("Unexpected value type %ld for value %s, expected REG_DWORD\n", type, debugstr_w(L"*"));
256 return FALSE;
259 return TRUE;
262 /********************************************************************
263 * search_domain_for_zone [internal]
265 * Searches the specified 'domain' registry key to see if 'host' maps into it, or any
266 * of its subdomain registry keys.
268 * Returns S_OK if a match is found, S_FALSE if no matches were found, or an error code.
270 static HRESULT search_domain_for_zone(HKEY domains, LPCWSTR domain, DWORD domain_len, LPCWSTR schema,
271 LPCWSTR host, DWORD host_len, DWORD *zone)
273 BOOL found = FALSE;
274 HKEY domain_key;
275 DWORD res;
276 LPCWSTR matched;
278 if(host_len >= domain_len && matches_domain_pattern(domain, host, TRUE, &matched)) {
279 res = RegOpenKeyW(domains, domain, &domain_key);
280 if(res != ERROR_SUCCESS) {
281 ERR("Failed to open domain key %s: %ld\n", debugstr_w(domain), res);
282 return E_UNEXPECTED;
285 if(matched == host)
286 found = get_zone_for_scheme(domain_key, schema, zone);
287 else {
288 INT domain_offset;
289 DWORD subdomain_count, subdomain_len;
290 BOOL check_domain = TRUE;
292 find_domain_name(domain, domain_len, &domain_offset);
294 res = RegQueryInfoKeyW(domain_key, NULL, NULL, NULL, &subdomain_count, &subdomain_len,
295 NULL, NULL, NULL, NULL, NULL, NULL);
296 if(res != ERROR_SUCCESS) {
297 ERR("Unable to query info for key %s: %ld\n", debugstr_w(domain), res);
298 RegCloseKey(domain_key);
299 return E_UNEXPECTED;
302 if(subdomain_count) {
303 WCHAR *subdomain;
304 WCHAR *component;
305 DWORD i;
307 subdomain = malloc((subdomain_len + 1) * sizeof(WCHAR));
308 if(!subdomain) {
309 RegCloseKey(domain_key);
310 return E_OUTOFMEMORY;
313 component = strndupW(host, matched-host - 1);
314 if(!component) {
315 free(subdomain);
316 RegCloseKey(domain_key);
317 return E_OUTOFMEMORY;
320 for(i = 0; i < subdomain_count; ++i) {
321 DWORD len = subdomain_len+1;
322 const WCHAR *sub_matched;
324 res = RegEnumKeyExW(domain_key, i, subdomain, &len, NULL, NULL, NULL, NULL);
325 if(res != ERROR_SUCCESS) {
326 free(component);
327 free(subdomain);
328 RegCloseKey(domain_key);
329 return E_UNEXPECTED;
332 if(matches_domain_pattern(subdomain, component, FALSE, &sub_matched)) {
333 HKEY subdomain_key;
335 res = RegOpenKeyW(domain_key, subdomain, &subdomain_key);
336 if(res != ERROR_SUCCESS) {
337 ERR("Unable to open subdomain key %s of %s: %ld\n", debugstr_w(subdomain),
338 debugstr_w(domain), res);
339 free(component);
340 free(subdomain);
341 RegCloseKey(domain_key);
342 return E_UNEXPECTED;
345 found = get_zone_for_scheme(subdomain_key, schema, zone);
346 check_domain = FALSE;
347 RegCloseKey(subdomain_key);
348 break;
351 free(subdomain);
352 free(component);
355 /* There's a chance that 'host' implicitly mapped into 'domain', in
356 * which case we check to see if 'domain' contains zone information.
358 * This can only happen if 'domain' is its own domain name.
359 * Example:
360 * "google.com" (domain name = "google.com")
362 * So if:
363 * host = "www.google.com"
365 * Then host would map directly into the "google.com" domain key.
367 * If 'domain' has more than just its domain name, or it does not
368 * have a domain name, then we don't perform the check. The reason
369 * for this is that these domains don't allow implicit mappings.
370 * Example:
371 * domain = "org" (has no domain name)
372 * host = "www.org"
374 * The mapping would only happen if the "org" key had an explicit subkey
375 * called "www".
377 if(check_domain && !domain_offset && !wcschr(host, matched-host-1))
378 found = get_zone_for_scheme(domain_key, schema, zone);
380 RegCloseKey(domain_key);
383 return found ? S_OK : S_FALSE;
386 static HRESULT search_for_domain_mapping(HKEY domains, LPCWSTR schema, LPCWSTR host, DWORD host_len, DWORD *zone)
388 WCHAR *domain;
389 DWORD domain_count, domain_len, i;
390 DWORD res;
391 HRESULT hres = S_FALSE;
393 res = RegQueryInfoKeyW(domains, NULL, NULL, NULL, &domain_count, &domain_len,
394 NULL, NULL, NULL, NULL, NULL, NULL);
395 if(res != ERROR_SUCCESS) {
396 WARN("Failed to retrieve information about key\n");
397 return E_UNEXPECTED;
400 if(!domain_count)
401 return S_FALSE;
403 domain = malloc((domain_len + 1) * sizeof(WCHAR));
404 if(!domain)
405 return E_OUTOFMEMORY;
407 for(i = 0; i < domain_count; ++i) {
408 DWORD len = domain_len+1;
410 res = RegEnumKeyExW(domains, i, domain, &len, NULL, NULL, NULL, NULL);
411 if(res != ERROR_SUCCESS) {
412 free(domain);
413 return E_UNEXPECTED;
416 hres = search_domain_for_zone(domains, domain, len, schema, host, host_len, zone);
417 if(FAILED(hres) || hres == S_OK)
418 break;
421 free(domain);
422 return hres;
425 static HRESULT get_zone_from_domains(IUri *uri, DWORD *zone)
427 HRESULT hres;
428 BSTR host, scheme;
429 DWORD res;
430 HKEY domains;
431 DWORD scheme_type;
433 hres = IUri_GetScheme(uri, &scheme_type);
434 if(FAILED(hres))
435 return hres;
437 /* Windows doesn't play nice with unknown scheme types when it tries
438 * to check if a host name maps into any domains.
440 if(scheme_type == URL_SCHEME_UNKNOWN)
441 return S_FALSE;
443 hres = IUri_GetHost(uri, &host);
444 if(FAILED(hres))
445 return hres;
447 /* Known hierarchical scheme types must have a host. If they don't Windows
448 * assigns URLZONE_INVALID to the zone.
450 if((scheme_type != URL_SCHEME_UNKNOWN && scheme_type != URL_SCHEME_FILE)
451 && is_hierarchical_scheme(scheme_type) && !*host) {
452 *zone = URLZONE_INVALID;
454 SysFreeString(host);
456 /* The MapUrlToZone functions return S_OK when this condition occurs. */
457 return S_OK;
460 hres = IUri_GetSchemeName(uri, &scheme);
461 if(FAILED(hres)) {
462 SysFreeString(host);
463 return hres;
466 /* First try CURRENT_USER. */
467 res = RegOpenKeyW(HKEY_CURRENT_USER, wszZoneMapDomainsKey, &domains);
468 if(res == ERROR_SUCCESS) {
469 hres = search_for_domain_mapping(domains, scheme, host, SysStringLen(host), zone);
470 RegCloseKey(domains);
471 } else
472 WARN("Failed to open HKCU's %s key\n", debugstr_w(wszZoneMapDomainsKey));
474 /* If that doesn't work try LOCAL_MACHINE. */
475 if(hres == S_FALSE) {
476 res = RegOpenKeyW(HKEY_LOCAL_MACHINE, wszZoneMapDomainsKey, &domains);
477 if(res == ERROR_SUCCESS) {
478 hres = search_for_domain_mapping(domains, scheme, host, SysStringLen(host), zone);
479 RegCloseKey(domains);
480 } else
481 WARN("Failed to open HKLM's %s key\n", debugstr_w(wszZoneMapDomainsKey));
484 SysFreeString(host);
485 SysFreeString(scheme);
486 return hres;
489 static HRESULT map_security_uri_to_zone(IUri *uri, DWORD *zone)
491 HRESULT hres;
492 BSTR scheme;
494 *zone = URLZONE_INVALID;
496 hres = IUri_GetSchemeName(uri, &scheme);
497 if(FAILED(hres))
498 return hres;
500 if(!wcsicmp(scheme, L"file")) {
501 BSTR path;
502 WCHAR *ptr, *path_start, root[20];
504 hres = IUri_GetPath(uri, &path);
505 if(FAILED(hres)) {
506 SysFreeString(scheme);
507 return hres;
510 if(*path == '/' && is_drive_path(path+1))
511 path_start = path+1;
512 else
513 path_start = path;
515 if((ptr = wcschr(path_start, ':')) && ptr-path_start+1 < ARRAY_SIZE(root)) {
516 UINT type;
518 memcpy(root, path_start, (ptr-path_start+1)*sizeof(WCHAR));
519 root[ptr-path_start+1] = 0;
521 type = GetDriveTypeW(root);
523 switch(type) {
524 case DRIVE_UNKNOWN:
525 case DRIVE_NO_ROOT_DIR:
526 break;
527 case DRIVE_REMOVABLE:
528 case DRIVE_FIXED:
529 case DRIVE_CDROM:
530 case DRIVE_RAMDISK:
531 *zone = URLZONE_LOCAL_MACHINE;
532 hres = S_OK;
533 break;
534 case DRIVE_REMOTE:
535 *zone = URLZONE_INTERNET;
536 hres = S_OK;
537 break;
538 default:
539 FIXME("unsupported drive type %d\n", type);
542 SysFreeString(path);
545 if(*zone == URLZONE_INVALID) {
546 hres = get_zone_from_domains(uri, zone);
547 if(hres == S_FALSE)
548 hres = get_zone_from_reg(scheme, zone);
551 SysFreeString(scheme);
552 return hres;
555 static HRESULT map_url_to_zone(LPCWSTR url, DWORD *zone, LPWSTR *ret_url)
557 IUri *secur_uri;
558 LPWSTR secur_url;
559 HRESULT hres;
561 *zone = URLZONE_INVALID;
563 hres = CoInternetGetSecurityUrl(url, &secur_url, PSU_SECURITY_URL_ONLY, 0);
564 if(hres != S_OK) {
565 DWORD size = lstrlenW(url)*sizeof(WCHAR);
567 secur_url = CoTaskMemAlloc(size);
568 if(!secur_url)
569 return E_OUTOFMEMORY;
571 memcpy(secur_url, url, size);
574 hres = CreateUri(secur_url, Uri_CREATE_ALLOW_IMPLICIT_FILE_SCHEME, 0, &secur_uri);
575 if(FAILED(hres)) {
576 CoTaskMemFree(secur_url);
577 return hres;
580 hres = map_security_uri_to_zone(secur_uri, zone);
581 IUri_Release(secur_uri);
583 if(FAILED(hres) || !ret_url)
584 CoTaskMemFree(secur_url);
585 else
586 *ret_url = secur_url;
588 return hres;
591 static HRESULT map_uri_to_zone(IUri *uri, DWORD *zone, IUri **ret_uri)
593 HRESULT hres;
594 IUri *secur_uri;
596 hres = CoInternetGetSecurityUrlEx(uri, &secur_uri, PSU_SECURITY_URL_ONLY, 0);
597 if(FAILED(hres))
598 return hres;
600 hres = map_security_uri_to_zone(secur_uri, zone);
601 if(FAILED(hres) || !ret_uri)
602 IUri_Release(secur_uri);
603 else
604 *ret_uri = secur_uri;
606 return hres;
609 static HRESULT open_zone_key(HKEY parent_key, DWORD zone, HKEY *hkey)
611 WCHAR key_name[ARRAY_SIZE(wszZonesKey) + 12];
612 DWORD res;
614 wsprintfW(key_name, L"%s%u", wszZonesKey, zone);
616 res = RegOpenKeyW(parent_key, key_name, hkey);
618 if(res != ERROR_SUCCESS) {
619 WARN("RegOpenKey failed\n");
620 return E_INVALIDARG;
623 return S_OK;
626 static HRESULT get_action_policy(DWORD zone, DWORD action, BYTE *policy, DWORD size, URLZONEREG zone_reg)
628 HKEY parent_key;
629 HKEY hkey;
630 LONG res;
631 HRESULT hres;
633 switch(action) {
634 case URLACTION_SCRIPT_OVERRIDE_SAFETY:
635 case URLACTION_ACTIVEX_OVERRIDE_SCRIPT_SAFETY:
636 *(DWORD*)policy = URLPOLICY_DISALLOW;
637 return S_OK;
640 switch(zone_reg) {
641 case URLZONEREG_DEFAULT:
642 case URLZONEREG_HKCU:
643 parent_key = HKEY_CURRENT_USER;
644 break;
645 case URLZONEREG_HKLM:
646 parent_key = HKEY_LOCAL_MACHINE;
647 break;
648 default:
649 WARN("Unknown URLZONEREG: %d\n", zone_reg);
650 return E_FAIL;
653 hres = open_zone_key(parent_key, zone, &hkey);
654 if(SUCCEEDED(hres)) {
655 WCHAR action_str[16];
656 DWORD len = size;
658 wsprintfW(action_str, L"%X", action);
660 res = RegQueryValueExW(hkey, action_str, NULL, NULL, policy, &len);
661 if(res == ERROR_MORE_DATA) {
662 hres = E_INVALIDARG;
663 }else if(res == ERROR_FILE_NOT_FOUND) {
664 hres = E_FAIL;
665 }else if(res != ERROR_SUCCESS) {
666 ERR("RegQueryValue failed: %ld\n", res);
667 hres = E_UNEXPECTED;
670 RegCloseKey(hkey);
673 if(FAILED(hres) && zone_reg == URLZONEREG_DEFAULT)
674 return get_action_policy(zone, action, policy, size, URLZONEREG_HKLM);
676 return hres;
679 static HRESULT generate_security_id(IUri *uri, BYTE *secid, DWORD *secid_len, DWORD zone)
681 DWORD len;
682 HRESULT hres;
683 DWORD scheme_type;
685 if(zone == URLZONE_INVALID)
686 return E_INVALIDARG;
688 hres = IUri_GetScheme(uri, &scheme_type);
689 if(FAILED(hres))
690 return hres;
692 /* Windows handles opaque URLs differently then hierarchical ones. */
693 if(!is_hierarchical_scheme(scheme_type) && scheme_type != URL_SCHEME_WILDCARD) {
694 BSTR display_uri;
696 hres = IUri_GetDisplayUri(uri, &display_uri);
697 if(FAILED(hres))
698 return hres;
700 len = WideCharToMultiByte(CP_ACP, 0, display_uri, -1, NULL, 0, NULL, NULL)-1;
702 if(len+sizeof(DWORD) > *secid_len) {
703 SysFreeString(display_uri);
704 return E_NOT_SUFFICIENT_BUFFER;
707 WideCharToMultiByte(CP_ACP, 0, display_uri, -1, (LPSTR)secid, len, NULL, NULL);
708 SysFreeString(display_uri);
710 *(DWORD*)(secid+len) = zone;
711 } else {
712 BSTR host, scheme;
713 DWORD host_len, scheme_len;
714 BYTE *ptr;
716 hres = IUri_GetHost(uri, &host);
717 if(FAILED(hres))
718 return hres;
720 /* The host can't be empty for Wildcard URIs. */
721 if(scheme_type == URL_SCHEME_WILDCARD && !*host) {
722 SysFreeString(host);
723 return E_INVALIDARG;
726 hres = IUri_GetSchemeName(uri, &scheme);
727 if(FAILED(hres)) {
728 SysFreeString(host);
729 return hres;
732 host_len = WideCharToMultiByte(CP_ACP, 0, host, -1, NULL, 0, NULL, NULL)-1;
733 scheme_len = WideCharToMultiByte(CP_ACP, 0, scheme, -1, NULL, 0, NULL, NULL)-1;
735 len = host_len+scheme_len+sizeof(BYTE);
737 if(len+sizeof(DWORD) > *secid_len) {
738 SysFreeString(host);
739 SysFreeString(scheme);
740 return E_NOT_SUFFICIENT_BUFFER;
743 WideCharToMultiByte(CP_ACP, 0, scheme, -1, (LPSTR)secid, len, NULL, NULL);
744 SysFreeString(scheme);
746 ptr = secid+scheme_len;
747 *ptr++ = ':';
749 WideCharToMultiByte(CP_ACP, 0, host, -1, (LPSTR)ptr, host_len, NULL, NULL);
750 SysFreeString(host);
752 ptr += host_len;
754 *(DWORD*)ptr = zone;
757 *secid_len = len+sizeof(DWORD);
759 return S_OK;
762 static HRESULT get_security_id_for_url(LPCWSTR url, BYTE *secid, DWORD *secid_len)
764 HRESULT hres;
765 DWORD zone = URLZONE_INVALID;
766 LPWSTR secur_url = NULL;
767 IUri *uri;
769 hres = map_url_to_zone(url, &zone, &secur_url);
770 if(FAILED(hres))
771 return hres == 0x80041001 ? E_INVALIDARG : hres;
773 hres = CreateUri(secur_url, Uri_CREATE_ALLOW_IMPLICIT_FILE_SCHEME, 0, &uri);
774 CoTaskMemFree(secur_url);
775 if(FAILED(hres))
776 return hres;
778 hres = generate_security_id(uri, secid, secid_len, zone);
779 IUri_Release(uri);
781 return hres;
784 static HRESULT get_security_id_for_uri(IUri *uri, BYTE *secid, DWORD *secid_len)
786 HRESULT hres;
787 IUri *secur_uri;
788 DWORD zone = URLZONE_INVALID;
790 hres = map_uri_to_zone(uri, &zone, &secur_uri);
791 if(FAILED(hres))
792 return hres;
794 hres = generate_security_id(secur_uri, secid, secid_len, zone);
795 IUri_Release(secur_uri);
797 return hres;
800 /***********************************************************************
801 * InternetSecurityManager implementation
804 typedef struct {
805 IInternetSecurityManagerEx2 IInternetSecurityManagerEx2_iface;
807 LONG ref;
809 IInternetSecurityMgrSite *mgrsite;
810 IInternetSecurityManager *custom_manager;
811 } SecManagerImpl;
813 static inline SecManagerImpl *impl_from_IInternetSecurityManagerEx2(IInternetSecurityManagerEx2 *iface)
815 return CONTAINING_RECORD(iface, SecManagerImpl, IInternetSecurityManagerEx2_iface);
818 static HRESULT WINAPI SecManagerImpl_QueryInterface(IInternetSecurityManagerEx2* iface,REFIID riid,void** ppvObject)
820 SecManagerImpl *This = impl_from_IInternetSecurityManagerEx2(iface);
822 TRACE("(%p)->(%s %p)\n",This,debugstr_guid(riid),ppvObject);
824 if(!ppvObject)
825 return E_INVALIDARG;
827 if(IsEqualIID(&IID_IUnknown, riid) ||
828 IsEqualIID(&IID_IInternetSecurityManager, riid) ||
829 IsEqualIID(&IID_IInternetSecurityManagerEx, riid) ||
830 IsEqualIID(&IID_IInternetSecurityManagerEx2, riid)) {
831 *ppvObject = iface;
832 } else {
833 WARN("not supported interface %s\n", debugstr_guid(riid));
834 *ppvObject = NULL;
835 return E_NOINTERFACE;
838 IInternetSecurityManagerEx2_AddRef(iface);
839 return S_OK;
842 static ULONG WINAPI SecManagerImpl_AddRef(IInternetSecurityManagerEx2* iface)
844 SecManagerImpl *This = impl_from_IInternetSecurityManagerEx2(iface);
845 ULONG refCount = InterlockedIncrement(&This->ref);
847 TRACE("(%p) ref=%lu\n", This, refCount);
849 return refCount;
852 static ULONG WINAPI SecManagerImpl_Release(IInternetSecurityManagerEx2* iface)
854 SecManagerImpl *This = impl_from_IInternetSecurityManagerEx2(iface);
855 ULONG refCount = InterlockedDecrement(&This->ref);
857 TRACE("(%p) ref=%lu\n", This, refCount);
859 /* destroy the object if there are no more references on it */
860 if (!refCount){
861 if(This->mgrsite)
862 IInternetSecurityMgrSite_Release(This->mgrsite);
863 if(This->custom_manager)
864 IInternetSecurityManager_Release(This->custom_manager);
866 free(This);
868 URLMON_UnlockModule();
871 return refCount;
874 static HRESULT WINAPI SecManagerImpl_SetSecuritySite(IInternetSecurityManagerEx2 *iface,
875 IInternetSecurityMgrSite *pSite)
877 SecManagerImpl *This = impl_from_IInternetSecurityManagerEx2(iface);
879 TRACE("(%p)->(%p)\n", This, pSite);
881 if(This->mgrsite)
882 IInternetSecurityMgrSite_Release(This->mgrsite);
884 if(This->custom_manager) {
885 IInternetSecurityManager_Release(This->custom_manager);
886 This->custom_manager = NULL;
889 This->mgrsite = pSite;
891 if(pSite) {
892 IServiceProvider *servprov;
893 HRESULT hres;
895 IInternetSecurityMgrSite_AddRef(pSite);
897 hres = IInternetSecurityMgrSite_QueryInterface(pSite, &IID_IServiceProvider,
898 (void**)&servprov);
899 if(SUCCEEDED(hres)) {
900 IServiceProvider_QueryService(servprov, &SID_SInternetSecurityManager,
901 &IID_IInternetSecurityManager, (void**)&This->custom_manager);
902 IServiceProvider_Release(servprov);
906 return S_OK;
909 static HRESULT WINAPI SecManagerImpl_GetSecuritySite(IInternetSecurityManagerEx2 *iface,
910 IInternetSecurityMgrSite **ppSite)
912 SecManagerImpl *This = impl_from_IInternetSecurityManagerEx2(iface);
914 TRACE("(%p)->(%p)\n", This, ppSite);
916 if(!ppSite)
917 return E_INVALIDARG;
919 if(This->mgrsite)
920 IInternetSecurityMgrSite_AddRef(This->mgrsite);
922 *ppSite = This->mgrsite;
923 return S_OK;
926 static HRESULT WINAPI SecManagerImpl_MapUrlToZone(IInternetSecurityManagerEx2 *iface,
927 LPCWSTR pwszUrl, DWORD *pdwZone,
928 DWORD dwFlags)
930 SecManagerImpl *This = impl_from_IInternetSecurityManagerEx2(iface);
931 HRESULT hres;
933 TRACE("(%p)->(%s %p %08lx)\n", iface, debugstr_w(pwszUrl), pdwZone, dwFlags);
935 if(This->custom_manager) {
936 hres = IInternetSecurityManager_MapUrlToZone(This->custom_manager,
937 pwszUrl, pdwZone, dwFlags);
938 if(hres != INET_E_DEFAULT_ACTION)
939 return hres;
942 if(!pwszUrl) {
943 *pdwZone = URLZONE_INVALID;
944 return E_INVALIDARG;
947 if(dwFlags)
948 FIXME("not supported flags: %08lx\n", dwFlags);
950 return map_url_to_zone(pwszUrl, pdwZone, NULL);
953 static HRESULT WINAPI SecManagerImpl_GetSecurityId(IInternetSecurityManagerEx2 *iface,
954 LPCWSTR pwszUrl, BYTE *pbSecurityId, DWORD *pcbSecurityId, DWORD_PTR dwReserved)
956 SecManagerImpl *This = impl_from_IInternetSecurityManagerEx2(iface);
958 TRACE("(%p)->(%s %p %p %08Ix)\n", iface, debugstr_w(pwszUrl), pbSecurityId,
959 pcbSecurityId, dwReserved);
961 if(This->custom_manager) {
962 HRESULT hres;
964 hres = IInternetSecurityManager_GetSecurityId(This->custom_manager,
965 pwszUrl, pbSecurityId, pcbSecurityId, dwReserved);
966 if(hres != INET_E_DEFAULT_ACTION)
967 return hres;
970 if(!pwszUrl || !pbSecurityId || !pcbSecurityId)
971 return E_INVALIDARG;
973 if(dwReserved)
974 FIXME("dwReserved is not supported\n");
976 return get_security_id_for_url(pwszUrl, pbSecurityId, pcbSecurityId);
980 static HRESULT WINAPI SecManagerImpl_ProcessUrlAction(IInternetSecurityManagerEx2 *iface,
981 LPCWSTR pwszUrl, DWORD dwAction,
982 BYTE *pPolicy, DWORD cbPolicy,
983 BYTE *pContext, DWORD cbContext,
984 DWORD dwFlags, DWORD dwReserved)
986 SecManagerImpl *This = impl_from_IInternetSecurityManagerEx2(iface);
987 DWORD zone, policy;
988 HRESULT hres;
990 TRACE("(%p)->(%s %08lx %p %08lx %p %08lx %08lx %08lx)\n", iface, debugstr_w(pwszUrl), dwAction,
991 pPolicy, cbPolicy, pContext, cbContext, dwFlags, dwReserved);
993 if(This->custom_manager) {
994 hres = IInternetSecurityManager_ProcessUrlAction(This->custom_manager, pwszUrl, dwAction,
995 pPolicy, cbPolicy, pContext, cbContext, dwFlags, dwReserved);
996 if(hres != INET_E_DEFAULT_ACTION)
997 return hres;
1000 if(dwFlags || dwReserved)
1001 FIXME("Unsupported arguments\n");
1003 if(!pwszUrl)
1004 return E_INVALIDARG;
1006 hres = map_url_to_zone(pwszUrl, &zone, NULL);
1007 if(FAILED(hres))
1008 return hres;
1010 hres = get_action_policy(zone, dwAction, (BYTE*)&policy, sizeof(policy), URLZONEREG_DEFAULT);
1011 if(FAILED(hres))
1012 return hres;
1014 TRACE("policy %lx\n", policy);
1015 if(cbPolicy >= sizeof(DWORD))
1016 *(DWORD*)pPolicy = policy;
1018 switch(GetUrlPolicyPermissions(policy)) {
1019 case URLPOLICY_ALLOW:
1020 case URLPOLICY_CHANNEL_SOFTDIST_PRECACHE:
1021 return S_OK;
1022 case URLPOLICY_DISALLOW:
1023 return S_FALSE;
1024 case URLPOLICY_QUERY:
1025 FIXME("URLPOLICY_QUERY not implemented\n");
1026 return E_FAIL;
1027 default:
1028 FIXME("Not implemented policy %lx\n", policy);
1031 return E_FAIL;
1035 static HRESULT WINAPI SecManagerImpl_QueryCustomPolicy(IInternetSecurityManagerEx2 *iface,
1036 LPCWSTR pwszUrl, REFGUID guidKey,
1037 BYTE **ppPolicy, DWORD *pcbPolicy,
1038 BYTE *pContext, DWORD cbContext,
1039 DWORD dwReserved)
1041 SecManagerImpl *This = impl_from_IInternetSecurityManagerEx2(iface);
1042 HRESULT hres;
1044 TRACE("(%p)->(%s %s %p %p %p %08lx %08lx )\n", iface, debugstr_w(pwszUrl), debugstr_guid(guidKey),
1045 ppPolicy, pcbPolicy, pContext, cbContext, dwReserved);
1047 if(This->custom_manager) {
1048 hres = IInternetSecurityManager_QueryCustomPolicy(This->custom_manager, pwszUrl, guidKey,
1049 ppPolicy, pcbPolicy, pContext, cbContext, dwReserved);
1050 if(hres != INET_E_DEFAULT_ACTION)
1051 return hres;
1054 WARN("Unknown guidKey %s\n", debugstr_guid(guidKey));
1055 return HRESULT_FROM_WIN32(ERROR_NOT_FOUND);
1058 static HRESULT WINAPI SecManagerImpl_SetZoneMapping(IInternetSecurityManagerEx2 *iface,
1059 DWORD dwZone, LPCWSTR pwszPattern, DWORD dwFlags)
1061 SecManagerImpl *This = impl_from_IInternetSecurityManagerEx2(iface);
1062 HRESULT hres;
1064 TRACE("(%p)->(%08lx %s %08lx)\n", iface, dwZone, debugstr_w(pwszPattern),dwFlags);
1066 if(This->custom_manager) {
1067 hres = IInternetSecurityManager_SetZoneMapping(This->custom_manager, dwZone,
1068 pwszPattern, dwFlags);
1069 if(hres != INET_E_DEFAULT_ACTION)
1070 return hres;
1073 FIXME("Default action is not implemented\n");
1074 return E_NOTIMPL;
1077 static HRESULT WINAPI SecManagerImpl_GetZoneMappings(IInternetSecurityManagerEx2 *iface,
1078 DWORD dwZone, IEnumString **ppenumString, DWORD dwFlags)
1080 SecManagerImpl *This = impl_from_IInternetSecurityManagerEx2(iface);
1081 HRESULT hres;
1083 TRACE("(%p)->(%08lx %p %08lx)\n", iface, dwZone, ppenumString,dwFlags);
1085 if(This->custom_manager) {
1086 hres = IInternetSecurityManager_GetZoneMappings(This->custom_manager, dwZone,
1087 ppenumString, dwFlags);
1088 if(hres != INET_E_DEFAULT_ACTION)
1089 return hres;
1092 FIXME("Default action is not implemented\n");
1093 return E_NOTIMPL;
1096 static HRESULT WINAPI SecManagerImpl_ProcessUrlActionEx(IInternetSecurityManagerEx2 *iface,
1097 LPCWSTR pwszUrl, DWORD dwAction, BYTE *pPolicy, DWORD cbPolicy, BYTE *pContext, DWORD cbContext,
1098 DWORD dwFlags, DWORD dwReserved, DWORD *pdwOutFlags)
1100 SecManagerImpl *This = impl_from_IInternetSecurityManagerEx2(iface);
1101 FIXME("(%p)->(%s %08lx %p %ld %p %ld %08lx %08lx %p) stub\n", This, debugstr_w(pwszUrl), dwAction, pPolicy, cbPolicy,
1102 pContext, cbContext, dwFlags, dwReserved, pdwOutFlags);
1103 return E_NOTIMPL;
1106 static HRESULT WINAPI SecManagerImpl_MapUrlToZoneEx2(IInternetSecurityManagerEx2 *iface,
1107 IUri *pUri, DWORD *pdwZone, DWORD dwFlags, LPWSTR *ppwszMappedUrl, DWORD *pdwOutFlags)
1109 SecManagerImpl *This = impl_from_IInternetSecurityManagerEx2(iface);
1111 TRACE("(%p)->(%p %p %08lx %p %p)\n", This, pUri, pdwZone, dwFlags, ppwszMappedUrl, pdwOutFlags);
1113 if(This->custom_manager) {
1114 HRESULT hres;
1115 IInternetSecurityManagerEx2 *sec_mgr2;
1117 hres = IInternetSecurityManager_QueryInterface(This->custom_manager, &IID_IInternetSecurityManagerEx2,
1118 (void**)&sec_mgr2);
1119 if(SUCCEEDED(hres)) {
1120 hres = IInternetSecurityManagerEx2_MapUrlToZoneEx2(sec_mgr2, pUri, pdwZone, dwFlags, ppwszMappedUrl, pdwOutFlags);
1121 IInternetSecurityManagerEx2_Release(sec_mgr2);
1122 } else {
1123 BSTR url;
1125 hres = IUri_GetDisplayUri(pUri, &url);
1126 if(FAILED(hres))
1127 return hres;
1129 hres = IInternetSecurityManager_MapUrlToZone(This->custom_manager, url, pdwZone, dwFlags);
1130 SysFreeString(url);
1133 if(hres != INET_E_DEFAULT_ACTION)
1134 return hres;
1137 if(!pdwZone)
1138 return E_INVALIDARG;
1140 if(!pUri) {
1141 *pdwZone = URLZONE_INVALID;
1142 return E_INVALIDARG;
1145 if(dwFlags)
1146 FIXME("Unsupported flags: %08lx\n", dwFlags);
1148 return map_uri_to_zone(pUri, pdwZone, NULL);
1151 static HRESULT WINAPI SecManagerImpl_ProcessUrlActionEx2(IInternetSecurityManagerEx2 *iface,
1152 IUri *pUri, DWORD dwAction, BYTE *pPolicy, DWORD cbPolicy, BYTE *pContext, DWORD cbContext,
1153 DWORD dwFlags, DWORD_PTR dwReserved, DWORD *pdwOutFlags)
1155 SecManagerImpl *This = impl_from_IInternetSecurityManagerEx2(iface);
1156 FIXME("(%p)->(%p %08lx %p %ld %p %ld %08lx %08Ix %p) stub\n", This, pUri, dwAction, pPolicy,
1157 cbPolicy, pContext, cbContext, dwFlags, dwReserved, pdwOutFlags);
1158 return E_NOTIMPL;
1161 static HRESULT WINAPI SecManagerImpl_GetSecurityIdEx2(IInternetSecurityManagerEx2 *iface,
1162 IUri *pUri, BYTE *pbSecurityId, DWORD *pcbSecurityId, DWORD_PTR dwReserved)
1164 SecManagerImpl *This = impl_from_IInternetSecurityManagerEx2(iface);
1165 TRACE("(%p)->(%p %p %p %08Ix) stub\n", This, pUri, pbSecurityId, pcbSecurityId, dwReserved);
1167 if(dwReserved)
1168 FIXME("dwReserved is not supported yet\n");
1170 if(!pUri || !pcbSecurityId || !pbSecurityId)
1171 return E_INVALIDARG;
1173 return get_security_id_for_uri(pUri, pbSecurityId, pcbSecurityId);
1176 static HRESULT WINAPI SecManagerImpl_QueryCustomPolicyEx2(IInternetSecurityManagerEx2 *iface,
1177 IUri *pUri, REFGUID guidKey, BYTE **ppPolicy, DWORD *pcbPolicy, BYTE *pContext,
1178 DWORD cbContext, DWORD_PTR dwReserved)
1180 SecManagerImpl *This = impl_from_IInternetSecurityManagerEx2(iface);
1181 FIXME("(%p)->(%p %s %p %p %p %ld %08Ix) stub\n", This, pUri, debugstr_guid(guidKey), ppPolicy, pcbPolicy,
1182 pContext, cbContext, dwReserved);
1183 return E_NOTIMPL;
1186 static const IInternetSecurityManagerEx2Vtbl VT_SecManagerImpl =
1188 SecManagerImpl_QueryInterface,
1189 SecManagerImpl_AddRef,
1190 SecManagerImpl_Release,
1191 SecManagerImpl_SetSecuritySite,
1192 SecManagerImpl_GetSecuritySite,
1193 SecManagerImpl_MapUrlToZone,
1194 SecManagerImpl_GetSecurityId,
1195 SecManagerImpl_ProcessUrlAction,
1196 SecManagerImpl_QueryCustomPolicy,
1197 SecManagerImpl_SetZoneMapping,
1198 SecManagerImpl_GetZoneMappings,
1199 SecManagerImpl_ProcessUrlActionEx,
1200 SecManagerImpl_MapUrlToZoneEx2,
1201 SecManagerImpl_ProcessUrlActionEx2,
1202 SecManagerImpl_GetSecurityIdEx2,
1203 SecManagerImpl_QueryCustomPolicyEx2
1206 HRESULT SecManagerImpl_Construct(IUnknown *pUnkOuter, LPVOID *ppobj)
1208 SecManagerImpl *This;
1210 TRACE("(%p,%p)\n",pUnkOuter,ppobj);
1211 This = malloc(sizeof(*This));
1213 /* Initialize the virtual function table. */
1214 This->IInternetSecurityManagerEx2_iface.lpVtbl = &VT_SecManagerImpl;
1216 This->ref = 1;
1217 This->mgrsite = NULL;
1218 This->custom_manager = NULL;
1220 *ppobj = This;
1222 URLMON_LockModule();
1224 return S_OK;
1227 /***********************************************************************
1228 * InternetZoneManager implementation
1231 typedef struct {
1232 IInternetZoneManagerEx2 IInternetZoneManagerEx2_iface;
1233 LONG ref;
1234 LPDWORD *zonemaps;
1235 DWORD zonemap_count;
1236 } ZoneMgrImpl;
1238 static inline ZoneMgrImpl *impl_from_IInternetZoneManagerEx2(IInternetZoneManagerEx2 *iface)
1240 return CONTAINING_RECORD(iface, ZoneMgrImpl, IInternetZoneManagerEx2_iface);
1244 /***********************************************************************
1245 * build_zonemap_from_reg [internal]
1247 * Enumerate the Zones in the Registry and return the Zones in a DWORD-array
1248 * The number of the Zones is returned in data[0]
1250 static LPDWORD build_zonemap_from_reg(void)
1252 WCHAR name[32];
1253 HKEY hkey;
1254 LPDWORD data = NULL;
1255 DWORD allocated = 6; /* space for the zonecount and Zone "0" up to Zone "4" */
1256 DWORD used = 0;
1257 DWORD res;
1258 DWORD len;
1261 res = RegOpenKeyW(HKEY_CURRENT_USER, wszZonesKey, &hkey);
1262 if (res)
1263 return NULL;
1265 data = malloc(allocated * sizeof(DWORD));
1266 if (!data)
1267 goto cleanup;
1269 while (!res) {
1270 name[0] = '\0';
1271 len = ARRAY_SIZE(name);
1272 res = RegEnumKeyExW(hkey, used, name, &len, NULL, NULL, NULL, NULL);
1274 if (!res) {
1275 used++;
1276 if (used == allocated) {
1277 LPDWORD new_data;
1279 allocated *= 2;
1280 new_data = realloc(data, allocated * sizeof(DWORD));
1281 if (!new_data)
1282 goto cleanup;
1284 data = new_data;
1286 data[used] = wcstol(name, NULL, 10);
1289 if (used) {
1290 RegCloseKey(hkey);
1291 data[0] = used;
1292 return data;
1295 cleanup:
1296 /* something failed */
1297 RegCloseKey(hkey);
1298 free(data);
1299 return NULL;
1302 /********************************************************************
1303 * IInternetZoneManager_QueryInterface
1305 static HRESULT WINAPI ZoneMgrImpl_QueryInterface(IInternetZoneManagerEx2* iface, REFIID riid, void** ppvObject)
1307 ZoneMgrImpl* This = impl_from_IInternetZoneManagerEx2(iface);
1309 TRACE("(%p)->(%s,%p)\n", This, debugstr_guid(riid), ppvObject);
1311 if(!This || !ppvObject)
1312 return E_INVALIDARG;
1314 if(IsEqualIID(&IID_IUnknown, riid)) {
1315 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppvObject);
1316 }else if(IsEqualIID(&IID_IInternetZoneManager, riid)) {
1317 TRACE("(%p)->(IID_InternetZoneManager %p)\n", This, ppvObject);
1318 }else if(IsEqualIID(&IID_IInternetZoneManagerEx, riid)) {
1319 TRACE("(%p)->(IID_InternetZoneManagerEx %p)\n", This, ppvObject);
1320 }else if(IsEqualIID(&IID_IInternetZoneManagerEx2, riid)) {
1321 TRACE("(%p)->(IID_InternetZoneManagerEx2 %p)\n", This, ppvObject);
1323 else
1325 FIXME("Unknown interface: %s\n", debugstr_guid(riid));
1326 *ppvObject = NULL;
1327 return E_NOINTERFACE;
1330 *ppvObject = iface;
1331 IInternetZoneManagerEx2_AddRef(iface);
1332 return S_OK;
1335 /********************************************************************
1336 * IInternetZoneManager_AddRef
1338 static ULONG WINAPI ZoneMgrImpl_AddRef(IInternetZoneManagerEx2* iface)
1340 ZoneMgrImpl* This = impl_from_IInternetZoneManagerEx2(iface);
1341 ULONG refCount = InterlockedIncrement(&This->ref);
1343 TRACE("(%p)->(ref before=%lu)\n",This, refCount - 1);
1345 return refCount;
1348 /********************************************************************
1349 * IInternetZoneManager_Release
1351 static ULONG WINAPI ZoneMgrImpl_Release(IInternetZoneManagerEx2* iface)
1353 ZoneMgrImpl* This = impl_from_IInternetZoneManagerEx2(iface);
1354 ULONG refCount = InterlockedDecrement(&This->ref);
1356 TRACE("(%p)->(ref before=%lu)\n",This, refCount + 1);
1358 if(!refCount) {
1359 while (This->zonemap_count) free(This->zonemaps[--This->zonemap_count]);
1360 free(This->zonemaps);
1361 free(This);
1362 URLMON_UnlockModule();
1365 return refCount;
1368 /********************************************************************
1369 * IInternetZoneManager_GetZoneAttributes
1371 static HRESULT WINAPI ZoneMgrImpl_GetZoneAttributes(IInternetZoneManagerEx2* iface,
1372 DWORD dwZone,
1373 ZONEATTRIBUTES* pZoneAttributes)
1375 ZoneMgrImpl* This = impl_from_IInternetZoneManagerEx2(iface);
1376 HRESULT hr;
1377 HKEY hcu;
1378 HKEY hklm = NULL;
1380 TRACE("(%p)->(%ld %p)\n", This, dwZone, pZoneAttributes);
1382 if (!pZoneAttributes)
1383 return E_INVALIDARG;
1385 hr = open_zone_key(HKEY_CURRENT_USER, dwZone, &hcu);
1386 if (FAILED(hr))
1387 return S_OK; /* IE6 and older returned E_FAIL here */
1389 hr = open_zone_key(HKEY_LOCAL_MACHINE, dwZone, &hklm);
1390 if (FAILED(hr))
1391 TRACE("Zone %ld not in HKLM\n", dwZone);
1393 get_string_from_reg(hcu, hklm, L"DisplayName", pZoneAttributes->szDisplayName, MAX_ZONE_PATH);
1394 get_string_from_reg(hcu, hklm, L"Description", pZoneAttributes->szDescription, MAX_ZONE_DESCRIPTION);
1395 get_string_from_reg(hcu, hklm, L"Icon", pZoneAttributes->szIconPath, MAX_ZONE_PATH);
1396 get_dword_from_reg(hcu, hklm, L"MinLevel", &pZoneAttributes->dwTemplateMinLevel);
1397 get_dword_from_reg(hcu, hklm, L"CurrentLevel", &pZoneAttributes->dwTemplateCurrentLevel);
1398 get_dword_from_reg(hcu, hklm, L"RecommendedLevel", &pZoneAttributes->dwTemplateRecommended);
1399 get_dword_from_reg(hcu, hklm, L"Flags", &pZoneAttributes->dwFlags);
1401 RegCloseKey(hklm);
1402 RegCloseKey(hcu);
1403 return S_OK;
1406 /********************************************************************
1407 * IInternetZoneManager_SetZoneAttributes
1409 static HRESULT WINAPI ZoneMgrImpl_SetZoneAttributes(IInternetZoneManagerEx2* iface,
1410 DWORD dwZone,
1411 ZONEATTRIBUTES* pZoneAttributes)
1413 ZoneMgrImpl* This = impl_from_IInternetZoneManagerEx2(iface);
1414 HRESULT hr;
1415 HKEY hcu;
1417 TRACE("(%p)->(%ld %p)\n", This, dwZone, pZoneAttributes);
1419 if (!pZoneAttributes)
1420 return E_INVALIDARG;
1422 hr = open_zone_key(HKEY_CURRENT_USER, dwZone, &hcu);
1423 if (FAILED(hr))
1424 return S_OK; /* IE6 returned E_FAIL here */
1426 /* cbSize is ignored */
1427 RegSetValueExW(hcu, L"DisplayName", 0, REG_SZ, (BYTE*)pZoneAttributes->szDisplayName,
1428 (lstrlenW(pZoneAttributes->szDisplayName)+1)* sizeof(WCHAR));
1430 RegSetValueExW(hcu, L"Description", 0, REG_SZ, (BYTE*)pZoneAttributes->szDescription,
1431 (lstrlenW(pZoneAttributes->szDescription)+1)* sizeof(WCHAR));
1433 RegSetValueExW(hcu, L"Icon", 0, REG_SZ, (BYTE*)pZoneAttributes->szIconPath,
1434 (lstrlenW(pZoneAttributes->szIconPath)+1)* sizeof(WCHAR));
1436 RegSetValueExW(hcu, L"MinLevel", 0, REG_DWORD,
1437 (const BYTE*) &pZoneAttributes->dwTemplateMinLevel, sizeof(DWORD));
1439 RegSetValueExW(hcu, L"CurrentLevel", 0, REG_DWORD,
1440 (const BYTE*) &pZoneAttributes->dwTemplateCurrentLevel, sizeof(DWORD));
1442 RegSetValueExW(hcu, L"RecommendedLevel", 0, REG_DWORD,
1443 (const BYTE*) &pZoneAttributes->dwTemplateRecommended, sizeof(DWORD));
1445 RegSetValueExW(hcu, L"Flags", 0, REG_DWORD, (const BYTE*) &pZoneAttributes->dwFlags, sizeof(DWORD));
1446 RegCloseKey(hcu);
1447 return S_OK;
1451 /********************************************************************
1452 * IInternetZoneManager_GetZoneCustomPolicy
1454 static HRESULT WINAPI ZoneMgrImpl_GetZoneCustomPolicy(IInternetZoneManagerEx2* iface,
1455 DWORD dwZone,
1456 REFGUID guidKey,
1457 BYTE** ppPolicy,
1458 DWORD* pcbPolicy,
1459 URLZONEREG ulrZoneReg)
1461 FIXME("(%p)->(%08lx %s %p %p %08x) stub\n", iface, dwZone, debugstr_guid(guidKey),
1462 ppPolicy, pcbPolicy, ulrZoneReg);
1463 return E_NOTIMPL;
1466 /********************************************************************
1467 * IInternetZoneManager_SetZoneCustomPolicy
1469 static HRESULT WINAPI ZoneMgrImpl_SetZoneCustomPolicy(IInternetZoneManagerEx2* iface,
1470 DWORD dwZone,
1471 REFGUID guidKey,
1472 BYTE* ppPolicy,
1473 DWORD cbPolicy,
1474 URLZONEREG ulrZoneReg)
1476 FIXME("(%p)->(%08lx %s %p %08lx %08x) stub\n", iface, dwZone, debugstr_guid(guidKey),
1477 ppPolicy, cbPolicy, ulrZoneReg);
1478 return E_NOTIMPL;
1481 /********************************************************************
1482 * IInternetZoneManager_GetZoneActionPolicy
1484 static HRESULT WINAPI ZoneMgrImpl_GetZoneActionPolicy(IInternetZoneManagerEx2* iface,
1485 DWORD dwZone, DWORD dwAction, BYTE* pPolicy, DWORD cbPolicy, URLZONEREG urlZoneReg)
1487 TRACE("(%p)->(%ld %08lx %p %ld %d)\n", iface, dwZone, dwAction, pPolicy,
1488 cbPolicy, urlZoneReg);
1490 if(!pPolicy)
1491 return E_INVALIDARG;
1493 return get_action_policy(dwZone, dwAction, pPolicy, cbPolicy, urlZoneReg);
1496 /********************************************************************
1497 * IInternetZoneManager_SetZoneActionPolicy
1499 static HRESULT WINAPI ZoneMgrImpl_SetZoneActionPolicy(IInternetZoneManagerEx2* iface,
1500 DWORD dwZone,
1501 DWORD dwAction,
1502 BYTE* pPolicy,
1503 DWORD cbPolicy,
1504 URLZONEREG urlZoneReg)
1506 FIXME("(%p)->(%08lx %08lx %p %08lx %08x) stub\n", iface, dwZone, dwAction, pPolicy,
1507 cbPolicy, urlZoneReg);
1508 return E_NOTIMPL;
1511 /********************************************************************
1512 * IInternetZoneManager_PromptAction
1514 static HRESULT WINAPI ZoneMgrImpl_PromptAction(IInternetZoneManagerEx2* iface,
1515 DWORD dwAction,
1516 HWND hwndParent,
1517 LPCWSTR pwszUrl,
1518 LPCWSTR pwszText,
1519 DWORD dwPromptFlags)
1521 FIXME("%p %08lx %p %s %s %08lx\n", iface, dwAction, hwndParent,
1522 debugstr_w(pwszUrl), debugstr_w(pwszText), dwPromptFlags );
1523 return E_NOTIMPL;
1526 /********************************************************************
1527 * IInternetZoneManager_LogAction
1529 static HRESULT WINAPI ZoneMgrImpl_LogAction(IInternetZoneManagerEx2* iface,
1530 DWORD dwAction,
1531 LPCWSTR pwszUrl,
1532 LPCWSTR pwszText,
1533 DWORD dwLogFlags)
1535 FIXME("(%p)->(%08lx %s %s %08lx) stub\n", iface, dwAction, debugstr_w(pwszUrl),
1536 debugstr_w(pwszText), dwLogFlags);
1537 return E_NOTIMPL;
1540 /********************************************************************
1541 * IInternetZoneManager_CreateZoneEnumerator
1543 static HRESULT WINAPI ZoneMgrImpl_CreateZoneEnumerator(IInternetZoneManagerEx2* iface,
1544 DWORD* pdwEnum,
1545 DWORD* pdwCount,
1546 DWORD dwFlags)
1548 ZoneMgrImpl* This = impl_from_IInternetZoneManagerEx2(iface);
1549 LPDWORD * new_maps;
1550 LPDWORD data;
1551 DWORD new_map_count;
1552 DWORD i;
1554 TRACE("(%p)->(%p, %p, 0x%08lx)\n", This, pdwEnum, pdwCount, dwFlags);
1555 if (!pdwEnum || !pdwCount || (dwFlags != 0))
1556 return E_INVALIDARG;
1558 data = build_zonemap_from_reg();
1559 TRACE("found %ld zones\n", data ? data[0] : -1);
1561 if (!data)
1562 return E_FAIL;
1564 for (i = 0; i < This->zonemap_count; i++) {
1565 if (This->zonemaps && !This->zonemaps[i]) {
1566 This->zonemaps[i] = data;
1567 *pdwEnum = i;
1568 *pdwCount = data[0];
1569 return S_OK;
1573 /* try to double the number of pointers in the array */
1574 new_map_count = This->zonemaps ? This->zonemap_count * 2 : 2;
1575 new_maps = _recalloc(This->zonemaps, new_map_count, sizeof(DWORD*));
1576 if (!new_maps) {
1577 free(data);
1578 return E_FAIL;
1580 This->zonemaps = new_maps;
1581 This->zonemap_count = new_map_count;
1582 This->zonemaps[i] = data;
1583 *pdwEnum = i;
1584 *pdwCount = data[0];
1585 return S_OK;
1588 /********************************************************************
1589 * IInternetZoneManager_GetZoneAt
1591 static HRESULT WINAPI ZoneMgrImpl_GetZoneAt(IInternetZoneManagerEx2* iface,
1592 DWORD dwEnum,
1593 DWORD dwIndex,
1594 DWORD* pdwZone)
1596 ZoneMgrImpl* This = impl_from_IInternetZoneManagerEx2(iface);
1597 LPDWORD data;
1599 TRACE("(%p)->(0x%08lx, %ld, %p)\n", This, dwEnum, dwIndex, pdwZone);
1601 /* make sure, that dwEnum and dwIndex are in the valid range */
1602 if (dwEnum < This->zonemap_count) {
1603 if ((data = This->zonemaps[dwEnum])) {
1604 if (dwIndex < data[0]) {
1605 *pdwZone = data[dwIndex + 1];
1606 return S_OK;
1610 return E_INVALIDARG;
1613 /********************************************************************
1614 * IInternetZoneManager_DestroyZoneEnumerator
1616 static HRESULT WINAPI ZoneMgrImpl_DestroyZoneEnumerator(IInternetZoneManagerEx2* iface,
1617 DWORD dwEnum)
1619 ZoneMgrImpl* This = impl_from_IInternetZoneManagerEx2(iface);
1620 LPDWORD data;
1622 TRACE("(%p)->(0x%08lx)\n", This, dwEnum);
1623 /* make sure, that dwEnum is valid */
1624 if (dwEnum < This->zonemap_count) {
1625 if ((data = This->zonemaps[dwEnum])) {
1626 This->zonemaps[dwEnum] = NULL;
1627 free(data);
1628 return S_OK;
1631 return E_INVALIDARG;
1634 /********************************************************************
1635 * IInternetZoneManager_CopyTemplatePoliciesToZone
1637 static HRESULT WINAPI ZoneMgrImpl_CopyTemplatePoliciesToZone(IInternetZoneManagerEx2* iface,
1638 DWORD dwTemplate,
1639 DWORD dwZone,
1640 DWORD dwReserved)
1642 FIXME("(%p)->(%08lx %08lx %08lx) stub\n", iface, dwTemplate, dwZone, dwReserved);
1643 return E_NOTIMPL;
1646 /********************************************************************
1647 * IInternetZoneManagerEx_GetZoneActionPolicyEx
1649 static HRESULT WINAPI ZoneMgrImpl_GetZoneActionPolicyEx(IInternetZoneManagerEx2* iface,
1650 DWORD dwZone,
1651 DWORD dwAction,
1652 BYTE* pPolicy,
1653 DWORD cbPolicy,
1654 URLZONEREG urlZoneReg,
1655 DWORD dwFlags)
1657 TRACE("(%p)->(%ld, 0x%lx, %p, %ld, %d, 0x%lx)\n", iface, dwZone,
1658 dwAction, pPolicy, cbPolicy, urlZoneReg, dwFlags);
1660 if(!pPolicy)
1661 return E_INVALIDARG;
1663 if (dwFlags)
1664 FIXME("dwFlags 0x%lx ignored\n", dwFlags);
1666 return get_action_policy(dwZone, dwAction, pPolicy, cbPolicy, urlZoneReg);
1669 /********************************************************************
1670 * IInternetZoneManagerEx_SetZoneActionPolicyEx
1672 static HRESULT WINAPI ZoneMgrImpl_SetZoneActionPolicyEx(IInternetZoneManagerEx2* iface,
1673 DWORD dwZone,
1674 DWORD dwAction,
1675 BYTE* pPolicy,
1676 DWORD cbPolicy,
1677 URLZONEREG urlZoneReg,
1678 DWORD dwFlags)
1680 FIXME("(%p)->(%ld, 0x%lx, %p, %ld, %d, 0x%lx) stub\n", iface, dwZone, dwAction, pPolicy,
1681 cbPolicy, urlZoneReg, dwFlags);
1682 return E_NOTIMPL;
1685 /********************************************************************
1686 * IInternetZoneManagerEx2_GetZoneAttributesEx
1688 static HRESULT WINAPI ZoneMgrImpl_GetZoneAttributesEx(IInternetZoneManagerEx2* iface,
1689 DWORD dwZone,
1690 ZONEATTRIBUTES* pZoneAttributes,
1691 DWORD dwFlags)
1693 TRACE("(%p)->(%ld, %p, 0x%lx)\n", iface, dwZone, pZoneAttributes, dwFlags);
1695 if (dwFlags)
1696 FIXME("dwFlags 0x%lx ignored\n", dwFlags);
1698 return IInternetZoneManagerEx2_GetZoneAttributes(iface, dwZone, pZoneAttributes);
1702 /********************************************************************
1703 * IInternetZoneManagerEx2_GetZoneSecurityState
1705 static HRESULT WINAPI ZoneMgrImpl_GetZoneSecurityState(IInternetZoneManagerEx2* iface,
1706 DWORD dwZoneIndex,
1707 BOOL fRespectPolicy,
1708 LPDWORD pdwState,
1709 BOOL *pfPolicyEncountered)
1711 FIXME("(%p)->(%ld, %d, %p, %p) stub\n", iface, dwZoneIndex, fRespectPolicy,
1712 pdwState, pfPolicyEncountered);
1714 *pdwState = SECURITY_IE_STATE_GREEN;
1716 if (pfPolicyEncountered)
1717 *pfPolicyEncountered = FALSE;
1719 return S_OK;
1722 /********************************************************************
1723 * IInternetZoneManagerEx2_GetIESecurityState
1725 static HRESULT WINAPI ZoneMgrImpl_GetIESecurityState(IInternetZoneManagerEx2* iface,
1726 BOOL fRespectPolicy,
1727 LPDWORD pdwState,
1728 BOOL *pfPolicyEncountered,
1729 BOOL fNoCache)
1731 FIXME("(%p)->(%d, %p, %p, %d) stub\n", iface, fRespectPolicy, pdwState,
1732 pfPolicyEncountered, fNoCache);
1734 *pdwState = SECURITY_IE_STATE_GREEN;
1736 if (pfPolicyEncountered)
1737 *pfPolicyEncountered = FALSE;
1739 return S_OK;
1742 /********************************************************************
1743 * IInternetZoneManagerEx2_FixInsecureSettings
1745 static HRESULT WINAPI ZoneMgrImpl_FixInsecureSettings(IInternetZoneManagerEx2* iface)
1747 FIXME("(%p) stub\n", iface);
1748 return S_OK;
1751 /********************************************************************
1752 * IInternetZoneManager_Construct
1754 static const IInternetZoneManagerEx2Vtbl ZoneMgrImplVtbl = {
1755 ZoneMgrImpl_QueryInterface,
1756 ZoneMgrImpl_AddRef,
1757 ZoneMgrImpl_Release,
1758 /* IInternetZoneManager */
1759 ZoneMgrImpl_GetZoneAttributes,
1760 ZoneMgrImpl_SetZoneAttributes,
1761 ZoneMgrImpl_GetZoneCustomPolicy,
1762 ZoneMgrImpl_SetZoneCustomPolicy,
1763 ZoneMgrImpl_GetZoneActionPolicy,
1764 ZoneMgrImpl_SetZoneActionPolicy,
1765 ZoneMgrImpl_PromptAction,
1766 ZoneMgrImpl_LogAction,
1767 ZoneMgrImpl_CreateZoneEnumerator,
1768 ZoneMgrImpl_GetZoneAt,
1769 ZoneMgrImpl_DestroyZoneEnumerator,
1770 ZoneMgrImpl_CopyTemplatePoliciesToZone,
1771 /* IInternetZoneManagerEx */
1772 ZoneMgrImpl_GetZoneActionPolicyEx,
1773 ZoneMgrImpl_SetZoneActionPolicyEx,
1774 /* IInternetZoneManagerEx2 */
1775 ZoneMgrImpl_GetZoneAttributesEx,
1776 ZoneMgrImpl_GetZoneSecurityState,
1777 ZoneMgrImpl_GetIESecurityState,
1778 ZoneMgrImpl_FixInsecureSettings,
1781 HRESULT ZoneMgrImpl_Construct(IUnknown *pUnkOuter, LPVOID *ppobj)
1783 ZoneMgrImpl *ret = calloc(1, sizeof(ZoneMgrImpl));
1785 TRACE("(%p %p)\n", pUnkOuter, ppobj);
1786 ret->IInternetZoneManagerEx2_iface.lpVtbl = &ZoneMgrImplVtbl;
1787 ret->ref = 1;
1788 *ppobj = &ret->IInternetZoneManagerEx2_iface;
1790 URLMON_LockModule();
1792 return S_OK;
1795 /***********************************************************************
1796 * CoInternetCreateSecurityManager (URLMON.@)
1799 HRESULT WINAPI CoInternetCreateSecurityManager( IServiceProvider *pSP,
1800 IInternetSecurityManager **ppSM, DWORD dwReserved )
1802 TRACE("%p %p %ld\n", pSP, ppSM, dwReserved );
1804 if(pSP)
1805 FIXME("pSP not supported\n");
1807 return SecManagerImpl_Construct(NULL, (void**) ppSM);
1810 /********************************************************************
1811 * CoInternetCreateZoneManager (URLMON.@)
1813 HRESULT WINAPI CoInternetCreateZoneManager(IServiceProvider* pSP, IInternetZoneManager** ppZM, DWORD dwReserved)
1815 TRACE("(%p %p %lx)\n", pSP, ppZM, dwReserved);
1816 return ZoneMgrImpl_Construct(NULL, (void**)ppZM);
1819 static HRESULT parse_security_url(const WCHAR *url, PSUACTION action, WCHAR **result) {
1820 IInternetProtocolInfo *protocol_info;
1821 WCHAR *tmp, *new_url = NULL, *alloc_url = NULL;
1822 DWORD size, new_size;
1823 HRESULT hres = S_OK, parse_hres;
1825 while(1) {
1826 TRACE("parsing %s\n", debugstr_w(url));
1828 protocol_info = get_protocol_info(url);
1829 if(!protocol_info)
1830 break;
1832 size = lstrlenW(url)+1;
1833 new_url = CoTaskMemAlloc(size*sizeof(WCHAR));
1834 if(!new_url) {
1835 hres = E_OUTOFMEMORY;
1836 break;
1839 new_size = 0;
1840 parse_hres = IInternetProtocolInfo_ParseUrl(protocol_info, url, PARSE_SECURITY_URL, 0, new_url, size, &new_size, 0);
1841 if(parse_hres == S_FALSE) {
1842 if(!new_size) {
1843 hres = E_UNEXPECTED;
1844 break;
1847 tmp = CoTaskMemRealloc(new_url, new_size*sizeof(WCHAR));
1848 if(!tmp) {
1849 hres = E_OUTOFMEMORY;
1850 break;
1852 new_url = tmp;
1853 parse_hres = IInternetProtocolInfo_ParseUrl(protocol_info, url, PARSE_SECURITY_URL, 0, new_url,
1854 new_size, &new_size, 0);
1855 if(parse_hres == S_FALSE) {
1856 hres = E_FAIL;
1857 break;
1861 if(parse_hres != S_OK || !wcscmp(url, new_url))
1862 break;
1864 CoTaskMemFree(alloc_url);
1865 url = alloc_url = new_url;
1866 new_url = NULL;
1869 CoTaskMemFree(new_url);
1871 if(hres != S_OK) {
1872 WARN("failed: %08lx\n", hres);
1873 CoTaskMemFree(alloc_url);
1874 return hres;
1877 if(action == PSU_DEFAULT && (protocol_info = get_protocol_info(url))) {
1878 size = lstrlenW(url)+1;
1879 new_url = CoTaskMemAlloc(size * sizeof(WCHAR));
1880 if(new_url) {
1881 new_size = 0;
1882 parse_hres = IInternetProtocolInfo_ParseUrl(protocol_info, url, PARSE_SECURITY_DOMAIN, 0,
1883 new_url, size, &new_size, 0);
1884 if(parse_hres == S_FALSE) {
1885 if(new_size) {
1886 tmp = CoTaskMemRealloc(new_url, new_size*sizeof(WCHAR));
1887 if(tmp) {
1888 new_url = tmp;
1889 parse_hres = IInternetProtocolInfo_ParseUrl(protocol_info, url, PARSE_SECURITY_DOMAIN, 0, new_url,
1890 new_size, &new_size, 0);
1891 if(parse_hres == S_FALSE)
1892 hres = E_FAIL;
1893 }else {
1894 hres = E_OUTOFMEMORY;
1896 }else {
1897 hres = E_UNEXPECTED;
1901 if(hres == S_OK && parse_hres == S_OK) {
1902 CoTaskMemFree(alloc_url);
1903 url = alloc_url = new_url;
1904 new_url = NULL;
1907 CoTaskMemFree(new_url);
1908 }else {
1909 hres = E_OUTOFMEMORY;
1911 IInternetProtocolInfo_Release(protocol_info);
1914 if(FAILED(hres)) {
1915 WARN("failed %08lx\n", hres);
1916 CoTaskMemFree(alloc_url);
1917 return hres;
1920 if(!alloc_url) {
1921 size = lstrlenW(url)+1;
1922 alloc_url = CoTaskMemAlloc(size * sizeof(WCHAR));
1923 if(!alloc_url)
1924 return E_OUTOFMEMORY;
1925 memcpy(alloc_url, url, size * sizeof(WCHAR));
1928 *result = alloc_url;
1929 return S_OK;
1932 /********************************************************************
1933 * CoInternetGetSecurityUrl (URLMON.@)
1935 HRESULT WINAPI CoInternetGetSecurityUrl(LPCWSTR pwzUrl, LPWSTR *ppwzSecUrl, PSUACTION psuAction, DWORD dwReserved)
1937 WCHAR *secure_url;
1938 HRESULT hres;
1940 TRACE("(%p,%p,%u,%lu)\n", pwzUrl, ppwzSecUrl, psuAction, dwReserved);
1942 hres = parse_security_url(pwzUrl, psuAction, &secure_url);
1943 if(FAILED(hres))
1944 return hres;
1946 if(psuAction != PSU_SECURITY_URL_ONLY) {
1947 PARSEDURLW parsed_url = { sizeof(parsed_url) };
1948 DWORD size;
1950 /* FIXME: Use helpers from uri.c */
1951 if(SUCCEEDED(ParseURLW(secure_url, &parsed_url))) {
1952 WCHAR *new_url;
1954 switch(parsed_url.nScheme) {
1955 case URL_SCHEME_FTP:
1956 case URL_SCHEME_HTTP:
1957 case URL_SCHEME_HTTPS:
1958 size = lstrlenW(secure_url)+1;
1959 new_url = CoTaskMemAlloc(size * sizeof(WCHAR));
1960 if(new_url)
1961 hres = UrlGetPartW(secure_url, new_url, &size, URL_PART_HOSTNAME, URL_PARTFLAG_KEEPSCHEME);
1962 else
1963 hres = E_OUTOFMEMORY;
1964 CoTaskMemFree(secure_url);
1965 if(hres != S_OK) {
1966 WARN("UrlGetPart failed: %08lx\n", hres);
1967 CoTaskMemFree(new_url);
1968 return FAILED(hres) ? hres : E_FAIL;
1970 secure_url = new_url;
1975 *ppwzSecUrl = secure_url;
1976 return S_OK;
1979 /********************************************************************
1980 * CoInternetGetSecurityUrlEx (URLMON.@)
1982 HRESULT WINAPI CoInternetGetSecurityUrlEx(IUri *pUri, IUri **ppSecUri, PSUACTION psuAction, DWORD_PTR dwReserved)
1984 URL_SCHEME scheme_type;
1985 BSTR secure_uri;
1986 WCHAR *ret_url;
1987 HRESULT hres;
1989 TRACE("(%p,%p,%u,%Iu)\n", pUri, ppSecUri, psuAction, dwReserved);
1991 if(!pUri || !ppSecUri)
1992 return E_INVALIDARG;
1994 hres = IUri_GetDisplayUri(pUri, &secure_uri);
1995 if(FAILED(hres))
1996 return hres;
1998 hres = parse_security_url(secure_uri, psuAction, &ret_url);
1999 SysFreeString(secure_uri);
2000 if(FAILED(hres))
2001 return hres;
2003 /* File URIs have to hierarchical. */
2004 hres = IUri_GetScheme(pUri, (DWORD*)&scheme_type);
2005 if(SUCCEEDED(hres) && scheme_type == URL_SCHEME_FILE) {
2006 const WCHAR *tmp = ret_url;
2008 /* Check and see if a "//" is after the scheme name. */
2009 tmp += ARRAY_SIZE(L"file");
2010 if(*tmp != '/' || *(tmp+1) != '/')
2011 hres = E_INVALIDARG;
2014 if(SUCCEEDED(hres))
2015 hres = CreateUri(ret_url, Uri_CREATE_ALLOW_IMPLICIT_WILDCARD_SCHEME, 0, ppSecUri);
2016 CoTaskMemFree(ret_url);
2017 return hres;
2020 /********************************************************************
2021 * CompareSecurityIds (URLMON.@)
2023 HRESULT WINAPI CompareSecurityIds(BYTE *secid1, DWORD size1, BYTE *secid2, DWORD size2, DWORD reserved)
2025 FIXME("(%p %ld %p %ld %lx)\n", secid1, size1, secid2, size2, reserved);
2026 return E_NOTIMPL;
2029 /********************************************************************
2030 * IsInternetESCEnabledLocal (URLMON.108)
2032 * Undocumented, returns TRUE if IE is running in Enhanced Security Configuration.
2034 BOOL WINAPI IsInternetESCEnabledLocal(void)
2036 static BOOL esc_initialized, esc_enabled;
2038 TRACE("()\n");
2040 if(!esc_initialized) {
2041 DWORD type, size, val;
2042 HKEY zone_map;
2044 if(RegOpenKeyExW(HKEY_CURRENT_USER, zone_map_keyW, 0, KEY_QUERY_VALUE, &zone_map) == ERROR_SUCCESS) {
2045 size = sizeof(DWORD);
2046 if(RegQueryValueExW(zone_map, L"IEHarden", NULL, &type, (BYTE*)&val, &size) == ERROR_SUCCESS)
2047 esc_enabled = type == REG_DWORD && val != 0;
2048 RegCloseKey(zone_map);
2050 esc_initialized = TRUE;
2053 return esc_enabled;