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.

97 lines
1.8 KiB

  1. //-----------------------------------------------------------------------------
  2. // checkuser.h
  3. //-----------------------------------------------------------------------------
  4. #ifndef _CHECKUSER_H
  5. #define _CHECKUSER_H
  6. HRESULT _CheckSIDInProcess( SID* pSID )
  7. {
  8. if( !pSID )
  9. {
  10. return E_POINTER;
  11. }
  12. BOOL bRet = FALSE;
  13. if( !CheckTokenMembership(NULL, pSID, &bRet) )
  14. {
  15. return HRESULT_FROM_WIN32( GetLastError() );
  16. }
  17. return bRet ? S_OK : S_FALSE;
  18. }
  19. HRESULT IsUserInGroup( DWORD dwRID )
  20. {
  21. PSID psid = NULL;
  22. SID_IDENTIFIER_AUTHORITY sia = SECURITY_NT_AUTHORITY;
  23. BOOL bRet = AllocateAndInitializeSid( &sia,
  24. 2,
  25. SECURITY_BUILTIN_DOMAIN_RID,
  26. dwRID,
  27. 0, 0, 0, 0, 0, 0,
  28. &psid);
  29. if( !bRet )
  30. {
  31. return HRESULT_FROM_WIN32( GetLastError() );
  32. }
  33. else if( !psid )
  34. {
  35. return E_FAIL;
  36. }
  37. HRESULT hr = _CheckSIDInProcess( (SID*)psid );
  38. FreeSid( psid );
  39. return hr;
  40. }
  41. HRESULT IsUserInGroup( const TCHAR* pszGroup )
  42. {
  43. if( !pszGroup )
  44. {
  45. return E_POINTER;
  46. }
  47. HRESULT hr = S_FALSE;
  48. DWORD dwSize = 0;
  49. DWORD dwDomainSize = 0;
  50. SID_NAME_USE snu;
  51. if( !LookupAccountName(NULL, pszGroup, NULL, &dwSize, NULL, &dwDomainSize, &snu) &&
  52. GetLastError() == ERROR_INSUFFICIENT_BUFFER )
  53. {
  54. SID* psid = (SID*)new BYTE[dwSize];
  55. if( !psid )
  56. {
  57. return E_OUTOFMEMORY;
  58. }
  59. TCHAR* pszDomain = new TCHAR[dwDomainSize];
  60. if( !pszDomain )
  61. {
  62. delete[] psid;
  63. return E_OUTOFMEMORY;
  64. }
  65. if( LookupAccountName(NULL, pszGroup, psid, &dwSize, pszDomain, &dwDomainSize, &snu) )
  66. {
  67. hr = _CheckSIDInProcess( psid );
  68. }
  69. else
  70. {
  71. hr = HRESULT_FROM_WIN32( GetLastError() );
  72. }
  73. delete[] psid;
  74. delete[] pszDomain;
  75. }
  76. else
  77. {
  78. return E_FAIL;
  79. }
  80. return hr;
  81. }
  82. #endif // _CHECKUSER_H