1 /*****************************************************************************
2 *
3 * Nagios plugins SSL utilities
4 *
5 * License: GPL
6 * Copyright (c) 2005-2007 Nagios Plugins Development Team
7 *
8 * Description:
9 *
10 * This file contains common functions for plugins that require SSL.
11 *
12 *
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.
17 *
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.
22 *
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/>.
25 *
26 *
27 *****************************************************************************/
29 #define LOCAL_TIMEOUT_ALARM_HANDLER
30 #include "common.h"
31 #include "netutils.h"
33 #ifdef HAVE_SSL
34 static SSL_CTX *c=NULL;
35 static SSL *s=NULL;
36 static int initialized=0;
38 int np_net_ssl_init (int sd) {
39 return np_net_ssl_init_with_hostname(sd, NULL);
40 }
42 int np_net_ssl_init_with_hostname (int sd, char *host_name) {
43 if (!initialized) {
44 /* Initialize SSL context */
45 SSLeay_add_ssl_algorithms ();
46 SSL_load_error_strings ();
47 OpenSSL_add_all_algorithms ();
48 initialized = 1;
49 }
50 if ((c = SSL_CTX_new (SSLv23_client_method ())) == NULL) {
51 printf ("%s\n", _("CRITICAL - Cannot create SSL context."));
52 return STATE_CRITICAL;
53 }
54 if ((s = SSL_new (c)) != NULL){
55 #ifdef SSL_set_tlsext_host_name
56 if (host_name != NULL)
57 SSL_set_tlsext_host_name(s, host_name);
58 #endif
59 SSL_set_fd (s, sd);
60 if (SSL_connect(s) == 1){
61 return OK;
62 } else {
63 printf ("%s\n", _("CRITICAL - Cannot make SSL connection "));
64 # ifdef USE_OPENSSL /* XXX look into ERR_error_string */
65 ERR_print_errors_fp (stdout);
66 # endif /* USE_OPENSSL */
67 }
68 } else {
69 printf ("%s\n", _("CRITICAL - Cannot initiate SSL handshake."));
70 }
71 return STATE_CRITICAL;
72 }
74 void np_net_ssl_cleanup (){
75 if(s){
76 #ifdef SSL_set_tlsext_host_name
77 SSL_set_tlsext_host_name(s, NULL);
78 #endif
79 SSL_shutdown (s);
80 SSL_free (s);
81 if(c) {
82 SSL_CTX_free (c);
83 c=NULL;
84 }
85 s=NULL;
86 }
87 }
89 int np_net_ssl_write(const void *buf, int num){
90 return SSL_write(s, buf, num);
91 }
93 int np_net_ssl_read(void *buf, int num){
94 return SSL_read(s, buf, num);
95 }
97 int np_net_ssl_check_cert(int days_till_exp){
98 # ifdef USE_OPENSSL
99 X509 *certificate=NULL;
100 ASN1_STRING *tm;
101 int offset;
102 struct tm stamp;
103 float time_left;
104 int days_left;
105 char timestamp[17] = "";
107 certificate=SSL_get_peer_certificate(s);
108 if(! certificate){
109 printf ("%s\n",_("CRITICAL - Cannot retrieve server certificate."));
110 return STATE_CRITICAL;
111 }
113 /* Retrieve timestamp of certificate */
114 tm = X509_get_notAfter (certificate);
116 /* Generate tm structure to process timestamp */
117 if (tm->type == V_ASN1_UTCTIME) {
118 if (tm->length < 10) {
119 printf ("%s\n", _("CRITICAL - Wrong time format in certificate."));
120 return STATE_CRITICAL;
121 } else {
122 stamp.tm_year = (tm->data[0] - '0') * 10 + (tm->data[1] - '0');
123 if (stamp.tm_year < 50)
124 stamp.tm_year += 100;
125 offset = 0;
126 }
127 } else {
128 if (tm->length < 12) {
129 printf ("%s\n", _("CRITICAL - Wrong time format in certificate."));
130 return STATE_CRITICAL;
131 } else {
132 stamp.tm_year =
133 (tm->data[0] - '0') * 1000 + (tm->data[1] - '0') * 100 +
134 (tm->data[2] - '0') * 10 + (tm->data[3] - '0');
135 stamp.tm_year -= 1900;
136 offset = 2;
137 }
138 }
139 stamp.tm_mon =
140 (tm->data[2 + offset] - '0') * 10 + (tm->data[3 + offset] - '0') - 1;
141 stamp.tm_mday =
142 (tm->data[4 + offset] - '0') * 10 + (tm->data[5 + offset] - '0');
143 stamp.tm_hour =
144 (tm->data[6 + offset] - '0') * 10 + (tm->data[7 + offset] - '0');
145 stamp.tm_min =
146 (tm->data[8 + offset] - '0') * 10 + (tm->data[9 + offset] - '0');
147 stamp.tm_sec = 0;
148 stamp.tm_isdst = -1;
150 time_left = difftime(timegm(&stamp), time(NULL));
151 days_left = time_left / 86400;
152 snprintf
153 (timestamp, 17, "%02d/%02d/%04d %02d:%02d",
154 stamp.tm_mon + 1,
155 stamp.tm_mday, stamp.tm_year + 1900, stamp.tm_hour, stamp.tm_min);
157 if (days_left > 0 && days_left <= days_till_exp) {
158 printf (_("WARNING - Certificate expires in %d day(s) (%s).\n"), days_left, timestamp);
159 return STATE_WARNING;
160 } else if (time_left < 0) {
161 printf (_("CRITICAL - Certificate expired on %s.\n"), timestamp);
162 return STATE_CRITICAL;
163 } else if (days_left == 0) {
164 printf (_("WARNING - Certificate expires today (%s).\n"), timestamp);
165 return STATE_WARNING;
166 }
168 printf (_("OK - Certificate will expire on %s.\n"), timestamp);
169 X509_free (certificate);
170 return STATE_OK;
171 # else /* ifndef USE_OPENSSL */
172 printf ("%s\n", _("WARNING - Plugin does not support checking certificates."));
173 return STATE_WARNING;
174 # endif /* USE_OPENSSL */
175 }
177 #endif /* HAVE_SSL */