mrc0mmand / rpms / libguestfs

Forked from rpms/libguestfs 3 years ago
Clone

Blame SOURCES/0046-options-Allow-multiple-key-parameters.patch

da373f
From 0267ad921c3be8a780342b052264666cef2cc1b1 Mon Sep 17 00:00:00 2001
3efd08
From: "Richard W.M. Jones" <rjones@redhat.com>
3efd08
Date: Tue, 12 Nov 2019 17:50:17 +0000
3efd08
Subject: [PATCH] options: Allow multiple --key parameters.
3efd08
3efd08
This allows multiple --key parameters on the command line to match a
3efd08
single device.  For example:
3efd08
3efd08
  tool --key /dev/sda1:key:trykey1 --key /dev/sda1:key:trykey2
3efd08
3efd08
would try "trykey1" and "trykey2" against /dev/sda1.
3efd08
3efd08
(cherry picked from commit c10c8baedb88e7c2988a01b70fc5f81fa8e4885c
3efd08
in libguestfs-common)
3efd08
---
da373f
 common/options/decrypt.c | 35 ++++++++++++++++++++++++-------
3efd08
 common/options/keys.c    | 45 +++++++++++++++++++++++++++++++---------
3efd08
 common/options/options.h |  6 ++++--
da373f
 3 files changed, 66 insertions(+), 20 deletions(-)
3efd08
3efd08
diff --git a/common/options/decrypt.c b/common/options/decrypt.c
3efd08
index 234163d8c..3511d9fe9 100644
3efd08
--- a/common/options/decrypt.c
3efd08
+++ b/common/options/decrypt.c
3efd08
@@ -26,6 +26,9 @@
3efd08
 #include <stdio.h>
3efd08
 #include <stdlib.h>
3efd08
 #include <string.h>
3efd08
+#include <libintl.h>
3efd08
+#include <error.h>
3efd08
+#include <assert.h>
3efd08
 
3efd08
 #include "c-ctype.h"
3efd08
 
3efd08
@@ -74,21 +77,37 @@ inspect_do_decrypt (guestfs_h *g, struct key_store *ks)
3efd08
   if (partitions == NULL)
3efd08
     exit (EXIT_FAILURE);
3efd08
 
3efd08
-  int need_rescan = 0;
3efd08
-  size_t i;
3efd08
+  int need_rescan = 0, r;
3efd08
+  size_t i, j;
3efd08
+
3efd08
   for (i = 0; partitions[i] != NULL; ++i) {
3efd08
     CLEANUP_FREE char *type = guestfs_vfs_type (g, partitions[i]);
3efd08
     if (type && STREQ (type, "crypto_LUKS")) {
3efd08
       char mapname[32];
3efd08
       make_mapname (partitions[i], mapname, sizeof mapname);
3efd08
 
3efd08
-      CLEANUP_FREE char *key = get_key (ks, partitions[i]);
3efd08
-      /* XXX Should we call guestfs_luks_open_ro if readonly flag
3efd08
-       * is set?  This might break 'mount_ro'.
3efd08
-       */
3efd08
-      if (guestfs_luks_open (g, partitions[i], key, mapname) == -1)
3efd08
-        exit (EXIT_FAILURE);
3efd08
+      CLEANUP_FREE_STRING_LIST char **keys = get_keys (ks, partitions[i]);
3efd08
+      assert (guestfs_int_count_strings (keys) > 0);
da373f
 
3efd08
+      /* Try each key in turn. */
3efd08
+      for (j = 0; keys[j] != NULL; ++j) {
3efd08
+        /* XXX Should we call guestfs_luks_open_ro if readonly flag
3efd08
+         * is set?  This might break 'mount_ro'.
3efd08
+         */
3efd08
+        guestfs_push_error_handler (g, NULL, NULL);
3efd08
+        r = guestfs_luks_open (g, partitions[i], keys[j], mapname);
3efd08
+        guestfs_pop_error_handler (g);
3efd08
+        if (r == 0)
3efd08
+          goto opened;
3efd08
+      }
3efd08
+      error (EXIT_FAILURE, 0,
3efd08
+             _("could not find key to open LUKS encrypted %s.\n\n"
3efd08
+               "Try using --key on the command line.\n\n"
3efd08
+               "Original error: %s (%d)"),
3efd08
+             partitions[i], guestfs_last_error (g),
3efd08
+             guestfs_last_errno (g));
3efd08
+
3efd08
+    opened:
3efd08
       need_rescan = 1;
3efd08
     }
3efd08
   }
