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.

164 lines
4.8 KiB

  1. /*****************************************************************************/
  2. /* Copyright (c) 1999-2001 Microsoft Corporation, All Rights Reserved /
  3. /*****************************************************************************/
  4. /*
  5. * CSid.h - header file for CSid class
  6. *
  7. * Created: 12-14-1997 by Sanjeev Surati
  8. * (based on classes from Windows NT Security by Nik Okuntseff)
  9. */
  10. #if !defined __CSID_H__
  11. #define __CSID_H__
  12. #include <comdef.h>
  13. ////////////////////////////////////////////////////////////////
  14. //
  15. // Class: CSid
  16. //
  17. // This class is intended to provide a wrapper for basic
  18. // Windows NT SIDs (Security Identifiers). There is a
  19. // possibility of a slight performance hit when instantiating
  20. // one of these as it uses LookupAccountName and LookupAccountSid
  21. // to initialize account information, and those calls can go
  22. // out over the network to get their data.
  23. //
  24. ////////////////////////////////////////////////////////////////
  25. class CSid
  26. {
  27. // Constructors and destructor
  28. public:
  29. CSid();
  30. CSid( PSID pSid, LPCTSTR pszComputerName = NULL );
  31. CSid( PSID pSid, LPCTSTR pszComputerName, bool fLookup );
  32. CSid( LPCTSTR pszDomainName, LPCTSTR pszName, LPCTSTR pszComputerName );
  33. #ifndef UNICODE
  34. CSid( LPCWSTR wstrDomainName, LPCWSTR wstrName, LPCWSTR wstrComputerName );
  35. #endif
  36. CSid( LPCTSTR pszAccountName, LPCTSTR pszComputerName = NULL );
  37. CSid( const CSid &r_Sid );
  38. ~CSid( void );
  39. // Public functions
  40. public:
  41. CSid & operator= ( const CSid & );
  42. BOOL operator== ( const CSid & ) const;
  43. void GetDomainAccountName( CHString& strName ) const;
  44. CHString GetAccountName( void ) const;
  45. WCHAR* GetAccountNameW( void ) const;
  46. CHString GetDomainName( void ) const;
  47. WCHAR* GetDomainNameW( void ) const;
  48. CHString GetSidString( void ) const;
  49. WCHAR* GetSidStringW( void ) const;
  50. SID_NAME_USE GetAccountType( void ) const;
  51. PSID GetPSid( void ) const;
  52. DWORD GetLength( void ) const;
  53. BOOL IsOK( void ) const;
  54. BOOL IsValid( void ) const;
  55. BOOL IsAccountTypeValid( void ) const;
  56. DWORD GetError( void ) const;
  57. static void StringFromSid( PSID psid, CHString& str );
  58. static void StringFromSidW( PSID psid, WCHAR** pwstr );
  59. #ifdef NTONLY
  60. void DumpSid(LPCWSTR wstrFilename = NULL);
  61. #endif
  62. // Private data members
  63. private:
  64. PSID m_pSid; // Pointer to standard Win32 SID
  65. SID_NAME_USE m_snuAccountType; // Type of SID
  66. //CHString m_strSid; // Wind32 SID in human readable form
  67. //WCHAR* m_wstrSid; // As above, for wchar support when UNICODE not defined
  68. //WCHAR* m_wstrAccountName; // ibid.
  69. //WCHAR* m_wstrDomainName; // ibid.
  70. //CHString m_strAccountName; // Name of the account
  71. //CHString m_strDomainName; // Domain name the account belongs to
  72. _bstr_t m_bstrtSid;
  73. _bstr_t m_bstrtAccountName;
  74. _bstr_t m_bstrtDomainName;
  75. DWORD m_dwLastError; // Last Error in the Sid;
  76. DWORD InitFromAccountName( LPCTSTR pszAccountName, LPCTSTR pszComputerName );
  77. DWORD InitFromAccountNameW( LPCWSTR wstrAccountName, LPCWSTR wstrComputerName );
  78. DWORD InitFromSid( PSID pSid, LPCTSTR pszComputerName, bool fLookup = true );
  79. };
  80. inline BOOL CSid::IsOK( void ) const
  81. {
  82. return ( ERROR_SUCCESS == m_dwLastError );
  83. }
  84. inline DWORD CSid::GetError( void ) const
  85. {
  86. return m_dwLastError;
  87. }
  88. // Lets us know if the Sid is Valid
  89. inline BOOL CSid::IsValid( void ) const
  90. {
  91. // If m_pSid is NULL, this will return FALSE.
  92. // dw: However, doing it this way causes a first chance exception, so...
  93. if (m_pSid != NULL)
  94. return ::IsValidSid( m_pSid );
  95. return FALSE;
  96. }
  97. inline BOOL CSid::IsAccountTypeValid( void ) const
  98. {
  99. // SID may be valid, and Lookup succeeded, but it may be of a type that isn't
  100. // necessarily a user/group/alias.
  101. return ( m_snuAccountType >= SidTypeUser && m_snuAccountType < SidTypeDeletedAccount );
  102. }
  103. inline SID_NAME_USE CSid::GetAccountType( void ) const
  104. {
  105. return m_snuAccountType;
  106. }
  107. inline CHString CSid::GetAccountName( void ) const
  108. {
  109. return ( CHString((LPCWSTR)m_bstrtAccountName) );
  110. }
  111. inline WCHAR* CSid::GetAccountNameW( void ) const
  112. {
  113. return ( m_bstrtAccountName );
  114. }
  115. inline CHString CSid::GetDomainName( void ) const
  116. {
  117. return ( CHString((LPCWSTR)m_bstrtDomainName) );
  118. }
  119. inline WCHAR* CSid::GetDomainNameW( void ) const
  120. {
  121. return ( m_bstrtDomainName );
  122. }
  123. inline CHString CSid::GetSidString( void ) const
  124. {
  125. return ( CHString((LPCWSTR)m_bstrtSid) );
  126. }
  127. inline WCHAR* CSid::GetSidStringW( void ) const
  128. {
  129. return ( m_bstrtSid );
  130. }
  131. inline PSID CSid::GetPSid( void ) const
  132. {
  133. return ( m_pSid );
  134. }
  135. #endif // __CSID_H__