add proxycommand --no-connect option to monkeysphere usage
[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  version (v)                         show version number
54  help (h,?)                          this help
55
56 EOF
57 }
58
59 # user gpg command to define common options
60 gpg_user() {
61     gpg --no-greeting --quiet --no-tty "$@"
62 }
63
64 # take a secret key ID and check that only zero or one ID is provided,
65 # and that it corresponds to only a single secret key ID
66 check_gpg_sec_key_id() {
67     local gpgSecOut
68
69     case "$#" in
70         0)
71             gpgSecOut=$(gpg_user --fixed-list-mode --list-secret-keys --with-colons 2>/dev/null | egrep '^sec:')
72             ;;
73         1)
74             gpgSecOut=$(gpg_user --fixed-list-mode --list-secret-keys --with-colons "$1" | egrep '^sec:') || failure
75             ;;
76         *)
77             failure "You must specify only a single primary key ID."
78             ;;
79     esac
80
81     # check that only a single secret key was found
82     case $(echo "$gpgSecOut" | grep -c '^sec:') in
83         0)
84             failure "No secret keys found.  Create an OpenPGP key with the following command:
85  gpg --gen-key"
86             ;;
87         1)
88             echo "$gpgSecOut" | cut -d: -f5
89             ;;
90         *)
91             local seckeys=$(echo "$gpgSecOut" | cut -d: -f5)
92             failure "Multiple primary secret keys found:
93 $seckeys
94 Please specify which primary key to use."
95             ;;
96     esac
97 }
98
99 # check that a valid authentication subkey does not already exist
100 check_gpg_authentication_subkey() {
101     local keyID
102     local IFS
103     local line
104     local type
105     local validity
106     local usage
107
108     keyID="$1"
109
110     # check that a valid authentication key does not already exist
111     IFS=$'\n'
112     for line in $(gpg_user --fixed-list-mode --list-keys --with-colons "$keyID") ; do
113         type=$(echo "$line" | cut -d: -f1)
114         validity=$(echo "$line" | cut -d: -f2)
115         usage=$(echo "$line" | cut -d: -f12)
116
117         # look at keys only
118         if [ "$type" != 'pub' -a "$type" != 'sub' ] ; then
119             continue
120         fi
121         # check for authentication capability
122         if ! check_capability "$usage" 'a' ; then
123             continue
124         fi
125         # if authentication key is valid, prompt to continue
126         if [ "$validity" = 'u' ] ; then
127             echo "A valid authentication key already exists for primary key '$keyID'." 1>&2
128             if [ "$PROMPT" = "true" ] ; then
129                 read -p "Are you sure you would like to generate another one? (y/N) " OK; OK=${OK:N}
130                 if [ "${OK/y/Y}" != 'Y' ] ; then
131                     failure "aborting."
132                 fi
133                 break
134             else
135                 failure "aborting."
136             fi
137         fi
138     done
139 }
140
141 ########################################################################
142 # MAIN
143 ########################################################################
144
145 # set unset default variables
146 GNUPGHOME=${GNUPGHOME:="${HOME}/.gnupg"}
147 KNOWN_HOSTS="${HOME}/.ssh/known_hosts"
148 HASH_KNOWN_HOSTS="true"
149 AUTHORIZED_KEYS="${HOME}/.ssh/authorized_keys"
150
151 # unset the check keyserver variable, since that needs to have
152 # different defaults for the different functions
153 unset CHECK_KEYSERVER
154
155 # load global config
156 [ -r "${SYSCONFIGDIR}/monkeysphere.conf" ] \
157     && . "${SYSCONFIGDIR}/monkeysphere.conf"
158
159 # set monkeysphere home directory
160 MONKEYSPHERE_HOME=${MONKEYSPHERE_HOME:="${HOME}/.monkeysphere"}
161 mkdir -p -m 0700 "$MONKEYSPHERE_HOME"
162
163 # load local config
164 [ -e ${MONKEYSPHERE_CONFIG:="${MONKEYSPHERE_HOME}/monkeysphere.conf"} ] \
165     && . "$MONKEYSPHERE_CONFIG"
166
167 # set empty config variables with ones from the environment
168 GNUPGHOME=${MONKEYSPHERE_GNUPGHOME:=$GNUPGHOME}
169 LOG_LEVEL=${MONKEYSPHERE_LOG_LEVEL:=$LOG_LEVEL}
170 KEYSERVER=${MONKEYSPHERE_KEYSERVER:=$KEYSERVER}
171 # if keyserver not specified in env or conf, then look in gpg.conf
172 if [ -z "$KEYSERVER" ] ; then
173     if [ -f "${GNUPGHOME}/gpg.conf" ] ; then
174         KEYSERVER=$(grep -e "^[[:space:]]*keyserver " "${GNUPGHOME}/gpg.conf" | tail -1 | awk '{ print $2 }')
175     fi
176 fi
177 PROMPT=${MONKEYSPHERE_PROMPT:=$PROMPT}
178 KNOWN_HOSTS=${MONKEYSPHERE_KNOWN_HOSTS:=$KNOWN_HOSTS}
179 HASH_KNOWN_HOSTS=${MONKEYSPHERE_HASH_KNOWN_HOSTS:=$HASH_KNOWN_HOSTS}
180 AUTHORIZED_KEYS=${MONKEYSPHERE_AUTHORIZED_KEYS:=$AUTHORIZED_KEYS}
181
182 # other variables not in config file
183 AUTHORIZED_USER_IDS=${MONKEYSPHERE_AUTHORIZED_USER_IDS:="${MONKEYSPHERE_HOME}/authorized_user_ids"}
184 REQUIRED_HOST_KEY_CAPABILITY=${MONKEYSPHERE_REQUIRED_HOST_KEY_CAPABILITY:="a"}
185 REQUIRED_USER_KEY_CAPABILITY=${MONKEYSPHERE_REQUIRED_USER_KEY_CAPABILITY:="a"}
186 # note that only using '=' instead of ':=' tests only if the variable
187 # in unset, not if it's "null"
188 LOG_PREFIX=${MONKEYSPHERE_LOG_PREFIX='ms: '}
189
190 # export GNUPGHOME and make sure gpg home exists with proper
191 # permissions
192 export GNUPGHOME
193 mkdir -p -m 0700 "$GNUPGHOME"
194 export LOG_LEVEL
195
196 # get subcommand
197 COMMAND="$1"
198 [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
199 shift
200
201 case $COMMAND in
202     'update-known_hosts'|'update-known-hosts'|'k')
203         # whether or not to check keyservers
204         CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:=${CHECK_KEYSERVER:="true"}}
205
206         # if hosts are specified on the command line, process just
207         # those hosts
208         if [ "$1" ] ; then
209             update_known_hosts "$@"
210
211         # otherwise, if no hosts are specified, process every host
212         # in the user's known_hosts file
213         else
214             process_known_hosts
215         fi
216         ;;
217
218     'update-authorized_keys'|'update-authorized-keys'|'a')
219         # whether or not to check keyservers
220         CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:=${CHECK_KEYSERVER:="true"}}
221
222         # process authorized_user_ids file
223         process_authorized_user_ids "$AUTHORIZED_USER_IDS"
224         ;;
225
226     'import-subkey'|'i')
227         source "${MSHAREDIR}/import_subkey"
228         import_subkey "$@"
229         ;;
230
231     'gen-subkey'|'g')
232         source "${MSHAREDIR}/gen_subkey"
233         gen_subkey "$@"
234         ;;
235
236     'ssh-proxycommand'|'p')
237         source "${MSHAREDIR}/ssh_proxycommand"
238         ssh_proxycommand "$@"
239         ;;
240
241     'subkey-to-ssh-agent'|'s')
242         source "${MSHAREDIR}/subkey_to_ssh_agent"
243         subkey_to_ssh_agent "$@"
244         ;;
245
246     'version'|'v')
247         version
248         ;;
249
250     '--help'|'help'|'-h'|'h'|'?')
251         usage
252         ;;
253
254     *)
255         failure "Unknown command: '$COMMAND'
256 Type '$PGRM help' for usage."
257         ;;
258 esac