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. --- TODO.org | 18 +++++++++++++--- fripost-newalias | 63 ++++++++++++++++++++++++++++++++++++++++--------------- fripost-newdomain | 10 ++++----- 3 files changed, 65 insertions(+), 26 deletions(-) diff --git a/TODO.org b/TODO.org index b065808..3da47af 100644 --- a/TODO.org +++ b/TODO.org @@ -1,6 +1,18 @@ # -*- mode: org-mode; truncate-lines: nil -*- * Planned -** Send email to newly created users -** Log changes in log table - +** DONE Send email to newly created users +** TODO Log changes in log table +** TODO Add a flag `--send=EMAIL' to `fripost-passwd' to send the new password to an email +** TODO Add flags `--sign=KEY' and `--encrypt=KEY' (and extend the configuration file) to respectively sign and encrypt outgoing emails. +** TODO Detect cycles when creating aliases. (E.g., a->b, b->a should not be allowed.) +** TODO Add a subroutine is_email_valid with options (e.g., `allow_empty_login'), and add options to prompt_email (e.g., `allow_list', `allow_empty_login', `ensure_domain_known', `ensure_user_known', `allow_empty_user'). +** TODO Merge the tools into a single executable? +*** fripost-adduser -> fripost user add +*** fripost-mkpass -> fripost mkpass +*** fripost-newalias -> fripost alias add +*** fripost-newdomain -> fripost domain add +*** fripost-passwd -> fripost user passwd +*** fripost-searchalias -> fripost alias search +*** fripost-searchdomain -> fripost domain search +*** fripost-searchuser -> fripost user search 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(); diff --git a/fripost-newdomain b/fripost-newdomain index 5c4c2fb..2f204ee 100755 --- a/fripost-newdomain +++ b/fripost-newdomain @@ -150,7 +150,7 @@ $domain{domain} //= prompt "Domain name: "; $domain{isActive} = 'TRUE'; # Ensure that the domain is valid. -Email::Valid->address('test@'.$domain{domain}) +Email::Valid->address('fake@'.$domain{domain}) or die "Error: Invalid domain `$domain{domain}'.\n"; if (defined $conf->{owner}) { @@ -220,7 +220,7 @@ else { # Create aliases. sub create_alias { - my ($ldap, $from, $to, $owner) = @_; + my ($ldap, $from, $to) = @_; my %alias = (address => $from, goto => $to); @@ -233,15 +233,13 @@ sub create_alias { return unless grep { $_->{goto} eq $alias{goto} } $res->entries; } - $alias{owner} = $owner if defined $owner; $alias{isActive} = 'TRUE'; $ldap->alias->add( \%alias ); say "Created alias from $from to $to."; } -create_alias($ldap, 'abuse@' . $domain{domain} ,'abuse@fripost.org', $domain{owner}); -create_alias($ldap, 'postmaster@' . $domain{domain},'postmaster@fripost.org', $domain{owner}); - +create_alias($ldap, 'abuse@' . $domain{domain} ,'abuse@fripost.org'); +create_alias($ldap, 'postmaster@' . $domain{domain},'postmaster@fripost.org'); $ldap->unbind(); -- cgit v1.2.3