Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ASDI and Java

Status
Not open for further replies.

mingus

Programmer
May 8, 2001
59
0
0
US
Windows has something called the Active Services Directory Interface (ASDI).

I am trying to get data from it via Java.
I have looked for help, but keep getting rather vauge answers. ("It's like LDAP using JNDI, but...").

I never used LDAP and until today never heard of ASDI. My Java's good, but my understanding of ASDI, JNDI and LDAP is just beginning.

Does anybody have a good example of doing getting data from ASDI via Java or know where to find one?

mingus
 
Could an example of Java/LDAP be useful to you? Pedro Andrés Solorzano
Pontificia Universidad Javeriana
Bogotá, Colombia, SurAmérica.
 
I think so.

Trying to go to an Exchange Server which I think is LDAP.

tim
 

Hi mingus, check this code:


import java.io.PrintStream;
import java.text.MessageFormat;
import java.util.*;
import javax.naming.*;
import javax.naming.directory.*;


public class SimpleAuthenticate
{

private Hashtable env;
private Vector ldapHostList;
private Vector ldapPortList;
private String baseDN;
private String isRegisAuth;
private String userID;
private String modifiedDn;
private boolean debug;
private boolean status;
private DirContext ctx;
private String debugStr;
private boolean readConfig;

public SimpleAuthenticate()
{
debug = false;
status = false;
readConfig = false;
ldapHostList = new Vector();
ldapPortList = new Vector();
try
{
if(!readConfig)
readConfig = readConfigParams("authentication");
}
catch(Exception exception)
{
exception.printStackTrace();
}
if(debug)
System.out.println("Debug flag set..");
else
System.out.println("Debug flag not set...");
}

public SimpleAuthenticate(String s)
{
debug = false;
status = false;
readConfig = false;
try
{
if(!readConfig)
readConfig = readConfigParams(s);
}
catch(Exception exception)
{
printDebug("Exception..." + exception.toString());
}
}

public SimpleAuthenticate(String s, String s1, String s2, String s3, String s4)
{
debug = false;
status = false;
readConfig = false;
ldapHostList = new Vector();
ldapPortList = new Vector();
try
{
if(s == null || s.trim().length() <= 1)
{
readConfig = false;
throw new SimpleLdapException(&quot;ldapHost(s) Not Specified !!!&quot;);
}
ldapHostList = resolveProperty(s);
readConfig = true;
if(s1 == null || s1.trim().length() <= 1)
{
readConfig = false;
throw new SimpleLdapException(&quot;ldapPort(s) Not Specified !!!&quot;);
}
ldapPortList = resolveProperty(s1);
readConfig = true;
if(s2 == null || s2.trim().length() <= 0)
throw new SimpleLdapException(&quot;BaseDN Not Specified&quot;);
baseDN = s2;
boolean flag = ldapHostList.size() == ldapPortList.size();
if(!flag)
{
readConfig = false;
throw new SimpleLdapException(&quot;Ldap Hosts != Ldap Ports&quot;);
}
isRegisAuth = s3;
if(s3 == null || s3.trim().length() <= 0)
s3 = new String(&quot;false&quot;);
if(s4 == null || s4.trim().length() <= 0 || !s4.equalsIgnoreCase(&quot;true&quot;))
debug = false;
else
if(s4.equalsIgnoreCase(&quot;true&quot;))
debug = true;
else
debug = false;
}
catch(SimpleLdapException simpleldapexception)
{
readConfig = false;
simpleldapexception.printStackTrace();
}
}

public boolean authenticate(String s, String s1)
{
userID = s;
status = false;
ctx = null;
if(readConfig)
{
try
{
ctx = getAnonymousBind();
if(ctx != null)
{
modifiedDn = getDNForUser(ctx, s);
closeConnection(ctx);
status = ldapAuthenticate(s, s1);
}
if(!status && isRegisAuth.equalsIgnoreCase(&quot;true&quot;))
try
{
printDebug(&quot;***regis Authentication will be happening now***&quot;);
status = regisAuthenticate(s, s1);
}
catch(Exception exception)
{
status = false;
throw new Exception(&quot;Authentication Failed...&quot;);
}
else
if(!isRegisAuth.equalsIgnoreCase(&quot;true&quot;))
printDebug(&quot;RegisAuth not enabled...&quot;);
if(status)
printDebug(&quot;Authenticated...&quot;);
else
printDebug(&quot;Authentication Failed...&quot;);
}
catch(Exception exception1)
{
status = false;
exception1.printStackTrace();
}
} else
{
status = false;
System.err.println(&quot;Error in Config Files...&quot;);
}
return status;
}

private void closeConnection(DirContext dircontext)
{
try
{
dircontext.close();
}
catch(Exception exception)
{
printDebug(&quot;Exception closing the Context...&quot; + exception.toString());
}
}

private DirContext getAnonymousBind()
{
Hashtable hashtable = new Hashtable(2);
boolean flag = false;
for(int i = 0; i < ldapHostList.size(); i++)
{
boolean flag1;
try
{
hashtable.put(&quot;java.naming.factory.initial&quot;, &quot;com.sun.jndi.ldap.LdapCtxFactory&quot;);
hashtable.put(&quot;java.naming.provider.url&quot;, &quot;ldap://&quot; + ldapHostList.elementAt(i).toString() + &quot;:&quot; + ldapPortList.elementAt(i).toString());
ctx = new InitialDirContext(hashtable);
flag1 = true;
}
catch(CommunicationException communicationexception)
{
flag1 = false;
}
catch(NamingException namingexception)
{
flag1 = false;
}
if(flag1)
break;
}

return ctx;
}

public String getAuthUserID()
{
if(userID != null)
return userID;
else
return null;
}

private String getDNForUser(DirContext dircontext, String s)
throws Exception
{
String s1 = &quot;employeenumber={0}&quot;;
String s2 = MessageFormat.format(s1, new String[] {
s
});
SearchControls searchcontrols = new SearchControls();
searchcontrols.setSearchScope(2);
searchcontrols.setReturningObjFlag(false);
searchcontrols.setCountLimit(1L);
NamingEnumeration namingenumeration = dircontext.search(baseDN, s2, searchcontrols);
if(namingenumeration.hasMore())
{
SearchResult searchresult = (SearchResult)namingenumeration.next();
modifiedDn = searchresult.getName() + ',' + baseDN;
}
return modifiedDn;
}

private boolean ldapAuthenticate(String s, String s1)
throws SimpleLdapException
{
boolean flag = false;
status = false;
printDebug(&quot;checking password for &quot; + s);
env = new Hashtable();
env.put(&quot;java.naming.factory.initial&quot;, &quot;com.sun.jndi.ldap.LdapCtxFactory&quot;);
if(modifiedDn != null)
env.put(&quot;java.naming.security.principal&quot;, modifiedDn);
else
throw new SimpleLdapException(&quot;Unable to retrieve Dn for User..&quot; + s);
if(s1.trim().length() > 0)
env.put(&quot;java.naming.security.credentials&quot;, s1);
else
if(s1 == null || s1.trim().length() == 0)
{
System.err.println(&quot;Anonymous Auth Not allowed...&quot;);
return false;
}
env.put(&quot;java.naming.security.authentication&quot;, &quot;simple&quot;);
for(int i = 0; i < ldapHostList.size(); i++)
{
env.put(&quot;java.naming.provider.url&quot;, &quot;ldap://&quot; + ldapHostList.elementAt(i).toString() + &quot;:&quot; + ldapPortList.elementAt(i).toString());
printDebug(&quot;Authenticating against,&quot; + ldapHostList.elementAt(i));
try
{
ctx = new InitialDirContext(env);
closeConnection(ctx);
status = true;
}
catch(CommunicationException communicationexception)
{
status = false;
}
catch(ServiceUnavailableException serviceunavailableexception)
{
status = false;
throw new SimpleLdapException(&quot;Requested Service Unavailable...\n\n&quot;);
}
catch(AuthenticationException authenticationexception)
{
status = false;
printDebug(&quot;Now Regis auth.. if enabled...!!!&quot;);
break;
}
catch(Exception exception1)
{
status = false;
throw new SimpleLdapException(&quot;\n\nUnowned Exception\n\n&quot; + exception1.toString());
}
if(status)
break;
printDebug(&quot;continuing to search next ldap.. continue.....&quot;);
}

if(!status && isRegisAuth.equalsIgnoreCase(&quot;true&quot;))
try
{
printDebug(&quot;regis Authentication will be happening now.&quot;);
status = regisAuthenticate(s, s1);
}
catch(Exception exception)
{
status = false;
throw new SimpleLdapException(&quot;\nAuthentication Failed...\n&quot;);
}
else
if(!isRegisAuth.equalsIgnoreCase(&quot;true&quot;))
printDebug(&quot;RegisAuth not enabled...&quot;);
return status;
}

public void printDebug(String s)
{
if(debug)
System.out.println(&quot;SCORE JavaAppAuthe: &quot; + s);
}

private boolean readConfigParams(String s)
throws SimpleLdapException
{
System.out.println(&quot;Aplicacion de autenticacion LDAP, modificada por PEDRO SOLORZANO (13/12/2002).\nOriginal por SCORE group.&quot;);
ResourceBundle resourcebundle = null;
if(s.trim() != null)
{
try
{
resourcebundle = ResourceBundle.getBundle(s);
}
catch(MissingResourceException missingresourceexception)
{
throw new SimpleLdapException(&quot;\n\nConfigFile, &quot; + s + &quot; Not Found...\n\n&quot;);
}
} else
{
printDebug(&quot;ConfigFile, is null&quot;);
return false;
}
String s1 = resourcebundle.getString(&quot;LDAP_HOSTS&quot;);
if(s1 == null || s1.trim().length() <= 1)
{
readConfig = false;
throw new SimpleLdapException(&quot;ldapHost(s) Not Specified !!!&quot;);
}
ldapHostList = resolveProperty(s1);
readConfig = true;
String s2 = resourcebundle.getString(&quot;LDAP_PORTS&quot;);
if(s2 == null || s2.trim().length() <= 1)
{
readConfig = false;
throw new SimpleLdapException(&quot;ldapPort(s) Not Specified !!!&quot;);
}
ldapPortList = resolveProperty(s2);
readConfig = true;
baseDN = resourcebundle.getString(&quot;LDAP_BASEDN&quot;);
if(baseDN == null || baseDN.trim().length() <= 0)
throw new SimpleLdapException(&quot;BaseDN Not Specified&quot;);
boolean flag = ldapHostList.size() == ldapPortList.size();
if(!flag)
{
readConfig = false;
throw new SimpleLdapException(&quot;Ldap Hosts != Ldap Ports&quot;);
}
isRegisAuth = resourcebundle.getString(&quot;REGIS_AUTHENTICATE&quot;);
if(isRegisAuth == null || isRegisAuth.trim().length() <= 0)
isRegisAuth = new String(&quot;false&quot;);
debugStr = resourcebundle.getString(&quot;DEBUG&quot;);
if(debugStr == null || debugStr.trim().length() <= 0 || !debugStr.equalsIgnoreCase(&quot;true&quot;))
debug = false;
else
if(debugStr.equalsIgnoreCase(&quot;true&quot;))
debug = true;
else
debug = false;
return readConfig;
}

private boolean regisAuthenticate(String s, String s1)
throws Exception
{
boolean flag = false;
try
{
Password.checkPassword(s, s1);
flag = true;
}
catch(DefaultPasswordException defaultpasswordexception)
{
flag = true;
}
catch(PasswordException passwordexception)
{
if(passwordexception.getMessage().startsWith(&quot;Password&quot;))
flag = false;
else
throw new Exception(passwordexception.getMessage());
}
return flag;
}

private Vector resolveProperty(String s)
{
StringTokenizer stringtokenizer = new StringTokenizer(s, &quot;,&quot;);
Vector vector = new Vector();
String s1;
for(; stringtokenizer.hasMoreTokens(); vector.add(s1))
s1 = stringtokenizer.nextToken().trim();

return vector;
}

public void setDebug(boolean flag)
{
debug = flag;
}
}




You can't compile this class because there are two classes left, an exception and a password manager. I think it is not useful to you to run this app because its adapted to an specific company LDAP server. Just to guide you. Pay attention to the java.naming uses and to the functions

getAnonymousBind() and
getDNForUser(DirContext dircontext...

Here is a sample of the configuration file:


LDAP_HOSTS=(company)-ds
LDAP_PORTS=###
LDAP_BASEDN=dc=(company),dc=com
REGIS_AUTHENTICATE=true
DEBUG=true


this configuration parameters guides the app to the LDAP server to authenticate.

Hope it helps, post any question you may have...

Pedro Andrés Solorzano
Pontificia Universidad Javeriana
Bogotá, Colombia, SurAmérica.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top