Skip to content

Commit bc61143

Browse files
committed
Add. Support CURLOPT_SSH_KNOWNHOSTS
1 parent 39c84f8 commit bc61143

File tree

5 files changed

+175
-16
lines changed

5 files changed

+175
-16
lines changed

src/lceasy.c

Lines changed: 158 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,13 @@ int lcurl_easy_create(lua_State *L, int error_mode){
9494
p->match.cb_ref = p->match.ud_ref = LUA_NOREF;
9595
p->chunk_bgn.cb_ref = p->chunk_bgn.ud_ref = LUA_NOREF;
9696
p->chunk_end.cb_ref = p->chunk_end.ud_ref = LUA_NOREF;
97+
#if LCURL_CURL_VER_GE(7,19,6)
98+
p->ssh_key.cb_ref = p->ssh_key.ud_ref = LUA_NOREF;
99+
#endif
97100
#if LCURL_CURL_VER_GE(7,64,0)
98101
p->trailer.cb_ref = p->trailer.ud_ref = LUA_NOREF;
99102
#endif
100-
#if LCURL_CURL_VER_GE(7,74,0)
103+
#if LCURL_CURL_VER_GE(7,74,0) && LCURL_USE_HSTS
101104
p->hstsread.cb_ref = p->hstsread.ud_ref = LUA_NOREF;
102105
p->hstswrite.cb_ref = p->hstswrite.ud_ref = LUA_NOREF;
103106
#endif
@@ -155,11 +158,15 @@ static int lcurl_easy_cleanup_storage(lua_State *L, lcurl_easy_t *p){
155158
luaL_unref(L, LCURL_LUA_REGISTRY, p->chunk_bgn.ud_ref);
156159
luaL_unref(L, LCURL_LUA_REGISTRY, p->chunk_end.cb_ref);
157160
luaL_unref(L, LCURL_LUA_REGISTRY, p->chunk_end.ud_ref);
161+
#if LCURL_CURL_VER_GE(7,19,6)
162+
luaL_unref(L, LCURL_LUA_REGISTRY, p->ssh_key.cb_ref);
163+
luaL_unref(L, LCURL_LUA_REGISTRY, p->ssh_key.ud_ref);
164+
#endif
158165
#if LCURL_CURL_VER_GE(7,64,0)
159166
luaL_unref(L, LCURL_LUA_REGISTRY, p->trailer.cb_ref);
160167
luaL_unref(L, LCURL_LUA_REGISTRY, p->trailer.ud_ref);
161168
#endif
162-
#if LCURL_CURL_VER_GE(7,74,0)
169+
#if LCURL_CURL_VER_GE(7,74,0) && LCURL_USE_HSTS
163170
luaL_unref(L, LCURL_LUA_REGISTRY, p->hstsread.cb_ref);
164171
luaL_unref(L, LCURL_LUA_REGISTRY, p->hstsread.ud_ref);
165172
luaL_unref(L, LCURL_LUA_REGISTRY, p->hstswrite.cb_ref);
@@ -178,10 +185,13 @@ static int lcurl_easy_cleanup_storage(lua_State *L, lcurl_easy_t *p){
178185
p->match.cb_ref = p->match.ud_ref = LUA_NOREF;
179186
p->chunk_bgn.cb_ref = p->chunk_bgn.ud_ref = LUA_NOREF;
180187
p->chunk_end.cb_ref = p->chunk_end.ud_ref = LUA_NOREF;
188+
#if LCURL_CURL_VER_GE(7,19,6)
189+
p->ssh_key.cb_ref = p->ssh_key.ud_ref = LUA_NOREF;
190+
#endif
181191
#if LCURL_CURL_VER_GE(7,64,0)
182192
p->trailer.cb_ref = p->trailer.ud_ref = LUA_NOREF;
183193
#endif
184-
#if LCURL_CURL_VER_GE(7,74,0)
194+
#if LCURL_CURL_VER_GE(7,74,0) && LCURL_USE_HSTS
185195
p->hstsread.cb_ref = p->hstsread.ud_ref = LUA_NOREF;
186196
p->hstswrite.cb_ref = p->hstswrite.ud_ref = LUA_NOREF;
187197
#endif
@@ -897,6 +907,27 @@ static int lcurl_easy_unset_DEBUGFUNCTION(lua_State *L){
897907
return 1;
898908
}
899909

910+
#if LCURL_CURL_VER_GE(7,19,6)
911+
912+
static int lcurl_easy_unset_SSH_KEYFUNCTION(lua_State *L){
913+
lcurl_easy_t *p = lcurl_geteasy(L);
914+
915+
CURLcode code = curl_easy_setopt(p->curl, CURLOPT_SSH_KEYFUNCTION, NULL);
916+
if(code != CURLE_OK){
917+
return lcurl_fail_ex(L, p->err_mode, LCURL_ERROR_EASY, code);
918+
}
919+
curl_easy_setopt(p->curl, CURLOPT_SSH_KEYDATA, NULL);
920+
921+
luaL_unref(L, LCURL_LUA_REGISTRY, p->ssh_key.cb_ref);
922+
luaL_unref(L, LCURL_LUA_REGISTRY, p->ssh_key.ud_ref);
923+
p->ssh_key.cb_ref = p->ssh_key.ud_ref = LUA_NOREF;
924+
925+
lua_settop(L, 1);
926+
return 1;
927+
}
928+
929+
#endif
930+
900931
#if LCURL_CURL_VER_GE(7,21,0)
901932

902933
static int lcurl_easy_unset_FNMATCH_FUNCTION(lua_State *L){
@@ -935,6 +966,7 @@ static int lcurl_easy_unset_CHUNK_BGN_FUNCTION(lua_State *L){
935966
lua_settop(L, 1);
936967
return 1;
937968
}
969+
938970
static int lcurl_easy_unset_CHUNK_END_FUNCTION(lua_State *L){
939971
lcurl_easy_t *p = lcurl_geteasy(L);
940972

@@ -1046,7 +1078,7 @@ static int lcurl_easy_unset_TRAILERFUNCTION(lua_State *L){
10461078

10471079
#endif
10481080

1049-
#if LCURL_CURL_VER_GE(7,74,0)
1081+
#if LCURL_CURL_VER_GE(7,74,0) && LCURL_USE_HSTS
10501082

10511083
static int lcurl_easy_unset_HSTSREADFUNCTION (lua_State *L){
10521084
lcurl_easy_t *p = lcurl_geteasy(L);
@@ -1818,7 +1850,7 @@ static int lcurl_easy_set_TRAILERFUNCTION (lua_State *L){
18181850

18191851
//{ HSTS Reader
18201852

1821-
#if LCURL_CURL_VER_GE(7,74,0)
1853+
#if LCURL_CURL_VER_GE(7,74,0) && LCURL_USE_HSTS
18221854

18231855
#define LCURL_HSTS_EXPIRE_LEN 18
18241856

@@ -1924,7 +1956,7 @@ static int lcurl_easy_set_HSTSREADFUNCTION(lua_State *L){
19241956

19251957
//{ HSTS Writer
19261958

1927-
#if LCURL_CURL_VER_GE(7,74,0)
1959+
#if LCURL_CURL_VER_GE(7,74,0) && LCURL_USE_HSTS
19281960

19291961
static int lcurl_hstswrite_callback(CURL *easy, struct curl_hstsentry *sts, struct curl_index *count, void *arg) {
19301962
lcurl_easy_t *p = arg;
@@ -1996,6 +2028,85 @@ static int lcurl_easy_set_HSTSWRITEFUNCTION(lua_State *L){
19962028

19972029
//}
19982030

2031+
//{ SSH key
2032+
2033+
#if LCURL_CURL_VER_GE(7,19,6)
2034+
2035+
static void lcurl_ssh_key_push(lua_State *L, const struct curl_khkey *key){
2036+
if (!key) {
2037+
lua_pushnil(L);
2038+
return;
2039+
}
2040+
2041+
lua_newtable(L);
2042+
2043+
if(key->len){
2044+
lua_pushliteral(L, "raw");
2045+
lua_pushlstring(L, key->key, key->len);
2046+
} else {
2047+
lua_pushliteral(L, "base64");
2048+
lua_pushstring(L, key->key);
2049+
}
2050+
lua_rawset(L, -3);
2051+
2052+
lua_pushliteral(L, "type");
2053+
lutil_pushuint(L, key->keytype);
2054+
lua_rawset(L, -3);
2055+
}
2056+
2057+
static int lcurl_ssh_key_callback(
2058+
CURL *easy,
2059+
const struct curl_khkey *knownkey,
2060+
const struct curl_khkey *foundkey,
2061+
enum curl_khmatch khmatch,
2062+
void *arg
2063+
) {
2064+
lcurl_easy_t *p = arg;
2065+
lua_State *L = p->L;
2066+
int top = lua_gettop(L);
2067+
int n = lcurl_util_push_cb(L, &p->ssh_key);
2068+
2069+
assert(NULL != p->L);
2070+
2071+
lcurl_ssh_key_push(L, knownkey);
2072+
lcurl_ssh_key_push(L, foundkey);
2073+
lutil_pushuint(L, khmatch);
2074+
2075+
if (lua_pcall(L, n + 2, LUA_MULTRET, 0)) {
2076+
assert(lua_gettop(L) >= top);
2077+
lua_pushlightuserdata(L, (void*)LCURL_ERROR_TAG);
2078+
lua_insert(L, top + 1);
2079+
return CURLKHSTAT_REJECT;
2080+
}
2081+
2082+
if (lua_gettop(L) > top) {
2083+
int ret = lua_tointeger(L, top + 1);
2084+
lua_settop(L, top);
2085+
2086+
switch (ret)
2087+
case CURLKHSTAT_FINE_REPLACE:
2088+
case CURLKHSTAT_FINE_ADD_TO_FILE:
2089+
case CURLKHSTAT_FINE:
2090+
case CURLKHSTAT_REJECT:
2091+
case CURLKHSTAT_DEFER:
2092+
return ret;
2093+
}
2094+
2095+
return CURLKHSTAT_REJECT;
2096+
}
2097+
2098+
static int lcurl_easy_set_SSH_KEYFUNCTION(lua_State *L){
2099+
lcurl_easy_t *p = lcurl_geteasy(L);
2100+
return lcurl_easy_set_callback(L, p, &p->ssh_key,
2101+
CURLOPT_SSH_KEYFUNCTION, CURLOPT_SSH_KEYDATA,
2102+
"ssh_key", lcurl_ssh_key_callback
2103+
);
2104+
}
2105+
2106+
#endif
2107+
2108+
//}
2109+
19992110
static int lcurl_easy_setopt(lua_State *L){
20002111
lcurl_easy_t *p = lcurl_geteasy(L);
20012112
long opt;
@@ -2023,8 +2134,11 @@ static int lcurl_easy_setopt(lua_State *L){
20232134
OPT_ENTRY(progressfunction, PROGRESSFUNCTION, TTT, 0, 0)
20242135
OPT_ENTRY(seekfunction, SEEKFUNCTION, TTT, 0, 0)
20252136
OPT_ENTRY(debugfunction, DEBUGFUNCTION, TTT, 0, 0)
2137+
#if LCURL_CURL_VER_GE(7,19,6)
2138+
OPT_ENTRY(ssh_keyfunction, SSH_KEYFUNCTION, TTT, 0, 0)
2139+
#endif
20262140
#if LCURL_CURL_VER_GE(7,21,0)
2027-
OPT_ENTRY(fnmatch_function, FNMATCH_FUNCTION, TTT, 0, 0)
2141+
OPT_ENTRY(fnmatch_function, FNMATCH_FUNCTION, TTT, 0, 0)
20282142
OPT_ENTRY(chunk_bgn_function, CHUNK_BGN_FUNCTION, TTT, 0, 0)
20292143
OPT_ENTRY(chunk_end_function, CHUNK_END_FUNCTION, TTT, 0, 0)
20302144
#endif
@@ -2034,6 +2148,16 @@ static int lcurl_easy_setopt(lua_State *L){
20342148
#endif
20352149
#if LCURL_CURL_VER_GE(7,56,0)
20362150
OPT_ENTRY(mimepost, MIMEPOST, TTT, 0, 0)
2151+
#endif
2152+
#if LCURL_CURL_VER_GE(7,63,0)
2153+
OPT_ENTRY(curlu, CURLU, TTT, 0, 0)
2154+
#endif
2155+
#if LCURL_CURL_VER_GE(7,64,0)
2156+
OPT_ENTRY(trailerfunction, TRAILERFUNCTION, TTT, 0, 0)
2157+
#endif
2158+
#if LCURL_CURL_VER_GE(7,74,0) && LCURL_USE_HSTS
2159+
OPT_ENTRY(hstsreadfunction, HSTSREADFUNCTION, TTT, 0, 0)
2160+
OPT_ENTRY(hstswritefunction, HSTSWRITEFUNCTION,TTT, 0, 0)
20372161
#endif
20382162
}
20392163
#undef OPT_ENTRY
@@ -2060,8 +2184,11 @@ static int lcurl_easy_unsetopt(lua_State *L){
20602184
OPT_ENTRY(progressfunction, PROGRESSFUNCTION, TTT, 0, 0)
20612185
OPT_ENTRY(seekfunction, SEEKFUNCTION, TTT, 0, 0)
20622186
OPT_ENTRY(debugfunction, DEBUGFUNCTION, TTT, 0, 0)
2187+
#if LCURL_CURL_VER_GE(7,19,6)
2188+
OPT_ENTRY(ssh_keyfunction, SSH_KEYFUNCTION, TTT, 0, 0)
2189+
#endif
20632190
#if LCURL_CURL_VER_GE(7,21,0)
2064-
OPT_ENTRY(fnmatch_function, FNMATCH_FUNCTION, TTT, 0, 0)
2191+
OPT_ENTRY(fnmatch_function, FNMATCH_FUNCTION, TTT, 0, 0)
20652192
OPT_ENTRY(chunk_bgn_function, CHUNK_BGN_FUNCTION, TTT, 0, 0)
20662193
OPT_ENTRY(chunk_end_function, CHUNK_END_FUNCTION, TTT, 0, 0)
20672194
#endif
@@ -2071,6 +2198,16 @@ static int lcurl_easy_unsetopt(lua_State *L){
20712198
#endif
20722199
#if LCURL_CURL_VER_GE(7,56,0)
20732200
OPT_ENTRY(mimepost, MIMEPOST, TTT, 0, 0)
2201+
#endif
2202+
#if LCURL_CURL_VER_GE(7,63,0)
2203+
OPT_ENTRY(curlu, CURLU, TTT, 0, 0)
2204+
#endif
2205+
#if LCURL_CURL_VER_GE(7,64,0)
2206+
OPT_ENTRY(trailerfunction, TRAILERFUNCTION, TTT, 0, 0)
2207+
#endif
2208+
#if LCURL_CURL_VER_GE(7,74,0) && LCURL_USE_HSTS
2209+
OPT_ENTRY(hstsreadfunction, HSTSREADFUNCTION, TTT, 0, 0)
2210+
OPT_ENTRY(hstswritefunction, HSTSWRITEFUNCTION,TTT, 0, 0)
20742211
#endif
20752212
}
20762213
#undef OPT_ENTRY
@@ -2141,6 +2278,9 @@ static const struct luaL_Reg lcurl_easy_methods[] = {
21412278
OPT_ENTRY(progressfunction, PROGRESSFUNCTION, TTT, 0, 0)
21422279
OPT_ENTRY(seekfunction, SEEKFUNCTION, TTT, 0, 0)
21432280
OPT_ENTRY(debugfunction, DEBUGFUNCTION, TTT, 0, 0)
2281+
#if LCURL_CURL_VER_GE(7,19,6)
2282+
OPT_ENTRY(ssh_keyfunction, SSH_KEYFUNCTION, TTT, 0, 0)
2283+
#endif
21442284
#if LCURL_CURL_VER_GE(7,21,0)
21452285
OPT_ENTRY(fnmatch_function, FNMATCH_FUNCTION, TTT, 0, 0)
21462286
OPT_ENTRY(chunk_bgn_function, CHUNK_BGN_FUNCTION, TTT, 0, 0)
@@ -2159,7 +2299,7 @@ static const struct luaL_Reg lcurl_easy_methods[] = {
21592299
#if LCURL_CURL_VER_GE(7,64,0)
21602300
OPT_ENTRY(trailerfunction, TRAILERFUNCTION, TTT, 0, 0)
21612301
#endif
2162-
#if LCURL_CURL_VER_GE(7,74,0)
2302+
#if LCURL_CURL_VER_GE(7,74,0) && LCURL_USE_HSTS
21632303
OPT_ENTRY(hstsreadfunction, HSTSREADFUNCTION, TTT, 0, 0)
21642304
OPT_ENTRY(hstswritefunction, HSTSWRITEFUNCTION,TTT, 0, 0)
21652305
#endif
@@ -2176,6 +2316,9 @@ static const struct luaL_Reg lcurl_easy_methods[] = {
21762316
OPT_ENTRY(progressfunction, PROGRESSFUNCTION, TTT, 0, 0)
21772317
OPT_ENTRY(seekfunction, SEEKFUNCTION, TTT, 0, 0)
21782318
OPT_ENTRY(debugfunction, DEBUGFUNCTION, TTT, 0, 0)
2319+
#if LCURL_CURL_VER_GE(7,19,6)
2320+
OPT_ENTRY(ssh_keyfunction, SSH_KEYFUNCTION, TTT, 0, 0)
2321+
#endif
21792322
#if LCURL_CURL_VER_GE(7,21,0)
21802323
OPT_ENTRY(fnmatch_function, FNMATCH_FUNCTION, TTT, 0, 0)
21812324
OPT_ENTRY(chunk_bgn_function, CHUNK_BGN_FUNCTION, TTT, 0, 0)
@@ -2194,7 +2337,7 @@ static const struct luaL_Reg lcurl_easy_methods[] = {
21942337
#if LCURL_CURL_VER_GE(7,64,0)
21952338
OPT_ENTRY(trailerfunction, TRAILERFUNCTION, TTT, 0, 0)
21962339
#endif
2197-
#if LCURL_CURL_VER_GE(7,74,0)
2340+
#if LCURL_CURL_VER_GE(7,74,0) && LCURL_USE_HSTS
21982341
OPT_ENTRY(hstsreadfunction, HSTSREADFUNCTION, TTT, 0, 0)
21992342
OPT_ENTRY(hstswritefunction, HSTSWRITEFUNCTION,TTT, 0, 0)
22002343
#endif
@@ -2243,8 +2386,11 @@ static const lcurl_const_t lcurl_easy_opt[] = {
22432386
OPT_ENTRY(progressfunction, PROGRESSFUNCTION, TTT, 0, 0)
22442387
OPT_ENTRY(seekfunction, SEEKFUNCTION, TTT, 0, 0)
22452388
OPT_ENTRY(debugfunction, DEBUGFUNCTION, TTT, 0, 0)
2389+
#if LCURL_CURL_VER_GE(7,19,6)
2390+
OPT_ENTRY(ssh_keyfunction, SSH_KEYFUNCTION, TTT, 0, 0)
2391+
#endif
22462392
#if LCURL_CURL_VER_GE(7,21,0)
2247-
OPT_ENTRY(fnmatch_function, FNMATCH_FUNCTION, TTT, 0, 0)
2393+
OPT_ENTRY(fnmatch_function, FNMATCH_FUNCTION, TTT, 0, 0)
22482394
OPT_ENTRY(chunk_bgn_function, CHUNK_BGN_FUNCTION, TTT, 0, 0)
22492395
OPT_ENTRY(chunk_end_function, CHUNK_END_FUNCTION, TTT, 0, 0)
22502396
#endif
@@ -2261,7 +2407,7 @@ static const lcurl_const_t lcurl_easy_opt[] = {
22612407
#if LCURL_CURL_VER_GE(7,64,0)
22622408
OPT_ENTRY(trailerfunction, TRAILERFUNCTION, TTT, 0, 0)
22632409
#endif
2264-
#if LCURL_CURL_VER_GE(7,74,0)
2410+
#if LCURL_CURL_VER_GE(7,74,0) && LCURL_USE_HSTS
22652411
OPT_ENTRY(hstsreadfunction, HSTSREADFUNCTION, TTT, 0, 0)
22662412
OPT_ENTRY(hstswritefunction, HSTSWRITEFUNCTION,TTT, 0, 0)
22672413
#endif

src/lceasy.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,13 @@ typedef struct lcurl_easy_tag{
8787
lcurl_callback_t match;
8888
lcurl_callback_t chunk_bgn;
8989
lcurl_callback_t chunk_end;
90+
#if LCURL_CURL_VER_GE(7,19,6)
91+
lcurl_callback_t ssh_key;
92+
#endif
9093
#if LCURL_CURL_VER_GE(7,64,0)
9194
lcurl_callback_t trailer;
9295
#endif
93-
#if LCURL_CURL_VER_GE(7,74,0)
96+
#if LCURL_CURL_VER_GE(7,74,0) && LCURL_USE_HSTS
9497
lcurl_callback_t hstsread;
9598
lcurl_callback_t hstswrite;
9699
#endif

src/lcflags.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@ FLG_ENTRY(AUTH_NTLM )
1010
#if LCURL_CURL_VER_GE(7,19,3)
1111
FLG_ENTRY(AUTH_DIGEST_IE )
1212
#endif
13+
#if LCURL_CURL_VER_GE(7,19,6)
14+
FLG_ENTRY(KHSTAT_FINE_REPLACE )
15+
FLG_ENTRY(KHSTAT_FINE_ADD_TO_FILE )
16+
FLG_ENTRY(KHSTAT_FINE )
17+
FLG_ENTRY(KHSTAT_REJECT )
18+
FLG_ENTRY(KHSTAT_DEFER )
19+
FLG_ENTRY(KHMATCH_OK )
20+
FLG_ENTRY(KHMATCH_MISMATCH )
21+
FLG_ENTRY(KHMATCH_MISSING )
22+
#endif
1323
#if LCURL_CURL_VER_GE(7,22,0)
1424
FLG_ENTRY(AUTH_NTLM_WB )
1525
#endif
@@ -254,7 +264,7 @@ FLG_ENTRY(OT_FUNCTION)
254264
FLG_ENTRY(OT_FLAG_ALIAS)
255265
#endif
256266

257-
#if LCURL_CURL_VER_GE(7,74,0)
267+
#if LCURL_CURL_VER_GE(7,74,0) && LCURL_USE_HSTS
258268
FLG_ENTRY(HSTS_ENABLE)
259269
FLG_ENTRY(HSTS_READONLYFILE)
260270
FLG_ENTRY(STS_OK)

src/lcopteasy.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ OPT_ENTRY(proxy_issuercert_blob, PROXY_ISSUERCERT_BLOB, BLB, 0, 0)
504504
OPT_ENTRY(ssl_ec_curves, SSL_EC_CURVES, STR, 0, LCURL_DEFAULT_VALUE)
505505
#endif
506506

507-
#if LCURL_CURL_VER_GE(7,74,0)
507+
#if LCURL_CURL_VER_GE(7,74,0) && LCURL_USE_HSTS
508508
OPT_ENTRY(hsts_ctrl, HSTS_CTRL, LNG, 0, 0)
509509
OPT_ENTRY(hsts, HSTS, STR, 0, LCURL_DEFAULT_VALUE)
510510
#endif

src/lcurl.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ static int lcurl_version_info(lua_State *L){
203203

204204
lua_newtable(L);
205205
lua_pushstring(L, data->version); lua_setfield(L, -2, "version"); /* LIBCURL_VERSION */
206-
lua_pushnumber(L, data->version_num); lua_setfield(L, -2, "version_num"); /* LIBCURL_VERSION_NUM */
206+
lutil_pushuint(L, data->version_num); lua_setfield(L, -2, "version_num"); /* LIBCURL_VERSION_NUM */
207207
lua_pushstring(L, data->host); lua_setfield(L, -2, "host"); /* OS/host/cpu/machine when configured */
208208

209209
lua_newtable(L);

0 commit comments

Comments
 (0)