cmCurl.cxx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2015 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmCurl.h"
  11. #include "cmSystemTools.h"
  12. // curl versions before 7.21.5 did not provide this error code
  13. #if defined(LIBCURL_VERSION_NUM) && LIBCURL_VERSION_NUM < 0x071505
  14. # define CURLE_NOT_BUILT_IN 4
  15. #endif
  16. #define check_curl_result(result, errstr) \
  17. if (result != CURLE_OK && result != CURLE_NOT_BUILT_IN) \
  18. { \
  19. e += e.empty()? "" : "\n"; \
  20. e += errstr; \
  21. e += ::curl_easy_strerror(result); \
  22. }
  23. //----------------------------------------------------------------------------
  24. std::string cmCurlSetCAInfo(::CURL *curl, const char* cafile)
  25. {
  26. std::string e;
  27. if(cafile && *cafile)
  28. {
  29. ::CURLcode res = ::curl_easy_setopt(curl, CURLOPT_CAINFO, cafile);
  30. check_curl_result(res, "Unable to set TLS/SSL Verify CAINFO: ");
  31. }
  32. #if !defined(CMAKE_USE_SYSTEM_CURL) && \
  33. !defined(_WIN32) && !defined(__APPLE__) && \
  34. !defined(CURL_CA_BUNDLE) && !defined(CURL_CA_PATH)
  35. # define CMAKE_CAFILE_FEDORA "/etc/pki/tls/certs/ca-bundle.crt"
  36. else if(cmSystemTools::FileExists(CMAKE_CAFILE_FEDORA, true))
  37. {
  38. ::CURLcode res =
  39. ::curl_easy_setopt(curl, CURLOPT_CAINFO, CMAKE_CAFILE_FEDORA);
  40. check_curl_result(res, "Unable to set TLS/SSL Verify CAINFO: ");
  41. }
  42. # undef CMAKE_CAFILE_FEDORA
  43. else
  44. {
  45. # define CMAKE_CAFILE_COMMON "/etc/ssl/certs/ca-certificates.crt"
  46. if(cmSystemTools::FileExists(CMAKE_CAFILE_COMMON, true))
  47. {
  48. ::CURLcode res =
  49. ::curl_easy_setopt(curl, CURLOPT_CAINFO, CMAKE_CAFILE_COMMON);
  50. check_curl_result(res, "Unable to set TLS/SSL Verify CAINFO: ");
  51. }
  52. # undef CMAKE_CAFILE_COMMON
  53. # define CMAKE_CAPATH_COMMON "/etc/ssl/certs"
  54. if(cmSystemTools::FileIsDirectory(CMAKE_CAPATH_COMMON))
  55. {
  56. ::CURLcode res =
  57. ::curl_easy_setopt(curl, CURLOPT_CAPATH, CMAKE_CAPATH_COMMON);
  58. check_curl_result(res, "Unable to set TLS/SSL Verify CAPATH: ");
  59. }
  60. # undef CMAKE_CAPATH_COMMON
  61. }
  62. #endif
  63. return e;
  64. }