1

I am working on creating a Ldap Socket in java, similar to A Simple Http Server with Java/Socket?
I want ldap socket to be able to receive ldap calls and print them and return a dummy ldap response in message back to client.
It seems to be able to connect to LDAP client but does not receive or send message to it.
LDAP Server :-

public class Server
{
    private Socket          socket   = null;
    private ServerSocket    server   = null;
    private DataInputStream in       =  null;

    public Server(int port)
    {
        try
        {
            server = new ServerSocket(port);
            System.out.println("Server started");

            System.out.println("Waiting for a client ...");

            socket = server.accept();
            System.out.println("Client accepted");

            // takes input from the client socket
            in = new DataInputStream(
                    new BufferedInputStream(socket.getInputStream()));

            String s;
            while ((s = in.readLine()) != null) {
                System.out.println(s);
                if (s.isEmpty()) {
                    break;
                }
            }
            System.out.println("Closing connection");

            // close connection
            socket.close();
            in.close();
        }
        catch(IOException i)
        {
            System.out.println(i);
        }
    }

    public static void main(String args[])
    {
        Server server = new Server(10389);
    }
}

LDAP Client

public class App 
{
    DirContext connection;

    public static void main( String[] args ) throws NamingException {
       App app = new App();
       app.createConnection();
       app.addUser();
    }

    private  void createConnection() {
        Properties properties = new Properties();
        properties.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
        properties.put(Context.PROVIDER_URL, "ldap://localhost:10389");
        properties.put(Context.SECURITY_PRINCIPAL, "uid=admin, ou=system");
        properties.put(Context.SECURITY_CREDENTIALS, "secret");

        try {
            connection = new InitialDirContext(properties);
            System.out.println( "Hello World! " + connection);

        } catch (NamingException e) {
            e.printStackTrace();
        } catch (Exception e){
            e.printStackTrace();
        }
    }

    public void addUser(){
        Attributes attributes = new BasicAttributes();
        Attribute attribute = new BasicAttribute("objectClass");
        attribute.add("inetOrgPerson");
        attributes.put(attribute);

        attributes.put("sn","c");
        try {
            connection.createSubcontext("cn=k,ou=users,ou=system", attributes);
        } catch (NamingException e) {
            e.printStackTrace();
        }
    }
}

Output:-
Server started
Waiting for a client ...
Client accepted

notFound
  • 50
  • 8
  • This is an enormous task. You have to implement the LDAP protocol, which includes ASN.1 BER parsing: and then you have to implement the LDAP semantics in the server. Far too broad and probably completely infeasible for you. – user207421 Jun 03 '21 at 01:40
  • I’m voting to close this question because it is far too broad to answer here. – user207421 Jun 03 '21 at 01:40
  • Any update? This would be awesome for testing purposes – JRichardsz Feb 02 '23 at 20:21

0 Answers0