#!/bin/sh ERR () { printf '[ERROR] %s\n' "$1" test -n "$2" && exit "$2" } TRY_AND_RETRY () { while ! $1 do ERR "$2" echo -n 'Want to try again? (Y/n) ' read choose case "$choose" in N|n) exit 1 ;; esac done } sudo_prog='sudo --prompt=Sudo_Password:' cur_uid="$(id -u)" cur_gid="$(id -g)" _MOUNT () { test -e "${2}" || TRY_AND_RETRY "mkdir ${2}" "Failed to create directory: ${2}" echo "Mounting ${1} into ${2}" TRY_AND_RETRY "${sudo_prog} mount ${1} ${2}" "Failed to mound ${1} on ${2}." echo "Succesfully Mounted ${1} into ${2}" } _LUKS_OPEN () { echo "Opening Encrypted file: ${1} as ${2}" TRY_AND_RETRY "${sudo_prog} cryptsetup luksOpen ${1} ${2}" "Failed to open luks file: ${1}." echo "Succesfully Opened Encrypted file: ${1} as ${2}" } _CHECK_DIR_PERM () { dir_perm="$(stat -c '%u:%g' "${1}")" cur_perm="${cur_uid}:${cur_gid}" test "$dir_perm" = "$cur_perm" && return 0 echo -n "Directory ${1} is not owned by current user. Want to change direcory owner? (Y/n) " read choose case "$choose" in N|n) return 0 ;; esac echo "Changing ownership of ${1} from ${dir_perm} to ${cur_perm}." TRY_AND_RETRY "${sudo_prog} chown ${cur_perm} ${1}" || ERR "Failed to change permission of directory ${1} from ${dir_perm} to ${cur_perm}." 1 } 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}" 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}" TRY_AND_RETRY "${sudo_prog} umount ./${name}" "Failed to unmount ./${name}." echo "Succesfully Unmounted ./${name}" echo "Closing luks block dev ${block_dev}" TRY_AND_RETRY "${sudo_prog} cryptsetup close ${block_dev}" "Failed to close luks file: ${file_path}." echo "Succesfully Closed ${file_path}." echo -n "Delete directory ./${name}? (Y/n) " read choose case "$choose" in N|n) exit 0 ;; esac TRY_AND_RETRY "rmdir ./${name}" "Failed to remove directory: ./${name}" exit 0 fi fi if test -e "$file_path" then test -f "$file_path" || ERR "${file_path} Is not a regular file" 1 file_type="$(file -b ${file_path} | cut -d' ' -f-2)" test "$file_type" = 'LUKS encrypted' || ERR "${file_path} is not a LUKS encrypted 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}" TRY_AND_RETRY "cryptsetup luksFormat -c aes-xts-plain64 -s 512 -y ${file_path}" "Failed to format file: ${file_path}" echo "Succesfully Encrypted file: ${file_path}" _LUKS_OPEN "$file_path" "$_name" echo "Formating ${_name} as ext4" TRY_AND_RETRY "${sudo_prog} mkfs.ext4 -m0 ${block_dev}" "Failed to format block dev: ${block_dev}" echo "Succesfully Formated ${_name} as ext4" _MOUNT "$block_dev" "./${name}" _CHECK_DIR_PERM "./${name}" echo "Succesfully Created and Opened file: ${file_path} into ./${name}" exit 0 fi _LUKS_OPEN "$file_path" "$_name" _MOUNT "$block_dev" "./${name}" _CHECK_DIR_PERM "./${name}" echo "Opened ${file_path} on ./${name}" exit 0