46 |
|
|
47 |
private static final String CURRENT_DIRECTORY = |
private static final String CURRENT_DIRECTORY = |
48 |
System.getProperty("user.dir"); |
System.getProperty("user.dir"); |
49 |
|
|
50 |
private boolean readPerm = false; |
private boolean readPerm = false; |
51 |
private boolean writePerm = false; |
private boolean writePerm = false; |
52 |
private boolean executePerm = false; |
private boolean executePerm = false; |
53 |
private boolean deletePerm = false; |
private boolean deletePerm = false; |
54 |
private final String actionsString; |
private final String actionsString; |
55 |
|
|
56 |
// Checks and caches the actions |
// Checks and caches the actions |
57 |
private void checkPerms() throws IllegalArgumentException |
private void checkPerms() throws IllegalArgumentException |
58 |
{ |
{ |
59 |
String action; |
String action; |
60 |
int i = actionsString.indexOf(','); |
int i = actionsString.indexOf(','); |
61 |
int startI = 0; |
int startI = 0; |
62 |
while(i != -1) |
while (i != -1) |
63 |
{ |
{ |
64 |
action = actionsString.substring(startI,i).trim().toLowerCase(); |
action = actionsString.substring(startI, i).trim().toLowerCase(); |
65 |
if(action.equals("read")) |
if (action.equals("read")) |
66 |
readPerm = true; |
readPerm = true; |
67 |
else if(action.equals("write")) |
else if (action.equals("write")) |
68 |
writePerm = true; |
writePerm = true; |
69 |
else if(action.equals("execute")) |
else if (action.equals("execute")) |
70 |
executePerm = true; |
executePerm = true; |
71 |
else if(action.equals("delete")) |
else if (action.equals("delete")) |
72 |
deletePerm = true; |
deletePerm = true; |
73 |
else |
else |
74 |
throw new IllegalArgumentException("Unknown action: " + action); |
throw new IllegalArgumentException("Unknown action: " + action); |
75 |
|
|
76 |
startI = i+1; |
startI = i + 1; |
77 |
i = actionsString.indexOf(',',startI); |
i = actionsString.indexOf(',', startI); |
78 |
} |
} |
79 |
|
|
80 |
action = actionsString.substring(startI).trim().toLowerCase(); |
action = actionsString.substring(startI).trim().toLowerCase(); |
81 |
if(action.equals("read")) |
if (action.equals("read")) |
82 |
readPerm = true; |
readPerm = true; |
83 |
else if(action.equals("write")) |
else if (action.equals("write")) |
84 |
writePerm = true; |
writePerm = true; |
85 |
else if(action.equals("execute")) |
else if (action.equals("execute")) |
86 |
executePerm = true; |
executePerm = true; |
87 |
else if(action.equals("delete")) |
else if (action.equals("delete")) |
88 |
deletePerm = true; |
deletePerm = true; |
89 |
else |
else |
90 |
throw new IllegalArgumentException("Unknown action: " + action); |
throw new IllegalArgumentException("Unknown action: " + action); |
91 |
} |
} |
92 |
|
|
93 |
/* |
/** |
94 |
* Create a new FilePermission. |
* Create a new FilePermission. |
95 |
* |
* |
96 |
* @param pathExpression an expression specifying the paths this |
* @param pathExpression an expression specifying the paths this |
98 |
* @param actionsString a comma-separated list of the actions this |
* @param actionsString a comma-separated list of the actions this |
99 |
* permission represents. The actions must be "read", "write", |
* permission represents. The actions must be "read", "write", |
100 |
* "execute" and/or "delete". |
* "execute" and/or "delete". |
|
* |
|
|
* FIXME: what to do when the file string is malformed? |
|
101 |
*/ |
*/ |
102 |
public FilePermission(String pathExpression, String actionsString) |
public FilePermission(String pathExpression, String actionsString) |
103 |
{ |
{ |
104 |
|
// FIXME: what to do when the file string is malformed? |
105 |
super(pathExpression); |
super(pathExpression); |
106 |
if (pathExpression == null) |
if (pathExpression == null) |
107 |
throw new NullPointerException("pathExpression"); |
throw new NullPointerException("pathExpression"); |
111 |
checkPerms(); |
checkPerms(); |
112 |
} |
} |
113 |
|
|
114 |
/** Get the actions this FilePermission supports. |
/** |
115 |
** @return the String representing the actions this FilePermission supports. |
* Get the actions this FilePermission supports. |
116 |
**/ |
* @return the String representing the actions this FilePermission supports. |
117 |
|
*/ |
118 |
public String getActions() |
public String getActions() |
119 |
{ |
{ |
120 |
return actionsString; |
return actionsString; |
121 |
} |
} |
122 |
|
|
123 |
/** Get the hash code for this Object.<P> |
/** |
124 |
** FilePermission's hash code is calculated as the exclusive or of the |
* Get the hash code for this Object.<P> |
125 |
** target |
* FilePermission's hash code is calculated as the exclusive or of the |
126 |
** String's hash code and the action String's hash code. |
* target |
127 |
** @specnote Sun did not specify how to calculate the hash code; |
* String's hash code and the action String's hash code. |
128 |
** I made this up. |
* @specnote Sun did not specify how to calculate the hash code; |
129 |
** @return the hash code for this Object. |
* I made this up. |
130 |
**/ |
* @return the hash code for this Object. |
131 |
|
*/ |
132 |
public int hashCode() |
public int hashCode() |
133 |
{ |
{ |
134 |
return getName().hashCode() ^ actionsString.hashCode(); |
return getName().hashCode() ^ actionsString.hashCode(); |
135 |
} |
} |
136 |
|
|
137 |
/** Check two FilePermissions for semantic equality. |
/** |
138 |
** Two FilePermissions are exactly equivalent if they have identical path |
* Check two FilePermissions for semantic equality. |
139 |
** expressions and have exactly the same access permissions. |
* Two FilePermissions are exactly equivalent if they have identical path |
140 |
** @param o the Object to compare to. |
* expressions and have exactly the same access permissions. |
141 |
** @return whether the Objects are semantically equivalent. |
* @param o the Object to compare to. |
142 |
**/ |
* @return whether the Objects are semantically equivalent. |
143 |
|
*/ |
144 |
public boolean equals(Object o) |
public boolean equals(Object o) |
145 |
{ |
{ |
146 |
if(!(o instanceof FilePermission)) |
if (! (o instanceof FilePermission)) |
147 |
return false; |
return false; |
148 |
FilePermission p = (FilePermission)o; |
FilePermission p = (FilePermission) o; |
149 |
|
|
150 |
String f1 = getName(); |
String f1 = getName(); |
151 |
String f2 = p.getName(); |
String f2 = p.getName(); |
152 |
|
|
153 |
/* Compare names, taking into account if they refer to a |
// Compare names, taking into account if they refer to a directory |
154 |
* directory and one has a separator and the other does not. |
// and one has a separator and the other does not. |
155 |
*/ |
if (f1.length() > 0 && f1.charAt(f1.length() - 1) == File.separatorChar) |
|
if(f1.length() > 0 && f1.charAt(f1.length() - 1) == File.separatorChar) |
|
156 |
{ |
{ |
157 |
if(f2.length() > 0 |
if (f2.length() > 0 |
158 |
&& f2.charAt(f2.length() - 1) == File.separatorChar) |
&& f2.charAt(f2.length() - 1) == File.separatorChar) |
159 |
{ |
{ |
160 |
if(!f2.equals(f1)) |
if (! f2.equals(f1)) |
161 |
return false; |
return false; |
162 |
} |
} |
163 |
else |
else |
164 |
{ |
{ |
165 |
if(!f2.equals(f1.substring(0,f1.length()-1))) |
if (! f2.equals(f1.substring(0, f1.length() - 1))) |
166 |
return false; |
return false; |
167 |
} |
} |
168 |
} |
} |
169 |
else |
else |
170 |
{ |
{ |
171 |
if(f2.length() > 0 |
if (f2.length() > 0 |
172 |
&& f2.charAt(f2.length() - 1) == File.separatorChar) |
&& f2.charAt(f2.length() - 1) == File.separatorChar) |
173 |
{ |
{ |
174 |
if(!f1.equals(f2.substring(0,f2.length()-1))) |
if (! f1.equals(f2.substring(0, f2.length() - 1))) |
175 |
return false; |
return false; |
176 |
} |
} |
177 |
else |
else |
178 |
{ |
{ |
179 |
if(!f1.equals(f2)) |
if (! f1.equals(f2)) |
180 |
return false; |
return false; |
181 |
} |
} |
182 |
} |
} |
183 |
return readPerm == p.readPerm && writePerm == p.writePerm && executePerm == p.executePerm && deletePerm == p.deletePerm; |
return (readPerm == p.readPerm |
184 |
|
&& writePerm == p.writePerm |
185 |
|
&& executePerm == p.executePerm |
186 |
|
&& deletePerm == p.deletePerm); |
187 |
} |
} |
188 |
|
|
189 |
/** Check to see if this permission implies another. |
/** |
190 |
** Permission A implies permission B if these things are all true: |
* Check to see if this permission implies another. |
191 |
** <OL> |
* Permission A implies permission B if these things are all true: |
192 |
** <LI>A and B are both FilePermissions.</LI> |
* <OL> |
193 |
** <LI>All possible files in B are included in A |
* <LI>A and B are both FilePermissions.</LI> |
194 |
** (possibly more are in A).</LI> |
* <LI>All possible files in B are included in A |
195 |
** <LI>All actions B supports, A also supports.</LI> |
* (possibly more are in A).</LI> |
196 |
** </OL> |
* <LI>All actions B supports, A also supports.</LI> |
197 |
** @param p the Permission to compare against. |
* </OL> |
198 |
** @return whether this Permission implies p |
* @param p the Permission to compare against. |
199 |
**/ |
* @return whether this Permission implies p |
200 |
|
*/ |
201 |
public boolean implies(Permission p) |
public boolean implies(Permission p) |
202 |
{ |
{ |
203 |
FilePermission fp; |
FilePermission fp; |
204 |
|
|
205 |
if(!(p instanceof FilePermission)) |
if (! (p instanceof FilePermission)) |
206 |
return false; |
return false; |
207 |
|
|
208 |
fp = (FilePermission)p; |
fp = (FilePermission) p; |
209 |
|
|
210 |
String f1 = getName(); |
String f1 = getName(); |
211 |
String f2 = fp.getName(); |
String f2 = fp.getName(); |
212 |
|
|
213 |
if(f1.charAt(0) != File.separatorChar) |
if (f1.charAt(0) != File.separatorChar) |
214 |
{ |
f1 = CURRENT_DIRECTORY + f1; |
215 |
f1 = CURRENT_DIRECTORY + f1; |
if (f2.charAt(0) != File.separatorChar) |
216 |
} |
f2 = CURRENT_DIRECTORY + f2; |
217 |
if(f2.charAt(0) != File.separatorChar) |
|
|
{ |
|
|
f2 = CURRENT_DIRECTORY + f2; |
|
|
} |
|
|
|
|
218 |
String sub1; |
String sub1; |
219 |
|
|
220 |
switch(f1.charAt(f1.length() - 1)) |
switch (f1.charAt(f1.length() - 1)) |
221 |
{ |
{ |
222 |
case '*': |
case '*': |
223 |
sub1 = f1.substring(0,f1.length() - 1); // chop off "*" |
sub1 = f1.substring(0, f1.length() - 1); // chop off "*" |
224 |
if(f2.length() <= sub1.length()) |
if (f2.length() <= sub1.length()) |
225 |
{ |
{ |
226 |
/* If it's smaller, there is no way it could be part of this |
// If it's smaller, there is no way it could be part of |
227 |
* directory. |
// this directory. If it's the same (or length - 1), it |
228 |
* If it's the same (or length - 1), it could be the same |
// could be the same directory but specifies access to |
229 |
* directory but |
// the directory rather than the files in it. |
230 |
* specifies access to the directory rather than the files in it. |
return false; |
231 |
*/ |
} |
232 |
return false; |
else if (f2.charAt(sub1.length() - 1) == File.separatorChar) |
233 |
} |
{ |
234 |
else if(f2.charAt(sub1.length() - 1) == File.separatorChar) |
// Make sure the part before the "/" is the same. |
235 |
{ |
if (! f2.substring(0, sub1.length()).equals(sub1)) |
236 |
/* Make sure the part before the "/" is the same */ |
return false; |
237 |
if(!f2.substring(0,sub1.length()).equals(sub1)) |
// Make sure there are no subdirectories specified |
238 |
return false; |
// underneath this one. |
239 |
/* Make sure there are no subdirectories specified |
if (f2.substring(sub1.length() + 1).indexOf(File.separatorChar) |
240 |
underneath this one */ |
!= -1) |
|
if(f2.substring(sub1.length()+1).indexOf(File.separatorChar) |
|
|
!= -1) |
|
|
return false; |
|
|
} |
|
|
else |
|
|
{ |
|
|
/* Obviously not equal: f2 is either not a directory or is not |
|
|
* the same directory (its name continues further than we want) |
|
|
*/ |
|
|
return false; |
|
|
} |
|
|
break; |
|
|
case '-': |
|
|
sub1 = f1.substring(0,f1.length() - 2); // chop off "/-" |
|
|
if(f2.length() < sub1.length()) |
|
|
{ |
|
|
/* If it's smaller, there is no way it could be part of |
|
|
* this directory. */ |
|
|
return false; |
|
|
} |
|
|
else if(f2.length() > sub1.length() && f2.charAt(sub1.length()) |
|
|
!= File.separatorChar) |
|
|
{ |
|
241 |
return false; |
return false; |
242 |
|
} |
243 |
} |
else |
244 |
else if(!f2.substring(0,sub1.length()).equals(sub1)) |
{ |
245 |
|
// Obviously not equal: f2 is either not a directory or |
246 |
|
// is not the same directory (its name continues further |
247 |
|
// than we want). |
248 |
|
return false; |
249 |
|
} |
250 |
|
break; |
251 |
|
case '-': |
252 |
|
// Chop off "/-". |
253 |
|
sub1 = f1.substring(0, f1.length() - 2); |
254 |
|
if (f2.length() < sub1.length()) |
255 |
|
{ |
256 |
|
// If it's smaller, there is no way it could be part of |
257 |
|
// this directory. |
258 |
return false; |
return false; |
259 |
break; |
} |
260 |
/* Looks redundant with default case and won't compile anyway - arenn |
else if (f2.length() > sub1.length() |
261 |
case File.separatorChar: |
&& f2.charAt(sub1.length()) != File.separatorChar) |
|
if(f2.charAt(f2.length()) == File.separatorChar) { |
|
|
if(!f2.equals(f1)) |
|
262 |
return false; |
return false; |
263 |
} else { |
else if (! f2.substring(0, sub1.length()).equals(sub1)) |
|
if(!f2.equals(f1.substring(0,f1.length()-1))) |
|
264 |
return false; |
return false; |
265 |
|
break; |
266 |
|
|
267 |
|
default: |
268 |
|
if (f2.charAt(f2.length() - 1) == File.separatorChar) |
269 |
|
{ |
270 |
|
if (! f1.equals(f2.substring(0, f2.length() - 1))) |
271 |
|
return false; |
272 |
|
} |
273 |
|
else if (!f1.equals(f2)) |
274 |
|
return false; |
275 |
|
break; |
276 |
} |
} |
277 |
break; |
|
278 |
*/ |
if (readPerm && ! fp.readPerm) |
|
default: |
|
|
if(f2.charAt(f2.length() - 1) == File.separatorChar) |
|
|
{ |
|
|
if(!f1.equals(f2.substring(0,f2.length() - 1))) |
|
|
return false; |
|
|
} |
|
|
else |
|
|
{ |
|
|
if(!f1.equals(f2)) |
|
|
return false; |
|
|
} |
|
|
break; |
|
|
} |
|
|
|
|
|
if(readPerm && !fp.readPerm) |
|
279 |
return false; |
return false; |
280 |
if(writePerm && !fp.writePerm) |
if (writePerm && ! fp.writePerm) |
281 |
return false; |
return false; |
282 |
if(executePerm && !fp.executePerm) |
if (executePerm && ! fp.executePerm) |
283 |
return false; |
return false; |
284 |
if(deletePerm && !fp.deletePerm) |
if (deletePerm && ! fp.deletePerm) |
285 |
return false; |
return false; |
286 |
|
|
287 |
return true; |
return true; |
288 |
} |
} |
289 |
} // class FilePermission |
} |
|
|
|