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 log "authorized_keys file updated."
189 # destroy temporary directory
194 # generate server gpg key
201 hostName=${1:-$(hostname --fqdn)}
203 SERVICE=${SERVICE:-"ssh"}
204 userID="${SERVICE}://${hostName}"
206 if gpg_host --list-key ="$userID" > /dev/null 2>&1 ; then
207 failure "Key for '$userID' already exists"
211 KEY_TYPE=${KEY_TYPE:-"RSA"}
212 KEY_LENGTH=${KEY_LENGTH:-"2048"}
213 KEY_USAGE=${KEY_USAGE:-"auth"}
214 KEY_EXPIRE=${KEY_EXPIRE:-"0"}
216 Please specify how long the key should be valid.
217 0 = key does not expire
218 <n> = key expires in n days
219 <n>w = key expires in n weeks
220 <n>m = key expires in n months
221 <n>y = key expires in n years
223 read -p "Key is valid for? ($KEY_EXPIRE) " KEY_EXPIRE; KEY_EXPIRE=${KEY_EXPIRE:-"0"}
226 keyParameters=$(cat <<EOF
228 Key-Length: $KEY_LENGTH
229 Key-Usage: $KEY_USAGE
231 Expire-Date: $KEY_EXPIRE
235 # add the revoker field if specified
236 # FIXME: the "1:" below assumes that $REVOKER's key is an RSA key. why?
237 # FIXME: why is this marked "sensitive"? how will this signature ever
238 # be transmitted to the expected revoker?
239 if [ "$REVOKER" ] ; then
240 keyParameters="${keyParameters}"$(cat <<EOF
242 Revoker: 1:$REVOKER sensitive
247 echo "The following key parameters will be used for the host private key:"
248 echo "$keyParameters"
250 read -p "Generate key? [Y|n]: " OK; OK=${OK:=Y}
251 if [ ${OK/y/Y} != 'Y' ] ; then
256 keyParameters="${keyParameters}"$(cat <<EOF
263 log "generating server key..."
264 echo "$keyParameters" | gpg_host --batch --gen-key
266 # output the server fingerprint
267 fingerprint_server_key "=${userID}"
269 # find the key fingerprint of the server primary key
270 fingerprint=$(gpg_host --list-key --with-colons --with-fingerprint "=${userID}" | \
271 grep '^fpr:' | head -1 | cut -d: -f10)
273 # translate the private key to ssh format, and export to a file
275 # NOTE: assumes that the primary key is the proper key to use
277 gpg_host --export-secret-key "$fingerprint" | \
278 openpgp2ssh "$fingerprint" > "${VARLIB}/ssh_host_rsa_key")
279 log "Private SSH host key output to file: ${VARLIB}/ssh_host_rsa_key"
282 # gpg output key fingerprint
283 fingerprint_server_key() {
284 gpg_host --fingerprint --list-secret-keys
287 # publish server key to keyserver
288 publish_server_key() {
289 read -p "really publish key to $KEYSERVER? [y|N]: " OK; OK=${OK:=N}
290 if [ ${OK/y/Y} != 'Y' ] ; then
295 # FIXME: need to figure out better way to identify host key
296 # dummy command so as not to publish fakes keys during testing
298 #gpg_authentication "--keyring $GNUPGHOME_HOST/pubring.gpg --keyserver $KEYSERVER --send-keys $(hostname -f)"
299 failure "NOT PUBLISHED (to avoid permanent publication errors during monkeysphere development)."
302 # retrieve key from web of trust, and set owner trust to "full"
310 # default values for trust depth and domain
314 if [ -z "$keyID" ] ; then
315 failure "You must specify key to trust."
320 # export host ownertrust to authentication keyring
321 gpg_host --export-ownertrust | gpg_authentication "--import-ownertrust"
323 # get the key from the key server
324 gpg_authentication "--keyserver $KEYSERVER --recv-key $keyID"
326 # get the full fingerprint of a key ID
327 fingerprint=$(gpg_authentication "--list-key --with-colons --with-fingerprint $keyID" | \
328 grep '^fpr:' | grep "$keyID" | cut -d: -f10)
330 if [ -z "$fingerprint" ] ; then
331 failure "Could not find key '$keyID'."
335 gpg_authentication "--fingerprint $fingerprint"
337 # export the key to the host keyring
338 gpg_authentication "--export $keyID" | gpg_host --import
341 # NOTE: *all* user IDs will be ltsigned
342 ltsignCommand=$(cat <<EOF
354 echo "$ltsignCommand" | gpg_host --quiet --command-fd 0 --edit-key "$fingerprint"
356 # update the trustdb for the authentication keyring
357 gpg_authentication "--check-trustdb"
360 ########################################################################
362 ########################################################################
365 [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
368 # load configuration file
369 MS_CONF=${MS_CONF:-"${ETC}/monkeysphere-server.conf"}
370 [ -e "$MS_CONF" ] && . "$MS_CONF"
372 # set empty config variable with defaults
373 MONKEYSPHERE_USER=${MONKEYSPHERE_USER:-"monkeysphere"}
374 KEYSERVER=${KEYSERVER:-"subkeys.pgp.net"}
375 CHECK_KEYSERVER=${CHECK_KEYSERVER:="true"}
376 AUTHORIZED_USER_IDS=${AUTHORIZED_USER_IDS:-"%h/.config/monkeysphere/authorized_user_ids"}
377 RAW_AUTHORIZED_KEYS=${RAW_AUTHORIZED_KEYS:-"%h/.ssh/authorized_keys"}
380 REQUIRED_USER_KEY_CAPABILITY=${REQUIRED_USER_KEY_CAPABILITY:-"a"}
381 GNUPGHOME_HOST=${GNUPGHOME_HOST:-"${VARLIB}/gnupg-host"}
382 GNUPGHOME_AUTHENTICATION=${GNUPGHOME_AUTHENTICATION:-"${VARLIB}/gnupg-authentication"}
387 export MONKEYSPHERE_USER
389 export CHECK_KEYSERVER
390 export REQUIRED_USER_KEY_CAPABILITY
391 export GNUPGHOME_HOST
392 export GNUPGHOME_AUTHENTICATION
396 'update-users'|'update-user'|'u')
404 'show-fingerprint'|'f')
405 fingerprint_server_key
421 failure "Unknown command: '$COMMAND'
422 Type '$PGRM help' for usage."