Hi Postfix Users!

I decided to write a bash script for extracting recipients from Windows Active Directory for a Postfix box in front of Exchange.

I realise there are other scripts out there but wanted to give it a try myself as part of me learning more about scripting and the command line.

Any feedback, suggestions, criticism appreciated.

Kind Regards,

James

#!/bin/bash

#=======================================================================================#
#    INFORMATION
#=======================================================================================#

## Author:    James Robertson
## Date:    14.06.2009
## Depends: hostname, echo, ldap-utils, grep, awk, mailx, tac, date, postmap
##
## Details: The script uses ldapsearch to perform an ldap query against a Windows
##        Domain Controller to extract email recipient addresses for use in
## Postfix. It might work against other LDAP servers but has not been
##        tested and would likely require some change to the query.
##        relay_domains need to be in a file.
##        This script is free to use, distribute and alter in any way.

#=======================================================================================#
#    VARIABLES
#=======================================================================================#

## ldap variables - edit these values
LDAPHOST=123.123.123.123
LDAPUSER='u...@example.local'
LDAPPASS='password'
LDAPBASE='dc=example,dc=local'
alerts=ale...@example.com

## command variables - based on Debian Lenny update for your distro
HOST=$(/bin/hostname -s | awk '{print toupper($1)}')
AWK=/usr/bin/awk
TAC=/usr/bin/tac
GREP=/bin/grep
ECHO=/bin/echo
MAIL=/usr/bin/mail
LDAPSEARCH=/usr/bin/ldapsearch
POSTMAP=/usr/sbin/postmap
TIMESTAMP=$(/bin/date +%Y%m%d%M%H)

## file variables
RELAYDOMAINS=/etc/postfix/relay_domains
EXCHRECIPIENTS=/etc/postfix/exchange_recipients
ERRORLOG=/tmp/ldap-recipients-log.$$

#=======================================================================================#
#    ERROR FUNCTIONS
#=======================================================================================#

## check exit status of last command for errors and set errortmp
ErrorTest ()
{
  if [ "$1" != "0" ]; then
     errortmp=1;
fi }

## email alerts address if something went wrong
ErrorReport ()
{
  if [ "$errortmp" == "1" ]; then
      $ECHO "$TIMESTAMP ERROR $1 " >> $ERRORLOG
      $TAC $ERRORLOG | $MAIL -s \
          "$HOST RECIPIENT UPDATE ENCOUNTERED AN ERROR!"  \
          $ALERTS
      exit 1
  fi
}

#=======================================================================================#
#    SCRIPT
#=======================================================================================#

## do ldap query and format output for postfix
errortmp=0
  $LDAPSEARCH -x -h $LDAPHOST -D $LDAPUSER -w $LDAPPASS -b $LDAPBASE \
'(proxyAddresses=*)' 2>$ERRORLOG | $GREP -f $RELAYDOMAINS | $GREP -Fi smtp \
  | $AWK -F':' '{print $3" OK"}' > $EXCHRECIPIENTS; RETVAL=${PIPESTATUS[0]}
  ErrorTest $RETVAL
ErrorReport "ldap query failed to $LDAPHOST";

## check recipients file exists and is not zero
errortmp=0
  [[ -s $EXCHRECIPIENTS ]] 2>$ERRORLOG; RETVAL="$?"
  ErrorTest $RETVAL
ErrorReport "$EXCHRECIPIENTS is missing or empty"

## hash the recipients file
errortmp=0
  $POSTMAP $EXCHRECIPIENTS 2>$ERRORLOG; RETVAL="$?"
  ErrorTest $RETVAL
ErrorReport "postmap failed to hash $EXCHRECIPIENTS"

## END

Reply via email to