diaspoir.net

安 大 衛

Some Blogs

  • WWdN: In Exile (Wil Wheaton)
  • Uncertain Principles
  • Crooked Timber
  • Official Google Blog
  • Wine Library TV
  • Slashdot
  • waider
  • waidesworld
  • Making Light
  • The Language Hat
  • Neil Gaiman
  • Whatever: John Scalzi
  • Charlie Stross's Diary
  • Ken MacLeod
  • Dave Langford's Ansible
  • Contrary Brin
  • Stephen Fry
  • Veronica Belmont
  • Mahalo
  • Merlin Mann
  • Kung Fu Grippe
  • Jonathan Coulton
  • David Gerrold
  • Orcinus
  • The Sideshow
  • Josh Marshall
  • Atrios (Eschaton)
  • Hullabulloo
  • 43 Folders
  • Wis[s]e Words: Ceci n'est pas un blog
  • Wil Harris
  • Boing Boing
  • Engadget
  • Gizmodo
  • LifeHacker
  • The Poor Man
  • Creative Commons

Environental Sites

  • Tree Hugger
  • Celsias
  • Real Climate

Apple News and blogs

  • Apple Hot News
  • Mac Rumors
  • Andy Ihnatko
  • TUAW
  • MacRumors
  • Apple Insider
  • Fake Steve Jobs
  • Erica Sadun iPhone developer
  • Mac OS X Hints
  • 9 to 5 Mac
  • MacWorld

Linux News

  • Linux Kernel News
  • Kernel Newbie
  • Fedora Project

Photography News and blogs

  • DPReview
  • Photo.net
  • This Week in Photography
  • TWiP Blog
  • Strobist (flash photography)
  • Stuck In Customs (HDR)
  • Pentax
  • Pentax Support
  • The Gimp
  • Photomatix
  • PhotoFocus

News and Politics

  • Google News
  • BBC News
  • South China Morning Post
  • HK Standard
  • UK Guardian
  • Your Yahoo!
  • WhiteHouse.org
  • Christian Science Monitor
  • The Onion (Even better than the real thing!)

Podcasts

  • TWiT.tv
  • MacBreak Weekly
  • This Week in Tech
  • FLOSS Weekly
  • Pixel Corps
  • Podango
  • TekZilla
  • Command-N

Web Comics

  • User Friendly by Illiad
  • Sheldon by Dave Kellett
  • Dilbert by Scott Adams
  • Doonesbury by Trudeau
  • XKCD by Randall Munroe
  • The Joy of Tech comic by nitrozac
  • PVP Online by Scott Kurtz
  • Real Life by Greg Dean
  • Questionable Content
  • Mega Tokyo
  • WonderMark by David Maliki
  • Girl Genius
  • Penny Arcade by Gabe and Tycho
  • NASA astronomy picture of the day
  • The World of Lily Wong
  • Hi Jinks Ensue
  • Three Panel Soul
  • Girls With Slingshots

The Problem:

I want to filter incoming email before passing it on to the Exchange server. Commercial solutions are expensive and not very flexible. A procmail based approach seemed like a good idea as I'm pretty familiar with procmail.

The Solution:

The solution is basically as follows:

  1. Configure sendmail to pass everything to procmail
  2. set up procmail filters to check every mail
  3. suspect mails get stored and a notice is sent to the mail-admin who can review the spam
  4. real mails get sent through
  5. suspect mails can get sent on if the filters have trapped one by mistake.

Sounds simple, eh? Well then you're a sendmail/procmail guru and you're just reading this page to find mistakes. (If you do find one, email me with it.)

If it doesn't sound simple, then I'll go through my solution step by step. It's simple in principle, but the devil is always in the details.

Configuring Sendmail

Some Sendmail Related Links: Sendmail (3rd Edition) - Brian Costales, Eric Allman Linux Sendmail Administration - Craig Hunt The first thing to do is get sendmail. Go to sendmail.org and download the latest version and follow the instructions. Eventually, you may get it to work.

Or you could get the rpm from RedHat, if you're a RedHat type. I use the most recent version built from source rather than the RPM supplied with the distribution. I tend to do this with most mission critical software - makes me happier about staying ahead of exploits.

For the filtering, there is enough information on the web to let you know how to do this, but most of it tends to make the assumption that you know exactly what every line in the sendmail.cf file does. Most of the methods I've seen just show you the ruleset extension below (in the sendmail.mc) file) and don't tell you what it's doing. It's important to know exactly what's going on, or you'll get mail loops.

The next step is to get sendmail to rewrite all incoming addresses and send them to a specific mail, then change them back before sending them on normally. To do this, you need to edit the sendmail.cf file. Well, actually you don't, but you need to specify a little rule in the .cf format which is bad enough. Your sendmail.mc should look like this:

