1#!/bin/sh2# Copyright (C) 2013-2016 Sören Tempel3#4# This program is free software: you can redistribute it and/or modify5# it under the terms of the GNU General Public License as published by6# the Free Software Foundation, either version 3 of the License, or7# (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 of11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the12# GNU General Public License for more details.13#14# You should have received a copy of the GNU General Public License15# along with this program. If not, see <http://www.gnu.org/licenses/>.1617umask 0771819##20# Variables21##2223GPG_OPTS="--quiet --yes --batch"24STORE_DIR="${PASSWORD_STORE_DIR:-${HOME}/.password-store}"2526if [ -r "${STORE_DIR}/.gpg-id" ] && [ -z "${PASSWORD_STORE_KEY}" ]; then27 read -r PASSWORD_STORE_KEY < "${STORE_DIR}/.gpg-id"28fi2930##31# Helper32##3334abort() {35 printf '%s\n' "${1}" 1>&236 exit 137}3839gpg() {40 if [ -n "${PASSWORD_STORE_KEY}" ]; then41 gpg2 $GPG_OPTS --recipient "${PASSWORD_STORE_KEY}" "$@"42 else43 gpg2 $GPG_OPTS --default-recipient-self "$@"44 fi45}4647readpw() {48 if [ -t 0 ]; then49 printf "%s" "${1}"50 stty -echo51 fi5253 IFS= read -r "${2}"54 [ -t 0 ] && stty echo55}5657##58# Commands59##6061show() {62 entry_name="${1}"63 entry_path="${STORE_DIR}/${entry_name}.gpg"6465 if [ -z "${entry_name}" ]; then66 abort "USAGE: tpm show ENTRY"67 fi6869 if [ ! -e "${entry_path}" ]; then70 abort "The requested entry doesn't exist."71 fi7273 gpg --decrypt "${entry_path}"74}7576insert() {77 entry_name="${1}"78 entry_path="${STORE_DIR}/${entry_name}.gpg"7980 if [ -z "${entry_name}" ]; then81 abort "USAGE: tpm insert ENTRY"82 fi8384 if [ -e "${entry_path}" ]; then85 abort "This entry already exists, please remove it first."86 fi8788 password=""89 readpw "Password for '${entry_name}': " password90 if [ -t 0 ]; then91 printf '\n'92 fi9394 if [ -z "${password}" ]; then95 abort "You didn't specify a password."96 fi9798 mkdir -p "${entry_path%/*}"99 printf '%s\n' "${password}" | gpg --encrypt \100 --output "${entry_path}"101}102103##104# Parse input105##106107if [ $# -gt 2 ]; then108 abort "tpm doesn't accept more than two arguments."109fi110111case "${1}" in112 "show") show "${2}" ;;113 "insert") insert "${2}" ;;114 *) abort "USAGE: tpm COMMAND ENTRY" ;;115esac116117# vim: et:sw=2:sts=2