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

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