diff options
| author | ogattaz <olivier@gattaz.com> | 2014-06-05 16:51:07 +0200 |
|---|---|---|
| committer | ogattaz <olivier@gattaz.com> | 2014-06-05 16:51:07 +0200 |
| commit | 2fd193d7936084de91eae46e8c2763914d87ab71 (patch) | |
| tree | b136ed97e535f11d4b3433d16c26570c89430ce4 /src/main/java/com/c2kernel/lookup | |
| parent | 1225792532f77e6e8f4a9addfc0c0a6cf56e89b8 (diff) | |
| parent | e73468fd08cc27aa31f76a27c916e45d5987c628 (diff) | |
Merge branch 'master' of ssh://dev.cccs.uwe.ac.uk:22/var/git/cristal-kernel
Diffstat (limited to 'src/main/java/com/c2kernel/lookup')
21 files changed, 1220 insertions, 1080 deletions
diff --git a/src/main/java/com/c2kernel/lookup/AgentPath.java b/src/main/java/com/c2kernel/lookup/AgentPath.java index 5ff6988..a0bb7fd 100644 --- a/src/main/java/com/c2kernel/lookup/AgentPath.java +++ b/src/main/java/com/c2kernel/lookup/AgentPath.java @@ -15,12 +15,8 @@ import java.security.NoSuchAlgorithmException; import org.apache.xerces.impl.dv.util.Base64;
-import com.c2kernel.common.ObjectCannotBeUpdated;
import com.c2kernel.common.ObjectNotFoundException;
import com.c2kernel.process.Gateway;
-import com.novell.ldap.LDAPAttribute;
-import com.novell.ldap.LDAPAttributeSet;
-import com.novell.ldap.LDAPEntry;
/**
@@ -29,14 +25,14 @@ import com.novell.ldap.LDAPEntry; * @version $Revision: 1.12 $ $Date: 2005/10/13 08:15:00 $
* @author $Author: abranson $
**/
-public class AgentPath extends EntityPath
+public class AgentPath extends ItemPath
{
private String mAgentName=null;
private String mPassword=null;
public AgentPath(int syskey, String agentName)
- throws InvalidAgentPathException,InvalidEntityPathException
+ throws InvalidAgentPathException,InvalidItemPathException
{
super(syskey);
if (agentName!=null && agentName.length()>0)
@@ -46,16 +42,16 @@ public class AgentPath extends EntityPath }
public AgentPath(int syskey)
- throws InvalidEntityPathException
+ throws InvalidItemPathException
{
super(syskey);
}
- public AgentPath(EntityPath entity) {
+ public AgentPath(ItemPath entity) {
super();
try {
setSysKey(entity.getSysKey());
- } catch (InvalidEntityPathException ex) {
+ } catch (InvalidItemPathException ex) {
//won't happen as the entity path was valid
}
}
@@ -70,8 +66,7 @@ public class AgentPath extends EntityPath if (mAgentName==null)
{
try {
- LDAPEntry agentEntry = LDAPLookupUtils.getEntry(Gateway.getLDAPLookup().getConnection(), this.getDN() + mLocalPath);
- mAgentName = LDAPLookupUtils.getFirstAttributeValue(agentEntry,"uid");
+ mAgentName = Gateway.getLookup().getAgentName(this);
} catch (ObjectNotFoundException e) {
mAgentName = "";
}
@@ -81,16 +76,16 @@ public class AgentPath extends EntityPath public RolePath[] getRoles()
{
- return Gateway.getLDAPLookup().getRoleManager().getRoles(this);
+ return Gateway.getLookup().getRoles(this);
}
public boolean hasRole(RolePath role) {
- return Gateway.getLDAPLookup().getRoleManager().hasRole(this, role);
+ return Gateway.getLookup().hasRole(this, role);
}
public boolean hasRole(String role) {
try {
- return hasRole(Gateway.getLDAPLookup().getRoleManager().getRolePath(role));
+ return hasRole(Gateway.getLookup().getRolePath(role));
} catch (ObjectNotFoundException ex) {
return false;
}
@@ -113,7 +108,7 @@ public class AgentPath extends EntityPath mAgentName;
}
- static String generateUserPassword(String pass, String algo) throws NoSuchAlgorithmException {
+ public static String generateUserPassword(String pass, String algo) throws NoSuchAlgorithmException {
MessageDigest sha = MessageDigest.getInstance(algo);
sha.reset();
sha.update(pass.getBytes());
@@ -123,32 +118,5 @@ public class AgentPath extends EntityPath return digest.toString();
}
- @Override
- public LDAPAttributeSet createAttributeSet() throws ObjectCannotBeUpdated
- {
- LDAPAttributeSet attrs = new LDAPAttributeSet();
- attrs.add(new LDAPAttribute("objectclass","cristalagent"));
- attrs.add(new LDAPAttribute("intsyskey",Integer.toString(mSysKey)));
- attrs.add(new LDAPAttribute("cn", getPath()[getPath().length-1]));
- if (mIOR != null)
- attrs.add(new LDAPAttribute("ior", Gateway.getORB().object_to_string(mIOR)));
-
- if (mAgentName!=null && mAgentName.length()>0)
- attrs.add(new LDAPAttribute("uid",mAgentName));
- else
- throw new ObjectCannotBeUpdated("Cannot create agent. No userId specified", "");
-
- if (mPassword!=null && mPassword.length()>0)
- try {
- attrs.add(new LDAPAttribute("userPassword",generateUserPassword(mPassword, "SHA")));
- } catch (NoSuchAlgorithmException ex) {
- throw new ObjectCannotBeUpdated("Cryptographic libraries for password hashing not found.", "");
- }
- else
- throw new ObjectCannotBeUpdated("Cannot create agent. No password given", "");
-
- return attrs;
- }
-
}
diff --git a/src/main/java/com/c2kernel/lookup/DomainPath.java b/src/main/java/com/c2kernel/lookup/DomainPath.java index ce849ce..706719e 100644 --- a/src/main/java/com/c2kernel/lookup/DomainPath.java +++ b/src/main/java/com/c2kernel/lookup/DomainPath.java @@ -13,8 +13,6 @@ package com.c2kernel.lookup; import com.c2kernel.common.ObjectNotFoundException;
import com.c2kernel.process.Gateway;
import com.c2kernel.utils.Logger;
-import com.novell.ldap.LDAPAttribute;
-import com.novell.ldap.LDAPAttributeSet;
/**
@@ -23,7 +21,7 @@ import com.novell.ldap.LDAPAttributeSet; **/
public class DomainPath extends Path
{
- private EntityPath target = null;
+ private ItemPath target = null;
protected static String mTypeRoot;
/* Very simple extension to Path. Only copies constructors and defines root */
@@ -49,7 +47,7 @@ public class DomainPath extends Path super(path, Path.UNKNOWN);
}
- public DomainPath(String path, EntityPath entity) throws InvalidEntityPathException
+ public DomainPath(String path, ItemPath entity) throws InvalidItemPathException
{
super(path, Path.UNKNOWN);
setEntity(entity);
@@ -76,7 +74,7 @@ public class DomainPath extends Path return new DomainPath(parentPath);
}
- public void setEntity(EntityPath newTarget) {
+ public void setEntity(ItemPath newTarget) {
if (newTarget == null) { // clear
target = null;
mType = Path.CONTEXT;
@@ -88,7 +86,7 @@ public class DomainPath extends Path }
@Override
- public EntityPath getEntity() throws ObjectNotFoundException {
+ public ItemPath getEntity() throws ObjectNotFoundException {
if (mType == UNKNOWN) { // must decide
checkType();
}
@@ -108,8 +106,8 @@ public class DomainPath extends Path public void checkType() {
try {
- setEntity(Gateway.getLDAPLookup().resolvePath(this));
- } catch (InvalidEntityPathException ex) {
+ setEntity(Gateway.getLookup().resolvePath(this));
+ } catch (InvalidItemPathException ex) {
Logger.error(ex);
mType = CONTEXT;
} catch (ObjectNotFoundException ex) {
@@ -137,22 +135,5 @@ public class DomainPath extends Path }
else return INVALID;
}
-
- @Override
- public LDAPAttributeSet createAttributeSet() {
- LDAPAttributeSet attrs = new LDAPAttributeSet();
- attrs.add(new LDAPAttribute("cn",getName()));
- if (getType() == ENTITY) {
- String objectclass_values[] = { "alias", "aliasObject" };
- attrs.add(new LDAPAttribute("objectclass",objectclass_values));
- attrs.add(new LDAPAttribute("aliasedObjectName",target.getFullDN()));
- }
-
- else
- {
- attrs.add(new LDAPAttribute("objectclass","cristalcontext"));
- }
- return attrs;
- }
}
diff --git a/src/main/java/com/c2kernel/lookup/InvalidAgentPathException.java b/src/main/java/com/c2kernel/lookup/InvalidAgentPathException.java index be4d952..a74e1ee 100644 --- a/src/main/java/com/c2kernel/lookup/InvalidAgentPathException.java +++ b/src/main/java/com/c2kernel/lookup/InvalidAgentPathException.java @@ -1,6 +1,6 @@ package com.c2kernel.lookup;
-public class InvalidAgentPathException extends InvalidEntityPathException {
+public class InvalidAgentPathException extends InvalidItemPathException {
public InvalidAgentPathException() {
super();
diff --git a/src/main/java/com/c2kernel/lookup/InvalidEntityPathException.java b/src/main/java/com/c2kernel/lookup/InvalidEntityPathException.java deleted file mode 100644 index 27c754d..0000000 --- a/src/main/java/com/c2kernel/lookup/InvalidEntityPathException.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.c2kernel.lookup;
-
-public class InvalidEntityPathException extends Exception {
-
- public InvalidEntityPathException() {
- super();
- }
-
- public InvalidEntityPathException(String msg) {
- super(msg);
- }
-
-}
diff --git a/src/main/java/com/c2kernel/lookup/InvalidItemPathException.java b/src/main/java/com/c2kernel/lookup/InvalidItemPathException.java new file mode 100644 index 0000000..58174a8 --- /dev/null +++ b/src/main/java/com/c2kernel/lookup/InvalidItemPathException.java @@ -0,0 +1,13 @@ +package com.c2kernel.lookup;
+
+public class InvalidItemPathException extends InvalidPathException {
+
+ public InvalidItemPathException() {
+ super();
+ }
+
+ public InvalidItemPathException(String msg) {
+ super(msg);
+ }
+
+}
diff --git a/src/main/java/com/c2kernel/lookup/InvalidPathException.java b/src/main/java/com/c2kernel/lookup/InvalidPathException.java new file mode 100644 index 0000000..77887cf --- /dev/null +++ b/src/main/java/com/c2kernel/lookup/InvalidPathException.java @@ -0,0 +1,13 @@ +package com.c2kernel.lookup;
+
+public class InvalidPathException extends Exception {
+
+ public InvalidPathException() {
+ super();
+ }
+
+ public InvalidPathException(String msg) {
+ super(msg);
+ }
+
+}
diff --git a/src/main/java/com/c2kernel/lookup/EntityPath.java b/src/main/java/com/c2kernel/lookup/ItemPath.java index 4f9b771..17e5659 100644 --- a/src/main/java/com/c2kernel/lookup/EntityPath.java +++ b/src/main/java/com/c2kernel/lookup/ItemPath.java @@ -12,11 +12,7 @@ package com.c2kernel.lookup; import java.util.ArrayList;
-import com.c2kernel.common.ObjectCannotBeUpdated;
import com.c2kernel.common.ObjectNotFoundException;
-import com.c2kernel.process.Gateway;
-import com.novell.ldap.LDAPAttribute;
-import com.novell.ldap.LDAPAttributeSet;
/**
@@ -25,7 +21,7 @@ import com.novell.ldap.LDAPAttributeSet; * @version $Revision: 1.14 $ $Date: 2006/03/03 13:52:21 $
* @author $Author: abranson $
**/
-public class EntityPath extends Path
+public class ItemPath extends Path
{
// no of components in a syskey
public static final int elementNo = 3;
@@ -40,14 +36,14 @@ public class EntityPath extends Path * The object decides that for itself from the number of components
*/
- public EntityPath(int syskey) throws InvalidEntityPathException {
+ public ItemPath(int syskey) throws InvalidItemPathException {
super();
setSysKey(syskey);
}
/*
*/
- public EntityPath()
+ public ItemPath()
{
super();
}
@@ -55,7 +51,7 @@ public class EntityPath extends Path /*
*/
- public EntityPath(String[] path) throws InvalidEntityPathException
+ public ItemPath(String[] path) throws InvalidItemPathException
{
super(path, Path.CONTEXT); // dummy - it will get replaced in checkSysPath()
checkSysPath();
@@ -63,7 +59,7 @@ public class EntityPath extends Path /*
*/
- public EntityPath(String path) throws InvalidEntityPathException
+ public ItemPath(String path) throws InvalidItemPathException
{
super(path, Path.CONTEXT);
checkSysPath();
@@ -71,7 +67,7 @@ public class EntityPath extends Path /*
*/
- public EntityPath(EntityPath parent, String child) throws InvalidEntityPathException {
+ public ItemPath(ItemPath parent, String child) throws InvalidItemPathException {
super(parent, child);
checkSysPath();
}
@@ -83,7 +79,7 @@ public class EntityPath extends Path }
@Override
- public EntityPath getEntity() throws ObjectNotFoundException {
+ public ItemPath getEntity() throws ObjectNotFoundException {
return this;
}
@@ -101,22 +97,22 @@ public class EntityPath extends Path if (mSysKey == Path.INVALID && mType == Path.ENTITY)
try {
if (mPath.length != elementNo)
- throw new InvalidEntityPathException("Incorrect number of components for a system key");
+ throw new InvalidItemPathException("Incorrect number of components for a system key");
mSysKey = 0;
for (String keypart : mPath) {
if (keypart.length()!=elementLen+1)
- throw new InvalidEntityPathException("Component '"+keypart+"' is not the correct size for a system key");
+ throw new InvalidItemPathException("Component '"+keypart+"' is not the correct size for a system key");
for(int j=1; j<elementLen+1; j++) // skip the 'd' prefix
{
char c = keypart.charAt(j);
if((c >= '0') && (c <='9'))
mSysKey = mSysKey*10 + c - '0';
else
- throw new InvalidEntityPathException("Component '"+keypart+"' contained a non-numeric char (excluding first char 'd').");
+ throw new InvalidItemPathException("Component '"+keypart+"' contained a non-numeric char (excluding first char 'd').");
}
}
- } catch (InvalidEntityPathException ex) {
+ } catch (InvalidItemPathException ex) {
mSysKey = Path.INVALID;
}
return mSysKey;
@@ -124,10 +120,10 @@ public class EntityPath extends Path /** Sets path from int syskey
*/
- public void setSysKey(int sysKey) throws InvalidEntityPathException
+ public void setSysKey(int sysKey) throws InvalidItemPathException
{
if (sysKey < 0 || sysKey > maxSysKey)
- throw new InvalidEntityPathException("System key "+sysKey+" out of range");
+ throw new InvalidItemPathException("System key "+sysKey+" out of range");
String stringPath = Integer.toString(sysKey);
ArrayList<String> newKey = new ArrayList<String>();
for (int i=0; i<elementNo; i++) {
@@ -147,29 +143,17 @@ public class EntityPath extends Path mPath = (newKey.toArray(mPath));
mSysKey = sysKey;
mStringPath = null;
- mDN = null;
mType = Path.ENTITY;
checkSysPath();
}
- public void checkSysPath() throws InvalidEntityPathException {
+ public void checkSysPath() throws InvalidItemPathException {
if (mPath.length > elementNo)
- throw new InvalidEntityPathException("EntityPath cannot have more than "+elementNo+" components: "+toString());
+ throw new InvalidItemPathException("EntityPath cannot have more than "+elementNo+" components: "+toString());
if (mPath.length == elementNo)
mType = Path.ENTITY;
else
mType = Path.CONTEXT;
}
-
- @Override
- public LDAPAttributeSet createAttributeSet() throws ObjectCannotBeUpdated {
- LDAPAttributeSet attrs = new LDAPAttributeSet();
- attrs.add(new LDAPAttribute("objectclass","cristalentity"));
- attrs.add(new LDAPAttribute("intsyskey",Integer.toString(mSysKey)));
- attrs.add(new LDAPAttribute("cn", getPath()[getPath().length-1]));
- if (mIOR != null)
- attrs.add(new LDAPAttribute("ior", Gateway.getORB().object_to_string(mIOR)));
- return attrs;
- }
}
diff --git a/src/main/java/com/c2kernel/lookup/LDAPLookup.java b/src/main/java/com/c2kernel/lookup/LDAPLookup.java deleted file mode 100644 index 08a80b6..0000000 --- a/src/main/java/com/c2kernel/lookup/LDAPLookup.java +++ /dev/null @@ -1,493 +0,0 @@ -/*
- * Directory Lookup Service *
- * author: Florida Estrella
-*/
-
-package com.c2kernel.lookup;
-
-import java.util.StringTokenizer;
-
-import com.c2kernel.common.ObjectAlreadyExistsException;
-import com.c2kernel.common.ObjectCannotBeUpdated;
-import com.c2kernel.common.ObjectNotFoundException;
-import com.c2kernel.entity.TraceableEntity;
-import com.c2kernel.entity.agent.ActiveEntity;
-import com.c2kernel.entity.proxy.EntityProxyManager;
-import com.c2kernel.entity.proxy.ProxyMessage;
-import com.c2kernel.process.Gateway;
-import com.c2kernel.property.PropertyDescription;
-import com.c2kernel.property.PropertyDescriptionList;
-import com.c2kernel.utils.Logger;
-import com.novell.ldap.LDAPAttributeSet;
-import com.novell.ldap.LDAPConnection;
-import com.novell.ldap.LDAPDN;
-import com.novell.ldap.LDAPEntry;
-import com.novell.ldap.LDAPException;
-import com.novell.ldap.LDAPSearchConstraints;
-import com.novell.ldap.LDAPSearchResults;
-
-/**
- * The LDAPLookup object, statically accessible through the Gateway, manages
- * the LDAP connection for the cristal process. It provides:
- * <ul>
- * <li>Authentication - returning an AgentProxy object if a user has logged in
- * <li>System key generation - through the NextKeyManager
- * <li>Agent and Role lookup/modification - through the RoleManager
- * <li>
- * @version $Revision: 1.113 $ $Date: 2006/03/03 13:52:21 $
- * @author $Author: abranson $
- */
-
-public class LDAPLookup
-
-{
- private LDAPConnection mLDAPConn;
- private final LDAPProperties mLDAPProps;
- private final NextKeyManager mNextKeyManager;
- private final LDAPPropertyManager mPropManager;
- private final LDAPRoleManager mRoleManager;
-
-
-
- /**
- * Creates a new LDAPLookup manager with the properties supplied.
- * This should be only done by the Gateway during initialisation.
- *
- * @param props The LDAP properties object that extracts LDAP connection properties from the global c2kprops
- */
- public LDAPLookup(LDAPProperties props) throws LDAPException
- {
- Logger.msg(8,"LDAPLookup - initialising.");
-
- mLDAPProps = props;
-
- mLDAPConn = createConnection(mLDAPProps);
-
- Path.mGlobalPath=props.mGlobalPath;
- Path.mRootPath=props.mRootPath;
- Path.mLocalPath=props.mLocalPath;
-
- EntityPath.mTypeRoot = "cn=entity,"+props.mLocalPath;
- DomainPath.mTypeRoot = "cn=domain,"+props.mLocalPath;
-
- mNextKeyManager = new NextKeyManager(this, "cn=last,"+EntityPath.mTypeRoot);
- Logger.msg(7, "LDAP.useOldProps="+Gateway.getProperties().getBoolean("LDAP.useOldProps", false));
- mPropManager = new LDAPPropertyManager(this);
- mRoleManager = new LDAPRoleManager(this, "cn=agent,"+DomainPath.mTypeRoot, EntityPath.mTypeRoot);
-
- }
-
- /**
- * Utility method to connect to an LDAP server
- * @param lp LDAP properties to connect with
- * @return a novell LDAPConnection object
- * @throws LDAPException when the connection was unsuccessful
- */
- public static LDAPConnection createConnection(LDAPProperties lp) throws LDAPException {
- LDAPConnection ld = new LDAPConnection();
-
- Logger.msg(3, "LDAPLookup - connecting to " + lp.mHost);
- ld.connect(lp.mHost, Integer.valueOf(lp.mPort).intValue());
-
- Logger.msg(3, "LDAPLookup - authenticating user:" + lp.mUser);
- ld.bind( LDAPConnection.LDAP_V3, lp.mUser,
- String.valueOf(lp.mPassword).getBytes());
-
- Logger.msg(3, "LDAPLookup - authentication successful");
- LDAPSearchConstraints searchCons = new LDAPSearchConstraints();
- searchCons.setMaxResults(0);
- ld.setConstraints(searchCons);
-
- return ld;
- }
-
- /**
- * Gets the entity key generator, used to get a unique system key for new entities.
- * @return the global NextKeyManager
- */
- public NextKeyManager getNextKeyManager()
- {
- return mNextKeyManager;
- }
-
- /**
- * Gets the property manager, that is used to read and write cristal properties to the LDAP store.
- * @return Returns the global LDAPPropertyManager.
- */
- public LDAPPropertyManager getPropManager() {
- return mPropManager;
- }
- /**
- * Gets the role manager, that is used to add and remove roles and agents.
- * @return Returns the mRoleManager.
- */
- public LDAPRoleManager getRoleManager() {
- return mRoleManager;
- }
-
- /**
- * Returns the current LDAP connection, and attempts to reconnect if it has been closed.
- * @return
- */
- protected LDAPConnection getConnection()
- {
- if (!mLDAPConn.isConnected()) {
- Logger.warning("LDAPLookup - lost connection to LDAP server. Attempting to reconnect.");
- try {
- mLDAPConn = createConnection(mLDAPProps);
- } catch (LDAPException ex) { }
- }
- return mLDAPConn;
- }
-
- /**
- * Disconnects the connection with the LDAP server during shutdown
- */
- public void disconnect() {
- Logger.msg(1, "LDAP Lookup: Shutting down LDAP connection.");
- if (mLDAPConn != null) {
- try {
- mLDAPConn.disconnect();
- } catch (LDAPException e) {
- Logger.error(e);
- }
- mLDAPConn = null;
- }
- }
-
- /**
- * Attempts to resolve the CORBA object for a Path, either directly or through an alias.
- * @param path the path to resolve
- * @return the CORBA object
- * @throws ObjectNotFoundException When the path does not exist
- */
- public org.omg.CORBA.Object getIOR(Path path)
- throws ObjectNotFoundException
- {
- return resolveObject(path.getFullDN());
- }
-
- /**
- * Attempts to resolve the CORBA object from the IOR attribute of a DN, either directly or through an alias
- * @param dn The String dn
- * @throws ObjectNotFoundException when the dn or aliased dn does not exist
- */
- private org.omg.CORBA.Object resolveObject(String dn)
- throws ObjectNotFoundException
- {
- Logger.msg(8,"LDAPLookup.resolveObject("+dn+")");
- LDAPEntry anEntry = LDAPLookupUtils.getEntry(getConnection(),dn,LDAPSearchConstraints.DEREF_NEVER);
- if (anEntry != null)
- {
- String iorString;
- try {
- iorString = LDAPLookupUtils.getFirstAttributeValue(anEntry, "ior");
- org.omg.CORBA.Object ior=Gateway.getORB().string_to_object(iorString);
- if (ior!=null)
- return ior;
- else
- throw new ObjectNotFoundException("LDAPLookup.resolveObject() - " + dn + " has no IOR", "");
- } catch (ObjectNotFoundException ex) {
- return resolveObject(LDAPLookupUtils.getFirstAttributeValue(anEntry,"aliasedObjectName"));
- }
- }
- else
- throw new ObjectNotFoundException("LDAPLookup.resolveObject() LDAP node " + dn + " is not in LDAP or has no IOR.", "");
- }
-
- /**
- *
- * @param domPath
- * @return
- * @throws InvalidEntityPathException
- * @throws ObjectNotFoundException
- */
- protected EntityPath resolvePath(DomainPath domPath)
- throws InvalidEntityPathException, ObjectNotFoundException {
- EntityPath referencedPath = null;
- LDAPEntry domEntry = LDAPLookupUtils.getEntry(getConnection(), domPath
- .getFullDN(), LDAPSearchConstraints.DEREF_ALWAYS);
- String entityKey = LDAPLookupUtils.getFirstAttributeValue(domEntry,
- "intsyskey");
- Logger.msg(7, "DomainPath " + domPath + " is a reference to "
- + entityKey);
- String objClass = LDAPLookupUtils.getFirstAttributeValue(domEntry,
- "objectClass");
- if (objClass.equals("cristalagent"))
- referencedPath = new AgentPath(Integer.parseInt(entityKey));
- else
- referencedPath = new EntityPath(Integer.parseInt(entityKey));
-
- return referencedPath;
- }
-
-
- public LDAPEntry add(Path path)
- throws ObjectCannotBeUpdated, ObjectAlreadyExistsException
- {
- try {
- checkLDAPContext(path);
- LDAPAttributeSet attrSet = path.createAttributeSet();
- LDAPEntry newEntry = new LDAPEntry(path.getFullDN(),attrSet);
- LDAPLookupUtils.addEntry(getConnection(),newEntry);
- if (path instanceof DomainPath)
- EntityProxyManager.sendProxyEvent(new ProxyMessage(ProxyMessage.NA, path.toString(), ProxyMessage.ADDED));
- return newEntry;
- } catch (LDAPException ex) {
- if (ex.getResultCode() == LDAPException.ENTRY_ALREADY_EXISTS)
- throw new ObjectAlreadyExistsException(ex.getLDAPErrorMessage(), "");
- else
- throw new ObjectCannotBeUpdated(ex.getLDAPErrorMessage(), "");
- }
- }
-
- //deletes a node
- //throws LDAPexception if node cannot be deleted (eg node is not a leaf)
- public void delete(Path path) throws ObjectCannotBeUpdated
- {
- try {
- LDAPLookupUtils.delete(getConnection(),path.getDN()+Path.mLocalPath);
- } catch (LDAPException ex) {
- throw new ObjectCannotBeUpdated(ex.getLDAPErrorMessage(), "");
- }
- if (path instanceof DomainPath) {
- EntityProxyManager.sendProxyEvent(new ProxyMessage(ProxyMessage.NA, path.toString(), ProxyMessage.DELETED));
- }
- }
-
- //change specs, add boolean alias leaf context
- protected void checkLDAPContext(Path path)
- {
- String dn = path.getFullDN();
- if (!LDAPLookupUtils.exists(getConnection(),dn))
- {
- String listDN[] = path.getPath();
- String name = "cn="+ path.getRoot() + "," + Path.mLocalPath;
- int i=0;
- while (i<listDN.length-1)
- {
- name= "cn="+LDAPLookupUtils.escapeDN(listDN[i])+","+name;
- if (!LDAPLookupUtils.exists(getConnection(),name))
- {
- try
- {
- //create cristalcontext
- Logger.msg(8,"LDAPLookup::addLDAPContext() context added " + name);
- LDAPLookupUtils.createCristalContext(getConnection(), name);
- }
- catch (Exception ex)
- {
- Logger.error("LDAPLookup::addContext() " + ex);
- }
- }
- i++;
- }
- }
- }
- public void createBootTree()
- {
- Logger.msg(8,"Initializing LDAP Boot tree");
-
- //create org
- LDAPLookupUtils.createOrganizationContext(getConnection(), Path.mGlobalPath);
- //create root
- LDAPLookupUtils.createCristalContext(getConnection(), Path.mRootPath);
- //create local
- LDAPLookupUtils.createCristalContext(getConnection(), Path.mLocalPath);
- }
-
- public void install() throws ObjectNotFoundException
- {
- createBootTree();
- initTree( Gateway.getResource().getTextResource(null, "boot/LDAPboot.txt"));
- }
-
- public void initTree(String bootFile)
- {
- Logger.msg(8,"Verifying Cristal LDAP roots");
- StringTokenizer strTokenizer = new StringTokenizer(bootFile, "\n\r");
- while (strTokenizer.hasMoreTokens())
- {
- String line = strTokenizer.nextToken();
- Logger.msg(8,"Checking " + line+Path.mLocalPath);
- LDAPLookupUtils.createCristalContext(getConnection(), line+Path.mLocalPath);
- }
-
- }
-
- //typically search for cn=barcode
- public LDAPPathSet search(Path start, String filter)
- {
- Logger.msg(8,"LDAPLookup::search() From " + start.getDN() + " for cn=" + filter );
- return search(start.getFullDN(),"cn="+LDAPLookupUtils.escapeSearchFilter(filter));
- }
-
- public LDAPPathSet search(Path start, String propname, String propvalue)
- {
- String val = propname+":"+propvalue;
- Logger.msg(8,"LDAPLookup::search() From " + start.getDN() + " for cristalprop=" + val );
- LDAPSearchConstraints searchCons = new LDAPSearchConstraints();
- searchCons.setBatchSize(0);
- searchCons.setDereference(LDAPSearchConstraints.DEREF_SEARCHING);
- return search(start.getFullDN(),LDAPConnection.SCOPE_SUB,"cristalprop="+LDAPLookupUtils.escapeSearchFilter(val),searchCons);
- }
-
- public LDAPPathSet search(Path start, PropertyDescriptionList props) {
- StringBuffer query = new StringBuffer();
- int propCount = 0;
- for (PropertyDescription propDesc: props.list) {
- if (propDesc.getIsClassIdentifier()) {
- String thisProp = LDAPLookupUtils.escapeSearchFilter(propDesc.getName()+(":")+propDesc.getDefaultValue());
- query.append("(cristalprop=").append(thisProp).append(")");
- propCount++;
- }
- }
- LDAPSearchConstraints searchCons = new LDAPSearchConstraints();
- searchCons.setBatchSize(0);
- searchCons.setDereference(LDAPSearchConstraints.DEREF_SEARCHING);
- if (propCount == 0)
- return search(start.getFullDN(),LDAPConnection.SCOPE_SUB,"objectClass=cristalentity",searchCons);
- else if (propCount == 1)
- return search(start.getFullDN(),LDAPConnection.SCOPE_SUB,query.toString(),searchCons);
- else
- return search(start.getFullDN(),LDAPConnection.SCOPE_SUB,"(&"+query.toString()+")",searchCons);
- }
-
- public LDAPPathSet searchType(Path start, Path type) {
- return null;
- }
-
- protected LDAPPathSet search(String startDN, int scope, String filter, LDAPSearchConstraints searchCons)
- {
- Logger.msg(8,"Searching for "+filter+" in "+startDN);
- searchCons.setMaxResults(0);
- String[] attr = { LDAPConnection.ALL_USER_ATTRS };
- try
- {
- LDAPSearchResults res = getConnection().search(startDN, scope,
- filter,attr,false,searchCons);
- return new LDAPPathSet(res);
- }
- catch (LDAPException ex)
- {
- Logger.error("LDAPException::LDAPLookup::search() " + ex.toString());
- return new LDAPPathSet();
- }
- }
- //typically search for (any filter combination)
- public LDAPPathSet search(String startDN,String filter)
- {
- LDAPSearchConstraints searchCons = new LDAPSearchConstraints();
- searchCons.setBatchSize(0);
- searchCons.setDereference(LDAPSearchConstraints.DEREF_NEVER);
- return search(startDN,LDAPConnection.SCOPE_SUB,filter,searchCons);
- }
-
- public LDAPPathSet searchEntities(Path start) {
- LDAPSearchConstraints searchCons = new LDAPSearchConstraints();
- searchCons.setBatchSize(0);
- searchCons.setDereference(LDAPSearchConstraints.DEREF_SEARCHING);
- return search(start.getFullDN(), LDAPConnection.SCOPE_SUB, "objectClass=cristalentity", searchCons);
- }
-
- public LDAPPathSet searchAliases(DomainPath start) {
- LDAPSearchConstraints searchCons = new LDAPSearchConstraints();
- searchCons.setBatchSize(0);
- searchCons.setDereference(LDAPSearchConstraints.DEREF_NEVER);
- return search(start.getFullDN(), LDAPConnection.SCOPE_SUB, "objectClass=aliasObject", searchCons);
- }
-
- public boolean exists(Path path) {
- return LDAPLookupUtils.exists(getConnection(), path.getFullDN());
- }
-
- public Class<?> getEntityClass(Path path) throws ObjectNotFoundException {
- String[] attr = { LDAPConnection.ALL_USER_ATTRS };
- try {
- LDAPEntry anEntry=getConnection().read(path.getDN()+Path.mLocalPath,attr);
- String type = LDAPLookupUtils.getFirstAttributeValue(anEntry, "objectClass");
- if (type.equals("cristalentity"))
- return TraceableEntity.class;
- else if (type.equals("cristalagent"))
- return ActiveEntity.class;
- else
- throw new ObjectNotFoundException("Not an entity", "");
-
- } catch (LDAPException ex) {
- if (ex.getResultCode() == LDAPException.NO_SUCH_OBJECT)
- throw new ObjectNotFoundException("Entity does not exist", "");
- Logger.error(ex);
- throw new ObjectNotFoundException("Error getting entity class", "");
- }
- }
-
- /** converts an LDAPentry to a Path object
- * Note that the search producing the entry should have retrieved the attrs
- * 'ior' and 'uniquemember'
- * @throws ObjectNotFoundException
- * @throws ObjectNotFoundException
- */
- protected Path nodeToPath(LDAPEntry entry) throws InvalidEntityPathException, ObjectNotFoundException
- {
- String dn = entry.getDN();
-
- // extract syskey
- int entityKey = -1;
- try {
- String entityKeyStr = LDAPLookupUtils.getFirstAttributeValue(entry,"intsyskey");
- entityKey = Integer.parseInt(entityKeyStr);
- } catch (Exception e) { }
-
- // extract IOR
- org.omg.CORBA.Object ior = null;
- try {
- String stringIOR = LDAPLookupUtils.getFirstAttributeValue(entry,"ior");
- ior = Gateway.getORB().string_to_object(stringIOR);
- } catch (ObjectNotFoundException e2) { }
-
- /* Find the right path class */
- Path thisPath;
- if (LDAPLookupUtils.existsAttributeValue(entry,"objectclass","cristalagent"))
- { //cristalagent
- String agentID = LDAPLookupUtils.getFirstAttributeValue(entry,"uid");
- thisPath = new AgentPath(entityKey, agentID);
- }
-
- else if (LDAPLookupUtils.existsAttributeValue(entry,"objectclass","cristalrole"))
- { //cristalrole
- thisPath = new RolePath(LDAPDN.explodeDN(dn,true)[0],
- LDAPLookupUtils.getFirstAttributeValue(entry, "jobList").equals("TRUE"));
- }
- else if (LDAPLookupUtils.existsAttributeValue(entry,"objectclass","aliasObject") ||
- (LDAPLookupUtils.existsAttributeValue(entry,"objectclass","cristalcontext") && dn.endsWith(DomainPath.mTypeRoot)))
- {
- DomainPath domainPath = new DomainPath();
- domainPath.setDN(dn);
- thisPath = domainPath;
- }
- else if (LDAPLookupUtils.existsAttributeValue(entry,"objectclass","cristalentity") ||
- (LDAPLookupUtils.existsAttributeValue(entry,"objectclass","cristalcontext") && dn.endsWith(EntityPath.mTypeRoot)))
- {
- if(dn.endsWith(EntityPath.mTypeRoot)) {
- EntityPath entityPath;
- if (entityKey != -1)
- entityPath = new EntityPath(entityKey);
- else {
- entityPath = new EntityPath();
- entityPath.setDN(dn);
- }
- thisPath = entityPath;
- }
- else
- throw new ObjectNotFoundException("Entity found outside entity tree");
- }
- else
- {
- throw new ObjectNotFoundException("Unrecognised LDAP entry. Not a cristal entry");
- }
-
- //set IOR if we have one
- if (ior!=null) thisPath.setIOR(ior);
- return thisPath;
- }
-}
diff --git a/src/main/java/com/c2kernel/lookup/LDAPProperties.java b/src/main/java/com/c2kernel/lookup/LDAPProperties.java deleted file mode 100644 index df0b85d..0000000 --- a/src/main/java/com/c2kernel/lookup/LDAPProperties.java +++ /dev/null @@ -1,54 +0,0 @@ -/*
- * Directory Lookup Service
-*/
-
-package com.c2kernel.lookup;
-
-import java.math.BigInteger;
-import java.security.SecureRandom;
-
-import com.c2kernel.process.Gateway;
-
-/**
- * @version $Revision: 1.16 $ $Date: 2005/10/12 12:51:54 $
- * @author $Author: abranson $
- */
-public class LDAPProperties
-{
- public String mGlobalPath = null; //o=cern.ch
- public String mRootPath = null; //cn=cristal2
- public String mLocalPath = null; //cn=lab27
- public Integer mPort = null;
- public String mHost = null;
- public String mUser = null;
- public String mPassword = null;
- public static String mGeneratedPassword = null;
- public String mDbPath = null;
-
- public LDAPProperties()
- {
- mGlobalPath = Gateway.getProperties().getProperty( "LDAP.GlobalPath" );
- mRootPath = Gateway.getProperties().getProperty( "LDAP.RootPath" );
- mLocalPath = Gateway.getProperties().getProperty( "LDAP.LocalPath" );
- mPort = Gateway.getProperties().getInt( "LDAP.port", 389 );
- mHost = Gateway.getProperties().getProperty( "LDAP.host" );
- mUser = Gateway.getProperties().getProperty( "LDAP.user" );
- mPassword = Gateway.getProperties().getProperty( "LDAP.password" );
-
- mRootPath += "," + mGlobalPath;
- mLocalPath += "," + mRootPath;
-
- }
-
-
- public void generateRootPassword() {
- if (mPassword == null) {
- if (mGeneratedPassword == null) {
- SecureRandom random = new SecureRandom();
- mGeneratedPassword = new BigInteger(130, random).toString(32);
- }
- mPassword = mGeneratedPassword;
- }
- }
-}
-
diff --git a/src/main/java/com/c2kernel/lookup/LDAPRoleManager.java b/src/main/java/com/c2kernel/lookup/LDAPRoleManager.java deleted file mode 100644 index b2cffc6..0000000 --- a/src/main/java/com/c2kernel/lookup/LDAPRoleManager.java +++ /dev/null @@ -1,211 +0,0 @@ -package com.c2kernel.lookup;
-
-import java.util.ArrayList;
-import java.util.Enumeration;
-
-import com.c2kernel.common.ObjectAlreadyExistsException;
-import com.c2kernel.common.ObjectCannotBeUpdated;
-import com.c2kernel.common.ObjectNotFoundException;
-import com.c2kernel.utils.Logger;
-import com.novell.ldap.LDAPConnection;
-import com.novell.ldap.LDAPEntry;
-import com.novell.ldap.LDAPException;
-import com.novell.ldap.LDAPSearchConstraints;
-
-/**************************************************************************
- *
- * $Revision: 1.1 $
- * $Date: 2005/04/26 06:48:12 $
- *
- * Copyright (C) 2003 CERN - European Organization for Nuclear Research
- * All rights reserved.
- **************************************************************************/
-
-// public static final String codeRevision = "$Revision: 1.1 $ $Date: 2005/04/26 06:48:12 $ $Author: abranson $";
-public class LDAPRoleManager {
-
- /**
- *
- */
- LDAPLookup mLdap;
- private final String mRolePath;
- private final String mEntityPath;
-
- public LDAPRoleManager(LDAPLookup ldap, String rolePath, String entityPath) {
- super();
- this.mLdap = ldap;
- this.mRolePath = rolePath;
- this.mEntityPath = entityPath;
- }
-
- //NOTE: A role must have at LEAST 1 userDN, cannot be empty...
- //Creates a cristalRole
- //CristalRole is-a specialized CristalContext which contains multi-valued uniqueMember attribute pointing to cristalagents
- public RolePath createRole(String roleName, boolean jobList)
- throws ObjectAlreadyExistsException, ObjectCannotBeUpdated
- {
-
- // create the role
- RolePath rolePath = new RolePath(roleName, jobList);
- String roleDN = rolePath.getFullDN();
- LDAPEntry roleNode;
- try
- {
- roleNode = LDAPLookupUtils.getEntry(mLdap.getConnection(), rolePath.getFullDN());
- throw new ObjectAlreadyExistsException();
- } catch (ObjectNotFoundException ex) { }
-
- //create CristalRole if it does not exist
- roleNode = new LDAPEntry(roleDN, rolePath.createAttributeSet());
- try {
- LDAPLookupUtils.addEntry(mLdap.getConnection(),roleNode);
- } catch (LDAPException e) {
- throw new ObjectCannotBeUpdated(e.getLDAPErrorMessage(), "");
- }
- return rolePath;
-
-
- }
- public void deleteRole(RolePath role) throws ObjectNotFoundException, ObjectCannotBeUpdated {
- try {
- LDAPLookupUtils.delete(mLdap.getConnection(), role.getFullDN());
- } catch (LDAPException ex) {
- throw new ObjectCannotBeUpdated("Could not remove role");
- }
- }
-
- protected void addRole(AgentPath agent, RolePath role)
- throws ObjectCannotBeUpdated, ObjectNotFoundException
- {
- LDAPEntry roleEntry = LDAPLookupUtils.getEntry(mLdap.getConnection(), role.getFullDN());
- //add memberDN to uniqueMember if it is not yet a member
- if (!LDAPLookupUtils.existsAttributeValue(roleEntry, "uniqueMember", agent.getFullDN()))
- LDAPLookupUtils.addAttributeValue(mLdap.getConnection(), roleEntry, "uniqueMember", agent.getFullDN());
- else
- throw new ObjectCannotBeUpdated("Agent " + agent.getAgentName() + " already has role " + role.getName());
- }
-
- protected void removeRole(AgentPath agent, RolePath role)
- throws ObjectCannotBeUpdated, ObjectNotFoundException
- {
- LDAPEntry roleEntry = LDAPLookupUtils.getEntry(mLdap.getConnection(), role.getFullDN());
- if (LDAPLookupUtils.existsAttributeValue(roleEntry, "uniqueMember", agent.getFullDN()))
- LDAPLookupUtils.removeAttributeValue(mLdap.getConnection(), roleEntry, "uniqueMember", agent.getFullDN());
- else
- throw new ObjectCannotBeUpdated("Agent did not have that role");
- }
-
- protected boolean hasRole(AgentPath agent, RolePath role) {
- String filter = "(&(objectclass=cristalrole)(uniqueMember="+agent.getFullDN()+")(cn="+role.getName()+"))";
- LDAPSearchConstraints searchCons = new LDAPSearchConstraints();
- searchCons.setBatchSize(0);
- searchCons.setDereference(LDAPSearchConstraints.DEREF_NEVER );
- return mLdap.search(mRolePath,LDAPConnection.SCOPE_SUB,filter,searchCons).hasMoreElements();
- }
-
- protected AgentPath[] getAgents(RolePath role)
- throws ObjectNotFoundException
- {
- //get the roleDN entry, and its uniqueMember entry pointing to
- LDAPEntry roleEntry;
- try {
- roleEntry = LDAPLookupUtils.getEntry(mLdap.getConnection(), role.getFullDN());
- } catch (ObjectNotFoundException e) {
- throw new ObjectNotFoundException("Role does not exist", "");
- }
-
- String[] res = LDAPLookupUtils.getAllAttributeValues(roleEntry,"uniqueMember");
- ArrayList<AgentPath> agents = new ArrayList<AgentPath>();
- for (String userDN : res) {
- try {
- LDAPEntry userEntry = LDAPLookupUtils.getEntry(mLdap.getConnection(), userDN);
- AgentPath path = (AgentPath)mLdap.nodeToPath(userEntry);
- agents.add(path);
- } catch (ObjectNotFoundException ex) {
- Logger.error("Agent "+userDN+" does not exist");
- } catch (InvalidEntityPathException ex) {
- Logger.error("Agent "+userDN+" is not a valid entity");
- }
- }
- AgentPath[] usersList = new AgentPath[0];
- usersList = agents.toArray(usersList);
- return usersList;
- }
-
- //returns the role/s of a user
- protected RolePath[] getRoles(AgentPath agentPath)
- {
- //search the mDomainPath tree uniqueMember=userDN
- //filter = objectclass=cristalrole AND uniqueMember=userDN
- String filter = "(&(objectclass=cristalrole)(uniqueMember="+agentPath.getFullDN()+"))";
- LDAPSearchConstraints searchCons = new LDAPSearchConstraints();
- searchCons.setBatchSize(0);
- searchCons.setDereference(LDAPSearchConstraints.DEREF_NEVER );
- Enumeration<?> roles = mLdap.search(mRolePath,LDAPConnection.SCOPE_SUB,filter,searchCons);
- ArrayList<RolePath> roleList = new ArrayList<RolePath>();
-
- while(roles.hasMoreElements())
- {
- RolePath path = (RolePath) roles.nextElement();
- roleList.add(path);
- }
- RolePath[] roleArr = new RolePath[roleList.size()];
- roleArr = roleList.toArray(roleArr);
- return roleArr;
- }
-
- /**
- * Utility for looking up a login name
- *
- * @param ld
- * @param agentName
- * @param baseDN
- * @return
- * @throws ObjectNotFoundException
- */
- public AgentPath getAgentPath(String agentName) throws ObjectNotFoundException
- {
- //search to get the userDN equivalent of the userID
- LDAPSearchConstraints searchCons = new LDAPSearchConstraints();
- searchCons.setBatchSize(0);
- searchCons.setDereference(LDAPSearchConstraints.DEREF_NEVER );
- String filter = "(&(objectclass=cristalagent)(uid="+agentName+"))";
- Enumeration<Path> res = mLdap.search(mEntityPath,LDAPConnection.SCOPE_SUB,filter,searchCons);
- if (!res.hasMoreElements())
- throw new ObjectNotFoundException("Agent not found");
- Path result = res.nextElement();
- if (result instanceof AgentPath)
- return (AgentPath)result;
- else
- throw new ObjectNotFoundException("Entry was not an Agent");
- }
-
- public RolePath getRolePath(String roleName) throws ObjectNotFoundException
- {
- LDAPSearchConstraints searchCons = new LDAPSearchConstraints();
- searchCons.setBatchSize(0);
- searchCons.setDereference(LDAPSearchConstraints.DEREF_NEVER );
- String filter = "(&(objectclass=cristalrole)(cn="+roleName+"))";
- Enumeration<Path> res = mLdap.search(mRolePath,LDAPConnection.SCOPE_SUB,filter,searchCons);
- if (!res.hasMoreElements())
- throw new ObjectNotFoundException("Role not found");
- Path result = res.nextElement();
- if (result instanceof RolePath)
- return (RolePath)result;
- else
- throw new ObjectNotFoundException("Entry was not a Role");
- }
-
- public void setHasJobList(RolePath role, boolean hasJobList) throws ObjectNotFoundException, ObjectCannotBeUpdated {
- // get entry
- LDAPEntry roleEntry;
- try {
- roleEntry = LDAPLookupUtils.getEntry(mLdap.getConnection(), role.getFullDN());
- } catch (ObjectNotFoundException e) {
- throw new ObjectNotFoundException("Role does not exist", "");
- }
- // set attribute
- LDAPLookupUtils.setAttributeValue(mLdap.getConnection(), roleEntry, "jobList", hasJobList?"TRUE":"FALSE");
- }
-
-}
diff --git a/src/main/java/com/c2kernel/lookup/Lookup.java b/src/main/java/com/c2kernel/lookup/Lookup.java new file mode 100644 index 0000000..026ad19 --- /dev/null +++ b/src/main/java/com/c2kernel/lookup/Lookup.java @@ -0,0 +1,77 @@ +package com.c2kernel.lookup;
+
+import java.security.NoSuchAlgorithmException;
+import java.util.Iterator;
+
+import com.c2kernel.common.ObjectAlreadyExistsException;
+import com.c2kernel.common.ObjectCannotBeUpdated;
+import com.c2kernel.common.ObjectNotFoundException;
+import com.c2kernel.process.auth.Authenticator;
+import com.c2kernel.property.PropertyDescriptionList;
+
+public interface Lookup {
+
+ public void initializeDirectory() throws ObjectNotFoundException;
+
+ public void open(Authenticator user);
+
+ public void close();
+
+ // Path resolution
+
+ public Class<?> getItemClass(Path path) throws ObjectNotFoundException;
+
+ public ItemPath resolvePath(DomainPath domainPath) throws InvalidItemPathException, ObjectNotFoundException;
+
+ public org.omg.CORBA.Object resolve(Path path) throws ObjectNotFoundException;
+
+ // Path management
+
+ public void add(Path newPath) throws ObjectCannotBeUpdated, ObjectAlreadyExistsException;
+
+ public void delete(Path path) throws ObjectCannotBeUpdated;
+
+ // Path finding and searching
+
+ public boolean exists(Path path);
+
+ public Iterator<Path> getChildren(Path path);
+
+ public Iterator<Path> search(Path path, String name);
+
+ public Iterator<Path> search(Path start, String propname, String propvalue);
+
+ public Iterator<Path> search(Path start, PropertyDescriptionList props);
+
+ public Iterator<Path> searchEntities(Path path);
+
+ public Iterator<Path> searchAliases(DomainPath start);
+
+ public Iterator<Path> searchAliases(ItemPath itemPath);
+
+ // Role and agent management
+
+ public AgentPath getAgentPath(String agentName) throws ObjectNotFoundException;
+
+ public RolePath getRolePath(String roleName) throws ObjectNotFoundException;
+
+ public RolePath createRole(String role, boolean b) throws ObjectAlreadyExistsException, ObjectCannotBeUpdated;
+
+ public void addRole(AgentPath agent, RolePath rolePath) throws ObjectCannotBeUpdated, ObjectNotFoundException;
+
+ public AgentPath[] getAgents(RolePath rolePath) throws ObjectNotFoundException;
+
+ public RolePath[] getRoles(AgentPath agentPath);
+
+ public boolean hasRole(AgentPath agentPath, RolePath role);
+
+ public void removeRole(AgentPath agent, RolePath role) throws ObjectCannotBeUpdated, ObjectNotFoundException;
+
+ public String getAgentName(AgentPath agentPath) throws ObjectNotFoundException;
+
+ public void setAgentPassword(AgentPath agent, String newPassword) throws ObjectNotFoundException, ObjectCannotBeUpdated, NoSuchAlgorithmException;
+
+ public void setHasJobList(RolePath role, boolean hasJobList) throws ObjectNotFoundException, ObjectCannotBeUpdated;
+
+
+}
diff --git a/src/main/java/com/c2kernel/lookup/NextKeyManager.java b/src/main/java/com/c2kernel/lookup/NextKeyManager.java deleted file mode 100644 index fd873fd..0000000 --- a/src/main/java/com/c2kernel/lookup/NextKeyManager.java +++ /dev/null @@ -1,71 +0,0 @@ -package com.c2kernel.lookup;
-
-import com.c2kernel.common.ObjectCannotBeUpdated;
-import com.c2kernel.common.ObjectNotFoundException;
-import com.novell.ldap.LDAPEntry;
-
-/**************************************************************************
- *
- * $Revision: 1.2 $
- * $Date: 2005/04/27 13:47:24 $
- *
- * Copyright (C) 2003 CERN - European Organization for Nuclear Research
- * All rights reserved.
- **************************************************************************/
-
-// public static final String codeRevision = "$Revision: 1.2 $ $Date: 2005/04/27 13:47:24 $ $Author: abranson $";
-public class NextKeyManager {
-
- LDAPLookup ldap;
- String lastKeyPath;
-
- public NextKeyManager(LDAPLookup ldap, String lastKeyPath) {
- super();
- this.ldap = ldap;
- this.lastKeyPath = lastKeyPath;
- }
-
- public synchronized EntityPath generateNextEntityKey()
- throws ObjectCannotBeUpdated, ObjectNotFoundException
- {
- EntityPath lastKey = getLastEntityPath();
-
- try {
- lastKey.setSysKey(lastKey.getSysKey()+1);
- } catch (InvalidEntityPathException ex) {
- throw new ObjectCannotBeUpdated("Invalid syskey "+(lastKey.getSysKey()+1)+". Maybe centre is full.");
- }
- //set the last key
- writeLastEntityKey(lastKey.getSysKey());
-
- return lastKey;
- }
-
- public synchronized AgentPath generateNextAgentKey()
- throws ObjectCannotBeUpdated, ObjectNotFoundException {
- EntityPath newEntity = generateNextEntityKey();
- return new AgentPath(newEntity);
- }
-
- public void writeLastEntityKey(int sysKey) throws ObjectCannotBeUpdated, ObjectNotFoundException {
- LDAPEntry lastKeyEntry = LDAPLookupUtils.getEntry(ldap.getConnection(),lastKeyPath);
- LDAPLookupUtils.setAttributeValue(ldap.getConnection(), lastKeyEntry,"intsyskey",Integer.toString(sysKey));
- }
-
- public EntityPath getLastEntityPath() throws ObjectNotFoundException
- {
- LDAPEntry lastKeyEntry = LDAPLookupUtils.getEntry(ldap.getConnection(),lastKeyPath);
- String lastKey = LDAPLookupUtils.getFirstAttributeValue(lastKeyEntry,"intsyskey");
- try {
- int sysKey = Integer.parseInt(lastKey);
- EntityPath sysPath = new EntityPath(sysKey);
- return sysPath;
- } catch (InvalidEntityPathException ex) {
- throw new ObjectNotFoundException("Invalid syskey. Maybe centre is full.");
- } catch (NumberFormatException ex) {
- throw new ObjectNotFoundException("Invalid syskey in lastkey.");
- }
-
- }
-
-}
diff --git a/src/main/java/com/c2kernel/lookup/Path.java b/src/main/java/com/c2kernel/lookup/Path.java index 4bec43a..f9fd15d 100644 --- a/src/main/java/com/c2kernel/lookup/Path.java +++ b/src/main/java/com/c2kernel/lookup/Path.java @@ -12,15 +12,11 @@ package com.c2kernel.lookup; import java.io.Serializable;
import java.util.ArrayList;
-import java.util.Enumeration;
+import java.util.Iterator;
import java.util.StringTokenizer;
-import com.c2kernel.common.ObjectCannotBeUpdated;
import com.c2kernel.common.ObjectNotFoundException;
import com.c2kernel.process.Gateway;
-import com.novell.ldap.LDAPAttributeSet;
-import com.novell.ldap.LDAPConnection;
-import com.novell.ldap.LDAPSearchConstraints;
/**
@@ -45,19 +41,11 @@ public abstract class Path implements Serializable protected String mStringPath = null;
// entity or context
protected short mType = CONTEXT;
- // LDAP dn
- protected String mDN = null;
// int syskey (only valid for entity SystemPaths)
protected int mSysKey = INVALID;
// ior is stored in here when it is resolved
protected org.omg.CORBA.Object mIOR = null;
- //
- // needed for unusual subclass constructors
-
- protected static String mGlobalPath; //cern.ch
- protected static String mRootPath; //cristal2
- protected static String mLocalPath; //lab27
public Path() {
}
@@ -114,7 +102,6 @@ public abstract class Path implements Serializable public void setPath(String[] path)
{
mStringPath = null;
- mDN = null;
mPath = path.clone();
mSysKey = INVALID;
}
@@ -136,7 +123,6 @@ public abstract class Path implements Serializable mPath = (newPath.toArray(mPath));
mStringPath = null;
- mDN = null;
mSysKey = INVALID;
}
@@ -152,40 +138,10 @@ public abstract class Path implements Serializable public void setPath(Path path)
{
mStringPath = null;
- mDN = null;
mPath = (path.getPath().clone());
mSysKey = INVALID;
}
- /* LDAP dn e.g. cn=6L,cn=Barrel,cn=Crystal,cn=Product,cn=domain,
- * system/domain node PRESENT
- * trailing comma
- */
- public void setDN(String dn)
- {
- // strip off root path components
- String root = "cn="+getRoot()+",";
- if (dn.endsWith(mLocalPath))
- dn = dn.substring(0, dn.lastIndexOf(mLocalPath));
-
- if (dn.endsWith(root))
- dn = dn.substring(0, dn.lastIndexOf(root));
-
- ArrayList<String> newPath = new ArrayList<String>();
- StringTokenizer tok = new StringTokenizer(dn, ",");
- while (tok.hasMoreTokens()) {
- String nextPath = tok.nextToken();
- if (nextPath.indexOf("cn=") == 0)
- newPath.add(0, LDAPLookupUtils.unescapeDN(nextPath.substring(3)));
- else
- break;
- }
- mPath = (newPath.toArray(mPath));
- mSysKey = INVALID;
- mStringPath = null;
- mDN = dn+root;
- }
-
/*************************************************************************/
@@ -212,23 +168,8 @@ public abstract class Path implements Serializable return mStringPath;
}
- public String getDN() {
- if (mDN == null) {
- StringBuffer dnBuffer = new StringBuffer();
- for (int i=mPath.length-1; i>=0; i--)
- dnBuffer.append("cn=").append(LDAPLookupUtils.escapeDN(mPath[i])).append(",");
- dnBuffer.append("cn="+getRoot()+",");
- mDN = dnBuffer.toString();
- }
- return mDN;
- }
-
- public String getFullDN() {
- return getDN()+mLocalPath;
- }
-
public boolean exists() {
- return Gateway.getLDAPLookup().exists(this);
+ return Gateway.getLookup().exists(this);
}
/** Queries the lookup for the IOR
@@ -237,9 +178,9 @@ public abstract class Path implements Serializable public org.omg.CORBA.Object getIOR() {
org.omg.CORBA.Object newIOR = null;
if (mIOR==null) { // if not cached try to resolve
- LDAPLookup myLookup = Gateway.getLDAPLookup();
+ Lookup myLookup = Gateway.getLookup();
try {
- newIOR = myLookup.getIOR(this);
+ newIOR = myLookup.resolve(this);
} catch (ObjectNotFoundException ex) {
}
setIOR(newIOR);
@@ -260,28 +201,18 @@ public abstract class Path implements Serializable return mSysKey;
}
- public Enumeration<? extends Path> getChildren() {
- String filter = "objectclass=*";
- LDAPSearchConstraints searchCons = new LDAPSearchConstraints();
- searchCons.setBatchSize(10);
- searchCons.setDereference(LDAPSearchConstraints.DEREF_FINDING );
- return Gateway.getLDAPLookup().search(getFullDN(), LDAPConnection.SCOPE_ONE,filter,searchCons);
- }
-
public Path find(String name) throws ObjectNotFoundException {
- Enumeration<Path> e = Gateway.getLDAPLookup().search(this, name);
- if (e.hasMoreElements()) {
- Path thisPath = e.nextElement();
- if (e.hasMoreElements())
+ Iterator<Path> e = Gateway.getLookup().search(this, name);
+ if (e.hasNext()) {
+ Path thisPath = e.next();
+ if (e.hasNext())
throw new ObjectNotFoundException("More than one match for "+name, "");
return thisPath;
}
throw new ObjectNotFoundException("No match for "+name, "");
}
- public abstract EntityPath getEntity() throws ObjectNotFoundException;
-
- public abstract LDAPAttributeSet createAttributeSet() throws ObjectCannotBeUpdated;
+ public abstract ItemPath getEntity() throws ObjectNotFoundException;
@Override
public boolean equals( Object path )
@@ -298,7 +229,7 @@ public abstract class Path implements Serializable StringBuffer comp = new StringBuffer("Components: { ");
for (String element : mPath)
comp.append("'").append(element).append("' ");
- return "Path - dump(): "+comp.toString()+"}\n dn="+getDN()+"\n string="+toString()+"\n int="+getSysKey()+"\n type="+mType;
+ return "Path - dump(): "+comp.toString()+"}\n string="+toString()+"\n int="+getSysKey()+"\n type="+mType;
}
}
diff --git a/src/main/java/com/c2kernel/lookup/RolePath.java b/src/main/java/com/c2kernel/lookup/RolePath.java index 0e07012..bd23991 100644 --- a/src/main/java/com/c2kernel/lookup/RolePath.java +++ b/src/main/java/com/c2kernel/lookup/RolePath.java @@ -10,15 +10,13 @@ package com.c2kernel.lookup;
-import java.util.Enumeration;
-import java.util.Vector;
+import java.util.ArrayList;
+import java.util.Iterator;
import com.c2kernel.common.ObjectCannotBeUpdated;
import com.c2kernel.common.ObjectNotFoundException;
import com.c2kernel.process.Gateway;
import com.c2kernel.utils.Logger;
-import com.novell.ldap.LDAPAttribute;
-import com.novell.ldap.LDAPAttributeSet;
@@ -56,7 +54,7 @@ public class RolePath extends DomainPath */
public void setHasJobList(boolean hasJobList) throws ObjectNotFoundException, ObjectCannotBeUpdated {
this.hasJobList = hasJobList;
- Gateway.getLDAPLookup().getRoleManager().setHasJobList(this, hasJobList);
+ Gateway.getLookup().setHasJobList(this, hasJobList);
}
@@ -65,18 +63,17 @@ public class RolePath extends DomainPath mType = CONTEXT;
}
- @Override
- public Enumeration<AgentPath> getChildren() {
+ public Iterator<Path> getChildren() {
AgentPath[] agents = getAgentsWithRole();
- Vector<AgentPath> children = new Vector<AgentPath>(agents.length);
+ ArrayList<Path> children = new ArrayList<Path>(agents.length);
for (int i = 0; i < agents.length; i++)
children.add(i, agents[i]);
- return children.elements();
+ return children.iterator();
}
public AgentPath[] getAgentsWithRole() {
try {
- return Gateway.getLDAPLookup().getRoleManager().getAgents(this);
+ return Gateway.getLookup().getAgents(this);
} catch (ObjectNotFoundException ex) {
Logger.error("Cannot retrieve agent list. Role "+getName()+" does not exist in LDAP");
return new AgentPath[0];
@@ -84,11 +81,11 @@ public class RolePath extends DomainPath }
public void addAgent(AgentPath agent) throws ObjectCannotBeUpdated, ObjectNotFoundException {
- Gateway.getLDAPLookup().getRoleManager().addRole(agent, this);
+ Gateway.getLookup().addRole(agent, this);
}
public void removeAgent(AgentPath agent) throws ObjectCannotBeUpdated, ObjectNotFoundException {
- Gateway.getLDAPLookup().getRoleManager().removeRole(agent, this);
+ Gateway.getLookup().removeRole(agent, this);
}
@Override
@@ -99,9 +96,7 @@ public class RolePath extends DomainPath return "Path - dump(): "+
comp.toString()+
- "}\n dn="+
- getDN()+
- "\n string="+
+ "}\n string="+
toString()+
"\n type="+
mType+
@@ -110,15 +105,5 @@ public class RolePath extends DomainPath "\n ";
}
- @Override
- public LDAPAttributeSet createAttributeSet()
- {
- LDAPAttributeSet attrs = new LDAPAttributeSet();
- attrs.add(new LDAPAttribute("objectclass","cristalrole"));
- String jobListString = hasJobList?"TRUE":"FALSE";
- attrs.add(new LDAPAttribute("jobList",jobListString));
- attrs.add(new LDAPAttribute("cn", getName()));
- return attrs;
- }
}
diff --git a/src/main/java/com/c2kernel/lookup/ldap/LDAPAuthManager.java b/src/main/java/com/c2kernel/lookup/ldap/LDAPAuthManager.java new file mode 100644 index 0000000..4c26de6 --- /dev/null +++ b/src/main/java/com/c2kernel/lookup/ldap/LDAPAuthManager.java @@ -0,0 +1,95 @@ +package com.c2kernel.lookup.ldap;
+
+import com.c2kernel.common.InvalidDataException;
+import com.c2kernel.common.ObjectNotFoundException;
+import com.c2kernel.process.Gateway;
+import com.c2kernel.process.auth.Authenticator;
+import com.c2kernel.utils.Logger;
+import com.novell.ldap.LDAPConnection;
+import com.novell.ldap.LDAPException;
+
+public class LDAPAuthManager implements Authenticator {
+
+ private LDAPConnection mLDAPConn;
+ private LDAPProperties ldapProps;
+
+
+ @Override
+ public boolean authenticate(String agentName,
+ String password, String resource) throws InvalidDataException, ObjectNotFoundException {
+
+ ldapProps = new LDAPProperties(Gateway.getProperties());
+
+ if (ldapProps.mHost!=null && ldapProps.mPort!= null && ldapProps.mLocalPath!=null )
+ {
+ try { // anonymously bind to LDAP and find the agent entry for the username
+ ldapProps.mUser = "";
+ ldapProps.mPassword = "";
+ mLDAPConn = LDAPLookupUtils.createConnection(ldapProps);
+ LDAPLookup anonLookup = new LDAPLookup(ldapProps);
+ anonLookup.open(this);
+ String agentDN = anonLookup.getFullDN(anonLookup.getAgentPath(agentName));
+
+ //found agentDN, try to log in with it
+ ldapProps.mUser = agentDN;
+ ldapProps.mPassword = password;
+ mLDAPConn = LDAPLookupUtils.createConnection(ldapProps);
+ return true;
+ } catch (LDAPException e) {
+ return false;
+ }
+ }
+ else
+ {
+ throw new InvalidDataException("Cannot log in. Some connection properties are not set.", "");
+ }
+
+ }
+
+ @Override
+ public boolean authenticate(String resource) throws InvalidDataException, ObjectNotFoundException {
+ ldapProps = new LDAPProperties(Gateway.getProperties());
+
+ if (ldapProps.mUser == null || ldapProps.mUser.length()==0 ||
+ ldapProps.mPassword == null || ldapProps.mPassword.length()==0)
+ throw new InvalidDataException("LDAP root user properties not found in config.");
+ try {
+ mLDAPConn = LDAPLookupUtils.createConnection(ldapProps);
+ return true;
+ } catch (LDAPException e) {
+ return false;
+ }
+ }
+
+ @Override
+ public LDAPConnection getAuthObject() {
+
+ if (!mLDAPConn.isConnected()) {
+ Logger.warning("LDAPAuthManager - lost connection to LDAP server. Attempting to reconnect.");
+ try {
+ mLDAPConn = LDAPLookupUtils.createConnection(ldapProps);
+ } catch (LDAPException ex) { }
+ }
+ return mLDAPConn;
+ }
+
+ @Override
+ public void disconnect() {
+ Logger.msg(1, "LDAP Lookup: Shutting down LDAP connection.");
+ if (mLDAPConn != null) {
+ try {
+ mLDAPConn.disconnect();
+ } catch (LDAPException e) {
+ Logger.error(e);
+ }
+ mLDAPConn = null;
+ }
+
+ }
+
+ public LDAPAuthManager() {
+ // TODO Auto-generated constructor stub
+ }
+
+
+}
diff --git a/src/main/java/com/c2kernel/lookup/ldap/LDAPLookup.java b/src/main/java/com/c2kernel/lookup/ldap/LDAPLookup.java new file mode 100644 index 0000000..10c1830 --- /dev/null +++ b/src/main/java/com/c2kernel/lookup/ldap/LDAPLookup.java @@ -0,0 +1,763 @@ +/*
+ * Directory Lookup Service *
+ * author: Florida Estrella
+*/
+
+package com.c2kernel.lookup.ldap;
+
+import java.security.NoSuchAlgorithmException;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.StringTokenizer;
+
+import org.omg.CORBA.Object;
+
+import com.c2kernel.common.ObjectAlreadyExistsException;
+import com.c2kernel.common.ObjectCannotBeUpdated;
+import com.c2kernel.common.ObjectNotFoundException;
+import com.c2kernel.entity.TraceableEntity;
+import com.c2kernel.entity.agent.ActiveEntity;
+import com.c2kernel.entity.proxy.ProxyMessage;
+import com.c2kernel.lookup.AgentPath;
+import com.c2kernel.lookup.DomainPath;
+import com.c2kernel.lookup.InvalidItemPathException;
+import com.c2kernel.lookup.ItemPath;
+import com.c2kernel.lookup.Lookup;
+import com.c2kernel.lookup.Path;
+import com.c2kernel.lookup.RolePath;
+import com.c2kernel.process.Gateway;
+import com.c2kernel.process.auth.Authenticator;
+import com.c2kernel.property.PropertyDescription;
+import com.c2kernel.property.PropertyDescriptionList;
+import com.c2kernel.utils.Logger;
+import com.novell.ldap.LDAPAttribute;
+import com.novell.ldap.LDAPAttributeSet;
+import com.novell.ldap.LDAPConnection;
+import com.novell.ldap.LDAPDN;
+import com.novell.ldap.LDAPEntry;
+import com.novell.ldap.LDAPException;
+import com.novell.ldap.LDAPSearchConstraints;
+import com.novell.ldap.LDAPSearchResults;
+
+/**
+ * The LDAPLookup object, statically accessible through the Gateway, manages
+ * the LDAP connection for the cristal process. It provides:
+ * <ul>
+ * <li>Authentication - returning an AgentProxy object if a user has logged in
+ * <li>System key generation - through the NextKeyManager
+ * <li>Agent and Role lookup/modification - through the RoleManager
+ * <li>
+ * @version $Revision: 1.113 $ $Date: 2006/03/03 13:52:21 $
+ * @author $Author: abranson $
+ */
+
+public class LDAPLookup implements Lookup
+
+{
+ private LDAPAuthManager mLDAPAuth;
+ private LDAPPropertyManager mPropManager;
+
+ final String mItemTypeRoot, mDomainTypeRoot, mGlobalPath, mRootPath, mLocalPath, mRolePath;
+
+ /**
+ * Creates a new LDAPLookup manager with the properties supplied.
+ * This should be only done by the Gateway during initialisation.
+ *
+ * @param props The LDAP properties object that extracts LDAP connection properties from the global c2kprops
+ */
+ public LDAPLookup(LDAPProperties props)
+ {
+ Logger.msg(8,"LDAPLookup - initialising.");
+
+ mGlobalPath=props.mGlobalPath;
+ mRootPath=props.mRootPath;
+ mLocalPath=props.mLocalPath;
+
+ mItemTypeRoot = "cn=entity,"+props.mLocalPath;
+ mDomainTypeRoot = "cn=domain,"+props.mLocalPath;
+ mRolePath = "cn=agent,"+mDomainTypeRoot;
+
+ }
+
+ public LDAPLookup() {
+ this(new LDAPProperties(Gateway.getProperties()));
+ }
+
+ @Override
+ public void open(Authenticator auth) {
+ mLDAPAuth = (LDAPAuthManager)auth;
+ Logger.msg(7, "LDAP.useOldProps="+Gateway.getProperties().getBoolean("LDAP.useOldProps", false));
+ mPropManager = new LDAPPropertyManager(this, mLDAPAuth);
+ }
+
+ /**
+ * Gets the property manager, that is used to read and write cristal properties to the LDAP store.
+ * @return Returns the global LDAPPropertyManager.
+ */
+ public LDAPPropertyManager getPropManager() {
+ return mPropManager;
+ }
+
+ /**
+ * Disconnects the connection with the LDAP server during shutdown
+ */
+ @Override
+ public void close() {
+ Logger.msg(1, "LDAP Lookup: Shutting down LDAP connection.");
+ if (mLDAPAuth != null) {
+ mLDAPAuth.disconnect();
+ mLDAPAuth = null;
+ }
+ }
+
+ /**
+ * Attempts to resolve the CORBA object for a Path, either directly or through an alias.
+ * @param path the path to resolve
+ * @return the CORBA object
+ * @throws ObjectNotFoundException When the path does not exist
+ */
+ public org.omg.CORBA.Object getIOR(Path path)
+ throws ObjectNotFoundException
+ {
+ return resolveObject(getFullDN(path));
+ }
+
+ /**
+ * Attempts to resolve the CORBA object from the IOR attribute of a DN, either directly or through an alias
+ * @param dn The String dn
+ * @throws ObjectNotFoundException when the dn or aliased dn does not exist
+ */
+ private org.omg.CORBA.Object resolveObject(String dn)
+ throws ObjectNotFoundException
+ {
+ Logger.msg(8,"LDAPLookup.resolveObject("+dn+")");
+ LDAPEntry anEntry = LDAPLookupUtils.getEntry(mLDAPAuth.getAuthObject(),dn,LDAPSearchConstraints.DEREF_NEVER);
+ if (anEntry != null)
+ {
+ String iorString;
+ try {
+ iorString = LDAPLookupUtils.getFirstAttributeValue(anEntry, "ior");
+ org.omg.CORBA.Object ior=Gateway.getORB().string_to_object(iorString);
+ if (ior!=null)
+ return ior;
+ else
+ throw new ObjectNotFoundException("LDAPLookup.resolveObject() - " + dn + " has no IOR", "");
+ } catch (ObjectNotFoundException ex) {
+ return resolveObject(LDAPLookupUtils.getFirstAttributeValue(anEntry,"aliasedObjectName"));
+ }
+ }
+ else
+ throw new ObjectNotFoundException("LDAPLookup.resolveObject() LDAP node " + dn + " is not in LDAP or has no IOR.", "");
+ }
+
+ /**
+ *
+ * @param domPath
+ * @return
+ * @throws InvalidItemPathException
+ * @throws ObjectNotFoundException
+ */
+ @Override
+ public ItemPath resolvePath(DomainPath domPath)
+ throws InvalidItemPathException, ObjectNotFoundException {
+ ItemPath referencedPath = null;
+ LDAPEntry domEntry = LDAPLookupUtils.getEntry(mLDAPAuth.getAuthObject(),
+ getFullDN(domPath), LDAPSearchConstraints.DEREF_ALWAYS);
+ String entityKey = LDAPLookupUtils.getFirstAttributeValue(domEntry,
+ "intsyskey");
+ Logger.msg(7, "DomainPath " + domPath + " is a reference to "
+ + entityKey);
+ String objClass = LDAPLookupUtils.getFirstAttributeValue(domEntry,
+ "objectClass");
+ if (objClass.equals("cristalagent"))
+ referencedPath = new AgentPath(Integer.parseInt(entityKey));
+ else
+ referencedPath = new ItemPath(Integer.parseInt(entityKey));
+
+ return referencedPath;
+ }
+
+
+ @Override
+ public void add(Path path)
+ throws ObjectCannotBeUpdated, ObjectAlreadyExistsException
+ {
+ try {
+ checkLDAPContext(path);
+ LDAPAttributeSet attrSet = createAttributeSet(path);
+ LDAPEntry newEntry = new LDAPEntry(getFullDN(path),attrSet);
+ LDAPLookupUtils.addEntry(mLDAPAuth.getAuthObject(),newEntry);
+ if (path instanceof DomainPath)
+ Gateway.getProxyServer().sendProxyEvent(new ProxyMessage(ProxyMessage.NA, path.toString(), ProxyMessage.ADDED));
+ } catch (LDAPException ex) {
+ if (ex.getResultCode() == LDAPException.ENTRY_ALREADY_EXISTS)
+ throw new ObjectAlreadyExistsException(ex.getLDAPErrorMessage(), "");
+ else
+ throw new ObjectCannotBeUpdated(ex.getLDAPErrorMessage(), "");
+ }
+ }
+
+ //deletes a node
+ //throws LDAPexception if node cannot be deleted (eg node is not a leaf)
+ @Override
+ public void delete(Path path) throws ObjectCannotBeUpdated
+ {
+ try {
+ LDAPLookupUtils.delete(mLDAPAuth.getAuthObject(),getDN(path)+mLocalPath);
+ } catch (LDAPException ex) {
+ throw new ObjectCannotBeUpdated(ex.getLDAPErrorMessage(), "");
+ }
+ if (path instanceof DomainPath) {
+ Gateway.getProxyServer().sendProxyEvent(new ProxyMessage(ProxyMessage.NA, path.toString(), ProxyMessage.DELETED));
+ }
+ }
+
+ //change specs, add boolean alias leaf context
+ protected void checkLDAPContext(Path path)
+ {
+ String dn = getFullDN(path);
+ if (!LDAPLookupUtils.exists(mLDAPAuth.getAuthObject(),dn))
+ {
+ String listDN[] = path.getPath();
+ String name = "cn="+ path.getRoot() + "," + mLocalPath;
+ int i=0;
+ while (i<listDN.length-1)
+ {
+ name= "cn="+LDAPLookupUtils.escapeDN(listDN[i])+","+name;
+ if (!LDAPLookupUtils.exists(mLDAPAuth.getAuthObject(),name))
+ {
+ try
+ {
+ //create cristalcontext
+ Logger.msg(8,"LDAPLookup::addLDAPContext() context added " + name);
+ LDAPLookupUtils.createCristalContext(mLDAPAuth.getAuthObject(), name);
+ }
+ catch (Exception ex)
+ {
+ Logger.error("LDAPLookup::addContext() " + ex);
+ }
+ }
+ i++;
+ }
+ }
+ }
+ public void createBootTree()
+ {
+ Logger.msg(8,"Initializing LDAP Boot tree");
+
+ //create org
+ LDAPLookupUtils.createOrganizationContext(mLDAPAuth.getAuthObject(), mGlobalPath);
+ //create root
+ LDAPLookupUtils.createCristalContext(mLDAPAuth.getAuthObject(), mRootPath);
+ //create local
+ LDAPLookupUtils.createCristalContext(mLDAPAuth.getAuthObject(), mLocalPath);
+ }
+
+ @Override
+ public void initializeDirectory() throws ObjectNotFoundException
+ {
+ createBootTree();
+ initTree( Gateway.getResource().getTextResource(null, "boot/LDAPboot.txt"));
+ }
+
+ public void initTree(String bootFile)
+ {
+ Logger.msg(8,"Verifying Cristal LDAP roots");
+ StringTokenizer strTokenizer = new StringTokenizer(bootFile, "\n\r");
+ while (strTokenizer.hasMoreTokens())
+ {
+ String line = strTokenizer.nextToken();
+ Logger.msg(8,"Checking " + line+mLocalPath);
+ LDAPLookupUtils.createCristalContext(mLDAPAuth.getAuthObject(), line+mLocalPath);
+ }
+
+ }
+
+ //typically search for cn=barcode
+ @Override
+ public LDAPPathSet search(Path start, String filter)
+ {
+ Logger.msg(8,"LDAPLookup::search() From " + getDN(start) + " for cn=" + filter );
+ return search(getFullDN(start),"cn="+LDAPLookupUtils.escapeSearchFilter(filter));
+ }
+
+ @Override
+ public LDAPPathSet search(Path start, String propname, String propvalue)
+ {
+ String val = propname+":"+propvalue;
+ Logger.msg(8,"LDAPLookup::search() From " + getDN(start) + " for cristalprop=" + val );
+ LDAPSearchConstraints searchCons = new LDAPSearchConstraints();
+ searchCons.setBatchSize(0);
+ searchCons.setDereference(LDAPSearchConstraints.DEREF_SEARCHING);
+ return search(getFullDN(start),LDAPConnection.SCOPE_SUB,"cristalprop="+LDAPLookupUtils.escapeSearchFilter(val),searchCons);
+ }
+
+ @Override
+ public LDAPPathSet search(Path start, PropertyDescriptionList props) {
+ StringBuffer query = new StringBuffer();
+ int propCount = 0;
+ for (PropertyDescription propDesc: props.list) {
+ if (propDesc.getIsClassIdentifier()) {
+ String thisProp = LDAPLookupUtils.escapeSearchFilter(propDesc.getName()+(":")+propDesc.getDefaultValue());
+ query.append("(cristalprop=").append(thisProp).append(")");
+ propCount++;
+ }
+ }
+ LDAPSearchConstraints searchCons = new LDAPSearchConstraints();
+ searchCons.setBatchSize(0);
+ searchCons.setDereference(LDAPSearchConstraints.DEREF_SEARCHING);
+ if (propCount == 0)
+ return search(getFullDN(start),LDAPConnection.SCOPE_SUB,"objectClass=cristalentity",searchCons);
+ else if (propCount == 1)
+ return search(getFullDN(start),LDAPConnection.SCOPE_SUB,query.toString(),searchCons);
+ else
+ return search(getFullDN(start),LDAPConnection.SCOPE_SUB,"(&"+query.toString()+")",searchCons);
+ }
+
+ protected LDAPPathSet search(String startDN, int scope, String filter, LDAPSearchConstraints searchCons)
+ {
+ Logger.msg(8,"Searching for "+filter+" in "+startDN);
+ searchCons.setMaxResults(0);
+ String[] attr = { LDAPConnection.ALL_USER_ATTRS };
+ try
+ {
+ LDAPSearchResults res = mLDAPAuth.getAuthObject().search(startDN, scope,
+ filter,attr,false,searchCons);
+ return new LDAPPathSet(res, this);
+ }
+ catch (LDAPException ex)
+ {
+ Logger.error("LDAPException::LDAPLookup::search() " + ex.toString());
+ return new LDAPPathSet(this);
+ }
+ }
+ //typically search for (any filter combination)
+ public LDAPPathSet search(String startDN,String filter)
+ {
+ LDAPSearchConstraints searchCons = new LDAPSearchConstraints();
+ searchCons.setBatchSize(0);
+ searchCons.setDereference(LDAPSearchConstraints.DEREF_NEVER);
+ return search(startDN,LDAPConnection.SCOPE_SUB,filter,searchCons);
+ }
+
+ @Override
+ public LDAPPathSet searchEntities(Path start) {
+ LDAPSearchConstraints searchCons = new LDAPSearchConstraints();
+ searchCons.setBatchSize(0);
+ searchCons.setDereference(LDAPSearchConstraints.DEREF_SEARCHING);
+ return search(getFullDN(start), LDAPConnection.SCOPE_SUB, "objectClass=cristalentity", searchCons);
+ }
+
+ @Override
+ public LDAPPathSet searchAliases(DomainPath start) {
+ LDAPSearchConstraints searchCons = new LDAPSearchConstraints();
+ searchCons.setBatchSize(0);
+ searchCons.setDereference(LDAPSearchConstraints.DEREF_NEVER);
+ return search(getFullDN(start), LDAPConnection.SCOPE_SUB, "objectClass=aliasObject", searchCons);
+ }
+
+ @Override
+ public LDAPPathSet searchAliases(ItemPath entity) {
+ LDAPSearchConstraints searchCons = new LDAPSearchConstraints();
+ searchCons.setBatchSize(0);
+ searchCons.setDereference(LDAPSearchConstraints.DEREF_NEVER);
+ return search(getFullDN(new DomainPath()), LDAPConnection.SCOPE_SUB, "(&(objectClass=aliasObject)(aliasedObjectName="+
+ LDAPLookupUtils.escapeDN(getFullDN(entity))+"))", searchCons);
+ }
+
+ @Override
+ public boolean exists(Path path) {
+ return LDAPLookupUtils.exists(mLDAPAuth.getAuthObject(), getFullDN(path));
+ }
+
+ @Override
+ public Class<?> getItemClass(Path path) throws ObjectNotFoundException {
+ String[] attr = { LDAPConnection.ALL_USER_ATTRS };
+ try {
+ LDAPEntry anEntry=mLDAPAuth.getAuthObject().read(getDN(path)+mLocalPath,attr);
+ String type = LDAPLookupUtils.getFirstAttributeValue(anEntry, "objectClass");
+ if (type.equals("cristalentity"))
+ return TraceableEntity.class;
+ else if (type.equals("cristalagent"))
+ return ActiveEntity.class;
+ else
+ throw new ObjectNotFoundException("Not an entity", "");
+
+ } catch (LDAPException ex) {
+ if (ex.getResultCode() == LDAPException.NO_SUCH_OBJECT)
+ throw new ObjectNotFoundException("Entity does not exist", "");
+ Logger.error(ex);
+ throw new ObjectNotFoundException("Error getting entity class", "");
+ }
+ }
+
+ /** converts an LDAPentry to a Path object
+ * Note that the search producing the entry should have retrieved the attrs
+ * 'ior' and 'uniquemember'
+ * @throws ObjectNotFoundException
+ * @throws ObjectNotFoundException
+ */
+ protected Path nodeToPath(LDAPEntry entry) throws InvalidItemPathException, ObjectNotFoundException
+ {
+ String dn = entry.getDN();
+
+ // extract syskey
+ int entityKey = -1;
+ try {
+ String entityKeyStr = LDAPLookupUtils.getFirstAttributeValue(entry,"intsyskey");
+ entityKey = Integer.parseInt(entityKeyStr);
+ } catch (Exception e) { }
+
+ // extract IOR
+ org.omg.CORBA.Object ior = null;
+ try {
+ String stringIOR = LDAPLookupUtils.getFirstAttributeValue(entry,"ior");
+ ior = Gateway.getORB().string_to_object(stringIOR);
+ } catch (ObjectNotFoundException e2) { }
+
+ /* Find the right path class */
+ Path thisPath;
+ if (LDAPLookupUtils.existsAttributeValue(entry,"objectclass","cristalagent"))
+ { //cristalagent
+ String agentID = LDAPLookupUtils.getFirstAttributeValue(entry,"uid");
+ thisPath = new AgentPath(entityKey, agentID);
+ }
+
+ else if (LDAPLookupUtils.existsAttributeValue(entry,"objectclass","cristalrole"))
+ { //cristalrole
+ thisPath = new RolePath(LDAPDN.explodeDN(dn,true)[0],
+ LDAPLookupUtils.getFirstAttributeValue(entry, "jobList").equals("TRUE"));
+ }
+ else if (LDAPLookupUtils.existsAttributeValue(entry,"objectclass","aliasObject") ||
+ (LDAPLookupUtils.existsAttributeValue(entry,"objectclass","cristalcontext") && dn.endsWith(mDomainTypeRoot)))
+ {
+ DomainPath domainPath = new DomainPath();
+ domainPath.setPath(getPathComponents(dn.substring(0, dn.lastIndexOf(mDomainTypeRoot))));
+ thisPath = domainPath;
+ }
+ else if (LDAPLookupUtils.existsAttributeValue(entry,"objectclass","cristalentity") ||
+ (LDAPLookupUtils.existsAttributeValue(entry,"objectclass","cristalcontext") && dn.endsWith(mItemTypeRoot)))
+ {
+ if(dn.endsWith(mItemTypeRoot)) {
+ ItemPath entityPath;
+ if (entityKey != -1)
+ entityPath = new ItemPath(entityKey);
+ else {
+ entityPath = new ItemPath();
+ entityPath.setPath(getPathComponents(dn.substring(0, dn.lastIndexOf(mItemTypeRoot))));
+ }
+ thisPath = entityPath;
+ }
+ else
+ throw new ObjectNotFoundException("Entity found outside entity tree");
+ }
+ else
+ {
+ throw new ObjectNotFoundException("Unrecognised LDAP entry. Not a cristal entry");
+ }
+
+ //set IOR if we have one
+ if (ior!=null) thisPath.setIOR(ior);
+ return thisPath;
+ }
+
+ public String getDN(Path path) {
+ StringBuffer dnBuffer = new StringBuffer();
+ String[] pathComp = path.getPath();
+ for (int i=pathComp.length-1; i>=0; i--)
+ dnBuffer.append("cn=").append(LDAPLookupUtils.escapeDN(pathComp[i])).append(",");
+ dnBuffer.append("cn="+path.getRoot()+",");
+ return dnBuffer.toString();
+ }
+
+ public String getFullDN(Path path) {
+ return getDN(path)+mLocalPath;
+ }
+
+ public String[] getPathComponents(String dnFragment) {
+ ArrayList<String> newPath = new ArrayList<String>();
+ StringTokenizer tok = new StringTokenizer(dnFragment, ",");
+ String[] path = new String[tok.countTokens()];
+ while (tok.hasMoreTokens()) {
+ String nextPath = tok.nextToken();
+ if (nextPath.indexOf("cn=") == 0)
+ newPath.add(0, LDAPLookupUtils.unescapeDN(nextPath.substring(3)));
+ else
+ break;
+ }
+ return newPath.toArray(path);
+ }
+
+ @Override
+ public Object resolve(Path path) throws ObjectNotFoundException {
+ return resolveObject(getFullDN(path));
+ }
+
+ @Override
+ public Iterator<Path> getChildren(Path path) {
+ if (path instanceof RolePath) return ((RolePath)path).getChildren();
+ String filter = "objectclass=*";
+ LDAPSearchConstraints searchCons = new LDAPSearchConstraints();
+ searchCons.setBatchSize(10);
+ searchCons.setDereference(LDAPSearchConstraints.DEREF_FINDING );
+ return search(getFullDN(path), LDAPConnection.SCOPE_ONE,filter,searchCons);
+ }
+
+ public LDAPAttributeSet createAttributeSet(Path path) throws ObjectCannotBeUpdated {
+ LDAPAttributeSet attrs = new LDAPAttributeSet();
+
+ if (path instanceof RolePath) {
+ RolePath rolePath = (RolePath)path;
+ attrs.add(new LDAPAttribute("objectclass","cristalrole"));
+ String jobListString = rolePath.hasJobList()?"TRUE":"FALSE";
+ attrs.add(new LDAPAttribute("jobList",jobListString));
+ attrs.add(new LDAPAttribute("cn", rolePath.getName()));
+ }
+ else if (path instanceof DomainPath) {
+ DomainPath domPath = (DomainPath)path;
+ attrs.add(new LDAPAttribute("cn",domPath.getName()));
+ try {
+ attrs.add(new LDAPAttribute("aliasedObjectName",getFullDN(domPath.getEntity())));
+ String objectclass_values[] = { "alias", "aliasObject" };
+ attrs.add(new LDAPAttribute("objectclass",objectclass_values));
+ } catch (ObjectNotFoundException e) { // no entity - is a context
+ attrs.add(new LDAPAttribute("objectclass","cristalcontext"));
+ }
+ }
+
+ else if (path instanceof ItemPath) {
+ ItemPath itemPath = (ItemPath)path;
+ attrs.add(new LDAPAttribute("intsyskey",Integer.toString(itemPath.getSysKey())));
+ attrs.add(new LDAPAttribute("cn", itemPath.getPath()[itemPath.getPath().length-1]));
+ if (itemPath.getIOR() != null)
+ attrs.add(new LDAPAttribute("ior", Gateway.getORB().object_to_string(itemPath.getIOR())));
+
+ if (path instanceof AgentPath) {
+ AgentPath agentPath = (AgentPath)path;
+ attrs.add(new LDAPAttribute("objectclass","cristalagent"));
+
+ String agentName = agentPath.getAgentName();
+ if (agentName != null && agentName.length() > 0)
+ attrs.add(new LDAPAttribute("uid", agentName));
+ else
+ throw new ObjectCannotBeUpdated("Cannot create agent. No userId specified", "");
+
+ String agentPass = agentPath.getPassword();
+ if (agentPass != null && agentPass.length() > 0)
+ try {
+ attrs.add(new LDAPAttribute("userPassword", AgentPath.generateUserPassword(agentPass, "SHA")));
+ } catch (NoSuchAlgorithmException ex) {
+ throw new ObjectCannotBeUpdated("Cryptographic libraries for password hashing not found.", "");
+ }
+ else
+ throw new ObjectCannotBeUpdated("Cannot create agent. No password given", "");
+ }
+ else {
+ attrs.add(new LDAPAttribute("objectclass","cristalentity"));
+ }
+ }
+
+ return attrs;
+
+ }
+
+ //NOTE: A role must have at LEAST 1 userDN, cannot be empty...
+ //Creates a cristalRole
+ //CristalRole is-a specialized CristalContext which contains multi-valued uniqueMember attribute pointing to cristalagents
+ @Override
+ public RolePath createRole(String roleName, boolean jobList)
+ throws ObjectAlreadyExistsException, ObjectCannotBeUpdated
+ {
+
+ // create the role
+ RolePath rolePath = new RolePath(roleName, jobList);
+ String roleDN = getFullDN(rolePath);
+ LDAPEntry roleNode;
+ try
+ {
+ roleNode = LDAPLookupUtils.getEntry(mLDAPAuth.getAuthObject(), getFullDN(rolePath));
+ throw new ObjectAlreadyExistsException();
+ } catch (ObjectNotFoundException ex) { }
+
+ //create CristalRole if it does not exist
+ roleNode = new LDAPEntry(roleDN, createAttributeSet(rolePath));
+ try {
+ LDAPLookupUtils.addEntry(mLDAPAuth.getAuthObject(),roleNode);
+ } catch (LDAPException e) {
+ throw new ObjectCannotBeUpdated(e.getLDAPErrorMessage(), "");
+ }
+ return rolePath;
+
+
+ }
+ public void deleteRole(RolePath role) throws ObjectNotFoundException, ObjectCannotBeUpdated {
+ try {
+ LDAPLookupUtils.delete(mLDAPAuth.getAuthObject(), getFullDN(role));
+ } catch (LDAPException ex) {
+ throw new ObjectCannotBeUpdated("Could not remove role");
+ }
+ }
+
+ @Override
+ public void addRole(AgentPath agent, RolePath role)
+ throws ObjectCannotBeUpdated, ObjectNotFoundException
+ {
+ LDAPEntry roleEntry = LDAPLookupUtils.getEntry(mLDAPAuth.getAuthObject(), getFullDN(role));
+ //add memberDN to uniqueMember if it is not yet a member
+ if (!LDAPLookupUtils.existsAttributeValue(roleEntry, "uniqueMember", getFullDN(agent)))
+ LDAPLookupUtils.addAttributeValue(mLDAPAuth.getAuthObject(), roleEntry, "uniqueMember", getFullDN(agent));
+ else
+ throw new ObjectCannotBeUpdated("Agent " + agent.getAgentName() + " already has role " + role.getName());
+ }
+
+ @Override
+ public void removeRole(AgentPath agent, RolePath role)
+ throws ObjectCannotBeUpdated, ObjectNotFoundException
+ {
+ LDAPEntry roleEntry = LDAPLookupUtils.getEntry(mLDAPAuth.getAuthObject(), getFullDN(role));
+ if (LDAPLookupUtils.existsAttributeValue(roleEntry, "uniqueMember", getFullDN(agent)))
+ LDAPLookupUtils.removeAttributeValue(mLDAPAuth.getAuthObject(), roleEntry, "uniqueMember", getFullDN(agent));
+ else
+ throw new ObjectCannotBeUpdated("Agent did not have that role");
+ }
+
+ @Override
+ public boolean hasRole(AgentPath agent, RolePath role) {
+ String filter = "(&(objectclass=cristalrole)(uniqueMember="+getFullDN(agent)+")(cn="+role.getName()+"))";
+ LDAPSearchConstraints searchCons = new LDAPSearchConstraints();
+ searchCons.setBatchSize(0);
+ searchCons.setDereference(LDAPSearchConstraints.DEREF_NEVER );
+ return search(mRolePath,LDAPConnection.SCOPE_SUB,filter,searchCons).hasNext();
+ }
+
+ @Override
+ public AgentPath[] getAgents(RolePath role)
+ throws ObjectNotFoundException
+ {
+ //get the roleDN entry, and its uniqueMember entry pointing to
+ LDAPEntry roleEntry;
+ try {
+ roleEntry = LDAPLookupUtils.getEntry(mLDAPAuth.getAuthObject(), getFullDN(role));
+ } catch (ObjectNotFoundException e) {
+ throw new ObjectNotFoundException("Role does not exist", "");
+ }
+
+ String[] res = LDAPLookupUtils.getAllAttributeValues(roleEntry,"uniqueMember");
+ ArrayList<AgentPath> agents = new ArrayList<AgentPath>();
+ for (String userDN : res) {
+ try {
+ LDAPEntry userEntry = LDAPLookupUtils.getEntry(mLDAPAuth.getAuthObject(), userDN);
+ AgentPath path = (AgentPath)nodeToPath(userEntry);
+ agents.add(path);
+ } catch (ObjectNotFoundException ex) {
+ Logger.error("Agent "+userDN+" does not exist");
+ } catch (InvalidItemPathException ex) {
+ Logger.error("Agent "+userDN+" is not a valid entity");
+ }
+ }
+ AgentPath[] usersList = new AgentPath[0];
+ usersList = agents.toArray(usersList);
+ return usersList;
+ }
+
+ //returns the role/s of a user
+ @Override
+ public RolePath[] getRoles(AgentPath agentPath)
+ {
+ //search the mDomainPath tree uniqueMember=userDN
+ //filter = objectclass=cristalrole AND uniqueMember=userDN
+ String filter = "(&(objectclass=cristalrole)(uniqueMember="+getFullDN(agentPath)+"))";
+ LDAPSearchConstraints searchCons = new LDAPSearchConstraints();
+ searchCons.setBatchSize(0);
+ searchCons.setDereference(LDAPSearchConstraints.DEREF_NEVER );
+ Iterator<?> roles = search(mRolePath,LDAPConnection.SCOPE_SUB,filter,searchCons);
+ ArrayList<RolePath> roleList = new ArrayList<RolePath>();
+
+ while(roles.hasNext())
+ {
+ RolePath path = (RolePath) roles.next();
+ roleList.add(path);
+ }
+ RolePath[] roleArr = new RolePath[roleList.size()];
+ roleArr = roleList.toArray(roleArr);
+ return roleArr;
+ }
+
+ /**
+ * Utility for looking up a login name
+ *
+ * @param ld
+ * @param agentName
+ * @param baseDN
+ * @return
+ * @throws ObjectNotFoundException
+ */
+ @Override
+ public AgentPath getAgentPath(String agentName) throws ObjectNotFoundException
+ {
+ //search to get the userDN equivalent of the userID
+ LDAPSearchConstraints searchCons = new LDAPSearchConstraints();
+ searchCons.setBatchSize(0);
+ searchCons.setDereference(LDAPSearchConstraints.DEREF_NEVER );
+ String filter = "(&(objectclass=cristalagent)(uid="+agentName+"))";
+ Iterator<Path> res = search(mItemTypeRoot,LDAPConnection.SCOPE_SUB,filter,searchCons);
+ if (!res.hasNext())
+ throw new ObjectNotFoundException("Agent not found: "+agentName, "");
+ Path result = res.next();
+ if (result instanceof AgentPath)
+ return (AgentPath)result;
+ else
+ throw new ObjectNotFoundException("Entry was not an Agent");
+ }
+
+ @Override
+ public RolePath getRolePath(String roleName) throws ObjectNotFoundException
+ {
+ LDAPSearchConstraints searchCons = new LDAPSearchConstraints();
+ searchCons.setBatchSize(0);
+ searchCons.setDereference(LDAPSearchConstraints.DEREF_NEVER );
+ String filter = "(&(objectclass=cristalrole)(cn="+roleName+"))";
+ Iterator<Path> res = search(mRolePath,LDAPConnection.SCOPE_SUB,filter,searchCons);
+ if (!res.hasNext())
+ throw new ObjectNotFoundException("Role not found");
+ Path result = res.next();
+ if (result instanceof RolePath)
+ return (RolePath)result;
+ else
+ throw new ObjectNotFoundException("Entry was not a Role");
+ }
+
+ @Override
+ public void setHasJobList(RolePath role, boolean hasJobList) throws ObjectNotFoundException, ObjectCannotBeUpdated {
+ // get entry
+ LDAPEntry roleEntry;
+ try {
+ roleEntry = LDAPLookupUtils.getEntry(mLDAPAuth.getAuthObject(), getFullDN(role));
+ } catch (ObjectNotFoundException e) {
+ throw new ObjectNotFoundException("Role does not exist", "");
+ }
+ // set attribute
+ LDAPLookupUtils.setAttributeValue(mLDAPAuth.getAuthObject(), roleEntry, "jobList", hasJobList?"TRUE":"FALSE");
+ }
+
+ @Override
+ public void setAgentPassword(AgentPath agent, String newPassword) throws ObjectNotFoundException, ObjectCannotBeUpdated, NoSuchAlgorithmException {
+ String encPasswd = AgentPath.generateUserPassword(newPassword, "SHA");
+ LDAPEntry agentEntry;
+ try {
+ agentEntry = LDAPLookupUtils.getEntry(mLDAPAuth.getAuthObject(), getFullDN(agent));
+ } catch (ObjectNotFoundException e) {
+ throw new ObjectNotFoundException("Agent "+agent.getAgentName()+" does not exist", "");
+ }
+ LDAPLookupUtils.setAttributeValue(mLDAPAuth.getAuthObject(), agentEntry, "userPassword", encPasswd);
+
+ }
+
+ @Override
+ public String getAgentName(AgentPath agentPath)
+ throws ObjectNotFoundException {
+ LDAPEntry agentEntry = LDAPLookupUtils.getEntry(mLDAPAuth.getAuthObject(), getFullDN(agentPath));
+ return LDAPLookupUtils.getFirstAttributeValue(agentEntry,"uid");
+ }
+
+}
diff --git a/src/main/java/com/c2kernel/lookup/LDAPLookupUtils.java b/src/main/java/com/c2kernel/lookup/ldap/LDAPLookupUtils.java index 0964565..e1c8ac4 100644 --- a/src/main/java/com/c2kernel/lookup/LDAPLookupUtils.java +++ b/src/main/java/com/c2kernel/lookup/ldap/LDAPLookupUtils.java @@ -2,7 +2,7 @@ * Lookup helper class.
*/
-package com.c2kernel.lookup;
+package com.c2kernel.lookup.ldap;
//import netscape.ldap.*;
//import netscape.ldap.util.*;
@@ -39,11 +39,36 @@ final public class LDAPLookupUtils LDAPEntry thisEntry = ld.read(dn,searchCons);
if (thisEntry != null) return thisEntry;
} catch (LDAPException ex) {
- throw new ObjectNotFoundException("LDAP Exception: "+ex.getMessage(), "");
+ throw new ObjectNotFoundException("LDAP Exception for dn:"+dn+": \n"+ex.getMessage(), "");
}
throw new ObjectNotFoundException(dn+" does not exist", "");
}
+
+
+ /**
+ * Utility method to connect to an LDAP server
+ * @param lp LDAP properties to connect with
+ * @return a novell LDAPConnection object
+ * @throws LDAPException when the connection was unsuccessful
+ */
+ public static LDAPConnection createConnection(LDAPProperties lp) throws LDAPException {
+ LDAPConnection ld = new LDAPConnection();
+
+ Logger.msg(3, "LDAPLookup - connecting to " + lp.mHost);
+ ld.connect(lp.mHost, Integer.valueOf(lp.mPort).intValue());
+
+ Logger.msg(3, "LDAPLookup - authenticating user:" + lp.mUser);
+ ld.bind( LDAPConnection.LDAP_V3, lp.mUser,
+ String.valueOf(lp.mPassword).getBytes());
+
+ Logger.msg(3, "LDAPLookup - authentication successful");
+ LDAPSearchConstraints searchCons = new LDAPSearchConstraints();
+ searchCons.setMaxResults(0);
+ ld.setConstraints(searchCons);
+
+ return ld;
+ }
//Given a DN, return an LDAP Entry
static public LDAPEntry getEntry(LDAPConnection ld, String dn)
@@ -290,7 +315,7 @@ final public class LDAPLookupUtils public static String escapeDN (String name) {
//From RFC 2253 and the / character for JNDI
-
+ if (name == null) return null;
String escapedStr = new String(name);
//Backslash is both a Java and an LDAP escape character, so escape it first
diff --git a/src/main/java/com/c2kernel/lookup/ldap/LDAPNextKeyManager.java b/src/main/java/com/c2kernel/lookup/ldap/LDAPNextKeyManager.java new file mode 100644 index 0000000..48f938e --- /dev/null +++ b/src/main/java/com/c2kernel/lookup/ldap/LDAPNextKeyManager.java @@ -0,0 +1,97 @@ +package com.c2kernel.lookup.ldap;
+
+import com.c2kernel.common.ObjectCannotBeUpdated;
+import com.c2kernel.common.ObjectNotFoundException;
+import com.c2kernel.lookup.AgentPath;
+import com.c2kernel.lookup.InvalidItemPathException;
+import com.c2kernel.lookup.ItemPath;
+import com.c2kernel.persistency.ClusterStorageException;
+import com.c2kernel.persistency.NextKeyManager;
+import com.c2kernel.process.Gateway;
+import com.c2kernel.process.auth.Authenticator;
+import com.c2kernel.utils.Logger;
+import com.novell.ldap.LDAPEntry;
+
+/**************************************************************************
+ *
+ * $Revision: 1.2 $
+ * $Date: 2005/04/27 13:47:24 $
+ *
+ * Copyright (C) 2003 CERN - European Organization for Nuclear Research
+ * All rights reserved.
+ **************************************************************************/
+
+// public static final String codeRevision = "$Revision: 1.2 $ $Date: 2005/04/27 13:47:24 $ $Author: abranson $";
+public class LDAPNextKeyManager implements NextKeyManager {
+
+ LDAPAuthManager ldap;
+ String lastKeyPath;
+
+ public LDAPNextKeyManager() {
+ super();
+ LDAPProperties props = new LDAPProperties(Gateway.getProperties());
+ this.lastKeyPath = "cn=last,cn=entity,"+props.mLocalPath;
+ }
+
+ public void open(Authenticator auth) {
+ this.ldap = (LDAPAuthManager)auth;
+ }
+
+ @Override
+ public synchronized ItemPath generateNextEntityKey()
+ throws ObjectCannotBeUpdated, ObjectNotFoundException
+ {
+ ItemPath lastKey = getLastEntityPath();
+
+ try {
+ lastKey.setSysKey(lastKey.getSysKey()+1);
+ } catch (InvalidItemPathException ex) {
+ throw new ObjectCannotBeUpdated("Invalid syskey "+(lastKey.getSysKey()+1)+". Maybe centre is full.");
+ }
+ //test that storage is empty for that key
+ try {
+ if (Gateway.getStorage().getClusterContents(lastKey.getSysKey(), "").length > 0)
+ throw new ObjectCannotBeUpdated("NextKeyManager: Storage already contains data for syskey "+lastKey.getSysKey()+
+ ". Storage is out of sync with nextkey. Please contact an administrator", "");
+ } catch (ClusterStorageException e) {
+ Logger.error(e);
+ throw new ObjectCannotBeUpdated("Could not check storage for prior data for the next generated systemKey: "+e.getMessage());
+ }
+
+ //set the last key
+ writeLastEntityKey(lastKey.getSysKey());
+
+ return lastKey;
+ }
+
+ @Override
+ public synchronized AgentPath generateNextAgentKey()
+ throws ObjectCannotBeUpdated, ObjectNotFoundException {
+ ItemPath newEntity = generateNextEntityKey();
+ return new AgentPath(newEntity);
+ }
+
+ @Override
+ public void writeLastEntityKey(int sysKey) throws ObjectCannotBeUpdated, ObjectNotFoundException {
+ LDAPEntry lastKeyEntry = LDAPLookupUtils.getEntry(ldap.getAuthObject(),lastKeyPath);
+ LDAPLookupUtils.setAttributeValue(ldap.getAuthObject(), lastKeyEntry,"intsyskey",Integer.toString(sysKey));
+ }
+
+ @Override
+ public ItemPath getLastEntityPath() throws ObjectNotFoundException
+ {
+ LDAPEntry lastKeyEntry = LDAPLookupUtils.getEntry(ldap.getAuthObject(),lastKeyPath);
+ String lastKey = LDAPLookupUtils.getFirstAttributeValue(lastKeyEntry,"intsyskey");
+ try {
+ int sysKey = Integer.parseInt(lastKey);
+ ItemPath sysPath = new ItemPath(sysKey);
+ return sysPath;
+ } catch (InvalidItemPathException ex) {
+ throw new ObjectNotFoundException("Invalid syskey. Maybe centre is full.");
+ } catch (NumberFormatException ex) {
+ throw new ObjectNotFoundException("Invalid syskey in lastkey.");
+ }
+
+ }
+
+}
diff --git a/src/main/java/com/c2kernel/lookup/LDAPPathSet.java b/src/main/java/com/c2kernel/lookup/ldap/LDAPPathSet.java index d3cf7d9..806976d 100644 --- a/src/main/java/com/c2kernel/lookup/LDAPPathSet.java +++ b/src/main/java/com/c2kernel/lookup/ldap/LDAPPathSet.java @@ -1,8 +1,8 @@ -package com.c2kernel.lookup;
+package com.c2kernel.lookup.ldap;
-import java.util.Enumeration;
+import java.util.Iterator;
-import com.c2kernel.process.Gateway;
+import com.c2kernel.lookup.Path;
import com.c2kernel.utils.Logger;
import com.novell.ldap.LDAPEntry;
import com.novell.ldap.LDAPException;
@@ -19,20 +19,23 @@ import com.novell.ldap.LDAPSearchResults; -public class LDAPPathSet implements Enumeration<Path> {
+public class LDAPPathSet implements Iterator<Path> {
LDAPSearchResults results;
LDAPEntry nextEntry;
+ LDAPLookup ldap;
- public LDAPPathSet() { // empty
+ public LDAPPathSet(LDAPLookup ldap) { // empty
+ this.ldap = ldap;
results = null;
- }
+ }
- public LDAPPathSet(LDAPSearchResults results) {
+ public LDAPPathSet(LDAPSearchResults results, LDAPLookup ldap) {
+ this.ldap = ldap;
this.results = results;
}
@Override
- public boolean hasMoreElements() {
+ public boolean hasNext() {
if (results == null) return false;
if (nextEntry != null) return true;
if (results.hasMore())
@@ -49,24 +52,30 @@ public class LDAPPathSet implements Enumeration<Path> { }
@Override
- public Path nextElement() {
+ public Path next() {
if (results == null) return null;
try {
if (nextEntry == null)
nextEntry = results.next();
- Path nextPath = Gateway.getLDAPLookup().nodeToPath(nextEntry);
+ Path nextPath = ldap.nodeToPath(nextEntry);
nextEntry = null;
return nextPath;
} catch (Exception ex) {
Logger.error("Error loading next path");
Logger.error(ex);
nextEntry = null;
- if (hasMoreElements()) {
+ if (hasNext()) {
Logger.error("Skipping to next entry");
- return nextElement();
+ return next();
}
else
return null;
}
}
+
+ @Override
+ public void remove() {
+ // do nothing
+
+ }
}
diff --git a/src/main/java/com/c2kernel/lookup/ldap/LDAPProperties.java b/src/main/java/com/c2kernel/lookup/ldap/LDAPProperties.java new file mode 100644 index 0000000..1e9f971 --- /dev/null +++ b/src/main/java/com/c2kernel/lookup/ldap/LDAPProperties.java @@ -0,0 +1,38 @@ +/*
+ * Directory Lookup Service
+*/
+
+package com.c2kernel.lookup.ldap;
+
+import com.c2kernel.utils.ObjectProperties;
+
+/**
+ * @version $Revision: 1.16 $ $Date: 2005/10/12 12:51:54 $
+ * @author $Author: abranson $
+ */
+public class LDAPProperties
+{
+ public String mGlobalPath = null; //o=cern.ch
+ public String mRootPath = null; //cn=cristal2
+ public String mLocalPath = null; //cn=lab27
+ public Integer mPort = null;
+ public String mHost = null;
+ public String mUser = null;
+ public String mPassword = null;
+
+ public LDAPProperties(ObjectProperties obj)
+ {
+ mGlobalPath = obj.getProperty( "LDAP.GlobalPath" );
+ mRootPath = obj.getProperty( "LDAP.RootPath" );
+ mLocalPath = obj.getProperty( "LDAP.LocalPath" );
+ mPort = obj.getInt( "LDAP.port", 389 );
+ mHost = obj.getProperty( "LDAP.host" );
+ mUser = obj.getProperty( "LDAP.user" );
+ mPassword = obj.getProperty( "LDAP.password" );
+
+ mRootPath += "," + mGlobalPath;
+ mLocalPath += "," + mRootPath;
+
+ }
+}
+
diff --git a/src/main/java/com/c2kernel/lookup/LDAPPropertyManager.java b/src/main/java/com/c2kernel/lookup/ldap/LDAPPropertyManager.java index fcf1ef8..1b6e906 100644 --- a/src/main/java/com/c2kernel/lookup/LDAPPropertyManager.java +++ b/src/main/java/com/c2kernel/lookup/ldap/LDAPPropertyManager.java @@ -1,10 +1,11 @@ -package com.c2kernel.lookup;
+package com.c2kernel.lookup.ldap;
import java.util.ArrayList;
import java.util.Enumeration;
import com.c2kernel.common.ObjectCannotBeUpdated;
import com.c2kernel.common.ObjectNotFoundException;
+import com.c2kernel.lookup.ItemPath;
import com.c2kernel.property.Property;
import com.c2kernel.utils.Logger;
import com.novell.ldap.LDAPAttribute;
@@ -24,29 +25,31 @@ public class LDAPPropertyManager { *
*/
protected LDAPLookup ldap;
+ private final LDAPAuthManager auth;
- public LDAPPropertyManager(LDAPLookup ldap) {
+ public LDAPPropertyManager(LDAPLookup ldap, LDAPAuthManager auth) {
super();
this.ldap = ldap;
+ this.auth = auth;
}
/**
- * @param thisEntity - EntityPath of the subject entity
+ * @param thisItem - EntityPath of the subject entity
* @return
* @throws ObjectNotFoundException
*/
- public boolean hasProperties(EntityPath thisEntity) throws ObjectNotFoundException {
- LDAPEntry entityEntry = LDAPLookupUtils.getEntry(ldap.getConnection(), thisEntity.getFullDN());
+ public boolean hasProperties(ItemPath thisItem) throws ObjectNotFoundException {
+ LDAPEntry entityEntry = LDAPLookupUtils.getEntry(auth.getAuthObject(), ldap.getFullDN(thisItem));
return entityEntry.getAttribute("cristalprop") != null;
}
/**
- * @param thisEntity - EntityPath of the subject entity
+ * @param thisItem - EntityPath of the subject entity
* @return array of Property
* @throws ObjectNotFoundException
*/
- public String[] getPropertyNames(EntityPath thisEntity) throws ObjectNotFoundException {
- LDAPEntry entityEntry = LDAPLookupUtils.getEntry(ldap.getConnection(), thisEntity.getFullDN());
+ public String[] getPropertyNames(ItemPath thisItem) throws ObjectNotFoundException {
+ LDAPEntry entityEntry = LDAPLookupUtils.getEntry(auth.getAuthObject(), ldap.getFullDN(thisItem));
ArrayList<String> propbag = new ArrayList<String>();
LDAPAttribute props = entityEntry.getAttribute("cristalprop");
for (Enumeration<?> e = props.getStringValues(); e.hasMoreElements();) {
@@ -61,27 +64,27 @@ public class LDAPPropertyManager { }
/**
- * @param thisEntity - EntityPath of the subject entity
+ * @param thisItem - EntityPath of the subject entity
* @param propName - the name of the property to retrieve
* @return The Property object
* @throws ObjectNotFoundException
*/
- public Property getProperty(EntityPath thisEntity, String name) throws ObjectNotFoundException {
- LDAPEntry entityEntry = LDAPLookupUtils.getEntry(ldap.getConnection(), thisEntity.getFullDN());
+ public Property getProperty(ItemPath thisItem, String name) throws ObjectNotFoundException {
+ LDAPEntry entityEntry = LDAPLookupUtils.getEntry(auth.getAuthObject(), ldap.getFullDN(thisItem));
return getProperty(entityEntry, name);
}
/**
- * @param thisEntity - EntityPath of the subject entity
+ * @param thisItem - EntityPath of the subject entity
* @param name - the property name to delete
* @throws ObjectNotFoundException
* @throws ObjectCannotBeUpdated
*/
- public void deleteProperty(EntityPath thisEntity, String name) throws ObjectNotFoundException, ObjectCannotBeUpdated {
- LDAPEntry entityEntry = LDAPLookupUtils.getEntry(ldap.getConnection(), thisEntity.getFullDN());
+ public void deleteProperty(ItemPath thisItem, String name) throws ObjectNotFoundException, ObjectCannotBeUpdated {
+ LDAPEntry entityEntry = LDAPLookupUtils.getEntry(auth.getAuthObject(), ldap.getFullDN(thisItem));
Property prop = getProperty(entityEntry, name);
Logger.msg(6, "LDAPLookupUtils.deleteProperty("+name+") - Deleting property");
- LDAPLookupUtils.removeAttributeValue(ldap.getConnection(), entityEntry, "cristalprop", getPropertyAttrValue(prop));
+ LDAPLookupUtils.removeAttributeValue(auth.getAuthObject(), entityEntry, "cristalprop", getPropertyAttrValue(prop));
}
private static String getPropertyAttrValue(Property prop) {
@@ -89,22 +92,22 @@ public class LDAPPropertyManager { }
/**
- * @param thisEntity - EntityPath of the subject entity
+ * @param thisItem - EntityPath of the subject entity
* @param prop - the property to store
* @throws ObjectNotFoundException
* @throws ObjectCannotBeUpdated
*/
- public void setProperty(EntityPath thisEntity, Property prop) throws ObjectNotFoundException, ObjectCannotBeUpdated {
- LDAPEntry entityEntry = LDAPLookupUtils.getEntry(ldap.getConnection(), thisEntity.getFullDN());
+ public void setProperty(ItemPath thisItem, Property prop) throws ObjectNotFoundException, ObjectCannotBeUpdated {
+ LDAPEntry entityEntry = LDAPLookupUtils.getEntry(auth.getAuthObject(), ldap.getFullDN(thisItem));
try {
Property oldProp = getProperty(entityEntry, prop.getName());
Logger.msg(6, "LDAPLookupUtils.setProperty("+prop.getName()+") - Removing old value '"+oldProp.getValue()+"'");
- LDAPLookupUtils.removeAttributeValue(ldap.getConnection(), entityEntry, "cristalprop", getPropertyAttrValue(oldProp));
+ LDAPLookupUtils.removeAttributeValue(auth.getAuthObject(), entityEntry, "cristalprop", getPropertyAttrValue(oldProp));
} catch (ObjectNotFoundException ex) {
Logger.msg(6, "LDAPLookupUtils.setProperty("+prop.getName()+") - creating new property.");
}
Logger.msg(6, "LDAPLookupUtils.setProperty("+prop.getName()+") - setting to '"+prop.getValue()+"'");
- LDAPLookupUtils.addAttributeValue(ldap.getConnection(), entityEntry, "cristalprop", getPropertyAttrValue(prop));
+ LDAPLookupUtils.addAttributeValue(auth.getAuthObject(), entityEntry, "cristalprop", getPropertyAttrValue(prop));
}
public static Property getProperty(LDAPEntry myEntry, String propName) throws ObjectNotFoundException {
|
