3 # monkeysphere-server: MonkeySphere server admin 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 SHARE=${SHARE:-"/usr/share/monkeysphere"}
16 . "${SHARE}/common" || exit 1
18 VARLIB="/var/lib/monkeysphere"
21 # date in UTF format if needed
22 DATE=$(date -u '+%FT%T')
24 # unset some environment variables that could screw things up
30 ########################################################################
32 ########################################################################
36 usage: $PGRM <subcommand> [args]
37 MonkeySphere server admin tool.
40 update-users (u) [USER]... update users authorized_keys files
41 gen-key (g) [HOSTNAME] generate gpg key for the server
42 show-fingerprint (f) show server's host key fingerprint
43 publish-key (p) publish server's host key to keyserver
44 trust-key (t) KEYID import and tsign a certification key
50 su_monkeysphere_user() {
51 su --preserve-environment "$MONKEYSPHERE_USER" -- -c "$@"
54 # function to interact with the host gnupg keyring
58 GNUPGHOME="$GNUPGHOME_HOST"
61 # NOTE: we supress this warning because we need the monkeysphere
62 # user to be able to read the host pubring. we realize this might
63 # be problematic, but it's the simplest solution, without too much
65 gpg --no-permission-warning "$@"
68 # always reset the permissions on the host pubring so that the
69 # monkeysphere user can read the trust signatures
70 chgrp "$MONKEYSPHERE_USER" "${GNUPGHOME_HOST}/pubring.gpg"
71 chmod g+r "${GNUPGHOME_HOST}/pubring.gpg"
76 # function to interact with the authentication gnupg keyring
77 gpg_authentication() {
78 GNUPGHOME="$GNUPGHOME_AUTHENTICATION"
81 su_monkeysphere_user "gpg $@"
84 # update authorized_keys for users
87 # get users from command line
90 # or just look at all users if none specified
91 unames=$(getent passwd | cut -d: -f1)
95 MODE="authorized_keys"
98 GNUPGHOME="$GNUPGHOME_AUTHENTICATION"
100 # check to see if the gpg trust database has been initialized
101 if [ ! -s "${GNUPGHOME}/trustdb.gpg" ] ; then
102 failure "GNUPG trust database uninitialized. Please see MONKEYSPHERE-SERVER(8)."
105 # make sure the authorized_keys directory exists
106 mkdir -p "${VARLIB}/authorized_keys"
109 for uname in $unames ; do
110 # check all specified users exist
111 if ! getent passwd "$uname" >/dev/null ; then
112 error "----- unknown user '$uname' -----"
116 # set authorized_user_ids and raw authorized_keys variables,
117 # translating ssh-style path variables
118 authorizedUserIDs=$(translate_ssh_variables "$uname" "$AUTHORIZED_USER_IDS")
119 rawAuthorizedKeys=$(translate_ssh_variables "$uname" "$RAW_AUTHORIZED_KEYS")
121 # if neither is found, skip user
122 if [ ! -s "$authorizedUserIDs" ] ; then
123 if [ "$rawAuthorizedKeys" = '-' -o ! -s "$rawAuthorizedKeys" ] ; then
128 log "----- user: $uname -----"
130 # make temporary directory
133 # trap to delete temporary directory on exit
134 trap "rm -rf $TMPDIR" EXIT
136 # create temporary authorized_user_ids file
137 TMP_AUTHORIZED_USER_IDS="${TMPDIR}/authorized_user_ids"
138 touch "$TMP_AUTHORIZED_USER_IDS"
140 # create temporary authorized_keys file
141 AUTHORIZED_KEYS="${TMPDIR}/authorized_keys"
142 touch "$AUTHORIZED_KEYS"
144 # set restrictive permissions on the temporary files
145 # FIXME: is there a better way to do this?
147 chmod 0600 "$AUTHORIZED_KEYS"
148 chmod 0600 "$TMP_AUTHORIZED_USER_IDS"
149 chown -R "$MONKEYSPHERE_USER" "$TMPDIR"
151 # if the authorized_user_ids file exists...
152 if [ -s "$authorizedUserIDs" ] ; then
153 # copy user authorized_user_ids file to temporary
155 cat "$authorizedUserIDs" > "$TMP_AUTHORIZED_USER_IDS"
157 # export needed variables
158 export AUTHORIZED_KEYS
159 export TMP_AUTHORIZED_USER_IDS
161 # process authorized_user_ids file, as monkeysphere
163 su_monkeysphere_user \
164 ". ${SHARE}/common; process_authorized_user_ids $TMP_AUTHORIZED_USER_IDS"
168 # add user-controlled authorized_keys file path if specified
169 if [ "$rawAuthorizedKeys" != '-' -a -s "$rawAuthorizedKeys" ] ; then
170 log -n "adding raw authorized_keys file... "
171 cat "$rawAuthorizedKeys" >> "$AUTHORIZED_KEYS"
175 # openssh appears to check the contents of the
176 # authorized_keys file as the user in question, so the
177 # file must be readable by that user at least.
178 # FIXME: is there a better way to do this?
179 chown root "$AUTHORIZED_KEYS"
180 chgrp $(getent passwd "$uname" | cut -f4 -d:) "$AUTHORIZED_KEYS"
181 chmod g+r "$AUTHORIZED_KEYS"
183 # if the resulting authorized_keys file is not empty, move
185 mv -f "$AUTHORIZED_KEYS" "${VARLIB}/authorized_keys/${uname}"
187 # destroy temporary directory
192 # generate server gpg key
199 hostName=${1:-$(hostname --fqdn)}
201 SERVICE=${SERVICE:-"ssh"}
202 userID="${SERVICE}://${hostName}"
204 if gpg_host --list-key ="$userID" > /dev/null 2>&1 ; then
205 failure "Key for '$userID' already exists"
209 KEY_TYPE=${KEY_TYPE:-"RSA"}
210 KEY_LENGTH=${KEY_LENGTH:-"2048"}
211 KEY_USAGE=${KEY_USAGE:-"auth"}
212 KEY_EXPIRE=${KEY_EXPIRE:-"0"}
214 Please specify how long the key should be valid.
215 0 = key does not expire
216 <n> = key expires in n days
217 <n>w = key expires in n weeks
218 <n>m = key expires in n months
219 <n>y = key expires in n years
221 read -p "Key is valid for? ($KEY_EXPIRE) " KEY_EXPIRE; KEY_EXPIRE=${KEY_EXPIRE:-"0"}
224 keyParameters=$(cat <<EOF
226 Key-Length: $KEY_LENGTH
227 Key-Usage: $KEY_USAGE
229 Expire-Date: $KEY_EXPIRE
233 # add the revoker field if specified
234 # FIXME: the "1:" below assumes that $REVOKER's key is an RSA key. why?
235 # FIXME: why is this marked "sensitive"? how will this signature ever
236 # be transmitted to the expected revoker?
237 if [ "$REVOKER" ] ; then
238 keyParameters="${keyParameters}"$(cat <<EOF
240 Revoker: 1:$REVOKER sensitive
245 echo "The following key parameters will be used for the host private key:"
246 echo "$keyParameters"
248 read -p "Generate key? [Y|n]: " OK; OK=${OK:=Y}
249 if [ ${OK/y/Y} != 'Y' ] ; then
254 keyParameters="${keyParameters}"$(cat <<EOF
261 log "generating server key..."
262 echo "$keyParameters" | gpg_host --batch --gen-key
264 # output the server fingerprint
265 fingerprint_server_key "=${userID}"
267 # find the key fingerprint of the server primary key
268 fingerprint=$(gpg_host --list-key --with-colons --with-fingerprint "=${userID}" | \
269 grep '^fpr:' | head -1 | cut -d: -f10)
271 # translate the private key to ssh format, and export to a file
273 # NOTE: assumes that the primary key is the proper key to use
275 gpg_host --export-secret-key "$fingerprint" | \
276 openpgp2ssh "$fingerprint" > "${VARLIB}/ssh_host_rsa_key")
277 log "Private SSH host key output to file: ${VARLIB}/ssh_host_rsa_key"
280 # gpg output key fingerprint
281 fingerprint_server_key() {
282 gpg_host --fingerprint --list-secret-keys
285 # publish server key to keyserver
286 publish_server_key() {
287 read -p "really publish key to $KEYSERVER? [y|N]: " OK; OK=${OK:=N}
288 if [ ${OK/y/Y} != 'Y' ] ; then
293 # FIXME: need to figure out better way to identify host key
294 # dummy command so as not to publish fakes keys during testing
296 #gpg_authentication "--keyring $GNUPGHOME_HOST/pubring.gpg --keyserver $KEYSERVER --send-keys $(hostname -f)"
297 failure "NOT PUBLISHED (to avoid permanent publication errors during monkeysphere development)."
300 # retrieve key from web of trust, and set owner trust to "full"
308 # default values for trust depth and domain
312 if [ -z "$keyID" ] ; then
313 failure "You must specify key to trust."
318 # export host ownertrust to authentication keyring
319 gpg_host --export-ownertrust | gpg_authentication "--import-ownertrust"
321 # get the key from the key server
322 gpg_authentication "--keyserver $KEYSERVER --recv-key $keyID"
324 # get the full fingerprint of a key ID
325 fingerprint=$(gpg_authentication "--list-key --with-colons --with-fingerprint $keyID" | \
326 grep '^fpr:' | grep "$keyID" | cut -d: -f10)
328 if [ -z "$fingerprint" ] ; then
329 failure "Could not find key '$keyID'."
333 gpg_authentication "--fingerprint $fingerprint"
335 # export the key to the host keyring
336 gpg_authentication "--export $keyID" | gpg_host --import
339 # NOTE: *all* user IDs will be ltsigned
340 ltsignCommand=$(cat <<EOF
352 echo "$ltsignCommand" | gpg_host --quiet --command-fd 0 --edit-key "$fingerprint"
354 # update the trustdb for the authentication keyring
355 gpg_authentication "--check-trustdb"
358 ########################################################################
360 ########################################################################
363 [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
366 # load configuration file
367 MS_CONF=${MS_CONF:-"${ETC}/monkeysphere-server.conf"}
368 [ -e "$MS_CONF" ] && . "$MS_CONF"
370 # set empty config variable with defaults
371 MONKEYSPHERE_USER=${MONKEYSPHERE_USER:-"monkeysphere"}
372 KEYSERVER=${KEYSERVER:-"subkeys.pgp.net"}
373 CHECK_KEYSERVER=${CHECK_KEYSERVER:="true"}
374 AUTHORIZED_USER_IDS=${AUTHORIZED_USER_IDS:-"%h/.config/monkeysphere/authorized_user_ids"}
375 RAW_AUTHORIZED_KEYS=${RAW_AUTHORIZED_KEYS:-"%h/.ssh/authorized_keys"}
378 REQUIRED_USER_KEY_CAPABILITY=${REQUIRED_USER_KEY_CAPABILITY:-"a"}
379 GNUPGHOME_HOST=${GNUPGHOME_HOST:-"${VARLIB}/gnupg-host"}
380 GNUPGHOME_AUTHENTICATION=${GNUPGHOME_AUTHENTICATION:-"${VARLIB}/gnupg-authentication"}
385 export MONKEYSPHERE_USER
387 export CHECK_KEYSERVER
388 export REQUIRED_USER_KEY_CAPABILITY
389 export GNUPGHOME_HOST
390 export GNUPGHOME_AUTHENTICATION
394 'update-users'|'update-user'|'u')
402 'show-fingerprint'|'f')
403 fingerprint_server_key
419 failure "Unknown command: '$COMMAND'
420 Type '$PGRM help' for usage."