View Javadoc

1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.directory.server.kerberos.shared.keytab;
21  
22  
23  import java.io.File;
24  import java.io.FileInputStream;
25  import java.io.FileOutputStream;
26  import java.io.IOException;
27  import java.io.InputStream;
28  import java.nio.channels.FileChannel;
29  import java.util.ArrayList;
30  import java.util.Collections;
31  import java.util.List;
32  
33  import org.apache.mina.common.ByteBuffer;
34  
35  
36  /**
37   * Keytab file.
38   *
39   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
40   * @version $Rev$, $Date$
41   */
42  public class Keytab
43  {
44      /**
45       * Byte array constant for keytab file format 5.1.
46       */
47      public static final byte[] VERSION_51 = new byte[]
48          { ( byte ) 0x05, ( byte ) 0x01 };
49  
50      /**
51       * Byte array constant for keytab file format 5.2.
52       */
53      public static final byte[] VERSION_52 = new byte[]
54          { ( byte ) 0x05, ( byte ) 0x02 };
55  
56      private byte[] keytabVersion = VERSION_52;
57      private List<KeytabEntry> entries = new ArrayList<KeytabEntry>();
58  
59  
60      /**
61       * Read a keytab file.
62       *
63       * @param file
64       * @return The keytab.
65       * @throws IOException
66       */
67      public static Keytab read( File file ) throws IOException
68      {
69          ByteBuffer buffer = ByteBuffer.wrap( getBytesFromFile( file ) );
70          return readKeytab( buffer );
71      }
72  
73  
74      /**
75       * Returns a new instance of a keytab with the version
76       * defaulted to 5.2.
77       *
78       * @return The keytab.
79       */
80      public static Keytab getInstance()
81      {
82          return new Keytab();
83      }
84  
85  
86      /**
87       * Write the keytab to a {@link File}.
88       *
89       * @param file
90       * @throws IOException
91       */
92      public void write( File file ) throws IOException
93      {
94          KeytabEncoder writer = new KeytabEncoder();
95          ByteBuffer buffer = writer.write( keytabVersion, entries );
96          writeFile( buffer, file );
97      }
98  
99  
100     /**
101      * @param entries The entries to set.
102      */
103     public void setEntries( List<KeytabEntry> entries )
104     {
105         this.entries = entries;
106     }
107 
108 
109     /**
110      * @param keytabVersion The keytabVersion to set.
111      */
112     public void setKeytabVersion( byte[] keytabVersion )
113     {
114         this.keytabVersion = keytabVersion;
115     }
116 
117 
118     /**
119      * @return The entries.
120      */
121     public List<KeytabEntry> getEntries()
122     {
123         return Collections.unmodifiableList( entries );
124     }
125 
126 
127     /**
128      * @return The keytabVersion.
129      */
130     public byte[] getKeytabVersion()
131     {
132         return keytabVersion;
133     }
134 
135 
136     /**
137      * Read bytes into a keytab.
138      *
139      * @param bytes
140      * @return The keytab.
141      */
142     static Keytab read( byte[] bytes )
143     {
144         ByteBuffer buffer = ByteBuffer.wrap( bytes );
145         return readKeytab( buffer );
146     }
147 
148 
149     /**
150      * Write the keytab to a {@link ByteBuffer}.
151      * @return The buffer.
152      */
153     ByteBuffer write()
154     {
155         KeytabEncoder writer = new KeytabEncoder();
156         return writer.write( keytabVersion, entries );
157     }
158 
159 
160     /**
161      * Read the contents of the buffer into a keytab.
162      *
163      * @param buffer
164      * @return The keytab.
165      */
166     private static Keytab readKeytab( ByteBuffer buffer )
167     {
168         KeytabDecoder reader = new KeytabDecoder();
169         byte[] keytabVersion = reader.getKeytabVersion( buffer );
170         List<KeytabEntry> entries = reader.getKeytabEntries( buffer );
171 
172         Keytab keytab = new Keytab();
173 
174         keytab.setKeytabVersion( keytabVersion );
175         keytab.setEntries( entries );
176 
177         return keytab;
178     }
179 
180 
181     /**
182      * Returns the contents of the {@link File} in a byte array.
183      *
184      * @param file
185      * @return The byte array of the file contents.
186      * @throws IOException
187      */
188     protected static byte[] getBytesFromFile( File file ) throws IOException
189     {
190         InputStream is = new FileInputStream( file );
191 
192         long length = file.length();
193 
194         // Check to ensure that file is not larger than Integer.MAX_VALUE.
195         if ( length > Integer.MAX_VALUE )
196         {
197             throw new IOException( "File is too large " + file.getName() );
198         }
199 
200         // Create the byte array to hold the data.
201         byte[] bytes = new byte[( int ) length];
202 
203         // Read in the bytes
204         int offset = 0;
205         int numRead = 0;
206         while ( offset < bytes.length && ( numRead = is.read( bytes, offset, bytes.length - offset ) ) >= 0 )
207         {
208             offset += numRead;
209         }
210 
211         // Ensure all the bytes have been read in.
212         if ( offset < bytes.length )
213         {
214             throw new IOException( "Could not completely read file " + file.getName() );
215         }
216 
217         // Close the input stream and return bytes.
218         is.close();
219         return bytes;
220     }
221 
222 
223     /**
224      * Write the contents of the {@link ByteBuffer} to a {@link File}.
225      *
226      * @param buffer
227      * @param file
228      * @throws IOException
229      */
230     protected void writeFile( ByteBuffer buffer, File file ) throws IOException
231     {
232         // Set append false to replace existing.
233         FileChannel wChannel = new FileOutputStream( file, false ).getChannel();
234 
235         // Write the bytes between the position and limit.
236         wChannel.write( buffer.buf() );
237 
238         wChannel.close();
239     }
240 }