import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NameNotFoundException;
import javax.naming.NamingException;
import javax.naming.NamingEnumeration;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.BasicAttributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import com.novell.utility.naming.Environment;
import com.novell.service.nds.NdsObject;
import com.novell.service.nds.NdsObjectACL;
import com.novell.service.nds.NdsObjectRights;
class Acl
{
static boolean ADD_OPTION,
DELETE_OPTION,
PRINT_STACK_TRACE,
RESULT,
URL_SPECIFIED = false;
static int ENTRY_RIGHTS;
static StringBuffer OBJECT_1, OBJECT_2, RIGHTS;
static StringBuffer PROVIDER_URL;
static String ACL_ATTR_ID = "acl";
static String ENTRY_RIGHTS_ACL_ATTR_VALUE = "[Entry Rights]";
static
{
ENTRY_RIGHTS = NdsObjectRights.DS_ENTRY_BROWSE;
OBJECT_1 = new StringBuffer("");
OBJECT_2 = new StringBuffer("");
PROVIDER_URL = new StringBuffer("");
RIGHTS = new StringBuffer("browse");
}
public static void main(String[] args)
{
parseArgs(args);
String object1URL =
PROVIDER_URL.toString()
+ OBJECT_1.toString();
String object2URL =
PROVIDER_URL.toString()
+ OBJECT_2.toString();
if (URL_SPECIFIED)
System.out.println("\nUsing "
+ PROVIDER_URL
+ " as the base URL");
if (ADD_OPTION)
{
DirContext target = Acl.createInitialContext(object1URL);
DirContext trustee = Acl.createInitialContext(object2URL);
RESULT = Acl.addTrustee(target, trustee);
}
if (DELETE_OPTION)
{
DirContext target = Acl.createInitialContext(object1URL);
DirContext trustee = Acl.createInitialContext(object2URL);
RESULT = Acl.deleteTrustee(target, trustee);
}
if (!(ADD_OPTION || DELETE_OPTION))
{
DirContext target = Acl.createInitialContext(object1URL);
RESULT = Acl.printACL(target);
}
if (RESULT)
System.out.println("Operation succeeded");
else
System.out.println("Operation failed");
}
private static boolean addTrustee(DirContext target, DirContext trustee)
{
System.out.println("\nAttempting to add "
+ OBJECT_2
+ " to ACL of "
+ OBJECT_1
);
NdsObjectACL trusteeACL = new NdsObjectACL(
ENTRY_RIGHTS_ACL_ATTR_VALUE,
((NdsObject)trustee).getDistinguishedName(),
ENTRY_RIGHTS);
Attributes attrs = new BasicAttributes(ACL_ATTR_ID, trusteeACL);
try
{
target.modifyAttributes("", DirContext.ADD_ATTRIBUTE, attrs);
return true;
}
catch (NamingException ne)
{
System.out.println("\nCouldn't add the trustee to the ACL\n");
if (PRINT_STACK_TRACE)
ne.printStackTrace();
return false;
}
}
private static boolean deleteTrustee(DirContext target, DirContext trustee)
{
System.out.println("\nAttempting to delete "
+ OBJECT_2
+ " from ACL of "
+ OBJECT_1
+ "."
);
try
{
NdsObject scoped_trustee = (NdsObject)trustee;
String trusteeName = scoped_trustee.getDistinguishedName();
Attributes attrs = target.getAttributes("", new String[] { ACL_ATTR_ID });
Attribute acl = attrs.get(ACL_ATTR_ID);
NamingEnumeration trustees = acl.getAll();
boolean SUCCESSFUL_DELETION = false;
while (trustees.hasMore())
{
NdsObjectACL aclEntry = (NdsObjectACL)trustees.next();
String subjectName = aclEntry.getSubjectName();
String attrName = aclEntry.getProtectedAttrName();
if ((subjectName.equalsIgnoreCase(trusteeName))
&&
(attrName.equals(ENTRY_RIGHTS_ACL_ATTR_VALUE)))
{
Attributes attrs2 = new BasicAttributes("ACL", aclEntry);
target.modifyAttributes("", DirContext.REMOVE_ATTRIBUTE, attrs2);
SUCCESSFUL_DELETION = true;
}
}
if (SUCCESSFUL_DELETION)
System.out.println("The trustee was removed from the ACL");
else
System.out.println("The trustee was not removed from the ACL");
return SUCCESSFUL_DELETION;
}
catch (NamingException ne)
{
System.out.println("\nCouldn't delete the trustee from the ACL\n");
if (PRINT_STACK_TRACE)
ne.printStackTrace();
return false;
}
}
private static boolean printACL(DirContext target)
{
try
{
Attributes attrs = target.getAttributes("");
Attribute acl = attrs.get(ACL_ATTR_ID);
if (acl == null)
{
System.out.println("\nCan't get the ACL, probably because you do not have browse rights to the object.");
System.exit(-1);
return false;
}
else
{
NamingEnumeration trustees = acl.getAll();
System.out.println("\n==============================================");
System.out.println("ACL of "
+ OBJECT_1
+ " [Entry Rights]"
);
System.out.println("==============================================");
while (trustees.hasMore())
{
NdsObjectACL trustee = (NdsObjectACL)trustees.next();
String attrName = trustee.getProtectedAttrName();
String subjectName = trustee.getSubjectName();
if (attrName.equalsIgnoreCase("[Entry Rights]"))
{
StringBuffer privs = new StringBuffer();
int rawprivs = (int)trustee.getPrivileges();
if ((rawprivs & 0x1) != 0)
privs.append("Browse ");
if ((rawprivs & 0x2) != 0)
privs.append("Add ");
if ((rawprivs & 0x4) != 0)
privs.append("Delete ");
if ((rawprivs & 0x8) != 0)
privs.append("Rename ");
if ((rawprivs & 0x10) != 0)
privs.append("Supervisor ");
System.out.println(
"Subject name:\t"
+ subjectName
+ "\nAttribute:\t"
+ attrName
+ "\nPrivileges:\t"
+ privs);
System.out.println("----------------------------------------------");
}
}
}
System.out.println("");
return true;
}
catch (Throwable t)
{
System.out.println("Can't get the trustee list");
if (PRINT_STACK_TRACE)
t.printStackTrace();
return false;
}
}
private static DirContext createInitialContext(String _providerURL)
{
System.out.println("\nLooking up "
+ _providerURL
);
try
{
Hashtable hash = new Hashtable(11);
hash.put(Context.INITIAL_CONTEXT_FACTORY,
Environment.NDS_INITIAL_CONTEXT_FACTORY);
hash.put(Context.PROVIDER_URL, _providerURL);
InitialDirContext initDirCtx = new InitialDirContext(hash);
return ((DirContext)initDirCtx.lookup(""));
}
catch (NamingException ne)
{
System.out.println("\nCouldn't lookup "
+ _providerURL);
System.out.println("\nOperation failed");
if (PRINT_STACK_TRACE)
ne.printStackTrace();
System.exit(-1);
}
return null;
}
private static void parseArgs(String[] args)
{
if (args.length < 1)
help();
if ((args[0].equals("/?")) || (args[0].equals("-h")))
help();
for (int i=0; i < args.length; i++)
{
if (args[i].startsWith("-", 0))
{
if (args[i].equalsIgnoreCase("-a"))
ADD_OPTION = true;
else if (args[i].equalsIgnoreCase("-d"))
DELETE_OPTION = true;
else if (args[i].equalsIgnoreCase("-u"))
{
URL_SPECIFIED = true;
if (i == args.length -1)
help();
i++;
PROVIDER_URL = new StringBuffer(args[i]);
}
else if (args[i].equalsIgnoreCase("-r"))
{
if (i == args.length -1)
help();
i++;
System.out.println("\nAssigning the following rights:");
System.out.print("[Browse] ");
String rights = args[i].toLowerCase();
int rights_count = rights.length();
for (int c=0; c < rights_count; c++)
{
if (rights.charAt(c) == new String("a").charAt(0))
{
System.out.print("[Add] ");
ENTRY_RIGHTS |= NdsObjectRights.DS_ENTRY_ADD;
}
else if (rights.charAt(c) == new String("d").charAt(0))
{
System.out.print("[Delete] ");
ENTRY_RIGHTS |= NdsObjectRights.DS_ENTRY_DELETE;
}
else if (rights.charAt(c) == new String("r").charAt(0))
{
System.out.print("[Rename] ");
ENTRY_RIGHTS |= NdsObjectRights.DS_ENTRY_RENAME;
}
else if (rights.charAt(c) == new String("s").charAt(0))
{
System.out.print("[Supervisor] ");
ENTRY_RIGHTS |= NdsObjectRights.DS_ENTRY_SUPERVISOR;
}
}
System.out.println("");
}
else if (args[i].equalsIgnoreCase("-p"))
PRINT_STACK_TRACE = true;
}
else
{
if ((ADD_OPTION || DELETE_OPTION || URL_SPECIFIED)
&&
((OBJECT_1.toString().equals("")))
||
(args.length == 1)
)
OBJECT_1 = new StringBuffer(args[i]);
else
OBJECT_2 = new StringBuffer(args[i]);
}
}
}
private static void help()
{
System.out.println("Acl -- access to NDS object ACLs through Java * ");
System.out.println("usage: java Acl [-a] [-d] [-u url] object [trustee]");
System.out.println("\nOPTIONS\n");
System.out.println("-a\tadds trustee to ACL of object");
System.out.println("-d\tdeletes trustee from ACL of object");
System.out.println("-p\tprints stack traces (no stack traces by default)");
System.out.println("-r\tsets the entry rights for trustee (only with the -a option)");
System.out.println("\tPermissible values:\n\ta (add)");
System.out.println("\tb (browse)");
System.out.println("\td (delete)");
System.out.println("\tr (rename)");
System.out.println("\ts (supervisor)");
System.out.println("-u\tspecifies the NDS URL as a base");
System.out.println("\nPARAMETERS\n");
System.out.println("url\tthe URL of the tree that contains object1");
System.out.println("\t(and if specified, object2) takes the form of nds://[treename]/");
System.out.println("object1\tobject with the target ACL");
System.out.println("object2\tobject to be added to object1's ACL");
System.out.println("\t(used in conjunction with [-a] or [-d])");
System.out.println("\nCAVEATS\n");
System.out.println(" * Requires an authenticated connection as a user with sufficient rights.");
System.out.println(" * Parms can be specified in any order.");
System.out.println(" * URL requires a trailing slash.");
System.out.println(" * To print out the ACL for an object, only specify that object as a parm.");
System.out.println(" * Granting rights only affects the entry, not the attributes.");
System.out.println(" * In an add operation, omitting the -r option sets browse rights by default.");
System.out.println(" Also, any addition of rights includes the browse option.");
System.out.println("\nEXAMPLES\n");
System.out.println("java Acl nds://superstring/admin.command");
System.out.println("\tPrints out ACL for admin.command");
System.out.println("java Acl -a -u nds://superstring/ admin.command resonance.command");
System.out.println("\tAdds resonance.command to admin.command's ACL with browse rights");
System.out.println("java Acl -d -u nds://superstring/ admin.command resonance.command");
System.out.println("\tDeletes resonance.command from admin.command's ACL");
System.out.println("java Acl -a -r s -u nds://superstring/ admin.command resonance.command");
System.out.println("\tAdds resonance.command to admin.command's ACL with supervisor rights");
System.out.println("java Acl -a -r ad -u nds://superstring/ admin.command resonance.command");
System.out.println("\tAdds resonance.command to admin.command's ACL with add and delete rights");
System.exit(-1);
}
}