Automatically reconfigure smtp relays in exim with NetworkManager and whereami

I’ve found a way to have my local SMTP server (exim) on my notebook automatically reconfigured to the proper relay host whenever I change networks.

I’m using NetworkManager in Gnome (on the Debian system), and its hook scripts in /etc/NetworkManager/dispatcher.d to launch whereami, which will detect which network conf is activated, and hence reconfigure the smtp relay to use, with its script setmailrelay.


Here’s the way I configured things :

I configure whereami in order for it to be as passive as possible on the detection tests (not using dhcp requests or doing things like ifconfig up/down) in order not to mess with what network-manager (and its dhcp “agent”) is doing :

In /etc/whereami/detect.conf, I then use things like :

testprocsys /proc/net/route,eth1[[:space:]]+006E9F9D work

  • testprocsys is just one of the scripts of whereami, which does nothing but check something in a file :no damage, then,
  • /proc/net/route is the file containing the route table (more or less the same as what’s provided by netstat -rn) : it will contain your network address, next to the interface of the network card, helping to determine which network you’re plugged to. Note that the notation is in hexa. Here, my network is 157.159.110.0, which gives 006E9F9D.
  • eth1 is the interface of my wired network card on the laptop
  • 006E9F9D is the network address, as explained above.

Then, in addition to the regular ifupdown hooks installed by whereami, I’ll add a hook for NetworkManager’s dispatcher in /etc/NetworkManager/dispatcher.d :

Here’s contents of this script : /etc/NetworkManager/dispatcher.d/30whereami (30 is arbitrary here) :


#!/bin/sh -e

if [ -z "$1" ]; then
echo "$0: called with no interface" 1>&2
exit 1;
fi

# Fake ifupdown environment
export IFACE="$1"
export LOGICAL="$1"
export ADDRFAM="NetworkManager"
export METHOD="NetworkManager"
#export VERBOSITY="0"
export VERBOSITY="1"

# Run the right scripts
case "$2" in
up)
export MODE="start"
export PHASE="up"

logger -t NetworkManager "dispatcher.d $0 up : running whereami"
whereami
;;
down)
export MODE="stop"
export PHASE="down"
;;
pre-up)
export MODE="start"
export PHASE="pre-up"
;;
post-down)
export MODE="stop"
export PHASE="post-down"
logger -t NetworkManager "dispatcher.d $0 post-down : running whereami"
whereami
*)
echo "$0: called with unknown action \`$2'" 1>&2
exit 1
;;
esac

Then in /etc/whereami/whereami.conf, finally, I set the mail relay host :

=work setmailrelay smtp.int-evry.fr

Hope this helps.

Any comments welcome, of course.

3 thoughts on “Automatically reconfigure smtp relays in exim with NetworkManager and whereami”

  1. Great info, but: your script 30whereami doesn’t work on my Fedora 8 laptop using bash as the default shell. In ‘case “$2″‘, I need to remove the double quotes from around ‘$2’.

Leave a Reply

Your email address will not be published.