Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.x]Fix bug #5206 #5210

Merged
merged 4 commits into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion thirdparty/php/curl/curl_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ typedef struct {
zval func_name;
zend_fcall_info_cache fci_cache;
int method;
} php_curl_progress, php_curl_fnmatch, php_curlm_server_push, php_curl_fnxferinfo;
} php_curl_progress, php_curl_fnmatch, php_curlm_server_push, php_curl_fnxferinfo, php_curl_sshhostkey;

typedef struct {
php_curl_write *write;
Expand All @@ -80,6 +80,9 @@ typedef struct {
php_curl_fnxferinfo *xferinfo;
#endif
php_curl_fnmatch *fnmatch;
#if LIBCURL_VERSION_NUM >= 0x075400 && PHP_VERSION_ID >= 80300
php_curl_sshhostkey *sshhostkey;
#endif
} php_curl_handlers;

struct _php_curl_error {
Expand Down
126 changes: 125 additions & 1 deletion thirdparty/php/curl/interface.cc
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,16 @@ static HashTable *swoole_curl_get_gc(zend_object *object, zval **table, int *n)
zend_get_gc_buffer_add_zval(gc_buffer, &curl_handlers(curl)->fnmatch->func_name);
}

#if LIBCURL_VERSION_NUM >= 0x075400 && PHP_VERSION_ID >= 80300
if (curl->handlers.sshhostkey) {
zend_get_gc_buffer_add_zval(gc_buffer, &curl->handlers.sshhostkey->func_name);
}
#endif

zend_get_gc_buffer_add_zval(gc_buffer, &curl_handlers(curl)->std_err);
#if PHP_VERSION_ID >= 80100
zend_get_gc_buffer_add_zval(gc_buffer, &curl->private_data);
#endif
}

zend_get_gc_buffer_use(gc_buffer, table, n);
Expand Down Expand Up @@ -567,6 +576,61 @@ static size_t fn_progress(void *clientp, double dltotal, double dlnow, double ul
}
/* }}} */

#if LIBCURL_VERSION_NUM >= 0x075400 && PHP_VERSION_ID >= 80300
static int fn_ssh_hostkeyfunction(void *clientp, int keytype, const char *key, size_t keylen)
{
php_curl *ch = (php_curl *)clientp;
php_curl_sshhostkey *t = ch->handlers.sshhostkey;
int rval = CURLKHMATCH_MISMATCH; /* cancel connection in case of an exception */

#if PHP_CURL_DEBUG
fprintf(stderr, "curl_ssh_hostkeyfunction() called\n");
fprintf(stderr, "clientp = %x, keytype = %d, key = %s, keylen = %zu\n", clientp, keytype, key, keylen);
#endif

zval argv[4];
zval retval;
zend_result error;
zend_fcall_info fci;

GC_ADDREF(&ch->std);
ZVAL_OBJ(&argv[0], &ch->std);
ZVAL_LONG(&argv[1], keytype);
ZVAL_STRINGL(&argv[2], key, keylen);
ZVAL_LONG(&argv[3], keylen);

fci.size = sizeof(fci);
ZVAL_COPY_VALUE(&fci.function_name, &t->func_name);
fci.object = NULL;
fci.retval = &retval;
fci.param_count = 4;
fci.params = argv;
fci.named_params = NULL;

ch->in_callback = 1;
error = zend_call_function(&fci, &t->fci_cache);
ch->in_callback = 0;
if (error == FAILURE) {
php_error_docref(NULL, E_WARNING, "Cannot call the CURLOPT_SSH_HOSTKEYFUNCTION");
} else if (!Z_ISUNDEF(retval)) {
swoole_curl_verify_handlers(ch, /* reporterror */ true);
if (Z_TYPE(retval) == IS_LONG) {
zend_long retval_long = Z_LVAL(retval);
if (retval_long == CURLKHMATCH_OK || retval_long == CURLKHMATCH_MISMATCH) {
rval = retval_long;
} else {
zend_throw_error(NULL, "The CURLOPT_SSH_HOSTKEYFUNCTION callback must return either CURLKHMATCH_OK or CURLKHMATCH_MISMATCH");
}
} else {
zend_throw_error(NULL, "The CURLOPT_SSH_HOSTKEYFUNCTION callback must return either CURLKHMATCH_OK or CURLKHMATCH_MISMATCH");
}
}
zval_ptr_dtor(&argv[0]);
zval_ptr_dtor(&argv[2]);
return rval;
}
#endif

