This repository has been archived by the owner on Jan 6, 2020. It is now read-only.
forked from cavaliercoder/sysinv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
os.cpp
230 lines (183 loc) · 5.92 KB
/
os.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#include "stdafx.h"
#include "sysinv.h"
#include <sddl.h>
PNODE GetOsVersionDetail();
PNODE GetOsIdentityDetail();
PNODE GetOperatingSystemDetail()
{
PNODE node = NULL;
PNODE osNode = node_alloc(_T("OperatingSystem"), 0);
if (NULL != (node = GetOsVersionDetail()))
node_append_child(osNode, node);
if (NULL != (node = GetOsIdentityDetail()))
node_append_child(osNode, node);
return osNode;
}
PNODE GetOsVersionDetail()
{
PNODE node = node_alloc(L"VersionInfo", 0);
OSVERSIONINFOEX osinfo;
TCHAR strBuffer[MAX_PATH + 1];
HKEY hKey = 0;
DWORD dwType = REG_SZ;
DWORD bufferSize;
osinfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
GetVersionEx((LPOSVERSIONINFOW) &osinfo);
// Get product name from
// HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProductName
RegOpenKey(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion", &hKey);
RegQueryValueEx(hKey, L"ProductName", NULL, &dwType, (LPBYTE) &strBuffer, &bufferSize);
node_att_set(node, L"Name", strBuffer, 0);
// Break down OS name
switch(osinfo.wProductType) {
case VER_NT_WORKSTATION:
switch(osinfo.dwMajorVersion) {
case 5:
switch(osinfo.dwMinorVersion) {
case 0:
wcscpy(strBuffer, L"Windows 2000");
break;
case 1:
wcscpy(strBuffer, L"Windows XP");
break;
}
break;
case 6:
switch(osinfo.dwMinorVersion) {
case 0:
wcscpy(strBuffer, L"Windows Vista");
break;
case 1:
wcscpy(strBuffer, L"Windows 7");
break;
case 2:
wcscpy(strBuffer, L"Windows 8");
break;
case 3:
wcscpy(strBuffer, L"Windows 8.1");
break;
}
}
break;
case VER_NT_DOMAIN_CONTROLLER:
case VER_NT_SERVER:
switch(osinfo.dwMajorVersion) {
case 5:
if (0 == osinfo.dwMinorVersion) {
wcscpy(strBuffer, L"Windows 2000 Server");
}
else {
// R2?
if (0 != GetSystemMetrics(SM_SERVERR2))
wcscpy(strBuffer, L"Windows Server 2003 R2");
else
wcscpy(strBuffer, L"Windows Server 2003");
}
break;
case 6:
switch(osinfo.dwMinorVersion) {
case 0:
wcscpy(strBuffer, L"Windows Server 2008");
break;
case 1:
wcscpy(strBuffer, L"Windows Server 2008 R2");
break;
case 2:
wcscpy(strBuffer, L"Windows Server 2012");
break;
case 3:
wcscpy(strBuffer, L"Windows Server 2012 R2");
break;
}
}
break;
default:
wcscpy(strBuffer, L"Unknown");
break;
}
node_att_set(node, L"BaseName", strBuffer, 0);
// Determine OS Edition (Needs to move into version specific logic)
if(VER_SUITE_BLADE & osinfo.wSuiteMask)
node_att_set(node, L"Edition", L"Web Edition", 0);
else if(VER_SUITE_COMPUTE_SERVER & osinfo.wSuiteMask)
node_att_set(node, L"Edition", L"Compute Cluster Edition", 0);
else if (VER_SUITE_DATACENTER & osinfo.wSuiteMask)
node_att_set(node, L"Edition", L"Datacenter Edition", 0);
else if (VER_SUITE_ENTERPRISE & osinfo.wSuiteMask)
node_att_set(node, L"Edition", L"Enterprise", 0);
else if (VER_SUITE_EMBEDDEDNT & osinfo.wSuiteMask)
node_att_set(node, L"Edition", L"Embedded", 0);
else if (VER_SUITE_PERSONAL & osinfo.wSuiteMask)
node_att_set(node, L"Edition", L"Home", 0);
else
node_att_set(node, L"Edition", L"Standard Edition", 0);
// OS Version numbers
swprintf(strBuffer, L"%u.%u.%u", osinfo.dwMajorVersion, osinfo.dwMinorVersion, osinfo.dwBuildNumber);
node_att_set(node, L"Version", strBuffer, 0);
// Service pack
swprintf(strBuffer, L"%u.%u", osinfo.wServicePackMajor, osinfo.wServicePackMinor);
node_att_set(node, L"ServicePack", strBuffer, 0);
switch(osinfo.wProductType) {
case VER_NT_DOMAIN_CONTROLLER:
node_att_set(node, L"Role", L"Domain Controller", 0);
break;
case VER_NT_SERVER:
node_att_set(node, L"Role", L"Server", 0);
break;
case VER_NT_WORKSTATION:
node_att_set(node, L"Role", L"Workstation", 0);
break;
}
return node;
}
PNODE GetOsIdentityDetail()
{
PNODE identityNode = NULL;
TCHAR *c = NULL;
TCHAR computerName[MAX_COMPUTERNAME_LENGTH + 1];
TCHAR computerAccountName[MAX_COMPUTERNAME_LENGTH + 2];
LPTSTR domainName = NULL;
LPTSTR szSid = NULL;
DWORD bufferlen = MAX_COMPUTERNAME_LENGTH + 1;
DWORD cbSid = 0;
DWORD refDomainLen = 0;
PSID sid = NULL;
SID_NAME_USE sidNameUse;
LPCTSTR pszSubKey = _T("SOFTWARE\\Microsoft\\Cryptography");
HKEY hKey = 0;
TCHAR machineGuid[38];
DWORD machineGuidLen = sizeof(machineGuid);
identityNode = node_alloc(_T("Identification"), 0);
DWORD result = 0;
// Get host name (for machine account)
GetComputerName(computerName, &bufferlen);
node_att_set(identityNode, _T("ComputerName"), computerName, 0);
// Append '\' to build machine account name
swprintf(computerAccountName, _T("%s\\"), computerName);
// Get required buffer sizes
if (! LookupAccountName(NULL, computerAccountName, NULL, &cbSid, NULL, &refDomainLen, &sidNameUse)) {
// Allocate
sid = (PSID) new BYTE[cbSid];
domainName = new TCHAR[refDomainLen];
// Get SID and domain
if (LookupAccountName(NULL, computerAccountName, sid, &cbSid, domainName, &refDomainLen, &sidNameUse)) {
node_att_set(identityNode, _T("Domain"), domainName, 0);
// Convert SID to string
if (ConvertSidToStringSid(sid, &szSid)) {
node_att_set(identityNode, _T("MachineSid"), szSid, 0);
LocalFree(szSid);
}
}
}
// Get Cryptography GUID
if (ERROR_SUCCESS == (result = RegOpenKeyEx(HKEY_LOCAL_MACHINE, pszSubKey, 0, KEY_READ, &hKey))){
if (ERROR_SUCCESS == (result = RegQueryValueEx(hKey, _T("MachineGuid"), NULL, NULL, (LPBYTE)&machineGuid, &machineGuidLen))) {
node_att_set(identityNode, _T("MachineGuid"), machineGuid, NAFLG_FMT_GUID);
}
RegCloseKey(hKey);
}
else {
SetError(ERR_WARN, GetLastError(), _T("Failed to get Machine GUID from Registry"));
}
return identityNode;
}