9 Commits 1529cb8286 ... dd617bc627

Auteur SHA1 Message Date
  Vinicius Teshima dd617bc627 Testing that file is LUKS encrypted il y a 10 mois
  Vinicius Teshima 5ec810b7ba Using TRY_AND_RETRY on closing route il y a 10 mois
  Vinicius Teshima de17ca2bdc Fixing error on mount function when referencing ${1} var il y a 10 mois
  Vinicius Teshima a393f35cc8 Refactor directory ownership check into a separate function il y a 10 mois
  Vinicius Teshima 81e1cb748a Add _LUKS_OPEN function to encapsulate luksOpen logic il y a 10 mois
  Vinicius Teshima 8c479126e4 Refactor mounting logic into a separate function il y a 10 mois
  Vinicius Teshima 2e31037dd7 Creating function to retry commands il y a 10 mois
  Vinicius Teshima 6955f44b97 Adding missing steps when creating luks file il y a 10 mois
  Vinicius Teshima b34681ea9a Adding \n on error printing il y a 10 mois
1 fichiers modifiés avec 87 ajouts et 48 suppressions
  1. 87 48
      luks.sh

+ 87 - 48
luks.sh

@@ -1,10 +1,49 @@
 #!/bin/sh
 
 ERR () {
-	printf '[ERROR] %s' "$1"
+	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:'
+
+_MOUNT () {
+	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")"
@@ -15,33 +54,8 @@ 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
 	test -d "./${name}" || ERR "File ${name} already exist in current directory. And is not a directory." 1
@@ -54,11 +68,11 @@ then
 			N|n) exit 0 ;;
 		esac
 		echo "Unmounting ./${name}"
-		$sudo_prog umount ./"$name" || ERR "Failed to unmount ./${name}." 1
+		TRY_AND_RETRY "${sudo_prog} umount ./${name}" "Failed to unmount ./${name}."
+		echo "Succesfully Unmounted ./${name}"
 
 		echo "Closing luks block dev ${block_dev}"
-		$sudo_prog cryptsetup close "$block_dev" || ERR "Failed to close luks file: ${file_path}." 1
-
+		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) "
@@ -67,35 +81,60 @@ then
 			N|n) exit 0 ;;
 		esac
 
-		rmdir ./$name || ERR "Failed to remove directory: ./${name}" 1
+		TRY_AND_RETRY rmdir "./${name}" "Failed to remove directory: ./${name}"
 		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
+if test -e "$file_path"
+then
+	test -f "$file_path" || ERR "${file_path} Is not a regular file" 1
 
-echo "Mounting ${block_dev} into ./${name}"
-$sudo_prog mount "$block_dev" ./"$name" || ERR "Failed to mound ${block_dev} on ./${name}." 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
 
-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
+	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