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.keytab;
021    
022    
023    import java.io.File;
024    import java.io.FileInputStream;
025    import java.io.FileOutputStream;
026    import java.io.IOException;
027    import java.io.InputStream;
028    import java.nio.channels.FileChannel;
029    import java.util.ArrayList;
030    import java.util.Collections;
031    import java.util.List;
032    
033    import org.apache.directory.server.i18n.I18n;
034    import org.apache.mina.core.buffer.IoBuffer;
035    
036    
037    /**
038     * Keytab file.
039     *
040     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
041     * @version $Rev$, $Date$
042     */
043    public class Keytab
044    {
045        /**
046         * Byte array constant for keytab file format 5.1.
047         */
048        public static final byte[] VERSION_51 = new byte[]
049            { ( byte ) 0x05, ( byte ) 0x01 };
050    
051        /**
052         * Byte array constant for keytab file format 5.2.
053         */
054        public static final byte[] VERSION_52 = new byte[]
055            { ( byte ) 0x05, ( byte ) 0x02 };
056    
057        private byte[] keytabVersion = VERSION_52;
058        private List<KeytabEntry> entries = new ArrayList<KeytabEntry>();
059    
060    
061        /**
062         * Read a keytab file.
063         *
064         * @param file
065         * @return The keytab.
066         * @throws IOException
067         */
068        public static Keytab read( File file ) throws IOException
069        {
070            IoBuffer buffer = IoBuffer.wrap( getBytesFromFile( file ) );
071            return readKeytab( buffer );
072        }
073    
074    
075        /**
076         * Returns a new instance of a keytab with the version
077         * defaulted to 5.2.
078         *
079         * @return The keytab.
080         */
081        public static Keytab getInstance()
082        {
083            return new Keytab();
084        }
085    
086    
087        /**
088         * Write the keytab to a {@link File}.
089         *
090         * @param file
091         * @throws IOException
092         */
093        public void write( File file ) throws IOException
094        {
095            KeytabEncoder writer = new KeytabEncoder();
096            IoBuffer buffer = writer.write( keytabVersion, entries );
097            writeFile( buffer, file );
098        }
099    
100    
101        /**
102         * @param entries The entries to set.
103         */
104        public void setEntries( List<KeytabEntry> entries )
105        {
106            this.entries = entries;
107        }
108    
109    
110        /**
111         * @param keytabVersion The keytabVersion to set.
112         */
113        public void setKeytabVersion( byte[] keytabVersion )
114        {
115            this.keytabVersion = keytabVersion;
116        }
117    
118    
119        /**
120         * @return The entries.
121         */
122        public List<KeytabEntry> getEntries()
123        {
124            return Collections.unmodifiableList( entries );
125        }
126    
127    
128        /**
129         * @return The keytabVersion.
130         */
131        public byte[] getKeytabVersion()
132        {
133            return keytabVersion;
134        }
135    
136    
137        /**
138         * Read bytes into a keytab.
139         *
140         * @param bytes
141         * @return The keytab.
142         */
143        static Keytab read( byte[] bytes )
144        {
145            IoBuffer buffer = IoBuffer.wrap( bytes );
146            return readKeytab( buffer );
147        }
148    
149    
150        /**
151         * Write the keytab to a {@link ByteBuffer}.
152         * @return The buffer.
153         */
154        IoBuffer write()
155        {
156            KeytabEncoder writer = new KeytabEncoder();
157            return writer.write( keytabVersion, entries );
158        }
159    
160    
161        /**
162         * Read the contents of the buffer into a keytab.
163         *
164         * @param buffer
165         * @return The keytab.
166         */
167        private static Keytab readKeytab( IoBuffer buffer )
168        {
169            KeytabDecoder reader = new KeytabDecoder();
170            byte[] keytabVersion = reader.getKeytabVersion( buffer );
171            List<KeytabEntry> entries = reader.getKeytabEntries( buffer );
172    
173            Keytab keytab = new Keytab();
174    
175            keytab.setKeytabVersion( keytabVersion );
176            keytab.setEntries( entries );
177    
178            return keytab;
179        }
180    
181    
182        /**
183         * Returns the contents of the {@link File} in a byte array.
184         *
185         * @param file
186         * @return The byte array of the file contents.
187         * @throws IOException
188         */
189        protected static byte[] getBytesFromFile( File file ) throws IOException
190        {
191            InputStream is = new FileInputStream( file );
192    
193            long length = file.length();
194    
195            // Check to ensure that file is not larger than Integer.MAX_VALUE.
196            if ( length > Integer.MAX_VALUE )
197            {
198                throw new IOException( I18n.err( I18n.ERR_618, file.getName() ) );
199            }
200    
201            // Create the byte array to hold the data.
202            byte[] bytes = new byte[( int ) length];
203    
204            // Read in the bytes
205            int offset = 0;
206            int numRead = 0;
207            while ( offset < bytes.length && ( numRead = is.read( bytes, offset, bytes.length - offset ) ) >= 0 )
208            {
209                offset += numRead;
210            }
211    
212            // Ensure all the bytes have been read in.
213            if ( offset < bytes.length )
214            {
215                throw new IOException( I18n.err( I18n.ERR_619, file.getName() ) );
216            }
217    
218            // Close the input stream and return bytes.
219            is.close();
220            return bytes;
221        }
222    
223    
224        /**
225         * Write the contents of the {@link ByteBuffer} to a {@link File}.
226         *
227         * @param buffer
228         * @param file
229         * @throws IOException
230         */
231        protected void writeFile( IoBuffer buffer, File file ) throws IOException
232        {
233            // Set append false to replace existing.
234            FileChannel wChannel = new FileOutputStream( file, false ).getChannel();
235    
236            // Write the bytes between the position and limit.
237            wChannel.write( buffer.buf() );
238    
239            wChannel.close();
240        }
241    }