author | weijun |
Thu, 08 May 2014 22:30:31 +0800 | |
changeset 24271 | 19000122bb5e |
parent 18536 | 092411ced388 |
child 24506 | abb4cc4647ee |
permissions | -rw-r--r-- |
18536 | 1 |
/* |
2 |
* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. |
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
||
26 |
||
27 |
package sun.security.krb5.internal.rcache; |
|
28 |
||
29 |
import java.io.*; |
|
30 |
import java.nio.BufferUnderflowException; |
|
31 |
import java.nio.ByteBuffer; |
|
32 |
import java.nio.ByteOrder; |
|
33 |
import java.nio.channels.SeekableByteChannel; |
|
34 |
import java.nio.file.Files; |
|
35 |
import java.nio.file.Path; |
|
36 |
import java.nio.file.StandardCopyOption; |
|
37 |
import java.nio.file.StandardOpenOption; |
|
38 |
import java.nio.file.attribute.PosixFilePermission; |
|
39 |
import java.security.AccessController; |
|
40 |
import java.util.*; |
|
41 |
||
24271
19000122bb5e
8039951: com.sun.security.auth.module missing classes on some platforms
weijun
parents:
18536
diff
changeset
|
42 |
import com.sun.security.auth.module.UnixSystem; |
18536 | 43 |
import sun.security.action.GetPropertyAction; |
44 |
import sun.security.krb5.internal.KerberosTime; |
|
45 |
import sun.security.krb5.internal.Krb5; |
|
46 |
import sun.security.krb5.internal.KrbApErrException; |
|
47 |
import sun.security.krb5.internal.ReplayCache; |
|
48 |
||
49 |
||
50 |
/** |
|
51 |
* A dfl file is used to sustores AuthTime entries when the system property |
|
52 |
* sun.security.krb5.rcache is set to |
|
53 |
* |
|
54 |
* dfl(|:path/|:path/name|:name) |
|
55 |
* |
|
56 |
* The file will be path/name. If path is not given, it will be |
|
57 |
* |
|
58 |
* System.getProperty("java.io.tmpdir") |
|
59 |
* |
|
60 |
* If name is not given, it will be |
|
61 |
* |
|
62 |
* service_euid |
|
63 |
* |
|
64 |
* Java does not have a method to get euid, so uid is used instead. This |
|
65 |
* should normally to be since a Java program is seldom used as a setuid app. |
|
66 |
* |
|
67 |
* The file has a header: |
|
68 |
* |
|
69 |
* i16 0x0501 (KRB5_RC_VNO) in network order |
|
70 |
* i32 number of seconds for lifespan (in native order, same below) |
|
71 |
* |
|
72 |
* followed by cache entries concatenated, which can be encoded in |
|
73 |
* 2 styles: |
|
74 |
* |
|
75 |
* The traditional style is: |
|
76 |
* |
|
77 |
* LC of client principal |
|
78 |
* LC of server principal |
|
79 |
* i32 cusec of Authenticator |
|
80 |
* i32 ctime of Authenticator |
|
81 |
* |
|
82 |
* The new style has a hash: |
|
83 |
* |
|
84 |
* LC of "" |
|
85 |
* LC of "HASH:%s %lu:%s %lu:%s" of (hash, clientlen, client, serverlen, |
|
86 |
* server) where msghash is 32 char (lower case) text mode md5sum |
|
87 |
* of the ciphertext of authenticator. |
|
88 |
* i32 cusec of Authenticator |
|
89 |
* i32 ctime of Authenticator |
|
90 |
* |
|
91 |
* where LC of a string means |
|
92 |
* |
|
93 |
* i32 strlen(string) + 1 |
|
94 |
* octets of string, with the \0x00 ending |
|
95 |
* |
|
96 |
* The old style block is always created by MIT krb5 used even if a new style |
|
97 |
* is available, which means there can be 2 entries for a single Authenticator. |
|
98 |
* Java also does this way. |
|
99 |
* |
|
100 |
* See src/lib/krb5/rcache/rc_io.c and src/lib/krb5/rcache/rc_dfl.c. |
|
101 |
*/ |
|
102 |
public class DflCache extends ReplayCache { |
|
103 |
||
104 |
private static final int KRB5_RV_VNO = 0x501; |
|
105 |
private static final int EXCESSREPS = 30; // if missed-hit>this, recreate |
|
106 |
||
107 |
private final String source; |
|
108 |
||
24271
19000122bb5e
8039951: com.sun.security.auth.module missing classes on some platforms
weijun
parents:
18536
diff
changeset
|
109 |
private static long uid; |
18536 | 110 |
static { |
111 |
try { |
|
112 |
// Available on Solaris, Linux and Mac. Otherwise, no _euid suffix |
|
24271
19000122bb5e
8039951: com.sun.security.auth.module missing classes on some platforms
weijun
parents:
18536
diff
changeset
|
113 |
UnixSystem us = new com.sun.security.auth.module.UnixSystem(); |
19000122bb5e
8039951: com.sun.security.auth.module missing classes on some platforms
weijun
parents:
18536
diff
changeset
|
114 |
uid = us.getUid(); |
19000122bb5e
8039951: com.sun.security.auth.module missing classes on some platforms
weijun
parents:
18536
diff
changeset
|
115 |
} catch (Throwable e) { |
19000122bb5e
8039951: com.sun.security.auth.module missing classes on some platforms
weijun
parents:
18536
diff
changeset
|
116 |
// Cannot be only Exception, might be UnsatisfiedLinkError |
18536 | 117 |
uid = -1; |
118 |
} |
|
119 |
} |
|
120 |
||
121 |
public DflCache (String source) { |
|
122 |
this.source = source; |
|
123 |
} |
|
124 |
||
125 |
private static String defaultPath() { |
|
126 |
return AccessController.doPrivileged( |
|
127 |
new GetPropertyAction("java.io.tmpdir")); |
|
128 |
} |
|
129 |
||
130 |
private static String defaultFile(String server) { |
|
131 |
// service/host@REALM -> service |
|
132 |
int slash = server.indexOf('/'); |
|
133 |
if (slash == -1) { |
|
134 |
// A normal principal? say, dummy@REALM |
|
135 |
slash = server.indexOf('@'); |
|
136 |
} |
|
137 |
if (slash != -1) { |
|
138 |
// Should not happen, but be careful |
|
139 |
server= server.substring(0, slash); |
|
140 |
} |
|
141 |
if (uid != -1) { |
|
142 |
server += "_" + uid; |
|
143 |
} |
|
144 |
return server; |
|
145 |
} |
|
146 |
||
147 |
private static Path getFileName(String source, String server) { |
|
148 |
String path, file; |
|
149 |
if (source.equals("dfl")) { |
|
150 |
path = defaultPath(); |
|
151 |
file = defaultFile(server); |
|
152 |
} else if (source.startsWith("dfl:")) { |
|
153 |
source = source.substring(4); |
|
154 |
int pos = source.lastIndexOf('/'); |
|
155 |
int pos1 = source.lastIndexOf('\\'); |
|
156 |
if (pos1 > pos) pos = pos1; |
|
157 |
if (pos == -1) { |
|
158 |
// Only file name |
|
159 |
path = defaultPath(); |
|
160 |
file = source; |
|
161 |
} else if (new File(source).isDirectory()) { |
|
162 |
// Only path |
|
163 |
path = source; |
|
164 |
file = defaultFile(server); |
|
165 |
} else { |
|
166 |
// Full pathname |
|
167 |
path = null; |
|
168 |
file = source; |
|
169 |
} |
|
170 |
} else { |
|
171 |
throw new IllegalArgumentException(); |
|
172 |
} |
|
173 |
return new File(path, file).toPath(); |
|
174 |
} |
|
175 |
||
176 |
@Override |
|
177 |
public void checkAndStore(KerberosTime currTime, AuthTimeWithHash time) |
|
178 |
throws KrbApErrException { |
|
179 |
try { |
|
180 |
checkAndStore0(currTime, time); |
|
181 |
} catch (IOException ioe) { |
|
182 |
KrbApErrException ke = new KrbApErrException(Krb5.KRB_ERR_GENERIC); |
|
183 |
ke.initCause(ioe); |
|
184 |
throw ke; |
|
185 |
} |
|
186 |
} |
|
187 |
||
188 |
private synchronized void checkAndStore0(KerberosTime currTime, AuthTimeWithHash time) |
|
189 |
throws IOException, KrbApErrException { |
|
190 |
Path p = getFileName(source, time.server); |
|
191 |
int missed = 0; |
|
192 |
try (Storage s = new Storage()) { |
|
193 |
try { |
|
194 |
missed = s.loadAndCheck(p, time, currTime); |
|
195 |
} catch (IOException ioe) { |
|
196 |
// Non-existing or invalid file |
|
197 |
Storage.create(p); |
|
198 |
missed = s.loadAndCheck(p, time, currTime); |
|
199 |
} |
|
200 |
s.append(time); |
|
201 |
} |
|
202 |
if (missed > EXCESSREPS) { |
|
203 |
Storage.expunge(p, currTime); |
|
204 |
} |
|
205 |
} |
|
206 |
||
207 |
||
208 |
private static class Storage implements Closeable { |
|
209 |
// Static methods |
|
210 |
@SuppressWarnings("try") |
|
211 |
private static void create(Path p) throws IOException { |
|
212 |
try (SeekableByteChannel newChan = createNoClose(p)) { |
|
213 |
// Do nothing, wait for close |
|
214 |
} |
|
215 |
makeMine(p); |
|
216 |
} |
|
217 |
||
218 |
private static void makeMine(Path p) throws IOException { |
|
219 |
// chmod to owner-rw only, otherwise MIT krb5 rejects |
|
220 |
try { |
|
221 |
Set<PosixFilePermission> attrs = new HashSet<>(); |
|
222 |
attrs.add(PosixFilePermission.OWNER_READ); |
|
223 |
attrs.add(PosixFilePermission.OWNER_WRITE); |
|
224 |
Files.setPosixFilePermissions(p, attrs); |
|
225 |
} catch (UnsupportedOperationException uoe) { |
|
226 |
// No POSIX permission. That's OK. |
|
227 |
} |
|
228 |
} |
|
229 |
||
230 |
private static SeekableByteChannel createNoClose(Path p) |
|
231 |
throws IOException { |
|
232 |
SeekableByteChannel newChan = Files.newByteChannel( |
|
233 |
p, StandardOpenOption.CREATE, |
|
234 |
StandardOpenOption.TRUNCATE_EXISTING, |
|
235 |
StandardOpenOption.WRITE); |
|
236 |
ByteBuffer buffer = ByteBuffer.allocate(6); |
|
237 |
buffer.putShort((short)KRB5_RV_VNO); |
|
238 |
buffer.order(ByteOrder.nativeOrder()); |
|
239 |
buffer.putInt(KerberosTime.getDefaultSkew()); |
|
240 |
buffer.flip(); |
|
241 |
newChan.write(buffer); |
|
242 |
return newChan; |
|
243 |
} |
|
244 |
||
245 |
private static void expunge(Path p, KerberosTime currTime) |
|
246 |
throws IOException { |
|
247 |
Path p2 = Files.createTempFile(p.getParent(), "rcache", null); |
|
248 |
try (SeekableByteChannel oldChan = Files.newByteChannel(p); |
|
249 |
SeekableByteChannel newChan = createNoClose(p2)) { |
|
250 |
long timeLimit = currTime.getSeconds() - readHeader(oldChan); |
|
251 |
while (true) { |
|
252 |
try { |
|
253 |
AuthTime at = AuthTime.readFrom(oldChan); |
|
254 |
if (at.ctime > timeLimit) { |
|
255 |
ByteBuffer bb = ByteBuffer.wrap(at.encode(true)); |
|
256 |
newChan.write(bb); |
|
257 |
} |
|
258 |
} catch (BufferUnderflowException e) { |
|
259 |
break; |
|
260 |
} |
|
261 |
} |
|
262 |
} |
|
263 |
makeMine(p2); |
|
264 |
Files.move(p2, p, |
|
265 |
StandardCopyOption.REPLACE_EXISTING, |
|
266 |
StandardCopyOption.ATOMIC_MOVE); |
|
267 |
} |
|
268 |
||
269 |
// Instance methods |
|
270 |
SeekableByteChannel chan; |
|
271 |
private int loadAndCheck(Path p, AuthTimeWithHash time, |
|
272 |
KerberosTime currTime) |
|
273 |
throws IOException, KrbApErrException { |
|
274 |
int missed = 0; |
|
275 |
if (Files.isSymbolicLink(p)) { |
|
276 |
throw new IOException("Symlink not accepted"); |
|
277 |
} |
|
278 |
try { |
|
279 |
Set<PosixFilePermission> perms = |
|
280 |
Files.getPosixFilePermissions(p); |
|
281 |
if (uid != -1 && |
|
282 |
(Integer)Files.getAttribute(p, "unix:uid") != uid) { |
|
283 |
throw new IOException("Not mine"); |
|
284 |
} |
|
285 |
if (perms.contains(PosixFilePermission.GROUP_READ) || |
|
286 |
perms.contains(PosixFilePermission.GROUP_WRITE) || |
|
287 |
perms.contains(PosixFilePermission.GROUP_EXECUTE) || |
|
288 |
perms.contains(PosixFilePermission.OTHERS_READ) || |
|
289 |
perms.contains(PosixFilePermission.OTHERS_WRITE) || |
|
290 |
perms.contains(PosixFilePermission.OTHERS_EXECUTE)) { |
|
291 |
throw new IOException("Accessible by someone else"); |
|
292 |
} |
|
293 |
} catch (UnsupportedOperationException uoe) { |
|
294 |
// No POSIX permissions? Ignore it. |
|
295 |
} |
|
296 |
chan = Files.newByteChannel(p, StandardOpenOption.WRITE, |
|
297 |
StandardOpenOption.READ); |
|
298 |
||
299 |
long timeLimit = currTime.getSeconds() - readHeader(chan); |
|
300 |
||
301 |
long pos = 0; |
|
302 |
boolean seeNewButNotSame = false; |
|
303 |
while (true) { |
|
304 |
try { |
|
305 |
pos = chan.position(); |
|
306 |
AuthTime a = AuthTime.readFrom(chan); |
|
307 |
if (a instanceof AuthTimeWithHash) { |
|
308 |
if (time.equals(a)) { |
|
309 |
// Exact match, must be a replay |
|
310 |
throw new KrbApErrException(Krb5.KRB_AP_ERR_REPEAT); |
|
311 |
} else if (time.isSameIgnoresHash(a)) { |
|
312 |
// Two different authenticators in the same second. |
|
313 |
// Remember it |
|
314 |
seeNewButNotSame = true; |
|
315 |
} |
|
316 |
} else { |
|
317 |
if (time.isSameIgnoresHash(a)) { |
|
318 |
// Two authenticators in the same second. Considered |
|
319 |
// same if we haven't seen a new style version of it |
|
320 |
if (!seeNewButNotSame) { |
|
321 |
throw new KrbApErrException(Krb5.KRB_AP_ERR_REPEAT); |
|
322 |
} |
|
323 |
} |
|
324 |
} |
|
325 |
if (a.ctime < timeLimit) { |
|
326 |
missed++; |
|
327 |
} else { |
|
328 |
missed--; |
|
329 |
} |
|
330 |
} catch (BufferUnderflowException e) { |
|
331 |
// Half-written file? |
|
332 |
chan.position(pos); |
|
333 |
break; |
|
334 |
} |
|
335 |
} |
|
336 |
return missed; |
|
337 |
} |
|
338 |
||
339 |
private static int readHeader(SeekableByteChannel chan) |
|
340 |
throws IOException { |
|
341 |
ByteBuffer bb = ByteBuffer.allocate(6); |
|
342 |
chan.read(bb); |
|
343 |
if (bb.getShort(0) != KRB5_RV_VNO) { |
|
344 |
throw new IOException("Not correct rcache version"); |
|
345 |
} |
|
346 |
bb.order(ByteOrder.nativeOrder()); |
|
347 |
return bb.getInt(2); |
|
348 |
} |
|
349 |
||
350 |
private void append(AuthTimeWithHash at) throws IOException { |
|
351 |
// Write an entry with hash, to be followed by one without it, |
|
352 |
// for the benefit of old implementations. |
|
353 |
ByteBuffer bb; |
|
354 |
bb = ByteBuffer.wrap(at.encode(true)); |
|
355 |
chan.write(bb); |
|
356 |
bb = ByteBuffer.wrap(at.encode(false)); |
|
357 |
chan.write(bb); |
|
358 |
} |
|
359 |
||
360 |
@Override |
|
361 |
public void close() throws IOException { |
|
362 |
if (chan != null) chan.close(); |
|
363 |
chan = null; |
|
364 |
} |
|
365 |
} |
|
366 |
} |