/* * Copyright (c) 2018, Red Hat, Inc. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package sun.security.krb5.internal; import java.util.Date; import java.util.HashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Map.Entry; import sun.security.krb5.PrincipalName; final class ReferralsCache { private static Map> referralsMap = new HashMap<>(); private static final class ReferralCacheEntry { private final String toRealm; private final Date validUntil; ReferralCacheEntry(String toRealm, Date validUntil) { this.toRealm = toRealm; this.validUntil = validUntil; } String getToRealm() { return toRealm; } Date getValidUntil() { return validUntil; } } static synchronized void put(PrincipalName service, String fromRealm, String toRealm, Date validUntil) { pruneExpired(service); if (validUntil.before(new Date())) { return; } Map entries = referralsMap.get(service); if (entries == null) { entries = new HashMap(); referralsMap.put(service, entries); } entries.remove(fromRealm); ReferralCacheEntry newEntry = new ReferralCacheEntry(toRealm, validUntil); entries.put(fromRealm, newEntry); // Remove loops within the cache ReferralCacheEntry current = newEntry; List seen = new LinkedList<>(); while (current != null) { if (seen.contains(current)) { // Loop found. Remove the first referral to cut the loop. entries.remove(newEntry.getToRealm()); break; } seen.add(current); current = entries.get(current.getToRealm()); } } static synchronized String get(PrincipalName service, String fromRealm) { pruneExpired(service); Map entries = referralsMap.get(service); if (entries != null) { ReferralCacheEntry toRef = entries.get(fromRealm); if (toRef != null) { return toRef.getToRealm(); } } return null; } private static void pruneExpired(PrincipalName service) { Date now = new Date(); Map entries = referralsMap.get(service); if (entries != null) { for (Entry mapEntry : entries.entrySet()) { if (mapEntry.getValue().getValidUntil().before(now)) { entries.remove(mapEntry.getKey()); } } } } }