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.

206 lines
4.9 KiB

  1. /*++
  2. Copyright (c) 1993-1995 Microsoft Corporation
  3. All rights reserved
  4. Module Name:
  5. reghand.c
  6. Abstract:
  7. Processes that do impersonation should not attempt to open
  8. per-process aliases like HKEY_CURRENT_USER. HKEY_CURRENT_USER
  9. has meaning only for end user programs that run in the context
  10. of a single local user.
  11. Server processes should not depend on predefined handles or any
  12. other per process state. It should determine whether
  13. the user (client) being impersonated is local or remote.
  14. Author:
  15. KrishnaG (20-May-93)
  16. Environment:
  17. User Mode -Win32
  18. Revision History:
  19. --*/
  20. #include "precomp.h"
  21. #pragma hdrstop
  22. //
  23. // Maximum size of TOKEN_USER information.
  24. //
  25. #define SIZE_OF_TOKEN_INFORMATION \
  26. sizeof( TOKEN_USER ) \
  27. + sizeof( SID ) \
  28. + sizeof( ULONG ) * SID_MAX_SUB_AUTHORITIES
  29. #define MAX_SID_STRING 256
  30. //
  31. // Function Declarations
  32. //
  33. BOOL
  34. InitClientUserString(
  35. LPWSTR pString
  36. );
  37. HKEY
  38. GetClientUserHandle(
  39. IN REGSAM samDesired
  40. )
  41. /*++
  42. Routine Description:
  43. Arguments:
  44. Returns:
  45. ---*/
  46. {
  47. HANDLE hKeyClient;
  48. WCHAR String[MAX_SID_STRING];
  49. LONG ReturnValue;
  50. if (!InitClientUserString(String)) {
  51. DBGMSG( DBG_WARNING, ("GetClientUserHandle InitClientUserString failed %d\n", GetLastError() ));
  52. return NULL ;
  53. }
  54. //
  55. // We now have the Unicode string representation of the
  56. // local client's Sid we'll use this string to open a handle
  57. // to the client's key in the registry.
  58. ReturnValue = RegOpenKeyEx( HKEY_USERS,
  59. String,
  60. 0,
  61. samDesired,
  62. &hKeyClient );
  63. //
  64. // If we couldn't get a handle to the local key
  65. // for some reason, return a NULL handle indicating
  66. // failure to obtain a handle to the key
  67. //
  68. if ( ReturnValue != ERROR_SUCCESS ) {
  69. DBGMSG( DBG_TRACE, ( "GetClientUserHandle failed %d\n", ReturnValue ));
  70. SetLastError( ReturnValue );
  71. return NULL;
  72. }
  73. return( hKeyClient );
  74. }
  75. BOOL
  76. InitClientUserString (
  77. LPWSTR pString
  78. )
  79. /*++
  80. Routine Description:
  81. Arguments:
  82. pString - output string of current user
  83. Return Value:
  84. TRUE = success,
  85. FALSE = fail
  86. Returns in pString a ansi string if the impersonated client's
  87. SID can be expanded successfully into Unicode string. If the conversion
  88. was unsuccessful, returns FALSE.
  89. --*/
  90. {
  91. HANDLE TokenHandle;
  92. UCHAR TokenInformation[ SIZE_OF_TOKEN_INFORMATION ];
  93. ULONG ReturnLength;
  94. NTSTATUS NtStatus;
  95. BOOL Status;
  96. DWORD dwLastError;
  97. UNICODE_STRING UnicodeString;
  98. //
  99. // We can use OpenThreadToken because this server thread
  100. // is impersonating a client
  101. //
  102. Status = OpenThreadToken( GetCurrentThread(),
  103. TOKEN_READ,
  104. TRUE, // Open as self
  105. &TokenHandle
  106. );
  107. if( Status == FALSE ) {
  108. DBGMSG(DBG_WARNING, ("InitClientUserString: OpenThreadToken failed: Error %d\n",
  109. GetLastError()));
  110. return FALSE ;
  111. }
  112. //
  113. // Notice that we've allocated enough space for the
  114. // TokenInformation structure. so if we fail, we
  115. // return a NULL pointer indicating failure
  116. //
  117. Status = GetTokenInformation( TokenHandle,
  118. TokenUser,
  119. TokenInformation,
  120. sizeof( TokenInformation ),
  121. &ReturnLength
  122. );
  123. dwLastError = GetLastError();
  124. CloseHandle( TokenHandle );
  125. if ( Status == FALSE ) {
  126. DBGMSG(DBG_WARNING, ("InitClientUserString: GetTokenInformation failed: Error %d\n",
  127. dwLastError ));
  128. return FALSE;
  129. }
  130. //
  131. // Convert the Sid (pointed to by pSid) to its
  132. // equivalent Unicode string representation.
  133. //
  134. UnicodeString.Length = 0;
  135. UnicodeString.MaximumLength = MAX_SID_STRING;
  136. UnicodeString.Buffer = pString;
  137. NtStatus = RtlConvertSidToUnicodeString(
  138. &UnicodeString,
  139. ((PTOKEN_USER)TokenInformation)->User.Sid,
  140. FALSE );
  141. if( !NT_SUCCESS( NtStatus )){
  142. DBGMSG( DBG_WARN,
  143. ( "InitClientUserString: RtlConvertSidToUnicodeString failed: Error %d\n",
  144. NtStatus ));
  145. dwLastError = RtlNtStatusToDosError( NtStatus );
  146. SetLastError( dwLastError );
  147. return FALSE;
  148. }
  149. return TRUE;
  150. }