2
|
1 |
/*
|
|
2 |
* Copyright 1997-2006 Sun Microsystems, Inc. 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. Sun designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
23 |
* have any questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
package sun.security.provider;
|
|
27 |
|
|
28 |
import java.io.*;
|
|
29 |
import java.lang.RuntimePermission;
|
|
30 |
import java.net.SocketPermission;
|
|
31 |
import java.net.URL;
|
|
32 |
import java.util.Enumeration;
|
|
33 |
import java.util.Hashtable;
|
|
34 |
import java.util.LinkedList;
|
|
35 |
import java.util.ListIterator;
|
|
36 |
import java.util.Vector;
|
|
37 |
import java.util.StringTokenizer;
|
|
38 |
import java.text.MessageFormat;
|
|
39 |
import javax.security.auth.x500.X500Principal;
|
|
40 |
|
|
41 |
import java.security.GeneralSecurityException;
|
|
42 |
import sun.security.util.Debug;
|
|
43 |
import sun.security.util.PropertyExpander;
|
|
44 |
import sun.security.util.ResourcesMgr;
|
|
45 |
|
|
46 |
/**
|
|
47 |
* The policy for a Java runtime (specifying
|
|
48 |
* which permissions are available for code from various principals)
|
|
49 |
* is represented as a separate
|
|
50 |
* persistent configuration. The configuration may be stored as a
|
|
51 |
* flat ASCII file, as a serialized binary file of
|
|
52 |
* the Policy class, or as a database. <p>
|
|
53 |
*
|
|
54 |
* <p>The Java runtime creates one global Policy object, which is used to
|
|
55 |
* represent the static policy configuration file. It is consulted by
|
|
56 |
* a ProtectionDomain when the protection domain initializes its set of
|
|
57 |
* permissions. <p>
|
|
58 |
*
|
|
59 |
* <p>The Policy <code>init</code> method parses the policy
|
|
60 |
* configuration file, and then
|
|
61 |
* populates the Policy object. The Policy object is agnostic in that
|
|
62 |
* it is not involved in making policy decisions. It is merely the
|
|
63 |
* Java runtime representation of the persistent policy configuration
|
|
64 |
* file. <p>
|
|
65 |
*
|
|
66 |
* <p>When a protection domain needs to initialize its set of
|
|
67 |
* permissions, it executes code such as the following
|
|
68 |
* to ask the global Policy object to populate a
|
|
69 |
* Permissions object with the appropriate permissions:
|
|
70 |
* <pre>
|
|
71 |
* policy = Policy.getPolicy();
|
|
72 |
* Permissions perms = policy.getPermissions(protectiondomain)
|
|
73 |
* </pre>
|
|
74 |
*
|
|
75 |
* <p>The protection domain contains CodeSource
|
|
76 |
* object, which encapsulates its codebase (URL) and public key attributes.
|
|
77 |
* It also contains the principals associated with the domain.
|
|
78 |
* The Policy object evaluates the global policy in light of who the
|
|
79 |
* principal is and what the code source is and returns an appropriate
|
|
80 |
* Permissions object.
|
|
81 |
*
|
|
82 |
* @author Roland Schemers
|
|
83 |
* @author Ram Marti
|
|
84 |
*
|
|
85 |
* @since 1.2
|
|
86 |
*/
|
|
87 |
|
|
88 |
public class PolicyParser {
|
|
89 |
|
|
90 |
// needs to be public for PolicyTool
|
|
91 |
public static final String REPLACE_NAME = "PolicyParser.REPLACE_NAME";
|
|
92 |
|
|
93 |
private static final String EXTDIRS_PROPERTY = "java.ext.dirs";
|
|
94 |
private static final String OLD_EXTDIRS_EXPANSION =
|
|
95 |
"${" + EXTDIRS_PROPERTY + "}";
|
|
96 |
|
|
97 |
// package-private: used by PolicyFile for static policy
|
|
98 |
static final String EXTDIRS_EXPANSION = "${{" + EXTDIRS_PROPERTY + "}}";
|
|
99 |
|
|
100 |
|
|
101 |
private Vector<GrantEntry> grantEntries;
|
|
102 |
|
|
103 |
// Convenience variables for parsing
|
|
104 |
private static final Debug debug = Debug.getInstance("parser",
|
|
105 |
"\t[Policy Parser]");
|
|
106 |
private StreamTokenizer st;
|
|
107 |
private int lookahead;
|
|
108 |
private boolean expandProp = false;
|
|
109 |
private String keyStoreUrlString = null; // unexpanded
|
|
110 |
private String keyStoreType = null;
|
|
111 |
private String keyStoreProvider = null;
|
|
112 |
private String storePassURL = null;
|
|
113 |
|
|
114 |
private String expand(String value)
|
|
115 |
throws PropertyExpander.ExpandException
|
|
116 |
{
|
|
117 |
return expand(value, false);
|
|
118 |
}
|
|
119 |
|
|
120 |
private String expand(String value, boolean encodeURL)
|
|
121 |
throws PropertyExpander.ExpandException
|
|
122 |
{
|
|
123 |
if (!expandProp) {
|
|
124 |
return value;
|
|
125 |
} else {
|
|
126 |
return PropertyExpander.expand(value, encodeURL);
|
|
127 |
}
|
|
128 |
}
|
|
129 |
|
|
130 |
/**
|
|
131 |
* Creates a PolicyParser object.
|
|
132 |
*/
|
|
133 |
|
|
134 |
public PolicyParser() {
|
|
135 |
grantEntries = new Vector<GrantEntry>();
|
|
136 |
}
|
|
137 |
|
|
138 |
|
|
139 |
public PolicyParser(boolean expandProp) {
|
|
140 |
this();
|
|
141 |
this.expandProp = expandProp;
|
|
142 |
}
|
|
143 |
|
|
144 |
/**
|
|
145 |
* Reads a policy configuration into the Policy object using a
|
|
146 |
* Reader object. <p>
|
|
147 |
*
|
|
148 |
* @param policy the policy Reader object.
|
|
149 |
*
|
|
150 |
* @exception ParsingException if the policy configuration contains
|
|
151 |
* a syntax error.
|
|
152 |
*
|
|
153 |
* @exception IOException if an error occurs while reading the policy
|
|
154 |
* configuration.
|
|
155 |
*/
|
|
156 |
|
|
157 |
public void read(Reader policy)
|
|
158 |
throws ParsingException, IOException
|
|
159 |
{
|
|
160 |
if (!(policy instanceof BufferedReader)) {
|
|
161 |
policy = new BufferedReader(policy);
|
|
162 |
}
|
|
163 |
|
|
164 |
/**
|
|
165 |
* Configure the stream tokenizer:
|
|
166 |
* Recognize strings between "..."
|
|
167 |
* Don't convert words to lowercase
|
|
168 |
* Recognize both C-style and C++-style comments
|
|
169 |
* Treat end-of-line as white space, not as a token
|
|
170 |
*/
|
|
171 |
st = new StreamTokenizer(policy);
|
|
172 |
|
|
173 |
st.resetSyntax();
|
|
174 |
st.wordChars('a', 'z');
|
|
175 |
st.wordChars('A', 'Z');
|
|
176 |
st.wordChars('.', '.');
|
|
177 |
st.wordChars('0', '9');
|
|
178 |
st.wordChars('_', '_');
|
|
179 |
st.wordChars('$', '$');
|
|
180 |
st.wordChars(128 + 32, 255);
|
|
181 |
st.whitespaceChars(0, ' ');
|
|
182 |
st.commentChar('/');
|
|
183 |
st.quoteChar('\'');
|
|
184 |
st.quoteChar('"');
|
|
185 |
st.lowerCaseMode(false);
|
|
186 |
st.ordinaryChar('/');
|
|
187 |
st.slashSlashComments(true);
|
|
188 |
st.slashStarComments(true);
|
|
189 |
|
|
190 |
/**
|
|
191 |
* The main parsing loop. The loop is executed once
|
|
192 |
* for each entry in the config file. The entries
|
|
193 |
* are delimited by semicolons. Once we've read in
|
|
194 |
* the information for an entry, go ahead and try to
|
|
195 |
* add it to the policy vector.
|
|
196 |
*
|
|
197 |
*/
|
|
198 |
|
|
199 |
lookahead = st.nextToken();
|
|
200 |
while (lookahead != StreamTokenizer.TT_EOF) {
|
|
201 |
if (peek("grant")) {
|
|
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
|
|
244 |
* expansion fails.
|
|
245 |
*/
|
|
246 |
public String getKeyStoreUrl() {
|
|
247 |
try {
|
|
248 |
if (keyStoreUrlString!=null && keyStoreUrlString.length()!=0) {
|
|
249 |
return expand(keyStoreUrlString, true).replace
|
|
250 |
(File.separatorChar, '/');
|
|
251 |
}
|
|
252 |
} catch (PropertyExpander.ExpandException peee) {
|
|
253 |
if (debug != null) {
|
|
254 |
debug.println(peee.toString());
|
|
255 |
}
|
|
256 |
return null;
|
|
257 |
}
|
|
258 |
return null;
|
|
259 |
}
|
|
260 |
|
|
261 |
public void setKeyStoreUrl(String url) {
|
|
262 |
keyStoreUrlString = url;
|
|
263 |
}
|
|
264 |
|
|
265 |
public String getKeyStoreType() {
|
|
266 |
return keyStoreType;
|
|
267 |
}
|
|
268 |
|
|
269 |
public void setKeyStoreType(String type) {
|
|
270 |
keyStoreType = type;
|
|
271 |
}
|
|
272 |
|
|
273 |
public String getKeyStoreProvider() {
|
|
274 |
return keyStoreProvider;
|
|
275 |
}
|
|
276 |
|
|
277 |
public void setKeyStoreProvider(String provider) {
|
|
278 |
keyStoreProvider = provider;
|
|
279 |
}
|
|
280 |
|
|
281 |
public String getStorePassURL() {
|
|
282 |
try {
|
|
283 |
if (storePassURL!=null && storePassURL.length()!=0) {
|
|
284 |
return expand(storePassURL, true).replace
|
|
285 |
(File.separatorChar, '/');
|
|
286 |
}
|
|
287 |
} catch (PropertyExpander.ExpandException peee) {
|
|
288 |
if (debug != null) {
|
|
289 |
debug.println(peee.toString());
|
|
290 |
}
|
|
291 |
return null;
|
|
292 |
}
|
|
293 |
return null;
|
|
294 |
}
|
|
295 |
|
|
296 |
public void setStorePassURL(String storePassURL) {
|
|
297 |
this.storePassURL = storePassURL;
|
|
298 |
}
|
|
299 |
|
|
300 |
/**
|
|
301 |
* Enumerate all the entries in the global policy object.
|
|
302 |
* This method is used by policy admin tools. The tools
|
|
303 |
* should use the Enumeration methods on the returned object
|
|
304 |
* to fetch the elements sequentially.
|
|
305 |
*/
|
|
306 |
public Enumeration<GrantEntry> grantElements(){
|
|
307 |
return grantEntries.elements();
|
|
308 |
}
|
|
309 |
|
|
310 |
/**
|
|
311 |
* write out the policy
|
|
312 |
*/
|
|
313 |
|
|
314 |
public void write(Writer policy)
|
|
315 |
{
|
|
316 |
PrintWriter out = new PrintWriter(new BufferedWriter(policy));
|
|
317 |
|
|
318 |
Enumeration<GrantEntry> enum_ = grantElements();
|
|
319 |
|
|
320 |
out.println("/* AUTOMATICALLY GENERATED ON "+
|
|
321 |
(new java.util.Date()) + "*/");
|
|
322 |
out.println("/* DO NOT EDIT */");
|
|
323 |
out.println();
|
|
324 |
|
|
325 |
// write the (unexpanded) keystore entry as the first entry of the
|
|
326 |
// policy file
|
|
327 |
if (keyStoreUrlString != null) {
|
|
328 |
writeKeyStoreEntry(out);
|
|
329 |
}
|
|
330 |
if (storePassURL != null) {
|
|
331 |
writeStorePassURL(out);
|
|
332 |
}
|
|
333 |
|
|
334 |
// write "grant" entries
|
|
335 |
while (enum_.hasMoreElements()) {
|
|
336 |
GrantEntry ge = enum_.nextElement();
|
|
337 |
ge.write(out);
|
|
338 |
out.println();
|
|
339 |
}
|
|
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(";");
|
|
394 |
out.println();
|
|
395 |
}
|
|
396 |
|
|
397 |
private void writeStorePassURL(PrintWriter out) {
|
|
398 |
out.print("keystorePasswordURL \"");
|
|
399 |
out.print(storePassURL);
|
|
400 |
out.print('"');
|
|
401 |
out.println(";");
|
|
402 |
out.println();
|
|
403 |
}
|
|
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;
|
|
472 |
} else {
|
|
473 |
principalClass = match("principal type");
|
|
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
|
|
578 |
String[] extDirs = parseExtDirs(e.codeBase, es);
|
|
579 |
if (extDirs != null && extDirs.length > 0) {
|
|
580 |
for (int i = 0; i < extDirs.length; i++) {
|
|
581 |
GrantEntry newGe = (GrantEntry)e.clone();
|
|
582 |
newGe.codeBase = extDirs[i];
|
|
583 |
add(newGe);
|
|
584 |
|
|
585 |
if (debug != null) {
|
|
586 |
debug.println("creating policy entry for " +
|
|
587 |
"expanded java.ext.dirs path:\n\t\t" +
|
|
588 |
extDirs[i]);
|
|
589 |
}
|
|
590 |
}
|
|
591 |
}
|
|
592 |
ignoreEntry = true;
|
|
593 |
}
|
|
594 |
}
|
|
595 |
} catch (PropertyExpander.ExpandException peee) {
|
|
596 |
if (debug != null) {
|
|
597 |
debug.println(peee.toString());
|
|
598 |
}
|
|
599 |
return null;
|
|
600 |
}
|
|
601 |
|
|
602 |
return (ignoreEntry == true) ? null : e;
|
|
603 |
}
|
|
604 |
|
|
605 |
/**
|
|
606 |
* parse a Permission entry
|
|
607 |
*/
|
|
608 |
private PermissionEntry parsePermissionEntry()
|
|
609 |
throws ParsingException, IOException, PropertyExpander.ExpandException
|
|
610 |
{
|
|
611 |
PermissionEntry e = new PermissionEntry();
|
|
612 |
|
|
613 |
// Permission
|
|
614 |
match("Permission");
|
|
615 |
e.permission = match("permission type");
|
|
616 |
|
|
617 |
if (peek("\"")) {
|
|
618 |
// Permission name
|
|
619 |
e.name = expand(match("quoted string"));
|
|
620 |
}
|
|
621 |
|
|
622 |
if (!peek(",")) {
|
|
623 |
return e;
|
|
624 |
}
|
|
625 |
match(",");
|
|
626 |
|
|
627 |
if (peek("\"")) {
|
|
628 |
e.action = expand(match("quoted string"));
|
|
629 |
if (!peek(",")) {
|
|
630 |
return e;
|
|
631 |
}
|
|
632 |
match(",");
|
|
633 |
}
|
|
634 |
|
|
635 |
if (peekAndMatch("SignedBy")) {
|
|
636 |
e.signedBy = expand(match("quoted string"));
|
|
637 |
}
|
|
638 |
return e;
|
|
639 |
}
|
|
640 |
|
|
641 |
// package-private: used by PolicyFile for static policy
|
|
642 |
static String[] parseExtDirs(String codebase, int start) {
|
|
643 |
|
|
644 |
String s = System.getProperty(EXTDIRS_PROPERTY);
|
|
645 |
String globalPrefix = (start > 0 ? codebase.substring(0, start) : "file:");
|
|
646 |
int end = start + EXTDIRS_EXPANSION.length();
|
|
647 |
String globalSuffix = (end < codebase.length() ? codebase.substring(end) :
|
|
648 |
(String) null);
|
|
649 |
|
|
650 |
String[] dirs = null;
|
|
651 |
String localSuffix;
|
|
652 |
if (s != null) {
|
|
653 |
StringTokenizer st =
|
|
654 |
new StringTokenizer(s, File.pathSeparator);
|
|
655 |
int count = st.countTokens();
|
|
656 |
dirs = new String[count];
|
|
657 |
for (int i = 0; i < count; i++) {
|
|
658 |
File file = new File(st.nextToken());
|
|
659 |
dirs[i] = sun.net.www.ParseUtil.encodePath
|
|
660 |
(file.getAbsolutePath());
|
|
661 |
|
|
662 |
if (!dirs[i].startsWith("/")) {
|
|
663 |
dirs[i] = "/" + dirs[i];
|
|
664 |
}
|
|
665 |
|
|
666 |
localSuffix = (globalSuffix == null ?
|
|
667 |
(dirs[i].endsWith("/") ? "*" : "/*") :
|
|
668 |
globalSuffix);
|
|
669 |
|
|
670 |
dirs[i] = globalPrefix + dirs[i] + localSuffix;
|
|
671 |
}
|
|
672 |
}
|
|
673 |
return dirs;
|
|
674 |
}
|
|
675 |
|
|
676 |
private boolean peekAndMatch(String expect)
|
|
677 |
throws ParsingException, IOException
|
|
678 |
{
|
|
679 |
if (peek(expect)) {
|
|
680 |
match(expect);
|
|
681 |
return true;
|
|
682 |
} else {
|
|
683 |
return false;
|
|
684 |
}
|
|
685 |
}
|
|
686 |
|
|
687 |
private boolean peek(String expect) {
|
|
688 |
boolean found = false;
|
|
689 |
|
|
690 |
switch (lookahead) {
|
|
691 |
|
|
692 |
case StreamTokenizer.TT_WORD:
|
|
693 |
if (expect.equalsIgnoreCase(st.sval))
|
|
694 |
found = true;
|
|
695 |
break;
|
|
696 |
case ',':
|
|
697 |
if (expect.equalsIgnoreCase(","))
|
|
698 |
found = true;
|
|
699 |
break;
|
|
700 |
case '{':
|
|
701 |
if (expect.equalsIgnoreCase("{"))
|
|
702 |
found = true;
|
|
703 |
break;
|
|
704 |
case '}':
|
|
705 |
if (expect.equalsIgnoreCase("}"))
|
|
706 |
found = true;
|
|
707 |
break;
|
|
708 |
case '"':
|
|
709 |
if (expect.equalsIgnoreCase("\""))
|
|
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();
|
|
756 |
} else if (expect.equalsIgnoreCase("permission type")) {
|
|
757 |
value = st.sval;
|
|
758 |
lookahead = st.nextToken();
|
|
759 |
} else if (expect.equalsIgnoreCase("principal type")) {
|
|
760 |
value = st.sval;
|
|
761 |
lookahead = st.nextToken();
|
|
762 |
} else {
|
|
763 |
throw new ParsingException(st.lineno(), expect, st.sval);
|
|
764 |
}
|
|
765 |
break;
|
|
766 |
case ',':
|
|
767 |
if (expect.equalsIgnoreCase(","))
|
|
768 |
lookahead = st.nextToken();
|
|
769 |
else
|
|
770 |
throw new ParsingException(st.lineno(), expect, ",");
|
|
771 |
break;
|
|
772 |
case '{':
|
|
773 |
if (expect.equalsIgnoreCase("{"))
|
|
774 |
lookahead = st.nextToken();
|
|
775 |
else
|
|
776 |
throw new ParsingException(st.lineno(), expect, "{");
|
|
777 |
break;
|
|
778 |
case '}':
|
|
779 |
if (expect.equalsIgnoreCase("}"))
|
|
780 |
lookahead = st.nextToken();
|
|
781 |
else
|
|
782 |
throw new ParsingException(st.lineno(), expect, "}");
|
|
783 |
break;
|
|
784 |
case ';':
|
|
785 |
if (expect.equalsIgnoreCase(";"))
|
|
786 |
lookahead = st.nextToken();
|
|
787 |
else
|
|
788 |
throw new ParsingException(st.lineno(), expect, ";");
|
|
789 |
break;
|
|
790 |
case '*':
|
|
791 |
if (expect.equalsIgnoreCase("*"))
|
|
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
|
|
837 |
* <pre>
|
|
838 |
*
|
|
839 |
* pe = new PermissionEntry("java.io.FilePermission",
|
|
840 |
* "/tmp", "read,write");
|
|
841 |
*
|
|
842 |
* ge = new GrantEntry("Duke", null);
|
|
843 |
*
|
|
844 |
* ge.add(pe);
|
|
845 |
*
|
|
846 |
* </pre>
|
|
847 |
*
|
|
848 |
* @author Roland Schemers
|
|
849 |
*
|
|
850 |
* version 1.19, 05/21/98
|
|
851 |
*/
|
|
852 |
|
|
853 |
public static class GrantEntry {
|
|
854 |
|
|
855 |
public String signedBy;
|
|
856 |
public String codeBase;
|
|
857 |
public LinkedList<PrincipalEntry> principals;
|
|
858 |
public Vector<PermissionEntry> permissionEntries;
|
|
859 |
|
|
860 |
public GrantEntry() {
|
|
861 |
principals = new LinkedList<PrincipalEntry>();
|
|
862 |
permissionEntries = new Vector<PermissionEntry>();
|
|
863 |
}
|
|
864 |
|
|
865 |
public GrantEntry(String signedBy, String codeBase) {
|
|
866 |
this.codeBase = codeBase;
|
|
867 |
this.signedBy = signedBy;
|
|
868 |
principals = new LinkedList<PrincipalEntry>();
|
|
869 |
permissionEntries = new Vector<PermissionEntry>();
|
|
870 |
}
|
|
871 |
|
|
872 |
public void add(PermissionEntry pe)
|
|
873 |
{
|
|
874 |
permissionEntries.addElement(pe);
|
|
875 |
}
|
|
876 |
|
|
877 |
public boolean remove(PrincipalEntry pe)
|
|
878 |
{
|
|
879 |
return principals.remove(pe);
|
|
880 |
}
|
|
881 |
|
|
882 |
public boolean remove(PermissionEntry pe)
|
|
883 |
{
|
|
884 |
return permissionEntries.removeElement(pe);
|
|
885 |
}
|
|
886 |
|
|
887 |
public boolean contains(PrincipalEntry pe)
|
|
888 |
{
|
|
889 |
return principals.contains(pe);
|
|
890 |
}
|
|
891 |
|
|
892 |
public boolean contains(PermissionEntry pe)
|
|
893 |
{
|
|
894 |
return permissionEntries.contains(pe);
|
|
895 |
}
|
|
896 |
|
|
897 |
/**
|
|
898 |
* Enumerate all the permission entries in this GrantEntry.
|
|
899 |
*/
|
|
900 |
public Enumeration<PermissionEntry> permissionElements(){
|
|
901 |
return permissionEntries.elements();
|
|
902 |
}
|
|
903 |
|
|
904 |
|
|
905 |
public void write(PrintWriter out) {
|
|
906 |
out.print("grant");
|
|
907 |
if (signedBy != null) {
|
|
908 |
out.print(" signedBy \"");
|
|
909 |
out.print(signedBy);
|
|
910 |
out.print('"');
|
|
911 |
if (codeBase != null)
|
|
912 |
out.print(", ");
|
|
913 |
}
|
|
914 |
if (codeBase != null) {
|
|
915 |
out.print(" codeBase \"");
|
|
916 |
out.print(codeBase);
|
|
917 |
out.print('"');
|
|
918 |
if (principals != null && principals.size() > 0)
|
|
919 |
out.print(",\n");
|
|
920 |
}
|
|
921 |
if (principals != null && principals.size() > 0) {
|
|
922 |
ListIterator<PrincipalEntry> pli = principals.listIterator();
|
|
923 |
while (pli.hasNext()) {
|
|
924 |
out.print(" ");
|
|
925 |
PrincipalEntry pe = pli.next();
|
|
926 |
pe.write(out);
|
|
927 |
if (pli.hasNext())
|
|
928 |
out.print(",\n");
|
|
929 |
}
|
|
930 |
}
|
|
931 |
out.println(" {");
|
|
932 |
Enumeration<PermissionEntry> enum_ = permissionEntries.elements();
|
|
933 |
while (enum_.hasMoreElements()) {
|
|
934 |
PermissionEntry pe = enum_.nextElement();
|
|
935 |
out.write(" ");
|
|
936 |
pe.write(out);
|
|
937 |
}
|
|
938 |
out.println("};");
|
|
939 |
}
|
|
940 |
|
|
941 |
public Object clone() {
|
|
942 |
GrantEntry ge = new GrantEntry();
|
|
943 |
ge.codeBase = this.codeBase;
|
|
944 |
ge.signedBy = this.signedBy;
|
|
945 |
ge.principals = new LinkedList<PrincipalEntry>(this.principals);
|
|
946 |
ge.permissionEntries =
|
|
947 |
new Vector<PermissionEntry>(this.permissionEntries);
|
|
948 |
return ge;
|
|
949 |
}
|
|
950 |
}
|
|
951 |
|
|
952 |
/**
|
|
953 |
* Principal info (class and name) in a grant entry
|
|
954 |
*/
|
|
955 |
public static class PrincipalEntry {
|
|
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 |
}
|
|
997 |
|
|
998 |
public String getDisplayName() {
|
|
999 |
return getDisplayName(false);
|
|
1000 |
}
|
|
1001 |
|
|
1002 |
public String getDisplayName(boolean addQuote) {
|
|
1003 |
if (principalName.equals(WILDCARD_NAME)) {
|
|
1004 |
return "*";
|
|
1005 |
}
|
|
1006 |
else {
|
|
1007 |
if (addQuote) return "\"" + principalName + "\"";
|
|
1008 |
else return principalName;
|
|
1009 |
}
|
|
1010 |
}
|
|
1011 |
|
|
1012 |
public String toString() {
|
|
1013 |
if (!principalClass.equals(REPLACE_NAME)) {
|
|
1014 |
return getDisplayClass() + "/" + getDisplayName();
|
|
1015 |
} else {
|
|
1016 |
return getDisplayName();
|
|
1017 |
}
|
|
1018 |
}
|
|
1019 |
|
|
1020 |
/**
|
|
1021 |
* Test for equality between the specified object and this object.
|
|
1022 |
* Two PrincipalEntries are equal if their PrincipalClass and
|
|
1023 |
* PrincipalName values are equal.
|
|
1024 |
*
|
|
1025 |
* <p>
|
|
1026 |
*
|
|
1027 |
* @param obj the object to test for equality with this object.
|
|
1028 |
*
|
|
1029 |
* @return true if the objects are equal, false otherwise.
|
|
1030 |
*/
|
|
1031 |
public boolean equals(Object obj) {
|
|
1032 |
if (this == obj)
|
|
1033 |
return true;
|
|
1034 |
|
|
1035 |
if (!(obj instanceof PrincipalEntry))
|
|
1036 |
return false;
|
|
1037 |
|
|
1038 |
PrincipalEntry that = (PrincipalEntry)obj;
|
|
1039 |
if (this.principalClass.equals(that.principalClass) &&
|
|
1040 |
this.principalName.equals(that.principalName)) {
|
|
1041 |
return true;
|
|
1042 |
}
|
|
1043 |
|
|
1044 |
return false;
|
|
1045 |
}
|
|
1046 |
|
|
1047 |
/**
|
|
1048 |
* Return a hashcode for this <code>PrincipalEntry</code>.
|
|
1049 |
*
|
|
1050 |
* <p>
|
|
1051 |
*
|
|
1052 |
* @return a hashcode for this <code>PrincipalEntry</code>.
|
|
1053 |
*/
|
|
1054 |
public int hashCode() {
|
|
1055 |
return principalClass.hashCode();
|
|
1056 |
}
|
|
1057 |
public void write(PrintWriter out) {
|
|
1058 |
out.print("principal " + getDisplayClass() + " " +
|
|
1059 |
getDisplayName(true));
|
|
1060 |
}
|
|
1061 |
}
|
|
1062 |
|
|
1063 |
/**
|
|
1064 |
* Each permission entry in the policy configuration file is
|
|
1065 |
* represented by a
|
|
1066 |
* PermissionEntry object. <p>
|
|
1067 |
*
|
|
1068 |
* <p>
|
|
1069 |
* For example, the entry
|
|
1070 |
* <pre>
|
|
1071 |
* permission java.io.FilePermission "/tmp", "read,write";
|
|
1072 |
* </pre>
|
|
1073 |
* is represented internally
|
|
1074 |
* <pre>
|
|
1075 |
*
|
|
1076 |
* pe = new PermissionEntry("java.io.FilePermission",
|
|
1077 |
* "/tmp", "read,write");
|
|
1078 |
* </pre>
|
|
1079 |
*
|
|
1080 |
* @author Roland Schemers
|
|
1081 |
*
|
|
1082 |
* version 1.19, 05/21/98
|
|
1083 |
*/
|
|
1084 |
|
|
1085 |
public static class PermissionEntry {
|
|
1086 |
|
|
1087 |
public String permission;
|
|
1088 |
public String name;
|
|
1089 |
public String action;
|
|
1090 |
public String signedBy;
|
|
1091 |
|
|
1092 |
public PermissionEntry() {
|
|
1093 |
}
|
|
1094 |
|
|
1095 |
public PermissionEntry(String permission,
|
|
1096 |
String name,
|
|
1097 |
String action) {
|
|
1098 |
this.permission = permission;
|
|
1099 |
this.name = name;
|
|
1100 |
this.action = action;
|
|
1101 |
}
|
|
1102 |
|
|
1103 |
/**
|
|
1104 |
* Calculates a hash code value for the object. Objects
|
|
1105 |
* which are equal will also have the same hashcode.
|
|
1106 |
*/
|
|
1107 |
public int hashCode() {
|
|
1108 |
int retval = permission.hashCode();
|
|
1109 |
if (name != null) retval ^= name.hashCode();
|
|
1110 |
if (action != null) retval ^= action.hashCode();
|
|
1111 |
return retval;
|
|
1112 |
}
|
|
1113 |
|
|
1114 |
public boolean equals(Object obj) {
|
|
1115 |
if (obj == this)
|
|
1116 |
return true;
|
|
1117 |
|
|
1118 |
if (! (obj instanceof PermissionEntry))
|
|
1119 |
return false;
|
|
1120 |
|
|
1121 |
PermissionEntry that = (PermissionEntry) obj;
|
|
1122 |
|
|
1123 |
if (this.permission == null) {
|
|
1124 |
if (that.permission != null) return false;
|
|
1125 |
} else {
|
|
1126 |
if (!this.permission.equals(that.permission)) return false;
|
|
1127 |
}
|
|
1128 |
|
|
1129 |
if (this.name == null) {
|
|
1130 |
if (that.name != null) return false;
|
|
1131 |
} else {
|
|
1132 |
if (!this.name.equals(that.name)) return false;
|
|
1133 |
}
|
|
1134 |
|
|
1135 |
if (this.action == null) {
|
|
1136 |
if (that.action != null) return false;
|
|
1137 |
} else {
|
|
1138 |
if (!this.action.equals(that.action)) return false;
|
|
1139 |
}
|
|
1140 |
|
|
1141 |
if (this.signedBy == null) {
|
|
1142 |
if (that.signedBy != null) return false;
|
|
1143 |
} else {
|
|
1144 |
if (!this.signedBy.equals(that.signedBy)) return false;
|
|
1145 |
}
|
|
1146 |
|
|
1147 |
// everything matched -- the 2 objects are equal
|
|
1148 |
return true;
|
|
1149 |
}
|
|
1150 |
|
|
1151 |
public void write(PrintWriter out) {
|
|
1152 |
out.print("permission ");
|
|
1153 |
out.print(permission);
|
|
1154 |
if (name != null) {
|
|
1155 |
out.print(" \"");
|
|
1156 |
|
|
1157 |
// ATTENTION: regex with double escaping,
|
|
1158 |
// the normal forms look like:
|
|
1159 |
// $name =~ s/\\/\\\\/g; and
|
|
1160 |
// $name =~ s/\"/\\\"/g;
|
|
1161 |
// and then in a java string, it's escaped again
|
|
1162 |
|
|
1163 |
out.print(name.replaceAll("\\\\", "\\\\\\\\").replaceAll("\\\"", "\\\\\\\""));
|
|
1164 |
out.print('"');
|
|
1165 |
}
|
|
1166 |
if (action != null) {
|
|
1167 |
out.print(", \"");
|
|
1168 |
out.print(action);
|
|
1169 |
out.print('"');
|
|
1170 |
}
|
|
1171 |
if (signedBy != null) {
|
|
1172 |
out.print(", signedBy \"");
|
|
1173 |
out.print(signedBy);
|
|
1174 |
out.print('"');
|
|
1175 |
}
|
|
1176 |
out.println(";");
|
|
1177 |
}
|
|
1178 |
}
|
|
1179 |
|
|
1180 |
public static class ParsingException extends GeneralSecurityException {
|
|
1181 |
|
|
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) {
|
|
1232 |
fr.close();
|
|
1233 |
}
|
|
1234 |
|
|
1235 |
if (fw != null) {
|
|
1236 |
fw.close();
|
|
1237 |
}
|
|
1238 |
}
|
|
1239 |
}
|
|
1240 |
}
|