1 23 24 33 34 package com.sun.enterprise.security.provider; 35 36 import sun.security.provider.PolicyFile; 37 import sun.security.util.PropertyExpander; 38 import java.security.*; 39 import javax.security.jacc.*; 40 import javax.management.MBeanPermission ; 41 import java.io.File ; 42 import java.net.URI ; 43 import java.net.URL ; 44 import java.util.Collection ; 45 import java.util.Enumeration ; 46 import java.util.Iterator ; 47 import java.util.logging.*; 48 import java.lang.*; 49 import com.sun.logging.LogDomains; 50 import com.sun.enterprise.util.LocalStringManagerImpl; 51 65 public final class PolicyWrapper extends java.security.Policy { 66 67 private java.security.Policy policy = null; 69 70 private static Logger logger = Logger.getLogger(LogDomains.SECURITY_LOGGER); 71 private static LocalStringManagerImpl localStrings = 72 new LocalStringManagerImpl(PolicyWrapper.class); 73 74 static String logMsg(Level level,String key,Object [] params,String defMsg){ 75 String msg = (key == null ? defMsg : localStrings.getLocalString 76 (key,defMsg == null ? key : defMsg, params)); 77 logger.log(level,msg); 78 return msg; 79 } 80 81 private static final String REUSE = "java.security.Policy.supportsReuse"; 82 83 93 private static final String IGNORE_REENTRANCY_PROP_NAME = 94 "com.sun.enterprise.security.provider.PolicyWrapper.ignoreReentrancy"; 95 96 103 private static final boolean avoidReentrancy = 104 (!Boolean.getBoolean(IGNORE_REENTRANCY_PROP_NAME)) && 105 (System.getSecurityManager() != null); 106 107 113 private static ThreadLocal reentrancyStatus; 114 115 static { 116 if(avoidReentrancy) { 117 reentrancyStatus = new ThreadLocal () { 118 protected synchronized Object initialValue() { 119 return new byte[]{0}; 120 } 121 }; 122 } 123 } 124 125 126 public PolicyWrapper() { 127 policy = (java.security.Policy ) new sun.security.provider.PolicyFile(); 129 refreshTime = 0L; 130 defaultContextChanged(); 132 } 133 134 150 public PermissionCollection getPermissions(CodeSource codesource) { 151 String contextId = PolicyContext.getContextID(); 152 PolicyConfigurationImpl pci = getPolicyConfigForContext(contextId); 153 Policy appPolicy = getPolicy(pci); 154 PermissionCollection perms = appPolicy.getPermissions(codesource); 155 if (perms != null) { 156 perms = removeExcludedPermissions(pci,perms); 157 } 158 if (logger.isLoggable(Level.FINEST)){ 159 logger.finest("JACC Policy Provider: PolicyWrapper.getPermissions(cs), context ("+contextId+") codesource ("+codesource+") permissions: "+perms); 160 } 161 return perms; 162 } 163 164 181 public PermissionCollection getPermissions(ProtectionDomain domain) { 182 String contextId = PolicyContext.getContextID(); 183 PolicyConfigurationImpl pci = getPolicyConfigForContext(contextId); 184 Policy appPolicy = getPolicy(pci); 185 PermissionCollection perms = appPolicy.getPermissions(domain); 186 if (perms != null) { 187 perms = removeExcludedPermissions(pci,perms); 188 } 189 if (logger.isLoggable(Level.FINEST)){ 190 logger.finest("JACC Policy Provider: PolicyWrapper.getPermissions(d), context ("+contextId+") permissions: "+perms); 191 } 192 return perms; 193 } 194 195 209 public boolean implies(ProtectionDomain domain, Permission permission) { 210 if(avoidReentrancy) { 211 byte[] alreadyCalled = (byte[]) reentrancyStatus.get(); 212 if(alreadyCalled[0] == 1) { 213 return true; 214 } else { 215 alreadyCalled[0] = 1; 216 try { 217 return doImplies(domain, permission); 218 } finally { 219 alreadyCalled[0] = 0; 220 } 221 } 222 } else { 223 return doImplies(domain, permission); 224 } 225 } 226 227 228 234 public void refresh() { 235 if (logger.isLoggable(Level.FINE)){ 236 logger.fine("JACC Policy Provider: Refreshing Policy files!"); 237 } 238 239 policy.refresh(); 244 245 252 boolean force = defaultContextChanged(); 253 254 Collection c = PolicyConfigurationFactoryImpl.getPolicyConfigurationImpls(); 255 if (c != null) { 256 Iterator it = c.iterator(); 257 while (it.hasNext()) { 258 PolicyConfigurationImpl pci = (PolicyConfigurationImpl)it.next(); 259 if (pci != null) { 260 pci.refresh(force); 263 } 264 } 265 } 266 try { 267 if (PolicyContext.getHandlerKeys().contains(REUSE)) { 268 PolicyContext.getContext(REUSE); 269 } 270 } catch(PolicyContextException pe) { 271 throw new IllegalStateException (pe.toString()); 272 } 273 } 274 275 private static PolicyConfigurationImpl getPolicyConfigForContext(String contextId) { 276 PolicyConfigurationImpl pci = null; 277 if (contextId != null) { 278 pci = PolicyConfigurationFactoryImpl.getPolicyConfigurationImpl(contextId); 279 } 280 return pci; 281 } 282 283 private Policy getPolicy(PolicyConfigurationImpl pci) { 284 Policy result = null; 285 if (pci == null) { 286 result = policy; 287 } else { 288 result = pci.getPolicy(); 289 if (result == null) { 290 result = policy; 292 } 293 } 294 return result; 295 } 296 297 private static Permissions getExcludedPolicy(PolicyConfigurationImpl pci) { 298 Permissions result = null; 299 if (pci != null) { 300 result = pci.getExcludedPolicy(); 301 } 302 return result; 303 } 304 305 private static PermissionCollection removeExcludedPermissions 310 (PolicyConfigurationImpl pci, PermissionCollection perms) { 311 PermissionCollection result = perms; 312 boolean noneRemoved = true; 313 Permissions excluded = getExcludedPolicy(pci); 314 if (excluded != null && excluded.elements().hasMoreElements()) { 315 result = null; 316 Enumeration e = perms.elements(); 317 while (e.hasMoreElements()) { 318 Permission granted = (Permission ) e.nextElement(); 319 if (!grantedIsExcluded(granted,excluded)) { 320 if (result == null) result = new Permissions(); 321 result.add(granted); 322 } else { 323 noneRemoved = false; 324 } 325 } 326 if (noneRemoved) { 327 result = perms; 328 } 329 } 330 return result; 331 } 332 333 private static boolean grantedIsExcluded(Permission granted, Permissions excluded) { 334 boolean isExcluded = false; 335 if (excluded != null) { 336 if (!excluded.implies(granted)) { 337 Enumeration e = excluded.elements(); 338 while (!isExcluded && e.hasMoreElements()) { 339 Permission excludedPerm = (Permission ) e.nextElement(); 340 if (granted.implies(excludedPerm)) { 341 isExcluded = true; 342 } 343 } 344 } else { 345 isExcluded = true; 346 } 347 } 348 if (logger.isLoggable(Level.FINEST)){ 349 if (isExcluded) { 350 logger.finest("JACC Policy Provider: permission is excluded: "+granted); 351 } 352 } 353 return isExcluded; 354 } 355 356 private boolean doImplies(ProtectionDomain domain, Permission permission) { 357 String contextId = PolicyContext.getContextID(); 358 PolicyConfigurationImpl pci = getPolicyConfigForContext(contextId); 359 Policy appPolicy = getPolicy(pci); 360 361 boolean result = appPolicy.implies(domain,permission); 362 363 if (!result) { 366 if(!(permission instanceof WebResourcePermission) && 367 !(permission instanceof MBeanPermission )) { 368 final String contextId2 = contextId; 369 final Permission permission2 = permission; 370 final ProtectionDomain domain2 = domain; 371 AccessController.doPrivileged(new PrivilegedAction() { 372 public Object run() { 373 logger.info("JACC Policy Provider: PolicyWrapper.implies, context(" 374 + contextId2 375 + ")- permission(" + permission2 376 + ") domain that failed(" +domain2 + ")"); 377 return null; 378 } 379 }); 380 } 381 } else { 382 Permissions excluded = getExcludedPolicy(pci); 383 if (excluded != null) { 384 result = !grantedIsExcluded(permission,excluded); 385 } 386 } 387 388 if (!result && logger.isLoggable(Level.FINEST)){ 390 logger.finest("JACC Policy Provider: PolicyWrapper.implies, context ("+ 391 contextId+")- result was("+result+") permission (" 392 +permission+")"); 393 } 394 395 return result; 396 } 397 398 460 private static final String POLICY = "java.security.policy"; 461 private static final String POLICY_URL = "policy.url."; 462 private static final String AUTH_POLICY = "java.security.auth.policy"; 463 private static final String AUTH_POLICY_URL = "auth.policy.url."; 464 465 479 private static final String FORCE_APP_REFRESH_PROP_NAME = 480 "com.sun.enterprise.security.provider.PolicyWrapper.force_app_refresh"; 481 482 487 private static final boolean forceAppRefresh = 488 Boolean.getBoolean(FORCE_APP_REFRESH_PROP_NAME); 489 490 private long refreshTime; 491 492 private synchronized boolean defaultContextChanged() { 493 494 if (forceAppRefresh) { 495 return true; 496 } 497 498 long newTime = getTimeStamp(POLICY,POLICY_URL); 499 newTime += getTimeStamp(AUTH_POLICY,AUTH_POLICY_URL); 500 boolean rvalue = refreshTime != newTime; 501 refreshTime = newTime; 502 return rvalue; 503 } 504 505 private static long 506 getTimeStamp(final String propname, final String urlname) { 507 Long l = (Long ) AccessController.doPrivileged(new PrivilegedAction() { 508 public Long run() { 509 long sum = 0L; 510 boolean allowSystemProperties = "true".equalsIgnoreCase 511 (Security.getProperty("policy.allowSystemProperty")); 512 if (allowSystemProperties) { 513 String extra_policy = System.getProperty(propname); 514 if (extra_policy != null) { 515 boolean overrideAll = false; 516 if (extra_policy.startsWith("=")) { 517 overrideAll = true; 518 extra_policy = extra_policy.substring(1); 519 } 520 try { 521 String path =PropertyExpander.expand(extra_policy); 522 File policyFile = new File(path); 523 boolean found = policyFile.exists(); 524 if (!found) { 525 URL policy_url = new URL (path); 526 if ("file".equals(policy_url.getProtocol())) { 527 path = policy_url.getFile(). 528 replace('/', File.separatorChar); 529 path = sun.net.www.ParseUtil.decode(path); 530 policyFile = new File(path); 531 found = policyFile.exists(); 532 } 533 } 534 if (found) { 535 sum += policyFile.lastModified(); 536 if (logger.isLoggable(Level.FINE)) { 537 logMsg(Level.FINE,"pc.file_refreshed", 538 new Object []{path},null); 539 } 540 } else { 541 if (logger.isLoggable(Level.FINE)) { 542 logMsg(Level.FINE,"pc.file_not_refreshed", 543 new Object []{path},null); 544 } 545 } 546 } catch (Exception e) { 547 } 549 if (overrideAll) { 550 return new Long (sum); 551 } 552 } 553 } 554 int n = 1; 555 String policy_uri; 556 while ((policy_uri = Security.getProperty(urlname+n)) != null){ 557 try { 558 URL policy_url = null; 559 String expanded_uri = PropertyExpander.expand 560 (policy_uri).replace(File.separatorChar, '/'); 561 if (policy_uri.startsWith("file:${java.home}/") || 562 policy_uri.startsWith("file:${user.home}/")) { 563 policy_url = new File 568 (expanded_uri.substring(5)).toURI().toURL(); 569 } else { 570 policy_url = new URI (expanded_uri).toURL(); 571 } 572 if ("file".equals(policy_url.getProtocol())) { 573 String path = policy_url.getFile(). 574 replace('/', File.separatorChar); 575 path = sun.net.www.ParseUtil.decode(path); 576 File policyFile = new File(path); 577 if (policyFile.exists()) { 578 sum += policyFile.lastModified(); 579 if (logger.isLoggable(Level.FINE)) { 580 logMsg(Level.FINE,"pc.file_refreshed", 581 new Object []{path},null); 582 } 583 } else { 584 if (logger.isLoggable(Level.FINE)) { 585 logMsg(Level.FINE,"pc.file_not_refreshed", 586 new Object []{path},null); 587 } 588 } 589 } else { 590 if (logger.isLoggable(Level.FINE)) { 591 logMsg(Level.FINE,"pc.file_not_refreshed", 592 new Object []{policy_url},null); 593 } 594 } 595 } catch (Exception e) { 596 } 598 n++; 599 } 600 return new Long (sum); 601 } 602 }); 603 return l.longValue(); 604 } 605 } 606 | Popular Tags |