ttpm - scripts - various script and utils
 (HTM) git clone git://z3bra.org/scripts
 (DIR) Log
 (DIR) Files
 (DIR) Refs
       ---
       ttpm (2330B)
       ---
            1 #!/bin/sh
            2 # Copyright (C) 2013-2015 Sören Tempel
            3 #
            4 # This program is free software: you can redistribute it and/or modify
            5 # it under the terms of the GNU General Public License as published by
            6 # the Free Software Foundation, either version 3 of the License, or
            7 # (at your option) any later version.
            8 #
            9 # This program is distributed in the hope that it will be useful,
           10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
           11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
           12 # GNU General Public License for more details.
           13 #
           14 # You should have received a copy of the GNU General Public License
           15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
           16 
           17 umask 077
           18 
           19 ##
           20 # Variables
           21 ##
           22 
           23 GPG_OPTS="--quiet --yes --batch"
           24 STORE_DIR="${PASSWORD_STORE_DIR:-${HOME}/.pass}"
           25 
           26 if [ -r "${STORE_DIR}/.gpg-id" ] && [ -z "${PASSWORD_STORE_KEY}" ]; then
           27   read -r PASSWORD_STORE_KEY < "${STORE_DIR}/.gpg-id"
           28 fi
           29 
           30 if [ -n "${PASSWORD_STORE_KEY}" ]; then
           31   GPG_OPTS="${GPG_OPTS} --recipient ${PASSWORD_STORE_KEY}"
           32 else
           33   GPG_OPTS="${GPG_OPTS} --default-recipient-self"
           34 fi
           35 
           36 ##
           37 # Helper
           38 ##
           39 
           40 abort() {
           41   echo "${1}" 1>&2
           42   exit 1
           43 }
           44 
           45 readpw() {
           46   if [ -t 0 ]; then
           47     printf "%s" "${1}"
           48     stty -echo
           49   fi
           50 
           51   IFS= read -r "${2}"
           52   [ -t 0 ] && stty echo
           53 }
           54 
           55 ##
           56 # Commands
           57 ##
           58 
           59 show() {
           60   entry_name="${1}"
           61   entry_path="${STORE_DIR}/${entry_name}.gpg"
           62 
           63   if [ -z "${entry_name}" ]; then
           64     abort "usage: tpm <entry>"
           65   fi
           66 
           67   if [ ! -e "${entry_path}" ]; then
           68     abort "The requested entry doesn't exist."
           69   fi
           70 
           71   gpg2 ${GPG_OPTS} --decrypt "${entry_path}"
           72 }
           73 
           74 insert() {
           75   entry_name="${1}"
           76   entry_path="${STORE_DIR}/${entry_name}.gpg"
           77 
           78   if [ -z "${entry_name}" ]; then
           79     abort "usage: tpm -i <entry>"
           80   fi
           81 
           82   if [ -e "${entry_path}" ]; then
           83     abort "This entry already exists, please remove it first."
           84   fi
           85 
           86   password=""
           87   readpw "Password for '${entry_name}': " password
           88   if [ -t 0 ]; then
           89     printf "\n"
           90   fi
           91 
           92   if [ -z "${password}" ]; then
           93     abort "You didn't specify a password."
           94   fi
           95 
           96   mkdir -p "$(dirname "${entry_path}")"
           97   printf '%s\n' "${password}" | gpg2 ${GPG_OPTS} \
           98     --encrypt --output "${entry_path}"
           99 }
          100 
          101 ##
          102 # Parse input
          103 ##
          104 
          105 if [ $# -lt 1 ]; then
          106         tree $STORE_DIR|sed 's/.gpg$//'
          107         exit 0
          108 fi
          109 
          110 case "${1}" in
          111         -i) insert "${2}" ;;
          112         -h) abort "usage: tpm [-i] <entry>" ;;
          113         *) show "$1"
          114 esac