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=${MONKEYSPHERE_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
42 gen-key (g) [HOSTNAME] generate gpg key for the server
43 -l|--length BITS key length in bits (2048)
44 -e|--expire EXPIRE date to expire
45 -r|--revoker FINGERPRINT add a revoker
46 show-fingerprint (f) show server's host key fingerprint
47 publish-key (p) publish server's host key to keyserver
49 add-identity-certifier (a) KEYID import and tsign a certification key
50 -n|--domain DOMAIN domain of certifier ()
51 -t|--trust TRUST trust level of certifier ('full')
52 -d|--depth DEPTH trust depth for certifier (1)
53 remove-identity-certifier (r) KEYID remove a certification key
54 list-identity-certifiers (l) list certification keys
61 su_monkeysphere_user() {
62 su --preserve-environment "$MONKEYSPHERE_USER" -- -c "$@"
65 # function to interact with the host gnupg keyring
69 GNUPGHOME="$GNUPGHOME_HOST"
72 # NOTE: we supress this warning because we need the monkeysphere
73 # user to be able to read the host pubring. we realize this might
74 # be problematic, but it's the simplest solution, without too much
76 gpg --no-permission-warning "$@"
79 # always reset the permissions on the host pubring so that the
80 # monkeysphere user can read the trust signatures
81 chgrp "$MONKEYSPHERE_USER" "${GNUPGHOME_HOST}/pubring.gpg"
82 chmod g+r "${GNUPGHOME_HOST}/pubring.gpg"
87 # function to interact with the authentication gnupg keyring
88 # FIXME: this function requires basically accepts only a single
89 # argument because of problems with quote expansion. this needs to be
91 gpg_authentication() {
92 GNUPGHOME="$GNUPGHOME_AUTHENTICATION"
95 su_monkeysphere_user "gpg $@"
98 # update authorized_keys for users
101 # get users from command line
104 # or just look at all users if none specified
105 unames=$(getent passwd | cut -d: -f1)
109 MODE="authorized_keys"
112 GNUPGHOME="$GNUPGHOME_AUTHENTICATION"
114 # check to see if the gpg trust database has been initialized
115 if [ ! -s "${GNUPGHOME}/trustdb.gpg" ] ; then
116 failure "GNUPG trust database uninitialized. Please see MONKEYSPHERE-SERVER(8)."
119 # make sure the authorized_keys directory exists
120 mkdir -p "${VARLIB}/authorized_keys"
123 for uname in $unames ; do
124 # check all specified users exist
125 if ! getent passwd "$uname" >/dev/null ; then
126 error "----- unknown user '$uname' -----"
130 # set authorized_user_ids and raw authorized_keys variables,
131 # translating ssh-style path variables
132 authorizedUserIDs=$(translate_ssh_variables "$uname" "$AUTHORIZED_USER_IDS")
133 rawAuthorizedKeys=$(translate_ssh_variables "$uname" "$RAW_AUTHORIZED_KEYS")
135 # if neither is found, skip user
136 if [ ! -s "$authorizedUserIDs" ] ; then
137 if [ "$rawAuthorizedKeys" = '-' -o ! -s "$rawAuthorizedKeys" ] ; then
142 log "----- user: $uname -----"
144 # make temporary directory
147 # trap to delete temporary directory on exit
148 trap "rm -rf $TMPDIR" EXIT
150 # create temporary authorized_user_ids file
151 TMP_AUTHORIZED_USER_IDS="${TMPDIR}/authorized_user_ids"
152 touch "$TMP_AUTHORIZED_USER_IDS"
154 # create temporary authorized_keys file
155 AUTHORIZED_KEYS="${TMPDIR}/authorized_keys"
156 touch "$AUTHORIZED_KEYS"
158 # set restrictive permissions on the temporary files
159 # FIXME: is there a better way to do this?
161 chmod 0600 "$AUTHORIZED_KEYS"
162 chmod 0600 "$TMP_AUTHORIZED_USER_IDS"
163 chown -R "$MONKEYSPHERE_USER" "$TMPDIR"
165 # if the authorized_user_ids file exists...
166 if [ -s "$authorizedUserIDs" ] ; then
167 # copy user authorized_user_ids file to temporary
169 cat "$authorizedUserIDs" > "$TMP_AUTHORIZED_USER_IDS"
171 # export needed variables
172 export AUTHORIZED_KEYS
173 export TMP_AUTHORIZED_USER_IDS
175 # process authorized_user_ids file, as monkeysphere
177 su_monkeysphere_user \
178 ". ${SHARE}/common; process_authorized_user_ids $TMP_AUTHORIZED_USER_IDS"
182 # add user-controlled authorized_keys file path if specified
183 if [ "$rawAuthorizedKeys" != '-' -a -s "$rawAuthorizedKeys" ] ; then
184 log -n "adding raw authorized_keys file... "
185 cat "$rawAuthorizedKeys" >> "$AUTHORIZED_KEYS"
189 # openssh appears to check the contents of the
190 # authorized_keys file as the user in question, so the
191 # file must be readable by that user at least.
192 # FIXME: is there a better way to do this?
193 chown root "$AUTHORIZED_KEYS"
194 chgrp $(getent passwd "$uname" | cut -f4 -d:) "$AUTHORIZED_KEYS"
195 chmod g+r "$AUTHORIZED_KEYS"
197 # if the resulting authorized_keys file is not empty, move
199 mv -f "$AUTHORIZED_KEYS" "${VARLIB}/authorized_keys/${uname}"
201 # destroy temporary directory
206 # generate server gpg key
213 hostName=${1:-$(hostname --fqdn)}
214 userID="ssh://${hostName}"
216 # check for presense of key with user ID
217 if gpg_host --list-key ="$userID" > /dev/null 2>&1 ; then
218 failure "Key for '$userID' already exists"
223 KEY_LENGTH=${KEY_LENGTH:="2048"}
225 # prompt about key expiration if not specified
226 if [ -z "$KEY_EXPIRE" ] ; then
228 Please specify how long the key should be valid.
229 0 = key does not expire
230 <n> = key expires in n days
231 <n>w = key expires in n weeks
232 <n>m = key expires in n months
233 <n>y = key expires in n years
235 while [ -z "$KEY_EXPIRE" ] ; do
236 read -p "Key is valid for? (0) " KEY_EXPIRE
237 if ! test_gpg_expire ${KEY_EXPIRE:=0} ; then
242 elif ! test_gpg_expire "$KEY_EXPIRE" ; then
243 failure "invalid key expiration value '$KEY_EXPIRE'."
247 keyParameters=$(cat <<EOF
249 Key-Length: $KEY_LENGTH
250 Key-Usage: $KEY_USAGE
252 Expire-Date: $KEY_EXPIRE
256 # add the revoker field if specified
257 # FIXME: the "1:" below assumes that $REVOKER's key is an RSA key.
258 # FIXME: key is marked "sensitive"? is this appropriate?
259 if [ "$REVOKER" ] ; then
260 keyParameters="${keyParameters}"$(cat <<EOF
261 Revoker: 1:$REVOKER sensitive
266 echo "The following key parameters will be used for the host private key:"
267 echo "$keyParameters"
269 read -p "Generate key? (Y/n) " OK; OK=${OK:=Y}
270 if [ ${OK/y/Y} != 'Y' ] ; then
275 keyParameters="${keyParameters}"$(cat <<EOF
282 log "generating server key..."
283 echo "$keyParameters" | gpg_host --batch --gen-key
285 # output the server fingerprint
286 fingerprint_server_key "=${userID}"
288 # find the key fingerprint of the server primary key
289 fingerprint=$(gpg_host --list-key --with-colons --with-fingerprint "=${userID}" | \
290 grep '^fpr:' | head -1 | cut -d: -f10)
292 # translate the private key to ssh format, and export to a file
294 # NOTE: assumes that the primary key is the proper key to use
296 gpg_host --export-secret-key "$fingerprint" | \
297 openpgp2ssh "$fingerprint" > "${VARLIB}/ssh_host_rsa_key")
298 log "Private SSH host key output to file: ${VARLIB}/ssh_host_rsa_key"
301 # gpg output key fingerprint
302 fingerprint_server_key() {
303 gpg_host --fingerprint --list-secret-keys
306 # publish server key to keyserver
307 publish_server_key() {
308 read -p "really publish key to $KEYSERVER? (y/N) " OK; OK=${OK:=N}
309 if [ ${OK/y/Y} != 'Y' ] ; then
314 # FIXME: need to figure out better way to identify host key
315 # dummy command so as not to publish fakes keys during testing
317 #gpg_authentication "--keyring $GNUPGHOME_HOST/pubring.gpg --keyserver $KEYSERVER --send-keys $(hostname -f)"
318 failure "NOT PUBLISHED (to avoid permanent publication errors during monkeysphere development)."
321 # retrieve key from web of trust, import it into the host keyring, and
322 # ltsign the key in the host keyring so that it may certify other keys
331 # export host ownertrust to authentication keyring
332 gpg_host --export-ownertrust | gpg_authentication "--import-ownertrust"
334 # get the key from the key server
335 gpg_authentication "--keyserver $KEYSERVER --recv-key '$keyID'"
337 # get the full fingerprint of a key ID
338 fingerprint=$(gpg_authentication "--list-key --with-colons --with-fingerprint $keyID" | \
339 grep '^fpr:' | grep "$keyID" | cut -d: -f10)
341 if [ -z "$fingerprint" ] ; then
342 failure "Could not find key \"${keyID}\"."
346 gpg_authentication "--fingerprint $fingerprint"
348 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}
349 if [ "${OK/y/Y}" != 'Y' ] ; then
353 # export the key to the host keyring
354 gpg_authentication "--export $keyID" | gpg_host --import
356 # default values for trust depth and domain
362 # NOTE: *all* user IDs will be ltsigned
363 ltsignCommand=$(cat <<EOF
375 echo "$ltsignCommand" | gpg_host --quiet --command-fd 0 --edit-key "$fingerprint"
377 # update the trustdb for the authentication keyring
378 gpg_authentication "--check-trustdb"
381 # delete a certifiers key from the host keyring
388 # delete the requested key (with prompting)
389 gpg_host --delete-key "$keyID"
391 # update the trustdb for the authentication keyring
392 gpg_authentication "--check-trustdb"
395 # list the host certifiers
400 ########################################################################
402 ########################################################################
404 # unset variables that should be defined only in config file
406 unset AUTHORIZED_USER_IDS
407 unset RAW_AUTHORIZED_KEYS
408 unset MONKEYSPHERE_USER
410 # load configuration file
411 [ -e ${MONKEYSPHERE_SERVER_CONFIG:="${ETC}/monkeysphere-server.conf"} ] && . "$MONKEYSPHERE_SERVER_CONFIG"
413 # set empty config variable with ones from the environment, or with
415 KEYSERVER=${MONKEYSPHERE_KEYSERVER:=${KEYSERVER:="subkeys.pgp.net"}}
416 AUTHORIZED_USER_IDS=${MONKEYSPHERE_AUTHORIZED_USER_IDS:=${AUTHORIZED_USER_IDS:="%h/.config/monkeysphere/authorized_user_ids"}}
417 RAW_AUTHORIZED_KEYS=${MONKEYSPHERE_RAW_AUTHORIZED_KEYS:=${RAW_AUTHORIZED_KEYS:="%h/.ssh/authorized_keys"}}
418 MONKEYSPHERE_USER=${MONKEYSPHERE_MONKEYSPHERE_USER:=${MONKEYSPHERE_USER:="monkeysphere"}}
421 CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:="true"}
422 REQUIRED_USER_KEY_CAPABILITY=${MONKEYSPHERE_REQUIRED_USER_KEY_CAPABILITY:="a"}
423 GNUPGHOME_HOST=${MONKEYSPHERE_GNUPGHOME_HOST:="${VARLIB}/gnupg-host"}
424 GNUPGHOME_AUTHENTICATION=${MONKEYSPHERE_GNUPGHOME_AUTHENTICATION:="${VARLIB}/gnupg-authentication"}
426 # export variables needed in su invocation
429 export MONKEYSPHERE_USER
431 export CHECK_KEYSERVER
432 export REQUIRED_USER_KEY_CAPABILITY
433 export GNUPGHOME_HOST
434 export GNUPGHOME_AUTHENTICATION
439 [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
442 # unset option variables
450 # get options for key generation and add-certifier functions
451 TEMP=$(getopt -o l:e:r:n:t:d: -l length:,expire:,revoker:,domain:,trust:,depth: -n "$PGRM" -- "$@")
453 if [ $? != 0 ] ; then
458 # Note the quotes around `$TEMP': they are essential!
497 'update-users'|'update-user'|'u')
505 'show-fingerprint'|'f')
506 fingerprint_server_key
513 'add-identity-certifier'|'add-certifier'|'a')
514 if [ -z "$1" ] ; then
515 failure "You must specify a key ID."
520 'remove-identity-certifier'|'remove-certifier'|'r')
521 if [ -z "$1" ] ; then
522 failure "You must specify a key ID."
524 remove_certifier "$1"
527 'list-identity-certifiers'|'list-certifiers'|'list-certifier'|'l')
536 failure "Unknown command: '$COMMAND'
537 Type '$PGRM help' for usage."