luks.sh 3.9 KB

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