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 SHAREDIR=${SHAREDIR:-"/usr/share/monkeysphere"}
16 . "${SHAREDIR}/common"
18 GLOBAL_CONFIG=${GLOBAL_CONFIG:-"${ETC}/monkeysphere.conf"}
19 [ -r "$GLOBAL_CONFIG" ] && . "$GLOBAL_CONFIG"
21 # date in UTF format if needed
22 DATE=$(date -u '+%FT%T')
24 # unset some environment variables that could screw things up
30 ########################################################################
32 ########################################################################
36 usage: $PGRM <subcommand> [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 'a' capable subkey
48 # generate a subkey with the 'a' usage flags set
49 # FIXME: this needs some tweaking to clean it up
57 gpgOut=$(gpg --quiet --fixed-list-mode --list-keys --with-colons \
58 "$keyID" 2> /dev/null)
60 # fail if there only "tru" lines are output from gpg, which
61 # indicates the key was not found.
62 if [ -z "$(echo "$gpgOut" | grep -v '^tru:')" ] ; then
63 failure "Key ID '$keyID' not found."
66 # fail if multiple pub lines are returned, which means the id given
68 if [ $(echo "$gpgOut" | grep '^pub:' | wc -l) -gt '1' ] ; then
69 failure "Key ID '$keyID' is not unique."
72 # prompt if an authentication subkey already exists
73 if echo "$gpgOut" | egrep "^(pub|sub):" | cut -d: -f 12 | grep -q a ; then
74 echo "An authentication subkey already exists for key '$keyID'."
75 read -p "Are you sure you would like to generate another one? [y|N]: " OK; OK=${OK:N}
76 if [ "${OK/y/Y}" != 'Y' ] ; then
82 SUBKEY_TYPE=${SUBKEY_TYPE:-"RSA"}
83 SUBKEY_LENGTH=${SUBKEY_LENGTH:-}
84 SUBKEY_USAGE=${SUBKEY_USAGE:-"auth"}
85 SUBKEY_EXPIRE=${SUBKEY_EXPIRE:-"0"}
87 Please specify how long the key should be valid.
88 0 = key does not expire
89 <n> = key expires in n days
90 <n>w = key expires in n weeks
91 <n>m = key expires in n months
92 <n>y = key expires in n years
94 read -p "Key is valid for? ($SUBKEY_EXPIRE) " SUBKEY_EXPIRE; SUBKEY_EXPIRE=${SUBKEY_EXPIRE:-"0"}
96 # generate the list of commands that will be passed to edit-key
97 editCommands=$(cat <<EOF
110 log "generating subkey..."
111 echo "$editCommands" | gpg --expert --command-fd 0 --edit-key "$keyID"
115 ########################################################################
117 ########################################################################
120 [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
123 # set ms home directory
124 MS_HOME=${MS_HOME:-"${HOME}/.config/monkeysphere"}
126 # load configuration file
127 MS_CONF=${MS_CONF:-"${MS_HOME}/monkeysphere.conf"}
128 [ -e "$MS_CONF" ] && . "$MS_CONF"
130 # set empty config variable with defaults
131 GNUPGHOME=${GNUPGHOME:-"${HOME}/.gnupg"}
132 KEYSERVER=${KEYSERVER:-"subkeys.pgp.net"}
133 CHECK_KEYSERVER=${CHECK_KEYSERVER:="true"}
134 KNOWN_HOSTS=${KNOWN_HOSTS:-"${HOME}/.ssh/known_hosts"}
135 HASH_KNOWN_HOSTS=${HASH_KNOWN_HOSTS:-"true"}
136 AUTHORIZED_KEYS=${AUTHORIZED_KEYS:-"${HOME}/.ssh/authorized_keys"}
139 AUTHORIZED_USER_IDS=${AUTHORIZED_USER_IDS:-"${MS_HOME}/authorized_user_ids"}
140 REQUIRED_HOST_KEY_CAPABILITY=${REQUIRED_HOST_KEY_CAPABILITY:-"a"}
141 REQUIRED_USER_KEY_CAPABILITY=${REQUIRED_USER_KEY_CAPABILITY:-"a"}
145 # make sure gpg home exists with proper permissions
146 mkdir -p -m 0700 "$GNUPGHOME"
148 # make sure the user monkeysphere home directory exists
149 mkdir -p -m 0700 "$MS_HOME"
150 touch "$AUTHORIZED_USER_IDS"
153 'update-known_hosts'|'update-known-hosts'|'k')
156 # touch the known_hosts file to make sure it exists
157 # ssh-keygen complains if it doesn't exist
160 # if hosts are specified on the command line, process just
163 update_known_hosts "$@" || ERR=1
165 # otherwise, if no hosts are specified, process every host
166 # in the user's known_hosts file
168 if [ ! -s "$KNOWN_HOSTS" ] ; then
169 failure "known_hosts file '$KNOWN_HOSTS' is empty."
171 process_known_hosts || ERR=1
174 log "known_hosts file updated."
177 'update-authorized_keys'|'update-authorized-keys'|'a')
178 MODE='authorized_keys'
180 # fail if the authorized_user_ids file is empty
181 if [ ! -s "$AUTHORIZED_USER_IDS" ] ; then
182 failure "$AUTHORIZED_USER_IDS is empty."
185 # process authorized_user_ids file
186 process_authorized_user_ids "$AUTHORIZED_USER_IDS" || ERR=1
187 log "authorized_keys file updated."
192 if [ -z "$keyID" ] ; then
193 failure "You must specify the key ID of your primary key."
203 failure "Unknown command: '$COMMAND'
204 Type '$PGRM help' for usage."