LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 03-02-2011, 12:52 PM   #1
riderryuken
LQ Newbie
 
Registered: Jul 2009
Posts: 16

Rep: Reputation: 0
Qmail subject filtering


Right now I have a script set up to filter out certain words from our email subject lines for any email received in the office. I want to know if it's possible to change it so that only external emails are checked and anything internal is not. Here is the script called checksucj.sh which I believe is a modified copy:


#!/bin/bash

SUBJECTLINE=`/usr/local/bin/822field Subject`
ERC=""

printsubj () {
# printf "I'm afraid I wasn't able to deliver your message with the following subject.\n"
# printf "This is a permanent error; I've given up. Sorry it didn't work out.\n"
printf "Subject:$SUBJECTLINE\n"
printf "Sorry, invalid subject line. (#$ERC)\n"
}

i="1" ; # Pharmacy
msg=`echo $SUBJECTLINE | egrep "[Pp]+ *[Hh]+ *[@Aa]+ *[Rr]+ *[Mm]+ *[@Aa]+ *[Cc]+ *[Yy]+"`
msg=`echo $msg | egrep -v "^[Pp]harmacy[., ]| [Pp]harmacy[., ]| [Pp]harmacy$"`
msg=`echo $msg | egrep -v "^[Pp]harmacies[., ]| [Pp]harmacies[., ]| [Pp]harmacies$"`
if [ -n "$msg" ]; then ERC=$ERC"$i" ; fi

and so on with words we don't want until:

if [ -n "$ERC" ]
then
printsubj $SUBJECTLINE
exit 100
fi

exit 0

Then in each user account under home/user this is called in the .qmail file:

|/var/qmail/bin/checksubj.sh
 
Old 03-02-2011, 03:51 PM   #2
roreilly
Member
 
Registered: Aug 2006
Location: Canada
Distribution: Debian, Slackware
Posts: 106

Rep: Reputation: 28
I would recommend procmail for this. Procmail is a very powerful tool for filtering mail.
 
Old 03-03-2011, 10:53 AM   #3
riderryuken
LQ Newbie
 
Registered: Jul 2009
Posts: 16

Original Poster
Rep: Reputation: 0
I'll do a search myself, but any good documentation you would recommend to set this up? Thanks for your help.
 
Old 03-03-2011, 03:08 PM   #4
riderryuken
LQ Newbie
 
Registered: Jul 2009
Posts: 16

Original Poster
Rep: Reputation: 0
Also is there no possible way to do this without installing something else? I don't want to risk messing something up if I don't have to.
 
Old 03-04-2011, 01:11 PM   #5
roreilly
Member
 
Registered: Aug 2006
Location: Canada
Distribution: Debian, Slackware
Posts: 106

Rep: Reputation: 28
You would need to install procmail at the very least.

Review this document before proceeding, and it will give you a good idea of what you
will need to do: http://www.lifewithqmail.org/lwq.html#procmail
 
Old 03-08-2011, 10:22 AM   #6
riderryuken
LQ Newbie
 
Registered: Jul 2009
Posts: 16

Original Poster
Rep: Reputation: 0
Alright I was hoping for an easier way by just altering some of the command line, but I'll try this and see if I can figure it out unless someone else has another way.
 
Old 03-09-2011, 08:02 AM   #7
roreilly
Member
 
Registered: Aug 2006
Location: Canada
Distribution: Debian, Slackware
Posts: 106

Rep: Reputation: 28
Procmail should be easy to install via your package manager.

Then instead of |/var/qmail/bin/checksubj.sh, you would use: | /usr/bin/procmail -m /home/vpopmail/domains/mydomain.com/username/.procmailrc

