Code

Add perfdata to check_ssh (#3244097 - Marco Beck)
[nagiosplug.git] / plugins / sslutils.c
index cf3c40322ada8078de3ff80a735c567ff35ecd7f..0bc61ed330fafcdb0720045cdd04e83b080e9ce9 100644 (file)
@@ -1,36 +1,32 @@
-/****************************************************************************
-*
+/*****************************************************************************
+* 
 * Nagios plugins SSL utilities
-*
+* 
 * License: GPL
-* Copyright (c) 2005 nagios-plugins team
-*
-* Last Modified: $Date$
-*
+* Copyright (c) 2005-2010 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 MAX_CN_LENGTH 256
 #define LOCAL_TIMEOUT_ALARM_HANDLER
 #include "common.h"
 #include "netutils.h"
 #ifdef HAVE_SSL
 static SSL_CTX *c=NULL;
 static SSL *s=NULL;
+static int initialized=0;
+
+int np_net_ssl_init (int sd) {
+               return np_net_ssl_init_with_hostname(sd, NULL);
+}
 
-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) {
+int np_net_ssl_init_with_hostname (int sd, char *host_name) {
+               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){
+#ifdef SSL_set_tlsext_host_name
+                               if (host_name != NULL)
+                                       SSL_set_tlsext_host_name(s, host_name);
+#endif
                                SSL_set_fd (s, sd);
                                if (SSL_connect(s) == 1){
                                                return OK;
@@ -68,10 +74,15 @@ int np_net_ssl_init (int sd){
 
 void np_net_ssl_cleanup (){
                if(s){
+#ifdef SSL_set_tlsext_host_name
+                               SSL_set_tlsext_host_name(s, NULL);
+#endif
                                SSL_shutdown (s);
                                SSL_free (s);
-                               if(c) SSL_CTX_free (c);
-                               c=NULL;
+                               if(c) {
+                                       SSL_CTX_free (c);
+                                       c=NULL;
+                               }
                                s=NULL;
                }
 }
@@ -87,9 +98,15 @@ int np_net_ssl_read(void *buf, int num){
 int np_net_ssl_check_cert(int days_till_exp){
 #  ifdef USE_OPENSSL
        X509 *certificate=NULL;
-        ASN1_STRING *tm;
+       X509_NAME *subj=NULL;
+       char cn[MAX_CN_LENGTH]= "";
+       int cnlen =-1;
+       int status=STATE_UNKNOWN;
+
+       ASN1_STRING *tm;
        int offset;
        struct tm stamp;
+       float time_left;
        int days_left;
        char timestamp[17] = "";
 
@@ -99,6 +116,17 @@ int np_net_ssl_check_cert(int days_till_exp){
                return STATE_CRITICAL;
        }
 
+       /* Extract CN from certificate subject */
+       subj=X509_get_subject_name(certificate);
+
+       if(! subj){
+               printf ("%s\n",_("CRITICAL - Cannot retrieve certificate subject."));
+               return STATE_CRITICAL;
+       }
+       cnlen = X509_NAME_get_text_by_NID (subj, NID_commonName, cn, sizeof(cn));
+       if ( cnlen == -1 )
+               strcpy(cn , _("Unknown CN"));
+
        /* Retrieve timestamp of certificate */
        tm = X509_get_notAfter (certificate);
 
@@ -136,26 +164,28 @@ 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,
                 stamp.tm_mday, stamp.tm_year + 1900, stamp.tm_hour, stamp.tm_min);
 
        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) {
-               printf (_("CRITICAL - Certificate expired on %s.\n"), timestamp);
-               return STATE_CRITICAL;
+               printf (_("WARNING - Certificate '%s' expires in %d day(s) (%s).\n"), cn, days_left, timestamp);
+               status=STATE_WARNING;
+       } else if (time_left < 0) {
+               printf (_("CRITICAL - Certificate '%s' expired on %s.\n"), cn, timestamp);
+               status=STATE_CRITICAL;
        } else if (days_left == 0) {
-               printf (_("WARNING - Certificate expires today (%s).\n"), timestamp);
-               return STATE_WARNING;
+               printf (_("WARNING - Certificate '%s' expires today (%s).\n"), cn, timestamp);
+               status=STATE_WARNING;
+       } else {
+               printf (_("OK - Certificate '%s' will expire on %s.\n"), cn, timestamp);
+               status=STATE_OK;
        }
-
-       printf (_("OK - Certificate will expire on %s.\n"), timestamp);
        X509_free (certificate);
-       return STATE_OK;
+       return status;
 #  else /* ifndef USE_OPENSSL */
        printf ("%s\n", _("WARNING - Plugin does not support checking certificates."));
        return STATE_WARNING;