#!/bin/sh # # /usr/local/sbin/localmailtransportinit # Copyright 2006-2008 Jonas Smedegaard # # $Id: localmailtransportupdate,v 1.13 2008-02-13 01:48:51 jonas Exp $ # # Setup postfix transport map for a redpill organisation # set -e PRG=$(basename $0) TEMP=$(getopt -s sh -o r:fid:h -l redpill:,force,init,diffopts:,help -n "$PRG" -- "$@") if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi eval set -- "$TEMP" usage() { cat <&2 Usage: $PRG [opts...] [redpill] -r, --redpill Redpill context (default: read /etc/local-ORG/redpill) -f, --force Update without asking for confirmation -i, --init Setup postfix to use the transport map -d, --diffopts=diffopts Extra options applied to diff when comparing current and new map -h|--help This help text EOF exit 0 } exit1() { echo >&2 "Error: $1" echo >&2 "Exiting..." exit 1 } redpill="" force="" init="" diffopts="" while true ; do case "$1" in -f|--force) force="1"; shift;; -i|--init) init="1"; shift;; -d|--diffopts) diffopts="$1"; shift 2;; -h|--help) usage;; --) shift; break;; *) exit1 "Internal error!";; esac done firstitem() { file="$1" cat "$file" | grep -v '^#' | sed 's/#.*//' | head -n 1 } items() { file="$1" cat "$file" | grep -v '^#' | sed 's/#.*//' } mktransportmap() { redpill="$1" echo "# NB! This file was auto-generated by $PRG" echo redpilldir="/etc/local-$redpill/" realms="$(items "$redpilldir/realms")" thisrealm="$(cat /etc/local-ORG/realm || dnsdomainname | tr '[a-z]' '[A-Z]')" for realm in $realms; do [ "$thisrealm" = "$realm" ] && continue realmdir="$redpilldir/$realm" [ -d "$realmdir" ] || continue mailhost="$(firstitem "$realmdir/mailhost")" maildomains="$(items "$realmdir/maildomains")" for maildomain in $maildomains; do echo "$maildomain relay:[$mailhost]:submission" done done } case $# in 0) if [ -z "$redpill" ]; then redpill="$(firstitem "/etc/local-ORG/redpill")"; fi;; 1) redpill="$1";; *) exit1 "Wrong number of arguments!";; esac [ -n "$redpill" ] || exit1 "Redpill context missing!" mktransportmap "$redpill" > /etc/postfix/transport.new if [ -f /etc/postfix/transport.addon ]; then cat /etc/postfix/transport.addon >> /etc/postfix/transport.new fi ( cd /etc/postfix && diff $diffopts transport transport.new || true ) if [ "$force" = "1" ]; then do_update="y" else echo -n "Above is the intended changes. OK to update (y/N)? " read do_update fi case $do_update in y|Y) mv /etc/postfix/transport.new /etc/postfix/transport postmap /etc/postfix/transport echo >&2 "Changes applied!" ;; *) exit1 "Aborted!" ;; esac if [ "$init" = "1" ]; then postconf -e "transport_maps = hash:/etc/postfix/transport" invoke-rc.d postfix reload fi