/* KerberosPrincipal.java -- a single entity in the system. Copyright (C) 2005 Free Software Foundation, Inc. Written by Jeff Bailey This file is part of GNU Classpath. GNU Classpath is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. GNU Classpath 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 for more details. You should have received a copy of the GNU General Public License along with GNU Classpath; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. Linking this library statically or dynamically with other modules is making a combined work based on this library. Thus, the terms and conditions of the GNU General Public License cover the whole combination. As a special exception, the copyright holders of this library give you permission to link this library with independent modules to produce an executable, regardless of the license terms of these independent modules, and to copy and distribute the resulting executable under terms of your choice, provided that you also meet, for each linked independent module, the terms and conditions of the license of that module. An independent module is a module which is not derived from or based on this library. If you modify this library, you may extend this exception to your version of the library, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version. */ package javax.security.auth.kerberos; import java.io.Serializable; import java.util.StringTokenizer; public final class KerberosPrincipal implements java.security.Principal, Serializable { // Fields. // ------------------------------------------------------------------------- private static final long serialVersionUID = -8308522755600156056L; /** * @serial The name of the principal. */ private final String principal; /** * @serial The name of the realm. */ private final String realm; /** * @serial The name type variable. */ private int nameType; // Constants from RFC 1510 Section 8.3 public static final int KRB_NT_UNKNOWN = 0; public static final int KRB_NT_PRINCIPAL = 1; public static final int KRB_NT_SRV_INST = 2; public static final int KRB_NT_SRV_HST = 3; public static final int KRB_NT_SRV_XHST = 4; public static final int KRB_NT_UID = 5; // Constructors. // ------------------------------------------------------------------------- /** *

Default Constructor for KerberosPrincipal

* *

In this constructor to default to KRB_NT_PRINCIPAL based on * section 7.2 of rfc1510

*/ public KerberosPrincipal(String name) { this(name, KRB_NT_PRINCIPAL); } public KerberosPrincipal(String name, int nameType) { StringTokenizer st = new StringTokenizer(name, "@", false); // FIXME: What do we do if multiple @'s are present? // FIXME: Does this function cope if it's handed an empty string? principal = st.nextToken(); if (st.hasMoreTokens()) realm = st.nextToken(); else realm = ""; this.nameType = nameType; } // Instance methods. // ------------------------------------------------------------------------- /** *

Return the default realm

* *

The default realm is either set through the system property * java.security.krb5.realm, located in the /etc/krb5.conf file * or is blank.

*/ private String getDefaultRealm() { String a = System.getProperty("java.security.krb5.realm"); if (a != null) return a; // FIXME: Handle looking in krb5.conf // Implement this when it's obvious how the rest is going to // get implemented. return new String(""); } /** *

Returns the value of the name type

*/ public int getNameType() { return nameType; } /** *

Returns the realm

*/ public String getRealm() { return realm; } // Methods from Interface Principal /** *

From Section 7.2 of rfc1510: When comparing names, a name of type * UNKNOWN will match principals authenticated with names of any type. * A principal authenticated with a name of type UNKNOWN, however, will * only match other names of type UNKNOWN. */ public boolean equals(Object that) { if (that == this) return true; if (! (that instanceof KerberosPrincipal)) return false; // The other type must be the same or UNKNOWN. if (((KerberosPrincipal)that).getNameType() != nameType) if (((KerberosPrincipal)that).getNameType() != KRB_NT_UNKNOWN) return false; // The toString output must match. if (! toString().equals(((KerberosPrincipal)that).toString())) return false; // Fallthrough case, we must be the same. return true; } /** *

Provide a human readable version of the principal.

*/ public String getName() { // Add a realm if one is present. if (! realm.equals("") ) return new String(principal + "@" + realm); // Otherwise return a barename. return principal; } public int hashCode() { return getName().hashCode(); } public String toString() { return getName(); } }