ReactOS 0.4.16-dev-1946-g52006dd
utils.c File Reference
#include "msgina.h"
Include dependency graph for utils.c:

Go to the source code of this file.

Functions

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.
 
LONG ReadRegSzValue (_In_ HKEY hKey, _In_ PCWSTR pszValue, _Out_ PWSTR *pValue)
 
LONG ReadRegDwordValue (_In_ HKEY hKey, _In_ PCWSTR pszValue, _Out_ PDWORD pValue)
 
BOOL TestTokenPrivilege (_In_opt_ HANDLE hToken, _In_ ULONG Privilege)
 Verifies whether the specified token has the given privilege.
 
PWSTR DuplicateString (_In_opt_ PCWSTR Str)
 

Function Documentation

◆ DuplicateString()

PWSTR DuplicateString ( _In_opt_ PCWSTR  Str)

Definition at line 189 of file utils.c.

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}
#define NULL
Definition: types.h:112
HLOCAL NTAPI LocalAlloc(UINT uFlags, SIZE_T dwBytes)
Definition: heapmem.c:1390
_CRTIMP size_t __cdecl wcslen(_In_z_ const wchar_t *_Str)
#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
uint16_t * PWSTR
Definition: typedefs.h:56
ULONG_PTR SIZE_T
Definition: typedefs.h:80
__wchar_t WCHAR
Definition: xmlstorage.h:180

◆ ReadRegDwordValue()

LONG ReadRegDwordValue ( _In_ HKEY  hKey,
_In_ PCWSTR  pszValue,
_Out_ PDWORD  pValue 
)

Definition at line 90 of file utils.c.

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}
Definition: bufpool.h:45
#define ERROR_SUCCESS
Definition: deptool.c:10
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
unsigned long DWORD
Definition: ntddk_ex.h:95
FxAutoRegKey hKey
PWCHAR pValue
#define C_ASSERT(e)
Definition: intsafe.h:73
#define REG_SZ
Definition: layer.c:22
#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)
uint16_t * PWCHAR
Definition: typedefs.h:56
#define ERROR_UNSUPPORTED_TYPE
Definition: winerror.h:1336
#define ERROR_INVALID_DATA
Definition: winerror.h:238

Referenced by GetPolicyDWORDValue(), GetRegistrySettings(), and OnInitSecurityDlg().

◆ ReadRegSzValue()

LONG ReadRegSzValue ( _In_ HKEY  hKey,
_In_ PCWSTR  pszValue,
_Out_ PWSTR pValue 
)

Definition at line 57 of file utils.c.

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}
#define ERROR_NOT_ENOUGH_MEMORY
Definition: dderror.h:7
#define GetProcessHeap()
Definition: compat.h:736
#define HeapAlloc
Definition: compat.h:733
#define HeapFree(x, y, z)
Definition: compat.h:735
_Must_inspect_result_ _In_ WDFKEY _In_ PCUNICODE_STRING _Out_opt_ PUSHORT _Inout_opt_ PUNICODE_STRING Value
Definition: wdfregistry.h:413

Referenced by ChooseGinaUI(), and GUILoggedOutSAS().

◆ RegOpenLoggedOnHKCU()

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.

Parameters
[in]hUserTokenOptional handle to a primary or impersonation access token that represents a logged-on user. See ImpersonateLoggedOnUser() for more information. If NULL, opens the SYSTEM's HKEY_USERS.Default (i.e. HKEY_USERS\S-1-5-18).
[in]samDesiredA mask (type: REGSAM or ACCESS_MASK) that specifies the desired access rights to the key. See RegOpenCurrentUser() for more information.
[out]phkResultA pointer to a variable that receives a handle to the opened key. When the handle is no longer needed, close it with RegCloseKey().

Definition at line 31 of file utils.c.

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}
HANDLE hUserToken
Definition: install.c:39
#define ERR(fmt,...)
Definition: precomp.h:57
LONG WINAPI RegOpenCurrentUser(IN REGSAM samDesired, OUT PHKEY phkResult)
Definition: reg.c:3209
BOOL WINAPI ImpersonateLoggedOnUser(HANDLE hToken)
Definition: misc.c:152
BOOL WINAPI RevertToSelf(void)
Definition: security.c:855
DWORD WINAPI GetLastError(void)
Definition: except.c:1042

Referenced by OnInitSecurityDlg(), OnShutDown(), and WlxActivateUserShell().

◆ TestTokenPrivilege()

BOOL TestTokenPrivilege ( _In_opt_ HANDLE  hToken,
_In_ ULONG  Privilege 
)

Verifies whether the specified token has the given privilege.

See also
shell32!SHTestTokenPrivilegeW(), http://undoc.airesoft.co.uk/shell32.dll/SHTestTokenPrivilegeW.php and setupapi!DoesUserHavePrivilege().

Definition at line 140 of file utils.c.

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}
#define ERROR_INSUFFICIENT_BUFFER
Definition: dderror.h:10
#define FALSE
Definition: types.h:117
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 GetCurrentProcess()
Definition: compat.h:759
static DWORD DWORD * dwLength
Definition: fusion.c:86
return ret
Definition: mutex.c:146
unsigned int BOOL
Definition: ntddk_ex.h:94
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 LocalFree(HLOCAL hMem)
Definition: heapmem.c:1594
#define LPTR
Definition: minwinbase.h:93
$ULONG PrivilegeCount
Definition: setypes.h:1035
LUID_AND_ATTRIBUTES Privileges[ANYSIZE_ARRAY]
Definition: setypes.h:1036
BOOL Privilege(LPTSTR pszPrivilege, BOOL bEnable)
Definition: user_lib.cpp:531
HANDLE WINAPI GetCurrentThread(void)
Definition: proc.c:1148
#define ERROR_NO_TOKEN
Definition: winerror.h:911
#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

Referenced by GetAllowedShutdownOptions(), and OnInitSecurityDlg().