1 /* 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3 * 4 * This code is free software; you can redistribute it and/or modify it 5 * under the terms of the GNU General Public License version 2 only, as 6 * published by the Free Software Foundation. Oracle designates this 7 * particular file as subject to the "Classpath" exception as provided 8 * by Oracle in the LICENSE file that accompanied this code. 9 * 10 * This code is distributed in the hope that it will be useful, but WITHOUT 11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 13 * version 2 for more details (a copy is included in the LICENSE file that 14 * accompanied this code). 15 * 16 * You should have received a copy of the GNU General Public License version 17 * 2 along with this work; if not, write to the Free Software Foundation, 18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 19 * 20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 21 * or visit www.oracle.com if you need additional information or have any 22 * questions. 23 */ 24 25 /* 26 * 27 * (C) Copyright IBM Corp. 1999 All Rights Reserved. 28 * Copyright 1997 The Open Group Research Institute. All rights reserved. 29 */ 30 31 package sun.security.krb5.internal.ccache; 32 33 import sun.security.krb5.*; 34 import sun.security.krb5.internal.*; 35 36 import java.util.List; 37 import java.io.IOException; 38 39 /** 40 * CredentialsCache stores credentials(tickets, session keys, etc) in a semi-permanent store 41 * for later use by different program. 42 * 43 * @author Yanni Zhang 44 */ 45 public abstract class CredentialsCache { 46 static CredentialsCache singleton = null; 47 static String cacheName; 48 getInstance(PrincipalName principal)49 public static CredentialsCache getInstance(PrincipalName principal) { 50 return FileCredentialsCache.acquireInstance(principal, null); 51 } 52 getInstance(String cache)53 public static CredentialsCache getInstance(String cache) { 54 if ((cache.length() >= 5) && cache.substring(0, 5).equalsIgnoreCase("FILE:")) { 55 return FileCredentialsCache.acquireInstance(null, cache.substring(5)); 56 } 57 // XXX else, memory credential cache 58 // default is file credential cache. 59 return FileCredentialsCache.acquireInstance(null, cache); 60 } 61 getInstance(PrincipalName principal, String cache)62 public static CredentialsCache getInstance(PrincipalName principal, 63 String cache) { 64 65 // XXX Modify this to use URL framework of the JDK 66 if (cache != null && 67 (cache.length() >= 5) && 68 cache.regionMatches(true, 0, "FILE:", 0, 5)) { 69 return FileCredentialsCache.acquireInstance(principal, 70 cache.substring(5)); 71 } 72 73 // When cache is null, read the default cache. 74 // XXX else ..we haven't provided support for memory credential cache 75 // yet. (supported in native code) 76 // default is file credentials cache. 77 return FileCredentialsCache.acquireInstance(principal, cache); 78 79 } 80 81 /** 82 * Gets the default credentials cache. 83 */ getInstance()84 public static CredentialsCache getInstance() { 85 // Default credentials cache is file-based. 86 return FileCredentialsCache.acquireInstance(); 87 } 88 create(PrincipalName principal, String name)89 public static CredentialsCache create(PrincipalName principal, String name) { 90 if (name == null) { 91 throw new RuntimeException("cache name error"); 92 } 93 if ((name.length() >= 5) 94 && name.regionMatches(true, 0, "FILE:", 0, 5)) { 95 name = name.substring(5); 96 return (FileCredentialsCache.New(principal, name)); 97 } 98 // else return file credentials cache 99 // default is file credentials cache. 100 return (FileCredentialsCache.New(principal, name)); 101 } 102 create(PrincipalName principal)103 public static CredentialsCache create(PrincipalName principal) { 104 // create a default credentials cache for a specified principal 105 return (FileCredentialsCache.New(principal)); 106 } 107 cacheName()108 public static String cacheName() { 109 return cacheName; 110 } 111 getPrimaryPrincipal()112 public abstract PrincipalName getPrimaryPrincipal(); update(Credentials c)113 public abstract void update(Credentials c); save()114 public abstract void save() throws IOException, KrbException; getCredsList()115 public abstract Credentials[] getCredsList(); getDefaultCreds()116 public abstract Credentials getDefaultCreds(); getInitialCreds()117 public abstract sun.security.krb5.Credentials getInitialCreds(); getCreds(PrincipalName sname)118 public abstract Credentials getCreds(PrincipalName sname); getCreds(LoginOptions options, PrincipalName sname)119 public abstract Credentials getCreds(LoginOptions options, PrincipalName sname); addConfigEntry(ConfigEntry e)120 public abstract void addConfigEntry(ConfigEntry e); getConfigEntries()121 public abstract List<ConfigEntry> getConfigEntries(); 122 getConfigEntry(String name)123 public ConfigEntry getConfigEntry(String name) { 124 List<ConfigEntry> entries = getConfigEntries(); 125 if (entries != null) { 126 for (ConfigEntry e : entries) { 127 if (e.getName().equals(name)) { 128 return e; 129 } 130 } 131 } 132 return null; 133 } 134 135 public static class ConfigEntry { 136 ConfigEntry(String name, PrincipalName princ, byte[] data)137 public ConfigEntry(String name, PrincipalName princ, byte[] data) { 138 this.name = name; 139 this.princ = princ; 140 this.data = data; 141 } 142 143 private final String name; 144 private final PrincipalName princ; 145 private final byte[] data; // not worth cloning 146 getName()147 public String getName() { 148 return name; 149 } 150 getPrinc()151 public PrincipalName getPrinc() { 152 return princ; 153 } 154 getData()155 public byte[] getData() { 156 return data; 157 } 158 159 @Override toString()160 public String toString() { 161 return name + (princ != null ? ("." + princ) : "") 162 + ": " + new String(data); 163 } 164 getSName()165 public PrincipalName getSName() { 166 try { 167 return new PrincipalName("krb5_ccache_conf_data/" + name 168 + (princ != null ? ("/" + princ) : "") 169 + "@X-CACHECONF:"); 170 } catch (RealmException e) { 171 throw new AssertionError(e); 172 } 173 } 174 } 175 } 176