1 /****************************************************************************
3 * Nagios plugins SSL utilities
6 * Copyright (c) 2005 nagios-plugins team
8 * Last Modified: $Date$
12 * This file contains common functions for plugins that require SSL.
14 * License Information:
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.
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.
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.
32 ****************************************************************************/
34 #define LOCAL_TIMEOUT_ALARM_HANDLER
39 static SSL_CTX
*c
=NULL
;
41 static int initialized
=0;
43 int np_net_ssl_init (int sd
){
45 /* Initialize SSL context */
46 SSLeay_add_ssl_algorithms ();
47 SSL_load_error_strings ();
48 OpenSSL_add_all_algorithms ();
51 if ((c
= SSL_CTX_new (SSLv23_client_method ())) == NULL
) {
52 printf ("%s\n", _("CRITICAL - Cannot create SSL context."));
53 return STATE_CRITICAL
;
55 if ((s
= SSL_new (c
)) != NULL
){
57 if (SSL_connect(s
) == 1){
60 printf ("%s\n", _("CRITICAL - Cannot make SSL connection "));
61 # ifdef USE_OPENSSL /* XXX look into ERR_error_string */
62 ERR_print_errors_fp (stdout
);
63 # endif /* USE_OPENSSL */
66 printf ("%s\n", _("CRITICAL - Cannot initiate SSL handshake."));
68 return STATE_CRITICAL
;
71 void np_net_ssl_cleanup (){
83 int np_net_ssl_write(const void *buf
, int num
){
84 return SSL_write(s
, buf
, num
);
87 int np_net_ssl_read(void *buf
, int num
){
88 return SSL_read(s
, buf
, num
);
91 int np_net_ssl_check_cert(int days_till_exp
){
93 X509
*certificate
=NULL
;
98 char timestamp
[17] = "";
100 certificate
=SSL_get_peer_certificate(s
);
102 printf ("%s\n",_("CRITICAL - Cannot retrieve server certificate."));
103 return STATE_CRITICAL
;
106 /* Retrieve timestamp of certificate */
107 tm
= X509_get_notAfter (certificate
);
109 /* Generate tm structure to process timestamp */
110 if (tm
->type
== V_ASN1_UTCTIME
) {
111 if (tm
->length
< 10) {
112 printf ("%s\n", _("CRITICAL - Wrong time format in certificate."));
113 return STATE_CRITICAL
;
115 stamp
.tm_year
= (tm
->data
[0] - '0') * 10 + (tm
->data
[1] - '0');
116 if (stamp
.tm_year
< 50)
117 stamp
.tm_year
+= 100;
121 if (tm
->length
< 12) {
122 printf ("%s\n", _("CRITICAL - Wrong time format in certificate."));
123 return STATE_CRITICAL
;
126 (tm
->data
[0] - '0') * 1000 + (tm
->data
[1] - '0') * 100 +
127 (tm
->data
[2] - '0') * 10 + (tm
->data
[3] - '0');
128 stamp
.tm_year
-= 1900;
133 (tm
->data
[2 + offset
] - '0') * 10 + (tm
->data
[3 + offset
] - '0') - 1;
135 (tm
->data
[4 + offset
] - '0') * 10 + (tm
->data
[5 + offset
] - '0');
137 (tm
->data
[6 + offset
] - '0') * 10 + (tm
->data
[7 + offset
] - '0');
139 (tm
->data
[8 + offset
] - '0') * 10 + (tm
->data
[9 + offset
] - '0');
143 days_left
= (mktime (&stamp
) - time (NULL
)) / 86400;
145 (timestamp
, 17, "%02d/%02d/%04d %02d:%02d",
147 stamp
.tm_mday
, stamp
.tm_year
+ 1900, stamp
.tm_hour
, stamp
.tm_min
);
149 if (days_left
> 0 && days_left
<= days_till_exp
) {
150 printf (_("WARNING - Certificate expires in %d day(s) (%s).\n"), days_left
, timestamp
);
151 return STATE_WARNING
;
152 } else if (days_left
< 0) {
153 printf (_("CRITICAL - Certificate expired on %s.\n"), timestamp
);
154 return STATE_CRITICAL
;
155 } else if (days_left
== 0) {
156 printf (_("WARNING - Certificate expires today (%s).\n"), timestamp
);
157 return STATE_WARNING
;
160 printf (_("OK - Certificate will expire on %s.\n"), timestamp
);
161 X509_free (certificate
);
163 # else /* ifndef USE_OPENSSL */
164 printf ("%s\n", _("WARNING - Plugin does not support checking certificates."));
165 return STATE_WARNING
;
166 # endif /* USE_OPENSSL */
169 #endif /* HAVE_SSL */