Conditional sender rewrite based on recipient address

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

Conditional sender rewrite based on recipient address

Nicolás-2
Hi,

I'm running Postfix 2.9.6 and I'm trying to do a conditional sender
rewrite based on recipient address. Machine sends e-mails as
[hidden email] by default, but if the recipient is
[hidden email] I want to rewrite sender to be
[hidden email].

To do so, I've tried doing the following:

1) In master.cnf I added:
    toext     unix  -       -       n       -       -       smtp -o
canonical_maps=hash:/etc/postfix/generic_toext

2) In the transport_maps map I added (and postmapped):
    [hidden email]   toext:

3) Content of /etc/postfix/generic_toext (postmapped):
    [hidden email]    [hidden email]

This seems not to work, as the original sender is still the same. As per
[1] something like this could be done by using the canonical_maps
parameter, however it doesn't seem to work in the way I'm using it.
Could someone point me what's wrong in the configuration above?

Thanks.

Nicolás

   [1]: http://www.postfix.org/ADDRESS_REWRITING_README.html#canonical
Reply | Threaded
Open this post in threaded view
|

Re: Conditional sender rewrite based on recipient address

Noel Jones-2
On 11/2/2017 7:48 AM, [hidden email] wrote:

> Hi,
>
> I'm running Postfix 2.9.6 and I'm trying to do a conditional sender
> rewrite based on recipient address. Machine sends e-mails as
> [hidden email] by default, but if the recipient is
> [hidden email] I want to rewrite sender to be
> [hidden email].
>
> To do so, I've tried doing the following:
>
> 1) In master.cnf I added:
>    toext     unix  -       -       n       -       -       smtp -o
> canonical_maps=hash:/etc/postfix/generic_toext
>
> 2) In the transport_maps map I added (and postmapped):
>    [hidden email]   toext:
>
> 3) Content of /etc/postfix/generic_toext (postmapped):
>    [hidden email]    [hidden email]
>
> This seems not to work, as the original sender is still the same. As
> per [1] something like this could be done by using the
> canonical_maps parameter, however it doesn't seem to work in the way
> I'm using it. Could someone point me what's wrong in the
> configuration above?

canonical_maps cannot be used as a -o override on a transport.
Postfix is not able to perform conditional sender rewrite based on
the recipient address.  You can use a milter to do this.



  -- Noel Jones
Reply | Threaded
Open this post in threaded view
|

Re: Conditional sender rewrite based on recipient address

Wietse Venema
Noel Jones:

> On 11/2/2017 7:48 AM, [hidden email] wrote:
> > Hi,
> >
> > I'm running Postfix 2.9.6 and I'm trying to do a conditional sender
> > rewrite based on recipient address. Machine sends e-mails as
> > [hidden email] by default, but if the recipient is
> > [hidden email] I want to rewrite sender to be
> > [hidden email].
> >
> > To do so, I've tried doing the following:
> >
> > 1) In master.cnf I added:
> >    toext     unix  -       -       n       -       -       smtp -o
> > canonical_maps=hash:/etc/postfix/generic_toext

Perhaps with smtp_generic_maps, but not with canonical
maps because that happens before the queue.

        Wietse
Reply | Threaded
Open this post in threaded view
|

Re: Conditional sender rewrite based on recipient address

Rick van Rein
Hi,

> Perhaps with smtp_generic_maps, but not with canonical
> maps

Which raises something I've wondered before:

why is it that the generic_maps cannot be separately
setup for header/envelope, and for sender/recipient, like
can be done with canonical_maps?  I've been struggling with
this in the past, and ended up renaming everything upon
entry and casting it back through the virtual table, but
it felt like generic_maps would have been a better way, had
it distinguished the things it would substitute.

-Rick
Reply | Threaded
Open this post in threaded view
|

Re: Conditional sender rewrite based on recipient address

Nicolás-2
In reply to this post by Wietse Venema
El 2017-11-03 11:05, [hidden email] escribió:

> Noel Jones:
>> On 11/2/2017 7:48 AM, [hidden email] wrote:
>> > Hi,
>> >
>> > I'm running Postfix 2.9.6 and I'm trying to do a conditional sender
>> > rewrite based on recipient address. Machine sends e-mails as
>> > [hidden email] by default, but if the recipient is
>> > [hidden email] I want to rewrite sender to be
>> > [hidden email].
>> >
>> > To do so, I've tried doing the following:
>> >
>> > 1) In master.cnf I added:
>> >    toext     unix  -       -       n       -       -       smtp -o
>> > canonical_maps=hash:/etc/postfix/generic_toext
>
> Perhaps with smtp_generic_maps, but not with canonical
> maps because that happens before the queue.
>
> Wietse

Thank you.

By now I'm trying to implement a content filter to change some headers
of the received message. To do that, I added a service to the master.cf
file:

   external   unix  -       n       n       -       -       pipe
     flags=Fq user=user:user argv=/opt/scripts/external.sh ${recipient}