/* {{{ curl_read
*/
static size_t fn_read(char *data, size_t size, size_t nmemb, void *ctx) {
Expand Down Expand Up @@ -825,9 +889,12 @@ void swoole_curl_init_handle(php_curl *ch) {
curl_handlers(ch)->write_header = (php_curl_write *) ecalloc(1, sizeof(php_curl_write));
curl_handlers(ch)->read = (php_curl_read *) ecalloc(1, sizeof(php_curl_read));
curl_handlers(ch)->progress = NULL;
curl_handlers(ch)->fnmatch = NULL;
#if LIBCURL_VERSION_NUM >= 0x072000 && PHP_VERSION_ID >= 80200
curl_handlers(ch)->xferinfo = NULL;
#endif
curl_handlers(ch)->fnmatch = NULL;
#if LIBCURL_VERSION_NUM >= 0x075400 && PHP_VERSION_ID >= 80300
curl_handlers(ch)->sshhostkey = NULL;
#endif
ch->clone = (uint32_t *) emalloc(sizeof(uint32_t));
*ch->clone = 1;
Expand All @@ -840,6 +907,10 @@ void swoole_curl_init_handle(php_curl *ch) {
zend_llist_init(&ch->to_free->post, sizeof(struct HttpPost *), (llist_dtor_func_t) curl_free_post, 0);
zend_llist_init(&ch->to_free->stream, sizeof(struct mime_data_cb_arg *), (llist_dtor_func_t) curl_free_cb_arg, 0);

#if LIBCURL_VERSION_NUM < 0x073800 && PHP_VERSION_ID >= 80100
zend_llist_init(&ch->to_free->buffers, sizeof(zend_string *), (llist_dtor_func_t)curl_free_buffers, 0);
#endif

ch->to_free->slist = (HashTable *) emalloc(sizeof(HashTable));
zend_hash_init(ch->to_free->slist, 4, NULL, curl_free_slist, 0);
ZVAL_UNDEF(&ch->postfields);
Expand Down Expand Up @@ -1012,6 +1083,21 @@ void swoole_setup_easy_copy_handlers(php_curl *ch, php_curl *source) {
curl_easy_setopt(ch->cp, CURLOPT_FNMATCH_DATA, (void *) ch);
}

#if LIBCURL_VERSION_NUM >= 0x075400 && PHP_VERSION_ID >= 80300
if (curl_handlers(source)->sshhostkey) {
curl_handlers(ch)->sshhostkey = (php_curl_sshhostkey *) ecalloc(1, sizeof(php_curl_sshhostkey));
if (!Z_ISUNDEF(curl_handlers(source)->sshhostkey->func_name)) {
ZVAL_COPY(&curl_handlers(ch)->sshhostkey->func_name, &curl_handlers(source)->sshhostkey->func_name);
}
curl_handlers(ch)->sshhostkey->method = curl_handlers(source)->sshhostkey->method;
curl_easy_setopt(ch->cp, CURLOPT_SSH_HOSTKEYDATA, (void *) ch);
}
#endif

#if PHP_VERSION_ID >= 80100
ZVAL_COPY(&ch->private_data, &source->private_data);
#endif

efree(ch->to_free->slist);
efree(ch->to_free);
ch->to_free = source->to_free;
Expand Down Expand Up @@ -1707,6 +1793,21 @@ static int _php_curl_setopt(php_curl *ch, zend_long option, zval *zvalue, bool i
curl_handlers(ch)->progress->method = PHP_CURL_USER;
break;

#if LIBCURL_VERSION_NUM >= 0x075400 && PHP_VERSION_ID >= 80300
case CURLOPT_SSH_HOSTKEYFUNCTION:
curl_easy_setopt(ch->cp, CURLOPT_SSH_HOSTKEYFUNCTION, fn_ssh_hostkeyfunction);
curl_easy_setopt(ch->cp, CURLOPT_SSH_HOSTKEYDATA, ch);
if (curl_handlers(ch)->sshhostkey == NULL) {
curl_handlers(ch)->sshhostkey = (php_curl_sshhostkey *) ecalloc(1, sizeof(php_curl_sshhostkey));
} else if (!Z_ISUNDEF(curl_handlers(ch)->sshhostkey->func_name)) {
zval_ptr_dtor(&curl_handlers(ch)->sshhostkey->func_name);
curl_handlers(ch)->sshhostkey->fci_cache = empty_fcall_info_cache;
}
ZVAL_COPY(&curl_handlers(ch)->sshhostkey->func_name, zvalue);
curl_handlers(ch)->sshhostkey->method = PHP_CURL_USER;
break;
#endif

case CURLOPT_READFUNCTION:
if (!Z_ISUNDEF(curl_handlers(ch)->read->func_name)) {
zval_ptr_dtor(&curl_handlers(ch)->read->func_name);
Expand Down Expand Up @@ -2154,6 +2255,14 @@ PHP_FUNCTION(swoole_native_curl_getinfo) {
if (curl_easy_getinfo(ch->cp, CURLINFO_EFFECTIVE_METHOD, &s_code) == CURLE_OK) {
CAAS("effective_method", s_code);
}
#endif
#if LIBCURL_VERSION_NUM >= 0x075400 && PHP_VERSION_ID >= 80300
if (curl_easy_getinfo(ch->cp, CURLINFO_CAPATH, &s_code) == CURLE_OK) {
CAAS("capath", s_code);
}
if (curl_easy_getinfo(ch->cp, CURLINFO_CAINFO, &s_code) == CURLE_OK) {
CAAS("cainfo", s_code);
}
#endif
} else {
switch (option) {
Expand Down Expand Up @@ -2381,6 +2490,13 @@ static void _php_curl_free(php_curl *ch) {
efree(curl_handlers(ch)->fnmatch);
}

#if LIBCURL_VERSION_NUM >= 0x075400 && php_version_id >= 80300
if (curl_handlers(ch)->sshhostkey) {
zval_ptr_dtor(&curl_handlers(ch)->sshhostkey->func_name);
efree(curl_handlers(ch)->sshhostkey);
}
#endif

#if PHP_VERSION_ID < 80100
efree(ch->handlers);
#endif
Expand Down Expand Up @@ -2462,6 +2578,14 @@ static void _php_curl_reset_handlers(php_curl *ch) {
efree(curl_handlers(ch)->fnmatch);
curl_handlers(ch)->fnmatch = NULL;
}

#if LIBCURL_VERSION_NUM >= 0x075400 && PHP_VERSION_ID >= 80300
if (curl_handlers(ch)->sshhostkey) {
zval_ptr_dtor(&curl_handlers(ch)->sshhostkey->func_name);
efree(curl_handlers(ch)->sshhostkey);
curl_handlers(ch)->sshhostkey = NULL;
}
#endif
}
/* }}} */

Expand Down
Loading