home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / lib / cryptsetup / scripts / decrypt_keyctl < prev    next >
Encoding:
Text File  |  2011-03-11  |  2.6 KB  |  79 lines

  1. #!/bin/sh
  2. # decrypt_keyctl - to use in /etc/crypttab as keyscript
  3. #  Allows to cache passwords for cryptdevices for 60s
  4. #  The same password is used for for cryptdevices with the same identifier.
  5. #  The keyfile parameter, which is the third field from /etc/crypttab, is
  6. #  used as identifier in this keyscript.
  7. #
  8. # sample crypttab entries:
  9. # test1   /dev/loop1    test_pw         luks,keyscript=decrypt_keyctl
  10. # test2   /dev/loop2    test_pw         luks,keyscript=decrypt_keyctl
  11. # test3   /dev/loop3    test_other_pw   luks,keyscript=decrypt_keyctl
  12. #
  13. #  test1 and test2 have the same identifier thus test2 does not need a password
  14. #  typed in manually
  15.  
  16. die()
  17. {
  18.     echo "$@" >&2
  19.     exit 1
  20. }
  21.  
  22. # the keyfile given from crypttab is used as identifier in the keyring
  23. # including the prefix "cryptkey-"
  24. ID_="cryptkey-$1"
  25. TIMEOUT_='60'
  26. ASKPASS_='/lib/cryptsetup/askpass'
  27. STTY_='/bin/stty'
  28. PW_READER_='undefined'
  29. PROMPT_="Caching passphrase for ${CRYPTTAB_SOURCE}: "
  30.  
  31. test -x "$STTY_" && PW_READER_='stty'           # 1. backup method
  32. test -x "$ASKPASS_" && PW_READER_='askpass'     # prefered method
  33.  
  34. KID_=$(keyctl search @u user "$ID_" 2>/dev/null)
  35. if [ $? -ne 0 ] || [ -z "$KID_" ]; then
  36.     # key not found, ask the user
  37.     case "$PW_READER_" in
  38.         askpass)
  39.             KEY_=$($ASKPASS_ "$PROMPT_") || die "Error executing $ASKPASS_"
  40.             ;;
  41.         stty) # disable echoing with stty
  42.             $STTY_ -echo
  43.             if ! read -r KEY_; then
  44.                 $STTY_ echo
  45.                 die "Error reading key from /dev/stdin"
  46.             else
  47.                 $STTY_ echo
  48.                 echo >&2
  49.             fi
  50.             ;;
  51.         *)  # first try to read the posix way, then at least give the user a chance
  52.             echo -n "$PROMPT_" >&2
  53.             if ! read -res KEY_; then
  54.                 echo
  55.                 echo "ERROR: Can not disable echoing, YOUR PASSWORD WILL BE VISIBLE!" >&2
  56.                 echo "This can be fixed if you add either $ASKPASS_" >&2
  57.                 echo "or $STTY_ to your initramfs" >&2
  58.                 echo -n "$PROMPT_" >&2
  59.                 if ! read -r KEY_; then
  60.                     die "Error reading key from /dev/stdin"
  61.                 else
  62.                     echo >&2
  63.                 fi
  64.             else
  65.                 echo >&2
  66.             fi
  67.             ;;
  68.     esac
  69.     KID_=$(echo -n "$KEY_" |keyctl padd user "$ID_" @u)
  70.     [ -z "$KID_" ] && die "Error adding passphrase to kernel keyring"
  71.     if ! keyctl timeout $KID_ $TIMEOUT_; then
  72.         keyctl unlink $KID_ @u
  73.         die "Error setting timeout on key ($KID_), removing"
  74.     fi
  75. else
  76.     echo "Using cached passphrase for ${CRYPTTAB_SOURCE}." >&2
  77. fi
  78. keyctl pipe $KID_
  79.