summaryrefslogtreecommitdiff
path: root/src/share/mh/diagnostics
blob: 51530e3a67d5efb224b418f2608a122777872cc0 (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-2009, and are all released under the GPL,
  11. # version 3 or later.
  12. # check on the status and validity of the key and public certificates
  13. diagnostics() {
  14. local seckey
  15. local keysfound
  16. local curdate
  17. local warnwindow
  18. local warndate
  19. local create
  20. local expire
  21. local uid
  22. local fingerprint
  23. local badhostkeys
  24. local sshd_config
  25. local problemsfound=0
  26. report_cruft
  27. # FIXME: what's the correct, cross-platform answer?
  28. sshd_config=/etc/ssh/sshd_config
  29. seckey=$(gpg_host --list-secret-keys --fingerprint --with-colons --fixed-list-mode)
  30. keysfound=$(echo "$seckey" | grep -c ^sec:)
  31. curdate=$(date +%s)
  32. # warn when anything is 2 months away from expiration
  33. warnwindow='2 months'
  34. warndate=$(advance_date $warnwindow +%s)
  35. if ! id monkeysphere >/dev/null ; then
  36. echo "! No monkeysphere user found! Please create a monkeysphere system user with bash as its shell."
  37. problemsfound=$(($problemsfound+1))
  38. fi
  39. if ! [ -d "$SYSDATADIR" ] ; then
  40. echo "! no $SYSDATADIR directory found. Please create it."
  41. problemsfound=$(($problemsfound+1))
  42. fi
  43. echo "Checking host GPG key..."
  44. if (( "$keysfound" < 1 )); then
  45. echo "! No host key found."
  46. echo " - Recommendation: run 'monkeysphere-host gen-key' or 'monkeysphere-host import-key'"
  47. problemsfound=$(($problemsfound+1))
  48. elif (( "$keysfound" > 1 )); then
  49. echo "! More than one host key found?"
  50. # FIXME: recommend a way to resolve this
  51. problemsfound=$(($problemsfound+1))
  52. else
  53. create=$(echo "$seckey" | grep ^sec: | cut -f6 -d:)
  54. expire=$(echo "$seckey" | grep ^sec: | cut -f7 -d:)
  55. fingerprint=$(echo "$seckey" | grep ^fpr: | head -n1 | cut -f10 -d:)
  56. # check for key expiration:
  57. if [ "$expire" ]; then
  58. if (( "$expire" < "$curdate" )); then
  59. echo "! Host key is expired."
  60. echo " - Recommendation: extend lifetime of key with 'monkeysphere-host extend-key'"
  61. problemsfound=$(($problemsfound+1))
  62. elif (( "$expire" < "$warndate" )); then
  63. echo "! Host key expires in less than $warnwindow:" $(advance_date $(( $expire - $curdate )) seconds +%F)
  64. echo " - Recommendation: extend lifetime of key with 'monkeysphere-host extend-key'"
  65. problemsfound=$(($problemsfound+1))
  66. fi
  67. fi
  68. # and weirdnesses:
  69. if [ "$create" ] && (( "$create" > "$curdate" )); then
  70. echo "! Host key was created in the future(?!). Is your clock correct?"
  71. echo " - Recommendation: Check clock ($(date +%F_%T)); use NTP?"
  72. problemsfound=$(($problemsfound+1))
  73. fi
  74. # check for UserID expiration:
  75. echo "$seckey" | grep ^uid: | cut -d: -f6,7,10 | \
  76. while IFS=: read create expire uid ; do
  77. # FIXME: should we be doing any checking on the form
  78. # of the User ID? Should we be unmangling it somehow?
  79. if [ "$create" ] && (( "$create" > "$curdate" )); then
  80. echo "! User ID '$uid' was created in the future(?!). Is your clock correct?"
  81. echo " - Recommendation: Check clock ($(date +%F_%T)); use NTP?"
  82. problemsfound=$(($problemsfound+1))
  83. fi
  84. if [ "$expire" ] ; then
  85. if (( "$expire" < "$curdate" )); then
  86. echo "! User ID '$uid' is expired."
  87. # FIXME: recommend a way to resolve this
  88. problemsfound=$(($problemsfound+1))
  89. elif (( "$expire" < "$warndate" )); then
  90. echo "! User ID '$uid' expires in less than $warnwindow:" $(advance_date $(( $expire - $curdate )) seconds +%F)
  91. # FIXME: recommend a way to resolve this
  92. problemsfound=$(($problemsfound+1))
  93. fi
  94. fi
  95. done
  96. # FIXME: verify that the host key is properly published to the
  97. # keyservers (do this with the non-privileged user)
  98. # FIXME: check that there are valid, non-expired certifying signatures
  99. # attached to the host key after fetching from the public keyserver
  100. # (do this with the non-privileged user as well)
  101. # FIXME: propose adding a revoker to the host key if none exist (do we
  102. # have a way to do that after key generation?)
  103. # Ensure that the ssh_host_rsa_key file is present and non-empty:
  104. echo
  105. echo "Checking host SSH key..."
  106. if [ ! -s "${SYSDATADIR}/ssh_host_rsa_key" ] ; then
  107. echo "! The host key as prepared for SSH (${SYSDATADIR}/ssh_host_rsa_key) is missing or empty."
  108. problemsfound=$(($problemsfound+1))
  109. else
  110. if [ $(ls -l "${SYSDATADIR}/ssh_host_rsa_key" | cut -f1 -d\ ) != '-rw-------' ] ; then
  111. echo "! Permissions seem wrong for ${SYSDATADIR}/ssh_host_rsa_key -- should be 0600."
  112. problemsfound=$(($problemsfound+1))
  113. fi
  114. # propose changes needed for sshd_config (if any)
  115. if ! grep -q "^HostKey[[:space:]]\+${SYSDATADIR}/ssh_host_rsa_key$" "$sshd_config"; then
  116. echo "! $sshd_config does not point to the monkeysphere host key (${SYSDATADIR}/ssh_host_rsa_key)."
  117. echo " - Recommendation: add a line to $sshd_config: 'HostKey ${SYSDATADIR}/ssh_host_rsa_key'"
  118. problemsfound=$(($problemsfound+1))
  119. fi
  120. if badhostkeys=$(grep -i '^HostKey' "$sshd_config" | grep -v "^HostKey[[:space:]]\+${SYSDATADIR}/ssh_host_rsa_key$") ; then
  121. echo "! $sshd_config refers to some non-monkeysphere host keys:"
  122. echo "$badhostkeys"
  123. echo " - Recommendation: remove the above HostKey lines from $sshd_config"
  124. problemsfound=$(($problemsfound+1))
  125. fi
  126. # FIXME: test (with ssh-keyscan?) that the running ssh
  127. # daemon is actually offering the monkeysphere host key.
  128. fi
  129. fi
  130. # FIXME: look at the ownership/privileges of the various keyrings,
  131. # directories housing them, etc (what should those values be? can
  132. # we make them as minimal as possible?)
  133. if [ "$problemsfound" -gt 0 ]; then
  134. echo "When the above $problemsfound issue"$(if [ "$problemsfound" -eq 1 ] ; then echo " is" ; else echo "s are" ; fi)" resolved, please re-run:"
  135. echo " monkeysphere-host diagnostics"
  136. else
  137. echo "Everything seems to be in order!"
  138. fi
  139. }