3efd08
diff --git a/common/options/keys.c b/common/options/keys.c
3efd08
index 74b549731..782bdb67f 100644
3efd08
--- a/common/options/keys.c
3efd08
+++ b/common/options/keys.c
3efd08
@@ -121,15 +121,32 @@ read_first_line_from_file (const char *filename)
3efd08
   return ret;
3efd08
 }
3efd08
 
3efd08
-char *
3efd08
-get_key (struct key_store *ks, const char *device)
3efd08
+/* Return the key(s) matching this particular device from the
3efd08
+ * keystore.  There may be multiple.  If none are read from the
3efd08
+ * keystore, ask the user.
3efd08
+ */
3efd08
+char **
3efd08
+get_keys (struct key_store *ks, const char *device)
3efd08
 {
3efd08
-  size_t i;
3efd08
+  size_t i, j, len;
3efd08
+  char **r;
3efd08
+  char *s;
3efd08
+
3efd08
+  /* We know the returned list must have at least one element and not
3efd08
+   * more than ks->nr_keys.
3efd08
+   */
3efd08
+  len = 1;
3efd08
+  if (ks)
3efd08
+    len = MIN (1, ks->nr_keys);
3efd08
+  r = calloc (len+1, sizeof (char *));
3efd08
+  if (r == NULL)
3efd08
+    error (EXIT_FAILURE, errno, "calloc");
3efd08
+
3efd08
+  j = 0;
3efd08
 
3efd08
   if (ks) {
3efd08
     for (i = 0; i < ks->nr_keys; ++i) {
3efd08
       struct key_store_key *key = &ks->keys[i];
3efd08
-      char *s;
3efd08
 
3efd08
       if (STRNEQ (key->device, device))
3efd08
         continue;
3efd08
@@ -139,17 +156,25 @@ get_key (struct key_store *ks, const char *device)
3efd08
         s = strdup (key->string.s);
3efd08
         if (!s)
3efd08
           error (EXIT_FAILURE, errno, "strdup");
3efd08
-        return s;
3efd08
+        r[j++] = s;
3efd08
+        break;
3efd08
       case key_file:
3efd08
-        return read_first_line_from_file (key->file.name);
3efd08
+        s = read_first_line_from_file (key->file.name);
3efd08
+        r[j++] = s;
3efd08
+        break;
3efd08
       }
3efd08
-
3efd08
-      /* Key not found in the key store, ask the user for it. */
3efd08
-      break;
3efd08
     }
3efd08
   }
3efd08
 
3efd08
-  return read_key (device);
3efd08
+  if (j == 0) {
3efd08
+    /* Key not found in the key store, ask the user for it. */
3efd08
+    s = read_key (device);
3efd08
+    if (!s)
3efd08
+      error (EXIT_FAILURE, 0, _("could not read key from user"));
3efd08
+    r[0] = s;
3efd08
+  }
3efd08
+
3efd08
+  return r;
3efd08
 }
3efd08
 
3efd08
 struct key_store *
3efd08
diff --git a/common/options/options.h b/common/options/options.h
3efd08
index 6fadf1e76..510e8a8a9 100644
3efd08
--- a/common/options/options.h
3efd08
+++ b/common/options/options.h
3efd08
@@ -104,7 +104,9 @@ struct mp {
3efd08
 
3efd08
 /* A key in the key store. */
3efd08
 struct key_store_key {
3efd08
-  /* The device this key refers to. */
3efd08
+  /* The device this key refers to.  There may be multiple matching
3efd08
+   * devices in the list.
3efd08
+   */
3efd08
   char *device;
3efd08
 
3efd08
   enum {
3efd08
@@ -146,7 +148,7 @@ extern void print_inspect_prompt (void);
3efd08
 
3efd08
 /* in key.c */
3efd08
 extern char *read_key (const char *param);
3efd08
-extern char *get_key (struct key_store *ks, const char *device);
3efd08
+extern char **get_keys (struct key_store *ks, const char *device);
3efd08
 extern struct key_store *key_store_add_from_selector (struct key_store *ks, const char *selector);
3efd08
 extern struct key_store *key_store_import_key (struct key_store *ks, const struct key_store_key *key);
3efd08
 extern void free_key_store (struct key_store *ks);
3efd08
-- 
da373f
2.18.4
3efd08