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) {
75 switch (fs->package_format) {
76 case OS_PACKAGE_FORMAT_RPM:
78 ret = list_applications_rpm (g, fs);
84 case OS_PACKAGE_FORMAT_DEB:
85 ret = list_applications_deb (g, fs);
90 case OS_PACKAGE_FORMAT_PACMAN:
91 case OS_PACKAGE_FORMAT_EBUILD:
92 case OS_PACKAGE_FORMAT_PISI:
93 case OS_PACKAGE_FORMAT_PKGSRC:
94 case OS_PACKAGE_FORMAT_UNKNOWN:
96 /* nothing - keep GCC happy */;
100 case OS_TYPE_WINDOWS:
101 ret = list_applications_windows (g, fs);
106 case OS_TYPE_FREEBSD:
107 case OS_TYPE_UNKNOWN:
109 /* nothing - keep GCC happy */;
114 /* Don't know how to do inspection. Not an error, return an
117 ret = safe_malloc (g, sizeof *ret);
122 sort_applications (ret);
129 /* This data comes from the Name database, and contains the application
130 * names and the first 4 bytes of the link field.
132 struct rpm_names_list {
133 struct rpm_name *names;
142 free_rpm_names_list (struct rpm_names_list *list)
146 for (i = 0; i < list->len; ++i)
147 free (list->names[i].name);
152 compare_links (const void *av, const void *bv)
154 const struct rpm_name *a = av;
155 const struct rpm_name *b = bv;
156 return memcmp (a->link, b->link, 4);
160 read_rpm_name (guestfs_h *g,
161 const unsigned char *key, size_t keylen,
162 const unsigned char *value, size_t valuelen,
165 struct rpm_names_list *list = listv;
168 /* Ignore bogus entries. */
169 if (keylen == 0 || valuelen < 4)
172 /* The name (key) field won't be NUL-terminated, so we must do that. */
173 name = safe_malloc (g, keylen+1);
174 memcpy (name, key, keylen);
177 list->names = safe_realloc (g, list->names,
178 (list->len + 1) * sizeof (struct rpm_name));
179 list->names[list->len].name = name;
180 memcpy (list->names[list->len].link, value, 4);
186 struct read_package_data {
187 struct rpm_names_list *list;
188 struct guestfs_application_list *apps;
192 read_package (guestfs_h *g,
193 const unsigned char *key, size_t keylen,
194 const unsigned char *value, size_t valuelen,
197 struct read_package_data *data = datav;
198 struct rpm_name nkey, *entry;
202 char *nul_name_nul, *version, *release;
204 /* This function reads one (key, value) pair from the Packages
205 * database. The key is the link field (see struct rpm_name). The
206 * value is a long binary string, but we can extract the version
207 * number from it as below. First we have to look up the link field
208 * in the list of links (which is sorted by link field).
211 /* Ignore bogus entries. */
212 if (keylen < 4 || valuelen == 0)
215 /* Look up the link (key) in the list. */
216 memcpy (nkey.link, key, 4);
217 entry = bsearch (&nkey, data->list->names, data->list->len,
218 sizeof (struct rpm_name), compare_links);
220 return 0; /* Not found - ignore it. */
222 /* We found a matching link entry, so that gives us the application
223 * name (entry->name). Now we can get other data for this
224 * application out of the binary value string. XXX This is a real
228 /* Look for \0<name>\0 */
229 len = strlen (entry->name);
230 nul_name_nul = safe_malloc (g, len + 2);
231 nul_name_nul[0] = '\0';
232 memcpy (&nul_name_nul[1], entry->name, len);
233 nul_name_nul[len+1] = '\0';
234 p = memmem (value, valuelen, nul_name_nul, len+2);
239 /* Following that are \0-delimited version and release fields. */
240 p += len + 2; /* Note we have to skip \0 + name + \0. */
241 max = valuelen - (p - (char *) value);
244 version = safe_strndup (g, p, max);
246 len = strlen (version);
248 max = valuelen - (p - (char *) value);
251 release = safe_strndup (g, p, max);
253 /* Add the application and what we know. */
254 add_application (g, data->apps, entry->name, "", 0, version, release,
263 static struct guestfs_application_list *
264 list_applications_rpm (guestfs_h *g, struct inspect_fs *fs)
266 char *Name = NULL, *Packages = NULL;
267 struct rpm_names_list list = { .names = NULL, .len = 0 };
268 struct guestfs_application_list *apps = NULL;
270 Name = guestfs___download_to_tmp (g, fs,
271 "/var/lib/rpm/Name", "rpm_Name",
276 Packages = guestfs___download_to_tmp (g, fs,
277 "/var/lib/rpm/Packages", "rpm_Packages",
279 if (Packages == NULL)
282 /* Read Name database. */
283 if (guestfs___read_db_dump (g, Name, &list, read_rpm_name) == -1)
286 /* Sort the names by link field for fast searching. */
287 qsort (list.names, list.len, sizeof (struct rpm_name), compare_links);
289 /* Allocate 'apps' list. */
290 apps = safe_malloc (g, sizeof *apps);
294 /* Read Packages database. */
295 struct read_package_data data = { .list = &list, .apps = apps };
296 if (guestfs___read_db_dump (g, Packages, &data, read_package) == -1)
301 free_rpm_names_list (&list);
308 free_rpm_names_list (&list);
310 guestfs_free_application_list (apps);
315 #endif /* defined DB_DUMP */
317 static struct guestfs_application_list *
318 list_applications_deb (guestfs_h *g, struct inspect_fs *fs)
321 status = guestfs___download_to_tmp (g, fs, "/var/lib/dpkg/status", "status",
326 struct guestfs_application_list *apps = NULL, *ret = NULL;
330 char *name = NULL, *version = NULL, *release = NULL;
331 int installed_flag = 0;
333 fp = fopen (status, "r");
335 perrorf (g, "fopen: %s", status);
339 /* Allocate 'apps' list. */
340 apps = safe_malloc (g, sizeof *apps);
344 /* Read the temporary file. Each package entry is separated by
346 * XXX Strictly speaking this is in mailbox header format, so it
347 * would be possible for fields to spread across multiple lines,
348 * although for the short fields that we are concerned about this is
349 * unlikely and not seen in practice.
351 while (fgets (line, sizeof line, fp) != NULL) {
353 if (len > 0 && line[len-1] == '\n') {
358 if (STRPREFIX (line, "Package: ")) {
360 name = safe_strdup (g, &line[9]);
362 else if (STRPREFIX (line, "Status: ")) {
363 installed_flag = strstr (&line[8], "installed") != NULL;
365 else if (STRPREFIX (line, "Version: ")) {
368 char *p = strchr (&line[9], '-');
371 version = safe_strdup (g, &line[9]);
372 release = safe_strdup (g, p+1);
374 version = safe_strdup (g, &line[9]);
378 else if (STREQ (line, "")) {
379 if (installed_flag && name && version)
380 add_application (g, apps, name, "", 0, version, release ? : "",
385 name = version = release = NULL;
390 if (fclose (fp) == -1) {
391 perrorf (g, "fclose: %s", status);
399 if (ret == NULL && apps != NULL)
400 guestfs_free_application_list (apps);
410 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);
412 static struct guestfs_application_list *
413 list_applications_windows (guestfs_h *g, struct inspect_fs *fs)
415 size_t len = strlen (fs->windows_systemroot) + 64;
417 snprintf (software, len, "%s/system32/config/software",
418 fs->windows_systemroot);
420 char *software_path = guestfs___case_sensitive_path_silently (g, software);
421 if (!software_path) {
422 /* Missing software hive is a problem. */
423 error (g, "no HKLM\\SOFTWARE hive found in the guest");
427 char *software_hive = NULL;
428 struct guestfs_application_list *ret = NULL;
431 software_hive = guestfs___download_to_tmp (g, fs, software_path, "software",
433 if (software_hive == NULL)
436 free (software_path);
437 software_path = NULL;
439 h = hivex_open (software_hive, g->verbose ? HIVEX_OPEN_VERBOSE : 0);
441 perrorf (g, "hivex_open");
445 /* Allocate apps list. */
446 ret = safe_malloc (g, sizeof *ret);
450 /* Ordinary native applications. */
451 const char *hivepath[] =
452 { "Microsoft", "Windows", "CurrentVersion", "Uninstall" };
453 list_applications_windows_from_path (g, h, ret, hivepath,
454 sizeof hivepath / sizeof hivepath[0]);
456 /* 32-bit emulated Windows apps running on the WOW64 emulator.
457 * http://support.microsoft.com/kb/896459 (RHBZ#692545).
459 const char *hivepath2[] =
460 { "WOW6432node", "Microsoft", "Windows", "CurrentVersion", "Uninstall" };
461 list_applications_windows_from_path (g, h, ret, hivepath2,
462 sizeof hivepath2 / sizeof hivepath2[0]);
465 if (h) hivex_close (h);
466 free (software_path);
467 free (software_hive);
473 list_applications_windows_from_path (guestfs_h *g, hive_h *h,
474 struct guestfs_application_list *apps,
475 const char **path, size_t path_len)
477 hive_node_h *children = NULL;
481 node = hivex_root (h);
483 for (i = 0; node != 0 && i < path_len; ++i)
484 node = hivex_node_get_child (h, node, path[i]);
489 children = hivex_node_children (h, node);
490 if (children == NULL)
493 /* Consider any child node that has a DisplayName key.
495 * http://nsis.sourceforge.net/Add_uninstall_information_to_Add/Remove_Programs#Optional_values
497 for (i = 0; children[i] != 0; ++i) {
500 char *display_name = NULL;
501 char *version = NULL;
502 char *install_path = NULL;
503 char *publisher = NULL;
505 char *comments = NULL;
507 /* Use the node name as a proxy for the package name in Linux. The
508 * display name is not language-independent, so it cannot be used.
510 name = hivex_node_name (h, children[i]);
514 value = hivex_node_get_value (h, children[i], "DisplayName");
516 display_name = hivex_value_string (h, value);
518 value = hivex_node_get_value (h, children[i], "DisplayVersion");
520 version = hivex_value_string (h, value);
521 value = hivex_node_get_value (h, children[i], "InstallLocation");
523 install_path = hivex_value_string (h, value);
524 value = hivex_node_get_value (h, children[i], "Publisher");
526 publisher = hivex_value_string (h, value);
527 value = hivex_node_get_value (h, children[i], "URLInfoAbout");
529 url = hivex_value_string (h, value);
530 value = hivex_node_get_value (h, children[i], "Comments");
532 comments = hivex_value_string (h, value);
534 add_application (g, apps, name, display_name, 0,
557 add_application (guestfs_h *g, struct guestfs_application_list *apps,
558 const char *name, const char *display_name, int32_t epoch,
559 const char *version, const char *release,
560 const char *install_path,
561 const char *publisher, const char *url,
562 const char *description)
565 apps->val = safe_realloc (g, apps->val,
566 apps->len * sizeof (struct guestfs_application));
567 apps->val[apps->len-1].app_name = safe_strdup (g, name);
568 apps->val[apps->len-1].app_display_name = safe_strdup (g, display_name);
569 apps->val[apps->len-1].app_epoch = epoch;
570 apps->val[apps->len-1].app_version = safe_strdup (g, version);
571 apps->val[apps->len-1].app_release = safe_strdup (g, release);
572 apps->val[apps->len-1].app_install_path = safe_strdup (g, install_path);
573 /* XXX Translated path is not implemented yet. */
574 apps->val[apps->len-1].app_trans_path = safe_strdup (g, "");
575 apps->val[apps->len-1].app_publisher = safe_strdup (g, publisher);
576 apps->val[apps->len-1].app_url = safe_strdup (g, url);
577 /* XXX The next two are not yet implemented for any package
578 * format, but we could easily support them for rpm and deb.
580 apps->val[apps->len-1].app_source_package = safe_strdup (g, "");
581 apps->val[apps->len-1].app_summary = safe_strdup (g, "");
582 apps->val[apps->len-1].app_description = safe_strdup (g, description);
585 /* Sort applications by name before returning the list. */
587 compare_applications (const void *vp1, const void *vp2)
589 const struct guestfs_application *v1 = vp1;
590 const struct guestfs_application *v2 = vp2;
592 return strcmp (v1->app_name, v2->app_name);
596 sort_applications (struct guestfs_application_list *apps)
598 if (apps && apps->val)
599 qsort (apps->val, apps->len, sizeof (struct guestfs_application),
600 compare_applications);
603 #else /* no hivex at compile time */
605 /* XXX These functions should be in an optgroup. */
607 #define NOT_IMPL(r) \
608 error (g, _("inspection API not available since this version of libguestfs was compiled without the hivex library")); \
611 struct guestfs_application_list *
612 guestfs__inspect_list_applications (guestfs_h *g, const char *root)
617 #endif /* no hivex at compile time */