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
27 ########################################################################
29 ########################################################################
33 usage: $PGRM <subcommand> [args]
34 MonkeySphere client tool.
37 update-known_hosts (k) [HOST]... update known_hosts file
38 update-userids (u) [USERID]... add/update user IDs
39 remove-userids (r) [USERID]... remove user IDs
40 update-authorized_keys (a) update authorized_keys file
41 gen-ae-subkey (g) KEYID generate an 'ae' capable subkey
47 # generate a subkey with the 'a' and 'e' usage flags set
53 log "warning: this function is still not working."
58 SUBKEY_TYPE=${KEY_TYPE:-"RSA"}
59 SUBKEY_LENGTH=${KEY_LENGTH:-"1024"}
60 SUBKEY_USAGE=${KEY_USAGE:-"encrypt,auth"}
62 gpgOut=$(gpg --fixed-list-mode --list-keys --with-colons \
63 "$keyID" 2> /dev/null)
65 # return 1 if there only "tru" lines are output from gpg
66 if [ -z "$(echo "$gpgOut" | grep -v '^tru:')" ] ; then
71 userID=$(echo "$gpgOut" | grep "^uid:" | cut -d: -f10)
74 keyParameters=$(cat <<EOF
75 Subkey-Type: $SUBKEY_TYPE
76 Subkey-Length: $SUBKEY_LENGTH
77 Subkey-Usage: $SUBKEY_USAGE
82 echo "The following key parameters will be used:"
85 read -p "generate key? [Y|n]: " OK; OK=${OK:=Y}
86 if [ ${OK/y/Y} != 'Y' ] ; then
91 keyParameters="${keyParameters}"$(cat <<EOF
98 echo "generating subkey..."
99 echo "$keyParameters" | gpg --batch --gen-key
102 ########################################################################
104 ########################################################################
107 [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
110 # set ms home directory
111 MS_HOME=${MS_HOME:-"${HOME}/.config/monkeysphere"}
113 # load configuration file
114 MS_CONF=${MS_CONF:-"${MS_HOME}/monkeysphere.conf"}
115 [ -e "$MS_CONF" ] && . "$MS_CONF"
117 # set empty config variable with defaults
118 AUTHORIZED_USER_IDS=${AUTHORIZED_USER_IDS:-"${MS_HOME}/authorized_user_ids"}
119 GNUPGHOME=${GNUPGHOME:-"${HOME}/.gnupg"}
120 KEYSERVER=${KEYSERVER:-"subkeys.pgp.net"}
121 REQUIRED_KEY_CAPABILITY=${REQUIRED_KEY_CAPABILITY:-"e a"}
122 USER_CONTROLLED_AUTHORIZED_KEYS=${USER_CONTROLLED_AUTHORIZED_KEYS:-"%h/.ssh/authorized_keys"}
123 USER_KNOWN_HOSTS=${USER_KNOWN_HOSTS:-"${HOME}/.ssh/known_hosts"}
124 HASH_KNOWN_HOSTS=${HASH_KNOWN_HOSTS:-"true"}
129 hostKeysCacheDir="${MS_HOME}/host_keys"
130 userKeysCacheDir="${MS_HOME}/user_keys"
131 msAuthorizedKeys="${MS_HOME}/authorized_keys"
133 # make sure gpg home exists with proper permissions
134 mkdir -p -m 0700 "$GNUPGHOME"
136 # make sure the user monkeysphere home directory exists
137 mkdir -p -m 0700 "$MS_HOME"
138 mkdir -p "$hostKeysCacheDir"
139 mkdir -p "$userKeysCacheDir"
140 touch "$AUTHORIZED_USER_IDS"
143 'update-known_hosts'|'update-known-hosts'|'k')
146 # touch the known_hosts file to make sure it exists
147 # ssh-keygen complains if it doesn't exist
148 touch "$USER_KNOWN_HOSTS"
150 # if hosts are specified on the command line, process just
154 process_host "$host" "$hostKeysCacheDir"
157 # otherwise, if no hosts are specified, process every user
158 # in the user's known_hosts file
160 if [ ! -s "$USER_KNOWN_HOSTS" ] ; then
161 failure "known_hosts file '$USER_KNOWN_HOSTS' is empty."
163 log "processing known_hosts file..."
164 process_known_hosts "$USER_KNOWN_HOSTS" "$hostKeysCacheDir"
168 'update-userids'|'update-userid'|'u')
169 if [ -z "$1" ] ; then
170 failure "you must specify at least one userid."
173 update_userid "$userID" "$userKeysCacheDir"
175 log "run the following to update your monkeysphere authorized_keys file:"
176 log "$PGRM update-authorized_keys"
179 'remove-userids'|'remove-userid'|'r')
180 if [ -z "$1" ] ; then
181 failure "you must specify at least one userid."
184 remove_userid "$userID"
186 log "run the following to update your monkeysphere authorized_keys file:"
187 log "$PGRM update-authorized_keys"
190 'update-authorized_keys'|'update-authorized-keys'|'a')
191 MODE='authorized_keys'
193 # fail if the authorized_user_ids file is empty
194 if [ ! -s "$AUTHORIZED_USER_IDS" ] ; then
195 failure "$AUTHORIZED_USER_IDS is empty."
198 # set user-controlled authorized_keys file path
199 userAuthorizedKeys=${USER_CONTROLLED_AUTHORIZED_KEYS/\%h/"$HOME"}
201 # update authorized_keys
202 update_authorized_keys "$msAuthorizedKeys" "$userAuthorizedKeys" "$userKeysCacheDir"
207 if [ -z "$keyID" ] ; then
208 failure "you must specify keyid of primary key."
210 gen_ae_subkey "$keyID"
218 failure "Unknown command: '$COMMAND'
219 Type '$PGRM help' for usage."