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-authorized_keys (a) update authorized_keys file
39 gen-subkey (g) KEYID generate an 'a' capable subkey
45 # generate a subkey with the 'a' usage flags set
46 # FIXME: this needs some tweaking to clean it up
54 gpgOut=$(gpg --quiet --fixed-list-mode --list-keys --with-colons \
55 "$keyID" 2> /dev/null)
57 # fail if there only "tru" lines are output from gpg, which
58 # indicates the key was not found.
59 if [ -z "$(echo "$gpgOut" | grep -v '^tru:')" ] ; then
60 failure "Key ID '$keyID' not found."
63 # fail if multiple pub lines are returned, which means the id given
65 if [ $(echo "$gpgOut" | grep '^pub:' | wc -l) -gt '1' ] ; then
66 failure "Key ID '$keyID' is not unique."
69 # prompt if an authentication subkey already exists
70 if echo "$gpgOut" | egrep "^(pub|sub):" | cut -d: -f 12 | grep -q a ; then
71 echo "An authentication subkey already exists for key '$keyID'."
72 read -p "Are you sure you would like to generate another one? [y|N]: " OK; OK=${OK:N}
73 if [ "${OK/y/Y}" != 'Y' ] ; then
79 SUBKEY_TYPE=${SUBKEY_TYPE:-"RSA"}
80 SUBKEY_LENGTH=${SUBKEY_LENGTH:-}
81 SUBKEY_USAGE=${SUBKEY_USAGE:-"auth"}
82 SUBKEY_EXPIRE=${SUBKEY_EXPIRE:-"0"}
84 Please specify how long the key should be valid.
85 0 = key does not expire
86 <n> = key expires in n days
87 <n>w = key expires in n weeks
88 <n>m = key expires in n months
89 <n>y = key expires in n years
91 read -p "Key is valid for? ($SUBKEY_EXPIRE) " SUBKEY_EXPIRE; SUBKEY_EXPIRE=${SUBKEY_EXPIRE:-"0"}
93 # generate the list of commands that will be passed to edit-key
94 editCommands=$(cat <<EOF
107 log "generating subkey..."
108 echo "$editCommands" | gpg --expert --command-fd 0 --edit-key "$keyID"
112 ########################################################################
114 ########################################################################
117 [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
120 # set ms home directory
121 MS_HOME=${MS_HOME:-"${HOME}/.config/monkeysphere"}
123 # load configuration file
124 MS_CONF=${MS_CONF:-"${MS_HOME}/monkeysphere.conf"}
125 [ -e "$MS_CONF" ] && . "$MS_CONF"
127 # set empty config variable with defaults
128 AUTHORIZED_USER_IDS=${AUTHORIZED_USER_IDS:-"${MS_HOME}/authorized_user_ids"}
129 GNUPGHOME=${GNUPGHOME:-"${HOME}/.gnupg"}
130 KEYSERVER=${KEYSERVER:-"subkeys.pgp.net"}
131 CHECK_KEYSERVER=${CHECK_KEYSERVER:="true"}
132 REQUIRED_HOST_KEY_CAPABILITY=${REQUIRED_HOST_KEY_CAPABILITY:-"a"}
133 REQUIRED_USER_KEY_CAPABILITY=${REQUIRED_USER_KEY_CAPABILITY:-"a"}
134 KNOWN_HOSTS=${KNOWN_HOSTS:-"${HOME}/.ssh/known_hosts"}
135 AUTHORIZED_KEYS=${AUTHORIZED_KEYS:-"${HOME}/.ssh/authorized_keys"}
136 HASH_KNOWN_HOSTS=${HASH_KNOWN_HOSTS:-"true"}
140 # make sure gpg home exists with proper permissions
141 mkdir -p -m 0700 "$GNUPGHOME"
143 # make sure the user monkeysphere home directory exists
144 mkdir -p -m 0700 "$MS_HOME"
145 touch "$AUTHORIZED_USER_IDS"
146 touch "$AUTHORIZED_KEYS"
149 'update-known_hosts'|'update-known-hosts'|'k')
152 # touch the known_hosts file to make sure it exists
153 # ssh-keygen complains if it doesn't exist
156 # if hosts are specified on the command line, process just
159 process_hosts_known_hosts "$@"
161 # otherwise, if no hosts are specified, process every host
162 # in the user's known_hosts file
164 if [ ! -s "$KNOWN_HOSTS" ] ; then
165 failure "known_hosts file '$KNOWN_HOSTS' is empty."
167 log "processing known_hosts file..."
171 log "known_hosts file updated."
174 'update-authorized_keys'|'update-authorized-keys'|'a')
175 MODE='authorized_keys'
177 # fail if the authorized_user_ids file is empty
178 if [ ! -s "$AUTHORIZED_USER_IDS" ] ; then
179 failure "$AUTHORIZED_USER_IDS is empty."
182 # process authorized_user_ids file
183 log "processing authorized_user_ids file..."
184 process_authorized_user_ids "$AUTHORIZED_USER_IDS"
185 log "authorized_keys file updated."
190 if [ -z "$keyID" ] ; then
191 failure "You must specify the key ID of your primary key."
201 failure "Unknown command: '$COMMAND'
202 Type '$PGRM help' for usage."