1 /*
   2  * Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.security.auth;
  27 
  28 import java.security.Principal;
  29 
  30 /**
  31  * <p> This class implements the <code>Principal</code> interface
  32  * and represents a user's Unix group identification number (GID).
  33  *
  34  * <p> Principals such as this <code>UnixNumericGroupPrincipal</code>
  35  * may be associated with a particular <code>Subject</code>
  36  * to augment that <code>Subject</code> with an additional
  37  * identity.  Refer to the <code>Subject</code> class for more information
  38  * on how to achieve this.  Authorization decisions can then be based upon
  39  * the Principals associated with a <code>Subject</code>.
  40  *
  41  * @see java.security.Principal
  42  * @see javax.security.auth.Subject
  43  */
  44 @jdk.Exported
  45 public class UnixNumericGroupPrincipal implements
  46                                         Principal,
  47                                         java.io.Serializable {
  48 
  49     private static final long serialVersionUID = 3941535899328403223L;
  50 
  51     /**
  52      * @serial
  53      */
  54     private String name;
  55 
  56     /**
  57      * @serial
  58      */
  59     private boolean primaryGroup;
  60 
  61     /**
  62      * Create a <code>UnixNumericGroupPrincipal</code> using a
  63      * <code>String</code> representation of the user's
  64      * group identification number (GID).
  65      *
  66      * <p>
  67      *
  68      * @param name the user's group identification number (GID)
  69      *                  for this user. <p>
  70      *
  71      * @param primaryGroup true if the specified GID represents the
  72      *                  primary group to which this user belongs.
  73      *
  74      * @exception NullPointerException if the <code>name</code>
  75      *                  is <code>null</code>.
  76      */
  77     public UnixNumericGroupPrincipal(String name, boolean primaryGroup) {
  78         if (name == null) {
  79             java.text.MessageFormat form = new java.text.MessageFormat
  80                 (sun.security.util.ResourcesMgr.getString
  81                         ("invalid.null.input.value",
  82                         "sun.security.util.AuthResources"));
  83             Object[] source = {"name"};
  84             throw new NullPointerException(form.format(source));
  85         }
  86 
  87         this.name = name;
  88         this.primaryGroup = primaryGroup;
  89     }
  90 
  91     /**
  92      * Create a <code>UnixNumericGroupPrincipal</code> using a
  93      * long representation of the user's group identification number (GID).
  94      *
  95      * <p>
  96      *
  97      * @param name the user's group identification number (GID) for this user
  98      *                  represented as a long. <p>
  99      *
 100      * @param primaryGroup true if the specified GID represents the
 101      *                  primary group to which this user belongs.
 102      *
 103      */
 104     public UnixNumericGroupPrincipal(long name, boolean primaryGroup) {
 105         this.name = Long.valueOf(name).toString();
 106         this.primaryGroup = primaryGroup;
 107     }
 108 
 109     /**
 110      * Return the user's group identification number (GID) for this
 111      * <code>UnixNumericGroupPrincipal</code>.
 112      *
 113      * <p>
 114      *
 115      * @return the user's group identification number (GID) for this
 116      *          <code>UnixNumericGroupPrincipal</code>
 117      */
 118     public String getName() {
 119         return name;
 120     }
 121 
 122     /**
 123      * Return the user's group identification number (GID) for this
 124      * <code>UnixNumericGroupPrincipal</code> as a long.
 125      *
 126      * <p>
 127      *
 128      * @return the user's group identification number (GID) for this
 129      *          <code>UnixNumericGroupPrincipal</code> as a long.
 130      */
 131     public long longValue() {
 132         return Long.valueOf(name);
 133     }
 134 
 135     /**
 136      * Return whether this group identification number (GID) represents
 137      * the primary group to which this user belongs.
 138      *
 139      * <p>
 140      *
 141      * @return true if this group identification number (GID) represents
 142      *          the primary group to which this user belongs,
 143      *          or false otherwise.
 144      */
 145     public boolean isPrimaryGroup() {
 146         return primaryGroup;
 147     }
 148 
 149     /**
 150      * Return a string representation of this
 151      * <code>UnixNumericGroupPrincipal</code>.
 152      *
 153      * <p>
 154      *
 155      * @return a string representation of this
 156      *          <code>UnixNumericGroupPrincipal</code>.
 157      */
 158     public String toString() {
 159 
 160         if (primaryGroup) {
 161             java.text.MessageFormat form = new java.text.MessageFormat
 162                 (sun.security.util.ResourcesMgr.getString
 163                         ("UnixNumericGroupPrincipal.Primary.Group.name",
 164                         "sun.security.util.AuthResources"));
 165             Object[] source = {name};
 166             return form.format(source);
 167         } else {
 168             java.text.MessageFormat form = new java.text.MessageFormat
 169                 (sun.security.util.ResourcesMgr.getString
 170                     ("UnixNumericGroupPrincipal.Supplementary.Group.name",
 171                     "sun.security.util.AuthResources"));
 172             Object[] source = {name};
 173             return form.format(source);
 174         }
 175     }
 176 
 177     /**
 178      * Compares the specified Object with this
 179      * <code>UnixNumericGroupPrincipal</code>
 180      * for equality.  Returns true if the given object is also a
 181      * <code>UnixNumericGroupPrincipal</code> and the two
 182      * UnixNumericGroupPrincipals
 183      * have the same group identification number (GID).
 184      *
 185      * <p>
 186      *
 187      * @param o Object to be compared for equality with this
 188      *          <code>UnixNumericGroupPrincipal</code>.
 189      *
 190      * @return true if the specified Object is equal equal to this
 191      *          <code>UnixNumericGroupPrincipal</code>.
 192      */
 193     public boolean equals(Object o) {
 194         if (o == null)
 195             return false;
 196 
 197         if (this == o)
 198             return true;
 199 
 200         if (!(o instanceof UnixNumericGroupPrincipal))
 201             return false;
 202         UnixNumericGroupPrincipal that = (UnixNumericGroupPrincipal)o;
 203 
 204         if (this.getName().equals(that.getName()) &&
 205             this.isPrimaryGroup() == that.isPrimaryGroup())
 206             return true;
 207         return false;
 208     }
 209 
 210     /**
 211      * Return a hash code for this <code>UnixNumericGroupPrincipal</code>.
 212      *
 213      * <p>
 214      *
 215      * @return a hash code for this <code>UnixNumericGroupPrincipal</code>.
 216      */
 217     public int hashCode() {
 218         return toString().hashCode();
 219     }
 220 }