ReactOS 0.4.16-dev-1972-gf20c09f
utils.c
Go to the documentation of this file.
1/*
2 * PROJECT: ReactOS Logon GINA DLL msgina.dll
3 * LICENSE: GPL-2.0-or-later (https://spdx.org/licenses/GPL-2.0-or-later)
4 * PURPOSE: Miscellaneous utility functions.
5 * COPYRIGHT: Copyright 2006 Hervé Poussineau <hpoussin@reactos.org>
6 * Copyright 2014 Eric Kohl <eric.kohl@reactos.org>
7 * Copyright 2025 Hermès Bélusca-Maïto <hermes.belusca-maito@reactos.org>
8 */
9
10#include "msgina.h"
11
30LONG
33 _In_ REGSAM samDesired,
34 _Out_ PHKEY phkResult)
35{
36 LONG rc;
37
38 /* Impersonate the logged-on user if necessary */
40 {
41 rc = GetLastError();
42 ERR("ImpersonateLoggedOnUser() failed with error %ld\n", rc);
43 return rc;
44 }
45
46 /* Open the logged-on user HKCU key */
47 rc = RegOpenCurrentUser(samDesired, phkResult);
48
49 /* Revert the impersonation */
50 if (hUserToken)
52
53 return rc;
54}
55
56LONG
61{
62 LONG rc;
63 DWORD dwType;
64 DWORD cbData = 0;
66
67 *pValue = NULL;
68 rc = RegQueryValueExW(hKey, pszValue, NULL, &dwType, NULL, &cbData);
69 if (rc != ERROR_SUCCESS)
70 return rc;
71 if (dwType != REG_SZ)
73 Value = HeapAlloc(GetProcessHeap(), 0, cbData + sizeof(WCHAR));
74 if (!Value)
77 if (rc != ERROR_SUCCESS)
78 {
80 return rc;
81 }
82 /* NULL-terminate the string */
83 Value[cbData / sizeof(WCHAR)] = UNICODE_NULL;
84
85 *pValue = Value;
86 return ERROR_SUCCESS;
87}
88
89LONG
94{
95 LONG rc;
96 DWORD dwValue, dwType, cbData;
97 /* Buffer big enough to hold the NULL-terminated string L"4294967295",
98 * corresponding to the literal 0xFFFFFFFF (MAXULONG) in decimal. */
99 WCHAR Buffer[sizeof("4294967295")];
100 C_ASSERT(sizeof(Buffer) >= sizeof(DWORD));
101
102 cbData = sizeof(Buffer);
103 rc = RegQueryValueExW(hKey, pszValue, NULL, &dwType, (PBYTE)&Buffer, &cbData);
104 if (rc != ERROR_SUCCESS)
105 return rc;
106
107 if (dwType == REG_DWORD)
108 {
109 if (cbData != sizeof(dwValue))
110 return ERROR_INVALID_DATA; // ERROR_DATATYPE_MISMATCH;
111 dwValue = *(PDWORD)Buffer;
112 }
113 else if (dwType == REG_SZ)
114 {
115 PWCHAR pEnd = NULL;
116 Buffer[cbData / sizeof(WCHAR) - 1] = UNICODE_NULL;
117 dwValue = wcstoul(Buffer, &pEnd, 0);
118 if (*pEnd) // Don't consider REG_SZ to be supported in this case!
120 }
121 else
122 {
124 }
125
126 *pValue = dwValue;
127 return ERROR_SUCCESS;
128}
129
139BOOL
141 _In_opt_ HANDLE hToken,
143{
144 LUID PrivilegeLuid = {Privilege, 0};
145 HANDLE hNewToken = NULL;
146 PTOKEN_PRIVILEGES pTokenPriv;
148 BOOL ret = FALSE;
149
150 if (!hToken)
151 {
152 /* Open effective token */
154 if (!ret && (GetLastError() == ERROR_NO_TOKEN))
156 if (!ret || !hNewToken)
157 return FALSE;
158 hToken = hNewToken;
159 }
160
161 dwLength = 0;
164 goto Quit;
165
166 ret = FALSE;
168 if (!pTokenPriv)
169 goto Quit;
170
171 if (GetTokenInformation(hToken, TokenPrivileges, pTokenPriv, dwLength, &dwLength))
172 {
173 DWORD i, cPrivs = pTokenPriv->PrivilegeCount;
174 for (i = 0; !ret && i < cPrivs; ++i)
175 {
176 ret = RtlEqualLuid(&PrivilegeLuid, &pTokenPriv->Privileges[i].Luid);
177 }
178 }
179
180 LocalFree(pTokenPriv);
181
182Quit:
183 if (hToken == hNewToken)
184 CloseHandle(hNewToken);
185 return ret;
186}
187
188PWSTR
191{
192 PWSTR NewStr;
193 SIZE_T cb;
194
195 if (!Str)
196 return NULL;
197
198 cb = (wcslen(Str) + 1) * sizeof(WCHAR);
199 if ((NewStr = LocalAlloc(LMEM_FIXED, cb)))
200 memcpy(NewStr, Str, cb);
201 return NewStr;
202}
HANDLE hUserToken
Definition: install.c:39
#define ERR(fmt,...)
Definition: precomp.h:57
Definition: bufpool.h:45
#define ERROR_NOT_ENOUGH_MEMORY
Definition: dderror.h:7
#define ERROR_INSUFFICIENT_BUFFER
Definition: dderror.h:10
#define ERROR_SUCCESS
Definition: deptool.c:10
#define NULL
Definition: types.h:112
#define FALSE
Definition: types.h:117
LONG WINAPI RegOpenCurrentUser(IN REGSAM samDesired, OUT PHKEY phkResult)
Definition: reg.c:3209
LONG WINAPI RegQueryValueExW(_In_ HKEY hkeyorg, _In_ LPCWSTR name, _In_ LPDWORD reserved, _In_ LPDWORD type, _In_ LPBYTE data, _In_ LPDWORD count)
Definition: reg.c:4103
BOOL WINAPI ImpersonateLoggedOnUser(HANDLE hToken)
Definition: misc.c:152
BOOL WINAPI GetTokenInformation(HANDLE TokenHandle, TOKEN_INFORMATION_CLASS TokenInformationClass, LPVOID TokenInformation, DWORD TokenInformationLength, PDWORD ReturnLength)
Definition: security.c:411
BOOL WINAPI OpenProcessToken(HANDLE ProcessHandle, DWORD DesiredAccess, PHANDLE TokenHandle)
Definition: security.c:294
BOOL WINAPI OpenThreadToken(HANDLE ThreadHandle, DWORD DesiredAccess, BOOL OpenAsSelf, HANDLE *TokenHandle)
Definition: security.c:336
#define CloseHandle
Definition: compat.h:739
#define GetProcessHeap()
Definition: compat.h:736
#define HeapAlloc
Definition: compat.h:733
#define GetCurrentProcess()
Definition: compat.h:759
#define HeapFree(x, y, z)
Definition: compat.h:735
static DWORD DWORD * dwLength
Definition: fusion.c:86
BOOL WINAPI RevertToSelf(void)
Definition: security.c:855
BOOL TestTokenPrivilege(_In_opt_ HANDLE hToken, _In_ ULONG Privilege)
Verifies whether the specified token has the given privilege.
Definition: utils.c:140
LONG ReadRegDwordValue(_In_ HKEY hKey, _In_ PCWSTR pszValue, _Out_ PDWORD pValue)
Definition: utils.c:90
LONG RegOpenLoggedOnHKCU(_In_opt_ HANDLE hUserToken, _In_ REGSAM samDesired, _Out_ PHKEY phkResult)
Opens and retrieves a handle to the HKEY_CURRENT_USER corresponding to the specified logged-on user.
Definition: utils.c:31
LONG ReadRegSzValue(_In_ HKEY hKey, _In_ PCWSTR pszValue, _Out_ PWSTR *pValue)
Definition: utils.c:57
return ret
Definition: mutex.c:146
unsigned int BOOL
Definition: ntddk_ex.h:94
unsigned long DWORD
Definition: ntddk_ex.h:95
FxAutoRegKey hKey
PWCHAR pValue
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
HLOCAL NTAPI LocalAlloc(UINT uFlags, SIZE_T dwBytes)
Definition: heapmem.c:1390
HLOCAL NTAPI LocalFree(HLOCAL hMem)
Definition: heapmem.c:1594
_CRTIMP size_t __cdecl wcslen(_In_z_ const wchar_t *_Str)
#define C_ASSERT(e)
Definition: intsafe.h:73
#define REG_SZ
Definition: layer.c:22
#define LPTR
Definition: minwinbase.h:93
#define LMEM_FIXED
Definition: minwinbase.h:81
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
static HMODULE MODULEINFO DWORD cb
Definition: module.c:33
#define _Out_
Definition: no_sal2.h:160
#define _In_
Definition: no_sal2.h:158
#define _In_opt_
Definition: no_sal2.h:212
#define UNICODE_NULL
BYTE * PBYTE
Definition: pedump.c:66
DWORD * PDWORD
Definition: pedump.c:68
long LONG
Definition: pedump.c:60
_In_opt_ _In_opt_ _In_ _In_ DWORD cbData
Definition: shlwapi.h:761
_In_opt_ LPCSTR _In_opt_ LPCSTR pszValue
Definition: shlwapi.h:783
#define REG_DWORD
Definition: sdbapi.c:615
_Check_return_ unsigned long __cdecl wcstoul(_In_z_ const wchar_t *_Str, _Out_opt_ _Deref_post_z_ wchar_t **_EndPtr, _In_ int _Radix)
#define DuplicateString(x)
Definition: stringutils.h:45
$ULONG PrivilegeCount
Definition: setypes.h:1035
LUID_AND_ATTRIBUTES Privileges[ANYSIZE_ARRAY]
Definition: setypes.h:1036
uint16_t * PWSTR
Definition: typedefs.h:56
const uint16_t * PCWSTR
Definition: typedefs.h:57
ULONG_PTR SIZE_T
Definition: typedefs.h:80
uint16_t * PWCHAR
Definition: typedefs.h:56
uint32_t ULONG
Definition: typedefs.h:59
BOOL Privilege(LPTSTR pszPrivilege, BOOL bEnable)
Definition: user_lib.cpp:531
_Must_inspect_result_ _In_ WDFKEY _In_ PCUNICODE_STRING _Out_opt_ PUSHORT _Inout_opt_ PUNICODE_STRING Value
Definition: wdfregistry.h:413
DWORD WINAPI GetLastError(void)
Definition: except.c:1042
HANDLE WINAPI GetCurrentThread(void)
Definition: proc.c:1148
#define ERROR_UNSUPPORTED_TYPE
Definition: winerror.h:1336
#define ERROR_NO_TOKEN
Definition: winerror.h:911
#define ERROR_INVALID_DATA
Definition: winerror.h:238
ACCESS_MASK REGSAM
Definition: winreg.h:76
#define RtlEqualLuid(Luid1, Luid2)
Definition: rtlfuncs.h:304
#define TOKEN_QUERY
Definition: setypes.h:940
@ TokenPrivileges
Definition: setypes.h:980
struct _TOKEN_PRIVILEGES * PTOKEN_PRIVILEGES
__wchar_t WCHAR
Definition: xmlstorage.h:180