]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/veritysetup/veritysetup.c
3b4e72bf9ec555dffbf485e0d31076db151cd60f
[thirdparty/systemd.git] / src / veritysetup / veritysetup.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 This file is part of systemd.
4
5 Copyright 2016 Lennart Poettering
6
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
11
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
19 ***/
20
21 #include <errno.h>
22 #include <stdio.h>
23 #include <sys/stat.h>
24
25 #include "alloc-util.h"
26 #include "crypt-util.h"
27 #include "hexdecoct.h"
28 #include "log.h"
29 #include "string-util.h"
30
31 static char *arg_root_hash = NULL;
32 static char *arg_data_what = NULL;
33 static char *arg_hash_what = NULL;
34
35 static int help(void) {
36 printf("%s attach VOLUME DATADEVICE HASHDEVICE ROOTHASH\n"
37 "%s detach VOLUME\n\n"
38 "Attaches or detaches an integrity protected block device.\n",
39 program_invocation_short_name,
40 program_invocation_short_name);
41
42 return 0;
43 }
44
45 int main(int argc, char *argv[]) {
46 _cleanup_(crypt_freep) struct crypt_device *cd = NULL;
47 int r;
48
49 if (argc <= 1) {
50 r = help();
51 goto finish;
52 }
53
54 if (argc < 3) {
55 log_error("This program requires at least two arguments.");
56 r = -EINVAL;
57 goto finish;
58 }
59
60 log_set_target(LOG_TARGET_AUTO);
61 log_parse_environment();
62 log_open();
63
64 umask(0022);
65
66 if (streq(argv[1], "attach")) {
67 _cleanup_free_ void *m = NULL;
68 crypt_status_info status;
69 size_t l;
70
71 if (argc < 6) {
72 log_error("attach requires at least two arguments.");
73 r = -EINVAL;
74 goto finish;
75 }
76
77 r = unhexmem(argv[5], strlen(argv[5]), &m, &l);
78 if (r < 0) {
79 log_error("Failed to parse root hash.");
80 goto finish;
81 }
82
83 r = crypt_init(&cd, argv[4]);
84 if (r < 0) {
85 log_error_errno(r, "Failed to open verity device %s: %m", argv[4]);
86 goto finish;
87 }
88
89 crypt_set_log_callback(cd, cryptsetup_log_glue, NULL);
90
91 status = crypt_status(cd, argv[2]);
92 if (IN_SET(status, CRYPT_ACTIVE, CRYPT_BUSY)) {
93 log_info("Volume %s already active.", argv[2]);
94 r = 0;
95 goto finish;
96 }
97
98 r = crypt_load(cd, CRYPT_VERITY, NULL);
99 if (r < 0) {
100 log_error_errno(r, "Failed to load verity superblock: %m");
101 goto finish;
102 }
103
104 r = crypt_set_data_device(cd, argv[3]);
105 if (r < 0) {
106 log_error_errno(r, "Failed to configure data device: %m");
107 goto finish;
108 }
109
110 r = crypt_activate_by_volume_key(cd, argv[2], m, l, CRYPT_ACTIVATE_READONLY);
111 if (r < 0) {
112 log_error_errno(r, "Failed to set up verity device: %m");
113 goto finish;
114 }
115
116 } else if (streq(argv[1], "detach")) {
117
118 r = crypt_init_by_name(&cd, argv[2]);
119 if (r == -ENODEV) {
120 log_info("Volume %s already inactive.", argv[2]);
121 goto finish;
122 } else if (r < 0) {
123 log_error_errno(r, "crypt_init_by_name() failed: %m");
124 goto finish;
125 }
126
127 crypt_set_log_callback(cd, cryptsetup_log_glue, NULL);
128
129 r = crypt_deactivate(cd, argv[2]);
130 if (r < 0) {
131 log_error_errno(r, "Failed to deactivate: %m");
132 goto finish;
133 }
134
135 } else {
136 log_error("Unknown verb %s.", argv[1]);
137 r = -EINVAL;
138 goto finish;
139 }
140
141 r = 0;
142
143 finish:
144 free(arg_root_hash);
145 free(arg_data_what);
146 free(arg_hash_what);
147
148 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
149 }