cryfs-automount 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. else
  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-$5" | tee -a $startfile > /dev/null
  81. echo "Comment=Automount CryFS for $5" | tee -a $startfile > /dev/null
  82. echo "X-GNOME-Autostart-Delay=0" | tee -a $startfile > /dev/null
  83. fi
  84. }
  85. function printpaths {
  86. #printpaths $name $basedir $mountdir $cryfsconfig
  87. printf "Name:\t\t%s\n" $1
  88. printf "Base Dir:\t%s\n" $2
  89. printf "Mount Dir:\t%s\n" $3
  90. printf "CryFS Dir:\t%s\n" $4
  91. echo
  92. }
  93. #Bash main
  94. export CRYFS_FRONTEND=noninteractive
  95. basedir=/home/$USER/.MegaEncrypted/ENCRYPTED
  96. mountdir=/home/$USER/MEGA
  97. blocksize=524288
  98. verbose=false
  99. startlogin=true
  100. configset=false
  101. newvolume=false
  102. #Parse args
  103. #With getopts
  104. 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")" -- "$@")"
  105. eval set -- "$TEMP"
  106. unset TEMP
  107. while true; do
  108. case "$1" in
  109. -b|--basedir)
  110. #basedir
  111. basedir=$2
  112. shift 2
  113. ;;
  114. -m|--mountdir)
  115. #mountdir
  116. mountdir=$2
  117. shift 2
  118. ;;
  119. -p|--password)
  120. #password
  121. pass=$2
  122. shift 2
  123. ;;
  124. -v|--verbose)
  125. #verbose
  126. verbose=true
  127. shift
  128. ;;
  129. -h|--help)
  130. #Help
  131. usage
  132. exit 0
  133. ;;
  134. -c|--config)
  135. #CryFS Config File
  136. cryfsconfig=$2
  137. configset=true
  138. shift 2
  139. ;;
  140. --not-start-at-login)
  141. startlogin=false
  142. shift
  143. ;;
  144. --new-volume)
  145. newvolume=true
  146. shift
  147. ;;
  148. --blocksize)
  149. blocksize=$2
  150. shift 2
  151. ;;
  152. --)
  153. #Last one
  154. shift
  155. break
  156. ;;
  157. *)
  158. #Unspected
  159. usage
  160. echo "ERROR: Invalid option $1"
  161. exit 1
  162. ;;
  163. esac
  164. done
  165. if [ ! $# -eq 1 ]; then
  166. echo "ERROR: You must set only one name for the installation"
  167. exit 1
  168. fi
  169. name=$1
  170. #Check if cryfs is installed
  171. if [ ! -x "$(which cryfs)" ]; then
  172. echo "ERROR: CryFS is not installed."
  173. echo "https://www.cryfs.org/#download"
  174. exit 1
  175. fi
  176. #Check if cryfs-automount is installed
  177. if [ ! -x "$(which $(basename "$0"))" ]; then
  178. echo "WARNING: "$(basename "$0")" is not installed."
  179. echo "We'll use root access only for installation."
  180. test=$(sudo whoami)
  181. if [ $test = "root" ]; then
  182. sudo cp ./$0 /usr/local/bin/
  183. sudo chmod +x /usr/local/bin/$(basename "$0")
  184. else
  185. echo "ERROR: No root access!"
  186. exit 1
  187. fi
  188. echo "Installation Success!"
  189. echo
  190. fi
  191. #Check if directories exist
  192. if [ ! -d $basedir ]; then
  193. printpaths $name $basedir $mountdir $cryfsconfig
  194. echo "ERROR: $basedir is not a directory."
  195. exit 1
  196. fi
  197. if [ ! -d $mountdir ]; then
  198. printpaths $name $basedir $mountdir $cryfsconfig
  199. echo "ERROR: $mountdir is not a directory."
  200. exit 1
  201. fi
  202. #Check encrypt cryfs volume exists (config file)
  203. if [ $configset = false ]; then
  204. #We use to set it in basedir (CryFS Default)
  205. cryfsconfig=$basedir/cryfs.config
  206. fi
  207. if [ ! -f $cryfsconfig ]; then
  208. #Config file doesn't exist
  209. if [ $newvolume = false ]; then
  210. printpaths $name $basedir $mountdir $cryfsconfig
  211. echo "ERROR: $cryfsconfig doesn't exist."
  212. echo "If this is a new volume enter the tag: --new-volume"
  213. exit 1
  214. fi
  215. else
  216. if [ $newvolume = true ]; then
  217. printpaths $name $basedir $mountdir $cryfsconfig
  218. echo "ERROR: Volume exists."
  219. echo "If you want to load it, remove the tag: --new-volume"
  220. exit 1
  221. fi
  222. fi
  223. #Convert to absolute path
  224. basedir=$(realpath $basedir)
  225. mountdir=$(realpath $mountdir)
  226. cryfsconfig=$(realpath $cryfsconfig)
  227. #Verbose
  228. if [ $verbose = true ];then
  229. printpaths $name $basedir $mountdir $cryfsconfig
  230. fi
  231. #Check if password is set
  232. if [ -z $pass ]; then
  233. if [ $newvolume = true ]; then
  234. #Twice
  235. cont=false
  236. while [ $cont = false ]; do
  237. printf "Password:\t"
  238. read -s pass1
  239. printf "\nRepeat Password:\t"
  240. read -s pass2
  241. echo
  242. if [ $pass1 = $pass2 ]; then
  243. pass=$pass1
  244. cont=true
  245. else
  246. printf "Wrong password. Try again:\n"
  247. fi
  248. done
  249. unset cont pass1 pass2
  250. else
  251. #Single
  252. printf "Password:\t"
  253. read -s pass
  254. echo
  255. fi
  256. fi
  257. #Before start let's check if autostart on login
  258. if [ $startlogin = true ]; then
  259. #Let's create it
  260. if [ $verbose = true ];then
  261. printf "Creating start at login.........\t"
  262. fi
  263. createautostart $basedir $mountdir $cryfsconfig $pass $name
  264. if [ $verbose = true ];then
  265. printf "Done!\n"
  266. printf "File: %s\n" "/home/$USER/.config/autostart/cryfs-$name.desktop"
  267. fi
  268. fi
  269. #Mount with CryFS
  270. if [ $verbose = true ]; then
  271. printf "Starting CryFS..................\t"
  272. fi
  273. echo $pass | cryfs --blocksize $blocksize -c $cryfsconfig $basedir $mountdir 2>&1 > /dev/null
  274. if [ $verbose = true ]; then
  275. printf "Done!\n"
  276. fi