CURLOPT_SSH_KEYFUNCTION.3 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. .\" generated by cd2nroff 0.1 from CURLOPT_SSH_KEYFUNCTION.md
  2. .TH CURLOPT_SSH_KEYFUNCTION 3 "2025-01-17" libcurl
  3. .SH NAME
  4. CURLOPT_SSH_KEYFUNCTION \- callback for known host matching logic
  5. .SH SYNOPSIS
  6. .nf
  7. #include <curl/curl.h>
  8. enum curl_khstat {
  9. CURLKHSTAT_FINE_ADD_TO_FILE,
  10. CURLKHSTAT_FINE,
  11. CURLKHSTAT_REJECT, /* reject the connection, return an error */
  12. CURLKHSTAT_DEFER, /* do not accept it, but we cannot answer right
  13. now. Causes a CURLE_PEER_FAILED_VERIFICATION error but
  14. the connection is left intact */
  15. CURLKHSTAT_FINE_REPLACE
  16. };
  17. enum curl_khmatch {
  18. CURLKHMATCH_OK, /* match */
  19. CURLKHMATCH_MISMATCH, /* host found, key mismatch! */
  20. CURLKHMATCH_MISSING, /* no matching host/key found */
  21. };
  22. struct curl_khkey {
  23. const char *key; /* points to a null-terminated string encoded with
  24. base64 if len is zero, otherwise to the "raw"
  25. data */
  26. size_t len;
  27. enum curl_khtype keytype;
  28. };
  29. int ssh_keycallback(CURL *easy,
  30. const struct curl_khkey *knownkey,
  31. const struct curl_khkey *foundkey,
  32. enum curl_khmatch match,
  33. void *clientp);
  34. CURLcode curl_easy_setopt(CURL *handle, CURLOPT_SSH_KEYFUNCTION,
  35. ssh_keycallback);
  36. .fi
  37. .SH DESCRIPTION
  38. Pass a pointer to your callback function, which should match the prototype
  39. shown above.
  40. It gets called when the known_host matching has been done, to allow the
  41. application to act and decide for libcurl how to proceed. The callback is only
  42. called if \fICURLOPT_SSH_KNOWNHOSTS(3)\fP is also set.
  43. This callback function gets passed the CURL handle, the key from the
  44. known_hosts file \fIknownkey\fP, the key from the remote site \fIfoundkey\fP,
  45. info from libcurl on the matching status and a custom pointer (set with
  46. \fICURLOPT_SSH_KEYDATA(3)\fP). It MUST return one of the following return
  47. codes to tell libcurl how to act:
  48. .IP CURLKHSTAT_FINE_REPLACE
  49. The new host+key is accepted and libcurl replaces the old host+key into the
  50. known_hosts file before continuing with the connection. This also adds the new
  51. host+key combo to the known_host pool kept in memory if it was not already
  52. present there. The adding of data to the file is done by completely replacing
  53. the file with a new copy, so the permissions of the file must allow
  54. this. (Added in 7.73.0)
  55. .IP CURLKHSTAT_FINE_ADD_TO_FILE
  56. The host+key is accepted and libcurl appends it to the known_hosts file before
  57. continuing with the connection. This also adds the host+key combo to the
  58. known_host pool kept in memory if it was not already present there. The adding
  59. of data to the file is done by completely replacing the file with a new copy,
  60. so the permissions of the file must allow this.
  61. .IP CURLKHSTAT_FINE
  62. The host+key is accepted libcurl continues with the connection. This also adds
  63. the host+key combo to the known_host pool kept in memory if it was not already
  64. present there.
  65. .IP CURLKHSTAT_REJECT
  66. The host+key is rejected. libcurl denies the connection to continue and it is
  67. closed.
  68. .IP CURLKHSTAT_DEFER
  69. The host+key is rejected, but the SSH connection is asked to be kept alive.
  70. This feature could be used when the app wants to return and act on the
  71. host+key situation and then retry without needing the overhead of setting it
  72. up from scratch again.
  73. .SH DEFAULT
  74. NULL
  75. .SH PROTOCOLS
  76. This functionality affects scp and sftp
  77. .SH EXAMPLE
  78. .nf
  79. struct mine {
  80. void *custom;
  81. };
  82. static int keycb(CURL *easy,
  83. const struct curl_khkey *knownkey,
  84. const struct curl_khkey *foundkey,
  85. enum curl_khmatch match,
  86. void *clientp)
  87. {
  88. /* 'clientp' points to the callback_data struct */
  89. /* investigate the situation and return the correct value */
  90. return CURLKHSTAT_FINE_ADD_TO_FILE;
  91. }
  92. int main(void)
  93. {
  94. CURL *curl = curl_easy_init();
  95. if(curl) {
  96. struct mine callback_data;
  97. curl_easy_setopt(curl, CURLOPT_URL, "sftp://example.com/thisfile.txt");
  98. curl_easy_setopt(curl, CURLOPT_SSH_KEYFUNCTION, keycb);
  99. curl_easy_setopt(curl, CURLOPT_SSH_KEYDATA, &callback_data);
  100. curl_easy_setopt(curl, CURLOPT_SSH_KNOWNHOSTS, "/home/user/known_hosts");
  101. curl_easy_perform(curl);
  102. }
  103. }
  104. .fi
  105. .SH AVAILABILITY
  106. Added in curl 7.19.6
  107. .SH RETURN VALUE
  108. Returns CURLE_OK if the option is supported, and CURLE_UNKNOWN_OPTION if not.
  109. .SH SEE ALSO
  110. .BR CURLOPT_SSH_KEYDATA (3),
  111. .BR CURLOPT_SSH_KNOWNHOSTS (3)