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.

144 lines
3.4 KiB

  1. //+--------------------------------------------------------------------------
  2. // File: sid.h
  3. // Contents: class incapsulating a SID
  4. //---------------------------------------------------------------------------
  5. #ifndef __CERTSRV_CSID__
  6. #define __CERTSRV_CSID__
  7. #include <sddl.h>
  8. namespace CertSrv
  9. {
  10. class CSid
  11. {
  12. public:
  13. CSid() {Init();}
  14. CSid(PSID pSid) {Init(); CopySid(pSid);}
  15. CSid(const CSid &copySid) {Init(); CopySid(copySid.m_pSid);}
  16. CSid(LPCWSTR pcwszSid) {Init(); CopySid(pcwszSid);}
  17. ~CSid()
  18. {
  19. if(m_pSid)
  20. LocalFree(m_pSid);
  21. if(m_pwszSid)
  22. LocalFree(m_pwszSid);
  23. if(m_pwszName)
  24. LocalFree(m_pwszName);
  25. }
  26. operator LPCWSTR()
  27. {
  28. return GetStringSid();
  29. }
  30. operator PSID() {return m_pSid;}
  31. LPCWSTR GetName()
  32. {
  33. // attemp to map sid to name only once
  34. if(m_fCantResolveName ||
  35. S_OK!=MapSidToName())
  36. {
  37. m_fCantResolveName = TRUE;
  38. return GetStringSid();
  39. }
  40. return m_pwszName;
  41. }
  42. PSID GetSid() { return m_pSid;}
  43. protected:
  44. void Init()
  45. {
  46. m_pSid = NULL;
  47. m_pwszSid = NULL;
  48. m_pwszName = NULL;
  49. m_fCantResolveName = FALSE;
  50. }
  51. void SetStringSid()
  52. {
  53. if(m_pSid)
  54. myConvertSidToStringSid(m_pSid, &m_pwszSid);
  55. }
  56. LPCWSTR GetStringSid()
  57. {
  58. if(!m_pwszSid)
  59. SetStringSid();
  60. return m_pwszSid?m_pwszSid:L"";
  61. }
  62. void CopySid(PSID pSid)
  63. {
  64. ULONG cbSid = GetLengthSid(pSid);
  65. m_pSid = (BYTE *) LocalAlloc(LMEM_FIXED, cbSid);
  66. if(m_pSid && !::CopySid(cbSid, m_pSid, pSid))
  67. {
  68. LocalFree(m_pSid);
  69. m_pSid = NULL;
  70. }
  71. }
  72. void CopySid(LPCWSTR pcwszSid)
  73. {
  74. if(pcwszSid)
  75. myConvertStringSidToSid(pcwszSid, &m_pSid);
  76. }
  77. HRESULT MapSidToName()
  78. {
  79. if(m_pwszName)
  80. return S_OK;
  81. WCHAR wszDummyBuffer[2];
  82. DWORD cchName = 0, cchDomain = 0;
  83. SID_NAME_USE use;
  84. LookupAccountSid(
  85. NULL,
  86. m_pSid,
  87. NULL,
  88. &cchName,
  89. NULL,
  90. &cchDomain,
  91. &use);
  92. if(ERROR_INSUFFICIENT_BUFFER!=GetLastError())
  93. {
  94. DWORD err = GetLastError();
  95. return HRESULT_FROM_WIN32(GetLastError());
  96. }
  97. // build the full name "Domain\Name"
  98. m_pwszName = (LPWSTR) LocalAlloc(
  99. LMEM_FIXED,
  100. sizeof(WCHAR)*(cchName+cchDomain+2));
  101. if(!m_pwszName)
  102. {
  103. return E_OUTOFMEMORY;
  104. }
  105. // special case for Everyone, LookupAccountSid returns empty domain name for it
  106. if(!LookupAccountSid(
  107. NULL,
  108. m_pSid,
  109. m_pwszName+((1==cchDomain)?0:cchDomain),
  110. &cchName,
  111. (1==cchDomain)?wszDummyBuffer:m_pwszName,
  112. &cchDomain,
  113. &use))
  114. {
  115. LocalFree(m_pwszName);
  116. m_pwszName = NULL;
  117. return HRESULT_FROM_WIN32(GetLastError());
  118. }
  119. if(cchDomain>1)
  120. {
  121. m_pwszName[cchDomain] = L'\\';
  122. }
  123. return S_OK;
  124. }
  125. CSid operator=(const CSid& sid); //protect callers from using it
  126. PSID m_pSid;
  127. LPWSTR m_pwszSid;
  128. LPWSTR m_pwszName;
  129. BOOL m_fCantResolveName;
  130. };
  131. };//namespace CertSrv
  132. #endif //__CERTSRV_CSID__