| 1 | # Connector::Proxy::Net::LDAP::Single | ||
| 2 | # | ||
| 3 | # Proxy class for accessing LDAP directories | ||
| 4 | # The class is designed to find and return a single entry | ||
| 5 | # | ||
| 6 | # Written by Oliver Welter for the OpenXPKI project 2012 | ||
| 7 | # | ||
| 8 | |||
| 9 | # FIXME - we need to find a syntax to pass multiple arguments in by | ||
| 10 | # all possible allowed path specs which is a problematic with | ||
| 11 | # Search Strings having the delimiter as character..... | ||
| 12 | # For now we just take is as it comes and assume a string as | ||
| 13 | # the one and only argument | ||
| 14 | |||
| 15 | package Connector::Proxy::Net::LDAP::Single; | ||
| 16 | |||
| 17 | use strict; | ||
| 18 | use warnings; | ||
| 19 | use English; | ||
| 20 | use Net::LDAP; | ||
| 21 | use Data::Dumper; | ||
| 22 | |||
| 23 | use Moose; | ||
| 24 | extends 'Connector::Proxy::Net::LDAP'; | ||
| 25 | |||
| 26 | has attrmap => ( | ||
| 27 | is => 'rw', | ||
| 28 | isa => 'HashRef', | ||
| 29 | trigger => \&_map_attrs | ||
| 30 | ); | ||
| 31 | |||
| 32 | |||
| 33 | sub _map_attrs { | ||
| 34 | |||
| 35 | my ( $self, $new, $old ) = @_; | ||
| 36 | |||
| 37 | # Write the attrs property from the keys of the map | ||
| 38 | if (ref $new eq "HASH") { | ||
| 39 | my @attrs = values %{$new}; | ||
| 40 | $self->attrs( \@attrs ) | ||
| 41 | } | ||
| 42 | } | ||
| 43 | |||
| 44 | |||
| 45 | sub get_hash { | ||
| 46 | my $self = shift; | ||
| 47 | my $args = shift; | ||
| 48 | my $params = shift; | ||
| 49 | |||
| 50 | my @args = $self->_build_path( $args ); | ||
| 51 | |||
| 52 | # compose a list of command arguments | ||
| 53 | my $template_vars = { | ||
| 54 | ARGS => \@args, | ||
| 55 | }; | ||
| 56 | |||
| 57 | my $mesg = $self->_run_search($template_vars); | ||
| 58 | |||
| 59 | if ($mesg->is_error()) { | ||
| 60 | $self->log()->error("LDAP search failed error code " . $mesg->code() . " (error: " . $mesg->error_desc() .")" ); | ||
| 61 | return $self->_node_not_exists( \@args ); | ||
| 62 | } | ||
| 63 | |||
| 64 | if ($mesg->count() == 0) { | ||
| 65 | return $self->_node_not_exists(\@args); | ||
| 66 | } | ||
| 67 | |||
| 68 | if ($mesg->count() > 1) { | ||
| 69 | $self->_log_and_die("More than one entry found - result is not unique."); | ||
| 70 | } | ||
| 71 | |||
| 72 | my $entry = $mesg->entry(0); | ||
| 73 | my $dn = $entry->dn(); | ||
| 74 | my %attribs = ( pkey => $dn ); | ||
| 75 | my ($target, $source); | ||
| 76 | |||
| 77 | while (($target, $source) = each %{$self->attrmap()}) { | ||
| 78 | my $ref = $entry->get_value ( $source, asref => 1 ); | ||
| 79 | next unless ($ref); | ||
| 80 | |||
| 81 | # return multivalued attributes as array ref if "deep" is set | ||
| 82 | if (@{$ref} > 1 && $params->{deep}) { | ||
| 83 | $attribs{$target} = $ref; | ||
| 84 | } else { | ||
| 85 | $attribs{$target} = $ref->[0]; | ||
| 86 | } | ||
| 87 | } | ||
| 88 | return \%attribs; | ||
| 89 | |||
| 90 | } | ||
| 91 | |||
| 92 | sub get_list { | ||
| 93 | my $self = shift; | ||
| 94 | my $args = shift; | ||
| 95 | my $params = shift; | ||
| 96 | |||
| 97 | my @args = $self->_build_path( $args ); | ||
| 98 | |||
| 99 | # compose a list of command arguments | ||
| 100 | my $template_vars = { | ||
| 101 | ARGS => \@args, | ||
| 102 | }; | ||
| 103 | |||
| 104 | my $mesg = $self->_run_search($template_vars); | ||
| 105 | |||
| 106 | if ($mesg->is_error()) { | ||
| 107 | $self->log()->error("LDAP search failed error code " . $mesg->code() . " (error: " . $mesg->error_desc() .")" ); | ||
| 108 | return $self->_node_not_exists( \@args ); | ||
| 109 | } | ||
| 110 | |||
| 111 | my @entries = $mesg->entries; | ||
| 112 | |||
| 113 | my @result; | ||
| 114 | ENTRY: | ||
| 115 | foreach my $entry (@entries) { | ||
| 116 | # check if any of the wanted attributes is present in the returned entry | ||
| 117 | foreach my $wantedattribute (@{$self->attrs}) { | ||
| 118 | $self->log()->debug("Checking for attribute " . $wantedattribute); | ||
| 119 | if ($entry->exists($wantedattribute)) { | ||
| 120 | $self->log()->debug(" Using wanted attribute " . $entry->get_value($wantedattribute)); | ||
| 121 | push @result, $entry->get_value($wantedattribute); | ||
| 122 | # use the first attribute found in the entry | ||
| 123 | next ENTRY; | ||
| 124 | } | ||
| 125 | } | ||
| 126 | } | ||
| 127 | |||
| 128 | return @result; | ||
| 129 | } | ||
| 130 | |||
| 131 | |||
| 132 | sub get_keys { | ||
| 133 | my $self = shift; | ||
| 134 | |||
| 135 | my $hash = $self->get_hash( @_ ); | ||
| 136 | return unless($hash); | ||
| 137 | return keys %{ $hash }; | ||
| 138 | } | ||
| 139 | |||
| 140 | sub get_meta { | ||
| 141 | my $self = shift; | ||
| 142 | |||
| 143 | # If we have no path, we tell the caller that we are a connector | ||
| 144 | my @path = $self->_build_path( shift ); | ||
| 145 | if (scalar @path == 0) { | ||
| 146 | return { TYPE => "connector" }; | ||
| 147 | } | ||
| 148 | |||
| 149 | return {TYPE => "hash" }; | ||
| 150 | } | ||
| 151 | |||
| 152 | sub exists { | ||
| 153 | |||
| 154 | my $self = shift; | ||
| 155 | |||
| 156 | # No path = connector root which always exists | ||
| 157 | my @path = $self->_build_path( @_ ); | ||
| 158 | if (scalar @path == 0) { | ||
| 159 | return 1; | ||
| 160 | } | ||
| 161 | my $val; | ||
| 162 | eval { | ||
| 163 | $val = $self->get_hash( @_ ); | ||
| 164 | }; | ||
| 165 | return defined $val; | ||
| 166 | |||
| 167 | } | ||
| 168 | |||
| 169 | sub set { | ||
| 170 | |||
| 171 | my $self = shift; | ||
| 172 | my $args = shift; | ||
| 173 | my $value = shift; | ||
| 174 | my $params = shift; | ||
| 175 | |||
| 176 | my @args = $self->_build_path( $args ); | ||
| 177 | |||
| 178 | my $entry; | ||
| 179 | |||
| 180 | # We accept only a hash as value | ||
| 181 | if (ref $value ne 'HASH') { | ||
| 182 | $self->_log_and_die('The value must be a hash reference.'); | ||
| 183 | } | ||
| 184 | |||
| 185 | $self->log()->debug('Set called on ' . Dumper \@args ); | ||
| 186 | |||
| 187 | # Check if a pkey/dn is passed | ||
| 188 | if ($params->{pkey}) { | ||
| 189 | $entry = $self->_getbyDN( $params->{pkey} ); | ||
| 190 | if (!defined $entry) { | ||
| 191 | $self->log()->warn('Set by dn had no result: '.$params->{pkey}); | ||
| 192 | return $self->_node_not_exists(\@args); | ||
| 193 | } | ||
| 194 | } | ||
| 195 | |||
| 196 | my $mesg = $self->_run_search({ ARGS => \@args }, { noattrs => 1}); | ||
| 197 | |||
| 198 | # Try to find the entry | ||
| 199 | if ($mesg->is_error()) { | ||
| 200 | $self->_log_and_die("LDAP search failed error code " . $mesg->code() . " (error: " . $mesg->error_desc() .")" ); | ||
| 201 | } | ||
| 202 | |||
| 203 | if ($mesg->count() > 1) { | ||
| 204 | $self->_log_and_die('Set by filter had multiple results: ' . join "|", @args ); | ||
| 205 | } | ||
| 206 | |||
| 207 | if ($mesg->count() == 1) { | ||
| 208 | $entry = $mesg->entry(0); | ||
| 209 | $self->log()->debug('Entry found ' . $entry->dn()); | ||
| 210 | } | ||
| 211 | |||
| 212 | my $action = $self->action(); | ||
| 213 | $self->log()->debug('Action is '.$action); | ||
| 214 | |||
| 215 | # autocreate if not delete and schema is defined | ||
| 216 | if (!$entry && ($action ne "delete") && $self->schema()) { | ||
| 217 | # create data array | ||
| 218 | my $attrib; | ||
| 219 | while (my ($source, $attribute) = each %{$self->attrmap()}) { | ||
| 220 | $attrib->{$attribute} = $value->{$source} if (exists $value->{$source}); | ||
| 221 | } | ||
| 222 | $entry = $self->_triggerAutoCreate( \@args, $attrib ); | ||
| 223 | if ($entry) { | ||
| 224 | $self->log()->debug('Entry was created with given attributes'); | ||
| 225 | return 1; | ||
| 226 | } else { | ||
| 227 | $self->log()->warn('Auto-Create failed'); | ||
| 228 | } | ||
| 229 | } | ||
| 230 | |||
| 231 | return $self->_node_not_exists(\@args) if (!$entry); | ||
| 232 | |||
| 233 | while (my ($source, $attribute) = each %{$self->attrmap()}) { | ||
| 234 | if (!$attribute) { | ||
| 235 | $self->_log_and_die('Attribute for '.$source.' is undef.'); | ||
| 236 | } | ||
| 237 | if (exists $value->{$source}) { | ||
| 238 | my $value = $value->{$source}; | ||
| 239 | $self->log()->debug("Perform $action on attribute $attribute") unless ($self->log()->is_trace()); | ||
| 240 | if ($action eq "append") { | ||
| 241 | $self->log()->trace('Append '.$value.' to Attribute '.$attribute); | ||
| 242 | $entry->add( $attribute => $value ); | ||
| 243 | } elsif($action eq "delete") { | ||
| 244 | $self->log()->trace('Delete '.$value.' from Attribute '.$attribute); | ||
| 245 | $entry->delete( $attribute => $value ) if (defined $value); | ||
| 246 | } elsif (defined $value) { | ||
| 247 | $self->log()->trace('Replace Attribute '.$attribute.' with '.$value); | ||
| 248 | $entry->replace( $attribute => $value ); | ||
| 249 | } else { # Implicit delete - replace with an undef value | ||
| 250 | $self->log()->trace('Remove Attribute '.$attribute); | ||
| 251 | $entry->delete( $attribute => undef ); | ||
| 252 | } | ||
| 253 | } | ||
| 254 | } | ||
| 255 | |||
| 256 | $mesg = $entry->update( $self->ldap() ); | ||
| 257 | if ($mesg->is_error()) { | ||
| 258 | $self->_log_and_die("LDAP update failed error code " . $mesg->code() . " (error: " . $mesg->error_desc() . ")" ); | ||
| 259 | } | ||
| 260 | |||
| 261 | return 1; | ||
| 262 | } | ||
| 263 | |||
| 264 | 1; | ||
| 265 | __END__ |