]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/cryptsetup/cryptsetup-keyfile.c
sd-boot+bootctl: invert order of entries w/o sort-key
[thirdparty/systemd.git] / src / cryptsetup / cryptsetup-keyfile.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "cryptsetup-keyfile.h"
4 #include "fileio.h"
5 #include "path-util.h"
6 #include "strv.h"
7
8 int find_key_file(
9 const char *key_file,
10 char **search_path,
11 const char *bindname,
12 void **ret_key,
13 size_t *ret_key_size) {
14
15 char **i;
16 int r;
17
18 assert(key_file);
19 assert(ret_key);
20 assert(ret_key_size);
21
22 if (strv_isempty(search_path) || path_is_absolute(key_file)) {
23
24 r = read_full_file_full(
25 AT_FDCWD, key_file, UINT64_MAX, SIZE_MAX,
26 READ_FULL_FILE_SECURE|READ_FULL_FILE_WARN_WORLD_READABLE|READ_FULL_FILE_CONNECT_SOCKET,
27 bindname,
28 (char**) ret_key, ret_key_size);
29 if (r == -E2BIG)
30 return log_error_errno(r, "Key file '%s' too large.", key_file);
31 if (r < 0)
32 return log_error_errno(r, "Failed to load key file '%s': %m", key_file);
33
34 return 1;
35 }
36
37 STRV_FOREACH(i, search_path) {
38 _cleanup_free_ char *joined = NULL;
39
40 joined = path_join(*i, key_file);
41 if (!joined)
42 return log_oom();
43
44 r = read_full_file_full(
45 AT_FDCWD, joined, UINT64_MAX, SIZE_MAX,
46 READ_FULL_FILE_SECURE|READ_FULL_FILE_WARN_WORLD_READABLE|READ_FULL_FILE_CONNECT_SOCKET,
47 bindname,
48 (char**) ret_key, ret_key_size);
49 if (r >= 0)
50 return 1;
51 if (r == -E2BIG) {
52 log_warning_errno(r, "Key file '%s' too large, ignoring.", key_file);
53 continue;
54 }
55 if (r != -ENOENT)
56 return log_error_errno(r, "Failed to load key file '%s': %m", key_file);
57 }
58
59 /* Search path supplied, but file not found, report by returning NULL, but not failing */
60 *ret_key = NULL;
61 *ret_key_size = 0;
62 return 0;
63 }