| 1 | # Connector::Proxy::Net::LDAP::Simple | ||
| 2 | # | ||
| 3 | # Proxy class for accessing LDAP directories | ||
| 4 | # The class is designed to find and return a single attribute | ||
| 5 | # value. It also supports set for a scalar value. | ||
| 6 | # | ||
| 7 | # Written by Oliver Welter for the OpenXPKI project 2012 | ||
| 8 | # | ||
| 9 | |||
| 10 | # FIXME - we need to find a syntax to pass multiple arguments in by | ||
| 11 | # all possible allowed path specs which is a problematic with | ||
| 12 | # Search Strings having the delimiter as character..... | ||
| 13 | # For now we just take is as it comes and assume a string as | ||
| 14 | # the one and only argument | ||
| 15 | |||
| 16 | package Connector::Proxy::Net::LDAP::Simple; | ||
| 17 | |||
| 18 | use strict; | ||
| 19 | use warnings; | ||
| 20 | use English; | ||
| 21 | use Net::LDAP; | ||
| 22 | |||
| 23 | use Moose; | ||
| 24 | extends 'Connector::Proxy::Net::LDAP'; | ||
| 25 | |||
| 26 | sub get { | ||
| 27 | my $self = shift; | ||
| 28 | my @args = $self->_build_path( shift ); | ||
| 29 | |||
| 30 | if (!$self->attrs || @{$self->attrs} != 1) { | ||
| 31 | $self->_log_and_die("The attribute list must contain at least one entry"); | ||
| 32 | } | ||
| 33 | |||
| 34 | my $mesg = $self->_run_search( { ARGS => \@args } ); | ||
| 35 | |||
| 36 | if ($mesg->is_error()) { | ||
| 37 | $self->log()->error("LDAP search failed error code " . $mesg->code() . " (error: " . $mesg->error_desc() .")" ); | ||
| 38 | return $self->_node_not_exists( \@args ); | ||
| 39 | } | ||
| 40 | |||
| 41 | if ($mesg->count() == 0) { | ||
| 42 | return $self->_node_not_exists( \@args ); | ||
| 43 | } | ||
| 44 | |||
| 45 | if ($mesg->count() > 1) { | ||
| 46 | $self->_log_and_die("More than one entry found - result is not unique."); | ||
| 47 | } | ||
| 48 | |||
| 49 | my $entry = $mesg->entry(0); | ||
| 50 | |||
| 51 | # Check for all attributes and return the first one | ||
| 52 | foreach my $attr (@{$self->attrs}) { | ||
| 53 | my $ref = $entry->get_value ( $self->attrs->[0], asref => 1 ); | ||
| 54 | return $ref->[0] if ($ref); | ||
| 55 | } | ||
| 56 | |||
| 57 | # No Attribute has a valid value | ||
| 58 | return $self->_node_not_exists( \@args ); | ||
| 59 | } | ||
| 60 | |||
| 61 | sub set { | ||
| 62 | |||
| 63 | my $self = shift; | ||
| 64 | my $args = shift; | ||
| 65 | my $value = shift; | ||
| 66 | |||
| 67 | my @args = $self->_build_path( $args ); | ||
| 68 | |||
| 69 | if (!$self->attrs || @{$self->attrs} != 1) { | ||
| 70 | $self->_log_and_die("The attribute list must contain exactly one entry"); | ||
| 71 | } | ||
| 72 | |||
| 73 | my $action = $self->action(); | ||
| 74 | my $attribute = $self->attrs->[0]; | ||
| 75 | |||
| 76 | my $entry; | ||
| 77 | |||
| 78 | # Try to find the entry | ||
| 79 | my $mesg = $self->_run_search( { ARGS => \@args }, { noattrs => 1} ); | ||
| 80 | |||
| 81 | if ($mesg->is_error()) { | ||
| 82 | $self->_log_and_die("LDAP search failed error code " . $mesg->code() . " (error: " . $mesg->error_desc() .")" ); | ||
| 83 | } | ||
| 84 | |||
| 85 | if ($mesg->count() > 1) { | ||
| 86 | $self->_log_and_die('Set by filter had multiple results: ' . join '|', @args); | ||
| 87 | } | ||
| 88 | |||
| 89 | if ($mesg->count() == 1) { | ||
| 90 | $entry = $mesg->entry(0); | ||
| 91 | $self->log()->debug('Entry found ' . $entry->dn()); | ||
| 92 | |||
| 93 | # Check if autocreate is configured | ||
| 94 | } elsif (($action ne "delete") && $self->schema()) { | ||
| 95 | |||
| 96 | $entry = $self->_triggerAutoCreate( \@args, { $attribute => $value } ); | ||
| 97 | if ($entry) { | ||
| 98 | $self->log()->debug('Entry was created with given value'); | ||
| 99 | return 1; | ||
| 100 | } else { | ||
| 101 | $self->log()->warn('Auto-Create failed'); | ||
| 102 | } | ||
| 103 | } | ||
| 104 | |||
| 105 | return $self->_node_not_exists(\@args) if (!$entry); | ||
| 106 | |||
| 107 | if ($action eq "append") { | ||
| 108 | $self->log()->trace('Append '.$value.' to Attribute '.$attribute); | ||
| 109 | $entry->add( $attribute => $value ); | ||
| 110 | } elsif($action eq "delete") { | ||
| 111 | $self->log()->trace('Delete '.$value.' from Attribute '.$attribute); | ||
| 112 | $entry->delete( $attribute => $value ) if ($value); | ||
| 113 | } elsif (defined $value) { | ||
| 114 | $self->log()->trace('Replace Attribute '.$attribute.' with '.$value); | ||
| 115 | $entry->replace( $attribute => $value ); | ||
| 116 | } else { # Implicit delete - replace with an undef value | ||
| 117 | $self->log()->trace('Remove Attribute '.$attribute); | ||
| 118 | $entry->delete( $attribute => undef ); | ||
| 119 | } | ||
| 120 | |||
| 121 | $mesg = $entry->update( $self->ldap() ); | ||
| 122 | if ($mesg->is_error()) { | ||
| 123 | $self->_log_and_die("LDAP update failed error code " . $mesg->code() . " (error: " . $mesg->error_desc() .")" ); | ||
| 124 | } | ||
| 125 | |||
| 126 | return 1; | ||
| 127 | |||
| 128 | } | ||
| 129 | |||
| 130 | sub get_meta { | ||
| 131 | |||
| 132 | my $self = shift; | ||
| 133 | |||
| 134 | # If we have no path, we tell the caller that we are a connector | ||
| 135 | my @path = $self->_build_path( shift ); | ||
| 136 | if (scalar @path == 0) { | ||
| 137 | return { TYPE => "connector" }; | ||
| 138 | } | ||
| 139 | |||
| 140 | return { TYPE => "scalar" }; | ||
| 141 | |||
| 142 | } | ||
| 143 | |||
| 144 | sub exists { | ||
| 145 | |||
| 146 | my $self = shift; | ||
| 147 | |||
| 148 | # No path = connector root which always exists | ||
| 149 | my @path = $self->_build_path( shift ); | ||
| 150 | if (scalar @path == 0) { | ||
| 151 | return 1; | ||
| 152 | } | ||
| 153 | my $val; | ||
| 154 | eval { | ||
| 155 | $val = $self->get( \@path ); | ||
| 156 | }; | ||
| 157 | return defined $val; | ||
| 158 | |||
| 159 | } | ||
| 160 | |||
| 161 | 1; | ||
| 162 | __END__ |