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.IOException;
25 import java.io.InputStream;
26 import java.io.OutputStream;
27 import java.nio.ByteBuffer;
28 import java.nio.channels.Channels;
29 import java.nio.channels.WritableByteChannel;
30 import java.nio.file.Files;
31 import java.util.ArrayList;
32 import java.util.Collections;
33 import java.util.List;
34
35 import org.apache.directory.server.i18n.I18n;
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74 public class Keytab
75 {
76
77
78
79 public static final byte[] VERSION_0X501_BYTES = new byte[]
80 { ( byte ) 0x05, ( byte ) 0x01 };
81
82
83 public static final short VERSION_0X501 = 0x0501;
84
85
86
87
88 public static final byte[] VERSION_0X502_BYTES = new byte[]
89 { ( byte ) 0x05, ( byte ) 0x02 };
90
91
92 public static final short VERSION_0X502 = 0x0502;
93
94 private byte[] keytabVersion = VERSION_0X502_BYTES;
95 private List<KeytabEntry> entries = new ArrayList<>();
96
97
98
99
100
101
102
103
104
105 public static Keytab read( File file ) throws IOException
106 {
107 ByteBuffer buffer = ByteBuffer.wrap( getBytesFromFile( file ) );
108 return readKeytab( buffer );
109 }
110
111
112
113
114
115
116
117
118 public static Keytab getInstance()
119 {
120 return new Keytab();
121 }
122
123
124
125
126
127
128
129
130 public void write( File file ) throws IOException
131 {
132 KeytabEncoderr/kerberos/shared/keytab/KeytabEncoder.html#KeytabEncoder">KeytabEncoder writer = new KeytabEncoder();
133 ByteBuffer buffer = writer.write( keytabVersion, entries );
134 writeFile( buffer, file );
135 }
136
137
138
139
140
141 public void setEntries( List<KeytabEntry> entries )
142 {
143 this.entries = entries;
144 }
145
146
147
148
149
150 public void setKeytabVersion( byte[] keytabVersion )
151 {
152 this.keytabVersion = keytabVersion;
153 }
154
155
156
157
158
159 public List<KeytabEntry> getEntries()
160 {
161 return Collections.unmodifiableList( entries );
162 }
163
164
165
166
167
168 public byte[] getKeytabVersion()
169 {
170 return keytabVersion;
171 }
172
173
174
175
176
177
178
179
180 static Keytab read( byte[] bytes ) throws IOException
181 {
182 ByteBuffer buffer = ByteBuffer.wrap( bytes );
183 return readKeytab( buffer );
184 }
185
186
187
188
189
190
191 ByteBuffer write()
192 {
193 KeytabEncoderr/kerberos/shared/keytab/KeytabEncoder.html#KeytabEncoder">KeytabEncoder writer = new KeytabEncoder();
194 return writer.write( keytabVersion, entries );
195 }
196
197
198
199
200
201
202
203
204 private static Keytab readKeytab( ByteBuffer buffer ) throws IOException
205 {
206 KeytabDecoderr/kerberos/shared/keytab/KeytabDecoder.html#KeytabDecoder">KeytabDecoder reader = new KeytabDecoder();
207 byte[] keytabVersion = reader.getKeytabVersion( buffer );
208 List<KeytabEntry> entries = reader.getKeytabEntries( buffer );
209
210 Keytaby/server/kerberos/shared/keytab/Keytab.html#Keytab">Keytab keytab = new Keytab();
211
212 keytab.setKeytabVersion( keytabVersion );
213 keytab.setEntries( entries );
214
215 return keytab;
216 }
217
218
219
220
221
222
223
224
225
226 protected static byte[] getBytesFromFile( File file ) throws IOException
227 {
228 try ( InputStream is = Files.newInputStream( file.toPath() ) )
229 {
230
231 long length = file.length();
232
233
234 if ( length > Integer.MAX_VALUE )
235 {
236 throw new IOException( I18n.err( I18n.ERR_618, file.getName() ) );
237 }
238
239
240 byte[] bytes = new byte[( int ) length];
241
242
243 int offset = 0;
244 int numRead = 0;
245 while ( offset < bytes.length && ( numRead = is.read( bytes, offset, bytes.length - offset ) ) >= 0 )
246 {
247 offset += numRead;
248 }
249
250
251 if ( offset < bytes.length )
252 {
253 throw new IOException( I18n.err( I18n.ERR_619, file.getName() ) );
254 }
255
256 return bytes;
257 }
258 }
259
260
261
262
263
264
265
266
267
268 protected void writeFile( ByteBuffer buffer, File file ) throws IOException
269 {
270
271 OutputStream out = Files.newOutputStream( file.toPath() );
272
273 try ( WritableByteChannel channel = Channels.newChannel( out ) )
274 {
275
276 channel.write( buffer );
277 }
278 finally
279 {
280 out.close();
281 }
282 }
283 }