NegotiateSecurityFilter.java
/**
* Waffle (https://github.com/dblock/waffle)
*
* Copyright (c) 2010 - 2015 Application Security, Inc.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Application Security, Inc.
*/
package waffle.servlet;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.security.Principal;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import javax.security.auth.Subject;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import waffle.servlet.spi.SecurityFilterProvider;
import waffle.servlet.spi.SecurityFilterProviderCollection;
import waffle.util.AuthorizationHeader;
import waffle.windows.auth.IWindowsAuthProvider;
import waffle.windows.auth.IWindowsIdentity;
import waffle.windows.auth.IWindowsImpersonationContext;
import waffle.windows.auth.PrincipalFormat;
import waffle.windows.auth.impl.WindowsAuthProviderImpl;
/**
* A Negotiate (NTLM/Kerberos) Security Filter.
*
* @author dblock[at]dblock[dot]org
*/
public class NegotiateSecurityFilter implements Filter {
/** The Constant LOGGER. */
private static final Logger LOGGER = LoggerFactory
.getLogger(NegotiateSecurityFilter.class);
/** The principal format. */
private PrincipalFormat principalFormat = PrincipalFormat.FQN;
/** The role format. */
private PrincipalFormat roleFormat = PrincipalFormat.FQN;
/** The providers. */
private SecurityFilterProviderCollection providers;
/** The auth. */
private IWindowsAuthProvider auth;
/** The allow guest login. */
private boolean allowGuestLogin = true;
/** The impersonate. */
private boolean impersonate;
/** The Constant PRINCIPALSESSIONKEY. */
private static final String PRINCIPALSESSIONKEY = NegotiateSecurityFilter.class.getName()
+ ".PRINCIPAL";
/**
* Instantiates a new negotiate security filter.
*/
public NegotiateSecurityFilter() {
NegotiateSecurityFilter.LOGGER.debug("[waffle.servlet.NegotiateSecurityFilter] loaded");
}
/* (non-Javadoc)
* @see javax.servlet.Filter#destroy()
*/
@Override
public void destroy() {
NegotiateSecurityFilter.LOGGER.info("[waffle.servlet.NegotiateSecurityFilter] stopped");
}
/* (non-Javadoc)
* @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
*/
@Override
public void doFilter(final ServletRequest sreq, final ServletResponse sres, final FilterChain chain)
throws IOException, ServletException {
final HttpServletRequest request = (HttpServletRequest) sreq;
final HttpServletResponse response = (HttpServletResponse) sres;
NegotiateSecurityFilter.LOGGER.debug("{} {}, contentlength: {}", request.getMethod(), request.getRequestURI(),
Integer.valueOf(request.getContentLength()));
if (this.doFilterPrincipal(request, response, chain)) {
// previously authenticated user
return;
}
final AuthorizationHeader authorizationHeader = new AuthorizationHeader(request);
// authenticate user
if (!authorizationHeader.isNull()) {
// log the user in using the token
IWindowsIdentity windowsIdentity;
try {
windowsIdentity = this.providers.doFilter(request, response);
if (windowsIdentity == null) {
return;
}
} catch (final IOException e) {
NegotiateSecurityFilter.LOGGER.warn("error logging in user: {}", e.getMessage());
NegotiateSecurityFilter.LOGGER.trace("{}", e);
this.sendUnauthorized(response, true);
return;
}
IWindowsImpersonationContext ctx = null;
try {
if (!this.allowGuestLogin && windowsIdentity.isGuest()) {
NegotiateSecurityFilter.LOGGER.warn("guest login disabled: {}", windowsIdentity.getFqn());
this.sendUnauthorized(response, true);
return;
}
NegotiateSecurityFilter.LOGGER.debug("logged in user: {} ({})", windowsIdentity.getFqn(), windowsIdentity.getSidString());
final HttpSession session = request.getSession(true);
if (session == null) {
throw new ServletException("Expected HttpSession");
}
Subject subject = (Subject) session.getAttribute("javax.security.auth.subject");
if (subject == null) {
subject = new Subject();
}
WindowsPrincipal windowsPrincipal = null;
if (this.impersonate) {
windowsPrincipal = new AutoDisposableWindowsPrincipal(windowsIdentity, this.principalFormat,
this.roleFormat);
} else {
windowsPrincipal = new WindowsPrincipal(windowsIdentity, this.principalFormat, this.roleFormat);
}
NegotiateSecurityFilter.LOGGER.debug("roles: {}", windowsPrincipal.getRolesString());
subject.getPrincipals().add(windowsPrincipal);
session.setAttribute("javax.security.auth.subject", subject);
NegotiateSecurityFilter.LOGGER.info("successfully logged in user: {}", windowsIdentity.getFqn());
request.getSession().setAttribute(NegotiateSecurityFilter.PRINCIPALSESSIONKEY, windowsPrincipal);
final NegotiateRequestWrapper requestWrapper = new NegotiateRequestWrapper(request, windowsPrincipal);
if (this.impersonate) {
NegotiateSecurityFilter.LOGGER.debug("impersonating user");
ctx = windowsIdentity.impersonate();
}
chain.doFilter(requestWrapper, response);
} finally {
if (this.impersonate && ctx != null) {
NegotiateSecurityFilter.LOGGER.debug("terminating impersonation");
ctx.revertToSelf();
} else {
windowsIdentity.dispose();
}
}
return;
}
NegotiateSecurityFilter.LOGGER.debug("authorization required");
this.sendUnauthorized(response, false);
}
/**
* Filter for a previously logged on user.
*
* @param request
* HTTP request.
* @param response
* HTTP response.
* @param chain
* Filter chain.
* @return True if a user already authenticated.
* @throws IOException
* Signals that an I/O exception has occurred.
* @throws ServletException
* the servlet exception
*/
private boolean doFilterPrincipal(final HttpServletRequest request, final HttpServletResponse response,
final FilterChain chain) throws IOException, ServletException {
Principal principal = request.getUserPrincipal();
if (principal == null) {
final HttpSession session = request.getSession(false);
if (session != null) {
principal = (Principal) session.getAttribute(NegotiateSecurityFilter.PRINCIPALSESSIONKEY);
}
}
if (principal == null) {
// no principal in this request
return false;
}
if (this.providers.isPrincipalException(request)) {
// the providers signal to authenticate despite an existing principal, eg. NTLM post
return false;
}
// user already authenticated
if (principal instanceof WindowsPrincipal) {
NegotiateSecurityFilter.LOGGER.debug("previously authenticated Windows user: {}", principal.getName());
final WindowsPrincipal windowsPrincipal = (WindowsPrincipal) principal;
if (this.impersonate && windowsPrincipal.getIdentity() == null) {
// This can happen when the session has been serialized then de-serialized
// and because the IWindowsIdentity field is transient. In this case re-ask an
// authentication to get a new identity.
return false;
}
final NegotiateRequestWrapper requestWrapper = new NegotiateRequestWrapper(request, windowsPrincipal);
IWindowsImpersonationContext ctx = null;
if (this.impersonate) {
NegotiateSecurityFilter.LOGGER.debug("re-impersonating user");
ctx = windowsPrincipal.getIdentity().impersonate();
}
try {
chain.doFilter(requestWrapper, response);
} finally {
if (this.impersonate && ctx != null) {
NegotiateSecurityFilter.LOGGER.debug("terminating impersonation");
ctx.revertToSelf();
}
}
} else {
NegotiateSecurityFilter.LOGGER.debug("previously authenticated user: {}", principal.getName());
chain.doFilter(request, response);
}
return true;
}
/* (non-Javadoc)
* @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
*/
@SuppressWarnings("unchecked")
@Override
public void init(final FilterConfig filterConfig) throws ServletException {
final Map<String, String> implParameters = new HashMap<String, String>();
String authProvider = null;
String[] providerNames = null;
if (filterConfig != null) {
final Enumeration<String> parameterNames = filterConfig.getInitParameterNames();
while (parameterNames.hasMoreElements()) {
final String parameterName = parameterNames.nextElement();
final String parameterValue = filterConfig.getInitParameter(parameterName);
NegotiateSecurityFilter.LOGGER.debug("{}={}", parameterName, parameterValue);
if (parameterName.equals("principalFormat")) {
this.principalFormat = PrincipalFormat.valueOf(parameterValue.toUpperCase(Locale.ENGLISH));
} else if (parameterName.equals("roleFormat")) {
this.roleFormat = PrincipalFormat.valueOf(parameterValue.toUpperCase(Locale.ENGLISH));
} else if (parameterName.equals("allowGuestLogin")) {
this.allowGuestLogin = Boolean.parseBoolean(parameterValue);
} else if (parameterName.equals("impersonate")) {
this.impersonate = Boolean.parseBoolean(parameterValue);
} else if (parameterName.equals("securityFilterProviders")) {
providerNames = parameterValue.split("\\s+");
} else if (parameterName.equals("authProvider")) {
authProvider = parameterValue;
} else {
implParameters.put(parameterName, parameterValue);
}
}
}
if (authProvider != null) {
try {
this.auth = (IWindowsAuthProvider) Class.forName(authProvider).getConstructor().newInstance();
} catch (final ClassNotFoundException e) {
NegotiateSecurityFilter.LOGGER.error("error loading '{}': {}", authProvider, e.getMessage());
NegotiateSecurityFilter.LOGGER.trace("{}", e);
throw new ServletException(e);
} catch (final IllegalArgumentException e) {
NegotiateSecurityFilter.LOGGER.error("error loading '{}': {}", authProvider, e.getMessage());
NegotiateSecurityFilter.LOGGER.trace("{}", e);
throw new ServletException(e);
} catch (final SecurityException e) {
NegotiateSecurityFilter.LOGGER.error("error loading '{}': {}", authProvider, e.getMessage());
NegotiateSecurityFilter.LOGGER.trace("{}", e);
throw new ServletException(e);
} catch (final InstantiationException e) {
NegotiateSecurityFilter.LOGGER.error("error loading '{}': {}", authProvider, e.getMessage());
NegotiateSecurityFilter.LOGGER.trace("{}", e);
throw new ServletException(e);
} catch (final IllegalAccessException e) {
NegotiateSecurityFilter.LOGGER.error("error loading '{}': {}", authProvider, e.getMessage());
NegotiateSecurityFilter.LOGGER.trace("{}", e);
throw new ServletException(e);
} catch (final InvocationTargetException e) {
NegotiateSecurityFilter.LOGGER.error("error loading '{}': {}", authProvider, e.getMessage());
NegotiateSecurityFilter.LOGGER.trace("{}", e);
throw new ServletException(e);
} catch (final NoSuchMethodException e) {
NegotiateSecurityFilter.LOGGER.error("error loading '{}': {}", authProvider, e.getMessage());
NegotiateSecurityFilter.LOGGER.trace("{}", e);
throw new ServletException(e);
}
}
if (this.auth == null) {
this.auth = new WindowsAuthProviderImpl();
}
if (providerNames != null) {
this.providers = new SecurityFilterProviderCollection(providerNames, this.auth);
}
// create default providers if none specified
if (this.providers == null) {
NegotiateSecurityFilter.LOGGER.debug("initializing default security filter providers");
this.providers = new SecurityFilterProviderCollection(this.auth);
}
// apply provider implementation parameters
for (final Entry<String, String> implParameter : implParameters.entrySet()) {
final String[] classAndParameter = implParameter.getKey().split("/", 2);
if (classAndParameter.length == 2) {
try {
NegotiateSecurityFilter.LOGGER.debug("setting {}, {}={}", classAndParameter[0], classAndParameter[1],
implParameter.getValue());
final SecurityFilterProvider provider = this.providers.getByClassName(classAndParameter[0]);
provider.initParameter(classAndParameter[1], implParameter.getValue());
} catch (final ClassNotFoundException e) {
NegotiateSecurityFilter.LOGGER.error("invalid class: {} in {}", classAndParameter[0], implParameter.getKey());
throw new ServletException(e);
} catch (final Exception e) {
NegotiateSecurityFilter.LOGGER.error("{}: error setting '{}': {}", classAndParameter[0], classAndParameter[1],
e.getMessage());
NegotiateSecurityFilter.LOGGER.trace("{}", e);
throw new ServletException(e);
}
} else {
NegotiateSecurityFilter.LOGGER.error("Invalid parameter: {}", implParameter.getKey());
throw new ServletException("Invalid parameter: " + implParameter.getKey());
}
}
NegotiateSecurityFilter.LOGGER.info("[waffle.servlet.NegotiateSecurityFilter] started");
}
/**
* Set the principal format.
*
* @param format
* Principal format.
*/
public void setPrincipalFormat(final String format) {
this.principalFormat = PrincipalFormat.valueOf(format.toUpperCase(Locale.ENGLISH));
NegotiateSecurityFilter.LOGGER.info("principal format: {}", this.principalFormat);
}
/**
* Principal format.
*
* @return Principal format.
*/
public PrincipalFormat getPrincipalFormat() {
return this.principalFormat;
}
/**
* Set the principal format.
*
* @param format
* Role format.
*/
public void setRoleFormat(final String format) {
this.roleFormat = PrincipalFormat.valueOf(format.toUpperCase(Locale.ENGLISH));
NegotiateSecurityFilter.LOGGER.info("role format: {}", this.roleFormat);
}
/**
* Principal format.
*
* @return Role format.
*/
public PrincipalFormat getRoleFormat() {
return this.roleFormat;
}
/**
* Send a 401 Unauthorized along with protocol authentication headers.
*
* @param response
* HTTP Response
* @param close
* Close connection.
*/
private void sendUnauthorized(final HttpServletResponse response, final boolean close) {
try {
this.providers.sendUnauthorized(response);
if (close) {
response.setHeader("Connection", "close");
} else {
response.setHeader("Connection", "keep-alive");
}
response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
response.flushBuffer();
} catch (final IOException e) {
throw new RuntimeException(e);
}
}
/**
* Windows auth provider.
*
* @return IWindowsAuthProvider.
*/
public IWindowsAuthProvider getAuth() {
return this.auth;
}
/**
* Set Windows auth provider.
*
* @param provider
* Class implements IWindowsAuthProvider.
*/
public void setAuth(final IWindowsAuthProvider provider) {
this.auth = provider;
}
/**
* True if guest login is allowed.
*
* @return True if guest login is allowed, false otherwise.
*/
public boolean isAllowGuestLogin() {
return this.allowGuestLogin;
}
/**
* Enable/Disable impersonation.
*
* @param value
* true to enable impersonation, false otherwise
*/
public void setImpersonate(final boolean value) {
this.impersonate = value;
}
/**
* Checks if is impersonate.
*
* @return true if impersonation is enabled, false otherwise
*/
public boolean isImpersonate() {
return this.impersonate;
}
/**
* Security filter providers.
*
* @return A collection of security filter providers.
*/
public SecurityFilterProviderCollection getProviders() {
return this.providers;
}
}