added comment about why the key file is named with whitespace
[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@finestructure.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  ssh-proxycommand HOST [PORT]        monkeysphere ssh ProxyCommand
49    --no-connect                        do not make TCP connection to host
50  subkey-to-ssh-agent (s)             store authentication subkey in ssh-agent
51
52  keys-for-userid (u) USERID          output valid keys for given user ids
53  sshfprs-for-userid USERID           output ssh fingerprints for given user ids
54  gen-subkey (g) [KEYID]              generate an authentication subkey
55    --length (-l) BITS                  key length in bits (2048)
56
57  version (v)                         show version number
58  help (h,?)                          this help
59
60 EOF
61 }
62
63 # user gpg command to define common options
64 gpg_user() {
65     gpg --no-greeting --quiet --no-tty "$@"
66 }
67
68 # output the ssh fingerprint of a gpg key
69 gpg_ssh_fingerprint() {
70     keyid="$1"
71     local tmpfile=$(msmktempfile)
72
73     # trap to remove tmp file if break
74     trap "rm -f $tmpfile" EXIT
75
76     # use temporary file, since ssh-keygen won't accept keys on stdin
77     gpg_user --export "$keyid" --no-armor | openpgp2ssh "$keyid" >"$tmpfile"
78     ssh-keygen -l -f "$tmpfile" | awk '{ print $1, $2, $4 }'
79
80     # remove the tmp file
81     trap - EXIT
82     rm -rf "$tmpfile"
83 }
84
85 # take a secret key ID and check that only zero or one ID is provided,
86 # and that it corresponds to only a single secret key ID
87 check_gpg_sec_key_id() {
88     local gpgSecOut
89
90     case "$#" in
91         0)
92             gpgSecOut=$(gpg_user --fixed-list-mode --list-secret-keys --with-colons 2>/dev/null | egrep '^sec:')
93             ;;
94         1)
95             gpgSecOut=$(gpg_user --fixed-list-mode --list-secret-keys --with-colons "$1" | egrep '^sec:') || failure
96             ;;
97         *)
98             failure "You must specify only a single primary key ID."
99             ;;
100     esac
101
102     # check that only a single secret key was found
103     case $(echo "$gpgSecOut" | grep -c '^sec:') in
104         0)
105             failure "No secret keys found.  Create an OpenPGP key with the following command:
106  gpg --gen-key"
107             ;;
108         1)
109             echo "$gpgSecOut" | cut -d: -f5
110             ;;
111         *)
112             local seckeys=$(echo "$gpgSecOut" | cut -d: -f5)
113             failure "Multiple primary secret keys found:
114 $seckeys
115 Please specify which primary key to use."
116             ;;
117     esac
118 }
119
120 # check that a valid authentication subkey does not already exist
121 check_gpg_authentication_subkey() {
122     local keyID
123     local IFS
124     local line
125     local type
126     local validity
127     local usage
128
129     keyID="$1"
130
131     # check that a valid authentication key does not already exist
132     IFS=$'\n'
133     for line in $(gpg_user --fixed-list-mode --list-keys --with-colons "$keyID") ; do
134         type=$(echo "$line" | cut -d: -f1)
135         validity=$(echo "$line" | cut -d: -f2)
136         usage=$(echo "$line" | cut -d: -f12)
137
138         # look at keys only
139         if [ "$type" != 'pub' -a "$type" != 'sub' ] ; then
140             continue
141         fi
142         # check for authentication capability
143         if ! check_capability "$usage" 'a' ; then
144             continue
145         fi
146         # if authentication key is valid, prompt to continue
147         if [ "$validity" = 'u' ] ; then
148             echo "A valid authentication key already exists for primary key '$keyID'." 1>&2
149             if [ "$PROMPT" != "false" ] ; then
150                 printf "Are you sure you would like to generate another one? (y/N) " >&2
151                 read OK; OK=${OK:N}
152                 if [ "${OK/y/Y}" != 'Y' ] ; then
153                     failure "aborting."
154                 fi
155                 break
156             else
157                 failure "aborting."
158             fi
159         fi
160     done
161 }
162
163 ########################################################################
164 # MAIN
165 ########################################################################
166
167 # set unset default variables
168 GNUPGHOME=${GNUPGHOME:="${HOME}/.gnupg"}
169 KNOWN_HOSTS="${HOME}/.ssh/known_hosts"
170 HASH_KNOWN_HOSTS="true"
171 AUTHORIZED_KEYS="${HOME}/.ssh/authorized_keys"
172
173 # unset the check keyserver variable, since that needs to have
174 # different defaults for the different functions
175 unset CHECK_KEYSERVER
176
177 # load global config
178 [ -r "${SYSCONFIGDIR}/monkeysphere.conf" ] \
179     && . "${SYSCONFIGDIR}/monkeysphere.conf"
180
181 # set monkeysphere home directory
182 MONKEYSPHERE_HOME=${MONKEYSPHERE_HOME:="${HOME}/.monkeysphere"}
183 mkdir -p -m 0700 "$MONKEYSPHERE_HOME"
184
185 # load local config
186 [ -e ${MONKEYSPHERE_CONFIG:="${MONKEYSPHERE_HOME}/monkeysphere.conf"} ] \
187     && . "$MONKEYSPHERE_CONFIG"
188
189 # set empty config variables with ones from the environment
190 GNUPGHOME=${MONKEYSPHERE_GNUPGHOME:=$GNUPGHOME}
191 LOG_LEVEL=${MONKEYSPHERE_LOG_LEVEL:=$LOG_LEVEL}
192 KEYSERVER=${MONKEYSPHERE_KEYSERVER:=$KEYSERVER}
193 # if keyserver not specified in env or conf, then look in gpg.conf
194 if [ -z "$KEYSERVER" ] ; then
195     if [ -f "${GNUPGHOME}/gpg.conf" ] ; then
196         KEYSERVER=$(grep -e "^[[:space:]]*keyserver " "${GNUPGHOME}/gpg.conf" | tail -1 | awk '{ print $2 }')
197     fi
198 fi
199 PROMPT=${MONKEYSPHERE_PROMPT:=$PROMPT}
200 KNOWN_HOSTS=${MONKEYSPHERE_KNOWN_HOSTS:=$KNOWN_HOSTS}
201 HASH_KNOWN_HOSTS=${MONKEYSPHERE_HASH_KNOWN_HOSTS:=$HASH_KNOWN_HOSTS}
202 AUTHORIZED_KEYS=${MONKEYSPHERE_AUTHORIZED_KEYS:=$AUTHORIZED_KEYS}
203 STRICT_MODES=${MONKEYSPHERE_STRICT_MODES:=$STRICT_MODES}
204
205 # other variables not in config file
206 AUTHORIZED_USER_IDS=${MONKEYSPHERE_AUTHORIZED_USER_IDS:="${MONKEYSPHERE_HOME}/authorized_user_ids"}
207 REQUIRED_HOST_KEY_CAPABILITY=${MONKEYSPHERE_REQUIRED_HOST_KEY_CAPABILITY:="a"}
208 REQUIRED_USER_KEY_CAPABILITY=${MONKEYSPHERE_REQUIRED_USER_KEY_CAPABILITY:="a"}
209 # note that only using '=' instead of ':=' tests only if the variable
210 # in unset, not if it's "null"
211 LOG_PREFIX=${MONKEYSPHERE_LOG_PREFIX='ms: '}
212
213 # export GNUPGHOME and make sure gpg home exists with proper
214 # permissions
215 export GNUPGHOME
216 mkdir -p -m 0700 "$GNUPGHOME"
217 export LOG_LEVEL
218 export LOG_PREFIX
219
220 if [ "$#" -eq 0 ] ; then 
221     usage
222     failure "Please supply a subcommand."
223 fi
224
225 # get subcommand
226 COMMAND="$1"
227 shift
228
229 case $COMMAND in
230     'update-known_hosts'|'update-known-hosts'|'k')
231         # whether or not to check keyservers
232         CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:=${CHECK_KEYSERVER:="true"}}
233
234         # if hosts are specified on the command line, process just
235         # those hosts
236         if [ "$1" ] ; then
237             update_known_hosts "$@"
238
239         # otherwise, if no hosts are specified, process every host
240         # in the user's known_hosts file
241         else
242             process_known_hosts
243         fi
244         ;;
245
246     'update-authorized_keys'|'update-authorized-keys'|'a')
247         # whether or not to check keyservers
248         CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:=${CHECK_KEYSERVER:="true"}}
249
250         # process authorized_user_ids file
251         process_authorized_user_ids "$AUTHORIZED_USER_IDS"
252         ;;
253
254     'import-subkey'|'import'|'i')
255         source "${MSHAREDIR}/import_subkey"
256         import_subkey "$@"
257         ;;
258
259     'gen-subkey'|'g')
260         source "${MSHAREDIR}/gen_subkey"
261         gen_subkey "$@"
262         ;;
263
264     'ssh-proxycommand'|'p')
265         source "${MSHAREDIR}/ssh_proxycommand"
266         ssh_proxycommand "$@"
267         ;;
268
269     'subkey-to-ssh-agent'|'s')
270         source "${MSHAREDIR}/subkey_to_ssh_agent"
271         subkey_to_ssh_agent "$@"
272         ;;
273
274     'sshfpr')
275         echo "Warning: 'sshfpr' is deprecated.  Please use 'sshfprs-for-userid' instead." >&2
276         gpg_ssh_fingerprint "$@"
277         ;;
278
279     'keys-for-userid'|'u')
280         CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:=${CHECK_KEYSERVER:="true"}}
281         keys_for_userid "$@"
282         ;;
283
284     'sshfprs-for-userid')
285         CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:=${CHECK_KEYSERVER:="true"}}
286         keytmpdir=$(msmktempdir)
287         # use a file named " " to avoid arbitrary non-whitespace text
288         # in the fingerprint output
289         keytmpfile="$keytmpdir/ "
290         cd "$keytmpdir"
291         keys_for_userid "$@" | while read KEYLINE ; do
292             printf '%s\n' "$KEYLINE" > "$keytmpdir/ "
293             ssh-keygen -l -f ' '
294         done
295         rm -f "$keytmpfile"
296         rmdir "$keytmpdir"
297         ;;
298
299     'keys-from-userid')
300         echo "Warning: 'keys-from-userid' is deprecated.  Please use 'keys-for-userid' instead." >&2
301         CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:=${CHECK_KEYSERVER:="true"}}
302         keys_for_userid "$@"
303         ;;
304
305     'version'|'--version'|'v')
306         version
307         ;;
308
309     'help'|'--help'|'-h'|'h'|'?')
310         usage
311         ;;
312
313     *)
314         failure "Unknown command: '$COMMAND'
315 Try '$PGRM help' for usage."
316         ;;
317 esac