| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- #!/bin/sh
- ERR () {
- printf '[ERROR] %s\n' "$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 ./"$name"
- then
- 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
- 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
- echo "Succesfully Allocated file: ${file_path}"
- echo "Encrypting file: ${file_path}"
- cryptsetup luksFormat -c aes-xts-plain64 -s 512 -y "$file_path" || ERR "Failed to format file: ${file_path}" 1
- echo "Succesfully Encrypted file: ${file_path}"
- echo "Opening Encrypted file: ${file_path} as ${_name}"
- $sudo_prog cryptsetup luksOpen "$file_path" "$_name" || ERR "Failed to open luks file: ${file_path}." 1
- echo "Succesfully Opened Encrypted file: ${file_path} as ${_name}"
- echo "Formating ${_name} as ext4"
- $sudo_prog mkfs.ext4 -m0 "$block_dev" || ERR "Failed to format block dev: ${block_dev}" 1
- echo "Succesfully Formated ${_name} as ext4"
- echo "Mounting ${block_dev} into ./${name}"
- $sudo_prog mount "$block_dev" ./"$name" || ERR "Failed to mound ${block_dev} on ./${name}." 1
- echo "Succesfully Mounted ${block_dev} into ./${name}"
- 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 "Succesfully Created and Opened file: ${file_path} into ./${name}"
- exit 0
- 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
|