Code

The "-e" option now accepts a comma-delimited list of expected status
[nagiosplug.git] / plugins / sslutils.c
index 66c729956050f7acb964e704d2904f7a216f95e6..3d4f90166dd7d3cb1754dfa5516164e3337ff7eb 100644 (file)
@@ -1,35 +1,33 @@
-/****************************************************************************
-*
+/*****************************************************************************
+* 
 * Nagios plugins SSL utilities
-*
+* 
 * License: GPL
-* Copyright (c) 2005 nagios-plugins team
-*
+* Copyright (c) 2005-2007 Nagios Plugins Development Team
+* 
 * Last Modified: $Date$
-*
+* 
 * Description:
-*
+* 
 * This file contains common functions for plugins that require SSL.
+* 
 *
-* License Information:
-*
-* This program is free software; you can redistribute it and/or modify
+* This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
-* the Free Software Foundation; either version 2 of the License, or
+* the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
-*
+* 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
-*
+* 
 * You should have received a copy of the GNU General Public License
-* along with this program; if not, write to the Free Software
-* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
-*
+* along with this program.  If not, see <http://www.gnu.org/licenses/>.
+* 
 * $Id$
-*
-****************************************************************************/
+* 
+*****************************************************************************/
 
 #define LOCAL_TIMEOUT_ALARM_HANDLER
 #include "common.h"
 #ifdef HAVE_SSL
 static SSL_CTX *c=NULL;
 static SSL *s=NULL;
+static int initialized=0;
 
 int np_net_ssl_init (int sd){
-               SSL_METHOD *m=NULL;
-               /* Initialize SSL context */
-               SSLeay_add_ssl_algorithms ();
-               m = SSLv23_client_method ();
-               SSL_load_error_strings ();
-               OpenSSL_add_all_algorithms();
-               if ((c = SSL_CTX_new (m)) == NULL) {
-                               printf (_("CRITICAL - Cannot create SSL context.\n"));
+               if (!initialized) {
+                       /* Initialize SSL context */
+                       SSLeay_add_ssl_algorithms ();
+                       SSL_load_error_strings ();
+                       OpenSSL_add_all_algorithms ();
+                       initialized = 1;
+               }
+               if ((c = SSL_CTX_new (SSLv23_client_method ())) == NULL) {
+                               printf ("%s\n", _("CRITICAL - Cannot create SSL context."));
                                return STATE_CRITICAL;
                }
                if ((s = SSL_new (c)) != NULL){
@@ -55,13 +55,13 @@ int np_net_ssl_init (int sd){
                                if (SSL_connect(s) == 1){
                                                return OK;
                                } else {
-                                               printf (_("CRITICAL - Cannot make SSL connection "));
+                                               printf ("%s\n", _("CRITICAL - Cannot make SSL connection "));
 #  ifdef USE_OPENSSL /* XXX look into ERR_error_string */
                                                ERR_print_errors_fp (stdout);
 #  endif /* USE_OPENSSL */
                                }
                } else {
-                               printf (_("CRITICAL - Cannot initiate SSL handshake.\n"));
+                               printf ("%s\n", _("CRITICAL - Cannot initiate SSL handshake."));
                }
                return STATE_CRITICAL;
 }
@@ -70,7 +70,11 @@ void np_net_ssl_cleanup (){
                if(s){
                                SSL_shutdown (s);
                                SSL_free (s);
-                               if(c) SSL_CTX_free (c);
+                               if(c) {
+                                       SSL_CTX_free (c);
+                                       c=NULL;
+                               }
+                               s=NULL;
                }
 }
 
@@ -93,7 +97,7 @@ int np_net_ssl_check_cert(int days_till_exp){
 
        certificate=SSL_get_peer_certificate(s);
        if(! certificate){
-               printf (_("CRITICAL - Cannot retrieve server certificate.\n"));
+               printf ("%s\n",_("CRITICAL - Cannot retrieve server certificate."));
                return STATE_CRITICAL;
        }
 
@@ -103,7 +107,7 @@ int np_net_ssl_check_cert(int days_till_exp){
        /* Generate tm structure to process timestamp */
        if (tm->type == V_ASN1_UTCTIME) {
                if (tm->length < 10) {
-                       printf (_("CRITICAL - Wrong time format in certificate.\n"));
+                       printf ("%s\n", _("CRITICAL - Wrong time format in certificate."));
                        return STATE_CRITICAL;
                } else {
                        stamp.tm_year = (tm->data[0] - '0') * 10 + (tm->data[1] - '0');
@@ -113,7 +117,7 @@ int np_net_ssl_check_cert(int days_till_exp){
                }
        } else {
                if (tm->length < 12) {
-                       printf (_("CRITICAL - Wrong time format in certificate.\n"));
+                       printf ("%s\n", _("CRITICAL - Wrong time format in certificate."));
                        return STATE_CRITICAL;
                } else {
                        stamp.tm_year =
@@ -155,7 +159,7 @@ int np_net_ssl_check_cert(int days_till_exp){
        X509_free (certificate);
        return STATE_OK;
 #  else /* ifndef USE_OPENSSL */
-       printf (_("WARNING - Plugin does not support checking certificates.\n"));
+       printf ("%s\n", _("WARNING - Plugin does not support checking certificates."));
        return STATE_WARNING;
 #  endif /* USE_OPENSSL */
 }