cleaning up output of cruft report
[monkeysphere.git] / src / share / ma / diagnostics
1 # -*-shell-script-*-
2 # This should be sourced by bash (though we welcome changes to make it POSIX sh compliant)
3
4 # Monkeysphere authentication diagnostics subcommand
5 #
6 # The monkeysphere scripts are written by:
7 # Jameson Rollins <jrollins@finestructure.net>
8 # Jamie McClelland <jm@mayfirst.org>
9 # Daniel Kahn Gillmor <dkg@fifthhorseman.net>
10 #
11 # They are Copyright 2008-2009, and are all released under the GPL,
12 # version 3 or later.
13
14 # check on the status and validity of the key and public certificates
15
16 diagnostics() {
17
18 local seckey
19 local keysfound
20 local curdate
21 local warnwindow
22 local warndate
23 local create
24 local expire
25 local uid
26 local fingerprint
27 local badhostkeys
28 local sshd_config
29 local problemsfound=0
30
31 report_cruft
32
33 if ! id monkeysphere >/dev/null ; then
34     echo "! No monkeysphere user found!  Please create a monkeysphere system user with bash as its shell."
35     problemsfound=$(($problemsfound+1))
36 fi
37
38 if ! [ -d "$SYSDATADIR" ] ; then
39     echo "! no $SYSDATADIR directory found.  Please create it."
40     problemsfound=$(($problemsfound+1))
41 fi
42
43 echo "Checking for authentication directory..."
44 if ! [ -d "$MADATADIR" ] ; then
45     echo "! No authentication data directory found."
46     echo " - Recommendation: run 'monkeysphere-authentication setup'"
47     exit
48 fi    
49
50 # FIXME: what's the correct, cross-platform answer?
51 seckey=$(gpg_core --list-secret-keys --fingerprint --with-colons --fixed-list-mode)
52 keysfound=$(echo "$seckey" | grep -c ^sec:)
53 curdate=$(date +%s)
54 # warn when anything is 2 months away from expiration
55 warnwindow='2 months'
56 warndate=$(advance_date $warnwindow +%s)
57
58 echo "Checking core GPG key..."
59 if (( "$keysfound" < 1 )); then
60     echo "! No core key found."
61     echo " - Recommendation: run 'monkeysphere-authentication setup'"
62     problemsfound=$(($problemsfound+1))
63 elif (( "$keysfound" > 1 )); then
64     echo "! More than one core key found?"
65     # FIXME: recommend a way to resolve this
66     problemsfound=$(($problemsfound+1))
67 else
68     create=$(echo "$seckey" | grep ^sec: | cut -f6 -d:)
69     expire=$(echo "$seckey" | grep ^sec: | cut -f7 -d:)
70     fingerprint=$(echo "$seckey" | grep ^fpr: | head -n1 | cut -f10 -d:)
71     # check for key expiration:
72     if [ "$expire" ]; then
73         if (( "$expire"  < "$curdate" )); then
74             echo "! Core key is expired."
75             echo " - Recommendation: ???"
76             problemsfound=$(($problemsfound+1))
77         elif (( "$expire" < "$warndate" )); then
78             echo "! Core key expires in less than $warnwindow:" $(advance_date $(( $expire - $curdate )) seconds +%F)
79             echo " - Recommendation: ???"
80             problemsfound=$(($problemsfound+1))
81         fi
82     fi
83
84     # and weirdnesses:
85     if [ "$create" ] && (( "$create" > "$curdate" )); then
86         echo "! Core key was created in the future(?!). Is your clock correct?"
87         echo " - Recommendation: Check clock ($(date +%F_%T)); use NTP?"
88         problemsfound=$(($problemsfound+1))
89     fi
90             
91 fi
92
93 # FIXME: look at the ownership/privileges of the various keyrings,
94 #    directories housing them, etc (what should those values be?  can
95 #    we make them as minimal as possible?)
96
97 # FIXME: look to see that the ownertrust rules are set properly on the
98 #    sphere keyring
99
100 # FIXME: make sure that at least one identity certifier exists
101
102 # FIXME: look at the timestamps on the monkeysphere-generated
103 # authorized_keys files -- warn if they seem out-of-date.
104
105 # FIXME: check for a cronjob that updates monkeysphere-generated
106 # authorized_keys?
107
108 echo
109 echo "Checking for Monkeysphere-enabled public-key authentication for users ..."
110 # Ensure that User ID authentication is enabled:
111 if ! grep -q "^AuthorizedKeysFile[[:space:]]\+${SYSDATADIR}/authorized_keys/%u$" "$sshd_config"; then
112     echo "! $sshd_config does not point to monkeysphere authorized keys."
113     echo " - Recommendation: add a line to $sshd_config: 'AuthorizedKeysFile ${SYSDATADIR}/authorized_keys/%u'"
114     problemsfound=$(($problemsfound+1))
115 fi
116 if badauthorizedkeys=$(grep -i '^AuthorizedKeysFile' "$sshd_config" | grep -v "^AuthorizedKeysFile[[:space:]]\+${SYSDATADIR}/authorized_keys/%u$") ; then
117     echo "! $sshd_config refers to non-monkeysphere authorized_keys files:"
118     echo "$badauthorizedkeys"
119     echo " - Recommendation: remove the above AuthorizedKeysFile lines from $sshd_config"
120     problemsfound=$(($problemsfound+1))
121 fi
122
123 if [ "$problemsfound" -gt 0 ]; then
124     echo "When the above $problemsfound issue"$(if [ "$problemsfound" -eq 1 ] ; then echo " is" ; else echo "s are" ; fi)" resolved, please re-run:"
125     echo "  monkeysphere-authentication diagnostics"
126 else
127     echo "Everything seems to be in order!"
128 fi
129
130 }