001/* DelegationPermission.java -- kerberos delegation permission
002   Copyright (C) 2006 Free Software Foundation, Inc.
003
004This file is part of GNU Classpath.
005
006GNU Classpath is free software; you can redistribute it and/or modify
007it under the terms of the GNU General Public License as published by
008the Free Software Foundation; either version 2, or (at your option)
009any later version.
010
011GNU Classpath is distributed in the hope that it will be useful, but
012WITHOUT ANY WARRANTY; without even the implied warranty of
013MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014General Public License for more details.
015
016You should have received a copy of the GNU General Public License
017along with GNU Classpath; see the file COPYING.  If not, write to the
018Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
01902110-1301 USA.
020
021Linking this library statically or dynamically with other modules is
022making a combined work based on this library.  Thus, the terms and
023conditions of the GNU General Public License cover the whole
024combination.
025
026As a special exception, the copyright holders of this library give you
027permission to link this library with independent modules to produce an
028executable, regardless of the license terms of these independent
029modules, and to copy and distribute the resulting executable under
030terms of your choice, provided that you also meet, for each linked
031independent module, the terms and conditions of the license of that
032module.  An independent module is a module which is not derived from
033or based on this library.  If you modify this library, you may extend
034this exception to your version of the library, but you are not
035obligated to do so.  If you do not wish to do so, delete this
036exception statement from your version. */
037
038
039package javax.security.auth.kerberos;
040
041import java.security.BasicPermission;
042import java.security.Permission;
043import java.security.PermissionCollection;
044import java.util.Enumeration;
045import java.util.Vector;
046
047/**
048 * @since 1.4
049 */
050public final class DelegationPermission
051    extends BasicPermission
052{
053  // FIXME: Enable this when serialization works.
054  // private static final long serialVersionUID = 883133252142523922L;
055
056  /**
057   * Create a new instance with the given name.
058   */
059  public DelegationPermission(String name)
060  {
061    super(name);
062    checkSyntax(name);
063  }
064
065  /**
066   * Create a new instance with the given name and actions.
067   *
068   * The name consists of two parts: first the subordinate
069   * service principal, then the target service principal.
070   * Each principal is surrounded by quotes; the two are separated
071   * by a space.
072   *
073   * @param name the name
074   * @param actions the actions; this is ignored
075   */
076  public DelegationPermission(String name, String actions)
077  {
078    super(name, actions);
079    checkSyntax(name);
080  }
081
082  private static void checkSyntax(String name)
083  {
084    int index = name.indexOf('"', 1);
085    int len = name.length();
086    if (name.charAt(0) != '"' || name.charAt(len - 1) != '"'
087        || index == -1 || index + 3 >= len
088        || name.charAt(index + 1) != ' '
089        || name.charAt(index + 2) != '"')
090      // FIXME: better message here.
091      throw new IllegalArgumentException("invalid syntax for principals");
092  }
093
094  public boolean implies(Permission perm)
095  {
096    return equals(perm);
097  }
098
099  public PermissionCollection newPermissionCollection()
100  {
101    // FIXME: don't know how to serialize here.  I suspect this
102    // class has to have a particular name, etc ...
103    return new PermissionCollection()
104    {
105      private Vector permissions = new Vector();
106
107      public void add(Permission perm)
108      {
109        if (isReadOnly())
110          throw new SecurityException("readonly");
111        if (! (perm instanceof DelegationPermission))
112          throw new IllegalArgumentException("can only add DelegationPermissions");
113        permissions.add(perm);
114      }
115
116      public boolean implies(Permission perm)
117      {
118        if (! (perm instanceof DelegationPermission))
119          return false;
120        Enumeration e = elements();
121        while (e.hasMoreElements())
122          {
123            DelegationPermission dp = (DelegationPermission) e.nextElement();
124            if (dp.implies(perm))
125              return true;
126          }
127        return false;
128      }
129
130      public Enumeration elements()
131      {
132        return permissions.elements();
133      }
134    };
135  }
136}