home *** CD-ROM | disk | FTP | other *** search
- package netscape.applet;
-
- import java.io.File;
- import java.io.FileDescriptor;
- import java.net.InetAddress;
- import java.net.URL;
- import java.net.UnknownHostException;
- import java.util.StringTokenizer;
- import java.util.Vector;
-
- public class AppletSecurity extends SecurityManager {
- boolean initACL;
- String[] readACL;
- String[] writeACL;
- int networkMode;
- static final int NETWORK_NONE = 1;
- static final int NETWORK_HOST = 2;
- static final int NETWORK_UNRESTRICTED = 3;
-
- public AppletSecurity() {
- this.reset();
- }
-
- void reset() {
- SecurityManager.setScopePermission();
- String str = System.getProperty("appletviewer.security.mode");
- SecurityManager.resetScopePermission();
- if (str == null) {
- str = "host";
- }
-
- if (str.equals("unrestricted")) {
- this.networkMode = 3;
- } else if (str.equals("none")) {
- this.networkMode = 1;
- } else {
- this.networkMode = 2;
- }
- }
-
- public synchronized boolean getInCheck() {
- return super.getInCheck();
- }
-
- boolean fromApplet() {
- return ((SecurityManager)this).checkClassLoader(1);
- }
-
- boolean inApplet() {
- return ((SecurityManager)this).inClassLoader();
- }
-
- public Object getSecurityContext() {
- AppletClassLoader loader = (AppletClassLoader)((SecurityManager)this).currentClassLoader();
- return loader == null ? null : loader.codeBaseURL;
- }
-
- public synchronized void checkCreateClassLoader(int caller_depth) {
- if (((SecurityManager)this).checkClassLoader(caller_depth + 1)) {
- throw new AppletSecurityException("classloader");
- }
- }
-
- public synchronized void checkAccess(Thread t, int caller_depth) {
- if (!((SecurityManager)this).checkScopePermission(caller_depth + 1) && !(t.getThreadGroup() instanceof AppletThreadGroup)) {
- throw new AppletSecurityException("thread");
- }
- }
-
- public synchronized void checkAccess(Thread t, Throwable o, int caller_depth) {
- if (!(o instanceof ThreadDeath) && !((SecurityManager)this).checkScopePermission(caller_depth + 1)) {
- throw new AppletSecurityException("thread can't send exception");
- }
- }
-
- public synchronized void checkAccess(ThreadGroup g, int caller_depth) {
- if (!((SecurityManager)this).checkScopePermission(caller_depth + 1) && !(g instanceof AppletThreadGroup)) {
- throw new AppletSecurityException("threadgroup", g.toString());
- }
- }
-
- public synchronized void checkExit(int status) {
- if (this.inApplet()) {
- throw new AppletSecurityException("exit", String.valueOf(status));
- }
- }
-
- public synchronized void checkExec(String cmd) {
- if (this.inApplet()) {
- throw new AppletSecurityException("exec", cmd);
- }
- }
-
- public synchronized void checkLink(String lib, int caller_depth) {
- if (!((SecurityManager)this).checkScopePermission(caller_depth + 1)) {
- throw new AppletSecurityException("link", lib);
- }
- }
-
- public synchronized void checkPropertiesAccess(int caller_depth) {
- if (!((SecurityManager)this).checkScopePermission(caller_depth + 1)) {
- throw new AppletSecurityException("properties");
- }
- }
-
- public synchronized void checkPropertyAccess(String key, int caller_depth) {
- if (!((SecurityManager)this).checkScopePermission(caller_depth + 1)) {
- SecurityManager.setScopePermission();
- if (!"true".equalsIgnoreCase(System.getProperty(key + ".applet"))) {
- throw new AppletSecurityException("properties");
- }
- }
-
- }
-
- void parseACL(Vector v, String path, String defaultPath) {
- SecurityManager.setScopePermission();
- StringTokenizer t = new StringTokenizer(path, System.getProperty("path.separator"));
-
- while(t.hasMoreTokens()) {
- String dir = t.nextToken();
- if (dir.startsWith("~")) {
- v.addElement(System.getProperty("user.home") + dir.substring(1));
- } else if (dir.equals("+")) {
- if (defaultPath != null) {
- this.parseACL(v, defaultPath, (String)null);
- }
- } else {
- v.addElement(dir);
- }
- }
-
- }
-
- String[] parseACL(String path, String defaultPath) {
- if (path == null) {
- return new String[0];
- } else if (path.equals("*")) {
- return null;
- } else {
- Vector v = new Vector();
- this.parseACL(v, path, defaultPath);
- String[] acl = new String[v.size()];
- v.copyInto(acl);
- return acl;
- }
- }
-
- void initializeACLs() {
- SecurityManager.setScopePermission();
- this.readACL = this.parseACL(System.getProperty("acl.read"), System.getProperty("acl.read.default"));
- this.writeACL = this.parseACL(System.getProperty("acl.write"), System.getProperty("acl.write.default"));
- this.initACL = true;
- }
-
- public synchronized void checkRead(String file) {
- AppletClassLoader loader = (AppletClassLoader)((SecurityManager)this).currentClassLoader();
- if (loader != null) {
- this.checkRead(file, loader.codeBaseURL);
- }
-
- }
-
- public synchronized void checkRead(String file, URL base) {
- if (base != null) {
- if (!this.initACL) {
- this.initializeACLs();
- }
-
- if (this.readACL != null) {
- int i = this.readACL.length;
-
- while(i-- > 0) {
- if (file.startsWith(this.readACL[i])) {
- return;
- }
- }
-
- if (base.getProtocol().equals("file")) {
- String dir = base.getFile().replace('/', File.separatorChar);
- if (file.startsWith(dir)) {
- return;
- }
- }
-
- throw new AppletSecurityException("file.read", file);
- }
- }
- }
-
- public void checkRead(String file, Object context) {
- this.checkRead(file);
- if (context != null) {
- this.checkRead(file, (URL)context);
- }
-
- }
-
- public synchronized void checkWrite(String file) {
- if (this.inApplet()) {
- if (!this.initACL) {
- this.initializeACLs();
- }
-
- if (this.writeACL != null) {
- int i = this.writeACL.length;
-
- while(i-- > 0) {
- if (file.startsWith(this.writeACL[i])) {
- return;
- }
- }
-
- throw new AppletSecurityException("file.write", file);
- }
- }
- }
-
- public synchronized void checkRead(FileDescriptor fd) {
- if (this.inApplet() && !((SecurityManager)this).inClass("java.net.SocketInputStream") || !fd.valid()) {
- throw new AppletSecurityException("fd.read");
- }
- }
-
- public synchronized void checkWrite(FileDescriptor fd) {
- if (this.inApplet() && !((SecurityManager)this).inClass("java.net.SocketOutputStream") || !fd.valid()) {
- throw new AppletSecurityException("fd.write");
- }
- }
-
- public synchronized void checkListen(int port) {
- AppletClassLoader loader = (AppletClassLoader)((SecurityManager)this).currentClassLoader();
- if (loader != null) {
- if (port <= 1024) {
- throw new AppletSecurityException("socket.listen", String.valueOf(port));
- }
- }
- }
-
- public synchronized void checkAccept(String host, int port) {
- AppletClassLoader loader = (AppletClassLoader)((SecurityManager)this).currentClassLoader();
- if (loader != null) {
- this.checkConnect(loader.codeBaseURL.getHost(), host);
- }
- }
-
- public synchronized void checkConnect(String host, int port) {
- AppletClassLoader loader = (AppletClassLoader)((SecurityManager)this).currentClassLoader();
- if (loader != null) {
- this.checkConnect(loader.codeBaseURL.getHost(), host);
- }
- }
-
- public void checkConnect(String host, int port, Object context) {
- this.checkConnect(host, port);
- if (context != null) {
- this.checkConnect(((URL)context).getHost(), host);
- }
-
- }
-
- public synchronized void checkConnect(String fromHost, String toHost) {
- if (fromHost != null) {
- switch (this.networkMode) {
- case 1:
- throw new AppletSecurityException("socket.connect", fromHost + "->" + toHost);
- case 2:
- if (fromHost.length() == 0 && toHost.length() == 0) {
- return;
- }
-
- super.inCheck = true;
-
- try {
- if (!InetAddress.getByName(fromHost).equals(InetAddress.getByName(toHost))) {
- break;
- }
- } catch (UnknownHostException var7) {
- break;
- } finally {
- super.inCheck = false;
- }
-
- return;
- case 3:
- return;
- }
-
- throw new AppletSecurityException("socket.connect", fromHost + "->" + toHost);
- }
- }
-
- public synchronized void checkURLConnect(URL url) {
- AppletClassLoader loader = (AppletClassLoader)((SecurityManager)this).currentClassLoader();
- if (loader != null) {
- String codeBaseProtocol = loader.codeBaseURL.getProtocol();
- String protocol = url.getProtocol();
- if (protocol.equals(codeBaseProtocol)) {
- if (protocol.equals("http") || protocol.equals("https") || protocol.equals("ftp") || protocol.equals("gopher")) {
- this.checkConnect(url.getHost(), url.getPort());
- return;
- }
-
- if (protocol.equals("file")) {
- this.checkConnect(url.getHost(), url.getPort());
- String baseDir = loader.codeBaseURL.getFile();
- String file = url.getFile();
- if (baseDir == null || file == null) {
- return;
- }
-
- if (file.startsWith(baseDir)) {
- return;
- }
- }
- }
-
- throw new AppletSecurityException("protocol", protocol);
- }
- }
-
- public synchronized boolean checkTopLevelWindow(Object window) {
- return !((SecurityManager)this).inClassLoader();
- }
-
- public synchronized void checkPackageAccess(String pkg) {
- int i = pkg.indexOf(46);
- if (i > 0) {
- pkg = pkg.substring(0, i);
- }
-
- if (((SecurityManager)this).inClassLoader() && Boolean.getBoolean("package.restrict.access." + pkg)) {
- throw new SecurityException();
- }
- }
-
- public synchronized void checkPackageDefinition(String pkg) {
- int i = pkg.indexOf(46);
- if (i > 0) {
- pkg = pkg.substring(0, i);
- }
-
- if (((SecurityManager)this).inClassLoader() && Boolean.getBoolean("package.restrict.definition." + pkg)) {
- throw new SecurityException();
- }
- }
-
- public synchronized void checkSetFactory() {
- throw new SecurityException();
- }
- }
-