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