pass: bump to latest release
[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     USER=`whoami`
41     export GIT_AUTHOR_EMAIL="[email protected]"
42     export GIT_AUTHOR_NAME="`getent passwd $USER | cut -d: -f5`"
43
44     export GPG_TTY=`tty`
45     export PASSWORD_STORE_GPG_OPTS="--trust-model always"
46
47     export PATH=$UCCPASS_ROOT/pass/bin:$PATH
48
49     if [ -n "$UCCPASS_DEBUG" ]; then set -x; fi
50 }
51
52 add_to_shell () {
53     # If you add more shells here, don't forget to add them to the egrep below.
54     # 
55     # Use keychain. It is just better than futzing around with {ssh,gpg}-agent.
56     # Maybe when systemd is on everything then https://github.com/vodik/envoy
57     # will be better; until then, stick with what works.
58     case $USER_SHELL in
59         bash)
60             bash_profile=". $UCCPASS_ROOT/bash_profile.uccpass"
61             grep -qsF "$bash_profile" ~/.bash_profile || echo "$bash_profile" >> ~/.bash_profile
62             bashrc=". $UCCPASS_ROOT/bashrc.uccpass"
63             grep -qsF "$bashrc" ~/.bashrc || echo "$bashrc" >> ~/.bashrc
64             ;;
65         zsh)
66             zprofile=". $UCCPASS_ROOT/zprofile.uccpass"
67             grep -qsF "$zprofile" ~/.zprofile || echo "$zprofile" >> ~/.zprofile
68             zshenv=". $UCCPASS_ROOT/zshenv.uccpass"
69             grep -qsF "$zshenv" ~/.zshenv || echo "$zshenv" >> ~/.zshenv
70             ;;
71         fish)
72             fishconfig="source $UCCPASS_ROOT/config.uccpass.fish"
73             grep -qsF "$fishconfig" ~/.config/fish/config.fish || { mkdir -p ~/.config/fish; echo $fishconfig >> ~/.config/fish/config.fish ; }
74             ;;
75     esac
76 }
77
78 refresh_keys () {
79     gpg2 --quiet --import $UCCPASS_KEYRING
80 }
81
82 new_user_setup () {
83     echo " >  Setting you up for uccpass"
84
85     # Set up an agent
86     echo -n " >> Checking for running GPG agent... "
87     # Checking for a running agent sucks!
88     if [ -z "$GPG_AGENT_INFO" ] || ! gpg-connect-agent /bye 2>/dev/null; then
89         echo "not found."
90         USER_SHELL=`basename $SHELL`
91         if echo $USER_SHELL | egrep -q 'zsh|bash|fish'; then
92             echo " [!] uccpass can install an password caching agent into your shell initialisation files."
93             echo -n " [?] Do you want to do so? [Y/n] " 
94             read install_agent
95             case $install_agent in
96                 N|n|[Nn][Oo])
97                     ;;
98                 *)
99                     add_to_shell
100                     echo " [!] You will need to start a new shell to pick up your new agent."
101                     ;;
102             esac
103         fi
104         echo -n " >> Starting GPG agent... "
105         eval `SHELL=/bin/sh keychain --eval --quiet --agents gpg`
106     fi
107     echo "ok."
108
109     # Generate new GPG key
110     if ! gpg --list-keys $GIT_AUTHOR_EMAIL >/dev/null 2>&1; then
111         echo " >> Generating new GPG key for $GIT_AUTHOR_NAME (UCC Wheel Group)"
112         echo " [!] At the next prompt, you will be asked for a secure passphrase."
113         echo "     This controls access to the password store - please choose something secure."
114         echo " [?] Press ENTER to continue..."
115         read -r _
116         new_key_info="    Key-Type: default
117                 Subkey-Type: default
118                 Name-Real: $GIT_AUTHOR_NAME (UCC Wheel Group)
119                 Name-Email: $GIT_AUTHOR_EMAIL
120                 Expire-Date: 5y
121                 %ask-passphrase
122                  "
123         echo "$new_key_info" | gpg2 --gen-key --batch
124     fi
125
126     # Get the key fingerprint
127     KEY_FINGERPRINT=`gpg --list-secret-keys --with-fingerprint --with-colons $GIT_AUTHOR_EMAIL | grep '^fpr' | head -n 1 | cut -d: -f 10`
128
129     # Add to .gpg-id
130     echo -n " >> Adding your key to the access list... "
131     if grep -qF "<$GIT_AUTHOR_EMAIL>" $PASSWORD_STORE_DIR/.gpg-id; then
132         echo "already present!"
133     else
134         echo "<$GIT_AUTHOR_EMAIL>" >> $PASSWORD_STORE_DIR/.gpg-id
135         echo "ok."
136     fi
137
138     # Add to key list
139     # XXX: is it worth submitting these to an online keyserver?
140     echo -n " >> Adding your key to the shared keyring... "
141     if gpg2 --with-fingerprint --with-colons $UCCPASS_KEYRING | grep -qF "$KEY_FINGERPRINT"; then
142         echo "already present!"
143     else
144         gpg2 --export --armor "$KEY_FINGERPRINT" >> $UCCPASS_KEYRING && echo "ok."
145     fi
146
147     # Done!
148     echo " > uccpass setup complete."
149     echo " [!] Ask someone with existing access to the password store to run \`uccpass reload\`."
150 }
151
152 init
153
154 case "$1" in
155     setup)
156         new_user_setup
157         refresh_keys
158         ;;
159     reload)
160         refresh_keys
161         echo "The following keys have access to the password store:"
162         gpg2 --with-colons --list-keys `cat $PASSWORD_STORE_DIR/.gpg-id` | grep '^uid' | cut -d: -f 10
163         echo
164         command pass init `cat $PASSWORD_STORE_DIR/.gpg-id`
165         ;;
166     help|--help)
167         command pass $@
168         echo "uccpass also supports the following commands:"
169         echo "setup: generate a new key and insert it into the password store"
170         echo "reload: re-encrypt the password store"
171         ;;
172     insert|edit|generate|rm|cp|mv|git)
173         refresh_keys
174         command pass $@
175         ;;
176     *)
177         command pass $@
178         ;;
179 esac
180
181 # vim: autoindent tabstop=4 shiftwidth=4 expandtab softtabstop=4

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