import com.novell.security.nmas.lcm.LCMEnvironment;
import com.novell.security.nmas.client.NMASCallback;
import com.novell.security.nmas.client.InvalidNMASCallbackException;
import com.novell.security.nmas.client.NMASCallbackHandler;
import com.novell.security.nmas.lcm.registry.LCMRegistryException;
import com.novell.security.nmas.lcm.registry.GenLCMRegistry;
import com.novell.security.nmas.lcm.registry.LCMRegistry;
import com.novell.security.nmas.NMASConstants;
import com.novell.security.nmas.ui.GenLcmUI;
import com.novell.security.nmas.ui.NMASTrace;
import com.novell.ldap.LDAPConnection;
import com.novell.ldap.LDAPException;
import com.novell.ldap.LDAPJSSESecureSocketFactory;
import javax.swing.*;
import javax.security.auth.callback.Callback;
import javax.security.auth.callback.UnsupportedCallbackException;
import javax.security.auth.callback.PasswordCallback;
import java.security.Security;
import java.util.Map;
import java.util.HashMap;
public class LCMTester
{
private String userDN = null;
private String password = null;
private String ldapHost = null;
private String methodName = null;
private String methodPath = null;
private String sslPath = null;
private int ldapPort = 389;
private LDAPConnection lc = null;
private boolean ssl = false;
public LCMTester(String host, int port, String method, String path, String sslpath, String user, String passwd) {
ldapHost = host;
ldapPort = port;
methodName = method;
methodPath = path;
sslPath = sslpath;
userDN = user;
password = passwd;
if(sslPath.equals("") == false)
ssl=true;
}
public void connectAndTest(JTextArea results)
{
if(ssl)
{
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
System.setProperty("javax.net.ssl.trustStore", sslPath);
lc = new LDAPConnection(new LDAPJSSESecureSocketFactory());
results.append("Doing an SSL Bind\r\n");
}
else
{
lc = new LDAPConnection();
results.append("Doing an Simple Bind\r\n");
}
String [] mechanisms = {"NMAS_LOGIN"};
Map props=new HashMap();
props.put("javax.security.sasl.client.pkgs","com.novell.sasl.client");
props.put("LoginSequence", methodName);
GenLcmUI lcmEnv = new GenLcmUI();
GenLCMRegistry lcmRegistry = new GenLCMRegistry();
try
{
lcmRegistry.registerLcm(methodPath);
}
catch(LCMRegistryException lcmRegE)
{
results.append("LCMRegistryException thrown: " + lcmRegE.toString() + "\r\n");
}
MyCallbackHdlr cbh = new MyCallbackHdlr(lcmEnv, lcmRegistry);
try
{
lc.connect( ldapHost, ldapPort );
lc.bind( userDN,
"dn:" + userDN,
mechanisms,
props,
cbh);
results.append("bind() complete\r\n");
results.append("NMAS Return Code = " + cbh.getNmasRetCode() + "\r\n");
if(lc.isBound())
results.append("LDAPConnection.bind() succeeded \r\n");
else
results.append("LDAPConnection.bind() failed (The Connection is NOT bound)\r\n");
if(lc.isConnected())
results.append("The connection is connected\r\n");
if(lc.isConnectionAlive())
results.append("The connection is alive\r\n\n");
}
catch( LDAPException lde )
{
results.append( "Error ==> " + lde.toString() + "\r\n\n");
}
}
public class MyCallbackHdlr extends NMASCallbackHandler
{
MyCallbackHdlr(LCMEnvironment env, LCMRegistry registry)
{
super(env, registry);
}
public void handle(Callback [] callbacks) throws UnsupportedCallbackException
{
for(int cbCnt = 0; cbCnt < callbacks.length; cbCnt++)
{
if(callbacks[cbCnt] instanceof NMASCallback)
{
try
{
handleNMASCallback((NMASCallback) callbacks[cbCnt]);
}
catch(InvalidNMASCallbackException uce)
{
}
}
else if(callbacks[cbCnt] instanceof PasswordCallback)
{
((PasswordCallback)callbacks[cbCnt]).setPassword(password.toCharArray());
}
else
{
throw new UnsupportedCallbackException(callbacks[cbCnt]);
}
}
}
}
}