KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jsmtpd > plugins > filters > ldap > LdapBodyRewriter


1 /*
2  *
3  * Jsmtpd, Java SMTP daemon
4  * Copyright (C) 2005 Jean-Francois POUX, jf.poux@laposte.net
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  *
20  */

21
22 package org.jsmtpd.plugins.filters.ldap;
23
24 import java.util.HashSet JavaDoc;
25 import java.util.Hashtable JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Set JavaDoc;
28
29 import javax.naming.Context JavaDoc;
30 import javax.naming.InitialContext JavaDoc;
31 import javax.naming.NamingEnumeration JavaDoc;
32 import javax.naming.NamingException JavaDoc;
33 import javax.naming.directory.Attribute JavaDoc;
34 import javax.naming.directory.Attributes JavaDoc;
35 import javax.naming.directory.DirContext JavaDoc;
36 import javax.naming.directory.SearchControls JavaDoc;
37 import javax.naming.directory.SearchResult JavaDoc;
38
39 import org.apache.commons.logging.Log;
40 import org.apache.commons.logging.LogFactory;
41 import org.jsmtpd.core.common.PluginInitException;
42 import org.jsmtpd.core.common.filter.FilterTreeFailureException;
43 import org.jsmtpd.core.common.filter.FilterTreeSuccesException;
44 import org.jsmtpd.core.common.filter.IFilter;
45 import org.jsmtpd.core.mail.Email;
46 import org.jsmtpd.core.mail.EmailAddress;
47 import org.jsmtpd.core.mail.InvalidAddress;
48 import org.jsmtpd.core.mail.Rcpt;
49 /**
50  * 1/04/06 : Change to query primary mail by fixed attribute, not by uid (for multiple domains and outgoing)
51  * @author jf poux
52  *
53  */

