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 userid
39 update-authorized_keys (a) update authorized_keys file
40 gen-ae-subkey (g) KEYID generate an 'ae' capable subkey
46 # generate a subkey with the 'a' and 'e' usage flags set
52 log "warning: this function is still not working."
57 SUBKEY_TYPE=${KEY_TYPE:-"RSA"}
58 SUBKEY_LENGTH=${KEY_LENGTH:-"1024"}
59 SUBKEY_USAGE=${KEY_USAGE:-"encrypt,auth"}
61 gpgOut=$(gpg --fixed-list-mode --list-keys --with-colons \
62 "$keyID" 2> /dev/null)
64 # return 1 if there only "tru" lines are output from gpg
65 if [ -z "$(echo "$gpgOut" | grep -v '^tru:')" ] ; then
70 userID=$(echo "$gpgOut" | grep "^uid:" | cut -d: -f10)
73 keyParameters=$(cat <<EOF
74 Subkey-Type: $SUBKEY_TYPE
75 Subkey-Length: $SUBKEY_LENGTH
76 Subkey-Usage: $SUBKEY_USAGE
81 echo "The following key parameters will be used:"
84 read -p "generate key? [Y|n]: " OK; OK=${OK:=Y}
85 if [ ${OK/y/Y} != 'Y' ] ; then
90 keyParameters="${keyParameters}"$(cat <<EOF
97 echo "generating subkey..."
98 echo "$keyParameters" | gpg --batch --gen-key
101 ########################################################################
103 ########################################################################
106 [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
109 # set ms home directory
110 MS_HOME=${MS_HOME:-"${HOME}/.config/monkeysphere"}
112 # load configuration file
113 MS_CONF=${MS_CONF:-"${MS_HOME}/monkeysphere.conf"}
114 [ -e "$MS_CONF" ] && . "$MS_CONF"
116 # set empty config variable with defaults
117 AUTHORIZED_USER_IDS=${AUTHORIZED_USER_IDS:-"${MS_HOME}/authorized_user_ids"}
118 GNUPGHOME=${GNUPGHOME:-"${HOME}/.gnupg"}
119 KEYSERVER=${KEYSERVER:-"subkeys.pgp.net"}
120 REQUIRED_KEY_CAPABILITY=${REQUIRED_KEY_CAPABILITY:-"e a"}
121 USER_CONTROLLED_AUTHORIZED_KEYS=${USER_CONTROLLED_AUTHORIZED_KEYS:-"%h/.ssh/authorized_keys"}
122 USER_KNOWN_HOSTS=${USER_KNOWN_HOSTS:-"${HOME}/.ssh/known_hosts"}
123 HASH_KNOWN_HOSTS=${HASH_KNOWN_HOSTS:-"true"}
128 hostKeysCacheDir="${MS_HOME}/host_keys"
129 userKeysCacheDir="${MS_HOME}/user_keys"
130 msAuthorizedKeys="${MS_HOME}/authorized_keys"
132 # make sure gpg home exists with proper permissions
133 mkdir -p -m 0700 "$GNUPGHOME"
135 # make sure the user monkeysphere home directory exists
136 mkdir -p -m 0700 "$MS_HOME"
137 mkdir -p "$hostKeysCacheDir"
138 mkdir -p "$userKeysCacheDir"
139 touch "$AUTHORIZED_USER_IDS"
142 'update-known_hosts'|'update-known-hosts'|'k')
145 # touch the known_hosts file to make sure it exists
146 # ssh-keygen complains if it doesn't exist
147 touch "$USER_KNOWN_HOSTS"
149 # if hosts are specified on the command line, process just
153 process_host "$host" "$hostKeysCacheDir"
156 # otherwise, if no hosts are specified, process every user
157 # in the user's known_hosts file
159 if [ ! -s "$USER_KNOWN_HOSTS" ] ; then
160 failure "known_hosts file '$USER_KNOWN_HOSTS' is empty."
162 log "processing known_hosts file..."
163 process_known_hosts "$USER_KNOWN_HOSTS" "$hostKeysCacheDir"
167 'update-userids'|'u')
168 if [ -z "$1" ] ; then
169 failure "you must specify at least one userid."
172 update_userid "$userID" "$userKeysCacheDir"
176 'update-authorized_keys'|'update-authorized-keys'|'a')
177 MODE='authorized_keys'
179 # fail if the authorized_user_ids file is empty
180 if [ ! -s "$AUTHORIZED_USER_IDS" ] ; then
181 failure "$AUTHORIZED_USER_IDS is empty."
184 # set user-controlled authorized_keys file path
185 userAuthorizedKeys=${USER_CONTROLLED_AUTHORIZED_KEYS/\%h/"$HOME"}
187 # update authorized_keys
188 update_authorized_keys "$msAuthorizedKeys" "$userAuthorizedKeys" "$userKeysCacheDir"
193 if [ -z "$keyID" ] ; then
194 failure "you must specify keyid of primary key."
196 gen_ae_subkey "$keyID"
204 failure "Unknown command: '$COMMAND'
205 Type '$PGRM help' for usage."