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