Code

Verbose should not have to exceed 3 as per developement guidelines
[nagiosplug.git] / plugins / sslutils.c
1 /*****************************************************************************
2
3 * Nagios plugins SSL utilities
4
5 * License: GPL
6 * Copyright (c) 2005-2007 Nagios Plugins Development Team
7
8 * Last Modified: $Date$
9
10 * Description:
11
12 * This file contains common functions for plugins that require SSL.
13
14 *
15 * This program is free software: you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation, either version 3 of the License, or
18 * (at your option) any later version.
19
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23 * GNU General Public License for more details.
24
25 * You should have received a copy of the GNU General Public License
26 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
27
28 * $Id$
29
30 *****************************************************************************/
32 #define LOCAL_TIMEOUT_ALARM_HANDLER
33 #include "common.h"
34 #include "netutils.h"
36 #ifdef HAVE_SSL
37 static SSL_CTX *c=NULL;
38 static SSL *s=NULL;
39 static int initialized=0;
41 int np_net_ssl_init (int sd){
42                 if (!initialized) {
43                         /* Initialize SSL context */
44                         SSLeay_add_ssl_algorithms ();
45                         SSL_load_error_strings ();
46                         OpenSSL_add_all_algorithms ();
47                         initialized = 1;
48                 }
49                 if ((c = SSL_CTX_new (SSLv23_client_method ())) == NULL) {
50                                 printf ("%s\n", _("CRITICAL - Cannot create SSL context."));
51                                 return STATE_CRITICAL;
52                 }
53                 if ((s = SSL_new (c)) != NULL){
54                                 SSL_set_fd (s, sd);
55                                 if (SSL_connect(s) == 1){
56                                                 return OK;
57                                 } else {
58                                                 printf ("%s\n", _("CRITICAL - Cannot make SSL connection "));
59 #  ifdef USE_OPENSSL /* XXX look into ERR_error_string */
60                                                 ERR_print_errors_fp (stdout);
61 #  endif /* USE_OPENSSL */
62                                 }
63                 } else {
64                                 printf ("%s\n", _("CRITICAL - Cannot initiate SSL handshake."));
65                 }
66                 return STATE_CRITICAL;
67 }
69 void np_net_ssl_cleanup (){
70                 if(s){
71                                 SSL_shutdown (s);
72                                 SSL_free (s);
73                                 if(c) {
74                                         SSL_CTX_free (c);
75                                         c=NULL;
76                                 }
77                                 s=NULL;
78                 }
79 }
81 int np_net_ssl_write(const void *buf, int num){
82         return SSL_write(s, buf, num);
83 }
85 int np_net_ssl_read(void *buf, int num){
86         return SSL_read(s, buf, num);
87 }
89 int np_net_ssl_check_cert(int days_till_exp){
90 #  ifdef USE_OPENSSL
91         X509 *certificate=NULL;
92         ASN1_STRING *tm;
93         int offset;
94         struct tm stamp;
95         int days_left;
96         char timestamp[17] = "";
98         certificate=SSL_get_peer_certificate(s);
99         if(! certificate){
100                 printf ("%s\n",_("CRITICAL - Cannot retrieve server certificate."));
101                 return STATE_CRITICAL;
102         }
104         /* Retrieve timestamp of certificate */
105         tm = X509_get_notAfter (certificate);
107         /* Generate tm structure to process timestamp */
108         if (tm->type == V_ASN1_UTCTIME) {
109                 if (tm->length < 10) {
110                         printf ("%s\n", _("CRITICAL - Wrong time format in certificate."));
111                         return STATE_CRITICAL;
112                 } else {
113                         stamp.tm_year = (tm->data[0] - '0') * 10 + (tm->data[1] - '0');
114                         if (stamp.tm_year < 50)
115                                 stamp.tm_year += 100;
116                         offset = 0;
117                 }
118         } else {
119                 if (tm->length < 12) {
120                         printf ("%s\n", _("CRITICAL - Wrong time format in certificate."));
121                         return STATE_CRITICAL;
122                 } else {
123                         stamp.tm_year =
124                                 (tm->data[0] - '0') * 1000 + (tm->data[1] - '0') * 100 +
125                                 (tm->data[2] - '0') * 10 + (tm->data[3] - '0');
126                         stamp.tm_year -= 1900;
127                         offset = 2;
128                 }
129         }
130         stamp.tm_mon =
131                 (tm->data[2 + offset] - '0') * 10 + (tm->data[3 + offset] - '0') - 1;
132         stamp.tm_mday =
133                 (tm->data[4 + offset] - '0') * 10 + (tm->data[5 + offset] - '0');
134         stamp.tm_hour =
135                 (tm->data[6 + offset] - '0') * 10 + (tm->data[7 + offset] - '0');
136         stamp.tm_min =
137                 (tm->data[8 + offset] - '0') * 10 + (tm->data[9 + offset] - '0');
138         stamp.tm_sec = 0;
139         stamp.tm_isdst = -1;
141         days_left = (mktime (&stamp) - time (NULL)) / 86400;
142         snprintf
143                 (timestamp, 17, "%02d/%02d/%04d %02d:%02d",
144                  stamp.tm_mon + 1,
145                  stamp.tm_mday, stamp.tm_year + 1900, stamp.tm_hour, stamp.tm_min);
147         if (days_left > 0 && days_left <= days_till_exp) {
148                 printf (_("WARNING - Certificate expires in %d day(s) (%s).\n"), days_left, timestamp);
149                 return STATE_WARNING;
150         } else if (days_left < 0) {
151                 printf (_("CRITICAL - Certificate expired on %s.\n"), timestamp);
152                 return STATE_CRITICAL;
153         } else if (days_left == 0) {
154                 printf (_("WARNING - Certificate expires today (%s).\n"), timestamp);
155                 return STATE_WARNING;
156         }
158         printf (_("OK - Certificate will expire on %s.\n"), timestamp);
159         X509_free (certificate);
160         return STATE_OK;
161 #  else /* ifndef USE_OPENSSL */
162         printf ("%s\n", _("WARNING - Plugin does not support checking certificates."));
163         return STATE_WARNING;
164 #  endif /* USE_OPENSSL */
167 #endif /* HAVE_SSL */