Skip to content

Commit

Permalink
CONNECTORS-1739: Reuse escaping facilities.
Browse files Browse the repository at this point in the history
  • Loading branch information
schuch committed Nov 28, 2022
1 parent f03354d commit 7df176b
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 144 deletions.
3 changes: 3 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ $Id$

======================= 2.24-dev =====================

CONNECTORS-1739: Reuse escaping facilities.
(Markus Schuch)

CONNECTORS-1738: Add connection-specific timeouts to ES connector.
(Nguyen Huu)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
import javax.naming.ldap.*;
import javax.naming.directory.*;

import static org.apache.manifoldcf.connectorcommon.common.LdapEscaper.escapeDN;
import static org.apache.manifoldcf.connectorcommon.common.LdapEscaper.escapeFilter;


/** This is the Active Directory implementation of the IAuthorityConnector interface.
* Access tokens for this connector are simple SIDs, except for the "global deny" token, which
Expand Down Expand Up @@ -358,10 +361,10 @@ protected AuthorizationResponse getAuthorizationResponseUncached(String userName
int k = domainPart.indexOf(".",j);
if (k == -1)
{
domainsb.append("DC=").append(ldapEscape(domainPart.substring(j)));
domainsb.append("DC=").append(escapeDN(domainPart.substring(j)));
break;
}
domainsb.append("DC=").append(ldapEscape(domainPart.substring(j,k)));
domainsb.append("DC=").append(escapeDN(domainPart.substring(j,k)));
j = k+1;
}

Expand Down Expand Up @@ -748,7 +751,7 @@ protected String getDistinguishedName(LdapContext ctx, String userName, String s
throws ManifoldCFException
{
String returnedAtts[] = {"distinguishedName"};
String searchFilter = "(&(objectClass=user)(" + userACLsUsername + "=" + userName + "))";
String searchFilter = "(&(objectClass=user)(" + userACLsUsername + "=" + escapeFilter(userName) + "))";
SearchControls searchCtls = new SearchControls();
searchCtls.setReturningAttributes(returnedAtts);
//Specify the search scope
Expand Down Expand Up @@ -776,28 +779,6 @@ protected String getDistinguishedName(LdapContext ctx, String userName, String s
throw new ManifoldCFException(e.getMessage(),e);
}
}

/** LDAP escape a string.
*/
protected static String ldapEscape(String input)
{
//Add escape sequence to all commas
StringBuilder sb = new StringBuilder();
int index = 0;
while (true)
{
int oldIndex = index;
index = input.indexOf(",",oldIndex);
if (index == -1)
{
sb.append(input.substring(oldIndex));
break;
}
sb.append(input.substring(oldIndex,index)).append("\\,");
index++;
}
return sb.toString();
}

/** Convert a binary SID to a string */
protected static String sid2String(byte[] SID)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,18 @@

import java.io.*;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.*;
import javax.naming.directory.*;
import javax.naming.ldap.*;
import org.apache.manifoldcf.authorities.interfaces.*;
import org.apache.manifoldcf.authorities.system.ManifoldCF;
import org.apache.manifoldcf.authorities.system.Logging;
import org.apache.manifoldcf.core.interfaces.*;
import org.apache.manifoldcf.connectorcommon.interfaces.*;
import org.apache.manifoldcf.ui.util.Encoder;
import org.apache.manifoldcf.core.common.LDAPSSLSocketFactory;

import static org.apache.manifoldcf.connectorcommon.common.LdapEscaper.escapeDN;
import static org.apache.manifoldcf.connectorcommon.common.LdapEscaper.escapeFilter;

/**
* This is the Active Directory implementation of the IAuthorityConnector
* interface. Access tokens for this connector are simple SIDs, except for the
Expand Down Expand Up @@ -473,7 +472,7 @@ protected AuthorizationResponse getAuthorizationResponseUncached(String userName

if (groupSearch != null && !groupSearch.isEmpty()) {
//specify the LDAP search filter
String searchFilter = groupSearch.replace("{0}", escapeLDAPSearchFilter(groupMemberDN ? usrRecord.getNameInNamespace() : usrName));
String searchFilter = groupSearch.replace("{0}", escapeFilter(groupMemberDN ? usrRecord.getNameInNamespace() : usrName));
SearchControls searchCtls = new SearchControls();
searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);
String returnedAtts[] = {groupNameAttr};
Expand Down Expand Up @@ -874,92 +873,6 @@ protected SearchResult getUserEntry(LdapContext ctx, String userName)
}
}

/**
* LDAP escape a string.
*/
protected static String ldapEscape(String input) {
//Add escape sequence to all commas
StringBuilder sb = new StringBuilder();
int index = 0;
while (true) {
int oldIndex = index;
index = input.indexOf(",", oldIndex);
if (index == -1) {
sb.append(input.substring(oldIndex));
break;
}
sb.append(input.substring(oldIndex, index)).append("\\,");
index++;
}
return sb.toString();
}

