src/java.security.jgss/share/classes/sun/security/krb5/internal/rcache/AuthList.java
author mbalao
Wed, 05 Jun 2019 01:42:11 -0300
changeset 55258 d65d3c37232c
parent 48985 3fbc7f109dad
permissions -rw-r--r--
8215032: Support Kerberos cross-realm referrals (RFC 6806) Reviewed-by: weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
48985
3fbc7f109dad 8197518: Kerberos krb5 authentication: AuthList's put method leads to performance issue
weijun
parents: 47216
diff changeset
     2
 * Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
2
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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
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.internal.Krb5;
18536
092411ced388 8001326: Improve Kerberos caching
weijun
parents: 14342
diff changeset
    35
092411ced388 8001326: Improve Kerberos caching
weijun
parents: 14342
diff changeset
    36
import java.util.Iterator;
2
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;
18536
092411ced388 8001326: Improve Kerberos caching
weijun
parents: 14342
diff changeset
    40
import sun.security.krb5.internal.KrbApErrException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
/**
18536
092411ced388 8001326: Improve Kerberos caching
weijun
parents: 14342
diff changeset
    43
 * This class provides an efficient caching mechanism to store AuthTimeWithHash
092411ced388 8001326: Improve Kerberos caching
weijun
parents: 14342
diff changeset
    44
 * from client authenticators. The cache minimizes the memory usage by doing
092411ced388 8001326: Improve Kerberos caching
weijun
parents: 14342
diff changeset
    45
 * self-cleanup of expired items in the cache.
092411ced388 8001326: Improve Kerberos caching
weijun
parents: 14342
diff changeset
    46
 *
092411ced388 8001326: Improve Kerberos caching
weijun
parents: 14342
diff changeset
    47
 * AuthTimeWithHash objects inside a cache are always sorted from big (new) to
32003
acb12269398a 8132130: some docs cleanup
avstepan
parents: 25859
diff changeset
    48
 * small (old) as determined by {@link AuthTimeWithHash#compareTo}. In the most
18536
092411ced388 8001326: Improve Kerberos caching
weijun
parents: 14342
diff changeset
    49
 * common case a newcomer should be newer than the first element.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * @author Yanni Zhang
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 */
18536
092411ced388 8001326: Improve Kerberos caching
weijun
parents: 14342
diff changeset
    53
