1 23 24 package com.sun.enterprise.security.acl; 25 26 import java.util.*; 27 import java.lang.reflect.Constructor ; 28 import java.security.Principal ; 29 import java.security.AccessController ; 30 import java.security.PrivilegedAction ; 31 import com.sun.enterprise.deployment.Role; 32 import com.sun.enterprise.deployment.interfaces.SecurityRoleMapper; 33 import com.sun.enterprise.deployment.PrincipalImpl; 34 import com.sun.enterprise.deployment.Group; 35 import com.sun.enterprise.config.serverbeans.Server; 36 import com.sun.enterprise.config.serverbeans.ServerBeansFactory; 37 import com.sun.enterprise.config.serverbeans.SecurityService; 38 import com.sun.enterprise.config.ConfigContext; 39 import com.sun.enterprise.server.ApplicationServer; 40 import com.sun.enterprise.server.ServerContext; 41 import com.sun.enterprise.Switch; 42 import java.util.logging.*; 43 import com.sun.logging.*; 44 import javax.security.auth.Subject ; 45 46 47 54 public class RoleMapper implements java.io.Serializable , SecurityRoleMapper { 55 56 private static Map ROLEMAPPER = new HashMap(); 57 private static final String DEFAULT_ROLE_NAME = "ANYONE"; 58 private static Role defaultRole = null; 59 private static String defaultRoleName = null; 60 private String appName; 61 private final Map roleToSubject = new HashMap(); 63 private static String defaultP2RMappingClassName = null; 65 private DefaultRoleToSubjectMapping defaultRTSM = 66 new DefaultRoleToSubjectMapping(); 67 68 private final Map roleToPrincipal = new HashMap(); 73 private final Map roleToGroup = new HashMap(); 75 76 private static Logger _logger = 77 LogDomains.getLogger(LogDomains.SECURITY_LOGGER); 78 79 private RoleMapper(String appName) { 80 this.appName = appName; 81 Switch sw = Switch.getSwitch(); 82 if (sw.getContainerType() == Switch.EJBWEB_CONTAINER) { initDefaultRole(); 84 } 85 if (defaultP2RMappingClassName==null) 86 defaultP2RMappingClassName = getDefaultP2RMappingClassName(); 87 } 88 89 private static synchronized void initDefaultRole() { 90 91 if(defaultRole == null) { 92 93 defaultRoleName = DEFAULT_ROLE_NAME; 94 95 try { 96 ConfigContext configContext = 97 ApplicationServer.getServerContext().getConfigContext(); 98 assert(configContext != null); 99 100 Server configBean = 101 ServerBeansFactory.getServerBean(configContext); 102 assert(configBean != null); 103 104 SecurityService securityBean = 105 ServerBeansFactory.getSecurityServiceBean(configContext); 106 assert(securityBean != null); 107 108 defaultRoleName = securityBean.getAnonymousRole(); 109 110 } catch (Exception e) { 111 _logger.log(Level.WARNING, 112 "java_security.anonymous_role_reading_exception", 113 e); 114 } 115 116 if(_logger.isLoggable(Level.FINE)){ 117 _logger.log(Level.FINE, "Default role is: " + defaultRoleName); 118 } 119 defaultRole = new Role(defaultRoleName); 120 } 121 } 122 123 127 public static RoleMapper getRoleMapper(String appName) { 128 RoleMapper r = (RoleMapper)ROLEMAPPER.get(appName); 129 if(r == null){ 130 r = new RoleMapper(appName); 131 synchronized(RoleMapper.class){ 132 ROLEMAPPER.put(appName,r); 133 } 134 } 135 return r; 136 } 137 138 142 public static void setRoleMapper(String appName, SecurityRoleMapper rmap) { 143 synchronized(RoleMapper.class){ 144 ROLEMAPPER.put(appName, rmap); 145 } 146 } 147 148 151 public static void removeRoleMapper(String appName) { 152 153 if( ROLEMAPPER.containsKey(appName)){ 154 synchronized(RoleMapper.class){ 155 ROLEMAPPER.remove(appName); 156 } 157 } 158 } 159 160 163 public String getName() { 164 return appName; 165 } 166 167 170 public void setName(String name) { 171 this.appName = name; 172 } 173 174 178 private void addRoleToPrincipal(final Principal principal, String role){ 179 assert roleToSubject != null; 180 Subject subject = (Subject )roleToSubject.get(role); 181 final Subject sub = (subject == null)?new Subject (): subject; 182 AccessController.doPrivileged(new PrivilegedAction () { 183 public java.lang.Object run() { 184 sub.getPrincipals().add(principal); 185 return null; 186 } 187 }); 188 roleToSubject.put(role, sub); 189 } 190 191 196 public void unassignPrincipalFromRole(Role role, Principal principal){ 197 assert roleToSubject != null; 198 String mrole = role.getName(); 199 final Subject sub = (Subject )roleToSubject.get(mrole); 200 final Principal p = principal; 201 if (sub != null){ 202 AccessController.doPrivileged(new PrivilegedAction () { 203 public java.lang.Object run() { 204 sub.getPrincipals().remove(p); 205 return null; 206 } 207 }); 208 roleToSubject.put(mrole, sub); 209 } 210 Map tmp = null; tmp = (principal instanceof Group)? roleToGroup: roleToPrincipal; 212 Vector v = (Vector)tmp.get(mrole); 213 if(v != null){ 214 v.remove(principal); 215 tmp.put(mrole, v); 216 } 217 } 218 219 static boolean isDefaultRTSMActivated() { 222 return (defaultP2RMappingClassName!=null); 223 } 224 225 229 public Map getRoleToSubjectMapping(){ 230 assert roleToSubject != null; 231 if (roleToSubject.isEmpty() && isDefaultRTSMActivated()) { 232 return defaultRTSM; 233 } 234 return roleToSubject; 235 } 236 237 242 public void assignRole(Principal p, Role r) { 243 String role = r.getName(); 244 Map tmp_map = null; 245 if(_logger.isLoggable(Level.FINE)){ 246 _logger.log(Level.FINE, "SECURITY:RoleMapper Assigning Role "+ role + 247 " to "+ p.getName()); 248 } 249 addRoleToPrincipal(p, role); 250 tmp_map = (p instanceof Group)? roleToGroup:roleToPrincipal; 252 Vector _principals = (Vector)tmp_map.get(role) ; 253 if(_principals == null) { 254 _principals = new Vector(); 255 } 256 _principals.add(p); 257 tmp_map.put(role, _principals); 258 } 259 260 263 public Iterator getRoles() { 264 assert roleToSubject != null; 265 return roleToSubject.keySet().iterator(); } 267 268 272 public Enumeration getGroupsAssignedTo(Role r) { 273 assert roleToGroup != null; 274 Vector v = (Vector)roleToGroup.get(r.getName()); 275 v = (v == null)? new Vector(): v; 276 return v.elements(); 277 } 278 279 283 public Enumeration getUsersAssignedTo(Role r) { 284 assert roleToPrincipal != null; 285 Vector v = (Vector)roleToPrincipal.get(r.getName()); 286 v = (v == null)? new Vector(): v; 287 return v.elements(); 288 } 289 290 public void unassignRole(Role r) { 291 if (r != null){ 292 String role = r.getName(); 293 roleToSubject.remove(role); 294 roleToPrincipal.remove(role); 295 roleToGroup.remove(role); 296 } 297 } 298 299 300 303 public String toString() { 304 305 StringBuffer s = new StringBuffer ("RoleMapper:"); 306 for (Iterator e = this.getRoles(); e.hasNext();) { 307 String r = (String ) e.next(); 308 s.append("\n\tRole (" + r + ") has Principals("); 309 Subject sub = (Subject )roleToSubject.get(r); 310 Iterator it = sub.getPrincipals().iterator(); 311 for(; it.hasNext(); ){ 312 Principal p = (Principal ) it.next(); 313 s.append(p.getName()+" "); 314 } 315 s.append(")"); 316 } 317 if(_logger.isLoggable(Level.FINER)){ 318 _logger.log(Level.FINER,s.toString()); 319 } 320 return s.toString(); 321 } 322 323 324 327 public RoleMapper(RoleMapper r){ 328 this.appName = r.getName(); 329 for(Iterator it = r.getRoles();it.hasNext(); ){ 330 String role = (String ) it.next(); 331 Enumeration groups = r.getGroupsAssignedTo(new Role(role)); 333 Vector groupsToRole = new Vector(); 334 for(;groups.hasMoreElements(); ){ 335 Group gp = (Group) groups.nextElement(); 336 groupsToRole.add(new Group(gp.getName())); 337 addRoleToPrincipal(gp,role); 338 } 339 this.roleToGroup.put(role,groupsToRole); 340 341 Enumeration users = r.getUsersAssignedTo(new Role(role)); 343 Vector usersToRole = new Vector(); 344 for(;users.hasMoreElements(); ){ 345 PrincipalImpl gp = (PrincipalImpl) users.nextElement(); 346 usersToRole.add(new PrincipalImpl(gp.getName())); 347 addRoleToPrincipal(gp,role); 348 } 349 this.roleToPrincipal.put(role,usersToRole); 350 } 351 } 352 353 357 static private String getDefaultP2RMappingClassName() { 358 if (defaultP2RMappingClassName != null ) 359 return defaultP2RMappingClassName; 360 361 String className=null; 362 try { 363 ServerContext serverContext = ApplicationServer.getServerContext(); 364 if (serverContext != null) { 365 ConfigContext configContext = serverContext.getConfigContext(); 366 if (configContext != null) { 367 SecurityService securityService = 368 ServerBeansFactory.getSecurityServiceBean(configContext); 369 if (securityService != null && 370 securityService.isActivateDefaultPrincipalToRoleMapping()==true) { 371 className = securityService.getMappedPrincipalClass(); 372 if (className==null || "".equals(className)) 373 className = "com.sun.enterprise.deployment.Group"; 374 } 375 } 376 } 377 if (className==null) 378 return null; 379 Class clazz = Class.forName(className); 380 Class [] argClasses = new Class [] { String .class }; 381 Object [] arg = new Object [] { "anystring" }; 382 Constructor c = clazz.getConstructor(argClasses); 383 Principal principal = (Principal ) c.newInstance(arg); 384 return className; 386 } catch (Exception e) { 387 _logger.log(Level.SEVERE,"pc.getDefaultP2RMappingClass: " + e); 388 return null; 389 } 390 } 391 392 class DefaultRoleToSubjectMapping extends HashMap { 393 private HashMap roleMap = new HashMap(); 394 395 DefaultRoleToSubjectMapping() { 396 super(); 397 } 398 399 Principal getSameNamedPrincipal(String roleName) { 400 Subject subject = new Subject (); 401 try { 402 Class clazz = Class.forName(defaultP2RMappingClassName); 403 Class [] argClasses = new Class [] { String .class }; 404 Object [] arg = new Object [] { new String (roleName) }; 405 Constructor c = clazz.getConstructor(argClasses); 406 Principal principal = (Principal ) c.newInstance(arg); 407 return principal; 408 } catch (Exception e) { 409 _logger.log(Level.SEVERE,"rm.getSameNamedPrincipal", new Object []{roleName, e}); 410 throw new RuntimeException ("Unable to get principal by default p2r mapping"); 411 } 412 } 413 414 public Object get(Object key) { 415 assert key instanceof String ; 416 synchronized (roleMap) { 417 Subject s = (Subject )roleMap.get(key); 418 if (s == null && key instanceof String ) { 419 final Subject fs = new Subject (); 420 final String roleName = (String ) key; 421 AccessController.doPrivileged(new PrivilegedAction () { 422 public java.lang.Object run() { 423 fs.getPrincipals().add(getSameNamedPrincipal(roleName)); 424 return null; 425 } 426 }); 427 roleMap.put(key,fs); 428 s = fs; 429 } 430 return s; 431 } 432 } 433 } 434 } 435 | Popular Tags |