1 package org.enhydra.shark.authentication; 2 3 import org.enhydra.shark.api.internal.working.CallbackUtilities; 4 5 import java.util.*; 6 7 import javax.naming.*; 8 import javax.naming.directory.*; 9 10 16 public class LDAPClient { 17 private final String boundary=","; 18 19 private LDAPOptions ldapOptions; 20 21 private CallbackUtilities cus; 22 23 private java.util.Properties env = new java.util.Properties (); 24 25 private SearchControls constraints=new SearchControls(); 26 27 30 public LDAPClient (CallbackUtilities cus) { 31 this.cus=cus; 32 ldapOptions=new LDAPOptions(cus); 33 37 env = new java.util.Properties (); 38 env.put(javax.naming.Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); 39 env.put(javax.naming.Context.REFERRAL, "throw"); 40 env.put(javax.naming.Context.PROVIDER_URL, "ldap://" + ldapOptions.getHost() + ":" + ldapOptions.getPort()); 41 env.put(javax.naming.Context.SECURITY_PRINCIPAL, ldapOptions.getUser()); 42 env.put(javax.naming.Context.SECURITY_CREDENTIALS, ldapOptions.getPassword()); 43 44 constraints.setSearchScope(ldapOptions.getSearchScope()); 46 constraints.setCountLimit(0); 47 } 48 49 55 public LDAPOptions getLDAPOptions () { 56 return ldapOptions; 57 } 58 59 67 public boolean checkPassword (String username,String pwd) { 68 try { 70 String base = ldapOptions.getSearchBase(); 71 String filter=""; 72 String [] objClasses; 73 Iterator results; 74 SearchResult sr; 75 String dn = null; 76 int count = 0; 77 78 if (ldapOptions.getStructureType() == 1) { 79 objClasses=tokenize(ldapOptions.getGroupObjectClasses(),boundary); 80 if (objClasses!=null && objClasses.length>0) { 81 if (objClasses.length>1) { 82 filter+="(|"; 83 } 84 for (int i=0; i<objClasses.length; i++) { 85 filter+="(objectClass="+objClasses[i]+")"; 86 } 87 if (objClasses.length>1) { 88 filter+=")"; 89 } 90 } 91 filter="(&("+ldapOptions.getGroupUniqueAttributeName()+"=" + ldapOptions.getGroupUsersName()+ ")"+filter+")"; 92 results=getEntries(filter).iterator(); 93 if (results.hasNext()) { 94 sr = (SearchResult) results.next(); 95 try { 97 dn = sr.getName(); 98 } 99 catch (Exception e){} 100 if (!dn.equals("")){ 101 if (!base.equals("")){ 102 dn = dn + "," + base; 103 } 104 } 105 else { 106 dn = base; 107 } 108 ldapOptions.setSearchBase(dn); 109 110 filter=""; 111 objClasses=tokenize(ldapOptions.getUserObjectClasses(),boundary); 112 if (objClasses!=null && objClasses.length>0) { 113 if (objClasses.length>1) { 114 filter+="(|"; 115 } 116 for (int i=0; i<objClasses.length; i++) { 117 filter+="(objectClass="+objClasses[i]+")"; 118 } 119 if (objClasses.length>1) { 120 filter+=")"; 121 } 122 } 123 filter="(&("+ldapOptions.getUserUniqueAttributeName()+"="+username+")"+ 124 "("+ldapOptions.getUserPasswordAttributeName()+"="+pwd+")"+ 125 filter+")"; 126 count = getEntries(filter).size(); 127 128 ldapOptions.setSearchBase(base); 129 } 130 } else { 132 objClasses=tokenize(ldapOptions.getUserObjectClasses(),boundary); 133 if (objClasses!=null && objClasses.length>0) { 134 if (objClasses.length>1) { 135 filter+="(|"; 136 } 137 for (int i=0; i<objClasses.length; i++) { 138 filter+="(objectClass="+objClasses[i]+")"; 139 } 140 if (objClasses.length>1) { 141 filter+=")"; 142 } 143 } 144 filter="(&("+ldapOptions.getUserUniqueAttributeName()+"="+username+")"+ 145 "("+ldapOptions.getUserPasswordAttributeName()+"="+pwd+")"+ 146 filter+")"; 147 count = getEntries(filter).size(); 148 } 149 150 return count > 0; 151 152 } catch (NamingException e) { 153 154 cus.warn("LDAPClient -> Search failed: "+e.getMessage()); 155 } 156 catch (Exception e) { 157 158 cus.warn("LDAPClient -> Non-naming error: " + e.getMessage()); 159 } 160 catch (Throwable e) { 161 162 cus.warn("LDAPClient -> Non-naming error: " + e.getMessage()); 163 } 164 return false; 165 } 166 167 172 private java.util.List getEntries (String filter) throws Throwable { 173 java.util.List entries=new java.util.ArrayList (); 174 DirContext ctx=new InitialDirContext(env); 175 try { 176 NamingEnumeration results = ctx.search( 178 ldapOptions.getSearchBase(), 179 filter, 180 constraints); 181 182 while (results.hasMore()) { 184 SearchResult sr = (SearchResult) results.next(); 186 entries.add(sr); 187 } 188 return entries; 190 } 191 catch (Exception e){ 192 e.printStackTrace(); 193 } 194 finally { 195 ctx.close(); 196 } 197 return entries; 198 } 199 200 210 static String [] tokenize(String input,String boundary) { 211 if (input==null) input=""; 212 Vector v = new Vector(); 213 StringTokenizer t = new StringTokenizer(input,boundary); 214 String cmd[]; 215 216 while (t.hasMoreTokens()) 217 v.addElement(t.nextToken()); 218 cmd = new String [v.size()]; 219 for (int i = 0; i < cmd.length; i++) 220 cmd[i] = (String )v.elementAt(i); 221 222 return cmd; 223 } 224 225 } 226 | Popular Tags |