mirror of
https://github.com/php/pecl-networking-ssh2.git
synced 2026-03-23 22:52:06 +01:00
Add ssh2_auth_pubkey to allow public and private keys as strings
This commit is contained in:
37
ssh2.c
37
ssh2.c
@@ -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)
|
||||
|
||||
24
tests/ssh2_auth_pubkey.phpt
Normal file
24
tests/ssh2_auth_pubkey.phpt
Normal 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
|
||||
|
||||
Reference in New Issue
Block a user