Changed default test values for check_dns (using nagios.com)
[monitoring-plugins.git] / plugins / sslutils.c
blob8d2e93c7977644cf18f6e76d5f92fb94cfbb9dbe
1 /****************************************************************************
3 * Nagios plugins SSL utilities
5 * License: GPL
6 * Copyright (c) 2005 nagios-plugins team
8 * Last Modified: $Date$
10 * Description:
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.
30 * $Id$
32 ****************************************************************************/
34 #define LOCAL_TIMEOUT_ALARM_HANDLER
35 #include "common.h"
36 #include "netutils.h"
38 #ifdef HAVE_SSL
39 static SSL_CTX *c=NULL;
40 static SSL *s=NULL;
41 static int initialized=0;
43 int np_net_ssl_init (int sd){
44 if (!initialized) {
45 /* Initialize SSL context */
46 SSLeay_add_ssl_algorithms ();
47 SSL_load_error_strings ();
48 OpenSSL_add_all_algorithms ();
49 initialized = 1;
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){
56 SSL_set_fd (s, sd);
57 if (SSL_connect(s) == 1){
58 return OK;
59 } else {
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 */
65 } else {
66 printf ("%s\n", _("CRITICAL - Cannot initiate SSL handshake."));
68 return STATE_CRITICAL;
71 void np_net_ssl_cleanup (){
72 if(s){
73 SSL_shutdown (s);
74 SSL_free (s);
75 if(c) {
76 SSL_CTX_free (c);
77 c=NULL;
79 s=NULL;
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){
92 # ifdef USE_OPENSSL
93 X509 *certificate=NULL;
94 ASN1_STRING *tm;
95 int offset;
96 struct tm stamp;
97 int days_left;
98 char timestamp[17] = "";
100 certificate=SSL_get_peer_certificate(s);
101 if(! certificate){
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;
114 } else {
115 stamp.tm_year = (tm->data[0] - '0') * 10 + (tm->data[1] - '0');
116 if (stamp.tm_year < 50)
117 stamp.tm_year += 100;
118 offset = 0;
120 } else {
121 if (tm->length < 12) {
122 printf ("%s\n", _("CRITICAL - Wrong time format in certificate."));
123 return STATE_CRITICAL;
124 } else {
125 stamp.tm_year =
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;
129 offset = 2;
132 stamp.tm_mon =
133 (tm->data[2 + offset] - '0') * 10 + (tm->data[3 + offset] - '0') - 1;
134 stamp.tm_mday =
135 (tm->data[4 + offset] - '0') * 10 + (tm->data[5 + offset] - '0');
136 stamp.tm_hour =
137 (tm->data[6 + offset] - '0') * 10 + (tm->data[7 + offset] - '0');
138 stamp.tm_min =
139 (tm->data[8 + offset] - '0') * 10 + (tm->data[9 + offset] - '0');
140 stamp.tm_sec = 0;
141 stamp.tm_isdst = -1;
143 days_left = (mktime (&stamp) - time (NULL)) / 86400;
144 snprintf
145 (timestamp, 17, "%02d/%02d/%04d %02d:%02d",
146 stamp.tm_mon + 1,
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);
162 return STATE_OK;
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 */