protected static void checkAndEncryptPasswordSyntax( Document doc, boolean encrypt, AMEncryption encryptObj) throws SMSException { // Get the node list of all AttributeSchema NodeList nl = doc.getElementsByTagName(SMSUtils.SCHEMA_ATTRIBUTE); for (int i = 0; i < nl.getLength(); i++) { Node node = nl.item(i); // Check if the "syntax" attribute is "password" String syntax = XMLUtils.getNodeAttributeValue(node, SMSUtils.ATTRIBUTE_SYNTAX); if (syntax.equals(AttributeSchema.Syntax.PASSWORD.toString())) { if (debug.messageEnabled()) { debug.message("ServiceManager: encrypting password syntax"); } // Get the DefaultValues and encrypt then Node defaultNode; if ((defaultNode = XMLUtils.getChildNode(node, SMSUtils.ATTRIBUTE_DEFAULT_ELEMENT)) != null) { // Get NodeList of "Value" nodes and encrypt them for (Iterator items = XMLUtils.getChildNodes(defaultNode, SMSUtils.ATTRIBUTE_VALUE).iterator(); items.hasNext(); ) { Node valueNode = (Node) items.next(); String value = XMLUtils.getValueOfValueNode(valueNode); String encValue; if (encrypt) { if (encryptObj != null) { value = (String) AccessController.doPrivileged(new DecodeAction(value, encryptObj)); if (value.equals("&#160;")) { try { byte[] b = new byte[1]; b[0] = -96; value = new String(b, "ISO-8859-1"); } catch (UnsupportedEncodingException e) { // ignore } } } encValue = (String) AccessController.doPrivileged(new EncodeAction(value)); } else { encValue = (String) AccessController.doPrivileged(new DecodeAction(value)); try { // this is catch the whitespace for password byte[] b = encValue.getBytes("ISO-8859-1"); if ((b.length == 1) && (b[0] == -96)) { encValue = "&#160;"; } } catch (UnsupportedEncodingException e) { // ignore } if (encryptObj != null) { encValue = (String) AccessController.doPrivileged(new EncodeAction(encValue, encryptObj)); } } // Construct the encrypted "Value" node StringBuffer sb = new StringBuffer(100); sb.append(AttributeSchema.VALUE_BEGIN) .append(encValue) .append(AttributeSchema.VALUE_END); Document newDoc = SMSSchema.getXMLDocument(sb.toString(), false); Node newValueNode = XMLUtils.getRootNode(newDoc, SMSUtils.ATTRIBUTE_VALUE); // Replace the node Node nValueNode = doc.importNode(newValueNode, true); defaultNode.replaceChild(nValueNode, valueNode); } } } } }
/** * Registers one or more services, defined by the XML input stream that follows the SMS DTD. * * @param xmlServiceSchema the input stream of service metadata in XML conforming to SMS DTD. * @param decryptObj Object to decrypt the password in the XML. * @return set of registered service names. * @throws SMSException if an error occurred while performing the operation * @throws SSOException if the user's single sign on token is invalid or expired. */ public Set registerServices(InputStream xmlServiceSchema, AMEncryption decryptObj) throws SMSException, SSOException { // Validate SSO Token SMSEntry.validateToken(token); Set sNames = new HashSet(); List serviceNodes = new ArrayList(); // Get the XML document and get the list of service nodes Document doc = SMSSchema.getXMLDocument(xmlServiceSchema); if (!validSMSDtdDocType(doc)) { throw new SMSException( IUMSConstants.UMS_BUNDLE_NAME, IUMSConstants.SMS_xml_invalid_doc_type, null); } // Before validating service schema, we need to check // for AttributeSchema having the syntax of "password" // and if present, encrypt the DefaultValues if any checkAndEncryptPasswordSyntax(doc, true, decryptObj); // Create service schema NodeList nodes = doc.getElementsByTagName(SMSUtils.SERVICE); for (int i = 0; (nodes != null) && (i < nodes.getLength()); i++) { Node serviceNode = nodes.item(i); String name = XMLUtils.getNodeAttributeValue(serviceNode, SMSUtils.NAME); String version = XMLUtils.getNodeAttributeValue(serviceNode, SMSUtils.VERSION); // Obtain the SMSSchema for Schema and PluginSchema SMSSchema smsSchema = new SMSSchema(name, version, doc); // Check if the schema element exists if (XMLUtils.getChildNode(serviceNode, SMSUtils.SCHEMA) != null) { validateServiceSchema(serviceNode); ServiceSchemaManager.createService(token, smsSchema); // Update the service name and version cached SMSEntry if (serviceNames == null) { serviceNames = CachedSubEntries.getInstance(token, serviceDN); } serviceNames.add(name); CachedSubEntries sVersions = (CachedSubEntries) serviceVersions.get(name); if (sVersions == null) { // Not present, hence create it and add it sVersions = CachedSubEntries.getInstance(token, getServiceNameDN(name)); serviceVersions.put(name, sVersions); } sVersions.add(version); sNames.add(name); } // Check if PluginSchema nodes exists for (Iterator pluginNodes = XMLUtils.getChildNodes(serviceNode, SMSUtils.PLUGIN_SCHEMA).iterator(); pluginNodes.hasNext(); ) { Node pluginNode = (Node) pluginNodes.next(); PluginSchema.createPluginSchema(token, pluginNode, smsSchema); } if (XMLUtils.getChildNode(serviceNode, SMSUtils.CONFIGURATION) != null) { serviceNodes.add(serviceNode); } } if (serviceNodes.size() > 0) { clearCache(); } /* * Need to do this after all the schema has been loaded */ for (Iterator i = serviceNodes.iterator(); i.hasNext(); ) { Node svcNode = (Node) i.next(); String name = XMLUtils.getNodeAttributeValue(svcNode, SMSUtils.NAME); String version = XMLUtils.getNodeAttributeValue(svcNode, SMSUtils.VERSION); Node configNode = XMLUtils.getChildNode(svcNode, SMSUtils.CONFIGURATION); /* * Store the configuration, will throw exception if * the service configuration already exists */ CreateServiceConfig.createService(this, name, version, configNode, true, decryptObj); } return sNames; }