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.

114 lines
2.6 KiB

  1. /*****************************************************************************
  2. I S M E M B E R
  3. Name: ismember.c
  4. Date: 21-Jan-1994
  5. Creator: Unknown
  6. Description:
  7. This file contains the function to check the user is a member
  8. of a given group.
  9. History:
  10. 21-Jan-1994 John Fu, reformat and cleanup.
  11. *****************************************************************************/
  12. #include <windows.h>
  13. #include "clipbook.h"
  14. #include "ismember.h"
  15. #include "security.h"
  16. #include "debugout.h"
  17. /*
  18. * IsUserMember
  19. *
  20. * Purpose: Determine if the current user is a member of the given group.
  21. *
  22. * Parameters:
  23. * psidGroup - Pointer to a SID describing the group.
  24. *
  25. * Returns: TRUE if the user is a member of the group, FALSE
  26. * otherwise
  27. */
  28. BOOL IsUserMember(
  29. PSID psidGroup)
  30. {
  31. TOKEN_GROUPS *ptokgrp;
  32. HANDLE hToken=NULL;
  33. BOOL fRet = FALSE;
  34. DWORD dwInfoSize;
  35. unsigned i;
  36. PINFO(TEXT("IsMember of ? "));
  37. PrintSid(psidGroup);
  38. if (!GetTokenHandle(&hToken))
  39. {
  40. PERROR(TEXT("IsUserMember: Couldn't get token handle\r\n"));
  41. return FALSE;
  42. }
  43. GetTokenInformation(hToken, TokenGroups, NULL, 0, &dwInfoSize);
  44. if (ptokgrp = LocalAlloc(LPTR, dwInfoSize))
  45. {
  46. if (GetTokenInformation(hToken, TokenGroups, ptokgrp,
  47. dwInfoSize, &dwInfoSize))
  48. {
  49. for (i = 0;i < ptokgrp->GroupCount;i++)
  50. {
  51. PrintSid(ptokgrp->Groups[i].Sid);
  52. if (EqualSid(ptokgrp->Groups[i].Sid, psidGroup))
  53. {
  54. PINFO(TEXT("YES"));
  55. fRet = TRUE;
  56. break;
  57. }
  58. }
  59. }
  60. LocalFree(ptokgrp);
  61. }
  62. if (!fRet)
  63. {
  64. TOKEN_USER *ptokusr;
  65. GetTokenInformation(hToken, TokenUser, NULL, 0, &dwInfoSize);
  66. if (ptokusr = LocalAlloc(LPTR, dwInfoSize))
  67. {
  68. if (GetTokenInformation(hToken, TokenUser, ptokusr,
  69. dwInfoSize, &dwInfoSize))
  70. {
  71. if (EqualSid(ptokusr->User.Sid, psidGroup))
  72. {
  73. PINFO(TEXT("YES"));
  74. fRet = TRUE;
  75. }
  76. }
  77. LocalFree(ptokusr);
  78. }
  79. }
  80. if (hToken)
  81. CloseHandle(hToken);
  82. PINFO(TEXT("\r\n"));
  83. return fRet;
  84. }