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.

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