summaryrefslogtreecommitdiff
path: root/src/share/mh/diagnostics
blob: 9409f1dd53d98a82879ca0d19cda591fe7bb21dd (plain)
  1. # -*-shell-script-*-
  2. # This should be sourced by bash (though we welcome changes to make it POSIX sh compliant)
  3. # Monkeysphere host diagnostics 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 2008-2010, and are all released under the GPL,
  11. # version 3 or later.
  12. # check on the status and validity of the host's public certificates (and keys?)
  13. # global vars for communicating between functions:
  14. MHD_CURDATE=$(date +%s)
  15. # warn when anything is 2 months away from expiration
  16. MHD_WARNWINDOW='2 months'
  17. MHD_WARNDATE=$(advance_date $MHD_WARNWINDOW +%s)
  18. MHD_PROBLEMSFOUND=0
  19. diagnose_key() {
  20. local fpr="$1"
  21. local certinfo
  22. local create
  23. local expire
  24. local uid
  25. local keysfound
  26. local uiderrs
  27. local errcount
  28. printf "Checking OpenPGP Certificate for key 0x%s\n" "$fpr"
  29. certinfo=$(get_cert_info "0x$fpr" <"$HOST_KEY_FILE")
  30. keysfound=$(grep -c ^pub: <<<"$certinfo")
  31. if [ "$keysfound" -lt 1 ] ; then
  32. printf "! Could not find key with fingerprint 0x%s\n" "$fpr"
  33. # FIXME: recommend a way to resolve this!
  34. MHD_PROBLEMSFOUND=$(($MHD_PROBLEMSFOUND+1))
  35. fi
  36. create=$(echo "$certinfo" | grep ^pub: | cut -f6 -d:)
  37. expire=$(echo "$certinfo" | grep ^pub: | cut -f7 -d:)
  38. # check for key expiration:
  39. if [ "$expire" ]; then
  40. if (( "$expire" < "$MHD_CURDATE" )); then
  41. printf "! Host key 0x%s is expired.\n" "$fpr"
  42. printf " - Recommendation: extend lifetime of key with 'monkeysphere-host set-expire 0x%s'\n" "$fpr"
  43. MHD_PROBLEMSFOUND=$(($MHD_PROBLEMSFOUND+1))
  44. elif (( "$expire" < "$MHD_WARNDATE" )); then
  45. printf "! Host key 0x%s expires in less than %s: %s\n" "$fpr" "$MHD_WARNWINDOW" $(advance_date $(( $expire - $MHD_CURDATE )) seconds +%F)
  46. printf " - Recommendation: extend lifetime of key with 'monkeysphere-host set-expire %s'\n" "$fpr"
  47. MHD_PROBLEMSFOUND=$(($MHD_PROBLEMSFOUND+1))
  48. fi
  49. fi
  50. # and weirdnesses:
  51. if [ "$create" ] && (( "$create" > "$MHD_CURDATE" )); then
  52. printf "! Host key 0x%s was created in the future(?!): %s. Is your clock correct?\n" "$fpr" $(date -d "1970-01-01 + $create seconds" +%F)
  53. printf " - Recommendation: Check your clock (is it really %s?); use NTP?\n" $(date +%F_%T)
  54. MHD_PROBLEMSFOUND=$(($MHD_PROBLEMSFOUND+1))
  55. fi
  56. # check for UserID expiration:
  57. uiderrs=$(printf '%s\n' "$certinfo" | grep ^uid: | cut -d: -f6,7,10 | \
  58. while IFS=: read -r create expire uid ; do
  59. uid=$(gpg_unescape <<<"$uid")
  60. check_service_name "$uid"
  61. if [ "$create" ] && (( "$create" > "$MHD_CURDATE" )); then
  62. printf "! The latest self-sig on User ID '%s' was created in the future(?!): %s.\n - Is your clock correct?\n" "$uid" $(date -d "1970-01-01 + $create seconds" +%F)
  63. printf " - Recommendation: Check your clock (is it really %s ?); use NTP?\n" $(date +%F_%T)
  64. fi
  65. if [ "$expire" ] ; then
  66. if (( "$expire" < "$MHD_CURDATE" )); then
  67. printf "! User ID '%s' is expired.\n" "$uid"
  68. # FIXME: recommend a way to resolve this
  69. elif (( "$expire" < "$MHD_WARNDATE" )); then
  70. printf "! User ID '%s' expires in less than %s: %s\n" "%s" "$MHD_WARNWINDOW" $(advance_date $(( $expire - $MHD_CURDATE )) seconds +%F)
  71. # FIXME: recommend a way to resolve this
  72. fi
  73. fi
  74. done)
  75. errcount=$(grep -c '^!' <<<"$uiderrs") || \
  76. MHD_PROBLEMSFOUND=$(($MHD_PROBLEMSFOUND+ $errcount ))
  77. printf '%s\n' "$uiderrs"
  78. # FIXME: verify that the host key is properly published to the
  79. # keyservers (do this with the non-privileged user)
  80. # FIXME: check that there are valid, non-expired certifying signatures
  81. # attached to the host key after fetching from the public keyserver
  82. # (do this with the non-privileged user as well)
  83. # FIXME: propose adding a revoker to the host key if none exist (do we
  84. # have a way to do that after key generation?)
  85. # FIXME: test (with ssh-keyscan?) that any running ssh daemon is
  86. # actually offering the monkeysphere host key, if such a key is
  87. # loaded.
  88. # FIXME: scan /proc/net/tcp and /proc/net/tcp6 to see what
  89. # known-crypto ports (ssh, https, imaps?, ldaps?, etc) are in use
  90. # locally. Propose bringing them into the monkeysphere.
  91. # FIXME: ensure that the key is of a reasonable size
  92. # FIXME: ensure that the cert has the right key usage flags
  93. # FIXME: ensure that the key doesn't match any known blacklist
  94. }
  95. diagnostics() {
  96. MHD_PROBLEMSFOUND=0
  97. if ! [ -d "$SYSDATADIR" ] ; then
  98. echo "! no $SYSDATADIR directory found. Please create it."
  99. exit
  100. fi
  101. if ! [ -f "$HOST_KEY_FILE" ] ; then
  102. echo "No host OpenPGP certificates file found!"
  103. echo " - Recommendation: run 'monkeysphere-host import-key' with a service key"
  104. exit
  105. fi
  106. if ! id monkeysphere >/dev/null ; then
  107. echo "! No monkeysphere user found! Please create a monkeysphere system user with bash as its shell."
  108. MHD_PROBLEMSFOUND=$(($MHD_PROBLEMSFOUND+1))
  109. fi
  110. echo "Checking host OpenPGP certificates..."
  111. multi_key diagnose_key
  112. # FIXME: look at the ownership/privileges of the various keyrings,
  113. # directories housing them, etc (what should those values be? can
  114. # we make them as minimal as possible?)
  115. # report on any cruft from old monkeysphere version
  116. report_cruft
  117. if [ "$MHD_PROBLEMSFOUND" -gt 0 ]; then
  118. echo "When the above $MHD_PROBLEMSFOUND issue"$(if [ "$MHD_PROBLEMSFOUND" -eq 1 ] ; then echo " is" ; else echo "s are" ; fi)" resolved, please re-run:"
  119. echo " monkeysphere-host diagnostics"
  120. else
  121. echo "Everything seems to be in order!"
  122. fi
  123. }