2014-03-30 15:49:22 +02:00
|
|
|
<?php
|
2014-12-30 03:44:31 +01:00
|
|
|
|
2014-03-30 15:49:22 +02:00
|
|
|
/**
|
|
|
|
* Pure-PHP ssh-agent client.
|
|
|
|
*
|
2015-04-02 12:57:52 +02:00
|
|
|
* PHP version 5
|
2014-03-30 15:49:22 +02:00
|
|
|
*
|
|
|
|
* Here are some examples of how to use this library:
|
|
|
|
* <code>
|
|
|
|
* <?php
|
2014-12-10 02:31:41 +01:00
|
|
|
* include 'vendor/autoload.php';
|
2014-03-30 15:49:22 +02:00
|
|
|
*
|
2014-12-15 18:25:46 +01:00
|
|
|
* $agent = new \phpseclib\System\SSH\Agent();
|
2014-03-30 15:49:22 +02:00
|
|
|
*
|
2014-12-10 02:31:41 +01:00
|
|
|
* $ssh = new \phpseclib\Net\SSH2('www.domain.tld');
|
2014-03-30 15:49:22 +02:00
|
|
|
* if (!$ssh->login('username', $agent)) {
|
|
|
|
* exit('Login Failed');
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* echo $ssh->exec('pwd');
|
|
|
|
* echo $ssh->exec('ls -la');
|
|
|
|
* ?>
|
|
|
|
* </code>
|
|
|
|
*
|
|
|
|
* @category System
|
2014-12-15 18:25:46 +01:00
|
|
|
* @package SSH\Agent
|
2014-03-30 15:49:22 +02:00
|
|
|
* @author Jim Wigginton <terrafrost@php.net>
|
2014-12-10 00:02:44 +01:00
|
|
|
* @copyright 2014 Jim Wigginton
|
2014-03-30 15:49:22 +02:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
|
|
|
* @link http://phpseclib.sourceforge.net
|
|
|
|
* @internal See http://api.libssh.org/rfc/PROTOCOL.agent
|
|
|
|
*/
|
|
|
|
|
2014-12-15 18:25:46 +01:00
|
|
|
namespace phpseclib\System\SSH;
|
|
|
|
|
2014-12-17 01:16:54 +01:00
|
|
|
use phpseclib\Crypt\RSA;
|
2016-01-18 05:05:31 +01:00
|
|
|
use phpseclib\System\SSH\Agent\Identity;
|
2014-03-30 15:49:22 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Pure-PHP ssh-agent client identity factory
|
|
|
|
*
|
2014-12-15 18:25:46 +01:00
|
|
|
* requestIdentities() method pumps out \phpseclib\System\SSH\Agent\Identity objects
|
2014-03-30 15:49:22 +02:00
|
|
|
*
|
2014-12-15 18:25:46 +01:00
|
|
|
* @package SSH\Agent
|
2014-03-30 15:49:22 +02:00
|
|
|
* @author Jim Wigginton <terrafrost@php.net>
|
|
|
|
* @access internal
|
|
|
|
*/
|
2014-12-15 18:25:46 +01:00
|
|
|
class Agent
|
2014-03-30 15:49:22 +02:00
|
|
|
{
|
2014-12-04 03:44:30 +01:00
|
|
|
/**#@+
|
|
|
|
* Message numbers
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
// to request SSH1 keys you have to use SSH_AGENTC_REQUEST_RSA_IDENTITIES (1)
|
|
|
|
const SSH_AGENTC_REQUEST_IDENTITIES = 11;
|
|
|
|
// this is the SSH2 response; the SSH1 response is SSH_AGENT_RSA_IDENTITIES_ANSWER (2).
|
|
|
|
const SSH_AGENT_IDENTITIES_ANSWER = 12;
|
|
|
|
// the SSH1 request is SSH_AGENTC_RSA_CHALLENGE (3)
|
|
|
|
const SSH_AGENTC_SIGN_REQUEST = 13;
|
|
|
|
// the SSH1 response is SSH_AGENT_RSA_RESPONSE (4)
|
|
|
|
const SSH_AGENT_SIGN_RESPONSE = 14;
|
|
|
|
/**#@-*/
|
|
|
|
|
2015-03-29 04:54:51 +02:00
|
|
|
/**@+
|
|
|
|
* Agent forwarding status
|
2014-03-30 15:49:22 +02:00
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
*/
|
2015-03-29 04:54:51 +02:00
|
|
|
// no forwarding requested and not active
|
|
|
|
const FORWARD_NONE = 0;
|
|
|
|
// request agent forwarding when opportune
|
|
|
|
const FORWARD_REQUEST = 1;
|
|
|
|
// forwarding has been request and is active
|
|
|
|
const FORWARD_ACTIVE = 2;
|
|
|
|
/**#@-*/
|
2014-03-30 15:49:22 +02:00
|
|
|
|
2014-12-04 19:38:58 +01:00
|
|
|
/**
|
|
|
|
* Unused
|
|
|
|
*/
|
|
|
|
const SSH_AGENT_FAILURE = 5;
|
|
|
|
|
2014-03-30 15:49:22 +02:00
|
|
|
/**
|
|
|
|
* Socket Resource
|
|
|
|
*
|
2016-04-10 18:25:31 +02:00
|
|
|
* @var Resource
|
2014-03-30 15:49:22 +02:00
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $fsock;
|
|
|
|
|
2014-12-30 03:44:31 +01:00
|
|
|
/**
|
2015-02-06 04:28:23 +01:00
|
|
|
* Agent forwarding status
|
2014-12-30 03:44:31 +01:00
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
*/
|
2015-03-29 04:54:51 +02:00
|
|
|
var $forward_status = self::FORWARD_NONE;
|
2014-12-30 03:44:31 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Buffer for accumulating forwarded authentication
|
|
|
|
* agent data arriving on SSH data channel destined
|
|
|
|
* for agent unix socket
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $socket_buffer = '';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tracking the number of bytes we are expecting
|
|
|
|
* to arrive for the agent socket on the SSH data
|
|
|
|
* channel
|
|
|
|
*/
|
|
|
|
var $expected_bytes = 0;
|
|
|
|
|
2014-03-30 15:49:22 +02:00
|
|
|
/**
|
|
|
|
* Default Constructor
|
|
|
|
*
|
2014-12-15 18:25:46 +01:00
|
|
|
* @return \phpseclib\System\SSH\Agent
|
2014-03-30 15:49:22 +02:00
|
|
|
* @access public
|
|
|
|
*/
|
2014-06-16 17:06:34 +02:00
|
|
|
function __construct()
|
2014-03-30 15:49:22 +02:00
|
|
|
{
|
|
|
|
switch (true) {
|
|
|
|
case isset($_SERVER['SSH_AUTH_SOCK']):
|
|
|
|
$address = $_SERVER['SSH_AUTH_SOCK'];
|
|
|
|
break;
|
|
|
|
case isset($_ENV['SSH_AUTH_SOCK']):
|
|
|
|
$address = $_ENV['SSH_AUTH_SOCK'];
|
|
|
|
break;
|
|
|
|
default:
|
2016-04-10 18:25:31 +02:00
|
|
|
user_error('SSH_AUTH_SOCK not found');
|
|
|
|
return false;
|
2014-03-30 15:49:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->fsock = fsockopen('unix://' . $address, 0, $errno, $errstr);
|
|
|
|
if (!$this->fsock) {
|
2016-04-10 18:25:31 +02:00
|
|
|
user_error("Unable to connect to ssh-agent (Error $errno: $errstr)");
|
2014-03-30 15:49:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Request Identities
|
|
|
|
*
|
|
|
|
* See "2.5.2 Requesting a list of protocol 2 keys"
|
2014-12-15 18:25:46 +01:00
|
|
|
* Returns an array containing zero or more \phpseclib\System\SSH\Agent\Identity objects
|
2014-03-30 15:49:22 +02:00
|
|
|
*
|
2016-04-10 18:25:31 +02:00
|
|
|
* @return Array
|
2014-03-30 15:49:22 +02:00
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function requestIdentities()
|
|
|
|
{
|
|
|
|
if (!$this->fsock) {
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
2014-12-04 03:44:30 +01:00
|
|
|
$packet = pack('NC', 1, self::SSH_AGENTC_REQUEST_IDENTITIES);
|
2014-03-30 15:49:22 +02:00
|
|
|
if (strlen($packet) != fputs($this->fsock, $packet)) {
|
2016-04-10 18:25:31 +02:00
|
|
|
user_error('Connection closed while requesting identities');
|
2014-03-30 15:49:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$length = current(unpack('N', fread($this->fsock, 4)));
|
|
|
|
$type = ord(fread($this->fsock, 1));
|
2014-12-04 03:44:30 +01:00
|
|
|
if ($type != self::SSH_AGENT_IDENTITIES_ANSWER) {
|
2016-04-10 18:25:31 +02:00
|
|
|
user_error('Unable to request identities');
|
2014-03-30 15:49:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$identities = array();
|
|
|
|
$keyCount = current(unpack('N', fread($this->fsock, 4)));
|
|
|
|
for ($i = 0; $i < $keyCount; $i++) {
|
|
|
|
$length = current(unpack('N', fread($this->fsock, 4)));
|
|
|
|
$key_blob = fread($this->fsock, $length);
|
|
|
|
$length = current(unpack('N', fread($this->fsock, 4)));
|
2016-04-10 18:25:31 +02:00
|
|
|
$key_comment = fread($this->fsock, $length);
|
2014-03-30 15:49:22 +02:00
|
|
|
$length = current(unpack('N', substr($key_blob, 0, 4)));
|
|
|
|
$key_type = substr($key_blob, 4, $length);
|
|
|
|
switch ($key_type) {
|
|
|
|
case 'ssh-rsa':
|
2014-12-17 01:16:54 +01:00
|
|
|
$key = new RSA();
|
2016-04-10 18:25:31 +02:00
|
|
|
$key->loadKey('ssh-rsa ' . base64_encode($key_blob) . ' ' . $key_comment);
|
2014-03-30 15:49:22 +02:00
|
|
|
break;
|
|
|
|
case 'ssh-dss':
|
|
|
|
// not currently supported
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// resources are passed by reference by default
|
|
|
|
if (isset($key)) {
|
2014-12-15 18:25:46 +01:00
|
|
|
$identity = new Identity($this->fsock);
|
2014-03-30 15:49:22 +02:00
|
|
|
$identity->setPublicKey($key);
|
|
|
|
$identity->setPublicKeyBlob($key_blob);
|
|
|
|
$identities[] = $identity;
|
|
|
|
unset($key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $identities;
|
|
|
|
}
|
2014-12-30 03:44:31 +01:00
|
|
|
|
|
|
|
/**
|
2015-02-06 04:28:23 +01:00
|
|
|
* Signal that agent forwarding should
|
|
|
|
* be requested when a channel is opened
|
2014-12-30 03:44:31 +01:00
|
|
|
*
|
|
|
|
* @param Net_SSH2 $ssh
|
2016-04-10 18:25:31 +02:00
|
|
|
* @return Boolean
|
2014-12-30 03:44:31 +01:00
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function startSSHForwarding($ssh)
|
|
|
|
{
|
2015-03-29 04:54:51 +02:00
|
|
|
if ($this->forward_status == self::FORWARD_NONE) {
|
|
|
|
$this->forward_status = self::FORWARD_REQUEST;
|
2014-12-30 03:44:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-02-06 04:28:23 +01:00
|
|
|
* Request agent forwarding of remote server
|
2014-12-30 03:44:31 +01:00
|
|
|
*
|
|
|
|
* @param Net_SSH2 $ssh
|
2016-04-10 18:25:31 +02:00
|
|
|
* @return Boolean
|
2014-12-30 03:44:31 +01:00
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
function _request_forwarding($ssh)
|
|
|
|
{
|
2015-02-06 04:28:23 +01:00
|
|
|
$request_channel = $ssh->_get_open_channel();
|
2015-03-24 06:38:56 +01:00
|
|
|
if ($request_channel === false) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-12-30 03:44:31 +01:00
|
|
|
|
2015-07-15 03:52:31 +02:00
|
|
|
$packet = pack(
|
|
|
|
'CNNa*C',
|
|
|
|
NET_SSH2_MSG_CHANNEL_REQUEST,
|
|
|
|
$ssh->server_channels[$request_channel],
|
|
|
|
strlen('auth-agent-req@openssh.com'),
|
|
|
|
'auth-agent-req@openssh.com',
|
|
|
|
1
|
|
|
|
);
|
2014-12-30 03:44:31 +01:00
|
|
|
|
2015-02-06 04:28:23 +01:00
|
|
|
$ssh->channel_status[$request_channel] = NET_SSH2_MSG_CHANNEL_REQUEST;
|
2014-12-30 03:44:31 +01:00
|
|
|
|
|
|
|
if (!$ssh->_send_binary_packet($packet)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-02-06 04:28:23 +01:00
|
|
|
$response = $ssh->_get_channel_packet($request_channel);
|
2014-12-30 03:44:31 +01:00
|
|
|
if ($response === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-02-06 04:28:23 +01:00
|
|
|
$ssh->channel_status[$request_channel] = NET_SSH2_MSG_CHANNEL_OPEN;
|
2015-03-29 04:54:51 +02:00
|
|
|
$this->forward_status = self::FORWARD_ACTIVE;
|
2015-02-06 04:28:23 +01:00
|
|
|
|
2014-12-30 03:44:31 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-02-06 04:28:23 +01:00
|
|
|
* On successful channel open
|
2014-12-30 03:44:31 +01:00
|
|
|
*
|
2015-02-06 04:28:23 +01:00
|
|
|
* This method is called upon successful channel
|
|
|
|
* open to give the SSH Agent an opportunity
|
|
|
|
* to take further action. i.e. request agent forwarding
|
2014-12-30 03:44:31 +01:00
|
|
|
*
|
|
|
|
* @param Net_SSH2 $ssh
|
|
|
|
* @access private
|
|
|
|
*/
|
2015-02-06 04:28:23 +01:00
|
|
|
function _on_channel_open($ssh)
|
2014-12-30 03:44:31 +01:00
|
|
|
{
|
2015-03-29 04:54:51 +02:00
|
|
|
if ($this->forward_status == self::FORWARD_REQUEST) {
|
2014-12-30 03:44:31 +01:00
|
|
|
$this->_request_forwarding($ssh);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Forward data to SSH Agent and return data reply
|
|
|
|
*
|
2016-04-10 18:25:31 +02:00
|
|
|
* @param String $data
|
2014-12-30 03:44:31 +01:00
|
|
|
* @return data from SSH Agent
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
function _forward_data($data)
|
|
|
|
{
|
|
|
|
if ($this->expected_bytes > 0) {
|
2015-01-12 10:13:33 +01:00
|
|
|
$this->socket_buffer.= $data;
|
2014-12-30 03:44:31 +01:00
|
|
|
$this->expected_bytes -= strlen($data);
|
|
|
|
} else {
|
|
|
|
$agent_data_bytes = current(unpack('N', $data));
|
|
|
|
$current_data_bytes = strlen($data);
|
|
|
|
$this->socket_buffer = $data;
|
|
|
|
if ($current_data_bytes != $agent_data_bytes + 4) {
|
|
|
|
$this->expected_bytes = ($agent_data_bytes + 4) - $current_data_bytes;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strlen($this->socket_buffer) != fwrite($this->fsock, $this->socket_buffer)) {
|
2016-04-10 18:25:31 +02:00
|
|
|
user_error('Connection closed attempting to forward data to SSH agent');
|
2014-12-30 03:44:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->socket_buffer = '';
|
|
|
|
$this->expected_bytes = 0;
|
|
|
|
|
|
|
|
$agent_reply_bytes = current(unpack('N', fread($this->fsock, 4)));
|
|
|
|
|
|
|
|
$agent_reply_data = fread($this->fsock, $agent_reply_bytes);
|
|
|
|
$agent_reply_data = current(unpack('a*', $agent_reply_data));
|
|
|
|
|
|
|
|
return pack('Na*', $agent_reply_bytes, $agent_reply_data);
|
|
|
|
}
|
2014-03-30 15:49:22 +02:00
|
|
|
}
|