From 0461d89edb3f8e272697726208ab7747c30a81df Mon Sep 17 00:00:00 2001 From: Guilhem Moulin Date: Thu, 31 May 2012 18:30:22 +0200 Subject: Catch-All aliases. --- fripost-newalias | 63 +++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 46 insertions(+), 17 deletions(-) (limited to 'fripost-newalias') diff --git a/fripost-newalias b/fripost-newalias index 0ca009b..3fc68f8 100755 --- a/fripost-newalias +++ b/fripost-newalias @@ -23,6 +23,33 @@ If I is not fully qualified, C is appended. If I is already an existing username or alias, B raises an error. +Inserted aliases conform to Postfix's B(5) alias table format, +with the restriction that I has to be either in the form: + +=over 4 + +=item . + +I@I, to redirect emails for I@I to I, or + +=item . + +@I, to catch all emails for users in I and redirect them +to I. +This form has the lowest precedence: If there is an alias from +I@I to I, emails to I@I will be +redirected to I only. +See B(5) for details and warnings. + +=back + +If serveral entries are matching, for instance if there are an alias from +I@I to I and another for I@I to +I, emails to I@I will be redirected to BOTH I +and I. Note that B forbids the creation of such +aliases, unless B<--force> is set. + + =head1 OPTIONS =over 8 @@ -157,7 +184,7 @@ say "goto adress: $goto"; # Show from adresses @addr = grep { - if (Email::Valid->address($_)) { + if (Email::Valid->address('fake'.$_)) { # Warn if the domain is unknown. my $domain = (split /\@/, $_, 2)[1]; if ($ldap->domain->search({ domain => $domain })->count) { @@ -183,28 +210,30 @@ confirm_or_abort(); ## Insert alias into database for my $addr (@addr) { - my $rs = $ldap->alias->search({ address => $addr }); - if (!$rs->count or defined $conf->{force}) { - if (!$ldap->user->search({ username => $addr })->count) { - if (!$conf->{pretend}) { - $ldap->alias->add({ address => $addr, goto => $goto, - isActive => 'TRUE' }); - say "New alias added from $addr to $goto."; - } else { - vsay "Pretending, will not add alias." - } - } - else { - die "Error: Username $addr already exists.\n"; - } - } - else { + + my ($u,$d) = split /\@/, $addr, 2; + my $rs; + + # Ensure that the alias doesn't already exist. + $rs = $ldap->alias->search({ address => $addr }); + if ($rs->count and not (defined $conf->{force})) { print STDERR "Error: Alias $addr already exists. "; print STDERR "(Targetting to "; print STDERR (join ', ', map { $_->{goto} } $rs->entries); say STDERR ".)"; exit 1; } + + die "Error: Username $addr exists.\n" + if ($ldap->user->search({ username => $addr })->count); + + if (!$conf->{pretend}) { + $ldap->alias->add({ address => $addr, goto => $goto, + isActive => 'TRUE' }); + say "New alias added from $addr to $goto."; + } else { + vsay "Pretending, will not add alias." + } } $ldap->unbind(); -- cgit v1.2.3