1237 lines · 39.9 KB · Java
RawDownload
                                ("javax.security.auth.x500.X500Principal") &&                        !principalName.equals(PrincipalEntry.WILDCARD_NAME)) {                         // 4702543:  X500 names with an EmailAddress                        // were encoded incorrectly.  construct a new                        // X500Principal with correct encoding.                         X500Principal p = new X500Principal                                ((new X500Principal(principalName)).toString());                        principalName = p.getName();                    }                     principals.add                        (new PrincipalEntry(principalClass, principalName));                } catch (PropertyExpander.ExpandException peee) {                    // ignore the entire policy entry                    // but continue parsing all the info                    // so we can get to the next entry                    if (debug != null) {                        debug.println("principal name expansion failed: " +                                        principalName);                    }                    ignoreEntry = true;                }                peekAndMatch(",");             } else {                throw new ParsingException(st.lineno(),                                  ResourcesMgr.getString(                                      "expected.codeBase.or.SignedBy.or.Principal"));            }        }         if (principals != null) e.principals = principals;        match("{");         while(!peek("}")) {            if (peek("Permission")) {                try {                    PermissionEntry pe = parsePermissionEntry();                    e.add(pe);                } catch (PropertyExpander.ExpandException peee) {                    // ignore. The add never happened                    if (debug != null) {                        debug.println(peee.toString());                    }                    skipEntry();  // BugId 4219343                }                match(";");            } else {                throw new                    ParsingException(st.lineno(),                                     ResourcesMgr.getString(                                        "expected.permission.entry"));            }        }        match("}");         try {            if (e.signedBy != null) e.signedBy = expand(e.signedBy);            if (e.codeBase != null) {                 // For backward compatibility with 1.4                if (e.codeBase.equals(OLD_EXTDIRS_EXPANSION)) {                    e.codeBase = EXTDIRS_EXPANSION;                }                int es;                if ((es=e.codeBase.indexOf(EXTDIRS_EXPANSION)) < 0) {                    e.codeBase = expand(e.codeBase, true).replace                                        (File.separatorChar, '/');                } else {                    // expand the system property "java.ext.dirs",                    // parse it into its path components,                    // and then create a grant entry for each component                    String[] extDirs = parseExtDirs(e.codeBase, es);                    if (extDirs != null && extDirs.length > 0) {                        for (int i = 0; i < extDirs.length; i++) {                            GrantEntry newGe = (GrantEntry)e.clone();                            newGe.codeBase = extDirs[i];                            add(newGe);                             if (debug != null) {                                debug.println("creating policy entry for " +                                        "expanded java.ext.dirs path:\n\t\t" +                                        extDirs[i]);                            }                        }                    }                    ignoreEntry = true;                }            }        } catch (PropertyExpander.ExpandException peee) {            if (debug != null) {                debug.println(peee.toString());            }            return null;        }         return (ignoreEntry == true) ? null : e;    }     /**     * parse a Permission entry     */    private PermissionEntry parsePermissionEntry()        throws ParsingException, IOException, PropertyExpander.ExpandException    {        PermissionEntry e = new PermissionEntry();         // Permission        match("Permission");        e.permission = match("permission type");         if (peek("\"")) {            // Permission name            e.name = expand(match("quoted string"));        }         if (!peek(",")) {            return e;        }        match(",");         if (peek("\"")) {                e.action = expand(match("quoted string"));                if (!peek(",")) {                    return e;                }                match(",");        }         if (peekAndMatch("SignedBy")) {            e.signedBy = expand(match("quoted string"));        }        return e;    }     // package-private: used by PolicyFile for static policy    static String[] parseExtDirs(String codebase, int start) {         String s = System.getProperty(EXTDIRS_PROPERTY);        String globalPrefix = (start > 0 ? codebase.substring(0, start) : "file:");        int end = start + EXTDIRS_EXPANSION.length();        String globalSuffix = (end < codebase.length() ? codebase.substring(end) :            (String) null);         String[] dirs = null;        String localSuffix;        if (s != null) {            StringTokenizer st =                new StringTokenizer(s, File.pathSeparator);            int count = st.countTokens();            dirs = new String[count];            for (int i = 0; i < count; i++) {                File file = new File(st.nextToken());                dirs[i] = sun.net.www.ParseUtil.encodePath                        (file.getAbsolutePath());                 if (!dirs[i].startsWith("/")) {                    dirs[i] = "/" + dirs[i];                }                 localSuffix = (globalSuffix == null ?                    (dirs[i].endsWith("/") ? "*" : "/*") :                    globalSuffix);                 dirs[i] = globalPrefix + dirs[i] + localSuffix;            }        }        return dirs;    }     private boolean peekAndMatch(String expect)        throws ParsingException, IOException    {        if (peek(expect)) {            match(expect);            return true;        } else {            return false;        }    }     private boolean peek(String expect) {        boolean found = false;         switch (lookahead) {         case StreamTokenizer.TT_WORD:            if (expect.equalsIgnoreCase(st.sval))                found = true;            break;        case ',':            if (expect.equalsIgnoreCase(","))                found = true;            break;        case '{':            if (expect.equalsIgnoreCase("{"))                found = true;            break;        case '}':            if (expect.equalsIgnoreCase("}"))                found = true;            break;        case '"':            if (expect.equalsIgnoreCase("\""))                found = true;            break;        case '*':            if (expect.equalsIgnoreCase("*"))                found = true;            break;        default:         }        return found;    }     private String match(String expect)        throws ParsingException, IOException    {        String value = null;         switch (lookahead) {        case StreamTokenizer.TT_NUMBER:            throw new ParsingException(st.lineno(), expect,                                       ResourcesMgr.getString("number.") +                                       String.valueOf(st.nval));        case StreamTokenizer.TT_EOF:            MessageFormat form = new MessageFormat(                    ResourcesMgr.getString                            ("expected.expect.read.end.of.file."));            Object[] source = {expect};            throw new ParsingException(form.format(source));        case StreamTokenizer.TT_WORD:            if (expect.equalsIgnoreCase(st.sval)) {                lookahead = st.nextToken();            } else if (expect.equalsIgnoreCase("permission type")) {                value = st.sval;                lookahead = st.nextToken();            } else if (expect.equalsIgnoreCase("principal type")) {                value = st.sval;                lookahead = st.nextToken();            } else {                 throw new ParsingException(st.lineno(), expect,                                            st.sval);            }            break;        case '"':            if (expect.equalsIgnoreCase("quoted string")) {                value = st.sval;                lookahead = st.nextToken();            } else if (expect.equalsIgnoreCase("permission type")) {                value = st.sval;                lookahead = st.nextToken();            } else if (expect.equalsIgnoreCase("principal type")) {                value = st.sval;                lookahead = st.nextToken();            } else {                throw new ParsingException(st.lineno(), expect, st.sval);            }            break;        case ',':            if (expect.equalsIgnoreCase(","))                lookahead = st.nextToken();            else                throw new ParsingException(st.lineno(), expect, ",");            break;        case '{':            if (expect.equalsIgnoreCase("{"))                lookahead = st.nextToken();            else                throw new ParsingException(st.lineno(), expect, "{");            break;        case '}':            if (expect.equalsIgnoreCase("}"))                lookahead = st.nextToken();            else                throw new ParsingException(st.lineno(), expect, "}");            break;        case ';':            if (expect.equalsIgnoreCase(";"))                lookahead = st.nextToken();            else                throw new ParsingException(st.lineno(), expect, ";");            break;        case '*':            if (expect.equalsIgnoreCase("*"))                lookahead = st.nextToken();            else                throw new ParsingException(st.lineno(), expect, "*");            break;        default:            throw new ParsingException(st.lineno(), expect,                               new String(new char[] {(char)lookahead}));        }        return value;    }     /**     * skip all tokens for this entry leaving the delimiter ";"     * in the stream.     */    private void skipEntry() throws ParsingException, IOException {        while(lookahead != ';') {            switch (lookahead) {            case StreamTokenizer.TT_NUMBER:                throw new ParsingException(st.lineno(), ";",                                          ResourcesMgr.getString("number.") +                                          String.valueOf(st.nval));            case StreamTokenizer.TT_EOF:                throw new ParsingException(ResourcesMgr.getString                        ("expected.read.end.of.file."));            default:                lookahead = st.nextToken();            }        }    }     /**     * Each grant entry in the policy configuration file is     * represented by a     * GrantEntry object.  <p>     *     * <p>     * For example, the entry     * <pre>     *      grant signedBy "Duke" {     *          permission java.io.FilePermission "/tmp", "read,write";     *      };     *     * </pre>     * is represented internally     * <pre>     *     * pe = new PermissionEntry("java.io.FilePermission",     *                           "/tmp", "read,write");     *     * ge = new GrantEntry("Duke", null);     *     * ge.add(pe);     *     * </pre>     *     * @author Roland Schemers     *     * version 1.19, 05/21/98     */     public static class GrantEntry {         public String signedBy;        public String codeBase;        public LinkedList<PrincipalEntry> principals;        public Vector<PermissionEntry> permissionEntries;         public GrantEntry() {            principals = new LinkedList<PrincipalEntry>();            permissionEntries = new Vector<PermissionEntry>();        }         public GrantEntry(String signedBy, String codeBase) {            this.codeBase = codeBase;            this.signedBy = signedBy;            principals = new LinkedList<PrincipalEntry>();            permissionEntries = new Vector<PermissionEntry>();        }         public void add(PermissionEntry pe)        {            permissionEntries.addElement(pe);        }         public boolean remove(PrincipalEntry pe)        {            return principals.remove(pe);        }         public boolean remove(PermissionEntry pe)        {            return permissionEntries.removeElement(pe);        }         public boolean contains(PrincipalEntry pe)        {            return principals.contains(pe);        }         public boolean contains(PermissionEntry pe)        {            return permissionEntries.contains(pe);        }         /**         * Enumerate all the permission entries in this GrantEntry.         */        public Enumeration<PermissionEntry> permissionElements(){            return permissionEntries.elements();        }          public void write(PrintWriter out) {            out.print("grant");            if (signedBy != null) {                out.print(" signedBy \"");                out.print(signedBy);                out.print('"');                if (codeBase != null)                    out.print(", ");            }            if (codeBase != null) {                out.print(" codeBase \"");                out.print(codeBase);                out.print('"');                if (principals != null && principals.size() > 0)                    out.print(",\n");            }            if (principals != null && principals.size() > 0) {                ListIterator<PrincipalEntry> pli = principals.listIterator();                while (pli.hasNext()) {                    out.print("      ");                    PrincipalEntry pe = pli.next();                    pe.write(out);                    if (pli.hasNext())                        out.print(",\n");                }            }            out.println(" {");            Enumeration<PermissionEntry> enum_ = permissionEntries.elements();            while (enum_.hasMoreElements()) {                PermissionEntry pe = enum_.nextElement();                out.write("  ");                pe.write(out);            }            out.println("};");        }         public Object clone() {            GrantEntry ge = new GrantEntry();            ge.codeBase = this.codeBase;            ge.signedBy = this.signedBy;            ge.principals = new LinkedList<PrincipalEntry>(this.principals);            ge.permissionEntries =                        new Vector<PermissionEntry>(this.permissionEntries);            return ge;        }    }     /**     * Principal info (class and name) in a grant entry     */    public static class PrincipalEntry {         public static final String WILDCARD_CLASS = "WILDCARD_PRINCIPAL_CLASS";        public static final String WILDCARD_NAME = "WILDCARD_PRINCIPAL_NAME";         String principalClass;        String principalName;         /**         * A PrincipalEntry consists of the <code>Principal</code>         * class and <code>Principal</code> name.         *         * <p>         *         * @param principalClass the <code>Principal</code> class. <p>         *         * @param principalName the <code>Principal</code> name. <p>         */        public PrincipalEntry(String principalClass, String principalName) {            if (principalClass == null || principalName == null)                throw new NullPointerException(ResourcesMgr.getString(                                  "null.principalClass.or.principalName"));            this.principalClass = principalClass;            this.principalName = principalName;        }         public String getPrincipalClass() {            return principalClass;        }         public String getPrincipalName() {            return principalName;        }         public String getDisplayClass() {            if (principalClass.equals(WILDCARD_CLASS)) {                return "*";            } else if (principalClass.equals(REPLACE_NAME)) {                return "";            }            else return principalClass;        }         public String getDisplayName() {            return getDisplayName(false);        }         public String getDisplayName(boolean addQuote) {            if (principalName.equals(WILDCARD_NAME)) {

Lines 501–1000 of 1237. Previous lines