sslutils.c 4.6 KB

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