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