001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied. See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 *
019 */
020 package org.apache.directory.server.kerberos.shared.store.operations;
021
022
023 import java.util.ArrayList;
024 import java.util.List;
025
026 import javax.naming.directory.DirContext;
027 import javax.security.auth.kerberos.KerberosPrincipal;
028
029 import org.apache.directory.server.core.CoreSession;
030 import org.apache.directory.server.core.entry.DefaultServerAttribute;
031 import org.apache.directory.server.core.entry.ServerAttribute;
032 import org.apache.directory.server.core.entry.ServerEntry;
033 import org.apache.directory.server.core.entry.ServerModification;
034 import org.apache.directory.server.kerberos.shared.store.KerberosAttribute;
035 import org.apache.directory.server.protocol.shared.store.DirectoryServiceOperation;
036 import org.apache.directory.server.schema.registries.AttributeTypeRegistry;
037 import org.apache.directory.shared.ldap.constants.SchemaConstants;
038 import org.apache.directory.shared.ldap.entry.Modification;
039 import org.apache.directory.shared.ldap.entry.ModificationOperation;
040 import org.apache.directory.shared.ldap.name.LdapDN;
041 import org.apache.directory.shared.ldap.util.StringTools;
042
043
044 /**
045 * Command for changing a principal's password in a JNDI context.
046 *
047 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
048 * @version $Rev: 762529 $, $Date: 2009-04-07 01:18:31 +0300 (Tue, 07 Apr 2009) $
049 */
050 public class ChangePassword implements DirectoryServiceOperation
051 {
052 private static final long serialVersionUID = -7147685183641418353L;
053
054 /** The Kerberos principal who's password is to be changed. */
055 protected KerberosPrincipal principal;
056 /** The new password for the update. */
057 protected String newPassword;
058
059
060 /**
061 * Creates the action to be used against the embedded ApacheDS DIT.
062 *
063 * @param principal The principal to change the password for.
064 * @param newPassword The password to change.
065 */
066 public ChangePassword( KerberosPrincipal principal, String newPassword )
067 {
068 this.principal = principal;
069 this.newPassword = newPassword;
070 }
071
072
073 public Object execute( CoreSession session, LdapDN searchBaseDn ) throws Exception
074 {
075 if ( principal == null )
076 {
077 return null;
078 }
079
080 AttributeTypeRegistry registry = session.getDirectoryService().getRegistries().getAttributeTypeRegistry();
081
082 List<Modification> mods = new ArrayList<Modification>(2);
083
084 ServerAttribute newPasswordAttribute = new DefaultServerAttribute(
085 registry.lookup( SchemaConstants.USER_PASSWORD_AT ), StringTools.getBytesUtf8( newPassword ) );
086 mods.add( new ServerModification( ModificationOperation.REPLACE_ATTRIBUTE, newPasswordAttribute ) );
087
088 ServerAttribute principalAttribute = new DefaultServerAttribute(
089 registry.lookup( KerberosAttribute.KRB5_PRINCIPAL_NAME_AT ), principal.getName() );
090 mods.add( new ServerModification( ModificationOperation.REPLACE_ATTRIBUTE, principalAttribute ) );
091
092 //FIXME check if keyderivation is necessary
093
094 ServerEntry entry = StoreUtils.findPrincipalEntry( session, searchBaseDn, principal.getName() );
095 session.modify( entry.getDn(), mods );
096
097 return entry.getDn().toString();
098 }
099 }