1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
38
39
40
41
42 public class Keytab
43 {
44
45
46
47 public static final byte[] VERSION_51 = new byte[]
48 { ( byte ) 0x05, ( byte ) 0x01 };
49
50
51
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
62
63
64
65
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
76
77
78
79
80 public static Keytab getInstance()
81 {
82 return new Keytab();
83 }
84
85
86
87
88
89
90
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
102
103 public void setEntries( List<KeytabEntry> entries )
104 {
105 this.entries = entries;
106 }
107
108
109
110
111
112 public void setKeytabVersion( byte[] keytabVersion )
113 {
114 this.keytabVersion = keytabVersion;
115 }
116
117
118
119
120
121 public List<KeytabEntry> getEntries()
122 {
123 return Collections.unmodifiableList( entries );
124 }
125
126
127
128
129
130 public byte[] getKeytabVersion()
131 {
132 return keytabVersion;
133 }
134
135
136
137
138
139
140
141
142 static Keytab read( byte[] bytes )
143 {
144 ByteBuffer buffer = ByteBuffer.wrap( bytes );
145 return readKeytab( buffer );
146 }
147
148
149
150
151
152
153 ByteBuffer write()
154 {
155 KeytabEncoder writer = new KeytabEncoder();
156 return writer.write( keytabVersion, entries );
157 }
158
159
160
161
162
163
164
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
183
184
185
186
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
195 if ( length > Integer.MAX_VALUE )
196 {
197 throw new IOException( "File is too large " + file.getName() );
198 }
199
200
201 byte[] bytes = new byte[( int ) length];
202
203
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
212 if ( offset < bytes.length )
213 {
214 throw new IOException( "Could not completely read file " + file.getName() );
215 }
216
217
218 is.close();
219 return bytes;
220 }
221
222
223
224
225
226
227
228
229
230 protected void writeFile( ByteBuffer buffer, File file ) throws IOException
231 {
232
233 FileChannel wChannel = new FileOutputStream( file, false ).getChannel();
234
235
236 wChannel.write( buffer.buf() );
237
238 wChannel.close();
239 }
240 }