public class AuthList {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
18536
092411ced388 8001326: Improve Kerberos caching
weijun
parents: 14342
diff changeset
    55
    private final LinkedList<AuthTimeWithHash> entries;
092411ced388 8001326: Improve Kerberos caching
weijun
parents: 14342
diff changeset
    56
    private final int lifespan;
11526
efcd7eae978f 7118809: rcache deadlock
weijun
parents: 5506
diff changeset
    57
48985
3fbc7f109dad 8197518: Kerberos krb5 authentication: AuthList's put method leads to performance issue
weijun
parents: 47216
diff changeset
    58
    // entries.getLast().ctime, updated after each cleanup.
3fbc7f109dad 8197518: Kerberos krb5 authentication: AuthList's put method leads to performance issue
weijun
parents: 47216
diff changeset
    59
    private volatile int oldestTime = Integer.MIN_VALUE;
3fbc7f109dad 8197518: Kerberos krb5 authentication: AuthList's put method leads to performance issue
weijun
parents: 47216
diff changeset
    60
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    /**
18536
092411ced388 8001326: Improve Kerberos caching
weijun
parents: 14342
diff changeset
    62
     * Constructs a AuthList.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     */
18536
092411ced388 8001326: Improve Kerberos caching
weijun
parents: 14342
diff changeset
    64
    public AuthList(int lifespan) {
092411ced388 8001326: Improve Kerberos caching
weijun
parents: 14342
diff changeset
    65
        this.lifespan = lifespan;
092411ced388 8001326: Improve Kerberos caching
weijun
parents: 14342
diff changeset
    66
        entries = new LinkedList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    /**
18536
092411ced388 8001326: Improve Kerberos caching
weijun
parents: 14342
diff changeset
    70
     * Puts the authenticator timestamp into the cache in descending order,
092411ced388 8001326: Improve Kerberos caching
weijun
parents: 14342
diff changeset
    71
     * and throw an exception if it's already there.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     */
48985
3fbc7f109dad 8197518: Kerberos krb5 authentication: AuthList's put method leads to performance issue
weijun
parents: 47216
diff changeset
    73
    public synchronized void put(AuthTimeWithHash t, KerberosTime currentTime)
18536
092411ced388 8001326: Improve Kerberos caching
weijun
parents: 14342
diff changeset
    74
            throws KrbApErrException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
18536
092411ced388 8001326: Improve Kerberos caching
weijun
parents: 14342
diff changeset
    76
        if (entries.isEmpty()) {
092411ced388 8001326: Improve Kerberos caching
weijun
parents: 14342
diff changeset
    77
            entries.addFirst(t);
48985
3fbc7f109dad 8197518: Kerberos krb5 authentication: AuthList's put method leads to performance issue
weijun
parents: 47216
diff changeset
    78
            oldestTime = t.ctime;
3fbc7f109dad 8197518: Kerberos krb5 authentication: AuthList's put method leads to performance issue
weijun
parents: 47216
diff changeset
    79
            return;
18536
092411ced388 8001326: Improve Kerberos caching
weijun
parents: 14342
diff changeset
    80
        } else {
092411ced388 8001326: Improve Kerberos caching
weijun
parents: 14342
diff changeset
    81
            AuthTimeWithHash temp = entries.getFirst();
092411ced388 8001326: Improve Kerberos caching
weijun
parents: 14342
diff changeset
    82
            int cmp = temp.compareTo(t);
092411ced388 8001326: Improve Kerberos caching
weijun
parents: 14342
diff changeset
    83
            if (cmp < 0) {
092411ced388 8001326: Improve Kerberos caching
weijun
parents: 14342
diff changeset
    84
                // This is the most common case, newly received authenticator
092411ced388 8001326: Improve Kerberos caching
weijun
parents: 14342
diff changeset
    85
                // has larger timestamp.
092411ced388 8001326: Improve Kerberos caching
weijun
parents: 14342
diff changeset
    86
                entries.addFirst(t);
092411ced388 8001326: Improve Kerberos caching
weijun
parents: 14342
diff changeset
    87
            } else if (cmp == 0) {
092411ced388 8001326: Improve Kerberos caching
weijun
parents: 14342
diff changeset
    88
                throw new KrbApErrException(Krb5.KRB_AP_ERR_REPEAT);
092411ced388 8001326: Improve Kerberos caching
weijun
parents: 14342
diff changeset
    89
            } else {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                //unless client clock being re-adjusted.
18536
092411ced388 8001326: Improve Kerberos caching
weijun
parents: 14342
diff changeset
    91
                ListIterator<AuthTimeWithHash> it = entries.listIterator(1);
092411ced388 8001326: Improve Kerberos caching
weijun
parents: 14342
diff changeset
    92
                boolean found = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                while (it.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                    temp = it.next();
18536
092411ced388 8001326: Improve Kerberos caching
weijun
parents: 14342
diff changeset
    95
                    cmp = temp.compareTo(t);
092411ced388 8001326: Improve Kerberos caching
weijun
parents: 14342
diff changeset
    96
                    if (cmp < 0) {
092411ced388 8001326: Improve Kerberos caching
weijun
parents: 14342
diff changeset
    97
                        // Find an older one, put in front of it
092411ced388 8001326: Improve Kerberos caching
weijun
parents: 14342
diff changeset
    98
                        entries.add(entries.indexOf(temp), t);
092411ced388 8001326: Improve Kerberos caching
weijun
parents: 14342
diff changeset
    99
                        found = true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                        break;
18536
092411ced388 8001326: Improve Kerberos caching
weijun
parents: 14342
diff changeset
   101
                    } else if (cmp == 0) {
092411ced388 8001326: Improve Kerberos caching
weijun
parents: 14342
diff changeset
   102
                        throw new KrbApErrException(Krb5.KRB_AP_ERR_REPEAT);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                    }
18536
092411ced388 8001326: Improve Kerberos caching
weijun
parents: 14342
diff changeset
   104
                }
092411ced388 8001326: Improve Kerberos caching
weijun
parents: 14342
diff changeset
   105
                if (!found) {
092411ced388 8001326: Improve Kerberos caching
weijun
parents: 14342
diff changeset
   106
                    // All is newer than the newcomer. Sigh.
092411ced388 8001326: Improve Kerberos caching
weijun
parents: 14342
diff changeset
   107
                    entries.addLast(t);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        // let us cleanup while we are here
18536
092411ced388 8001326: Improve Kerberos caching
weijun
parents: 14342
diff changeset
   113
        long timeLimit = currentTime.getSeconds() - lifespan;
48985
3fbc7f109dad 8197518: Kerberos krb5 authentication: AuthList's put method leads to performance issue
weijun
parents: 47216
diff changeset
   114
3fbc7f109dad 8197518: Kerberos krb5 authentication: AuthList's put method leads to performance issue
weijun
parents: 47216
diff changeset
   115
        // Only trigger a cleanup when the earliest entry is
3fbc7f109dad 8197518: Kerberos krb5 authentication: AuthList's put method leads to performance issue
weijun
parents: 47216
diff changeset
   116
        // lifespan + 5 sec ago. This ensures a cleanup is done
3fbc7f109dad 8197518: Kerberos krb5 authentication: AuthList's put method leads to performance issue
weijun
parents: 47216
diff changeset
   117
        // at most every 5 seconds so that we don't always
3fbc7f109dad 8197518: Kerberos krb5 authentication: AuthList's put method leads to performance issue
weijun
parents: 47216
diff changeset
   118
        // addLast(removeLast).
3fbc7f109dad 8197518: Kerberos krb5 authentication: AuthList's put method leads to performance issue
weijun
parents: 47216
diff changeset
   119
        if (oldestTime > timeLimit - 5) {
3fbc7f109dad 8197518: Kerberos krb5 authentication: AuthList's put method leads to performance issue
weijun
parents: 47216
diff changeset
   120
            return;
3fbc7f109dad 8197518: Kerberos krb5 authentication: AuthList's put method leads to performance issue
weijun
parents: 47216
diff changeset
   121
        }
3fbc7f109dad 8197518: Kerberos krb5 authentication: AuthList's put method leads to performance issue
weijun
parents: 47216
diff changeset
   122
3fbc7f109dad 8197518: Kerberos krb5 authentication: AuthList's put method leads to performance issue
weijun
parents: 47216
diff changeset
   123
        // and we remove the *enough* old ones (1 lifetime ago)
3fbc7f109dad 8197518: Kerberos krb5 authentication: AuthList's put method leads to performance issue
weijun
parents: 47216
diff changeset
   124
        while (!entries.isEmpty()) {
3fbc7f109dad 8197518: Kerberos krb5 authentication: AuthList's put method leads to performance issue
weijun
parents: 47216
diff changeset
   125
            AuthTimeWithHash removed = entries.removeLast();
3fbc7f109dad 8197518: Kerberos krb5 authentication: AuthList's put method leads to performance issue
weijun
parents: 47216
diff changeset
   126
            if (removed.ctime >= timeLimit) {
3fbc7f109dad 8197518: Kerberos krb5 authentication: AuthList's put method leads to performance issue
weijun
parents: 47216
diff changeset
   127
                entries.addLast(removed);
3fbc7f109dad 8197518: Kerberos krb5 authentication: AuthList's put method leads to performance issue
weijun
parents: 47216
diff changeset
   128
                oldestTime = removed.ctime;
3fbc7f109dad 8197518: Kerberos krb5 authentication: AuthList's put method leads to performance issue
weijun
parents: 47216
diff changeset
   129
                return;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        }
48985
3fbc7f109dad 8197518: Kerberos krb5 authentication: AuthList's put method leads to performance issue
weijun
parents: 47216
diff changeset
   132
3fbc7f109dad 8197518: Kerberos krb5 authentication: AuthList's put method leads to performance issue
weijun
parents: 47216
diff changeset
   133
        oldestTime = Integer.MIN_VALUE;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
18536
092411ced388 8001326: Improve Kerberos caching
weijun
parents: 14342
diff changeset
   136
    public boolean isEmpty() {
092411ced388 8001326: Improve Kerberos caching
weijun
parents: 14342
diff changeset
   137
        return entries.isEmpty();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
18536
092411ced388 8001326: Improve Kerberos caching
weijun
parents: 14342
diff changeset
   140
    public String toString() {
092411ced388 8001326: Improve Kerberos caching
weijun
parents: 14342
diff changeset
   141
        StringBuilder sb = new StringBuilder();
092411ced388 8001326: Improve Kerberos caching
weijun
parents: 14342
diff changeset
   142
        Iterator<AuthTimeWithHash> iter = entries.descendingIterator();
092411ced388 8001326: Improve Kerberos caching
weijun
parents: 14342
diff changeset
   143
        int pos = entries.size();
092411ced388 8001326: Improve Kerberos caching
weijun
parents: 14342
diff changeset
   144
        while (iter.hasNext()) {
092411ced388 8001326: Improve Kerberos caching
weijun
parents: 14342
diff changeset
   145
            AuthTimeWithHash at = iter.next();
092411ced388 8001326: Improve Kerberos caching
weijun
parents: 14342
diff changeset
   146
            sb.append('#').append(pos--).append(": ")
092411ced388 8001326: Improve Kerberos caching
weijun
parents: 14342
diff changeset
   147
                    .append(at.toString()).append('\n');
092411ced388 8001326: Improve Kerberos caching
weijun
parents: 14342
diff changeset
   148
        }
092411ced388 8001326: Improve Kerberos caching
weijun
parents: 14342
diff changeset
   149
        return sb.toString();
092411ced388 8001326: Improve Kerberos caching
weijun
parents: 14342
diff changeset
   150
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
}