import java.io.*;
import java.net.*;
import java.util.*;
public class SimpleSecurityManager extends SecurityManager {
/**
* This method returns true if invoked indirectly from an applet.
* The assumption is that there are no class loaders except those
* used to load the applet.
*/
boolean inApplet() {
return inClassLoader();
}
/**
* Applets may not create new class loaders.
*/
public synchronized void checkCreateClassLoader() {
if (classLoaderDepth() == 2) {
throw new SecurityException("Applets may not create new class loaders");
}
}
/**
* Applets may not manipulate threads outside their own thread group.
* This is actually a stronger restriction than imposed by Java 1.0
* in which threads are allowed to manipulate any applet thread group.
*/
public synchronized void checkAccess(Thread t) {
if (classLoaderDepth()==2) {
ThreadGroup ctg = Thread.currentThread().getThreadGroup();
if (t.getThreadGroup() != ctg) {
throw new SecurityException("Applets may not manipulate threads outside their own thread group");
}
}
}
/**
* Applets may only manipulate their own thread group
*/
public synchronized void checkAccess(ThreadGroup tg) {
if (classLoaderDepth()==4 && (tg != Thread.currentThread().getThreadGroup())) {
throw new SecurityException("Applets may only manipulate their own thread group");
}
}
/**
* Applets may not call System.exit().
*/
public synchronized void checkExit(int status) {
if (inApplet()) {
throw new SecurityException("Applets may not exit the VM");
}
}
/**
* Applets may not call System.exec()
*/
public synchronized void checkExec(String cmd){
if (inApplet()) {
throw new SecurityException("Applets may not call system commands");
}
}
/**
* Applets may not link to native libraries.
*/
public synchronized void checkLink(String lib){
if (classLoaderDepth() == 3) {
throw new SecurityException("Applets may not link to native libraries.");
}
}
/**
* Applets may not read the entire system properties list
*/
public synchronized void checkPropertiesAccess() {
if (classLoaderDepth() == 2) {
throw new SecurityException("Applets may not read the entire system properties list");
}
}
/**
* Applets may only read the system property foo
* if the system property foo.applet exists and has
* the String value "true".
*/
public synchronized void checkPropertyAccess(String name) {
if (classLoaderDepth() == 2) {
if (!((System.getProperty(name + ".applet").equalsIgnoreCase("true")))) {
throw new SecurityException("Cannot read system property " + name);
}
}
}
/**
* Applets may not read files unless they're loaded from a file URL.
*/
public synchronized void checkRead(String file) {
URLClassLoader loader = (URLClassLoader) currentClassLoader();
if (loader == null) return;
if (loader.getURL().getProtocol().equalsIgnoreCase("file")) return;
throw new SecurityException("Applets cannot read files");
}
public void checkRead(String file, Object context) {
if (context != null) {
URL u = (URL) context;
if (!(u.getProtocol().equalsIgnoreCase("file"))) {
throw new SecurityException("Applets cannot read files");
}
}
}
/**
* Applets may not write files.
*/
public synchronized void checkWrite(String file) {
throw new SecurityException("Applets may not write files.");
}
/**
* Applets may not read from non-socket file descriptors
*/
public synchronized void checkRead(FileDescriptor fd) {
if ((inApplet() && !inClass("java.net.SocketInputStream")) || (!fd.valid()) ) {
throw new SecurityException("Applets cannot open file descriptors");
}
}
/**
* Applets may not write to non-socket file descriptors
*/
public synchronized void checkWrite(FileDescriptor fd) {
if ((inApplet() && !inClass("java.net.SocketInputStream")) || (!fd.valid()) ) {
throw new SecurityException("Applets cannot open file descriptors");
}
}
/**
* Applets may not open server sockets
*/
public synchronized void checkListen(int port) {
if (inApplet()) {
throw new SecurityException("Applets may not open server sockets");
}
}
/**
* Applets may not open server sockets.
*/
public synchronized void checkAccept(String host, int port) {
throw new SecurityException("Applets may not open server sockets");
}
/**
* Check if an applet can connect to the given host:port.
*/
public synchronized void checkConnect(String remoteHost, int port) {
URLClassLoader loader = (URLClassLoader) currentClassLoader();
if (loader == null) {
return;
}
String localHost = loader.getURL().getHost();
if (remoteHost.equals(localHost)) return;
try {
inCheck = true;
if (InetAddress.getByName(localHost).equals(InetAddress.getByName(remoteHost))) {
return;
}
}
catch (UnknownHostException e) {
}
finally {
inCheck = false;
}
throw new SecurityException("Cannot open a socket to " + remoteHost);
}
public void checkConnect(String host, int port, Object context) {
checkConnect(host, port);
}
/**
* Applets may not create top-level windows
*/
public synchronized boolean checkTopLevelWindow(Object window) {
if (inClassLoader()) return false;
return true;
}
/**
* Allow applets unrestricted package access.
*/
public synchronized void checkPackageAccess(String pkg) {
}
/**
* Allow applets to define packages
*/
public synchronized void checkPackageDefinition(String pkg) {
}
/**
* Applets may not set a networking-related object factory.
*/
public synchronized void checkSetFactory() {
throw new SecurityException("Applets cannot set network factories");
}
// New methods in 1.1
/**
* Applets may not access the AWT event queue
*/
public synchronized void checkAwtEventQueueAccess() {
if (inClassLoader()) {
throw new SecurityException("Applets may not access the AWT event queue directly");
}
}
/**
* Applets may not multicast
*/
public synchronized void checkMulticast(InetAddress maddr) {
throw new SecurityException("Applets may not multicast");
}
/**
* Applets may not multicast
*/
public synchronized void checkMulticast(InetAddress maddr, byte ttl) {
throw new SecurityException("Applets may not multicast");
}
/**
* Applets may not print
*/
public synchronized void checkPrintJobAccess() {
if (inClassLoader()) {
throw new SecurityException("Applets may not print");
}
}
/**
* Applets may not access the System clipboard directly
*/
public synchronized void checkSystemClipboardAccess() {
if (inClassLoader()) {
throw new SecurityException("Applets may not access the System clipboard");
}
}
/**
* Applets may only access the public members or a class through reflection
*/
public synchronized void checkMemberAccess(Class c, int type) {
if (type != 0) {
throw new SecurityException("Applets may not access declared members");
}
}
/**
* For now, do not restrict access based on the provider
*/
public synchronized void checkSecurityAccess(String provider) {
}
}