LDAP to Mailman

Printer-friendly versionSend to friendI maintain various web sites which use LDAP to maintain information about users. I often need these user to be subscribed to mailing lists based on groups or other attributes stored in the LDAP directory. The following script, called regurlarly with a few appropriate parameters will do just that :
#! /bin/sh
# mmldap : An ldap extractor for mailman
#
# Usage: mmldap <listname> <base> <filter> [<listoptions>]
#  <listname> is the name of an existing mailman list
#  <base> is the basedn for ldapsearch
#  <filter> is the ldap filter for ldapsearch
#  <listoptions> are the parameters passed to mailman's sync_members command
#     defaults are : -a -w=no -g=no
#
# Example :
#   mmldap MyList ou=users,o=test "(&(objectclass=user)(mail=*@*)(subscribeML=true)(!(logindisabled=true)))
#
# Author : nicolas@barcet.com


## You can define here the ice command option. Type ice on the command prompt
## for more help.
LDAPOPTIONS=""

#set default is nothing set above
if (test LDAPOPTIONS="") then
        # by default will connect anonymously to localhost with a subtree search
        LDAPOPTIONS="-s sub -h localhost"
fi

case "$1" in
    -h)
        cat $0.help
        ;;

    --help)
        more $0.readme
        ;;

    *)
        if (test -n "$3") then
                LISTNAME=$1
                BASE=$2
                FILTER=$3

                echo "$0 invoked at `date` for $LISTNAME"

                if (test -n "$4") then
                        OPTIONS=$4
                else
                        OPTIONS="-a -w=no -g=no"
                fi

                ldapsearch $LDAPOPTION -b $BASE -x $FILTER mail | grep mail: | awk '{print $2}' > /tmp/maillist
                /usr/lib/mailman/bin/sync_members $OPTIONS -f /tmp/maillist $LISTNAME
                rm /tmp/maillist

        else
                echo "  Usage: $0 <listname> <base> <filter> [<listoptions>]"
                echo "  <listname> is the name of an existing mailman list"
                echo "  <base> is the basedn for ldapsearch"
                echo "  <filter> is the ldap filter for ldapsearch"
                echo "  <listoptions> are the parameters passed to mailman's sync_members command"
                echo "     defaults are : -a -w=no -g=no"
        fi
        ;;
esac

Share this

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

no ldap = clear memberslist

I've had to add a check whether LDAP is working, because this script cleared all my members from all my lists ;(

Interesting: care to share?

Thanks a lot for catching this. Care to share your modification?

Nick

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.