/usr/share/perl5/Net/LDAP/LDIF.pm is in libnet-ldap-perl 1:0.4300-2ubuntu1.
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 | # Copyright (c) 1997-2008 Graham Barr <gbarr@pobox.com>. All rights reserved.
# This program is free software; you can redistribute it and/or
# modify it under the same terms as Perl itself.
package Net::LDAP::LDIF;
use strict;
use SelectSaver;
require Net::LDAP::Entry;
use vars qw($VERSION);
use constant CHECK_UTF8 => $] > 5.007;
BEGIN {
require Encode
if (CHECK_UTF8);
}
$VERSION = "0.18";
my %mode = qw(w > r < a >>);
sub new {
my $pkg = shift;
my $file = shift || "-";
my $mode = shift || "r";
my %opt = @_;
my $fh;
my $opened_fh = 0;
if (ref($file)) {
$fh = $file;
}
else {
if ($file eq "-") {
if ($mode eq "w") {
($file,$fh) = ("STDOUT",\*STDOUT);
}
else {
($file,$fh) = ("STDIN",\*STDIN);
}
}
else {
require Symbol;
$fh = Symbol::gensym();
my $open = $file =~ /^\| | \|$/x
? $file
: (($mode{$mode} || "<") . $file);
open($fh,$open) or return;
$opened_fh = 1;
}
}
# Default the encoding of DNs to 'none' unless the user specifies
$opt{'encode'} = 'none' unless exists $opt{'encode'};
# Default the error handling to die
$opt{'onerror'} = 'die' unless exists $opt{'onerror'};
# sanitize options
$opt{'lowercase'} ||= 0;
$opt{'change'} ||= 0;
$opt{'sort'} ||= 0;
$opt{'version'} ||= 0;
my $self = {
changetype => "modify",
modify => 'add',
wrap => 78,
%opt,
fh => $fh,
file => "$file",
opened_fh => $opened_fh,
_eof => 0,
write_count => ($mode eq 'a' and tell($fh) > 0) ? 1 : 0,
};
# fetch glob for URL type attributes (one per LDIF object)
if ($mode eq "r") {
require Symbol;
$self->{_attr_fh} = Symbol::gensym();
}
bless $self, $pkg;
}
sub _read_lines {
my $self = shift;
my $fh = $self->{'fh'};
my @ldif = ();
my $entry = '';
my $in_comment = 0;
my $entry_completed = 0;
my $ln;
return @ldif if ($self->eof());
while (defined($ln = $self->{_buffered_line} || scalar <$fh>)) {
delete($self->{_buffered_line});
if ($ln =~ /^#/o) { # ignore 1st line of comments
$in_comment = 1;
}
else {
if ($ln =~ /^[ \t]/o) { # append wrapped line (if not in a comment)
$entry .= $ln if (!$in_comment);
}
else {
$in_comment = 0;
if ($ln =~ /^\r?\n$/o) {
# ignore empty line on start of entry
# empty line at non-empty entry indicate entry completion
$entry_completed++ if (length($entry));
}
else {
if ($entry_completed) {
$self->{_buffered_line} = $ln;
last;
}
else {
# append non-empty line
$entry .= $ln;
}
}
}
}
}
$self->eof(1) if (!defined($ln));
$self->{_current_lines} = $entry;
$entry =~ s/\r?\n //sgo; # un-wrap wrapped lines
$entry =~ s/\r?\n\t/ /sgo; # OpenLDAP extension !!!
@ldif = split(/^/, $entry);
map { s/\r?\n$//; } @ldif;
@ldif;
}
# read attribute value from URL (currently only file: URLs)
sub _read_url_attribute {
my $self = shift;
my $url = shift;
my @ldif = @_;
my $line;
if ($url =~ s/^file:(?:\/\/)?//) {
my $fh = $self->{_attr_fh};
unless (open($fh, '<'.$url)) {
$self->_error("can't open $line: $!", @ldif);
return;
}
binmode($fh);
{ # slurp in whole file at once
local $/;
$line = <$fh>;
}
close($fh);
} else {
$self->_error("unsupported URL type", @ldif);
return;
}
$line;
}
# _read_one() is deprecated and will be removed
# in a future version
*_read_one = \&_read_entry;
sub _read_entry {
my $self = shift;
my @ldif;
$self->_clear_error();
@ldif = $self->_read_lines;
unless (@ldif) { # empty records are errors if not at eof
$self->_error("illegal empty LDIF entry") if (!$self->eof());
return;
}
if (@ldif and $ldif[0] =~ /^version:\s+(\d+)/) {
$self->{'version'} = $1;
shift @ldif;
return $self->_read_entry
unless @ldif;
}
if (@ldif < 1) {
$self->_error("LDIF entry is not valid", @ldif);
return;
}
elsif (not ( $ldif[0] =~ s/^dn:(:?) *//) ) {
$self->_error("First line of LDIF entry does not begin with 'dn:'", @ldif);
return;
}
my $dn = shift @ldif;
if (length($1)) { # $1 is the optional colon from above
eval { require MIME::Base64 };
if ($@) {
$self->_error($@, @ldif);
return;
}
$dn = MIME::Base64::decode($dn);
}
my $entry = Net::LDAP::Entry->new;
$dn = Encode::decode_utf8($dn)
if (CHECK_UTF8 && $self->{raw} && ('dn' !~ /$self->{raw}/));
$entry->dn($dn);
if ((scalar @ldif) && ($ldif[0] =~ /^changetype:\s*/)) {
my $changetype = $ldif[0] =~ s/^changetype:\s*//
? shift(@ldif) : $self->{'changetype'};
$entry->changetype($changetype);
return $entry if ($changetype eq "delete");
unless (@ldif) {
$self->_error("LDAP entry is not valid",@ldif);
return;
}
while(@ldif) {
my $modify = $self->{'modify'};
my $modattr;
my $lastattr;
if($changetype eq "modify") {
unless ( (my $tmp = shift @ldif) =~ s/^(add|delete|replace|increment):\s*([-;\w]+)// ) {
$self->_error("LDAP entry is not valid",@ldif);
return;
}
$lastattr = $modattr = $2;
$modify = $1;
}
my @values;
while(@ldif) {
my $line = shift @ldif;
my $attr;
my $xattr;
if ($line eq "-") {
if (defined $lastattr) {
if (CHECK_UTF8 && $self->{raw}) {
map { $_ = Encode::decode_utf8($_) } @values
if ($lastattr !~ /$self->{raw}/);
}
$entry->$modify($lastattr, \@values);
}
undef $lastattr;
@values = ();
last;
}
$line =~ s/^([-;\w]+):([\<\:]?)\s*// and
($attr, $xattr) = ($1, $2);
# base64 encoded attribute: decode it
if ($xattr eq ':') {
eval { require MIME::Base64 };
if ($@) {
$self->_error($@, @ldif);
return;
}
$line = MIME::Base64::decode($line);
}
# url attribute: read in file:// url, fail on others
elsif ($xattr eq '<' and $line =~ s/^(.*?)\s*$/$1/) {
$line = $self->_read_url_attribute($line, @ldif);
return if !defined($line);
}
if( defined($modattr) && $attr ne $modattr ) {
$self->_error("LDAP entry is not valid", @ldif);
return;
}
if(!defined($lastattr) || $lastattr ne $attr) {
if (defined $lastattr) {
if (CHECK_UTF8 && $self->{raw}) {
map { $_ = Encode::decode_utf8($_) } @values
if ($lastattr !~ /$self->{raw}/);
}
$entry->$modify($lastattr, \@values);
}
$lastattr = $attr;
@values = ($line);
next;
}
push @values, $line;
}
if (defined $lastattr) {
if (CHECK_UTF8 && $self->{raw}) {
map { $_ = Encode::decode_utf8($_) } @values
if ($lastattr !~ /$self->{raw}/);
}
$entry->$modify($lastattr, \@values);
}
}
}
else {
my @attr;
my $last = "";
my $vals = [];
my $line;
my $attr;
my $xattr;
foreach $line (@ldif) {
$line =~ s/^([-;\w]+):([\<\:]?)\s*// &&
(($attr, $xattr) = ($1, $2)) or next;
# base64 encoded attribute: decode it
if ($xattr eq ':') {
eval { require MIME::Base64 };
if ($@) {
$self->_error($@, @ldif);
return;
}
$line = MIME::Base64::decode($line);
}
# url attribute: read in file:// url, fail on others
elsif ($xattr eq '<' and $line =~ s/^(.*?)\s*$/$1/) {
$line = $self->_read_url_attribute($line, @ldif);
return if !defined($line);
}
if (CHECK_UTF8 && $self->{raw}) {
$line = Encode::decode_utf8($line)
if ($attr !~ /$self->{raw}/);
}
if ($attr eq $last) {
push @$vals, $line;
next;
}
else {
$vals = [$line];
push(@attr,$last=$attr,$vals);
}
}
$entry->add(@attr);
}
$self->{_current_entry} = $entry;
$entry;
}
sub read_entry {
my $self = shift;
unless ($self->{'fh'}) {
$self->_error("LDIF file handle not valid");
return;
}
$self->_read_entry();
}
# read() is deprecated and will be removed
# in a future version
sub read {
my $self = shift;
return $self->read_entry() unless wantarray;
my($entry, @entries);
push(@entries,$entry) while $entry = $self->read_entry;
@entries;
}
sub eof {
my $self = shift;
my $eof = shift;
if ($eof) {
$self->{_eof} = $eof;
}
$self->{_eof};
}
sub _wrap {
my $len=$_[1]; # needs to be >= 2 to avoid division by zero
return $_[0] if length($_[0]) <= $len or $len <= 40;
use integer;
my $l2 = $len-1;
my $x = (length($_[0]) - $len) / $l2;
my $extra = (length($_[0]) == ($l2 * $x + $len)) ? "" : "a*";
join("\n ",unpack("a$len" . "a$l2" x $x . $extra,$_[0]));
}
sub _write_attr {
my($attr,$val,$wrap,$lower) = @_;
my $v;
my $res = 1; # result value
foreach $v (@$val) {
my $ln = $lower ? lc $attr : $attr;
$v = Encode::encode_utf8($v)
if (CHECK_UTF8 and Encode::is_utf8($v));
if ($v =~ /(^[ :<]|[\x00-\x1f\x7f-\xff])/) {
require MIME::Base64;
$ln .= ":: " . MIME::Base64::encode($v,"");
}
else {
$ln .= ": " . $v;
}
$res &&= print _wrap($ln,$wrap),"\n";
}
$res;
}
# helper function to compare attribute names (sort objectClass first)
sub _cmpAttrs {
($a =~ /^objectclass$/io)
? -1 : (($b =~ /^objectclass$/io) ? 1 : ($a cmp $b));
}
sub _write_attrs {
my($entry,$wrap,$lower,$sort) = @_;
my @attributes = $entry->attributes();
my $attr;
my $res = 1; # result value
@attributes = sort _cmpAttrs @attributes if ($sort);
foreach $attr (@attributes) {
my $val = $entry->get_value($attr, asref => 1);
$res &&= _write_attr($attr,$val,$wrap,$lower);
}
$res;
}
sub _write_dn {
my($dn,$encode,$wrap) = @_;
$dn = Encode::encode_utf8($dn)
if (CHECK_UTF8 and Encode::is_utf8($dn));
if ($dn =~ /^[ :<]|[\x00-\x1f\x7f-\xff]/) {
if ($encode =~ /canonical/i) {
require Net::LDAP::Util;
$dn = Net::LDAP::Util::canonical_dn($dn);
# Canonicalizer won't fix leading spaces, colons or less-thans, which
# are special in LDIF, so we fix those up here.
$dn =~ s/^([ :<])/\\$1/;
$dn = "dn: $dn";
} elsif ($encode =~ /base64/i) {
require MIME::Base64;
$dn = "dn:: " . MIME::Base64::encode($dn,"");
} else {
$dn = "dn: $dn";
}
} else {
$dn = "dn: $dn";
}
print _wrap($dn,$wrap), "\n";
}
# write() is deprecated and will be removed
# in a future version
sub write {
my $self = shift;
$self->_write_entry(0, @_);
}
sub write_entry {
my $self = shift;
$self->_write_entry($self->{change}, @_);
}
sub write_version {
my $self = shift;
my $res = 1;
$res &&= print "version: $self->{'version'}\n"
if ($self->{'version'} && !$self->{version_written}++);
return $res;
}
# internal helper: write entry in different format depending on 1st arg
sub _write_entry {
my $self = shift;
my $change = shift;
my $entry;
my $wrap = int($self->{'wrap'});
my $lower = $self->{'lowercase'};
my $sort = $self->{'sort'};
my $res = 1; # result value
local($\,$,); # output field and record separators
unless ($self->{'fh'}) {
$self->_error("LDIF file handle not valid");
return;
}
my $saver = SelectSaver->new($self->{'fh'});
my $fh = $self->{'fh'};
foreach $entry (@_) {
unless (ref $entry) {
$self->_error("Entry '$entry' is not a valid Net::LDAP::Entry object.");
$res = 0;
next;
}
if ($change) {
my @changes = $entry->changes;
my $type = $entry->changetype;
# Skip entry if there is nothing to write
next if $type eq 'modify' and !@changes;
$res &&= $self->write_version() unless $self->{write_count}++;
$res &&= print "\n";
$res &&= _write_dn($entry->dn,$self->{'encode'},$wrap);
$res &&= print "changetype: $type\n";
if ($type eq 'delete') {
next;
}
elsif ($type eq 'add') {
$res &&= _write_attrs($entry,$wrap,$lower,$sort);
next;
}
elsif ($type =~ /modr?dn/o) {
my $deleteoldrdn = $entry->get_value('deleteoldrdn') || 0;
$res &&= _write_attr('newrdn',$entry->get_value('newrdn', asref => 1),$wrap,$lower);
$res &&= print 'deleteoldrdn: ', $deleteoldrdn,"\n";
my $ns = $entry->get_value('newsuperior', asref => 1);
$res &&= _write_attr('newsuperior',$ns,$wrap,$lower) if defined $ns;
next;
}
my $dash=0;
foreach my $chg (@changes) {
unless (ref($chg)) {
$type = $chg;
next;
}
my $i = 0;
while ($i < @$chg) {
$res &&= print "-\n" if (!$self->{'version'} && $dash++);
my $attr = $chg->[$i++];
my $val = $chg->[$i++];
$res &&= print $type,": ",$attr,"\n";
$res &&= _write_attr($attr,$val,$wrap,$lower);
$res &&= print "-\n" if ($self->{'version'});
}
}
}
else {
$res &&= $self->write_version() unless $self->{write_count}++;
$res &&= print "\n";
$res &&= _write_dn($entry->dn,$self->{'encode'},$wrap);
$res &&= _write_attrs($entry,$wrap,$lower,$sort);
}
}
$res;
}
# read_cmd() is deprecated in favor of read_entry()
# and will be removed in a future version
sub read_cmd {
my $self = shift;
return $self->read_entry() unless wantarray;
my($entry, @entries);
push(@entries,$entry) while $entry = $self->read_entry;
@entries;
}
# _read_one_cmd() is deprecated in favor of _read_one()
# and will be removed in a future version
*_read_one_cmd = \&_read_entry;
# write_cmd() is deprecated in favor of write_entry()
# and will be removed in a future version
sub write_cmd {
my $self = shift;
$self->_write_entry(1, @_);
}
sub done {
my $self = shift;
my $res = 1; # result value
if ($self->{fh}) {
if ($self->{opened_fh}) {
$res = close $self->{fh};
undef $self->{opened_fh};
}
delete $self->{fh};
}
$res;
}
sub handle {
my $self = shift;
return $self->{fh};
}
my %onerror = (
'die' => sub {
my $self = shift;
require Carp;
$self->done;
Carp::croak($self->error(@_));
},
'warn' => sub {
my $self = shift;
require Carp;
Carp::carp($self->error(@_));
},
'undef' => sub {
my $self = shift;
require Carp;
Carp::carp($self->error(@_)) if $^W;
},
);
sub _error {
my ($self,$errmsg,@errlines) = @_;
$self->{_err_msg} = $errmsg;
$self->{_err_lines} = join "\n",@errlines;
scalar &{ $onerror{ $self->{onerror} } }($self,$self->{_err_msg}) if $self->{onerror};
}
sub _clear_error {
my $self = shift;
undef $self->{_err_msg};
undef $self->{_err_lines};
}
sub error {
my $self = shift;
$self->{_err_msg};
}
sub error_lines {
my $self = shift;
$self->{_err_lines};
}
sub current_entry {
my $self = shift;
$self->{_current_entry};
}
sub current_lines {
my $self = shift;
$self->{_current_lines};
}
sub version {
my $self = shift;
return $self->{'version'} unless @_;
$self->{'version'} = shift || 0;
}
sub next_lines {
my $self = shift;
$self->{_next_lines};
}
sub DESTROY {
my $self = shift;
$self->done();
}
1;
|