1CURLOPT_SSH_KEYFUNCTION(3) curl_easy_setopt options CURLOPT_SSH_KEYFUNCTION(3)
2
3
4

NAME

6       CURLOPT_SSH_KEYFUNCTION - callback for known host matching logic
7

SYNOPSIS

9       #include <curl/curl.h>
10
11       enum curl_khstat {
12         CURLKHSTAT_FINE_ADD_TO_FILE,
13         CURLKHSTAT_FINE,
14         CURLKHSTAT_REJECT, /* reject the connection, return an error */
15         CURLKHSTAT_DEFER,  /* do not accept it, but we can't answer right
16                               now so this causes a CURLE_DEFER error but
17                               otherwise the connection will be left intact
18                               etc */
19         CURLKHSTAT_FINE_REPLACE
20       };
21
22       enum curl_khmatch {
23         CURLKHMATCH_OK,       /* match */
24         CURLKHMATCH_MISMATCH, /* host found, key mismatch! */
25         CURLKHMATCH_MISSING,  /* no matching host/key found */
26       };
27
28       struct curl_khkey {
29         const char *key; /* points to a null-terminated string encoded with
30                             base64 if len is zero, otherwise to the "raw"
31                             data */
32         size_t len;
33         enum curl_khtype keytype;
34       };
35
36       int ssh_keycallback(CURL *easy,
37                           const struct curl_khkey *knownkey,
38                           const struct curl_khkey *foundkey,
39                           enum curl_khmatch,
40                           void *clientp);
41
42       CURLcode curl_easy_setopt(CURL *handle, CURLOPT_SSH_KEYFUNCTION,
43                                 ssh_keycallback);
44

DESCRIPTION

46       Pass a pointer to your callback function, which should match the proto‐
47       type shown above.
48
49       It gets called when the known_host matching has been done, to allow the
50       application  to act and decide for libcurl how to proceed. The callback
51       will only be called if CURLOPT_SSH_KNOWNHOSTS(3) is also set.
52
53       This callback function gets passed the CURL handle, the  key  from  the
54       known_hosts  file knownkey, the key from the remote site foundkey, info
55       from libcurl on the matching status and a custom pointer (set with CUR‐
56       LOPT_SSH_KEYDATA(3)).  It MUST return one of the following return codes
57       to tell libcurl how to act:
58
59       CURLKHSTAT_FINE_REPLACE
60              The new host+key is accepted and libcurl will  replace  the  old
61              host+key  into  the  known_hosts file before continuing with the
62              connection.  This will also add the new host+key  combo  to  the
63              known_host  pool  kept  in  memory  if it wasn't already present
64              there. The adding of data to the file is done by completely  re‐
65              placing the file with a new copy, so the permissions of the file
66              must allow this. (Added in 7.73.0)
67
68       CURLKHSTAT_FINE_ADD_TO_FILE
69              The host+key is accepted and  libcurl  will  append  it  to  the
70              known_hosts  file  before  continuing  with the connection. This
71              will also add the host+key combo to the known_host pool kept  in
72              memory if it wasn't already present there. The adding of data to
73              the file is done by completely replacing the  file  with  a  new
74              copy, so the permissions of the file must allow this.
75
76       CURLKHSTAT_FINE
77              The  host+key is accepted libcurl will continue with the connec‐
78              tion. This will also add the host+key combo  to  the  known_host
79              pool kept in memory if it wasn't already present there.
80
81       CURLKHSTAT_REJECT
82              The  host+key  is  rejected. libcurl will deny the connection to
83              continue and it will be closed.
84
85       CURLKHSTAT_DEFER
86              The host+key is rejected, but the SSH connection is asked to  be
87              kept  alive.   This  feature could be used when the app wants to
88              somehow return back and act on the host+key situation  and  then
89              retry without needing the overhead of setting it up from scratch
90              again.
91

DEFAULT

93       NULL
94

PROTOCOLS

96       SFTP and SCP
97

EXAMPLE

99       static int keycb(CURL *easy,
100                        const struct curl_khkey *knownkey,
101                        const struct curl_khkey *foundkey,
102                        enum curl_khmatch,
103                        void *clientp)
104       {
105         /* 'clientp' points to the callback_data struct */
106         /* investigate the situation and return the correct value */
107         return CURLKHSTAT_FINE_ADD_TO_FILE;
108       }
109       {
110         curl_easy_setopt(curl, CURLOPT_URL, "sftp://example.com/thisfile.txt");
111         curl_easy_setopt(curl, CURLOPT_SSH_KEYFUNCTION, keycb);
112         curl_easy_setopt(curl, CURLOPT_SSH_KEYDATA, &callback_data);
113         curl_easy_setopt(curl, CURLOPT_SSH_KNOWNHOSTS, "/home/user/known_hosts");
114
115         curl_easy_perform(curl);
116       }
117

AVAILABILITY

119       Added in 7.19.6
120

RETURN VALUE

122       Returns CURLE_OK if the option is supported,  and  CURLE_UNKNOWN_OPTION
123       if not.
124

SEE ALSO

126       CURLOPT_SSH_KEYDATA(3), CURLOPT_SSH_KNOWNHOSTS(3),
127
128
129
130libcurl 7.79.1                 November 04, 2020    CURLOPT_SSH_KEYFUNCTION(3)
Impressum