#include <sys/types.h>
#include <sys/param.h>
#include <sys/lofi.h>
#include <sys/stat.h>
#include <sys/sysmacros.h>
#include <sys/modctl.h>
#include <netinet/in.h>
#include <stdio.h>
#include <fcntl.h>
#include <locale.h>
#include <string.h>
#include <strings.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <stropts.h>
#include <libdevinfo.h>
#include <libgen.h>
#include <ctype.h>
#include <dlfcn.h>
#include <limits.h>
#include <security/cryptoki.h>
#include <cryptoutil.h>
#include <sys/crypto/ioctl.h>
#include <sys/crypto/ioctladmin.h>
#include <sys/cmlb.h>
#include <sys/mkdev.h>
#include "utils.h"
#include <LzmaEnc.h>
#include <aes/aes_impl.h>
#include <des/des_impl.h>
#include <blowfish/blowfish_impl.h>
static const char USAGE[] =
"Usage: %s [-r] [-l] -a file [ device ]\n"
" %s [-r] -c crypto_algorithm -a file [device]\n"
" %s [-r] -c crypto_algorithm -k raw_key_file -a file [device]\n"
" %s [-r] -c crypto_algorithm -T [token]:[manuf]:[serial]:key "
"-a file [device]\n"
" %s [-r] -c crypto_algorithm -T [token]:[manuf]:[serial]:key "
"-k wrapped_key_file -a file [device]\n"
" %s [-r] -c crypto_algorithm -e -a file [device]\n"
" %s -d file | device\n"
" %s -C [gzip|gzip-6|gzip-9|lzma] [-s segment_size] file\n"
" %s -U file\n"
" %s [ file | device ]\n";
typedef struct token_spec {
char *name;
char *mfr;
char *serno;
char *key;
} token_spec_t;
typedef struct mech_alias {
char *alias;
CK_MECHANISM_TYPE type;
char *name;
char *iv_name;
size_t iv_len;
iv_method_t iv_type;
size_t min_keysize;
size_t max_keysize;
token_spec_t *token;
CK_SLOT_ID slot;
} mech_alias_t;
static mech_alias_t mech_aliases[] = {
{ "aes-256-cbc", CKM_AES_CBC, "CKM_AES_CBC", "CKM_AES_ECB", AES_IV_LEN,
IVM_ENC_BLKNO, ULONG_MAX, 0L, NULL, (CK_SLOT_ID) -1 },
{ "aes-192-cbc", CKM_AES_CBC, "CKM_AES_CBC", "CKM_AES_ECB", AES_IV_LEN,
IVM_ENC_BLKNO, ULONG_MAX, 0L, NULL, (CK_SLOT_ID) -1 },
{ "aes-128-cbc", CKM_AES_CBC, "CKM_AES_CBC", "CKM_AES_ECB", AES_IV_LEN,
IVM_ENC_BLKNO, ULONG_MAX, 0L, NULL, (CK_SLOT_ID) -1 },
{ "des3-cbc", CKM_DES3_CBC, "CKM_DES3_CBC", "CKM_DES3_ECB", DES_IV_LEN,
IVM_ENC_BLKNO, ULONG_MAX, 0L, NULL, (CK_SLOT_ID)-1 },
{ "blowfish-cbc", CKM_BLOWFISH_CBC, "CKM_BLOWFISH_CBC",
"CKM_BLOWFISH_ECB", BLOWFISH_IV_LEN, IVM_ENC_BLKNO, ULONG_MAX,
0L, NULL, (CK_SLOT_ID)-1 }
};
int mech_aliases_count = (sizeof (mech_aliases) / sizeof (mech_alias_t));
#define DEFAULT_CIPHER (&mech_aliases[0])
#define DEFAULT_CIPHER_NUM 64
#define DEFAULT_MECHINFO_NUM 16
#define MIN_PASSLEN 8
static int gzip_compress(void *src, size_t srclen, void *dst,
size_t *destlen, int level);
static int lzma_compress(void *src, size_t srclen, void *dst,
size_t *destlen, int level);
lofi_compress_info_t lofi_compress_table[LOFI_COMPRESS_FUNCTIONS] = {
{NULL, gzip_compress, 6, "gzip"},
{NULL, gzip_compress, 6, "gzip-6"},
{NULL, gzip_compress, 9, "gzip-9"},
{NULL, lzma_compress, 0, "lzma"}
};
#define FORMAT "%-20s %-30s %s\n"
#define COMPRESS_ALGORITHM "gzip"
#define COMPRESS_THRESHOLD 2048
#define SEGSIZE 131072
#define BLOCK_SIZE 512
#define KILOBYTE 1024
#define MEGABYTE (KILOBYTE * KILOBYTE)
#define GIGABYTE (KILOBYTE * MEGABYTE)
#define LIBZ "libz.so.1"
const char lofi_crypto_magic[6] = LOFI_CRYPTO_MAGIC;
static void
usage(const char *pname)
{
(void) fprintf(stderr, gettext(USAGE), pname, pname, pname,
pname, pname, pname, pname, pname, pname, pname);
exit(E_USAGE);
}
static int
gzip_compress(void *src, size_t srclen, void *dst, size_t *dstlen, int level)
{
static int (*compress2p)(void *, ulong_t *, void *, size_t, int) = NULL;
void *libz_hdl = NULL;
if (compress2p == NULL) {
if ((libz_hdl = openlib(LIBZ)) == NULL)
die(gettext("could not find %s. "
"gzip compression unavailable\n"), LIBZ);
if ((compress2p =
(int (*)(void *, ulong_t *, void *, size_t, int))
dlsym(libz_hdl, "compress2")) == NULL) {
closelib();
die(gettext("could not find the correct %s. "
"gzip compression unavailable\n"), LIBZ);
}
}
if ((*compress2p)(dst, (ulong_t *)dstlen, src, srclen, level) != 0)
return (-1);
return (0);
}
static void
*SzAlloc(void *p, size_t size)
{
return (malloc(size));
}
static void
SzFree(void *p, void *address, size_t size)
{
free(address);
}
static ISzAlloc g_Alloc = {
SzAlloc,
SzFree
};
#define LZMA_UNCOMPRESSED_SIZE 8
#define LZMA_HEADER_SIZE (LZMA_PROPS_SIZE + LZMA_UNCOMPRESSED_SIZE)
static int
lzma_compress(void *src, size_t srclen, void *dst,
size_t *dstlen, int level)
{
CLzmaEncProps props;
size_t outsize2;
size_t outsizeprocessed;
size_t outpropssize = LZMA_PROPS_SIZE;
uint64_t t = 0;
SRes res;
Byte *dstp;
int i;
outsize2 = *dstlen;
LzmaEncProps_Init(&props);
props.dictSize = 1 << 23;
if (*dstlen < LZMA_HEADER_SIZE)
return (SZ_ERROR_OUTPUT_EOF);
dstp = (Byte *)dst;
t = srclen;
for (i = 0; i < LZMA_UNCOMPRESSED_SIZE; i++, t >>= 8) {
dstp[LZMA_PROPS_SIZE + i] = (Byte)t;
}
outsizeprocessed = outsize2 - LZMA_HEADER_SIZE;
res = LzmaEncode(dstp + LZMA_HEADER_SIZE, &outsizeprocessed,
src, srclen, &props, dstp, &outpropssize, 0, NULL,
&g_Alloc, &g_Alloc);
if (res != 0)
return (-1);
*dstlen = outsizeprocessed + LZMA_HEADER_SIZE;
return (0);
}
static int
name_to_minor(const char *devicename)
{
struct stat st;
if (stat(devicename, &st)) {
int minor, rv;
rv = sscanf(devicename, "/dev/" LOFI_BLOCK_NAME "/%d", &minor);
if (rv == 1)
return (minor);
rv = sscanf(devicename, "/dev/" LOFI_CHAR_NAME "/%d", &minor);
if (rv == 1)
return (minor);
return (0);
}
if (st.st_mode & S_IFCHR || st.st_mode & S_IFBLK) {
major_t maj;
char mname[MODMAXNAMELEN];
maj = major(st.st_rdev);
if (modctl(MODGETNAME, mname, MODMAXNAMELEN, &maj) == 0) {
if (strncmp(mname, LOFI_DRIVER_NAME,
sizeof (LOFI_DRIVER_NAME)) == 0) {
return (LOFI_MINOR2ID(minor(st.st_rdev)));
}
}
}
return (0);
}
static int sleeptime = 2;
static int maxsleep = 120;
static void
make_blkdevname(struct lofi_ioctl *li, char *path, size_t len)
{
char *r1, *r2;
size_t l1;
if (li->li_devpath[0] == '\0') {
if (li->li_labeled)
(void) strlcpy(path, "unknown", len);
else
(void) snprintf(path, len,
"/dev/" LOFI_BLOCK_NAME "/%d", li->li_id);
return;
}
(void) strlcpy(path, li->li_devpath, len);
r1 = strchr(path, 'r');
l1 = r1 - path;
r2 = strchr(li->li_devpath, 'r');
(void) strlcpy(r1, r2+1, len - l1);
if (li->li_labeled) {
(void) strlcat(path, "p0", len);
}
}
static void
wait_until_dev_complete(struct lofi_ioctl *li)
{
struct stat64 buf;
int cursleep;
char blkpath[MAXPATHLEN];
char charpath[MAXPATHLEN];
di_devlink_handle_t hdl;
make_blkdevname(li, blkpath, sizeof (blkpath));
(void) strlcpy(charpath, li->li_devpath, sizeof (charpath));
if (li->li_labeled) {
(void) strlcat(charpath, "p0", sizeof (charpath));
}
if (stat64(blkpath, &buf) == 0 && stat64(charpath, &buf) == 0)
return;
if (hdl = di_devlink_init("lofi", DI_MAKE_LINK)) {
(void) di_devlink_fini(&hdl);
goto out;
}
for (cursleep = 0; cursleep < maxsleep; cursleep += sleeptime) {
if (stat64(blkpath, &buf) == 0 && stat64(charpath, &buf) == 0)
return;
(void) sleep(sleeptime);
}
out:
if (stat64(blkpath, &buf) == -1) {
die(gettext("%s was not created"), blkpath);
}
if (stat64(charpath, &buf) == -1) {
die(gettext("%s was not created"), charpath);
}
}
static int
lofi_map_file(int lfd, struct lofi_ioctl *li, const char *filename)
{
int minor;
li->li_id = 0;
(void) strlcpy(li->li_filename, filename, sizeof (li->li_filename));
minor = ioctl(lfd, LOFI_MAP_FILE, li);
if (minor == -1) {
if (errno == ENOTSUP)
warn(gettext("encrypting compressed files is "
"unsupported"));
die(gettext("could not map file %s"), filename);
}
wait_until_dev_complete(li);
return (minor);
}
static void
add_mapping(int lfd, const char *devicename, const char *filename,
mech_alias_t *cipher, const char *rkey, size_t rksz, boolean_t rdonly,
boolean_t label)
{
struct lofi_ioctl li;
bzero(&li, sizeof (li));
li.li_readonly = rdonly;
li.li_labeled = label;
li.li_crypto_enabled = B_FALSE;
if (cipher != NULL) {
li.li_crypto_enabled = B_TRUE;
(void) strlcpy(li.li_cipher, cipher->name,
sizeof (li.li_cipher));
if (rksz > sizeof (li.li_key)) {
die(gettext("key too large"));
}
bcopy(rkey, li.li_key, rksz);
li.li_key_len = rksz << 3;
li.li_iv_type = cipher->iv_type;
li.li_iv_len = cipher->iv_len;
switch (cipher->iv_type) {
case IVM_ENC_BLKNO:
(void) strlcpy(li.li_iv_cipher, cipher->iv_name,
sizeof (li.li_iv_cipher));
break;
case IVM_NONE:
default:
break;
}
}
if (devicename == NULL) {
int minor;
char path[MAXPATHLEN];
minor = lofi_map_file(lfd, &li, filename);
if (minor > 0) {
make_blkdevname(&li, path, sizeof (path));
(void) printf("%s\n", path);
}
return;
}
li.li_id = name_to_minor(devicename);
if (li.li_id == 0) {
die(gettext("malformed device name %s\n"), devicename);
}
(void) strlcpy(li.li_filename, filename, sizeof (li.li_filename));
if (ioctl(lfd, LOFI_MAP_FILE_MINOR, &li) == -1) {
if (errno == ENOTSUP)
warn(gettext("encrypting compressed files is "
"unsupported"));
die(gettext("could not map file %s to %s"), filename,
devicename);
}
wait_until_dev_complete(&li);
}
static void
delete_mapping(int lfd, const char *devicename, const char *filename,
boolean_t force)
{
struct lofi_ioctl li;
li.li_force = force;
li.li_cleanup = B_FALSE;
if (devicename == NULL) {
(void) strlcpy(li.li_filename, filename,
sizeof (li.li_filename));
li.li_id = 0;
if (ioctl(lfd, LOFI_UNMAP_FILE, &li) == -1) {
die(gettext("could not unmap file %s"), filename);
}
return;
}
li.li_id = name_to_minor(devicename);
if (li.li_id == 0) {
die(gettext("malformed device name %s\n"), devicename);
}
if (ioctl(lfd, LOFI_UNMAP_FILE_MINOR, &li) == -1) {
die(gettext("could not unmap device %s"), devicename);
}
}
static void
print_one_mapping(int lfd, const char *devicename, const char *filename)
{
struct lofi_ioctl li;
char blkpath[MAXPATHLEN];
if (devicename == NULL) {
li.li_id = 0;
(void) strlcpy(li.li_filename, filename,
sizeof (li.li_filename));
if (ioctl(lfd, LOFI_GET_MINOR, &li) == -1) {
die(gettext("could not find device for %s"), filename);
}
make_blkdevname(&li, blkpath, sizeof (blkpath));
(void) printf("%s\n", blkpath);
return;
}
li.li_id = name_to_minor(devicename);
if (li.li_id == 0) {
die(gettext("malformed device name %s\n"), devicename);
}
if (ioctl(lfd, LOFI_GET_FILENAME, &li) == -1) {
die(gettext("could not find filename for %s"), devicename);
}
(void) printf("%s\n", li.li_filename);
}
static void
print_mappings(int fd)
{
struct lofi_ioctl li;
int minor;
int maxminor;
char path[MAXPATHLEN];
char options[MAXPATHLEN] = { 0 };
li.li_id = 0;
if (ioctl(fd, LOFI_GET_MAXMINOR, &li) == -1) {
die("ioctl");
}
maxminor = li.li_id;
(void) printf(FORMAT, gettext("Block Device"), gettext("File"),
gettext("Options"));
for (minor = 1; minor <= maxminor; minor++) {
li.li_id = minor;
if (ioctl(fd, LOFI_GET_FILENAME, &li) == -1) {
if (errno == ENXIO)
continue;
warn("ioctl");
break;
}
make_blkdevname(&li, path, sizeof (path));
options[0] = '\0';
if (li.li_crypto_enabled)
(void) snprintf(options, sizeof (options),
gettext("Encrypted"));
else if (li.li_algorithm[0] != '\0')
(void) snprintf(options, sizeof (options),
gettext("Compressed(%s)"), li.li_algorithm);
if (li.li_readonly) {
if (strlen(options) != 0) {
(void) strlcat(options, ",Readonly",
sizeof (options));
} else {
(void) snprintf(options, sizeof (options),
gettext("Readonly"));
}
}
if (li.li_labeled) {
if (strlen(options) != 0) {
(void) strlcat(options, ",Labeled",
sizeof (options));
} else {
(void) snprintf(options, sizeof (options),
gettext("Labeled"));
}
}
if (strlen(options) == 0)
(void) snprintf(options, sizeof (options), "-");
(void) printf(FORMAT, path, li.li_filename, options);
}
}
static mech_alias_t *
ciph2mech(const char *alias)
{
int i;
for (i = 0; i < mech_aliases_count; i++) {
if (strcasecmp(alias, mech_aliases[i].alias) == 0)
return (&mech_aliases[i]);
}
return (NULL);
}
static boolean_t
kernel_cipher_check(mech_alias_t *cipher)
{
boolean_t ciph_ok = B_FALSE;
boolean_t iv_ok = B_FALSE;
int i;
int count;
crypto_get_mechanism_list_t *kciphers = NULL;
crypto_get_all_mechanism_info_t *kinfo = NULL;
int fd = -1;
size_t keymin;
size_t keymax;
if (cipher->iv_name == NULL)
iv_ok = B_TRUE;
count = DEFAULT_CIPHER_NUM;
kciphers = malloc(sizeof (crypto_get_mechanism_list_t) +
sizeof (crypto_mech_name_t) * (count - 1));
if (kciphers == NULL)
die(gettext("failed to allocate memory for list of "
"kernel mechanisms"));
kciphers->ml_count = count;
if ((fd = open("/dev/crypto", O_RDWR)) == -1) {
warn(gettext("failed to open %s"), "/dev/crypto");
goto kcc_out;
}
if (ioctl(fd, CRYPTO_GET_MECHANISM_LIST, kciphers) == -1) {
warn(gettext("CRYPTO_GET_MECHANISM_LIST ioctl failed"));
goto kcc_out;
}
if (kciphers->ml_return_value == CRYPTO_BUFFER_TOO_SMALL) {
count = kciphers->ml_count;
free(kciphers);
kciphers = malloc(sizeof (crypto_get_mechanism_list_t) +
sizeof (crypto_mech_name_t) * (count - 1));
if (kciphers == NULL) {
warn(gettext("failed to allocate memory for list of "
"kernel mechanisms"));
goto kcc_out;
}
kciphers->ml_count = count;
if (ioctl(fd, CRYPTO_GET_MECHANISM_LIST, kciphers) == -1) {
warn(gettext("CRYPTO_GET_MECHANISM_LIST ioctl failed"));
goto kcc_out;
}
}
if (kciphers->ml_return_value != CRYPTO_SUCCESS) {
warn(gettext(
"CRYPTO_GET_MECHANISM_LIST ioctl return value = %d\n"),
kciphers->ml_return_value);
goto kcc_out;
}
count = kciphers->ml_count;
for (i = 0; i < count && !(ciph_ok && iv_ok); i++) {
if (!ciph_ok &&
strcasecmp(cipher->name, kciphers->ml_list[i]) == 0)
ciph_ok = B_TRUE;
if (!iv_ok &&
strcasecmp(cipher->iv_name, kciphers->ml_list[i]) == 0)
iv_ok = B_TRUE;
}
free(kciphers);
kciphers = NULL;
if (!ciph_ok)
warn(gettext("%s mechanism not supported in kernel\n"),
cipher->name);
if (!iv_ok)
warn(gettext("%s mechanism not supported in kernel\n"),
cipher->iv_name);
if (ciph_ok) {
count = DEFAULT_MECHINFO_NUM;
kinfo = malloc(sizeof (crypto_get_all_mechanism_info_t) +
sizeof (crypto_mechanism_info_t) * (count - 1));
if (kinfo == NULL) {
warn(gettext("failed to allocate memory for "
"kernel mechanism info"));
goto kcc_out;
}
kinfo->mi_count = count;
(void) strlcpy(kinfo->mi_mechanism_name, cipher->name,
CRYPTO_MAX_MECH_NAME);
if (ioctl(fd, CRYPTO_GET_ALL_MECHANISM_INFO, kinfo) == -1) {
warn(gettext(
"CRYPTO_GET_ALL_MECHANISM_INFO ioctl failed"));
goto kcc_out;
}
if (kinfo->mi_return_value == CRYPTO_BUFFER_TOO_SMALL) {
count = kinfo->mi_count;
free(kinfo);
kinfo = malloc(
sizeof (crypto_get_all_mechanism_info_t) +
sizeof (crypto_mechanism_info_t) * (count - 1));
if (kinfo == NULL) {
warn(gettext("failed to allocate memory for "
"kernel mechanism info"));
goto kcc_out;
}
kinfo->mi_count = count;
(void) strlcpy(kinfo->mi_mechanism_name, cipher->name,
CRYPTO_MAX_MECH_NAME);
if (ioctl(fd, CRYPTO_GET_ALL_MECHANISM_INFO, kinfo) ==
-1) {
warn(gettext("CRYPTO_GET_ALL_MECHANISM_INFO "
"ioctl failed"));
goto kcc_out;
}
}
if (kinfo->mi_return_value != CRYPTO_SUCCESS) {
warn(gettext("CRYPTO_GET_ALL_MECHANISM_INFO ioctl "
"return value = %d\n"), kinfo->mi_return_value);
goto kcc_out;
}
count = kinfo->mi_count;
i = 0;
if (i < count) {
keymin = kinfo->mi_list[i].mi_min_key_size;
keymax = kinfo->mi_list[i].mi_max_key_size;
if (kinfo->mi_list[i].mi_keysize_unit &
CRYPTO_KEYSIZE_UNIT_IN_BITS) {
keymin = CRYPTO_BITS2BYTES(keymin);
keymax = CRYPTO_BITS2BYTES(keymax);
}
cipher->min_keysize = keymin;
cipher->max_keysize = keymax;
}
free(kinfo);
kinfo = NULL;
if (i == count) {
(void) close(fd);
die(gettext(
"failed to find usable %s kernel mechanism, "
"use \"cryptoadm list -m\" to find available "
"mechanisms\n"),
cipher->name);
}
}
return (ciph_ok && iv_ok);
kcc_out:
if (kinfo != NULL)
free(kinfo);
if (kciphers != NULL)
free(kciphers);
if (fd != -1)
(void) close(fd);
return (B_FALSE);
}
static token_spec_t *
parsetoken(char *spec)
{
#define FLD_NAME 0
#define FLD_MANUF 1
#define FLD_SERIAL 2
#define FLD_LABEL 3
#define NFIELDS 4
#define nullfield(i) ((field[(i)+1] - field[(i)]) <= 1)
#define copyfield(fld, i) \
{ \
int n; \
(fld) = NULL; \
if ((n = (field[(i)+1] - field[(i)])) > 1) { \
if (((fld) = malloc(n)) != NULL) { \
(void) strncpy((fld), field[(i)], n); \
((fld))[n - 1] = '\0'; \
} \
} \
}
int i;
char *field[NFIELDS + 1];
token_spec_t *ti = NULL;
if (spec == NULL)
return (NULL);
field[0] = spec;
for (i = 1; i < NFIELDS + 1; i++) {
field[i] = strchr(field[i-1], ':');
if (field[i] == NULL)
break;
field[i]++;
}
if (i < NFIELDS)
return (NULL);
if (field[NFIELDS] != NULL)
return (NULL);
field[NFIELDS] = strchr(field[NFIELDS-1], '\0') + 1;
if (nullfield(FLD_LABEL))
return (NULL);
ti = malloc(sizeof (token_spec_t));
if (ti == NULL)
return (NULL);
copyfield(ti->name, FLD_NAME);
copyfield(ti->mfr, FLD_MANUF);
copyfield(ti->serno, FLD_SERIAL);
copyfield(ti->key, FLD_LABEL);
if (ti->name == NULL && ti->mfr == NULL && ti->serno == NULL)
ti->name = strdup(pkcs11_default_token());
return (ti);
}
static void
getkeyfromuser(mech_alias_t *cipher, char **raw_key, size_t *raw_key_sz,
boolean_t with_confirmation)
{
CK_SESSION_HANDLE sess;
CK_RV rv;
char *pass = NULL;
size_t passlen = 0;
void *salt = NULL;
size_t saltlen = 0;
CK_KEY_TYPE ktype;
void *kvalue;
size_t klen;
if (cipher->slot == (CK_SLOT_ID)-1 || cipher->max_keysize == 0) {
rv = CKR_MECHANISM_INVALID;
goto cleanup;
}
rv = pkcs11_mech2keytype(cipher->type, &ktype);
if (rv != CKR_OK)
goto cleanup;
rv = C_OpenSession(cipher->slot, CKF_SERIAL_SESSION, NULL, NULL, &sess);
if (rv != CKR_OK)
goto cleanup;
if (pkcs11_get_pass(NULL, &pass, &passlen, MIN_PASSLEN,
with_confirmation) < 0) {
die(gettext("passphrases do not match\n"));
}
salt = pass;
saltlen = passlen;
klen = cipher->max_keysize;
rv = pkcs11_PasswdToKey(sess, pass, passlen, salt, saltlen, ktype,
cipher->max_keysize, &kvalue, &klen);
(void) C_CloseSession(sess);
if (rv != CKR_OK) {
goto cleanup;
}
*raw_key_sz = klen;
*raw_key = (char *)kvalue;
return;
cleanup:
die(gettext("failed to generate %s key from passphrase: %s"),
cipher->alias, pkcs11_strerror(rv));
}
void
getkeyfromfile(const char *pathname, mech_alias_t *cipher, char **key,
size_t *ksz)
{
int fd;
struct stat sbuf;
boolean_t notplain = B_FALSE;
ssize_t cursz;
ssize_t nread;
if (pathname == NULL) {
*ksz = cipher->max_keysize;
*key = malloc(*ksz);
if (*key == NULL)
die(gettext("failed to allocate memory for"
" ephemeral key"));
if (pkcs11_get_urandom(*key, *ksz) < 0) {
free(*key);
die(gettext("failed to get enough random data"));
}
return;
}
if ((fd = open(pathname, O_RDONLY, 0)) == -1)
die(gettext("open of keyfile (%s) failed"), pathname);
if (fstat(fd, &sbuf) == -1)
die(gettext("fstat of keyfile (%s) failed"), pathname);
if (S_ISREG(sbuf.st_mode)) {
if ((sbuf.st_mode & (S_IWGRP | S_IWOTH)) != 0)
die(gettext("insecure permissions on keyfile %s\n"),
pathname);
*ksz = sbuf.st_size;
if (*ksz < cipher->min_keysize || cipher->max_keysize < *ksz) {
warn(gettext("%s: invalid keysize: %d\n"),
pathname, (int)*ksz);
die(gettext("\t%d <= keysize <= %d\n"),
cipher->min_keysize, cipher->max_keysize);
}
} else {
*ksz = cipher->max_keysize;
notplain = B_TRUE;
}
*key = malloc(*ksz);
if (*key == NULL)
die(gettext("failed to allocate memory for key from file"));
for (cursz = 0, nread = 0; cursz < *ksz; cursz += nread) {
nread = read(fd, *key, *ksz);
if (nread > 0)
continue;
if (nread == 0 && notplain && cursz >= cipher->min_keysize) {
*ksz = (cursz / cipher->min_keysize) *
cipher->min_keysize;
break;
}
die(gettext("%s: can't read all keybytes"), pathname);
}
(void) close(fd);
}
void
getkeyfromtoken(CK_SESSION_HANDLE sess,
token_spec_t *token, const char *keyfile, mech_alias_t *cipher,
char **raw_key, size_t *raw_key_sz)
{
CK_RV rv = CKR_OK;
CK_BBOOL trueval = B_TRUE;
CK_OBJECT_CLASS kclass;
CK_KEY_TYPE ktype;
CK_KEY_TYPE raw_ktype;
CK_ATTRIBUTE key_tmpl[] = {
{ CKA_CLASS, NULL, 0 },
{ CKA_KEY_TYPE, NULL, 0 },
{ CKA_LABEL, NULL, 0 },
{ CKA_TOKEN, NULL, 0 },
{ CKA_PRIVATE, NULL, 0 }
};
CK_ULONG attrs = sizeof (key_tmpl) / sizeof (CK_ATTRIBUTE);
int i;
char *pass = NULL;
size_t passlen = 0;
CK_OBJECT_HANDLE obj, rawobj;
CK_ULONG num_objs = 1;
CK_MECHANISM unwrap = { CKM_RSA_PKCS, NULL, 0 };
char *rkey;
size_t rksz;
if (token == NULL || token->key == NULL)
return;
if (cipher->slot == (CK_SLOT_ID)-1 || cipher->max_keysize == 0) {
die(gettext("failed to find any cryptographic provider, "
"use \"cryptoadm list -p\" to find providers: %s\n"),
pkcs11_strerror(CKR_MECHANISM_INVALID));
}
if (pkcs11_get_pass(token->name, &pass, &passlen, 0, B_FALSE) < 0)
die(gettext("unable to get passphrase"));
if (pass != NULL && passlen > 0) {
rv = C_Login(sess, CKU_USER, (CK_UTF8CHAR_PTR)pass, passlen);
if (rv != CKR_OK) {
die(gettext("cannot login to the token %s: %s\n"),
token->name, pkcs11_strerror(rv));
}
}
rv = pkcs11_mech2keytype(cipher->type, &raw_ktype);
if (rv != CKR_OK) {
die(gettext("failed to get key type for cipher %s: %s\n"),
cipher->name, pkcs11_strerror(rv));
}
if (keyfile == NULL) {
kclass = CKO_SECRET_KEY;
ktype = raw_ktype;
} else {
kclass = CKO_PRIVATE_KEY;
ktype = CKK_RSA;
}
for (i = 0; i < attrs; i++) {
switch (key_tmpl[i].type) {
case CKA_CLASS:
key_tmpl[i].pValue = &kclass;
key_tmpl[i].ulValueLen = sizeof (kclass);
break;
case CKA_KEY_TYPE:
key_tmpl[i].pValue = &ktype;
key_tmpl[i].ulValueLen = sizeof (ktype);
break;
case CKA_LABEL:
key_tmpl[i].pValue = token->key;
key_tmpl[i].ulValueLen = strlen(token->key);
break;
case CKA_TOKEN:
key_tmpl[i].pValue = &trueval;
key_tmpl[i].ulValueLen = sizeof (trueval);
break;
case CKA_PRIVATE:
key_tmpl[i].pValue = &trueval;
key_tmpl[i].ulValueLen = sizeof (trueval);
break;
default:
break;
}
}
rv = C_FindObjectsInit(sess, key_tmpl, attrs);
if (rv != CKR_OK)
die(gettext("cannot find key %s: %s\n"), token->key,
pkcs11_strerror(rv));
rv = C_FindObjects(sess, &obj, 1, &num_objs);
(void) C_FindObjectsFinal(sess);
if (num_objs == 0) {
die(gettext("cannot find key %s\n"), token->key);
} else if (rv != CKR_OK) {
die(gettext("cannot find key %s: %s\n"), token->key,
pkcs11_strerror(rv));
}
if (keyfile == NULL) {
rv = pkcs11_ObjectToKey(sess, obj, (void **)&rkey, &rksz,
B_FALSE);
if (rv != CKR_OK) {
die(gettext("failed to get key value for %s"
" from token %s, %s\n"), token->key,
token->name, pkcs11_strerror(rv));
}
} else {
getkeyfromfile(keyfile, cipher, &rkey, &rksz);
kclass = CKO_SECRET_KEY;
ktype = raw_ktype;
rv = C_UnwrapKey(sess, &unwrap, obj, (CK_BYTE_PTR)rkey,
rksz, key_tmpl, 2, &rawobj);
if (rv != CKR_OK) {
die(gettext("failed to unwrap key in keyfile %s,"
" %s\n"), keyfile, pkcs11_strerror(rv));
}
rv = pkcs11_ObjectToKey(sess, rawobj, (void **)&rkey, &rksz,
B_TRUE);
if (rv != CKR_OK) {
die(gettext("failed to get unwrapped key value for"
" key in keyfile %s, %s\n"), keyfile,
pkcs11_strerror(rv));
}
}
if (rksz < cipher->min_keysize || cipher->max_keysize < rksz) {
warn(gettext("%s: invalid keysize: %d\n"), keyfile, (int)rksz);
die(gettext("\t%d <= keysize <= %d\n"), cipher->min_keysize,
cipher->max_keysize);
}
*raw_key_sz = rksz;
*raw_key = (char *)rkey;
}
boolean_t
match_token_cipher(CK_SLOT_ID slot_id, void *args, CK_RV *rv)
{
token_spec_t *token;
mech_alias_t *cipher;
CK_TOKEN_INFO tokinfo;
CK_MECHANISM_INFO mechinfo;
boolean_t token_match;
*rv = CKR_FUNCTION_FAILED;
if (args == NULL) {
return (B_FALSE);
}
cipher = (mech_alias_t *)args;
token = cipher->token;
if (C_GetMechanismInfo(slot_id, cipher->type, &mechinfo) != CKR_OK) {
return (B_FALSE);
}
if (token == NULL) {
if (C_GetMechanismInfo(slot_id, CKM_PKCS5_PBKD2, &mechinfo) !=
CKR_OK) {
return (B_FALSE);
}
goto foundit;
}
if (token->key == NULL || (C_GetTokenInfo(slot_id, &tokinfo) != CKR_OK))
return (B_FALSE);
token_match = B_TRUE;
if (token->name != NULL && (token->name)[0] != '\0' &&
strncmp((char *)token->name, (char *)tokinfo.label,
TOKEN_LABEL_SIZE) != 0)
token_match = B_FALSE;
if (token->mfr != NULL && (token->mfr)[0] != '\0' &&
strncmp((char *)token->mfr, (char *)tokinfo.manufacturerID,
TOKEN_MANUFACTURER_SIZE) != 0)
token_match = B_FALSE;
if (token->serno != NULL && (token->serno)[0] != '\0' &&
strncmp((char *)token->serno, (char *)tokinfo.serialNumber,
TOKEN_SERIAL_SIZE) != 0)
token_match = B_FALSE;
if (!token_match)
return (B_FALSE);
foundit:
cipher->slot = slot_id;
return (B_TRUE);
}
static void
end_crypto(CK_SESSION_HANDLE sess)
{
(void) C_CloseSession(sess);
(void) C_Finalize(NULL);
}
static void
init_crypto(token_spec_t *token, mech_alias_t *cipher,
CK_SESSION_HANDLE_PTR sess)
{
CK_RV rv;
cipher->token = token;
if (setenv("METASLOT_ENABLED", "false", 1) < 0) {
die(gettext("could not disable Metaslot"));
}
rv = pkcs11_GetCriteriaSession(match_token_cipher, (void *)cipher,
sess);
if (rv != CKR_OK) {
end_crypto(*sess);
if (rv == CKR_HOST_MEMORY) {
die("malloc");
}
die(gettext("failed to find any cryptographic provider, "
"use \"cryptoadm list -p\" to find providers: %s\n"),
pkcs11_strerror(rv));
}
}
static void
lofi_uncompress(int lfd, const char *filename)
{
struct lofi_ioctl li;
char buf[MAXBSIZE];
char devicename[32];
char tmpfilename[MAXPATHLEN];
char *x;
char *dir = NULL;
char *file = NULL;
int minor = 0;
struct stat64 statbuf;
int compfd = -1;
int uncompfd = -1;
ssize_t rbytes;
li.li_crypto_enabled = B_FALSE;
li.li_id = 0;
(void) strlcpy(li.li_filename, filename, sizeof (li.li_filename));
if (ioctl(lfd, LOFI_GET_MINOR, &li) != -1)
die(gettext("%s must be unmapped before uncompressing"),
filename);
if (stat64(filename, &statbuf) == -1)
die(gettext("stat: %s"), filename);
if (statbuf.st_size == 0)
return;
minor = lofi_map_file(lfd, &li, filename);
(void) snprintf(devicename, sizeof (devicename), "/dev/%s/%d",
LOFI_BLOCK_NAME, minor);
if ((ioctl(lfd, LOFI_CHECK_COMPRESSED, &li) == -1) ||
(li.li_algorithm[0] == '\0')) {
delete_mapping(lfd, devicename, filename, B_TRUE);
die("%s is not compressed\n", filename);
}
if ((compfd = open64(devicename, O_RDONLY | O_NONBLOCK)) == -1) {
delete_mapping(lfd, devicename, filename, B_TRUE);
die(gettext("open: %s"), filename);
}
x = strdup(filename);
dir = strdup(dirname(x));
free(x);
x = strdup(filename);
file = strdup(basename(x));
free(x);
(void) snprintf(tmpfilename, sizeof (tmpfilename),
"%s/.%sXXXXXX", dir, file);
free(dir);
free(file);
if ((uncompfd = mkstemp64(tmpfilename)) == -1) {
(void) close(compfd);
delete_mapping(lfd, devicename, filename, B_TRUE);
die("%s could not be uncompressed\n", filename);
}
(void) fchmod(uncompfd, statbuf.st_mode);
if (fchown(uncompfd, statbuf.st_uid, statbuf.st_gid) == -1) {
(void) close(compfd);
(void) close(uncompfd);
delete_mapping(lfd, devicename, filename, B_TRUE);
die("%s could not be uncompressed\n", filename);
}
for (;;) {
rbytes = read(compfd, buf, sizeof (buf));
if (rbytes <= 0)
break;
if (write(uncompfd, buf, rbytes) != rbytes) {
rbytes = -1;
break;
}
}
(void) close(compfd);
(void) close(uncompfd);
delete_mapping(lfd, devicename, filename, B_TRUE);
if (rbytes < 0) {
(void) unlink(tmpfilename);
die(gettext("could not read from %s"), filename);
}
if (rename(tmpfilename, filename) == -1)
(void) unlink(tmpfilename);
}
static void
lofi_compress(int *lfd, const char *filename, int compress_index,
uint32_t segsize)
{
struct lofi_ioctl lic;
lofi_compress_info_t *li;
struct flock lock;
char tmpfilename[MAXPATHLEN];
char comp_filename[MAXPATHLEN];
char algorithm[MAXALGLEN];
char *x;
char *dir = NULL, *file = NULL;
uchar_t *uncompressed_seg = NULL;
uchar_t *compressed_seg = NULL;
uint32_t compressed_segsize;
uint32_t len_compressed, count;
uint32_t index_entries, index_sz;
uint64_t *index = NULL;
uint64_t offset;
size_t real_segsize;
struct stat64 statbuf;
int compfd = -1, uncompfd = -1;
int tfd = -1;
ssize_t rbytes, wbytes, lastread;
int i, type;
lic.li_id = 0;
(void) strlcpy(lic.li_filename, filename, sizeof (lic.li_filename));
if (ioctl(*lfd, LOFI_GET_MINOR, &lic) != -1)
die(gettext("%s must be unmapped before compressing"),
filename);
(void) close(*lfd);
*lfd = -1;
li = &lofi_compress_table[compress_index];
compressed_segsize = segsize + (segsize >> 6);
compressed_seg = (uchar_t *)malloc(compressed_segsize + SEGHDR);
uncompressed_seg = (uchar_t *)malloc(segsize);
if (compressed_seg == NULL || uncompressed_seg == NULL)
die(gettext("No memory"));
if ((uncompfd = open64(filename, O_RDWR|O_LARGEFILE, 0)) == -1)
die(gettext("open: %s"), filename);
lock.l_type = F_WRLCK;
lock.l_whence = SEEK_SET;
lock.l_start = 0;
lock.l_len = 0;
if (fcntl(uncompfd, F_SETLKW, &lock) == -1)
die(gettext("fcntl: %s"), filename);
if (fstat64(uncompfd, &statbuf) == -1) {
(void) close(uncompfd);
die(gettext("fstat: %s"), filename);
}
if (statbuf.st_size == 0) {
(void) close(uncompfd);
return;
}
x = strdup(filename);
dir = strdup(dirname(x));
free(x);
x = strdup(filename);
file = strdup(basename(x));
free(x);
(void) snprintf(tmpfilename, sizeof (tmpfilename),
"%s/.%sXXXXXX", dir, file);
(void) snprintf(comp_filename, sizeof (comp_filename),
"%s/.%sXXXXXX", dir, file);
free(dir);
free(file);
if ((tfd = mkstemp64(tmpfilename)) == -1)
goto cleanup;
if ((compfd = mkstemp64(comp_filename)) == -1)
goto cleanup;
(void) fchmod(compfd, statbuf.st_mode);
if (fchown(compfd, statbuf.st_uid, statbuf.st_gid) == -1)
goto cleanup;
index_sz = (statbuf.st_size / segsize) + 2;
index = malloc(sizeof (*index) * index_sz);
if (index == NULL)
goto cleanup;
offset = 0;
lastread = segsize;
count = 0;
for (;;) {
rbytes = read(uncompfd, uncompressed_seg, segsize);
if (rbytes <= 0)
break;
if (lastread < segsize)
goto cleanup;
real_segsize = segsize - 1;
(void) li->l_compress(uncompressed_seg, rbytes,
compressed_seg + SEGHDR, &real_segsize, li->l_level);
len_compressed = real_segsize;
if (segsize <= COMPRESS_THRESHOLD ||
real_segsize > (segsize - COMPRESS_THRESHOLD)) {
(void) memcpy(compressed_seg + SEGHDR, uncompressed_seg,
rbytes);
type = UNCOMPRESSED;
len_compressed = rbytes;
} else {
type = COMPRESSED;
}
*compressed_seg = type;
wbytes = write(tfd, compressed_seg, len_compressed + SEGHDR);
if (wbytes != (len_compressed + SEGHDR)) {
rbytes = -1;
break;
}
index[count] = BE_64(offset);
offset += wbytes;
lastread = rbytes;
count++;
}
(void) close(uncompfd);
if (rbytes < 0)
goto cleanup;
index[count++] = BE_64(offset);
(void) bzero(algorithm, sizeof (algorithm));
(void) strlcpy(algorithm, li->l_name, sizeof (algorithm));
if (write(compfd, algorithm, sizeof (algorithm))
!= sizeof (algorithm))
goto cleanup;
segsize = htonl(segsize);
if (write(compfd, &segsize, sizeof (segsize)) != sizeof (segsize))
goto cleanup;
index_entries = htonl(count);
if (write(compfd, &index_entries, sizeof (index_entries)) !=
sizeof (index_entries))
goto cleanup;
lastread = htonl(lastread);
if (write(compfd, &lastread, sizeof (lastread)) != sizeof (lastread))
goto cleanup;
for (i = 0; i < count; i++) {
if (write(compfd, index + i, sizeof (*index)) !=
sizeof (*index))
goto cleanup;
}
if (lseek(tfd, 0, SEEK_SET) != 0)
goto cleanup;
rbytes = wbytes = 0;
for (;;) {
rbytes = read(tfd, compressed_seg, compressed_segsize + SEGHDR);
if (rbytes <= 0)
break;
if (write(compfd, compressed_seg, rbytes) != rbytes)
goto cleanup;
}
if (fstat64(compfd, &statbuf) == -1)
goto cleanup;
if ((offset = statbuf.st_size % DEV_BSIZE) > 0) {
offset = DEV_BSIZE - offset;
for (i = 0; i < offset; i++)
uncompressed_seg[i] = '\0';
if (write(compfd, uncompressed_seg, offset) != offset)
goto cleanup;
}
(void) close(compfd);
(void) close(tfd);
(void) unlink(tmpfilename);
cleanup:
if (rbytes < 0) {
if (tfd != -1)
(void) unlink(tmpfilename);
if (compfd != -1)
(void) unlink(comp_filename);
die(gettext("error compressing file %s"), filename);
} else {
if (rename(comp_filename, filename) == -1) {
(void) unlink(comp_filename);
die(gettext("error compressing file %s"), filename);
}
}
if (compressed_seg != NULL)
free(compressed_seg);
if (uncompressed_seg != NULL)
free(uncompressed_seg);
if (index != NULL)
free(index);
if (compfd != -1)
(void) close(compfd);
if (uncompfd != -1)
(void) close(uncompfd);
if (tfd != -1)
(void) close(tfd);
}
static int
lofi_compress_select(const char *algname)
{
int i;
for (i = 0; i < LOFI_COMPRESS_FUNCTIONS; i++) {
if (strcmp(lofi_compress_table[i].l_name, algname) == 0)
return (i);
}
return (-1);
}
static void
check_algorithm_validity(const char *algname, int *compress_index)
{
*compress_index = lofi_compress_select(algname);
if (*compress_index < 0)
die(gettext("invalid algorithm name: %s\n"), algname);
}
static void
check_file_validity(const char *filename)
{
struct stat64 buf;
int error;
int fd;
fd = open64(filename, O_RDONLY);
if (fd == -1) {
die(gettext("open: %s"), filename);
}
error = fstat64(fd, &buf);
if (error == -1) {
die(gettext("fstat: %s"), filename);
} else if (!S_ISLOFIABLE(buf.st_mode)) {
die(gettext("%s is not a regular file, "
"block, or character device\n"),
filename);
} else if ((buf.st_size % DEV_BSIZE) != 0) {
die(gettext("size of %s is not a multiple of %d\n"),
filename, DEV_BSIZE);
}
(void) close(fd);
if (name_to_minor(filename) != 0) {
die(gettext("cannot use %s on itself\n"), LOFI_DRIVER_NAME);
}
}
static boolean_t
check_file_is_encrypted(const char *filename)
{
int fd;
char buf[sizeof (lofi_crypto_magic)];
int got;
int rest = sizeof (lofi_crypto_magic);
fd = open64(filename, O_RDONLY);
if (fd == -1)
die(gettext("failed to open: %s"), filename);
if (lseek(fd, CRYOFF, SEEK_SET) != CRYOFF)
die(gettext("failed to seek to offset 0x%lx in file %s"),
CRYOFF, filename);
do {
got = read(fd, buf + sizeof (lofi_crypto_magic) - rest, rest);
if ((got == 0) || ((got == -1) && (errno != EINTR)))
die(gettext("failed to read crypto header"
" at offset 0x%lx in file %s"), CRYOFF, filename);
if (got > 0)
rest -= got;
} while (rest > 0);
while (close(fd) == -1) {
if (errno != EINTR)
die(gettext("failed to close file %s"), filename);
}
return (strncmp(buf, lofi_crypto_magic,
sizeof (lofi_crypto_magic)) == 0);
}
static uint32_t
convert_to_num(const char *str)
{
int len;
uint32_t segsize, mult = 1;
len = strlen(str);
if (len && isalpha(str[len - 1])) {
switch (str[len - 1]) {
case 'k':
case 'K':
mult = KILOBYTE;
break;
case 'b':
case 'B':
mult = BLOCK_SIZE;
break;
case 'm':
case 'M':
mult = MEGABYTE;
break;
case 'g':
case 'G':
mult = GIGABYTE;
break;
default:
die(gettext("invalid segment size %s\n"), str);
}
}
segsize = atol(str);
segsize *= mult;
return (segsize);
}
int
main(int argc, char *argv[])
{
int lfd;
int c;
const char *devicename = NULL;
const char *filename = NULL;
const char *algname = COMPRESS_ALGORITHM;
int openflag;
int minor;
int compress_index;
uint32_t segsize = SEGSIZE;
static char *lofictl = "/dev/" LOFI_CTL_NAME;
boolean_t force = B_FALSE;
const char *pname;
boolean_t errflag = B_FALSE;
boolean_t addflag = B_FALSE;
boolean_t labelflag = B_FALSE;
boolean_t rdflag = B_FALSE;
boolean_t deleteflag = B_FALSE;
boolean_t ephflag = B_FALSE;
boolean_t compressflag = B_FALSE;
boolean_t uncompressflag = B_FALSE;
boolean_t need_crypto = B_FALSE;
boolean_t cipher_only = B_TRUE;
const char *keyfile = NULL;
mech_alias_t *cipher = NULL;
token_spec_t *token = NULL;
char *rkey = NULL;
size_t rksz = 0;
char realfilename[MAXPATHLEN];
pname = getpname(argv[0]);
(void) setlocale(LC_ALL, "");
(void) textdomain(TEXT_DOMAIN);
while ((c = getopt(argc, argv, "a:c:Cd:efk:lrs:T:U")) != EOF) {
switch (c) {
case 'a':
addflag = B_TRUE;
if ((filename = realpath(optarg, realfilename)) == NULL)
die("%s", optarg);
if (((argc - optind) > 0) && (*argv[optind] != '-')) {
devicename = argv[optind];
optind++;
}
break;
case 'C':
compressflag = B_TRUE;
if (((argc - optind) > 1) && (*argv[optind] != '-')) {
algname = argv[optind];
optind++;
}
check_algorithm_validity(algname, &compress_index);
break;
case 'c':
if ((cipher = ciph2mech(optarg)) == NULL) {
errflag = B_TRUE;
warn(gettext("cipher %s not allowed\n"),
optarg);
}
need_crypto = B_TRUE;
break;
case 'd':
deleteflag = B_TRUE;
minor = name_to_minor(optarg);
if (minor != 0)
devicename = optarg;
else {
if ((filename = realpath(optarg,
realfilename)) == NULL)
die("%s", optarg);
}
break;
case 'e':
ephflag = B_TRUE;
need_crypto = B_TRUE;
cipher_only = B_FALSE;
break;
case 'f':
force = B_TRUE;
break;
case 'k':
keyfile = optarg;
need_crypto = B_TRUE;
cipher_only = B_FALSE;
break;
case 'l':
labelflag = B_TRUE;
break;
case 'r':
rdflag = B_TRUE;
break;
case 's':
segsize = convert_to_num(optarg);
if (segsize < DEV_BSIZE || !ISP2(segsize))
die(gettext("segment size %s is invalid "
"or not a multiple of minimum block "
"size %ld\n"), optarg, DEV_BSIZE);
break;
case 'T':
if ((token = parsetoken(optarg)) == NULL) {
errflag = B_TRUE;
warn(
gettext("invalid token key specifier %s\n"),
optarg);
}
need_crypto = B_TRUE;
cipher_only = B_FALSE;
break;
case 'U':
uncompressflag = B_TRUE;
break;
case '?':
default:
errflag = B_TRUE;
break;
}
}
if (errflag ||
(addflag && deleteflag) ||
(labelflag && !addflag) ||
(rdflag && !addflag) ||
(!addflag && need_crypto) ||
(need_crypto && labelflag) ||
((compressflag || uncompressflag) &&
(labelflag || addflag || deleteflag)))
usage(pname);
if (ephflag && (keyfile != NULL || token != NULL)) {
die(gettext("ephemeral key cannot be used with keyfile"
" or token key\n"));
}
switch (argc - optind) {
case 0:
if (compressflag || uncompressflag)
usage(pname);
break;
case 1:
if (addflag || deleteflag)
usage(pname);
if (compressflag || uncompressflag) {
if ((filename = realpath(argv[optind],
realfilename)) == NULL)
die("%s", argv[optind]);
} else {
minor = name_to_minor(argv[optind]);
if (minor != 0)
devicename = argv[optind];
else {
if ((filename = realpath(argv[optind],
realfilename)) == NULL)
die("%s", argv[optind]);
}
}
break;
default:
usage(pname);
break;
}
if (addflag || compressflag || uncompressflag)
check_file_validity(filename);
if (filename && !valid_abspath(filename))
exit(E_ERROR);
openflag = O_EXCL;
if (addflag || deleteflag || compressflag || uncompressflag)
openflag |= O_RDWR;
else
openflag |= O_RDONLY;
lfd = open(lofictl, openflag);
if (lfd == -1) {
if ((errno == EPERM) || (errno == EACCES)) {
die(gettext("you do not have permission to perform "
"that operation.\n"));
} else {
die(gettext("open: %s"), lofictl);
}
}
if (need_crypto) {
CK_SESSION_HANDLE sess;
if (cipher == NULL)
cipher = DEFAULT_CIPHER;
if (!kernel_cipher_check(cipher))
die(gettext(
"use \"cryptoadm list -m\" to find available "
"mechanisms\n"));
init_crypto(token, cipher, &sess);
if (cipher_only) {
getkeyfromuser(cipher, &rkey, &rksz,
!check_file_is_encrypted(filename));
} else if (token != NULL) {
getkeyfromtoken(sess, token, keyfile, cipher,
&rkey, &rksz);
} else {
getkeyfromfile(keyfile, cipher, &rkey, &rksz);
}
end_crypto(sess);
}
if (addflag)
add_mapping(lfd, devicename, filename, cipher, rkey, rksz,
rdflag, labelflag);
else if (compressflag)
lofi_compress(&lfd, filename, compress_index, segsize);
else if (uncompressflag)
lofi_uncompress(lfd, filename);
else if (deleteflag)
delete_mapping(lfd, devicename, filename, force);
else if (filename || devicename)
print_one_mapping(lfd, devicename, filename);
else
print_mappings(lfd);
if (lfd != -1)
(void) close(lfd);
closelib();
return (E_SUCCESS);
}