#!/bin/sh # # /usr/local/sbin/localmailtransportinit # Copyright 2006 Jonas Smedegaard # # $Id: localmailtransportupdate,v 1.2 2006-07-28 17:04:48 jonas Exp $ # # Setup postfix transport map for a redpill organisation # set -e PRG=`basename $0` TEMP=$(getopt -s sh -o fid:h -l force,init,diffopts:,help -n "$PRG" -- "$@") if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi eval set -- "$TEMP" function usage() { echo "Usage: $PRG [opts...]" echo echo " -f, --force Update without asking for confirmation" echo " -i, --init Setup postfix to use the transport map" echo " -d, --diffopts=diffopts Extra options applied to diff when" echo " comparing current and new map" echo " -h|--help This help text" exit 1 } function exit1() { echo "Error: $1" echo "Exiting..." exit 1 } force="" init="" diffopts="" while true ; do case "$1" in -f|--force) force="1" ; shift ;; -i|--init) init="1" ; shift ;; -d|--diffopts) diffopts="1" ; shift ;; -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() { for realm in $@; do realmdir="/etc/local-$realm" [ -d "$realmdir/redpill" ] || continue mailhost="$(firstitem "$realmdir/redpill/mailhost")" maildomains="$(items "$realmdir/redpill/maildomains")" for maildomain in $maildomains; do echo "$maildomain relay:$mailhost:submission" done done } mktransportmap $(items /etc/local-ORG/realms) > /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 ) 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 "Changes applied!" ;; *) echo "Aborted!" exit 1 ;; esac if [ "$init" = "1" ]; then postconf -e "transport_maps = hash:/etc/postfix/transport" invoke-rc.d postfix reload fi