SaslServerCallbackHandler.java
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.zookeeper.server.auth;
import java.io.IOException;
import java.util.Map;
import javax.security.auth.callback.Callback;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.NameCallback;
import javax.security.auth.callback.PasswordCallback;
import javax.security.auth.callback.UnsupportedCallbackException;
import javax.security.sasl.AuthorizeCallback;
import javax.security.sasl.RealmCallback;
import org.apache.zookeeper.common.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class SaslServerCallbackHandler implements CallbackHandler {
private static final Logger LOG = LoggerFactory.getLogger(SaslServerCallbackHandler.class);
private static final String SYSPROP_SUPER_PASSWORD = "zookeeper.SASLAuthenticationProvider.superPassword";
private static final String SYSPROP_REMOVE_HOST = "zookeeper.kerberos.removeHostFromPrincipal";
private static final String SYSPROP_REMOVE_REALM = "zookeeper.kerberos.removeRealmFromPrincipal";
private String userName;
private final Map<String, String> credentials;
public SaslServerCallbackHandler(Map<String, String> credentials) {
this.credentials = credentials;
}
public void handle(Callback[] callbacks) throws UnsupportedCallbackException {
for (Callback callback : callbacks) {
if (callback instanceof NameCallback) {
handleNameCallback((NameCallback) callback);
} else if (callback instanceof PasswordCallback) {
handlePasswordCallback((PasswordCallback) callback);
} else if (callback instanceof RealmCallback) {
handleRealmCallback((RealmCallback) callback);
} else if (callback instanceof AuthorizeCallback) {
handleAuthorizeCallback((AuthorizeCallback) callback);
}
}
}
private void handleNameCallback(NameCallback nc) {
// check to see if this user is in the user password database.
if (credentials.get(nc.getDefaultName()) == null) {
LOG.warn("User '{}' not found in list of DIGEST-MD5 authenticateable users.",
StringUtils.sanitizeForLog(nc.getDefaultName()));
return;
}
nc.setName(nc.getDefaultName());
userName = nc.getDefaultName();
}
private void handlePasswordCallback(PasswordCallback pc) {
if ("super".equals(this.userName) && System.getProperty(SYSPROP_SUPER_PASSWORD) != null) {
// superuser: use Java system property for password, if available.
pc.setPassword(System.getProperty(SYSPROP_SUPER_PASSWORD).toCharArray());
} else if (credentials.containsKey(userName)) {
pc.setPassword(credentials.get(userName).toCharArray());
} else {
LOG.warn("No password found for user: {}", userName);
}
}
private void handleRealmCallback(RealmCallback rc) {
LOG.debug("client supplied realm: {}", StringUtils.sanitizeForLog(rc.getDefaultText()));
rc.setText(rc.getDefaultText());
}
private void handleAuthorizeCallback(AuthorizeCallback ac) {
String authenticationID = ac.getAuthenticationID();
String authorizationID = ac.getAuthorizationID();
// A client must not be allowed to authorize as an identity other than the
// one it authenticated as. The SASL authorizationID (authzid) is an
// attacker-controlled field of the client token. If it differs from the
// authenticated identity we must reject it here: otherwise, whenever the
// identity canonicalization below fails -- which happens for any principal
// not covered by zookeeper.security.auth_to_local rules, i.e. every
// cross-realm principal under the default DEFAULT rule -- the JDK SASL
// server would fall back to returning the client-requested authzid as the
// negotiated identity (AuthorizeCallback.getAuthorizedID()), letting a
// low-privilege client assume any identity (e.g. "super"). This mirrors the
// equality check in SaslQuorumServerCallbackHandler and
// SaslClientCallbackHandler.
if (authenticationID == null || !authenticationID.equals(authorizationID)) {
// authenticationID and authorizationID are unvalidated, client-controlled
// strings from the SASL token; sanitize before logging to prevent log
// injection (CWE-117). WARN, not ERROR: a rejected client is not an
// operator-actionable server fault, and ERROR should stay meaningful for
// alerting since a client can repeat failed attempts at will.
LOG.warn("Client attempted to authorize as a different identity: "
+ "authenticationID={}; requested authorizationID={}. Denying authorization.",
StringUtils.sanitizeForLog(authenticationID),
StringUtils.sanitizeForLog(authorizationID));
ac.setAuthorized(false);
return;
}
LOG.info("Successfully authenticated client: authenticationID={}; authorizationID={}.",
StringUtils.sanitizeForLog(authenticationID), StringUtils.sanitizeForLog(authorizationID));
ac.setAuthorized(true);
// canonicalize authorization id according to system properties:
// zookeeper.kerberos.removeRealmFromPrincipal(={true,false})
// zookeeper.kerberos.removeHostFromPrincipal(={true,false})
KerberosName kerberosName = new KerberosName(authenticationID);
try {
StringBuilder userNameBuilder = new StringBuilder(kerberosName.getShortName());
if (shouldAppendHost(kerberosName)) {
userNameBuilder.append("/").append(kerberosName.getHostName());
}
if (shouldAppendRealm(kerberosName)) {
userNameBuilder.append("@").append(kerberosName.getRealm());
}
LOG.info("Setting authorizedID: {}", StringUtils.sanitizeForLog(userNameBuilder.toString()));
ac.setAuthorizedID(userNameBuilder.toString());
} catch (IOException e) {
LOG.error("Failed to set name based on Kerberos authentication rules.", e);
}
}
private boolean shouldAppendRealm(KerberosName kerberosName) {
return !isSystemPropertyTrue(SYSPROP_REMOVE_REALM) && kerberosName.getRealm() != null;
}
private boolean shouldAppendHost(KerberosName kerberosName) {
return !isSystemPropertyTrue(SYSPROP_REMOVE_HOST) && kerberosName.getHostName() != null;
}
private boolean isSystemPropertyTrue(String propertyName) {
return "true".equals(System.getProperty(propertyName));
}
}