home *** CD-ROM | disk | FTP | other *** search
/ PC User 1998 October / Image.iso / BROWSER / CLASSES.ZIP / MS / APPLET / AppletSecurity.class (.txt) < prev    next >
Encoding:
Java Class File  |  1997-04-14  |  6.6 KB  |  372 lines

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