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