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 --quiet --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 log "generating subkey..."
94 echo "$editCommands" | gpg --expert --command-fd 0 --edit-key "$keyID"
98 ########################################################################
100 ########################################################################
103 [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
106 # set ms home directory
107 MS_HOME=${MS_HOME:-"${HOME}/.config/monkeysphere"}
109 # load configuration file
110 MS_CONF=${MS_CONF:-"${MS_HOME}/monkeysphere.conf"}
111 [ -e "$MS_CONF" ] && . "$MS_CONF"
113 # set empty config variable with defaults
114 AUTHORIZED_USER_IDS=${AUTHORIZED_USER_IDS:-"${MS_HOME}/authorized_user_ids"}
115 GNUPGHOME=${GNUPGHOME:-"${HOME}/.gnupg"}
116 KEYSERVER=${KEYSERVER:-"subkeys.pgp.net"}
117 CHECK_KEYSERVER=${CHECK_KEYSERVER:="true"}
118 REQUIRED_HOST_KEY_CAPABILITY=${REQUIRED_HOST_KEY_CAPABILITY:-"a"}
119 REQUIRED_USER_KEY_CAPABILITY=${REQUIRED_USER_KEY_CAPABILITY:-"a"}
120 KNOWN_HOSTS=${KNOWN_HOSTS:-"${HOME}/.ssh/known_hosts"}
121 AUTHORIZED_KEYS=${AUTHORIZED_KEYS:-"${HOME}/.ssh/authorized_keys"}
122 HASH_KNOWN_HOSTS=${HASH_KNOWN_HOSTS:-"true"}
126 # make sure gpg home exists with proper permissions
127 mkdir -p -m 0700 "$GNUPGHOME"
129 # make sure the user monkeysphere home directory exists
130 mkdir -p -m 0700 "$MS_HOME"
131 touch "$AUTHORIZED_USER_IDS"
132 touch "$AUTHORIZED_KEYS"
135 'update-known_hosts'|'update-known-hosts'|'k')
138 # touch the known_hosts file to make sure it exists
139 # ssh-keygen complains if it doesn't exist
142 # if hosts are specified on the command line, process just
146 process_host_known_hosts "$host"
148 log "known_hosts file updated."
150 # otherwise, if no hosts are specified, process every user
151 # in the user's known_hosts file
153 if [ ! -s "$KNOWN_HOSTS" ] ; then
154 failure "known_hosts file '$KNOWN_HOSTS' is empty."
156 log "processing known_hosts file..."
158 log "known_hosts file updated."
162 'update-userids'|'update-userid'|'u')
163 if [ -z "$1" ] ; then
164 failure "you must specify at least one userid."
167 update_userid "$userID"
169 log "Run the following to update your monkeysphere authorized_keys file:"
170 log "$PGRM update-authorized_keys"
173 'remove-userids'|'remove-userid'|'r')
174 if [ -z "$1" ] ; then
175 failure "you must specify at least one userid."
178 remove_userid "$userID"
180 log "Run the following to update your monkeysphere authorized_keys file:"
181 log "$PGRM update-authorized_keys"
184 'update-authorized_keys'|'update-authorized-keys'|'a')
185 MODE='authorized_keys'
187 # fail if the authorized_user_ids file is empty
188 if [ ! -s "$AUTHORIZED_USER_IDS" ] ; then
189 failure "$AUTHORIZED_USER_IDS is empty."
192 # process authorized_user_ids file
193 log "processing authorized_user_ids file..."
194 process_authorized_user_ids
195 log "authorized_keys file updated."
200 if [ -z "$keyID" ] ; then
201 failure "You must specify the key ID of your primary key."
211 failure "Unknown command: '$COMMAND'
212 Type '$PGRM help' for usage."