2
|
1 |
/*
|
|
2 |
* Portions Copyright 2000-2006 Sun Microsystems, Inc. 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. Sun designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
23 |
* have any questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
/*
|
|
27 |
*
|
|
28 |
* (C) Copyright IBM Corp. 1999 All Rights Reserved.
|
|
29 |
* Copyright 1997 The Open Group Research Institute. All rights reserved.
|
|
30 |
*/
|
|
31 |
|
|
32 |
package sun.security.krb5.internal.rcache;
|
|
33 |
|
|
34 |
import sun.security.krb5.KrbException;
|
|
35 |
import sun.security.krb5.Config;
|
|
36 |
import sun.security.krb5.internal.Krb5;
|
|
37 |
import java.util.LinkedList;
|
|
38 |
import java.util.ListIterator;
|
|
39 |
import sun.security.krb5.internal.KerberosTime;
|
|
40 |
|
|
41 |
/**
|
|
42 |
* This class provides an efficient caching mechanism to store the timestamp of client authenticators.
|
|
43 |
* The cache minimizes the memory usage by doing self-cleanup of expired items in the cache.
|
|
44 |
*
|
|
45 |
* @author Yanni Zhang
|
|
46 |
*/
|
|
47 |
public class ReplayCache extends LinkedList<AuthTime> {
|
|
48 |
|
|
49 |
private static final long serialVersionUID = 2997933194993803994L;
|
|
50 |
|
|
51 |
private String principal;
|
|
52 |
private CacheTable table;
|
|
53 |
private int nap = 10 * 60 * 1000; //10 minutes break
|
|
54 |
private boolean DEBUG = Krb5.DEBUG;
|
|
55 |
/**
|
|
56 |
* Constructs a ReplayCache for a client principal in specified <code>CacheTable</code>.
|
|
57 |
* @param p client principal name.
|
|
58 |
* @param ct CacheTable.
|
|
59 |
*/
|
|
60 |
public ReplayCache (String p, CacheTable ct) {
|
|
61 |
principal = p;
|
|
62 |
table = ct;
|
|
63 |
}
|
|
64 |
|
|
65 |
/**
|
|
66 |
* Puts the authenticator timestamp into the cache in descending order.
|
|
67 |
* @param t <code>AuthTime</code>
|
|
68 |
*/
|
|
69 |
public synchronized void put(AuthTime t, long currentTime) {
|
|
70 |
|
|
71 |
if (this.size() == 0) {
|
|
72 |
addFirst(t);
|
|
73 |
}
|
|
74 |
else {
|
|
75 |
AuthTime temp = getFirst();
|
|
76 |
if (temp.kerberosTime < t.kerberosTime) {
|
|
77 |
// in most cases, newly received authenticator has
|
|
78 |
// larger timestamp.
|
|
79 |
addFirst(t);
|
|
80 |
}
|
|
81 |
else if (temp.kerberosTime == t.kerberosTime) {
|
|
82 |
if (temp.cusec < t.cusec) {
|
|
83 |
addFirst(t);
|
|
84 |
}
|
|
85 |
}
|
|
86 |
else {
|
|
87 |
//unless client clock being re-adjusted.
|
|
88 |
ListIterator<AuthTime> it = listIterator(1);
|
|
89 |
while (it.hasNext()) {
|
|
90 |
temp = it.next();
|
|
91 |
if (temp.kerberosTime < t.kerberosTime) {
|
|
92 |
add(indexOf(temp), t);
|
|
93 |
break;
|
|
94 |
//we always put the bigger timestamp at the front.
|
|
95 |
}
|
|
96 |
else if (temp.kerberosTime == t.kerberosTime) {
|
|
97 |
if (temp.cusec < t.cusec) {
|
|
98 |
add(indexOf(temp), t);
|
|
99 |
break;
|
|
100 |
}
|
|
101 |
}
|
|
102 |
}
|
|
103 |
}
|
|
104 |
}
|
|
105 |
|
|
106 |
// let us cleanup while we are here
|
|
107 |
long timeLimit = currentTime - KerberosTime.getDefaultSkew() * 1000L;
|
|
108 |
ListIterator<AuthTime> it = listIterator(0);
|
|
109 |
AuthTime temp = null;
|
|
110 |
int index = -1;
|
|
111 |
while (it.hasNext()) {
|
|
112 |
//search expired timestamps.
|
|
113 |
temp = it.next();
|
|
114 |
if (temp.kerberosTime < timeLimit) {
|
|
115 |
index = indexOf(temp);
|
|
116 |
break;
|
|
117 |
}
|
|
118 |
}
|
|
119 |
if (index > -1) {
|
|
120 |
do {
|
|
121 |
//remove expired timestamps from the list.
|
|
122 |
removeLast();
|
|
123 |
} while(size() > index);
|
|
124 |
}
|
|
125 |
if (DEBUG) {
|
|
126 |
printList();
|
|
127 |
}
|
|
128 |
|
|
129 |
// if there are no entries in the replay cache,
|
|
130 |
// remove the replay cache from the table.
|
|
131 |
if (this.size() == 0) {
|
|
132 |
table.remove(principal);
|
|
133 |
}
|
|
134 |
if (DEBUG) {
|
|
135 |
printList();
|
|
136 |
}
|
|
137 |
}
|
|
138 |
|
|
139 |
|
|
140 |
/**
|
|
141 |
* Printes out the debug message.
|
|
142 |
*/
|
|
143 |
private void printList() {
|
|
144 |
Object[] total = toArray();
|
|
145 |
for (int i = 0; i < total.length; i++) {
|
|
146 |
System.out.println("object " + i + ": " + ((AuthTime)total[i]).kerberosTime + "/"
|
|
147 |
+ ((AuthTime)total[i]).cusec);
|
|
148 |
}
|
|
149 |
}
|
|
150 |
|
|
151 |
}
|