wininet: Fixed NULL cookie data pointer handling in InternetGetCookieA.
[wine] / dlls / inetcpl.cpl / security.c
1 /*
2  * Internet control panel applet: security propsheet
3  *
4  * Copyright 2011 Detlef Riekenberg
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  *
20  */
21
22 #define COBJMACROS
23 #define CONST_VTABLE
24 #define NONAMELESSUNION
25
26 #include <stdarg.h>
27 #include <windef.h>
28 #include <winbase.h>
29 #include <winuser.h>
30 #include <prsht.h>
31 #include "commctrl.h"
32
33 #include "ole2.h"
34 #include "urlmon.h"
35 #include "initguid.h"
36 #include "winreg.h"
37 #include "shlwapi.h"
38
39 #include "inetcpl.h"
40 #include "wine/debug.h"
41
42 WINE_DEFAULT_DEBUG_CHANNEL(inetcpl);
43
44 typedef struct secdlg_data_s {
45     HWND hsec;  /* security propsheet */
46     HWND hlv;   /* listview */
47     HWND htb;   /* trackbar */
48     IInternetSecurityManager *sec_mgr;
49     IInternetZoneManager *zone_mgr;
50     DWORD zone_enumerator;
51     DWORD num_zones;
52     ZONEATTRIBUTES *zone_attr;
53     DWORD *zones;
54     DWORD *levels;
55     HIMAGELIST himages;
56     DWORD last_lv_index;
57     DWORD last_level;
58 } secdlg_data;
59
60 #define NUM_TRACKBAR_POS 5
61
62 static DWORD url_templates[] = {URLTEMPLATE_CUSTOM,
63                                 URLTEMPLATE_LOW,
64                                 URLTEMPLATE_MEDLOW,
65                                 URLTEMPLATE_MEDIUM,
66                                 URLTEMPLATE_MEDHIGH,
67                                 URLTEMPLATE_HIGH};
68
69 /*********************************************************************
70  * index_from_urltemplate [internal]
71  *
72  */
73 static DWORD index_from_urltemplate(URLTEMPLATE value)
74 {
75
76     DWORD index = sizeof(url_templates) / sizeof(url_templates[0]);
77
78     while((index > 0) && (url_templates[index-1] != value))
79         index--;
80
81     index--; /* table entries are 0 based */
82     if (!index && value)
83         FIXME("URLTEMPLATE 0x%x not supported\n", value);
84
85     TRACE("URLTEMPLATE 0x%08x=> Level %d\n", value, index);
86     return index;
87 }
88
89 /*********************************************************************
90  * update_security_level [internal]
91  *
92  */
93 static void update_security_level(secdlg_data *sd, DWORD lv_index, DWORD tb_index)
94 {
95     WCHAR name[512];
96     DWORD current_index;
97
98     TRACE("(%p, lv_index: %u, tb_index: %u)\n", sd, lv_index, tb_index);
99
100     if ((sd->levels[lv_index] != sd->last_level) || (tb_index > 0)) {
101         /* show or hide the trackbar */
102         if (!sd->levels[lv_index] || !sd->last_level)
103             ShowWindow(sd->htb, sd->levels[lv_index] ? SW_NORMAL : SW_HIDE);
104
105         current_index = (tb_index > 0) ? tb_index : index_from_urltemplate(sd->levels[lv_index]);
106
107         name[0] = 0;
108         LoadStringW(hcpl, IDS_SEC_LEVEL0 + current_index, name, sizeof(name)/sizeof(name[0]));
109         TRACE("new level #%d: %s\n", current_index, debugstr_w(name));
110         SetWindowTextW(GetDlgItem(sd->hsec, IDC_SEC_LEVEL), name);
111
112         name[0] = 0;
113         LoadStringW(hcpl, IDS_SEC_LEVEL0_INFO + (current_index * 0x10), name, sizeof(name)/sizeof(name[0]));
114         TRACE("new level info: %s\n", debugstr_w(name));
115         SetWindowTextW(GetDlgItem(sd->hsec, IDC_SEC_LEVEL_INFO), name);
116
117         if (current_index)
118             SendMessageW(sd->htb, TBM_SETPOS, TRUE, NUM_TRACKBAR_POS - current_index);
119
120         sd->last_level = sd->levels[lv_index];
121
122     }
123 }
124
125 /*********************************************************************
126  * update_zone_info [internal]
127  *
128  */
129 static void update_zone_info(secdlg_data *sd, DWORD lv_index)
130 {
131     ZONEATTRIBUTES *za = &sd->zone_attr[lv_index];
132     WCHAR name[MAX_PATH];
133     DWORD len;
134
135     SetWindowTextW(GetDlgItem(sd->hsec, IDC_SEC_ZONE_INFO), za->szDescription);
136
137     LoadStringW(hcpl, IDS_SEC_SETTINGS, name, sizeof(name)/sizeof(*name));
138     len = lstrlenW(name);
139     lstrcpynW(&name[len], za->szDisplayName, sizeof(name)/sizeof(*name) - len - 1);
140
141     TRACE("new title: %s\n", debugstr_w(name));
142     SetWindowTextW(GetDlgItem(sd->hsec, IDC_SEC_GROUP), name);
143
144     update_security_level(sd, lv_index, 0);
145     sd->last_lv_index = lv_index;
146 }
147
148 /*********************************************************************
149  * add_zone_to_listview [internal]
150  *
151  */
152 static void add_zone_to_listview(secdlg_data *sd, DWORD *pindex, DWORD zone)
153 {
154     DWORD lv_index = *pindex;
155     ZONEATTRIBUTES *za = &sd->zone_attr[lv_index];
156     LVITEMW lvitem;
157     HRESULT hr;
158     INT iconid = 0;
159     HMODULE hdll = NULL;
160     WCHAR * ptr;
161     HICON icon;
162
163     TRACE("item %d (zone %d)\n", lv_index, zone);
164
165     sd->zones[lv_index] = zone;
166
167     memset(&lvitem, 0, sizeof(LVITEMW));
168     memset(za, 0, sizeof(ZONEATTRIBUTES));
169     za->cbSize = sizeof(ZONEATTRIBUTES);
170     hr = IInternetZoneManager_GetZoneAttributes(sd->zone_mgr, zone, za);
171     if (SUCCEEDED(hr)) {
172         TRACE("displayname: %s\n", debugstr_w(za->szDisplayName));
173         TRACE("description: %s\n", debugstr_w(za->szDescription));
174         TRACE("minlevel: 0x%x, recommended: 0x%x, current: 0x%x (flags: 0x%x)\n", za->dwTemplateMinLevel,
175              za->dwTemplateRecommended, za->dwTemplateCurrentLevel, za->dwFlags);
176
177         if (za->dwFlags & ZAFLAGS_NO_UI ) {
178             TRACE("item %d (zone %d): UI disabled for %s\n", lv_index, zone, debugstr_w(za->szDisplayName));
179             return;
180         }
181
182         sd->levels[lv_index] = za->dwTemplateCurrentLevel;
183
184         lvitem.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM;
185         lvitem.iItem = lv_index;
186         lvitem.iSubItem = 0;
187         lvitem.pszText = za->szDisplayName;
188         lvitem.lParam = (LPARAM) zone;
189
190         /* format is "filename.ext#iconid" */
191         ptr = StrChrW(za->szIconPath, '#');
192         if (ptr) {
193             *ptr = 0;
194             ptr++;
195             iconid = StrToIntW(ptr);
196             hdll = LoadLibraryExW(za->szIconPath, NULL, LOAD_LIBRARY_AS_DATAFILE);
197             TRACE("%p: icon #%d from %s\n", hdll, iconid, debugstr_w(za->szIconPath));
198
199             icon = LoadImageW(hdll, MAKEINTRESOURCEW(iconid), IMAGE_ICON, GetSystemMetrics(SM_CXICON),
200                               GetSystemMetrics(SM_CYICON), LR_SHARED);
201
202             if (!icon) {
203                 FIXME("item %d (zone %d): missing icon #%d in %s\n", lv_index, zone, iconid, debugstr_w(za->szIconPath));
204             }
205
206             /* the failure result (NULL) from LoadImageW let ImageList_AddIcon fail
207                with -1, which is reused in ListView_InsertItemW to disable the image */
208             lvitem.iImage = ImageList_AddIcon(sd->himages, icon);
209         }
210         else
211             FIXME("item %d (zone %d): malformed szIconPath %s\n", lv_index, zone, debugstr_w(za->szIconPath));
212
213         if (ListView_InsertItemW(sd->hlv, &lvitem) >= 0) {
214             /* activate first item in the listview */
215             if (! lv_index) {
216                 lvitem.state = LVIS_FOCUSED | LVIS_SELECTED;
217                 lvitem.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
218                 SendMessageW(sd->hlv, LVM_SETITEMSTATE, 0, (LPARAM) &lvitem);
219                 sd->last_level = ~0;
220                 update_zone_info(sd, lv_index);
221             }
222             (*pindex)++;
223         }
224         FreeLibrary(hdll);
225     }
226     else
227         FIXME("item %d (zone %d): GetZoneAttributes failed with 0x%x\n", lv_index, zone, hr);
228 }
229
230 /*********************************************************************
231  * security_cleanup_zones [internal]
232  *
233  */
234 static void security_cleanup_zones(secdlg_data *sd)
235 {
236     if (sd->zone_enumerator) {
237         IInternetZoneManager_DestroyZoneEnumerator(sd->zone_mgr, sd->zone_enumerator);
238     }
239
240     if (sd->zone_mgr) {
241         IInternetZoneManager_Release(sd->zone_mgr);
242     }
243
244     if (sd->sec_mgr) {
245         IInternetSecurityManager_Release(sd->sec_mgr);
246     }
247 }
248
249 /*********************************************************************
250  * security_enum_zones [internal]
251  *
252  */
253 static HRESULT security_enum_zones(secdlg_data * sd)
254 {
255     HRESULT hr;
256
257     hr = CoInternetCreateSecurityManager(NULL, &sd->sec_mgr, 0);
258     if (SUCCEEDED(hr)) {
259         hr = CoInternetCreateZoneManager(NULL, &sd->zone_mgr, 0);
260         if (SUCCEEDED(hr)) {
261             hr = IInternetZoneManager_CreateZoneEnumerator(sd->zone_mgr, &sd->zone_enumerator, &sd->num_zones, 0);
262         }
263     }
264     return hr;
265 }
266
267 /*********************************************************************
268  * security_on_destroy [internal]
269  *
270  * handle WM_NCDESTROY
271  *
272  */
273 static INT_PTR security_on_destroy(secdlg_data * sd)
274 {
275     TRACE("(%p)\n", sd);
276
277     heap_free(sd->zone_attr);
278     heap_free(sd->zones);
279     if (sd->himages) {
280         SendMessageW(sd->hlv, LVM_SETIMAGELIST, LVSIL_NORMAL, 0);
281         ImageList_Destroy(sd->himages);
282     }
283
284     security_cleanup_zones(sd);
285     SetWindowLongPtrW(sd->hsec, DWLP_USER, 0);
286     heap_free(sd);
287     return TRUE;
288 }
289
290 /*********************************************************************
291  * security_on_initdialog [internal]
292  *
293  * handle WM_INITDIALOG
294  *
295  */
296 static INT_PTR security_on_initdialog(HWND hsec)
297 {
298     secdlg_data *sd;
299     HRESULT hr;
300     DWORD current_zone;
301     DWORD lv_index = 0;
302     DWORD i;
303
304     sd = heap_alloc_zero(sizeof(secdlg_data));
305     SetWindowLongPtrW(hsec, DWLP_USER, (LONG_PTR) sd);
306     if (!sd) {
307         return FALSE;
308     }
309
310     sd->hsec = hsec;
311     sd->hlv = GetDlgItem(hsec, IDC_SEC_LISTVIEW);
312     sd->htb = GetDlgItem(hsec, IDC_SEC_TRACKBAR);
313
314     EnableWindow(sd->htb, FALSE); /* not changeable yet */
315
316     TRACE("(%p)   (data: %p, listview: %p, trackbar: %p)\n", hsec, sd, sd->hlv, sd->htb);
317
318     SendMessageW(sd->htb, TBM_SETRANGE, FALSE, MAKELONG(0, NUM_TRACKBAR_POS - 1));
319     SendMessageW(sd->htb, TBM_SETTICFREQ, 1, 0 );
320
321     /* Create the image lists for the listview */
322     sd->himages = ImageList_Create(GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON), ILC_COLOR32 | ILC_MASK, 1, 1);
323
324     TRACE("using imagelist: %p\n", sd->himages);
325     if (!sd->himages) {
326         ERR("ImageList_Create failed!\n");
327         return FALSE;
328     }
329     SendMessageW(sd->hlv, LVM_SETIMAGELIST, LVSIL_NORMAL, (LPARAM)sd->himages);
330
331     hr = security_enum_zones(sd);
332     if (FAILED(hr)) {
333         ERR("got 0x%x\n", hr);
334         security_on_destroy(sd);
335         return FALSE;
336     }
337
338     TRACE("found %d zones\n", sd->num_zones);
339
340     /* remember ZONEATTRIBUTES for a listview entry */
341     sd->zone_attr = heap_alloc(sizeof(ZONEATTRIBUTES) * sd->num_zones);
342     if (!sd->zone_attr) {
343         security_on_destroy(sd);
344         return FALSE;
345     }
346
347     /* remember zone number and current security level for a listview entry */
348     sd->zones = heap_alloc((sizeof(DWORD) + sizeof(DWORD)) * sd->num_zones);
349     if (!sd->zones) {
350         security_on_destroy(sd);
351         return FALSE;
352     }
353     sd->levels = &sd->zones[sd->num_zones];
354
355     /* use the same order as visible with native inetcpl.cpl */
356     add_zone_to_listview(sd, &lv_index, URLZONE_INTERNET);
357     add_zone_to_listview(sd, &lv_index, URLZONE_INTRANET);
358     add_zone_to_listview(sd, &lv_index, URLZONE_TRUSTED);
359     add_zone_to_listview(sd, &lv_index, URLZONE_UNTRUSTED);
360
361     for (i = 0; i < sd->num_zones; i++)
362     {
363         hr = IInternetZoneManager_GetZoneAt(sd->zone_mgr, sd->zone_enumerator, i, &current_zone);
364         if (SUCCEEDED(hr) && (current_zone != (DWORD)URLZONE_INVALID)) {
365             if (!current_zone || (current_zone > URLZONE_UNTRUSTED)) {
366                 add_zone_to_listview(sd, &lv_index, current_zone);
367             }
368         }
369     }
370     return TRUE;
371 }
372
373 /*********************************************************************
374  * security_on_notify [internal]
375  *
376  * handle WM_NOTIFY
377  *
378  */
379 static INT_PTR security_on_notify(secdlg_data *sd, WPARAM wparam, LPARAM lparam)
380 {
381     NMLISTVIEW *nm;
382
383     nm = (NMLISTVIEW *) lparam;
384     switch (nm->hdr.code)
385     {
386         case LVN_ITEMCHANGED:
387             TRACE("LVN_ITEMCHANGED (0x%lx, 0x%lx) from %p with code: %d (item: %d, uNewState: %u)\n",
388                     wparam, lparam, nm->hdr.hwndFrom, nm->hdr.code, nm->iItem, nm->uNewState);
389             if ((nm->uNewState & LVIS_SELECTED) == LVIS_SELECTED) {
390                 update_zone_info(sd, nm->iItem);
391             }
392             break;
393
394         case PSN_APPLY:
395             TRACE("PSN_APPLY (0x%lx, 0x%lx) from %p with code: %d\n", wparam, lparam,
396                     nm->hdr.hwndFrom, nm->hdr.code);
397             break;
398
399         default:
400             TRACE("WM_NOTIFY (0x%lx, 0x%lx) from %p with code: %d\n", wparam, lparam,
401                     nm->hdr.hwndFrom, nm->hdr.code);
402
403     }
404     return FALSE;
405 }
406
407 /*********************************************************************
408  * security_dlgproc [internal]
409  *
410  */
411 INT_PTR CALLBACK security_dlgproc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
412 {
413     secdlg_data *sd;
414
415     if (msg == WM_INITDIALOG) {
416         return security_on_initdialog(hwnd);
417     }
418
419     sd = (secdlg_data *)GetWindowLongPtrW(hwnd, DWLP_USER);
420     if (sd) {
421         switch (msg)
422         {
423             case WM_NOTIFY:
424                 return security_on_notify(sd, wparam, lparam);
425
426             case WM_NCDESTROY:
427                 return security_on_destroy(sd);
428
429             default:
430                 /* do not flood the log */
431                 if ((msg == WM_SETCURSOR) || (msg == WM_NCHITTEST) ||
432                     (msg == WM_MOUSEMOVE) || (msg == WM_MOUSEACTIVATE) || (msg == WM_PARENTNOTIFY))
433                     return FALSE;
434
435                 TRACE("(%p, 0x%08x/%03d, 0x%08lx, 0x%08lx)\n", hwnd, msg, msg, wparam, lparam);
436         }
437     }
438     return FALSE;
439 }