URI: 
       passino.sh - passino - password-store clone in POSIX shell script
  HTML hg clone https://bitbucket.org/iamleot/passino
   DIR Log
   DIR Files
   DIR Refs
       ---
       passino.sh
       ---
            1 #!/bin/sh
            2 
            3 #
            4 # Copyright (c) 2019 Leonardo Taccari
            5 # All rights reserved.
            6 # 
            7 # Redistribution and use in source and binary forms, with or without
            8 # modification, are permitted provided that the following conditions
            9 # are met:
           10 # 
           11 # 1. Redistributions of source code must retain the above copyright
           12 #    notice, this list of conditions and the following disclaimer.
           13 # 2. Redistributions in binary form must reproduce the above copyright
           14 #    notice, this list of conditions and the following disclaimer in the
           15 #    documentation and/or other materials provided with the distribution.
           16 # 
           17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
           18 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
           19 # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
           20 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
           21 # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
           22 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
           23 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
           24 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
           25 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
           26 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
           27 # POSSIBILITY OF SUCH DAMAGE.
           28 #
           29 
           30 umask 077
           31 
           32 editor=${EDITOR:=vi}
           33 gpg=gpg2
           34 password_store_dir=${PASSWORD_STORE_DIR:=${HOME}/.password-store}
           35 
           36 
           37 #
           38 # Print usage message and exit
           39 #
           40 usage()
           41 {
           42         echo "usage: passino edit|help|list|show"
           43 
           44         exit 1
           45 }
           46 
           47 
           48 #
           49 # Print help message and exit
           50 #
           51 help()
           52 {
           53         echo "usage: passino edit|help|ls|show"
           54         echo
           55         echo "e|ed|edit passname"
           56         echo "Edit the password \`passname'."
           57         echo
           58         echo "h|help"
           59         echo "Show an help message"
           60         echo
           61         echo "l|ls|list"
           62         echo "List all passwords"
           63         echo
           64         echo "s|show passname"
           65         echo "Print the password \`passname' to the stdout."
           66         echo
           67 
           68         exit 1
           69 }
           70 
           71 
           72 #
           73 # Invoke GPG to decrypt stdin
           74 #
           75 decrypt()
           76 {
           77 
           78         ${gpg} -q -d
           79 }
           80 
           81 
           82 #
           83 # Invoke GPG to encrypt stdin
           84 #
           85 encrypt()
           86 {
           87 
           88         ${gpg} -q -e --default-recipient-self
           89 }
           90 
           91 
           92 #
           93 # Edit the password passname.
           94 #
           95 edit()
           96 {
           97         passname=$1
           98         passfile="${password_store_dir}/${passname}.gpg"
           99 
          100         if [ -z "${passname}" ]; then
          101                 return
          102         fi
          103 
          104         tmpfile=`mktemp -t passino` || return
          105 
          106         if [ -f "${passfile}" ]; then
          107                 decrypt < "${passfile}" > "${tmpfile}"
          108         fi
          109 
          110         ${EDITOR} "${tmpfile}"
          111         encrypt < "${tmpfile}" > "${passfile}"
          112         rm -f "${tmpfile}"
          113 }
          114 
          115 #
          116 # List all passwords
          117 #
          118 list()
          119 {
          120 
          121         find ${password_store_dir} -name '*.gpg' |
          122                 sed -e "s;^${password_store_dir};;" \
          123                     -e 's;^/;;' \
          124                     -e 's;\.gpg$;;' |
          125                 sort
          126 }
          127 
          128 
          129 #
          130 # Decrypt the password passname and print it to stdout.
          131 #
          132 show()
          133 {
          134         passname=$1
          135         passfile="${password_store_dir}/${passname}.gpg"
          136 
          137         if [ -z "${passname}" ] || [ ! -f "${passfile}" ]; then
          138                 return
          139         fi
          140 
          141         decrypt < "${passfile}"
          142 }
          143 
          144 
          145 #
          146 # passino, a password-store clone in POSIX shell script
          147 #
          148 main()
          149 {
          150         subcommand=$1
          151 
          152         case $subcommand in
          153         e|ed|edit)
          154                 passname=$2
          155                 edit "${passname}"
          156                 ;;
          157         h|help)
          158                 help
          159                 ;;
          160         l|ls|list)
          161                 list
          162                 ;;
          163         s|show)
          164                 passname=$2
          165                 show "${passname}"
          166                 ;;
          167         *)
          168                 usage
          169                 ;;
          170         esac
          171 
          172         exit 0
          173 }
          174 
          175 main "$@"