[URLMON]
[reactos.git] / reactos / dll / win32 / urlmon / sec_mgr.c
1 /*
2 * Internet Security and Zone Manager
3 *
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
8 *
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.
13 *
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.
18 *
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
22 */
23
24 #include "urlmon_main.h"
25
26 static const WCHAR currentlevelW[] = {'C','u','r','r','e','n','t','L','e','v','e','l',0};
27 static const WCHAR descriptionW[] = {'D','e','s','c','r','i','p','t','i','o','n',0};
28 static const WCHAR displaynameW[] = {'D','i','s','p','l','a','y','N','a','m','e',0};
29 static const WCHAR fileW[] = {'f','i','l','e',0};
30 static const WCHAR flagsW[] = {'F','l','a','g','s',0};
31 static const WCHAR iconW[] = {'I','c','o','n',0};
32 static const WCHAR minlevelW[] = {'M','i','n','L','e','v','e','l',0};
33 static const WCHAR recommendedlevelW[] = {'R','e','c','o','m','m','e','n','d','e','d',
34 'L','e','v','e','l',0};
35 static const WCHAR wszZonesKey[] = {'S','o','f','t','w','a','r','e','\\',
36 'M','i','c','r','o','s','o','f','t','\\',
37 'W','i','n','d','o','w','s','\\',
38 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
39 'I','n','t','e','r','n','e','t',' ','S','e','t','t','i','n','g','s','\\',
40 'Z','o','n','e','s','\\',0};
41 static const WCHAR wszZoneMapDomainsKey[] = {'S','o','f','t','w','a','r','e','\\',
42 'M','i','c','r','o','s','o','f','t','\\',
43 'W','i','n','d','o','w','s','\\',
44 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
45 'I','n','t','e','r','n','e','t',' ','S','e','t','t','i','n','g','s','\\',
46 'Z','o','n','e','M','a','p','\\',
47 'D','o','m','a','i','n','s',0};
48
49 static inline BOOL is_drive_path(const WCHAR *path)
50 {
51 return isalphaW(*path) && *(path+1) == ':';
52 }
53
54 /* List of schemes types Windows seems to expect to be hierarchical. */
55 static inline BOOL is_hierarchical_scheme(URL_SCHEME type) {
56 return(type == URL_SCHEME_HTTP || type == URL_SCHEME_FTP ||
57 type == URL_SCHEME_GOPHER || type == URL_SCHEME_NNTP ||
58 type == URL_SCHEME_TELNET || type == URL_SCHEME_WAIS ||
59 type == URL_SCHEME_FILE || type == URL_SCHEME_HTTPS ||
60 type == URL_SCHEME_RES);
61 }
62
63 /********************************************************************
64 * get_string_from_reg [internal]
65 *
66 * helper to get a string from the reg.
67 *
68 */
69 static void get_string_from_reg(HKEY hcu, HKEY hklm, LPCWSTR name, LPWSTR out, DWORD maxlen)
70 {
71 DWORD type = REG_SZ;
72 DWORD len = maxlen * sizeof(WCHAR);
73 DWORD res;
74
75 res = RegQueryValueExW(hcu, name, NULL, &type, (LPBYTE) out, &len);
76
77 if (res && hklm) {
78 len = maxlen * sizeof(WCHAR);
79 type = REG_SZ;
80 res = RegQueryValueExW(hklm, name, NULL, &type, (LPBYTE) out, &len);
81 }
82
83 if (res) {
84 TRACE("%s failed: %d\n", debugstr_w(name), res);
85 *out = '\0';
86 }
87 }
88
89 /********************************************************************
90 * get_dword_from_reg [internal]
91 *
92 * helper to get a dword from the reg.
93 *
94 */
95 static void get_dword_from_reg(HKEY hcu, HKEY hklm, LPCWSTR name, LPDWORD out)
96 {
97 DWORD type = REG_DWORD;
98 DWORD len = sizeof(DWORD);
99 DWORD res;
100
101 res = RegQueryValueExW(hcu, name, NULL, &type, (LPBYTE) out, &len);
102
103 if (res && hklm) {
104 len = sizeof(DWORD);
105 type = REG_DWORD;
106 res = RegQueryValueExW(hklm, name, NULL, &type, (LPBYTE) out, &len);
107 }
108
109 if (res) {
110 TRACE("%s failed: %d\n", debugstr_w(name), res);
111 *out = 0;
112 }
113 }
114
115 static HRESULT get_zone_from_reg(LPCWSTR schema, DWORD *zone)
116 {
117 DWORD res, size;
118 HKEY hkey;
119
120 static const WCHAR wszZoneMapProtocolKey[] =
121 {'S','o','f','t','w','a','r','e','\\',
122 'M','i','c','r','o','s','o','f','t','\\',
123 'W','i','n','d','o','w','s','\\',
124 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
125 'I','n','t','e','r','n','e','t',' ','S','e','t','t','i','n','g','s','\\',
126 'Z','o','n','e','M','a','p','\\',
127 'P','r','o','t','o','c','o','l','D','e','f','a','u','l','t','s',0};
128
129 res = RegOpenKeyW(HKEY_CURRENT_USER, wszZoneMapProtocolKey, &hkey);
130 if(res != ERROR_SUCCESS) {
131 ERR("Could not open key %s\n", debugstr_w(wszZoneMapProtocolKey));
132 return E_UNEXPECTED;
133 }
134
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;
140
141 res = RegOpenKeyW(HKEY_LOCAL_MACHINE, wszZoneMapProtocolKey, &hkey);
142 if(res != ERROR_SUCCESS) {
143 ERR("Could not open key %s\n", debugstr_w(wszZoneMapProtocolKey));
144 return E_UNEXPECTED;
145 }
146
147 size = sizeof(DWORD);
148 res = RegQueryValueExW(hkey, schema, NULL, NULL, (PBYTE)zone, &size);
149 RegCloseKey(hkey);
150 if(res == ERROR_SUCCESS)
151 return S_OK;
152
153 *zone = 3;
154 return S_OK;
155 }
156
157 /********************************************************************
158 * matches_domain_pattern [internal]
159 *
160 * Checks if the given string matches the specified domain pattern.
161 *
162 * This function looks for explicit wildcard domain components iff
163 * they appear at the very beginning of the 'pattern' string
164 *
165 * pattern = "*.google.com"
166 */
167 static BOOL matches_domain_pattern(LPCWSTR pattern, LPCWSTR str, BOOL implicit_wildcard, LPCWSTR *matched)
168 {
169 BOOL matches = FALSE;
170 DWORD pattern_len = strlenW(pattern);
171 DWORD str_len = strlenW(str);
172
173 TRACE("(%d) Checking if %s matches %s\n", implicit_wildcard, debugstr_w(str), debugstr_w(pattern));
174
175 *matched = NULL;
176 if(str_len >= pattern_len) {
177 /* Check if there's an explicit wildcard in the pattern. */
178 if(pattern[0] == '*' && pattern[1] == '.') {
179 /* Make sure that 'str' matches the wildcard pattern.
180 *
181 * Example:
182 * pattern = "*.google.com"
183 *
184 * So in this case 'str' would have to end with ".google.com" in order
185 * to map to this pattern.
186 */
187 if(str_len >= pattern_len+1 && !strcmpiW(str+(str_len-pattern_len+1), pattern+1)) {
188 /* Check if there's another '.' inside of the "unmatched" portion
189 * of 'str'.
190 *
191 * Example:
192 * pattern = "*.google.com"
193 * str = "test.testing.google.com"
194 *
195 * The currently matched portion is ".google.com" in 'str', we need
196 * see if there's a '.' inside of the unmatched portion ("test.testing"), because
197 * if there is and 'implicit_wildcard' isn't set, then this isn't
198 * a match.
199 */
200 const WCHAR *ptr;
201 if(str_len > pattern_len+1 && (ptr = memrchrW(str, '.', str_len-pattern_len-2))) {
202 if(implicit_wildcard) {
203 matches = TRUE;
204 *matched = ptr+1;
205 }
206 } else {
207 matches = TRUE;
208 *matched = str;
209 }
210 }
211 } else if(implicit_wildcard && str_len > pattern_len) {
212 /* When the pattern has an implicit wildcard component, it means
213 * that anything goes in 'str' as long as it ends with the pattern
214 * and that the beginning of the match has a '.' before it.
215 *
216 * Example:
217 * pattern = "google.com"
218 * str = "www.google.com"
219 *
220 * Implicitly matches the pattern, where as:
221 *
222 * pattern = "google.com"
223 * str = "wwwgoogle.com"
224 *
225 * Doesn't match the pattern.
226 */
227 if(str_len > pattern_len) {
228 if(str[str_len-pattern_len-1] == '.' && !strcmpiW(str+(str_len-pattern_len), pattern)) {
229 matches = TRUE;
230 *matched = str+(str_len-pattern_len);
231 }
232 }
233 } else {
234 /* The pattern doesn't have an implicit wildcard, or an explicit wildcard,
235 * so 'str' has to be an exact match to the 'pattern'.
236 */
237 if(!strcmpiW(str, pattern)) {
238 matches = TRUE;
239 *matched = str;
240 }
241 }
242 }
243
244 if(matches)
245 TRACE("Found a match: matched=%s\n", debugstr_w(*matched));
246 else
247 TRACE("No match found\n");
248
249 return matches;
250 }
251
252 static BOOL get_zone_for_scheme(HKEY key, LPCWSTR schema, DWORD *zone)
253 {
254 static const WCHAR wildcardW[] = {'*',0};
255
256 DWORD res;
257 DWORD size = sizeof(DWORD);
258 DWORD type;
259
260 /* See if the key contains a value for the scheme first. */
261 res = RegQueryValueExW(key, schema, NULL, &type, (BYTE*)zone, &size);
262 if(res == ERROR_SUCCESS) {
263 if(type == REG_DWORD)
264 return TRUE;
265 WARN("Unexpected value type %d for value %s, expected REG_DWORD\n", type, debugstr_w(schema));
266 }
267
268 /* Try to get the zone for the wildcard scheme. */
269 size = sizeof(DWORD);
270 res = RegQueryValueExW(key, wildcardW, NULL, &type, (BYTE*)zone, &size);
271 if(res != ERROR_SUCCESS)
272 return FALSE;
273
274 if(type != REG_DWORD) {
275 WARN("Unexpected value type %d for value %s, expected REG_DWORD\n", type, debugstr_w(wildcardW));
276 return FALSE;
277 }
278
279 return TRUE;
280 }
281
282 /********************************************************************
283 * search_domain_for_zone [internal]
284 *
285 * Searches the specified 'domain' registry key to see if 'host' maps into it, or any
286 * of it's subdomain registry keys.
287 *
288 * Returns S_OK if a match is found, S_FALSE if no matches were found, or an error code.
289 */
290 static HRESULT search_domain_for_zone(HKEY domains, LPCWSTR domain, DWORD domain_len, LPCWSTR schema,
291 LPCWSTR host, DWORD host_len, DWORD *zone)
292 {
293 BOOL found = FALSE;
294 HKEY domain_key;
295 DWORD res;
296 LPCWSTR matched;
297
298 if(host_len >= domain_len && matches_domain_pattern(domain, host, TRUE, &matched)) {
299 res = RegOpenKeyW(domains, domain, &domain_key);
300 if(res != ERROR_SUCCESS) {
301 ERR("Failed to open domain key %s: %d\n", debugstr_w(domain), res);
302 return E_UNEXPECTED;
303 }
304
305 if(matched == host)
306 found = get_zone_for_scheme(domain_key, schema, zone);
307 else {
308 INT domain_offset;
309 DWORD subdomain_count, subdomain_len;
310 BOOL check_domain = TRUE;
311
312 find_domain_name(domain, domain_len, &domain_offset);
313
314 res = RegQueryInfoKeyW(domain_key, NULL, NULL, NULL, &subdomain_count, &subdomain_len,
315 NULL, NULL, NULL, NULL, NULL, NULL);
316 if(res != ERROR_SUCCESS) {
317 ERR("Unable to query info for key %s: %d\n", debugstr_w(domain), res);
318 RegCloseKey(domain_key);
319 return E_UNEXPECTED;
320 }
321
322 if(subdomain_count) {
323 WCHAR *subdomain;
324 WCHAR *component;
325 DWORD i;
326
327 subdomain = heap_alloc((subdomain_len+1)*sizeof(WCHAR));
328 if(!subdomain) {
329 RegCloseKey(domain_key);
330 return E_OUTOFMEMORY;
331 }
332
333 component = heap_strndupW(host, matched-host-1);
334 if(!component) {
335 heap_free(subdomain);
336 RegCloseKey(domain_key);
337 return E_OUTOFMEMORY;
338 }
339
340 for(i = 0; i < subdomain_count; ++i) {
341 DWORD len = subdomain_len+1;
342 const WCHAR *sub_matched;
343
344 res = RegEnumKeyExW(domain_key, i, subdomain, &len, NULL, NULL, NULL, NULL);
345 if(res != ERROR_SUCCESS) {
346 heap_free(component);
347 heap_free(subdomain);
348 RegCloseKey(domain_key);
349 return E_UNEXPECTED;
350 }
351
352 if(matches_domain_pattern(subdomain, component, FALSE, &sub_matched)) {
353 HKEY subdomain_key;
354
355 res = RegOpenKeyW(domain_key, subdomain, &subdomain_key);
356 if(res != ERROR_SUCCESS) {
357 ERR("Unable to open subdomain key %s of %s: %d\n", debugstr_w(subdomain),
358 debugstr_w(domain), res);
359 heap_free(component);
360 heap_free(subdomain);
361 RegCloseKey(domain_key);
362 return E_UNEXPECTED;
363 }
364
365 found = get_zone_for_scheme(subdomain_key, schema, zone);
366 check_domain = FALSE;
367 RegCloseKey(subdomain_key);
368 break;
369 }
370 }
371 heap_free(subdomain);
372 heap_free(component);
373 }
374
375 /* There's a chance that 'host' implicitly mapped into 'domain', in
376 * which case we check to see if 'domain' contains zone information.
377 *
378 * This can only happen if 'domain' is it's own domain name.
379 * Example:
380 * "google.com" (domain name = "google.com")
381 *
382 * So if:
383 * host = "www.google.com"
384 *
385 * Then host would map directly into the "google.com" domain key.
386 *
387 * If 'domain' has more than just it's domain name, or it does not
388 * have a domain name, then we don't perform the check. The reason
389 * for this is that these domains don't allow implicit mappings.
390 * Example:
391 * domain = "org" (has no domain name)
392 * host = "www.org"
393 *
394 * The mapping would only happen if the "org" key had an explicit subkey
395 * called "www".
396 */
397 if(check_domain && !domain_offset && !strchrW(host, matched-host-1))
398 found = get_zone_for_scheme(domain_key, schema, zone);
399 }
400 RegCloseKey(domain_key);
401 }
402
403 return found ? S_OK : S_FALSE;
404 }
405
406 static HRESULT search_for_domain_mapping(HKEY domains, LPCWSTR schema, LPCWSTR host, DWORD host_len, DWORD *zone)
407 {
408 WCHAR *domain;
409 DWORD domain_count, domain_len, i;
410 DWORD res;
411 HRESULT hres = S_FALSE;
412
413 res = RegQueryInfoKeyW(domains, NULL, NULL, NULL, &domain_count, &domain_len,
414 NULL, NULL, NULL, NULL, NULL, NULL);
415 if(res != ERROR_SUCCESS) {
416 WARN("Failed to retrieve information about key\n");
417 return E_UNEXPECTED;
418 }
419
420 if(!domain_count)
421 return S_FALSE;
422
423 domain = heap_alloc((domain_len+1)*sizeof(WCHAR));
424 if(!domain)
425 return E_OUTOFMEMORY;
426
427 for(i = 0; i < domain_count; ++i) {
428 DWORD len = domain_len+1;
429
430 res = RegEnumKeyExW(domains, i, domain, &len, NULL, NULL, NULL, NULL);
431 if(res != ERROR_SUCCESS) {
432 heap_free(domain);
433 return E_UNEXPECTED;
434 }
435
436 hres = search_domain_for_zone(domains, domain, len, schema, host, host_len, zone);
437 if(FAILED(hres) || hres == S_OK)
438 break;
439 }
440
441 heap_free(domain);
442 return hres;
443 }
444
445 static HRESULT get_zone_from_domains(IUri *uri, DWORD *zone)
446 {
447 HRESULT hres;
448 BSTR host, scheme;
449 DWORD res;
450 HKEY domains;
451 DWORD scheme_type;
452
453 hres = IUri_GetScheme(uri, &scheme_type);
454 if(FAILED(hres))
455 return hres;
456
457 /* Windows doesn't play nice with unknown scheme types when it tries
458 * to check if a host name maps into any domains.
459 */
460 if(scheme_type == URL_SCHEME_UNKNOWN)
461 return S_FALSE;
462
463 hres = IUri_GetHost(uri, &host);
464 if(FAILED(hres))
465 return hres;
466
467 /* Known hierarchical scheme types must have a host. If they don't Windows
468 * assigns URLZONE_INVALID to the zone.
469 */
470 if((scheme_type != URL_SCHEME_UNKNOWN && scheme_type != URL_SCHEME_FILE)
471 && is_hierarchical_scheme(scheme_type) && !*host) {
472 *zone = URLZONE_INVALID;
473
474 SysFreeString(host);
475
476 /* The MapUrlToZone functions return S_OK when this condition occurs. */
477 return S_OK;
478 }
479
480 hres = IUri_GetSchemeName(uri, &scheme);
481 if(FAILED(hres)) {
482 SysFreeString(host);
483 return hres;
484 }
485
486 /* First try CURRENT_USER. */
487 res = RegOpenKeyW(HKEY_CURRENT_USER, wszZoneMapDomainsKey, &domains);
488 if(res == ERROR_SUCCESS) {
489 hres = search_for_domain_mapping(domains, scheme, host, SysStringLen(host), zone);
490 RegCloseKey(domains);
491 } else
492 WARN("Failed to open HKCU's %s key\n", debugstr_w(wszZoneMapDomainsKey));
493
494 /* If that doesn't work try LOCAL_MACHINE. */
495 if(hres == S_FALSE) {
496 res = RegOpenKeyW(HKEY_LOCAL_MACHINE, wszZoneMapDomainsKey, &domains);
497 if(res == ERROR_SUCCESS) {
498 hres = search_for_domain_mapping(domains, scheme, host, SysStringLen(host), zone);
499 RegCloseKey(domains);
500 } else
501 WARN("Failed to open HKLM's %s key\n", debugstr_w(wszZoneMapDomainsKey));
502 }
503
504 SysFreeString(host);
505 SysFreeString(scheme);
506 return hres;
507 }
508
509 static HRESULT map_security_uri_to_zone(IUri *uri, DWORD *zone)
510 {
511 HRESULT hres;
512 BSTR scheme;
513
514 *zone = URLZONE_INVALID;
515
516 hres = IUri_GetSchemeName(uri, &scheme);
517 if(FAILED(hres))
518 return hres;
519
520 if(!strcmpiW(scheme, fileW)) {
521 BSTR path;
522 WCHAR *ptr, *path_start, root[20];
523
524 hres = IUri_GetPath(uri, &path);
525 if(FAILED(hres)) {
526 SysFreeString(scheme);
527 return hres;
528 }
529
530 if(*path == '/' && is_drive_path(path+1))
531 path_start = path+1;
532 else
533 path_start = path;
534
535 if((ptr = strchrW(path_start, ':')) && ptr-path_start+1 < sizeof(root)/sizeof(WCHAR)) {
536 UINT type;
537
538 memcpy(root, path_start, (ptr-path_start+1)*sizeof(WCHAR));
539 root[ptr-path_start+1] = 0;
540
541 type = GetDriveTypeW(root);
542
543 switch(type) {
544 case DRIVE_UNKNOWN:
545 case DRIVE_NO_ROOT_DIR:
546 break;
547 case DRIVE_REMOVABLE:
548 case DRIVE_FIXED:
549 case DRIVE_CDROM:
550 case DRIVE_RAMDISK:
551 *zone = URLZONE_LOCAL_MACHINE;
552 hres = S_OK;
553 break;
554 case DRIVE_REMOTE:
555 *zone = URLZONE_INTERNET;
556 hres = S_OK;
557 break;
558 default:
559 FIXME("unsupported drive type %d\n", type);
560 }
561 }
562 SysFreeString(path);
563 }
564
565 if(*zone == URLZONE_INVALID) {
566 hres = get_zone_from_domains(uri, zone);
567 if(hres == S_FALSE)
568 hres = get_zone_from_reg(scheme, zone);
569 }
570
571 SysFreeString(scheme);
572 return hres;
573 }
574
575 static HRESULT map_url_to_zone(LPCWSTR url, DWORD *zone, LPWSTR *ret_url)
576 {
577 IUri *secur_uri;
578 LPWSTR secur_url;
579 HRESULT hres;
580
581 *zone = URLZONE_INVALID;
582
583 hres = CoInternetGetSecurityUrl(url, &secur_url, PSU_SECURITY_URL_ONLY, 0);
584 if(hres != S_OK) {
585 DWORD size = strlenW(url)*sizeof(WCHAR);
586
587 secur_url = CoTaskMemAlloc(size);
588 if(!secur_url)
589 return E_OUTOFMEMORY;
590
591 memcpy(secur_url, url, size);
592 }
593
594 hres = CreateUri(secur_url, Uri_CREATE_ALLOW_IMPLICIT_FILE_SCHEME, 0, &secur_uri);
595 if(FAILED(hres)) {
596 CoTaskMemFree(secur_url);
597 return hres;
598 }
599
600 hres = map_security_uri_to_zone(secur_uri, zone);
601 IUri_Release(secur_uri);
602
603 if(FAILED(hres) || !ret_url)
604 CoTaskMemFree(secur_url);
605 else
606 *ret_url = secur_url;
607
608 return hres;
609 }
610
611 static HRESULT map_uri_to_zone(IUri *uri, DWORD *zone, IUri **ret_uri)
612 {
613 HRESULT hres;
614 IUri *secur_uri;
615
616 hres = CoInternetGetSecurityUrlEx(uri, &secur_uri, PSU_SECURITY_URL_ONLY, 0);
617 if(FAILED(hres))
618 return hres;
619
620 hres = map_security_uri_to_zone(secur_uri, zone);
621 if(FAILED(hres) || !ret_uri)
622 IUri_Release(secur_uri);
623 else
624 *ret_uri = secur_uri;
625
626 return hres;
627 }
628
629 static HRESULT open_zone_key(HKEY parent_key, DWORD zone, HKEY *hkey)
630 {
631 static const WCHAR wszFormat[] = {'%','s','%','u',0};
632
633 WCHAR key_name[sizeof(wszZonesKey)/sizeof(WCHAR)+12];
634 DWORD res;
635
636 wsprintfW(key_name, wszFormat, wszZonesKey, zone);
637
638 res = RegOpenKeyW(parent_key, key_name, hkey);
639
640 if(res != ERROR_SUCCESS) {
641 WARN("RegOpenKey failed\n");
642 return E_INVALIDARG;
643 }
644
645 return S_OK;
646 }
647
648 static HRESULT get_action_policy(DWORD zone, DWORD action, BYTE *policy, DWORD size, URLZONEREG zone_reg)
649 {
650 HKEY parent_key;
651 HKEY hkey;
652 LONG res;
653 HRESULT hres;
654
655 switch(action) {
656 case URLACTION_SCRIPT_OVERRIDE_SAFETY:
657 case URLACTION_ACTIVEX_OVERRIDE_SCRIPT_SAFETY:
658 *(DWORD*)policy = URLPOLICY_DISALLOW;
659 return S_OK;
660 }
661
662 switch(zone_reg) {
663 case URLZONEREG_DEFAULT:
664 case URLZONEREG_HKCU:
665 parent_key = HKEY_CURRENT_USER;
666 break;
667 case URLZONEREG_HKLM:
668 parent_key = HKEY_LOCAL_MACHINE;
669 break;
670 default:
671 WARN("Unknown URLZONEREG: %d\n", zone_reg);
672 return E_FAIL;
673 };
674
675 hres = open_zone_key(parent_key, zone, &hkey);
676 if(SUCCEEDED(hres)) {
677 WCHAR action_str[16];
678 DWORD len = size;
679
680 static const WCHAR formatW[] = {'%','X',0};
681
682 wsprintfW(action_str, formatW, action);
683
684 res = RegQueryValueExW(hkey, action_str, NULL, NULL, policy, &len);
685 if(res == ERROR_MORE_DATA) {
686 hres = E_INVALIDARG;
687 }else if(res == ERROR_FILE_NOT_FOUND) {
688 hres = E_FAIL;
689 }else if(res != ERROR_SUCCESS) {
690 ERR("RegQueryValue failed: %d\n", res);
691 hres = E_UNEXPECTED;
692 }
693
694 RegCloseKey(hkey);
695 }
696
697 if(FAILED(hres) && zone_reg == URLZONEREG_DEFAULT)
698 return get_action_policy(zone, action, policy, size, URLZONEREG_HKLM);
699
700 return hres;
701 }
702
703 static HRESULT generate_security_id(IUri *uri, BYTE *secid, DWORD *secid_len, DWORD zone)
704 {
705 DWORD len;
706 HRESULT hres;
707 DWORD scheme_type;
708
709 if(zone == URLZONE_INVALID)
710 return E_INVALIDARG;
711
712 hres = IUri_GetScheme(uri, &scheme_type);
713 if(FAILED(hres))
714 return hres;
715
716 /* Windows handles opaque URLs differently then hierarchical ones. */
717 if(!is_hierarchical_scheme(scheme_type) && scheme_type != URL_SCHEME_WILDCARD) {
718 BSTR display_uri;
719
720 hres = IUri_GetDisplayUri(uri, &display_uri);
721 if(FAILED(hres))
722 return hres;
723
724 len = WideCharToMultiByte(CP_ACP, 0, display_uri, -1, NULL, 0, NULL, NULL)-1;
725
726 if(len+sizeof(DWORD) > *secid_len) {
727 SysFreeString(display_uri);
728 return HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
729 }
730
731 WideCharToMultiByte(CP_ACP, 0, display_uri, -1, (LPSTR)secid, len, NULL, NULL);
732 SysFreeString(display_uri);
733
734 *(DWORD*)(secid+len) = zone;
735 } else {
736 BSTR host, scheme;
737 DWORD host_len, scheme_len;
738 BYTE *ptr;
739
740 hres = IUri_GetHost(uri, &host);
741 if(FAILED(hres))
742 return hres;
743
744 /* The host can't be empty for Wildcard URIs. */
745 if(scheme_type == URL_SCHEME_WILDCARD && !*host) {
746 SysFreeString(host);
747 return E_INVALIDARG;
748 }
749
750 hres = IUri_GetSchemeName(uri, &scheme);
751 if(FAILED(hres)) {
752 SysFreeString(host);
753 return hres;
754 }
755
756 host_len = WideCharToMultiByte(CP_ACP, 0, host, -1, NULL, 0, NULL, NULL)-1;
757 scheme_len = WideCharToMultiByte(CP_ACP, 0, scheme, -1, NULL, 0, NULL, NULL)-1;
758
759 len = host_len+scheme_len+sizeof(BYTE);
760
761 if(len+sizeof(DWORD) > *secid_len) {
762 SysFreeString(host);
763 SysFreeString(scheme);
764 return HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
765 }
766
767 WideCharToMultiByte(CP_ACP, 0, scheme, -1, (LPSTR)secid, len, NULL, NULL);
768 SysFreeString(scheme);
769
770 ptr = secid+scheme_len;
771 *ptr++ = ':';
772
773 WideCharToMultiByte(CP_ACP, 0, host, -1, (LPSTR)ptr, host_len, NULL, NULL);
774 SysFreeString(host);
775
776 ptr += host_len;
777
778 *(DWORD*)ptr = zone;
779 }
780
781 *secid_len = len+sizeof(DWORD);
782
783 return S_OK;
784 }
785
786 static HRESULT get_security_id_for_url(LPCWSTR url, BYTE *secid, DWORD *secid_len)
787 {
788 HRESULT hres;
789 DWORD zone = URLZONE_INVALID;
790 LPWSTR secur_url = NULL;
791 IUri *uri;
792
793 hres = map_url_to_zone(url, &zone, &secur_url);
794 if(FAILED(hres))
795 return hres == 0x80041001 ? E_INVALIDARG : hres;
796
797 hres = CreateUri(secur_url, Uri_CREATE_ALLOW_IMPLICIT_FILE_SCHEME, 0, &uri);
798 CoTaskMemFree(secur_url);
799 if(FAILED(hres))
800 return hres;
801
802 hres = generate_security_id(uri, secid, secid_len, zone);
803 IUri_Release(uri);
804
805 return hres;
806 }
807
808 static HRESULT get_security_id_for_uri(IUri *uri, BYTE *secid, DWORD *secid_len)
809 {
810 HRESULT hres;
811 IUri *secur_uri;
812 DWORD zone = URLZONE_INVALID;
813
814 hres = map_uri_to_zone(uri, &zone, &secur_uri);
815 if(FAILED(hres))
816 return hres;
817
818 hres = generate_security_id(secur_uri, secid, secid_len, zone);
819 IUri_Release(secur_uri);
820
821 return hres;
822 }
823
824 /***********************************************************************
825 * InternetSecurityManager implementation
826 *
827 */
828 typedef struct {
829 IInternetSecurityManagerEx2 IInternetSecurityManagerEx2_iface;
830
831 LONG ref;
832
833 IInternetSecurityMgrSite *mgrsite;
834 IInternetSecurityManager *custom_manager;
835 } SecManagerImpl;
836
837 static inline SecManagerImpl *impl_from_IInternetSecurityManagerEx2(IInternetSecurityManagerEx2 *iface)
838 {
839 return CONTAINING_RECORD(iface, SecManagerImpl, IInternetSecurityManagerEx2_iface);
840 }
841
842 static HRESULT WINAPI SecManagerImpl_QueryInterface(IInternetSecurityManagerEx2* iface,REFIID riid,void** ppvObject)
843 {
844 SecManagerImpl *This = impl_from_IInternetSecurityManagerEx2(iface);
845
846 TRACE("(%p)->(%s %p)\n",This,debugstr_guid(riid),ppvObject);
847
848 if(!ppvObject)
849 return E_INVALIDARG;
850
851 if(IsEqualIID(&IID_IUnknown, riid) ||
852 IsEqualIID(&IID_IInternetSecurityManager, riid) ||
853 IsEqualIID(&IID_IInternetSecurityManagerEx, riid) ||
854 IsEqualIID(&IID_IInternetSecurityManagerEx2, riid)) {
855 *ppvObject = iface;
856 } else {
857 WARN("not supported interface %s\n", debugstr_guid(riid));
858 *ppvObject = NULL;
859 return E_NOINTERFACE;
860 }
861
862 IInternetSecurityManagerEx2_AddRef(iface);
863 return S_OK;
864 }
865
866 static ULONG WINAPI SecManagerImpl_AddRef(IInternetSecurityManagerEx2* iface)
867 {
868 SecManagerImpl *This = impl_from_IInternetSecurityManagerEx2(iface);
869 ULONG refCount = InterlockedIncrement(&This->ref);
870
871 TRACE("(%p) ref=%u\n", This, refCount);
872
873 return refCount;
874 }
875
876 static ULONG WINAPI SecManagerImpl_Release(IInternetSecurityManagerEx2* iface)
877 {
878 SecManagerImpl *This = impl_from_IInternetSecurityManagerEx2(iface);
879 ULONG refCount = InterlockedDecrement(&This->ref);
880
881 TRACE("(%p) ref=%u\n", This, refCount);
882
883 /* destroy the object if there's no more reference on it */
884 if (!refCount){
885 if(This->mgrsite)
886 IInternetSecurityMgrSite_Release(This->mgrsite);
887 if(This->custom_manager)
888 IInternetSecurityManager_Release(This->custom_manager);
889
890 heap_free(This);
891
892 URLMON_UnlockModule();
893 }
894
895 return refCount;
896 }
897
898 static HRESULT WINAPI SecManagerImpl_SetSecuritySite(IInternetSecurityManagerEx2 *iface,
899 IInternetSecurityMgrSite *pSite)
900 {
901 SecManagerImpl *This = impl_from_IInternetSecurityManagerEx2(iface);
902
903 TRACE("(%p)->(%p)\n", This, pSite);
904
905 if(This->mgrsite)
906 IInternetSecurityMgrSite_Release(This->mgrsite);
907
908 if(This->custom_manager) {
909 IInternetSecurityManager_Release(This->custom_manager);
910 This->custom_manager = NULL;
911 }
912
913 This->mgrsite = pSite;
914
915 if(pSite) {
916 IServiceProvider *servprov;
917 HRESULT hres;
918
919 IInternetSecurityMgrSite_AddRef(pSite);
920
921 hres = IInternetSecurityMgrSite_QueryInterface(pSite, &IID_IServiceProvider,
922 (void**)&servprov);
923 if(SUCCEEDED(hres)) {
924 IServiceProvider_QueryService(servprov, &SID_SInternetSecurityManager,
925 &IID_IInternetSecurityManager, (void**)&This->custom_manager);
926 IServiceProvider_Release(servprov);
927 }
928 }
929
930 return S_OK;
931 }
932
933 static HRESULT WINAPI SecManagerImpl_GetSecuritySite(IInternetSecurityManagerEx2 *iface,
934 IInternetSecurityMgrSite **ppSite)
935 {
936 SecManagerImpl *This = impl_from_IInternetSecurityManagerEx2(iface);
937
938 TRACE("(%p)->(%p)\n", This, ppSite);
939
940 if(!ppSite)
941 return E_INVALIDARG;
942
943 if(This->mgrsite)
944 IInternetSecurityMgrSite_AddRef(This->mgrsite);
945
946 *ppSite = This->mgrsite;
947 return S_OK;
948 }
949
950 static HRESULT WINAPI SecManagerImpl_MapUrlToZone(IInternetSecurityManagerEx2 *iface,
951 LPCWSTR pwszUrl, DWORD *pdwZone,
952 DWORD dwFlags)
953 {
954 SecManagerImpl *This = impl_from_IInternetSecurityManagerEx2(iface);
955 HRESULT hres;
956
957 TRACE("(%p)->(%s %p %08x)\n", iface, debugstr_w(pwszUrl), pdwZone, dwFlags);
958
959 if(This->custom_manager) {
960 hres = IInternetSecurityManager_MapUrlToZone(This->custom_manager,
961 pwszUrl, pdwZone, dwFlags);
962 if(hres != INET_E_DEFAULT_ACTION)
963 return hres;
964 }
965
966 if(!pwszUrl) {
967 *pdwZone = URLZONE_INVALID;
968 return E_INVALIDARG;
969 }
970
971 if(dwFlags)
972 FIXME("not supported flags: %08x\n", dwFlags);
973
974 return map_url_to_zone(pwszUrl, pdwZone, NULL);
975 }
976
977 static HRESULT WINAPI SecManagerImpl_GetSecurityId(IInternetSecurityManagerEx2 *iface,
978 LPCWSTR pwszUrl, BYTE *pbSecurityId, DWORD *pcbSecurityId, DWORD_PTR dwReserved)
979 {
980 SecManagerImpl *This = impl_from_IInternetSecurityManagerEx2(iface);
981
982 TRACE("(%p)->(%s %p %p %08lx)\n", iface, debugstr_w(pwszUrl), pbSecurityId,
983 pcbSecurityId, dwReserved);
984
985 if(This->custom_manager) {
986 HRESULT hres;
987
988 hres = IInternetSecurityManager_GetSecurityId(This->custom_manager,
989 pwszUrl, pbSecurityId, pcbSecurityId, dwReserved);
990 if(hres != INET_E_DEFAULT_ACTION)
991 return hres;
992 }
993
994 if(!pwszUrl || !pbSecurityId || !pcbSecurityId)
995 return E_INVALIDARG;
996
997 if(dwReserved)
998 FIXME("dwReserved is not supported\n");
999
1000 return get_security_id_for_url(pwszUrl, pbSecurityId, pcbSecurityId);
1001 }
1002
1003
1004 static HRESULT WINAPI SecManagerImpl_ProcessUrlAction(IInternetSecurityManagerEx2 *iface,
1005 LPCWSTR pwszUrl, DWORD dwAction,
1006 BYTE *pPolicy, DWORD cbPolicy,
1007 BYTE *pContext, DWORD cbContext,
1008 DWORD dwFlags, DWORD dwReserved)
1009 {
1010 SecManagerImpl *This = impl_from_IInternetSecurityManagerEx2(iface);
1011 DWORD zone, policy;
1012 HRESULT hres;
1013
1014 TRACE("(%p)->(%s %08x %p %08x %p %08x %08x %08x)\n", iface, debugstr_w(pwszUrl), dwAction,
1015 pPolicy, cbPolicy, pContext, cbContext, dwFlags, dwReserved);
1016
1017 if(This->custom_manager) {
1018 hres = IInternetSecurityManager_ProcessUrlAction(This->custom_manager, pwszUrl, dwAction,
1019 pPolicy, cbPolicy, pContext, cbContext, dwFlags, dwReserved);
1020 if(hres != INET_E_DEFAULT_ACTION)
1021 return hres;
1022 }
1023
1024 if(dwFlags || dwReserved)
1025 FIXME("Unsupported arguments\n");
1026
1027 if(!pwszUrl)
1028 return E_INVALIDARG;
1029
1030 hres = map_url_to_zone(pwszUrl, &zone, NULL);
1031 if(FAILED(hres))
1032 return hres;
1033
1034 hres = get_action_policy(zone, dwAction, (BYTE*)&policy, sizeof(policy), URLZONEREG_DEFAULT);
1035 if(FAILED(hres))
1036 return hres;
1037
1038 TRACE("policy %x\n", policy);
1039 if(cbPolicy >= sizeof(DWORD))
1040 *(DWORD*)pPolicy = policy;
1041
1042 switch(GetUrlPolicyPermissions(policy)) {
1043 case URLPOLICY_ALLOW:
1044 case URLPOLICY_CHANNEL_SOFTDIST_PRECACHE:
1045 return S_OK;
1046 case URLPOLICY_DISALLOW:
1047 return S_FALSE;
1048 case URLPOLICY_QUERY:
1049 FIXME("URLPOLICY_QUERY not implemented\n");
1050 return E_FAIL;
1051 default:
1052 FIXME("Not implemented policy %x\n", policy);
1053 }
1054
1055 return E_FAIL;
1056 }
1057
1058
1059 static HRESULT WINAPI SecManagerImpl_QueryCustomPolicy(IInternetSecurityManagerEx2 *iface,
1060 LPCWSTR pwszUrl, REFGUID guidKey,
1061 BYTE **ppPolicy, DWORD *pcbPolicy,
1062 BYTE *pContext, DWORD cbContext,
1063 DWORD dwReserved)
1064 {
1065 SecManagerImpl *This = impl_from_IInternetSecurityManagerEx2(iface);
1066 HRESULT hres;
1067
1068 TRACE("(%p)->(%s %s %p %p %p %08x %08x )\n", iface, debugstr_w(pwszUrl), debugstr_guid(guidKey),
1069 ppPolicy, pcbPolicy, pContext, cbContext, dwReserved);
1070
1071 if(This->custom_manager) {
1072 hres = IInternetSecurityManager_QueryCustomPolicy(This->custom_manager, pwszUrl, guidKey,
1073 ppPolicy, pcbPolicy, pContext, cbContext, dwReserved);
1074 if(hres != INET_E_DEFAULT_ACTION)
1075 return hres;
1076 }
1077
1078 WARN("Unknown guidKey %s\n", debugstr_guid(guidKey));
1079 return HRESULT_FROM_WIN32(ERROR_NOT_FOUND);
1080 }
1081
1082 static HRESULT WINAPI SecManagerImpl_SetZoneMapping(IInternetSecurityManagerEx2 *iface,
1083 DWORD dwZone, LPCWSTR pwszPattern, DWORD dwFlags)
1084 {
1085 SecManagerImpl *This = impl_from_IInternetSecurityManagerEx2(iface);
1086 HRESULT hres;
1087
1088 TRACE("(%p)->(%08x %s %08x)\n", iface, dwZone, debugstr_w(pwszPattern),dwFlags);
1089
1090 if(This->custom_manager) {
1091 hres = IInternetSecurityManager_SetZoneMapping(This->custom_manager, dwZone,
1092 pwszPattern, dwFlags);
1093 if(hres != INET_E_DEFAULT_ACTION)
1094 return hres;
1095 }
1096
1097 FIXME("Default action is not implemented\n");
1098 return E_NOTIMPL;
1099 }
1100
1101 static HRESULT WINAPI SecManagerImpl_GetZoneMappings(IInternetSecurityManagerEx2 *iface,
1102 DWORD dwZone, IEnumString **ppenumString, DWORD dwFlags)
1103 {
1104 SecManagerImpl *This = impl_from_IInternetSecurityManagerEx2(iface);
1105 HRESULT hres;
1106
1107 TRACE("(%p)->(%08x %p %08x)\n", iface, dwZone, ppenumString,dwFlags);
1108
1109 if(This->custom_manager) {
1110 hres = IInternetSecurityManager_GetZoneMappings(This->custom_manager, dwZone,
1111 ppenumString, dwFlags);
1112 if(hres != INET_E_DEFAULT_ACTION)
1113 return hres;
1114 }
1115
1116 FIXME("Default action is not implemented\n");
1117 return E_NOTIMPL;
1118 }
1119
1120 static HRESULT WINAPI SecManagerImpl_ProcessUrlActionEx(IInternetSecurityManagerEx2 *iface,
1121 LPCWSTR pwszUrl, DWORD dwAction, BYTE *pPolicy, DWORD cbPolicy, BYTE *pContext, DWORD cbContext,
1122 DWORD dwFlags, DWORD dwReserved, DWORD *pdwOutFlags)
1123 {
1124 SecManagerImpl *This = impl_from_IInternetSecurityManagerEx2(iface);
1125 FIXME("(%p)->(%s %08x %p %d %p %d %08x %08x %p) stub\n", This, debugstr_w(pwszUrl), dwAction, pPolicy, cbPolicy,
1126 pContext, cbContext, dwFlags, dwReserved, pdwOutFlags);
1127 return E_NOTIMPL;
1128 }
1129
1130 static HRESULT WINAPI SecManagerImpl_MapUrlToZoneEx2(IInternetSecurityManagerEx2 *iface,
1131 IUri *pUri, DWORD *pdwZone, DWORD dwFlags, LPWSTR *ppwszMappedUrl, DWORD *pdwOutFlags)
1132 {
1133 SecManagerImpl *This = impl_from_IInternetSecurityManagerEx2(iface);
1134
1135 TRACE("(%p)->(%p %p %08x %p %p)\n", This, pUri, pdwZone, dwFlags, ppwszMappedUrl, pdwOutFlags);
1136
1137 if(This->custom_manager) {
1138 HRESULT hres;
1139 IInternetSecurityManagerEx2 *sec_mgr2;
1140
1141 hres = IInternetSecurityManager_QueryInterface(This->custom_manager, &IID_IInternetSecurityManagerEx2,
1142 (void**)&sec_mgr2);
1143 if(SUCCEEDED(hres)) {
1144 hres = IInternetSecurityManagerEx2_MapUrlToZoneEx2(sec_mgr2, pUri, pdwZone, dwFlags, ppwszMappedUrl, pdwOutFlags);
1145 IInternetSecurityManagerEx2_Release(sec_mgr2);
1146 } else {
1147 BSTR url;
1148
1149 hres = IUri_GetDisplayUri(pUri, &url);
1150 if(FAILED(hres))
1151 return hres;
1152
1153 hres = IInternetSecurityManager_MapUrlToZone(This->custom_manager, url, pdwZone, dwFlags);
1154 SysFreeString(url);
1155 }
1156
1157 if(hres != INET_E_DEFAULT_ACTION)
1158 return hres;
1159 }
1160
1161 if(!pdwZone)
1162 return E_INVALIDARG;
1163
1164 if(!pUri) {
1165 *pdwZone = URLZONE_INVALID;
1166 return E_INVALIDARG;
1167 }
1168
1169 if(dwFlags)
1170 FIXME("Unsupported flags: %08x\n", dwFlags);
1171
1172 return map_uri_to_zone(pUri, pdwZone, NULL);
1173 }
1174
1175 static HRESULT WINAPI SecManagerImpl_ProcessUrlActionEx2(IInternetSecurityManagerEx2 *iface,
1176 IUri *pUri, DWORD dwAction, BYTE *pPolicy, DWORD cbPolicy, BYTE *pContext, DWORD cbContext,
1177 DWORD dwFlags, DWORD_PTR dwReserved, DWORD *pdwOutFlags)
1178 {
1179 SecManagerImpl *This = impl_from_IInternetSecurityManagerEx2(iface);
1180 FIXME("(%p)->(%p %08x %p %d %p %d %08x %08x %p) stub\n", This, pUri, dwAction, pPolicy,
1181 cbPolicy, pContext, cbContext, dwFlags, (DWORD)dwReserved, pdwOutFlags);
1182 return E_NOTIMPL;
1183 }
1184
1185 static HRESULT WINAPI SecManagerImpl_GetSecurityIdEx2(IInternetSecurityManagerEx2 *iface,
1186 IUri *pUri, BYTE *pbSecurityId, DWORD *pcbSecurityId, DWORD_PTR dwReserved)
1187 {
1188 SecManagerImpl *This = impl_from_IInternetSecurityManagerEx2(iface);
1189 TRACE("(%p)->(%p %p %p %08x) stub\n", This, pUri, pbSecurityId, pcbSecurityId, (DWORD)dwReserved);
1190
1191 if(dwReserved)
1192 FIXME("dwReserved is not supported yet\n");
1193
1194 if(!pUri || !pcbSecurityId || !pbSecurityId)
1195 return E_INVALIDARG;
1196
1197 return get_security_id_for_uri(pUri, pbSecurityId, pcbSecurityId);
1198 }
1199
1200 static HRESULT WINAPI SecManagerImpl_QueryCustomPolicyEx2(IInternetSecurityManagerEx2 *iface,
1201 IUri *pUri, REFGUID guidKey, BYTE **ppPolicy, DWORD *pcbPolicy, BYTE *pContext,
1202 DWORD cbContext, DWORD_PTR dwReserved)
1203 {
1204 SecManagerImpl *This = impl_from_IInternetSecurityManagerEx2(iface);
1205 FIXME("(%p)->(%p %s %p %p %p %d %08x) stub\n", This, pUri, debugstr_guid(guidKey), ppPolicy, pcbPolicy,
1206 pContext, cbContext, (DWORD)dwReserved);
1207 return E_NOTIMPL;
1208 }
1209
1210 static const IInternetSecurityManagerEx2Vtbl VT_SecManagerImpl =
1211 {
1212 SecManagerImpl_QueryInterface,
1213 SecManagerImpl_AddRef,
1214 SecManagerImpl_Release,
1215 SecManagerImpl_SetSecuritySite,
1216 SecManagerImpl_GetSecuritySite,
1217 SecManagerImpl_MapUrlToZone,
1218 SecManagerImpl_GetSecurityId,
1219 SecManagerImpl_ProcessUrlAction,
1220 SecManagerImpl_QueryCustomPolicy,
1221 SecManagerImpl_SetZoneMapping,
1222 SecManagerImpl_GetZoneMappings,
1223 SecManagerImpl_ProcessUrlActionEx,
1224 SecManagerImpl_MapUrlToZoneEx2,
1225 SecManagerImpl_ProcessUrlActionEx2,
1226 SecManagerImpl_GetSecurityIdEx2,
1227 SecManagerImpl_QueryCustomPolicyEx2
1228 };
1229
1230 HRESULT SecManagerImpl_Construct(IUnknown *pUnkOuter, LPVOID *ppobj)
1231 {
1232 SecManagerImpl *This;
1233
1234 TRACE("(%p,%p)\n",pUnkOuter,ppobj);
1235 This = heap_alloc(sizeof(*This));
1236
1237 /* Initialize the virtual function table. */
1238 This->IInternetSecurityManagerEx2_iface.lpVtbl = &VT_SecManagerImpl;
1239
1240 This->ref = 1;
1241 This->mgrsite = NULL;
1242 This->custom_manager = NULL;
1243
1244 *ppobj = This;
1245
1246 URLMON_LockModule();
1247
1248 return S_OK;
1249 }
1250
1251 /***********************************************************************
1252 * InternetZoneManager implementation
1253 *
1254 */
1255 typedef struct {
1256 IInternetZoneManagerEx2 IInternetZoneManagerEx2_iface;
1257 LONG ref;
1258 LPDWORD *zonemaps;
1259 DWORD zonemap_count;
1260 } ZoneMgrImpl;
1261
1262 static inline ZoneMgrImpl *impl_from_IInternetZoneManagerEx2(IInternetZoneManagerEx2 *iface)
1263 {
1264 return CONTAINING_RECORD(iface, ZoneMgrImpl, IInternetZoneManagerEx2_iface);
1265 }
1266
1267
1268 /***********************************************************************
1269 * build_zonemap_from_reg [internal]
1270 *
1271 * Enumerate the Zones in the Registry and return the Zones in a DWORD-array
1272 * The number of the Zones is returned in data[0]
1273 */
1274 static LPDWORD build_zonemap_from_reg(void)
1275 {
1276 WCHAR name[32];
1277 HKEY hkey;
1278 LPDWORD data = NULL;
1279 DWORD allocated = 6; /* space for the zonecount and Zone "0" up to Zone "4" */
1280 DWORD used = 0;
1281 DWORD res;
1282 DWORD len;
1283
1284
1285 res = RegOpenKeyW(HKEY_CURRENT_USER, wszZonesKey, &hkey);
1286 if (res)
1287 return NULL;
1288
1289 data = heap_alloc(allocated * sizeof(DWORD));
1290 if (!data)
1291 goto cleanup;
1292
1293 while (!res) {
1294 name[0] = '\0';
1295 len = sizeof(name) / sizeof(name[0]);
1296 res = RegEnumKeyExW(hkey, used, name, &len, NULL, NULL, NULL, NULL);
1297
1298 if (!res) {
1299 used++;
1300 if (used == allocated) {
1301 LPDWORD new_data;
1302
1303 allocated *= 2;
1304 new_data = heap_realloc_zero(data, allocated * sizeof(DWORD));
1305 if (!new_data)
1306 goto cleanup;
1307
1308 data = new_data;
1309 }
1310 data[used] = atoiW(name);
1311 }
1312 }
1313 if (used) {
1314 RegCloseKey(hkey);
1315 data[0] = used;
1316 return data;
1317 }
1318
1319 cleanup:
1320 /* something failed */
1321 RegCloseKey(hkey);
1322 heap_free(data);
1323 return NULL;
1324 }
1325
1326 /********************************************************************
1327 * IInternetZoneManager_QueryInterface
1328 */
1329 static HRESULT WINAPI ZoneMgrImpl_QueryInterface(IInternetZoneManagerEx2* iface, REFIID riid, void** ppvObject)
1330 {
1331 ZoneMgrImpl* This = impl_from_IInternetZoneManagerEx2(iface);
1332
1333 TRACE("(%p)->(%s,%p)\n", This, debugstr_guid(riid), ppvObject);
1334
1335 if(!This || !ppvObject)
1336 return E_INVALIDARG;
1337
1338 if(IsEqualIID(&IID_IUnknown, riid)) {
1339 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppvObject);
1340 }else if(IsEqualIID(&IID_IInternetZoneManager, riid)) {
1341 TRACE("(%p)->(IID_InternetZoneManager %p)\n", This, ppvObject);
1342 }else if(IsEqualIID(&IID_IInternetZoneManagerEx, riid)) {
1343 TRACE("(%p)->(IID_InternetZoneManagerEx %p)\n", This, ppvObject);
1344 }else if(IsEqualIID(&IID_IInternetZoneManagerEx2, riid)) {
1345 TRACE("(%p)->(IID_InternetZoneManagerEx2 %p)\n", This, ppvObject);
1346 }
1347 else
1348 {
1349 FIXME("Unknown interface: %s\n", debugstr_guid(riid));
1350 *ppvObject = NULL;
1351 return E_NOINTERFACE;
1352 }
1353
1354 *ppvObject = iface;
1355 IInternetZoneManagerEx2_AddRef(iface);
1356 return S_OK;
1357 }
1358
1359 /********************************************************************
1360 * IInternetZoneManager_AddRef
1361 */
1362 static ULONG WINAPI ZoneMgrImpl_AddRef(IInternetZoneManagerEx2* iface)
1363 {
1364 ZoneMgrImpl* This = impl_from_IInternetZoneManagerEx2(iface);
1365 ULONG refCount = InterlockedIncrement(&This->ref);
1366
1367 TRACE("(%p)->(ref before=%u)\n",This, refCount - 1);
1368
1369 return refCount;
1370 }
1371
1372 /********************************************************************
1373 * IInternetZoneManager_Release
1374 */
1375 static ULONG WINAPI ZoneMgrImpl_Release(IInternetZoneManagerEx2* iface)
1376 {
1377 ZoneMgrImpl* This = impl_from_IInternetZoneManagerEx2(iface);
1378 ULONG refCount = InterlockedDecrement(&This->ref);
1379
1380 TRACE("(%p)->(ref before=%u)\n",This, refCount + 1);
1381
1382 if(!refCount) {
1383 while (This->zonemap_count) heap_free(This->zonemaps[--This->zonemap_count]);
1384 heap_free(This->zonemaps);
1385 heap_free(This);
1386 URLMON_UnlockModule();
1387 }
1388
1389 return refCount;
1390 }
1391
1392 /********************************************************************
1393 * IInternetZoneManager_GetZoneAttributes
1394 */
1395 static HRESULT WINAPI ZoneMgrImpl_GetZoneAttributes(IInternetZoneManagerEx2* iface,
1396 DWORD dwZone,
1397 ZONEATTRIBUTES* pZoneAttributes)
1398 {
1399 ZoneMgrImpl* This = impl_from_IInternetZoneManagerEx2(iface);
1400 HRESULT hr;
1401 HKEY hcu;
1402 HKEY hklm = NULL;
1403
1404 TRACE("(%p)->(%d %p)\n", This, dwZone, pZoneAttributes);
1405
1406 if (!pZoneAttributes)
1407 return E_INVALIDARG;
1408
1409 hr = open_zone_key(HKEY_CURRENT_USER, dwZone, &hcu);
1410 if (FAILED(hr))
1411 return S_OK; /* IE6 and older returned E_FAIL here */
1412
1413 hr = open_zone_key(HKEY_LOCAL_MACHINE, dwZone, &hklm);
1414 if (FAILED(hr))
1415 TRACE("Zone %d not in HKLM\n", dwZone);
1416
1417 get_string_from_reg(hcu, hklm, displaynameW, pZoneAttributes->szDisplayName, MAX_ZONE_PATH);
1418 get_string_from_reg(hcu, hklm, descriptionW, pZoneAttributes->szDescription, MAX_ZONE_DESCRIPTION);
1419 get_string_from_reg(hcu, hklm, iconW, pZoneAttributes->szIconPath, MAX_ZONE_PATH);
1420 get_dword_from_reg(hcu, hklm, minlevelW, &pZoneAttributes->dwTemplateMinLevel);
1421 get_dword_from_reg(hcu, hklm, currentlevelW, &pZoneAttributes->dwTemplateCurrentLevel);
1422 get_dword_from_reg(hcu, hklm, recommendedlevelW, &pZoneAttributes->dwTemplateRecommended);
1423 get_dword_from_reg(hcu, hklm, flagsW, &pZoneAttributes->dwFlags);
1424
1425 RegCloseKey(hklm);
1426 RegCloseKey(hcu);
1427 return S_OK;
1428 }
1429
1430 /********************************************************************
1431 * IInternetZoneManager_SetZoneAttributes
1432 */
1433 static HRESULT WINAPI ZoneMgrImpl_SetZoneAttributes(IInternetZoneManagerEx2* iface,
1434 DWORD dwZone,
1435 ZONEATTRIBUTES* pZoneAttributes)
1436 {
1437 ZoneMgrImpl* This = impl_from_IInternetZoneManagerEx2(iface);
1438 HRESULT hr;
1439 HKEY hcu;
1440
1441 TRACE("(%p)->(%d %p)\n", This, dwZone, pZoneAttributes);
1442
1443 if (!pZoneAttributes)
1444 return E_INVALIDARG;
1445
1446 hr = open_zone_key(HKEY_CURRENT_USER, dwZone, &hcu);
1447 if (FAILED(hr))
1448 return S_OK; /* IE6 returned E_FAIL here */
1449
1450 /* cbSize is ignored */
1451 RegSetValueExW(hcu, displaynameW, 0, REG_SZ, (LPBYTE) pZoneAttributes->szDisplayName,
1452 (lstrlenW(pZoneAttributes->szDisplayName)+1)* sizeof(WCHAR));
1453
1454 RegSetValueExW(hcu, descriptionW, 0, REG_SZ, (LPBYTE) pZoneAttributes->szDescription,
1455 (lstrlenW(pZoneAttributes->szDescription)+1)* sizeof(WCHAR));
1456
1457 RegSetValueExW(hcu, iconW, 0, REG_SZ, (LPBYTE) pZoneAttributes->szIconPath,
1458 (lstrlenW(pZoneAttributes->szIconPath)+1)* sizeof(WCHAR));
1459
1460 RegSetValueExW(hcu, minlevelW, 0, REG_DWORD,
1461 (const BYTE*) &pZoneAttributes->dwTemplateMinLevel, sizeof(DWORD));
1462
1463 RegSetValueExW(hcu, currentlevelW, 0, REG_DWORD,
1464 (const BYTE*) &pZoneAttributes->dwTemplateCurrentLevel, sizeof(DWORD));
1465
1466 RegSetValueExW(hcu, recommendedlevelW, 0, REG_DWORD,
1467 (const BYTE*) &pZoneAttributes->dwTemplateRecommended, sizeof(DWORD));
1468
1469 RegSetValueExW(hcu, flagsW, 0, REG_DWORD, (const BYTE*) &pZoneAttributes->dwFlags, sizeof(DWORD));
1470 RegCloseKey(hcu);
1471 return S_OK;
1472
1473 }
1474
1475 /********************************************************************
1476 * IInternetZoneManager_GetZoneCustomPolicy
1477 */
1478 static HRESULT WINAPI ZoneMgrImpl_GetZoneCustomPolicy(IInternetZoneManagerEx2* iface,
1479 DWORD dwZone,
1480 REFGUID guidKey,
1481 BYTE** ppPolicy,
1482 DWORD* pcbPolicy,
1483 URLZONEREG ulrZoneReg)
1484 {
1485 FIXME("(%p)->(%08x %s %p %p %08x) stub\n", iface, dwZone, debugstr_guid(guidKey),
1486 ppPolicy, pcbPolicy, ulrZoneReg);
1487 return E_NOTIMPL;
1488 }
1489
1490 /********************************************************************
1491 * IInternetZoneManager_SetZoneCustomPolicy
1492 */
1493 static HRESULT WINAPI ZoneMgrImpl_SetZoneCustomPolicy(IInternetZoneManagerEx2* iface,
1494 DWORD dwZone,
1495 REFGUID guidKey,
1496 BYTE* ppPolicy,
1497 DWORD cbPolicy,
1498 URLZONEREG ulrZoneReg)
1499 {
1500 FIXME("(%p)->(%08x %s %p %08x %08x) stub\n", iface, dwZone, debugstr_guid(guidKey),
1501 ppPolicy, cbPolicy, ulrZoneReg);
1502 return E_NOTIMPL;
1503 }
1504
1505 /********************************************************************
1506 * IInternetZoneManager_GetZoneActionPolicy
1507 */
1508 static HRESULT WINAPI ZoneMgrImpl_GetZoneActionPolicy(IInternetZoneManagerEx2* iface,
1509 DWORD dwZone, DWORD dwAction, BYTE* pPolicy, DWORD cbPolicy, URLZONEREG urlZoneReg)
1510 {
1511 TRACE("(%p)->(%d %08x %p %d %d)\n", iface, dwZone, dwAction, pPolicy,
1512 cbPolicy, urlZoneReg);
1513
1514 if(!pPolicy)
1515 return E_INVALIDARG;
1516
1517 return get_action_policy(dwZone, dwAction, pPolicy, cbPolicy, urlZoneReg);
1518 }
1519
1520 /********************************************************************
1521 * IInternetZoneManager_SetZoneActionPolicy
1522 */
1523 static HRESULT WINAPI ZoneMgrImpl_SetZoneActionPolicy(IInternetZoneManagerEx2* iface,
1524 DWORD dwZone,
1525 DWORD dwAction,
1526 BYTE* pPolicy,
1527 DWORD cbPolicy,
1528 URLZONEREG urlZoneReg)
1529 {
1530 FIXME("(%p)->(%08x %08x %p %08x %08x) stub\n", iface, dwZone, dwAction, pPolicy,
1531 cbPolicy, urlZoneReg);
1532 return E_NOTIMPL;
1533 }
1534
1535 /********************************************************************
1536 * IInternetZoneManager_PromptAction
1537 */
1538 static HRESULT WINAPI ZoneMgrImpl_PromptAction(IInternetZoneManagerEx2* iface,
1539 DWORD dwAction,
1540 HWND hwndParent,
1541 LPCWSTR pwszUrl,
1542 LPCWSTR pwszText,
1543 DWORD dwPromptFlags)
1544 {
1545 FIXME("%p %08x %p %s %s %08x\n", iface, dwAction, hwndParent,
1546 debugstr_w(pwszUrl), debugstr_w(pwszText), dwPromptFlags );
1547 return E_NOTIMPL;
1548 }
1549
1550 /********************************************************************
1551 * IInternetZoneManager_LogAction
1552 */
1553 static HRESULT WINAPI ZoneMgrImpl_LogAction(IInternetZoneManagerEx2* iface,
1554 DWORD dwAction,
1555 LPCWSTR pwszUrl,
1556 LPCWSTR pwszText,
1557 DWORD dwLogFlags)
1558 {
1559 FIXME("(%p)->(%08x %s %s %08x) stub\n", iface, dwAction, debugstr_w(pwszUrl),
1560 debugstr_w(pwszText), dwLogFlags);
1561 return E_NOTIMPL;
1562 }
1563
1564 /********************************************************************
1565 * IInternetZoneManager_CreateZoneEnumerator
1566 */
1567 static HRESULT WINAPI ZoneMgrImpl_CreateZoneEnumerator(IInternetZoneManagerEx2* iface,
1568 DWORD* pdwEnum,
1569 DWORD* pdwCount,
1570 DWORD dwFlags)
1571 {
1572 ZoneMgrImpl* This = impl_from_IInternetZoneManagerEx2(iface);
1573 LPDWORD * new_maps;
1574 LPDWORD data;
1575 DWORD i;
1576
1577 TRACE("(%p)->(%p, %p, 0x%08x)\n", This, pdwEnum, pdwCount, dwFlags);
1578 if (!pdwEnum || !pdwCount || (dwFlags != 0))
1579 return E_INVALIDARG;
1580
1581 data = build_zonemap_from_reg();
1582 TRACE("found %d zones\n", data ? data[0] : -1);
1583
1584 if (!data)
1585 return E_FAIL;
1586
1587 for (i = 0; i < This->zonemap_count; i++) {
1588 if (This->zonemaps && !This->zonemaps[i]) {
1589 This->zonemaps[i] = data;
1590 *pdwEnum = i;
1591 *pdwCount = data[0];
1592 return S_OK;
1593 }
1594 }
1595
1596 if (This->zonemaps) {
1597 /* try to double the nr. of pointers in the array */
1598 new_maps = heap_realloc_zero(This->zonemaps, This->zonemap_count * 2 * sizeof(LPDWORD));
1599 if (new_maps)
1600 This->zonemap_count *= 2;
1601 }
1602 else
1603 {
1604 This->zonemap_count = 2;
1605 new_maps = heap_alloc_zero(This->zonemap_count * sizeof(LPDWORD));
1606 }
1607
1608 if (!new_maps) {
1609 heap_free(data);
1610 return E_FAIL;
1611 }
1612 This->zonemaps = new_maps;
1613 This->zonemaps[i] = data;
1614 *pdwEnum = i;
1615 *pdwCount = data[0];
1616 return S_OK;
1617 }
1618
1619 /********************************************************************
1620 * IInternetZoneManager_GetZoneAt
1621 */
1622 static HRESULT WINAPI ZoneMgrImpl_GetZoneAt(IInternetZoneManagerEx2* iface,
1623 DWORD dwEnum,
1624 DWORD dwIndex,
1625 DWORD* pdwZone)
1626 {
1627 ZoneMgrImpl* This = impl_from_IInternetZoneManagerEx2(iface);
1628 LPDWORD data;
1629
1630 TRACE("(%p)->(0x%08x, %d, %p)\n", This, dwEnum, dwIndex, pdwZone);
1631
1632 /* make sure, that dwEnum and dwIndex are in the valid range */
1633 if (dwEnum < This->zonemap_count) {
1634 if ((data = This->zonemaps[dwEnum])) {
1635 if (dwIndex < data[0]) {
1636 *pdwZone = data[dwIndex + 1];
1637 return S_OK;
1638 }
1639 }
1640 }
1641 return E_INVALIDARG;
1642 }
1643
1644 /********************************************************************
1645 * IInternetZoneManager_DestroyZoneEnumerator
1646 */
1647 static HRESULT WINAPI ZoneMgrImpl_DestroyZoneEnumerator(IInternetZoneManagerEx2* iface,
1648 DWORD dwEnum)
1649 {
1650 ZoneMgrImpl* This = impl_from_IInternetZoneManagerEx2(iface);
1651 LPDWORD data;
1652
1653 TRACE("(%p)->(0x%08x)\n", This, dwEnum);
1654 /* make sure, that dwEnum is valid */
1655 if (dwEnum < This->zonemap_count) {
1656 if ((data = This->zonemaps[dwEnum])) {
1657 This->zonemaps[dwEnum] = NULL;
1658 heap_free(data);
1659 return S_OK;
1660 }
1661 }
1662 return E_INVALIDARG;
1663 }
1664
1665 /********************************************************************
1666 * IInternetZoneManager_CopyTemplatePoliciesToZone
1667 */
1668 static HRESULT WINAPI ZoneMgrImpl_CopyTemplatePoliciesToZone(IInternetZoneManagerEx2* iface,
1669 DWORD dwTemplate,
1670 DWORD dwZone,
1671 DWORD dwReserved)
1672 {
1673 FIXME("(%p)->(%08x %08x %08x) stub\n", iface, dwTemplate, dwZone, dwReserved);
1674 return E_NOTIMPL;
1675 }
1676
1677 /********************************************************************
1678 * IInternetZoneManagerEx_GetZoneActionPolicyEx
1679 */
1680 static HRESULT WINAPI ZoneMgrImpl_GetZoneActionPolicyEx(IInternetZoneManagerEx2* iface,
1681 DWORD dwZone,
1682 DWORD dwAction,
1683 BYTE* pPolicy,
1684 DWORD cbPolicy,
1685 URLZONEREG urlZoneReg,
1686 DWORD dwFlags)
1687 {
1688 TRACE("(%p)->(%d, 0x%x, %p, %d, %d, 0x%x)\n", iface, dwZone,
1689 dwAction, pPolicy, cbPolicy, urlZoneReg, dwFlags);
1690
1691 if(!pPolicy)
1692 return E_INVALIDARG;
1693
1694 if (dwFlags)
1695 FIXME("dwFlags 0x%x ignored\n", dwFlags);
1696
1697 return get_action_policy(dwZone, dwAction, pPolicy, cbPolicy, urlZoneReg);
1698 }
1699
1700 /********************************************************************
1701 * IInternetZoneManagerEx_SetZoneActionPolicyEx
1702 */
1703 static HRESULT WINAPI ZoneMgrImpl_SetZoneActionPolicyEx(IInternetZoneManagerEx2* iface,
1704 DWORD dwZone,
1705 DWORD dwAction,
1706 BYTE* pPolicy,
1707 DWORD cbPolicy,
1708 URLZONEREG urlZoneReg,
1709 DWORD dwFlags)
1710 {
1711 FIXME("(%p)->(%d, 0x%x, %p, %d, %d, 0x%x) stub\n", iface, dwZone, dwAction, pPolicy,
1712 cbPolicy, urlZoneReg, dwFlags);
1713 return E_NOTIMPL;
1714 }
1715
1716 /********************************************************************
1717 * IInternetZoneManagerEx2_GetZoneAttributesEx
1718 */
1719 static HRESULT WINAPI ZoneMgrImpl_GetZoneAttributesEx(IInternetZoneManagerEx2* iface,
1720 DWORD dwZone,
1721 ZONEATTRIBUTES* pZoneAttributes,
1722 DWORD dwFlags)
1723 {
1724 TRACE("(%p)->(%d, %p, 0x%x)\n", iface, dwZone, pZoneAttributes, dwFlags);
1725
1726 if (dwFlags)
1727 FIXME("dwFlags 0x%x ignored\n", dwFlags);
1728
1729 return IInternetZoneManagerEx2_GetZoneAttributes(iface, dwZone, pZoneAttributes);
1730 }
1731
1732
1733 /********************************************************************
1734 * IInternetZoneManagerEx2_GetZoneSecurityState
1735 */
1736 static HRESULT WINAPI ZoneMgrImpl_GetZoneSecurityState(IInternetZoneManagerEx2* iface,
1737 DWORD dwZoneIndex,
1738 BOOL fRespectPolicy,
1739 LPDWORD pdwState,
1740 BOOL *pfPolicyEncountered)
1741 {
1742 FIXME("(%p)->(%d, %d, %p, %p) stub\n", iface, dwZoneIndex, fRespectPolicy,
1743 pdwState, pfPolicyEncountered);
1744
1745 *pdwState = SECURITY_IE_STATE_GREEN;
1746
1747 if (pfPolicyEncountered)
1748 *pfPolicyEncountered = FALSE;
1749
1750 return S_OK;
1751 }
1752
1753 /********************************************************************
1754 * IInternetZoneManagerEx2_GetIESecurityState
1755 */
1756 static HRESULT WINAPI ZoneMgrImpl_GetIESecurityState(IInternetZoneManagerEx2* iface,
1757 BOOL fRespectPolicy,
1758 LPDWORD pdwState,
1759 BOOL *pfPolicyEncountered,
1760 BOOL fNoCache)
1761 {
1762 FIXME("(%p)->(%d, %p, %p, %d) stub\n", iface, fRespectPolicy, pdwState,
1763 pfPolicyEncountered, fNoCache);
1764
1765 *pdwState = SECURITY_IE_STATE_GREEN;
1766
1767 if (pfPolicyEncountered)
1768 *pfPolicyEncountered = FALSE;
1769
1770 return S_OK;
1771 }
1772
1773 /********************************************************************
1774 * IInternetZoneManagerEx2_FixInsecureSettings
1775 */
1776 static HRESULT WINAPI ZoneMgrImpl_FixInsecureSettings(IInternetZoneManagerEx2* iface)
1777 {
1778 FIXME("(%p) stub\n", iface);
1779 return S_OK;
1780 }
1781
1782 /********************************************************************
1783 * IInternetZoneManager_Construct
1784 */
1785 static const IInternetZoneManagerEx2Vtbl ZoneMgrImplVtbl = {
1786 ZoneMgrImpl_QueryInterface,
1787 ZoneMgrImpl_AddRef,
1788 ZoneMgrImpl_Release,
1789 /* IInternetZoneManager */
1790 ZoneMgrImpl_GetZoneAttributes,
1791 ZoneMgrImpl_SetZoneAttributes,
1792 ZoneMgrImpl_GetZoneCustomPolicy,
1793 ZoneMgrImpl_SetZoneCustomPolicy,
1794 ZoneMgrImpl_GetZoneActionPolicy,
1795 ZoneMgrImpl_SetZoneActionPolicy,
1796 ZoneMgrImpl_PromptAction,
1797 ZoneMgrImpl_LogAction,
1798 ZoneMgrImpl_CreateZoneEnumerator,
1799 ZoneMgrImpl_GetZoneAt,
1800 ZoneMgrImpl_DestroyZoneEnumerator,
1801 ZoneMgrImpl_CopyTemplatePoliciesToZone,
1802 /* IInternetZoneManagerEx */
1803 ZoneMgrImpl_GetZoneActionPolicyEx,
1804 ZoneMgrImpl_SetZoneActionPolicyEx,
1805 /* IInternetZoneManagerEx2 */
1806 ZoneMgrImpl_GetZoneAttributesEx,
1807 ZoneMgrImpl_GetZoneSecurityState,
1808 ZoneMgrImpl_GetIESecurityState,
1809 ZoneMgrImpl_FixInsecureSettings,
1810 };
1811
1812 HRESULT ZoneMgrImpl_Construct(IUnknown *pUnkOuter, LPVOID *ppobj)
1813 {
1814 ZoneMgrImpl* ret = heap_alloc_zero(sizeof(ZoneMgrImpl));
1815
1816 TRACE("(%p %p)\n", pUnkOuter, ppobj);
1817 ret->IInternetZoneManagerEx2_iface.lpVtbl = &ZoneMgrImplVtbl;
1818 ret->ref = 1;
1819 *ppobj = (IInternetZoneManagerEx*)ret;
1820
1821 URLMON_LockModule();
1822
1823 return S_OK;
1824 }
1825
1826 /***********************************************************************
1827 * CoInternetCreateSecurityManager (URLMON.@)
1828 *
1829 */
1830 HRESULT WINAPI CoInternetCreateSecurityManager( IServiceProvider *pSP,
1831 IInternetSecurityManager **ppSM, DWORD dwReserved )
1832 {
1833 TRACE("%p %p %d\n", pSP, ppSM, dwReserved );
1834
1835 if(pSP)
1836 FIXME("pSP not supported\n");
1837
1838 return SecManagerImpl_Construct(NULL, (void**) ppSM);
1839 }
1840
1841 /********************************************************************
1842 * CoInternetCreateZoneManager (URLMON.@)
1843 */
1844 HRESULT WINAPI CoInternetCreateZoneManager(IServiceProvider* pSP, IInternetZoneManager** ppZM, DWORD dwReserved)
1845 {
1846 TRACE("(%p %p %x)\n", pSP, ppZM, dwReserved);
1847 return ZoneMgrImpl_Construct(NULL, (void**)ppZM);
1848 }
1849
1850 static HRESULT parse_security_url(const WCHAR *url, PSUACTION action, WCHAR **result) {
1851 IInternetProtocolInfo *protocol_info;
1852 WCHAR *tmp, *new_url = NULL, *alloc_url = NULL;
1853 DWORD size, new_size;
1854 HRESULT hres = S_OK, parse_hres;
1855
1856 while(1) {
1857 TRACE("parsing %s\n", debugstr_w(url));
1858
1859 protocol_info = get_protocol_info(url);
1860 if(!protocol_info)
1861 break;
1862
1863 size = strlenW(url)+1;
1864 new_url = CoTaskMemAlloc(size*sizeof(WCHAR));
1865 if(!new_url) {
1866 hres = E_OUTOFMEMORY;
1867 break;
1868 }
1869
1870 new_size = 0;
1871 parse_hres = IInternetProtocolInfo_ParseUrl(protocol_info, url, PARSE_SECURITY_URL, 0, new_url, size, &new_size, 0);
1872 if(parse_hres == S_FALSE) {
1873 if(!new_size) {
1874 hres = E_UNEXPECTED;
1875 break;
1876 }
1877
1878 tmp = CoTaskMemRealloc(new_url, new_size*sizeof(WCHAR));
1879 if(!tmp) {
1880 hres = E_OUTOFMEMORY;
1881 break;
1882 }
1883 new_url = tmp;
1884 parse_hres = IInternetProtocolInfo_ParseUrl(protocol_info, url, PARSE_SECURITY_URL, 0, new_url,
1885 new_size, &new_size, 0);
1886 if(parse_hres == S_FALSE) {
1887 hres = E_FAIL;
1888 break;
1889 }
1890 }
1891
1892 if(parse_hres != S_OK || !strcmpW(url, new_url))
1893 break;
1894
1895 CoTaskMemFree(alloc_url);
1896 url = alloc_url = new_url;
1897 new_url = NULL;
1898 }
1899
1900 CoTaskMemFree(new_url);
1901
1902 if(hres != S_OK) {
1903 WARN("failed: %08x\n", hres);
1904 CoTaskMemFree(alloc_url);
1905 return hres;
1906 }
1907
1908 if(action == PSU_DEFAULT && (protocol_info = get_protocol_info(url))) {
1909 size = strlenW(url)+1;
1910 new_url = CoTaskMemAlloc(size * sizeof(WCHAR));
1911 if(new_url) {
1912 new_size = 0;
1913 parse_hres = IInternetProtocolInfo_ParseUrl(protocol_info, url, PARSE_SECURITY_DOMAIN, 0,
1914 new_url, size, &new_size, 0);
1915 if(parse_hres == S_FALSE) {
1916 if(new_size) {
1917 tmp = CoTaskMemRealloc(new_url, new_size*sizeof(WCHAR));
1918 if(tmp) {
1919 new_url = tmp;
1920 parse_hres = IInternetProtocolInfo_ParseUrl(protocol_info, url, PARSE_SECURITY_DOMAIN, 0, new_url,
1921 new_size, &new_size, 0);
1922 if(parse_hres == S_FALSE)
1923 hres = E_FAIL;
1924 }else {
1925 hres = E_OUTOFMEMORY;
1926 }
1927 }else {
1928 hres = E_UNEXPECTED;
1929 }
1930 }
1931
1932 if(hres == S_OK && parse_hres == S_OK) {
1933 CoTaskMemFree(alloc_url);
1934 url = alloc_url = new_url;
1935 new_url = NULL;
1936 }
1937
1938 CoTaskMemFree(new_url);
1939 }else {
1940 hres = E_OUTOFMEMORY;
1941 }
1942 IInternetProtocolInfo_Release(protocol_info);
1943 }
1944
1945 if(FAILED(hres)) {
1946 WARN("failed %08x\n", hres);
1947 CoTaskMemFree(alloc_url);
1948 return hres;
1949 }
1950
1951 if(!alloc_url) {
1952 size = strlenW(url)+1;
1953 alloc_url = CoTaskMemAlloc(size * sizeof(WCHAR));
1954 if(!alloc_url)
1955 return E_OUTOFMEMORY;
1956 memcpy(alloc_url, url, size * sizeof(WCHAR));
1957 }
1958
1959 *result = alloc_url;
1960 return S_OK;
1961 }
1962
1963 /********************************************************************
1964 * CoInternetGetSecurityUrl (URLMON.@)
1965 */
1966 HRESULT WINAPI CoInternetGetSecurityUrl(LPCWSTR pwzUrl, LPWSTR *ppwzSecUrl, PSUACTION psuAction, DWORD dwReserved)
1967 {
1968 WCHAR *secure_url;
1969 HRESULT hres;
1970
1971 TRACE("(%p,%p,%u,%u)\n", pwzUrl, ppwzSecUrl, psuAction, dwReserved);
1972
1973 hres = parse_security_url(pwzUrl, psuAction, &secure_url);
1974 if(FAILED(hres))
1975 return hres;
1976
1977 if(psuAction != PSU_SECURITY_URL_ONLY) {
1978 PARSEDURLW parsed_url = { sizeof(parsed_url) };
1979 DWORD size;
1980
1981 /* FIXME: Use helpers from uri.c */
1982 if(SUCCEEDED(ParseURLW(secure_url, &parsed_url))) {
1983 WCHAR *new_url;
1984
1985 switch(parsed_url.nScheme) {
1986 case URL_SCHEME_FTP:
1987 case URL_SCHEME_HTTP:
1988 case URL_SCHEME_HTTPS:
1989 size = strlenW(secure_url)+1;
1990 new_url = CoTaskMemAlloc(size * sizeof(WCHAR));
1991 if(new_url)
1992 hres = UrlGetPartW(secure_url, new_url, &size, URL_PART_HOSTNAME, URL_PARTFLAG_KEEPSCHEME);
1993 else
1994 hres = E_OUTOFMEMORY;
1995 CoTaskMemFree(secure_url);
1996 if(hres != S_OK) {
1997 WARN("UrlGetPart failed: %08x\n", hres);
1998 CoTaskMemFree(new_url);
1999 return FAILED(hres) ? hres : E_FAIL;
2000 }
2001 secure_url = new_url;
2002 }
2003 }
2004 }
2005
2006 *ppwzSecUrl = secure_url;
2007 return S_OK;
2008 }
2009
2010 /********************************************************************
2011 * CoInternetGetSecurityUrlEx (URLMON.@)
2012 */
2013 HRESULT WINAPI CoInternetGetSecurityUrlEx(IUri *pUri, IUri **ppSecUri, PSUACTION psuAction, DWORD_PTR dwReserved)
2014 {
2015 URL_SCHEME scheme_type;
2016 BSTR secure_uri;
2017 WCHAR *ret_url;
2018 HRESULT hres;
2019
2020 TRACE("(%p,%p,%u,%u)\n", pUri, ppSecUri, psuAction, (DWORD)dwReserved);
2021
2022 if(!pUri || !ppSecUri)
2023 return E_INVALIDARG;
2024
2025 hres = IUri_GetDisplayUri(pUri, &secure_uri);
2026 if(FAILED(hres))
2027 return hres;
2028
2029 hres = parse_security_url(secure_uri, psuAction, &ret_url);
2030 SysFreeString(secure_uri);
2031 if(FAILED(hres))
2032 return hres;
2033
2034 /* File URIs have to hierarchical. */
2035 hres = IUri_GetScheme(pUri, (DWORD*)&scheme_type);
2036 if(SUCCEEDED(hres) && scheme_type == URL_SCHEME_FILE) {
2037 const WCHAR *tmp = ret_url;
2038
2039 /* Check and see if a "//" is after the scheme name. */
2040 tmp += sizeof(fileW)/sizeof(WCHAR);
2041 if(*tmp != '/' || *(tmp+1) != '/')
2042 hres = E_INVALIDARG;
2043 }
2044
2045 if(SUCCEEDED(hres))
2046 hres = CreateUri(ret_url, Uri_CREATE_ALLOW_IMPLICIT_WILDCARD_SCHEME, 0, ppSecUri);
2047 CoTaskMemFree(ret_url);
2048 return hres;
2049 }
2050
2051 /********************************************************************
2052 * CompareSecurityIds (URLMON.@)
2053 */
2054 HRESULT WINAPI CompareSecurityIds(BYTE *secid1, DWORD size1, BYTE *secid2, DWORD size2, DWORD reserved)
2055 {
2056 FIXME("(%p %d %p %d %x)\n", secid1, size1, secid2, size2, reserved);
2057 return E_NOTIMPL;
2058 }