Forward email based on Regexps

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

Forward email based on Regexps

Frank DiGennaro

All;

    I’ve made progress with this problem. By adding virtual_alias_maps = regexp:/etc/postfix/virtual_users at the bottom of main.cf and adding the regexps below, and running “postmap /etc/postfix/virtual_users “, the two catch-alls work fine, but the “longer” matches do not and ultimately they get delivered to the catch-all address. I’m almost there. What am I missing?

Thanks in advance;

Frank

 

 

 

>> From: Frank DiGennaro [[hidden email]]
>> Sent: Monday, October 5, 2020 1:34 PM
>> To: '[hidden email]' <[hidden email]>
>> Subject: Forward email based on Regexps

>>

>> Hello;

 >>   I’m pretty new to postfix but I’m really glad that I finally made the switch from sendmail. Postfix is just so much easier to use. This is my scenario. I need to route emails based on regexps and I know how to write them but I’m not exactly sure where in the configs to put them. For example, I want to do this

>> 

>> /^To: [0-9]{10,11}@faxserver.acmewidgets.net/   faxadmin        # The user part is a fax number

>> /^To: [0-9]{10,11}@acmewidgets.net/                     faxadmin        # The user part is a fax number

>> /^To: [hidden email]                                       admin             # Catch-all for the domain acmewidgets.com

>> /^To: [hidden email]                                                       fooadmin       # Catch-all for the domain foo.com

>>

>> But I’m not sure these belong. Any insight at all would be greatly appreciated.

>> Thanks;

>> Frank

 

Reply | Threaded
Open this post in threaded view
|

Re: Forward email based on Regexps

Wietse Venema
Frank DiGennaro:
> All;
>
>     I've made progress with this problem. By adding virtual_alias_maps =
> regexp:/etc/postfix/virtual_users at the bottom of main.cf and adding the
> regexps below, and running "postmap /etc/postfix/virtual_users ", the two
> catch-alls work fine, but the "longer" matches do not and ultimately they
> get delivered to the catch-all address. I'm almost there. What am I missing?
>
> Thanks in advance;

Maybe you can show what you did. It is easy to make regexp mistakes.
Note that regexp and pcre tables are parsed in the specified order,
and that the first match wins.

You can test stuff with the postmap command.

postmap -q email-address pcre:/path/to/file

        Wietse
Reply | Threaded
Open this post in threaded view
|

RE: Forward email based on Regexps

Frank DiGennaro
Thanks for the reply. This is what I have in my 'virtual_users' file
verbatim. I've even kept the domain names and users the same.

# Regexp # User to forward to
/\d{10,}@fax.mobilepbx.net/     asterisk (I've tried both
\d{10,} and [0-9]{10,} without success)
/\[0-9]{10,}@mobilepbx.net/     asterisk
#/[hidden email]/     asterisk (When I try a specific
number like this, this does work)
/.*@mobilepbx.net/               mobilepbx

/.*@voipbiz.org/                 voipbiz

/.*@contessa.net/               [hidden email]


When I do a couple of tests, this is what I get.

(10 digits) (No)
postmap -q [hidden email] regexp:/etc/postfix/virtual_users    
mobilepbx

(11 digits) (No)
postmap -q [hidden email] regexp:/etc/postfix/virtual_users
mobilepbx

(Yes)
postmap -q [hidden email] regexp:/etc/postfix/virtual_users  
mobilepbx

(Yes)
postmap -q [hidden email] regexp:/etc/postfix/virtual_users
voipbiz
postmap -q [hidden email] regexp:/etc/postfix/virtual_users        
voipbiz

(Yes)
postmap -q [hidden email] regexp:/etc/postfix/virtual_users

[hidden email]

Thanks;
Frank


-----Original Message-----
From: [hidden email]
[mailto:[hidden email]] On Behalf Of Wietse Venema
Sent: Tuesday, October 6, 2020 1:37 PM
To: Postfix users <[hidden email]>
Subject: Re: Forward email based on Regexps