public static String escapeDN(String name) {
StringBuilder sb = new StringBuilder(); // If using JDK >= 1.5 consider using StringBuilder
if ((name.length() > 0) && ((name.charAt(0) == ' ') || (name.charAt(0) == '#'))) {
sb.append('\\'); // add the leading backslash if needed
}
for (int i = 0; i < name.length(); i++) {
char curChar = name.charAt(i);
switch (curChar) {
case '\\':
sb.append("\\\\");
break;
case ',':
sb.append("\\,");
break;
case '+':
sb.append("\\+");
break;
case '"':
sb.append("\\\"");
break;
case '<':
sb.append("\\<");
break;
case '>':
sb.append("\\>");
break;
case ';':
sb.append("\\;");
break;
default:
sb.append(curChar);
}
}
if ((name.length() > 1) && (name.charAt(name.length() - 1) == ' ')) {
sb.insert(sb.length() - 1, '\\'); // add the trailing backslash if needed
}
return sb.toString();
}

public static String escapeLDAPSearchFilter(String filter) {
StringBuilder sb = new StringBuilder(); // If using JDK >= 1.5 consider using StringBuilder
for (int i = 0; i < filter.length(); i++) {
char curChar = filter.charAt(i);
switch (curChar) {
case '\\':
sb.append("\\5c");
break;
case '*':
sb.append("\\2a");
break;
case '(':
sb.append("\\28");
break;
case ')':
sb.append("\\29");
break;
case '\u0000':
sb.append("\\00");
break;
default:
sb.append(curChar);
}
}
return sb.toString();
}

protected static StringSet emptyStringSet = new StringSet();

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,20 @@
package org.apache.manifoldcf.authorities.authorities.sharepoint;

import org.apache.manifoldcf.core.interfaces.*;
import org.apache.manifoldcf.agents.interfaces.*;
import org.apache.manifoldcf.authorities.interfaces.*;
import org.apache.manifoldcf.authorities.system.Logging;
import org.apache.manifoldcf.authorities.system.ManifoldCF;
import org.apache.manifoldcf.core.util.URLEncoder;

import java.io.*;
import java.util.*;
import java.net.*;
import java.util.concurrent.TimeUnit;
import javax.naming.*;
import javax.naming.ldap.*;
import javax.naming.directory.*;

import static org.apache.manifoldcf.connectorcommon.common.LdapEscaper.escapeDN;
import static org.apache.manifoldcf.connectorcommon.common.LdapEscaper.escapeFilter;


/** This is the Active Directory implementation of the IAuthorityConnector interface, as used
* by SharePoint in Claim Space. It is meant to be used in conjunction with other SharePoint authorities,
Expand Down Expand Up @@ -381,10 +381,10 @@ protected List<String> getADTokens(String userPart, String domainPart, String us
int k = domainPart.indexOf(".",j);
if (k == -1)
{
domainsb.append("DC=").append(ldapEscape(domainPart.substring(j)));
domainsb.append("DC=").append(escapeDN(domainPart.substring(j)));
break;
}
domainsb.append("DC=").append(ldapEscape(domainPart.substring(j,k)));
domainsb.append("DC=").append(escapeDN(domainPart.substring(j,k)));
j = k+1;
}

Expand Down Expand Up @@ -733,7 +733,7 @@ protected String getDistinguishedName(LdapContext ctx, String userName, String s
throws ManifoldCFException
{
String returnedAtts[] = {"distinguishedName"};
String searchFilter = "(&(objectClass=user)(" + userACLsUsername + "=" + userName + "))";
String searchFilter = "(&(objectClass=user)(" + userACLsUsername + "=" + escapeFilter(userName) + "))";
SearchControls searchCtls = new SearchControls();
searchCtls.setReturningAttributes(returnedAtts);
//Specify the search scope
Expand All @@ -760,28 +760,6 @@ protected String getDistinguishedName(LdapContext ctx, String userName, String s
throw new ManifoldCFException(e.getMessage(),e);
}
}

/** LDAP escape a string.
*/
protected static String ldapEscape(String input)
{
//Add escape sequence to all commas
StringBuilder sb = new StringBuilder();
int index = 0;
while (true)
{
int oldIndex = index;
index = input.indexOf(",",oldIndex);
if (index == -1)
{
sb.append(input.substring(oldIndex));
break;
}
sb.append(input.substring(oldIndex,index)).append("\\,");
index++;
}
return sb.toString();
}

/** Convert a binary SID to a string */
protected static String sid2String(byte[] SID)
Expand Down

0 comments on commit 7df176b

Please sign in to comment.