├── README.md ├── LICENSE └── phpseclib.php /README.md: -------------------------------------------------------------------------------- 1 | libssh2-compatibility-layer 2 | =========================== 3 | 4 | Let's say your application has calls to [PHP's ssh2_* functions](http://php.net/ssh2) but you don't have libssh2 installed. With libssh2-compatibility-layer all you need to do is include phpseclib.php and (assuming you have phpseclib installed in the include_path) and they should work without issue! 5 | 6 | An example follows: 7 | 8 | ```php 9 | enableQuietMode(); 35 | return $session; 36 | } 37 | 38 | function ssh2_auth_agent($session, $username) 39 | { 40 | $agent = new System_SSH_Agent(); 41 | return $session->login($username, $agent); 42 | } 43 | 44 | // phpseclib doesn't support hostbased authentication 45 | function ssh2_auth_hostbased_file($session, $username, $hostname, $pubkeyfile, $privkeyfile, $passphrase = NULL, $local_username = NULL) 46 | { 47 | return false; 48 | } 49 | 50 | // phpseclib does not provide a mechanism to get supported authentication methods 51 | function ssh2_auth_none($session, $username) 52 | { 53 | if ($session->login($username)) { 54 | return true; 55 | } 56 | 57 | return array(); 58 | } 59 | 60 | function ssh2_auth_password($session, $username, $password) 61 | { 62 | return $session->login($username, $password); 63 | } 64 | 65 | // only RSA keys are currently supported 66 | function ssh2_auth_pubkey_file($session, $username, $pubkeyfile, $privkeyfile, $passphrase = NULL) 67 | { 68 | $privkey = new Crypt_RSA(); 69 | if (isset($passphrase)) { 70 | $privkey->setPassword($passphrase); 71 | } 72 | $privkey->loadKey(file_get_contents($privkeyfile)); 73 | if ($privkey === false) { 74 | return false; 75 | } 76 | return $session->login($username, $privkey); 77 | } 78 | 79 | // phpseclib only supports one type of pty: vt100 80 | // environmental variables cannot be set through phpseclib 81 | // the only $width_height_type option supported is SSH2_TERM_UNIT_CHARS 82 | function ssh2_exec($session, $command, $pty = NULL, $env = NULL, $width = 80, $height = 25, $width_height_type = SSH2_TERM_UNIT_CHARS) 83 | { 84 | if (isset($pty)) { 85 | $session->enablePTY(); 86 | } 87 | if ($width_height_type == SSH2_TERM_UNIT_CHARS) { 88 | $session->setWindowSize($width, $height); 89 | } 90 | $res = fopen('php://memory', 'w+'); 91 | fputs($res, $session->exec($command)); 92 | rewind($res); 93 | if (isset($pty)) { 94 | $session->disablePTY(); 95 | } 96 | $session->setWindowSize(80, 24); 97 | return $res; 98 | } 99 | 100 | // phpseclib does not work in the same way libssh2 does. ssh2_exec returns 101 | // a resource stream that you can do fread on. phpseclib's Net_SSH2::exec() 102 | // returns a string. ssh2_exec() emulates libssh2 by dumping the string to 103 | // php://memory but it's only an emulation and you can't extract $session 104 | // from $channel. with phpseclib the way you'd get STDERR is by doing 105 | // $session->getStdError() 106 | function ssh2_fetch_stream($channel, $streamid) 107 | { 108 | return false; 109 | } 110 | 111 | function ssh2_fingerprint($session, $flags = 0) 112 | { 113 | $hostkey = substr($session->getServerPublicHostKey(), 8); 114 | $hostkey = ($flags & 1) ? sha1($hostkey) : md5($hostkey); 115 | return ($flags & 2) ? pack('H*', $hostkey) : strtoupper($hostkey); 116 | } 117 | 118 | function ssh2_methods_negotiated($session) 119 | { 120 | return array( 121 | 'client_to_server' => array( 122 | 'crypt' => $session->getEncryptionAlgorithmsClient2Server(), 123 | 'comp' => $session->getCompressionAlgorithmsClient2Server(), 124 | 'mac' => $session->getMACAlgorithmsClient2Server()), 125 | 'server_to_client' => array( 126 | 'crypt' => $session->getEncryptionAlgorithmsServer2Client(), 127 | 'comp' => $session->getCompressionAlgorithmsServer2Client(), 128 | 'mac' => $session->getMACAlgorithmsServer2Client()) 129 | ); 130 | } 131 | 132 | // not implemented in phpseclib 133 | function ssh2_publickey_add($pkey, $algoname, $blob, $overwrite = false, $attributes = array()) 134 | { 135 | return false; 136 | } 137 | 138 | // not implemented in phpseclib 139 | function ssh2_publickey_init($session) 140 | { 141 | return false; 142 | } 143 | 144 | // not implemented in phpseclib 145 | function ssh2_publickey_list($pkey) 146 | { 147 | return false; 148 | } 149 | 150 | // not implemented in phpseclib 151 | function ssh2_publickey_remove($pkey, $algoname, $blob) 152 | { 153 | return false; 154 | } 155 | 156 | function ssh2_scp_recv($session, $remote_file, $local_file) 157 | { 158 | $scp = new Net_SCP($session); 159 | return $scp->get($remote_file, $local_file); 160 | } 161 | 162 | // phpseclib does not let you change the $create_mode 163 | function ssh2_scp_send($session, $local_file, $remote_file, $create_mode = 0644) 164 | { 165 | $scp = new Net_SCP($session); 166 | return $scp->put($remote_file, $local_file, NET_SCP_LOCAL_FILE); 167 | } 168 | 169 | function ssh2_sftp($session) 170 | { 171 | return $session; 172 | } 173 | 174 | function ssh2_sftp_chmod($sftp, $filename, $mode) 175 | { 176 | return $sftp->chmod($mode, $filename) !== false; 177 | } 178 | 179 | function ssh2_sftp_lstat($sftp, $path) 180 | { 181 | return $sftp->lstat($path); 182 | } 183 | 184 | function ssh2_sftp_stat($sftp, $path) 185 | { 186 | return $sftp->stat($path); 187 | } 188 | 189 | function ssh2_sftp_mkdir($sftp, $dirname, $mode = 0777, $recursive = false) 190 | { 191 | return $sftp->mkdir($dirname, $mode, $recursive); 192 | } 193 | 194 | function ssh2_sftp_readlink($sftp, $link) 195 | { 196 | return $sftp->readlink($link); 197 | } 198 | 199 | function ssh2_sftp_symlink($sftp, $target, $link) 200 | { 201 | return $sftp->symlink($target, $link); 202 | } 203 | 204 | function ssh2_sftp_unlink($sftp, $filename) 205 | { 206 | return $sftp->delete($filename, false); 207 | } 208 | 209 | // phpseclib supports this via $ssh->read() / $ssh->write() but it is not currently possible to make that 210 | // work with fread() / fwrite() in the way this function does. a yet to be written stream wrapper may do 211 | // the trick though.. hard to say. 212 | function ssh2_shell($session, $term_type = 'vanilla', $env = NULL, $width = 80, $height = 25, $width_height_type = 0) 213 | { 214 | return false; 215 | } 216 | 217 | // phpseclib doesn't currently support tunneling 218 | function ssh2_tunnel($session, $host, $port) 219 | { 220 | return false; 221 | } 222 | } 223 | --------------------------------------------------------------------------------