home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / jbuilder / unsupported / JDK1.2beta3 / SOURCE / SRC.ZIP / java / security / AccessControlContext.java next >
Encoding:
Java Source  |  1998-03-20  |  11.7 KB  |  434 lines

  1. /*
  2.  * @(#)AccessControlContext.java    1.13 98/03/18
  3.  *
  4.  * Copyright 1997, 1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.security;
  16.  
  17. import java.util.Vector;
  18.  
  19. /** 
  20.  * An AccessControlContext is used to make system resource access decisions
  21.  * based on the context it encapsulates.
  22.  * 
  23.  * <p>More specifically, it encapsulates a context and
  24.  * has a single method, <code>checkPermission</code>,
  25.  * that is equivalent to the <code>checkPermission</code> method
  26.  * in the AccessController class, with one difference: The AccessControlContext
  27.  * <code>checkPermission</code> method makes access decisions based on the 
  28.  * context it encapsulates,
  29.  * rather than that of the current execution thread.
  30.  * 
  31.  * <p>Thus, the purpose of AccessControlContext is for those situations where
  32.  * a security check that should be made within a given context
  33.  * actually needs to be done from within a
  34.  * <i>different</i> context (for example, from within a worker thread).
  35.  * 
  36.  * <p> An AccessControlContext is created by calling the 
  37.  * <code>AccessController.getContext</code> method. 
  38.  * The <code>getContext</code> method takes a "snapshot"
  39.  * of the current calling context, and places
  40.  * it in an AccessControlContext object, which it returns. A sample call is
  41.  * the following:
  42.  * 
  43.  * <pre>
  44.  * 
  45.  *   AccessControlContext acc = AccessController.getContext()
  46.  * 
  47.  * </pre>
  48.  * 
  49.  * <p>
  50.  * Code within a different context can subsequently call the
  51.  * <code>checkPermission</code> method on the
  52.  * previously-saved AccessControlContext object. A sample call is the
  53.  * following:
  54.  * 
  55.  * <pre>
  56.  * 
  57.  *   acc.checkPermission(permission)
  58.  * 
  59.  * </pre> 
  60.  * 
  61.  * @see AccessController
  62.  *
  63.  * @author Roland Schemers
  64.  */
  65.  
  66. public final class AccessControlContext {
  67.  
  68.     private ProtectionDomain context[];
  69.     private boolean isPrivileged;
  70.     private AccessControlContext privilegedContext;
  71.  
  72.     /**
  73.      * Create an AccessControlContext with the given set of ProtectionDomains.
  74.      * Context must not be null. Duplicate domains will be removed from the
  75.      * context.
  76.      *
  77.      * @param context the ProtectionDomains associated with this context.
  78.      */
  79.  
  80.     public AccessControlContext(ProtectionDomain context[])
  81.     {
  82.     if (context.length == 1) {
  83.         this.context = (ProtectionDomain[])context.clone();
  84.     } else {
  85.         Vector v = new Vector(context.length);
  86.         for (int i =0; i< context.length; i++) {
  87.         if ((context[i] != null) &&  (!v.contains(context[i])))
  88.             v.addElement(context[i]);
  89.         }
  90.         this.context = new ProtectionDomain[v.size()];
  91.         v.copyInto(this.context);
  92.     }
  93.     }
  94.  
  95.     /**
  96.      * package private constructor for AccessController.getContext()
  97.      */
  98.  
  99.     AccessControlContext(ProtectionDomain context[], 
  100.                  boolean isPrivileged)
  101.     {
  102.     this.context = context;
  103.     this.isPrivileged = isPrivileged;
  104.     }
  105.  
  106.     /**
  107.      * Returns true if this context is privileged.
  108.      */
  109.     boolean isPrivileged() 
  110.     {
  111.     return isPrivileged;
  112.  
  113.     }
  114.  
  115.     /** 
  116.      * Determines whether the access request indicated by the
  117.      * specified permission should be allowed or denied, based on
  118.      * the security policy currently in effect, and the context in
  119.      * this object.
  120.      * <p>
  121.      * This method quietly returns if the access request
  122.      * is permitted, or throws a suitable AccessControlException otherwise. 
  123.      *
  124.      * @param perm the requested permission.
  125.      * 
  126.      * @exception AccessControlException if the specified permission
  127.      * is not permitted, based on the current security policy and the
  128.      * context encapsulated by this object.
  129.      */
  130.  
  131.     public void checkPermission(Permission perm)
  132.          throws AccessControlException 
  133.     {
  134.     //System.err.println("checkPermission "+perm);
  135.     //    Thread.currentThread().dumpStack();
  136.  
  137.     /*
  138.      * iterate through the ProtectionDomains in the context.
  139.      * Stop at the first one that doesn't allow the
  140.      * requested permission (throwing an exception).
  141.      *
  142.      */
  143.  
  144.     /* if ctxt is null, all we had on the stack were system domains,
  145.        or the first domain was a Privileged system domain. This
  146.        is to make the common case for system code very fast */
  147.  
  148.     if (context == null)
  149.         return;
  150.  
  151.     for (int i=0; i< context.length; i++) {
  152.         if (context[i] != null &&  !context[i].implies(perm)) {
  153.         throw new AccessControlException("access denied "+perm, perm);
  154.         }
  155.     }
  156.  
  157.     // allow if all of them allowed access
  158.     return;    
  159.     }
  160.  
  161.     /**
  162.      * Take the stack-based context (this) and combine it with
  163.      * the privileged context. this method will only be called
  164.      * if privilegedContext is non-null.
  165.      */
  166.     AccessControlContext combineWithPrivilegedContext()
  167.     {
  168.     // this.context could be null if only system code is on the stack
  169.     // in that case, ignore the stack context
  170.     boolean skipStack = (context == null);
  171.  
  172.     AccessControlContext pacc = privilegedContext;
  173.     boolean skipPrivileged = (pacc.context == null);
  174.  
  175.     if (skipPrivileged && skipStack) {
  176.         return this;
  177.     }
  178.  
  179.     int slen = (skipStack) ? 0 : context.length;
  180.  
  181.     // optimization: if the length is less then or equal to two,
  182.     // there is no reason to compress the stack context, it already is
  183.     if (skipPrivileged && slen <= 2)
  184.         return this;
  185.  
  186.     int plen = (skipPrivileged) ? 0 : pacc.context.length;
  187.  
  188.     // optimization: if the length is less then or equal to two,
  189.     // there is no reason to compress the priv context, it already is
  190.     if (skipStack && plen <= 2)
  191.         return pacc;
  192.  
  193.     // optimization: case where we have a length of 1 and
  194.     // protection domains for priv context and stack are equal
  195.     if ((slen == 1) && (plen == 1) && (context[0] == pacc.context[0]))
  196.         return this;
  197.  
  198.     // now we combine both of them, and create a new context.
  199.     ProtectionDomain pd[] = new ProtectionDomain[slen + plen];
  200.  
  201.     int i, j, n;
  202.  
  203.     n = 0;
  204.  
  205.     // first add all the protection domains from the stack context,
  206.     // throwing out nulls and duplicates
  207.  
  208.     if (!skipStack) {
  209.         for (i = 0; i < context.length; i++) {
  210.         boolean add = true;
  211.         for (j= 0; (j < n) && add; j++) {
  212.             add = (context[i] != null) && (context[i] != pd[j]);
  213.         }
  214.         if (add) {
  215.             pd[n++] = context[i];
  216.         }
  217.         }
  218.     }
  219.  
  220.     // now add all the protection domains from the priv context,
  221.     // throwing out nulls and duplicates
  222.  
  223.     if (!skipPrivileged) {
  224.         for (i = 0; i < pacc.context.length; i++) {
  225.         boolean add = true;
  226.         for (j= 0; (j < n) && add; j++) {
  227.             add = (pacc.context[i] != null) &&
  228.             (pacc.context[i] != pd[j]);
  229.         }
  230.         if (add) {
  231.             pd[n++] = pacc.context[i];
  232.         }
  233.         }
  234.     }
  235.  
  236.     // if length isn't equal, we need to shorten the array
  237.     if (n != pd.length) {
  238.         // if all we had were system domains, context is null
  239.         if (n == 0) {
  240.         pd = null;
  241.         } else {
  242.         ProtectionDomain tmp[] = new ProtectionDomain[n];
  243.         System.arraycopy(pd, 0, tmp, 0, n);
  244.         pd = tmp;
  245.         }
  246.     }
  247.  
  248.     return new AccessControlContext(pd, true);
  249.     }
  250.  
  251.  
  252.     /**
  253.      * Take the stack-based context (this) and combine it with
  254.      * the inherited context, if need be.
  255.      */
  256.     AccessControlContext optimize()
  257.     {
  258.     // this.context could be null if only system code is on the stack
  259.     // in that case, ignore the stack context
  260.  
  261.     boolean skipStack = (context == null);
  262.  
  263.     // if this context is privileged, 
  264.     // or if tacc is null, or if tacc.context is null,
  265.     // don't do the thread context
  266.  
  267.     boolean skipThread;
  268.     AccessControlContext tacc;
  269.  
  270.     if (isPrivileged) {
  271.         if (privilegedContext != null)
  272.         return combineWithPrivilegedContext();
  273.         else {
  274.         skipThread = true;
  275.         tacc = null;
  276.         }
  277.     } else {
  278.         tacc = AccessController.getInheritedAccessControlContext();
  279.         skipThread = (tacc == null) || (tacc.context == null);
  280.     }
  281.  
  282.     if (skipThread && skipStack) {
  283.         return this;
  284.     }
  285.  
  286.     int slen = (skipStack) ? 0 : context.length;
  287.  
  288.     // optimization: if the length is less then or equal to two,
  289.     // there is no reason to compress the stack context, it already is
  290.     if (skipThread && slen <= 2)
  291.         return this;
  292.  
  293.     int tlen = (skipThread) ? 0 : tacc.context.length;
  294.  
  295.     // optimization: if the length is less then or equal to two,
  296.     // there is no reason to compress the thread context, it already is
  297.     if (skipStack && tlen <= 2)
  298.         return tacc;
  299.  
  300.     // optimization: case where we have a length of 1 and
  301.     // protection domains for thread and stack are equal
  302.     if ((slen == 1) && (tlen == 1) && (context[0] == tacc.context[0]))
  303.         return this;
  304.  
  305.     // now we combine both of them, and create a new context.
  306.     ProtectionDomain pd[] = new ProtectionDomain[slen + tlen];
  307.  
  308.     int i, j, n;
  309.  
  310.     n = 0;
  311.  
  312.     // first add all the protection domains from the stack context,
  313.     // throwing out nulls and duplicates
  314.  
  315.     if (!skipStack) {
  316.         for (i = 0; i < context.length; i++) {
  317.         boolean add = true;
  318.         for (j= 0; (j < n) && add; j++) {
  319.             add = (context[i] != null) && (context[i] != pd[j]);
  320.         }
  321.         if (add) {
  322.             pd[n++] = context[i];
  323.         }
  324.         }
  325.     }
  326.  
  327.     // now add all the protection domains from the inherited context,
  328.     // throwing out nulls and duplicates
  329.  
  330.     // only do if stack context is not privileged, and the thread context
  331.     // is not null.
  332.  
  333.     if (!skipThread) {
  334.         for (i = 0; i < tacc.context.length; i++) {
  335.         boolean add = true;
  336.         for (j= 0; (j < n) && add; j++) {
  337.             add = (tacc.context[i] != null) &&
  338.             (tacc.context[i] != pd[j]);
  339.         }
  340.         if (add) {
  341.             pd[n++] = tacc.context[i];
  342.         }
  343.         }
  344.     }
  345.  
  346.     // if length isn't equal, we need to shorten the array
  347.     if (n != pd.length) {
  348.         // if all we had were system domains, context is null
  349.         if (n == 0) {
  350.         pd = null;
  351.         } else {
  352.         ProtectionDomain tmp[] = new ProtectionDomain[n];
  353.         System.arraycopy(pd, 0, tmp, 0, n);
  354.         pd = tmp;
  355.         }
  356.     }
  357.  
  358.     // the new context is privileged if either one of the contexts
  359.     // was privileged. XXX: is this right?
  360.  
  361.     return new AccessControlContext(pd, isPrivileged || tacc.isPrivileged);
  362.     }
  363.  
  364.     /**
  365.      * Checks two AccessControlContext objects for equality. 
  366.      * Checks that <i>obj</i> is
  367.      * an AccessControlContext and has the same set of ProtectionDomains
  368.      * as this context.
  369.      * <P>
  370.      * @param obj the object we are testing for equality with this object.
  371.      * @return true if <i>obj</i> is an AccessControlContext, and has the 
  372.      * same set of ProtectionDomains as this context, false otherwise.
  373.      */
  374.     public boolean equals(Object obj) {
  375.     if (obj == this)
  376.         return true;
  377.  
  378.     if (! (obj instanceof AccessControlContext))
  379.         return false;
  380.  
  381.     AccessControlContext that = (AccessControlContext) obj;
  382.  
  383.  
  384.     if (context == null) {
  385.         return (that.context == null);
  386.     }
  387.  
  388.     boolean match;
  389.     for (int i = 0; i < this.context.length; i++) {
  390.         match = false;
  391.         for (int j = 0; (j < that.context.length) && !match; j++) {
  392.         match = 
  393.             ((this.context[i] == null) && (that.context[j] == null)) ||
  394.             (this.context[i].equals(that.context[j]));
  395.         }
  396.         if (!match) return false;
  397.     }
  398.  
  399.     match = false;
  400.     for (int i = 0; i < that.context.length; i++) {
  401.         match = false;
  402.         for (int j = 0; (j < this.context.length) && !match; j++) {
  403.         match = 
  404.             ((that.context[i] == null) && (this.context[j] == null)) ||
  405.             (that.context[i].equals(this.context[j]));
  406.         }
  407.         if (!match) return false;
  408.     }
  409.     return true;
  410.  
  411.     }
  412.  
  413.     /**
  414.      * Returns the hash code value for this context. The hash code
  415.      * is computed by exclusive or-ing the hash code of all the protection
  416.      * domains in the context together.
  417.      * 
  418.      * @return a hash code value for this context.
  419.      */
  420.  
  421.     public int hashCode() {
  422.     int hashCode = 0;
  423.  
  424.     if (context == null)
  425.         return hashCode;
  426.  
  427.     for (int i =0; i < context.length; i++) {
  428.         if (context[i] != null)
  429.         hashCode ^= context[i].hashCode();
  430.     }
  431.     return hashCode;
  432.     }
  433. }
  434.