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_UNKNOWN:
95 /* nothing - keep GCC happy */;
100 ret = list_applications_windows (g, fs);
105 case OS_TYPE_FREEBSD:
106 case OS_TYPE_UNKNOWN:
108 /* nothing - keep GCC happy */;
113 /* Don't know how to do inspection. Not an error, return an
116 ret = safe_malloc (g, sizeof *ret);
121 sort_applications (ret);
128 /* This data comes from the Name database, and contains the application
129 * names and the first 4 bytes of the link field.
131 struct rpm_names_list {
132 struct rpm_name *names;
141 free_rpm_names_list (struct rpm_names_list *list)
145 for (i = 0; i < list->len; ++i)
146 free (list->names[i].name);
151 compare_links (const void *av, const void *bv)
153 const struct rpm_name *a = av;
154 const struct rpm_name *b = bv;
155 return memcmp (a->link, b->link, 4);
159 read_rpm_name (guestfs_h *g,
160 const unsigned char *key, size_t keylen,
161 const unsigned char *value, size_t valuelen,
164 struct rpm_names_list *list = listv;
167 /* Ignore bogus entries. */
168 if (keylen == 0 || valuelen < 4)
171 /* The name (key) field won't be NUL-terminated, so we must do that. */
172 name = safe_malloc (g, keylen+1);
173 memcpy (name, key, keylen);
176 list->names = safe_realloc (g, list->names,
177 (list->len + 1) * sizeof (struct rpm_name));
178 list->names[list->len].name = name;
179 memcpy (list->names[list->len].link, value, 4);
185 struct read_package_data {
186 struct rpm_names_list *list;
187 struct guestfs_application_list *apps;
191 read_package (guestfs_h *g,
192 const unsigned char *key, size_t keylen,
193 const unsigned char *value, size_t valuelen,
196 struct read_package_data *data = datav;
197 struct rpm_name nkey, *entry;
201 char *nul_name_nul, *version, *release;
203 /* This function reads one (key, value) pair from the Packages
204 * database. The key is the link field (see struct rpm_name). The
205 * value is a long binary string, but we can extract the version
206 * number from it as below. First we have to look up the link field
207 * in the list of links (which is sorted by link field).
210 /* Ignore bogus entries. */
211 if (keylen < 4 || valuelen == 0)
214 /* Look up the link (key) in the list. */
215 memcpy (nkey.link, key, 4);
216 entry = bsearch (&nkey, data->list->names, data->list->len,
217 sizeof (struct rpm_name), compare_links);
219 return 0; /* Not found - ignore it. */
221 /* We found a matching link entry, so that gives us the application
222 * name (entry->name). Now we can get other data for this
223 * application out of the binary value string. XXX This is a real
227 /* Look for \0<name>\0 */
228 len = strlen (entry->name);
229 nul_name_nul = safe_malloc (g, len + 2);
230 nul_name_nul[0] = '\0';
231 memcpy (&nul_name_nul[1], entry->name, len);
232 nul_name_nul[len+1] = '\0';
233 p = memmem (value, valuelen, nul_name_nul, len+2);
238 /* Following that are \0-delimited version and release fields. */
239 p += len + 2; /* Note we have to skip \0 + name + \0. */
240 max = valuelen - (p - (char *) value);
243 version = safe_strndup (g, p, max);
245 len = strlen (version);
247 max = valuelen - (p - (char *) value);
250 release = safe_strndup (g, p, max);
252 /* Add the application and what we know. */
253 add_application (g, data->apps, entry->name, "", 0, version, release,
262 static struct guestfs_application_list *
263 list_applications_rpm (guestfs_h *g, struct inspect_fs *fs)
265 char *Name = NULL, *Packages = NULL;
266 struct rpm_names_list list = { .names = NULL, .len = 0 };
267 struct guestfs_application_list *apps = NULL;
269 Name = guestfs___download_to_tmp (g, fs,
270 "/var/lib/rpm/Name", "rpm_Name",
275 Packages = guestfs___download_to_tmp (g, fs,
276 "/var/lib/rpm/Packages", "rpm_Packages",
278 if (Packages == NULL)
281 /* Read Name database. */
282 if (guestfs___read_db_dump (g, Name, &list, read_rpm_name) == -1)
285 /* Sort the names by link field for fast searching. */
286 qsort (list.names, list.len, sizeof (struct rpm_name), compare_links);
288 /* Allocate 'apps' list. */
289 apps = safe_malloc (g, sizeof *apps);
293 /* Read Packages database. */
294 struct read_package_data data = { .list = &list, .apps = apps };
295 if (guestfs___read_db_dump (g, Packages, &data, read_package) == -1)
300 free_rpm_names_list (&list);
307 free_rpm_names_list (&list);
309 guestfs_free_application_list (apps);
314 #endif /* defined DB_DUMP */
316 static struct guestfs_application_list *
317 list_applications_deb (guestfs_h *g, struct inspect_fs *fs)
320 status = guestfs___download_to_tmp (g, fs, "/var/lib/dpkg/status", "status",
325 struct guestfs_application_list *apps = NULL, *ret = NULL;
329 char *name = NULL, *version = NULL, *release = NULL;
330 int installed_flag = 0;
332 fp = fopen (status, "r");
334 perrorf (g, "fopen: %s", status);
338 /* Allocate 'apps' list. */
339 apps = safe_malloc (g, sizeof *apps);
343 /* Read the temporary file. Each package entry is separated by
345 * XXX Strictly speaking this is in mailbox header format, so it
346 * would be possible for fields to spread across multiple lines,
347 * although for the short fields that we are concerned about this is
348 * unlikely and not seen in practice.
350 while (fgets (line, sizeof line, fp) != NULL) {
352 if (len > 0 && line[len-1] == '\n') {
357 if (STRPREFIX (line, "Package: ")) {
359 name = safe_strdup (g, &line[9]);
361 else if (STRPREFIX (line, "Status: ")) {
362 installed_flag = strstr (&line[8], "installed") != NULL;
364 else if (STRPREFIX (line, "Version: ")) {
367 char *p = strchr (&line[9], '-');
370 version = safe_strdup (g, &line[9]);
371 release = safe_strdup (g, p+1);
373 version = safe_strdup (g, &line[9]);
377 else if (STREQ (line, "")) {
378 if (installed_flag && name && version)
379 add_application (g, apps, name, "", 0, version, release ? : "",
384 name = version = release = NULL;
389 if (fclose (fp) == -1) {
390 perrorf (g, "fclose: %s", status);
398 if (ret == NULL && apps != NULL)
399 guestfs_free_application_list (apps);
409 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);
411 static struct guestfs_application_list *
412 list_applications_windows (guestfs_h *g, struct inspect_fs *fs)
414 size_t len = strlen (fs->windows_systemroot) + 64;
416 snprintf (software, len, "%s/system32/config/software",
417 fs->windows_systemroot);
419 char *software_path = guestfs___case_sensitive_path_silently (g, software);
420 if (!software_path) {
421 /* Missing software hive is a problem. */
422 error (g, "no HKLM\\SOFTWARE hive found in the guest");
426 char *software_hive = NULL;
427 struct guestfs_application_list *ret = NULL;
430 software_hive = guestfs___download_to_tmp (g, fs, software_path, "software",
432 if (software_hive == NULL)
435 free (software_path);
436 software_path = NULL;
438 h = hivex_open (software_hive, g->verbose ? HIVEX_OPEN_VERBOSE : 0);
440 perrorf (g, "hivex_open");
444 /* Allocate apps list. */
445 ret = safe_malloc (g, sizeof *ret);
449 /* Ordinary native applications. */
450 const char *hivepath[] =
451 { "Microsoft", "Windows", "CurrentVersion", "Uninstall" };
452 list_applications_windows_from_path (g, h, ret, hivepath,
453 sizeof hivepath / sizeof hivepath[0]);
455 /* 32-bit emulated Windows apps running on the WOW64 emulator.
456 * http://support.microsoft.com/kb/896459 (RHBZ#692545).
458 const char *hivepath2[] =
459 { "WOW6432node", "Microsoft", "Windows", "CurrentVersion", "Uninstall" };
460 list_applications_windows_from_path (g, h, ret, hivepath2,
461 sizeof hivepath2 / sizeof hivepath2[0]);
464 if (h) hivex_close (h);
465 free (software_path);
466 free (software_hive);
472 list_applications_windows_from_path (guestfs_h *g, hive_h *h,
473 struct guestfs_application_list *apps,
474 const char **path, size_t path_len)
476 hive_node_h *children = NULL;
480 node = hivex_root (h);
482 for (i = 0; node != 0 && i < path_len; ++i)
483 node = hivex_node_get_child (h, node, path[i]);
488 children = hivex_node_children (h, node);
489 if (children == NULL)
492 /* Consider any child node that has a DisplayName key.
494 * http://nsis.sourceforge.net/Add_uninstall_information_to_Add/Remove_Programs#Optional_values
496 for (i = 0; children[i] != 0; ++i) {
499 char *display_name = NULL;
500 char *version = NULL;
501 char *install_path = NULL;
502 char *publisher = NULL;
504 char *comments = NULL;
506 /* Use the node name as a proxy for the package name in Linux. The
507 * display name is not language-independent, so it cannot be used.
509 name = hivex_node_name (h, children[i]);
513 value = hivex_node_get_value (h, children[i], "DisplayName");
515 display_name = hivex_value_string (h, value);
517 value = hivex_node_get_value (h, children[i], "DisplayVersion");
519 version = hivex_value_string (h, value);
520 value = hivex_node_get_value (h, children[i], "InstallLocation");
522 install_path = hivex_value_string (h, value);
523 value = hivex_node_get_value (h, children[i], "Publisher");
525 publisher = hivex_value_string (h, value);
526 value = hivex_node_get_value (h, children[i], "URLInfoAbout");
528 url = hivex_value_string (h, value);
529 value = hivex_node_get_value (h, children[i], "Comments");
531 comments = hivex_value_string (h, value);
533 add_application (g, apps, name, display_name, 0,
556 add_application (guestfs_h *g, struct guestfs_application_list *apps,
557 const char *name, const char *display_name, int32_t epoch,
558 const char *version, const char *release,
559 const char *install_path,
560 const char *publisher, const char *url,
561 const char *description)
564 apps->val = safe_realloc (g, apps->val,
565 apps->len * sizeof (struct guestfs_application));
566 apps->val[apps->len-1].app_name = safe_strdup (g, name);
567 apps->val[apps->len-1].app_display_name = safe_strdup (g, display_name);
568 apps->val[apps->len-1].app_epoch = epoch;
569 apps->val[apps->len-1].app_version = safe_strdup (g, version);
570 apps->val[apps->len-1].app_release = safe_strdup (g, release);
571 apps->val[apps->len-1].app_install_path = safe_strdup (g, install_path);
572 /* XXX Translated path is not implemented yet. */
573 apps->val[apps->len-1].app_trans_path = safe_strdup (g, "");
574 apps->val[apps->len-1].app_publisher = safe_strdup (g, publisher);
575 apps->val[apps->len-1].app_url = safe_strdup (g, url);
576 /* XXX The next two are not yet implemented for any package
577 * format, but we could easily support them for rpm and deb.
579 apps->val[apps->len-1].app_source_package = safe_strdup (g, "");
580 apps->val[apps->len-1].app_summary = safe_strdup (g, "");
581 apps->val[apps->len-1].app_description = safe_strdup (g, description);
584 /* Sort applications by name before returning the list. */
586 compare_applications (const void *vp1, const void *vp2)
588 const struct guestfs_application *v1 = vp1;
589 const struct guestfs_application *v2 = vp2;
591 return strcmp (v1->app_name, v2->app_name);
595 sort_applications (struct guestfs_application_list *apps)
597 if (apps && apps->val)
598 qsort (apps->val, apps->len, sizeof (struct guestfs_application),
599 compare_applications);
602 #else /* no hivex at compile time */
604 /* XXX These functions should be in an optgroup. */
606 #define NOT_IMPL(r) \
607 error (g, _("inspection API not available since this version of libguestfs was compiled without the hivex library")); \
610 struct guestfs_application_list *
611 guestfs__inspect_list_applications (guestfs_h *g, const char *root)
616 #endif /* no hivex at compile time */