LDAPAccountManager/lam/templates/tests/lamdaemonTest.php

286 lines
9.6 KiB
PHP
Raw Normal View History

2006-10-04 18:12:22 +00:00
<?php
2017-09-16 13:09:25 +00:00
namespace LAM\TOOLS\TESTS;
use \LAM\REMOTE\Remote;
use \htmlTitle;
use \htmlOutputText;
2018-01-03 17:40:51 +00:00
use \htmlResponsiveSelect;
use \htmlResponsiveInputCheckbox;
2017-09-16 13:09:25 +00:00
use \htmlButton;
use \htmlStatusMessage;
use \htmlImage;
use \htmlSubTitle;
use \Exception;
2018-01-03 17:40:51 +00:00
use \htmlResponsiveRow;
2017-09-16 13:09:25 +00:00
2006-10-04 18:12:22 +00:00
/*
2009-10-27 18:47:12 +00:00
This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/)
2020-03-02 15:44:15 +00:00
Copyright (C) 2006 - 2020 Roland Gruber
2006-10-04 18:12:22 +00:00
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
2006-10-04 18:12:22 +00:00
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
2006-10-04 18:12:22 +00:00
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/**
2017-09-16 13:09:25 +00:00
* Tests the remote script.
2006-10-04 18:12:22 +00:00
*
* @author Roland Gruber
2007-02-22 17:38:22 +00:00
* @author Thomas Manninger
2006-10-04 18:12:22 +00:00
* @package tools
*/
/** security functions */
2018-12-22 20:33:07 +00:00
include_once(__DIR__ . "/../../lib/security.inc");
2006-10-04 18:12:22 +00:00
/** access to configuration options */
2018-12-22 20:33:07 +00:00
include_once(__DIR__ . "/../../lib/config.inc");
2006-10-04 18:12:22 +00:00
// start session
startSecureSession();
2017-02-11 16:11:37 +00:00
enforceUserIsLoggedIn();
2006-10-04 18:12:22 +00:00
2007-12-30 13:15:39 +00:00
// die if no write access
2019-10-13 18:12:55 +00:00
if (!checkIfWriteAccessIsAllowed()) {
die();
}
2007-12-30 13:15:39 +00:00
2012-07-22 10:37:01 +00:00
checkIfToolIsActive('toolTests');
2006-10-04 18:12:22 +00:00
setlanguage();
2018-01-03 17:40:51 +00:00
include '../../lib/adminHeader.inc';
2013-01-19 13:18:52 +00:00
echo "<div class=\"user-bright smallPaddingContent\">\n";
2010-10-17 13:38:32 +00:00
echo "<form action=\"lamdaemonTest.php\" method=\"post\">\n";
2006-10-04 18:12:22 +00:00
2018-01-03 17:40:51 +00:00
$container = new htmlResponsiveRow();
$container->add(new htmlTitle(_("Lamdaemon test")), 12);
2006-10-04 18:12:22 +00:00
2019-10-13 18:12:55 +00:00
$servers = $_SESSION['config']->getConfiguredScriptServers();
2008-01-27 16:12:06 +00:00
$serverIDs = array();
$serverTitles = array();
2019-10-13 18:12:55 +00:00
foreach ($servers as $server) {
$serverName = $server->getServer();
$label = $server->getLabel();
if ($label !== $serverName) {
$label = $label . " (" . $serverName . ")";
2008-01-27 16:12:06 +00:00
}
$serverIDs[] = $serverName;
2019-10-13 18:12:55 +00:00
$serverTitles[$serverName] = $label;
2008-01-27 16:12:06 +00:00
}
if (isset($_POST['runTest'])) {
2017-09-16 13:09:25 +00:00
lamRunTestSuite($_POST['server'], $serverTitles[$_POST['server']] , isset($_POST['checkQuotas']), $container);
2008-01-27 16:12:06 +00:00
}
2019-10-13 18:12:55 +00:00
elseif (!empty($servers)) {
2010-10-17 13:38:32 +00:00
$serverOptions = array();
2019-10-13 18:12:55 +00:00
foreach ($servers as $server) {
$serverName = $server->getServer();
$label = $server->getLabel();
if ($label !== $serverName) {
$label = $label . " (" . $serverName . ")";
2010-10-17 13:38:32 +00:00
}
2019-10-13 18:12:55 +00:00
$serverOptions[$label] = $serverName;
2010-10-17 13:38:32 +00:00
}
2018-01-03 17:40:51 +00:00
$serverSelect = new htmlResponsiveSelect('server', $serverOptions, array(), _("Server"));
2010-10-17 13:38:32 +00:00
$serverSelect->setHasDescriptiveElements(true);
2018-01-03 17:40:51 +00:00
$container->add($serverSelect, 12);
2018-01-03 17:40:51 +00:00
$container->add(new htmlResponsiveInputCheckbox('checkQuotas', false, _("Check quotas")), 12);
2018-01-03 17:40:51 +00:00
$container->addVerticalSpacer('1rem');
2010-10-17 13:38:32 +00:00
$okButton = new htmlButton('runTest', _("Ok"));
$okButton->colspan = 2;
2018-01-03 17:40:51 +00:00
$container->addLabel($okButton);
$container->addField(new htmlOutputText('&nbsp;', false));
2008-01-27 16:12:06 +00:00
}
else {
2018-01-03 17:40:51 +00:00
$container->add(new htmlStatusMessage("ERROR", _('No lamdaemon server set, please update your LAM configuration settings.')), 12);
2008-01-27 16:12:06 +00:00
}
2010-10-17 13:38:32 +00:00
$tabindex = 1;
parseHtml(null, $container, array(), false, $tabindex, 'user');
2008-01-27 16:12:06 +00:00
2010-10-17 13:38:32 +00:00
echo "</form>\n";
echo "</div>\n";
2018-01-03 17:40:51 +00:00
include '../../lib/adminFooter.inc';
2008-01-27 16:12:06 +00:00
2006-11-05 12:16:07 +00:00
/**
* Runs a test case of lamdaemon.
*
* @param string $command test command
* @param boolean $stopTest specifies if test should be run
2017-09-16 13:09:25 +00:00
* @param Remote $remote SSH connection
2006-11-05 12:16:07 +00:00
* @param string $testText describing text
2018-01-03 17:40:51 +00:00
* @param htmlResponsiveRow $container container for HTML output
2020-03-02 15:44:15 +00:00
* @return boolean true, if errors occurred
2006-11-05 12:16:07 +00:00
*/
2017-09-17 07:21:37 +00:00
function testRemoteCommand($command, $stopTest, $remote, $testText, $container) {
2010-10-17 13:38:32 +00:00
$okImage = "../../graphics/pass.png";
$failImage = "../../graphics/fail.png";
2017-09-16 13:09:25 +00:00
// run remote command
2006-11-05 12:16:07 +00:00
if (!$stopTest) {
2018-01-03 17:40:51 +00:00
$container->add(new htmlOutputText($testText), 10, 4);
2006-11-05 12:16:07 +00:00
flush();
$lamdaemonOk = false;
2017-09-16 13:09:25 +00:00
$output = $remote->execute($command);
2010-05-23 14:08:46 +00:00
if ((stripos(strtolower($output), "error") === false) && ((strpos($output, 'INFO,') === 0) || (strpos($output, 'QUOTA_ENTRY') === 0))) {
2006-11-05 12:16:07 +00:00
$lamdaemonOk = true;
2006-10-04 18:12:22 +00:00
}
2006-11-05 12:16:07 +00:00
if ($lamdaemonOk) {
2018-01-03 17:40:51 +00:00
$container->add(new htmlImage($okImage), 2);
$container->add(new htmlOutputText(_("Lamdaemon successfully run.")), 12, 6);
2006-11-05 12:16:07 +00:00
}
else {
2018-01-03 17:40:51 +00:00
$container->add(new htmlImage($failImage), 2);
2010-05-23 14:08:46 +00:00
if (!(strpos($output, 'ERROR,') === 0) && !(strpos($output, 'WARN,') === 0)) {
// error messages from console (e.g. sudo)
2018-01-03 17:40:51 +00:00
$container->add(new htmlStatusMessage('ERROR', $output), 12, 6);
2010-05-23 14:08:46 +00:00
}
else {
// error messages from lamdaemon
2010-10-17 13:38:32 +00:00
$parts = explode(",", $output);
if (sizeof($parts) == 2) {
2018-01-03 17:40:51 +00:00
$container->add(new htmlStatusMessage($parts[0], $parts[1]), 12, 6);
2010-10-17 13:38:32 +00:00
}
elseif (sizeof($parts) == 3) {
2018-01-03 17:40:51 +00:00
$container->add(new htmlStatusMessage($parts[0], $parts[1], $parts[2]), 12, 6);
2010-10-17 13:38:32 +00:00
}
else {
2018-01-03 17:40:51 +00:00
$container->add(new htmlOutputText($output), 12, 6);
2010-10-17 13:38:32 +00:00
}
2006-11-05 12:16:07 +00:00
}
2007-02-22 17:38:22 +00:00
$stopTest = true;
2006-11-05 12:16:07 +00:00
}
2006-10-04 18:12:22 +00:00
}
2018-01-03 17:40:51 +00:00
$container->addVerticalSpacer('0.5rem');
2006-11-05 12:16:07 +00:00
return $stopTest;
2006-10-04 18:12:22 +00:00
}
2008-01-27 16:12:06 +00:00
/**
* Runs all tests for a given server.
*
* @param String $serverName server ID
* @param String $serverTitle server name
* @param boolean $testQuota true, if Quotas should be checked
2018-01-03 17:40:51 +00:00
* @param htmlResponsiveRow $container container for HTML output
2008-01-27 16:12:06 +00:00
*/
2017-09-16 13:09:25 +00:00
function lamRunTestSuite($serverName, $serverTitle, $testQuota, $container) {
2019-10-23 19:14:54 +00:00
$remoteServer = $_SESSION['config']->getScriptServerByName($serverName);
$SPLIT_DELIMITER = "###x##y##x###";
$LAMDAEMON_PROTOCOL_VERSION = '5';
2010-10-17 13:38:32 +00:00
$okImage = "../../graphics/pass.png";
$failImage = "../../graphics/fail.png";
2007-02-22 17:38:22 +00:00
$stopTest = false;
2018-01-03 17:40:51 +00:00
$container->add(new htmlSubTitle($serverTitle), 12);
2007-02-22 17:38:22 +00:00
// check script server and path
2018-01-03 17:40:51 +00:00
$container->add(new htmlOutputText(_("Lamdaemon server and path")), 10, 4);
2007-02-22 17:38:22 +00:00
if (!isset($serverName) || (strlen($serverName) < 3)) {
2018-01-03 17:40:51 +00:00
$container->add(new htmlImage($failImage), 2);
$container->add(new htmlOutputText(_("No lamdaemon server set, please update your LAM configuration settings.")), 12, 6);
2007-02-22 17:38:22 +00:00
}
2007-12-29 18:59:09 +00:00
elseif (($_SESSION['config']->get_scriptPath() == null) || (strlen($_SESSION['config']->get_scriptPath()) < 10)) {
2018-01-03 17:40:51 +00:00
$container->add(new htmlImage($failImage), 2);
$container->add(new htmlOutputText(_("No lamdaemon path set, please update your LAM configuration settings.")), 12, 6);
2007-02-22 17:38:22 +00:00
$stopTest = true;
}
2012-09-29 10:43:13 +00:00
elseif (substr($_SESSION['config']->get_scriptPath(), -3) != '.pl') {
2018-01-03 17:40:51 +00:00
$container->add(new htmlImage($failImage), 2);
$container->add(new htmlOutputText(_("Lamdaemon path does not end with \".pl\". Did you enter the full path to the script?")), 12, 6);
2012-09-29 10:43:13 +00:00
$stopTest = true;
}
2007-02-22 17:38:22 +00:00
else {
2018-01-03 17:40:51 +00:00
$container->add(new htmlImage($okImage), 2);
$container->add(new htmlOutputText(sprintf(_("Using %s as lamdaemon remote server."), $serverName)), 12, 6);
2007-02-22 17:38:22 +00:00
}
2018-01-03 17:40:51 +00:00
$container->addVerticalSpacer('0.5rem');
2007-02-22 17:38:22 +00:00
// check Unix account of LAM admin
2019-08-05 19:56:06 +00:00
$ldapUser = $_SESSION['ldap']->getUserName();
2007-02-22 17:38:22 +00:00
if (!$stopTest) {
2016-12-20 21:12:31 +00:00
$scriptUserName = $_SESSION['config']->getScriptUserName();
if (empty($scriptUserName)) {
2018-01-03 17:40:51 +00:00
$container->add(new htmlOutputText(_("Unix account")), 10, 4);
$unixOk = false;
2019-08-05 19:56:06 +00:00
$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);
$userName = $entry[0]['uid'][0];
if ($userName) {
$unixOk = true;
}
}
if ($unixOk) {
2018-01-03 17:40:51 +00:00
$container->add(new htmlImage($okImage), 2);
$container->add(new htmlOutputText(sprintf(_("Using %s to connect to remote server."), $userName)), 12, 6);
}
else {
2018-01-03 17:40:51 +00:00
$container->add(new htmlImage($failImage), 2);
2019-08-05 19:56:06 +00:00
$container->add(new htmlOutputText(sprintf(_("Your LAM admin user (%s) must be a valid Unix account to work with lamdaemon!"), $ldapUser)), 12, 6);
$stopTest = true;
}
2018-01-03 17:40:51 +00:00
$container->addVerticalSpacer('0.5rem');
2007-02-22 17:38:22 +00:00
}
else {
$userName = $_SESSION['config']->getScriptUserName();
2007-02-22 17:38:22 +00:00
}
}
// check SSH login
2017-09-16 13:09:25 +00:00
$remote = new Remote();
2007-02-22 17:38:22 +00:00
if (!$stopTest) {
2018-01-03 17:40:51 +00:00
$container->add(new htmlOutputText(_("SSH connection")), 10, 4);
2007-02-22 17:38:22 +00:00
flush();
try {
2019-10-23 19:14:54 +00:00
$remote->connect($remoteServer);
2018-01-03 17:40:51 +00:00
$container->add(new htmlImage($okImage), 2);
$container->add(new htmlOutputText(_("SSH connection established.")), 12, 6);
2007-02-22 17:38:22 +00:00
}
catch (Exception $e) {
2018-01-03 17:40:51 +00:00
$container->add(new htmlImage($failImage), 2);
$container->add(new htmlOutputText($e->getMessage()), 12, 6);
2007-02-22 17:38:22 +00:00
$stopTest = true;
}
}
2018-01-03 17:40:51 +00:00
$container->addVerticalSpacer('0.5rem');
2010-10-17 13:38:32 +00:00
if (!$stopTest) {
2017-09-17 07:21:37 +00:00
$stopTest = testRemoteCommand("+" . $SPLIT_DELIMITER . "test" . $SPLIT_DELIMITER . "basic", $stopTest, $remote, _("Execute lamdaemon"), $container);
2010-10-17 13:38:32 +00:00
}
if (!$stopTest) {
2017-09-17 07:21:37 +00:00
$stopTest = testRemoteCommand("+" . $SPLIT_DELIMITER . "test" . $SPLIT_DELIMITER . "version" . $SPLIT_DELIMITER . $LAMDAEMON_PROTOCOL_VERSION, $stopTest, $remote, _("Lamdaemon version"), $container);
}
2010-10-17 13:38:32 +00:00
if (!$stopTest) {
2017-09-17 07:21:37 +00:00
$stopTest = testRemoteCommand("+" . $SPLIT_DELIMITER . "test" . $SPLIT_DELIMITER . "nss" . $SPLIT_DELIMITER . "$userName", $stopTest, $remote, _("Lamdaemon: check NSS LDAP"), $container);
2010-10-17 13:38:32 +00:00
if (!$stopTest && $testQuota) {
2017-09-17 07:21:37 +00:00
$stopTest = testRemoteCommand("+" . $SPLIT_DELIMITER . "test" . $SPLIT_DELIMITER . "quota", $stopTest, $remote, _("Lamdaemon: Quota module installed"), $container);
$stopTest = testRemoteCommand("+" . $SPLIT_DELIMITER . "quota" . $SPLIT_DELIMITER . "get" . $SPLIT_DELIMITER . "user", $stopTest, $remote, _("Lamdaemon: read quotas"), $container);
2010-10-17 13:38:32 +00:00
}
2008-01-27 16:12:06 +00:00
}
2017-09-16 14:55:21 +00:00
$remote->disconnect();
2007-02-22 17:38:22 +00:00
2018-01-03 17:40:51 +00:00
$container->addVerticalSpacer('1rem');
2010-10-17 13:38:32 +00:00
$endMessage = new htmlOutputText(_("Lamdaemon test finished."));
$endMessage->colspan = 5;
2018-01-03 17:40:51 +00:00
$container->add($endMessage, 12);
2007-02-22 17:38:22 +00:00
}
2006-10-04 18:12:22 +00:00
?>