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.

196 lines
5.1 KiB

  1. // NCUtility.cpp: implementation of the CNCUtility class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include <pch.h>
  5. #pragma hdrstop
  6. #include <tchar.h>
  7. #include <regkysec.h>
  8. #include <ncutil.h>
  9. #include <winsock2.h>
  10. //////////////////////////////////////////////////////////////////////
  11. // Construction/Destruction
  12. //////////////////////////////////////////////////////////////////////
  13. CNCUtility::CNCUtility()
  14. {
  15. }
  16. CNCUtility::~CNCUtility()
  17. {
  18. }
  19. HRESULT CNCUtility::SidToString(PCSID pSid, tstring &strSid)
  20. {
  21. HRESULT hr = S_OK;
  22. PSID_IDENTIFIER_AUTHORITY psia;
  23. DWORD dwSubAuthorities;
  24. DWORD dwSidRev=SID_REVISION;
  25. DWORD dwCounter;
  26. DWORD dwSidSize;
  27. LPTSTR strTextualSid = NULL;
  28. // Validate the binary SID.
  29. if(!IsValidSid(const_cast<PSID>(pSid)))
  30. {
  31. hr = HRESULT_FROM_WIN32(ERROR_INVALID_SID);
  32. return hr;
  33. }
  34. // Get the identifier authority value from the SID.
  35. psia = GetSidIdentifierAuthority(const_cast<PSID>(pSid));
  36. // Get the number of subauthorities in the SID.
  37. dwSubAuthorities = *GetSidSubAuthorityCount(const_cast<PSID>(pSid));
  38. // Compute the buffer length.
  39. // S-SID_REVISION- + IdentifierAuthority- + subauthorities- + NULL
  40. dwSidSize=(15 + 12 + (12 * dwSubAuthorities) + 1) * sizeof(TCHAR);
  41. strTextualSid = new TCHAR[dwSidSize];
  42. if (!strTextualSid)
  43. {
  44. hr = E_OUTOFMEMORY;
  45. return hr;
  46. }
  47. // Add 'S' prefix and revision number to the string.
  48. dwSidSize=wsprintf(strTextualSid, TEXT("S-%lu-"), dwSidRev);
  49. // Add SID identifier authority to the string.
  50. if ( (psia->Value[0] != 0) || (psia->Value[1] != 0) )
  51. {
  52. dwSidSize+=wsprintf(strTextualSid + lstrlen(strTextualSid),
  53. TEXT("0x%02hx%02hx%02hx%02hx%02hx%02hx"),
  54. static_cast<USHORT>(psia->Value[0]),
  55. static_cast<USHORT>(psia->Value[1]),
  56. static_cast<USHORT>(psia->Value[2]),
  57. static_cast<USHORT>(psia->Value[3]),
  58. static_cast<USHORT>(psia->Value[4]),
  59. static_cast<USHORT>(psia->Value[5]));
  60. }
  61. else
  62. {
  63. dwSidSize+=wsprintf(strTextualSid + lstrlen(strTextualSid),
  64. TEXT("%lu"),
  65. (static_cast<ULONG>(psia->Value[5])) +
  66. (static_cast<ULONG>(psia->Value[4]) << 8) +
  67. (static_cast<ULONG>(psia->Value[3]) << 16) +
  68. (static_cast<ULONG>(psia->Value[2]) << 24) );
  69. }
  70. // Add SID subauthorities to the string.
  71. //
  72. for (dwCounter=0 ; dwCounter < dwSubAuthorities ; dwCounter++)
  73. {
  74. dwSidSize+=wsprintf(strTextualSid + dwSidSize, TEXT("-%lu"),
  75. *GetSidSubAuthority(const_cast<PSID>(pSid), dwCounter) );
  76. }
  77. strSid = strTextualSid;
  78. delete[] strTextualSid;
  79. return hr;
  80. }
  81. HRESULT CNCUtility::StringToSid(const tstring strSid, PSID &pSid)
  82. {
  83. HRESULT hr = S_OK;
  84. TCHAR seperators[] = _T("-");
  85. TCHAR *token;
  86. SID_IDENTIFIER_AUTHORITY sia;
  87. BYTE nSubAuthorityCount;
  88. DWORD SubAuthorities[8];
  89. BYTE count = 0;
  90. DWORD dwTemp;
  91. LPTSTR lpstrSid = new TCHAR[strSid.length()+1];
  92. ZeroMemory(sia.Value, sizeof (SID_IDENTIFIER_AUTHORITY));
  93. ZeroMemory(SubAuthorities, 8 * sizeof(DWORD));
  94. _tcscpy(lpstrSid, strSid.c_str());
  95. token = _tcstok(lpstrSid, seperators);
  96. if (_tcscmp(token, _T("S")) != 0)
  97. {
  98. return 0;
  99. }
  100. token = _tcstok(NULL, seperators); // Skip the revision
  101. token = _tcstok(NULL, seperators); // Start the real conversion
  102. while (token != NULL)
  103. {
  104. if (count == 0)
  105. {
  106. if (token[1] == 'x')
  107. {
  108. // > MAXINT
  109. unsigned int usTemp;
  110. TCHAR bytes[2];
  111. for (int iCount = 0; iCount < 6; iCount++)
  112. {
  113. _tcsncpy(bytes,(token + iCount + 2),2 * sizeof(TCHAR));
  114. usTemp = _ttoi(reinterpret_cast<LPCTSTR>(bytes));
  115. sia.Value[iCount] = (unsigned char) usTemp;
  116. }
  117. }
  118. else
  119. {
  120. // <= MAXINT
  121. dwTemp = _ttol(reinterpret_cast<LPCTSTR>(token));
  122. dwTemp = htonl(dwTemp);
  123. memmove(sia.Value + 2, &dwTemp, sizeof(dwTemp));
  124. count++;
  125. }
  126. }
  127. else
  128. {
  129. SubAuthorities[count - 1] = _ttol(reinterpret_cast<LPCTSTR>(token));
  130. count++;
  131. }
  132. token = _tcstok(NULL, seperators); // get the next string
  133. }
  134. nSubAuthorityCount = count-1;
  135. if (!AllocateAndInitializeSid(&sia,
  136. nSubAuthorityCount,
  137. SubAuthorities[0],
  138. SubAuthorities[1],
  139. SubAuthorities[2],
  140. SubAuthorities[3],
  141. SubAuthorities[4],
  142. SubAuthorities[5],
  143. SubAuthorities[6],
  144. SubAuthorities[7],
  145. &pSid))
  146. {
  147. hr = HRESULT_FROM_WIN32(GetLastError());
  148. }
  149. delete[] lpstrSid;
  150. #ifdef DBG
  151. tstring strSidString;
  152. SidToString(pSid, strSidString);
  153. Assert(strSidString == strSid);
  154. #endif
  155. return hr;
  156. }