Source code of Windows XP (NT5)
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.

257 lines
6.1 KiB

  1. //*************************************************************
  2. //
  3. // SID management functions.
  4. //
  5. // THESE FUNCTIONS ARE WINDOWS NT SPECIFIC!!!!!
  6. //
  7. // Microsoft Confidential
  8. // Copyright (c) Microsoft Corporation 1995
  9. // All rights reserved
  10. //
  11. //*************************************************************
  12. #include "uenv.h"
  13. LPTSTR GetSidString(HANDLE UserToken);
  14. VOID DeleteSidString(LPTSTR SidString);
  15. PSID GetUserSid (HANDLE UserToken);
  16. VOID DeleteUserSid(PSID Sid);
  17. #define DebugMsg(x)
  18. /***************************************************************************\
  19. * GetSidString
  20. *
  21. * Allocates and returns a string representing the sid of the current user
  22. * The returned pointer should be freed using DeleteSidString().
  23. *
  24. * Returns a pointer to the string or NULL on failure.
  25. *
  26. * History:
  27. * 26-Aug-92 Davidc Created
  28. *
  29. \***************************************************************************/
  30. LPTSTR GetSidString(HANDLE UserToken)
  31. {
  32. NTSTATUS NtStatus;
  33. PSID UserSid;
  34. UNICODE_STRING UnicodeString;
  35. #ifndef UNICODE
  36. STRING String;
  37. #endif
  38. //
  39. // Get the user sid
  40. //
  41. UserSid = GetUserSid(UserToken);
  42. if (UserSid == NULL) {
  43. DebugMsg((DM_WARNING, TEXT("GetSidString: GetUserSid returned NULL")));
  44. return NULL;
  45. }
  46. //
  47. // Convert user SID to a string.
  48. //
  49. NtStatus = RtlConvertSidToUnicodeString(
  50. &UnicodeString,
  51. UserSid,
  52. (BOOLEAN)TRUE // Allocate
  53. );
  54. //
  55. // We're finished with the user sid
  56. //
  57. DeleteUserSid(UserSid);
  58. //
  59. // See if the conversion to a string worked
  60. //
  61. if (!NT_SUCCESS(NtStatus)) {
  62. DebugMsg((DM_WARNING, TEXT("GetSidString: RtlConvertSidToUnicodeString failed, status = 0x%x"),
  63. NtStatus));
  64. return NULL;
  65. }
  66. #ifdef UNICODE
  67. return(UnicodeString.Buffer);
  68. #else
  69. //
  70. // Convert the string to ansi
  71. //
  72. NtStatus = RtlUnicodeStringToAnsiString(&String, &UnicodeString, TRUE);
  73. RtlFreeUnicodeString(&UnicodeString);
  74. if (!NT_SUCCESS(NtStatus)) {
  75. DebugMsg((DM_WARNING, TEXT("GetSidString: RtlUnicodeStringToAnsiString failed, status = 0x%x"),
  76. status));
  77. return NULL;
  78. }
  79. return(String.Buffer);
  80. #endif
  81. }
  82. /***************************************************************************\
  83. * DeleteSidString
  84. *
  85. * Frees up a sid string previously returned by GetSidString()
  86. *
  87. * Returns nothing.
  88. *
  89. * History:
  90. * 26-Aug-92 Davidc Created
  91. *
  92. \***************************************************************************/
  93. VOID DeleteSidString(LPTSTR SidString)
  94. {
  95. #ifdef UNICODE
  96. UNICODE_STRING String;
  97. RtlInitUnicodeString(&String, SidString);
  98. RtlFreeUnicodeString(&String);
  99. #else
  100. ANSI_STRING String;
  101. RtlInitAnsiString(&String, SidString);
  102. RtlFreeAnsiString(&String);
  103. #endif
  104. }
  105. /***************************************************************************\
  106. * GetUserSid
  107. *
  108. * Allocs space for the user sid, fills it in and returns a pointer. Caller
  109. * The sid should be freed by calling DeleteUserSid.
  110. *
  111. * Note the sid returned is the user's real sid, not the per-logon sid.
  112. *
  113. * Returns pointer to sid or NULL on failure.
  114. *
  115. * History:
  116. * 26-Aug-92 Davidc Created.
  117. \***************************************************************************/
  118. PSID GetUserSid (HANDLE UserToken)
  119. {
  120. PTOKEN_USER pUser;
  121. PTOKEN_USER pTemp;
  122. PSID pSid;
  123. DWORD BytesRequired = 200;
  124. NTSTATUS status;
  125. //
  126. // Allocate space for the user info
  127. //
  128. pUser = (PTOKEN_USER)LocalAlloc(LMEM_FIXED, BytesRequired);
  129. if (pUser == NULL) {
  130. DebugMsg((DM_WARNING, TEXT("GetUserSid: Failed to allocate %d bytes"),
  131. BytesRequired));
  132. return NULL;
  133. }
  134. //
  135. // Read in the UserInfo
  136. //
  137. status = NtQueryInformationToken(
  138. UserToken, // Handle
  139. TokenUser, // TokenInformationClass
  140. pUser, // TokenInformation
  141. BytesRequired, // TokenInformationLength
  142. &BytesRequired // ReturnLength
  143. );
  144. if (status == STATUS_BUFFER_TOO_SMALL) {
  145. //
  146. // Allocate a bigger buffer and try again.
  147. //
  148. pTemp = pUser;
  149. pUser = LocalReAlloc(pUser, BytesRequired, LMEM_MOVEABLE);
  150. if (pUser == NULL) {
  151. LocalFree( pTemp );
  152. DebugMsg((DM_WARNING, TEXT("GetUserSid: Failed to allocate %d bytes"),
  153. BytesRequired));
  154. return NULL;
  155. }
  156. status = NtQueryInformationToken(
  157. UserToken, // Handle
  158. TokenUser, // TokenInformationClass
  159. pUser, // TokenInformation
  160. BytesRequired, // TokenInformationLength
  161. &BytesRequired // ReturnLength
  162. );
  163. }
  164. if (!NT_SUCCESS(status)) {
  165. DebugMsg((DM_WARNING, TEXT("GetUserSid: Failed to query user info from user token, status = 0x%x"),
  166. status));
  167. LocalFree(pUser);
  168. return NULL;
  169. }
  170. BytesRequired = RtlLengthSid(pUser->User.Sid);
  171. pSid = LocalAlloc(LMEM_FIXED, BytesRequired);
  172. if (pSid == NULL) {
  173. DebugMsg((DM_WARNING, TEXT("GetUserSid: Failed to allocate %d bytes"),
  174. BytesRequired));
  175. LocalFree(pUser);
  176. return NULL;
  177. }
  178. status = RtlCopySid(BytesRequired, pSid, pUser->User.Sid);
  179. LocalFree(pUser);
  180. if (!NT_SUCCESS(status)) {
  181. DebugMsg((DM_WARNING, TEXT("GetUserSid: RtlCopySid Failed. status = %d"),
  182. status));
  183. LocalFree(pSid);
  184. pSid = NULL;
  185. }
  186. return pSid;
  187. }
  188. /***************************************************************************\
  189. * DeleteUserSid
  190. *
  191. * Deletes a user sid previously returned by GetUserSid()
  192. *
  193. * Returns nothing.
  194. *
  195. * History:
  196. * 26-Aug-92 Davidc Created
  197. *
  198. \***************************************************************************/
  199. VOID DeleteUserSid(PSID Sid)
  200. {
  201. LocalFree(Sid);
  202. }