1 25 26 package org.objectweb.jonas_lib.security.jacc; 27 28 import java.net.SocketPermission ; 29 import java.security.CodeSource ; 30 import java.security.Permission ; 31 import java.security.PermissionCollection ; 32 import java.security.Policy ; 33 import java.security.Principal ; 34 import java.security.ProtectionDomain ; 35 36 import javax.security.jacc.EJBMethodPermission ; 37 import javax.security.jacc.EJBRoleRefPermission ; 38 import javax.security.jacc.PolicyConfiguration ; 39 import javax.security.jacc.PolicyConfigurationFactory ; 40 import javax.security.jacc.PolicyContext ; 41 import javax.security.jacc.PolicyContextException ; 42 import javax.security.jacc.WebResourcePermission ; 43 import javax.security.jacc.WebRoleRefPermission ; 44 import javax.security.jacc.WebUserDataPermission ; 45 46 import org.objectweb.common.TraceCore; 47 48 import org.objectweb.jonas_lib.I18n; 49 50 import org.objectweb.util.monolog.api.BasicLevel; 51 import org.objectweb.util.monolog.api.Logger; 52 53 68 public class JPolicy extends Policy { 69 70 73 private static Logger logger = null; 74 75 78 private static JPolicy unique = null; 79 80 83 private static Policy initialPolicy = null; 84 85 88 private static I18n i18n = I18n.getInstance(JPolicy.class); 89 90 95 private static PolicyConfigurationFactory policyConfigurationFactory = null; 96 97 98 102 public JPolicy() { 103 initialPolicy = Policy.getPolicy(); 105 106 logger = TraceCore.sec; 108 } 109 110 114 private void initPolicyConfigurationFactory() throws JPolicyException { 115 try { 117 policyConfigurationFactory = PolicyConfigurationFactory.getPolicyConfigurationFactory(); 118 } catch (ClassNotFoundException cnfe) { 119 throw new JPolicyException("PolicyConfigurationFactory class implementation was not found : '" + cnfe.getMessage() + "'."); 120 } catch (PolicyContextException pce) { 121 throw new JPolicyException("PolicyContextException in PolicyConfigurationFactory : '" + pce.getMessage() + "'."); 122 } 123 124 } 125 126 127 131 public static JPolicy getInstance() { 132 if (unique == null) { 133 unique = new JPolicy(); 134 } 135 return unique; 136 } 137 138 139 140 146 147 155 public boolean implies(ProtectionDomain domain, Permission permission) { 156 160 if (permission instanceof RuntimePermission || permission instanceof SocketPermission ) { 161 return initialPolicy.implies(domain, permission); 162 } 163 164 165 String contextID = PolicyContext.getContextID(); 167 if (contextID == null) { 169 return initialPolicy.implies(domain, permission); 170 } 171 172 if (!(permission instanceof EJBMethodPermission || permission instanceof EJBRoleRefPermission 173 || permission instanceof WebUserDataPermission || permission instanceof WebRoleRefPermission 174 || permission instanceof WebResourcePermission )) { 175 return initialPolicy.implies(domain, permission); 176 } 177 178 179 180 181 if (logger.isLoggable(BasicLevel.DEBUG)) { 182 logger.log(BasicLevel.DEBUG, "!= null, Permission being checked = " + permission); 183 } 184 185 try { 187 if (policyConfigurationFactory == null) { 188 initPolicyConfigurationFactory(); 189 } 190 191 if (!policyConfigurationFactory.inService(contextID)) { 192 if (logger.isLoggable(BasicLevel.DEBUG)) { 193 logger.log(BasicLevel.DEBUG, "Not in service, return false"); 194 } 195 return false; 196 } 197 } catch (JPolicyException jpe) { 198 if (logger.isLoggable(BasicLevel.ERROR)) { 199 logger.log(BasicLevel.ERROR, i18n.getMessage("JPolicy.implies.canNotCheck", jpe.getMessage())); 200 } 201 return false; 202 } catch (PolicyContextException pce) { 203 if (logger.isLoggable(BasicLevel.ERROR)) { 204 logger.log(BasicLevel.ERROR, i18n.getMessage("JPolicy.implies.canNotCheck", pce.getMessage())); 205 } 206 return false; 207 } 208 209 JPolicyConfiguration jPolicyConfiguration = null; 210 try { 211 PolicyConfiguration pc = policyConfigurationFactory.getPolicyConfiguration(contextID, false); 212 if (pc instanceof JPolicyConfiguration) { 213 jPolicyConfiguration = (JPolicyConfiguration) pc; 214 } else { 215 jPolicyConfiguration = JPolicyConfigurationKeeper.getConfiguration(contextID); 217 if (jPolicyConfiguration == null) { 218 throw new RuntimeException ("This policy provider can only manage JPolicyConfiguration objects"); 219 } 220 } 221 } catch (PolicyContextException pce) { 222 if (logger.isLoggable(BasicLevel.ERROR)) { 223 logger.log(BasicLevel.ERROR, i18n.getMessage("JPolicy.implies.canNotRetrieve", contextID, pce.getMessage())); 224 } 225 return false; 226 } 227 228 234 PermissionCollection excludedPermissions = jPolicyConfiguration.getExcludedPermissions(); 235 PermissionCollection uncheckedPermissions = jPolicyConfiguration.getUncheckedPermissions(); 236 237 if (logger.isLoggable(BasicLevel.DEBUG)) { 238 logger.log(BasicLevel.DEBUG, "Check permission"); 239 logger.log(BasicLevel.DEBUG, "Excluded permissions = " + excludedPermissions); 240 logger.log(BasicLevel.DEBUG, "unchecked permissions = " + uncheckedPermissions); 241 } 242 243 if (excludedPermissions.implies(permission)) { 245 if (logger.isLoggable(BasicLevel.DEBUG)) { 246 logger.log(BasicLevel.DEBUG, "Permission '" + permission + "' is excluded, return false"); 247 } 248 return false; 249 } else if (uncheckedPermissions.implies(permission)) { if (logger.isLoggable(BasicLevel.DEBUG)) { 251 logger.log(BasicLevel.DEBUG, "Permission '" + permission + "' is unchecked, return true"); 252 } 253 return true; 254 } else { 255 if (domain.getPrincipals().length > 0) { 257 if (logger.isLoggable(BasicLevel.DEBUG)) { 258 logger.log(BasicLevel.DEBUG, "There are principals, checking principals..."); 259 } 260 return isImpliedPermissionForPrincipals(jPolicyConfiguration, permission, domain.getPrincipals()); 262 } else { 263 if (logger.isLoggable(BasicLevel.DEBUG)) { 264 logger.log(BasicLevel.DEBUG, "Principals length = 0, there is no principal on this domain"); 265 } 266 if (logger.isLoggable(BasicLevel.DEBUG)) { 268 logger.log(BasicLevel.DEBUG, "Permission '" + permission + "' not found, return false"); 269 } 270 return false; 271 } 272 } 273 } 274 275 276 286 public PermissionCollection getPermissions(ProtectionDomain domain) { 287 288 return initialPolicy.getPermissions(domain); 290 291 303 } 304 305 306 319 public PermissionCollection getPermissions(CodeSource codeSource) { 320 321 return initialPolicy.getPermissions(codeSource); 323 324 } 333 334 335 338 public void refresh() { 339 initialPolicy.refresh(); 340 } 341 342 343 344 351 private boolean isImpliedPermissionForPrincipals(JPolicyConfiguration jPolicyConfiguration, Permission permission, Principal [] principals) { 352 PermissionCollection permissions = null; 356 int i = 0; 357 boolean implied = false; 358 while (i < principals.length && !implied) { 360 if (logger.isLoggable(BasicLevel.DEBUG)) { 361 logger.log(BasicLevel.DEBUG, "Checking permission '" + permission + "' with permissions of Principal '" + principals[i].getName() + "'."); 362 } 363 permissions = jPolicyConfiguration.getPermissionsForPrincipal(principals[i]); 364 365 if (permissions.implies(permission)) { 366 if (logger.isLoggable(BasicLevel.DEBUG)) { 367 logger.log(BasicLevel.DEBUG, "Permission implied with principal '" + principals[i].getName() + "'."); 368 } 369 implied = true; 370 } 371 i++; 372 } 373 if (logger.isLoggable(BasicLevel.DEBUG)) { 374 if (!implied) { 375 logger.log(BasicLevel.DEBUG, "Permission '" + permission + "' was not found in each permissions of the given roles, return false"); 376 } 377 } 378 return implied; 379 } 380 381 } 382 383 | Popular Tags |