Comments
Richard Davies wrote: The UK has a good crop of technology pioneers in cloud computing - for example ElasticHosts, FlexiScale, Flexiant, OnApp - and also some strong government initiatives such as G-Cloud. We will have to see whether this kind of technical leadership converts into swift mass-market adoption or not.
Cloud Expo on Google News

SYS-CON.TV
Cloud Expo & Virtualization 2009 East
PLATINUM SPONSORS:
IBM
Smarter Business Solutions Through Dynamic Infrastructure
IBM
Smarter Insights: How the CIO Becomes a Hero Again
Microsoft
Windows Azure
GOLD SPONSORS:
Appsense
Why VDI?
CA
Maximizing the Business Value of Virtualization in Enterprise and Cloud Computing Environments
ExactTarget
Messaging in the Cloud - Email, SMS and Voice
Freedom OSS
Stairway to the Cloud
Sun
Sun's Incubation Platform: Helping Startups Serve the Enterprise
POWER PANELS:
Cloud Computing & Enterprise IT: Cost & Operational Benefits
How and Why is a Flexible IT Infrastructure the Key To the Future?
Click For 2008 West
Event Webcasts
Fight spam with procmail
procmail is standard on most distros

Q: I use sendmail and a POP mail server on a Linux box. I am desperately looking for a way to filter incoming messages processed by sendmail based on their subject names and enclosure names. I would like to do this to filter spam and potentially dangerous viruses.

A: I'm not sure if you are talking about the MTA (Mail Transfer Agent, aka sendmail) or the MUA (Mail User Agent, aka Netscape Mail). However, there are solutions for both. Most graphical clients have built-in filtering that you can use.

On the server side, you can use a program called procmail. Procmail is installed by default on most Linux systems, and can be used system-wide or user by user. Procmail is a powerful program that uses recipes to define filtering mechanisms that result in certain actions.

For example, you can define a filter that states that any mail coming from the user bgates@microsoft.com is automatically redirected to /dev/null. Such a filter would look something like this:

:0
* ^From.*bgates@microsoft.com
{
:0
/dev/null
}

All procmail filters are kept in a procmailrc file. The procmailrc file is placed in /etc for global declarations, or in $HOME/.procmailrc for user declarations. $HOME is a variable for the home directory of the user. A typical procmailrc file looks like this:

#
#
# Begin /etc/procmailrc
#
#

ORGMAIL /var/spool/mail/$LOGNAME MAILDIR $HOME/ SENDMAIL /usr/sbin/sendmail

:0 * ^From.*bgates@microsoft.com { :0 /dev/null }

# # End /etc/procmailrc #

The ORGMAIL variable sets the global mail directory for the system -- in other words, the system mailbox. I have set ORGMAIL to be /var/spool/mail/$LOGNAME where $LOGNAME is the login name of the user.

MAILDIR is the current directory used when procmail is executing. I have declared that MAILDIR will be the / (root) of the user's home directory.

The SENDMAIL variable tells procmail where the sendmail MTA program is. In this case: /usr/sbin/sendmail.

As I mentioned, procmail is a very powerful program. Your recipes can be used to generate a slew of useful actions. What if we don't want to send all the email from bgates@microsoft.com to /dev/null? What if we want to keep it for an upcoming antitrust trial? To do this, we could use the following recipe instead:

:0
* ^From.*bgates@microsoft.com
{
:0
antitrust
}

This recipe will cause all email from bgates@microsoft.com to be saved to a file called antitrust. The file will be located in the area where the MAILDIR variable is set. To make the recipe a little more useful, we could set the file to be saved in a directory that is below the MAILDIR variable directory. For example:

:0
* ^From.*bgates@microsoft.com
{
:0
mail/antitrust
}

This recipe will cause the antitrust file to be used within the $HOME/mail directory. You may want to set this as global so you don't have a bunch of mail files within the / (root) of your home directory.

You can write recipes for procmail to support multiple conditions. Let's take the following:

:0
* ^From.*bgates@microsoft.com
* ^Subject:.*competition
{
:0
mail/antitrust
}

As before, we are using the bgates recipe. This time, if we receive email from bgates@microsoft.com that contains the subject "competition," the recipe will take action and move the email to the mail/antitrust location.

You can initiate multiple actions within a recipe by doing the following:

:0
* ^From.*bgates@microsoft.com
* ^Subject:.*competition
{
:0 c

! justicedept@us.gov

:0 mail/antitrust

}

The recipe now will forward all email from bgates@microsoft.com with the subject "competition" to justicedept@us.gov, and move the email message to mail/antitrust.

Using multiple recipes within procmailrc can be accomplished by doing the following:

