1 21 22 package org.apache.derby.impl.jdbc.authentication; 23 24 import org.apache.derby.authentication.UserAuthenticator; 25 import org.apache.derby.iapi.reference.Property; 26 import org.apache.derby.iapi.jdbc.AuthenticationService; 27 28 import org.apache.derby.iapi.reference.Limits; 29 30 import org.apache.derby.iapi.error.StandardException; 31 import org.apache.derby.iapi.services.i18n.MessageService; 32 33 import org.apache.derby.iapi.services.context.ContextService; 34 import org.apache.derby.iapi.services.daemon.Serviceable; 35 36 import org.apache.derby.iapi.services.monitor.ModuleSupportable; 37 import org.apache.derby.iapi.services.monitor.ModuleControl; 38 import org.apache.derby.iapi.services.monitor.Monitor; 39 import org.apache.derby.iapi.store.access.AccessFactory; 40 import org.apache.derby.iapi.services.property.PropertyFactory; 41 import org.apache.derby.iapi.store.access.TransactionController; 42 import org.apache.derby.iapi.services.property.PropertySetCallback; 43 44 import org.apache.derby.iapi.services.sanity.SanityManager; 45 46 import org.apache.derby.iapi.reference.Attribute; 47 48 import org.apache.derby.iapi.services.property.PropertyUtil; 49 import org.apache.derby.iapi.util.StringUtil; 50 51 import java.security.MessageDigest ; 52 import java.security.NoSuchAlgorithmException ; 53 54 import java.io.Serializable ; 55 import java.util.Dictionary ; 56 import java.util.Properties ; 57 import java.util.Date ; 58 59 79 public abstract class AuthenticationServiceBase 80 implements AuthenticationService, ModuleControl, ModuleSupportable, PropertySetCallback { 81 82 protected UserAuthenticator authenticationScheme; 83 84 private AccessFactory store; 86 87 90 public static final String AuthenticationTrace = 91 SanityManager.DEBUG ? "AuthenticationTrace" : null; 92 95 public static final String ID_PATTERN_NEW_SCHEME = "3b60"; 96 97 100 protected static final int SECMEC_USRSSBPWD = 8; 101 102 106 public static final int MAGICLEN_NEWENCRYPT_SCHEME=44; 107 108 public AuthenticationServiceBase() { 112 } 113 114 protected void setAuthenticationService(UserAuthenticator aScheme) { 115 this.authenticationScheme = aScheme; 117 118 if (SanityManager.DEBUG) 119 { 120 SanityManager.ASSERT(this.authenticationScheme != null, 121 "There is no authentication scheme for that service!"); 122 123 if (SanityManager.DEBUG_ON(AuthenticationTrace)) { 124 125 java.io.PrintWriter iDbgStream = 126 SanityManager.GET_DEBUG_STREAM(); 127 128 iDbgStream.println("Authentication Service: [" + 129 this.toString() + "]"); 130 iDbgStream.println("Authentication Scheme : [" + 131 this.authenticationScheme.toString() + "]"); 132 } 133 } 134 } 135 136 140 141 148 public void boot(boolean create, Properties properties) 149 throws StandardException 150 { 151 store = (AccessFactory) 156 Monitor.getServiceModule(this, AccessFactory.MODULE); 157 160 PropertyFactory pf = (PropertyFactory) 161 Monitor.getServiceModule(this, org.apache.derby.iapi.reference.Module.PropertyFactory); 162 if (pf != null) 163 pf.addPropertySetNotification(this); 164 165 } 166 167 170 public void stop() { 171 172 } 174 177 178 190 public boolean authenticate(String databaseName, Properties userInfo) throws java.sql.SQLException 191 { 192 if (userInfo == (Properties ) null) 193 return false; 194 195 String userName = userInfo.getProperty(Attribute.USERNAME_ATTR); 196 if ((userName != null) && userName.length() > Limits.DB2_MAX_USERID_LENGTH) { 197 return false; 203 } 204 205 if (SanityManager.DEBUG) 206 { 207 if (SanityManager.DEBUG_ON(AuthenticationTrace)) { 208 209 java.io.PrintWriter iDbgStream = 210 SanityManager.GET_DEBUG_STREAM(); 211 212 iDbgStream.println( 213 " - Authentication request: user [" + 214 userName + "]"+ ", database [" + 215 databaseName + "]"); 216 } 222 } 223 return this.authenticationScheme.authenticateUser(userName, 224 userInfo.getProperty(Attribute.PASSWORD_ATTR), 225 databaseName, 226 userInfo 227 ); 228 } 229 230 236 public String getProperty(String key) { 237 238 String propertyValue = null; 239 TransactionController tc = null; 240 241 try { 242 243 if (store != null) 244 { 245 tc = store.getTransaction( 246 ContextService.getFactory().getCurrentContextManager()); 247 } 248 249 propertyValue = 250 PropertyUtil.getServiceProperty(tc, 251 key, 252 (String ) null); 253 if (tc != null) { 254 tc.commit(); 255 tc = null; 256 } 257 258 } catch (StandardException se) { 259 } 261 262 return propertyValue; 263 } 264 265 public String getDatabaseProperty(String key) { 266 267 String propertyValue = null; 268 TransactionController tc = null; 269 270 try { 271 272 if (store != null) 273 tc = store.getTransaction( 274 ContextService.getFactory().getCurrentContextManager()); 275 276 propertyValue = 277 PropertyUtil.getDatabaseProperty(tc, key); 278 279 if (tc != null) { 280 tc.commit(); 281 tc = null; 282 } 283 284 } catch (StandardException se) { 285 } 287 288 return propertyValue; 289 } 290 291 public String getSystemProperty(String key) { 292 293 boolean dbOnly = false; 294 dbOnly = Boolean.valueOf( 295 this.getDatabaseProperty( 296 Property.DATABASE_PROPERTIES_ONLY)).booleanValue(); 297 298 if (dbOnly) 299 return null; 300 301 return PropertyUtil.getSystemProperty(key); 302 } 303 304 307 public void init(boolean dbOnly, Dictionary p) { 308 } 310 311 314 public boolean validate(String key, Serializable value, Dictionary p) { 315 return key.startsWith(org.apache.derby.iapi.reference.Property.USER_PROPERTY_PREFIX); 316 } 317 320 public Serviceable apply(String key,Serializable value,Dictionary p) 321 { 322 return null; 323 } 324 328 public Serializable map(String key, Serializable value, Dictionary p) 329 throws StandardException 330 { 331 if (!key.startsWith(org.apache.derby.iapi.reference.Property.USER_PROPERTY_PREFIX)) return null; 334 340 String authService = 341 (String )p.get(org.apache.derby.iapi.reference.Property.AUTHENTICATION_PROVIDER_PARAMETER); 342 343 if ((authService != null) && 344 (StringUtil.SQLEqualsIgnoreCase(authService, org.apache.derby.iapi.reference.Property.AUTHENTICATION_PROVIDER_LDAP))) 345 return null; 346 347 String userPassword = (String ) value; 349 350 if (userPassword != null) { 351 userPassword = encryptPassword(userPassword); 354 } 355 356 return userPassword; 357 } 358 359 360 362 protected final boolean requireAuthentication(Properties properties) { 363 364 String requireAuthentication = PropertyUtil.getPropertyFromSet( 370 properties, 371 org.apache.derby.iapi.reference.Property.REQUIRE_AUTHENTICATION_PARAMETER 372 ); 373 return Boolean.valueOf(requireAuthentication).booleanValue(); 374 } 375 376 387 protected String encryptPassword(String plainTxtUserPassword) 388 { 389 if (plainTxtUserPassword == null) 390 return null; 391 392 MessageDigest algorithm = null; 393 try 394 { 395 algorithm = MessageDigest.getInstance("SHA-1"); 396 } catch (NoSuchAlgorithmException nsae) 397 { 398 } 400 401 algorithm.reset(); 402 byte[] bytePasswd = null; 403 bytePasswd = StringUtil.toHexByte( 404 plainTxtUserPassword,0,plainTxtUserPassword.length()); 405 algorithm.update(bytePasswd); 406 byte[] encryptVal = algorithm.digest(); 407 String hexString = ID_PATTERN_NEW_SCHEME + 408 StringUtil.toHexString(encryptVal,0,encryptVal.length); 409 return (hexString); 410 411 } 412 413 447 protected String substitutePassword( 448 String userName, 449 String password, 450 Properties info, 451 boolean databaseUser) { 452 453 MessageDigest messageDigest = null; 454 455 String ID_PATTERN_NEW_SCHEME = "3b60"; 457 458 byte SECMEC_USRSSBPWD_PWDSEQS[] = { 460 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, 461 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x01 462 }; 463 464 byte[] passwordSubstitute; 466 467 try 468 { 469 messageDigest = MessageDigest.getInstance("SHA-1"); 470 } catch (NoSuchAlgorithmException nsae) 471 { 472 } 474 messageDigest.reset(); 483 484 byte[] bytePasswd = null; 485 byte[] userBytes = StringUtil.toHexByte(userName, 0, userName.length()); 486 487 if (SanityManager.DEBUG) 488 { 489 SanityManager.ASSERT( 491 (((String ) info.getProperty(Attribute.DRDA_SECTKN_IN) != null) && 492 ((String ) info.getProperty(Attribute.DRDA_SECTKN_OUT) != null)), 493 "Unexpected: Requester or server seed not available"); 494 } 495 496 String sourceSeedstr = info.getProperty(Attribute.DRDA_SECTKN_IN); 498 String targetSeedstr = info.getProperty(Attribute.DRDA_SECTKN_OUT); 499 500 byte[] sourceSeed_ = 501 StringUtil.fromHexString(sourceSeedstr, 0, sourceSeedstr.length()); 502 byte[] targetSeed_ = 503 StringUtil.fromHexString(targetSeedstr, 0, targetSeedstr.length()); 504 505 String hexString = null; 506 if (!databaseUser) 512 { 513 bytePasswd = StringUtil.toHexByte(password, 0, password.length()); 514 messageDigest.update(bytePasswd); 515 byte[] encryptVal = messageDigest.digest(); 516 hexString = ID_PATTERN_NEW_SCHEME + 517 StringUtil.toHexString(encryptVal, 0, encryptVal.length); 518 } 519 else 520 hexString = password; 522 523 525 messageDigest.update(userBytes); 527 messageDigest.update( 528 StringUtil.toHexByte(hexString, 0, hexString.length())); 529 byte[] passwordToken = messageDigest.digest(); 530 531 messageDigest.update(passwordToken); 533 messageDigest.update(targetSeed_); 534 messageDigest.update(sourceSeed_); 535 messageDigest.update(userBytes); 536 messageDigest.update(SECMEC_USRSSBPWD_PWDSEQS); 537 538 passwordSubstitute = messageDigest.digest(); 539 540 return StringUtil.toHexString(passwordSubstitute, 0, 541 passwordSubstitute.length); 542 } 543 } 544 | Popular Tags |