Debian Codegraph @

templates/tests/lamdaemonTest.php

raw symbols SCIP
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 - 2026 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_once __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")));
69
70 $servers = $_SESSION['config']->getConfiguredScriptServers();
71 $serverTitles = [];
72 foreach ($servers as $server) {
73 $serverName = $server->getServer();
74 $label = $server->getLabel();
75 if ($label !== $serverName) {
76 $label = $label . " (" . $serverName . ")";
77 }
78 $serverTitles[$serverName] = $label;
79 }
80
81 if (isset($_POST['runTest'])) {
82 lamRunTestSuite($_POST['server'], $serverTitles[$_POST['server']] , isset($_POST['checkQuotas']), $container);
83 }
84 elseif (!empty($servers)) {
85 $serverOptions = [];
86 foreach ($servers as $server) {
87 $serverName = $server->getServer();
88 $label = $server->getLabel();
89 if ($label !== $serverName) {
90 $label = $label . " (" . $serverName . ")";
91 }
92 $serverOptions[$label] = $serverName;
93 }
94 $serverSelect = new htmlResponsiveSelect('server', $serverOptions, [], _("Server"));
95 $serverSelect->setHasDescriptiveElements(true);
96 $container->add($serverSelect);
97
98 $container->add(new htmlResponsiveInputCheckbox('checkQuotas', false, _("Check quotas")));
99
100 $container->addVerticalSpacer('1rem');
101
102 $okButton = new htmlButton('runTest', _("Ok"));
103 $okButton->colspan = 2;
104 $container->addLabel($okButton);
105 $container->addField(new htmlOutputText('&nbsp;', false));
106 }
107 else {
108 $container->add(new htmlStatusMessage("ERROR", _('No lamdaemon server set, please update your LAM configuration settings.')));
109 }
110
111 parseHtml(null, $container, [], false);
112
113 echo "</form>\n";
114 echo "</div>\n";
115 include_once __DIR__ . '/../../lib/adminFooter.inc';
116
117
118 /**
119 * Runs a test case of lamdaemon.
120 *
121 * @param string $command test command
122 * @param boolean $stopTest specifies if test should be run
123 * @param Remote $remote SSH connection
124 * @param string $testText describing text
125 * @param htmlResponsiveRow $container container for HTML output
126 * @return boolean true, if errors occurred
127 */
128 function testRemoteCommand($command, $stopTest, $remote, $testText, $container) {
129 $okImage = "../../graphics/pass.svg";
130 $failImage = "../../graphics/del.svg";
131 // run remote command
132 if (!$stopTest) {
133 $container->add(new htmlOutputText($testText), 10, 4);
134 flush();
135 $lamdaemonOk = false;
136 $output = $remote->execute($command);
137 if ((stripos(strtolower($output), "error") === false) && ((str_starts_with($output, 'INFO,')) || (str_starts_with($output, 'QUOTA_ENTRY')))) {
138 $lamdaemonOk = true;
139 }
140 if ($lamdaemonOk) {
141 $container->add(new htmlImage($okImage), 2);
142 $container->add(new htmlOutputText(_("Lamdaemon successfully run.")), 12, 6);
143 }
144 else {
145 $container->add(new htmlImage($failImage), 2);
146 if (!(str_starts_with($output, 'ERROR,')) && !(str_starts_with($output, 'WARN,'))) {
147 // error messages from console (e.g. sudo)
148 $container->add(new htmlStatusMessage('ERROR', $output), 12, 6);
149 }
150 else {
151 // error messages from lamdaemon
152 $parts = explode(",", $output);
153 if (count($parts) === 2) {
154 $container->add(new htmlStatusMessage($parts[0], $parts[1]), 12, 6);
155 }
156 elseif (count($parts) === 3) {
157 $container->add(new htmlStatusMessage($parts[0], $parts[1], $parts[2]), 12, 6);
158 }
159 else {
160 $container->add(new htmlOutputText($output), 12, 6);
161 }
162 }
163 $stopTest = true;
164 }
165 }
166 $container->addVerticalSpacer('0.5rem');
167 return $stopTest;
168 }
169
170 /**
171 * Runs all tests for a given server.
172 *
173 * @param String $serverName server ID
174 * @param String $serverTitle server name
175 * @param boolean $testQuota true, if Quotas should be checked
176 * @param htmlResponsiveRow $container container for HTML output
177 */
178 function lamRunTestSuite($serverName, $serverTitle, $testQuota, $container): void {
179 $remoteServer = $_SESSION['config']->getScriptServerByName($serverName);
180 $SPLIT_DELIMITER = "###x##y##x###";
181 $LAMDAEMON_PROTOCOL_VERSION = '5';
182 $okImage = "../../graphics/pass.svg";
183 $failImage = "../../graphics/del.svg";
184
185 $stopTest = false;
186
187 $container->add(new htmlSubTitle($serverTitle));
188
189 // check script server and path
190 $container->add(new htmlOutputText(_("Lamdaemon server and path")), 10, 4);
191 if (strlen($serverName) < 3) {
192 $container->add(new htmlImage($failImage), 2);
193 $container->add(new htmlOutputText(_("No lamdaemon server set, please update your LAM configuration settings.")), 12, 6);
194 }
195 elseif (($_SESSION['config']->get_scriptPath() == null) || (strlen($_SESSION['config']->get_scriptPath()) < 10)) {
196 $container->add(new htmlImage($failImage), 2);
197 $container->add(new htmlOutputText(_("No lamdaemon path set, please update your LAM configuration settings.")), 12, 6);
198 $stopTest = true;
199 }
200 elseif (!str_ends_with($_SESSION['config']->get_scriptPath(), '.pl')) {
201 $container->add(new htmlImage($failImage), 2);
202 $container->add(new htmlOutputText(_("Lamdaemon path does not end with \".pl\". Did you enter the full path to the script?")), 12, 6);
203 $stopTest = true;
204 }
205 else {
206 $container->add(new htmlImage($okImage), 2);
207 $container->add(new htmlOutputText(sprintf(_("Using %s as lamdaemon remote server."), $serverName)), 12, 6);
208 }
209 $container->addVerticalSpacer('0.5rem');
210
211 // check Unix account of LAM admin
212 $ldapUser = $_SESSION['ldap']->getUserName();
213 $userName = '';
214 if (!$stopTest) {
215 $scriptUserName = $_SESSION['config']->getScriptUserName();
216 if (empty($scriptUserName)) {
217 $container->add(new htmlOutputText(_("Unix account")), 10, 4);
218 $unixOk = false;
219 $sr = @ldap_read($_SESSION['ldap']->server(), $ldapUser, "objectClass=posixAccount", ['uid'], 0, 0, 0, LDAP_DEREF_NEVER);
220 if ($sr) {
221 $entry = @ldap_get_entries($_SESSION['ldap']->server(), $sr);
222 if (($entry !== false) && isset($entry[0]['uid'][0])) {
223 $userName = $entry[0]['uid'][0];
224 $unixOk = true;
225 }
226 }
227 if ($unixOk) {
228 $container->add(new htmlImage($okImage), 2);
229 $container->add(new htmlOutputText(sprintf(_("Using %s to connect to remote server."), $userName)), 12, 6);
230 }
231 else {
232 $container->add(new htmlImage($failImage), 2);
233 $container->add(new htmlOutputText(sprintf(_("Your LAM admin user (%s) must be a valid Unix account to work with lamdaemon!"), $ldapUser)), 12, 6);
234 $stopTest = true;
235 }
236 $container->addVerticalSpacer('0.5rem');
237 }
238 else {
239 $userName = $_SESSION['config']->getScriptUserName();
240 }
241 }
242
243 // check SSH login
244 $remote = new Remote();
245 if (!$stopTest) {
246 $container->add(new htmlOutputText(_("SSH connection")), 10, 4);
247 flush();
248 try {
249 $remote->connect($remoteServer);
250 $container->add(new htmlImage($okImage), 2);
251 $container->add(new htmlOutputText(_("SSH connection established.")), 12, 6);
252 }
253 catch (LAMException $e) {
254 $container->add(new htmlImage($failImage), 2);
255 $container->add(new htmlOutputText($e->getTitle() . ' ' . $e->getMessage()), 12, 6);
256 $stopTest = true;
257 }
258 catch (Exception $e) {
259 $container->add(new htmlImage($failImage), 2);
260 $container->add(new htmlOutputText($e->getMessage()), 12, 6);
261 $stopTest = true;
262 }
263 }
264 $container->addVerticalSpacer('0.5rem');
265
266 if (!$stopTest) {
267 $stopTest = testRemoteCommand("+" . $SPLIT_DELIMITER . "test" . $SPLIT_DELIMITER . "basic", $stopTest, $remote, _("Execute lamdaemon"), $container);
268 }
269
270 if (!$stopTest) {
271 $stopTest = testRemoteCommand("+" . $SPLIT_DELIMITER . "test" . $SPLIT_DELIMITER . "version" . $SPLIT_DELIMITER . $LAMDAEMON_PROTOCOL_VERSION, $stopTest, $remote, _("Lamdaemon version"), $container);
272 }
273
274 if (!$stopTest) {
275 $stopTest = testRemoteCommand("+" . $SPLIT_DELIMITER . "test" . $SPLIT_DELIMITER . "nss" . $SPLIT_DELIMITER . "$userName", $stopTest, $remote, _("Lamdaemon: check NSS LDAP"), $container);
276 if (!$stopTest && $testQuota) {
277 testRemoteCommand("+" . $SPLIT_DELIMITER . "test" . $SPLIT_DELIMITER . "quota", $stopTest, $remote, _("Lamdaemon: Quota module installed"), $container);
278 testRemoteCommand("+" . $SPLIT_DELIMITER . "quota" . $SPLIT_DELIMITER . "get" . $SPLIT_DELIMITER . "user", $stopTest, $remote, _("Lamdaemon: read quotas"), $container);
279 }
280 }
281 $remote->disconnect();
282
283 $container->addVerticalSpacer('1rem');
284 $endMessage = new htmlOutputText(_("Lamdaemon test finished."));
285 $endMessage->colspan = 5;
286 $container->add($endMessage);
287 }