src/java.security.jgss/share/classes/sun/security/krb5/internal/ccache/CredentialsCache.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 25859 jdk/src/java.security.jgss/share/classes/sun/security/krb5/internal/ccache/CredentialsCache.java@3317bb8137f4
child 58611 53ddf218eddd
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
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.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.StringTokenizer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.io.FileInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.io.FileOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.io.BufferedReader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.io.InputStreamReader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * CredentialsCache stores credentials(tickets, session keys, etc) in a semi-permanent store
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * for later use by different program.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * @author Yanni Zhang
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
public abstract class CredentialsCache {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    static CredentialsCache singleton = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    static String cacheName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private static boolean DEBUG = Krb5.DEBUG;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    public static CredentialsCache getInstance(PrincipalName principal) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        return FileCredentialsCache.acquireInstance(principal, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    public static CredentialsCache getInstance(String cache) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        if ((cache.length() >= 5) && cache.substring(0, 5).equalsIgnoreCase("FILE:")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            return FileCredentialsCache.acquireInstance(null, cache.substring(5));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        // XXX else, memory credential cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        // default is file credential cache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        return FileCredentialsCache.acquireInstance(null, cache);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    public static CredentialsCache getInstance(PrincipalName principal,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                                               String cache) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        // XXX Modify this to use URL framework of the JDK
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        if (cache != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            (cache.length() >= 5) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            cache.regionMatches(true, 0, "FILE:", 0, 5)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            return FileCredentialsCache.acquireInstance(principal,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                                                        cache.substring(5));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        // When cache is null, read the default cache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        // XXX else ..we haven't provided support for memory credential cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        // yet. (supported in native code)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        // default is file credentials cache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        return FileCredentialsCache.acquireInstance(principal, cache);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * Gets the default credentials cache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    public static CredentialsCache getInstance() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        // Default credentials cache is file-based.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        return FileCredentialsCache.acquireInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    public static CredentialsCache create(PrincipalName principal, String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        if (name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            throw new RuntimeException("cache name error");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        if ((name.length() >= 5)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            && name.regionMatches(true, 0, "FILE:", 0, 5)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            name = name.substring(5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            return (FileCredentialsCache.New(principal, name));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        // else return file credentials cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        // default is file credentials cache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        return (FileCredentialsCache.New(principal, name));
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 CredentialsCache create(PrincipalName principal) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        // create a default credentials cache for a specified principal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        return (FileCredentialsCache.New(principal));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    public static String cacheName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        return cacheName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    public abstract PrincipalName getPrimaryPrincipal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    public abstract void update(Credentials c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    public abstract void save() throws IOException, KrbException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    public abstract Credentials[] getCredsList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    public abstract Credentials getDefaultCreds();
13247
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 5506
diff changeset
   123
    public abstract Credentials getCreds(PrincipalName sname);
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 5506
diff changeset
   124
    public abstract Credentials getCreds(LoginOptions options, PrincipalName sname);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
}