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: not working yet.
54 log "warning: this function is still not working."
58 gpgOut=$(gpg --fixed-list-mode --list-keys --with-colons \
59 "$keyID" 2> /dev/null)
61 # return 1 if there only "tru" lines are output from gpg
62 if [ -z "$(echo "$gpgOut" | grep -v '^tru:')" ] ; then
68 SUBKEY_TYPE=${SUBKEY_TYPE:-"RSA"}
69 #SUBKEY_LENGTH=${SUBKEY_LENGTH:-"2048"}
70 SUBKEY_USAGE=${SUBKEY_USAGE:-"auth"}
71 SUBKEY_EXPIRE=${SUBKEY_EXPIRE:-"0"}
73 Please specify how long the key should be valid.
74 0 = key does not expire
75 <n> = key expires in n days
76 <n>w = key expires in n weeks
77 <n>m = key expires in n months
78 <n>y = key expires in n years
80 read -p "Key is valid for? ($SUBKEY_EXPIRE) " SUBKEY_EXPIRE; SUBKEY_EXPIRE=${SUBKEY_EXPIRE:-"0"}
82 # generate the list of commands that will be passed to edit-key
83 editCommands=$(cat <<EOF
96 echo "generating subkey..."
97 echo "$editCommands" | gpg --expert --command-fd 0 --edit-key "$keyID"
100 ########################################################################
102 ########################################################################
105 [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
108 # set ms home directory
109 MS_HOME=${MS_HOME:-"${HOME}/.config/monkeysphere"}
111 # load configuration file
112 MS_CONF=${MS_CONF:-"${MS_HOME}/monkeysphere.conf"}
113 [ -e "$MS_CONF" ] && . "$MS_CONF"
115 # set empty config variable with defaults
116 AUTHORIZED_USER_IDS=${AUTHORIZED_USER_IDS:-"${MS_HOME}/authorized_user_ids"}
117 GNUPGHOME=${GNUPGHOME:-"${HOME}/.gnupg"}
118 KEYSERVER=${KEYSERVER:-"subkeys.pgp.net"}
119 REQUIRED_HOST_KEY_CAPABILITY=${REQUIRED_HOST_KEY_CAPABILITY:-"e a"}
120 REQUIRED_USER_KEY_CAPABILITY=${REQUIRED_USER_KEY_CAPABILITY:-"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'|'update-userid'|'u')
168 if [ -z "$1" ] ; then
169 failure "you must specify at least one userid."
172 update_userid "$userID" "$userKeysCacheDir"
174 log "run the following to update your monkeysphere authorized_keys file:"
175 log "$PGRM update-authorized_keys"
178 'remove-userids'|'remove-userid'|'r')
179 if [ -z "$1" ] ; then
180 failure "you must specify at least one userid."
183 remove_userid "$userID"
185 log "run the following to update your monkeysphere authorized_keys file:"
186 log "$PGRM update-authorized_keys"
189 'update-authorized_keys'|'update-authorized-keys'|'a')
190 MODE='authorized_keys'
192 # fail if the authorized_user_ids file is empty
193 if [ ! -s "$AUTHORIZED_USER_IDS" ] ; then
194 failure "$AUTHORIZED_USER_IDS is empty."
197 # set user-controlled authorized_keys file path
198 userAuthorizedKeys=${USER_CONTROLLED_AUTHORIZED_KEYS/\%h/"$HOME"}
200 # update authorized_keys
201 update_authorized_keys "$msAuthorizedKeys" "$userAuthorizedKeys" "$userKeysCacheDir"
206 if [ -z "$keyID" ] ; then
207 failure "you must specify keyid of primary key."
217 failure "Unknown command: '$COMMAND'
218 Type '$PGRM help' for usage."