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;
021    
022    
023    import java.io.IOException;
024    import java.util.HashMap;
025    import java.util.Map;
026    
027    import javax.naming.NamingException;
028    import javax.security.auth.kerberos.KerberosPrincipal;
029    
030    import org.apache.directory.server.core.entry.ServerStringValue;
031    import org.apache.directory.server.kerberos.shared.crypto.encryption.EncryptionType;
032    import org.apache.directory.server.kerberos.shared.io.decoder.EncryptionKeyDecoder;
033    import org.apache.directory.server.kerberos.shared.messages.value.EncryptionKey;
034    import org.apache.directory.server.kerberos.shared.messages.value.KerberosTime;
035    import org.apache.directory.server.kerberos.shared.messages.value.SamType;
036    import org.apache.directory.shared.ldap.entry.EntryAttribute;
037    import org.apache.directory.shared.ldap.entry.Value;
038    
039    
040    /**
041     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
042     * @version $Rev: 798550 $, $Date: 2009-07-28 17:54:01 +0300 (Tue, 28 Jul 2009) $
043     */
044    public class PrincipalStoreEntryModifier
045    {
046        // principal
047        private String distinguishedName;
048        private String commonName;
049        private KerberosPrincipal principal;
050        private String realmName;
051    
052        // uidObject
053        private String userId;
054    
055        // KDCEntry
056        // must
057        private int keyVersionNumber;
058        // may
059        private KerberosTime validStart;
060        private KerberosTime validEnd;
061        private KerberosTime passwordEnd;
062        private int maxLife;
063        private int maxRenew;
064        private int kdcFlags;
065        private SamType samType;
066    
067        private boolean disabled = false;
068        private boolean lockedOut = false;
069        private KerberosTime expiration = KerberosTime.INFINITY;
070    
071        private Map<EncryptionType, EncryptionKey> keyMap;
072    
073    
074        /**
075         * Returns the {@link PrincipalStoreEntry}.
076         *
077         * @return The {@link PrincipalStoreEntry}.
078         */
079        public PrincipalStoreEntry getEntry()
080        {
081            return new PrincipalStoreEntry( distinguishedName, commonName, userId, principal, keyVersionNumber, validStart,
082                validEnd, passwordEnd, maxLife, maxRenew, kdcFlags, keyMap, realmName, samType, disabled, lockedOut,
083                expiration );
084        }
085    
086    
087        /**
088         * Sets whether the account is disabled.
089         *
090         * @param disabled
091         */
092        public void setDisabled( boolean disabled )
093        {
094            this.disabled = disabled;
095        }
096    
097    
098        /**
099         * Sets whether the account is locked-out.
100         *
101         * @param lockedOut
102         */
103        public void setLockedOut( boolean lockedOut )
104        {
105            this.lockedOut = lockedOut;
106        }
107    
108    
109        /**
110         * Sets the expiration time.
111         *
112         * @param expiration
113         */
114        public void setExpiration( KerberosTime expiration )
115        {
116            this.expiration = expiration;
117        }
118    
119    
120        /**
121         * Sets the distinguished name (DN).
122         *
123         * @param distinguishedName
124         */
125        public void setDistinguishedName( String distinguishedName )
126        {
127            this.distinguishedName = distinguishedName;
128        }
129    
130    
131        /**
132         * Sets the common name (cn).
133         *
134         * @param commonName
135         */
136        public void setCommonName( String commonName )
137        {
138            this.commonName = commonName;
139        }
140    
141    
142        /**
143         * Sets the user ID.
144         *
145         * @param userId
146         */
147        public void setUserId( String userId )
148        {
149            this.userId = userId;
150        }
151    
152    
153        /**
154         * Sets the KDC flags.
155         *
156         * @param kdcFlags
157         */
158        public void setKDCFlags( int kdcFlags )
159        {
160            this.kdcFlags = kdcFlags;
161        }
162    
163    
164        /**
165         * Sets the key map.
166         *
167         * @param keyMap
168         */
169        public void setKeyMap( Map<EncryptionType, EncryptionKey> keyMap )
170        {
171            this.keyMap = keyMap;
172        }
173    
174    
175        /**
176         * Sets the key version number.
177         *
178         * @param keyVersionNumber
179         */
180        public void setKeyVersionNumber( int keyVersionNumber )
181        {
182            this.keyVersionNumber = keyVersionNumber;
183        }
184    
185    
186        /**
187         * Sets the ticket maximum life time.
188         *
189         * @param maxLife
190         */
191        public void setMaxLife( int maxLife )
192        {
193            this.maxLife = maxLife;
194        }
195    
196    
197        /**
198         * Sets the ticket maximum renew time.
199         *
200         * @param maxRenew
201         */
202        public void setMaxRenew( int maxRenew )
203        {
204            this.maxRenew = maxRenew;
205        }
206    
207    
208        /**
209         * Sets the end-of-life for the password.
210         *
211         * @param passwordEnd
212         */
213        public void setPasswordEnd( KerberosTime passwordEnd )
214        {
215            this.passwordEnd = passwordEnd;
216        }
217    
218    
219        /**
220         * Sets the principal.
221         *
222         * @param principal
223         */
224        public void setPrincipal( KerberosPrincipal principal )
225        {
226            this.principal = principal;
227        }
228    
229    
230        /**
231         * Sets the realm.
232         *
233         * @param realmName
234         */
235        public void setRealmName( String realmName )
236        {
237            this.realmName = realmName;
238        }
239    
240    
241        /**
242         * Sets the end of validity.
243         *
244         * @param validEnd
245         */
246        public void setValidEnd( KerberosTime validEnd )
247        {
248            this.validEnd = validEnd;
249        }
250    
251    
252        /**
253         * Sets the start of validity.
254         *
255         * @param validStart
256         */
257        public void setValidStart( KerberosTime validStart )
258        {
259            this.validStart = validStart;
260        }
261    
262    
263        /**
264         * Sets the single-use authentication (SAM) type.
265         *
266         * @param samType
267         */
268        public void setSamType( SamType samType )
269        {
270            this.samType = samType;
271        }
272    
273    
274        /**
275         * Converts the ASN.1 encoded key set to a map of encryption types to encryption keys.
276         *
277         * @param krb5key
278         * @return The map of encryption types to encryption keys.
279         * @throws NamingException
280         * @throws IOException
281         */
282        public Map<EncryptionType, EncryptionKey> reconstituteKeyMap( EntryAttribute krb5key ) throws Exception
283        {
284            Map<EncryptionType, EncryptionKey> map = new HashMap<EncryptionType, EncryptionKey>();
285    
286            for ( Value<?> val : krb5key )
287            {
288                if ( val instanceof ServerStringValue )
289                {
290                    throw new IllegalStateException( "Kerberos key should not be a String." );
291                }
292    
293                byte[] encryptionKeyBytes = val.getBytes();
294                EncryptionKey encryptionKey = EncryptionKeyDecoder.decode( encryptionKeyBytes );
295                map.put( encryptionKey.getKeyType(), encryptionKey );
296            }
297    
298            return map;
299        }
300    }