cryfs-dropbox 7.0 KB

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