-
Notifications
You must be signed in to change notification settings - Fork 7
/
packages.cpp
414 lines (342 loc) · 13.1 KB
/
packages.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
/*
* Complements to:
* http://mariusbancila.ro/blog/2011/05/01/finding-installed-applications-with-vc/
*
* Potential registry locations of hotfixes per version:
* http://support.microsoft.com/kb/184305
* http://support.microsoft.com/kb/262841
* HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing\Packages
*/
#include "stdafx.h"
#include "sysinv.h"
#include <time.h>
#define PACKAGES_REG_PATH _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall")
#define HOTFIXES_REG_PATH _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Component Based Servicing\\Packages")
#define HOTFIXES_INSTALL_CLIENT _T("WindowsUpdateAgent")
#define DELIM_KB_REF " identifier=\""
#define DELIM_KB_URL " supportInformation=\""
#define DELIM_DESC " description=\""
#define DELIM_COMPANY " company=\""
#define DELIM_TYPE " releaseType=\""
#define DELIM_TIME " creationTimeStamp=\""
#define MAX_KEY_LEN 255
void EnumWin51Hotfixes(PNODE hotfixesNode);
void EnumWin6Hotfixes(PNODE hotfixesNode);
BOOL GetAttributeValue(PCHAR attribute, PCHAR szBuffer, DWORD dwBufferSize);
PNODE EnumPackages()
{
PNODE packagesNode = NULL;
PNODE packageNode = NULL;
HKEY hKey = NULL;
HKEY hSubkey = NULL;
DWORD dwRetVal = 0;
DWORD dwIndex = 0;
DWORD dwBufferSize = MAX_KEY_LEN;
TCHAR szBuffer[MAX_KEY_LEN];
LPTSTR pszBuffer = NULL;
// Get handle to packages registry key
// We could use MSI API but it won't return packages built
// with Windows Installer versions prior to v3.0.
if (ERROR_SUCCESS != (dwRetVal = RegOpenKeyEx(HKEY_LOCAL_MACHINE, PACKAGES_REG_PATH, 0, KEY_READ, &hKey))) {
SetError(ERR_CRIT, dwRetVal, _T("Failed to enumerate installed packages"));
return packagesNode;
}
packagesNode = node_alloc(_T("Packages"), NFLG_TABLE);
// Get all subkeys (one for each package)
dwIndex = 0;
while (ERROR_SUCCESS == (dwRetVal = RegEnumKeyEx(hKey, dwIndex++, szBuffer, &dwBufferSize, 0, NULL, NULL, NULL))) {
// Prevent KBXXXXXXXX packages from being read on Win2003/XP systems
// These are enumerated as Hotfixes
if (0 == wcsncmp(szBuffer, _T("KB"), 2))
goto cleanup_package;
if (ERROR_SUCCESS == (dwRetVal = RegOpenKeyEx(hKey, szBuffer, 0, KEY_READ, &hSubkey))) {
// Get value of 'DisplayName' value
if (NULL != (pszBuffer = GetRegString(hSubkey, _T("DisplayName")))) {
packageNode = node_append_new(packagesNode, _T("Package"), NFLG_TABLE_ROW);
node_att_set(packageNode, _T("Name"), pszBuffer, 0);
FREE(pszBuffer);
// Add GUID for Windows Installer v3+ Packages (GUID key name)
if (38 == wcslen(szBuffer) && '{' == szBuffer[0] && '}' == szBuffer[37])
node_att_set(packageNode, _T("UpgradeCode"), szBuffer, NAFLG_FMT_GUID);
// Publisher
if (NULL != (pszBuffer = GetRegString(hSubkey, _T("Publisher")))) {
node_att_set(packageNode, _T("Publisher"), pszBuffer, 0);
FREE(pszBuffer);
}
// Version
if (NULL != (pszBuffer = GetRegString(hSubkey, _T("DisplayVersion")))) {
node_att_set(packageNode, _T("Version"), pszBuffer, 0);
FREE(pszBuffer);
}
// Install source
if (NULL != (pszBuffer = GetRegString(hSubkey, _T("InstallSource")))) {
node_att_set(packageNode, _T("InstallSource"), pszBuffer, 0);
FREE(pszBuffer);
}
// Install path
if (NULL != (pszBuffer = GetRegString(hSubkey, _T("InstallLocation")))) {
node_att_set(packageNode, _T("InstallLocation"), pszBuffer, 0);
FREE(pszBuffer);
}
// Install date
if (NULL != (pszBuffer = GetRegString(hSubkey, _T("InstallDate")))) {
node_att_set(packageNode, _T("InstallDate"), pszBuffer, 0);
FREE(pszBuffer);
}
// Help URL
if (NULL != (pszBuffer = GetRegString(hSubkey, _T("HelpLink")))) {
node_att_set(packageNode, _T("HelpUrl"), pszBuffer, NAFLG_FMT_URI);
FREE(pszBuffer);
}
// Help telephone
if (NULL != (pszBuffer = GetRegString(hSubkey, _T("HelpTelephone")))) {
node_att_set(packageNode, _T("HelpTelephone"), pszBuffer, 0);
FREE(pszBuffer);
}
// About URL
if (NULL != (pszBuffer = GetRegString(hSubkey, _T("URLInfoAbout")))) {
node_att_set(packageNode, _T("AboutUrl"), pszBuffer, NAFLG_FMT_URI);
FREE(pszBuffer);
}
// Upgrade URL
if (NULL != (pszBuffer = GetRegString(hSubkey, _T("URLUpdateInfo")))) {
node_att_set(packageNode, _T("UpdateUrl"), pszBuffer, NAFLG_FMT_URI);
FREE(pszBuffer);
}
}
RegCloseKey(hSubkey);
}
cleanup_package:
dwBufferSize = MAX_KEY_LEN;
}
RegCloseKey(hKey);
return packagesNode;
}
/*
* CBS Store details:
* http://technet.microsoft.com/en-us/library/ee619779(v=ws.10).aspx
*
* Files: C:\Windows\servicing\packages\Package_for_KB*.mum
*/
PNODE EnumHotfixes()
{
OSVERSIONINFOEX osinfo;
PNODE hotfixesNode = NULL;
// Get Windows version
osinfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
GetVersionEx((LPOSVERSIONINFOW)&osinfo);
hotfixesNode = node_alloc(_T("Hotfixes"), NFLG_TABLE);
EnumWin51Hotfixes(hotfixesNode);
if (6 <= osinfo.dwMajorVersion)
EnumWin6Hotfixes(hotfixesNode);
return hotfixesNode;
}
void EnumWin51Hotfixes(PNODE hotfixesNode)
{
PNODE hotfixNode = NULL;
HKEY hKey = NULL;
HKEY hSubkey = NULL;
DWORD dwRetVal = 0;
DWORD dwIndex = 0;
DWORD dwBufferSize = MAX_KEY_LEN;
TCHAR szBuffer[MAX_KEY_LEN];
LPTSTR pszBuffer = NULL;
// TODO: Detect QXXXXXX and 'File 1' (From service pack) updates
// Get handle to packages registry key
// We could use MSI API but it won't return packages built
// with Windows Installer versions prior to v3.0.
if (ERROR_SUCCESS != (dwRetVal = RegOpenKeyEx(HKEY_LOCAL_MACHINE, PACKAGES_REG_PATH, 0, KEY_READ, &hKey))) {
SetError(ERR_CRIT, dwRetVal, _T("Failed to enumerate installed packages"));
return;
}
// Get all subkeys (one for each package)
dwIndex = 0;
while (ERROR_SUCCESS == (dwRetVal = RegEnumKeyEx(hKey, dwIndex++, szBuffer, &dwBufferSize, 0, NULL, NULL, NULL))) {
// Only read KBXXXXXXXX registry keys
if (0 == wcsncmp(szBuffer, _T("KB"), 2) && ERROR_SUCCESS == (dwRetVal = RegOpenKeyEx(hKey, szBuffer, 0, KEY_READ, &hSubkey))) {
// Get value of 'DisplayName' value
if (NULL != (pszBuffer = GetRegString(hSubkey, _T("DisplayName")))) {
// Create hotfix node!
hotfixNode = node_alloc(_T("Hotfix"), NFLG_TABLE_ROW);
// KB Reference
node_att_set(hotfixNode, _T("Id"), szBuffer, NAFLG_KEY);
// Display name
hotfixNode = node_append_new(hotfixesNode, _T("Hotfix"), NFLG_TABLE_ROW);
node_att_set(hotfixNode, _T("Name"), pszBuffer, NAFLG_KEY);
FREE(pszBuffer);
// Release type
if (NULL != (pszBuffer = GetRegString(hSubkey, _T("ReleaseType")))) {
node_att_set(hotfixNode, _T("Category"), pszBuffer, 0);
FREE(pszBuffer);
}
// URL
if (NULL != (pszBuffer = GetRegString(hSubkey, _T("HelpLink")))) {
node_att_set(hotfixNode, _T("HelpUrl"), pszBuffer, NAFLG_FMT_URI);
FREE(pszBuffer);
}
// Install date
// TODO: Parse Install Date for a useful format
if (NULL != (pszBuffer = GetRegString(hSubkey, _T("InstallDate")))) {
node_att_set(hotfixNode, _T("InstallDate"), pszBuffer, NAFLG_FMT_DATETIME);
FREE(pszBuffer);
}
}
RegCloseKey(hSubkey);
}
cleanup_package:
dwBufferSize = MAX_KEY_LEN;
}
RegCloseKey(hKey);
}
void EnumWin6Hotfixes(PNODE hotfixesNode)
{
PNODE hotfixNode = NULL;
HKEY hKey = NULL;
HKEY hSubkey = NULL;
DWORD dwRetVal = 0;
DWORD dwIndex = 0;
DWORD dwBufferSize = MAX_KEY_LEN;
TCHAR szBuffer[MAX_KEY_LEN];
TCHAR szPathBuffer[MAX_PATH + 1];
TCHAR szPathBuffer2[MAX_PATH + 1];
CHAR szFileBuffer[4096];
CHAR szValueBuffer[4096];
PCHAR c1 = NULL;
LPTSTR pszBuffer = NULL;
HANDLE hFile = INVALID_HANDLE_VALUE;
DWORD installTimeHigh, installTimeLow;
FILETIME ftInstallTime;
SYSTEMTIME stInstallTime;
// Get handle to hotfixes registry key
// We could use MSI API but it won't return hotfixes built
// with Windows Installer versions prior to v3.0.
if (ERROR_SUCCESS != (dwRetVal = RegOpenKeyEx(HKEY_LOCAL_MACHINE, HOTFIXES_REG_PATH, 0, KEY_READ, &hKey))) {
SetError(ERR_CRIT, dwRetVal, _T("Failed to enumerate installed hotfixes"));
return;
}
// Get all subkeys (one for each hotfix)
dwIndex = 0;
while (ERROR_SUCCESS == (dwRetVal = RegEnumKeyEx(hKey, dwIndex++, szBuffer, &dwBufferSize, 0, NULL, NULL, NULL))) {
// Get handle to subkey
if (ERROR_SUCCESS == (dwRetVal = RegOpenKeyEx(hKey, szBuffer, 0, KEY_READ, &hSubkey))) {
// Make sure key starts with 'Package_for_KB'
if (0 != wcsncmp(szBuffer, _T("Package_for_KB"), 14))
goto cleanup_key;
// Get package installer
if (NULL == (pszBuffer = GetRegString(hSubkey, _T("InstallClient"))))
goto cleanup_key;
// Ensure InstallClient == "WindowsUpdateAgent"
if (0 != wcsicmp(pszBuffer, HOTFIXES_INSTALL_CLIENT)) {
FREE(pszBuffer);
goto cleanup_key;
}
// Ensure InstallName == "update.mum"
if (NULL != (pszBuffer = GetRegString(hSubkey, _T("InstallName")))) {
if (0 != wcsicmp(pszBuffer, _T("update.mum"))) {
FREE(pszBuffer);
goto cleanup_key;
}
}
// Free buffer allocated for InstallClient
FREE(pszBuffer);
// Check for corresponding .mum file at C:\Windows\servicing\Packages\Package_for_KB*.mum
SWPRINTF(szPathBuffer2, _T("%%SystemRoot%%\\servicing\\Packages\\%s.mum"), szBuffer);
ExpandEnvironmentStrings(szPathBuffer2, szPathBuffer, MAX_PATH + 1);
// Get a file handle to the package .mum file
if (INVALID_HANDLE_VALUE == (hFile = CreateFile(szPathBuffer, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)))
goto cleanup_key;
// Create a node
hotfixNode = node_append_new(hotfixesNode, _T("Hotfix"), NFLG_TABLE_ROW);
// Install date
if (-1 != (installTimeHigh = GetRegDword(hSubkey, _T("InstallTimeHigh"))) && -1 != (installTimeLow = GetRegDword(hSubkey, _T("InstallTimeLow")))) {
if (FormatDateTime(installTimeHigh, installTimeLow, szBuffer, ARRAYSIZE(szBuffer))) {
node_att_set(hotfixNode, _T("InstallDate"), szBuffer, NAFLG_FMT_DATETIME);
}
}
// DEBUG: CurrentState should be 0x70???
if (-1 != (dwRetVal = GetRegDword(hSubkey, _T("CurrentState")))) {
SWPRINTF(szBuffer, L"0x%X (%u)", dwRetVal, dwRetVal);
node_att_set(hotfixNode, L"State", szBuffer, 0);
}
// Parse the first chunk of the file for hostfix info
if (ReadFile(hFile, szFileBuffer, ARRAYSIZE(szFileBuffer), &dwRetVal, NULL) && dwRetVal) {
// KB Reference
if (NULL != (c1 = strstr(szFileBuffer, DELIM_KB_REF))) {
if (GetAttributeValue(c1, szValueBuffer, ARRAYSIZE(szValueBuffer))) {
UTF8_TO_UNICODE(szValueBuffer, szBuffer, ARRAYSIZE(szBuffer));
node_att_set(hotfixNode, _T("Id"), szBuffer, NAFLG_KEY);
}
}
// Type
if (NULL != (c1 = strstr(szFileBuffer, DELIM_TYPE))) {
if (GetAttributeValue(c1, szValueBuffer, ARRAYSIZE(szValueBuffer))) {
UTF8_TO_UNICODE(szValueBuffer, szBuffer, ARRAYSIZE(szBuffer));
node_att_set(hotfixNode, _T("Category"), szBuffer, 0);
}
}
// Description
if (NULL != (c1 = strstr(szFileBuffer, DELIM_DESC))) {
if (GetAttributeValue(c1, szValueBuffer, ARRAYSIZE(szValueBuffer))) {
UTF8_TO_UNICODE(szValueBuffer, szBuffer, ARRAYSIZE(szBuffer));
node_att_set(hotfixNode, _T("Name"), szBuffer, 0);
}
}
// Company
if (NULL != (c1 = strstr(szFileBuffer, DELIM_COMPANY))) {
if (GetAttributeValue(c1, szValueBuffer, ARRAYSIZE(szValueBuffer))) {
UTF8_TO_UNICODE(szValueBuffer, szBuffer, ARRAYSIZE(szBuffer));
node_att_set(hotfixNode, _T("Publisher"), szBuffer, 0);
}
}
// Get KB URL
if (NULL != (c1 = strstr(szFileBuffer, DELIM_KB_URL))) {
if (GetAttributeValue(c1, szValueBuffer, ARRAYSIZE(szValueBuffer))) {
UTF8_TO_UNICODE(szValueBuffer, szBuffer, ARRAYSIZE(szBuffer));
node_att_set(hotfixNode, _T("HelpUrl"), szBuffer, NAFLG_FMT_URI);
}
}
}
CloseHandle(hFile);
}
cleanup_key:
RegCloseKey(hSubkey);
dwBufferSize = MAX_KEY_LEN;
}
RegCloseKey(hKey);
}
/*
* This function is not bullet proof but should be resilient in the real world
* as the .mum files are machine generated.
*/
BOOL GetAttributeValue(PCHAR attribute, PCHAR szBuffer, DWORD dwBufferSize)
{
PCHAR in = attribute;
DWORD i = 0;
// Seek to the beginning of the attribute value (eg. key="value")
// ^
while ((*in) != '=') {
if ((*in) == '\0')
return 0;
in++;
}
// Seek to the beginning of the value (eg. key = "value")
// ^
while ((*in) != '"') {
if ((*in) == '\0')
return 0;
in++;
}
// Start copying to the output buffer
in++;
for (i = 0; i < dwBufferSize; i++, in++) {
if ((*in) == '\0')
return 0;
// Have we reached the end of a value quote?
if ((*in) == '"') {
szBuffer[i] = '\0';
return i;
}
szBuffer[i] = (*in);
}
return 0;
}