1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 1999
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
package com.netscape.jndi.ldap;
import java.util.StringTokenizer;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.BasicAttribute;
import javax.naming.directory.BasicAttributes;
import javax.naming.directory.InvalidSearchControlsException;
import javax.naming.directory.InvalidSearchFilterException;
import javax.naming.directory.SearchControls;
import netscape.ldap.LDAPConnection;
/**
* Common utility methods
*
*/
class ProviderUtils {
public static final String DEFAULT_FILTER = "(objectclass=*)";
static int jndiSearchScopeToLdap(int jndiScope) throws NamingException {
int scope = -1;
if (jndiScope == SearchControls.SUBTREE_SCOPE) {
scope = LDAPConnection.SCOPE_SUB;
}
else if (jndiScope == SearchControls.ONELEVEL_SCOPE) {
scope = LDAPConnection.SCOPE_ONE;
}
else if (jndiScope == SearchControls.OBJECT_SCOPE) {
scope = LDAPConnection.SCOPE_BASE;
}
else {
throw new InvalidSearchControlsException("Illegal value for the search scope");
}
return scope;
}
/**
* Convert Attribute List to a LDAP filter
*
* @return LDAP Filter
* @throw NamingException
* @param attrs An Attribute List
*/
static String attributesToFilter(Attributes attrs) throws NamingException{
if (attrs == null || attrs.size() == 0) {
return DEFAULT_FILTER;
}
String filter = "";
for (NamingEnumeration<? extends Attribute> attrEnum = attrs.getAll(); attrEnum.hasMore();) {
Attribute attrib = attrEnum.next();
//Has attributes any values
if ( attrib.size() == 0) {
// test only for presence of the attribute
filter += "(" + attrib.getID() + "=*)";
continue;
}
// Add attribute values to the filter, ecsaping if necessary
String attrValues = "";
for (NamingEnumeration<?> valEnum = attrib.getAll(); valEnum.hasMore();) {
Object val = valEnum.next();
if (val instanceof String) {
attrValues += "(" + attrib.getID() + "=" + escapeString((String)val) +")";
}
else if (val instanceof byte[]) {
attrValues += "(" + attrib.getID() + "=" + escapeBytes((byte[])val) +")";
}
else if (val == null) {
//null is allowed value in Attribute.add(Object), accept it just in case
attrValues += "(" + attrib.getID() + "=*)";
}
else {
throw new NamingException(
"Wrong Attribute value, expecting String or byte[]");
}
}
filter += (attrib.size() > 1) ? ("(|" + attrValues + ")") : attrValues;
}
return (attrs.size() > 1) ? ("(&" + filter + ")") : filter;
}
/**
* Expand filterExpr. Each occurrence of a variable "{n}", where n is a non-negative
* integer, is replaced with a variable from the filterArgs array indexed by the 'n'.
* FilterArgs can be Strings or byte[] and they are escaped according to the RFC2254
*/
static String expandFilterExpr(String filterExpr, Object[] filterArgs) throws InvalidSearchFilterException{
StringTokenizer tok = new StringTokenizer(filterExpr, "{}", /*returnTokens=*/true);
if (tok.countTokens() == 1) {
return filterExpr; // No escape characters
}
StringBuffer out= new StringBuffer();
boolean expectVarIdx = false, expectVarOff = false;
Object arg= null;
while (tok.hasMoreTokens()) {
String s = tok.nextToken();
if (expectVarIdx) {
expectVarIdx = false;
try {
int idx = Integer.parseInt(s);
arg = filterArgs[idx];
expectVarOff = true;
}
catch (IndexOutOfBoundsException e) {
throw new InvalidSearchFilterException("Filter expression variable index out of bounds");
}
catch (Exception e) {
throw new InvalidSearchFilterException("Invalid filter expression");
}
}
else if (expectVarOff) {
expectVarOff = false;
if (!s.equals("}")) {
throw new InvalidSearchFilterException("Invalid filter expression");
}
if (arg instanceof String) {
out.append(escapeString((String)arg));
}
else if (arg instanceof byte[]) {
out.append(escapeBytes((byte[])arg));
}
else {
throw new InvalidSearchFilterException("Invalid filter argument type");
}
arg = null;
}
else if (s.equals("{")) {
expectVarIdx = true;
}
else {
out.append(s);
}
}
if (expectVarIdx || expectVarOff) {
throw new InvalidSearchFilterException("Invalid filter expression");
}
return out.toString();
}
/**
* Escape a string according to the RFC 2254
*/
static String escapeString(String str) {
String charToEscape = "\\*()\000";
StringTokenizer tok = new StringTokenizer(str, charToEscape, /*returnTokens=*/true);
if (tok.countTokens() == 1) {
return str; // No escape characters
}
StringBuffer out= new StringBuffer();
while (tok.hasMoreTokens()) {
String s = tok.nextToken();
if (s.equals("*")) {
out.append("\\2a");
}
else if (s.equals("(")) {
out.append("\\28");
}
else if (s.equals(")")) {
out.append("\\29");
}
else if (s.equals("\\")) {
out.append("\\5c");
}
else if (s.equals("\000")) {
out.append("\\00");
}
else {
out.append(s);
}
}
return out.toString();
}
/**
* Escape a byte array according to the RFC 2254
*/
static final String hexDigits="0123456789abcdef";
static String escapeBytes(byte[] bytes) {
StringBuffer out = new StringBuffer("");
for (int i=0; i < bytes.length; i++) {
int low = bytes[i] & 0x0f;
int high = (bytes[i] & 0xf0) >> 4;
out.append("\\");
out.append(hexDigits.charAt(high));
out.append(hexDigits.charAt(low));
}
return out.toString();
}
/**
* A method used only for testing
*/
private static void testAttributesToFilter() {
try {
System.out.println(attributesToFilter(null));
BasicAttributes attrs = new BasicAttributes(true);
System.out.println(attrs + " = " + attributesToFilter(attrs));
attrs.put (new BasicAttribute("attr1", "val1"));
attrs.put (new BasicAttribute("attr2", "(val2)\\*x"));
attrs.put (new BasicAttribute("attr3"));
BasicAttribute attr4 = new BasicAttribute("attr4", "val41");
attr4.add("val42");
attrs.put(attr4);
attrs.put("attr5", new byte[] { (byte)0x23, (byte)0x3, (byte)0x0, (byte)0xab, (byte)0xff});
System.out.println(attrs + " = " +attributesToFilter(attrs));
}
catch (Exception e) {
System.err.println(e);
}
}
/**
* A method used only for testing
*/
private static void testFilterExpr() {
try {
String filterExpr = "(&(attr0={0})(attr1={1}))";
Object [] args = new Object[] { "val*0", new byte[] { (byte)0xf0, (byte) 0x3a}};
String filter = null;
filter = expandFilterExpr(filterExpr, args);
System.out.println(filterExpr + " -> " + filter);
}
catch (Exception e) {
System.err.println(e);
}
}
/**
* Test
*/
public static void main(String[] args) {
/*testAttributesToFilter();
String x = "012\0003";
byte[] b = x.getBytes();
for (int i=0; i < b.length; i++) {
System.out.println(i+"="+b[i]);
}*/
testFilterExpr();
}
}
|