summaryrefslogtreecommitdiff
path: root/src/share/ma/setup
blob: 6745da789603a7936f045611ea7f93a1141cf7de (plain)
  1. # -*-shell-script-*-
  2. # This should be sourced by bash (though we welcome changes to make it POSIX sh compliant)
  3. # Monkeysphere authentication setup subcommand
  4. #
  5. # The monkeysphere scripts are written by:
  6. # Jameson Rollins <jrollins@finestructure.net>
  7. # Jamie McClelland <jm@mayfirst.org>
  8. # Daniel Kahn Gillmor <dkg@fifthhorseman.net>
  9. #
  10. # They are Copyright 2009, and are all released under the GPL,
  11. # version 3 or later.
  12. setup() {
  13. # make all needed directories
  14. log debug "checking authentication directory structure..."
  15. mkdir -p "${MADATADIR}"
  16. chmod 0750 "${MADATADIR}"
  17. chgrp "$MONKEYSPHERE_GROUP" "${MADATADIR}"
  18. mkdir -p "${MATMPDIR}"
  19. chmod 0750 "${MATMPDIR}"
  20. chgrp "$MONKEYSPHERE_GROUP" "${MATMPDIR}"
  21. mkdir -p "${GNUPGHOME_CORE}"
  22. chmod 0700 "${GNUPGHOME_CORE}"
  23. mkdir -p "${GNUPGHOME_SPHERE}"
  24. chmod 0700 "${GNUPGHOME_SPHERE}"
  25. mkdir -p "${SYSDATADIR}"/authorized_keys
  26. # deliberately replace the config files via truncation
  27. # FIXME: should we be dumping to tmp files and then moving atomically?
  28. log debug "writing core gpg.conf..."
  29. cat >"${GNUPGHOME_CORE}"/gpg.conf <<EOF
  30. # Monkeysphere trust core GnuPG configuration
  31. # This file is maintained by the Monkeysphere software.
  32. # Edits will be overwritten.
  33. no-greeting
  34. keyid-format 0xlong
  35. EOF
  36. KEYSERVER_OPTIONS=""
  37. for anchorfile in "${SYSCONFIGDIR}/monkeysphere-authentication-x509-anchors.crt" "${SYSCONFIGDIR}/monkeysphere-x509-anchors.crt"; do
  38. if [ -z "$KEYSERVER_OPTIONS" ] && [ -r "$anchorfile" ] ; then
  39. KEYSERVER_OPTIONS="keyserver-options ca-cert-file=$anchorfile"
  40. log debug "using $anchorfile for keyserver X.509 anchor"
  41. fi
  42. done
  43. log debug "writing sphere gpg.conf..."
  44. cat >"${GNUPGHOME_SPHERE}"/gpg.conf <<EOF
  45. # Monkeysphere trust sphere GnuPG configuration
  46. # This file is maintained by the Monkeysphere software.
  47. # Edits will be overwritten.
  48. no-greeting
  49. list-options show-uid-validity
  50. keyid-format 0xlong
  51. ${KEYSERVER_OPTIONS}
  52. EOF
  53. # make sure the monkeysphere user owns everything in the sphere
  54. # gnupghome
  55. log debug "fixing sphere gnupg home ownership..."
  56. chown "$MONKEYSPHERE_USER:$MONKEYSPHERE_GROUP" "${GNUPGHOME_SPHERE}" "${GNUPGHOME_SPHERE}"/gpg.conf
  57. # get fingerprint of core key. this should be empty on unconfigured systems.
  58. local CORE_FPR=$(core_fingerprint)
  59. log debug "core fingerprint: $CORE_FPR"
  60. if [ -z "$CORE_FPR" ] ; then
  61. log info "setting up Monkeysphere authentication trust core..."
  62. local CORE_UID=$(printf "Monkeysphere authentication trust core UID (random string: %s)" $(dd if=/dev/urandom bs=21 count=1 2>/dev/null | perl -MMIME::Base64 -ne 'print encode_base64($_)'))
  63. printf "generating monkeysphere authentication trust core key:\nsize: %d bits\nuid: '%s'\n" "$CORE_KEYLENGTH" "$CORE_UID" | log debug
  64. PEM2OPENPGP_USAGE_FLAGS=certify \
  65. PEM2OPENPGP_NEWKEY=$CORE_KEYLENGTH pem2openpgp "$CORE_UID" \
  66. | gpg_core --import \
  67. || failure "Could not import new key for Monkeysphere authentication trust core"
  68. # get fingerprint of core key. should definitely not be empty at this point
  69. CORE_FPR=$(core_fingerprint)
  70. log debug "core fingerprint: $CORE_FPR"
  71. if [ -z "$CORE_FPR" ] ; then
  72. failure "Failed to create Monkeysphere authentication trust core!"
  73. fi
  74. else
  75. log verbose "Monkeysphere authentication trust core already exists."
  76. fi
  77. # export the core key to the sphere keyring
  78. log debug "exporting core pub key to sphere keyring..."
  79. gpg_core --export | gpg_sphere --import
  80. # ensure that the authentication sphere checker has absolute ownertrust on the expected key.
  81. log debug "setting ultimate owner trust on core key in gpg_sphere..."
  82. printf "%s:6:\n" "$CORE_FPR" | gpg_sphere --import-ownertrust 2>&1 | log verbose
  83. gpg_sphere --export-ownertrust 2>&1 | log debug
  84. # check the owner trust
  85. log debug "checking gpg_sphere owner trust set properly..."
  86. local ORIG_TRUST
  87. if ORIG_TRUST=$(gpg_sphere --export-ownertrust | grep '^[^#]') ; then
  88. if [ "${CORE_FPR}:6:" != "$ORIG_TRUST" ] ; then
  89. failure "Monkeysphere authentication trust sphere should explicitly trust the core. It does not have proper ownertrust settings."
  90. fi
  91. else
  92. failure "Could not get monkeysphere-authentication trust guidelines."
  93. # FIXME: what does this mean? should we suggest how to fix?
  94. fi
  95. # ensure that we're using the extended trust model (1), and that
  96. # our preferences are reasonable (i.e. 3 marginal OR 1 fully
  97. # trusted certifications are sufficient to grant full validity.
  98. log debug "checking trust model for authentication ..."
  99. local TRUST_MODEL=$(gpg_sphere --with-colons --list-keys 2>/dev/null \
  100. | head -n1 | grep "^tru:" | cut -d: -f3,6,7)
  101. log debug "sphere trust model: $TRUST_MODEL"
  102. if [ "$TRUST_MODEL" != '1:3:1' ] ; then
  103. failure "monkeysphere-authentication does not have the expected trust model settings."
  104. # FIXME: what does this mean? should we suggest how to fix?
  105. fi
  106. }