Debian Codegraph @

src/integritysetup/integrity-util.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "alloc-util.h"
4 #include "cryptsetup-util.h"
5 #include "extract-word.h"
6 #include "integrity-util.h"
7 #include "log.h"
8 #include "percent-util.h"
9 #include "string-table.h"
10 #include "string-util.h"
11 #include "time-util.h"
12
13 /* Integrity algorithm names used by integritysetup/integritytab */
14 static const char* const integrity_algorithm_table[_INTEGRITY_ALGORITHM_MAX] = {
15 [INTEGRITY_ALGORITHM_CRC32] = "crc32",
16 [INTEGRITY_ALGORITHM_CRC32C] = "crc32c",
17 [INTEGRITY_ALGORITHM_XXHASH64] = "xxhash64",
18 [INTEGRITY_ALGORITHM_SHA1] = "sha1",
19 [INTEGRITY_ALGORITHM_SHA256] = "sha256",
20 [INTEGRITY_ALGORITHM_HMAC_SHA256] = "hmac-sha256",
21 [INTEGRITY_ALGORITHM_HMAC_SHA512] = "hmac-sha512",
22 [INTEGRITY_ALGORITHM_PHMAC_SHA256] = "phmac-sha256",
23 [INTEGRITY_ALGORITHM_PHMAC_SHA512] = "phmac-sha512",
24 };
25
26 DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(integrity_algorithm, IntegrityAlgorithm);
27
28 int parse_integrity_options(
29 const char *options,
30 uint32_t *ret_activate_flags,
31 int *ret_percent,
32 usec_t *ret_commit_time,
33 char **ret_data_device,
34 IntegrityAlgorithm *ret_integrity_alg) {
35 int r;
36
37 for (;;) {
38 _cleanup_free_ char *word = NULL;
39 char *val;
40
41 r = extract_first_word(&options, &word, ",", EXTRACT_DONT_COALESCE_SEPARATORS | EXTRACT_UNESCAPE_SEPARATORS);
42 if (r < 0)
43 return log_error_errno(r, "Failed to parse options: %m");
44 if (r == 0)
45 break;
46 else if (streq(word, "allow-discards")) {
47 if (ret_activate_flags)
48 *ret_activate_flags |= CRYPT_ACTIVATE_ALLOW_DISCARDS;
49 } else if ((val = startswith(word, "mode="))) {
50 if (streq(val, "journal")) {
51 if (ret_activate_flags)
52 *ret_activate_flags &= ~(CRYPT_ACTIVATE_NO_JOURNAL | CRYPT_ACTIVATE_NO_JOURNAL_BITMAP);
53 } else if (streq(val, "bitmap")) {
54 if (ret_activate_flags) {
55 *ret_activate_flags &= ~CRYPT_ACTIVATE_NO_JOURNAL;
56 *ret_activate_flags |= CRYPT_ACTIVATE_NO_JOURNAL_BITMAP;
57 }
58 } else if (streq(val, "direct")) {
59 if (ret_activate_flags) {
60 *ret_activate_flags |= CRYPT_ACTIVATE_NO_JOURNAL;
61 *ret_activate_flags &= ~CRYPT_ACTIVATE_NO_JOURNAL_BITMAP;
62 }
63 } else
64 log_warning("Encountered unknown mode option '%s', ignoring.", val);
65 } else if ((val = startswith(word, "journal-watermark="))) {
66 r = parse_percent(val);
67 if (r < 0)
68 return log_error_errno(r, "Failed to parse journal-watermark value or value out of range (%s)", val);
69 if (ret_percent)
70 *ret_percent = r;
71 } else if ((val = startswith(word, "journal-commit-time="))) {
72 usec_t tmp_commit_time;
73 r = parse_sec(val, &tmp_commit_time);
74 if (r < 0)
75 return log_error_errno(r, "Failed to parse journal-commit-time value (%s)", val);
76 if (ret_commit_time)
77 *ret_commit_time = tmp_commit_time;
78 } else if ((val = startswith(word, "data-device="))) {
79 if (ret_data_device) {
80 r = free_and_strdup(ret_data_device, val);
81 if (r < 0)
82 return log_oom();
83 }
84 } else if ((val = startswith(word, "integrity-algorithm="))) {
85 IntegrityAlgorithm a = integrity_algorithm_from_string(val);
86 if (a < 0)
87 return log_error_errno(a, "Unsupported integrity algorithm (%s)", val);
88
89 if (ret_integrity_alg)
90 *ret_integrity_alg = a;
91 } else
92 log_warning("Encountered unknown option '%s', ignoring.", word);
93 }
94
95 return r;
96 }