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