Print this page
5857 add -o option to lofiadm

Split Close
Expand all
Collapse all
          --- old/usr/src/cmd/lofiadm/main.c
          +++ new/usr/src/cmd/lofiadm/main.c
↓ open down ↓ 17 lines elided ↑ open up ↑
  18   18   *
  19   19   * CDDL HEADER END
  20   20   */
  21   21  /*
  22   22   * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
  23   23   * Use is subject to license terms.
  24   24   * Copyright 2012 Joyent, Inc.  All rights reserved.
  25   25   *
  26   26   * Copyright 2013 Nexenta Systems, Inc. All rights reserved.
  27   27   * Copyright (c) 2014 Gary Mills
       28 + * Copyright (c) 2016 Andrey Sokolov
  28   29   */
  29   30  
  30   31  /*
  31   32   * lofiadm - administer lofi(7d). Very simple, add and remove file<->device
  32   33   * associations, and display status. All the ioctls are private between
  33   34   * lofi and lofiadm, and so are very simple - device information is
  34   35   * communicated via a minor number.
  35   36   */
  36   37  
  37   38  #include <sys/types.h>
↓ open down ↓ 23 lines elided ↑ open up ↑
  61   62  #include "utils.h"
  62   63  #include <LzmaEnc.h>
  63   64  
  64   65  /* Only need the IV len #defines out of these files, nothing else. */
  65   66  #include <aes/aes_impl.h>
  66   67  #include <des/des_impl.h>
  67   68  #include <blowfish/blowfish_impl.h>
  68   69  
  69   70  static const char USAGE[] =
  70   71          "Usage: %s [-r] -a file [ device ]\n"
  71      -        "       %s [-r] -c crypto_algorithm -a file [device]\n"
       72 +        "       %s [-r] [-o] -c crypto_algorithm -a file [device]\n"
  72   73          "       %s [-r] -c crypto_algorithm -k raw_key_file -a file [device]\n"
  73   74          "       %s [-r] -c crypto_algorithm -T [token]:[manuf]:[serial]:key "
  74   75          "-a file [device]\n"
  75   76          "       %s [-r] -c crypto_algorithm -T [token]:[manuf]:[serial]:key "
  76   77          "-k wrapped_key_file -a file [device]\n"
  77   78          "       %s [-r] -c crypto_algorithm -e -a file [device]\n"
  78   79          "       %s -d file | device\n"
  79   80          "       %s -C [gzip|gzip-6|gzip-9|lzma] [-s segment_size] file\n"
  80   81          "       %s -U file\n"
  81   82          "       %s [ file | device ]\n";
