import java.io.*;
import java.util.*;
import javax.naming.ldap.*;
import javax.naming.directory.*;
import javax.naming.*;
import java.security.cert.*;

class getTemp
{
    public static void ShowListCert(NamingEnumeration ans)
    {
        int count=0;
        System.out.println("");
        try
        {
            NamingEnumeration answer = ans;
            if(answer.hasMoreElements())
            {
                while(answer.hasMore())
                {
                    SearchResult sr = (SearchResult)answer.next();
                    Attributes pp = sr.getAttributes();
                    for (Enumeration e = pp.getAll() ; e.hasMoreElements() ;)
                    {
                        Attribute attr = (Attribute) e.nextElement();
                        String attrID = attr.getID();
                        try
                        {
                            if(attrID.equals("userTemplate"))
                            {
                                for(int i=0;i<attr.size();i++)
                                {
                                    Object csd = attr.get(i);
                                    byte[] csd_b = (byte[]) csd;
                                    ByteArrayInputStream bis1 = new ByteArrayInputStream(csd_b);
                                    CertificateFactory cf1 = CertificateFactory.getInstance("X.509");
                                    Certificate cert = cf1.generateCertificate(bis1);
                                    X509Certificate x509cert = (X509Certificate)cert;
                                    java.security.Principal principal = x509cert.getSubjectDN();
                                    System.out.println("SubjectDN: " + principal.getName()+"\n\n");
                                    count++;
                                }
                            }
                        }
                        catch(Exception f)
                        {
                            System.out.println("-------------------------------------------");
                            f.printStackTrace();
                            System.out.println("-------------------------------------------");
                        }
                    }
                }
            }
        }
        catch(Exception e)
        {
            System.out.println(e);
        }
        System.out.println("Count templates: " + count);
    }
    public static void main(String args[])
    {
        try
        {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

            String arg[] = new String[4];
            int i=0;
            while(i<arg.length)
            {
                switch(i)
                {
                    case 0: System.out.print("Enter ip store: ");break;
                    case 1: System.out.print("Enter user name: ");break;
                    case 2: System.out.print("Enter user password: ");break;
                    case 3: System.out.print("Eneter DN name: ");break;
                }
                arg[i] = br.readLine();
                if(arg[i].length()!=0)
                    i++;
            }
            String ldapURL = arg[0];
            String userName = "dn:t=L;cn="+arg[1];
            String userPass = arg[2];
            String dnName = arg[3];

            Hashtable<String, String> env = new Hashtable<String, String>();
            env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
            env.put(Context.SECURITY_AUTHENTICATION,"DIGEST-MD5");
            env.put(Context.PROVIDER_URL,ldapURL);
            env.put(Context.SECURITY_PRINCIPAL,userName);
            env.put(Context.SECURITY_CREDENTIALS,userPass);
            env.put("java.naming.ldap.attributes.binary","userTemplate");

            LdapContext ctx = new InitialLdapContext(env, null);
            SearchControls ctls = new SearchControls();
            ctls.setSearchScope(SearchControls.SUBTREE_SCOPE);
            NamingEnumeration answer = ctx.search(dnName, "cn=*", ctls);
            ShowListCert(answer);
            ctx.close();
        }
        catch(Exception e){System.out.println("Exception: "+ e +"\n Possible, there were enter not correct data");}
    }
}