sslutils.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*****************************************************************************
  2. *
  3. * Nagios plugins SSL utilities
  4. *
  5. * License: GPL
  6. * Copyright (c) 2005-2007 Nagios Plugins Development Team
  7. *
  8. * Last Modified: $Date$
  9. *
  10. * Description:
  11. *
  12. * This file contains common functions for plugins that require SSL.
  13. *
  14. *
  15. * This program is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License as published by
  17. * the Free Software Foundation, either version 3 of the License, or
  18. * (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  27. *
  28. * $Id$
  29. *
  30. *****************************************************************************/
  31. #define LOCAL_TIMEOUT_ALARM_HANDLER
  32. #include "common.h"
  33. #include "netutils.h"
  34. #ifdef HAVE_SSL
  35. static SSL_CTX *c=NULL;
  36. static SSL *s=NULL;
  37. static int initialized=0;
  38. int np_net_ssl_init (int sd){
  39. if (!initialized) {
  40. /* Initialize SSL context */
  41. SSLeay_add_ssl_algorithms ();
  42. SSL_load_error_strings ();
  43. OpenSSL_add_all_algorithms ();
  44. initialized = 1;
  45. }
  46. if ((c = SSL_CTX_new (SSLv23_client_method ())) == NULL) {
  47. printf ("%s\n", _("CRITICAL - Cannot create SSL context."));
  48. return STATE_CRITICAL;
  49. }
  50. if ((s = SSL_new (c)) != NULL){
  51. SSL_set_fd (s, sd);
  52. if (SSL_connect(s) == 1){
  53. return OK;
  54. } else {
  55. printf ("%s\n", _("CRITICAL - Cannot make SSL connection "));
  56. # ifdef USE_OPENSSL /* XXX look into ERR_error_string */
  57. ERR_print_errors_fp (stdout);
  58. # endif /* USE_OPENSSL */
  59. }
  60. } else {
  61. printf ("%s\n", _("CRITICAL - Cannot initiate SSL handshake."));
  62. }
  63. return STATE_CRITICAL;
  64. }
  65. void np_net_ssl_cleanup (){
  66. if(s){
  67. SSL_shutdown (s);
  68. SSL_free (s);
  69. if(c) {
  70. SSL_CTX_free (c);
  71. c=NULL;
  72. }
  73. s=NULL;
  74. }
  75. }
  76. int np_net_ssl_write(const void *buf, int num){
  77. return SSL_write(s, buf, num);
  78. }
  79. int np_net_ssl_read(void *buf, int num){
  80. return SSL_read(s, buf, num);
  81. }
  82. int np_net_ssl_check_cert(int days_till_exp){
  83. # ifdef USE_OPENSSL
  84. X509 *certificate=NULL;
  85. ASN1_STRING *tm;
  86. int offset;
  87. struct tm stamp;
  88. int days_left;
  89. char timestamp[17] = "";
  90. certificate=SSL_get_peer_certificate(s);
  91. if(! certificate){
  92. printf ("%s\n",_("CRITICAL - Cannot retrieve server certificate."));
  93. return STATE_CRITICAL;
  94. }
  95. /* Retrieve timestamp of certificate */
  96. tm = X509_get_notAfter (certificate);
  97. /* Generate tm structure to process timestamp */
  98. if (tm->type == V_ASN1_UTCTIME) {
  99. if (tm->length < 10) {
  100. printf ("%s\n", _("CRITICAL - Wrong time format in certificate."));
  101. return STATE_CRITICAL;
  102. } else {
  103. stamp.tm_year = (tm->data[0] - '0') * 10 + (tm->data[1] - '0');
  104. if (stamp.tm_year < 50)
  105. stamp.tm_year += 100;
  106. offset = 0;
  107. }
  108. } else {
  109. if (tm->length < 12) {
  110. printf ("%s\n", _("CRITICAL - Wrong time format in certificate."));
  111. return STATE_CRITICAL;
  112. } else {
  113. stamp.tm_year =
  114. (tm->data[0] - '0') * 1000 + (tm->data[1] - '0') * 100 +
  115. (tm->data[2] - '0') * 10 + (tm->data[3] - '0');
  116. stamp.tm_year -= 1900;
  117. offset = 2;
  118. }
  119. }
  120. stamp.tm_mon =
  121. (tm->data[2 + offset] - '0') * 10 + (tm->data[3 + offset] - '0') - 1;
  122. stamp.tm_mday =
  123. (tm->data[4 + offset] - '0') * 10 + (tm->data[5 + offset] - '0');
  124. stamp.tm_hour =
  125. (tm->data[6 + offset] - '0') * 10 + (tm->data[7 + offset] - '0');
  126. stamp.tm_min =
  127. (tm->data[8 + offset] - '0') * 10 + (tm->data[9 + offset] - '0');
  128. stamp.tm_sec = 0;
  129. stamp.tm_isdst = -1;
  130. days_left = (mktime (&stamp) - time (NULL)) / 86400;
  131. snprintf
  132. (timestamp, 17, "%02d/%02d/%04d %02d:%02d",
  133. stamp.tm_mon + 1,
  134. stamp.tm_mday, stamp.tm_year + 1900, stamp.tm_hour, stamp.tm_min);
  135. if (days_left > 0 && days_left <= days_till_exp) {
  136. printf (_("WARNING - Certificate expires in %d day(s) (%s).\n"), days_left, timestamp);
  137. return STATE_WARNING;
  138. } else if (days_left < 0) {
  139. printf (_("CRITICAL - Certificate expired on %s.\n"), timestamp);
  140. return STATE_CRITICAL;
  141. } else if (days_left == 0) {
  142. printf (_("WARNING - Certificate expires today (%s).\n"), timestamp);
  143. return STATE_WARNING;
  144. }
  145. printf (_("OK - Certificate will expire on %s.\n"), timestamp);
  146. X509_free (certificate);
  147. return STATE_OK;
  148. # else /* ifndef USE_OPENSSL */
  149. printf ("%s\n", _("WARNING - Plugin does not support checking certificates."));
  150. return STATE_WARNING;
  151. # endif /* USE_OPENSSL */
  152. }
  153. #endif /* HAVE_SSL */