// Path: src/main/java/com/asolutions/scmsshd/ldap/ILDAPAuthLookupProvider.java
// public interface ILDAPAuthLookupProvider {
// 
// 	Object provide(String url, String username, String password, boolean promiscuous) throws NamingException;
// 
// }
// 
// Path: src/main/java/com/asolutions/scmsshd/ssl/PromiscuousSSLSocketFactory.java
// public class PromiscuousSSLSocketFactory extends SocketFactory {
// 	protected final Logger log = LoggerFactory.getLogger(getClass());
// 	private static SocketFactory blindFactory = null;
// 
// 	/**
// 	 * 
// 	 * Builds an all trusting "blind" ssl socket factory.
// 	 * 
// 	 */
// 
// 	static {
// 		// create a trust manager that will purposefully fall down on the
// 		// job
// 		TrustManager[] blindTrustMan = new TrustManager[] { new X509TrustManager() {
// 			public X509Certificate[] getAcceptedIssuers() {
// 				return null;
// 			}
// 			public void checkClientTrusted(X509Certificate[] c, String a) {
// 			}
// 			public void checkServerTrusted(X509Certificate[] c, String a) {
// 			}
// 		} };
// 
// 		// create our "blind" ssl socket factory with our lazy trust manager
// 		try {
// 			SSLContext sc = SSLContext.getInstance("SSL");
// 			sc.init(null, blindTrustMan, new java.security.SecureRandom());
// 			blindFactory = sc.getSocketFactory();
// 		} catch (GeneralSecurityException e) {
// 			LoggerFactory.getLogger(PromiscuousSSLSocketFactory.class).error("Error taking security promiscuous" , e);
// 		}
// 	}
// 
// 	/**
// 	 * 
// 	 * @see javax.net.SocketFactory#getDefault()
// 	 * 
// 	 */
// 
// 	public static SocketFactory getDefault() {
// 		return new PromiscuousSSLSocketFactory();
// 	}
// 
// 	/**
// 	 * 
// 	 * @see javax.net.SocketFactory#createSocket(java.lang.String, int)
// 	 * 
// 	 */
// 
// 	@Override
// 	public Socket createSocket(String arg0, int arg1) throws IOException,
// 	UnknownHostException {
// 		return blindFactory.createSocket(arg0, arg1);
// 	}
// 
// 	/**
// 	 * 
// 	 * @see javax.net.SocketFactory#createSocket(java.net.InetAddress, int)
// 	 * 
// 	 */
// 
// 	@Override
// 	public Socket createSocket(InetAddress arg0, int arg1) throws IOException {
// 		return blindFactory.createSocket(arg0, arg1);
// 	}
// 
// 	/**
// 	 * 
// 	 * @see javax.net.SocketFactory#createSocket(java.lang.String, int,
// 	 * 
// 	 * java.net.InetAddress, int)
// 	 * 
// 	 */
// 
// 	@Override
// 	public Socket createSocket(String arg0, int arg1, InetAddress arg2, int arg3)
// 	throws IOException, UnknownHostException {
// 		return blindFactory.createSocket(arg0, arg1, arg2, arg3);
// 
// 	}
// 
// 	/**
// 	 * 
// 	 * @see javax.net.SocketFactory#createSocket(java.net.InetAddress, int,
// 	 * 
// 	 * java.net.InetAddress, int)
// 	 * 
// 	 */
// 
// 	@Override
// 	public Socket createSocket(InetAddress arg0, int arg1, InetAddress arg2, int arg3) throws IOException {
// 		return blindFactory.createSocket(arg0, arg1, arg2, arg3);
// 	}
// 
// }


import java.util.Properties;
import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.asolutions.scmsshd.ldap.ILDAPAuthLookupProvider;
import com.asolutions.scmsshd.ssl.PromiscuousSSLSocketFactory;
package com.asolutions.scmsshd.authenticators;





