1 23 package com.sun.enterprise.security; 24 25 import java.lang.*; 26 import java.security.*; 27 import java.lang.reflect.*; 28 import java.io.File ; 29 import java.util.Set ; 30 import java.util.HashSet ; 31 32 import javax.security.auth.Subject ; 33 import javax.security.jacc.*; 34 35 import com.sun.enterprise.deployment.Application; 36 import com.sun.enterprise.SecurityManager; 37 import com.sun.enterprise.security.application.EJBSecurityManager; 38 import com.sun.enterprise.server.pluggable.PluggableFeatureFactoryImpl; 39 import com.sun.enterprise.server.pluggable.SecuritySupport; 40 import com.sun.ejb.Container; 41 import com.sun.ejb.Invocation; 42 import com.sun.enterprise.security.util.IASSecurityException; 43 import com.sun.logging.LogDomains; 44 import com.sun.enterprise.util.LocalStringManagerImpl; 45 import com.sun.logging.*; 46 import java.util.logging.*; 47 import com.sun.web.security.WebSecurityManagerFactory; 48 54 public class SecurityUtil{ 55 56 private static LocalStringManagerImpl localStrings = 57 new LocalStringManagerImpl(SecurityUtil.class); 58 private static Logger _logger = 59 LogDomains.getLogger(LogDomains.SECURITY_LOGGER); 60 public static String VENDOR_PRESENT = 61 "com.sun.enterprise.security.provider.jaccvendorpresent"; 62 private static boolean vendorPresent = Boolean.getBoolean(VENDOR_PRESENT); 63 public static String repository = System.getProperty("com.sun.enterprise.jaccprovider.property.repository"); 66 69 81 public static Object runMethod(Method beanClassMethod, Invocation inv, Object o, Object [] oa, Container c) 82 throws Throwable { 83 84 final Method meth = beanClassMethod; 85 final Object obj = o; 86 final Object [] objArr = oa; 87 Object ret; 88 EJBSecurityManager mgr = (EJBSecurityManager) c.getSecurityManager(); 89 if (mgr == null) { 90 throw new SecurityException ("SecurityManager not set"); 91 } 92 93 if((inv.isLocal && mgr.getUsesCallerIdentity()) || 99 System.getSecurityManager() == null) { 100 ret = mgr.runMethod(meth, obj, objArr); 101 } else { 102 try { 103 PrivilegedExceptionAction pea = 104 new PrivilegedExceptionAction(){ 105 public java.lang.Object run() throws Exception { 106 return meth.invoke(obj, objArr); 107 } 108 }; 109 110 ret = mgr.doAsPrivileged(pea); 111 } catch(PrivilegedActionException pae) { 112 Throwable cause = pae.getCause(); 113 if( cause instanceof InvocationTargetException ) { 114 cause = ((InvocationTargetException) cause).getCause(); 115 } 116 throw cause; 117 } 118 } 119 return ret; 120 } 121 138 public static Object invoke(Method beanClassMethod, Invocation inv, Object o, Object [] oa, Container c, 139 SecurityManager mgr) throws Throwable { 140 141 final Method meth = beanClassMethod; 142 final Object obj = o; 143 final Object [] objArr = oa; 144 Object ret = null; 145 EJBSecurityManager ejbSecMgr = null; 146 147 if(mgr == null) { 148 if (c != null) { 149 ejbSecMgr = (EJBSecurityManager) c.getSecurityManager(); 150 } 151 if (ejbSecMgr == null) { 152 throw new SecurityException ("SecurityManager not set"); 153 } 154 } else { 155 ejbSecMgr = (EJBSecurityManager) mgr; 156 } 157 158 if((inv.isLocal && ejbSecMgr.getUsesCallerIdentity()) || 164 System.getSecurityManager() == null) { 165 ret = ejbSecMgr.runMethod(meth, obj, objArr); 166 } else { 167 168 PrivilegedExceptionAction pea = 169 new PrivilegedExceptionAction(){ 170 public java.lang.Object run() throws Exception { 171 return meth.invoke(obj, objArr); 172 } 173 }; 174 175 try { 176 ret = ejbSecMgr.doAsPrivileged(pea); 177 } catch(PrivilegedActionException pae) { 178 Throwable cause = pae.getCause(); 179 throw cause; 180 } 181 } 182 return ret; 183 } 184 185 198 public static void generatePolicyFile(String name) throws IASSecurityException { 199 200 assert name != null; 201 202 if (name == null) { 203 throw new IASSecurityException("Invalid Module Name"); 204 } 205 206 try { 207 208 boolean inService = 209 PolicyConfigurationFactory.getPolicyConfigurationFactory(). 210 inService(name); 211 212 if (!inService) { 213 214 217 223 224 PolicyConfigurationFactory pcf = 225 PolicyConfigurationFactory.getPolicyConfigurationFactory(); 226 PolicyConfiguration pc = 227 pcf.getPolicyConfiguration(name, false); 228 229 pc.commit(); 230 231 if (_logger.isLoggable(Level.FINE)){ 232 _logger.fine("JACC: committed policy for context: "+name); 233 } 234 } 235 236 Policy.getPolicy().refresh(); 237 238 } catch(java.lang.ClassNotFoundException cnfe){ 239 String msg = localStrings.getLocalString("enterprise.security.securityutil.classnotfound","Could not find PolicyConfigurationFactory class. Check javax.security.jacc.PolicyConfigurationFactory.provider property"); 240 throw new IASSecurityException(msg); 241 } catch(javax.security.jacc.PolicyContextException pce){ 242 throw new IASSecurityException(pce.toString()); 243 } 244 } 245 246 255 public static void removePolicy(String name) throws IASSecurityException { 256 257 assert name != null; 258 259 if (name == null) { 260 throw new IASSecurityException("Invalid Module Name"); 261 } 262 263 try { 264 265 boolean wasInService = 266 PolicyConfigurationFactory.getPolicyConfigurationFactory(). 267 inService(name); 268 269 271 PolicyConfiguration pc = 272 PolicyConfigurationFactory.getPolicyConfigurationFactory(). 273 getPolicyConfiguration(name, false); 274 275 pc.delete(); 276 WebSecurityManagerFactory.getInstance().removeWebSecurityManager(name); 278 279 281 if (wasInService) { 282 Policy.getPolicy().refresh(); 283 } 284 285 } catch(java.lang.ClassNotFoundException cnfe){ 286 String msg = localStrings.getLocalString("enterprise.security.securityutil.classnotfound","Could not find PolicyConfigurationFactory class. Check javax.security.jacc.PolicyConfigurationFactory.provider property"); 287 throw new IASSecurityException(msg); 288 } catch(javax.security.jacc.PolicyContextException pce){ 289 throw new IASSecurityException(pce.toString()); 290 } 291 } 292 308 public static boolean linkPolicyFile(String name, String linkName, boolean lastInService) 309 throws IASSecurityException { 310 311 boolean rvalue = lastInService; 312 313 assert name != null; 314 315 if (name == null) { 316 throw new IASSecurityException("Invalid Module Name"); 317 } 318 try { 319 PolicyConfigurationFactory pcf = PolicyConfigurationFactory.getPolicyConfigurationFactory(); 320 boolean inService = pcf.inService(name); 321 322 if (linkName == null) { 323 rvalue = inService; 324 } else if (inService == lastInService) { 325 326 if (!inService) { 328 329 332 PolicyConfiguration pc = 333 pcf.getPolicyConfiguration(name, false); 334 PolicyConfiguration linkPc = 335 pcf.getPolicyConfiguration(linkName, false); 336 pc.linkConfiguration(linkPc); 337 } 338 } else { 339 throw new IASSecurityException("Inconsistent Module State"); 340 } 341 342 } catch(java.lang.ClassNotFoundException cnfe){ 343 String msg = localStrings.getLocalString("enterprise.security.securityutil.classnotfound","Could not find PolicyConfigurationFactory class. Check javax.security.jacc.PolicyConfigurationFactory.provider property"); 344 throw new IASSecurityException(msg); 345 } catch(javax.security.jacc.PolicyContextException pce){ 346 throw new IASSecurityException(pce.toString()); 347 } 348 return rvalue; 349 } 350 351 352 355 public static SecuritySupport getSecuritySupport() { 356 return PluggableFeatureFactoryImpl.getFactory().getSecuritySupport(); 357 } 358 } 359 | Popular Tags |