home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 39 / IOPROG_39.ISO / SOFT / sdkjava40.exe / data1.cab / fg_Samples / Samples / Security / rni / Main.java < prev    next >
Encoding:
Java Source  |  2000-05-04  |  6.5 KB  |  242 lines

  1. // demo.java
  2. //
  3. //
  4. // (C) Copyright 1995 - 1999 Microsoft Corporation.  All rights reserved.
  5. //
  6.  
  7. package demo;
  8.  
  9. import java.applet.*;
  10. import java.io.PrintStream;
  11. import rnisamp.*;
  12. import java.io.ByteArrayOutputStream;
  13. import java.awt.TextArea;
  14. import java.awt.BorderLayout;
  15. import com.ms.security.PolicyEngine;
  16. import com.ms.security.PermissionID;
  17. import com.ms.security.SecurityExceptionEx;
  18. import com.ms.com.ComFailException;
  19.  
  20.  
  21. public class Main extends Applet implements Runnable
  22. {
  23.     static PrintStream out;
  24.     static String untrustedcaller;
  25.     
  26.  
  27.     public void init ()
  28.     {
  29.         TextArea ta = new TextArea();
  30.         setLayout(new BorderLayout());
  31.         add("Center", ta);
  32.         out = new PrintStream(new TextAreaOutputStream(ta), true);
  33.     }
  34.  
  35.     boolean isAppletFullyTrusted ()
  36.     {
  37.         try
  38.         {
  39.             PolicyEngine.checkCallerForAllPermissions(null);
  40.             return true;
  41.         }
  42.         catch (SecurityException e)
  43.         {
  44.             return false;
  45.         }
  46.     }
  47.  
  48.     boolean isPageFullyTrusted ()
  49.     {
  50.         Class[] skip = { Main.class };
  51.         try
  52.         {
  53.             PolicyEngine.checkCallerForAllPermissions(skip);
  54.             return true;
  55.         }
  56.         catch (SecurityException e)
  57.         {
  58.             return false;
  59.         }
  60.     }
  61.  
  62.     public void start ()
  63.     {
  64.         boolean applettrusted = isAppletFullyTrusted();
  65.         boolean pagetrusted = isPageFullyTrusted();
  66.  
  67.         out.print("Applet is ");
  68.         if (!applettrusted)
  69.             out.print("not ");
  70.         out.println("fully trusted.");
  71.  
  72.         out.print("Page is ");
  73.         if (!pagetrusted)
  74.             out.print("not ");
  75.         out.println("fully trusted.");
  76.         
  77.         out.println();
  78.  
  79.         if (!applettrusted)
  80.             untrustedcaller = "applet is not trusted";
  81.         else if (!pagetrusted)
  82.             untrustedcaller = "docbase is not trusted";
  83.             
  84.         // start is 'called' from the HTML page containing the applet,
  85.         // so there is an extra frame above start with the privileges
  86.         // of the zone of the containing page.  This may cause the
  87.         // applet to fail even if it has file permissions.
  88.         out.println("-----------------------------------------------------");
  89.         out.println("Performing checks from 'start':");
  90.         out.println();
  91.         run();
  92.  
  93.         // The HTML page frame is not at the top of the threads created
  94.         // by the applet, so if the applet has file permissions, the
  95.         // access will succeed regardless of the permissions of the
  96.         // containing page.
  97.         out.println();
  98.         out.println("-----------------------------------------------------");
  99.         out.println("Performing checks from a new thread:");
  100.         out.println();
  101.         if (applettrusted)
  102.             untrustedcaller = null;
  103.         else
  104.             untrustedcaller = "applet is not trusted";
  105.         (new Thread(this)).start();
  106.     }
  107.  
  108.     public void run ()
  109.     {
  110.         go(untrustedcaller);
  111.     }
  112.  
  113.     static void dumpData (byte[] data, int len)
  114.     {
  115.         if (len > 50)
  116.             len = 50;
  117.         StringBuffer line = new StringBuffer();
  118.         for (int i = 0; i < len; i++)
  119.         {
  120.             char ch = (char)(data[i] & 0xff);
  121.             if (ch == '\r' || ch == '\n')
  122.                 break;
  123.             if (ch < 32 || ch > 127)
  124.                 ch = '.';
  125.             line.append(ch);
  126.         }
  127.         out.println(line);
  128.     }
  129.  
  130.     public static void main (String[] args)
  131.     {
  132.         out = System.out;
  133.  
  134.         go(null);
  135.     }
  136.  
  137.     static void go (String untrustedcaller)
  138.     {
  139.         tryfile("c:\\autoexec.bat", untrustedcaller);
  140.         tryfile("rnisamp.dll",      untrustedcaller);
  141.     }
  142.  
  143.     static void tryfile (String filename, String untrustedcaller)
  144.     {
  145.         MyRNIObject rniobj;
  146.         try
  147.         {
  148.             rniobj = new MyRNIObject();
  149.         }
  150.         catch (Throwable e)
  151.         {
  152.             out.println("Could not instantiate RNI object MyRNIObject. Is rnisamp.dll on the path?");
  153.             e.printStackTrace();
  154.             return;
  155.         }
  156.         
  157.         loadfile(rniobj, filename, false, untrustedcaller);
  158.         loadfile(rniobj, filename, true,  untrustedcaller);
  159.     }
  160.  
  161.     static void loadfile (MyRNIObject rniobj, String filename, boolean rnichecked, String untrustedcaller)
  162.     {
  163.         boolean isaccessible;
  164.         SecurityManager sm = System.getSecurityManager();
  165.         boolean havesm = sm != null;
  166.         try
  167.         {
  168.             if (sm != null)
  169.                 sm.checkRead(filename);
  170.             isaccessible = true;
  171.         }
  172.         catch (SecurityException e)
  173.         {
  174.             isaccessible = false;
  175.         }
  176.     
  177.         try
  178.         {
  179.             out.println("Loading file \""+filename+"\".");
  180.             if (havesm)
  181.                 out.println("Security check made by "
  182.                         +(rnichecked ? "RNI" : "Java")+"CheckedLoad should "
  183.                         +(isaccessible ? "" : "not ")+"succeed");
  184.             else
  185.                 out.println("No security check should be made.");
  186.             
  187.             byte[] data;
  188.             if (rnichecked)
  189.                 data = rniobj.RNICheckedLoad(filename);
  190.             else
  191.                 data = rniobj.JavaCheckedLoad(filename);
  192.                 
  193.             if (!isaccessible)
  194.                 out.println("Call should not have succeeded because "+untrustedcaller+"!");
  195.             else
  196.                 out.print("Call succeeded.  ");
  197.                 
  198.             out.print("First line:");
  199.             dumpData(data, data.length);
  200.         }
  201.         catch (Throwable e)
  202.         {
  203.             if (e instanceof SecurityException)
  204.             {
  205.                 if (!isaccessible)
  206.                     out.println("Caught security exception as expected - "+untrustedcaller+".");
  207.                 else
  208.                     out.println("Unexpected security exception!");
  209.             }
  210.             else
  211.             {
  212.                 out.println("Load failed: "+e);
  213.             }
  214.             e.printStackTrace();
  215.         }
  216.  
  217.         out.println();
  218.     }
  219. }
  220.  
  221.  
  222. class TextAreaOutputStream extends ByteArrayOutputStream
  223. {
  224.     TextArea ta;
  225.  
  226.  
  227.     TextAreaOutputStream (TextArea i_ta)
  228.     {
  229.         ta = i_ta;
  230.     }
  231.  
  232.  
  233.     public void flush ()
  234.     {
  235.         String s = toString();
  236.         ta.appendText(s);
  237.         System.out.print(s);
  238.         reset();
  239.     }
  240. }
  241.  
  242.