master
c 97 lines 2.69 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 #include "../libnetdata.h"
4
5 #if defined(OS_WINDOWS)
6 long netdata_registry_get_dword_from_open_key(unsigned int *out, void *lKey, char *name)
7 {
8 DWORD length = 260;
9 return RegQueryValueEx(lKey, name, NULL, NULL, (LPBYTE) out, &length);
10 }
11
12 bool netdata_registry_get_dword(unsigned int *out, void *hKey, char *subKey, char *name)
13 {
14 HKEY lKey;
15 bool status = true;
16 long ret = RegOpenKeyEx(hKey,
17 subKey,
18 0,
19 KEY_READ,
20 &lKey);
21 if (ret != ERROR_SUCCESS)
22 return false;
23
24 ret = netdata_registry_get_dword_from_open_key(out, lKey, name);
25 if (ret != ERROR_SUCCESS)
26 status = false;
27
28 RegCloseKey(lKey);
29
30 return status;
31 }
32
33 long netdata_registry_get_string_from_open_key(char *out, unsigned int length, void *lKey, char *name)
34 {
35 return RegQueryValueEx(lKey, name, NULL, NULL, (LPBYTE) out, &length);
36 }
37
38 bool netdata_registry_get_string(char *out, unsigned int length, void *hKey, char *subKey, char *name)
39 {
40 HKEY lKey;
41 bool status = true;
42 long ret = RegOpenKeyEx(hKey,
43 subKey,
44 0,
45 KEY_READ,
46 &lKey);
47 if (ret != ERROR_SUCCESS)
48 return false;
49
50 ret = netdata_registry_get_string_from_open_key(out, length, lKey, name);
51 if (ret != ERROR_SUCCESS)
52 status = false;
53
54 RegCloseKey(lKey);
55
56 return status;
57 }
58
59 bool EnableWindowsPrivilege(const char *privilegeName) {
60 HANDLE hToken;
61 LUID luid;
62 TOKEN_PRIVILEGES tkp;
63
64 // Open the process token with appropriate access rights
65 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
66 return false;
67
68 // Lookup the LUID for the specified privilege
69 if (!LookupPrivilegeValue(NULL, privilegeName, &luid)) {
70 CloseHandle(hToken); // Close the token handle before returning
71 return false;
72 }
73
74 // Set up the TOKEN_PRIVILEGES structure
75 tkp.PrivilegeCount = 1;
76 tkp.Privileges[0].Luid = luid;
77 tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
78
79 // Adjust the token's privileges
80 if (!AdjustTokenPrivileges(hToken, FALSE, &tkp, sizeof(tkp), NULL, NULL)) {
81 CloseHandle(hToken); // Close the token handle before returning
82 return false;
83 }
84
85 // Check if AdjustTokenPrivileges succeeded
86 if (GetLastError() == ERROR_NOT_ALL_ASSIGNED) {
87 CloseHandle(hToken); // Close the token handle before returning
88 return false;
89 }
90
91 // Close the handle to the token after success
92 CloseHandle(hToken);
93
94 return true;
95 }
96
97 #endif