venerdì 12 giugno 2015

List All Entries in a LDAP Directory with JSP/JNDI


<%@page import="java.util.*" %>
<%@page import="javax.naming.ldap.*" %>
<%@page import="javax.naming.directory.*"%>
<%@page import="javax.naming.directory.InitialDirContext"%>
<%@page import="javax.naming.directory.DirContext"%>
<%@page import="javax.naming.Context" %>
<%@page import="javax.naming.InitialContext" %>
<%@page import="javax.naming.NamingException" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
<%@page import="java.util.*" %>
<%@page import="javax.naming.*" %>


<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h3>Rubrica:</h3>
        <%
            //CREATING AN INITIAL CONTEXT for search function:
            //context = objects whose state is a set of bindings (=ldap entries), that have distinct atomic names.
            //The Hashtable class represents the environments properties parameters           
            Hashtable env = new Hashtable();

            env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
            env.put(Context.PROVIDER_URL, "ldap://localhost:1389/o=Rubrica,dc=example,dc=com");
            env.put(Context.SECURITY_AUTHENTICATION, "simple");
            env.put(Context.SECURITY_PRINCIPAL, "cn=Directory Manager");
            env.put(Context.SECURITY_CREDENTIALS, "secret");
            //create initial context
            DirContext ctx = new InitialDirContext(env);

            try {
                //get listing of context
                NamingEnumeration list = ctx.list("");                      //class Context.list: contains object name + class name
//                NamingEnumeration bindings = ctx.listBindings("");        //class Context.listbindings: contains object name + class name + object
                while (list.hasMore()) {
                    NameClassPair ncPair = (NameClassPair) list.next();      //ClassPair= name + class name             
                    out.print("<b>" + ncPair.getName() + "</b>  </br>");
                    Attributes attrs = ctx.getAttributes(ncPair.getName());
                    for (NamingEnumeration ae = attrs.getAll(); ae.hasMore();) {
                        Attribute attr = (Attribute) ae.next();
                        out.println(attr.getID());
                        for (NamingEnumeration e = attr.getAll(); e.hasMore();) {
                            out.println(": " + e.next() + "<br>");
                        }
                    }
                        out.println("------------------------------------------------------<br><br>");                   
//               Binding bd = (Binding)bindings.next();
//                 out.println(bd.getName() + ":</br></br> " + bd.getObject());               
                }
                ctx.close();
            } catch (NamingException e) {
                out.println("List failed: " + e);
            }
        %>


        <br><br><br>
    </body>
</html>

giovedì 11 giugno 2015

JNDI / JSP Ldap Search, Add, Remove Entry.

<%@page import="javax.naming.NamingEnumeration"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<%@page import="java.util.*" %>
<%@page import="javax.naming.ldap.*" %>
<%@page import="javax.naming.directory.*"%>
<%@page import="javax.naming.directory.InitialDirContext"%>
<%@page import="javax.naming.directory.DirContext"%>
<%@page import="javax.naming.Context" %>
<%@page import="javax.naming.InitialContext" %>
<%@page import="javax.naming.NamingException" %>


