Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.50 KB | None | 0 0
  1. package com.test;
  2.  
  3. import java.io.IOException;
  4. import java.util.Properties;
  5.  
  6. import javax.naming.CompositeName;
  7. import javax.naming.Context;
  8. import javax.naming.NamingEnumeration;
  9. import javax.naming.NamingException;
  10. import javax.naming.directory.DirContext;
  11. import javax.naming.directory.SearchControls;
  12. import javax.naming.directory.SearchResult;
  13. import javax.naming.ldap.Control;
  14. import javax.naming.ldap.InitialLdapContext;
  15. import javax.naming.ldap.LdapContext;
  16. import javax.naming.ldap.PagedResultsControl;
  17.  
  18. public class Main {
  19. Properties ENV = new Properties();
  20. private DirContext CTX;
  21. private static final String BIN_ENV = "java.naming.ldap.attributes.binary";
  22. private static final String CTX_CLASS = "com.sun.jndi.ldap.LdapCtxFactory";
  23.  
  24. String host = "127.0.0.1";
  25. String port = "389";
  26.  
  27. String OU = "OU=b#,DC=domain,DC=local";
  28.  
  29. public int getConnection() {
  30. int result = 0;
  31. ENV.clear();
  32. String username = "Administrator@domain.local";
  33. String password = "admin-123";
  34.  
  35. if ((username != null) && (password != null)) {
  36. ENV.put(Context.SECURITY_AUTHENTICATION, "simple");
  37. ENV.put(Context.SECURITY_PRINCIPAL, username);
  38. ENV.put(Context.SECURITY_CREDENTIALS, password);
  39. }
  40.  
  41. ENV.put(Context.INITIAL_CONTEXT_FACTORY, CTX_CLASS);
  42.  
  43. ENV.put(Context.PROVIDER_URL, "ldap://" + host + ":" + port);
  44.  
  45. // some attributes must be retrieved in binary format
  46. ENV.put(BIN_ENV, "userCertificate");
  47. ENV.put(BIN_ENV, "objectGUID");
  48. try {
  49. CTX = new InitialLdapContext(ENV, null);
  50. } catch (NamingException ex) {
  51. ENV.clear();
  52. result = -4;
  53. if (ex.toString().indexOf("AuthenticationException") > 0) {
  54. result = -1;
  55. } else if (ex.toString().indexOf("ConnectException") > 0) {
  56. result = -2;
  57. } else if (ex.toString().indexOf("UnknownHostException") > 0) {
  58. result = -3;
  59. }
  60. }
  61. return result;
  62. }
  63.  
  64. public static void main(String args[]) {
  65. Main a = new Main();
  66.  
  67. String filter = "(&(objectClass=user)(!(objectCategory=computer)))";
  68. String[] availAttrs = { "objectGUID", "name", "sAMAccountName",
  69. "distinguishedName", "userCertificate", "userPrincipalName" };
  70.  
  71. SearchControls cons = new SearchControls();
  72. cons.setSearchScope(SearchControls.ONELEVEL_SCOPE);
  73. cons.setReturningAttributes(availAttrs);
  74.  
  75. int result = a.getConnection();
  76.  
  77. if (result != 0)
  78. System.exit(-1);
  79. try {
  80. ((LdapContext) a.CTX)
  81. .setRequestControls(new Control[] { new PagedResultsControl(
  82. 1000, Control.CRITICAL) });
  83.  
  84. String jndi_dn = new CompositeName().add(a.OU).toString();
  85. NamingEnumeration<SearchResult> ne = (NamingEnumeration<SearchResult>) a.CTX
  86. .search(jndi_dn, filter, cons);
  87.  
  88. if ((ne != null) && ne.hasMoreElements()) {
  89.  
  90. SearchResult sr = (SearchResult) ne.next();
  91. String name = sr.getAttributes().get(availAttrs[1]).get(0)
  92. .toString();
  93.  
  94. System.out.println(name);
  95. }
  96.  
  97. } catch (NamingException e) {
  98. // TODO Auto-generated catch block
  99. e.printStackTrace();
  100. } catch (IOException e) {
  101. // TODO Auto-generated catch block
  102. e.printStackTrace();
  103. }
  104.  
  105. }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement