Code

Test installs into temporary directories
[nagiosplug.git] / plugins / sslutils.c
1 /****************************************************************************
2 *
3 * Nagios plugins SSL utilities
4 *
5 * License: GPL
6 * Copyright (c) 2005 nagios-plugins team
7 *
8 * Last Modified: $Date$
9 *
10 * Description:
11 *
12 * This file contains common functions for plugins that require SSL.
13 *
14 * License Information:
15 *
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.
20 *
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.
25 *
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.
29 *
30 * $Id$
31 *
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;
42 int np_net_ssl_init (int sd){
43                 SSL_METHOD *m=NULL;
44                 /* Initialize SSL context */
45                 SSLeay_add_ssl_algorithms ();
46                 m = SSLv23_client_method ();
47                 SSL_load_error_strings ();
48                 OpenSSL_add_all_algorithms();
49                 if ((c = SSL_CTX_new (m)) == 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) SSL_CTX_free (c);
74                 }
75 }
77 int np_net_ssl_write(const void *buf, int num){
78         return SSL_write(s, buf, num);
79 }
81 int np_net_ssl_read(void *buf, int num){
82         return SSL_read(s, buf, num);
83 }
85 int np_net_ssl_check_cert(int days_till_exp){
86 #  ifdef USE_OPENSSL
87         X509 *certificate=NULL;
88         ASN1_STRING *tm;
89         int offset;
90         struct tm stamp;
91         int days_left;
92         char timestamp[17] = "";
94         certificate=SSL_get_peer_certificate(s);
95         if(! certificate){
96                 printf ("%s\n",_("CRITICAL - Cannot retrieve server certificate."));
97                 return STATE_CRITICAL;
98         }
100         /* Retrieve timestamp of certificate */
101         tm = X509_get_notAfter (certificate);
103         /* Generate tm structure to process timestamp */
104         if (tm->type == V_ASN1_UTCTIME) {
105                 if (tm->length < 10) {
106                         printf ("%s\n", _("CRITICAL - Wrong time format in certificate."));
107                         return STATE_CRITICAL;
108                 } else {
109                         stamp.tm_year = (tm->data[0] - '0') * 10 + (tm->data[1] - '0');
110                         if (stamp.tm_year < 50)
111                                 stamp.tm_year += 100;
112                         offset = 0;
113                 }
114         } else {
115                 if (tm->length < 12) {
116                         printf ("%s\n", _("CRITICAL - Wrong time format in certificate."));
117                         return STATE_CRITICAL;
118                 } else {
119                         stamp.tm_year =
120                                 (tm->data[0] - '0') * 1000 + (tm->data[1] - '0') * 100 +
121                                 (tm->data[2] - '0') * 10 + (tm->data[3] - '0');
122                         stamp.tm_year -= 1900;
123                         offset = 2;
124                 }
125         }
126         stamp.tm_mon =
127                 (tm->data[2 + offset] - '0') * 10 + (tm->data[3 + offset] - '0') - 1;
128         stamp.tm_mday =
129                 (tm->data[4 + offset] - '0') * 10 + (tm->data[5 + offset] - '0');
130         stamp.tm_hour =
131                 (tm->data[6 + offset] - '0') * 10 + (tm->data[7 + offset] - '0');
132         stamp.tm_min =
133                 (tm->data[8 + offset] - '0') * 10 + (tm->data[9 + offset] - '0');
134         stamp.tm_sec = 0;
135         stamp.tm_isdst = -1;
137         days_left = (mktime (&stamp) - time (NULL)) / 86400;
138         snprintf
139                 (timestamp, 17, "%02d/%02d/%04d %02d:%02d",
140                  stamp.tm_mon + 1,
141                  stamp.tm_mday, stamp.tm_year + 1900, stamp.tm_hour, stamp.tm_min);
143         if (days_left > 0 && days_left <= days_till_exp) {
144                 printf (_("WARNING - Certificate expires in %d day(s) (%s).\n"), days_left, timestamp);
145                 return STATE_WARNING;
146         } else if (days_left < 0) {
147                 printf (_("CRITICAL - Certificate expired on %s.\n"), timestamp);
148                 return STATE_CRITICAL;
149         } else if (days_left == 0) {
150                 printf (_("WARNING - Certificate expires today (%s).\n"), timestamp);
151                 return STATE_WARNING;
152         }
154         printf (_("OK - Certificate will expire on %s.\n"), timestamp);
155         X509_free (certificate);
156         return STATE_OK;
157 #  else /* ifndef USE_OPENSSL */
158         printf ("%s\n", _("WARNING - Plugin does not support checking certificates."));
159         return STATE_WARNING;
160 #  endif /* USE_OPENSSL */
163 #endif /* HAVE_SSL */