aclui: Populate the users list.
[wine.git] / dlls / aclui / aclui_main.c
blob199e7295adcdcaf990fc0696dabca199ed3494c8
1 /*
2 * Implementation of the AclUI
4 * Copyright (C) 2009 Nikolay Sivov
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.
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.
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
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"
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(aclui);
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)
42 struct user
44 WCHAR *name;
45 PSID sid;
48 struct security_page
50 ISecurityInformation *security;
51 SI_OBJECT_INFO info;
52 PSECURITY_DESCRIPTOR sd;
54 struct user *users;
55 unsigned int user_count;
57 HWND dialog;
60 static HINSTANCE aclui_instance;
62 static WCHAR *get_sid_name(PSID sid, SID_NAME_USE *sid_type)
64 WCHAR *name, *domain;
65 DWORD domain_len = 0;
66 DWORD name_len = 0;
67 BOOL ret;
69 LookupAccountSidW(NULL, sid, NULL, &name_len, NULL, &domain_len, sid_type);
70 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
71 return NULL;
72 if (!(name = malloc(name_len * sizeof(WCHAR))))
73 return NULL;
74 if (!(domain = malloc(domain_len * sizeof(WCHAR))))
76 free(name);
77 return NULL;
80 ret = LookupAccountSidW(NULL, sid, name, &name_len, domain, &domain_len, sid_type);
81 free(domain);
82 if (ret) return name;
83 free(name);
84 return NULL;
87 static void add_user(struct security_page *page, PSID sid)
89 struct user *new_array, *user;
90 SID_NAME_USE sid_type;
91 unsigned int i;
92 LVITEMW item;
93 WCHAR *name;
95 /* check if we already processed this user or group */
96 for (i = 0; i < page->user_count; ++i)
98 if (EqualSid(sid, page->users[i].sid))
99 return;
102 if (!(name = get_sid_name(sid, &sid_type)))
103 return;
105 if (!(new_array = realloc(page->users, (page->user_count + 1) * sizeof(*page->users))))
106 return;
107 page->users = new_array;
108 user = &page->users[page->user_count++];
110 user->name = name;
111 user->sid = sid;
113 item.mask = LVIF_PARAM | LVIF_TEXT;
114 item.iItem = -1;
115 item.iSubItem = 0;
116 item.pszText = name;
117 item.lParam = (LPARAM)user;
119 SendMessageW(GetDlgItem(page->dialog, IDC_USERS), LVM_INSERTITEMW, 0, (LPARAM)&item);
122 static PSID get_sid_from_ace(ACE_HEADER *ace)
124 switch (ace->AceType)
126 case ACCESS_ALLOWED_ACE_TYPE:
127 return &((ACCESS_ALLOWED_ACE *)ace)->SidStart;
128 case ACCESS_DENIED_ACE_TYPE:
129 return &((ACCESS_DENIED_ACE *)ace)->SidStart;
130 default:
131 FIXME("Unhandled ACE type %#x.\n", ace->AceType);
132 return NULL;
136 static void init_users(struct security_page *page)
138 BOOL defaulted, present;
139 ACE_HEADER *ace;
140 DWORD index;
141 ACL *dacl;
142 PSID sid;
144 if (!GetSecurityDescriptorDacl(page->sd, &present, &dacl, &defaulted))
146 ERR("Failed to query descriptor information, error %u.\n", GetLastError());
147 return;
150 if (!present)
151 return;
153 for (index = 0; index < dacl->AceCount; index++)
155 if (!GetAce(dacl, index, (void **)&ace))
156 break;
157 if (!(sid = get_sid_from_ace(ace)))
158 continue;
159 add_user(page, sid);
163 static void security_page_free(struct security_page *page)
165 unsigned int i;
167 for (i = 0; i < page->user_count; ++i)
168 free(page->users[i].name);
169 free(page->users);
171 LocalFree(page->sd);
172 if (page->security)
173 ISecurityInformation_Release(page->security);
174 free(page);
177 static void security_page_init_dlg(HWND hwnd, struct security_page *page)
179 LVCOLUMNW column;
180 HWND control;
181 HRESULT hr;
182 RECT rect;
184 page->dialog = hwnd;
186 if (FAILED(hr = ISecurityInformation_GetSecurity(page->security,
187 DACL_SECURITY_INFORMATION, &page->sd, FALSE)))
189 ERR("Failed to get security descriptor, hr %#x.\n", hr);
190 return;
193 control = GetDlgItem(hwnd, IDC_USERS);
194 SendMessageW(control, LVM_SETEXTENDEDLISTVIEWSTYLE, LVS_EX_FULLROWSELECT, LVS_EX_FULLROWSELECT);
196 GetClientRect(control, &rect);
197 column.mask = LVCF_FMT | LVCF_WIDTH;
198 column.fmt = LVCFMT_LEFT;
199 column.cx = rect.right - rect.left;
200 SendMessageW(control, LVM_INSERTCOLUMNW, 0, (LPARAM)&column);
202 init_users(page);
204 if (page->user_count)
206 LVITEMW item;
207 item.mask = LVIF_STATE;
208 item.iItem = 0;
209 item.iSubItem = 0;
210 item.state = LVIS_FOCUSED | LVIS_SELECTED;
211 item.stateMask = item.state;
212 SendMessageW(control, LVM_SETITEMW, 0, (LPARAM)&item);
216 static INT_PTR CALLBACK security_page_proc(HWND dialog, UINT msg, WPARAM wparam, LPARAM lparam)
218 switch (msg)
220 case WM_INITDIALOG:
222 PROPSHEETPAGEW *propsheet = (PROPSHEETPAGEW *)lparam;
223 security_page_init_dlg(dialog, (struct security_page *)propsheet->lParam);
224 break;
227 return FALSE;
230 static UINT CALLBACK security_page_callback(HWND hwnd, UINT msg, PROPSHEETPAGEW *ppsp)
232 struct security_page *page = (struct security_page *)ppsp->lParam;
234 if (msg == PSPCB_RELEASE)
235 security_page_free(page);
237 return 1;
240 HPROPSHEETPAGE WINAPI CreateSecurityPage(ISecurityInformation *security)
242 struct security_page *page;
243 PROPSHEETPAGEW propsheet;
244 HPROPSHEETPAGE ret;
246 TRACE("%p\n", security);
248 InitCommonControls();
250 if (!(page = calloc(1, sizeof(*page))))
251 return NULL;
253 if (FAILED(ISecurityInformation_GetObjectInformation(security, &page->info)))
255 free(page);
256 return NULL;
259 page->security = security;
260 ISecurityInformation_AddRef(security);
262 memset(&propsheet, 0, sizeof(propsheet));
263 propsheet.dwSize = sizeof(propsheet);
264 propsheet.dwFlags = PSP_DEFAULT | PSP_USECALLBACK;
265 propsheet.hInstance = aclui_instance;
266 propsheet.pszTemplate = (WCHAR *)MAKEINTRESOURCE(IDD_SECURITY_PROPERTIES);
267 propsheet.pfnDlgProc = security_page_proc;
268 propsheet.pfnCallback = security_page_callback;
269 propsheet.lParam = (LPARAM)page;
271 if (page->info.dwFlags & SI_PAGE_TITLE)
273 propsheet.pszTitle = page->info.pszPageTitle;
274 propsheet.dwFlags |= PSP_USETITLE;
277 if (!(ret = CreatePropertySheetPageW(&propsheet)))
279 ERR("Failed to create property sheet page.\n");
280 ISecurityInformation_Release(security);
281 free(page);
282 return NULL;
284 return ret;
287 BOOL WINAPI EditSecurity(HWND owner, LPSECURITYINFO psi)
289 FIXME("(%p, %p): stub\n", owner, psi);
290 return FALSE;
293 BOOL WINAPI DllMain(HINSTANCE instance, DWORD reason, void *reserved)
295 if (reason == DLL_PROCESS_ATTACH)
297 aclui_instance = instance;
298 DisableThreadLibraryCalls(instance);
300 return TRUE;