inspect: Centralize all file downloads through a single function.
[libguestfs.git] / src / inspect.c
index 3b2720c..f0002cd 100644 (file)
@@ -57,6 +57,8 @@ static pcre *re_rhel_no_minor;
 static pcre *re_major_minor;
 static pcre *re_aug_seq;
 static pcre *re_xdev;
+static pcre *re_first_partition;
+static pcre *re_freebsd;
 static pcre *re_windows_version;
 
 static void compile_regexps (void) __attribute__((constructor));
@@ -87,6 +89,8 @@ compile_regexps (void)
   COMPILE (re_major_minor, "(\\d+)\\.(\\d+)", 0);
   COMPILE (re_aug_seq, "/\\d+$", 0);
   COMPILE (re_xdev, "^/dev/(?:h|s|v|xv)d([a-z]\\d*)$", 0);
+  COMPILE (re_first_partition, "^/dev/(?:h|s|v)d.1$", 0);
+  COMPILE (re_freebsd, "^/dev/ad(\\d+)s(\\d+)([a-z])$", 0);
   COMPILE (re_windows_version, "^(\\d+)\\.(\\d+)", 0);
 }
 
@@ -100,6 +104,8 @@ free_regexps (void)
   pcre_free (re_major_minor);
   pcre_free (re_aug_seq);
   pcre_free (re_xdev);
+  pcre_free (re_first_partition);
+  pcre_free (re_freebsd);
   pcre_free (re_windows_version);
 }
 
@@ -187,6 +193,8 @@ guestfs__inspect_os (guestfs_h *g)
  */
 static int check_filesystem (guestfs_h *g, const char *device);
 static int check_linux_root (guestfs_h *g, struct inspect_fs *fs);
+static int check_freebsd_root (guestfs_h *g, struct inspect_fs *fs);
+static void check_architecture (guestfs_h *g, struct inspect_fs *fs);
 static int check_fstab (guestfs_h *g, struct inspect_fs *fs);
 static int check_windows_root (guestfs_h *g, struct inspect_fs *fs);
 static int check_windows_arch (guestfs_h *g, struct inspect_fs *fs);
