1 25 26 package org.objectweb.jonas.security; 27 28 import java.io.File ; 29 import java.io.FileNotFoundException ; 30 import java.io.FileReader ; 31 import java.io.Reader ; 32 import java.io.StringReader ; 33 import java.security.NoSuchAlgorithmException ; 34 35 import javax.management.MBeanServer ; 36 import javax.naming.Context ; 37 import javax.naming.InitialContext ; 38 import javax.naming.NamingException ; 39 40 import org.objectweb.jonas.common.Log; 41 import org.objectweb.jonas.jmx.JmxService; 42 import org.objectweb.jonas.jmx.JonasObjectName; 43 import org.objectweb.jonas.security.lib.wrapper.JResourceManagerWrapper; 44 import org.objectweb.jonas.security.realm.factory.JResource; 45 import org.objectweb.jonas.security.realm.factory.JResourceDS; 46 import org.objectweb.jonas.security.realm.factory.JResourceLDAP; 47 import org.objectweb.jonas.security.realm.factory.JResourceMemory; 48 import org.objectweb.jonas.security.realm.factory.JResourceRemote; 49 import org.objectweb.jonas.security.realm.factory.JResourceRemoteImpl; 50 import org.objectweb.jonas.security.realm.lib.HashHelper; 51 import org.objectweb.jonas.service.AbsServiceImpl; 52 import org.objectweb.jonas.service.ServiceException; 53 import org.objectweb.jonas.service.ServiceManager; 54 55 import org.objectweb.util.monolog.api.BasicLevel; 56 import org.objectweb.util.monolog.api.Logger; 57 58 65 66 public class JonasSecurityServiceImpl extends AbsServiceImpl implements SecurityService, JonasSecurityServiceImplMBean { 67 68 71 private static Logger logger = null; 72 73 76 public static final String CLASS = "jonas.service.security.class"; 77 78 81 public static final String REMOTE_RESOUCE = "_remoteres"; 82 83 86 protected static final String CONFIG_FILE = "conf" + File.separator + "jonas-realm.xml"; 87 88 91 protected static final String BIND_RESOURCES_INTO_JNDI = "jonas.service.security.realm.jndi.registration"; 92 93 96 private MBeanServer mbeanServer = null; 97 98 101 private JResources jResources; 102 103 106 private Context ictx = null; 107 108 111 private boolean bindResourcesIntoJndi = false; 112 113 114 120 public void doInit(Context ctx) throws ServiceException { 121 if (logger == null) { 122 logger = Log.getLogger(Log.JONAS_SECURITY_PREFIX); 123 } 124 125 jResources = new JResources(this); 127 128 try { 130 ictx = new InitialContext (); 131 } catch (NamingException e) { 132 logger.log(BasicLevel.ERROR, "Cannot create initial context during the mail service initializing", e); 133 throw new ServiceException("Cannot create initial context during the mail service initializing", e); 134 } 135 136 JResourceRemote jResourceRemote = null; 139 try { 140 jResourceRemote = new JResourceRemoteImpl(); 141 ictx.bind(getJonasServerName() + REMOTE_RESOUCE, jResourceRemote); 142 } catch (Exception e) { 143 logger.log(BasicLevel.ERROR, "Cannot bind remote resource for security access", e); 144 throw new ServiceException("Cannot create initial context during the mail service initializing", e); 145 } 146 147 try { 149 mbeanServer = ((JmxService) ServiceManager.getInstance().getJmxService()).getJmxServer(); 150 } catch (Exception e) { 151 mbeanServer = null; 153 } 154 155 try { 156 String s = (String ) ctx.lookup(BIND_RESOURCES_INTO_JNDI); 157 bindResourcesIntoJndi = new Boolean (s).booleanValue(); 158 } catch (Exception e) { 159 if (logger.isLoggable(BasicLevel.DEBUG)) { 160 logger.log(BasicLevel.DEBUG, "Property '" + BIND_RESOURCES_INTO_JNDI + "' not available, set it to false by default", e); 161 } 162 } 163 164 165 if (logger.isLoggable(BasicLevel.DEBUG)) { 166 logger.log(BasicLevel.DEBUG, "JonasSecurityService initialized"); 167 } 168 } 169 170 175 public void removeJResource(String resourceName) throws Exception { 176 177 JResource jResource = jResources.remove(resourceName); 179 180 if (bindResourcesIntoJndi) { 182 try { 183 ictx.unbind(resourceName); 184 if (logger.isLoggable(BasicLevel.DEBUG)) { 185 logger.log(BasicLevel.DEBUG, "jResource " + resourceName + " remove from the registry."); 186 } 187 } catch (NamingException e) { 188 logger.log(BasicLevel.ERROR, "Cannot unbind the resource '" + resourceName + "' into JNDI", e); 189 } 190 } 191 192 try { 193 jResource.removeMBeans(); 195 196 if (jResource instanceof JResourceMemory) { 198 mbeanServer.unregisterMBean(JonasObjectName.securityMemoryFactory(resourceName)); 199 } else if (jResource instanceof JResourceDS) { 200 mbeanServer.unregisterMBean(JonasObjectName.securityDatasourceFactory(resourceName)); 201 } else if (jResource instanceof JResourceLDAP) { 202 mbeanServer.unregisterMBean(JonasObjectName.securityLdapFactory(resourceName)); 203 } 204 } catch (ServiceException se) { 205 logger.log(BasicLevel.ERROR, "JMX service not available", se); 206 } catch (Exception e) { 207 logger.log(BasicLevel.ERROR, "Can not unregister the MBean for the resource " + resourceName + " : " 208 + e.getMessage()); 209 throw new ServiceException("Can not unregister the MBean for the resource " + resourceName + " : " 210 + e.getMessage()); 211 } 212 213 } 214 215 219 public void doStart() throws ServiceException { 220 try { 221 mbeanServer.registerMBean(this, JonasObjectName.securityService()); 223 } catch (ServiceException se) { 224 logger.log(BasicLevel.ERROR, "JMX service not available", se); 225 } catch (Exception e) { 226 logger.log(BasicLevel.ERROR, "SecurityService: Cannot start the Security service:\n" + e); 227 throw new ServiceException("SecurityService: Cannot start the Security service", e); 228 } 229 230 createRealm(); 231 } 232 233 236 public void doStop() { 237 238 try { 240 ((JmxService) ServiceManager.getInstance().getJmxService()).getJmxServer().unregisterMBean( 242 JonasObjectName.securityService()); 243 } catch (ServiceException se) { 244 logger.log(BasicLevel.ERROR, "JMX service not available", se); 245 } catch (Exception e) { 246 logger.log(BasicLevel.ERROR, "Cannot stop the security service:\n" + e); 247 throw new ServiceException("Cannot stop the security service", e); 248 } 249 } 250 251 256 public JResource getJResource(String name) { 257 return jResources.getJResource(name); 258 } 259 260 264 private void createRealm() throws ServiceException { 265 266 File configFile = null; 268 Reader reader = null; 269 try { 270 configFile = getConfigFile(); 271 reader = new FileReader (configFile); 272 } catch (FileNotFoundException e) { 273 logger.log(BasicLevel.ERROR, "Cannot find config file " + configFile); 274 throw new ServiceException(e.getMessage(), e); 275 } 276 277 try { 278 JResourceManagerWrapper.addResources(jResources, reader, configFile.getPath()); 279 } catch (Exception e1) { 280 String err = "Cannot add security resource from '" + configFile + "'"; 281 logger.log(BasicLevel.ERROR, err); 282 throw new ServiceException(err, e1); 283 } 284 } 285 286 292 protected File getConfigFile() throws FileNotFoundException { 293 String fileName = System.getProperty("jonas.base"); 294 fileName = fileName + File.separator + CONFIG_FILE; 295 File file = new File (fileName); 296 if (!file.exists()) { 297 String err = "Can't find configuration file : " + fileName; 298 throw new FileNotFoundException (err); 299 } 300 return (file); 301 } 302 303 307 public String toXML() { 308 return jResources.toXML(); 309 } 310 311 318 public String encryptPassword(String string, String algo) throws NoSuchAlgorithmException { 319 String encrypt = HashHelper.hashPassword(string, algo); 320 return "{" + algo.toUpperCase() + "}" + encrypt; 322 } 323 324 329 public boolean isValidAlgorithm(String algo) { 330 boolean b = true; 331 try { 332 encryptPassword("test", algo); 333 } catch (NoSuchAlgorithmException nsae) { 334 b = false; 335 } 336 return b; 337 } 338 339 344 public void addResources(String xml) throws Exception { 345 346 try { 347 JResourceManagerWrapper.addResources(jResources, new StringReader (xml), ""); 348 } catch (Exception e1) { 349 String err = "Cannot add security resource from xml '" + xml + "'"; 350 logger.log(BasicLevel.ERROR, err); 351 throw new ServiceException(err, e1); 352 } 353 } 354 355 360 public void addJResourceMemory(String name) throws Exception { 361 362 JResourceMemory jResourceMemory = new JResourceMemory(); 364 jResourceMemory.setName(name); 365 366 StringBuffer xml = new StringBuffer (JResources.HEADER_XML); 368 xml.append("<jonas-realm>"); 369 xml.append("<jonas-memoryrealm>"); 370 xml.append(jResourceMemory.toXML()); 371 xml.append("</jonas-memoryrealm>"); 372 xml.append("</jonas-realm>"); 373 374 addResources(xml.toString()); 376 377 } 378 379 393 public void addJResourceDS(String name, String dsName, String userTable, String userTableUsernameCol, 394 String userTablePasswordCol, String roleTable, String roleTableUsernameCol, String roleTableRolenameCol, 395 String algorithm) throws Exception { 396 397 JResourceDS jResourceDS = new JResourceDS(); 399 jResourceDS.setName(name); 400 jResourceDS.setDsName(dsName); 401 jResourceDS.setUserTable(userTable); 402 jResourceDS.setUserTableUsernameCol(userTableUsernameCol); 403 jResourceDS.setUserTablePasswordCol(userTablePasswordCol); 404 jResourceDS.setRoleTable(roleTable); 405 jResourceDS.setRoleTableUsernameCol(roleTableUsernameCol); 406 jResourceDS.setRoleTableRolenameCol(roleTableRolenameCol); 407 jResourceDS.setAlgorithm(algorithm); 408 409 StringBuffer xml = new StringBuffer (JResources.HEADER_XML); 411 xml.append("<jonas-realm>"); 412 xml.append("<jonas-dsrealm>"); 413 xml.append(jResourceDS.toXML()); 414 xml.append("</jonas-dsrealm>"); 415 xml.append("</jonas-realm>"); 416 417 addResources(xml.toString()); 419 420 } 421 422 460 public void addJResourceLDAP(String name, String initialContextFactory, String providerUrl, 461 String securityAuthentication, String securityPrincipal, String securityCredentials, 462 String securityProtocol, String language, String referral, String stateFactories, 463 String authenticationMode, String userPasswordAttribute, String userRolesAttribute, 464 String roleNameAttribute, String baseDN, String userDN, String userSearchFilter, String roleDN, 465 String roleSearchFilter, String algorithm) throws Exception { 466 467 JResourceLDAP jResourceLDAP = new JResourceLDAP(); 469 jResourceLDAP.setName(name); 470 jResourceLDAP.setInitialContextFactory(initialContextFactory); 471 jResourceLDAP.setProviderUrl(providerUrl); 472 jResourceLDAP.setSecurityAuthentication(securityAuthentication); 473 jResourceLDAP.setSecurityPrincipal(securityPrincipal); 474 jResourceLDAP.setSecurityCredentials(securityCredentials); 475 jResourceLDAP.setSecurityProtocol(securityProtocol); 476 jResourceLDAP.setLanguage(language); 477 jResourceLDAP.setReferral(referral); 478 jResourceLDAP.setStateFactories(stateFactories); 479 jResourceLDAP.setAuthenticationMode(authenticationMode); 480 jResourceLDAP.setUserPasswordAttribute(userPasswordAttribute); 481 jResourceLDAP.setUserRolesAttribute(userRolesAttribute); 482 jResourceLDAP.setRoleNameAttribute(roleNameAttribute); 483 jResourceLDAP.setBaseDN(baseDN); 484 jResourceLDAP.setUserDN(userDN); 485 jResourceLDAP.setUserSearchFilter(userSearchFilter); 486 jResourceLDAP.setRoleDN(roleDN); 487 jResourceLDAP.setRoleSearchFilter(roleSearchFilter); 488 jResourceLDAP.setAlgorithm(algorithm); 489 490 StringBuffer xml = new StringBuffer (JResources.HEADER_XML); 492 xml.append("<jonas-realm>"); 493 xml.append("<jonas-ldaprealm>"); 494 xml.append(jResourceLDAP.toXML()); 495 xml.append("</jonas-ldaprealm>"); 496 xml.append("</jonas-realm>"); 497 498 addResources(xml.toString()); 500 501 } 502 503 509 public void bindResource(String name, JResource jResource) { 510 if (bindResourcesIntoJndi) { 512 try { 513 ictx.rebind(jResource.getName(), jResource); 514 if (logger.isLoggable(BasicLevel.DEBUG)) { 515 logger.log(BasicLevel.DEBUG, "jResource " + jResource.getName() + " bound into the registry."); 516 } 517 } catch (NamingException e) { 518 logger.log(BasicLevel.ERROR, "Cannot bind the resource '" + jResource.getName() + "' into JNDI", e); 519 } 520 } 521 522 try { 523 if (jResource instanceof JResourceMemory) { 525 mbeanServer.registerMBean(jResource, JonasObjectName.securityMemoryFactory(jResource.getName())); 526 } else if (jResource instanceof JResourceDS) { 527 mbeanServer.registerMBean(jResource, JonasObjectName.securityDatasourceFactory(jResource.getName())); 528 } else if (jResource instanceof JResourceLDAP) { 529 mbeanServer.registerMBean(jResource, JonasObjectName.securityLdapFactory(jResource.getName())); 530 } 531 } catch (ServiceException se) { 532 logger.log(BasicLevel.ERROR, "JMX service not available", se); 533 } catch (Exception e) { 534 logger.log(BasicLevel.ERROR, "Can not register the MBean for the resource " + jResource.getName() + " : " 535 + e.getMessage()); 536 throw new ServiceException("Can not register the MBean for the resource " + jResource.getName() + " : " 537 + e.getMessage()); 538 } 539 540 } 541 } 542 | Popular Tags |