import-fs-formatting-to-windows-20031207
[openafs.git] / src / WINNT / client_config / isadmin.cpp
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 extern "C" {
11 #include <afs/param.h>
12 #include <afs/stds.h>
13 }
14
15 #include <windows.h>
16 #include <WINNT/TaLocale.h>
17
18
19 /*
20  * ISWINNT ____________________________________________________________________
21  *
22  */
23
24 BOOL IsWindowsNT (void)
25 {
26    static BOOL fChecked = FALSE;
27    static BOOL fIsWinNT = FALSE;
28
29    if (!fChecked)
30       {
31       fChecked = TRUE;
32
33       OSVERSIONINFO Version;
34       memset (&Version, 0x00, sizeof(Version));
35       Version.dwOSVersionInfoSize = sizeof(Version);
36
37       if (GetVersionEx (&Version))
38          {
39          if (Version.dwPlatformId == VER_PLATFORM_WIN32_NT)
40             fIsWinNT = TRUE;
41          }
42       }
43
44    return fIsWinNT;
45 }
46
47
48 /*
49  * ISADMIN ____________________________________________________________________
50  *
51  */
52
53 BOOL IsAdmin (void)
54 {
55    static BOOL fAdmin = FALSE;
56    static BOOL fTested = FALSE;
57    if (!fTested)
58       {
59       fTested = TRUE;
60
61       // Obtain the SID for BUILTIN\Administrators. If this is Windows NT,
62       // expect this call to succeed; if it does not, we can presume that
63       // it's not NT and therefore the user always has administrative
64       // privileges.
65       //
66       PSID psidAdmin = NULL;
67       SID_IDENTIFIER_AUTHORITY auth = SECURITY_NT_AUTHORITY;
68       if (!AllocateAndInitializeSid (&auth, 2, SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, &psidAdmin))
69          fAdmin = TRUE;
70       else
71          {
72
73          // Then open our current ProcessToken
74          //
75          HANDLE hToken;
76          if (OpenProcessToken (GetCurrentProcess(), TOKEN_QUERY, &hToken))
77             {
78
79             // We'll have to allocate a chunk of memory to store the list of
80             // groups to which this user belongs; find out how much memory
81             // we'll need.
82             //
83             DWORD dwSize = 0;
84             GetTokenInformation (hToken, TokenGroups, NULL, dwSize, &dwSize);
85             
86             // Allocate that buffer, and read in the list of groups.
87             //
88             PTOKEN_GROUPS pGroups = (PTOKEN_GROUPS)Allocate (dwSize);
89             if (GetTokenInformation (hToken, TokenGroups, pGroups, dwSize, &dwSize))
90                {
91                // Look through the list of group SIDs and see if any of them
92                // matches the Administrator group SID.
93                //
94                for (size_t iGroup = 0; (!fAdmin) && (iGroup < pGroups->GroupCount); ++iGroup)
95                   {
96                   if (EqualSid (psidAdmin, pGroups->Groups[ iGroup ].Sid))
97                      fAdmin = TRUE;
98                   }
99                }
100
101             if (pGroups)
102                Free (pGroups);
103             }
104          }
105
106       if (psidAdmin)
107          FreeSid (psidAdmin);
108       }
109
110    return fAdmin;
111 }
112