| 1 | #=head1 NAME | ||
| 2 | # StoredHash::LD - LDAP storage backend for StoredHash | ||
| 3 | #=SYNOPSIS | ||
| 4 | # use StoredHash::LD; | ||
| 5 | # my $ld = Net::LDAP->new('ldap.myshop.com'); | ||
| 6 | # my $shp = StoredHash::LD->new('table' => 'ou=people,o=myshop', 'dbh' => $ld); | ||
| 7 | # | ||
| 8 | #=head1 DESCRIPTION | ||
| 9 | # General notes: | ||
| 10 | #=over 4 | ||
| 11 | #=item * LDAP Does not support composite keys. Because of this passing id as | ||
| 12 | #scalar value (i.e. not as an arrayref) is allowed. | ||
| 13 | #=item * LDAP Update is an inherently complex operation as it is broken down to: | ||
| 14 | #=over 4 | ||
| 15 | #=item * attribute additions | ||
| 16 | #=item * attribute deletions | ||
| 17 | #=item * attribute value modifications | ||
| 18 | #back | ||
| 19 | #=item * Update complexity is further aplified by the multiple values (per attribute) | ||
| 20 | #that every LDAP entry supports | ||
| 21 | #=item * As such LDAP update may be costly operation, especially in large bulk | ||
| 22 | #modifications. | ||
| 23 | #=item * Because of multivalue holding capability of all All entries | ||
| 24 | #=back | ||
| 25 | |||
| 26 | # | ||
| 27 | |||
| 28 | #=cut | ||
| 29 | package StoredHash::LD; | ||
| 30 | |||
| 31 | use Net::LDAP; | ||
| 32 | use Net::LDAP::Constant qw(LDAP_SUCCESS LDAP_COMPARE_TRUE LDAP_COMPARE_FALSE); | ||
| 33 | |||
| 34 | use strict; | ||
| 35 | use warnings; | ||
| 36 | use Data::Dumper; | ||
| 37 | use StoredHash; | ||
| 38 | # Inherit all | ||
| 39 | #our @ISA = ('StoredHash'); | ||
| 40 | use base ('StoredHash'); | ||
| 41 | #use base ('StoredHash::NoSQL'); | ||
| 42 | our $VERSION = '0.30'; | ||
| 43 | # LDAP Helpers | ||
| 44 | sub success { | ||
| 45 | my ($mesg) = @_; | ||
| 46 | |||
| 47 | if ($mesg->code() eq Net::LDAP::LDAP_SUCCESS()) {return(1);} | ||
| 48 | return(0); | ||
| 49 | } | ||
| 50 | |||
| 51 | #=head2 StoredHash->new(%opts) | ||
| 52 | # | ||
| 53 | #Notes on keyword parameters (in %opts). | ||
| 54 | # | ||
| 55 | #=over 4 | ||
| 56 | # | ||
| 57 | #=item * 'table' needs to be a LDAP base suffix for the directory path | ||
| 58 | # where entries will reside and where they are searched from (with scope=one). | ||
| 59 | # | ||
| 60 | #=item * 'dbh' must be a valid Net::LDAP connection. | ||
| 61 | # | ||
| 62 | #=cut | ||
| 63 | sub new { | ||
| 64 | my ($class, %opt) = @_; | ||
| 65 | my $shp = StoredHash->new(%opt); | ||
| 66 | bless($shp, $class); | ||
| 67 | } | ||
| 68 | |||
| 69 | #sub errstr { | ||
| 70 | # my ($p, $v) = @_; | ||
| 71 | # $ars_errstr | ||
| 72 | #} | ||
| 73 | #sub qexecute { | ||
| 74 | # my ($p, $q, $vals, $rett) = @_; | ||
| 75 | |||
| 76 | # Class utility method to extract and validate the id from either array (ref) | ||
| 77 | # or scalar id paramater. simpleid considers composite keys as an error and | ||
| 78 | # throws exceptions on them. | ||
| 79 | sub simpleid { | ||
| 80 | my ($eid) = @_; | ||
| 81 | my $isarr = ref($eid) eq 'ARRAY' ? 1 : 0; | ||
| 82 | if ($isarr && (scalar(@$eid) == 1)) {$eid = $eid->[0];} | ||
| 83 | elsif ($isarr) {die("Entry ID must be passed as array of single id or non-empty scalar id");} | ||
| 84 | # Check other refs ? | ||
| 85 | # Check non-empty | ||
| 86 | if (!$eid) {die("ID should not be empty value !");} # Allow '0' / 0 ? | ||
| 87 | return($eid); | ||
| 88 | } | ||
| 89 | |||
| 90 | # Hash %$e may contain attributes with single (scalar) value or array | ||
| 91 | # of multiple values | ||
| 92 | sub insert { | ||
| 93 | my ($p, $e) = @_; | ||
| 94 | my ($ldap, $sc) = @$p{'dbh', 'table'}; | ||
| 95 | my $ida = $p->{'pkey'}->[0]; | ||
| 96 | my $idv; | ||
| 97 | if (!($idv = $e->{$ida})) {die("LDAP Entry Must have ID (In hash) on insert");} | ||
| 98 | my $dn = "$ida=$idv,$sc"; | ||
| 99 | # Serialize to array as perl Net::LDAP API. values (odd index | ||
| 100 | # elems) may be arrays | ||
| 101 | my @kvs = map({($_, $e->{$_});} keys(%$e)); | ||
| 102 | my $mesg = $ldap->add($dn, 'attrs' => [@kvs]); | ||
| 103 | if (!success($mesg)) {die("Failed to create entry (".$mesg->error().")\n");} | ||
| 104 | return([$idv]); | ||
| 105 | } | ||
| 106 | # | ||
| 107 | sub update { | ||
| 108 | my ($p, $e, $eid) = @_; | ||
| 109 | my ($ldap, $sc) = @$p{'dbh', 'table'}; | ||
| 110 | my $ida = $p->{'pkey'}->[0]; | ||
| 111 | if ((ref($eid) eq 'ARRAY') && (scalar(@$eid) == 1)) {$eid = $eid->[0];} | ||
| 112 | #my @kvs = map({($_, $e->{$_});} keys(%$e)); | ||
| 113 | my $eo = $p->load($eid); | ||
| 114 | if (!$eo) {die("No Entry to update ('$eid')");} | ||
| 115 | if (ref($eo) ne 'HASH') {die("No Entry as HASH to update ('$eid')");} | ||
| 116 | # Do some diffing on $eo, $e | ||
| 117 | # Change organized as add,delete,replace | ||
| 118 | my @ch = ld_delta($eo, $e, $ida); | ||
| 119 | print(Dumper(\@ch)); | ||
| 120 | return(0); | ||
| 121 | # Separate round for ID ? | ||
| 122 | my $rdn; | ||
| 123 | # Check that NOT multival ? | ||
| 124 | #if ($rep{$ida}) {$rdn = "$ida=$rep{$ida}";} | ||
| 125 | # Can be done in single step | ||
| 126 | my $dn = "$ida=$eid,$sc"; | ||
| 127 | my $mesg = $ldap->modify($dn, 'add' => {} ); | ||
| 128 | if (!success($mesg)) {die("Failed to modify entry (".$mesg->error().")\n"); | ||
| 129 | return(0); | ||
| 130 | } | ||
| 131 | #if ($rdn) {$mesg = $ldap->moddn( $dn, 'newrdn' => $rdn );} | ||
| 132 | return(1); | ||
| 133 | } | ||
| 134 | |||
| 135 | # How to (distinguish): add,delete,replace | ||
| 136 | # Need to get existing ? Perform a diff to see add/del/replace | ||
| 137 | # By default no attr deletions ? Or delete on existing attr = undef | ||
| 138 | sub ld_delta { | ||
| 139 | my ($eo, $e, $ida) = @_; | ||
| 140 | my (%add, %del, %rep); | ||
| 141 | |||
| 142 | my $ismv = sub {ref($_[0]) eq 'ARRAY' ? 1 : 0;}; | ||
| 143 | my $mvcnt = sub { | ||
| 144 | if (ref($_[0]) eq 'ARRAY') {return(scalar(@{$_[0]}));} | ||
| 145 | return(1); | ||
| 146 | }; | ||
| 147 | my @ksnew = keys(%$e); | ||
| 148 | # Consider keys of mod | ||
| 149 | for my $k (@ksnew) { | ||
| 150 | # Skip RDN / ID Attribute | ||
| 151 | if ($k eq $ida) {next;} | ||
| 152 | my $vnew = $e->{$k}; | ||
| 153 | my $vold = $eo->{$k}; # #$eo->get_value($_); | ||
| 154 | # New undef (but exists), delete | ||
| 155 | if (!defined($vnew)) { | ||
| 156 | if ($vold) {$del{$k} = 1;} | ||
| 157 | else {} | ||
| 158 | next; | ||
| 159 | } | ||
| 160 | # Not in old, add new | ||
| 161 | elsif (!$vold) { | ||
| 162 | if ($vnew) {$add{$k}= $vnew;} | ||
| 163 | else {} | ||
| 164 | } | ||
| 165 | # In both, replace | ||
| 166 | # Check if same, also consider multival | ||
| 167 | # Use Value counts ? | ||
| 168 | else { | ||
| 169 | my $vcold = $mvcnt->($vold); | ||
| 170 | my $vcnew = $mvcnt->($vnew); | ||
| 171 | # TODO: test single here to elim most simple case ????? | ||
| 172 | #if (!$ismv->($vold) && !$ismv->($vnew)) {$rep{$k} = $vnew;} | ||
| 173 | # Different type: multival/ singleval | ||
| 174 | if (!$ismv->($vold) && $ismv->($vnew)) {$rep{$k} = $vnew;} | ||
| 175 | elsif ($ismv->($vold) && !$ismv->($vnew)) {$rep{$k} = $vnew;} | ||
| 176 | # Same type: single or multival | ||
| 177 | # Else Need more thorough comp | ||
| 178 | else { | ||
| 179 | if (($vcold == 1) && ($vcnew == 1)) { | ||
| 180 | # Count 1, value Same, do nothing | ||
| 181 | if ($vold eq $vnew) {} | ||
| 182 | # Replace with new | ||
| 183 | else {$rep{$k} = $vnew;} | ||
| 184 | } | ||
| 185 | # Both multival | ||
| 186 | else { | ||
| 187 | # Counts differ, use new | ||
| 188 | if ($vcold != $vcnew) {$rep{$k} = $vnew;} | ||
| 189 | # Same count | ||
| 190 | # Trick: Sort vals, join and compare | ||
| 191 | else { | ||
| 192 | my $cnt = $vcold; | ||
| 193 | my $o = join('', sort($vold)); | ||
| 194 | my $n = join('', sort($vnew)); | ||
| 195 | # Same effective content | ||
| 196 | # TODO: Allow re-order (by temp delete) | ||
| 197 | if ($o eq $n) {} | ||
| 198 | # Different content | ||
| 199 | else {$rep{$k} = $vnew;} | ||
| 200 | } | ||
| 201 | } | ||
| 202 | } | ||
| 203 | |||
| 204 | } | ||
| 205 | } | ||
| 206 | return(\%add, \%del, \%rep); | ||
| 207 | #return('add' => \%add, 'delete' => \%del, 'replace' => \%rep); | ||
| 208 | } | ||
| 209 | |||
| 210 | #=head2 $shp->delete([$eid]) | ||
| 211 | # Delete an Entry by id from LDAP directory. | ||
| 212 | #=cut | ||
| 213 | sub delete { | ||
| 214 | my ($p, $eid) = @_; | ||
| 215 | my ($ldap, $sc) = @$p{'dbh', 'table'}; | ||
| 216 | if ((ref($eid) eq 'ARRAY') && (scalar(@$eid) == 1)) {$eid = $eid->[0];} | ||
| 217 | if (!$eid || ref($eid)) {die("Entry ID must be passed as array of single item or non-empty scalar");} | ||
| 218 | my $pka = $p->{'pkey'}->[0]; | ||
| 219 | my $dn = "$pka=$eid,$sc"; | ||
| 220 | my $mesg = $ldap->delete($dn); | ||
| 221 | if (!success($mesg)) {die("Failed to delete entry (".$mesg->error().")\n");} | ||
| 222 | } | ||
| 223 | sub exists { | ||
| 224 | my ($p, $eid) = @_; | ||
| 225 | my ($ldap, $sc) = @$p{'dbh', 'table'}; | ||
| 226 | if ((ref($eid) eq 'ARRAY') && (scalar(@$eid) == 1)) {$eid = $eid->[0];} | ||
| 227 | # Implement as compare ? | ||
| 228 | my $pka = $p->{'pkey'}->[0]; | ||
| 229 | my $dn = "$pka=$eid,$sc"; | ||
| 230 | my $base = $sc; # $dn | ||
| 231 | my $mesg = $ldap->compare($dn, 'attr' => $pka, 'value' => $eid); | ||
| 232 | # How to analyze mesg for compare | ||
| 233 | my $cm = $mesg->code(); | ||
| 234 | if ($cm eq LDAP_COMPARE_TRUE()) {return(1);} # | ||
| 235 | return(0); | ||
| 236 | } | ||
| 237 | # Load | ||
| 238 | sub load { | ||
| 239 | my ($p, $eid) = @_; | ||
| 240 | my ($ldap, $sc) = @$p{'dbh', 'table'}; | ||
| 241 | if ((ref($eid) eq 'ARRAY') && (scalar(@$eid) == 1)) {$eid = $eid->[0];} | ||
| 242 | my @ats = $p->{'attrs'} || (); # TODO: %o $o{'attrs'} | ||
| 243 | my $pka = $p->{'pkey'}->[0]; | ||
| 244 | my $dn = "$pka=$eid,$sc"; | ||
| 245 | #attrs => [qw(cn)] | ||
| 246 | my @atpara = (); | ||
| 247 | if (@ats) {@atpara = ('attrs', \@ats);} | ||
| 248 | my $qs = wherefilter({$pka => $eid, }); | ||
| 249 | # Use scope ? | ||
| 250 | my $base = $sc; | ||
| 251 | my $mesg = $ldap->search('base' => $base, 'filter' => $qs, @atpara); | ||
| 252 | if (!success($mesg)) {die("Failed to load entry (".$mesg->error().")\n");} | ||
| 253 | my $ent; | ||
| 254 | my @es = $mesg->entries(); | ||
| 255 | if (!@es || (@es > 1)) {die("None or not unique");return(undef);} | ||
| 256 | $ent = $es[0]; | ||
| 257 | if (!ref($ent)) {die("Not a reference");} | ||
| 258 | #NOT:my %vals = %$ent; | ||
| 259 | my $vals = entry2hash($ent); | ||
| 260 | return($vals); | ||
| 261 | } | ||
| 262 | # Convert Net::LDAP::Entry To Plain Perl hash(ref) | ||
| 263 | # Add a pseudo-attribute 'dn' for distinguished name. | ||
| 264 | # Return hash(ref) | ||
| 265 | sub entry2hash { | ||
| 266 | my ($ent) = @_; | ||
| 267 | my @atvs = @{$ent->{'asn'}->{'attributes'}}; # $_[0] | ||
| 268 | #if ($p->{'debug'}) {print(Dumper(\@atvs));} | ||
| 269 | my %vals = map({ | ||
| 270 | if (!$_->{'vals'}) {();} | ||
| 271 | else { | ||
| 272 | my $v = (scalar(@{$_->{'vals'}}) == 1 )? $_->{'vals'}->[0] : $_->{'vals'}; | ||
| 273 | ($_->{'type'}, $v); | ||
| 274 | } | ||
| 275 | } @atvs); | ||
| 276 | $vals{'dn'} = $ent->dn(); | ||
| 277 | return(\%vals); | ||
| 278 | } | ||
| 279 | |||
| 280 | |||
| 281 | sub loadset { | ||
| 282 | my ($p, $h, $sort) = @_; # filter, sortby | ||
| 283 | my ($ldap, $sc) = @$p{'dbh', 'table'}; | ||
| 284 | my $qs = wherefilter($h); | ||
| 285 | my $mesg = $ldap->search('base'=> $sc, 'filter' => $qs); | ||
| 286 | |||
| 287 | my @es; | ||
| 288 | # Convert to plain perl hash | ||
| 289 | @es = map({entry2hash($_);} $mesg->entries()); | ||
| 290 | #for my $e ($mesg->entries()) {push(@es, entry2hash($e));} | ||
| 291 | |||
| 292 | return(\@es); | ||
| 293 | } | ||
| 294 | # Need to do entry stats or inspect schema | ||
| 295 | # Option: Sample objectclasses, query schema by them ? | ||
| 296 | sub cols { | ||
| 297 | my ($p, %c) = @_; | ||
| 298 | my ($ldap, $sc) = @$p{'dbh', 'table'}; | ||
| 299 | # Use scope ? | ||
| 300 | my $mesg = $ldap->search('base'=> $sc, 'filter' => 'objectclass=*'); | ||
| 301 | my $ent; | ||
| 302 | my @es = $mesg->entries(); | ||
| 303 | if (!($ent = $es[0])) {die("No entries");} | ||
| 304 | # Sample all ? | ||
| 305 | my @attrs = $ent->attributes(); | ||
| 306 | return(\@attrs); | ||
| 307 | } | ||
| 308 | #sub loadtree { | ||
| 309 | # my ($p, %c) = @_; | ||
| 310 | # | ||
| 311 | #} | ||
| 312 | |||
| 313 | # Load Children by DN attrs (to what attrs "_..." ?) | ||
| 314 | #sub loadchildren { | ||
| 315 | # my ($p, $e, %c) = @_; | ||
| 316 | # # Load type by created filter | ||
| 317 | # my ($ct, $park, $memn) = @$cinfo{'table','parkey','memname',}; | ||
| 318 | #} | ||
| 319 | |||
| 320 | #sub makecinfo { | ||
| 321 | # my ($p, $cv) = @_; | ||
| 322 | #sub validate { | ||
| 323 | # my ($p) = @_; | ||
| 324 | #} | ||
| 325 | |||
| 326 | #sub pkeys { | ||
| 327 | # my ($p) = @_; | ||
| 328 | # | ||
| 329 | #} | ||
| 330 | #sub pkeyvals { | ||
| 331 | # my ($p, $e) = @_; | ||
| 332 | |||
| 333 | # Should not be used with LDAP | ||
| 334 | sub fetchautoid { | ||
| 335 | my ($p) = @_; | ||
| 336 | die("fetchautoid - Not Relevant with LDAP\n"); | ||
| 337 | } | ||
| 338 | # Should always produce 1 for Remedy | ||
| 339 | #sub pkeycnt { | ||
| 340 | # my ($p) = @_; | ||
| 341 | # | ||
| 342 | #sub invalidids { | ||
| 343 | # my ($p, @idv) = @_; | ||
| 344 | #} | ||
| 345 | # Irrelevant with Remedy API | ||
| 346 | #sub whereid { | ||
| 347 | # my ($p) = @_; | ||
| 348 | #} | ||
| 349 | |||
| 350 | #sub sqlvalesc { | ||
| 351 | # my ($v) = @_; | ||
| 352 | # $v =~ s/'/\'/g; | ||
| 353 | # return($v); | ||
| 354 | #} | ||
| 355 | |||
| 356 | sub invalues { | ||
| 357 | my ($vals, $attr) = @_; | ||
| 358 | my @ivc = map({"($attr=$_)";} @$vals); | ||
| 359 | #"(". | ||
| 360 | return join('', @ivc); | ||
| 361 | #.")"; | ||
| 362 | } | ||
| 363 | sub rangefilter { | ||
| 364 | my ($attr, $v) = @_; | ||
| 365 | } | ||
| 366 | |||
| 367 | # Internal method to generate LDAP search Filter. | ||
| 368 | sub wherefilter { | ||
| 369 | my ($e, %c) = @_; | ||
| 370 | my $w = ''; | ||
| 371 | my $fop = ' AND '; | ||
| 372 | #my $rnga = $c{'rnga'}; # Range attributes | ||
| 373 | if (ref($e) ne 'HASH') {die("No hash for filter generation");} | ||
| 374 | my @keys = sort keys(%$e); | ||
| 375 | my @qc; | ||
| 376 | # Assume hard values, treat everything as string (?) | ||
| 377 | # TODO: forcestr ? | ||
| 378 | @qc = map({ | ||
| 379 | my $v = $e->{$_}; | ||
| 380 | #my $rv = ref($v); | ||
| 381 | #if ($rnga->{$_} && ($rv eq 'ARRAY') && (@$v == 2)) {rangefilter($_, $v);} | ||
| 382 | if (ref($v) eq 'ARRAY') { | ||
| 383 | "(|".invalues($v, $_).")"; | ||
| 384 | } | ||
| 385 | # SQL Wildcard | ||
| 386 | elsif ($v =~ /%/) {"($_=$v)";} | ||
| 387 | # Detect numeric (likely numeric, not perfect) | ||
| 388 | elsif ($v =~ /^\d+$/) {"($_=$v)";} | ||
| 389 | # Assume string | ||
| 390 | else {"($_=".StoredHash::sqlvalesc($v).")";} | ||
| 391 | |||
| 392 | } @keys); | ||
| 393 | return(join($fop, @qc)); | ||
| 394 | } | ||
| 395 | sub allentvals { | ||
| 396 | my ($h) = @_; | ||
| 397 | } |