source: trunk/gsinstaller/gsPlatform.cpp@ 1537

Last change on this file since 1537 was 1536, checked in by sjboddie, 24 years ago

Changes to get compiling on VC++ and gcc

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