Merge pull request #74 from LDAPAccountManager/refactoring_lamdaemon

Refactoring lamdaemon
This commit is contained in:
gruberroland 2019-10-23 21:27:27 +02:00 committed by GitHub
commit f21f8ee423
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 58 additions and 37 deletions

View File

@ -1475,6 +1475,22 @@ class LAMConfig {
return $servers; return $servers;
} }
/**
* Returns the script server object for the given name.
*
* @param string $serverName server name
* @return RemoteServerConfiguration server
*/
public function getScriptServerByName($serverName) {
$serverList = $this->getConfiguredScriptServers();
foreach ($serverList as $server) {
if ($serverName === $server->getServer()) {
return $server;
}
}
return null;
}
/** /**
* Sets the servers of the external script * Sets the servers of the external script
* *

View File

@ -708,17 +708,13 @@ class posixAccount extends baseModule implements passwordService {
// create home directories if needed // create home directories if needed
$homeDirAttr = $this->getHomedirAttrName($modules); $homeDirAttr = $this->getHomedirAttrName($modules);
$lamdaemonServerList = $_SESSION['config']->getConfiguredScriptServers(); $lamdaemonServerList = $_SESSION['config']->getConfiguredScriptServers();
$lamdaemonServerNames = array();
foreach ($lamdaemonServerList as $lamdaemonServer) {
$lamdaemonServerNames[] = $lamdaemonServer->getServer();
}
if (sizeof($this->lamdaemonServers) > 0) { if (sizeof($this->lamdaemonServers) > 0) {
foreach ($lamdaemonServerNames as $server) { foreach ($lamdaemonServerList as $lamdaemonServer) {
if (!in_array($server, $this->lamdaemonServers)) { if (!in_array($lamdaemonServer->getServer(), $this->lamdaemonServers)) {
continue; continue;
} }
$remote = new \LAM\REMOTE\Remote(); $remote = new \LAM\REMOTE\Remote();
$remote->connect($server); $remote->connect($lamdaemonServer);
$result = $remote->execute( $result = $remote->execute(
implode( implode(
self::$SPLIT_DELIMITER, self::$SPLIT_DELIMITER,
@ -747,9 +743,9 @@ class posixAccount extends baseModule implements passwordService {
// move home directory if needed // move home directory if needed
if (!empty($this->orig[$homeDirAttr][0]) && !empty($this->attributes[$homeDirAttr][0]) if (!empty($this->orig[$homeDirAttr][0]) && !empty($this->attributes[$homeDirAttr][0])
&& ($this->orig[$homeDirAttr][0] != $this->attributes[$homeDirAttr][0])) { && ($this->orig[$homeDirAttr][0] != $this->attributes[$homeDirAttr][0])) {
foreach ($lamdaemonServerNames as $server) { foreach ($lamdaemonServerList as $lamdaemonServer) {
$remote = new \LAM\REMOTE\Remote(); $remote = new \LAM\REMOTE\Remote();
$remote->connect($server); $remote->connect($lamdaemonServer);
$result = $remote->execute( $result = $remote->execute(
implode( implode(
self::$SPLIT_DELIMITER, self::$SPLIT_DELIMITER,
@ -774,9 +770,9 @@ class posixAccount extends baseModule implements passwordService {
// set new group on homedirectory // set new group on homedirectory
if (!empty($this->orig[$homeDirAttr][0]) && !empty($this->attributes[$homeDirAttr][0]) if (!empty($this->orig[$homeDirAttr][0]) && !empty($this->attributes[$homeDirAttr][0])
&& ($this->orig['gidNumber'][0] != $this->attributes['gidNumber'][0])) { && ($this->orig['gidNumber'][0] != $this->attributes['gidNumber'][0])) {
foreach ($lamdaemonServerNames as $server) { foreach ($lamdaemonServerList as $lamdaemonServer) {
$remote = new \LAM\REMOTE\Remote(); $remote = new \LAM\REMOTE\Remote();
$remote->connect($server); $remote->connect($lamdaemonServer);
$result = $remote->execute( $result = $remote->execute(
implode( implode(
self::$SPLIT_DELIMITER, self::$SPLIT_DELIMITER,
@ -784,7 +780,7 @@ class posixAccount extends baseModule implements passwordService {
$this->attributes['uid'][0], $this->attributes['uid'][0],
"home", "home",
"chgrp", "chgrp",
$this->orig[$homeDirAttr][0], $this->attributes[$homeDirAttr][0],
$this->attributes['uidNumber'][0], $this->attributes['uidNumber'][0],
$this->attributes['gidNumber'][0]) $this->attributes['gidNumber'][0])
)); ));
@ -935,7 +931,7 @@ class posixAccount extends baseModule implements passwordService {
foreach ($lamdaemonServers as $lamdaemonServer) { foreach ($lamdaemonServers as $lamdaemonServer) {
$remote = new \LAM\REMOTE\Remote(); $remote = new \LAM\REMOTE\Remote();
try { try {
$remote->connect($lamdaemonServer->getServer()); $remote->connect($lamdaemonServer);
$result = $remote->execute( $result = $remote->execute(
implode( implode(
self::$SPLIT_DELIMITER, self::$SPLIT_DELIMITER,
@ -1469,10 +1465,9 @@ class posixAccount extends baseModule implements passwordService {
$modules = $this->getAccountContainer()->get_type()->getModules(); $modules = $this->getAccountContainer()->get_type()->getModules();
$homeDirAttr = $this->getHomedirAttrName($modules); $homeDirAttr = $this->getHomedirAttrName($modules);
for ($i = 0; $i < sizeof($lamdaemonServers); $i++) { for ($i = 0; $i < sizeof($lamdaemonServers); $i++) {
$server = $lamdaemonServers[$i]->getServer();
if (isset($_POST['form_subpage_' . get_class($this) . '_homedir_create_' . $i])) { if (isset($_POST['form_subpage_' . get_class($this) . '_homedir_create_' . $i])) {
$remote = new \LAM\REMOTE\Remote(); $remote = new \LAM\REMOTE\Remote();
$remote->connect($server); $remote->connect($lamdaemonServers[$i]);
$result = $remote->execute( $result = $remote->execute(
implode( implode(
self::$SPLIT_DELIMITER, self::$SPLIT_DELIMITER,
@ -1498,7 +1493,7 @@ class posixAccount extends baseModule implements passwordService {
} }
elseif (isset($_POST['form_subpage_' . get_class($this) . '_homedir_delete_' . $i])) { elseif (isset($_POST['form_subpage_' . get_class($this) . '_homedir_delete_' . $i])) {
$remote = new \LAM\REMOTE\Remote(); $remote = new \LAM\REMOTE\Remote();
$remote->connect($server); $remote->connect($lamdaemonServers[$i]);
$result = $remote->execute( $result = $remote->execute(
implode( implode(
self::$SPLIT_DELIMITER, self::$SPLIT_DELIMITER,
@ -1910,10 +1905,9 @@ class posixAccount extends baseModule implements passwordService {
// get list of lamdaemon servers // get list of lamdaemon servers
$lamdaemonServers = $_SESSION['config']->getConfiguredScriptServers(); $lamdaemonServers = $_SESSION['config']->getConfiguredScriptServers();
for ($i = 0; $i < sizeof($lamdaemonServers); $i++) { for ($i = 0; $i < sizeof($lamdaemonServers); $i++) {
$server = $lamdaemonServers[$i]->getServer();
$label = $lamdaemonServers[$i]->getLabel(); $label = $lamdaemonServers[$i]->getLabel();
$remote = new \LAM\REMOTE\Remote(); $remote = new \LAM\REMOTE\Remote();
$remote->connect($server); $remote->connect($lamdaemonServers[$i]);
$result = $remote->execute( $result = $remote->execute(
implode( implode(
self::$SPLIT_DELIMITER, self::$SPLIT_DELIMITER,
@ -2887,7 +2881,8 @@ class posixAccount extends baseModule implements passwordService {
$pos = $temp['createHomes'][$temp['counter'] - sizeof($temp['groups'])]; $pos = $temp['createHomes'][$temp['counter'] - sizeof($temp['groups'])];
try { try {
$remote = new \LAM\REMOTE\Remote(); $remote = new \LAM\REMOTE\Remote();
$remote->connect($data[$pos][$ids['posixAccount_createHomeDir']]); $remoteServer = $_SESSION['config']->getScriptServerByName($data[$pos][$ids['posixAccount_createHomeDir']]);
$remote->connect($remoteServer);
$result = $remote->execute( $result = $remote->execute(
implode( implode(
self::$SPLIT_DELIMITER, self::$SPLIT_DELIMITER,

View File

@ -174,11 +174,15 @@ class quota extends baseModule {
$userName = '+'; $userName = '+';
if (($this->getAccountContainer() != null) && !$this->getAccountContainer()->isNewAccount) { if (($this->getAccountContainer() != null) && !$this->getAccountContainer()->isNewAccount) {
if ($this->get_scope() == 'user') { if ($this->get_scope() == 'user') {
if (!isset($this->attributes['uid'][0])) return; if (!isset($this->attributes['uid'][0])) {
return;
}
$userName = $this->attributes['uid'][0]; $userName = $this->attributes['uid'][0];
} }
else if ($this->get_scope() == 'group') { else if ($this->get_scope() == 'group') {
if (!isset($this->attributes['cn'][0])) return; if (!isset($this->attributes['cn'][0])) {
return;
}
$userName = $this->attributes['cn'][0]; $userName = $this->attributes['cn'][0];
} }
} }
@ -188,7 +192,7 @@ class quota extends baseModule {
$server = $lamdaemonServer->getServer(); $server = $lamdaemonServer->getServer();
// get quotas // get quotas
$remote = new \LAM\REMOTE\Remote(); $remote = new \LAM\REMOTE\Remote();
$remote->connect($server); $remote->connect($lamdaemonServer);
$quotas = $remote->execute(implode(quota::$SPLIT_DELIMITER, array($userName, "quota", "get", $this->get_scope()))); $quotas = $remote->execute(implode(quota::$SPLIT_DELIMITER, array($userName, "quota", "get", $this->get_scope())));
$remote->disconnect(); $remote->disconnect();
if (empty($quotas)) { if (empty($quotas)) {
@ -289,7 +293,8 @@ class quota extends baseModule {
$quotastring .= ',' . $this->getQuotaNumber($this->quota[$server][$i][7]) . ':'; $quotastring .= ',' . $this->getQuotaNumber($this->quota[$server][$i][7]) . ':';
} }
$remote = new \LAM\REMOTE\Remote(); $remote = new \LAM\REMOTE\Remote();
$remote->connect($server); $remoteServer = $_SESSION['config']->getScriptServerByName($server);
$remote->connect($remoteServer);
$remote->execute(implode(quota::$SPLIT_DELIMITER, array($id, "quota", "set", $this->get_scope(), "$quotastring\n"))); $remote->execute(implode(quota::$SPLIT_DELIMITER, array($id, "quota", "set", $this->get_scope(), "$quotastring\n")));
$remote->disconnect(); $remote->disconnect();
} }
@ -367,7 +372,8 @@ class quota extends baseModule {
$i++; $i++;
} }
$remote = new \LAM\REMOTE\Remote(); $remote = new \LAM\REMOTE\Remote();
$remote->connect($server); $remoteServer = $_SESSION['config']->getScriptServerByName($server);
$remote->connect($remoteServer);
$remote->execute(implode(quota::$SPLIT_DELIMITER, array($id, "quota", "set", $this->get_scope(), "$quotastring\n"))); $remote->execute(implode(quota::$SPLIT_DELIMITER, array($id, "quota", "set", $this->get_scope(), "$quotastring\n")));
$remote->disconnect(); $remote->disconnect();
} }
@ -528,7 +534,7 @@ class quota extends baseModule {
} }
// Get quotas // Get quotas
$remote = new \LAM\REMOTE\Remote(); $remote = new \LAM\REMOTE\Remote();
$remote->connect($server); $remote->connect($lamdaemonServer);
$quotas = $remote->execute(implode(quota::$SPLIT_DELIMITER, array("+", "quota", "get", $this->get_scope()))); $quotas = $remote->execute(implode(quota::$SPLIT_DELIMITER, array("+", "quota", "get", $this->get_scope())));
$remote->disconnect(); $remote->disconnect();
if (empty($quotas)) { if (empty($quotas)) {
@ -592,7 +598,7 @@ class quota extends baseModule {
$id = $this->replaceSpecialChars($server); $id = $this->replaceSpecialChars($server);
// Get quotas // Get quotas
$remote = new \LAM\REMOTE\Remote(); $remote = new \LAM\REMOTE\Remote();
$remote->connect($server); $remote->connect($lamdaemonServer);
$quotas = $remote->execute(implode(quota::$SPLIT_DELIMITER, array("+", "quota", "get", $this->get_scope()))); $quotas = $remote->execute(implode(quota::$SPLIT_DELIMITER, array("+", "quota", "get", $this->get_scope())));
$remote->disconnect(); $remote->disconnect();
$dirs = explode(":", $quotas); $dirs = explode(":", $quotas);
@ -753,7 +759,7 @@ class quota extends baseModule {
$server = $lamdaemonServer->getServer(); $server = $lamdaemonServer->getServer();
// Get quotas // Get quotas
$remote = new \LAM\REMOTE\Remote(); $remote = new \LAM\REMOTE\Remote();
$remote->connect($server); $remote->connect($lamdaemonServer);
$quotas = $remote->execute(implode(quota::$SPLIT_DELIMITER, array("+", "quota", "get", $this->get_scope()))); $quotas = $remote->execute(implode(quota::$SPLIT_DELIMITER, array("+", "quota", "get", $this->get_scope())));
$remote->disconnect(); $remote->disconnect();
$dirs = explode(":", $quotas); $dirs = explode(":", $quotas);
@ -883,7 +889,8 @@ class quota extends baseModule {
$quotaString = implode(quota::$SPLIT_DELIMITER, array($name, "quota", "set", $this->get_scope(), $dir . ',' . $quotaString = implode(quota::$SPLIT_DELIMITER, array($name, "quota", "set", $this->get_scope(), $dir . ',' .
implode(',', $temp['accounts'][$name][$mountPoints[$m]]) . "\n")); implode(',', $temp['accounts'][$name][$mountPoints[$m]]) . "\n"));
$remote = new \LAM\REMOTE\Remote(); $remote = new \LAM\REMOTE\Remote();
$remote->connect($server); $remoteServer = $_SESSION['config']->getScriptServerByName($server);
$remote->connect($remoteServer);
$result = $remote->execute($quotaString); $result = $remote->execute($quotaString);
$remote->disconnect(); $remote->disconnect();
if (!empty($result)) { if (!empty($result)) {

View File

@ -1545,10 +1545,9 @@ class sambaSamAccount extends baseModule implements passwordService {
$remoteServers = $_SESSION['config']->getConfiguredScriptServers(); $remoteServers = $_SESSION['config']->getConfiguredScriptServers();
for ($i = 0; $i < sizeof($remoteServers); $i++) { for ($i = 0; $i < sizeof($remoteServers); $i++) {
$remoteServer = $remoteServers[$i]; $remoteServer = $remoteServers[$i];
$server = $remoteServer->getServer();
$label = $remoteServer->getLabel(); $label = $remoteServer->getLabel();
$remote = new \LAM\REMOTE\Remote(); $remote = new \LAM\REMOTE\Remote();
$remote->connect($server); $remote->connect($remoteServer);
$result = $remote->execute( $result = $remote->execute(
implode( implode(
self::$SPLIT_DELIMITER, self::$SPLIT_DELIMITER,
@ -1617,10 +1616,9 @@ class sambaSamAccount extends baseModule implements passwordService {
$remoteServers = $_SESSION['config']->getConfiguredScriptServers(); $remoteServers = $_SESSION['config']->getConfiguredScriptServers();
for ($i = 0; $i < sizeof($remoteServers); $i++) { for ($i = 0; $i < sizeof($remoteServers); $i++) {
$remoteServer = $remoteServers[$i]; $remoteServer = $remoteServers[$i];
$server = $remoteServer->getServer();
if (isset($_POST['form_subpage_' . get_class($this) . '_homedir_create_' . $i])) { if (isset($_POST['form_subpage_' . get_class($this) . '_homedir_create_' . $i])) {
$remote = new \LAM\REMOTE\Remote(); $remote = new \LAM\REMOTE\Remote();
$remote->connect($server); $remote->connect($remoteServer);
$result = $remote->execute( $result = $remote->execute(
implode( implode(
self::$SPLIT_DELIMITER, self::$SPLIT_DELIMITER,
@ -1645,7 +1643,7 @@ class sambaSamAccount extends baseModule implements passwordService {
} }
elseif (isset($_POST['form_subpage_' . get_class($this) . '_homedir_delete_' . $i])) { elseif (isset($_POST['form_subpage_' . get_class($this) . '_homedir_delete_' . $i])) {
$remote = new \LAM\REMOTE\Remote(); $remote = new \LAM\REMOTE\Remote();
$remote->connect($server); $remote->connect($remoteServer);
$result = $remote->execute( $result = $remote->execute(
implode( implode(
self::$SPLIT_DELIMITER, self::$SPLIT_DELIMITER,

View File

@ -65,16 +65,20 @@ class Remote {
/** /**
* Connects to the given SSH server. * Connects to the given SSH server.
* *
* @param String $server server name (e.g. localhost or localhost,1234) * @param RemoteServerConfiguration $server server (e.g. localhost or localhost,1234)
*/ */
public function connect($server) { public function connect($server) {
$serverNameParts = explode(",", $server); if ($server === null) {
logNewMessage(LOG_ERR, 'No server provided for lamdaemon');
throw new LAMException(_("Unable to connect to remote server!"));
}
$serverNameParts = explode(",", $server->getServer());
$handle = false; $handle = false;
if (sizeof($serverNameParts) > 1) { if (sizeof($serverNameParts) > 1) {
$handle = @new SSH2($serverNameParts[0], $serverNameParts[1]); $handle = @new SSH2($serverNameParts[0], $serverNameParts[1]);
} }
else { else {
$handle = @new SSH2($server); $handle = @new SSH2($serverNameParts[0]);
} }
if (!$handle) { if (!$handle) {
throw new LAMException(_("Unable to connect to remote server!")); throw new LAMException(_("Unable to connect to remote server!"));

View File

@ -178,6 +178,7 @@ function testRemoteCommand($command, $stopTest, $remote, $testText, $container)
* @param htmlResponsiveRow $container container for HTML output * @param htmlResponsiveRow $container container for HTML output
*/ */
function lamRunTestSuite($serverName, $serverTitle, $testQuota, $container) { function lamRunTestSuite($serverName, $serverTitle, $testQuota, $container) {
$remoteServer = $_SESSION['config']->getScriptServerByName($serverName);
$SPLIT_DELIMITER = "###x##y##x###"; $SPLIT_DELIMITER = "###x##y##x###";
$LAMDAEMON_PROTOCOL_VERSION = '5'; $LAMDAEMON_PROTOCOL_VERSION = '5';
$okImage = "../../graphics/pass.png"; $okImage = "../../graphics/pass.png";
@ -246,7 +247,7 @@ function lamRunTestSuite($serverName, $serverTitle, $testQuota, $container) {
$container->add(new htmlOutputText(_("SSH connection")), 10, 4); $container->add(new htmlOutputText(_("SSH connection")), 10, 4);
flush(); flush();
try { try {
$remote->connect($serverName); $remote->connect($remoteServer);
$container->add(new htmlImage($okImage), 2); $container->add(new htmlImage($okImage), 2);
$container->add(new htmlOutputText(_("SSH connection established.")), 12, 6); $container->add(new htmlOutputText(_("SSH connection established.")), 12, 6);
} }