1 7 8 9 package java.security; 10 11 import java.io.*; 12 import java.lang.RuntimePermission ; 13 import java.net.MalformedURLException ; 14 import java.net.URL ; 15 import java.util.Enumeration ; 16 import java.util.Hashtable ; 17 import java.util.Vector ; 18 import java.util.StringTokenizer ; 19 import java.util.PropertyPermission ; 20 21 import java.lang.reflect.*; 22 23 import java.util.WeakHashMap ; 24 import sun.security.util.Debug; 25 import sun.security.util.SecurityConstants; 26 27 28 76 77 public abstract class Policy { 78 79 80 private static Policy policy; private static final Debug debug = Debug.getInstance("policy"); 82 83 private WeakHashMap pdMapping; 85 86 87 static boolean isSet() 88 { 89 return policy != null; 90 } 91 92 110 public static Policy getPolicy() 111 { 112 SecurityManager sm = System.getSecurityManager(); 113 if (sm != null) 114 sm.checkPermission(SecurityConstants.GET_POLICY_PERMISSION); 115 return getPolicyNoCheck(); 116 } 117 118 125 static synchronized Policy getPolicyNoCheck() 126 { 127 if (policy == null) { 128 String policy_class = null; 129 policy_class = (String )AccessController.doPrivileged( 130 new PrivilegedAction () { 131 public Object run() { 132 return Security.getProperty("policy.provider"); 133 } 134 }); 135 if (policy_class == null) { 136 policy_class = "sun.security.provider.PolicyFile"; 137 } 138 139 145 policy = new sun.security.provider.PolicyFile(true); 146 147 try { 148 policy = (Policy ) 149 Class.forName(policy_class).newInstance(); 150 } catch (Exception e) { 151 152 154 final String pc = policy_class; 155 Policy p = (Policy ) 156 AccessController.doPrivileged(new PrivilegedAction () { 157 public Object run() { 158 try { 159 ClassLoader cl = 160 ClassLoader.getSystemClassLoader(); 161 ClassLoader extcl = null; 163 while (cl != null) { 164 extcl = cl; 165 cl = cl.getParent(); 166 } 167 return (extcl != null? Class.forName 168 (pc, true, extcl).newInstance(): 169 null); 170 } catch (Exception e) { 171 return null; 172 } 173 } 174 }); 175 179 if (p != null) 180 policy = p; 181 182 if (p == null && debug != null) { 183 debug.println("policy provider " + 184 policy_class + " not available;using " + 185 "sun.security.provider.PolicyFile"); 186 e.printStackTrace(); 187 } 188 } 189 } 190 return policy; 191 } 192 193 210 public static void setPolicy(Policy p) 211 { 212 SecurityManager sm = System.getSecurityManager(); 213 if (sm != null) sm.checkPermission( 214 new SecurityPermission ("setPolicy")); 215 if (p != null) { 216 initPolicy(p); 217 } 218 synchronized (Policy .class) { 219 Policy.policy = p; 220 } 221 } 222 223 229 private static void initPolicy (final Policy p) { 230 250 251 ProtectionDomain policyDomain = (ProtectionDomain ) 252 AccessController.doPrivileged(new PrivilegedAction () { 253 public Object run() { 254 return p.getClass().getProtectionDomain(); 255 } 256 }); 257 258 263 PermissionCollection policyPerms = null; 264 synchronized (p) { 265 if (p.pdMapping == null) { 266 p.pdMapping = new WeakHashMap (); 267 } 268 } 269 270 if (policyDomain.getCodeSource() != null) { 271 if (Policy.isSet()) { 272 policyPerms = policy.getPermissions(policyDomain); 273 } 274 275 if (policyPerms == null) { policyPerms = new Permissions (); 277 policyPerms.add(SecurityConstants.ALL_PERMISSION); 278 } 279 280 synchronized (p.pdMapping) { 281 p.pdMapping.put(policyDomain, policyPerms); 283 } 284 } 285 return; 286 } 287 288 304 public abstract PermissionCollection getPermissions(CodeSource codesource); 305 306 323 public PermissionCollection getPermissions(ProtectionDomain domain) { 324 PermissionCollection pc = null; 325 326 if (domain == null) 327 return new Permissions (); 328 329 if (pdMapping == null) { 330 initPolicy(this); 331 } 332 333 synchronized (pdMapping) { 334 pc = (PermissionCollection )pdMapping.get(domain); 335 } 336 337 if (pc != null) { 338 Permissions perms = new Permissions (); 339 synchronized (pc) { 340 for (Enumeration e = pc.elements() ; e.hasMoreElements() ;) { 341 perms.add((Permission )e.nextElement()); 342 } 343 } 344 return perms; 345 } 346 347 pc = getPermissions(domain.getCodeSource()); 348 if (pc == null) { 349 pc = new Permissions (); 350 } 351 352 addStaticPerms(pc, domain.getPermissions()); 353 return pc; 354 } 355 356 359 private void addStaticPerms(PermissionCollection perms, 360 PermissionCollection statics) { 361 if (statics != null) { 362 synchronized (statics) { 363 Enumeration e = statics.elements(); 364 while (e.hasMoreElements()) { 365 perms.add((Permission )e.nextElement()); 366 } 367 } 368 } 369 } 370 371 385 public boolean implies(ProtectionDomain domain, Permission permission) { 386 PermissionCollection pc; 387 388 if (pdMapping == null) { 389 initPolicy(this); 390 } 391 392 synchronized (pdMapping) { 393 pc = (PermissionCollection )pdMapping.get(domain); 394 } 395 396 if (pc != null) { 397 return pc.implies(permission); 398 } 399 400 pc = getPermissions(domain); 401 if (pc == null) { 402 return false; 403 } 404 405 synchronized (pdMapping) { 406 pdMapping.put(domain, pc); 408 } 409 410 return pc.implies(permission); 411 } 412 413 419 public abstract void refresh(); 420 } 421 | Popular Tags |