0411080fb8cd3599fee36ecc0ab6cf11e997faf5
[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 if ! id monkeysphere >/dev/null ; then
32     echo "! No monkeysphere user found!  Please create a monkeysphere system user with bash as its shell."
33     problemsfound=$(($problemsfound+1))
34 fi
35
36 if ! [ -d "$SYSDATADIR" ] ; then
37     echo "! no $SYSDATADIR directory found.  Please create it."
38     problemsfound=$(($problemsfound+1))
39 fi
40
41 echo "Checking for authentication directory..."
42 if ! [ -d "$MADATADIR" ] ; then
43     echo "! No authentication data directory found."
44     echo " - Recommendation: run 'monkeysphere-authentication setup'"
45     exit
46 fi    
47
48 # FIXME: what's the correct, cross-platform answer?
49 seckey=$(gpg_core --list-secret-keys --fingerprint --with-colons --fixed-list-mode)
50 keysfound=$(echo "$seckey" | grep -c ^sec:)
51 curdate=$(date +%s)
52 # warn when anything is 2 months away from expiration
53 warnwindow='2 months'
54 warndate=$(advance_date $warnwindow +%s)
55
56 echo "Checking core GPG key..."
57 if (( "$keysfound" < 1 )); then
58     echo "! No core key found."
59     echo " - Recommendation: run 'monkeysphere-authentication setup'"
60     problemsfound=$(($problemsfound+1))
61 elif (( "$keysfound" > 1 )); then
62     echo "! More than one core key found?"
63     # FIXME: recommend a way to resolve this
64     problemsfound=$(($problemsfound+1))
65 else
66     create=$(echo "$seckey" | grep ^sec: | cut -f6 -d:)
67     expire=$(echo "$seckey" | grep ^sec: | cut -f7 -d:)
68     fingerprint=$(echo "$seckey" | grep ^fpr: | head -n1 | cut -f10 -d:)
69     # check for key expiration:
70     if [ "$expire" ]; then
71         if (( "$expire"  < "$curdate" )); then
72             echo "! Core key is expired."
73             echo " - Recommendation: ???"
74             problemsfound=$(($problemsfound+1))
75         elif (( "$expire" < "$warndate" )); then
76             echo "! Core key expires in less than $warnwindow:" $(advance_date $(( $expire - $curdate )) seconds +%F)
77             echo " - Recommendation: ???"
78             problemsfound=$(($problemsfound+1))
79         fi
80     fi
81
82     # and weirdnesses:
83     if [ "$create" ] && (( "$create" > "$curdate" )); then
84         echo "! Core key was created in the future(?!). Is your clock correct?"
85         echo " - Recommendation: Check clock ($(date +%F_%T)); use NTP?"
86         problemsfound=$(($problemsfound+1))
87     fi
88             
89 fi
90
91 # FIXME: look at the ownership/privileges of the various keyrings,
92 #    directories housing them, etc (what should those values be?  can
93 #    we make them as minimal as possible?)
94
95 # FIXME: look to see that the ownertrust rules are set properly on the
96 #    sphere keyring
97
98 # FIXME: make sure that at least one identity certifier exists
99
100 # FIXME: look at the timestamps on the monkeysphere-generated
101 # authorized_keys files -- warn if they seem out-of-date.
102
103 # FIXME: check for a cronjob that updates monkeysphere-generated
104 # authorized_keys?
105
106 echo
107 echo "Checking for Monkeysphere-enabled public-key authentication for users ..."
108 # Ensure that User ID authentication is enabled:
109 if ! grep -q "^AuthorizedKeysFile[[:space:]]\+${SYSDATADIR}/authorized_keys/%u$" "$sshd_config"; then
110     echo "! $sshd_config does not point to monkeysphere authorized keys."
111     echo " - Recommendation: add a line to $sshd_config: 'AuthorizedKeysFile ${SYSDATADIR}/authorized_keys/%u'"
112     problemsfound=$(($problemsfound+1))
113 fi
114 if badauthorizedkeys=$(grep -i '^AuthorizedKeysFile' "$sshd_config" | grep -v "^AuthorizedKeysFile[[:space:]]\+${SYSDATADIR}/authorized_keys/%u$") ; then
115     echo "! $sshd_config refers to non-monkeysphere authorized_keys files:"
116     echo "$badauthorizedkeys"
117     echo " - Recommendation: remove the above AuthorizedKeysFile lines from $sshd_config"
118     problemsfound=$(($problemsfound+1))
119 fi
120
121 if [ "$problemsfound" -gt 0 ]; then
122     echo "When the above $problemsfound issue"$(if [ "$problemsfound" -eq 1 ] ; then echo " is" ; else echo "s are" ; fi)" resolved, please re-run:"
123     echo "  monkeysphere-authentication diagnostics"
124 else
125     echo "Everything seems to be in order!"
126 fi
127
128 }