From f5ce3d17c00c2cfc6a178c0748d5e68bc2604db8 Mon Sep 17 00:00:00 2001 From: Daniel Berteaud Date: Fri, 18 May 2012 15:24:13 +0200 Subject: [PATCH] optimize update-ldap-pseudonyms --- .../e-smith/events/actions/update-ldap-pseudonyms | 38 +++++++++++++--------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/root/etc/e-smith/events/actions/update-ldap-pseudonyms b/root/etc/e-smith/events/actions/update-ldap-pseudonyms index d437cec..d4ceb7f 100644 --- a/root/etc/e-smith/events/actions/update-ldap-pseudonyms +++ b/root/etc/e-smith/events/actions/update-ldap-pseudonyms @@ -45,18 +45,15 @@ if (($ip eq 'disabled') || ($alias eq 'disabled')){ my $l = $c->get('ldap'); my $status = $l->prop('status') || "disabled"; -unless ($status eq "enabled" ) -{ +unless ($status eq "enabled" ){ warn "Not running action script $0, LDAP service not enabled!\n"; exit(0); } -my $hostname = $c->get('SystemName') - || die("Couldn't determine system name"); +my $hostname = $c->get('SystemName') || die("Couldn't determine system name"); $hostname = $hostname->value; -my $domain = $c->get('DomainName') - || die("Couldn't determine domain name"); +my $domain = $c->get('DomainName') || die("Couldn't determine domain name"); $domain = $domain->value; my @accounts; @@ -64,18 +61,15 @@ my $account; my $event = shift || die "Event name must be specified"; if ($event eq 'ldap-update' or $event eq 'bootstrap-ldap-save' or - $event =~ m/(pseudonym)\-(create|modify|delete)/) -{ + $event =~ m/(pseudonym)\-(create|modify|delete)/){ @accounts = ($a->users,$a->groups); push(@accounts, $a->get('admin')); } -else -{ +else{ my @name = @ARGV; die "Account name argument missing." unless scalar (@name) >= 1; - foreach my $name (@name) - { + foreach my $name (@name){ $account = $a->get($name); die "Account $name not found.\n" unless defined $account; @@ -95,8 +89,7 @@ $ldap->bind( ); my $result; -foreach my $acct (@accounts) -{ +foreach my $acct (@accounts){ my $key = $acct->key; my @pseudo = ("$key"); @@ -136,14 +129,27 @@ foreach my $acct (@accounts) $ou = "Groups"; $dn = "cn"; } - $result = $ldap->modify( + $result = $ldap->search(base => "ou=$ou,$base", + filter => "$dn=$key"); + $result->code && warn "failed to search for entry $dn=$key,ou=$ou,$base: ", $result->error; + my @mails = (); + foreach my $entry ($result->all_entries()){ + push @mails, $entry->get_value('mail'); + } + my $alias = join('\0', sort @alias); + my $mails = join('\0', sort @mails); + + unless ($alias eq $mails){ + $result = $ldap->modify( "$dn=$key,ou=$ou,$base", replace => { mail => \@alias } ); - $result->code && warn "failed to modify entry $dn=$key,ou=$ou,$base: ", $result->error; + $result->code && warn "failed to modify entry $dn=$key,ou=$ou,$base: ", $result->error; + } } + $ldap->unbind;