home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1997 August / PCO0897.ISO / browser / tonline / ie32.exe / IEXPLORE.CAB / TCLASSES.ZIP / sun / applet / AppletSecurity.class (.txt) < prev    next >
Encoding:
Java Class File  |  1996-10-01  |  6.4 KB  |  348 lines

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