2
0

LDAP.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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, "Magnesium12");
  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. /*Hashtable tmpEnv = (Hashtable) ctx.getEnvironment().clone();
  51. tmpEnv.put(Context.SECURITY_PRINCIPAL, userName);
  52. tmpEnv.put(Context.SECURITY_CREDENTIALS, passWord);
  53. try {
  54. new InitialDirContext(tmpEnv);
  55. return 0;
  56. }
  57. catch(NamingException e) {
  58. return getErrorCode(e.toString()) + 700;
  59. }*/
  60. try {
  61. String[] key = {"cn"};
  62. SearchControls cons = new SearchControls();
  63. cons.setSearchScope(SearchControls.SUBTREE_SCOPE);
  64. cons.setReturningAttributes(key);
  65. NamingEnumeration<SearchResult> answer = ctx.search(base, "cn=" + userName, cons);
  66. if(answer.hasMore()) {
  67. return 0;
  68. }
  69. else {
  70. return 730;
  71. }
  72. }
  73. catch(NamingException e) {
  74. return getErrorCode(e.toString()) + 700;
  75. }
  76. }
  77. public HashMap getUserInfo(String[] keys) throws NamingException {
  78. SearchControls cons = new SearchControls();
  79. cons.setSearchScope(SearchControls.SUBTREE_SCOPE);
  80. cons.setReturningAttributes(keys);
  81. HashMap<String, String> array = new HashMap();
  82. NamingEnumeration<SearchResult> answer = ctx.search(base, "cn=" + userName, cons);
  83. if(answer.hasMore()) {
  84. Attributes attrs = answer.next().getAttributes();
  85. String tmp;
  86. for(String key : keys) {
  87. if(attrs.get(key) != null) {
  88. tmp = attrs.get(key).toString();
  89. array.put(key, tmp.substring(tmp.indexOf(":") + 2));
  90. }
  91. else {
  92. array.put(key, null);
  93. }
  94. }
  95. return array;
  96. }
  97. return null;
  98. }
  99. public boolean writeInfo(String key, String value) {
  100. System.out.println("Modding: "+key+", "+value);
  101. ModificationItem[] mods = new ModificationItem[1];
  102. String name = "CN="+userName+",CN=Users,DC=vpn,DC=local";
  103. System.out.println(name);
  104. mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
  105. new BasicAttribute(key, value));
  106. try {
  107. ctx.modifyAttributes(name, mods);
  108. }
  109. catch(NamingException e){
  110. System.out.println(e);
  111. return false;
  112. }
  113. return true;
  114. }
  115. public boolean deleteInfo(String key) {
  116. ModificationItem[] mods = new ModificationItem[1];
  117. String name = "CN="+userName+",CN=Users,DC=vpn,DC=local";
  118. System.out.println("unregistering: "+key);
  119. mods[0] = new ModificationItem(DirContext.REMOVE_ATTRIBUTE,new BasicAttribute(key));
  120. try {
  121. ctx.modifyAttributes(name, mods);
  122. }
  123. catch(NamingException e){
  124. System.out.println(e);
  125. return false;
  126. }
  127. return true;
  128. }
  129. private int getErrorCode(final String exceptionMsg)
  130. {
  131. String pattern="-?\\d+";
  132. Pattern p=Pattern.compile(pattern);
  133. Matcher m=p.matcher(exceptionMsg);
  134. if (m.find()) {
  135. return Integer.valueOf(m.group(0));
  136. }
  137. return -1;
  138. }
  139. }