| 1 |
<?php |
| 2 |
|
| 3 |
use LAM\PDF\PDFTable; |
| 4 |
use LAM\PDF\PDFTableCell; |
| 5 |
use LAM\PDF\PDFTableRow; |
| 6 |
use LAM\TYPES\ConfiguredType; |
| 7 |
|
| 8 |
/* |
| 9 |
|
| 10 |
This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/) |
| 11 |
Copyright (C) 2005 - 2026 Roland Gruber |
| 12 |
|
| 13 |
This program is free software; you can redistribute it and/or modify |
| 14 |
it under the terms of the GNU General Public License as published by |
| 15 |
the Free Software Foundation; either version 2 of the License, or |
| 16 |
(at your option) any later version. |
| 17 |
|
| 18 |
This program is distributed in the hope that it will be useful, |
| 19 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 |
GNU General Public License for more details. |
| 22 |
|
| 23 |
You should have received a copy of the GNU General Public License |
| 24 |
along with this program; if not, write to the Free Software |
| 25 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 26 |
*/ |
| 27 |
|
| 28 |
/** |
| 29 |
* Manages SSH public keys. |
| 30 |
* |
| 31 |
* @package modules |
| 32 |
* @author Roland Gruber |
| 33 |
* @author Andrew Dibble <[email protected]> |
| 34 |
*/ |
| 35 |
|
| 36 |
/** |
| 37 |
* Manages SSH public keys. |
| 38 |
* |
| 39 |
* @package modules |
| 40 |
*/ |
| 41 |
class ldapPublicKey extends baseModule { |
| 42 |
|
| 43 |
/** session variable for existing keys in self-service */ |
| 44 |
private const SESS_KEY_LIST = 'ldapPublicKey_keyList'; |
| 45 |
|
| 46 |
/** |
| 47 |
* Creates a new ldapPublicKey object. |
| 48 |
* |
| 49 |
* @param string $scope account type (user, group, host) |
| 50 |
*/ |
| 51 |
public function __construct($scope) { |
| 52 |
// call parent constructor |
| 53 |
parent::__construct($scope); |
| 54 |
$this->autoAddObjectClasses = false; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Returns the LDAP attribute name for the keys. |
| 59 |
* |
| 60 |
* @return string attribute name |
| 61 |
*/ |
| 62 |
protected function getAttributeName(): string { |
| 63 |
return 'sshpublickey'; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Returns if the extension uses an object class. |
| 68 |
* |
| 69 |
* @return bool uses an object class |
| 70 |
*/ |
| 71 |
protected function hasObjectClass(): bool { |
| 72 |
return true; |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Returns the object class name. |
| 77 |
* |
| 78 |
* @return string object class name |
| 79 |
*/ |
| 80 |
protected function getObjectClass(): string { |
| 81 |
return 'ldapPublicKey'; |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* {@inheritDoc} |
| 86 |
*/ |
| 87 |
public function can_manage(): bool { |
| 88 |
return $this->get_scope() === 'user'; |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* {@inheritDoc} |
| 93 |
*/ |
| 94 |
public function get_metaData(): array { |
| 95 |
$return = []; |
| 96 |
// icon |
| 97 |
$return['icon'] = 'key.svg'; |
| 98 |
// alias name |
| 99 |
$return["alias"] = _("SSH public key"); |
| 100 |
// module dependencies |
| 101 |
$return['dependencies'] = ['depends' => [], 'conflicts' => []]; |
| 102 |
// managed object classes |
| 103 |
if ($this->hasObjectClass()) { |
| 104 |
$return['objectClasses'] = [$this->getObjectClass()]; |
| 105 |
} |
| 106 |
// managed attributes |
| 107 |
$return['attributes'] = [$this->getAttributeName()]; |
| 108 |
// help Entries |
| 109 |
$return['help'] = [ |
| 110 |
$this->getAttributeName() => [ |
| 111 |
"Headline" => _("SSH public key"), 'attr' => $this->getAttributeName(), |
| 112 |
"Text" => _("Please enter your public SSH key.") |
| 113 |
], |
| 114 |
'keyList' => [ |
| 115 |
"Headline" => _("SSH public key"), 'attr' => $this->getAttributeName(), |
| 116 |
"Text" => _("Please a comma separated list of your public SSH keys.") |
| 117 |
], |
| 118 |
'upload' => [ |
| 119 |
"Headline" => _("File upload"), 'attr' => $this->getAttributeName(), |
| 120 |
"Text" => _("Upload a file with one or more keys. Each line contains one key.") |
| 121 |
], |
| 122 |
'autoAdd' => [ |
| 123 |
"Headline" => _("Automatically add this extension"), |
| 124 |
"Text" => _("This will enable the extension automatically if this profile is loaded.") |
| 125 |
], |
| 126 |
]; |
| 127 |
// profile options |
| 128 |
if ($this->hasObjectClass()) { |
| 129 |
$profileContainer = new htmlResponsiveRow(); |
| 130 |
$profileContainer->add(new htmlResponsiveInputCheckbox(static::class . '_addExt', false, _('Automatically add this extension'), 'autoAdd')); |
| 131 |
$return['profile_options'] = $profileContainer; |
| 132 |
} |
| 133 |
// upload fields |
| 134 |
$return['upload_columns'] = [ |
| 135 |
[ |
| 136 |
'name' => static::class . '_sshPublicKey', |
| 137 |
'description' => _('SSH public key'), |
| 138 |
'help' => 'keyList', |
| 139 |
'example' => _('ssh-rsa 234234 user@host') |
| 140 |
] |
| 141 |
]; |
| 142 |
// available PDF fields |
| 143 |
$return['PDF_fields'] = [ |
| 144 |
'sshPublicKey' => _('SSH public keys') |
| 145 |
]; |
| 146 |
// self-service field settings |
| 147 |
$return['selfServiceFieldSettings'] = [ |
| 148 |
'sshPublicKey' => _('SSH public keys'), |
| 149 |
]; |
| 150 |
return $return; |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* {@inheritDoc} |
| 155 |
*/ |
| 156 |
protected function load_Messages(): void { |
| 157 |
$this->messages['file'][0] = ['ERROR', _('No file selected.')]; |
| 158 |
$this->messages['file'][1] = ['ERROR', _('The uploaded file seems to be no valid public key.')]; |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* {@inheritDoc} |
| 163 |
*/ |
| 164 |
public function loadAttributesFromAccountCopy(array $ldapAttributes, array $attributesToIgnore = []): void { |
| 165 |
parent::loadAttributesFromAccountCopy($ldapAttributes, array_merge(baseModule::ATTRIBUTES_TO_IGNORE_ON_COPY_DEFAULT, [$this->getAttributeName()])); |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* {@inheritDoc} |
| 170 |
*/ |
| 171 |
public function display_html_attributes(): htmlElement|array { |
| 172 |
$return = new htmlResponsiveRow(); |
| 173 |
if (!$this->hasObjectClass() || in_array_ignore_case($this->getObjectClass(), $this->attributes['objectClass'])) { |
| 174 |
$this->addMultiValueInputTextField($return, $this->getAttributeName(), _('SSH public key'), |
| 175 |
false, 16384, false, null, 50); |
| 176 |
// file upload |
| 177 |
$return->addVerticalSpacer('2rem'); |
| 178 |
$return->addLabel(new htmlOutputText(_('Upload a file'))); |
| 179 |
$uploadGroup = new htmlGroup(); |
| 180 |
$uploadGroup->addElement(new htmlInputFileUpload($this->getAttributeName() . 'File')); |
| 181 |
$uploadGroup->addElement(new htmlSpacer('1px', null)); |
| 182 |
$uploadGroup->addElement(new htmlButton($this->getAttributeName() . 'FileSubmit', _('Upload'))); |
| 183 |
$uploadGroup->addElement(new htmlSpacer('5px', null)); |
| 184 |
$uploadGroup->addElement(new htmlHelpLink('upload')); |
| 185 |
$return->addField($uploadGroup); |
| 186 |
|
| 187 |
if ($this->hasObjectClass()) { |
| 188 |
$return->addVerticalSpacer('2rem'); |
| 189 |
$remButton = new htmlButton('remObjectClass', _('Remove SSH public key extension')); |
| 190 |
$remButton->setCSSClasses(['lam-danger']); |
| 191 |
$return->add($remButton, 12, 12, 12, 'text-center'); |
| 192 |
} |
| 193 |
} |
| 194 |
else { |
| 195 |
$return->add(new htmlButton('addObjectClass', _('Add SSH public key extension'))); |
| 196 |
} |
| 197 |
return $return; |
| 198 |
} |
| 199 |
|
| 200 |
/** |
| 201 |
* {@inheritDoc} |
| 202 |
*/ |
| 203 |
public function process_attributes(): array { |
| 204 |
$messages = []; |
| 205 |
// add extension |
| 206 |
if (isset($_POST['addObjectClass'])) { |
| 207 |
$this->attributes['objectClass'][] = $this->getObjectClass(); |
| 208 |
return []; |
| 209 |
} |
| 210 |
// remove extension |
| 211 |
elseif (isset($_POST['remObjectClass'])) { |
| 212 |
$this->attributes['objectClass'] = array_delete([$this->getObjectClass()], $this->attributes['objectClass']); |
| 213 |
if (!empty($this->attributes[$this->getAttributeName()])) { |
| 214 |
unset($this->attributes[$this->getAttributeName()]); |
| 215 |
} |
| 216 |
return []; |
| 217 |
} |
| 218 |
// skip processing if extension is not active |
| 219 |
if ($this->hasObjectClass() && !in_array($this->getObjectClass(), $this->attributes['objectClass'])) { |
| 220 |
return []; |
| 221 |
} |
| 222 |
$this->processMultiValueInputTextField($this->getAttributeName(), $messages); |
| 223 |
// file upload |
| 224 |
if (isset($_POST[$this->getAttributeName() . 'FileSubmit'])) { |
| 225 |
if ($_FILES[$this->getAttributeName() . 'File'] && ($_FILES[$this->getAttributeName() . 'File']['size'] > 0)) { |
| 226 |
$handle = fopen($_FILES[$this->getAttributeName() . 'File']['tmp_name'], "r"); |
| 227 |
$data = fread($handle, 10000000); |
| 228 |
fclose($handle); |
| 229 |
$isOk = true; |
| 230 |
if (is_executable('/usr/bin/ssh-keygen')) { |
| 231 |
$out = []; |
| 232 |
$retVal = 0; |
| 233 |
exec('/usr/bin/ssh-keygen -l -f ' . escapeshellarg($_FILES[$this->getAttributeName() . 'File']['tmp_name']), $out, $retVal); |
| 234 |
if ($retVal !== 0) { |
| 235 |
$messages[] = $this->messages['file'][1]; |
| 236 |
$isOk = false; |
| 237 |
} |
| 238 |
} |
| 239 |
if ($isOk) { |
| 240 |
$data = str_replace("\r\n", "\n", $data); |
| 241 |
$data = str_replace("\r", "\n", $data); |
| 242 |
$lines = explode("\n", $data); |
| 243 |
foreach ($lines as $line) { |
| 244 |
if (!empty($line) && !(str_starts_with($line, '#'))) { |
| 245 |
$this->attributes[$this->getAttributeName()][] = $line; |
| 246 |
} |
| 247 |
} |
| 248 |
} |
| 249 |
} |
| 250 |
else { |
| 251 |
$messages[] = $this->messages['file'][0]; |
| 252 |
} |
| 253 |
} |
| 254 |
$this->attributes[$this->getAttributeName()] = array_values(array_unique($this->attributes[$this->getAttributeName()])); |
| 255 |
return $messages; |
| 256 |
} |
| 257 |
|
| 258 |
/** |
| 259 |
* {@inheritDoc} |
| 260 |
*/ |
| 261 |
public function save_attributes(): array { |
| 262 |
if ($this->hasObjectClass() |
| 263 |
&& !in_array($this->getObjectClass(), $this->attributes['objectClass']) |
| 264 |
&& !in_array($this->getObjectClass(), $this->orig['objectClass'])) { |
| 265 |
// skip saving if the extension was not added/modified |
| 266 |
return []; |
| 267 |
} |
| 268 |
return parent::save_attributes(); |
| 269 |
} |
| 270 |
|
| 271 |
/** |
| 272 |
* {@inheritDoc} |
| 273 |
*/ |
| 274 |
public function load_profile(array $profile): void { |
| 275 |
// profile mappings in meta data |
| 276 |
parent::load_profile($profile); |
| 277 |
// add extension |
| 278 |
if ($this->hasObjectClass() |
| 279 |
&& isset($profile[static::class . '_addExt'][0]) |
| 280 |
&& ($profile[static::class . '_addExt'][0] === "true") |
| 281 |
&& !in_array($this->getObjectClass(), $this->attributes['objectClass'])) { |
| 282 |
$this->attributes['objectClass'][] = $this->getObjectClass(); |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
/** |
| 287 |
* {@inheritDoc} |
| 288 |
*/ |
| 289 |
public function build_uploadAccounts(array $rawAccounts, array $ids, array &$partialAccounts, array $selectedModules, ConfiguredType &$type): array { |
| 290 |
$messages = []; |
| 291 |
for ($i = 0; $i < count($rawAccounts); $i++) { |
| 292 |
// add object class |
| 293 |
if ($this->hasObjectClass() && !in_array($this->getObjectClass(), $partialAccounts[$i]['objectClass'])) { |
| 294 |
$partialAccounts[$i]['objectClass'][] = $this->getObjectClass(); |
| 295 |
} |
| 296 |
// add keys |
| 297 |
$this->mapSimpleUploadField($rawAccounts, $ids, $partialAccounts, $i, static::class . '_sshPublicKey', |
| 298 |
$this->getAttributeName(), null, [], $messages, '/,[ ]*/'); |
| 299 |
} |
| 300 |
return $messages; |
| 301 |
} |
| 302 |
|
| 303 |
/** |
| 304 |
* {@inheritDoc} |
| 305 |
*/ |
| 306 |
public function get_pdfEntries(array $pdfKeys, string $typeId): array { |
| 307 |
$return = []; |
| 308 |
if (!empty($this->attributes[$this->getAttributeName()])) { |
| 309 |
$pdfTable = new PDFTable(_('SSH public keys')); |
| 310 |
for ($i = 0; $i < count($this->attributes[$this->getAttributeName()]); $i++) { |
| 311 |
$pdfRow = new PDFTableRow(); |
| 312 |
$pdfRow->cells[] = new PDFTableCell($this->attributes[$this->getAttributeName()][$i]); |
| 313 |
$pdfTable->rows[] = $pdfRow; |
| 314 |
} |
| 315 |
$this->addPDFTable($return, 'sshPublicKey', $pdfTable); |
| 316 |
} |
| 317 |
return $return; |
| 318 |
} |
| 319 |
|
| 320 |
/** |
| 321 |
* {@inheritDoc} |
| 322 |
*/ |
| 323 |
public function getSelfServiceOptions(array $fields, array $attributes, bool $passwordChangeOnly, array $readOnlyFields): array { |
| 324 |
$return = []; |
| 325 |
if ($passwordChangeOnly) { |
| 326 |
return $return; // no fields as long no LDAP content can be read |
| 327 |
} |
| 328 |
if (in_array('sshPublicKey', $fields)) { |
| 329 |
$sshPublicKeys = []; |
| 330 |
if (isset($attributes[$this->getAttributeName()][0])) { |
| 331 |
$sshPublicKeys = $attributes[$this->getAttributeName()]; |
| 332 |
} |
| 333 |
$_SESSION[self::SESS_KEY_LIST] = $sshPublicKeys; |
| 334 |
$keyTable = new htmlTable(); |
| 335 |
// JavaScript functions |
| 336 |
$keyTable->addElement($this->getSelfServiceKeysJSBlock(), true); |
| 337 |
// input fields |
| 338 |
$keyTable->addElement(new htmlDiv('sshPublicKeyDiv', $this->getSelfServiceKeys()), true); |
| 339 |
$keyLabel = new htmlOutputText($this->getSelfServiceLabel('sshPublicKey', _('SSH public keys'))); |
| 340 |
$row = new htmlResponsiveRow(); |
| 341 |
$row->addLabel($keyLabel); |
| 342 |
$row->addField($keyTable); |
| 343 |
$return['sshPublicKey'] = $row; |
| 344 |
} |
| 345 |
return $return; |
| 346 |
} |
| 347 |
|
| 348 |
/** |
| 349 |
* Returns the meta HTML code to display the key area. |
| 350 |
* This also includes the file upload. |
| 351 |
* |
| 352 |
* @return htmlResponsiveRow key content |
| 353 |
*/ |
| 354 |
private function getSelfServiceKeys(): htmlResponsiveRow { |
| 355 |
$keys = $_SESSION[self::SESS_KEY_LIST]; |
| 356 |
$content = new htmlResponsiveRow(); |
| 357 |
if (count($keys) > 0) { |
| 358 |
for ($i = 0; $i < count($keys); $i++) { |
| 359 |
$group = new htmlGroup(); |
| 360 |
$keyInput = new htmlInputField('sshPublicKey_' . $i, $keys[$i]); |
| 361 |
$keyInput->setFieldMaxLength(16384); |
| 362 |
$group->addElement($keyInput); |
| 363 |
$delLink = new htmlLink('', '#', '../../graphics/del.svg'); |
| 364 |
$delLink->setTitle(_('Delete')); |
| 365 |
$delLink->setOnClick('ldapPublicKeyDeleteKey(' . $i . ');return false;'); |
| 366 |
$group->addElement($delLink); |
| 367 |
if ($i === (count($keys) - 1)) { |
| 368 |
$addLink = new htmlLink('', '#', '../../graphics/add.svg'); |
| 369 |
$addLink->setTitle(_('Add')); |
| 370 |
$addLink->setOnClick('ldapPublicKeyAddKey(' . count($keys) . ');return false;'); |
| 371 |
$group->addElement($addLink); |
| 372 |
} |
| 373 |
$content->add($group, 12, 12, 12, 'nowrap'); |
| 374 |
} |
| 375 |
} |
| 376 |
else { |
| 377 |
$addLink = new htmlLink('', '#', '../../graphics/add.svg'); |
| 378 |
$addLink->setTitle(_('Add')); |
| 379 |
$addLink->setOnClick('ldapPublicKeyAddKey(' . count($keys) . ');return false;'); |
| 380 |
$content->add($addLink); |
| 381 |
} |
| 382 |
// upload button |
| 383 |
$uploadButtonGroup = new htmlGroup(); |
| 384 |
$uploadLabel = new htmlLabel('ldappublickey_file', _('Upload a file')); |
| 385 |
$uploadButtonGroup->addElement($uploadLabel); |
| 386 |
$uploadInput = new htmlInputFileUpload('ldappublickey_file'); |
| 387 |
$uploadInput->setOnChange('ldapPublicKeyUploadKeyFunction();'); |
| 388 |
$uploadButtonGroup->addElement($uploadInput); |
| 389 |
$content->add(new htmlDiv('ldapPublicKeyKeyUploadId', $uploadButtonGroup, ['lam-upload-button'])); |
| 390 |
return $content; |
| 391 |
} |
| 392 |
|
| 393 |
/** |
| 394 |
* Returns the Java Script functions to manage the keys. |
| 395 |
* |
| 396 |
* @return htmlJavaScript JS block |
| 397 |
*/ |
| 398 |
private function getSelfServiceKeysJSBlock() { |
| 399 |
$content = ' |
| 400 |
function ldapPublicKeyDeleteKey(id) { |
| 401 |
var actionJSON = { |
| 402 |
"action": "deleteKey", |
| 403 |
"id": id |
| 404 |
}; |
| 405 |
let count = 0; |
| 406 |
while (document.getElementById("sshPublicKey_" + count)) { |
| 407 |
actionJSON["sshPublicKey_" + count] = document.getElementById(\'sshPublicKey_\' + count).value; |
| 408 |
count++; |
| 409 |
} |
| 410 |
let data = new FormData(); |
| 411 |
data.append("' . getSecurityTokenName() . '", "' . getSecurityTokenValue() . '"); |
| 412 |
data.append("jsonInput", JSON.stringify(actionJSON)); |
| 413 |
fetch(\'../misc/ajax.php?selfservice=1&module=ldapPublicKey&scope=user\', { |
| 414 |
method: "POST", |
| 415 |
body: data |
| 416 |
}) |
| 417 |
.then(async response => { |
| 418 |
const jsonData = await response.json(); |
| 419 |
ldapPublicKeyDeleteKeyHandleReply(jsonData); |
| 420 |
}); |
| 421 |
} |
| 422 |
|
| 423 |
function ldapPublicKeyDeleteKeyHandleReply(data) { |
| 424 |
if (data.errorsOccurred == "false") { |
| 425 |
document.getElementById(\'sshPublicKeyDiv\').innerHTML = data.html; |
| 426 |
} |
| 427 |
else { |
| 428 |
window.lam.dialog.showInfo(data.errormessage, "' . _('Ok') . '"); |
| 429 |
} |
| 430 |
} |
| 431 |
|
| 432 |
function ldapPublicKeyAddKey(count) { |
| 433 |
var actionJSON = { |
| 434 |
"action": "addKey" |
| 435 |
}; |
| 436 |
for (c = 0; c < count; c++) { |
| 437 |
actionJSON["sshPublicKey_" + c] = document.getElementById(\'sshPublicKey_\' + c).value; |
| 438 |
} |
| 439 |
let data = new FormData(); |
| 440 |
data.append("' . getSecurityTokenName() . '", "' . getSecurityTokenValue() . '"); |
| 441 |
data.append("jsonInput", JSON.stringify(actionJSON)); |
| 442 |
fetch(\'../misc/ajax.php?selfservice=1&module=ldapPublicKey&scope=user\', { |
| 443 |
method: "POST", |
| 444 |
body: data |
| 445 |
}) |
| 446 |
.then(async response => { |
| 447 |
const jsonData = await response.json(); |
| 448 |
ldapPublicKeyAddKeyHandleReply(jsonData); |
| 449 |
}); |
| 450 |
} |
| 451 |
|
| 452 |
function ldapPublicKeyAddKeyHandleReply(data) { |
| 453 |
if (data.errorsOccurred == "false") { |
| 454 |
document.getElementById(\'sshPublicKeyDiv\').innerHTML = data.html; |
| 455 |
} |
| 456 |
else { |
| 457 |
window.lam.dialog.showInfo(data.errormessage, "' . _('Ok') . '"); |
| 458 |
} |
| 459 |
} |
| 460 |
|
| 461 |
const ldapPublicKeyUploadKeyFunction = function() { |
| 462 |
let data = new FormData(); |
| 463 |
data.append("action", "ajaxKeyUpload"); |
| 464 |
let count = 0; |
| 465 |
while (document.getElementById("sshPublicKey_" + count)) { |
| 466 |
data.append("sshPublicKey_" + count, document.getElementById(\'sshPublicKey_\' + count).value); |
| 467 |
count++; |
| 468 |
} |
| 469 |
data.append("' . getSecurityTokenName() . '", "' . getSecurityTokenValue() . '"); |
| 470 |
let reader = new FileReader(); |
| 471 |
reader.onload = function () { |
| 472 |
const content = reader.result; |
| 473 |
data.append("file", content); |
| 474 |
fetch(\'../misc/ajax.php?selfservice=1&module=ldapPublicKey&scope=user\', { |
| 475 |
method: "POST", |
| 476 |
body: data |
| 477 |
}) |
| 478 |
.then(async response => { |
| 479 |
const data = await response.json(); |
| 480 |
if (data.success) { |
| 481 |
if (data.html) { |
| 482 |
document.getElementById(\'sshPublicKeyDiv\').innerHTML = data.html; |
| 483 |
} |
| 484 |
} |
| 485 |
else if (data.error) { |
| 486 |
window.lam.dialog.showInfo(data.error, "' . _('Ok') . '"); |
| 487 |
} |
| 488 |
else if (data.errormessage) { |
| 489 |
window.lam.dialog.showInfo(data.errormessage, "' . _('Ok') . '"); |
| 490 |
} |
| 491 |
}); |
| 492 |
}; |
| 493 |
const fileInput = document.getElementById("ldappublickey_file"); |
| 494 |
reader.readAsBinaryString(fileInput.files[0]); |
| 495 |
} |
| 496 |
|
| 497 |
'; |
| 498 |
return new htmlJavaScript($content); |
| 499 |
} |
| 500 |
|
| 501 |
/** |
| 502 |
* {@inheritDoc} |
| 503 |
*/ |
| 504 |
public function checkSelfServiceOptions(array $fields, array $attributes, bool $passwordChangeOnly, array $readOnlyFields): array { |
| 505 |
$return = ['messages' => [], 'add' => [], 'del' => [], 'mod' => [], 'info' => []]; |
| 506 |
if ($passwordChangeOnly) { |
| 507 |
return $return; // skip processing if only a password change is done |
| 508 |
} |
| 509 |
if (in_array('sshPublicKey', $fields)) { |
| 510 |
$newKeys = []; |
| 511 |
$counter = 0; |
| 512 |
while (isset($_POST['sshPublicKey_' . $counter])) { |
| 513 |
$newKeys[] = $_POST['sshPublicKey_' . $counter]; |
| 514 |
$counter++; |
| 515 |
} |
| 516 |
$count = count($newKeys); |
| 517 |
for ($i = 0; $i < $count; $i++) { |
| 518 |
if (trim($newKeys[$i]) === '') { |
| 519 |
unset($newKeys[$i]); |
| 520 |
} |
| 521 |
} |
| 522 |
$newKeys = array_values(array_unique($newKeys)); |
| 523 |
$oldKeys = []; |
| 524 |
if (isset($attributes[$this->getAttributeName()][0])) { |
| 525 |
$oldKeys = $attributes[$this->getAttributeName()]; |
| 526 |
} |
| 527 |
$update = false; |
| 528 |
if (count($newKeys) !== count($oldKeys)) { |
| 529 |
$update = true; |
| 530 |
} |
| 531 |
else { |
| 532 |
for ($i = 0; $i < count($newKeys); $i++) { |
| 533 |
if (!in_array($newKeys[$i], $oldKeys)) { |
| 534 |
$update = true; |
| 535 |
break; |
| 536 |
} |
| 537 |
} |
| 538 |
} |
| 539 |
if ($update) { |
| 540 |
if (count($oldKeys) === 0) { |
| 541 |
$return['add'][$this->getAttributeName()] = $newKeys; |
| 542 |
} |
| 543 |
elseif (count($newKeys) === 0) { |
| 544 |
$return['del'][$this->getAttributeName()] = $newKeys; |
| 545 |
} |
| 546 |
else { |
| 547 |
$return['mod'][$this->getAttributeName()] = $newKeys; |
| 548 |
} |
| 549 |
} |
| 550 |
} |
| 551 |
return $return; |
| 552 |
} |
| 553 |
|
| 554 |
/** |
| 555 |
* {@inheritDoc} |
| 556 |
*/ |
| 557 |
public function handleAjaxRequest(): void { |
| 558 |
// AJAX uploads are non-JSON |
| 559 |
if (isset($_POST['action']) && ($_POST['action'] == 'ajaxKeyUpload')) { |
| 560 |
$this->ajaxUpload(); |
| 561 |
return; |
| 562 |
} |
| 563 |
$jsonInput = json_decode($_POST['jsonInput'], true); |
| 564 |
$jsonReturn = self::invalidAjaxRequest(); |
| 565 |
if (isset($jsonInput['action'])) { |
| 566 |
if ($jsonInput['action'] == 'deleteKey') { |
| 567 |
$jsonReturn = $this->ajaxDeleteSelfServiceKey($jsonInput); |
| 568 |
} |
| 569 |
elseif ($jsonInput['action'] == 'addKey') { |
| 570 |
foreach ($_SESSION[self::SESS_KEY_LIST] as $index => $value) { |
| 571 |
if (isset($jsonInput['sshPublicKey_' . $index])) { |
| 572 |
$_SESSION[self::SESS_KEY_LIST][$index] = $jsonInput['sshPublicKey_' . $index]; |
| 573 |
} |
| 574 |
} |
| 575 |
$_SESSION[self::SESS_KEY_LIST][] = ''; |
| 576 |
ob_start(); |
| 577 |
$contentElement = $this->getSelfServiceKeys(); |
| 578 |
ob_end_clean(); |
| 579 |
ob_start(); |
| 580 |
parseHtml(null, $contentElement, [], true); |
| 581 |
$content = ob_get_contents(); |
| 582 |
ob_end_clean(); |
| 583 |
$jsonReturn = [ |
| 584 |
'errorsOccurred' => 'false', |
| 585 |
'html' => $content, |
| 586 |
]; |
| 587 |
} |
| 588 |
} |
| 589 |
echo json_encode($jsonReturn); |
| 590 |
} |
| 591 |
|
| 592 |
/** |
| 593 |
* Handles an AJAX file upload and prints the JSON result. |
| 594 |
*/ |
| 595 |
private function ajaxUpload(): void { |
| 596 |
$result = ['success' => true]; |
| 597 |
if (!isset($_POST['file']) || (strlen($_POST['file']) < 10)) { |
| 598 |
$result = ['error' => _('No file received.')]; |
| 599 |
} |
| 600 |
else { |
| 601 |
foreach ($_SESSION[self::SESS_KEY_LIST] as $index => $value) { |
| 602 |
if (isset($_POST['sshPublicKey_' . $index])) { |
| 603 |
$_SESSION[self::SESS_KEY_LIST][$index] = $_POST['sshPublicKey_' . $index]; |
| 604 |
} |
| 605 |
} |
| 606 |
$data = $_POST['file']; |
| 607 |
if (is_executable('/usr/bin/ssh-keygen')) { |
| 608 |
$tmpFile = tmpfile(); |
| 609 |
$tmpFilePath = stream_get_meta_data($tmpFile)['uri']; |
| 610 |
fclose($tmpFile); |
| 611 |
$tmpFile = fopen($tmpFilePath, 'w+'); |
| 612 |
fwrite($tmpFile, $data); |
| 613 |
fclose($tmpFile); |
| 614 |
$out = []; |
| 615 |
$retVal = 0; |
| 616 |
$command = '/usr/bin/ssh-keygen -l -f ' . escapeshellarg($tmpFilePath); |
| 617 |
exec($command, $out, $retVal); |
| 618 |
unlink($tmpFilePath); |
| 619 |
if ($retVal !== 0) { |
| 620 |
logNewMessage(LOG_NOTICE, 'Invalid SSH key: ' . print_r($out, true)); |
| 621 |
$result = ['error' => $this->messages['file'][1][1]]; |
| 622 |
echo json_encode($result); |
| 623 |
return; |
| 624 |
} |
| 625 |
} |
| 626 |
$data = str_replace("\r\n", "\n", $data); |
| 627 |
$data = str_replace("\r", "\n", $data); |
| 628 |
$lines = explode("\n", $data); |
| 629 |
foreach ($lines as $line) { |
| 630 |
if (!empty($line) && !(str_starts_with($line, '#'))) { |
| 631 |
$_SESSION[self::SESS_KEY_LIST][] = $line; |
| 632 |
} |
| 633 |
} |
| 634 |
$_SESSION[self::SESS_KEY_LIST] = array_values(array_unique($_SESSION[self::SESS_KEY_LIST])); |
| 635 |
ob_start(); |
| 636 |
$contentElement = $this->getSelfServiceKeys(); |
| 637 |
ob_end_clean(); |
| 638 |
ob_start(); |
| 639 |
parseHtml(null, $contentElement, [], true); |
| 640 |
$content = ob_get_contents(); |
| 641 |
ob_end_clean(); |
| 642 |
$result['html'] = $content; |
| 643 |
} |
| 644 |
echo json_encode($result); |
| 645 |
} |
| 646 |
|
| 647 |
/** |
| 648 |
* Manages the deletion of a key. |
| 649 |
* |
| 650 |
* @param array<string, string> $data JSON data |
| 651 |
* @return array<string, string> error response |
| 652 |
*/ |
| 653 |
private function ajaxDeleteSelfServiceKey(array $data): array { |
| 654 |
if (!isset($data['id'])) { |
| 655 |
return self::invalidAjaxRequest(); |
| 656 |
} |
| 657 |
foreach ($_SESSION[self::SESS_KEY_LIST] as $index => $value) { |
| 658 |
if (isset($data['sshPublicKey_' . $index])) { |
| 659 |
$_SESSION[self::SESS_KEY_LIST][$index] = $data['sshPublicKey_' . $index]; |
| 660 |
} |
| 661 |
} |
| 662 |
$index = $data['id']; |
| 663 |
if (array_key_exists($index, $_SESSION[self::SESS_KEY_LIST])) { |
| 664 |
unset($_SESSION[self::SESS_KEY_LIST][$index]); |
| 665 |
$_SESSION[self::SESS_KEY_LIST] = array_values($_SESSION[self::SESS_KEY_LIST]); |
| 666 |
} |
| 667 |
ob_start(); |
| 668 |
$contentElement = $this->getSelfServiceKeys(); |
| 669 |
ob_end_clean(); |
| 670 |
ob_start(); |
| 671 |
parseHtml(null, $contentElement, [], true); |
| 672 |
$content = ob_get_contents(); |
| 673 |
ob_end_clean(); |
| 674 |
return [ |
| 675 |
'errorsOccurred' => 'false', |
| 676 |
'html' => $content, |
| 677 |
]; |
| 678 |
} |
| 679 |
|
| 680 |
/** |
| 681 |
* Invalid AJAX request received. |
| 682 |
* |
| 683 |
* @param string $message error message |
| 684 |
* @return array<string, string> error response |
| 685 |
*/ |
| 686 |
public static function invalidAjaxRequest($message = null): array { |
| 687 |
if ($message === null) { |
| 688 |
$message = _('Invalid request'); |
| 689 |
} |
| 690 |
return ['errorsOccurred' => 'true', 'errormessage' => $message]; |
| 691 |
} |
| 692 |
|
| 693 |
/** |
| 694 |
* @inheritDoc |
| 695 |
*/ |
| 696 |
public function getListAttributeDescriptions(ConfiguredType $type): array { |
| 697 |
return [ |
| 698 |
$this->getAttributeName() => _('SSH public keys') |
| 699 |
]; |
| 700 |
} |
| 701 |
|
| 702 |
} |