pass: switch to using email addresses instead of keyids
[zanchey/uccpass.git] / uccpass
1 #!/bin/sh
2 #
3 # uccpass, a wrapper around the pass(1) password manager for UCC
4 #
5 # David Adam <[email protected]>, 2015
6 # (and your name here?)
7 #
8 # Permission is hereby granted, free of charge, to any person obtaining a copy
9 # of this software and associated documentation files (the "Software"), to deal
10 # in the Software without restriction, including without limitation the rights
11 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 # copies of the Software, and to permit persons to whom the Software is
13 # furnished to do so, subject to the following conditions:
14
15 # The above copyright notice and this permission notice shall be included in
16 # all copies or substantial portions of the Software.
17
18 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 # THE SOFTWARE.
25
26 init () {
27     # Check for pass subcommand
28     if ! type pass >/dev/null; then
29         echo "uccpass: can't find the pass(1) command, is it installed correctly?"
30     fi
31     if ! type gpg2 >/dev/null; then
32         echo "uccpass: can't find gpg2(1), is it installed correctly?"
33     fi
34
35     UCCPASS_ROOT="/home/wheel/bin/uccpass"
36     UCCPASS_KEYRING="$UCCPASS_ROOT/keyring.gpg"
37     export PASSWORD_STORE_DIR="$UCCPASS_ROOT/store"
38     export PASSWORD_STORE_UMASK=007
39
40     export GIT_AUTHOR_EMAIL="[email protected]"
41     export GIT_AUTHOR_NAME="`getent passwd $USER | cut -d: -f5`"
42
43     export GPG_TTY=`tty`
44     export PASSWORD_STORE_GPG_OPTS="--trust-model always"
45
46     export PATH=$UCCPASS_ROOT/pass/bin:$PATH
47
48     if [ -n "$UCCPASS_DEBUG" ]; then set -x; fi
49 }
50
51 add_to_shell () {
52     # If you add more shells here, don't forget to add them to the egrep below.
53     # 
54     # Use keychain. It is just better than futzing around with {ssh,gpg}-agent.
55     # Maybe when systemd is on everything then https://github.com/vodik/envoy
56     # will be better; until then, stick with what works.
57     case $USER_SHELL in
58         bash)
59             bash_profile=". $UCCPASS_ROOT/bash_profile.uccpass"
60             grep -qsF "$bash_profile" ~/.bash_profile || echo "$bash_profile" >> ~/.bash_profile
61             bashrc=". $UCCPASS_ROOT/bashrc.uccpass"
62             grep -qsF "$bashrc" ~/.bashrc || echo "$bashrc" >> ~/.bashrc
63             ;;
64         zsh)
65             zprofile=". $UCCPASS_ROOT/zprofile.uccpass"
66             grep -qsF "$zprofile" ~/.zprofile || echo "$zprofile" >> ~/.zprofile
67             zshenv=". $UCCPASS_ROOT/zshenv.uccpass"
68             grep -qsF "$zshenv" ~/.zshenv || echo "$zshenv" >> ~/.zshenv
69             ;;
70         fish)
71             fishconfig="source $UCCPASS_ROOT/config.uccpass.fish"
72             grep -qsF "$fishconfig" ~/.config/fish/config.fish || { mkdir -p ~/.config/fish; echo $fishconfig >> ~/.config/fish/config.fish ; }
73             ;;
74     esac
75 }
76
77 refresh_keys () {
78     gpg2 --quiet --import $UCCPASS_KEYRING
79 }
80
81 new_user_setup () {
82     echo " >  Setting you up for uccpass"
83
84     # Set up an agent
85     echo -n " >> Checking for running GPG agent... "
86     # Checking for a running agent sucks!
87     if [ -z "$GPG_AGENT_INFO" ] || ! gpg-connect-agent /bye 2>/dev/null; then
88         echo "not found."
89         USER_SHELL=`basename $SHELL`
90         if echo $USER_SHELL | egrep -q 'zsh|bash|fish'; then
91             echo " [!] uccpass can install an password caching agent into your shell initialisation files."
92             echo -n " [?] Do you want to do so? [Y/n] " 
93             read install_agent
94             case $install_agent in
95                 N|n|[Nn][Oo])
96                     ;;
97                 *)
98                     add_to_shell
99                     echo " [!] You will need to start a new shell to pick up your new agent."
100                     ;;
101             esac
102         fi
103         echo -n " >> Starting GPG agent... "
104         eval `SHELL=/bin/sh keychain --eval --quiet --agents gpg`
105     fi
106     echo "ok."
107
108     # Generate new GPG key
109     if ! gpg --list-keys $GIT_AUTHOR_EMAIL >/dev/null 2>&1; then
110         echo " >> Generating new GPG key for $GIT_AUTHOR_NAME (UCC Wheel Group)"
111         echo " [!] At the next prompt, you will be asked for a secure passphrase."
112         echo "     This controls access to the password store - please choose something secure."
113         echo " [?] Press ENTER to continue..."
114         read -r _
115         new_key_info="    Key-Type: default
116                 Subkey-Type: default
117                 Name-Real: $GIT_AUTHOR_NAME (UCC Wheel Group)
118                 Name-Email: $GIT_AUTHOR_EMAIL
119                 Expire-Date: 5y
120                 %ask-passphrase
121                  "
122         echo "$new_key_info" | gpg2 --gen-key --batch
123     fi
124
125     # Get the key fingerprint
126     KEY_FINGERPRINT=`gpg --list-secret-keys --with-fingerprint --with-colons $GIT_AUTHOR_EMAIL | grep '^fpr' | head -n 1 | cut -d: -f 10`
127
128     # Add to .gpg-id
129     echo -n " >> Adding your key to the access list... "
130     if grep -qF "<$GIT_AUTHOR_EMAIL>" $PASSWORD_STORE_DIR/.gpg-id; then
131         echo "already present!"
132     else
133         echo "<$GIT_AUTHOR_EMAIL>" >> $PASSWORD_STORE_DIR/.gpg-id
134         echo "ok."
135     fi
136
137     # Add to key list
138     # XXX: is it worth submitting these to an online keyserver?
139     echo -n " >> Adding your key to the shared keyring... "
140     if gpg2 --with-fingerprint --with-colons $UCCPASS_KEYRING | grep -qF "$KEY_FINGERPRINT"; then
141         echo "already present!"
142     else
143         gpg2 --export --armor "$KEY_FINGERPRINT" >> $UCCPASS_KEYRING && echo "ok."
144     fi
145
146     # Done!
147     echo " > uccpass setup complete."
148     echo " [!] Ask someone with existing access to the password store to run \`uccpass reload\`."
149 }
150
151 init
152
153 case "$1" in
154     setup)
155         new_user_setup
156         refresh_keys
157         ;;
158     reload)
159         refresh_keys
160         echo "The following keys have access to the password store:"
161         gpg2 --with-colons --list-keys `cat $PASSWORD_STORE_DIR/.gpg-id` | grep '^uid' | cut -d: -f 10
162         echo
163         command pass init `cat $PASSWORD_STORE_DIR/.gpg-id`
164         ;;
165     help|--help)
166         command pass $@
167         echo "uccpass also supports the following commands:"
168         echo "setup: generate a new key and insert it into the password store"
169         echo "reload: re-encrypt the password store"
170         ;;
171     insert|edit|generate|rm|cp|mv|git)
172         refresh_keys
173         command pass $@
174         ;;
175     *)
176         command pass $@
177         ;;
178 esac
179
180 # vim: autoindent tabstop=4 shiftwidth=4 expandtab softtabstop=4

UCC git Repository :: git.ucc.asn.au