src/java.security.jgss/share/classes/sun/security/krb5/internal/ccache/FileCredentialsCache.java
author weijun
Tue, 14 Aug 2018 22:39:34 +0800
changeset 51398 3c389a284345
parent 47216 71c04702a3d5
child 58611 53ddf218eddd
permissions -rw-r--r--
8209416: Refactoring GetPropertyAction calls in security libs Reviewed-by: xuelei, rriggs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
51398
3c389a284345 8209416: Refactoring GetPropertyAction calls in security libs
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: 2919
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: 2919
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: 2919
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2919
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2919
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *  Copyright 1997 The Open Group Research Institute.  All rights reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * ===========================================================================
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
package sun.security.krb5.internal.ccache;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
51398
3c389a284345 8209416: Refactoring GetPropertyAction calls in security libs
weijun
parents: 47216
diff changeset
    36
import sun.security.action.GetPropertyAction;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import sun.security.krb5.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import sun.security.krb5.internal.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.util.StringTokenizer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.io.FileInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.io.FileOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.io.BufferedReader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.io.InputStreamReader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * CredentialsCache stores credentials(tickets, session keys, etc) in a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * semi-permanent store
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * for later use by different program.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * @author Yanni Zhang
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * @author Ram Marti
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
public class FileCredentialsCache extends CredentialsCache
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    implements FileCCacheConstants {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    public int version;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    public Tag tag; // optional
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    public PrincipalName primaryPrincipal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private Vector<Credentials> credentialsList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private static String dir;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private static boolean DEBUG = Krb5.DEBUG;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    public static synchronized FileCredentialsCache acquireInstance(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                PrincipalName principal, String cache) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            FileCredentialsCache fcc = new FileCredentialsCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            if (cache == null) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
    71
                cacheName = FileCredentialsCache.getDefaultCacheName();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            } else {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
    73
                cacheName = FileCredentialsCache.checkValidation(cache);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            if ((cacheName == null) || !(new File(cacheName)).exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                // invalid cache name or the file doesn't exist
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            if (principal != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                fcc.primaryPrincipal = principal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            fcc.load(cacheName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            return fcc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            // we don't handle it now, instead we return a null at the end.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        } catch (KrbException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            // we don't handle it now, instead we return a null at the end.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    public static FileCredentialsCache acquireInstance() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        return acquireInstance(null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    static synchronized FileCredentialsCache New(PrincipalName principal,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                                                String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            FileCredentialsCache fcc = new FileCredentialsCache();
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   106
            cacheName = FileCredentialsCache.checkValidation(name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            if (cacheName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                // invalid cache name or the file doesn't exist
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            fcc.init(principal, cacheName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            return fcc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        catch (KrbException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    static synchronized FileCredentialsCache New(PrincipalName principal) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            FileCredentialsCache fcc = new FileCredentialsCache();
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   124
            cacheName = FileCredentialsCache.getDefaultCacheName();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            fcc.init(principal, cacheName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            return fcc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        } catch (KrbException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    private FileCredentialsCache() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    boolean exists(String cache) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        File file = new File(cache);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        if (file.exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        } else return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    synchronized void init(PrincipalName principal, String name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        throws IOException, KrbException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        primaryPrincipal = principal;
28553
6c0b398bd744 8060474: Resolve more parsing ambiguity
weijun
parents: 28059
diff changeset
   154
        try (FileOutputStream fos = new FileOutputStream(name);
6c0b398bd744 8060474: Resolve more parsing ambiguity
weijun
parents: 28059
diff changeset
   155
             CCacheOutputStream cos = new CCacheOutputStream(fos)) {
6c0b398bd744 8060474: Resolve more parsing ambiguity
weijun
parents: 28059
diff changeset
   156
            version = KRB5_FCC_FVNO_3;
6c0b398bd744 8060474: Resolve more parsing ambiguity
weijun
parents: 28059
diff changeset
   157
            cos.writeHeader(primaryPrincipal, version);
6c0b398bd744 8060474: Resolve more parsing ambiguity
weijun
parents: 28059
diff changeset
   158
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        load(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    synchronized void load(String name) throws IOException, KrbException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        PrincipalName p;
28553
6c0b398bd744 8060474: Resolve more parsing ambiguity
weijun
parents: 28059
diff changeset
   164
        try (FileInputStream fis = new FileInputStream(name);
6c0b398bd744 8060474: Resolve more parsing ambiguity
weijun
parents: 28059
diff changeset
   165
             CCacheInputStream cis = new CCacheInputStream(fis)) {
6c0b398bd744 8060474: Resolve more parsing ambiguity
weijun
parents: 28059
diff changeset
   166
            version = cis.readVersion();
6c0b398bd744 8060474: Resolve more parsing ambiguity
weijun
parents: 28059
diff changeset
   167
            if (version == KRB5_FCC_FVNO_4) {
6c0b398bd744 8060474: Resolve more parsing ambiguity
weijun
parents: 28059
diff changeset
   168
                tag = cis.readTag();
6c0b398bd744 8060474: Resolve more parsing ambiguity
weijun
parents: 28059
diff changeset
   169
            } else {
6c0b398bd744 8060474: Resolve more parsing ambiguity
weijun
parents: 28059
diff changeset
   170
                tag = null;
6c0b398bd744 8060474: Resolve more parsing ambiguity
weijun
parents: 28059
diff changeset
   171
                if (version == KRB5_FCC_FVNO_1 || version == KRB5_FCC_FVNO_2) {
6c0b398bd744 8060474: Resolve more parsing ambiguity
weijun
parents: 28059
diff changeset
   172
                    cis.setNativeByteOrder();
6c0b398bd744 8060474: Resolve more parsing ambiguity
weijun
parents: 28059
diff changeset
   173
                }
6c0b398bd744 8060474: Resolve more parsing ambiguity
weijun
parents: 28059
diff changeset
   174
            }
6c0b398bd744 8060474: Resolve more parsing ambiguity
weijun
parents: 28059
diff changeset
   175
            p = cis.readPrincipal(version);
6c0b398bd744 8060474: Resolve more parsing ambiguity
weijun
parents: 28059
diff changeset
   176
6c0b398bd744 8060474: Resolve more parsing ambiguity
weijun
parents: 28059
diff changeset
   177
            if (primaryPrincipal != null) {
6c0b398bd744 8060474: Resolve more parsing ambiguity
weijun
parents: 28059
diff changeset
   178
                if (!(primaryPrincipal.match(p))) {
6c0b398bd744 8060474: Resolve more parsing ambiguity
weijun
parents: 28059
diff changeset
   179
                    throw new IOException("Primary principals don't match.");
6c0b398bd744 8060474: Resolve more parsing ambiguity
weijun
parents: 28059
diff changeset
   180
                }
6c0b398bd744 8060474: Resolve more parsing ambiguity
weijun
parents: 28059
diff changeset
   181
            } else
6c0b398bd744 8060474: Resolve more parsing ambiguity
weijun
parents: 28059
diff changeset
   182
                primaryPrincipal = p;
6c0b398bd744 8060474: Resolve more parsing ambiguity
weijun
parents: 28059
diff changeset
   183
            credentialsList = new Vector<Credentials>();
6c0b398bd744 8060474: Resolve more parsing ambiguity
weijun
parents: 28059
diff changeset
   184
            while (cis.available() > 0) {
6c0b398bd744 8060474: Resolve more parsing ambiguity
weijun
parents: 28059
diff changeset
   185
                Credentials cred = cis.readCred(version);
6c0b398bd744 8060474: Resolve more parsing ambiguity
weijun
parents: 28059
diff changeset
   186
                if (cred != null) {
6c0b398bd744 8060474: Resolve more parsing ambiguity
weijun
parents: 28059
diff changeset
   187
                    credentialsList.addElement(cred);
6c0b398bd744 8060474: Resolve more parsing ambiguity
weijun
parents: 28059
diff changeset
   188
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * Updates the credentials list. If the specified credentials for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * service is new, add it to the list. If there is an entry in the list,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * replace the old credentials with the new one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * @param c the credentials.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    public synchronized void update(Credentials c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        if (credentialsList != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            if (credentialsList.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                credentialsList.addElement(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                Credentials tmp = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                boolean matched = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                for (int i = 0; i < credentialsList.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                    tmp = credentialsList.elementAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                    if (match(c.sname.getNameStrings(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                              tmp.sname.getNameStrings()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                        ((c.sname.getRealmString()).equalsIgnoreCase(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                                     tmp.sname.getRealmString()))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                        matched = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                        if (c.endtime.getTime() >= tmp.endtime.getTime()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                            if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                                System.out.println(" >>> FileCredentialsCache "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                                         +  "Ticket matched, overwrite "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                                         +  "the old one.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                            credentialsList.removeElementAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                            credentialsList.addElement(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                if (matched == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                    if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                        System.out.println(" >>> FileCredentialsCache Ticket "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                                        +   "not exactly matched, "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                                        +   "add new one into cache.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                    credentialsList.addElement(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    public synchronized PrincipalName getPrimaryPrincipal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        return primaryPrincipal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * Saves the credentials cache file to the disk.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    public synchronized void save() throws IOException, Asn1Exception {
28553
6c0b398bd744 8060474: Resolve more parsing ambiguity
weijun
parents: 28059
diff changeset
   249
        try (FileOutputStream fos = new FileOutputStream(cacheName);
6c0b398bd744 8060474: Resolve more parsing ambiguity
weijun
parents: 28059
diff changeset
   250
             CCacheOutputStream cos = new CCacheOutputStream(fos)) {
6c0b398bd744 8060474: Resolve more parsing ambiguity
weijun
parents: 28059
diff changeset
   251
            cos.writeHeader(primaryPrincipal, version);
6c0b398bd744 8060474: Resolve more parsing ambiguity
weijun
parents: 28059
diff changeset
   252
            Credentials[] tmp = null;
6c0b398bd744 8060474: Resolve more parsing ambiguity
weijun
parents: 28059
diff changeset
   253
            if ((tmp = getCredsList()) != null) {
6c0b398bd744 8060474: Resolve more parsing ambiguity
weijun
parents: 28059
diff changeset
   254
                for (int i = 0; i < tmp.length; i++) {
6c0b398bd744 8060474: Resolve more parsing ambiguity
weijun
parents: 28059
diff changeset
   255
                    cos.addCreds(tmp[i]);
6c0b398bd744 8060474: Resolve more parsing ambiguity
weijun
parents: 28059
diff changeset
   256
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    boolean match(String[] s1, String[] s2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        if (s1.length != s2.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            for (int i = 0; i < s1.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                if (!(s1[i].equalsIgnoreCase(s2[i]))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * Returns the list of credentials entries in the cache file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    public synchronized Credentials[] getCredsList() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        if ((credentialsList == null) || (credentialsList.isEmpty())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            Credentials[] tmp = new Credentials[credentialsList.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            for (int i = 0; i < credentialsList.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                tmp[i] = credentialsList.elementAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            return tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
13247
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10336
diff changeset
   290
    public Credentials getCreds(LoginOptions options, PrincipalName sname) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        if (options == null) {
13247
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10336
diff changeset
   292
            return getCreds(sname);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            Credentials[] list = getCredsList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            if (list == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                for (int i = 0; i < list.length; i++) {
13247
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10336
diff changeset
   299
                    if (sname.match(list[i].sname)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                        if (list[i].flags.match(options)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                            return list[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * Gets a credentials for a specified service.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * @param sname service principal name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     */
13247
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10336
diff changeset
   315
    public Credentials getCreds(PrincipalName sname) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        Credentials[] list = getCredsList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        if (list == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            for (int i = 0; i < list.length; i++) {
13247
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10336
diff changeset
   321
                if (sname.match(list[i].sname)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                    return list[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    public Credentials getDefaultCreds() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        Credentials[] list = getCredsList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        if (list == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            for (int i = list.length-1; i >= 0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                if (list[i].sname.toString().startsWith("krbtgt")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                    String[] nameStrings = list[i].sname.getNameStrings();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                    // find the TGT for the current realm krbtgt/realm@realm
13247
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 10336
diff changeset
   338
                    if (nameStrings[1].equals(list[i].sname.getRealm().toString())) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                       return list[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * Returns path name of the credentials cache file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * The path name is searched in the following order:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     *
14202
159b750b93c3 8001208: Fix for KRB5CCNAME not complete
weijun
parents: 13247
diff changeset
   351
     * 1. KRB5CCNAME (bare file name without FILE:)
2919
85e23f3ff211 6832353: Krb5LoginModule: use the KRB5CCNAME when searching for Kerberos ticket cache
weijun
parents: 2
diff changeset
   352
     * 2. /tmp/krb5cc_<uid> on unix systems
85e23f3ff211 6832353: Krb5LoginModule: use the KRB5CCNAME when searching for Kerberos ticket cache
weijun
parents: 2
diff changeset
   353
     * 3. <user.home>/krb5cc_<user.name>
85e23f3ff211 6832353: Krb5LoginModule: use the KRB5CCNAME when searching for Kerberos ticket cache
weijun
parents: 2
diff changeset
   354
     * 4. <user.home>/krb5cc (if can't get <user.name>)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    public static String getDefaultCacheName() {
2919
85e23f3ff211 6832353: Krb5LoginModule: use the KRB5CCNAME when searching for Kerberos ticket cache
weijun
parents: 2
diff changeset
   358
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        String stdCacheNameComponent = "krb5cc";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        String name;
2919
85e23f3ff211 6832353: Krb5LoginModule: use the KRB5CCNAME when searching for Kerberos ticket cache
weijun
parents: 2
diff changeset
   361
14202
159b750b93c3 8001208: Fix for KRB5CCNAME not complete
weijun
parents: 13247
diff changeset
   362
        // The env var can start with TYPE:, we only support FILE: here.
159b750b93c3 8001208: Fix for KRB5CCNAME not complete
weijun
parents: 13247
diff changeset
   363
        // http://docs.oracle.com/cd/E19082-01/819-2252/6n4i8rtr3/index.html
2919
85e23f3ff211 6832353: Krb5LoginModule: use the KRB5CCNAME when searching for Kerberos ticket cache
weijun
parents: 2
diff changeset
   364
        name = java.security.AccessController.doPrivileged(
85e23f3ff211 6832353: Krb5LoginModule: use the KRB5CCNAME when searching for Kerberos ticket cache
weijun
parents: 2
diff changeset
   365
                new java.security.PrivilegedAction<String>() {
85e23f3ff211 6832353: Krb5LoginModule: use the KRB5CCNAME when searching for Kerberos ticket cache
weijun
parents: 2
diff changeset
   366
            @Override
85e23f3ff211 6832353: Krb5LoginModule: use the KRB5CCNAME when searching for Kerberos ticket cache
weijun
parents: 2
diff changeset
   367
            public String run() {
14202
159b750b93c3 8001208: Fix for KRB5CCNAME not complete
weijun
parents: 13247
diff changeset
   368
                String cache = System.getenv("KRB5CCNAME");
159b750b93c3 8001208: Fix for KRB5CCNAME not complete
weijun
parents: 13247
diff changeset
   369
                if (cache != null &&
159b750b93c3 8001208: Fix for KRB5CCNAME not complete
weijun
parents: 13247
diff changeset
   370
                        (cache.length() >= 5) &&
159b750b93c3 8001208: Fix for KRB5CCNAME not complete
weijun
parents: 13247
diff changeset
   371
                        cache.regionMatches(true, 0, "FILE:", 0, 5)) {
159b750b93c3 8001208: Fix for KRB5CCNAME not complete
weijun
parents: 13247
diff changeset
   372
                    cache = cache.substring(5);
159b750b93c3 8001208: Fix for KRB5CCNAME not complete
weijun
parents: 13247
diff changeset
   373
                }
159b750b93c3 8001208: Fix for KRB5CCNAME not complete
weijun
parents: 13247
diff changeset
   374
                return cache;
2919
85e23f3ff211 6832353: Krb5LoginModule: use the KRB5CCNAME when searching for Kerberos ticket cache
weijun
parents: 2
diff changeset
   375
            }
85e23f3ff211 6832353: Krb5LoginModule: use the KRB5CCNAME when searching for Kerberos ticket cache
weijun
parents: 2
diff changeset
   376
        });
85e23f3ff211 6832353: Krb5LoginModule: use the KRB5CCNAME when searching for Kerberos ticket cache
weijun
parents: 2
diff changeset
   377
        if (name != null) {
85e23f3ff211 6832353: Krb5LoginModule: use the KRB5CCNAME when searching for Kerberos ticket cache
weijun
parents: 2
diff changeset
   378
            if (DEBUG) {
85e23f3ff211 6832353: Krb5LoginModule: use the KRB5CCNAME when searching for Kerberos ticket cache
weijun
parents: 2
diff changeset
   379
                System.out.println(">>>KinitOptions cache name is " + name);
85e23f3ff211 6832353: Krb5LoginModule: use the KRB5CCNAME when searching for Kerberos ticket cache
weijun
parents: 2
diff changeset
   380
            }
85e23f3ff211 6832353: Krb5LoginModule: use the KRB5CCNAME when searching for Kerberos ticket cache
weijun
parents: 2
diff changeset
   381
            return name;
85e23f3ff211 6832353: Krb5LoginModule: use the KRB5CCNAME when searching for Kerberos ticket cache
weijun
parents: 2
diff changeset
   382
        }
85e23f3ff211 6832353: Krb5LoginModule: use the KRB5CCNAME when searching for Kerberos ticket cache
weijun
parents: 2
diff changeset
   383
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        // get cache name from system.property
51398
3c389a284345 8209416: Refactoring GetPropertyAction calls in security libs
weijun
parents: 47216
diff changeset
   385
        String osname = GetPropertyAction.privilegedGetProperty("os.name");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
         * For Unix platforms we use the default cache name to be
25995
6267b0694e45 8054817: File ccache only recognizes Linux and Solaris defaults
weijun
parents: 25859
diff changeset
   389
         * /tmp/krb5cc_uid ; for all other platforms  we use
6267b0694e45 8054817: File ccache only recognizes Linux and Solaris defaults
weijun
parents: 25859
diff changeset
   390
         * {user_home}/krb5cc_{user_name}
6267b0694e45 8054817: File ccache only recognizes Linux and Solaris defaults
weijun
parents: 25859
diff changeset
   391
         * Please note that for Windows we will use LSA to get
28059
e576535359cc 8067377: My hobby: caning, then then canning, the the can-can
martin
parents: 25995
diff changeset
   392
         * the TGT from the default cache even before we come here;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
         * however when we create cache we will create a cache under
25995
6267b0694e45 8054817: File ccache only recognizes Linux and Solaris defaults
weijun
parents: 25859
diff changeset
   394
         * {user_home}/krb5cc_{user_name} for non-Unix platforms including
6267b0694e45 8054817: File ccache only recognizes Linux and Solaris defaults
weijun
parents: 25859
diff changeset
   395
         * Windows.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
25995
6267b0694e45 8054817: File ccache only recognizes Linux and Solaris defaults
weijun
parents: 25859
diff changeset
   398
        if (osname != null && !osname.startsWith("Windows")) {
34882
ce2a8ec851c1 8145544: Move sun.misc.VM to jdk.internal.misc
chegar
parents: 28553
diff changeset
   399
            long uid = jdk.internal.misc.VM.getuid();
25995
6267b0694e45 8054817: File ccache only recognizes Linux and Solaris defaults
weijun
parents: 25859
diff changeset
   400
            if (uid != -1) {
6267b0694e45 8054817: File ccache only recognizes Linux and Solaris defaults
weijun
parents: 25859
diff changeset
   401
                name = File.separator + "tmp" +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                        File.separator + stdCacheNameComponent + "_" + uid;
25995
6267b0694e45 8054817: File ccache only recognizes Linux and Solaris defaults
weijun
parents: 25859
diff changeset
   403
                if (DEBUG) {
6267b0694e45 8054817: File ccache only recognizes Linux and Solaris defaults
weijun
parents: 25859
diff changeset
   404
                    System.out.println(">>>KinitOptions cache name is " +
6267b0694e45 8054817: File ccache only recognizes Linux and Solaris defaults
weijun
parents: 25859
diff changeset
   405
                            name);
6267b0694e45 8054817: File ccache only recognizes Linux and Solaris defaults
weijun
parents: 25859
diff changeset
   406
                }
6267b0694e45 8054817: File ccache only recognizes Linux and Solaris defaults
weijun
parents: 25859
diff changeset
   407
                return name;
6267b0694e45 8054817: File ccache only recognizes Linux and Solaris defaults
weijun
parents: 25859
diff changeset
   408
            } else {
6267b0694e45 8054817: File ccache only recognizes Linux and Solaris defaults
weijun
parents: 25859
diff changeset
   409
                if (DEBUG) {
6267b0694e45 8054817: File ccache only recognizes Linux and Solaris defaults
weijun
parents: 25859
diff changeset
   410
                    System.out.println("Error in obtaining uid " +
6267b0694e45 8054817: File ccache only recognizes Linux and Solaris defaults
weijun
parents: 25859
diff changeset
   411
                                        "for Unix platforms " +
6267b0694e45 8054817: File ccache only recognizes Linux and Solaris defaults
weijun
parents: 25859
diff changeset
   412
                                        "Using user's home directory");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        // we did not get the uid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
51398
3c389a284345 8209416: Refactoring GetPropertyAction calls in security libs
weijun
parents: 47216
diff changeset
   419
        String user_name = GetPropertyAction.privilegedGetProperty("user.name");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
51398
3c389a284345 8209416: Refactoring GetPropertyAction calls in security libs
weijun
parents: 47216
diff changeset
   421
        String user_home = GetPropertyAction.privilegedGetProperty("user.home");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        if (user_home == null) {
51398
3c389a284345 8209416: Refactoring GetPropertyAction calls in security libs
weijun
parents: 47216
diff changeset
   424
            user_home = GetPropertyAction.privilegedGetProperty("user.dir");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        if (user_name != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            name = user_home + File.separator  +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                stdCacheNameComponent + "_" + user_name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            name = user_home + File.separator + stdCacheNameComponent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            System.out.println(">>>KinitOptions cache name is " + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    public static String checkValidation(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        String fullname = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        if (name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            // get full path name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            fullname = (new File(name)).getCanonicalPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            File fCheck = new File(fullname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            if (!(fCheck.exists())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                // get absolute directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                File temp = new File(fCheck.getParent());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                // test if the directory exists
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                if (!(temp.isDirectory()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                    fullname = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                temp = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            fCheck = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            fullname = null; // invalid name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        return fullname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
    private static String exec(String c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        StringTokenizer st = new StringTokenizer(c);
7977
f47f211cd627 7008713: diamond conversion of kerberos5 and security tools
smarks
parents: 7288
diff changeset
   469
        Vector<String> v = new Vector<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        while (st.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            v.addElement(st.nextToken());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        final String[] command = new String[v.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        v.copyInto(command);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            Process p =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                java.security.AccessController.doPrivileged
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                (new java.security.PrivilegedAction<Process> () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                        public Process run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                                return (Runtime.getRuntime().exec(command));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                            } catch (java.io.IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                                if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                                    e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                    });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            if (p == null) {
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 14342
diff changeset
   492
                // exception occurred during executing the command
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            BufferedReader commandResult =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                new BufferedReader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                    (new InputStreamReader(p.getInputStream(), "8859_1"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            String s1 = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            if ((command.length == 1) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                (command[0].equals("/usr/bin/env"))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                while ((s1 = commandResult.readLine()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                    if (s1.length() >= 11) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                        if ((s1.substring(0, 11)).equalsIgnoreCase
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                            ("KRB5CCNAME=")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                            s1 = s1.substring(11);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            } else     s1 = commandResult.readLine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            commandResult.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
            return s1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
            if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
}