| 1 | # Connector::Proxy::Net::LDAP::DN | ||
| 2 | # | ||
| 3 | # Proxy class for accessing LDAP directories | ||
| 4 | # The class is designed to find and return one to many matching records | ||
| 5 | # in an ldap repository and return their DNs. | ||
| 6 | # Using the set function you can create and delete entries. | ||
| 7 | # | ||
| 8 | # Written by Oliver Welter for the OpenXPKI project 2012 | ||
| 9 | # | ||
| 10 | |||
| 11 | # FIXME - we need to find a syntax to pass multiple arguments in by | ||
| 12 | # all possible allowed path specs which is a problematic with | ||
| 13 | # Search Strings having the delimiter as character..... | ||
| 14 | # For now we just take is as it comes and assume a string as | ||
| 15 | # the one and only argument | ||
| 16 | |||
| 17 | package Connector::Proxy::Net::LDAP::DN; | ||
| 18 | |||
| 19 | use strict; | ||
| 20 | use warnings; | ||
| 21 | use English; | ||
| 22 | use Net::LDAP; | ||
| 23 | |||
| 24 | use Moose; | ||
| 25 | extends 'Connector::Proxy::Net::LDAP'; | ||
| 26 | |||
| 27 | sub get_list { | ||
| 28 | |||
| 29 | my $self = shift; | ||
| 30 | my @args = $self->_build_path( shift ); | ||
| 31 | |||
| 32 | my $mesg = $self->_run_search( { ARGS => \@args } ); | ||
| 33 | |||
| 34 | if ($mesg->is_error()) { | ||
| 35 | $self->log()->error("LDAP search failed error code " . $mesg->code() . " (error: " . $mesg->error_desc() .")" ); | ||
| 36 | return $self->_node_not_exists( \@args ); | ||
| 37 | } | ||
| 38 | |||
| 39 | my @list; | ||
| 40 | |||
| 41 | if ($mesg->count() == 0) { | ||
| 42 | $self->_node_not_exists( \@args ); | ||
| 43 | return @list; | ||
| 44 | } | ||
| 45 | |||
| 46 | foreach my $loop_entry ( $mesg->entries()) { | ||
| 47 | push @list, $loop_entry->dn(); | ||
| 48 | } | ||
| 49 | |||
| 50 | return @list; | ||
| 51 | |||
| 52 | } | ||
| 53 | |||
| 54 | sub get_size { | ||
| 55 | |||
| 56 | my $self = shift; | ||
| 57 | my @args = $self->_build_path( shift ); | ||
| 58 | |||
| 59 | my $mesg = $self->_run_search( { ARGS => \@args } ); | ||
| 60 | return $mesg->count(); | ||
| 61 | } | ||
| 62 | |||
| 63 | sub get_meta { | ||
| 64 | my $self = shift; | ||
| 65 | |||
| 66 | # If we have no path, we tell the caller that we are a connector | ||
| 67 | my @path = $self->_build_path( shift ); | ||
| 68 | if (scalar @path == 0) { | ||
| 69 | return { TYPE => "connector" }; | ||
| 70 | } | ||
| 71 | |||
| 72 | return { TYPE => "list" }; | ||
| 73 | } | ||
| 74 | |||
| 75 | |||
| 76 | sub exists { | ||
| 77 | |||
| 78 | my $self = shift; | ||
| 79 | |||
| 80 | # No path = connector root which always exists | ||
| 81 | my @path = $self->_build_path( shift ); | ||
| 82 | if (scalar @path == 0) { | ||
| 83 | return 1; | ||
| 84 | } | ||
| 85 | my $val; | ||
| 86 | eval { | ||
| 87 | $val = $self->get_size( \@path ) > 0; | ||
| 88 | }; | ||
| 89 | return (defined $val && $val > 0); | ||
| 90 | |||
| 91 | } | ||
| 92 | |||
| 93 | |||
| 94 | sub set { | ||
| 95 | |||
| 96 | my $self = shift; | ||
| 97 | my $args = shift; | ||
| 98 | my $value = shift; | ||
| 99 | my $params = shift; | ||
| 100 | |||
| 101 | my @args = $self->_build_path( $args ); | ||
| 102 | |||
| 103 | if (!$params->{pkey}) { | ||
| 104 | $self->_log_and_die('You must pass the pkey as parameter to delete an entry.'); | ||
| 105 | } | ||
| 106 | |||
| 107 | my $mesg = $self->_run_search( { ARGS => \@args }, { noattrs => 1} ); | ||
| 108 | |||
| 109 | if ($mesg->is_error()) { | ||
| 110 | $self->_log_and_die("LDAP search failed error code " . $mesg->code() . " (error: " . $mesg->error_desc() .")" ); | ||
| 111 | } | ||
| 112 | |||
| 113 | if ($mesg->count() == 0) { | ||
| 114 | return $self->_node_not_exists( \@args ); | ||
| 115 | } | ||
| 116 | |||
| 117 | my $match_dn = lc($params->{pkey}); | ||
| 118 | foreach my $entry ( $mesg->entries()) { | ||
| 119 | if (lc($entry->dn()) eq $match_dn) { | ||
| 120 | $entry->delete(); | ||
| 121 | my $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 | $self->log()->debug('Delete LDAP entry by DN: '.$params->{pkey}); | ||
| 126 | return 1; | ||
| 127 | } | ||
| 128 | } | ||
| 129 | |||
| 130 | $self->log()->warn('DN to delete not found in result: '.$params->{pkey}); | ||
| 131 | return $self->_node_not_exists( \@args ); | ||
| 132 | |||
| 133 | } | ||
| 134 | |||
| 135 | 1; | ||
| 136 | __END__ |