Last active
September 9, 2016 10:08
-
-
Save jagland/c8ce0d6b89fb3c09193d to your computer and use it in GitHub Desktop.
Generate Aliases file based on multiple sources
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
################################################################## | |
# Copyright (C) 2013 - Jon Agland # | |
# # | |
# This program is free software; you can redistribute it and/or # | |
# modify it under the terms of the GNU General Public License # | |
# as published by the Free Software Foundation; either version 2 # | |
# of the License, or (at your option) any later version. # | |
# # | |
# This program is distributed in the hope that it will be useful,# | |
# but WITHOUT ANY WARRANTY; without even the implied warranty of # | |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # | |
# GNU General Public License for more details. # | |
################################################################## | |
# --------------------------------------------------------------- | |
# Name: generate-aliases | |
# Version: 0.06 | |
# Description: This script is intended to run as a cronjob. there | |
# should be no output to the console. It's based on querying a | |
# number of LDAP/Active Directory instances to create a single | |
# alias file. Useful if trying to merger two domains into new one | |
# | |
DIR=/usr/local/generate-aliases | |
ALERTEMAIL="" | |
NUMBERLDAPCALLS=4 | |
DESTFILE="/etc/exim/incoming/newdom" | |
LDAPDN[1]="" | |
LDAPPASS[1]="" | |
LDAPBASE[1]="" | |
LDAPHOST[1]="" | |
LDAPOPTS[1]="-s sub -E pr=200/noprompt" | |
GREPOPTS[1]="@dom1" | |
IGNOREOPTS[1]=`cat $DIR/ignore/dom1` | |
PERMFILE[1]=$DIR/output/dom1 | |
LDAPDN[2]="" | |
LDAPPASS[2]="" | |
LDAPBASE[2]="" | |
LDAPHOST[2]="" | |
LDAPOPTS[2]="-s sub -E pr=200/noprompt" | |
GREPOPTS[2]="@dom2" | |
IGNOREOPTS[2]=`cat $DIR/ignore/dom2` | |
PERMFILE[2]=$DIR/output/dom2 | |
LDAPDN[3]="" | |
LDAPPASS[3]="" | |
LDAPBASE[3]="" | |
LDAPHOST[3]="" | |
LDAPOPTS[3]="-s sub -E pr=200/noprompt" | |
PERMFILE[3]=$DIR/output/dom3 | |
IGNOREOPTS[3]=`cat $DIR/ignore/dom3` | |
GREPOPTS[3]="@dom" | |
LDAPDN[4]="" | |
LDAPPASS[4]="" | |
LDAPBASE[4]="" | |
LDAPHOST[4]="" | |
LDAPOPTS[4]="-s sub -E pr=200/noprompt" | |
PERMFILE[4]=$DIR/output/dom4 | |
GREPOPTS[4]="@dom" | |
IGNOREOPTS[4]=`cat $DIR/ignore/dom4` | |
# common | |
STATICALIASES=$DIR/static-aliases | |
ERRORLOG=$DIR/log/generate-aliases-err.log | |
LOG=$DIR/log/generate-aliases.log | |
LDAPSEARCH="/usr/bin/ldapsearch" | |
TMPDESTFILE=`mktemp` | |
TMPDUPFILE=`mktemp` | |
RANDOMTMP=`mktemp` | |
if [ -f $ERRORLOG ] | |
then | |
rm $ERRORLOG | |
fi | |
j=0 | |
while [ "$j" -lt $NUMBERLDAPCALLS ] | |
do | |
let "j++" | |
FILE=${PERMFILE[$j]} | |
if [ "${GREPOPTS[$j]}" ] | |
then | |
$LDAPSEARCH ${LDAPOPTS[$j]} -w ${LDAPPASS[$j]} -D ${LDAPDN[$j]} -h ${LDAPHOST[$j]} -b ${LDAPBASE[$j]} '(&(!(UserAccountControl:1.2.840.113556.1.4.803:=2))(!(objectclass=contact)))' proxyAddresses | grep -i "${GREPOPTS[$j]}" >$FILE | |
else | |
$LDAPSEARCH ${LDAPOPTS[$j]} -w ${LDAPPASS[$j]} -D ${LDAPDN[$j]} -h ${LDAPHOST[$j]} -b ${LDAPBASE[$j]} '(&(!(UserAccountControl:1.2.840.113556.1.4.803:=2))(!(objectclass=contact)))' proxyAddresses > $FILE | |
fi | |
cat $FILE | grep -i "smtp:" | awk -F\: '{ print $3}' | awk -F@ '{print $1":"$1"@"$2}'| sort -u > $RANDOMTMP | |
dd if=$RANDOMTMP of=$FILE conv=lcase >/dev/null 2>&1 | |
cp $FILE ${PERMFILE[$j]}-org | |
if [ "${IGNOREOPTS[$j]}" ] | |
then | |
for i in ${IGNOREOPTS[$j]} | |
do | |
echo $(date +"%b %d %H:%M:%S") "ignoring duplicate for "$i" in "${LDAPHOST[$j]},${LDAPBASE[$j]} >>$LOG | |
cat $FILE | grep -i -v $i > $RANDOMTMP | |
cp $RANDOMTMP $FILE | |
done | |
fi | |
if [ -s ${PERMFILE[$j]} ] | |
then | |
# good to go | |
cat ${PERMFILE[$j]} >> $TMPDESTFILE | |
else | |
echo $(date +"%b %d %H:%M:%S") "LDAP Search against $LDAPHOST failed (temp file is empty)" >>$LOG | |
echo $(date +"%b %d %H:%M:%S") "LDAP Search against $LDAPHOST failed (temp file is empty)" >>$ERRORLOG | |
exit 1 | |
fi | |
done | |
sort $TMPDESTFILE | awk -F\: '{ print $1 }' | uniq -c | grep -v ' 1 ' > $TMPDUPFILE | |
if [ -s $TMPDUPFILE ] | |
then | |
echo $(date +"%b %d %H:%M:%S") "We have the following duplicates." >>$LOG | |
echo $(date +"%b %d %H:%M:%S") "We have the following duplicates." >>$ERRORLOG | |
cat $TMPDUPFILE >>$LOG | |
cat $TMPDUPFILE >>$ERRORLOG | |
echo $(date +"%b %d %H:%M:%S") "Identifying source of duplicates." >>$LOG | |
echo $(date +"%b %d %H:%M:%S") "Identifying source of duplicates." >>$ERRORLOG | |
g=0 | |
while [ "$g" -lt $NUMBERLDAPCALLS ] | |
do | |
let "g++" | |
echo $(date +"%b %d %H:%M:%S") "Duplicates for host ${LDAPHOST[$g]},${LDAPBASE[$g]}" >>$LOG | |
echo $(date +"%b %d %H:%M:%S") "Duplicates for host ${LDAPHOST[$g]},${LDAPBASE[$g]}" >>$ERRORLOG | |
DUPS=`cat $TMPDUPFILE | awk '{ print $2}'` | |
for k in $DUPS | |
do | |
if [ -f ${PERMFILE[$g]} ] | |
then | |
cat ${PERMFILE[$g]} | grep $k:$k@ >>$LOG | |
cat ${PERMFILE[$g]} | grep $k:$k@ >>$ERRORLOG | |
else | |
echo $(date +"%b %d %H:%M:%S") "no duplicates for the above host" >>$ERRORLOG | |
echo $(date +"%b %d %H:%M:%S") "no duplicates for the above host" >>$LOG | |
fi | |
done | |
done | |
else | |
# Good to go | |
cp $TMPDESTFILE $DESTFILE | |
cat $STATICALIASES >> $DESTFILE | |
sort $DESTFILE --output=$DESTFILE | |
g=0 | |
while [ "$g" -lt $NUMBERLDAPCALLS ] | |
do | |
let "g++" | |
# removing temp files | |
#rm ${PERMFILE[$g]} | |
#echo ${PERMFILE[$g]} | |
done | |
# removing temp files | |
rm $TMPDESTFILE $TMPDUPFILE $RANDOMTMP | |
fi | |
if [ -f $ERRORLOG ] | |
then | |
FQDN=`hostname -f` | |
mail -s "Error in $0 on $FQDN" $ALERTEMAIL < $ERRORLOG | |
rm $ERRORLOG | |
fi | |
echo $(date +"%b %d %H:%M:%S") "$0 complete." >>$LOG | |
chmod +r $DESTFILE |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment