From 89e9889865ad453095355f7be65075d599a3b92d Mon Sep 17 00:00:00 2001 From: Juan Carlos Luciani Date: Mon, 6 Nov 2006 06:53:57 +0000 Subject: [PATCH] Adding configuration file editor utilities. --- CASA-auth-token/java/server/Svc/Makefile.am | 19 +- .../casa/authtoksvc/AuthPolicyEditor.java | 871 ++++++++++++++++++ .../authtoksvc/AuthTokenSettingsEditor.java | 324 +++++++ .../casa/authtoksvc/IVerifySetting.java | 52 ++ .../authtoksvc/IdenTokenSettingsEditor.java | 297 ++++++ .../com/novell/casa/authtoksvc/Makefile.am | 8 +- .../casa/authtoksvc/SettingsFileUtil.java | 429 +++++++++ .../casa/authtoksvc/SvcSettingsEditor.java | 374 ++++++++ 8 files changed, 2371 insertions(+), 3 deletions(-) create mode 100644 CASA-auth-token/java/server/Svc/src/com/novell/casa/authtoksvc/AuthPolicyEditor.java create mode 100644 CASA-auth-token/java/server/Svc/src/com/novell/casa/authtoksvc/AuthTokenSettingsEditor.java create mode 100644 CASA-auth-token/java/server/Svc/src/com/novell/casa/authtoksvc/IVerifySetting.java create mode 100644 CASA-auth-token/java/server/Svc/src/com/novell/casa/authtoksvc/IdenTokenSettingsEditor.java create mode 100644 CASA-auth-token/java/server/Svc/src/com/novell/casa/authtoksvc/SettingsFileUtil.java create mode 100644 CASA-auth-token/java/server/Svc/src/com/novell/casa/authtoksvc/SvcSettingsEditor.java diff --git a/CASA-auth-token/java/server/Svc/Makefile.am b/CASA-auth-token/java/server/Svc/Makefile.am index 2950d628..ab7c238d 100644 --- a/CASA-auth-token/java/server/Svc/Makefile.am +++ b/CASA-auth-token/java/server/Svc/Makefile.am @@ -43,6 +43,8 @@ WEBAPP_NAME = CasaAuthTokenSvc WEBAPP_EXT = war MODULE_NAME = CasaAuthToken MODULE_EXT = jar +UTIL_MODULE_NAME = CasaAuthTokenUtil +UTIL_MODULE_EXT = jar JAVAFILES = src/com/novell/casa/authtoksvc/ProtoDefs.java \ src/com/novell/casa/authtoksvc/AuthMechConfig.java \ @@ -69,7 +71,13 @@ JAVAFILES = src/com/novell/casa/authtoksvc/ProtoDefs.java \ src/com/novell/casa/authtoksvc/GetAuthTokReqMsg.java \ src/com/novell/casa/authtoksvc/GetAuthTokRespMsg.java \ src/com/novell/casa/authtoksvc/Krb5Authenticate.java \ - src/com/novell/casa/authtoksvc/PwdAuthenticate.java + src/com/novell/casa/authtoksvc/PwdAuthenticate.java \ + src/com/novell/casa/authtoksvc/IVerifySetting.java \ + src/com/novell/casa/authtoksvc/SettingsFileUtil.java \ + src/com/novell/casa/authtoksvc/AuthPolicyEditor.java \ + src/com/novell/casa/authtoksvc/AuthTokenSettingsEditor.java \ + src/com/novell/casa/authtoksvc/IdenTokenSettingsEditor.java \ + src/com/novell/casa/authtoksvc/SvcSettingsEditor.java BUILDDIR = build @@ -77,6 +85,8 @@ WEBAPP = $(WEBAPP_NAME).$(WEBAPP_EXT) AUTHTOKEN_FILES = -C $(BUILDDIR)/webapp/WEB-INF/classes com +AUTHTOKEN_UTIL_FILES = -C $(BUILDDIR)/webapp/WEB-INF/classes com + CLASSES = $(addprefix $(BUILDDIR)/, $(JAVAFILES:%.java=%.class)) #AXIS_LIBS = $(AXIS_JARS_DIR)/axis.jar:$(AXIS_JARS_DIR)/axis-ant.jar:$(AXIS_JARS_DIR)/commons-discovery-0.2.jar:$(AXIS_JARS_DIR)/commons-logging-1.0.4.jar:$(AXIS_JARS_DIR)/commons-logging-api.jar:$(AXIS_JARS_DIR)/jaxrpc.jar:$(AXIS_JARS_DIR)/log4j-1.2.8.jar:$(AXIS_JARS_DIR)/saaj.jar:$(AXIS_JARS_DIR)/wsdl4j-1.5.1.jar:$(AXIS_JARS_DIR)/wss4j-1.5.0.jar:$(AXIS_JARS_DIR)/xalan.jar:$(AXIS_JARS_DIR)/xercesImpl.jar:$(AXIS_JARS_DIR)/xml-apis.jar:$(AXIS_JARS_DIR)/xmlsec-1.2.1.jar @@ -88,7 +98,7 @@ CLASSPATH = $(AXIS_LIBS):$(IDENT_ABSTRACTION_DIR)/identity-abstraction.jar:$(LIB CUR_DIR := $(shell pwd) -all: $(BUILDDIR)/$(WEBAPP) $(BUILDDIR)/$(MODULE_NAME).$(MODULE_EXT) +all: $(BUILDDIR)/$(WEBAPP) $(BUILDDIR)/$(MODULE_NAME).$(MODULE_EXT) $(BUILDDIR)/$(UTIL_MODULE_NAME).$(UTIL_MODULE_EXT) $(BUILDDIR)/%.class: %.java @echo [======== Compiling $@ ========] @@ -117,6 +127,11 @@ $(BUILDDIR)/$(MODULE_NAME).$(MODULE_EXT): $(BUILDDIR) $(CLASSES) jar cvf $(BUILDDIR)/$(MODULE_NAME).$(MODULE_EXT) $(AUTHTOKEN_FILES) cp $(BUILDDIR)/$(MODULE_NAME).$(MODULE_EXT) $(LIBDIR)/java/ +$(BUILDDIR)/$(UTIL_MODULE_NAME).$(UTIL_MODULE_EXT): $(BUILDDIR) $(CLASSES) + @echo [======== Jarring $@ ========] + jar cvf $(BUILDDIR)/$(UTIL_MODULE_NAME).$(UTIL_MODULE_EXT) $(AUTHTOKEN_UTIL_FILES) + cp $(BUILDDIR)/$(UTIL_MODULE_NAME).$(UTIL_MODULE_EXT) $(LIBDIR)/java/ + $(BUILDDIR): [ -d $(BUILDDIR) ] || mkdir -p $(BUILDDIR) [ -d $(BUILDDIR)/webapp ] || mkdir -p $(BUILDDIR)/webapp diff --git a/CASA-auth-token/java/server/Svc/src/com/novell/casa/authtoksvc/AuthPolicyEditor.java b/CASA-auth-token/java/server/Svc/src/com/novell/casa/authtoksvc/AuthPolicyEditor.java new file mode 100644 index 00000000..5c0e2858 --- /dev/null +++ b/CASA-auth-token/java/server/Svc/src/com/novell/casa/authtoksvc/AuthPolicyEditor.java @@ -0,0 +1,871 @@ +/*********************************************************************** + * + * Copyright (C) 2006 Novell, Inc. All Rights Reserved. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; version 2.1 + * of the License. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, Novell, Inc. + * + * To contact Novell about this file by physical or electronic mail, + * you may find current contact information at www.novell.com. + * + * Author: Juan Carlos Luciani + * + ***********************************************************************/ + +package com.novell.casa.authtoksvc; + +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; +import org.apache.xerces.parsers.DOMParser; +import org.apache.xml.serialize.OutputFormat; +import org.apache.xml.serialize.XMLSerializer; + +import java.io.*; +import java.util.Formatter; + +/** + * + * Class for the creation and editing of auth.policy files. + * + **/ +public class AuthPolicyEditor +{ + private static final String usage = + "java auth_policy_editor -op [-entry realm:mechanismName[:mechanismInfo]] [-refentry realm:mechanismName] -file policyFilePath\n\n" + + " where:\n" + + " -op - Corresponds to one of the following operations:\n" + + " -create - Create new auth policy file\n" + + " -list - List auth source entries\n" + + " -prepend - Insert auth source entry at the head\n" + + " -append - Insert auth source entry at the tail\n" + + " -insert - Insert auth source entry after specified reference entry\n" + + " -remove - Remove auth source entry\n" + + " -file - Path the the auth policy file\n" + + " -entry - Auth source entry to be inserted or removed. Must be followed by\n" + + " a string formated as follows:\n" + + " insert operations format: realm:mechanismName or realm:mechanismName:mechanismInfo\n" + + " remove operations format: realm:mechanismName\n" + + " -refentry - Reference auth source entry. Must be followed by a string formated\n" + + " as follows: realm:mechanismName\n"; + + private static final String initialPolicy = + "\n" + + "\n" + + "\n"; + + private final static String AuthSourceElementName = "auth_source"; + private final static String RealmElementName = "realm"; + private final static String MechanismElementName = "mechanism"; + private final static String MechanismInfoElementName = "mechanism_info"; + private final static String Krb5Mechanism = "Krb5Authenticate"; + private final static String PwdMechanism = "PwdAuthenticate"; + + + /** + * Returns the formal mechanism name if well known + * + * @param mechName Name of mechanism. + * @return Mechanism formal name. + */ + private static String mechFormalName(String mechName) + { + String formalName; + + if (mechName.compareToIgnoreCase(Krb5Mechanism) == 0) + formalName = Krb5Mechanism; + else if (mechName.compareToIgnoreCase(PwdMechanism) == 0) + formalName = PwdMechanism; + else + formalName = mechName; + + return formalName; + } + + /** + * Update the contents of the specified file with the provided + * policy document. + * + * @param filePath Path to policy file to be updated. + * @param doc Policy document. + * @return True if successful. + */ + private static boolean updateFile(String filePath, Document doc) + { + boolean result = false; + + // Update the file with the specified document + // after removing the text nodes. + try + { + // Remove text nodes + Element root = doc.getDocumentElement(); + Node child; + Node next = (Node) root.getFirstChild(); + while ((child = next) != null) + { + next = child.getNextSibling(); + if (child.getNodeType() == Node.TEXT_NODE) + { + // Remove the node + root.removeChild(child); + } + } + + // Update file + File f = new File(filePath); + FileOutputStream out = new FileOutputStream(f); + OutputFormat format = new OutputFormat(doc); + XMLSerializer serializer = new XMLSerializer(out, format); + serializer.serialize(doc.getDocumentElement()); + out.close(); + + result = true; + } + catch (IOException e) + { + System.out.println("Error writing to file " + filePath + ", exception: " + e.toString()); + } + catch (SecurityException e) + { + System.out.println("SecurityException writting to file " + filePath); + } + + return result; + } + + /** + * Gets document for the specified policy file. + * + * @param filePath Path to the policy file. + * @return Document representation of the policy file. + */ + private static Document getPolicyFileDoc(String filePath) + { + Document doc = null; + + try + { + // Get an input stream to read from policy file + File f = new File(filePath); + FileInputStream inStream = new FileInputStream(f); + InputSource source = new InputSource(inStream); + + DOMParser parser = new DOMParser(); + parser.parse(source); + doc = parser.getDocument(); + + inStream.close(); + } + catch (FileNotFoundException e) + { + System.err.println("Policy file " + filePath + " not found"); + } + catch (SecurityException e) + { + System.err.println("SecurityException accessing " + filePath); + } + catch (IOException e) + { + System.err.println("IOException accessing " + filePath + " Exception=" + e.toString()); + } + catch (SAXException e) + { + System.err.println("Policy file " + filePath + " format error"); + } + + return doc; + } + + /** + * Gets a starting policy document + * + * @return Starting policy document. + */ + private static Document getPolicyDoc() + { + Document doc = null; + + try + { + StringReader reader = new StringReader(initialPolicy); + InputSource source = new InputSource(reader); + + DOMParser parser = new DOMParser(); + parser.parse(source); + doc = parser.getDocument(); + reader.close(); + } + catch (Exception e) + { + System.err.println("Program error, exception: " + e.toString()); + } + + return doc; + } + + /** + * List the auth_source entries in the specified policy file. + * + * @param filePath Path to the policy file. + * @return True if the operation is successfully performed. + */ + private static boolean performListOperation(String filePath) + { + boolean opPerformed = false; + + // List the auth sources present in the policy file + Document doc = getPolicyFileDoc(filePath); + if (doc != null) + { + // Go through the elements of the document + Element root = doc.getDocumentElement(); + Node auth_source_node; + Node next_auth_source_node = root.getFirstChild(); + while ((auth_source_node = next_auth_source_node) != null) + { + next_auth_source_node = auth_source_node.getNextSibling(); + if (auth_source_node.getNodeType() == Node.ELEMENT_NODE + && auth_source_node.getLocalName().compareToIgnoreCase("auth_source") == 0) + { + System.out.println("Auth_Source: "); + + // We are dealing with an auth_source, display its children. + Node child; + Node next = auth_source_node.getFirstChild(); + while ((child = next) != null) + { + next = child.getNextSibling(); + if (child.getNodeType() == Node.ELEMENT_NODE) + { + if (child.getLocalName().compareToIgnoreCase("realm") == 0) + { + System.out.println(" Identity source: " + child.getTextContent()); + } + else if (child.getLocalName().compareToIgnoreCase("mechanism") == 0) + { + System.out.println(" Authentication Mechanism: " + child.getTextContent()); + } + else if (child.getLocalName().compareToIgnoreCase("mechanism_info") == 0) + { + System.out.println(" Authentication Mechanism Info: " + child.getTextContent()); + } + } + } + } + } + + opPerformed = true; + } + + return opPerformed; + } + + /** + * Create policy file. + * + * @param filePath Path to the settings file. + * @return True if the operation is successfully performed. + */ + private static boolean performCreateOperation(String filePath) + { + boolean opPerformed = false; + + // create a policy file + Document doc = getPolicyDoc(); + if (doc != null) + { + try + { + File f = new File(filePath); + boolean createStatus = f.createNewFile(); + if (createStatus == true) + { + FileOutputStream out = new FileOutputStream(f); + OutputFormat format = new OutputFormat(doc); + XMLSerializer serializer = new XMLSerializer(out, format); + serializer.serialize(doc.getDocumentElement()); + out.close(); + + opPerformed = true; + } + else + { + System.out.println("File " + filePath + " already exists"); + } + } + catch (IOException e) + { + System.out.println("Error creating file " + filePath + ", exception: " + e.toString()); + } + catch (SecurityException e) + { + System.out.println("SecurityException creating " + filePath); + } + } + + return opPerformed; + } + + /** + * Prepend the auth_source entry to the specified policy file. + * + * @param filePath Path to the policy file. + * @param entry Auth_source entry to be prepended. Entry is formated as + * follows: realm:mech:mechinfo (Note that the mechinfo + * is optional). + * @return True if operation is successfully performed. + */ + private static boolean performPrependOperation(String filePath, String entry) + { + boolean opPerformed = false; + + // Prepend auth source entry to the policy file + Document doc = getPolicyFileDoc(filePath); + if (doc != null) + { + // Parse the entry into its components. Entry is formated as + // follows: realm:mech:mechinfo (Note that the mechinfo + // is optional). + String[] entryComponents = entry.split(":"); + if (entryComponents.length >= 2 + && entryComponents.length <= 3) + { + // Create and prepend the entry elements + Element root = doc.getDocumentElement(); + Element auth_source_element = doc.createElement(AuthSourceElementName); + Element realm_element = doc.createElement(RealmElementName); + realm_element.setTextContent(entryComponents[0]); + auth_source_element.appendChild(realm_element); + Element mechanism_element = doc.createElement(MechanismElementName); + mechanism_element.setTextContent(mechFormalName(entryComponents[1])); + auth_source_element.appendChild(mechanism_element); + if (entryComponents.length == 3) + { + Element mechanism_info_element = doc.createElement(MechanismInfoElementName); + mechanism_info_element.setTextContent(mechFormalName(entryComponents[2])); + auth_source_element.appendChild(mechanism_info_element); + } + + Element firstEntry = null; + Node child; + Node next = (Node) root.getFirstChild(); + while ((child = next) != null) + { + next = child.getNextSibling(); + if (child.getNodeType() == Node.ELEMENT_NODE) + { + // This is the first entry + firstEntry = (Element) child; + break; + } + } + if (firstEntry != null) + root.insertBefore(auth_source_element, firstEntry); + else + root.appendChild(auth_source_element); + + // Update the file + opPerformed = updateFile(filePath, doc); + } + else + { + System.out.println("Invalid entry format"); + } + } + + return opPerformed; + } + + /** + * Append the auth_source entry to the specified policy file. + * + * @param filePath Path to the policy file. + * @param entry Auth_source entry to be appended. Entry is formated as + * follows: realm:mech:mechinfo (Note that the mechinfo + * is optional). + * @return True if operation is successfully performed. + */ + private static boolean performAppendOperation(String filePath, String entry) + { + boolean opPerformed = false; + + // Append auth source entry to the policy file + Document doc = getPolicyFileDoc(filePath); + if (doc != null) + { + // Parse the entry into its components. Entry is formated as + // follows: realm:mech:mechinfo (Note that the mechinfo + // is optional). + String[] entryComponents = entry.split(":"); + if (entryComponents.length >= 2 + && entryComponents.length <= 3) + { + // Create and append the entry elements + Element root = doc.getDocumentElement(); + Element auth_source_element = doc.createElement(AuthSourceElementName); + Element realm_element = doc.createElement(RealmElementName); + realm_element.setTextContent(entryComponents[0]); + auth_source_element.appendChild(realm_element); + Element mechanism_element = doc.createElement(MechanismElementName); + mechanism_element.setTextContent(mechFormalName(entryComponents[1])); + auth_source_element.appendChild(mechanism_element); + if (entryComponents.length == 3) + { + Element mechanism_info_element = doc.createElement(MechanismInfoElementName); + mechanism_info_element.setTextContent(mechFormalName(entryComponents[2])); + auth_source_element.appendChild(mechanism_info_element); + } + root.appendChild(auth_source_element); + + // Update the file + opPerformed = updateFile(filePath, doc); + } + else + { + System.out.println("Invalid entry format"); + } + } + + return opPerformed; + } + + /** + * Insert the auth_source entry to the specified policy file. + * + * @param filePath Path to the policy file. + * @param entry Auth_source entry to be inserted. Entry is formated as + * follows: realm:mech:mechinfo (Note that the mechinfo + * is optional). + * @param refEntry Reference auth_source entry (New entry is inserted after + * it). Entry is formated as follows: realm:mech. + * @return True if operation is successfully performed. + */ + private static boolean performInsertOperation(String filePath, String entry, String refEntry) + { + boolean opPerformed = false; + + // Remove auth sources present in the policy file + Document doc = getPolicyFileDoc(filePath); + if (doc != null) + { + // Parse the entries into their components. Entry is formated as + // follows: realm:mech:mechinfo (Note that the mechinfo + // is optional). + String[] entryComponents = entry.split(":"); + String[] refEntryComponents = refEntry.split(":"); + if (refEntryComponents.length == 2 + && entryComponents.length >= 2 + && entryComponents.length <= 3) + { + // Go through the elements of the document + Element root = doc.getDocumentElement(); + Node curr_auth_source_node; + Node next_auth_source_node = root.getFirstChild(); + while ((curr_auth_source_node = next_auth_source_node) != null) + { + next_auth_source_node = curr_auth_source_node.getNextSibling(); + if (curr_auth_source_node.getNodeType() == Node.ELEMENT_NODE + && curr_auth_source_node.getLocalName().compareToIgnoreCase("auth_source") == 0) + { + // We are dealing with an auth_source, check if this is the + // reference entry. + boolean realmMatch = false; + boolean mechanismMatch = false; + + Node child; + Node next = curr_auth_source_node.getFirstChild(); + while ((child = next) != null) + { + next = child.getNextSibling(); + if (child.getNodeType() == Node.ELEMENT_NODE) + { + if (child.getLocalName().compareToIgnoreCase(RealmElementName) == 0) + { + // Compare the realm name + if (child.getTextContent().compareToIgnoreCase(refEntryComponents[0]) == 0) + realmMatch = true; + } + else if (child.getLocalName().compareToIgnoreCase(MechanismElementName) == 0) + { + // Compare the realm name + if (child.getTextContent().compareToIgnoreCase(mechFormalName(refEntryComponents[1])) == 0) + mechanismMatch = true; + } + } + } + + // Insert entry after current entry if we have a match for the reference entry + if (realmMatch && mechanismMatch) + { + Element auth_source_element = doc.createElement(AuthSourceElementName); + Element realm_element = doc.createElement(RealmElementName); + realm_element.setTextContent(entryComponents[0]); + auth_source_element.appendChild(realm_element); + Element mechanism_element = doc.createElement(MechanismElementName); + mechanism_element.setTextContent(mechFormalName(entryComponents[1])); + auth_source_element.appendChild(mechanism_element); + if (entryComponents.length == 3) + { + Element mechanism_info_element = doc.createElement(MechanismInfoElementName); + mechanism_info_element.setTextContent(mechFormalName(entryComponents[2])); + auth_source_element.appendChild(mechanism_info_element); + } + curr_auth_source_node.getNextSibling(); + Element nextEntry = null; + next = (Node) curr_auth_source_node.getNextSibling();; + while ((child = next) != null) + { + next = child.getNextSibling(); + if (child.getNodeType() == Node.ELEMENT_NODE) + { + // This is the next entry + nextEntry = (Element) child; + break; + } + } + if (nextEntry != null) + root.insertBefore(auth_source_element, nextEntry); + else + root.appendChild(auth_source_element); + + // Update the file + opPerformed = updateFile(filePath, doc); + break; + } + } + } + } + else + { + System.out.println("Invalid entry format"); + } + } + + return opPerformed; + } + + /** + * Remove the auth_source entry from the specified policy file. + * + * @param filePath Path to the policy file. + * @param entry Auth_source entry to be removed. Entry is formated as + * follows: realm:mech. + * @return True if operation is successfully performed. + */ + private static boolean performRemoveOperation(String filePath, String entry) + { + boolean opPerformed = false; + + // Remove auth sources present in the policy file + Document doc = getPolicyFileDoc(filePath); + if (doc != null) + { + // Parse the entry into its components. Entry is formated as + // follows: realm:mech:mechinfo (Note that the mechinfo + // is optional). + String[] entryComponents = entry.split(":"); + if (entryComponents.length == 2) + { + // Go through the elements of the document + Element root = doc.getDocumentElement(); + Node auth_source_node; + Node next_auth_source_node = root.getFirstChild(); + while ((auth_source_node = next_auth_source_node) != null) + { + next_auth_source_node = auth_source_node.getNextSibling(); + if (auth_source_node.getNodeType() == Node.ELEMENT_NODE + && auth_source_node.getLocalName().compareToIgnoreCase("auth_source") == 0) + { + // We are dealing with an auth_source, check if this is the entry + // that must be removed. + boolean realmMatch = false; + boolean mechanismMatch = false; + + Node child; + Node next = auth_source_node.getFirstChild(); + while ((child = next) != null) + { + next = child.getNextSibling(); + if (child.getNodeType() == Node.ELEMENT_NODE) + { + if (child.getLocalName().compareToIgnoreCase(RealmElementName) == 0) + { + // Compare the realm name + if (child.getTextContent().compareToIgnoreCase(entryComponents[0]) == 0) + realmMatch = true; + } + else if (child.getLocalName().compareToIgnoreCase(MechanismElementName) == 0) + { + // Compare the realm name + if (child.getTextContent().compareToIgnoreCase(mechFormalName(entryComponents[1])) == 0) + mechanismMatch = true; + } + } + } + + // Remove current entry if it matches + if (realmMatch && mechanismMatch) + { + System.out.println("RemovingChild"); + root.removeChild(auth_source_node); + + // Update the file + opPerformed = updateFile(filePath, doc); + break; + } + } + } + } + else + { + System.out.println("Invalid entry format"); + } + } + + return opPerformed; + } + + /** + * Applications Entry Point + * + * @param args + */ + public static void main(String[] args) + { + String op = null; + boolean opPerformed = false; + boolean argumentsError = false; + String filePath = null; + String entry = null; + String refEntry = null; + + // Process the command line arguments + for (int i = 0; i < args.length; i++) + { + // Proceed based on the command + if (args[i].compareToIgnoreCase("-list") == 0) + { + // List operation requested + if (op == null) + { + op = "list"; + } + else + { + argumentsError = true; + break; + } + } + else if (args[i].compareToIgnoreCase("-create") == 0) + { + // Create operation requested + if (op == null) + { + op = "create"; + } + else + { + argumentsError = true; + break; + } + } + else if (args[i].compareToIgnoreCase("-prepend") == 0) + { + // Prepend operation requested + if (op == null) + { + op = "prepend"; + } + else + { + argumentsError = true; + break; + } + } + else if (args[i].compareToIgnoreCase("-append") == 0) + { + // Append operation requested + if (op == null) + { + op = "append"; + } + else + { + argumentsError = true; + break; + } + } + else if (args[i].compareToIgnoreCase("-insert") == 0) + { + // Insert operation requested + if (op == null) + { + op = "insert"; + } + else + { + argumentsError = true; + break; + } + } + else if (args[i].compareToIgnoreCase("-remove") == 0) + { + // Remove operation requested + if (op == null) + { + op = "remove"; + } + else + { + argumentsError = true; + break; + } + } + else if (args[i].compareToIgnoreCase("-entry") == 0) + { + // The next argument should contain the entry information + if (args.length > (i + 1)) + { + entry = args[i + 1]; + i++; + } + else + { + argumentsError = true; + break; + } + } + else if (args[i].compareToIgnoreCase("-refentry") == 0) + { + // The next argument should contain the reference entry information + if (args.length > (i + 1)) + { + refEntry = args[i + 1]; + i++; + } + else + { + argumentsError = true; + break; + } + } + else if (args[i].compareToIgnoreCase("-file") == 0) + { + // The next argument should contain the filepath + if (args.length > (i + 1)) + { + filePath = args[i + 1]; + i++; + } + else + { + argumentsError = true; + break; + } + } + else + { + argumentsError = true; + } + } + + // Proceed based on the specified parameters + if (argumentsError == false) + { + if (filePath != null && op != null) + { + System.out.println("Dealing with policy file: " + filePath); + + // Proceed based on the operation requested + if (op.compareTo("list") == 0) + { + opPerformed = performListOperation(filePath); + } + else if (op.compareTo("create") == 0) + { + opPerformed = performCreateOperation(filePath); + } + else if (op.compareTo("prepend") == 0) + { + // Verify that the required parameters were specified + if (entry != null) + { + opPerformed = performPrependOperation(filePath, entry); + } + else + { + argumentsError = true; + } + } + else if (op.compareTo("append") == 0) + { + // Verify that the required parameters were specified + if (entry != null) + { + opPerformed = performAppendOperation(filePath, entry); + } + else + { + argumentsError = true; + } + } + else if (op.compareTo("insert") == 0) + { + // Verify that the required parameters were specified + if (entry != null && refEntry != null) + { + opPerformed = performInsertOperation(filePath, entry, refEntry); + } + else + { + argumentsError = true; + } + } + else if (op.compareTo("remove") == 0) + { + // Verify that the required parameters were specified + if (entry != null) + { + opPerformed = performRemoveOperation(filePath, entry); + } + else + { + argumentsError = true; + } + } + else + { + System.err.println("Tool error"); + } + } + else + { + argumentsError = true; + } + } + + // Display the usage string if we encountered an error with the + // command line arguments. + if (argumentsError) + System.out.print(usage); + + // Set the exit code appropriatedly + if (opPerformed) + System.exit(0); + else + System.exit(1); + } +} diff --git a/CASA-auth-token/java/server/Svc/src/com/novell/casa/authtoksvc/AuthTokenSettingsEditor.java b/CASA-auth-token/java/server/Svc/src/com/novell/casa/authtoksvc/AuthTokenSettingsEditor.java new file mode 100644 index 00000000..64bd421d --- /dev/null +++ b/CASA-auth-token/java/server/Svc/src/com/novell/casa/authtoksvc/AuthTokenSettingsEditor.java @@ -0,0 +1,324 @@ +/*********************************************************************** + * + * Copyright (C) 2006 Novell, Inc. All Rights Reserved. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; version 2.1 + * of the License. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, Novell, Inc. + * + * To contact Novell about this file by physical or electronic mail, + * you may find current contact information at www.novell.com. + * + * Author: Juan Carlos Luciani + * + ***********************************************************************/ + +package com.novell.casa.authtoksvc; + +/** + * + * Class for the creation and editing of authtoken.settings files. + * + **/ +public class AuthTokenSettingsEditor implements IVerifySetting +{ + private static final String usage = + "java authtoken_settings_editor -op [settingName [settingValue]] -file settingsFilePath\n\n" + + " where:\n" + + " -op - Corresponds to one of the following operations:\n" + + " -create - Create new authtoken settings file\n" + + " -list - List settings\n" + + " -get - Get settings, must be followed by settingName parameter\n" + + " -set - Set settings, must be followed by settingName and settingValue parameters\n" + + " -remove - Remove settings\n" + + " -file - Path the the authtoken settings file\n" + + " settingName - Name of the setting being retrieved or set\n" + + " settingValue - Value of the setting being set\n\n" + + " The following settings are valid:\n" + + " TokenLifetime\n" + + " LifetimeShorter\n"; + + private static final String settings = + "\n" + + "\n" + + "\n"; + + + /** + * Checks if the specified setting is valid. + * + * @param setting The name of the setting being checked. + * @return True if the specified setting is valid. + */ + public boolean validSetting(String setting) + { + boolean result = false; + + if (setting.compareToIgnoreCase(AuthTokenConfig.TokenLifetime) == 0) + result = true; + else if (setting.compareToIgnoreCase(AuthTokenConfig.LifetimeShorter) == 0) + result = true; + else if (setting.compareToIgnoreCase(AuthTokenConfig.IdentityTokenType) == 0) + result = true; + else + System.out.println("Invalid setting specified"); + + return result; + } + + /** + * Checks if the specified setting is valid in conjunction + * with the specified value. + * + * @param setting The name of the setting being checked. + * @param value The value of the specified setting. + * @return The formal name of the setting if found to be valid. + */ + public String validSettingNameAndValue(String setting, + String value) + { + String validSetting = null; + + if (setting.compareToIgnoreCase(AuthTokenConfig.TokenLifetime) == 0) + { + // Verify that we are dealing with a numeric value + try + { + Integer.valueOf(value); + + // Good + validSetting = AuthTokenConfig.TokenLifetime; + } + catch (NumberFormatException e) + { + System.out.println("Invalid setting value specified"); + } + } + else if (setting.compareToIgnoreCase(AuthTokenConfig.LifetimeShorter) == 0) + { + // Verify that we are dealing with a numeric value + try + { + Integer.valueOf(value); + + // Good + validSetting = AuthTokenConfig.LifetimeShorter; + } + catch (NumberFormatException e) + { + System.out.println("Invalid setting value specified"); + } + } + else if (setting.compareToIgnoreCase(AuthTokenConfig.IdentityTokenType) == 0) + { + // Always succeed + validSetting = AuthTokenConfig.IdentityTokenType; + } + else + System.out.println("Invalid setting specified"); + + return validSetting; + } + + /** + * Applications Entry Point + * + * @param args + */ + public static void main(String[] args) + { + String op = null; + boolean opPerformed = false; + boolean argumentsError = false; + String filePath = null; + String setting = null; + String value = null; + authtoken_settings_editor editor = new authtoken_settings_editor(); + + // Process the command line arguments + for (int i = 0; i < args.length; i++) + { + // Proceed based on the command + if (args[i].compareToIgnoreCase("-file") == 0) + { + // The next argument should contain the filepath + if (args.length > (i + 1)) + { + filePath = args[i + 1]; + i++; + } + else + { + argumentsError = true; + break; + } + } + else if (args[i].compareToIgnoreCase("-list") == 0) + { + // List operation requested + if (op == null) + { + op = "list"; + } + else + { + argumentsError = true; + break; + } + } + else if (args[i].compareToIgnoreCase("-create") == 0) + { + // List operation requested + if (op == null) + { + op = "create"; + } + else + { + argumentsError = true; + break; + } + } + else if (args[i].compareToIgnoreCase("-get") == 0) + { + // Get setting operation requested + if (op == null) + { + op = "get"; + + // The next argument should contain the setting name + if (args.length > (i + 1)) + { + setting = args[i + 1]; + i++; + } + else + { + argumentsError = true; + break; + } + } + else + { + argumentsError = true; + break; + } + } + else if (args[i].compareToIgnoreCase("-set") == 0) + { + // Set setting operation requested + if (op == null) + { + op = "set"; + + // The next two arguments should contain the setting name + // and the setting value. + if (args.length > (i + 2)) + { + setting = args[i + 1]; + value = args[i + 2]; + i += 2; + } + else + { + argumentsError = true; + break; + } + } + else + { + argumentsError = true; + break; + } + } + else if (args[i].compareToIgnoreCase("-remove") == 0) + { + // Remove setting operation requested + if (op == null) + { + op = "remove"; + + // The next argument should contain the setting name + if (args.length > (i + 1)) + { + setting = args[i + 1]; + i++; + } + else + { + argumentsError = true; + break; + } + } + else + { + argumentsError = true; + break; + } + } + else + { + argumentsError = true; + } + } + + // Proceed based on the specified parameters + if (argumentsError == false) + { + if (filePath != null && op != null) + { + System.out.println("Dealing with settings file: " + filePath); + + // Proceed based on the operation requested + if (op.compareTo("list") == 0) + { + opPerformed = SettingsFileUtil.performListOperation(filePath); + } + else if (op.compareTo("create") == 0) + { + opPerformed = SettingsFileUtil.performCreateOperation(filePath, settings); + } + else if (op.compareTo("get") == 0) + { + opPerformed = SettingsFileUtil.performGetOperation(filePath, setting, editor); + } + else if (op.compareTo("set") == 0) + { + opPerformed = SettingsFileUtil.performSetOperation(filePath, setting, value, editor); + } + else if (op.compareTo("remove") == 0) + { + opPerformed = SettingsFileUtil.performRemoveOperation(filePath, setting, editor); + } + else + { + System.err.println("Tool error"); + } + } + else + { + argumentsError = true; + } + } + + // Display the usage string if we encountered an error with the + // command line arguments. + if (argumentsError) + System.out.print(usage); + + // Set the exit code appropriatedly + if (opPerformed) + System.exit(0); + else + System.exit(1); + } +} diff --git a/CASA-auth-token/java/server/Svc/src/com/novell/casa/authtoksvc/IVerifySetting.java b/CASA-auth-token/java/server/Svc/src/com/novell/casa/authtoksvc/IVerifySetting.java new file mode 100644 index 00000000..17aefad2 --- /dev/null +++ b/CASA-auth-token/java/server/Svc/src/com/novell/casa/authtoksvc/IVerifySetting.java @@ -0,0 +1,52 @@ +/*********************************************************************** + * + * Copyright (C) 2006 Novell, Inc. All Rights Reserved. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; version 2.1 + * of the License. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, Novell, Inc. + * + * To contact Novell about this file by physical or electronic mail, + * you may find current contact information at www.novell.com. + * + * Author: Juan Carlos Luciani + * + ***********************************************************************/ + +package com.novell.casa.authtoksvc; + +/** + * + * Interface exported by users of the SettingsFileUtil class. + * + */ +public interface IVerifySetting +{ + /** + * Checks if the specified setting is valid. + * + * @param setting The name of the setting being checked. + * @return True if the specified setting is valid. + */ + boolean validSetting(String setting); + + /** + * Checks if the specified setting is valid in conjunction + * with the specified value. + * + * @param setting The name of the setting being checked. + * @param value The value of the specified setting. + * @return The formal name of the setting if found to be valid. + */ + String validSettingNameAndValue(String setting, + String value); +} diff --git a/CASA-auth-token/java/server/Svc/src/com/novell/casa/authtoksvc/IdenTokenSettingsEditor.java b/CASA-auth-token/java/server/Svc/src/com/novell/casa/authtoksvc/IdenTokenSettingsEditor.java new file mode 100644 index 00000000..fa974b94 --- /dev/null +++ b/CASA-auth-token/java/server/Svc/src/com/novell/casa/authtoksvc/IdenTokenSettingsEditor.java @@ -0,0 +1,297 @@ +/*********************************************************************** + * + * Copyright (C) 2006 Novell, Inc. All Rights Reserved. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; version 2.1 + * of the License. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, Novell, Inc. + * + * To contact Novell about this file by physical or electronic mail, + * you may find current contact information at www.novell.com. + * + * Author: Juan Carlos Luciani + * + ***********************************************************************/ + +package com.novell.casa.authtoksvc; + +/** + * + * Class for the creation and editing of identtoken.settings files. + * + **/ +public class IdenTokenSettingsEditor implements IVerifySetting +{ + private static final String usage = + "java identoken_settings_editor -op [settingName [settingValue]] -file settingsFilePath\n\n" + + " where:\n" + + " -op - Corresponds to one of the following operations:\n" + + " -create - Create new identoken settings file\n" + + " -list - List settings\n" + + " -get - Get settings, must be followed by settingName parameter\n" + + " -set - Set settings, must be followed by settingName and settingValue parameters\n" + + " -remove - Remove settings\n" + + " -file - Path the the identoken settings file\n" + + " settingName - Name of the setting being retrieved or set\n" + + " settingValue - Value of the setting being set\n\n" + + " The following settings are valid:\n" + + " Attributes\n"; + + private static final String settings = + "\n" + + "\n" + + "\n"; + + + /** + * Checks if the specified setting is valid. + * + * @param setting The name of the setting being checked. + * @return True if the specified setting is valid. + */ + public boolean validSetting(String setting) + { + boolean result = false; + + if (setting.compareToIgnoreCase(IdenTokenConfig.EncryptAttributes) == 0) + result = true; + else if (setting.compareToIgnoreCase(IdenTokenConfig.Attributes) == 0) + result = true; + else + System.out.println("Invalid setting specified"); + + return result; + } + + /** + * Checks if the specified setting is valid in conjunction + * with the specified value. + * + * @param setting The name of the setting being checked. + * @param value The value of the specified setting. + * @return The formal name of the setting if found to be valid. + */ + public String validSettingNameAndValue(String setting, + String value) + { + String validSetting = null; + + if (setting.compareToIgnoreCase(IdenTokenConfig.EncryptAttributes) == 0) + { + // Always succeed + validSetting = IdenTokenConfig.EncryptAttributes; + } + else if (setting.compareToIgnoreCase(IdenTokenConfig.Attributes) == 0) + { + // Always succeed + validSetting = IdenTokenConfig.Attributes; + } + else + System.out.println("Invalid setting specified"); + + return validSetting; + } + + + /** + * Applications Entry Point + * + * @param args + */ + public static void main(String[] args) + { + String op = null; + boolean opPerformed = false; + boolean argumentsError = false; + String filePath = null; + String setting = null; + String value = null; + identoken_settings_editor editor = new identoken_settings_editor(); + + // Process the command line arguments + for (int i = 0; i < args.length; i++) + { + // Proceed based on the command + if (args[i].compareToIgnoreCase("-file") == 0) + { + // The next argument should contain the filepath + if (args.length > (i + 1)) + { + filePath = args[i + 1]; + i++; + } + else + { + argumentsError = true; + break; + } + } + else if (args[i].compareToIgnoreCase("-list") == 0) + { + // List operation requested + if (op == null) + { + op = "list"; + } + else + { + argumentsError = true; + break; + } + } + else if (args[i].compareToIgnoreCase("-create") == 0) + { + // List operation requested + if (op == null) + { + op = "create"; + } + else + { + argumentsError = true; + break; + } + } + else if (args[i].compareToIgnoreCase("-get") == 0) + { + // Get setting operation requested + if (op == null) + { + op = "get"; + + // The next argument should contain the setting name + if (args.length > (i + 1)) + { + setting = args[i + 1]; + i++; + } + else + { + argumentsError = true; + break; + } + } + else + { + argumentsError = true; + break; + } + } + else if (args[i].compareToIgnoreCase("-set") == 0) + { + // Set setting operation requested + if (op == null) + { + op = "set"; + + // The next two arguments should contain the setting name + // and the setting value. + if (args.length > (i + 2)) + { + setting = args[i + 1]; + value = args[i + 2]; + i += 2; + } + else + { + argumentsError = true; + break; + } + } + else + { + argumentsError = true; + break; + } + } + else if (args[i].compareToIgnoreCase("-remove") == 0) + { + // Remove setting operation requested + if (op == null) + { + op = "remove"; + + // The next argument should contain the setting name + if (args.length > (i + 1)) + { + setting = args[i + 1]; + i++; + } + else + { + argumentsError = true; + break; + } + } + else + { + argumentsError = true; + break; + } + } + else + { + argumentsError = true; + } + } + + // Proceed based on the specified parameters + if (argumentsError == false) + { + if (filePath != null && op != null) + { + System.out.println("Dealing with settings file: " + filePath); + + // Proceed based on the operation requested + if (op.compareTo("list") == 0) + { + opPerformed = SettingsFileUtil.performListOperation(filePath); + } + else if (op.compareTo("create") == 0) + { + opPerformed = SettingsFileUtil.performCreateOperation(filePath, settings); + } + else if (op.compareTo("get") == 0) + { + opPerformed = SettingsFileUtil.performGetOperation(filePath, setting, editor); + } + else if (op.compareTo("set") == 0) + { + opPerformed = SettingsFileUtil.performSetOperation(filePath, setting, value, editor); + } + else if (op.compareTo("remove") == 0) + { + opPerformed = SettingsFileUtil.performRemoveOperation(filePath, setting, editor); + } + else + { + System.err.println("Tool error"); + } + } + else + { + argumentsError = true; + } + } + + // Display the usage string if we encountered an error with the + // command line arguments. + if (argumentsError) + System.out.print(usage); + + // Set the exit code appropriatedly + if (opPerformed) + System.exit(0); + else + System.exit(1); + } +} diff --git a/CASA-auth-token/java/server/Svc/src/com/novell/casa/authtoksvc/Makefile.am b/CASA-auth-token/java/server/Svc/src/com/novell/casa/authtoksvc/Makefile.am index b9899c28..f92118b9 100644 --- a/CASA-auth-token/java/server/Svc/src/com/novell/casa/authtoksvc/Makefile.am +++ b/CASA-auth-token/java/server/Svc/src/com/novell/casa/authtoksvc/Makefile.am @@ -49,7 +49,13 @@ JAVAFILES = ProtoDefs.java \ Krb5Authenticate.java \ PwdAuthenticate.java \ SessionToken.java \ - WSSecurity.java + WSSecurity.java \ + AuthPolicyEditor.java \ + AuthTokenSettingsEditor.java \ + IdenTokenSettingsEditor.java \ + IVerifySetting.java \ + SettingsFileUtil.java \ + SvcSettingsEditor.java EXTRA_DIST = $(JAVAFILES) \ Krb5_mechanism.settings \ diff --git a/CASA-auth-token/java/server/Svc/src/com/novell/casa/authtoksvc/SettingsFileUtil.java b/CASA-auth-token/java/server/Svc/src/com/novell/casa/authtoksvc/SettingsFileUtil.java new file mode 100644 index 00000000..a5f4fca0 --- /dev/null +++ b/CASA-auth-token/java/server/Svc/src/com/novell/casa/authtoksvc/SettingsFileUtil.java @@ -0,0 +1,429 @@ +/*********************************************************************** + * + * Copyright (C) 2006 Novell, Inc. All Rights Reserved. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; version 2.1 + * of the License. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, Novell, Inc. + * + * To contact Novell about this file by physical or electronic mail, + * you may find current contact information at www.novell.com. + * + * Author: Juan Carlos Luciani + * + ***********************************************************************/ + +package com.novell.casa.authtoksvc; + +import org.apache.xerces.parsers.DOMParser; +import org.apache.xml.serialize.OutputFormat; +import org.apache.xml.serialize.XMLSerializer; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; +import org.w3c.dom.Document; +import org.w3c.dom.Node; +import org.w3c.dom.Element; + +import java.io.*; +import java.io.File; +import java.io.FileInputStream; + +/** + * + * Class for the creation and editing of settings files. + * + **/ +public class SettingsFileUtil +{ + /** + * Gets document for the specified settings file. + * + * @param filePath Path to the settings file. + * @return Document representation of the settings file. + */ + private static Document getSettingsFileDoc(String filePath) + { + Document doc = null; + + try + { + // Get an input stream to read from settings file + File f = new File(filePath); + FileInputStream inStream = new FileInputStream(f); + InputSource source = new InputSource(inStream); + + DOMParser parser = new DOMParser(); + parser.parse(source); + doc = parser.getDocument(); + + inStream.close(); + } + catch (FileNotFoundException e) + { + System.err.println("Settings file " + filePath + " not found"); + doc = null; + } + catch (SecurityException e) + { + System.err.println("SecurityException accessing " + filePath); + doc = null; + } + catch (IOException e) + { + System.err.println("IOException accessing " + filePath + " Exception=" + e.toString()); + doc = null; + } + catch (SAXException e) + { + System.err.println("Settings file " + filePath + " format error"); + doc = null; + } + + return doc; + } + + /** + * Gets a starting settings document + * + * @param settings String with starting settings document. + * @return Starting settings document. + */ + private static Document getSettingsDoc(String settings) + { + Document doc = null; + + try + { + StringReader reader = new StringReader(settings); + InputSource source = new InputSource(reader); + + DOMParser parser = new DOMParser(); + parser.parse(source); + doc = parser.getDocument(); + reader.close(); + } + catch (Exception e) + { + System.err.println("Program error, exception: " + e.toString()); + } + + return doc; + } + + /** + * List all of the settings present in the specified file. + * + * @param filePath` Path to the settings file. + * @return True if the operation is successfully performed. + */ + public static boolean performListOperation(String filePath) + { + boolean opPerformed = false; + + // List the settings present in the file + Document doc = getSettingsFileDoc(filePath); + if (doc != null) + { + // Go through the elements of the document + Element root = doc.getDocumentElement(); + Node child; + Node next = root.getFirstChild(); + while ((child = next) != null) + { + next = child.getNextSibling(); + if (child.getNodeType() == Node.ELEMENT_NODE) + { + System.out.println(child.getLocalName() + "=" + child.getTextContent()); + } + } + + opPerformed = true; + } + + return opPerformed; + } + + /** + * Create settings file. + * + * @param filePath Path to the settings file. + * @param settings String containing an settings document. + * @return True if the operation is successfully performed. + */ + public static boolean performCreateOperation(String filePath, + String settings) + { + boolean opPerformed = false; + + // create a settings file + Document doc = getSettingsDoc(settings); + if (doc != null) + { + try + { + File f = new File(filePath); + boolean createStatus = f.createNewFile(); + if (createStatus == true) + { + FileOutputStream out = new FileOutputStream(f); + OutputFormat format = new OutputFormat(doc); + XMLSerializer serializer = new XMLSerializer(out, format); + serializer.serialize(doc.getDocumentElement()); + out.close(); + + opPerformed = true; + } + else + { + System.out.println("File " + filePath + " already exists"); + } + } + catch (IOException e) + { + System.out.println("Error creating file " + filePath + ", exception: " + e.toString()); + } + catch (SecurityException e) + { + System.out.println("SecurityException creating " + filePath); + } + } + + return opPerformed; + } + + /** + * Gets value of the specified setting in the specified settings file. + * + * @param filePath Path to the settings file. + * @param setting Name of the setting being queried. + * @return True if the operation is successfully performed. + */ + public static boolean performGetOperation(String filePath, + String setting, + IVerifySetting settingVerifier) + { + boolean opPerformed = false; + + // Validate the setting name specified + if (settingVerifier.validSetting(setting)) + { + // Get settings present in the file + Document doc = getSettingsFileDoc(filePath); + if (doc != null) + { + // Go through the elements of the document until + // we find the one specified. + Element root = doc.getDocumentElement(); + Node child; + Node next = root.getFirstChild(); + boolean settingFound = false; + while ((child = next) != null + && settingFound == false) + { + next = child.getNextSibling(); + if (child.getNodeType() == Node.ELEMENT_NODE + && child.getLocalName().compareToIgnoreCase(setting) == 0) + { + System.out.println(child.getLocalName() + "=" + child.getTextContent()); + settingFound = true; + } + } + + if (settingFound == false) + System.out.println("Not set"); + + opPerformed = true; + } + } + + return opPerformed; + } + + /** + * Sets the specified setting in the specified settings file. + * + * @param filePath Path to the settings file. + * @param setting`` Name of the setting to be set. + * @param value Value to be assigned to the setting. + * @return True if the operation is successfully performed. + */ + public static boolean performSetOperation(String filePath, + String setting, + String value, + IVerifySetting settingVerifier) + { + boolean opPerformed = false; + + // Validate specified setting name and value + String formalSetting; + if ((formalSetting = settingVerifier.validSettingNameAndValue(setting, value)) != null) + { + // Get settings present in the file + Document doc = getSettingsFileDoc(filePath); + if (doc != null) + { + // Go through the elements of the document until + // we find the one specified. + Element root = doc.getDocumentElement(); + Node child; + Node next = (Node) root.getFirstChild(); + boolean settingFound = false; + while ((child = next) != null + && settingFound == false) + { + next = child.getNextSibling(); + if (child.getNodeType() == Node.ELEMENT_NODE + && child.getLocalName().compareToIgnoreCase(setting) == 0) + { + // Change the value of the setting + child.setTextContent(value); + settingFound = true; + } + } + + if (settingFound == false) + { + try + { + Element element = doc.createElement(formalSetting); + element.setTextContent(value); + root.appendChild(element); + } + catch (Exception e) + { + System.err.println("Exception caught " + e.toString()); + } + } + + // Update the file after removing the text nodes + try + { + // Remove text nodes + next = (Node) root.getFirstChild(); + while ((child = next) != null) + { + next = child.getNextSibling(); + if (child.getNodeType() == Node.TEXT_NODE) + { + // Remove the node + root.removeChild(child); + } + } + + // Update file + File f = new File(filePath); + FileOutputStream out = new FileOutputStream(f); + OutputFormat format = new OutputFormat(doc); + XMLSerializer serializer = new XMLSerializer(out, format); + serializer.serialize(doc.getDocumentElement()); + out.close(); + + opPerformed = true; + } + catch (IOException e) + { + System.out.println("Error writing to file " + filePath + ", exception: " + e.toString()); + } + catch (SecurityException e) + { + System.out.println("SecurityException writting to file " + filePath); + } + } + } + + return opPerformed; + } + + /** + * Remove specified setting from the specified settings file. + * + * @param filePath Path to the settings file. + * @param setting Name of the setting to be removed. + * @return True if the operation is successfully performed. + */ + public static boolean performRemoveOperation(String filePath, + String setting, + IVerifySetting settingVerifier) + { + boolean opPerformed = false; + + // Validate specified setting name + if (settingVerifier.validSetting(setting)) + { + // Get settings present in the file + Document doc = getSettingsFileDoc(filePath); + if (doc != null) + { + // Go through the elements of the document until + // we find the one specified. + Element root = doc.getDocumentElement(); + Node child; + Node next = (Node) root.getFirstChild(); + boolean settingFound = false; + while ((child = next) != null + && settingFound == false) + { + next = child.getNextSibling(); + if (child.getNodeType() == Node.ELEMENT_NODE + && child.getLocalName().compareToIgnoreCase(setting) == 0) + { + // Remove the element from the document + root.removeChild(child); + settingFound = true; + + // Update the file after removing the text nodes + try + { + // Remove text nodes + next = (Node) root.getFirstChild(); + while ((child = next) != null) + { + next = child.getNextSibling(); + if (child.getNodeType() == Node.TEXT_NODE) + { + // Remove the node + root.removeChild(child); + } + } + + // Update file + File f = new File(filePath); + FileOutputStream out = new FileOutputStream(f); + OutputFormat format = new OutputFormat(doc); + XMLSerializer serializer = new XMLSerializer(out, format); + serializer.serialize(doc.getDocumentElement()); + out.close(); + + opPerformed = true; + } + catch (IOException e) + { + System.out.println("Error writing to file " + filePath + ", exception: " + e.toString()); + } + catch (SecurityException e) + { + System.out.println("SecurityException writting to file " + filePath); + } + } + } + + if (settingFound == false) + { + // Succeed anyway + opPerformed = true; + } + } + } + + return opPerformed; + } +} diff --git a/CASA-auth-token/java/server/Svc/src/com/novell/casa/authtoksvc/SvcSettingsEditor.java b/CASA-auth-token/java/server/Svc/src/com/novell/casa/authtoksvc/SvcSettingsEditor.java new file mode 100644 index 00000000..1618e931 --- /dev/null +++ b/CASA-auth-token/java/server/Svc/src/com/novell/casa/authtoksvc/SvcSettingsEditor.java @@ -0,0 +1,374 @@ +/*********************************************************************** + * + * Copyright (C) 2006 Novell, Inc. All Rights Reserved. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; version 2.1 + * of the License. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, Novell, Inc. + * + * To contact Novell about this file by physical or electronic mail, + * you may find current contact information at www.novell.com. + * + * Author: Juan Carlos Luciani + * + ***********************************************************************/ + +package com.novell.casa.authtoksvc; + +import java.io.File; + +/** + * + * Class for the creation and editing of svc.settings files. + * + **/ +public class SvcSettingsEditor implements IVerifySetting +{ + private static final String usage = + "java svc_settings_editor -op [settingName [settingValue]] -file settingsFilePath\n\n" + + " where:\n" + + " -op - Corresponds to one of the following operations:\n" + + " -create - Create new svc settings file\n" + + " -list - List settings\n" + + " -get - Get settings, must be followed by settingName parameter\n" + + " -set - Set settings, must be followed by settingName and settingValue parameters\n" + + " -remove - Remove settings\n" + + " -file - Path the the svc settings file\n" + + " settingName - Name of the setting being retrieved or set\n" + + " settingValue - Value of the setting being set\n\n" + + " The following settings are valid:\n" + + " SessionTokenLifetime\n" + + " LifetimeShorter\n" + + " IAConfigFile\n" + + " ReconfigureInterval\n" + + " KeyStoreUser\n" + + " KeyStorePwd\n"; + + private static final String settings = + "\n" + + "\n" + + " /etc/CASA/authtoken/svc/iaRealms.xml\n" + + "\n"; + + + /** + * Checks if the specified setting is valid. + * + * @param setting The name of the setting being checked. + * @return True if the specified setting is valid. + */ + public boolean validSetting(String setting) + { + boolean result = false; + + if (setting.compareToIgnoreCase(SvcConfig.SessionTokenLifetime) == 0) + result = true; + else if (setting.compareToIgnoreCase(SvcConfig.LifetimeShorter) == 0) + result = true; + else if (setting.compareToIgnoreCase(SvcConfig.IdentityAbstractionConfigFile) == 0) + result = true; + else if (setting.compareToIgnoreCase(SvcConfig.ReconfigureInterval) == 0) + result = true; + else if (setting.compareToIgnoreCase(SvcConfig.KeyStoreUser) == 0) + result = true; + else if (setting.compareToIgnoreCase(SvcConfig.KeyStorePwd) == 0) + result = true; + else + System.out.println("Invalid setting specified"); + + return result; + } + + /** + * Checks if the specified setting is valid in conjunction + * with the specified value. + * + * @param setting The name of the setting being checked. + * @param value The value of the specified setting. + * @return The formal name of the setting if found to be valid. + */ + public String validSettingNameAndValue(String setting, + String value) + { + String validSetting = null; + + if (setting.compareToIgnoreCase(SvcConfig.SessionTokenLifetime) == 0) + { + // Verify that we are dealing with a numeric value + try + { + Integer.valueOf(value); + + // Good + validSetting = SvcConfig.SessionTokenLifetime; + } + catch (NumberFormatException e) + { + System.out.println("Invalid setting value specified"); + } + } + else if (setting.compareToIgnoreCase(SvcConfig.LifetimeShorter) == 0) + { + // Verify that we are dealing with a numeric value + try + { + Integer.valueOf(value); + + // Good + validSetting = SvcConfig.LifetimeShorter; + } + catch (NumberFormatException e) + { + System.out.println("Invalid setting value specified"); + } + } + else if (setting.compareToIgnoreCase(SvcConfig.IdentityAbstractionConfigFile) == 0) + { + // Output a warning if the specified file does not exist + try + { + File f = new File(value); + if (f.exists() == false) + { + System.out.println("Warning: File " + value + " does not exist"); + } + } + catch (SecurityException e) + { + System.out.println("Warning: Not able to access file " + value); + } + + // Always succeed + validSetting = SvcConfig.IdentityAbstractionConfigFile; + } + else if (setting.compareToIgnoreCase(SvcConfig.ReconfigureInterval) == 0) + { + // Verify that we are dealing with a numeric value + try + { + Integer.valueOf(value); + + // Good + validSetting = SvcConfig.ReconfigureInterval; + } + catch (NumberFormatException e) + { + System.out.println("Invalid setting value specified"); + } + } + else if (setting.compareToIgnoreCase(SvcConfig.KeyStoreUser) == 0) + { + validSetting = SvcConfig.KeyStoreUser; + } + else if (setting.compareToIgnoreCase(SvcConfig.KeyStorePwd) == 0) + { + validSetting = SvcConfig.KeyStorePwd; + } + else + System.out.println("Invalid setting specified"); + + return validSetting; + } + + /** + * Applications Entry Point + * + * @param args + */ + public static void main(String[] args) + { + String op = null; + boolean opPerformed = false; + boolean argumentsError = false; + String filePath = null; + String setting = null; + String value = null; + svc_settings_editor editor = new svc_settings_editor(); + + // Process the command line arguments + for (int i = 0; i < args.length; i++) + { + // Proceed based on the command + if (args[i].compareToIgnoreCase("-file") == 0) + { + // The next argument should contain the filepath + if (args.length > (i + 1)) + { + filePath = args[i + 1]; + i++; + } + else + { + argumentsError = true; + break; + } + } + else if (args[i].compareToIgnoreCase("-list") == 0) + { + // List operation requested + if (op == null) + { + op = "list"; + } + else + { + argumentsError = true; + break; + } + } + else if (args[i].compareToIgnoreCase("-create") == 0) + { + // List operation requested + if (op == null) + { + op = "create"; + } + else + { + argumentsError = true; + break; + } + } + else if (args[i].compareToIgnoreCase("-get") == 0) + { + // Get setting operation requested + if (op == null) + { + op = "get"; + + // The next argument should contain the setting name + if (args.length > (i + 1)) + { + setting = args[i + 1]; + i++; + } + else + { + argumentsError = true; + break; + } + } + else + { + argumentsError = true; + break; + } + } + else if (args[i].compareToIgnoreCase("-set") == 0) + { + // Set setting operation requested + if (op == null) + { + op = "set"; + + // The next two arguments should contain the setting name + // and the setting value. + if (args.length > (i + 2)) + { + setting = args[i + 1]; + value = args[i + 2]; + i += 2; + } + else + { + argumentsError = true; + break; + } + } + else + { + argumentsError = true; + break; + } + } + else if (args[i].compareToIgnoreCase("-remove") == 0) + { + // Remove setting operation requested + if (op == null) + { + op = "remove"; + + // The next argument should contain the setting name + if (args.length > (i + 1)) + { + setting = args[i + 1]; + i++; + } + else + { + argumentsError = true; + break; + } + } + else + { + argumentsError = true; + break; + } + } + else + { + argumentsError = true; + } + } + + // Proceed based on the specified parameters + if (argumentsError == false) + { + if (filePath != null && op != null) + { + System.out.println("Dealing with settings file: " + filePath); + + // Proceed based on the operation requested + if (op.compareTo("list") == 0) + { + opPerformed = SettingsFileUtil.performListOperation(filePath); + } + else if (op.compareTo("create") == 0) + { + opPerformed = SettingsFileUtil.performCreateOperation(filePath, settings); + } + else if (op.compareTo("get") == 0) + { + opPerformed = SettingsFileUtil.performGetOperation(filePath, setting, editor); + } + else if (op.compareTo("set") == 0) + { + opPerformed = SettingsFileUtil.performSetOperation(filePath, setting, value, editor); + } + else if (op.compareTo("remove") == 0) + { + opPerformed = SettingsFileUtil.performRemoveOperation(filePath, setting, editor); + } + else + { + System.err.println("Tool error"); + } + } + else + { + argumentsError = true; + } + } + + // Display the usage string if we encountered an error with the + // command line arguments. + if (argumentsError) + System.out.print(usage); + + // Set the exit code appropriatedly + if (opPerformed) + System.exit(0); + else + System.exit(1); + } +}