Guest User

Untitled

a guest
Sep 5th, 2018
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.80 KB | None | 0 0
  1. How to programmatically add user account to openDS?
  2. import java.util.Hashtable;
  3. import javax.naming.Context;
  4. import javax.naming.directory.BasicAttribute;
  5. import javax.naming.directory.BasicAttributes;
  6. import javax.naming.directory.InitialDirContext;
  7. import javax.naming.directory.DirContext;
  8. import javax.naming.directory.Attributes;
  9. import javax.naming.directory.Attribute;
  10. import javax.naming.NamingException;
  11.  
  12. public class App {
  13.  
  14. /* Ugly HardCoded stuff */
  15. public static String ldapUri = "ldap://localhost:2389";
  16. public static String admindn = "cn=Directory Manager";
  17. public static String admincred = "password";
  18. public static String usersContainer = "ou=users,dc=example,dc=com";
  19.  
  20. public static void main(String args[]){
  21.  
  22. if (args.length != 2) {
  23. System.out.println("Usage: App userName password");
  24. return;
  25. }
  26. String username = args[0];
  27. String password = args[1];
  28.  
  29. Hashtable env = new Hashtable();
  30. env.put(Context.INITIAL_CONTEXT_FACTORY,
  31. "com.sun.jndi.ldap.LdapCtxFactory");
  32. env.put(Context.PROVIDER_URL, ldapUri);
  33. env.put( Context.SECURITY_PRINCIPAL, admindn );
  34. env.put( Context.SECURITY_CREDENTIALS, admincred );
  35. try {
  36. DirContext ctx = new InitialDirContext(env);
  37.  
  38. Attributes attrs = new BasicAttributes(true);
  39.  
  40. Attribute objclass = new BasicAttribute("objectclass");
  41. objclass.add("top");
  42. objclass.add("inetorgperson");
  43.  
  44. Attribute surname = new BasicAttribute("sn");
  45. surname.add(username);
  46.  
  47. Attribute pwd = new BasicAttribute("userpassword");
  48. pwd.add(password);
  49.  
  50. attrs.put(objclass);
  51. attrs.put(surname);
  52. attrs.put(pwd);
  53.  
  54. ctx.createSubcontext("cn="+username+","+usersContainer, attrs);
  55. ctx.close();
  56.  
  57.  
  58. } catch (NamingException e) {
  59. e.printStackTrace();
  60. }
  61.  
  62.  
  63. }
  64. }
  65.  
  66. <html>
  67. <head>
  68. <meta http-equiv="Content-Type" content="text/html;charset=utf-8" >
  69. </head>
  70. <body>
  71. <?php
  72. $ldapconfig['host'] = 'PC100';
  73. $ldapconfig['port'] = 1389;
  74. $ldapconfig['basedn'] = 'dc=company,dc=com';
  75.  
  76. $ds=ldap_connect($ldapconfig['host'], $ldapconfig['port']);
  77.  
  78. $password=1;
  79. $username="cn=Directory Manager";
  80.  
  81.  
  82. if ($bind=ldap_bind($ds, $username, $password)) {
  83. echo("Login correct");
  84.  
  85. ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3); // IMPORTANT
  86. $dn = "cn=roshanis,dc=example,dc=com";
  87.  
  88.  
  89. $ldaprecord['cn'] = "roshanis";
  90. $ldaprecord['givenName'] = "mkljl";
  91. $ldaprecord['sn'] = "roshan";
  92. $ldaprecord['objectclass'][0] = "inetOrgPerson";
  93. $ldaprecord['objectclass'][1] = "test";
  94. $ldaprecord['mail'] = "lkl@fh.com";
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101. // add data to directory
  102. $r = ldap_add($ds, $dn, $ldaprecord);
  103.  
  104. // $r= ldap_modify($ds, $dn, $ldaprecord);
  105.  
  106. } else {
  107.  
  108. echo("Unable to bind to server.</br>");
  109.  
  110.  
  111. }
  112. ?>
  113.  
  114. </body>
  115. </html>
Add Comment
Please, Sign In to add comment