diff options
-rwxr-xr-x | mount | 67 |
1 files changed, 67 insertions, 0 deletions
@@ -0,0 +1,67 @@ +#!/bin/bash + +set -e + +base_dir="$(readlink -f "${0%/*}")" + +if ! mountpoint -q "${base_dir}"; then + echo 'Komisch, das Skript liegt nicht, wo es sein sollte.' + exit 1 +fi + +if ! [ -d "${base_dir%1}2" ]; then + echo 'Neben dem Verzeichnis des Skriptes sollte es noch ein zweites geben.' + exit +fi + +if mountpoint -q "${base_dir%1}2"; then + mount=false +else + mount=true +fi + +device="$( + mount | \ + grep "^\S\+1 on ${base_dir} " | \ + awk '{print $1}' | \ + sed 's|1$||' +)" + +if ! [ -b "${device}" ] || \ + ! [ -b "${device}1" ] || \ + ! [ -b "${device}2" ]; then + echo 'Komisch, das Skript befindet sich nicht auf der Partition, wo es sein sollte.' + exit 1 +fi + +crypt_device="usb-disk-$( + sha512sum "$0" | \ + head -c16 +)" + +tmp_dir="$(mktemp -d)" +trap 'rm -rf --one-file-system "${tmp_dir}"' EXIT + +if ${mount}; then + + mkfifo "${tmp_dir}/fifo" + + gpg -o - -d "/mnt/usb/usbhd-sdg1/key_$(whoami).gpg" > \ + /dev/null + + gpg -o - -d "/mnt/usb/usbhd-sdg1/key_$(whoami).gpg" > \ + "${tmp_dir}/fifo" & + + su -c ' + cryptsetup luksOpen "'"${device}2"'" "'"${crypt_device}"'" --key-file "'"${tmp_dir}/fifo"'" + mount "/dev/mapper/'"${crypt_device}"'" "'"${base_dir%1}2"'" + ' + +else + + su -c ' + umount -l "'"${base_dir%1}2"'" + cryptsetup luksClose "'"${crypt_device}"'" + ' + +fi |