From ab5fd2d895f3df3cac4c4333d38aa371b37ad281 Mon Sep 17 00:00:00 2001 From: David Runge Date: Fri, 20 Oct 2017 22:06:14 +0200 Subject: bin/cs: Adding validate_cryptdevice_name. Moving settings for devices to configuration file holding an associative array for name and UUID of device. --- bin/cs | 77 +++++++++++++++++++++++++++++++----------------------------------- 1 file changed, 36 insertions(+), 41 deletions(-) diff --git a/bin/cs b/bin/cs index 8788afd..c4d6de9 100755 --- a/bin/cs +++ b/bin/cs @@ -2,21 +2,18 @@ set -euo pipefail -storage="/dev/disk/by-uuid/3f781aab-24b5-4de0-8632-b9df922d6433" -music="/dev/disk/by-uuid/e0b19c7d-9e90-4117-aea1-7e4b321d9d5a" -photos="/dev/disk/by-uuid/b3c5a842-474b-4c2f-a246-abebe1da3450" -backup="/dev/disk/by-uuid/e003337a-33e9-4691-bc57-e77243e55de5" -video="/dev/disk/by-uuid/5ac700be-e168-495b-b133-42064fa101e4" -media="/dev/disk/by-uuid/e5adbc14-732e-43e8-b6e1-febdfe6d1d3c" -mediabackup="/dev/disk/by-uuid/4467b7af-ab65-4fb7-9798-00d0ee9b4002" -videobackup="/dev/disk/by-uuid/80d50a4c-a04e-432e-8265-9a11a4386612" -audiobackup="/dev/disk/by-uuid/c1d8cea9-aa95-4063-a44d-63e0a702477b" +declare -A devices +valid_name=0 +command_name=${1:-} +device_name=${2:-} + +. ~/.config/cs.conf function open_cryptdevice() { - if [ -L "$1" ]; then - /usr/bin/sudo /usr/bin/cryptsetup luksOpen $1 $2 + if [ -L "/dev/disk/by-uuid/$1" ]; then + /usr/bin/sudo /usr/bin/cryptsetup luksOpen "/dev/disk/by-uuid/$1" $2 else - echo "Error: Device $2 ($1) is not available." + echo "Error: Device $2 (UUID=$1) is not available." return 1 fi } @@ -48,42 +45,40 @@ function unmount_cryptdevice() { /usr/bin/sudo /usr/bin/umount /mnt/$1 } +function validate_cryptdevice_name() { + local _result_valid_name=$1 + local name_found=0 + local input_name=${2:-} + for name in "${!devices[@]}"; do + if [ $name = "${input_name}" ]; then + name_found=1 + eval $_result_valid_name="'$name_found'" + fi + done +} + +validate_cryptdevice_name valid_name ${device_name} +if [ $valid_name -eq 0 ]; then + echo "No such device: '$device_name'!" + exit 1 +fi + case $1 in "open") - case $2 in - "storage") - open_cryptdevice $storage $2 - ;; - "music") - open_cryptdevice $music $2 - ;; - "photos") - open_cryptdevice $photos $2 - ;; - "backup") - open_cryptdevice $backup $2 - ;; - "video") - open_cryptdevice $video $2 - ;; - "videobackup") - open_cryptdevice $videobackup $2 - ;; - "media") - open_cryptdevice $media $2 - ;; - "mediabackup") - open_cryptdevice $mediabackup $2 - ;; - "audiobackup") - open_cryptdevice $audiobackup $2 - ;; - esac + open_cryptdevice ${devices[${2}]} $2 mount_cryptdevice $2 ;; "close") unmount_cryptdevice $2 close_cryptdevice $2 ;; + *) + echo "cs only understands 'open' and 'close'." + exit 1 + ;; esac +exit 0 + +#TODO: create mount point if not available +#TODO: display list of devices available, if no argument given -- cgit v1.2.3-70-g09d2