summaryrefslogtreecommitdiff
path: root/src/cryptsetup/cryptsetup-keyfile.c
blob: a6281fbdeeeafb616c4c75af4f8b0f39fce26ed3 (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
/* SPDX-License-Identifier: LGPL-2.1-or-later */

#include "cryptsetup-keyfile.h"
#include "fileio.h"
#include "path-util.h"
#include "strv.h"

int find_key_file(
                const char *key_file,
                char **search_path,
                const char *bindname,
                void **ret_key,
                size_t *ret_key_size) {

        char **i;
        int r;

        assert(key_file);
        assert(ret_key);
        assert(ret_key_size);

        if (strv_isempty(search_path) || path_is_absolute(key_file)) {

                r = read_full_file_full(
                                AT_FDCWD, key_file, UINT64_MAX, SIZE_MAX,
                                READ_FULL_FILE_SECURE|READ_FULL_FILE_WARN_WORLD_READABLE|READ_FULL_FILE_CONNECT_SOCKET,
                                bindname,
                                (char**) ret_key, ret_key_size);
                if (r < 0)
                        return log_error_errno(r, "Failed to load key file '%s': %m", key_file);

                return 1;
        }

        STRV_FOREACH(i, search_path) {
                _cleanup_free_ char *joined;

                joined = path_join(*i, key_file);
                if (!joined)
                        return log_oom();

                r = read_full_file_full(
                                AT_FDCWD, joined, UINT64_MAX, SIZE_MAX,
                                READ_FULL_FILE_SECURE|READ_FULL_FILE_WARN_WORLD_READABLE|READ_FULL_FILE_CONNECT_SOCKET,
                                bindname,
                                (char**) ret_key, ret_key_size);
                if (r >= 0)
                        return 1;
                if (r != -ENOENT)
                        return log_error_errno(r, "Failed to load key file '%s': %m", key_file);
        }

        /* Search path supplied, but file not found, report by returning NULL, but not failing */
        *ret_key = NULL;
        *ret_key_size = 0;
        return 0;
}