| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "sd-json.h" |
| 4 |
|
| 5 |
#include "alloc-util.h" |
| 6 |
#include "cryptenroll.h" |
| 7 |
#include "cryptenroll-list.h" |
| 8 |
#include "cryptsetup-util.h" |
| 9 |
#include "format-table.h" |
| 10 |
#include "json-util.h" |
| 11 |
#include "log.h" |
| 12 |
#include "parse-util.h" |
| 13 |
|
| 14 |
struct keyslot_metadata { |
| 15 |
int slot; |
| 16 |
const char *type; |
| 17 |
}; |
| 18 |
|
| 19 |
int list_enrolled(struct crypt_device *cd) { |
| 20 |
_cleanup_free_ struct keyslot_metadata *keyslot_metadata = NULL; |
| 21 |
_cleanup_(table_unrefp) Table *t = NULL; |
| 22 |
size_t n_keyslot_metadata = 0; |
| 23 |
int slot_max, r; |
| 24 |
TableCell *cell; |
| 25 |
|
| 26 |
assert(cd); |
| 27 |
|
| 28 |
/* First step, find out all currently used slots */ |
| 29 |
assert_se((slot_max = sym_crypt_keyslot_max(CRYPT_LUKS2)) > 0); |
| 30 |
for (int slot = 0; slot < slot_max; slot++) { |
| 31 |
crypt_keyslot_info status; |
| 32 |
|
| 33 |
status = sym_crypt_keyslot_status(cd, slot); |
| 34 |
if (!IN_SET(status, CRYPT_SLOT_ACTIVE, CRYPT_SLOT_ACTIVE_LAST)) |
| 35 |
continue; |
| 36 |
|
| 37 |
if (!GREEDY_REALLOC(keyslot_metadata, n_keyslot_metadata+1)) |
| 38 |
return log_oom(); |
| 39 |
|
| 40 |
keyslot_metadata[n_keyslot_metadata++] = (struct keyslot_metadata) { |
| 41 |
.slot = slot, |
| 42 |
}; |
| 43 |
} |
| 44 |
|
| 45 |
/* Second step, enumerate through all tokens, and update the slot table, indicating what kind of |
| 46 |
* token they are assigned to */ |
| 47 |
for (int token = 0; token < sym_crypt_token_max(CRYPT_LUKS2); token++) { |
| 48 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; |
| 49 |
const char *type; |
| 50 |
sd_json_variant *w, *z; |
| 51 |
EnrollType et; |
| 52 |
|
| 53 |
r = cryptsetup_get_token_as_json(cd, token, NULL, &v); |
| 54 |
if (IN_SET(r, -ENOENT, -EINVAL)) |
| 55 |
continue; |
| 56 |
if (r < 0) { |
| 57 |
log_warning_errno(r, "Failed to read JSON token data off disk, ignoring: %m"); |
| 58 |
continue; |
| 59 |
} |
| 60 |
|
| 61 |
w = sd_json_variant_by_key(v, "type"); |
| 62 |
if (!w || !sd_json_variant_is_string(w)) { |
| 63 |
log_warning("Token JSON data lacks type field, ignoring."); |
| 64 |
continue; |
| 65 |
} |
| 66 |
|
| 67 |
et = luks2_token_type_from_string(sd_json_variant_string(w)); |
| 68 |
if (et < 0) |
| 69 |
type = "other"; |
| 70 |
else |
| 71 |
type = enroll_type_to_string(et); |
| 72 |
|
| 73 |
w = sd_json_variant_by_key(v, "keyslots"); |
| 74 |
if (!w || !sd_json_variant_is_array(w)) { |
| 75 |
log_warning("Token JSON data lacks keyslots field, ignoring."); |
| 76 |
continue; |
| 77 |
} |
| 78 |
|
| 79 |
JSON_VARIANT_ARRAY_FOREACH(z, w) { |
| 80 |
unsigned u; |
| 81 |
|
| 82 |
if (!sd_json_variant_is_string(z)) { |
| 83 |
log_warning("Token JSON data's keyslot field is not an array of strings, ignoring."); |
| 84 |
continue; |
| 85 |
} |
| 86 |
|
| 87 |
r = safe_atou(sd_json_variant_string(z), &u); |
| 88 |
if (r < 0) { |
| 89 |
log_warning_errno(r, "Token JSON data's keyslot field is not an integer formatted as string, ignoring."); |
| 90 |
continue; |
| 91 |
} |
| 92 |
|
| 93 |
for (size_t i = 0; i < n_keyslot_metadata; i++) { |
| 94 |
if ((unsigned) keyslot_metadata[i].slot != u) |
| 95 |
continue; |
| 96 |
|
| 97 |
if (keyslot_metadata[i].type) /* Slot claimed multiple times? */ |
| 98 |
keyslot_metadata[i].type = POINTER_MAX; |
| 99 |
else |
| 100 |
keyslot_metadata[i].type = type; |
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
/* Finally, create a table out of it all */ |
| 106 |
t = table_new("slot", "type"); |
| 107 |
if (!t) |
| 108 |
return log_oom(); |
| 109 |
|
| 110 |
assert_se(cell = table_get_cell(t, 0, 0)); |
| 111 |
(void) table_set_align_percent(t, cell, 100); |
| 112 |
|
| 113 |
for (size_t i = 0; i < n_keyslot_metadata; i++) { |
| 114 |
r = table_add_many( |
| 115 |
t, |
| 116 |
TABLE_INT, keyslot_metadata[i].slot, |
| 117 |
TABLE_STRING, keyslot_metadata[i].type == POINTER_MAX ? "conflict" : |
| 118 |
keyslot_metadata[i].type ?: "password"); |
| 119 |
if (r < 0) |
| 120 |
return table_log_add_error(r); |
| 121 |
} |
| 122 |
|
| 123 |
if (table_isempty(t)) { |
| 124 |
log_info("No slots found."); |
| 125 |
return 0; |
| 126 |
} |
| 127 |
|
| 128 |
return table_print_or_warn(t); |
| 129 |
} |