src/java.base/share/classes/sun/security/provider/PolicyParser.java

Print this page




  66  * <pre>
  67  *  policy = Policy.getPolicy();
  68  *  Permissions perms = policy.getPermissions(protectiondomain)
  69  * </pre>
  70  *
  71  * <p>The protection domain contains a CodeSource
  72  * object, which encapsulates its codebase (URL) and public key attributes.
  73  * It also contains the principals associated with the domain.
  74  * The Policy object evaluates the global policy in light of who the
  75  * principal is and what the code source is and returns an appropriate
  76  * Permissions object.
  77  *
  78  * @author Roland Schemers
  79  * @author Ram Marti
  80  *
  81  * @since 1.2
  82  */
  83 
  84 public class PolicyParser {
  85 
  86     private static final String EXTDIRS_PROPERTY = "java.ext.dirs";
  87     private static final String OLD_EXTDIRS_EXPANSION =
  88         "${" + EXTDIRS_PROPERTY + "}";
  89 
  90     // package-private: used by PolicyFile for static policy
  91     static final String EXTDIRS_EXPANSION = "${{" + EXTDIRS_PROPERTY + "}}";
  92 
  93 
  94     private Vector<GrantEntry> grantEntries;
  95     private Map<String, DomainEntry> domainEntries;
  96 
  97     // Convenience variables for parsing
  98     private static final Debug debug = Debug.getInstance("parser",
  99                                                 "\t[Policy Parser]");
 100     private StreamTokenizer st;
 101     private int lookahead;
 102     private boolean expandProp = false;
 103     private String keyStoreUrlString = null; // unexpanded
 104     private String keyStoreType = null;
 105     private String keyStoreProvider = null;
 106     private String storePassURL = null;
 107 
 108     private String expand(String value)
 109         throws PropertyExpander.ExpandException
 110     {
 111         return expand(value, false);
 112     }
 113 


 559                 } catch (PropertyExpander.ExpandException peee) {
 560                     // ignore. The add never happened
 561                     if (debug != null) {
 562                         debug.println(peee.toString());
 563                     }
 564                     skipEntry();  // BugId 4219343
 565                 }
 566                 match(";");
 567             } else {
 568                 throw new
 569                     ParsingException(st.lineno(),
 570                                      ResourcesMgr.getString(
 571                                         "expected.permission.entry"));
 572             }
 573         }
 574         match("}");
 575 
 576         try {
 577             if (e.signedBy != null) e.signedBy = expand(e.signedBy);
 578             if (e.codeBase != null) {
 579 
 580                 // For backward compatibility with 1.4
 581                 if (e.codeBase.equals(OLD_EXTDIRS_EXPANSION)) {
 582                     e.codeBase = EXTDIRS_EXPANSION;
 583                 }
 584                 int es;
 585                 if ((es=e.codeBase.indexOf(EXTDIRS_EXPANSION)) < 0) {
 586                     e.codeBase = expand(e.codeBase, true).replace
 587                                         (File.separatorChar, '/');
 588                 } else {
 589                     // expand the system property "java.ext.dirs",
 590                     // parse it into its path components,
 591                     // and then create a grant entry for each component
 592                     String[] extDirs = parseExtDirs(e.codeBase, es);
 593                     if (extDirs != null && extDirs.length > 0) {
 594                         for (int i = 0; i < extDirs.length; i++) {
 595                             GrantEntry newGe = (GrantEntry)e.clone();
 596                             newGe.codeBase = extDirs[i];
 597                             add(newGe);
 598 
 599                             if (debug != null) {
 600                                 debug.println("creating policy entry for " +
 601                                         "expanded java.ext.dirs path:\n\t\t" +
 602                                         extDirs[i]);
 603                             }
 604                         }
 605                     }
 606                     ignoreEntry = true;
 607                 }
 608             }
 609         } catch (PropertyExpander.ExpandException peee) {
 610             if (debug != null) {
 611                 debug.println(peee.toString());
 612             }
 613             return null;
 614         }
 615 
 616         return (ignoreEntry == true) ? null : e;
 617     }
 618 
 619     /**
 620      * parse a Permission entry
 621      */
 622     private PermissionEntry parsePermissionEntry()
 623         throws ParsingException, IOException, PropertyExpander.ExpandException
 624     {
 625         PermissionEntry e = new PermissionEntry();
 626 
 627         // Permission


 696         throws ParsingException, IOException {
 697 
 698         Map<String, String> properties = new HashMap<>();
 699         String key;
 700         String value;
 701         while (!peek(terminator)) {
 702             key = match("property name");
 703             match("=");
 704 
 705             try {
 706                 value = expand(match("quoted string"));
 707             } catch (PropertyExpander.ExpandException peee) {
 708                 throw new IOException(peee.getLocalizedMessage());
 709             }
 710             properties.put(key.toLowerCase(Locale.ENGLISH), value);
 711         }
 712 
 713         return properties;
 714     }
 715 
 716     // package-private: used by PolicyFile for static policy
 717     static String[] parseExtDirs(String codebase, int start) {
 718 
 719         String s = System.getProperty(EXTDIRS_PROPERTY);
 720         String globalPrefix = (start > 0 ? codebase.substring(0, start) : "file:");
 721         int end = start + EXTDIRS_EXPANSION.length();
 722         String globalSuffix = (end < codebase.length() ? codebase.substring(end) :
 723             (String) null);
 724 
 725         String[] dirs = null;
 726         String localSuffix;
 727         if (s != null) {
 728             StringTokenizer st =
 729                 new StringTokenizer(s, File.pathSeparator);
 730             int count = st.countTokens();
 731             dirs = new String[count];
 732             for (int i = 0; i < count; i++) {
 733                 File file = new File(st.nextToken());
 734                 dirs[i] = sun.net.www.ParseUtil.encodePath
 735                         (file.getAbsolutePath());
 736 
 737                 if (!dirs[i].startsWith("/")) {
 738                     dirs[i] = "/" + dirs[i];
 739                 }
 740 
 741                 localSuffix = (globalSuffix == null ?
 742                     (dirs[i].endsWith("/") ? "*" : "/*") :
 743                     globalSuffix);
 744 
 745                 dirs[i] = globalPrefix + dirs[i] + localSuffix;
 746             }
 747         }
 748         return dirs;
 749     }
 750 
 751     private boolean peekAndMatch(String expect)
 752         throws ParsingException, IOException
 753     {
 754         if (peek(expect)) {
 755             match(expect);
 756             return true;
 757         } else {
 758             return false;
 759         }
 760     }
 761 
 762     private boolean peek(String expect) {
 763         boolean found = false;
 764 
 765         switch (lookahead) {
 766 
 767         case StreamTokenizer.TT_WORD:
 768             if (expect.equalsIgnoreCase(st.sval))
 769                 found = true;
 770             break;




  66  * <pre>
  67  *  policy = Policy.getPolicy();
  68  *  Permissions perms = policy.getPermissions(protectiondomain)
  69  * </pre>
  70  *
  71  * <p>The protection domain contains a CodeSource
  72  * object, which encapsulates its codebase (URL) and public key attributes.
  73  * It also contains the principals associated with the domain.
  74  * The Policy object evaluates the global policy in light of who the
  75  * principal is and what the code source is and returns an appropriate
  76  * Permissions object.
  77  *
  78  * @author Roland Schemers
  79  * @author Ram Marti
  80  *
  81  * @since 1.2
  82  */
  83 
  84 public class PolicyParser {
  85 








  86     private Vector<GrantEntry> grantEntries;
  87     private Map<String, DomainEntry> domainEntries;
  88 
  89     // Convenience variables for parsing
  90     private static final Debug debug = Debug.getInstance("parser",
  91                                                 "\t[Policy Parser]");
  92     private StreamTokenizer st;
  93     private int lookahead;
  94     private boolean expandProp = false;
  95     private String keyStoreUrlString = null; // unexpanded
  96     private String keyStoreType = null;
  97     private String keyStoreProvider = null;
  98     private String storePassURL = null;
  99 
 100     private String expand(String value)
 101         throws PropertyExpander.ExpandException
 102     {
 103         return expand(value, false);
 104     }
 105 


 551                 } catch (PropertyExpander.ExpandException peee) {
 552                     // ignore. The add never happened
 553                     if (debug != null) {
 554                         debug.println(peee.toString());
 555                     }
 556                     skipEntry();  // BugId 4219343
 557                 }
 558                 match(";");
 559             } else {
 560                 throw new
 561                     ParsingException(st.lineno(),
 562                                      ResourcesMgr.getString(
 563                                         "expected.permission.entry"));
 564             }
 565         }
 566         match("}");
 567 
 568         try {
 569             if (e.signedBy != null) e.signedBy = expand(e.signedBy);
 570             if (e.codeBase != null) {







 571                 e.codeBase = expand(e.codeBase, true).replace
 572                                     (File.separatorChar, '/');




















 573             }
 574         } catch (PropertyExpander.ExpandException peee) {
 575             if (debug != null) {
 576                 debug.println(peee.toString());
 577             }
 578             return null;
 579         }
 580 
 581         return (ignoreEntry == true) ? null : e;
 582     }
 583 
 584     /**
 585      * parse a Permission entry
 586      */
 587     private PermissionEntry parsePermissionEntry()
 588         throws ParsingException, IOException, PropertyExpander.ExpandException
 589     {
 590         PermissionEntry e = new PermissionEntry();
 591 
 592         // Permission


 661         throws ParsingException, IOException {
 662 
 663         Map<String, String> properties = new HashMap<>();
 664         String key;
 665         String value;
 666         while (!peek(terminator)) {
 667             key = match("property name");
 668             match("=");
 669 
 670             try {
 671                 value = expand(match("quoted string"));
 672             } catch (PropertyExpander.ExpandException peee) {
 673                 throw new IOException(peee.getLocalizedMessage());
 674             }
 675             properties.put(key.toLowerCase(Locale.ENGLISH), value);
 676         }
 677 
 678         return properties;
 679     }
 680 



































 681     private boolean peekAndMatch(String expect)
 682         throws ParsingException, IOException
 683     {
 684         if (peek(expect)) {
 685             match(expect);
 686             return true;
 687         } else {
 688             return false;
 689         }
 690     }
 691 
 692     private boolean peek(String expect) {
 693         boolean found = false;
 694 
 695         switch (lookahead) {
 696 
 697         case StreamTokenizer.TT_WORD:
 698             if (expect.equalsIgnoreCase(st.sval))
 699                 found = true;
 700             break;