Code

Fixed crash from check_nt when -H unspecified or given multiple times (sf.net #273175...
[nagiosplug.git] / plugins / sslutils.c
index 84a1c8fc9c121f1248bd551b4f12f9ae54f0e71d..1d4ef94a210683b55bf3cde64a8cc86d50ad1bac 100644 (file)
@@ -1,35 +1,30 @@
-/****************************************************************************
-*
+/*****************************************************************************
+* 
 * Nagios plugins SSL utilities
-*
+* 
 * License: GPL
-* Copyright (c) 2005 nagios-plugins team
-*
-* Last Modified: $Date$
-*
+* Copyright (c) 2005-2007 Nagios Plugins Development Team
+* 
 * 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.
-*
-* $Id$
-*
-****************************************************************************/
+* along with this program.  If not, see <http://www.gnu.org/licenses/>.
+* 
+* 
+*****************************************************************************/
 
 #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) {
+               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;
                }
@@ -92,6 +89,7 @@ int np_net_ssl_check_cert(int days_till_exp){
         ASN1_STRING *tm;
        int offset;
        struct tm stamp;
+       float time_left;
        int days_left;
        char timestamp[17] = "";
 
@@ -138,7 +136,8 @@ int np_net_ssl_check_cert(int days_till_exp){
        stamp.tm_sec = 0;
        stamp.tm_isdst = -1;
 
-       days_left = (mktime (&stamp) - time (NULL)) / 86400;
+       time_left = difftime(timegm(&stamp), time(NULL));
+       days_left = time_left / 86400;
        snprintf
                (timestamp, 17, "%02d/%02d/%04d %02d:%02d",
                 stamp.tm_mon + 1,
@@ -147,7 +146,7 @@ int np_net_ssl_check_cert(int days_till_exp){
        if (days_left > 0 && days_left <= days_till_exp) {
                printf (_("WARNING - Certificate expires in %d day(s) (%s).\n"), days_left, timestamp);
                return STATE_WARNING;
-       } else if (days_left < 0) {
+       } else if (time_left < 0) {
                printf (_("CRITICAL - Certificate expired on %s.\n"), timestamp);
                return STATE_CRITICAL;
        } else if (days_left == 0) {