|
@@ -0,0 +1,100 @@
|
|
|
|
|
+#!/bin/sh
|
|
|
|
|
+
|
|
|
|
|
+ERR () {
|
|
|
|
|
+ printf '[ERROR] %s' "$1"
|
|
|
|
|
+ test -n "$2" && exit "$2"
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+file_path="$1"
|
|
|
|
|
+test -n "$file_path" || { echo 'No file given'; exit 1; }
|
|
|
|
|
+file="$(basename "$file_path")"
|
|
|
|
|
+name="$(echo "$file" | cut -d. -f1)"
|
|
|
|
|
+_name="__${name}__"
|
|
|
|
|
+ext="$(echo "$file" | cut -d. -f2)"
|
|
|
|
|
+block_dev="/dev/mapper/${_name}"
|
|
|
|
|
+cur_uid="$(id -u)"
|
|
|
|
|
+cur_gid="$(id -g)"
|
|
|
|
|
+
|
|
|
|
|
+sudo_prog='sudo --prompt=Sudo_Password:'
|
|
|
|
|
+
|
|
|
|
|
+test "$ext" = 'luks' || ERR "File $file_path does not have extencion .luks." 1
|
|
|
|
|
+
|
|
|
|
|
+if test -e "$file_path"
|
|
|
|
|
+then
|
|
|
|
|
+ test -f "$file_path" || ERR "${file_path} Is not a regular file" 1
|
|
|
|
|
+else
|
|
|
|
|
+ echo -n "File ${file_path} does not exist. Want to create? (Y/n) "
|
|
|
|
|
+ read choose
|
|
|
|
|
+ case "$choose" in
|
|
|
|
|
+ N|n) exit 1 ;;
|
|
|
|
|
+ esac
|
|
|
|
|
+
|
|
|
|
|
+ echo -n "What size? (10G) "
|
|
|
|
|
+ read _size
|
|
|
|
|
+ if test "$choose" = ''
|
|
|
|
|
+ then
|
|
|
|
|
+ _size='10G'
|
|
|
|
|
+ fi
|
|
|
|
|
+ truncate --size="$_size" "$file_path" || ERR "Failed to allocate file: ${file_path}" 1
|
|
|
|
|
+
|
|
|
|
|
+ cryptsetup luksFormat -c aes-xts-plain64 -s 512 -y "$file_path" || ERR "Failed to format file: ${file_path}" 1
|
|
|
|
|
+
|
|
|
|
|
+ echo "Successifuly create encrypted file: ${file_path}"
|
|
|
|
|
+fi
|
|
|
|
|
+
|
|
|
|
|
+if test -e ./"$name"
|
|
|
|
|
+then
|
|
|
|
|
+ if test -d "./${name}" || ERR "File ${name} already exist in current directory. And is not a directory." 1
|
|
|
|
|
+
|
|
|
|
|
+ if test -b "$block_dev"
|
|
|
|
|
+ then
|
|
|
|
|
+ echo -n "File ${file_path} Already open. Want to close? (Y/n) "
|
|
|
|
|
+ read choose
|
|
|
|
|
+ case "$choose" in
|
|
|
|
|
+ N|n) exit 0 ;;
|
|
|
|
|
+ esac
|
|
|
|
|
+ echo "Unmounting ./${name}"
|
|
|
|
|
+ $sudo_prog umount ./"$name" || ERR "Failed to unmount ./${name}." 1
|
|
|
|
|
+
|
|
|
|
|
+ echo "Closing luks block dev ${block_dev}"
|
|
|
|
|
+ $sudo_prog cryptsetup close "$block_dev" || ERR "Failed to close luks file: ${file_path}." 1
|
|
|
|
|
+
|
|
|
|
|
+ echo "Succesfully Closed ${file_path}."
|
|
|
|
|
+
|
|
|
|
|
+ echo -n "Delete directory ./${name}? (Y/n) "
|
|
|
|
|
+ read choose
|
|
|
|
|
+ case "$choose" in
|
|
|
|
|
+ N|n) exit 0 ;;
|
|
|
|
|
+ esac
|
|
|
|
|
+
|
|
|
|
|
+ rmdir ./$name || ERR "Failed to remove directory: ./${name}" 1
|
|
|
|
|
+ exit 0
|
|
|
|
|
+ fi
|
|
|
|
|
+else
|
|
|
|
|
+ mkdir ./"$name"
|
|
|
|
|
+fi
|
|
|
|
|
+
|
|
|
|
|
+echo "Opening luks file: ${file_path} as ${_name}."
|
|
|
|
|
+$sudo_prog cryptsetup luksOpen "$file_path" "$_name" || ERR "Failed to open luks file: ${file_path}." 1
|
|
|
|
|
+
|
|
|
|
|
+echo "Mounting ${block_dev} into ./${name}"
|
|
|
|
|
+$sudo_prog mount "$block_dev" ./"$name" || ERR "Failed to mound ${block_dev} on ./${name}." 1
|
|
|
|
|
+
|
|
|
|
|
+dir_perm="$(stat -c '%u:%g' "$file_path")"
|
|
|
|
|
+cur_perm="${cur_uid}:${cur_gid}"
|
|
|
|
|
+if ! test "$dir_perm" = "$cur_perm"
|
|
|
|
|
+then
|
|
|
|
|
+ (
|
|
|
|
|
+ echo -n "Directory ./${name} not owned by current user. Want to change direcory owner? (Y/n) "
|
|
|
|
|
+ read choose
|
|
|
|
|
+ case "$choose" in
|
|
|
|
|
+ N|n) exit 0 ;;
|
|
|
|
|
+ esac
|
|
|
|
|
+ echo "Changing ownership of ./${name} from ${dir_perm} to ${cur_perm}."
|
|
|
|
|
+ $sudo_prog chown "$cur_perm" ./"$name" || ERR "Failed to change permission of directory ./${name} from ${dir_perm} to ${cur_perm}." 1
|
|
|
|
|
+ ) || exit 1
|
|
|
|
|
+fi
|
|
|
|
|
+
|
|
|
|
|
+echo "Opened ${file_path} on ./${name}"
|
|
|
|
|
+
|
|
|
|
|
+exit 0
|