SHARE
TWEET

Untitled

a guest Aug 11th, 2012 23 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/sh
  2.  
  3. # Utility script to map plaintext and encrypted filenames
  4. # in an eCryptfs directory
  5. # Sergio Mena. 2011-06-18
  6.  
  7. #Default values
  8. encryptedroot="`dirname ${HOME}`/.ecryptfs/`basename ${HOME}`/.Private"
  9. plaintextroot="$HOME"
  10.  
  11. usage()
  12. {
  13. cat << EOF
  14. usage: $0 [options] filename
  15.  
  16. This script prints the ecryptfs counterpart filename (including path) of the plaintext filename \
  17. passed as argument. Note that the script does not use PWD/CWD to locate the filename. Filename \
  18. is a path to the target file/directory, relative to the plaintext root. Likewise, the resulting \
  19. filename includes the path relative to the encrypted root.
  20.  
  21. OPTIONS:
  22.    -h show this message
  23.    -e path path to encrypted root path (default: $encryptedroot)
  24.    -p path path to plaintext root path (default: $plaintextroot)
  25.    -s swap root paths. The command effectively takes the opposite effect (i.e., from \
  26. encrypted filename to plaintext).
  27. EOF
  28. }
  29.  
  30. reverse=0
  31. while getopts "he:p:s" OPTION; do
  32.     case $OPTION in
  33.         h)
  34.             usage
  35.             exit 0
  36.             ;;
  37.         e)
  38.             encryptedroot="$OPTARG"
  39.             ;;
  40.         p)
  41.             plaintextroot="$OPTARG"
  42.             ;;
  43.         s)
  44.             reverse=1
  45.             ;;
  46.         ?)
  47.             usage >&2
  48.             exit 1
  49.             ;;
  50.     esac
  51. done
  52.  
  53. shift $((OPTIND - 1))
  54.  
  55. [ -z "$1" ] &&\
  56.     echo "$0: No filename provided" >&2 &&\
  57.     usage >&2 &&\
  58.     exit 2
  59.  
  60. [ $reverse -eq 1 ] &&\
  61.     aux="${encryptedroot}" &&\
  62.     encryptedroot="${plaintextroot}" &&\
  63.     plaintextroot="${aux}"
  64.  
  65. currentencryptedpath=
  66. currentplaintextpath=
  67. rest="$1"
  68.  
  69. while true; do
  70.     nextplaintextdir=`echo ${rest} | sed 's/\/.*$//'`
  71.     rest=`echo ${rest} | sed 's/^[^\/]*\/*//'`
  72.     currentplaintextpath=${currentplaintextpath}/${nextplaintextdir}
  73.     [ ! -e "${plaintextroot}/${currentplaintextpath}" ] &&\
  74.         echo "$0: cannot access $1: No such file or directory" >&2 &&\
  75.         exit 1
  76.     inode=`ls -aid "${plaintextroot}/${currentplaintextpath}" | awk '{print $1}' `
  77.     nextencrypteddir=`ls -ai "${encryptedroot}/${currentencryptedpath}" | \
  78.                       grep ${inode} | awk '{print $2}'`
  79.     [ -z "$nextencrypteddir" ] &&\
  80.         echo "$0: Hmmm strange, no encrypted file/dir corresponds to plaintext file/dir" >&2 &&\
  81.         exit 2
  82.     currentencryptedpath="${currentencryptedpath}/${nextencrypteddir}"
  83.     [ -z "$rest" ] &&\
  84.         ( echo "${currentencryptedpath}" | sed 's/^\///' ) &&\
  85.         exit 0
  86. done
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top