inspect: Refuse to download software hive if it is huge.
[libguestfs.git] / src / inspect.c
1 /* libguestfs
2  * Copyright (C) 2010 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 <string.h>
27 #include <sys/stat.h>
28
29 #ifdef HAVE_PCRE
30 #include <pcre.h>
31 #endif
32
33 #ifdef HAVE_HIVEX
34 #include <hivex.h>
35 #endif
36
37 #include "c-ctype.h"
38 #include "ignore-value.h"
39 #include "xstrtol.h"
40
41 #include "guestfs.h"
42 #include "guestfs-internal.h"
43 #include "guestfs-internal-actions.h"
44 #include "guestfs_protocol.h"
45
46 #if defined(HAVE_PCRE) && defined(HAVE_HIVEX)
47
48 /* Compile all the regular expressions once when the shared library is
49  * loaded.  PCRE is thread safe so we're supposedly OK here if
50  * multiple threads call into the libguestfs API functions below
51  * simultaneously.
52  */
53 static pcre *re_fedora;
54 static pcre *re_rhel_old;
55 static pcre *re_rhel;
56 static pcre *re_rhel_no_minor;
57 static pcre *re_major_minor;
58 static pcre *re_aug_seq;
59 static pcre *re_xdev;
60 static pcre *re_windows_version;
61
62 static void compile_regexps (void) __attribute__((constructor));
63 static void free_regexps (void) __attribute__((destructor));
64
65 static void
66 compile_regexps (void)
67 {
68   const char *err;
69   int offset;
70
71 #define COMPILE(re,pattern,options)                                     \
72   do {                                                                  \
73     re = pcre_compile ((pattern), (options), &err, &offset, NULL);      \
74     if (re == NULL) {                                                   \
75       ignore_value (write (2, err, strlen (err)));                      \
76       abort ();                                                         \
77     }                                                                   \
78   } while (0)
79
80   COMPILE (re_fedora, "Fedora release (\\d+)", 0);
81   COMPILE (re_rhel_old,
82            "(?:Red Hat Enterprise Linux|CentOS|Scientific Linux).*release (\\d+).*Update (\\d+)", 0);
83   COMPILE (re_rhel,
84            "(?:Red Hat Enterprise Linux|CentOS|Scientific Linux).*release (\\d+)\\.(\\d+)", 0);
85   COMPILE (re_rhel_no_minor,
86            "(?:Red Hat Enterprise Linux|CentOS|Scientific Linux).*release (\\d+)", 0);
87   COMPILE (re_major_minor, "(\\d+)\\.(\\d+)", 0);
88   COMPILE (re_aug_seq, "/\\d+$", 0);
89   COMPILE (re_xdev, "^/dev/(?:h|s|v|xv)d([a-z]\\d*)$", 0);
90   COMPILE (re_windows_version, "^(\\d+)\\.(\\d+)", 0);
91 }
92
93 static void
94 free_regexps (void)
95 {
96   pcre_free (re_fedora);
97   pcre_free (re_rhel_old);
98   pcre_free (re_rhel);
99   pcre_free (re_rhel_no_minor);
100   pcre_free (re_major_minor);
101   pcre_free (re_aug_seq);
102   pcre_free (re_xdev);
103   pcre_free (re_windows_version);
104 }
105
106 /* The main inspection code. */
107 static int check_for_filesystem_on (guestfs_h *g, const char *device);
108
109 char **
110 guestfs__inspect_os (guestfs_h *g)
111 {
112   /* Remove any information previously stored in the handle. */
113   guestfs___free_inspect_info (g);
114
115   if (guestfs_umount_all (g) == -1)
116     return NULL;
117
118   /* Iterate over all possible devices.  Try to mount each
119    * (read-only).  Examine ones which contain filesystems and add that
120    * information to the handle.
121    */
122   /* Look to see if any devices directly contain filesystems (RHBZ#590167). */
123   char **devices;
124   devices = guestfs_list_devices (g);
125   if (devices == NULL)
126     return NULL;
127
128   size_t i;
129   for (i = 0; devices[i] != NULL; ++i) {
130     if (check_for_filesystem_on (g, devices[i]) == -1) {
131       guestfs___free_string_list (devices);
132       guestfs___free_inspect_info (g);
133       return NULL;
134     }
135   }
136   guestfs___free_string_list (devices);
137
138   /* Look at all partitions. */
139   char **partitions;
140   partitions = guestfs_list_partitions (g);
141   if (partitions == NULL) {
142     guestfs___free_inspect_info (g);
143     return NULL;
144   }
145
146   for (i = 0; partitions[i] != NULL; ++i) {
147     if (check_for_filesystem_on (g, partitions[i]) == -1) {
148       guestfs___free_string_list (partitions);
149       guestfs___free_inspect_info (g);
150       return NULL;
151     }
152   }
153   guestfs___free_string_list (partitions);
154
155   /* Look at all LVs. */
156   if (guestfs___feature_available (g, "lvm2")) {
157     char **lvs;
158     lvs = guestfs_lvs (g);
159     if (lvs == NULL) {
160       guestfs___free_inspect_info (g);
161       return NULL;
162     }
163
164     for (i = 0; lvs[i] != NULL; ++i) {
165       if (check_for_filesystem_on (g, lvs[i]) == -1) {
166         guestfs___free_string_list (lvs);
167         guestfs___free_inspect_info (g);
168         return NULL;
169       }
170     }
171     guestfs___free_string_list (lvs);
172   }
173
174   /* At this point we have, in the handle, a list of all filesystems
175    * found and data about each one.  Now we assemble the list of
176    * filesystems which are root devices and return that to the user.
177    */
178   size_t count = 0;
179   for (i = 0; i < g->nr_fses; ++i)
180     if (g->fses[i].is_root)
181       count++;
182
183   char **ret = calloc (count+1, sizeof (char *));
184   if (ret == NULL) {
185     perrorf (g, "calloc");
186     guestfs___free_inspect_info (g);
187     return NULL;
188   }
189
190   count = 0;
191   for (i = 0; i < g->nr_fses; ++i) {
192     if (g->fses[i].is_root) {
193       ret[count] = safe_strdup (g, g->fses[i].device);
194       count++;
195     }
196   }
197   ret[count] = NULL;
198
199   return ret;
200 }
201
202 /* Find out if 'device' contains a filesystem.  If it does, add
203  * another entry in g->fses.
204  */
205 static int check_filesystem (guestfs_h *g, const char *device);
206 static int check_linux_root (guestfs_h *g, struct inspect_fs *fs);
207 static void check_architecture (guestfs_h *g, struct inspect_fs *fs);
208 static int check_fstab (guestfs_h *g, struct inspect_fs *fs);
209 static int check_windows_root (guestfs_h *g, struct inspect_fs *fs);
210 static int check_windows_arch (guestfs_h *g, struct inspect_fs *fs);
211 static int check_windows_registry (guestfs_h *g, struct inspect_fs *fs);
212 static char *resolve_windows_path_silently (guestfs_h *g, const char *);
213 static int extend_fses (guestfs_h *g);
214 static int parse_unsigned_int (guestfs_h *g, const char *str);
215 static int add_fstab_entry (guestfs_h *g, struct inspect_fs *fs,
216                             const char *spec, const char *mp);
217 static char *resolve_fstab_device (guestfs_h *g, const char *spec);
218
219 static int
220 check_for_filesystem_on (guestfs_h *g, const char *device)
221 {
222   /* Get vfs-type in order to check if it's a Linux(?) swap device.
223    * If there's an error we should ignore it, so to do that we have to
224    * temporarily replace the error handler with a null one.
225    */
226   guestfs_error_handler_cb old_error_cb = g->error_cb;
227   g->error_cb = NULL;
228   char *vfs_type = guestfs_vfs_type (g, device);
229   g->error_cb = old_error_cb;
230
231   int is_swap = vfs_type && STREQ (vfs_type, "swap");
232
233   if (g->verbose)
234     fprintf (stderr, "check_for_filesystem_on: %s (%s)\n",
235              device, vfs_type ? vfs_type : "failed to get vfs type");
236
237   if (is_swap) {
238     free (vfs_type);
239     if (extend_fses (g) == -1)
240       return -1;
241     g->fses[g->nr_fses-1].is_swap = 1;
242     return 0;
243   }
244
245   /* Try mounting the device.  As above, ignore errors. */
246   g->error_cb = NULL;
247   int r = guestfs_mount_ro (g, device, "/");
248   if (r == -1 && vfs_type && STREQ (vfs_type, "ufs")) /* Hack for the *BSDs. */
249     r = guestfs_mount_vfs (g, "ro,ufstype=ufs2", "ufs", device, "/");
250   free (vfs_type);
251   g->error_cb = old_error_cb;
252   if (r == -1)
253     return 0;
254
255   /* Do the rest of the checks. */
256   r = check_filesystem (g, device);
257
258   /* Unmount the filesystem. */
259   if (guestfs_umount_all (g) == -1)
260     return -1;
261
262   return r;
263 }
264
265 static int
266 check_filesystem (guestfs_h *g, const char *device)
267 {
268   if (extend_fses (g) == -1)
269     return -1;
270
271   struct inspect_fs *fs = &g->fses[g->nr_fses-1];
272
273   fs->device = safe_strdup (g, device);
274   fs->is_mountable = 1;
275
276   /* Grub /boot? */
277   if (guestfs_is_file (g, "/grub/menu.lst") > 0 ||
278       guestfs_is_file (g, "/grub/grub.conf") > 0)
279     fs->content = FS_CONTENT_LINUX_BOOT;
280   /* Linux root? */
281   else if (guestfs_is_dir (g, "/etc") > 0 &&
282            guestfs_is_dir (g, "/bin") > 0 &&
283            guestfs_is_file (g, "/etc/fstab") > 0) {
284     fs->is_root = 1;
285     fs->content = FS_CONTENT_LINUX_ROOT;
286     if (check_linux_root (g, fs) == -1)
287       return -1;
288   }
289   /* Linux /usr/local? */
290   else if (guestfs_is_dir (g, "/etc") > 0 &&
291            guestfs_is_dir (g, "/bin") > 0 &&
292            guestfs_is_dir (g, "/share") > 0 &&
293            guestfs_exists (g, "/local") == 0 &&
294            guestfs_is_file (g, "/etc/fstab") == 0)
295     fs->content = FS_CONTENT_LINUX_USR_LOCAL;
296   /* Linux /usr? */
297   else if (guestfs_is_dir (g, "/etc") > 0 &&
298            guestfs_is_dir (g, "/bin") > 0 &&
299            guestfs_is_dir (g, "/share") > 0 &&
300            guestfs_exists (g, "/local") > 0 &&
301            guestfs_is_file (g, "/etc/fstab") == 0)
302     fs->content = FS_CONTENT_LINUX_USR;
303   /* Linux /var? */
304   else if (guestfs_is_dir (g, "/log") > 0 &&
305            guestfs_is_dir (g, "/run") > 0 &&
306            guestfs_is_dir (g, "/spool") > 0)
307     fs->content = FS_CONTENT_LINUX_VAR;
308   /* Windows root? */
309   else if (guestfs_is_file (g, "/AUTOEXEC.BAT") > 0 ||
310            guestfs_is_file (g, "/autoexec.bat") > 0 ||
311            guestfs_is_dir (g, "/Program Files") > 0 ||
312            guestfs_is_dir (g, "/WINDOWS") > 0 ||
313            guestfs_is_dir (g, "/Windows") > 0 ||
314            guestfs_is_dir (g, "/windows") > 0 ||
315            guestfs_is_dir (g, "/WIN32") > 0 ||
316            guestfs_is_dir (g, "/Win32") > 0 ||
317            guestfs_is_dir (g, "/WINNT") > 0 ||
318            guestfs_is_file (g, "/boot.ini") > 0 ||
319            guestfs_is_file (g, "/ntldr") > 0) {
320     fs->is_root = 1;
321     fs->content = FS_CONTENT_WINDOWS_ROOT;
322     if (check_windows_root (g, fs) == -1)
323       return -1;
324   }
325
326   return 0;
327 }
328
329 /* Set fs->product_name to the first line of the release file. */
330 static int
331 parse_release_file (guestfs_h *g, struct inspect_fs *fs,
332                     const char *release_filename)
333 {
334   char **product_name = guestfs_head_n (g, 1, release_filename);
335   if (product_name == NULL)
336     return -1;
337   if (product_name[0] == NULL) {
338     error (g, _("%s: file is empty"), release_filename);
339     guestfs___free_string_list (product_name);
340     return -1;
341   }
342
343   /* Note that this string becomes owned by the handle and will
344    * be freed by guestfs___free_inspect_info.
345    */
346   fs->product_name = product_name[0];
347   free (product_name);
348
349   return 0;
350 }
351
352 /* Parse generic MAJOR.MINOR from the fs->product_name string. */
353 static int
354 parse_major_minor (guestfs_h *g, struct inspect_fs *fs)
355 {
356   char *major, *minor;
357
358   if (match2 (g, fs->product_name, re_major_minor, &major, &minor)) {
359     fs->major_version = parse_unsigned_int (g, major);
360     free (major);
361     if (fs->major_version == -1) {
362       free (minor);
363       return -1;
364     }
365     fs->minor_version = parse_unsigned_int (g, minor);
366     free (minor);
367     if (fs->minor_version == -1)
368       return -1;
369   }
370   return 0;
371 }
372
373 /* Ubuntu has /etc/lsb-release containing:
374  *   DISTRIB_ID=Ubuntu                                # Distro
375  *   DISTRIB_RELEASE=10.04                            # Version
376  *   DISTRIB_CODENAME=lucid
377  *   DISTRIB_DESCRIPTION="Ubuntu 10.04.1 LTS"         # Product name
378  * In theory other distros could have this LSB file, but none do.
379  */
380 static int
381 parse_lsb_release (guestfs_h *g, struct inspect_fs *fs)
382 {
383   char **lines;
384   size_t i;
385   int r = 0;
386
387   lines = guestfs_head_n (g, 10, "/etc/lsb-release");
388   if (lines == NULL)
389     return -1;
390
391   for (i = 0; lines[i] != NULL; ++i) {
392     if (fs->distro == 0 &&
393         STREQ (lines[i], "DISTRIB_ID=Ubuntu")) {
394       fs->distro = OS_DISTRO_UBUNTU;
395       r = 1;
396     }
397     else if (STRPREFIX (lines[i], "DISTRIB_RELEASE=")) {
398       char *major, *minor;
399       if (match2 (g, &lines[i][16], re_major_minor, &major, &minor)) {
400         fs->major_version = parse_unsigned_int (g, major);
401         free (major);
402         if (fs->major_version == -1) {
403           free (minor);
404           guestfs___free_string_list (lines);
405           return -1;
406         }
407         fs->minor_version = parse_unsigned_int (g, minor);
408         free (minor);
409         if (fs->minor_version == -1) {
410           guestfs___free_string_list (lines);
411           return -1;
412         }
413       }
414     }
415     else if (fs->product_name == NULL &&
416              (STRPREFIX (lines[i], "DISTRIB_DESCRIPTION=\"") ||
417               STRPREFIX (lines[i], "DISTRIB_DESCRIPTION='"))) {
418       size_t len = strlen (lines[i]) - 21 - 1;
419       fs->product_name = safe_strndup (g, &lines[i][21], len);
420       r = 1;
421     }
422     else if (fs->product_name == NULL &&
423              STRPREFIX (lines[i], "DISTRIB_DESCRIPTION=")) {
424       size_t len = strlen (lines[i]) - 20;
425       fs->product_name = safe_strndup (g, &lines[i][20], len);
426       r = 1;
427     }
428   }
429
430   guestfs___free_string_list (lines);
431   return r;
432 }
433
434 /* The currently mounted device is known to be a Linux root.  Try to
435  * determine from this the distro, version, etc.  Also parse
436  * /etc/fstab to determine the arrangement of mountpoints and
437  * associated devices.
438  */
439 static int
440 check_linux_root (guestfs_h *g, struct inspect_fs *fs)
441 {
442   int r;
443
444   fs->type = OS_TYPE_LINUX;
445
446   if (guestfs_exists (g, "/etc/lsb-release") > 0) {
447     r = parse_lsb_release (g, fs);
448     if (r == -1)        /* error */
449       return -1;
450     if (r == 1)         /* ok - detected the release from this file */
451       goto skip_release_checks;
452   }
453
454   if (guestfs_exists (g, "/etc/redhat-release") > 0) {
455     fs->distro = OS_DISTRO_REDHAT_BASED; /* Something generic Red Hat-like. */
456
457     if (parse_release_file (g, fs, "/etc/redhat-release") == -1)
458       return -1;
459
460     char *major, *minor;
461     if ((major = match1 (g, fs->product_name, re_fedora)) != NULL) {
462       fs->distro = OS_DISTRO_FEDORA;
463       fs->major_version = parse_unsigned_int (g, major);
464       free (major);
465       if (fs->major_version == -1)
466         return -1;
467     }
468     else if (match2 (g, fs->product_name, re_rhel_old, &major, &minor) ||
469              match2 (g, fs->product_name, re_rhel, &major, &minor)) {
470       fs->distro = OS_DISTRO_RHEL;
471       fs->major_version = parse_unsigned_int (g, major);
472       free (major);
473       if (fs->major_version == -1) {
474         free (minor);
475         return -1;
476       }
477       fs->minor_version = parse_unsigned_int (g, minor);
478       free (minor);
479       if (fs->minor_version == -1)
480         return -1;
481     }
482     else if ((major = match1 (g, fs->product_name, re_rhel_no_minor)) != NULL) {
483       fs->distro = OS_DISTRO_RHEL;
484       fs->major_version = parse_unsigned_int (g, major);
485       free (major);
486       if (fs->major_version == -1)
487         return -1;
488       fs->minor_version = 0;
489     }
490   }
491   else if (guestfs_exists (g, "/etc/debian_version") > 0) {
492     fs->distro = OS_DISTRO_DEBIAN;
493
494     if (parse_release_file (g, fs, "/etc/debian_version") == -1)
495       return -1;
496
497     if (parse_major_minor (g, fs) == -1)
498       return -1;
499   }
500   else if (guestfs_exists (g, "/etc/pardus-release") > 0) {
501     fs->distro = OS_DISTRO_PARDUS;
502
503     if (parse_release_file (g, fs, "/etc/pardus-release") == -1)
504       return -1;
505
506     if (parse_major_minor (g, fs) == -1)
507       return -1;
508   }
509   else if (guestfs_exists (g, "/etc/arch-release") > 0) {
510     fs->distro = OS_DISTRO_ARCHLINUX;
511
512     /* /etc/arch-release file is empty and I can't see a way to
513      * determine the actual release or product string.
514      */
515   }
516   else if (guestfs_exists (g, "/etc/gentoo-release") > 0) {
517     fs->distro = OS_DISTRO_GENTOO;
518
519     if (parse_release_file (g, fs, "/etc/gentoo-release") == -1)
520       return -1;
521
522     if (parse_major_minor (g, fs) == -1)
523       return -1;
524   }
525   else if (guestfs_exists (g, "/etc/meego-release") > 0) {
526     fs->distro = OS_DISTRO_MEEGO;
527
528     if (parse_release_file (g, fs, "/etc/meego-release") == -1)
529       return -1;
530
531     if (parse_major_minor (g, fs) == -1)
532       return -1;
533   }
534
535  skip_release_checks:;
536
537   /* Determine the architecture. */
538   check_architecture (g, fs);
539
540   /* We already know /etc/fstab exists because it's part of the test
541    * for Linux root above.  We must now parse this file to determine
542    * which filesystems are used by the operating system and how they
543    * are mounted.
544    */
545   if (check_fstab (g, fs) == -1)
546     return -1;
547
548   return 0;
549 }
550
551 static void
552 check_architecture (guestfs_h *g, struct inspect_fs *fs)
553 {
554   const char *binaries[] =
555     { "/bin/bash", "/bin/ls", "/bin/echo", "/bin/rm", "/bin/sh" };
556   size_t i;
557
558   for (i = 0; i < sizeof binaries / sizeof binaries[0]; ++i) {
559     if (guestfs_is_file (g, binaries[i]) > 0) {
560       /* Ignore errors from file_architecture call. */
561       guestfs_error_handler_cb old_error_cb = g->error_cb;
562       g->error_cb = NULL;
563       char *arch = guestfs_file_architecture (g, binaries[i]);
564       g->error_cb = old_error_cb;
565
566       if (arch) {
567         /* String will be owned by handle, freed by
568          * guestfs___free_inspect_info.
569          */
570         fs->arch = arch;
571         break;
572       }
573     }
574   }
575 }
576
577 static int check_fstab_aug_open (guestfs_h *g, struct inspect_fs *fs);
578
579 static int
580 check_fstab (guestfs_h *g, struct inspect_fs *fs)
581 {
582   int r;
583   int64_t size;
584
585   /* Security: Refuse to do this if /etc/fstab is huge. */
586   size = guestfs_filesize (g, "/etc/fstab");
587   if (size == -1 || size > 100000) {
588     error (g, _("size of /etc/fstab unreasonable (%" PRIi64 " bytes)"), size);
589     return -1;
590   }
591
592   /* XXX What if !feature_available (g, "augeas")? */
593   if (guestfs_aug_init (g, "/", 16|32) == -1)
594     return -1;
595
596   /* Tell Augeas to only load /etc/fstab (thanks Raphaël Pinson). */
597   guestfs_aug_rm (g, "/augeas/load//incl[. != \"/etc/fstab\"]");
598   guestfs_aug_load (g);
599
600   r = check_fstab_aug_open (g, fs);
601   guestfs_aug_close (g);
602   if (r == -1)
603     return -1;
604
605   return 0;
606 }
607
608 static int
609 check_fstab_aug_open (guestfs_h *g, struct inspect_fs *fs)
610 {
611   char **lines = guestfs_aug_ls (g, "/files/etc/fstab");
612   if (lines == NULL)
613     return -1;
614
615   if (lines[0] == NULL) {
616     error (g, _("could not parse /etc/fstab or empty file"));
617     guestfs___free_string_list (lines);
618     return -1;
619   }
620
621   size_t i;
622   char augpath[256];
623   for (i = 0; lines[i] != NULL; ++i) {
624     /* Ignore comments.  Only care about sequence lines which
625      * match m{/\d+$}.
626      */
627     if (match (g, lines[i], re_aug_seq)) {
628       snprintf (augpath, sizeof augpath, "%s/spec", lines[i]);
629       char *spec = guestfs_aug_get (g, augpath);
630       if (spec == NULL) {
631         guestfs___free_string_list (lines);
632         return -1;
633       }
634
635       snprintf (augpath, sizeof augpath, "%s/file", lines[i]);
636       char *mp = guestfs_aug_get (g, augpath);
637       if (mp == NULL) {
638         guestfs___free_string_list (lines);
639         free (spec);
640         return -1;
641       }
642
643       int r = add_fstab_entry (g, fs, spec, mp);
644       free (spec);
645       free (mp);
646
647       if (r == -1) {
648         guestfs___free_string_list (lines);
649         return -1;
650       }
651     }
652   }
653
654   guestfs___free_string_list (lines);
655   return 0;
656 }
657
658 /* Add a filesystem and possibly a mountpoint entry for
659  * the root filesystem 'fs'.
660  *
661  * 'spec' is the fstab spec field, which might be a device name or a
662  * pseudodevice or 'UUID=...' or 'LABEL=...'.
663  *
664  * 'mp' is the mount point, which could also be 'swap' or 'none'.
665  */
666 static int
667 add_fstab_entry (guestfs_h *g, struct inspect_fs *fs,
668                  const char *spec, const char *mp)
669 {
670   /* Ignore certain mountpoints. */
671   if (STRPREFIX (mp, "/dev/") ||
672       STREQ (mp, "/dev") ||
673       STRPREFIX (mp, "/media/") ||
674       STRPREFIX (mp, "/proc/") ||
675       STREQ (mp, "/proc") ||
676       STRPREFIX (mp, "/selinux/") ||
677       STREQ (mp, "/selinux") ||
678       STRPREFIX (mp, "/sys/") ||
679       STREQ (mp, "/sys"))
680     return 0;
681
682   /* Ignore /dev/fd (floppy disks) (RHBZ#642929) and CD-ROM drives. */
683   if ((STRPREFIX (spec, "/dev/fd") && c_isdigit (spec[7])) ||
684       STREQ (spec, "/dev/floppy") ||
685       STREQ (spec, "/dev/cdrom"))
686     return 0;
687
688   /* Resolve UUID= and LABEL= to the actual device. */
689   char *device = NULL;
690   if (STRPREFIX (spec, "UUID="))
691     device = guestfs_findfs_uuid (g, &spec[5]);
692   else if (STRPREFIX (spec, "LABEL="))
693     device = guestfs_findfs_label (g, &spec[6]);
694   /* Ignore "/.swap" (Pardus) and pseudo-devices like "tmpfs". */
695   else if (STRPREFIX (spec, "/dev/"))
696     /* Resolve guest block device names. */
697     device = resolve_fstab_device (g, spec);
698
699   /* If we haven't resolved the device successfully by this point,
700    * we don't care, just ignore it.
701    */
702   if (device == NULL)
703     return 0;
704
705   char *mountpoint = safe_strdup (g, mp);
706
707   /* Add this to the fstab entry in 'fs'.
708    * Note these are further filtered by guestfs_inspect_get_mountpoints
709    * and guestfs_inspect_get_filesystems.
710    */
711   size_t n = fs->nr_fstab + 1;
712   struct inspect_fstab_entry *p;
713
714   p = realloc (fs->fstab, n * sizeof (struct inspect_fstab_entry));
715   if (p == NULL) {
716     perrorf (g, "realloc");
717     free (device);
718     free (mountpoint);
719     return -1;
720   }
721
722   fs->fstab = p;
723   fs->nr_fstab = n;
724
725   /* These are owned by the handle and freed by guestfs___free_inspect_info. */
726   fs->fstab[n-1].device = device;
727   fs->fstab[n-1].mountpoint = mountpoint;
728
729   if (g->verbose)
730     fprintf (stderr, "fstab: device=%s mountpoint=%s\n", device, mountpoint);
731
732   return 0;
733 }
734
735 /* Resolve block device name to the libguestfs device name, eg.
736  * /dev/xvdb1 => /dev/vdb1; and /dev/mapper/VG-LV => /dev/VG/LV.  This
737  * assumes that disks were added in the same order as they appear to
738  * the real VM, which is a reasonable assumption to make.  Return
739  * anything we don't recognize unchanged.
740  */
741 static char *
742 resolve_fstab_device (guestfs_h *g, const char *spec)
743 {
744   char *a1;
745   char *device = NULL;
746
747   if (STRPREFIX (spec, "/dev/mapper/")) {
748     /* LVM2 does some strange munging on /dev/mapper paths for VGs and
749      * LVs which contain '-' character:
750      *
751      * ><fs> lvcreate LV--test VG--test 32
752      * ><fs> debug ls /dev/mapper
753      * VG----test-LV----test
754      *
755      * This makes it impossible to reverse those paths directly, so
756      * we have implemented lvm_canonical_lv_name in the daemon.
757      */
758     device = guestfs_lvm_canonical_lv_name (g, spec);
759   }
760   else if ((a1 = match1 (g, spec, re_xdev)) != NULL) {
761     char **devices = guestfs_list_devices (g);
762     if (devices == NULL)
763       return NULL;
764
765     size_t count;
766     for (count = 0; devices[count] != NULL; count++)
767       ;
768
769     size_t i = a1[0] - 'a'; /* a1[0] is always [a-z] because of regex. */
770     if (i < count) {
771       size_t len = strlen (devices[i]) + strlen (a1) + 16;
772       device = safe_malloc (g, len);
773       snprintf (device, len, "%s%s", devices[i], &a1[1]);
774     }
775
776     free (a1);
777     guestfs___free_string_list (devices);
778   }
779   else {
780     /* Didn't match device pattern, return original spec unchanged. */
781     device = safe_strdup (g, spec);
782   }
783
784   return device;
785 }
786
787 /* XXX Handling of boot.ini in the Perl version was pretty broken.  It
788  * essentially didn't do anything for modern Windows guests.
789  * Therefore I've omitted all that code.
790  */
791 static int
792 check_windows_root (guestfs_h *g, struct inspect_fs *fs)
793 {
794   fs->type = OS_TYPE_WINDOWS;
795   fs->distro = OS_DISTRO_WINDOWS;
796
797   /* Try to find Windows systemroot using some common locations. */
798   const char *systemroots[] =
799     { "/windows", "/winnt", "/win32", "/win" };
800   size_t i;
801   char *systemroot = NULL;
802   for (i = 0;
803        systemroot == NULL && i < sizeof systemroots / sizeof systemroots[0];
804        ++i) {
805     systemroot = resolve_windows_path_silently (g, systemroots[i]);
806   }
807
808   if (!systemroot) {
809     error (g, _("cannot resolve Windows %%SYSTEMROOT%%"));
810     return -1;
811   }
812
813   if (g->verbose)
814     fprintf (stderr, "windows %%SYSTEMROOT%% = %s", systemroot);
815
816   /* Freed by guestfs___free_inspect_info. */
817   fs->windows_systemroot = systemroot;
818
819   if (check_windows_arch (g, fs) == -1)
820     return -1;
821
822   if (check_windows_registry (g, fs) == -1)
823     return -1;
824
825   return 0;
826 }
827
828 static int
829 check_windows_arch (guestfs_h *g, struct inspect_fs *fs)
830 {
831   size_t len = strlen (fs->windows_systemroot) + 32;
832   char cmd_exe[len];
833   snprintf (cmd_exe, len, "%s/system32/cmd.exe", fs->windows_systemroot);
834
835   char *cmd_exe_path = resolve_windows_path_silently (g, cmd_exe);
836   if (!cmd_exe_path)
837     return 0;
838
839   char *arch = guestfs_file_architecture (g, cmd_exe_path);
840   free (cmd_exe_path);
841
842   if (arch)
843     fs->arch = arch;        /* freed by guestfs___free_inspect_info */
844
845   return 0;
846 }
847
848 /* At the moment, pull just the ProductName and version numbers from
849  * the registry.  In future there is a case for making many more
850  * registry fields available to callers.
851  */
852 static int
853 check_windows_registry (guestfs_h *g, struct inspect_fs *fs)
854 {
855   TMP_TEMPLATE_ON_STACK (dir);
856 #define dir_len (strlen (dir))
857 #define software_hive_len (dir_len + 16)
858   char software_hive[software_hive_len];
859 #define cmd_len (dir_len + 16)
860   char cmd[cmd_len];
861
862   size_t len = strlen (fs->windows_systemroot) + 64;
863   char software[len];
864   snprintf (software, len, "%s/system32/config/software",
865             fs->windows_systemroot);
866
867   char *software_path = resolve_windows_path_silently (g, software);
868   if (!software_path)
869     /* If the software hive doesn't exist, just accept that we cannot
870      * find product_name etc.
871      */
872     return 0;
873
874   int ret = -1;
875   hive_h *h = NULL;
876   hive_value_h *values = NULL;
877
878   /* Security: Refuse to download registry if it is huge. */
879   int64_t size = guestfs_filesize (g, software_path);
880   if (size == -1 || size > 100000000) {
881     error (g, _("size of %s unreasonable (%" PRIi64 " bytes)"),
882            software_path, size);
883     goto out;
884   }
885
886   if (mkdtemp (dir) == NULL) {
887     perrorf (g, "mkdtemp");
888     goto out;
889   }
890
891   snprintf (software_hive, software_hive_len, "%s/software", dir);
892
893   if (guestfs_download (g, software_path, software_hive) == -1)
894     goto out;
895
896   h = hivex_open (software_hive, g->verbose ? HIVEX_OPEN_VERBOSE : 0);
897   if (h == NULL) {
898     perrorf (g, "hivex_open");
899     goto out;
900   }
901
902   hive_node_h node = hivex_root (h);
903   const char *hivepath[] =
904     { "Microsoft", "Windows NT", "CurrentVersion" };
905   size_t i;
906   for (i = 0;
907        node != 0 && i < sizeof hivepath / sizeof hivepath[0];
908        ++i) {
909     node = hivex_node_get_child (h, node, hivepath[i]);
910   }
911
912   if (node == 0) {
913     perrorf (g, "hivex: cannot locate HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion");
914     goto out;
915   }
916
917   values = hivex_node_values (h, node);
918
919   for (i = 0; values[i] != 0; ++i) {
920     char *key = hivex_value_key (h, values[i]);
921     if (key == NULL) {
922       perrorf (g, "hivex_value_key");
923       goto out;
924     }
925
926     if (STRCASEEQ (key, "ProductName")) {
927       fs->product_name = hivex_value_string (h, values[i]);
928       if (!fs->product_name) {
929         perrorf (g, "hivex_value_string");
930         free (key);
931         goto out;
932       }
933     }
934     else if (STRCASEEQ (key, "CurrentVersion")) {
935       char *version = hivex_value_string (h, values[i]);
936       if (!version) {
937         perrorf (g, "hivex_value_string");
938         free (key);
939         goto out;
940       }
941       char *major, *minor;
942       if (match2 (g, version, re_windows_version, &major, &minor)) {
943         fs->major_version = parse_unsigned_int (g, major);
944         free (major);
945         if (fs->major_version == -1) {
946           free (minor);
947           free (key);
948           free (version);
949           goto out;
950         }
951         fs->minor_version = parse_unsigned_int (g, minor);
952         free (minor);
953         if (fs->minor_version == -1) {
954           free (key);
955           free (version);
956           goto out;
957         }
958       }
959
960       free (version);
961     }
962
963     free (key);
964   }
965
966   ret = 0;
967
968  out:
969   if (h) hivex_close (h);
970   free (values);
971   free (software_path);
972
973   /* Free up the temporary directory.  Note the directory name cannot
974    * contain shell meta-characters because of the way it was
975    * constructed above.
976    */
977   snprintf (cmd, cmd_len, "rm -rf %s", dir);
978   ignore_value (system (cmd));
979 #undef dir_len
980 #undef software_hive_len
981 #undef cmd_len
982
983   return ret;
984 }
985
986 static char *
987 resolve_windows_path_silently (guestfs_h *g, const char *path)
988 {
989   guestfs_error_handler_cb old_error_cb = g->error_cb;
990   g->error_cb = NULL;
991   char *ret = guestfs_case_sensitive_path (g, path);
992   g->error_cb = old_error_cb;
993   return ret;
994 }
995
996 static int
997 extend_fses (guestfs_h *g)
998 {
999   size_t n = g->nr_fses + 1;
1000   struct inspect_fs *p;
1001
1002   p = realloc (g->fses, n * sizeof (struct inspect_fs));
1003   if (p == NULL) {
1004     perrorf (g, "realloc");
1005     return -1;
1006   }
1007
1008   g->fses = p;
1009   g->nr_fses = n;
1010
1011   memset (&g->fses[n-1], 0, sizeof (struct inspect_fs));
1012
1013   return 0;
1014 }
1015
1016 /* Parse small, unsigned ints, as used in version numbers. */
1017 static int
1018 parse_unsigned_int (guestfs_h *g, const char *str)
1019 {
1020   long ret;
1021   int r = xstrtol (str, NULL, 10, &ret, "");
1022   if (r != LONGINT_OK) {
1023     error (g, _("could not parse integer in version number: %s"), str);
1024     return -1;
1025   }
1026   return ret;
1027 }
1028
1029 static struct inspect_fs *
1030 search_for_root (guestfs_h *g, const char *root)
1031 {
1032   if (g->nr_fses == 0) {
1033     error (g, _("no inspection data: call guestfs_inspect_os first"));
1034     return NULL;
1035   }
1036
1037   size_t i;
1038   struct inspect_fs *fs;
1039   for (i = 0; i < g->nr_fses; ++i) {
1040     fs = &g->fses[i];
1041     if (fs->is_root && STREQ (root, fs->device))
1042       return fs;
1043   }
1044
1045   error (g, _("%s: root device not found: only call this function with a root device previously returned by guestfs_inspect_os"),
1046          root);
1047   return NULL;
1048 }
1049
1050 char *
1051 guestfs__inspect_get_type (guestfs_h *g, const char *root)
1052 {
1053   struct inspect_fs *fs = search_for_root (g, root);
1054   if (!fs)
1055     return NULL;
1056
1057   char *ret;
1058   switch (fs->type) {
1059   case OS_TYPE_LINUX: ret = safe_strdup (g, "linux"); break;
1060   case OS_TYPE_WINDOWS: ret = safe_strdup (g, "windows"); break;
1061   case OS_TYPE_UNKNOWN: default: ret = safe_strdup (g, "unknown"); break;
1062   }
1063
1064   return ret;
1065 }
1066
1067 char *
1068 guestfs__inspect_get_arch (guestfs_h *g, const char *root)
1069 {
1070   struct inspect_fs *fs = search_for_root (g, root);
1071   if (!fs)
1072     return NULL;
1073
1074   return safe_strdup (g, fs->arch ? : "unknown");
1075 }
1076
1077 char *
1078 guestfs__inspect_get_distro (guestfs_h *g, const char *root)
1079 {
1080   struct inspect_fs *fs = search_for_root (g, root);
1081   if (!fs)
1082     return NULL;
1083
1084   char *ret;
1085   switch (fs->distro) {
1086   case OS_DISTRO_ARCHLINUX: ret = safe_strdup (g, "archlinux"); break;
1087   case OS_DISTRO_DEBIAN: ret = safe_strdup (g, "debian"); break;
1088   case OS_DISTRO_FEDORA: ret = safe_strdup (g, "fedora"); break;
1089   case OS_DISTRO_GENTOO: ret = safe_strdup (g, "gentoo"); break;
1090   case OS_DISTRO_MEEGO: ret = safe_strdup (g, "meego"); break;
1091   case OS_DISTRO_PARDUS: ret = safe_strdup (g, "pardus"); break;
1092   case OS_DISTRO_REDHAT_BASED: ret = safe_strdup (g, "redhat-based"); break;
1093   case OS_DISTRO_RHEL: ret = safe_strdup (g, "rhel"); break;
1094   case OS_DISTRO_WINDOWS: ret = safe_strdup (g, "windows"); break;
1095   case OS_DISTRO_UBUNTU: ret = safe_strdup (g, "ubuntu"); break;
1096   case OS_DISTRO_UNKNOWN: default: ret = safe_strdup (g, "unknown"); break;
1097   }
1098
1099   return ret;
1100 }
1101
1102 int
1103 guestfs__inspect_get_major_version (guestfs_h *g, const char *root)
1104 {
1105   struct inspect_fs *fs = search_for_root (g, root);
1106   if (!fs)
1107     return -1;
1108
1109   return fs->major_version;
1110 }
1111
1112 int
1113 guestfs__inspect_get_minor_version (guestfs_h *g, const char *root)
1114 {
1115   struct inspect_fs *fs = search_for_root (g, root);
1116   if (!fs)
1117     return -1;
1118
1119   return fs->minor_version;
1120 }
1121
1122 char *
1123 guestfs__inspect_get_product_name (guestfs_h *g, const char *root)
1124 {
1125   struct inspect_fs *fs = search_for_root (g, root);
1126   if (!fs)
1127     return NULL;
1128
1129   return safe_strdup (g, fs->product_name ? : "unknown");
1130 }
1131
1132 char *
1133 guestfs__inspect_get_windows_systemroot (guestfs_h *g, const char *root)
1134 {
1135   struct inspect_fs *fs = search_for_root (g, root);
1136   if (!fs)
1137     return NULL;
1138
1139   if (!fs->windows_systemroot) {
1140     error (g, _("not a Windows guest, or systemroot could not be determined"));
1141     return NULL;
1142   }
1143
1144   return safe_strdup (g, fs->windows_systemroot);
1145 }
1146
1147 char **
1148 guestfs__inspect_get_mountpoints (guestfs_h *g, const char *root)
1149 {
1150   struct inspect_fs *fs = search_for_root (g, root);
1151   if (!fs)
1152     return NULL;
1153
1154   char **ret;
1155
1156   /* If no fstab information (Windows) return just the root. */
1157   if (fs->nr_fstab == 0) {
1158     ret = calloc (3, sizeof (char *));
1159     ret[0] = safe_strdup (g, "/");
1160     ret[1] = safe_strdup (g, root);
1161     ret[2] = NULL;
1162     return ret;
1163   }
1164
1165 #define CRITERION fs->fstab[i].mountpoint[0] == '/'
1166   size_t i, count = 0;
1167   for (i = 0; i < fs->nr_fstab; ++i)
1168     if (CRITERION)
1169       count++;
1170
1171   /* Hashtables have 2N+1 entries. */
1172   ret = calloc (2*count+1, sizeof (char *));
1173   if (ret == NULL) {
1174     perrorf (g, "calloc");
1175     return NULL;
1176   }
1177
1178   count = 0;
1179   for (i = 0; i < fs->nr_fstab; ++i)
1180     if (CRITERION) {
1181       ret[2*count] = safe_strdup (g, fs->fstab[i].mountpoint);
1182       ret[2*count+1] = safe_strdup (g, fs->fstab[i].device);
1183       count++;
1184     }
1185 #undef CRITERION
1186
1187   return ret;
1188 }
1189
1190 char **
1191 guestfs__inspect_get_filesystems (guestfs_h *g, const char *root)
1192 {
1193   struct inspect_fs *fs = search_for_root (g, root);
1194   if (!fs)
1195     return NULL;
1196
1197   char **ret;
1198
1199   /* If no fstab information (Windows) return just the root. */
1200   if (fs->nr_fstab == 0) {
1201     ret = calloc (2, sizeof (char *));
1202     ret[0] = safe_strdup (g, root);
1203     ret[1] = NULL;
1204     return ret;
1205   }
1206
1207   ret = calloc (fs->nr_fstab + 1, sizeof (char *));
1208   if (ret == NULL) {
1209     perrorf (g, "calloc");
1210     return NULL;
1211   }
1212
1213   size_t i;
1214   for (i = 0; i < fs->nr_fstab; ++i)
1215     ret[i] = safe_strdup (g, fs->fstab[i].device);
1216
1217   return ret;
1218 }
1219
1220 #else /* no PCRE or hivex at compile time */
1221
1222 /* XXX These functions should be in an optgroup. */
1223
1224 #define NOT_IMPL(r)                                                     \
1225   error (g, _("inspection API not available since this version of libguestfs was compiled without PCRE or hivex libraries")); \
1226   return r
1227
1228 char **
1229 guestfs__inspect_os (guestfs_h *g)
1230 {
1231   NOT_IMPL(NULL);
1232 }
1233
1234 char **
1235 guestfs__inspect_get_roots (guestfs_h *g)
1236 {
1237   NOT_IMPL(NULL);
1238 }
1239
1240 char *
1241 guestfs__inspect_get_type (guestfs_h *g, const char *root)
1242 {
1243   NOT_IMPL(NULL);
1244 }
1245
1246 char *
1247 guestfs__inspect_get_arch (guestfs_h *g, const char *root)
1248 {
1249   NOT_IMPL(NULL);
1250 }
1251
1252 char *
1253 guestfs__inspect_get_distro (guestfs_h *g, const char *root)
1254 {
1255   NOT_IMPL(NULL);
1256 }
1257
1258 int
1259 guestfs__inspect_get_major_version (guestfs_h *g, const char *root)
1260 {
1261   NOT_IMPL(-1);
1262 }
1263
1264 int
1265 guestfs__inspect_get_minor_version (guestfs_h *g, const char *root)
1266 {
1267   NOT_IMPL(-1);
1268 }
1269
1270 char *
1271 guestfs__inspect_get_product_name (guestfs_h *g, const char *root)
1272 {
1273   NOT_IMPL(NULL);
1274 }
1275
1276 char *
1277 guestfs__inspect_get_windows_systemroot (guestfs_h *g, const char *root)
1278 {
1279   NOT_IMPL(NULL);
1280 }
1281
1282 char **
1283 guestfs__inspect_get_mountpoints (guestfs_h *g, const char *root)
1284 {
1285   NOT_IMPL(NULL);
1286 }
1287
1288 char **
1289 guestfs__inspect_get_filesystems (guestfs_h *g, const char *root)
1290 {
1291   NOT_IMPL(NULL);
1292 }
1293
1294 #endif /* no PCRE or hivex at compile time */
1295
1296 void
1297 guestfs___free_inspect_info (guestfs_h *g)
1298 {
1299   size_t i;
1300   for (i = 0; i < g->nr_fses; ++i) {
1301     free (g->fses[i].device);
1302     free (g->fses[i].product_name);
1303     free (g->fses[i].arch);
1304     free (g->fses[i].windows_systemroot);
1305     size_t j;
1306     for (j = 0; j < g->fses[i].nr_fstab; ++j) {
1307       free (g->fses[i].fstab[j].device);
1308       free (g->fses[i].fstab[j].mountpoint);
1309     }
1310     free (g->fses[i].fstab);
1311   }
1312   free (g->fses);
1313   g->nr_fses = 0;
1314   g->fses = NULL;
1315 }
1316
1317 /* In the Perl code this is a public function. */
1318 int
1319 guestfs___feature_available (guestfs_h *g, const char *feature)
1320 {
1321   /* If there's an error we should ignore it, so to do that we have to
1322    * temporarily replace the error handler with a null one.
1323    */
1324   guestfs_error_handler_cb old_error_cb = g->error_cb;
1325   g->error_cb = NULL;
1326
1327   const char *groups[] = { feature, NULL };
1328   int r = guestfs_available (g, (char * const *) groups);
1329
1330   g->error_cb = old_error_cb;
1331
1332   return r == 0 ? 1 : 0;
1333 }
1334
1335 #ifdef HAVE_PCRE
1336
1337 /* Match a regular expression which contains no captures.  Returns
1338  * true if it matches or false if it doesn't.
1339  */
1340 int
1341 guestfs___match (guestfs_h *g, const char *str, const pcre *re)
1342 {
1343   size_t len = strlen (str);
1344   int vec[30], r;
1345
1346   r = pcre_exec (re, NULL, str, len, 0, 0, vec, sizeof vec / sizeof vec[0]);
1347   if (r == PCRE_ERROR_NOMATCH)
1348     return 0;
1349   if (r != 1) {
1350     /* Internal error -- should not happen. */
1351     fprintf (stderr, "libguestfs: %s: %s: internal error: pcre_exec returned unexpected error code %d when matching against the string \"%s\"\n",
1352              __FILE__, __func__, r, str);
1353     return 0;
1354   }
1355
1356   return 1;
1357 }
1358
1359 /* Match a regular expression which contains exactly one capture.  If
1360  * the string matches, return the capture, otherwise return NULL.  The
1361  * caller must free the result.
1362  */
1363 char *
1364 guestfs___match1 (guestfs_h *g, const char *str, const pcre *re)
1365 {
1366   size_t len = strlen (str);
1367   int vec[30], r;
1368
1369   r = pcre_exec (re, NULL, str, len, 0, 0, vec, sizeof vec / sizeof vec[0]);
1370   if (r == PCRE_ERROR_NOMATCH)
1371     return NULL;
1372   if (r != 2) {
1373     /* Internal error -- should not happen. */
1374     fprintf (stderr, "libguestfs: %s: %s: internal error: pcre_exec returned unexpected error code %d when matching against the string \"%s\"\n",
1375              __FILE__, __func__, r, str);
1376     return NULL;
1377   }
1378
1379   return safe_strndup (g, &str[vec[2]], vec[3]-vec[2]);
1380 }
1381
1382 /* Match a regular expression which contains exactly two captures. */
1383 int
1384 guestfs___match2 (guestfs_h *g, const char *str, const pcre *re,
1385                   char **ret1, char **ret2)
1386 {
1387   size_t len = strlen (str);
1388   int vec[30], r;
1389
1390   r = pcre_exec (re, NULL, str, len, 0, 0, vec, 30);
1391   if (r == PCRE_ERROR_NOMATCH)
1392     return 0;
1393   if (r != 3) {
1394     /* Internal error -- should not happen. */
1395     fprintf (stderr, "libguestfs: %s: %s: internal error: pcre_exec returned unexpected error code %d when matching against the string \"%s\"\n",
1396              __FILE__, __func__, r, str);
1397     return 0;
1398   }
1399
1400   *ret1 = safe_strndup (g, &str[vec[2]], vec[3]-vec[2]);
1401   *ret2 = safe_strndup (g, &str[vec[4]], vec[5]-vec[4]);
1402
1403   return 1;
1404 }
1405
1406 /* Match a regular expression which contains exactly three captures. */
1407 int
1408 guestfs___match3 (guestfs_h *g, const char *str, const pcre *re,
1409                   char **ret1, char **ret2, char **ret3)
1410 {
1411   size_t len = strlen (str);
1412   int vec[30], r;
1413
1414   r = pcre_exec (re, NULL, str, len, 0, 0, vec, 30);
1415   if (r == PCRE_ERROR_NOMATCH)
1416     return 0;
1417   if (r != 4) {
1418     /* Internal error -- should not happen. */
1419     fprintf (stderr, "libguestfs: %s: %s: internal error: pcre_exec returned unexpected error code %d when matching against the string \"%s\"\n",
1420              __FILE__, __func__, r, str);
1421     return 0;
1422   }
1423
1424   *ret1 = safe_strndup (g, &str[vec[2]], vec[3]-vec[2]);
1425   *ret2 = safe_strndup (g, &str[vec[4]], vec[5]-vec[4]);
1426   *ret3 = safe_strndup (g, &str[vec[6]], vec[7]-vec[6]);
1427
1428   return 1;
1429 }
1430
1431 #endif /* HAVE_PCRE */