luks.sh 3.6 KB

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