jdk/src/share/classes/sun/security/krb5/internal/ccache/FileCredentialsCache.java
author jjg
Mon, 15 Aug 2011 11:48:20 -0700
changeset 10336 0bb1999251f8
parent 9035 1255eb81cc2f
child 13247 74902cfeb9c6
permissions -rw-r--r--
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror Reviewed-by: xuelei, mullan Contributed-by: alexandre.boulgakov@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 7977
diff changeset
     2
 * Copyright (c) 2000, 2011, 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import sun.security.krb5.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import sun.security.krb5.internal.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.StringTokenizer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.io.FileInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.io.FileOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.io.BufferedReader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.io.InputStreamReader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.lang.reflect.*;
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
    public Realm primaryRealm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private Vector<Credentials> credentialsList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private static String dir;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private static boolean DEBUG = Krb5.DEBUG;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    public static synchronized FileCredentialsCache acquireInstance(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                PrincipalName principal, String cache) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            FileCredentialsCache fcc = new FileCredentialsCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            if (cache == null) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
    72
                cacheName = FileCredentialsCache.getDefaultCacheName();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            } else {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
    74
                cacheName = FileCredentialsCache.checkValidation(cache);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            if ((cacheName == null) || !(new File(cacheName)).exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                // invalid cache name or the file doesn't exist
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            if (principal != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                fcc.primaryPrincipal = principal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                fcc.primaryRealm = principal.getRealm();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            fcc.load(cacheName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            return fcc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            // we don't handle it now, instead we return a null at the end.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        } catch (KrbException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            // we don't handle it now, instead we return a null at the end.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    public static FileCredentialsCache acquireInstance() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        return acquireInstance(null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    static synchronized FileCredentialsCache New(PrincipalName principal,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                                                String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            FileCredentialsCache fcc = new FileCredentialsCache();
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   108
            cacheName = FileCredentialsCache.checkValidation(name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            if (cacheName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                // invalid cache name or the file doesn't exist
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            fcc.init(principal, cacheName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            return fcc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        catch (KrbException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    static synchronized FileCredentialsCache New(PrincipalName principal) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            FileCredentialsCache fcc = new FileCredentialsCache();
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   126
            cacheName = FileCredentialsCache.getDefaultCacheName();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            fcc.init(principal, cacheName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            return fcc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        } catch (KrbException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    private FileCredentialsCache() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    boolean exists(String cache) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        File file = new File(cache);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        if (file.exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        } else return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    synchronized void init(PrincipalName principal, String name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        throws IOException, KrbException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        primaryPrincipal = principal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        primaryRealm = principal.getRealm();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        CCacheOutputStream cos =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            new CCacheOutputStream(new FileOutputStream(name));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        version = KRB5_FCC_FVNO_3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        cos.writeHeader(primaryPrincipal, version);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        cos.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        load(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    synchronized void load(String name) throws IOException, KrbException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        PrincipalName p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        CCacheInputStream cis =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            new CCacheInputStream(new FileInputStream(name));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        version = cis.readVersion();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        if (version == KRB5_FCC_FVNO_4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            tag = cis.readTag();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            tag = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            if (version == KRB5_FCC_FVNO_1 || version == KRB5_FCC_FVNO_2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                cis.setNativeByteOrder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        p = cis.readPrincipal(version);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        if (primaryPrincipal != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            if (!(primaryPrincipal.match(p))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                throw new IOException("Primary principals don't match.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            primaryPrincipal = p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        primaryRealm = primaryPrincipal.getRealm();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        credentialsList = new Vector<Credentials> ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        while (cis.available() > 0) {
7288
a38f3099e518 6979329: CCacheInputStream fails to read ticket cache files from Kerberos 1.8.1
weijun
parents: 5506
diff changeset
   189
            Credentials cred = cis.readCred(version);
a38f3099e518 6979329: CCacheInputStream fails to read ticket cache files from Kerberos 1.8.1
weijun
parents: 5506
diff changeset
   190
            if (cred != null) {
a38f3099e518 6979329: CCacheInputStream fails to read ticket cache files from Kerberos 1.8.1
weijun
parents: 5506
diff changeset
   191
                credentialsList.addElement(cred);
a38f3099e518 6979329: CCacheInputStream fails to read ticket cache files from Kerberos 1.8.1
weijun
parents: 5506
diff changeset
   192
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        cis.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * Updates the credentials list. If the specified credentials for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * service is new, add it to the list. If there is an entry in the list,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * replace the old credentials with the new one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * @param c the credentials.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    public synchronized void update(Credentials c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        if (credentialsList != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            if (credentialsList.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                credentialsList.addElement(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                Credentials tmp = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                boolean matched = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                for (int i = 0; i < credentialsList.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                    tmp = credentialsList.elementAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                    if (match(c.sname.getNameStrings(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                              tmp.sname.getNameStrings()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                        ((c.sname.getRealmString()).equalsIgnoreCase(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                                     tmp.sname.getRealmString()))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                        matched = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                        if (c.endtime.getTime() >= tmp.endtime.getTime()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                            if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                                System.out.println(" >>> FileCredentialsCache "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                                         +  "Ticket matched, overwrite "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                                         +  "the old one.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                            credentialsList.removeElementAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                            credentialsList.addElement(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                if (matched == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                    if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                        System.out.println(" >>> FileCredentialsCache Ticket "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                                        +   "not exactly matched, "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                                        +   "add new one into cache.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                    credentialsList.addElement(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    public synchronized PrincipalName getPrimaryPrincipal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        return primaryPrincipal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * Saves the credentials cache file to the disk.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    public synchronized void save() throws IOException, Asn1Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        CCacheOutputStream cos
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            = new CCacheOutputStream(new FileOutputStream(cacheName));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        cos.writeHeader(primaryPrincipal, version);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        Credentials[] tmp = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        if ((tmp = getCredsList()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            for (int i = 0; i < tmp.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                cos.addCreds(tmp[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        cos.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    boolean match(String[] s1, String[] s2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        if (s1.length != s2.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            for (int i = 0; i < s1.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                if (!(s1[i].equalsIgnoreCase(s2[i]))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                    return false;
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
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * Returns the list of credentials entries in the cache file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    public synchronized Credentials[] getCredsList() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        if ((credentialsList == null) || (credentialsList.isEmpty())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            Credentials[] tmp = new Credentials[credentialsList.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            for (int i = 0; i < credentialsList.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                tmp[i] = credentialsList.elementAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            return tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    public Credentials getCreds(LoginOptions options,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                                PrincipalName sname, Realm srealm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        if (options == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            return getCreds(sname, srealm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            Credentials[] list = getCredsList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            if (list == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                for (int i = 0; i < list.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                    if (sname.match(list[i].sname) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                        (srealm.toString().equals(list[i].srealm.toString()))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                        if (list[i].flags.match(options)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                            return list[i];
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
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * Gets a credentials for a specified service.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * @param sname service principal name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * @param srealm the realm that the service belongs to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    public Credentials getCreds(PrincipalName sname, Realm srealm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        Credentials[] list = getCredsList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        if (list == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            for (int i = 0; i < list.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                if (sname.match(list[i].sname) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                    (srealm.toString().equals(list[i].srealm.toString()))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                    return list[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    public Credentials getDefaultCreds() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        Credentials[] list = getCredsList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        if (list == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            for (int i = list.length-1; i >= 0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                if (list[i].sname.toString().startsWith("krbtgt")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                    String[] nameStrings = list[i].sname.getNameStrings();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                    // find the TGT for the current realm krbtgt/realm@realm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                    if (nameStrings[1].equals(list[i].srealm.toString())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                       return list[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * Returns path name of the credentials cache file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * The path name is searched in the following order:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     *
2919
85e23f3ff211 6832353: Krb5LoginModule: use the KRB5CCNAME when searching for Kerberos ticket cache
weijun
parents: 2
diff changeset
   359
     * 1. KRB5CCNAME
85e23f3ff211 6832353: Krb5LoginModule: use the KRB5CCNAME when searching for Kerberos ticket cache
weijun
parents: 2
diff changeset
   360
     * 2. /tmp/krb5cc_<uid> on unix systems
85e23f3ff211 6832353: Krb5LoginModule: use the KRB5CCNAME when searching for Kerberos ticket cache
weijun
parents: 2
diff changeset
   361
     * 3. <user.home>/krb5cc_<user.name>
85e23f3ff211 6832353: Krb5LoginModule: use the KRB5CCNAME when searching for Kerberos ticket cache
weijun
parents: 2
diff changeset
   362
     * 4. <user.home>/krb5cc (if can't get <user.name>)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    public static String getDefaultCacheName() {
2919
85e23f3ff211 6832353: Krb5LoginModule: use the KRB5CCNAME when searching for Kerberos ticket cache
weijun
parents: 2
diff changeset
   366
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        String stdCacheNameComponent = "krb5cc";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        String name;
2919
85e23f3ff211 6832353: Krb5LoginModule: use the KRB5CCNAME when searching for Kerberos ticket cache
weijun
parents: 2
diff changeset
   369
85e23f3ff211 6832353: Krb5LoginModule: use the KRB5CCNAME when searching for Kerberos ticket cache
weijun
parents: 2
diff changeset
   370
        name = java.security.AccessController.doPrivileged(
85e23f3ff211 6832353: Krb5LoginModule: use the KRB5CCNAME when searching for Kerberos ticket cache
weijun
parents: 2
diff changeset
   371
                new java.security.PrivilegedAction<String>() {
85e23f3ff211 6832353: Krb5LoginModule: use the KRB5CCNAME when searching for Kerberos ticket cache
weijun
parents: 2
diff changeset
   372
            @Override
85e23f3ff211 6832353: Krb5LoginModule: use the KRB5CCNAME when searching for Kerberos ticket cache
weijun
parents: 2
diff changeset
   373
            public String run() {
85e23f3ff211 6832353: Krb5LoginModule: use the KRB5CCNAME when searching for Kerberos ticket cache
weijun
parents: 2
diff changeset
   374
                return System.getenv("KRB5CCNAME");
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        String osname =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            java.security.AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                        new sun.security.action.GetPropertyAction("os.name"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
         * For Unix platforms we use the default cache name to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
         * /tmp/krbcc_uid ; for all other platforms  we use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
         * {user_home}/krb5_cc{user_name}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
         * Please note that for Windows 2K we will use LSA to get
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
         * the TGT from the the default cache even before we come here;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
         * however when we create cache we will create a cache under
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
         * {user_home}/krb5_cc{user_name} for non-Unix platforms including
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
         * Windows 2K.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        if (osname != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            String cmd = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            String uidStr = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            long uid = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            if (osname.startsWith("SunOS") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                (osname.startsWith("Linux"))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                    Class<?> c = Class.forName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                        ("com.sun.security.auth.module.UnixSystem");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                    Constructor<?> constructor = c.getConstructor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                    Object obj = constructor.newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                    Method method = c.getMethod("getUid");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                    uid =  ((Long)method.invoke(obj)).longValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                    name = File.separator + "tmp" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                        File.separator + stdCacheNameComponent + "_" + uid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                    if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                        System.out.println(">>>KinitOptions cache name is " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                                           name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                    return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                    if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                        System.out.println("Exception in obtaining uid " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                                            "for Unix platforms " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                                            "Using user's home directory");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                        e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        // we did not get the uid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        String user_name =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            java.security.AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                        new sun.security.action.GetPropertyAction("user.name"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        String user_home =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            java.security.AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                        new sun.security.action.GetPropertyAction("user.home"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        if (user_home == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            user_home =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                java.security.AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                        new sun.security.action.GetPropertyAction("user.dir"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        if (user_name != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            name = user_home + File.separator  +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                stdCacheNameComponent + "_" + user_name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            name = user_home + File.separator + stdCacheNameComponent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            System.out.println(">>>KinitOptions cache name is " + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
    public static String checkValidation(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        String fullname = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        if (name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            // get full path name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            fullname = (new File(name)).getCanonicalPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            File fCheck = new File(fullname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            if (!(fCheck.exists())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                // get absolute directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                File temp = new File(fCheck.getParent());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                // test if the directory exists
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                if (!(temp.isDirectory()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                    fullname = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                temp = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            fCheck = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
            fullname = null; // invalid name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        return fullname;
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
    private static String exec(String c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        StringTokenizer st = new StringTokenizer(c);
7977
f47f211cd627 7008713: diamond conversion of kerberos5 and security tools
smarks
parents: 7288
diff changeset
   493
        Vector<String> v = new Vector<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        while (st.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            v.addElement(st.nextToken());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        final String[] command = new String[v.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        v.copyInto(command);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            Process p =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                java.security.AccessController.doPrivileged
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                (new java.security.PrivilegedAction<Process> () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                        public Process run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                                return (Runtime.getRuntime().exec(command));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                            } catch (java.io.IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                                if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                                    e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                    });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
            if (p == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                // exception occured in execing the command
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
            BufferedReader commandResult =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                new BufferedReader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                    (new InputStreamReader(p.getInputStream(), "8859_1"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
            String s1 = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            if ((command.length == 1) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                (command[0].equals("/usr/bin/env"))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                while ((s1 = commandResult.readLine()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                    if (s1.length() >= 11) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                        if ((s1.substring(0, 11)).equalsIgnoreCase
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                            ("KRB5CCNAME=")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                            s1 = s1.substring(11);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
            } else     s1 = commandResult.readLine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            commandResult.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            return s1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
}