home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 39 / IOPROG_39.ISO / SOFT / sdkjava40.exe / data1.cab / fg_Samples / Samples / COM / security_com / Main.java < prev    next >
Encoding:
Java Source  |  2000-05-04  |  7.1 KB  |  254 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 comsamp.*;
  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 > 60)
  116.             len = 60;
  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("Main.java",      untrustedcaller);
  141.     }
  142.  
  143.     static void tryfile (String filename, String untrustedcaller)
  144.     {
  145.         CMyCOMObject comobj;
  146.         try
  147.         {
  148.             comobj = new CMyCOMObject();
  149.         }
  150.         catch (Throwable e)
  151.         {
  152.             out.println("Could not instantiate COM object CMyCOMObject. Is comsamp.dll registered?");
  153.             e.printStackTrace(out);
  154.             return;
  155.         }
  156.         
  157.         loadfile(comobj, filename, false, untrustedcaller);
  158.         loadfile(comobj, filename, true,  untrustedcaller);
  159.     }
  160.  
  161.     static final int E_PRIVILEGE_NOT_HELD = (int)0x80070522;
  162.  
  163.     static void loadfile (CMyCOMObject comobj, String filename, boolean comchecked, String untrustedcaller)
  164.     {
  165.         boolean isaccessible;
  166.         SecurityManager sm = System.getSecurityManager();
  167.         boolean havesm = sm != null;
  168.         try
  169.         {
  170.             if (sm != null)
  171.                 sm.checkRead(filename);
  172.             isaccessible = true;
  173.         }
  174.         catch (SecurityException e)
  175.         {
  176.             isaccessible = false;
  177.         }
  178.     
  179.         try
  180.         {
  181.             out.println("Loading file \""+filename+"\".");
  182.             if (havesm)
  183.                 out.println("Security check made by "
  184.                         +(comchecked ? "COM" : "Java")+"CheckedLoad should "
  185.                         +(isaccessible ? "" : "not ")+"succeed");
  186.             else
  187.                 out.println("No security check should be made.");
  188.             
  189.             byte[] data = new byte[1024];
  190.             int[] len = new int[1];
  191.             len[0] = data.length;
  192.             if (comchecked)
  193.                 comobj.COMCheckedLoad(filename, data, len);
  194.             else
  195.                 comobj.JavaCheckedLoad(filename, data, len);
  196.                 
  197.             if (!isaccessible)
  198.                 out.println("Call should not have succeeded because "+untrustedcaller+"!");
  199.             else
  200.                 out.print("Call succeeded.  ");
  201.                 
  202.             out.print("First line:");
  203.             dumpData(data, len[0]);
  204.         }
  205.         catch (Throwable e)
  206.         {
  207.             if (e instanceof SecurityException)
  208.             {
  209.                 if (!comchecked && !isaccessible)
  210.                     out.println("Caught Java security exception as expected - "+untrustedcaller+".");
  211.                 else
  212.                     out.println("Unexpected security exception!");
  213.             }
  214.             else if (e instanceof ComFailException)
  215.             {
  216.                 int hr = ((ComFailException)e).getHResult();
  217.                 if (comchecked && hr == E_PRIVILEGE_NOT_HELD && !isaccessible)
  218.                     out.println("Caught COM security exception as expected - "+untrustedcaller+".");
  219.                 else
  220.                     out.println("Unexpected security exception!");
  221.             }
  222.             else
  223.             {
  224.                 out.println("Load failed: "+e);
  225.             }
  226.             e.printStackTrace();
  227.         }
  228.  
  229.         out.println();
  230.     }
  231. }
  232.  
  233.  
  234. class TextAreaOutputStream extends ByteArrayOutputStream
  235. {
  236.     TextArea ta;
  237.  
  238.  
  239.     TextAreaOutputStream (TextArea i_ta)
  240.     {
  241.         ta = i_ta;
  242.     }
  243.  
  244.  
  245.     public void flush ()
  246.     {
  247.         String s = toString();
  248.         ta.appendText(s);
  249.         System.out.print(s);
  250.         reset();
  251.     }
  252. }
  253.  
  254.