util.WinNT.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /* Utilities - Windows NT specific utilities (not in Win95)
  2. Copyright (C) 1994, 1995, 1996 the Free Software Foundation.
  3. Written 1996 by Juan Grigera<grigera@isis.unlp.edu.ar>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. */
  16. #include <config.h>
  17. #include <windows.h>
  18. #include "util.win32.h"
  19. #include "util.debug.h"
  20. /* int winnt_IsAdministrator() - Determines whether user has Administrator (root)
  21. priviledges.
  22. Return: 1 if administrator
  23. 0 if not
  24. Note: Code taken from MSKbase Number: Q118626.
  25. To determine whether or not a user is an administrator, you need to examine
  26. the user's access token with GetTokenInformation(). The access token
  27. represents the user's privileges and the groups to which the user belongs.
  28. */
  29. int winnt_IsAdministrator()
  30. {
  31. HANDLE hAccessToken;
  32. UCHAR InfoBuffer[1024];
  33. PTOKEN_GROUPS ptgGroups = (PTOKEN_GROUPS)InfoBuffer;
  34. DWORD dwInfoBufferSize;
  35. PSID psidAdministrators;
  36. SID_IDENTIFIER_AUTHORITY siaNtAuthority = SECURITY_NT_AUTHORITY;
  37. UINT x;
  38. BOOL bSuccess;
  39. if(!OpenProcessToken(GetCurrentProcess(),TOKEN_READ,&hAccessToken))
  40. return 0;
  41. bSuccess = GetTokenInformation(hAccessToken,TokenGroups,InfoBuffer,
  42. 1024, &dwInfoBufferSize);
  43. CloseHandle(hAccessToken);
  44. if( !bSuccess )
  45. return 0;
  46. if(!AllocateAndInitializeSid(&siaNtAuthority, 2,
  47. SECURITY_BUILTIN_DOMAIN_RID,
  48. DOMAIN_ALIAS_RID_ADMINS,
  49. 0, 0, 0, 0, 0, 0,
  50. &psidAdministrators))
  51. return 0;
  52. bSuccess = 0;
  53. for(x=0;x<ptgGroups->GroupCount;x++) {
  54. if( EqualSid(psidAdministrators, ptgGroups->Groups[x].Sid) ) {
  55. bSuccess = 1;
  56. break;
  57. }
  58. }
  59. FreeSid(psidAdministrators);
  60. return bSuccess;
  61. }
  62. int geteuid ()
  63. {
  64. if (winnt_IsAdministrator())
  65. return 0;
  66. return 1;
  67. }