1e4b0a90eSBrooks Davis /*-
24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
3e4b0a90eSBrooks Davis *
42f07cdf8SPawel Jakub Dawidek * Copyright (c) 2004-2019 Pawel Jakub Dawidek <pawel@dawidek.net>
5e4b0a90eSBrooks Davis * All rights reserved.
6e4b0a90eSBrooks Davis *
7e4b0a90eSBrooks Davis * Redistribution and use in source and binary forms, with or without
8e4b0a90eSBrooks Davis * modification, are permitted provided that the following conditions
9e4b0a90eSBrooks Davis * are met:
10e4b0a90eSBrooks Davis * 1. Redistributions of source code must retain the above copyright
11e4b0a90eSBrooks Davis * notice, this list of conditions and the following disclaimer.
12e4b0a90eSBrooks Davis * 2. Redistributions in binary form must reproduce the above copyright
13e4b0a90eSBrooks Davis * notice, this list of conditions and the following disclaimer in the
14e4b0a90eSBrooks Davis * documentation and/or other materials provided with the distribution.
15e4b0a90eSBrooks Davis *
16e4b0a90eSBrooks Davis * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
17e4b0a90eSBrooks Davis * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18e4b0a90eSBrooks Davis * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19e4b0a90eSBrooks Davis * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
20e4b0a90eSBrooks Davis * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21e4b0a90eSBrooks Davis * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22e4b0a90eSBrooks Davis * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23e4b0a90eSBrooks Davis * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24e4b0a90eSBrooks Davis * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25e4b0a90eSBrooks Davis * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26e4b0a90eSBrooks Davis * SUCH DAMAGE.
27e4b0a90eSBrooks Davis */
28e4b0a90eSBrooks Davis
29e4b0a90eSBrooks Davis #include <sys/param.h>
30e4b0a90eSBrooks Davis #include <sys/mman.h>
31e4b0a90eSBrooks Davis #include <sys/sysctl.h>
32e4b0a90eSBrooks Davis #include <sys/resource.h>
33e4b0a90eSBrooks Davis #include <opencrypto/cryptodev.h>
34e4b0a90eSBrooks Davis
35e4b0a90eSBrooks Davis #include <assert.h>
36e4b0a90eSBrooks Davis #include <err.h>
37e4b0a90eSBrooks Davis #include <errno.h>
38e4b0a90eSBrooks Davis #include <fcntl.h>
39e4b0a90eSBrooks Davis #include <libgeom.h>
40e4b0a90eSBrooks Davis #include <paths.h>
41e4b0a90eSBrooks Davis #include <readpassphrase.h>
42e4b0a90eSBrooks Davis #include <stdbool.h>
43e4b0a90eSBrooks Davis #include <stdint.h>
44e4b0a90eSBrooks Davis #include <stdio.h>
45e4b0a90eSBrooks Davis #include <stdlib.h>
46e4b0a90eSBrooks Davis #include <string.h>
47e4b0a90eSBrooks Davis #include <strings.h>
48e4b0a90eSBrooks Davis #include <unistd.h>
49e4b0a90eSBrooks Davis
50e4b0a90eSBrooks Davis #include <geom/eli/g_eli.h>
51e4b0a90eSBrooks Davis #include <geom/eli/pkcs5v2.h>
52e4b0a90eSBrooks Davis
53e4b0a90eSBrooks Davis #include "core/geom.h"
54e4b0a90eSBrooks Davis #include "misc/subr.h"
55e4b0a90eSBrooks Davis
56e4b0a90eSBrooks Davis
57e4b0a90eSBrooks Davis uint32_t lib_version = G_LIB_VERSION;
58e4b0a90eSBrooks Davis uint32_t version = G_ELI_VERSION;
59e4b0a90eSBrooks Davis
60e4b0a90eSBrooks Davis #define GELI_BACKUP_DIR "/var/backups/"
61e4b0a90eSBrooks Davis #define GELI_ENC_ALGO "aes"
62217df2daSBen Woods #define BUFSIZE 1024
63217df2daSBen Woods
64217df2daSBen Woods /*
65217df2daSBen Woods * Passphrase cached when attaching multiple providers, in order to be more
66217df2daSBen Woods * user-friendly if they are using the same passphrase.
67217df2daSBen Woods */
68217df2daSBen Woods static char cached_passphrase[BUFSIZE] = "";
69e4b0a90eSBrooks Davis
70e4b0a90eSBrooks Davis static void eli_main(struct gctl_req *req, unsigned flags);
71e4b0a90eSBrooks Davis static void eli_init(struct gctl_req *req);
72e4b0a90eSBrooks Davis static void eli_attach(struct gctl_req *req);
73e4b0a90eSBrooks Davis static void eli_configure(struct gctl_req *req);
74e4b0a90eSBrooks Davis static void eli_setkey(struct gctl_req *req);
75e4b0a90eSBrooks Davis static void eli_delkey(struct gctl_req *req);
76e4b0a90eSBrooks Davis static void eli_resume(struct gctl_req *req);
77e4b0a90eSBrooks Davis static void eli_kill(struct gctl_req *req);
78e4b0a90eSBrooks Davis static void eli_backup(struct gctl_req *req);
79e4b0a90eSBrooks Davis static void eli_restore(struct gctl_req *req);
80e4b0a90eSBrooks Davis static void eli_resize(struct gctl_req *req);
81e4b0a90eSBrooks Davis static void eli_version(struct gctl_req *req);
82e4b0a90eSBrooks Davis static void eli_clear(struct gctl_req *req);
83e4b0a90eSBrooks Davis static void eli_dump(struct gctl_req *req);
84e4b0a90eSBrooks Davis
85e4b0a90eSBrooks Davis static int eli_backup_create(struct gctl_req *req, const char *prov,
86e4b0a90eSBrooks Davis const char *file);
87e4b0a90eSBrooks Davis
88e4b0a90eSBrooks Davis /*
89e4b0a90eSBrooks Davis * Available commands:
90e4b0a90eSBrooks Davis *
912f07cdf8SPawel Jakub Dawidek * init [-bdgPRTv] [-a aalgo] [-B backupfile] [-e ealgo] [-i iterations] [-l keylen] [-J newpassfile] [-K newkeyfile] [-s sectorsize] [-V version] prov ...
92e4b0a90eSBrooks Davis * label - alias for 'init'
93217df2daSBen Woods * attach [-Cdprv] [-n keyno] [-j passfile] [-k keyfile] prov ...
94e4b0a90eSBrooks Davis * detach [-fl] prov ...
95e4b0a90eSBrooks Davis * stop - alias for 'detach'
962f07cdf8SPawel Jakub Dawidek * onetime [-dRT] [-a aalgo] [-e ealgo] [-l keylen] prov
972f07cdf8SPawel Jakub Dawidek * configure [-bBgGrRtT] prov ...
98e4b0a90eSBrooks Davis * setkey [-pPv] [-n keyno] [-j passfile] [-J newpassfile] [-k keyfile] [-K newkeyfile] prov
99e4b0a90eSBrooks Davis * delkey [-afv] [-n keyno] prov
100e4b0a90eSBrooks Davis * suspend [-v] -a | prov ...
101e4b0a90eSBrooks Davis * resume [-pv] [-j passfile] [-k keyfile] prov
102e4b0a90eSBrooks Davis * kill [-av] [prov ...]
103e4b0a90eSBrooks Davis * backup [-v] prov file
104e4b0a90eSBrooks Davis * restore [-fv] file prov
105e4b0a90eSBrooks Davis * resize [-v] -s oldsize prov
106e4b0a90eSBrooks Davis * version [prov ...]
107e4b0a90eSBrooks Davis * clear [-v] prov ...
108e4b0a90eSBrooks Davis * dump [-v] prov ...
109e4b0a90eSBrooks Davis */
110e4b0a90eSBrooks Davis struct g_command class_commands[] = {
111e4b0a90eSBrooks Davis { "init", G_FLAG_VERBOSE, eli_main,
112e4b0a90eSBrooks Davis {
113e4b0a90eSBrooks Davis { 'a', "aalgo", "", G_TYPE_STRING },
114e4b0a90eSBrooks Davis { 'b', "boot", NULL, G_TYPE_BOOL },
115e4b0a90eSBrooks Davis { 'B', "backupfile", "", G_TYPE_STRING },
116e4b0a90eSBrooks Davis { 'd', "displaypass", NULL, G_TYPE_BOOL },
117e4b0a90eSBrooks Davis { 'e', "ealgo", "", G_TYPE_STRING },
118e4b0a90eSBrooks Davis { 'g', "geliboot", NULL, G_TYPE_BOOL },
119e4b0a90eSBrooks Davis { 'i', "iterations", "-1", G_TYPE_NUMBER },
120e4b0a90eSBrooks Davis { 'J', "newpassfile", G_VAL_OPTIONAL, G_TYPE_STRING | G_TYPE_MULTI },
121e4b0a90eSBrooks Davis { 'K', "newkeyfile", G_VAL_OPTIONAL, G_TYPE_STRING | G_TYPE_MULTI },
122e4b0a90eSBrooks Davis { 'l', "keylen", "0", G_TYPE_NUMBER },
123e4b0a90eSBrooks Davis { 'P', "nonewpassphrase", NULL, G_TYPE_BOOL },
1242f07cdf8SPawel Jakub Dawidek { 'R', "noautoresize", NULL, G_TYPE_BOOL },
125e4b0a90eSBrooks Davis { 's', "sectorsize", "0", G_TYPE_NUMBER },
126e4b0a90eSBrooks Davis { 'T', "notrim", NULL, G_TYPE_BOOL },
127e4b0a90eSBrooks Davis { 'V', "mdversion", "-1", G_TYPE_NUMBER },
128e4b0a90eSBrooks Davis G_OPT_SENTINEL
129e4b0a90eSBrooks Davis },
1302f07cdf8SPawel Jakub Dawidek "[-bdgPRTv] [-a aalgo] [-B backupfile] [-e ealgo] [-i iterations] [-l keylen] [-J newpassfile] [-K newkeyfile] [-s sectorsize] [-V version] prov ..."
131e4b0a90eSBrooks Davis },
132e4b0a90eSBrooks Davis { "label", G_FLAG_VERBOSE, eli_main,
133e4b0a90eSBrooks Davis {
134e4b0a90eSBrooks Davis { 'a', "aalgo", "", G_TYPE_STRING },
135e4b0a90eSBrooks Davis { 'b', "boot", NULL, G_TYPE_BOOL },
136e4b0a90eSBrooks Davis { 'B', "backupfile", "", G_TYPE_STRING },
137e4b0a90eSBrooks Davis { 'd', "displaypass", NULL, G_TYPE_BOOL },
138e4b0a90eSBrooks Davis { 'e', "ealgo", "", G_TYPE_STRING },
139e4b0a90eSBrooks Davis { 'g', "geliboot", NULL, G_TYPE_BOOL },
140e4b0a90eSBrooks Davis { 'i', "iterations", "-1", G_TYPE_NUMBER },
141e4b0a90eSBrooks Davis { 'J', "newpassfile", G_VAL_OPTIONAL, G_TYPE_STRING | G_TYPE_MULTI },
142e4b0a90eSBrooks Davis { 'K', "newkeyfile", G_VAL_OPTIONAL, G_TYPE_STRING | G_TYPE_MULTI },
143e4b0a90eSBrooks Davis { 'l', "keylen", "0", G_TYPE_NUMBER },
144e4b0a90eSBrooks Davis { 'P', "nonewpassphrase", NULL, G_TYPE_BOOL },
1452f07cdf8SPawel Jakub Dawidek { 'R', "noautoresize", NULL, G_TYPE_BOOL },
146e4b0a90eSBrooks Davis { 's', "sectorsize", "0", G_TYPE_NUMBER },
147b4f850c0SPawel Jakub Dawidek { 'T', "notrim", NULL, G_TYPE_BOOL },
148e4b0a90eSBrooks Davis { 'V', "mdversion", "-1", G_TYPE_NUMBER },
149e4b0a90eSBrooks Davis G_OPT_SENTINEL
150e4b0a90eSBrooks Davis },
151e4b0a90eSBrooks Davis "- an alias for 'init'"
152e4b0a90eSBrooks Davis },
153e4b0a90eSBrooks Davis { "attach", G_FLAG_VERBOSE | G_FLAG_LOADKLD, eli_main,
154e4b0a90eSBrooks Davis {
155e4b0a90eSBrooks Davis { 'C', "dryrun", NULL, G_TYPE_BOOL },
156e4b0a90eSBrooks Davis { 'd', "detach", NULL, G_TYPE_BOOL },
157e4b0a90eSBrooks Davis { 'j', "passfile", G_VAL_OPTIONAL, G_TYPE_STRING | G_TYPE_MULTI },
158e4b0a90eSBrooks Davis { 'k', "keyfile", G_VAL_OPTIONAL, G_TYPE_STRING | G_TYPE_MULTI },
159e4b0a90eSBrooks Davis { 'n', "keyno", "-1", G_TYPE_NUMBER },
160e4b0a90eSBrooks Davis { 'p', "nopassphrase", NULL, G_TYPE_BOOL },
161e4b0a90eSBrooks Davis { 'r', "readonly", NULL, G_TYPE_BOOL },
162e4b0a90eSBrooks Davis G_OPT_SENTINEL
163e4b0a90eSBrooks Davis },
164217df2daSBen Woods "[-Cdprv] [-n keyno] [-j passfile] [-k keyfile] prov ..."
165e4b0a90eSBrooks Davis },
166e4b0a90eSBrooks Davis { "detach", 0, NULL,
167e4b0a90eSBrooks Davis {
168e4b0a90eSBrooks Davis { 'f', "force", NULL, G_TYPE_BOOL },
169e4b0a90eSBrooks Davis { 'l', "last", NULL, G_TYPE_BOOL },
170e4b0a90eSBrooks Davis G_OPT_SENTINEL
171e4b0a90eSBrooks Davis },
172e4b0a90eSBrooks Davis "[-fl] prov ..."
173e4b0a90eSBrooks Davis },
174e4b0a90eSBrooks Davis { "stop", 0, NULL,
175e4b0a90eSBrooks Davis {
176e4b0a90eSBrooks Davis { 'f', "force", NULL, G_TYPE_BOOL },
177e4b0a90eSBrooks Davis { 'l', "last", NULL, G_TYPE_BOOL },
178e4b0a90eSBrooks Davis G_OPT_SENTINEL
179e4b0a90eSBrooks Davis },
180e4b0a90eSBrooks Davis "- an alias for 'detach'"
181e4b0a90eSBrooks Davis },
182e4b0a90eSBrooks Davis { "onetime", G_FLAG_VERBOSE | G_FLAG_LOADKLD, NULL,
183e4b0a90eSBrooks Davis {
184e4b0a90eSBrooks Davis { 'a', "aalgo", "", G_TYPE_STRING },
185e4b0a90eSBrooks Davis { 'd', "detach", NULL, G_TYPE_BOOL },
186e4b0a90eSBrooks Davis { 'e', "ealgo", GELI_ENC_ALGO, G_TYPE_STRING },
187e4b0a90eSBrooks Davis { 'l', "keylen", "0", G_TYPE_NUMBER },
1882f07cdf8SPawel Jakub Dawidek { 'R', "noautoresize", NULL, G_TYPE_BOOL },
189e4b0a90eSBrooks Davis { 's', "sectorsize", "0", G_TYPE_NUMBER },
190e4b0a90eSBrooks Davis { 'T', "notrim", NULL, G_TYPE_BOOL },
191e4b0a90eSBrooks Davis G_OPT_SENTINEL
192e4b0a90eSBrooks Davis },
1932f07cdf8SPawel Jakub Dawidek "[-dRT] [-a aalgo] [-e ealgo] [-l keylen] [-s sectorsize] prov"
194e4b0a90eSBrooks Davis },
195e4b0a90eSBrooks Davis { "configure", G_FLAG_VERBOSE, eli_main,
196e4b0a90eSBrooks Davis {
197e4b0a90eSBrooks Davis { 'b', "boot", NULL, G_TYPE_BOOL },
198e4b0a90eSBrooks Davis { 'B', "noboot", NULL, G_TYPE_BOOL },
199e4b0a90eSBrooks Davis { 'd', "displaypass", NULL, G_TYPE_BOOL },
200e4b0a90eSBrooks Davis { 'D', "nodisplaypass", NULL, G_TYPE_BOOL },
201e4b0a90eSBrooks Davis { 'g', "geliboot", NULL, G_TYPE_BOOL },
202e4b0a90eSBrooks Davis { 'G', "nogeliboot", NULL, G_TYPE_BOOL },
2032f07cdf8SPawel Jakub Dawidek { 'r', "autoresize", NULL, G_TYPE_BOOL },
2042f07cdf8SPawel Jakub Dawidek { 'R', "noautoresize", NULL, G_TYPE_BOOL },
205e4b0a90eSBrooks Davis { 't', "trim", NULL, G_TYPE_BOOL },
206e4b0a90eSBrooks Davis { 'T', "notrim", NULL, G_TYPE_BOOL },
207e4b0a90eSBrooks Davis G_OPT_SENTINEL
208e4b0a90eSBrooks Davis },
2092f07cdf8SPawel Jakub Dawidek "[-bBdDgGrRtT] prov ..."
210e4b0a90eSBrooks Davis },
211e4b0a90eSBrooks Davis { "setkey", G_FLAG_VERBOSE, eli_main,
212e4b0a90eSBrooks Davis {
213e4b0a90eSBrooks Davis { 'i', "iterations", "-1", G_TYPE_NUMBER },
214e4b0a90eSBrooks Davis { 'j', "passfile", G_VAL_OPTIONAL, G_TYPE_STRING | G_TYPE_MULTI },
215e4b0a90eSBrooks Davis { 'J', "newpassfile", G_VAL_OPTIONAL, G_TYPE_STRING | G_TYPE_MULTI },
216e4b0a90eSBrooks Davis { 'k', "keyfile", G_VAL_OPTIONAL, G_TYPE_STRING | G_TYPE_MULTI },
217e4b0a90eSBrooks Davis { 'K', "newkeyfile", G_VAL_OPTIONAL, G_TYPE_STRING | G_TYPE_MULTI },
218e4b0a90eSBrooks Davis { 'n', "keyno", "-1", G_TYPE_NUMBER },
219e4b0a90eSBrooks Davis { 'p', "nopassphrase", NULL, G_TYPE_BOOL },
220e4b0a90eSBrooks Davis { 'P', "nonewpassphrase", NULL, G_TYPE_BOOL },
221e4b0a90eSBrooks Davis G_OPT_SENTINEL
222e4b0a90eSBrooks Davis },
223e4b0a90eSBrooks Davis "[-pPv] [-n keyno] [-i iterations] [-j passfile] [-J newpassfile] [-k keyfile] [-K newkeyfile] prov"
224e4b0a90eSBrooks Davis },
225e4b0a90eSBrooks Davis { "delkey", G_FLAG_VERBOSE, eli_main,
226e4b0a90eSBrooks Davis {
227e4b0a90eSBrooks Davis { 'a', "all", NULL, G_TYPE_BOOL },
228e4b0a90eSBrooks Davis { 'f', "force", NULL, G_TYPE_BOOL },
229e4b0a90eSBrooks Davis { 'n', "keyno", "-1", G_TYPE_NUMBER },
230e4b0a90eSBrooks Davis G_OPT_SENTINEL
231e4b0a90eSBrooks Davis },
232e4b0a90eSBrooks Davis "[-afv] [-n keyno] prov"
233e4b0a90eSBrooks Davis },
234e4b0a90eSBrooks Davis { "suspend", G_FLAG_VERBOSE, NULL,
235e4b0a90eSBrooks Davis {
236e4b0a90eSBrooks Davis { 'a', "all", NULL, G_TYPE_BOOL },
237e4b0a90eSBrooks Davis G_OPT_SENTINEL
238e4b0a90eSBrooks Davis },
239e4b0a90eSBrooks Davis "[-v] -a | prov ..."
240e4b0a90eSBrooks Davis },
241e4b0a90eSBrooks Davis { "resume", G_FLAG_VERBOSE, eli_main,
242e4b0a90eSBrooks Davis {
243e4b0a90eSBrooks Davis { 'j', "passfile", G_VAL_OPTIONAL, G_TYPE_STRING | G_TYPE_MULTI },
244e4b0a90eSBrooks Davis { 'k', "keyfile", G_VAL_OPTIONAL, G_TYPE_STRING | G_TYPE_MULTI },
245e4b0a90eSBrooks Davis { 'p', "nopassphrase", NULL, G_TYPE_BOOL },
246e4b0a90eSBrooks Davis G_OPT_SENTINEL
247e4b0a90eSBrooks Davis },
248e4b0a90eSBrooks Davis "[-pv] [-j passfile] [-k keyfile] prov"
249e4b0a90eSBrooks Davis },
250e4b0a90eSBrooks Davis { "kill", G_FLAG_VERBOSE, eli_main,
251e4b0a90eSBrooks Davis {
252e4b0a90eSBrooks Davis { 'a', "all", NULL, G_TYPE_BOOL },
253e4b0a90eSBrooks Davis G_OPT_SENTINEL
254e4b0a90eSBrooks Davis },
255e4b0a90eSBrooks Davis "[-av] [prov ...]"
256e4b0a90eSBrooks Davis },
257e4b0a90eSBrooks Davis { "backup", G_FLAG_VERBOSE, eli_main, G_NULL_OPTS,
258e4b0a90eSBrooks Davis "[-v] prov file"
259e4b0a90eSBrooks Davis },
260e4b0a90eSBrooks Davis { "restore", G_FLAG_VERBOSE, eli_main,
261e4b0a90eSBrooks Davis {
262e4b0a90eSBrooks Davis { 'f', "force", NULL, G_TYPE_BOOL },
263e4b0a90eSBrooks Davis G_OPT_SENTINEL
264e4b0a90eSBrooks Davis },
265e4b0a90eSBrooks Davis "[-fv] file prov"
266e4b0a90eSBrooks Davis },
267e4b0a90eSBrooks Davis { "resize", G_FLAG_VERBOSE, eli_main,
268e4b0a90eSBrooks Davis {
269e4b0a90eSBrooks Davis { 's', "oldsize", NULL, G_TYPE_NUMBER },
270e4b0a90eSBrooks Davis G_OPT_SENTINEL
271e4b0a90eSBrooks Davis },
272e4b0a90eSBrooks Davis "[-v] -s oldsize prov"
273e4b0a90eSBrooks Davis },
274e4b0a90eSBrooks Davis { "version", G_FLAG_LOADKLD, eli_main, G_NULL_OPTS,
275e4b0a90eSBrooks Davis "[prov ...]"
276e4b0a90eSBrooks Davis },
277e4b0a90eSBrooks Davis { "clear", G_FLAG_VERBOSE, eli_main, G_NULL_OPTS,
278e4b0a90eSBrooks Davis "[-v] prov ..."
279e4b0a90eSBrooks Davis },
280e4b0a90eSBrooks Davis { "dump", G_FLAG_VERBOSE, eli_main, G_NULL_OPTS,
281e4b0a90eSBrooks Davis "[-v] prov ..."
282e4b0a90eSBrooks Davis },
283e4b0a90eSBrooks Davis G_CMD_SENTINEL
284e4b0a90eSBrooks Davis };
285e4b0a90eSBrooks Davis
286e4b0a90eSBrooks Davis static int verbose = 0;
287e4b0a90eSBrooks Davis
288e4b0a90eSBrooks Davis static int
eli_protect(struct gctl_req * req)289e4b0a90eSBrooks Davis eli_protect(struct gctl_req *req)
290e4b0a90eSBrooks Davis {
291e4b0a90eSBrooks Davis struct rlimit rl;
292e4b0a90eSBrooks Davis
293e4b0a90eSBrooks Davis /* Disable core dumps. */
294e4b0a90eSBrooks Davis rl.rlim_cur = 0;
295e4b0a90eSBrooks Davis rl.rlim_max = 0;
296e4b0a90eSBrooks Davis if (setrlimit(RLIMIT_CORE, &rl) == -1) {
297e4b0a90eSBrooks Davis gctl_error(req, "Cannot disable core dumps: %s.",
298e4b0a90eSBrooks Davis strerror(errno));
299e4b0a90eSBrooks Davis return (-1);
300e4b0a90eSBrooks Davis }
301e4b0a90eSBrooks Davis /* Disable swapping. */
302e4b0a90eSBrooks Davis if (mlockall(MCL_FUTURE) == -1) {
303e4b0a90eSBrooks Davis gctl_error(req, "Cannot lock memory: %s.", strerror(errno));
304e4b0a90eSBrooks Davis return (-1);
305e4b0a90eSBrooks Davis }
306e4b0a90eSBrooks Davis return (0);
307e4b0a90eSBrooks Davis }
308e4b0a90eSBrooks Davis
309e4b0a90eSBrooks Davis static void
eli_main(struct gctl_req * req,unsigned int flags)310e4b0a90eSBrooks Davis eli_main(struct gctl_req *req, unsigned int flags)
311e4b0a90eSBrooks Davis {
312e4b0a90eSBrooks Davis const char *name;
313e4b0a90eSBrooks Davis
314e4b0a90eSBrooks Davis if (eli_protect(req) == -1)
315e4b0a90eSBrooks Davis return;
316e4b0a90eSBrooks Davis
317e4b0a90eSBrooks Davis if ((flags & G_FLAG_VERBOSE) != 0)
318e4b0a90eSBrooks Davis verbose = 1;
319e4b0a90eSBrooks Davis
320e4b0a90eSBrooks Davis name = gctl_get_ascii(req, "verb");
321e4b0a90eSBrooks Davis if (name == NULL) {
322e4b0a90eSBrooks Davis gctl_error(req, "No '%s' argument.", "verb");
323e4b0a90eSBrooks Davis return;
324e4b0a90eSBrooks Davis }
325e4b0a90eSBrooks Davis if (strcmp(name, "init") == 0 || strcmp(name, "label") == 0)
326e4b0a90eSBrooks Davis eli_init(req);
327e4b0a90eSBrooks Davis else if (strcmp(name, "attach") == 0)
328e4b0a90eSBrooks Davis eli_attach(req);
329e4b0a90eSBrooks Davis else if (strcmp(name, "configure") == 0)
330e4b0a90eSBrooks Davis eli_configure(req);
331e4b0a90eSBrooks Davis else if (strcmp(name, "setkey") == 0)
332e4b0a90eSBrooks Davis eli_setkey(req);
333e4b0a90eSBrooks Davis else if (strcmp(name, "delkey") == 0)
334e4b0a90eSBrooks Davis eli_delkey(req);
335e4b0a90eSBrooks Davis else if (strcmp(name, "resume") == 0)
336e4b0a90eSBrooks Davis eli_resume(req);
337e4b0a90eSBrooks Davis else if (strcmp(name, "kill") == 0)
338e4b0a90eSBrooks Davis eli_kill(req);
339e4b0a90eSBrooks Davis else if (strcmp(name, "backup") == 0)
340e4b0a90eSBrooks Davis eli_backup(req);
341e4b0a90eSBrooks Davis else if (strcmp(name, "restore") == 0)
342e4b0a90eSBrooks Davis eli_restore(req);
343e4b0a90eSBrooks Davis else if (strcmp(name, "resize") == 0)
344e4b0a90eSBrooks Davis eli_resize(req);
345e4b0a90eSBrooks Davis else if (strcmp(name, "version") == 0)
346e4b0a90eSBrooks Davis eli_version(req);
347e4b0a90eSBrooks Davis else if (strcmp(name, "dump") == 0)
348e4b0a90eSBrooks Davis eli_dump(req);
349e4b0a90eSBrooks Davis else if (strcmp(name, "clear") == 0)
350e4b0a90eSBrooks Davis eli_clear(req);
351e4b0a90eSBrooks Davis else
352e4b0a90eSBrooks Davis gctl_error(req, "Unknown command: %s.", name);
353e4b0a90eSBrooks Davis }
354e4b0a90eSBrooks Davis
355e4b0a90eSBrooks Davis static bool
eli_is_attached(const char * prov)356e4b0a90eSBrooks Davis eli_is_attached(const char *prov)
357e4b0a90eSBrooks Davis {
358e4b0a90eSBrooks Davis char name[MAXPATHLEN];
359e4b0a90eSBrooks Davis
360e4b0a90eSBrooks Davis /*
361e4b0a90eSBrooks Davis * Not the best way to do it, but the easiest.
362e4b0a90eSBrooks Davis * We try to open provider and check if it is a GEOM provider
363e4b0a90eSBrooks Davis * by asking about its sectorsize.
364e4b0a90eSBrooks Davis */
365e4b0a90eSBrooks Davis snprintf(name, sizeof(name), "%s%s", prov, G_ELI_SUFFIX);
366e4b0a90eSBrooks Davis return (g_get_sectorsize(name) > 0);
367e4b0a90eSBrooks Davis }
368e4b0a90eSBrooks Davis
369e4b0a90eSBrooks Davis static int
eli_genkey_files(struct gctl_req * req,bool new,const char * type,struct hmac_ctx * ctxp,char * passbuf,size_t passbufsize)370e4b0a90eSBrooks Davis eli_genkey_files(struct gctl_req *req, bool new, const char *type,
371e4b0a90eSBrooks Davis struct hmac_ctx *ctxp, char *passbuf, size_t passbufsize)
372e4b0a90eSBrooks Davis {
373e4b0a90eSBrooks Davis char *p, buf[BUFSIZE], argname[16];
374e4b0a90eSBrooks Davis const char *file;
375e4b0a90eSBrooks Davis int error, fd, i;
376e4b0a90eSBrooks Davis ssize_t done;
377e4b0a90eSBrooks Davis
378e4b0a90eSBrooks Davis assert((strcmp(type, "keyfile") == 0 && ctxp != NULL &&
379e4b0a90eSBrooks Davis passbuf == NULL && passbufsize == 0) ||
380e4b0a90eSBrooks Davis (strcmp(type, "passfile") == 0 && ctxp == NULL &&
381e4b0a90eSBrooks Davis passbuf != NULL && passbufsize > 0));
382e4b0a90eSBrooks Davis assert(strcmp(type, "keyfile") == 0 || passbuf[0] == '\0');
383e4b0a90eSBrooks Davis
384e4b0a90eSBrooks Davis for (i = 0; ; i++) {
385e4b0a90eSBrooks Davis snprintf(argname, sizeof(argname), "%s%s%d",
386e4b0a90eSBrooks Davis new ? "new" : "", type, i);
387e4b0a90eSBrooks Davis
388e4b0a90eSBrooks Davis /* No more {key,pass}files? */
389e4b0a90eSBrooks Davis if (!gctl_has_param(req, argname))
390e4b0a90eSBrooks Davis return (i);
391e4b0a90eSBrooks Davis
392e4b0a90eSBrooks Davis file = gctl_get_ascii(req, "%s", argname);
393e4b0a90eSBrooks Davis assert(file != NULL);
394e4b0a90eSBrooks Davis
395e4b0a90eSBrooks Davis if (strcmp(file, "-") == 0)
396e4b0a90eSBrooks Davis fd = STDIN_FILENO;
397e4b0a90eSBrooks Davis else {
398e4b0a90eSBrooks Davis fd = open(file, O_RDONLY);
399e4b0a90eSBrooks Davis if (fd == -1) {
400e4b0a90eSBrooks Davis gctl_error(req, "Cannot open %s %s: %s.",
401e4b0a90eSBrooks Davis type, file, strerror(errno));
402e4b0a90eSBrooks Davis return (-1);
403e4b0a90eSBrooks Davis }
404e4b0a90eSBrooks Davis }
405e4b0a90eSBrooks Davis if (strcmp(type, "keyfile") == 0) {
406e4b0a90eSBrooks Davis while ((done = read(fd, buf, sizeof(buf))) > 0)
407e4b0a90eSBrooks Davis g_eli_crypto_hmac_update(ctxp, buf, done);
408e4b0a90eSBrooks Davis } else /* if (strcmp(type, "passfile") == 0) */ {
409e4b0a90eSBrooks Davis assert(strcmp(type, "passfile") == 0);
410e4b0a90eSBrooks Davis
411e4b0a90eSBrooks Davis while ((done = read(fd, buf, sizeof(buf) - 1)) > 0) {
412e4b0a90eSBrooks Davis buf[done] = '\0';
413e4b0a90eSBrooks Davis p = strchr(buf, '\n');
414e4b0a90eSBrooks Davis if (p != NULL) {
415e4b0a90eSBrooks Davis *p = '\0';
416e4b0a90eSBrooks Davis done = p - buf;
417e4b0a90eSBrooks Davis }
418e4b0a90eSBrooks Davis if (strlcat(passbuf, buf, passbufsize) >=
419e4b0a90eSBrooks Davis passbufsize) {
420e4b0a90eSBrooks Davis gctl_error(req,
421e4b0a90eSBrooks Davis "Passphrase in %s too long.", file);
422217df2daSBen Woods explicit_bzero(buf, sizeof(buf));
423e4b0a90eSBrooks Davis return (-1);
424e4b0a90eSBrooks Davis }
425e4b0a90eSBrooks Davis if (p != NULL)
426e4b0a90eSBrooks Davis break;
427e4b0a90eSBrooks Davis }
428e4b0a90eSBrooks Davis }
429e4b0a90eSBrooks Davis error = errno;
430e4b0a90eSBrooks Davis if (strcmp(file, "-") != 0)
431e4b0a90eSBrooks Davis close(fd);
432217df2daSBen Woods explicit_bzero(buf, sizeof(buf));
433e4b0a90eSBrooks Davis if (done == -1) {
434e4b0a90eSBrooks Davis gctl_error(req, "Cannot read %s %s: %s.",
435e4b0a90eSBrooks Davis type, file, strerror(error));
436e4b0a90eSBrooks Davis return (-1);
437e4b0a90eSBrooks Davis }
438e4b0a90eSBrooks Davis }
439e4b0a90eSBrooks Davis /* NOTREACHED */
440e4b0a90eSBrooks Davis }
441e4b0a90eSBrooks Davis
442e4b0a90eSBrooks Davis static int
eli_genkey_passphrase_prompt(struct gctl_req * req,bool new,char * passbuf,size_t passbufsize)443e4b0a90eSBrooks Davis eli_genkey_passphrase_prompt(struct gctl_req *req, bool new, char *passbuf,
444e4b0a90eSBrooks Davis size_t passbufsize)
445e4b0a90eSBrooks Davis {
446e4b0a90eSBrooks Davis char *p;
447e4b0a90eSBrooks Davis
448e4b0a90eSBrooks Davis for (;;) {
449e4b0a90eSBrooks Davis p = readpassphrase(
450e4b0a90eSBrooks Davis new ? "Enter new passphrase: " : "Enter passphrase: ",
451e4b0a90eSBrooks Davis passbuf, passbufsize, RPP_ECHO_OFF | RPP_REQUIRE_TTY);
452e4b0a90eSBrooks Davis if (p == NULL) {
453217df2daSBen Woods explicit_bzero(passbuf, passbufsize);
454e4b0a90eSBrooks Davis gctl_error(req, "Cannot read passphrase: %s.",
455e4b0a90eSBrooks Davis strerror(errno));
456e4b0a90eSBrooks Davis return (-1);
457e4b0a90eSBrooks Davis }
458e4b0a90eSBrooks Davis
459e4b0a90eSBrooks Davis if (new) {
460e4b0a90eSBrooks Davis char tmpbuf[BUFSIZE];
461e4b0a90eSBrooks Davis
462e4b0a90eSBrooks Davis p = readpassphrase("Reenter new passphrase: ",
463e4b0a90eSBrooks Davis tmpbuf, sizeof(tmpbuf),
464e4b0a90eSBrooks Davis RPP_ECHO_OFF | RPP_REQUIRE_TTY);
465e4b0a90eSBrooks Davis if (p == NULL) {
466217df2daSBen Woods explicit_bzero(passbuf, passbufsize);
467e4b0a90eSBrooks Davis gctl_error(req,
468e4b0a90eSBrooks Davis "Cannot read passphrase: %s.",
469e4b0a90eSBrooks Davis strerror(errno));
470e4b0a90eSBrooks Davis return (-1);
471e4b0a90eSBrooks Davis }
472e4b0a90eSBrooks Davis
473e4b0a90eSBrooks Davis if (strcmp(passbuf, tmpbuf) != 0) {
474217df2daSBen Woods explicit_bzero(passbuf, passbufsize);
475e4b0a90eSBrooks Davis fprintf(stderr, "They didn't match.\n");
476e4b0a90eSBrooks Davis continue;
477e4b0a90eSBrooks Davis }
478217df2daSBen Woods explicit_bzero(tmpbuf, sizeof(tmpbuf));
479e4b0a90eSBrooks Davis }
480e4b0a90eSBrooks Davis return (0);
481e4b0a90eSBrooks Davis }
482e4b0a90eSBrooks Davis /* NOTREACHED */
483e4b0a90eSBrooks Davis }
484e4b0a90eSBrooks Davis
485e4b0a90eSBrooks Davis static int
eli_genkey_passphrase(struct gctl_req * req,struct g_eli_metadata * md,bool new,struct hmac_ctx * ctxp)486e4b0a90eSBrooks Davis eli_genkey_passphrase(struct gctl_req *req, struct g_eli_metadata *md, bool new,
487e4b0a90eSBrooks Davis struct hmac_ctx *ctxp)
488e4b0a90eSBrooks Davis {
489e4b0a90eSBrooks Davis char passbuf[BUFSIZE];
490e4b0a90eSBrooks Davis bool nopassphrase;
491e4b0a90eSBrooks Davis int nfiles;
492e4b0a90eSBrooks Davis
493217df2daSBen Woods /*
494217df2daSBen Woods * Return error if the 'do not use passphrase' flag was given but a
495217df2daSBen Woods * passfile was provided.
496217df2daSBen Woods */
497e4b0a90eSBrooks Davis nopassphrase =
498e4b0a90eSBrooks Davis gctl_get_int(req, new ? "nonewpassphrase" : "nopassphrase");
499e4b0a90eSBrooks Davis if (nopassphrase) {
500e4b0a90eSBrooks Davis if (gctl_has_param(req, new ? "newpassfile0" : "passfile0")) {
501e4b0a90eSBrooks Davis gctl_error(req,
502e4b0a90eSBrooks Davis "Options -%c and -%c are mutually exclusive.",
503e4b0a90eSBrooks Davis new ? 'J' : 'j', new ? 'P' : 'p');
504e4b0a90eSBrooks Davis return (-1);
505e4b0a90eSBrooks Davis }
506e4b0a90eSBrooks Davis return (0);
507e4b0a90eSBrooks Davis }
508e4b0a90eSBrooks Davis
509217df2daSBen Woods /*
510217df2daSBen Woods * Return error if using a provider which does not require a passphrase
511217df2daSBen Woods * but the 'do not use passphrase' flag was not given.
512217df2daSBen Woods */
513e4b0a90eSBrooks Davis if (!new && md->md_iterations == -1) {
514e4b0a90eSBrooks Davis gctl_error(req, "Missing -p flag.");
515e4b0a90eSBrooks Davis return (-1);
516e4b0a90eSBrooks Davis }
517e4b0a90eSBrooks Davis passbuf[0] = '\0';
518217df2daSBen Woods
519217df2daSBen Woods /* Use cached passphrase if defined. */
520217df2daSBen Woods if (strlen(cached_passphrase) > 0) {
521217df2daSBen Woods strlcpy(passbuf, cached_passphrase, sizeof(passbuf));
522217df2daSBen Woods } else {
523e4b0a90eSBrooks Davis nfiles = eli_genkey_files(req, new, "passfile", NULL, passbuf,
524e4b0a90eSBrooks Davis sizeof(passbuf));
525217df2daSBen Woods if (nfiles == -1) {
526e4b0a90eSBrooks Davis return (-1);
527217df2daSBen Woods } else if (nfiles == 0) {
528e4b0a90eSBrooks Davis if (eli_genkey_passphrase_prompt(req, new, passbuf,
529e4b0a90eSBrooks Davis sizeof(passbuf)) == -1) {
530e4b0a90eSBrooks Davis return (-1);
531e4b0a90eSBrooks Davis }
532e4b0a90eSBrooks Davis }
533217df2daSBen Woods /* Cache the passphrase for other providers. */
534217df2daSBen Woods strlcpy(cached_passphrase, passbuf, sizeof(cached_passphrase));
535217df2daSBen Woods }
536e4b0a90eSBrooks Davis /*
537e4b0a90eSBrooks Davis * Field md_iterations equal to -1 means "choose some sane
538e4b0a90eSBrooks Davis * value for me".
539e4b0a90eSBrooks Davis */
540e4b0a90eSBrooks Davis if (md->md_iterations == -1) {
541e4b0a90eSBrooks Davis assert(new);
542e4b0a90eSBrooks Davis if (verbose)
543e4b0a90eSBrooks Davis printf("Calculating number of iterations...\n");
544e4b0a90eSBrooks Davis md->md_iterations = pkcs5v2_calculate(2000000);
545e4b0a90eSBrooks Davis assert(md->md_iterations > 0);
546e4b0a90eSBrooks Davis if (verbose) {
547e4b0a90eSBrooks Davis printf("Done, using %d iterations.\n",
548e4b0a90eSBrooks Davis md->md_iterations);
549e4b0a90eSBrooks Davis }
550e4b0a90eSBrooks Davis }
551e4b0a90eSBrooks Davis /*
552e4b0a90eSBrooks Davis * If md_iterations is equal to 0, user doesn't want PKCS#5v2.
553e4b0a90eSBrooks Davis */
554e4b0a90eSBrooks Davis if (md->md_iterations == 0) {
555e4b0a90eSBrooks Davis g_eli_crypto_hmac_update(ctxp, md->md_salt,
556e4b0a90eSBrooks Davis sizeof(md->md_salt));
557e4b0a90eSBrooks Davis g_eli_crypto_hmac_update(ctxp, passbuf, strlen(passbuf));
558e4b0a90eSBrooks Davis } else /* if (md->md_iterations > 0) */ {
559e4b0a90eSBrooks Davis unsigned char dkey[G_ELI_USERKEYLEN];
560e4b0a90eSBrooks Davis
561e4b0a90eSBrooks Davis pkcs5v2_genkey(dkey, sizeof(dkey), md->md_salt,
562e4b0a90eSBrooks Davis sizeof(md->md_salt), passbuf, md->md_iterations);
563e4b0a90eSBrooks Davis g_eli_crypto_hmac_update(ctxp, dkey, sizeof(dkey));
564217df2daSBen Woods explicit_bzero(dkey, sizeof(dkey));
565e4b0a90eSBrooks Davis }
566217df2daSBen Woods explicit_bzero(passbuf, sizeof(passbuf));
567e4b0a90eSBrooks Davis
568e4b0a90eSBrooks Davis return (0);
569e4b0a90eSBrooks Davis }
570e4b0a90eSBrooks Davis
5715fff0966SMariusz Zaborski static bool
eli_init_key_hmac_ctx(struct gctl_req * req,struct hmac_ctx * ctx,bool new)5725fff0966SMariusz Zaborski eli_init_key_hmac_ctx(struct gctl_req *req, struct hmac_ctx *ctx, bool new)
573e4b0a90eSBrooks Davis {
574e4b0a90eSBrooks Davis int nfiles;
5755fff0966SMariusz Zaborski bool nopassphrase;
576e4b0a90eSBrooks Davis
577e4b0a90eSBrooks Davis nopassphrase =
578e4b0a90eSBrooks Davis gctl_get_int(req, new ? "nonewpassphrase" : "nopassphrase");
579e4b0a90eSBrooks Davis
5805fff0966SMariusz Zaborski g_eli_crypto_hmac_init(ctx, NULL, 0);
5815fff0966SMariusz Zaborski nfiles = eli_genkey_files(req, new, "keyfile", ctx, NULL, 0);
5825fff0966SMariusz Zaborski if (nfiles == -1) {
5835fff0966SMariusz Zaborski return (false);
5845fff0966SMariusz Zaborski } else if (nfiles == 0 && nopassphrase) {
585e4b0a90eSBrooks Davis gctl_error(req, "No key components given.");
5865fff0966SMariusz Zaborski return (false);
587e4b0a90eSBrooks Davis }
588e4b0a90eSBrooks Davis
5895fff0966SMariusz Zaborski return (true);
5905fff0966SMariusz Zaborski }
5915fff0966SMariusz Zaborski
5925fff0966SMariusz Zaborski static unsigned char *
eli_genkey(struct gctl_req * req,const struct hmac_ctx * ctxtemplate,struct g_eli_metadata * md,unsigned char * key,bool new)5935fff0966SMariusz Zaborski eli_genkey(struct gctl_req *req, const struct hmac_ctx *ctxtemplate,
5945fff0966SMariusz Zaborski struct g_eli_metadata *md, unsigned char *key, bool new)
5955fff0966SMariusz Zaborski {
5965fff0966SMariusz Zaborski struct hmac_ctx ctx;
5975fff0966SMariusz Zaborski
5985fff0966SMariusz Zaborski memcpy(&ctx, ctxtemplate, sizeof(ctx));
5995fff0966SMariusz Zaborski
600e4b0a90eSBrooks Davis if (eli_genkey_passphrase(req, md, new, &ctx) == -1)
601e4b0a90eSBrooks Davis return (NULL);
602e4b0a90eSBrooks Davis
603e4b0a90eSBrooks Davis g_eli_crypto_hmac_final(&ctx, key, 0);
604e4b0a90eSBrooks Davis
605e4b0a90eSBrooks Davis return (key);
606e4b0a90eSBrooks Davis }
607e4b0a90eSBrooks Davis
6085fff0966SMariusz Zaborski static unsigned char *
eli_genkey_single(struct gctl_req * req,struct g_eli_metadata * md,unsigned char * key,bool new)6095fff0966SMariusz Zaborski eli_genkey_single(struct gctl_req *req, struct g_eli_metadata *md,
6105fff0966SMariusz Zaborski unsigned char *key, bool new)
6115fff0966SMariusz Zaborski {
6125fff0966SMariusz Zaborski struct hmac_ctx ctx;
6135fff0966SMariusz Zaborski unsigned char *rkey;
6145fff0966SMariusz Zaborski
6155fff0966SMariusz Zaborski if (!eli_init_key_hmac_ctx(req, &ctx, new)) {
6165fff0966SMariusz Zaborski return (NULL);
6175fff0966SMariusz Zaborski }
6185fff0966SMariusz Zaborski rkey = eli_genkey(req, &ctx, md, key, new);
6195fff0966SMariusz Zaborski explicit_bzero(&ctx, sizeof(ctx));
6205fff0966SMariusz Zaborski
6215fff0966SMariusz Zaborski return (rkey);
6225fff0966SMariusz Zaborski }
6235fff0966SMariusz Zaborski
624e4b0a90eSBrooks Davis static int
eli_metadata_read(struct gctl_req * req,const char * prov,struct g_eli_metadata * md)625e4b0a90eSBrooks Davis eli_metadata_read(struct gctl_req *req, const char *prov,
626e4b0a90eSBrooks Davis struct g_eli_metadata *md)
627e4b0a90eSBrooks Davis {
628e4b0a90eSBrooks Davis unsigned char sector[sizeof(struct g_eli_metadata)];
629e4b0a90eSBrooks Davis int error;
630e4b0a90eSBrooks Davis
631e4b0a90eSBrooks Davis if (g_get_sectorsize(prov) == 0) {
632e4b0a90eSBrooks Davis int fd;
633e4b0a90eSBrooks Davis
634e4b0a90eSBrooks Davis /* This is a file probably. */
635e4b0a90eSBrooks Davis fd = open(prov, O_RDONLY);
636e4b0a90eSBrooks Davis if (fd == -1) {
637e4b0a90eSBrooks Davis gctl_error(req, "Cannot open %s: %s.", prov,
638e4b0a90eSBrooks Davis strerror(errno));
639e4b0a90eSBrooks Davis return (-1);
640e4b0a90eSBrooks Davis }
641e4b0a90eSBrooks Davis if (read(fd, sector, sizeof(sector)) != sizeof(sector)) {
642e4b0a90eSBrooks Davis gctl_error(req, "Cannot read metadata from %s: %s.",
643e4b0a90eSBrooks Davis prov, strerror(errno));
644e4b0a90eSBrooks Davis close(fd);
645e4b0a90eSBrooks Davis return (-1);
646e4b0a90eSBrooks Davis }
647e4b0a90eSBrooks Davis close(fd);
648e4b0a90eSBrooks Davis } else {
649e4b0a90eSBrooks Davis /* This is a GEOM provider. */
650e4b0a90eSBrooks Davis error = g_metadata_read(prov, sector, sizeof(sector),
651e4b0a90eSBrooks Davis G_ELI_MAGIC);
652e4b0a90eSBrooks Davis if (error != 0) {
653e4b0a90eSBrooks Davis gctl_error(req, "Cannot read metadata from %s: %s.",
654e4b0a90eSBrooks Davis prov, strerror(error));
655e4b0a90eSBrooks Davis return (-1);
656e4b0a90eSBrooks Davis }
657e4b0a90eSBrooks Davis }
658e4b0a90eSBrooks Davis error = eli_metadata_decode(sector, md);
659e4b0a90eSBrooks Davis switch (error) {
660e4b0a90eSBrooks Davis case 0:
661e4b0a90eSBrooks Davis break;
662e4b0a90eSBrooks Davis case EOPNOTSUPP:
663e4b0a90eSBrooks Davis gctl_error(req,
664e4b0a90eSBrooks Davis "Provider's %s metadata version %u is too new.\n"
665e4b0a90eSBrooks Davis "geli: The highest supported version is %u.",
666e4b0a90eSBrooks Davis prov, (unsigned int)md->md_version, G_ELI_VERSION);
667e4b0a90eSBrooks Davis return (-1);
668e4b0a90eSBrooks Davis case EINVAL:
669e4b0a90eSBrooks Davis gctl_error(req, "Inconsistent provider's %s metadata.", prov);
670e4b0a90eSBrooks Davis return (-1);
671e4b0a90eSBrooks Davis default:
672e4b0a90eSBrooks Davis gctl_error(req,
673e4b0a90eSBrooks Davis "Unexpected error while decoding provider's %s metadata: %s.",
674e4b0a90eSBrooks Davis prov, strerror(error));
675e4b0a90eSBrooks Davis return (-1);
676e4b0a90eSBrooks Davis }
677e4b0a90eSBrooks Davis return (0);
678e4b0a90eSBrooks Davis }
679e4b0a90eSBrooks Davis
680e4b0a90eSBrooks Davis static int
eli_metadata_store(struct gctl_req * req,const char * prov,struct g_eli_metadata * md)681e4b0a90eSBrooks Davis eli_metadata_store(struct gctl_req *req, const char *prov,
682e4b0a90eSBrooks Davis struct g_eli_metadata *md)
683e4b0a90eSBrooks Davis {
684e4b0a90eSBrooks Davis unsigned char sector[sizeof(struct g_eli_metadata)];
685e4b0a90eSBrooks Davis int error;
686e4b0a90eSBrooks Davis
687e4b0a90eSBrooks Davis eli_metadata_encode(md, sector);
688e4b0a90eSBrooks Davis if (g_get_sectorsize(prov) == 0) {
689e4b0a90eSBrooks Davis int fd;
690e4b0a90eSBrooks Davis
691e4b0a90eSBrooks Davis /* This is a file probably. */
692e4b0a90eSBrooks Davis fd = open(prov, O_WRONLY | O_TRUNC);
693e4b0a90eSBrooks Davis if (fd == -1) {
694e4b0a90eSBrooks Davis gctl_error(req, "Cannot open %s: %s.", prov,
695e4b0a90eSBrooks Davis strerror(errno));
696217df2daSBen Woods explicit_bzero(sector, sizeof(sector));
697e4b0a90eSBrooks Davis return (-1);
698e4b0a90eSBrooks Davis }
699e4b0a90eSBrooks Davis if (write(fd, sector, sizeof(sector)) != sizeof(sector)) {
700e4b0a90eSBrooks Davis gctl_error(req, "Cannot write metadata to %s: %s.",
701e4b0a90eSBrooks Davis prov, strerror(errno));
702217df2daSBen Woods explicit_bzero(sector, sizeof(sector));
703e4b0a90eSBrooks Davis close(fd);
704e4b0a90eSBrooks Davis return (-1);
705e4b0a90eSBrooks Davis }
706e4b0a90eSBrooks Davis close(fd);
707e4b0a90eSBrooks Davis } else {
708e4b0a90eSBrooks Davis /* This is a GEOM provider. */
709e4b0a90eSBrooks Davis error = g_metadata_store(prov, sector, sizeof(sector));
710e4b0a90eSBrooks Davis if (error != 0) {
711e4b0a90eSBrooks Davis gctl_error(req, "Cannot write metadata to %s: %s.",
712e4b0a90eSBrooks Davis prov, strerror(errno));
713217df2daSBen Woods explicit_bzero(sector, sizeof(sector));
714e4b0a90eSBrooks Davis return (-1);
715e4b0a90eSBrooks Davis }
716e4b0a90eSBrooks Davis }
717217df2daSBen Woods explicit_bzero(sector, sizeof(sector));
718e4b0a90eSBrooks Davis return (0);
719e4b0a90eSBrooks Davis }
720e4b0a90eSBrooks Davis
721e4b0a90eSBrooks Davis static void
eli_init(struct gctl_req * req)722e4b0a90eSBrooks Davis eli_init(struct gctl_req *req)
723e4b0a90eSBrooks Davis {
724e4b0a90eSBrooks Davis struct g_eli_metadata md;
725368455ecSBen Woods struct gctl_req *r;
726e4b0a90eSBrooks Davis unsigned char sector[sizeof(struct g_eli_metadata)] __aligned(4);
727e4b0a90eSBrooks Davis unsigned char key[G_ELI_USERKEYLEN];
728e4b0a90eSBrooks Davis char backfile[MAXPATHLEN];
729e4b0a90eSBrooks Davis const char *str, *prov;
730ca64a75eSAlan Somers unsigned int secsize, eli_version;
731e4b0a90eSBrooks Davis off_t mediasize;
732e4b0a90eSBrooks Davis intmax_t val;
733368455ecSBen Woods int error, i, nargs, nparams, param;
734368455ecSBen Woods const int one = 1;
7355fff0966SMariusz Zaborski struct hmac_ctx ctxtemplate;
736e4b0a90eSBrooks Davis
737e4b0a90eSBrooks Davis nargs = gctl_get_int(req, "nargs");
738368455ecSBen Woods if (nargs <= 0) {
739368455ecSBen Woods gctl_error(req, "Too few arguments.");
740e4b0a90eSBrooks Davis return;
741e4b0a90eSBrooks Davis }
742e4b0a90eSBrooks Davis
743368455ecSBen Woods /* Start generating metadata for provider(s) being initialized. */
744217df2daSBen Woods explicit_bzero(&md, sizeof(md));
745e4b0a90eSBrooks Davis strlcpy(md.md_magic, G_ELI_MAGIC, sizeof(md.md_magic));
746e4b0a90eSBrooks Davis val = gctl_get_intmax(req, "mdversion");
747e4b0a90eSBrooks Davis if (val == -1) {
748ca64a75eSAlan Somers eli_version = G_ELI_VERSION;
749e4b0a90eSBrooks Davis } else if (val < 0 || val > G_ELI_VERSION) {
750e4b0a90eSBrooks Davis gctl_error(req,
751e4b0a90eSBrooks Davis "Invalid version specified should be between %u and %u.",
752e4b0a90eSBrooks Davis G_ELI_VERSION_00, G_ELI_VERSION);
753e4b0a90eSBrooks Davis return;
754e4b0a90eSBrooks Davis } else {
755ca64a75eSAlan Somers eli_version = val;
756e4b0a90eSBrooks Davis }
757ca64a75eSAlan Somers md.md_version = eli_version;
7582f07cdf8SPawel Jakub Dawidek md.md_flags = G_ELI_FLAG_AUTORESIZE;
759e4b0a90eSBrooks Davis if (gctl_get_int(req, "boot"))
760e4b0a90eSBrooks Davis md.md_flags |= G_ELI_FLAG_BOOT;
761e4b0a90eSBrooks Davis if (gctl_get_int(req, "geliboot"))
762e4b0a90eSBrooks Davis md.md_flags |= G_ELI_FLAG_GELIBOOT;
763e4b0a90eSBrooks Davis if (gctl_get_int(req, "displaypass"))
764e4b0a90eSBrooks Davis md.md_flags |= G_ELI_FLAG_GELIDISPLAYPASS;
765e4b0a90eSBrooks Davis if (gctl_get_int(req, "notrim"))
766e4b0a90eSBrooks Davis md.md_flags |= G_ELI_FLAG_NODELETE;
7672f07cdf8SPawel Jakub Dawidek if (gctl_get_int(req, "noautoresize"))
7682f07cdf8SPawel Jakub Dawidek md.md_flags &= ~G_ELI_FLAG_AUTORESIZE;
769e4b0a90eSBrooks Davis md.md_ealgo = CRYPTO_ALGORITHM_MIN - 1;
770e4b0a90eSBrooks Davis str = gctl_get_ascii(req, "aalgo");
771e4b0a90eSBrooks Davis if (*str != '\0') {
772ca64a75eSAlan Somers if (eli_version < G_ELI_VERSION_01) {
773e4b0a90eSBrooks Davis gctl_error(req,
774e4b0a90eSBrooks Davis "Data authentication is supported starting from version %u.",
775e4b0a90eSBrooks Davis G_ELI_VERSION_01);
776e4b0a90eSBrooks Davis return;
777e4b0a90eSBrooks Davis }
778e4b0a90eSBrooks Davis md.md_aalgo = g_eli_str2aalgo(str);
779e4b0a90eSBrooks Davis if (md.md_aalgo >= CRYPTO_ALGORITHM_MIN &&
780e4b0a90eSBrooks Davis md.md_aalgo <= CRYPTO_ALGORITHM_MAX) {
781e4b0a90eSBrooks Davis md.md_flags |= G_ELI_FLAG_AUTH;
782e4b0a90eSBrooks Davis } else {
783e4b0a90eSBrooks Davis /*
784e4b0a90eSBrooks Davis * For backward compatibility, check if the -a option
785e4b0a90eSBrooks Davis * was used to provide encryption algorithm.
786e4b0a90eSBrooks Davis */
787e4b0a90eSBrooks Davis md.md_ealgo = g_eli_str2ealgo(str);
788e4b0a90eSBrooks Davis if (md.md_ealgo < CRYPTO_ALGORITHM_MIN ||
789e4b0a90eSBrooks Davis md.md_ealgo > CRYPTO_ALGORITHM_MAX) {
790e4b0a90eSBrooks Davis gctl_error(req,
791e4b0a90eSBrooks Davis "Invalid authentication algorithm.");
792e4b0a90eSBrooks Davis return;
793e4b0a90eSBrooks Davis } else {
794e4b0a90eSBrooks Davis fprintf(stderr, "warning: The -e option, not "
795e4b0a90eSBrooks Davis "the -a option is now used to specify "
796e4b0a90eSBrooks Davis "encryption algorithm to use.\n");
797e4b0a90eSBrooks Davis }
798e4b0a90eSBrooks Davis }
799e4b0a90eSBrooks Davis }
800e4b0a90eSBrooks Davis if (md.md_ealgo < CRYPTO_ALGORITHM_MIN ||
801e4b0a90eSBrooks Davis md.md_ealgo > CRYPTO_ALGORITHM_MAX) {
802e4b0a90eSBrooks Davis str = gctl_get_ascii(req, "ealgo");
803e4b0a90eSBrooks Davis if (*str == '\0') {
804ca64a75eSAlan Somers if (eli_version < G_ELI_VERSION_05)
805e4b0a90eSBrooks Davis str = "aes-cbc";
806e4b0a90eSBrooks Davis else
807e4b0a90eSBrooks Davis str = GELI_ENC_ALGO;
808e4b0a90eSBrooks Davis }
809e4b0a90eSBrooks Davis md.md_ealgo = g_eli_str2ealgo(str);
810e4b0a90eSBrooks Davis if (md.md_ealgo < CRYPTO_ALGORITHM_MIN ||
811e4b0a90eSBrooks Davis md.md_ealgo > CRYPTO_ALGORITHM_MAX) {
812e4b0a90eSBrooks Davis gctl_error(req, "Invalid encryption algorithm.");
813e4b0a90eSBrooks Davis return;
814e4b0a90eSBrooks Davis }
815e4b0a90eSBrooks Davis if (md.md_ealgo == CRYPTO_CAMELLIA_CBC &&
816ca64a75eSAlan Somers eli_version < G_ELI_VERSION_04) {
817e4b0a90eSBrooks Davis gctl_error(req,
818e4b0a90eSBrooks Davis "Camellia-CBC algorithm is supported starting from version %u.",
819e4b0a90eSBrooks Davis G_ELI_VERSION_04);
820e4b0a90eSBrooks Davis return;
821e4b0a90eSBrooks Davis }
822e4b0a90eSBrooks Davis if (md.md_ealgo == CRYPTO_AES_XTS &&
823ca64a75eSAlan Somers eli_version < G_ELI_VERSION_05) {
824e4b0a90eSBrooks Davis gctl_error(req,
825e4b0a90eSBrooks Davis "AES-XTS algorithm is supported starting from version %u.",
826e4b0a90eSBrooks Davis G_ELI_VERSION_05);
827e4b0a90eSBrooks Davis return;
828e4b0a90eSBrooks Davis }
829e4b0a90eSBrooks Davis }
830e4b0a90eSBrooks Davis val = gctl_get_intmax(req, "keylen");
831e4b0a90eSBrooks Davis md.md_keylen = val;
832e4b0a90eSBrooks Davis md.md_keylen = g_eli_keylen(md.md_ealgo, md.md_keylen);
833e4b0a90eSBrooks Davis if (md.md_keylen == 0) {
834e4b0a90eSBrooks Davis gctl_error(req, "Invalid key length.");
835e4b0a90eSBrooks Davis return;
836e4b0a90eSBrooks Davis }
837e4b0a90eSBrooks Davis
838e4b0a90eSBrooks Davis val = gctl_get_intmax(req, "iterations");
839e4b0a90eSBrooks Davis if (val != -1) {
840e4b0a90eSBrooks Davis int nonewpassphrase;
841e4b0a90eSBrooks Davis
842e4b0a90eSBrooks Davis /*
843e4b0a90eSBrooks Davis * Don't allow to set iterations when there will be no
844e4b0a90eSBrooks Davis * passphrase.
845e4b0a90eSBrooks Davis */
846e4b0a90eSBrooks Davis nonewpassphrase = gctl_get_int(req, "nonewpassphrase");
847e4b0a90eSBrooks Davis if (nonewpassphrase) {
848e4b0a90eSBrooks Davis gctl_error(req,
849e4b0a90eSBrooks Davis "Options -i and -P are mutually exclusive.");
850e4b0a90eSBrooks Davis return;
851e4b0a90eSBrooks Davis }
852e4b0a90eSBrooks Davis }
853e4b0a90eSBrooks Davis md.md_iterations = val;
854e4b0a90eSBrooks Davis
855e4b0a90eSBrooks Davis val = gctl_get_intmax(req, "sectorsize");
856e4b0a90eSBrooks Davis if (val > sysconf(_SC_PAGE_SIZE)) {
857e4b0a90eSBrooks Davis fprintf(stderr,
858e4b0a90eSBrooks Davis "warning: Using sectorsize bigger than the page size!\n");
859e4b0a90eSBrooks Davis }
860368455ecSBen Woods
861368455ecSBen Woods md.md_keys = 0x01;
862368455ecSBen Woods
863368455ecSBen Woods /*
864368455ecSBen Woods * Determine number of parameters in the parent geom request before the
865368455ecSBen Woods * nargs parameter and list of providers.
866368455ecSBen Woods */
867368455ecSBen Woods nparams = req->narg - nargs - 1;
868368455ecSBen Woods
8695fff0966SMariusz Zaborski /* Generate HMAC context template. */
8705fff0966SMariusz Zaborski if (!eli_init_key_hmac_ctx(req, &ctxtemplate, true))
8715fff0966SMariusz Zaborski return;
8725fff0966SMariusz Zaborski
873368455ecSBen Woods /* Create new child request for each provider and issue to kernel */
874368455ecSBen Woods for (i = 0; i < nargs; i++) {
875368455ecSBen Woods r = gctl_get_handle();
876368455ecSBen Woods
877368455ecSBen Woods /* Copy each parameter from the parent request to the child */
878368455ecSBen Woods for (param = 0; param < nparams; param++) {
879368455ecSBen Woods gctl_ro_param(r, req->arg[param].name,
880368455ecSBen Woods req->arg[param].len, req->arg[param].value);
881368455ecSBen Woods }
882368455ecSBen Woods
883368455ecSBen Woods /* Add a single provider to the parameter list of the child */
884368455ecSBen Woods gctl_ro_param(r, "nargs", sizeof(one), &one);
885368455ecSBen Woods prov = gctl_get_ascii(req, "arg%d", i);
886368455ecSBen Woods gctl_ro_param(r, "arg0", -1, prov);
887368455ecSBen Woods
888368455ecSBen Woods mediasize = g_get_mediasize(prov);
889368455ecSBen Woods secsize = g_get_sectorsize(prov);
890368455ecSBen Woods if (mediasize == 0 || secsize == 0) {
891368455ecSBen Woods gctl_error(r, "Cannot get information about %s: %s.",
892368455ecSBen Woods prov, strerror(errno));
893368455ecSBen Woods goto out;
894368455ecSBen Woods }
895368455ecSBen Woods
896368455ecSBen Woods md.md_provsize = mediasize;
897368455ecSBen Woods
898368455ecSBen Woods val = gctl_get_intmax(r, "sectorsize");
899368455ecSBen Woods if (val == 0) {
900368455ecSBen Woods md.md_sectorsize = secsize;
901368455ecSBen Woods } else {
902368455ecSBen Woods if (val < 0 || (val % secsize) != 0 || !powerof2(val)) {
903368455ecSBen Woods gctl_error(r, "Invalid sector size.");
904368455ecSBen Woods goto out;
905368455ecSBen Woods }
906e4b0a90eSBrooks Davis md.md_sectorsize = val;
907e4b0a90eSBrooks Davis }
908e4b0a90eSBrooks Davis
909368455ecSBen Woods /* Use different salt and Master Key for each provider. */
910e4b0a90eSBrooks Davis arc4random_buf(md.md_salt, sizeof(md.md_salt));
911e4b0a90eSBrooks Davis arc4random_buf(md.md_mkeys, sizeof(md.md_mkeys));
912e4b0a90eSBrooks Davis
913e4b0a90eSBrooks Davis /* Generate user key. */
9145fff0966SMariusz Zaborski if (eli_genkey(r, &ctxtemplate, &md, key, true) == NULL) {
915368455ecSBen Woods /*
916368455ecSBen Woods * Error generating key - details added to geom request
917368455ecSBen Woods * by eli_genkey().
918368455ecSBen Woods */
919368455ecSBen Woods goto out;
920e4b0a90eSBrooks Davis }
921e4b0a90eSBrooks Davis
922e4b0a90eSBrooks Davis /* Encrypt the first and the only Master Key. */
923368455ecSBen Woods error = g_eli_mkey_encrypt(md.md_ealgo, key, md.md_keylen,
924368455ecSBen Woods md.md_mkeys);
925e4b0a90eSBrooks Davis if (error != 0) {
926368455ecSBen Woods gctl_error(r, "Cannot encrypt Master Key: %s.",
927e4b0a90eSBrooks Davis strerror(error));
928368455ecSBen Woods goto out;
929e4b0a90eSBrooks Davis }
930e4b0a90eSBrooks Davis
931e45068e3SBen Woods /* Convert metadata to on-disk format. */
932e4b0a90eSBrooks Davis eli_metadata_encode(&md, sector);
933368455ecSBen Woods
934e45068e3SBen Woods /* Store metadata to disk. */
935e4b0a90eSBrooks Davis error = g_metadata_store(prov, sector, sizeof(sector));
936e4b0a90eSBrooks Davis if (error != 0) {
937368455ecSBen Woods gctl_error(r, "Cannot store metadata on %s: %s.", prov,
938e4b0a90eSBrooks Davis strerror(error));
939368455ecSBen Woods goto out;
940e4b0a90eSBrooks Davis }
941e4b0a90eSBrooks Davis if (verbose)
942e4b0a90eSBrooks Davis printf("Metadata value stored on %s.\n", prov);
943368455ecSBen Woods
944e4b0a90eSBrooks Davis /* Backup metadata to a file. */
945368455ecSBen Woods const char *p = prov;
946368455ecSBen Woods unsigned int j;
947368455ecSBen Woods
948368455ecSBen Woods /*
949368455ecSBen Woods * Check if provider string includes the devfs mountpoint
950368455ecSBen Woods * (typically /dev/).
951368455ecSBen Woods */
952368455ecSBen Woods if (strncmp(p, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0) {
953368455ecSBen Woods /* Skip forward to the device filename only. */
954368455ecSBen Woods p += sizeof(_PATH_DEV) - 1;
955368455ecSBen Woods }
956368455ecSBen Woods
957368455ecSBen Woods str = gctl_get_ascii(r, "backupfile");
958e4b0a90eSBrooks Davis if (str[0] != '\0') {
959368455ecSBen Woods /* Backupfile given by the user, just copy it. */
960e4b0a90eSBrooks Davis strlcpy(backfile, str, sizeof(backfile));
961368455ecSBen Woods
962e45068e3SBen Woods /* If multiple providers have been initialized in one
963e45068e3SBen Woods * command, and the backup filename has been specified
964e45068e3SBen Woods * as anything other than "none", make the backup
965e45068e3SBen Woods * filename unique for each provider. */
966e45068e3SBen Woods if (nargs > 1 && strcmp(backfile, "none") != 0) {
967368455ecSBen Woods /*
968368455ecSBen Woods * Replace first occurrence of "PROV" with
969368455ecSBen Woods * provider name.
970368455ecSBen Woods */
971368455ecSBen Woods str = strnstr(backfile, "PROV",
972368455ecSBen Woods sizeof(backfile));
973368455ecSBen Woods if (str != NULL) {
974368455ecSBen Woods char suffix[MAXPATHLEN];
975368455ecSBen Woods j = str - backfile;
976368455ecSBen Woods strlcpy(suffix, &backfile[j+4],
977368455ecSBen Woods sizeof(suffix));
978368455ecSBen Woods backfile[j] = '\0';
979368455ecSBen Woods strlcat(backfile, p, sizeof(backfile));
980368455ecSBen Woods strlcat(backfile, suffix,
981368455ecSBen Woods sizeof(backfile));
982368455ecSBen Woods } else {
983368455ecSBen Woods /*
984368455ecSBen Woods * "PROV" not found in backfile, append
985368455ecSBen Woods * provider name.
986368455ecSBen Woods */
987368455ecSBen Woods strlcat(backfile, "-",
988368455ecSBen Woods sizeof(backfile));
989368455ecSBen Woods strlcat(backfile, p, sizeof(backfile));
990368455ecSBen Woods }
991368455ecSBen Woods }
992e4b0a90eSBrooks Davis } else {
993e4b0a90eSBrooks Davis /* Generate filename automatically. */
994e4b0a90eSBrooks Davis snprintf(backfile, sizeof(backfile), "%s%s.eli",
995e4b0a90eSBrooks Davis GELI_BACKUP_DIR, p);
996e4b0a90eSBrooks Davis /* Replace all / with _. */
997368455ecSBen Woods for (j = strlen(GELI_BACKUP_DIR); backfile[j] != '\0';
998368455ecSBen Woods j++) {
999368455ecSBen Woods if (backfile[j] == '/')
1000368455ecSBen Woods backfile[j] = '_';
1001e4b0a90eSBrooks Davis }
1002e4b0a90eSBrooks Davis }
1003e4b0a90eSBrooks Davis if (strcmp(backfile, "none") != 0 &&
1004368455ecSBen Woods eli_backup_create(r, prov, backfile) == 0) {
1005368455ecSBen Woods printf("\nMetadata backup for provider %s can be found "
1006368455ecSBen Woods "in %s\n", prov, backfile);
1007368455ecSBen Woods printf("and can be restored with the following "
1008368455ecSBen Woods "command:\n");
1009e4b0a90eSBrooks Davis printf("\n\t# geli restore %s %s\n\n", backfile, prov);
1010e4b0a90eSBrooks Davis }
1011368455ecSBen Woods
1012368455ecSBen Woods out:
1013368455ecSBen Woods /*
1014368455ecSBen Woods * Print error for this request, and set parent request error
1015368455ecSBen Woods * message.
1016368455ecSBen Woods */
1017368455ecSBen Woods if (r->error != NULL && r->error[0] != '\0') {
1018368455ecSBen Woods warnx("%s", r->error);
1019368455ecSBen Woods gctl_error(req, "There was an error with at least one "
1020368455ecSBen Woods "provider.");
1021368455ecSBen Woods }
1022368455ecSBen Woods
1023368455ecSBen Woods gctl_free(r);
1024368455ecSBen Woods
1025368455ecSBen Woods /*
1026e45068e3SBen Woods * Erase sensitive and provider specific data from memory.
1027368455ecSBen Woods */
1028368455ecSBen Woods explicit_bzero(key, sizeof(key));
1029e45068e3SBen Woods explicit_bzero(sector, sizeof(sector));
1030e45068e3SBen Woods explicit_bzero(&md.md_provsize, sizeof(md.md_provsize));
1031e45068e3SBen Woods explicit_bzero(&md.md_sectorsize, sizeof(md.md_sectorsize));
1032e45068e3SBen Woods explicit_bzero(&md.md_salt, sizeof(md.md_salt));
1033e45068e3SBen Woods explicit_bzero(&md.md_mkeys, sizeof(md.md_mkeys));
1034368455ecSBen Woods }
1035368455ecSBen Woods
1036368455ecSBen Woods /* Clear the cached metadata, including keys. */
1037368455ecSBen Woods explicit_bzero(&md, sizeof(md));
10385fff0966SMariusz Zaborski explicit_bzero(&ctxtemplate, sizeof(ctxtemplate));
1039e4b0a90eSBrooks Davis }
1040e4b0a90eSBrooks Davis
1041e4b0a90eSBrooks Davis static void
eli_attach(struct gctl_req * req)1042e4b0a90eSBrooks Davis eli_attach(struct gctl_req *req)
1043e4b0a90eSBrooks Davis {
1044e4b0a90eSBrooks Davis struct g_eli_metadata md;
1045217df2daSBen Woods struct gctl_req *r;
1046e4b0a90eSBrooks Davis const char *prov;
1047e4b0a90eSBrooks Davis off_t mediasize;
1048217df2daSBen Woods int i, nargs, nparams, param;
1049217df2daSBen Woods const int one = 1;
10505fff0966SMariusz Zaborski struct hmac_ctx ctxtemplate;
1051e4b0a90eSBrooks Davis
1052e4b0a90eSBrooks Davis nargs = gctl_get_int(req, "nargs");
1053368455ecSBen Woods if (nargs <= 0) {
1054217df2daSBen Woods gctl_error(req, "Too few arguments.");
1055e4b0a90eSBrooks Davis return;
1056e4b0a90eSBrooks Davis }
1057e4b0a90eSBrooks Davis
1058217df2daSBen Woods unsigned char key[G_ELI_USERKEYLEN];
1059217df2daSBen Woods
1060217df2daSBen Woods /*
1061217df2daSBen Woods * Determine number of parameters in the parent geom request before the
1062217df2daSBen Woods * nargs parameter and list of providers.
1063217df2daSBen Woods */
1064217df2daSBen Woods nparams = req->narg - nargs - 1;
1065217df2daSBen Woods
10665fff0966SMariusz Zaborski /* Generate HMAC context template. */
10675fff0966SMariusz Zaborski if (!eli_init_key_hmac_ctx(req, &ctxtemplate, false))
10685fff0966SMariusz Zaborski return;
10695fff0966SMariusz Zaborski
1070368455ecSBen Woods /* Create new child request for each provider and issue to kernel */
1071217df2daSBen Woods for (i = 0; i < nargs; i++) {
1072217df2daSBen Woods r = gctl_get_handle();
1073217df2daSBen Woods
1074368455ecSBen Woods /* Copy each parameter from the parent request to the child */
1075217df2daSBen Woods for (param = 0; param < nparams; param++) {
1076368455ecSBen Woods gctl_ro_param(r, req->arg[param].name,
1077368455ecSBen Woods req->arg[param].len, req->arg[param].value);
1078217df2daSBen Woods }
1079217df2daSBen Woods
1080217df2daSBen Woods /* Add a single provider to the parameter list of the child */
1081217df2daSBen Woods gctl_ro_param(r, "nargs", sizeof(one), &one);
1082217df2daSBen Woods prov = gctl_get_ascii(req, "arg%d", i);
1083217df2daSBen Woods gctl_ro_param(r, "arg0", -1, prov);
1084217df2daSBen Woods
10854b8e4d53SBen Woods if (eli_metadata_read(r, prov, &md) == -1) {
10864b8e4d53SBen Woods /*
10874b8e4d53SBen Woods * Error reading metadata - details added to geom
10884b8e4d53SBen Woods * request by eli_metadata_read().
10894b8e4d53SBen Woods */
10904b8e4d53SBen Woods goto out;
10914b8e4d53SBen Woods }
1092e4b0a90eSBrooks Davis
1093e4b0a90eSBrooks Davis mediasize = g_get_mediasize(prov);
1094e4b0a90eSBrooks Davis if (md.md_provsize != (uint64_t)mediasize) {
1095217df2daSBen Woods gctl_error(r, "Provider size mismatch.");
10964b8e4d53SBen Woods goto out;
1097e4b0a90eSBrooks Davis }
1098e4b0a90eSBrooks Davis
10995fff0966SMariusz Zaborski if (eli_genkey(r, &ctxtemplate, &md, key, false) == NULL) {
11004b8e4d53SBen Woods /*
11014b8e4d53SBen Woods * Error generating key - details added to geom request
11024b8e4d53SBen Woods * by eli_genkey().
11034b8e4d53SBen Woods */
11044b8e4d53SBen Woods goto out;
1105e4b0a90eSBrooks Davis }
1106e4b0a90eSBrooks Davis
1107217df2daSBen Woods gctl_ro_param(r, "key", sizeof(key), key);
1108217df2daSBen Woods
1109217df2daSBen Woods if (gctl_issue(r) == NULL) {
1110e4b0a90eSBrooks Davis if (verbose)
1111e4b0a90eSBrooks Davis printf("Attached to %s.\n", prov);
1112e4b0a90eSBrooks Davis }
1113217df2daSBen Woods
11144b8e4d53SBen Woods out:
1115368455ecSBen Woods /*
1116368455ecSBen Woods * Print error for this request, and set parent request error
1117368455ecSBen Woods * message.
1118368455ecSBen Woods */
1119217df2daSBen Woods if (r->error != NULL && r->error[0] != '\0') {
1120217df2daSBen Woods warnx("%s", r->error);
1121368455ecSBen Woods gctl_error(req, "There was an error with at least one "
1122368455ecSBen Woods "provider.");
1123217df2daSBen Woods }
1124217df2daSBen Woods
1125217df2daSBen Woods gctl_free(r);
1126217df2daSBen Woods
11274b8e4d53SBen Woods /* Clear sensitive data from memory. */
1128217df2daSBen Woods explicit_bzero(key, sizeof(key));
1129217df2daSBen Woods }
1130217df2daSBen Woods
11314b8e4d53SBen Woods /* Clear sensitive data from memory. */
1132217df2daSBen Woods explicit_bzero(cached_passphrase, sizeof(cached_passphrase));
11335fff0966SMariusz Zaborski explicit_bzero(&ctxtemplate, sizeof(ctxtemplate));
1134e4b0a90eSBrooks Davis }
1135e4b0a90eSBrooks Davis
1136e4b0a90eSBrooks Davis static void
eli_configure_detached(struct gctl_req * req,const char * prov,int boot,int geliboot,int displaypass,int trim,int autoresize)1137e4b0a90eSBrooks Davis eli_configure_detached(struct gctl_req *req, const char *prov, int boot,
11382f07cdf8SPawel Jakub Dawidek int geliboot, int displaypass, int trim, int autoresize)
1139e4b0a90eSBrooks Davis {
1140e4b0a90eSBrooks Davis struct g_eli_metadata md;
1141e4b0a90eSBrooks Davis bool changed = 0;
1142e4b0a90eSBrooks Davis
1143e4b0a90eSBrooks Davis if (eli_metadata_read(req, prov, &md) == -1)
1144e4b0a90eSBrooks Davis return;
1145e4b0a90eSBrooks Davis
1146e4b0a90eSBrooks Davis if (boot == 1 && (md.md_flags & G_ELI_FLAG_BOOT)) {
1147e4b0a90eSBrooks Davis if (verbose)
1148e4b0a90eSBrooks Davis printf("BOOT flag already configured for %s.\n", prov);
1149e4b0a90eSBrooks Davis } else if (boot == 0 && !(md.md_flags & G_ELI_FLAG_BOOT)) {
1150e4b0a90eSBrooks Davis if (verbose)
1151e4b0a90eSBrooks Davis printf("BOOT flag not configured for %s.\n", prov);
1152e4b0a90eSBrooks Davis } else if (boot >= 0) {
1153e4b0a90eSBrooks Davis if (boot)
1154e4b0a90eSBrooks Davis md.md_flags |= G_ELI_FLAG_BOOT;
1155e4b0a90eSBrooks Davis else
1156e4b0a90eSBrooks Davis md.md_flags &= ~G_ELI_FLAG_BOOT;
1157e4b0a90eSBrooks Davis changed = 1;
1158e4b0a90eSBrooks Davis }
1159e4b0a90eSBrooks Davis
1160e4b0a90eSBrooks Davis if (geliboot == 1 && (md.md_flags & G_ELI_FLAG_GELIBOOT)) {
1161e4b0a90eSBrooks Davis if (verbose)
1162e4b0a90eSBrooks Davis printf("GELIBOOT flag already configured for %s.\n", prov);
1163e4b0a90eSBrooks Davis } else if (geliboot == 0 && !(md.md_flags & G_ELI_FLAG_GELIBOOT)) {
1164e4b0a90eSBrooks Davis if (verbose)
1165e4b0a90eSBrooks Davis printf("GELIBOOT flag not configured for %s.\n", prov);
1166e4b0a90eSBrooks Davis } else if (geliboot >= 0) {
1167e4b0a90eSBrooks Davis if (geliboot)
1168e4b0a90eSBrooks Davis md.md_flags |= G_ELI_FLAG_GELIBOOT;
1169e4b0a90eSBrooks Davis else
1170e4b0a90eSBrooks Davis md.md_flags &= ~G_ELI_FLAG_GELIBOOT;
1171e4b0a90eSBrooks Davis changed = 1;
1172e4b0a90eSBrooks Davis }
1173e4b0a90eSBrooks Davis
1174e4b0a90eSBrooks Davis if (displaypass == 1 && (md.md_flags & G_ELI_FLAG_GELIDISPLAYPASS)) {
1175e4b0a90eSBrooks Davis if (verbose)
1176e4b0a90eSBrooks Davis printf("GELIDISPLAYPASS flag already configured for %s.\n", prov);
1177e4b0a90eSBrooks Davis } else if (displaypass == 0 &&
1178e4b0a90eSBrooks Davis !(md.md_flags & G_ELI_FLAG_GELIDISPLAYPASS)) {
1179e4b0a90eSBrooks Davis if (verbose)
1180e4b0a90eSBrooks Davis printf("GELIDISPLAYPASS flag not configured for %s.\n", prov);
1181e4b0a90eSBrooks Davis } else if (displaypass >= 0) {
1182e4b0a90eSBrooks Davis if (displaypass)
1183e4b0a90eSBrooks Davis md.md_flags |= G_ELI_FLAG_GELIDISPLAYPASS;
1184e4b0a90eSBrooks Davis else
1185e4b0a90eSBrooks Davis md.md_flags &= ~G_ELI_FLAG_GELIDISPLAYPASS;
1186e4b0a90eSBrooks Davis changed = 1;
1187e4b0a90eSBrooks Davis }
1188e4b0a90eSBrooks Davis
1189e4b0a90eSBrooks Davis if (trim == 0 && (md.md_flags & G_ELI_FLAG_NODELETE)) {
1190e4b0a90eSBrooks Davis if (verbose)
1191e4b0a90eSBrooks Davis printf("TRIM disable flag already configured for %s.\n", prov);
1192e4b0a90eSBrooks Davis } else if (trim == 1 && !(md.md_flags & G_ELI_FLAG_NODELETE)) {
1193e4b0a90eSBrooks Davis if (verbose)
1194e4b0a90eSBrooks Davis printf("TRIM disable flag not configured for %s.\n", prov);
1195e4b0a90eSBrooks Davis } else if (trim >= 0) {
1196e4b0a90eSBrooks Davis if (trim)
1197e4b0a90eSBrooks Davis md.md_flags &= ~G_ELI_FLAG_NODELETE;
1198e4b0a90eSBrooks Davis else
1199e4b0a90eSBrooks Davis md.md_flags |= G_ELI_FLAG_NODELETE;
1200e4b0a90eSBrooks Davis changed = 1;
1201e4b0a90eSBrooks Davis }
1202e4b0a90eSBrooks Davis
12032f07cdf8SPawel Jakub Dawidek if (autoresize == 1 && (md.md_flags & G_ELI_FLAG_AUTORESIZE)) {
12042f07cdf8SPawel Jakub Dawidek if (verbose)
12052f07cdf8SPawel Jakub Dawidek printf("AUTORESIZE flag already configured for %s.\n", prov);
12062f07cdf8SPawel Jakub Dawidek } else if (autoresize == 0 && !(md.md_flags & G_ELI_FLAG_AUTORESIZE)) {
12072f07cdf8SPawel Jakub Dawidek if (verbose)
12082f07cdf8SPawel Jakub Dawidek printf("AUTORESIZE flag not configured for %s.\n", prov);
12092f07cdf8SPawel Jakub Dawidek } else if (autoresize >= 0) {
12102f07cdf8SPawel Jakub Dawidek if (autoresize)
12112f07cdf8SPawel Jakub Dawidek md.md_flags |= G_ELI_FLAG_AUTORESIZE;
12122f07cdf8SPawel Jakub Dawidek else
12132f07cdf8SPawel Jakub Dawidek md.md_flags &= ~G_ELI_FLAG_AUTORESIZE;
12142f07cdf8SPawel Jakub Dawidek changed = 1;
12152f07cdf8SPawel Jakub Dawidek }
12162f07cdf8SPawel Jakub Dawidek
1217e4b0a90eSBrooks Davis if (changed)
1218e4b0a90eSBrooks Davis eli_metadata_store(req, prov, &md);
1219217df2daSBen Woods explicit_bzero(&md, sizeof(md));
1220e4b0a90eSBrooks Davis }
1221e4b0a90eSBrooks Davis
1222e4b0a90eSBrooks Davis static void
eli_configure(struct gctl_req * req)1223e4b0a90eSBrooks Davis eli_configure(struct gctl_req *req)
1224e4b0a90eSBrooks Davis {
1225e4b0a90eSBrooks Davis const char *prov;
1226e4b0a90eSBrooks Davis bool boot, noboot, geliboot, nogeliboot, displaypass, nodisplaypass;
12272f07cdf8SPawel Jakub Dawidek bool autoresize, noautoresize, trim, notrim;
12282f07cdf8SPawel Jakub Dawidek int doboot, dogeliboot, dodisplaypass, dotrim, doautoresize;
1229e4b0a90eSBrooks Davis int i, nargs;
1230e4b0a90eSBrooks Davis
1231e4b0a90eSBrooks Davis nargs = gctl_get_int(req, "nargs");
1232e4b0a90eSBrooks Davis if (nargs == 0) {
1233e4b0a90eSBrooks Davis gctl_error(req, "Too few arguments.");
1234e4b0a90eSBrooks Davis return;
1235e4b0a90eSBrooks Davis }
1236e4b0a90eSBrooks Davis
1237e4b0a90eSBrooks Davis boot = gctl_get_int(req, "boot");
1238e4b0a90eSBrooks Davis noboot = gctl_get_int(req, "noboot");
1239e4b0a90eSBrooks Davis geliboot = gctl_get_int(req, "geliboot");
1240e4b0a90eSBrooks Davis nogeliboot = gctl_get_int(req, "nogeliboot");
1241e4b0a90eSBrooks Davis displaypass = gctl_get_int(req, "displaypass");
1242e4b0a90eSBrooks Davis nodisplaypass = gctl_get_int(req, "nodisplaypass");
1243e4b0a90eSBrooks Davis trim = gctl_get_int(req, "trim");
1244e4b0a90eSBrooks Davis notrim = gctl_get_int(req, "notrim");
12452f07cdf8SPawel Jakub Dawidek autoresize = gctl_get_int(req, "autoresize");
12462f07cdf8SPawel Jakub Dawidek noautoresize = gctl_get_int(req, "noautoresize");
1247e4b0a90eSBrooks Davis
1248e4b0a90eSBrooks Davis doboot = -1;
1249e4b0a90eSBrooks Davis if (boot && noboot) {
1250e4b0a90eSBrooks Davis gctl_error(req, "Options -b and -B are mutually exclusive.");
1251e4b0a90eSBrooks Davis return;
1252e4b0a90eSBrooks Davis }
1253e4b0a90eSBrooks Davis if (boot)
1254e4b0a90eSBrooks Davis doboot = 1;
1255e4b0a90eSBrooks Davis else if (noboot)
1256e4b0a90eSBrooks Davis doboot = 0;
1257e4b0a90eSBrooks Davis
1258e4b0a90eSBrooks Davis dogeliboot = -1;
1259e4b0a90eSBrooks Davis if (geliboot && nogeliboot) {
1260e4b0a90eSBrooks Davis gctl_error(req, "Options -g and -G are mutually exclusive.");
1261e4b0a90eSBrooks Davis return;
1262e4b0a90eSBrooks Davis }
1263e4b0a90eSBrooks Davis if (geliboot)
1264e4b0a90eSBrooks Davis dogeliboot = 1;
1265e4b0a90eSBrooks Davis else if (nogeliboot)
1266e4b0a90eSBrooks Davis dogeliboot = 0;
1267e4b0a90eSBrooks Davis
1268e4b0a90eSBrooks Davis dodisplaypass = -1;
1269e4b0a90eSBrooks Davis if (displaypass && nodisplaypass) {
1270e4b0a90eSBrooks Davis gctl_error(req, "Options -d and -D are mutually exclusive.");
1271e4b0a90eSBrooks Davis return;
1272e4b0a90eSBrooks Davis }
1273e4b0a90eSBrooks Davis if (displaypass)
1274e4b0a90eSBrooks Davis dodisplaypass = 1;
1275e4b0a90eSBrooks Davis else if (nodisplaypass)
1276e4b0a90eSBrooks Davis dodisplaypass = 0;
1277e4b0a90eSBrooks Davis
1278e4b0a90eSBrooks Davis dotrim = -1;
1279e4b0a90eSBrooks Davis if (trim && notrim) {
1280e4b0a90eSBrooks Davis gctl_error(req, "Options -t and -T are mutually exclusive.");
1281e4b0a90eSBrooks Davis return;
1282e4b0a90eSBrooks Davis }
1283e4b0a90eSBrooks Davis if (trim)
1284e4b0a90eSBrooks Davis dotrim = 1;
1285e4b0a90eSBrooks Davis else if (notrim)
1286e4b0a90eSBrooks Davis dotrim = 0;
1287e4b0a90eSBrooks Davis
12882f07cdf8SPawel Jakub Dawidek doautoresize = -1;
12892f07cdf8SPawel Jakub Dawidek if (autoresize && noautoresize) {
12902f07cdf8SPawel Jakub Dawidek gctl_error(req, "Options -r and -R are mutually exclusive.");
12912f07cdf8SPawel Jakub Dawidek return;
12922f07cdf8SPawel Jakub Dawidek }
12932f07cdf8SPawel Jakub Dawidek if (autoresize)
12942f07cdf8SPawel Jakub Dawidek doautoresize = 1;
12952f07cdf8SPawel Jakub Dawidek else if (noautoresize)
12962f07cdf8SPawel Jakub Dawidek doautoresize = 0;
12972f07cdf8SPawel Jakub Dawidek
1298e4b0a90eSBrooks Davis if (doboot == -1 && dogeliboot == -1 && dodisplaypass == -1 &&
12992f07cdf8SPawel Jakub Dawidek dotrim == -1 && doautoresize == -1) {
1300e4b0a90eSBrooks Davis gctl_error(req, "No option given.");
1301e4b0a90eSBrooks Davis return;
1302e4b0a90eSBrooks Davis }
1303e4b0a90eSBrooks Davis
1304e4b0a90eSBrooks Davis /* First attached providers. */
1305e4b0a90eSBrooks Davis gctl_issue(req);
1306e4b0a90eSBrooks Davis /* Now the rest. */
1307e4b0a90eSBrooks Davis for (i = 0; i < nargs; i++) {
1308e4b0a90eSBrooks Davis prov = gctl_get_ascii(req, "arg%d", i);
1309e4b0a90eSBrooks Davis if (!eli_is_attached(prov)) {
1310e4b0a90eSBrooks Davis eli_configure_detached(req, prov, doboot, dogeliboot,
13112f07cdf8SPawel Jakub Dawidek dodisplaypass, dotrim, doautoresize);
1312e4b0a90eSBrooks Davis }
1313e4b0a90eSBrooks Davis }
1314e4b0a90eSBrooks Davis }
1315e4b0a90eSBrooks Davis
1316e4b0a90eSBrooks Davis static void
eli_setkey_attached(struct gctl_req * req,struct g_eli_metadata * md)1317e4b0a90eSBrooks Davis eli_setkey_attached(struct gctl_req *req, struct g_eli_metadata *md)
1318e4b0a90eSBrooks Davis {
1319e4b0a90eSBrooks Davis unsigned char key[G_ELI_USERKEYLEN];
1320e4b0a90eSBrooks Davis intmax_t val, old = 0;
1321e4b0a90eSBrooks Davis int error;
1322e4b0a90eSBrooks Davis
1323e4b0a90eSBrooks Davis val = gctl_get_intmax(req, "iterations");
1324e4b0a90eSBrooks Davis /* Check if iterations number should be changed. */
1325e4b0a90eSBrooks Davis if (val != -1)
1326e4b0a90eSBrooks Davis md->md_iterations = val;
1327e4b0a90eSBrooks Davis else
1328e4b0a90eSBrooks Davis old = md->md_iterations;
1329e4b0a90eSBrooks Davis
1330e4b0a90eSBrooks Davis /* Generate key for Master Key encryption. */
13315fff0966SMariusz Zaborski if (eli_genkey_single(req, md, key, true) == NULL) {
1332217df2daSBen Woods explicit_bzero(key, sizeof(key));
1333e4b0a90eSBrooks Davis return;
1334e4b0a90eSBrooks Davis }
1335e4b0a90eSBrooks Davis /*
1336e4b0a90eSBrooks Davis * If number of iterations has changed, but wasn't given as a
1337e4b0a90eSBrooks Davis * command-line argument, update the request.
1338e4b0a90eSBrooks Davis */
1339e4b0a90eSBrooks Davis if (val == -1 && md->md_iterations != old) {
1340e4b0a90eSBrooks Davis error = gctl_change_param(req, "iterations", sizeof(intmax_t),
1341e4b0a90eSBrooks Davis &md->md_iterations);
1342e4b0a90eSBrooks Davis assert(error == 0);
1343e4b0a90eSBrooks Davis }
1344e4b0a90eSBrooks Davis
1345e4b0a90eSBrooks Davis gctl_ro_param(req, "key", sizeof(key), key);
1346e4b0a90eSBrooks Davis gctl_issue(req);
1347217df2daSBen Woods explicit_bzero(key, sizeof(key));
1348e4b0a90eSBrooks Davis }
1349e4b0a90eSBrooks Davis
1350e4b0a90eSBrooks Davis static void
eli_setkey_detached(struct gctl_req * req,const char * prov,struct g_eli_metadata * md)1351e4b0a90eSBrooks Davis eli_setkey_detached(struct gctl_req *req, const char *prov,
1352e4b0a90eSBrooks Davis struct g_eli_metadata *md)
1353e4b0a90eSBrooks Davis {
1354e4b0a90eSBrooks Davis unsigned char key[G_ELI_USERKEYLEN], mkey[G_ELI_DATAIVKEYLEN];
1355e4b0a90eSBrooks Davis unsigned char *mkeydst;
1356e4b0a90eSBrooks Davis unsigned int nkey;
1357e4b0a90eSBrooks Davis intmax_t val;
1358e4b0a90eSBrooks Davis int error;
1359e4b0a90eSBrooks Davis
1360e4b0a90eSBrooks Davis if (md->md_keys == 0) {
1361e4b0a90eSBrooks Davis gctl_error(req, "No valid keys on %s.", prov);
1362e4b0a90eSBrooks Davis return;
1363e4b0a90eSBrooks Davis }
1364e4b0a90eSBrooks Davis
1365e4b0a90eSBrooks Davis /* Generate key for Master Key decryption. */
13665fff0966SMariusz Zaborski if (eli_genkey_single(req, md, key, false) == NULL) {
1367217df2daSBen Woods explicit_bzero(key, sizeof(key));
1368e4b0a90eSBrooks Davis return;
1369e4b0a90eSBrooks Davis }
1370e4b0a90eSBrooks Davis
1371e4b0a90eSBrooks Davis /* Decrypt Master Key. */
1372e4b0a90eSBrooks Davis error = g_eli_mkey_decrypt_any(md, key, mkey, &nkey);
1373217df2daSBen Woods explicit_bzero(key, sizeof(key));
1374e4b0a90eSBrooks Davis if (error != 0) {
1375217df2daSBen Woods explicit_bzero(md, sizeof(*md));
1376e4b0a90eSBrooks Davis if (error == -1)
1377e4b0a90eSBrooks Davis gctl_error(req, "Wrong key for %s.", prov);
1378e4b0a90eSBrooks Davis else /* if (error > 0) */ {
1379e4b0a90eSBrooks Davis gctl_error(req, "Cannot decrypt Master Key: %s.",
1380e4b0a90eSBrooks Davis strerror(error));
1381e4b0a90eSBrooks Davis }
1382e4b0a90eSBrooks Davis return;
1383e4b0a90eSBrooks Davis }
1384e4b0a90eSBrooks Davis if (verbose)
1385e4b0a90eSBrooks Davis printf("Decrypted Master Key %u.\n", nkey);
1386e4b0a90eSBrooks Davis
1387e4b0a90eSBrooks Davis val = gctl_get_intmax(req, "keyno");
1388e4b0a90eSBrooks Davis if (val != -1)
1389e4b0a90eSBrooks Davis nkey = val;
1390e4b0a90eSBrooks Davis #if 0
1391e4b0a90eSBrooks Davis else
1392e4b0a90eSBrooks Davis ; /* Use the key number which was found during decryption. */
1393e4b0a90eSBrooks Davis #endif
1394e4b0a90eSBrooks Davis if (nkey >= G_ELI_MAXMKEYS) {
1395e4b0a90eSBrooks Davis gctl_error(req, "Invalid '%s' argument.", "keyno");
1396e4b0a90eSBrooks Davis return;
1397e4b0a90eSBrooks Davis }
1398e4b0a90eSBrooks Davis
1399e4b0a90eSBrooks Davis val = gctl_get_intmax(req, "iterations");
1400e4b0a90eSBrooks Davis /* Check if iterations number should and can be changed. */
1401e4b0a90eSBrooks Davis if (val != -1 && md->md_iterations == -1) {
1402e4b0a90eSBrooks Davis md->md_iterations = val;
1403e4b0a90eSBrooks Davis } else if (val != -1 && val != md->md_iterations) {
1404e4b0a90eSBrooks Davis if (bitcount32(md->md_keys) != 1) {
1405e4b0a90eSBrooks Davis gctl_error(req, "To be able to use '-i' option, only "
1406e4b0a90eSBrooks Davis "one key can be defined.");
1407e4b0a90eSBrooks Davis return;
1408e4b0a90eSBrooks Davis }
1409e4b0a90eSBrooks Davis if (md->md_keys != (1 << nkey)) {
1410e4b0a90eSBrooks Davis gctl_error(req, "Only already defined key can be "
1411e4b0a90eSBrooks Davis "changed when '-i' option is used.");
1412e4b0a90eSBrooks Davis return;
1413e4b0a90eSBrooks Davis }
1414e4b0a90eSBrooks Davis md->md_iterations = val;
1415e4b0a90eSBrooks Davis }
1416e4b0a90eSBrooks Davis
1417e4b0a90eSBrooks Davis mkeydst = md->md_mkeys + nkey * G_ELI_MKEYLEN;
1418e4b0a90eSBrooks Davis md->md_keys |= (1 << nkey);
1419e4b0a90eSBrooks Davis
1420e4b0a90eSBrooks Davis bcopy(mkey, mkeydst, sizeof(mkey));
1421217df2daSBen Woods explicit_bzero(mkey, sizeof(mkey));
1422e4b0a90eSBrooks Davis
1423*88d13bf3SArjan de Vet /*
1424*88d13bf3SArjan de Vet * The previous eli_genkey() set cached_passphrase, we do not want to
1425*88d13bf3SArjan de Vet * use that for the new passphrase so always prompt for it
1426*88d13bf3SArjan de Vet */
1427*88d13bf3SArjan de Vet explicit_bzero(cached_passphrase, sizeof(cached_passphrase));
1428*88d13bf3SArjan de Vet
1429e4b0a90eSBrooks Davis /* Generate key for Master Key encryption. */
14305fff0966SMariusz Zaborski if (eli_genkey_single(req, md, key, true) == NULL) {
1431217df2daSBen Woods explicit_bzero(key, sizeof(key));
1432217df2daSBen Woods explicit_bzero(md, sizeof(*md));
1433e4b0a90eSBrooks Davis return;
1434e4b0a90eSBrooks Davis }
1435e4b0a90eSBrooks Davis
1436e4b0a90eSBrooks Davis /* Encrypt the Master-Key with the new key. */
1437e4b0a90eSBrooks Davis error = g_eli_mkey_encrypt(md->md_ealgo, key, md->md_keylen, mkeydst);
1438217df2daSBen Woods explicit_bzero(key, sizeof(key));
1439e4b0a90eSBrooks Davis if (error != 0) {
1440217df2daSBen Woods explicit_bzero(md, sizeof(*md));
1441e4b0a90eSBrooks Davis gctl_error(req, "Cannot encrypt Master Key: %s.",
1442e4b0a90eSBrooks Davis strerror(error));
1443e4b0a90eSBrooks Davis return;
1444e4b0a90eSBrooks Davis }
1445e4b0a90eSBrooks Davis
1446e4b0a90eSBrooks Davis /* Store metadata with fresh key. */
1447e4b0a90eSBrooks Davis eli_metadata_store(req, prov, md);
1448217df2daSBen Woods explicit_bzero(md, sizeof(*md));
1449e4b0a90eSBrooks Davis }
1450e4b0a90eSBrooks Davis
1451e4b0a90eSBrooks Davis static void
eli_setkey(struct gctl_req * req)1452e4b0a90eSBrooks Davis eli_setkey(struct gctl_req *req)
1453e4b0a90eSBrooks Davis {
1454e4b0a90eSBrooks Davis struct g_eli_metadata md;
1455e4b0a90eSBrooks Davis const char *prov;
1456e4b0a90eSBrooks Davis int nargs;
1457e4b0a90eSBrooks Davis
1458e4b0a90eSBrooks Davis nargs = gctl_get_int(req, "nargs");
1459e4b0a90eSBrooks Davis if (nargs != 1) {
1460e4b0a90eSBrooks Davis gctl_error(req, "Invalid number of arguments.");
1461e4b0a90eSBrooks Davis return;
1462e4b0a90eSBrooks Davis }
1463e4b0a90eSBrooks Davis prov = gctl_get_ascii(req, "arg0");
1464e4b0a90eSBrooks Davis
1465e4b0a90eSBrooks Davis if (eli_metadata_read(req, prov, &md) == -1)
1466e4b0a90eSBrooks Davis return;
1467e4b0a90eSBrooks Davis
1468e4b0a90eSBrooks Davis if (eli_is_attached(prov))
1469e4b0a90eSBrooks Davis eli_setkey_attached(req, &md);
1470e4b0a90eSBrooks Davis else
1471e4b0a90eSBrooks Davis eli_setkey_detached(req, prov, &md);
1472e4b0a90eSBrooks Davis
1473e4b0a90eSBrooks Davis if (req->error == NULL || req->error[0] == '\0') {
1474e4b0a90eSBrooks Davis printf("Note, that the master key encrypted with old keys "
147506db6a9dSMariusz Zaborski "and/or passphrase may still exist in a metadata backup "
1476e4b0a90eSBrooks Davis "file.\n");
1477e4b0a90eSBrooks Davis }
1478e4b0a90eSBrooks Davis }
1479e4b0a90eSBrooks Davis
1480e4b0a90eSBrooks Davis static void
eli_delkey_attached(struct gctl_req * req,const char * prov __unused)1481e4b0a90eSBrooks Davis eli_delkey_attached(struct gctl_req *req, const char *prov __unused)
1482e4b0a90eSBrooks Davis {
1483e4b0a90eSBrooks Davis
1484e4b0a90eSBrooks Davis gctl_issue(req);
1485e4b0a90eSBrooks Davis }
1486e4b0a90eSBrooks Davis
1487e4b0a90eSBrooks Davis static void
eli_delkey_detached(struct gctl_req * req,const char * prov)1488e4b0a90eSBrooks Davis eli_delkey_detached(struct gctl_req *req, const char *prov)
1489e4b0a90eSBrooks Davis {
1490e4b0a90eSBrooks Davis struct g_eli_metadata md;
1491e4b0a90eSBrooks Davis unsigned char *mkeydst;
1492e4b0a90eSBrooks Davis unsigned int nkey;
1493e4b0a90eSBrooks Davis intmax_t val;
1494e4b0a90eSBrooks Davis bool all, force;
1495e4b0a90eSBrooks Davis
1496e4b0a90eSBrooks Davis if (eli_metadata_read(req, prov, &md) == -1)
1497e4b0a90eSBrooks Davis return;
1498e4b0a90eSBrooks Davis
1499e4b0a90eSBrooks Davis all = gctl_get_int(req, "all");
1500e4b0a90eSBrooks Davis if (all)
1501e4b0a90eSBrooks Davis arc4random_buf(md.md_mkeys, sizeof(md.md_mkeys));
1502e4b0a90eSBrooks Davis else {
1503e4b0a90eSBrooks Davis force = gctl_get_int(req, "force");
1504e4b0a90eSBrooks Davis val = gctl_get_intmax(req, "keyno");
1505e4b0a90eSBrooks Davis if (val == -1) {
1506e4b0a90eSBrooks Davis gctl_error(req, "Key number has to be specified.");
1507e4b0a90eSBrooks Davis return;
1508e4b0a90eSBrooks Davis }
1509e4b0a90eSBrooks Davis nkey = val;
1510e4b0a90eSBrooks Davis if (nkey >= G_ELI_MAXMKEYS) {
1511e4b0a90eSBrooks Davis gctl_error(req, "Invalid '%s' argument.", "keyno");
1512e4b0a90eSBrooks Davis return;
1513e4b0a90eSBrooks Davis }
1514e4b0a90eSBrooks Davis if (!(md.md_keys & (1 << nkey)) && !force) {
1515e4b0a90eSBrooks Davis gctl_error(req, "Master Key %u is not set.", nkey);
1516e4b0a90eSBrooks Davis return;
1517e4b0a90eSBrooks Davis }
1518e4b0a90eSBrooks Davis md.md_keys &= ~(1 << nkey);
1519e4b0a90eSBrooks Davis if (md.md_keys == 0 && !force) {
1520e4b0a90eSBrooks Davis gctl_error(req, "This is the last Master Key. Use '-f' "
1521e4b0a90eSBrooks Davis "option if you really want to remove it.");
1522e4b0a90eSBrooks Davis return;
1523e4b0a90eSBrooks Davis }
1524e4b0a90eSBrooks Davis mkeydst = md.md_mkeys + nkey * G_ELI_MKEYLEN;
1525e4b0a90eSBrooks Davis arc4random_buf(mkeydst, G_ELI_MKEYLEN);
1526e4b0a90eSBrooks Davis }
1527e4b0a90eSBrooks Davis
1528e4b0a90eSBrooks Davis eli_metadata_store(req, prov, &md);
1529217df2daSBen Woods explicit_bzero(&md, sizeof(md));
1530e4b0a90eSBrooks Davis }
1531e4b0a90eSBrooks Davis
1532e4b0a90eSBrooks Davis static void
eli_delkey(struct gctl_req * req)1533e4b0a90eSBrooks Davis eli_delkey(struct gctl_req *req)
1534e4b0a90eSBrooks Davis {
1535e4b0a90eSBrooks Davis const char *prov;
1536e4b0a90eSBrooks Davis int nargs;
1537e4b0a90eSBrooks Davis
1538e4b0a90eSBrooks Davis nargs = gctl_get_int(req, "nargs");
1539e4b0a90eSBrooks Davis if (nargs != 1) {
1540e4b0a90eSBrooks Davis gctl_error(req, "Invalid number of arguments.");
1541e4b0a90eSBrooks Davis return;
1542e4b0a90eSBrooks Davis }
1543e4b0a90eSBrooks Davis prov = gctl_get_ascii(req, "arg0");
1544e4b0a90eSBrooks Davis
1545e4b0a90eSBrooks Davis if (eli_is_attached(prov))
1546e4b0a90eSBrooks Davis eli_delkey_attached(req, prov);
1547e4b0a90eSBrooks Davis else
1548e4b0a90eSBrooks Davis eli_delkey_detached(req, prov);
1549e4b0a90eSBrooks Davis }
1550e4b0a90eSBrooks Davis
1551e4b0a90eSBrooks Davis static void
eli_resume(struct gctl_req * req)1552e4b0a90eSBrooks Davis eli_resume(struct gctl_req *req)
1553e4b0a90eSBrooks Davis {
1554e4b0a90eSBrooks Davis struct g_eli_metadata md;
1555e4b0a90eSBrooks Davis unsigned char key[G_ELI_USERKEYLEN];
1556e4b0a90eSBrooks Davis const char *prov;
1557e4b0a90eSBrooks Davis off_t mediasize;
1558e4b0a90eSBrooks Davis int nargs;
1559e4b0a90eSBrooks Davis
1560e4b0a90eSBrooks Davis nargs = gctl_get_int(req, "nargs");
1561e4b0a90eSBrooks Davis if (nargs != 1) {
1562e4b0a90eSBrooks Davis gctl_error(req, "Invalid number of arguments.");
1563e4b0a90eSBrooks Davis return;
1564e4b0a90eSBrooks Davis }
1565e4b0a90eSBrooks Davis prov = gctl_get_ascii(req, "arg0");
1566e4b0a90eSBrooks Davis
1567e4b0a90eSBrooks Davis if (eli_metadata_read(req, prov, &md) == -1)
1568e4b0a90eSBrooks Davis return;
1569e4b0a90eSBrooks Davis
1570e4b0a90eSBrooks Davis mediasize = g_get_mediasize(prov);
1571e4b0a90eSBrooks Davis if (md.md_provsize != (uint64_t)mediasize) {
1572e4b0a90eSBrooks Davis gctl_error(req, "Provider size mismatch.");
1573e4b0a90eSBrooks Davis return;
1574e4b0a90eSBrooks Davis }
1575e4b0a90eSBrooks Davis
15765fff0966SMariusz Zaborski if (eli_genkey_single(req, &md, key, false) == NULL) {
1577217df2daSBen Woods explicit_bzero(key, sizeof(key));
1578e4b0a90eSBrooks Davis return;
1579e4b0a90eSBrooks Davis }
1580e4b0a90eSBrooks Davis
1581e4b0a90eSBrooks Davis gctl_ro_param(req, "key", sizeof(key), key);
1582e4b0a90eSBrooks Davis if (gctl_issue(req) == NULL) {
1583e4b0a90eSBrooks Davis if (verbose)
1584e4b0a90eSBrooks Davis printf("Resumed %s.\n", prov);
1585e4b0a90eSBrooks Davis }
1586217df2daSBen Woods explicit_bzero(key, sizeof(key));
1587e4b0a90eSBrooks Davis }
1588e4b0a90eSBrooks Davis
1589e4b0a90eSBrooks Davis static int
eli_trash_metadata(struct gctl_req * req,const char * prov,int fd,off_t offset)1590e4b0a90eSBrooks Davis eli_trash_metadata(struct gctl_req *req, const char *prov, int fd, off_t offset)
1591e4b0a90eSBrooks Davis {
1592e4b0a90eSBrooks Davis unsigned int overwrites;
1593e4b0a90eSBrooks Davis unsigned char *sector;
1594e4b0a90eSBrooks Davis ssize_t size;
1595e4b0a90eSBrooks Davis int error;
1596e4b0a90eSBrooks Davis
1597e4b0a90eSBrooks Davis size = sizeof(overwrites);
1598e4b0a90eSBrooks Davis if (sysctlbyname("kern.geom.eli.overwrites", &overwrites, &size,
1599e4b0a90eSBrooks Davis NULL, 0) == -1 || overwrites == 0) {
1600e4b0a90eSBrooks Davis overwrites = G_ELI_OVERWRITES;
1601e4b0a90eSBrooks Davis }
1602e4b0a90eSBrooks Davis
1603e4b0a90eSBrooks Davis size = g_sectorsize(fd);
1604e4b0a90eSBrooks Davis if (size <= 0) {
1605e4b0a90eSBrooks Davis gctl_error(req, "Cannot obtain provider sector size %s: %s.",
1606e4b0a90eSBrooks Davis prov, strerror(errno));
1607e4b0a90eSBrooks Davis return (-1);
1608e4b0a90eSBrooks Davis }
1609e4b0a90eSBrooks Davis sector = malloc(size);
1610e4b0a90eSBrooks Davis if (sector == NULL) {
1611e4b0a90eSBrooks Davis gctl_error(req, "Cannot allocate %zd bytes of memory.", size);
1612e4b0a90eSBrooks Davis return (-1);
1613e4b0a90eSBrooks Davis }
1614e4b0a90eSBrooks Davis
1615e4b0a90eSBrooks Davis error = 0;
1616e4b0a90eSBrooks Davis do {
1617e4b0a90eSBrooks Davis arc4random_buf(sector, size);
1618e4b0a90eSBrooks Davis if (pwrite(fd, sector, size, offset) != size) {
1619e4b0a90eSBrooks Davis if (error == 0)
1620e4b0a90eSBrooks Davis error = errno;
1621e4b0a90eSBrooks Davis }
1622e4b0a90eSBrooks Davis (void)g_flush(fd);
1623e4b0a90eSBrooks Davis } while (--overwrites > 0);
1624e4b0a90eSBrooks Davis free(sector);
1625e4b0a90eSBrooks Davis if (error != 0) {
1626e4b0a90eSBrooks Davis gctl_error(req, "Cannot trash metadata on provider %s: %s.",
1627e4b0a90eSBrooks Davis prov, strerror(error));
1628e4b0a90eSBrooks Davis return (-1);
1629e4b0a90eSBrooks Davis }
1630e4b0a90eSBrooks Davis return (0);
1631e4b0a90eSBrooks Davis }
1632e4b0a90eSBrooks Davis
1633e4b0a90eSBrooks Davis static void
eli_kill_detached(struct gctl_req * req,const char * prov)1634e4b0a90eSBrooks Davis eli_kill_detached(struct gctl_req *req, const char *prov)
1635e4b0a90eSBrooks Davis {
1636e4b0a90eSBrooks Davis off_t offset;
1637e4b0a90eSBrooks Davis int fd;
1638e4b0a90eSBrooks Davis
1639e4b0a90eSBrooks Davis /*
1640e4b0a90eSBrooks Davis * NOTE: Maybe we should verify if this is geli provider first,
1641e4b0a90eSBrooks Davis * but 'kill' command is quite critical so better don't waste
1642e4b0a90eSBrooks Davis * the time.
1643e4b0a90eSBrooks Davis */
1644e4b0a90eSBrooks Davis #if 0
1645e4b0a90eSBrooks Davis error = g_metadata_read(prov, (unsigned char *)&md, sizeof(md),
1646e4b0a90eSBrooks Davis G_ELI_MAGIC);
1647e4b0a90eSBrooks Davis if (error != 0) {
1648e4b0a90eSBrooks Davis gctl_error(req, "Cannot read metadata from %s: %s.", prov,
1649e4b0a90eSBrooks Davis strerror(error));
1650e4b0a90eSBrooks Davis return;
1651e4b0a90eSBrooks Davis }
1652e4b0a90eSBrooks Davis #endif
1653e4b0a90eSBrooks Davis
1654e4b0a90eSBrooks Davis fd = g_open(prov, 1);
1655e4b0a90eSBrooks Davis if (fd == -1) {
1656e4b0a90eSBrooks Davis gctl_error(req, "Cannot open provider %s: %s.", prov,
1657e4b0a90eSBrooks Davis strerror(errno));
1658e4b0a90eSBrooks Davis return;
1659e4b0a90eSBrooks Davis }
1660e4b0a90eSBrooks Davis offset = g_mediasize(fd) - g_sectorsize(fd);
1661e4b0a90eSBrooks Davis if (offset <= 0) {
1662e4b0a90eSBrooks Davis gctl_error(req,
1663e4b0a90eSBrooks Davis "Cannot obtain media size or sector size for provider %s: %s.",
1664e4b0a90eSBrooks Davis prov, strerror(errno));
1665e4b0a90eSBrooks Davis (void)g_close(fd);
1666e4b0a90eSBrooks Davis return;
1667e4b0a90eSBrooks Davis }
1668e4b0a90eSBrooks Davis (void)eli_trash_metadata(req, prov, fd, offset);
1669e4b0a90eSBrooks Davis (void)g_close(fd);
1670e4b0a90eSBrooks Davis }
1671e4b0a90eSBrooks Davis
1672e4b0a90eSBrooks Davis static void
eli_kill(struct gctl_req * req)1673e4b0a90eSBrooks Davis eli_kill(struct gctl_req *req)
1674e4b0a90eSBrooks Davis {
1675e4b0a90eSBrooks Davis const char *prov;
1676e4b0a90eSBrooks Davis int i, nargs, all;
1677e4b0a90eSBrooks Davis
1678e4b0a90eSBrooks Davis nargs = gctl_get_int(req, "nargs");
1679e4b0a90eSBrooks Davis all = gctl_get_int(req, "all");
1680e4b0a90eSBrooks Davis if (!all && nargs == 0) {
1681e4b0a90eSBrooks Davis gctl_error(req, "Too few arguments.");
1682e4b0a90eSBrooks Davis return;
1683e4b0a90eSBrooks Davis }
1684e4b0a90eSBrooks Davis /*
1685e4b0a90eSBrooks Davis * How '-a' option combine with a list of providers:
1686e4b0a90eSBrooks Davis * Delete Master Keys from all attached providers:
1687e4b0a90eSBrooks Davis * geli kill -a
1688e4b0a90eSBrooks Davis * Delete Master Keys from all attached providers and from
1689e4b0a90eSBrooks Davis * detached da0 and da1:
1690e4b0a90eSBrooks Davis * geli kill -a da0 da1
1691e4b0a90eSBrooks Davis * Delete Master Keys from (attached or detached) da0 and da1:
1692e4b0a90eSBrooks Davis * geli kill da0 da1
1693e4b0a90eSBrooks Davis */
1694e4b0a90eSBrooks Davis
1695e4b0a90eSBrooks Davis /* First detached providers. */
1696e4b0a90eSBrooks Davis for (i = 0; i < nargs; i++) {
1697e4b0a90eSBrooks Davis prov = gctl_get_ascii(req, "arg%d", i);
1698e4b0a90eSBrooks Davis if (!eli_is_attached(prov))
1699e4b0a90eSBrooks Davis eli_kill_detached(req, prov);
1700e4b0a90eSBrooks Davis }
1701e4b0a90eSBrooks Davis /* Now attached providers. */
1702e4b0a90eSBrooks Davis gctl_issue(req);
1703e4b0a90eSBrooks Davis }
1704e4b0a90eSBrooks Davis
1705e4b0a90eSBrooks Davis static int
eli_backup_create(struct gctl_req * req,const char * prov,const char * file)1706e4b0a90eSBrooks Davis eli_backup_create(struct gctl_req *req, const char *prov, const char *file)
1707e4b0a90eSBrooks Davis {
1708e4b0a90eSBrooks Davis unsigned char *sector;
1709e4b0a90eSBrooks Davis ssize_t secsize;
1710e4b0a90eSBrooks Davis int error, filefd, ret;
1711e4b0a90eSBrooks Davis
1712e4b0a90eSBrooks Davis ret = -1;
1713e4b0a90eSBrooks Davis filefd = -1;
1714e4b0a90eSBrooks Davis sector = NULL;
1715e4b0a90eSBrooks Davis secsize = 0;
1716e4b0a90eSBrooks Davis
1717e4b0a90eSBrooks Davis secsize = g_get_sectorsize(prov);
1718e4b0a90eSBrooks Davis if (secsize == 0) {
1719e4b0a90eSBrooks Davis gctl_error(req, "Cannot get informations about %s: %s.", prov,
1720e4b0a90eSBrooks Davis strerror(errno));
1721e4b0a90eSBrooks Davis goto out;
1722e4b0a90eSBrooks Davis }
1723e4b0a90eSBrooks Davis sector = malloc(secsize);
1724e4b0a90eSBrooks Davis if (sector == NULL) {
1725e4b0a90eSBrooks Davis gctl_error(req, "Cannot allocate memory.");
1726e4b0a90eSBrooks Davis goto out;
1727e4b0a90eSBrooks Davis }
1728e4b0a90eSBrooks Davis /* Read metadata from the provider. */
1729e4b0a90eSBrooks Davis error = g_metadata_read(prov, sector, secsize, G_ELI_MAGIC);
1730e4b0a90eSBrooks Davis if (error != 0) {
1731e4b0a90eSBrooks Davis gctl_error(req, "Unable to read metadata from %s: %s.", prov,
1732e4b0a90eSBrooks Davis strerror(error));
1733e4b0a90eSBrooks Davis goto out;
1734e4b0a90eSBrooks Davis }
1735e4b0a90eSBrooks Davis
1736e4b0a90eSBrooks Davis filefd = open(file, O_WRONLY | O_TRUNC | O_CREAT, 0600);
1737e4b0a90eSBrooks Davis if (filefd == -1) {
1738e4b0a90eSBrooks Davis gctl_error(req, "Unable to open %s: %s.", file,
1739e4b0a90eSBrooks Davis strerror(errno));
1740e4b0a90eSBrooks Davis goto out;
1741e4b0a90eSBrooks Davis }
1742e4b0a90eSBrooks Davis /* Write metadata to the destination file. */
1743e4b0a90eSBrooks Davis if (write(filefd, sector, secsize) != secsize) {
1744e4b0a90eSBrooks Davis gctl_error(req, "Unable to write to %s: %s.", file,
1745e4b0a90eSBrooks Davis strerror(errno));
1746e4b0a90eSBrooks Davis (void)close(filefd);
1747e4b0a90eSBrooks Davis (void)unlink(file);
1748e4b0a90eSBrooks Davis goto out;
1749e4b0a90eSBrooks Davis }
1750e4b0a90eSBrooks Davis (void)fsync(filefd);
1751e4b0a90eSBrooks Davis (void)close(filefd);
1752e4b0a90eSBrooks Davis /* Success. */
1753e4b0a90eSBrooks Davis ret = 0;
1754e4b0a90eSBrooks Davis out:
1755e4b0a90eSBrooks Davis if (sector != NULL) {
1756217df2daSBen Woods explicit_bzero(sector, secsize);
1757e4b0a90eSBrooks Davis free(sector);
1758e4b0a90eSBrooks Davis }
1759e4b0a90eSBrooks Davis return (ret);
1760e4b0a90eSBrooks Davis }
1761e4b0a90eSBrooks Davis
1762e4b0a90eSBrooks Davis static void
eli_backup(struct gctl_req * req)1763e4b0a90eSBrooks Davis eli_backup(struct gctl_req *req)
1764e4b0a90eSBrooks Davis {
1765e4b0a90eSBrooks Davis const char *file, *prov;
1766e4b0a90eSBrooks Davis int nargs;
1767e4b0a90eSBrooks Davis
1768e4b0a90eSBrooks Davis nargs = gctl_get_int(req, "nargs");
1769e4b0a90eSBrooks Davis if (nargs != 2) {
1770e4b0a90eSBrooks Davis gctl_error(req, "Invalid number of arguments.");
1771e4b0a90eSBrooks Davis return;
1772e4b0a90eSBrooks Davis }
1773e4b0a90eSBrooks Davis prov = gctl_get_ascii(req, "arg0");
1774e4b0a90eSBrooks Davis file = gctl_get_ascii(req, "arg1");
1775e4b0a90eSBrooks Davis
1776e4b0a90eSBrooks Davis eli_backup_create(req, prov, file);
1777e4b0a90eSBrooks Davis }
1778e4b0a90eSBrooks Davis
1779e4b0a90eSBrooks Davis static void
eli_restore(struct gctl_req * req)1780e4b0a90eSBrooks Davis eli_restore(struct gctl_req *req)
1781e4b0a90eSBrooks Davis {
1782e4b0a90eSBrooks Davis struct g_eli_metadata md;
1783e4b0a90eSBrooks Davis const char *file, *prov;
1784e4b0a90eSBrooks Davis off_t mediasize;
1785e4b0a90eSBrooks Davis int nargs;
1786e4b0a90eSBrooks Davis
1787e4b0a90eSBrooks Davis nargs = gctl_get_int(req, "nargs");
1788e4b0a90eSBrooks Davis if (nargs != 2) {
1789e4b0a90eSBrooks Davis gctl_error(req, "Invalid number of arguments.");
1790e4b0a90eSBrooks Davis return;
1791e4b0a90eSBrooks Davis }
1792e4b0a90eSBrooks Davis file = gctl_get_ascii(req, "arg0");
1793e4b0a90eSBrooks Davis prov = gctl_get_ascii(req, "arg1");
1794e4b0a90eSBrooks Davis
1795e4b0a90eSBrooks Davis /* Read metadata from the backup file. */
1796e4b0a90eSBrooks Davis if (eli_metadata_read(req, file, &md) == -1)
1797e4b0a90eSBrooks Davis return;
1798e4b0a90eSBrooks Davis /* Obtain provider's mediasize. */
1799e4b0a90eSBrooks Davis mediasize = g_get_mediasize(prov);
1800e4b0a90eSBrooks Davis if (mediasize == 0) {
1801e4b0a90eSBrooks Davis gctl_error(req, "Cannot get informations about %s: %s.", prov,
1802e4b0a90eSBrooks Davis strerror(errno));
1803e4b0a90eSBrooks Davis return;
1804e4b0a90eSBrooks Davis }
1805e4b0a90eSBrooks Davis /* Check if the provider size has changed since we did the backup. */
1806e4b0a90eSBrooks Davis if (md.md_provsize != (uint64_t)mediasize) {
1807e4b0a90eSBrooks Davis if (gctl_get_int(req, "force")) {
1808e4b0a90eSBrooks Davis md.md_provsize = mediasize;
1809e4b0a90eSBrooks Davis } else {
1810e4b0a90eSBrooks Davis gctl_error(req, "Provider size mismatch: "
1811e4b0a90eSBrooks Davis "wrong backup file?");
1812e4b0a90eSBrooks Davis return;
1813e4b0a90eSBrooks Davis }
1814e4b0a90eSBrooks Davis }
1815e4b0a90eSBrooks Davis /* Write metadata to the provider. */
1816e4b0a90eSBrooks Davis (void)eli_metadata_store(req, prov, &md);
1817e4b0a90eSBrooks Davis }
1818e4b0a90eSBrooks Davis
1819e4b0a90eSBrooks Davis static void
eli_resize(struct gctl_req * req)1820e4b0a90eSBrooks Davis eli_resize(struct gctl_req *req)
1821e4b0a90eSBrooks Davis {
1822e4b0a90eSBrooks Davis struct g_eli_metadata md;
1823e4b0a90eSBrooks Davis const char *prov;
1824e4b0a90eSBrooks Davis unsigned char *sector;
1825e4b0a90eSBrooks Davis ssize_t secsize;
1826e4b0a90eSBrooks Davis off_t mediasize, oldsize;
1827e4b0a90eSBrooks Davis int error, nargs, provfd;
1828e4b0a90eSBrooks Davis
1829e4b0a90eSBrooks Davis nargs = gctl_get_int(req, "nargs");
1830e4b0a90eSBrooks Davis if (nargs != 1) {
1831e4b0a90eSBrooks Davis gctl_error(req, "Invalid number of arguments.");
1832e4b0a90eSBrooks Davis return;
1833e4b0a90eSBrooks Davis }
1834e4b0a90eSBrooks Davis prov = gctl_get_ascii(req, "arg0");
1835e4b0a90eSBrooks Davis
1836e4b0a90eSBrooks Davis provfd = -1;
1837e4b0a90eSBrooks Davis sector = NULL;
1838e4b0a90eSBrooks Davis secsize = 0;
1839e4b0a90eSBrooks Davis
1840e4b0a90eSBrooks Davis provfd = g_open(prov, 1);
1841e4b0a90eSBrooks Davis if (provfd == -1) {
1842e4b0a90eSBrooks Davis gctl_error(req, "Cannot open %s: %s.", prov, strerror(errno));
1843e4b0a90eSBrooks Davis goto out;
1844e4b0a90eSBrooks Davis }
1845e4b0a90eSBrooks Davis
1846e4b0a90eSBrooks Davis mediasize = g_mediasize(provfd);
1847e4b0a90eSBrooks Davis secsize = g_sectorsize(provfd);
1848e4b0a90eSBrooks Davis if (mediasize == -1 || secsize == -1) {
1849e4b0a90eSBrooks Davis gctl_error(req, "Cannot get information about %s: %s.", prov,
1850e4b0a90eSBrooks Davis strerror(errno));
1851e4b0a90eSBrooks Davis goto out;
1852e4b0a90eSBrooks Davis }
1853e4b0a90eSBrooks Davis
1854e4b0a90eSBrooks Davis sector = malloc(secsize);
1855e4b0a90eSBrooks Davis if (sector == NULL) {
1856e4b0a90eSBrooks Davis gctl_error(req, "Cannot allocate memory.");
1857e4b0a90eSBrooks Davis goto out;
1858e4b0a90eSBrooks Davis }
1859e4b0a90eSBrooks Davis
1860e4b0a90eSBrooks Davis oldsize = gctl_get_intmax(req, "oldsize");
1861e4b0a90eSBrooks Davis if (oldsize < 0 || oldsize > mediasize) {
1862e4b0a90eSBrooks Davis gctl_error(req, "Invalid oldsize: Out of range.");
1863e4b0a90eSBrooks Davis goto out;
1864e4b0a90eSBrooks Davis }
1865e4b0a90eSBrooks Davis
1866e4b0a90eSBrooks Davis /* Read metadata from the 'oldsize' offset. */
1867e4b0a90eSBrooks Davis if (pread(provfd, sector, secsize, oldsize - secsize) != secsize) {
1868e4b0a90eSBrooks Davis gctl_error(req, "Cannot read old metadata: %s.",
1869e4b0a90eSBrooks Davis strerror(errno));
1870e4b0a90eSBrooks Davis goto out;
1871e4b0a90eSBrooks Davis }
1872e4b0a90eSBrooks Davis
1873e4b0a90eSBrooks Davis /* Check if this sector contains geli metadata. */
1874e4b0a90eSBrooks Davis error = eli_metadata_decode(sector, &md);
1875e4b0a90eSBrooks Davis switch (error) {
1876e4b0a90eSBrooks Davis case 0:
1877e4b0a90eSBrooks Davis break;
1878e4b0a90eSBrooks Davis case EOPNOTSUPP:
1879e4b0a90eSBrooks Davis gctl_error(req,
1880e4b0a90eSBrooks Davis "Provider's %s metadata version %u is too new.\n"
1881e4b0a90eSBrooks Davis "geli: The highest supported version is %u.",
1882e4b0a90eSBrooks Davis prov, (unsigned int)md.md_version, G_ELI_VERSION);
1883e4b0a90eSBrooks Davis goto out;
1884e4b0a90eSBrooks Davis case EINVAL:
1885e4b0a90eSBrooks Davis gctl_error(req, "Inconsistent provider's %s metadata.", prov);
1886e4b0a90eSBrooks Davis goto out;
1887e4b0a90eSBrooks Davis default:
1888e4b0a90eSBrooks Davis gctl_error(req,
1889e4b0a90eSBrooks Davis "Unexpected error while decoding provider's %s metadata: %s.",
1890e4b0a90eSBrooks Davis prov, strerror(error));
1891e4b0a90eSBrooks Davis goto out;
1892e4b0a90eSBrooks Davis }
1893e4b0a90eSBrooks Davis
1894e4b0a90eSBrooks Davis /*
1895e4b0a90eSBrooks Davis * If the old metadata doesn't have a correct provider size, refuse
1896e4b0a90eSBrooks Davis * to resize.
1897e4b0a90eSBrooks Davis */
1898e4b0a90eSBrooks Davis if (md.md_provsize != (uint64_t)oldsize) {
1899e4b0a90eSBrooks Davis gctl_error(req, "Provider size mismatch at oldsize.");
1900e4b0a90eSBrooks Davis goto out;
1901e4b0a90eSBrooks Davis }
1902e4b0a90eSBrooks Davis
1903c7cf100aSAlexander Motin /* The metadata is valid and nothing has changed. Just exit. */
1904c7cf100aSAlexander Motin if (oldsize == mediasize)
1905c7cf100aSAlexander Motin goto out;
1906c7cf100aSAlexander Motin
1907e4b0a90eSBrooks Davis /*
1908e4b0a90eSBrooks Davis * Update the old metadata with the current provider size and write
1909e4b0a90eSBrooks Davis * it back to the correct place on the provider.
1910e4b0a90eSBrooks Davis */
1911e4b0a90eSBrooks Davis md.md_provsize = mediasize;
1912e4b0a90eSBrooks Davis /* Write metadata to the provider. */
1913e4b0a90eSBrooks Davis (void)eli_metadata_store(req, prov, &md);
1914e4b0a90eSBrooks Davis /* Now trash the old metadata. */
1915e4b0a90eSBrooks Davis (void)eli_trash_metadata(req, prov, provfd, oldsize - secsize);
1916e4b0a90eSBrooks Davis out:
1917e4b0a90eSBrooks Davis if (provfd != -1)
1918e4b0a90eSBrooks Davis (void)g_close(provfd);
1919e4b0a90eSBrooks Davis if (sector != NULL) {
1920217df2daSBen Woods explicit_bzero(sector, secsize);
1921e4b0a90eSBrooks Davis free(sector);
1922e4b0a90eSBrooks Davis }
1923e4b0a90eSBrooks Davis }
1924e4b0a90eSBrooks Davis
1925e4b0a90eSBrooks Davis static void
eli_version(struct gctl_req * req)1926e4b0a90eSBrooks Davis eli_version(struct gctl_req *req)
1927e4b0a90eSBrooks Davis {
1928e4b0a90eSBrooks Davis struct g_eli_metadata md;
1929e4b0a90eSBrooks Davis const char *name;
1930ca64a75eSAlan Somers unsigned int eli_version;
1931e4b0a90eSBrooks Davis int error, i, nargs;
1932e4b0a90eSBrooks Davis
1933e4b0a90eSBrooks Davis nargs = gctl_get_int(req, "nargs");
1934e4b0a90eSBrooks Davis
1935e4b0a90eSBrooks Davis if (nargs == 0) {
1936e4b0a90eSBrooks Davis unsigned int kernver;
1937e4b0a90eSBrooks Davis ssize_t size;
1938e4b0a90eSBrooks Davis
1939e4b0a90eSBrooks Davis size = sizeof(kernver);
1940e4b0a90eSBrooks Davis if (sysctlbyname("kern.geom.eli.version", &kernver, &size,
1941e4b0a90eSBrooks Davis NULL, 0) == -1) {
1942e4b0a90eSBrooks Davis warn("Unable to obtain GELI kernel version");
1943e4b0a90eSBrooks Davis } else {
1944e4b0a90eSBrooks Davis printf("kernel: %u\n", kernver);
1945e4b0a90eSBrooks Davis }
1946e4b0a90eSBrooks Davis printf("userland: %u\n", G_ELI_VERSION);
1947e4b0a90eSBrooks Davis return;
1948e4b0a90eSBrooks Davis }
1949e4b0a90eSBrooks Davis
1950e4b0a90eSBrooks Davis for (i = 0; i < nargs; i++) {
1951e4b0a90eSBrooks Davis name = gctl_get_ascii(req, "arg%d", i);
1952e4b0a90eSBrooks Davis error = g_metadata_read(name, (unsigned char *)&md,
1953e4b0a90eSBrooks Davis sizeof(md), G_ELI_MAGIC);
1954e4b0a90eSBrooks Davis if (error != 0) {
1955e4b0a90eSBrooks Davis warn("%s: Unable to read metadata: %s.", name,
1956e4b0a90eSBrooks Davis strerror(error));
1957e4b0a90eSBrooks Davis gctl_error(req, "Not fully done.");
1958e4b0a90eSBrooks Davis continue;
1959e4b0a90eSBrooks Davis }
1960ca64a75eSAlan Somers eli_version = le32dec(&md.md_version);
1961ca64a75eSAlan Somers printf("%s: %u\n", name, eli_version);
1962e4b0a90eSBrooks Davis }
1963e4b0a90eSBrooks Davis }
1964e4b0a90eSBrooks Davis
1965e4b0a90eSBrooks Davis static void
eli_clear(struct gctl_req * req)1966e4b0a90eSBrooks Davis eli_clear(struct gctl_req *req)
1967e4b0a90eSBrooks Davis {
1968e4b0a90eSBrooks Davis const char *name;
1969e4b0a90eSBrooks Davis int error, i, nargs;
1970e4b0a90eSBrooks Davis
1971e4b0a90eSBrooks Davis nargs = gctl_get_int(req, "nargs");
1972e4b0a90eSBrooks Davis if (nargs < 1) {
1973e4b0a90eSBrooks Davis gctl_error(req, "Too few arguments.");
1974e4b0a90eSBrooks Davis return;
1975e4b0a90eSBrooks Davis }
1976e4b0a90eSBrooks Davis
1977e4b0a90eSBrooks Davis for (i = 0; i < nargs; i++) {
1978e4b0a90eSBrooks Davis name = gctl_get_ascii(req, "arg%d", i);
1979e4b0a90eSBrooks Davis error = g_metadata_clear(name, G_ELI_MAGIC);
1980e4b0a90eSBrooks Davis if (error != 0) {
1981e4b0a90eSBrooks Davis fprintf(stderr, "Cannot clear metadata on %s: %s.\n",
1982e4b0a90eSBrooks Davis name, strerror(error));
1983e4b0a90eSBrooks Davis gctl_error(req, "Not fully done.");
1984e4b0a90eSBrooks Davis continue;
1985e4b0a90eSBrooks Davis }
1986e4b0a90eSBrooks Davis if (verbose)
1987e4b0a90eSBrooks Davis printf("Metadata cleared on %s.\n", name);
1988e4b0a90eSBrooks Davis }
1989e4b0a90eSBrooks Davis }
1990e4b0a90eSBrooks Davis
1991e4b0a90eSBrooks Davis static void
eli_dump(struct gctl_req * req)1992e4b0a90eSBrooks Davis eli_dump(struct gctl_req *req)
1993e4b0a90eSBrooks Davis {
1994e4b0a90eSBrooks Davis struct g_eli_metadata md;
1995e4b0a90eSBrooks Davis const char *name;
1996e4b0a90eSBrooks Davis int i, nargs;
1997e4b0a90eSBrooks Davis
1998e4b0a90eSBrooks Davis nargs = gctl_get_int(req, "nargs");
1999e4b0a90eSBrooks Davis if (nargs < 1) {
2000e4b0a90eSBrooks Davis gctl_error(req, "Too few arguments.");
2001e4b0a90eSBrooks Davis return;
2002e4b0a90eSBrooks Davis }
2003e4b0a90eSBrooks Davis
2004e4b0a90eSBrooks Davis for (i = 0; i < nargs; i++) {
2005e4b0a90eSBrooks Davis name = gctl_get_ascii(req, "arg%d", i);
2006e4b0a90eSBrooks Davis if (eli_metadata_read(NULL, name, &md) == -1) {
2007e4b0a90eSBrooks Davis gctl_error(req, "Not fully done.");
2008e4b0a90eSBrooks Davis continue;
2009e4b0a90eSBrooks Davis }
2010e4b0a90eSBrooks Davis printf("Metadata on %s:\n", name);
2011e4b0a90eSBrooks Davis eli_metadata_dump(&md);
2012e4b0a90eSBrooks Davis printf("\n");
2013e4b0a90eSBrooks Davis }
2014e4b0a90eSBrooks Davis }
2015