Debian Codegraph @

lib/schema.inc

raw
1 <?php
2
3 namespace LAM\SCHEMA;
4 /*
5
6 Copyright (C) 2004 David Smith
7 modified to fit for LDAP Account Manager 2005 - 2026 Roland Gruber
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22
23 */
24
25 /**
26 * Classes and functions for fetching and parsing schema from an LDAP server.
27 *
28 * @package lib
29 *
30 * @author The phpLDAPadmin development team
31 * @author Roland Gruber
32 */
33
34 /**
35 * Generic parent class for all schema items. A schema item is
36 * an ObjectClass, an AttributeBype, a MatchingRule, or a Syntax.
37 * All schema items have at least two things in common: An OID
38 * and a description. This class provides an implementation for
39 * these two data.
40 *
41 * @package lib
42 */
43 class SchemaItem {
44 /** @var string|null $oid The OID of this schema item. */
45 protected $oid;
46 /** @var string|null $description The description of this schema item. */
47 protected $description;
48
49 /** Initialize class members to default values. */
50 protected function initVars(): void {
51 $this->oid = null;
52 $this->description = null;
53 }
54
55 /** Default constructor. */
56 public function __construct() {
57 $this->initVars();
58 }
59
60 /**
61 * @param string|null $new_oid oid
62 */
63 public function setOID($new_oid): void {
64 $this->oid = $new_oid;
65 }
66
67 /**
68 * @param string|null $new_desc description
69 */
70 public function setDescription($new_desc): void {
71 $this->description = $new_desc;
72 }
73
74 /**
75 * @return string|null oid
76 */
77 public function getOID() {
78 return $this->oid;
79 }
80
81 /**
82 * @return string|null description
83 */
84 public function getDescription() {
85 return $this->description;
86 }
87 }
88
89 /**
90 * Represents an LDAP objectClass
91 *
92 * @package lib
93 */
94 class ObjectClass extends SchemaItem {
95 /** @var string|null $name This objectClass' name, ie "inetOrgPerson" */
96 protected $name;
97 /** @var string[] $sup_classes array of objectClass names from which this objectClass inherits */
98 protected $sup_classes;
99 /** @var string|null $type one of STRUCTURAL, ABSTRACT, or AUXILIARY */
100 protected $type;
101 /** @var string[] $must_attrs arrays of attribute names that this objectClass requires */
102 protected $must_attrs;
103 /** @var string[] $may_attrs arrays of attribute names that this objectClass allows, but does not require */
104 protected $may_attrs;
105 /** @var bool $is_obsolete boolean value indicating whether this objectClass is obsolete */
106 protected $is_obsolete;
107 /** @var string[] $children_objectclasses array of objectClasses which inherit from this one (must be set at runtime explicitly by the caller) */
108 protected array $children_objectclasses = [];
109
110 /** Initialize the class' member variables */
111 protected function initVars(): void {
112 parent::initVars();
113 $this->oid = null;
114 $this->name = null;
115 $this->description = null;
116 $this->sup_classes = [];
117 $this->type = null;
118 $this->must_attrs = [];
119 $this->may_attrs = [];
120 $this->is_obsolete = false;
121 $this->children_objectclasses = [];
122 }
123
124 /**
125 * Creates a new ObjectClass object given a raw LDAP objectClass string.
126 *
127 * @param string $raw_ldap_schema_string schema string
128 */
129 public function __construct($raw_ldap_schema_string) {
130 $this->initVars();
131 $class = $raw_ldap_schema_string;
132 $class = preg_replace('/\\(([a-z])/i', '( $1', $class);
133 $class = preg_replace('/([a-z])\\)/i', '$1 )', $class);
134 $strings = preg_split('/[\s,]+/', $class, -1, PREG_SPLIT_DELIM_CAPTURE);
135 for ($i = 0; $i < count($strings); $i++) {
136 switch ($strings[$i]) {
137 case '(':
138 break;
139 case 'NAME':
140 if ($strings[$i + 1] !== "(") {
141 do {
142 $i++;
143 if (($this->name === null) || ((string) $this->name === '')) {
144 $this->name = $strings[$i];
145 }
146 else {
147 $this->name .= " " . $strings[$i];
148 }
149 }
150 while (!str_ends_with($strings[$i], '\''));
151 }
152 else {
153 $i++;
154 do {
155 $i++;
156 if (($this->name === null) || ((string) $this->name === '')) {
157 $this->name = $strings[$i];
158 }
159 else {
160 $this->name .= " " . $strings[$i];
161 }
162 }
163 while (!str_ends_with($strings[$i], '\''));
164 do {
165 $i++;
166 }
167 while (!preg_match('/\)+\)?/', $strings[$i]));
168 }
169 $this->name = preg_replace('/^\'/', "", $this->name);
170 $this->name = preg_replace('/\'$/', "", $this->name);
171 break;
172 case 'DESC':
173 do {
174 $i++;
175 if (($this->description === null) || ((string) $this->description === '')) {
176 $this->description = $strings[$i];
177 }
178 else {
179 $this->description = $this->description . " " . $strings[$i];
180 }
181 }
182 while (!str_ends_with($strings[$i], '\''));
183 break;
184 case 'OBSOLETE':
185 $this->is_obsolete = TRUE;
186 break;
187 case 'SUP':
188 if ($strings[$i + 1] !== "(") {
189 $i++;
190 $this->sup_classes[] = preg_replace("/'/", "", $strings[$i]);
191 }
192 else {
193 $i++;
194 do {
195 $i++;
196 if ($strings[$i] !== "$") {
197 $this->sup_classes[] = preg_replace("/'/", "", $strings[$i]);
198 }
199 }
200 while (!preg_match('/\)+\)?/', $strings[$i + 1]));
201 }
202 break;
203 case 'ABSTRACT':
204 $this->type = 'abstract';
205 break;
206 case 'STRUCTURAL':
207 $this->type = 'structural';
208 break;
209 case 'AUXILIARY':
210 $this->type = 'auxiliary';
211 break;
212 case 'MUST':
213 if (preg_match('/^\(./', $strings[$i + 1])) {
214 $i++;
215 $attr = new ObjectClassAttribute(preg_replace('/^\(/', "", $strings[$i]), $this->name);
216 $this->must_attrs[] = $attr;
217 do {
218 $i++;
219 if ($strings[$i] !== "$") {
220 $attr = new ObjectClassAttribute($strings[$i], $this->name);
221 $this->must_attrs[] = $attr;
222 }
223 }
224 while (!preg_match('/\)+\)?/', $strings[$i + 1]));
225 }
226 elseif ($strings[$i + 1] !== "(") {
227 $i++;
228 $attr = new ObjectClassAttribute($strings[$i], $this->name);
229 $this->must_attrs[] = $attr;
230 }
231 else {
232 $i++;
233 do {
234 $i++;
235 if ($strings[$i] !== "$") {
236 $attr = new ObjectClassAttribute($strings[$i], $this->name);
237 $this->must_attrs[] = $attr;
238 }
239 }
240 while (!preg_match('/\)+\)?/', $strings[$i + 1]));
241 }
242 sort($this->must_attrs);
243 break;
244 case 'MAY':
245 if (preg_match('/^\(./', $strings[$i + 1])) {
246 $i++;
247 $attr = new ObjectClassAttribute(preg_replace('/^\(/', "", $strings[$i]), $this->name);
248 $this->may_attrs[] = $attr;
249 do {
250 $i++;
251 if ($strings[$i] !== "$") {
252 $attr = new ObjectClassAttribute($strings[$i], $this->name);
253 $this->may_attrs[] = $attr;
254 }
255 }
256 while (!preg_match('/\)+\)?/', $strings[$i + 1]));
257 }
258 elseif ($strings[$i + 1] !== "(") {
259 $i++;
260 $attr = new ObjectClassAttribute($strings[$i], $this->name);
261 $this->may_attrs[] = $attr;
262 }
263 else {
264 $i++;
265 do {
266 $i++;
267 if ($strings[$i] !== "$") {
268 $attr = new ObjectClassAttribute($strings[$i], $this->name);
269 $this->may_attrs[] = $attr;
270 }
271 }
272 while (!preg_match('/\)+\)?/', $strings[$i + 1]));
273 }
274 sort($this->may_attrs);
275 break;
276 default:
277 if (preg_match('/[\d\.]+/i', $strings[$i]) && $i === 1) {
278 $this->oid = $strings[$i];
279 }
280 }
281 }
282
283 if ($this->description !== null) {
284 $this->description = preg_replace('/^\'/', "", $this->description);
285 $this->description = preg_replace('/\'$/', "", $this->description);
286 }
287 }
288
289 /**
290 * Gets an array of AttributeType objects that entries of this ObjectClass must define.
291 * This differs from getMustAttrNames in that it returns an array of AttributeType objects
292 *
293 * @param ObjectClass[] $oclasses An array of ObjectClass objects to use when traversing
294 * the inheritance tree. This presents some what of a bootstrapping problem
295 * as we must fetch all objectClasses to determine through inheritance which
296 * attributes this objectClass requires.
297 * @return AttributeType[] attributes
298 *
299 * @see getMustAttrNames
300 * @see getMayAttrs
301 * @see getMayAttrNames
302 */
303 public function getMustAttrs($oclasses = NULL) {
304 $all_must_attrs = $this->must_attrs;
305 foreach ($this->sup_classes as $sup_class) {
306 if ($oclasses != null
307 && $sup_class != "top"
308 && isset($oclasses[strtolower($sup_class)])) {
309 $sup_class = $oclasses[strtolower($sup_class)];
310 $sup_class_must_attrs = $sup_class->getMustAttrs($oclasses);
311 $all_must_attrs = array_merge($sup_class_must_attrs, $all_must_attrs);
312 }
313 }
314
315 ksort($all_must_attrs);
316 return $all_must_attrs;
317 }
318
319 /**
320 * Gets an array of AttributeType objects that entries of this ObjectClass may define.
321 * This differs from getMayAttrNames in that it returns an array of AttributeType objects
322 *
323 * @param ObjectClass[] $oclasses An array of ObjectClass objects to use when traversing
324 * the inheritance tree. This presents some what of a bootstrapping problem
325 * as we must fetch all objectClasses to determine through inheritance which
326 * attributes this objectClass provides.
327 * @return AttributeType[] attributes
328 *
329 * @see getMustAttrNames
330 * @see getMustAttrs
331 * @see getMayAttrNames
332 * @see AttributeType
333 */
334 public function getMayAttrs($oclasses = NULL) {
335 $all_may_attrs = $this->may_attrs;
336 foreach ($this->sup_classes as $sup_class_name) {
337 if ($oclasses != null
338 && $sup_class_name != "top"
339 && isset($oclasses[strtolower($sup_class_name)])) {
340 $sup_class = $oclasses[strtolower($sup_class_name)];
341 $sup_class_may_attrs = $sup_class->getMayAttrs($oclasses);
342 $all_may_attrs = array_merge($sup_class_may_attrs, $all_may_attrs);
343 }
344 }
345
346 ksort($all_may_attrs);
347 return $all_may_attrs;
348 }
349
350 /**
351 * Gets an array of attribute names (strings) that entries of this ObjectClass must define.
352 * This differs from getMustAttrs in that it returns an array of strings rather than
353 * array of AttributeType objects
354 *
355 * @param ObjectClass[] $oclasses An array of ObjectClass objects to use when traversing
356 * the inheritance tree. This presents some what of a bootstrapping problem
357 * as we must fetch all objectClasses to determine through inheritance which
358 * attributes this objectClass provides.
359 * @return string[] The array of allowed attribute names (strings).
360 *
361 * @see getMustAttrs
362 * @see getMayAttrs
363 * @see getMayAttrNames
364 */
365 public function getMustAttrNames($oclasses = null): array {
366 $attrs = $this->getMustAttrs($oclasses);
367 $attr_names = [];
368 foreach ($attrs as $attr)
369 $attr_names[] = $attr->getName();
370 return $attr_names;
371 }
372
373 /**
374 * Gets an array of attribute names (strings) that entries of this ObjectClass must define.
375 * This differs from getMayAttrs in that it returns an array of strings rather than
376 * array of AttributeType objects
377 *
378 * @param ObjectClass[] $oclasses An array of ObjectClass objects to use when traversing
379 * the inheritance tree. This presents some what of a bootstrapping problem
380 * as we must fetch all objectClasses to determine through inheritance which
381 * attributes this objectClass provides.
382 * @return string[] The array of allowed attribute names (strings).
383 *
384 * @see getMustAttrs
385 * @see getMayAttrs
386 * @see getMustAttrNames
387 */
388 public function getMayAttrNames($oclasses = null): array {
389 $attrs = $this->getMayAttrs($oclasses);
390 $attr_names = [];
391 foreach ($attrs as $attr)
392 $attr_names[] = $attr->getName();
393 return $attr_names;
394 }
395
396 /**
397 * Adds an objectClass to the list of objectClasses that inherit
398 * from this objectClass.
399 * @param string $object_class_name The name of the objectClass to add
400 * @return bool Returns true on success or false on failure (objectclass already existed for example)
401 */
402 public function addChildObjectClass($object_class_name) {
403 $object_class_name = trim($object_class_name);
404 foreach ($this->children_objectclasses as $existing_objectclass)
405 if (0 === strcasecmp($object_class_name, $existing_objectclass)) {
406 return false;
407 }
408 $this->children_objectclasses[] = $object_class_name;
409 return true;
410 }
411
412 /**
413 * Returns the array of objectClass names which inherit from this objectClass.
414 * @return string[] Names of objectClasses which inherit from this objectClass.
415 */
416 public function getChildObjectClasses() {
417 return $this->children_objectclasses;
418 }
419
420 /**
421 * Gets the name of this objectClass (ie, "inetOrgPerson")
422 * @return string The name of the objectClass
423 */
424 public function getName() {
425 return $this->name;
426 }
427
428 /**
429 * Gets the objectClass names from which this objectClass inherits.
430 *
431 * @return string[] objectClass names
432 */
433 public function getSupClasses(): array {
434 return $this->sup_classes;
435 }
436
437 /**
438 * Gets the type of this objectClass: STRUCTURAL, ABSTRACT, or AUXILIARY.
439 *
440 * @return string type
441 */
442 public function getType() {
443 return $this->type;
444 }
445
446 /**
447 * Gets whether this objectClass is flagged as obsolete by the LDAP server.
448 *
449 * @return bool obsolete
450 */
451 public function getIsObsolete() {
452 return $this->is_obsolete;
453 }
454
455 }
456
457 /**
458 * A simple class for representing AttributeTypes used only by the ObjectClass class.
459 * Users should never instantiate this class. It represents an attribute internal to
460 * an ObjectClass. If PHP supported inner-classes and variable permissions, this would
461 * be interior to class ObjectClass and flagged private. The reason this class is used
462 * and not the "real" class AttributeType is because this class supports the notion of
463 * a "source" objectClass, meaning that it keeps track of which objectClass originally
464 * specified it. This class is therefore used by the class ObjectClass to determine
465 * inheritance.
466 *
467 * @package lib
468 */
469 class ObjectClassAttribute {
470 /** @var string $name This Attribute's name */
471 protected $name;
472 /** @var string $source This Attribute's root */
473 protected $source;
474
475 /**
476 * Creates a new ObjectClassAttribute with specified name and source objectClass.
477 * @param string $name the name of the new attribute.
478 * @param string $source the name of the ObjectClass which
479 * specifies this attribute.
480 */
481 public function __construct($name, $source) {
482 $this->name = $name;
483 $this->source = $source;
484 }
485
486 /**
487 * Gets this attribute's name
488 *
489 * @return string name
490 */
491 public function getName() {
492 return $this->name;
493 }
494
495 /**
496 * Gets the name of the ObjectClass which originally specified this attribute.
497 * @return string source
498 */
499 public function getSource() {
500 return $this->source;
501 }
502 }
503
504
505 /**
506 * Represents an LDAP AttributeType
507 *
508 * @package lib
509 */
510 class AttributeType extends SchemaItem {
511 /** @var string|null $name The name of this attributeType */
512 protected $name;
513 /** @var bool $is_obsolete obsolete */
514 protected $is_obsolete;
515 /** @var string|null $sup_attribute The attribute from which this attribute inherits (if any) */
516 protected $sup_attribute;
517 /** @var string|null $equality The equality rule used */
518 protected $equality;
519 /** @var string|null $ordering The ordering of the attributeType */
520 protected $ordering;
521 /** @var string|null $sub_str substring matching */
522 protected $sub_str;
523 /** @var string|null $syntax The full syntax string, ie 1.2.3.4{16} */
524 protected $syntax;
525 /** @var string|null $syntax_oid syntax OID */
526 protected $syntax_oid;
527 /** @var bool|null $is_single_value boolean: is single valued only? */
528 protected $is_single_value;
529 /** @var bool $is_collective boolean: is collective? */
530 protected $is_collective;
531 /** @var bool $is_no_user_modification boolean: can use modify? */
532 protected $is_no_user_modification;
533 /** @var string|null $usage The usage string set by the LDAP schema */
534 protected $usage;
535 /** @var string[] $aliases An array of alias attribute names, strings */
536 protected $aliases;
537 /** @var string|null $max_length The max number of characters this attribute can be */
538 protected $max_length;
539 /** @var string|null $type A string description of the syntax type (taken from the LDAPSyntaxes) */
540 protected $type;
541 /** @var string[] $used_in_object_classes An array of objectClasses which use this attributeType (must be set by caller) */
542 protected $used_in_object_classes;
543 /** @var string[] $required_by_object_classes A list of object class names that require this attribute type. */
544 protected $required_by_object_classes = [];
545
546 /**
547 * Initialize the class' member variables
548 */
549 protected function initVars(): void {
550 parent::initVars();
551 $this->oid = null;
552 $this->name = null;
553 $this->description = null;
554 $this->is_obsolete = false;
555 $this->sup_attribute = null;
556 $this->equality = null;
557 $this->ordering = null;
558 $this->sub_str = null;
559 $this->syntax_oid = null;
560 $this->syntax = null;
561 $this->max_length = null;
562 $this->is_single_value = null;
563 $this->is_collective = false;
564 $this->is_no_user_modification = false;
565 $this->usage = null;
566 $this->aliases = [];
567 $this->type = null;
568 $this->used_in_object_classes = [];
569 $this->required_by_object_classes = [];
570 }
571
572 /**
573 * Creates a new AttributeType objcet from a raw LDAP AttributeType string.
574 * @param string $raw_ldap_attr_string attribute string
575 */
576 public function __construct($raw_ldap_attr_string) {
577 $this->initVars();
578 $attr = $raw_ldap_attr_string;
579 $strings = preg_split("/[\s,]+/", $attr, -1, PREG_SPLIT_DELIM_CAPTURE);
580 for ($i = 0; $i < count($strings); $i++) {
581 switch ($strings[$i]) {
582 case '(':
583 break;
584 case 'NAME':
585 if ($strings[$i + 1] !== "(") {
586 do {
587 $i++;
588 if (($this->name === null) || ((string) $this->name === '')) {
589 $this->name = $strings[$i];
590 }
591 else {
592 $this->name .= " " . $strings[$i];
593 }
594 }
595 while (!preg_match("/\'$/", $strings[$i]));
596 // this attribute has no aliases
597 $this->aliases = [];
598 }
599 else {
600 $i++;
601 do {
602 $i++;
603 if (empty($this->name)) {
604 $this->name = $strings[$i];
605 }
606 else {
607 $this->name .= " " . $strings[$i];
608 }
609 }
610 while (!preg_match("/\'$/", $strings[$i]));
611 // add alias names for this attribute
612 while ($strings[++$i] !== ")") {
613 $alias = $strings[$i];
614 $alias = preg_replace("/^\'/", "", $alias);
615 $alias = preg_replace("/\'$/", "", $alias);
616 $this->aliases[] = $alias;
617 }
618 }
619 break;
620 case 'DESC':
621 do {
622 $i++;
623 if (empty($this->description)) {
624 $this->description .= $strings[$i];
625 }
626 else {
627 $this->description = $this->description . " " . $strings[$i];
628 }
629 }
630 while (!preg_match("/\'$/", $strings[$i]));
631 break;
632 case 'OBSOLETE':
633 $this->is_obsolete = TRUE;
634 break;
635 case 'SUP':
636 $i++;
637 $this->sup_attribute = $strings[$i];
638 break;
639 case 'EQUALITY':
640 $i++;
641 $this->equality = $strings[$i];
642 break;
643 case 'ORDERING':
644 $i++;
645 $this->ordering = $strings[$i];
646 break;
647 case 'SUBSTR':
648 $i++;
649 $this->sub_str = $strings[$i];
650 break;
651 case 'SYNTAX':
652 $i++;
653 $this->syntax = $strings[$i];
654 $this->syntax_oid = preg_replace("/{\d+}$/", "", $this->syntax);
655 // does this SYNTAX string specify a max length (ie, 1.2.3.4{16})
656 $matches = [];
657 $this->max_length = preg_match("/{(\d+)}$/", $this->syntax, $matches) ? $matches[1] : null;
658 if ($i < count($strings) - 1 && $strings[$i + 1] === "{") {
659 do {
660 $i++;
661 $this->name .= " " . $strings[$i];
662 }
663 while ($strings[$i] !== "}");
664 }
665 break;
666 case 'SINGLE-VALUE':
667 $this->is_single_value = TRUE;
668 break;
669 case 'COLLECTIVE':
670 $this->is_collective = TRUE;
671 break;
672 case 'NO-USER-MODIFICATION':
673 $this->is_no_user_modification = TRUE;
674 break;
675 case 'USAGE':
676 $i++;
677 $this->usage = $strings[$i];
678 break;
679 default:
680 if (preg_match("/[\d\.]+/i", $strings[$i]) && $i === 1) {
681 $this->oid = $strings[$i];
682 }
683 }
684 }
685
686 if ($this->name !== null) {
687 $this->name = preg_replace("/^\'/", "", $this->name);
688 $this->name = preg_replace("/\'$/", "", $this->name);
689 }
690 if ($this->description !== null) {
691 $this->description = preg_replace("/^\'/", "", $this->description);
692 $this->description = preg_replace("/\'$/", "", $this->description);
693 }
694 if (!empty($this->syntax)) {
695 $this->syntax = preg_replace("/^\'/", "", $this->syntax);
696 $this->syntax = preg_replace("/\'$/", "", $this->syntax);
697 }
698 if (!empty($this->syntax_oid)) {
699 $this->syntax_oid = preg_replace("/^\'/", "", $this->syntax_oid);
700 $this->syntax_oid = preg_replace("/\'$/", "", $this->syntax_oid);
701 }
702 if ($this->sup_attribute !== null) {
703 $this->sup_attribute = preg_replace("/^\'/", "", $this->sup_attribute);
704 $this->sup_attribute = preg_replace("/\'$/", "", $this->sup_attribute);
705 }
706 }
707
708 /**
709 * Gets this attribute's name
710 * @return string
711 */
712 public function getName() {
713 return $this->name;
714 }
715
716 /**
717 * Gets whether this attribute has been flagged as obsolete by the LDAP server
718 * @return bool
719 */
720 public function getIsObsolete() {
721 return $this->is_obsolete;
722 }
723
724 /**
725 * Gets this attribute's usage string as defined by the LDAP server
726 * @return string
727 */
728 public function getUsage() {
729 return $this->usage;
730 }
731
732 /**
733 * Gets this attribute's parent attribute (if any). If this attribute does not
734 * inherit from another attribute, null is returned.
735 * @return string|null parent attribute name
736 */
737 public function getSupAttribute(): ?string {
738 return $this->sup_attribute;
739 }
740
741 /**
742 * Gets this attribute's equality string
743 * @return string
744 */
745 public function getEquality() {
746 return $this->equality;
747 }
748
749 /**
750 * Gets this attribute's ordering specification.
751 * @return string
752 */
753 public function getOrdering() {
754 return $this->ordering;
755 }
756
757 /**
758 * Gets this attribute's substring matching specification
759 * @return string
760 */
761 public function getSubstr() {
762 return $this->sub_str;
763 }
764
765 /**
766 * Gets the names of attributes that are an alias for this attribute (if any).
767 * @return string[] An array of names of attributes which alias this attribute or
768 * an empty array if no attribute aliases this object.
769 */
770 public function getAliases(): array {
771 return $this->aliases;
772 }
773
774 /**
775 * Returns whether the specified attribute is an alias for this one (based on this attribute's alias list).
776 * @param string $attr_name The name of the attribute to check.
777 * @return bool True if the specified attribute is an alias for this one, or false otherwise.
778 */
779 public function isAliasFor($attr_name) {
780 foreach ($this->aliases as $alias_attr_name)
781 if (0 === strcasecmp($alias_attr_name, $attr_name)) {
782 return true;
783 }
784 return false;
785
786 }
787
788 /**
789 * Gets this attribute's raw syntax string (ie: "1.2.3.4{16}").
790 * @return string The raw syntax string
791 */
792 public function getSyntaxString() {
793 return $this->syntax;
794 }
795
796 /**
797 * Gets this attribute's syntax OID. Differs from getSyntaxString() in that this
798 * function only returns the actual OID with any length specification removed.
799 * Ie, if the syntax string is "1.2.3.4{16}", this function only returns
800 * "1.2.3.4".
801 * @return string The syntax OID string.
802 */
803 public function getSyntaxOID() {
804 return $this->syntax_oid;
805 }
806
807 /**
808 * Gets this attribute's the maximum length. If no maximum is defined by the LDAP server, null is returned.
809 * @return string|null The maximum length (in characters) of this attribute or null if no maximum is specified.
810 */
811 public function getMaxLength() {
812 return $this->max_length;
813 }
814
815 /**
816 * Gets whether this attribute is single-valued. If this attribute only supports single values, true
817 * is returned. If this attribute supports multiple values, false is returned.
818 * @return bool Returns true if this attribute is single-valued or false otherwise.
819 */
820 public function getIsSingleValue() {
821 return $this->is_single_value;
822 }
823
824 /**
825 * Sets whether this attribute is single-valued.
826 * @param bool $is_single_value
827 */
828 public function setIsSingleValue($is_single_value): void {
829 $this->is_single_value = $is_single_value;
830 }
831
832 /**
833 * Gets whether this attribute is collective.
834 * @return bool Returns true if this attribute is collective and false otherwise.
835 */
836 public function getIsCollective() {
837 return $this->is_collective;
838 }
839
840 /**
841 * Gets whether this attribute is not modifiable by users.
842 * @return bool Returns true if this attribute is not modifiable by users.
843 */
844 public function getIsNoUserModification() {
845 return $this->is_no_user_modification;
846 }
847
848 /**
849 * Gets this attribute's type
850 * @return string The attribute's type.
851 */
852 public function getType() {
853 return $this->type;
854 }
855
856 /**
857 * Removes an attribute name from this attribute's alias array.
858 * @param string $remove_alias_name The name of the attribute to remove.
859 * @return bool true on success or false on failure (ie, if the specified
860 * attribute name is not found in this attribute's list of aliases)
861 */
862 public function removeAlias($remove_alias_name) {
863 foreach ($this->aliases as $i => $alias_name) {
864 if (0 === strcasecmp($alias_name, $remove_alias_name)) {
865 unset($this->aliases[$i]);
866 $this->aliases = array_values($this->aliases);
867 return true;
868 }
869 }
870 return false;
871 }
872
873 /**
874 * Adds an attribute name to the alias array.
875 * @param string $new_alias_name The name of a new attribute to add to this attribute's list of aliases.
876 */
877 public function addAlias($new_alias_name): void {
878 $this->aliases[] = $new_alias_name;
879 }
880
881 /**
882 * Sets this attriute's name.
883 * @param string $new_name The new name to give this attribute.
884 */
885 public function setName($new_name): void {
886 $this->name = $new_name;
887 }
888
889 /**
890 * Sets this attriute's SUP attribute (ie, the attribute from which this attribute inherits).
891 * @param string $new_sup_attr The name of the new parent (SUP) attribute
892 */
893 public function setSupAttribute($new_sup_attr): void {
894 $this->sup_attribute = $new_sup_attr;
895 }
896
897 /**
898 * Sets this attribute's list of aliases.
899 * @param string[] $new_aliases The array of alias names (strings)
900 */
901 public function setAliases($new_aliases): void {
902 $this->aliases = $new_aliases;
903 }
904
905 /**
906 * Sets this attribute's type.
907 * @param string $new_type The new type.
908 */
909 public function setType($new_type): void {
910 $this->type = $new_type;
911 }
912
913 /**
914 * Adds an objectClass name to this attribute's list of "used in" objectClasses,
915 * that is the list of objectClasses which provide this attribute.
916 * @param string $object_class_name The name of the objectClass to add.
917 * @return bool false if already added
918 */
919 public function addUsedInObjectClass($object_class_name) {
920 foreach ($this->used_in_object_classes as $used_in_object_class)
921 if (0 === strcasecmp($used_in_object_class, $object_class_name)) {
922 return false;
923 }
924 $this->used_in_object_classes[] = $object_class_name;
925 return true;
926 }
927
928 /**
929 * Gets the list of "used in" objectClasses, that is the list of objectClasses
930 * which provide this attribute.
931 * @return string[] An array of names of objectclasses (strings) which provide this attribute
932 */
933 public function getUsedInObjectClasses(): array {
934 return $this->used_in_object_classes;
935 }
936
937 /**
938 * Adds an objectClass name to this attribute's list of "required by" objectClasses,
939 * that is the list of objectClasses which must have this attribute.
940 * @param string $object_class_name The name of the objectClass to add.
941 * @return bool false if already added
942 */
943 public function addRequiredByObjectClass($object_class_name) {
944 foreach ($this->required_by_object_classes as $required_by_object_class)
945 if (0 === strcasecmp($required_by_object_class, $object_class_name)) {
946 return false;
947 }
948 $this->required_by_object_classes[] = $object_class_name;
949 return true;
950 }
951
952 /**
953 * Gets the list of "required by" objectClasses, that is the list of objectClasses
954 * which provide must have attribute.
955 * @return string[] An array of names of objectclasses (strings) which provide this attribute
956 */
957 public function getRequiredByObjectClasses() {
958 return $this->required_by_object_classes;
959 }
960
961 /**
962 * Returns if this attribute is binary.
963 *
964 * @return bool is binary
965 */
966 public function isBinary(): bool {
967 return strcasecmp($this->type, 'Certificate') === 0 ||
968 strcasecmp($this->type, 'Binary') === 0 ||
969 strcasecmp($this->name, 'usercertificate') === 0 ||
970 strcasecmp($this->name, 'usersmimecertificate') === 0 ||
971 strcasecmp($this->name, 'networkaddress') === 0 ||
972 strcasecmp($this->name, 'objectGUID') === 0 ||
973 strcasecmp($this->name, 'objectSID') === 0 ||
974 strcasecmp($this->name, 'auditingPolicy') === 0 ||
975 strcasecmp($this->name, 'jpegPhoto') === 0 ||
976 strcasecmp($this->name, 'krbExtraData') === 0 ||
977 strcasecmp($this->name, 'krbPrincipalKey') === 0 ||
978 $this->syntax_oid === '1.3.6.1.4.1.1466.115.121.1.10' ||
979 $this->syntax_oid === '1.3.6.1.4.1.1466.115.121.1.28' ||
980 $this->syntax_oid === '1.3.6.1.4.1.1466.115.121.1.5' ||
981 $this->syntax_oid === '1.3.6.1.4.1.1466.115.121.1.8' ||
982 $this->syntax_oid === '1.3.6.1.4.1.1466.115.121.1.9';
983 }
984
985 }
986
987 /**
988 * Represents an LDAP Syntax
989 *
990 * @package lib
991 */
992 class Syntax extends SchemaItem {
993 /** Initializes the class' member variables */
994 protected function initVars(): void {
995 parent::initVars();
996 $this->oid = null;
997 $this->description = null;
998 }
999
1000 /**
1001 * Creates a new Syntax object from a raw LDAP syntax string.
1002 *
1003 * @param string $raw_ldap_syntax_string syntax string
1004 */
1005 public function __construct($raw_ldap_syntax_string) {
1006 $this->initVars();
1007 $class = $raw_ldap_syntax_string;
1008 $strings = preg_split("/[\s,]+/", $class, -1, PREG_SPLIT_DELIM_CAPTURE);
1009 for ($i = 0; $i < count($strings); $i++) {
1010 switch ($strings[$i]) {
1011 case '(':
1012 break;
1013 case 'DESC':
1014 do {
1015 $i++;
1016 if (empty($this->description)) {
1017 $this->description .= $strings[$i];
1018 }
1019 else {
1020 $this->description = $this->description . " " . $strings[$i];
1021 }
1022 }
1023 while (!preg_match("/\'$/", $strings[$i]));
1024 break;
1025 default:
1026 if (preg_match("/[\d\.]+/i", $strings[$i]) && $i === 1) {
1027 $this->oid = $strings[$i];
1028 }
1029 }
1030 }
1031 $this->description = preg_replace("/^\'/", "", $this->description);
1032 $this->description = preg_replace("/\'$/", "", $this->description);
1033 }
1034 }
1035
1036 /**
1037 * Represents an LDAP MatchingRule
1038 *
1039 * @package lib
1040 */
1041 class MatchingRule extends SchemaItem {
1042 /** @var string|null $name This rule's name */
1043 protected $name;
1044 /** @var string|null $syntax This rule's syntax OID */
1045 protected $syntax;
1046 /** @var bool $is_obsolete Boolean value indicating whether this MatchingRule is obsolete */
1047 protected $is_obsolete;
1048 /** @var string[] $used_by_attrs An array of attribute names who use this MatchingRule */
1049 protected $used_by_attrs;
1050
1051 /** Initialize the class' member variables */
1052 protected function initVars(): void {
1053 parent::initVars();
1054 $this->oid = null;
1055 $this->name = null;
1056 $this->description = null;
1057 $this->is_obsolete = false;
1058 $this->syntax = null;
1059 $this->used_by_attrs = [];
1060 }
1061
1062 /**
1063 * Creates a new MatchingRule object from a raw LDAP MatchingRule string.
1064 *
1065 * @param string $raw_ldap_matching_rule_string rule string
1066 */
1067 public function __construct($raw_ldap_matching_rule_string) {
1068 $this->initVars();
1069 $strings = preg_split("/[\s,]+/", $raw_ldap_matching_rule_string, -1, PREG_SPLIT_DELIM_CAPTURE);
1070 for ($i = 0; $i < count($strings); $i++) {
1071 switch ($strings[$i]) {
1072 case '(':
1073 break;
1074 case 'NAME':
1075 if ($strings[$i + 1] !== "(") {
1076 do {
1077 $i++;
1078 if (empty($this->name)) {
1079 $this->name = $strings[$i];
1080 }
1081 else {
1082 $this->name .= " " . $strings[$i];
1083 }
1084 }
1085 while (!preg_match("/\'$/", $strings[$i]));
1086 }
1087 else {
1088 $i++;
1089 do {
1090 $i++;
1091 if ((string) $this->name === '') {
1092 $this->name = $strings[$i];
1093 }
1094 else {
1095 $this->name .= " " . $strings[$i];
1096 }
1097 }
1098 while (!preg_match("/\'$/", $strings[$i]));
1099 do {
1100 $i++;
1101 }
1102 while (!preg_match('/\)+\)?/', $strings[$i]));
1103 }
1104 $this->name = preg_replace("/^\'/", "", $this->name);
1105 $this->name = preg_replace("/\'$/", "", $this->name);
1106 break;
1107 case 'DESC':
1108 do {
1109 $i++;
1110 if ((string) $this->description === '') {
1111 $this->description .= $strings[$i];
1112 }
1113 else {
1114 $this->description = $this->description . " " . $strings[$i];
1115 }
1116 }
1117 while (!preg_match("/\'$/", $strings[$i]));
1118 break;
1119 case 'OBSOLETE':
1120 $this->is_obsolete = TRUE;
1121 break;
1122 case 'SYNTAX':
1123 $this->syntax = $strings[++$i];
1124 break;
1125 default:
1126 if (preg_match("/[\d\.]+/i", $strings[$i]) && $i === 1) {
1127 $this->oid = $strings[$i];
1128 }
1129 }
1130 }
1131 if (!empty($this->description)) {
1132 $this->description = preg_replace("/^\'/", "", $this->description);
1133 $this->description = preg_replace("/\'$/", "", $this->description);
1134 }
1135 }
1136
1137 /**
1138 * Sets the list of used_by_attrs to the array specified by $attrs;
1139 * @param string[] $attrs The array of attribute names (strings) which use this MatchingRule
1140 */
1141 public function setUsedByAttrs($attrs): void {
1142 $this->used_by_attrs = $attrs;
1143 }
1144
1145 /**
1146 * Adds an attribute name to the list of attributes who use this MatchingRule
1147 *
1148 * @param string $new_attr_name attribute name
1149 * @return bool true if the attribute was added and false otherwise (already in the list)
1150 */
1151 public function addUsedByAttr($new_attr_name): bool {
1152 foreach ($this->used_by_attrs as $attr_name)
1153 if (0 === strcasecmp($attr_name, $new_attr_name)) {
1154 return false;
1155 }
1156 $this->used_by_attrs[] = $new_attr_name;
1157 return true;
1158 }
1159
1160 /**
1161 * Gets this MatchingRule's name.
1162 * @return string The name.
1163 */
1164 public function getName() {
1165 return $this->name;
1166 }
1167
1168 /**
1169 * Gets whether this MatchingRule is flagged as obsolete by the LDAP server.
1170 * @return bool True if this MatchingRule is obsolete and false otherwise.
1171 */
1172 public function getIsObsolete() {
1173 return $this->is_obsolete;
1174 }
1175
1176 /**
1177 * Gets an array of attribute names (strings) which use this MatchingRule
1178 * @return string[] The array of attribute names (strings).
1179 */
1180 public function getUsedByAttrs() {
1181 return $this->used_by_attrs;
1182 }
1183 }
1184
1185 /**
1186 * Represents an LDAP schema matchingRuleUse entry
1187 *
1188 * @package lib
1189 */
1190 class MatchingRuleUse extends SchemaItem {
1191 /** @var string|null The name of the MathingRule this applies to */
1192 protected $name;
1193 /** @var string[] An array of attributeType names who make use of the mathingRule
1194 * identified by $this->oid and $this->name */
1195 protected $used_by_attrs;
1196
1197 /** Initialize the class' member variables */
1198 protected function initVars(): void {
1199 parent::initVars();
1200 $this->oid = null;
1201 $this->name = null;
1202 $this->used_by_attrs = [];
1203 }
1204
1205 /**
1206 * @param string $raw_matching_rule_use_string rule string
1207 */
1208 public function __construct($raw_matching_rule_use_string) {
1209 $this->initVars();
1210 $strings = preg_split("/[\s,]+/", $raw_matching_rule_use_string, -1, PREG_SPLIT_DELIM_CAPTURE);
1211 for ($i = 0; $i < count($strings); $i++) {
1212 switch ($strings[$i]) {
1213 case '(':
1214 break;
1215 case 'NAME':
1216 if ($strings[$i + 1] !== "(") {
1217 do {
1218 $i++;
1219 if ($this->name === null || (string) $this->name === '') {
1220 $this->name = $strings[$i];
1221 }
1222 else {
1223 $this->name .= " " . $strings[$i];
1224 }
1225 }
1226 while (!preg_match("/\'$/", $strings[$i]));
1227 }
1228 else {
1229 $i++;
1230 do {
1231 $i++;
1232 if ((string) $this->name === '') {
1233 $this->name = $strings[$i];
1234 }
1235 else {
1236 $this->name .= " " . $strings[$i];
1237 }
1238 }
1239 while (!preg_match("/\'$/", $strings[$i]));
1240 do {
1241 $i++;
1242 }
1243 while (!preg_match('/\)+\)?/', $strings[$i]));
1244 }
1245 $this->name = preg_replace("/^\'/", "", $this->name);
1246 $this->name = preg_replace("/\'$/", "", $this->name);
1247 break;
1248 case 'APPLIES':
1249 if ($strings[$i + 1] !== "(") {
1250 // has a single attribute name
1251 $i++;
1252 $this->used_by_attrs = [$strings[$i]];
1253 //echo "Adding single: " . $strings[$i] . "<br />";
1254 }
1255 else {
1256 // has multiple attribute names
1257 $i++;
1258 while ($strings[$i] !== ")") {
1259 $i++;
1260 $new_attr = $strings[$i];
1261 $new_attr = preg_replace("/^\'/", "", $new_attr);
1262 $new_attr = preg_replace("/\'$/", "", $new_attr);
1263 $this->used_by_attrs[] = $new_attr;
1264 //echo "Adding $new_attr<br />";
1265 $i++;
1266 }
1267 }
1268 break;
1269 default:
1270 if (preg_match("/[\d\.]+/i", $strings[$i]) && $i === 1) {
1271 $this->oid = $strings[$i];
1272 }
1273 }
1274 }
1275 sort($this->used_by_attrs);
1276 }
1277
1278 /**
1279 * Gets this MatchingRuleUse's name
1280 * @return string The name
1281 */
1282 public function getName() {
1283 return $this->name;
1284 }
1285
1286 /**
1287 * Gets an array of attribute names (strings) which use this MatchingRuleUse object.
1288 * @return string[] The array of attribute names (strings).
1289 */
1290 public function getUsedByAttrs() {
1291 return $this->used_by_attrs;
1292 }
1293 }
1294
1295 /**
1296 * Helper for _get_raw_schema() which fetches the DN of the schema object
1297 * in an LDAP server based on a DN. Entries should set the subSchemaSubEntry
1298 * attribute pointing to the DN of the server schema. You can specify the
1299 * DN whose subSchemaSubEntry you wish to retrieve of specify an empty string
1300 * to fetch the subScehamSubEntry from the Root DSE.
1301 *
1302 * @param string $dn the DN (may be null) which houses the subschemaSubEntry attribute which
1303 * this function can use to determine the schema entry's DN.
1304 * @param bool $debug switch to true to see some nice and copious output. :)
1305 *
1306 * @return string|false the DN of the entry which houses this LDAP server's schema.
1307 */
1308 function _get_schema_dn($dn, $debug = false): bool|string {
1309 if ($debug) {
1310 echo "<pre>";
1311 }
1312 $ds = $_SESSION['ldap']->server();
1313
1314 $search = @ldap_read($ds, $dn, '(objectClass=*)', ['subschemaSubentry']);
1315 if ($debug) {
1316 echo "Search result (ldap_read): ";
1317 var_dump($search);
1318 echo "\n";
1319 }
1320 if (!$search) {
1321 if ($debug) {
1322 echo "_get_schema_dn() returning false. (search val is false)\n";
1323 }
1324 return false;
1325 }
1326
1327 if (@ldap_count_entries($ds, $search) === 0) {
1328 if ($debug) {
1329 echo "_get_schema_dn() returning false (ldap_count_entries() == 0).\n";
1330 }
1331 return false;
1332 }
1333
1334 $entries = @ldap_get_entries($ds, $search);
1335 if ($debug) {
1336 echo "Entries (ldap_get_entries): ";
1337 var_dump($entries);
1338 echo "\n";
1339 }
1340 if ($entries === false) {
1341 if ($debug) {
1342 echo "_get_schema_dn() returning false (Bad entries val, false or not array).\n";
1343 }
1344 return false;
1345 }
1346
1347 $entry = $entries[0] ?? false;
1348 if (!$entry) {
1349 if ($debug) {
1350 echo "_get_schema_dn() returning false (entry val is false)\n";
1351 }
1352 return false;
1353 }
1354
1355 $sub_schema_sub_entry = $entry[0] ?? false;
1356 if (!$sub_schema_sub_entry) {
1357 if ($debug) {
1358 echo "_get_schema_dn() returning false (sub_schema_sub_entry val is false)\n";
1359 }
1360 return false;
1361 }
1362
1363 $schema_dn = $entry[$sub_schema_sub_entry][0] ?? false;
1364
1365 if ($debug) {
1366 echo "_get_schema_dn() returning: \"" . $schema_dn . "\"\n";
1367 }
1368 return $schema_dn;
1369 }
1370
1371 /**
1372 * Fetches the raw schema array for the subschemaSubentry of the server. Note,
1373 * this function has grown many hairs to accommodate more LDAP servers. It is
1374 * needfully complicated as it now supports many popular LDAP servers that
1375 * don't necessarily expose their schema "the right way".
1376 *
1377 * @param string $schema_to_fetch - A string indicating which type of schema to
1378 * fetch. Five valid values: 'objectclasses', 'attributetypes',
1379 * 'ldapsyntaxes', 'matchingruleuse', or 'matchingrules'.
1380 * Case insensitive.
1381 * @param string $dn (optional) This parameter is the DN of the entry whose schema you
1382 * would like to fetch. Entries have the option of specifying
1383 * their own subschemaSubentry that points to the DN of the system
1384 * schema entry which applies to this attribute. If unspecified,
1385 * this will try to retrieve the schema from the RootDSE subschemaSubentry.
1386 * Failing that, we use some commonly known schema DNs. Default
1387 * value is the Root DSE DN (zero-length string)
1388 * @return string[]|false an array of strings of this form:
1389 * Array (
1390 * [0] => "( 1.3.6.1.4.1.7165.1.2.2.4 NAME 'gidPool' DESC 'Pool ...
1391 * [1] => "( 1.3.6.1.4.1.7165.2.2.3 NAME 'sambaAccount' DESC 'Sa ...
1392 * etc.
1393 */
1394 function _get_raw_schema($schema_to_fetch, $dn = '') {
1395
1396 $ds = $_SESSION['ldap']->server();
1397
1398 // error checking
1399 $schema_to_fetch = strtolower($schema_to_fetch);
1400
1401 // Try to get the schema DN from the specified entry.
1402 $schema_dn = _get_schema_dn($dn);
1403
1404 // Do we need to try again with the Root DSE?
1405 if (!$schema_dn) {
1406 $schema_dn = _get_schema_dn('');
1407 }
1408
1409 // Store the eventual schema retrieval in $schema_search
1410 $schema_search = null;
1411
1412 if ($schema_dn) {
1413 $schema_search = @ldap_read($ds, $schema_dn, '(objectClass=*)',
1414 [$schema_to_fetch], 0, 0, 0,
1415 LDAP_DEREF_ALWAYS);
1416
1417 // Were we not able to fetch the schema from the $schema_dn?
1418 $schema_entries = $schema_search !== false ? @ldap_get_entries($ds, $schema_search) : [];
1419 if ($schema_search === false ||
1420 0 === @ldap_count_entries($ds, $schema_search) ||
1421 !isset($schema_entries[0][$schema_to_fetch])) {
1422 // Try again with a different filter (some servers require (objectClass=subschema) like M-Vault)
1423 $schema_search = @ldap_read($ds, $schema_dn, '(objectClass=subschema)',
1424 [$schema_to_fetch], 0, 0, 0,
1425 LDAP_DEREF_ALWAYS);
1426 if ($schema_search !== false) {
1427 $schema_entries = @ldap_get_entries($ds, $schema_search);
1428 }
1429 // Still didn't get it?
1430 if ($schema_search === false ||
1431 0 === @ldap_count_entries($ds, $schema_search) ||
1432 !isset($schema_entries[0][$schema_to_fetch])) {
1433 unset($schema_entries);
1434 unset($schema_dn);
1435 $schema_search = null;
1436 }
1437 }
1438 }
1439
1440 // Second chance: If the DN or Root DSE didn't give us the subschemaSubentry, ie $schema_search
1441 // is still null, use some common subSchemaSubentry DNs as a work-around.
1442
1443 // cn=subschema for OpenLDAP
1444 if ($schema_search === null) {
1445 // try with the standard DN
1446 $schema_search = @ldap_read($ds, 'cn=subschema', '(objectClass=*)',
1447 [$schema_to_fetch], 0, 0, 0, LDAP_DEREF_ALWAYS);
1448 if ($schema_search === false) {
1449 $schema_search = null;
1450 }
1451 }
1452
1453 // cn=schema for Novell eDirectory
1454 if ($schema_search === null) {
1455 // try again, with a different schema DN
1456 $schema_search = @ldap_read($ds, 'cn=schema', '(objectClass=*)',
1457 [$schema_to_fetch], 0, 0, 0, LDAP_DEREF_ALWAYS);
1458 if ($schema_search === false) {
1459 $schema_search = null;
1460 }
1461 }
1462
1463 $base_dn = null;
1464 // cn=schema,cn=configuration,dc=example,dc=com for ActiveDirectory
1465 if ($schema_search === null) {
1466 // try again, with a different schema DN
1467 global $servers;
1468 $base_dn = $servers['base'] ?? null;
1469 if ($base_dn != null) {
1470 $schema_search = @ldap_read($ds, 'cn=schema,cn=configuration,' . $base_dn, '(objectClass=*)',
1471 [$schema_to_fetch], 0, 0, 0, LDAP_DEREF_ALWAYS);
1472 if ($schema_search === false) {
1473 $schema_search = null;
1474 }
1475 }
1476 }
1477
1478 // cn=Schema,ou=Admin,dc=example,dc=com for SiteServer
1479 if ($schema_search === null) {
1480 // try again, with a different schema DN
1481 global $servers;
1482 $base_dn = $servers['base'] ?? null;
1483 if ($base_dn != null) {
1484 $schema_search = @ldap_read($ds, 'cn=Schema,ou=Admin,' . $base_dn, '(objectClass=*)',
1485 [$schema_to_fetch], 0, 0, 0, LDAP_DEREF_ALWAYS);
1486 if ($schema_search === false) {
1487 $schema_search = null;
1488 }
1489 }
1490 }
1491
1492 // Attempt to pull schema from Root DSE with scope "base"
1493 if (($schema_search === null) && ($base_dn !== null)) {
1494 // try again, with a different schema DN
1495 $schema_search = @ldap_read($ds, '', '(objectClass=*)',
1496 [$schema_to_fetch], 0, 0, 0, LDAP_DEREF_ALWAYS);
1497 if ($schema_search === false) {
1498 $schema_search = null;
1499 }
1500 else {
1501 $schema_entries = @ldap_get_entries($ds, $schema_search);
1502 if (($schema_entries === false) || !isset($schema_entries[0][$schema_to_fetch])) {
1503 $schema_search = null;
1504 }
1505 }
1506 }
1507
1508 // Attempt to pull schema from Root DSE with scope "one" (work-around for Isode M-Vault X.500/LDAP)
1509 if (($schema_search === null) && ($base_dn !== null)) {
1510 // try again, with a different schema DN
1511 $schema_search = @ldap_list($ds, '', '(objectClass=*)',
1512 [$schema_to_fetch], 0, 0, 0, LDAP_DEREF_ALWAYS);
1513 if ($schema_search === false) {
1514 $schema_search = null;
1515 }
1516 else {
1517 $schema_entries = @ldap_get_entries($ds, $schema_search);
1518 if (($schema_entries === false) || !isset($schema_entries[0][$schema_to_fetch])) {
1519 $schema_search = null;
1520 }
1521 }
1522 }
1523
1524 // Shall we just give up?
1525 if ($schema_search === null) {
1526 set_schema_cache_unavailable();
1527 return false;
1528 }
1529
1530 $schema = @ldap_get_entries($ds, $schema_search);
1531 if ($schema === false) {
1532 set_schema_cache_unavailable();
1533 return false;
1534 }
1535
1536 if (!isset($schema[0][$schema_to_fetch])) {
1537 set_schema_cache_unavailable();
1538 return false;
1539 }
1540
1541 // Make a nice array of this form:
1542 // Array (
1543 // [0] => "( 1.3.6.1.4.1.7165.1.2.2.4 NAME 'gidPool' DESC 'Pool ...
1544 // [1] => "( 1.3.6.1.4.1.7165.2.2.3 NAME 'sambaAccount' DESC 'Sa ...
1545 // etc.
1546 $schema = $schema[0][$schema_to_fetch];
1547 unset($schema['count']);
1548
1549 return $schema;
1550 }
1551
1552 /**
1553 * Gets an associative array of ObjectClass objects for the specified
1554 * server. Each array entry's key is the name of the objectClass
1555 * in lower-case and the value is an ObjectClass object.
1556 *
1557 * @param string $dn (optional) It is easier to fetch schema if a DN is provided
1558 * which defines the subschemaSubEntry attribute (all entries should).
1559 * @param bool $use_cache use cache
1560 *
1561 * @return ObjectClass[]|false An array of ObjectClass objects.
1562 *
1563 * @see ObjectClass
1564 * @see get_schema_objectclass
1565 */
1566 function get_schema_objectclasses($dn = null, $use_cache = true): array|false {
1567 if ($use_cache && cached_schema_available('objectclasses')) {
1568 return get_cached_schema('objectclasses');
1569 }
1570
1571 $raw_oclasses = _get_raw_schema('objectclasses', $dn);
1572 if (!$raw_oclasses) {
1573 return [];
1574 }
1575
1576 // build the array of objectClasses
1577 $object_classes = [];
1578 foreach ($raw_oclasses as $class_string) {
1579 if (empty($class_string)) {
1580 continue;
1581 }
1582 $object_class = new ObjectClass($class_string);
1583 $name = $object_class->getName();
1584 $key = strtolower($name);
1585 $object_classes[$key] = $object_class;
1586 }
1587
1588 ksort($object_classes);
1589
1590 // cache the schema to prevent multiple schema fetches from LDAP server
1591 set_cached_schema('objectclasses', $object_classes);
1592 return ($object_classes);
1593 }
1594
1595 /**
1596 * Gets a single ObjectClass object specified by name.
1597 *
1598 * @param string $oclass_name The name of the objectClass to fetch.
1599 * @param string $dn (optional) It is easier to fetch schema if a DN is provided
1600 * which defines the subschemaSubEntry attribute (all entries should).
1601 * @param bool $use_cache use cache
1602 *
1603 * @return ObjectClass|false The specified ObjectClass object or false on error.
1604 *
1605 * @see ObjectClass
1606 * @see get_schema_objectclasses
1607 */
1608 function get_schema_objectclass($oclass_name, $dn = null, $use_cache = true): bool|ObjectClass {
1609 $oclass_name = strtolower($oclass_name);
1610 $oclasses = get_schema_objectclasses($dn, $use_cache);
1611 if ($oclasses === []) {
1612 return false;
1613 }
1614 return $oclasses[$oclass_name] ?? false;
1615 }
1616
1617 /**
1618 * Gets an associative array of AttributeType objects for the specified
1619 * server. Each array entry's key is the name of the attributeType
1620 * in lower-case and the value is an AttributeType object.
1621 *
1622 * @param string $dn (optional) It is easier to fetch schema if a DN is provided
1623 * which defines the subschemaSubEntry attribute (all entries should).
1624 * @param bool $use_cache use cache
1625 *
1626 * @return AttributeType[]|false an array of AttributeType objects.
1627 */
1628 function get_schema_attributes($dn = null, $use_cache = true) {
1629 if ($use_cache && cached_schema_available('attributetypes')) {
1630 return get_cached_schema('attributetypes');
1631 }
1632
1633 $raw_attrs = _get_raw_schema('attributeTypes', $dn);
1634 if (!$raw_attrs) {
1635 return [];
1636 }
1637
1638 // build the array of attributeTypes
1639 $syntaxes = get_schema_syntaxes($dn);
1640 $attrs = [];
1641 /**
1642 * bug 856832: create two arrays - one indexed by name (the standard
1643 * $attrs array above) and one indexed by oid (the new $attrs_oid array
1644 * below). This will help for directory servers, like IBM's, that use OIDs
1645 * in their attribute definitions of SUP, etc
1646 */
1647 $attrs_oid = [];
1648 foreach ($raw_attrs as $attr_string) {
1649 if (empty($attr_string)) {
1650 continue;
1651 }
1652 $attr = new AttributeType($attr_string);
1653 if (!empty($attr->getSyntaxOID()) && isset($syntaxes[$attr->getSyntaxOID()])) {
1654 $syntax = $syntaxes[$attr->getSyntaxOID()];
1655 $attr->setType($syntax->getDescription());
1656 }
1657 $name = $attr->getName();
1658 $key = strtolower($name);
1659 $attrs[$key] = $attr;
1660
1661 /**
1662 * bug 856832: create an entry in the $attrs_oid array too. This
1663 * will be a ref to the $attrs entry for maintenance and performance
1664 * reasons
1665 */
1666 $oid = $attr->getOID();
1667 $attrs_oid[$oid] = &$attrs[$key];
1668 }
1669
1670 add_aliases_to_attrs($attrs);
1671 /**
1672 * bug 856832: pass the $attrs_oid array as a second (new) parameter
1673 * to add_sup_to_attrs. This will allow lookups by either name or oid.
1674 */
1675 add_sup_to_attrs($attrs, $attrs_oid);
1676
1677 ksort($attrs);
1678
1679 // Add the used in and required_by values.
1680 $schema_object_classes = get_schema_objectclasses();
1681 if ($schema_object_classes === false) {
1682 return [];
1683 }
1684
1685 foreach ($schema_object_classes as $object_class) {
1686 $must_attrs = $object_class->getMustAttrNames($schema_object_classes);
1687 $may_attrs = $object_class->getMayAttrNames($schema_object_classes);
1688 $oclass_attrs = array_unique(array_merge($must_attrs, $may_attrs));
1689
1690 // Add Used In.
1691 foreach ($oclass_attrs as $attr_name) {
1692 if (isset($attrs[strtolower($attr_name)])) {
1693 $attrs[strtolower($attr_name)]->addUsedInObjectClass(
1694 $object_class->getName());
1695 }
1696 }
1697
1698 // Add Required By.
1699 foreach ($must_attrs as $attr_name) {
1700 if (isset($attrs[strtolower($attr_name)])) {
1701 $attrs[strtolower($attr_name)]->addRequiredByObjectClass(
1702 $object_class->getName());
1703 }
1704 }
1705
1706 }
1707
1708 // cache the schema to prevent multiple schema fetches from LDAP server
1709 set_cached_schema('attributetypes', $attrs);
1710 return ($attrs);
1711 }
1712
1713 /**
1714 * For each attribute that has multiple names, this function adds unique entries to
1715 * the attrs array for those names. Ie, attributeType has name 'gn' and 'givenName'.
1716 * This function will create a unique entry for 'gn' and 'givenName'.
1717 *
1718 * @param AttributeType[] $attrs attributes
1719 */
1720 function add_aliases_to_attrs(&$attrs): void {
1721 $toaddAttrs = [];
1722 // go back and add data from aliased attributeTypes
1723 foreach ($attrs as $attr) {
1724 $aliases = $attr->getAliases();
1725 // foreach of the attribute's aliases, create a new entry in the attrs array
1726 // with its name set to the alias name, and all other data copied
1727 foreach ($aliases as $alias_attr_name) {
1728 $new_attr = clone $attr;
1729 $new_attr->setName($alias_attr_name);
1730 $new_attr->addAlias($attr->getName());
1731 $new_attr->removeAlias($alias_attr_name);
1732 $new_attr_key = strtolower($alias_attr_name);
1733 $toaddAttrs[$new_attr_key] = $new_attr;
1734 }
1735 }
1736 $attrs = array_merge($attrs, $toaddAttrs);
1737 }
1738
1739 /**
1740 * Adds inherited values to each attributeType specified by the SUP directive.
1741 * Supports infinite levels of inheritance.
1742 * Bug 856832: require a second parameter that has all attributes indexed by OID
1743 *
1744 * @param array<string, AttributeType> $attrs attributes
1745 * @param array<string, AttributeType> $attrs_oid attributes by OIDs
1746 */
1747 function add_sup_to_attrs(&$attrs, &$attrs_oid): void {
1748 // go back and add any inherited descriptions from parent attributes (ie, cn inherits name)
1749 foreach ($attrs as $key => $attr) {
1750 $sup_attr_name = $attr->getSupAttribute();
1751 $sup_attr = null;
1752
1753 // Does this attribute have any inheritance happening here?
1754 if (($sup_attr_name !== null) && !empty(trim($sup_attr_name))) {
1755
1756 // This loop really should traverse infinite levels of inheritance (SUP) for attributeTypes,
1757 // but just in case we get carried away, stop at 100. This shouldn't happen, but for
1758 // some weird reason, we have had someone report that it has happened. Oh well.
1759 $i = 0;
1760 while ($i++ < 100/** 100 == INFINITY ;) */) {
1761 /**
1762 * Bug 856832: check if sup is indexed by OID. If it is,
1763 * replace the OID with the appropriate name. Then reset
1764 * $sup_attr_name to the name instead of the OID. This will
1765 * make all the remaining code in this function work as
1766 * expected.
1767 */
1768 if (isset($attrs_oid[$sup_attr_name])) {
1769 $attr->setSupAttribute($attrs_oid[$sup_attr_name]->getName());
1770 $sup_attr_name = $attr->getSupAttribute();
1771 }
1772
1773 if (!isset($attrs[strtolower($sup_attr_name)])) {
1774 echo("Schema error: attributeType '" . $attr->getName() . "' inherits from
1775 '" . $sup_attr_name . "', but attributeType '" . $sup_attr_name . "' does not
1776 exist.");
1777 return;
1778 }
1779
1780 $sup_attr = $attrs[strtolower($sup_attr_name)];
1781
1782 $sup_attr_name = $sup_attr->getSupAttribute();
1783
1784 // Does this superior attributeType not have a superior attributeType?
1785 if (null == $sup_attr_name || trim($sup_attr_name) === '') {
1786
1787 // Since this attribute's superior attribute does not have another superior
1788 // attribute, clone its properties for this attribute. Then, replace
1789 // those cloned values with those that can be explicitly set by the child
1790 // attribute attr). Save those few properties which the child can set here:
1791 $tmp_name = $attr->getName();
1792 $tmp_oid = $attr->getOID();
1793 $tmp_sup = $attr->getSupAttribute();
1794 $tmp_aliases = $attr->getAliases();
1795 $tmp_single_val = $attr->getIsSingleValue();
1796
1797
1798 // clone the SUP attributeType and populate those values
1799 // that were set by the child attributeType
1800 $attr = clone $sup_attr;
1801 $attr->setOID($tmp_oid);
1802 $attr->setName($tmp_name);
1803 $attr->setSupAttribute($tmp_sup);
1804 $attr->setAliases($tmp_aliases);
1805
1806 // only overwrite the SINGLE-VALUE property if the child explicitly sets it
1807 // (note: All LDAP attributes default to multi-value if not explicitly set SINGLE-VALUE)
1808 if (true === $tmp_single_val) {
1809 $attr->setIsSingleValue(true);
1810 }
1811
1812 // replace this attribute in the attrs array now that we have populated
1813 // new values therein
1814 $attrs[$key] = $attr;
1815
1816 // very important: break out after we are done with this attribute
1817 $sup_attr_name = null;
1818 $sup_attr = null;
1819 break;
1820
1821 }
1822 }
1823 }
1824 }
1825 }
1826
1827 /**
1828 * @param string $dn DN
1829 * @param bool $use_cache use cache
1830 * @return MatchingRule[]|false Returns an array of MatchingRule objects for the specified server.
1831 * The key of each entry is the OID of the matching rule.
1832 */
1833 function get_schema_matching_rules($dn = null, $use_cache = true) {
1834 if ($use_cache && cached_schema_available('matchingrules')) {
1835 return get_cached_schema('matchingrules');
1836 }
1837
1838 // build the array of MatchingRule objects
1839 $raw_matching_rules = _get_raw_schema('matchingRules', $dn);
1840 if (!$raw_matching_rules) {
1841 return false;
1842 }
1843 $rules = [];
1844 foreach ($raw_matching_rules as $rule_string) {
1845 if (empty($rule_string)) {
1846 continue;
1847 }
1848 $rule = new MatchingRule($rule_string);
1849 $key = strtolower($rule->getName());
1850 $rules[$key] = $rule;
1851 }
1852
1853 ksort($rules);
1854
1855 // For each MatchingRuleUse entry, add the attributes who use it to the
1856 // MatchingRule in the $rules array.
1857 $raw_matching_rule_use = _get_raw_schema('matchingRuleUse');
1858 if ($raw_matching_rule_use !== false) {
1859 foreach ($raw_matching_rule_use as $rule_use_string) {
1860 if (empty($rule_use_string)) {
1861 continue;
1862 }
1863 $rule_use = new MatchingRuleUse($rule_use_string);
1864 $key = strtolower($rule_use->getName());
1865 if (isset($rules[$key])) {
1866 $rules[$key]->setUsedByAttrs($rule_use->getUsedByAttrs());
1867 }
1868 }
1869 }
1870 else {
1871 // No MatchingRuleUse entry in the subschema, so brute-forcing
1872 // the reverse-map for the "$rule->getUsedByAttrs()" data.
1873 $attrs = get_schema_attributes($dn);
1874 foreach ($attrs as $attr) {
1875 $rule_key = strtolower($attr->getEquality());
1876 if (isset($rules[$rule_key])) {
1877 $rules[$rule_key]->addUsedByAttr($attr->getName());
1878 }
1879 }
1880 }
1881
1882 // cache the schema to prevent multiple schema fetches from LDAP server
1883 set_cached_schema('matchingrules', $rules);
1884 return $rules;
1885 }
1886
1887 /**
1888 * @param string $dn DN
1889 * @param bool $use_cache use cache
1890 * @return Syntax[]|false returns an array of Syntax objects that this LDAP server uses mapped to
1891 * their descriptions. The key of each entry is the OID of the Syntax.
1892 */
1893 function get_schema_syntaxes($dn = null, $use_cache = true) {
1894 if ($use_cache && cached_schema_available('ldapsyntaxes')) {
1895 return get_cached_schema('ldapsyntaxes');
1896 }
1897
1898 $raw_syntaxes = _get_raw_schema('ldapSyntaxes', $dn);
1899 if (!$raw_syntaxes) {
1900 return false;
1901 }
1902
1903 // build the array of attributes
1904 $syntaxes = [];
1905 foreach ($raw_syntaxes as $syntax_string) {
1906 $syntax = new Syntax($syntax_string);
1907 $key = strtolower(trim($syntax->getOID()));
1908 if ($key === '') {
1909 continue;
1910 }
1911 $syntaxes[$key] = $syntax;
1912 }
1913
1914 ksort($syntaxes);
1915
1916 // cache the schema to prevent multiple schema fetches from LDAP server
1917 set_cached_schema('ldapsyntaxes', $syntaxes);
1918
1919 return $syntaxes;
1920 }
1921
1922 // --------------------------------------------------------------------
1923 // Schema caching functions
1924 // --------------------------------------------------------------------
1925
1926 /**
1927 * @return bool returns true if the schema for $schema_type has been cached and
1928 * is available. $schema_type may be one of (lowercase) the following:
1929 * objectclasses
1930 * attributetypes
1931 * ldapsyntaxes
1932 * matchingrules
1933 * matchingruleuse
1934 * Note that _get_raw_schema() takes a similar parameter.
1935 *
1936 * @param string $schema_type type
1937 */
1938 function cached_schema_available($schema_type): bool {
1939 // Static memory cache available?
1940 // (note: this memory cache buys us a 20% speed improvement over strictly
1941 // checking the session, ie 0.05 to 0.04 secs)
1942 $schema_type = strtolower($schema_type);
1943 static $cache_avail;
1944 if (isset($cache_avail[$schema_type])) {
1945 return true;
1946 }
1947
1948 // Session cache available?
1949 if (isset($_SESSION['schema'][$schema_type])) {
1950 $cache_avail[$schema_type] = true;
1951 return true;
1952 }
1953 return false;
1954 }
1955
1956 /**
1957 * Returns the cached array of schemaitem objects for the specified
1958 * $schema_type. For list of valid $schema_type values, see above
1959 * schema_cache_available(). Note that internally, this function
1960 * utilizes a two-layer cache, one in memory using a static variable
1961 * for multiple calls within the same page load, and one in a session
1962 * for multiple calls within the same user session (spanning multiple
1963 * page loads).
1964 *
1965 * @param string $schema_type type
1966 * @return SchemaItem[]|false returns an array of SchemaItem objects on success or false on failure.
1967 */
1968 function get_cached_schema($schema_type) {
1969 static $cache;
1970 $schema_type = strtolower($schema_type);
1971 if (isset($cache[$schema_type])) {
1972 //echo "Getting memory-cached schema for \"$schema_type\"...<br />\n";
1973 return $cache[$schema_type];
1974 }
1975
1976 //echo "Getting session-cached schema for \"$schema_type\"...<br />\n";
1977 if (cached_schema_available($schema_type) && array_key_exists($schema_type, $_SESSION['schema'])) {
1978 $schema = $_SESSION['schema'][$schema_type];
1979 $cache[$schema_type] = $schema;
1980 return $schema;
1981 }
1982 else {
1983 return false;
1984 }
1985 }
1986
1987 /**
1988 * Caches the specified $schema_type.
1989 * $schema_items should be an array of SchemaItem instances (ie,
1990 * an array of ObjectClass, AttributeType, LDAPSyntax, MatchingRuleUse,
1991 * or MatchingRule objects.
1992 *
1993 * @param string $schema_type type
1994 * @param SchemaItem[] $schema_items items
1995 */
1996 function set_cached_schema($schema_type, array $schema_items): void {
1997 $schema_type = strtolower($schema_type);
1998 $_SESSION['schema'][$schema_type] = $schema_items;
1999 }
2000
2001 /**
2002 * Sets the schema entry for the server_id to be "unavailable" so that we realize
2003 * that we tried to get the schema but could not, so quit trying next time to
2004 * fetch it from the server.
2005 */
2006 function set_schema_cache_unavailable(): void {
2007 $_SESSION['schema']['unavailable'] = true;
2008 }