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_HOST_KEY_CAPABILITY=${REQUIRED_HOST_KEY_CAPABILITY:-"e a"}
122 REQUIRED_USER_KEY_CAPABILITY=${REQUIRED_USER_KEY_CAPABILITY:-"a"}
123 USER_CONTROLLED_AUTHORIZED_KEYS=${USER_CONTROLLED_AUTHORIZED_KEYS:-"%h/.ssh/authorized_keys"}
124 USER_KNOWN_HOSTS=${USER_KNOWN_HOSTS:-"${HOME}/.ssh/known_hosts"}
125 HASH_KNOWN_HOSTS=${HASH_KNOWN_HOSTS:-"true"}
130 hostKeysCacheDir="${MS_HOME}/host_keys"
131 userKeysCacheDir="${MS_HOME}/user_keys"
132 msAuthorizedKeys="${MS_HOME}/authorized_keys"
134 # make sure gpg home exists with proper permissions
135 mkdir -p -m 0700 "$GNUPGHOME"
137 # make sure the user monkeysphere home directory exists
138 mkdir -p -m 0700 "$MS_HOME"
139 mkdir -p "$hostKeysCacheDir"
140 mkdir -p "$userKeysCacheDir"
141 touch "$AUTHORIZED_USER_IDS"
144 'update-known_hosts'|'update-known-hosts'|'k')
147 # touch the known_hosts file to make sure it exists
148 # ssh-keygen complains if it doesn't exist
149 touch "$USER_KNOWN_HOSTS"
151 # if hosts are specified on the command line, process just
155 process_host "$host" "$hostKeysCacheDir"
158 # otherwise, if no hosts are specified, process every user
159 # in the user's known_hosts file
161 if [ ! -s "$USER_KNOWN_HOSTS" ] ; then
162 failure "known_hosts file '$USER_KNOWN_HOSTS' is empty."
164 log "processing known_hosts file..."
165 process_known_hosts "$USER_KNOWN_HOSTS" "$hostKeysCacheDir"
169 'update-userids'|'update-userid'|'u')
170 if [ -z "$1" ] ; then
171 failure "you must specify at least one userid."
174 update_userid "$userID" "$userKeysCacheDir"
176 log "run the following to update your monkeysphere authorized_keys file:"
177 log "$PGRM update-authorized_keys"
180 'remove-userids'|'remove-userid'|'r')
181 if [ -z "$1" ] ; then
182 failure "you must specify at least one userid."
185 remove_userid "$userID"
187 log "run the following to update your monkeysphere authorized_keys file:"
188 log "$PGRM update-authorized_keys"
191 'update-authorized_keys'|'update-authorized-keys'|'a')
192 MODE='authorized_keys'
194 # fail if the authorized_user_ids file is empty
195 if [ ! -s "$AUTHORIZED_USER_IDS" ] ; then
196 failure "$AUTHORIZED_USER_IDS is empty."
199 # set user-controlled authorized_keys file path
200 userAuthorizedKeys=${USER_CONTROLLED_AUTHORIZED_KEYS/\%h/"$HOME"}
202 # update authorized_keys
203 update_authorized_keys "$msAuthorizedKeys" "$userAuthorizedKeys" "$userKeysCacheDir"
208 if [ -z "$keyID" ] ; then
209 failure "you must specify keyid of primary key."
211 gen_ae_subkey "$keyID"
219 failure "Unknown command: '$COMMAND'
220 Type '$PGRM help' for usage."