stdex
Additional custom or not Standard C++ covered algorithms
Loading...
Searching...
No Matches
system.hpp
1/*
2 SPDX-License-Identifier: MIT
3 Copyright © 2023 Amebis
4*/
5
6#pragma once
7
8#ifdef _WIN32
9#ifndef NOMINMAX
10#define NOMINMAX // Collides with std::min/max
11#endif
12#include <windows.h>
13#include <intrin.h>
14#include <intsafe.h>
15#include <oaidl.h>
16#include <tchar.h>
17#else
18#define _LARGEFILE64_SOURCE
19#include <grp.h>
20#include <pwd.h>
21#include <string.h>
22#include <sys/types.h>
23#include <unistd.h>
24#endif
25#include "compat.hpp"
26#include <regex>
27#include <stdexcept>
28#include <string>
29
30// In case somebody #included <windows.h> before us and didn't #define NOMINMAX
31#ifdef _WIN32
32#ifdef min
33#undef min
34#endif
35#ifdef max
36#undef max
37#endif
38#endif
39
40#if defined(_WIN32)
41#define PATH_SEPARATOR '\\'
42#define PATH_SEPARATOR_STR "\\"
43#else
44#define PATH_SEPARATOR '/'
45#define PATH_SEPARATOR_STR "/"
46#endif
47
48namespace stdex
49{
53#if defined(_WIN32)
54 using sys_handle = HANDLE;
55 const sys_handle invalid_handle = INVALID_HANDLE_VALUE;
56#else
57 using sys_handle = int;
58 const sys_handle invalid_handle = (sys_handle)-1;
59#endif
60
64#if defined(_WIN32)
65 inline DWORD sys_error() { return GetLastError(); }
66#else
67 inline int sys_error() { return errno; }
68#endif
69
73#if defined(_WIN32)
74 using schar_t = TCHAR;
75#else
76 using schar_t = char;
77#define _T(x) x
78#endif
79
84 using sys_char = schar_t;
85
89 using sstring = std::basic_string<stdex::schar_t>;
90
95 using sys_string = sstring;
96
100 using sregex = std::basic_regex<stdex::schar_t>;
101
106 {
107 public:
108 sys_object(_In_opt_ sys_handle h = invalid_handle) : m_h(h) {}
109
110 sys_object(_In_ const sys_object& other) : m_h(other.m_h != invalid_handle ? duplicate(other.m_h, false) : invalid_handle) {}
111
112 sys_object& operator =(_In_ const sys_object& other)
113 {
114 if (this != std::addressof(other)) {
115 if (m_h != invalid_handle)
116 close(m_h);
117 m_h = other.m_h != invalid_handle ? duplicate(other.m_h, false) : invalid_handle;
118 }
119 return *this;
120 }
121
122 sys_object(_Inout_ sys_object&& other) noexcept : m_h(other.m_h)
123 {
124 other.m_h = invalid_handle;
125 }
126
127 sys_object& operator =(_Inout_ sys_object&& other) noexcept
128 {
129 if (this != std::addressof(other)) {
130 if (m_h != invalid_handle)
131 close(m_h);
132 m_h = other.m_h;
133 other.m_h = invalid_handle;
134 }
135 return *this;
136 }
137
138 virtual ~sys_object() noexcept(false)
139 {
140 if (m_h != invalid_handle)
141 close(m_h);
142 }
143
147 virtual void close()
148 {
149 if (m_h != invalid_handle) {
150 close(m_h);
151 m_h = invalid_handle;
152 }
153 }
154
158 inline operator bool() const noexcept { return m_h != invalid_handle; }
159
163 inline sys_handle get() const noexcept { return m_h; }
164
165 protected:
169 static void close(_In_ sys_handle h)
170 {
171#ifdef _WIN32
172 if (CloseHandle(h) || GetLastError() == ERROR_INVALID_HANDLE)
173 return;
174 throw std::system_error(GetLastError(), std::system_category(), "CloseHandle failed");
175#else
176 if (::close(h) >= 0 || errno == EBADF)
177 return;
178 throw std::system_error(errno, std::system_category(), "close failed");
179#endif
180 }
181
185 static sys_handle duplicate(_In_ sys_handle h, _In_ bool inherit)
186 {
187 sys_handle h_new;
188#ifdef _WIN32
189 HANDLE process = GetCurrentProcess();
190 if (DuplicateHandle(process, h, process, &h_new, 0, inherit, DUPLICATE_SAME_ACCESS))
191 return h_new;
192 throw std::system_error(GetLastError(), std::system_category(), "DuplicateHandle failed");
193#else
194 _Unreferenced_(inherit);
195 if ((h_new = dup(h)) >= 0)
196 return h_new;
197 throw std::system_error(errno, std::system_category(), "dup failed");
198#endif
199 }
200
201 protected:
202 sys_handle m_h;
203 };
204
205#ifdef _WIN32
206 template <class T>
207 class safearray_accessor
208 {
209 public:
210 safearray_accessor(_In_ LPSAFEARRAY sa) : m_sa(sa)
211 {
212 HRESULT hr = SafeArrayAccessData(sa, reinterpret_cast<void HUGEP**>(&m_data));
213 if (FAILED(hr))
214 throw std::system_error(hr, std::system_category(), "SafeArrayAccessData failed");
215 }
216
217 ~safearray_accessor()
218 {
219 SafeArrayUnaccessData(m_sa);
220 }
221
222 T* data() const { return m_data; }
223
224 protected:
225 LPSAFEARRAY m_sa;
226 T* m_data;
227 };
228
232 struct SafeArrayDestroy_delete
233 {
237 void operator()(_In_ LPSAFEARRAY sa) const
238 {
239 SafeArrayDestroy(sa);
240 }
241 };
242
246 struct SysFreeString_delete
247 {
251 void operator()(_In_ BSTR sa) const
252 {
253 SysFreeString(sa);
254 }
255 };
256#endif
257
261#ifdef _WIN32
262 typedef uint16_t platform_id;
263#else
264 typedef const char* platform_id;
265
266 inline bool operator ==(_In_ const platform_id a, _In_ const platform_id b) { return a == b; }
267 inline bool operator !=(_In_ const platform_id a, _In_ const platform_id b) { return a != b; }
268 inline bool operator <(_In_ const platform_id a, _In_ const platform_id b) { return a == IMAGE_FILE_MACHINE_UNKNOWN && b != IMAGE_FILE_MACHINE_UNKNOWN || a != IMAGE_FILE_MACHINE_UNKNOWN && b != IMAGE_FILE_MACHINE_UNKNOWN && strcmp(a, b) < 0; }
269 inline bool operator <=(_In_ const platform_id a, _In_ const platform_id b) { return a == IMAGE_FILE_MACHINE_UNKNOWN || a != IMAGE_FILE_MACHINE_UNKNOWN && b != IMAGE_FILE_MACHINE_UNKNOWN && strcmp(a, b) <= 0; }
270 inline bool operator >(_In_ const platform_id a, _In_ const platform_id b) { return a != IMAGE_FILE_MACHINE_UNKNOWN && b == IMAGE_FILE_MACHINE_UNKNOWN || a != IMAGE_FILE_MACHINE_UNKNOWN && b != IMAGE_FILE_MACHINE_UNKNOWN && strcmp(a, b) > 0; }
271 inline bool operator >=(_In_ const platform_id a, _In_ const platform_id b) { return b == IMAGE_FILE_MACHINE_UNKNOWN || a != IMAGE_FILE_MACHINE_UNKNOWN && b != IMAGE_FILE_MACHINE_UNKNOWN && strcmp(a, b) >= 0; }
272#endif
273}
274
275#ifndef _WIN32
276constexpr stdex::platform_id IMAGE_FILE_MACHINE_UNKNOWN = nullptr;
277constexpr stdex::platform_id IMAGE_FILE_MACHINE_I386 = "i386";
278constexpr stdex::platform_id IMAGE_FILE_MACHINE_AMD64 = "x86_64";
279constexpr stdex::platform_id IMAGE_FILE_MACHINE_ARMNT = "arm";
280constexpr stdex::platform_id IMAGE_FILE_MACHINE_ARM64 = "aarch64";
281#endif
282
283namespace stdex
284{
288 const struct sys_info_t
289 {
293#if _M_IX86
294 static constexpr platform_id process_platform = IMAGE_FILE_MACHINE_I386;
295#elif _M_X64 // _M_ARM64EC is introducing as x64
296 static constexpr platform_id process_platform = IMAGE_FILE_MACHINE_AMD64;
297#elif _M_ARM
298 static constexpr platform_id process_platform = IMAGE_FILE_MACHINE_ARMNT;
299#elif _M_ARM64
300 static constexpr platform_id process_platform = IMAGE_FILE_MACHINE_ARM64;
301#elif __i386__
302 static constexpr platform_id process_platform = "i386";
303#elif __x86_64__
304 static constexpr platform_id process_platform = "x86_64";
305#elif __aarch64__
306 static constexpr platform_id process_platform = "aarch64";
307#else
308 #error Unknown platform
309#endif
310
314 platform_id os_platform;
315
316#ifdef _WIN32
320 bool wow64;
321#endif
322
327
331 bool admin;
332
337
338 sys_info_t() :
339 os_platform(IMAGE_FILE_MACHINE_UNKNOWN),
340 wow64(false),
342 admin(false),
343 elevated(false)
344 {
345#ifdef _WIN32
346 HMODULE kernel32_handle;
347 kernel32_handle = LoadLibrary(_T("kernel32.dll"));
348 _Assume_(kernel32_handle);
349 BOOL(WINAPI * IsWow64Process2)(HANDLE hProcess, USHORT * pProcessMachine, USHORT * pNativeMachine);
350 *reinterpret_cast<FARPROC*>(&IsWow64Process2) = GetProcAddress(kernel32_handle, "IsWow64Process2");
351 HANDLE process = GetCurrentProcess();
352 USHORT process_machine;
353#ifndef _WIN64
354 BOOL Wow64Process;
355#endif
356 if (IsWow64Process2 && IsWow64Process2(process, &process_machine, &os_platform)) {
357 wow64 = process_machine != IMAGE_FILE_MACHINE_UNKNOWN;
358 }
359#ifdef _WIN64
360 else {
361 os_platform = process_platform;
362 wow64 = false;
363 }
364#else
365 else if (IsWow64Process(process, &Wow64Process)) {
366 if (Wow64Process) {
367 os_platform = IMAGE_FILE_MACHINE_AMD64;
368 wow64 = true;
369 }
370 else {
371 os_platform = process_platform;
372 wow64 = false;
373 }
374 }
375#endif
376 FreeLibrary(kernel32_handle);
377#else
378 memset(&m_utsn, 0, sizeof(m_utsn));
379 if (uname(&m_utsn) != -1)
380 os_platform = reinterpret_cast<platform_id>(m_utsn.machine);
381#endif
382
383#ifdef _WIN32
384 HWINSTA hWinSta = GetProcessWindowStation();
385 if (hWinSta) {
386 TCHAR sName[MAX_PATH];
387 if (GetUserObjectInformation(hWinSta, UOI_NAME, sName, sizeof(sName), NULL)) {
388 sName[_countof(sName) - 1] = 0;
389 // Only "WinSta0" is interactive (Source: KB171890)
390 interactive_process = _tcsicmp(sName, _T("WinSta0")) == 0;
391 }
392 }
393#else
394 // TODO: Research interactive process vs service/agent/daemon on this platform.
395#endif
396
397#if defined(_WIN32)
398 {
399 HANDLE token_h;
400 if (OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &token_h)) {
401 sys_object token(token_h);
402
403 TOKEN_ELEVATION elevation;
404 DWORD size = sizeof(TOKEN_ELEVATION);
405 if (GetTokenInformation(token_h, TokenElevation, &elevation, sizeof(elevation), &size))
406 elevated = elevation.TokenIsElevated;
407
408 GetTokenInformation(token.get(), TokenGroups, NULL, 0, &size);
409 std::unique_ptr<TOKEN_GROUPS> groups((TOKEN_GROUPS*)new uint8_t[size]);
410 if (GetTokenInformation(token.get(), TokenGroups, (LPVOID)groups.get(), size, &size)) {
411 SID_IDENTIFIER_AUTHORITY authority = SECURITY_NT_AUTHORITY;
412 PSID sid_admins_h = NULL;
413 if (AllocateAndInitializeSid(&authority, 2, SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, &sid_admins_h)) {
414 struct SID_delete { void operator()(_In_ PSID p) const { FreeSid(p); } };
415 std::unique_ptr<void, SID_delete> sid_admins(sid_admins_h);
416 for (DWORD i = 0; i < groups->GroupCount; ++i)
417 if (EqualSid(sid_admins.get(), groups->Groups[i].Sid)) {
418 admin = true;
419 break;
420 }
421 }
422 }
423 }
424 }
425#elif defined(__APPLE__)
426 {
427 gid_t gids[NGROUPS + 1]; // A user cannot be member in more than NGROUPS groups, not counting the default group (hence the + 1)
428 for (int i = 0, n = getgroups(_countof(gids), gids); i < n; ++i) {
429 struct group* group = getgrgid(gids[i]);
430 if (!group) continue;
431 if (strcmp(group->gr_name, "admin") == 0) {
432 admin = true;
433 break;
434 }
435 }
436 }
437
438 elevated = geteuid() == 0;
439#else
440 // TODO: Set admin.
441 elevated = geteuid() == 0;
442#endif
443 }
444
445 protected:
446#ifndef _WIN32
447 struct utsname m_utsn;
448#endif
449 } sys_info;
450}
Operating system object (file, pipe, anything with an OS handle etc.)
Definition system.hpp:106
sys_handle get() const noexcept
Returns object handle.
Definition system.hpp:163
virtual void close()
Closes object.
Definition system.hpp:147
static sys_handle duplicate(sys_handle h, bool inherit)
Duplicates given object.
Definition system.hpp:185
static void close(sys_handle h)
Closes object.
Definition system.hpp:169
System information.
Definition system.hpp:289
bool admin
Is member of local group Administrators (Windows) or member of group wheel/sudoers (others)?
Definition system.hpp:331
bool elevated
Is elevated process (Windows) or running as root (others)?
Definition system.hpp:336
platform_id os_platform
The platform this process was compiled for.
Definition system.hpp:314
bool interactive_process
Is interactive process?
Definition system.hpp:326