diff options
-rw-r--r-- | lib/Fripost/Schema.pm | 6 | ||||
-rwxr-xr-x | misc/deleteExpiredEntries.pl | 83 |
2 files changed, 87 insertions, 2 deletions
diff --git a/lib/Fripost/Schema.pm b/lib/Fripost/Schema.pm index b5dcdb6..2e1cbef 100644 --- a/lib/Fripost/Schema.pm +++ b/lib/Fripost/Schema.pm @@ -52,7 +52,8 @@ sub SASLauth { $self->suffix( ldap_explode_dn(@{$cfg{ldap_suffix}}) ); $self->whoami( canonical_dn( {fvu => $l}, {fvd => $d}, @{$self->suffix} )); - $self->ldap( Net::LDAP::->new( $cfg{ldap_uri}, async => 0 )); + $self->ldap( Net::LDAP::->new( $cfg{ldap_uri} // 'ldap://127.0.0.1:389/' + , async => 0 )); my $callback; if ($cfg{ldap_SASL_mechanism} eq 'DIGEST-MD5') { @@ -106,7 +107,8 @@ sub auth { $self->whoami( canonical_dn( {fvu => $l}, {fvd => $d}, @{$self->suffix} )); } - $self->ldap( Net::LDAP::->new( $cfg{ldap_uri}, async => 0 ) ); + $self->ldap( Net::LDAP::->new( $cfg{ldap_uri} // 'ldap://127.0.0.1:389/' + , async => 0 ) ); my $mesg = $self->ldap->bind( $self->whoami, password => $pw ); if ($mesg->code) { diff --git a/misc/deleteExpiredEntries.pl b/misc/deleteExpiredEntries.pl new file mode 100755 index 0000000..d4de6b6 --- /dev/null +++ b/misc/deleteExpiredEntries.pl @@ -0,0 +1,83 @@ +#!/usr/bin/perl + +use 5.010_000; +use strict; +use warnings; + +our $VERSION = '0.01'; + +=head1 NAME + +deleteExpiredEntries.pl - Clean the LDAP directory out of expired entries. + +=head1 SYNOPSIS + +B<deleteExpiredEntries.pl> [maximum age in seconds] + +=cut + + +####################################################################### +# + +use Pod::Usage; +use Config::Auto; +use lib 'lib'; +use Fripost::Schema; +use POSIX 'strftime'; + +# TODO: put that in a config file +my $config = { ldap_bind_dn => [ 'cn=DeletePendingEntries','ou=services','o=mailHosting','dc=fripost,dc=dev' ] + , ldap_uri => 'ldap://127.0.0.1:389/' + , ldap_suffix => [ 'ou=virtual','o=mailHosting','dc=fripost,dc=dev' ] + }; + +my $fp = Fripost::Schema::->auth( undef, 'deletependingentries', %$config ); + + +my $maxage = $ARGV[0] // 86400; # 24h by default +my $now = int(strftime "%s", gmtime); + +my $maxdate = Net::LDAP::Util::escape_filter_value( + strftime ("%Y%m%d%H%M%SZ", localtime($now - $maxage)) ); +my $entries = $fp->ldap->search ( + base => Fripost::Schema::Util::canonical_dn(@{$fp->suffix}), + scope => 'subtree', + deref => 'never', + filter => "(&(fripostPendingToken=*) + (createTimestamp<=$maxdate))", + attrs => [], + callback => \&delete_entry + ); +die $entries->error."\n" if $entries->code; +$fp->done; + +sub delete_entry { + shift; + my $obj = shift; + if (defined $obj and $obj->isa('Net::LDAP::Entry')) { +# print STDERR "Deleting DN ".$obj->dn."\n"; + $obj->delete; + my $mesg = $obj->update($fp->ldap); + die $mesg->error."\n" if $mesg->code; + } +} + + +####################################################################### +# + +=head1 AUTHOR + +Guilhem Moulin C<< <guilhem at fripost.org> >> + +=head1 COPYRIGHT + +Copyright 2013 Guilhem Moulin. + +=head1 LICENSE + +This program is free software; you can redistribute it and/or modify it +under the same terms as perl itself. + +=cut |