/*
Copyright 1999-2011 (c) My-Channels
Copyright (c) 2012-2014 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.
*/
using System;
namespace com.pcbsys.nirvana.nAdminAPI.apps
{
using com.pcbsys.nirvana.nAdminAPI;
using com.pcbsys.nirvana.client;
///
/// * 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 delrealmacl
{
///
/// * Private variables used in this application
///
private string realm = null;
private string name = null;
private string host = null;
private bool 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 delrealmacl(string[] args)
{
try
{
// set the parameters required for this operation
getOptions(args);
Console.WriteLine("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())
{
Console.WriteLine("User not authorised on this node " + realm);
return;
}
// wait for the entire node namespace to be constructed if
// the operation is recursive
node.waitForEntireNameSpace();
Console.WriteLine("Removing entry for " + name + "@" + host);
// remove the entry from the acl
node.removeRealmACLEntry(new nRealmACLEntry(name + "@" + host));
Console.WriteLine("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
Console.WriteLine("Removed ACL entry for " + node.Name);
Console.WriteLine("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
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)
{
Console.WriteLine(e.StackTrace);
}
}
///
/// * recursively search through the nodes from a realm node looking for other realm nodes
///
public virtual nRealmNode traverse(nRealmNode p_node)
{
// get the enumeration of child nodes from p_node
System.Collections.IEnumerator enum1 = p_node.getNodes();
while (enum1.MoveNext())
{
object obj = enum1.Current;
// only deal with realm nodes
if (obj is nRealmNode)
{
nRealmNode node = (nRealmNode)obj;
try
{
node.removeRealmACLEntry(new nRealmACLEntry(name + "@" + host));
}
catch (Exception e)
{
Console.WriteLine(e.StackTrace);
}
Console.WriteLine("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
Console.WriteLine("Removed ACL entry for " + node.Name);
Console.WriteLine("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
dump();
// now traverse the child nodes
return traverse(node);
}
else if (obj is nContainer)
{
nContainer cont = (nContainer)obj;
searchNode(cont.getNodes());
}
}
return null;
}
///
/// * Search the enumeration of nodes passed as a parameter
///
private void searchNode(System.Collections.IEnumerator p_nodes)
{
try
{
while (p_nodes.MoveNext())
{
object obj = p_nodes.Current;
if (obj is 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)
{
Console.WriteLine(e.StackTrace);
}
Console.WriteLine("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
Console.WriteLine("Removed ACL entry for " + node.Name);
Console.WriteLine("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
dump();
}
else if (obj is nContainer)
{
searchNode(((nContainer)obj).getNodes());
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.StackTrace);
}
}
///
/// * If you construct an instance of this class from another class, you can set the name
/// * and host for the subject to remove.
///
public virtual 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 virtual void dump()
{
Console.WriteLine("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
Console.WriteLine("NAME : " + name);
Console.WriteLine("HOST : " + host);
Console.WriteLine("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
}
///
/// * Set the program variables and flags based on command line args
///
public virtual void getOptions(string[] args)
{
if (args == null || args.Length == 0)
{
Usage();
System.Environment.Exit(1);
}
realm = args[0];
if (realm == null)
{
Usage();
System.Environment.Exit(1);
}
name = args[1];
if (name == null)
{
Usage();
System.Environment.Exit(1);
}
host = args[2];
if (host == null)
{
Usage();
System.Environment.Exit(1);
}
for (int i = 0; i < args.Length; i++)
{
if (args[i].Equals("-r"))
{
recursive = true;
}
}
}
///
/// * 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
/// *
///
static void Main(string[] args)
{
delrealmacl setAcl = new delrealmacl(args);
System.Environment.Exit(0);
}
///
/// * Prints the usage message for this class
///
private static void Usage()
{
Console.WriteLine("Usage ...\n");
Console.WriteLine("ndelrealmacl [-r] \n");
Console.WriteLine(" \n");
Console.WriteLine(" - the rname of the server to connect to");
Console.WriteLine(" - User name parameter to delete the realm ACL entry from");
Console.WriteLine(" - Host name parameter to delete the realm ACL entry from");
Console.WriteLine("\n[Optional Arguments] \n");
Console.WriteLine("[-r] - Specifies whether recursive traversal of the namespace should be done");
}
}
}