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-subkey (g) KEYID generate an 'a' capable subkey
47 # generate a subkey with the 'a' usage flags set
48 # FIXME: this needs some tweaking to clean it up
56 gpgOut=$(gpg --fixed-list-mode --list-keys --with-colons \
57 "$keyID" 2> /dev/null)
59 # return 1 if there only "tru" lines are output from gpg
60 if [ -z "$(echo "$gpgOut" | grep -v '^tru:')" ] ; then
61 failure "Key ID '$keyID' not found."
65 SUBKEY_TYPE=${SUBKEY_TYPE:-"RSA"}
66 #SUBKEY_LENGTH=${SUBKEY_LENGTH:-"2048"}
67 SUBKEY_USAGE=${SUBKEY_USAGE:-"auth"}
68 SUBKEY_EXPIRE=${SUBKEY_EXPIRE:-"0"}
70 Please specify how long the key should be valid.
71 0 = key does not expire
72 <n> = key expires in n days
73 <n>w = key expires in n weeks
74 <n>m = key expires in n months
75 <n>y = key expires in n years
77 read -p "Key is valid for? ($SUBKEY_EXPIRE) " SUBKEY_EXPIRE; SUBKEY_EXPIRE=${SUBKEY_EXPIRE:-"0"}
79 # generate the list of commands that will be passed to edit-key
80 editCommands=$(cat <<EOF
93 echo "generating subkey..."
94 echo "$editCommands" | gpg --expert --command-fd 0 --edit-key "$keyID"
97 ########################################################################
99 ########################################################################
102 [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
105 # set ms home directory
106 MS_HOME=${MS_HOME:-"${HOME}/.config/monkeysphere"}
108 # load configuration file
109 MS_CONF=${MS_CONF:-"${MS_HOME}/monkeysphere.conf"}
110 [ -e "$MS_CONF" ] && . "$MS_CONF"
112 # set empty config variable with defaults
113 AUTHORIZED_USER_IDS=${AUTHORIZED_USER_IDS:-"${MS_HOME}/authorized_user_ids"}
114 GNUPGHOME=${GNUPGHOME:-"${HOME}/.gnupg"}
115 KEYSERVER=${KEYSERVER:-"subkeys.pgp.net"}
116 REQUIRED_HOST_KEY_CAPABILITY=${REQUIRED_HOST_KEY_CAPABILITY:-"e a"}
117 REQUIRED_USER_KEY_CAPABILITY=${REQUIRED_USER_KEY_CAPABILITY:-"a"}
118 USER_CONTROLLED_AUTHORIZED_KEYS=${USER_CONTROLLED_AUTHORIZED_KEYS:-"%h/.ssh/authorized_keys"}
119 USER_KNOWN_HOSTS=${USER_KNOWN_HOSTS:-"${HOME}/.ssh/known_hosts"}
120 HASH_KNOWN_HOSTS=${HASH_KNOWN_HOSTS:-"true"}
125 hostKeysCacheDir="${MS_HOME}/host_keys"
126 userKeysCacheDir="${MS_HOME}/user_keys"
127 msAuthorizedKeys="${MS_HOME}/authorized_keys"
129 # make sure gpg home exists with proper permissions
130 mkdir -p -m 0700 "$GNUPGHOME"
132 # make sure the user monkeysphere home directory exists
133 mkdir -p -m 0700 "$MS_HOME"
134 mkdir -p "$hostKeysCacheDir"
135 mkdir -p "$userKeysCacheDir"
136 touch "$AUTHORIZED_USER_IDS"
139 'update-known_hosts'|'update-known-hosts'|'k')
142 # touch the known_hosts file to make sure it exists
143 # ssh-keygen complains if it doesn't exist
144 touch "$USER_KNOWN_HOSTS"
146 # if hosts are specified on the command line, process just
153 # otherwise, if no hosts are specified, process every user
154 # in the user's known_hosts file
156 if [ ! -s "$USER_KNOWN_HOSTS" ] ; then
157 failure "known_hosts file '$USER_KNOWN_HOSTS' is empty."
159 log "processing known_hosts file..."
160 process_known_hosts "$USER_KNOWN_HOSTS"
164 'update-userids'|'update-userid'|'u')
165 if [ -z "$1" ] ; then
166 failure "you must specify at least one userid."
169 update_userid "$userID"
171 log "Run the following to update your monkeysphere authorized_keys file:"
172 log "$PGRM update-authorized_keys"
175 'remove-userids'|'remove-userid'|'r')
176 if [ -z "$1" ] ; then
177 failure "you must specify at least one userid."
180 remove_userid "$userID"
182 log "Run the following to update your monkeysphere authorized_keys file:"
183 log "$PGRM update-authorized_keys"
186 'update-authorized_keys'|'update-authorized-keys'|'a')
187 MODE='authorized_keys'
189 # fail if the authorized_user_ids file is empty
190 if [ ! -s "$AUTHORIZED_USER_IDS" ] ; then
191 failure "$AUTHORIZED_USER_IDS is empty."
194 # set user-controlled authorized_keys file path
195 userAuthorizedKeys=${USER_CONTROLLED_AUTHORIZED_KEYS/\%h/"$HOME"}
197 # update authorized_keys
198 update_authorized_keys "$msAuthorizedKeys" "$userAuthorizedKeys" "$userKeysCacheDir"
203 if [ -z "$keyID" ] ; then
204 failure "You must specify the key ID of your primary key."
214 failure "Unknown command: '$COMMAND'
215 Type '$PGRM help' for usage."