[SOLVED] Writing Regex for Virtual Domains

kransom

New Member
Aug 22, 2023
22
1
3
Hello,

Is it possible for postfix to recognize regex inside a virtual alias file? I wrote some regex in /etc/postfix/virtual, but it does not seem to be read.
Here is one example of my regex:
^contact\+.*\@example\.com$ admin@example.com
This should catch any addresses such as contact+help-desk@example.com and send it to admin@example.com but it does not work.

I run these commands below after each edit but still no luck.
postmap /etc/postfix/virtual
systemctl restart postfix
 
You need the postfix-pcre package installed.
Also, specify the lookup in main.cf (note the pcre prefix):
virtual_alias_maps = pcre:/etc/postfix/virtual
On PMG this should be done in the template /etc/pmg/templates/main.cf.in.

Whilst I have done this in postfix, I haven't as yet tried it in PMG. virtual_alias_maps appears to be undefined in my PMG.
Bash:
# postconf -x | grep virtual_alias_maps
virtual_alias_maps =

BTW
it is not necessary to postmap pcre lookup files.
A systemctl reload postfix should be sufficient.
You can chain different lookup types in virtual_alias_maps. For example
virtual_alias_maps = ldap:/etc/postfix/ldap-group-members.cf ldap:/etc/postfix/ldap-users.cf pcre:/etc/postfix/virtual_alias.pcre

But, I am not sure a mail gateway is the appropriate place to rewrite recipient addresses?
 
Last edited:
@keeka
I actually got this to work by
#1 updating my regex inside of /etc/postfix/virtual to
/^contact\+.*\@example\.com$/ admin@example.com

use this to test any regex:
postmap -q "string" regexp:/etc/postfix/virtual

#2 updating the path inside /etc/postfix/main.cf to
virtual_alias_maps = regexp:/etc/postfix/virtual
virtual_alias_domains = example.com

refresh PMG:
postmap /etc/postfix/virtual
systemctl restart postfix

I originally tried my configuration in /etc/pmg/templates/main.cf.in but it was returning a relay access denied error. I don't think my setup of PMG was reading anything from the template files, so I resorted to using the postfix directory. If you happen to know what causes that, it would be helpful to know myself. Thank you for the reply.
 
Last edited:
I originally tried my configuration in /etc/pmg/templates/main.cf.in but it was returning a relay access denied error. I don't think my setup of PMG was reading anything from the template files, so I resorted to using the postfix directory. If you happen to know what causes that, it would be helpful to know myself.
Glad you have it working. However postfix customisation needs to be done in the copied template. Copy the entire main.cf.in template from /var/lib/pmg/templates/ to /etc/pmg/templates/, add your config and then apply the new config with pmgconfig sync --restart 1
Otherwise your customisation will get lost next time pmg regenerates config files from the template system.
EDIT: oh I see you used regexp rather than pcre, so no needto install additional postfix packages. I like the pcre regex library.
 
Last edited:
Glad you have it working. However postfix customisation needs to be done in the copied template. Copy the entire main.cf.in template from /var/lib/pmg/templates/ to /etc/pmg/templates/, add your config and then apply the new config with pmgconfig sync --restart 1
Otherwise your customisation will get lost next time pmg regenerates config files from the template system.
EDIT: oh I see you used regexp rather than pcre, so no needto install additional postfix packages. I like the pcre regex library.
I am hesitant to cp over that file as I've made many changes in the UI. Do the UI changes show up in the template file?
Will running pmgconfig sync overwrite my file as well? I am running my production machine for a couple domains and have made quite a few adjustments through the console and UI do not want to lose them.
 
Configuration changes made in the GUI are applied to the relevant services's config files (e.g. /etc/postfix/main.cf) by pmgconfig.
This means, if you edit directly any of the generated files (such as /etc/postfix/main.cf), and subsequently make changes via the GUI, your changes will be overwritten.
Therefore you have to work with the templating system, for those services that are managed by it. Not all files in /etc/ are managed by PMG. But for those that are, there may be an asociated template file(s). This you need to copy and customise. Postfix's main.cf is an example of this.
For main.cf.in, copy the distribution template to /etc/pmg/templates/. Make your changes, run pmgconfig sync --restart 1.
The virtual alias file you create in /etc/postfix/ is not managed by PMG, so pmgconfig won't touch that.
 
Last edited:
@keeka thank you. I'll take a look at that.
I guess last question, if anyone knows, but what are all the files that PMG over writes if not edited in a template?
For instance, I have custom rules for spamassassin in /etc/mail/spamassassin/custom.cf with that also get over wrote?

Soon I am going to upgrade PMG from ver 7.3 to ver 8.0 and want to make sure nothing gets lost.
I appreciate the replies. Thanks.
 
I believe some /etc/ files, that are potentially overwritten by pmg, have something like 'auto-generated' somewhere near the top. I don't know of a definitive list.
PMG doesn't overwrite spamassassin's custom.cf. Nor does it touch any supplemental files you might have created by way of customising postfix e.g. your virtual aliases file above.
The templates themselves (/var/lib/pmg/templates) shed light on some of the config files that are managed.
 

About

The Proxmox community has been around for many years and offers help and support for Proxmox VE, Proxmox Backup Server, and Proxmox Mail Gateway.
We think our community is one of the best thanks to people like you!

Get your subscription!

The Proxmox team works very hard to make sure you are running the best software and getting stable updates and security enhancements, as well as quick enterprise support. Tens of thousands of happy customers have a Proxmox subscription. Get yours easily in our online shop.

Buy now!