LDAPAccountManager/lam/templates/tests/lamdaemonTest.php

316 lines
11 KiB
PHP
Raw Permalink Normal View History

2006-10-04 18:12:22 +00:00
<?php
/*
$Id$
2009-10-27 18:47:12 +00:00
This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/)
Copyright (C) 2006 - 2015 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.
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.
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
*/
/**
* Tests the lamdaemon script.
*
* @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 */
include_once("../../lib/security.inc");
/** access to configuration options */
include_once("../../lib/config.inc");
// start session
startSecureSession();
2007-12-30 13:15:39 +00:00
// die if no write access
if (!checkIfWriteAccessIsAllowed()) die();
2012-07-22 10:37:01 +00:00
checkIfToolIsActive('toolTests');
2006-10-04 18:12:22 +00:00
setlanguage();
2010-01-01 17:21:46 +00:00
include '../main_header.php';
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
2010-10-17 13:38:32 +00:00
$container = new htmlTable();
$container->addElement(new htmlTitle(_("Lamdaemon test")), true);
2006-10-04 18:12:22 +00:00
2008-01-27 16:12:06 +00:00
$servers = explode(";", $_SESSION['config']->get_scriptServers());
$serverIDs = array();
$serverTitles = array();
for ($i = 0; $i < sizeof($servers); $i++) {
$serverParts = explode(":", $servers[$i]);
$serverName = $serverParts[0];
$title = $serverName;
if (isset($serverParts[1])) {
$title = $serverParts[1] . " (" . $serverName . ")";
}
$serverIDs[] = $serverName;
$serverTitles[$serverName] = $title;
}
if (isset($_POST['runTest'])) {
2010-10-17 13:38:32 +00:00
lamRunLamdaemonTestSuite($_POST['server'], $serverTitles[$_POST['server']] , isset($_POST['checkQuotas']), $container);
2008-01-27 16:12:06 +00:00
}
else if ((sizeof($servers) > 0) && isset($servers[0]) && ($servers[0] != '')) {
2010-10-17 13:38:32 +00:00
$container->addElement(new htmlOutputText(_("Server")));
$serverOptions = array();
for ($i = 0; $i < sizeof($servers); $i++) {
$servers[$i] = explode(":", $servers[$i]);
$serverName = $servers[$i][0];
$title = $serverName;
if (isset($servers[$i][1])) {
$title = $servers[$i][1] . " (" . $serverName . ")";
}
$serverOptions[$title] = $serverName;
}
$serverSelect = new htmlSelect('server', $serverOptions);
$serverSelect->setHasDescriptiveElements(true);
$container->addElement($serverSelect, true);
$container->addElement(new htmlOutputText(_("Check quotas")));
$container->addElement(new htmlInputCheckbox('checkQuotas', false), true);
2008-01-27 16:12:06 +00:00
2010-10-17 13:38:32 +00:00
$container->addElement(new htmlSpacer(null, '10px'), true);
2008-01-27 16:12:06 +00:00
2010-10-17 13:38:32 +00:00
$okButton = new htmlButton('runTest', _("Ok"));
$okButton->colspan = 2;
$container->addElement($okButton);
2008-01-27 16:12:06 +00:00
}
else {
2010-10-17 13:38:32 +00:00
$container->addElement(new htmlStatusMessage("ERROR", _('No lamdaemon server set, please update your LAM configuration settings.')));
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";
2010-08-21 09:43:52 +00:00
include '../main_footer.php';
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
* @param connection $handle SSH connection
* @param string $testText describing text
2010-10-17 13:38:32 +00:00
* @param htmlTable $container container for HTML output
2006-11-05 12:16:07 +00:00
* @return boolean true, if errors occured
*/
2010-10-17 13:38:32 +00:00
function lamTestLamdaemon($command, $stopTest, $handle, $testText, $container) {
$okImage = "../../graphics/pass.png";
$failImage = "../../graphics/fail.png";
$spacer = new htmlSpacer('10px', null);
2006-11-05 12:16:07 +00:00
// run lamdaemon and get user quotas
if (!$stopTest) {
2010-10-17 13:38:32 +00:00
$container->addElement(new htmlOutputText($testText));
$container->addElement($spacer);
2006-11-05 12:16:07 +00:00
flush();
$lamdaemonOk = false;
2010-05-23 14:08:46 +00:00
$output = $handle->exec("sudo " . $_SESSION['config']->get_scriptPath() . ' ' . escapeshellarg($command));
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) {
2010-10-17 13:38:32 +00:00
$container->addElement(new htmlImage($okImage));
$container->addElement($spacer);
$container->addElement(new htmlOutputText(_("Lamdaemon successfully run.")), true);
2006-11-05 12:16:07 +00:00
}
else {
2010-10-17 13:38:32 +00:00
$container->addElement(new htmlImage($failImage));
$container->addElement($spacer);
2010-05-23 14:08:46 +00:00
if (!(strpos($output, 'ERROR,') === 0) && !(strpos($output, 'WARN,') === 0)) {
// error messages from console (e.g. sudo)
2010-10-17 13:38:32 +00:00
$container->addElement(new htmlStatusMessage('ERROR', $output), true);
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) {
$container->addElement(new htmlStatusMessage($parts[0], $parts[1]), true);
}
elseif (sizeof($parts) == 3) {
$container->addElement(new htmlStatusMessage($parts[0], $parts[1], $parts[2]), true);
}
else {
$container->addElement(new htmlOutputText($output), true);
}
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
}
2006-11-05 12:16:07 +00:00
flush();
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
2010-10-17 13:38:32 +00:00
* @param htmlTable $container container for HTML output
2008-01-27 16:12:06 +00:00
*/
2010-10-17 13:38:32 +00:00
function lamRunLamdaemonTestSuite($serverName, $serverTitle, $testQuota, $container) {
$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";
2008-01-27 16:12:06 +00:00
2007-02-22 17:38:22 +00:00
flush();
$stopTest = false;
2010-10-17 13:38:32 +00:00
$spacer = new htmlSpacer('10px', null);
2007-02-22 17:38:22 +00:00
2010-10-17 13:38:32 +00:00
$container->addElement(new htmlSubTitle($serverTitle), true);
2007-02-22 17:38:22 +00:00
// check script server and path
2010-10-17 13:38:32 +00:00
$container->addElement(new htmlOutputText(_("Lamdaemon server and path")));
$container->addElement(new htmlSpacer('10px', null));
2007-02-22 17:38:22 +00:00
if (!isset($serverName) || (strlen($serverName) < 3)) {
2010-10-17 13:38:32 +00:00
$container->addElement(new htmlImage($failImage));
$container->addElement($spacer);
$container->addElement(new htmlOutputText(_("No lamdaemon server set, please update your LAM configuration settings.")), true);
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)) {
2010-10-17 13:38:32 +00:00
$container->addElement(new htmlImage($failImage));
$container->addElement($spacer);
$container->addElement(new htmlOutputText(_("No lamdaemon path set, please update your LAM configuration settings.")), true);
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') {
$container->addElement(new htmlImage($failImage));
$container->addElement($spacer);
$container->addElement(new htmlOutputText(_("Lamdaemon path does not end with \".pl\". Did you enter the full path to the script?")), true);
$stopTest = true;
}
2007-02-22 17:38:22 +00:00
else {
2010-10-17 13:38:32 +00:00
$container->addElement(new htmlImage($okImage));
$container->addElement($spacer);
$container->addElement(new htmlOutputText(sprintf(_("Using %s as lamdaemon remote server."), $serverName)), true);
2007-02-22 17:38:22 +00:00
}
flush();
// check Unix account of LAM admin
if (!$stopTest) {
2010-10-17 13:38:32 +00:00
$container->addElement(new htmlOutputText(_("Unix account")));
$container->addElement($spacer);
2007-02-22 17:38:22 +00:00
$credentials = $_SESSION['ldap']->decrypt_login();
$unixOk = false;
2014-12-11 18:22:35 +00:00
$sr = @ldap_read($_SESSION['ldap']->server(), $credentials[0], "objectClass=posixAccount", array('uid'), 0, 0, 0, LDAP_DEREF_NEVER);
2007-02-22 17:38:22 +00:00
if ($sr) {
$entry = @ldap_get_entries($_SESSION['ldap']->server(), $sr);
$userName = $entry[0]['uid'][0];
if ($userName) {
$unixOk = true;
}
}
if ($unixOk) {
2010-10-17 13:38:32 +00:00
$container->addElement(new htmlImage($okImage));
$container->addElement($spacer);
$container->addElement(new htmlOutputText(sprintf(_("Using %s to connect to remote server."), $userName)), true);
2007-02-22 17:38:22 +00:00
}
else {
2010-10-17 13:38:32 +00:00
$container->addElement(new htmlImage($failImage));
$container->addElement($spacer);
$container->addElement(new htmlOutputText(sprintf(_("Your LAM admin user (%s) must be a valid Unix account to work with lamdaemon!"), $credentials[0])), true);
2007-02-22 17:38:22 +00:00
$stopTest = true;
}
}
flush();
// check SSH login
if (!$stopTest) {
2010-10-17 13:38:32 +00:00
$container->addElement(new htmlOutputText(_("SSH connection")));
$container->addElement($spacer);
2007-02-22 17:38:22 +00:00
flush();
$sshOk = false;
$handle = lamTestConnectSSH($serverName);
2007-02-22 17:38:22 +00:00
if ($handle) {
2010-05-23 14:08:46 +00:00
if ($handle->login($userName, $credentials[1])) {
2007-02-22 17:38:22 +00:00
$sshOk = true;
}
}
if ($sshOk) {
2010-10-17 13:38:32 +00:00
$container->addElement(new htmlImage($okImage));
$container->addElement($spacer);
$container->addElement(new htmlOutputText(_("SSH connection could be established.")), true);
2007-02-22 17:38:22 +00:00
}
else {
2010-10-17 13:38:32 +00:00
$container->addElement(new htmlImage($failImage));
$container->addElement($spacer);
$container->addElement(new htmlOutputText(_("Unable to connect to remote server!")), true);
2007-02-22 17:38:22 +00:00
$stopTest = true;
}
}
flush();
2008-12-18 14:44:42 +00:00
2010-10-17 13:38:32 +00:00
if (!$stopTest) {
$stopTest = lamTestLamdaemon("+" . $SPLIT_DELIMITER . "test" . $SPLIT_DELIMITER . "basic", $stopTest, $handle, _("Execute lamdaemon"), $container);
}
if (!$stopTest) {
$stopTest = lamTestLamdaemon("+" . $SPLIT_DELIMITER . "test" . $SPLIT_DELIMITER . "version" . $SPLIT_DELIMITER . $LAMDAEMON_PROTOCOL_VERSION, $stopTest, $handle, _("Lamdaemon version"), $container);
}
2010-10-17 13:38:32 +00:00
if (!$stopTest) {
$handle = lamTestConnectSSH($serverName);
2010-05-23 14:08:46 +00:00
@$handle->login($userName, $credentials[1]);
2010-10-17 13:38:32 +00:00
$stopTest = lamTestLamdaemon("+" . $SPLIT_DELIMITER . "test" . $SPLIT_DELIMITER . "nss" . $SPLIT_DELIMITER . "$userName", $stopTest, $handle, _("Lamdaemon: check NSS LDAP"), $container);
if (!$stopTest && $testQuota) {
$handle = lamTestConnectSSH($serverName);
@$handle->login($userName, $credentials[1]);
$stopTest = lamTestLamdaemon("+" . $SPLIT_DELIMITER . "test" . $SPLIT_DELIMITER . "quota", $stopTest, $handle, _("Lamdaemon: Quota module installed"), $container);
$handle = lamTestConnectSSH($serverName);
@$handle->login($userName, $credentials[1]);
$stopTest = lamTestLamdaemon("+" . $SPLIT_DELIMITER . "quota" . $SPLIT_DELIMITER . "get" . $SPLIT_DELIMITER . "user", $stopTest, $handle, _("Lamdaemon: read quotas"), $container);
}
2008-01-27 16:12:06 +00:00
}
2007-02-22 17:38:22 +00:00
2010-10-17 13:38:32 +00:00
$container->addElement(new htmlSpacer(null, '10px'), true);
$endMessage = new htmlOutputText(_("Lamdaemon test finished."));
$endMessage->colspan = 5;
$container->addElement($endMessage);
2007-02-22 17:38:22 +00:00
}
2006-10-04 18:12:22 +00:00
/**
* Connects to the given SSH server.
*
* @param String $server server name (e.g. localhost or localhost,1234)
* @return object handle
*/
function lamTestConnectSSH($server) {
2010-05-23 14:08:46 +00:00
// add phpseclib to include path
set_include_path(get_include_path() . PATH_SEPARATOR . dirname(__FILE__) . '/../../lib/3rdParty/phpseclib');
include_once('Net/SSH2.php');
$serverNameParts = explode(",", $server);
if (sizeof($serverNameParts) > 1) {
2010-05-23 14:08:46 +00:00
return @new Net_SSH2($serverNameParts[0], $serverNameParts[1]);
}
else {
2010-05-23 14:08:46 +00:00
return @new Net_SSH2($server);
}
}
2006-10-04 18:12:22 +00:00
?>