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 add-certifier (a) KEYID import and tsign a certification key
45 remove-certifier (r) KEYID remove a certification key
46 list-certifiers (l) list certification keys
52 su_monkeysphere_user() {
53 su --preserve-environment "$MONKEYSPHERE_USER" -- -c "$@"
56 # function to interact with the host gnupg keyring
60 GNUPGHOME="$GNUPGHOME_HOST"
63 # NOTE: we supress this warning because we need the monkeysphere
64 # user to be able to read the host pubring. we realize this might
65 # be problematic, but it's the simplest solution, without too much
67 gpg --no-permission-warning "$@"
70 # always reset the permissions on the host pubring so that the
71 # monkeysphere user can read the trust signatures
72 chgrp "$MONKEYSPHERE_USER" "${GNUPGHOME_HOST}/pubring.gpg"
73 chmod g+r "${GNUPGHOME_HOST}/pubring.gpg"
78 # function to interact with the authentication gnupg keyring
79 gpg_authentication() {
80 GNUPGHOME="$GNUPGHOME_AUTHENTICATION"
83 su_monkeysphere_user "gpg $@"
86 # update authorized_keys for users
89 # get users from command line
92 # or just look at all users if none specified
93 unames=$(getent passwd | cut -d: -f1)
97 MODE="authorized_keys"
100 GNUPGHOME="$GNUPGHOME_AUTHENTICATION"
102 # check to see if the gpg trust database has been initialized
103 if [ ! -s "${GNUPGHOME}/trustdb.gpg" ] ; then
104 failure "GNUPG trust database uninitialized. Please see MONKEYSPHERE-SERVER(8)."
107 # make sure the authorized_keys directory exists
108 mkdir -p "${VARLIB}/authorized_keys"
111 for uname in $unames ; do
112 # check all specified users exist
113 if ! getent passwd "$uname" >/dev/null ; then
114 error "----- unknown user '$uname' -----"
118 # set authorized_user_ids and raw authorized_keys variables,
119 # translating ssh-style path variables
120 authorizedUserIDs=$(translate_ssh_variables "$uname" "$AUTHORIZED_USER_IDS")
121 rawAuthorizedKeys=$(translate_ssh_variables "$uname" "$RAW_AUTHORIZED_KEYS")
123 # if neither is found, skip user
124 if [ ! -s "$authorizedUserIDs" ] ; then
125 if [ "$rawAuthorizedKeys" = '-' -o ! -s "$rawAuthorizedKeys" ] ; then
130 log "----- user: $uname -----"
132 # make temporary directory
135 # trap to delete temporary directory on exit
136 trap "rm -rf $TMPDIR" EXIT
138 # create temporary authorized_user_ids file
139 TMP_AUTHORIZED_USER_IDS="${TMPDIR}/authorized_user_ids"
140 touch "$TMP_AUTHORIZED_USER_IDS"
142 # create temporary authorized_keys file
143 AUTHORIZED_KEYS="${TMPDIR}/authorized_keys"
144 touch "$AUTHORIZED_KEYS"
146 # set restrictive permissions on the temporary files
147 # FIXME: is there a better way to do this?
149 chmod 0600 "$AUTHORIZED_KEYS"
150 chmod 0600 "$TMP_AUTHORIZED_USER_IDS"
151 chown -R "$MONKEYSPHERE_USER" "$TMPDIR"
153 # if the authorized_user_ids file exists...
154 if [ -s "$authorizedUserIDs" ] ; then
155 # copy user authorized_user_ids file to temporary
157 cat "$authorizedUserIDs" > "$TMP_AUTHORIZED_USER_IDS"
159 # export needed variables
160 export AUTHORIZED_KEYS
161 export TMP_AUTHORIZED_USER_IDS
163 # process authorized_user_ids file, as monkeysphere
165 su_monkeysphere_user \
166 ". ${SHARE}/common; process_authorized_user_ids $TMP_AUTHORIZED_USER_IDS"
170 # add user-controlled authorized_keys file path if specified
171 if [ "$rawAuthorizedKeys" != '-' -a -s "$rawAuthorizedKeys" ] ; then
172 log -n "adding raw authorized_keys file... "
173 cat "$rawAuthorizedKeys" >> "$AUTHORIZED_KEYS"
177 # openssh appears to check the contents of the
178 # authorized_keys file as the user in question, so the
179 # file must be readable by that user at least.
180 # FIXME: is there a better way to do this?
181 chown root "$AUTHORIZED_KEYS"
182 chgrp $(getent passwd "$uname" | cut -f4 -d:) "$AUTHORIZED_KEYS"
183 chmod g+r "$AUTHORIZED_KEYS"
185 # if the resulting authorized_keys file is not empty, move
187 mv -f "$AUTHORIZED_KEYS" "${VARLIB}/authorized_keys/${uname}"
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, import it into the host keyring, and
303 # ltsign the key in the host keyring so that it may certify other keys
312 # export host ownertrust to authentication keyring
313 gpg_host --export-ownertrust | gpg_authentication "--import-ownertrust"
315 # get the key from the key server
316 gpg_authentication "--keyserver $KEYSERVER --recv-key $keyID"
318 # get the full fingerprint of a key ID
319 fingerprint=$(gpg_authentication "--list-key --with-colons --with-fingerprint $keyID" | \
320 grep '^fpr:' | grep "$keyID" | cut -d: -f10)
322 if [ -z "$fingerprint" ] ; then
323 failure "Could not find key '$keyID'."
327 gpg_authentication "--fingerprint $fingerprint"
329 read -p "Are you sure you want to add this key as a certifier of users on this system? (y/N) " OK; OK=${OK:-N}
330 if [ "${OK/y/Y}" != 'Y' ] ; then
334 # export the key to the host keyring
335 gpg_authentication "--export $keyID" | gpg_host --import
337 # default values for trust depth and domain
342 # NOTE: *all* user IDs will be ltsigned
343 ltsignCommand=$(cat <<EOF
355 echo "$ltsignCommand" | gpg_host --quiet --command-fd 0 --edit-key "$fingerprint"
357 # update the trustdb for the authentication keyring
358 gpg_authentication "--check-trustdb"
361 # delete a certifiers key from the host keyring
368 # delete the requested key (with prompting)
369 gpg_host --delete-key "$keyID"
371 # update the trustdb for the authentication keyring
372 gpg_authentication "--check-trustdb"
375 # list the host certifiers
380 ########################################################################
382 ########################################################################
385 [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
388 # load configuration file
389 MS_CONF=${MS_CONF:-"${ETC}/monkeysphere-server.conf"}
390 [ -e "$MS_CONF" ] && . "$MS_CONF"
392 # set empty config variable with defaults
393 MONKEYSPHERE_USER=${MONKEYSPHERE_USER:-"monkeysphere"}
394 KEYSERVER=${KEYSERVER:-"subkeys.pgp.net"}
395 CHECK_KEYSERVER=${CHECK_KEYSERVER:="true"}
396 AUTHORIZED_USER_IDS=${AUTHORIZED_USER_IDS:-"%h/.config/monkeysphere/authorized_user_ids"}
397 RAW_AUTHORIZED_KEYS=${RAW_AUTHORIZED_KEYS:-"%h/.ssh/authorized_keys"}
400 REQUIRED_USER_KEY_CAPABILITY=${REQUIRED_USER_KEY_CAPABILITY:-"a"}
401 GNUPGHOME_HOST=${GNUPGHOME_HOST:-"${VARLIB}/gnupg-host"}
402 GNUPGHOME_AUTHENTICATION=${GNUPGHOME_AUTHENTICATION:-"${VARLIB}/gnupg-authentication"}
407 export MONKEYSPHERE_USER
409 export CHECK_KEYSERVER
410 export REQUIRED_USER_KEY_CAPABILITY
411 export GNUPGHOME_HOST
412 export GNUPGHOME_AUTHENTICATION
416 'update-users'|'update-user'|'u')
424 'show-fingerprint'|'f')
425 fingerprint_server_key
433 if [ -z "$1" ] ; then
434 failure "You must specify a key ID."
439 'remove-certifier'|'r')
440 if [ -z "$1" ] ; then
441 failure "You must specify a key ID."
443 remove_certifier "$1"
446 'list-certifiers'|'l')
455 failure "Unknown command: '$COMMAND'
456 Type '$PGRM help' for usage."