:0
* ^From.*bgates@microsoft.com
* ^Subject:.*competition
{
:0 c

! justicedept@us.gov

:0 mail/antitrust

}

:0 * ^From.*sexcity {

:0 /dev/null

}

The first recipe is the one we used for the earlier examples. The second will take any email from sexcity and dump it to /dev/null. You may have noticed that I did not put a domain on the end of sexcity. If you don't specify a domain in the recipe, it will grab all email from "sexcity" that comes to your box, regardless of domain. You should be careful with this type of recipe if you host multiple domain names on your machine.

As you can see from the above examples, procmail is not difficult, but you'll want to be careful. If you make a mistake in your procmail configuration, you can blow away your entire email configuration. Test each recipe and watch what happens with your email. Once you get it locked down, however, it works great.

One last point I need to make is that you must configure sendmail to understand that procmail exists, and to accept procmail as a mailer for the sendmail daemon. Configuring sendmail to use procmail isn't hard, but it's more involved than what the scope of this article will allow. Consult the procmail man pages and the sendmail FAQ for more information. As a side note, sendmail itself provides facilities to aid spam reduction, as well.

That's it for this week's Ask the Geek.

About Joshua Drake
Joshua Drake is the co-founder of Command Prompt, Inc., a PostgreSQL and Linux custom development company. He is also the current author of the Linux Networking HOWTO, Linux PPP HOWTO, and Linux Consultants HOWTO. His most demanding project at this time is a new PostgreSQL book for O'Reilly, 'Practical PostgreSQL'

In order to post a comment you need to be registered and logged in.

Register | Sign-in

Reader Feedback: Page 1 of 1

One word: http://www.spambouncer.org

A complete, freeware, and highly effective procmail spam filter system, upated constantly. Added to my own filters which target specific items (such as known spammers' Millions CD addresses) rather than general ones, I get an average of less than 1 spam every three days that leaks through the filters. Considering all my addresses, including all the "wildcard" or "catchall" addresses at various virtual domains that I own or administer, many of which receive over 750 spam attempts per day, that's quite a reduction.


Your Feedback
Ken Luke wrote: One word: http://www.spambouncer.org A complete, freeware, and highly effective procmail spam filter system, upated constantly. Added to my own filters which target specific items (such as known spammers' Millions CD addresses) rather than general ones, I get an average of less than 1 spam every three days that leaks through the filters. Considering all my addresses, including all the "wildcard" or "catchall" addresses at various virtual domains that I own or administer, many of which receive over 750 spam attempts per day, that's quite a reduction.
Latest Cloud Developer Stories
Swisscom, the Swiss telecom, is going into the cloud business. Its subsidiary Swisscom IT Services AG has signed up with Red Hat as a Certified Cloud Provider and launched a public cloud Infrastructure-as-a-Service (IaaS) cloud targeting enterprise-class customers primarily in ...
Apache Deltacloud, the Red Hat-contributed ReSTful API that abstracts differences between clouds so services on any cloud can be managed – provided of course there’s a driver – has graduated from the Apache Foundation’s incubator and is now a full-fledged Top-Level Project (TLP)....
In a surprise move on Tuesday, January 10, Oracle wheeled out its Big Data Appliance. That’s the one it said in October would be ready sometime in the first half. Only nobody believed it meant early in the first half. Heck, it’s not even clear anybody thought Oracle could make ...
Rackspace Hosting, the service leader in cloud computing, on Thursday announced its acquisition of SharePoint911, an industry leader in SharePoint consulting, training, and "JumpStart" services within SharePoint. The unification of both companies provides capabilities to deliver ...
CloudLinux, Inc., on Thursday released CafeFS 3, a virtualized file system for shared hosters that cages each customer within its own virtualized file system. CageFS becomes part of CloudLinux OS at no additional charge. CloudLinux OS, the only commercially-supported Linux OS m...
Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
Click to Add our RSS Feeds to the Service of Your Choice:
Google Reader or Homepage Add to My Yahoo! Subscribe with Bloglines Subscribe in NewsGator Online
myFeedster Add to My AOL Subscribe in Rojo Add 'Hugg' to Newsburst from CNET News.com Kinja Digest View Additional SYS-CON Feeds
Publish Your Article! Please send it to editorial(at)sys-con.com!

Advertise on this site! Contact advertising(at)sys-con.com! 201 802-3021

SYS-CON Featured Whitepapers
ADS BY GOOGLE

Breaking Cloud Computing News

NASHVILLE, Tenn., Feb. 16, 2012 /PRNewswire/ -- Brookdale Senior Living Inc. (NYSE: BKD) (the "Co...