Packetwatch.net

Configuring email for system event notification in OpenBSD



Last modified: Oct. 22, 2008

Contents
1 - Summary
2 - Sendmail configuration
3 - Example shell script
4 - Service check


1 - Summary

This guide will show you how to configure email using the mail utility for
system event notication in OpenBSD. Mail is a command line utility that sends
email using SMTP. This has been tested in OpenBSD 4.3.


2 - Sendmail configuration

Sendmail is included in OpenBSD by default. Edit /etc/rc.conf.local to have
the following options so it will start on bootup.

  sendmail_flags="-L sm-mta -C/etc/mail/server.test.com.cf -bd -q30m"

# sudo vi /etc/rc.conf.local

Get the hostname of the computer.
# hostname
server.test.com

Do the following to get a configuration file.
# sudo mkdir -p /root/backups/originals
# sudo cp -Rp /etc/mail/ /root/backups/originals/mail/
# cd /usr/share/sendmail/cf
# sudo cp openbsd-proto.mc server.test.com.mc
# sudo chmod u+w server.test.com.mc

After this you will have a configuration file named server.test.com. Make the
following changes to server.test.com.mc.

  define(`SMART_HOST', `mail.test.com')dnl
  define(`confCW_FILE', `-o MAIL_SETTINGS_DIR`'local-host-names')dnl
  FEATURE(genericstable, `hash -o /etc/mail/genericstable')dnl
  GENERICS_DOMAIN_FILE(`/etc/mail/generics-domains')dnl
  dnl DAEMON_OPTIONS(`Family=inet6, Address=::, Name=MTA6, M=O')dnl
  dnl DAEMON_OPTIONS(`Family=inet, Address=0.0.0.0, Port=587, Name=MSA, M=E')dnl
  dnl DAEMON_OPTIONS(`Family=inet6, Address=::, Port=587, Name=MSA6, M=O, M=E')dnl
  dnl CLIENT_OPTIONS(`Family=inet6, Address=::')dnl
  FEATURE(masquerade_envelope)dnl

# sudo vi server.test.com.mc

Create the generics-domains file with the following so that you can rewrite
your (envelope) From: address for your host.

  server.test.com

# cd /etc/mail
# sudo vi generics-domains

Add the following to the genericstable file which contains the mapping between
your local email address and your external one. We will use the root user so
you might want to change the comment field in /etc/passwd appropriately.

  root                noreply@test.com

# sudo vi genericstable

Add the following to the local-host-names file so that the computer can
deliver email to local users.

  server.test.com
  server

# sudo vi local-host-names

Rehash the files.
# sudo make

Rebuild the configuration file.
# cd /usr/share/sendmail/cf
# sudo m4 ../m4/cf.m4 server.test.com.mc > /etc/mail/server.test.com.cf

Stop the sendmail service and then start it up again.
# sudo kill -9 `head -n 1 /var/run/sendmail.pid`
# sudo rm -fP /var/run/sendmail.pid
# sudo ps aux | grep sendmail
# sudo sendmail -L sm-mta -C/etc/mail/server.test.com.cf -bd -q30m
# sudo cat /var/run/sendmail.pid
20413
sendmail -L sm-mta -C/etc/mail/server.test.com.cf -bd -q30m


3 - Example shell script

Here is an example shell script that sends an email.

#!/bin/sh

clear="/usr/bin/clear"
echo="/bin/echo"
mail="/usr/bin/mail"
recipient="testuser@test.com"
subject="Subject"
body="Body"

$clear
$echo $body | $mail -s "$subject" $recipient
exit 0


4 - Service check

Reboot your computer. Log in like normal and check to see that the sendmail
service is running. That's it, now you have configured email for system event
notification in OpenBSD.
# sudo shutdown -r now
# sudo ls -1 /var/run/sendmail.pid
/var/run/sendmail.pid
# sudo cat /var/run/sendmail.pid
13276
/usr/sbin/sendmail -L sm-mta -C/etc/mail/server.test.com.cf -bd -q30m


Last modified: Wed Dec 31 19:00:00 1969 EST
Packetwatch Research 2002-2008.