[SYSDM] Only Administrators should see all user profiles and be able to copy or delete them

This commit is contained in:
Eric Kohl 2019-09-01 19:35:15 +02:00
parent 17bb6d10eb
commit 12e2039743
3 changed files with 66 additions and 38 deletions

View file

@ -17,6 +17,7 @@
#include <tchar.h> #include <tchar.h>
#include <shellapi.h> #include <shellapi.h>
#include <shlobj.h> #include <shlobj.h>
#include <setupapi.h>
#include "resource.h" #include "resource.h"

View file

@ -11,7 +11,6 @@
#include "precomp.h" #include "precomp.h"
#include <shlwapi.h> #include <shlwapi.h>
#include <setupapi.h>
typedef struct _STARTINFO typedef struct _STARTINFO
{ {

View file

@ -325,7 +325,7 @@ SetListViewColumns(
static VOID static VOID
AddUserProfile( AddUserProfile(
_In_ HWND hwndListView, _In_ HWND hwndListView,
_In_ LPTSTR lpProfileSid, _In_ PSID pProfileSid,
_In_ PSID pMySid, _In_ PSID pMySid,
_In_ HKEY hProfileKey) _In_ HKEY hProfileKey)
{ {
@ -336,18 +336,13 @@ AddUserProfile(
DWORD dwAccountNameSize, dwDomainNameSize; DWORD dwAccountNameSize, dwDomainNameSize;
DWORD dwProfileData, dwSize, dwType, dwState = 0; DWORD dwProfileData, dwSize, dwType, dwState = 0;
PWSTR ptr; PWSTR ptr;
PSID pSid = NULL;
INT nId, iItem; INT nId, iItem;
LV_ITEM lvi; LV_ITEM lvi;
if (!ConvertStringSidToSid(lpProfileSid,
&pSid))
return;
dwAccountNameSize = ARRAYSIZE(szAccountName); dwAccountNameSize = ARRAYSIZE(szAccountName);
dwDomainNameSize = ARRAYSIZE(szDomainName); dwDomainNameSize = ARRAYSIZE(szDomainName);
if (!LookupAccountSidW(NULL, if (!LookupAccountSidW(NULL,
pSid, pProfileSid,
szAccountName, szAccountName,
&dwAccountNameSize, &dwAccountNameSize,
szDomainName, szDomainName,
@ -361,7 +356,7 @@ AddUserProfile(
{ {
/* Show only the user accounts */ /* Show only the user accounts */
if (Use != SidTypeUser) if (Use != SidTypeUser)
goto done; return;
if (szAccountName[0] == UNICODE_NULL) if (szAccountName[0] == UNICODE_NULL)
{ {
@ -394,9 +389,9 @@ AddUserProfile(
0, 0,
dwProfileData); dwProfileData);
if (pProfileData == NULL) if (pProfileData == NULL)
goto done; return;
pProfileData->bMyProfile = EqualSid(pMySid, pSid); pProfileData->bMyProfile = EqualSid(pMySid, pProfileSid);
pProfileData->dwState = dwState; pProfileData->dwState = dwState;
ptr = (PWSTR)((ULONG_PTR)pProfileData + sizeof(PROFILEDATA)); ptr = (PWSTR)((ULONG_PTR)pProfileData + sizeof(PROFILEDATA));
@ -423,10 +418,6 @@ AddUserProfile(
LoadStringW(hApplet, nId, szAccountName, ARRAYSIZE(szAccountName)); LoadStringW(hApplet, nId, szAccountName, ARRAYSIZE(szAccountName));
ListView_SetItemText(hwndListView, iItem, 2, szAccountName); ListView_SetItemText(hwndListView, iItem, 2, szAccountName);
done:
if (pSid != NULL)
LocalFree(pSid);
} }
@ -450,7 +441,7 @@ UpdateButtonState(
if (Item.lParam != 0) if (Item.lParam != 0)
{ {
bMyProfile = ((PPROFILEDATA)Item.lParam)->bMyProfile; bMyProfile = ((PPROFILEDATA)Item.lParam)->bMyProfile;
if (/*IsUserAnAdmin() &&*/ !bMyProfile) if (!bMyProfile)
{ {
EnableWindow(GetDlgItem(hwndDlg, IDC_USERPROFILE_DELETE), TRUE); EnableWindow(GetDlgItem(hwndDlg, IDC_USERPROFILE_DELETE), TRUE);
EnableWindow(GetDlgItem(hwndDlg, IDC_USERPROFILE_COPY), TRUE); EnableWindow(GetDlgItem(hwndDlg, IDC_USERPROFILE_COPY), TRUE);
@ -471,7 +462,8 @@ UpdateButtonState(
static VOID static VOID
AddUserProfiles( AddUserProfiles(
_In_ HWND hwndDlg, _In_ HWND hwndDlg,
_In_ HWND hwndListView) _In_ HWND hwndListView,
_In_ BOOL bAdmin)
{ {
HKEY hKeyUserProfiles = INVALID_HANDLE_VALUE; HKEY hKeyUserProfiles = INVALID_HANDLE_VALUE;
HKEY hProfileKey; HKEY hProfileKey;
@ -482,6 +474,8 @@ AddUserProfiles(
DWORD dwSize; DWORD dwSize;
HANDLE hToken = NULL; HANDLE hToken = NULL;
PTOKEN_USER pTokenUser = NULL; PTOKEN_USER pTokenUser = NULL;
PSID pProfileSid;
PWSTR pszProfileSid;
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken)) if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken))
return; return;
@ -504,32 +498,57 @@ AddUserProfiles(
&hKeyUserProfiles)) &hKeyUserProfiles))
goto done; goto done;
for (dwIndex = 0; ; dwIndex++) if (bAdmin)
{ {
dwSidLength = ARRAYSIZE(szProfileSid); for (dwIndex = 0; ; dwIndex++)
if (RegEnumKeyExW(hKeyUserProfiles,
dwIndex,
szProfileSid,
&dwSidLength,
NULL,
NULL,
NULL,
&ftLastWrite))
break;
if (RegOpenKeyExW(hKeyUserProfiles,
szProfileSid,
0,
KEY_READ,
&hProfileKey) == ERROR_SUCCESS)
{ {
AddUserProfile(hwndListView, szProfileSid, pTokenUser->User.Sid, hProfileKey); dwSidLength = ARRAYSIZE(szProfileSid);
RegCloseKey(hProfileKey); if (RegEnumKeyExW(hKeyUserProfiles,
dwIndex,
szProfileSid,
&dwSidLength,
NULL,
NULL,
NULL,
&ftLastWrite))
break;
if (RegOpenKeyExW(hKeyUserProfiles,
szProfileSid,
0,
KEY_READ,
&hProfileKey) == ERROR_SUCCESS)
{
if (ConvertStringSidToSid(szProfileSid, &pProfileSid))
{
AddUserProfile(hwndListView, pProfileSid, pTokenUser->User.Sid, hProfileKey);
LocalFree(pProfileSid);
}
RegCloseKey(hProfileKey);
}
}
}
else
{
if (ConvertSidToStringSidW(pTokenUser->User.Sid, &pszProfileSid))
{
if (RegOpenKeyExW(hKeyUserProfiles,
pszProfileSid,
0,
KEY_READ,
&hProfileKey) == ERROR_SUCCESS)
{
AddUserProfile(hwndListView, pTokenUser->User.Sid, pTokenUser->User.Sid, hProfileKey);
RegCloseKey(hProfileKey);
}
LocalFree(pszProfileSid);
} }
} }
if (ListView_GetItemCount(hwndListView) != 0) if (ListView_GetItemCount(hwndListView) != 0)
ListView_SetItemState(hwndListView, 0, LVIS_SELECTED, LVIS_SELECTED); ListView_SetItemState(hwndListView, 0, LVIS_SELECTED | LVIS_FOCUSED, LVIS_SELECTED | LVIS_FOCUSED);
UpdateButtonState(hwndDlg, hwndListView); UpdateButtonState(hwndDlg, hwndListView);
@ -548,10 +567,19 @@ done:
static VOID static VOID
OnInitUserProfileDialog(HWND hwndDlg) OnInitUserProfileDialog(HWND hwndDlg)
{ {
BOOL bAdmin;
bAdmin = IsUserAdmin();
/* Initialize the list view control */ /* Initialize the list view control */
SetListViewColumns(GetDlgItem(hwndDlg, IDC_USERPROFILE_LIST)); SetListViewColumns(GetDlgItem(hwndDlg, IDC_USERPROFILE_LIST));
AddUserProfiles(hwndDlg, GetDlgItem(hwndDlg, IDC_USERPROFILE_LIST)); /* Hide the delete and copy buttons for non-admins */
ShowWindow(GetDlgItem(hwndDlg, IDC_USERPROFILE_DELETE), bAdmin ? SW_SHOW : SW_HIDE);
ShowWindow(GetDlgItem(hwndDlg, IDC_USERPROFILE_COPY), bAdmin ? SW_SHOW : SW_HIDE);
/* Add the profiles to the list view */
AddUserProfiles(hwndDlg, GetDlgItem(hwndDlg, IDC_USERPROFILE_LIST), bAdmin);
} }