6f43632307b25d235ed5094457e96fe77de5b7a8
[monkeysphere.git] / src / monkeysphere
1 #!/usr/bin/env bash
2
3 # monkeysphere: Monkeysphere client tool
4 #
5 # The monkeysphere scripts are written by:
6 # Jameson Rollins <jrollins@fifthhorseman.net>
7 # Jamie McClelland <jm@mayfirst.org>
8 # Daniel Kahn Gillmor <dkg@fifthhorseman.net>
9 # Micah Anderson <micah@riseup.net>
10 #
11 # They are Copyright 2008-2009, and are all released under the GPL, version 3
12 # or later.
13
14 ########################################################################
15 set -e
16
17 PGRM=$(basename $0)
18
19 SYSSHAREDIR=${MONKEYSPHERE_SYSSHAREDIR:-"/usr/share/monkeysphere"}
20 export SYSSHAREDIR
21 . "${SYSSHAREDIR}/defaultenv"
22 . "${SYSSHAREDIR}/common"
23
24 # sharedir for host functions
25 MSHAREDIR="${SYSSHAREDIR}/m"
26
27 # UTC date in ISO 8601 format if needed
28 DATE=$(date -u '+%FT%T')
29
30 # unset some environment variables that could screw things up
31 unset GREP_OPTIONS
32
33 # set the file creation mask to be only owner rw
34 umask 077
35
36 ########################################################################
37 # FUNCTIONS
38 ########################################################################
39
40 usage() {
41     cat <<EOF >&2
42 usage: $PGRM <subcommand> [options] [args]
43 Monkeysphere client tool.
44
45 subcommands:
46  update-known_hosts (k) [HOST]...    update known_hosts file
47  update-authorized_keys (a)          update authorized_keys file
48  gen-subkey (g) [KEYID]              generate an authentication subkey
49    --length (-l) BITS                  key length in bits (2048)
50  ssh-proxycommand HOST [PORT]        monkeysphere ssh ProxyCommand
51    --no-connect                        do not make TCP connection to host
52  subkey-to-ssh-agent (s)             store authentication subkey in ssh-agent
53  sshfpr (f) KEYID                    output ssh fingerprint of gpg key
54  version (v)                         show version number
55  help (h,?)                          this help
56
57 EOF
58 }
59
60 # user gpg command to define common options
61 gpg_user() {
62     gpg --no-greeting --quiet --no-tty "$@"
63 }
64
65 # output the ssh fingerprint of a gpg key
66 gpg_ssh_fingerprint() {
67     keyid="$1"
68     local tmpfile=$(mktemp)
69
70     # trap to remove tmp file if break
71     trap "rm -f $tmpfile" EXIT
72
73     # use temporary file, since ssh-keygen won't accept keys on stdin
74     gpg_user --export "$keyid" | openpgp2ssh "$keyid" >"$tmpfile"
75     ssh-keygen -l -f "$tmpfile" | awk '{ print $1, $2, $4 }'
76
77     # remove the tmp file
78     trap - EXIT
79     rm -rf "$tmpfile"
80 }
81
82 # take a secret key ID and check that only zero or one ID is provided,
83 # and that it corresponds to only a single secret key ID
84 check_gpg_sec_key_id() {
85     local gpgSecOut
86
87     case "$#" in
88         0)
89             gpgSecOut=$(gpg_user --fixed-list-mode --list-secret-keys --with-colons 2>/dev/null | egrep '^sec:')
90             ;;
91         1)
92             gpgSecOut=$(gpg_user --fixed-list-mode --list-secret-keys --with-colons "$1" | egrep '^sec:') || failure
93             ;;
94         *)
95             failure "You must specify only a single primary key ID."
96             ;;
97     esac
98
99     # check that only a single secret key was found
100     case $(echo "$gpgSecOut" | grep -c '^sec:') in
101         0)
102             failure "No secret keys found.  Create an OpenPGP key with the following command:
103  gpg --gen-key"
104             ;;
105         1)
106             echo "$gpgSecOut" | cut -d: -f5
107             ;;
108         *)
109             local seckeys=$(echo "$gpgSecOut" | cut -d: -f5)
110             failure "Multiple primary secret keys found:
111 $seckeys
112 Please specify which primary key to use."
113             ;;
114     esac
115 }
116
117 # check that a valid authentication subkey does not already exist
118 check_gpg_authentication_subkey() {
119     local keyID
120     local IFS
121     local line
122     local type
123     local validity
124     local usage
125
126     keyID="$1"
127
128     # check that a valid authentication key does not already exist
129     IFS=$'\n'
130     for line in $(gpg_user --fixed-list-mode --list-keys --with-colons "$keyID") ; do
131         type=$(echo "$line" | cut -d: -f1)
132         validity=$(echo "$line" | cut -d: -f2)
133         usage=$(echo "$line" | cut -d: -f12)
134
135         # look at keys only
136         if [ "$type" != 'pub' -a "$type" != 'sub' ] ; then
137             continue
138         fi
139         # check for authentication capability
140         if ! check_capability "$usage" 'a' ; then
141             continue
142         fi
143         # if authentication key is valid, prompt to continue
144         if [ "$validity" = 'u' ] ; then
145             echo "A valid authentication key already exists for primary key '$keyID'." 1>&2
146             if [ "$PROMPT" = "true" ] ; then
147                 read -p "Are you sure you would like to generate another one? (y/N) " OK; OK=${OK:N}
148                 if [ "${OK/y/Y}" != 'Y' ] ; then
149                     failure "aborting."
150                 fi
151                 break
152             else
153                 failure "aborting."
154             fi
155         fi
156     done
157 }
158
159 ########################################################################
160 # MAIN
161 ########################################################################
162
163 # set unset default variables
164 GNUPGHOME=${GNUPGHOME:="${HOME}/.gnupg"}
165 KNOWN_HOSTS="${HOME}/.ssh/known_hosts"
166 HASH_KNOWN_HOSTS="true"
167 AUTHORIZED_KEYS="${HOME}/.ssh/authorized_keys"
168
169 # unset the check keyserver variable, since that needs to have
170 # different defaults for the different functions
171 unset CHECK_KEYSERVER
172
173 # load global config
174 [ -r "${SYSCONFIGDIR}/monkeysphere.conf" ] \
175     && . "${SYSCONFIGDIR}/monkeysphere.conf"
176
177 # set monkeysphere home directory
178 MONKEYSPHERE_HOME=${MONKEYSPHERE_HOME:="${HOME}/.monkeysphere"}
179 mkdir -p -m 0700 "$MONKEYSPHERE_HOME"
180
181 # load local config
182 [ -e ${MONKEYSPHERE_CONFIG:="${MONKEYSPHERE_HOME}/monkeysphere.conf"} ] \
183     && . "$MONKEYSPHERE_CONFIG"
184
185 # set empty config variables with ones from the environment
186 GNUPGHOME=${MONKEYSPHERE_GNUPGHOME:=$GNUPGHOME}
187 LOG_LEVEL=${MONKEYSPHERE_LOG_LEVEL:=$LOG_LEVEL}
188 KEYSERVER=${MONKEYSPHERE_KEYSERVER:=$KEYSERVER}
189 # if keyserver not specified in env or conf, then look in gpg.conf
190 if [ -z "$KEYSERVER" ] ; then
191     if [ -f "${GNUPGHOME}/gpg.conf" ] ; then
192         KEYSERVER=$(grep -e "^[[:space:]]*keyserver " "${GNUPGHOME}/gpg.conf" | tail -1 | awk '{ print $2 }')
193     fi
194 fi
195 PROMPT=${MONKEYSPHERE_PROMPT:=$PROMPT}
196 KNOWN_HOSTS=${MONKEYSPHERE_KNOWN_HOSTS:=$KNOWN_HOSTS}
197 HASH_KNOWN_HOSTS=${MONKEYSPHERE_HASH_KNOWN_HOSTS:=$HASH_KNOWN_HOSTS}
198 AUTHORIZED_KEYS=${MONKEYSPHERE_AUTHORIZED_KEYS:=$AUTHORIZED_KEYS}
199
200 # other variables not in config file
201 AUTHORIZED_USER_IDS=${MONKEYSPHERE_AUTHORIZED_USER_IDS:="${MONKEYSPHERE_HOME}/authorized_user_ids"}
202 REQUIRED_HOST_KEY_CAPABILITY=${MONKEYSPHERE_REQUIRED_HOST_KEY_CAPABILITY:="a"}
203 REQUIRED_USER_KEY_CAPABILITY=${MONKEYSPHERE_REQUIRED_USER_KEY_CAPABILITY:="a"}
204 # note that only using '=' instead of ':=' tests only if the variable
205 # in unset, not if it's "null"
206 LOG_PREFIX=${MONKEYSPHERE_LOG_PREFIX='ms: '}
207
208 # export GNUPGHOME and make sure gpg home exists with proper
209 # permissions
210 export GNUPGHOME
211 mkdir -p -m 0700 "$GNUPGHOME"
212 export LOG_LEVEL
213
214 # get subcommand
215 COMMAND="$1"
216 [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
217 shift
218
219 case $COMMAND in
220     'update-known_hosts'|'update-known-hosts'|'k')
221         # whether or not to check keyservers
222         CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:=${CHECK_KEYSERVER:="true"}}
223
224         # if hosts are specified on the command line, process just
225         # those hosts
226         if [ "$1" ] ; then
227             update_known_hosts "$@"
228
229         # otherwise, if no hosts are specified, process every host
230         # in the user's known_hosts file
231         else
232             process_known_hosts
233         fi
234         ;;
235
236     'update-authorized_keys'|'update-authorized-keys'|'a')
237         # whether or not to check keyservers
238         CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:=${CHECK_KEYSERVER:="true"}}
239
240         # process authorized_user_ids file
241         process_authorized_user_ids "$AUTHORIZED_USER_IDS"
242         ;;
243
244     'import-subkey'|'i')
245         source "${MSHAREDIR}/import_subkey"
246         import_subkey "$@"
247         ;;
248
249     'gen-subkey'|'g')
250         source "${MSHAREDIR}/gen_subkey"
251         gen_subkey "$@"
252         ;;
253
254     'ssh-proxycommand'|'p')
255         source "${MSHAREDIR}/ssh_proxycommand"
256         ssh_proxycommand "$@"
257         ;;
258
259     'subkey-to-ssh-agent'|'s')
260         source "${MSHAREDIR}/subkey_to_ssh_agent"
261         subkey_to_ssh_agent "$@"
262         ;;
263
264     'sshfpr'|'f')
265         gpg_ssh_fingerprint "$@"
266         ;;
267
268     'version'|'v')
269         version
270         ;;
271
272     '--help'|'help'|'-h'|'h'|'?')
273         usage
274         ;;
275
276     *)
277         failure "Unknown command: '$COMMAND'
278 Type '$PGRM help' for usage."
279         ;;
280 esac