1 7 package org.jboss.security.jacc; 8 9 import java.security.CodeSource ; 10 import java.security.Permission ; 11 import java.security.PermissionCollection ; 12 import java.security.Policy ; 13 import java.security.ProtectionDomain ; 14 import java.util.Iterator ; 15 import java.util.Enumeration ; 16 import javax.security.jacc.EJBMethodPermission ; 17 import javax.security.jacc.EJBRoleRefPermission ; 18 import javax.security.jacc.PolicyConfiguration ; 19 import javax.security.jacc.PolicyContext ; 20 import javax.security.jacc.PolicyContextException ; 21 import javax.security.jacc.WebResourcePermission ; 22 import javax.security.jacc.WebRoleRefPermission ; 23 import javax.security.jacc.WebUserDataPermission ; 24 import javax.security.auth.Subject ; 25 26 import org.jboss.logging.Logger; 27 import EDU.oswego.cs.dl.util.concurrent.ConcurrentReaderHashMap; 28 29 36 public class DelegatingPolicy extends Policy 37 { 38 private static Logger log = Logger.getLogger(DelegatingPolicy.class); 39 private static DelegatingPolicy instance; 40 41 44 private Policy delegate; 45 49 private ConcurrentReaderHashMap activePolicies = new ConcurrentReaderHashMap(); 50 54 private ConcurrentReaderHashMap openPolicies = new ConcurrentReaderHashMap(); 55 58 private boolean trace; 59 62 private PolicyProxy policyProxy = new PolicyProxy(this); 63 64 public synchronized static DelegatingPolicy getInstance() 65 { 66 if (instance == null) 67 { 68 instance = new DelegatingPolicy(); 69 } 70 return instance; 71 } 72 73 public DelegatingPolicy() 74 { 75 this(null); 76 } 77 78 public DelegatingPolicy(Policy delegate) 79 { 80 if (delegate == null) 81 delegate = Policy.getPolicy(); 82 this.delegate = delegate; 83 this.trace = log.isTraceEnabled(); 84 if (instance == null) 85 instance = this; 86 98 Permission permission = new RuntimePermission ("test"); 99 boolean loadedPerms = !(permission instanceof EJBMethodPermission 100 || permission instanceof EJBRoleRefPermission 101 || permission instanceof WebResourcePermission 102 || permission instanceof WebRoleRefPermission 103 || permission instanceof WebUserDataPermission ); 104 if (trace) 105 log.trace("Loaded JACC permissions: " + loadedPerms); 106 Class c = PolicyContext .class; 107 } 108 109 public PermissionCollection getPermissions(ProtectionDomain domain) 110 { 111 PermissionCollection pc = super.getPermissions(domain); 112 PermissionCollection delegated = delegate.getPermissions(domain); 113 for (Enumeration e = delegated.elements(); e.hasMoreElements();) 114 { 115 Permission p = (Permission ) e.nextElement(); 116 pc.add(p); 117 } 118 return pc; 119 } 120 121 public boolean implies(ProtectionDomain domain, Permission permission) 122 { 123 boolean isJaccPermission = permission instanceof EJBMethodPermission 124 || permission instanceof EJBRoleRefPermission 125 || permission instanceof WebResourcePermission 126 || permission instanceof WebRoleRefPermission 127 || permission instanceof WebUserDataPermission ; 128 if (trace) 129 { 130 log.trace("implies, domain=" + domain + ", permission=" + permission 131 + ", isJaccPermission=" + isJaccPermission); 132 try 133 { 134 Subject caller = (Subject ) PolicyContext.getContext("javax.security.auth.Subject.container"); 135 log.trace("implies javax.security.auth.Subject.container: "+caller); 136 } 137 catch(Throwable e) 138 { 139 log.trace("Failed to access Subject context", e); 140 } 141 } 142 boolean implied = false; 143 if (isJaccPermission == false) 144 { 145 implied = delegate.implies(domain, permission); 147 } 148 else 149 { 150 String contextID = PolicyContext.getContextID(); 151 ContextPolicy contextPolicy = (ContextPolicy) activePolicies.get(contextID); 152 if (contextPolicy != null) 153 implied = contextPolicy.implies(domain, permission); 154 else if (trace) 155 log.trace("No PolicyContext found for contextID=" + contextID); 156 } 157 if (trace) 158 { 159 log.trace("implied=" + implied); 160 } 161 return implied; 162 } 163 164 176 public PermissionCollection getPermissions(CodeSource cs) 177 { 178 PermissionCollection pc = null; 179 String contextID = PolicyContext.getContextID(); 180 if (contextID == null) 181 { 182 pc = delegate.getPermissions(cs); 183 } 184 else 185 { 186 ContextPolicy policy = (ContextPolicy) activePolicies.get(contextID); 187 if (policy != null) 188 { 189 pc = policy.getPermissions(); 190 } 191 else 192 { 193 pc = delegate.getPermissions(cs); 194 } 195 } 196 return pc; 197 } 198 199 204 public void refresh() 205 { 206 207 } 208 209 212 public Policy getPolicyProxy() 213 { 214 return policyProxy; 215 } 216 217 219 223 public String listContextPolicies() 224 { 225 StringBuffer tmp = new StringBuffer ("<ActiveContextPolicies>"); 226 Iterator iter = activePolicies.keySet().iterator(); 227 while (iter.hasNext()) 228 { 229 String contextID = (String ) iter.next(); 230 ContextPolicy cp = (ContextPolicy) activePolicies.get(contextID); 231 tmp.append(cp); 232 tmp.append('\n'); 233 } 234 tmp.append("</ActiveContextPolicies>"); 235 236 tmp.append("<OpenContextPolicies>"); 237 iter = openPolicies.keySet().iterator(); 238 while (iter.hasNext()) 239 { 240 String contextID = (String ) iter.next(); 241 ContextPolicy cp = (ContextPolicy) openPolicies.get(contextID); 242 tmp.append(cp); 243 tmp.append('\n'); 244 } 245 tmp.append("</OpenContextPolicies>"); 246 247 return tmp.toString(); 248 } 249 250 synchronized ContextPolicy getContextPolicy(String contextID) 251 throws PolicyContextException 252 { 253 ContextPolicy policy = (ContextPolicy) openPolicies.get(contextID); 254 if (policy == null) 255 throw new PolicyContextException ("No ContextPolicy exists for contextID=" + contextID); 256 return policy; 257 } 258 259 267 synchronized void initPolicyConfiguration(String contextID, boolean remove) 268 throws PolicyContextException 269 { 270 ContextPolicy policy = (ContextPolicy) activePolicies.remove(contextID); 272 if( policy == null ) 273 policy = (ContextPolicy) openPolicies.get(contextID); 274 if (policy == null) 275 { 276 policy = new ContextPolicy(contextID); 277 } 278 openPolicies.put(contextID, policy); 280 if (remove == true) 281 policy.clear(); 282 } 283 284 void addToExcludedPolicy(String contextID, Permission permission) 285 throws PolicyContextException 286 { 287 ContextPolicy policy = getContextPolicy(contextID); 288 policy.addToExcludedPolicy(permission); 289 } 290 291 void addToExcludedPolicy(String contextID, PermissionCollection permissions) 292 throws PolicyContextException 293 { 294 ContextPolicy policy = getContextPolicy(contextID); 295 policy.addToExcludedPolicy(permissions); 296 } 297 298 void addToRole(String contextID, String roleName, Permission permission) 299 throws PolicyContextException 300 { 301 ContextPolicy policy = getContextPolicy(contextID); 302 policy.addToRole(roleName, permission); 303 } 304 305 void addToRole(String contextID, String roleName, PermissionCollection permissions) 306 throws PolicyContextException 307 { 308 ContextPolicy policy = getContextPolicy(contextID); 309 policy.addToRole(roleName, permissions); 310 } 311 312 void addToUncheckedPolicy(String contextID, Permission permission) 313 throws PolicyContextException 314 { 315 ContextPolicy policy = getContextPolicy(contextID); 316 policy.addToUncheckedPolicy(permission); 317 } 318 319 void addToUncheckedPolicy(String contextID, PermissionCollection permissions) 320 throws PolicyContextException 321 { 322 ContextPolicy policy = getContextPolicy(contextID); 323 policy.addToUncheckedPolicy(permissions); 324 } 325 326 void linkConfiguration(String contextID, PolicyConfiguration link) 327 throws PolicyContextException 328 { 329 ContextPolicy policy = getContextPolicy(contextID); 330 ContextPolicy linkPolicy = getContextPolicy(link.getContextID()); 331 policy.linkConfiguration(linkPolicy); 332 } 333 334 342 public void commit(String contextID) 343 throws PolicyContextException 344 { 345 ContextPolicy policy = getContextPolicy(contextID); 346 openPolicies.remove(contextID); 347 activePolicies.put(contextID, policy); 348 policy.commit(); 349 } 350 351 public void delete(String contextID) 352 throws PolicyContextException 353 { 354 ContextPolicy policy = (ContextPolicy) activePolicies.remove(contextID); 355 if( policy == null ) 356 policy = (ContextPolicy) openPolicies.remove(contextID); 357 if( policy != null ) 358 policy.delete(); 359 } 360 361 void removeExcludedPolicy(String contextID) 362 throws PolicyContextException 363 { 364 ContextPolicy policy = getContextPolicy(contextID); 365 policy.removeExcludedPolicy(); 366 } 367 368 void removeRole(String contextID, String roleName) 369 throws PolicyContextException 370 { 371 ContextPolicy policy = getContextPolicy(contextID); 372 policy.removeRole(roleName); 373 } 374 375 void removeUncheckedPolicy(String contextID) 376 throws PolicyContextException 377 { 378 ContextPolicy policy = getContextPolicy(contextID); 379 policy.removeUncheckedPolicy(); 380 } 381 382 386 private static class PolicyProxy extends Policy 387 { 388 private Policy delegate; 389 390 PolicyProxy(Policy delegate) 391 { 392 this.delegate = delegate; 393 } 394 395 public void refresh() 396 { 397 delegate.refresh(); 398 } 399 400 public PermissionCollection getPermissions(CodeSource codesource) 401 { 402 return delegate.getPermissions(codesource); 403 } 404 405 public boolean implies(ProtectionDomain domain, Permission permission) 406 { 407 return delegate.implies(domain, permission); 408 } 409 410 public PermissionCollection getPermissions(ProtectionDomain domain) 411 { 412 return delegate.getPermissions(domain); 413 } 414 } 415 } 416 | Popular Tags |