sslutils.c 4.5 KB

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