ssh.inc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. /**
  3. * The SSH connection class for the update module.
  4. */
  5. class FileTransferSSH extends FileTransfer implements FileTransferChmodInterface {
  6. function __construct($jail, $username, $password, $hostname = "localhost", $port = 22) {
  7. $this->username = $username;
  8. $this->password = $password;
  9. $this->hostname = $hostname;
  10. $this->port = $port;
  11. parent::__construct($jail);
  12. }
  13. function connect() {
  14. $this->connection = @ssh2_connect($this->hostname, $this->port);
  15. if (!$this->connection) {
  16. throw new FileTransferException('SSH Connection failed to @host:@port', NULL, array('@host' => $this->hostname, '@port' => $this->port));
  17. }
  18. if (!@ssh2_auth_password($this->connection, $this->username, $this->password)) {
  19. throw new FileTransferException('The supplied username/password combination was not accepted.');
  20. }
  21. }
  22. static function factory($jail, $settings) {
  23. $username = empty($settings['username']) ? '' : $settings['username'];
  24. $password = empty($settings['password']) ? '' : $settings['password'];
  25. $hostname = empty($settings['advanced']['hostname']) ? 'localhost' : $settings['advanced']['hostname'];
  26. $port = empty($settings['advanced']['port']) ? 22 : $settings['advanced']['port'];
  27. return new FileTransferSSH($jail, $username, $password, $hostname, $port);
  28. }
  29. protected function copyFileJailed($source, $destination) {
  30. if (!@ssh2_scp_send($this->connection, $source, $destination)) {
  31. throw new FileTransferException('Cannot copy @source_file to @destination_file.', NULL, array('@source' => $source, '@destination' => $destination));
  32. }
  33. }
  34. protected function copyDirectoryJailed($source, $destination) {
  35. if (@!ssh2_exec($this->connection, 'cp -Rp ' . escapeshellarg($source) . ' ' . escapeshellarg($destination))) {
  36. throw new FileTransferException('Cannot copy directory @directory.', NULL, array('@directory' => $source));
  37. }
  38. }
  39. protected function createDirectoryJailed($directory) {
  40. if (@!ssh2_exec($this->connection, 'mkdir ' . escapeshellarg($directory))) {
  41. throw new FileTransferException('Cannot create directory @directory.', NULL, array('@directory' => $directory));
  42. }
  43. }
  44. protected function removeDirectoryJailed($directory) {
  45. if (@!ssh2_exec($this->connection, 'rm -Rf ' . escapeshellarg($directory))) {
  46. throw new FileTransferException('Cannot remove @directory.', NULL, array('@directory' => $directory));
  47. }
  48. }
  49. protected function removeFileJailed($destination) {
  50. if (!@ssh2_exec($this->connection, 'rm ' . escapeshellarg($destination))) {
  51. throw new FileTransferException('Cannot remove @directory.', NULL, array('@directory' => $destination));
  52. }
  53. }
  54. /**
  55. * WARNING: This is untested. It is not currently used, but should do the trick.
  56. */
  57. public function isDirectory($path) {
  58. $directory = escapeshellarg($path);
  59. $cmd = "[ -d {$directory} ] && echo 'yes'";
  60. if ($output = @ssh2_exec($this->connection, $cmd)) {
  61. if ($output == 'yes') {
  62. return TRUE;
  63. }
  64. return FALSE;
  65. }
  66. else {
  67. throw new FileTransferException('Cannot check @path.', NULL, array('@path' => $path));
  68. }
  69. }
  70. public function isFile($path) {
  71. $file = escapeshellarg($path);
  72. $cmd = "[ -f {$file} ] && echo 'yes'";
  73. if ($output = @ssh2_exec($this->connection, $cmd)) {
  74. if ($output == 'yes') {
  75. return TRUE;
  76. }
  77. return FALSE;
  78. }
  79. else {
  80. throw new FileTransferException('Cannot check @path.', NULL, array('@path' => $path));
  81. }
  82. }
  83. function chmodJailed($path, $mode, $recursive) {
  84. $cmd = sprintf("chmod %s%o %s", $recursive ? '-R ' : '', $mode, escapeshellarg($path));
  85. if (@!ssh2_exec($this->connection, $cmd)) {
  86. throw new FileTransferException('Cannot change permissions of @path.', NULL, array('@path' => $path));
  87. }
  88. }
  89. /**
  90. * Returns the form to configure the FileTransfer class for SSH.
  91. */
  92. public function getSettingsForm() {
  93. $form = parent::getSettingsForm();
  94. $form['advanced']['port']['#default_value'] = 22;
  95. return $form;
  96. }
  97. }