1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2004-2019 Pawel Jakub Dawidek <pawel@dawidek.net> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 #include <sys/param.h> 31 #include <sys/mman.h> 32 #include <sys/sysctl.h> 33 #include <sys/resource.h> 34 #include <opencrypto/cryptodev.h> 35 36 #include <assert.h> 37 #include <err.h> 38 #include <errno.h> 39 #include <fcntl.h> 40 #include <libgeom.h> 41 #include <paths.h> 42 #include <readpassphrase.h> 43 #include <stdbool.h> 44 #include <stdint.h> 45 #include <stdio.h> 46 #include <stdlib.h> 47 #include <string.h> 48 #include <strings.h> 49 #include <unistd.h> 50 51 #include <geom/eli/g_eli.h> 52 #include <geom/eli/pkcs5v2.h> 53 54 #include "core/geom.h" 55 #include "misc/subr.h" 56 57 58 uint32_t lib_version = G_LIB_VERSION; 59 uint32_t version = G_ELI_VERSION; 60 61 #define GELI_BACKUP_DIR "/var/backups/" 62 #define GELI_ENC_ALGO "aes" 63 #define BUFSIZE 1024 64 65 /* 66 * Passphrase cached when attaching multiple providers, in order to be more 67 * user-friendly if they are using the same passphrase. 68 */ 69 static char cached_passphrase[BUFSIZE] = ""; 70 71 static void eli_main(struct gctl_req *req, unsigned flags); 72 static void eli_init(struct gctl_req *req); 73 static void eli_attach(struct gctl_req *req); 74 static void eli_configure(struct gctl_req *req); 75 static void eli_setkey(struct gctl_req *req); 76 static void eli_delkey(struct gctl_req *req); 77 static void eli_resume(struct gctl_req *req); 78 static void eli_kill(struct gctl_req *req); 79 static void eli_backup(struct gctl_req *req); 80 static void eli_restore(struct gctl_req *req); 81 static void eli_resize(struct gctl_req *req); 82 static void eli_version(struct gctl_req *req); 83 static void eli_clear(struct gctl_req *req); 84 static void eli_dump(struct gctl_req *req); 85 86 static int eli_backup_create(struct gctl_req *req, const char *prov, 87 const char *file); 88 89 /* 90 * Available commands: 91 * 92 * init [-bdgPRTv] [-a aalgo] [-B backupfile] [-e ealgo] [-i iterations] [-l keylen] [-J newpassfile] [-K newkeyfile] [-s sectorsize] [-V version] prov ... 93 * label - alias for 'init' 94 * attach [-Cdprv] [-n keyno] [-j passfile] [-k keyfile] prov ... 95 * detach [-fl] prov ... 96 * stop - alias for 'detach' 97 * onetime [-dRT] [-a aalgo] [-e ealgo] [-l keylen] prov 98 * configure [-bBgGrRtT] prov ... 99 * setkey [-pPv] [-n keyno] [-j passfile] [-J newpassfile] [-k keyfile] [-K newkeyfile] prov 100 * delkey [-afv] [-n keyno] prov 101 * suspend [-v] -a | prov ... 102 * resume [-pv] [-j passfile] [-k keyfile] prov 103 * kill [-av] [prov ...] 104 * backup [-v] prov file 105 * restore [-fv] file prov 106 * resize [-v] -s oldsize prov 107 * version [prov ...] 108 * clear [-v] prov ... 109 * dump [-v] prov ... 110 */ 111 struct g_command class_commands[] = { 112 { "init", G_FLAG_VERBOSE, eli_main, 113 { 114 { 'a', "aalgo", "", G_TYPE_STRING }, 115 { 'b', "boot", NULL, G_TYPE_BOOL }, 116 { 'B', "backupfile", "", G_TYPE_STRING }, 117 { 'd', "displaypass", NULL, G_TYPE_BOOL }, 118 { 'e', "ealgo", "", G_TYPE_STRING }, 119 { 'g', "geliboot", NULL, G_TYPE_BOOL }, 120 { 'i', "iterations", "-1", G_TYPE_NUMBER }, 121 { 'J', "newpassfile", G_VAL_OPTIONAL, G_TYPE_STRING | G_TYPE_MULTI }, 122 { 'K', "newkeyfile", G_VAL_OPTIONAL, G_TYPE_STRING | G_TYPE_MULTI }, 123 { 'l', "keylen", "0", G_TYPE_NUMBER }, 124 { 'P', "nonewpassphrase", NULL, G_TYPE_BOOL }, 125 { 'R', "noautoresize", NULL, G_TYPE_BOOL }, 126 { 's', "sectorsize", "0", G_TYPE_NUMBER }, 127 { 'T', "notrim", NULL, G_TYPE_BOOL }, 128 { 'V', "mdversion", "-1", G_TYPE_NUMBER }, 129 G_OPT_SENTINEL 130 }, 131 "[-bdgPRTv] [-a aalgo] [-B backupfile] [-e ealgo] [-i iterations] [-l keylen] [-J newpassfile] [-K newkeyfile] [-s sectorsize] [-V version] prov ..." 132 }, 133 { "label", G_FLAG_VERBOSE, eli_main, 134 { 135 { 'a', "aalgo", "", G_TYPE_STRING }, 136 { 'b', "boot", NULL, G_TYPE_BOOL }, 137 { 'B', "backupfile", "", G_TYPE_STRING }, 138 { 'd', "displaypass", NULL, G_TYPE_BOOL }, 139 { 'e', "ealgo", "", G_TYPE_STRING }, 140 { 'g', "geliboot", NULL, G_TYPE_BOOL }, 141 { 'i', "iterations", "-1", G_TYPE_NUMBER }, 142 { 'J', "newpassfile", G_VAL_OPTIONAL, G_TYPE_STRING | G_TYPE_MULTI }, 143 { 'K', "newkeyfile", G_VAL_OPTIONAL, G_TYPE_STRING | G_TYPE_MULTI }, 144 { 'l', "keylen", "0", G_TYPE_NUMBER }, 145 { 'P', "nonewpassphrase", NULL, G_TYPE_BOOL }, 146 { 'R', "noautoresize", NULL, G_TYPE_BOOL }, 147 { 's', "sectorsize", "0", G_TYPE_NUMBER }, 148 { 'T', "notrim", NULL, G_TYPE_BOOL }, 149 { 'V', "mdversion", "-1", G_TYPE_NUMBER }, 150 G_OPT_SENTINEL 151 }, 152 "- an alias for 'init'" 153 }, 154 { "attach", G_FLAG_VERBOSE | G_FLAG_LOADKLD, eli_main, 155 { 156 { 'C', "dryrun", NULL, G_TYPE_BOOL }, 157 { 'd', "detach", NULL, G_TYPE_BOOL }, 158 { 'j', "passfile", G_VAL_OPTIONAL, G_TYPE_STRING | G_TYPE_MULTI }, 159 { 'k', "keyfile", G_VAL_OPTIONAL, G_TYPE_STRING | G_TYPE_MULTI }, 160 { 'n', "keyno", "-1", G_TYPE_NUMBER }, 161 { 'p', "nopassphrase", NULL, G_TYPE_BOOL }, 162 { 'r', "readonly", NULL, G_TYPE_BOOL }, 163 G_OPT_SENTINEL 164 }, 165 "[-Cdprv] [-n keyno] [-j passfile] [-k keyfile] prov ..." 166 }, 167 { "detach", 0, NULL, 168 { 169 { 'f', "force", NULL, G_TYPE_BOOL }, 170 { 'l', "last", NULL, G_TYPE_BOOL }, 171 G_OPT_SENTINEL 172 }, 173 "[-fl] prov ..." 174 }, 175 { "stop", 0, NULL, 176 { 177 { 'f', "force", NULL, G_TYPE_BOOL }, 178 { 'l', "last", NULL, G_TYPE_BOOL }, 179 G_OPT_SENTINEL 180 }, 181 "- an alias for 'detach'" 182 }, 183 { "onetime", G_FLAG_VERBOSE | G_FLAG_LOADKLD, NULL, 184 { 185 { 'a', "aalgo", "", G_TYPE_STRING }, 186 { 'd', "detach", NULL, G_TYPE_BOOL }, 187 { 'e', "ealgo", GELI_ENC_ALGO, G_TYPE_STRING }, 188 { 'l', "keylen", "0", G_TYPE_NUMBER }, 189 { 'R', "noautoresize", NULL, G_TYPE_BOOL }, 190 { 's', "sectorsize", "0", G_TYPE_NUMBER }, 191 { 'T', "notrim", NULL, G_TYPE_BOOL }, 192 G_OPT_SENTINEL 193 }, 194 "[-dRT] [-a aalgo] [-e ealgo] [-l keylen] [-s sectorsize] prov" 195 }, 196 { "configure", G_FLAG_VERBOSE, eli_main, 197 { 198 { 'b', "boot", NULL, G_TYPE_BOOL }, 199 { 'B', "noboot", NULL, G_TYPE_BOOL }, 200 { 'd', "displaypass", NULL, G_TYPE_BOOL }, 201 { 'D', "nodisplaypass", NULL, G_TYPE_BOOL }, 202 { 'g', "geliboot", NULL, G_TYPE_BOOL }, 203 { 'G', "nogeliboot", NULL, G_TYPE_BOOL }, 204 { 'r', "autoresize", NULL, G_TYPE_BOOL }, 205 { 'R', "noautoresize", NULL, G_TYPE_BOOL }, 206 { 't', "trim", NULL, G_TYPE_BOOL }, 207 { 'T', "notrim", NULL, G_TYPE_BOOL }, 208 G_OPT_SENTINEL 209 }, 210 "[-bBdDgGrRtT] prov ..." 211 }, 212 { "setkey", G_FLAG_VERBOSE, eli_main, 213 { 214 { 'i', "iterations", "-1", G_TYPE_NUMBER }, 215 { 'j', "passfile", G_VAL_OPTIONAL, G_TYPE_STRING | G_TYPE_MULTI }, 216 { 'J', "newpassfile", G_VAL_OPTIONAL, G_TYPE_STRING | G_TYPE_MULTI }, 217 { 'k', "keyfile", G_VAL_OPTIONAL, G_TYPE_STRING | G_TYPE_MULTI }, 218 { 'K', "newkeyfile", G_VAL_OPTIONAL, G_TYPE_STRING | G_TYPE_MULTI }, 219 { 'n', "keyno", "-1", G_TYPE_NUMBER }, 220 { 'p', "nopassphrase", NULL, G_TYPE_BOOL }, 221 { 'P', "nonewpassphrase", NULL, G_TYPE_BOOL }, 222 G_OPT_SENTINEL 223 }, 224 "[-pPv] [-n keyno] [-i iterations] [-j passfile] [-J newpassfile] [-k keyfile] [-K newkeyfile] prov" 225 }, 226 { "delkey", G_FLAG_VERBOSE, eli_main, 227 { 228 { 'a', "all", NULL, G_TYPE_BOOL }, 229 { 'f', "force", NULL, G_TYPE_BOOL }, 230 { 'n', "keyno", "-1", G_TYPE_NUMBER }, 231 G_OPT_SENTINEL 232 }, 233 "[-afv] [-n keyno] prov" 234 }, 235 { "suspend", G_FLAG_VERBOSE, NULL, 236 { 237 { 'a', "all", NULL, G_TYPE_BOOL }, 238 G_OPT_SENTINEL 239 }, 240 "[-v] -a | prov ..." 241 }, 242 { "resume", G_FLAG_VERBOSE, eli_main, 243 { 244 { 'j', "passfile", G_VAL_OPTIONAL, G_TYPE_STRING | G_TYPE_MULTI }, 245 { 'k', "keyfile", G_VAL_OPTIONAL, G_TYPE_STRING | G_TYPE_MULTI }, 246 { 'p', "nopassphrase", NULL, G_TYPE_BOOL }, 247 G_OPT_SENTINEL 248 }, 249 "[-pv] [-j passfile] [-k keyfile] prov" 250 }, 251 { "kill", G_FLAG_VERBOSE, eli_main, 252 { 253 { 'a', "all", NULL, G_TYPE_BOOL }, 254 G_OPT_SENTINEL 255 }, 256 "[-av] [prov ...]" 257 }, 258 { "backup", G_FLAG_VERBOSE, eli_main, G_NULL_OPTS, 259 "[-v] prov file" 260 }, 261 { "restore", G_FLAG_VERBOSE, eli_main, 262 { 263 { 'f', "force", NULL, G_TYPE_BOOL }, 264 G_OPT_SENTINEL 265 }, 266 "[-fv] file prov" 267 }, 268 { "resize", G_FLAG_VERBOSE, eli_main, 269 { 270 { 's', "oldsize", NULL, G_TYPE_NUMBER }, 271 G_OPT_SENTINEL 272 }, 273 "[-v] -s oldsize prov" 274 }, 275 { "version", G_FLAG_LOADKLD, eli_main, G_NULL_OPTS, 276 "[prov ...]" 277 }, 278 { "clear", G_FLAG_VERBOSE, eli_main, G_NULL_OPTS, 279 "[-v] prov ..." 280 }, 281 { "dump", G_FLAG_VERBOSE, eli_main, G_NULL_OPTS, 282 "[-v] prov ..." 283 }, 284 G_CMD_SENTINEL 285 }; 286 287 static int verbose = 0; 288 289 static int 290 eli_protect(struct gctl_req *req) 291 { 292 struct rlimit rl; 293 294 /* Disable core dumps. */ 295 rl.rlim_cur = 0; 296 rl.rlim_max = 0; 297 if (setrlimit(RLIMIT_CORE, &rl) == -1) { 298 gctl_error(req, "Cannot disable core dumps: %s.", 299 strerror(errno)); 300 return (-1); 301 } 302 /* Disable swapping. */ 303 if (mlockall(MCL_FUTURE) == -1) { 304 gctl_error(req, "Cannot lock memory: %s.", strerror(errno)); 305 return (-1); 306 } 307 return (0); 308 } 309 310 static void 311 eli_main(struct gctl_req *req, unsigned int flags) 312 { 313 const char *name; 314 315 if (eli_protect(req) == -1) 316 return; 317 318 if ((flags & G_FLAG_VERBOSE) != 0) 319 verbose = 1; 320 321 name = gctl_get_ascii(req, "verb"); 322 if (name == NULL) { 323 gctl_error(req, "No '%s' argument.", "verb"); 324 return; 325 } 326 if (strcmp(name, "init") == 0 || strcmp(name, "label") == 0) 327 eli_init(req); 328 else if (strcmp(name, "attach") == 0) 329 eli_attach(req); 330 else if (strcmp(name, "configure") == 0) 331 eli_configure(req); 332 else if (strcmp(name, "setkey") == 0) 333 eli_setkey(req); 334 else if (strcmp(name, "delkey") == 0) 335 eli_delkey(req); 336 else if (strcmp(name, "resume") == 0) 337 eli_resume(req); 338 else if (strcmp(name, "kill") == 0) 339 eli_kill(req); 340 else if (strcmp(name, "backup") == 0) 341 eli_backup(req); 342 else if (strcmp(name, "restore") == 0) 343 eli_restore(req); 344 else if (strcmp(name, "resize") == 0) 345 eli_resize(req); 346 else if (strcmp(name, "version") == 0) 347 eli_version(req); 348 else if (strcmp(name, "dump") == 0) 349 eli_dump(req); 350 else if (strcmp(name, "clear") == 0) 351 eli_clear(req); 352 else 353 gctl_error(req, "Unknown command: %s.", name); 354 } 355 356 static bool 357 eli_is_attached(const char *prov) 358 { 359 char name[MAXPATHLEN]; 360 361 /* 362 * Not the best way to do it, but the easiest. 363 * We try to open provider and check if it is a GEOM provider 364 * by asking about its sectorsize. 365 */ 366 snprintf(name, sizeof(name), "%s%s", prov, G_ELI_SUFFIX); 367 return (g_get_sectorsize(name) > 0); 368 } 369 370 static int 371 eli_genkey_files(struct gctl_req *req, bool new, const char *type, 372 struct hmac_ctx *ctxp, char *passbuf, size_t passbufsize) 373 { 374 char *p, buf[BUFSIZE], argname[16]; 375 const char *file; 376 int error, fd, i; 377 ssize_t done; 378 379 assert((strcmp(type, "keyfile") == 0 && ctxp != NULL && 380 passbuf == NULL && passbufsize == 0) || 381 (strcmp(type, "passfile") == 0 && ctxp == NULL && 382 passbuf != NULL && passbufsize > 0)); 383 assert(strcmp(type, "keyfile") == 0 || passbuf[0] == '\0'); 384 385 for (i = 0; ; i++) { 386 snprintf(argname, sizeof(argname), "%s%s%d", 387 new ? "new" : "", type, i); 388 389 /* No more {key,pass}files? */ 390 if (!gctl_has_param(req, argname)) 391 return (i); 392 393 file = gctl_get_ascii(req, "%s", argname); 394 assert(file != NULL); 395 396 if (strcmp(file, "-") == 0) 397 fd = STDIN_FILENO; 398 else { 399 fd = open(file, O_RDONLY); 400 if (fd == -1) { 401 gctl_error(req, "Cannot open %s %s: %s.", 402 type, file, strerror(errno)); 403 return (-1); 404 } 405 } 406 if (strcmp(type, "keyfile") == 0) { 407 while ((done = read(fd, buf, sizeof(buf))) > 0) 408 g_eli_crypto_hmac_update(ctxp, buf, done); 409 } else /* if (strcmp(type, "passfile") == 0) */ { 410 assert(strcmp(type, "passfile") == 0); 411 412 while ((done = read(fd, buf, sizeof(buf) - 1)) > 0) { 413 buf[done] = '\0'; 414 p = strchr(buf, '\n'); 415 if (p != NULL) { 416 *p = '\0'; 417 done = p - buf; 418 } 419 if (strlcat(passbuf, buf, passbufsize) >= 420 passbufsize) { 421 gctl_error(req, 422 "Passphrase in %s too long.", file); 423 explicit_bzero(buf, sizeof(buf)); 424 return (-1); 425 } 426 if (p != NULL) 427 break; 428 } 429 } 430 error = errno; 431 if (strcmp(file, "-") != 0) 432 close(fd); 433 explicit_bzero(buf, sizeof(buf)); 434 if (done == -1) { 435 gctl_error(req, "Cannot read %s %s: %s.", 436 type, file, strerror(error)); 437 return (-1); 438 } 439 } 440 /* NOTREACHED */ 441 } 442 443 static int 444 eli_genkey_passphrase_prompt(struct gctl_req *req, bool new, char *passbuf, 445 size_t passbufsize) 446 { 447 char *p; 448 449 for (;;) { 450 p = readpassphrase( 451 new ? "Enter new passphrase: " : "Enter passphrase: ", 452 passbuf, passbufsize, RPP_ECHO_OFF | RPP_REQUIRE_TTY); 453 if (p == NULL) { 454 explicit_bzero(passbuf, passbufsize); 455 gctl_error(req, "Cannot read passphrase: %s.", 456 strerror(errno)); 457 return (-1); 458 } 459 460 if (new) { 461 char tmpbuf[BUFSIZE]; 462 463 p = readpassphrase("Reenter new passphrase: ", 464 tmpbuf, sizeof(tmpbuf), 465 RPP_ECHO_OFF | RPP_REQUIRE_TTY); 466 if (p == NULL) { 467 explicit_bzero(passbuf, passbufsize); 468 gctl_error(req, 469 "Cannot read passphrase: %s.", 470 strerror(errno)); 471 return (-1); 472 } 473 474 if (strcmp(passbuf, tmpbuf) != 0) { 475 explicit_bzero(passbuf, passbufsize); 476 fprintf(stderr, "They didn't match.\n"); 477 continue; 478 } 479 explicit_bzero(tmpbuf, sizeof(tmpbuf)); 480 } 481 return (0); 482 } 483 /* NOTREACHED */ 484 } 485 486 static int 487 eli_genkey_passphrase(struct gctl_req *req, struct g_eli_metadata *md, bool new, 488 struct hmac_ctx *ctxp) 489 { 490 char passbuf[BUFSIZE]; 491 bool nopassphrase; 492 int nfiles; 493 494 /* 495 * Return error if the 'do not use passphrase' flag was given but a 496 * passfile was provided. 497 */ 498 nopassphrase = 499 gctl_get_int(req, new ? "nonewpassphrase" : "nopassphrase"); 500 if (nopassphrase) { 501 if (gctl_has_param(req, new ? "newpassfile0" : "passfile0")) { 502 gctl_error(req, 503 "Options -%c and -%c are mutually exclusive.", 504 new ? 'J' : 'j', new ? 'P' : 'p'); 505 return (-1); 506 } 507 return (0); 508 } 509 510 /* 511 * Return error if using a provider which does not require a passphrase 512 * but the 'do not use passphrase' flag was not given. 513 */ 514 if (!new && md->md_iterations == -1) { 515 gctl_error(req, "Missing -p flag."); 516 return (-1); 517 } 518 passbuf[0] = '\0'; 519 520 /* Use cached passphrase if defined. */ 521 if (strlen(cached_passphrase) > 0) { 522 strlcpy(passbuf, cached_passphrase, sizeof(passbuf)); 523 } else { 524 nfiles = eli_genkey_files(req, new, "passfile", NULL, passbuf, 525 sizeof(passbuf)); 526 if (nfiles == -1) { 527 return (-1); 528 } else if (nfiles == 0) { 529 if (eli_genkey_passphrase_prompt(req, new, passbuf, 530 sizeof(passbuf)) == -1) { 531 return (-1); 532 } 533 } 534 /* Cache the passphrase for other providers. */ 535 strlcpy(cached_passphrase, passbuf, sizeof(cached_passphrase)); 536 } 537 /* 538 * Field md_iterations equal to -1 means "choose some sane 539 * value for me". 540 */ 541 if (md->md_iterations == -1) { 542 assert(new); 543 if (verbose) 544 printf("Calculating number of iterations...\n"); 545 md->md_iterations = pkcs5v2_calculate(2000000); 546 assert(md->md_iterations > 0); 547 if (verbose) { 548 printf("Done, using %d iterations.\n", 549 md->md_iterations); 550 } 551 } 552 /* 553 * If md_iterations is equal to 0, user doesn't want PKCS#5v2. 554 */ 555 if (md->md_iterations == 0) { 556 g_eli_crypto_hmac_update(ctxp, md->md_salt, 557 sizeof(md->md_salt)); 558 g_eli_crypto_hmac_update(ctxp, passbuf, strlen(passbuf)); 559 } else /* if (md->md_iterations > 0) */ { 560 unsigned char dkey[G_ELI_USERKEYLEN]; 561 562 pkcs5v2_genkey(dkey, sizeof(dkey), md->md_salt, 563 sizeof(md->md_salt), passbuf, md->md_iterations); 564 g_eli_crypto_hmac_update(ctxp, dkey, sizeof(dkey)); 565 explicit_bzero(dkey, sizeof(dkey)); 566 } 567 explicit_bzero(passbuf, sizeof(passbuf)); 568 569 return (0); 570 } 571 572 static bool 573 eli_init_key_hmac_ctx(struct gctl_req *req, struct hmac_ctx *ctx, bool new) 574 { 575 int nfiles; 576 bool nopassphrase; 577 578 nopassphrase = 579 gctl_get_int(req, new ? "nonewpassphrase" : "nopassphrase"); 580 581 g_eli_crypto_hmac_init(ctx, NULL, 0); 582 nfiles = eli_genkey_files(req, new, "keyfile", ctx, NULL, 0); 583 if (nfiles == -1) { 584 return (false); 585 } else if (nfiles == 0 && nopassphrase) { 586 gctl_error(req, "No key components given."); 587 return (false); 588 } 589 590 return (true); 591 } 592 593 static unsigned char * 594 eli_genkey(struct gctl_req *req, const struct hmac_ctx *ctxtemplate, 595 struct g_eli_metadata *md, unsigned char *key, bool new) 596 { 597 struct hmac_ctx ctx; 598 599 memcpy(&ctx, ctxtemplate, sizeof(ctx)); 600 601 if (eli_genkey_passphrase(req, md, new, &ctx) == -1) 602 return (NULL); 603 604 g_eli_crypto_hmac_final(&ctx, key, 0); 605 606 return (key); 607 } 608 609 static unsigned char * 610 eli_genkey_single(struct gctl_req *req, struct g_eli_metadata *md, 611 unsigned char *key, bool new) 612 { 613 struct hmac_ctx ctx; 614 unsigned char *rkey; 615 616 if (!eli_init_key_hmac_ctx(req, &ctx, new)) { 617 return (NULL); 618 } 619 rkey = eli_genkey(req, &ctx, md, key, new); 620 explicit_bzero(&ctx, sizeof(ctx)); 621 622 return (rkey); 623 } 624 625 static int 626 eli_metadata_read(struct gctl_req *req, const char *prov, 627 struct g_eli_metadata *md) 628 { 629 unsigned char sector[sizeof(struct g_eli_metadata)]; 630 int error; 631 632 if (g_get_sectorsize(prov) == 0) { 633 int fd; 634 635 /* This is a file probably. */ 636 fd = open(prov, O_RDONLY); 637 if (fd == -1) { 638 gctl_error(req, "Cannot open %s: %s.", prov, 639 strerror(errno)); 640 return (-1); 641 } 642 if (read(fd, sector, sizeof(sector)) != sizeof(sector)) { 643 gctl_error(req, "Cannot read metadata from %s: %s.", 644 prov, strerror(errno)); 645 close(fd); 646 return (-1); 647 } 648 close(fd); 649 } else { 650 /* This is a GEOM provider. */ 651 error = g_metadata_read(prov, sector, sizeof(sector), 652 G_ELI_MAGIC); 653 if (error != 0) { 654 gctl_error(req, "Cannot read metadata from %s: %s.", 655 prov, strerror(error)); 656 return (-1); 657 } 658 } 659 error = eli_metadata_decode(sector, md); 660 switch (error) { 661 case 0: 662 break; 663 case EOPNOTSUPP: 664 gctl_error(req, 665 "Provider's %s metadata version %u is too new.\n" 666 "geli: The highest supported version is %u.", 667 prov, (unsigned int)md->md_version, G_ELI_VERSION); 668 return (-1); 669 case EINVAL: 670 gctl_error(req, "Inconsistent provider's %s metadata.", prov); 671 return (-1); 672 default: 673 gctl_error(req, 674 "Unexpected error while decoding provider's %s metadata: %s.", 675 prov, strerror(error)); 676 return (-1); 677 } 678 return (0); 679 } 680 681 static int 682 eli_metadata_store(struct gctl_req *req, const char *prov, 683 struct g_eli_metadata *md) 684 { 685 unsigned char sector[sizeof(struct g_eli_metadata)]; 686 int error; 687 688 eli_metadata_encode(md, sector); 689 if (g_get_sectorsize(prov) == 0) { 690 int fd; 691 692 /* This is a file probably. */ 693 fd = open(prov, O_WRONLY | O_TRUNC); 694 if (fd == -1) { 695 gctl_error(req, "Cannot open %s: %s.", prov, 696 strerror(errno)); 697 explicit_bzero(sector, sizeof(sector)); 698 return (-1); 699 } 700 if (write(fd, sector, sizeof(sector)) != sizeof(sector)) { 701 gctl_error(req, "Cannot write metadata to %s: %s.", 702 prov, strerror(errno)); 703 explicit_bzero(sector, sizeof(sector)); 704 close(fd); 705 return (-1); 706 } 707 close(fd); 708 } else { 709 /* This is a GEOM provider. */ 710 error = g_metadata_store(prov, sector, sizeof(sector)); 711 if (error != 0) { 712 gctl_error(req, "Cannot write metadata to %s: %s.", 713 prov, strerror(errno)); 714 explicit_bzero(sector, sizeof(sector)); 715 return (-1); 716 } 717 } 718 explicit_bzero(sector, sizeof(sector)); 719 return (0); 720 } 721 722 static void 723 eli_init(struct gctl_req *req) 724 { 725 struct g_eli_metadata md; 726 struct gctl_req *r; 727 unsigned char sector[sizeof(struct g_eli_metadata)] __aligned(4); 728 unsigned char key[G_ELI_USERKEYLEN]; 729 char backfile[MAXPATHLEN]; 730 const char *str, *prov; 731 unsigned int secsize, eli_version; 732 off_t mediasize; 733 intmax_t val; 734 int error, i, nargs, nparams, param; 735 const int one = 1; 736 struct hmac_ctx ctxtemplate; 737 738 nargs = gctl_get_int(req, "nargs"); 739 if (nargs <= 0) { 740 gctl_error(req, "Too few arguments."); 741 return; 742 } 743 744 /* Start generating metadata for provider(s) being initialized. */ 745 explicit_bzero(&md, sizeof(md)); 746 strlcpy(md.md_magic, G_ELI_MAGIC, sizeof(md.md_magic)); 747 val = gctl_get_intmax(req, "mdversion"); 748 if (val == -1) { 749 eli_version = G_ELI_VERSION; 750 } else if (val < 0 || val > G_ELI_VERSION) { 751 gctl_error(req, 752 "Invalid version specified should be between %u and %u.", 753 G_ELI_VERSION_00, G_ELI_VERSION); 754 return; 755 } else { 756 eli_version = val; 757 } 758 md.md_version = eli_version; 759 md.md_flags = G_ELI_FLAG_AUTORESIZE; 760 if (gctl_get_int(req, "boot")) 761 md.md_flags |= G_ELI_FLAG_BOOT; 762 if (gctl_get_int(req, "geliboot")) 763 md.md_flags |= G_ELI_FLAG_GELIBOOT; 764 if (gctl_get_int(req, "displaypass")) 765 md.md_flags |= G_ELI_FLAG_GELIDISPLAYPASS; 766 if (gctl_get_int(req, "notrim")) 767 md.md_flags |= G_ELI_FLAG_NODELETE; 768 if (gctl_get_int(req, "noautoresize")) 769 md.md_flags &= ~G_ELI_FLAG_AUTORESIZE; 770 md.md_ealgo = CRYPTO_ALGORITHM_MIN - 1; 771 str = gctl_get_ascii(req, "aalgo"); 772 if (*str != '\0') { 773 if (eli_version < G_ELI_VERSION_01) { 774 gctl_error(req, 775 "Data authentication is supported starting from version %u.", 776 G_ELI_VERSION_01); 777 return; 778 } 779 md.md_aalgo = g_eli_str2aalgo(str); 780 if (md.md_aalgo >= CRYPTO_ALGORITHM_MIN && 781 md.md_aalgo <= CRYPTO_ALGORITHM_MAX) { 782 md.md_flags |= G_ELI_FLAG_AUTH; 783 } else { 784 /* 785 * For backward compatibility, check if the -a option 786 * was used to provide encryption algorithm. 787 */ 788 md.md_ealgo = g_eli_str2ealgo(str); 789 if (md.md_ealgo < CRYPTO_ALGORITHM_MIN || 790 md.md_ealgo > CRYPTO_ALGORITHM_MAX) { 791 gctl_error(req, 792 "Invalid authentication algorithm."); 793 return; 794 } else { 795 fprintf(stderr, "warning: The -e option, not " 796 "the -a option is now used to specify " 797 "encryption algorithm to use.\n"); 798 } 799 } 800 } 801 if (md.md_ealgo < CRYPTO_ALGORITHM_MIN || 802 md.md_ealgo > CRYPTO_ALGORITHM_MAX) { 803 str = gctl_get_ascii(req, "ealgo"); 804 if (*str == '\0') { 805 if (eli_version < G_ELI_VERSION_05) 806 str = "aes-cbc"; 807 else 808 str = GELI_ENC_ALGO; 809 } 810 md.md_ealgo = g_eli_str2ealgo(str); 811 if (md.md_ealgo < CRYPTO_ALGORITHM_MIN || 812 md.md_ealgo > CRYPTO_ALGORITHM_MAX) { 813 gctl_error(req, "Invalid encryption algorithm."); 814 return; 815 } 816 if (md.md_ealgo == CRYPTO_CAMELLIA_CBC && 817 eli_version < G_ELI_VERSION_04) { 818 gctl_error(req, 819 "Camellia-CBC algorithm is supported starting from version %u.", 820 G_ELI_VERSION_04); 821 return; 822 } 823 if (md.md_ealgo == CRYPTO_AES_XTS && 824 eli_version < G_ELI_VERSION_05) { 825 gctl_error(req, 826 "AES-XTS algorithm is supported starting from version %u.", 827 G_ELI_VERSION_05); 828 return; 829 } 830 } 831 val = gctl_get_intmax(req, "keylen"); 832 md.md_keylen = val; 833 md.md_keylen = g_eli_keylen(md.md_ealgo, md.md_keylen); 834 if (md.md_keylen == 0) { 835 gctl_error(req, "Invalid key length."); 836 return; 837 } 838 839 val = gctl_get_intmax(req, "iterations"); 840 if (val != -1) { 841 int nonewpassphrase; 842 843 /* 844 * Don't allow to set iterations when there will be no 845 * passphrase. 846 */ 847 nonewpassphrase = gctl_get_int(req, "nonewpassphrase"); 848 if (nonewpassphrase) { 849 gctl_error(req, 850 "Options -i and -P are mutually exclusive."); 851 return; 852 } 853 } 854 md.md_iterations = val; 855 856 val = gctl_get_intmax(req, "sectorsize"); 857 if (val > sysconf(_SC_PAGE_SIZE)) { 858 fprintf(stderr, 859 "warning: Using sectorsize bigger than the page size!\n"); 860 } 861 862 md.md_keys = 0x01; 863 864 /* 865 * Determine number of parameters in the parent geom request before the 866 * nargs parameter and list of providers. 867 */ 868 nparams = req->narg - nargs - 1; 869 870 /* Generate HMAC context template. */ 871 if (!eli_init_key_hmac_ctx(req, &ctxtemplate, true)) 872 return; 873 874 /* Create new child request for each provider and issue to kernel */ 875 for (i = 0; i < nargs; i++) { 876 r = gctl_get_handle(); 877 878 /* Copy each parameter from the parent request to the child */ 879 for (param = 0; param < nparams; param++) { 880 gctl_ro_param(r, req->arg[param].name, 881 req->arg[param].len, req->arg[param].value); 882 } 883 884 /* Add a single provider to the parameter list of the child */ 885 gctl_ro_param(r, "nargs", sizeof(one), &one); 886 prov = gctl_get_ascii(req, "arg%d", i); 887 gctl_ro_param(r, "arg0", -1, prov); 888 889 mediasize = g_get_mediasize(prov); 890 secsize = g_get_sectorsize(prov); 891 if (mediasize == 0 || secsize == 0) { 892 gctl_error(r, "Cannot get information about %s: %s.", 893 prov, strerror(errno)); 894 goto out; 895 } 896 897 md.md_provsize = mediasize; 898 899 val = gctl_get_intmax(r, "sectorsize"); 900 if (val == 0) { 901 md.md_sectorsize = secsize; 902 } else { 903 if (val < 0 || (val % secsize) != 0 || !powerof2(val)) { 904 gctl_error(r, "Invalid sector size."); 905 goto out; 906 } 907 md.md_sectorsize = val; 908 } 909 910 /* Use different salt and Master Key for each provider. */ 911 arc4random_buf(md.md_salt, sizeof(md.md_salt)); 912 arc4random_buf(md.md_mkeys, sizeof(md.md_mkeys)); 913 914 /* Generate user key. */ 915 if (eli_genkey(r, &ctxtemplate, &md, key, true) == NULL) { 916 /* 917 * Error generating key - details added to geom request 918 * by eli_genkey(). 919 */ 920 goto out; 921 } 922 923 /* Encrypt the first and the only Master Key. */ 924 error = g_eli_mkey_encrypt(md.md_ealgo, key, md.md_keylen, 925 md.md_mkeys); 926 if (error != 0) { 927 gctl_error(r, "Cannot encrypt Master Key: %s.", 928 strerror(error)); 929 goto out; 930 } 931 932 /* Convert metadata to on-disk format. */ 933 eli_metadata_encode(&md, sector); 934 935 /* Store metadata to disk. */ 936 error = g_metadata_store(prov, sector, sizeof(sector)); 937 if (error != 0) { 938 gctl_error(r, "Cannot store metadata on %s: %s.", prov, 939 strerror(error)); 940 goto out; 941 } 942 if (verbose) 943 printf("Metadata value stored on %s.\n", prov); 944 945 /* Backup metadata to a file. */ 946 const char *p = prov; 947 unsigned int j; 948 949 /* 950 * Check if provider string includes the devfs mountpoint 951 * (typically /dev/). 952 */ 953 if (strncmp(p, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0) { 954 /* Skip forward to the device filename only. */ 955 p += sizeof(_PATH_DEV) - 1; 956 } 957 958 str = gctl_get_ascii(r, "backupfile"); 959 if (str[0] != '\0') { 960 /* Backupfile given by the user, just copy it. */ 961 strlcpy(backfile, str, sizeof(backfile)); 962 963 /* If multiple providers have been initialized in one 964 * command, and the backup filename has been specified 965 * as anything other than "none", make the backup 966 * filename unique for each provider. */ 967 if (nargs > 1 && strcmp(backfile, "none") != 0) { 968 /* 969 * Replace first occurrence of "PROV" with 970 * provider name. 971 */ 972 str = strnstr(backfile, "PROV", 973 sizeof(backfile)); 974 if (str != NULL) { 975 char suffix[MAXPATHLEN]; 976 j = str - backfile; 977 strlcpy(suffix, &backfile[j+4], 978 sizeof(suffix)); 979 backfile[j] = '\0'; 980 strlcat(backfile, p, sizeof(backfile)); 981 strlcat(backfile, suffix, 982 sizeof(backfile)); 983 } else { 984 /* 985 * "PROV" not found in backfile, append 986 * provider name. 987 */ 988 strlcat(backfile, "-", 989 sizeof(backfile)); 990 strlcat(backfile, p, sizeof(backfile)); 991 } 992 } 993 } else { 994 /* Generate filename automatically. */ 995 snprintf(backfile, sizeof(backfile), "%s%s.eli", 996 GELI_BACKUP_DIR, p); 997 /* Replace all / with _. */ 998 for (j = strlen(GELI_BACKUP_DIR); backfile[j] != '\0'; 999 j++) { 1000 if (backfile[j] == '/') 1001 backfile[j] = '_'; 1002 } 1003 } 1004 if (strcmp(backfile, "none") != 0 && 1005 eli_backup_create(r, prov, backfile) == 0) { 1006 printf("\nMetadata backup for provider %s can be found " 1007 "in %s\n", prov, backfile); 1008 printf("and can be restored with the following " 1009 "command:\n"); 1010 printf("\n\t# geli restore %s %s\n\n", backfile, prov); 1011 } 1012 1013 out: 1014 /* 1015 * Print error for this request, and set parent request error 1016 * message. 1017 */ 1018 if (r->error != NULL && r->error[0] != '\0') { 1019 warnx("%s", r->error); 1020 gctl_error(req, "There was an error with at least one " 1021 "provider."); 1022 } 1023 1024 gctl_free(r); 1025 1026 /* 1027 * Erase sensitive and provider specific data from memory. 1028 */ 1029 explicit_bzero(key, sizeof(key)); 1030 explicit_bzero(sector, sizeof(sector)); 1031 explicit_bzero(&md.md_provsize, sizeof(md.md_provsize)); 1032 explicit_bzero(&md.md_sectorsize, sizeof(md.md_sectorsize)); 1033 explicit_bzero(&md.md_salt, sizeof(md.md_salt)); 1034 explicit_bzero(&md.md_mkeys, sizeof(md.md_mkeys)); 1035 } 1036 1037 /* Clear the cached metadata, including keys. */ 1038 explicit_bzero(&md, sizeof(md)); 1039 explicit_bzero(&ctxtemplate, sizeof(ctxtemplate)); 1040 } 1041 1042 static void 1043 eli_attach(struct gctl_req *req) 1044 { 1045 struct g_eli_metadata md; 1046 struct gctl_req *r; 1047 const char *prov; 1048 off_t mediasize; 1049 int i, nargs, nparams, param; 1050 const int one = 1; 1051 struct hmac_ctx ctxtemplate; 1052 1053 nargs = gctl_get_int(req, "nargs"); 1054 if (nargs <= 0) { 1055 gctl_error(req, "Too few arguments."); 1056 return; 1057 } 1058 1059 unsigned char key[G_ELI_USERKEYLEN]; 1060 1061 /* 1062 * Determine number of parameters in the parent geom request before the 1063 * nargs parameter and list of providers. 1064 */ 1065 nparams = req->narg - nargs - 1; 1066 1067 /* Generate HMAC context template. */ 1068 if (!eli_init_key_hmac_ctx(req, &ctxtemplate, false)) 1069 return; 1070 1071 /* Create new child request for each provider and issue to kernel */ 1072 for (i = 0; i < nargs; i++) { 1073 r = gctl_get_handle(); 1074 1075 /* Copy each parameter from the parent request to the child */ 1076 for (param = 0; param < nparams; param++) { 1077 gctl_ro_param(r, req->arg[param].name, 1078 req->arg[param].len, req->arg[param].value); 1079 } 1080 1081 /* Add a single provider to the parameter list of the child */ 1082 gctl_ro_param(r, "nargs", sizeof(one), &one); 1083 prov = gctl_get_ascii(req, "arg%d", i); 1084 gctl_ro_param(r, "arg0", -1, prov); 1085 1086 if (eli_metadata_read(r, prov, &md) == -1) { 1087 /* 1088 * Error reading metadata - details added to geom 1089 * request by eli_metadata_read(). 1090 */ 1091 goto out; 1092 } 1093 1094 mediasize = g_get_mediasize(prov); 1095 if (md.md_provsize != (uint64_t)mediasize) { 1096 gctl_error(r, "Provider size mismatch."); 1097 goto out; 1098 } 1099 1100 if (eli_genkey(r, &ctxtemplate, &md, key, false) == NULL) { 1101 /* 1102 * Error generating key - details added to geom request 1103 * by eli_genkey(). 1104 */ 1105 goto out; 1106 } 1107 1108 gctl_ro_param(r, "key", sizeof(key), key); 1109 1110 if (gctl_issue(r) == NULL) { 1111 if (verbose) 1112 printf("Attached to %s.\n", prov); 1113 } 1114 1115 out: 1116 /* 1117 * Print error for this request, and set parent request error 1118 * message. 1119 */ 1120 if (r->error != NULL && r->error[0] != '\0') { 1121 warnx("%s", r->error); 1122 gctl_error(req, "There was an error with at least one " 1123 "provider."); 1124 } 1125 1126 gctl_free(r); 1127 1128 /* Clear sensitive data from memory. */ 1129 explicit_bzero(key, sizeof(key)); 1130 } 1131 1132 /* Clear sensitive data from memory. */ 1133 explicit_bzero(cached_passphrase, sizeof(cached_passphrase)); 1134 explicit_bzero(&ctxtemplate, sizeof(ctxtemplate)); 1135 } 1136 1137 static void 1138 eli_configure_detached(struct gctl_req *req, const char *prov, int boot, 1139 int geliboot, int displaypass, int trim, int autoresize) 1140 { 1141 struct g_eli_metadata md; 1142 bool changed = 0; 1143 1144 if (eli_metadata_read(req, prov, &md) == -1) 1145 return; 1146 1147 if (boot == 1 && (md.md_flags & G_ELI_FLAG_BOOT)) { 1148 if (verbose) 1149 printf("BOOT flag already configured for %s.\n", prov); 1150 } else if (boot == 0 && !(md.md_flags & G_ELI_FLAG_BOOT)) { 1151 if (verbose) 1152 printf("BOOT flag not configured for %s.\n", prov); 1153 } else if (boot >= 0) { 1154 if (boot) 1155 md.md_flags |= G_ELI_FLAG_BOOT; 1156 else 1157 md.md_flags &= ~G_ELI_FLAG_BOOT; 1158 changed = 1; 1159 } 1160 1161 if (geliboot == 1 && (md.md_flags & G_ELI_FLAG_GELIBOOT)) { 1162 if (verbose) 1163 printf("GELIBOOT flag already configured for %s.\n", prov); 1164 } else if (geliboot == 0 && !(md.md_flags & G_ELI_FLAG_GELIBOOT)) { 1165 if (verbose) 1166 printf("GELIBOOT flag not configured for %s.\n", prov); 1167 } else if (geliboot >= 0) { 1168 if (geliboot) 1169 md.md_flags |= G_ELI_FLAG_GELIBOOT; 1170 else 1171 md.md_flags &= ~G_ELI_FLAG_GELIBOOT; 1172 changed = 1; 1173 } 1174 1175 if (displaypass == 1 && (md.md_flags & G_ELI_FLAG_GELIDISPLAYPASS)) { 1176 if (verbose) 1177 printf("GELIDISPLAYPASS flag already configured for %s.\n", prov); 1178 } else if (displaypass == 0 && 1179 !(md.md_flags & G_ELI_FLAG_GELIDISPLAYPASS)) { 1180 if (verbose) 1181 printf("GELIDISPLAYPASS flag not configured for %s.\n", prov); 1182 } else if (displaypass >= 0) { 1183 if (displaypass) 1184 md.md_flags |= G_ELI_FLAG_GELIDISPLAYPASS; 1185 else 1186 md.md_flags &= ~G_ELI_FLAG_GELIDISPLAYPASS; 1187 changed = 1; 1188 } 1189 1190 if (trim == 0 && (md.md_flags & G_ELI_FLAG_NODELETE)) { 1191 if (verbose) 1192 printf("TRIM disable flag already configured for %s.\n", prov); 1193 } else if (trim == 1 && !(md.md_flags & G_ELI_FLAG_NODELETE)) { 1194 if (verbose) 1195 printf("TRIM disable flag not configured for %s.\n", prov); 1196 } else if (trim >= 0) { 1197 if (trim) 1198 md.md_flags &= ~G_ELI_FLAG_NODELETE; 1199 else 1200 md.md_flags |= G_ELI_FLAG_NODELETE; 1201 changed = 1; 1202 } 1203 1204 if (autoresize == 1 && (md.md_flags & G_ELI_FLAG_AUTORESIZE)) { 1205 if (verbose) 1206 printf("AUTORESIZE flag already configured for %s.\n", prov); 1207 } else if (autoresize == 0 && !(md.md_flags & G_ELI_FLAG_AUTORESIZE)) { 1208 if (verbose) 1209 printf("AUTORESIZE flag not configured for %s.\n", prov); 1210 } else if (autoresize >= 0) { 1211 if (autoresize) 1212 md.md_flags |= G_ELI_FLAG_AUTORESIZE; 1213 else 1214 md.md_flags &= ~G_ELI_FLAG_AUTORESIZE; 1215 changed = 1; 1216 } 1217 1218 if (changed) 1219 eli_metadata_store(req, prov, &md); 1220 explicit_bzero(&md, sizeof(md)); 1221 } 1222 1223 static void 1224 eli_configure(struct gctl_req *req) 1225 { 1226 const char *prov; 1227 bool boot, noboot, geliboot, nogeliboot, displaypass, nodisplaypass; 1228 bool autoresize, noautoresize, trim, notrim; 1229 int doboot, dogeliboot, dodisplaypass, dotrim, doautoresize; 1230 int i, nargs; 1231 1232 nargs = gctl_get_int(req, "nargs"); 1233 if (nargs == 0) { 1234 gctl_error(req, "Too few arguments."); 1235 return; 1236 } 1237 1238 boot = gctl_get_int(req, "boot"); 1239 noboot = gctl_get_int(req, "noboot"); 1240 geliboot = gctl_get_int(req, "geliboot"); 1241 nogeliboot = gctl_get_int(req, "nogeliboot"); 1242 displaypass = gctl_get_int(req, "displaypass"); 1243 nodisplaypass = gctl_get_int(req, "nodisplaypass"); 1244 trim = gctl_get_int(req, "trim"); 1245 notrim = gctl_get_int(req, "notrim"); 1246 autoresize = gctl_get_int(req, "autoresize"); 1247 noautoresize = gctl_get_int(req, "noautoresize"); 1248 1249 doboot = -1; 1250 if (boot && noboot) { 1251 gctl_error(req, "Options -b and -B are mutually exclusive."); 1252 return; 1253 } 1254 if (boot) 1255 doboot = 1; 1256 else if (noboot) 1257 doboot = 0; 1258 1259 dogeliboot = -1; 1260 if (geliboot && nogeliboot) { 1261 gctl_error(req, "Options -g and -G are mutually exclusive."); 1262 return; 1263 } 1264 if (geliboot) 1265 dogeliboot = 1; 1266 else if (nogeliboot) 1267 dogeliboot = 0; 1268 1269 dodisplaypass = -1; 1270 if (displaypass && nodisplaypass) { 1271 gctl_error(req, "Options -d and -D are mutually exclusive."); 1272 return; 1273 } 1274 if (displaypass) 1275 dodisplaypass = 1; 1276 else if (nodisplaypass) 1277 dodisplaypass = 0; 1278 1279 dotrim = -1; 1280 if (trim && notrim) { 1281 gctl_error(req, "Options -t and -T are mutually exclusive."); 1282 return; 1283 } 1284 if (trim) 1285 dotrim = 1; 1286 else if (notrim) 1287 dotrim = 0; 1288 1289 doautoresize = -1; 1290 if (autoresize && noautoresize) { 1291 gctl_error(req, "Options -r and -R are mutually exclusive."); 1292 return; 1293 } 1294 if (autoresize) 1295 doautoresize = 1; 1296 else if (noautoresize) 1297 doautoresize = 0; 1298 1299 if (doboot == -1 && dogeliboot == -1 && dodisplaypass == -1 && 1300 dotrim == -1 && doautoresize == -1) { 1301 gctl_error(req, "No option given."); 1302 return; 1303 } 1304 1305 /* First attached providers. */ 1306 gctl_issue(req); 1307 /* Now the rest. */ 1308 for (i = 0; i < nargs; i++) { 1309 prov = gctl_get_ascii(req, "arg%d", i); 1310 if (!eli_is_attached(prov)) { 1311 eli_configure_detached(req, prov, doboot, dogeliboot, 1312 dodisplaypass, dotrim, doautoresize); 1313 } 1314 } 1315 } 1316 1317 static void 1318 eli_setkey_attached(struct gctl_req *req, struct g_eli_metadata *md) 1319 { 1320 unsigned char key[G_ELI_USERKEYLEN]; 1321 intmax_t val, old = 0; 1322 int error; 1323 1324 val = gctl_get_intmax(req, "iterations"); 1325 /* Check if iterations number should be changed. */ 1326 if (val != -1) 1327 md->md_iterations = val; 1328 else 1329 old = md->md_iterations; 1330 1331 /* Generate key for Master Key encryption. */ 1332 if (eli_genkey_single(req, md, key, true) == NULL) { 1333 explicit_bzero(key, sizeof(key)); 1334 return; 1335 } 1336 /* 1337 * If number of iterations has changed, but wasn't given as a 1338 * command-line argument, update the request. 1339 */ 1340 if (val == -1 && md->md_iterations != old) { 1341 error = gctl_change_param(req, "iterations", sizeof(intmax_t), 1342 &md->md_iterations); 1343 assert(error == 0); 1344 } 1345 1346 gctl_ro_param(req, "key", sizeof(key), key); 1347 gctl_issue(req); 1348 explicit_bzero(key, sizeof(key)); 1349 } 1350 1351 static void 1352 eli_setkey_detached(struct gctl_req *req, const char *prov, 1353 struct g_eli_metadata *md) 1354 { 1355 unsigned char key[G_ELI_USERKEYLEN], mkey[G_ELI_DATAIVKEYLEN]; 1356 unsigned char *mkeydst; 1357 unsigned int nkey; 1358 intmax_t val; 1359 int error; 1360 1361 if (md->md_keys == 0) { 1362 gctl_error(req, "No valid keys on %s.", prov); 1363 return; 1364 } 1365 1366 /* Generate key for Master Key decryption. */ 1367 if (eli_genkey_single(req, md, key, false) == NULL) { 1368 explicit_bzero(key, sizeof(key)); 1369 return; 1370 } 1371 1372 /* Decrypt Master Key. */ 1373 error = g_eli_mkey_decrypt_any(md, key, mkey, &nkey); 1374 explicit_bzero(key, sizeof(key)); 1375 if (error != 0) { 1376 explicit_bzero(md, sizeof(*md)); 1377 if (error == -1) 1378 gctl_error(req, "Wrong key for %s.", prov); 1379 else /* if (error > 0) */ { 1380 gctl_error(req, "Cannot decrypt Master Key: %s.", 1381 strerror(error)); 1382 } 1383 return; 1384 } 1385 if (verbose) 1386 printf("Decrypted Master Key %u.\n", nkey); 1387 1388 val = gctl_get_intmax(req, "keyno"); 1389 if (val != -1) 1390 nkey = val; 1391 #if 0 1392 else 1393 ; /* Use the key number which was found during decryption. */ 1394 #endif 1395 if (nkey >= G_ELI_MAXMKEYS) { 1396 gctl_error(req, "Invalid '%s' argument.", "keyno"); 1397 return; 1398 } 1399 1400 val = gctl_get_intmax(req, "iterations"); 1401 /* Check if iterations number should and can be changed. */ 1402 if (val != -1 && md->md_iterations == -1) { 1403 md->md_iterations = val; 1404 } else if (val != -1 && val != md->md_iterations) { 1405 if (bitcount32(md->md_keys) != 1) { 1406 gctl_error(req, "To be able to use '-i' option, only " 1407 "one key can be defined."); 1408 return; 1409 } 1410 if (md->md_keys != (1 << nkey)) { 1411 gctl_error(req, "Only already defined key can be " 1412 "changed when '-i' option is used."); 1413 return; 1414 } 1415 md->md_iterations = val; 1416 } 1417 1418 mkeydst = md->md_mkeys + nkey * G_ELI_MKEYLEN; 1419 md->md_keys |= (1 << nkey); 1420 1421 bcopy(mkey, mkeydst, sizeof(mkey)); 1422 explicit_bzero(mkey, sizeof(mkey)); 1423 1424 /* Generate key for Master Key encryption. */ 1425 if (eli_genkey_single(req, md, key, true) == NULL) { 1426 explicit_bzero(key, sizeof(key)); 1427 explicit_bzero(md, sizeof(*md)); 1428 return; 1429 } 1430 1431 /* Encrypt the Master-Key with the new key. */ 1432 error = g_eli_mkey_encrypt(md->md_ealgo, key, md->md_keylen, mkeydst); 1433 explicit_bzero(key, sizeof(key)); 1434 if (error != 0) { 1435 explicit_bzero(md, sizeof(*md)); 1436 gctl_error(req, "Cannot encrypt Master Key: %s.", 1437 strerror(error)); 1438 return; 1439 } 1440 1441 /* Store metadata with fresh key. */ 1442 eli_metadata_store(req, prov, md); 1443 explicit_bzero(md, sizeof(*md)); 1444 } 1445 1446 static void 1447 eli_setkey(struct gctl_req *req) 1448 { 1449 struct g_eli_metadata md; 1450 const char *prov; 1451 int nargs; 1452 1453 nargs = gctl_get_int(req, "nargs"); 1454 if (nargs != 1) { 1455 gctl_error(req, "Invalid number of arguments."); 1456 return; 1457 } 1458 prov = gctl_get_ascii(req, "arg0"); 1459 1460 if (eli_metadata_read(req, prov, &md) == -1) 1461 return; 1462 1463 if (eli_is_attached(prov)) 1464 eli_setkey_attached(req, &md); 1465 else 1466 eli_setkey_detached(req, prov, &md); 1467 1468 if (req->error == NULL || req->error[0] == '\0') { 1469 printf("Note, that the master key encrypted with old keys " 1470 "and/or passphrase may still exist in a metadata backup " 1471 "file.\n"); 1472 } 1473 } 1474 1475 static void 1476 eli_delkey_attached(struct gctl_req *req, const char *prov __unused) 1477 { 1478 1479 gctl_issue(req); 1480 } 1481 1482 static void 1483 eli_delkey_detached(struct gctl_req *req, const char *prov) 1484 { 1485 struct g_eli_metadata md; 1486 unsigned char *mkeydst; 1487 unsigned int nkey; 1488 intmax_t val; 1489 bool all, force; 1490 1491 if (eli_metadata_read(req, prov, &md) == -1) 1492 return; 1493 1494 all = gctl_get_int(req, "all"); 1495 if (all) 1496 arc4random_buf(md.md_mkeys, sizeof(md.md_mkeys)); 1497 else { 1498 force = gctl_get_int(req, "force"); 1499 val = gctl_get_intmax(req, "keyno"); 1500 if (val == -1) { 1501 gctl_error(req, "Key number has to be specified."); 1502 return; 1503 } 1504 nkey = val; 1505 if (nkey >= G_ELI_MAXMKEYS) { 1506 gctl_error(req, "Invalid '%s' argument.", "keyno"); 1507 return; 1508 } 1509 if (!(md.md_keys & (1 << nkey)) && !force) { 1510 gctl_error(req, "Master Key %u is not set.", nkey); 1511 return; 1512 } 1513 md.md_keys &= ~(1 << nkey); 1514 if (md.md_keys == 0 && !force) { 1515 gctl_error(req, "This is the last Master Key. Use '-f' " 1516 "option if you really want to remove it."); 1517 return; 1518 } 1519 mkeydst = md.md_mkeys + nkey * G_ELI_MKEYLEN; 1520 arc4random_buf(mkeydst, G_ELI_MKEYLEN); 1521 } 1522 1523 eli_metadata_store(req, prov, &md); 1524 explicit_bzero(&md, sizeof(md)); 1525 } 1526 1527 static void 1528 eli_delkey(struct gctl_req *req) 1529 { 1530 const char *prov; 1531 int nargs; 1532 1533 nargs = gctl_get_int(req, "nargs"); 1534 if (nargs != 1) { 1535 gctl_error(req, "Invalid number of arguments."); 1536 return; 1537 } 1538 prov = gctl_get_ascii(req, "arg0"); 1539 1540 if (eli_is_attached(prov)) 1541 eli_delkey_attached(req, prov); 1542 else 1543 eli_delkey_detached(req, prov); 1544 } 1545 1546 static void 1547 eli_resume(struct gctl_req *req) 1548 { 1549 struct g_eli_metadata md; 1550 unsigned char key[G_ELI_USERKEYLEN]; 1551 const char *prov; 1552 off_t mediasize; 1553 int nargs; 1554 1555 nargs = gctl_get_int(req, "nargs"); 1556 if (nargs != 1) { 1557 gctl_error(req, "Invalid number of arguments."); 1558 return; 1559 } 1560 prov = gctl_get_ascii(req, "arg0"); 1561 1562 if (eli_metadata_read(req, prov, &md) == -1) 1563 return; 1564 1565 mediasize = g_get_mediasize(prov); 1566 if (md.md_provsize != (uint64_t)mediasize) { 1567 gctl_error(req, "Provider size mismatch."); 1568 return; 1569 } 1570 1571 if (eli_genkey_single(req, &md, key, false) == NULL) { 1572 explicit_bzero(key, sizeof(key)); 1573 return; 1574 } 1575 1576 gctl_ro_param(req, "key", sizeof(key), key); 1577 if (gctl_issue(req) == NULL) { 1578 if (verbose) 1579 printf("Resumed %s.\n", prov); 1580 } 1581 explicit_bzero(key, sizeof(key)); 1582 } 1583 1584 static int 1585 eli_trash_metadata(struct gctl_req *req, const char *prov, int fd, off_t offset) 1586 { 1587 unsigned int overwrites; 1588 unsigned char *sector; 1589 ssize_t size; 1590 int error; 1591 1592 size = sizeof(overwrites); 1593 if (sysctlbyname("kern.geom.eli.overwrites", &overwrites, &size, 1594 NULL, 0) == -1 || overwrites == 0) { 1595 overwrites = G_ELI_OVERWRITES; 1596 } 1597 1598 size = g_sectorsize(fd); 1599 if (size <= 0) { 1600 gctl_error(req, "Cannot obtain provider sector size %s: %s.", 1601 prov, strerror(errno)); 1602 return (-1); 1603 } 1604 sector = malloc(size); 1605 if (sector == NULL) { 1606 gctl_error(req, "Cannot allocate %zd bytes of memory.", size); 1607 return (-1); 1608 } 1609 1610 error = 0; 1611 do { 1612 arc4random_buf(sector, size); 1613 if (pwrite(fd, sector, size, offset) != size) { 1614 if (error == 0) 1615 error = errno; 1616 } 1617 (void)g_flush(fd); 1618 } while (--overwrites > 0); 1619 free(sector); 1620 if (error != 0) { 1621 gctl_error(req, "Cannot trash metadata on provider %s: %s.", 1622 prov, strerror(error)); 1623 return (-1); 1624 } 1625 return (0); 1626 } 1627 1628 static void 1629 eli_kill_detached(struct gctl_req *req, const char *prov) 1630 { 1631 off_t offset; 1632 int fd; 1633 1634 /* 1635 * NOTE: Maybe we should verify if this is geli provider first, 1636 * but 'kill' command is quite critical so better don't waste 1637 * the time. 1638 */ 1639 #if 0 1640 error = g_metadata_read(prov, (unsigned char *)&md, sizeof(md), 1641 G_ELI_MAGIC); 1642 if (error != 0) { 1643 gctl_error(req, "Cannot read metadata from %s: %s.", prov, 1644 strerror(error)); 1645 return; 1646 } 1647 #endif 1648 1649 fd = g_open(prov, 1); 1650 if (fd == -1) { 1651 gctl_error(req, "Cannot open provider %s: %s.", prov, 1652 strerror(errno)); 1653 return; 1654 } 1655 offset = g_mediasize(fd) - g_sectorsize(fd); 1656 if (offset <= 0) { 1657 gctl_error(req, 1658 "Cannot obtain media size or sector size for provider %s: %s.", 1659 prov, strerror(errno)); 1660 (void)g_close(fd); 1661 return; 1662 } 1663 (void)eli_trash_metadata(req, prov, fd, offset); 1664 (void)g_close(fd); 1665 } 1666 1667 static void 1668 eli_kill(struct gctl_req *req) 1669 { 1670 const char *prov; 1671 int i, nargs, all; 1672 1673 nargs = gctl_get_int(req, "nargs"); 1674 all = gctl_get_int(req, "all"); 1675 if (!all && nargs == 0) { 1676 gctl_error(req, "Too few arguments."); 1677 return; 1678 } 1679 /* 1680 * How '-a' option combine with a list of providers: 1681 * Delete Master Keys from all attached providers: 1682 * geli kill -a 1683 * Delete Master Keys from all attached providers and from 1684 * detached da0 and da1: 1685 * geli kill -a da0 da1 1686 * Delete Master Keys from (attached or detached) da0 and da1: 1687 * geli kill da0 da1 1688 */ 1689 1690 /* First detached providers. */ 1691 for (i = 0; i < nargs; i++) { 1692 prov = gctl_get_ascii(req, "arg%d", i); 1693 if (!eli_is_attached(prov)) 1694 eli_kill_detached(req, prov); 1695 } 1696 /* Now attached providers. */ 1697 gctl_issue(req); 1698 } 1699 1700 static int 1701 eli_backup_create(struct gctl_req *req, const char *prov, const char *file) 1702 { 1703 unsigned char *sector; 1704 ssize_t secsize; 1705 int error, filefd, ret; 1706 1707 ret = -1; 1708 filefd = -1; 1709 sector = NULL; 1710 secsize = 0; 1711 1712 secsize = g_get_sectorsize(prov); 1713 if (secsize == 0) { 1714 gctl_error(req, "Cannot get informations about %s: %s.", prov, 1715 strerror(errno)); 1716 goto out; 1717 } 1718 sector = malloc(secsize); 1719 if (sector == NULL) { 1720 gctl_error(req, "Cannot allocate memory."); 1721 goto out; 1722 } 1723 /* Read metadata from the provider. */ 1724 error = g_metadata_read(prov, sector, secsize, G_ELI_MAGIC); 1725 if (error != 0) { 1726 gctl_error(req, "Unable to read metadata from %s: %s.", prov, 1727 strerror(error)); 1728 goto out; 1729 } 1730 1731 filefd = open(file, O_WRONLY | O_TRUNC | O_CREAT, 0600); 1732 if (filefd == -1) { 1733 gctl_error(req, "Unable to open %s: %s.", file, 1734 strerror(errno)); 1735 goto out; 1736 } 1737 /* Write metadata to the destination file. */ 1738 if (write(filefd, sector, secsize) != secsize) { 1739 gctl_error(req, "Unable to write to %s: %s.", file, 1740 strerror(errno)); 1741 (void)close(filefd); 1742 (void)unlink(file); 1743 goto out; 1744 } 1745 (void)fsync(filefd); 1746 (void)close(filefd); 1747 /* Success. */ 1748 ret = 0; 1749 out: 1750 if (sector != NULL) { 1751 explicit_bzero(sector, secsize); 1752 free(sector); 1753 } 1754 return (ret); 1755 } 1756 1757 static void 1758 eli_backup(struct gctl_req *req) 1759 { 1760 const char *file, *prov; 1761 int nargs; 1762 1763 nargs = gctl_get_int(req, "nargs"); 1764 if (nargs != 2) { 1765 gctl_error(req, "Invalid number of arguments."); 1766 return; 1767 } 1768 prov = gctl_get_ascii(req, "arg0"); 1769 file = gctl_get_ascii(req, "arg1"); 1770 1771 eli_backup_create(req, prov, file); 1772 } 1773 1774 static void 1775 eli_restore(struct gctl_req *req) 1776 { 1777 struct g_eli_metadata md; 1778 const char *file, *prov; 1779 off_t mediasize; 1780 int nargs; 1781 1782 nargs = gctl_get_int(req, "nargs"); 1783 if (nargs != 2) { 1784 gctl_error(req, "Invalid number of arguments."); 1785 return; 1786 } 1787 file = gctl_get_ascii(req, "arg0"); 1788 prov = gctl_get_ascii(req, "arg1"); 1789 1790 /* Read metadata from the backup file. */ 1791 if (eli_metadata_read(req, file, &md) == -1) 1792 return; 1793 /* Obtain provider's mediasize. */ 1794 mediasize = g_get_mediasize(prov); 1795 if (mediasize == 0) { 1796 gctl_error(req, "Cannot get informations about %s: %s.", prov, 1797 strerror(errno)); 1798 return; 1799 } 1800 /* Check if the provider size has changed since we did the backup. */ 1801 if (md.md_provsize != (uint64_t)mediasize) { 1802 if (gctl_get_int(req, "force")) { 1803 md.md_provsize = mediasize; 1804 } else { 1805 gctl_error(req, "Provider size mismatch: " 1806 "wrong backup file?"); 1807 return; 1808 } 1809 } 1810 /* Write metadata to the provider. */ 1811 (void)eli_metadata_store(req, prov, &md); 1812 } 1813 1814 static void 1815 eli_resize(struct gctl_req *req) 1816 { 1817 struct g_eli_metadata md; 1818 const char *prov; 1819 unsigned char *sector; 1820 ssize_t secsize; 1821 off_t mediasize, oldsize; 1822 int error, nargs, provfd; 1823 1824 nargs = gctl_get_int(req, "nargs"); 1825 if (nargs != 1) { 1826 gctl_error(req, "Invalid number of arguments."); 1827 return; 1828 } 1829 prov = gctl_get_ascii(req, "arg0"); 1830 1831 provfd = -1; 1832 sector = NULL; 1833 secsize = 0; 1834 1835 provfd = g_open(prov, 1); 1836 if (provfd == -1) { 1837 gctl_error(req, "Cannot open %s: %s.", prov, strerror(errno)); 1838 goto out; 1839 } 1840 1841 mediasize = g_mediasize(provfd); 1842 secsize = g_sectorsize(provfd); 1843 if (mediasize == -1 || secsize == -1) { 1844 gctl_error(req, "Cannot get information about %s: %s.", prov, 1845 strerror(errno)); 1846 goto out; 1847 } 1848 1849 sector = malloc(secsize); 1850 if (sector == NULL) { 1851 gctl_error(req, "Cannot allocate memory."); 1852 goto out; 1853 } 1854 1855 oldsize = gctl_get_intmax(req, "oldsize"); 1856 if (oldsize < 0 || oldsize > mediasize) { 1857 gctl_error(req, "Invalid oldsize: Out of range."); 1858 goto out; 1859 } 1860 1861 /* Read metadata from the 'oldsize' offset. */ 1862 if (pread(provfd, sector, secsize, oldsize - secsize) != secsize) { 1863 gctl_error(req, "Cannot read old metadata: %s.", 1864 strerror(errno)); 1865 goto out; 1866 } 1867 1868 /* Check if this sector contains geli metadata. */ 1869 error = eli_metadata_decode(sector, &md); 1870 switch (error) { 1871 case 0: 1872 break; 1873 case EOPNOTSUPP: 1874 gctl_error(req, 1875 "Provider's %s metadata version %u is too new.\n" 1876 "geli: The highest supported version is %u.", 1877 prov, (unsigned int)md.md_version, G_ELI_VERSION); 1878 goto out; 1879 case EINVAL: 1880 gctl_error(req, "Inconsistent provider's %s metadata.", prov); 1881 goto out; 1882 default: 1883 gctl_error(req, 1884 "Unexpected error while decoding provider's %s metadata: %s.", 1885 prov, strerror(error)); 1886 goto out; 1887 } 1888 1889 /* 1890 * If the old metadata doesn't have a correct provider size, refuse 1891 * to resize. 1892 */ 1893 if (md.md_provsize != (uint64_t)oldsize) { 1894 gctl_error(req, "Provider size mismatch at oldsize."); 1895 goto out; 1896 } 1897 1898 /* The metadata is valid and nothing has changed. Just exit. */ 1899 if (oldsize == mediasize) 1900 goto out; 1901 1902 /* 1903 * Update the old metadata with the current provider size and write 1904 * it back to the correct place on the provider. 1905 */ 1906 md.md_provsize = mediasize; 1907 /* Write metadata to the provider. */ 1908 (void)eli_metadata_store(req, prov, &md); 1909 /* Now trash the old metadata. */ 1910 (void)eli_trash_metadata(req, prov, provfd, oldsize - secsize); 1911 out: 1912 if (provfd != -1) 1913 (void)g_close(provfd); 1914 if (sector != NULL) { 1915 explicit_bzero(sector, secsize); 1916 free(sector); 1917 } 1918 } 1919 1920 static void 1921 eli_version(struct gctl_req *req) 1922 { 1923 struct g_eli_metadata md; 1924 const char *name; 1925 unsigned int eli_version; 1926 int error, i, nargs; 1927 1928 nargs = gctl_get_int(req, "nargs"); 1929 1930 if (nargs == 0) { 1931 unsigned int kernver; 1932 ssize_t size; 1933 1934 size = sizeof(kernver); 1935 if (sysctlbyname("kern.geom.eli.version", &kernver, &size, 1936 NULL, 0) == -1) { 1937 warn("Unable to obtain GELI kernel version"); 1938 } else { 1939 printf("kernel: %u\n", kernver); 1940 } 1941 printf("userland: %u\n", G_ELI_VERSION); 1942 return; 1943 } 1944 1945 for (i = 0; i < nargs; i++) { 1946 name = gctl_get_ascii(req, "arg%d", i); 1947 error = g_metadata_read(name, (unsigned char *)&md, 1948 sizeof(md), G_ELI_MAGIC); 1949 if (error != 0) { 1950 warn("%s: Unable to read metadata: %s.", name, 1951 strerror(error)); 1952 gctl_error(req, "Not fully done."); 1953 continue; 1954 } 1955 eli_version = le32dec(&md.md_version); 1956 printf("%s: %u\n", name, eli_version); 1957 } 1958 } 1959 1960 static void 1961 eli_clear(struct gctl_req *req) 1962 { 1963 const char *name; 1964 int error, i, nargs; 1965 1966 nargs = gctl_get_int(req, "nargs"); 1967 if (nargs < 1) { 1968 gctl_error(req, "Too few arguments."); 1969 return; 1970 } 1971 1972 for (i = 0; i < nargs; i++) { 1973 name = gctl_get_ascii(req, "arg%d", i); 1974 error = g_metadata_clear(name, G_ELI_MAGIC); 1975 if (error != 0) { 1976 fprintf(stderr, "Cannot clear metadata on %s: %s.\n", 1977 name, strerror(error)); 1978 gctl_error(req, "Not fully done."); 1979 continue; 1980 } 1981 if (verbose) 1982 printf("Metadata cleared on %s.\n", name); 1983 } 1984 } 1985 1986 static void 1987 eli_dump(struct gctl_req *req) 1988 { 1989 struct g_eli_metadata md; 1990 const char *name; 1991 int i, nargs; 1992 1993 nargs = gctl_get_int(req, "nargs"); 1994 if (nargs < 1) { 1995 gctl_error(req, "Too few arguments."); 1996 return; 1997 } 1998 1999 for (i = 0; i < nargs; i++) { 2000 name = gctl_get_ascii(req, "arg%d", i); 2001 if (eli_metadata_read(NULL, name, &md) == -1) { 2002 gctl_error(req, "Not fully done."); 2003 continue; 2004 } 2005 printf("Metadata on %s:\n", name); 2006 eli_metadata_dump(&md); 2007 printf("\n"); 2008 } 2009 } 2010