|
26 | 26 | #include <stdio.h> |
27 | 27 | #include <stdlib.h> |
28 | 28 | #include <string.h> |
| 29 | +#include <libintl.h> |
| 30 | +#include <error.h> |
| 31 | +#include <assert.h> |
29 | 32 |
|
30 | 33 | #include "c-ctype.h" |
31 | 34 |
|
@@ -74,21 +77,37 @@ inspect_do_decrypt (guestfs_h *g, struct key_store *ks) |
74 | 77 | if (partitions == NULL) |
75 | 78 | exit (EXIT_FAILURE); |
76 | 79 |
|
77 | | - int need_rescan = 0; |
78 | | - size_t i; |
| 80 | + int need_rescan = 0, r; |
| 81 | + size_t i, j; |
| 82 | + |
79 | 83 | for (i = 0; partitions[i] != NULL; ++i) { |
80 | 84 | CLEANUP_FREE char *type = guestfs_vfs_type (g, partitions[i]); |
81 | 85 | if (type && STREQ (type, "crypto_LUKS")) { |
82 | 86 | char mapname[32]; |
83 | 87 | make_mapname (partitions[i], mapname, sizeof mapname); |
84 | 88 |
|
85 | | - CLEANUP_FREE char *key = get_key (ks, partitions[i]); |
86 | | - /* XXX Should we call guestfs_luks_open_ro if readonly flag |
87 | | - * is set? This might break 'mount_ro'. |
88 | | - */ |
89 | | - if (guestfs_luks_open (g, partitions[i], key, mapname) == -1) |
90 | | - exit (EXIT_FAILURE); |
91 | | - |
| 89 | + CLEANUP_FREE_STRING_LIST char **keys = get_keys (ks, partitions[i]); |
| 90 | + assert (guestfs_int_count_strings (keys) > 0); |
| 91 | + |
| 92 | + /* Try each key in turn. */ |
| 93 | + for (j = 0; keys[j] != NULL; ++j) { |
| 94 | + /* XXX Should we call guestfs_luks_open_ro if readonly flag |
| 95 | + * is set? This might break 'mount_ro'. |
| 96 | + */ |
| 97 | + guestfs_push_error_handler (g, NULL, NULL); |
| 98 | + r = guestfs_luks_open (g, partitions[i], keys[j], mapname); |
| 99 | + guestfs_pop_error_handler (g); |
| 100 | + if (r == 0) |
| 101 | + goto opened; |
| 102 | + } |
| 103 | + error (EXIT_FAILURE, 0, |
| 104 | + _("could not find key to open LUKS encrypted %s.\n\n" |
| 105 | + "Try using --key on the command line.\n\n" |
| 106 | + "Original error: %s (%d)"), |
| 107 | + partitions[i], guestfs_last_error (g), |
| 108 | + guestfs_last_errno (g)); |
| 109 | + |
| 110 | + opened: |
92 | 111 | need_rescan = 1; |
93 | 112 | } |
94 | 113 | } |
|
0 commit comments