home *** CD-ROM | disk | FTP | other *** search
/ Print Shop Ensemble 3 / the-print-shop-ensemble-iii.iso / worldnet / disk2 / java.z / MOZ2_01.ZIP / netscape / applet / AppletSecurity.class (.txt) < prev    next >
Encoding:
Java Class File  |  1996-03-08  |  8.3 KB  |  333 lines

  1. package netscape.applet;
  2.  
  3. import java.io.File;
  4. import java.io.FileDescriptor;
  5. import java.net.InetAddress;
  6. import java.net.URL;
  7. import java.net.UnknownHostException;
  8. import java.util.StringTokenizer;
  9. import java.util.Vector;
  10.  
  11. public class AppletSecurity extends SecurityManager {
  12.    boolean initACL;
  13.    String[] readACL;
  14.    String[] writeACL;
  15.    int networkMode;
  16.    static final int NETWORK_NONE = 1;
  17.    static final int NETWORK_HOST = 2;
  18.    static final int NETWORK_UNRESTRICTED = 3;
  19.  
  20.    public AppletSecurity() {
  21.       this.reset();
  22.    }
  23.  
  24.    void reset() {
  25.       String str = System.getProperty("appletviewer.security.mode");
  26.       if (str == null) {
  27.          str = "host";
  28.       }
  29.  
  30.       if (str.equals("unrestricted")) {
  31.          this.networkMode = 3;
  32.       } else if (str.equals("none")) {
  33.          this.networkMode = 1;
  34.       } else {
  35.          this.networkMode = 2;
  36.       }
  37.    }
  38.  
  39.    boolean fromApplet() {
  40.       return ((SecurityManager)this).classLoaderDepth() == 1;
  41.    }
  42.  
  43.    boolean inApplet() {
  44.       return ((SecurityManager)this).inClassLoader();
  45.    }
  46.  
  47.    public Object getSecurityContext() {
  48.       AppletClassLoader loader = (AppletClassLoader)((SecurityManager)this).currentClassLoader();
  49.       return loader == null ? null : loader.codeBaseURL;
  50.    }
  51.  
  52.    public synchronized void checkCreateClassLoader() {
  53.       if (((SecurityManager)this).classLoaderDepth() == 2) {
  54.          throw new AppletSecurityException("classloader");
  55.       }
  56.    }
  57.  
  58.    public synchronized void checkAccess(Thread t) {
  59.       if (((SecurityManager)this).classLoaderDepth() == 2 && !(t.getThreadGroup() instanceof AppletThreadGroup)) {
  60.          throw new AppletSecurityException("thread");
  61.       }
  62.    }
  63.  
  64.    public synchronized void checkAccess(ThreadGroup g) {
  65.       if (((SecurityManager)this).classLoaderDepth() == 4 && !(g instanceof AppletThreadGroup)) {
  66.          throw new AppletSecurityException("threadgroup", g.toString());
  67.       }
  68.    }
  69.  
  70.    public synchronized void checkExit(int status) {
  71.       if (this.inApplet()) {
  72.          throw new AppletSecurityException("exit", String.valueOf(status));
  73.       }
  74.    }
  75.  
  76.    public synchronized void checkExec(String cmd) {
  77.       if (this.inApplet()) {
  78.          throw new AppletSecurityException("exec", cmd);
  79.       }
  80.    }
  81.  
  82.    public synchronized void checkLink(String lib) {
  83.       switch (((SecurityManager)this).classLoaderDepth()) {
  84.          case 2:
  85.          case 3:
  86.             throw new AppletSecurityException("link", lib);
  87.          default:
  88.       }
  89.    }
  90.  
  91.    public synchronized void checkPropertiesAccess() {
  92.       if (((SecurityManager)this).classLoaderDepth() == 2) {
  93.          throw new AppletSecurityException("properties");
  94.       }
  95.    }
  96.  
  97.    public synchronized void checkPropertyAccess(String key) {
  98.       if (((SecurityManager)this).classLoaderDepth() == 2 && !"true".equalsIgnoreCase(System.getProperty(key + ".applet"))) {
  99.          throw new AppletSecurityException("properties");
  100.       }
  101.    }
  102.  
  103.    void parseACL(Vector v, String path, String defaultPath) {
  104.       StringTokenizer t = new StringTokenizer(path, System.getProperty("path.separator"));
  105.  
  106.       while(t.hasMoreTokens()) {
  107.          String dir = t.nextToken();
  108.          if (dir.startsWith("~")) {
  109.             v.addElement(System.getProperty("user.home") + dir.substring(1));
  110.          } else if (dir.equals("+")) {
  111.             if (defaultPath != null) {
  112.                this.parseACL(v, defaultPath, (String)null);
  113.             }
  114.          } else {
  115.             v.addElement(dir);
  116.          }
  117.       }
  118.  
  119.    }
  120.  
  121.    String[] parseACL(String path, String defaultPath) {
  122.       if (path == null) {
  123.          return new String[0];
  124.       } else if (path.equals("*")) {
  125.          return null;
  126.       } else {
  127.          Vector v = new Vector();
  128.          this.parseACL(v, path, defaultPath);
  129.          String[] acl = new String[v.size()];
  130.          v.copyInto(acl);
  131.          return acl;
  132.       }
  133.    }
  134.  
  135.    void initializeACLs() {
  136.       this.readACL = this.parseACL(System.getProperty("acl.read"), System.getProperty("acl.read.default"));
  137.       this.writeACL = this.parseACL(System.getProperty("acl.write"), System.getProperty("acl.write.default"));
  138.       this.initACL = true;
  139.    }
  140.  
  141.    public synchronized void checkRead(String file) {
  142.       AppletClassLoader loader = (AppletClassLoader)((SecurityManager)this).currentClassLoader();
  143.       if (loader != null) {
  144.          this.checkRead(file, loader.codeBaseURL);
  145.       }
  146.  
  147.    }
  148.  
  149.    public synchronized void checkRead(String file, URL base) {
  150.       if (base != null) {
  151.          if (!this.initACL) {
  152.             this.initializeACLs();
  153.          }
  154.  
  155.          if (this.readACL != null) {
  156.             int i = this.readACL.length;
  157.  
  158.             while(i-- > 0) {
  159.                if (file.startsWith(this.readACL[i])) {
  160.                   return;
  161.                }
  162.             }
  163.  
  164.             if (base.getProtocol().equals("file")) {
  165.                String dir = base.getFile().replace('/', File.separatorChar);
  166.                if (file.startsWith(dir)) {
  167.                   return;
  168.                }
  169.             }
  170.  
  171.             throw new AppletSecurityException("file.read", file);
  172.          }
  173.       }
  174.    }
  175.  
  176.    public void checkRead(String file, Object context) {
  177.       this.checkRead(file);
  178.       if (context != null) {
  179.          this.checkRead(file, (URL)context);
  180.       }
  181.  
  182.    }
  183.  
  184.    public synchronized void checkWrite(String file) {
  185.       if (this.inApplet()) {
  186.          if (!this.initACL) {
  187.             this.initializeACLs();
  188.          }
  189.  
  190.          if (this.writeACL != null) {
  191.             int i = this.writeACL.length;
  192.  
  193.             while(i-- > 0) {
  194.                if (file.startsWith(this.writeACL[i])) {
  195.                   return;
  196.                }
  197.             }
  198.  
  199.             throw new AppletSecurityException("file.write", file);
  200.          }
  201.       }
  202.    }
  203.  
  204.    public synchronized void checkRead(FileDescriptor fd) {
  205.       if (this.inApplet() && !((SecurityManager)this).inClass("java.net.SocketInputStream") || !fd.valid()) {
  206.          throw new AppletSecurityException("fd.read");
  207.       }
  208.    }
  209.  
  210.    public synchronized void checkWrite(FileDescriptor fd) {
  211.       if (this.inApplet() && !((SecurityManager)this).inClass("java.net.SocketOutputStream") || !fd.valid()) {
  212.          throw new AppletSecurityException("fd.write");
  213.       }
  214.    }
  215.  
  216.    public synchronized void checkListen(int port) {
  217.       if (this.inApplet()) {
  218.          throw new AppletSecurityException("socket.listen", String.valueOf(port));
  219.       }
  220.    }
  221.  
  222.    public synchronized void checkAccept(String host, int port) {
  223.       if (this.inApplet()) {
  224.          throw new AppletSecurityException("socket.accept", host + ":" + port);
  225.       }
  226.    }
  227.  
  228.    public synchronized void checkConnect(String host, int port) {
  229.       AppletClassLoader loader = (AppletClassLoader)((SecurityManager)this).currentClassLoader();
  230.       if (loader != null) {
  231.          this.checkConnect(loader.codeBaseURL.getHost(), host);
  232.       }
  233.    }
  234.  
  235.    public void checkConnect(String host, int port, Object context) {
  236.       this.checkConnect(host, port);
  237.       if (context != null) {
  238.          this.checkConnect(((URL)context).getHost(), host);
  239.       }
  240.  
  241.    }
  242.  
  243.    public synchronized void checkConnect(String fromHost, String toHost) {
  244.       if (fromHost != null) {
  245.          switch (this.networkMode) {
  246.             case 1:
  247.                throw new AppletSecurityException("socket.connect", fromHost + "->" + toHost);
  248.             case 2:
  249.                if (fromHost.equals(toHost)) {
  250.                   return;
  251.                }
  252.  
  253.                super.inCheck = true;
  254.  
  255.                try {
  256.                   if (!InetAddress.getByName(fromHost).equals(InetAddress.getByName(toHost))) {
  257.                      break;
  258.                   }
  259.                } catch (UnknownHostException var7) {
  260.                   break;
  261.                } finally {
  262.                   super.inCheck = false;
  263.                }
  264.  
  265.                return;
  266.             case 3:
  267.                return;
  268.          }
  269.  
  270.          throw new AppletSecurityException("socket.connect", fromHost + "->" + toHost);
  271.       }
  272.    }
  273.  
  274.    public synchronized void checkURLConnect(URL url) {
  275.       AppletClassLoader loader = (AppletClassLoader)((SecurityManager)this).currentClassLoader();
  276.       if (loader != null) {
  277.          String codeBaseProtocol = loader.codeBaseURL.getProtocol();
  278.          String protocol = url.getProtocol();
  279.          if (protocol.equals(codeBaseProtocol)) {
  280.             if (protocol.equals("http") || protocol.equals("https") || protocol.equals("ftp") || protocol.equals("gopher")) {
  281.                this.checkConnect(url.getHost(), url.getPort());
  282.                return;
  283.             }
  284.  
  285.             if (protocol.equals("file")) {
  286.                this.checkConnect(url.getHost(), url.getPort());
  287.                String baseDir = loader.codeBaseURL.getFile();
  288.                String file = url.getFile();
  289.                if (baseDir == null || file == null) {
  290.                   return;
  291.                }
  292.  
  293.                if (file.startsWith(baseDir)) {
  294.                   return;
  295.                }
  296.             }
  297.          }
  298.  
  299.          throw new AppletSecurityException("protocol", protocol);
  300.       }
  301.    }
  302.  
  303.    public synchronized boolean checkTopLevelWindow(Object window) {
  304.       return !((SecurityManager)this).inClassLoader();
  305.    }
  306.  
  307.    public synchronized void checkPackageAccess(String pkg) {
  308.       int i = pkg.indexOf(46);
  309.       if (i > 0) {
  310.          pkg = pkg.substring(0, i);
  311.       }
  312.  
  313.       if (((SecurityManager)this).inClassLoader() && Boolean.getBoolean("package.restrict.access." + pkg)) {
  314.          throw new SecurityException();
  315.       }
  316.    }
  317.  
  318.    public synchronized void checkPackageDefinition(String pkg) {
  319.       int i = pkg.indexOf(46);
  320.       if (i > 0) {
  321.          pkg = pkg.substring(0, i);
  322.       }
  323.  
  324.       if (((SecurityManager)this).inClassLoader() && Boolean.getBoolean("package.restrict.definition." + pkg)) {
  325.          throw new SecurityException();
  326.       }
  327.    }
  328.  
  329.    public synchronized void checkSetFactory() {
  330.       throw new SecurityException();
  331.    }
  332. }
  333.