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