↓ open down ↓ 126 lines elided ↑ open up ↑
 208  209          SzAlloc,
 209  210          SzFree
 210  211  };
 211  212  
 212  213  #define LZMA_UNCOMPRESSED_SIZE  8
 213  214  #define LZMA_HEADER_SIZE (LZMA_PROPS_SIZE + LZMA_UNCOMPRESSED_SIZE)
 214  215  
 215  216  /*ARGSUSED*/
 216  217  static int
 217  218  lzma_compress(void *src, size_t srclen, void *dst,
 218      -        size_t *dstlen, int level)
      219 +    size_t *dstlen, int level)
 219  220  {
 220  221          CLzmaEncProps props;
 221  222          size_t outsize2;
 222  223          size_t outsizeprocessed;
 223  224          size_t outpropssize = LZMA_PROPS_SIZE;
 224  225          uint64_t t = 0;
 225  226          SRes res;
 226  227          Byte *dstp;
 227  228          int i;
 228  229  
↓ open down ↓ 599 lines elided ↑ open up ↑
 828  829           */
 829  830          if (ti->name == NULL && ti->mfr == NULL && ti->serno == NULL)
 830  831                  ti->name = strdup(pkcs11_default_token());
 831  832          return (ti);
 832  833  }
 833  834  
 834  835  /*
 835  836   * PBE the passphrase into a raw key
 836  837   */
 837  838  static void
 838      -getkeyfromuser(mech_alias_t *cipher, char **raw_key, size_t *raw_key_sz)
      839 +getkeyfromuser(mech_alias_t *cipher, char **raw_key, size_t *raw_key_sz,
      840 +    boolean_t with_confirmation)
 839  841  {
 840  842          CK_SESSION_HANDLE sess;
 841  843          CK_RV   rv;
 842  844          char    *pass = NULL;
 843  845          size_t  passlen = 0;
 844  846          void    *salt = NULL;   /* don't use NULL, see note on salt below */
 845  847          size_t  saltlen = 0;
 846  848          CK_KEY_TYPE ktype;
 847  849          void    *kvalue;
 848  850          size_t  klen;
↓ open down ↓ 10 lines elided ↑ open up ↑
 859  861  
 860  862          /*
 861  863           * use the passphrase to generate a PBE PKCS#5 secret key and
 862  864           * retrieve the raw key data to eventually pass it to the kernel;
 863  865           */
 864  866          rv = C_OpenSession(cipher->slot, CKF_SERIAL_SESSION, NULL, NULL, &sess);
 865  867          if (rv != CKR_OK)
 866  868                  goto cleanup;
 867  869  
 868  870          /* get user passphrase with 8 byte minimum */
 869      -        if (pkcs11_get_pass(NULL, &pass, &passlen, MIN_PASSLEN, B_TRUE) < 0) {
      871 +        if (pkcs11_get_pass(NULL, &pass, &passlen, MIN_PASSLEN,
      872 +            with_confirmation) < 0) {
 870  873                  die(gettext("passphrases do not match\n"));
 871  874          }
 872  875  
 873  876          /*
 874  877           * salt should not be NULL, or else pkcs11_PasswdToKey() will
 875  878           * complain about CKR_MECHANISM_PARAM_INVALID; the following is
 876  879           * to make up for not having a salt until a proper one is used
 877  880           */
 878  881          salt = pass;
 879  882          saltlen = passlen;
↓ open down ↓ 933 lines elided ↑ open up ↑
1813 1816          boolean_t errflag = B_FALSE;
1814 1817          boolean_t addflag = B_FALSE;
1815 1818          boolean_t rdflag = B_FALSE;
1816 1819          boolean_t deleteflag = B_FALSE;
1817 1820          boolean_t ephflag = B_FALSE;
1818 1821          boolean_t compressflag = B_FALSE;
1819 1822          boolean_t uncompressflag = B_FALSE;
1820 1823          /* the next two work together for -c, -k, -T, -e options only */
1821 1824          boolean_t need_crypto = B_FALSE;        /* if any -c, -k, -T, -e */
1822 1825          boolean_t cipher_only = B_TRUE;         /* if -c only */
     1826 +        boolean_t with_confirmation = B_TRUE;
1823 1827          const char *keyfile = NULL;
1824 1828          mech_alias_t *cipher = NULL;
1825 1829          token_spec_t *token = NULL;
1826 1830          char    *rkey = NULL;
1827 1831          size_t  rksz = 0;
1828 1832          char realfilename[MAXPATHLEN];
1829 1833  
1830 1834          pname = getpname(argv[0]);
1831 1835  
1832 1836          (void) setlocale(LC_ALL, "");
1833 1837          (void) textdomain(TEXT_DOMAIN);
1834 1838  
1835      -        while ((c = getopt(argc, argv, "a:c:Cd:efk:o:rs:T:U")) != EOF) {
     1839 +        while ((c = getopt(argc, argv, "a:c:Cd:efk:ors:T:U")) != EOF) {
1836 1840                  switch (c) {
1837 1841                  case 'a':
1838 1842                          addflag = B_TRUE;
1839 1843                          if ((filename = realpath(optarg, realfilename)) == NULL)
1840 1844                                  die("%s", optarg);
1841 1845                          if (((argc - optind) > 0) && (*argv[optind] != '-')) {
1842 1846                                  /* optional device */
1843 1847                                  devicename = argv[optind];
1844 1848                                  optind++;
1845 1849                          }
↓ open down ↓ 57 lines elided ↑ open up ↑
1903 1907                                  warn(
1904 1908                                      gettext("invalid token key specifier %s\n"),
1905 1909                                      optarg);
1906 1910                          }
1907 1911                          need_crypto = B_TRUE;
1908 1912                          cipher_only = B_FALSE;  /* need to unset cipher_only */
1909 1913                          break;
1910 1914                  case 'U':
1911 1915                          uncompressflag = B_TRUE;
1912 1916                          break;
     1917 +                case 'o':
     1918 +                        with_confirmation = B_FALSE;
     1919 +                        break;
1913 1920                  case '?':
1914 1921                  default:
1915 1922                          errflag = B_TRUE;
1916 1923                          break;
1917 1924                  }
1918 1925          }
1919 1926  
1920 1927          /* Check for mutually exclusive combinations of options */
1921 1928          if (errflag ||
1922 1929              (addflag && deleteflag) ||
1923 1930              (rdflag && !addflag) ||
1924 1931              (!addflag && need_crypto) ||
     1932 +            (!with_confirmation && (!cipher_only || !need_crypto)) ||
1925 1933              ((compressflag || uncompressflag) && (addflag || deleteflag)))
1926 1934                  usage(pname);
1927 1935  
1928 1936          /* ephemeral key, and key from either file or token are incompatible */
1929 1937          if (ephflag && (keyfile != NULL || token != NULL)) {
1930 1938                  die(gettext("ephemeral key cannot be used with keyfile"
1931 1939                      " or token key\n"));
1932 1940          }
1933 1941  
1934 1942          /*
↓ open down ↓ 79 lines elided ↑ open up ↑
2014 2022                          cipher = DEFAULT_CIPHER;
2015 2023  
2016 2024                  if (!kernel_cipher_check(cipher))
2017 2025                          die(gettext(
2018 2026                              "use \"cryptoadm list -m\" to find available "
2019 2027                              "mechanisms\n"));
2020 2028  
2021 2029                  init_crypto(token, cipher, &sess);
2022 2030  
2023 2031                  if (cipher_only) {
2024      -                        getkeyfromuser(cipher, &rkey, &rksz);
     2032 +                        getkeyfromuser(cipher, &rkey, &rksz, with_confirmation);
2025 2033                  } else if (token != NULL) {
2026 2034                          getkeyfromtoken(sess, token, keyfile, cipher,
2027 2035                              &rkey, &rksz);
2028 2036                  } else {
2029 2037                          /* this also handles ephemeral keys */
2030 2038                          getkeyfromfile(keyfile, cipher, &rkey, &rksz);
2031 2039                  }
2032 2040  
2033 2041                  end_crypto(sess);
2034 2042          }
↓ open down ↓ 23 lines elided ↑ open up ↑
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX