luks.sh 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #!/bin/sh
  2. ERR () {
  3. printf '[ERROR] %s\n' "$1"
  4. test -n "$2" && exit "$2"
  5. }
  6. TRY_AND_RETRY () {
  7. while ! $1
  8. do
  9. ERR "$2"
  10. echo -n 'Want to try again? (Y/n) '
  11. read choose
  12. case "$choose" in
  13. N|n) exit 1 ;;
  14. esac
  15. done
  16. }
  17. sudo_prog='sudo --prompt=Sudo_Password:'
  18. _MOUNT () {
  19. echo "Mounting ${1} into ${2}"
  20. TRY_AND_RETRY "${sudo_prog} mount ${1} ${2}" "Failed to mound ${1} on ${2}."
  21. echo "Succesfully Mounted ${1} into ${2}"
  22. }
  23. _LUKS_OPEN () {
  24. echo "Opening Encrypted file: ${1} as ${2}"
  25. TRY_AND_RETRY "${sudo_prog} cryptsetup luksOpen ${1} ${2}" "Failed to open luks file: ${1}."
  26. echo "Succesfully Opened Encrypted file: ${1} as ${2}"
  27. }
  28. _CHECK_DIR_PERM () {
  29. dir_perm="$(stat -c '%u:%g' "${1}")"
  30. cur_perm="${cur_uid}:${cur_gid}"
  31. test "$dir_perm" = "$cur_perm" || return 0
  32. echo -n "Directory ${1} is not owned by current user. Want to change direcory owner? (Y/n) "
  33. read choose
  34. case "$choose" in
  35. N|n) return 0 ;;
  36. esac
  37. echo "Changing ownership of ${1} from ${dir_perm} to ${cur_perm}."
  38. TRY_AND_RETRY "${sudo_prog} chown ${cur_perm} ${1}" || ERR "Failed to change permission of directory ${1} from ${dir_perm} to ${cur_perm}." 1
  39. }
  40. file_path="$1"
  41. test -n "$file_path" || { echo 'No file given'; exit 1; }
  42. file="$(basename "$file_path")"
  43. name="$(echo "$file" | cut -d. -f1)"
  44. _name="__${name}__"
  45. ext="$(echo "$file" | cut -d. -f2)"
  46. block_dev="/dev/mapper/${_name}"
  47. cur_uid="$(id -u)"
  48. cur_gid="$(id -g)"
  49. test "$ext" = 'luks' || ERR "File $file_path does not have extencion .luks." 1
  50. if test -e ./"$name"
  51. then
  52. test -d "./${name}" || ERR "File ${name} already exist in current directory. And is not a directory." 1
  53. if test -b "$block_dev"
  54. then
  55. echo -n "File ${file_path} Already open. Want to close? (Y/n) "
  56. read choose
  57. case "$choose" in
  58. N|n) exit 0 ;;
  59. esac
  60. echo "Unmounting ./${name}"
  61. TRY_AND_RETRY "${sudo_prog} umount ./${name}" "Failed to unmount ./${name}."
  62. echo "Succesfully Unmounted ./${name}"
  63. echo "Closing luks block dev ${block_dev}"
  64. TRY_AND_RETRY "${sudo_prog} cryptsetup close ${block_dev}" "Failed to close luks file: ${file_path}."
  65. echo "Succesfully Closed ${file_path}."
  66. echo -n "Delete directory ./${name}? (Y/n) "
  67. read choose
  68. case "$choose" in
  69. N|n) exit 0 ;;
  70. esac
  71. TRY_AND_RETRY rmdir "./${name}" "Failed to remove directory: ./${name}"
  72. exit 0
  73. fi
  74. else
  75. mkdir ./"$name"
  76. fi
  77. if test -e "$file_path"
  78. then
  79. test -f "$file_path" || ERR "${file_path} Is not a regular file" 1
  80. else
  81. echo -n "File ${file_path} does not exist. Want to create? (Y/n) "
  82. read choose
  83. case "$choose" in
  84. N|n) exit 1 ;;
  85. esac
  86. echo -n "What size? (10G) "
  87. read _size
  88. if test "$choose" = ''
  89. then
  90. _size='10G'
  91. fi
  92. truncate --size="$_size" "$file_path" || ERR "Failed to allocate file: ${file_path}" 1
  93. echo "Succesfully Allocated file: ${file_path}"
  94. echo "Encrypting file: ${file_path}"
  95. TRY_AND_RETRY "cryptsetup luksFormat -c aes-xts-plain64 -s 512 -y ${file_path}" "Failed to format file: ${file_path}"
  96. echo "Succesfully Encrypted file: ${file_path}"
  97. _LUKS_OPEN "$file_path" "$_name"
  98. echo "Formating ${_name} as ext4"
  99. TRY_AND_RETRY "${sudo_prog} mkfs.ext4 -m0 ${block_dev}" "Failed to format block dev: ${block_dev}"
  100. echo "Succesfully Formated ${_name} as ext4"
  101. _MOUNT "$block_dev" "./${name}"
  102. _CHECK_DIR_PERM "./${name}"
  103. echo "Succesfully Created and Opened file: ${file_path} into ./${name}"
  104. exit 0
  105. fi
  106. _LUKS_OPEN "$file_path" "$_name"
  107. _MOUNT "$block_dev" "./${name}"
  108. _CHECK_DIR_PERM "./${name}"
  109. echo "Opened ${file_path} on ./${name}"
  110. exit 0