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