1--- 2c: Copyright (C) Daniel Stenberg, <daniel.se>, et al. 3SPDX-License-Identifier: curl 4Title: CURLOPT_SSH_HOSTKEYFUNCTION 5Section: 3 6Source: libcurl 7See-also: 8 - CURLOPT_SSH_HOSTKEYDATA (3) 9 - CURLOPT_SSH_KNOWNHOSTS (3) 10--- 11 12# NAME 13 14CURLOPT_SSH_HOSTKEYFUNCTION - callback to check host key 15 16# SYNOPSIS 17 18~~~c 19#include <curl/curl.h> 20 21int keycallback(void *clientp, 22 int keytype, 23 const char *key, 24 size_t keylen); 25 26CURLcode curl_easy_setopt(CURL *handle, CURLOPT_SSH_HOSTKEYFUNCTION, 27 keycallback); 28~~~ 29 30# DESCRIPTION 31 32Pass a pointer to your callback function, which should match the prototype 33shown above. It overrides CURLOPT_SSH_KNOWNHOSTS(3). 34 35This callback gets called when the verification of the SSH host key is needed. 36 37**key** is **keylen** bytes long and is the key to check. **keytype** 38says what type it is, from the **CURLKHTYPE_*** series in the 39**curl_khtype** enum. 40 41**clientp** is a custom pointer set with CURLOPT_SSH_HOSTKEYDATA(3). 42 43The callback MUST return one of the following return codes to tell libcurl how 44to act: 45 46## CURLKHMATCH_OK 47 48The host key is accepted, the connection should continue. 49 50## CURLKHMATCH_MISMATCH 51 52the host key is rejected, the connection is canceled. 53 54# DEFAULT 55 56NULL 57 58# PROTOCOLS 59 60SCP and SFTP 61 62# EXAMPLE 63 64~~~c 65struct mine { 66 void *custom; 67}; 68 69int hostkeycb(void *clientp, /* passed with CURLOPT_SSH_HOSTKEYDATA */ 70 int keytype, /* CURLKHTYPE */ 71 const char *key, /* host key to check */ 72 size_t keylen) /* length of the key */ 73{ 74 /* 'clientp' points to the callback_data struct */ 75 /* investigate the situation and return the correct value */ 76 return CURLKHMATCH_OK; 77} 78int main(void) 79{ 80 struct mine callback_data; 81 CURL *curl = curl_easy_init(); 82 if(curl) { 83 curl_easy_setopt(curl, CURLOPT_URL, "sftp://example.com/thisfile.txt"); 84 curl_easy_setopt(curl, CURLOPT_SSH_HOSTKEYFUNCTION, hostkeycb); 85 curl_easy_setopt(curl, CURLOPT_SSH_HOSTKEYDATA, &callback_data); 86 87 curl_easy_perform(curl); 88 } 89} 90~~~ 91 92# AVAILABILITY 93 94Added in 7.84.0 , work only with libssh2 backend. 95 96# RETURN VALUE 97 98Returns CURLE_OK if the option is supported, and CURLE_UNKNOWN_OPTION if not. 99