/* Copyright 1999-2011 (c) My-Channels Copyright (c) 2012-2015 Software AG, Darmstadt, Germany and/or Software AG USA Inc., Reston, VA, USA, and/or its subsidiaries and/or its affiliates and/or their licensors. Use, reproduction, transfer, publication or disclosure is prohibited except as specifically provided for in your License Agreement with Software AG. */ package com.pcbsys.nirvana.nAdminAPI.apps; import com.pcbsys.foundation.utils.fEnvironment; import com.pcbsys.nirvana.nAdminAPI.*; import com.pcbsys.nirvana.client.*; import java.util.*; /** * This application can be used to remove a subject from a realm * * You can also specify to remove an acl entry from all known realms within a namespace, by * recursively searching through looking for other realm nodes and removing the acl entry * * This is achieved by specifying -r as a command line parameter. */ public class nDelRealmAclEntry { /** * Private variables used in this application */ private String realm = null; private String name = null; private String host = null; private boolean recursive = false; private nSessionAttributes attr = null; private nRealmNode node = null; /** * Construct and instance of this class using the command line arguments passed * when it is executed. */ public nDelRealmAclEntry(String args[]) { try { // set the parameters required for this operation processArgs(args); System.out.println( "Connecting to " + realm ); // construct the session attributes from the realm attr = new nSessionAttributes( realm ); // get the root realm node from the realm admin node = new nRealmNode(attr); if(!node.isAuthorised()){ System.out.println("User not authorised on this node "+realm); return; } // wait for the entire node namespace to be constructed if // the operation is recursive node.waitForEntireNameSpace(); System.out.println( "Removing entry for " + name + "@" + host ); // remove the entry from the acl node.removeRealmACLEntry(new nRealmACLEntry(name+"@"+host)); System.out.println( "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" ); System.out.println( "Removed ACL entry for "+node.getName() ); System.out.println( "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" ); dump(); // if you specify -r as a command line parameter, you can choose to traverse the entire // realm namespace, including any realms that have been added to the root realm node if (recursive) { traverse(node); } node.close(); } catch (Exception e) { e.printStackTrace(); } } /** * recursively search through the nodes from a realm node looking for other realm nodes */ public nRealmNode traverse(nRealmNode p_node) { // get the enumeration of child nodes from p_node Enumeration enum1 = p_node.getNodes(); while ( enum1.hasMoreElements() ) { Object obj = enum1.nextElement(); // only deal with realm nodes if ( obj instanceof nRealmNode ) { nRealmNode node = (nRealmNode)obj; try { node.removeRealmACLEntry(new nRealmACLEntry(name+"@"+host)); } catch (Exception e) { e.printStackTrace(); } System.out.println( "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" ); System.out.println( "Removed ACL entry for "+node.getName() ); System.out.println( "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" ); dump(); // now traverse the child nodes return traverse(node); } else if ( obj instanceof nContainer ) { nContainer cont = (nContainer)obj; searchNode(cont.getNodes()); } } return null; } /** * Search the enumeration of nodes passed as a parameter */ private void searchNode( Enumeration p_nodes ) { try { while (p_nodes.hasMoreElements()) { Object obj = p_nodes.nextElement(); if (obj instanceof nRealmNode) { nRealmNode node = (nRealmNode)obj; try { // get the acl for this realm node nACL acl = node.getACLs(); // remove the entry from the acl acl.remove(name +"@"+host); // set the node acl to the acl list with the removed entry node.setACLs(acl); } catch (Exception e) { e.printStackTrace(); } System.out.println( "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" ); System.out.println( "Removed ACL entry for "+node.getName() ); System.out.println( "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" ); dump(); } else if (obj instanceof nContainer) { searchNode(((nContainer)obj).getNodes()); } } } catch ( Exception ex ) { ex.printStackTrace(); } } /** * If you construct an instance of this class from another class, you can set the name * and host for the subject to remove. */ public void setSubject(String p_name, String p_host) { name = p_name; host = p_host; } /** * Output to system.out the permissions that have been set */ public void dump() { System.out.println( "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" ); System.out.println( "NAME : "+name ); System.out.println( "HOST : "+host ); System.out.println( "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" ); } /** * Set the program variables and flags based on command line args */ public void getOptions(String args[]) { realm = System.getProperty("RNAME", null); if (realm==null) { Usage(); System.exit(1); } name = System.getProperty("NAME", null); if (name==null) { Usage(); System.exit(1); } host = System.getProperty("HOST", null); if (host==null) { Usage(); System.exit(1); } for (int i = 0; i < args.length; i++) { if (args[i].equalsIgnoreCase("-r")) { recursive = true; } } } private void processArgs(String[] args){ if (args.length == 0) { Usage(); System.exit(1); } switch (args.length){ case 1: if (args[0].equals("-?")) UsageEnv(); System.setProperty("NAME",args[0]); getOptions(null); break; default: System.setProperty("NAME",args[0]); System.setProperty("HOST",args[1]); getOptions(args); } } private static void processEnvironmentVariable(String variable){ String laxVAR=System.getProperty("lax.nl.env."+variable); if (laxVAR!=null) System.setProperty(variable,laxVAR); } /** * Run this as a command line program passing the command line args. * * Or construct one of these classes from another class ensuring you have added : * * RNAME * NAME * HOST * * as system properties * */ public static void main( String[] args ) { //Process Environment Variables processEnvironmentVariable("RNAME"); processEnvironmentVariable("LOGLEVEL"); processEnvironmentVariable("HPROXY"); processEnvironmentVariable("HAUTH"); processEnvironmentVariable("CKEYSTORE"); processEnvironmentVariable("CKEYSTOREPASSWD"); processEnvironmentVariable("CAKEYSTORE"); processEnvironmentVariable("CAKEYSTOREPASSWD"); // Install any proxy server settings fEnvironment.setProxyEnvironments(); // Install JSSE SSL Environement settings fEnvironment.setSSLEnvironments(); nDelRealmAclEntry setAcl = new nDelRealmAclEntry( args ); System.exit(0); } /** * Prints the usage message for this class */ private static void Usage() { System.out.println( "Usage ...\n" ); System.out.println("ndelrealmacl [-r] \n"); System.out.println( " \n"); System.out.println( " - User name parameter to delete the realm ACL entry from" ); System.out.println( " - Host name parameter to delete the realm ACL entry from" ); System.out.println( "\n[Optional Arguments] \n"); System.out.println( "[-r] - Specifies whether recursive traversal of the namespace should be done" ); System.out.println( "\n\nNote: -? provides help on environment variables \n"); } private static void UsageEnv() { System.out.println( "\n\n(Environment Variables) \n"); System.out.println( "(RNAME) - One or more RNAME entries in the form protocol://host:port" ); System.out.println( " protocol - Can be one of nsp, nhp, nsps, or nhps, where:" ); System.out.println( " nsp - Specifies Nirvana Socket Protocol (nsp)" ); System.out.println( " nhp - Specifies Nirvana HTTP Protocol (nhp)" ); System.out.println( " nsps - Specifies Nirvana Socket Protocol Secure (nsps), i.e. using SSL/TLS" ); System.out.println( " nhps - Specifies Nirvana HTTP Protocol Secure (nhps), i.e. using SSL/TLS" ); System.out.println( " port - The port number of the server" ); System.out.println( "\nHint: - For multiple RNAME entries, use comma separated values which will be attempted in connection weight order\n" ); System.out.println( "(LOGLEVEL) - This determines how much information the nirvana api will output 0 = verbose 7 = quiet\n" ); System.out.println( "(CKEYSTORE) - If using SSL, the location of the keystore containing the client cert\n"); System.out.println( "(CKEYSTOREPASSWD) - If using SSL, the password for the keystore containing the client cert\n"); System.out.println( "(CAKEYSTORE) - If using SSL, the location of the ca truststore\n"); System.out.println( "(CAKEYSTOREPASSWD) - If using SSL, the password for the ca truststore\n"); System.out.println( "(HPROXY) - HTTP Proxy details in the form proxyhost:proxyport, where:" ); System.out.println( " proxyhost - The HTTP proxy host" ); System.out.println( " proxyport - The HTTP proxy port\n" ); System.out.println( "(HAUTH) - HTTP Proxy authentication details in the form user:pass, where:" ); System.out.println( " user - The HTTP proxy authentication username" ); System.out.println( " pass - The HTTP proxy authentication password\n" ); System.exit(1); } }