includeSshLibrary(); } /** * Sends commands to remote script. * * @param string $command command to execute * @return string output of remote script */ public function execute($command) { if ($this->server == null) { return array(); } return $this->server->exec("sudo " . $_SESSION['config']->get_scriptPath() . ' ' . escapeshellarg($command)); } /** * Connects to the given SSH server. * * @param String $server server name (e.g. localhost or localhost,1234) */ public function connect($server) { $serverNameParts = explode(",", $server); $handle = false; if (sizeof($serverNameParts) > 1) { $handle = @new SSH2($serverNameParts[0], $serverNameParts[1]); } else { $handle = @new SSH2($server); } if (!$handle) { throw new LAMException(_("Unable to connect to remote server!")); } $this->loginSSH($handle); $this->server = $handle; } /** * Closes the connection. */ public function disconnect() { if ($this->server == null) { return; } $this->server->disconnect(); } /** * Performs a login to the provided SSH handle. * * @param SSH2 $handle SSH handle * @throws Exception login failed */ private function loginSSH($handle) { $username = $_SESSION['config']->getScriptUserName(); $ldapUser = $_SESSION['ldap']->getUserName(); if (empty($username)) { // get user name from current LAM user $sr = @ldap_read($_SESSION['ldap']->server(), $ldapUser, "objectClass=posixAccount", array('uid'), 0, 0, 0, LDAP_DEREF_NEVER); if ($sr) { $entry = @ldap_get_entries($_SESSION['ldap']->server(), $sr); if (!empty($entry[0]['uid'])) { $username = $entry[0]['uid'][0]; } } if (empty($username)) { throw new LAMException(sprintf(_("Your LAM admin user (%s) must be a valid Unix account to work with lamdaemon!"), getAbstractDN($ldapUser))); } } $password = $_SESSION['ldap']->getPassword(); $keyPath = $_SESSION['config']->getScriptSSHKey(); if (!empty($keyPath)) { // use key authentication if (!file_exists($keyPath) || !is_readable($keyPath)) { throw new LAMException(sprintf(_("Unable to read %s."), htmlspecialchars($keyPath))); } $key = file_get_contents($keyPath); $rsa = new RSA(); $keyPassword = $_SESSION['config']->getScriptSSHKeyPassword(); if (!empty($keyPassword)) { $rsa->setPassword($keyPassword); } if (!$rsa->loadKey($key)) { throw new LAMException(sprintf(_("Unable to load key %s."), htmlspecialchars($keyPath))); } $password = $rsa; } $login = @$handle->login($username, $password); if (!$login) { throw new LAMException(_("Unable to login to remote server!")); } } /** * Include the SSH files. */ private function includeSshLibrary() { $prefix = dirname(__FILE__) . '/3rdParty/phpseclib/'; require_once($prefix . 'Crypt/Base.php'); require_once($prefix . 'Crypt/Blowfish.php'); require_once($prefix . 'Crypt/Hash.php'); require_once($prefix . 'Crypt/Random.php'); require_once($prefix . 'Crypt/RC4.php'); require_once($prefix . 'Crypt/Rijndael.php'); require_once($prefix . 'Crypt/AES.php'); require_once($prefix . 'Crypt/RSA.php'); require_once($prefix . 'Crypt/DES.php'); require_once($prefix . 'Crypt/TripleDES.php'); require_once($prefix . 'Crypt/Twofish.php'); require_once($prefix . 'Math/BigInteger.php'); require_once($prefix . 'System/SSH/Agent.php'); require_once($prefix . 'Net/SSH2.php'); } } ?>