PageRenderTime 37ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/code/classes/Daemon/SSHd/Channel.class.php

https://github.com/blekkzor/pinetd2
PHP | 152 lines | 121 code | 25 blank | 6 comment | 12 complexity | 26f08b43094c70cd05f89e6e8e92f839 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. namespace Daemon\SSHd;
  3. class Channel {
  4. private $tp; // transport
  5. protected $channel;
  6. protected $window_out;
  7. protected $window_in;
  8. protected $packet_max_out;
  9. protected $packet_max_in;
  10. protected $buf_out = '';
  11. protected $buf_in = '';
  12. protected $closed = false;
  13. protected $recv_spent = 0;
  14. final public function __construct($tp, $channel, $window, $packet_max, $pkt) {
  15. if (is_null($tp)) return;
  16. $this->tp = $tp;
  17. $this->channel = $channel;
  18. $this->window_out = $window;
  19. $this->window_in = $window;
  20. $this->packet_max_out = $packet_max;
  21. $this->packet_max_in = 32768;
  22. $this->init($pkt);
  23. }
  24. final public function getLogin() {
  25. return $this->tp->getLogin();
  26. }
  27. // return the amount of bytes we allow remote party to send us
  28. public function remoteWindow() {
  29. return $this->window_in;
  30. }
  31. // max packet remote can send us (can only be changed in init())
  32. public function maxPacket() {
  33. return $this->packet_max_in;
  34. }
  35. public function specificConfirmationData() {
  36. // overload me to add data in the SSH_MSG_CHANNEL_OPEN_CONFIRMATION packet
  37. return '';
  38. }
  39. public function request($request, $data) {
  40. $func = '_req_'.str_replace('-', '_', $request);
  41. if (is_callable(array($this, $func))) return $this->$func($data);
  42. echo "Request for $request denied.\n";
  43. return false;
  44. }
  45. protected function parseBuffer() {
  46. // default implementation: echo (overload me!)
  47. $this->send($this->buf_in);
  48. $this->buf_in = '';
  49. // refill window if needed
  50. if ($this->recv_spent > 1024) {
  51. $this->window($this->recv_spent);
  52. $this->recv_spent = 0;
  53. }
  54. }
  55. final public function translate($class) {
  56. $class = relativeclass($this, 'SFTP');
  57. $class = new $class(NULL,NULL,NULL,NULL,NULL);
  58. $class->tp = $this->tp;
  59. $class->channel = $this->channel;
  60. $class->window_out = $this->window_out;
  61. $class->window_in = $this->window_in;
  62. $class->packet_max_out = $this->packet_max_out;
  63. $class->packet_max_in = $this->packet_max_in;
  64. $class->buf_out = $this->buf_out;
  65. $class->buf_in = $this->buf_in;
  66. $class->closed = $this->closed;
  67. $class->recv_spent = $this->recv_spent;
  68. $this->tp->channelChangeObject($this->channel, $class);
  69. return $class;
  70. }
  71. final public function window($bytes) {
  72. $this->tp->channelWindow($this->channel, $bytes);
  73. $this->window_in += $bytes;
  74. }
  75. final public function closed() {
  76. $this->closed = true;
  77. }
  78. public function gotEof() {
  79. // overload me to receive EOF event
  80. }
  81. final public function eof() {
  82. if ($this->closed) return false;
  83. $this->tp->channelEof($this->channel);
  84. }
  85. final public function close() {
  86. if ($this->closed) return false;
  87. $this->tp->channelClose($this->channel);
  88. }
  89. final public function recv($str) {
  90. $this->recv_spent += strlen($str);
  91. $this->window_in -= strlen($str);
  92. $this->buf_in .= $str;
  93. $this->parseBuffer();
  94. }
  95. final public function send($str) {
  96. if ($this->closed) return false;
  97. $this->buf_out .= $str;
  98. $max_send = min($this->window_out, strlen($this->buf_out), $this->packet_max_out);
  99. if ($max_send == 0) return;
  100. $this->tp->channelWrite($this->channel, substr($this->buf_out, 0, $max_send));
  101. $this->window_out -= $max_send;
  102. $this->buf_out = substr($this->buf_out, $max_send);
  103. }
  104. public function windowAdjust($bytes) {
  105. $this->window_out += $bytes;
  106. $this->send(''); // force flush if possible
  107. }
  108. protected function str($str) {
  109. return pack('N', strlen($str)).$str;
  110. }
  111. protected function parseInt32(&$pkt) {
  112. list(,$int) = unpack('N', substr($pkt, 0, 4));
  113. $pkt = substr($pkt, 4);
  114. return $int;
  115. }
  116. protected function parseStr(&$pkt) {
  117. list(,$len) = unpack('N', substr($pkt, 0, 4));
  118. if ($len == 0) {
  119. $pkt = substr($pkt, 4);
  120. return '';
  121. }
  122. if ($len+4 > strlen($pkt)) return false;
  123. $res = substr($pkt, 4, $len);
  124. $pkt = substr($pkt, $len+4);
  125. return $res;
  126. }
  127. }