/** * {@inheritDoc} */ public function authenticate($session) { return ssh2_auth_hostbased_file($session, $this->username, $this->hostname, $this->publicKeyFile, $this->privateKeyFile, $this->passPhrase, $this->localUsername); }
/** * Create SSH connection */ protected function makeConnection() { $host = $this->host; // Extract from hostname $matches = null; if (!preg_match('/^(?:([^:@]++)(?:\\:([^:@]++))?@)?([^:@]++)(?:\\:([^:@]++))?$/', $host, $matches)) { throw new Exception("Could not create SSH connection: Illegal host string."); } $matches = $matches + array_fill(0, 5, null); list(, $username, $password, $host, $port) = $matches; // Get user/password if (!empty($this->options->username)) { $username = $this->options->username; } if (empty($username)) { $_tmp_ = posix_getpwuid(posix_getuid()); $username = $_tmp_['name']; } if (!empty($this->options->password)) { $password = $this->options->password; } // Get port $port = !empty($this->options->port) ? $this->options->port : 22; if (isset($port) && !is_int($port) && !ctype_digit($port)) { throw new Exception("Could not create SSH connection for '{$host}': Given port '{$port}' is not a numeric value."); } // Get methods and callbacks if (!isset($this->options->methods)) { $this->options->methods = array_chunk_assoc($this->options, 'methods'); } if (!isset($this->options->callbacks)) { $this->options->callbacks = array_chunk_assoc($this->options, 'callbacks'); } // Make the connection $this->connection = ssh2_connect($host, $port, $this->options->methods, $this->options->callbacks); if (!$this->connection) { throw new Exception("Could not create SSH connection for '{$host}:{$port}': Failed to connect to server."); } // Autenticate $auth_methods = isset($this->options->auth) ? (array) $this->options->auth : ssh2_auth_none($this->connection, $username); $authenticated = $auth_methods === true; while (!$authenticated && current($auth_methods)) { switch (current($auth_methods)) { case 'none': $authenticated = @ssh2_auth_none($this->connection, $username) === true; break; case 'password': $authenticated = isset($password) && @ssh2_auth_password($this->connection, $username, $password); break; case 'publickey': $authenticated = @ssh2_auth_pubkey_file($this->connection, $username, $this->options->pubkeyfile, $this->options->privkeyfile, $this->options->passphrase); break; case 'hostbased': $authenticated = @ssh2_auth_hostbased_file($this->connection, $username, $this->options->hostbased, $this->options->pubkeyfile, $this->options->privkeyfile, $this->options->passphrase); break; } next($auth_methods); } if (!$authenticated) { throw new Exception("Could not create SSH connection for '{$host}:{$port}': Authentication for user '{$username}' failed (" . join(', ', $auth_methods) . ")."); } }
/** * @param $resource * * @throws AuthenticationFailed */ public function authenticate($resource) { if (true !== ssh2_auth_hostbased_file($resource, $this->username, $this->pubkeyFile, $this->privkeyFile, $this->passphrase, $this->localUser)) { throw new AuthenticationFailed(); } }
/** * Run SSH authentication based on the selected method * * @param resource $connection * @param int $method * @param mixed ...$args * * @return void */ protected function __authenticate($connection, $method, ...$args) { switch ($method) { case SecureShell::AUTH_PASSWORD: return ssh2_auth_password($connection, ...$args); case SecureShell::AUTH_PUBKEY: return ssh2_auth_pubkey_file($connection, ...$args); case SecureShell::AUTH_HOSTKEY: return ssh2_auth_hostbased_file($connection, ...$args); case SecureShell::AUTH_AGENT: return ssh2_auth_agent($connection, ...$args); case SecureShell::AUTH_NONE: return ssh2_auth_none($connection, ...$args); default: throw new ModuleException(__CLASS__, 'Unsupported authentication method'); } }