Skip to content

Instantly share code, notes, and snippets.

@bergercookie
Last active December 29, 2017 01:05
Show Gist options
  • Save bergercookie/d808bade22e62afbb2abe64fb1d20688 to your computer and use it in GitHub Desktop.
Save bergercookie/d808bade22e62afbb2abe64fb1d20688 to your computer and use it in GitHub Desktop.
Script for decrypting and viewing docs stored in the Pass Unix Password Manager
#!/usr/bin/env bash
# vi:syntax=sh
# vi:filetype=sh
# Current script decrypts and opens a gpg-encrypted document that is stored in
# the password-store of the *pass* Unix password manager
#
# Support exists exclusively on Linux desktop platforms
function print_fatal_msg() {
printf "$1\nPExiting...\n"
exit 1
}
gpg_prog="$(which gpg)"
recip="Nikos Koukis"
dst_file="/tmp/$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1)"
open_prog=
del_prog=
del_prog_flags=
if [[ $(uname) == "Linux" ]]; then
del_prog=$(which shred)
del_prog_flags="--remove"
elif [[ $(uname) == "Darwin" ]]; then
del_prog=$(which srm)
else
print_fatal_msg "Unknown platform \($(uname)\)"
fi
# Verify number of arguments
if [[ $# != 1 ]]; then
print_fatal_msg "pass_open_doc <path_to_file>"
fi
# Verify that the path exists
fpath=$1
printf "${fpath}"
if ! [[ $(ls "${fpath}") ]]; then
print_fatal_msg "Filepath ${fpath} doesn't exist."
fi
# Decrypt
${gpg_prog} --recipient "${recip}" --decrypt --output "${dst_file}" "${fpath}"
declare -A app_name_to_exec
# Don't use xdg-open as it it launched as a different process, so the shell
# cannot wait until it finished...
#
# Determine the mime type
# https://unix.stackexchange.com/questions/114224/open-file-with-default-program-and-wait-until-the-app-is-terminated
ftype=$(xdg-mime query filetype ${dst_file})
open_prog_raw=$(xdg-mime query default ${ftype})
open_prog=$(echo ${open_prog_raw} | sed 's/\..*//')
# remove the gibberish kde4-...Application.. from kde4 application names
open_prog=$(echo ${open_prog} | sed 's/kde4-\(.*\)Application.*/\1/')
${open_prog} ${dst_file}
# Make sure I delete the destination file
printf "Removing output file...\n"
${del_prog} ${del_prog_flags} ${dst_file}
printf "Done!\n"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment