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