inspection: Add outline support for GNU/Hurd.
[libguestfs.git] / src / inspect_fs_unix.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 /* Compile all the regular expressions once when the shared library is
50  * loaded.  PCRE is thread safe so we're supposedly OK here if
51  * multiple threads call into the libguestfs API functions below
52  * simultaneously.
53  */
54 static pcre *re_fedora;
55 static pcre *re_rhel_old;
56 static pcre *re_rhel;
57 static pcre *re_rhel_no_minor;
58 static pcre *re_centos_old;
59 static pcre *re_centos;
60 static pcre *re_centos_no_minor;
61 static pcre *re_scientific_linux_old;
62 static pcre *re_scientific_linux;
63 static pcre *re_scientific_linux_no_minor;
64 static pcre *re_major_minor;
65 static pcre *re_aug_seq;
66 static pcre *re_xdev;
67 static pcre *re_cciss;
68 static pcre *re_first_partition;
69 static pcre *re_freebsd;
70 static pcre *re_netbsd;
71
72 static void compile_regexps (void) __attribute__((constructor));
73 static void free_regexps (void) __attribute__((destructor));
74
75 static void
76 compile_regexps (void)
77 {
78   const char *err;
79   int offset;
80
81 #define COMPILE(re,pattern,options)                                     \
82   do {                                                                  \
83     re = pcre_compile ((pattern), (options), &err, &offset, NULL);      \
84     if (re == NULL) {                                                   \
85       ignore_value (write (2, err, strlen (err)));                      \
86       abort ();                                                         \
87     }                                                                   \
88   } while (0)
89
90   COMPILE (re_fedora, "Fedora release (\\d+)", 0);
91   COMPILE (re_rhel_old,
92            "Red Hat.*release (\\d+).*Update (\\d+)", 0);
93   COMPILE (re_rhel,
94            "Red Hat.*release (\\d+)\\.(\\d+)", 0);
95   COMPILE (re_rhel_no_minor,
96            "Red Hat.*release (\\d+)", 0);
97   COMPILE (re_centos_old,
98            "CentOS.*release (\\d+).*Update (\\d+)", 0);
99   COMPILE (re_centos,
100            "CentOS.*release (\\d+)\\.(\\d+)", 0);
101   COMPILE (re_centos_no_minor,
102            "CentOS.*release (\\d+)", 0);
103   COMPILE (re_scientific_linux_old,
104            "Scientific Linux.*release (\\d+).*Update (\\d+)", 0);
105   COMPILE (re_scientific_linux,
106            "Scientific Linux.*release (\\d+)\\.(\\d+)", 0);
107   COMPILE (re_scientific_linux_no_minor,
108            "Scientific Linux.*release (\\d+)", 0);
109   COMPILE (re_major_minor, "(\\d+)\\.(\\d+)", 0);
110   COMPILE (re_aug_seq, "/\\d+$", 0);
111   COMPILE (re_xdev, "^/dev/(h|s|v|xv)d([a-z]+)(\\d*)$", 0);
112   COMPILE (re_cciss, "^/dev/(cciss/c\\d+d\\d+)(?:p(\\d+))?$", 0);
113   COMPILE (re_freebsd, "^/dev/ad(\\d+)s(\\d+)([a-z])$", 0);
114   COMPILE (re_netbsd, "^NetBSD (\\d+)\\.(\\d+)", 0);
115 }
116
117 static void
118 free_regexps (void)
119 {
120   pcre_free (re_fedora);
121   pcre_free (re_rhel_old);
122   pcre_free (re_rhel);
123   pcre_free (re_rhel_no_minor);
124   pcre_free (re_centos_old);
125   pcre_free (re_centos);
126   pcre_free (re_centos_no_minor);
127   pcre_free (re_scientific_linux_old);
128   pcre_free (re_scientific_linux);
129   pcre_free (re_scientific_linux_no_minor);
130   pcre_free (re_major_minor);
131   pcre_free (re_aug_seq);
132   pcre_free (re_xdev);
133   pcre_free (re_cciss);
134   pcre_free (re_freebsd);
135   pcre_free (re_netbsd);
136 }
137
138 static void check_architecture (guestfs_h *g, struct inspect_fs *fs);
139 static int check_hostname_unix (guestfs_h *g, struct inspect_fs *fs);
140 static int check_hostname_redhat (guestfs_h *g, struct inspect_fs *fs);
141 static int check_hostname_freebsd (guestfs_h *g, struct inspect_fs *fs);
142 static int check_fstab (guestfs_h *g, struct inspect_fs *fs);
143 static int add_fstab_entry (guestfs_h *g, struct inspect_fs *fs,
144                             const char *spec, const char *mp);
145 static char *resolve_fstab_device (guestfs_h *g, const char *spec);
146 static int inspect_with_augeas (guestfs_h *g, struct inspect_fs *fs, const char **configfiles, int (*f) (guestfs_h *, struct inspect_fs *));
147
148 /* Set fs->product_name to the first line of the release file. */
149 static int
150 parse_release_file (guestfs_h *g, struct inspect_fs *fs,
151                     const char *release_filename)
152 {
153   fs->product_name = guestfs___first_line_of_file (g, release_filename);
154   if (fs->product_name == NULL)
155     return -1;
156   return 0;
157 }
158
159 /* Ubuntu has /etc/lsb-release containing:
160  *   DISTRIB_ID=Ubuntu                                # Distro
161  *   DISTRIB_RELEASE=10.04                            # Version
162  *   DISTRIB_CODENAME=lucid
163  *   DISTRIB_DESCRIPTION="Ubuntu 10.04.1 LTS"         # Product name
164  *
165  * [Ubuntu-derived ...] Linux Mint was found to have this:
166  *   DISTRIB_ID=LinuxMint
167  *   DISTRIB_RELEASE=10
168  *   DISTRIB_CODENAME=julia
169  *   DISTRIB_DESCRIPTION="Linux Mint 10 Julia"
170  * Linux Mint also has /etc/linuxmint/info with more information,
171  * but we can use the LSB file.
172  *
173  * Mandriva has:
174  *   LSB_VERSION=lsb-4.0-amd64:lsb-4.0-noarch
175  *   DISTRIB_ID=MandrivaLinux
176  *   DISTRIB_RELEASE=2010.1
177  *   DISTRIB_CODENAME=Henry_Farman
178  *   DISTRIB_DESCRIPTION="Mandriva Linux 2010.1"
179  * Mandriva also has a normal release file called /etc/mandriva-release.
180  */
181 static int
182 parse_lsb_release (guestfs_h *g, struct inspect_fs *fs)
183 {
184   const char *filename = "/etc/lsb-release";
185   int64_t size;
186   char **lines;
187   size_t i;
188   int r = 0;
189
190   /* Don't trust guestfs_head_n not to break with very large files.
191    * Check the file size is something reasonable first.
192    */
193   size = guestfs_filesize (g, filename);
194   if (size == -1)
195     /* guestfs_filesize failed and has already set error in handle */
196     return -1;
197   if (size > MAX_SMALL_FILE_SIZE) {
198     error (g, _("size of %s is unreasonably large (%" PRIi64 " bytes)"),
199            filename, size);
200     return -1;
201   }
202
203   lines = guestfs_head_n (g, 10, filename);
204   if (lines == NULL)
205     return -1;
206
207   for (i = 0; lines[i] != NULL; ++i) {
208     if (fs->distro == 0 &&
209         STREQ (lines[i], "DISTRIB_ID=Ubuntu")) {
210       fs->distro = OS_DISTRO_UBUNTU;
211       r = 1;
212     }
213     else if (fs->distro == 0 &&
214              STREQ (lines[i], "DISTRIB_ID=LinuxMint")) {
215       fs->distro = OS_DISTRO_LINUX_MINT;
216       r = 1;
217     }
218     else if (fs->distro == 0 &&
219              STREQ (lines[i], "DISTRIB_ID=MandrivaLinux")) {
220       fs->distro = OS_DISTRO_MANDRIVA;
221       r = 1;
222     }
223     else if (fs->distro == 0 &&
224              STREQ (lines[i], "DISTRIB_ID=\"Mageia\"")) {
225       fs->distro = OS_DISTRO_MAGEIA;
226       r = 1;
227     }
228     else if (STRPREFIX (lines[i], "DISTRIB_RELEASE=")) {
229       char *major, *minor;
230       if (match2 (g, &lines[i][16], re_major_minor, &major, &minor)) {
231         fs->major_version = guestfs___parse_unsigned_int (g, major);
232         free (major);
233         if (fs->major_version == -1) {
234           free (minor);
235           guestfs___free_string_list (lines);
236           return -1;
237         }
238         fs->minor_version = guestfs___parse_unsigned_int (g, minor);
239         free (minor);
240         if (fs->minor_version == -1) {
241           guestfs___free_string_list (lines);
242           return -1;
243         }
244       }
245     }
246     else if (fs->product_name == NULL &&
247              (STRPREFIX (lines[i], "DISTRIB_DESCRIPTION=\"") ||
248               STRPREFIX (lines[i], "DISTRIB_DESCRIPTION='"))) {
249       size_t len = strlen (lines[i]) - 21 - 1;
250       fs->product_name = safe_strndup (g, &lines[i][21], len);
251       r = 1;
252     }
253     else if (fs->product_name == NULL &&
254              STRPREFIX (lines[i], "DISTRIB_DESCRIPTION=")) {
255       size_t len = strlen (lines[i]) - 20;
256       fs->product_name = safe_strndup (g, &lines[i][20], len);
257       r = 1;
258     }
259   }
260
261   guestfs___free_string_list (lines);
262   return r;
263 }
264
265 /* The currently mounted device is known to be a Linux root.  Try to
266  * determine from this the distro, version, etc.  Also parse
267  * /etc/fstab to determine the arrangement of mountpoints and
268  * associated devices.
269  */
270 int
271 guestfs___check_linux_root (guestfs_h *g, struct inspect_fs *fs)
272 {
273   int r;
274
275   fs->type = OS_TYPE_LINUX;
276
277   if (guestfs_exists (g, "/etc/lsb-release") > 0) {
278     r = parse_lsb_release (g, fs);
279     if (r == -1)        /* error */
280       return -1;
281     if (r == 1)         /* ok - detected the release from this file */
282       goto skip_release_checks;
283   }
284
285   if (guestfs_exists (g, "/etc/redhat-release") > 0) {
286     fs->distro = OS_DISTRO_REDHAT_BASED; /* Something generic Red Hat-like. */
287
288     if (parse_release_file (g, fs, "/etc/redhat-release") == -1)
289       return -1;
290
291     char *major, *minor;
292     if ((major = match1 (g, fs->product_name, re_fedora)) != NULL) {
293       fs->distro = OS_DISTRO_FEDORA;
294       fs->major_version = guestfs___parse_unsigned_int (g, major);
295       free (major);
296       if (fs->major_version == -1)
297         return -1;
298     }
299     else if (match2 (g, fs->product_name, re_rhel_old, &major, &minor) ||
300              match2 (g, fs->product_name, re_rhel, &major, &minor)) {
301       fs->distro = OS_DISTRO_RHEL;
302       fs->major_version = guestfs___parse_unsigned_int (g, major);
303       free (major);
304       if (fs->major_version == -1) {
305         free (minor);
306         return -1;
307       }
308       fs->minor_version = guestfs___parse_unsigned_int (g, minor);
309       free (minor);
310       if (fs->minor_version == -1)
311         return -1;
312     }
313     else if ((major = match1 (g, fs->product_name, re_rhel_no_minor)) != NULL) {
314       fs->distro = OS_DISTRO_RHEL;
315       fs->major_version = guestfs___parse_unsigned_int (g, major);
316       free (major);
317       if (fs->major_version == -1)
318         return -1;
319       fs->minor_version = 0;
320     }
321     else if (match2 (g, fs->product_name, re_centos_old, &major, &minor) ||
322              match2 (g, fs->product_name, re_centos, &major, &minor)) {
323       fs->distro = OS_DISTRO_CENTOS;
324       fs->major_version = guestfs___parse_unsigned_int (g, major);
325       free (major);
326       if (fs->major_version == -1) {
327         free (minor);
328         return -1;
329       }
330       fs->minor_version = guestfs___parse_unsigned_int (g, minor);
331       free (minor);
332       if (fs->minor_version == -1)
333         return -1;
334     }
335     else if ((major = match1 (g, fs->product_name, re_centos_no_minor)) != NULL) {
336       fs->distro = OS_DISTRO_CENTOS;
337       fs->major_version = guestfs___parse_unsigned_int (g, major);
338       free (major);
339       if (fs->major_version == -1)
340         return -1;
341       fs->minor_version = 0;
342     }
343     else if (match2 (g, fs->product_name, re_scientific_linux_old, &major, &minor) ||
344              match2 (g, fs->product_name, re_scientific_linux, &major, &minor)) {
345       fs->distro = OS_DISTRO_SCIENTIFIC_LINUX;
346       fs->major_version = guestfs___parse_unsigned_int (g, major);
347       free (major);
348       if (fs->major_version == -1) {
349         free (minor);
350         return -1;
351       }
352       fs->minor_version = guestfs___parse_unsigned_int (g, minor);
353       free (minor);
354       if (fs->minor_version == -1)
355         return -1;
356     }
357     else if ((major = match1 (g, fs->product_name, re_scientific_linux_no_minor)) != NULL) {
358       fs->distro = OS_DISTRO_SCIENTIFIC_LINUX;
359       fs->major_version = guestfs___parse_unsigned_int (g, major);
360       free (major);
361       if (fs->major_version == -1)
362         return -1;
363       fs->minor_version = 0;
364     }
365   }
366   else if (guestfs_exists (g, "/etc/debian_version") > 0) {
367     fs->distro = OS_DISTRO_DEBIAN;
368
369     if (parse_release_file (g, fs, "/etc/debian_version") == -1)
370       return -1;
371
372     if (guestfs___parse_major_minor (g, fs) == -1)
373       return -1;
374   }
375   else if (guestfs_exists (g, "/etc/pardus-release") > 0) {
376     fs->distro = OS_DISTRO_PARDUS;
377
378     if (parse_release_file (g, fs, "/etc/pardus-release") == -1)
379       return -1;
380
381     if (guestfs___parse_major_minor (g, fs) == -1)
382       return -1;
383   }
384   else if (guestfs_exists (g, "/etc/arch-release") > 0) {
385     fs->distro = OS_DISTRO_ARCHLINUX;
386
387     /* /etc/arch-release file is empty and I can't see a way to
388      * determine the actual release or product string.
389      */
390   }
391   else if (guestfs_exists (g, "/etc/gentoo-release") > 0) {
392     fs->distro = OS_DISTRO_GENTOO;
393
394     if (parse_release_file (g, fs, "/etc/gentoo-release") == -1)
395       return -1;
396
397     if (guestfs___parse_major_minor (g, fs) == -1)
398       return -1;
399   }
400   else if (guestfs_exists (g, "/etc/meego-release") > 0) {
401     fs->distro = OS_DISTRO_MEEGO;
402
403     if (parse_release_file (g, fs, "/etc/meego-release") == -1)
404       return -1;
405
406     if (guestfs___parse_major_minor (g, fs) == -1)
407       return -1;
408   }
409   else if (guestfs_exists (g, "/etc/slackware-version") > 0) {
410     fs->distro = OS_DISTRO_SLACKWARE;
411
412     if (parse_release_file (g, fs, "/etc/slackware-version") == -1)
413       return -1;
414
415     if (guestfs___parse_major_minor (g, fs) == -1)
416       return -1;
417   }
418   else if (guestfs_exists (g, "/etc/ttylinux-target") > 0) {
419     fs->distro = OS_DISTRO_TTYLINUX;
420
421     fs->product_name = guestfs___first_line_of_file (g, "/etc/ttylinux-target");
422     if (fs->product_name == NULL)
423       return -1;
424
425     if (guestfs___parse_major_minor (g, fs) == -1)
426       return -1;
427   }
428   else if (guestfs_exists (g, "/etc/SuSE-release") > 0) {
429     fs->distro = OS_DISTRO_OPENSUSE;
430
431     if (parse_release_file (g, fs, "/etc/SuSE-release") == -1)
432       return -1;
433
434     if (guestfs___parse_major_minor (g, fs) == -1)
435       return -1;
436   }
437
438
439  skip_release_checks:;
440
441   /* Determine the architecture. */
442   check_architecture (g, fs);
443
444   /* We already know /etc/fstab exists because it's part of the test
445    * for Linux root above.  We must now parse this file to determine
446    * which filesystems are used by the operating system and how they
447    * are mounted.
448    */
449   const char *configfiles[] = { "/etc/fstab", NULL };
450   if (inspect_with_augeas (g, fs, configfiles, check_fstab) == -1)
451     return -1;
452
453   /* Determine hostname. */
454   if (check_hostname_unix (g, fs) == -1)
455     return -1;
456
457   return 0;
458 }
459
460 /* The currently mounted device is known to be a FreeBSD root. */
461 int
462 guestfs___check_freebsd_root (guestfs_h *g, struct inspect_fs *fs)
463 {
464   fs->type = OS_TYPE_FREEBSD;
465
466   /* FreeBSD has no authoritative version file.  The version number is
467    * in /etc/motd, which the system administrator might edit, but
468    * we'll use that anyway.
469    */
470
471   if (guestfs_exists (g, "/etc/motd") > 0) {
472     if (parse_release_file (g, fs, "/etc/motd") == -1)
473       return -1;
474
475     if (guestfs___parse_major_minor (g, fs) == -1)
476       return -1;
477   }
478
479   /* Determine the architecture. */
480   check_architecture (g, fs);
481
482   /* We already know /etc/fstab exists because it's part of the test above. */
483   const char *configfiles[] = { "/etc/fstab", NULL };
484   if (inspect_with_augeas (g, fs, configfiles, check_fstab) == -1)
485     return -1;
486
487   /* Determine hostname. */
488   if (check_hostname_unix (g, fs) == -1)
489     return -1;
490
491   return 0;
492 }
493
494 /* The currently mounted device is maybe to be a *BSD root. */
495 int
496 guestfs___check_netbsd_root (guestfs_h *g, struct inspect_fs *fs)
497 {
498
499   if (guestfs_exists (g, "/etc/release") > 0) {
500     char *major, *minor;
501     if (parse_release_file (g, fs, "/etc/release") == -1)
502       return -1;
503
504     if (match2 (g, fs->product_name, re_netbsd, &major, &minor)) {
505       fs->type = OS_TYPE_NETBSD;
506       fs->major_version = guestfs___parse_unsigned_int (g, major);
507       free (major);
508       if (fs->major_version == -1) {
509         free (minor);
510         return -1;
511       }
512       fs->minor_version = guestfs___parse_unsigned_int (g, minor);
513       free (minor);
514       if (fs->minor_version == -1)
515         return -1;
516     }
517   } else {
518     return -1;
519   }
520
521   /* Determine the architecture. */
522   check_architecture (g, fs);
523
524   /* We already know /etc/fstab exists because it's part of the test above. */
525   const char *configfiles[] = { "/etc/fstab", NULL };
526   if (inspect_with_augeas (g, fs, configfiles, check_fstab) == -1)
527     return -1;
528
529   /* Determine hostname. */
530   if (check_hostname_unix (g, fs) == -1)
531     return -1;
532
533   return 0;
534 }
535
536 /* The currently mounted device may be a Hurd root.  Hurd has distros
537  * just like Linux.
538  */
539 int
540 guestfs___check_hurd_root (guestfs_h *g, struct inspect_fs *fs)
541 {
542   int r;
543
544   fs->type = OS_TYPE_HURD;
545
546   if (guestfs_exists (g, "/etc/debian_version") > 0) {
547     fs->distro = OS_DISTRO_DEBIAN;
548
549     if (parse_release_file (g, fs, "/etc/debian_version") == -1)
550       return -1;
551
552     if (guestfs___parse_major_minor (g, fs) == -1)
553       return -1;
554   }
555
556   /* Arch Hurd also exists, but inconveniently it doesn't have
557    * the normal /etc/arch-release file.  XXX
558    */
559
560   /* Determine the architecture. */
561   check_architecture (g, fs);
562
563   /* XXX Check for /etc/fstab. */
564
565   /* Determine hostname. */
566   if (check_hostname_unix (g, fs) == -1)
567     return -1;
568
569   return 0;
570 }
571
572 static void
573 check_architecture (guestfs_h *g, struct inspect_fs *fs)
574 {
575   const char *binaries[] =
576     { "/bin/bash", "/bin/ls", "/bin/echo", "/bin/rm", "/bin/sh" };
577   size_t i;
578
579   for (i = 0; i < sizeof binaries / sizeof binaries[0]; ++i) {
580     if (guestfs_is_file (g, binaries[i]) > 0) {
581       /* Ignore errors from file_architecture call. */
582       guestfs_error_handler_cb old_error_cb = g->error_cb;
583       g->error_cb = NULL;
584       char *arch = guestfs_file_architecture (g, binaries[i]);
585       g->error_cb = old_error_cb;
586
587       if (arch) {
588         /* String will be owned by handle, freed by
589          * guestfs___free_inspect_info.
590          */
591         fs->arch = arch;
592         break;
593       }
594     }
595   }
596 }
597
598 /* Try several methods to determine the hostname from a Linux or
599  * FreeBSD guest.  Note that type and distro have been set, so we can
600  * use that information to direct the search.
601  */
602 static int
603 check_hostname_unix (guestfs_h *g, struct inspect_fs *fs)
604 {
605   switch (fs->type) {
606   case OS_TYPE_LINUX:
607   case OS_TYPE_HURD:
608     /* Red Hat-derived would be in /etc/sysconfig/network, and
609      * Debian-derived in the file /etc/hostname.  Very old Debian and
610      * SUSE use /etc/HOSTNAME.  It's best to just look for each of
611      * these files in turn, rather than try anything clever based on
612      * distro.
613      */
614     if (guestfs_is_file (g, "/etc/HOSTNAME")) {
615       fs->hostname = guestfs___first_line_of_file (g, "/etc/HOSTNAME");
616       if (fs->hostname == NULL)
617         return -1;
618     }
619     else if (guestfs_is_file (g, "/etc/hostname")) {
620       fs->hostname = guestfs___first_line_of_file (g, "/etc/hostname");
621       if (fs->hostname == NULL)
622         return -1;
623     }
624     else if (guestfs_is_file (g, "/etc/sysconfig/network")) {
625       const char *configfiles[] = { "/etc/sysconfig/network", NULL };
626       if (inspect_with_augeas (g, fs, configfiles,
627                                check_hostname_redhat) == -1)
628         return -1;
629     }
630     break;
631
632   case OS_TYPE_FREEBSD:
633   case OS_TYPE_NETBSD:
634     /* /etc/rc.conf contains the hostname, but there is no Augeas lens
635      * for this file.
636      */
637     if (guestfs_is_file (g, "/etc/rc.conf")) {
638       if (check_hostname_freebsd (g, fs) == -1)
639         return -1;
640     }
641     break;
642
643   case OS_TYPE_WINDOWS: /* not here, see check_windows_system_registry */
644   case OS_TYPE_UNKNOWN:
645   default:
646     /* nothing, keep GCC warnings happy */;
647   }
648
649   return 0;
650 }
651
652 /* Parse the hostname from /etc/sysconfig/network.  This must be called
653  * from the inspect_with_augeas wrapper.
654  */
655 static int
656 check_hostname_redhat (guestfs_h *g, struct inspect_fs *fs)
657 {
658   char *hostname;
659
660   /* Errors here are not fatal (RHBZ#726739), since it could be
661    * just missing HOSTNAME field in the file.
662    */
663   guestfs_error_handler_cb old_error_cb = g->error_cb;
664   g->error_cb = NULL;
665   hostname = guestfs_aug_get (g, "/files/etc/sysconfig/network/HOSTNAME");
666   g->error_cb = old_error_cb;
667
668   /* This is freed by guestfs___free_inspect_info.  Note that hostname
669    * could be NULL because we ignored errors above.
670    */
671   fs->hostname = hostname;
672   return 0;
673 }
674
675 /* Parse the hostname from /etc/rc.conf.  On FreeBSD this file
676  * contains comments, blank lines and:
677  *   hostname="freebsd8.example.com"
678  *   ifconfig_re0="DHCP"
679  *   keymap="uk.iso"
680  *   sshd_enable="YES"
681  */
682 static int
683 check_hostname_freebsd (guestfs_h *g, struct inspect_fs *fs)
684 {
685   const char *filename = "/etc/rc.conf";
686   int64_t size;
687   char **lines;
688   size_t i;
689
690   /* Don't trust guestfs_read_lines not to break with very large files.
691    * Check the file size is something reasonable first.
692    */
693   size = guestfs_filesize (g, filename);
694   if (size == -1)
695     /* guestfs_filesize failed and has already set error in handle */
696     return -1;
697   if (size > MAX_SMALL_FILE_SIZE) {
698     error (g, _("size of %s is unreasonably large (%" PRIi64 " bytes)"),
699            filename, size);
700     return -1;
701   }
702
703   lines = guestfs_read_lines (g, filename);
704   if (lines == NULL)
705     return -1;
706
707   for (i = 0; lines[i] != NULL; ++i) {
708     if (STRPREFIX (lines[i], "hostname=\"") ||
709         STRPREFIX (lines[i], "hostname='")) {
710       size_t len = strlen (lines[i]) - 10 - 1;
711       fs->hostname = safe_strndup (g, &lines[i][10], len);
712       break;
713     } else if (STRPREFIX (lines[i], "hostname=")) {
714       size_t len = strlen (lines[i]) - 9;
715       fs->hostname = safe_strndup (g, &lines[i][9], len);
716       break;
717     }
718   }
719
720   guestfs___free_string_list (lines);
721   return 0;
722 }
723
724 static int
725 check_fstab (guestfs_h *g, struct inspect_fs *fs)
726 {
727   char **lines = guestfs_aug_ls (g, "/files/etc/fstab");
728   if (lines == NULL) goto error;
729
730   if (lines[0] == NULL) {
731     error (g, _("could not parse /etc/fstab or empty file"));
732     goto error;
733   }
734
735   size_t i;
736   char augpath[256];
737   for (i = 0; lines[i] != NULL; ++i) {
738     /* Ignore comments.  Only care about sequence lines which
739      * match m{/\d+$}.
740      */
741     if (match (g, lines[i], re_aug_seq)) {
742       snprintf (augpath, sizeof augpath, "%s/spec", lines[i]);
743       char *spec = guestfs_aug_get (g, augpath);
744       if (spec == NULL) goto error;
745
746       snprintf (augpath, sizeof augpath, "%s/file", lines[i]);
747       char *mp = guestfs_aug_get (g, augpath);
748       if (mp == NULL) {
749         free (spec);
750         goto error;
751       }
752
753       int r = add_fstab_entry (g, fs, spec, mp);
754       free (spec);
755       free (mp);
756
757       if (r == -1) goto error;
758     }
759   }
760
761   guestfs___free_string_list (lines);
762   return 0;
763
764 error:
765   if (lines) guestfs___free_string_list (lines);
766   return -1;
767 }
768
769 /* Add a filesystem and possibly a mountpoint entry for
770  * the root filesystem 'fs'.
771  *
772  * 'spec' is the fstab spec field, which might be a device name or a
773  * pseudodevice or 'UUID=...' or 'LABEL=...'.
774  *
775  * 'mp' is the mount point, which could also be 'swap' or 'none'.
776  */
777 static int
778 add_fstab_entry (guestfs_h *g, struct inspect_fs *fs,
779                  const char *spec, const char *mp)
780 {
781   /* Ignore certain mountpoints. */
782   if (STRPREFIX (mp, "/dev/") ||
783       STREQ (mp, "/dev") ||
784       STRPREFIX (mp, "/media/") ||
785       STRPREFIX (mp, "/proc/") ||
786       STREQ (mp, "/proc") ||
787       STRPREFIX (mp, "/selinux/") ||
788       STREQ (mp, "/selinux") ||
789       STRPREFIX (mp, "/sys/") ||
790       STREQ (mp, "/sys"))
791     return 0;
792
793   /* Ignore /dev/fd (floppy disks) (RHBZ#642929) and CD-ROM drives. */
794   if ((STRPREFIX (spec, "/dev/fd") && c_isdigit (spec[7])) ||
795       STREQ (spec, "/dev/floppy") ||
796       STREQ (spec, "/dev/cdrom"))
797     return 0;
798
799   /* Resolve UUID= and LABEL= to the actual device. */
800   char *device = NULL;
801   if (STRPREFIX (spec, "UUID="))
802     device = guestfs_findfs_uuid (g, &spec[5]);
803   else if (STRPREFIX (spec, "LABEL="))
804     device = guestfs_findfs_label (g, &spec[6]);
805   /* Ignore "/.swap" (Pardus) and pseudo-devices like "tmpfs". */
806   else if (STREQ (spec, "/dev/root"))
807     /* Resolve /dev/root to the current device. */
808     device = safe_strdup (g, fs->device);
809   else if (STRPREFIX (spec, "/dev/"))
810     /* Resolve guest block device names. */
811     device = resolve_fstab_device (g, spec);
812
813   /* If we haven't resolved the device successfully by this point,
814    * we don't care, just ignore it.
815    */
816   if (device == NULL)
817     return 0;
818
819   char *mountpoint = safe_strdup (g, mp);
820
821   /* Add this to the fstab entry in 'fs'.
822    * Note these are further filtered by guestfs_inspect_get_mountpoints
823    * and guestfs_inspect_get_filesystems.
824    */
825   size_t n = fs->nr_fstab + 1;
826   struct inspect_fstab_entry *p;
827
828   p = realloc (fs->fstab, n * sizeof (struct inspect_fstab_entry));
829   if (p == NULL) {
830     perrorf (g, "realloc");
831     free (device);
832     free (mountpoint);
833     return -1;
834   }
835
836   fs->fstab = p;
837   fs->nr_fstab = n;
838
839   /* These are owned by the handle and freed by guestfs___free_inspect_info. */
840   fs->fstab[n-1].device = device;
841   fs->fstab[n-1].mountpoint = mountpoint;
842
843   debug (g, "fstab: device=%s mountpoint=%s", device, mountpoint);
844
845   return 0;
846 }
847
848 /* Resolve block device name to the libguestfs device name, eg.
849  * /dev/xvdb1 => /dev/vdb1; and /dev/mapper/VG-LV => /dev/VG/LV.  This
850  * assumes that disks were added in the same order as they appear to
851  * the real VM, which is a reasonable assumption to make.  Return
852  * anything we don't recognize unchanged.
853  */
854 static char *
855 resolve_fstab_device (guestfs_h *g, const char *spec)
856 {
857   char *device = NULL;
858   char *type, *slice, *disk, *part;
859
860   if (STRPREFIX (spec, "/dev/mapper/")) {
861     /* LVM2 does some strange munging on /dev/mapper paths for VGs and
862      * LVs which contain '-' character:
863      *
864      * ><fs> lvcreate LV--test VG--test 32
865      * ><fs> debug ls /dev/mapper
866      * VG----test-LV----test
867      *
868      * This makes it impossible to reverse those paths directly, so
869      * we have implemented lvm_canonical_lv_name in the daemon.
870      */
871     device = guestfs_lvm_canonical_lv_name (g, spec);
872   }
873   else if (match3 (g, spec, re_xdev, &type, &disk, &part)) {
874     /* type: (h|s|v|xv)
875      * disk: ([a-z]+)
876      * part: (\d*) */
877     char **devices = guestfs_list_devices (g);
878     if (devices == NULL)
879       return NULL;
880
881     /* Check any hints we were passed for a non-heuristic mapping */
882     char *name = safe_asprintf (g, "%sd%s", type, disk);
883     size_t i = 0;
884     struct drive *drive = g->drives;
885     while (drive) {
886       if (drive->name && STREQ(drive->name, name)) {
887         device = safe_asprintf (g, "%s%s", devices[i], part);
888         break;
889       }
890
891       i++; drive = drive->next;
892     }
893     free (name);
894
895     /* Guess the appliance device name if we didn't find a matching hint */
896     if (!device) {
897       /* Count how many disks the libguestfs appliance has */
898       size_t count;
899       for (count = 0; devices[count] != NULL; count++)
900         ;
901
902       /* Calculate the numerical index of the disk */
903       i = disk[0] - 'a';
904       for (char *p = disk + 1; *p != '\0'; p++) {
905         i += 1; i *= 26;
906         i += *p - 'a';
907       }
908
909       /* Check the index makes sense wrt the number of disks the appliance has.
910        * If it does, map it to an appliance disk. */
911       if (i < count) {
912         device = safe_asprintf (g, "%s%s", devices[i], part);
913       }
914     }
915
916     free (type);
917     free (disk);
918     free (part);
919     guestfs___free_string_list (devices);
920   }
921   else if (match2 (g, spec, re_cciss, &disk, &part)) {
922     /* disk: (cciss/c\d+d\d+)
923      * part: (\d+)? */
924     char **devices = guestfs_list_devices (g);
925     if (devices == NULL)
926       return NULL;
927
928     /* Check any hints we were passed for a non-heuristic mapping */
929     size_t i = 0;
930     struct drive *drive = g->drives;
931     while (drive) {
932       if (drive->name && STREQ(drive->name, disk)) {
933         if (part) {
934           device = safe_asprintf (g, "%s%s", devices[i], part);
935         } else {
936           device = safe_strdup (g, devices[i]);
937         }
938         break;
939       }
940
941       i++; drive = drive->next;
942     }
943
944     /* We don't try to guess mappings for cciss devices */
945
946     free (disk);
947     free (part);
948     guestfs___free_string_list (devices);
949   }
950   else if (match3 (g, spec, re_freebsd, &disk, &slice, &part)) {
951     /* FreeBSD disks are organized quite differently.  See:
952      * http://www.freebsd.org/doc/handbook/disk-organization.html
953      * FreeBSD "partitions" are exposed as quasi-extended partitions
954      * numbered from 5 in Linux.  I have no idea what happens when you
955      * have multiple "slices" (the FreeBSD term for MBR partitions).
956      */
957     int disk_i = guestfs___parse_unsigned_int (g, disk);
958     int slice_i = guestfs___parse_unsigned_int (g, slice);
959     int part_i = part[0] - 'a' /* counting from 0 */;
960     free (disk);
961     free (slice);
962     free (part);
963
964     if (disk_i != -1 && disk_i <= 26 &&
965         slice_i > 0 && slice_i <= 1 /* > 4 .. see comment above */ &&
966         part_i >= 0 && part_i < 26) {
967       device = safe_asprintf (g, "/dev/sd%c%d", disk_i + 'a', part_i + 5);
968     }
969   }
970
971   /* Didn't match device pattern, return original spec unchanged. */
972   if (device == NULL)
973     device = safe_strdup (g, spec);
974
975   return device;
976 }
977
978 /* Call 'f' with Augeas opened and having parsed 'filename' (this file
979  * must exist).  As a security measure, this bails if the file is too
980  * large for a reasonable configuration file.  After the call to 'f'
981  * Augeas is closed.
982  */
983 static int
984 inspect_with_augeas (guestfs_h *g, struct inspect_fs *fs,
985                      const char **configfiles,
986                      int (*f) (guestfs_h *, struct inspect_fs *))
987 {
988   /* Security: Refuse to do this if a config file is too large. */
989   for (const char **i = configfiles; *i != NULL; i++) {
990     if (guestfs_exists(g, *i) == 0) continue;
991
992     int64_t size = guestfs_filesize (g, *i);
993     if (size == -1)
994       /* guestfs_filesize failed and has already set error in handle */
995       return -1;
996     if (size > MAX_AUGEAS_FILE_SIZE) {
997       error (g, _("size of %s is unreasonably large (%" PRIi64 " bytes)"),
998              *i, size);
999       return -1;
1000     }
1001   }
1002
1003   /* If !feature_available (g, "augeas") then the next call will fail.
1004    * Arguably we might want to fall back to a non-Augeas method in
1005    * this case.
1006    */
1007   if (guestfs_aug_init (g, "/", 16|32) == -1)
1008     return -1;
1009
1010   int r = -1;
1011
1012   /* Tell Augeas to only load one file (thanks RaphaĆ«l Pinson). */
1013 #define AUGEAS_LOAD "/augeas/load//incl[. != \""
1014 #define AUGEAS_LOAD_LEN (strlen(AUGEAS_LOAD))
1015   size_t conflen = strlen(configfiles[0]);
1016   size_t buflen = AUGEAS_LOAD_LEN + conflen + 1 /* Closing " */;
1017   char *buf = safe_malloc(g, buflen + 2 /* Closing ] + null terminator */);
1018
1019   memcpy(buf, AUGEAS_LOAD, AUGEAS_LOAD_LEN);
1020   memcpy(buf + AUGEAS_LOAD_LEN, configfiles[0], conflen);
1021   buf[buflen - 1] = '"';
1022 #undef AUGEAS_LOAD_LEN
1023 #undef AUGEAS_LOAD
1024
1025 #define EXCL " and . != \""
1026 #define EXCL_LEN (strlen(EXCL))
1027   for (const char **i = &configfiles[1]; *i != NULL; i++) {
1028     size_t orig_buflen = buflen;
1029     conflen = strlen(*i);
1030     buflen += EXCL_LEN + conflen + 1 /* Closing " */;
1031     buf = safe_realloc(g, buf, buflen + 2 /* Closing ] + null terminator */);
1032     char *s = buf + orig_buflen;
1033
1034     memcpy(s, EXCL, EXCL_LEN);
1035     memcpy(s + EXCL_LEN, *i, conflen);
1036     buf[buflen - 1] = '"';
1037   }
1038 #undef EXCL_LEN
1039 #undef EXCL
1040
1041   buf[buflen] = ']';
1042   buf[buflen + 1] = '\0';
1043
1044   if (guestfs_aug_rm (g, buf) == -1) {
1045     free(buf);
1046     goto out;
1047   }
1048   free(buf);
1049
1050   if (guestfs_aug_load (g) == -1)
1051     goto out;
1052
1053   r = f (g, fs);
1054
1055  out:
1056   guestfs_aug_close (g);
1057
1058   return r;
1059 }
1060
1061 #endif /* defined(HAVE_HIVEX) */