Require PCRE library.
[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 #include <pcre.h>
33
34 #ifdef HAVE_HIVEX
35 #include <hivex.h>
36 #endif
37
38 #include "c-ctype.h"
39 #include "ignore-value.h"
40 #include "xstrtol.h"
41
42 #include "guestfs.h"
43 #include "guestfs-internal.h"
44 #include "guestfs-internal-actions.h"
45 #include "guestfs_protocol.h"
46
47 #if defined(HAVE_HIVEX)
48
49 #ifdef DB_DUMP
50 static struct guestfs_application_list *list_applications_rpm (guestfs_h *g, struct inspect_fs *fs);
51 #endif
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 *);
56
57 /* Unlike the simple inspect-get-* calls, this one assumes that the
58  * disks are mounted up, and reads files from the mounted disks.
59  */
60 struct guestfs_application_list *
61 guestfs__inspect_list_applications (guestfs_h *g, const char *root)
62 {
63   struct inspect_fs *fs = guestfs___search_for_root (g, root);
64   if (!fs)
65     return NULL;
66
67   struct guestfs_application_list *ret = NULL;
68
69   /* Presently we can only list applications for installed disks.  It
70    * is possible in future to get lists of packages from installers.
71    */
72   if (fs->format == OS_FORMAT_INSTALLED) {
73     switch (fs->type) {
74     case OS_TYPE_LINUX:
75       switch (fs->package_format) {
76       case OS_PACKAGE_FORMAT_RPM:
77 #ifdef DB_DUMP
78         ret = list_applications_rpm (g, fs);
79         if (ret == NULL)
80           return NULL;
81 #endif
82         break;
83
84       case OS_PACKAGE_FORMAT_DEB:
85         ret = list_applications_deb (g, fs);
86         if (ret == NULL)
87           return NULL;
88         break;
89
90       case OS_PACKAGE_FORMAT_PACMAN:
91       case OS_PACKAGE_FORMAT_EBUILD:
92       case OS_PACKAGE_FORMAT_PISI:
93       case OS_PACKAGE_FORMAT_UNKNOWN:
94       default:
95         /* nothing - keep GCC happy */;
96       }
97       break;
98
99     case OS_TYPE_WINDOWS:
100       ret = list_applications_windows (g, fs);
101       if (ret == NULL)
102         return NULL;
103       break;
104
105     case OS_TYPE_FREEBSD:
106     case OS_TYPE_UNKNOWN:
107     default:
108       /* nothing - keep GCC happy */;
109     }
110   }
111
112   if (ret == NULL) {
113     /* Don't know how to do inspection.  Not an error, return an
114      * empty list.
115      */
116     ret = safe_malloc (g, sizeof *ret);
117     ret->len = 0;
118     ret->val = NULL;
119   }
120
121   sort_applications (ret);
122
123   return ret;
124 }
125
126 #ifdef DB_DUMP
127
128 /* This data comes from the Name database, and contains the application
129  * names and the first 4 bytes of the link field.
130  */
131 struct rpm_names_list {
132   struct rpm_name *names;
133   size_t len;
134 };
135 struct rpm_name {
136   char *name;
137   char link[4];
138 };
139
140 static void
141 free_rpm_names_list (struct rpm_names_list *list)
142 {
143   size_t i;
144
145   for (i = 0; i < list->len; ++i)
146     free (list->names[i].name);
147   free (list->names);
148 }
149
150 static int
151 compare_links (const void *av, const void *bv)
152 {
153   const struct rpm_name *a = av;
154   const struct rpm_name *b = bv;
155   return memcmp (a->link, b->link, 4);
156 }
157
158 static int
159 read_rpm_name (guestfs_h *g,
160                const unsigned char *key, size_t keylen,
161                const unsigned char *value, size_t valuelen,
162                void *listv)
163 {
164   struct rpm_names_list *list = listv;
165   char *name;
166
167   /* Ignore bogus entries. */
168   if (keylen == 0 || valuelen < 4)
169     return 0;
170
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);
174   name[keylen] = '\0';
175
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);
180   list->len++;
181
182   return 0;
183 }
184
185 struct read_package_data {
186   struct rpm_names_list *list;
187   struct guestfs_application_list *apps;
188 };
189
190 static int
191 read_package (guestfs_h *g,
192               const unsigned char *key, size_t keylen,
193               const unsigned char *value, size_t valuelen,
194               void *datav)
195 {
196   struct read_package_data *data = datav;
197   struct rpm_name nkey, *entry;
198   char *p;
199   size_t len;
200   ssize_t max;
201   char *nul_name_nul, *version, *release;
202
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).
208    */
209
210   /* Ignore bogus entries. */
211   if (keylen < 4 || valuelen == 0)
212     return 0;
213
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);
218   if (!entry)
219     return 0;                   /* Not found - ignore it. */
220
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
224    * hack.
225    */
226
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);
234   free (nul_name_nul);
235   if (!p)
236     return 0;
237
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);
241   if (max < 0)
242     max = 0;
243   version = safe_strndup (g, p, max);
244
245   len = strlen (version);
246   p += len + 1;
247   max = valuelen - (p - (char *) value);
248   if (max < 0)
249     max = 0;
250   release = safe_strndup (g, p, max);
251
252   /* Add the application and what we know. */
253   add_application (g, data->apps, entry->name, "", 0, version, release,
254                    "", "", "", "");
255
256   free (version);
257   free (release);
258
259   return 0;
260 }
261
262 static struct guestfs_application_list *
263 list_applications_rpm (guestfs_h *g, struct inspect_fs *fs)
264 {
265   char *Name = NULL, *Packages = NULL;
266   struct rpm_names_list list = { .names = NULL, .len = 0 };
267   struct guestfs_application_list *apps = NULL;
268
269   Name = guestfs___download_to_tmp (g, fs,
270                                     "/var/lib/rpm/Name", "rpm_Name",
271                                     MAX_PKG_DB_SIZE);
272   if (Name == NULL)
273     goto error;
274
275   Packages = guestfs___download_to_tmp (g, fs,
276                                         "/var/lib/rpm/Packages", "rpm_Packages",
277                                         MAX_PKG_DB_SIZE);
278   if (Packages == NULL)
279     goto error;
280
281   /* Read Name database. */
282   if (guestfs___read_db_dump (g, Name, &list, read_rpm_name) == -1)
283     goto error;
284
285   /* Sort the names by link field for fast searching. */
286   qsort (list.names, list.len, sizeof (struct rpm_name), compare_links);
287
288   /* Allocate 'apps' list. */
289   apps = safe_malloc (g, sizeof *apps);
290   apps->len = 0;
291   apps->val = NULL;
292
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)
296     goto error;
297
298   free (Name);
299   free (Packages);
300   free_rpm_names_list (&list);
301
302   return apps;
303
304  error:
305   free (Name);
306   free (Packages);
307   free_rpm_names_list (&list);
308   if (apps != NULL)
309     guestfs_free_application_list (apps);
310
311   return NULL;
312 }
313
314 #endif /* defined DB_DUMP */
315
316 static struct guestfs_application_list *
317 list_applications_deb (guestfs_h *g, struct inspect_fs *fs)
318 {
319   char *status = NULL;
320   status = guestfs___download_to_tmp (g, fs, "/var/lib/dpkg/status", "status",
321                                       MAX_PKG_DB_SIZE);
322   if (status == NULL)
323     return NULL;
324
325   struct guestfs_application_list *apps = NULL, *ret = NULL;
326   FILE *fp = NULL;
327   char line[1024];
328   size_t len;
329   char *name = NULL, *version = NULL, *release = NULL;
330   int installed_flag = 0;
331
332   fp = fopen (status, "r");
333   if (fp == NULL) {
334     perrorf (g, "fopen: %s", status);
335     goto out;
336   }
337
338   /* Allocate 'apps' list. */
339   apps = safe_malloc (g, sizeof *apps);
340   apps->len = 0;
341   apps->val = NULL;
342
343   /* Read the temporary file.  Each package entry is separated by
344    * a blank line.
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.
349    */
350   while (fgets (line, sizeof line, fp) != NULL) {
351     len = strlen (line);
352     if (len > 0 && line[len-1] == '\n') {
353       line[len-1] = '\0';
354       len--;
355     }
356
357     if (STRPREFIX (line, "Package: ")) {
358       free (name);
359       name = safe_strdup (g, &line[9]);
360     }
361     else if (STRPREFIX (line, "Status: ")) {
362       installed_flag = strstr (&line[8], "installed") != NULL;
363     }
364     else if (STRPREFIX (line, "Version: ")) {
365       free (version);
366       free (release);
367       char *p = strchr (&line[9], '-');
368       if (p) {
369         *p = '\0';
370         version = safe_strdup (g, &line[9]);
371         release = safe_strdup (g, p+1);
372       } else {
373         version = safe_strdup (g, &line[9]);
374         release = NULL;
375       }
376     }
377     else if (STREQ (line, "")) {
378       if (installed_flag && name && version)
379         add_application (g, apps, name, "", 0, version, release ? : "",
380                          "", "", "", "");
381       free (name);
382       free (version);
383       free (release);
384       name = version = release = NULL;
385       installed_flag = 0;
386     }
387   }
388
389   if (fclose (fp) == -1) {
390     perrorf (g, "fclose: %s", status);
391     goto out;
392   }
393   fp = NULL;
394
395   ret = apps;
396
397  out:
398   if (ret == NULL && apps != NULL)
399     guestfs_free_application_list (apps);
400   if (fp)
401     fclose (fp);
402   free (name);
403   free (version);
404   free (release);
405   free (status);
406   return ret;
407 }
408
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);
410
411 static struct guestfs_application_list *
412 list_applications_windows (guestfs_h *g, struct inspect_fs *fs)
413 {
414   size_t len = strlen (fs->windows_systemroot) + 64;
415   char software[len];
416   snprintf (software, len, "%s/system32/config/software",
417             fs->windows_systemroot);
418
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");
423     return NULL;
424   }
425
426   char *software_hive = NULL;
427   struct guestfs_application_list *ret = NULL;
428   hive_h *h = NULL;
429
430   software_hive = guestfs___download_to_tmp (g, fs, software_path, "software",
431                                              MAX_REGISTRY_SIZE);
432   if (software_hive == NULL)
433     goto out;
434
435   free (software_path);
436   software_path = NULL;
437
438   h = hivex_open (software_hive, g->verbose ? HIVEX_OPEN_VERBOSE : 0);
439   if (h == NULL) {
440     perrorf (g, "hivex_open");
441     goto out;
442   }
443
444   /* Allocate apps list. */
445   ret = safe_malloc (g, sizeof *ret);
446   ret->len = 0;
447   ret->val = NULL;
448
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]);
454
455   /* 32-bit emulated Windows apps running on the WOW64 emulator.
456    * http://support.microsoft.com/kb/896459 (RHBZ#692545).
457    */
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]);
462
463  out:
464   if (h) hivex_close (h);
465   free (software_path);
466   free (software_hive);
467
468   return ret;
469 }
470
471 static void
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)
475 {
476   hive_node_h *children = NULL;
477   hive_node_h node;
478   size_t i;
479
480   node = hivex_root (h);
481
482   for (i = 0; node != 0 && i < path_len; ++i)
483     node = hivex_node_get_child (h, node, path[i]);
484
485   if (node == 0)
486     return;
487
488   children = hivex_node_children (h, node);
489   if (children == NULL)
490     return;
491
492   /* Consider any child node that has a DisplayName key.
493    * See also:
494    * http://nsis.sourceforge.net/Add_uninstall_information_to_Add/Remove_Programs#Optional_values
495    */
496   for (i = 0; children[i] != 0; ++i) {
497     hive_value_h value;
498     char *name = NULL;
499     char *display_name = NULL;
500     char *version = NULL;
501     char *install_path = NULL;
502     char *publisher = NULL;
503     char *url = NULL;
504     char *comments = NULL;
505
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.
508      */
509     name = hivex_node_name (h, children[i]);
510     if (name == NULL)
511       continue;
512
513     value = hivex_node_get_value (h, children[i], "DisplayName");
514     if (value) {
515       display_name = hivex_value_string (h, value);
516       if (display_name) {
517         value = hivex_node_get_value (h, children[i], "DisplayVersion");
518         if (value)
519           version = hivex_value_string (h, value);
520         value = hivex_node_get_value (h, children[i], "InstallLocation");
521         if (value)
522           install_path = hivex_value_string (h, value);
523         value = hivex_node_get_value (h, children[i], "Publisher");
524         if (value)
525           publisher = hivex_value_string (h, value);
526         value = hivex_node_get_value (h, children[i], "URLInfoAbout");
527         if (value)
528           url = hivex_value_string (h, value);
529         value = hivex_node_get_value (h, children[i], "Comments");
530         if (value)
531           comments = hivex_value_string (h, value);
532
533         add_application (g, apps, name, display_name, 0,
534                          version ? : "",
535                          "",
536                          install_path ? : "",
537                          publisher ? : "",
538                          url ? : "",
539                          comments ? : "");
540       }
541     }
542
543     free (name);
544     free (display_name);
545     free (version);
546     free (install_path);
547     free (publisher);
548     free (url);
549     free (comments);
550   }
551
552   free (children);
553 }
554
555 static void
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)
562 {
563   apps->len++;
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.
578    */
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);
582 }
583
584 /* Sort applications by name before returning the list. */
585 static int
586 compare_applications (const void *vp1, const void *vp2)
587 {
588   const struct guestfs_application *v1 = vp1;
589   const struct guestfs_application *v2 = vp2;
590
591   return strcmp (v1->app_name, v2->app_name);
592 }
593
594 static void
595 sort_applications (struct guestfs_application_list *apps)
596 {
597   if (apps && apps->val)
598     qsort (apps->val, apps->len, sizeof (struct guestfs_application),
599            compare_applications);
600 }
601
602 #else /* no hivex at compile time */
603
604 /* XXX These functions should be in an optgroup. */
605
606 #define NOT_IMPL(r)                                                     \
607   error (g, _("inspection API not available since this version of libguestfs was compiled without the hivex library")); \
608   return r
609
610 struct guestfs_application_list *
611 guestfs__inspect_list_applications (guestfs_h *g, const char *root)
612 {
613   NOT_IMPL(NULL);
614 }
615
616 #endif /* no hivex at compile time */