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
39 #include "ignore-value.h"
43 #include "guestfs-internal.h"
44 #include "guestfs-internal-actions.h"
45 #include "guestfs_protocol.h"
47 #if defined(HAVE_HIVEX)
50 static struct guestfs_application_list *list_applications_rpm (guestfs_h *g, struct inspect_fs *fs);
52 static struct guestfs_application_list *list_applications_deb (guestfs_h *g, struct inspect_fs *fs);
53 static struct guestfs_application_list *list_applications_windows (guestfs_h *g, struct inspect_fs *fs);
54 static void add_application (guestfs_h *g, struct guestfs_application_list *, const char *name, const char *display_name, int32_t epoch, const char *version, const char *release, const char *install_path, const char *publisher, const char *url, const char *description);
55 static void sort_applications (struct guestfs_application_list *);
57 /* Unlike the simple inspect-get-* calls, this one assumes that the
58 * disks are mounted up, and reads files from the mounted disks.
60 struct guestfs_application_list *
61 guestfs__inspect_list_applications (guestfs_h *g, const char *root)
63 struct inspect_fs *fs = guestfs___search_for_root (g, root);
67 struct guestfs_application_list *ret = NULL;
69 /* Presently we can only list applications for installed disks. It
70 * is possible in future to get lists of packages from installers.
72 if (fs->format == OS_FORMAT_INSTALLED) {
76 switch (fs->package_format) {
77 case OS_PACKAGE_FORMAT_RPM:
79 ret = list_applications_rpm (g, fs);
85 case OS_PACKAGE_FORMAT_DEB:
86 ret = list_applications_deb (g, fs);
91 case OS_PACKAGE_FORMAT_PACMAN:
92 case OS_PACKAGE_FORMAT_EBUILD:
93 case OS_PACKAGE_FORMAT_PISI:
94 case OS_PACKAGE_FORMAT_PKGSRC:
95 case OS_PACKAGE_FORMAT_UNKNOWN:
97 /* nothing - keep GCC happy */;
101 case OS_TYPE_WINDOWS:
102 ret = list_applications_windows (g, fs);
107 case OS_TYPE_FREEBSD:
109 case OS_TYPE_UNKNOWN:
111 /* nothing - keep GCC happy */;
116 /* Don't know how to do inspection. Not an error, return an
119 ret = safe_malloc (g, sizeof *ret);
124 sort_applications (ret);
131 /* This data comes from the Name database, and contains the application
132 * names and the first 4 bytes of the link field.
134 struct rpm_names_list {
135 struct rpm_name *names;
144 free_rpm_names_list (struct rpm_names_list *list)
148 for (i = 0; i < list->len; ++i)
149 free (list->names[i].name);
154 compare_links (const void *av, const void *bv)
156 const struct rpm_name *a = av;
157 const struct rpm_name *b = bv;
158 return memcmp (a->link, b->link, 4);
162 read_rpm_name (guestfs_h *g,
163 const unsigned char *key, size_t keylen,
164 const unsigned char *value, size_t valuelen,
167 struct rpm_names_list *list = listv;
170 /* Ignore bogus entries. */
171 if (keylen == 0 || valuelen < 4)
174 /* The name (key) field won't be NUL-terminated, so we must do that. */
175 name = safe_malloc (g, keylen+1);
176 memcpy (name, key, keylen);
179 list->names = safe_realloc (g, list->names,
180 (list->len + 1) * sizeof (struct rpm_name));
181 list->names[list->len].name = name;
182 memcpy (list->names[list->len].link, value, 4);
188 struct read_package_data {
189 struct rpm_names_list *list;
190 struct guestfs_application_list *apps;
194 read_package (guestfs_h *g,
195 const unsigned char *key, size_t keylen,
196 const unsigned char *value, size_t valuelen,
199 struct read_package_data *data = datav;
200 struct rpm_name nkey, *entry;
204 char *nul_name_nul, *version, *release;
206 /* This function reads one (key, value) pair from the Packages
207 * database. The key is the link field (see struct rpm_name). The
208 * value is a long binary string, but we can extract the version
209 * number from it as below. First we have to look up the link field
210 * in the list of links (which is sorted by link field).
213 /* Ignore bogus entries. */
214 if (keylen < 4 || valuelen == 0)
217 /* Look up the link (key) in the list. */
218 memcpy (nkey.link, key, 4);
219 entry = bsearch (&nkey, data->list->names, data->list->len,
220 sizeof (struct rpm_name), compare_links);
222 return 0; /* Not found - ignore it. */
224 /* We found a matching link entry, so that gives us the application
225 * name (entry->name). Now we can get other data for this
226 * application out of the binary value string. XXX This is a real
230 /* Look for \0<name>\0 */
231 len = strlen (entry->name);
232 nul_name_nul = safe_malloc (g, len + 2);
233 nul_name_nul[0] = '\0';
234 memcpy (&nul_name_nul[1], entry->name, len);
235 nul_name_nul[len+1] = '\0';
236 p = memmem (value, valuelen, nul_name_nul, len+2);
241 /* Following that are \0-delimited version and release fields. */
242 p += len + 2; /* Note we have to skip \0 + name + \0. */
243 max = valuelen - (p - (char *) value);
246 version = safe_strndup (g, p, max);
248 len = strlen (version);
250 max = valuelen - (p - (char *) value);
253 release = safe_strndup (g, p, max);
255 /* Add the application and what we know. */
256 add_application (g, data->apps, entry->name, "", 0, version, release,
265 static struct guestfs_application_list *
266 list_applications_rpm (guestfs_h *g, struct inspect_fs *fs)
268 char *Name = NULL, *Packages = NULL;
269 struct rpm_names_list list = { .names = NULL, .len = 0 };
270 struct guestfs_application_list *apps = NULL;
272 Name = guestfs___download_to_tmp (g, fs,
273 "/var/lib/rpm/Name", "rpm_Name",
278 Packages = guestfs___download_to_tmp (g, fs,
279 "/var/lib/rpm/Packages", "rpm_Packages",
281 if (Packages == NULL)
284 /* Read Name database. */
285 if (guestfs___read_db_dump (g, Name, &list, read_rpm_name) == -1)
288 /* Sort the names by link field for fast searching. */
289 qsort (list.names, list.len, sizeof (struct rpm_name), compare_links);
291 /* Allocate 'apps' list. */
292 apps = safe_malloc (g, sizeof *apps);
296 /* Read Packages database. */
297 struct read_package_data data = { .list = &list, .apps = apps };
298 if (guestfs___read_db_dump (g, Packages, &data, read_package) == -1)
303 free_rpm_names_list (&list);
310 free_rpm_names_list (&list);
312 guestfs_free_application_list (apps);
317 #endif /* defined DB_DUMP */
319 static struct guestfs_application_list *
320 list_applications_deb (guestfs_h *g, struct inspect_fs *fs)
323 status = guestfs___download_to_tmp (g, fs, "/var/lib/dpkg/status", "status",
328 struct guestfs_application_list *apps = NULL, *ret = NULL;
332 char *name = NULL, *version = NULL, *release = NULL;
333 int installed_flag = 0;
335 fp = fopen (status, "r");
337 perrorf (g, "fopen: %s", status);
341 /* Allocate 'apps' list. */
342 apps = safe_malloc (g, sizeof *apps);
346 /* Read the temporary file. Each package entry is separated by
348 * XXX Strictly speaking this is in mailbox header format, so it
349 * would be possible for fields to spread across multiple lines,
350 * although for the short fields that we are concerned about this is
351 * unlikely and not seen in practice.
353 while (fgets (line, sizeof line, fp) != NULL) {
355 if (len > 0 && line[len-1] == '\n') {
360 if (STRPREFIX (line, "Package: ")) {
362 name = safe_strdup (g, &line[9]);
364 else if (STRPREFIX (line, "Status: ")) {
365 installed_flag = strstr (&line[8], "installed") != NULL;
367 else if (STRPREFIX (line, "Version: ")) {
370 char *p = strchr (&line[9], '-');
373 version = safe_strdup (g, &line[9]);
374 release = safe_strdup (g, p+1);
376 version = safe_strdup (g, &line[9]);
380 else if (STREQ (line, "")) {
381 if (installed_flag && name && version)
382 add_application (g, apps, name, "", 0, version, release ? : "",
387 name = version = release = NULL;
392 if (fclose (fp) == -1) {
393 perrorf (g, "fclose: %s", status);
401 if (ret == NULL && apps != NULL)
402 guestfs_free_application_list (apps);
412 static void list_applications_windows_from_path (guestfs_h *g, hive_h *h, struct guestfs_application_list *apps, const char **path, size_t path_len);
414 static struct guestfs_application_list *
415 list_applications_windows (guestfs_h *g, struct inspect_fs *fs)
417 size_t len = strlen (fs->windows_systemroot) + 64;
419 snprintf (software, len, "%s/system32/config/software",
420 fs->windows_systemroot);
422 char *software_path = guestfs___case_sensitive_path_silently (g, software);
423 if (!software_path) {
424 /* Missing software hive is a problem. */
425 error (g, "no HKLM\\SOFTWARE hive found in the guest");
429 char *software_hive = NULL;
430 struct guestfs_application_list *ret = NULL;
433 software_hive = guestfs___download_to_tmp (g, fs, software_path, "software",
435 if (software_hive == NULL)
438 free (software_path);
439 software_path = NULL;
441 h = hivex_open (software_hive, g->verbose ? HIVEX_OPEN_VERBOSE : 0);
443 perrorf (g, "hivex_open");
447 /* Allocate apps list. */
448 ret = safe_malloc (g, sizeof *ret);
452 /* Ordinary native applications. */
453 const char *hivepath[] =
454 { "Microsoft", "Windows", "CurrentVersion", "Uninstall" };
455 list_applications_windows_from_path (g, h, ret, hivepath,
456 sizeof hivepath / sizeof hivepath[0]);
458 /* 32-bit emulated Windows apps running on the WOW64 emulator.
459 * http://support.microsoft.com/kb/896459 (RHBZ#692545).
461 const char *hivepath2[] =
462 { "WOW6432node", "Microsoft", "Windows", "CurrentVersion", "Uninstall" };
463 list_applications_windows_from_path (g, h, ret, hivepath2,
464 sizeof hivepath2 / sizeof hivepath2[0]);
467 if (h) hivex_close (h);
468 free (software_path);
469 free (software_hive);
475 list_applications_windows_from_path (guestfs_h *g, hive_h *h,
476 struct guestfs_application_list *apps,
477 const char **path, size_t path_len)
479 hive_node_h *children = NULL;
483 node = hivex_root (h);
485 for (i = 0; node != 0 && i < path_len; ++i)
486 node = hivex_node_get_child (h, node, path[i]);
491 children = hivex_node_children (h, node);
492 if (children == NULL)
495 /* Consider any child node that has a DisplayName key.
497 * http://nsis.sourceforge.net/Add_uninstall_information_to_Add/Remove_Programs#Optional_values
499 for (i = 0; children[i] != 0; ++i) {
502 char *display_name = NULL;
503 char *version = NULL;
504 char *install_path = NULL;
505 char *publisher = NULL;
507 char *comments = NULL;
509 /* Use the node name as a proxy for the package name in Linux. The
510 * display name is not language-independent, so it cannot be used.
512 name = hivex_node_name (h, children[i]);
516 value = hivex_node_get_value (h, children[i], "DisplayName");
518 display_name = hivex_value_string (h, value);
520 value = hivex_node_get_value (h, children[i], "DisplayVersion");
522 version = hivex_value_string (h, value);
523 value = hivex_node_get_value (h, children[i], "InstallLocation");
525 install_path = hivex_value_string (h, value);
526 value = hivex_node_get_value (h, children[i], "Publisher");
528 publisher = hivex_value_string (h, value);
529 value = hivex_node_get_value (h, children[i], "URLInfoAbout");
531 url = hivex_value_string (h, value);
532 value = hivex_node_get_value (h, children[i], "Comments");
534 comments = hivex_value_string (h, value);
536 add_application (g, apps, name, display_name, 0,
559 add_application (guestfs_h *g, struct guestfs_application_list *apps,
560 const char *name, const char *display_name, int32_t epoch,
561 const char *version, const char *release,
562 const char *install_path,
563 const char *publisher, const char *url,
564 const char *description)
567 apps->val = safe_realloc (g, apps->val,
568 apps->len * sizeof (struct guestfs_application));
569 apps->val[apps->len-1].app_name = safe_strdup (g, name);
570 apps->val[apps->len-1].app_display_name = safe_strdup (g, display_name);
571 apps->val[apps->len-1].app_epoch = epoch;
572 apps->val[apps->len-1].app_version = safe_strdup (g, version);
573 apps->val[apps->len-1].app_release = safe_strdup (g, release);
574 apps->val[apps->len-1].app_install_path = safe_strdup (g, install_path);
575 /* XXX Translated path is not implemented yet. */
576 apps->val[apps->len-1].app_trans_path = safe_strdup (g, "");
577 apps->val[apps->len-1].app_publisher = safe_strdup (g, publisher);
578 apps->val[apps->len-1].app_url = safe_strdup (g, url);
579 /* XXX The next two are not yet implemented for any package
580 * format, but we could easily support them for rpm and deb.
582 apps->val[apps->len-1].app_source_package = safe_strdup (g, "");
583 apps->val[apps->len-1].app_summary = safe_strdup (g, "");
584 apps->val[apps->len-1].app_description = safe_strdup (g, description);
587 /* Sort applications by name before returning the list. */
589 compare_applications (const void *vp1, const void *vp2)
591 const struct guestfs_application *v1 = vp1;
592 const struct guestfs_application *v2 = vp2;
594 return strcmp (v1->app_name, v2->app_name);
598 sort_applications (struct guestfs_application_list *apps)
600 if (apps && apps->val)
601 qsort (apps->val, apps->len, sizeof (struct guestfs_application),
602 compare_applications);
605 #else /* no hivex at compile time */
607 /* XXX These functions should be in an optgroup. */
609 #define NOT_IMPL(r) \
610 error (g, _("inspection API not available since this version of libguestfs was compiled without the hivex library")); \
613 struct guestfs_application_list *
614 guestfs__inspect_list_applications (guestfs_h *g, const char *root)
619 #endif /* no hivex at compile time */