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.

311 lines
6.9 KiB

  1. /*++
  2. Copyright (c) 2001 Microsoft Corporation
  3. All rights reserved
  4. Module Name:
  5. lsasid.cxx
  6. Abstract:
  7. This file provides useful accssors and mutators.
  8. Author:
  9. Larry Zhu (LZhu) May 1, 2001 Created
  10. Environment:
  11. User Mode -Win32
  12. Revision History:
  13. --*/
  14. #include "precomp.hxx"
  15. #pragma hdrstop
  16. #include "lsasid.hxx"
  17. #include <stdio.h>
  18. #include <string.h>
  19. TSID::TSID(void) : m_hr(E_FAIL)
  20. {
  21. }
  22. TSID::TSID(IN ULONG64 baseOffset)
  23. : m_baseOffset(baseOffset), m_hr(E_FAIL)
  24. {
  25. m_hr = Initialize();
  26. }
  27. TSID::~TSID(void)
  28. {
  29. }
  30. HRESULT TSID::IsValid(void) const
  31. {
  32. return m_hr;
  33. }
  34. BOOLEAN TSID::IsSidValid(void) const
  35. {
  36. SID sid = {0};
  37. if (!ReadMemory(m_baseOffset, &sid, sizeof(sid), NULL)) {
  38. DBG_LOG(LSA_ERROR, ("Unable to read SID at %#I64x\n", m_baseOffset));
  39. throw "TSID::IsSidValid failed";
  40. }
  41. return RtlValidSid(&sid);
  42. }
  43. UCHAR TSID::GetSubAuthorityCount(void) const
  44. {
  45. UCHAR value = 0;
  46. ReadStructField(m_baseOffset, kstrSid, "SubAuthorityCount", sizeof(value), &value);
  47. return value;
  48. }
  49. UCHAR TSID::GetSubAuthorityCountDirect(void) const
  50. {
  51. UCHAR value = 0;
  52. DBG_LOG(LSA_LOG, ("Read _SID %#I64x SubAuthorityCount\n", m_baseOffset));
  53. if (!ReadMemory(m_baseOffset + sizeof(UCHAR), &value, sizeof(value), NULL)) {
  54. DBG_LOG(LSA_ERROR, ("Unable read SID::SubAuthorityCount from %#I64x\n", m_baseOffset));
  55. throw "TSID::GetSubAuthorityCountDirect failed";
  56. }
  57. return value;
  58. }
  59. ULONG TSID::GetSize(void) const
  60. {
  61. return RtlLengthRequiredSid(GetSubAuthorityCount());
  62. }
  63. ULONG TSID::GetSizeDirect(void) const
  64. {
  65. return RtlLengthRequiredSid(GetSubAuthorityCountDirect());
  66. }
  67. PCSTR TSID::toStr(void) const
  68. {
  69. return toStr(kstrSidFmt);
  70. }
  71. PCSTR TSID::toStr(IN PCSTR pszFmt) const
  72. {
  73. return InternalToStr(GetSize(), pszFmt);
  74. }
  75. PCSTR TSID::toStrDirect(void) const
  76. {
  77. return toStrDirect(kstrSidFmt);
  78. }
  79. PCSTR TSID::toStrDirect(IN PCSTR pszFmt) const
  80. {
  81. return InternalToStr(GetSizeDirect(), pszFmt);
  82. }
  83. PCSTR TSID::GetFriendlyNameDirect(IN PCSTR pszFmt) const
  84. {
  85. return InternalGetFriendlyName(GetSizeDirect(), pszFmt);
  86. }
  87. PCSTR TSID::ConvertSidToFriendlyName(IN PSID pSid, IN PCSTR pszFmt)
  88. {
  89. return ConvertSidToFriendlyName(reinterpret_cast<SID*>(pSid), pszFmt);
  90. }
  91. PCSTR TSID::ConvertSidToFriendlyName(IN SID* pSid, IN PCSTR pszFmt)
  92. {
  93. HRESULT hRetval = E_FAIL;
  94. static CHAR szSid[MAX_PATH] = {0};
  95. CHAR szName[MAX_PATH] = {0};
  96. CHAR szDomainName[MAX_PATH] = {0};
  97. SID_NAME_USE eUse = SidTypeInvalid;
  98. DWORD cbName = sizeof(szName) - 1;
  99. DWORD cbDomainName = sizeof(szDomainName) - 1;
  100. ExitIfControlC();
  101. //
  102. // null terminates szSid
  103. //
  104. szSid[0] = 0;
  105. hRetval = LsaLookupAccountSidA(NULL, pSid,
  106. szName, &cbName,
  107. szDomainName, &cbDomainName,
  108. &eUse) ? S_OK : GetLastErrorAsHResult();
  109. if (SUCCEEDED(hRetval)) {
  110. hRetval = _snprintf(szSid, sizeof(szSid) -1, pszFmt, GetSidTypeStr(eUse), *szDomainName ? szDomainName : "localhost", szName) >= 0 ? S_OK : HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
  111. }
  112. if (FAILED(hRetval) && (ERROR_NONE_MAPPED != HRESULT_CODE(hRetval))) {
  113. DBG_LOG(LSA_ERROR, ("ConvertSidToFriendlyName LsaLookupAccountSidA failed with error code %#x\n", HRESULT_CODE(hRetval)));
  114. throw "LsaLookupAccountSid failed";
  115. }
  116. //
  117. // Indicate none mapped if so
  118. //
  119. if (!*szSid) {
  120. _snprintf(szSid, sizeof(szSid) - 1, "(no name mapped)");
  121. }
  122. return szSid;
  123. }
  124. PCSTR TSID::GetSidTypeStr(IN SID_NAME_USE eUse)
  125. {
  126. static PCSTR acszSidTypeStr[] = {
  127. kstrInvalid, "User", "Group", "Domain", "Alias", "Well Known Group",
  128. "Deleted Account", kstrInvalid, "Unknown", "Computer",
  129. };
  130. if (eUse < SidTypeUser || eUse > SidTypeComputer) {
  131. throw "Unrecognized SID";
  132. }
  133. return acszSidTypeStr[eUse];
  134. }
  135. /******************************************************************************
  136. Private Methods
  137. ******************************************************************************/
  138. /*++
  139. Routine Name:
  140. Initialize
  141. Routine Description:
  142. Do necessary initialization.
  143. Arguments:
  144. None
  145. Return Value:
  146. An HRESULT
  147. --*/
  148. HRESULT TSID::Initialize(void)
  149. {
  150. HRESULT hRetval = E_FAIL;
  151. hRetval = S_OK;
  152. return hRetval;
  153. }
  154. HRESULT TSID::Initialize(IN ULONG64 baseOffset)
  155. {
  156. m_baseOffset = baseOffset;
  157. m_hr = Initialize();
  158. return m_hr;
  159. }
  160. PCSTR TSID::InternalToStr(IN ULONG cbSid, IN PCSTR pszFmt) const
  161. {
  162. static CHAR szBuffer[SID_MAX_SUB_AUTHORITIES * sizeof(ULONG) + kSidHeaderLen];
  163. UNICODE_STRING ucsSid = {0};
  164. NTSTATUS NtStatus = STATUS_UNSUCCESSFUL;
  165. ExitIfControlC();
  166. if (!IsSidValid()) {
  167. DBG_LOG(LSA_ERROR, ("_SID %#I64x is invlaid\n", toPtr(m_baseOffset)));
  168. throw "Invalid SID";
  169. }
  170. if (cbSid > sizeof(szBuffer)) {
  171. DBG_LOG(LSA_ERROR, ("Insufficient buffer in TSID::InternalToStr, _SID %#I64x maybe invalid, SubAuthCount is %d\n", m_baseOffset, (cbSid - sizeof(SID)) / sizeof(ULONG) + 1));
  172. throw "Insufficient buffer";
  173. }
  174. if (!ReadMemory(m_baseOffset, szBuffer, cbSid, NULL)) {
  175. DBG_LOG(LSA_ERROR, ("Unable to read SID from location %#I64x\n", toPtr(m_baseOffset)));
  176. throw "Unable to read SID";
  177. }
  178. NtStatus = RtlConvertSidToUnicodeString(&ucsSid, reinterpret_cast<SID*>(szBuffer), TRUE);
  179. if (NT_SUCCESS(NtStatus)) {
  180. NtStatus = _snprintf(szBuffer, sizeof(szBuffer) - 1, pszFmt, &ucsSid) >= 0 ? STATUS_SUCCESS : STATUS_UNSUCCESSFUL;
  181. }
  182. if (!NT_SUCCESS(NtStatus)) {
  183. DBG_LOG(LSA_ERROR, ("Unable to convert SID at address %#I64x to string\n", toPtr(m_baseOffset)));
  184. throw "TSID::InternalToStr failed";
  185. }
  186. RtlFreeUnicodeString(&ucsSid);
  187. return szBuffer;
  188. }
  189. PCSTR TSID::InternalGetFriendlyName(IN ULONG cbSid, IN PCSTR pszFmt) const
  190. {
  191. static CHAR szBuffer[SID_MAX_SUB_AUTHORITIES * sizeof(ULONG) + kSidHeaderLen] = {0};
  192. ExitIfControlC();
  193. if (!IsSidValid()) {
  194. DBG_LOG(LSA_ERROR, ("_SID %#I64x is invlaid\n", toPtr(m_baseOffset)));
  195. throw "Invalid SID";
  196. }
  197. if (cbSid > sizeof(szBuffer)) {
  198. DBG_LOG(LSA_ERROR, ("Insufficient buffer in TSID::GetFriendlyName, _SID %#I64x maybe invalid, SubAuthCount is %d\n", m_baseOffset, (cbSid - sizeof(SID)) / sizeof(ULONG) + 1));
  199. throw "Insufficient buffer";
  200. }
  201. if (!ReadMemory(m_baseOffset, szBuffer, cbSid, NULL)) {
  202. DBG_LOG(LSA_ERROR, ("Unable to read SID from location %#I64x\n", toPtr(m_baseOffset)));
  203. throw "Unable to read SID";
  204. }
  205. return ConvertSidToFriendlyName(reinterpret_cast<SID*>(szBuffer), pszFmt);
  206. }