1 21 22 package org.apache.derby.impl.jdbc.authentication; 23 24 import org.apache.derby.iapi.reference.MessageId; 25 import org.apache.derby.iapi.services.monitor.Monitor; 26 import org.apache.derby.iapi.error.StandardException; 27 import org.apache.derby.iapi.services.i18n.MessageService; 28 import org.apache.derby.iapi.jdbc.AuthenticationService; 29 30 import org.apache.derby.authentication.UserAuthenticator; 31 32 import org.apache.derby.iapi.services.sanity.SanityManager; 33 import org.apache.derby.iapi.util.StringUtil; 34 35 import javax.naming.*; 36 import javax.naming.directory.*; 37 38 39 import java.util.Properties ; 40 import java.sql.SQLException ; 41 42 69 70 public final class LDAPAuthenticationSchemeImpl 71 extends JNDIAuthenticationSchemeBase 72 { 73 private static final String dfltLDAPURL = "ldap://"; 74 75 private String searchBaseDN; 76 77 private String leftSearchFilter; private String rightSearchFilter; 79 private boolean useUserPropertyAsDN; 80 81 private String searchAuthDN; 83 private String searchAuthPW; 84 private static final String [] attrDN = {"dn"}; ; 86 87 private static final String LDAP_SEARCH_BASE = 91 "derby.authentication.ldap.searchBase"; 92 private static final String LDAP_SEARCH_FILTER = 93 "derby.authentication.ldap.searchFilter"; 94 private static final String LDAP_SEARCH_AUTH_DN = 95 "derby.authentication.ldap.searchAuthDN"; 96 private static final String LDAP_SEARCH_AUTH_PW = 97 "derby.authentication.ldap.searchAuthPW"; 98 private static final String LDAP_LOCAL_USER_DN = 99 "derby.user"; 100 private static final String LDAP_SEARCH_FILTER_USERNAME = 101 "%USERNAME%"; 102 103 public LDAPAuthenticationSchemeImpl(JNDIAuthenticationService as, Properties dbProperties) { 104 105 super(as, dbProperties); 106 } 107 108 119 public boolean authenticateUser(String userName, 120 String userPassword, 121 String databaseName, 122 Properties info 123 ) 124 throws java.sql.SQLException 125 { 126 if ( ((userName == null) || (userName.length() == 0)) || 127 ((userPassword == null) || (userPassword.length() == 0)) ) 128 { 129 return false; 136 } 137 138 139 Exception e; 140 try { 141 Properties env = (Properties ) initDirContextEnv.clone(); 142 String userDN = null; 143 if (useUserPropertyAsDN) 150 userDN = 151 authenticationService.getProperty( 152 org.apache.derby.iapi.reference.Property.USER_PROPERTY_PREFIX); 153 154 if (userDN == (String ) null) { 155 userDN = getDNFromUID(userName); 156 } 157 158 if (SanityManager.DEBUG) 159 { 160 if (SanityManager.DEBUG_ON( 161 AuthenticationServiceBase.AuthenticationTrace)) { 162 SanityManager.DEBUG(AuthenticationServiceBase.AuthenticationTrace, 163 "User DN = ["+ userDN+"]\n"); 164 } 165 } 166 167 env.put(Context.SECURITY_PRINCIPAL, userDN); 168 env.put(Context.SECURITY_CREDENTIALS, userPassword); 169 170 172 DirContext ctx = new InitialDirContext(env); 174 175 return true; 178 179 } catch (javax.naming.AuthenticationException jndiae) { 180 return false; 181 182 } catch (javax.naming.NameNotFoundException jndinnfe) { 183 return false; 184 185 } catch (javax.naming.NamingException jndine) { 186 e = jndine; 187 } 188 189 throw getLoginSQLException(e); 190 } 191 192 197 protected void setJNDIProviderProperties() 198 { 199 200 if (initDirContextEnv.getProperty( 202 Context.INITIAL_CONTEXT_FACTORY) == (String ) null) 203 { 204 initDirContextEnv.put(Context.INITIAL_CONTEXT_FACTORY, 205 "com.sun.jndi.ldap.LdapCtxFactory"); 206 } 207 208 if (initDirContextEnv.getProperty( 210 Context.PROVIDER_URL) == (String ) null) 211 { 212 String ldapServer = authenticationService.getProperty( 216 org.apache.derby.iapi.reference.Property.AUTHENTICATION_SERVER_PARAMETER); 217 218 if (ldapServer == (String ) null) { 219 220 Monitor.logTextMessage( 222 MessageId.AUTH_NO_LDAP_HOST_MENTIONED, 223 org.apache.derby.iapi.reference.Property.AUTHENTICATION_SERVER_PARAMETER); 224 225 this.providerURL = dfltLDAPURL + "/"; 226 227 } else { 228 229 if (ldapServer.startsWith(dfltLDAPURL) || ldapServer.startsWith("ldaps://") ) 230 this.providerURL = ldapServer; 231 else if (ldapServer.startsWith("//")) 232 this.providerURL = "ldap:" + ldapServer; 233 else 234 this.providerURL = dfltLDAPURL + ldapServer; 235 } 236 initDirContextEnv.put(Context.PROVIDER_URL, providerURL); 237 } 238 239 if (initDirContextEnv.getProperty( 243 Context.SECURITY_AUTHENTICATION) == (String ) null) 244 { 245 initDirContextEnv.put(Context.SECURITY_AUTHENTICATION, 253 "simple" 254 ); 255 } 256 257 String ldapSearchBase = 260 authenticationService.getProperty(LDAP_SEARCH_BASE); 261 if (ldapSearchBase != (String ) null) 262 this.searchBaseDN = ldapSearchBase; 263 else 264 this.searchBaseDN = ""; 265 266 this.searchAuthDN = 269 authenticationService.getProperty(LDAP_SEARCH_AUTH_DN); 270 this.searchAuthPW = 271 authenticationService.getProperty(LDAP_SEARCH_AUTH_PW); 272 273 String searchFilterProp = 295 authenticationService.getProperty(LDAP_SEARCH_FILTER); 296 297 if (searchFilterProp == (String ) null) 298 { 299 this.leftSearchFilter = "(&(objectClass=inetOrgPerson)(uid="; 301 this.rightSearchFilter = "))"; 302 303 } else if (StringUtil.SQLEqualsIgnoreCase(searchFilterProp,LDAP_LOCAL_USER_DN)) { 304 305 this.leftSearchFilter = "(&(objectClass=inetOrgPerson)(uid="; 307 this.rightSearchFilter = "))"; 308 this.useUserPropertyAsDN = true; 309 310 } else if (searchFilterProp.indexOf( 311 LDAP_SEARCH_FILTER_USERNAME) != -1) { 312 313 this.leftSearchFilter = searchFilterProp.substring(0, 315 searchFilterProp.indexOf(LDAP_SEARCH_FILTER_USERNAME)); 316 this.rightSearchFilter = searchFilterProp.substring( 317 searchFilterProp.indexOf(LDAP_SEARCH_FILTER_USERNAME)+ 318 (int) LDAP_SEARCH_FILTER_USERNAME.length()); 319 320 321 } else { 323 this.leftSearchFilter = "(&("+searchFilterProp+")"+ 325 "(objectClass=inetOrgPerson)(uid="; 326 this.rightSearchFilter = "))"; 327 328 } 329 330 if (SanityManager.DEBUG) 331 { 332 if (SanityManager.DEBUG_ON( 333 AuthenticationServiceBase.AuthenticationTrace)) { 334 335 java.io.PrintWriter iDbgStream = 336 SanityManager.GET_DEBUG_STREAM(); 337 338 iDbgStream.println( 339 "\n\n+ LDAP Authentication Configuration:\n"+ 340 " - provider URL ["+this.providerURL+"]\n"+ 341 " - search base ["+this.searchBaseDN+"]\n"+ 342 " - search filter to be [" + 343 this.leftSearchFilter + "<uid>" + 344 this.rightSearchFilter + "]\n" + 345 " - use local DN [" + 346 (useUserPropertyAsDN ? "true" : "false") + 347 "]\n" 348 ); 349 } 350 } 351 352 if (SanityManager.DEBUG) 353 { 354 if (SanityManager.DEBUG_ON( 355 AuthenticationServiceBase.AuthenticationTrace)) { 356 try { 357 initDirContextEnv.put("com.sun.naming.ldap.trace.ber", 358 new java.io.FileOutputStream ("CloudLDAP.out")); 359 } catch (java.io.IOException ie) {} 360 } 361 } 362 } 363 364 374 private String getDNFromUID(String uid) 375 throws javax.naming.NamingException 376 { 377 Properties env = null; 384 if (this.searchAuthDN != (String ) null) { 385 env = (Properties ) initDirContextEnv.clone(); 386 env.put(Context.SECURITY_PRINCIPAL, this.searchAuthDN); 387 env.put(Context.SECURITY_CREDENTIALS, this.searchAuthPW); 388 } 389 else 390 env = initDirContextEnv; 391 392 DirContext ctx = new InitialDirContext(env); 393 394 SearchControls ctls = new SearchControls(); 396 ctls.setSearchScope(SearchControls.SUBTREE_SCOPE); 398 399 ctls.setReturningAttributes(attrDN); 401 402 String searchFilter = 403 this.leftSearchFilter + uid + this.rightSearchFilter; 404 NamingEnumeration results = 405 ctx.search(searchBaseDN, searchFilter, ctls); 406 407 if (results == null || !results.hasMore()) 409 throw new NameNotFoundException(); 410 411 SearchResult result = (SearchResult)results.next(); 412 413 if (results.hasMore()) 414 { 415 if (SanityManager.DEBUG) 418 { 419 if (SanityManager.DEBUG_ON( 420 AuthenticationServiceBase.AuthenticationTrace)) { 421 422 java.io.PrintWriter iDbgStream = 423 SanityManager.GET_DEBUG_STREAM(); 424 425 iDbgStream.println( 426 " - LDAP Authentication request failure: "+ 427 "search filter [" + searchFilter + "]"+ 428 ", retrieve more than one occurence in "+ 429 "LDAP server [" + this.providerURL + "]"); 430 } 431 } 432 throw new NameNotFoundException(); 433 } 434 435 NameParser parser = ctx.getNameParser(searchBaseDN); 436 Name userDN = parser.parse(searchBaseDN); 437 438 if (userDN == (Name) null) 439 throw new NameNotFoundException(); 441 else 442 userDN.addAll(parser.parse(result.getName())); 443 444 return userDN.toString(); 446 } 447 } 448 | Popular Tags |