procmail
[Top] [All Lists]

Re: running commands from procmail

1998-08-16 09:13:47
On Thu, 13 Aug 98 23:41:11 -0400, Jerry Shenk <jas(_at_)dect(_dot_)com> wrote:
I'd think we could have a rule that would allow a line in a message cause a 
remote host to be pinged and have the response e-mailed back.  We already 
have a number of processes that monitor remote sites and send pages if they 
fail.  All of the techs at D&E CNS have PCS phones which allow us to send an 
SMTP message from the road.  If I get a page, I'd like to be able to do a 
little research while driving.

All of what you're asking about is fairly trivial to do with Procmail.
The thing to watch out for, of course, is security risks.

Here's a bare-bones ping responder:

    :0  # DON'T USE THIS VERBATIM. See improved regex below.
    *   ^Subject:[      ]*ping[  ]*\/[^         "]*
    * ! ^X-Loop: ping(_at_)dect\(_dot_)com
    | ( formail -rt ; ping "$MATCH" ) | $SENDMAIL $SENDMAILFLAGS -t

Imagine for a moment what would happen if "$MATCH" was unquoted and
somebody sent you mail with Subject: ping ; rm -rf *

This is still not very secure; you should probably tighten up the
regular expression to something like 

    *   ^Subject:[      ]*ping[  ]*\/[-a-z0-9_]+(\.[-a-z0-9_]+)*

As a general rule, it's usually better to have a (perhaps slightly too
tight) regular expression which you are sure lets through only secure
characters (and not necessarily always even that. Paranoia rules). 

You don't even necessarily need Procmail for this, although it's a
nice wrapper to put around potentially misbehaving programs (and it
makes it easy to securely store received ping requests in a mail
folder, too, and even if you don't do that, at least you get a log if
you want it). The following in /etc/aliases will do more or less the
same as the above recipe, but more or less requires you to use a more
complicated script which uses a temporary file:

    ping: "| your-responder-script-here"

The script could be something like (untested)

#!/bin/sh

PATH=/make:/usr/sure:/you:/set/this:/appropriately

tmp=/tmp/ping-responder.$$
trap 'rm -f $tmp; exit 2' 1 2 3 15

cat >$tmp
host=`formail -zxSubject: <$tmp| 
      sed -e 's/^ping[  ]*//' -e 's/^\([-a-z0-9A-Z_.]*\).*/\1/'`
# Here, we hope your sed understands \(\) backrefs

( formail -rt <$tmp ; ping "$host" ) | sendmail -oi -t

rm -f $tmp

# (end of script)

Finally, if your ping returns a one-line response like just "elvis is
alive" instead of a list of ttl:s and other statistics, you could
actually include it in the Subject: line directly, and generate a
message without any body at all. (If not, you want to make sure you
set up the call to ping to pass it some options in order to prevent it
for running forever like some pings do ;^)

(I'm not entirely sure you might not have to fiddle with the quoting
on this. The following works under Bash-2.00:

formail -rt -I "Subject: `ping "$MATCH"`"

Yes, it's embarrassing to not be sure of this :-)

The other ideas you had would be fairly simple to implement along the
same lines.

See also <http://www.iki.fi/~era/procmail/links.html>

Hope this helps,

/* era */

-- 
Bot Bait: It shouldn't even matter whether  (`')  Just  (`')  http://www.iki
I am a resident of the State of Washington   \/ Married! \/   .fi/~era/

<Prev in Thread] Current Thread [Next in Thread>