2 * Copyright (c) 2004-2006 Pawel Jakub Dawidek <pjd@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
35 #include <readpassphrase.h>
43 #include <sys/param.h>
45 #include <sys/resource.h>
46 #include <opencrypto/cryptodev.h>
47 #include <geom/eli/g_eli.h>
48 #include <geom/eli/pkcs5v2.h>
50 #include "core/geom.h"
51 #include "misc/subr.h"
54 uint32_t lib_version = G_LIB_VERSION;
55 uint32_t version = G_ELI_VERSION;
57 static char aalgo[] = "none";
58 static char ealgo[] = "aes";
59 static intmax_t keylen = 0;
60 static intmax_t keyno = -1;
61 static intmax_t iterations = -1;
62 static intmax_t sectorsize = 0;
63 static char keyfile[] = "", newkeyfile[] = "";
65 static void eli_main(struct gctl_req *req, unsigned flags);
66 static void eli_init(struct gctl_req *req);
67 static void eli_attach(struct gctl_req *req);
68 static void eli_configure(struct gctl_req *req);
69 static void eli_setkey(struct gctl_req *req);
70 static void eli_delkey(struct gctl_req *req);
71 static void eli_kill(struct gctl_req *req);
72 static void eli_backup(struct gctl_req *req);
73 static void eli_restore(struct gctl_req *req);
74 static void eli_clear(struct gctl_req *req);
75 static void eli_dump(struct gctl_req *req);
80 * init [-bhPv] [-a aalgo] [-e ealgo] [-i iterations] [-l keylen] [-K newkeyfile] prov
81 * label - alias for 'init'
82 * attach [-dprv] [-k keyfile] prov
83 * detach [-fl] prov ...
84 * stop - alias for 'detach'
85 * onetime [-d] [-a aalgo] [-e ealgo] [-l keylen] prov ...
86 * configure [-bB] prov ...
87 * setkey [-pPv] [-n keyno] [-k keyfile] [-K newkeyfile] prov
88 * delkey [-afv] [-n keyno] prov
89 * kill [-av] [prov ...]
90 * backup [-v] prov file
91 * restore [-v] file prov
95 struct g_command class_commands[] = {
96 { "init", G_FLAG_VERBOSE, eli_main,
98 { 'a', "aalgo", aalgo, G_TYPE_STRING },
99 { 'b', "boot", NULL, G_TYPE_BOOL },
100 { 'e', "ealgo", ealgo, G_TYPE_STRING },
101 { 'i', "iterations", &iterations, G_TYPE_NUMBER },
102 { 'K', "newkeyfile", newkeyfile, G_TYPE_STRING },
103 { 'l', "keylen", &keylen, G_TYPE_NUMBER },
104 { 'P', "nonewpassphrase", NULL, G_TYPE_BOOL },
105 { 's', "sectorsize", §orsize, G_TYPE_NUMBER },
108 "[-bPv] [-a aalgo] [-e ealgo] [-i iterations] [-l keylen] [-K newkeyfile] [-s sectorsize] prov"
110 { "label", G_FLAG_VERBOSE, eli_main,
112 { 'a', "aalgo", aalgo, G_TYPE_STRING },
113 { 'b', "boot", NULL, G_TYPE_BOOL },
114 { 'e', "ealgo", ealgo, G_TYPE_STRING },
115 { 'i', "iterations", &iterations, G_TYPE_NUMBER },
116 { 'K', "newkeyfile", newkeyfile, G_TYPE_STRING },
117 { 'l', "keylen", &keylen, G_TYPE_NUMBER },
118 { 'P', "nonewpassphrase", NULL, G_TYPE_BOOL },
119 { 's', "sectorsize", §orsize, G_TYPE_NUMBER },
122 "- an alias for 'init'"
124 { "attach", G_FLAG_VERBOSE | G_FLAG_LOADKLD, eli_main,
126 { 'd', "detach", NULL, G_TYPE_BOOL },
127 { 'k', "keyfile", keyfile, G_TYPE_STRING },
128 { 'p', "nopassphrase", NULL, G_TYPE_BOOL },
129 { 'r', "readonly", NULL, G_TYPE_BOOL },
132 "[-dprv] [-k keyfile] prov"
136 { 'f', "force", NULL, G_TYPE_BOOL },
137 { 'l', "last", NULL, G_TYPE_BOOL },
144 { 'f', "force", NULL, G_TYPE_BOOL },
145 { 'l', "last", NULL, G_TYPE_BOOL },
148 "- an alias for 'detach'"
150 { "onetime", G_FLAG_VERBOSE | G_FLAG_LOADKLD, NULL,
152 { 'a', "aalgo", aalgo, G_TYPE_STRING },
153 { 'd', "detach", NULL, G_TYPE_BOOL },
154 { 'e', "ealgo", ealgo, G_TYPE_STRING },
155 { 'l', "keylen", &keylen, G_TYPE_NUMBER },
156 { 's', "sectorsize", §orsize, G_TYPE_NUMBER },
159 "[-d] [-a aalgo] [-e ealgo] [-l keylen] [-s sectorsize] prov ..."
161 { "configure", G_FLAG_VERBOSE, eli_main,
163 { 'b', "boot", NULL, G_TYPE_BOOL },
164 { 'B', "noboot", NULL, G_TYPE_BOOL },
169 { "setkey", G_FLAG_VERBOSE, eli_main,
171 { 'i', "iterations", &iterations, G_TYPE_NUMBER },
172 { 'k', "keyfile", keyfile, G_TYPE_STRING },
173 { 'K', "newkeyfile", newkeyfile, G_TYPE_STRING },
174 { 'n', "keyno", &keyno, G_TYPE_NUMBER },
175 { 'p', "nopassphrase", NULL, G_TYPE_BOOL },
176 { 'P', "nonewpassphrase", NULL, G_TYPE_BOOL },
179 "[-pPv] [-n keyno] [-i iterations] [-k keyfile] [-K newkeyfile] prov"
181 { "delkey", G_FLAG_VERBOSE, eli_main,
183 { 'a', "all", NULL, G_TYPE_BOOL },
184 { 'f', "force", NULL, G_TYPE_BOOL },
185 { 'n', "keyno", &keyno, G_TYPE_NUMBER },
188 "[-afv] [-n keyno] prov"
190 { "kill", G_FLAG_VERBOSE, eli_main,
192 { 'a', "all", NULL, G_TYPE_BOOL },
197 { "backup", G_FLAG_VERBOSE, eli_main, G_NULL_OPTS,
200 { "restore", G_FLAG_VERBOSE, eli_main, G_NULL_OPTS,
203 { "clear", G_FLAG_VERBOSE, eli_main, G_NULL_OPTS,
206 { "dump", G_FLAG_VERBOSE, eli_main, G_NULL_OPTS,
212 static int verbose = 0;
215 eli_protect(struct gctl_req *req)
219 /* Disable core dumps. */
222 if (setrlimit(RLIMIT_CORE, &rl) == -1) {
223 gctl_error(req, "Cannot disable core dumps: %s.",
227 /* Disable swapping. */
228 if (mlockall(MCL_FUTURE) == -1) {
229 gctl_error(req, "Cannot lock memory: %s.", strerror(errno));
236 eli_main(struct gctl_req *req, unsigned flags)
240 if (eli_protect(req) == -1)
243 if ((flags & G_FLAG_VERBOSE) != 0)
246 name = gctl_get_ascii(req, "verb");
248 gctl_error(req, "No '%s' argument.", "verb");
251 if (strcmp(name, "init") == 0 || strcmp(name, "label") == 0)
253 else if (strcmp(name, "attach") == 0)
255 else if (strcmp(name, "configure") == 0)
257 else if (strcmp(name, "setkey") == 0)
259 else if (strcmp(name, "delkey") == 0)
261 else if (strcmp(name, "kill") == 0)
263 else if (strcmp(name, "backup") == 0)
265 else if (strcmp(name, "restore") == 0)
267 else if (strcmp(name, "dump") == 0)
269 else if (strcmp(name, "clear") == 0)
272 gctl_error(req, "Unknown command: %s.", name);
276 arc4rand(unsigned char *buf, size_t size)
282 buf4 = (uint32_t *)buf;
285 for (i = 0; i < size4; i++)
286 buf4[i] = arc4random();
287 for (i *= 4; i < size; i++)
288 buf[i] = arc4random() % 0xff;
292 eli_is_attached(const char *prov)
294 char name[MAXPATHLEN];
298 * Not the best way to do it, but the easiest.
299 * We try to open provider and check if it is a GEOM provider
300 * by asking about its sectorsize.
302 snprintf(name, sizeof(name), "%s%s", prov, G_ELI_SUFFIX);
303 secsize = g_get_sectorsize(name);
309 static unsigned char *
310 eli_genkey(struct gctl_req *req, struct g_eli_metadata *md, unsigned char *key,
315 int error, nopassphrase;
318 gctl_get_int(req, new ? "nonewpassphrase" : "nopassphrase");
320 g_eli_crypto_hmac_init(&ctx, NULL, 0);
322 str = gctl_get_ascii(req, new ? "newkeyfile" : "keyfile");
323 if (str[0] == '\0' && nopassphrase) {
324 gctl_error(req, "No key components given.");
326 } else if (str[0] != '\0') {
331 if (strcmp(str, "-") == 0)
334 fd = open(str, O_RDONLY);
336 gctl_error(req, "Cannot open keyfile %s: %s.",
337 str, strerror(errno));
341 while ((done = read(fd, buf, sizeof(buf))) > 0)
342 g_eli_crypto_hmac_update(&ctx, buf, done);
344 if (strcmp(str, "-") != 0)
346 bzero(buf, sizeof(buf));
348 gctl_error(req, "Cannot read keyfile %s: %s.", str,
355 char buf1[BUFSIZ], buf2[BUFSIZ], *p;
357 if (!new && md->md_iterations == -1) {
358 gctl_error(req, "Missing -p flag.");
363 new ? "Enter new passphrase:" : "Enter passphrase:",
364 buf1, sizeof(buf1), RPP_ECHO_OFF | RPP_REQUIRE_TTY);
366 bzero(buf1, sizeof(buf1));
367 gctl_error(req, "Cannot read passphrase: %s.",
373 p = readpassphrase("Reenter new passphrase: ",
375 RPP_ECHO_OFF | RPP_REQUIRE_TTY);
377 bzero(buf1, sizeof(buf1));
379 "Cannot read passphrase: %s.",
384 if (strcmp(buf1, buf2) != 0) {
385 bzero(buf2, sizeof(buf2));
386 fprintf(stderr, "They didn't match.\n");
389 bzero(buf2, sizeof(buf2));
394 * Field md_iterations equal to -1 means "choose some sane
397 if (md->md_iterations == -1) {
400 printf("Calculating number of iterations...\n");
401 md->md_iterations = pkcs5v2_calculate(2000000);
402 assert(md->md_iterations > 0);
404 printf("Done, using %d iterations.\n",
409 * If md_iterations is equal to 0, user don't want PKCS#5v2.
411 if (md->md_iterations == 0) {
412 g_eli_crypto_hmac_update(&ctx, md->md_salt,
413 sizeof(md->md_salt));
414 g_eli_crypto_hmac_update(&ctx, buf1, strlen(buf1));
415 } else /* if (md->md_iterations > 0) */ {
416 unsigned char dkey[G_ELI_USERKEYLEN];
418 pkcs5v2_genkey(dkey, sizeof(dkey), md->md_salt,
419 sizeof(md->md_salt), buf1, md->md_iterations);
420 g_eli_crypto_hmac_update(&ctx, dkey, sizeof(dkey));
421 bzero(dkey, sizeof(dkey));
423 bzero(buf1, sizeof(buf1));
425 g_eli_crypto_hmac_final(&ctx, key, 0);
430 eli_metadata_read(struct gctl_req *req, const char *prov,
431 struct g_eli_metadata *md)
433 unsigned char sector[sizeof(struct g_eli_metadata)];
436 if (g_get_sectorsize(prov) == 0) {
439 /* This is a file probably. */
440 fd = open(prov, O_RDONLY);
442 gctl_error(req, "Cannot open %s: %s.", prov,
446 if (read(fd, sector, sizeof(sector)) != sizeof(sector)) {
447 gctl_error(req, "Cannot read metadata from %s: %s.",
448 prov, strerror(errno));
454 /* This is a GEOM provider. */
455 error = g_metadata_read(prov, sector, sizeof(sector),
458 gctl_error(req, "Cannot read metadata from %s: %s.",
459 prov, strerror(error));
463 if (eli_metadata_decode(sector, md) != 0) {
464 gctl_error(req, "MD5 hash mismatch for %s.", prov);
471 eli_metadata_store(struct gctl_req *req, const char *prov,
472 struct g_eli_metadata *md)
474 unsigned char sector[sizeof(struct g_eli_metadata)];
477 eli_metadata_encode(md, sector);
478 if (g_get_sectorsize(prov) == 0) {
481 /* This is a file probably. */
482 fd = open(prov, O_WRONLY | O_TRUNC);
484 gctl_error(req, "Cannot open %s: %s.", prov,
486 bzero(sector, sizeof(sector));
489 if (write(fd, sector, sizeof(sector)) != sizeof(sector)) {
490 gctl_error(req, "Cannot write metadata to %s: %s.",
491 prov, strerror(errno));
492 bzero(sector, sizeof(sector));
498 /* This is a GEOM provider. */
499 error = g_metadata_store(prov, sector, sizeof(sector));
501 gctl_error(req, "Cannot write metadata to %s: %s.",
502 prov, strerror(errno));
503 bzero(sector, sizeof(sector));
507 bzero(sector, sizeof(sector));
512 eli_init(struct gctl_req *req)
514 struct g_eli_metadata md;
515 unsigned char sector[sizeof(struct g_eli_metadata)];
516 unsigned char key[G_ELI_USERKEYLEN];
517 const char *str, *prov;
523 nargs = gctl_get_int(req, "nargs");
525 gctl_error(req, "Invalid number of arguments.");
528 prov = gctl_get_ascii(req, "arg0");
529 mediasize = g_get_mediasize(prov);
530 secsize = g_get_sectorsize(prov);
531 if (mediasize == 0 || secsize == 0) {
532 gctl_error(req, "Cannot get informations about %s: %s.", prov,
537 bzero(&md, sizeof(md));
538 strlcpy(md.md_magic, G_ELI_MAGIC, sizeof(md.md_magic));
539 md.md_version = G_ELI_VERSION;
541 if (gctl_get_int(req, "boot"))
542 md.md_flags |= G_ELI_FLAG_BOOT;
543 md.md_ealgo = CRYPTO_ALGORITHM_MIN - 1;
544 str = gctl_get_ascii(req, "aalgo");
545 if (strcmp(str, "none") != 0) {
546 md.md_aalgo = g_eli_str2aalgo(str);
547 if (md.md_aalgo >= CRYPTO_ALGORITHM_MIN &&
548 md.md_aalgo <= CRYPTO_ALGORITHM_MAX) {
549 md.md_flags |= G_ELI_FLAG_AUTH;
552 * For backward compatibility, check if the -a option
553 * was used to provide encryption algorithm.
555 md.md_ealgo = g_eli_str2ealgo(str);
556 if (md.md_ealgo < CRYPTO_ALGORITHM_MIN ||
557 md.md_ealgo > CRYPTO_ALGORITHM_MAX) {
559 "Invalid authentication algorithm.");
562 fprintf(stderr, "warning: The -e option, not "
563 "the -a option is now used to specify "
564 "encryption algorithm to use.\n");
568 if (md.md_ealgo < CRYPTO_ALGORITHM_MIN ||
569 md.md_ealgo > CRYPTO_ALGORITHM_MAX) {
570 str = gctl_get_ascii(req, "ealgo");
571 md.md_ealgo = g_eli_str2ealgo(str);
572 if (md.md_ealgo < CRYPTO_ALGORITHM_MIN ||
573 md.md_ealgo > CRYPTO_ALGORITHM_MAX) {
574 gctl_error(req, "Invalid encryption algorithm.");
578 val = gctl_get_intmax(req, "keylen");
580 md.md_keylen = g_eli_keylen(md.md_ealgo, md.md_keylen);
581 if (md.md_keylen == 0) {
582 gctl_error(req, "Invalid key length.");
585 md.md_provsize = mediasize;
587 val = gctl_get_intmax(req, "iterations");
592 * Don't allow to set iterations when there will be no
595 nonewpassphrase = gctl_get_int(req, "nonewpassphrase");
596 if (nonewpassphrase) {
598 "Options -i and -P are mutually exclusive.");
602 md.md_iterations = val;
604 val = gctl_get_intmax(req, "sectorsize");
606 md.md_sectorsize = secsize;
608 if (val < 0 || (val % secsize) != 0) {
609 gctl_error(req, "Invalid sector size.");
612 md.md_sectorsize = val;
616 arc4rand(md.md_salt, sizeof(md.md_salt));
617 arc4rand(md.md_mkeys, sizeof(md.md_mkeys));
619 /* Generate user key. */
620 if (eli_genkey(req, &md, key, 1) == NULL) {
621 bzero(key, sizeof(key));
622 bzero(&md, sizeof(md));
626 /* Encrypt the first and the only Master Key. */
627 error = g_eli_mkey_encrypt(md.md_ealgo, key, md.md_keylen, md.md_mkeys);
628 bzero(key, sizeof(key));
630 bzero(&md, sizeof(md));
631 gctl_error(req, "Cannot encrypt Master Key: %s.",
636 eli_metadata_encode(&md, sector);
637 bzero(&md, sizeof(md));
638 error = g_metadata_store(prov, sector, sizeof(sector));
639 bzero(sector, sizeof(sector));
641 gctl_error(req, "Cannot store metadata on %s: %s.", prov,
646 printf("Metadata value stored on %s.\n", prov);
650 eli_attach(struct gctl_req *req)
652 struct g_eli_metadata md;
653 unsigned char key[G_ELI_USERKEYLEN];
657 nargs = gctl_get_int(req, "nargs");
659 gctl_error(req, "Invalid number of arguments.");
662 prov = gctl_get_ascii(req, "arg0");
664 if (eli_metadata_read(req, prov, &md) == -1)
667 if (eli_genkey(req, &md, key, 0) == NULL) {
668 bzero(key, sizeof(key));
672 gctl_ro_param(req, "key", sizeof(key), key);
673 if (gctl_issue(req) == NULL) {
675 printf("Attched to %s.\n", prov);
677 bzero(key, sizeof(key));
681 eli_configure_detached(struct gctl_req *req, const char *prov, int boot)
683 struct g_eli_metadata md;
685 if (eli_metadata_read(req, prov, &md) == -1)
688 if (boot && (md.md_flags & G_ELI_FLAG_BOOT)) {
690 printf("BOOT flag already configured for %s.\n", prov);
691 } else if (!boot && !(md.md_flags & G_ELI_FLAG_BOOT)) {
693 printf("BOOT flag not configured for %s.\n", prov);
696 md.md_flags |= G_ELI_FLAG_BOOT;
698 md.md_flags &= ~G_ELI_FLAG_BOOT;
699 eli_metadata_store(req, prov, &md);
701 bzero(&md, sizeof(md));
705 eli_configure(struct gctl_req *req)
708 int i, nargs, boot, noboot;
710 nargs = gctl_get_int(req, "nargs");
712 gctl_error(req, "Too few arguments.");
716 boot = gctl_get_int(req, "boot");
717 noboot = gctl_get_int(req, "noboot");
719 if (boot && noboot) {
720 gctl_error(req, "Options -b and -B are mutually exclusive.");
723 if (!boot && !noboot) {
724 gctl_error(req, "No option given.");
728 /* First attached providers. */
731 for (i = 0; i < nargs; i++) {
732 prov = gctl_get_ascii(req, "arg%d", i);
733 if (!eli_is_attached(prov))
734 eli_configure_detached(req, prov, boot);
739 eli_setkey_attached(struct gctl_req *req, struct g_eli_metadata *md)
741 unsigned char key[G_ELI_USERKEYLEN];
744 val = gctl_get_intmax(req, "iterations");
745 /* Check if iterations number should be changed. */
747 md->md_iterations = val;
749 /* Generate key for Master Key encryption. */
750 if (eli_genkey(req, md, key, 1) == NULL) {
751 bzero(key, sizeof(key));
755 gctl_ro_param(req, "key", sizeof(key), key);
757 bzero(key, sizeof(key));
761 eli_setkey_detached(struct gctl_req *req, const char *prov,
762 struct g_eli_metadata *md)
764 unsigned char key[G_ELI_USERKEYLEN], mkey[G_ELI_DATAIVKEYLEN];
765 unsigned char *mkeydst;
770 if (md->md_keys == 0) {
771 gctl_error(req, "No valid keys on %s.", prov);
775 /* Generate key for Master Key decryption. */
776 if (eli_genkey(req, md, key, 0) == NULL) {
777 bzero(key, sizeof(key));
781 /* Decrypt Master Key. */
782 error = g_eli_mkey_decrypt(md, key, mkey, &nkey);
783 bzero(key, sizeof(key));
785 bzero(md, sizeof(*md));
787 gctl_error(req, "Wrong key for %s.", prov);
788 else /* if (error > 0) */ {
789 gctl_error(req, "Cannot decrypt Master Key: %s.",
795 printf("Decrypted Master Key %u.\n", nkey);
797 val = gctl_get_intmax(req, "keyno");
802 ; /* Use the key number which was found during decryption. */
804 if (nkey >= G_ELI_MAXMKEYS) {
805 gctl_error(req, "Invalid '%s' argument.", "keyno");
809 val = gctl_get_intmax(req, "iterations");
810 /* Check if iterations number should and can be changed. */
812 if (bitcount32(md->md_keys) != 1) {
813 gctl_error(req, "To be able to use '-i' option, only "
814 "one key can be defined.");
817 if (md->md_keys != (1 << nkey)) {
818 gctl_error(req, "Only already defined key can be "
819 "changed when '-i' option is used.");
822 md->md_iterations = val;
825 mkeydst = md->md_mkeys + nkey * G_ELI_MKEYLEN;
826 md->md_keys |= (1 << nkey);
828 bcopy(mkey, mkeydst, sizeof(mkey));
829 bzero(mkey, sizeof(mkey));
831 /* Generate key for Master Key encryption. */
832 if (eli_genkey(req, md, key, 1) == NULL) {
833 bzero(key, sizeof(key));
834 bzero(md, sizeof(*md));
838 /* Encrypt the Master-Key with the new key. */
839 error = g_eli_mkey_encrypt(md->md_ealgo, key, md->md_keylen, mkeydst);
840 bzero(key, sizeof(key));
842 bzero(md, sizeof(*md));
843 gctl_error(req, "Cannot encrypt Master Key: %s.",
848 /* Store metadata with fresh key. */
849 eli_metadata_store(req, prov, md);
850 bzero(md, sizeof(*md));
854 eli_setkey(struct gctl_req *req)
856 struct g_eli_metadata md;
860 nargs = gctl_get_int(req, "nargs");
862 gctl_error(req, "Invalid number of arguments.");
865 prov = gctl_get_ascii(req, "arg0");
867 if (eli_metadata_read(req, prov, &md) == -1)
870 if (eli_is_attached(prov))
871 eli_setkey_attached(req, &md);
873 eli_setkey_detached(req, prov, &md);
877 eli_delkey_attached(struct gctl_req *req, const char *prov __unused)
884 eli_delkey_detached(struct gctl_req *req, const char *prov)
886 struct g_eli_metadata md;
887 unsigned char *mkeydst;
892 if (eli_metadata_read(req, prov, &md) == -1)
895 all = gctl_get_int(req, "all");
897 arc4rand(md.md_mkeys, sizeof(md.md_mkeys));
899 force = gctl_get_int(req, "force");
900 val = gctl_get_intmax(req, "keyno");
902 gctl_error(req, "Key number has to be specified.");
906 if (nkey >= G_ELI_MAXMKEYS) {
907 gctl_error(req, "Invalid '%s' argument.", "keyno");
910 if (!(md.md_keys & (1 << nkey)) && !force) {
911 gctl_error(req, "Master Key %u is not set.", nkey);
914 md.md_keys &= ~(1 << nkey);
915 if (md.md_keys == 0 && !force) {
916 gctl_error(req, "This is the last Master Key. Use '-f' "
917 "option if you really want to remove it.");
920 mkeydst = md.md_mkeys + nkey * G_ELI_MKEYLEN;
921 arc4rand(mkeydst, G_ELI_MKEYLEN);
924 eli_metadata_store(req, prov, &md);
925 bzero(&md, sizeof(md));
929 eli_delkey(struct gctl_req *req)
934 nargs = gctl_get_int(req, "nargs");
936 gctl_error(req, "Invalid number of arguments.");
939 prov = gctl_get_ascii(req, "arg0");
941 if (eli_is_attached(prov))
942 eli_delkey_attached(req, prov);
944 eli_delkey_detached(req, prov);
948 eli_kill_detached(struct gctl_req *req, const char *prov)
950 struct g_eli_metadata md;
954 * NOTE: Maybe we should verify if this is geli provider first,
955 * but 'kill' command is quite critical so better don't waste
959 error = g_metadata_read(prov, (unsigned char *)&md, sizeof(md),
962 gctl_error(req, "Cannot read metadata from %s: %s.", prov,
968 arc4rand((unsigned char *)&md, sizeof(md));
969 error = g_metadata_store(prov, (unsigned char *)&md, sizeof(md));
971 gctl_error(req, "Cannot write metadata to %s: %s.", prov,
977 eli_kill(struct gctl_req *req)
982 nargs = gctl_get_int(req, "nargs");
983 all = gctl_get_int(req, "all");
984 if (!all && nargs == 0) {
985 gctl_error(req, "Too few arguments.");
989 * How '-a' option combine with a list of providers:
990 * Delete Master Keys from all attached providers:
992 * Delete Master Keys from all attached provider and from
993 * detached da0 and da1:
994 * geli kill -a da0 da1
995 * Delete Master Keys from (attached or detached) da0 and da1:
999 /* First detached provider. */
1000 for (i = 0; i < nargs; i++) {
1001 prov = gctl_get_ascii(req, "arg%d", i);
1002 if (!eli_is_attached(prov))
1003 eli_kill_detached(req, prov);
1005 /* Now attached providers. */
1010 eli_backup(struct gctl_req *req)
1012 struct g_eli_metadata md;
1013 const char *file, *prov;
1015 unsigned char *sector;
1017 int nargs, filefd, provfd;
1019 nargs = gctl_get_int(req, "nargs");
1021 gctl_error(req, "Invalid number of arguments.");
1024 prov = gctl_get_ascii(req, "arg0");
1025 file = gctl_get_ascii(req, "arg1");
1027 provfd = filefd = -1;
1031 provfd = open(prov, O_RDONLY);
1032 if (provfd == -1 && errno == ENOENT && prov[0] != '/') {
1033 char devprov[MAXPATHLEN];
1035 snprintf(devprov, sizeof(devprov), "%s%s", _PATH_DEV, prov);
1036 provfd = open(devprov, O_RDONLY);
1039 gctl_error(req, "Cannot open %s: %s.", prov, strerror(errno));
1042 filefd = open(file, O_WRONLY | O_TRUNC | O_CREAT, 0600);
1044 gctl_error(req, "Cannot open %s: %s.", file, strerror(errno));
1048 mediasize = g_get_mediasize(prov);
1049 secsize = g_get_sectorsize(prov);
1050 if (mediasize == 0 || secsize == 0) {
1051 gctl_error(req, "Cannot get informations about %s: %s.", prov,
1056 sector = malloc(secsize);
1057 if (sector == NULL) {
1058 gctl_error(req, "Cannot allocate memory.");
1062 /* Read metadata from the provider. */
1063 if (pread(provfd, sector, secsize, mediasize - secsize) !=
1065 gctl_error(req, "Cannot read metadata: %s.", strerror(errno));
1068 /* Check if this is geli provider. */
1069 if (eli_metadata_decode(sector, &md) != 0) {
1070 gctl_error(req, "MD5 hash mismatch: not a geli provider?");
1073 /* Write metadata to the destination file. */
1074 if (write(filefd, sector, secsize) != (ssize_t)secsize) {
1075 gctl_error(req, "Cannot write to %s: %s.", file,
1084 if (sector != NULL) {
1085 bzero(sector, secsize);
1091 eli_restore(struct gctl_req *req)
1093 struct g_eli_metadata md;
1094 const char *file, *prov;
1095 unsigned char *sector;
1098 int nargs, filefd, provfd;
1100 nargs = gctl_get_int(req, "nargs");
1102 gctl_error(req, "Invalid number of arguments.");
1105 file = gctl_get_ascii(req, "arg0");
1106 prov = gctl_get_ascii(req, "arg1");
1108 provfd = filefd = -1;
1112 filefd = open(file, O_RDONLY);
1114 gctl_error(req, "Cannot open %s: %s.", file, strerror(errno));
1117 provfd = open(prov, O_WRONLY);
1118 if (provfd == -1 && errno == ENOENT && prov[0] != '/') {
1119 char devprov[MAXPATHLEN];
1121 snprintf(devprov, sizeof(devprov), "%s%s", _PATH_DEV, prov);
1122 provfd = open(devprov, O_WRONLY);
1125 gctl_error(req, "Cannot open %s: %s.", prov, strerror(errno));
1129 mediasize = g_get_mediasize(prov);
1130 secsize = g_get_sectorsize(prov);
1131 if (mediasize == 0 || secsize == 0) {
1132 gctl_error(req, "Cannot get informations about %s: %s.", prov,
1137 sector = malloc(secsize);
1138 if (sector == NULL) {
1139 gctl_error(req, "Cannot allocate memory.");
1143 /* Read metadata from the backup file. */
1144 if (read(filefd, sector, secsize) != (ssize_t)secsize) {
1145 gctl_error(req, "Cannot read from %s: %s.", file,
1149 /* Check if this file contains geli metadata. */
1150 if (eli_metadata_decode(sector, &md) != 0) {
1151 gctl_error(req, "MD5 hash mismatch: not a geli backup file?");
1154 /* Write metadata from the provider. */
1155 if (pwrite(provfd, sector, secsize, mediasize - secsize) !=
1157 gctl_error(req, "Cannot write metadata: %s.", strerror(errno));
1165 if (sector != NULL) {
1166 bzero(sector, secsize);
1172 eli_clear(struct gctl_req *req)
1175 int error, i, nargs;
1177 nargs = gctl_get_int(req, "nargs");
1179 gctl_error(req, "Too few arguments.");
1183 for (i = 0; i < nargs; i++) {
1184 name = gctl_get_ascii(req, "arg%d", i);
1185 error = g_metadata_clear(name, G_ELI_MAGIC);
1187 fprintf(stderr, "Cannot clear metadata on %s: %s.\n",
1188 name, strerror(error));
1189 gctl_error(req, "Not fully done.");
1193 printf("Metadata cleared on %s.\n", name);
1198 eli_dump(struct gctl_req *req)
1200 struct g_eli_metadata md, tmpmd;
1202 int error, i, nargs;
1204 nargs = gctl_get_int(req, "nargs");
1206 gctl_error(req, "Too few arguments.");
1210 for (i = 0; i < nargs; i++) {
1211 name = gctl_get_ascii(req, "arg%d", i);
1212 error = g_metadata_read(name, (unsigned char *)&tmpmd,
1213 sizeof(tmpmd), G_ELI_MAGIC);
1215 fprintf(stderr, "Cannot read metadata from %s: %s.\n",
1216 name, strerror(error));
1217 gctl_error(req, "Not fully done.");
1220 if (eli_metadata_decode((unsigned char *)&tmpmd, &md) != 0) {
1221 fprintf(stderr, "MD5 hash mismatch for %s, skipping.\n",
1223 gctl_error(req, "Not fully done.");
1226 printf("Metadata on %s:\n", name);
1227 eli_metadata_dump(&md);