I currently work in a project (should be open source) and try to implement something like a "ldap proxy"
currently I only want to receive requests from the client, pass them to a "real" ldap server and handle the response back to the requesting client.
But it doesn't work. I couldn't find out, where and why.
I included example code to illustrate the problem.
Any ideas are welcome.
thanks for your time
regards
Ingo Schaefer
/* Background: We want to create an universal user management system, which enhances the possibilities of existing operating systems and directory services. So it is necessary to make our system speak many protocols in order to be able to support existing applications. As one thing the system should be able to understand ldap-queries and should respond appropriate (after doing some logic with it) We want to be able to answer authentication requests from ldap-aware operating systems as well as answering search requests from e.g. Outlook in order to provide a global address book Currently the system is based on openldap 2.1.4 (or any newer version) Programming language: Java, J2SDK 1.4 LDAP-Library: Netscape LDAP SDK 4.1 Problem description:
I can't manage to get this working (get accessed as ldap server, query the "real" ldap
and give the response back to the client)
Reading the "pure" byte-stream is not enough, I have to "understand" the query and the
response.
Concrete: I got an unspecified java.io.IOException (e.toString gives:
"java.io.IOException", without message)
Example:
*/
import java.io.*;
import java.net.*;
import netscape.ldap.ber.stream.*;
import netscape.ldap.LDAPMessage;
import netscape.ldap.client.*;
import java.util.*;
public class ConnectionHandler extends Thread {
private Socket clientsocket=null; /* Here is the ldapBrowser */
private Socket ldapsocket=null; /* Here is the openLDAP-Server */
private OutputStream out = null;
private OutputStream ldapout = null;
private InputStream in = null;
private InputStream ldapin=null;
private boolean shutdown=false;
private BERTagDecoder decoder; /* Decoder to decode requests from
client */
private BERTagDecoder decoderldap; /* Decoder to decode responses from
server */
public ConnectionHandler( Socket connectedSocket ) throws IllegalArgumentException
{
if (connectedSocket == null) {
throw new IllegalArgumentException("You have to provide a connected
socket.");
}
else {
clientsocket = connectedSocket;
}
try {
out = new BufferedOutputStream(clientsocket.getOutputStream());
in = new BufferedInputStream(clientsocket.getInputStream());
}
catch (IOException e) {
throw new IllegalArgumentException("could not get stream from your
socket");
}
try {
ldapsocket = new Socket("localhost",389);
}
catch (IOException e) {
throw new Error("Could not connect to Server");
}
decoder = new JDAPBERTagDecoder();
decoderldap = new JDAPBERTagDecoder(); /* could I handle two streams
with one Decoder-instance? */
}
public void run() {
BERElement element=null;
BERElement ldapelement=null;
while (clientsocket.isConnected()) {
if (shutdown){
return;
}
else {
yield();
int[] nread = new int[1];
nread[0] = 0;
try {
element = BERElement.getElement(decoder,
in,
nread);
}
catch (IOException e) {
System.out.println("IOException while reading from client");
shutdown=true;
}
try {
if (ldapout==null) {
ldapout = new
BufferedOutputStream(ldapsocket.getOutputStream());
}
if (ldapin==null) {
ldapin = new BufferedInputStream(ldapsocket.getInputStream());
}
}
catch (IOException e) {
System.out.println("Could not get server-Streams");
shutdown=true;
}
System.out.println("---input---"+"\r");
System.out.println(element.toString());
try{
element.write(ldapout);
ldapout.flush();
}
catch(IOException e)
{
System.out.println("sth went wrong writing to server");
}
yield();
int[] nreadldap = new int[1];
nreadldap[0] = 0;
try {
ldapelement = BERElement.getElement(decoderldap,
ldapin,
nreadldap);
}
catch (IOException e) {
System.out.println("IOException reading from server");
System.out.println(e.toString());
shutdown=true;
}
// responsemsg = LDAPMessage.parseMessage(ldapelement);
System.out.println("---output---"+"\r");
System.out.print(ldapelement.toString());
try{
ldapelement.write(out);
out.flush();
}
catch (IOException e)
{
System.out.println("sth went wrong writing to client.");
}
}
}
}
public static void main(String[] args) {
try{
ServerSocket socket = new ServerSocket(1500);
Socket clientsocket=socket.accept();
ConnectionHandler connHdl = new ConnectionHandler(clientsocket);
System.out.println("Connection accepted from:" +
clientsocket.getRemoteSocketAddress().toString().trim());
connHdl.start();
}
catch(IllegalArgumentException e)
{
System.out.println(e.toString());
}
catch(IOException e)
{
System.out.println("Could not bind to port." + e.toString());
}
}
}
