Add ssh2_auth_pubkey to allow public and private keys as strings

This commit is contained in:
Andreas Treichel
2021-05-27 23:46:21 +02:00
parent 5c08a92cdd
commit e690abe9e7
3 changed files with 61 additions and 0 deletions

37
ssh2.c
View File

@@ -701,6 +701,34 @@ PHP_FUNCTION(ssh2_auth_pubkey_file)
}
/* }}} */
/* {{{ proto bool ssh2_auth_pubkey(resource session, string username, string pubkey, string privkey[, string passphrase])
* Authenticate using a public key
*/
PHP_FUNCTION(ssh2_auth_pubkey)
{
LIBSSH2_SESSION *session;
zval *zsession;
zend_string *username, *pubkey, *privkey, *passphrase;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "rSSS|S", &zsession, &username, &pubkey, &privkey, &passphrase) == FAILURE) {
return;
}
SSH2_FETCH_NONAUTHENTICATED_SESSION(session, zsession);
if (libssh2_userauth_publickey_frommemory(session, ZSTR_VAL(username), ZSTR_LEN(username), ZSTR_VAL(pubkey), ZSTR_LEN(pubkey), ZSTR_VAL(privkey), ZSTR_LEN(privkey), ZSTR_VAL(passphrase))) {
char *buf;
int len;
libssh2_session_last_error(session, &buf, &len, 0);
php_error_docref(NULL, E_WARNING, "Authentication failed for %s using public key: %s", ZSTR_VAL(username), buf);
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} */
/* {{{ proto bool ssh2_auth_hostbased_file(resource session, string username, string hostname, string pubkeyfile, string privkeyfile[, string passphrase[, string local_username]])
* Authenticate using a hostkey
*/
@@ -1401,6 +1429,14 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_ssh2_auth_pubkey_file, 0, 0, 4)
ZEND_ARG_INFO(0, passphrase)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_ssh2_auth_pubkey, 0, 0, 4)
ZEND_ARG_INFO(0, session)
ZEND_ARG_INFO(0, username)
ZEND_ARG_INFO(0, pubkey)
ZEND_ARG_INFO(0, privkey)
ZEND_ARG_INFO(0, passphrase)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_ssh2_auth_hostbased_file, 0, 0, 5)
ZEND_ARG_INFO(0, session)
ZEND_ARG_INFO(0, username)
@@ -1580,6 +1616,7 @@ zend_function_entry ssh2_functions[] = {
PHP_FE(ssh2_auth_none, arginfo_ssh2_auth_none)
PHP_FE(ssh2_auth_password, arginfo_ssh2_auth_password)
PHP_FE(ssh2_auth_pubkey_file, arginfo_ssh2_auth_pubkey_file)
PHP_FE(ssh2_auth_pubkey, arginfo_ssh2_auth_pubkey)
PHP_FE(ssh2_auth_hostbased_file, arginfo_ssh2_auth_hostbased_file)
PHP_FE(ssh2_forward_listen, arginfo_ssh2_forward_listen)

View File

@@ -0,0 +1,24 @@
--TEST--
ssh2_auth_pubkey() - Tests authentication with a key
--SKIPIF--
<?php require('ssh2_skip.inc'); ?>
--FILE--
<?php require('ssh2_test.inc');
$ssh = ssh2_connect(TEST_SSH2_HOSTNAME, TEST_SSH2_PORT);
var_dump(ssh2_auth_pubkey_file($ssh, TEST_SSH2_USER, file_get_contents(TEST_SSH2_PUB_KEY), file_get_contents(TEST_SSH2_PRIV_KEY)));
$cmd=ssh2_exec($ssh, 'echo "testing echo with key auth"' . PHP_EOL);
var_dump($cmd);
stream_set_blocking($cmd, true);
$response = stream_get_contents($cmd);
echo $response . PHP_EOL;
--EXPECTF--
bool(true)
resource(%d) of type (stream)
testing echo with key auth