Export Exchange recipients to Postfix server

When  you have an Exchange server in your organization and you also use a Postfix server as gateway, you need the list of all valid recipients of your organization at your gateway. In this way, you can reject invalid emails at the gateway, and what’s more important, when the sender address is forged, you don’t spam innocent people with undeliverable emails.

I use this script in Exchange 2003 to generate all addresses.

export_exchange_recipients.vbs:

' Export all valid recipients (= proxyAddresses) into a
' file virtual.txt
'
' Ferdinand Hoffmann & Patrick Koetter
' 20021100901
' Shamelessly stolen from 
' http://www.microsoft.com/windows2000/techinfo/ 
' planning/activedirectory/bulksteps.asp
'
' http://postfix.state-of-mind.de/patrick.koetter/mailrelay/


'Global variables
Dim Container
Dim OutPutFile
Dim FileSystem

'Initialize global variables
Set FileSystem = WScript.CreateObject("Scripting.FileSystemObject")
Set OutPutFile = FileSystem.CreateTextFile("virtual.txt", True)
Set Container=GetObject("LDAP://DC=ahesp,DC=net")

'Enumerate Container
EnumerateUsers Container
EnumerateGroups Container
EnumerateContacts Container

'Clean up
OutPutFile.Close
Set FileSystem = Nothing
Set Container = Nothing

'Say Finished when your done
' WScript.Echo "Finished"
WScript.Quit(0)

'List all Users
Sub EnumerateUsers(Cont)
        Dim User

        'Go through all Users and select them
        For Each User In Cont
                Select Case LCase(User.Class)

                'If you find Users
                Case "user"
                  'Select all proxyAddresses
                  Dim Alias
                  If Not IsEmpty(User.proxyAddresses) Then
                        For Each Alias in User.proxyAddresses
                        OutPutFile.WriteLine Alias & "   x"
                        'WScript.Echo Alias
                  Next
                  End If

                Case "organizationalunit" , "container"
                  EnumerateUsers User

                End Select
        Next
End Sub


'List all Groups
Sub EnumerateGroups(Cont)
        Dim Group

        'Go through all Groups and select them
        For Each Group In Cont
                Select Case LCase(Group.Class)

                'If you find Group
                Case "group"
                  'Select all proxyAddresses
                  Dim Alias
                  If Not IsEmpty(Group.proxyAddresses) Then
                        For Each Alias in Group.proxyAddresses
                        OutPutFile.WriteLine Alias & "   x"
                        'WScript.Echo Alias
                  Next
                  End If

                Case "organizationalunit" , "container"
                  EnumerateGroups Group

                End Select
        Next
End Sub

'List all Contacts
Sub EnumerateContacts(Cont)
        Dim Contact

        'Go through all Contacts and select them
        For Each Contact In Cont
                Select Case LCase(Contact.Class)

                'If you find Contact
                Case "contact"
                  'Select all proxyAddresses
                  Dim Alias
                  If Not IsEmpty(Contact.proxyAddresses) Then
                        For Each Alias in Contact.proxyAddresses
                        OutPutFile.WriteLine Alias & "   x"
                        'WScript.Echo Alias
                  Next
                  End If

                Case "organizationalunit" , "container"
                  EnumerateContacts Contact

                End Select
        Next
End Sub

Then, I just have to filter the smtp addresses and loading them in Postfix. I use Cygwin to be able to scp them to the Postfix server.

clean_and_copy.sh:

#!/bin/bash
cat virtual.txt | egrep '^smtp:|^SMTP:' | sed -e 's/smtp://' | sed -e 's/SMTP://' > virtual2.txt

scp virtual2.txt root@postfix.server:/etc/postfix/relay_recipient_maps
ssh root@postfix.server "postmap /etc/postfix/relay_recipient_maps"
ssh root@postfix.server "postfix reload"

I run all this with a bat file in a Windows scheduled task:

export_recipients.bat:

@ECHO OFF
SET PATH=C:cygwinbin;%PATH%
c:
cd C:export_recipients
export_exchange_recipients.vbs
c:cygwinbinbash clean_and_copy.sh

To enforce the recipient address validation, this directive is needed in Postfix:

relay_recipient_maps = hash:/etc/postfix/relay_recipients_maps
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s