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 CHECK_KEYSERVER=${CHECK_KEYSERVER:="true"}
117 REQUIRED_HOST_KEY_CAPABILITY=${REQUIRED_HOST_KEY_CAPABILITY:-"e a"}
118 REQUIRED_USER_KEY_CAPABILITY=${REQUIRED_USER_KEY_CAPABILITY:-"a"}
119 USER_CONTROLLED_AUTHORIZED_KEYS=${USER_CONTROLLED_AUTHORIZED_KEYS:-"${HOME}/.ssh/authorized_keys"}
120 USER_KNOWN_HOSTS=${USER_KNOWN_HOSTS:-"${HOME}/.ssh/known_hosts"}
121 HASH_KNOWN_HOSTS=${HASH_KNOWN_HOSTS:-"true"}
126 hostKeysCacheDir="${MS_HOME}/host_keys"
127 userKeysCacheDir="${MS_HOME}/user_keys"
128 msAuthorizedKeys="${MS_HOME}/authorized_keys"
130 # make sure gpg home exists with proper permissions
131 mkdir -p -m 0700 "$GNUPGHOME"
133 # make sure the user monkeysphere home directory exists
134 mkdir -p -m 0700 "$MS_HOME"
135 mkdir -p "$hostKeysCacheDir"
136 mkdir -p "$userKeysCacheDir"
137 touch "$AUTHORIZED_USER_IDS"
140 'update-known_hosts'|'update-known-hosts'|'k')
143 # touch the known_hosts file to make sure it exists
144 # ssh-keygen complains if it doesn't exist
145 touch "$USER_KNOWN_HOSTS"
147 # if hosts are specified on the command line, process just
151 process_host "$host" "$hostKeysCacheDir"
154 # otherwise, if no hosts are specified, process every user
155 # in the user's known_hosts file
157 if [ ! -s "$USER_KNOWN_HOSTS" ] ; then
158 failure "known_hosts file '$USER_KNOWN_HOSTS' is empty."
160 log "processing known_hosts file..."
161 process_known_hosts "$hostKeysCacheDir"
165 'update-userids'|'update-userid'|'u')
166 if [ -z "$1" ] ; then
167 failure "you must specify at least one userid."
170 update_userid "$userID" "$userKeysCacheDir"
172 log "Run the following to update your monkeysphere authorized_keys file:"
173 log "$PGRM update-authorized_keys"
176 'remove-userids'|'remove-userid'|'r')
177 if [ -z "$1" ] ; then
178 failure "you must specify at least one userid."
181 remove_userid "$userID"
183 log "Run the following to update your monkeysphere authorized_keys file:"
184 log "$PGRM update-authorized_keys"
187 'update-authorized_keys'|'update-authorized-keys'|'a')
188 MODE='authorized_keys'
190 # fail if the authorized_user_ids file is empty
191 if [ ! -s "$AUTHORIZED_USER_IDS" ] ; then
192 failure "$AUTHORIZED_USER_IDS is empty."
195 # update authorized_keys
196 update_authorized_keys "$msAuthorizedKeys" "$USER_CONTROLLED_AUTHORIZED_KEYS" "$userKeysCacheDir"
201 if [ -z "$keyID" ] ; then
202 failure "You must specify the key ID of your primary key."
212 failure "Unknown command: '$COMMAND'
213 Type '$PGRM help' for usage."