PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/google-listings-and-ads/vendor/phpseclib/phpseclib/phpseclib/System/SSH/Agent.php

https://gitlab.com/remyvianne/krowkaramel
PHP | 297 lines | 126 code | 33 blank | 138 comment | 22 complexity | ccaef1ac6fdbc4e6959b0992c7dcdba8 MD5 | raw file
  1. <?php
  2. /**
  3. * Pure-PHP ssh-agent client.
  4. *
  5. * {@internal See http://api.libssh.org/rfc/PROTOCOL.agent}
  6. *
  7. * PHP version 5
  8. *
  9. * Here are some examples of how to use this library:
  10. * <code>
  11. * <?php
  12. * include 'vendor/autoload.php';
  13. *
  14. * $agent = new \phpseclib3\System\SSH\Agent();
  15. *
  16. * $ssh = new \phpseclib3\Net\SSH2('www.domain.tld');
  17. * if (!$ssh->login('username', $agent)) {
  18. * exit('Login Failed');
  19. * }
  20. *
  21. * echo $ssh->exec('pwd');
  22. * echo $ssh->exec('ls -la');
  23. * ?>
  24. * </code>
  25. *
  26. * @category System
  27. * @package SSH\Agent
  28. * @author Jim Wigginton <terrafrost@php.net>
  29. * @copyright 2014 Jim Wigginton
  30. * @license http://www.opensource.org/licenses/mit-license.html MIT License
  31. * @link http://phpseclib.sourceforge.net
  32. */
  33. namespace phpseclib3\System\SSH;
  34. use phpseclib3\Crypt\RSA;
  35. use phpseclib3\Exception\BadConfigurationException;
  36. use phpseclib3\System\SSH\Agent\Identity;
  37. use phpseclib3\Common\Functions\Strings;
  38. use phpseclib3\Crypt\PublicKeyLoader;
  39. /**
  40. * Pure-PHP ssh-agent client identity factory
  41. *
  42. * requestIdentities() method pumps out \phpseclib3\System\SSH\Agent\Identity objects
  43. *
  44. * @package SSH\Agent
  45. * @author Jim Wigginton <terrafrost@php.net>
  46. * @access public
  47. */
  48. class Agent
  49. {
  50. use Common\Traits\ReadBytes;
  51. // Message numbers
  52. // to request SSH1 keys you have to use SSH_AGENTC_REQUEST_RSA_IDENTITIES (1)
  53. const SSH_AGENTC_REQUEST_IDENTITIES = 11;
  54. // this is the SSH2 response; the SSH1 response is SSH_AGENT_RSA_IDENTITIES_ANSWER (2).
  55. const SSH_AGENT_IDENTITIES_ANSWER = 12;
  56. // the SSH1 request is SSH_AGENTC_RSA_CHALLENGE (3)
  57. const SSH_AGENTC_SIGN_REQUEST = 13;
  58. // the SSH1 response is SSH_AGENT_RSA_RESPONSE (4)
  59. const SSH_AGENT_SIGN_RESPONSE = 14;
  60. // Agent forwarding status
  61. // no forwarding requested and not active
  62. const FORWARD_NONE = 0;
  63. // request agent forwarding when opportune
  64. const FORWARD_REQUEST = 1;
  65. // forwarding has been request and is active
  66. const FORWARD_ACTIVE = 2;
  67. /**
  68. * Unused
  69. */
  70. const SSH_AGENT_FAILURE = 5;
  71. /**
  72. * Socket Resource
  73. *
  74. * @var resource
  75. * @access private
  76. */
  77. private $fsock;
  78. /**
  79. * Agent forwarding status
  80. *
  81. * @var int
  82. * @access private
  83. */
  84. private $forward_status = self::FORWARD_NONE;
  85. /**
  86. * Buffer for accumulating forwarded authentication
  87. * agent data arriving on SSH data channel destined
  88. * for agent unix socket
  89. *
  90. * @var string
  91. * @access private
  92. */
  93. private $socket_buffer = '';
  94. /**
  95. * Tracking the number of bytes we are expecting
  96. * to arrive for the agent socket on the SSH data
  97. * channel
  98. *
  99. * @var int
  100. * @access private
  101. */
  102. private $expected_bytes = 0;
  103. /**
  104. * The current request channel
  105. *
  106. * @var int
  107. * @access private
  108. */
  109. private $request_channel;
  110. /**
  111. * Default Constructor
  112. *
  113. * @return \phpseclib3\System\SSH\Agent
  114. * @throws \phpseclib3\Exception\BadConfigurationException if SSH_AUTH_SOCK cannot be found
  115. * @throws \RuntimeException on connection errors
  116. * @access public
  117. */
  118. public function __construct($address = null)
  119. {
  120. if (!$address) {
  121. switch (true) {
  122. case isset($_SERVER['SSH_AUTH_SOCK']):
  123. $address = $_SERVER['SSH_AUTH_SOCK'];
  124. break;
  125. case isset($_ENV['SSH_AUTH_SOCK']):
  126. $address = $_ENV['SSH_AUTH_SOCK'];
  127. break;
  128. default:
  129. throw new BadConfigurationException('SSH_AUTH_SOCK not found');
  130. }
  131. }
  132. $this->fsock = fsockopen('unix://' . $address, 0, $errno, $errstr);
  133. if (!$this->fsock) {
  134. throw new \RuntimeException("Unable to connect to ssh-agent (Error $errno: $errstr)");
  135. }
  136. }
  137. /**
  138. * Request Identities
  139. *
  140. * See "2.5.2 Requesting a list of protocol 2 keys"
  141. * Returns an array containing zero or more \phpseclib3\System\SSH\Agent\Identity objects
  142. *
  143. * @return array
  144. * @throws \RuntimeException on receipt of unexpected packets
  145. * @access public
  146. */
  147. public function requestIdentities()
  148. {
  149. if (!$this->fsock) {
  150. return [];
  151. }
  152. $packet = pack('NC', 1, self::SSH_AGENTC_REQUEST_IDENTITIES);
  153. if (strlen($packet) != fputs($this->fsock, $packet)) {
  154. throw new \RuntimeException('Connection closed while requesting identities');
  155. }
  156. $length = current(unpack('N', $this->readBytes(4)));
  157. $packet = $this->readBytes($length);
  158. list($type, $keyCount) = Strings::unpackSSH2('CN', $packet);
  159. if ($type != self::SSH_AGENT_IDENTITIES_ANSWER) {
  160. throw new \RuntimeException('Unable to request identities');
  161. }
  162. $identities = [];
  163. for ($i = 0; $i < $keyCount; $i++) {
  164. list($key_blob, $comment) = Strings::unpackSSH2('ss', $packet);
  165. $temp = $key_blob;
  166. list($key_type) = Strings::unpackSSH2('s', $temp);
  167. switch ($key_type) {
  168. case 'ssh-rsa':
  169. case 'ssh-dss':
  170. case 'ssh-ed25519':
  171. case 'ecdsa-sha2-nistp256':
  172. case 'ecdsa-sha2-nistp384':
  173. case 'ecdsa-sha2-nistp521':
  174. $key = PublicKeyLoader::load($key_type . ' ' . base64_encode($key_blob));
  175. }
  176. // resources are passed by reference by default
  177. if (isset($key)) {
  178. $identity = (new Identity($this->fsock))
  179. ->withPublicKey($key)
  180. ->withPublicKeyBlob($key_blob);
  181. $identities[] = $identity;
  182. unset($key);
  183. }
  184. }
  185. return $identities;
  186. }
  187. /**
  188. * Signal that agent forwarding should
  189. * be requested when a channel is opened
  190. *
  191. * @param \phpseclib3\Net\SSH2 $ssh
  192. * @return bool
  193. * @access public
  194. */
  195. public function startSSHForwarding($ssh)
  196. {
  197. if ($this->forward_status == self::FORWARD_NONE) {
  198. $this->forward_status = self::FORWARD_REQUEST;
  199. }
  200. }
  201. /**
  202. * Request agent forwarding of remote server
  203. *
  204. * @param \phpseclib3\Net\SSH2 $ssh
  205. * @return bool
  206. * @access private
  207. */
  208. private function request_forwarding($ssh)
  209. {
  210. if (!$ssh->requestAgentForwarding()) {
  211. return false;
  212. }
  213. $this->forward_status = self::FORWARD_ACTIVE;
  214. return true;
  215. }
  216. /**
  217. * On successful channel open
  218. *
  219. * This method is called upon successful channel
  220. * open to give the SSH Agent an opportunity
  221. * to take further action. i.e. request agent forwarding
  222. *
  223. * @param \phpseclib3\Net\SSH2 $ssh
  224. * @access private
  225. */
  226. public function registerChannelOpen($ssh)
  227. {
  228. if ($this->forward_status == self::FORWARD_REQUEST) {
  229. $this->request_forwarding($ssh);
  230. }
  231. }
  232. /**
  233. * Forward data to SSH Agent and return data reply
  234. *
  235. * @param string $data
  236. * @return string Data from SSH Agent
  237. * @throws \RuntimeException on connection errors
  238. * @access public
  239. */
  240. public function forwardData($data)
  241. {
  242. if ($this->expected_bytes > 0) {
  243. $this->socket_buffer.= $data;
  244. $this->expected_bytes -= strlen($data);
  245. } else {
  246. $agent_data_bytes = current(unpack('N', $data));
  247. $current_data_bytes = strlen($data);
  248. $this->socket_buffer = $data;
  249. if ($current_data_bytes != $agent_data_bytes + 4) {
  250. $this->expected_bytes = ($agent_data_bytes + 4) - $current_data_bytes;
  251. return false;
  252. }
  253. }
  254. if (strlen($this->socket_buffer) != fwrite($this->fsock, $this->socket_buffer)) {
  255. throw new \RuntimeException('Connection closed attempting to forward data to SSH agent');
  256. }
  257. $this->socket_buffer = '';
  258. $this->expected_bytes = 0;
  259. $agent_reply_bytes = current(unpack('N', $this->readBytes(4)));
  260. $agent_reply_data = $this->readBytes($agent_reply_bytes);
  261. $agent_reply_data = current(unpack('a*', $agent_reply_data));
  262. return pack('Na*', $agent_reply_bytes, $agent_reply_data);
  263. }
  264. }