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
|
/*****************************************************************************
*
* Nagios plugins SSL utilities
*
* License: GPL
* Copyright (c) 2005-2007 Nagios Plugins Development Team
*
* Description:
*
* This file contains common functions for plugins that require SSL.
*
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*
*****************************************************************************/
#define LOCAL_TIMEOUT_ALARM_HANDLER
#include "common.h"
#include "netutils.h"
/* Max length of timestamps, ex: "03/05/2009 00:13 GMT". Calculate up to 6
* chars for the timezone (ex: "GMT-10") and one terminating \0 */
#define TS_LENGTH 24
#ifdef HAVE_SSL
static SSL_CTX *c=NULL;
static SSL *s=NULL;
static int initialized=0;
int np_net_ssl_init (int sd){
if (!initialized) {
/* Initialize SSL context */
SSLeay_add_ssl_algorithms ();
SSL_load_error_strings ();
OpenSSL_add_all_algorithms ();
initialized = 1;
}
if ((c = SSL_CTX_new (SSLv23_client_method ())) == NULL) {
printf ("%s\n", _("CRITICAL - Cannot create SSL context."));
return STATE_CRITICAL;
}
if ((s = SSL_new (c)) != NULL){
SSL_set_fd (s, sd);
if (SSL_connect(s) == 1){
return OK;
} else {
printf ("%s\n", _("CRITICAL - Cannot make SSL connection "));
# ifdef USE_OPENSSL /* XXX look into ERR_error_string */
ERR_print_errors_fp (stdout);
# endif /* USE_OPENSSL */
}
} else {
printf ("%s\n", _("CRITICAL - Cannot initiate SSL handshake."));
}
return STATE_CRITICAL;
}
void np_net_ssl_cleanup (){
if(s){
SSL_shutdown (s);
SSL_free (s);
if(c) {
SSL_CTX_free (c);
c=NULL;
}
s=NULL;
}
}
int np_net_ssl_write(const void *buf, int num){
return SSL_write(s, buf, num);
}
int np_net_ssl_read(void *buf, int num){
return SSL_read(s, buf, num);
}
int np_net_ssl_check_cert(int days_till_exp){
# ifdef USE_OPENSSL
X509 *certificate=NULL;
ASN1_STRING *tm;
int offset;
struct tm stamp;
int days_left;
char timestamp[TS_LENGTH] = "";
certificate=SSL_get_peer_certificate(s);
if(! certificate){
printf ("%s\n",_("CRITICAL - Cannot retrieve server certificate."));
return STATE_CRITICAL;
}
/* Retrieve timestamp of certificate */
tm = X509_get_notAfter (certificate);
/* Generate tm structure to process timestamp */
if (tm->type == V_ASN1_UTCTIME) {
if (tm->length < 10) {
printf ("%s\n", _("CRITICAL - Wrong time format in certificate."));
return STATE_CRITICAL;
} else {
stamp.tm_year = (tm->data[0] - '0') * 10 + (tm->data[1] - '0');
if (stamp.tm_year < 50)
stamp.tm_year += 100;
offset = 0;
}
} else {
if (tm->length < 12) {
printf ("%s\n", _("CRITICAL - Wrong time format in certificate."));
return STATE_CRITICAL;
} else {
stamp.tm_year =
(tm->data[0] - '0') * 1000 + (tm->data[1] - '0') * 100 +
(tm->data[2] - '0') * 10 + (tm->data[3] - '0');
stamp.tm_year -= 1900;
offset = 2;
}
}
stamp.tm_mon =
(tm->data[2 + offset] - '0') * 10 + (tm->data[3 + offset] - '0') - 1;
stamp.tm_mday =
(tm->data[4 + offset] - '0') * 10 + (tm->data[5 + offset] - '0');
stamp.tm_hour =
(tm->data[6 + offset] - '0') * 10 + (tm->data[7 + offset] - '0');
stamp.tm_min =
(tm->data[8 + offset] - '0') * 10 + (tm->data[9 + offset] - '0');
stamp.tm_sec = 0;
stamp.tm_isdst = -1;
float time_left = difftime(timegm(&stamp), time(NULL));
days_left = time_left / 86400;
snprintf
(timestamp, TS_LENGTH, "%02d/%02d/%04d %02d:%02d %s",
stamp.tm_mon + 1,
stamp.tm_mday, stamp.tm_year + 1900, stamp.tm_hour, stamp.tm_min, stamp.tm_zone);
if (days_left > 0 && days_left <= days_till_exp) {
printf (_("WARNING - Certificate expires in %d day(s) (%s).\n"), days_left, timestamp);
return STATE_WARNING;
} else if (time_left < 0) {
printf (_("CRITICAL - Certificate expired on %s.\n"), timestamp);
return STATE_CRITICAL;
} else if (days_left == 0) {
printf (_("WARNING - Certificate expires today (%s).\n"), timestamp);
return STATE_WARNING;
}
printf (_("OK - Certificate will expire on %s.\n"), timestamp);
X509_free (certificate);
return STATE_OK;
# else /* ifndef USE_OPENSSL */
printf ("%s\n", _("WARNING - Plugin does not support checking certificates."));
return STATE_WARNING;
# endif /* USE_OPENSSL */
}
#endif /* HAVE_SSL */
|