| 1 |
<?php |
| 2 |
|
| 3 |
use LAM\PDF\PDFTable; |
| 4 |
use LAM\PDF\PDFTableCell; |
| 5 |
use LAM\PDF\PDFTableRow; |
| 6 |
use LAM\TYPES\ConfiguredType; |
| 7 |
use LAM\TYPES\TypeManager; |
| 8 |
|
| 9 |
/* |
| 10 |
|
| 11 |
This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/) |
| 12 |
Copyright (C) 2015 - 2026 Roland Gruber |
| 13 |
|
| 14 |
This program is free software; you can redistribute it and/or modify |
| 15 |
it under the terms of the GNU General Public License as published by |
| 16 |
the Free Software Foundation; either version 2 of the License, or |
| 17 |
(at your option) any later version. |
| 18 |
|
| 19 |
This program is distributed in the hope that it will be useful, |
| 20 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 |
GNU General Public License for more details. |
| 23 |
|
| 24 |
You should have received a copy of the GNU General Public License |
| 25 |
along with this program; if not, write to the Free Software |
| 26 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 27 |
|
| 28 |
*/ |
| 29 |
|
| 30 |
/** |
| 31 |
* Manages memberships in NIS net groups. |
| 32 |
* |
| 33 |
* @package modules |
| 34 |
* @author Roland Gruber |
| 35 |
*/ |
| 36 |
|
| 37 |
/** |
| 38 |
* Manages memberships in NIS net groups. |
| 39 |
* |
| 40 |
* @package modules |
| 41 |
*/ |
| 42 |
class nisNetGroupUser extends baseModule { |
| 43 |
|
| 44 |
/** @var array<array<string, string|string[]>> list of NIS netgroups the user is memberOf (array(array(name => '', dn => '', host => '', domain => ''))) */ |
| 45 |
protected $groups = []; |
| 46 |
/** @var array<array<string, string|string[]>> list of NIS netgroups the user was memberOf (array(array(name => '', dn => '', host => '', domain => ''))) */ |
| 47 |
protected $groupsOrig = []; |
| 48 |
/** @var array<string, string[]|string>[]|null $groupCache group cache (array(array(cn => '', dn => '', nisnetgrouptriple => []))) */ |
| 49 |
protected $groupCache; |
| 50 |
/** original uid */ |
| 51 |
protected ?string $uidOrig = null; |
| 52 |
|
| 53 |
/** |
| 54 |
* {@inheritDoc} |
| 55 |
*/ |
| 56 |
public function can_manage(): bool { |
| 57 |
return $this->get_scope() === 'user'; |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* {@inheritDoc} |
| 62 |
*/ |
| 63 |
public function get_metaData(): array { |
| 64 |
$return = []; |
| 65 |
// icon |
| 66 |
$return['icon'] = 'group.svg'; |
| 67 |
// module dependencies |
| 68 |
$return['dependencies'] = ['depends' => [['posixAccount', 'inetOrgPerson']], 'conflicts' => []]; |
| 69 |
// alias name |
| 70 |
$return["alias"] = _("NIS net groups"); |
| 71 |
// available PDF fields |
| 72 |
$return['PDF_fields']['memberships'] = _('NIS net groups'); |
| 73 |
// help Entries |
| 74 |
$return['help'] = [ |
| 75 |
'memberships_upload' => [ |
| 76 |
"Headline" => _('NIS net groups'), |
| 77 |
"Text" => _("Here you can enter a list of net groups. Group blocks are separated by comma in format GROUP#HOST#DOMAIN. Host and domain are optional.") |
| 78 |
], |
| 79 |
]; |
| 80 |
// upload columns |
| 81 |
$return['upload_columns'][] = [ |
| 82 |
'name' => 'nisNetGroup_memberships', |
| 83 |
'description' => _('Memberships'), |
| 84 |
'help' => 'memberships_upload', |
| 85 |
'example' => 'group1#host#domain,group2#host#domain' |
| 86 |
]; |
| 87 |
return $return; |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* {@inheritDoc} |
| 92 |
*/ |
| 93 |
protected function load_Messages(): void { |
| 94 |
$this->messages['host'][0] = ['ERROR', _('Host name'), _('Host name contains invalid characters. Valid characters are: a-z, A-Z, 0-9 and .-_ !')]; |
| 95 |
$this->messages['domain'][0] = ['ERROR', _('Domain name'), _('Domain name is invalid!')]; |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* {@inheritDoc} |
| 100 |
*/ |
| 101 |
public function init(string $base): void { |
| 102 |
// call parent init |
| 103 |
parent::init($base); |
| 104 |
$this->groups = []; |
| 105 |
$this->groupsOrig = []; |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* {@inheritDoc} |
| 110 |
*/ |
| 111 |
public function load_attributes(array $attributes): void { |
| 112 |
parent::load_attributes($attributes); |
| 113 |
if (empty($attributes['uid'][0])) { |
| 114 |
return; |
| 115 |
} |
| 116 |
$this->uidOrig = $attributes['uid'][0]; |
| 117 |
$typeManager = new TypeManager(); |
| 118 |
$types = $typeManager->getConfiguredTypesForScope('netgroup'); |
| 119 |
$groupList = []; |
| 120 |
foreach ($types as $type) { |
| 121 |
$filter = '(&(objectClass=nisNetgroup)(nisnetgrouptriple=*))'; |
| 122 |
$typeFilter = $type->getAdditionalLdapFilter(); |
| 123 |
if (!empty($typeFilter)) { |
| 124 |
if (!str_starts_with($typeFilter, '(')) { |
| 125 |
$typeFilter = '(' . $typeFilter . ')'; |
| 126 |
} |
| 127 |
$filter = '(&' . $filter . $typeFilter . ')'; |
| 128 |
} |
| 129 |
$groupsFound = searchLDAP($type->getSuffix(), $filter, ['dn', 'cn', 'nisnetgrouptriple']); |
| 130 |
$groupList = array_merge($groupList, $groupsFound); |
| 131 |
} |
| 132 |
$this->groupsOrig = []; |
| 133 |
$tripleRegex = '/^\\(([^,]*),([^,]*),([^,]*)\\)$/'; |
| 134 |
foreach ($groupList as $group) { |
| 135 |
foreach ($group['nisnetgrouptriple'] as $triple) { |
| 136 |
$matches = []; |
| 137 |
if (preg_match($tripleRegex, $triple, $matches) == 0) { |
| 138 |
continue; |
| 139 |
} |
| 140 |
$host = $matches[1]; |
| 141 |
$user = $matches[2]; |
| 142 |
$domain = $matches[3]; |
| 143 |
if ($this->isMatchingNetGroup($user, $host, $domain, $this->uidOrig)) { |
| 144 |
$this->groupsOrig[] = [ |
| 145 |
'name' => $group['cn'][0], |
| 146 |
'dn' => $group['dn'], |
| 147 |
'host' => $host, |
| 148 |
'user' => $user, |
| 149 |
'domain' => $domain |
| 150 |
]; |
| 151 |
} |
| 152 |
} |
| 153 |
} |
| 154 |
usort($this->groupsOrig, $this->sortTriple(...)); |
| 155 |
$this->groups = $this->groupsOrig; |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Checks if the netgroup matches this entry. |
| 160 |
* |
| 161 |
* @param string $user netgroup user name |
| 162 |
* @param string $host netgroup host name |
| 163 |
* @param string $domain netgroup domain name |
| 164 |
* @param string $uid username of this entry |
| 165 |
* @return bool matching |
| 166 |
*/ |
| 167 |
protected function isMatchingNetGroup($user, $host, $domain, $uid): bool { |
| 168 |
return $user == $uid; |
| 169 |
} |
| 170 |
|
| 171 |
/** |
| 172 |
* {@inheritDoc} |
| 173 |
*/ |
| 174 |
public function display_html_attributes(): htmlElement|array { |
| 175 |
$return = new htmlTable(); |
| 176 |
$return->addElement(new htmlOutputText(_('Group'))); |
| 177 |
$return->addElement(new htmlOutputText(_('Host name'))); |
| 178 |
$return->addElement(new htmlOutputText(_('Domain name')), true); |
| 179 |
for ($i = 0; $i < count($this->groups); $i++) { |
| 180 |
$group = $this->groups[$i]; |
| 181 |
$return->addElement(new htmlOutputText($group['name'])); |
| 182 |
$return->addElement(new htmlInputField('host_' . $i, $group['host'])); |
| 183 |
$return->addElement(new htmlInputField('domain_' . $i, $group['domain'])); |
| 184 |
$delButton = new htmlButton('del_' . $i, 'del.svg', true); |
| 185 |
$delButton->setTitle(_('Delete')); |
| 186 |
$return->addElement($delButton, true); |
| 187 |
} |
| 188 |
$return->addVerticalSpace('40px'); |
| 189 |
|
| 190 |
// new entry |
| 191 |
$groupList = []; |
| 192 |
$groupData = $this->findGroups(); |
| 193 |
if (count($groupData) > 0) { |
| 194 |
$filterGroup = new htmlGroup(); |
| 195 |
$filterGroup->addElement(new htmlOutputText(_('Filter') . ' ')); |
| 196 |
$filter = new htmlInputField('group_filter'); |
| 197 |
$filter->setFieldSize(5); |
| 198 |
$filter->filterSelectBox('group_add'); |
| 199 |
$filterGroup->addElement($filter); |
| 200 |
$return->addElement($filterGroup, true); |
| 201 |
|
| 202 |
foreach ($groupData as $group) { |
| 203 |
$groupList[$group['cn'][0]] = $group['cn'][0] . '#+#' . $group['dn']; |
| 204 |
} |
| 205 |
$groupSelect = new htmlSelect('group_add', $groupList); |
| 206 |
$groupSelect->setHasDescriptiveElements(true); |
| 207 |
$return->addElement($groupSelect); |
| 208 |
$return->addElement(new htmlInputField('host_add')); |
| 209 |
$return->addElement(new htmlInputField('domain_add')); |
| 210 |
$addButton = new htmlButton('addGroup', 'add.svg', true); |
| 211 |
$addButton->setTitle(_('Add')); |
| 212 |
$return->addElement($addButton, true); |
| 213 |
} |
| 214 |
return $return; |
| 215 |
} |
| 216 |
|
| 217 |
/** |
| 218 |
* {@inheritDoc} |
| 219 |
*/ |
| 220 |
public function process_attributes(): array { |
| 221 |
$errors = []; |
| 222 |
// add new entry |
| 223 |
if (isset($_POST['addGroup'])) { |
| 224 |
$parts = explode('#+#', $_POST['group_add']); |
| 225 |
$this->groups[] = [ |
| 226 |
'name' => $parts[0], |
| 227 |
'dn' => $parts[1], |
| 228 |
'host' => $_POST['host_add'], |
| 229 |
'user' => $this->uidOrig, |
| 230 |
'domain' => $_POST['domain_add'] |
| 231 |
]; |
| 232 |
if (!empty($_POST['host_add']) && !get_preg($_POST['host_add'], 'DNSname')) { |
| 233 |
$message = $this->messages['host'][0]; |
| 234 |
$message[2] = $message[2] . '<br><br>' . $_POST['host_add']; |
| 235 |
$errors[] = $message; |
| 236 |
} |
| 237 |
if (!empty($_POST['domain_add']) && !get_preg($_POST['domain_add'], 'DNSname')) { |
| 238 |
$message = $this->messages['domain'][0]; |
| 239 |
$message[2] = $message[2] . '<br><br>' . $_POST['domain_add']; |
| 240 |
$errors[] = $message; |
| 241 |
} |
| 242 |
} |
| 243 |
// check existing |
| 244 |
$counter = 0; |
| 245 |
while (isset($_POST['host_' . $counter])) { |
| 246 |
if (isset($_POST['del_' . $counter])) { |
| 247 |
unset($this->groups[$counter]); |
| 248 |
} |
| 249 |
else { |
| 250 |
$this->groups[$counter]['host'] = $_POST['host_' . $counter]; |
| 251 |
if (!empty($_POST['host_' . $counter]) && !get_preg($_POST['host_' . $counter], 'DNSname')) { |
| 252 |
$message = $this->messages['host'][0]; |
| 253 |
$message[2] = $message[2] . '<br><br>' . $_POST['host_' . $counter]; |
| 254 |
$errors[] = $message; |
| 255 |
} |
| 256 |
$this->groups[$counter]['domain'] = $_POST['domain_' . $counter]; |
| 257 |
if (!empty($_POST['domain_' . $counter]) && !get_preg($_POST['domain_' . $counter], 'DNSname')) { |
| 258 |
$message = $this->messages['domain'][0]; |
| 259 |
$message[2] = $message[2] . '<br><br>' . $_POST['domain_' . $counter]; |
| 260 |
$errors[] = $message; |
| 261 |
} |
| 262 |
} |
| 263 |
$counter++; |
| 264 |
} |
| 265 |
$this->groups = array_values($this->groups); |
| 266 |
usort($this->groups, $this->sortTriple(...)); |
| 267 |
return $errors; |
| 268 |
} |
| 269 |
|
| 270 |
/** |
| 271 |
* Returns the user ID for this user. |
| 272 |
* |
| 273 |
* @return string|null user ID |
| 274 |
*/ |
| 275 |
protected function getUid(): ?string { |
| 276 |
if ($this->getAccountContainer()->getAccountModule('posixAccount') != null) { |
| 277 |
$moduleAttributes = $this->getAccountContainer()->getAccountModule('posixAccount')->getAttributes(); |
| 278 |
} |
| 279 |
else { |
| 280 |
$moduleAttributes = $this->getAccountContainer()->getAccountModule('inetOrgPerson')->getAttributes(); |
| 281 |
} |
| 282 |
if (empty($moduleAttributes['uid'][0])) { |
| 283 |
return null; |
| 284 |
} |
| 285 |
return $moduleAttributes['uid'][0]; |
| 286 |
} |
| 287 |
|
| 288 |
/** |
| 289 |
* {@inheritDoc} |
| 290 |
*/ |
| 291 |
public function postModifyActions(bool $newAccount, array $attributes): array { |
| 292 |
$accountContainer = $this->getAccountContainer(); |
| 293 |
if (empty($accountContainer)) { |
| 294 |
return []; |
| 295 |
} |
| 296 |
$uid = $this->getUid(); |
| 297 |
if (empty($uid)) { |
| 298 |
return []; |
| 299 |
} |
| 300 |
$messages = []; |
| 301 |
// calculate differences |
| 302 |
$toRem = $this->groupsOrig; |
| 303 |
$toAdd = $this->groups; |
| 304 |
$counter = count($toRem); |
| 305 |
for ($i = 0; $i < $counter; $i++) { |
| 306 |
$group_orig = $toRem[$i]; |
| 307 |
foreach ($toAdd as $k => $group) { |
| 308 |
if (($group_orig['dn'] == $group['dn']) |
| 309 |
&& ($group_orig['domain'] == $group['domain']) |
| 310 |
&& ($group_orig['user'] == $group['user']) |
| 311 |
&& ($group_orig['host'] == $group['host'])) { |
| 312 |
if (empty($this->uidOrig) || ($this->uidOrig === $uid)) { |
| 313 |
// do not touch existing memberships |
| 314 |
unset($toRem[$i]); |
| 315 |
unset($toAdd[$k]); |
| 316 |
} |
| 317 |
break; |
| 318 |
} |
| 319 |
} |
| 320 |
} |
| 321 |
// group by DN |
| 322 |
$changes = []; |
| 323 |
foreach ($toAdd as $add) { |
| 324 |
$changes[$add['dn']]['add'][] = $this->createNetGroupValue($add, $uid); |
| 325 |
} |
| 326 |
foreach ($toRem as $del) { |
| 327 |
$delUid = empty($this->uidOrig) ? $uid : $this->uidOrig; |
| 328 |
$changes[$del['dn']]['del'][] = $this->createNetGroupValue($del, $delUid); |
| 329 |
} |
| 330 |
// update groups |
| 331 |
foreach ($changes as $dn => $changeSet) { |
| 332 |
$current = ldapGetDN($dn, ['nisnetgrouptriple']); |
| 333 |
if (empty($current)) { |
| 334 |
$messages[] = ['ERROR', sprintf(_('Was unable to modify attributes of DN: %s.'), $dn)]; |
| 335 |
continue; |
| 336 |
} |
| 337 |
$triples = empty($current['nisnetgrouptriple']) ? [] : $current['nisnetgrouptriple']; |
| 338 |
if (isset($changeSet['del'][0])) { |
| 339 |
$triples = array_delete($changeSet['del'], $triples); |
| 340 |
} |
| 341 |
if (isset($changeSet['add'][0])) { |
| 342 |
$triples = array_merge($changeSet['add'], $triples); |
| 343 |
} |
| 344 |
$triples = array_values(array_unique($triples)); |
| 345 |
$attributes = [ |
| 346 |
'nisnetgrouptriple' => $triples |
| 347 |
]; |
| 348 |
logNewMessage(LOG_DEBUG, 'Updating ' . $dn . ' with ' . print_r($attributes, true)); |
| 349 |
$success = @ldap_mod_replace($_SESSION['ldap']->server(), $dn, $attributes); |
| 350 |
if (!$success) { |
| 351 |
logNewMessage(LOG_ERR, 'Unable to modify attributes of DN: ' . $dn . ' (' . ldap_error($_SESSION['ldap']->server()) . ').'); |
| 352 |
$messages[] = ['ERROR', sprintf(_('Was unable to modify attributes of DN: %s.'), $dn), getDefaultLDAPErrorString($_SESSION['ldap']->server())]; |
| 353 |
} |
| 354 |
} |
| 355 |
return $messages; |
| 356 |
} |
| 357 |
|
| 358 |
/** |
| 359 |
* Creates a netgroup triple from a group object. |
| 360 |
* |
| 361 |
* @param array<string, string> $group group object |
| 362 |
* @param string $uid own uid |
| 363 |
* @return string value |
| 364 |
*/ |
| 365 |
protected function createNetGroupValue(array $group, string $uid): string { |
| 366 |
return '(' . $group['host'] . ',' . $uid . ',' . $group['domain'] . ')'; |
| 367 |
} |
| 368 |
|
| 369 |
/** |
| 370 |
* {@inheritDoc} |
| 371 |
*/ |
| 372 |
public function delete_attributes(): array { |
| 373 |
$uid = $this->getUid(); |
| 374 |
if (empty($uid)) { |
| 375 |
return []; |
| 376 |
} |
| 377 |
$return = []; |
| 378 |
// remove from NIS netgroups |
| 379 |
$changes = []; |
| 380 |
foreach ($this->groups as $group) { |
| 381 |
$changes[$group['dn']][] = $this->createNetGroupValue($group, $uid); |
| 382 |
} |
| 383 |
foreach ($changes as $dn => $changeSet) { |
| 384 |
$current = ldapGetDN($dn, ['nisnetgrouptriple']); |
| 385 |
if (empty($current)) { |
| 386 |
$return[$this->getAccountContainer()->dn_orig]['errors'][] = ['ERROR', sprintf(_('Was unable to modify attributes of DN: %s.'), $dn)]; |
| 387 |
continue; |
| 388 |
} |
| 389 |
$triples = empty($current['nisnetgrouptriple']) ? [] : $current['nisnetgrouptriple']; |
| 390 |
$triples = array_delete($changeSet, $triples); |
| 391 |
$triples = array_values(array_unique($triples)); |
| 392 |
$attributes = [ |
| 393 |
'nisnetgrouptriple' => $triples |
| 394 |
]; |
| 395 |
$success = @ldap_mod_replace($_SESSION['ldap']->server(), $dn, $attributes); |
| 396 |
if (!$success) { |
| 397 |
logNewMessage(LOG_ERR, 'Unable to modify attributes of DN: ' . $dn . ' (' . ldap_error($_SESSION['ldap']->server()) . ').'); |
| 398 |
$return[$this->getAccountContainer()->dn_orig]['errors'][] = ['ERROR', sprintf(_('Was unable to modify attributes of DN: %s.'), $dn), getDefaultLDAPErrorString($_SESSION['ldap']->server())]; |
| 399 |
} |
| 400 |
} |
| 401 |
return $return; |
| 402 |
} |
| 403 |
|
| 404 |
/** |
| 405 |
* {@inheritDoc} |
| 406 |
*/ |
| 407 |
public function get_profileOptions(string $typeId): ?htmlResponsiveRow { |
| 408 |
$groups = $this->findGroups(); |
| 409 |
$groupOptions = ['' => '']; |
| 410 |
foreach ($groups as $group) { |
| 411 |
$groupOptions[$group['cn'][0]] = $group['cn'][0] . '#+#' . $group['dn']; |
| 412 |
} |
| 413 |
$return = new htmlResponsiveRow(); |
| 414 |
$labels = [_('Group'), _('Host name'), _('Domain name')]; |
| 415 |
$data = []; |
| 416 |
for ($i = 0; $i < 5; $i++) { |
| 417 |
$select = new htmlSelect('nisNetGroupUser_group' . $i, $groupOptions, ['']); |
| 418 |
$select->setHasDescriptiveElements(true); |
| 419 |
$data[$i][] = $select; |
| 420 |
$data[$i][] = new htmlInputField('nisNetGroupUser_host' . $i); |
| 421 |
$data[$i][] = new htmlInputField('nisNetGroupUser_domain' . $i); |
| 422 |
} |
| 423 |
$return->add(new htmlResponsiveTable($labels, $data)); |
| 424 |
return $return; |
| 425 |
} |
| 426 |
|
| 427 |
/** |
| 428 |
* {@inheritDoc} |
| 429 |
*/ |
| 430 |
public function load_profile(array $profile): void { |
| 431 |
for ($i = 0; $i < 5; $i++) { |
| 432 |
if (!empty($profile['nisNetGroupUser_group' . $i][0])) { |
| 433 |
$parts = explode('#+#', $profile['nisNetGroupUser_group' . $i][0]); |
| 434 |
$this->groups[] = [ |
| 435 |
'name' => $parts[0], |
| 436 |
'dn' => $parts[1], |
| 437 |
'host' => $profile['nisNetGroupUser_host' . $i][0], |
| 438 |
'user' => $this->uidOrig, |
| 439 |
'domain' => $profile['nisNetGroupUser_domain' . $i][0], |
| 440 |
]; |
| 441 |
} |
| 442 |
} |
| 443 |
usort($this->groups, $this->sortTriple(...)); |
| 444 |
} |
| 445 |
|
| 446 |
/** |
| 447 |
* {@inheritDoc} |
| 448 |
*/ |
| 449 |
public function get_pdfEntries(array $pdfKeys, string $typeId): array { |
| 450 |
$return = []; |
| 451 |
$pdfTable = new PDFTable(); |
| 452 |
$pdfRow = new PDFTableRow(); |
| 453 |
$pdfRow->cells[] = new PDFTableCell(_('Group'), '25%', null, true); |
| 454 |
$pdfRow->cells[] = new PDFTableCell(_('Host name'), '25%', null, true); |
| 455 |
$pdfRow->cells[] = new PDFTableCell(_('Domain name'), '25%', null, true); |
| 456 |
$pdfTable->rows[] = $pdfRow; |
| 457 |
foreach ($this->groups as $group) { |
| 458 |
$pdfRow = new PDFTableRow(); |
| 459 |
$pdfRow->cells[] = new PDFTableCell($group['name'], '25%'); |
| 460 |
$pdfRow->cells[] = new PDFTableCell($group['host'], '25%'); |
| 461 |
$pdfRow->cells[] = new PDFTableCell($group['domain'], '25%'); |
| 462 |
$pdfTable->rows[] = $pdfRow; |
| 463 |
} |
| 464 |
$this->addPDFTable($return, 'memberships', $pdfTable); |
| 465 |
return $return; |
| 466 |
} |
| 467 |
|
| 468 |
/** |
| 469 |
* {@inheritDoc} |
| 470 |
*/ |
| 471 |
public function build_uploadAccounts(array $rawAccounts, array $ids, array &$partialAccounts, array $selectedModules, ConfiguredType &$type): array { |
| 472 |
$errors = []; |
| 473 |
// get list of existing group of names |
| 474 |
$groups = $this->findGroups(); |
| 475 |
$groupNames = []; |
| 476 |
foreach ($groups as $group) { |
| 477 |
$groupNames[] = $group['cn'][0]; |
| 478 |
} |
| 479 |
// check input |
| 480 |
for ($i = 0; $i < count($rawAccounts); $i++) { |
| 481 |
// group names |
| 482 |
if (!empty($rawAccounts[$i][$ids['nisNetGroup_memberships']])) { |
| 483 |
$triples = preg_split('/,[ ]*/', $rawAccounts[$i][$ids['nisNetGroup_memberships']]); |
| 484 |
foreach ($triples as $triple) { |
| 485 |
$parts = explode('#', $triple); |
| 486 |
if (!in_array($parts[0], $groupNames)) { |
| 487 |
$errors[] = ['ERROR', _('Unable to find group in LDAP.'), $parts[0]]; |
| 488 |
} |
| 489 |
} |
| 490 |
} |
| 491 |
} |
| 492 |
return $errors; |
| 493 |
} |
| 494 |
|
| 495 |
/** |
| 496 |
* {@inheritDoc} |
| 497 |
*/ |
| 498 |
public function doUploadPostActions(array &$data, array $ids, array $failed, array &$temp, array &$accounts, |
| 499 |
array $selectedModules, ConfiguredType $type): array { |
| 500 |
if (!checkIfWriteAccessIsAllowed($this->get_scope())) { |
| 501 |
die(); |
| 502 |
} |
| 503 |
// on first call generate list of LDAP operations |
| 504 |
if (!isset($temp['counter'])) { |
| 505 |
$temp['groups'] = []; |
| 506 |
$temp['counter'] = 0; |
| 507 |
// get list of existing groups |
| 508 |
$groupList = $this->findGroups(); |
| 509 |
$groupMap = []; |
| 510 |
foreach ($groupList as $group) { |
| 511 |
$groupMap[$group['cn'][0]] = $group['dn']; |
| 512 |
} |
| 513 |
for ($i = 0; $i < count($data); $i++) { |
| 514 |
if (in_array($i, $failed)) { |
| 515 |
continue; |
| 516 |
} // ignore failed accounts |
| 517 |
if (empty($accounts[$i]['uid'])) { |
| 518 |
continue; |
| 519 |
} |
| 520 |
$uid = $accounts[$i]['uid']; |
| 521 |
if (!empty($data[$i][$ids['nisNetGroup_memberships']])) { |
| 522 |
$triples = preg_split('/,[ ]*/', $data[$i][$ids['nisNetGroup_memberships']]); |
| 523 |
foreach ($triples as $triple) { |
| 524 |
$parts = explode('#', $triple); |
| 525 |
$group = $parts[0]; |
| 526 |
$temp['groups'][$groupMap[$group]][] = $this->buildNetGroupTripleFromUploadValue($parts, $uid); |
| 527 |
} |
| 528 |
} |
| 529 |
} |
| 530 |
$temp['groupDNs'] = array_keys($temp['groups']); |
| 531 |
return [ |
| 532 |
'status' => 'inProgress', |
| 533 |
'progress' => 0, |
| 534 |
'errors' => [] |
| 535 |
]; |
| 536 |
} |
| 537 |
// add users to groups |
| 538 |
elseif ($temp['counter'] < count($temp['groupDNs'])) { |
| 539 |
$errors = []; |
| 540 |
$dn = $temp['groupDNs'][$temp['counter']]; |
| 541 |
$current = ldapGetDN($dn, ['nisnetgrouptriple']); |
| 542 |
if (empty($current)) { |
| 543 |
$errors[] = ['ERROR', sprintf(_('Was unable to modify attributes of DN: %s.'), $dn)]; |
| 544 |
$temp['counter']++; |
| 545 |
return [ |
| 546 |
'status' => 'inProgress', |
| 547 |
'progress' => ($temp['counter'] * 100) / count($temp['groupDNs']), |
| 548 |
'errors' => $errors |
| 549 |
]; |
| 550 |
} |
| 551 |
$triples = empty($current['nisnetgrouptriple']) ? [] : $current['nisnetgrouptriple']; |
| 552 |
$triples = array_merge($temp['groups'][$dn], $triples); |
| 553 |
$triples = array_values(array_unique($triples)); |
| 554 |
$attributes = [ |
| 555 |
'nisnetgrouptriple' => $triples |
| 556 |
]; |
| 557 |
$success = @ldap_mod_replace($_SESSION['ldap']->server(), $dn, $attributes); |
| 558 |
if (!$success) { |
| 559 |
$errors[] = [ |
| 560 |
"ERROR", |
| 561 |
_("LAM was unable to modify group memberships for group: %s"), |
| 562 |
getDefaultLDAPErrorString($_SESSION['ldap']->server()), |
| 563 |
[$dn] |
| 564 |
]; |
| 565 |
} |
| 566 |
$temp['counter']++; |
| 567 |
return [ |
| 568 |
'status' => 'inProgress', |
| 569 |
'progress' => ($temp['counter'] * 100) / count($temp['groupDNs']), |
| 570 |
'errors' => $errors |
| 571 |
]; |
| 572 |
} |
| 573 |
// all modifications are done |
| 574 |
else { |
| 575 |
return [ |
| 576 |
'status' => 'finished', |
| 577 |
'progress' => 100, |
| 578 |
'errors' => [] |
| 579 |
]; |
| 580 |
} |
| 581 |
} |
| 582 |
|
| 583 |
/** |
| 584 |
* Creates a netgroup triple from the input value of file upload. |
| 585 |
* |
| 586 |
* @param string[] $value upload value (e.g. array(group1, host, domain)) |
| 587 |
* @param string $uid own uid |
| 588 |
* @return string netgroup triple |
| 589 |
*/ |
| 590 |
protected function buildNetGroupTripleFromUploadValue($value, $uid): string { |
| 591 |
$host = empty($value[1]) ? '' : $value[1]; |
| 592 |
$domain = empty($value[2]) ? '' : $value[2]; |
| 593 |
return '(' . $host . ',' . $uid . ',' . $domain . ')'; |
| 594 |
} |
| 595 |
|
| 596 |
/** |
| 597 |
* Finds all existing LDAP NIS net groups. |
| 598 |
* |
| 599 |
* @return array<string, string[]|string>[] groups array(array(cn => [], dn => '', nisnetgrouptriple => [])) |
| 600 |
*/ |
| 601 |
protected function findGroups() { |
| 602 |
if ($this->groupCache !== null) { |
| 603 |
return $this->groupCache; |
| 604 |
} |
| 605 |
$return = []; |
| 606 |
$typeManager = new TypeManager(); |
| 607 |
$types = $typeManager->getConfiguredTypesForScope('netgroup'); |
| 608 |
foreach ($types as $type) { |
| 609 |
$filter = '(objectClass=nisNetgroup)'; |
| 610 |
$typeFilter = $type->getAdditionalLdapFilter(); |
| 611 |
if (!empty($typeFilter)) { |
| 612 |
if (!str_starts_with($typeFilter, '(')) { |
| 613 |
$typeFilter = '(' . $typeFilter . ')'; |
| 614 |
} |
| 615 |
$filter = '(&' . $filter . $typeFilter . ')'; |
| 616 |
} |
| 617 |
$results = searchLDAP($type->getSuffix(), $filter, ['cn', 'dn', 'nisnetgrouptriple']); |
| 618 |
for ($i = 0; $i < count($results); $i++) { |
| 619 |
if (isset($results[$i]['cn'][0]) && isset($results[$i]['dn'])) { |
| 620 |
$return[] = $results[$i]; |
| 621 |
} |
| 622 |
} |
| 623 |
} |
| 624 |
$this->groupCache = $return; |
| 625 |
return $return; |
| 626 |
} |
| 627 |
|
| 628 |
/** |
| 629 |
* Sorts NIS netgroup triples by group, host, user and domain. |
| 630 |
* |
| 631 |
* @param array<string, string> $first first array |
| 632 |
* @param array<string, string> $second second array |
| 633 |
* @return int comparison result |
| 634 |
*/ |
| 635 |
protected function sortTriple(array $first, array $second): int { |
| 636 |
if ($first['name'] != $second['name']) { |
| 637 |
return strnatcasecmp($first['name'], $second['name']); |
| 638 |
} |
| 639 |
elseif ($first['host'] != $second['host']) { |
| 640 |
return strnatcasecmp($first['host'], $second['host']); |
| 641 |
} |
| 642 |
elseif ($first['user'] != $second['user']) { |
| 643 |
return strnatcasecmp($first['user'], $second['user']); |
| 644 |
} |
| 645 |
return strnatcasecmp($first['domain'], $second['domain']); |
| 646 |
} |
| 647 |
|
| 648 |
} |