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.

185 lines
5.2 KiB

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (C) 1998, Microsoft Corp. All rights reserved.
  4. //
  5. // FILE
  6. //
  7. // textsid.c
  8. //
  9. // SYNOPSIS
  10. //
  11. // This file defines functions for converting Security Indentifiers (SIDs)
  12. // to and from a textual representation.
  13. //
  14. // MODIFICATION HISTORY
  15. //
  16. // 01/18/1998 Original version.
  17. //
  18. ///////////////////////////////////////////////////////////////////////////////
  19. #define IAS_LEAN_AND_MEAN
  20. #include "precompiled.h"
  21. #include <ias.h>
  22. #include <stdio.h>
  23. #include "textsid.h"
  24. DWORD
  25. WINAPI
  26. IASSidToTextW(
  27. IN PSID pSid,
  28. OUT PWSTR szTextualSid,
  29. IN OUT PDWORD pdwBufferLen
  30. )
  31. {
  32. PSID_IDENTIFIER_AUTHORITY psia;
  33. DWORD dwSubAuthorities;
  34. DWORD dwSidRev=SID_REVISION;
  35. DWORD dwCounter;
  36. DWORD dwSidSize;
  37. // Ensure input parameters are valid.
  38. if ((szTextualSid == NULL && pdwBufferLen != 0) || !IsValidSid(pSid))
  39. {
  40. return ERROR_INVALID_PARAMETER;
  41. }
  42. // obtain SidIdentifierAuthority
  43. psia=GetSidIdentifierAuthority(pSid);
  44. // obtain SidSubSuthority count
  45. dwSubAuthorities=*GetSidSubAuthorityCount(pSid);
  46. //////////
  47. // compute buffer length
  48. // S-SID_REVISION- + identifierauthority- + subauthorities- + NULL
  49. //////////
  50. dwSidSize=(15 + 12 + (12 * dwSubAuthorities) + 1);
  51. //////////
  52. // check provided buffer length.
  53. // If not large enough, indicate proper size and return error.
  54. //////////
  55. if (*pdwBufferLen < dwSidSize)
  56. {
  57. *pdwBufferLen = dwSidSize;
  58. return ERROR_INSUFFICIENT_BUFFER;
  59. }
  60. //////////
  61. // prepare S-SID_REVISION-
  62. //////////
  63. dwSidSize=swprintf(szTextualSid, L"S-%lu-", dwSidRev );
  64. //////////
  65. // prepare SidIdentifierAuthority
  66. //////////
  67. if ( (psia->Value[0] != 0) || (psia->Value[1] != 0) )
  68. {
  69. dwSidSize+=swprintf(szTextualSid + lstrlenW(szTextualSid),
  70. L"0x%02hx%02hx%02hx%02hx%02hx%02hx",
  71. (USHORT)psia->Value[0],
  72. (USHORT)psia->Value[1],
  73. (USHORT)psia->Value[2],
  74. (USHORT)psia->Value[3],
  75. (USHORT)psia->Value[4],
  76. (USHORT)psia->Value[5]);
  77. }
  78. else
  79. {
  80. dwSidSize+=swprintf(szTextualSid + wcslen(szTextualSid),
  81. L"%lu",
  82. (ULONG)(psia->Value[5] ) +
  83. (ULONG)(psia->Value[4] << 8) +
  84. (ULONG)(psia->Value[3] << 16) +
  85. (ULONG)(psia->Value[2] << 24) );
  86. }
  87. //////////
  88. // loop through SidSubAuthorities
  89. //////////
  90. for (dwCounter=0 ; dwCounter < dwSubAuthorities ; dwCounter++)
  91. {
  92. dwSidSize+=swprintf(szTextualSid + dwSidSize,
  93. L"-%lu",
  94. *GetSidSubAuthority(pSid, dwCounter));
  95. }
  96. return NO_ERROR;
  97. }
  98. DWORD
  99. WINAPI
  100. IASSidFromTextW(
  101. IN PCWSTR szTextualSid,
  102. OUT PSID *pSid
  103. )
  104. {
  105. DWORDLONG rawsia;
  106. DWORD revision;
  107. DWORD subAuth[8];
  108. SID_IDENTIFIER_AUTHORITY sia;
  109. int fields;
  110. if (szTextualSid == NULL || pSid == NULL)
  111. {
  112. return ERROR_INVALID_PARAMETER;
  113. }
  114. //////////
  115. // Scan the input string.
  116. //////////
  117. fields = swscanf(szTextualSid,
  118. L"S-%lu-%I64u-%lu-%lu-%lu-%lu-%lu-%lu-%lu-%lu",
  119. &revision,
  120. &rawsia,
  121. subAuth,
  122. subAuth + 1,
  123. subAuth + 2,
  124. subAuth + 3,
  125. subAuth + 4,
  126. subAuth + 5,
  127. subAuth + 6,
  128. subAuth + 7);
  129. //////////
  130. // We must have at least three fields (revision, identifier authority, and
  131. // at least one sub authority), the current revision, and a 48-bit SIA.
  132. //////////
  133. if (fields < 3 || revision != SID_REVISION || rawsia > 0xffffffffffffI64)
  134. {
  135. return ERROR_INVALID_PARAMETER;
  136. }
  137. //////////
  138. // Pack the SID_IDENTIFIER_AUTHORITY.
  139. //////////
  140. sia.Value[0] = (UCHAR)((rawsia >> 40) & 0xff);
  141. sia.Value[1] = (UCHAR)((rawsia >> 32) & 0xff);
  142. sia.Value[2] = (UCHAR)((rawsia >> 24) & 0xff);
  143. sia.Value[3] = (UCHAR)((rawsia >> 16) & 0xff);
  144. sia.Value[4] = (UCHAR)((rawsia >> 8) & 0xff);
  145. sia.Value[5] = (UCHAR)((rawsia ) & 0xff);
  146. //////////
  147. // Allocate the SID. Must be freed through FreeSid().
  148. //////////
  149. if (!AllocateAndInitializeSid(&sia,
  150. (BYTE)(fields - 2),
  151. subAuth[0],
  152. subAuth[1],
  153. subAuth[2],
  154. subAuth[3],
  155. subAuth[4],
  156. subAuth[5],
  157. subAuth[6],
  158. subAuth[7],
  159. pSid))
  160. {
  161. return GetLastError();
  162. }
  163. return NO_ERROR;
  164. }