1 17 package org.alfresco.filesys.server.auth; 18 19 import java.security.NoSuchAlgorithmException ; 20 21 import org.alfresco.filesys.server.SrvSession; 22 import org.alfresco.filesys.smb.server.SMBSrvSession; 23 import org.alfresco.filesys.util.DataPacker; 24 import org.alfresco.repo.security.authentication.NTLMMode; 25 26 37 public class AlfrescoAuthenticator extends SrvAuthenticator 38 { 39 44 public AlfrescoAuthenticator() 45 { 46 setAccessMode(SrvAuthenticator.USER_MODE); 47 setEncryptedPasswords(true); 48 } 49 50 55 protected boolean validateAuthenticationMode() 56 { 57 59 if ( m_authComponent.getNTLMMode() != NTLMMode.MD4_PROVIDER && 60 m_authComponent.getNTLMMode() != NTLMMode.PASS_THROUGH) 61 return false; 62 return true; 63 } 64 65 72 public int authenticateUser(ClientInfo client, SrvSession sess, int alg) 73 { 74 78 if (client.isNullSession() && sess instanceof SMBSrvSession) 79 { 80 82 if ( logger.isDebugEnabled()) 83 logger.debug("Null CIFS logon allowed"); 84 85 return SrvAuthenticator.AUTH_ALLOW; 86 } 87 88 90 if ( client.getAuthenticationToken() != null && client.getLogonType() != ClientInfo.LogonNull) 91 { 92 94 if ( client.isGuest()) 95 m_authComponent.setGuestUserAsCurrentUser(); 96 else 97 m_authComponent.setCurrentUser(client.getUserName()); 98 99 101 if ( logger.isDebugEnabled()) 102 logger.debug("Re-using existing authentication token"); 103 104 106 return client.getLogonType() != ClientInfo.LogonGuest ? AUTH_ALLOW : AUTH_GUEST; 107 } 108 109 111 int authSts = AUTH_DISALLOW; 112 113 if ( client.isGuest() || client.getUserName().equalsIgnoreCase(GUEST_USERNAME)) 114 { 115 117 if ( allowGuest() == false) 118 return AUTH_DISALLOW; 119 120 122 doGuestLogon( client, sess); 123 124 126 authSts = AUTH_GUEST; 127 128 130 if ( logger.isDebugEnabled()) 131 logger.debug("Authenticated user " + client.getUserName() + " sts=" + getStatusAsString(authSts)); 132 133 135 return authSts; 136 } 137 138 140 else if ( m_authComponent.getNTLMMode() == NTLMMode.MD4_PROVIDER) 141 { 142 144 authSts = doMD4UserAuthentication(client, sess, alg); 145 } 146 147 149 if ( authSts == AUTH_GUEST) 150 { 151 153 if ( mapUnknownUserToGuest()) 154 { 155 157 doGuestLogon( client, sess); 158 } 159 else 160 { 161 163 authSts = AUTH_DISALLOW; 164 } 165 } 166 167 169 if ( logger.isDebugEnabled()) 170 logger.debug("Authenticated user " + client.getUserName() + " sts=" + getStatusAsString(authSts) + 171 " via " + (m_authComponent.getNTLMMode() == NTLMMode.MD4_PROVIDER ? "MD4" : "Passthru")); 172 173 175 return authSts; 176 } 177 178 184 public byte[] getChallengeKey(SrvSession sess) 185 { 186 188 byte[] key = null; 189 190 192 if ( sess.hasClientInformation() && sess.getClientInformation().getAuthenticationToken() != null && 193 sess.getClientInformation().getLogonType() != ClientInfo.LogonNull) 194 { 195 197 key = sess.getChallengeKey(); 198 199 201 if ( logger.isDebugEnabled()) 202 logger.debug("Re-using existing challenge, already authenticated"); 203 } 204 else if ( m_authComponent.getNTLMMode() == NTLMMode.MD4_PROVIDER) 205 { 206 208 key = new byte[8]; 209 210 DataPacker.putIntelLong(m_random.nextLong(), key, 0); 211 } 212 213 215 return key; 216 } 217 218 226 private final int doMD4UserAuthentication(ClientInfo client, SrvSession sess, int alg) 227 { 228 230 String md4hash = m_authComponent.getMD4HashedPassword(client.getUserName()); 231 232 if ( md4hash != null) 233 { 234 236 if ( client.getPassword() == null) 237 return SrvAuthenticator.AUTH_BADPASSWORD; 238 239 try 240 { 241 243 byte[] p21 = new byte[21]; 244 byte[] md4byts = m_md4Encoder.decodeHash(md4hash); 245 System.arraycopy(md4byts, 0, p21, 0, 16); 246 247 249 byte[] localHash = getEncryptor().doNTLM1Encryption(p21, sess.getChallengeKey()); 250 251 253 byte[] clientHash = client.getPassword(); 254 255 if ( clientHash == null || clientHash.length != localHash.length) 256 return SrvAuthenticator.AUTH_BADPASSWORD; 257 258 for ( int i = 0; i < clientHash.length; i++) 259 { 260 if ( clientHash[i] != localHash[i]) 261 return SrvAuthenticator.AUTH_BADPASSWORD; 262 } 263 264 266 client.setAuthenticationToken( m_authComponent.setCurrentUser(client.getUserName())); 267 268 270 getHomeFolderForUser( client); 271 272 274 return SrvAuthenticator.AUTH_ALLOW; 275 } 276 catch (NoSuchAlgorithmException ex) 277 { 278 } 279 280 282 return SrvAuthenticator.AUTH_DISALLOW; 283 } 284 285 289 if (client.isNullSession() && sess instanceof SMBSrvSession) 290 return SrvAuthenticator.AUTH_ALLOW; 291 292 294 return allowGuest() ? SrvAuthenticator.AUTH_GUEST : SrvAuthenticator.AUTH_DISALLOW; 295 } 296 } | Popular Tags |