From be34fa9e8dadf9cb7a6c77e53af76c3e1d199de5 Mon Sep 17 00:00:00 2001
From: Joern Muehlencord
+ * Important:
+ * To provide access to a trust center you can specify the following
+ * parameter to your application by providing the following parameter
+ *
If you want to use ldaps - usually port 636 make sure you
+ * provide a trustkeystore in case your ldap server does not use a
+ * certificate which can be trusted by the build in root certificates. (e.g.
+ * self signed certificates)
+ * -Djavax.net.ssl.trustStore=/path/to/truststore.keystore
+ *
ldap://ldapserver.your.domain:389
+ * @param searchBase the search base to use - e.g.
+ * DC=wincor-nixdorf,DC=com
+ * @param username the username to connect with
+ * @param password the password to connect with
+ */
+ public LDAPSearch(String url, String searchBase, String username, String password) {
+ String authentication = LDAPConnection.AUTHENTICATION_SIMPLE;
+ String securityProtocol = LDAPConnection.SECURITYPROTOCOL_SIMPLE;
+
+ this.ldapConnection = new LDAPConnection(authentication, url, securityProtocol, username, password);
+ this.searchBase = searchBase;
+ }
+
+ /**
+ * Creates a new instance of a ldap search.
+ *
+ *
+ * Important:
If you want to use ldaps - usually port 636 make sure you
+ * provide a trustkeystore in case your ldap server does not use a
+ * certificate which can be trusted by the build in root certificates. (e.g.
+ * self signed certificates)
+ * To provide access to a trust center you can specify the following + * parameter to your application by providing the following parameter + *
+ * -Djavax.net.ssl.trustStore=/path/to/truststore.keystore + *+ * + * @param authentication the authentification type to use -e.g. "SIMPLE" + * @param url the url of the ldap server to connect to like + *
ldap://ldapserver.your.domain:389
+ * @param securityProtoco the security protocol to use - e.g. SIMPLE
+ * @param searchBase the search base to use - e.g.
+ * DC=wincor-nixdorf,DC=com
+ * @param username the username to connect with
+ * @param password the password to connect with
+ */
+ public LDAPSearch(String authentication, String url, String securityProtocol, String searchBase, String username, String password) {
+ this.ldapConnection = new LDAPConnection(authentication, url, securityProtocol, username, password);
+ this.searchBase = searchBase;
+ }
+
+ /**
+ * execute several init steps, connect to ldap
+ */
+ public void init() throws LDAPException {
+ try {
+ ldapConnection.init();
+ } catch (NamingException ex) {
+ throw new LDAPException("Connection refused.", ex);
+ }
+ }
+
+ /**
+ * close the ldap connection
+ */
+ public void close() throws LDAPException {
+ if (ldapConnection != null) {
+ try {
+ ldapConnection.close();
+ ldapConnection = null;
+ } catch (NamingException ex) {
+ throw new LDAPException("Connection could not be closed.", ex);
+ }
+ }
+ }
+
+ /**
+ * Returns the search base of the ldap connection
+ *
+ * @return the search base of the ldap connection
+ */
+ public String getSearchBase() {
+ return searchBase;
+ }
+
+ /**
+ * Searches a contact according to emailaddress in the address directory
+ *
+ * @param email emailaddress to search for
+ * @return ldap contact or null if nothing could be found
+ * @throws LDAPException when search fails
+ */
+ public LDAPContact searchContactWithEmail(String email) throws LDAPException {
+ return searchContact("mail", email);
+ }
+
+ public LDAPContact searchContact(String searchField, String searchValue) throws LDAPException {
+
+ if (ldapConnection == null) {
+ throw new LDAPException("No connection established. Please execute init before.", null);
+ }
+
+ // prepare search parameters
+ String[] resultattributes = {"objectClass", "name", "givenName", "sn", "department", "co", "telephoneNumber", "sAMAccountName", "c",
+ "userAccountControl", "managedBy", "distinguishedName", "mail"};
+ String searchfilter = "(" + searchField + "=" + searchValue + ")";
+ SearchControls searchcontrols = new SearchControls();
+ String[] resultAttributes = resultattributes;
+ searchcontrols.setReturningAttributes(resultAttributes);
+ searchcontrols.setSearchScope(SearchControls.SUBTREE_SCOPE);
+
+ NamingEnumeration result;
+ try {
+ // search
+ result = ldapConnection.search(searchBase, searchfilter, searchcontrols);
+
+ // process result
+ Attributes attributes;
+ if (result.hasMoreElements()) {
+ SearchResult searchresult = (SearchResult) result.next();
+ attributes = searchresult.getAttributes();
+ } else {
+ // clearly nothing found
+ return null;
+ }
+
+ // create contact from search attributes
+ LDAPContact ldapContact = createLDAPContact(attributes);
+ return ldapContact;
+ } catch (NamingException ex) {
+ throw new LDAPException("Search failed for unkown reason.", ex);
+ }
+
+ }
+
+ /**
+ * Returns true, if the given email address can be found in the configured
+ * ldap
+ *
+ * @param email the emailaddress to search for
+ * @return true, if the email address could be found; else false
+ * @throws LDAPException if the search fails
+ */
+ public boolean emailExists(String email) throws LDAPException {
+ return searchContactWithEmail(email) != null;
+ }
+
+ /**
+ * Returns true, if the given email address is member of the given group,
+ * specified by the DN
+ *
+ * @param email the email to validat
+ * @param groupDn the group search base - all members must be found as
+ * "member" in this group
+ * @return
+ */
+ public boolean isMemberOfGroup(String email, String groupDn) throws LDAPException {
+ boolean returnValue = false;
+
+ LDAPContact contact = searchContactWithEmail(email);
+ if (contact == null) {
+ return false;
+ }
+
+ // prepare search parameters
+ String[] resultattributes = {"member"};
+ String searchfilter = "(objectClass=*)";
+ SearchControls searchcontrols = new SearchControls();
+ String[] resultAttributes = resultattributes;
+ searchcontrols.setReturningAttributes(resultAttributes);
+ searchcontrols.setSearchScope(SearchControls.SUBTREE_SCOPE);
+
+ NamingEnumeration result;
+ try {
+ result = ldapConnection.search(groupDn, searchfilter, searchcontrols);
+ while (result.hasMoreElements()) {
+ SearchResult searchresult = (SearchResult) result.next();
+ Attributes attributes = searchresult.getAttributes();
+ if ((attributes.get("member")) != null) {
+ String memberList = attributes.get("member").toString();
+ if (memberList == null) {
+ return false;
+ }
+ returnValue = memberList.contains(contact.getDistinguishedName());
+ } else {
+ // if
+ return false;
+ }
+ }
+ } catch (NamingException ex) {
+ throw new LDAPException(ex.getMessage(), ex);
+ }
+
+ return returnValue;
+ }
+
+ private LDAPContact createLDAPContact(Attributes attributes) throws LDAPException {
+
+ LDAPContact ldapContact = new LDAPContact();
+
+ if (attributes.get("mail") != null) {
+ ldapContact.setEmailaddress(attributes.get("mail").toString());
+ } else {
+ ldapContact.setEmailaddress("");
+ }
+
+ if (attributes.get("objectClass") != null) {
+ String objectClass = attributes.get("objectClass").toString();
+ if (objectClass.contains(":")) {
+ objectClass = objectClass.substring(objectClass.indexOf(":") + 2);
+ }
+ if (objectClass.startsWith(LDAPContact.TYPE_PERSON)) {
+ ldapContact.setType(LDAPContact.TYPE_PERSON);
+ } else if (objectClass.startsWith(LDAPContact.TYPE_PUBLICFOLDER)) {
+ ldapContact.setType(LDAPContact.TYPE_PUBLICFOLDER);
+ } else if (objectClass.startsWith(LDAPContact.TYPE_GROUP)) {
+ ldapContact.setType(LDAPContact.TYPE_GROUP);
+ } else {
+ throw new LDAPException("Invalid objectClass " + objectClass + " found. ", null);
+ }
+ }
+ switch (ldapContact.getType()) {
+ case LDAPContact.TYPE_PERSON:
+ // handle persons
+ // get mandatory fields from ad entry
+ if (attributes.get("distinguishedName") != null) {
+ String distinguishedName = attributes.get("distinguishedName").toString();
+ distinguishedName = distinguishedName.substring(distinguishedName.indexOf(":") + 2);
+ ldapContact.setDistinguishedName(distinguishedName);
+ }
+ if (attributes.get("givenName") != null) {
+ String firstname = attributes.get("givenName").toString();
+ firstname = firstname.substring(firstname.indexOf(":") + 2);
+ ldapContact.setFirstname(firstname);
+ }
+ if (attributes.get("sn") != null) {
+ String lastname = attributes.get("sn").toString();
+ lastname = lastname.substring(lastname.indexOf(":") + 2);
+ ldapContact.setLastname(lastname);
+ }
+ if (attributes.get("c") != null) {
+ String countryCode = attributes.get("c").toString();
+ countryCode = countryCode.substring(countryCode.indexOf(":") + 2);
+ ldapContact.setCountryCode(countryCode);
+
+ }
+ if (attributes.get("co") != null) {
+ String country = attributes.get("co").toString();
+ country = country.substring(country.indexOf(":") + 2);
+ ldapContact.setCountry(country);
+ }
+ if (attributes.get("department") != null) {
+ String department = attributes.get("department").toString();
+ department = department.substring(department.indexOf(":") + 2);
+ ldapContact.setDepartment(department);
+ }
+ if (attributes.get("telephoneNumber") != null) {
+ String phone = attributes.get("telephoneNumber").toString();
+ phone = phone.substring(phone.indexOf(":") + 2);
+ ldapContact.setPhone(phone);
+ }
+ if (attributes.get("sAMAccountName") != null) {
+ String crmname = attributes.get("sAMAccountName").toString().toLowerCase();
+ crmname = crmname.substring(crmname.indexOf(":") + 2);
+ ldapContact.setCrmname(crmname);
+ }
+ if (attributes.get("userAccountControl") != null) {
+ String userAccountControl = attributes.get("userAccountControl").toString();
+ userAccountControl = userAccountControl.substring(userAccountControl.indexOf(":") + 2);
+ if (userAccountControl.equals("512")) {
+ ldapContact.setEnabled(true);
+ } else {
+ ldapContact.setEnabled(false);
+ }
+ } else {
+ ldapContact.setEnabled(false);
+ }
+ break;
+ case LDAPContact.TYPE_GROUP:
+ case LDAPContact.TYPE_PUBLICFOLDER:
+ // handle groups
+ ldapContact.setEnabled(true);
+ ldapContact.setFirstname("Group");
+ if (attributes.get("name") != null) {
+ String name = attributes.get("name").toString().toLowerCase();
+ name = name.substring(name.indexOf(":") + 2);
+ ldapContact.setLastname(name);
+ } else {
+ String lastName = ldapContact.getEmailaddress();
+ lastName = lastName.substring(0, lastName.indexOf("@") - 1);
+ ldapContact.setLastname(lastName);
+ }
+ if (attributes.get("managedBy") != null) {
+ try {
+ String managedBy = attributes.get("managedBy").toString().toLowerCase();
+ managedBy = managedBy.substring(managedBy.indexOf(":") + 2);
+ managedBy = managedBy.substring(managedBy.lastIndexOf("ou="));
+ managedBy = managedBy.substring(3, managedBy.indexOf(","));
+ ldapContact.setCountry(managedBy);
+ } catch (Exception ex) {
+ ldapContact.setCountry(null);
+ }
+ }
+ break;
+
+ default:
+ throw new LDAPException("Unknown / unsupported ldap type " + ldapContact.getType());
+
+ }
+
+ return ldapContact;
+ }
+}
+
+/**
+ * History:
+ *
+ * $$Log: src/main/java/com/wincornixdorf/shared/network/ldap/LDAPSearch.java $
+ * Revision 1.1 2013/12/16 16:42:52MEZ Muehlencord, Joern (joern.muehlencord)
+ * Initial revision Member added to project
+ * m:/MKS/ESP_Tools/shared/shared-network/shared-network.pj $Revision 1.6
+ * 2013/09/05 07:14:33 jomu $fixed ldap search if group setup is not complete.
+ * (1147451) $ $Revision 1.5 2013/09/04 15:07:26 jomu $fixed ldap search if
+ * group setup is not complete. (1147451) $$
+ *
+ */