1 package org.ejbca.core.model.ca.publisher; 2 3 import java.io.UnsupportedEncodingException ; 4 import java.util.HashMap ; 5 import java.util.Iterator ; 6 import java.util.regex.Pattern ; 7 8 import org.apache.log4j.Logger; 9 import org.ejbca.core.model.InternalResources; 10 import org.ejbca.util.CertTools; 11 12 import com.novell.ldap.LDAPConnection; 13 import com.novell.ldap.LDAPEntry; 14 import com.novell.ldap.LDAPException; 15 import com.novell.ldap.LDAPSearchResults; 16 17 public class LdapSearchPublisher extends LdapPublisher { 18 19 private static final Logger log = Logger.getLogger(LdapSearchPublisher.class); 20 21 private static final InternalResources intres = InternalResources.getInstance(); 22 23 public static final int TYPE_LDAPSEARCHPUBLISHER = 4; 24 25 protected static final String SEARCHBASEDN = "searchbasedn"; 27 protected static final String SEARCHFILTER = "searchfilter"; 28 29 public LdapSearchPublisher() { 30 super(); 31 data.put(TYPE, new Integer (TYPE_LDAPSEARCHPUBLISHER)); 32 33 setSearchBaseDN(""); 34 setSearchFilter(""); 35 } 36 37 39 40 43 protected LDAPEntry searchOldEntity(String username, int ldapVersion, LDAPConnection lc, String dn) throws PublisherException { 44 LDAPEntry oldEntry = null; int searchScope; 46 String searchbasedn; 47 boolean attributeOnly; 48 49 try { 53 log.debug("Connecting to " + getHostname()); 55 lc.connect(getHostname(), Integer.parseInt(getPort())); 56 log.debug("Logging in with BIND DN " + getLoginDN()); 58 lc.bind(ldapVersion, getLoginDN(), getLoginPassword().getBytes("UTF8")); 59 String searchFilter = getSearchFilter(); 62 log.debug("Compiling search filter: " +searchFilter); 63 if (username != null) { 64 Pattern USER = Pattern.compile("\\$USERNAME", Pattern.CASE_INSENSITIVE); 65 searchFilter = USER.matcher(searchFilter).replaceAll(username); 66 } 67 if (CertTools.getPartFromDN(dn, "CN") != null) { 68 Pattern CN = Pattern.compile("\\$CN", Pattern.CASE_INSENSITIVE); 69 searchFilter = CN.matcher(searchFilter).replaceAll(CertTools.getPartFromDN(dn, "CN")); 70 } 71 if (CertTools.getPartFromDN(dn, "O") != null) { 72 Pattern O = Pattern.compile("\\$O", Pattern.CASE_INSENSITIVE); 73 searchFilter = O.matcher(searchFilter).replaceAll(CertTools.getPartFromDN(dn, "O")); 74 } 75 if (CertTools.getPartFromDN(dn, "OU") != null) { 76 Pattern OU = Pattern.compile("\\$OU", Pattern.CASE_INSENSITIVE); 77 searchFilter = OU.matcher(searchFilter).replaceAll(CertTools.getPartFromDN(dn, "OU")); 78 } 79 if (CertTools.getPartFromDN(dn, "C") != null) { 80 Pattern C = Pattern.compile("\\$C", Pattern.CASE_INSENSITIVE); 81 searchFilter = C.matcher(searchFilter).replaceAll(CertTools.getPartFromDN(dn, "C")); 82 } 83 log.debug("Resulting search filter '" + searchFilter+"'."); 84 searchScope = LDAPConnection.SCOPE_SUB; 85 log.debug("Making SRCH with BaseDN '" + getSearchBaseDN() + "' and filter '" + searchFilter+"'."); 86 searchbasedn = getSearchBaseDN(); 87 String attrs[] = { LDAPConnection.NO_ATTRS }; 88 attributeOnly = true; 89 LDAPSearchResults searchResults = lc.search(searchbasedn, searchScope, searchFilter, attrs, attributeOnly); if (log.isDebugEnabled()) { 96 log.debug("serachResults contains entries: "+searchResults.hasMore()); 97 } 98 if (searchResults.hasMore()) { 99 oldEntry = searchResults.next(); 100 dn = oldEntry.getDN(); 101 if (searchResults.hasMore()) { 102 log.debug("Found more than one matches with filter '" + searchFilter + 103 "'. Using the first match with LDAP entry with DN: " +oldEntry.getDN()); 104 } else { 105 log.debug("Found one match with filter: '"+searchFilter+"', match with DN: " + oldEntry.getDN()); 106 } 107 } else { 108 log.debug("No matches found using filter: '" +searchFilter + "'. Using DN: " + dn); 109 } 110 try { 112 oldEntry = lc.read(dn); 113 } catch (LDAPException e) { 114 if (e.getResultCode() == LDAPException.NO_SUCH_OBJECT) { 115 String msg = intres.getLocalizedMessage("publisher.noentry", dn); 116 log.info(msg); 117 } else { 118 String msg = intres.getLocalizedMessage("publisher.infoexists", dn); 119 log.info(msg); 120 } 121 } 122 } catch (LDAPException e) { 123 if (e.getResultCode() == LDAPException.NO_SUCH_OBJECT) { 124 String msg = intres.getLocalizedMessage("publisher.noentry", dn); 125 log.info(msg); 126 } else { 127 String msg = intres.getLocalizedMessage("publisher.errorldapbind", e.getMessage()); 128 log.error(msg, e); 129 throw new PublisherException(msg); 130 } 131 } catch (UnsupportedEncodingException e) { 132 String msg = intres.getLocalizedMessage("publisher.errorpassword", getLoginPassword()); 133 throw new PublisherException(msg); 134 } finally { 135 try { 137 lc.disconnect(); 138 } catch (LDAPException e) { 139 String msg = intres.getLocalizedMessage("publisher.errordisconnect"); 140 log.error(msg, e); 141 } 142 } 143 return oldEntry; 144 } 145 146 149 public String getSearchBaseDN() { 150 return (String ) data.get(SEARCHBASEDN); 151 } 152 153 156 public void setSearchBaseDN(String searchbasedn) { 157 data.put(SEARCHBASEDN, searchbasedn); 158 } 159 160 163 public String getSearchFilter() { 164 return (String ) data.get(SEARCHFILTER); 165 } 166 167 170 public void setSearchFilter(String searchfilter) { 171 data.put(SEARCHFILTER, searchfilter); 172 } 173 174 175 177 178 181 public Object clone() throws CloneNotSupportedException { 182 LdapSearchPublisher clone = new LdapSearchPublisher(); 183 HashMap clonedata = (HashMap ) clone.saveData(); 184 185 Iterator i = (data.keySet()).iterator(); 186 while (i.hasNext()) { 187 Object key = i.next(); 188 clonedata.put(key, data.get(key)); 189 } 190 191 clone.loadData(clonedata); 192 return clone; 193 } 194 195 } 196 | Popular Tags |