luks.sh 3.8 KB

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