SubjectComber.java 8.5 KB
Newer Older
D
duke 已提交
1
/*
W
weijun 已提交
2
 * Copyright (c) 2002, 2011, Oracle and/or its affiliates. All rights reserved.
D
duke 已提交
3 4 5 6
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
7
 * published by the Free Software Foundation.  Oracle designates this
D
duke 已提交
8
 * particular file as subject to the "Classpath" exception as provided
9
 * by Oracle in the LICENSE file that accompanied this code.
D
duke 已提交
10 11 12 13 14 15 16 17 18 19 20
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
21 22 23
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
D
duke 已提交
24 25 26 27 28 29 30 31 32 33 34 35
 */

package sun.security.jgss.krb5;

import javax.security.auth.kerberos.KerberosTicket;
import javax.security.auth.kerberos.KerberosKey;
import javax.security.auth.Subject;
import javax.security.auth.DestroyFailedException;
import java.util.Iterator;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
W
weijun 已提交
36
import javax.security.auth.kerberos.KeyTab;
D
duke 已提交
37 38

/**
W
weijun 已提交
39 40
 * This utility looks through the current Subject and retrieves private
 * credentials for the desired client/server principals.
D
duke 已提交
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
 *
 * @author Ram Marti
 * @since 1.4.2
 */

class SubjectComber {

    private static final boolean DEBUG = Krb5Util.DEBUG;

    /**
     * Default constructor
     */
    private SubjectComber() {  // Cannot create one of these
    }

W
weijun 已提交
56 57
    static <T> T find(Subject subject, String serverPrincipal,
        String clientPrincipal, Class<T> credClass) {
D
duke 已提交
58

W
weijun 已提交
59
        return (T)findAux(subject, serverPrincipal, clientPrincipal, credClass,
D
duke 已提交
60 61 62
            true);
    }

W
weijun 已提交
63 64
    static <T> List<T> findMany(Subject subject, String serverPrincipal,
        String clientPrincipal, Class<T> credClass) {
D
duke 已提交
65

W
weijun 已提交
66
        return (List<T>)findAux(subject, serverPrincipal, clientPrincipal, credClass,
D
duke 已提交
67 68 69 70
            false);
    }

    /**
W
weijun 已提交
71
     * Find private credentials for the specified client/server principals
D
duke 已提交
72 73
     * in the subject. Returns null if the subject is null.
     *
W
weijun 已提交
74
     * @return the private credentials
D
duke 已提交
75
     */
W
weijun 已提交
76 77
    private static <T> Object findAux(Subject subject, String serverPrincipal,
        String clientPrincipal, Class<T> credClass, boolean oneOnly) {
D
duke 已提交
78 79 80 81

        if (subject == null) {
            return null;
        } else {
W
weijun 已提交
82
            List<T> answer = (oneOnly ? null : new ArrayList<T>());
D
duke 已提交
83

W
weijun 已提交
84 85 86 87
            if (credClass == KeyTab.class) {    // Principal un-related
                // We are looking for credentials unrelated to serverPrincipal
                Iterator<T> iterator =
                    subject.getPrivateCredentials(credClass).iterator();
D
duke 已提交
88
                while (iterator.hasNext()) {
W
weijun 已提交
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
                    T t = iterator.next();
                    if (DEBUG) {
                        System.out.println("Found " + credClass.getSimpleName());
                    }
                    if (oneOnly) {
                        return t;
                    } else {
                        answer.add(t);
                    }
                }
            } else if (credClass == KerberosKey.class) {
                // We are looking for credentials for the serverPrincipal
                Iterator<T> iterator =
                    subject.getPrivateCredentials(credClass).iterator();
                while (iterator.hasNext()) {
                    T t = iterator.next();
                    String name = ((KerberosKey)t).getPrincipal().getName();
                    if (serverPrincipal == null || serverPrincipal.equals(name)) {
D
duke 已提交
107
                         if (DEBUG) {
W
weijun 已提交
108 109
                             System.out.println("Found " +
                                     credClass.getSimpleName() + " for " + name);
D
duke 已提交
110 111
                         }
                         if (oneOnly) {
W
weijun 已提交
112
                             return t;
D
duke 已提交
113 114 115 116
                         } else {
                             if (serverPrincipal == null) {
                                 // Record name so that keys returned will all
                                 // belong to the same principal
W
weijun 已提交
117
                                 serverPrincipal = name;
D
duke 已提交
118
                             }
W
weijun 已提交
119
                             answer.add(t);
D
duke 已提交
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
                         }
                    }
                }
            } else if (credClass == KerberosTicket.class) {
                // we are looking for a KerberosTicket credentials
                // for client-service principal pair
                Set<Object> pcs = subject.getPrivateCredentials();
                synchronized (pcs) {
                    Iterator<Object> iterator = pcs.iterator();
                    while (iterator.hasNext()) {
                        Object obj = iterator.next();
                        if (obj instanceof KerberosTicket) {
                            KerberosTicket ticket = (KerberosTicket)obj;
                            if (DEBUG) {
                                System.out.println("Found ticket for "
                                                    + ticket.getClient()
                                                    + " to go to "
                                                    + ticket.getServer()
                                                    + " expiring on "
                                                    + ticket.getEndTime());
                            }
                            if (!ticket.isCurrent()) {
                                // let us remove the ticket from the Subject
                                // Note that both TGT and service ticket will be
                                // removed  upon expiration
                                if (!subject.isReadOnly()) {
                                    iterator.remove();
                                    try {
                                        ticket.destroy();
                                        if (DEBUG) {
                                            System.out.println("Removed and destroyed "
                                                        + "the expired Ticket \n"
                                                        + ticket);

                                        }
                                    } catch (DestroyFailedException dfe) {
                                        if (DEBUG) {
                                            System.out.println("Expired ticket not" +
                                                    " detroyed successfully. " + dfe);
                                        }
                                    }

                                }
                            } else {
                                if (serverPrincipal == null ||
                                    ticket.getServer().getName().equals(serverPrincipal))  {

                                    if (clientPrincipal == null ||
                                        clientPrincipal.equals(
                                            ticket.getClient().getName())) {
                                        if (oneOnly) {
                                            return ticket;
                                        } else {
                                            // Record names so that tickets will
                                            // all belong to same principals
                                            if (clientPrincipal == null) {
                                                clientPrincipal =
                                                ticket.getClient().getName();
                                            }
                                            if (serverPrincipal == null) {
                                                serverPrincipal =
                                                ticket.getServer().getName();
                                            }
W
weijun 已提交
183
                                            answer.add((T)ticket);
D
duke 已提交
184 185 186 187 188 189 190 191 192 193 194 195
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return answer;
        }
    }
}