Index: trunk/extensions/Deployment/includes/filesystems/Ssh2Filesystem.php |
— | — | @@ -27,11 +27,18 @@ |
28 | 28 | /** |
29 | 29 | * The FTP connection link. |
30 | 30 | * |
31 | | - * @var resource |
| 31 | + * @var FTP resource |
32 | 32 | */ |
33 | 33 | protected $connection; |
34 | 34 | |
35 | 35 | /** |
| 36 | + * The SFTP connection link. |
| 37 | + * |
| 38 | + * @var SSH2 SFTP resource |
| 39 | + */ |
| 40 | + protected $sftpConnection; |
| 41 | + |
| 42 | + /** |
36 | 43 | * Indicates if public key authentication is used instead of a regular password. |
37 | 44 | * |
38 | 45 | * @var boolean |
— | — | @@ -68,12 +75,17 @@ |
69 | 76 | // TODO: validate that both keys are set (error if only one) |
70 | 77 | $this->publicKeyAuthentication = array_key_exists( 'public_key', $options ) && array_key_exists( 'private_key', $options ); |
71 | 78 | |
| 79 | + if ( $this->publicKeyAuthentication ) { |
| 80 | + $options['hostkey'] = array( 'hostkey' => 'ssh-rsa' ); |
| 81 | + } |
| 82 | + |
72 | 83 | // Regular authentication needs a username. |
73 | 84 | if ( !$this->publicKeyAuthentication && !array_key_exists( 'username', $options ) ) { |
74 | 85 | $this->addError( 'deploy-ssh2-username-required' ); |
75 | 86 | } |
76 | 87 | |
77 | 88 | // Regular authentication needs a password. |
| 89 | + // TODO: if publick key: make sure the key is not empty |
78 | 90 | if ( !$this->publicKeyAuthentication && !array_key_exists( 'password', $options ) ) { |
79 | 91 | $this->addError( 'deploy-ssh2-password-required' ); |
80 | 92 | } |
— | — | @@ -91,7 +103,41 @@ |
92 | 104 | * @see Filesystem::connect |
93 | 105 | */ |
94 | 106 | public function connect() { |
95 | | - |
| 107 | + if ( $this->publicKeyAuthentication ) { |
| 108 | + wfSuppressWarnings(); |
| 109 | + $this->connection = ssh2_connect( $this->options['hostname'], $this->options['port'], $this->options['hostkey'] ); |
| 110 | + wfRestoreWarnings(); |
| 111 | + } else { |
| 112 | + wfSuppressWarnings(); |
| 113 | + $this->connection = ssh2_connect( $this->options['hostname'], $this->options['port'] ); |
| 114 | + wfRestoreWarnings(); |
| 115 | + } |
| 116 | + |
| 117 | + if ( !$this->connection ) { |
| 118 | + $this->addErrorMessage( wfMsgExt( 'deploy-ssh2-connect-failed', $this->options['hostname'], $this->options['port'] ) ); |
| 119 | + return false; |
| 120 | + } |
| 121 | + |
| 122 | + if ( $this->publicKeyAuthentication ) { |
| 123 | + $ssh2_auth_pubkey_file = ssh2_auth_pubkey_file($this->link, $this->options['username'], $this->options['public_key'], $this->options['private_key'], $this->options['password'] ); |
| 124 | + |
| 125 | + if ( !$ssh2_auth_pubkey_file ) { |
| 126 | + $this->addErrorMessage( wfMsgExt( 'deploy-ssh2-key-authentication-failed', $this->options['username'] ) ); |
| 127 | + return false; |
| 128 | + } |
| 129 | + |
| 130 | + } else { |
| 131 | + $ssh2_auth_password = ssh2_auth_password( $this->connection, $this->options['username'], $this->options['password'] ); |
| 132 | + |
| 133 | + if ( !$ssh2_auth_password ) { |
| 134 | + $this->addErrorMessage( wfMsgExt( 'deploy-ssh2-password-authentication-failed', $this->options['username'] ) ); |
| 135 | + return false; |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + $this->sftpConnection = ssh2_sftp( $this->connection ); |
| 140 | + |
| 141 | + return true; |
96 | 142 | } |
97 | 143 | |
98 | 144 | /** |