From c4cf288f4bf4bf6b479f3987bd288edd7728f3fe Mon Sep 17 00:00:00 2001 From: Roland Gruber Date: Sun, 23 May 2010 13:33:04 +0000 Subject: [PATCH] use phpseclib for SSH connection --- lam/lib/lamdaemon.inc | 51 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/lam/lib/lamdaemon.inc b/lam/lib/lamdaemon.inc index af5efc5a..6faef023 100644 --- a/lam/lib/lamdaemon.inc +++ b/lam/lib/lamdaemon.inc @@ -40,6 +40,8 @@ $Id$ * */ function lamdaemon($command, $server) { + // remove the following line to restore SSH via PHP SSH2 + return lamdaemonSeclib($command, $server); if (!function_exists('ssh2_connect')) { return array('ERROR,' . _('This module requires the PHP ssh2 extension.')); } @@ -93,4 +95,53 @@ function lamdaemon($command, $server) { } } +/** +* Sends commands to lamdaemon script. +* +* @param array $command command to execute +* @param string $server remote server +* @return array Output of lamdaemon +* +*/ +function lamdaemonSeclib($command, $server) { + // add phpseclib to include path + set_include_path(get_include_path() . PATH_SEPARATOR . dirname(__FILE__) . '/3rdParty/phpseclib'); + include_once('Net/SSH2.php'); + // get username and password of the current lam-admin + $credentials = $_SESSION['ldap']->decrypt_login(); + $serverNameParts = explode(",", $server); + if (sizeof($serverNameParts) > 1) { + $handle = new Net_SSH2($serverNameParts[0], $serverNameParts[1]); + } + else { + $handle = new Net_SSH2($server); + } + if ($handle) { + $sr = @ldap_read($_SESSION['ldap']->server(), $credentials[0], "objectClass=posixAccount", array('uid'), 0, 0, 0, LDAP_DEREF_NEVER); + if (!$sr) { + $return = array("ERROR," . _("Your LAM admin user must be a valid Unix account to work with lamdaemon!") . ","); + return $return; + } + $entry = @ldap_get_entries($_SESSION['ldap']->server(), $sr); + if (!isset($entry[0]['uid'][0])) { + $return = array("ERROR," . _("Your LAM admin user must be a valid Unix account to work with lamdaemon!") . ","); + return $return; + } + $userName = $entry[0]['uid'][0]; + if ($handle->login($userName, $credentials[1])) { + $output = $handle->exec("sudo " . $_SESSION['config']->get_scriptPath() . ' ' . escapeshellarg($command)); + $return = array($output); + return $return; + } + else { + $return = array("ERROR," . _('Unable to connect to remote server!') . "," . $server); + return $return; + } + } + else { + $return = array("ERROR," . _('Unable to connect to remote server!') . "," . $server); + return $return; + } +} + ?>