1 26 27 package org.objectweb.jonas.security.realm.factory; 28 29 import java.security.NoSuchAlgorithmException ; 30 import java.util.Enumeration ; 31 import java.util.Hashtable ; 32 import java.util.ArrayList ; 33 34 import javax.naming.BinaryRefAddr ; 35 import javax.naming.NamingException ; 36 import javax.naming.Reference ; 37 import javax.naming.StringRefAddr ; 38 39 import org.objectweb.util.monolog.api.BasicLevel; 40 import org.objectweb.jonas.common.JNDIUtils; 41 import org.objectweb.jonas.jmx.JonasObjectName; 42 import org.objectweb.jonas.security.realm.lib.XML; 43 import org.objectweb.jonas.security.realm.principals.User; 44 import org.objectweb.jonas.security.realm.principals.Group; 45 import org.objectweb.jonas.security.realm.principals.Role; 46 import org.objectweb.jonas.security.realm.lib.HashHelper; 47 48 52 public class JResourceMemory extends JResource implements JResourceMemoryMBean { 53 54 57 private Hashtable groups = new Hashtable (); 58 59 62 private Hashtable roles = new Hashtable (); 63 64 67 private static final String FACTORY_TYPE = "org.objectweb.jonas.security.realm.factory.JResourceMemory"; 68 69 72 private static final String FACTORY_NAME = "org.objectweb.jonas.security.realm.factory.JResourceMemoryFactory"; 73 74 78 public JResourceMemory() throws Exception { 79 super(); 80 } 81 82 87 public void addUser(User user) throws Exception { 88 89 if (getUsers().get(user.getName()) != null) { 90 throw new Exception ("User " + user.getName() + " already exists."); 91 } 92 93 String [] userGroups = user.getArrayGroups(); 95 String groupName = null; 96 for (int g = 0; g < userGroups.length; g++) { 97 groupName = userGroups[g]; 98 if (!groups.containsKey(groupName)) { 99 addGroup(new Group(groupName)); 100 } 101 } 102 103 String [] userRoles = user.getArrayRoles(); 105 String roleName = null; 106 for (int g = 0; g < userRoles.length; g++) { 107 roleName = userRoles[g]; 108 if (!roles.containsKey(roleName)) { 109 addRole(new Role(roleName)); 110 } 111 } 112 113 getUsers().put(user.getName(), user); 115 116 String userName = user.getName(); 118 try { 119 getMBeanServer().registerMBean(user, JonasObjectName.user(getName(), userName)); 121 } catch (Exception e) { 122 getLogger().log(BasicLevel.ERROR, "Cannot register user '" + userName + "' in JMX server : " + e.getMessage()); 123 } 124 } 125 126 131 public void addGroup(Group group) throws Exception { 132 133 if (groups.get(group.getName()) != null) { 134 throw new Exception ("Group " + group.getName() + " already exists."); 135 } 136 137 String [] groupRoles = group.getArrayRoles(); 139 String roleName = null; 140 for (int g = 0; g < groupRoles.length; g++) { 141 roleName = groupRoles[g]; 142 if (!roles.containsKey(roleName)) { 143 addRole(new Role(roleName)); 144 } 145 } 146 147 groups.put(group.getName(), group); 149 150 String groupName = group.getName(); 152 try { 153 getMBeanServer().registerMBean(group, JonasObjectName.group(getName(), groupName)); 155 } catch (Exception e) { 156 getLogger().log(BasicLevel.ERROR, "Cannot register group '" + groupName + "' in JMX server : " + e.getMessage()); 157 } 158 } 159 160 165 public void addRole(Role role) throws Exception { 166 167 if (roles.get(role.getName()) != null) { 168 throw new Exception ("Role " + role.getName() + " already exists."); 169 } 170 171 roles.put(role.getName(), role); 173 174 String roleName = role.getName(); 176 try { 177 getMBeanServer().registerMBean(role, JonasObjectName.role(getName(), roleName)); 179 } catch (Exception e) { 180 getLogger().log(BasicLevel.ERROR, "Cannot register role '" + roleName + "' in JMX server : " + e.getMessage()); 181 } 182 } 183 184 190 public User findUser(String name) throws JResourceException { 191 if (name == null) { 192 return null; 193 } 194 return ((User) getUsers().get(name)); 195 } 196 197 203 public boolean isValidUser(User user, String credentials) { 204 205 boolean validated = false; 206 207 String pass = user.getHashPassword().getPassword(); 209 String algo = user.getHashPassword().getAlgorithm(); 210 211 if (algo != null) { 213 try { 214 validated = HashHelper.hashPassword(credentials, algo).equalsIgnoreCase(pass); 215 } catch (NoSuchAlgorithmException nsae) { 216 getLogger().log(BasicLevel.ERROR, "Can't make a password with the algorithm " + algo + ". " 217 + nsae.getMessage()); 218 } 219 } else { 220 validated = credentials.equals(pass); 222 } 223 return validated; 224 } 225 226 230 public Hashtable getGroups() { 231 return groups; 232 } 233 234 238 public Hashtable getRoles() { 239 return roles; 240 } 241 242 248 public ArrayList getArrayListCombinedRoles(User user) throws JResourceException { 249 ArrayList allCombinedRoles = new ArrayList (); 250 251 if (user == null) { 253 return allCombinedRoles; 254 } 255 256 String [] userRoles = user.getArrayRoles(); 258 for (int r = 0; r < userRoles.length; r++) { 259 String roleName = userRoles[r]; 260 if (!allCombinedRoles.contains(roleName)) { 261 allCombinedRoles.add(roleName); 262 } 263 } 264 265 String [] userGroups = user.getArrayGroups(); 267 for (int g = 0; g < userGroups.length; g++) { 268 String groupName = userGroups[g]; 269 270 Group group = (Group) groups.get(groupName); 272 if (group == null) { 273 continue; 274 } 275 276 String [] groupRoles = group.getArrayRoles(); 277 for (int gr = 0; gr < groupRoles.length; gr++) { 278 String roleName = groupRoles[gr]; 279 if (!allCombinedRoles.contains(roleName)) { 280 allCombinedRoles.add(roleName); 281 } 282 } 283 } 284 285 return allCombinedRoles; 286 } 287 288 292 public void setGroups(Hashtable groups) { 293 this.groups = groups; 294 } 295 296 300 public void setRoles(Hashtable roles) { 301 this.roles = roles; 302 } 303 304 310 public void addUser(String username, String password) throws Exception { 311 addUser(new User(username, password)); 312 } 313 314 319 public void addGroup(String groupname) throws Exception { 320 addGroup(new Group(groupname)); 321 } 322 323 328 public void addRole(String rolename) throws Exception { 329 addRole(new Role(rolename)); 330 } 331 332 337 public void removeUser(String username) throws Exception { 338 if (getUsers().get(username) == null) { 339 throw new Exception ("Can not remove user " + username + ". This user doesn't exist"); 340 } 341 getUsers().remove(username); 342 343 try { 345 getMBeanServer().unregisterMBean(JonasObjectName.user(getName(), username)); 347 } catch (Exception e) { 348 getLogger().log(BasicLevel.ERROR, "Cannot unregister user '" + username + "' in JMX server : " + e.getMessage()); 349 } 350 351 } 352 353 358 public void removeGroup(String groupname) throws Exception { 359 if (groups.get(groupname) == null) { 360 throw new Exception ("Can not remove group " + groupname + ". This group doesn't exist"); 361 } 362 groups.remove(groupname); 363 try { 365 getMBeanServer().unregisterMBean(JonasObjectName.group(getName(), groupname)); 367 } catch (Exception e) { 368 getLogger().log(BasicLevel.ERROR, "Cannot unregister group '" + groupname + "' in JMX server : " 369 + e.getMessage()); 370 } 371 } 372 373 378 public void removeRole(String rolename) throws Exception { 379 if (roles.get(rolename) == null) { 380 throw new Exception ("Can not remove role " + rolename + ". This role doesn't exist"); 381 } 382 roles.remove(rolename); 383 384 try { 386 getMBeanServer().unregisterMBean(JonasObjectName.role(getName(), rolename)); 388 } catch (Exception e) { 389 getLogger().log(BasicLevel.ERROR, "Cannot unregister role '" + rolename + "' in JMX server : " + e.getMessage()); 390 } 391 } 392 393 397 public String toXML() { 398 StringBuffer xml = new StringBuffer (" <memoryrealm name=\""); 399 xml.append(getName()); 400 xml.append("\">\n"); 401 402 xml.append(" <roles>\n"); 404 XML.xmlHashtable(xml, getRoles(), " "); 405 xml.append(" </roles>\n"); 406 407 xml.append(" <groups>\n"); 409 XML.xmlHashtable(xml, getGroups(), " "); 410 xml.append(" </groups>\n"); 411 412 xml.append(" <users>\n"); 414 XML.xmlHashtable(xml, getUsers(), " "); 415 xml.append(" </users>\n"); 416 417 xml.append(" </memoryrealm>"); 418 return xml.toString(); 419 } 420 421 425 public String toString() { 426 return this.toXML(); 427 } 428 429 437 public Reference getReference() throws NamingException { 438 439 Reference reference = new Reference (FACTORY_TYPE, FACTORY_NAME, null); 441 442 reference.add(new StringRefAddr ("name", getName())); 444 445 byte[] bytes = JNDIUtils.getBytesFromObject(getUsers(), getLogger()); 447 if (bytes != null) { 448 reference.add(new BinaryRefAddr ("users", bytes)); 449 } 450 451 bytes = JNDIUtils.getBytesFromObject(groups, getLogger()); 453 if (bytes != null) { 454 reference.add(new BinaryRefAddr ("groups", bytes)); 455 } 456 457 bytes = JNDIUtils.getBytesFromObject(roles, getLogger()); 459 if (bytes != null) { 460 reference.add(new BinaryRefAddr ("roles", bytes)); 461 } 462 463 return reference; 464 465 } 466 467 471 public String [] listRoles() { 472 String [] s = new String [roles.size()]; 473 int i = 0; 474 for (Enumeration e = roles.keys(); e.hasMoreElements(); i++) { 475 s[i] = (String ) e.nextElement(); 476 } 477 return s; 478 } 479 480 484 public String [] listGroups() { 485 String [] s = new String [groups.size()]; 486 int i = 0; 487 for (Enumeration e = groups.keys(); e.hasMoreElements(); i++) { 488 s[i] = (String ) e.nextElement(); 489 } 490 return s; 491 } 492 493 497 public void removeMBeans() throws JResourceException { 498 499 boolean error = false; 500 for (Enumeration e = getUsers().elements(); e.hasMoreElements();) { 502 User u = (User) e.nextElement(); 503 try { 504 getMBeanServer().unregisterMBean(JonasObjectName.user(getName(), u.getName())); 506 } catch (Exception ex) { 507 error = true; 508 getLogger().log(BasicLevel.ERROR, "Cannot unregister mbean user '" + u.getName() + "' in JMX server : " 509 + ex.getMessage()); 510 } 511 } 512 513 for (Enumeration e = roles.elements(); e.hasMoreElements();) { 515 Role r = (Role) e.nextElement(); 516 try { 517 getMBeanServer().unregisterMBean(JonasObjectName.role(getName(), r.getName())); 519 } catch (Exception ex) { 520 error = true; 521 getLogger().log(BasicLevel.ERROR, "Cannot unregister mbean role '" + r.getName() + "' in JMX server : " 522 + ex.getMessage()); 523 } 524 } 525 526 for (Enumeration e = groups.elements(); e.hasMoreElements();) { 528 Group g = (Group) e.nextElement(); 529 try { 530 getMBeanServer().unregisterMBean(JonasObjectName.group(getName(), g.getName())); 532 } catch (Exception ex) { 533 error = true; 534 getLogger().log(BasicLevel.ERROR, "Cannot unregister mbean group '" + g.getName() + "' in JMX server : " 535 + ex.getMessage()); 536 } 537 } 538 539 if (error) { 540 throw new JResourceException( 541 "There was errors during the remove of the MBeans of this resource. See the traces."); 542 } 543 544 } 545 546 } | Popular Tags |