/************************************************************************** * 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 ItemPath { private String mAgentName=null; private String mPassword=null; public AgentPath(int syskey, String agentName) throws InvalidAgentPathException,InvalidItemPathException { super(syskey); if (agentName!=null && agentName.length()>0) setAgentName(agentName); else throw new InvalidAgentPathException(); } public AgentPath(int syskey) throws InvalidItemPathException { super(syskey); } public AgentPath(ItemPath entity) { super(); try { setSysKey(entity.getSysKey()); } catch (InvalidItemPathException 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; } @Override 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(); } @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; } }