2002-10-20 11:16:13 +00:00
|
|
|
/*-
|
2017-11-27 15:37:16 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD
|
|
|
|
*
|
2002-10-20 11:16:13 +00:00
|
|
|
* Copyright (c) 2002 Poul-Henning Kamp
|
|
|
|
* Copyright (c) 2002 Networks Associates Technology, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This software was developed for the FreeBSD Project by Poul-Henning Kamp
|
|
|
|
* and NAI Labs, the Security Research Division of Network Associates, Inc.
|
|
|
|
* under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
|
|
|
|
* DARPA CHATS research program.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* $FreeBSD$
|
2003-10-13 20:14:02 +00:00
|
|
|
*
|
|
|
|
* XXX: Future stuff
|
2004-02-03 11:12:29 +00:00
|
|
|
*
|
2003-10-13 20:14:02 +00:00
|
|
|
* Replace the template file options (-i & -f) with command-line variables
|
|
|
|
* "-v property=foo"
|
|
|
|
*
|
|
|
|
* Introduce -e, extra entropy source (XOR with /dev/random)
|
|
|
|
*
|
|
|
|
* Introduce -E, alternate entropy source (instead of /dev/random)
|
|
|
|
*
|
2004-02-03 11:12:29 +00:00
|
|
|
* Introduce -i take IV from keyboard or
|
2003-10-13 20:14:02 +00:00
|
|
|
*
|
|
|
|
* Introduce -I take IV from file/cmd
|
|
|
|
*
|
|
|
|
* Introduce -m/-M store encrypted+encoded masterkey in file
|
|
|
|
*
|
|
|
|
* Introduce -k/-K get pass-phrase part from file/cmd
|
|
|
|
*
|
|
|
|
* Introduce -d add more dest-devices to worklist.
|
|
|
|
*
|
|
|
|
* Add key-option: selfdestruct bit.
|
|
|
|
*
|
|
|
|
* New/changed verbs:
|
|
|
|
* "onetime" attach with onetime nonstored locksector
|
|
|
|
* "key"/"unkey" to blast memory copy of key without orphaning
|
|
|
|
* "nuke" blow away everything attached, crash/halt/power-off if possible.
|
|
|
|
* "blast" destroy all copies of the masterkey
|
|
|
|
* "destroy" destroy one copy of the masterkey
|
|
|
|
* "backup"/"restore" of masterkey sectors.
|
|
|
|
*
|
|
|
|
* Make all verbs work on both attached/detached devices.
|
|
|
|
*
|
2002-10-20 11:16:13 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/queue.h>
|
|
|
|
#include <sys/mutex.h>
|
|
|
|
#include <md5.h>
|
|
|
|
#include <readpassphrase.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
2002-12-01 15:58:28 +00:00
|
|
|
#include <paths.h>
|
2002-10-20 11:16:13 +00:00
|
|
|
#include <strings.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <err.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <libutil.h>
|
2003-03-31 18:38:31 +00:00
|
|
|
#include <libgeom.h>
|
2002-10-20 11:16:13 +00:00
|
|
|
#include <sys/errno.h>
|
|
|
|
#include <sys/disk.h>
|
2002-11-04 09:27:01 +00:00
|
|
|
#include <sys/stat.h>
|
2005-03-11 22:07:04 +00:00
|
|
|
#include <crypto/rijndael/rijndael-api-fst.h>
|
2015-12-27 17:33:59 +00:00
|
|
|
#include <crypto/sha2/sha512.h>
|
2003-10-07 09:29:59 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/linker.h>
|
2002-11-04 09:27:01 +00:00
|
|
|
|
2003-10-07 09:29:59 +00:00
|
|
|
#define GBDEMOD "geom_bde"
|
2002-11-04 09:27:01 +00:00
|
|
|
#define KASSERT(foo, bar) do { if(!(foo)) { warn bar ; exit (1); } } while (0)
|
2002-10-20 11:16:13 +00:00
|
|
|
|
|
|
|
#include <geom/geom.h>
|
|
|
|
#include <geom/bde/g_bde.h>
|
|
|
|
|
|
|
|
extern const char template[];
|
|
|
|
|
2002-11-04 09:27:01 +00:00
|
|
|
|
|
|
|
#if 0
|
|
|
|
static void
|
|
|
|
g_hexdump(void *ptr, int length)
|
|
|
|
{
|
|
|
|
int i, j, k;
|
|
|
|
unsigned char *cp;
|
|
|
|
|
|
|
|
cp = ptr;
|
|
|
|
for (i = 0; i < length; i+= 16) {
|
|
|
|
printf("%04x ", i);
|
|
|
|
for (j = 0; j < 16; j++) {
|
|
|
|
k = i + j;
|
|
|
|
if (k < length)
|
|
|
|
printf(" %02x", cp[k]);
|
|
|
|
else
|
|
|
|
printf(" ");
|
|
|
|
}
|
|
|
|
printf(" |");
|
|
|
|
for (j = 0; j < 16; j++) {
|
|
|
|
k = i + j;
|
|
|
|
if (k >= length)
|
|
|
|
printf(" ");
|
|
|
|
else if (cp[k] >= ' ' && cp[k] <= '~')
|
|
|
|
printf("%c", cp[k]);
|
|
|
|
else
|
|
|
|
printf(".");
|
|
|
|
}
|
|
|
|
printf("|\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2002-10-20 11:16:13 +00:00
|
|
|
static void __dead2
|
2005-02-12 21:47:05 +00:00
|
|
|
usage(void)
|
2002-10-20 11:16:13 +00:00
|
|
|
{
|
|
|
|
|
2005-02-12 21:47:05 +00:00
|
|
|
(void)fprintf(stderr,
|
2006-02-08 06:52:15 +00:00
|
|
|
"usage: gbde attach destination [-k keyfile] [-l lockfile] [-p pass-phrase]\n"
|
2005-02-12 21:47:05 +00:00
|
|
|
" gbde detach destination\n"
|
2006-02-08 06:52:15 +00:00
|
|
|
" gbde init destination [-i] [-f filename] [-K new-keyfile]\n"
|
2005-02-12 21:47:05 +00:00
|
|
|
" [-L new-lockfile] [-P new-pass-phrase]\n"
|
2006-02-08 06:52:15 +00:00
|
|
|
" gbde setkey destination [-n key]\n"
|
|
|
|
" [-k keyfile] [-l lockfile] [-p pass-phrase]\n"
|
|
|
|
" [-K new-keyfile] [-L new-lockfile] [-P new-pass-phrase]\n"
|
|
|
|
" gbde nuke destination [-n key]\n"
|
|
|
|
" [-k keyfile] [-l lockfile] [-p pass-phrase]\n"
|
|
|
|
" gbde destroy destination [-k keyfile] [-l lockfile] [-p pass-phrase]\n");
|
2005-02-12 21:47:05 +00:00
|
|
|
exit(1);
|
2002-10-20 11:16:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
|
|
|
g_read_data(struct g_consumer *cp, off_t offset, off_t length, int *error)
|
|
|
|
{
|
|
|
|
void *p;
|
|
|
|
int fd, i;
|
|
|
|
off_t o2;
|
|
|
|
|
|
|
|
p = malloc(length);
|
|
|
|
if (p == NULL)
|
|
|
|
err(1, "malloc");
|
|
|
|
fd = *(int *)cp;
|
|
|
|
o2 = lseek(fd, offset, SEEK_SET);
|
|
|
|
if (o2 != offset)
|
|
|
|
err(1, "lseek");
|
|
|
|
i = read(fd, p, length);
|
|
|
|
if (i != length)
|
|
|
|
err(1, "read");
|
|
|
|
if (error != NULL)
|
|
|
|
error = 0;
|
|
|
|
return (p);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
random_bits(void *p, u_int len)
|
|
|
|
{
|
2019-02-11 00:11:02 +00:00
|
|
|
arc4random_buf(p, len);
|
2002-10-20 11:16:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* XXX: not nice */
|
2002-11-04 09:27:01 +00:00
|
|
|
static u_char sha2[SHA512_DIGEST_LENGTH];
|
2002-10-20 11:16:13 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
reset_passphrase(struct g_bde_softc *sc)
|
|
|
|
{
|
|
|
|
|
2002-11-04 09:27:01 +00:00
|
|
|
memcpy(sc->sha2, sha2, SHA512_DIGEST_LENGTH);
|
2002-10-20 11:16:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2006-02-08 06:52:15 +00:00
|
|
|
setup_passphrase(struct g_bde_softc *sc, int sure, const char *input,
|
|
|
|
const char *keyfile)
|
2002-10-20 11:16:13 +00:00
|
|
|
{
|
2006-02-08 06:52:15 +00:00
|
|
|
char buf1[BUFSIZ + SHA512_DIGEST_LENGTH];
|
|
|
|
char buf2[BUFSIZ + SHA512_DIGEST_LENGTH];
|
|
|
|
char *p;
|
|
|
|
int kfd, klen, bpos = 0;
|
|
|
|
|
|
|
|
if (keyfile != NULL) {
|
|
|
|
/* Read up to BUFSIZ bytes from keyfile */
|
|
|
|
kfd = open(keyfile, O_RDONLY, 0);
|
|
|
|
if (kfd < 0)
|
|
|
|
err(1, "%s", keyfile);
|
|
|
|
klen = read(kfd, buf1, BUFSIZ);
|
|
|
|
if (klen == -1)
|
|
|
|
err(1, "%s", keyfile);
|
|
|
|
close(kfd);
|
|
|
|
|
|
|
|
/* Prepend the passphrase with the hash of the key read */
|
|
|
|
g_bde_hash_pass(sc, buf1, klen);
|
|
|
|
memcpy(buf1, sc->sha2, SHA512_DIGEST_LENGTH);
|
|
|
|
memcpy(buf2, sc->sha2, SHA512_DIGEST_LENGTH);
|
|
|
|
bpos = SHA512_DIGEST_LENGTH;
|
|
|
|
}
|
2002-10-20 11:16:13 +00:00
|
|
|
|
|
|
|
if (input != NULL) {
|
2006-02-08 06:52:15 +00:00
|
|
|
if (strlen(input) >= BUFSIZ)
|
|
|
|
errx(1, "Passphrase too long");
|
|
|
|
strcpy(buf1 + bpos, input);
|
|
|
|
|
|
|
|
g_bde_hash_pass(sc, buf1, strlen(buf1 + bpos) + bpos);
|
2002-11-04 09:27:01 +00:00
|
|
|
memcpy(sha2, sc->sha2, SHA512_DIGEST_LENGTH);
|
2002-10-20 11:16:13 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (;;) {
|
|
|
|
p = readpassphrase(
|
|
|
|
sure ? "Enter new passphrase:" : "Enter passphrase: ",
|
2006-02-08 06:52:15 +00:00
|
|
|
buf1 + bpos, sizeof buf1 - bpos,
|
2002-10-20 11:16:13 +00:00
|
|
|
RPP_ECHO_OFF | RPP_REQUIRE_TTY);
|
|
|
|
if (p == NULL)
|
|
|
|
err(1, "readpassphrase");
|
|
|
|
|
|
|
|
if (sure) {
|
|
|
|
p = readpassphrase("Reenter new passphrase: ",
|
2006-02-08 06:52:15 +00:00
|
|
|
buf2 + bpos, sizeof buf2 - bpos,
|
2002-10-20 11:16:13 +00:00
|
|
|
RPP_ECHO_OFF | RPP_REQUIRE_TTY);
|
|
|
|
if (p == NULL)
|
|
|
|
err(1, "readpassphrase");
|
|
|
|
|
2006-02-08 06:52:15 +00:00
|
|
|
if (strcmp(buf1 + bpos, buf2 + bpos)) {
|
2002-10-20 11:16:13 +00:00
|
|
|
printf("They didn't match.\n");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2006-02-08 06:52:15 +00:00
|
|
|
if (strlen(buf1 + bpos) < 3) {
|
2002-10-20 11:16:13 +00:00
|
|
|
printf("Too short passphrase.\n");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2006-02-08 06:52:15 +00:00
|
|
|
g_bde_hash_pass(sc, buf1, strlen(buf1 + bpos) + bpos);
|
2002-11-04 09:27:01 +00:00
|
|
|
memcpy(sha2, sc->sha2, SHA512_DIGEST_LENGTH);
|
2002-10-20 11:16:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2002-11-04 09:27:01 +00:00
|
|
|
encrypt_sector(void *d, int len, int klen, void *key)
|
2002-10-20 11:16:13 +00:00
|
|
|
{
|
|
|
|
keyInstance ki;
|
|
|
|
cipherInstance ci;
|
|
|
|
int error;
|
2004-02-03 11:12:29 +00:00
|
|
|
|
2002-10-20 11:16:13 +00:00
|
|
|
error = rijndael_cipherInit(&ci, MODE_CBC, NULL);
|
|
|
|
if (error <= 0)
|
|
|
|
errx(1, "rijndael_cipherInit=%d", error);
|
2002-11-04 09:27:01 +00:00
|
|
|
error = rijndael_makeKey(&ki, DIR_ENCRYPT, klen, key);
|
2002-10-20 11:16:13 +00:00
|
|
|
if (error <= 0)
|
|
|
|
errx(1, "rijndael_makeKeY=%d", error);
|
|
|
|
error = rijndael_blockEncrypt(&ci, &ki, d, len * 8, d);
|
|
|
|
if (error <= 0)
|
|
|
|
errx(1, "rijndael_blockEncrypt=%d", error);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
cmd_attach(const struct g_bde_softc *sc, const char *dest, const char *lfile)
|
|
|
|
{
|
2003-03-31 18:38:31 +00:00
|
|
|
int ffd;
|
|
|
|
u_char buf[16];
|
|
|
|
struct gctl_req *r;
|
|
|
|
const char *errstr;
|
2002-10-20 11:16:13 +00:00
|
|
|
|
2003-06-01 13:47:51 +00:00
|
|
|
r = gctl_get_handle();
|
|
|
|
gctl_ro_param(r, "verb", -1, "create geom");
|
2003-03-31 18:38:31 +00:00
|
|
|
gctl_ro_param(r, "class", -1, "BDE");
|
|
|
|
gctl_ro_param(r, "provider", -1, dest);
|
|
|
|
gctl_ro_param(r, "pass", SHA512_DIGEST_LENGTH, sc->sha2);
|
2002-10-20 11:16:13 +00:00
|
|
|
if (lfile != NULL) {
|
|
|
|
ffd = open(lfile, O_RDONLY, 0);
|
|
|
|
if (ffd < 0)
|
2003-02-23 06:35:33 +00:00
|
|
|
err(1, "%s", lfile);
|
2003-03-31 18:38:31 +00:00
|
|
|
read(ffd, buf, 16);
|
|
|
|
gctl_ro_param(r, "key", 16, buf);
|
2002-10-20 11:16:13 +00:00
|
|
|
close(ffd);
|
|
|
|
}
|
2003-03-31 18:38:31 +00:00
|
|
|
errstr = gctl_issue(r);
|
|
|
|
if (errstr != NULL)
|
2004-02-03 11:10:34 +00:00
|
|
|
errx(1, "Attach to %s failed: %s", dest, errstr);
|
2002-10-20 11:16:13 +00:00
|
|
|
|
|
|
|
exit (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2002-10-20 19:08:56 +00:00
|
|
|
cmd_detach(const char *dest)
|
2002-10-20 11:16:13 +00:00
|
|
|
{
|
2003-03-31 18:38:31 +00:00
|
|
|
struct gctl_req *r;
|
|
|
|
const char *errstr;
|
|
|
|
char buf[BUFSIZ];
|
2002-10-20 11:16:13 +00:00
|
|
|
|
2003-06-01 13:47:51 +00:00
|
|
|
r = gctl_get_handle();
|
|
|
|
gctl_ro_param(r, "verb", -1, "destroy geom");
|
2003-03-31 18:38:31 +00:00
|
|
|
gctl_ro_param(r, "class", -1, "BDE");
|
|
|
|
sprintf(buf, "%s.bde", dest);
|
|
|
|
gctl_ro_param(r, "geom", -1, buf);
|
|
|
|
/* gctl_dump(r, stdout); */
|
|
|
|
errstr = gctl_issue(r);
|
|
|
|
if (errstr != NULL)
|
2004-02-03 11:10:34 +00:00
|
|
|
errx(1, "Detach of %s failed: %s", dest, errstr);
|
2002-10-20 11:16:13 +00:00
|
|
|
exit (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2002-11-04 09:27:01 +00:00
|
|
|
cmd_open(struct g_bde_softc *sc, int dfd , const char *l_opt, u_int *nkey)
|
2002-10-20 11:16:13 +00:00
|
|
|
{
|
|
|
|
int error;
|
|
|
|
int ffd;
|
|
|
|
u_char keyloc[16];
|
2002-11-04 09:27:01 +00:00
|
|
|
u_int sectorsize;
|
|
|
|
off_t mediasize;
|
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
error = ioctl(dfd, DIOCGSECTORSIZE, §orsize);
|
|
|
|
if (error)
|
|
|
|
sectorsize = 512;
|
|
|
|
error = ioctl(dfd, DIOCGMEDIASIZE, &mediasize);
|
|
|
|
if (error) {
|
|
|
|
error = fstat(dfd, &st);
|
|
|
|
if (error == 0 && S_ISREG(st.st_mode))
|
|
|
|
mediasize = st.st_size;
|
|
|
|
else
|
|
|
|
error = ENOENT;
|
|
|
|
}
|
|
|
|
if (error)
|
|
|
|
mediasize = (off_t)-1;
|
2002-10-20 11:16:13 +00:00
|
|
|
if (l_opt != NULL) {
|
|
|
|
ffd = open(l_opt, O_RDONLY, 0);
|
|
|
|
if (ffd < 0)
|
2003-02-23 06:35:33 +00:00
|
|
|
err(1, "%s", l_opt);
|
2002-10-20 11:16:13 +00:00
|
|
|
read(ffd, keyloc, sizeof keyloc);
|
|
|
|
close(ffd);
|
|
|
|
} else {
|
|
|
|
memset(keyloc, 0, sizeof keyloc);
|
|
|
|
}
|
|
|
|
|
2002-11-04 09:27:01 +00:00
|
|
|
error = g_bde_decrypt_lock(sc, sc->sha2, keyloc, mediasize,
|
|
|
|
sectorsize, nkey);
|
2002-10-20 11:16:13 +00:00
|
|
|
if (error == ENOENT)
|
|
|
|
errx(1, "Lock was destroyed.");
|
|
|
|
if (error == ESRCH)
|
|
|
|
errx(1, "Lock was nuked.");
|
|
|
|
if (error == ENOTDIR)
|
|
|
|
errx(1, "Lock not found");
|
|
|
|
if (error != 0)
|
|
|
|
errx(1, "Error %d decrypting lock", error);
|
|
|
|
if (nkey)
|
2014-11-09 15:53:29 +00:00
|
|
|
printf("Opened with key %u\n", 1 + *nkey);
|
2002-10-20 11:16:13 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
cmd_nuke(struct g_bde_key *gl, int dfd , int key)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
u_char *sbuf;
|
|
|
|
off_t offset, offset2;
|
|
|
|
|
|
|
|
sbuf = malloc(gl->sectorsize);
|
|
|
|
memset(sbuf, 0, gl->sectorsize);
|
|
|
|
offset = (gl->lsector[key] & ~(gl->sectorsize - 1));
|
|
|
|
offset2 = lseek(dfd, offset, SEEK_SET);
|
|
|
|
if (offset2 != offset)
|
|
|
|
err(1, "lseek");
|
|
|
|
i = write(dfd, sbuf, gl->sectorsize);
|
2004-02-05 08:39:38 +00:00
|
|
|
free(sbuf);
|
2002-10-20 11:16:13 +00:00
|
|
|
if (i != (int)gl->sectorsize)
|
|
|
|
err(1, "write");
|
2014-11-09 15:53:29 +00:00
|
|
|
printf("Nuked key %d\n", 1 + key);
|
2002-10-20 11:16:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
cmd_write(struct g_bde_key *gl, struct g_bde_softc *sc, int dfd , int key, const char *l_opt)
|
|
|
|
{
|
|
|
|
int i, ffd;
|
|
|
|
uint64_t off[2];
|
|
|
|
u_char keyloc[16];
|
|
|
|
u_char *sbuf, *q;
|
|
|
|
off_t offset, offset2;
|
|
|
|
|
|
|
|
sbuf = malloc(gl->sectorsize);
|
|
|
|
/*
|
|
|
|
* Find the byte-offset in the lock sector where we will put the lock
|
|
|
|
* data structure. We can put it any random place as long as the
|
|
|
|
* structure fits.
|
|
|
|
*/
|
|
|
|
for(;;) {
|
|
|
|
random_bits(off, sizeof off);
|
|
|
|
off[0] &= (gl->sectorsize - 1);
|
|
|
|
if (off[0] + G_BDE_LOCKSIZE > gl->sectorsize)
|
|
|
|
continue;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add the sector offset in bytes */
|
|
|
|
off[0] += (gl->lsector[key] & ~(gl->sectorsize - 1));
|
|
|
|
gl->lsector[key] = off[0];
|
|
|
|
|
2003-10-07 09:28:07 +00:00
|
|
|
i = g_bde_keyloc_encrypt(sc->sha2, off[0], off[1], keyloc);
|
2002-10-20 11:16:13 +00:00
|
|
|
if (i)
|
|
|
|
errx(1, "g_bde_keyloc_encrypt()");
|
|
|
|
if (l_opt != NULL) {
|
|
|
|
ffd = open(l_opt, O_WRONLY | O_CREAT | O_TRUNC, 0600);
|
|
|
|
if (ffd < 0)
|
2003-02-23 06:35:33 +00:00
|
|
|
err(1, "%s", l_opt);
|
2002-10-20 11:16:13 +00:00
|
|
|
write(ffd, keyloc, sizeof keyloc);
|
|
|
|
close(ffd);
|
2003-10-07 09:28:07 +00:00
|
|
|
} else if (gl->flags & GBDE_F_SECT0) {
|
2002-10-20 11:16:13 +00:00
|
|
|
offset2 = lseek(dfd, 0, SEEK_SET);
|
|
|
|
if (offset2 != 0)
|
|
|
|
err(1, "lseek");
|
|
|
|
i = read(dfd, sbuf, gl->sectorsize);
|
|
|
|
if (i != (int)gl->sectorsize)
|
|
|
|
err(1, "read");
|
|
|
|
memcpy(sbuf + key * 16, keyloc, sizeof keyloc);
|
|
|
|
offset2 = lseek(dfd, 0, SEEK_SET);
|
|
|
|
if (offset2 != 0)
|
|
|
|
err(1, "lseek");
|
|
|
|
i = write(dfd, sbuf, gl->sectorsize);
|
|
|
|
if (i != (int)gl->sectorsize)
|
|
|
|
err(1, "write");
|
|
|
|
} else {
|
|
|
|
errx(1, "No -L option and no space in sector 0 for lockfile");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate a sectorbuffer and fill it with random junk */
|
|
|
|
if (sbuf == NULL)
|
|
|
|
err(1, "malloc");
|
|
|
|
random_bits(sbuf, gl->sectorsize);
|
|
|
|
|
|
|
|
/* Fill random bits in the spare field */
|
|
|
|
random_bits(gl->spare, sizeof(gl->spare));
|
|
|
|
|
|
|
|
/* Encode the structure where we want it */
|
|
|
|
q = sbuf + (off[0] % gl->sectorsize);
|
2003-10-07 09:28:07 +00:00
|
|
|
i = g_bde_encode_lock(sc->sha2, gl, q);
|
2002-11-04 09:27:01 +00:00
|
|
|
if (i < 0)
|
|
|
|
errx(1, "programming error encoding lock");
|
2002-10-20 11:16:13 +00:00
|
|
|
|
2002-11-04 09:27:01 +00:00
|
|
|
encrypt_sector(q, G_BDE_LOCKSIZE, 256, sc->sha2 + 16);
|
2002-10-20 11:16:13 +00:00
|
|
|
offset = gl->lsector[key] & ~(gl->sectorsize - 1);
|
|
|
|
offset2 = lseek(dfd, offset, SEEK_SET);
|
|
|
|
if (offset2 != offset)
|
|
|
|
err(1, "lseek");
|
|
|
|
i = write(dfd, sbuf, gl->sectorsize);
|
|
|
|
if (i != (int)gl->sectorsize)
|
|
|
|
err(1, "write");
|
2004-02-05 08:39:38 +00:00
|
|
|
free(sbuf);
|
2002-12-18 19:57:27 +00:00
|
|
|
#if 0
|
2004-09-10 12:16:54 +00:00
|
|
|
printf("Wrote key %d at %jd\n", key, (intmax_t)offset);
|
2002-12-18 19:57:27 +00:00
|
|
|
printf("s0 = %jd\n", (intmax_t)gl->sector0);
|
|
|
|
printf("sN = %jd\n", (intmax_t)gl->sectorN);
|
|
|
|
printf("l[0] = %jd\n", (intmax_t)gl->lsector[0]);
|
|
|
|
printf("l[1] = %jd\n", (intmax_t)gl->lsector[1]);
|
|
|
|
printf("l[2] = %jd\n", (intmax_t)gl->lsector[2]);
|
|
|
|
printf("l[3] = %jd\n", (intmax_t)gl->lsector[3]);
|
|
|
|
printf("k = %jd\n", (intmax_t)gl->keyoffset);
|
|
|
|
printf("ss = %jd\n", (intmax_t)gl->sectorsize);
|
|
|
|
#endif
|
2002-10-20 11:16:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
cmd_destroy(struct g_bde_key *gl, int nkey)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
bzero(&gl->sector0, sizeof gl->sector0);
|
|
|
|
bzero(&gl->sectorN, sizeof gl->sectorN);
|
|
|
|
bzero(&gl->keyoffset, sizeof gl->keyoffset);
|
2014-11-09 15:53:29 +00:00
|
|
|
gl->flags &= GBDE_F_SECT0;
|
2002-10-30 22:14:34 +00:00
|
|
|
bzero(gl->mkey, sizeof gl->mkey);
|
2002-10-20 11:16:13 +00:00
|
|
|
for (i = 0; i < G_BDE_MAXKEYS; i++)
|
|
|
|
if (i != nkey)
|
|
|
|
gl->lsector[i] = ~0;
|
|
|
|
}
|
|
|
|
|
2002-12-18 19:57:27 +00:00
|
|
|
static int
|
|
|
|
sorthelp(const void *a, const void *b)
|
|
|
|
{
|
2004-09-10 12:16:54 +00:00
|
|
|
const uint64_t *oa, *ob;
|
2002-12-18 19:57:27 +00:00
|
|
|
|
|
|
|
oa = a;
|
|
|
|
ob = b;
|
2004-06-25 13:04:49 +00:00
|
|
|
if (*oa > *ob)
|
|
|
|
return 1;
|
|
|
|
if (*oa < *ob)
|
|
|
|
return -1;
|
|
|
|
return 0;
|
2002-12-18 19:57:27 +00:00
|
|
|
}
|
|
|
|
|
2002-10-20 11:16:13 +00:00
|
|
|
static void
|
|
|
|
cmd_init(struct g_bde_key *gl, int dfd, const char *f_opt, int i_opt, const char *l_opt)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
u_char *buf;
|
|
|
|
unsigned sector_size;
|
|
|
|
uint64_t first_sector;
|
|
|
|
uint64_t last_sector;
|
|
|
|
uint64_t total_sectors;
|
|
|
|
off_t off, off2;
|
|
|
|
unsigned nkeys;
|
|
|
|
const char *p;
|
|
|
|
char *q, cbuf[BUFSIZ];
|
|
|
|
unsigned u, u2;
|
|
|
|
uint64_t o;
|
|
|
|
properties params;
|
|
|
|
|
|
|
|
bzero(gl, sizeof *gl);
|
|
|
|
if (f_opt != NULL) {
|
|
|
|
i = open(f_opt, O_RDONLY);
|
|
|
|
if (i < 0)
|
2003-02-23 06:35:33 +00:00
|
|
|
err(1, "%s", f_opt);
|
2002-10-20 11:16:13 +00:00
|
|
|
params = properties_read(i);
|
|
|
|
close (i);
|
2004-02-05 10:57:29 +00:00
|
|
|
} else if (i_opt) {
|
2002-10-20 11:16:13 +00:00
|
|
|
/* XXX: Polish */
|
2004-02-05 10:57:29 +00:00
|
|
|
asprintf(&q, "%stemp.XXXXXXXXXX", _PATH_TMP);
|
|
|
|
if (q == NULL)
|
|
|
|
err(1, "asprintf");
|
2002-10-20 11:16:13 +00:00
|
|
|
i = mkstemp(q);
|
|
|
|
if (i < 0)
|
2003-02-23 06:35:33 +00:00
|
|
|
err(1, "%s", q);
|
2002-10-20 11:16:13 +00:00
|
|
|
write(i, template, strlen(template));
|
|
|
|
close (i);
|
2004-02-05 10:57:29 +00:00
|
|
|
p = getenv("EDITOR");
|
|
|
|
if (p == NULL)
|
|
|
|
p = "vi";
|
|
|
|
if (snprintf(cbuf, sizeof(cbuf), "%s %s\n", p, q) >=
|
|
|
|
(ssize_t)sizeof(cbuf)) {
|
|
|
|
unlink(q);
|
|
|
|
errx(1, "EDITOR is too long");
|
2002-10-20 11:16:13 +00:00
|
|
|
}
|
2004-02-05 10:57:29 +00:00
|
|
|
system(cbuf);
|
2002-10-20 11:16:13 +00:00
|
|
|
i = open(q, O_RDONLY);
|
|
|
|
if (i < 0)
|
2003-02-23 06:35:33 +00:00
|
|
|
err(1, "%s", f_opt);
|
2002-10-20 11:16:13 +00:00
|
|
|
params = properties_read(i);
|
|
|
|
close (i);
|
|
|
|
unlink(q);
|
2004-02-05 08:39:38 +00:00
|
|
|
free(q);
|
2004-02-05 10:57:29 +00:00
|
|
|
} else {
|
|
|
|
/* XXX: Hack */
|
|
|
|
i = open(_PATH_DEVNULL, O_RDONLY);
|
|
|
|
if (i < 0)
|
|
|
|
err(1, "%s", _PATH_DEVNULL);
|
|
|
|
params = properties_read(i);
|
|
|
|
close (i);
|
2002-10-20 11:16:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* <sector_size> */
|
|
|
|
p = property_find(params, "sector_size");
|
|
|
|
i = ioctl(dfd, DIOCGSECTORSIZE, &u);
|
|
|
|
if (p != NULL) {
|
|
|
|
sector_size = strtoul(p, &q, 0);
|
|
|
|
if (!*p || *q)
|
|
|
|
errx(1, "sector_size not a proper number");
|
2002-12-18 07:25:33 +00:00
|
|
|
} else if (i == 0) {
|
|
|
|
sector_size = u;
|
|
|
|
} else {
|
|
|
|
errx(1, "Missing sector_size property");
|
2002-10-20 11:16:13 +00:00
|
|
|
}
|
|
|
|
if (sector_size & (sector_size - 1))
|
|
|
|
errx(1, "sector_size not a power of 2");
|
|
|
|
if (sector_size < 512)
|
|
|
|
errx(1, "sector_size is smaller than 512");
|
|
|
|
buf = malloc(sector_size);
|
|
|
|
if (buf == NULL)
|
|
|
|
err(1, "Failed to malloc sector buffer");
|
|
|
|
gl->sectorsize = sector_size;
|
|
|
|
|
|
|
|
i = ioctl(dfd, DIOCGMEDIASIZE, &off);
|
|
|
|
if (i == 0) {
|
|
|
|
first_sector = 0;
|
|
|
|
total_sectors = off / sector_size;
|
|
|
|
last_sector = total_sectors - 1;
|
|
|
|
} else {
|
|
|
|
first_sector = 0;
|
|
|
|
last_sector = 0;
|
|
|
|
total_sectors = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* <first_sector> */
|
|
|
|
p = property_find(params, "first_sector");
|
|
|
|
if (p != NULL) {
|
|
|
|
first_sector = strtoul(p, &q, 0);
|
|
|
|
if (!*p || *q)
|
|
|
|
errx(1, "first_sector not a proper number");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* <last_sector> */
|
|
|
|
p = property_find(params, "last_sector");
|
|
|
|
if (p != NULL) {
|
|
|
|
last_sector = strtoul(p, &q, 0);
|
|
|
|
if (!*p || *q)
|
|
|
|
errx(1, "last_sector not a proper number");
|
|
|
|
if (last_sector <= first_sector)
|
|
|
|
errx(1, "last_sector not larger than first_sector");
|
|
|
|
total_sectors = last_sector + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* <total_sectors> */
|
|
|
|
p = property_find(params, "total_sectors");
|
|
|
|
if (p != NULL) {
|
|
|
|
total_sectors = strtoul(p, &q, 0);
|
|
|
|
if (!*p || *q)
|
|
|
|
errx(1, "total_sectors not a proper number");
|
2004-02-03 11:12:29 +00:00
|
|
|
if (last_sector == 0)
|
2002-10-20 11:16:13 +00:00
|
|
|
last_sector = first_sector + total_sectors - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (l_opt == NULL && first_sector != 0)
|
|
|
|
errx(1, "No -L new-lockfile argument and first_sector != 0");
|
|
|
|
else if (l_opt == NULL) {
|
|
|
|
first_sector++;
|
|
|
|
total_sectors--;
|
2003-10-07 09:28:07 +00:00
|
|
|
gl->flags |= GBDE_F_SECT0;
|
2002-10-20 11:16:13 +00:00
|
|
|
}
|
2002-12-18 22:11:54 +00:00
|
|
|
gl->sector0 = first_sector * gl->sectorsize;
|
2002-10-20 11:16:13 +00:00
|
|
|
|
|
|
|
if (total_sectors != (last_sector - first_sector) + 1)
|
|
|
|
errx(1, "total_sectors disagree with first_sector and last_sector");
|
|
|
|
if (total_sectors == 0)
|
|
|
|
errx(1, "missing last_sector or total_sectors");
|
|
|
|
|
|
|
|
gl->sectorN = (last_sector + 1) * gl->sectorsize;
|
|
|
|
|
|
|
|
/* Find a random keyoffset */
|
|
|
|
random_bits(&o, sizeof o);
|
|
|
|
o %= (gl->sectorN - gl->sector0);
|
|
|
|
o &= ~(gl->sectorsize - 1);
|
|
|
|
gl->keyoffset = o;
|
|
|
|
|
|
|
|
/* <number_of_keys> */
|
|
|
|
p = property_find(params, "number_of_keys");
|
2004-02-05 10:57:29 +00:00
|
|
|
if (p != NULL) {
|
|
|
|
nkeys = strtoul(p, &q, 0);
|
|
|
|
if (!*p || *q)
|
|
|
|
errx(1, "number_of_keys not a proper number");
|
|
|
|
if (nkeys < 1 || nkeys > G_BDE_MAXKEYS)
|
|
|
|
errx(1, "number_of_keys out of range");
|
|
|
|
} else {
|
|
|
|
nkeys = 4;
|
|
|
|
}
|
2002-10-20 11:16:13 +00:00
|
|
|
for (u = 0; u < nkeys; u++) {
|
|
|
|
for(;;) {
|
|
|
|
do {
|
|
|
|
random_bits(&o, sizeof o);
|
|
|
|
o %= gl->sectorN;
|
|
|
|
o &= ~(gl->sectorsize - 1);
|
|
|
|
} while(o < gl->sector0);
|
|
|
|
for (u2 = 0; u2 < u; u2++)
|
|
|
|
if (o == gl->lsector[u2])
|
|
|
|
break;
|
|
|
|
if (u2 < u)
|
|
|
|
continue;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
gl->lsector[u] = o;
|
2004-02-03 11:12:29 +00:00
|
|
|
}
|
2002-10-20 11:16:13 +00:00
|
|
|
for (; u < G_BDE_MAXKEYS; u++) {
|
2004-02-03 11:12:29 +00:00
|
|
|
do
|
2002-10-20 11:16:13 +00:00
|
|
|
random_bits(&o, sizeof o);
|
|
|
|
while (o < gl->sectorN);
|
|
|
|
gl->lsector[u] = o;
|
|
|
|
}
|
2002-12-18 19:57:27 +00:00
|
|
|
qsort(gl->lsector, G_BDE_MAXKEYS, sizeof gl->lsector[0], sorthelp);
|
2002-10-20 11:16:13 +00:00
|
|
|
|
|
|
|
/* Flush sector zero if we use it for lockfile data */
|
2003-10-07 09:28:07 +00:00
|
|
|
if (gl->flags & GBDE_F_SECT0) {
|
2002-10-20 11:16:13 +00:00
|
|
|
off2 = lseek(dfd, 0, SEEK_SET);
|
|
|
|
if (off2 != 0)
|
|
|
|
err(1, "lseek(2) to sector 0");
|
|
|
|
random_bits(buf, sector_size);
|
|
|
|
i = write(dfd, buf, sector_size);
|
|
|
|
if (i != (int)sector_size)
|
|
|
|
err(1, "write sector 0");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* <random_flush> */
|
|
|
|
p = property_find(params, "random_flush");
|
|
|
|
if (p != NULL) {
|
|
|
|
off = first_sector * sector_size;
|
|
|
|
off2 = lseek(dfd, off, SEEK_SET);
|
|
|
|
if (off2 != off)
|
|
|
|
err(1, "lseek(2) to first_sector");
|
|
|
|
off2 = last_sector * sector_size;
|
|
|
|
while (off <= off2) {
|
|
|
|
random_bits(buf, sector_size);
|
|
|
|
i = write(dfd, buf, sector_size);
|
|
|
|
if (i != (int)sector_size)
|
|
|
|
err(1, "write to $device_name");
|
|
|
|
off += sector_size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-10-30 22:14:34 +00:00
|
|
|
random_bits(gl->mkey, sizeof gl->mkey);
|
|
|
|
random_bits(gl->salt, sizeof gl->salt);
|
2004-02-03 11:12:29 +00:00
|
|
|
|
2002-10-20 11:16:13 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
static enum action {
|
|
|
|
ACT_HUH,
|
2002-10-20 19:08:56 +00:00
|
|
|
ACT_ATTACH, ACT_DETACH,
|
2002-10-20 11:16:13 +00:00
|
|
|
ACT_INIT, ACT_SETKEY, ACT_DESTROY, ACT_NUKE
|
|
|
|
} action;
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
const char *opts;
|
2006-02-08 06:52:15 +00:00
|
|
|
const char *k_opt, *K_opt;
|
2002-10-20 11:16:13 +00:00
|
|
|
const char *l_opt, *L_opt;
|
|
|
|
const char *p_opt, *P_opt;
|
|
|
|
const char *f_opt;
|
2002-12-01 15:58:28 +00:00
|
|
|
char *dest;
|
|
|
|
int i_opt, n_opt, ch, dfd, doopen;
|
|
|
|
u_int nkey;
|
2002-10-20 11:16:13 +00:00
|
|
|
int i;
|
2002-12-01 15:58:28 +00:00
|
|
|
char *q, buf[BUFSIZ];
|
2002-10-20 11:16:13 +00:00
|
|
|
struct g_bde_key *gl;
|
|
|
|
struct g_bde_softc sc;
|
|
|
|
|
|
|
|
if (argc < 3)
|
2005-02-12 21:47:05 +00:00
|
|
|
usage();
|
2002-10-20 11:16:13 +00:00
|
|
|
|
2010-06-11 17:03:04 +00:00
|
|
|
if (modfind("g_bde") < 0) {
|
2005-02-03 21:25:35 +00:00
|
|
|
/* need to load the gbde module */
|
|
|
|
if (kldload(GBDEMOD) < 0 || modfind("g_bde") < 0)
|
2005-02-12 21:47:05 +00:00
|
|
|
err(1, GBDEMOD ": Kernel module not available");
|
2005-02-03 21:25:35 +00:00
|
|
|
}
|
2002-10-20 11:16:13 +00:00
|
|
|
doopen = 0;
|
|
|
|
if (!strcmp(argv[1], "attach")) {
|
|
|
|
action = ACT_ATTACH;
|
2006-02-08 06:52:15 +00:00
|
|
|
opts = "k:l:p:";
|
2002-10-20 19:08:56 +00:00
|
|
|
} else if (!strcmp(argv[1], "detach")) {
|
|
|
|
action = ACT_DETACH;
|
2002-10-20 11:16:13 +00:00
|
|
|
opts = "";
|
|
|
|
} else if (!strcmp(argv[1], "init")) {
|
|
|
|
action = ACT_INIT;
|
|
|
|
doopen = 1;
|
2006-02-08 06:52:15 +00:00
|
|
|
opts = "f:iK:L:P:";
|
2002-10-20 11:16:13 +00:00
|
|
|
} else if (!strcmp(argv[1], "setkey")) {
|
|
|
|
action = ACT_SETKEY;
|
|
|
|
doopen = 1;
|
2006-02-08 06:52:15 +00:00
|
|
|
opts = "k:K:l:L:n:p:P:";
|
2002-10-20 11:16:13 +00:00
|
|
|
} else if (!strcmp(argv[1], "destroy")) {
|
|
|
|
action = ACT_DESTROY;
|
|
|
|
doopen = 1;
|
2006-02-08 06:52:15 +00:00
|
|
|
opts = "k:l:p:";
|
2002-10-20 11:16:13 +00:00
|
|
|
} else if (!strcmp(argv[1], "nuke")) {
|
|
|
|
action = ACT_NUKE;
|
|
|
|
doopen = 1;
|
2006-02-08 06:52:15 +00:00
|
|
|
opts = "k:l:n:p:";
|
2002-10-20 11:16:13 +00:00
|
|
|
} else {
|
2005-02-12 21:47:05 +00:00
|
|
|
usage();
|
2002-10-20 11:16:13 +00:00
|
|
|
}
|
|
|
|
argc--;
|
|
|
|
argv++;
|
|
|
|
|
2002-12-01 15:58:28 +00:00
|
|
|
dest = strdup(argv[1]);
|
2002-10-20 11:16:13 +00:00
|
|
|
argc--;
|
|
|
|
argv++;
|
|
|
|
|
|
|
|
p_opt = NULL;
|
|
|
|
P_opt = NULL;
|
2006-02-08 06:52:15 +00:00
|
|
|
k_opt = NULL;
|
|
|
|
K_opt = NULL;
|
2002-10-20 11:16:13 +00:00
|
|
|
l_opt = NULL;
|
|
|
|
L_opt = NULL;
|
|
|
|
f_opt = NULL;
|
|
|
|
n_opt = 0;
|
|
|
|
i_opt = 0;
|
|
|
|
|
|
|
|
while((ch = getopt(argc, argv, opts)) != -1)
|
|
|
|
switch (ch) {
|
|
|
|
case 'f':
|
|
|
|
f_opt = optarg;
|
|
|
|
break;
|
|
|
|
case 'i':
|
|
|
|
i_opt = !i_opt;
|
2014-04-15 07:37:56 +00:00
|
|
|
break;
|
2006-02-08 06:52:15 +00:00
|
|
|
case 'k':
|
|
|
|
k_opt = optarg;
|
|
|
|
break;
|
|
|
|
case 'K':
|
|
|
|
K_opt = optarg;
|
|
|
|
break;
|
2002-10-20 11:16:13 +00:00
|
|
|
case 'l':
|
|
|
|
l_opt = optarg;
|
|
|
|
break;
|
|
|
|
case 'L':
|
|
|
|
L_opt = optarg;
|
|
|
|
break;
|
2005-02-03 21:34:39 +00:00
|
|
|
case 'n':
|
|
|
|
n_opt = strtoul(optarg, &q, 0);
|
|
|
|
if (!*optarg || *q)
|
2005-02-12 21:47:05 +00:00
|
|
|
errx(1, "-n argument not numeric");
|
2005-02-03 21:34:39 +00:00
|
|
|
if (n_opt < -1 || n_opt > G_BDE_MAXKEYS)
|
2005-02-12 21:47:05 +00:00
|
|
|
errx(1, "-n argument out of range");
|
2005-02-03 21:34:39 +00:00
|
|
|
break;
|
2002-10-20 11:16:13 +00:00
|
|
|
case 'p':
|
|
|
|
p_opt = optarg;
|
|
|
|
break;
|
|
|
|
case 'P':
|
|
|
|
P_opt = optarg;
|
|
|
|
break;
|
|
|
|
default:
|
2005-02-12 21:47:05 +00:00
|
|
|
usage();
|
2002-10-20 11:16:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (doopen) {
|
2005-02-03 13:12:17 +00:00
|
|
|
dfd = open(dest, O_RDWR);
|
|
|
|
if (dfd < 0 && dest[0] != '/') {
|
2003-02-23 07:37:47 +00:00
|
|
|
if (snprintf(buf, sizeof(buf), "%s%s",
|
|
|
|
_PATH_DEV, dest) >= (ssize_t)sizeof(buf))
|
|
|
|
errno = ENAMETOOLONG;
|
|
|
|
else
|
2005-02-03 13:12:17 +00:00
|
|
|
dfd = open(buf, O_RDWR);
|
2002-12-01 15:58:28 +00:00
|
|
|
}
|
2002-10-20 11:16:13 +00:00
|
|
|
if (dfd < 0)
|
2003-02-23 06:35:33 +00:00
|
|
|
err(1, "%s", dest);
|
2002-12-01 15:58:28 +00:00
|
|
|
} else {
|
|
|
|
if (!memcmp(dest, _PATH_DEV, strlen(_PATH_DEV)))
|
|
|
|
strcpy(dest, dest + strlen(_PATH_DEV));
|
2002-10-20 11:16:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
memset(&sc, 0, sizeof sc);
|
2002-12-17 19:16:10 +00:00
|
|
|
sc.consumer = (void *)&dfd;
|
2002-10-20 11:16:13 +00:00
|
|
|
gl = &sc.key;
|
|
|
|
switch(action) {
|
|
|
|
case ACT_ATTACH:
|
2006-02-08 06:52:15 +00:00
|
|
|
setup_passphrase(&sc, 0, p_opt, k_opt);
|
2002-10-20 11:16:13 +00:00
|
|
|
cmd_attach(&sc, dest, l_opt);
|
|
|
|
break;
|
2002-10-20 19:08:56 +00:00
|
|
|
case ACT_DETACH:
|
|
|
|
cmd_detach(dest);
|
2002-10-20 11:16:13 +00:00
|
|
|
break;
|
|
|
|
case ACT_INIT:
|
|
|
|
cmd_init(gl, dfd, f_opt, i_opt, L_opt);
|
2006-02-08 06:52:15 +00:00
|
|
|
setup_passphrase(&sc, 1, P_opt, K_opt);
|
2002-10-20 11:16:13 +00:00
|
|
|
cmd_write(gl, &sc, dfd, 0, L_opt);
|
|
|
|
break;
|
|
|
|
case ACT_SETKEY:
|
2006-02-08 06:52:15 +00:00
|
|
|
setup_passphrase(&sc, 0, p_opt, k_opt);
|
2002-10-20 11:16:13 +00:00
|
|
|
cmd_open(&sc, dfd, l_opt, &nkey);
|
|
|
|
if (n_opt == 0)
|
|
|
|
n_opt = nkey + 1;
|
2006-02-08 06:52:15 +00:00
|
|
|
setup_passphrase(&sc, 1, P_opt, K_opt);
|
2002-10-20 11:16:13 +00:00
|
|
|
cmd_write(gl, &sc, dfd, n_opt - 1, L_opt);
|
|
|
|
break;
|
|
|
|
case ACT_DESTROY:
|
2006-02-08 06:52:15 +00:00
|
|
|
setup_passphrase(&sc, 0, p_opt, k_opt);
|
2002-10-20 11:16:13 +00:00
|
|
|
cmd_open(&sc, dfd, l_opt, &nkey);
|
|
|
|
cmd_destroy(gl, nkey);
|
|
|
|
reset_passphrase(&sc);
|
|
|
|
cmd_write(gl, &sc, dfd, nkey, l_opt);
|
|
|
|
break;
|
|
|
|
case ACT_NUKE:
|
2006-02-08 06:52:15 +00:00
|
|
|
setup_passphrase(&sc, 0, p_opt, k_opt);
|
2002-10-20 11:16:13 +00:00
|
|
|
cmd_open(&sc, dfd, l_opt, &nkey);
|
|
|
|
if (n_opt == 0)
|
|
|
|
n_opt = nkey + 1;
|
|
|
|
if (n_opt == -1) {
|
|
|
|
for(i = 0; i < G_BDE_MAXKEYS; i++)
|
|
|
|
cmd_nuke(gl, dfd, i);
|
|
|
|
} else {
|
|
|
|
cmd_nuke(gl, dfd, n_opt - 1);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2005-02-12 21:47:05 +00:00
|
|
|
errx(1, "internal error");
|
2002-10-20 11:16:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return(0);
|
|
|
|
}
|