public NtlmHipergateFilter() { }
public void doFilter( ServletRequest request,ServletResponse response, FilterChain chain )
throws IOException, ServletException {
NtlmPasswordAuthentication ntlm = null;
HttpServletRequest req = (HttpServletRequest)request;
HttpServletResponse resp = (HttpServletResponse)response;
String msg = req.getHeader( "Authorization" );
if (DebugFile.trace) DebugFile.writeln("NtlmHipergateFilter Authorization=" + msg);
UniAddress dc;
String user = "", password = "", domain = "";
boolean offerBasic = enableBasic && (insecureBasic || req.isSecure());
if (DebugFile.trace) DebugFile.writeln("offerBasic=" + String.valueOf(offerBasic));
if( msg != null && (msg.startsWith( "NTLM " ) || (offerBasic && msg.startsWith("Basic ")))) {
if( loadBalance ) {
if (DebugFile.trace) DebugFile.writeln("new UniAddress(" + NbtAddress.getByName( domainController, 0x1C, null ) + ")");
dc = new UniAddress( NbtAddress.getByName( domainController, 0x1C, null ));
} else {
if (DebugFile.trace) DebugFile.writeln("UniAddress.getByName( " + domainController + ", true)");
dc = UniAddress.getByName( domainController, true );
}
if (msg.startsWith("NTLM ")) {
req.getSession();
byte[] challenge = SmbSession.getChallenge( dc );
if (( ntlm = NtlmSsp.authenticate( req, resp, challenge )) == null ) {
if (DebugFile.trace) DebugFile.writeln("NtlmPasswordAuthentication = null");
return;
}
} else {
String auth = new String (Base64Decoder.decodeToBytes(msg.substring(6)), "US-ASCII");
int index = auth.indexOf(':');
user = (index != -1) ? auth.substring(0, index) : auth;
if (DebugFile.trace) DebugFile.writeln("user=" + user);
password = (index != -1) ? auth.substring(index + 1) : "";
index = user.indexOf('\\');
if (index == -1) index = user.indexOf('/');
domain = (index != -1) ? user.substring(0, index) : defaultDomain;
if (DebugFile.trace) DebugFile.writeln("domain=" + domain);
user = (index != -1) ? user.substring(index + 1) : user;
ntlm = new NtlmPasswordAuthentication(domain, user, password);
} // fi (msg.startsWith("NTLM "))
try {
if (DebugFile.trace && (dc!=null) && (ntlm!=null))
DebugFile.writeln("SmbSession.logon(" + dc.toString() + "," + ntlm.toString());
SmbSession.logon( dc, ntlm );
} catch( SmbAuthException sae ) {
if (DebugFile.trace) DebugFile.writeln("SmbAuthException" + Gadgets.toHexString(sae.getNtStatus(), 8) + " " + sae.getMessage());
if( sae.getNtStatus() == sae.NT_STATUS_ACCESS_VIOLATION ) {
/* Server challenge no longer valid for
* externally supplied password hashes.
*/
HttpSession ssn = req.getSession(false);
if (ssn != null) {
ssn.removeAttribute( "NtlmHttpAuth" );
}
if (DebugFile.trace) DebugFile.writeln("HttpServletResponse.sendRedirect(" + req.getRequestURL().toString() + ")");
resp.sendRedirect( req.getRequestURL().toString() );
return;
}
if (DebugFile.trace) DebugFile.writeln("HttpServletResponse.setHeader(WWW-Authenticate, NTLM)");
resp.setHeader( "WWW-Authenticate", "NTLM" );
if (offerBasic) {
resp.addHeader( "WWW-Authenticate", "Basic realm=\"" + realm + "\"");
}
resp.setHeader( "Connection", "close" );
resp.setStatus( HttpServletResponse.SC_UNAUTHORIZED );
resp.flushBuffer();
return;
}
if (DebugFile.trace) DebugFile.writeln("HttpServletRequest.getSession().setAttribute(NtlmHttpAuth, " + ntlm.toString() + ")");
req.getSession().setAttribute( "NtlmHttpAuth", ntlm );
if (DebugFile.trace) DebugFile.writeln("HttpServletResponse.addCookie(domainnm, " + ntlm.getDomain().toUpperCase() + ")");
if (DebugFile.trace) DebugFile.writeln("HttpServletResponse.addCookie(nickname, " + ntlm.getUsername() + ")");
resp.addCookie(new Cookie("domainnm", ntlm.getDomain().toUpperCase()));
resp.addCookie(new Cookie("NickCookie", ntlm.getUsername()));
resp.addCookie(new Cookie("authstr", ntlm.getPassword()));
} else {
if (DebugFile.trace) DebugFile.writeln("HttpSession = HttpServletRequest.getSession(false)");
HttpSession ssn = req.getSession(false);