From 949f9a7ff637c8b3b4facb2ae7baca211af42917 Mon Sep 17 00:00:00 2001 From: Eric Kohl Date: Mon, 31 Jan 2005 12:49:46 +0000 Subject: [PATCH] - Implement DoesUserHavePrivilege and EnablePrivilege. - Fix IsUserAdmin. svn path=/trunk/; revision=13370 --- reactos/include/wine/setupapi.h | 2 + reactos/lib/setupapi/misc.c | 124 ++++++++++++++++++++++++++++- reactos/lib/setupapi/setupapi.spec | 4 +- 3 files changed, 126 insertions(+), 4 deletions(-) diff --git a/reactos/include/wine/setupapi.h b/reactos/include/wine/setupapi.h index bc534e23962..763b07202e7 100644 --- a/reactos/include/wine/setupapi.h +++ b/reactos/include/wine/setupapi.h @@ -666,7 +666,9 @@ DECL_WINELIB_SETUPAPI_TYPE_AW(PFILEPATHS) LONG WINAPI AddTagToGroupOrderList(PCWSTR lpGroupName, DWORD dwUnknown2, DWORD dwUnknown3); +BOOL WINAPI DoesUserHavePrivilege(PCWSTR lpPrivilegeName); PWSTR WINAPI DuplicateString(PCWSTR lpSrc); +BOOL WINAPI EnablePrivilege(PCWSTR lpPrivilegeName, BOOL bEnable); void WINAPI InstallHinfSectionA( HWND hwnd, HINSTANCE handle, PCSTR cmdline, INT show ); void WINAPI InstallHinfSectionW( HWND hwnd, HINSTANCE handle, PCWSTR cmdline, INT show ); #define InstallHinfSection WINELIB_NAME_AW(InstallHinfSection) diff --git a/reactos/lib/setupapi/misc.c b/reactos/lib/setupapi/misc.c index 56d6bd39c54..4a07712a991 100644 --- a/reactos/lib/setupapi/misc.c +++ b/reactos/lib/setupapi/misc.c @@ -206,12 +206,17 @@ BOOL WINAPI IsUserAdmin(VOID) TRACE("\n"); if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken)) + { return FALSE; + } if (!GetTokenInformation(hToken, TokenGroups, NULL, 0, &dwSize)) { - CloseHandle(hToken); - return FALSE; + if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) + { + CloseHandle(hToken); + return FALSE; + } } lpGroups = MyMalloc(dwSize); @@ -338,3 +343,118 @@ LPSTR WINAPI UnicodeToMultiByte(LPCWSTR lpUnicodeStr, UINT uCodePage) return lpMultiByteStr; } + + +/************************************************************************** + * DoesUserHavePrivilege [SETUPAPI.@] + * + * Check whether the current user has got a given privilege. + * + * PARAMS + * lpPrivilegeName [I] Name of the privilege to be checked + * + * RETURNS + * Success: TRUE + * Failure: FALSE + */ +BOOL WINAPI DoesUserHavePrivilege(LPCWSTR lpPrivilegeName) +{ + HANDLE hToken; + DWORD dwSize; + PTOKEN_PRIVILEGES lpPrivileges; + LUID PrivilegeLuid; + DWORD i; + BOOL bResult = FALSE; + + TRACE("%s\n", debugstr_w(lpPrivilegeName)); + + if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken)) + return FALSE; + + if (!GetTokenInformation(hToken, TokenPrivileges, NULL, 0, &dwSize)) + { + if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) + { + CloseHandle(hToken); + return FALSE; + } + } + + lpPrivileges = MyMalloc(dwSize); + if (lpPrivileges == NULL) + { + CloseHandle(hToken); + return FALSE; + } + + if (!GetTokenInformation(hToken, TokenPrivileges, lpPrivileges, dwSize, &dwSize)) + { + MyFree(lpPrivileges); + CloseHandle(hToken); + return FALSE; + } + + CloseHandle(hToken); + + if (!LookupPrivilegeValueW(NULL, lpPrivilegeName, &PrivilegeLuid)) + { + MyFree(lpPrivileges); + return FALSE; + } + + for (i = 0; i < lpPrivileges->PrivilegeCount; i++) + { + if (lpPrivileges->Privileges[i].Luid.HighPart == PrivilegeLuid.HighPart && + lpPrivileges->Privileges[i].Luid.LowPart == PrivilegeLuid.LowPart) + { + bResult = TRUE; + } + } + + MyFree(lpPrivileges); + + return bResult; +} + + +/************************************************************************** + * EnablePrivilege [SETUPAPI.@] + * + * Enables or disables one of the current users privileges. + * + * PARAMS + * lpPrivilegeName [I] Name of the privilege to be changed + * bEnable [I] TRUE: Enables the privilege + * FALSE: Disables the privilege + * + * RETURNS + * Success: TRUE + * Failure: FALSE + */ +BOOL WINAPI EnablePrivilege(LPCWSTR lpPrivilegeName, BOOL bEnable) +{ + TOKEN_PRIVILEGES Privileges; + HANDLE hToken; + BOOL bResult; + + TRACE("%s %s\n", debugstr_w(lpPrivilegeName), bEnable ? "TRUE" : "FALSE"); + + if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken)) + return FALSE; + + Privileges.PrivilegeCount = 1; + Privileges.Privileges[0].Attributes = (bEnable) ? SE_PRIVILEGE_ENABLED : 0; + + if (!LookupPrivilegeValueW(NULL, lpPrivilegeName, + &Privileges.Privileges[0].Luid)) + { + CloseHandle(hToken); + return FALSE; + } + + bResult = AdjustTokenPrivileges(hToken, FALSE, &Privileges, 0, NULL, NULL); + + CloseHandle(hToken); + + return bResult; +} diff --git a/reactos/lib/setupapi/setupapi.spec b/reactos/lib/setupapi/setupapi.spec index bed0ba5f6d0..9ecca648230 100644 --- a/reactos/lib/setupapi/setupapi.spec +++ b/reactos/lib/setupapi/setupapi.spec @@ -197,9 +197,9 @@ @ stub DelayedMove @ stub DelimStringToMultiSz @ stub DestroyTextFileReadBuffer -@ stub DoesUserHavePrivilege +@ stdcall DoesUserHavePrivilege(wstr) @ stdcall DuplicateString(wstr) -@ stub EnablePrivilege +@ stdcall EnablePrivilege(wstr long) @ stub ExtensionPropSheetPageProc @ stub FileExists @ stub FreeStringArray