1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2016 Lennart Poettering
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.
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.
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/>.
25 #include "alloc-util.h"
26 #include "crypt-util.h"
27 #include "hexdecoct.h"
29 #include "string-util.h"
31 static char *arg_root_hash
= NULL
;
32 static char *arg_data_what
= NULL
;
33 static char *arg_hash_what
= NULL
;
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
);
45 int main(int argc
, char *argv
[]) {
46 _cleanup_(crypt_freep
) struct crypt_device
*cd
= NULL
;
55 log_error("This program requires at least two arguments.");
60 log_set_target(LOG_TARGET_AUTO
);
61 log_parse_environment();
66 if (streq(argv
[1], "attach")) {
67 _cleanup_free_
void *m
= NULL
;
68 crypt_status_info status
;
72 log_error("attach requires at least two arguments.");
77 r
= unhexmem(argv
[5], strlen(argv
[5]), &m
, &l
);
79 log_error("Failed to parse root hash.");
83 r
= crypt_init(&cd
, argv
[4]);
85 log_error_errno(r
, "Failed to open verity device %s: %m", argv
[4]);
89 crypt_set_log_callback(cd
, cryptsetup_log_glue
, NULL
);
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]);
98 r
= crypt_load(cd
, CRYPT_VERITY
, NULL
);
100 log_error_errno(r
, "Failed to load verity superblock: %m");
104 r
= crypt_set_data_device(cd
, argv
[3]);
106 log_error_errno(r
, "Failed to configure data device: %m");
110 r
= crypt_activate_by_volume_key(cd
, argv
[2], m
, l
, CRYPT_ACTIVATE_READONLY
);
112 log_error_errno(r
, "Failed to set up verity device: %m");
116 } else if (streq(argv
[1], "detach")) {
118 r
= crypt_init_by_name(&cd
, argv
[2]);
120 log_info("Volume %s already inactive.", argv
[2]);
123 log_error_errno(r
, "crypt_init_by_name() failed: %m");
127 crypt_set_log_callback(cd
, cryptsetup_log_glue
, NULL
);
129 r
= crypt_deactivate(cd
, argv
[2]);
131 log_error_errno(r
, "Failed to deactivate: %m");
136 log_error("Unknown verb %s.", argv
[1]);
148 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;