(You can also set a global procmailrc for this, and have custom procmailrc's for specific accounts.)

Here is my procmailrc. This is a very small subset of procmail's capabilities, and procmail wizards will no doubt be able to show you much more effective ways to do things, but this works.
Lastly, do you run SpamAssassin on this machine? Take a look at qmailrocks.org, which is a bit outdated, but will give you a step by step way to integrate SpamAssassin into your system.
You could always build a mail server in a VM to test before trying this in your production system of course.

You could use something like:
:0:
* ^FROM: *@mydomain.com
$MAILDIR/new

followed by your processing rules. That should allow anything from your domain to pass through, then filter if it doesn't meet that condition.



# .procmailrc
PATH=/usr/bin:/usr/local/bin
MAILDIR=/home/vpopmail/domains/mydomain.com/myusername/Maildir
LOGFILE=/home/vpopmail/domains/mydomain.com/myusername/procmail.logfile
VERBOSE=off

## move cvs commit messages to /dev/null

:0:
* ^Subject:.MYDOMAIN-CVS-COMMIT*
/dev/null

## delete svn commit messages:
:0:
* ^Subject:.SVN-COMMIT*
/dev/null

# delete messages from cruisecontrol

:0:
* ^FROM: cruisecontrol@mydomain.com
/dev/null

:0:
* ^Subject:.mydomainportal Build Failed*
/dev/null

:0:
* ^Subject:.*replica*
/dev/null

:0:
* ^Subject:.*SansCash*
/dev/null

# this person wouldn't remove me from her company mailing list,
# so now I just delete everything that comes in:

:0:
* ^FROM: caitlin@compuzonic.com
/dev/null

# this solves backscatter:

:0:
*^Subject:failure notice*
/dev/null

# this recipe moves all spam that needs to be learned
# by spamassassin back into the spam monitoring folder
#

:0
* ^Subject:.*SPAM
{
foldername=SPAM

:0c
.$foldername/ # stores in .$foldername/new/

:0
* LASTFOLDER ?? /\/[^/]+$
{ tail=$MATCH }

TRAP="mv $LASTFOLDER .$foldername/cur/$tail:2,S"

HOST
}


:0:
*

#lastly, place all remaining email in my new mail folder:

$MAILDIR/new

Last edited by roreilly; 03-09-2011 at 06:18 PM. Reason: more info
 
Old 03-10-2011, 10:45 AM   #8
riderryuken
LQ Newbie
 
Registered: Jul 2009
Posts: 16

Original Poster
Rep: Reputation: 0
Alright procmail and spamassassin are installed. I'm confused about this line though

/usr/bin/procmail -m /home/vpopmail/domains/mydomain.com/username/.procmailrc

I don't have any vpopmail folder and I'm not sure what -m does.

Also when I list the processing rules can I keep them how they were in checksubj?
 
Old 03-11-2011, 08:13 AM   #9
roreilly
Member
 
Registered: Aug 2006
Location: Canada
Distribution: Debian, Slackware
Posts: 106

Rep: Reputation: 28
Re: procmail -m:

From procmail manpage:

-m Turns procmail into a general purpose mail filter. In this mode
one rcfile must be specified on the command line. After the
rcfile, procmail will accept an unlimited number of arguments. If
the rcfile is an absolute path starting with /etc/procmailrcs/
without backward references (i.e. the parent directory cannot be
mentioned) procmail will, only if no security violations are
found, take on the identity of the owner of the rcfile (or sym-
bolic link). For some advanced usage of this option you should
look in the EXAMPLES section below..SH ARGUMENTS Any arguments
containing an '=' are considered to be environment variable
assignments, they will all be evaluated after the default values
have been assigned and before the first rcfile is opened.


the vpopmail is the path to the user accounts on that particular mailserver. the .procmailrc was my procmail recipe for handling mail.
This allowed custom recipes for each user. I had a very simple rule: if you wanted a procmail recipe, you had to write it yourself, and I would
install it on the mail server. Otherwise, you had to use the filters that were readily available.

So if your mailboxes are in /home/$USERNAME/Maildir, then you would use /home/$USERNAME/.procmailrc

When processing rules, you will have to use procmail syntax. Your checksubj is quite effective, but a well configured spamassassin
would do the same thing with less work. I would recommend using spamassassin with the latest rules and fuzzyocr. That will take care
of most of your spam. Then use the procmail recipe to fine tune and filter out any remaining items.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Filtering with Qmail(qmr situation) ... plz help. AchoMashinata Linux - Server 0 12-14-2006 06:21 AM
Need content filtering... (sensitive subject) fannymites Linux - Security 5 03-03-2006 04:49 PM
Procmail Filtering Using a Full Subject Line lmcilwain Linux - Software 3 02-08-2006 01:07 PM
Spam Filtering in Qmail Joey.Dale Linux - Software 5 12-05-2004 01:29 PM
filtering spam in Qmail? IceNineJon Linux - Software 2 07-05-2003 02:35 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration