ebyPrincipalEntry = StoreUtils.findPrincipalEntry( adminSession, searchBaseDn, byPrincipal.getName() );
if ( ebyPrincipalEntry == null )
{
throw new ChangePasswordException( ChangePasswdErrorType.KRB5_KPASSWD_HARDERROR,
( "No such principal " + byPrincipal ).getBytes() );
}
SchemaManager schemaManager = directoryService.getSchemaManager();
CoreSession bySession = null;
boolean isAdmin = ebyPrincipalEntry.getDn().getNormName()
.equals( ServerDNConstants.ADMIN_SYSTEM_DN_NORMALIZED );
if ( !isInitialTicket && !isAdmin )
{
throw new ChangePasswordException( ChangePasswdErrorType.KRB5_KPASSWD_INITIAL_FLAG_NEEDED );
}
// if admin assign the admin session
if ( isAdmin )
{
bySession = adminSession;
}
// otherwise create a new session for the user with 'byPrincipal' who is trying to change the password for 'forPrincipal'
else
{
LdapPrincipal byLdapPrincipal = new LdapPrincipal( schemaManager, ebyPrincipalEntry.getDn(),
AuthenticationLevel.SIMPLE );
bySession = new DefaultCoreSession( byLdapPrincipal, directoryService );
}
Attribute newPasswordAttribute = new DefaultAttribute(
schemaManager.lookupAttributeTypeRegistry( SchemaConstants.USER_PASSWORD_AT ),
Strings.getBytesUtf8( newPassword ) );
Modification passwordMod = new DefaultModification( ModificationOperation.REPLACE_ATTRIBUTE,
newPasswordAttribute );
Attribute principalAttribute = new DefaultAttribute(
schemaManager.lookupAttributeTypeRegistry( KerberosAttribute.KRB5_PRINCIPAL_NAME_AT ),
forPrincipal.getName() );
Modification principalMod = new DefaultModification( ModificationOperation.REPLACE_ATTRIBUTE,
principalAttribute );
Entry forPrincipalEntry = StoreUtils.findPrincipalEntry( bySession, searchBaseDn, forPrincipal.getName() );
adminSession.modify( forPrincipalEntry.getDn(), passwordMod, principalMod );
}
catch ( LdapException e )
{
throw new ChangePasswordException( ChangePasswdErrorType.KRB5_KPASSWD_ACCESSDENIED, e );
}
catch ( Exception e )
{
throw new ChangePasswordException( ChangePasswdErrorType.KRB5_KPASSWD_HARDERROR, e );
}
}