home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-03-20 | 9.0 KB | 335 lines |
- /*
- * @(#)RMISecurityManager.java 1.17 98/03/18
- *
- * Copyright 1996-1998 by Sun Microsystems, Inc.,
- * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
- * All rights reserved.
- *
- * This software is the confidential and proprietary information
- * of Sun Microsystems, Inc. ("Confidential Information"). You
- * shall not disclose such Confidential Information and shall use
- * it only in accordance with the terms of the license agreement
- * you entered into with Sun.
- */
-
- package java.rmi;
-
- import java.security.AccessController;
- import java.security.AccessControlException;
- import java.io.IOException;
- import java.io.File;
- import java.io.FilePermission;
- import java.net.InetAddress;
- import java.net.SocketPermission;
- import java.net.UnknownHostException;
- import java.net.URL;
- import java.rmi.server.RMIClassLoader;
-
- /**
- * <code>RMISecurityManager</code> provides a default security manager
- * for use by RMI applications that need one because they use downloaded
- * code. RMI's class loader will not download any classes if no security
- * manager has been set. <code>RMISecurityManager</code> does not apply
- * to applets, which run under the protection of their browser's security
- * manager.<p>
- *
- * To set the <code>RMISecurityManager</code>, add the following to an
- * application's main() method:<p>
- *
- * <pre>
- * System.setSecurityManager(new RMISecurityManager());
- * </pre><p>
- *
- * The <code>RMISecurityManager</code> follows the same policy as the
- * <code>java.lang.SecurityManager</code> class for security check
- * methods that it does not override.
- *
- * @author Roger Riggs
- * @author Peter Jones
- * @version 1.17, 03/18/98
- * @since JDK1.1
- */
- public class RMISecurityManager extends SecurityManager {
-
- /**
- * Construct a new <code>RMISecurityManager</code> object.
- */
- public RMISecurityManager() {
- }
-
- /**
- * Return the security context (e.g., a <code>URL</code>).
- */
- public Object getSecurityContext() {
- return RMIClassLoader.getSecurityContext(currentClassLoader());
- }
-
- /**
- * Check access to threads.
- */
- public synchronized void checkAccess(Thread t) {
- super.checkAccess(t);
- }
-
- /**
- * Check access to threads.
- */
- public synchronized void checkAccess(ThreadGroup g) {
- super.checkAccess(g);
- }
-
- /**
- * Check file read access.
- */
- public synchronized void checkRead(String file) {
- try {
- // allow it if the AccessController does
- super.checkRead(file);
- return;
- } catch (SecurityException se) {
- }
-
- // allow if file is inside of security context URL base
- Object url = getSecurityContext();
- if (url != null && url instanceof java.net.URL) {
- checkRead(file, (URL) url);
- } else {
- FilePermission fp = new FilePermission(file, "read");
- throw new AccessControlException("access denied " + fp, fp);
- }
- }
-
- /**
- * Check file read access on behalf of the given context.
- */
- public void checkRead(String file, Object context) {
- checkRead(file);
- if (context != null) {
- if (context instanceof URL) {
- checkRead(file, (URL) context);
- } else {
- FilePermission fp = new FilePermission(file, "read");
- throw new AccessControlException(
- "access denied (unknown context) " + fp, fp);
- }
- }
- }
-
- /**
- * Throw <code>AccessControlException</code> if given file is not
- * within given URL.
- */
- private synchronized void checkRead(String file, URL base) {
- String realPath = null;
- try {
- AccessController.beginPrivileged();
- realPath = (new File(file)).getCanonicalPath();
- } catch (IOException e) {
- FilePermission fp = new FilePermission(file, "read");
- throw new AccessControlException("access denied " + fp, fp);
- } finally {
- AccessController.endPrivileged();
- }
-
- // if base is a "file:" URL, allow reading in that directory
- if (base.getProtocol().equals("file")) {
- String dir = null;
- try {
- AccessController.beginPrivileged();
- dir = (new File(base.getFile()).getCanonicalPath());
- } catch (IOException e) { // shouldn't happen
- FilePermission fp = new FilePermission(file, "read");
- throw new AccessControlException("access denied " + fp, fp);
- } finally {
- AccessController.endPrivileged();
- }
- if (realPath.startsWith(dir)) {
- return;
- }
- }
-
- FilePermission fp = new FilePermission(file, "read");
- throw new AccessControlException("access denied " + fp, fp);
- }
-
- /**
- * Check if a network connection can be made to the given
- * host and port.
- */
- public synchronized void checkConnect(String host, int port) {
- try {
- // allow it if the AccessController does
- super.checkConnect(host, port);
- return;
- } catch (SecurityException se) {
- }
-
- // REMIND: This is only appropriate for sun.* implementations.
- int depth = classDepth("sun.net.www.http.HttpClient");
- if (depth >= 0) {
- // called through sun http protocol handler
- return;
- }
- depth = classDepth("sun.rmi.transport.tcp.TCPChannel");
- if (depth >= 0) {
- // called through sun rmi transport
- return;
- }
-
- Object url = getSecurityContext();
- if (url != null && url instanceof java.net.URL) {
- checkConnect(((URL) url).getHost(), host);
- } else {
- SocketPermission sp =
- new SocketPermission(host + ":" + port, "connect");
- throw new AccessControlException("access denied " + sp, sp);
- }
- }
-
- /**
- * Check if a network connection can be made to the given
- * host and port on behalf of the given context.
- */
- public void checkConnect(String host, int port, Object context) {
- checkConnect(host, port);
- if (context != null) {
- if (context instanceof URL) {
- checkConnect(((URL) context).getHost(), host);
- } else {
- SocketPermission sp =
- new SocketPermission(host + ":" + port, "connect");
- throw new AccessControlException(
- "access denied (unknown context) " + sp, sp);
- }
- }
- }
-
- /**
- * Throw <code>AccessControlException</code> if attempted connection
- * is not to he host that it is acting on behalf of.
- */
- private synchronized void checkConnect(String fromHost, String toHost) {
- InetAddress toHostAddr, fromHostAddr;
- if (!fromHost.equals(toHost)) {
- try {
- // only allow non-matching strings when the IPs match
- try {
- AccessController.beginPrivileged();
- toHostAddr = InetAddress.getByName(toHost);
- fromHostAddr = InetAddress.getByName(fromHost);
- } finally {
- AccessController.endPrivileged();
- }
-
- if (fromHostAddr.equals(toHostAddr)) {
- return;
- } else {
- throw new AccessControlException(
- "connect from " + fromHost +
- " to " + toHost + " denied");
- }
- } catch (UnknownHostException e) {
- throw new AccessControlException(
- "connect from " + fromHost +
- " to " + toHost + " denied");
- }
- } else {
- try {
- // strings match: must have IP
- try {
- AccessController.beginPrivileged();
- toHostAddr = InetAddress.getByName(toHost);
- } finally {
- AccessController.endPrivileged();
- }
- return;
- } catch (UnknownHostException e) {
- throw new AccessControlException(
- "connect from " + fromHost +
- " to " + toHost + " denied");
- }
- }
- }
-
- /**
- * Check if a network connection can be accepted from the
- * given host on the given port.
- */
- public synchronized void checkAccept(String host, int port) {
- try {
- // allow it if the AccessController does
- super.checkAccept(host, port);
- return;
- } catch (SecurityException se) {
- }
-
- if (inClassLoader() && port < 1024) {
- SocketPermission sp =
- new SocketPermission(host + ":" + port, "accept");
- throw new AccessControlException("access denied " + sp, sp);
- }
- }
-
- /**
- * Check access to classes of a given package.
- */
- public synchronized void checkPackageAccess(String pkg) {
- try {
- // allow it if the AccessController does
- super.checkPackageAccess(pkg);
- return;
- } catch (SecurityException se) {
- }
-
- int i = pkg.indexOf('.');
- try {
- AccessController.beginPrivileged();
- while (i > 0) {
- String subpkg = pkg.substring(0, i);
- if (Boolean.getBoolean("package.restrict.access." + subpkg)) {
- throw new AccessControlException(
- "checkaccessdefinition " + pkg);
- }
- i = pkg.indexOf('.', i + 1);
- }
- } finally {
- AccessController.endPrivileged();
- }
- }
-
- /**
- * Check access to defining classes of a given package.
- */
- public synchronized void checkPackageDefinition(String pkg) {
- try {
- // allow it if the AccessController does
- super.checkPackageDefinition(pkg);
- return;
- } catch (SecurityException se) {
- }
-
- int i = pkg.indexOf('.');
- try {
- AccessController.beginPrivileged();
- while (i > 0) {
- String subpkg = pkg.substring(0, i);
- if (Boolean.getBoolean("package.restrict.definition." +
- subpkg)) {
- throw new AccessControlException(
- "checkpackagedefinition " + pkg);
- }
- i = pkg.indexOf('.', i + 1);
- }
- } finally {
- AccessController.endPrivileged();
- }
- }
-
- /**
- * Return the thread group that new threads should be created in.
- */
- public ThreadGroup getThreadGroup() {
- return Thread.currentThread().getThreadGroup();
- }
- }
-