2
0

LDAP.java 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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.Attribute;
  16. import javax.naming.directory.Attributes;
  17. import javax.naming.directory.BasicAttribute;
  18. import javax.naming.directory.InitialDirContext;
  19. import javax.naming.directory.ModificationItem;
  20. import javax.naming.directory.SearchControls;
  21. import javax.naming.directory.SearchResult;
  22. /**
  23. *
  24. * @author Deben Oldert
  25. */
  26. public class LDAP {
  27. private static DirContext ctx;
  28. private static String userName;
  29. private static String passWord;
  30. private static String base = "dc=vpn,dc=local";
  31. LDAP(String username, String password) throws NamingException {
  32. Hashtable<String, String> env = new Hashtable();
  33. env.put(LdapContext.CONTROL_FACTORIES, "com.sun.jndi.ldap.ControlFactory");
  34. env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
  35. env.put(Context.PROVIDER_URL, "ldap://192.168.2.240:389");
  36. env.put(Context.SECURITY_AUTHENTICATION, "simple");
  37. env.put(Context.SECURITY_PRINCIPAL, "VPN\\Administrator");
  38. env.put(Context.SECURITY_CREDENTIALS, "XXX");
  39. try {
  40. ctx = new InitialDirContext(env);
  41. }
  42. catch(NamingException e) {
  43. int errCode= getErrorCode(e.getMessage());
  44. System.out.println("*** Conection with LDAP failed ("+username+", "+password+") Error: "+errCode+" ***");
  45. }
  46. userName = username;
  47. passWord = password;
  48. }
  49. public int userCheck() throws NamingException {
  50. try {
  51. String[] key = {"cn"};
  52. SearchControls cons = new SearchControls();
  53. cons.setSearchScope(SearchControls.SUBTREE_SCOPE);
  54. cons.setReturningAttributes(key);
  55. NamingEnumeration<SearchResult> answer = ctx.search(base, "cn=" + userName, cons);
  56. if(answer.hasMore()) {
  57. return 0;
  58. }
  59. else {
  60. return 730;
  61. }
  62. }
  63. catch(NamingException e) {
  64. return getErrorCode(e.toString()) + 700;
  65. }
  66. }
  67. public HashMap getUserInfo(String[] keys) throws NamingException {
  68. SearchControls cons = new SearchControls();
  69. cons.setSearchScope(SearchControls.SUBTREE_SCOPE);
  70. cons.setReturningAttributes(keys);
  71. HashMap<String, String> array = new HashMap();
  72. NamingEnumeration<SearchResult> answer = ctx.search(base, "cn=" + userName, cons);
  73. if(answer.hasMore()) {
  74. Attributes attrs = answer.next().getAttributes();
  75. String tmp;
  76. for(String key : keys) {
  77. if(attrs.get(key) != null) {
  78. tmp = attrs.get(key).toString();
  79. array.put(key, tmp.substring(tmp.indexOf(":") + 2));
  80. }
  81. else {
  82. array.put(key, null);
  83. }
  84. }
  85. return array;
  86. }
  87. return null;
  88. }
  89. public boolean writeInfo(String key, String value) {
  90. System.out.println("Modding: "+key+", "+value);
  91. ModificationItem[] mods = new ModificationItem[1];
  92. String name = "CN="+userName+",CN=Users,DC=vpn,DC=local";
  93. System.out.println(name);
  94. mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
  95. new BasicAttribute(key, value));
  96. try {
  97. ctx.modifyAttributes(name, mods);
  98. }
  99. catch(NamingException e){
  100. System.out.println(e);
  101. return false;
  102. }
  103. return true;
  104. }
  105. public boolean deleteInfo(String key) {
  106. ModificationItem[] mods = new ModificationItem[1];
  107. String name = "CN="+userName+",CN=Users,DC=vpn,DC=local";
  108. System.out.println("unregistering: "+key);
  109. mods[0] = new ModificationItem(DirContext.REMOVE_ATTRIBUTE,new BasicAttribute(key));
  110. try {
  111. ctx.modifyAttributes(name, mods);
  112. }
  113. catch(NamingException e){
  114. System.out.println(e);
  115. return false;
  116. }
  117. return true;
  118. }
  119. private int getErrorCode(final String exceptionMsg)
  120. {
  121. String pattern="-?\\d+";
  122. Pattern p=Pattern.compile(pattern);
  123. Matcher m=p.matcher(exceptionMsg);
  124. if (m.find()) {
  125. return Integer.valueOf(m.group(0));
  126. }
  127. return -1;
  128. }
  129. }