pm

barebones password manager
git clone git://git.ckyln.com/~cem/pm.git
Log | Files | Refs | README

pm (1515B)


      1 #!/bin/sh
      2 umask 077
      3 error() { printf '\033[1;31m!> \033[merror: %s\n' "$@" >&2 ;}
      4 die() { error "$1" ; exit 1 ;}
      5 
      6 PM_DIR="${PM_DIR:-$HOME/.local/share/pm}"
      7 usage() { printf '\033[1;36m-> \033[m%s\n' "usage: ${0##*/} [a|d|l|s] [options]" "" \
      8         "[a]dd  <name> - Reads the password from stdin to the given entry" \
      9         "[d]el  <name> - Deletes given enry" \
     10         "[l]ist        - Lists all the passwords" \
     11         "[s]how <name> - Shows the given password" "" \
     12         "VARIABLES:" "PM_DIR:      $PM_DIR" "PM_GPG_USER: $PM_GPG_USER" >&2 ; exit "${1:-0}" ;}
     13 
     14 gpg="$(command -v gpg2 || command -v gpg)" || die "gnupg cannot be found"
     15 case "$1" in
     16     a|add)
     17         [ "$2" ] || usage 1
     18         [ "$PM_GPG_USER" ] || die "Please set a \$PM_GPG_USER variable"
     19         [ -e "$PM_DIR/$2.asc" ] && die "an entry for $2 already exists"
     20         mkdir -p "$PM_DIR"
     21         tr -d '\n' < /dev/stdin > "$PM_DIR/$2"
     22         "$gpg" -e -a -r "$PM_GPG_USER" -- "$PM_DIR/$2" ||
     23             error "Could not encrypt password"
     24         rm -f "$PM_DIR/$2"
     25         ;;
     26     d|del) [ "$2" ] || usage 1 ; rm -f "$PM_DIR/$2.asc" ;;
     27     l|ls|list) { find "$PM_DIR" -type f -name '*asc' 2>/dev/null || ls -1 "$PM_DIR"/*.asc ;} | sort ;;
     28     s|show)
     29         [ "$2" ] || usage 1
     30         [ -r "$PM_DIR/$2.asc" ] || 
     31             die "Entry for $2 doesn't exist or is not readable"
     32         "$gpg" --decrypt "$PM_DIR/$2.asc" 2>/dev/null | tr -d '\n' ||
     33             die "Could not decrypt $PM_DIR/$2.asc"
     34         ;;
     35     *) usage 0 ;; esac