54 public class LdapBodyRewriter implements IFilter {
55     private String JavaDoc adminBindDn; // ex cn=administrator,dc=jsmtpd,dc=org
56
private String JavaDoc adminBindPassword;
57     private String JavaDoc ldapUrl; // ex ldap://172.16.0.200/
58

59     // How to query the user db
60
private String JavaDoc ldapUserProvider; // Branch containing user objects, like ou=people,dc=jsmtpd,dc=org
61
private String JavaDoc ldapUserAliasAttribute="rfc822MailAlias"; // Attribute of mail aliases
62
private String JavaDoc ldapUserPrimaryMail="mail"; // Attribute of primary mail address.
63

64     private Hashtable JavaDoc<String JavaDoc,String JavaDoc> environnement;
65     private static Log log = LogFactory.getLog(LdapBodyRewriter.class);
66     
67     public boolean doFilter(Email input) throws FilterTreeFailureException, FilterTreeSuccesException {
68         // First, see if it is an existing uid. ok = return
69
// The, see if it is an alias. ok = find uid, rewrite to uid, return
70
// See if it has catchAll. Appli catches
71
Set JavaDoc<Rcpt> toRemove = new HashSet JavaDoc<Rcpt>();
72         Set JavaDoc<Rcpt> toAdd = new HashSet JavaDoc<Rcpt>();
73         List JavaDoc<Rcpt> recipients=input.getRcpt();
74         for (Rcpt rcpt : recipients) {
75             if (!isPrimaryMail(rcpt)) {
76                 Set JavaDoc<Rcpt> aliases = computeAliases(rcpt);
77                 if (aliases!=null) {
78                     toRemove.add(rcpt);
79                     for (Rcpt alias : aliases) {
80                         log.debug("user "+rcpt.toString()+" is aliased to "+alias.toString()+" (recipient changed)");
81                         toAdd.add(alias);
82                     }
83                 } else {
84                     Set JavaDoc<Rcpt> catchAlls = computeCatchAll(rcpt);
85                     if (catchAlls!=null) {
86                         toRemove.add(rcpt);
87                         for (Rcpt catchAll : catchAlls) {
88                             log.debug("user "+rcpt.toString()+" is catch all to "+catchAll.toString()+" (recipient changed)");
89                             toAdd.add(catchAll);
90                         }
91                     } else {
92                         log.debug(" user "+rcpt.toString()+" is neither uid, alias, or catchall");
93                     }
94                 }
95             } else {
96                 log.debug(" user "+rcpt.toString()+" is a real uid");
97             }
98         }
99         // Commit changes
100
recipients.removeAll(toRemove);
101         recipients.addAll(toAdd);
102         return true;
103     }
104
105     private boolean isPrimaryMail (Rcpt recipient) {
106         InitialContext JavaDoc initialContext;
107         try {
108             initialContext = new InitialContext JavaDoc(environnement);
109             DirContext JavaDoc ctx = (DirContext JavaDoc) initialContext.lookup(ldapUrl);
110             SearchControls JavaDoc searchControl = new SearchControls JavaDoc();
111             NamingEnumeration JavaDoc<SearchResult JavaDoc> namingEnumeration;
112             String JavaDoc ldapQuery="("+ldapUserPrimaryMail+"="+recipient.getEmailAddress().toString()+")";
113             namingEnumeration = ctx.search(ldapUserProvider,ldapQuery,searchControl);
114             if (namingEnumeration.hasMore()) {
115                 return true;
116             }
117         } catch (NamingException JavaDoc e1) {
118             log.error(" Failed to query server",e1);
119         }
120         return false;
121     }
122     
123     // Returns null is no alias
124
// Returns a set of real Rcpt if aliases are set
125
private Set JavaDoc<Rcpt> computeAliases (Rcpt recipient) {
126         Set JavaDoc<Rcpt> newRecipients = new HashSet JavaDoc<Rcpt>();
127         InitialContext JavaDoc initialContext;
128         try {
129             initialContext = new InitialContext JavaDoc(environnement);
130             DirContext JavaDoc ctx = (DirContext JavaDoc) initialContext.lookup(ldapUrl);
131             SearchControls JavaDoc searchControl = new SearchControls JavaDoc();
132             NamingEnumeration JavaDoc<SearchResult JavaDoc> namingEnumeration = ctx.search(ldapUserProvider,"("+ldapUserAliasAttribute+"="+
133                     recipient.getEmailAddress().toString()+")",searchControl);
134             while (namingEnumeration.hasMore()) {
135                 SearchResult JavaDoc result = namingEnumeration.next();
136                 Attributes JavaDoc attributes = result.getAttributes();
137                 Attribute JavaDoc uid = attributes.get(ldapUserPrimaryMail);
138                 EmailAddress ad = EmailAddress.parseAddress((String JavaDoc)uid.get());
139                 ad.setHost(recipient.getEmailAddress().getHost());
140                 newRecipients.add(new Rcpt(ad));
141             }
142         } catch (NamingException JavaDoc e) {
143             log.error(" Can't query server for aliases",e);
144         } catch (InvalidAddress e) {
145             log.error(" invalid addres in alias of "+recipient.getEmailAddress().toString(),e);
146         }
147         
148         if (newRecipients.size()>0)
149             return newRecipients;
150         else
151             return null;
152     }
153     // Returns null is no alliases are defined
154
// Returns a set of real uid rcpt if positive.
155
private Set JavaDoc<Rcpt> computeCatchAll (Rcpt recipient) {
156         Set JavaDoc<Rcpt> newRecipients = new HashSet JavaDoc<Rcpt>();
157         InitialContext JavaDoc initialContext;
158         try {
159             initialContext = new InitialContext JavaDoc(environnement);
160             DirContext JavaDoc ctx = (DirContext JavaDoc) initialContext.lookup(ldapUrl);
161             SearchControls JavaDoc searchControl = new SearchControls JavaDoc();
162             NamingEnumeration JavaDoc<SearchResult JavaDoc> namingEnumeration = ctx.search(ldapUserProvider,"("+ldapUserAliasAttribute+"=@"+
163                     recipient.getEmailAddress().getHost()+")",searchControl);
164             while (namingEnumeration.hasMore()) {
165                 SearchResult JavaDoc result = namingEnumeration.next();
166                 Attributes JavaDoc attributes = result.getAttributes();
167                 Attribute JavaDoc uid = attributes.get(ldapUserPrimaryMail);
168                 EmailAddress ad = EmailAddress.parseAddress((String JavaDoc)uid.get());
169                 newRecipients.add(new Rcpt(ad));
170             }
171         } catch (NamingException JavaDoc e) {
172             log.error(" Can't query server for catchAll",e);
173         } catch (InvalidAddress e) {
174             log.error(" invalid addres in alias(catchall) of "+recipient.getEmailAddress().toString(),e);
175         }
176         
177         if (newRecipients.size()>0)
178             return newRecipients;
179         else
180             return null;
181     }
182     
183     public String JavaDoc getPluginName() {
184         return "Ldap body rewriter for Jsmtpd";
185     }
186
187     public void initPlugin() throws PluginInitException {
188         environnement = new Hashtable JavaDoc<String JavaDoc,String JavaDoc>();
189         environnement.put(Context.SECURITY_PRINCIPAL,adminBindDn);
190         environnement.put(Context.SECURITY_CREDENTIALS,adminBindPassword);
191     }
192
193     public void shutdownPlugin() {
194         
195     }
196
197     public void setAdminBindDn(String JavaDoc adminBindDn) {
198         this.adminBindDn = adminBindDn;
199     }
200
201     public void setAdminBindPassword(String JavaDoc adminBindPassword) {
202         this.adminBindPassword = adminBindPassword;
203     }
204  
205     public void setLdapUrl(String JavaDoc ldapUrl) {
206         this.ldapUrl = ldapUrl;
207     }
208
209     
210     public void setLdapUserAliasAttribute(String JavaDoc ldapUserAliasAttribute) {
211         this.ldapUserAliasAttribute = ldapUserAliasAttribute;
212     }
213
214     public void setLdapUserPrimaryMail(String JavaDoc ldapUserPrimaryMail) {
215         this.ldapUserPrimaryMail = ldapUserPrimaryMail;
216     }
217
218     public void setLdapUserProvider(String JavaDoc ldapUserProvider) {
219         this.ldapUserProvider = ldapUserProvider;
220     }
221 }
222
Popular Tags