@@ -199,6 +207,7 @@ static int add_fstab_entry (guestfs_h *g, struct inspect_fs *fs,
 static char *resolve_fstab_device (guestfs_h *g, const char *spec);
 static void check_package_format (guestfs_h *g, struct inspect_fs *fs);
 static void check_package_management (guestfs_h *g, struct inspect_fs *fs);
+static int download_to_tmp (guestfs_h *g, const char *filename, char *localtmp, int64_t max_size);
 
 static int
 check_for_filesystem_on (guestfs_h *g, const char *device)
@@ -257,13 +266,35 @@ check_filesystem (guestfs_h *g, const char *device)
   fs->device = safe_strdup (g, device);
   fs->is_mountable = 1;
 
+  /* Optimize some of the tests by avoiding multiple tests of the same thing. */
+  int is_dir_etc = guestfs_is_dir (g, "/etc") > 0;
+  int is_dir_bin = guestfs_is_dir (g, "/bin") > 0;
+  int is_dir_share = guestfs_is_dir (g, "/share") > 0;
+
   /* Grub /boot? */
   if (guestfs_is_file (g, "/grub/menu.lst") > 0 ||
       guestfs_is_file (g, "/grub/grub.conf") > 0)
     fs->content = FS_CONTENT_LINUX_BOOT;
+  /* FreeBSD root? */
+  else if (is_dir_etc &&
+           is_dir_bin &&
+           guestfs_is_file (g, "/etc/freebsd-update.conf") > 0 &&
+           guestfs_is_file (g, "/etc/fstab") > 0) {
+    /* Ignore /dev/sda1 which is a shadow of the real root filesystem
+     * that is probably /dev/sda5 (see:
+     * http://www.freebsd.org/doc/handbook/disk-organization.html)
+     */
+    if (match (g, device, re_first_partition))
+      return 0;
+
+    fs->is_root = 1;
+    fs->content = FS_CONTENT_FREEBSD_ROOT;
+    if (check_freebsd_root (g, fs) == -1)
+      return -1;
+  }
   /* Linux root? */
-  else if (guestfs_is_dir (g, "/etc") > 0 &&
-           guestfs_is_dir (g, "/bin") > 0 &&
+  else if (is_dir_etc &&
+           is_dir_bin &&
            guestfs_is_file (g, "/etc/fstab") > 0) {
     fs->is_root = 1;
     fs->content = FS_CONTENT_LINUX_ROOT;
@@ -271,16 +302,16 @@ check_filesystem (guestfs_h *g, const char *device)
       return -1;
   }
   /* Linux /usr/local? */
-  else if (guestfs_is_dir (g, "/etc") > 0 &&
-           guestfs_is_dir (g, "/bin") > 0 &&
-           guestfs_is_dir (g, "/share") > 0 &&
+  else if (is_dir_etc &&
+           is_dir_bin &&
+           is_dir_share &&
            guestfs_exists (g, "/local") == 0 &&
            guestfs_is_file (g, "/etc/fstab") == 0)
     fs->content = FS_CONTENT_LINUX_USR_LOCAL;
   /* Linux /usr? */
-  else if (guestfs_is_dir (g, "/etc") > 0 &&
-           guestfs_is_dir (g, "/bin") > 0 &&
-           guestfs_is_dir (g, "/share") > 0 &&
+  else if (is_dir_etc &&
+           is_dir_bin &&
+           is_dir_share &&
            guestfs_exists (g, "/local") > 0 &&
            guestfs_is_file (g, "/etc/fstab") == 0)
     fs->content = FS_CONTENT_LINUX_USR;
@@ -319,7 +350,7 @@ parse_release_file (guestfs_h *g, struct inspect_fs *fs,
   if (product_name == NULL)
     return -1;
   if (product_name[0] == NULL) {
-    error (g, "%s: file is empty", release_filename);
+    error (g, _("%s: file is empty"), release_filename);
     guestfs___free_string_list (product_name);
     return -1;
   }
@@ -359,7 +390,22 @@ parse_major_minor (guestfs_h *g, struct inspect_fs *fs)
  *   DISTRIB_RELEASE=10.04                            # Version
  *   DISTRIB_CODENAME=lucid
  *   DISTRIB_DESCRIPTION="Ubuntu 10.04.1 LTS"         # Product name
- * In theory other distros could have this LSB file, but none do.
+ *
+ * [Ubuntu-derived ...] Linux Mint was found to have this:
+ *   DISTRIB_ID=LinuxMint
+ *   DISTRIB_RELEASE=10
+ *   DISTRIB_CODENAME=julia
+ *   DISTRIB_DESCRIPTION="Linux Mint 10 Julia"
+ * Linux Mint also has /etc/linuxmint/info with more information,
+ * but we can use the LSB file.
+ *
+ * Mandriva has:
+ *   LSB_VERSION=lsb-4.0-amd64:lsb-4.0-noarch
+ *   DISTRIB_ID=MandrivaLinux
+ *   DISTRIB_RELEASE=2010.1
+ *   DISTRIB_CODENAME=Henry_Farman
+ *   DISTRIB_DESCRIPTION="Mandriva Linux 2010.1"
+ * Mandriva also has a normal release file called /etc/mandriva-release.
  */
 static int
 parse_lsb_release (guestfs_h *g, struct inspect_fs *fs)
@@ -378,6 +424,16 @@ parse_lsb_release (guestfs_h *g, struct inspect_fs *fs)
       fs->distro = OS_DISTRO_UBUNTU;
       r = 1;
     }
+    else if (fs->distro == 0 &&
+             STREQ (lines[i], "DISTRIB_ID=LinuxMint")) {
+      fs->distro = OS_DISTRO_LINUX_MINT;
+      r = 1;
+    }
+    else if (fs->distro == 0 &&
+             STREQ (lines[i], "DISTRIB_ID=MandrivaLinux")) {
+      fs->distro = OS_DISTRO_MANDRIVA;
+      r = 1;
+    }
     else if (STRPREFIX (lines[i], "DISTRIB_RELEASE=")) {
       char *major, *minor;
       if (match2 (g, &lines[i][16], re_major_minor, &major, &minor)) {
@@ -523,9 +579,57 @@ check_linux_root (guestfs_h *g, struct inspect_fs *fs)
   check_package_management (g, fs);
 
   /* Determine the architecture. */
+  check_architecture (g, fs);
+
+  /* We already know /etc/fstab exists because it's part of the test
+   * for Linux root above.  We must now parse this file to determine
+   * which filesystems are used by the operating system and how they
+   * are mounted.
+   */
+  if (check_fstab (g, fs) == -1)
+    return -1;
+
+  return 0;
+}
+
+/* The currently mounted device is known to be a FreeBSD root. */
+static int
+check_freebsd_root (guestfs_h *g, struct inspect_fs *fs)
+{
+  int r;
+
+  fs->type = OS_TYPE_FREEBSD;
+
+  /* FreeBSD has no authoritative version file.  The version number is
+   * in /etc/motd, which the system administrator might edit, but
+   * we'll use that anyway.
+   */
+
+  if (guestfs_exists (g, "/etc/motd") > 0) {
+    if (parse_release_file (g, fs, "/etc/motd") == -1)
+      return -1;
+
+    if (parse_major_minor (g, fs) == -1)
+      return -1;
+  }
+
+  /* Determine the architecture. */
+  check_architecture (g, fs);
+
+  /* We already know /etc/fstab exists because it's part of the test above. */
+  if (check_fstab (g, fs) == -1)
+    return -1;
+
+  return 0;
+}
+
+static void
+check_architecture (guestfs_h *g, struct inspect_fs *fs)
+{
   const char *binaries[] =
     { "/bin/bash", "/bin/ls", "/bin/echo", "/bin/rm", "/bin/sh" };
   size_t i;
+
   for (i = 0; i < sizeof binaries / sizeof binaries[0]; ++i) {
     if (guestfs_is_file (g, binaries[i]) > 0) {
       /* Ignore errors from file_architecture call. */
@@ -543,13 +647,24 @@ check_linux_root (guestfs_h *g, struct inspect_fs *fs)
       }
     }
   }
+}
 
-  /* We already know /etc/fstab exists because it's part of the test
-   * for Linux root above.  We must now parse this file to determine
-   * which filesystems are used by the operating system and how they
-   * are mounted.
-   * XXX What if !feature_available (g, "augeas")?
-   */
+static int check_fstab_aug_open (guestfs_h *g, struct inspect_fs *fs);
+
+static int
+check_fstab (guestfs_h *g, struct inspect_fs *fs)
+{
+  int r;
+  int64_t size;
+
+  /* Security: Refuse to do this if /etc/fstab is huge. */
+  size = guestfs_filesize (g, "/etc/fstab");
+  if (size == -1 || size > 100000) {
+    error (g, _("size of /etc/fstab unreasonable (%" PRIi64 " bytes)"), size);
+    return -1;
+  }
+
+  /* XXX What if !feature_available (g, "augeas")? */
   if (guestfs_aug_init (g, "/", 16|32) == -1)
     return -1;
 
@@ -557,7 +672,7 @@ check_linux_root (guestfs_h *g, struct inspect_fs *fs)
   guestfs_aug_rm (g, "/augeas/load//incl[. != \"/etc/fstab\"]");
   guestfs_aug_load (g);
 
-  r = check_fstab (g, fs);
+  r = check_fstab_aug_open (g, fs);
   guestfs_aug_close (g);
   if (r == -1)
     return -1;
@@ -566,14 +681,14 @@ check_linux_root (guestfs_h *g, struct inspect_fs *fs)
 }
 
 static int
-check_fstab (guestfs_h *g, struct inspect_fs *fs)
+check_fstab_aug_open (guestfs_h *g, struct inspect_fs *fs)
 {
   char **lines = guestfs_aug_ls (g, "/files/etc/fstab");
   if (lines == NULL)
     return -1;
 
   if (lines[0] == NULL) {
-    error (g, "could not parse /etc/fstab or empty file");
+    error (g, _("could not parse /etc/fstab or empty file"));
     guestfs___free_string_list (lines);
     return -1;
   }
@@ -703,6 +818,7 @@ resolve_fstab_device (guestfs_h *g, const char *spec)
 {
   char *a1;
   char *device = NULL;
+  char *bsddisk, *bsdslice, *bsdpart;
 
   if (STRPREFIX (spec, "/dev/mapper/")) {
     /* LVM2 does some strange munging on /dev/mapper paths for VGs and
@@ -736,11 +852,33 @@ resolve_fstab_device (guestfs_h *g, const char *spec)
     free (a1);
     guestfs___free_string_list (devices);
   }
-  else {
-    /* Didn't match device pattern, return original spec unchanged. */
-    device = safe_strdup (g, spec);
+  else if (match3 (g, spec, re_freebsd, &bsddisk, &bsdslice, &bsdpart)) {
+    /* FreeBSD disks are organized quite differently.  See:
+     * http://www.freebsd.org/doc/handbook/disk-organization.html
+     * FreeBSD "partitions" are exposed as quasi-extended partitions
+     * numbered from 5 in Linux.  I have no idea what happens when you
+     * have multiple "slices" (the FreeBSD term for MBR partitions).
+     */
+    int disk = parse_unsigned_int (g, bsddisk);
+    int slice = parse_unsigned_int (g, bsdslice);
+    int part = bsdpart[0] - 'a' /* counting from 0 */;
+    free (bsddisk);
+    free (bsdslice);
+    free (bsdpart);
+
+    if (disk == -1 || disk > 26 ||
+        slice <= 0 || slice > 1 /* > 4 .. see comment above */ ||
+        part < 0 || part >= 26)
+      goto out;
+
+    device = safe_asprintf (g, "/dev/sd%c%d", disk + 'a', part + 5);
   }
 
+ out:
+  /* Didn't match device pattern, return original spec unchanged. */
+  if (device == NULL)
+    device = safe_strdup (g, spec);
+
   return device;
 }
 
@@ -815,12 +953,7 @@ check_windows_arch (guestfs_h *g, struct inspect_fs *fs)
 static int
 check_windows_registry (guestfs_h *g, struct inspect_fs *fs)
 {
-  TMP_TEMPLATE_ON_STACK (dir);
-#define dir_len (strlen (dir))
-#define software_hive_len (dir_len + 16)
-  char software_hive[software_hive_len];
-#define cmd_len (dir_len + 16)
-  char cmd[cmd_len];
+  TMP_TEMPLATE_ON_STACK (software_local);
 
   size_t len = strlen (fs->windows_systemroot) + 64;
   char software[len];
@@ -838,17 +971,10 @@ check_windows_registry (guestfs_h *g, struct inspect_fs *fs)
   hive_h *h = NULL;
   hive_value_h *values = NULL;
 
-  if (mkdtemp (dir) == NULL) {
-    perrorf (g, "mkdtemp");
-    goto out;
-  }
-
-  snprintf (software_hive, software_hive_len, "%s/software", dir);
-
-  if (guestfs_download (g, software_path, software_hive) == -1)
+  if (download_to_tmp (g, software_path, software_local, 100000000) == -1)
     goto out;
 
-  h = hivex_open (software_hive, g->verbose ? HIVEX_OPEN_VERBOSE : 0);
+  h = hivex_open (software_local, g->verbose ? HIVEX_OPEN_VERBOSE : 0);
   if (h == NULL) {
     perrorf (g, "hivex_open");
     goto out;
@@ -908,7 +1034,7 @@ check_windows_registry (guestfs_h *g, struct inspect_fs *fs)
         if (fs->minor_version == -1) {
           free (key);
           free (version);
-          return -1;
+          goto out;
         }
       }
 
@@ -925,15 +1051,9 @@ check_windows_registry (guestfs_h *g, struct inspect_fs *fs)
   free (values);
   free (software_path);
 
-  /* Free up the temporary directory.  Note the directory name cannot
-   * contain shell meta-characters because of the way it was
-   * constructed above.
-   */
-  snprintf (cmd, cmd_len, "rm -rf %s", dir);
-  ignore_value (system (cmd));
-#undef dir_len
-#undef software_hive_len
-#undef cmd_len
+  /* Free up the temporary file. */
+  unlink (software_local);
+#undef software_local_len
 
   return ret;
 }
@@ -975,7 +1095,7 @@ parse_unsigned_int (guestfs_h *g, const char *str)
   long ret;
   int r = xstrtol (str, NULL, 10, &ret, "");
   if (r != LONGINT_OK) {
-    error (g, "could not parse integer in version number: %s", str);
+    error (g, _("could not parse integer in version number: %s"), str);
     return -1;
   }
   return ret;
@@ -993,11 +1113,13 @@ check_package_format (guestfs_h *g, struct inspect_fs *fs)
   case OS_DISTRO_MEEGO:
   case OS_DISTRO_REDHAT_BASED:
   case OS_DISTRO_RHEL:
+  case OS_DISTRO_MANDRIVA:
     fs->package_format = OS_PACKAGE_FORMAT_RPM;
     break;
 
   case OS_DISTRO_DEBIAN:
   case OS_DISTRO_UBUNTU:
+  case OS_DISTRO_LINUX_MINT:
     fs->package_format = OS_PACKAGE_FORMAT_DEB;
     break;
 
@@ -1038,6 +1160,7 @@ check_package_management (guestfs_h *g, struct inspect_fs *fs)
 
   case OS_DISTRO_DEBIAN:
   case OS_DISTRO_UBUNTU:
+  case OS_DISTRO_LINUX_MINT:
     fs->package_management = OS_PACKAGE_MANAGEMENT_APT;
     break;
 
@@ -1050,6 +1173,9 @@ check_package_management (guestfs_h *g, struct inspect_fs *fs)
   case OS_DISTRO_PARDUS:
     fs->package_management = OS_PACKAGE_MANAGEMENT_PISI;
     break;
+  case OS_DISTRO_MANDRIVA:
+    fs->package_management = OS_PACKAGE_MANAGEMENT_URPMI;
+    break;
 
   case OS_DISTRO_WINDOWS:
   case OS_DISTRO_UNKNOWN:
@@ -1122,6 +1248,7 @@ guestfs__inspect_get_type (guestfs_h *g, const char *root)
   switch (fs->type) {
   case OS_TYPE_LINUX: ret = safe_strdup (g, "linux"); break;
   case OS_TYPE_WINDOWS: ret = safe_strdup (g, "windows"); break;
+  case OS_TYPE_FREEBSD: ret = safe_strdup (g, "freebsd"); break;
   case OS_TYPE_UNKNOWN: default: ret = safe_strdup (g, "unknown"); break;
   }
 
@@ -1151,6 +1278,8 @@ guestfs__inspect_get_distro (guestfs_h *g, const char *root)
   case OS_DISTRO_DEBIAN: ret = safe_strdup (g, "debian"); break;
   case OS_DISTRO_FEDORA: ret = safe_strdup (g, "fedora"); break;
   case OS_DISTRO_GENTOO: ret = safe_strdup (g, "gentoo"); break;
+  case OS_DISTRO_LINUX_MINT: ret = safe_strdup (g, "linuxmint"); break;
+  case OS_DISTRO_MANDRIVA: ret = safe_strdup (g, "mandriva"); break;
   case OS_DISTRO_MEEGO: ret = safe_strdup (g, "meego"); break;
   case OS_DISTRO_PARDUS: ret = safe_strdup (g, "pardus"); break;
   case OS_DISTRO_REDHAT_BASED: ret = safe_strdup (g, "redhat-based"); break;
@@ -1319,6 +1448,7 @@ guestfs__inspect_get_package_management (guestfs_h *g, const char *root)
   case OS_PACKAGE_MANAGEMENT_PACMAN: ret = safe_strdup (g, "pacman"); break;
   case OS_PACKAGE_MANAGEMENT_PORTAGE: ret = safe_strdup (g, "portage"); break;
   case OS_PACKAGE_MANAGEMENT_PISI: ret = safe_strdup (g, "pisi"); break;
+  case OS_PACKAGE_MANAGEMENT_URPMI: ret = safe_strdup (g, "urpmi"); break;
   case OS_PACKAGE_MANAGEMENT_UNKNOWN:
   default:
     ret = safe_strdup (g, "unknown");
@@ -1328,6 +1458,51 @@ guestfs__inspect_get_package_management (guestfs_h *g, const char *root)
   return ret;
 }
 
+/* Download to a guest file to a local temporary file.  Refuse to
+ * download the guest file if it is larger than max_size.  The caller
+ * is responsible for deleting the temporary file after use.
+ */
+static int
+download_to_tmp (guestfs_h *g, const char *filename,
+                 char *localtmp, int64_t max_size)
+{
+  int fd;
+  char buf[32];
+  int64_t size;
+
+  size = guestfs_filesize (g, filename);
+  if (size == -1)
+    /* guestfs_filesize failed and has already set error in handle */
+    return -1;
+  if (size > max_size) {
+    error (g, _("size of %s is unreasonably large (%" PRIi64 " bytes)"),
+           filename, size);
+    return -1;
+  }
+
+  fd = mkstemp (localtmp);
+  if (fd == -1) {
+    perrorf (g, "mkstemp");
+    return -1;
+  }
+
+  snprintf (buf, sizeof buf, "/dev/fd/%d", fd);
+
+  if (guestfs_download (g, filename, buf) == -1) {
+    close (fd);
+    unlink (localtmp);
+    return -1;
+  }
+
+  if (close (fd) == -1) {
+    perrorf (g, "close: %s", localtmp);
+    unlink (localtmp);
+    return -1;
+  }
+
+  return 0;
+}
+
 #else /* no PCRE or hivex at compile time */
 
 /* XXX These functions should be in an optgroup. */
@@ -1526,4 +1701,29 @@ guestfs___match2 (guestfs_h *g, const char *str, const pcre *re,
   return 1;
 }
 
+/* Match a regular expression which contains exactly three captures. */
+int
+guestfs___match3 (guestfs_h *g, const char *str, const pcre *re,
+                  char **ret1, char **ret2, char **ret3)
+{
+  size_t len = strlen (str);
+  int vec[30], r;
+
+  r = pcre_exec (re, NULL, str, len, 0, 0, vec, 30);
+  if (r == PCRE_ERROR_NOMATCH)
+    return 0;
+  if (r != 4) {
+    /* Internal error -- should not happen. */
+    fprintf (stderr, "libguestfs: %s: %s: internal error: pcre_exec returned unexpected error code %d when matching against the string \"%s\"\n",
+             __FILE__, __func__, r, str);
+    return 0;
+  }
+
+  *ret1 = safe_strndup (g, &str[vec[2]], vec[3]-vec[2]);
+  *ret2 = safe_strndup (g, &str[vec[4]], vec[5]-vec[4]);
+  *ret3 = safe_strndup (g, &str[vec[6]], vec[7]-vec[6]);
+
+  return 1;
+}
+
 #endif /* HAVE_PCRE */