The script is a very simple bash script that replaces the 'From' header:

     #!/bin/bash

     content=$(cat)
     contfin=$(sed 's/From: [hidden email]/From:
[hidden email]/' <<< "$content")

     TMPFILE=`mktemp`
     echo $contfin > $TMPFILE

     cat $TMPFILE | /usr/sbin/sendmail -G -i -f [hidden email]
-- $1

     /bin/rm -f $TMPFILE

     exit $?

In transports map file (postmapped):

     [hidden email]  external:

The script seems to do what it should, however, in the Postfix logs it
seems to loop, like the message was put in the queue again and passed to
the queue over and over (and the message never arrives):

     Nov  3 11:13:52 machine postfix/pickup[19571]: 965731DA9: uid=0
from=<root>
     Nov  3 11:13:52 machine postfix/cleanup[19751]: 965731DA9:
message-id=<[hidden email]>
     Nov  3 11:13:52 machine postfix/qmgr[19572]: 965731DA9:
from=<[hidden email]>, size=307, nrcpt=1 (queue active)
     Nov  3 11:13:52 machine postfix/pickup[19571]: AB1CA613A: uid=110
from=<[hidden email]>
     Nov  3 11:13:52 machine postfix/cleanup[19751]: AB1CA613A:
message-id=<>
     Nov  3 11:13:52 machine postfix/pipe[19753]: 965731DA9:
to=<[hidden email]>, relay=external, delay=0.12,
delays=0.08/0/0/0.04, dsn=2.0.0, status=sent (delivered via external
service)
     Nov  3 11:13:52 machine postfix/qmgr[19572]: 965731DA9: removed
     Nov  3 11:13:52 machine postfix/qmgr[19572]: AB1CA613A:
from=<[hidden email]>, size=117, nrcpt=1 (queue active)
     Nov  3 11:13:52 machine postfix/pickup[19571]: BDEB56499: uid=110
from=<[hidden email]>
     Nov  3 11:13:52 machine postfix/cleanup[19751]: BDEB56499:
message-id=<>
     Nov  3 11:13:52 machine postfix/pipe[19753]: AB1CA613A:
to=<[hidden email]>, relay=external, delay=0.1,
delays=0.06/0/0/0.04, dsn=2.0.0, status=sent (delivered via external
service)
     Nov  3 11:13:52 machine postfix/qmgr[19572]: AB1CA613A: removed
     Nov  3 11:13:52 machine postfix/qmgr[19572]: BDEB56499:
from=<[hidden email]>, size=117, nrcpt=1 (queue active)
     Nov  3 11:13:52 machine postfix/pickup[19571]: CBB38613A: uid=110
from=<[hidden email]>
     Nov  3 11:13:52 machine postfix/cleanup[19751]: CBB38613A:
message-id=<>
     Nov  3 11:13:52 machine postfix/pipe[19753]: BDEB56499:
to=<[hidden email]>, relay=external, delay=0.09,
delays=0.05/0/0/0.03, dsn=2.0.0, status=sent (delivered via external
service)
     Nov  3 11:13:52 machine postfix/qmgr[19572]: BDEB56499: removed

How can I avoid this behavior? Once the message is modified I'd like to
send it without passing it again to the filter.

Thanks again.

Reply | Threaded
Open this post in threaded view
|

Re: Conditional sender rewrite based on recipient address

Noel Jones-2
On 11/3/2017 6:26 AM, [hidden email] wrote:

> El 2017-11-03 11:05, [hidden email] escribió:
>> Noel Jones:
>>> On 11/2/2017 7:48 AM, [hidden email] wrote:
>>> > Hi,
>>> >
>>> > I'm running Postfix 2.9.6 and I'm trying to do a conditional
>>> sender
>>> > rewrite based on recipient address. Machine sends e-mails as
>>> > [hidden email] by default, but if the recipient is
>>> > [hidden email] I want to rewrite sender to be
>>> > [hidden email].
>>> >
>>> > To do so, I've tried doing the following:
>>> >
>>> > 1) In master.cnf I added:
>>> >    toext     unix  -       -       n       -       -       smtp -o
>>> > canonical_maps=hash:/etc/postfix/generic_toext
>>
>> Perhaps with smtp_generic_maps, but not with canonical
>> maps because that happens before the queue.
>>
>>     Wietse
>
> Thank you.
>
> By now I'm trying to implement a content filter to change some
> headers of the received message. To do that, I added a service to
> the master.cf file:
>
>   external   unix  -       n       n       -       -       pipe
>     flags=Fq user=user:user argv=/opt/scripts/external.sh ${recipient}
>
> The script is a very simple bash script that replaces the 'From'
> header:
>
>     #!/bin/bash
>
>     content=$(cat)
>     contfin=$(sed 's/From: [hidden email]/From:
> [hidden email]/' <<< "$content")
>
>     TMPFILE=`mktemp`
>     echo $contfin > $TMPFILE
>
>     cat $TMPFILE | /usr/sbin/sendmail -G -i -f
> [hidden email] -- $1
>
>     /bin/rm -f $TMPFILE
>
>     exit $?
>
> In transports map file (postmapped):
>
>     [hidden email]  external:
>
> The script seems to do what it should, however, in the Postfix logs
> it seems to loop, like the message was put in the queue again and
> passed to the queue over and over (and the message never arrives):

Yes, it does loop because the transport map will always send the
mail to the 'external' transport.

Two choices to fix this.  The clean way is to setup a second postfix
instance and have your script submit the mail to that second
instance. This clearly separates the mangle and delivery functions
and will be fairly easy to maintain.
http://www.postfix.org/MULTI_INSTANCE_README.html

The other way is to have your script also mangle the envelope
recipient address so the transport table no longer matches.  Then
use smtp_generic_maps to correct the recipient address during delivery.



  -- Noel Jones
Reply | Threaded
Open this post in threaded view
|

Re: Conditional sender rewrite based on recipient address

Nicolás-2
El 2017-11-03 15:15, Noel Jones escribió:

> On 11/3/2017 6:26 AM, [hidden email] wrote:
>> El 2017-11-03 11:05, [hidden email] escribió:
>>> Noel Jones:
>>>> On 11/2/2017 7:48 AM, [hidden email] wrote:
>>>> > Hi,
>>>> >
>>>> > I'm running Postfix 2.9.6 and I'm trying to do a conditional
>>>> sender
>>>> > rewrite based on recipient address. Machine sends e-mails as
>>>> > [hidden email] by default, but if the recipient is
>>>> > [hidden email] I want to rewrite sender to be
>>>> > [hidden email].
>>>> >
>>>> > To do so, I've tried doing the following:
>>>> >
>>>> > 1) In master.cnf I added:
>>>> >    toext     unix  -       -       n       -       -       smtp -o
>>>> > canonical_maps=hash:/etc/postfix/generic_toext
>>>
>>> Perhaps with smtp_generic_maps, but not with canonical
>>> maps because that happens before the queue.
>>>
>>>     Wietse
>>
>> Thank you.
>>
>> By now I'm trying to implement a content filter to change some
>> headers of the received message. To do that, I added a service to
>> the master.cf file:
>>
>>   external   unix  -       n       n       -       -       pipe
>>     flags=Fq user=user:user argv=/opt/scripts/external.sh ${recipient}
>>
>> The script is a very simple bash script that replaces the 'From'
>> header:
>>
>>     #!/bin/bash
>>
>>     content=$(cat)
>>     contfin=$(sed 's/From: [hidden email]/From:
>> [hidden email]/' <<< "$content")
>>
>>     TMPFILE=`mktemp`
>>     echo $contfin > $TMPFILE
>>
>>     cat $TMPFILE | /usr/sbin/sendmail -G -i -f
>> [hidden email] -- $1
>>
>>     /bin/rm -f $TMPFILE
>>
>>     exit $?
>>
>> In transports map file (postmapped):
>>
>>     [hidden email]  external:
>>
>> The script seems to do what it should, however, in the Postfix logs
>> it seems to loop, like the message was put in the queue again and
>> passed to the queue over and over (and the message never arrives):
>
> Yes, it does loop because the transport map will always send the
> mail to the 'external' transport.
>
> Two choices to fix this.  The clean way is to setup a second postfix
> instance and have your script submit the mail to that second
> instance. This clearly separates the mangle and delivery functions
> and will be fairly easy to maintain.
> http://www.postfix.org/MULTI_INSTANCE_README.html
>
> The other way is to have your script also mangle the envelope
> recipient address so the transport table no longer matches.  Then
> use smtp_generic_maps to correct the recipient address during delivery.
>
>

Got it, I'll try to go the clean way.

Thanks so much.

Nicolás

>
>   -- Noel Jones
Reply | Threaded
Open this post in threaded view
|

Re: Conditional sender rewrite based on recipient address

Wietse Venema
In reply to this post by Rick van Rein
Rick van Rein:

> Hi,
>
> > Perhaps with smtp_generic_maps, but not with canonical
> > maps
>
> Which raises something I've wondered before:
>
> why is it that the generic_maps cannot be separately
> setup for header/envelope, and for sender/recipient, like
> can be done with canonical_maps?

I am not convinced that is a good fit for smtp_generic_maps (or for
canonical_maps). Designing a general-purpose system for context-dependent
rewriting is hard, and using it may be even harder.

> I've been struggling with
> this in the past, and ended up renaming everything upon
> entry and casting it back through the virtual table, but

Indeed. Use smtp_generic_maps to convert X into a globally-valid
address, and use virtual_alias_maps to deliver that address to X.
X may be a fantasy domain or a real address.

        Wietse