home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 1999 April / DPPCPRO0499.ISO / April / Notes / 50b2wic.exe / DATA1.CAB / NotesProgramFilesJavaSupport / rt.jar / sun / applet / AppletSecurity.class (.txt) < prev    next >
Encoding:
Java Class File  |  1998-04-23  |  9.7 KB  |  479 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.                var9 = (new File(var1)).getCanonicalPath();
  233.             } catch (IOException var8) {
  234.                throw new AppletSecurityException("checkread.exception1", ((Throwable)var8).getMessage(), var1);
  235.             }
  236.  
  237.             int var4 = this.readACL.length;
  238.  
  239.             while(var4-- > 0) {
  240.                if (var9.startsWith(this.readACL[var4])) {
  241.                   return;
  242.                }
  243.             }
  244.  
  245.             if (var2.getProtocol().equals("file")) {
  246.                Object var5 = null;
  247.  
  248.                try {
  249.                   var10 = (new File(var2.getFile())).getCanonicalPath();
  250.                } catch (IOException var7) {
  251.                   throw new AppletSecurityException("checkread.exception2", ((Throwable)var7).toString());
  252.                }
  253.  
  254.                if (var9.startsWith(var10)) {
  255.                   return;
  256.                }
  257.             }
  258.  
  259.             throw new AppletSecurityException("checkread", var1, var9);
  260.          }
  261.       }
  262.    }
  263.  
  264.    public void checkRead(String var1, Object var2) {
  265.       this.checkRead(var1);
  266.       if (var2 != null) {
  267.          this.checkRead(var1, (URL)var2);
  268.       }
  269.  
  270.    }
  271.  
  272.    public synchronized void checkWrite(String var1) {
  273.       if (this.inApplet()) {
  274.          if (!this.initACL) {
  275.             this.initializeACLs();
  276.          }
  277.  
  278.          if (this.writeACL != null) {
  279.             Object var2 = null;
  280.  
  281.             try {
  282.                var5 = (new File(var1)).getCanonicalPath();
  283.             } catch (IOException var4) {
  284.                throw new AppletSecurityException("checkwrite.exception", ((Throwable)var4).getMessage(), var1);
  285.             }
  286.  
  287.             int var3 = this.writeACL.length;
  288.  
  289.             while(var3-- > 0) {
  290.                if (var5.startsWith(this.writeACL[var3])) {
  291.                   return;
  292.                }
  293.             }
  294.  
  295.             throw new AppletSecurityException("checkwrite", var1, var5);
  296.          }
  297.       }
  298.    }
  299.  
  300.    public synchronized void checkRead(FileDescriptor var1) {
  301.       if (this.inApplet() && !((SecurityManager)this).inClass("java.net.SocketInputStream") || !var1.valid()) {
  302.          throw new AppletSecurityException("checkread.fd");
  303.       }
  304.    }
  305.  
  306.    public synchronized void checkWrite(FileDescriptor var1) {
  307.       if (this.inApplet() && !((SecurityManager)this).inClass("java.net.SocketOutputStream") || !var1.valid()) {
  308.          throw new AppletSecurityException("checkwrite.fd");
  309.       }
  310.    }
  311.  
  312.    public synchronized void checkListen(int var1) {
  313.       if (this.inApplet() && var1 > 0 && var1 < 1024) {
  314.          throw new AppletSecurityException("checklisten", String.valueOf(var1));
  315.       }
  316.    }
  317.  
  318.    public synchronized void checkAccept(String var1, int var2) {
  319.       if (this.inApplet() && var2 < 1024) {
  320.          throw new AppletSecurityException("checkaccept", var1, String.valueOf(var2));
  321.       } else {
  322.          this.checkConnect(var1, var2);
  323.       }
  324.    }
  325.  
  326.    public synchronized void checkConnect(String var1, int var2) {
  327.       ClassLoader var3 = ((SecurityManager)this).currentClassLoader();
  328.       if (var3 != null) {
  329.          int var4 = ((SecurityManager)this).classDepth("sun.net.www.http.HttpClient");
  330.          if (var4 <= 1) {
  331.             if (var3 instanceof AppletClassLoader) {
  332.                AppletClassLoader var5 = (AppletClassLoader)var3;
  333.                this.checkConnect(var5.base.getHost(), var1);
  334.             } else {
  335.                throw new AppletSecurityException("checkconnect.unknown");
  336.             }
  337.          }
  338.       }
  339.    }
  340.  
  341.    public void checkConnect(String var1, int var2, Object var3) {
  342.       this.checkConnect(var1, var2);
  343.       if (var3 != null) {
  344.          this.checkConnect(((URL)var3).getHost(), var1);
  345.       }
  346.  
  347.    }
  348.  
  349.    public synchronized void checkConnect(String var1, String var2, boolean var3) {
  350.       if (var1 != null) {
  351.          switch (this.networkMode) {
  352.             case 1:
  353.                throw new AppletSecurityException("checkconnect.networknone", var1, var2);
  354.             case 2:
  355.                try {
  356.                   super.inCheck = true;
  357.                   if (var1.equals(var2)) {
  358.                      try {
  359.                         InetAddress var14 = InetAddress.getByName(var2);
  360.                         return;
  361.                      } catch (UnknownHostException var12) {
  362.                         if (!var3) {
  363.                            throw new AppletSecurityException("checkconnect.networkhost3", var2);
  364.                         }
  365.  
  366.                         return;
  367.                      }
  368.                   }
  369.  
  370.                   try {
  371.                      InetAddress var6 = InetAddress.getByName(var2);
  372.                      InetAddress var7 = InetAddress.getByName(var1);
  373.                      if (!var7.equals(var6)) {
  374.                         throw new AppletSecurityException("checkconnect.networkhost1", var2, var1);
  375.                      }
  376.                   } catch (UnknownHostException var11) {
  377.                      throw new AppletSecurityException("checkconnect.networkhost2", var2, var1);
  378.                   }
  379.                } finally {
  380.                   super.inCheck = false;
  381.                }
  382.  
  383.                return;
  384.             case 3:
  385.                return;
  386.             default:
  387.                throw new AppletSecurityException("checkconnect", var1, var2);
  388.          }
  389.       }
  390.    }
  391.  
  392.    public synchronized void checkConnect(String var1, String var2) {
  393.       this.checkConnect(var1, var2, Boolean.getBoolean("trustProxy"));
  394.    }
  395.  
  396.    public synchronized boolean checkTopLevelWindow(Object var1) {
  397.       return !((SecurityManager)this).inClassLoader();
  398.    }
  399.  
  400.    public synchronized void checkPackageAccess(String var1) {
  401.       if (((SecurityManager)this).inClassLoader()) {
  402.          for(int var2 = var1.indexOf(46); var2 > 0; var2 = var1.indexOf(46, var2 + 1)) {
  403.             String var3 = var1.substring(0, var2);
  404.             if (Boolean.getBoolean("package.restrict.access." + var3)) {
  405.                throw new AppletSecurityException("checkpackageaccess", var1);
  406.             }
  407.          }
  408.  
  409.       }
  410.    }
  411.  
  412.    public synchronized void checkPackageDefinition(String var1) {
  413.       if (((SecurityManager)this).inClassLoader()) {
  414.          for(int var2 = var1.indexOf(46); var2 > 0; var2 = var1.indexOf(46, var2 + 1)) {
  415.             String var3 = var1.substring(0, var2);
  416.             if (Boolean.getBoolean("package.restrict.definition." + var3)) {
  417.                throw new AppletSecurityException("checkpackagedefinition", var1);
  418.             }
  419.          }
  420.  
  421.       }
  422.    }
  423.  
  424.    public synchronized void checkSetFactory() {
  425.       if (this.inApplet() && !((SecurityManager)this).inClass("sun.net.www.MimeTable")) {
  426.          throw new AppletSecurityException("cannotsetfactory");
  427.       }
  428.    }
  429.  
  430.    public void checkMemberAccess(Class var1, int var2) {
  431.       if (var2 != 0) {
  432.          ClassLoader var3 = ((SecurityManager)this).currentClassLoader();
  433.          if (var3 != null && ((SecurityManager)this).classLoaderDepth() <= 3 && var3 != var1.getClassLoader()) {
  434.             throw new AppletSecurityException("checkmemberaccess");
  435.          }
  436.       }
  437.  
  438.    }
  439.  
  440.    public void checkPrintJobAccess() {
  441.       if (this.inApplet()) {
  442.          throw new AppletSecurityException("checkgetprintjob");
  443.       }
  444.    }
  445.  
  446.    public void checkSystemClipboardAccess() {
  447.       if (this.inApplet()) {
  448.          throw new AppletSecurityException("checksystemclipboardaccess");
  449.       }
  450.    }
  451.  
  452.    public void checkAwtEventQueueAccess() {
  453.       throw new AppletSecurityException("checkawteventqueueaccess");
  454.    }
  455.  
  456.    public void checkSecurityAccess(String var1) {
  457.       if (this.inApplet()) {
  458.          throw new AppletSecurityException("checksecurityaccess", var1);
  459.       }
  460.    }
  461.  
  462.    public ThreadGroup getThreadGroup() {
  463.       ClassLoader var1 = ((SecurityManager)this).currentClassLoader();
  464.       if (var1 != null && var1 instanceof AppletClassLoader) {
  465.          AppletClassLoader var2 = (AppletClassLoader)var1;
  466.          return var2.getThreadGroup();
  467.       } else {
  468.          return super.getThreadGroup();
  469.       }
  470.    }
  471.  
  472.    public void debug(String var1) {
  473.       if (debug) {
  474.          System.err.println(var1);
  475.       }
  476.  
  477.    }
  478. }
  479.