divert(-1)

dnl This is the macro config file used to generate the /etc/sendmail.cf

dnl file. If you modify this file you will have to regenerate the

dnl /etc/sendmail.cf by running this macro config through the m4

dnl preprocessor:

dnl

dnl m4 /etc/sendmail.mc > /etc/sendmail.cf

dnl

dnl You will need to have the sendmail-cf package installed for this to

dnl work.

include(`/usr/lib/sendmail-cf/m4/cf.m4')

define(`confDEF_USER_ID',``8:12'')

OSTYPE(`linux')

define(`confPRIVACY_FLAGS',`noexpn, novrfy')

define(`ALIAS_FILE',`/etc/aliases')

define(`confFORWARD_PATH',`')

define(`confLOG_LEVEL',`14')

define(`MAIL_HUB',`mail.foo.com')

define(`SMART_HOST',`mail.foo.com')

FEATURE(`access_db')

FEATURE(`blacklist_recipients')

FEATURE(`virtusertable',`hash -o /etc/mail/virtusertable')dnl

MAILER(smtp)

MAILER(procmail)

LOCAL_CONFIG

CPprocmail

LOCAL_RULE_0

R$*<@foo.com.> $#procmail $@/etc/procmail/filter.rc $:$1@foo.com.procmail.

R$*<@foo.com> $#procmail $@/etc/procmail/filter.rc $:$1@foo.com.procmail.

R$*<@$*.procmail.> $1<@$2> Already filtered, map back

R$*<@$*.procmail> $1<@$2> Already filtered, map back

MAILER_DEFINITIONS

Mprocmail, P=/usr/bin/procmail, F=DFMmShun, S=11/31, R=21/31, T=DNS/RFC822/X-Unix,

           A=procmail -m $h $g $u

The lines you want to pay attention to there are the lines after LOCAL_RULE_0.

These lines rewrite any incoming address from foo.com to foo.com.procmail. (with a final period). They then specify that the mailer for these addresses is the local procmail program and the filter specified in /etc/procmail/filter.rc.

There should be [TAB] characters after the first term in each of the lines beginning with R$, i.e. the first (and only the first) space in the line should actually be a [TAB] character. This is probably the single most asked question on this entire topic.

when procmail is finished with the message, it gets sent back with a foo.com.procmail. address, and this lets sendmail know that the mail has already been sent through procmail and filtered. Sendmail then changes the address back and hands the message on to the relay for delivery.


Procmail setup

Your procmail filter.rc file should be in the /etc/procmail directory.

################################################################

#

# procmail rules to filter mail on a gateway

# This code © dave o'brien 2002

# Released under GPL.

#

################################################################

PATH="/usr/bin:$PATH:/usr/local/bin"

SHELL="/bin/sh"

LOGFILE="/var/log/procmail.log"

NL="

"

LOGABSTRACT=all

#VERBOSE=yes

LINEBUF=8192

:0 H

* 32000^0

* 1^1 .

{

LINEBUF="$="

}

#Spamassassin (we run the spamd daemon all the time)

:0fw

| spamc -a

#Forward to recipients

:0

! -oi -f "$@"

#

################################################################

Some Procmail Related Links: The Procmail Companion - Martin McCarthy What the above filter file is doing should be pretty obvious if you know procmail, but basically, it sets up some environment variables, sends the mail to an external filter (SpamAssassin, that's the spamc line) and forwards it back to sendmail after processing. The email will have the SpamAssassin header added to it if it's spam. Optionally, you can have it send all spam to one mailbox.

I recommend you look at the Sendmail code in the sendmail.mc above and understand exactly what's going on (try testing some email addresses using sendmail in test mode). Then go buy the Unix System Administrator's Handbook and read the chapter on electronic mail, closely followed by the O'Reilly Sendmail Book (3rd Edition - Brian Costales, Eric Allman). Then install sendmail from source. *Now* you know whats going on. Sendmail is complex, but study pays dividends.

This Section

  • Codered and Nimda Idiots
  • DNS Cache abusers
  • Portscanners
  • Proxy Spammers
  • Referral Spammers
  • Sendmail Permissions
  • Sendmail Configuration
  • SSH Logon Attempts

About Me

  • odaiwai on twitter
  • odaiwai on youtube
  • odaiwai on flickr

Contact

  • Unsolicited Bulk Email (spam), commercial solicitations, SEO related items, link exchange requests, and abuse are not welcome here and will result in complaints to your ISP.
  • Owing to stupid amounts of spam, you'll have to figure out an appropriate email address, but preferably don't try to contact me.
  • Any email to any address at this site may be made public at the sole discretion of the recipient.

Other Stuff

  • Powered by Linux
  • (Fedora Linux)

This page © 1996-2025 Dave O´Brien