jdk/src/share/classes/sun/security/krb5/internal/rcache/ReplayCache.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Portions Copyright 2000-2006 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *  (C) Copyright IBM Corp. 1999 All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *  Copyright 1997 The Open Group Research Institute.  All rights reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
package sun.security.krb5.internal.rcache;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import sun.security.krb5.KrbException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import sun.security.krb5.Config;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import sun.security.krb5.internal.Krb5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.LinkedList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.ListIterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import sun.security.krb5.internal.KerberosTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * This class provides an efficient caching mechanism to store the timestamp of client authenticators.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * The cache minimizes the memory usage by doing self-cleanup of expired items in the cache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * @author Yanni Zhang
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
public class ReplayCache extends LinkedList<AuthTime> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private static final long serialVersionUID = 2997933194993803994L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private String principal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private CacheTable table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private int nap = 10 * 60 * 1000; //10 minutes break
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private boolean DEBUG = Krb5.DEBUG;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * Constructs a ReplayCache for a client principal in specified <code>CacheTable</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * @param p client principal name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * @param ct CacheTable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    public ReplayCache (String p, CacheTable ct) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        principal = p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        table = ct;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * Puts the authenticator timestamp into the cache in descending order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * @param t <code>AuthTime</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    public synchronized void put(AuthTime t, long currentTime) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        if (this.size() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            addFirst(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            AuthTime temp = getFirst();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            if (temp.kerberosTime < t.kerberosTime) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                // in most cases, newly received authenticator has
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                // larger timestamp.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                addFirst(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            else if (temp.kerberosTime == t.kerberosTime) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                if (temp.cusec < t.cusec) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                    addFirst(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                //unless client clock being re-adjusted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                ListIterator<AuthTime> it = listIterator(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                while (it.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                    temp = it.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                    if (temp.kerberosTime < t.kerberosTime) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                        add(indexOf(temp), t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                        //we always put the bigger timestamp at the front.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                    else if (temp.kerberosTime == t.kerberosTime) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                        if (temp.cusec < t.cusec) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                            add(indexOf(temp), t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        // let us cleanup while we are here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        long timeLimit = currentTime - KerberosTime.getDefaultSkew() * 1000L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        ListIterator<AuthTime> it = listIterator(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        AuthTime temp = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        int index = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        while (it.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            //search expired timestamps.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            temp = it.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            if (temp.kerberosTime < timeLimit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                index = indexOf(temp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        if (index > -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                //remove expired timestamps from the list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                removeLast();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            } while(size() > index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            printList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        // if there are no entries in the replay cache,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        // remove the replay cache from the table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        if (this.size() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            table.remove(principal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            printList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * Printes out the debug message.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    private void printList() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        Object[] total = toArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        for (int i = 0; i < total.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            System.out.println("object " + i + ": " + ((AuthTime)total[i]).kerberosTime + "/"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                               + ((AuthTime)total[i]).cusec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
}