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.

261 lines
4.6 KiB

  1. /*++
  2. Microsoft Confidential
  3. Copyright (c) 1992-1997 Microsoft Corporation
  4. All rights reserved
  5. Module Name:
  6. sid.c
  7. Abstract:
  8. SID management functions
  9. Author:
  10. (davidc) 26-Aug-1992
  11. --*/
  12. // NT base apis
  13. #include <nt.h>
  14. #include <ntrtl.h>
  15. #include <nturtl.h>
  16. #include <ntdddisk.h>
  17. #include "sysdm.h"
  18. LPTSTR
  19. GetSidString(
  20. void
  21. )
  22. /*++
  23. Routine Description:
  24. Allocates and returns a string representing the sid of the current user
  25. The returned pointer should be freed using DeleteSidString().
  26. Arguments:
  27. None
  28. Return Value:
  29. Returns a pointer to the string or NULL on failure.
  30. --*/
  31. {
  32. NTSTATUS NtStatus;
  33. PSID UserSid;
  34. UNICODE_STRING UnicodeString;
  35. LPTSTR lpEnd;
  36. //
  37. // Get the user sid
  38. //
  39. UserSid = GetUserSid();
  40. if (UserSid == NULL) {
  41. return NULL;
  42. }
  43. //
  44. // Convert user SID to a string.
  45. //
  46. NtStatus = RtlConvertSidToUnicodeString(
  47. &UnicodeString,
  48. UserSid,
  49. (BOOLEAN)TRUE // Allocate
  50. );
  51. //
  52. // We're finished with the user sid
  53. //
  54. DeleteUserSid(UserSid);
  55. //
  56. // See if the conversion to a string worked
  57. //
  58. if (!NT_SUCCESS(NtStatus)) {
  59. return NULL;
  60. }
  61. return(UnicodeString.Buffer);
  62. }
  63. VOID
  64. DeleteSidString(
  65. IN LPTSTR SidString
  66. )
  67. /*++
  68. Routine Description:
  69. Frees up a sid string previously returned by GetSidString()
  70. Arguments:
  71. SidString -
  72. Supplies string to free
  73. Return Value:
  74. None
  75. --*/
  76. {
  77. UNICODE_STRING String;
  78. RtlInitUnicodeString(&String, SidString);
  79. RtlFreeUnicodeString(&String);
  80. }
  81. PSID
  82. GetUserSid(
  83. void
  84. )
  85. /*++
  86. Routine Description:
  87. Allocs space for the user sid, fills it in and returns a pointer. Caller
  88. The sid should be freed by calling DeleteUserSid.
  89. Note the sid returned is the user's real sid, not the per-logon sid.
  90. Arguments:
  91. None
  92. Return Value:
  93. Returns pointer to sid or NULL on failure.
  94. --*/
  95. {
  96. PTOKEN_USER pUser;
  97. PSID pSid;
  98. DWORD BytesRequired = 200;
  99. NTSTATUS status;
  100. HANDLE UserToken;
  101. if (!OpenProcessToken (GetCurrentProcess(), TOKEN_READ, &UserToken)) {
  102. return NULL;
  103. }
  104. //
  105. // Allocate space for the user info
  106. //
  107. pUser = (PTOKEN_USER)LocalAlloc(LMEM_FIXED, BytesRequired);
  108. if (pUser == NULL) {
  109. CloseHandle (UserToken);
  110. return NULL;
  111. }
  112. //
  113. // Read in the UserInfo
  114. //
  115. status = NtQueryInformationToken(
  116. UserToken, // Handle
  117. TokenUser, // TokenInformationClass
  118. pUser, // TokenInformation
  119. BytesRequired, // TokenInformationLength
  120. &BytesRequired // ReturnLength
  121. );
  122. if (status == STATUS_BUFFER_TOO_SMALL) {
  123. HLOCAL pTemp;
  124. //
  125. // Allocate a bigger buffer and try again.
  126. //
  127. pTemp = LocalReAlloc(pUser, BytesRequired, LMEM_MOVEABLE);
  128. if (pTemp == NULL) {
  129. LocalFree((HLOCAL) pUser);
  130. CloseHandle (UserToken);
  131. return NULL;
  132. }
  133. else
  134. {
  135. pUser = (PTOKEN_USER)pTemp;
  136. }
  137. status = NtQueryInformationToken(
  138. UserToken, // Handle
  139. TokenUser, // TokenInformationClass
  140. pUser, // TokenInformation
  141. BytesRequired, // TokenInformationLength
  142. &BytesRequired // ReturnLength
  143. );
  144. }
  145. if (!NT_SUCCESS(status)) {
  146. LocalFree(pUser);
  147. CloseHandle (UserToken);
  148. return NULL;
  149. }
  150. BytesRequired = RtlLengthSid(pUser->User.Sid);
  151. pSid = LocalAlloc(LMEM_FIXED, BytesRequired);
  152. if (pSid == NULL) {
  153. LocalFree(pUser);
  154. CloseHandle (UserToken);
  155. return NULL;
  156. }
  157. status = RtlCopySid(BytesRequired, pSid, pUser->User.Sid);
  158. LocalFree(pUser);
  159. if (!NT_SUCCESS(status)) {
  160. LocalFree(pSid);
  161. pSid = NULL;
  162. }
  163. CloseHandle (UserToken);
  164. return pSid;
  165. }
  166. VOID
  167. DeleteUserSid(
  168. IN PSID Sid
  169. )
  170. /*++
  171. Routine Description:
  172. Deletes a user sid previously returned by GetUserSid()
  173. Arguments:
  174. Sid -
  175. Supplies sid to delete
  176. Return Value:
  177. None
  178. --*/
  179. {
  180. LocalFree(Sid);
  181. }