Get User password from LDAP using PUMA

LDAP does not return the password that it is encrypted in LDAP, you have to make a connection via LDAP can use this jar CLICK HERE and after making the connection you run a command that returns the password of a user

Example.:

code:


package br.com.tpd.sigresunificado.SSHLDAPConnection;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;

/**
* Class for connecting to an LDAP server using SSH protocol
*
* @author MaurĂ­cio Cababe
*
*/
public class SSHLDAPConnection
{
/**
* Return the user's password
*
* @param server
* The LDAP server to connect to
* @param authenticationUser
* The LDAP authentication user
* @param authenticationPassword
* The LDAP authentication password
* @param port
* The port to connect to
* @param user
* The user whose password will be retrieved
* @param timeout
* The timeout value in milliseconds for the server response
* @return
* @throws SSHLDAPException
*/
public static String getUserPassword(String server,
String authenticationUser, String authenticationPassword,
Integer port, String user, Integer timeout, Map configurations) throws SSHLDAPException
{

String cn = configurations.get("cn").toString();
String ou = configurations.get("ou").toString();
String o = configurations.get("o").toString();
String pwd = "-w " + configurations.get("pwd").toString();

String command = "idsldapsearch -b cn=" + cn + ",ou=" + ou + ","
+ "o=" + o + " -D cn=root " + pwd + " \"uid=" + user
+ "\" | grep user";

// Default timeout and port values
if(timeout == null)
{
timeout = new Integer(15000);
}
if(port == null)
{
port = new Integer(22);
}

// Create a new connection
Connection connection = new Connection(server, port.intValue());
if(connection == null)
{
throw new SSHLDAPException("Not able to connect to server "
+ server + " at port " + port.intValue(),
SSHLDAPException.ERROR_CODE_CONNECTION);
}

try
{
boolean authenticated = false;
Session session = null;

// Connect and authenticate
connection.connect();
authenticated = connection.authenticateWithPassword(
authenticationUser, authenticationPassword);
if(!authenticated)
{
throw new SSHLDAPException("Not able to authenticate user "
+ authenticationUser + " on server " + server,
SSHLDAPException.ERROR_CODE_AUTHENTICATION);
}

// Open a session and execute the command
session = connection.openSession();
System.out.println("command: " + command);
session.execCommand(command);

// Get the response
String response = "";
InputStream stdout = new StreamGobbler(session.getStdout());
BufferedReader br = new BufferedReader(
new InputStreamReader(stdout));
while(true)
{
String line = br.readLine();
response += line;
if(line == null)
{
break;
}
}
System.out.println("response: " + response);

// Close the session
session.close();

// Close the connection
session.close();

// Get the password from the response
try
{
String password = response.substring(13, response.length() - 4);
return password;
}
catch(Exception e)
{
throw new SSHLDAPException(
"An unknown error occured when executing the command",
SSHLDAPException.ERROR_CODE_UNKNOWN);
}
}
catch(IOException e)
{
throw new SSHLDAPException(e.getMessage(),
SSHLDAPException.ERROR_CODE_IO);
}
finally
{
connection.close();
}
}

public static void main(String[] args)
{
System.out.println("testing password...");
testPassword();
}

private static void testPassword()
{
String server = "***.***.***.***";
String authenticationUser = "idsldap";
String authenticationPassword = "ldapprs@2";
Integer port = new Integer("222222");
String user = "administrador";
Integer timeout = new Integer("5000000");
Map configurations = new HashMap();
configurations.put("cn", "pr");
configurations.put("ou", "portal");
configurations.put("o", "blablabla");
configurations.put("pwd", "tabajara");

try
{
System.out.println("Resposta: "
+ SSHLDAPConnection.getUserPassword(server,
authenticationUser, authenticationPassword, port,
user, timeout, configurations));
}
catch(Exception e)
{
e.printStackTrace();
}
}

}

0 comments: