/*
*
* Copyright (c) 1999 - 2011 my-Channels Ltd
* Copyright (c) 2012 - 2017 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 nSessionAttributes = com.pcbsys.nirvana.client.nSessionAttributes;
///
/// * This application can be used to add a new subject to a queue, and assign permissions
/// * for operations performed on the queue.
///
public class addqueueacl
{
///
/// * Private variables used in this application
///
private string realm = null;
private string name = null;
private string host = null;
private bool canListAcl = false;
private bool canModifyAcl = false;
private bool fullPrivileges = false;
private bool canGetLastEid = false;
private bool canPeek = false;
private bool canPush = false;
private bool canPurge = false;
private bool canPop = false;
private nSessionAttributes attr = null;
private string queueName = null;
private nRealmNode node = null;
///
/// * Construct an instance of this class using the command line arguments passed
/// * when it is executed.
///
public addqueueacl(string[] args)
{
try
{
// set the permissions flags from the command line
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
Console.Write("waiting for namepsace construction..... ");
node.waitForEntireNameSpace();
Console.WriteLine("finished");
dump();
// begin searching the root realm node
searchNode(node);
node.close();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
///
/// * Create the new acl entry and set the permissions on the queue
///
public virtual void setQueue(nRealmNode p_node, nLeafNode p_leaf)
{
try
{
Console.WriteLine("Creating new entry for " + name + "@" + host);
// create a new acl entry with the name and host
nChannelACLEntry newEntry = createNewACLEntry(name, host);
// add the new entry to the acl
p_leaf.addACLEntry(newEntry);
Console.WriteLine("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
Console.WriteLine("Added ACL for " + p_leaf.Name + " in realm " + p_node.Name);
Console.WriteLine("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
dump(newEntry);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
///
/// * search the enumeration of child nodes for other realms and queues
///
private void searchNodes(nRealmNode p_node, System.Collections.IEnumerator enum1)
{
while (enum1.MoveNext())
{
object obj = enum1.Current;
if (obj is nLeafNode)
{
nLeafNode leaf = (nLeafNode)obj;
string fullyQualifiedName = leaf.getAbsolutePath();
Console.WriteLine("Found queue " + fullyQualifiedName);
if ((!leaf.isChannel()) && (fullyQualifiedName.Equals(queueName)))
{
// we have found the correct queue, so set the permissions on the queue
setQueue(p_node, leaf);
}
}
else if (obj is nRealmNode)
{
// we have found a realm, so search this realm for instances of the queue
searchNode((nRealmNode)obj);
}
else if (obj is nContainer)
{
nContainer cont = (nContainer)obj;
searchNodes(p_node, cont.getNodes());
}
}
}
///
/// * Search the children of the realm passed as a paremeter
///
private void searchNode(nRealmNode p_node)
{
try
{
searchNodes(p_node, p_node.getNodes());
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
///
/// * Create a new acl entry and set the permissions based on the permissions flags
///
public virtual nChannelACLEntry createNewACLEntry(string p_name, string p_host)
{
nChannelACLEntry aclEntry = null;
try
{
aclEntry = new nChannelACLEntry(p_name, p_host);
this.setPermissions(aclEntry);
}
catch (nAdminIllegalArgumentException e)
{
Console.WriteLine(e.Message);
}
return aclEntry;
}
///
/// * If you construct an instance of this class from another class, you can set the name
/// * and host for the subject.
///
public virtual void setSubject(string p_name, string p_host)
{
name = p_name;
host = p_host;
}
///
/// * Set the permissions on the queue acl entry
///
public virtual void setPermissions(nChannelACLEntry aclEntry)
{
aclEntry.setList(canListAcl);
aclEntry.setModify(canModifyAcl);
aclEntry.setFullPrivileges(fullPrivileges);
aclEntry.setGetLastEID(canGetLastEid);
aclEntry.setRead(canPeek);
aclEntry.setWrite(canPush);
aclEntry.setPurge(canPurge);
aclEntry.setPop(canPop);
}
///
/// * Output to system.out the permissions that have been set
///
public virtual void dump(nChannelACLEntry entry)
{
Console.WriteLine("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
Console.WriteLine("List ACL : " + entry.canList());
Console.WriteLine("Set ACL : " + entry.canModify());
Console.WriteLine("Full Privileges : " + entry.hasFullPrivileges());
Console.WriteLine("Get Last EID : " + entry.canGetLastEID());
Console.WriteLine("Peek queue : " + entry.canRead());
Console.WriteLine("Push to queue : " + entry.canWrite());
Console.WriteLine("Purge queue : " + entry.canPurge());
Console.WriteLine("Pop queue : " + entry.canPop());
Console.WriteLine("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
}
///
/// * Output to system.out the permissions that will be set
///
public virtual void dump()
{
Console.WriteLine("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
Console.WriteLine("ACL will be set to.... ");
Console.WriteLine("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
Console.WriteLine("List ACL : " + canListAcl);
Console.WriteLine("Set ACL : " + canModifyAcl);
Console.WriteLine("Full Privileges : " + fullPrivileges);
Console.WriteLine("Get Last EID : " + canGetLastEid);
Console.WriteLine("Peek queue : " + canPeek);
Console.WriteLine("Push to queue : " + canPush);
Console.WriteLine("Purge queue : " + canPurge);
Console.WriteLine("Pop queue : " + canPop);
Console.WriteLine("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
}
///
/// * Set the program variables and permissions 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);
}
queueName = args[3];
if (queueName == null)
{
Usage();
System.Environment.Exit(1);
}
for (int i = 0; i < args.Length; i++)
{
if (args[i].Equals("list_acl"))
{
canListAcl = true;
}
else if (args[i].Equals("modify_acl"))
{
canModifyAcl = true;
}
else if (args[i].Equals("full"))
{
fullPrivileges = true;
}
else if (args[i].Equals("last_eid"))
{
canGetLastEid = true;
}
else if (args[i].Equals("peek"))
{
canPeek = true;
}
else if (args[i].Equals("push"))
{
canPush = true;
}
else if (args[i].Equals("purge"))
{
canPurge = true;
}
else if (args[i].Equals("pop"))
{
canPop = 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
/// * QUEUE
/// *
/// * as system properties, and pass in a list of permissions in the constructor
/// *
///
static void Main(string[] args)
{
addqueueacl setAcl = new addqueueacl(args);
System.Environment.Exit(0);
}
///
/// * Prints the usage message for this class
///
private static void Usage()
{
Console.WriteLine("Usage ...\n");
Console.WriteLine("naddqueueacl [list_acl] [modify_acl] [full] [peek] [push] [purge] [pop]\n");
Console.WriteLine(" \n");
Console.WriteLine(" - the rname of the server to connect to");
Console.WriteLine(" - User name parameter for the queue to add the ACL entry to");
Console.WriteLine(" - Host name parameter for the queue to add the ACL entry to");
Console.WriteLine(" - Queue name parameter for the queue to add the ACL entry to");
Console.WriteLine("\n[Optional Arguments] \n");
Console.WriteLine("[list_acl] - Specifies that the list acl permission should be added");
Console.WriteLine("[modify_acl] - Specifies that the modify acl permission should be added");
Console.WriteLine("[full] - Specifies that the full permission should be added");
Console.WriteLine("[peek] - Specifies that the peak permission should be added");
Console.WriteLine("[push] - Specifies that the push permission should be added");
Console.WriteLine("[purge] - Specifies that the purge permission should be added");
Console.WriteLine("[pop] - Specifies that the pop permission should be added");
}
}
}