From fddf20f89c87775eedabb26c8d63783c507b9faa Mon Sep 17 00:00:00 2001 From: Clint Nelissen Date: Mon, 15 Dec 2014 09:25:46 -0800 Subject: [PATCH] Namespaced classes --- phpseclib/Net/SSH2.php | 47 +++---------------------- phpseclib/System/SSH/Agent.php | 23 ++++++------ phpseclib/System/SSH/Agent/Identity.php | 28 ++++++++------- tests/Functional/Net/SSH2AgentTest.php | 4 ++- 4 files changed, 35 insertions(+), 67 deletions(-) diff --git a/phpseclib/Net/SSH2.php b/phpseclib/Net/SSH2.php index ec7ed63a..b298df1e 100644 --- a/phpseclib/Net/SSH2.php +++ b/phpseclib/Net/SSH2.php @@ -52,45 +52,6 @@ use \phpseclib\Crypt\Random; // Used to do Diffie-Hellman key exchange and DSA/RSA signature verification. use \phpseclib\Math\BigInteger; -/**#@+ - * Execution Bitmap Masks - * - * @see Net_SSH2::bitmap - * @access private - */ -define('NET_SSH2_MASK_CONSTRUCTOR', 0x00000001); -define('NET_SSH2_MASK_CONNECTED', 0x00000002); -define('NET_SSH2_MASK_LOGIN_REQ', 0x00000004); -define('NET_SSH2_MASK_LOGIN', 0x00000008); -define('NET_SSH2_MASK_SHELL', 0x00000010); -define('NET_SSH2_MASK_WINDOW_ADJUST', 0x00000020); -/**#@-*/ - -/**#@+ - * Channel constants - * - * RFC4254 refers not to client and server channels but rather to sender and recipient channels. we don't refer - * to them in that way because RFC4254 toggles the meaning. the client sends a SSH_MSG_CHANNEL_OPEN message with - * a sender channel and the server sends a SSH_MSG_CHANNEL_OPEN_CONFIRMATION in response, with a sender and a - * recepient channel. at first glance, you might conclude that SSH_MSG_CHANNEL_OPEN_CONFIRMATION's sender channel - * would be the same thing as SSH_MSG_CHANNEL_OPEN's sender channel, but it's not, per this snipet: - * The 'recipient channel' is the channel number given in the original - * open request, and 'sender channel' is the channel number allocated by - * the other side. - * - * @see Net_SSH2::_send_channel_packet() - * @see Net_SSH2::_get_channel_packet() - * @access private - */ -define('NET_SSH2_CHANNEL_EXEC', 0); // PuTTy uses 0x100 -define('NET_SSH2_CHANNEL_SHELL', 1); -define('NET_SSH2_CHANNEL_SUBSYSTEM', 2); -/**#@-*/ - -/**#@+ - * @access public - * @see Net_SSH2::getLog() - */ /** * Pure-PHP implementation of SSHv2. * @@ -1896,10 +1857,10 @@ class Net_SSH2 // although PHP5's get_class() preserves the case, PHP4's does not if (is_object($password)) { - switch (strtolower(get_class($password))) { - case 'crypt_rsa': + switch (get_class($password)) { + case 'Crypt_RSA': return $this->_privatekey_login($username, $password); - case 'system_ssh_agent': + case 'phpseclib\System\SSH\Agent': return $this->_ssh_agent_login($username, $password); } } @@ -2141,7 +2102,7 @@ class Net_SSH2 * Login with an ssh-agent provided key * * @param String $username - * @param System_SSH_Agent $agent + * @param \phpseclib\System\SSH\Agent $agent * @return Boolean * @access private */ diff --git a/phpseclib/System/SSH/Agent.php b/phpseclib/System/SSH/Agent.php index 6739d5a4..c4c79918 100644 --- a/phpseclib/System/SSH/Agent.php +++ b/phpseclib/System/SSH/Agent.php @@ -10,7 +10,7 @@ * include 'System/SSH/Agent.php'; * include 'Net/SSH2.php'; * - * $agent = new System_SSH_Agent(); + * $agent = new \phpseclib\System\SSH\Agent(); * * $ssh = new Net_SSH2('www.domain.tld'); * if (!$ssh->login('username', $agent)) { @@ -23,7 +23,7 @@ * * * @category System - * @package System_SSH_Agent + * @package SSH\Agent * @author Jim Wigginton * @copyright 2014 Jim Wigginton * @license http://www.opensource.org/licenses/mit-license.html MIT License @@ -31,20 +31,21 @@ * @internal See http://api.libssh.org/rfc/PROTOCOL.agent */ -if (!class_exists('System_SSH_Agent_Identity')) { - include_once 'Agent/Identity.php'; -} +namespace phpseclib\System\SSH; + +use Crypt_RSA; //This should be removed once the Crypt package is fully namespaced +use phpseclib\System\SSH\Agent\Identity; /** * Pure-PHP ssh-agent client identity factory * - * requestIdentities() method pumps out System_SSH_Agent_Identity objects + * requestIdentities() method pumps out \phpseclib\System\SSH\Agent\Identity objects * - * @package System_SSH_Agent + * @package SSH\Agent * @author Jim Wigginton * @access internal */ -class System_SSH_Agent +class Agent { /**#@+ * Message numbers @@ -77,7 +78,7 @@ class System_SSH_Agent /** * Default Constructor * - * @return System_SSH_Agent + * @return \phpseclib\System\SSH\Agent * @access public */ function __construct() @@ -104,7 +105,7 @@ class System_SSH_Agent * Request Identities * * See "2.5.2 Requesting a list of protocol 2 keys" - * Returns an array containing zero or more System_SSH_Agent_Identity objects + * Returns an array containing zero or more \phpseclib\System\SSH\Agent\Identity objects * * @return Array * @access public @@ -149,7 +150,7 @@ class System_SSH_Agent } // resources are passed by reference by default if (isset($key)) { - $identity = new System_SSH_Agent_Identity($this->fsock); + $identity = new Identity($this->fsock); $identity->setPublicKey($key); $identity->setPublicKeyBlob($key_blob); $identities[] = $identity; diff --git a/phpseclib/System/SSH/Agent/Identity.php b/phpseclib/System/SSH/Agent/Identity.php index c5f92983..0cc768dd 100644 --- a/phpseclib/System/SSH/Agent/Identity.php +++ b/phpseclib/System/SSH/Agent/Identity.php @@ -5,7 +5,7 @@ * PHP versions 4 and 5 * * @category System - * @package System_SSH_Agent + * @package SSH\Agent * @author Jim Wigginton * @copyright 2009 Jim Wigginton * @license http://www.opensource.org/licenses/mit-license.html MIT License @@ -13,28 +13,32 @@ * @internal See http://api.libssh.org/rfc/PROTOCOL.agent */ +namespace phpseclib\System\SSH\Agent; + +use phpseclib\System\SSH\Agent; + /** * Pure-PHP ssh-agent client identity object * - * Instantiation should only be performed by System_SSH_Agent class. + * Instantiation should only be performed by \phpseclib\System\SSH\Agent class. * This could be thought of as implementing an interface that Crypt_RSA * implements. ie. maybe a Net_SSH_Auth_PublicKey interface or something. * The methods in this interface would be getPublicKey, setSignatureMode * and sign since those are the methods phpseclib looks for to perform * public key authentication. * - * @package System_SSH_Agent + * @package SSH\Agent * @author Jim Wigginton * @access internal */ -class System_SSH_Agent_Identity +class Identity { /** * Key Object * * @var Crypt_RSA * @access private - * @see System_SSH_Agent_Identity::getPublicKey() + * @see \phpseclib\System\SSH\Agent\Identity::getPublicKey() */ var $key; @@ -43,7 +47,7 @@ class System_SSH_Agent_Identity * * @var String * @access private - * @see System_SSH_Agent_Identity::sign() + * @see \phpseclib\System\SSH\Agent\Identity::sign() */ var $key_blob; @@ -52,7 +56,7 @@ class System_SSH_Agent_Identity * * @var Resource * @access private - * @see System_SSH_Agent_Identity::sign() + * @see \phpseclib\System\SSH\Agent\Identity::sign() */ var $fsock; @@ -60,7 +64,7 @@ class System_SSH_Agent_Identity * Default Constructor. * * @param Resource $fsock - * @return System_SSH_Agent_Identity + * @return \phpseclib\System\SSH\Agent\Identity * @access private */ function __construct($fsock) @@ -71,7 +75,7 @@ class System_SSH_Agent_Identity /** * Set Public Key * - * Called by System_SSH_Agent::requestIdentities() + * Called by \phpseclib\System\SSH\Agent::requestIdentities() * * @param Crypt_RSA $key * @access private @@ -85,7 +89,7 @@ class System_SSH_Agent_Identity /** * Set Public Key * - * Called by System_SSH_Agent::requestIdentities(). The key blob could be extracted from $this->key + * Called by \phpseclib\System\SSH\Agent::requestIdentities(). The key blob could be extracted from $this->key * but this saves a small amount of computation. * * @param String $key_blob @@ -135,7 +139,7 @@ class System_SSH_Agent_Identity function sign($message) { // the last parameter (currently 0) is for flags and ssh-agent only defines one flag (for ssh-dss): SSH_AGENT_OLD_SIGNATURE - $packet = pack('CNa*Na*N', System_SSH_Agent::SSH_AGENTC_SIGN_REQUEST, strlen($this->key_blob), $this->key_blob, strlen($message), $message, 0); + $packet = pack('CNa*Na*N', Agent::SSH_AGENTC_SIGN_REQUEST, strlen($this->key_blob), $this->key_blob, strlen($message), $message, 0); $packet = pack('Na*', strlen($packet), $packet); if (strlen($packet) != fputs($this->fsock, $packet)) { user_error('Connection closed during signing'); @@ -143,7 +147,7 @@ class System_SSH_Agent_Identity $length = current(unpack('N', fread($this->fsock, 4))); $type = ord(fread($this->fsock, 1)); - if ($type != System_SSH_Agent::SSH_AGENT_SIGN_RESPONSE) { + if ($type != Agent::SSH_AGENT_SIGN_RESPONSE) { user_error('Unable to retreive signature'); } diff --git a/tests/Functional/Net/SSH2AgentTest.php b/tests/Functional/Net/SSH2AgentTest.php index d92bb705..e8dc4403 100644 --- a/tests/Functional/Net/SSH2AgentTest.php +++ b/tests/Functional/Net/SSH2AgentTest.php @@ -6,6 +6,8 @@ * @license http://www.opensource.org/licenses/mit-license.html MIT License */ +use phpseclib\System\SSH\Agent; + class Functional_Net_SSH2AgentTest extends PhpseclibFunctionalTestCase { public static function setUpBeforeClass() @@ -21,7 +23,7 @@ class Functional_Net_SSH2AgentTest extends PhpseclibFunctionalTestCase public function testAgentLogin() { $ssh = new Net_SSH2($this->getEnv('SSH_HOSTNAME')); - $agent = new System_SSH_Agent; + $agent = new Agent; $this->assertTrue( $ssh->login($this->getEnv('SSH_USERNAME'), $agent),