From 254ee6f47eebfc00462c10756a92066e82cc1a96 Mon Sep 17 00:00:00 2001 From: Andrew Branson Date: Tue, 21 Jun 2011 15:46:02 +0200 Subject: Initial commit --- source/com/c2kernel/lookup/AgentPath.java | 152 ++++++++++++++++++++++++++++++ 1 file changed, 152 insertions(+) create mode 100755 source/com/c2kernel/lookup/AgentPath.java (limited to 'source/com/c2kernel/lookup/AgentPath.java') diff --git a/source/com/c2kernel/lookup/AgentPath.java b/source/com/c2kernel/lookup/AgentPath.java new file mode 100755 index 0000000..01c764f --- /dev/null +++ b/source/com/c2kernel/lookup/AgentPath.java @@ -0,0 +1,152 @@ +/************************************************************************** + * EntityPath.java + * + * $Revision: 1.12 $ + * $Date: 2005/10/13 08:15:00 $ + * + * Copyright (C) 2001 CERN - European Organization for Nuclear Research + * All rights reserved. + **************************************************************************/ + +package com.c2kernel.lookup; + +import java.security.MessageDigest; +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; + + +/** +* Extends Path to enforce SystemKey structure and support int form +* +* @version $Revision: 1.12 $ $Date: 2005/10/13 08:15:00 $ +* @author $Author: abranson $ +**/ +public class AgentPath extends EntityPath +{ + + private String mAgentName=null; + private String mPassword=null; + + public AgentPath(int syskey, String agentName) + throws InvalidAgentPathException,InvalidEntityPathException + { + super(syskey); + if (agentName!=null && agentName.length()>0) + setAgentName(agentName); + else + throw new InvalidAgentPathException(); + } + + public AgentPath(int syskey) + throws InvalidEntityPathException + { + super(syskey); + } + + public AgentPath(EntityPath entity) { + super(); + try { + setSysKey(entity.getSysKey()); + } catch (InvalidEntityPathException ex) { + //won't happen as the entity path was valid + } + } + + public void setAgentName(String agentID) + { + mAgentName = agentID; + } + + public String getAgentName() + { + if (mAgentName==null) + { + try { + LDAPEntry agentEntry = LDAPLookupUtils.getEntry(Gateway.getLDAPLookup().getConnection(), this.getDN() + mLocalPath); + mAgentName = LDAPLookupUtils.getFirstAttributeValue(agentEntry,"uid"); + } catch (ObjectNotFoundException e) { + mAgentName = ""; + } + } + return mAgentName; + } + + public RolePath[] getRoles() + { + return Gateway.getLDAPLookup().getRoleManager().getRoles(this); + } + + public boolean hasRole(RolePath role) { + return Gateway.getLDAPLookup().getRoleManager().hasRole(this, role); + } + + public boolean hasRole(String role) { + try { + return hasRole(Gateway.getLDAPLookup().getRoleManager().getRolePath(role)); + } catch (ObjectNotFoundException ex) { + return false; + } + } + + public void setPassword(String passwd) + { + mPassword = passwd; + } + + public String getPassword() + { + return mPassword; + } + + public String dump() { + return super.dump()+ + "\n agentID="+ + mAgentName; + } + + static String generateUserPassword(String pass, String algo) throws NoSuchAlgorithmException { + MessageDigest sha = MessageDigest.getInstance(algo); + sha.reset(); + sha.update(pass.getBytes()); + byte hash[] = sha.digest(); + StringBuffer digest = new StringBuffer("{").append(algo).append("}"); + digest.append(Base64.encode(hash)); + return digest.toString(); + } + + 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; + } + +} + -- cgit v1.2.3