2 * Copyright (C) 2010-2011 Red Hat Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
41 #include "ignore-value.h"
45 #include "guestfs-internal.h"
46 #include "guestfs-internal-actions.h"
47 #include "guestfs_protocol.h"
49 #if defined(HAVE_PCRE) && defined(HAVE_HIVEX)
51 /* Compile all the regular expressions once when the shared library is
52 * loaded. PCRE is thread safe so we're supposedly OK here if
53 * multiple threads call into the libguestfs API functions below
56 static pcre *re_windows_version;
58 static void compile_regexps (void) __attribute__((constructor));
59 static void free_regexps (void) __attribute__((destructor));
62 compile_regexps (void)
67 #define COMPILE(re,pattern,options) \
69 re = pcre_compile ((pattern), (options), &err, &offset, NULL); \
71 ignore_value (write (2, err, strlen (err))); \
76 COMPILE (re_windows_version, "^(\\d+)\\.(\\d+)", 0);
82 pcre_free (re_windows_version);
85 static int check_windows_arch (guestfs_h *g, struct inspect_fs *fs);
86 static int check_windows_software_registry (guestfs_h *g, struct inspect_fs *fs);
87 static int check_windows_system_registry (guestfs_h *g, struct inspect_fs *fs);
88 static char *map_registry_disk_blob (guestfs_h *g, const char *blob);
90 /* XXX Handling of boot.ini in the Perl version was pretty broken. It
91 * essentially didn't do anything for modern Windows guests.
92 * Therefore I've omitted all that code.
95 guestfs___check_windows_root (guestfs_h *g, struct inspect_fs *fs)
97 fs->type = OS_TYPE_WINDOWS;
98 fs->distro = OS_DISTRO_WINDOWS;
100 /* Try to find Windows systemroot using some common locations. */
101 const char *systemroots[] =
102 { "/windows", "/winnt", "/win32", "/win" };
104 char *systemroot = NULL;
106 systemroot == NULL && i < sizeof systemroots / sizeof systemroots[0];
108 systemroot = guestfs___case_sensitive_path_silently (g, systemroots[i]);
112 error (g, _("cannot resolve Windows %%SYSTEMROOT%%"));
116 debug (g, "windows %%SYSTEMROOT%% = %s", systemroot);
118 /* Freed by guestfs___free_inspect_info. */
119 fs->windows_systemroot = systemroot;
121 if (check_windows_arch (g, fs) == -1)
124 /* Product name and version. */
125 if (check_windows_software_registry (g, fs) == -1)
129 if (check_windows_system_registry (g, fs) == -1)
136 check_windows_arch (guestfs_h *g, struct inspect_fs *fs)
138 size_t len = strlen (fs->windows_systemroot) + 32;
140 snprintf (cmd_exe, len, "%s/system32/cmd.exe", fs->windows_systemroot);
142 char *cmd_exe_path = guestfs___case_sensitive_path_silently (g, cmd_exe);
146 char *arch = guestfs_file_architecture (g, cmd_exe_path);
150 fs->arch = arch; /* freed by guestfs___free_inspect_info */
155 /* At the moment, pull just the ProductName and version numbers from
156 * the registry. In future there is a case for making many more
157 * registry fields available to callers.
160 check_windows_software_registry (guestfs_h *g, struct inspect_fs *fs)
162 size_t len = strlen (fs->windows_systemroot) + 64;
164 snprintf (software, len, "%s/system32/config/software",
165 fs->windows_systemroot);
167 char *software_path = guestfs___case_sensitive_path_silently (g, software);
169 /* If the software hive doesn't exist, just accept that we cannot
170 * find product_name etc.
174 char *software_hive = NULL;
177 hive_value_h *values = NULL;
179 software_hive = guestfs___download_to_tmp (g, fs, software_path, "software",
181 if (software_hive == NULL)
184 h = hivex_open (software_hive, g->verbose ? HIVEX_OPEN_VERBOSE : 0);
186 perrorf (g, "hivex_open");
190 hive_node_h node = hivex_root (h);
191 const char *hivepath[] =
192 { "Microsoft", "Windows NT", "CurrentVersion" };
195 node != 0 && i < sizeof hivepath / sizeof hivepath[0];
197 node = hivex_node_get_child (h, node, hivepath[i]);
201 perrorf (g, "hivex: cannot locate HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion");
205 values = hivex_node_values (h, node);
207 for (i = 0; values[i] != 0; ++i) {
208 char *key = hivex_value_key (h, values[i]);
210 perrorf (g, "hivex_value_key");
214 if (STRCASEEQ (key, "ProductName")) {
215 fs->product_name = hivex_value_string (h, values[i]);
216 if (!fs->product_name) {
217 perrorf (g, "hivex_value_string");
222 else if (STRCASEEQ (key, "CurrentVersion")) {
223 char *version = hivex_value_string (h, values[i]);
225 perrorf (g, "hivex_value_string");
230 if (match2 (g, version, re_windows_version, &major, &minor)) {
231 fs->major_version = guestfs___parse_unsigned_int (g, major);
233 if (fs->major_version == -1) {
239 fs->minor_version = guestfs___parse_unsigned_int (g, minor);
241 if (fs->minor_version == -1) {
250 else if (STRCASEEQ (key, "InstallationType")) {
251 fs->product_variant = hivex_value_string (h, values[i]);
252 if (!fs->product_variant) {
253 perrorf (g, "hivex_value_string");
265 if (h) hivex_close (h);
267 free (software_path);
268 free (software_hive);
274 check_windows_system_registry (guestfs_h *g, struct inspect_fs *fs)
276 size_t len = strlen (fs->windows_systemroot) + 64;
278 snprintf (system, len, "%s/system32/config/system",
279 fs->windows_systemroot);
281 char *system_path = guestfs___case_sensitive_path_silently (g, system);
283 /* If the system hive doesn't exist, just accept that we cannot
288 char *system_hive = NULL;
291 hive_node_h root, node;
292 hive_value_h value, *values = NULL;
297 guestfs___download_to_tmp (g, fs, system_path, "system",
299 if (system_hive == NULL)
302 h = hivex_open (system_hive, g->verbose ? HIVEX_OPEN_VERBOSE : 0);
304 perrorf (g, "hivex_open");
308 root = hivex_root (h);
310 perrorf (g, "hivex_root");
314 /* Get the CurrentControlSet. */
316 node = hivex_node_get_child (h, root, "Select");
319 perrorf (g, "hivex_node_get_child");
321 error (g, "hivex: could not locate HKLM\\SYSTEM\\Select");
326 value = hivex_node_get_value (h, node, "Current");
329 perrorf (g, "hivex_node_get_value");
331 error (g, "hivex: HKLM\\System\\Select Default entry not found.");
335 /* XXX Should check the type. */
336 dword = hivex_value_dword (h, value);
337 fs->windows_current_control_set = safe_asprintf (g, "ControlSet%03d", dword);
339 /* Get the drive mappings.
340 * This page explains the contents of HKLM\System\MountedDevices:
341 * http://www.goodells.net/multiboot/partsigs.shtml
344 node = hivex_node_get_child (h, root, "MountedDevices");
347 perrorf (g, "hivex_node_get_child");
349 error (g, "hivex: could not locate HKLM\\SYSTEM\\MountedDevices");
353 values = hivex_node_values (h, node);
355 /* Count how many DOS drive letter mappings there are. This doesn't
356 * ignore removable devices, so it overestimates, but that doesn't
357 * matter because it just means we'll allocate a few bytes extra.
359 for (i = count = 0; values[i] != 0; ++i) {
360 char *key = hivex_value_key (h, values[i]);
362 perrorf (g, "hivex_value_key");
365 if (STRCASEEQLEN (key, "\\DosDevices\\", 12) &&
366 c_isalpha (key[12]) && key[13] == ':')
371 fs->drive_mappings = calloc (2*count + 1, sizeof (char *));
372 if (fs->drive_mappings == NULL) {
373 perrorf (g, "calloc");
377 for (i = count = 0; values[i] != 0; ++i) {
378 char *key = hivex_value_key (h, values[i]);
380 perrorf (g, "hivex_value_key");
383 if (STRCASEEQLEN (key, "\\DosDevices\\", 12) &&
384 c_isalpha (key[12]) && key[13] == ':') {
385 /* Get the binary value. Is it a fixed disk? */
390 blob = hivex_value_value (h, values[i], &type, &len);
391 if (blob != NULL && type == 3 && len == 12) {
392 /* Try to map the blob to a known disk and partition. */
393 device = map_registry_disk_blob (g, blob);
394 if (device != NULL) {
395 fs->drive_mappings[count++] = safe_strndup (g, &key[12], 1);
396 fs->drive_mappings[count++] = device;
404 /* Get the hostname. */
405 const char *hivepath[] =
406 { fs->windows_current_control_set, "Services", "Tcpip", "Parameters" };
407 for (node = root, i = 0;
408 node != 0 && i < sizeof hivepath / sizeof hivepath[0];
410 node = hivex_node_get_child (h, node, hivepath[i]);
414 perrorf (g, "hivex: cannot locate HKLM\\SYSTEM\\%s\\Services\\Tcpip\\Parameters",
415 fs->windows_current_control_set);
420 values = hivex_node_values (h, node);
422 for (i = 0; values[i] != 0; ++i) {
423 char *key = hivex_value_key (h, values[i]);
425 perrorf (g, "hivex_value_key");
429 if (STRCASEEQ (key, "Hostname")) {
430 fs->hostname = hivex_value_string (h, values[i]);
432 perrorf (g, "hivex_value_string");
437 /* many other interesting fields here ... */
445 if (h) hivex_close (h);
453 /* Windows Registry HKLM\SYSTEM\MountedDevices uses a blob of data
454 * to store partitions. This blob is described here:
455 * http://www.goodells.net/multiboot/partsigs.shtml
456 * The following function maps this blob to a libguestfs partition
460 map_registry_disk_blob (guestfs_h *g, const char *blob)
462 char **devices = NULL;
463 struct guestfs_partition_list *partitions = NULL;
467 uint64_t part_offset;
469 /* First 4 bytes are the disk ID. Search all devices to find the
470 * disk with this disk ID.
472 devices = guestfs_list_devices (g);
476 for (i = 0; devices[i] != NULL; ++i) {
477 /* Read the disk ID. */
478 diskid = guestfs_pread_device (g, devices[i], 4, 0x01b8, &len);
485 if (memcmp (diskid, blob, 4) == 0) { /* found it */
494 /* Next 8 bytes are the offset of the partition in bytes(!) given as
495 * a 64 bit little endian number. Luckily it's easy to get the
496 * partition byte offset from guestfs_part_list.
498 part_offset = le64toh (* (uint64_t *) &blob[4]);
500 partitions = guestfs_part_list (g, devices[i]);
501 if (partitions == NULL)
504 for (j = 0; j < partitions->len; ++j) {
505 if (partitions->val[j].part_start == part_offset) /* found it */
506 goto found_partition;
511 /* Construct the full device name. */
512 ret = safe_asprintf (g, "%s%d", devices[i], partitions->val[j].part_num);
516 guestfs___free_string_list (devices);
518 guestfs_free_partition_list (partitions);
523 guestfs___case_sensitive_path_silently (guestfs_h *g, const char *path)
525 guestfs_error_handler_cb old_error_cb = g->error_cb;
527 char *ret = guestfs_case_sensitive_path (g, path);
528 g->error_cb = old_error_cb;
532 #endif /* defined(HAVE_PCRE) && defined(HAVE_HIVEX) */