/usr/lib/perl5/Mozilla/LDAP/Entry.pm is in libmozilla-ldap-perl 1.5.3-1build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 | #############################################################################
# $Id: Entry.pm,v 1.13.2.9 2007/06/14 09:21:14 gerv%gerv.net Exp $
#
# ***** BEGIN LICENSE BLOCK *****
# Version: MPL 1.1/GPL 2.0/LGPL 2.1
#
# The contents of this file are subject to the Mozilla Public License Version
# 1.1 (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
# http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS IS" basis,
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
# for the specific language governing rights and limitations under the
# License.
#
# The Original Code is PerLDAP.
#
# The Initial Developer of the Original Code is
# Netscape Communications Corporation.
# Portions created by the Initial Developer are Copyright (C) 2001
# the Initial Developer. All Rights Reserved.
#
# Contributor(s):
# Clayton Donley
# Leif Hedstrom <leif@perldap.org>
# Michelle Hedstrom <mwyner@perldap.org>
#
# Alternatively, the contents of this file may be used under the terms of
# either the GNU General Public License Version 2 or later (the "GPL"), or
# the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
# in which case the provisions of the GPL or the LGPL are applicable instead
# of those above. If you wish to allow use of your version of this file only
# under the terms of either the GPL or the LGPL, and not to allow others to
# use your version of this file under the terms of the MPL, indicate your
# decision by deleting the provisions above and replace them with the notice
# and other provisions required by the GPL or the LGPL. If you do not delete
# the provisions above, a recipient may use your version of this file under
# the terms of any one of the MPL, the GPL or the LGPL.
#
# ***** END LICENSE BLOCK *****
# DESCRIPTION
# This package defines an object class to manage one single LDAP
# entry. This entry can either be a newly created one, or one
# retrieved from an LDAP server, using the Mozilla::LDAP::Conn class.
require 5.005;
package Mozilla::LDAP::Entry;
use Mozilla::LDAP::Utils 1.5 qw(normalizeDN);
use Tie::Hash;
use strict;
use vars qw($VERSION @ISA);
@ISA = ('Tie::StdHash');
$VERSION = "1.5";
#############################################################################
# Constructor, for convenience.
#
sub new
{
my $class = shift;
my (%entry, $obj);
tie %entry, $class;
$obj = bless \%entry, $class;
return $obj;
}
#############################################################################
# Creator, make a new tie hash instance, which will keep track of all
# changes made to the hash array. This is needed so we only update modified
# attributes.
#
sub TIEHASH
{
my $class = shift;
my $self = {};
return bless $self, $class;
}
#############################################################################
# Destructor, free a bunch of memory. This makes a lot more sense now,
# since apparently Perl does not handle self references properly within an
# object(??).
#
sub DESTROY
{
my $self = shift;
if (defined($self))
{
# This casuses Perl to segfault ... Go figure.
# undef %{$self} if (%{$self});
undef $self;
}
}
#############################################################################
# Store method, to keep track of changes on an entire array of values (per
# attribute, of course).
#
sub STORE
{
my $self = shift;
my $attr = lc shift;
my $val = shift;
return unless (defined($val) && ($val ne ""));
return unless (defined($attr) && ($attr ne ""));
# We don't "track" internal values, or DNs...
if (($attr =~ /^_.+_$/) || ($attr eq "dn"))
{
$self->{$attr} = $val;
return;
}
if (defined($self->{$attr}))
{
$self->{"_${attr}_save_"} = [ @{$self->{$attr}} ]
unless defined($self->{"_${attr}_save_"});
}
$self->{$attr} = $val;
$self->{"_${attr}_modified_"} = 1;
delete $self->{"_${attr}_deleted_"}
if defined($self->{"_${attr}_deleted_"});
# Potentially add the attribute to the OC order list.
if (! grep(/^$attr$/i, @{$self->{"_oc_order_"}}))
{
push(@{$self->{"_oc_order_"}}, $attr);
$self->{"_oc_numattr_"}++;
}
}
#############################################################################
# Fetch method, this is case insensitive (since LDAP is...).
#
sub FETCH
{
my $self = shift;
my $attr = lc shift;
return unless defined($self->{$attr});
return if defined($self->{"_${attr}_deleted_"});
return $self->{$attr};
}
#############################################################################
# Delete method, to keep track of changes. Note that we actually don't
# delete the attribute, just mark it as deleted. Now, the $1M question is,
# why don't we delete this? Seriously guys, this looks wrong...
#
sub DELETE
{
my $self = shift;
my $attr = lc shift;
return unless (defined($attr) && ($attr ne ""));
return unless defined($self->{$attr});
if ($attr =~ /^_.+_$/)
{
delete $self->{$attr};
}
else
{
$self->{"_${attr}_deleted_"} = 1;
}
}
#############################################################################
# See if an attribute/key exists in the entry (could still be undefined).
# The exists() (lowercase) is a kludge, kept for backward compatibility.
# Please use the EXISTS method (or just exists ... instead).
#
sub EXISTS
{
my $self = shift;
my $attr = lc shift;
return 0 unless (defined($attr) && ($attr ne ""));
return 0 if defined($self->{"_${attr}_deleted_"});
return exists $self->{$attr};
}
sub exists
{
my $self = shift;
my $attr = lc shift;
return exists $self->{$attr};
}
#############################################################################
# Reset the each()/key() session, and return the first key. This honors
# the oc_order, i.e. the order the attributes were returned in.
#
sub FIRSTKEY
{
my ($self, $idx) = ($_[$[], 0);
my (@attrs, $key);
return unless defined($self->{"_oc_order_"});
@attrs = @{$self->{"_oc_order_"}};
while ($idx < $self->{"_oc_numattr_"})
{
$key = $attrs[$idx++];
next if ($key =~ /^_.+_$/);
next if defined($self->{"_${key}_deleted_"});
$self->{"_oc_keyidx_"} = $idx;
return $key;
}
$self->{"_oc_keyidx_"} = $idx;
return;
}
#############################################################################
# Get the next key, if appropriate.
#
sub NEXTKEY
{
my $self = shift;
my $idx = $self->{"_oc_keyidx_"};
my (@attrs, $key);
return unless defined($self->{"_oc_order_"});
@attrs = @{$self->{"_oc_order_"}};
while ($idx < $self->{"_oc_numattr_"})
{
$key = $attrs[$idx++];
next if ($key =~ /^_.+_$/);
next if defined($self->{"_${key}_deleted_"});
last;
}
$self->{"_oc_keyidx_"} = $idx;
return unless (defined($key) && ($key ne ""));
return if ($key =~ /^_.+_$/);
return if defined($self->{"_${key}_deleted_"});
return $key;
}
#############################################################################
# Check if anything in the entry has been modified
#
sub isEntryModified {
my $self = shift;
foreach (@{$self->{'_oc_order_'}})
{
return(1) if ($self->isModified($_) || $self->isDeleted($_));
}
return(0);
}
#############################################################################
# Mark an attribute as changed. Normally you shouldn't have to use this,
# unless you're doing something really weird...
#
sub attrModified
{
my $self = shift;
my $attr = lc shift;
return 0 unless (defined($attr) && ($attr ne ""));
return 0 unless defined($self->{$attr});
return 0 if defined($self->{"_${attr}_deleted_"});
return 0 if ($attr eq "dn");
$self->{"_${attr}_save_"} = [ @{$self->{$attr}} ]
unless defined($self->{"_${attr}_save_"});
$self->{"_${attr}_modified_"} = 1;
return 1;
}
*markModified = \*attrModified;
#############################################################################
# Mark an attribute as "clean", meaning nothing has been changed in it.
# You should probably not use this method, unless you really know what
# you are doing... It is however used heavily by the Conn.pm package.
#
sub attrClean
{
my $self = shift;
my $attr = lc shift;
return 0 unless (defined($attr) && ($attr ne ""));
return 0 if ($attr eq "dn");
delete $self->{"_${attr}_modified_"}
if defined($self->{"_${attr}_modified_"});
delete $self->{"_${attr}_deleted_"}
if defined($self->{"_${attr}_deleted_"});
if (defined($self->{"_${attr}_save_"}))
{
undef @{$self->{"_${attr}_save_"}};
delete $self->{"_${attr}_save_"};
}
}
#############################################################################
# Ask if a particular attribute has been modified already. Return True or
# false depending on the internal status of the attribute.
#
sub isModified
{
my $self = shift;
my $attr = lc shift;
return 0 unless (defined($attr) && ($attr ne ""));
return 0 unless defined($self->{$attr});
return 0 unless defined($self->{"_${attr}_modified_"});
return 1;
}
#############################################################################
# Ask if a particular attribute has been deleted already. Return True or
# false depending on the internal status of the attribute.
#
sub isDeleted
{
my $self = shift;
my $attr = lc shift;
return 0 unless (defined($attr) && ($attr ne ""));
return 0 unless defined($self->{"_${attr}_deleted_"});
return 1;
}
#############################################################################
# Test if a attribute name is actually a real attribute, and not part of
# the internal structures.
#
sub isAttr
{
my $self = shift;
my $attr = lc shift;
return 0 unless (defined($attr) && ($attr ne ""));
return 0 unless defined($self->{$attr});
return 0 if defined($self->{"_${attr}_deleted_"});
return ($attr !~ /^_.+_$/);
}
#############################################################################
# Remove an attribute from the entry, basically the same as the DELETE
# method. We also make an alias for "delete" here, just in case (and to be
# somewhat backward compatible).
#
sub remove
{
my $self = shift;
my $attr = lc shift;
return 0 unless (defined($attr) && ($attr ne ""));
return 0 unless defined($self->{$attr});
$self->{"_${attr}_deleted_"} = 1;
return 1;
}
*delete = \*remove;
#############################################################################
# Move (rename) an attribute, return TRUE or FALSE depending on the outcome.
# The first argument is the name of the old attribute (e.g. CN), and the last
# argument is the new name (e.g. SN). Note that the "new" attribute can not
# already exist, and the old attribute must exist.
#
# The "force" argument can be used to override the check if the new
# attribute already exists. This is potentially dangerous.
#
sub move
{
my $self = shift;
my ($old, $new, $force);
if (ref $_[$[] eq "HASH")
{
my $hash = shift;
$old = lc $hash->{"old"};
$new = lc $hash->{"new"};
$force = $hash->{"force"} || 0;
}
else
{
$old = lc shift;
$new = lc shift;
$force = shift;
}
return 0 if ($self->isAttr($new) && (!defined($force) || !$force));
return 0 unless $self->isAttr($old);
$self->setValues($new, @{$self->{$old}}) || return 0;
$self->remove($old);
return 1;
}
*rename = \*move;
#############################################################################
# Copy an attribute, return TRUE or FALSE depending on the outcome. This
# is almost identical to the move method, except we don't delete the source.
#
sub copy
{
my $self = shift;
my ($old, $new, $force);
if (ref $_[$[] eq "HASH")
{
my $hash = shift;
$old = lc $hash->{"old"};
$new = lc $hash->{"new"};
$force = $hash->{"force"} || 0;
}
else
{
$old = lc shift;
$new = lc shift;
$force = shift;
}
return 0 if ($self->isAttr($new) && (!defined($force) || !$force));
return 0 unless $self->isAttr($old);
$self->setValues($new, @{$self->{$old}}) || return 0;
return 1;
}
#############################################################################
# Undo a remove(), or set of removeValues() fairly useless, to restore an
# attribute to it's original state. This is fairly useless, but hey...
#
sub unRemove
{
my $self = shift;
my $attr = lc shift;
return 0 unless (defined($attr) && ($attr ne ""));
return 0 if ($attr eq "dn");
# ToDo: We need to verify that this sucker works...
delete $self->{"_${attr}_deleted_"};
if (defined($self->{"_${attr}_save_"}))
{
undef @{$self->{$attr}};
$self->{$attr} = [ @{$self->{"_${attr}_save_"}} ];
undef @{$self->{"_${attr}_save_"}};
delete $self->{"_${attr}_save_"};
}
return 1;
}
*unDelete = \*unRemove;
#############################################################################
# Delete a value from an attribute, if it exists. NOTE: If it was the last
# value, we'll actually remove the entire attribute!
#
sub removeValue
{
my $self = shift;
my ($attr, $val, $norm);
my $i = 0;
my $attrval;
local $_;
if (ref $_[$[] eq "HASH")
{
my $hash = shift;
$attr = lc $hash->{"attr"};
$val = $hash->{"val"};
$norm = $hash->{"norm"} || 0;
}
else
{
$attr = lc shift;
($val, $norm) = @_;
}
return 0 unless (defined($val) && ($val ne ""));
return 0 unless (defined($attr) && ($attr ne ""));
return 0 unless defined($self->{$attr});
return 0 if ($attr eq "dn");
$val = normalizeDN($val) if (defined($norm) && $norm);
$self->{"_${attr}_save_"} = [ @{$self->{$attr}} ] unless
defined($self->{"_${attr}_save_"});
foreach $attrval (@{$self->{$attr}})
{
$_ = ((defined($norm) && $norm) ? normalizeDN($attrval) : $attrval);
if ($_ eq $val)
{
splice(@{$self->{$attr}}, $i, 1);
if ($self->size($attr) > 0)
{
$self->{"_${attr}_modified_"} = 1;
}
else
{
$self->{"_${attr}_deleted_"} = 1;
}
return 1;
}
$i++;
}
return 0;
}
*deleteValue = \*removeValue;
#############################################################################
# Just like removeValue(), but force the DN normalization of the value.
#
sub removeDNValue
{
my $self = shift;
my ($attr, $val);
if (ref $_[$[] eq "HASH")
{
my $hash = shift;
$attr = lc $hash->{"attr"};
$val = $hash->{"val"};
}
else
{
$attr = lc shift;
$val = shift;
}
return $self->removeValue($attr, $val, 1);
}
*deleteDNValue = \*removeDNValue;
#############################################################################
# Add a value to an attribute. The optional third argument indicates that
# we should not enforce the uniqueness on this attibute, thus bypassing
# the test and always add the value.
#
sub addValue
{
my $self = shift;
my ($attr, $val, $force, $norm, $nocase);
local $_;
if (ref $_[$[] eq "HASH")
{
my $hash = shift;
$attr = lc $hash->{"attr"};
$val = $hash->{"val"};
$force = $hash->{"force"};
$norm = $hash->{"norm"};
$nocase = $hash->{"nocase"};
}
else
{
$attr = lc shift;
($val, $force, $norm, $nocase) = @_;
}
return 0 unless (defined($val) && ($val ne ""));
return 0 unless (defined($attr) && ($attr ne ""));
return 0 if ($attr eq "dn");
if (defined($self->{$attr}) && (!defined($force) || !$force))
{
my $nval = $val;
my $attrval;
$nval = normalizeDN($val) if (defined($norm) && $norm);
foreach $attrval (@{$self->{$attr}})
{
$_ = ((defined($norm) && $norm) ? normalizeDN($attrval) : $attrval);
$_ = lc $_ if (defined($nocase) && $nocase);
return 0 if ($_ eq $nval);
}
}
if (defined($self->{$attr}))
{
$self->{"_${attr}_save_"} = [ @{$self->{$attr}} ]
unless defined($self->{"_${attr}_save_"});
}
else
{
$self->{"_${attr}_save_"} = []
unless defined($self->{"_${attr}_save_"});
}
$self->{"_${attr}_modified_"} = 1;
if (defined($self->{"_${attr}_deleted_"}))
{
delete $self->{"_${attr}_deleted_"};
$self->{$attr} = [$val];
}
else
{
push(@{$self->{$attr}}, $val);
}
# Potentially add the attribute to the OC order list.
if (! grep(/^$attr$/i, @{$self->{"_oc_order_"}}))
{
push(@{$self->{"_oc_order_"}}, $attr);
$self->{"_oc_numattr_"}++;
}
return 1;
}
#############################################################################
# Just like addValue(), but force the DN normalization of the value. Note
# that we also have an $norm argument here, to normalize the DN value
# before we add it.
#
sub addDNValue
{
my $self = shift;
my ($attr, $val, $force, $norm, $nocase);
if (ref $_[$[] eq "HASH")
{
my $hash = shift;
$attr = lc $hash->{"attr"};
$val = $hash->{"val"};
$force = $hash->{"force"};
$norm = $hash->{"norm"};
$nocase = $hash->{"nocase"};
}
else
{
$attr = lc shift;
($val, $force, $norm, $nocase) = @_;
}
$val = normalizeDN($val) if (defined($norm) && $norm);
return $self->addValue($attr, $val, $force, 1, $nocase);
}
#############################################################################
# Set the entire value of an attribute, removing whatever was already set.
# The arguments are the name of the attribute, and then one or more values,
# passed as scalar or an array (not pointer).
#
sub setValues
{
my $self = shift;
my $attr = lc shift;
my @vals = @_;
local $_;
return 0 unless ($#vals >= $[);
return 0 unless (defined($attr) && ($attr ne ""));
return 0 if ($attr eq "dn");
if (defined($self->{$attr}))
{
$self->{"_self_obj_"}->{"_${attr}_save_"} = [ @{$self->{$attr}} ]
unless defined($self->{"_${attr}_save_"});
}
else
{
$self->{"_self_obj_"}->{"_${attr}_save_"} = [ ]
unless defined($self->{"_${attr}_save_"});
}
$self->{$attr} = [ @vals ];
$self->{"_${attr}_modified_"} = 1;
delete $self->{"_${attr}_deleted_"}
if defined($self->{"_${attr}_deleted_"});
if (! grep(/^$attr$/i, @{$self->{"_oc_order_"}}))
{
push(@{$self->{"_oc_order_"}}, $attr);
$self->{"_oc_numattr_"}++;
}
return 1;
}
*setValue = \*setValues;
#############################################################################
# Get the entire array of attribute values. This returns the array, not
# the pointer to the array...
# in a scalar context, returns the first attribute
#
sub getValues
{
my $self = shift;
my $attr = lc shift;
return unless (defined($attr) && ($attr ne ""));
return unless defined($self->{$attr});
return (wantarray ? @{$self->{$attr}} : $self->{$attr}->[0]);
}
*getValue = \*getValues;
#############################################################################
# Return TRUE or FALSE, if the attribute has the specified value. The
# optional third argument says we should do case insensitive search.
# This is very unfortunate that $nocase and $norm are reversersed ... :-/.
#
sub hasValue
{
my $self = shift;
my ($attr, $val, $nocase, $norm, $attrval);
local $_;
if (ref $_[$[] eq "HASH")
{
my $hash = shift;
$attr = lc $hash->{"attr"};
$val = $hash->{"val"};
$nocase = $hash->{"nocase"};
$norm = $hash->{"norm"};
}
else
{
$attr = lc shift;
($val, $nocase, $norm) = @_;
}
return 0 unless (defined($val) && ($val ne ""));
return 0 unless (defined($attr) && ($attr ne ""));
return 0 unless defined($self->{$attr});
$val = normalizeDN($val) if (defined($norm) && $norm);
if ($nocase)
{
foreach $attrval (@{$self->{$attr}})
{
$_ = ((defined($norm) && $norm) ? normalizeDN($attrval) : $attrval);
return 1 if /^\Q$val\E$/i;
}
}
else
{
foreach $attrval (@{$self->{$attr}})
{
$_ = ((defined($norm) && $norm) ? normalizeDN($attrval) : $attrval);
return 1 if /^\Q$val\E$/;
}
}
return 0;
}
#############################################################################
# Just like hasValue(), but force the DN normalization of the value.
#
sub hasDNValue
{
my $self = shift;
my ($attr, $val, $nocase);
if (ref $_[$[] eq "HASH")
{
my $hash = shift;
$attr = $hash->{"attr"};
$val = $hash->{"val"};
$nocase = $hash->{"nocase"};
}
else
{
($attr, $val, $nocase) = @_;
}
# We don't need to lc the attribute, since hasValue() does that for us.
return $self->hasValue($attr, $val, $nocase, 1);
}
#############################################################################
# Return TRUE or FALSE, if the attribute matches the specified regexp. The
# optional third argument says we should do case insensitive search, and the
# optional fourth argument indicates we should normalize for DN matches.
#
sub matchValue
{
my $self = shift;
my ($attr, $regex, $nocase, $norm, $attrval);
if (ref $_[$[] eq "HASH")
{
my $hash = shift;
$attr = lc $hash->{"attr"};
$regex = $hash->{"regex"};
$nocase = $hash->{"nocase"};
$norm = $hash->{"norm"};
}
else
{
$attr = lc shift;
($regex, $nocase, $norm) = @_;
}
return 0 unless (defined($regex) && ($regex ne ""));
return 0 unless (defined($attr) && ($attr ne ""));
return 0 unless defined($self->{$attr});
if ($nocase)
{
foreach $attrval (@{$self->{$attr}})
{
$_ = ((defined($norm) && $norm) ? normalizeDN($attrval) : $attrval);
return 1 if /$regex/i;
}
}
else
{
foreach $attrval (@{$self->{$attr}})
{
$_ = ((defined($norm) && $norm) ? normalizeDN($attrval) : $attrval);
return 1 if /$regex/;
}
}
return 0;
}
#############################################################################
# Just like matchValue(), but force the DN normalization of the values.
#
sub matchDNValue
{
my $self = shift;
my ($attr, $regex, $nocase);
if (ref $_[$[] eq "HASH")
{
my $hash = shift;
$attr = $hash->{"attr"};
$regex = $hash->{"regex"};
$nocase = $hash->{"nocase"};
}
else
{
($attr, $regex, $nocase) = @_;
}
# We don't need to lc the attribute, since hasValue() does that for us.
return $self->matchValue($attr, $regex, $nocase, 1);
}
#############################################################################
# Set the DN of this entry.
#
sub setDN
{
my $self = shift;
my ($val, $norm);
if (ref $_[$[] eq "HASH")
{
my $hash = shift;
$val = $hash->{"val"};
$norm = $hash->{"norm"};
}
else
{
($val, $norm) = @_;
}
return 0 unless (defined($val) && ($val ne ""));
$val = normalizeDN($val) if (defined($norm) && $norm);
$self->{"dn"} = $val;
return 1;
}
#############################################################################
# Get the DN of this entry.
#
sub getDN
{
my ($self, $norm) = @_;
return normalizeDN($self->{"dn"}) if (defined($norm) && $norm);
return $self->{"dn"};
}
#############################################################################
#
# Return the number of elements in an attribute.
#
sub size
{
my $self = shift;
my $attr = lc shift;
return 0 unless (defined($attr) && ($attr ne ""));
return 0 unless defined($self->{$attr});
return scalar(@{$self->{$attr}});
}
#############################################################################
#
# Return LDIF entries.
#
sub getLDIFrecords # called from LDIF.pm (at least)
{
my $self = shift;
my (@record) = (dn => $self->getDN());
my ($attr, $values);
while (($attr, $values) = each %$self)
{
next if "dn" eq lc $attr; # this shouldn't happen; should it?
push @record, ($attr => $values);
# This is dangerous: @record and %$self now both contain
# references to @$values. To avoid this, copy it:
# push @record, ($attr => [@$values]);
# But that's not necessary, because the array and its
# contents are not modified as a side-effect of getting
# other attributes, from this or other objects.
}
return \@record;
}
#############################################################################
# Print an entry, in LDIF format.
#
use vars qw($_no_LDIF_module $_tested_LDIF_module);
undef $_no_LDIF_module;
undef $_tested_LDIF_module;
sub printLDIF
{
my $self = shift;
if (not defined($_tested_LDIF_module))
{
eval {require Mozilla::LDAP::LDIF; Mozilla::LDAP::LDIF->VERSION(0.07)};
$_no_LDIF_module = $@;
$_tested_LDIF_module = 1;
}
if ($_no_LDIF_module)
{
my $record = $self->getLDIFrecords();
my ($attr, $values);
while (($attr, $values) = splice @$record, 0, 2)
{
grep((print "$attr: $_\n"),
((ref($values) eq "ARRAY") ? @$values : $values));
}
print "\n";
}
else
{
Mozilla::LDAP::LDIF::put_LDIF(select(), 78, $self);
}
}
#############################################################################
# Mandatory TRUE return value.
#
1;
#############################################################################
# POD documentation...
#
__END__
=head1 NAME
Mozilla::LDAP::Entry.pm - Object class to hold one LDAP entry.
=head1 SYNOPSIS
use Mozilla::LDAP::Conn;
use Mozilla::LDAP::Entry;
=head1 ABSTRACT
The LDAP::Conn object is used to perform LDAP searches, updates, adds and
deletes. All such functions works on LDAP::Entry objects only. All
modifications and additions you'll do to an LDAP entry, will be done
through this object class.
=head1 DESCRIPTION
The LDAP::Entry object class is built on top of the Tie::Hash standard
object class. This gives us several powerful features, the main one being
to keep track of what is changing in the LDAP entry. This makes it very
easy to write LDAP clients that needs to update/modify entries, since
you'll just do the changes, and this object class will take care of the
rest.
We define local functions for STORE, FETCH, DELETE, EXISTS, FIRSTKEY and
NEXTKEY in this object class, and inherit the rest from the super
class. Overloading these specific functions is how we can keep track of
what is changing in the entry, which turns out to be very convenient. We
can also easily "loop" over the attribute types, ignoring internal data,
or deleted attributes.
Most of the methods here either return the requested LDAP value, or a
status code. The status code (either 0 or 1) indicates the failure or
success of a certain operation. 0 (False) meaning the operation failed,
and a return code of 1 (True) means complete success.
One thing to remember is that in LDAP, attribute names are case
insensitive. All methods in this class are aware of this, and will convert
all attribute name arguments to lower case before performing any
operations. This does not mean that the values are case insensitive. On
the contrary, all values are considered case sensitive by this module,
even if the LDAP server itself treats it as a CIS attribute.
=head1 OBJECT CLASS METHODS
The LDAP::Entry class implements many methods you can use to access and
modify LDAP entries. It is strongly recommended that you use this API as
much as possible, and avoid using the internals of the class
directly. Failing to do so may actually break the functionality.
=head2 Creating a new entry
To create a completely new entry, use the B<new> method, for instance
$entry = Mozilla::LDAP::Entry->new()
$entry->setDN("uid=leif,ou=people,dc=netscape,dc=com");
$entry->{objectclass} = [ "top", "person", "inetOrgPerson" ];
$entry->addValue("cn", "Leif Hedstrom");
$entry->addValue("sn", "Hedstrom");
$entry->addValue("givenName", "Leif");
$entry->addValue("mail", "leif@netscape.com);
$conn->add($entry);
This is the minimum requirements for an LDAP entry. It must have a DN, and
it must have at least one objectclass. As it turns out, by adding the
I<person> and I<inetOrgPerson> classes, we also must provide some more
attributes, like I<CN> and I<SN>. This is because the object classes have
these attributes marked as "required", and we'd get a schema violation
without those values.
In the example above we use both native API methods to add values, and
setting an attribute entire value set directly. Note that the value set is
a pointer to an array, and not the array itself. In the example above, the
object classes are set using an anonymous array, which the API handles
properly. It's important to be aware that the attribute value list is
indeed a pointer.
Finally, as you can see there's only only one way to add new LDAP entries,
and it's called add(). It normally takes an LDAP::Entry object instance as
argument, but it can also be called with a regular hash array if so
desired.
=head2 Adding and removing attributes and values
This is the main functionality of this module. Use these methods to do any
modifications and updates to your LDAP entries.
=over 13
=item B<addValue>
Add a value to an attribute. If the attribute value already exists, or we
couldn't add the value for any other reason, we'll return FALSE (0),
otherwise we return TRUE (1). The first two arguments are the attribute
name, and the value to add.
The optional third argument is a flag, indicating that we want to add the
attribute without checking for duplicates. This is useful if you know the
values are unique already, or if you perhaps want to allow duplicates for
a particular attribute. The fourth argument (again optional) is a flag
indicating that we want to perform DN normalization on the attribute. The
final, fifth, optional argument indicates that the attribute values are
case insensitive (CIS).
To add a CN to an existing entry/attribute, do:
$entry->addValue("cn", "Leif Hedstrom");
=item B<addDNValue>
Just like B<addValue>, except this method assume the value is a DN
attribute, and will enforce DN normalization. For instance
$dn = "uid=Leif, dc=Netscape, dc=COM";
$entry->addDNValue("uniqueMember", $dn);
will only add the DN for "uid=leif" if it does not exist as a DN in the
uniqueMember attribute.
=item B<attrModified>
This is an internal function, that can be used to force the API to
consider an attribute (value) to have been modified. The only argument is
the name of the attribute. In almost all situation, you never, ever,
should call this. If you do, please contact the developers, and as us to
fix the API. Example
$entry->attrModified("cn");
=item B<copy>
Copy the value of one attribute to another. Requires at least two
arguments. The first argument is the name of the attribute to copy, and
the second argument is the name of the new attribute to copy to. The new
attribute can not currently exist in the entry, else the copy will fail.
There is an optional third argument (a boolean flag), which, when set to
1, will force an
override and copy to the new attribute even if it already exists. Returns TRUE if the copy
was successful.
$entry->copy("cn", "description");
=item B<exists>
Return TRUE if the specified attribute is defined in the LDAP entry. This
is useful to know if an entry has a particular attribute, regardless of
the value. For instance:
if ($entry->exists("jpegphoto")) { # do something special }
=item B<getDN>
Return the DN for the entry. For instance
print "The DN is: ", $entry->getDN(), "\n";
Just like B<setDN>, this method also has an optional argument, which
indicates we should normalize the DN before returning it to the caller.
=item B<getValues>
Returns an entire array of values for the attribute specified. Note that
this returns an array, and not a pointer to an array. In a scalar
context, this returns the first value. This is different - this method
used to always return an array, which meant the array size in a scalar
context. If you need to get the array size, use the B<size> method
described below.
@someArray = $entry->getValues("description");
$scalval = $entry->getValues("cn");
=item B<hasValue>
Return TRUE or FALSE if the attribute has the specified value. A typical
usage is to see if an entry is of a certain object class, e.g.
if ($entry->hasValue("objectclass", "person", 1)) { # do something }
The (optional) third argument indicates if the string comparison should be
case insensitive or not, and the (optional) fourth argument indicats
whether we should normalize the string as if it was a DN. The first two
arguments are the name and value of the attribute, respectively.
=item B<hasDNValue>
Exactly like B<hasValue>, except we assume the attribute values are DN
attributes.
=item B<isAttr>
This method can be used to decide if an attribute name really is a valid
LDAP attribute in the current entry. Use of this method is fairly limited,
but could potentially be useful. Usage is like previous examples, like
if ($entry->isAttr("cn")) { # do something }
The code section will only be executed if these criterias are true:
1. The name of the attribute is a non-empty string.
2. The name of the attribute does not begin, and end, with an
underscore character (_).
2. The attribute has one or more values in the entry.
=item B<isDeleted>
This is almost identical to B<isModified>, except it tests if an attribute
has been deleted. You use it the same way as above, like
if (! $entry->isDeleted("cn")) { # do something }
=item B<isModified>
This is a somewhat more useful method, which will return the internal
modification status of a particular attribute. The argument is the name of
the attribute, and the return value is True or False. If the attribute has
been modified, in any way, we return True (1), otherwise we return False
(0). For example:
if ($entry->isModified("cn")) { # do something }
=item B<isEntryModified>
This is a wrapper over isModified(), and it will check if any attribute
in the entry object has been modified or deleted.
=item B<matchValue>
This is very similar to B<hasValue>, except it does a regular expression
match instead of a full string match. It takes the same arguments,
including the optional third argument to specify case insensitive
matching. The usage is identical to the example for hasValue, e.g.
if ($entry->matchValue("objectclass", "pers", 1)) { # do something }
=item B<matchDNValue>
Like B<matchValue>, except the attribute values are considered being DNs.
=item B<move>
Identical to the copy method, except the original attribute is
deleted once the move to the new attribute is complete.
$entry->move("cn", "sn");
=item B<printLDIF>
Print the entry in a format called LDIF (LDAP Data Interchange
Format, RFC xxxx). An example of an LDIF entry is:
dn: uid=leif,ou=people,dc=netscape,dc=com
objectclass: top
objectclass: person
objectclass: inetOrgPerson
uid: leif
cn: Leif Hedstrom
mail: leif@netscape.com
The above would be the result of
$entry->printLDIF();
If you need to write to a file, open and then select() it.
For more useful LDIF functionality, check out the
Mozilla::LDAP::LDIF.pm module.
=item B<remove>
This will remove the entire attribute, including all it's values, from the
entry. The only argument is the name of the attribute to remove. Let's say
you want to nuke all I<mailAlternateAddress> values (i.e. the entire
attribute should be removed from the entry):
$entry->remove("mailAlternateAddress");
=item B<removeValue>
Remove a value from an attribute, if it exists. Of course, if the
attribute has no such value, we won't try to remove it, and instead return
a False (0) status code. The arguments are the name of the attribute, and
the particular value to remove. Note that values are considered case
sensitive, so make sure you preserve case properly. An example is:
$entry->removeValue("objectclass", "nscpPerson");
=item B<removeDNValue>
This is almost identical to B<removeValue>, except it will normalize the
attribute values before trying to remove them. This is useful if you know
that the attribute is a DN value, but perhaps the values are not cosistent
in all LDAP entries. For example
$dn = "uid=Leif, dc=Netscape, dc=COM";
$entry->removeDNValue("owner", $dn);
will remove the owner "uid=leif,dc=netscape,dc=com", no matter how it's
capitalized and formatted in the entry.
=item B<setDN>
Set the DN to the specified value. Only do this on new entries, it will
not work well if you try to do this on an existing entry. If you wish to
rename an entry, use the Mozilla::Conn::modifyRDN method instead.
Eventually we'll provide a complete "rename" method. To set the DN for a
newly created entry, we can do
$entry->setDN("uid=leif,ou=people,dc=netscape,dc=com");
There is an optional third argument, a boolean flag, indicating that we
should normalize the DN before setting it. This will assure a consistent
format of your DNs.
=item B<setValues>
Set the specified attribute to the new value (or values), overwriting
whatever old values it had before. This is a little dangerous, since you
can lose attribute values you didn't intend to remove. Therefore, it's
usually recommended to use B<removeValue()> and B<setValues()>. If you know
exactly what the new values should be like, you can use this method like
$entry->setValues("cn", "Leif Hedstrom", "The Swede");
$entry->setValues("mail", @mailAddresses);
or if it's a single value attribute,
$entry->setValues("uidNumber", "12345");
=item B<size>
Return the number of values for a particular attribute. For instance
$entry->{cn} = [ "Leif Hedstrom", "The Swede" ];
$numVals = $entry->size("cn");
This will set C<$numVals> to two (2). The only argument is the name of the
attribute, and the return value is the size of the value array.
=back
=head2 Deleting entries
To delete an LDAP entry from the LDAP server, you have to use the
B<delete> method from the Mozilla::LDAP::Conn module. It will actually
delete any entry, if you provide an legitimate DN.
=head2 Renaming entries
Again, there's no functionality in this object class to rename the entry
(i.e. changing it's DN). For now, there is a way to modify the RDN
component of a DN through the Mozilla::LDAP::Conn module, with
B<modifyRDN>. Eventually we hope to have a complete B<rename> method,
which should be capable of renaming any entry, in any way, including
moving it to a different part of the DIT (Directory Information Tree).
=head1 EXAMPLES
There are plenty of examples to look at, in the examples directory. We are
adding more examples every day (almost).
=head1 INSTALLATION
Installing this package is part of the Makefile supplied in the
package. See the installation procedures which are part of this package.
=head1 AVAILABILITY
This package can be retrieved from a number of places, including:
http://www.mozilla.org/directory/
Your local CPAN server
=head1 CREDITS
Most of this code was developed by Leif Hedstrom, Netscape Communications
Corporation.
=head1 BUGS
None. :)
=head1 SEE ALSO
L<Mozilla::LDAP::Conn>, L<Mozilla::LDAP::API>, and of course L<Perl>.
=cut
|