home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD 31 / SUPERCDa.iso / Inet / HotJava / hjava.exe / Windows / resource / jre / lib / rt.jar / sun / applet / AppletSecurity.class (.txt) < prev    next >
Encoding:
Java Class File  |  1998-11-11  |  10.0 KB  |  498 lines

  1. package sun.applet;
  2.  
  3. import java.io.File;
  4. import java.io.FileDescriptor;
  5. import java.io.IOException;
  6. import java.net.InetAddress;
  7. import java.net.URL;
  8. import java.net.UnknownHostException;
  9. import java.security.IdentityScope;
  10. import java.util.Hashtable;
  11. import java.util.StringTokenizer;
  12. import java.util.Vector;
  13. import sun.security.provider.IdentityDatabase;
  14. import sun.security.provider.SystemIdentity;
  15. import sun.security.provider.SystemSigner;
  16.  
  17. public class AppletSecurity extends SecurityManager {
  18.    private static boolean debug;
  19.    static final int NETWORK_NONE = 1;
  20.    static final int NETWORK_HOST = 2;
  21.    static final int NETWORK_UNRESTRICTED = 3;
  22.    private static final int PRIVELEGED_PORT = 1024;
  23.    boolean initACL;
  24.    String[] readACL;
  25.    String[] writeACL;
  26.    int networkMode;
  27.    IdentityScope scope;
  28.    Hashtable loadedClasses;
  29.  
  30.    public AppletSecurity() {
  31.       this.reset();
  32.    }
  33.  
  34.    public void reset() {
  35.       String var1 = System.getProperty("appletviewer.security.mode");
  36.       if (var1 == null) {
  37.          var1 = "host";
  38.       }
  39.  
  40.       if (var1.equals("unrestricted")) {
  41.          this.networkMode = 3;
  42.       } else if (var1.equals("none")) {
  43.          this.networkMode = 1;
  44.       } else {
  45.          this.networkMode = 2;
  46.       }
  47.  
  48.       IdentityScope var2 = IdentityScope.getSystemScope();
  49.       if (var2 instanceof IdentityDatabase) {
  50.          this.scope = (IdentityDatabase)var2;
  51.          this.debug("installing " + var2 + " as the scope for signers.");
  52.       } else {
  53.          this.debug("no signer scope found.");
  54.       }
  55.  
  56.       this.loadedClasses = new Hashtable();
  57.    }
  58.  
  59.    boolean fromApplet() {
  60.       return ((SecurityManager)this).classLoaderDepth() == 1;
  61.    }
  62.  
  63.    protected boolean assessTrust(Object[] var1) {
  64.       for(int var2 = 0; var2 < var1.length; ++var2) {
  65.          if (var1[var2] instanceof SystemIdentity) {
  66.             SystemIdentity var3 = (SystemIdentity)var1[var2];
  67.             if (var3.isTrusted()) {
  68.                return true;
  69.             }
  70.          } else if (var1[var2] instanceof SystemSigner) {
  71.             SystemSigner var4 = (SystemSigner)var1[var2];
  72.             if (var4.isTrusted()) {
  73.                return true;
  74.             }
  75.          }
  76.       }
  77.  
  78.       return false;
  79.    }
  80.  
  81.    boolean inApplet() {
  82.       return ((SecurityManager)this).inClassLoader();
  83.    }
  84.  
  85.    public Object getSecurityContext() {
  86.       ClassLoader var1 = ((SecurityManager)this).currentClassLoader();
  87.       if (var1 == null) {
  88.          return null;
  89.       } else if (var1 instanceof AppletClassLoader) {
  90.          AppletClassLoader var2 = (AppletClassLoader)var1;
  91.          return var2.base;
  92.       } else {
  93.          throw new AppletSecurityException("getsecuritycontext.unknown");
  94.       }
  95.    }
  96.  
  97.    public synchronized boolean getInCheck() {
  98.       return super.getInCheck();
  99.    }
  100.  
  101.    public synchronized void checkCreateClassLoader() {
  102.       if (((SecurityManager)this).classLoaderDepth() == 2) {
  103.          throw new AppletSecurityException("checkcreateclassloader");
  104.       }
  105.    }
  106.  
  107.    protected boolean inThreadGroup(ThreadGroup var1) {
  108.       ClassLoader var2 = ((SecurityManager)this).currentClassLoader();
  109.       if (var2 instanceof AppletClassLoader) {
  110.          AppletClassLoader var3 = (AppletClassLoader)var2;
  111.          ThreadGroup var4 = var3.getThreadGroup();
  112.          return var4.parentOf(var1);
  113.       } else {
  114.          return false;
  115.       }
  116.    }
  117.  
  118.    protected boolean inThreadGroup(Thread var1) {
  119.       return this.inThreadGroup(var1.getThreadGroup());
  120.    }
  121.  
  122.    public synchronized void checkAccess(Thread var1) {
  123.       if (((SecurityManager)this).classLoaderDepth() == 3 && !this.inThreadGroup(var1)) {
  124.          throw new AppletSecurityException("checkaccess.thread");
  125.       }
  126.    }
  127.  
  128.    public synchronized void checkAccess(ThreadGroup var1) {
  129.       if (((SecurityManager)this).classLoaderDepth() == 4 && !this.inThreadGroup(var1)) {
  130.          throw new AppletSecurityException("checkaccess.threadgroup", var1.toString());
  131.       }
  132.    }
  133.  
  134.    public synchronized void checkExit(int var1) {
  135.       if (this.inApplet()) {
  136.          throw new AppletSecurityException("checkexit", String.valueOf(var1));
  137.       }
  138.    }
  139.  
  140.    public synchronized void checkExec(String var1) {
  141.       if (this.inApplet()) {
  142.          throw new AppletSecurityException("checkexec", var1);
  143.       }
  144.    }
  145.  
  146.    public synchronized void checkLink(String var1) {
  147.       switch (((SecurityManager)this).classLoaderDepth()) {
  148.          case 2:
  149.          case 3:
  150.             throw new AppletSecurityException("checklink", var1);
  151.          default:
  152.       }
  153.    }
  154.  
  155.    public synchronized void checkPropertiesAccess() {
  156.       if (((SecurityManager)this).classLoaderDepth() == 2) {
  157.          throw new AppletSecurityException("checkpropsaccess");
  158.       }
  159.    }
  160.  
  161.    public synchronized void checkPropertyAccess(String var1) {
  162.       if (((SecurityManager)this).classLoaderDepth() == 2) {
  163.          String var2 = System.getProperty(var1 + ".applet");
  164.          boolean var3 = new Boolean(var2);
  165.          if (!var3) {
  166.             throw new AppletSecurityException("checkpropsaccess.key", var2);
  167.          }
  168.       }
  169.    }
  170.  
  171.    void parseACL(Vector var1, String var2, String var3) {
  172.       String var4 = System.getProperty("path.separator");
  173.       StringTokenizer var5 = new StringTokenizer(var2, var4);
  174.  
  175.       while(var5.hasMoreTokens()) {
  176.          String var6 = var5.nextToken();
  177.          if (var6.startsWith("~")) {
  178.             var1.addElement(System.getProperty("user.home") + var6.substring(1));
  179.          } else if (var6.equals("+")) {
  180.             if (var3 != null) {
  181.                this.parseACL(var1, var3, (String)null);
  182.             }
  183.          } else {
  184.             var1.addElement(var6);
  185.          }
  186.       }
  187.  
  188.    }
  189.  
  190.    String[] parseACL(String var1, String var2) {
  191.       if (var1 == null) {
  192.          return new String[0];
  193.       } else if (var1.equals("*")) {
  194.          return null;
  195.       } else {
  196.          Vector var3 = new Vector();
  197.          this.parseACL(var3, var1, var2);
  198.          String[] var4 = new String[var3.size()];
  199.          var3.copyInto(var4);
  200.          return var4;
  201.       }
  202.    }
  203.  
  204.    void initializeACLs() {
  205.       this.readACL = this.parseACL(System.getProperty("acl.read"), System.getProperty("acl.read.default"));
  206.       this.writeACL = this.parseACL(System.getProperty("acl.write"), System.getProperty("acl.write.default"));
  207.       this.initACL = true;
  208.    }
  209.  
  210.    public synchronized void checkRead(String var1) {
  211.       ClassLoader var2 = ((SecurityManager)this).currentClassLoader();
  212.       if (var2 != null) {
  213.          if (!(var2 instanceof AppletClassLoader)) {
  214.             throw new AppletSecurityException("checkread.unknown", var1);
  215.          } else {
  216.             AppletClassLoader var3 = (AppletClassLoader)var2;
  217.             this.checkRead(var1, var3.base);
  218.          }
  219.       }
  220.    }
  221.  
  222.    public synchronized void checkRead(String var1, URL var2) {
  223.       if (var2 != null) {
  224.          if (!this.initACL) {
  225.             this.initializeACLs();
  226.          }
  227.  
  228.          if (this.readACL != null) {
  229.             Object var3 = null;
  230.  
  231.             try {
  232.                var10 = (new File(var1)).getCanonicalPath();
  233.             } catch (IOException var9) {
  234.                throw new AppletSecurityException("checkread.exception1", ((Throwable)var9).getMessage(), var1);
  235.             }
  236.  
  237.             int var4 = this.readACL.length;
  238.  
  239.             while(var4-- > 0) {
  240.                if (var10.startsWith(this.readACL[var4])) {
  241.                   return;
  242.                }
  243.             }
  244.  
  245.             String var5 = var2.getHost();
  246.             if (var2.getProtocol().equals("file") && (var5 == null || var5.equals("") || var5.equals("~") || var5.equals("localhost"))) {
  247.                Object var6 = null;
  248.  
  249.                try {
  250.                   var11 = (new File(var2.getFile())).getCanonicalPath();
  251.                } catch (IOException var8) {
  252.                   throw new AppletSecurityException("checkread.exception2", ((Throwable)var8).toString());
  253.                }
  254.  
  255.                if (var10.startsWith(var11)) {
  256.                   return;
  257.                }
  258.             }
  259.  
  260.             throw new AppletSecurityException("checkread", var1, var10);
  261.          }
  262.       }
  263.    }
  264.  
  265.    public void checkRead(String var1, Object var2) {
  266.       this.checkRead(var1);
  267.       if (var2 != null) {
  268.          this.checkRead(var1, (URL)var2);
  269.       }
  270.  
  271.    }
  272.  
  273.    public synchronized void checkWrite(String var1) {
  274.       if (this.inApplet()) {
  275.          if (!this.initACL) {
  276.             this.initializeACLs();
  277.          }
  278.  
  279.          if (this.writeACL != null) {
  280.             Object var2 = null;
  281.  
  282.             try {
  283.                var5 = (new File(var1)).getCanonicalPath();
  284.             } catch (IOException var4) {
  285.                throw new AppletSecurityException("checkwrite.exception", ((Throwable)var4).getMessage(), var1);
  286.             }
  287.  
  288.             int var3 = this.writeACL.length;
  289.  
  290.             while(var3-- > 0) {
  291.                if (var5.startsWith(this.writeACL[var3])) {
  292.                   return;
  293.                }
  294.             }
  295.  
  296.             throw new AppletSecurityException("checkwrite", var1, var5);
  297.          }
  298.       }
  299.    }
  300.  
  301.    public synchronized void checkDelete(String var1) {
  302.       this.checkWrite(var1);
  303.    }
  304.  
  305.    public synchronized void checkRead(FileDescriptor var1) {
  306.       if (this.inApplet() && !((SecurityManager)this).inClass("java.net.SocketInputStream") || !var1.valid()) {
  307.          throw new AppletSecurityException("checkread.fd");
  308.       }
  309.    }
  310.  
  311.    public synchronized void checkWrite(FileDescriptor var1) {
  312.       if (this.inApplet() && !((SecurityManager)this).inClass("java.net.SocketOutputStream") || !var1.valid()) {
  313.          throw new AppletSecurityException("checkwrite.fd");
  314.       }
  315.    }
  316.  
  317.    public synchronized void checkListen(int var1) {
  318.       if (this.inApplet() && var1 > 0 && var1 < 1024) {
  319.          throw new AppletSecurityException("checklisten", String.valueOf(var1));
  320.       }
  321.    }
  322.  
  323.    public synchronized void checkAccept(String var1, int var2) {
  324.       if (this.inApplet() && var2 < 1024) {
  325.          throw new AppletSecurityException("checkaccept", var1, String.valueOf(var2));
  326.       } else {
  327.          this.checkConnect(var1, var2);
  328.       }
  329.    }
  330.  
  331.    public synchronized void checkConnect(String var1, int var2) {
  332.       ClassLoader var3 = ((SecurityManager)this).currentClassLoader();
  333.       if (var3 != null) {
  334.          int var4 = ((SecurityManager)this).classDepth("sun.net.www.http.HttpClient");
  335.          if (var4 <= 1) {
  336.             if (var3 instanceof AppletClassLoader) {
  337.                AppletClassLoader var5 = (AppletClassLoader)var3;
  338.                this.checkConnect(var5.base.getHost(), var1);
  339.             } else {
  340.                throw new AppletSecurityException("checkconnect.unknown");
  341.             }
  342.          }
  343.       }
  344.    }
  345.  
  346.    public void checkConnect(String var1, int var2, Object var3) {
  347.       this.checkConnect(var1, var2);
  348.       if (var3 != null) {
  349.          this.checkConnect(((URL)var3).getHost(), var1);
  350.       }
  351.  
  352.    }
  353.  
  354.    public synchronized void checkConnect(String var1, String var2, boolean var3) {
  355.       if (var1 != null) {
  356.          switch (this.networkMode) {
  357.             case 1:
  358.                throw new AppletSecurityException("checkconnect.networknone", var1, var2);
  359.             case 2:
  360.                try {
  361.                   super.inCheck = true;
  362.                   if (var1.equals(var2)) {
  363.                      try {
  364.                         InetAddress var14 = InetAddress.getByName(var2);
  365.                         return;
  366.                      } catch (UnknownHostException var12) {
  367.                         if (!var3) {
  368.                            throw new AppletSecurityException("checkconnect.networkhost3", var2);
  369.                         }
  370.  
  371.                         return;
  372.                      }
  373.                   }
  374.  
  375.                   try {
  376.                      InetAddress var6 = InetAddress.getByName(var2);
  377.                      InetAddress var7 = InetAddress.getByName(var1);
  378.                      if (!var7.equals(var6)) {
  379.                         throw new AppletSecurityException("checkconnect.networkhost1", var2, var1);
  380.                      }
  381.                   } catch (UnknownHostException var11) {
  382.                      throw new AppletSecurityException("checkconnect.networkhost2", var2, var1);
  383.                   }
  384.                } finally {
  385.                   super.inCheck = false;
  386.                }
  387.  
  388.                return;
  389.             case 3:
  390.                return;
  391.             default:
  392.                throw new AppletSecurityException("checkconnect", var1, var2);
  393.          }
  394.       }
  395.    }
  396.  
  397.    public synchronized void checkConnect(String var1, String var2) {
  398.       this.checkConnect(var1, var2, Boolean.getBoolean("trustProxy"));
  399.    }
  400.  
  401.    public void checkMulticast(InetAddress var1) {
  402.       if (this.inApplet()) {
  403.          throw new AppletSecurityException("checkMulticast");
  404.       }
  405.    }
  406.  
  407.    public void checkMulticast(InetAddress var1, byte var2) {
  408.       if (this.inApplet()) {
  409.          throw new AppletSecurityException("checkMulticast");
  410.       }
  411.    }
  412.  
  413.    public synchronized boolean checkTopLevelWindow(Object var1) {
  414.       return !((SecurityManager)this).inClassLoader();
  415.    }
  416.  
  417.    public synchronized void checkPackageAccess(String var1) {
  418.       if (((SecurityManager)this).inClassLoader()) {
  419.          for(int var2 = var1.indexOf(46); var2 > 0; var2 = var1.indexOf(46, var2 + 1)) {
  420.             String var3 = var1.substring(0, var2);
  421.             if (Boolean.getBoolean("package.restrict.access." + var3)) {
  422.                throw new AppletSecurityException("checkpackageaccess", var1);
  423.             }
  424.          }
  425.  
  426.       }
  427.    }
  428.  
  429.    public synchronized void checkPackageDefinition(String var1) {
  430.       if (((SecurityManager)this).inClassLoader()) {
  431.          for(int var2 = var1.indexOf(46); var2 > 0; var2 = var1.indexOf(46, var2 + 1)) {
  432.             String var3 = var1.substring(0, var2);
  433.             if (Boolean.getBoolean("package.restrict.definition." + var3)) {
  434.                throw new AppletSecurityException("checkpackagedefinition", var1);
  435.             }
  436.          }
  437.  
  438.       }
  439.    }
  440.  
  441.    public synchronized void checkSetFactory() {
  442.       if (this.inApplet() && !((SecurityManager)this).inClass("sun.net.www.MimeTable")) {
  443.          throw new AppletSecurityException("cannotsetfactory");
  444.       }
  445.    }
  446.  
  447.    public void checkMemberAccess(Class var1, int var2) {
  448.       if (var2 != 0) {
  449.          ClassLoader var3 = ((SecurityManager)this).currentClassLoader();
  450.          if (var3 != null && ((SecurityManager)this).classLoaderDepth() <= 3 && var3 != var1.getClassLoader()) {
  451.             throw new AppletSecurityException("checkmemberaccess");
  452.          }
  453.       }
  454.  
  455.    }
  456.  
  457.    public void checkPrintJobAccess() {
  458.       if (this.inApplet()) {
  459.          throw new AppletSecurityException("checkgetprintjob");
  460.       }
  461.    }
  462.  
  463.    public void checkSystemClipboardAccess() {
  464.       if (this.inApplet()) {
  465.          throw new AppletSecurityException("checksystemclipboardaccess");
  466.       }
  467.    }
  468.  
  469.    public void checkAwtEventQueueAccess() {
  470.       if (this.inApplet()) {
  471.          throw new AppletSecurityException("checkawteventqueueaccess");
  472.       }
  473.    }
  474.  
  475.    public void checkSecurityAccess(String var1) {
  476.       if (this.inApplet()) {
  477.          throw new AppletSecurityException("checksecurityaccess", var1);
  478.       }
  479.    }
  480.  
  481.    public ThreadGroup getThreadGroup() {
  482.       ClassLoader var1 = ((SecurityManager)this).currentClassLoader();
  483.       if (var1 != null && var1 instanceof AppletClassLoader) {
  484.          AppletClassLoader var2 = (AppletClassLoader)var1;
  485.          return var2.getThreadGroup();
  486.       } else {
  487.          return super.getThreadGroup();
  488.       }
  489.    }
  490.  
  491.    public void debug(String var1) {
  492.       if (debug) {
  493.          System.err.println(var1);
  494.       }
  495.  
  496.    }
  497. }
  498.