This repository has been archived by the owner on Mar 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 89
/
commands.cpp
417 lines (334 loc) · 11.3 KB
/
commands.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/*********************************************************************
* Copyright (c) Intel Corporation 2019 - 2020
* SPDX-License-Identifier: Apache-2.0
**********************************************************************/
#include "commands.h"
#include <string>
#include "port.h"
#include "utils.h"
#ifndef _WIN32
#include <string.h>
#endif
extern "C" {
#ifndef _WIN32
#include "HECILinux.h"
#endif
#include "PTHICommand.h"
#ifdef bool
#undef bool
#endif
}
#include "version.h"
bool cmd_is_admin()
{
if (heci_Init(NULL, PTHI_CLIENT) == 0) return false;
return true;
}
bool cmd_get_version(std::string& version)
{
version.clear();
// initialize HECI interface
if (heci_Init(NULL, PTHI_CLIENT) == 0) return false;
// get code version
CODE_VERSIONS codeVersion;
AMT_STATUS status = pthi_GetCodeVersions(&codeVersion);
// additional versions
if (status == 0)
{
for (int i = 0; i < (int) codeVersion.VersionsCount; i++)
{
if (strcmp((char*)(codeVersion.Versions[i].Description.String), "AMT") == 0)
{
version = ((char*)codeVersion.Versions[i].Version.String);
return true;
}
}
}
return false;
}
bool cmd_get_build_number(std::string& version)
{
version.clear();
// initialize HECI interface
if (heci_Init(NULL, PTHI_CLIENT) == 0) return false;
// get code version
CODE_VERSIONS codeVersion;
AMT_STATUS status = pthi_GetCodeVersions(&codeVersion);
// additional versions
if (status == 0)
{
for (int i = 0; i < (int) codeVersion.VersionsCount; i++)
{
if (strcmp((char*)(codeVersion.Versions[i].Description.String), "Build Number") == 0)
{
version = ((char*)codeVersion.Versions[i].Version.String);
return true;
}
}
}
return false;
}
bool cmd_get_sku(std::string& version)
{
version.clear();
// initialize HECI interface
if (heci_Init(NULL, PTHI_CLIENT) == 0) return false;
// get code version
CODE_VERSIONS codeVersion;
AMT_STATUS status = pthi_GetCodeVersions(&codeVersion);
// additional versions
if (status == 0)
{
for (int i = 0; i < (int) codeVersion.VersionsCount; i++)
{
if (strcmp((char*)(codeVersion.Versions[i].Description.String), "Sku") == 0)
{
version = ((char*)codeVersion.Versions[i].Version.String);
return true;
}
}
}
return false;
}
bool cmd_get_uuid(std::vector<unsigned char>& uuid)
{
uuid.clear();
// initialize HECI interface
if (heci_Init(NULL, PTHI_CLIENT) == 0) return false;
// get UUID
AMT_UUID amt_uuid;
AMT_STATUS amt_status = pthi_GetUUID(&amt_uuid);
if (amt_status == 0)
{
for (int i = 0; i < 16; i++)
{
uuid.push_back(amt_uuid[i]);
}
return true;
}
return false;
}
bool cmd_get_local_system_account(std::string& username, std::string& password)
{
username.clear();
password.clear();
// initialize HECI interface
if (heci_Init(NULL, PTHI_CLIENT) == 0) return false;
// get Local System Account
LOCAL_SYSTEM_ACCOUNT local_system_account;
AMT_STATUS amt_status = pthi_GetLocalSystemAccount(&local_system_account);
if (amt_status == 0)
{
username = local_system_account.username;
password = local_system_account.password;
return true;
}
return false;
}
bool cmd_get_control_mode(int& mode)
{
mode = 0;
// initialize HECI interface
if (heci_Init(NULL, PTHI_CLIENT) == 0) return false;
// get Control Mode
int controlMode;
AMT_STATUS amt_status = pthi_GetControlMode(&controlMode);
if (amt_status == 0)
{
mode = controlMode;
return true;
}
return false;
}
bool cmd_get_fqdn(fqdn_settings& fqdn_settings)
{
fqdn_settings.fqdn.clear();
// initialize HECI interface
if (heci_Init(NULL, PTHI_CLIENT) == 0) return false;
// get fqdn
CFG_GET_FQDN_RESPONSE fqdn;
memset(&fqdn, 0, sizeof(CFG_GET_FQDN_RESPONSE));
AMT_STATUS amt_status = pthi_GetHostFQDN(&fqdn);
if (amt_status == 0)
{
fqdn_settings.ddns_ttl = fqdn.DDNSTTL;
fqdn_settings.ddns_update_enabled = fqdn.DDNSUpdateEnabled;
fqdn_settings.ddns_update_interval = fqdn.DDNSPeriodicUpdateInterval;
if (fqdn.FQDN.Length > 0)
{
fqdn_settings.fqdn = std::string(fqdn.FQDN.Buffer, fqdn.FQDN.Length);
free(fqdn.FQDN.Buffer);
}
return true;
}
return false;
}
bool cmd_get_dns_suffix(std::string& suffix)
{
suffix.clear();
// initialize HECI interface
if (heci_Init(NULL, PTHI_CLIENT) == 0) return false;
// get DNS according to AMT
AMT_ANSI_STRING amt_dns_suffix;
AMT_STATUS amt_status = pthi_GetDnsSuffix(&amt_dns_suffix);
if (amt_status == 0)
{
if (amt_dns_suffix.Buffer != NULL)
{
std::string tmp(amt_dns_suffix.Buffer, amt_dns_suffix.Length);
suffix = tmp;
free(amt_dns_suffix.Buffer);
}
return true;
}
return false;
}
bool cmd_get_wired_mac_address(std::vector<unsigned char>& address)
{
address.clear();
// initialize HECI interface
if (heci_Init(NULL, PTHI_CLIENT) == 0) return false;
// get wired interface
LAN_SETTINGS lan_settings;
UINT32 interface_settings = 0; // wired=0, wireless=1
AMT_STATUS amt_status = pthi_GetLanInterfaceSettings(interface_settings, &lan_settings);
if (amt_status == 0)
{
if (!lan_settings.Enabled)
{
return false;
}
for (int i = 0; i < 6; i++)
{
address.push_back(lan_settings.MacAddress[i]);
}
return true;
}
return false;
}
bool cmd_get_certificate_hashes(std::vector<cert_hash_entry>& hash_entries)
{
hash_entries.clear();
// initialize HECI interface
if (heci_Init(NULL, PTHI_CLIENT) == 0) return false;
// get the hash handles
AMT_HASH_HANDLES amt_hash_handles;
CERTHASH_ENTRY certhash_entry;
memset(&amt_hash_handles, 0, sizeof(AMT_HASH_HANDLES));
if (pthi_EnumerateHashHandles(&amt_hash_handles) == 0)
{
for (int i = 0; i < (int) amt_hash_handles.Length; i++)
{
// get each entry
AMT_STATUS amt_status = pthi_GetCertificateHashEntry(amt_hash_handles.Handles[i], &certhash_entry);
if (amt_status == 0)
{
int hashSize;
cert_hash_entry tmp;
switch (certhash_entry.HashAlgorithm) {
case 0: // MD5
hashSize = 16;
tmp.algorithm = "MD5";
break;
case 1: // SHA1
hashSize = 20;
tmp.algorithm = "SHA1";
break;
case 2: // SHA256
hashSize = 32;
tmp.algorithm = "SHA256";
break;
case 3: // SHA512
hashSize = 64;
tmp.algorithm = "SHA512";
break;
default:
hashSize = 0;
tmp.algorithm = "UNKNOWN";
break;
}
if (certhash_entry.IsActive == 1)
{
std::string cert_name(certhash_entry.Name.Buffer, certhash_entry.Name.Length);
tmp.name = cert_name;
tmp.is_default = certhash_entry.IsDefault;
tmp.is_active = certhash_entry.IsActive;
std::string hashString;
for (int i = 0; i < hashSize; i++)
{
char hex[10];
snprintf(hex, 10, "%02x", certhash_entry.CertificateHash[i]);
hashString += hex;
}
tmp.hash = hashString;
hash_entries.push_back(tmp);
}
}
}
return true;
}
return false;
}
bool cmd_get_remote_access_connection_status(int& network_status, int& remote_status, int& remote_trigger, std::string& mps_hostname)
{
network_status = 0;
remote_status = 0;
remote_trigger = 0;
mps_hostname = "";
const int MPS_SERVER_MAXLENGTH = 256;
// initialize HECI interface
if (heci_Init(NULL, PTHI_CLIENT) == 0) return false;
// get DNS according to AMT
REMOTE_ACCESS_STATUS remote_access_connection_status;
AMT_STATUS amt_status = pthi_GetRemoteAccessConnectionStatus(&remote_access_connection_status);
if (amt_status == 0)
{
network_status = remote_access_connection_status.AmtNetworkConnectionStatus;
remote_status = remote_access_connection_status.RemoteAccessConnectionStatus;
remote_trigger = remote_access_connection_status.RemoteAccessConnectionTrigger;
if (remote_access_connection_status.MpsHostname.Buffer != NULL)
{
if (remote_access_connection_status.MpsHostname.Length < MPS_SERVER_MAXLENGTH)
{
std::string tmp(remote_access_connection_status.MpsHostname.Buffer, remote_access_connection_status.MpsHostname.Length);
if (util_is_printable(tmp))
{
mps_hostname = tmp;
}
}
free(remote_access_connection_status.MpsHostname.Buffer);
}
return true;
}
return false;
}
bool cmd_get_lan_interface_settings(lan_interface_settings& lan_interface_settings, bool wired_interface)
{
// initialize HECI interface
if (heci_Init(NULL, PTHI_CLIENT) == 0) return false;
// get wired interface
LAN_SETTINGS lan_settings;
UINT32 interface_settings = (wired_interface) ? 0 : 1; // wired=0, wireless=1
AMT_STATUS amt_status = pthi_GetLanInterfaceSettings(interface_settings, &lan_settings);
if (amt_status == 0)
{
lan_interface_settings.is_enabled = lan_settings.Enabled;
lan_interface_settings.dhcp_mode = lan_settings.DhcpIpMode;
lan_interface_settings.dhcp_enabled = lan_settings.DhcpEnabled;
lan_interface_settings.link_status = lan_settings.LinkStatus;
lan_interface_settings.ip_address.clear();
lan_interface_settings.ip_address.push_back((lan_settings.Ipv4Address >> 24) & 0xff);
lan_interface_settings.ip_address.push_back((lan_settings.Ipv4Address >> 16) & 0xff);
lan_interface_settings.ip_address.push_back((lan_settings.Ipv4Address >> 8) & 0xff);
lan_interface_settings.ip_address.push_back((lan_settings.Ipv4Address) & 0xff);
lan_interface_settings.mac_address.clear();
lan_interface_settings.mac_address.push_back(lan_settings.MacAddress[0]);
lan_interface_settings.mac_address.push_back(lan_settings.MacAddress[1]);
lan_interface_settings.mac_address.push_back(lan_settings.MacAddress[2]);
lan_interface_settings.mac_address.push_back(lan_settings.MacAddress[3]);
lan_interface_settings.mac_address.push_back(lan_settings.MacAddress[4]);
lan_interface_settings.mac_address.push_back(lan_settings.MacAddress[5]);
return true;
}
return false;
}