| 1 |
<?php |
| 2 |
|
| 3 |
namespace LAM\REMOTE; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
use \LAMException; |
| 7 |
use \phpseclib3\Net\SSH2; |
| 8 |
use \phpseclib3\Crypt\PublicKeyLoader; |
| 9 |
use \phpseclib3\Exception\NoKeyLoadedException; |
| 10 |
|
| 11 |
/* |
| 12 |
|
| 13 |
This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/) |
| 14 |
Copyright (C) 2017 - 2026 Roland Gruber |
| 15 |
|
| 16 |
This program is free software; you can redistribute it and/or modify |
| 17 |
it under the terms of the GNU General Public License as published by |
| 18 |
the Free Software Foundation; either version 2 of the License, or |
| 19 |
(at your option) any later version. |
| 20 |
|
| 21 |
This program is distributed in the hope that it will be useful, |
| 22 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 23 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 24 |
GNU General Public License for more details. |
| 25 |
|
| 26 |
You should have received a copy of the GNU General Public License |
| 27 |
along with this program; if not, write to the Free Software |
| 28 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 29 |
|
| 30 |
*/ |
| 31 |
|
| 32 |
/** |
| 33 |
* This file includes functions to control LAM remote executions. |
| 34 |
* |
| 35 |
* @author Roland Gruber |
| 36 |
* |
| 37 |
* @package modules |
| 38 |
*/ |
| 39 |
|
| 40 |
/** |
| 41 |
* Runs remote commands. |
| 42 |
* |
| 43 |
* @author Roland Gruber |
| 44 |
*/ |
| 45 |
class Remote { |
| 46 |
|
| 47 |
private ?RemoteServerConfiguration $serverConfig = null; |
| 48 |
|
| 49 |
/** |
| 50 |
* Constructor |
| 51 |
*/ |
| 52 |
public function __construct() { |
| 53 |
include_once __DIR__ . '/3rdParty/composer/autoload.php'; |
| 54 |
} |
| 55 |
|
| 56 |
/** @var SSH2|null SSH2 server handle */ |
| 57 |
private ?SSH2 $server = null; |
| 58 |
|
| 59 |
/** |
| 60 |
* Sends commands to remote script. |
| 61 |
* |
| 62 |
* @param string $command command to execute |
| 63 |
* @return string output of remote script |
| 64 |
*/ |
| 65 |
public function execute($command) { |
| 66 |
if ($this->server == null) { |
| 67 |
return ''; |
| 68 |
} |
| 69 |
return $this->server->exec("sudo " . $this->serverConfig->getScriptPath() . ' ' . escapeshellarg($command)); |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Connects to the given SSH server. |
| 74 |
* |
| 75 |
* @param RemoteServerConfiguration|null $server server (e.g. localhost or localhost,1234) |
| 76 |
* @throws LAMException error connecting to remote server |
| 77 |
*/ |
| 78 |
public function connect(?RemoteServerConfiguration $server): void { |
| 79 |
if ($server === null) { |
| 80 |
logNewMessage(LOG_ERR, 'No server provided for lamdaemon'); |
| 81 |
throw new LAMException(_("Unable to connect to remote server!")); |
| 82 |
} |
| 83 |
$this->serverConfig = $server; |
| 84 |
$serverNameParts = explode(",", $this->serverConfig->getServer()); |
| 85 |
if (count($serverNameParts) > 1) { |
| 86 |
$handle = @new SSH2($serverNameParts[0], (int) $serverNameParts[1]); |
| 87 |
} |
| 88 |
else { |
| 89 |
$handle = @new SSH2($serverNameParts[0]); |
| 90 |
} |
| 91 |
$this->loginSSH($handle); |
| 92 |
$this->server = $handle; |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Closes the connection. |
| 97 |
*/ |
| 98 |
public function disconnect(): void { |
| 99 |
if ($this->server == null) { |
| 100 |
return; |
| 101 |
} |
| 102 |
$this->server->disconnect(); |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Performs a login to the provided SSH handle. |
| 107 |
* |
| 108 |
* @param SSH2 $handle SSH handle |
| 109 |
* @throws Exception login failed |
| 110 |
* @throws LAMException error on login |
| 111 |
*/ |
| 112 |
private function loginSSH($handle): void { |
| 113 |
$username = $this->serverConfig->getUserName(); |
| 114 |
if (empty($username)) { |
| 115 |
// get user name from current LAM user |
| 116 |
$ldapUser = $_SESSION['ldap']->getUserName(); |
| 117 |
$sr = @ldap_read($_SESSION['ldap']->server(), $ldapUser, "objectClass=posixAccount", ['uid'], 0, 0, 0, LDAP_DEREF_NEVER); |
| 118 |
if ($sr) { |
| 119 |
$entry = @ldap_get_entries($_SESSION['ldap']->server(), $sr); |
| 120 |
if (!empty($entry[0]['uid'])) { |
| 121 |
$username = $entry[0]['uid'][0]; |
| 122 |
} |
| 123 |
} |
| 124 |
if (empty($username)) { |
| 125 |
throw new LAMException(sprintf(_("Your LAM admin user (%s) must be a valid Unix account to work with lamdaemon!"), getAbstractDN($ldapUser))); |
| 126 |
} |
| 127 |
} |
| 128 |
$keyPath = $this->serverConfig->getSshKey(); |
| 129 |
$keyPassword = $this->serverConfig->getSshKeyPassword(); |
| 130 |
$password = empty($keyPath) ? $_SESSION['ldap']->getPassword() : $this->loadKey($keyPath, $keyPassword); |
| 131 |
try { |
| 132 |
$login = @$handle->login($username, $password); |
| 133 |
if (!$login) { |
| 134 |
$errors = $handle->getErrors(); |
| 135 |
$errors = empty($errors) ? null : implode(' ', $errors); |
| 136 |
throw new LAMException(_("Unable to login to lamdaemon server."), $errors); |
| 137 |
} |
| 138 |
} |
| 139 |
catch (Exception $e) { |
| 140 |
throw new LAMException(_("Unable to login to lamdaemon server."), $e->getMessage(), $e); |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Loads the key |
| 146 |
* |
| 147 |
* @param string $keyPath file name |
| 148 |
* @param string $keyPassword password |
| 149 |
* @return \phpseclib3\Crypt\Common\AsymmetricKey key object |
| 150 |
* @throws LAMException error loading key |
| 151 |
*/ |
| 152 |
public function loadKey($keyPath, $keyPassword) { |
| 153 |
// use key authentication |
| 154 |
if (!file_exists($keyPath) || !is_readable($keyPath)) { |
| 155 |
throw new LAMException(sprintf(_("Unable to read %s."), htmlspecialchars($keyPath))); |
| 156 |
} |
| 157 |
$keyData = file_get_contents($keyPath); |
| 158 |
try { |
| 159 |
if (empty($keyPassword)) { |
| 160 |
return PublicKeyLoader::load($keyData); |
| 161 |
} |
| 162 |
else { |
| 163 |
return PublicKeyLoader::load($keyData, $keyPassword); |
| 164 |
} |
| 165 |
} |
| 166 |
catch (NoKeyLoadedException $e) { |
| 167 |
logNewMessage(LOG_ERR, sprintf('Unable to load key %s: %s', $keyPath, $e->getMessage())); |
| 168 |
throw new LAMException(sprintf(_("Unable to load key %s."), htmlspecialchars($keyPath)), '', $e); |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
} |
| 173 |
|
| 174 |
/** |
| 175 |
* Server configuration. |
| 176 |
* |
| 177 |
* @author Roland Gruber |
| 178 |
*/ |
| 179 |
class RemoteServerConfiguration { |
| 180 |
|
| 181 |
private string $server; |
| 182 |
private string $label; |
| 183 |
private string $homeDirPrefix; |
| 184 |
private string $scriptPath; |
| 185 |
private string $userName; |
| 186 |
private string $sshKey; |
| 187 |
private string $sshKeyPassword; |
| 188 |
|
| 189 |
/** |
| 190 |
* Constructor |
| 191 |
* |
| 192 |
* @param string $server server DNS name |
| 193 |
* @param string $label label for GUI |
| 194 |
* @param string $homeDirPrefix prefix for home directories |
| 195 |
* @param string $scriptPath script path |
| 196 |
* @param string $userName user name |
| 197 |
* @param string $sshKey SSH key path |
| 198 |
* @param string $sshKeyPassword SSH key password |
| 199 |
*/ |
| 200 |
public function __construct(string $server, string $label, string $homeDirPrefix, string $scriptPath, string $userName, string $sshKey, string $sshKeyPassword) { |
| 201 |
$this->server = $server; |
| 202 |
$this->label = $label; |
| 203 |
if (empty($label)) { |
| 204 |
$this->label = $server; |
| 205 |
} |
| 206 |
$this->homeDirPrefix = $homeDirPrefix; |
| 207 |
$this->scriptPath = $scriptPath; |
| 208 |
$this->userName = $userName; |
| 209 |
$this->sshKey = $sshKey; |
| 210 |
$this->sshKeyPassword = $sshKeyPassword; |
| 211 |
} |
| 212 |
|
| 213 |
/** |
| 214 |
* Returns the server's DNS name. |
| 215 |
* |
| 216 |
* @return string server name |
| 217 |
*/ |
| 218 |
public function getServer(): string { |
| 219 |
return $this->server; |
| 220 |
} |
| 221 |
|
| 222 |
/** |
| 223 |
* Returns a descriptive label. |
| 224 |
* |
| 225 |
* @return string label |
| 226 |
*/ |
| 227 |
public function getLabel(): string { |
| 228 |
return $this->label; |
| 229 |
} |
| 230 |
|
| 231 |
/** |
| 232 |
* Returns the prefix for user home directories. |
| 233 |
* |
| 234 |
* @return string prefix for user home directories |
| 235 |
*/ |
| 236 |
public function getHomeDirPrefix(): string { |
| 237 |
return $this->homeDirPrefix; |
| 238 |
} |
| 239 |
|
| 240 |
/** |
| 241 |
* Returns the script path. |
| 242 |
* |
| 243 |
* @return string script path |
| 244 |
*/ |
| 245 |
public function getScriptPath(): string { |
| 246 |
return $this->scriptPath; |
| 247 |
} |
| 248 |
|
| 249 |
/** |
| 250 |
* Returns the username to connect. |
| 251 |
* |
| 252 |
* @return string user name |
| 253 |
*/ |
| 254 |
public function getUserName(): string { |
| 255 |
return $this->userName; |
| 256 |
} |
| 257 |
|
| 258 |
/** |
| 259 |
* Returns the path to the SSH key. |
| 260 |
* |
| 261 |
* @return string path |
| 262 |
*/ |
| 263 |
public function getSshKey(): string { |
| 264 |
return $this->sshKey; |
| 265 |
} |
| 266 |
|
| 267 |
/** |
| 268 |
* Returns the SSH key password. |
| 269 |
* |
| 270 |
* @return string password |
| 271 |
*/ |
| 272 |
public function getSshKeyPassword(): string { |
| 273 |
return $this->sshKeyPassword; |
| 274 |
} |
| 275 |
|
| 276 |
} |