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