cmCurl.cxx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmCurl.h"
  4. #include "cmThirdParty.h"
  5. #if !defined(CMAKE_USE_SYSTEM_CURL) && !defined(_WIN32) && \
  6. !defined(__APPLE__) && !defined(CURL_CA_BUNDLE) && !defined(CURL_CA_PATH)
  7. #define CMAKE_FIND_CAFILE
  8. #include "cmSystemTools.h"
  9. #endif
  10. // curl versions before 7.21.5 did not provide this error code
  11. #if defined(LIBCURL_VERSION_NUM) && LIBCURL_VERSION_NUM < 0x071505
  12. #define CURLE_NOT_BUILT_IN 4
  13. #endif
  14. #define check_curl_result(result, errstr) \
  15. if ((result) != CURLE_OK && (result) != CURLE_NOT_BUILT_IN) { \
  16. e += e.empty() ? "" : "\n"; \
  17. e += (errstr); \
  18. e += ::curl_easy_strerror(result); \
  19. }
  20. std::string cmCurlSetCAInfo(::CURL* curl, const char* cafile)
  21. {
  22. std::string e;
  23. if (cafile && *cafile) {
  24. ::CURLcode res = ::curl_easy_setopt(curl, CURLOPT_CAINFO, cafile);
  25. check_curl_result(res, "Unable to set TLS/SSL Verify CAINFO: ");
  26. }
  27. #ifdef CMAKE_FIND_CAFILE
  28. #define CMAKE_CAFILE_FEDORA "/etc/pki/tls/certs/ca-bundle.crt"
  29. else if (cmSystemTools::FileExists(CMAKE_CAFILE_FEDORA, true)) {
  30. ::CURLcode res =
  31. ::curl_easy_setopt(curl, CURLOPT_CAINFO, CMAKE_CAFILE_FEDORA);
  32. check_curl_result(res, "Unable to set TLS/SSL Verify CAINFO: ");
  33. }
  34. #undef CMAKE_CAFILE_FEDORA
  35. else {
  36. #define CMAKE_CAFILE_COMMON "/etc/ssl/certs/ca-certificates.crt"
  37. if (cmSystemTools::FileExists(CMAKE_CAFILE_COMMON, true)) {
  38. ::CURLcode res =
  39. ::curl_easy_setopt(curl, CURLOPT_CAINFO, CMAKE_CAFILE_COMMON);
  40. check_curl_result(res, "Unable to set TLS/SSL Verify CAINFO: ");
  41. }
  42. #undef CMAKE_CAFILE_COMMON
  43. #define CMAKE_CAPATH_COMMON "/etc/ssl/certs"
  44. if (cmSystemTools::FileIsDirectory(CMAKE_CAPATH_COMMON)) {
  45. ::CURLcode res =
  46. ::curl_easy_setopt(curl, CURLOPT_CAPATH, CMAKE_CAPATH_COMMON);
  47. check_curl_result(res, "Unable to set TLS/SSL Verify CAPATH: ");
  48. }
  49. #undef CMAKE_CAPATH_COMMON
  50. }
  51. #endif
  52. return e;
  53. }
  54. std::string cmCurlSetNETRCOption(::CURL* curl, const std::string& netrc_level,
  55. const std::string& netrc_file)
  56. {
  57. std::string e;
  58. CURL_NETRC_OPTION curl_netrc_level = CURL_NETRC_LAST;
  59. ::CURLcode res;
  60. if (!netrc_level.empty()) {
  61. if (netrc_level == "OPTIONAL") {
  62. curl_netrc_level = CURL_NETRC_OPTIONAL;
  63. } else if (netrc_level == "REQUIRED") {
  64. curl_netrc_level = CURL_NETRC_REQUIRED;
  65. } else if (netrc_level == "IGNORED") {
  66. curl_netrc_level = CURL_NETRC_IGNORED;
  67. } else {
  68. e = "NETRC accepts OPTIONAL, IGNORED or REQUIRED but got: ";
  69. e += netrc_level;
  70. return e;
  71. }
  72. }
  73. if (curl_netrc_level != CURL_NETRC_LAST &&
  74. curl_netrc_level != CURL_NETRC_IGNORED) {
  75. res = ::curl_easy_setopt(curl, CURLOPT_NETRC, curl_netrc_level);
  76. check_curl_result(res, "Unable to set netrc level: ");
  77. if (!e.empty()) {
  78. return e;
  79. }
  80. // check to see if a .netrc file has been specified
  81. if (!netrc_file.empty()) {
  82. res = ::curl_easy_setopt(curl, CURLOPT_NETRC_FILE, netrc_file.c_str());
  83. check_curl_result(res, "Unable to set .netrc file path : ");
  84. }
  85. }
  86. return e;
  87. }