1e4b0a90eSBrooks Davis /*- 2e4b0a90eSBrooks Davis * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3e4b0a90eSBrooks Davis * 4e4b0a90eSBrooks Davis * Copyright (c) 2004-2010 Pawel Jakub Dawidek <pjd@FreeBSD.org> 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/cdefs.h> 30e4b0a90eSBrooks Davis __FBSDID("$FreeBSD$"); 31e4b0a90eSBrooks Davis 32e4b0a90eSBrooks Davis #include <sys/param.h> 33e4b0a90eSBrooks Davis #include <sys/mman.h> 34e4b0a90eSBrooks Davis #include <sys/sysctl.h> 35e4b0a90eSBrooks Davis #include <sys/resource.h> 36e4b0a90eSBrooks Davis #include <opencrypto/cryptodev.h> 37e4b0a90eSBrooks Davis 38e4b0a90eSBrooks Davis #include <assert.h> 39e4b0a90eSBrooks Davis #include <err.h> 40e4b0a90eSBrooks Davis #include <errno.h> 41e4b0a90eSBrooks Davis #include <fcntl.h> 42e4b0a90eSBrooks Davis #include <libgeom.h> 43e4b0a90eSBrooks Davis #include <paths.h> 44e4b0a90eSBrooks Davis #include <readpassphrase.h> 45e4b0a90eSBrooks Davis #include <stdbool.h> 46e4b0a90eSBrooks Davis #include <stdint.h> 47e4b0a90eSBrooks Davis #include <stdio.h> 48e4b0a90eSBrooks Davis #include <stdlib.h> 49e4b0a90eSBrooks Davis #include <string.h> 50e4b0a90eSBrooks Davis #include <strings.h> 51e4b0a90eSBrooks Davis #include <unistd.h> 52e4b0a90eSBrooks Davis 53e4b0a90eSBrooks Davis #include <geom/eli/g_eli.h> 54e4b0a90eSBrooks Davis #include <geom/eli/pkcs5v2.h> 55e4b0a90eSBrooks Davis 56e4b0a90eSBrooks Davis #include "core/geom.h" 57e4b0a90eSBrooks Davis #include "misc/subr.h" 58e4b0a90eSBrooks Davis 59e4b0a90eSBrooks Davis 60e4b0a90eSBrooks Davis uint32_t lib_version = G_LIB_VERSION; 61e4b0a90eSBrooks Davis uint32_t version = G_ELI_VERSION; 62e4b0a90eSBrooks Davis 63e4b0a90eSBrooks Davis #define GELI_BACKUP_DIR "/var/backups/" 64e4b0a90eSBrooks Davis #define GELI_ENC_ALGO "aes" 65217df2daSBen Woods #define BUFSIZE 1024 66217df2daSBen Woods 67217df2daSBen Woods /* 68217df2daSBen Woods * Passphrase cached when attaching multiple providers, in order to be more 69217df2daSBen Woods * user-friendly if they are using the same passphrase. 70217df2daSBen Woods */ 71217df2daSBen Woods static char cached_passphrase[BUFSIZE] = ""; 72e4b0a90eSBrooks Davis 73e4b0a90eSBrooks Davis static void eli_main(struct gctl_req *req, unsigned flags); 74e4b0a90eSBrooks Davis static void eli_init(struct gctl_req *req); 75e4b0a90eSBrooks Davis static void eli_attach(struct gctl_req *req); 76e4b0a90eSBrooks Davis static void eli_configure(struct gctl_req *req); 77e4b0a90eSBrooks Davis static void eli_setkey(struct gctl_req *req); 78e4b0a90eSBrooks Davis static void eli_delkey(struct gctl_req *req); 79e4b0a90eSBrooks Davis static void eli_resume(struct gctl_req *req); 80e4b0a90eSBrooks Davis static void eli_kill(struct gctl_req *req); 81e4b0a90eSBrooks Davis static void eli_backup(struct gctl_req *req); 82e4b0a90eSBrooks Davis static void eli_restore(struct gctl_req *req); 83e4b0a90eSBrooks Davis static void eli_resize(struct gctl_req *req); 84e4b0a90eSBrooks Davis static void eli_version(struct gctl_req *req); 85e4b0a90eSBrooks Davis static void eli_clear(struct gctl_req *req); 86e4b0a90eSBrooks Davis static void eli_dump(struct gctl_req *req); 87e4b0a90eSBrooks Davis 88e4b0a90eSBrooks Davis static int eli_backup_create(struct gctl_req *req, const char *prov, 89e4b0a90eSBrooks Davis const char *file); 90e4b0a90eSBrooks Davis 91e4b0a90eSBrooks Davis /* 92e4b0a90eSBrooks Davis * Available commands: 93e4b0a90eSBrooks Davis * 94368455ecSBen Woods * init [-bdgPTv] [-a aalgo] [-B backupfile] [-e ealgo] [-i iterations] [-l keylen] [-J newpassfile] [-K newkeyfile] [-s sectorsize] [-V version] prov ... 95e4b0a90eSBrooks Davis * label - alias for 'init' 96217df2daSBen Woods * attach [-Cdprv] [-n keyno] [-j passfile] [-k keyfile] prov ... 97e4b0a90eSBrooks Davis * detach [-fl] prov ... 98e4b0a90eSBrooks Davis * stop - alias for 'detach' 99e4b0a90eSBrooks Davis * onetime [-d] [-a aalgo] [-e ealgo] [-l keylen] prov 100e4b0a90eSBrooks Davis * configure [-bBgGtT] prov ... 101e4b0a90eSBrooks Davis * setkey [-pPv] [-n keyno] [-j passfile] [-J newpassfile] [-k keyfile] [-K newkeyfile] prov 102e4b0a90eSBrooks Davis * delkey [-afv] [-n keyno] prov 103e4b0a90eSBrooks Davis * suspend [-v] -a | prov ... 104e4b0a90eSBrooks Davis * resume [-pv] [-j passfile] [-k keyfile] prov 105e4b0a90eSBrooks Davis * kill [-av] [prov ...] 106e4b0a90eSBrooks Davis * backup [-v] prov file 107e4b0a90eSBrooks Davis * restore [-fv] file prov 108e4b0a90eSBrooks Davis * resize [-v] -s oldsize prov 109e4b0a90eSBrooks Davis * version [prov ...] 110e4b0a90eSBrooks Davis * clear [-v] prov ... 111e4b0a90eSBrooks Davis * dump [-v] prov ... 112e4b0a90eSBrooks Davis */ 113e4b0a90eSBrooks Davis struct g_command class_commands[] = { 114e4b0a90eSBrooks Davis { "init", G_FLAG_VERBOSE, eli_main, 115e4b0a90eSBrooks Davis { 116e4b0a90eSBrooks Davis { 'a', "aalgo", "", G_TYPE_STRING }, 117e4b0a90eSBrooks Davis { 'b', "boot", NULL, G_TYPE_BOOL }, 118e4b0a90eSBrooks Davis { 'B', "backupfile", "", G_TYPE_STRING }, 119e4b0a90eSBrooks Davis { 'd', "displaypass", NULL, G_TYPE_BOOL }, 120e4b0a90eSBrooks Davis { 'e', "ealgo", "", G_TYPE_STRING }, 121e4b0a90eSBrooks Davis { 'g', "geliboot", NULL, G_TYPE_BOOL }, 122e4b0a90eSBrooks Davis { 'i', "iterations", "-1", G_TYPE_NUMBER }, 123e4b0a90eSBrooks Davis { 'J', "newpassfile", G_VAL_OPTIONAL, G_TYPE_STRING | G_TYPE_MULTI }, 124e4b0a90eSBrooks Davis { 'K', "newkeyfile", G_VAL_OPTIONAL, G_TYPE_STRING | G_TYPE_MULTI }, 125e4b0a90eSBrooks Davis { 'l', "keylen", "0", G_TYPE_NUMBER }, 126e4b0a90eSBrooks Davis { 'P', "nonewpassphrase", NULL, G_TYPE_BOOL }, 127e4b0a90eSBrooks Davis { 's', "sectorsize", "0", G_TYPE_NUMBER }, 128e4b0a90eSBrooks Davis { 'T', "notrim", NULL, G_TYPE_BOOL }, 129e4b0a90eSBrooks Davis { 'V', "mdversion", "-1", G_TYPE_NUMBER }, 130e4b0a90eSBrooks Davis G_OPT_SENTINEL 131e4b0a90eSBrooks Davis }, 132368455ecSBen Woods "[-bdgPTv] [-a aalgo] [-B backupfile] [-e ealgo] [-i iterations] [-l keylen] [-J newpassfile] [-K newkeyfile] [-s sectorsize] [-V version] prov ..." 133e4b0a90eSBrooks Davis }, 134e4b0a90eSBrooks Davis { "label", G_FLAG_VERBOSE, eli_main, 135e4b0a90eSBrooks Davis { 136e4b0a90eSBrooks Davis { 'a', "aalgo", "", G_TYPE_STRING }, 137e4b0a90eSBrooks Davis { 'b', "boot", NULL, G_TYPE_BOOL }, 138e4b0a90eSBrooks Davis { 'B', "backupfile", "", G_TYPE_STRING }, 139e4b0a90eSBrooks Davis { 'd', "displaypass", NULL, G_TYPE_BOOL }, 140e4b0a90eSBrooks Davis { 'e', "ealgo", "", G_TYPE_STRING }, 141e4b0a90eSBrooks Davis { 'g', "geliboot", NULL, G_TYPE_BOOL }, 142e4b0a90eSBrooks Davis { 'i', "iterations", "-1", G_TYPE_NUMBER }, 143e4b0a90eSBrooks Davis { 'J', "newpassfile", G_VAL_OPTIONAL, G_TYPE_STRING | G_TYPE_MULTI }, 144e4b0a90eSBrooks Davis { 'K', "newkeyfile", G_VAL_OPTIONAL, G_TYPE_STRING | G_TYPE_MULTI }, 145e4b0a90eSBrooks Davis { 'l', "keylen", "0", G_TYPE_NUMBER }, 146e4b0a90eSBrooks Davis { 'P', "nonewpassphrase", NULL, G_TYPE_BOOL }, 147e4b0a90eSBrooks Davis { 's', "sectorsize", "0", G_TYPE_NUMBER }, 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 }, 188e4b0a90eSBrooks Davis { 's', "sectorsize", "0", G_TYPE_NUMBER }, 189e4b0a90eSBrooks Davis { 'T', "notrim", NULL, G_TYPE_BOOL }, 190e4b0a90eSBrooks Davis G_OPT_SENTINEL 191e4b0a90eSBrooks Davis }, 192e4b0a90eSBrooks Davis "[-dT] [-a aalgo] [-e ealgo] [-l keylen] [-s sectorsize] prov" 193e4b0a90eSBrooks Davis }, 194e4b0a90eSBrooks Davis { "configure", G_FLAG_VERBOSE, eli_main, 195e4b0a90eSBrooks Davis { 196e4b0a90eSBrooks Davis { 'b', "boot", NULL, G_TYPE_BOOL }, 197e4b0a90eSBrooks Davis { 'B', "noboot", NULL, G_TYPE_BOOL }, 198e4b0a90eSBrooks Davis { 'd', "displaypass", NULL, G_TYPE_BOOL }, 199e4b0a90eSBrooks Davis { 'D', "nodisplaypass", NULL, G_TYPE_BOOL }, 200e4b0a90eSBrooks Davis { 'g', "geliboot", NULL, G_TYPE_BOOL }, 201e4b0a90eSBrooks Davis { 'G', "nogeliboot", NULL, G_TYPE_BOOL }, 202e4b0a90eSBrooks Davis { 't', "trim", NULL, G_TYPE_BOOL }, 203e4b0a90eSBrooks Davis { 'T', "notrim", NULL, G_TYPE_BOOL }, 204e4b0a90eSBrooks Davis G_OPT_SENTINEL 205e4b0a90eSBrooks Davis }, 206e4b0a90eSBrooks Davis "[-bBdDgGtT] prov ..." 207e4b0a90eSBrooks Davis }, 208e4b0a90eSBrooks Davis { "setkey", G_FLAG_VERBOSE, eli_main, 209e4b0a90eSBrooks Davis { 210e4b0a90eSBrooks Davis { 'i', "iterations", "-1", G_TYPE_NUMBER }, 211e4b0a90eSBrooks Davis { 'j', "passfile", G_VAL_OPTIONAL, G_TYPE_STRING | G_TYPE_MULTI }, 212e4b0a90eSBrooks Davis { 'J', "newpassfile", G_VAL_OPTIONAL, G_TYPE_STRING | G_TYPE_MULTI }, 213e4b0a90eSBrooks Davis { 'k', "keyfile", G_VAL_OPTIONAL, G_TYPE_STRING | G_TYPE_MULTI }, 214e4b0a90eSBrooks Davis { 'K', "newkeyfile", G_VAL_OPTIONAL, G_TYPE_STRING | G_TYPE_MULTI }, 215e4b0a90eSBrooks Davis { 'n', "keyno", "-1", G_TYPE_NUMBER }, 216e4b0a90eSBrooks Davis { 'p', "nopassphrase", NULL, G_TYPE_BOOL }, 217e4b0a90eSBrooks Davis { 'P', "nonewpassphrase", NULL, G_TYPE_BOOL }, 218e4b0a90eSBrooks Davis G_OPT_SENTINEL 219e4b0a90eSBrooks Davis }, 220e4b0a90eSBrooks Davis "[-pPv] [-n keyno] [-i iterations] [-j passfile] [-J newpassfile] [-k keyfile] [-K newkeyfile] prov" 221e4b0a90eSBrooks Davis }, 222e4b0a90eSBrooks Davis { "delkey", G_FLAG_VERBOSE, eli_main, 223e4b0a90eSBrooks Davis { 224e4b0a90eSBrooks Davis { 'a', "all", NULL, G_TYPE_BOOL }, 225e4b0a90eSBrooks Davis { 'f', "force", NULL, G_TYPE_BOOL }, 226e4b0a90eSBrooks Davis { 'n', "keyno", "-1", G_TYPE_NUMBER }, 227e4b0a90eSBrooks Davis G_OPT_SENTINEL 228e4b0a90eSBrooks Davis }, 229e4b0a90eSBrooks Davis "[-afv] [-n keyno] prov" 230e4b0a90eSBrooks Davis }, 231e4b0a90eSBrooks Davis { "suspend", G_FLAG_VERBOSE, NULL, 232e4b0a90eSBrooks Davis { 233e4b0a90eSBrooks Davis { 'a', "all", NULL, G_TYPE_BOOL }, 234e4b0a90eSBrooks Davis G_OPT_SENTINEL 235e4b0a90eSBrooks Davis }, 236e4b0a90eSBrooks Davis "[-v] -a | prov ..." 237e4b0a90eSBrooks Davis }, 238e4b0a90eSBrooks Davis { "resume", G_FLAG_VERBOSE, eli_main, 239e4b0a90eSBrooks Davis { 240e4b0a90eSBrooks Davis { 'j', "passfile", G_VAL_OPTIONAL, G_TYPE_STRING | G_TYPE_MULTI }, 241e4b0a90eSBrooks Davis { 'k', "keyfile", G_VAL_OPTIONAL, G_TYPE_STRING | G_TYPE_MULTI }, 242e4b0a90eSBrooks Davis { 'p', "nopassphrase", NULL, G_TYPE_BOOL }, 243e4b0a90eSBrooks Davis G_OPT_SENTINEL 244e4b0a90eSBrooks Davis }, 245e4b0a90eSBrooks Davis "[-pv] [-j passfile] [-k keyfile] prov" 246e4b0a90eSBrooks Davis }, 247e4b0a90eSBrooks Davis { "kill", G_FLAG_VERBOSE, eli_main, 248e4b0a90eSBrooks Davis { 249e4b0a90eSBrooks Davis { 'a', "all", NULL, G_TYPE_BOOL }, 250e4b0a90eSBrooks Davis G_OPT_SENTINEL 251e4b0a90eSBrooks Davis }, 252e4b0a90eSBrooks Davis "[-av] [prov ...]" 253e4b0a90eSBrooks Davis }, 254e4b0a90eSBrooks Davis { "backup", G_FLAG_VERBOSE, eli_main, G_NULL_OPTS, 255e4b0a90eSBrooks Davis "[-v] prov file" 256e4b0a90eSBrooks Davis }, 257e4b0a90eSBrooks Davis { "restore", G_FLAG_VERBOSE, eli_main, 258e4b0a90eSBrooks Davis { 259e4b0a90eSBrooks Davis { 'f', "force", NULL, G_TYPE_BOOL }, 260e4b0a90eSBrooks Davis G_OPT_SENTINEL 261e4b0a90eSBrooks Davis }, 262e4b0a90eSBrooks Davis "[-fv] file prov" 263e4b0a90eSBrooks Davis }, 264e4b0a90eSBrooks Davis { "resize", G_FLAG_VERBOSE, eli_main, 265e4b0a90eSBrooks Davis { 266e4b0a90eSBrooks Davis { 's', "oldsize", NULL, G_TYPE_NUMBER }, 267e4b0a90eSBrooks Davis G_OPT_SENTINEL 268e4b0a90eSBrooks Davis }, 269e4b0a90eSBrooks Davis "[-v] -s oldsize prov" 270e4b0a90eSBrooks Davis }, 271e4b0a90eSBrooks Davis { "version", G_FLAG_LOADKLD, eli_main, G_NULL_OPTS, 272e4b0a90eSBrooks Davis "[prov ...]" 273e4b0a90eSBrooks Davis }, 274e4b0a90eSBrooks Davis { "clear", G_FLAG_VERBOSE, eli_main, G_NULL_OPTS, 275e4b0a90eSBrooks Davis "[-v] prov ..." 276e4b0a90eSBrooks Davis }, 277e4b0a90eSBrooks Davis { "dump", G_FLAG_VERBOSE, eli_main, G_NULL_OPTS, 278e4b0a90eSBrooks Davis "[-v] prov ..." 279e4b0a90eSBrooks Davis }, 280e4b0a90eSBrooks Davis G_CMD_SENTINEL 281e4b0a90eSBrooks Davis }; 282e4b0a90eSBrooks Davis 283e4b0a90eSBrooks Davis static int verbose = 0; 284e4b0a90eSBrooks Davis 285e4b0a90eSBrooks Davis static int 286e4b0a90eSBrooks Davis eli_protect(struct gctl_req *req) 287e4b0a90eSBrooks Davis { 288e4b0a90eSBrooks Davis struct rlimit rl; 289e4b0a90eSBrooks Davis 290e4b0a90eSBrooks Davis /* Disable core dumps. */ 291e4b0a90eSBrooks Davis rl.rlim_cur = 0; 292e4b0a90eSBrooks Davis rl.rlim_max = 0; 293e4b0a90eSBrooks Davis if (setrlimit(RLIMIT_CORE, &rl) == -1) { 294e4b0a90eSBrooks Davis gctl_error(req, "Cannot disable core dumps: %s.", 295e4b0a90eSBrooks Davis strerror(errno)); 296e4b0a90eSBrooks Davis return (-1); 297e4b0a90eSBrooks Davis } 298e4b0a90eSBrooks Davis /* Disable swapping. */ 299e4b0a90eSBrooks Davis if (mlockall(MCL_FUTURE) == -1) { 300e4b0a90eSBrooks Davis gctl_error(req, "Cannot lock memory: %s.", strerror(errno)); 301e4b0a90eSBrooks Davis return (-1); 302e4b0a90eSBrooks Davis } 303e4b0a90eSBrooks Davis return (0); 304e4b0a90eSBrooks Davis } 305e4b0a90eSBrooks Davis 306e4b0a90eSBrooks Davis static void 307e4b0a90eSBrooks Davis eli_main(struct gctl_req *req, unsigned int flags) 308e4b0a90eSBrooks Davis { 309e4b0a90eSBrooks Davis const char *name; 310e4b0a90eSBrooks Davis 311e4b0a90eSBrooks Davis if (eli_protect(req) == -1) 312e4b0a90eSBrooks Davis return; 313e4b0a90eSBrooks Davis 314e4b0a90eSBrooks Davis if ((flags & G_FLAG_VERBOSE) != 0) 315e4b0a90eSBrooks Davis verbose = 1; 316e4b0a90eSBrooks Davis 317e4b0a90eSBrooks Davis name = gctl_get_ascii(req, "verb"); 318e4b0a90eSBrooks Davis if (name == NULL) { 319e4b0a90eSBrooks Davis gctl_error(req, "No '%s' argument.", "verb"); 320e4b0a90eSBrooks Davis return; 321e4b0a90eSBrooks Davis } 322e4b0a90eSBrooks Davis if (strcmp(name, "init") == 0 || strcmp(name, "label") == 0) 323e4b0a90eSBrooks Davis eli_init(req); 324e4b0a90eSBrooks Davis else if (strcmp(name, "attach") == 0) 325e4b0a90eSBrooks Davis eli_attach(req); 326e4b0a90eSBrooks Davis else if (strcmp(name, "configure") == 0) 327e4b0a90eSBrooks Davis eli_configure(req); 328e4b0a90eSBrooks Davis else if (strcmp(name, "setkey") == 0) 329e4b0a90eSBrooks Davis eli_setkey(req); 330e4b0a90eSBrooks Davis else if (strcmp(name, "delkey") == 0) 331e4b0a90eSBrooks Davis eli_delkey(req); 332e4b0a90eSBrooks Davis else if (strcmp(name, "resume") == 0) 333e4b0a90eSBrooks Davis eli_resume(req); 334e4b0a90eSBrooks Davis else if (strcmp(name, "kill") == 0) 335e4b0a90eSBrooks Davis eli_kill(req); 336e4b0a90eSBrooks Davis else if (strcmp(name, "backup") == 0) 337e4b0a90eSBrooks Davis eli_backup(req); 338e4b0a90eSBrooks Davis else if (strcmp(name, "restore") == 0) 339e4b0a90eSBrooks Davis eli_restore(req); 340e4b0a90eSBrooks Davis else if (strcmp(name, "resize") == 0) 341e4b0a90eSBrooks Davis eli_resize(req); 342e4b0a90eSBrooks Davis else if (strcmp(name, "version") == 0) 343e4b0a90eSBrooks Davis eli_version(req); 344e4b0a90eSBrooks Davis else if (strcmp(name, "dump") == 0) 345e4b0a90eSBrooks Davis eli_dump(req); 346e4b0a90eSBrooks Davis else if (strcmp(name, "clear") == 0) 347e4b0a90eSBrooks Davis eli_clear(req); 348e4b0a90eSBrooks Davis else 349e4b0a90eSBrooks Davis gctl_error(req, "Unknown command: %s.", name); 350e4b0a90eSBrooks Davis } 351e4b0a90eSBrooks Davis 352e4b0a90eSBrooks Davis static bool 353e4b0a90eSBrooks Davis eli_is_attached(const char *prov) 354e4b0a90eSBrooks Davis { 355e4b0a90eSBrooks Davis char name[MAXPATHLEN]; 356e4b0a90eSBrooks Davis 357e4b0a90eSBrooks Davis /* 358e4b0a90eSBrooks Davis * Not the best way to do it, but the easiest. 359e4b0a90eSBrooks Davis * We try to open provider and check if it is a GEOM provider 360e4b0a90eSBrooks Davis * by asking about its sectorsize. 361e4b0a90eSBrooks Davis */ 362e4b0a90eSBrooks Davis snprintf(name, sizeof(name), "%s%s", prov, G_ELI_SUFFIX); 363e4b0a90eSBrooks Davis return (g_get_sectorsize(name) > 0); 364e4b0a90eSBrooks Davis } 365e4b0a90eSBrooks Davis 366e4b0a90eSBrooks Davis static int 367e4b0a90eSBrooks Davis eli_genkey_files(struct gctl_req *req, bool new, const char *type, 368e4b0a90eSBrooks Davis struct hmac_ctx *ctxp, char *passbuf, size_t passbufsize) 369e4b0a90eSBrooks Davis { 370e4b0a90eSBrooks Davis char *p, buf[BUFSIZE], argname[16]; 371e4b0a90eSBrooks Davis const char *file; 372e4b0a90eSBrooks Davis int error, fd, i; 373e4b0a90eSBrooks Davis ssize_t done; 374e4b0a90eSBrooks Davis 375e4b0a90eSBrooks Davis assert((strcmp(type, "keyfile") == 0 && ctxp != NULL && 376e4b0a90eSBrooks Davis passbuf == NULL && passbufsize == 0) || 377e4b0a90eSBrooks Davis (strcmp(type, "passfile") == 0 && ctxp == NULL && 378e4b0a90eSBrooks Davis passbuf != NULL && passbufsize > 0)); 379e4b0a90eSBrooks Davis assert(strcmp(type, "keyfile") == 0 || passbuf[0] == '\0'); 380e4b0a90eSBrooks Davis 381e4b0a90eSBrooks Davis for (i = 0; ; i++) { 382e4b0a90eSBrooks Davis snprintf(argname, sizeof(argname), "%s%s%d", 383e4b0a90eSBrooks Davis new ? "new" : "", type, i); 384e4b0a90eSBrooks Davis 385e4b0a90eSBrooks Davis /* No more {key,pass}files? */ 386e4b0a90eSBrooks Davis if (!gctl_has_param(req, argname)) 387e4b0a90eSBrooks Davis return (i); 388e4b0a90eSBrooks Davis 389e4b0a90eSBrooks Davis file = gctl_get_ascii(req, "%s", argname); 390e4b0a90eSBrooks Davis assert(file != NULL); 391e4b0a90eSBrooks Davis 392e4b0a90eSBrooks Davis if (strcmp(file, "-") == 0) 393e4b0a90eSBrooks Davis fd = STDIN_FILENO; 394e4b0a90eSBrooks Davis else { 395e4b0a90eSBrooks Davis fd = open(file, O_RDONLY); 396e4b0a90eSBrooks Davis if (fd == -1) { 397e4b0a90eSBrooks Davis gctl_error(req, "Cannot open %s %s: %s.", 398e4b0a90eSBrooks Davis type, file, strerror(errno)); 399e4b0a90eSBrooks Davis return (-1); 400e4b0a90eSBrooks Davis } 401e4b0a90eSBrooks Davis } 402e4b0a90eSBrooks Davis if (strcmp(type, "keyfile") == 0) { 403e4b0a90eSBrooks Davis while ((done = read(fd, buf, sizeof(buf))) > 0) 404e4b0a90eSBrooks Davis g_eli_crypto_hmac_update(ctxp, buf, done); 405e4b0a90eSBrooks Davis } else /* if (strcmp(type, "passfile") == 0) */ { 406e4b0a90eSBrooks Davis assert(strcmp(type, "passfile") == 0); 407e4b0a90eSBrooks Davis 408e4b0a90eSBrooks Davis while ((done = read(fd, buf, sizeof(buf) - 1)) > 0) { 409e4b0a90eSBrooks Davis buf[done] = '\0'; 410e4b0a90eSBrooks Davis p = strchr(buf, '\n'); 411e4b0a90eSBrooks Davis if (p != NULL) { 412e4b0a90eSBrooks Davis *p = '\0'; 413e4b0a90eSBrooks Davis done = p - buf; 414e4b0a90eSBrooks Davis } 415e4b0a90eSBrooks Davis if (strlcat(passbuf, buf, passbufsize) >= 416e4b0a90eSBrooks Davis passbufsize) { 417e4b0a90eSBrooks Davis gctl_error(req, 418e4b0a90eSBrooks Davis "Passphrase in %s too long.", file); 419217df2daSBen Woods explicit_bzero(buf, sizeof(buf)); 420e4b0a90eSBrooks Davis return (-1); 421e4b0a90eSBrooks Davis } 422e4b0a90eSBrooks Davis if (p != NULL) 423e4b0a90eSBrooks Davis break; 424e4b0a90eSBrooks Davis } 425e4b0a90eSBrooks Davis } 426e4b0a90eSBrooks Davis error = errno; 427e4b0a90eSBrooks Davis if (strcmp(file, "-") != 0) 428e4b0a90eSBrooks Davis close(fd); 429217df2daSBen Woods explicit_bzero(buf, sizeof(buf)); 430e4b0a90eSBrooks Davis if (done == -1) { 431e4b0a90eSBrooks Davis gctl_error(req, "Cannot read %s %s: %s.", 432e4b0a90eSBrooks Davis type, file, strerror(error)); 433e4b0a90eSBrooks Davis return (-1); 434e4b0a90eSBrooks Davis } 435e4b0a90eSBrooks Davis } 436e4b0a90eSBrooks Davis /* NOTREACHED */ 437e4b0a90eSBrooks Davis } 438e4b0a90eSBrooks Davis 439e4b0a90eSBrooks Davis static int 440e4b0a90eSBrooks Davis eli_genkey_passphrase_prompt(struct gctl_req *req, bool new, char *passbuf, 441e4b0a90eSBrooks Davis size_t passbufsize) 442e4b0a90eSBrooks Davis { 443e4b0a90eSBrooks Davis char *p; 444e4b0a90eSBrooks Davis 445e4b0a90eSBrooks Davis for (;;) { 446e4b0a90eSBrooks Davis p = readpassphrase( 447e4b0a90eSBrooks Davis new ? "Enter new passphrase: " : "Enter passphrase: ", 448e4b0a90eSBrooks Davis passbuf, passbufsize, RPP_ECHO_OFF | RPP_REQUIRE_TTY); 449e4b0a90eSBrooks Davis if (p == NULL) { 450217df2daSBen Woods explicit_bzero(passbuf, passbufsize); 451e4b0a90eSBrooks Davis gctl_error(req, "Cannot read passphrase: %s.", 452e4b0a90eSBrooks Davis strerror(errno)); 453e4b0a90eSBrooks Davis return (-1); 454e4b0a90eSBrooks Davis } 455e4b0a90eSBrooks Davis 456e4b0a90eSBrooks Davis if (new) { 457e4b0a90eSBrooks Davis char tmpbuf[BUFSIZE]; 458e4b0a90eSBrooks Davis 459e4b0a90eSBrooks Davis p = readpassphrase("Reenter new passphrase: ", 460e4b0a90eSBrooks Davis tmpbuf, sizeof(tmpbuf), 461e4b0a90eSBrooks Davis RPP_ECHO_OFF | RPP_REQUIRE_TTY); 462e4b0a90eSBrooks Davis if (p == NULL) { 463217df2daSBen Woods explicit_bzero(passbuf, passbufsize); 464e4b0a90eSBrooks Davis gctl_error(req, 465e4b0a90eSBrooks Davis "Cannot read passphrase: %s.", 466e4b0a90eSBrooks Davis strerror(errno)); 467e4b0a90eSBrooks Davis return (-1); 468e4b0a90eSBrooks Davis } 469e4b0a90eSBrooks Davis 470e4b0a90eSBrooks Davis if (strcmp(passbuf, tmpbuf) != 0) { 471217df2daSBen Woods explicit_bzero(passbuf, passbufsize); 472e4b0a90eSBrooks Davis fprintf(stderr, "They didn't match.\n"); 473e4b0a90eSBrooks Davis continue; 474e4b0a90eSBrooks Davis } 475217df2daSBen Woods explicit_bzero(tmpbuf, sizeof(tmpbuf)); 476e4b0a90eSBrooks Davis } 477e4b0a90eSBrooks Davis return (0); 478e4b0a90eSBrooks Davis } 479e4b0a90eSBrooks Davis /* NOTREACHED */ 480e4b0a90eSBrooks Davis } 481e4b0a90eSBrooks Davis 482e4b0a90eSBrooks Davis static int 483e4b0a90eSBrooks Davis eli_genkey_passphrase(struct gctl_req *req, struct g_eli_metadata *md, bool new, 484e4b0a90eSBrooks Davis struct hmac_ctx *ctxp) 485e4b0a90eSBrooks Davis { 486e4b0a90eSBrooks Davis char passbuf[BUFSIZE]; 487e4b0a90eSBrooks Davis bool nopassphrase; 488e4b0a90eSBrooks Davis int nfiles; 489e4b0a90eSBrooks Davis 490217df2daSBen Woods /* 491217df2daSBen Woods * Return error if the 'do not use passphrase' flag was given but a 492217df2daSBen Woods * passfile was provided. 493217df2daSBen Woods */ 494e4b0a90eSBrooks Davis nopassphrase = 495e4b0a90eSBrooks Davis gctl_get_int(req, new ? "nonewpassphrase" : "nopassphrase"); 496e4b0a90eSBrooks Davis if (nopassphrase) { 497e4b0a90eSBrooks Davis if (gctl_has_param(req, new ? "newpassfile0" : "passfile0")) { 498e4b0a90eSBrooks Davis gctl_error(req, 499e4b0a90eSBrooks Davis "Options -%c and -%c are mutually exclusive.", 500e4b0a90eSBrooks Davis new ? 'J' : 'j', new ? 'P' : 'p'); 501e4b0a90eSBrooks Davis return (-1); 502e4b0a90eSBrooks Davis } 503e4b0a90eSBrooks Davis return (0); 504e4b0a90eSBrooks Davis } 505e4b0a90eSBrooks Davis 506217df2daSBen Woods /* 507217df2daSBen Woods * Return error if using a provider which does not require a passphrase 508217df2daSBen Woods * but the 'do not use passphrase' flag was not given. 509217df2daSBen Woods */ 510e4b0a90eSBrooks Davis if (!new && md->md_iterations == -1) { 511e4b0a90eSBrooks Davis gctl_error(req, "Missing -p flag."); 512e4b0a90eSBrooks Davis return (-1); 513e4b0a90eSBrooks Davis } 514e4b0a90eSBrooks Davis passbuf[0] = '\0'; 515217df2daSBen Woods 516217df2daSBen Woods /* Use cached passphrase if defined. */ 517217df2daSBen Woods if (strlen(cached_passphrase) > 0) { 518217df2daSBen Woods strlcpy(passbuf, cached_passphrase, sizeof(passbuf)); 519217df2daSBen Woods } else { 520e4b0a90eSBrooks Davis nfiles = eli_genkey_files(req, new, "passfile", NULL, passbuf, 521e4b0a90eSBrooks Davis sizeof(passbuf)); 522217df2daSBen Woods if (nfiles == -1) { 523e4b0a90eSBrooks Davis return (-1); 524217df2daSBen Woods } else if (nfiles == 0) { 525e4b0a90eSBrooks Davis if (eli_genkey_passphrase_prompt(req, new, passbuf, 526e4b0a90eSBrooks Davis sizeof(passbuf)) == -1) { 527e4b0a90eSBrooks Davis return (-1); 528e4b0a90eSBrooks Davis } 529e4b0a90eSBrooks Davis } 530217df2daSBen Woods /* Cache the passphrase for other providers. */ 531217df2daSBen Woods strlcpy(cached_passphrase, passbuf, sizeof(cached_passphrase)); 532217df2daSBen Woods } 533e4b0a90eSBrooks Davis /* 534e4b0a90eSBrooks Davis * Field md_iterations equal to -1 means "choose some sane 535e4b0a90eSBrooks Davis * value for me". 536e4b0a90eSBrooks Davis */ 537e4b0a90eSBrooks Davis if (md->md_iterations == -1) { 538e4b0a90eSBrooks Davis assert(new); 539e4b0a90eSBrooks Davis if (verbose) 540e4b0a90eSBrooks Davis printf("Calculating number of iterations...\n"); 541e4b0a90eSBrooks Davis md->md_iterations = pkcs5v2_calculate(2000000); 542e4b0a90eSBrooks Davis assert(md->md_iterations > 0); 543e4b0a90eSBrooks Davis if (verbose) { 544e4b0a90eSBrooks Davis printf("Done, using %d iterations.\n", 545e4b0a90eSBrooks Davis md->md_iterations); 546e4b0a90eSBrooks Davis } 547e4b0a90eSBrooks Davis } 548e4b0a90eSBrooks Davis /* 549e4b0a90eSBrooks Davis * If md_iterations is equal to 0, user doesn't want PKCS#5v2. 550e4b0a90eSBrooks Davis */ 551e4b0a90eSBrooks Davis if (md->md_iterations == 0) { 552e4b0a90eSBrooks Davis g_eli_crypto_hmac_update(ctxp, md->md_salt, 553e4b0a90eSBrooks Davis sizeof(md->md_salt)); 554e4b0a90eSBrooks Davis g_eli_crypto_hmac_update(ctxp, passbuf, strlen(passbuf)); 555e4b0a90eSBrooks Davis } else /* if (md->md_iterations > 0) */ { 556e4b0a90eSBrooks Davis unsigned char dkey[G_ELI_USERKEYLEN]; 557e4b0a90eSBrooks Davis 558e4b0a90eSBrooks Davis pkcs5v2_genkey(dkey, sizeof(dkey), md->md_salt, 559e4b0a90eSBrooks Davis sizeof(md->md_salt), passbuf, md->md_iterations); 560e4b0a90eSBrooks Davis g_eli_crypto_hmac_update(ctxp, dkey, sizeof(dkey)); 561217df2daSBen Woods explicit_bzero(dkey, sizeof(dkey)); 562e4b0a90eSBrooks Davis } 563217df2daSBen Woods explicit_bzero(passbuf, sizeof(passbuf)); 564e4b0a90eSBrooks Davis 565e4b0a90eSBrooks Davis return (0); 566e4b0a90eSBrooks Davis } 567e4b0a90eSBrooks Davis 568e4b0a90eSBrooks Davis static unsigned char * 569e4b0a90eSBrooks Davis eli_genkey(struct gctl_req *req, struct g_eli_metadata *md, unsigned char *key, 570e4b0a90eSBrooks Davis bool new) 571e4b0a90eSBrooks Davis { 572e4b0a90eSBrooks Davis struct hmac_ctx ctx; 573e4b0a90eSBrooks Davis bool nopassphrase; 574e4b0a90eSBrooks Davis int nfiles; 575e4b0a90eSBrooks Davis 576e4b0a90eSBrooks Davis nopassphrase = 577e4b0a90eSBrooks Davis gctl_get_int(req, new ? "nonewpassphrase" : "nopassphrase"); 578e4b0a90eSBrooks Davis 579e4b0a90eSBrooks Davis g_eli_crypto_hmac_init(&ctx, NULL, 0); 580e4b0a90eSBrooks Davis 581e4b0a90eSBrooks Davis nfiles = eli_genkey_files(req, new, "keyfile", &ctx, NULL, 0); 582e4b0a90eSBrooks Davis if (nfiles == -1) 583e4b0a90eSBrooks Davis return (NULL); 584e4b0a90eSBrooks Davis else if (nfiles == 0 && nopassphrase) { 585e4b0a90eSBrooks Davis gctl_error(req, "No key components given."); 586e4b0a90eSBrooks Davis return (NULL); 587e4b0a90eSBrooks Davis } 588e4b0a90eSBrooks Davis 589e4b0a90eSBrooks Davis if (eli_genkey_passphrase(req, md, new, &ctx) == -1) 590e4b0a90eSBrooks Davis return (NULL); 591e4b0a90eSBrooks Davis 592e4b0a90eSBrooks Davis g_eli_crypto_hmac_final(&ctx, key, 0); 593e4b0a90eSBrooks Davis 594e4b0a90eSBrooks Davis return (key); 595e4b0a90eSBrooks Davis } 596e4b0a90eSBrooks Davis 597e4b0a90eSBrooks Davis static int 598e4b0a90eSBrooks Davis eli_metadata_read(struct gctl_req *req, const char *prov, 599e4b0a90eSBrooks Davis struct g_eli_metadata *md) 600e4b0a90eSBrooks Davis { 601e4b0a90eSBrooks Davis unsigned char sector[sizeof(struct g_eli_metadata)]; 602e4b0a90eSBrooks Davis int error; 603e4b0a90eSBrooks Davis 604e4b0a90eSBrooks Davis if (g_get_sectorsize(prov) == 0) { 605e4b0a90eSBrooks Davis int fd; 606e4b0a90eSBrooks Davis 607e4b0a90eSBrooks Davis /* This is a file probably. */ 608e4b0a90eSBrooks Davis fd = open(prov, O_RDONLY); 609e4b0a90eSBrooks Davis if (fd == -1) { 610e4b0a90eSBrooks Davis gctl_error(req, "Cannot open %s: %s.", prov, 611e4b0a90eSBrooks Davis strerror(errno)); 612e4b0a90eSBrooks Davis return (-1); 613e4b0a90eSBrooks Davis } 614e4b0a90eSBrooks Davis if (read(fd, sector, sizeof(sector)) != sizeof(sector)) { 615e4b0a90eSBrooks Davis gctl_error(req, "Cannot read metadata from %s: %s.", 616e4b0a90eSBrooks Davis prov, strerror(errno)); 617e4b0a90eSBrooks Davis close(fd); 618e4b0a90eSBrooks Davis return (-1); 619e4b0a90eSBrooks Davis } 620e4b0a90eSBrooks Davis close(fd); 621e4b0a90eSBrooks Davis } else { 622e4b0a90eSBrooks Davis /* This is a GEOM provider. */ 623e4b0a90eSBrooks Davis error = g_metadata_read(prov, sector, sizeof(sector), 624e4b0a90eSBrooks Davis G_ELI_MAGIC); 625e4b0a90eSBrooks Davis if (error != 0) { 626e4b0a90eSBrooks Davis gctl_error(req, "Cannot read metadata from %s: %s.", 627e4b0a90eSBrooks Davis prov, strerror(error)); 628e4b0a90eSBrooks Davis return (-1); 629e4b0a90eSBrooks Davis } 630e4b0a90eSBrooks Davis } 631e4b0a90eSBrooks Davis error = eli_metadata_decode(sector, md); 632e4b0a90eSBrooks Davis switch (error) { 633e4b0a90eSBrooks Davis case 0: 634e4b0a90eSBrooks Davis break; 635e4b0a90eSBrooks Davis case EOPNOTSUPP: 636e4b0a90eSBrooks Davis gctl_error(req, 637e4b0a90eSBrooks Davis "Provider's %s metadata version %u is too new.\n" 638e4b0a90eSBrooks Davis "geli: The highest supported version is %u.", 639e4b0a90eSBrooks Davis prov, (unsigned int)md->md_version, G_ELI_VERSION); 640e4b0a90eSBrooks Davis return (-1); 641e4b0a90eSBrooks Davis case EINVAL: 642e4b0a90eSBrooks Davis gctl_error(req, "Inconsistent provider's %s metadata.", prov); 643e4b0a90eSBrooks Davis return (-1); 644e4b0a90eSBrooks Davis default: 645e4b0a90eSBrooks Davis gctl_error(req, 646e4b0a90eSBrooks Davis "Unexpected error while decoding provider's %s metadata: %s.", 647e4b0a90eSBrooks Davis prov, strerror(error)); 648e4b0a90eSBrooks Davis return (-1); 649e4b0a90eSBrooks Davis } 650e4b0a90eSBrooks Davis return (0); 651e4b0a90eSBrooks Davis } 652e4b0a90eSBrooks Davis 653e4b0a90eSBrooks Davis static int 654e4b0a90eSBrooks Davis eli_metadata_store(struct gctl_req *req, const char *prov, 655e4b0a90eSBrooks Davis struct g_eli_metadata *md) 656e4b0a90eSBrooks Davis { 657e4b0a90eSBrooks Davis unsigned char sector[sizeof(struct g_eli_metadata)]; 658e4b0a90eSBrooks Davis int error; 659e4b0a90eSBrooks Davis 660e4b0a90eSBrooks Davis eli_metadata_encode(md, sector); 661e4b0a90eSBrooks Davis if (g_get_sectorsize(prov) == 0) { 662e4b0a90eSBrooks Davis int fd; 663e4b0a90eSBrooks Davis 664e4b0a90eSBrooks Davis /* This is a file probably. */ 665e4b0a90eSBrooks Davis fd = open(prov, O_WRONLY | O_TRUNC); 666e4b0a90eSBrooks Davis if (fd == -1) { 667e4b0a90eSBrooks Davis gctl_error(req, "Cannot open %s: %s.", prov, 668e4b0a90eSBrooks Davis strerror(errno)); 669217df2daSBen Woods explicit_bzero(sector, sizeof(sector)); 670e4b0a90eSBrooks Davis return (-1); 671e4b0a90eSBrooks Davis } 672e4b0a90eSBrooks Davis if (write(fd, sector, sizeof(sector)) != sizeof(sector)) { 673e4b0a90eSBrooks Davis gctl_error(req, "Cannot write metadata to %s: %s.", 674e4b0a90eSBrooks Davis prov, strerror(errno)); 675217df2daSBen Woods explicit_bzero(sector, sizeof(sector)); 676e4b0a90eSBrooks Davis close(fd); 677e4b0a90eSBrooks Davis return (-1); 678e4b0a90eSBrooks Davis } 679e4b0a90eSBrooks Davis close(fd); 680e4b0a90eSBrooks Davis } else { 681e4b0a90eSBrooks Davis /* This is a GEOM provider. */ 682e4b0a90eSBrooks Davis error = g_metadata_store(prov, sector, sizeof(sector)); 683e4b0a90eSBrooks Davis if (error != 0) { 684e4b0a90eSBrooks Davis gctl_error(req, "Cannot write metadata to %s: %s.", 685e4b0a90eSBrooks Davis prov, strerror(errno)); 686217df2daSBen Woods explicit_bzero(sector, sizeof(sector)); 687e4b0a90eSBrooks Davis return (-1); 688e4b0a90eSBrooks Davis } 689e4b0a90eSBrooks Davis } 690217df2daSBen Woods explicit_bzero(sector, sizeof(sector)); 691e4b0a90eSBrooks Davis return (0); 692e4b0a90eSBrooks Davis } 693e4b0a90eSBrooks Davis 694e4b0a90eSBrooks Davis static void 695e4b0a90eSBrooks Davis eli_init(struct gctl_req *req) 696e4b0a90eSBrooks Davis { 697e4b0a90eSBrooks Davis struct g_eli_metadata md; 698368455ecSBen Woods struct gctl_req *r; 699e4b0a90eSBrooks Davis unsigned char sector[sizeof(struct g_eli_metadata)] __aligned(4); 700e4b0a90eSBrooks Davis unsigned char key[G_ELI_USERKEYLEN]; 701e4b0a90eSBrooks Davis char backfile[MAXPATHLEN]; 702e4b0a90eSBrooks Davis const char *str, *prov; 703e4b0a90eSBrooks Davis unsigned int secsize, version; 704e4b0a90eSBrooks Davis off_t mediasize; 705e4b0a90eSBrooks Davis intmax_t val; 706368455ecSBen Woods int error, i, nargs, nparams, param; 707368455ecSBen Woods const int one = 1; 708e4b0a90eSBrooks Davis 709e4b0a90eSBrooks Davis nargs = gctl_get_int(req, "nargs"); 710368455ecSBen Woods if (nargs <= 0) { 711368455ecSBen Woods gctl_error(req, "Too few arguments."); 712e4b0a90eSBrooks Davis return; 713e4b0a90eSBrooks Davis } 714e4b0a90eSBrooks Davis 715368455ecSBen Woods /* Start generating metadata for provider(s) being initialized. */ 716217df2daSBen Woods explicit_bzero(&md, sizeof(md)); 717e4b0a90eSBrooks Davis strlcpy(md.md_magic, G_ELI_MAGIC, sizeof(md.md_magic)); 718e4b0a90eSBrooks Davis val = gctl_get_intmax(req, "mdversion"); 719e4b0a90eSBrooks Davis if (val == -1) { 720e4b0a90eSBrooks Davis version = G_ELI_VERSION; 721e4b0a90eSBrooks Davis } else if (val < 0 || val > G_ELI_VERSION) { 722e4b0a90eSBrooks Davis gctl_error(req, 723e4b0a90eSBrooks Davis "Invalid version specified should be between %u and %u.", 724e4b0a90eSBrooks Davis G_ELI_VERSION_00, G_ELI_VERSION); 725e4b0a90eSBrooks Davis return; 726e4b0a90eSBrooks Davis } else { 727e4b0a90eSBrooks Davis version = val; 728e4b0a90eSBrooks Davis } 729e4b0a90eSBrooks Davis md.md_version = version; 730e4b0a90eSBrooks Davis md.md_flags = 0; 731e4b0a90eSBrooks Davis if (gctl_get_int(req, "boot")) 732e4b0a90eSBrooks Davis md.md_flags |= G_ELI_FLAG_BOOT; 733e4b0a90eSBrooks Davis if (gctl_get_int(req, "geliboot")) 734e4b0a90eSBrooks Davis md.md_flags |= G_ELI_FLAG_GELIBOOT; 735e4b0a90eSBrooks Davis if (gctl_get_int(req, "displaypass")) 736e4b0a90eSBrooks Davis md.md_flags |= G_ELI_FLAG_GELIDISPLAYPASS; 737e4b0a90eSBrooks Davis if (gctl_get_int(req, "notrim")) 738e4b0a90eSBrooks Davis md.md_flags |= G_ELI_FLAG_NODELETE; 739e4b0a90eSBrooks Davis md.md_ealgo = CRYPTO_ALGORITHM_MIN - 1; 740e4b0a90eSBrooks Davis str = gctl_get_ascii(req, "aalgo"); 741e4b0a90eSBrooks Davis if (*str != '\0') { 742e4b0a90eSBrooks Davis if (version < G_ELI_VERSION_01) { 743e4b0a90eSBrooks Davis gctl_error(req, 744e4b0a90eSBrooks Davis "Data authentication is supported starting from version %u.", 745e4b0a90eSBrooks Davis G_ELI_VERSION_01); 746e4b0a90eSBrooks Davis return; 747e4b0a90eSBrooks Davis } 748e4b0a90eSBrooks Davis md.md_aalgo = g_eli_str2aalgo(str); 749e4b0a90eSBrooks Davis if (md.md_aalgo >= CRYPTO_ALGORITHM_MIN && 750e4b0a90eSBrooks Davis md.md_aalgo <= CRYPTO_ALGORITHM_MAX) { 751e4b0a90eSBrooks Davis md.md_flags |= G_ELI_FLAG_AUTH; 752e4b0a90eSBrooks Davis } else { 753e4b0a90eSBrooks Davis /* 754e4b0a90eSBrooks Davis * For backward compatibility, check if the -a option 755e4b0a90eSBrooks Davis * was used to provide encryption algorithm. 756e4b0a90eSBrooks Davis */ 757e4b0a90eSBrooks Davis md.md_ealgo = g_eli_str2ealgo(str); 758e4b0a90eSBrooks Davis if (md.md_ealgo < CRYPTO_ALGORITHM_MIN || 759e4b0a90eSBrooks Davis md.md_ealgo > CRYPTO_ALGORITHM_MAX) { 760e4b0a90eSBrooks Davis gctl_error(req, 761e4b0a90eSBrooks Davis "Invalid authentication algorithm."); 762e4b0a90eSBrooks Davis return; 763e4b0a90eSBrooks Davis } else { 764e4b0a90eSBrooks Davis fprintf(stderr, "warning: The -e option, not " 765e4b0a90eSBrooks Davis "the -a option is now used to specify " 766e4b0a90eSBrooks Davis "encryption algorithm to use.\n"); 767e4b0a90eSBrooks Davis } 768e4b0a90eSBrooks Davis } 769e4b0a90eSBrooks Davis } 770e4b0a90eSBrooks Davis if (md.md_ealgo < CRYPTO_ALGORITHM_MIN || 771e4b0a90eSBrooks Davis md.md_ealgo > CRYPTO_ALGORITHM_MAX) { 772e4b0a90eSBrooks Davis str = gctl_get_ascii(req, "ealgo"); 773e4b0a90eSBrooks Davis if (*str == '\0') { 774e4b0a90eSBrooks Davis if (version < G_ELI_VERSION_05) 775e4b0a90eSBrooks Davis str = "aes-cbc"; 776e4b0a90eSBrooks Davis else 777e4b0a90eSBrooks Davis str = GELI_ENC_ALGO; 778e4b0a90eSBrooks Davis } 779e4b0a90eSBrooks Davis md.md_ealgo = g_eli_str2ealgo(str); 780e4b0a90eSBrooks Davis if (md.md_ealgo < CRYPTO_ALGORITHM_MIN || 781e4b0a90eSBrooks Davis md.md_ealgo > CRYPTO_ALGORITHM_MAX) { 782e4b0a90eSBrooks Davis gctl_error(req, "Invalid encryption algorithm."); 783e4b0a90eSBrooks Davis return; 784e4b0a90eSBrooks Davis } 785e4b0a90eSBrooks Davis if (md.md_ealgo == CRYPTO_CAMELLIA_CBC && 786e4b0a90eSBrooks Davis version < G_ELI_VERSION_04) { 787e4b0a90eSBrooks Davis gctl_error(req, 788e4b0a90eSBrooks Davis "Camellia-CBC algorithm is supported starting from version %u.", 789e4b0a90eSBrooks Davis G_ELI_VERSION_04); 790e4b0a90eSBrooks Davis return; 791e4b0a90eSBrooks Davis } 792e4b0a90eSBrooks Davis if (md.md_ealgo == CRYPTO_AES_XTS && 793e4b0a90eSBrooks Davis version < G_ELI_VERSION_05) { 794e4b0a90eSBrooks Davis gctl_error(req, 795e4b0a90eSBrooks Davis "AES-XTS algorithm is supported starting from version %u.", 796e4b0a90eSBrooks Davis G_ELI_VERSION_05); 797e4b0a90eSBrooks Davis return; 798e4b0a90eSBrooks Davis } 799e4b0a90eSBrooks Davis } 800e4b0a90eSBrooks Davis val = gctl_get_intmax(req, "keylen"); 801e4b0a90eSBrooks Davis md.md_keylen = val; 802e4b0a90eSBrooks Davis md.md_keylen = g_eli_keylen(md.md_ealgo, md.md_keylen); 803e4b0a90eSBrooks Davis if (md.md_keylen == 0) { 804e4b0a90eSBrooks Davis gctl_error(req, "Invalid key length."); 805e4b0a90eSBrooks Davis return; 806e4b0a90eSBrooks Davis } 807e4b0a90eSBrooks Davis 808e4b0a90eSBrooks Davis val = gctl_get_intmax(req, "iterations"); 809e4b0a90eSBrooks Davis if (val != -1) { 810e4b0a90eSBrooks Davis int nonewpassphrase; 811e4b0a90eSBrooks Davis 812e4b0a90eSBrooks Davis /* 813e4b0a90eSBrooks Davis * Don't allow to set iterations when there will be no 814e4b0a90eSBrooks Davis * passphrase. 815e4b0a90eSBrooks Davis */ 816e4b0a90eSBrooks Davis nonewpassphrase = gctl_get_int(req, "nonewpassphrase"); 817e4b0a90eSBrooks Davis if (nonewpassphrase) { 818e4b0a90eSBrooks Davis gctl_error(req, 819e4b0a90eSBrooks Davis "Options -i and -P are mutually exclusive."); 820e4b0a90eSBrooks Davis return; 821e4b0a90eSBrooks Davis } 822e4b0a90eSBrooks Davis } 823e4b0a90eSBrooks Davis md.md_iterations = val; 824e4b0a90eSBrooks Davis 825e4b0a90eSBrooks Davis val = gctl_get_intmax(req, "sectorsize"); 826e4b0a90eSBrooks Davis if (val > sysconf(_SC_PAGE_SIZE)) { 827e4b0a90eSBrooks Davis fprintf(stderr, 828e4b0a90eSBrooks Davis "warning: Using sectorsize bigger than the page size!\n"); 829e4b0a90eSBrooks Davis } 830368455ecSBen Woods 831368455ecSBen Woods md.md_keys = 0x01; 832368455ecSBen Woods 833368455ecSBen Woods /* 834368455ecSBen Woods * Determine number of parameters in the parent geom request before the 835368455ecSBen Woods * nargs parameter and list of providers. 836368455ecSBen Woods */ 837368455ecSBen Woods nparams = req->narg - nargs - 1; 838368455ecSBen Woods 839368455ecSBen Woods /* Create new child request for each provider and issue to kernel */ 840368455ecSBen Woods for (i = 0; i < nargs; i++) { 841368455ecSBen Woods r = gctl_get_handle(); 842368455ecSBen Woods 843368455ecSBen Woods /* Copy each parameter from the parent request to the child */ 844368455ecSBen Woods for (param = 0; param < nparams; param++) { 845368455ecSBen Woods gctl_ro_param(r, req->arg[param].name, 846368455ecSBen Woods req->arg[param].len, req->arg[param].value); 847368455ecSBen Woods } 848368455ecSBen Woods 849368455ecSBen Woods /* Add a single provider to the parameter list of the child */ 850368455ecSBen Woods gctl_ro_param(r, "nargs", sizeof(one), &one); 851368455ecSBen Woods prov = gctl_get_ascii(req, "arg%d", i); 852368455ecSBen Woods gctl_ro_param(r, "arg0", -1, prov); 853368455ecSBen Woods 854368455ecSBen Woods mediasize = g_get_mediasize(prov); 855368455ecSBen Woods secsize = g_get_sectorsize(prov); 856368455ecSBen Woods if (mediasize == 0 || secsize == 0) { 857368455ecSBen Woods gctl_error(r, "Cannot get information about %s: %s.", 858368455ecSBen Woods prov, strerror(errno)); 859368455ecSBen Woods goto out; 860368455ecSBen Woods } 861368455ecSBen Woods 862368455ecSBen Woods md.md_provsize = mediasize; 863368455ecSBen Woods 864368455ecSBen Woods val = gctl_get_intmax(r, "sectorsize"); 865368455ecSBen Woods if (val == 0) { 866368455ecSBen Woods md.md_sectorsize = secsize; 867368455ecSBen Woods } else { 868368455ecSBen Woods if (val < 0 || (val % secsize) != 0 || !powerof2(val)) { 869368455ecSBen Woods gctl_error(r, "Invalid sector size."); 870368455ecSBen Woods goto out; 871368455ecSBen Woods } 872e4b0a90eSBrooks Davis md.md_sectorsize = val; 873e4b0a90eSBrooks Davis } 874e4b0a90eSBrooks Davis 875368455ecSBen Woods /* Use different salt and Master Key for each provider. */ 876e4b0a90eSBrooks Davis arc4random_buf(md.md_salt, sizeof(md.md_salt)); 877e4b0a90eSBrooks Davis arc4random_buf(md.md_mkeys, sizeof(md.md_mkeys)); 878e4b0a90eSBrooks Davis 879e4b0a90eSBrooks Davis /* Generate user key. */ 880368455ecSBen Woods if (eli_genkey(r, &md, key, true) == NULL) { 881368455ecSBen Woods /* 882368455ecSBen Woods * Error generating key - details added to geom request 883368455ecSBen Woods * by eli_genkey(). 884368455ecSBen Woods */ 885368455ecSBen Woods goto out; 886e4b0a90eSBrooks Davis } 887e4b0a90eSBrooks Davis 888e4b0a90eSBrooks Davis /* Encrypt the first and the only Master Key. */ 889368455ecSBen Woods error = g_eli_mkey_encrypt(md.md_ealgo, key, md.md_keylen, 890368455ecSBen Woods md.md_mkeys); 891e4b0a90eSBrooks Davis if (error != 0) { 892368455ecSBen Woods gctl_error(r, "Cannot encrypt Master Key: %s.", 893e4b0a90eSBrooks Davis strerror(error)); 894368455ecSBen Woods goto out; 895e4b0a90eSBrooks Davis } 896e4b0a90eSBrooks Davis 897*e45068e3SBen Woods /* Convert metadata to on-disk format. */ 898e4b0a90eSBrooks Davis eli_metadata_encode(&md, sector); 899368455ecSBen Woods 900*e45068e3SBen Woods /* Store metadata to disk. */ 901e4b0a90eSBrooks Davis error = g_metadata_store(prov, sector, sizeof(sector)); 902e4b0a90eSBrooks Davis if (error != 0) { 903368455ecSBen Woods gctl_error(r, "Cannot store metadata on %s: %s.", prov, 904e4b0a90eSBrooks Davis strerror(error)); 905368455ecSBen Woods goto out; 906e4b0a90eSBrooks Davis } 907e4b0a90eSBrooks Davis if (verbose) 908e4b0a90eSBrooks Davis printf("Metadata value stored on %s.\n", prov); 909368455ecSBen Woods 910e4b0a90eSBrooks Davis /* Backup metadata to a file. */ 911368455ecSBen Woods const char *p = prov; 912368455ecSBen Woods unsigned int j; 913368455ecSBen Woods 914368455ecSBen Woods /* 915368455ecSBen Woods * Check if provider string includes the devfs mountpoint 916368455ecSBen Woods * (typically /dev/). 917368455ecSBen Woods */ 918368455ecSBen Woods if (strncmp(p, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0) { 919368455ecSBen Woods /* Skip forward to the device filename only. */ 920368455ecSBen Woods p += sizeof(_PATH_DEV) - 1; 921368455ecSBen Woods } 922368455ecSBen Woods 923368455ecSBen Woods str = gctl_get_ascii(r, "backupfile"); 924e4b0a90eSBrooks Davis if (str[0] != '\0') { 925368455ecSBen Woods /* Backupfile given by the user, just copy it. */ 926e4b0a90eSBrooks Davis strlcpy(backfile, str, sizeof(backfile)); 927368455ecSBen Woods 928*e45068e3SBen Woods /* If multiple providers have been initialized in one 929*e45068e3SBen Woods * command, and the backup filename has been specified 930*e45068e3SBen Woods * as anything other than "none", make the backup 931*e45068e3SBen Woods * filename unique for each provider. */ 932*e45068e3SBen Woods if (nargs > 1 && strcmp(backfile, "none") != 0) { 933368455ecSBen Woods /* 934368455ecSBen Woods * Replace first occurrence of "PROV" with 935368455ecSBen Woods * provider name. 936368455ecSBen Woods */ 937368455ecSBen Woods str = strnstr(backfile, "PROV", 938368455ecSBen Woods sizeof(backfile)); 939368455ecSBen Woods if (str != NULL) { 940368455ecSBen Woods char suffix[MAXPATHLEN]; 941368455ecSBen Woods j = str - backfile; 942368455ecSBen Woods strlcpy(suffix, &backfile[j+4], 943368455ecSBen Woods sizeof(suffix)); 944368455ecSBen Woods backfile[j] = '\0'; 945368455ecSBen Woods strlcat(backfile, p, sizeof(backfile)); 946368455ecSBen Woods strlcat(backfile, suffix, 947368455ecSBen Woods sizeof(backfile)); 948368455ecSBen Woods } else { 949368455ecSBen Woods /* 950368455ecSBen Woods * "PROV" not found in backfile, append 951368455ecSBen Woods * provider name. 952368455ecSBen Woods */ 953368455ecSBen Woods strlcat(backfile, "-", 954368455ecSBen Woods sizeof(backfile)); 955368455ecSBen Woods strlcat(backfile, p, sizeof(backfile)); 956368455ecSBen Woods } 957368455ecSBen Woods } 958e4b0a90eSBrooks Davis } else { 959e4b0a90eSBrooks Davis /* Generate filename automatically. */ 960e4b0a90eSBrooks Davis snprintf(backfile, sizeof(backfile), "%s%s.eli", 961e4b0a90eSBrooks Davis GELI_BACKUP_DIR, p); 962e4b0a90eSBrooks Davis /* Replace all / with _. */ 963368455ecSBen Woods for (j = strlen(GELI_BACKUP_DIR); backfile[j] != '\0'; 964368455ecSBen Woods j++) { 965368455ecSBen Woods if (backfile[j] == '/') 966368455ecSBen Woods backfile[j] = '_'; 967e4b0a90eSBrooks Davis } 968e4b0a90eSBrooks Davis } 969e4b0a90eSBrooks Davis if (strcmp(backfile, "none") != 0 && 970368455ecSBen Woods eli_backup_create(r, prov, backfile) == 0) { 971368455ecSBen Woods printf("\nMetadata backup for provider %s can be found " 972368455ecSBen Woods "in %s\n", prov, backfile); 973368455ecSBen Woods printf("and can be restored with the following " 974368455ecSBen Woods "command:\n"); 975e4b0a90eSBrooks Davis printf("\n\t# geli restore %s %s\n\n", backfile, prov); 976e4b0a90eSBrooks Davis } 977368455ecSBen Woods 978368455ecSBen Woods out: 979368455ecSBen Woods /* 980368455ecSBen Woods * Print error for this request, and set parent request error 981368455ecSBen Woods * message. 982368455ecSBen Woods */ 983368455ecSBen Woods if (r->error != NULL && r->error[0] != '\0') { 984368455ecSBen Woods warnx("%s", r->error); 985368455ecSBen Woods gctl_error(req, "There was an error with at least one " 986368455ecSBen Woods "provider."); 987368455ecSBen Woods } 988368455ecSBen Woods 989368455ecSBen Woods gctl_free(r); 990368455ecSBen Woods 991368455ecSBen Woods /* 992*e45068e3SBen Woods * Erase sensitive and provider specific data from memory. 993368455ecSBen Woods */ 994368455ecSBen Woods explicit_bzero(key, sizeof(key)); 995*e45068e3SBen Woods explicit_bzero(sector, sizeof(sector)); 996*e45068e3SBen Woods explicit_bzero(&md.md_provsize, sizeof(md.md_provsize)); 997*e45068e3SBen Woods explicit_bzero(&md.md_sectorsize, sizeof(md.md_sectorsize)); 998*e45068e3SBen Woods explicit_bzero(&md.md_salt, sizeof(md.md_salt)); 999*e45068e3SBen Woods explicit_bzero(&md.md_mkeys, sizeof(md.md_mkeys)); 1000368455ecSBen Woods } 1001368455ecSBen Woods 1002368455ecSBen Woods /* Clear the cached metadata, including keys. */ 1003368455ecSBen Woods explicit_bzero(&md, sizeof(md)); 1004e4b0a90eSBrooks Davis } 1005e4b0a90eSBrooks Davis 1006e4b0a90eSBrooks Davis static void 1007e4b0a90eSBrooks Davis eli_attach(struct gctl_req *req) 1008e4b0a90eSBrooks Davis { 1009e4b0a90eSBrooks Davis struct g_eli_metadata md; 1010217df2daSBen Woods struct gctl_req *r; 1011e4b0a90eSBrooks Davis const char *prov; 1012e4b0a90eSBrooks Davis off_t mediasize; 1013217df2daSBen Woods int i, nargs, nparams, param; 1014217df2daSBen Woods const int one = 1; 1015e4b0a90eSBrooks Davis 1016e4b0a90eSBrooks Davis nargs = gctl_get_int(req, "nargs"); 1017368455ecSBen Woods if (nargs <= 0) { 1018217df2daSBen Woods gctl_error(req, "Too few arguments."); 1019e4b0a90eSBrooks Davis return; 1020e4b0a90eSBrooks Davis } 1021e4b0a90eSBrooks Davis 1022217df2daSBen Woods unsigned char key[G_ELI_USERKEYLEN]; 1023217df2daSBen Woods 1024217df2daSBen Woods /* 1025217df2daSBen Woods * Determine number of parameters in the parent geom request before the 1026217df2daSBen Woods * nargs parameter and list of providers. 1027217df2daSBen Woods */ 1028217df2daSBen Woods nparams = req->narg - nargs - 1; 1029217df2daSBen Woods 1030368455ecSBen Woods /* Create new child request for each provider and issue to kernel */ 1031217df2daSBen Woods for (i = 0; i < nargs; i++) { 1032217df2daSBen Woods r = gctl_get_handle(); 1033217df2daSBen Woods 1034368455ecSBen Woods /* Copy each parameter from the parent request to the child */ 1035217df2daSBen Woods for (param = 0; param < nparams; param++) { 1036368455ecSBen Woods gctl_ro_param(r, req->arg[param].name, 1037368455ecSBen Woods req->arg[param].len, req->arg[param].value); 1038217df2daSBen Woods } 1039217df2daSBen Woods 1040217df2daSBen Woods /* Add a single provider to the parameter list of the child */ 1041217df2daSBen Woods gctl_ro_param(r, "nargs", sizeof(one), &one); 1042217df2daSBen Woods prov = gctl_get_ascii(req, "arg%d", i); 1043217df2daSBen Woods gctl_ro_param(r, "arg0", -1, prov); 1044217df2daSBen Woods 10454b8e4d53SBen Woods if (eli_metadata_read(r, prov, &md) == -1) { 10464b8e4d53SBen Woods /* 10474b8e4d53SBen Woods * Error reading metadata - details added to geom 10484b8e4d53SBen Woods * request by eli_metadata_read(). 10494b8e4d53SBen Woods */ 10504b8e4d53SBen Woods goto out; 10514b8e4d53SBen Woods } 1052e4b0a90eSBrooks Davis 1053e4b0a90eSBrooks Davis mediasize = g_get_mediasize(prov); 1054e4b0a90eSBrooks Davis if (md.md_provsize != (uint64_t)mediasize) { 1055217df2daSBen Woods gctl_error(r, "Provider size mismatch."); 10564b8e4d53SBen Woods goto out; 1057e4b0a90eSBrooks Davis } 1058e4b0a90eSBrooks Davis 1059217df2daSBen Woods if (eli_genkey(r, &md, key, false) == NULL) { 10604b8e4d53SBen Woods /* 10614b8e4d53SBen Woods * Error generating key - details added to geom request 10624b8e4d53SBen Woods * by eli_genkey(). 10634b8e4d53SBen Woods */ 10644b8e4d53SBen Woods goto out; 1065e4b0a90eSBrooks Davis } 1066e4b0a90eSBrooks Davis 1067217df2daSBen Woods gctl_ro_param(r, "key", sizeof(key), key); 1068217df2daSBen Woods 1069217df2daSBen Woods if (gctl_issue(r) == NULL) { 1070e4b0a90eSBrooks Davis if (verbose) 1071e4b0a90eSBrooks Davis printf("Attached to %s.\n", prov); 1072e4b0a90eSBrooks Davis } 1073217df2daSBen Woods 10744b8e4d53SBen Woods out: 1075368455ecSBen Woods /* 1076368455ecSBen Woods * Print error for this request, and set parent request error 1077368455ecSBen Woods * message. 1078368455ecSBen Woods */ 1079217df2daSBen Woods if (r->error != NULL && r->error[0] != '\0') { 1080217df2daSBen Woods warnx("%s", r->error); 1081368455ecSBen Woods gctl_error(req, "There was an error with at least one " 1082368455ecSBen Woods "provider."); 1083217df2daSBen Woods } 1084217df2daSBen Woods 1085217df2daSBen Woods gctl_free(r); 1086217df2daSBen Woods 10874b8e4d53SBen Woods /* Clear sensitive data from memory. */ 1088217df2daSBen Woods explicit_bzero(key, sizeof(key)); 1089217df2daSBen Woods } 1090217df2daSBen Woods 10914b8e4d53SBen Woods /* Clear sensitive data from memory. */ 1092217df2daSBen Woods explicit_bzero(cached_passphrase, sizeof(cached_passphrase)); 1093e4b0a90eSBrooks Davis } 1094e4b0a90eSBrooks Davis 1095e4b0a90eSBrooks Davis static void 1096e4b0a90eSBrooks Davis eli_configure_detached(struct gctl_req *req, const char *prov, int boot, 1097e4b0a90eSBrooks Davis int geliboot, int displaypass, int trim) 1098e4b0a90eSBrooks Davis { 1099e4b0a90eSBrooks Davis struct g_eli_metadata md; 1100e4b0a90eSBrooks Davis bool changed = 0; 1101e4b0a90eSBrooks Davis 1102e4b0a90eSBrooks Davis if (eli_metadata_read(req, prov, &md) == -1) 1103e4b0a90eSBrooks Davis return; 1104e4b0a90eSBrooks Davis 1105e4b0a90eSBrooks Davis if (boot == 1 && (md.md_flags & G_ELI_FLAG_BOOT)) { 1106e4b0a90eSBrooks Davis if (verbose) 1107e4b0a90eSBrooks Davis printf("BOOT flag already configured for %s.\n", prov); 1108e4b0a90eSBrooks Davis } else if (boot == 0 && !(md.md_flags & G_ELI_FLAG_BOOT)) { 1109e4b0a90eSBrooks Davis if (verbose) 1110e4b0a90eSBrooks Davis printf("BOOT flag not configured for %s.\n", prov); 1111e4b0a90eSBrooks Davis } else if (boot >= 0) { 1112e4b0a90eSBrooks Davis if (boot) 1113e4b0a90eSBrooks Davis md.md_flags |= G_ELI_FLAG_BOOT; 1114e4b0a90eSBrooks Davis else 1115e4b0a90eSBrooks Davis md.md_flags &= ~G_ELI_FLAG_BOOT; 1116e4b0a90eSBrooks Davis changed = 1; 1117e4b0a90eSBrooks Davis } 1118e4b0a90eSBrooks Davis 1119e4b0a90eSBrooks Davis if (geliboot == 1 && (md.md_flags & G_ELI_FLAG_GELIBOOT)) { 1120e4b0a90eSBrooks Davis if (verbose) 1121e4b0a90eSBrooks Davis printf("GELIBOOT flag already configured for %s.\n", prov); 1122e4b0a90eSBrooks Davis } else if (geliboot == 0 && !(md.md_flags & G_ELI_FLAG_GELIBOOT)) { 1123e4b0a90eSBrooks Davis if (verbose) 1124e4b0a90eSBrooks Davis printf("GELIBOOT flag not configured for %s.\n", prov); 1125e4b0a90eSBrooks Davis } else if (geliboot >= 0) { 1126e4b0a90eSBrooks Davis if (geliboot) 1127e4b0a90eSBrooks Davis md.md_flags |= G_ELI_FLAG_GELIBOOT; 1128e4b0a90eSBrooks Davis else 1129e4b0a90eSBrooks Davis md.md_flags &= ~G_ELI_FLAG_GELIBOOT; 1130e4b0a90eSBrooks Davis changed = 1; 1131e4b0a90eSBrooks Davis } 1132e4b0a90eSBrooks Davis 1133e4b0a90eSBrooks Davis if (displaypass == 1 && (md.md_flags & G_ELI_FLAG_GELIDISPLAYPASS)) { 1134e4b0a90eSBrooks Davis if (verbose) 1135e4b0a90eSBrooks Davis printf("GELIDISPLAYPASS flag already configured for %s.\n", prov); 1136e4b0a90eSBrooks Davis } else if (displaypass == 0 && 1137e4b0a90eSBrooks Davis !(md.md_flags & G_ELI_FLAG_GELIDISPLAYPASS)) { 1138e4b0a90eSBrooks Davis if (verbose) 1139e4b0a90eSBrooks Davis printf("GELIDISPLAYPASS flag not configured for %s.\n", prov); 1140e4b0a90eSBrooks Davis } else if (displaypass >= 0) { 1141e4b0a90eSBrooks Davis if (displaypass) 1142e4b0a90eSBrooks Davis md.md_flags |= G_ELI_FLAG_GELIDISPLAYPASS; 1143e4b0a90eSBrooks Davis else 1144e4b0a90eSBrooks Davis md.md_flags &= ~G_ELI_FLAG_GELIDISPLAYPASS; 1145e4b0a90eSBrooks Davis changed = 1; 1146e4b0a90eSBrooks Davis } 1147e4b0a90eSBrooks Davis 1148e4b0a90eSBrooks Davis if (trim == 0 && (md.md_flags & G_ELI_FLAG_NODELETE)) { 1149e4b0a90eSBrooks Davis if (verbose) 1150e4b0a90eSBrooks Davis printf("TRIM disable flag already configured for %s.\n", prov); 1151e4b0a90eSBrooks Davis } else if (trim == 1 && !(md.md_flags & G_ELI_FLAG_NODELETE)) { 1152e4b0a90eSBrooks Davis if (verbose) 1153e4b0a90eSBrooks Davis printf("TRIM disable flag not configured for %s.\n", prov); 1154e4b0a90eSBrooks Davis } else if (trim >= 0) { 1155e4b0a90eSBrooks Davis if (trim) 1156e4b0a90eSBrooks Davis md.md_flags &= ~G_ELI_FLAG_NODELETE; 1157e4b0a90eSBrooks Davis else 1158e4b0a90eSBrooks Davis md.md_flags |= G_ELI_FLAG_NODELETE; 1159e4b0a90eSBrooks Davis changed = 1; 1160e4b0a90eSBrooks Davis } 1161e4b0a90eSBrooks Davis 1162e4b0a90eSBrooks Davis if (changed) 1163e4b0a90eSBrooks Davis eli_metadata_store(req, prov, &md); 1164217df2daSBen Woods explicit_bzero(&md, sizeof(md)); 1165e4b0a90eSBrooks Davis } 1166e4b0a90eSBrooks Davis 1167e4b0a90eSBrooks Davis static void 1168e4b0a90eSBrooks Davis eli_configure(struct gctl_req *req) 1169e4b0a90eSBrooks Davis { 1170e4b0a90eSBrooks Davis const char *prov; 1171e4b0a90eSBrooks Davis bool boot, noboot, geliboot, nogeliboot, displaypass, nodisplaypass; 1172e4b0a90eSBrooks Davis bool trim, notrim; 1173e4b0a90eSBrooks Davis int doboot, dogeliboot, dodisplaypass, dotrim; 1174e4b0a90eSBrooks Davis int i, nargs; 1175e4b0a90eSBrooks Davis 1176e4b0a90eSBrooks Davis nargs = gctl_get_int(req, "nargs"); 1177e4b0a90eSBrooks Davis if (nargs == 0) { 1178e4b0a90eSBrooks Davis gctl_error(req, "Too few arguments."); 1179e4b0a90eSBrooks Davis return; 1180e4b0a90eSBrooks Davis } 1181e4b0a90eSBrooks Davis 1182e4b0a90eSBrooks Davis boot = gctl_get_int(req, "boot"); 1183e4b0a90eSBrooks Davis noboot = gctl_get_int(req, "noboot"); 1184e4b0a90eSBrooks Davis geliboot = gctl_get_int(req, "geliboot"); 1185e4b0a90eSBrooks Davis nogeliboot = gctl_get_int(req, "nogeliboot"); 1186e4b0a90eSBrooks Davis displaypass = gctl_get_int(req, "displaypass"); 1187e4b0a90eSBrooks Davis nodisplaypass = gctl_get_int(req, "nodisplaypass"); 1188e4b0a90eSBrooks Davis trim = gctl_get_int(req, "trim"); 1189e4b0a90eSBrooks Davis notrim = gctl_get_int(req, "notrim"); 1190e4b0a90eSBrooks Davis 1191e4b0a90eSBrooks Davis doboot = -1; 1192e4b0a90eSBrooks Davis if (boot && noboot) { 1193e4b0a90eSBrooks Davis gctl_error(req, "Options -b and -B are mutually exclusive."); 1194e4b0a90eSBrooks Davis return; 1195e4b0a90eSBrooks Davis } 1196e4b0a90eSBrooks Davis if (boot) 1197e4b0a90eSBrooks Davis doboot = 1; 1198e4b0a90eSBrooks Davis else if (noboot) 1199e4b0a90eSBrooks Davis doboot = 0; 1200e4b0a90eSBrooks Davis 1201e4b0a90eSBrooks Davis dogeliboot = -1; 1202e4b0a90eSBrooks Davis if (geliboot && nogeliboot) { 1203e4b0a90eSBrooks Davis gctl_error(req, "Options -g and -G are mutually exclusive."); 1204e4b0a90eSBrooks Davis return; 1205e4b0a90eSBrooks Davis } 1206e4b0a90eSBrooks Davis if (geliboot) 1207e4b0a90eSBrooks Davis dogeliboot = 1; 1208e4b0a90eSBrooks Davis else if (nogeliboot) 1209e4b0a90eSBrooks Davis dogeliboot = 0; 1210e4b0a90eSBrooks Davis 1211e4b0a90eSBrooks Davis dodisplaypass = -1; 1212e4b0a90eSBrooks Davis if (displaypass && nodisplaypass) { 1213e4b0a90eSBrooks Davis gctl_error(req, "Options -d and -D are mutually exclusive."); 1214e4b0a90eSBrooks Davis return; 1215e4b0a90eSBrooks Davis } 1216e4b0a90eSBrooks Davis if (displaypass) 1217e4b0a90eSBrooks Davis dodisplaypass = 1; 1218e4b0a90eSBrooks Davis else if (nodisplaypass) 1219e4b0a90eSBrooks Davis dodisplaypass = 0; 1220e4b0a90eSBrooks Davis 1221e4b0a90eSBrooks Davis dotrim = -1; 1222e4b0a90eSBrooks Davis if (trim && notrim) { 1223e4b0a90eSBrooks Davis gctl_error(req, "Options -t and -T are mutually exclusive."); 1224e4b0a90eSBrooks Davis return; 1225e4b0a90eSBrooks Davis } 1226e4b0a90eSBrooks Davis if (trim) 1227e4b0a90eSBrooks Davis dotrim = 1; 1228e4b0a90eSBrooks Davis else if (notrim) 1229e4b0a90eSBrooks Davis dotrim = 0; 1230e4b0a90eSBrooks Davis 1231e4b0a90eSBrooks Davis if (doboot == -1 && dogeliboot == -1 && dodisplaypass == -1 && 1232e4b0a90eSBrooks Davis dotrim == -1) { 1233e4b0a90eSBrooks Davis gctl_error(req, "No option given."); 1234e4b0a90eSBrooks Davis return; 1235e4b0a90eSBrooks Davis } 1236e4b0a90eSBrooks Davis 1237e4b0a90eSBrooks Davis /* First attached providers. */ 1238e4b0a90eSBrooks Davis gctl_issue(req); 1239e4b0a90eSBrooks Davis /* Now the rest. */ 1240e4b0a90eSBrooks Davis for (i = 0; i < nargs; i++) { 1241e4b0a90eSBrooks Davis prov = gctl_get_ascii(req, "arg%d", i); 1242e4b0a90eSBrooks Davis if (!eli_is_attached(prov)) { 1243e4b0a90eSBrooks Davis eli_configure_detached(req, prov, doboot, dogeliboot, 1244e4b0a90eSBrooks Davis dodisplaypass, dotrim); 1245e4b0a90eSBrooks Davis } 1246e4b0a90eSBrooks Davis } 1247e4b0a90eSBrooks Davis } 1248e4b0a90eSBrooks Davis 1249e4b0a90eSBrooks Davis static void 1250e4b0a90eSBrooks Davis eli_setkey_attached(struct gctl_req *req, struct g_eli_metadata *md) 1251e4b0a90eSBrooks Davis { 1252e4b0a90eSBrooks Davis unsigned char key[G_ELI_USERKEYLEN]; 1253e4b0a90eSBrooks Davis intmax_t val, old = 0; 1254e4b0a90eSBrooks Davis int error; 1255e4b0a90eSBrooks Davis 1256e4b0a90eSBrooks Davis val = gctl_get_intmax(req, "iterations"); 1257e4b0a90eSBrooks Davis /* Check if iterations number should be changed. */ 1258e4b0a90eSBrooks Davis if (val != -1) 1259e4b0a90eSBrooks Davis md->md_iterations = val; 1260e4b0a90eSBrooks Davis else 1261e4b0a90eSBrooks Davis old = md->md_iterations; 1262e4b0a90eSBrooks Davis 1263e4b0a90eSBrooks Davis /* Generate key for Master Key encryption. */ 1264e4b0a90eSBrooks Davis if (eli_genkey(req, md, key, true) == NULL) { 1265217df2daSBen Woods explicit_bzero(key, sizeof(key)); 1266e4b0a90eSBrooks Davis return; 1267e4b0a90eSBrooks Davis } 1268e4b0a90eSBrooks Davis /* 1269e4b0a90eSBrooks Davis * If number of iterations has changed, but wasn't given as a 1270e4b0a90eSBrooks Davis * command-line argument, update the request. 1271e4b0a90eSBrooks Davis */ 1272e4b0a90eSBrooks Davis if (val == -1 && md->md_iterations != old) { 1273e4b0a90eSBrooks Davis error = gctl_change_param(req, "iterations", sizeof(intmax_t), 1274e4b0a90eSBrooks Davis &md->md_iterations); 1275e4b0a90eSBrooks Davis assert(error == 0); 1276e4b0a90eSBrooks Davis } 1277e4b0a90eSBrooks Davis 1278e4b0a90eSBrooks Davis gctl_ro_param(req, "key", sizeof(key), key); 1279e4b0a90eSBrooks Davis gctl_issue(req); 1280217df2daSBen Woods explicit_bzero(key, sizeof(key)); 1281e4b0a90eSBrooks Davis } 1282e4b0a90eSBrooks Davis 1283e4b0a90eSBrooks Davis static void 1284e4b0a90eSBrooks Davis eli_setkey_detached(struct gctl_req *req, const char *prov, 1285e4b0a90eSBrooks Davis struct g_eli_metadata *md) 1286e4b0a90eSBrooks Davis { 1287e4b0a90eSBrooks Davis unsigned char key[G_ELI_USERKEYLEN], mkey[G_ELI_DATAIVKEYLEN]; 1288e4b0a90eSBrooks Davis unsigned char *mkeydst; 1289e4b0a90eSBrooks Davis unsigned int nkey; 1290e4b0a90eSBrooks Davis intmax_t val; 1291e4b0a90eSBrooks Davis int error; 1292e4b0a90eSBrooks Davis 1293e4b0a90eSBrooks Davis if (md->md_keys == 0) { 1294e4b0a90eSBrooks Davis gctl_error(req, "No valid keys on %s.", prov); 1295e4b0a90eSBrooks Davis return; 1296e4b0a90eSBrooks Davis } 1297e4b0a90eSBrooks Davis 1298e4b0a90eSBrooks Davis /* Generate key for Master Key decryption. */ 1299e4b0a90eSBrooks Davis if (eli_genkey(req, md, key, false) == NULL) { 1300217df2daSBen Woods explicit_bzero(key, sizeof(key)); 1301e4b0a90eSBrooks Davis return; 1302e4b0a90eSBrooks Davis } 1303e4b0a90eSBrooks Davis 1304e4b0a90eSBrooks Davis /* Decrypt Master Key. */ 1305e4b0a90eSBrooks Davis error = g_eli_mkey_decrypt_any(md, key, mkey, &nkey); 1306217df2daSBen Woods explicit_bzero(key, sizeof(key)); 1307e4b0a90eSBrooks Davis if (error != 0) { 1308217df2daSBen Woods explicit_bzero(md, sizeof(*md)); 1309e4b0a90eSBrooks Davis if (error == -1) 1310e4b0a90eSBrooks Davis gctl_error(req, "Wrong key for %s.", prov); 1311e4b0a90eSBrooks Davis else /* if (error > 0) */ { 1312e4b0a90eSBrooks Davis gctl_error(req, "Cannot decrypt Master Key: %s.", 1313e4b0a90eSBrooks Davis strerror(error)); 1314e4b0a90eSBrooks Davis } 1315e4b0a90eSBrooks Davis return; 1316e4b0a90eSBrooks Davis } 1317e4b0a90eSBrooks Davis if (verbose) 1318e4b0a90eSBrooks Davis printf("Decrypted Master Key %u.\n", nkey); 1319e4b0a90eSBrooks Davis 1320e4b0a90eSBrooks Davis val = gctl_get_intmax(req, "keyno"); 1321e4b0a90eSBrooks Davis if (val != -1) 1322e4b0a90eSBrooks Davis nkey = val; 1323e4b0a90eSBrooks Davis #if 0 1324e4b0a90eSBrooks Davis else 1325e4b0a90eSBrooks Davis ; /* Use the key number which was found during decryption. */ 1326e4b0a90eSBrooks Davis #endif 1327e4b0a90eSBrooks Davis if (nkey >= G_ELI_MAXMKEYS) { 1328e4b0a90eSBrooks Davis gctl_error(req, "Invalid '%s' argument.", "keyno"); 1329e4b0a90eSBrooks Davis return; 1330e4b0a90eSBrooks Davis } 1331e4b0a90eSBrooks Davis 1332e4b0a90eSBrooks Davis val = gctl_get_intmax(req, "iterations"); 1333e4b0a90eSBrooks Davis /* Check if iterations number should and can be changed. */ 1334e4b0a90eSBrooks Davis if (val != -1 && md->md_iterations == -1) { 1335e4b0a90eSBrooks Davis md->md_iterations = val; 1336e4b0a90eSBrooks Davis } else if (val != -1 && val != md->md_iterations) { 1337e4b0a90eSBrooks Davis if (bitcount32(md->md_keys) != 1) { 1338e4b0a90eSBrooks Davis gctl_error(req, "To be able to use '-i' option, only " 1339e4b0a90eSBrooks Davis "one key can be defined."); 1340e4b0a90eSBrooks Davis return; 1341e4b0a90eSBrooks Davis } 1342e4b0a90eSBrooks Davis if (md->md_keys != (1 << nkey)) { 1343e4b0a90eSBrooks Davis gctl_error(req, "Only already defined key can be " 1344e4b0a90eSBrooks Davis "changed when '-i' option is used."); 1345e4b0a90eSBrooks Davis return; 1346e4b0a90eSBrooks Davis } 1347e4b0a90eSBrooks Davis md->md_iterations = val; 1348e4b0a90eSBrooks Davis } 1349e4b0a90eSBrooks Davis 1350e4b0a90eSBrooks Davis mkeydst = md->md_mkeys + nkey * G_ELI_MKEYLEN; 1351e4b0a90eSBrooks Davis md->md_keys |= (1 << nkey); 1352e4b0a90eSBrooks Davis 1353e4b0a90eSBrooks Davis bcopy(mkey, mkeydst, sizeof(mkey)); 1354217df2daSBen Woods explicit_bzero(mkey, sizeof(mkey)); 1355e4b0a90eSBrooks Davis 1356e4b0a90eSBrooks Davis /* Generate key for Master Key encryption. */ 1357e4b0a90eSBrooks Davis if (eli_genkey(req, md, key, true) == NULL) { 1358217df2daSBen Woods explicit_bzero(key, sizeof(key)); 1359217df2daSBen Woods explicit_bzero(md, sizeof(*md)); 1360e4b0a90eSBrooks Davis return; 1361e4b0a90eSBrooks Davis } 1362e4b0a90eSBrooks Davis 1363e4b0a90eSBrooks Davis /* Encrypt the Master-Key with the new key. */ 1364e4b0a90eSBrooks Davis error = g_eli_mkey_encrypt(md->md_ealgo, key, md->md_keylen, mkeydst); 1365217df2daSBen Woods explicit_bzero(key, sizeof(key)); 1366e4b0a90eSBrooks Davis if (error != 0) { 1367217df2daSBen Woods explicit_bzero(md, sizeof(*md)); 1368e4b0a90eSBrooks Davis gctl_error(req, "Cannot encrypt Master Key: %s.", 1369e4b0a90eSBrooks Davis strerror(error)); 1370e4b0a90eSBrooks Davis return; 1371e4b0a90eSBrooks Davis } 1372e4b0a90eSBrooks Davis 1373e4b0a90eSBrooks Davis /* Store metadata with fresh key. */ 1374e4b0a90eSBrooks Davis eli_metadata_store(req, prov, md); 1375217df2daSBen Woods explicit_bzero(md, sizeof(*md)); 1376e4b0a90eSBrooks Davis } 1377e4b0a90eSBrooks Davis 1378e4b0a90eSBrooks Davis static void 1379e4b0a90eSBrooks Davis eli_setkey(struct gctl_req *req) 1380e4b0a90eSBrooks Davis { 1381e4b0a90eSBrooks Davis struct g_eli_metadata md; 1382e4b0a90eSBrooks Davis const char *prov; 1383e4b0a90eSBrooks Davis int nargs; 1384e4b0a90eSBrooks Davis 1385e4b0a90eSBrooks Davis nargs = gctl_get_int(req, "nargs"); 1386e4b0a90eSBrooks Davis if (nargs != 1) { 1387e4b0a90eSBrooks Davis gctl_error(req, "Invalid number of arguments."); 1388e4b0a90eSBrooks Davis return; 1389e4b0a90eSBrooks Davis } 1390e4b0a90eSBrooks Davis prov = gctl_get_ascii(req, "arg0"); 1391e4b0a90eSBrooks Davis 1392e4b0a90eSBrooks Davis if (eli_metadata_read(req, prov, &md) == -1) 1393e4b0a90eSBrooks Davis return; 1394e4b0a90eSBrooks Davis 1395e4b0a90eSBrooks Davis if (eli_is_attached(prov)) 1396e4b0a90eSBrooks Davis eli_setkey_attached(req, &md); 1397e4b0a90eSBrooks Davis else 1398e4b0a90eSBrooks Davis eli_setkey_detached(req, prov, &md); 1399e4b0a90eSBrooks Davis 1400e4b0a90eSBrooks Davis if (req->error == NULL || req->error[0] == '\0') { 1401e4b0a90eSBrooks Davis printf("Note, that the master key encrypted with old keys " 1402e4b0a90eSBrooks Davis "and/or passphrase may still exists in a metadata backup " 1403e4b0a90eSBrooks Davis "file.\n"); 1404e4b0a90eSBrooks Davis } 1405e4b0a90eSBrooks Davis } 1406e4b0a90eSBrooks Davis 1407e4b0a90eSBrooks Davis static void 1408e4b0a90eSBrooks Davis eli_delkey_attached(struct gctl_req *req, const char *prov __unused) 1409e4b0a90eSBrooks Davis { 1410e4b0a90eSBrooks Davis 1411e4b0a90eSBrooks Davis gctl_issue(req); 1412e4b0a90eSBrooks Davis } 1413e4b0a90eSBrooks Davis 1414e4b0a90eSBrooks Davis static void 1415e4b0a90eSBrooks Davis eli_delkey_detached(struct gctl_req *req, const char *prov) 1416e4b0a90eSBrooks Davis { 1417e4b0a90eSBrooks Davis struct g_eli_metadata md; 1418e4b0a90eSBrooks Davis unsigned char *mkeydst; 1419e4b0a90eSBrooks Davis unsigned int nkey; 1420e4b0a90eSBrooks Davis intmax_t val; 1421e4b0a90eSBrooks Davis bool all, force; 1422e4b0a90eSBrooks Davis 1423e4b0a90eSBrooks Davis if (eli_metadata_read(req, prov, &md) == -1) 1424e4b0a90eSBrooks Davis return; 1425e4b0a90eSBrooks Davis 1426e4b0a90eSBrooks Davis all = gctl_get_int(req, "all"); 1427e4b0a90eSBrooks Davis if (all) 1428e4b0a90eSBrooks Davis arc4random_buf(md.md_mkeys, sizeof(md.md_mkeys)); 1429e4b0a90eSBrooks Davis else { 1430e4b0a90eSBrooks Davis force = gctl_get_int(req, "force"); 1431e4b0a90eSBrooks Davis val = gctl_get_intmax(req, "keyno"); 1432e4b0a90eSBrooks Davis if (val == -1) { 1433e4b0a90eSBrooks Davis gctl_error(req, "Key number has to be specified."); 1434e4b0a90eSBrooks Davis return; 1435e4b0a90eSBrooks Davis } 1436e4b0a90eSBrooks Davis nkey = val; 1437e4b0a90eSBrooks Davis if (nkey >= G_ELI_MAXMKEYS) { 1438e4b0a90eSBrooks Davis gctl_error(req, "Invalid '%s' argument.", "keyno"); 1439e4b0a90eSBrooks Davis return; 1440e4b0a90eSBrooks Davis } 1441e4b0a90eSBrooks Davis if (!(md.md_keys & (1 << nkey)) && !force) { 1442e4b0a90eSBrooks Davis gctl_error(req, "Master Key %u is not set.", nkey); 1443e4b0a90eSBrooks Davis return; 1444e4b0a90eSBrooks Davis } 1445e4b0a90eSBrooks Davis md.md_keys &= ~(1 << nkey); 1446e4b0a90eSBrooks Davis if (md.md_keys == 0 && !force) { 1447e4b0a90eSBrooks Davis gctl_error(req, "This is the last Master Key. Use '-f' " 1448e4b0a90eSBrooks Davis "option if you really want to remove it."); 1449e4b0a90eSBrooks Davis return; 1450e4b0a90eSBrooks Davis } 1451e4b0a90eSBrooks Davis mkeydst = md.md_mkeys + nkey * G_ELI_MKEYLEN; 1452e4b0a90eSBrooks Davis arc4random_buf(mkeydst, G_ELI_MKEYLEN); 1453e4b0a90eSBrooks Davis } 1454e4b0a90eSBrooks Davis 1455e4b0a90eSBrooks Davis eli_metadata_store(req, prov, &md); 1456217df2daSBen Woods explicit_bzero(&md, sizeof(md)); 1457e4b0a90eSBrooks Davis } 1458e4b0a90eSBrooks Davis 1459e4b0a90eSBrooks Davis static void 1460e4b0a90eSBrooks Davis eli_delkey(struct gctl_req *req) 1461e4b0a90eSBrooks Davis { 1462e4b0a90eSBrooks Davis const char *prov; 1463e4b0a90eSBrooks Davis int nargs; 1464e4b0a90eSBrooks Davis 1465e4b0a90eSBrooks Davis nargs = gctl_get_int(req, "nargs"); 1466e4b0a90eSBrooks Davis if (nargs != 1) { 1467e4b0a90eSBrooks Davis gctl_error(req, "Invalid number of arguments."); 1468e4b0a90eSBrooks Davis return; 1469e4b0a90eSBrooks Davis } 1470e4b0a90eSBrooks Davis prov = gctl_get_ascii(req, "arg0"); 1471e4b0a90eSBrooks Davis 1472e4b0a90eSBrooks Davis if (eli_is_attached(prov)) 1473e4b0a90eSBrooks Davis eli_delkey_attached(req, prov); 1474e4b0a90eSBrooks Davis else 1475e4b0a90eSBrooks Davis eli_delkey_detached(req, prov); 1476e4b0a90eSBrooks Davis } 1477e4b0a90eSBrooks Davis 1478e4b0a90eSBrooks Davis static void 1479e4b0a90eSBrooks Davis eli_resume(struct gctl_req *req) 1480e4b0a90eSBrooks Davis { 1481e4b0a90eSBrooks Davis struct g_eli_metadata md; 1482e4b0a90eSBrooks Davis unsigned char key[G_ELI_USERKEYLEN]; 1483e4b0a90eSBrooks Davis const char *prov; 1484e4b0a90eSBrooks Davis off_t mediasize; 1485e4b0a90eSBrooks Davis int nargs; 1486e4b0a90eSBrooks Davis 1487e4b0a90eSBrooks Davis nargs = gctl_get_int(req, "nargs"); 1488e4b0a90eSBrooks Davis if (nargs != 1) { 1489e4b0a90eSBrooks Davis gctl_error(req, "Invalid number of arguments."); 1490e4b0a90eSBrooks Davis return; 1491e4b0a90eSBrooks Davis } 1492e4b0a90eSBrooks Davis prov = gctl_get_ascii(req, "arg0"); 1493e4b0a90eSBrooks Davis 1494e4b0a90eSBrooks Davis if (eli_metadata_read(req, prov, &md) == -1) 1495e4b0a90eSBrooks Davis return; 1496e4b0a90eSBrooks Davis 1497e4b0a90eSBrooks Davis mediasize = g_get_mediasize(prov); 1498e4b0a90eSBrooks Davis if (md.md_provsize != (uint64_t)mediasize) { 1499e4b0a90eSBrooks Davis gctl_error(req, "Provider size mismatch."); 1500e4b0a90eSBrooks Davis return; 1501e4b0a90eSBrooks Davis } 1502e4b0a90eSBrooks Davis 1503e4b0a90eSBrooks Davis if (eli_genkey(req, &md, key, false) == NULL) { 1504217df2daSBen Woods explicit_bzero(key, sizeof(key)); 1505e4b0a90eSBrooks Davis return; 1506e4b0a90eSBrooks Davis } 1507e4b0a90eSBrooks Davis 1508e4b0a90eSBrooks Davis gctl_ro_param(req, "key", sizeof(key), key); 1509e4b0a90eSBrooks Davis if (gctl_issue(req) == NULL) { 1510e4b0a90eSBrooks Davis if (verbose) 1511e4b0a90eSBrooks Davis printf("Resumed %s.\n", prov); 1512e4b0a90eSBrooks Davis } 1513217df2daSBen Woods explicit_bzero(key, sizeof(key)); 1514e4b0a90eSBrooks Davis } 1515e4b0a90eSBrooks Davis 1516e4b0a90eSBrooks Davis static int 1517e4b0a90eSBrooks Davis eli_trash_metadata(struct gctl_req *req, const char *prov, int fd, off_t offset) 1518e4b0a90eSBrooks Davis { 1519e4b0a90eSBrooks Davis unsigned int overwrites; 1520e4b0a90eSBrooks Davis unsigned char *sector; 1521e4b0a90eSBrooks Davis ssize_t size; 1522e4b0a90eSBrooks Davis int error; 1523e4b0a90eSBrooks Davis 1524e4b0a90eSBrooks Davis size = sizeof(overwrites); 1525e4b0a90eSBrooks Davis if (sysctlbyname("kern.geom.eli.overwrites", &overwrites, &size, 1526e4b0a90eSBrooks Davis NULL, 0) == -1 || overwrites == 0) { 1527e4b0a90eSBrooks Davis overwrites = G_ELI_OVERWRITES; 1528e4b0a90eSBrooks Davis } 1529e4b0a90eSBrooks Davis 1530e4b0a90eSBrooks Davis size = g_sectorsize(fd); 1531e4b0a90eSBrooks Davis if (size <= 0) { 1532e4b0a90eSBrooks Davis gctl_error(req, "Cannot obtain provider sector size %s: %s.", 1533e4b0a90eSBrooks Davis prov, strerror(errno)); 1534e4b0a90eSBrooks Davis return (-1); 1535e4b0a90eSBrooks Davis } 1536e4b0a90eSBrooks Davis sector = malloc(size); 1537e4b0a90eSBrooks Davis if (sector == NULL) { 1538e4b0a90eSBrooks Davis gctl_error(req, "Cannot allocate %zd bytes of memory.", size); 1539e4b0a90eSBrooks Davis return (-1); 1540e4b0a90eSBrooks Davis } 1541e4b0a90eSBrooks Davis 1542e4b0a90eSBrooks Davis error = 0; 1543e4b0a90eSBrooks Davis do { 1544e4b0a90eSBrooks Davis arc4random_buf(sector, size); 1545e4b0a90eSBrooks Davis if (pwrite(fd, sector, size, offset) != size) { 1546e4b0a90eSBrooks Davis if (error == 0) 1547e4b0a90eSBrooks Davis error = errno; 1548e4b0a90eSBrooks Davis } 1549e4b0a90eSBrooks Davis (void)g_flush(fd); 1550e4b0a90eSBrooks Davis } while (--overwrites > 0); 1551e4b0a90eSBrooks Davis free(sector); 1552e4b0a90eSBrooks Davis if (error != 0) { 1553e4b0a90eSBrooks Davis gctl_error(req, "Cannot trash metadata on provider %s: %s.", 1554e4b0a90eSBrooks Davis prov, strerror(error)); 1555e4b0a90eSBrooks Davis return (-1); 1556e4b0a90eSBrooks Davis } 1557e4b0a90eSBrooks Davis return (0); 1558e4b0a90eSBrooks Davis } 1559e4b0a90eSBrooks Davis 1560e4b0a90eSBrooks Davis static void 1561e4b0a90eSBrooks Davis eli_kill_detached(struct gctl_req *req, const char *prov) 1562e4b0a90eSBrooks Davis { 1563e4b0a90eSBrooks Davis off_t offset; 1564e4b0a90eSBrooks Davis int fd; 1565e4b0a90eSBrooks Davis 1566e4b0a90eSBrooks Davis /* 1567e4b0a90eSBrooks Davis * NOTE: Maybe we should verify if this is geli provider first, 1568e4b0a90eSBrooks Davis * but 'kill' command is quite critical so better don't waste 1569e4b0a90eSBrooks Davis * the time. 1570e4b0a90eSBrooks Davis */ 1571e4b0a90eSBrooks Davis #if 0 1572e4b0a90eSBrooks Davis error = g_metadata_read(prov, (unsigned char *)&md, sizeof(md), 1573e4b0a90eSBrooks Davis G_ELI_MAGIC); 1574e4b0a90eSBrooks Davis if (error != 0) { 1575e4b0a90eSBrooks Davis gctl_error(req, "Cannot read metadata from %s: %s.", prov, 1576e4b0a90eSBrooks Davis strerror(error)); 1577e4b0a90eSBrooks Davis return; 1578e4b0a90eSBrooks Davis } 1579e4b0a90eSBrooks Davis #endif 1580e4b0a90eSBrooks Davis 1581e4b0a90eSBrooks Davis fd = g_open(prov, 1); 1582e4b0a90eSBrooks Davis if (fd == -1) { 1583e4b0a90eSBrooks Davis gctl_error(req, "Cannot open provider %s: %s.", prov, 1584e4b0a90eSBrooks Davis strerror(errno)); 1585e4b0a90eSBrooks Davis return; 1586e4b0a90eSBrooks Davis } 1587e4b0a90eSBrooks Davis offset = g_mediasize(fd) - g_sectorsize(fd); 1588e4b0a90eSBrooks Davis if (offset <= 0) { 1589e4b0a90eSBrooks Davis gctl_error(req, 1590e4b0a90eSBrooks Davis "Cannot obtain media size or sector size for provider %s: %s.", 1591e4b0a90eSBrooks Davis prov, strerror(errno)); 1592e4b0a90eSBrooks Davis (void)g_close(fd); 1593e4b0a90eSBrooks Davis return; 1594e4b0a90eSBrooks Davis } 1595e4b0a90eSBrooks Davis (void)eli_trash_metadata(req, prov, fd, offset); 1596e4b0a90eSBrooks Davis (void)g_close(fd); 1597e4b0a90eSBrooks Davis } 1598e4b0a90eSBrooks Davis 1599e4b0a90eSBrooks Davis static void 1600e4b0a90eSBrooks Davis eli_kill(struct gctl_req *req) 1601e4b0a90eSBrooks Davis { 1602e4b0a90eSBrooks Davis const char *prov; 1603e4b0a90eSBrooks Davis int i, nargs, all; 1604e4b0a90eSBrooks Davis 1605e4b0a90eSBrooks Davis nargs = gctl_get_int(req, "nargs"); 1606e4b0a90eSBrooks Davis all = gctl_get_int(req, "all"); 1607e4b0a90eSBrooks Davis if (!all && nargs == 0) { 1608e4b0a90eSBrooks Davis gctl_error(req, "Too few arguments."); 1609e4b0a90eSBrooks Davis return; 1610e4b0a90eSBrooks Davis } 1611e4b0a90eSBrooks Davis /* 1612e4b0a90eSBrooks Davis * How '-a' option combine with a list of providers: 1613e4b0a90eSBrooks Davis * Delete Master Keys from all attached providers: 1614e4b0a90eSBrooks Davis * geli kill -a 1615e4b0a90eSBrooks Davis * Delete Master Keys from all attached providers and from 1616e4b0a90eSBrooks Davis * detached da0 and da1: 1617e4b0a90eSBrooks Davis * geli kill -a da0 da1 1618e4b0a90eSBrooks Davis * Delete Master Keys from (attached or detached) da0 and da1: 1619e4b0a90eSBrooks Davis * geli kill da0 da1 1620e4b0a90eSBrooks Davis */ 1621e4b0a90eSBrooks Davis 1622e4b0a90eSBrooks Davis /* First detached providers. */ 1623e4b0a90eSBrooks Davis for (i = 0; i < nargs; i++) { 1624e4b0a90eSBrooks Davis prov = gctl_get_ascii(req, "arg%d", i); 1625e4b0a90eSBrooks Davis if (!eli_is_attached(prov)) 1626e4b0a90eSBrooks Davis eli_kill_detached(req, prov); 1627e4b0a90eSBrooks Davis } 1628e4b0a90eSBrooks Davis /* Now attached providers. */ 1629e4b0a90eSBrooks Davis gctl_issue(req); 1630e4b0a90eSBrooks Davis } 1631e4b0a90eSBrooks Davis 1632e4b0a90eSBrooks Davis static int 1633e4b0a90eSBrooks Davis eli_backup_create(struct gctl_req *req, const char *prov, const char *file) 1634e4b0a90eSBrooks Davis { 1635e4b0a90eSBrooks Davis unsigned char *sector; 1636e4b0a90eSBrooks Davis ssize_t secsize; 1637e4b0a90eSBrooks Davis int error, filefd, ret; 1638e4b0a90eSBrooks Davis 1639e4b0a90eSBrooks Davis ret = -1; 1640e4b0a90eSBrooks Davis filefd = -1; 1641e4b0a90eSBrooks Davis sector = NULL; 1642e4b0a90eSBrooks Davis secsize = 0; 1643e4b0a90eSBrooks Davis 1644e4b0a90eSBrooks Davis secsize = g_get_sectorsize(prov); 1645e4b0a90eSBrooks Davis if (secsize == 0) { 1646e4b0a90eSBrooks Davis gctl_error(req, "Cannot get informations about %s: %s.", prov, 1647e4b0a90eSBrooks Davis strerror(errno)); 1648e4b0a90eSBrooks Davis goto out; 1649e4b0a90eSBrooks Davis } 1650e4b0a90eSBrooks Davis sector = malloc(secsize); 1651e4b0a90eSBrooks Davis if (sector == NULL) { 1652e4b0a90eSBrooks Davis gctl_error(req, "Cannot allocate memory."); 1653e4b0a90eSBrooks Davis goto out; 1654e4b0a90eSBrooks Davis } 1655e4b0a90eSBrooks Davis /* Read metadata from the provider. */ 1656e4b0a90eSBrooks Davis error = g_metadata_read(prov, sector, secsize, G_ELI_MAGIC); 1657e4b0a90eSBrooks Davis if (error != 0) { 1658e4b0a90eSBrooks Davis gctl_error(req, "Unable to read metadata from %s: %s.", prov, 1659e4b0a90eSBrooks Davis strerror(error)); 1660e4b0a90eSBrooks Davis goto out; 1661e4b0a90eSBrooks Davis } 1662e4b0a90eSBrooks Davis 1663e4b0a90eSBrooks Davis filefd = open(file, O_WRONLY | O_TRUNC | O_CREAT, 0600); 1664e4b0a90eSBrooks Davis if (filefd == -1) { 1665e4b0a90eSBrooks Davis gctl_error(req, "Unable to open %s: %s.", file, 1666e4b0a90eSBrooks Davis strerror(errno)); 1667e4b0a90eSBrooks Davis goto out; 1668e4b0a90eSBrooks Davis } 1669e4b0a90eSBrooks Davis /* Write metadata to the destination file. */ 1670e4b0a90eSBrooks Davis if (write(filefd, sector, secsize) != secsize) { 1671e4b0a90eSBrooks Davis gctl_error(req, "Unable to write to %s: %s.", file, 1672e4b0a90eSBrooks Davis strerror(errno)); 1673e4b0a90eSBrooks Davis (void)close(filefd); 1674e4b0a90eSBrooks Davis (void)unlink(file); 1675e4b0a90eSBrooks Davis goto out; 1676e4b0a90eSBrooks Davis } 1677e4b0a90eSBrooks Davis (void)fsync(filefd); 1678e4b0a90eSBrooks Davis (void)close(filefd); 1679e4b0a90eSBrooks Davis /* Success. */ 1680e4b0a90eSBrooks Davis ret = 0; 1681e4b0a90eSBrooks Davis out: 1682e4b0a90eSBrooks Davis if (sector != NULL) { 1683217df2daSBen Woods explicit_bzero(sector, secsize); 1684e4b0a90eSBrooks Davis free(sector); 1685e4b0a90eSBrooks Davis } 1686e4b0a90eSBrooks Davis return (ret); 1687e4b0a90eSBrooks Davis } 1688e4b0a90eSBrooks Davis 1689e4b0a90eSBrooks Davis static void 1690e4b0a90eSBrooks Davis eli_backup(struct gctl_req *req) 1691e4b0a90eSBrooks Davis { 1692e4b0a90eSBrooks Davis const char *file, *prov; 1693e4b0a90eSBrooks Davis int nargs; 1694e4b0a90eSBrooks Davis 1695e4b0a90eSBrooks Davis nargs = gctl_get_int(req, "nargs"); 1696e4b0a90eSBrooks Davis if (nargs != 2) { 1697e4b0a90eSBrooks Davis gctl_error(req, "Invalid number of arguments."); 1698e4b0a90eSBrooks Davis return; 1699e4b0a90eSBrooks Davis } 1700e4b0a90eSBrooks Davis prov = gctl_get_ascii(req, "arg0"); 1701e4b0a90eSBrooks Davis file = gctl_get_ascii(req, "arg1"); 1702e4b0a90eSBrooks Davis 1703e4b0a90eSBrooks Davis eli_backup_create(req, prov, file); 1704e4b0a90eSBrooks Davis } 1705e4b0a90eSBrooks Davis 1706e4b0a90eSBrooks Davis static void 1707e4b0a90eSBrooks Davis eli_restore(struct gctl_req *req) 1708e4b0a90eSBrooks Davis { 1709e4b0a90eSBrooks Davis struct g_eli_metadata md; 1710e4b0a90eSBrooks Davis const char *file, *prov; 1711e4b0a90eSBrooks Davis off_t mediasize; 1712e4b0a90eSBrooks Davis int nargs; 1713e4b0a90eSBrooks Davis 1714e4b0a90eSBrooks Davis nargs = gctl_get_int(req, "nargs"); 1715e4b0a90eSBrooks Davis if (nargs != 2) { 1716e4b0a90eSBrooks Davis gctl_error(req, "Invalid number of arguments."); 1717e4b0a90eSBrooks Davis return; 1718e4b0a90eSBrooks Davis } 1719e4b0a90eSBrooks Davis file = gctl_get_ascii(req, "arg0"); 1720e4b0a90eSBrooks Davis prov = gctl_get_ascii(req, "arg1"); 1721e4b0a90eSBrooks Davis 1722e4b0a90eSBrooks Davis /* Read metadata from the backup file. */ 1723e4b0a90eSBrooks Davis if (eli_metadata_read(req, file, &md) == -1) 1724e4b0a90eSBrooks Davis return; 1725e4b0a90eSBrooks Davis /* Obtain provider's mediasize. */ 1726e4b0a90eSBrooks Davis mediasize = g_get_mediasize(prov); 1727e4b0a90eSBrooks Davis if (mediasize == 0) { 1728e4b0a90eSBrooks Davis gctl_error(req, "Cannot get informations about %s: %s.", prov, 1729e4b0a90eSBrooks Davis strerror(errno)); 1730e4b0a90eSBrooks Davis return; 1731e4b0a90eSBrooks Davis } 1732e4b0a90eSBrooks Davis /* Check if the provider size has changed since we did the backup. */ 1733e4b0a90eSBrooks Davis if (md.md_provsize != (uint64_t)mediasize) { 1734e4b0a90eSBrooks Davis if (gctl_get_int(req, "force")) { 1735e4b0a90eSBrooks Davis md.md_provsize = mediasize; 1736e4b0a90eSBrooks Davis } else { 1737e4b0a90eSBrooks Davis gctl_error(req, "Provider size mismatch: " 1738e4b0a90eSBrooks Davis "wrong backup file?"); 1739e4b0a90eSBrooks Davis return; 1740e4b0a90eSBrooks Davis } 1741e4b0a90eSBrooks Davis } 1742e4b0a90eSBrooks Davis /* Write metadata to the provider. */ 1743e4b0a90eSBrooks Davis (void)eli_metadata_store(req, prov, &md); 1744e4b0a90eSBrooks Davis } 1745e4b0a90eSBrooks Davis 1746e4b0a90eSBrooks Davis static void 1747e4b0a90eSBrooks Davis eli_resize(struct gctl_req *req) 1748e4b0a90eSBrooks Davis { 1749e4b0a90eSBrooks Davis struct g_eli_metadata md; 1750e4b0a90eSBrooks Davis const char *prov; 1751e4b0a90eSBrooks Davis unsigned char *sector; 1752e4b0a90eSBrooks Davis ssize_t secsize; 1753e4b0a90eSBrooks Davis off_t mediasize, oldsize; 1754e4b0a90eSBrooks Davis int error, nargs, provfd; 1755e4b0a90eSBrooks Davis 1756e4b0a90eSBrooks Davis nargs = gctl_get_int(req, "nargs"); 1757e4b0a90eSBrooks Davis if (nargs != 1) { 1758e4b0a90eSBrooks Davis gctl_error(req, "Invalid number of arguments."); 1759e4b0a90eSBrooks Davis return; 1760e4b0a90eSBrooks Davis } 1761e4b0a90eSBrooks Davis prov = gctl_get_ascii(req, "arg0"); 1762e4b0a90eSBrooks Davis 1763e4b0a90eSBrooks Davis provfd = -1; 1764e4b0a90eSBrooks Davis sector = NULL; 1765e4b0a90eSBrooks Davis secsize = 0; 1766e4b0a90eSBrooks Davis 1767e4b0a90eSBrooks Davis provfd = g_open(prov, 1); 1768e4b0a90eSBrooks Davis if (provfd == -1) { 1769e4b0a90eSBrooks Davis gctl_error(req, "Cannot open %s: %s.", prov, strerror(errno)); 1770e4b0a90eSBrooks Davis goto out; 1771e4b0a90eSBrooks Davis } 1772e4b0a90eSBrooks Davis 1773e4b0a90eSBrooks Davis mediasize = g_mediasize(provfd); 1774e4b0a90eSBrooks Davis secsize = g_sectorsize(provfd); 1775e4b0a90eSBrooks Davis if (mediasize == -1 || secsize == -1) { 1776e4b0a90eSBrooks Davis gctl_error(req, "Cannot get information about %s: %s.", prov, 1777e4b0a90eSBrooks Davis strerror(errno)); 1778e4b0a90eSBrooks Davis goto out; 1779e4b0a90eSBrooks Davis } 1780e4b0a90eSBrooks Davis 1781e4b0a90eSBrooks Davis sector = malloc(secsize); 1782e4b0a90eSBrooks Davis if (sector == NULL) { 1783e4b0a90eSBrooks Davis gctl_error(req, "Cannot allocate memory."); 1784e4b0a90eSBrooks Davis goto out; 1785e4b0a90eSBrooks Davis } 1786e4b0a90eSBrooks Davis 1787e4b0a90eSBrooks Davis oldsize = gctl_get_intmax(req, "oldsize"); 1788e4b0a90eSBrooks Davis if (oldsize < 0 || oldsize > mediasize) { 1789e4b0a90eSBrooks Davis gctl_error(req, "Invalid oldsize: Out of range."); 1790e4b0a90eSBrooks Davis goto out; 1791e4b0a90eSBrooks Davis } 1792e4b0a90eSBrooks Davis if (oldsize == mediasize) { 1793e4b0a90eSBrooks Davis gctl_error(req, "Size hasn't changed."); 1794e4b0a90eSBrooks Davis goto out; 1795e4b0a90eSBrooks Davis } 1796e4b0a90eSBrooks Davis 1797e4b0a90eSBrooks Davis /* Read metadata from the 'oldsize' offset. */ 1798e4b0a90eSBrooks Davis if (pread(provfd, sector, secsize, oldsize - secsize) != secsize) { 1799e4b0a90eSBrooks Davis gctl_error(req, "Cannot read old metadata: %s.", 1800e4b0a90eSBrooks Davis strerror(errno)); 1801e4b0a90eSBrooks Davis goto out; 1802e4b0a90eSBrooks Davis } 1803e4b0a90eSBrooks Davis 1804e4b0a90eSBrooks Davis /* Check if this sector contains geli metadata. */ 1805e4b0a90eSBrooks Davis error = eli_metadata_decode(sector, &md); 1806e4b0a90eSBrooks Davis switch (error) { 1807e4b0a90eSBrooks Davis case 0: 1808e4b0a90eSBrooks Davis break; 1809e4b0a90eSBrooks Davis case EOPNOTSUPP: 1810e4b0a90eSBrooks Davis gctl_error(req, 1811e4b0a90eSBrooks Davis "Provider's %s metadata version %u is too new.\n" 1812e4b0a90eSBrooks Davis "geli: The highest supported version is %u.", 1813e4b0a90eSBrooks Davis prov, (unsigned int)md.md_version, G_ELI_VERSION); 1814e4b0a90eSBrooks Davis goto out; 1815e4b0a90eSBrooks Davis case EINVAL: 1816e4b0a90eSBrooks Davis gctl_error(req, "Inconsistent provider's %s metadata.", prov); 1817e4b0a90eSBrooks Davis goto out; 1818e4b0a90eSBrooks Davis default: 1819e4b0a90eSBrooks Davis gctl_error(req, 1820e4b0a90eSBrooks Davis "Unexpected error while decoding provider's %s metadata: %s.", 1821e4b0a90eSBrooks Davis prov, strerror(error)); 1822e4b0a90eSBrooks Davis goto out; 1823e4b0a90eSBrooks Davis } 1824e4b0a90eSBrooks Davis 1825e4b0a90eSBrooks Davis /* 1826e4b0a90eSBrooks Davis * If the old metadata doesn't have a correct provider size, refuse 1827e4b0a90eSBrooks Davis * to resize. 1828e4b0a90eSBrooks Davis */ 1829e4b0a90eSBrooks Davis if (md.md_provsize != (uint64_t)oldsize) { 1830e4b0a90eSBrooks Davis gctl_error(req, "Provider size mismatch at oldsize."); 1831e4b0a90eSBrooks Davis goto out; 1832e4b0a90eSBrooks Davis } 1833e4b0a90eSBrooks Davis 1834e4b0a90eSBrooks Davis /* 1835e4b0a90eSBrooks Davis * Update the old metadata with the current provider size and write 1836e4b0a90eSBrooks Davis * it back to the correct place on the provider. 1837e4b0a90eSBrooks Davis */ 1838e4b0a90eSBrooks Davis md.md_provsize = mediasize; 1839e4b0a90eSBrooks Davis /* Write metadata to the provider. */ 1840e4b0a90eSBrooks Davis (void)eli_metadata_store(req, prov, &md); 1841e4b0a90eSBrooks Davis /* Now trash the old metadata. */ 1842e4b0a90eSBrooks Davis (void)eli_trash_metadata(req, prov, provfd, oldsize - secsize); 1843e4b0a90eSBrooks Davis out: 1844e4b0a90eSBrooks Davis if (provfd != -1) 1845e4b0a90eSBrooks Davis (void)g_close(provfd); 1846e4b0a90eSBrooks Davis if (sector != NULL) { 1847217df2daSBen Woods explicit_bzero(sector, secsize); 1848e4b0a90eSBrooks Davis free(sector); 1849e4b0a90eSBrooks Davis } 1850e4b0a90eSBrooks Davis } 1851e4b0a90eSBrooks Davis 1852e4b0a90eSBrooks Davis static void 1853e4b0a90eSBrooks Davis eli_version(struct gctl_req *req) 1854e4b0a90eSBrooks Davis { 1855e4b0a90eSBrooks Davis struct g_eli_metadata md; 1856e4b0a90eSBrooks Davis const char *name; 1857e4b0a90eSBrooks Davis unsigned int version; 1858e4b0a90eSBrooks Davis int error, i, nargs; 1859e4b0a90eSBrooks Davis 1860e4b0a90eSBrooks Davis nargs = gctl_get_int(req, "nargs"); 1861e4b0a90eSBrooks Davis 1862e4b0a90eSBrooks Davis if (nargs == 0) { 1863e4b0a90eSBrooks Davis unsigned int kernver; 1864e4b0a90eSBrooks Davis ssize_t size; 1865e4b0a90eSBrooks Davis 1866e4b0a90eSBrooks Davis size = sizeof(kernver); 1867e4b0a90eSBrooks Davis if (sysctlbyname("kern.geom.eli.version", &kernver, &size, 1868e4b0a90eSBrooks Davis NULL, 0) == -1) { 1869e4b0a90eSBrooks Davis warn("Unable to obtain GELI kernel version"); 1870e4b0a90eSBrooks Davis } else { 1871e4b0a90eSBrooks Davis printf("kernel: %u\n", kernver); 1872e4b0a90eSBrooks Davis } 1873e4b0a90eSBrooks Davis printf("userland: %u\n", G_ELI_VERSION); 1874e4b0a90eSBrooks Davis return; 1875e4b0a90eSBrooks Davis } 1876e4b0a90eSBrooks Davis 1877e4b0a90eSBrooks Davis for (i = 0; i < nargs; i++) { 1878e4b0a90eSBrooks Davis name = gctl_get_ascii(req, "arg%d", i); 1879e4b0a90eSBrooks Davis error = g_metadata_read(name, (unsigned char *)&md, 1880e4b0a90eSBrooks Davis sizeof(md), G_ELI_MAGIC); 1881e4b0a90eSBrooks Davis if (error != 0) { 1882e4b0a90eSBrooks Davis warn("%s: Unable to read metadata: %s.", name, 1883e4b0a90eSBrooks Davis strerror(error)); 1884e4b0a90eSBrooks Davis gctl_error(req, "Not fully done."); 1885e4b0a90eSBrooks Davis continue; 1886e4b0a90eSBrooks Davis } 1887e4b0a90eSBrooks Davis version = le32dec(&md.md_version); 1888e4b0a90eSBrooks Davis printf("%s: %u\n", name, version); 1889e4b0a90eSBrooks Davis } 1890e4b0a90eSBrooks Davis } 1891e4b0a90eSBrooks Davis 1892e4b0a90eSBrooks Davis static void 1893e4b0a90eSBrooks Davis eli_clear(struct gctl_req *req) 1894e4b0a90eSBrooks Davis { 1895e4b0a90eSBrooks Davis const char *name; 1896e4b0a90eSBrooks Davis int error, i, nargs; 1897e4b0a90eSBrooks Davis 1898e4b0a90eSBrooks Davis nargs = gctl_get_int(req, "nargs"); 1899e4b0a90eSBrooks Davis if (nargs < 1) { 1900e4b0a90eSBrooks Davis gctl_error(req, "Too few arguments."); 1901e4b0a90eSBrooks Davis return; 1902e4b0a90eSBrooks Davis } 1903e4b0a90eSBrooks Davis 1904e4b0a90eSBrooks Davis for (i = 0; i < nargs; i++) { 1905e4b0a90eSBrooks Davis name = gctl_get_ascii(req, "arg%d", i); 1906e4b0a90eSBrooks Davis error = g_metadata_clear(name, G_ELI_MAGIC); 1907e4b0a90eSBrooks Davis if (error != 0) { 1908e4b0a90eSBrooks Davis fprintf(stderr, "Cannot clear metadata on %s: %s.\n", 1909e4b0a90eSBrooks Davis name, strerror(error)); 1910e4b0a90eSBrooks Davis gctl_error(req, "Not fully done."); 1911e4b0a90eSBrooks Davis continue; 1912e4b0a90eSBrooks Davis } 1913e4b0a90eSBrooks Davis if (verbose) 1914e4b0a90eSBrooks Davis printf("Metadata cleared on %s.\n", name); 1915e4b0a90eSBrooks Davis } 1916e4b0a90eSBrooks Davis } 1917e4b0a90eSBrooks Davis 1918e4b0a90eSBrooks Davis static void 1919e4b0a90eSBrooks Davis eli_dump(struct gctl_req *req) 1920e4b0a90eSBrooks Davis { 1921e4b0a90eSBrooks Davis struct g_eli_metadata md; 1922e4b0a90eSBrooks Davis const char *name; 1923e4b0a90eSBrooks Davis int i, nargs; 1924e4b0a90eSBrooks Davis 1925e4b0a90eSBrooks Davis nargs = gctl_get_int(req, "nargs"); 1926e4b0a90eSBrooks Davis if (nargs < 1) { 1927e4b0a90eSBrooks Davis gctl_error(req, "Too few arguments."); 1928e4b0a90eSBrooks Davis return; 1929e4b0a90eSBrooks Davis } 1930e4b0a90eSBrooks Davis 1931e4b0a90eSBrooks Davis for (i = 0; i < nargs; i++) { 1932e4b0a90eSBrooks Davis name = gctl_get_ascii(req, "arg%d", i); 1933e4b0a90eSBrooks Davis if (eli_metadata_read(NULL, name, &md) == -1) { 1934e4b0a90eSBrooks Davis gctl_error(req, "Not fully done."); 1935e4b0a90eSBrooks Davis continue; 1936e4b0a90eSBrooks Davis } 1937e4b0a90eSBrooks Davis printf("Metadata on %s:\n", name); 1938e4b0a90eSBrooks Davis eli_metadata_dump(&md); 1939e4b0a90eSBrooks Davis printf("\n"); 1940e4b0a90eSBrooks Davis } 1941e4b0a90eSBrooks Davis } 1942