<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
                            <h3>Rubrica:</h3>
       
                           
                           
                            <!-- SEARCH ENTRY  -->
       
        <h3>Search Entry:</h3>
        <form method="post">
            Search Entry: <input type="text" name="search""><br>
        <input type="submit" value="search">
        </form>
        <br><br>
       
        <%
        //CREATING AN INITIAL CONTEXT for search function:
        //context = objects whose state is a set of bindings (=ldap entries), that have distinct atomic names.
        //The Hashtable class represents the environments properties parameters           
            Hashtable env = new Hashtable();


            env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");                    
            env.put(Context.PROVIDER_URL, "ldap://localhost:1389/o=Rubrica,dc=example,dc=com");

            env.put(Context.SECURITY_AUTHENTICATION, "simple");           
                                 
            env.put(Context.SECURITY_PRINCIPAL,"cn=Directory Manager");           
            env.put(Context.SECURITY_CREDENTIALS,"secret");
                       

            DirContext ctx = new InitialDirContext(env);
           
           
       
        String searchName = (String)request.getParameter("search");
       
         try{
            request.getParameter("search");
            Attributes attrs = ctx.getAttributes("cn = " + searchName);
            out.println(attrs.get("cn").get());           
            out.println(" telephone number: "+attrs.get("telephonenumber").get());
        }
         catch (Exception e){
            out.println("An exception occurred: " + e.getMessage());
         }       
        %>
       
       
       
       
       
        <br>------------------------------------</br>
                        <!-- ADD ENTRY  -->
       
                       
       
      
        <h3>Add Entry:</h3>
        <form method="post">
            Add Entry:<br><br>
            Full Name:   <input type="text" name="addcn"><br>
            Surname:     <input type="text" name="surname"><br>
            PhoneNumber: <input type="text" name="pn"><br>
        <input type="submit" value="addEntry">
        </form><br><br>
       
        <%            
       
         String addcn = (String)request.getParameter("addcn");
         String surname = (String)request.getParameter("surname");
         String pn = (String)request.getParameter("pn");
        
         try{
            //Create new set of attributes
            BasicAttributes attrs1 = new BasicAttributes();
            //(The item is a person)
            Attribute classes = new BasicAttribute("objectClass");
//            classes.add("top");
            classes.add("person");
//            classes.add("organizationalPerson");
            // Add the objectClass attribute to the attribute set
               attrs1.put(classes);
            // Store the other attributes in the attribute set
               attrs1.put("sn", surname);
               attrs1.put("telephonenumber", pn);
            // Add the new entry to the directory server
               ctx.createSubcontext("ldap://localhost:1389/cn="+addcn+",o=Rubrica,dc=example,dc=com", attrs1);    
            }
            catch (Exception e){
                out.println("An exception occurred: " + e.getMessage());
         }           
         %>
       
        
        
         <br>------------------------------------</br>
                    <!--       REMOVE ENTRY    -->
        
        
        <h3>Remove Entry:</h3>
        <form method="post">
            Insert Entry To Remove: <input type="text" name="delUser""><br>
        <input type="submit" value="Remove">
        </form><br><br>
        
       
       
        <%
        String delUser = (String)request.getParameter("delUser");
        try
        {
            ctx.destroySubcontext("cn="+delUser);
            }
            catch (Exception e){
                out.println("An exception occurred: " + e.getMessage());
            }
        %>
        
        
        <br><br><br>
    </body>
</html>

mercoledì 3 giugno 2015

OpenDJ Multi-Master Replication

dpkg -i opendj....
./setup



1- Enable:

./bin/dsreplication enable --host1 Host1 --port1 4444 --bindDN1 "cn=Directory Manager" --bindPassword1 pass --replicationPort1 8989 --host2 Host2 --port2 4444 --bindDN2 "cn=Directory Manager" --bindPassword2 pass --replicationPort2 8989 --adminUID admin --adminPassword pass --baseDN "dc=example,dc=com"

oppure: ./bin/dsreplication -> enable



2- Initialize:

./bin/dsreplication initialize --baseDN "dc=example,dc=com" --adminUID admin --adminPassword pass --hostSource Host1 --portSource 4444 --hostDestination Host2 --portDestination 4444 -X -n

oppure: ./bin/dsreplication-> initialize all


-----
..if everything's fine:

create new entry "o=Rubrica Telefonica",
create MichaelMyers.ldif:

dn: cn=Mychael Myers, o=Rubrica Telefonica, dc=example, dc=com
cn: Mychael Myers
sn: Myers
ObjectClass: person

and add the second one:

ldapadd -h workstation -p 1389 -D "cn=Directory Manager" -w secret -f MichaelMyers.ldif


And search for it.

ldapsearch -p 1389 -h workstation -b "dc=example,dc=com" -w secret -D "cn=Directory Manager" -LLL "(sn=Myers)"

Run minikube with podman on Fedora

After install minikube as described in the documentation , set rootless property to true to use Podman without sudo: minikube config set roo...