1 17 package org.alfresco.filesys.server.auth.ntlm; 18 19 import java.security.NoSuchAlgorithmException ; 20 import net.sf.acegisecurity.Authentication; 21 22 import org.alfresco.filesys.server.SrvSession; 23 import org.alfresco.filesys.server.auth.ClientInfo; 24 import org.alfresco.filesys.server.auth.SrvAuthenticator; 25 import org.alfresco.filesys.smb.server.SMBSrvSession; 26 import org.alfresco.filesys.util.DataPacker; 27 import org.alfresco.repo.security.authentication.NTLMMode; 28 import org.alfresco.repo.security.authentication.ntlm.NTLMPassthruToken; 29 30 41 public class AlfrescoAuthenticator extends SrvAuthenticator 42 { 43 48 public AlfrescoAuthenticator() 49 { 50 setAccessMode(SrvAuthenticator.USER_MODE); 51 setEncryptedPasswords(true); 52 } 53 54 59 protected boolean validateAuthenticationMode() 60 { 61 63 if ( m_authComponent.getNTLMMode() != NTLMMode.MD4_PROVIDER && 64 m_authComponent.getNTLMMode() != NTLMMode.PASS_THROUGH) 65 return false; 66 return true; 67 } 68 69 76 public int authenticateUser(ClientInfo client, SrvSession sess, int alg) 77 { 78 82 if (client.isNullSession() && sess instanceof SMBSrvSession) 83 { 84 86 if ( logger.isDebugEnabled()) 87 logger.debug("Null CIFS logon allowed"); 88 89 return SrvAuthenticator.AUTH_ALLOW; 90 } 91 92 94 if ( client.getAuthenticationToken() != null && client.getLogonType() != ClientInfo.LogonNull) 95 { 96 98 m_authComponent.setCurrentUser(client.getUserName()); 99 100 102 if ( logger.isDebugEnabled()) 103 logger.debug("Re-using existing authentication token"); 104 105 107 return client.getLogonType() != ClientInfo.LogonGuest ? AUTH_ALLOW : AUTH_GUEST; 108 } 109 110 112 int authSts = AUTH_DISALLOW; 113 114 if ( client.isGuest() || client.getUserName().equalsIgnoreCase(getGuestUserName())) 115 { 116 118 if ( allowGuest() == false) 119 return AUTH_DISALLOW; 120 121 123 doGuestLogon( client, sess); 124 125 127 authSts = AUTH_GUEST; 128 129 131 if ( logger.isDebugEnabled()) 132 logger.debug("Authenticated user " + client.getUserName() + " sts=" + getStatusAsString(authSts)); 133 134 136 return authSts; 137 } 138 139 141 else if ( m_authComponent.getNTLMMode() == NTLMMode.MD4_PROVIDER) 142 { 143 145 authSts = doMD4UserAuthentication(client, sess, alg); 146 } 147 else 148 { 149 151 authSts = doPassthruUserAuthentication(client, sess, alg); 152 } 153 154 156 if ( authSts == AUTH_GUEST) 157 { 158 160 if ( mapUnknownUserToGuest()) 161 { 162 164 doGuestLogon( client, sess); 165 } 166 else 167 { 168 170 authSts = AUTH_DISALLOW; 171 } 172 } 173 174 176 if ( logger.isDebugEnabled()) 177 logger.debug("Authenticated user " + client.getUserName() + " sts=" + getStatusAsString(authSts) + 178 " via " + (m_authComponent.getNTLMMode() == NTLMMode.MD4_PROVIDER ? "MD4" : "Passthru")); 179 180 182 return authSts; 183 } 184 185 191 public byte[] getChallengeKey(SrvSession sess) 192 { 193 195 byte[] key = null; 196 197 199 if ( sess.hasClientInformation() && sess.getClientInformation().getAuthenticationToken() != null && 200 sess.getClientInformation().getLogonType() != ClientInfo.LogonNull) 201 { 202 204 key = sess.getChallengeKey(); 205 206 208 if ( logger.isDebugEnabled()) 209 logger.debug("Re-using existing challenge, already authenticated"); 210 } 211 else if ( m_authComponent.getNTLMMode() == NTLMMode.MD4_PROVIDER) 212 { 213 215 key = new byte[8]; 216 217 DataPacker.putIntelLong(m_random.nextLong(), key, 0); 218 } 219 else 220 { 221 223 NTLMPassthruToken authToken = new NTLMPassthruToken(); 224 225 227 m_authComponent.authenticate( authToken); 228 229 231 sess.setAuthenticationToken(authToken); 232 233 235 if ( authToken.getChallenge() != null) 236 key = authToken.getChallenge().getBytes(); 237 } 238 239 241 return key; 242 } 243 244 252 private final int doMD4UserAuthentication(ClientInfo client, SrvSession sess, int alg) 253 { 254 256 String md4hash = m_authComponent.getMD4HashedPassword(client.getUserName()); 257 258 if ( md4hash != null) 259 { 260 262 if ( client.getPassword() == null) 263 return SrvAuthenticator.AUTH_BADPASSWORD; 264 265 try 266 { 267 269 byte[] p21 = new byte[21]; 270 byte[] md4byts = m_md4Encoder.decodeHash(md4hash); 271 System.arraycopy(md4byts, 0, p21, 0, 16); 272 273 275 byte[] localHash = getEncryptor().doNTLM1Encryption(p21, sess.getChallengeKey()); 276 277 279 byte[] clientHash = client.getPassword(); 280 281 if ( clientHash == null || clientHash.length != localHash.length) 282 return SrvAuthenticator.AUTH_BADPASSWORD; 283 284 for ( int i = 0; i < clientHash.length; i++) 285 { 286 if ( clientHash[i] != localHash[i]) 287 return SrvAuthenticator.AUTH_BADPASSWORD; 288 } 289 290 292 client.setAuthenticationToken( m_authComponent.setCurrentUser(client.getUserName())); 293 294 296 getHomeFolderForUser( client); 297 298 300 return SrvAuthenticator.AUTH_ALLOW; 301 } 302 catch (NoSuchAlgorithmException ex) 303 { 304 } 305 306 308 return SrvAuthenticator.AUTH_DISALLOW; 309 } 310 311 315 if (client.isNullSession() && sess instanceof SMBSrvSession) 316 return SrvAuthenticator.AUTH_ALLOW; 317 318 320 return allowGuest() ? SrvAuthenticator.AUTH_GUEST : SrvAuthenticator.AUTH_DISALLOW; 321 } 322 323 331 private final int doPassthruUserAuthentication(ClientInfo client, SrvSession sess, int alg) 332 { 333 335 NTLMPassthruToken authToken = (NTLMPassthruToken) sess.getAuthenticationToken(); 336 337 if ( authToken == null) 338 return SrvAuthenticator.AUTH_DISALLOW; 339 340 342 int authSts = SrvAuthenticator.AUTH_DISALLOW; 343 byte[] hashedPassword = null; 344 345 if ( alg == NTLM1) 346 hashedPassword = client.getPassword(); 347 else if ( alg == LANMAN) 348 hashedPassword = client.getANSIPassword(); 349 else 350 { 351 353 return SrvAuthenticator.AUTH_DISALLOW; 354 } 355 356 358 authToken.setUserAndPassword( client.getUserName(), hashedPassword, alg); 359 360 362 Authentication genAuthToken = null; 363 364 try 365 { 366 368 genAuthToken = m_authComponent.authenticate( authToken); 369 370 372 if (authToken.isGuestLogon()) 373 { 374 375 377 if (allowGuest() == true) 378 { 379 380 382 authSts = SrvAuthenticator.AUTH_GUEST; 383 } 384 } 385 else 386 { 387 388 390 authSts = SrvAuthenticator.AUTH_ALLOW; 391 } 392 393 395 client.setAuthenticationToken( genAuthToken); 396 397 399 getHomeFolderForUser( client); 400 401 403 if ( logger.isDebugEnabled()) 404 logger.debug("Auth token " + genAuthToken); 405 } 406 catch ( Exception ex) 407 { 408 logger.error("Error during passthru authentication", ex); 409 } 410 411 413 sess.setAuthenticationToken(null); 414 415 417 return authSts; 418 } 419 } | Popular Tags |