8ce09ab7019f7e682b6d729d408b11860bebffc4
[libguestfs.git] / src / inspect_apps.c
1 /* libguestfs
2  * Copyright (C) 2010-2011 Red Hat Inc.
3  *
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.
8  *
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.
13  *
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
17  */
18
19 #include <config.h>
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <stdint.h>
24 #include <inttypes.h>
25 #include <unistd.h>
26 #include <fcntl.h>
27 #include <string.h>
28 #include <sys/stat.h>
29 #include <errno.h>
30 #include <endian.h>
31
32 #ifdef HAVE_PCRE
33 #include <pcre.h>
34 #endif
35
36 #ifdef HAVE_HIVEX
37 #include <hivex.h>
38 #endif
39
40 #include "c-ctype.h"
41 #include "ignore-value.h"
42 #include "xstrtol.h"
43
44 #include "guestfs.h"
45 #include "guestfs-internal.h"
46 #include "guestfs-internal-actions.h"
47 #include "guestfs_protocol.h"
48
49 #if defined(HAVE_PCRE) && defined(HAVE_HIVEX)
50
51 #ifdef DB_DUMP
52 static struct guestfs_application_list *list_applications_rpm (guestfs_h *g, struct inspect_fs *fs);
53 #endif
54 static struct guestfs_application_list *list_applications_deb (guestfs_h *g, struct inspect_fs *fs);
55 static struct guestfs_application_list *list_applications_windows (guestfs_h *g, struct inspect_fs *fs);
56 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);
57 static void sort_applications (struct guestfs_application_list *);
58
59 /* Unlike the simple inspect-get-* calls, this one assumes that the
60  * disks are mounted up, and reads files from the mounted disks.
61  */
62 struct guestfs_application_list *
63 guestfs__inspect_list_applications (guestfs_h *g, const char *root)
64 {
65   struct inspect_fs *fs = guestfs___search_for_root (g, root);
66   if (!fs)
67     return NULL;
68
69   struct guestfs_application_list *ret = NULL;
70
71   /* Presently we can only list applications for installed disks.  It
72    * is possible in future to get lists of packages from installers.
73    */
74   if (fs->format == OS_FORMAT_INSTALLED) {
75     switch (fs->type) {
76     case OS_TYPE_LINUX:
77       switch (fs->package_format) {
78       case OS_PACKAGE_FORMAT_RPM:
79 #ifdef DB_DUMP
80         ret = list_applications_rpm (g, fs);
81         if (ret == NULL)
82           return NULL;
83 #endif
84         break;
85
86       case OS_PACKAGE_FORMAT_DEB:
87         ret = list_applications_deb (g, fs);
88         if (ret == NULL)
89           return NULL;
90         break;
91
92       case OS_PACKAGE_FORMAT_PACMAN:
93       case OS_PACKAGE_FORMAT_EBUILD:
94       case OS_PACKAGE_FORMAT_PISI:
95       case OS_PACKAGE_FORMAT_UNKNOWN:
96       default:
97         /* nothing - keep GCC happy */;
98       }
99       break;
100
101     case OS_TYPE_WINDOWS:
102       ret = list_applications_windows (g, fs);
103       if (ret == NULL)
104         return NULL;
105       break;
106
107     case OS_TYPE_FREEBSD:
108     case OS_TYPE_UNKNOWN:
109     default:
110       /* nothing - keep GCC happy */;
111     }
112   }
113
114   if (ret == NULL) {
115     /* Don't know how to do inspection.  Not an error, return an
116      * empty list.
117      */
118     ret = safe_malloc (g, sizeof *ret);
119     ret->len = 0;
120     ret->val = NULL;
121   }
122
123   sort_applications (ret);
124
125   return ret;
126 }
127
128 #ifdef DB_DUMP
129
130 static int
131 read_rpm_name (guestfs_h *g,
132                const unsigned char *key, size_t keylen,
133                const unsigned char *value, size_t valuelen,
134                void *appsv)
135 {
136   struct guestfs_application_list *apps = appsv;
137   char *name;
138
139   /* The name (key) field won't be NUL-terminated, so we must do that. */
140   name = safe_malloc (g, keylen+1);
141   memcpy (name, key, keylen);
142   name[keylen] = '\0';
143
144   add_application (g, apps, name, "", 0, "", "", "", "", "", "");
145
146   free (name);
147
148   return 0;
149 }
150
151 static struct guestfs_application_list *
152 list_applications_rpm (guestfs_h *g, struct inspect_fs *fs)
153 {
154   const char *basename = "rpm_Name";
155   char tmpdir_basename[strlen (g->tmpdir) + strlen (basename) + 2];
156   snprintf (tmpdir_basename, sizeof tmpdir_basename, "%s/%s",
157             g->tmpdir, basename);
158
159   if (guestfs___download_to_tmp (g, "/var/lib/rpm/Name", basename,
160                                  MAX_PKG_DB_SIZE) == -1)
161     return NULL;
162
163   /* Allocate 'apps' list. */
164   struct guestfs_application_list *apps;
165   apps = safe_malloc (g, sizeof *apps);
166   apps->len = 0;
167   apps->val = NULL;
168
169   if (guestfs___read_db_dump (g, tmpdir_basename, apps, read_rpm_name) == -1) {
170     guestfs_free_application_list (apps);
171     return NULL;
172   }
173
174   return apps;
175 }
176
177 #endif /* defined DB_DUMP */
178
179 static struct guestfs_application_list *
180 list_applications_deb (guestfs_h *g, struct inspect_fs *fs)
181 {
182   const char *basename = "deb_status";
183   char tmpdir_basename[strlen (g->tmpdir) + strlen (basename) + 2];
184   snprintf (tmpdir_basename, sizeof tmpdir_basename, "%s/%s",
185             g->tmpdir, basename);
186
187   if (guestfs___download_to_tmp (g, "/var/lib/dpkg/status", basename,
188                                  MAX_PKG_DB_SIZE) == -1)
189     return NULL;
190
191   struct guestfs_application_list *apps = NULL, *ret = NULL;
192   FILE *fp = NULL;
193   char line[1024];
194   size_t len;
195   char *name = NULL, *version = NULL, *release = NULL;
196   int installed_flag = 0;
197
198   fp = fopen (tmpdir_basename, "r");
199   if (fp == NULL) {
200     perrorf (g, "fopen: %s", tmpdir_basename);
201     goto out;
202   }
203
204   /* Allocate 'apps' list. */
205   apps = safe_malloc (g, sizeof *apps);
206   apps->len = 0;
207   apps->val = NULL;
208
209   /* Read the temporary file.  Each package entry is separated by
210    * a blank line.
211    * XXX Strictly speaking this is in mailbox header format, so it
212    * would be possible for fields to spread across multiple lines,
213    * although for the short fields that we are concerned about this is
214    * unlikely and not seen in practice.
215    */
216   while (fgets (line, sizeof line, fp) != NULL) {
217     len = strlen (line);
218     if (len > 0 && line[len-1] == '\n') {
219       line[len-1] = '\0';
220       len--;
221     }
222
223     if (STRPREFIX (line, "Package: ")) {
224       free (name);
225       name = safe_strdup (g, &line[9]);
226     }
227     else if (STRPREFIX (line, "Status: ")) {
228       installed_flag = strstr (&line[8], "installed") != NULL;
229     }
230     else if (STRPREFIX (line, "Version: ")) {
231       free (version);
232       free (release);
233       char *p = strchr (&line[9], '-');
234       if (p) {
235         *p = '\0';
236         version = safe_strdup (g, &line[9]);
237         release = safe_strdup (g, p+1);
238       } else {
239         version = safe_strdup (g, &line[9]);
240         release = NULL;
241       }
242     }
243     else if (STREQ (line, "")) {
244       if (installed_flag && name && version)
245         add_application (g, apps, name, "", 0, version, release ? : "",
246                          "", "", "", "");
247       free (name);
248       free (version);
249       free (release);
250       name = version = release = NULL;
251       installed_flag = 0;
252     }
253   }
254
255   if (fclose (fp) == -1) {
256     perrorf (g, "fclose: %s", tmpdir_basename);
257     goto out;
258   }
259   fp = NULL;
260
261   ret = apps;
262
263  out:
264   if (ret == NULL && apps != NULL)
265     guestfs_free_application_list (apps);
266   if (fp)
267     fclose (fp);
268   free (name);
269   free (version);
270   free (release);
271   return ret;
272 }
273
274 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);
275
276 static struct guestfs_application_list *
277 list_applications_windows (guestfs_h *g, struct inspect_fs *fs)
278 {
279   const char *basename = "software";
280   char tmpdir_basename[strlen (g->tmpdir) + strlen (basename) + 2];
281   snprintf (tmpdir_basename, sizeof tmpdir_basename, "%s/%s",
282             g->tmpdir, basename);
283
284   size_t len = strlen (fs->windows_systemroot) + 64;
285   char software[len];
286   snprintf (software, len, "%s/system32/config/software",
287             fs->windows_systemroot);
288
289   char *software_path = guestfs___case_sensitive_path_silently (g, software);
290   if (!software_path)
291     /* If the software hive doesn't exist, just accept that we cannot
292      * list windows apps.
293      */
294     return 0;
295
296   struct guestfs_application_list *ret = NULL;
297   hive_h *h = NULL;
298
299   if (guestfs___download_to_tmp (g, software_path, basename,
300                                  MAX_REGISTRY_SIZE) == -1)
301     goto out;
302
303   free (software_path);
304   software_path = NULL;
305
306   h = hivex_open (tmpdir_basename, g->verbose ? HIVEX_OPEN_VERBOSE : 0);
307   if (h == NULL) {
308     perrorf (g, "hivex_open");
309     goto out;
310   }
311
312   /* Allocate apps list. */
313   ret = safe_malloc (g, sizeof *ret);
314   ret->len = 0;
315   ret->val = NULL;
316
317   /* Ordinary native applications. */
318   const char *hivepath[] =
319     { "Microsoft", "Windows", "CurrentVersion", "Uninstall" };
320   list_applications_windows_from_path (g, h, ret, hivepath,
321                                        sizeof hivepath / sizeof hivepath[0]);
322
323   /* 32-bit emulated Windows apps running on the WOW64 emulator.
324    * http://support.microsoft.com/kb/896459 (RHBZ#692545).
325    */
326   const char *hivepath2[] =
327     { "WOW6432node", "Microsoft", "Windows", "CurrentVersion", "Uninstall" };
328   list_applications_windows_from_path (g, h, ret, hivepath2,
329                                        sizeof hivepath2 / sizeof hivepath2[0]);
330
331  out:
332   if (h) hivex_close (h);
333   free (software_path);
334
335   return ret;
336 }
337
338 static void
339 list_applications_windows_from_path (guestfs_h *g, hive_h *h,
340                                      struct guestfs_application_list *apps,
341                                      const char **path, size_t path_len)
342 {
343   hive_node_h *children = NULL;
344   hive_node_h node;
345   size_t i;
346
347   node = hivex_root (h);
348
349   for (i = 0; node != 0 && i < path_len; ++i)
350     node = hivex_node_get_child (h, node, path[i]);
351
352   if (node == 0)
353     return;
354
355   children = hivex_node_children (h, node);
356   if (children == NULL)
357     return;
358
359   /* Consider any child node that has a DisplayName key.
360    * See also:
361    * http://nsis.sourceforge.net/Add_uninstall_information_to_Add/Remove_Programs#Optional_values
362    */
363   for (i = 0; children[i] != 0; ++i) {
364     hive_value_h value;
365     char *name = NULL;
366     char *display_name = NULL;
367     char *version = NULL;
368     char *install_path = NULL;
369     char *publisher = NULL;
370     char *url = NULL;
371     char *comments = NULL;
372
373     /* Use the node name as a proxy for the package name in Linux.  The
374      * display name is not language-independent, so it cannot be used.
375      */
376     name = hivex_node_name (h, children[i]);
377     if (name == NULL)
378       continue;
379
380     value = hivex_node_get_value (h, children[i], "DisplayName");
381     if (value) {
382       display_name = hivex_value_string (h, value);
383       if (display_name) {
384         value = hivex_node_get_value (h, children[i], "DisplayVersion");
385         if (value)
386           version = hivex_value_string (h, value);
387         value = hivex_node_get_value (h, children[i], "InstallLocation");
388         if (value)
389           install_path = hivex_value_string (h, value);
390         value = hivex_node_get_value (h, children[i], "Publisher");
391         if (value)
392           publisher = hivex_value_string (h, value);
393         value = hivex_node_get_value (h, children[i], "URLInfoAbout");
394         if (value)
395           url = hivex_value_string (h, value);
396         value = hivex_node_get_value (h, children[i], "Comments");
397         if (value)
398           comments = hivex_value_string (h, value);
399
400         add_application (g, apps, name, display_name, 0,
401                          version ? : "",
402                          "",
403                          install_path ? : "",
404                          publisher ? : "",
405                          url ? : "",
406                          comments ? : "");
407       }
408     }
409
410     free (name);
411     free (display_name);
412     free (version);
413     free (install_path);
414     free (publisher);
415     free (url);
416     free (comments);
417   }
418
419   free (children);
420 }
421
422 static void
423 add_application (guestfs_h *g, struct guestfs_application_list *apps,
424                  const char *name, const char *display_name, int32_t epoch,
425                  const char *version, const char *release,
426                  const char *install_path,
427                  const char *publisher, const char *url,
428                  const char *description)
429 {
430   apps->len++;
431   apps->val = safe_realloc (g, apps->val,
432                             apps->len * sizeof (struct guestfs_application));
433   apps->val[apps->len-1].app_name = safe_strdup (g, name);
434   apps->val[apps->len-1].app_display_name = safe_strdup (g, display_name);
435   apps->val[apps->len-1].app_epoch = epoch;
436   apps->val[apps->len-1].app_version = safe_strdup (g, version);
437   apps->val[apps->len-1].app_release = safe_strdup (g, release);
438   apps->val[apps->len-1].app_install_path = safe_strdup (g, install_path);
439   /* XXX Translated path is not implemented yet. */
440   apps->val[apps->len-1].app_trans_path = safe_strdup (g, "");
441   apps->val[apps->len-1].app_publisher = safe_strdup (g, publisher);
442   apps->val[apps->len-1].app_url = safe_strdup (g, url);
443   /* XXX The next two are not yet implemented for any package
444    * format, but we could easily support them for rpm and deb.
445    */
446   apps->val[apps->len-1].app_source_package = safe_strdup (g, "");
447   apps->val[apps->len-1].app_summary = safe_strdup (g, "");
448   apps->val[apps->len-1].app_description = safe_strdup (g, description);
449 }
450
451 /* Sort applications by name before returning the list. */
452 static int
453 compare_applications (const void *vp1, const void *vp2)
454 {
455   const struct guestfs_application *v1 = vp1;
456   const struct guestfs_application *v2 = vp2;
457
458   return strcmp (v1->app_name, v2->app_name);
459 }
460
461 static void
462 sort_applications (struct guestfs_application_list *apps)
463 {
464   if (apps && apps->val)
465     qsort (apps->val, apps->len, sizeof (struct guestfs_application),
466            compare_applications);
467 }
468
469 #else /* no PCRE or hivex at compile time */
470
471 /* XXX These functions should be in an optgroup. */
472
473 #define NOT_IMPL(r)                                                     \
474   error (g, _("inspection API not available since this version of libguestfs was compiled without PCRE or hivex libraries")); \
475   return r
476
477 struct guestfs_application_list *
478 guestfs__inspect_list_applications (guestfs_h *g, const char *root)
479 {
480   NOT_IMPL(NULL);
481 }
482
483 #endif /* no PCRE or hivex at compile time */