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/>.
21 #include <libcryptsetup.h>
26 #include "hexdecoct.h"
27 #include "string-util.h"
28 #include "alloc-util.h"
30 static char *arg_root_hash
= NULL
;
31 static char *arg_data_what
= NULL
;
32 static char *arg_hash_what
= NULL
;
34 static int help(void) {
35 printf("%s attach VOLUME DATADEVICE HASHDEVICE ROOTHASH\n"
36 "%s detach VOLUME\n\n"
37 "Attaches or detaches an integrity protected block device.\n",
38 program_invocation_short_name
,
39 program_invocation_short_name
);
44 static void log_glue(int level
, const char *msg
, void *usrptr
) {
48 int main(int argc
, char *argv
[]) {
49 struct crypt_device
*cd
= NULL
;
58 log_error("This program requires at least two arguments.");
63 log_set_target(LOG_TARGET_AUTO
);
64 log_parse_environment();
69 if (streq(argv
[1], "attach")) {
70 _cleanup_free_
void *m
= NULL
;
71 crypt_status_info status
;
75 log_error("attach requires at least two arguments.");
80 r
= unhexmem(argv
[5], strlen(argv
[5]), &m
, &l
);
82 log_error("Failed to parse root hash.");
86 r
= crypt_init(&cd
, argv
[4]);
88 log_error_errno(r
, "Failed to open verity device %s: %m", argv
[4]);
92 crypt_set_log_callback(cd
, log_glue
, NULL
);
94 status
= crypt_status(cd
, argv
[2]);
95 if (IN_SET(status
, CRYPT_ACTIVE
, CRYPT_BUSY
)) {
96 log_info("Volume %s already active.", argv
[2]);
101 r
= crypt_load(cd
, CRYPT_VERITY
, NULL
);
103 log_error_errno(r
, "Failed to load verity superblock: %m");
107 r
= crypt_set_data_device(cd
, argv
[3]);
109 log_error_errno(r
, "Failed to configure data device: %m");
113 r
= crypt_activate_by_volume_key(cd
, argv
[2], m
, l
, CRYPT_ACTIVATE_READONLY
);
115 log_error_errno(r
, "Failed to set up verity device: %m");
119 } else if (streq(argv
[1], "detach")) {
121 r
= crypt_init_by_name(&cd
, argv
[2]);
123 log_info("Volume %s already inactive.", argv
[2]);
126 log_error_errno(r
, "crypt_init_by_name() failed: %m");
130 crypt_set_log_callback(cd
, log_glue
, NULL
);
132 r
= crypt_deactivate(cd
, argv
[2]);
134 log_error_errno(r
, "Failed to deactivate: %m");
139 log_error("Unknown verb %s.", argv
[1]);
154 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;