private String includePattern = null;
protected final boolean doAuthentication(final Credentials credentials)
throws AuthenticationException {
final SpnegoCredentials ntlmCredentials = (SpnegoCredentials) credentials;
final byte[] src = ntlmCredentials.getInitToken();
UniAddress dc = null;
try {
if (this.loadBalance) {
// find the first dc that matches the includepattern
if(this.includePattern != null){
NbtAddress [] dcs = NbtAddress.getAllByName(this.domainController,0x1C, null,null);
for(int i=0;i<dcs.length;i++){
if(dcs[i].getHostAddress().matches(this.includePattern)){
dc = new UniAddress(dcs[i]);
break;
}
}
}
else
dc = new UniAddress(NbtAddress.getByName(this.domainController,
0x1C, null));
} else {
dc = UniAddress.getByName(this.domainController, true);
}
final byte[] challenge = SmbSession.getChallenge(dc);
switch (src[8]) {
case 1:
log.debug("Type 1 received");
final Type1Message type1 = new Type1Message(src);
final Type2Message type2 = new Type2Message(type1,
challenge, null);
log.debug("Type 2 returned. Setting next token.");
ntlmCredentials.setNextToken(type2.toByteArray());
return false;
case 3:
log.debug("Type 3 received");
final Type3Message type3 = new Type3Message(src);
final byte[] lmResponse = type3.getLMResponse() == null
? new byte[0] : type3.getLMResponse();
byte[] ntResponse = type3.getNTResponse() == null
? new byte[0] : type3.getNTResponse();
final NtlmPasswordAuthentication ntlm = new NtlmPasswordAuthentication(
type3.getDomain(), type3.getUser(), challenge,
lmResponse, ntResponse);
log.debug("Trying to authenticate " + type3.getUser()
+ " with domain controller");
try {
SmbSession.logon(dc, ntlm);
ntlmCredentials.setPrincipal(new SimplePrincipal(type3
.getUser()));
return true;
} catch (final SmbAuthException sae) {
log.debug("Authentication failed", sae);
return false;