-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_certificate.sh
187 lines (173 loc) · 4.71 KB
/
check_certificate.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#!/bin/bash
describe_openssl_error () {
grep -i 'no peer certificate available' $tmp_folder/openssl.out &>/dev/null
if [ $? -eq 0 ]; then
echo "No certificate sent by server"
else
# Source: https://github.com/torvalds/linux/blob/master/include/uapi/asm-generic/errno.h
case "$(awk -F'=' '/connect:errno/{print $2}' $tmp_folder/openssl.err)" in
"11" | "110")
echo "Timeout"
;;
"22")
echo "Name or service not known"
;;
"71")
echo "Protocol error"
;;
"92")
echo "Protocol not available"
;;
"93")
echo "Protocol not supported"
;;
"111")
echo "Connection refused"
;;
"112")
echo "Host is down"
;;
"113")
echo "No route to host"
;;
*)
echo "Unknown error"
cat $tmp_folder/openssl.err
;;
esac
fi
}
return_message() {
case "$1" in
0)
echo "OK: $2"
;;
1)
echo "WARNING: $2"
;;
2)
echo "CRITICAL: $2"
;;
3)
echo "UNKNOWN: $2"
;;
esac
rm -rf $tmp_folder
exit $1
}
usage () {
cat << EOF
Usage: $0 -H hostname -p port -s type
Check certificate expiration date
Options:
-h
Print this help
-c days
Minimum number of days before critical alert (default=30)
-H hostname
Host name or IP Address
-p port
Port number
-s protocol
Send the protocol-specific message(s) to switch to TLS for communication.
Allowed values:
- smtp
- pop3
- imap
- ftp
- xmpp
- xmpp-server
- irc
- postgres
- mysql
- lmtp
- nntp
- sieve
- ldap
-v
Verify certificate hostname match
-w days
Minimum number of days before warning alert (default=60)
EOF
exit 3
}
test_missing_arg () {
if [[ $OPTARG == -* ]]; then
echo "Argument required for -$option"
usage
fi
}
is_int='^[0-9]+$'
while getopts ":c:hH:p:s:vw:" option;do
case $option in
c)
test_missing_arg
if ! [[ $OPTARG =~ $is_int ]] ; then
echo "-c should be an integer"
usage
fi
critical=$(( $OPTARG ))
;;
h)
usage
;;
H)
test_missing_arg
host="$OPTARG"
;;
p)
test_missing_arg
port="$OPTARG"
;;
s)
test_missing_arg
starttls="$OPTARG"
;;
v)
verify="true"
;;
w)
test_missing_arg
if ! [[ $OPTARG =~ $is_int ]] ; then
echo "-w should be an integer"
usage
fi
warning=$(( $OPTARG ))
;;
:)
echo "Argument required for -$OPTARG"
usage
;;
\?)
echo "$OPTARG : unknown option"
usage
;;
esac
done
[[ -z "$host" ]] && echo "-H hostname is mandatory" && usage
[[ -z "$port" ]] && echo "-p port is mandatory" && usage
[[ -z "$critical" ]] && critical=30
[[ -z "$warning" ]] && warning=60
[[ -n "$verify" ]] && options="-verify_hostname $host"
[[ -n "$starttls" ]] && options="${options} -starttls ftp"
tmp_folder=$(mktemp -d)
echo "QUIT" | openssl s_client -host $host -port $port ${options} 2>$tmp_folder/openssl.err 1>$tmp_folder/openssl.out
if [ $? -ne 0 ]; then
return_message 3 "Unable to get certificate : $(describe_openssl_error)"
fi
# Certificate verification: expiry and hostname
verification_status=$(awk -F': ' '/Verification error: /{ print $2}' $tmp_folder/openssl.out)
if [ -n "$verification_status" ]; then
return_message 2 "Error on certificate validation: $verification_status"
fi
expiry_date=$(openssl x509 -in $tmp_folder/openssl.out -noout -text | awk -F':' '/Not After/{print $2}')
critical_delta=$(( ( $(date -d "$expiry_date -${critical} days" +%s) - $(date +%s) ) / (60*60*24) ))
warning_delta=$(( ( $(date -d "$expiry_date -${warning} days" +%s) - $(date +%s) ) / (60*60*24) ))
if [ $critical_delta -le 0 ]; then
return_code=2
elif [ $warning_delta -le 0 ]; then
return_code=1
else
return_code=0
fi
return_message $return_code "Certificate '$(openssl x509 -in $tmp_folder/openssl.out -noout -text | awk -F'=' '/Subject:.*, CN ?= ?/{print $NF}')' will expire on $(date -d "$expiry_date")"