Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wine-mirror
GitHub Repository: wine-mirror/wine
Path: blob/master/dlls/aclui/aclui_main.c
4388 views
1
/*
2
* Implementation of the AclUI
3
*
4
* Copyright (C) 2009 Nikolay Sivov
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
#include <stdarg.h>
22
#define COBJMACROS
23
#include "initguid.h"
24
#include "windef.h"
25
#include "winbase.h"
26
#include "winuser.h"
27
#include "winnt.h"
28
#include "aclui.h"
29
#include "resource.h"
30
31
#include "wine/debug.h"
32
33
WINE_DEFAULT_DEBUG_CHANNEL(aclui);
34
35
/* the aclui.h files does not contain the necessary COBJMACROS */
36
#define ISecurityInformation_AddRef(This) (This)->lpVtbl->AddRef(This)
37
#define ISecurityInformation_Release(This) (This)->lpVtbl->Release(This)
38
#define ISecurityInformation_GetObjectInformation(This, obj) (This)->lpVtbl->GetObjectInformation(This, obj)
39
#define ISecurityInformation_GetSecurity(This, info, sd, def) (This)->lpVtbl->GetSecurity(This, info, sd, def)
40
#define ISecurityInformation_GetAccessRights(This, type, flags, access, count, def) (This)->lpVtbl->GetAccessRights(This, type, flags, access, count, def)
41
42
struct user
43
{
44
WCHAR *name;
45
PSID sid;
46
};
47
48
struct security_page
49
{
50
ISecurityInformation *security;
51
SI_OBJECT_INFO info;
52
PSECURITY_DESCRIPTOR sd;
53
54
SI_ACCESS *access;
55
ULONG access_count;
56
57
struct user *users;
58
unsigned int user_count;
59
60
HWND dialog;
61
HIMAGELIST image_list;
62
};
63
64
static HINSTANCE aclui_instance;
65
66
static WCHAR *WINAPIV load_formatstr(UINT resource, ...)
67
{
68
va_list valist;
69
WCHAR *str;
70
DWORD ret;
71
72
va_start(valist, resource);
73
ret = FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_HMODULE,
74
aclui_instance, resource, 0, (WCHAR*)&str, 0, &valist);
75
va_end(valist);
76
return ret ? str : NULL;
77
}
78
79
static WCHAR *get_sid_name(PSID sid, SID_NAME_USE *sid_type)
80
{
81
WCHAR *name, *domain;
82
DWORD domain_len = 0;
83
DWORD name_len = 0;
84
BOOL ret;
85
86
LookupAccountSidW(NULL, sid, NULL, &name_len, NULL, &domain_len, sid_type);
87
if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
88
return NULL;
89
if (!(name = malloc(name_len * sizeof(WCHAR))))
90
return NULL;
91
if (!(domain = malloc(domain_len * sizeof(WCHAR))))
92
{
93
free(name);
94
return NULL;
95
}
96
97
ret = LookupAccountSidW(NULL, sid, name, &name_len, domain, &domain_len, sid_type);
98
free(domain);
99
if (ret) return name;
100
free(name);
101
return NULL;
102
}
103
104
static void add_user(struct security_page *page, PSID sid)
105
{
106
struct user *new_array, *user;
107
SID_NAME_USE sid_type;
108
unsigned int i;
109
LVITEMW item;
110
WCHAR *name;
111
112
/* check if we already processed this user or group */
113
for (i = 0; i < page->user_count; ++i)
114
{
115
if (EqualSid(sid, page->users[i].sid))
116
return;
117
}
118
119
if (!(name = get_sid_name(sid, &sid_type)))
120
return;
121
122
if (!(new_array = realloc(page->users, (page->user_count + 1) * sizeof(*page->users))))
123
{
124
free(name);
125
return;
126
}
127
page->users = new_array;
128
user = &page->users[page->user_count++];
129
130
user->name = name;
131
user->sid = sid;
132
133
item.mask = LVIF_PARAM | LVIF_TEXT | LVIF_IMAGE;
134
item.iItem = -1;
135
item.iSubItem = 0;
136
item.pszText = name;
137
item.lParam = (LPARAM)user;
138
item.iImage = (sid_type == SidTypeGroup || sid_type == SidTypeWellKnownGroup) ? 0 : 1;
139
140
SendMessageW(GetDlgItem(page->dialog, IDC_USERS), LVM_INSERTITEMW, 0, (LPARAM)&item);
141
}
142
143
static PSID get_sid_from_ace(ACE_HEADER *ace)
144
{
145
switch (ace->AceType)
146
{
147
case ACCESS_ALLOWED_ACE_TYPE:
148
return &((ACCESS_ALLOWED_ACE *)ace)->SidStart;
149
case ACCESS_DENIED_ACE_TYPE:
150
return &((ACCESS_DENIED_ACE *)ace)->SidStart;
151
default:
152
FIXME("Unhandled ACE type %#x.\n", ace->AceType);
153
return NULL;
154
}
155
}
156
157
static void compute_access_masks(PSECURITY_DESCRIPTOR sd, PSID sid, ACCESS_MASK *allowed, ACCESS_MASK *denied)
158
{
159
BOOL defaulted, present;
160
ACE_HEADER *ace;
161
PSID ace_sid;
162
DWORD index;
163
ACL *dacl;
164
165
*allowed = 0;
166
*denied = 0;
167
168
if (!GetSecurityDescriptorDacl(sd, &present, &dacl, &defaulted) || !present)
169
return;
170
171
for (index = 0; index < dacl->AceCount; index++)
172
{
173
if (!GetAce(dacl, index, (void**)&ace))
174
break;
175
176
ace_sid = get_sid_from_ace(ace);
177
if (!ace_sid || !EqualSid(ace_sid, sid))
178
continue;
179
180
if (ace->AceType == ACCESS_ALLOWED_ACE_TYPE)
181
*allowed |= ((ACCESS_ALLOWED_ACE*)ace)->Mask;
182
else if (ace->AceType == ACCESS_DENIED_ACE_TYPE)
183
*denied |= ((ACCESS_DENIED_ACE*)ace)->Mask;
184
}
185
}
186
187
static void update_access_list(struct security_page *page, struct user *user)
188
{
189
ACCESS_MASK allowed, denied;
190
WCHAR *infotext;
191
ULONG i, index;
192
LVITEMW item;
193
HWND control;
194
195
compute_access_masks(page->sd, user->sid, &allowed, &denied);
196
197
if ((infotext = load_formatstr(IDS_PERMISSION_FOR, user->name)))
198
{
199
SetDlgItemTextW(page->dialog, IDC_ACE_USER, infotext);
200
LocalFree(infotext);
201
}
202
203
control = GetDlgItem(page->dialog, IDC_ACE);
204
index = 0;
205
for (i = 0; i < page->access_count; i++)
206
{
207
if (!(page->access[i].dwFlags & SI_ACCESS_GENERAL))
208
continue;
209
210
item.mask = LVIF_TEXT;
211
item.iItem = index;
212
213
item.iSubItem = 1;
214
if ((page->access[i].mask & allowed) == page->access[i].mask)
215
item.pszText = (WCHAR *)L"X";
216
else
217
item.pszText = (WCHAR *)L"-";
218
SendMessageW(control, LVM_SETITEMW, 0, (LPARAM)&item);
219
220
item.iSubItem = 2;
221
if ((page->access[i].mask & denied) == page->access[i].mask)
222
item.pszText = (WCHAR *)L"X";
223
else
224
item.pszText = (WCHAR *)L"-";
225
SendMessageW(control, LVM_SETITEMW, 0, (LPARAM)&item);
226
227
index++;
228
}
229
}
230
231
static void init_users(struct security_page *page)
232
{
233
BOOL defaulted, present;
234
ACE_HEADER *ace;
235
DWORD index;
236
ACL *dacl;
237
PSID sid;
238
239
if (!GetSecurityDescriptorDacl(page->sd, &present, &dacl, &defaulted))
240
{
241
ERR("Failed to query descriptor information, error %lu.\n", GetLastError());
242
return;
243
}
244
245
if (!present)
246
return;
247
248
for (index = 0; index < dacl->AceCount; index++)
249
{
250
if (!GetAce(dacl, index, (void **)&ace))
251
break;
252
if (!(sid = get_sid_from_ace(ace)))
253
continue;
254
add_user(page, sid);
255
}
256
}
257
258
static void init_access_list(struct security_page *page)
259
{
260
ULONG i, index;
261
WCHAR str[256];
262
LVITEMW item;
263
HWND control;
264
265
control = GetDlgItem(page->dialog, IDC_ACE);
266
index = 0;
267
for (i = 0; i < page->access_count; i++)
268
{
269
if (!(page->access[i].dwFlags & SI_ACCESS_GENERAL))
270
continue;
271
272
item.mask = LVIF_TEXT;
273
item.iItem = index;
274
item.iSubItem = 0;
275
if (IS_INTRESOURCE(page->access[i].pszName))
276
{
277
str[0] = 0;
278
LoadStringW(page->info.hInstance, (DWORD_PTR)page->access[i].pszName, str, ARRAY_SIZE(str));
279
item.pszText = str;
280
}
281
else
282
item.pszText = (WCHAR *)page->access[i].pszName;
283
SendMessageW(control, LVM_INSERTITEMW, 0, (LPARAM)&item);
284
285
index++;
286
}
287
}
288
289
static HIMAGELIST create_image_list(UINT resource, UINT width, UINT height, UINT count, COLORREF mask_color)
290
{
291
HIMAGELIST image_list;
292
HBITMAP image;
293
INT ret;
294
295
if (!(image_list = ImageList_Create(width, height, ILC_COLOR32 | ILC_MASK, 0, count)))
296
return NULL;
297
if (!(image = LoadBitmapW(aclui_instance, MAKEINTRESOURCEW(resource))))
298
{
299
ImageList_Destroy(image_list);
300
return NULL;
301
}
302
303
ret = ImageList_AddMasked(image_list, image, mask_color);
304
DeleteObject(image);
305
if (ret == -1)
306
{
307
ImageList_Destroy(image_list);
308
return NULL;
309
}
310
return image_list;
311
}
312
313
static void security_page_free(struct security_page *page)
314
{
315
unsigned int i;
316
317
for (i = 0; i < page->user_count; ++i)
318
free(page->users[i].name);
319
free(page->users);
320
321
LocalFree(page->sd);
322
if (page->image_list)
323
ImageList_Destroy(page->image_list);
324
if (page->security)
325
ISecurityInformation_Release(page->security);
326
free(page);
327
}
328
329
static void security_page_init_dlg(HWND hwnd, struct security_page *page)
330
{
331
LVCOLUMNW column;
332
HWND control;
333
HRESULT hr;
334
ULONG def;
335
RECT rect;
336
337
page->dialog = hwnd;
338
339
if (FAILED(hr = ISecurityInformation_GetSecurity(page->security,
340
DACL_SECURITY_INFORMATION, &page->sd, FALSE)))
341
{
342
ERR("Failed to get security descriptor, hr %#lx.\n", hr);
343
return;
344
}
345
346
if (FAILED(hr = ISecurityInformation_GetAccessRights(page->security,
347
NULL, 0, &page->access, &page->access_count, &def)))
348
{
349
ERR("Failed to get access mapping, hr %#lx.\n", hr);
350
return;
351
}
352
353
/* user list */
354
355
control = GetDlgItem(hwnd, IDC_USERS);
356
SendMessageW(control, LVM_SETEXTENDEDLISTVIEWSTYLE, LVS_EX_FULLROWSELECT, LVS_EX_FULLROWSELECT);
357
358
GetClientRect(control, &rect);
359
column.mask = LVCF_FMT | LVCF_WIDTH;
360
column.fmt = LVCFMT_LEFT;
361
column.cx = rect.right - rect.left;
362
SendMessageW(control, LVM_INSERTCOLUMNW, 0, (LPARAM)&column);
363
364
if (!(page->image_list = create_image_list(IDB_USER_ICONS, 18, 18, 2, RGB(255, 0, 255))))
365
return;
366
SendMessageW(control, LVM_SETIMAGELIST, LVSIL_SMALL, (LPARAM)page->image_list);
367
368
init_users(page);
369
370
/* ACE list */
371
372
control = GetDlgItem(hwnd, IDC_ACE);
373
SendMessageW(control, LVM_SETEXTENDEDLISTVIEWSTYLE, LVS_EX_FULLROWSELECT, LVS_EX_FULLROWSELECT);
374
375
column.mask = LVCF_FMT | LVCF_WIDTH;
376
column.fmt = LVCFMT_LEFT;
377
column.cx = 170;
378
SendMessageW(control, LVM_INSERTCOLUMNW, 0, (LPARAM)&column);
379
380
column.fmt = LVCFMT_CENTER;
381
column.cx = 85;
382
SendMessageW(control, LVM_INSERTCOLUMNW, 1, (LPARAM)&column);
383
SendMessageW(control, LVM_INSERTCOLUMNW, 2, (LPARAM)&column);
384
385
init_access_list(page);
386
387
if (page->user_count)
388
{
389
LVITEMW item;
390
item.mask = LVIF_STATE;
391
item.iItem = 0;
392
item.iSubItem = 0;
393
item.state = LVIS_FOCUSED | LVIS_SELECTED;
394
item.stateMask = item.state;
395
SendMessageW(control, LVM_SETITEMW, 0, (LPARAM)&item);
396
}
397
}
398
399
static INT_PTR CALLBACK security_page_proc(HWND dialog, UINT msg, WPARAM wparam, LPARAM lparam)
400
{
401
switch (msg)
402
{
403
case WM_INITDIALOG:
404
{
405
PROPSHEETPAGEW *propsheet = (PROPSHEETPAGEW *)lparam;
406
SetWindowLongPtrW(dialog, DWLP_USER, propsheet->lParam);
407
security_page_init_dlg(dialog, (struct security_page *)propsheet->lParam);
408
break;
409
}
410
411
case WM_NOTIFY:
412
{
413
struct security_page *page = (struct security_page *)GetWindowLongPtrW(dialog, DWLP_USER);
414
NMHDR *hdr = (NMHDR *)lparam;
415
416
if (hdr->hwndFrom == GetDlgItem(dialog, IDC_USERS) && hdr->code == LVN_ITEMCHANGED)
417
{
418
NMLISTVIEW *listview = (NMLISTVIEW *)lparam;
419
if (!(listview->uOldState & LVIS_SELECTED) && (listview->uNewState & LVIS_SELECTED))
420
update_access_list(page, (struct user *)listview->lParam);
421
return TRUE;
422
}
423
break;
424
}
425
}
426
return FALSE;
427
}
428
429
static UINT CALLBACK security_page_callback(HWND hwnd, UINT msg, PROPSHEETPAGEW *ppsp)
430
{
431
struct security_page *page = (struct security_page *)ppsp->lParam;
432
433
if (msg == PSPCB_RELEASE)
434
security_page_free(page);
435
436
return 1;
437
}
438
439
HPROPSHEETPAGE WINAPI CreateSecurityPage(ISecurityInformation *security)
440
{
441
struct security_page *page;
442
PROPSHEETPAGEW propsheet;
443
HPROPSHEETPAGE ret;
444
445
TRACE("%p\n", security);
446
447
InitCommonControls();
448
449
if (!(page = calloc(1, sizeof(*page))))
450
return NULL;
451
452
if (FAILED(ISecurityInformation_GetObjectInformation(security, &page->info)))
453
{
454
free(page);
455
return NULL;
456
}
457
458
page->security = security;
459
ISecurityInformation_AddRef(security);
460
461
memset(&propsheet, 0, sizeof(propsheet));
462
propsheet.dwSize = sizeof(propsheet);
463
propsheet.dwFlags = PSP_DEFAULT | PSP_USECALLBACK;
464
propsheet.hInstance = aclui_instance;
465
propsheet.pszTemplate = (WCHAR *)MAKEINTRESOURCE(IDD_SECURITY_PROPERTIES);
466
propsheet.pfnDlgProc = security_page_proc;
467
propsheet.pfnCallback = security_page_callback;
468
propsheet.lParam = (LPARAM)page;
469
470
if (page->info.dwFlags & SI_PAGE_TITLE)
471
{
472
propsheet.pszTitle = page->info.pszPageTitle;
473
propsheet.dwFlags |= PSP_USETITLE;
474
}
475
476
if (!(ret = CreatePropertySheetPageW(&propsheet)))
477
{
478
ERR("Failed to create property sheet page.\n");
479
ISecurityInformation_Release(security);
480
free(page);
481
return NULL;
482
}
483
return ret;
484
}
485
486
BOOL WINAPI EditSecurity(HWND owner, ISecurityInformation *security)
487
{
488
PROPSHEETHEADERW sheet = {0};
489
HPROPSHEETPAGE pages[1];
490
SI_OBJECT_INFO info;
491
BOOL ret;
492
493
TRACE("(%p, %p)\n", owner, security);
494
495
if (FAILED(ISecurityInformation_GetObjectInformation(security, &info)))
496
return FALSE;
497
if (!(pages[0] = CreateSecurityPage(security)))
498
return FALSE;
499
500
sheet.dwSize = sizeof(sheet);
501
sheet.dwFlags = PSH_DEFAULT;
502
sheet.hwndParent = owner;
503
sheet.hInstance = aclui_instance;
504
sheet.pszCaption = load_formatstr(IDS_PERMISSION_FOR, info.pszObjectName);
505
sheet.nPages = 1;
506
sheet.nStartPage = 0;
507
sheet.phpage = pages;
508
509
ret = PropertySheetW(&sheet) != -1;
510
LocalFree((void *)sheet.pszCaption);
511
return ret;
512
}
513
514
BOOL WINAPI DllMain(HINSTANCE instance, DWORD reason, void *reserved)
515
{
516
if (reason == DLL_PROCESS_ATTACH)
517
{
518
aclui_instance = instance;
519
DisableThreadLibraryCalls(instance);
520
}
521
return TRUE;
522
}
523
524