1 /*****************************************************************************
3 * Nagios plugins SSL utilities
6 * Copyright (c) 2005-2007 Nagios Plugins Development Team
10 * This file contains common functions for plugins that require SSL.
13 * This program is free software: you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation, either version 3 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program. If not, see <http://www.gnu.org/licenses/>.
27 *****************************************************************************/
29 #define LOCAL_TIMEOUT_ALARM_HANDLER
34 static SSL_CTX
*c
=NULL
;
36 static int initialized
=0;
38 int np_net_ssl_init (int sd
){
40 /* Initialize SSL context */
41 SSLeay_add_ssl_algorithms ();
42 SSL_load_error_strings ();
43 OpenSSL_add_all_algorithms ();
46 if ((c
= SSL_CTX_new (SSLv23_client_method ())) == NULL
) {
47 printf ("%s\n", _("CRITICAL - Cannot create SSL context."));
48 return STATE_CRITICAL
;
50 if ((s
= SSL_new (c
)) != NULL
){
52 if (SSL_connect(s
) == 1){
55 printf ("%s\n", _("CRITICAL - Cannot make SSL connection "));
56 # ifdef USE_OPENSSL /* XXX look into ERR_error_string */
57 ERR_print_errors_fp (stdout
);
58 # endif /* USE_OPENSSL */
61 printf ("%s\n", _("CRITICAL - Cannot initiate SSL handshake."));
63 return STATE_CRITICAL
;
66 void np_net_ssl_cleanup (){
78 int np_net_ssl_write(const void *buf
, int num
){
79 return SSL_write(s
, buf
, num
);
82 int np_net_ssl_read(void *buf
, int num
){
83 return SSL_read(s
, buf
, num
);
86 int np_net_ssl_check_cert(int days_till_exp
){
88 X509
*certificate
=NULL
;
94 char timestamp
[17] = "";
96 certificate
=SSL_get_peer_certificate(s
);
98 printf ("%s\n",_("CRITICAL - Cannot retrieve server certificate."));
99 return STATE_CRITICAL
;
102 /* Retrieve timestamp of certificate */
103 tm
= X509_get_notAfter (certificate
);
105 /* Generate tm structure to process timestamp */
106 if (tm
->type
== V_ASN1_UTCTIME
) {
107 if (tm
->length
< 10) {
108 printf ("%s\n", _("CRITICAL - Wrong time format in certificate."));
109 return STATE_CRITICAL
;
111 stamp
.tm_year
= (tm
->data
[0] - '0') * 10 + (tm
->data
[1] - '0');
112 if (stamp
.tm_year
< 50)
113 stamp
.tm_year
+= 100;
117 if (tm
->length
< 12) {
118 printf ("%s\n", _("CRITICAL - Wrong time format in certificate."));
119 return STATE_CRITICAL
;
122 (tm
->data
[0] - '0') * 1000 + (tm
->data
[1] - '0') * 100 +
123 (tm
->data
[2] - '0') * 10 + (tm
->data
[3] - '0');
124 stamp
.tm_year
-= 1900;
129 (tm
->data
[2 + offset
] - '0') * 10 + (tm
->data
[3 + offset
] - '0') - 1;
131 (tm
->data
[4 + offset
] - '0') * 10 + (tm
->data
[5 + offset
] - '0');
133 (tm
->data
[6 + offset
] - '0') * 10 + (tm
->data
[7 + offset
] - '0');
135 (tm
->data
[8 + offset
] - '0') * 10 + (tm
->data
[9 + offset
] - '0');
139 time_left
= difftime(timegm(&stamp
), time(NULL
));
140 days_left
= time_left
/ 86400;
142 (timestamp
, 17, "%02d/%02d/%04d %02d:%02d",
144 stamp
.tm_mday
, stamp
.tm_year
+ 1900, stamp
.tm_hour
, stamp
.tm_min
);
146 if (days_left
> 0 && days_left
<= days_till_exp
) {
147 printf (_("WARNING - Certificate expires in %d day(s) (%s).\n"), days_left
, timestamp
);
148 return STATE_WARNING
;
149 } else if (time_left
< 0) {
150 printf (_("CRITICAL - Certificate expired on %s.\n"), timestamp
);
151 return STATE_CRITICAL
;
152 } else if (days_left
== 0) {
153 printf (_("WARNING - Certificate expires today (%s).\n"), timestamp
);
154 return STATE_WARNING
;
157 printf (_("OK - Certificate will expire on %s.\n"), timestamp
);
158 X509_free (certificate
);
160 # else /* ifndef USE_OPENSSL */
161 printf ("%s\n", _("WARNING - Plugin does not support checking certificates."));
162 return STATE_WARNING
;
163 # endif /* USE_OPENSSL */
166 #endif /* HAVE_SSL */