summaryrefslogtreecommitdiffstats
path: root/src/integritysetup/integrity-util.c
blob: c29d4fcd5da847a5da276b23b06e3a72c24b2f6e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include "integrity-util.h"

#include "extract-word.h"
#include "fileio.h"
#include "path-util.h"
#include "percent-util.h"


static int supported_integrity_algorithm(char *user_supplied) {
        if (!STR_IN_SET(user_supplied, "crc32", "crc32c", "sha1", "sha256", "hmac-sha256"))
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Unsupported integrity algorithm (%s)", user_supplied);
        return 0;
}

int parse_integrity_options(
                const char *options,
                uint32_t *ret_activate_flags,
                int *ret_percent,
                usec_t *ret_commit_time,
                char **ret_data_device,
                char **ret_integrity_alg) {
        int r;

        for (;;) {
                _cleanup_free_ char *word = NULL;
                char *val;

                r = extract_first_word(&options, &word, ",", EXTRACT_DONT_COALESCE_SEPARATORS | EXTRACT_UNESCAPE_SEPARATORS);
                if (r < 0)
                        return log_error_errno(r, "Failed to parse options: %m");
                if (r == 0)
                        break;
                else if (streq(word, "allow-discards")) {
                        if (ret_activate_flags)
                                *ret_activate_flags |= CRYPT_ACTIVATE_ALLOW_DISCARDS;
                } else if ((val = startswith(word, "mode="))) {
                        if (streq(val, "journal")) {
                                if (ret_activate_flags)
                                        *ret_activate_flags &= ~(CRYPT_ACTIVATE_NO_JOURNAL | CRYPT_ACTIVATE_NO_JOURNAL_BITMAP);
                        } else if (streq(val, "bitmap")) {
                                if (ret_activate_flags) {
                                        *ret_activate_flags &= ~CRYPT_ACTIVATE_NO_JOURNAL;
                                        *ret_activate_flags |= CRYPT_ACTIVATE_NO_JOURNAL_BITMAP;
                                }
                        } else if (streq(val, "direct")) {
                                if (ret_activate_flags) {
                                        *ret_activate_flags |= CRYPT_ACTIVATE_NO_JOURNAL;
                                        *ret_activate_flags &= ~CRYPT_ACTIVATE_NO_JOURNAL_BITMAP;
                                }
                        } else
                                log_warning("Encountered unknown mode option '%s', ignoring.", val);
                } else if ((val = startswith(word, "journal-watermark="))) {
                        r = parse_percent(val);
                        if (r < 0)
                                return log_error_errno(r, "Failed to parse journal-watermark value or value out of range (%s)", val);
                        if (ret_percent)
                                *ret_percent = r;
                } else if ((val = startswith(word, "journal-commit-time="))) {
                        usec_t tmp_commit_time;
                        r = parse_sec(val, &tmp_commit_time);
                        if (r < 0)
                                return log_error_errno(r, "Failed to parse journal-commit-time value (%s)", val);
                        if (ret_commit_time)
                                *ret_commit_time = tmp_commit_time;
                } else if ((val = startswith(word, "data-device="))) {
                        if (ret_data_device) {
                                r = free_and_strdup(ret_data_device, val);
                                if (r < 0)
                                        return log_oom();
                        }
                } else if ((val = startswith(word, "integrity-algorithm="))) {
                        r = supported_integrity_algorithm(val);
                        if (r < 0)
                                return r;
                        if (ret_integrity_alg) {
                                r = free_and_strdup(ret_integrity_alg, val);
                                if (r < 0)
                                        return log_oom();
                        }
                } else
                        log_warning("Encountered unknown option '%s', ignoring.", word);
        }

        return r;
}