Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

216 lines
4.9 KiB

  1. /*++
  2. Copyright(c) 1995 Microsoft Corporation
  3. MODULE NAME
  4. reg.c
  5. ABSTRACT
  6. Registry routines for the automatic connection service.
  7. AUTHOR
  8. Anthony Discolo (adiscolo) 20-Mar-1995
  9. REVISION HISTORY
  10. Original version from Gurdeep
  11. --*/
  12. #define UNICODE
  13. #define _UNICODE
  14. #include <nt.h>
  15. #include <ntrtl.h>
  16. #include <nturtl.h>
  17. #include <stdlib.h>
  18. #include <windows.h>
  19. #include <stdio.h>
  20. #include <npapi.h>
  21. #include <acd.h>
  22. #include <debug.h>
  23. #include "reg.h"
  24. #include "misc.h"
  25. //
  26. // The maximum size of TOKEN_USER information.
  27. //
  28. #define TOKEN_INFORMATION_SIZE (sizeof (TOKEN_USER) + sizeof (SID) + (sizeof (ULONG) * SID_MAX_SUB_AUTHORITIES))
  29. HKEY
  30. GetHkeyCurrentUser(
  31. HANDLE hToken
  32. )
  33. {
  34. BOOLEAN fSuccess;
  35. HKEY hkey = NULL;
  36. UCHAR TokenInformation[TOKEN_INFORMATION_SIZE];
  37. DWORD dwReturnLength;
  38. UNICODE_STRING sidString, keyString;
  39. OBJECT_ATTRIBUTES objectAttributes;
  40. if (!GetTokenInformation(
  41. hToken,
  42. TokenUser,
  43. TokenInformation,
  44. sizeof (TokenInformation),
  45. &dwReturnLength))
  46. {
  47. RASAUTO_TRACE1(
  48. "GetHkeyCurrentUser: GetTokenInformation failed (error=%d)",
  49. GetLastError());
  50. return NULL;
  51. }
  52. if (RtlConvertSidToUnicodeString(
  53. &sidString,
  54. ((PTOKEN_USER)TokenInformation)->User.Sid,
  55. TRUE) != STATUS_SUCCESS)
  56. {
  57. RASAUTO_TRACE1(
  58. "GetHkeyCurrentUser: RtlConvertSidToUnicodeString failed (error=%d)",
  59. GetLastError());
  60. return NULL;
  61. }
  62. keyString.Length = 0;
  63. keyString.MaximumLength =
  64. sidString.Length + sizeof (L"\\REGISTRY\\USER\\") + sizeof (L"\0");
  65. keyString.Buffer = LocalAlloc(LPTR, keyString.MaximumLength);
  66. if (keyString.Buffer == NULL) {
  67. RASAUTO_TRACE("GetHkeyCurrentUser: LocalAlloc failed");
  68. RtlFreeUnicodeString(&sidString);
  69. return NULL;
  70. }
  71. //
  72. // Copy \REGISTRY\USER to keyString.
  73. //
  74. RtlAppendUnicodeToString(&keyString, L"\\REGISTRY\\USER\\");
  75. //
  76. // Append the user's SID to keyString.
  77. //
  78. if (RtlAppendUnicodeStringToString(
  79. &keyString,
  80. &sidString) != STATUS_SUCCESS)
  81. {
  82. RASAUTO_TRACE1(
  83. "GetHkeyCurrentUser: RtlAppendUnicodeToString failed (error=%d)",
  84. GetLastError());
  85. RtlFreeUnicodeString(&sidString);
  86. LocalFree(keyString.Buffer);
  87. return NULL;
  88. }
  89. RtlFreeUnicodeString(&sidString);
  90. RASAUTO_TRACE1(
  91. "GetHkeyCurrentUser: HKEY_CURRENT_USER is %S",
  92. keyString.Buffer);
  93. //
  94. // Initialize the object attributes.
  95. //
  96. InitializeObjectAttributes(
  97. &objectAttributes,
  98. &keyString,
  99. OBJ_CASE_INSENSITIVE,
  100. NULL,
  101. NULL);
  102. //
  103. // Open the registry key.
  104. //
  105. if (NtOpenKey(
  106. &hkey,
  107. MAXIMUM_ALLOWED,
  108. &objectAttributes) != STATUS_SUCCESS)
  109. {
  110. RASAUTO_TRACE1(
  111. "GetHkeyCurrentUser: NtOpenKey failed (error=%d)",
  112. GetLastError());
  113. LocalFree(keyString.Buffer);
  114. return NULL;
  115. }
  116. LocalFree(keyString.Buffer);
  117. return hkey;
  118. } // GetHkeyCurrentUser
  119. BOOLEAN
  120. RegGetValue(
  121. IN HKEY hkey,
  122. IN LPTSTR pszKey,
  123. OUT PVOID *ppvData,
  124. OUT LPDWORD pdwcbData,
  125. OUT LPDWORD pdwType
  126. )
  127. {
  128. DWORD dwError, dwType, dwSize;
  129. PVOID pvData;
  130. //
  131. // Get the length of the string.
  132. //
  133. dwSize = 0;
  134. dwError = RegQueryValueEx(
  135. hkey,
  136. pszKey,
  137. NULL,
  138. &dwType,
  139. NULL,
  140. &dwSize);
  141. if (dwError != ERROR_SUCCESS)
  142. return FALSE;
  143. pvData = LocalAlloc(LPTR, dwSize);
  144. if (pvData == NULL) {
  145. RASAUTO_TRACE("RegGetValue: LocalAlloc failed");
  146. return FALSE;
  147. }
  148. //
  149. // Read the value for real this time.
  150. //
  151. dwError = RegQueryValueEx(
  152. hkey,
  153. pszKey,
  154. NULL,
  155. NULL,
  156. (LPBYTE)pvData,
  157. &dwSize);
  158. if (dwError != ERROR_SUCCESS) {
  159. LocalFree(pvData);
  160. return FALSE;
  161. }
  162. if(NULL != pdwType)
  163. {
  164. *pdwType = dwType;
  165. }
  166. *ppvData = pvData;
  167. if (pdwcbData != NULL)
  168. *pdwcbData = dwSize;
  169. return TRUE;
  170. } // RegGetValue
  171. BOOLEAN
  172. RegGetDword(
  173. IN HKEY hkey,
  174. IN LPTSTR pszKey,
  175. OUT LPDWORD pdwValue
  176. )
  177. {
  178. DWORD dwError, dwType, dwSize = sizeof (DWORD);
  179. dwError = RegQueryValueEx(
  180. hkey,
  181. pszKey,
  182. NULL,
  183. &dwType,
  184. (LPBYTE)pdwValue,
  185. &dwSize);
  186. if (dwError != ERROR_SUCCESS || dwType != REG_DWORD)
  187. return FALSE;
  188. return TRUE;
  189. } // RegGetDword