$#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:

Mrandom,        P=/opt/bin/random,
                S=ruleset_noop, R=ruleset_random,
                A=random $u

C{Random} adamo yiorgos admin system operator
Kcomp arith

# This ruleset does nothing

# 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

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:

# 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
        # noop

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