2014-03-30 15:49:22 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Pure-PHP ssh-agent client.
|
|
|
|
*
|
|
|
|
* PHP versions 4 and 5
|
|
|
|
*
|
|
|
|
* Here are some examples of how to use this library:
|
|
|
|
* <code>
|
|
|
|
* <?php
|
2014-06-01 23:28:49 +02:00
|
|
|
* include 'System/SSH/Agent.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;
|
|
|
|
|
|
|
|
use Crypt_RSA; //This should be removed once the Crypt package is fully namespaced
|
|
|
|
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;
|
|
|
|
/**#@-*/
|
|
|
|
|
2014-12-04 19:38:58 +01:00
|
|
|
/**
|
|
|
|
* Unused
|
|
|
|
*/
|
|
|
|
const SSH_AGENT_FAILURE = 5;
|
|
|
|
|
2014-03-30 15:49:22 +02:00
|
|
|
/**
|
|
|
|
* Socket Resource
|
|
|
|
*
|
|
|
|
* @var Resource
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $fsock;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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:
|
|
|
|
user_error('SSH_AUTH_SOCK not found');
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->fsock = fsockopen('unix://' . $address, 0, $errno, $errstr);
|
|
|
|
if (!$this->fsock) {
|
|
|
|
user_error("Unable to connect to ssh-agent (Error $errno: $errstr)");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
*
|
|
|
|
* @return Array
|
|
|
|
* @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)) {
|
|
|
|
user_error('Connection closed while requesting identities');
|
|
|
|
}
|
|
|
|
|
|
|
|
$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) {
|
2014-03-30 15:49:22 +02:00
|
|
|
user_error('Unable to request identities');
|
|
|
|
}
|
|
|
|
|
|
|
|
$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)));
|
|
|
|
$key_comment = fread($this->fsock, $length);
|
|
|
|
$length = current(unpack('N', substr($key_blob, 0, 4)));
|
|
|
|
$key_type = substr($key_blob, 4, $length);
|
|
|
|
switch ($key_type) {
|
|
|
|
case 'ssh-rsa':
|
|
|
|
if (!class_exists('Crypt_RSA')) {
|
|
|
|
include_once 'Crypt/RSA.php';
|
|
|
|
}
|
|
|
|
$key = new Crypt_RSA();
|
|
|
|
$key->loadKey('ssh-rsa ' . base64_encode($key_blob) . ' ' . $key_comment);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|