home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / Java2 / src / java / lang / SecurityManager.java < prev    next >
Encoding:
Java Source  |  1999-05-28  |  62.9 KB  |  1,574 lines  |  [TEXT/CWIE]

  1. /*
  2.  * @(#)SecurityManager.java    1.104 98/10/06
  3.  *
  4.  * Copyright 1995-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.lang;
  16.  
  17. import java.security.*;
  18. import java.io.FileDescriptor;
  19. import java.io.File;
  20. import java.io.FilePermission;
  21. import java.awt.AWTPermission;
  22. import java.util.PropertyPermission;
  23. import java.lang.RuntimePermission;
  24. import java.net.SocketPermission;
  25. import java.net.NetPermission;
  26. import java.util.Hashtable;
  27. import java.net.InetAddress;
  28. import java.lang.reflect.Member;
  29. import java.lang.reflect.*;
  30. import java.net.URL;
  31.  
  32. /**
  33.  * The security manager is a class that allows 
  34.  * applications to implement a security policy. It allows an 
  35.  * application to determine, before performing a possibly unsafe or 
  36.  * sensitive operation, what the operation is and whether 
  37.  * it is being attempted in a security context that allows the
  38.  * operation to be performed. The 
  39.  * application can allow or disallow the operation. 
  40.  * <p>
  41.  * The <code>SecurityManager</code> class contains many methods with 
  42.  * names that begin with the word <code>check</code>. These methods 
  43.  * are called by various methods in the Java libraries before those 
  44.  * methods perform certain potentially sensitive operations. The 
  45.  * invocation of such a <code>check</code> method typically looks like this: 
  46.  * <p><blockquote><pre>
  47.  *     SecurityManager security = System.getSecurityManager();
  48.  *     if (security != null) {
  49.  *         security.check<i>XXX</i>(argument,  . . . );
  50.  *     }
  51.  * </pre></blockquote>
  52.  * <p>
  53.  * The security manager is thereby given an opportunity to prevent 
  54.  * completion of the operation by throwing an exception. A security 
  55.  * manager routine simply returns if the operation is permitted, but 
  56.  * throws a <code>SecurityException</code> if the operation is not 
  57.  * permitted. The only exception to this convention is 
  58.  * <code>checkTopLevelWindow</code>, which returns a 
  59.  * <code>boolean</code> value. 
  60.  * <p>
  61.  * The current security manager is set by the 
  62.  * <code>setSecurityManager</code> method in class 
  63.  * <code>System</code>. The current security manager is obtained 
  64.  * by the <code>getSecurityManager</code> method. 
  65.  * <p> 
  66.  * The special method 
  67.  * {@link SecurityManager#checkPermission(java.security.Permission)}
  68.  * determines whether an access request indicated by a specified
  69.  * permission should be granted or denied. The 
  70.  * default implementation calls
  71.  * 
  72.  * <pre>
  73.  *   AccessController.checkPermission(perm);
  74.  * </pre>
  75.  *
  76.  * <p> 
  77.  * If a requested access is allowed, 
  78.  * <code>checkPermission</code> returns quietly. If denied, a 
  79.  * <code>SecurityException</code> is thrown. 
  80.  * <p>
  81.  * As of JDK 1.2, the default implementation of each of the other
  82.  * <code>check</code> methods in <code>SecurityManager</code> is to 
  83.  * call the <code>SecurityManager checkPermission</code> method
  84.  * to determine if the calling thread has permission to perform the requested
  85.  * operation.
  86.  * <p> 
  87.  * Note that the <code>checkPermission</code> method with
  88.  * just a single permission argument always performs security checks
  89.  * within the context of the currently executing thread.
  90.  * Sometimes a security check that should be made within a given context
  91.  * will actually need to be done from within a
  92.  * <i>different</i> context (for example, from within a worker thread).
  93.  * The {@link SecurityManager#getSecurityContext getSecurityContext} method 
  94.  * and the {@link SecurityManager#checkPermission(java.security.Permission, 
  95.  * java.lang.Object) checkPermission}
  96.  * method that includes a context argument are provided 
  97.  * for this situation. The 
  98.  * <code>getSecurityContext</code> method returns a "snapshot"
  99.  * of the current calling context. (The default implementation 
  100.  * returns an AccessControlContext object.) A sample call is
  101.  * the following:
  102.  * 
  103.  * <pre>
  104.  *   Object context = null;
  105.  *   SecurityManager sm = System.getSecurityManager();
  106.  *   if (sm != null) context = sm.getSecurityContext(); 
  107.  * </pre>
  108.  * 
  109.  * <p>
  110.  * The <code>checkPermission</code> method
  111.  * that takes a context object in addition to a permission 
  112.  * makes access decisions based on that context,
  113.  * rather than on that of the current execution thread.
  114.  * Code within a different context can thus call that method,
  115.  * passing the permission and the
  116.  * previously-saved context object. A sample call, using the
  117.  * SecurityManager <code>sm</code> obtained as in the previous example, 
  118.  * is the following:
  119.  * 
  120.  * <pre>
  121.  *   if (sm != null) sm.checkPermission(permission, context);
  122.  * </pre> 
  123.  *
  124.  * <p>Permissions fall into these categories: File, Socket, Net, 
  125.  * Security, Runtime, Property, AWT, Reflect, and Serializable. 
  126.  * The classes managing these various
  127.  * permission categories are <code>java.io.FilePermission</code>,
  128.  * <code>java.net.SocketPermission</code>, 
  129.  * <code>java.net.NetPermission</code>, 
  130.  * <code>java.security.SecurityPermission</code>,
  131.  * <code>java.lang.RuntimePermission</code>, 
  132.  * <code>java.util.PropertyPermission</code>, 
  133.  * <code>java.awt.AWTPermission</code>,
  134.  * <code>java.lang.reflect.ReflectPermission</code>, and
  135.  * <code>java.io.SerializablePermission</code>. 
  136.  * 
  137.  * <p>All but the first two (FilePermission and SocketPermission) are 
  138.  * subclasses of <code>java.security.BasicPermission</code>, which itself 
  139.  * is an abstract subclass of the
  140.  * top-level class for permissions, which is 
  141.  * <code>java.security.Permission</code>. BasicPermission defines the 
  142.  * functionality needed for all permissions that contain a name 
  143.  * that follows the hierarchical property naming convention 
  144.  * (for example, "exitVM", "setFactory", "queuePrintJob", etc). 
  145.  * An asterisk 
  146.  * may appear at the end of the name, following a ".", or by itself, to 
  147.  * signify a wildcard match. For example: "a.*" or "*" is valid, 
  148.  * "*a" or "a*b" is not valid.
  149.  *
  150.  * <p>FilePermission and SocketPermission are subclasses of the
  151.  * top-level class for permissions 
  152.  * (<code>java.security.Permission</code>). Classes like these
  153.  * that have a more complicated name syntax than that used by
  154.  * BasicPermission subclass directly from Permission rather than from
  155.  * BasicPermission. For example, 
  156.  * for a <code>java.io.FilePermission</code> object, the permission name is
  157.  * the pathname of a file (or directory).
  158.  *
  159.  * <p>Some of the permission classes have an "actions" list that tells 
  160.  * the actions that are permitted for the object.  For example, 
  161.  * for a <code>java.io.FilePermission</code> object, the actions list
  162.  * (such as "read, write") specifies which actions are granted for the
  163.  * specified file (or for files in the specified directory).
  164.  * 
  165.  * <p>Other permission classes are for "named" permissions - 
  166.  * ones that contain a name but no actions list; you either have the
  167.  * named permission or you don't.
  168.  * 
  169.  * <p>Note: There is also a <code>java.security.AllPermission</code>
  170.  * permission that implies all permissions. It exists to simplify the work
  171.  * of system administrators who might need to perform multiple
  172.  * tasks that require all (or numerous) permissions.
  173.  * <p>
  174.  * See <a href ="../../../guide/security/permissions.html">
  175.  * Permissions in JDK1.2</a> for permission-related information.
  176.  * This document includes, for example, a table listing the various SecurityManager
  177.  * <code>check</code> methods and the permission(s) the default 
  178.  * implementation of each such method requires. 
  179.  * It also contains a table of all the JDK 1.2 methods
  180.  * that require permissions, and for each such method tells 
  181.  * which permission it requires.
  182.  * <p>
  183.  * For more information about <code>SecurityManager</code> changes made in 
  184.  * JDK 1.2 and advice regarding porting of 1.1-style security managers,
  185.  * see the release documentation at 
  186.  * <a href= "http://java.sun.com/products/jdk/1.2/docs/guide/security/index.html">
  187.  * http://java.sun.com/products/jdk/1.2/docs/guide/security/index.html</a>.
  188.  *
  189.  * @author  Arthur van Hoff
  190.  * @author  Roland Schemers
  191.  *
  192.  * @version 1.104, 10/06/98
  193.  * @see     java.lang.ClassLoader
  194.  * @see     java.lang.SecurityException
  195.  * @see     java.lang.SecurityManager#checkTopLevelWindow(java.lang.Object)
  196.  *  checkTopLevelWindow
  197.  * @see     java.lang.System#getSecurityManager() getSecurityManager
  198.  * @see     java.lang.System#setSecurityManager(java.lang.SecurityManager)
  199.  *  setSecurityManager
  200.  * @see     java.security.AccessController AccessController
  201.  * @see     java.security.AccessControlContext AccessControlContext
  202.  * @see     java.security.AccessControlException AccessControlException
  203.  * @see     java.security.Permission 
  204.  * @see     java.security.BasicPermission
  205.  * @see     java.io.FilePermission
  206.  * @see     java.net.SocketPermission
  207.  * @see     java.util.PropertyPermission
  208.  * @see     java.lang.RuntimePermission
  209.  * @see     java.awt.AWTPermission
  210.  * @see     java.security.Policy Policy
  211.  * @see     java.security.SecurityPermission SecurityPermission
  212.  * @see     java.security.ProtectionDomain
  213.  *
  214.  * @since   JDK1.0
  215.  */
  216. public 
  217. class SecurityManager {
  218.  
  219.     /**
  220.      * This field is <code>true</code> if there is a security check in 
  221.      * progress; <code>false</code> otherwise.
  222.      *
  223.      * @deprecated This type of security checking is not recommended.
  224.      *  It is recommended that the <code>checkPermission</code>
  225.      *  call be used instead.
  226.      */
  227.     protected boolean inCheck;
  228.  
  229.     /* 
  230.      * Have we been initialized. Effective against finalizer attacks.
  231.      */
  232.     private boolean initialized = false;
  233.  
  234.     // static permissions. Create here and used in the various
  235.     // check permission calls
  236.  
  237.     private static RuntimePermission createClassLoaderPermission;
  238.  
  239.     private static AWTPermission topLevelWindowPermission;
  240.  
  241.     private static AWTPermission accessClipboardPermission;
  242.  
  243.     private static AWTPermission checkAwtEventQueuePermission;
  244.  
  245.     private static RuntimePermission checkMemberAccessPermission;
  246.  
  247.     private static AllPermission allPermission;
  248.  
  249.     private static RuntimePermission threadPermission;
  250.  
  251.     private static RuntimePermission threadGroupPermission;
  252.  
  253.     /**
  254.      * returns true if the current context has been granted AllPermission
  255.      */
  256.     private boolean hasAllPermission() 
  257.     {
  258.     if (allPermission == null)
  259.         allPermission = new AllPermission();
  260.     try {
  261.         checkPermission(allPermission);
  262.         return true;
  263.     } catch (SecurityException se) {
  264.         return false;
  265.     }
  266.     }
  267.  
  268.     /** 
  269.      * Tests if there is a security check in progress.
  270.      *
  271.      * @return the value of the <code>inCheck</code> field. This field 
  272.      *          should contain <code>true</code> if a security check is
  273.      *          in progress,
  274.      *          <code>false</code> otherwise.
  275.      * @see     java.lang.SecurityManager#inCheck
  276.      * @deprecated This type of security checking is not recommended.
  277.      *  It is recommended that the <code>checkPermission</code>
  278.      *  call be used instead.
  279.      */
  280.     public boolean getInCheck() {
  281.     return inCheck;
  282.     }
  283.  
  284.     /**
  285.      * Constructs a new <code>SecurityManager</code>.
  286.      *
  287.      * <p> If there is a security manager already installed, this method first
  288.      * calls the security manager's <code>checkPermission</code> method
  289.      * with the <code>RuntimePermission("createSecurityManager")</code>
  290.      * permission to ensure the calling thread has permission to create a new 
  291.      * security manager.
  292.      * This may result in throwing a <code>SecurityException</code>.
  293.      *
  294.      * @exception  java.lang.SecurityException if a security manager already 
  295.      *             exists and its <code>checkPermission</code> method 
  296.      *             doesn't allow creation of a new security manager.
  297.      * @see        java.lang.System#getSecurityManager()
  298.      * @see        #checkPermission(java.security.Permission) checkPermission
  299.      * @see java.lang.RuntimePermission
  300.      */
  301.     public SecurityManager() {
  302.     synchronized(SecurityManager.class) {
  303.          SecurityManager sm = System.getSecurityManager();
  304.          if (sm != null) {
  305.         // ask the currently installed security manager if we 
  306.         // can create a new one.
  307.          sm.checkPermission(new RuntimePermission
  308.                    ("createSecurityManager"));
  309.           }
  310.         initialized = true;
  311.     }
  312.     }
  313.  
  314.     /**
  315.      * Returns the current execution stack as an array of classes. 
  316.      * <p>
  317.      * The length of the array is the number of methods on the execution 
  318.      * stack. The element at index <code>0</code> is the class of the 
  319.      * currently executing method, the element at index <code>1</code> is 
  320.      * the class of that method's caller, and so on. 
  321.      *
  322.      * @return  the execution stack.
  323.      */
  324.     protected native Class[] getClassContext();
  325.  
  326.     /**
  327.      * Returns the class loader of the most recently executing method from
  328.      * a class defined using a non-system class loader. A non-system 
  329.      * class loader is defined as being a class loader that is not equal to
  330.      * the system class loader (as returned 
  331.      * by {@link ClassLoader#getSystemClassLoader}) or one of its ancestors.
  332.      * <p>
  333.      * This method will return
  334.      * <code>null</code> in the following three cases:<p>
  335.      * <ol>
  336.      *   <li>All methods on the execution stack are from classes
  337.      *   defined using the system class loader or one of its ancestors.
  338.      *
  339.      *   <li>All methods on the execution stack up to the first
  340.      *   "privileged" caller 
  341.      *   (see {@link java.security.AccessController#doPrivileged})
  342.      *   are from classes
  343.      *   defined using the system class loader or one of its ancestors.
  344.      *
  345.      *   <li> A call to <code>checkPermission</code> with 
  346.      *   <code>java.security.AllPermission</code> does not 
  347.      *   result in a SecurityException. 
  348.      *
  349.      * </ol>
  350.      *
  351.      * @return  the class loader of the most recent occurrence on the stack
  352.      *          of a method from a class defined using a non-system class 
  353.      *          loader.
  354.      *
  355.      * @deprecated This type of security checking is not recommended.
  356.      *  It is recommended that the <code>checkPermission</code>
  357.      *  call be used instead.
  358.      * 
  359.      * @see  java.lang.ClassLoader#getSystemClassLoader() getSystemClassLoader
  360.      * @see  #checkPermission(java.security.Permission) checkPermission
  361.      */
  362.     protected ClassLoader currentClassLoader()
  363.     {
  364.     ClassLoader cl = currentClassLoader0();
  365.     if ((cl != null) && hasAllPermission())
  366.         cl = null;
  367.     return cl;
  368.     }
  369.  
  370.     private native ClassLoader currentClassLoader0();
  371.  
  372.     /**
  373.      * Returns the class of the most recently executing method from
  374.      * a class defined using a non-system class loader. A non-system 
  375.      * class loader is defined as being a class loader that is not equal to
  376.      * the system class loader (as returned 
  377.      * by {@link ClassLoader#getSystemClassLoader}) or one of its ancestors.
  378.      * <p>
  379.      * This method will return
  380.      * <code>null</code> in the following three cases:<p>
  381.      * <ol>
  382.      *   <li>All methods on the execution stack are from classes
  383.      *   defined using the system class loader or one of its ancestors.
  384.      *
  385.      *   <li>All methods on the execution stack up to the first
  386.      *   "privileged" caller
  387.      *   (see {@link java.security.AccessController#doPrivileged})
  388.      *   are from classes
  389.      *   defined using the system class loader or one of its ancestors.
  390.      *
  391.      *   <li> A call to <code>checkPermission</code> with 
  392.      *   <code>java.security.AllPermission</code> does not 
  393.      *   result in a SecurityException. 
  394.      *
  395.      * </ol>
  396.      *
  397.      * @return  the class  of the most recent occurrence on the stack
  398.      *          of a method from a class defined using a non-system class 
  399.      *          loader.
  400.      *
  401.      * @deprecated This type of security checking is not recommended.
  402.      *  It is recommended that the <code>checkPermission</code>
  403.      *  call be used instead.
  404.      * 
  405.      * @see  java.lang.ClassLoader#getSystemClassLoader() getSystemClassLoader
  406.      * @see  #checkPermission(java.security.Permission) checkPermission
  407.      */
  408.     protected Class currentLoadedClass() {
  409.     Class c = currentLoadedClass0();
  410.     if ((c != null) && hasAllPermission())
  411.         c = null;
  412.     return c;
  413.     }
  414.  
  415.     /**
  416.      * Returns the stack depth of the specified class. 
  417.      *
  418.      * @param   name   the fully qualified name of the class to search for.
  419.      * @return  the depth on the stack frame of the first occurrence of a
  420.      *          method from a class with the specified name;
  421.      *          <code>-1</code> if such a frame cannot be found.
  422.      * @deprecated This type of security checking is not recommended.
  423.      *  It is recommended that the <code>checkPermission</code>
  424.      *  call be used instead.
  425.      *
  426.      */
  427.     protected native int classDepth(String name);
  428.  
  429.     /**
  430.      * Returns the stack depth of the most recently executing method 
  431.      * from a class defined using a non-system class loader.  A non-system 
  432.      * class loader is defined as being a class loader that is not equal to
  433.      * the system class loader (as returned 
  434.      * by {@link ClassLoader#getSystemClassLoader}) or one of its ancestors.
  435.      * <p>
  436.      * This method will return
  437.      * -1 in the following three cases:<p>
  438.      * <ol>
  439.      *   <li>All methods on the execution stack are from classes
  440.      *   defined using the system class loader or one of its ancestors.
  441.      *
  442.      *   <li>All methods on the execution stack up to the first
  443.      *   "privileged" caller
  444.      *   (see {@link java.security.AccessController#doPrivileged})
  445.      *   are from classes
  446.      *   defined using the system class loader or one of its ancestors.
  447.      *
  448.      *   <li> A call to <code>checkPermission</code> with 
  449.      *   <code>java.security.AllPermission</code> does not 
  450.      *   result in a SecurityException. 
  451.      *
  452.      * </ol>
  453.      *
  454.      * @return the depth on the stack frame of the most recent occurrence of
  455.      *          a method from a class defined using a non-system class loader.
  456.      *
  457.      * @deprecated This type of security checking is not recommended.
  458.      *  It is recommended that the <code>checkPermission</code>
  459.      *  call be used instead.
  460.      * 
  461.      * @see   java.lang.ClassLoader#getSystemClassLoader() getSystemClassLoader
  462.      * @see   #checkPermission(java.security.Permission) checkPermission
  463.      */
  464.     protected int classLoaderDepth()
  465.     {
  466.     int depth = classLoaderDepth0();
  467.     if (depth != -1) {
  468.         if (hasAllPermission())
  469.         depth = -1;
  470.         else
  471.         depth--; // make sure we don't include ourself
  472.     }
  473.     return depth;
  474.     }
  475.  
  476.     private native int classLoaderDepth0();
  477.  
  478.     /**
  479.      * Tests if a method from a class with the specified
  480.      *         name is on the execution stack. 
  481.      *
  482.      * @param  name   the fully qualified name of the class.
  483.      * @return <code>true</code> if a method from a class with the specified
  484.      *         name is on the execution stack; <code>false</code> otherwise.
  485.      * @deprecated This type of security checking is not recommended.
  486.      *  It is recommended that the <code>checkPermission</code>
  487.      *  call be used instead.
  488.      */
  489.     protected boolean inClass(String name) {
  490.     return classDepth(name) >= 0;
  491.     }
  492.  
  493.     /**
  494.      * Basically, tests if a method from a class defined using a
  495.      *          class loader is on the execution stack.
  496.      *
  497.      * @return  <code>true</code> if a call to <code>currentClassLoader</code>
  498.      *          has a non-null return value.
  499.      *
  500.      * @deprecated This type of security checking is not recommended.
  501.      *  It is recommended that the <code>checkPermission</code>
  502.      *  call be used instead.
  503.      * @see        #currentClassLoader() currentClassLoader 
  504.      */
  505.     protected boolean inClassLoader() {
  506.     return currentClassLoader() != null;
  507.     }
  508.  
  509.     /**
  510.      * Creates an object that encapsulates the current execution 
  511.      * environment. The result of this method is used, for example, by the 
  512.      * three-argument <code>checkConnect</code> method and by the 
  513.      * two-argument <code>checkRead</code> method. 
  514.      * These methods are needed because a trusted method may be called 
  515.      * on to read a file or open a socket on behalf of another method. 
  516.      * The trusted method needs to determine if the other (possibly 
  517.      * untrusted) method would be allowed to perform the operation on its 
  518.      * own. 
  519.      * <p> The default implementation of this method is to return 
  520.      * an <code>AccessControlContext</code> object.
  521.      *
  522.      * @return  an implementation-dependent object that encapsulates
  523.      *          sufficient information about the current execution environment
  524.      *          to perform some security checks later.
  525.      * @see     java.lang.SecurityManager#checkConnect(java.lang.String, int, 
  526.      *   java.lang.Object) checkConnect
  527.      * @see     java.lang.SecurityManager#checkRead(java.lang.String, 
  528.      *   java.lang.Object) checkRead
  529.      * @see     java.security.AccessControlContext AccessControlContext
  530.      */
  531.     public Object getSecurityContext() {
  532.     return AccessController.getContext();
  533.     }
  534.  
  535.     /**
  536.      * Throws a <code>SecurityException</code> if the requested
  537.      * access, specified by the given permission, is not permitted based
  538.      * on the security policy currently in effect.
  539.      * <p>
  540.      * This method calls <code>AccessController.checkPermission</code> 
  541.      * with the given permission.
  542.      *
  543.      * @param     perm   the requested permission.
  544.      * @exception SecurityException if access is not permitted based on
  545.      *          the current security policy.
  546.      * @since     JDK1.2
  547.      */
  548.     public void checkPermission(Permission perm) {
  549.     java.security.AccessController.checkPermission(perm);
  550.     }
  551.  
  552.     /**
  553.      * Throws a <code>SecurityException</code> if the
  554.      * specified security context is denied access to the resource
  555.      * specified by the given permission.
  556.      * The context must be a security 
  557.      * context returned by a previous call to 
  558.      * <code>getSecurityContext</code> and the access control
  559.      * decision is based upon the configured security policy for
  560.      * that security context.
  561.      * <p>
  562.      * If <code>context</code> is an instance of 
  563.      * <code>AccessControlContext</code> then the
  564.      * <code>AccessControlContext.checkPermission</code> method is
  565.      * invoked with the specified permission.
  566.      * <p>
  567.      * If <code>context</code> is not an instance of 
  568.      * <code>AccessControlContext</code> then a
  569.      * <code>SecurityException</code> is thrown. 
  570.      *
  571.      * @param      perm      the specified permission
  572.      * @param      context   a system-dependent security context.
  573.      * @exception  SecurityException  if the specified security context is
  574.      *            denied access to the resource specified by the
  575.      *            given permission.     
  576.      * @see        java.lang.SecurityManager#getSecurityContext()
  577.      * @see java.security.AccessControlContext#checkPermission(java.security.Permission) 
  578.      * @since      JDK1.2
  579.      */
  580.     public void checkPermission(Permission perm, Object context) {
  581.     if (context instanceof AccessControlContext) {
  582.         ((AccessControlContext)context).checkPermission(perm);
  583.     } else {
  584.         throw new SecurityException();
  585.     }
  586.     }
  587.  
  588.     /**
  589.      * Throws a <code>SecurityException</code> if the 
  590.      * calling thread is not allowed to create a new class loader. 
  591.      * <p>
  592.      * This method calls <code>checkPermission</code> with the
  593.      * <code>RuntimePermission("createClassLoader")</code>
  594.      * permission.
  595.      * <p>
  596.      * If you override this method, then you should make a call to 
  597.      * <code>super.checkCreateClassLoader</code>
  598.      * at the point the overridden method would normally throw an
  599.      * exception.
  600.      *
  601.      * @exception SecurityException if the calling thread does not 
  602.      *             have permission
  603.      *             to create a new class loader.
  604.      * @see        java.lang.ClassLoader#ClassLoader()
  605.      * @see        #checkPermission(java.security.Permission) checkPermission
  606.      */
  607.     public void checkCreateClassLoader() {
  608.     if (createClassLoaderPermission == null)
  609.         createClassLoaderPermission = 
  610.                         new RuntimePermission("createClassLoader");
  611.     checkPermission(createClassLoaderPermission);
  612.     }
  613.  
  614.     /** 
  615.      * reference to the root thread group, used for the checkAccess
  616.      * methods.
  617.      */
  618.  
  619.     private static ThreadGroup rootGroup = getRootGroup();
  620.    
  621.     private static ThreadGroup getRootGroup() {
  622.     ThreadGroup root =  Thread.currentThread().getThreadGroup();
  623.     while (root.getParent() != null) {
  624.         root = root.getParent();
  625.     }
  626.     return root;
  627.     }
  628.  
  629.     /**
  630.      * Throws a <code>SecurityException</code> if the 
  631.      * calling thread is not allowed to modify the thread argument. 
  632.      * <p>
  633.      * This method is invoked for the current security manager by the 
  634.      * <code>stop</code>, <code>suspend</code>, <code>resume</code>, 
  635.      * <code>setPriority</code>, <code>setName</code>, and 
  636.      * <code>setDaemon</code> methods of class <code>Thread</code>. 
  637.      * <p>
  638.      * If the thread argument is a system thread (belongs to
  639.      * the thread group with a <code>null</code> parent) then 
  640.      * this method calls <code>checkPermission</code> with the
  641.      * <code>RuntimePermission("modifyThread")</code> permission.
  642.      * If the thread argument is <i>not</i> a system thread,
  643.      * this method just returns silently.
  644.      * <p>
  645.      * Applications that want a stricter policy should override this
  646.      * method. If this method is overridden, the method that overrides
  647.      * it should additionally check to see if the calling thread has the
  648.      * <code>RuntimePermission("modifyThread")</code> permission, and
  649.      * if so, return silently. This is to ensure that code granted
  650.      * that permission (such as the JDK itself) is allowed to
  651.      * manipulate any thread.
  652.      * <p>
  653.      * If this method is overridden, then 
  654.      * <code>super.checkAccess</code> should
  655.      * be called by the first statement in the overridden method, or the 
  656.      * equivalent security check should be placed in the overridden method.
  657.      *
  658.      * @param      t   the thread to be checked.
  659.      * @exception  SecurityException  if the calling thread does not have 
  660.      *             permission to modify the thread.
  661.      * @see        java.lang.Thread#resume() resume
  662.      * @see        java.lang.Thread#setDaemon(boolean) setDaemon
  663.      * @see        java.lang.Thread#setName(java.lang.String) setName
  664.      * @see        java.lang.Thread#setPriority(int) setPriority
  665.      * @see        java.lang.Thread#stop() stop
  666.      * @see        java.lang.Thread#suspend() suspend
  667.      * @see        #checkPermission(java.security.Permission) checkPermission
  668.      */
  669.     public void checkAccess(Thread t) {
  670.     if (t.getThreadGroup() == rootGroup) {
  671.         if (threadPermission == null)
  672.           threadPermission = new RuntimePermission("modifyThread");
  673.         checkPermission(threadPermission);
  674.     } else {
  675.         // just return
  676.     }
  677.     }
  678.     /**
  679.      * Throws a <code>SecurityException</code> if the 
  680.      * calling thread is not allowed to modify the thread group argument. 
  681.      * <p>
  682.      * This method is invoked for the current security manager when a 
  683.      * new child thread or child thread group is created, and by the 
  684.      * <code>setDaemon</code>, <code>setMaxPriority</code>, 
  685.      * <code>stop</code>, <code>suspend</code>, <code>resume</code>, and 
  686.      * <code>destroy</code> methods of class <code>ThreadGroup</code>. 
  687.      * <p>
  688.      * If the thread group argument is the system thread group (
  689.      * has a <code>null</code> parent) then 
  690.      * this method calls <code>checkPermission</code> with the
  691.      * <code>RuntimePermission("modifyThreadGroup")</code> permission.
  692.      * If the thread group argument is <i>not</i> the system thread group,
  693.      * this method just returns silently.
  694.      * <p>
  695.      * Applications that want a stricter policy should override this
  696.      * method. If this method is overridden, the method that overrides
  697.      * it should additionally check to see if the calling thread has the
  698.      * <code>RuntimePermission("modifyThreadGroup")</code> permission, and
  699.      * if so, return silently. This is to ensure that code granted
  700.      * that permission (such as the JDK itself) is allowed to
  701.      * manipulate any thread.
  702.      * <p>
  703.      * If this method is overridden, then 
  704.      * <code>super.checkAccess</code> should
  705.      * be called by the first statement in the overridden method, or the 
  706.      * equivalent security check should be placed in the overridden method.
  707.      *
  708.      * @param      g   the thread group to be checked.
  709.      * @exception  SecurityException  if the calling thread does not have
  710.      *             permission to modify the thread group.
  711.      * @see        java.lang.ThreadGroup#destroy() destroy
  712.      * @see        java.lang.ThreadGroup#resume() resume
  713.      * @see        java.lang.ThreadGroup#setDaemon(boolean) setDaemon
  714.      * @see        java.lang.ThreadGroup#setMaxPriority(int) setMaxPriority
  715.      * @see        java.lang.ThreadGroup#stop() stop
  716.      * @see        java.lang.ThreadGroup#suspend() suspend
  717.      * @see        #checkPermission(java.security.Permission) checkPermission
  718.      */
  719.     public void checkAccess(ThreadGroup g) {
  720.     if (g == rootGroup) {
  721.         if (threadGroupPermission == null)
  722.         threadGroupPermission = 
  723.             new RuntimePermission("modifyThreadGroup");
  724.         checkPermission(threadGroupPermission);
  725.     } else {
  726.         // just return
  727.     }
  728.     }
  729.  
  730.     /**
  731.      * Throws a <code>SecurityException</code> if the 
  732.      * calling thread is not allowed to cause the Java Virtual Machine to 
  733.      * halt with the specified status code. 
  734.      * <p>
  735.      * This method is invoked for the current security manager by the 
  736.      * <code>exit</code> method of class <code>Runtime</code>. A status 
  737.      * of <code>0</code> indicates success; other values indicate various 
  738.      * errors. 
  739.      * <p>
  740.      * This method calls <code>checkPermission</code> with the
  741.      * <code>RuntimePermission("exitVM")</code> permission.
  742.      * <p>
  743.      * If you override this method, then you should make a call to 
  744.      * <code>super.checkExit</code>
  745.      * at the point the overridden method would normally throw an
  746.      * exception.
  747.      *
  748.      * @param      status   the exit status.
  749.      * @exception SecurityException if the calling thread does not have 
  750.      *              permission to halt the Java Virtual Machine with 
  751.      *              the specified status.
  752.      * @see        java.lang.Runtime#exit(int) exit
  753.      * @see        #checkPermission(java.security.Permission) checkPermission
  754.      */
  755.     public void checkExit(int status) {
  756.     checkPermission(new RuntimePermission("exitVM"));
  757.     }
  758.  
  759.     /**
  760.      * Throws a <code>SecurityException</code> if the 
  761.      * calling thread is not allowed to create a subprocess. 
  762.      * <p>
  763.      * This method is invoked for the current security manager by the 
  764.      * <code>exec</code> methods of class <code>Runtime</code>.
  765.      * <p>
  766.      * This method calls <code>checkPermission</code> with the
  767.      * <code>FilePermission(cmd,"execute")</code> permission.
  768.      * <p>
  769.      * If you override this method, then you should make a call to 
  770.      * <code>super.checkExec</code>
  771.      * at the point the overridden method would normally throw an
  772.      * exception.
  773.      *
  774.      * @param      cmd   the specified system command.
  775.      * @exception SecurityException if the calling thread does not have 
  776.      *               permission to create a subprocess.
  777.      * @see     java.lang.Runtime#exec(java.lang.String)
  778.      * @see     java.lang.Runtime#exec(java.lang.String, java.lang.String[])
  779.      * @see     java.lang.Runtime#exec(java.lang.String[])
  780.      * @see     java.lang.Runtime#exec(java.lang.String[], java.lang.String[])
  781.      * @see     #checkPermission(java.security.Permission) checkPermission
  782.      */
  783.     public void checkExec(String cmd) {
  784.     File f = new File(cmd);
  785.     if (f.isAbsolute()) {
  786.         checkPermission(new FilePermission(cmd, "execute"));
  787.     } else {
  788.         checkPermission(new FilePermission("<<ALL FILES>>", "execute"));
  789.     }
  790.     }
  791.  
  792.     /**
  793.      * Throws a <code>SecurityException</code> if the 
  794.      * calling thread is not allowed to dynamic link the library code 
  795.      * specified by the string argument file. The argument is either a 
  796.      * simple library name or a complete filename. 
  797.      * <p>
  798.      * This method is invoked for the current security manager by 
  799.      * methods <code>load</code> and <code>loadLibrary</code> of class 
  800.      * <code>Runtime</code>. 
  801.      * <p>
  802.      * This method calls <code>checkPermission</code> with the
  803.      * <code>RuntimePermission("loadLibrary."+lib)</code> permission.
  804.      * <p>
  805.      * If you override this method, then you should make a call to 
  806.      * <code>super.checkLink</code>
  807.      * at the point the overridden method would normally throw an
  808.      * exception.
  809.      *
  810.      * @param      lib   the name of the library.
  811.      * @exception  SecurityException if the calling thread does not have
  812.      *             permission to dynamically link the library.
  813.      * @see        java.lang.Runtime#load(java.lang.String)
  814.      * @see        java.lang.Runtime#loadLibrary(java.lang.String)
  815.      * @see        #checkPermission(java.security.Permission) checkPermission
  816.      */
  817.     public void checkLink(String lib) {
  818.         checkPermission(new RuntimePermission("loadLibrary."+lib));
  819.     }
  820.  
  821.     /**
  822.      * Throws a <code>SecurityException</code> if the 
  823.      * calling thread is not allowed to read from the specified file 
  824.      * descriptor. 
  825.      * <p>
  826.      * This method calls <code>checkPermission</code> with the
  827.      * <code>RuntimePermission("readFileDescriptor")</code>
  828.      * permission.
  829.      * <p>
  830.      * If you override this method, then you should make a call to 
  831.      * <code>super.checkRead</code>
  832.      * at the point the overridden method would normally throw an
  833.      * exception.
  834.      *
  835.      * @param      fd   the system-dependent file descriptor.
  836.      * @exception  SecurityException  if the calling thread does not have
  837.      *             permission to access the specified file descriptor.
  838.      * @see        java.io.FileDescriptor
  839.      * @see        #checkPermission(java.security.Permission) checkPermission
  840.      */
  841.     public void checkRead(FileDescriptor fd) {
  842.           checkPermission(new RuntimePermission("readFileDescriptor"));
  843.     }
  844.  
  845.     /**
  846.      * Throws a <code>SecurityException</code> if the 
  847.      * calling thread is not allowed to read the file specified by the 
  848.      * string argument. 
  849.      * <p>
  850.      * This method calls <code>checkPermission</code> with the
  851.      * <code>FilePermission(file,"read")</code> permission.
  852.      * <p>
  853.      * If you override this method, then you should make a call to 
  854.      * <code>super.checkRead</code>
  855.      * at the point the overridden method would normally throw an
  856.      * exception. 
  857.      *
  858.      * @param      file   the system-dependent file name.
  859.      * @exception  SecurityException if the calling thread does not have 
  860.      *             permission to access the specified file.
  861.      * @see        #checkPermission(java.security.Permission) checkPermission
  862.      */
  863.     public void checkRead(String file) {
  864.     checkPermission(new FilePermission(file, "read"));
  865.     }
  866.  
  867.     /**
  868.      * Throws a <code>SecurityException</code> if the 
  869.      * specified security context is not allowed to read the file 
  870.      * specified by the string argument. The context must be a security 
  871.      * context returned by a previous call to 
  872.      * <code>getSecurityContext</code>. 
  873.      * <p> If <code>context</code> is an instance of 
  874.      * <code>AccessControlContext</code> then the
  875.      * <code>AccessControlContext.checkPermission</code> method will
  876.      * be invoked with the <code>FilePermission(file,"read")</code> permission.
  877.      * <p> If <code>context</code> is not an instance of 
  878.      * <code>AccessControlContext</code> then a
  879.      * <code>SecurityException</code> is thrown. 
  880.      * <p>
  881.      * If you override this method, then you should make a call to 
  882.      * <code>super.checkRead</code>
  883.      * at the point the overridden method would normally throw an
  884.      * exception. 
  885.      *
  886.      * @param      file      the system-dependent filename.
  887.      * @param      context   a system-dependent security context.
  888.      * @exception  SecurityException  if the specified security context does
  889.      *               not have permission to read the specified file.
  890.      * @see        java.lang.SecurityManager#getSecurityContext()
  891.      * @see        java.security.AccessControlContext#checkPermission(java.security.Permission)
  892.      */
  893.     public void checkRead(String file, Object context) {
  894.     if (context instanceof AccessControlContext) {
  895.         ((AccessControlContext)context).checkPermission(
  896.                    new FilePermission(file, "read"));
  897.     } else{
  898.         throw new SecurityException();
  899.     }
  900.     }
  901.  
  902.     /**
  903.      * Throws a <code>SecurityException</code> if the 
  904.      * calling thread is not allowed to write to the specified file 
  905.      * descriptor. 
  906.      * <p>
  907.      * This method calls <code>checkPermission</code> with the
  908.      * <code>RuntimePermission("writeFileDescriptor")</code>
  909.      * permission.
  910.      * <p>
  911.      * If you override this method, then you should make a call to 
  912.      * <code>super.checkWrite</code>
  913.      * at the point the overridden method would normally throw an
  914.      * exception.
  915.      *
  916.      * @param      fd   the system-dependent file descriptor.
  917.      * @exception SecurityException  if the calling thread does not have
  918.      *             permission to access the specified file descriptor.
  919.      * @see        java.io.FileDescriptor
  920.      * @see        #checkPermission(java.security.Permission) checkPermission
  921.      */
  922.     public void checkWrite(FileDescriptor fd) {
  923.           checkPermission(new RuntimePermission("writeFileDescriptor"));
  924.  
  925.     }
  926.  
  927.     /**
  928.      * Throws a <code>SecurityException</code> if the 
  929.      * calling thread is not allowed to write to the file specified by 
  930.      * the string argument. 
  931.      * <p>
  932.      * This method calls <code>checkPermission</code> with the
  933.      * <code>FilePermission(file,"write")</code> permission.
  934.      * <p>
  935.      * If you override this method, then you should make a call to 
  936.      * <code>super.checkWrite</code>
  937.      * at the point the overridden method would normally throw an
  938.      * exception.
  939.      *
  940.      * @param      file   the system-dependent filename.
  941.      * @exception  SecurityException  if the calling thread does not 
  942.      *             have permission to access the specified file.
  943.      * @see        #checkPermission(java.security.Permission) checkPermission
  944.      */
  945.     public void checkWrite(String file) {
  946.     checkPermission(new FilePermission(file, "write"));
  947.     }
  948.  
  949.     /**
  950.      * Throws a <code>SecurityException</code> if the 
  951.      * calling thread is not allowed to delete the specified file. 
  952.      * <p>
  953.      * This method is invoked for the current security manager by the 
  954.      * <code>delete</code> method of class <code>File</code>.
  955.      * <p>
  956.      * This method calls <code>checkPermission</code> with the
  957.      * <code>FilePermission(file,"delete")</code> permission.
  958.      * <p>
  959.      * If you override this method, then you should make a call to 
  960.      * <code>super.checkDelete</code>
  961.      * at the point the overridden method would normally throw an
  962.      * exception.
  963.      *
  964.      * @param      file   the system-dependent filename.
  965.      * @exception  SecurityException if the calling thread does not 
  966.      *             have permission to delete the file.
  967.      *
  968.      * @see        java.io.File#delete()
  969.      * @see        #checkPermission(java.security.Permission) checkPermission
  970.      */
  971.     public void checkDelete(String file) {
  972.     checkPermission(new FilePermission(file, "delete"));
  973.     }
  974.  
  975.     /**
  976.      * Throws a <code>SecurityException</code> if the 
  977.      * calling thread is not allowed to open a socket connection to the 
  978.      * specified host and port number. 
  979.      * <p>
  980.      * A port number of <code>-1</code> indicates that the calling 
  981.      * method is attempting to determine the IP address of the specified 
  982.      * host name. 
  983.      * <p>
  984.      * This method calls <code>checkPermission</code> with the
  985.      * <code>SocketPermission(host+":"+port,"connect")</code> permission if
  986.      * the port is not equal to -1. If the port is equal to -1, then
  987.      * it calls <code>checkPermission</code> with the
  988.      * <code>SocketPermission(host,"resolve")</code> permission.
  989.      * <p>
  990.      * If you override this method, then you should make a call to 
  991.      * <code>super.checkConnect</code>
  992.      * at the point the overridden method would normally throw an
  993.      * exception.
  994.      *
  995.      * @param      host   the host name port to connect to.
  996.      * @param      port   the protocol port to connect to.
  997.      * @exception  SecurityException  if the calling thread does not have
  998.      *             permission to open a socket connection to the specified
  999.      *               <code>host</code> and <code>port</code>.
  1000.      * @see        #checkPermission(java.security.Permission) checkPermission
  1001.      */
  1002.     public void checkConnect(String host, int port) {
  1003.     if (port == -1) {
  1004.         checkPermission(new SocketPermission(host,"resolve"));
  1005.     } else {
  1006.         checkPermission(new SocketPermission(host+":"+port,"connect"));
  1007.     }
  1008.     }
  1009.  
  1010.     /**
  1011.      * Throws a <code>SecurityException</code> if the 
  1012.      * specified security context is not allowed to open a socket 
  1013.      * connection to the specified host and port number. 
  1014.      * <p>
  1015.      * A port number of <code>-1</code> indicates that the calling 
  1016.      * method is attempting to determine the IP address of the specified 
  1017.      * host name. 
  1018.      * <p> If <code>context</code> is not an instance of 
  1019.      * <code>AccessControlContext</code> then a
  1020.      * <code>SecurityException</code> is thrown.
  1021.      * <p>
  1022.      * Otherwise, the port number is checked. If it is not equal
  1023.      * to -1, the <code>context</code>'s <code>checkPermission</code>
  1024.      * method is called with a 
  1025.      * <code>SocketPermission(host+":"+port,"connect")</code> permission.
  1026.      * If the port is equal to -1, then
  1027.      * the <code>context</code>'s <code>checkPermission</code> method 
  1028.      * is called with a
  1029.      * <code>SocketPermission(host,"resolve")</code> permission.
  1030.      * <p>
  1031.      * If you override this method, then you should make a call to 
  1032.      * <code>super.checkConnect</code>
  1033.      * at the point the overridden method would normally throw an
  1034.      * exception.
  1035.      *
  1036.      * @param      host      the host name port to connect to.
  1037.      * @param      port      the protocol port to connect to.
  1038.      * @param      context   a system-dependent security context.
  1039.      * @exception  SecurityException  if the specified security context does
  1040.      *               not have permission to open a socket connection to the
  1041.      *               specified <code>host</code> and <code>port</code>.
  1042.      * @see        java.lang.SecurityManager#getSecurityContext()
  1043.      * @see        java.security.AccessControlContext#checkPermission(java.security.Permission)
  1044.      */
  1045.     public void checkConnect(String host, int port, Object context) {
  1046.     if (context instanceof AccessControlContext) {
  1047.         if (port == -1) {
  1048.         ((AccessControlContext)context).checkPermission(
  1049.                      new SocketPermission(host,"resolve"));
  1050.         } else {
  1051.         ((AccessControlContext)context).checkPermission(
  1052.                 new SocketPermission(host+":"+port,"connect"));
  1053.         }
  1054.     } else{
  1055.         throw new SecurityException();
  1056.     }
  1057.     }
  1058.  
  1059.     /**
  1060.      * Throws a <code>SecurityException</code> if the 
  1061.      * calling thread is not allowed to wait for a connection request on 
  1062.      * the specified local port number. 
  1063.      * <p>
  1064.      * If port is not 0, this method calls
  1065.      * <code>checkPermission</code> with the
  1066.      * <code>SocketPermission("localhost:"+port,"listen")</code>.
  1067.      * If port is zero, this method calls <code>checkPermission</code>
  1068.      * with <code>SocketPermission("localhost:1024-","listen").</code>
  1069.      * <p>
  1070.      * If you override this method, then you should make a call to 
  1071.      * <code>super.checkListen</code>
  1072.      * at the point the overridden method would normally throw an
  1073.      * exception.
  1074.      *
  1075.      * @param      port   the local port.
  1076.      * @exception  SecurityException  if the calling thread does not have 
  1077.      *             permission to listen on the specified port.
  1078.      * @see        #checkPermission(java.security.Permission) checkPermission
  1079.      */
  1080.     public void checkListen(int port) {
  1081.     if (port == 0) {
  1082.         if (localListenPermission == null)
  1083.         localListenPermission = 
  1084.                           new SocketPermission("localhost:1024-","listen");
  1085.         checkPermission(localListenPermission);
  1086.     } else {
  1087.         checkPermission(new SocketPermission("localhost:"+port,"listen"));
  1088.     }
  1089.     }
  1090.  
  1091.     private static SocketPermission localListenPermission;
  1092.  
  1093.     /**
  1094.      * Throws a <code>SecurityException</code> if the 
  1095.      * calling thread is not permitted to accept a socket connection from 
  1096.      * the specified host and port number. 
  1097.      * <p>
  1098.      * This method is invoked for the current security manager by the 
  1099.      * <code>accept</code> method of class <code>ServerSocket</code>. 
  1100.      * <p>
  1101.      * This method calls <code>checkPermission</code> with the
  1102.      * <code>SocketPermission(host+":"+port,"accept")</code> permission.
  1103.      * <p>
  1104.      * If you override this method, then you should make a call to 
  1105.      * <code>super.checkAccept</code>
  1106.      * at the point the overridden method would normally throw an
  1107.      * exception.
  1108.      *
  1109.      * @param      host   the host name of the socket connection.
  1110.      * @param      port   the port number of the socket connection.
  1111.      * @exception  SecurityException  if the calling thread does not have 
  1112.      *             permission to accept the connection.
  1113.      * @see        java.net.ServerSocket#accept()
  1114.      * @see        #checkPermission(java.security.Permission) checkPermission
  1115.      */
  1116.     public void checkAccept(String host, int port) {
  1117.     checkPermission(new SocketPermission(host+":"+port,"accept"));
  1118.     }
  1119.  
  1120.     /**
  1121.      * Throws a <code>SecurityException</code> if the 
  1122.      * calling thread is not allowed to use
  1123.      * (join/leave/send/receive) IP multicast.
  1124.      * <p>
  1125.      * This method calls <code>checkPermission</code> with the
  1126.      * <code>java.net.SocketPermission(maddr.getHostAddress(), 
  1127.      * "accept,connect")</code> permission.
  1128.      * <p>
  1129.      * If you override this method, then you should make a call to 
  1130.      * <code>super.checkMulticast</code>
  1131.      * at the point the overridden method would normally throw an
  1132.      * exception.
  1133.      *
  1134.      * @param      maddr  Internet group address to be used.
  1135.      * @exception  SecurityException  if the calling thread is not allowed to 
  1136.      *  use (join/leave/send/receive) IP multicast.
  1137.      * @since      JDK1.1
  1138.      * @see        #checkPermission(java.security.Permission) checkPermission
  1139.      */
  1140.     public void checkMulticast(InetAddress maddr) {
  1141.           checkPermission(new SocketPermission(maddr.getHostAddress(), "accept,connect"));
  1142.     }
  1143.  
  1144.     /**
  1145.      * Throws a <code>SecurityException</code> if the 
  1146.      * calling thread is not allowed to use
  1147.      * (join/leave/send/receive) IP multicast.
  1148.      * <p>
  1149.      * This method calls <code>checkPermission</code> with the
  1150.      * <code>java.net.SocketPermission(maddr.getHostAddress(), 
  1151.      * "accept,connect")</code> permission.
  1152.      * <p>
  1153.      * If you override this method, then you should make a call to 
  1154.      * <code>super.checkMulticast</code>
  1155.      * at the point the overridden method would normally throw an
  1156.      * exception. 
  1157.      *
  1158.      * @param      maddr  Internet group address to be used.
  1159.      * @param      ttl        value in use, if it is multicast send.
  1160.      * @exception  SecurityException  if the calling thread is not allowed to 
  1161.      *  use (join/leave/send/receive) IP multicast.
  1162.      * @since      JDK1.1
  1163.      * @see        #checkPermission(java.security.Permission) checkPermission
  1164.      */
  1165.     public void checkMulticast(InetAddress maddr, byte ttl) {
  1166.           checkPermission(new SocketPermission(maddr.getHostAddress(),
  1167.                          "accept,connect"));
  1168.     }
  1169.  
  1170.     /**
  1171.      * Throws a <code>SecurityException</code> if the 
  1172.      * calling thread is not allowed to access or modify the system 
  1173.      * properties. 
  1174.      * <p>
  1175.      * This method is used by the <code>getProperties</code> and 
  1176.      * <code>setProperties</code> methods of class <code>System</code>. 
  1177.      * <p>
  1178.      * This method calls <code>checkPermission</code> with the
  1179.      * <code>PropertyPermission("*", "read,write")</code> permission.
  1180.      * <p>
  1181.      * If you override this method, then you should make a call to 
  1182.      * <code>super.checkPropertiesAccess</code>
  1183.      * at the point the overridden method would normally throw an
  1184.      * exception.
  1185.      * <p>
  1186.      *
  1187.      * @exception  SecurityException  if the calling thread does not have 
  1188.      *             permission to access or modify the system properties.
  1189.      * @see        java.lang.System#getProperties()
  1190.      * @see        java.lang.System#setProperties(java.util.Properties)
  1191.      * @see        #checkPermission(java.security.Permission) checkPermission
  1192.      */
  1193.     public void checkPropertiesAccess() {
  1194.           checkPermission(new PropertyPermission("*", "read,write"));
  1195.     }
  1196.  
  1197.     /**
  1198.      * Throws a <code>SecurityException</code> if the 
  1199.      * calling thread is not allowed to access the system property with 
  1200.      * the specified <code>key</code> name. 
  1201.      * <p>
  1202.      * This method is used by the <code>getProperty</code> method of 
  1203.      * class <code>System</code>. 
  1204.      * <p>
  1205.      * This method calls <code>checkPermission</code> with the
  1206.      * <code>PropertyPermission(key, "read")</code> permission.
  1207.      * <p>
  1208.      * <p>
  1209.      * If you override this method, then you should make a call to 
  1210.      * <code>super.checkPropertyAccess</code>
  1211.      * at the point the overridden method would normally throw an
  1212.      * exception.
  1213.      *
  1214.      * @param      key   a system property key.
  1215.      * @exception  SecurityException  if the calling thread does not have 
  1216.      *             permission to access the specified system property.
  1217.      * @see        java.lang.System#getProperty(java.lang.String)
  1218.      * @see        #checkPermission(java.security.Permission) checkPermission
  1219.      */
  1220.  
  1221.     public void checkPropertyAccess(String key) {
  1222.           checkPermission(new PropertyPermission(key,"read"));
  1223.     }
  1224.  
  1225.     /**
  1226.      * Returns <code>false</code> if the calling 
  1227.      * thread is not trusted to bring up the top-level window indicated 
  1228.      * by the <code>window</code> argument. In this case, the caller can 
  1229.      * still decide to show the window, but the window should include 
  1230.      * some sort of visual warning. If the method returns 
  1231.      * <code>true</code>, then the window can be shown without any 
  1232.      * special restrictions. 
  1233.      * <p>
  1234.      * See class <code>Window</code> for more information on trusted and 
  1235.      * untrusted windows. 
  1236.      * <p>
  1237.      * This method calls
  1238.      * <code>checkPermission</code> with the
  1239.      * <code>AWTPermission("showWindowWithoutWarningBanner")</code> permission,
  1240.      * and returns <code>true</code> if a SecurityException is not thrown,
  1241.      * otherwise it returns <code>false</code>.
  1242.      * <p>
  1243.      * If you override this method, then you should make a call to 
  1244.      * <code>super.checkTopLevelWindow</code>
  1245.      * at the point the overridden method would normally return 
  1246.      * <code>false</code>, and the value of 
  1247.      * <code>super.checkTopLevelWindow</code> should
  1248.      * be returned.
  1249.      *
  1250.      * @param      window   the new window that is being created.
  1251.      * @return     <code>true</code> if the calling thread is trusted to put up
  1252.      *             top-level windows; <code>false</code> otherwise.
  1253.      * @exception  SecurityException  if creation is disallowed entirely.
  1254.      * @see        java.awt.Window
  1255.      * @see        #checkPermission(java.security.Permission) checkPermission
  1256.      */
  1257.     public boolean checkTopLevelWindow(Object window) {
  1258.     try {
  1259.     if (topLevelWindowPermission == null)
  1260.         topLevelWindowPermission =
  1261.         new AWTPermission("showWindowWithoutWarningBanner");
  1262.         checkPermission(topLevelWindowPermission);
  1263.         return true;
  1264.     } catch (SecurityException se) {
  1265.         // just return false
  1266.     }
  1267.     return false;
  1268.     }
  1269.  
  1270.     /**
  1271.      * Throws a <code>SecurityException</code> if the 
  1272.      * calling thread is not allowed to initiate a print job request.
  1273.      * <p>
  1274.      * This method calls
  1275.      * <code>checkPermission</code> with the
  1276.      * <code>RuntimePermission("queuePrintJob")</code> permission.
  1277.      * <p>
  1278.      * If you override this method, then you should make a call to 
  1279.      * <code>super.checkPrintJobAccess</code>
  1280.      * at the point the overridden method would normally throw an
  1281.      * exception.
  1282.      * <p>
  1283.      *
  1284.      * @exception  SecurityException  if the calling thread does not have 
  1285.      *             permission to initiate a print job request.
  1286.      * @since   JDK1.1
  1287.      * @see        #checkPermission(java.security.Permission) checkPermission
  1288.      */
  1289.     public void checkPrintJobAccess() {
  1290.           checkPermission(new RuntimePermission("queuePrintJob"));
  1291.     }
  1292.  
  1293.     /**
  1294.      * Throws a <code>SecurityException</code> if the 
  1295.      * calling thread is not allowed to access the system clipboard.
  1296.      * <p>
  1297.      * This method calls <code>checkPermission</code> with the
  1298.      * <code>AWTPermission("accessClipboard")</code> 
  1299.      * permission.
  1300.      * <p>
  1301.      * If you override this method, then you should make a call to 
  1302.      * <code>super.checkSystemClipboardAccess</code>
  1303.      * at the point the overridden method would normally throw an
  1304.      * exception.
  1305.      *
  1306.      * @since   JDK1.1
  1307.      * @exception  SecurityException  if the calling thread does not have 
  1308.      *             permission to access the system clipboard.
  1309.      * @see        #checkPermission(java.security.Permission) checkPermission
  1310.      */
  1311.     public void checkSystemClipboardAccess() {
  1312.     if (accessClipboardPermission == null)
  1313.         accessClipboardPermission = new AWTPermission("accessClipboard");
  1314.      checkPermission(accessClipboardPermission);
  1315.     }
  1316.  
  1317.     /**
  1318.      * Throws a <code>SecurityException</code> if the 
  1319.      * calling thread is not allowed to access the AWT event queue.
  1320.      * <p>
  1321.      * This method calls <code>checkPermission</code> with the
  1322.      * <code>AWTPermission("accessEventQueue")</code> permission.
  1323.      * <p>
  1324.      * If you override this method, then you should make a call to 
  1325.      * <code>super.checkAwtEventQueueAccess</code>
  1326.      * at the point the overridden method would normally throw an
  1327.      * exception.
  1328.      *
  1329.      * @since   JDK1.1
  1330.      * @exception  SecurityException  if the calling thread does not have 
  1331.      *             permission to access the AWT event queue.
  1332.      * @see        #checkPermission(java.security.Permission) checkPermission
  1333.      */
  1334.     public void checkAwtEventQueueAccess() {
  1335.     if (checkAwtEventQueuePermission == null)
  1336.         checkAwtEventQueuePermission = 
  1337.         new AWTPermission("accessEventQueue");
  1338.           checkPermission(checkAwtEventQueuePermission);
  1339.     }
  1340.  
  1341.     private static String[] packageAccess =
  1342.     getPackages("package.access");
  1343.  
  1344.     private static String[] packageDefinition = 
  1345.     getPackages("package.definition");
  1346.  
  1347.     private static String[] getPackages(String prop) {
  1348.       String packages[] = null;
  1349.       String p = java.security.Security.getProperty(prop);
  1350.       if (p != null && !p.equals("")) {
  1351.       java.util.StringTokenizer tok =
  1352.           new java.util.StringTokenizer(p, ",");
  1353.       int n = tok.countTokens();
  1354.       if (n > 0) {
  1355.           packages =  new String[n];
  1356.           int i = 0;
  1357.           while (tok.hasMoreElements()) {
  1358.           String s = tok.nextToken();
  1359.           packages[i++] = s;
  1360.           }
  1361.       }
  1362.       }
  1363.       if (packages == null)
  1364.       packages = new String[0];
  1365.       return packages;
  1366.     }
  1367.  
  1368.     /**
  1369.      * Throws a <code>SecurityException</code> if the 
  1370.      * calling thread is not allowed to access the package specified by 
  1371.      * the argument. 
  1372.      * <p>
  1373.      * This method is used by the <code>loadClass</code> method of class 
  1374.      * loaders. 
  1375.      * <p>
  1376.      * This method first gets a list of
  1377.      * restricted packages by obtaining a comma-separated list from 
  1378.      * a call to
  1379.      * <code>java.security.Security.getProperty("package.access")</code>,
  1380.      * and checks to see if <code>pkg</code> starts with or equals
  1381.      * any of the restricted packages. If it does, then 
  1382.      * <code>checkPermission</code> gets called with the
  1383.      * <code>RuntimePermission("accessClassInPackage."+pkg)</code>
  1384.      * permission.
  1385.      * <p>
  1386.      * If this method is overridden, then 
  1387.      * <code>super.checkPackageAccess</code> should be called
  1388.      * as the first line in the overridden method.
  1389.      *
  1390.      * @param      pkg   the package name.
  1391.      * @exception  SecurityException  if the calling thread does not have
  1392.      *             permission to access the specified package.
  1393.      * @see        java.lang.ClassLoader#loadClass(java.lang.String, boolean)
  1394.      *  loadClass
  1395.      * @see        java.security.Security#getProperty getProperty
  1396.      * @see        #checkPermission(java.security.Permission) checkPermission
  1397.      */
  1398.     public void checkPackageAccess(String pkg) {
  1399.     for (int i = 0; i < packageAccess.length; i++) {
  1400.         if (pkg.startsWith(packageAccess[i]) ||
  1401.         (pkg.equals(packageAccess[i]))) {
  1402.         checkPermission(
  1403.                new RuntimePermission("accessClassInPackage."+pkg));
  1404.         }
  1405.     }
  1406.     }
  1407.  
  1408.     /**
  1409.      * Throws a <code>SecurityException</code> if the 
  1410.      * calling thread is not allowed to define classes in the package 
  1411.      * specified by the argument. 
  1412.      * <p>
  1413.      * This method is used by the <code>loadClass</code> method of some 
  1414.      * class loaders. 
  1415.      * <p>
  1416.      * This method first gets a list of restricted packages by 
  1417.      * obtaining a comma-separated list from a call to
  1418.      * <code>java.security.Security.getProperty("package.definition")</code>,
  1419.      * and checks to see if <code>pkg</code> starts with or equals
  1420.      * any of the restricted packages. If it does, then
  1421.      * <code>checkPermission</code> gets called with the
  1422.      * <code>RuntimePermission("defineClassInPackage."+pkg)</code>
  1423.      * permission.
  1424.      * <p>
  1425.      * If this method is overridden, then 
  1426.      * <code>super.checkPackageDefinition</code> should be called
  1427.      * as the first line in the overridden method.
  1428.      *
  1429.      * @param      pkg   the package name.
  1430.      * @exception  SecurityException  if the calling thread does not have
  1431.      *             permission to define classes in the specified package.
  1432.      * @see        java.lang.ClassLoader#loadClass(java.lang.String, boolean)
  1433.      * @see        java.security.Security#getProperty getProperty
  1434.      * @see        #checkPermission(java.security.Permission) checkPermission
  1435.      */
  1436.     public void checkPackageDefinition(String pkg) {
  1437.     for (int i = 0; i < packageDefinition.length; i++) {
  1438.         if (pkg.startsWith(packageDefinition[i]) ||
  1439.         pkg.equals(packageDefinition[i])) {
  1440.         checkPermission(
  1441.               new RuntimePermission("defineClassInPackage."+pkg));
  1442.         }
  1443.     }
  1444.     }
  1445.  
  1446.     /**
  1447.      * Throws a <code>SecurityException</code> if the 
  1448.      * calling thread is not allowed to set the socket factory used by 
  1449.      * <code>ServerSocket</code> or <code>Socket</code>, or the stream 
  1450.      * handler factory used by <code>URL</code>. 
  1451.      * <p>
  1452.      * This method calls <code>checkPermission</code> with the
  1453.      * <code>RuntimePermission("setFactory")</code> permission.
  1454.      * <p>
  1455.      * If you override this method, then you should make a call to 
  1456.      * <code>super.checkSetFactory</code>
  1457.      * at the point the overridden method would normally throw an
  1458.      * exception.
  1459.      * <p>
  1460.      *
  1461.      * @exception  SecurityException  if the calling thread does not have 
  1462.      *             permission to specify a socket factory or a stream 
  1463.      *             handler factory.
  1464.      *
  1465.      * @see        java.net.ServerSocket#setSocketFactory(java.net.SocketImplFactory) setSocketFactory
  1466.      * @see        java.net.Socket#setSocketImplFactory(java.net.SocketImplFactory) setSocketImplFactory
  1467.      * @see        java.net.URL#setURLStreamHandlerFactory(java.net.URLStreamHandlerFactory) setURLStreamHandlerFactory
  1468.      * @see        #checkPermission(java.security.Permission) checkPermission
  1469.      */
  1470.     public void checkSetFactory() {
  1471.           checkPermission(new RuntimePermission("setFactory"));
  1472.     }
  1473.  
  1474.     /**
  1475.      * Throws a <code>SecurityException</code> if the 
  1476.      * calling thread is not allowed to access members. 
  1477.      * <p>
  1478.      * The default policy is to allow access to PUBLIC members, as well
  1479.      * as access to classes that have the same class loader as the caller.
  1480.      * In all other cases, this method calls <code>checkPermission</code> 
  1481.      * with the <code>RuntimePermission("accessDeclaredMembers")
  1482.      * </code> permission.
  1483.      * <p>
  1484.      * If this method is overridden, then a call to 
  1485.      * <code>super.checkMemberAccess</code> cannot be made,
  1486.      * as the default implementation of <code>checkMemberAccess</code>
  1487.      * relies on the code being checked being at a stack depth of
  1488.      * 4.
  1489.      * 
  1490.      * @param clazz the class that reflection is to be performed on.
  1491.      *
  1492.      * @param which type of access, PUBLIC or DECLARED.
  1493.      *
  1494.      * @exception  SecurityException if the caller does not have
  1495.      *             permission to access members.
  1496.      *
  1497.      * @see java.lang.reflect.Member
  1498.      * @since JDK1.1
  1499.      * @see        #checkPermission(java.security.Permission) checkPermission
  1500.      */
  1501.     public void checkMemberAccess(Class clazz, int which) {
  1502.     if (which != Member.PUBLIC) {
  1503.         Class stack[] = getClassContext();
  1504.         /*
  1505.          * stack depth of 4 should be the caller of one of the
  1506.          * methods in java.lang.Class that invoke checkMember
  1507.          * access. The stack should look like:
  1508.          * 
  1509.          * someCaller                         [3]
  1510.          * java.lang.Class.someReflectionAPI  [2]
  1511.          * java.lang.Class.checkMemeberAccess [1]
  1512.          * SecurityManager.checkMemeberAccess [0]
  1513.          *
  1514.          */
  1515.         if ((stack.length<4) || 
  1516.         (stack[3].getClassLoader() != clazz.getClassLoader())) {
  1517.         if (checkMemberAccessPermission == null)
  1518.             checkMemberAccessPermission = 
  1519.             new RuntimePermission("accessDeclaredMembers");
  1520.         checkPermission(checkMemberAccessPermission);
  1521.         }
  1522.     }
  1523.     }
  1524.  
  1525.     /**
  1526.      * Determines whether the permission with the specified permission target
  1527.      * name should be granted or denied.
  1528.      *
  1529.      * <p> If the requested permission is allowed, this method returns
  1530.      * quietly. If denied, a SecurityException is raised. 
  1531.      *
  1532.      * <p> This method creates a <code>SecurityPermission</code> object for
  1533.      * the given permission target name and calls <code>checkPermission</code>
  1534.      * with it.
  1535.      *
  1536.      * <p> See the documentation for
  1537.      * <code>{@link java.security.SecurityPermission}</code> for
  1538.      * a list of possible permission target names.
  1539.      * 
  1540.      * <p> If you override this method, then you should make a call to 
  1541.      * <code>super.checkSecurityAccess</code>
  1542.      * at the point the overridden method would normally throw an
  1543.      * exception.
  1544.      *
  1545.      * @param target the target name of the <code>SecurityPermission</code>.
  1546.      *
  1547.      * @exception SecurityException if the calling thread does not have 
  1548.      * permission for the requested access.
  1549.      *
  1550.      * @since   JDK1.1
  1551.      * @see        #checkPermission(java.security.Permission) checkPermission
  1552.      */
  1553.     public void checkSecurityAccess(String target) {
  1554.     checkPermission(new SecurityPermission(target));
  1555.     }
  1556.  
  1557.     private native Class currentLoadedClass0();
  1558.  
  1559.     /**
  1560.      * Returns the thread group into which to instantiate any new
  1561.      * thread being created at the time this is being called.
  1562.      * By default, it returns the thread group of the current
  1563.      * thread. This should be overridden by a specific security
  1564.      * manager to return the appropriate thread group.
  1565.      *
  1566.      * @since   JDK1.1
  1567.      * @see        java.lang.ThreadGroup
  1568.      */
  1569.     public ThreadGroup getThreadGroup() {
  1570.     return Thread.currentThread().getThreadGroup();
  1571.     }
  1572.  
  1573. }    
  1574.