tpm

Tiny password manager

git clone https://git.8pit.net/tpm.git

  1#!/bin/sh
  2# Copyright (C) 2013-2016 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
 17umask 077
 18
 19##
 20# Variables
 21##
 22
 23GPG_OPTS="--quiet --yes --batch"
 24STORE_DIR="${PASSWORD_STORE_DIR:-${HOME}/.password-store}"
 25
 26if [ -r "${STORE_DIR}/.gpg-id" ] && [ -z "${PASSWORD_STORE_KEY}" ]; then
 27  read -r PASSWORD_STORE_KEY < "${STORE_DIR}/.gpg-id"
 28fi
 29
 30##
 31# Helper
 32##
 33
 34abort() {
 35  printf '%s\n' "${1}" 1>&2
 36  exit 1
 37}
 38
 39gpg() {
 40  if [ -n "${PASSWORD_STORE_KEY}" ]; then
 41    gpg2 $GPG_OPTS --recipient "${PASSWORD_STORE_KEY}" "$@"
 42  else
 43    gpg2 $GPG_OPTS --default-recipient-self "$@"
 44  fi
 45}
 46
 47readpw() {
 48  if [ -t 0 ]; then
 49    printf "%s" "${1}"
 50    stty -echo
 51  fi
 52
 53  IFS= read -r "${2}"
 54  [ -t 0 ] && stty echo
 55}
 56
 57##
 58# Commands
 59##
 60
 61show() {
 62  entry_name="${1}"
 63  entry_path="${STORE_DIR}/${entry_name}.gpg"
 64
 65  if [ -z "${entry_name}" ]; then
 66    abort "USAGE: tpm show ENTRY"
 67  fi
 68
 69  if [ ! -e "${entry_path}" ]; then
 70    abort "The requested entry doesn't exist."
 71  fi
 72
 73  gpg --decrypt "${entry_path}"
 74}
 75
 76insert() {
 77  entry_name="${1}"
 78  entry_path="${STORE_DIR}/${entry_name}.gpg"
 79
 80  if [ -z "${entry_name}" ]; then
 81    abort "USAGE: tpm insert ENTRY"
 82  fi
 83
 84  if [ -e "${entry_path}" ]; then
 85    abort "This entry already exists, please remove it first."
 86  fi
 87
 88  password=""
 89  readpw "Password for '${entry_name}': " password
 90  if [ -t 0 ]; then
 91    printf '\n'
 92  fi
 93
 94  if [ -z "${password}" ]; then
 95    abort "You didn't specify a password."
 96  fi
 97
 98  mkdir -p "${entry_path%/*}"
 99  printf '%s\n' "${password}" | gpg --encrypt \
100    --output "${entry_path}"
101}
102
103##
104# Parse input
105##
106
107if [ $# -gt 2 ]; then
108  abort "tpm doesn't accept more than two arguments."
109fi
110
111case "${1}" in
112  "show")   show   "${2}" ;;
113  "insert") insert "${2}" ;;
114  *) abort "USAGE: tpm COMMAND ENTRY" ;;
115esac
116
117# vim: et:sw=2:sts=2