1 | #include "gsPlatform.h"
|
---|
2 |
|
---|
3 | #define ACCESS_READ 0x01
|
---|
4 | #define ACCESS_WRITE 0x02
|
---|
5 |
|
---|
6 | gsPlatform::gsPlatform()
|
---|
7 | { this->platformInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
|
---|
8 | GetVersionEx(&this->platformInfo);
|
---|
9 | }
|
---|
10 |
|
---|
11 | bool gsPlatform::isWindows9x()
|
---|
12 | { return (this->platformInfo.dwPlatformId & VER_PLATFORM_WIN32_WINDOWS);
|
---|
13 | }
|
---|
14 |
|
---|
15 | bool gsPlatform::isWindowsNT()
|
---|
16 | { return (this->platformInfo.dwPlatformId & VER_PLATFORM_WIN32_NT);
|
---|
17 | }
|
---|
18 |
|
---|
19 | bool gsPlatform::isWindows32s()
|
---|
20 | { return (this->platformInfo.dwPlatformId & VER_PLATFORM_WIN32s);
|
---|
21 | }
|
---|
22 |
|
---|
23 | bool gsPlatform::isOldWindows32s()
|
---|
24 | { if (this->platformInfo.dwMajorVersion == 0 ||
|
---|
25 | this->platformInfo.dwMinorVersion < 30)
|
---|
26 | { return true;
|
---|
27 | }
|
---|
28 | return false;
|
---|
29 | }
|
---|
30 |
|
---|
31 | bool gsPlatform::isExplorerShell()
|
---|
32 | { if (this->isWindows9x() ||
|
---|
33 | (this->isWindowsNT() && this->platformInfo.dwMajorVersion >= 4))
|
---|
34 | { return true;
|
---|
35 | }
|
---|
36 | return false;
|
---|
37 | }
|
---|
38 |
|
---|
39 | bool gsPlatform::isUserAdministrator()
|
---|
40 | { if (this->isWindowsNT())
|
---|
41 | { HANDLE token;
|
---|
42 | PSID adminId;
|
---|
43 | PACL aclData;
|
---|
44 | PSECURITY_DESCRIPTOR psdAdmin;
|
---|
45 | SID_IDENTIFIER_AUTHORITY systemSidAuthority;
|
---|
46 | GENERIC_MAPPING genericMapping;
|
---|
47 | PRIVILEGE_SET privilegeSet;
|
---|
48 | DWORD aclSize, status, structureSize;
|
---|
49 | BOOL result;
|
---|
50 |
|
---|
51 | // Get an impersonation token
|
---|
52 | ImpersonateSelf(SecurityImpersonation);
|
---|
53 |
|
---|
54 | if (!OpenThreadToken(GetCurrentThread(), TOKEN_QUERY, FALSE, &token))
|
---|
55 | { if (GetLastError() != ERROR_NO_TOKEN)
|
---|
56 | { return false;
|
---|
57 | }
|
---|
58 |
|
---|
59 | // if we didn't have an access token, take the process token instead
|
---|
60 | if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &token))
|
---|
61 | { return false;
|
---|
62 | }
|
---|
63 | }
|
---|
64 |
|
---|
65 | // By now we have a valid process/thread token; now get information on the
|
---|
66 | // admin group in this domain
|
---|
67 | if (!AllocateAndInitializeSid(&systemSidAuthority, 2, SECURITY_BUILTIN_DOMAIN_RID,
|
---|
68 | DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0,
|
---|
69 | &adminId))
|
---|
70 | { return false;
|
---|
71 | }
|
---|
72 |
|
---|
73 | // allocate space for security descriptor
|
---|
74 | psdAdmin = LocalAlloc(LPTR, SECURITY_DESCRIPTOR_MIN_LENGTH);
|
---|
75 | if (psdAdmin == NULL)
|
---|
76 | { return false;
|
---|
77 | }
|
---|
78 |
|
---|
79 | // initialise descriptor
|
---|
80 | if (!InitializeSecurityDescriptor(psdAdmin, SECURITY_DESCRIPTOR_REVISION))
|
---|
81 | { return false;
|
---|
82 | }
|
---|
83 |
|
---|
84 | // get ACL size and allocate ACL block
|
---|
85 | aclSize = sizeof(ACL) + sizeof(ACCESS_ALLOWED_ACE) + GetLengthSid(adminId) - sizeof(DWORD);
|
---|
86 | aclData = (PACL) LocalAlloc(LPTR, aclSize);
|
---|
87 | if (aclData == NULL)
|
---|
88 | { return false;
|
---|
89 | }
|
---|
90 |
|
---|
91 | // initalise the acl block
|
---|
92 | if (!InitializeAcl(aclData, aclSize, ACL_REVISION2))
|
---|
93 | { return false;
|
---|
94 | }
|
---|
95 |
|
---|
96 | // try to add the given item to the Access Control Entry (ACE) list (ACL)
|
---|
97 | if (!AddAccessAllowedAce(aclData, ACL_REVISION2, ACCESS_READ | ACCESS_WRITE, adminId))
|
---|
98 | { return false;
|
---|
99 | }
|
---|
100 |
|
---|
101 | // endeavour to add the acl to the discretionary acl (DACL)
|
---|
102 | if (!SetSecurityDescriptorDacl(psdAdmin, TRUE, aclData, FALSE))
|
---|
103 | { return false;
|
---|
104 | }
|
---|
105 |
|
---|
106 | // endeavour to set the owner/group identification
|
---|
107 | SetSecurityDescriptorGroup(psdAdmin, adminId, FALSE);
|
---|
108 | SetSecurityDescriptorOwner(psdAdmin, adminId, FALSE);
|
---|
109 |
|
---|
110 | // right, we've now built up a discretionary acl on the current user,
|
---|
111 | // finally get the generic mapping ready
|
---|
112 | genericMapping.GenericRead = ACCESS_READ;
|
---|
113 | genericMapping.GenericWrite = ACCESS_WRITE;
|
---|
114 | genericMapping.GenericExecute = 0;
|
---|
115 | genericMapping.GenericAll = ACCESS_READ | ACCESS_WRITE;
|
---|
116 |
|
---|
117 | // do the actual access check
|
---|
118 | if (!AccessCheck( psdAdmin, token, ACCESS_READ, &genericMapping,
|
---|
119 | &privilegeSet, &structureSize, &status, &result))
|
---|
120 | { return false;
|
---|
121 | }
|
---|
122 |
|
---|
123 | // end impersonisation
|
---|
124 | RevertToSelf();
|
---|
125 | return result;
|
---|
126 | }
|
---|
127 | return true;
|
---|
128 | }
|
---|
129 |
|
---|
130 | bool gsPlatform::reboot()
|
---|
131 | {
|
---|
132 | // Check for appropriate privileges if this is NT
|
---|
133 | if (this->isWindowsNT())
|
---|
134 | { HANDLE token;
|
---|
135 | TOKEN_PRIVILEGES tkp;
|
---|
136 |
|
---|
137 | if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
|
---|
138 | &token))
|
---|
139 | { return false;
|
---|
140 | }
|
---|
141 |
|
---|
142 | LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid);
|
---|
143 |
|
---|
144 | tkp.PrivilegeCount = 1; // set one privilege
|
---|
145 | tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
|
---|
146 |
|
---|
147 | // Get the actual shutdown privilege
|
---|
148 | AdjustTokenPrivileges(token, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES) NULL, 0);
|
---|
149 |
|
---|
150 | // as we can't test a return value on the previous call, use GetLastError
|
---|
151 | if (GetLastError() != ERROR_SUCCESS)
|
---|
152 | { return false;
|
---|
153 | }
|
---|
154 | }
|
---|
155 | return ExitWindowsEx(EWX_REBOOT | EWX_FORCE, 0);
|
---|
156 | }
|
---|
157 |
|
---|
158 | string gsPlatform::platformString()
|
---|
159 | { if (this->isWindowsNT())
|
---|
160 | { return gsPlatform_WINDOWSNT;
|
---|
161 | }
|
---|
162 | else if (this->isWindows9x())
|
---|
163 | { return gsPlatform_WINDOWS9X;
|
---|
164 | }
|
---|
165 | else if (this->isWindows32s())
|
---|
166 | { return gsPlatform_WINDOWS32S;
|
---|
167 | }
|
---|
168 | else
|
---|
169 | { return gsPlatform_WINDOWS;
|
---|
170 | }
|
---|
171 | } |
---|