3 # monkeysphere: MonkeySphere client tool
5 # The monkeysphere scripts are written by:
6 # Jameson Rollins <jrollins@fifthhorseman.net>
8 # They are Copyright 2008, and are all released under the GPL, version 3
11 ########################################################################
14 SHARE=${MONKEYSPHERE_SHARE:-"/usr/share/monkeysphere"}
16 . "${SHARE}/common" || exit 1
18 # date in UTF format if needed
19 DATE=$(date -u '+%FT%T')
21 # unset some environment variables that could screw things up
27 # set the file creation mask to be only owner rw
30 ########################################################################
32 ########################################################################
36 usage: $PGRM <subcommand> [options] [args]
37 MonkeySphere client tool.
40 update-known_hosts (k) [HOST]... update known_hosts file
41 update-authorized_keys (a) update authorized_keys file
42 gen-subkey (g) [KEYID] generate an authentication subkey
43 --length (-l) BITS key length in bits (2048)
44 --expire (-e) EXPIRE date to expire
50 # generate a subkey with the 'a' usage flags set
58 # set default key parameter values
63 TEMP=$(getopt -o l:e: -l length:,expire: -n "$PGRM" -- "$@")
69 # Note the quotes around `$TEMP': they are essential!
92 # find all secret keys
93 keyID=$(gpg --with-colons --list-secret-keys | grep ^sec | cut -f5 -d:)
94 # if multiple sec keys exist, fail
95 if (( $(echo "$keyID" | wc -l) > 1 )) ; then
96 echo "Multiple secret keys found:"
98 failure "Please specify which primary key to use."
103 if [ -z "$keyID" ] ; then
104 failure "You have no secret key available. You should create an OpenPGP
105 key before joining the monkeysphere. You can do this with:
109 # get key output, and fail if not found
110 gpgOut=$(gpg --quiet --fixed-list-mode --list-secret-keys --with-colons \
113 # fail if multiple sec lines are returned, which means the id
114 # given is not unique
115 if [ $(echo "$gpgOut" | grep '^sec:' | wc -l) -gt '1' ] ; then
116 failure "Key ID '$keyID' is not unique."
119 # prompt if an authentication subkey already exists
120 if echo "$gpgOut" | egrep "^(sec|ssb):" | cut -d: -f 12 | grep -q a ; then
121 echo "An authentication subkey already exists for key '$keyID'."
122 read -p "Are you sure you would like to generate another one? (y/N) " OK; OK=${OK:N}
123 if [ "${OK/y/Y}" != 'Y' ] ; then
128 # set subkey defaults
129 # prompt about key expiration if not specified
130 if [ -z "$keyExpire" ] ; then
132 Please specify how long the key should be valid.
133 0 = key does not expire
134 <n> = key expires in n days
135 <n>w = key expires in n weeks
136 <n>m = key expires in n months
137 <n>y = key expires in n years
139 while [ -z "$keyExpire" ] ; do
140 read -p "Key is valid for? (0) " keyExpire
141 if ! test_gpg_expire ${keyExpire:=0} ; then
146 elif ! test_gpg_expire "$keyExpire" ; then
147 failure "invalid key expiration value '$keyExpire'."
150 # generate the list of commands that will be passed to edit-key
151 editCommands=$(cat <<EOF
164 log "generating subkey..."
166 (umask 077 && mkfifo "$fifoDir/pass")
167 echo "$editCommands" | gpg --passphrase-fd 3 3< "$fifoDir/pass" --expert --command-fd 0 --edit-key "$keyID" &
169 if [ "$DISPLAY" ] && which ssh-askpass >/dev/null; then
170 ssh-askpass "Please enter your passphrase for $keyID: " > "$fifoDir/pass"
172 read -s -p "Please enter your passphrase for $keyID: " PASS
173 echo "$PASS" > "$fifoDir/pass"
180 ########################################################################
182 ########################################################################
184 # unset variables that should be defined only in config file
186 unset CHECK_KEYSERVER
188 unset HASH_KNOWN_HOSTS
189 unset AUTHORIZED_KEYS
192 [ -r "${ETC}/monkeysphere.conf" ] && . "${ETC}/monkeysphere.conf"
194 # set monkeysphere home directory
195 MONKEYSPHERE_HOME=${MONKEYSPHERE_HOME:="${HOME}/.config/monkeysphere"}
196 mkdir -p -m 0700 "$MONKEYSPHERE_HOME"
199 [ -e ${MONKEYSPHERE_CONFIG:="${MONKEYSPHERE_HOME}/monkeysphere.conf"} ] && . "$MONKEYSPHERE_CONFIG"
201 # set empty config variables with ones from the environment, or from
202 # config file, or with defaults
203 GNUPGHOME=${MONKEYSPHERE_GNUPGHOME:=${GNUPGHOME:="${HOME}/.gnupg"}}
204 KEYSERVER=${MONKEYSPHERE_KEYSERVER:="$KEYSERVER"}
205 # if keyserver not specified in env or monkeysphere.conf,
207 if [ -z "$KEYSERVER" ] ; then
208 if [ -f "${GNUPGHOME}/gpg.conf" ] ; then
209 KEYSERVER=$(grep -e "^[[:space:]]*keyserver " "${GNUPGHOME}/gpg.conf" | tail -1 | awk '{ print $2 }')
212 # if it's still not specified, use the default
213 KEYSERVER=${KEYSERVER:="subkeys.pgp.net"}
214 CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:=${CHECK_KEYSERVER:="true"}}
215 KNOWN_HOSTS=${MONKEYSPHERE_KNOWN_HOSTS:=${KNOWN_HOSTS:="${HOME}/.ssh/known_hosts"}}
216 HASH_KNOWN_HOSTS=${MONKEYSPHERE_HASH_KNOWN_HOSTS:=${HASH_KNOWN_HOSTS:="true"}}
217 AUTHORIZED_KEYS=${MONKEYSPHERE_AUTHORIZED_KEYS:=${AUTHORIZED_KEYS:="${HOME}/.ssh/authorized_keys"}}
219 # other variables not in config file
220 AUTHORIZED_USER_IDS=${MONKEYSPHERE_AUTHORIZED_USER_IDS:="${MONKEYSPHERE_HOME}/authorized_user_ids"}
221 REQUIRED_HOST_KEY_CAPABILITY=${MONKEYSPHERE_REQUIRED_HOST_KEY_CAPABILITY:="a"}
222 REQUIRED_USER_KEY_CAPABILITY=${MONKEYSPHERE_REQUIRED_USER_KEY_CAPABILITY:="a"}
224 # export GNUPGHOME and make sure gpg home exists with proper
227 mkdir -p -m 0700 "$GNUPGHOME"
231 [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
235 'update-known_hosts'|'update-known-hosts'|'k')
238 # check permissions on the known_hosts file path
239 if ! check_key_file_permissions "$USER" "$KNOWN_HOSTS" ; then
240 failure "Improper permissions on known_hosts file path."
243 # if hosts are specified on the command line, process just
246 update_known_hosts "$@"
249 # otherwise, if no hosts are specified, process every host
250 # in the user's known_hosts file
252 # exit if the known_hosts file does not exist
253 if [ ! -e "$KNOWN_HOSTS" ] ; then
254 log "known_hosts file '$KNOWN_HOSTS' does not exist."
263 'update-authorized_keys'|'update-authorized-keys'|'a')
264 MODE='authorized_keys'
266 # check permissions on the authorized_user_ids file path
267 if ! check_key_file_permissions "$USER" "$AUTHORIZED_USER_IDS" ; then
268 failure "Improper permissions on authorized_user_ids file path."
271 # check permissions on the authorized_keys file path
272 if ! check_key_file_permissions "$USER" "$AUTHORIZED_KEYS" ; then
273 failure "Improper permissions on authorized_keys file path."
276 # exit if the authorized_user_ids file is empty
277 if [ ! -e "$AUTHORIZED_USER_IDS" ] ; then
278 log "authorized_user_ids file '$AUTHORIZED_USER_IDS' does not exist."
282 # process authorized_user_ids file
283 process_authorized_user_ids "$AUTHORIZED_USER_IDS"
291 '--help'|'help'|'-h'|'h'|'?')
296 failure "Unknown command: '$COMMAND'
297 Type '$PGRM help' for usage."