< prev index next >

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

Print this page
rev 1461 : 6987827: security/util/Resources.java needs improvement
Reviewed-by: valeriep
   1 /*
   2  * Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 202                 GrantEntry ge = parseGrantEntry();
 203                 // could be null if we couldn't expand a property
 204                 if (ge != null)
 205                     add(ge);
 206             } else if (peek("keystore") && keyStoreUrlString==null) {
 207                 // only one keystore entry per policy file, others will be
 208                 // ignored
 209                 parseKeyStoreEntry();
 210             } else if (peek("keystorePasswordURL") && storePassURL==null) {
 211                 // only one keystore passwordURL per policy file, others will be
 212                 // ignored
 213                 parseStorePassURL();
 214             } else {
 215                 // error?
 216             }
 217             match(";");
 218         }
 219 
 220         if (keyStoreUrlString == null && storePassURL != null) {
 221             throw new ParsingException(ResourcesMgr.getString
 222                 ("keystorePasswordURL can not be specified without also " +
 223                 "specifying keystore"));
 224         }
 225     }
 226 
 227     public void add(GrantEntry ge)
 228     {
 229         grantEntries.addElement(ge);
 230     }
 231 
 232     public void replace(GrantEntry origGe, GrantEntry newGe)
 233     {
 234         grantEntries.setElementAt(newGe, grantEntries.indexOf(origGe));
 235     }
 236 
 237     public boolean remove(GrantEntry ge)
 238     {
 239         return grantEntries.removeElement(ge);
 240     }
 241 
 242     /**
 243      * Returns the (possibly expanded) keystore location, or null if the


 340         out.flush();
 341     }
 342 
 343     /**
 344      * parses a keystore entry
 345      */
 346     private void parseKeyStoreEntry() throws ParsingException, IOException {
 347         match("keystore");
 348         keyStoreUrlString = match("quoted string");
 349 
 350         // parse keystore type
 351         if (!peek(",")) {
 352             return; // default type
 353         }
 354         match(",");
 355 
 356         if (peek("\"")) {
 357             keyStoreType = match("quoted string");
 358         } else {
 359             throw new ParsingException(st.lineno(),
 360                         ResourcesMgr.getString("expected keystore type"));
 361         }
 362 
 363         // parse keystore provider
 364         if (!peek(",")) {
 365             return; // provider optional
 366         }
 367         match(",");
 368 
 369         if (peek("\"")) {
 370             keyStoreProvider = match("quoted string");
 371         } else {
 372             throw new ParsingException(st.lineno(),
 373                         ResourcesMgr.getString("expected keystore provider"));
 374         }
 375     }
 376 
 377     private void parseStorePassURL() throws ParsingException, IOException {
 378         match("keyStorePasswordURL");
 379         storePassURL = match("quoted string");
 380     }
 381 
 382     /**
 383      * writes the (unexpanded) keystore entry
 384      */
 385     private void writeKeyStoreEntry(PrintWriter out) {
 386         out.print("keystore \"");
 387         out.print(keyStoreUrlString);
 388         out.print('"');
 389         if (keyStoreType != null && keyStoreType.length() > 0)
 390             out.print(", \"" + keyStoreType + "\"");
 391         if (keyStoreProvider != null && keyStoreProvider.length() > 0)
 392             out.print(", \"" + keyStoreProvider + "\"");
 393         out.println(";");


 404 
 405     /**
 406      * parse a Grant entry
 407      */
 408     private GrantEntry parseGrantEntry()
 409         throws ParsingException, IOException
 410     {
 411         GrantEntry e = new GrantEntry();
 412         LinkedList<PrincipalEntry> principals = null;
 413         boolean ignoreEntry = false;
 414 
 415         match("grant");
 416 
 417         while(!peek("{")) {
 418 
 419             if (peekAndMatch("Codebase")) {
 420                 if (e.codeBase != null)
 421                     throw new ParsingException(
 422                             st.lineno(),
 423                             ResourcesMgr.getString
 424                                 ("multiple Codebase expressions"));
 425                 e.codeBase = match("quoted string");
 426                 peekAndMatch(",");
 427             } else if (peekAndMatch("SignedBy")) {
 428                 if (e.signedBy != null)
 429                     throw new ParsingException(
 430                             st.lineno(),
 431                             ResourcesMgr.getString(
 432                                 "multiple SignedBy expressions"));
 433                 e.signedBy = match("quoted string");
 434 
 435                 // verify syntax of the aliases
 436                 StringTokenizer aliases = new StringTokenizer(e.signedBy,
 437                                                               ",", true);
 438                 int actr = 0;
 439                 int cctr = 0;
 440                 while (aliases.hasMoreTokens()) {
 441                     String alias = aliases.nextToken().trim();
 442                     if (alias.equals(","))
 443                         cctr++;
 444                     else if (alias.length() > 0)
 445                         actr++;
 446                 }
 447                 if (actr <= cctr)
 448                     throw new ParsingException(
 449                             st.lineno(),
 450                             ResourcesMgr.getString(
 451                                 "SignedBy has empty alias"));
 452 
 453                 peekAndMatch(",");
 454             } else if (peekAndMatch("Principal")) {
 455                 if (principals == null) {
 456                     principals = new LinkedList<PrincipalEntry>();
 457                 }
 458 
 459                 String principalClass;
 460                 String principalName;
 461 
 462                 if (peek("\"")) {
 463                     // both the principalClass and principalName
 464                     // will be replaced later
 465                     principalClass = REPLACE_NAME;
 466                     principalName = match("principal type");
 467                 } else {
 468                     // check for principalClass wildcard
 469                     if (peek("*")) {
 470                         match("*");
 471                         principalClass = PrincipalEntry.WILDCARD_CLASS;


 474                     }
 475 
 476                     // check for principalName wildcard
 477                     if (peek("*")) {
 478                         match("*");
 479                         principalName = PrincipalEntry.WILDCARD_NAME;
 480                     } else {
 481                         principalName = match("quoted string");
 482                     }
 483 
 484                     // disallow WILDCARD_CLASS && actual name
 485                     if (principalClass.equals(PrincipalEntry.WILDCARD_CLASS) &&
 486                         !principalName.equals(PrincipalEntry.WILDCARD_NAME)) {
 487                         if (debug != null) {
 488                                 debug.println("disallowing principal that " +
 489                                     "has WILDCARD class but no WILDCARD name");
 490                         }
 491                         throw new ParsingException
 492                                 (st.lineno(),
 493                                  ResourcesMgr.getString
 494                                     ("can not specify Principal with a " +
 495                                      "wildcard class without a wildcard name"));
 496                     }
 497                 }
 498 
 499                 try {
 500                     principalName = expand(principalName);
 501 
 502                     if (principalClass.equals
 503                                 ("javax.security.auth.x500.X500Principal") &&
 504                         !principalName.equals(PrincipalEntry.WILDCARD_NAME)) {
 505 
 506                         // 4702543:  X500 names with an EmailAddress
 507                         // were encoded incorrectly.  construct a new
 508                         // X500Principal with correct encoding.
 509 
 510                         X500Principal p = new X500Principal
 511                                 ((new X500Principal(principalName)).toString());
 512                         principalName = p.getName();
 513                     }
 514 
 515                     principals.add
 516                         (new PrincipalEntry(principalClass, principalName));
 517                 } catch (PropertyExpander.ExpandException peee) {
 518                     // ignore the entire policy entry
 519                     // but continue parsing all the info
 520                     // so we can get to the next entry
 521                     if (debug != null) {
 522                         debug.println("principal name expansion failed: " +
 523                                         principalName);
 524                     }
 525                     ignoreEntry = true;
 526                 }
 527                 peekAndMatch(",");
 528 
 529             } else {
 530                 throw new ParsingException(st.lineno(),
 531                                   ResourcesMgr.getString(
 532                                       "expected codeBase or SignedBy or " +
 533                                       "Principal"));
 534             }
 535         }
 536 
 537         if (principals != null) e.principals = principals;
 538         match("{");
 539 
 540         while(!peek("}")) {
 541             if (peek("Permission")) {
 542                 try {
 543                     PermissionEntry pe = parsePermissionEntry();
 544                     e.add(pe);
 545                 } catch (PropertyExpander.ExpandException peee) {
 546                     // ignore. The add never happened
 547                     if (debug != null) {
 548                         debug.println(peee.toString());
 549                     }
 550                     skipEntry();  // BugId 4219343
 551                 }
 552                 match(";");
 553             } else {
 554                 throw new
 555                     ParsingException(st.lineno(),
 556                                      ResourcesMgr.getString(
 557                                         "expected permission entry"));
 558             }
 559         }
 560         match("}");
 561 
 562         try {
 563             if (e.signedBy != null) e.signedBy = expand(e.signedBy);
 564             if (e.codeBase != null) {
 565 
 566                 // For backward compatibility with 1.4
 567                 if (e.codeBase.equals(OLD_EXTDIRS_EXPANSION)) {
 568                     e.codeBase = EXTDIRS_EXPANSION;
 569                 }
 570                 int es;
 571                 if ((es=e.codeBase.indexOf(EXTDIRS_EXPANSION)) < 0) {
 572                     e.codeBase = expand(e.codeBase, true).replace
 573                                         (File.separatorChar, '/');
 574                 } else {
 575                     // expand the system property "java.ext.dirs",
 576                     // parse it into its path components,
 577                     // and then create a grant entry for each component


 710                 found = true;
 711             break;
 712         case '*':
 713             if (expect.equalsIgnoreCase("*"))
 714                 found = true;
 715             break;
 716         default:
 717 
 718         }
 719         return found;
 720     }
 721 
 722     private String match(String expect)
 723         throws ParsingException, IOException
 724     {
 725         String value = null;
 726 
 727         switch (lookahead) {
 728         case StreamTokenizer.TT_NUMBER:
 729             throw new ParsingException(st.lineno(), expect,
 730                                        ResourcesMgr.getString("number ") +
 731                                        String.valueOf(st.nval));
 732         case StreamTokenizer.TT_EOF:
 733             MessageFormat form = new MessageFormat(
 734                     ResourcesMgr.getString
 735                             ("expected [expect], read [end of file]"));
 736             Object[] source = {expect};
 737             throw new ParsingException(form.format(source));
 738         case StreamTokenizer.TT_WORD:
 739             if (expect.equalsIgnoreCase(st.sval)) {
 740                 lookahead = st.nextToken();
 741             } else if (expect.equalsIgnoreCase("permission type")) {
 742                 value = st.sval;
 743                 lookahead = st.nextToken();
 744             } else if (expect.equalsIgnoreCase("principal type")) {
 745                 value = st.sval;
 746                 lookahead = st.nextToken();
 747             } else {
 748                  throw new ParsingException(st.lineno(), expect,
 749                                             st.sval);
 750             }
 751             break;
 752         case '"':
 753             if (expect.equalsIgnoreCase("quoted string")) {
 754                 value = st.sval;
 755                 lookahead = st.nextToken();


 792                 lookahead = st.nextToken();
 793             else
 794                 throw new ParsingException(st.lineno(), expect, "*");
 795             break;
 796         default:
 797             throw new ParsingException(st.lineno(), expect,
 798                                new String(new char[] {(char)lookahead}));
 799         }
 800         return value;
 801     }
 802 
 803     /**
 804      * skip all tokens for this entry leaving the delimiter ";"
 805      * in the stream.
 806      */
 807     private void skipEntry() throws ParsingException, IOException {
 808         while(lookahead != ';') {
 809             switch (lookahead) {
 810             case StreamTokenizer.TT_NUMBER:
 811                 throw new ParsingException(st.lineno(), ";",
 812                                           ResourcesMgr.getString("number ") +
 813                                           String.valueOf(st.nval));
 814             case StreamTokenizer.TT_EOF:
 815                 throw new ParsingException(ResourcesMgr.getString
 816                         ("expected [;], read [end of file]"));
 817             default:
 818                 lookahead = st.nextToken();
 819             }
 820         }
 821     }
 822 
 823     /**
 824      * Each grant entry in the policy configuration file is
 825      * represented by a
 826      * GrantEntry object.  <p>
 827      *
 828      * <p>
 829      * For example, the entry
 830      * <pre>
 831      *      grant signedBy "Duke" {
 832      *          permission java.io.FilePermission "/tmp", "read,write";
 833      *      };
 834      *
 835      * </pre>
 836      * is represented internally


 956 
 957         public static final String WILDCARD_CLASS = "WILDCARD_PRINCIPAL_CLASS";
 958         public static final String WILDCARD_NAME = "WILDCARD_PRINCIPAL_NAME";
 959 
 960         String principalClass;
 961         String principalName;
 962 
 963         /**
 964          * A PrincipalEntry consists of the <code>Principal</code>
 965          * class and <code>Principal</code> name.
 966          *
 967          * <p>
 968          *
 969          * @param principalClass the <code>Principal</code> class. <p>
 970          *
 971          * @param principalName the <code>Principal</code> name. <p>
 972          */
 973         public PrincipalEntry(String principalClass, String principalName) {
 974             if (principalClass == null || principalName == null)
 975                 throw new NullPointerException(ResourcesMgr.getString(
 976                                   "null principalClass or principalName"));
 977             this.principalClass = principalClass;
 978             this.principalName = principalName;
 979         }
 980 
 981         public String getPrincipalClass() {
 982             return principalClass;
 983         }
 984 
 985         public String getPrincipalName() {
 986             return principalName;
 987         }
 988 
 989         public String getDisplayClass() {
 990             if (principalClass.equals(WILDCARD_CLASS)) {
 991                 return "*";
 992             } else if (principalClass.equals(REPLACE_NAME)) {
 993                 return "";
 994             }
 995             else return principalClass;
 996         }


1182         private static final long serialVersionUID = -4330692689482574072L;
1183 
1184         private String i18nMessage;
1185 
1186         /**
1187          * Constructs a ParsingException with the specified
1188          * detail message. A detail message is a String that describes
1189          * this particular exception, which may, for example, specify which
1190          * algorithm is not available.
1191          *
1192          * @param msg the detail message.
1193          */
1194         public ParsingException(String msg) {
1195             super(msg);
1196             i18nMessage = msg;
1197         }
1198 
1199         public ParsingException(int line, String msg) {
1200             super("line " + line + ": " + msg);
1201             MessageFormat form = new MessageFormat
1202                 (ResourcesMgr.getString("line number: msg"));
1203             Object[] source = {new Integer(line), msg};
1204             i18nMessage = form.format(source);
1205         }
1206 
1207         public ParsingException(int line, String expect, String actual) {
1208             super("line " + line + ": expected [" + expect +
1209                 "], found [" + actual + "]");
1210             MessageFormat form = new MessageFormat(ResourcesMgr.getString
1211                 ("line number: expected [expect], found [actual]"));
1212             Object[] source = {new Integer(line), expect, actual};
1213             i18nMessage = form.format(source);
1214         }
1215 
1216         public String getLocalizedMessage() {
1217             return i18nMessage;
1218         }
1219     }
1220 
1221     public static void main(String arg[]) throws Exception {
1222         FileReader fr = null;
1223         FileWriter fw = null;
1224         try {
1225             PolicyParser pp = new PolicyParser(true);
1226             fr = new FileReader(arg[0]);
1227             pp.read(fr);
1228             fw = new FileWriter(arg[1]);
1229             pp.write(fw);
1230         } finally {
1231             if (fr != null) {
   1 /*
   2  * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 202                 GrantEntry ge = parseGrantEntry();
 203                 // could be null if we couldn't expand a property
 204                 if (ge != null)
 205                     add(ge);
 206             } else if (peek("keystore") && keyStoreUrlString==null) {
 207                 // only one keystore entry per policy file, others will be
 208                 // ignored
 209                 parseKeyStoreEntry();
 210             } else if (peek("keystorePasswordURL") && storePassURL==null) {
 211                 // only one keystore passwordURL per policy file, others will be
 212                 // ignored
 213                 parseStorePassURL();
 214             } else {
 215                 // error?
 216             }
 217             match(";");
 218         }
 219 
 220         if (keyStoreUrlString == null && storePassURL != null) {
 221             throw new ParsingException(ResourcesMgr.getString
 222                 ("keystorePasswordURL.can.not.be.specified.without.also.specifying.keystore"));

 223         }
 224     }
 225 
 226     public void add(GrantEntry ge)
 227     {
 228         grantEntries.addElement(ge);
 229     }
 230 
 231     public void replace(GrantEntry origGe, GrantEntry newGe)
 232     {
 233         grantEntries.setElementAt(newGe, grantEntries.indexOf(origGe));
 234     }
 235 
 236     public boolean remove(GrantEntry ge)
 237     {
 238         return grantEntries.removeElement(ge);
 239     }
 240 
 241     /**
 242      * Returns the (possibly expanded) keystore location, or null if the


 339         out.flush();
 340     }
 341 
 342     /**
 343      * parses a keystore entry
 344      */
 345     private void parseKeyStoreEntry() throws ParsingException, IOException {
 346         match("keystore");
 347         keyStoreUrlString = match("quoted string");
 348 
 349         // parse keystore type
 350         if (!peek(",")) {
 351             return; // default type
 352         }
 353         match(",");
 354 
 355         if (peek("\"")) {
 356             keyStoreType = match("quoted string");
 357         } else {
 358             throw new ParsingException(st.lineno(),
 359                         ResourcesMgr.getString("expected.keystore.type"));
 360         }
 361 
 362         // parse keystore provider
 363         if (!peek(",")) {
 364             return; // provider optional
 365         }
 366         match(",");
 367 
 368         if (peek("\"")) {
 369             keyStoreProvider = match("quoted string");
 370         } else {
 371             throw new ParsingException(st.lineno(),
 372                         ResourcesMgr.getString("expected.keystore.provider"));
 373         }
 374     }
 375 
 376     private void parseStorePassURL() throws ParsingException, IOException {
 377         match("keyStorePasswordURL");
 378         storePassURL = match("quoted string");
 379     }
 380 
 381     /**
 382      * writes the (unexpanded) keystore entry
 383      */
 384     private void writeKeyStoreEntry(PrintWriter out) {
 385         out.print("keystore \"");
 386         out.print(keyStoreUrlString);
 387         out.print('"');
 388         if (keyStoreType != null && keyStoreType.length() > 0)
 389             out.print(", \"" + keyStoreType + "\"");
 390         if (keyStoreProvider != null && keyStoreProvider.length() > 0)
 391             out.print(", \"" + keyStoreProvider + "\"");
 392         out.println(";");


 403 
 404     /**
 405      * parse a Grant entry
 406      */
 407     private GrantEntry parseGrantEntry()
 408         throws ParsingException, IOException
 409     {
 410         GrantEntry e = new GrantEntry();
 411         LinkedList<PrincipalEntry> principals = null;
 412         boolean ignoreEntry = false;
 413 
 414         match("grant");
 415 
 416         while(!peek("{")) {
 417 
 418             if (peekAndMatch("Codebase")) {
 419                 if (e.codeBase != null)
 420                     throw new ParsingException(
 421                             st.lineno(),
 422                             ResourcesMgr.getString
 423                                 ("multiple.Codebase.expressions"));
 424                 e.codeBase = match("quoted string");
 425                 peekAndMatch(",");
 426             } else if (peekAndMatch("SignedBy")) {
 427                 if (e.signedBy != null)
 428                     throw new ParsingException(
 429                             st.lineno(),
 430                             ResourcesMgr.getString(
 431                                 "multiple.SignedBy.expressions"));
 432                 e.signedBy = match("quoted string");
 433 
 434                 // verify syntax of the aliases
 435                 StringTokenizer aliases = new StringTokenizer(e.signedBy,
 436                                                               ",", true);
 437                 int actr = 0;
 438                 int cctr = 0;
 439                 while (aliases.hasMoreTokens()) {
 440                     String alias = aliases.nextToken().trim();
 441                     if (alias.equals(","))
 442                         cctr++;
 443                     else if (alias.length() > 0)
 444                         actr++;
 445                 }
 446                 if (actr <= cctr)
 447                     throw new ParsingException(
 448                             st.lineno(),
 449                             ResourcesMgr.getString(
 450                                 "SignedBy.has.empty.alias"));
 451 
 452                 peekAndMatch(",");
 453             } else if (peekAndMatch("Principal")) {
 454                 if (principals == null) {
 455                     principals = new LinkedList<PrincipalEntry>();
 456                 }
 457 
 458                 String principalClass;
 459                 String principalName;
 460 
 461                 if (peek("\"")) {
 462                     // both the principalClass and principalName
 463                     // will be replaced later
 464                     principalClass = REPLACE_NAME;
 465                     principalName = match("principal type");
 466                 } else {
 467                     // check for principalClass wildcard
 468                     if (peek("*")) {
 469                         match("*");
 470                         principalClass = PrincipalEntry.WILDCARD_CLASS;


 473                     }
 474 
 475                     // check for principalName wildcard
 476                     if (peek("*")) {
 477                         match("*");
 478                         principalName = PrincipalEntry.WILDCARD_NAME;
 479                     } else {
 480                         principalName = match("quoted string");
 481                     }
 482 
 483                     // disallow WILDCARD_CLASS && actual name
 484                     if (principalClass.equals(PrincipalEntry.WILDCARD_CLASS) &&
 485                         !principalName.equals(PrincipalEntry.WILDCARD_NAME)) {
 486                         if (debug != null) {
 487                                 debug.println("disallowing principal that " +
 488                                     "has WILDCARD class but no WILDCARD name");
 489                         }
 490                         throw new ParsingException
 491                                 (st.lineno(),
 492                                  ResourcesMgr.getString
 493                                     ("can.not.specify.Principal.with.a.wildcard.class.without.a.wildcard.name"));

 494                     }
 495                 }
 496 
 497                 try {
 498                     principalName = expand(principalName);
 499 
 500                     if (principalClass.equals
 501                                 ("javax.security.auth.x500.X500Principal") &&
 502                         !principalName.equals(PrincipalEntry.WILDCARD_NAME)) {
 503 
 504                         // 4702543:  X500 names with an EmailAddress
 505                         // were encoded incorrectly.  construct a new
 506                         // X500Principal with correct encoding.
 507 
 508                         X500Principal p = new X500Principal
 509                                 ((new X500Principal(principalName)).toString());
 510                         principalName = p.getName();
 511                     }
 512 
 513                     principals.add
 514                         (new PrincipalEntry(principalClass, principalName));
 515                 } catch (PropertyExpander.ExpandException peee) {
 516                     // ignore the entire policy entry
 517                     // but continue parsing all the info
 518                     // so we can get to the next entry
 519                     if (debug != null) {
 520                         debug.println("principal name expansion failed: " +
 521                                         principalName);
 522                     }
 523                     ignoreEntry = true;
 524                 }
 525                 peekAndMatch(",");
 526 
 527             } else {
 528                 throw new ParsingException(st.lineno(),
 529                                   ResourcesMgr.getString(
 530                                       "expected.codeBase.or.SignedBy.or.Principal"));

 531             }
 532         }
 533 
 534         if (principals != null) e.principals = principals;
 535         match("{");
 536 
 537         while(!peek("}")) {
 538             if (peek("Permission")) {
 539                 try {
 540                     PermissionEntry pe = parsePermissionEntry();
 541                     e.add(pe);
 542                 } catch (PropertyExpander.ExpandException peee) {
 543                     // ignore. The add never happened
 544                     if (debug != null) {
 545                         debug.println(peee.toString());
 546                     }
 547                     skipEntry();  // BugId 4219343
 548                 }
 549                 match(";");
 550             } else {
 551                 throw new
 552                     ParsingException(st.lineno(),
 553                                      ResourcesMgr.getString(
 554                                         "expected.permission.entry"));
 555             }
 556         }
 557         match("}");
 558 
 559         try {
 560             if (e.signedBy != null) e.signedBy = expand(e.signedBy);
 561             if (e.codeBase != null) {
 562 
 563                 // For backward compatibility with 1.4
 564                 if (e.codeBase.equals(OLD_EXTDIRS_EXPANSION)) {
 565                     e.codeBase = EXTDIRS_EXPANSION;
 566                 }
 567                 int es;
 568                 if ((es=e.codeBase.indexOf(EXTDIRS_EXPANSION)) < 0) {
 569                     e.codeBase = expand(e.codeBase, true).replace
 570                                         (File.separatorChar, '/');
 571                 } else {
 572                     // expand the system property "java.ext.dirs",
 573                     // parse it into its path components,
 574                     // and then create a grant entry for each component


 707                 found = true;
 708             break;
 709         case '*':
 710             if (expect.equalsIgnoreCase("*"))
 711                 found = true;
 712             break;
 713         default:
 714 
 715         }
 716         return found;
 717     }
 718 
 719     private String match(String expect)
 720         throws ParsingException, IOException
 721     {
 722         String value = null;
 723 
 724         switch (lookahead) {
 725         case StreamTokenizer.TT_NUMBER:
 726             throw new ParsingException(st.lineno(), expect,
 727                                        ResourcesMgr.getString("number.") +
 728                                        String.valueOf(st.nval));
 729         case StreamTokenizer.TT_EOF:
 730             MessageFormat form = new MessageFormat(
 731                     ResourcesMgr.getString
 732                             ("expected.expect.read.end.of.file."));
 733             Object[] source = {expect};
 734             throw new ParsingException(form.format(source));
 735         case StreamTokenizer.TT_WORD:
 736             if (expect.equalsIgnoreCase(st.sval)) {
 737                 lookahead = st.nextToken();
 738             } else if (expect.equalsIgnoreCase("permission type")) {
 739                 value = st.sval;
 740                 lookahead = st.nextToken();
 741             } else if (expect.equalsIgnoreCase("principal type")) {
 742                 value = st.sval;
 743                 lookahead = st.nextToken();
 744             } else {
 745                  throw new ParsingException(st.lineno(), expect,
 746                                             st.sval);
 747             }
 748             break;
 749         case '"':
 750             if (expect.equalsIgnoreCase("quoted string")) {
 751                 value = st.sval;
 752                 lookahead = st.nextToken();


 789                 lookahead = st.nextToken();
 790             else
 791                 throw new ParsingException(st.lineno(), expect, "*");
 792             break;
 793         default:
 794             throw new ParsingException(st.lineno(), expect,
 795                                new String(new char[] {(char)lookahead}));
 796         }
 797         return value;
 798     }
 799 
 800     /**
 801      * skip all tokens for this entry leaving the delimiter ";"
 802      * in the stream.
 803      */
 804     private void skipEntry() throws ParsingException, IOException {
 805         while(lookahead != ';') {
 806             switch (lookahead) {
 807             case StreamTokenizer.TT_NUMBER:
 808                 throw new ParsingException(st.lineno(), ";",
 809                                           ResourcesMgr.getString("number.") +
 810                                           String.valueOf(st.nval));
 811             case StreamTokenizer.TT_EOF:
 812                 throw new ParsingException(ResourcesMgr.getString
 813                         ("expected.read.end.of.file."));
 814             default:
 815                 lookahead = st.nextToken();
 816             }
 817         }
 818     }
 819 
 820     /**
 821      * Each grant entry in the policy configuration file is
 822      * represented by a
 823      * GrantEntry object.  <p>
 824      *
 825      * <p>
 826      * For example, the entry
 827      * <pre>
 828      *      grant signedBy "Duke" {
 829      *          permission java.io.FilePermission "/tmp", "read,write";
 830      *      };
 831      *
 832      * </pre>
 833      * is represented internally


 953 
 954         public static final String WILDCARD_CLASS = "WILDCARD_PRINCIPAL_CLASS";
 955         public static final String WILDCARD_NAME = "WILDCARD_PRINCIPAL_NAME";
 956 
 957         String principalClass;
 958         String principalName;
 959 
 960         /**
 961          * A PrincipalEntry consists of the <code>Principal</code>
 962          * class and <code>Principal</code> name.
 963          *
 964          * <p>
 965          *
 966          * @param principalClass the <code>Principal</code> class. <p>
 967          *
 968          * @param principalName the <code>Principal</code> name. <p>
 969          */
 970         public PrincipalEntry(String principalClass, String principalName) {
 971             if (principalClass == null || principalName == null)
 972                 throw new NullPointerException(ResourcesMgr.getString(
 973                                   "null.principalClass.or.principalName"));
 974             this.principalClass = principalClass;
 975             this.principalName = principalName;
 976         }
 977 
 978         public String getPrincipalClass() {
 979             return principalClass;
 980         }
 981 
 982         public String getPrincipalName() {
 983             return principalName;
 984         }
 985 
 986         public String getDisplayClass() {
 987             if (principalClass.equals(WILDCARD_CLASS)) {
 988                 return "*";
 989             } else if (principalClass.equals(REPLACE_NAME)) {
 990                 return "";
 991             }
 992             else return principalClass;
 993         }


1179         private static final long serialVersionUID = -4330692689482574072L;
1180 
1181         private String i18nMessage;
1182 
1183         /**
1184          * Constructs a ParsingException with the specified
1185          * detail message. A detail message is a String that describes
1186          * this particular exception, which may, for example, specify which
1187          * algorithm is not available.
1188          *
1189          * @param msg the detail message.
1190          */
1191         public ParsingException(String msg) {
1192             super(msg);
1193             i18nMessage = msg;
1194         }
1195 
1196         public ParsingException(int line, String msg) {
1197             super("line " + line + ": " + msg);
1198             MessageFormat form = new MessageFormat
1199                 (ResourcesMgr.getString("line.number.msg"));
1200             Object[] source = {new Integer(line), msg};
1201             i18nMessage = form.format(source);
1202         }
1203 
1204         public ParsingException(int line, String expect, String actual) {
1205             super("line " + line + ": expected [" + expect +
1206                 "], found [" + actual + "]");
1207             MessageFormat form = new MessageFormat(ResourcesMgr.getString
1208                 ("line.number.expected.expect.found.actual."));
1209             Object[] source = {new Integer(line), expect, actual};
1210             i18nMessage = form.format(source);
1211         }
1212 
1213         public String getLocalizedMessage() {
1214             return i18nMessage;
1215         }
1216     }
1217 
1218     public static void main(String arg[]) throws Exception {
1219         FileReader fr = null;
1220         FileWriter fw = null;
1221         try {
1222             PolicyParser pp = new PolicyParser(true);
1223             fr = new FileReader(arg[0]);
1224             pp.read(fr);
1225             fw = new FileWriter(arg[1]);
1226             pp.write(fw);
1227         } finally {
1228             if (fr != null) {
< prev index next >