| 1 | # Connector::Proxy::Net::LDAP | ||
| 2 | # | ||
| 3 | # Proxy class for accessing LDAP directories | ||
| 4 | # | ||
| 5 | # Written by Scott Hardin, Martin Bartosch and Oliver Welter for the OpenXPKI project 2012 | ||
| 6 | # | ||
| 7 | package Connector::Proxy::Net::LDAP; | ||
| 8 | |||
| 9 | use strict; | ||
| 10 | use warnings; | ||
| 11 | use English; | ||
| 12 | use Net::LDAP; | ||
| 13 | use Template; | ||
| 14 | use Data::Dumper; | ||
| 15 | |||
| 16 | use Moose; | ||
| 17 | extends 'Connector::Proxy'; | ||
| 18 | |||
| 19 | has base => ( | ||
| 20 | is => 'rw', | ||
| 21 | isa => 'Str', | ||
| 22 | required => 1, | ||
| 23 | ); | ||
| 24 | |||
| 25 | has binddn => ( | ||
| 26 | is => 'rw', | ||
| 27 | isa => 'Str', | ||
| 28 | ); | ||
| 29 | |||
| 30 | has password => ( | ||
| 31 | is => 'rw', | ||
| 32 | isa => 'Str', | ||
| 33 | ); | ||
| 34 | |||
| 35 | has filter => ( | ||
| 36 | is => 'rw', | ||
| 37 | # TODO: this does not work (currently); NB: do we need that? | ||
| 38 | # isa => 'Str|Net::LDAP::Filter', | ||
| 39 | isa => 'Str', | ||
| 40 | required => 1, | ||
| 41 | ); | ||
| 42 | |||
| 43 | has attrs => ( | ||
| 44 | is => 'rw', | ||
| 45 | isa => 'ArrayRef|Str', | ||
| 46 | trigger => \&_convert_attrs | ||
| 47 | ); | ||
| 48 | |||
| 49 | has scope => ( | ||
| 50 | is => 'rw', | ||
| 51 | isa => 'Str', | ||
| 52 | ); | ||
| 53 | |||
| 54 | has timeout => ( | ||
| 55 | is => 'rw', | ||
| 56 | isa => 'Int', | ||
| 57 | ); | ||
| 58 | |||
| 59 | has keepalive => ( | ||
| 60 | is => 'rw', | ||
| 61 | isa => 'Int', | ||
| 62 | ); | ||
| 63 | |||
| 64 | has timelimit => ( | ||
| 65 | is => 'rw', | ||
| 66 | isa => 'Int', | ||
| 67 | ); | ||
| 68 | |||
| 69 | has sizelimit => ( | ||
| 70 | is => 'rw', | ||
| 71 | isa => 'Int', | ||
| 72 | ); | ||
| 73 | |||
| 74 | has multihomed => ( | ||
| 75 | is => 'rw', | ||
| 76 | isa => 'Int', | ||
| 77 | ); | ||
| 78 | |||
| 79 | has localaddr => ( | ||
| 80 | is => 'rw', | ||
| 81 | isa => 'Str', | ||
| 82 | ); | ||
| 83 | |||
| 84 | has raw => ( | ||
| 85 | is => 'rw', | ||
| 86 | isa => 'RegexpRef|Undef', | ||
| 87 | default => sub { return qr/(?i:;binary)/; }, | ||
| 88 | ); | ||
| 89 | |||
| 90 | has debug => ( | ||
| 91 | is => 'rw', | ||
| 92 | isa => 'Int', | ||
| 93 | default => 0, | ||
| 94 | ); | ||
| 95 | |||
| 96 | # ssl certificate options in SSLUserAgent format | ||
| 97 | has certificate_file => ( | ||
| 98 | is => 'rw', | ||
| 99 | isa => 'Str', | ||
| 100 | ); | ||
| 101 | |||
| 102 | has certificate_key_file => ( | ||
| 103 | is => 'rw', | ||
| 104 | isa => 'Str', | ||
| 105 | ); | ||
| 106 | |||
| 107 | has ca_certificate_path => ( | ||
| 108 | is => 'rw', | ||
| 109 | isa => 'Str', | ||
| 110 | ); | ||
| 111 | |||
| 112 | has ca_certificate_file => ( | ||
| 113 | is => 'rw', | ||
| 114 | isa => 'Str', | ||
| 115 | ); | ||
| 116 | |||
| 117 | has ssl_ignore_mode => ( | ||
| 118 | is => 'rw', | ||
| 119 | isa => 'Bool', | ||
| 120 | default => 0, | ||
| 121 | ); | ||
| 122 | |||
| 123 | # ssl options in Net::LDAP format | ||
| 124 | has verify => ( | ||
| 125 | is => 'rw', | ||
| 126 | isa => 'Str|Undef', | ||
| 127 | lazy => 1, | ||
| 128 | default => sub { shift->ssl_ignore_mode() ? 'none' : undef } | ||
| 129 | ); | ||
| 130 | |||
| 131 | has capath => ( | ||
| 132 | is => 'rw', | ||
| 133 | isa => 'Str|Undef', | ||
| 134 | lazy => 1, | ||
| 135 | default => sub { shift->ca_certificate_path(); } | ||
| 136 | ); | ||
| 137 | |||
| 138 | has cafile => ( | ||
| 139 | is => 'rw', | ||
| 140 | isa => 'Str|Undef', | ||
| 141 | lazy => 1, | ||
| 142 | default => sub { shift->ca_certificate_file(); } | ||
| 143 | ); | ||
| 144 | |||
| 145 | has sslversion => ( | ||
| 146 | is => 'rw', | ||
| 147 | isa => 'Str', | ||
| 148 | ); | ||
| 149 | |||
| 150 | has ciphers => ( | ||
| 151 | is => 'rw', | ||
| 152 | isa => 'Str', | ||
| 153 | ); | ||
| 154 | |||
| 155 | has clientcert => ( | ||
| 156 | is => 'rw', | ||
| 157 | isa => 'Str|Undef', | ||
| 158 | lazy => 1, | ||
| 159 | default => sub { shift->certificate_file(); } | ||
| 160 | ); | ||
| 161 | |||
| 162 | has clientkey => ( | ||
| 163 | is => 'rw', | ||
| 164 | isa => 'Str|Undef', | ||
| 165 | lazy => 1, | ||
| 166 | default => sub { shift->certificate_key_file(); } | ||
| 167 | ); | ||
| 168 | |||
| 169 | has checkcrl => ( | ||
| 170 | is => 'rw', | ||
| 171 | isa => 'Int', | ||
| 172 | ); | ||
| 173 | |||
| 174 | has bind => ( | ||
| 175 | is => 'ro', | ||
| 176 | isa => 'Net::LDAP', | ||
| 177 | reader => '_bind', | ||
| 178 | builder => '_init_bind', | ||
| 179 | clearer => '_purge_bind', | ||
| 180 | lazy => 1, | ||
| 181 | ); | ||
| 182 | |||
| 183 | has action => ( | ||
| 184 | is => 'rw', | ||
| 185 | isa => 'Str', | ||
| 186 | lazy => 1, | ||
| 187 | default => 'replace', | ||
| 188 | ); | ||
| 189 | |||
| 190 | has create => ( | ||
| 191 | is => 'ro', | ||
| 192 | isa => 'HashRef', | ||
| 193 | default => sub { return {} }, | ||
| 194 | ); | ||
| 195 | |||
| 196 | has schema => ( | ||
| 197 | is => 'ro', | ||
| 198 | isa => 'HashRef', | ||
| 199 | ); | ||
| 200 | |||
| 201 | sub _build_config { | ||
| 202 | my $self = shift; | ||
| 203 | |||
| 204 | } | ||
| 205 | |||
| 206 | sub _build_options { | ||
| 207 | my $self = shift; | ||
| 208 | |||
| 209 | my %options; | ||
| 210 | foreach my $key (@_) { | ||
| 211 | if (defined $self->$key()) { | ||
| 212 | $options{$key} = $self->$key(); | ||
| 213 | } | ||
| 214 | } | ||
| 215 | return %options; | ||
| 216 | } | ||
| 217 | |||
| 218 | sub _build_new_options { | ||
| 219 | my $self = shift; | ||
| 220 | return $self->_build_options(qw( | ||
| 221 | timeout verify keepalive debug raw multihomed localaddr | ||
| 222 | verify sslversion ciphers capath cafile capath clientcert clientkey checkcrl | ||
| 223 | )); | ||
| 224 | } | ||
| 225 | |||
| 226 | sub _build_bind_options { | ||
| 227 | my $self = shift; | ||
| 228 | return $self->_build_options(qw( password )); | ||
| 229 | } | ||
| 230 | |||
| 231 | # the argument passed to this method will be used as template parameters | ||
| 232 | # in the expansion of the filter attribute | ||
| 233 | sub _build_search_options { | ||
| 234 | my $self = shift; | ||
| 235 | my $arg = shift; | ||
| 236 | my $params = shift; | ||
| 237 | |||
| 238 | my %options = $self->_build_options(qw( base scope sizelimit timelimit )); | ||
| 239 | |||
| 240 | my $filter = $self->filter(); | ||
| 241 | |||
| 242 | # template expansion is performed on filter strings only, not | ||
| 243 | # on Net::LDAP::Filter objects | ||
| 244 | my $value; | ||
| 245 | if (ref $filter eq '') { | ||
| 246 | Template->new()->process(\$filter, $arg, \$value) || $self->_log_and_die("Error processing argument template."); | ||
| 247 | $options{filter} = $value; | ||
| 248 | } else { | ||
| 249 | $options{filter} = $filter; | ||
| 250 | } | ||
| 251 | |||
| 252 | |||
| 253 | # Add the attributes to the query to return only the ones we are asked for | ||
| 254 | # Will not work if we allow Filters | ||
| 255 | $options{attrs} = $self->attrs unless( $params->{noattrs} ); | ||
| 256 | |||
| 257 | $self->log()->debug('LDAP Search options ' . Dumper %options); | ||
| 258 | |||
| 259 | return %options; | ||
| 260 | } | ||
| 261 | |||
| 262 | # If the attr property is set using a string (necessary atm for Config::Std) | ||
| 263 | # its converted to an arrayref. Might be removed if Config::* improves | ||
| 264 | # This might create indefinite loops if something goes wrong on the conversion! | ||
| 265 | sub _convert_attrs { | ||
| 266 | my ( $self, $new, $old ) = @_; | ||
| 267 | |||
| 268 | # Test if the given value is a non empty scalar | ||
| 269 | if ($new && !ref $new && (!$old || $new ne $old)) { | ||
| 270 | my @attrs = split(" ", $new); | ||
| 271 | $self->attrs( \@attrs ) | ||
| 272 | } | ||
| 273 | |||
| 274 | } | ||
| 275 | |||
| 276 | sub _init_bind { | ||
| 277 | |||
| 278 | my $self = shift; | ||
| 279 | |||
| 280 | $self->log()->debug('Open bind to to ' . $self->LOCATION()); | ||
| 281 | |||
| 282 | my $ldap = Net::LDAP->new( | ||
| 283 | $self->LOCATION(), | ||
| 284 | $self->_build_new_options(), | ||
| 285 | ); | ||
| 286 | |||
| 287 | if (! $ldap) { | ||
| 288 | $self->_log_and_die("Could not instantiate ldap object ($@)"); | ||
| 289 | } | ||
| 290 | |||
| 291 | $self->rebind($ldap); | ||
| 292 | return $ldap; | ||
| 293 | } | ||
| 294 | |||
| 295 | sub rebind() { | ||
| 296 | |||
| 297 | my $self = shift; | ||
| 298 | # this as the potential for an inifinte loop so be careful ;) | ||
| 299 | my $ldap = shift || $self->ldap(); | ||
| 300 | |||
| 301 | my $mesg; | ||
| 302 | if (defined $self->binddn()) { | ||
| 303 | $self->log()->debug('Binding with ' . $self->binddn()); | ||
| 304 | my %options = $self->_build_bind_options(); | ||
| 305 | $self->log()->warn('Binding with DN but without password') if (!defined $options{password}); | ||
| 306 | $mesg = $ldap->bind( | ||
| 307 | $self->binddn(), | ||
| 308 | %options, | ||
| 309 | ); | ||
| 310 | } else { | ||
| 311 | # anonymous bind | ||
| 312 | $self->log()->debug('Binding anonymously'); | ||
| 313 | $mesg = $ldap->bind( | ||
| 314 | $self->_build_bind_options(), | ||
| 315 | ); | ||
| 316 | } | ||
| 317 | |||
| 318 | if ($mesg->is_error()) { | ||
| 319 | $self->_log_and_die(sprintf("LDAP bind failed with error code %s (error: %s)", $mesg->code(), $mesg->error_desc())); | ||
| 320 | } | ||
| 321 | |||
| 322 | return $mesg; | ||
| 323 | } | ||
| 324 | |||
| 325 | sub ldap { | ||
| 326 | # ToDo - check if still bound | ||
| 327 | my $self = shift; | ||
| 328 | return $self->_bind; | ||
| 329 | } | ||
| 330 | |||
| 331 | # It looks like a half closed connection (server gone / load balancer etc) | ||
| 332 | # causes an operational error and not a connection error | ||
| 333 | # so the list of codes to use this reconnect foo is somewhat experimental | ||
| 334 | sub _is_transient_error { | ||
| 335 | my $self = shift; | ||
| 336 | my $mesg = shift; | ||
| 337 | if ($mesg->is_error() && (grep { $_ == $mesg->code() } (1,81,82))) { | ||
| 338 | $self->log()->debug('Connection lost - try rebind and rerun query'); | ||
| 339 | $self->_purge_bind(); | ||
| 340 | return 1; | ||
| 341 | } | ||
| 342 | return 0; | ||
| 343 | } | ||
| 344 | |||
| 345 | sub _getbyDN { | ||
| 346 | |||
| 347 | my $self = shift; | ||
| 348 | my $dn = shift; | ||
| 349 | my $params = shift; | ||
| 350 | |||
| 351 | my $ldap = $self->ldap(); | ||
| 352 | |||
| 353 | my %options = $self->_build_options(qw( sizelimit timelimit )); | ||
| 354 | $options{attrs} = $params->{attrs} if ($params->{attrs}); | ||
| 355 | |||
| 356 | my $mesg = $ldap->search( base => $dn, scope => 'base', filter => '(objectclass=*)', %options ); | ||
| 357 | |||
| 358 | if ($self->_is_transient_error($mesg)) { | ||
| 359 | $mesg = $ldap->search( base => $dn, scope => 'base', filter => '(objectclass=*)', %options ); | ||
| 360 | } | ||
| 361 | |||
| 362 | if ($mesg->is_error()) { | ||
| 363 | $self->log()->debug("LDAP getByDN failed with error code " . $mesg->code() . " (error: " . $mesg->error_desc() .")" ); | ||
| 364 | return; | ||
| 365 | } | ||
| 366 | |||
| 367 | if ( $mesg->count() == 1) { | ||
| 368 | |||
| 369 | my $entry = $mesg->entry(0); | ||
| 370 | # For testing | ||
| 371 | if (lc($entry->dn()) ne lc($dn)) { | ||
| 372 | $self->log()->warn('Search by DN with result looks fishy. Request: '.$dn.' - Entry: '.$entry->dn()); | ||
| 373 | } | ||
| 374 | return $entry; | ||
| 375 | } | ||
| 376 | |||
| 377 | # Query is ambigous - can this happen ? | ||
| 378 | if ( $mesg->count() > 1) { | ||
| 379 | $self->_log_and_die("There is more than one matching node.", | ||
| 380 | sprintf('Search by DN got multiple (%01d) results (%s)', $mesg->count(), $dn)); | ||
| 381 | } | ||
| 382 | |||
| 383 | # If autocreation is not requested, return undef | ||
| 384 | if (!$params->{create}) { | ||
| 385 | return; | ||
| 386 | } | ||
| 387 | |||
| 388 | # No match, so split up the DN and walk upwards | ||
| 389 | my $base_dn = $self->base; | ||
| 390 | |||
| 391 | # we cant do much if the base dn does not exists | ||
| 392 | if ($dn eq $base_dn) { | ||
| 393 | $self->_log_and_die('Request to auto-create the base dn'); | ||
| 394 | } | ||
| 395 | |||
| 396 | # Strip the basedn from the dn and tokenize the rest | ||
| 397 | my $path = $dn; | ||
| 398 | $path =~ s/$base_dn\z//; | ||
| 399 | |||
| 400 | if (!$path) { | ||
| 401 | $self->_log_and_die('Request to auto-create empty path'); | ||
| 402 | } | ||
| 403 | |||
| 404 | my @dn_attributes = $self->_splitDN( $path ); | ||
| 405 | |||
| 406 | my $currentPath = $base_dn; | ||
| 407 | my @nextComponent; | ||
| 408 | my $i; | ||
| 409 | for ($i = scalar(@dn_attributes)-1; $i >= 0; $i--) { | ||
| 410 | |||
| 411 | # For the moment we just implement single value components | ||
| 412 | my $nextComponentKey = $dn_attributes[$i][0]; | ||
| 413 | my $nextComponentValue = $dn_attributes[$i][1]; | ||
| 414 | |||
| 415 | my $nextComponent = $nextComponentKey.'='.$nextComponentValue; | ||
| 416 | |||
| 417 | # Search for the next node | ||
| 418 | #print "Probe $currentPath - $nextComponent: "; | ||
| 419 | $mesg = $ldap->search( base => $currentPath, scope => 'one', filter => '('.$nextComponent.')' ); | ||
| 420 | |||
| 421 | # Lost connection, try to rebind and rerun query | ||
| 422 | if ($self->_is_transient_error($mesg)) { | ||
| 423 | $mesg = $ldap->search( base => $currentPath, scope => 'one', filter => '('.$nextComponent.')' ); | ||
| 424 | } | ||
| 425 | |||
| 426 | # found, push to path and test next | ||
| 427 | if ( $mesg->count() == 1) { | ||
| 428 | #print "Found\n"; | ||
| 429 | $currentPath = $nextComponent.','.$currentPath; | ||
| 430 | next; | ||
| 431 | } | ||
| 432 | |||
| 433 | #print Dumper( $mesg ); | ||
| 434 | #print "not Found - i: $i\n\n"; | ||
| 435 | |||
| 436 | # Reuse counter and list to build the missing nodes | ||
| 437 | while ($i >= 0) { | ||
| 438 | $nextComponentKey = $dn_attributes[$i][0]; | ||
| 439 | $nextComponentValue = $dn_attributes[$i][1]; | ||
| 440 | $currentPath = $self->_createPathItem($currentPath, $nextComponentKey, $nextComponentValue); | ||
| 441 | $i--; | ||
| 442 | } | ||
| 443 | } | ||
| 444 | |||
| 445 | return $self->_getbyDN( $dn ); | ||
| 446 | } | ||
| 447 | |||
| 448 | sub _createPathItem { | ||
| 449 | |||
| 450 | my $self = shift; | ||
| 451 | $self->log()->trace("Create Path called with " . Dumper \@_) if ($self->log()->is_trace); | ||
| 452 | |||
| 453 | my $currentPath = shift; | ||
| 454 | my $nextComponentKey = shift; | ||
| 455 | my $nextComponentValue; | ||
| 456 | |||
| 457 | my $values = {}; | ||
| 458 | if (ref $nextComponentKey) { | ||
| 459 | $values = $nextComponentKey; | ||
| 460 | } else { | ||
| 461 | $nextComponentValue = shift; | ||
| 462 | $values = { lc($nextComponentKey) => $nextComponentValue }; | ||
| 463 | } | ||
| 464 | my $attributes = shift; | ||
| 465 | |||
| 466 | my $rdnkey = lc(join("+", sort keys %{$values})); | ||
| 467 | |||
| 468 | my $newDN = join( "+", map { sprintf("%s=%s", $_, $values->{$_}) } sort keys %{$values}); | ||
| 469 | $newDN .= ','.$currentPath; | ||
| 470 | |||
| 471 | my $schema = $self->schema(); | ||
| 472 | $self->_log_and_die("No schema data to create nodes") if (!$schema); | ||
| 473 | |||
| 474 | $schema = $schema->{$rdnkey} || $schema->{default}; | ||
| 475 | $self->_log_and_die("No schema data for create path item ($rdnkey)") if (!$schema); | ||
| 476 | |||
| 477 | my @attrib; | ||
| 478 | if (!$schema->{objectclass}) { | ||
| 479 | $self->_log_and_die("No objectclass defined for path item $rdnkey"); | ||
| 480 | } | ||
| 481 | |||
| 482 | push @attrib, "objectclass"; | ||
| 483 | if (ref $schema->{objectclass} eq 'ARRAY') { | ||
| 484 | push @attrib, $schema->{objectclass}; | ||
| 485 | } else { | ||
| 486 | my @classnames = split " ", $schema->{objectclass}; | ||
| 487 | push @attrib, \@classnames; | ||
| 488 | } | ||
| 489 | |||
| 490 | # Default Values to push | ||
| 491 | $values = { %{$values}, %{$schema->{values} } } if ($schema->{values}); | ||
| 492 | |||
| 493 | # append attributes to value hash, this will overwrite values from the | ||
| 494 | $values = { %{$attributes}, %{$values} }; | ||
| 495 | |||
| 496 | foreach my $key ( keys %{$values}) { | ||
| 497 | my $val = $values->{$key}; | ||
| 498 | next unless defined $val; | ||
| 499 | if ($val eq 'copy:self') { | ||
| 500 | die "copy:self does not work with multivalued rdns" unless defined $nextComponentValue; | ||
| 501 | $val = $nextComponentValue; | ||
| 502 | } | ||
| 503 | push @attrib, $key, $val; | ||
| 504 | } | ||
| 505 | |||
| 506 | #print "Create Node $newDN \n"; | ||
| 507 | #print Dumper( $attrib ); | ||
| 508 | |||
| 509 | $self->log()->trace("Create Node $newDN with attributes " . Dumper \@attrib) if ($self->log()->is_trace); | ||
| 510 | $self->log()->debug("Create Node $newDN with attributes " . Dumper \@attrib); | ||
| 511 | |||
| 512 | my $result = $self->ldap()->add( $newDN, attr => \@attrib ); | ||
| 513 | |||
| 514 | if ($result->is_error()) { | ||
| 515 | $self->_log_and_die($result->error_desc); | ||
| 516 | } | ||
| 517 | |||
| 518 | return $newDN; | ||
| 519 | |||
| 520 | } | ||
| 521 | |||
| 522 | sub _triggerAutoCreate { | ||
| 523 | |||
| 524 | my $self = shift; | ||
| 525 | my $args = shift; | ||
| 526 | my $data = shift || {}; | ||
| 527 | |||
| 528 | my $path = $self->base(); | ||
| 529 | my @rdn; | ||
| 530 | |||
| 531 | my $tt = Template->new({}); | ||
| 532 | |||
| 533 | my $create_info = $self->create(); | ||
| 534 | if ($create_info->{basedn}) { | ||
| 535 | $path = $create_info->{basedn}; | ||
| 536 | } | ||
| 537 | |||
| 538 | if ($create_info->{dn}) { | ||
| 539 | my $dn; | ||
| 540 | $tt->process(\$create_info->{dn}, { ARGS => $args, DATA => $data }, \$dn) || $self->_log_and_die("Error processing argument template for DN."); | ||
| 541 | $self->log()->debug('Auto-Create with full dn from template'); | ||
| 542 | if (!$dn || $dn !~ /(([^=]+)=(.*?[^\\])\s*,)(.+)/) { | ||
| 543 | $self->_log_and_die("Unable to split DN from template"); | ||
| 544 | } | ||
| 545 | @rdn = ($2, $3); | ||
| 546 | # triggers creation of path components below later | ||
| 547 | $path = $4; | ||
| 548 | |||
| 549 | } elsif ($create_info->{rdn}) { | ||
| 550 | if (ref $create_info->{rdn}) { | ||
| 551 | my $hash; | ||
| 552 | foreach my $rdtpl (@{$create_info->{rdn}}){ | ||
| 553 | my $rdn; | ||
| 554 | $tt->process(\$rdtpl, { ARGS => $args, DATA => $data }, \$rdn) || $self->_log_and_die("Error processing argument template for RDN $rdtpl."); | ||
| 555 | next unless ($rdn); | ||
| 556 | my @t = split("=", $rdn, 2); | ||
| 557 | $hash->{$t[0]} = $t[1]; | ||
| 558 | } | ||
| 559 | @rdn = ($hash); | ||
| 560 | $self->log()->debug('Auto-Create with RDN template (Multivalued)'); | ||
| 561 | } else { | ||
| 562 | my $rdn; | ||
| 563 | $tt->process(\$create_info->{rdn}, { ARGS => $args, DATA => $data }, \$rdn) || $self->_log_and_die("Error processing argument template for RDN " . $create_info->{rdn}); | ||
| 564 | @rdn = split("=", $rdn, 2); | ||
| 565 | $self->log()->debug('Auto-Create with RDN template'); | ||
| 566 | } | ||
| 567 | |||
| 568 | } elsif ($create_info->{rdnkey}) { | ||
| 569 | @rdn = ($create_info->{rdnkey}, $args->[0]); | ||
| 570 | $self->log()->debug('Auto-Create with RDN key') if($self->log()->is_debug); | ||
| 571 | |||
| 572 | } else { | ||
| 573 | my $schema = $self->schema(); | ||
| 574 | if ($schema->{'cn'}) { | ||
| 575 | $self->log()->debug('Auto-Create with commonName based on schema:'); | ||
| 576 | @rdn = ('cn', $args->[0]); | ||
| 577 | } else { | ||
| 578 | $self->log()->warn('Auto-Create not configured'); | ||
| 579 | return; | ||
| 580 | } | ||
| 581 | } | ||
| 582 | |||
| 583 | # create the components for the path first, without usage of extra data | ||
| 584 | if ($path ne $self->base()) { | ||
| 585 | $path = $self->_getbyDN( $path, { create => 1 } ); | ||
| 586 | } | ||
| 587 | |||
| 588 | # now create the leaf node now using the extra data | ||
| 589 | my $nodeDN = $self->_createPathItem( $path, @rdn, $data ); | ||
| 590 | $self->log()->debug('Auto-Create done - nodeDN: '.$nodeDN); | ||
| 591 | return $nodeDN; | ||
| 592 | |||
| 593 | } | ||
| 594 | |||
| 595 | sub _splitDN { | ||
| 596 | |||
| 597 | my $self = shift; | ||
| 598 | my $dn = shift; | ||
| 599 | |||
| 600 | my @parsed; | ||
| 601 | while ($dn =~ /(([^=]+)=(.*?[^\\])\s*,)(.*)/) { | ||
| 602 | push @parsed, [ $2, $3 ]; | ||
| 603 | $self->log()->debug(sprintf 'Split-Result: Key: %s, Value: %s, Remainder: %s ', $2, $3, $4); | ||
| 604 | $dn = $4; | ||
| 605 | }; | ||
| 606 | |||
| 607 | # soemthing is really wrong - likely broken input with comma in the end | ||
| 608 | if (!$dn) { | ||
| 609 | $self->_log_and_die('Empty dn part in splitDN'); | ||
| 610 | } | ||
| 611 | |||
| 612 | # Split last remainder at = | ||
| 613 | my @last = split ("=", $dn); | ||
| 614 | push @parsed, \@last; | ||
| 615 | |||
| 616 | return @parsed; | ||
| 617 | } | ||
| 618 | |||
| 619 | sub _run_search { | ||
| 620 | |||
| 621 | my $self = shift; | ||
| 622 | my $arg = shift; | ||
| 623 | my $params = shift; | ||
| 624 | |||
| 625 | my %option = $self->_build_search_options( $arg, $params ); | ||
| 626 | |||
| 627 | my $mesg = $self->ldap()->search( %option ); | ||
| 628 | |||
| 629 | # Lost connection, try to rebind and rerun query | ||
| 630 | if ($self->_is_transient_error($mesg)) { | ||
| 631 | $mesg = $self->ldap()->search( %option ); | ||
| 632 | } | ||
| 633 | |||
| 634 | return $mesg; | ||
| 635 | |||
| 636 | } | ||
| 637 | |||
| 638 | no Moose; | ||
| 639 | __PACKAGE__->meta->make_immutable; | ||
| 640 | |||
| 641 | 1; | ||
| 642 | __END__ |