cryfs-automount 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. #!/bin/bash
  2. #/##################################################################\
  3. #| Basic scrypt to encrypt with CryFS file hosting services like |
  4. #| Dropbox, MEGA, Drive, etc. |
  5. #| Guzmán Castanedo (guzman@castanedo.es) |
  6. #| November 2016 |
  7. #| Licence: GPL 3.0 -> https://www.gnu.org/licenses/gpl-3.0.en.html |
  8. #| NOTE: It's necesary to configure Dropbox to basedir folder |
  9. #| before run this script, because it need to be empty or |
  10. #| Dropbox client will move to a new folder. |
  11. #| NEXT: I want to implement auto config the Dropbox client, but |
  12. #| it use a encrypted SQLite database. So it's harder. |
  13. #\##################################################################/
  14. #Functions
  15. function usage {
  16. printf "USAGE:\t"$(basename "$0")" [OPTIONS] name\n"
  17. printf "NAME:\n"
  18. printf "\tSet the automount name (without spaces).\n"
  19. printf "\tIt's usefull if you wanty to encrypt more than one file hosting service.\n"
  20. printf "\tThis parameter is OBLIGATORY.\n"
  21. printf "OPTIONS:\n"
  22. printf "\t-b, --basedir basedir\n"
  23. printf "\t\tSet the encrypted folder that Dropbox client are going to\n"
  24. printf "\t\tupload.\n"
  25. printf "\t\tDefault: /home/$USER/.MegaEncrypted/ENCRYPTED\n"
  26. printf "\t-m, --mountdir mountdir\n"
  27. printf "\t\tSet the decrypted folder.\n"
  28. printf "\t\tDefault: /home/$USER/MEGA\n"
  29. printf "\t-v, --verbose\n"
  30. printf "\t\tSet verbose mode.\n"
  31. printf "\t\tDefault: false\n"
  32. printf "\t-p, --password password\n"
  33. printf "\t\tSet password for CryFS encrypted volume.\n"
  34. printf "\t\tIf not set it'll ask iteractively.\n"
  35. printf "\t-c, --config cryfsconfig\n"
  36. printf "\t\tSet the configuration file for CryFS.\n"
  37. printf "\t\tDefault: basedir/cryfs.config\n"
  38. printf "\t--not-start-at-login\n"
  39. printf "\t\tNot start cryfs at login time.\n"
  40. printf "\t\tDefault: true\n"
  41. printf "\t\tCAUTION: plain password will be saved in:\n"
  42. printf "\t\t~/.config/autostart/cryfs-dropbox.desktop\n"
  43. printf "\t--new-volume\n"
  44. printf "\t\tCreate a new volume.\n"
  45. printf "\t\tDefault: false\n"
  46. printf "\t--blocksize\n"
  47. printf "\t\tChange the ciphertext block size in bytes\n"
  48. printf "\t\tDefault: 524288 (512 KB)\n"
  49. printf "\t\tCryFS use by default 32KB, but a 17 GB volume can generate\n"
  50. printf "\t\tup to 400,000 files with this...\n"
  51. printf "\t\tThe Dropbox client get crazzy!\n"
  52. }
  53. function createautostart {
  54. #createautostart $basedir $mountdir $cryfsconfig $pass $name
  55. if [ ! $# -eq 5 ];then
  56. echo "ERROR: Internal error generating autostart file."
  57. exit 1
  58. fi
  59. startfile=/home/$USER/.config/autostart/cryfs-$5.desktop
  60. if [ ! -d /home/$USER/.config/autostart ]; then
  61. #Create directory (parents if needed)
  62. mkdir -p /home/$USER/.config/autostart
  63. fi
  64. #Need to check existance because we're going to overwrite
  65. if [ -f $startfile ];then
  66. grep "Exec=$(basename "$0") -b $1 -m $2 -c $3 -p $4 $5" $startfile > /dev/null
  67. if [ $? -ne 0 ];then
  68. printpaths $name $basedir $mountdir $cryfsconfig
  69. printf "\nERROR:\tThere is another $(basename "$0") using that name (%s).\n" $5
  70. printf "\tUse a diferent name.\n"
  71. exit 1
  72. fi
  73. fi
  74. echo "[Desktop Entry]" | tee $startfile > /dev/null
  75. echo "Type=Application" | tee -a $startfile > /dev/null
  76. echo "Exec=$(basename "$0") -b $1 -m $2 -c $3 -p $4 $5" | tee -a $startfile > /dev/null
  77. echo "X-GNOME-Autostart-enabled=true" | tee -a $startfile > /dev/null
  78. echo "NoDisplay=false" | tee -a $startfile > /dev/null
  79. echo "Hidden=false" | tee -a $startfile > /dev/null
  80. echo "Name=CryFS-Dropbox" | tee -a $startfile > /dev/null
  81. echo "Comment=Automount CryFS for Dropbox" | tee -a $startfile > /dev/null
  82. echo "X-GNOME-Autostart-Delay=0" | tee -a $startfile > /dev/null
  83. }
  84. function printpaths {
  85. #printpaths $name $basedir $mountdir $cryfsconfig
  86. printf "Name:\t\t%s\n" $1
  87. printf "Base Dir:\t%s\n" $2
  88. printf "Mount Dir:\t%s\n" $3
  89. printf "CryFS Dir:\t%s\n" $4
  90. echo
  91. }
  92. #Bash main
  93. export CRYFS_FRONTEND=noninteractive
  94. basedir=/home/$USER/.MegaEncrypted/ENCRYPTED
  95. mountdir=/home/$USER/MEGA
  96. blocksize=524288
  97. verbose=false
  98. startlogin=true
  99. configset=false
  100. newvolume=false
  101. #Parse args
  102. #With getopts
  103. TEMP="$(getopt -q -o b:m:p:vhc: --long basedir:,mountdir:,password:,verbose,help,config:,not-start-at-login,new-volume,blocksize: -n "$(basename "$0")" -- "$@")"
  104. eval set -- "$TEMP"
  105. unset TEMP
  106. while true; do
  107. case "$1" in
  108. -b|--basedir)
  109. #basedir
  110. basedir=$2
  111. shift 2
  112. ;;
  113. -m|--mountdir)
  114. #mountdir
  115. mountdir=$2
  116. shift 2
  117. ;;
  118. -p|--password)
  119. #password
  120. pass=$2
  121. shift 2
  122. ;;
  123. -v|--verbose)
  124. #verbose
  125. verbose=true
  126. shift
  127. ;;
  128. -h|--help)
  129. #Help
  130. usage
  131. exit 0
  132. ;;
  133. -c|--config)
  134. #CryFS Config File
  135. cryfsconfig=$2
  136. configset=true
  137. shift 2
  138. ;;
  139. --not-start-at-login)
  140. startlogin=false
  141. shift
  142. ;;
  143. --new-volume)
  144. newvolume=true
  145. shift
  146. ;;
  147. --blocksize)
  148. blocksize=$2
  149. shift 2
  150. ;;
  151. --)
  152. #Last one
  153. shift
  154. break
  155. ;;
  156. *)
  157. #Unspected
  158. usage
  159. echo "ERROR: Invalid option $1"
  160. exit 1
  161. ;;
  162. esac
  163. done
  164. if [ ! $# -eq 1 ]; then
  165. echo "ERROR: You must set only one name for the installation"
  166. exit 1
  167. fi
  168. name=$1
  169. #Check if cryfs is installed
  170. if [ ! -x /usr/local/bin/cryfs ]; then
  171. echo "ERROR: CryFS is not installed."
  172. echo "https://www.cryfs.org/#download"
  173. exit 1
  174. fi
  175. #Check if cryfs-automount is installed
  176. if [ ! -x /usr/local/bin/$(basename "$0") ]; then
  177. echo "WARNING: "$(basename "$0")" is not installed."
  178. echo "We'll use root access only for installation."
  179. test=$(sudo whoami)
  180. if [ $test = "root" ]; then
  181. sudo cp ./$0 /usr/local/bin/
  182. sudo chmod +x /usr/local/bin/$(basename "$0")
  183. else
  184. echo "ERROR: No root access!"
  185. exit 1
  186. fi
  187. echo "Installation Success!"
  188. echo
  189. fi
  190. #Check if directories exist
  191. if [ ! -d $basedir ]; then
  192. printpaths $name $basedir $mountdir $cryfsconfig
  193. echo "ERROR: $basedir is not a directory."
  194. exit 1
  195. fi
  196. if [ ! -d $mountdir ]; then
  197. printpaths $name $basedir $mountdir $cryfsconfig
  198. echo "ERROR: $mountdir is not a directory."
  199. exit 1
  200. fi
  201. #Check encrypt cryfs volume exists (config file)
  202. if [ $configset = false ]; then
  203. #We use to set it in basedir (CryFS Default)
  204. cryfsconfig=$basedir/cryfs.config
  205. fi
  206. if [ ! -f $cryfsconfig ]; then
  207. #Config file doesn't exist
  208. if [ $newvolume = false ]; then
  209. printpaths $name $basedir $mountdir $cryfsconfig
  210. echo "ERROR: $cryfsconfig doesn't exist."
  211. echo "If this is a new volume enter the tag: --new-volume"
  212. exit 1
  213. fi
  214. else
  215. if [ $newvolume = true ]; then
  216. printpaths $name $basedir $mountdir $cryfsconfig
  217. echo "ERROR: Volume exists."
  218. echo "If you want to load it, remove the tag: --new-volume"
  219. exit 1
  220. fi
  221. fi
  222. #Convert to absolute path
  223. basedir=$(realpath $basedir)
  224. mountdir=$(realpath $mountdir)
  225. cryfsconfig=$(realpath $cryfsconfig)
  226. #Verbose
  227. if [ $verbose = true ];then
  228. printpaths $name $basedir $mountdir $cryfsconfig
  229. fi
  230. #Check if password is set
  231. if [ -z $pass ]; then
  232. if [ $newvolume = true ]; then
  233. #Twice
  234. cont=false
  235. while [ $cont = false ]; do
  236. printf "Password:\t"
  237. read -s pass1
  238. printf "\nRepeat Password:\t"
  239. read -s pass2
  240. echo
  241. if [ $pass1 = $pass2 ]; then
  242. pass=$pass1
  243. cont=true
  244. else
  245. printf "Wrong password. Try again:\n"
  246. fi
  247. done
  248. unset cont pass1 pass2
  249. else
  250. #Single
  251. printf "Password:\t"
  252. read -s pass
  253. echo
  254. fi
  255. fi
  256. #Before start let's check if autostart on login
  257. if [ $startlogin = true ]; then
  258. #Let's create it
  259. if [ $verbose = true ];then
  260. printf "Creating start at login.........\t"
  261. fi
  262. createautostart $basedir $mountdir $cryfsconfig $pass $name
  263. if [ $verbose = true ];then
  264. printf "Done!\n"
  265. printf "File: %s\n" "/home/$USER/.config/autostart/cryfs-$name.desktop"
  266. fi
  267. fi
  268. #Mount with CryFS
  269. if [ $verbose = true ]; then
  270. printf "Starting CryFS..................\t"
  271. fi
  272. echo $pass | cryfs --blocksize $blocksize -c $cryfsconfig $basedir $mountdir 2>&1 > /dev/null
  273. if [ $verbose = true ]; then
  274. printf "Done!\n"
  275. fi