SubjectComber.java 9.6 KB
Newer Older
D
duke 已提交
1
/*
2
 * Copyright (c) 2002, 2013, 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;
36
import javax.security.auth.kerberos.KerberosPrincipal;
W
weijun 已提交
37
import javax.security.auth.kerberos.KeyTab;
D
duke 已提交
38 39

/**
W
weijun 已提交
40 41
 * This utility looks through the current Subject and retrieves private
 * credentials for the desired client/server principals.
D
duke 已提交
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
 *
 * @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 已提交
57 58
    static <T> T find(Subject subject, String serverPrincipal,
        String clientPrincipal, Class<T> credClass) {
D
duke 已提交
59

60 61 62
        // findAux returns T if oneOnly.
        return credClass.cast(findAux(subject, serverPrincipal,
                                      clientPrincipal, credClass, true));
D
duke 已提交
63 64
    }

65
    @SuppressWarnings("unchecked") // findAux returns List<T> if !oneOnly.
W
weijun 已提交
66 67
    static <T> List<T> findMany(Subject subject, String serverPrincipal,
        String clientPrincipal, Class<T> credClass) {
D
duke 已提交
68

69 70
        return (List<T>)findAux(subject, serverPrincipal, clientPrincipal,
            credClass, false);
D
duke 已提交
71 72 73
    }

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

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

88
            if (credClass == KeyTab.class) {
89 90 91 92 93 94 95 96 97 98
                Iterator<KeyTab> iterator =
                    subject.getPrivateCredentials(KeyTab.class).iterator();
                while (iterator.hasNext()) {
                    KeyTab t = iterator.next();
                    if (serverPrincipal != null && t.isBound()) {
                        KerberosPrincipal name = t.getPrincipal();
                        if (name != null) {
                            if (!serverPrincipal.equals(name.getName())) {
                                continue;
                            }
99
                        } else {
100 101 102 103 104 105 106 107 108 109 110
                            // legacy bound keytab. although we don't know who
                            // the bound principal is, it must be in allPrincs
                            boolean found = false;
                            for (KerberosPrincipal princ:
                                    subject.getPrincipals(KerberosPrincipal.class)) {
                                if (princ.getName().equals(serverPrincipal)) {
                                    found = true;
                                    break;
                                }
                            }
                            if (!found) continue;
111
                        }
W
weijun 已提交
112
                    }
113 114 115 116 117 118 119 120 121 122
                    // Check passed, we can add now
                    if (DEBUG) {
                        System.out.println("Found " + credClass.getSimpleName()
                                + " " + t);
                    }
                    if (oneOnly) {
                        return t;
                    } else {
                        answer.add(credClass.cast(t));
                    }
W
weijun 已提交
123 124 125
                }
            } else if (credClass == KerberosKey.class) {
                // We are looking for credentials for the serverPrincipal
126 127
                Iterator<KerberosKey> iterator =
                    subject.getPrivateCredentials(KerberosKey.class).iterator();
W
weijun 已提交
128
                while (iterator.hasNext()) {
129 130
                    KerberosKey t = iterator.next();
                    String name = t.getPrincipal().getName();
W
weijun 已提交
131
                    if (serverPrincipal == null || serverPrincipal.equals(name)) {
D
duke 已提交
132
                         if (DEBUG) {
W
weijun 已提交
133 134
                             System.out.println("Found " +
                                     credClass.getSimpleName() + " for " + name);
D
duke 已提交
135 136
                         }
                         if (oneOnly) {
W
weijun 已提交
137
                             return t;
D
duke 已提交
138
                         } else {
139
                             answer.add(credClass.cast(t));
D
duke 已提交
140 141 142 143 144 145 146 147 148 149 150 151
                         }
                    }
                }
            } 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) {
152
                            @SuppressWarnings("unchecked")
D
duke 已提交
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 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
                            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();
                                            }
204
                                            answer.add(credClass.cast(ticket));
D
duke 已提交
205 206 207 208 209 210 211 212 213 214 215 216
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return answer;
        }
    }
}