[fetchmail]LDAP search in fetchmail

Ansgar Jazdzewski AnsgarJ@gmx.de
Wed, 22 Mar 2006 03:11:02 +0100


--Boundary-00=_3ILIEGF7rZBrVDq
Content-Type: text/plain;
  charset="us-ascii"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

Hallo,

i did a ldap integtion in fetchmail it worke fine, but i must write a temporay 
config file for fetchmail is ther a way to use fetchmail without 
"fetchmail.rc" ???

Thanks
Ansgar

--Boundary-00=_3ILIEGF7rZBrVDq
Content-Type: text/plain;
  charset="us-ascii";
  name="fetchmail.schema"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
	filename="fetchmail.schema"

#######################[ THE LDIF ]########################
#
# FMMail=ansgarj@web.de,uid=ansgar,ou=Users,ou=OxObjects,dc=attanet,dc=de
# objectclass: FMFetchMail
# FMMail: ansgarj@web.de
# FMMailServerType: pop3
# FMMailServer: pop3.web.de
# FMUserName: ansgarj
# FMUserPasswd: secret
# FMFetchMode: all

attributetype ( 1.1.2.1.2.1
	NAME 'FMMail'
        DESC 'The mail adress wehre the mails fetched from'
        EQUALITY caseIgnoreMatch
        SUBSTR caseIgnoreSubstringsMatch
        SYNTAX 1.3.6.1.4.1.1466.115.121.1.15{255} 
	SINGLE-VALUE )

attributetype ( 1.1.2.1.2.2 
	NAME 'FMMailServerType'
        DESC 'The mailservertype POP3, IMAP ...'
        EQUALITY caseIgnoreMatch
        SUBSTR caseIgnoreSubstringsMatch
        SYNTAX 1.3.6.1.4.1.1466.115.121.1.15{32768} 
	SINGLE-VALUE )

attributetype ( 1.1.2.1.2.3 
	NAME 'FMMailServer'
        DESC 'The mailserver DNS name'
        EQUALITY caseIgnoreMatch
        SUBSTR caseIgnoreSubstringsMatch
        SYNTAX 1.3.6.1.4.1.1466.115.121.1.15{32768} 
	SINGLE-VALUE )

attributetype ( 1.1.2.1.2.4 
	NAME 'FMUserName'
        DESC 'The username for loginto the mailserver'
        EQUALITY caseIgnoreMatch
        SUBSTR caseIgnoreSubstringsMatch
        SYNTAX 1.3.6.1.4.1.1466.115.121.1.15{32768} 
	SINGLE-VALUE )

attributetype ( 1.1.2.1.2.5 
	NAME 'FMUserPasswd'
        DESC 'The passwd for loginto the mailserver'
        EQUALITY caseIgnoreMatch
        SUBSTR caseIgnoreSubstringsMatch
        SYNTAX 1.3.6.1.4.1.1466.115.121.1.15{32768} 
	SINGLE-VALUE )

attributetype ( 1.1.2.1.2.6 
	NAME 'FMFetchMode'
        DESC 'The fetchmailmode kep, all ...'
        EQUALITY caseIgnoreMatch
        SUBSTR caseIgnoreSubstringsMatch
        SYNTAX 1.3.6.1.4.1.1466.115.121.1.15{16} 
	SINGLE-VALUE )

objectClass ( 1.1.2.2.2.1 NAME 'fetchmail'
	DESC 'Additional Objectclass for FetchMail'
	MUST (FMMail $ FMMailServerType $ FMMailServer $ FMUserName $ FMUserPasswd $ FMFetchMode))

--Boundary-00=_3ILIEGF7rZBrVDq
Content-Type: application/x-shellscript;
  name="fetchmail_ldap.sh"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
	filename="fetchmail_ldap.sh"

#!/bin/sh
basedn="ou=Users,ou=OxObjects,dc=attanet,dc=de"
username="cn=admin,dc=attanet,dc=de"
#passwd=absdef

rm /var/tmp/fetchmail.tmp

users=`ldapsearch -LLL -h localhost -D $username -w abcdef -x -b $basedn uid | grep uid: | cut -d ' ' -f 2 | sed -e 's/\n/ /g'`

for user in $users; do

 searchbase="uid="$user","$basedn

 temp1=`ldapsearch -LLL -h localhost -D $username -w abcdef -x -b $searchbase mail FMMail`
 EMAILTO=`echo "$temp1" | grep mail: | cut -d ' ' -f 2 | sed -e 's/^$//g'`
 TOFETCH=`echo "$temp1" | grep FMMail: | cut -d ' ' -f 2 | sed -e 's/^$//g'`

 echo $EMAILTO
 echo $TOFETCH

 if [ "x$TOFETCH" != "x" ]; then

  for EMAILFROM in $TOFETCH; do

   fetchmailsearchbase="FMMail="$EMAILFROM",uid="$user","$basedn

   temp=`ldapsearch -LLL -h localhost -D $username -w abcdef -x -b $fetchmailsearchbase FMMailServer FMMailServerType FMUserName FMUserPasswd FMFetchMode`

   SERVERNAME=`echo "$temp" | grep FMMailServer: | cut -d ' ' -f 2 | sed -e 's/^$//g'`
   PROTOCOL=`echo "$temp" | grep FMMailServerType: | cut -d ' ' -f 2 | sed -e 's/^$//g'`
   NAME=`echo "$temp" | grep FMUserName: | cut -d ' ' -f 2 | sed -e 's/^$//g'`
   PASSWORD=`echo "$temp" | grep FMUserPasswd: | cut -d ' ' -f 2 | sed -e 's/^$//g'`
   MODE=`echo "$temp" | grep FMFetchMode: |  cut -d ':' -f 2 | sed -e 's/^$//g'`

   touch /var/tmp/fetchmail.tmp
   echo "poll $SERVERNAME protocol $PROTOCOL username \"$NAME\" password \"$PASSWORD\" $MODE to $EMAILTO" >> /var/tmp/fetchmail.tmp
 
  done

 fi

done

chmod 0710 /var/tmp/fetchmail.tmp
fetchmail --fetchmailrc /var/tmp/fetchmail.tmp
--Boundary-00=_3ILIEGF7rZBrVDq
Content-Type: text/x-ldif;
  charset="us-ascii";
  name="fetchmail.ldif"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
	filename="fetchmail.ldif"

dn: FMMail=ansgarj@gmx.de,uid=ansgar,ou=Users,ou=OxObjects,dc=attanet,dc=de
FMFetchMode: no keep fetchall
FMMail: ansgarj@gmx.de
FMMailServer: pop3.web.de
FMMailServerType: pop3
FMUserName: ansgarj@gmx.de
FMUserPasswd: SECRET
objectClass: top
objectClass: fetchmail

dn: FMMail=A.Jazdzewski@t-online.de,uid=ansgar,ou=Users,ou=OxObjects,dc=attanet,dc=de
FMFetchMode: no keep fetchall
FMMail: A.Jazdzewski@t-online.de
FMMailServer: pop.btx.dtag.de
FMMailServerType: pop3
FMUserName: A.Jazdzewski
FMUserPasswd: SECRET
objectClass: top
objectClass: fetchmail


--Boundary-00=_3ILIEGF7rZBrVDq--