public class JavaxNamingLDAPAuthLookupProvider implements
		ILDAPAuthLookupProvider {
	protected final Logger log = LoggerFactory.getLogger(getClass());

	public Object provide(String url, String username, String password,
			boolean promiscuous) throws NamingException {
		InitialDirContext context = new InitialDirContext(getProperties(url, username, password, promiscuous));
		
		SearchControls searchCtls = new SearchControls();
		//Specify the search scope
		searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);

		//specify the LDAP search filter
		String searchFilter = "(objectClass=user)";

		//initialize counter to total the results

		// Search for objects using the filter
		NamingEnumeration<SearchResult> answer = context.search(username, searchFilter, searchCtls);
		return (answer.next());
	}
	
	public Properties getProperties(String url, String username, String password, boolean promiscuous)
	{
		Properties properties = new Properties();
		properties.setProperty(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
		properties.setProperty(Context.PROVIDER_URL, url);
		properties.setProperty(Context.SECURITY_PRINCIPAL, username);
		properties.setProperty(Context.SECURITY_CREDENTIALS, password);
		if (promiscuous){
// Path: src/main/java/com/asolutions/scmsshd/ldap/ILDAPAuthLookupProvider.java
// public interface ILDAPAuthLookupProvider {
// 
// 	Object provide(String url, String username, String password, boolean promiscuous) throws NamingException;
// 
// }
// 
// Path: src/main/java/com/asolutions/scmsshd/ssl/PromiscuousSSLSocketFactory.java
// public class PromiscuousSSLSocketFactory extends SocketFactory {
// 	protected final Logger log = LoggerFactory.getLogger(getClass());
// 	private static SocketFactory blindFactory = null;
// 
// 	/**
// 	 * 
// 	 * Builds an all trusting "blind" ssl socket factory.
// 	 * 
// 	 */
// 
// 	static {
// 		// create a trust manager that will purposefully fall down on the
// 		// job
// 		TrustManager[] blindTrustMan = new TrustManager[] { new X509TrustManager() {
// 			public X509Certificate[] getAcceptedIssuers() {
// 				return null;
// 			}
// 			public void checkClientTrusted(X509Certificate[] c, String a) {
// 			}
// 			public void checkServerTrusted(X509Certificate[] c, String a) {
// 			}
// 		} };
// 
// 		// create our "blind" ssl socket factory with our lazy trust manager
// 		try {
// 			SSLContext sc = SSLContext.getInstance("SSL");
// 			sc.init(null, blindTrustMan, new java.security.SecureRandom());
// 			blindFactory = sc.getSocketFactory();
// 		} catch (GeneralSecurityException e) {
// 			LoggerFactory.getLogger(PromiscuousSSLSocketFactory.class).error("Error taking security promiscuous" , e);
// 		}
// 	}
// 
// 	/**
// 	 * 
// 	 * @see javax.net.SocketFactory#getDefault()
// 	 * 
// 	 */
// 
// 	public static SocketFactory getDefault() {
// 		return new PromiscuousSSLSocketFactory();
// 	}
// 
// 	/**
// 	 * 
// 	 * @see javax.net.SocketFactory#createSocket(java.lang.String, int)
// 	 * 
// 	 */
// 
// 	@Override
// 	public Socket createSocket(String arg0, int arg1) throws IOException,
// 	UnknownHostException {
// 		return blindFactory.createSocket(arg0, arg1);
// 	}
// 
// 	/**
// 	 * 
// 	 * @see javax.net.SocketFactory#createSocket(java.net.InetAddress, int)
// 	 * 
// 	 */
// 
// 	@Override
// 	public Socket createSocket(InetAddress arg0, int arg1) throws IOException {
// 		return blindFactory.createSocket(arg0, arg1);
// 	}
// 
// 	/**
// 	 * 
// 	 * @see javax.net.SocketFactory#createSocket(java.lang.String, int,
// 	 * 
// 	 * java.net.InetAddress, int)
// 	 * 
// 	 */
// 
// 	@Override
// 	public Socket createSocket(String arg0, int arg1, InetAddress arg2, int arg3)
// 	throws IOException, UnknownHostException {
// 		return blindFactory.createSocket(arg0, arg1, arg2, arg3);
// 
// 	}
// 
// 	/**
// 	 * 
// 	 * @see javax.net.SocketFactory#createSocket(java.net.InetAddress, int,
// 	 * 
// 	 * java.net.InetAddress, int)
// 	 * 
// 	 */
// 
// 	@Override
// 	public Socket createSocket(InetAddress arg0, int arg1, InetAddress arg2, int arg3) throws IOException {
// 		return blindFactory.createSocket(arg0, arg1, arg2, arg3);
// 	}
// 
// }


// Path: src/main/java/com/asolutions/scmsshd/authenticators/JavaxNamingLDAPAuthLookupProvider.java
import java.util.Properties;
import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.asolutions.scmsshd.ldap.ILDAPAuthLookupProvider;
import com.asolutions.scmsshd.ssl.PromiscuousSSLSocketFactory;

package com.asolutions.scmsshd.authenticators;





public class JavaxNamingLDAPAuthLookupProvider implements
		ILDAPAuthLookupProvider {
	protected final Logger log = LoggerFactory.getLogger(getClass());

	public Object provide(String url, String username, String password,
			boolean promiscuous) throws NamingException {
		InitialDirContext context = new InitialDirContext(getProperties(url, username, password, promiscuous));
		
		SearchControls searchCtls = new SearchControls();
		//Specify the search scope
		searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);

		//specify the LDAP search filter
		String searchFilter = "(objectClass=user)";

		//initialize counter to total the results

		// Search for objects using the filter
		NamingEnumeration<SearchResult> answer = context.search(username, searchFilter, searchCtls);
		return (answer.next());
	}
	
	public Properties getProperties(String url, String username, String password, boolean promiscuous)
	{
		Properties properties = new Properties();
		properties.setProperty(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
		properties.setProperty(Context.PROVIDER_URL, url);
		properties.setProperty(Context.SECURITY_PRINCIPAL, username);
		properties.setProperty(Context.SECURITY_CREDENTIALS, password);
		if (promiscuous){
			properties.setProperty("java.naming.ldap.factory.socket", PromiscuousSSLSocketFactory.class.getName());