LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Server (https://www.linuxquestions.org/questions/linux-server-73/)
-   -   postfix/dovecot virtual users - email rules per mailbox? (https://www.linuxquestions.org/questions/linux-server-73/postfix-dovecot-virtual-users-email-rules-per-mailbox-4175556750/)

Red Squirrel 10-21-2015 01:43 AM

postfix/dovecot virtual users - email rules per mailbox?
 
I am trying to find info on how to setup email rules on a per mailbox level. For example I want to filter through spamassassin, then if the email is spam move to the spam folder. I've done this before with procmail and regular unix users and each user has it's own .procmailrc, but how do I do it with virtual users? I can't seem to find much online on how to do it, only on how to do it globally.

I tried to specify a recipe file when calling procmail in the transport table file but it does not work, it probably does not like that there's a space, and quotes don't seem to work either.

It does not have to be with procmail, either, I just want to be able to decide on a per mailbox basis what the email goes through, such as spam assassin, or calling an external program, or being forwarded etc....

Basically I'm moving my local mail server to a new setup, so I'm reconfiguring everything.

I'm also willing to write my own program if I have to, I just need guidance on the basics of where I'd start, like how the email gets piped through etc.

MensaWater 10-22-2015 02:29 PM

You can setup a .forward in the home of each user that has:
"|exec /usr/bin/procmail"

That tells it to "forward" the mail into a pipe into procmail.

You can also create .procmailrc in the home of each user that has the recipe you want for that user and procmail will use that.

descendant_command 10-22-2015 02:45 PM

I find using dovecot-lda to do delivery and sieve for filtering to be a very good solution for virtual users.

Red Squirrel 10-22-2015 03:11 PM

There are no home directories, this is virtual users not unix users. Never heard of dovecot-lda, I'll google that further.

MensaWater 10-23-2015 10:09 AM

Quote:

Originally Posted by descendant_command (Post 5438681)
I find using dovecot-lda to do delivery and sieve for filtering to be a very good solution for virtual users.

Interesting. Found link at http://wiki.dovecot.org/LDA that seems to give a fair amount of information.

Also found nice information about virtual user setups at the HOWTO link on that page.

I'm in pre-planning stage to setup a full mail server and this information should come in handy.

Red Squirrel 10-23-2015 03:22 PM

Actually what IS dovecot-lda anyway? I'm reading through all that but just trying to figure out where what I want to (spam filtering and rules on a per mailbox basis) do actually comes in.

descendant_command 10-23-2015 04:31 PM

Quote:

Originally Posted by Red Squirrel (Post 5439214)
Actually what IS dovecot-lda anyway?

Quote:

Originally Posted by http://wiki.dovecot.org/LDA
The Dovecot LDA, called deliver, is a local delivery agent which takes mail from an MTA and delivers it to a user's mailbox

Quote:

I'm reading through all that but just trying to figure out where what I want to (spam filtering and rules on a per mailbox basis) do actually comes in.
Quote:

Originally Posted by http://wiki.dovecot.org/LDA/Sieve
The Dovecot Sieve plugin provides mail filtering facilities at time of final message delivery using the Sieve (RFC 5228) language

My dovecot already has/needs all the user account details (name,pw,home) for POP/IMAP, and also provides SMTP AUTH service to postfix, so it makes sense for it to do delivery too, and filter it with the sieve plugin in the process.

Red Squirrel 10-23-2015 05:01 PM

That does not really tell me much though, isnt that what postfix already does? Right now my setup works, the mail gets delivered to the mailboxes etc. But what I need to figure out how to do is how to use procmail or similar program, but on a per mailbox basis. For example I may want some mailboxes to simply have spam filtering, while I want some to do something special such as pipe the mail through a special program to do something with it, or forward etc. The only thing I can find is how to make a global procmail. I basically want a procmailrc file for each mailbox, but if there is none then it just skips procmail altogether. Or is "The Dovecot Sieve plugin provides mail filtering facilities at time of final message delivery using the Sieve (RFC 5228) language" just a fancy way of saying that? Since I'm not really sure what that is actually saying.

I guess what I COULD do is use a global procmail to pipe to a custom program that then has rule sets to do stuff, but if I can avoid needing to write my own program I want to, but I suppose it is an option. Basically dovecot is not even in the picture until a user needs to check their mail.

descendant_command 10-23-2015 11:26 PM

Quote:

Originally Posted by Red Squirrel (Post 5439274)
That does not really tell me much though, isnt that what postfix already does?

To some extent, yes - you would be using dovecot's 'deliver' rather than, say, maildrop or procmail - the point being you are using a tool that already has access to all the user details required and was designed with virtual setups in mind - my recollection of maildrop and procmail is that they were originally designed for serving local users primarily, with some features that won't work with virtual setups.
Quote:

Right now my setup works, the mail gets delivered to the mailboxes etc. But what I need to figure out how to do is how to use procmail or similar program, but on a per mailbox basis. For example I may want some mailboxes to simply have spam filtering, while I want some to do something special such as pipe the mail through a special program to do something with it, or forward etc. The only thing I can find is how to make a global procmail. I basically want a procmailrc file for each mailbox, but if there is none then it just skips procmail altogether. Or is "The Dovecot Sieve plugin provides mail filtering facilities at time of final message delivery using the Sieve (RFC 5228) language" just a fancy way of saying that? Since I'm not really sure what that is actually saying.

I guess what I COULD do is use a global procmail to pipe to a custom program that then has rule sets to do stuff, but if I can avoid needing to write my own program I want to, but I suppose it is an option. Basically dovecot is not even in the picture until a user needs to check their mail.
OK.
My setups are usually postfix with amavisd doing global AV/anti-spam to either tag or quarantine (you seem to want to do this AFTER delivery?).
Postfix then hands off to dovecot for local (virtual) delivery.
Sieve scripts can be global or per user, or both, and the user can manage them via the managesieve plugin (I usually use a Tbird extension and/or a Roundcube plugin) for personalised spam handling, conditional forwarding, sub-folder filing, vacation messages etc.

I recall looking at both maildrop & procmail some years ago and had troubles getting them to play nice with virtual users (may have been just me, as I was a bit new to it all then), and ended up with the above setup which works well for me.

Red Squirrel 10-24-2015 12:11 AM

Yeah I want it to run before delivery, but for incoming mail, not outgoing. The way my server works right now is fetchmail goes online and retrieves mail then delivers it to local imap mailboxes but before it gets put in the mailbox it goes through procmail, but my current (production, not one I'm testing now) setup is unix users so I just have a procmail file in each home directory. I want to get away from this though as it's kinda dirty to do it this way. I just want to do way with the unix accounts and do it with virtual users.

Though I'm starting to wonder if it would be easier to just use a global procmail, but pipe it through a custom app that does exactly what I code it to do. The neat thing with procmail is how you can pipe entire emails through a program, then that program can do various things to/with it. For example I have this one email address that when you send a specially formatted email to it, it parses it and posts the info on one of my sites and I just have to go in and validate it. I also have some local only emails that I don't want to bother running through spam filtering.

Red Squirrel 10-31-2015 05:24 AM

So I'm trying to get dovecot-lda to work but I'm not finding enough details on the config I have to do and what each line means.

In the postfix main.cf I changed virtual_transport to dovecot, and I also added the line in master.cf:

Code:

dovecot  unix  -      n      n      -      -      pipe
  flags=DRhu user=vmail:vmail argv=/usr/libexec/dovecot/dovecot-lda -f ${sender} -d ${recipient}

In the tutorial it said /usr/local but that path was invalid... not sure if this could be a problem.


I restarted postfix and dovecot too for good measure. Now mail does not get sent, I keep getting permission related errors for this file:

/var/run/dovecot/auth-userdb


And this is where I'm confused, what is that userdb for? Arn't the users in /etc/dovecot/passwd? But things get even weirder, I had not noticed it till now but in the /etc/dovecot/dovecont.conf file it has this:


Code:

userdb {
    driver = static
    args = uid=1999 gid=1999 home=/var/vmail/d/n allow_all_users=yes
}

What is that suppose to be for? I don't have a vmail folder in /var either. Am I suppose to change that to where my Maildir is stored? What is this userdb for? There is already a passwd setting for the mail users, and a passwd file for those. It has both the user names and passwords in the file.


Also if I try to send mail from within squirrelmail, nothing happens. No log entry no nothing. But if I use thunderbird or the linux mail command then I get deferred errors because of that mysterious auth-userdb file.

Red Squirrel 11-03-2015 11:24 PM

Why is it so hard to find information on doing this? Is it really THAT unusual of a setup to want to have mail rules on a per mailbox basis in a virtual user environment? I've been trying to google this for days now and absolutely not getting anywhere. I'm at the point where I'm thinking of just writing my own mail server... the protocol is rather simple anyway. I just rather use something that's already made if I can as if I write my own there are security implications if I overlook stuff.

I tried to use the mailbox_commands route, but it seems that does not get executed? I even set mailbox_command to /dev/null figuring mail would just get lost, or I would get an error in the logs, but mail still gets delivered. I was thinking that if I can figure out the syntax to that I can have it so there is a procmail file in each mailbox that gets executed. (mailbox, not user... there are no users, it's virtual users).

There's got to be a way, that is properly documented, on how to do this? Is it really considered that unusual of a setup?

descendant_command 11-04-2015 01:06 AM

Sieve.

Check that the sieve plugin is enabled in your dovecot.conf (might be already depending on the packagers chosen defaults).

Then just put a dovecot.sieve script in the target home dir containing what you want.

descendant_command 11-04-2015 01:20 AM

Missed your earlier post.
Quote:

Originally Posted by Red Squirrel (Post 5442732)
So I'm trying to get dovecot-lda to work but I'm not finding enough details on the config I have to do and what each line means.

In the postfix main.cf I changed virtual_transport to dovecot, and I also added the line in master.cf:

Code:

dovecot  unix  -      n      n      -      -      pipe
  flags=DRhu user=vmail:vmail argv=/usr/libexec/dovecot/dovecot-lda -f ${sender} -d ${recipient}

In the tutorial it said /usr/local but that path was invalid... not sure if this could be a problem.


I restarted postfix and dovecot too for good measure. Now mail does not get sent, I keep getting permission related errors for this file:

/var/run/dovecot/auth-userdb


And this is where I'm confused, what is that userdb for? Arn't the users in /etc/dovecot/passwd? But things get even weirder, I had not noticed it till now but in the /etc/dovecot/dovecont.conf file it has this:


Code:

userdb {
    driver = static
    args = uid=1999 gid=1999 home=/var/vmail/d/n allow_all_users=yes
}

What is that suppose to be for? I don't have a vmail folder in /var either. Am I suppose to change that to where my Maildir is stored? What is this userdb for? There is already a passwd setting for the mail users, and a passwd file for those. It has both the user names and passwords in the file.

Yes, this tells dovecot where ti store the mails (i.e. specifies 'home' dirs for your virtual users.
You can use the same passwd file as both a passdb (to validate user/pass combos) and a userdb (to privide the user/home mapping).
See http://wiki.dovecot.org/AuthDatabase/PasswdFile
and http://wiki.dovecot.org/VirtualUsers
Quote:

Also if Itry to send mail from within squirrelmail, nothing happens. No log entry no nothing. But if I use thunderbird or the linux mail command then I get deferred errors because of that mysterious auth-userdb file.
Check the squirrelmail logs - if you're getting nothing in your mail logs that means the squirrel processes are not even reaching it.

Red Squirrel 11-04-2015 01:28 AM

Quote:

Originally Posted by descendant_command (Post 5444391)
Sieve.

Check that the sieve plugin is enabled in your dovecot.conf (might be already depending on the packagers chosen defaults).

Then just put a dovecot.sieve script in the target home dir containing what you want.

I can't get even the basic non sieve config to work, as soon as I set the virtual_transport to dovecot, things start to act real strange such as that userdb error. I'm confused as to what that is even for, because all that information is already in passdb and the mailbox information is already in the postfix vmailbox etc...

So what exactly do I set this userdb to?

My virtual mailbox is in format:

/localdata/mail/vmail/domain/user

That is already set in mail_location

The user/pass file is in /etc/dovecot/passwd, that is already set in the passwd { } portion.

Sow hat do I set userdb { } to? The same thing as passwd?

I have also yet to find any concise examples of how to make rules with sieve. I also read somewhere that it does not support piping mail through a program then getting it back (ex: sending to spamassassin or a custom app and getting it back). Is that really the case?

I did fix the squirrelmail issue, turns out it was still sending mail to my current live mail server.


I may be getting somewhere with the mailbox_command though, I have not tried the mailbox_command_maps yet. Only thing, what user does that command run as? I made a basic procmail recipe file for a test to just put mail in a folder but I get errors, I think it may be permission related.

Also what is /var/run/dovecot/auth-userdb? it keeps wanting me to add permissions to it. I get errors in the log when I try to send mail. But now I'm at a point where a lot of my mailboxes are saying they are unknown. This is a huge mess. Is there a site that actually tells me what the entire config of both postfix and dovecot is suppose to be instead of just little bits here and there?

ex error:
Code:

Nov  4 02:41:55 rohan dovecot: lda: Error: userdb lookup: connect(/var/run/dovecot/auth-userdb) failed: Permission denied (euid=1999(vmail) egid=1999(vmail) missing +r perm: /var/run/dovecot/auth-userdb, euid is not dir owner)


All times are GMT -5. The time now is 02:58 AM.