| 1 |
<?php |
| 2 |
namespace LAM\TOOLS\TESTS; |
| 3 |
use \LAM\REMOTE\Remote; |
| 4 |
use \htmlTitle; |
| 5 |
use \htmlOutputText; |
| 6 |
use \htmlResponsiveSelect; |
| 7 |
use \htmlResponsiveInputCheckbox; |
| 8 |
use \htmlButton; |
| 9 |
use \htmlStatusMessage; |
| 10 |
use \htmlImage; |
| 11 |
use \htmlSubTitle; |
| 12 |
use \Exception; |
| 13 |
use \htmlResponsiveRow; |
| 14 |
use LAMException; |
| 15 |
|
| 16 |
/* |
| 17 |
|
| 18 |
This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/) |
| 19 |
Copyright (C) 2006 - 2023 Roland Gruber |
| 20 |
|
| 21 |
This program is free software; you can redistribute it and/or modify |
| 22 |
it under the terms of the GNU General Public License as published by |
| 23 |
the Free Software Foundation; either version 2 of the License, or |
| 24 |
(at your option) any later version. |
| 25 |
|
| 26 |
This program is distributed in the hope that it will be useful, |
| 27 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 28 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 29 |
GNU General Public License for more details. |
| 30 |
|
| 31 |
You should have received a copy of the GNU General Public License |
| 32 |
along with this program; if not, write to the Free Software |
| 33 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 34 |
|
| 35 |
*/ |
| 36 |
|
| 37 |
/** |
| 38 |
* Tests the remote script. |
| 39 |
* |
| 40 |
* @author Roland Gruber |
| 41 |
* @author Thomas Manninger |
| 42 |
* @package tools |
| 43 |
*/ |
| 44 |
|
| 45 |
/** security functions */ |
| 46 |
include_once(__DIR__ . "/../../lib/security.inc"); |
| 47 |
/** access to configuration options */ |
| 48 |
include_once(__DIR__ . "/../../lib/config.inc"); |
| 49 |
|
| 50 |
// start session |
| 51 |
startSecureSession(); |
| 52 |
enforceUserIsLoggedIn(); |
| 53 |
|
| 54 |
// die if no write access |
| 55 |
if (!checkIfWriteAccessIsAllowed()) { |
| 56 |
die(); |
| 57 |
} |
| 58 |
|
| 59 |
checkIfToolIsActive('toolTests'); |
| 60 |
|
| 61 |
setlanguage(); |
| 62 |
|
| 63 |
include __DIR__ . '/../../lib/adminHeader.inc'; |
| 64 |
echo "<div class=\"smallPaddingContent\">\n"; |
| 65 |
echo "<form action=\"lamdaemonTest.php\" method=\"post\">\n"; |
| 66 |
|
| 67 |
$container = new htmlResponsiveRow(); |
| 68 |
$container->add(new htmlTitle(_("Lamdaemon test")), 12); |
| 69 |
|
| 70 |
$servers = $_SESSION['config']->getConfiguredScriptServers(); |
| 71 |
$serverIDs = []; |
| 72 |
$serverTitles = []; |
| 73 |
foreach ($servers as $server) { |
| 74 |
$serverName = $server->getServer(); |
| 75 |
$label = $server->getLabel(); |
| 76 |
if ($label !== $serverName) { |
| 77 |
$label = $label . " (" . $serverName . ")"; |
| 78 |
} |
| 79 |
$serverIDs[] = $serverName; |
| 80 |
$serverTitles[$serverName] = $label; |
| 81 |
} |
| 82 |
|
| 83 |
if (isset($_POST['runTest'])) { |
| 84 |
lamRunTestSuite($_POST['server'], $serverTitles[$_POST['server']] , isset($_POST['checkQuotas']), $container); |
| 85 |
} |
| 86 |
elseif (!empty($servers)) { |
| 87 |
$serverOptions = []; |
| 88 |
foreach ($servers as $server) { |
| 89 |
$serverName = $server->getServer(); |
| 90 |
$label = $server->getLabel(); |
| 91 |
if ($label !== $serverName) { |
| 92 |
$label = $label . " (" . $serverName . ")"; |
| 93 |
} |
| 94 |
$serverOptions[$label] = $serverName; |
| 95 |
} |
| 96 |
$serverSelect = new htmlResponsiveSelect('server', $serverOptions, [], _("Server")); |
| 97 |
$serverSelect->setHasDescriptiveElements(true); |
| 98 |
$container->add($serverSelect, 12); |
| 99 |
|
| 100 |
$container->add(new htmlResponsiveInputCheckbox('checkQuotas', false, _("Check quotas")), 12); |
| 101 |
|
| 102 |
$container->addVerticalSpacer('1rem'); |
| 103 |
|
| 104 |
$okButton = new htmlButton('runTest', _("Ok")); |
| 105 |
$okButton->colspan = 2; |
| 106 |
$container->addLabel($okButton); |
| 107 |
$container->addField(new htmlOutputText(' ', false)); |
| 108 |
} |
| 109 |
else { |
| 110 |
$container->add(new htmlStatusMessage("ERROR", _('No lamdaemon server set, please update your LAM configuration settings.')), 12); |
| 111 |
} |
| 112 |
|
| 113 |
parseHtml(null, $container, [], false, 'user'); |
| 114 |
|
| 115 |
echo "</form>\n"; |
| 116 |
echo "</div>\n"; |
| 117 |
include __DIR__ . '/../../lib/adminFooter.inc'; |
| 118 |
|
| 119 |
|
| 120 |
/** |
| 121 |
* Runs a test case of lamdaemon. |
| 122 |
* |
| 123 |
* @param string $command test command |
| 124 |
* @param boolean $stopTest specifies if test should be run |
| 125 |
* @param Remote $remote SSH connection |
| 126 |
* @param string $testText describing text |
| 127 |
* @param htmlResponsiveRow $container container for HTML output |
| 128 |
* @return boolean true, if errors occurred |
| 129 |
*/ |
| 130 |
function testRemoteCommand($command, $stopTest, $remote, $testText, $container) { |
| 131 |
$okImage = "../../graphics/pass.svg"; |
| 132 |
$failImage = "../../graphics/del.svg"; |
| 133 |
// run remote command |
| 134 |
if (!$stopTest) { |
| 135 |
$container->add(new htmlOutputText($testText), 10, 4); |
| 136 |
flush(); |
| 137 |
$lamdaemonOk = false; |
| 138 |
$output = $remote->execute($command); |
| 139 |
if ((stripos(strtolower($output), "error") === false) && ((str_starts_with($output, 'INFO,')) || (str_starts_with($output, 'QUOTA_ENTRY')))) { |
| 140 |
$lamdaemonOk = true; |
| 141 |
} |
| 142 |
if ($lamdaemonOk) { |
| 143 |
$container->add(new htmlImage($okImage), 2); |
| 144 |
$container->add(new htmlOutputText(_("Lamdaemon successfully run.")), 12, 6); |
| 145 |
} |
| 146 |
else { |
| 147 |
$container->add(new htmlImage($failImage), 2); |
| 148 |
if (!(str_starts_with($output, 'ERROR,')) && !(str_starts_with($output, 'WARN,'))) { |
| 149 |
// error messages from console (e.g. sudo) |
| 150 |
$container->add(new htmlStatusMessage('ERROR', $output), 12, 6); |
| 151 |
} |
| 152 |
else { |
| 153 |
// error messages from lamdaemon |
| 154 |
$parts = explode(",", $output); |
| 155 |
if (count($parts) === 2) { |
| 156 |
$container->add(new htmlStatusMessage($parts[0], $parts[1]), 12, 6); |
| 157 |
} |
| 158 |
elseif (count($parts) === 3) { |
| 159 |
$container->add(new htmlStatusMessage($parts[0], $parts[1], $parts[2]), 12, 6); |
| 160 |
} |
| 161 |
else { |
| 162 |
$container->add(new htmlOutputText($output), 12, 6); |
| 163 |
} |
| 164 |
} |
| 165 |
$stopTest = true; |
| 166 |
} |
| 167 |
} |
| 168 |
$container->addVerticalSpacer('0.5rem'); |
| 169 |
return $stopTest; |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* Runs all tests for a given server. |
| 174 |
* |
| 175 |
* @param String $serverName server ID |
| 176 |
* @param String $serverTitle server name |
| 177 |
* @param boolean $testQuota true, if Quotas should be checked |
| 178 |
* @param htmlResponsiveRow $container container for HTML output |
| 179 |
*/ |
| 180 |
function lamRunTestSuite($serverName, $serverTitle, $testQuota, $container): void { |
| 181 |
$remoteServer = $_SESSION['config']->getScriptServerByName($serverName); |
| 182 |
$SPLIT_DELIMITER = "###x##y##x###"; |
| 183 |
$LAMDAEMON_PROTOCOL_VERSION = '5'; |
| 184 |
$okImage = "../../graphics/pass.svg"; |
| 185 |
$failImage = "../../graphics/del.svg"; |
| 186 |
|
| 187 |
$stopTest = false; |
| 188 |
|
| 189 |
$container->add(new htmlSubTitle($serverTitle), 12); |
| 190 |
|
| 191 |
// check script server and path |
| 192 |
$container->add(new htmlOutputText(_("Lamdaemon server and path")), 10, 4); |
| 193 |
if (strlen($serverName) < 3) { |
| 194 |
$container->add(new htmlImage($failImage), 2); |
| 195 |
$container->add(new htmlOutputText(_("No lamdaemon server set, please update your LAM configuration settings.")), 12, 6); |
| 196 |
} |
| 197 |
elseif (($_SESSION['config']->get_scriptPath() == null) || (strlen($_SESSION['config']->get_scriptPath()) < 10)) { |
| 198 |
$container->add(new htmlImage($failImage), 2); |
| 199 |
$container->add(new htmlOutputText(_("No lamdaemon path set, please update your LAM configuration settings.")), 12, 6); |
| 200 |
$stopTest = true; |
| 201 |
} |
| 202 |
elseif (!str_ends_with($_SESSION['config']->get_scriptPath(), '.pl')) { |
| 203 |
$container->add(new htmlImage($failImage), 2); |
| 204 |
$container->add(new htmlOutputText(_("Lamdaemon path does not end with \".pl\". Did you enter the full path to the script?")), 12, 6); |
| 205 |
$stopTest = true; |
| 206 |
} |
| 207 |
else { |
| 208 |
$container->add(new htmlImage($okImage), 2); |
| 209 |
$container->add(new htmlOutputText(sprintf(_("Using %s as lamdaemon remote server."), $serverName)), 12, 6); |
| 210 |
} |
| 211 |
$container->addVerticalSpacer('0.5rem'); |
| 212 |
|
| 213 |
// check Unix account of LAM admin |
| 214 |
$ldapUser = $_SESSION['ldap']->getUserName(); |
| 215 |
$userName = ''; |
| 216 |
if (!$stopTest) { |
| 217 |
$scriptUserName = $_SESSION['config']->getScriptUserName(); |
| 218 |
if (empty($scriptUserName)) { |
| 219 |
$container->add(new htmlOutputText(_("Unix account")), 10, 4); |
| 220 |
$unixOk = false; |
| 221 |
$sr = @ldap_read($_SESSION['ldap']->server(), $ldapUser, "objectClass=posixAccount", ['uid'], 0, 0, 0, LDAP_DEREF_NEVER); |
| 222 |
if ($sr) { |
| 223 |
$entry = @ldap_get_entries($_SESSION['ldap']->server(), $sr); |
| 224 |
if (($entry !== false) && isset($entry[0]['uid'][0])) { |
| 225 |
$userName = $entry[0]['uid'][0]; |
| 226 |
$unixOk = true; |
| 227 |
} |
| 228 |
} |
| 229 |
if ($unixOk) { |
| 230 |
$container->add(new htmlImage($okImage), 2); |
| 231 |
$container->add(new htmlOutputText(sprintf(_("Using %s to connect to remote server."), $userName)), 12, 6); |
| 232 |
} |
| 233 |
else { |
| 234 |
$container->add(new htmlImage($failImage), 2); |
| 235 |
$container->add(new htmlOutputText(sprintf(_("Your LAM admin user (%s) must be a valid Unix account to work with lamdaemon!"), $ldapUser)), 12, 6); |
| 236 |
$stopTest = true; |
| 237 |
} |
| 238 |
$container->addVerticalSpacer('0.5rem'); |
| 239 |
} |
| 240 |
else { |
| 241 |
$userName = $_SESSION['config']->getScriptUserName(); |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
// check SSH login |
| 246 |
$remote = new Remote(); |
| 247 |
if (!$stopTest) { |
| 248 |
$container->add(new htmlOutputText(_("SSH connection")), 10, 4); |
| 249 |
flush(); |
| 250 |
try { |
| 251 |
$remote->connect($remoteServer); |
| 252 |
$container->add(new htmlImage($okImage), 2); |
| 253 |
$container->add(new htmlOutputText(_("SSH connection established.")), 12, 6); |
| 254 |
} |
| 255 |
catch (LAMException $e) { |
| 256 |
$container->add(new htmlImage($failImage), 2); |
| 257 |
$container->add(new htmlOutputText($e->getTitle() . ' ' . $e->getMessage()), 12, 6); |
| 258 |
$stopTest = true; |
| 259 |
} |
| 260 |
catch (Exception $e) { |
| 261 |
$container->add(new htmlImage($failImage), 2); |
| 262 |
$container->add(new htmlOutputText($e->getMessage()), 12, 6); |
| 263 |
$stopTest = true; |
| 264 |
} |
| 265 |
} |
| 266 |
$container->addVerticalSpacer('0.5rem'); |
| 267 |
|
| 268 |
if (!$stopTest) { |
| 269 |
$stopTest = testRemoteCommand("+" . $SPLIT_DELIMITER . "test" . $SPLIT_DELIMITER . "basic", $stopTest, $remote, _("Execute lamdaemon"), $container); |
| 270 |
} |
| 271 |
|
| 272 |
if (!$stopTest) { |
| 273 |
$stopTest = testRemoteCommand("+" . $SPLIT_DELIMITER . "test" . $SPLIT_DELIMITER . "version" . $SPLIT_DELIMITER . $LAMDAEMON_PROTOCOL_VERSION, $stopTest, $remote, _("Lamdaemon version"), $container); |
| 274 |
} |
| 275 |
|
| 276 |
if (!$stopTest) { |
| 277 |
$stopTest = testRemoteCommand("+" . $SPLIT_DELIMITER . "test" . $SPLIT_DELIMITER . "nss" . $SPLIT_DELIMITER . "$userName", $stopTest, $remote, _("Lamdaemon: check NSS LDAP"), $container); |
| 278 |
if (!$stopTest && $testQuota) { |
| 279 |
$stopTest = testRemoteCommand("+" . $SPLIT_DELIMITER . "test" . $SPLIT_DELIMITER . "quota", $stopTest, $remote, _("Lamdaemon: Quota module installed"), $container); |
| 280 |
$stopTest = testRemoteCommand("+" . $SPLIT_DELIMITER . "quota" . $SPLIT_DELIMITER . "get" . $SPLIT_DELIMITER . "user", $stopTest, $remote, _("Lamdaemon: read quotas"), $container); |
| 281 |
} |
| 282 |
} |
| 283 |
$remote->disconnect(); |
| 284 |
|
| 285 |
$container->addVerticalSpacer('1rem'); |
| 286 |
$endMessage = new htmlOutputText(_("Lamdaemon test finished.")); |
| 287 |
$endMessage->colspan = 5; |
| 288 |
$container->add($endMessage, 12); |
| 289 |
} |