]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/veritysetup/veritysetup.c
util-lib: export cryptsetup logging glue function
[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 <stdio.h>
22 #include <sys/stat.h>
23
24 #include "crypt-util.h"
25 #include "log.h"
26 #include "hexdecoct.h"
27 #include "string-util.h"
28 #include "alloc-util.h"
29
30 static char *arg_root_hash = NULL;
31 static char *arg_data_what = NULL;
32 static char *arg_hash_what = NULL;
33
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);
40
41 return 0;
42 }
43
44 int main(int argc, char *argv[]) {
45 _cleanup_(crypt_freep) struct crypt_device *cd = NULL;
46 int r;
47
48 if (argc <= 1) {
49 r = help();
50 goto finish;
51 }
52
53 if (argc < 3) {
54 log_error("This program requires at least two arguments.");
55 r = -EINVAL;
56 goto finish;
57 }
58
59 log_set_target(LOG_TARGET_AUTO);
60 log_parse_environment();
61 log_open();
62
63 umask(0022);
64
65 if (streq(argv[1], "attach")) {
66 _cleanup_free_ void *m = NULL;
67 crypt_status_info status;
68 size_t l;
69
70 if (argc < 6) {
71 log_error("attach requires at least two arguments.");
72 r = -EINVAL;
73 goto finish;
74 }
75
76 r = unhexmem(argv[5], strlen(argv[5]), &m, &l);
77 if (r < 0) {
78 log_error("Failed to parse root hash.");
79 goto finish;
80 }
81
82 r = crypt_init(&cd, argv[4]);
83 if (r < 0) {
84 log_error_errno(r, "Failed to open verity device %s: %m", argv[4]);
85 goto finish;
86 }
87
88 crypt_set_log_callback(cd, cryptsetup_log_glue, NULL);
89
90 status = crypt_status(cd, argv[2]);
91 if (IN_SET(status, CRYPT_ACTIVE, CRYPT_BUSY)) {
92 log_info("Volume %s already active.", argv[2]);
93 r = 0;
94 goto finish;
95 }
96
97 r = crypt_load(cd, CRYPT_VERITY, NULL);
98 if (r < 0) {
99 log_error_errno(r, "Failed to load verity superblock: %m");
100 goto finish;
101 }
102
103 r = crypt_set_data_device(cd, argv[3]);
104 if (r < 0) {
105 log_error_errno(r, "Failed to configure data device: %m");
106 goto finish;
107 }
108
109 r = crypt_activate_by_volume_key(cd, argv[2], m, l, CRYPT_ACTIVATE_READONLY);
110 if (r < 0) {
111 log_error_errno(r, "Failed to set up verity device: %m");
112 goto finish;
113 }
114
115 } else if (streq(argv[1], "detach")) {
116
117 r = crypt_init_by_name(&cd, argv[2]);
118 if (r == -ENODEV) {
119 log_info("Volume %s already inactive.", argv[2]);
120 goto finish;
121 } else if (r < 0) {
122 log_error_errno(r, "crypt_init_by_name() failed: %m");
123 goto finish;
124 }
125
126 crypt_set_log_callback(cd, cryptsetup_log_glue, NULL);
127
128 r = crypt_deactivate(cd, argv[2]);
129 if (r < 0) {
130 log_error_errno(r, "Failed to deactivate: %m");
131 goto finish;
132 }
133
134 } else {
135 log_error("Unknown verb %s.", argv[1]);
136 r = -EINVAL;
137 goto finish;
138 }
139
140 r = 0;
141
142 finish:
143 free(arg_root_hash);
144 free(arg_data_what);
145 free(arg_hash_what);
146
147 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
148 }