Commit 8c60bb63 authored by Alex Wenckus's avatar Alex Wenckus Committed by alex

Missing LDAP close connection in one location, improved close logic in another location. JM-596

git-svn-id: http://svn.igniterealtime.org/svn/repos/wildfire/trunk@3566 b35dd754-fafc-0310-a699-88a17e54d16e
parent 07830d33
...@@ -11,14 +11,14 @@ ...@@ -11,14 +11,14 @@
package org.jivesoftware.wildfire.ldap; package org.jivesoftware.wildfire.ldap;
import org.jivesoftware.util.JiveConstants;
import org.jivesoftware.util.Log;
import org.jivesoftware.wildfire.XMPPServer; import org.jivesoftware.wildfire.XMPPServer;
import org.jivesoftware.wildfire.group.Group; import org.jivesoftware.wildfire.group.Group;
import org.jivesoftware.wildfire.group.GroupProvider;
import org.jivesoftware.wildfire.group.GroupNotFoundException; import org.jivesoftware.wildfire.group.GroupNotFoundException;
import org.jivesoftware.wildfire.group.GroupProvider;
import org.jivesoftware.wildfire.user.UserManager; import org.jivesoftware.wildfire.user.UserManager;
import org.jivesoftware.wildfire.user.UserNotFoundException; import org.jivesoftware.wildfire.user.UserNotFoundException;
import org.jivesoftware.util.JiveConstants;
import org.jivesoftware.util.Log;
import org.xmpp.packet.JID; import org.xmpp.packet.JID;
import javax.naming.NamingEnumeration; import javax.naming.NamingEnumeration;
...@@ -82,7 +82,8 @@ public class LdapGroupProvider implements GroupProvider { ...@@ -82,7 +82,8 @@ public class LdapGroupProvider implements GroupProvider {
String filter = MessageFormat.format(manager.getGroupSearchFilter(), "*"); String filter = MessageFormat.format(manager.getGroupSearchFilter(), "*");
String searchFilter = "(&" + filter + "(" + String searchFilter = "(&" + filter + "(" +
manager.getGroupNameField() + "=" + group + "))"; manager.getGroupNameField() + "=" + group + "))";
Collection<Group> groups = populateGroups(searchForGroups(searchFilter, standardAttributes)); Collection<Group> groups =
populateGroups(searchForGroups(searchFilter, standardAttributes));
if (groups.size() > 1) { if (groups.size() > 1) {
// If multiple groups found, throw exception. // If multiple groups found, throw exception.
throw new GroupNotFoundException("Too many groups with name " + group + " were found."); throw new GroupNotFoundException("Too many groups with name " + group + " were found.");
...@@ -111,11 +112,12 @@ public class LdapGroupProvider implements GroupProvider { ...@@ -111,11 +112,12 @@ public class LdapGroupProvider implements GroupProvider {
* Always throws an UnsupportedOperationException because * Always throws an UnsupportedOperationException because
* LDAP groups are read-only. * LDAP groups are read-only.
* *
* @param name the group name. * @param name the group name.
* @param description the group description. * @param description the group description.
* @throws UnsupportedOperationException when called. * @throws UnsupportedOperationException when called.
*/ */
public void setDescription(String name, String description) throws UnsupportedOperationException { public void setDescription(String name, String description)
throws UnsupportedOperationException {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
...@@ -195,14 +197,13 @@ public class LdapGroupProvider implements GroupProvider { ...@@ -195,14 +197,13 @@ public class LdapGroupProvider implements GroupProvider {
* Always throws an UnsupportedOperationException because LDAP groups * Always throws an UnsupportedOperationException because LDAP groups
* are read-only. * are read-only.
* *
* @param groupName name of a group. * @param groupName name of a group.
* @param user the JID of the user to add * @param user the JID of the user to add
* @param administrator true if is an administrator. * @param administrator true if is an administrator.
* @throws UnsupportedOperationException when called. * @throws UnsupportedOperationException when called.
*/ */
public void addMember(String groupName, JID user, boolean administrator) public void addMember(String groupName, JID user, boolean administrator)
throws UnsupportedOperationException throws UnsupportedOperationException {
{
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
...@@ -210,14 +211,13 @@ public class LdapGroupProvider implements GroupProvider { ...@@ -210,14 +211,13 @@ public class LdapGroupProvider implements GroupProvider {
* Always throws an UnsupportedOperationException because LDAP groups * Always throws an UnsupportedOperationException because LDAP groups
* are read-only. * are read-only.
* *
* @param groupName the naame of a group. * @param groupName the naame of a group.
* @param user the JID of the user with new privileges * @param user the JID of the user with new privileges
* @param administrator true if is an administrator. * @param administrator true if is an administrator.
* @throws UnsupportedOperationException when called. * @throws UnsupportedOperationException when called.
*/ */
public void updateMember(String groupName, JID user, boolean administrator) public void updateMember(String groupName, JID user, boolean administrator)
throws UnsupportedOperationException throws UnsupportedOperationException {
{
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
...@@ -226,7 +226,7 @@ public class LdapGroupProvider implements GroupProvider { ...@@ -226,7 +226,7 @@ public class LdapGroupProvider implements GroupProvider {
* are read-only. * are read-only.
* *
* @param groupName the name of a group. * @param groupName the name of a group.
* @param user the JID of the user to delete. * @param user the JID of the user to delete.
* @throws UnsupportedOperationException when called. * @throws UnsupportedOperationException when called.
*/ */
public void deleteMember(String groupName, JID user) throws UnsupportedOperationException { public void deleteMember(String groupName, JID user) throws UnsupportedOperationException {
...@@ -254,8 +254,8 @@ public class LdapGroupProvider implements GroupProvider { ...@@ -254,8 +254,8 @@ public class LdapGroupProvider implements GroupProvider {
if (manager.isDebugEnabled()) { if (manager.isDebugEnabled()) {
Log.debug("Trying to find all groups in the system."); Log.debug("Trying to find all groups in the system.");
} }
DirContext ctx; DirContext ctx = null;
NamingEnumeration<SearchResult> answer = null; NamingEnumeration<SearchResult> answer;
try { try {
ctx = manager.getContext(); ctx = manager.getContext();
if (manager.isDebugEnabled()) { if (manager.isDebugEnabled()) {
...@@ -272,13 +272,23 @@ public class LdapGroupProvider implements GroupProvider { ...@@ -272,13 +272,23 @@ public class LdapGroupProvider implements GroupProvider {
if (manager.isDebugEnabled()) { if (manager.isDebugEnabled()) {
Log.debug("... search finished"); Log.debug("... search finished");
} }
return answer;
} }
catch (Exception e) { catch (Exception e) {
if (manager.isDebugEnabled()) { if (manager.isDebugEnabled()) {
Log.debug("Error while searching for groups.", e); Log.debug("Error while searching for groups.", e);
} }
return null;
}
finally {
if (ctx != null) {
try {
ctx.close();
}
catch (Exception ex) { /* do nothing */ }
}
} }
return answer;
} }
/** /**
...@@ -292,115 +302,124 @@ public class LdapGroupProvider implements GroupProvider { ...@@ -292,115 +302,124 @@ public class LdapGroupProvider implements GroupProvider {
if (manager.isDebugEnabled()) { if (manager.isDebugEnabled()) {
Log.debug("Starting to populate groups with users."); Log.debug("Starting to populate groups with users.");
} }
DirContext ctx = null;
TreeMap<String, Group> groups = new TreeMap<String, Group>();
DirContext ctx;
try { try {
ctx = manager.getContext(); TreeMap<String, Group> groups = new TreeMap<String, Group>();
}
catch (Exception e) { try {
return new ArrayList<Group>(); ctx = manager.getContext();
} }
catch (Exception e) {
return new ArrayList<Group>();
}
SearchControls ctrls = new SearchControls(); SearchControls ctrls = new SearchControls();
ctrls.setReturningAttributes(new String[]{manager.getUsernameField()}); ctrls.setReturningAttributes(new String[]{manager.getUsernameField()});
ctrls.setSearchScope(SearchControls.SUBTREE_SCOPE); ctrls.setSearchScope(SearchControls.SUBTREE_SCOPE);
String userSearchFilter = MessageFormat.format(manager.getSearchFilter(), "*"); String userSearchFilter = MessageFormat.format(manager.getSearchFilter(), "*");
XMPPServer server = XMPPServer.getInstance(); XMPPServer server = XMPPServer.getInstance();
String serverName = server.getServerInfo().getName(); String serverName = server.getServerInfo().getName();
while (answer.hasMoreElements()) { while (answer.hasMoreElements()) {
String name = ""; String name = "";
try {
Attributes a = answer.nextElement().getAttributes();
String description;
try { try {
name = ((String) ((a.get(manager.getGroupNameField())).get())); Attributes a = answer.nextElement().getAttributes();
description = ((String) ((a.get(manager.getGroupDescriptionField())).get())); String description;
} try {
catch (Exception e) { name = ((String) ((a.get(manager.getGroupNameField())).get()));
description = ""; description =
} ((String) ((a.get(manager.getGroupDescriptionField())).get()));
TreeSet<JID> members = new TreeSet<JID>(); }
Attribute member = a.get(manager.getGroupMemberField()); catch (Exception e) {
NamingEnumeration ne = member.getAll(); description = "";
while (ne.hasMore()) { }
String username = (String) ne.next(); TreeSet<JID> members = new TreeSet<JID>();
if (!manager.isPosixMode()) { //userName is full dn if not posix Attribute member = a.get(manager.getGroupMemberField());
NamingEnumeration ne = member.getAll();
while (ne.hasMore()) {
String username = (String) ne.next();
if (!manager.isPosixMode()) { //userName is full dn if not posix
try {
// Get the CN using LDAP
LdapName ldapname = new LdapName(username);
String ldapcn = ldapname.get(ldapname.size() - 1);
// We have to do a new search to find the username field
String combinedFilter =
"(&(" + ldapcn + ")" + userSearchFilter + ")";
NamingEnumeration usrAnswer = ctx.search("", combinedFilter, ctrls);
if (usrAnswer.hasMoreElements()) {
username = (String) ((SearchResult) usrAnswer.next())
.getAttributes().get(
manager.getUsernameField()).get();
}
else {
throw new UserNotFoundException();
}
}
catch (Exception e) {
if (manager.isDebugEnabled()) {
Log.debug("Error populating user with DN: " + username, e);
}
}
}
// A search filter may have been defined in the LdapUserProvider.
// Therefore, we have to try to load each user we found to see if
// it passes the filter.
try { try {
// Get the CN using LDAP JID userJID;
LdapName ldapname = new LdapName(username); // Create JID of local user if JID does not match a component's JID
String ldapcn = ldapname.get(ldapname.size() - 1); if (!username.contains(serverName)) {
// In order to lookup a username from the manager, the username
// We have to do a new search to find the username field // must be a properly escaped JID node.
String escapedUsername = JID.escapeNode(username);
String combinedFilter = "(&(" + ldapcn + ")" + userSearchFilter + ")"; userManager.getUser(escapedUsername);
NamingEnumeration usrAnswer = ctx.search("", combinedFilter, ctrls); // No exception, so the user must exist. Add the user as a group
if (usrAnswer.hasMoreElements()) { // member using the escaped username.
username = (String) ((SearchResult) usrAnswer.next()).getAttributes().get( userJID = server.createJID(escapedUsername, null);
manager.getUsernameField()).get();
} }
else { else {
throw new UserNotFoundException(); // This is a JID of a component or node of a server's component
userJID = new JID(username);
} }
members.add(userJID);
} }
catch (Exception e) { catch (UserNotFoundException e) {
if (manager.isDebugEnabled()) { if (manager.isDebugEnabled()) {
Log.debug("Error populating user with DN: " + username, e); Log.debug("User not found: " + username);
} }
} }
} }
// A search filter may have been defined in the LdapUserProvider. if (manager.isDebugEnabled()) {
// Therefore, we have to try to load each user we found to see if Log.debug("Adding group \"" + name + "\" with " + members.size() +
// it passes the filter. " members.");
try {
JID userJID;
// Create JID of local user if JID does not match a component's JID
if (!username.contains(serverName)) {
// In order to lookup a username from the manager, the username
// must be a properly escaped JID node.
String escapedUsername = JID.escapeNode(username);
userManager.getUser(escapedUsername);
// No exception, so the user must exist. Add the user as a group
// member using the escaped username.
userJID = server.createJID(escapedUsername, null);
}
else {
// This is a JID of a component or node of a server's component
userJID = new JID(username);
}
members.add(userJID);
} }
catch (UserNotFoundException e) { Group g = new Group(name, description, members, new ArrayList<JID>());
if (manager.isDebugEnabled()) { groups.put(name, g);
Log.debug("User not found: " + username); }
} catch (Exception e) {
if (manager.isDebugEnabled()) {
Log.debug("Error while populating group, " + name + ".", e);
} }
} }
if (manager.isDebugEnabled()) { }
Log.debug("Adding group \"" + name + "\" with " + members.size() + " members."); if (manager.isDebugEnabled()) {
Log.debug("Finished populating group(s) with users.");
}
return groups.values();
}
finally {
try {
if (ctx != null) {
ctx.close();
} }
Group g = new Group(name, description, members, new ArrayList<JID>());
groups.put(name, g);
} }
catch (Exception e) { catch (Exception e) {
if (manager.isDebugEnabled()) { // Ignore.
Log.debug("Error while populating group, " + name + ".", e);
}
} }
} }
if (manager.isDebugEnabled()) {
Log.debug("Finished populating group(s) with users.");
}
try {
ctx.close();
}
catch (Exception e) {
// Ignore.
}
return groups.values();
} }
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment