$#random – a local mailer that returns a random EX_*

I found myself in a situation where I needed a local mailer that was required to return random exit values0 for email directed to certain users. Luckily, most of the job can be done via your .mc file:

MAILER_DEFINITIONS
Mrandom,        P=/opt/bin/random,
                F=lsDFMqbE,
                S=ruleset_noop, R=ruleset_random,
                T=DNS/RFC822/X-Unix,
                A=random $u

LOCAL_CONFIG
C{Random} adamo yiorgos admin system operator
Kcomp arith

LOCAL_RULESETS
Sruleset_noop
# This ruleset does nothing

Sruleset_random
# EX__BASE == 64 and EX__MAX == 78. EX_OK == 0, so we mask it to 79.
R$*             $: $1 . $(comp r $@ 64 $@ 79 $)
R$* . 79        $@ $1 . 0

LOCAL_RULE_0
R$={Random}  < $=w . > $*        $#random $: $1

When $#random is called, ruleset_random is called with $1 (the username) as an argument and returns $1.number, where number is a random number between EX__BASE (64) and EX__MAX (78) or zero. Therefore, the $#random binary is executed with $1.number as an argument.

/opt/bin/random can be as simple as this shell script1:

#!/bin/sh
# This is an example (non) delivery agent and is not considered safe to run on a 
# production server.
status=`echo $1 | sed -e 's/.*\.\(.*\)$/\1/'`

while read x
do
        # noop
done

exit $status

A random EX_* value is returned to the sender of the email, assuming s/he has emailed a local user contained in class $={Random}.


[0] – Valid sendmail exit codes (EX_*) are defined in sysexits.h.
[1] – In my case I wrote the mail delivery agent in C, as I needed to do a few more stuff prior to calling exit().

2 thoughts on “$#random – a local mailer that returns a random EX_*

  1. Indeed. But then again I needed to test a system on how it handles unexpected failures, and it seemed like an nice toy to have.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s