2
0

LDAP.java 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Feel free to copy/use it for your own project.
  3. * Keep in mind that it took me several days/weeks, beers and asperines to make this.
  4. * So be nice, and give me some credit, I won't bite and it won't hurt you.
  5. */
  6. import java.util.HashMap;
  7. import javax.naming.Context;
  8. import javax.naming.directory.DirContext;
  9. import javax.naming.ldap.LdapContext;
  10. import java.util.Hashtable;
  11. import java.util.regex.Matcher;
  12. import java.util.regex.Pattern;
  13. import javax.naming.NamingEnumeration;
  14. import javax.naming.NamingException;
  15. import javax.naming.directory.Attributes;
  16. import javax.naming.directory.BasicAttribute;
  17. import javax.naming.directory.InitialDirContext;
  18. import javax.naming.directory.ModificationItem;
  19. import javax.naming.directory.SearchControls;
  20. import javax.naming.directory.SearchResult;
  21. /**
  22. *
  23. * @author Deben Oldert
  24. */
  25. public class LDAP {
  26. private static DirContext ctx;
  27. private static String userName;
  28. private static String passWord;
  29. private static String base = "dc=vpn,dc=local";
  30. LDAP(String username, String password) throws NamingException {
  31. Hashtable<String, String> env = new Hashtable();
  32. env.put(LdapContext.CONTROL_FACTORIES, "com.sun.jndi.ldap.ControlFactory");
  33. env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
  34. env.put(Context.PROVIDER_URL, "ldap://192.168.2.240:389");
  35. env.put(Context.SECURITY_AUTHENTICATION, "simple");
  36. env.put(Context.SECURITY_PRINCIPAL, "VPN\\Administrator");
  37. env.put(Context.SECURITY_CREDENTIALS, "Magnesium12");
  38. try {
  39. ctx = new InitialDirContext(env);
  40. }
  41. catch(NamingException e) {
  42. int errCode= getErrorCode(e.getMessage());
  43. System.out.println("*** Conection with LDAP failed ("+username+", "+password+") Error: "+errCode+" ***");
  44. }
  45. userName = username;
  46. passWord = password;
  47. }
  48. public int userCheck() throws NamingException {
  49. /*Hashtable tmpEnv = (Hashtable) ctx.getEnvironment().clone();
  50. tmpEnv.put(Context.SECURITY_PRINCIPAL, userName);
  51. tmpEnv.put(Context.SECURITY_CREDENTIALS, passWord);
  52. try {
  53. new InitialDirContext(tmpEnv);
  54. return 0;
  55. }
  56. catch(NamingException e) {
  57. return getErrorCode(e.toString()) + 700;
  58. }*/
  59. try {
  60. String[] key = {"cn"};
  61. SearchControls cons = new SearchControls();
  62. cons.setSearchScope(SearchControls.SUBTREE_SCOPE);
  63. cons.setReturningAttributes(key);
  64. NamingEnumeration<SearchResult> answer = ctx.search(base, "cn=" + userName, cons);
  65. if(answer.hasMore()) {
  66. return 0;
  67. }
  68. else {
  69. return 730;
  70. }
  71. }
  72. catch(NamingException e) {
  73. return getErrorCode(e.toString()) + 700;
  74. }
  75. }
  76. public HashMap getUserInfo(String[] keys) throws NamingException {
  77. SearchControls cons = new SearchControls();
  78. cons.setSearchScope(SearchControls.SUBTREE_SCOPE);
  79. cons.setReturningAttributes(keys);
  80. HashMap<String, String> array = new HashMap();
  81. NamingEnumeration<SearchResult> answer = ctx.search(base, "cn=" + userName, cons);
  82. if(answer.hasMore()) {
  83. Attributes attrs = answer.next().getAttributes();
  84. String tmp;
  85. for(String key : keys) {
  86. if(attrs.get(key) != null) {
  87. tmp = attrs.get(key).toString();
  88. array.put(key, tmp.substring(tmp.indexOf(":") + 2));
  89. }
  90. else {
  91. array.put(key, null);
  92. }
  93. }
  94. return array;
  95. }
  96. return null;
  97. }
  98. public boolean writeInfo(String key, String value) {
  99. System.out.println("Modding: "+key+", "+value);
  100. ModificationItem[] mods = new ModificationItem[1];
  101. String name = "CN="+userName+",CN=Users,DC=vpn,DC=local";
  102. System.out.println(name);
  103. mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
  104. new BasicAttribute(key, value));
  105. try {
  106. ctx.modifyAttributes(name, mods);
  107. }
  108. catch(NamingException e){
  109. System.out.println(e);
  110. return false;
  111. }
  112. return true;
  113. }
  114. private int getErrorCode(final String exceptionMsg)
  115. {
  116. String pattern="-?\\d+";
  117. Pattern p=Pattern.compile(pattern);
  118. Matcher m=p.matcher(exceptionMsg);
  119. if (m.find()) {
  120. return Integer.valueOf(m.group(0));
  121. }
  122. return -1;
  123. }
  124. }