Frank DiGennaro:
> All;
>
>     I've made progress with this problem. By adding virtual_alias_maps
> = regexp:/etc/postfix/virtual_users at the bottom of main.cf and
> adding the regexps below, and running "postmap
> /etc/postfix/virtual_users ", the two catch-alls work fine, but the
> "longer" matches do not and ultimately they get delivered to the catch-all
address. I'm almost there. What am I missing?
>
> Thanks in advance;

Maybe you can show what you did. It is easy to make regexp mistakes.
Note that regexp and pcre tables are parsed in the specified order, and that
the first match wins.

You can test stuff with the postmap command.

postmap -q email-address pcre:/path/to/file

        Wietse

Reply | Threaded
Open this post in threaded view
|

Re: Forward email based on Regexps

Wietse Venema
Frank DiGennaro:
> Thanks for the reply. This is what I have in my 'virtual_users' file
> verbatim. I've even kept the domain names and users the same.
>
> # Regexp # User to forward to
> /\d{10,}@fax.mobilepbx.net/     asterisk (I've tried both

That is PCRE not REGEXP syntax.

        Wietse
Reply | Threaded
Open this post in threaded view
|

Re: Forward email based on Regexps

Noel Jones-2
In reply to this post by Frank DiGennaro
On 10/6/2020 3:56 PM, Frank DiGennaro wrote:
> Thanks for the reply. This is what I have in my 'virtual_users' file
> verbatim. I've even kept the domain names and users the same.
>
> # Regexp # User to forward to
> /\d{10,}@fax.mobilepbx.net/     asterisk (I've tried both
> \d{10,} and [0-9]{10,} without success)

\d is a pcre construct that not all regexp libraries recognize.
You should anchor the expression to prevent unexpected matches.
The result should have a domain (even if it's @localhost) to prevent
surprises.

> /\[0-9]{10,}@mobilepbx.net/     asterisk

Escaping the first [ makes it match a literal "[" instead of a
character class. Don't do that.

/^[0-9]+@fax\.mobliepbx\.net$/   [hidden email]





   -- Noel Jones


> #/[hidden email]/     asterisk (When I try a specific
> number like this, this does work)
> /.*@mobilepbx.net/               mobilepbx
>
> /.*@voipbiz.org/                 voipbiz
>
> /.*@contessa.net/               [hidden email]
>
>
> When I do a couple of tests, this is what I get.
>
> (10 digits) (No)
> postmap -q [hidden email] regexp:/etc/postfix/virtual_users
> mobilepbx
>
> (11 digits) (No)
> postmap -q [hidden email] regexp:/etc/postfix/virtual_users
> mobilepbx
>
> (Yes)
> postmap -q [hidden email] regexp:/etc/postfix/virtual_users
> mobilepbx
>
> (Yes)
> postmap -q [hidden email] regexp:/etc/postfix/virtual_users
> voipbiz
> postmap -q [hidden email] regexp:/etc/postfix/virtual_users
> voipbiz
>
> (Yes)
> postmap -q [hidden email] regexp:/etc/postfix/virtual_users
>
> [hidden email]
>
> Thanks;
> Frank
>
>
> -----Original Message-----
> From: [hidden email]
> [mailto:[hidden email]] On Behalf Of Wietse Venema
> Sent: Tuesday, October 6, 2020 1:37 PM
> To: Postfix users <[hidden email]>
> Subject: Re: Forward email based on Regexps
>
> Frank DiGennaro:
>> All;
>>
>>      I've made progress with this problem. By adding virtual_alias_maps
>> = regexp:/etc/postfix/virtual_users at the bottom of main.cf and
>> adding the regexps below, and running "postmap
>> /etc/postfix/virtual_users ", the two catch-alls work fine, but the
>> "longer" matches do not and ultimately they get delivered to the catch-all
> address. I'm almost there. What am I missing?
>>
>> Thanks in advance;
>
> Maybe you can show what you did. It is easy to make regexp mistakes.
> Note that regexp and pcre tables are parsed in the specified order, and that
> the first match wins.
>
> You can test stuff with the postmap command.
>
> postmap -q email-address pcre:/path/to/file
>
> Wietse
>

Reply | Threaded
Open this post in threaded view
|

RE: Forward email based on Regexps

Frank DiGennaro
In reply to this post by Wietse Venema
Wietse;
    I made the change but no luck. What I don't understand is how some
regex's work and others don't considering regex's themselves are so simple.
I mean, they should all work or they should all now work.
Thanks;
Frank  

-----Original Message-----
From: [hidden email]
[mailto:[hidden email]] On Behalf Of Wietse Venema
Sent: Tuesday, October 6, 2020 6:07 PM
To: Postfix users <[hidden email]>
Subject: Re: Forward email based on Regexps

Frank DiGennaro:
> Thanks for the reply. This is what I have in my 'virtual_users' file
> verbatim. I've even kept the domain names and users the same.
>
> # Regexp # User to forward to
> /\d{10,}@fax.mobilepbx.net/     asterisk (I've tried both

That is PCRE not REGEXP syntax.

        Wietse

Reply | Threaded
Open this post in threaded view
|

Re: Forward email based on Regexps

Wietse Venema
Frank DiGennaro:
> Thanks for the reply. This is what I have in my 'virtual_users' file
> verbatim. I've even kept the domain names and users the same.
>
> # Regexp # User to forward to
> /\d{10,}@fax.mobilepbx.net/     asterisk (I've tried both

Wietse:
> That is PCRE not REGEXP syntax.

Frank DiGennaro:
> Wietse;
>     I made the change but no luck. What I don't understand is how some
> regex's work and others don't considering regex's themselves are so simple.
> I mean, they should all work or they should all now work.

If you show what you did. then I am sure that someone can explain what
mistake you made. Otherwise this email thread is just a waste of time.

        Wietse
Reply | Threaded
Open this post in threaded view
|

RE: Forward email based on Regexps

Frank DiGennaro
I apologize. I'm an idiot. I thought that I was showing you what I did. I
showed how I defined the virtual_alias_maps in man.cf, added the regex's
into the file 'virtual_users', and ran several tests such as 'postmap -q
[hidden email] pcre:/etc/postfix/virtual_users'. What am I not
showing? I appreciate all your help.
Thanks;
FSD    
     

-----Original Message-----
From: [hidden email]
[mailto:[hidden email]] On Behalf Of Wietse Venema
Sent: Wednesday, October 7, 2020 2:26 PM
To: Postfix users <[hidden email]>
Subject: Re: Forward email based on Regexps

Frank DiGennaro:
> Thanks for the reply. This is what I have in my 'virtual_users' file
> verbatim. I've even kept the domain names and users the same.
>
> # Regexp # User to forward to
> /\d{10,}@fax.mobilepbx.net/     asterisk (I've tried both

Wietse:
> That is PCRE not REGEXP syntax.

Frank DiGennaro:
> Wietse;
>     I made the change but no luck. What I don't understand is how some
> regex's work and others don't considering regex's themselves are so
simple.
> I mean, they should all work or they should all now work.

If you show what you did. then I am sure that someone can explain what
mistake you made. Otherwise this email thread is just a waste of time.

        Wietse

Reply | Threaded
Open this post in threaded view
|

Re: Forward email based on Regexps

Wietse Venema
Frank DiGennaro:
> Thanks for the reply. This is what I have in my 'virtual_users' file
> verbatim. I've even kept the domain names and users the same.
>
> # Regexp # User to forward to
> /\d{10,}@fax.mobilepbx.net/     asterisk (I've tried both

Wietse:
> That is PCRE not REGEXP syntax.

Frank DiGennaro:
> Wietse;
>     I made the change but no luck. What I don't understand is how some
> regex's work and others don't considering regex's themselves are so
simple.
> I mean, they should all work or they should all now work.

Wietse:
> If you show what you did. then I am sure that someone can explain what
> mistake you made. Otherwise this email thread is just a waste of time.

Frank DiGennaro:
> I apologize. I'm an idiot. I thought that I was showing you what I did. I
> showed how I defined the virtual_alias_maps in man.cf, added the regex's
> into the file 'virtual_users', and ran several tests such as 'postmap -q
> [hidden email] pcre:/etc/postfix/virtual_users'. What am I not
> showing? I appreciate all your help.

- The regexp or pcre table, instead of saying you made some change.

- The (postmap) command and output, instead of saying it does not work.

        Wietse
Reply | Threaded
Open this post in threaded view
|

Re: Forward email based on Regexps

Viktor Dukhovni
In reply to this post by Frank DiGennaro
On Wed, Oct 07, 2020 at 02:44:47PM -0400, Frank DiGennaro wrote:

> I apologize. I'm an idiot. I thought that I was showing you what I did. I
> showed how I defined the virtual_alias_maps in man.cf, added the regex's
> into the file 'virtual_users', and ran several tests such as 'postmap -q
> [hidden email] pcre:/etc/postfix/virtual_users'. What am I not
> showing? I appreciate all your help.

Context matters.  Piecing the puzzle together from isolated fragments of
separate messages is too much to expect of those willing and able to
help.

Therefore, you may need to repeat yourself, by posting a single message,
containing all the relevant information, that is, per:

    http://www.postfix.org/DEBUG_README.html#mail

    - Output of: postconf -nf
    - Output of: postconf -Mf
    - Logs showing what happened when you tested
    - A brief note stating what you expected instead

A quick glance over the thread, shows that:

    - You have wildcard rules, keep in mind that virtual expansion
      is recursive, and the RHS of each rule is subject to further
      lookups until either there's no match, or the result is the
      same as the input.

    - You don't qualify your virtual(5) RHS values with an explicit
      domain.  That's generally unwise.  They'll get implicitly
      qualified with $myorigin, and perhaps then match some other
      pattern in the PCRE table?

    - Your regular expressions are sloppy, missing "^" and "$"
      anchors, fail to escape literal "." as "\.", ... While
      it may be simple to write regular expressions that match
      a particular input pattern, it is more difficult to write
      *correct* regular expressions that match *only* the desired
      pattern and not also lots of other patterns that should not
      be matched.

    - You posted no logs that showed the unexpected results, with
      a brief explanation of what you expected instead.

--
    Viktor.
Reply | Threaded
Open this post in threaded view
|

RE: Forward email based on Regexps

Frank DiGennaro
In reply to this post by Noel Jones-2
I went back and made the changes Noel Jones suggested Like so.

/^[0-9]+@fax.mobilepbx.net/     asterisk
/^[0-9]+@mobilepbx.net/         asterisk

This now works. Thank you very, very much.
Regards;
FSD

-----Original Message-----
From: [hidden email] [mailto:[hidden email]] On Behalf Of Noel Jones
Sent: Tuesday, October 6, 2020 6:24 PM
To: [hidden email]
Subject: Re: Forward email based on Regexps

On 10/6/2020 3:56 PM, Frank DiGennaro wrote:
> Thanks for the reply. This is what I have in my 'virtual_users' file
> verbatim. I've even kept the domain names and users the same.
>
> # Regexp # User to forward to
> /\d{10,}@fax.mobilepbx.net/     asterisk (I've tried both
> \d{10,} and [0-9]{10,} without success)

\d is a pcre construct that not all regexp libraries recognize.
You should anchor the expression to prevent unexpected matches.
The result should have a domain (even if it's @localhost) to prevent surprises.

> /\[0-9]{10,}@mobilepbx.net/     asterisk

Escaping the first [ makes it match a literal "[" instead of a character class. Don't do that.

/^[0-9]+@fax\.mobliepbx\.net$/   [hidden email]





   -- Noel Jones


> #/[hidden email]/     asterisk (When I try a specific
> number like this, this does work)
> /.*@mobilepbx.net/               mobilepbx
>
> /.*@voipbiz.org/                 voipbiz
>
> /.*@contessa.net/               [hidden email]
>
>
> When I do a couple of tests, this is what I get.
>
> (10 digits) (No)
> postmap -q [hidden email] regexp:/etc/postfix/virtual_users
> mobilepbx
>
> (11 digits) (No)
> postmap -q [hidden email] regexp:/etc/postfix/virtual_users
> mobilepbx
>
> (Yes)
> postmap -q [hidden email] regexp:/etc/postfix/virtual_users
> mobilepbx
>
> (Yes)
> postmap -q [hidden email] regexp:/etc/postfix/virtual_users
> voipbiz postmap -q [hidden email]
> regexp:/etc/postfix/virtual_users voipbiz
>
> (Yes)
> postmap -q [hidden email] regexp:/etc/postfix/virtual_users
>
> [hidden email]
>
> Thanks;
> Frank
>
>
> -----Original Message-----
> From: [hidden email]
> [mailto:[hidden email]] On Behalf Of Wietse Venema
> Sent: Tuesday, October 6, 2020 1:37 PM
> To: Postfix users <[hidden email]>
> Subject: Re: Forward email based on Regexps
>
> Frank DiGennaro:
>> All;
>>
>>      I've made progress with this problem. By adding
>> virtual_alias_maps = regexp:/etc/postfix/virtual_users at the bottom
>> of main.cf and adding the regexps below, and running "postmap
>> /etc/postfix/virtual_users ", the two catch-alls work fine, but the
>> "longer" matches do not and ultimately they get delivered to the
>> catch-all
> address. I'm almost there. What am I missing?
>>
>> Thanks in advance;
>
> Maybe you can show what you did. It is easy to make regexp mistakes.
> Note that regexp and pcre tables are parsed in the specified order,
> and that the first match wins.
>
> You can test stuff with the postmap command.
>
> postmap -q email-address pcre:/path/to/file
>
> Wietse
>

Reply | Threaded
Open this post in threaded view
|

RE: Forward email based on Regexps

Frank DiGennaro
In reply to this post by Viktor Dukhovni
Thanks for the heads up. I will do that.
Regards;
Frank

-----Original Message-----
From: [hidden email]
[mailto:[hidden email]] On Behalf Of Viktor Dukhovni
Sent: Wednesday, October 7, 2020 3:09 PM
To: [hidden email]
Subject: Re: Forward email based on Regexps

On Wed, Oct 07, 2020 at 02:44:47PM -0400, Frank DiGennaro wrote:

> I apologize. I'm an idiot. I thought that I was showing you what I
> did. I showed how I defined the virtual_alias_maps in man.cf, added
> the regex's into the file 'virtual_users', and ran several tests such
> as 'postmap -q [hidden email]
> pcre:/etc/postfix/virtual_users'. What am I not showing? I appreciate all
your help.

Context matters.  Piecing the puzzle together from isolated fragments of
separate messages is too much to expect of those willing and able to help.

Therefore, you may need to repeat yourself, by posting a single message,
containing all the relevant information, that is, per:

    http://www.postfix.org/DEBUG_README.html#mail

    - Output of: postconf -nf
    - Output of: postconf -Mf
    - Logs showing what happened when you tested
    - A brief note stating what you expected instead

A quick glance over the thread, shows that:

    - You have wildcard rules, keep in mind that virtual expansion
      is recursive, and the RHS of each rule is subject to further
      lookups until either there's no match, or the result is the
      same as the input.

    - You don't qualify your virtual(5) RHS values with an explicit
      domain.  That's generally unwise.  They'll get implicitly
      qualified with $myorigin, and perhaps then match some other
      pattern in the PCRE table?

    - Your regular expressions are sloppy, missing "^" and "$"
      anchors, fail to escape literal "." as "\.", ... While
      it may be simple to write regular expressions that match
      a particular input pattern, it is more difficult to write
      *correct* regular expressions that match *only* the desired
      pattern and not also lots of other patterns that should not
      be matched.

    - You posted no logs that showed the unexpected results, with
      a brief explanation of what you expected instead.

--
    Viktor.

Reply | Threaded
Open this post in threaded view
|

Re: Forward email based on Regexps

Viktor Dukhovni
In reply to this post by Frank DiGennaro
> On Oct 7, 2020, at 5:24 PM, Frank DiGennaro <[hidden email]> wrote:
>
> I went back and made the changes Noel Jones suggested Like so.

But these are not what Noel suggested:

> /^[0-9]+@fax.mobilepbx.net/     asterisk
> /^[0-9]+@mobilepbx.net/         asterisk
>
> This now works. Thank you very, very much.

- The "." characters are NOT escaped, they should be "\." not just ".".
- The domain name is not anchored at the end, it should be "\.net$" not
  "\.net"

That is:

        /^[0-9]+@fax\.mobilepbx\.net$/     asterisk
        /^[0-9]+@mobilepbx\.net$/          asterisk

And, you generally should specify an explicit "@domain" qualifier for the RHS
"asterisk" result, though if you're sure that "$myorigin" will always continue
to be the correct domain suffix, this will work as written.

--
        Viktor.

Reply | Threaded
Open this post in threaded view
|

RE: Forward email based on Regexps

Frank DiGennaro
    Yet it will still match regardless, so what's your point? I'm sorry if
my question was "beneath" you all and according to Wietz I "wasted" your
time with my question but it's not obvious to anyone that \d is not
supported. Bottom line, fuck you and all of you, I'm sorry that I wasted all
of your time with such an elementary problem after I've provided ALL of the
required data to solve the problem.
FSD  

-----Original Message-----
From: Viktor Dukhovni [mailto:[hidden email]]
Sent: Wednesday, October 7, 2020 3:32 PM
To: Frank DiGennaro <[hidden email]>
Cc: Postfix users <[hidden email]>
Subject: Re: Forward email based on Regexps

> On Oct 7, 2020, at 5:24 PM, Frank DiGennaro <[hidden email]> wrote:
>
> I went back and made the changes Noel Jones suggested Like so.

But these are not what Noel suggested:

> /^[0-9]+@fax.mobilepbx.net/     asterisk
> /^[0-9]+@mobilepbx.net/         asterisk
>
> This now works. Thank you very, very much.

- The "." characters are NOT escaped, they should be "\." not just ".".
- The domain name is not anchored at the end, it should be "\.net$" not
  "\.net"

That is:

        /^[0-9]+@fax\.mobilepbx\.net$/     asterisk
        /^[0-9]+@mobilepbx\.net$/          asterisk

And, you generally should specify an explicit "@domain" qualifier for the
RHS "asterisk" result, though if you're sure that "$myorigin" will always
continue to be the correct domain suffix, this will work as written.

--
        Viktor.

Reply | Threaded
Open this post in threaded view
|

Re: Forward email based on Regexps

Viktor Dukhovni
On Wed, Oct 07, 2020 at 06:54:10PM -0400, Frank DiGennaro wrote:

> Yet it will still match regardless, so what's your point?

My point, and I am sorry if it is not welcome, is that a regular
expression has to not only match the things it SHOULD match, it *also*
needs to NOT match the things it SHOULD NOT match.  The regular
expressions you crafted fail the second requirement.  You're of course
more than free to not care, and even resent the helpful advice.

--
    Viktor.