PasswordModifyExtendedOperation.java revision 3bfde8a324ef1dc0d757a9a34007cdb15018dc9a
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at
* trunk/opends/resource/legal-notices/OpenDS.LICENSE. If applicable,
* add the following below this CDDL HEADER, with the fields enclosed
* by brackets "[]" replaced with your own identifying information:
* Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*
*
* Copyright 2006-2010 Sun Microsystems, Inc.
* Portions Copyright 2011 ForgeRock AS
*/
/**
* This class implements the password modify extended operation defined in RFC
* 3062. It includes support for requiring the user's current password as well
* as for generating a new password if none was provided.
*/
public class PasswordModifyExtendedOperation
extends ExtendedOperationHandler<
implements ConfigurationChangeListener<
{
// The following attachments may be used by post-op plugins (e.g. Samba) in
// order to avoid re-decoding the request parameters and also to enforce
// atomicity.
/**
* The name of the attachment which will be used to store the fully resolved
* target entry.
*/
public static final String AUTHZ_DN_ATTACHMENT;
/**
* The name of the attachment which will be used to store the password
* attribute.
*/
public static final String PWD_ATTRIBUTE_ATTACHMENT;
/**
* The clear text password, which may not be present if the provided password
* was pre-encoded.
*/
public static final String CLEAR_PWD_ATTACHMENT;
/**
* A list containing the encoded passwords: plugins can perform changes
* atomically via CAS.
*/
public static final String ENCODED_PWD_ATTACHMENT;
static
{
}
/**
* The tracer object for the debug logger.
*/
// The current configuration state.
// The DN of the identity mapper.
private DN identityMapperDN;
// The reference to the identity mapper.
private IdentityMapper<?> identityMapper;
// The default set of supported control OIDs for this extended
/**
* Create an instance of this password modify extended operation. All
* initialization should be performed in the
* <CODE>initializeExtendedOperationHandler</CODE> method.
*/
public PasswordModifyExtendedOperation()
{
super();
}
/**
* Initializes this extended operation handler based on the information in the
* provided configuration. It should also register itself with the
* Directory Server for the particular kinds of extended operations that it
* will process.
*
* @param config The configuration that contains the information
* to use to initialize this extended operation handler.
*
* @throws ConfigException If an unrecoverable problem arises in the
* process of performing the initialization.
*
* @throws InitializationException If a problem occurs during initialization
* that is not related to the server
* configuration.
*/
public void initializeExtendedOperationHandler(
{
try
{
if (identityMapper == null)
{
throw new ConfigException(message);
}
}
catch (Exception e)
{
if (debugEnabled())
{
}
throw new InitializationException(message, e);
}
// Save this configuration for future reference.
// Register this as a change listener.
this);
}
/**
* Performs any finalization that may be necessary for this extended
* operation handler. By default, no finalization is performed.
*/
public void finalizeExtendedOperationHandler()
{
}
/**
* {@inheritDoc}
*/
@Override()
{
return supportedControlOIDs;
}
/**
* Processes the provided extended operation.
*
* @param operation The extended operation to be processed.
*/
{
// Initialize the variables associated with components that may be included
// in the request.
// Look at the set of controls included in the request, if there are any.
boolean noOpRequested = false;
boolean pwPolicyRequested = false;
int pwPolicyWarningValue = 0;
{
{
{
noOpRequested = true;
}
{
pwPolicyRequested = true;
}
}
}
// Parse the encoded request, if there is one.
if (requestValue != null)
{
try
{
if(reader.hasNextElement() &&
{
}
if(reader.hasNextElement() &&
{
}
if(reader.hasNextElement() &&
{
}
}
{
if (debugEnabled())
{
}
return;
}
}
// Get the entry for the user that issued the request.
// See if a user identity was provided. If so, then try to resolve it to
// an actual user.
try
{
if (userIdentity == null)
{
// This request must be targeted at changing the password for the
// currently-authenticated user. Make sure that the user actually is
// authenticated.
{
return;
}
// Retrieve a write lock on that user's entry.
for (int i=0; i < 3; i++)
{
{
break;
}
}
{
return;
}
}
else
{
// There was a userIdentity field in the request.
{
try
{
}
catch (DirectoryException de)
{
if (debugEnabled())
{
}
return;
}
// If the provided DN is an alternate DN for a root user, then replace
// it with the actual root DN.
if (actualRootDN != null)
{
}
{
return;
}
}
{
try
{
{
return;
}
else
{
}
}
catch (DirectoryException de)
{
if (debugEnabled())
{
}
//Encountered an exception while resolving identity.
return;
}
}
// the userIdentity provided does not follow Authorization Identity
// form. RFC3062 declaration "may or may not be an LDAPDN" allows
// for pretty much anything in that field. we gonna try to parse it
// as DN first then if that fails as user ID.
else
{
try
{
}
catch (DirectoryException de)
{
if (debugEnabled())
{
}
// IGNORE.
}
// If the provided DN is an alternate DN for a root user,
// then replace it with the actual root DN.
if (actualRootDN != null) {
}
} else {
try
{
}
catch (DirectoryException de)
{
if (debugEnabled())
{
}
// IGNORE.
}
}
// The userIdentity was invalid.
return;
}
else
{
}
}
}
// At this point, we should have the user entry. Get the associated
// password policy.
try
{
false);
if (!policy.isPasswordPolicy())
{
return;
}
}
catch (DirectoryException de)
{
if (debugEnabled())
{
}
de.getMessageObject()));
return;
}
// Determine whether the user is changing his own password or if it's an
// administrative reset. If it's an administrative reset, then the
// requester must have the PASSWORD_RESET privilege.
boolean selfChange;
if (userIdentity == null)
{
selfChange = true;
}
else if (requestorEntry == null)
{
}
else
{
}
if (! selfChange)
{
{
return;
}
}
// See if the account is locked. If so, then reject the request.
if (pwPolicyState.isDisabled())
{
if (pwPolicyRequested)
{
}
return;
}
else if (selfChange &&
{
if (pwPolicyRequested)
{
}
return;
}
// If the current password was provided, then we'll need to verify whether
// it was correct. If it wasn't provided but this is a self change, then
// make sure that's OK.
if (oldPassword == null)
{
if (selfChange
{
if (pwPolicyRequested)
{
}
return;
}
}
else
{
{
getClass(), "additionalInfo",
return;
}
{
}
else
{
getClass(), "additionalInfo",
{
}
return;
}
}
// If it is a self password change and we don't allow that, then reject
// the request.
if (selfChange
{
if (pwPolicyRequested)
{
}
return;
}
// If we require secure password changes and the connection isn't secure,
// then reject the request.
{
return;
}
// If it's a self-change request and the user is within the minimum age,
// then reject it.
{
if (pwPolicyRequested)
{
}
return;
}
// If the user's password is expired and it's a self-change request, then
// see if that's OK.
{
if (pwPolicyRequested)
{
}
return;
}
// If the a new password was provided, then perform any appropriate
// validation on it. If not, then see if we can generate one.
boolean generatedPassword = false;
boolean isPreEncoded = false;
if (newPassword == null)
{
try
{
if (newPassword == null)
{
return;
}
else
{
generatedPassword = true;
}
}
catch (DirectoryException de)
{
if (debugEnabled())
{
}
de.getMessageObject()));
return;
}
}
else
{
{
// The password modify extended operation isn't intended to be invoked
// by an internal operation or during synchronization, so we don't
// need to check for those cases.
isPreEncoded = true;
{
return;
}
}
else
{
// Run the new password through the set of password validators.
if (selfChange
{
if (oldPassword == null)
{
}
else
{
{
{
}
}
}
{
if (pwPolicyRequested)
{
}
return;
}
}
// Prepare to update the password history, if necessary.
if (pwPolicyState.maintainHistory())
{
{
{
return;
}
}
else
{
}
}
}
}
// Get the encoded forms of the new password.
if (isPreEncoded)
{
}
else
{
try
{
}
catch (DirectoryException de)
{
if (debugEnabled())
{
}
de.getMessageObject()));
return;
}
}
// If the current password was provided, then remove all matching values
// from the user's entry and replace them with the new password.
// Otherwise replace all password values.
if (oldPassword != null)
{
// Remove all existing encoded values that match the old password.
{
for (AttributeValue v : existingValues)
{
try
{
{
// The password is encoded using an unknown scheme. Remove it
// from the user's entry.
deleteValues.add(v);
}
else
{
{
deleteValues.add(v);
}
}
}
catch (DirectoryException de)
{
if (debugEnabled())
{
}
// We couldn't decode the provided password value, so remove it
// from the user's entry.
deleteValues.add(v);
}
}
}
else
{
for (AttributeValue v : existingValues)
{
try
{
String[] components =
{
// The password is encoded using an unknown scheme. Remove it
// from the user's entry.
deleteValues.add(v);
}
else
{
{
deleteValues.add(v);
}
}
}
catch (DirectoryException de)
{
if (debugEnabled())
{
}
// We couldn't decode the provided password value, so remove it
// from the user's entry.
deleteValues.add(v);
}
}
}
// Add the new encoded values.
for (ByteString s : encodedPasswords)
{
}
}
else
{
for (ByteString s : encodedPasswords)
{
}
}
// Update the password changed time for the user entry.
// If the password was changed by an end user, then clear any reset flag
// that might exist. If the password was changed by an administrator,
// then see if we need to set the reset flag.
if (selfChange)
{
pwPolicyState.setMustChangePassword(false);
}
else
{
}
// Clear any record of grace logins, auth failures, and expiration
// warnings.
// If the LDAP no-op control was included in the request, then set the
// appropriate response. Otherwise, process the operation.
if (noOpRequested)
{
}
else
{
{
}
// Get an internal connection and use it to perform the modification.
isRoot);
{
return;
}
// If there were any password policy state changes, we need to apply
// them using a root connection because the end user may not have
// sufficient access to apply them. This is less efficient than
// doing them all in the same modification, but it's safer.
if (! pwPolicyMods.isEmpty())
{
{
// At this point, the user's password is already changed so there's
// not much point in returning a non-success result. However, we
// should at least log that something went wrong.
modOp.getErrorMessage());
}
}
// If we've gotten here, then everything is OK, so indicate that the
// operation was successful.
// Save attachments for post-op plugins (e.g. Samba password plugin).
if (!isPreEncoded)
{
}
// If a password was generated, then include it in the response.
if (generatedPassword)
{
try
{
}
catch(Exception e)
{
}
}
// If this was a self password change, and the client is authenticated
// as the user whose password was changed, then clear the "must change
// password" flag in the client connection. Note that we're using the
// authentication DN rather than the authorization DN in this case to
// avoid mistakenly clearing the flag for the wrong user.
{
}
// If the password policy control was requested, then add the
// appropriate response control.
if (pwPolicyRequested)
{
}
// Handle Account Status Notifications that may be needed.
// They are not handled by the backend for internal operations.
if (oldPassword != null)
{
}
if (newPassword != null)
{
}
if (selfChange)
{
}
else
{
}
}
}
finally
{
{
}
}
}
/**
* Retrieves the entry for the specified user based on the provided DN. If
* any problem is encountered or the requested entry does not exist, then the
* provided operation will be updated with appropriate result information and
* this method will return <CODE>null</CODE>. The caller must hold a write
* lock on the specified entry.
*
* @param operation The extended operation being processed.
* @param entryDN The DN of the user entry to retrieve.
*
* @return The requested entry, or <CODE>null</CODE> if there was no such
* entry or it could not be retrieved.
*/
{
// Retrieve the user's entry from the directory. If it does not exist, then
// fail.
try
{
{
// See if one of the entry's ancestors exists.
{
try
{
{
break;
}
}
catch (Exception e)
{
if (debugEnabled())
{
}
break;
}
}
return null;
}
return userEntry;
}
catch (DirectoryException de)
{
if (debugEnabled())
{
}
return null;
}
}
/**
* {@inheritDoc}
*/
@Override()
public boolean isConfigurationAcceptable(ExtendedOperationHandlerCfg
{
}
/**
* Indicates whether the provided configuration entry has an acceptable
* configuration for this component. If it does not, then detailed
* information about the problem(s) should be added to the provided list.
*
* @param config The configuration entry for which to make the
* determination.
* @param unacceptableReasons A list that can be used to hold messages about
* why the provided entry does not have an
* acceptable configuration.
*
* @return <CODE>true</CODE> if the provided entry has an acceptable
* configuration for this component, or <CODE>false</CODE> if not.
*/
public boolean isConfigurationChangeAcceptable(
{
// Make sure that the specified identity mapper is OK.
try
{
{
return false;
}
}
catch (Exception e)
{
if (debugEnabled())
{
}
getExceptionMessage(e));
return false;
}
// If we've gotten here, then everything is OK.
return true;
}
/**
* Makes a best-effort attempt to apply the configuration contained in the
* provided entry. Information about the result of this processing should be
* added to the provided message list. Information should always be added to
* this list if a configuration change could not be applied. If detailed
* results are requested, then information about the changes applied
* successfully (and optionally about parameters that were not changed) should
* also be included.
*
* @param config The entry containing the new configuration to
* apply for this component.
*
* @return Information about the result of the configuration update.
*/
{
boolean adminActionRequired = false;
// Make sure that the specified identity mapper is OK.
try
{
{
}
}
catch (Exception e)
{
if (debugEnabled())
{
}
getExceptionMessage(e)));
}
// If all of the changes were acceptable, then apply them.
{
{
}
}
// Save this configuration for future reference.
}
/**
* {@inheritDoc}
*/
public String getExtendedOperationName()
{
return "Password Modify";
}
}