summaryrefslogtreecommitdiffstats
path: root/plugins/t
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/t')
0 files changed, 0 insertions, 0 deletions
diff --git a/plugins/Makefile.am b/plugins/Makefile.am
index beefb32..9222771 100644
--- a/plugins/Makefile.am
+++ b/plugins/Makefile.am
@@ -24,13 +24,15 @@ EXTRA_PROGRAMS = check_mysql check_radius check_pgsql check_snmp check_hpjd \
24 check_nagios check_by_ssh check_dns check_nt check_ide_smart \ 24 check_nagios check_by_ssh check_dns check_nt check_ide_smart \
25 check_procs 25 check_procs
26 26
27EXTRA_DIST = t utils.c netutils.c popen.c utils.h netutils.h popen.h common.h \ 27EXTRA_DIST = t utils.c netutils.c sslutils.c popen.c utils.h netutils.h \
28 getaddrinfo.c getaddrinfo.h gethostbyname.c gethostbyname.h 28 popen.h common.h getaddrinfo.c getaddrinfo.h \
29 gethostbyname.c gethostbyname.h
29 30
30PLUGINHDRS = common.h 31PLUGINHDRS = common.h
31 32
32BASEOBJS = utils.o ../lib/libnagiosplug.a ../lib/libcoreutils.a 33BASEOBJS = utils.o ../lib/libnagiosplug.a ../lib/libcoreutils.a
33NETOBJS = netutils.o $(BASEOBJS) $(EXTRA_NETOBJS) 34NETOBJS = netutils.o $(BASEOBJS) $(EXTRA_NETOBJS)
35SSLOBJS = sslutils.o
34NETLIBS = $(NETOBJS) $(SOCKETLIBS) 36NETLIBS = $(NETOBJS) $(SOCKETLIBS)
35 37
36TESTS_ENVIRONMENT = perl -I $(top_builddir) -I $(top_srcdir) 38TESTS_ENVIRONMENT = perl -I $(top_builddir) -I $(top_srcdir)
@@ -51,7 +53,7 @@ check_dns_LDADD = $(NETLIBS) popen.o
51check_dummy_LDADD = $(BASEOBJS) 53check_dummy_LDADD = $(BASEOBJS)
52check_fping_LDADD = $(NETLIBS) popen.o 54check_fping_LDADD = $(NETLIBS) popen.o
53check_game_LDADD = $(BASEOBJS) popen.o 55check_game_LDADD = $(BASEOBJS) popen.o
54check_http_LDADD = $(NETLIBS) 56check_http_LDADD = $(SSLOBJS) $(NETLIBS)
55check_hpjd_LDADD = $(NETLIBS) popen.o 57check_hpjd_LDADD = $(NETLIBS) popen.o
56check_ldap_LDADD = $(NETLIBS) $(LDAPLIBS) 58check_ldap_LDADD = $(NETLIBS) $(LDAPLIBS)
57check_load_LDADD = $(BASEOBJS) popen.o 59check_load_LDADD = $(BASEOBJS) popen.o
@@ -68,12 +70,12 @@ check_procs_LDADD = $(BASEOBJS) popen.o
68check_radius_LDADD = $(NETLIBS) $(RADIUSLIBS) 70check_radius_LDADD = $(NETLIBS) $(RADIUSLIBS)
69check_real_LDADD = $(NETLIBS) 71check_real_LDADD = $(NETLIBS)
70check_snmp_LDADD = $(BASEOBJS) popen.o 72check_snmp_LDADD = $(BASEOBJS) popen.o
71check_smtp_LDADD = $(NETLIBS) 73check_smtp_LDADD = $(SSLOBJS) $(NETLIBS)
72check_ssh_LDADD = $(NETLIBS) 74check_ssh_LDADD = $(NETLIBS)
73check_swap_LDADD = $(MATHLIBS) $(BASEOBJS) popen.o 75check_swap_LDADD = $(MATHLIBS) $(BASEOBJS) popen.o
74check_tcp_LDADD = $(NETLIBS) 76check_tcp_LDADD = $(SSLOBJS) $(NETLIBS)
75check_time_LDADD = $(NETLIBS) 77check_time_LDADD = $(NETLIBS)
76check_udp_LDADD = $(NETLIBS) 78check_udp_LDADD = $(SSLOBJS) $(NETLIBS)
77check_ups_LDADD = $(NETLIBS) 79check_ups_LDADD = $(NETLIBS)
78check_users_LDADD = $(BASEOBJS) popen.o 80check_users_LDADD = $(BASEOBJS) popen.o
79check_by_ssh_LDADD = $(NETLIBS) popen.o 81check_by_ssh_LDADD = $(NETLIBS) popen.o
@@ -87,7 +89,7 @@ check_dns_DEPENDENCIES = check_dns.c $(NETOBJS) popen.o $(DEPLIBS)
87check_dummy_DEPENDENCIES = check_dummy.c $(DEPLIBS) 89check_dummy_DEPENDENCIES = check_dummy.c $(DEPLIBS)
88check_fping_DEPENDENCIES = check_fping.c $(NETOBJS) popen.o $(DEPLIBS) 90check_fping_DEPENDENCIES = check_fping.c $(NETOBJS) popen.o $(DEPLIBS)
89check_game_DEPENDENCIES = check_game.c $(DEPLIBS) 91check_game_DEPENDENCIES = check_game.c $(DEPLIBS)
90check_http_DEPENDENCIES = check_http.c $(NETOBJS) $(DEPLIBS) 92check_http_DEPENDENCIES = check_http.c $(SSLOBJS) $(NETOBJS) $(DEPLIBS)
91check_hpjd_DEPENDENCIES = check_hpjd.c $(NETOBJS) popen.o $(DEPLIBS) 93check_hpjd_DEPENDENCIES = check_hpjd.c $(NETOBJS) popen.o $(DEPLIBS)
92check_ide_smart_DEPENDENCIES = check_ide_smart.c $(BASEOBJS) $(DEPLIBS) 94check_ide_smart_DEPENDENCIES = check_ide_smart.c $(BASEOBJS) $(DEPLIBS)
93check_ldap_DEPENDENCIES = check_ldap.c $(NETOBJS) $(DEPLIBS) 95check_ldap_DEPENDENCIES = check_ldap.c $(NETOBJS) $(DEPLIBS)
@@ -105,12 +107,12 @@ check_procs_DEPENDENCIES = check_procs.c $(BASEOBJS) popen.o $(DEPLIBS)
105check_radius_DEPENDENCIES = check_radius.c $(NETOBJS) $(DEPLIBS) 107check_radius_DEPENDENCIES = check_radius.c $(NETOBJS) $(DEPLIBS)
106check_real_DEPENDENCIES = check_real.c $(NETOBJS) $(DEPLIBS) 108check_real_DEPENDENCIES = check_real.c $(NETOBJS) $(DEPLIBS)
107check_snmp_DEPENDENCIES = check_snmp.c $(BASEOBJS) popen.o $(DEPLIBS) 109check_snmp_DEPENDENCIES = check_snmp.c $(BASEOBJS) popen.o $(DEPLIBS)
108check_smtp_DEPENDENCIES = check_smtp.c $(NETOBJS) $(DEPLIBS) 110check_smtp_DEPENDENCIES = check_smtp.c $(SSLOBJS) $(NETOBJS) $(DEPLIBS)
109check_ssh_DEPENDENCIES = check_ssh.c $(NETOBJS) $(DEPLIBS) 111check_ssh_DEPENDENCIES = check_ssh.c $(NETOBJS) $(DEPLIBS)
110check_swap_DEPENDENCIES = check_swap.c $(BASEOBJS) popen.o $(DEPLIBS) 112check_swap_DEPENDENCIES = check_swap.c $(BASEOBJS) popen.o $(DEPLIBS)
111check_tcp_DEPENDENCIES = check_tcp.c $(NETOBJS) $(DEPLIBS) 113check_tcp_DEPENDENCIES = check_tcp.c $(SSLOBJS) $(NETOBJS) $(DEPLIBS)
112check_time_DEPENDENCIES = check_time.c $(NETOBJS) $(DEPLIBS) 114check_time_DEPENDENCIES = check_time.c $(NETOBJS) $(DEPLIBS)
113check_udp_DEPENDENCIES = check_udp.c $(NETOBJS) $(DEPLIBS) 115check_udp_DEPENDENCIES = check_udp.c $(SSLOBJS) $(NETOBJS) $(DEPLIBS)
114check_ups_DEPENDENCIES = check_ups.c $(NETOBJS) $(DEPLIBS) 116check_ups_DEPENDENCIES = check_ups.c $(NETOBJS) $(DEPLIBS)
115check_users_DEPENDENCIES = check_users.c $(BASEOBJS) popen.o $(DEPLIBS) 117check_users_DEPENDENCIES = check_users.c $(BASEOBJS) popen.o $(DEPLIBS)
116check_by_ssh_DEPENDENCIES = check_by_ssh.c $(NETOBJS) popen.o $(DEPLIBS) 118check_by_ssh_DEPENDENCIES = check_by_ssh.c $(NETOBJS) popen.o $(DEPLIBS)
@@ -125,6 +127,7 @@ popen.o: popen.c popen.h $(PLUGINHDRS)
125utils.o: utils.c utils.h $(PLUGINHDRS) 127utils.o: utils.c utils.h $(PLUGINHDRS)
126 128
127netutils.o: netutils.c netutils.h $(PLUGINHDRS) 129netutils.o: netutils.c netutils.h $(PLUGINHDRS)
130sslutils.o: sslutils.c netutils.h $(PLUGINHDRS)
128 131
129getaddrinfo.o: getaddrinfo.h $(PLUGINHDRS) 132getaddrinfo.o: getaddrinfo.h $(PLUGINHDRS)
130 133
diff --git a/plugins/netutils.c b/plugins/netutils.c
index 2678f91..db64ef0 100644
--- a/plugins/netutils.c
+++ b/plugins/netutils.c
@@ -234,133 +234,6 @@ np_net_connect (const char *host_name, int port, int *sd, int proto)
234 } 234 }
235} 235}
236 236
237#ifdef HAVE_SSL
238static SSL_CTX *c=NULL;
239static SSL *s=NULL;
240
241int np_net_ssl_init (int sd){
242 SSL_METHOD *m=NULL;
243 /* Initialize SSL context */
244 SSLeay_add_ssl_algorithms ();
245 m = SSLv23_client_method ();
246 SSL_load_error_strings ();
247 OpenSSL_add_all_algorithms();
248 if ((c = SSL_CTX_new (m)) == NULL) {
249 printf (_("CRITICAL - Cannot create SSL context.\n"));
250 return STATE_CRITICAL;
251 }
252 if ((s = SSL_new (c)) != NULL){
253 SSL_set_fd (s, sd);
254 if (SSL_connect(s) == 1){
255 return OK;
256 } else {
257 printf (_("CRITICAL - Cannot make SSL connection "));
258#ifdef USE_OPENSSL /* XXX look into ERR_error_string */
259 ERR_print_errors_fp (stdout);
260#endif /* USE_OPENSSL */
261 }
262 } else {
263 printf (_("CRITICAL - Cannot initiate SSL handshake.\n"));
264 }
265 return STATE_CRITICAL;
266}
267
268void np_net_ssl_cleanup (){
269 if(s){
270 SSL_shutdown (s);
271 SSL_free (s);
272 if(c) SSL_CTX_free (c);
273 }
274}
275
276int np_net_ssl_write(const void *buf, int num){
277 return SSL_write(s, buf, num);
278}
279
280int np_net_ssl_read(void *buf, int num){
281 return SSL_read(s, buf, num);
282}
283
284int np_net_ssl_check_cert(int days_till_exp){
285# ifdef USE_OPENSSL
286 X509 *certificate=NULL;
287 ASN1_STRING *tm;
288 int offset;
289 struct tm stamp;
290 int days_left;
291 char timestamp[17] = "";
292
293 certificate=SSL_get_peer_certificate(s);
294 if(! certificate){
295 printf (_("CRITICAL - Cannot retrieve server certificate.\n"));
296 return STATE_CRITICAL;
297 }
298
299 /* Retrieve timestamp of certificate */
300 tm = X509_get_notAfter (certificate);
301
302 /* Generate tm structure to process timestamp */
303 if (tm->type == V_ASN1_UTCTIME) {
304 if (tm->length < 10) {
305 printf (_("CRITICAL - Wrong time format in certificate.\n"));
306 return STATE_CRITICAL;
307 } else {
308 stamp.tm_year = (tm->data[0] - '0') * 10 + (tm->data[1] - '0');
309 if (stamp.tm_year < 50)
310 stamp.tm_year += 100;
311 offset = 0;
312 }
313 } else {
314 if (tm->length < 12) {
315 printf (_("CRITICAL - Wrong time format in certificate.\n"));
316 return STATE_CRITICAL;
317 } else {
318 stamp.tm_year =
319 (tm->data[0] - '0') * 1000 + (tm->data[1] - '0') * 100 +
320 (tm->data[2] - '0') * 10 + (tm->data[3] - '0');
321 stamp.tm_year -= 1900;
322 offset = 2;
323 }
324 }
325 stamp.tm_mon =
326 (tm->data[2 + offset] - '0') * 10 + (tm->data[3 + offset] - '0') - 1;
327 stamp.tm_mday =
328 (tm->data[4 + offset] - '0') * 10 + (tm->data[5 + offset] - '0');
329 stamp.tm_hour =
330 (tm->data[6 + offset] - '0') * 10 + (tm->data[7 + offset] - '0');
331 stamp.tm_min =
332 (tm->data[8 + offset] - '0') * 10 + (tm->data[9 + offset] - '0');
333 stamp.tm_sec = 0;
334 stamp.tm_isdst = -1;
335
336 days_left = (mktime (&stamp) - time (NULL)) / 86400;
337 snprintf
338 (timestamp, 17, "%02d/%02d/%04d %02d:%02d",
339 stamp.tm_mon + 1,
340 stamp.tm_mday, stamp.tm_year + 1900, stamp.tm_hour, stamp.tm_min);
341
342 if (days_left > 0 && days_left <= days_till_exp) {
343 printf (_("WARNING - Certificate expires in %d day(s) (%s).\n"), days_left, timestamp);
344 return STATE_WARNING;
345 } else if (days_left < 0) {
346 printf (_("CRITICAL - Certificate expired on %s.\n"), timestamp);
347 return STATE_CRITICAL;
348 } else if (days_left == 0) {
349 printf (_("WARNING - Certificate expires today (%s).\n"), timestamp);
350 return STATE_WARNING;
351 }
352
353 printf (_("OK - Certificate will expire on %s.\n"), timestamp);
354 X509_free (certificate);
355 return STATE_OK;
356# else /* ifndef USE_OPENSSL */
357 printf (_("WARNING - Plugin does not support checking certificates.\n"));
358 return STATE_WARNING;
359# endif /* USE_OPENSSL */
360}
361
362#endif /* HAVE_SSL */
363
364int 237int
365send_request (int sd, int proto, const char *send_buffer, char *recv_buffer, int recv_size) 238send_request (int sd, int proto, const char *send_buffer, char *recv_buffer, int recv_size)
366{ 239{
diff --git a/plugins/sslutils.c b/plugins/sslutils.c
new file mode 100644
index 0000000..d785fb7
--- /dev/null
+++ b/plugins/sslutils.c
@@ -0,0 +1,162 @@
1/****************************************************************************
2*
3* Nagios plugins SSL utilities
4*
5* License: GPL
6* Copyright (c) 2005 nagios-plugins team
7*
8* Last Modified: $Date$
9*
10* Description:
11*
12* This file contains common functions for plugins that require SSL.
13*
14* License Information:
15*
16* This program is free software; you can redistribute it and/or modify
17* it under the terms of the GNU General Public License as published by
18* the Free Software Foundation; either version 2 of the License, or
19* (at your option) any later version.
20*
21* This program is distributed in the hope that it will be useful,
22* but WITHOUT ANY WARRANTY; without even the implied warranty of
23* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24* GNU General Public License for more details.
25*
26* You should have received a copy of the GNU General Public License
27* along with this program; if not, write to the Free Software
28* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29*
30* $Id$
31*
32****************************************************************************/
33
34#include "common.h"
35#include "netutils.h"
36
37#ifdef HAVE_SSL
38static SSL_CTX *c=NULL;
39static SSL *s=NULL;
40
41int np_net_ssl_init (int sd){
42 SSL_METHOD *m=NULL;
43 /* Initialize SSL context */
44 SSLeay_add_ssl_algorithms ();
45 m = SSLv23_client_method ();
46 SSL_load_error_strings ();
47 OpenSSL_add_all_algorithms();
48 if ((c = SSL_CTX_new (m)) == NULL) {
49 printf (_("CRITICAL - Cannot create SSL context.\n"));
50 return STATE_CRITICAL;
51 }
52 if ((s = SSL_new (c)) != NULL){
53 SSL_set_fd (s, sd);
54 if (SSL_connect(s) == 1){
55 return OK;
56 } else {
57 printf (_("CRITICAL - Cannot make SSL connection "));
58# ifdef USE_OPENSSL /* XXX look into ERR_error_string */
59 ERR_print_errors_fp (stdout);
60# endif /* USE_OPENSSL */
61 }
62 } else {
63 printf (_("CRITICAL - Cannot initiate SSL handshake.\n"));
64 }
65 return STATE_CRITICAL;
66}
67
68void np_net_ssl_cleanup (){
69 if(s){
70 SSL_shutdown (s);
71 SSL_free (s);
72 if(c) SSL_CTX_free (c);
73 }
74}
75
76int np_net_ssl_write(const void *buf, int num){
77 return SSL_write(s, buf, num);
78}
79
80int np_net_ssl_read(void *buf, int num){
81 return SSL_read(s, buf, num);
82}
83
84int np_net_ssl_check_cert(int days_till_exp){
85# ifdef USE_OPENSSL
86 X509 *certificate=NULL;
87 ASN1_STRING *tm;
88 int offset;
89 struct tm stamp;
90 int days_left;
91 char timestamp[17] = "";
92
93 certificate=SSL_get_peer_certificate(s);
94 if(! certificate){
95 printf (_("CRITICAL - Cannot retrieve server certificate.\n"));
96 return STATE_CRITICAL;
97 }
98
99 /* Retrieve timestamp of certificate */
100 tm = X509_get_notAfter (certificate);
101
102 /* Generate tm structure to process timestamp */
103 if (tm->type == V_ASN1_UTCTIME) {
104 if (tm->length < 10) {
105 printf (_("CRITICAL - Wrong time format in certificate.\n"));
106 return STATE_CRITICAL;
107 } else {
108 stamp.tm_year = (tm->data[0] - '0') * 10 + (tm->data[1] - '0');
109 if (stamp.tm_year < 50)
110 stamp.tm_year += 100;
111 offset = 0;
112 }
113 } else {
114 if (tm->length < 12) {
115 printf (_("CRITICAL - Wrong time format in certificate.\n"));
116 return STATE_CRITICAL;
117 } else {
118 stamp.tm_year =
119 (tm->data[0] - '0') * 1000 + (tm->data[1] - '0') * 100 +
120 (tm->data[2] - '0') * 10 + (tm->data[3] - '0');
121 stamp.tm_year -= 1900;
122 offset = 2;
123 }
124 }
125 stamp.tm_mon =
126 (tm->data[2 + offset] - '0') * 10 + (tm->data[3 + offset] - '0') - 1;
127 stamp.tm_mday =
128 (tm->data[4 + offset] - '0') * 10 + (tm->data[5 + offset] - '0');
129 stamp.tm_hour =
130 (tm->data[6 + offset] - '0') * 10 + (tm->data[7 + offset] - '0');
131 stamp.tm_min =
132 (tm->data[8 + offset] - '0') * 10 + (tm->data[9 + offset] - '0');
133 stamp.tm_sec = 0;
134 stamp.tm_isdst = -1;
135
136 days_left = (mktime (&stamp) - time (NULL)) / 86400;
137 snprintf
138 (timestamp, 17, "%02d/%02d/%04d %02d:%02d",
139 stamp.tm_mon + 1,
140 stamp.tm_mday, stamp.tm_year + 1900, stamp.tm_hour, stamp.tm_min);
141
142 if (days_left > 0 && days_left <= days_till_exp) {
143 printf (_("WARNING - Certificate expires in %d day(s) (%s).\n"), days_left, timestamp);
144 return STATE_WARNING;
145 } else if (days_left < 0) {
146 printf (_("CRITICAL - Certificate expired on %s.\n"), timestamp);
147 return STATE_CRITICAL;
148 } else if (days_left == 0) {
149 printf (_("WARNING - Certificate expires today (%s).\n"), timestamp);
150 return STATE_WARNING;
151 }
152
153 printf (_("OK - Certificate will expire on %s.\n"), timestamp);
154 X509_free (certificate);
155 return STATE_OK;
156# else /* ifndef USE_OPENSSL */
157 printf (_("WARNING - Plugin does not support checking certificates.\n"));
158 return STATE_WARNING;
159# endif /* USE_OPENSSL */
160}
161
162#endif /* HAVE_SSL */