Debian Codegraph @

java-sdk/ietfldap/org/ietf/ldap/util/RDN.java

raw symbols SCIP
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 *
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
10 *
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
15 *
16 * The Original Code is mozilla.org code.
17 *
18 * The Initial Developer of the Original Code is
19 * Netscape Communications Corporation.
20 * Portions created by the Initial Developer are Copyright (C) 1999
21 * the Initial Developer. All Rights Reserved.
22 *
23 * Contributor(s):
24 *
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
36 *
37 * ***** END LICENSE BLOCK ***** */
38 package org.ietf.ldap.util;
39
40 import java.util.Enumeration;
41 import java.util.Hashtable;
42 import java.util.Vector;
43
44 /**
45 * Objects of this class represent the components of a distinguished
46 * name (DN). (In some situations, these components are referred to
47 * as relative distinguished names, or RDNs.) For example, the
48 * DN "uid=bjensen, ou=People, o=Airius.com" has three components:
49 * "uid=bjensen", "ou=People", and "o=Airius.com".
50 * <P>
51 *
52 * Each DN component consists of an attribute type and a value.
53 * For example, in "o=Airius.com", the attribute type is "o"
54 * and the value is "Airius.com".
55 * <P>
56 *
57 * You can use objects of this class to add components to an
58 * existing <CODE>DN</CODE> object.
59 * <P>
60 *
61 * @version 1.0
62 * @see org.ietf.ldap.util.DN
63 */
64 public final class RDN implements java.io.Serializable {
65
66 static final long serialVersionUID = 7895454691174650321L;
67
68 /**
69 * List of RDNs. DN consists of one or more RDNs.
70 */
71 private String[] m_type = null;
72 private String[] m_value = null;
73 private boolean m_ismultivalued = false;
74
75 /**
76 * Hash table of case sensitive attributes
77 */
78 private static Hashtable<String, String> m_attributehash = new Hashtable<>();
79
80 /**
81 * Constructs a new <CODE>RDN</CODE> object from the specified
82 * DN component.
83 * @param rdn DN component
84 */
85 public RDN( String rdn ) {
86 String neutralRDN = neutralizeEscapes(rdn);
87 if (neutralRDN == null) {
88 return; // malformed RDN
89 }
90 int index = neutralRDN.indexOf( "=" );
91 int next_plus;
92
93 // if the rdn doesnt have = or = positions right at the beginning of the rdn
94 if (index <= 0)
95 return;
96
97 Vector<String> values = new Vector<>();
98 Vector<String> types = new Vector<>();
99
100 types.addElement( rdn.substring( 0, index ).trim() );
101 next_plus = neutralRDN.indexOf( '+', index );
102 while ( next_plus != -1 ) {
103 m_ismultivalued = true;
104 values.addElement( rdn.substring( index + 1, next_plus).trim() );
105 index = neutralRDN.indexOf( "=", next_plus + 1 );
106 if ( index == -1 ) {
107 // malformed RDN?
108 return;
109 }
110 types.addElement( rdn.substring( next_plus + 1, index ).trim() );
111 next_plus = neutralRDN.indexOf('+', index );
112 }
113 values.addElement( rdn.substring( index + 1 ).trim() );
114
115 m_type = new String[types.size()];
116 m_value = new String[values.size()];
117
118 for( int i = 0; i < types.size(); i++ ) {
119 m_type[i] = types.elementAt( i );
120 if (!isValidType(m_type[i])) {
121 m_type = m_value = null;
122 return; // malformed
123 }
124 m_value[i] = values.elementAt( i );
125 if (!isValidValue(m_value[i])) {
126 m_type = m_value = null;
127 return; // malformed
128 }
129 }
130 }
131
132 /**
133 * Neutralize backslash escapes and quoted sequences for easy parsing.
134 * @return rdn string with disabled escapes or null if malformed rdn
135 */
136 static String neutralizeEscapes(String rdn) {
137 if (rdn == null) {
138 return null;
139 }
140 StringBuffer sb = new StringBuffer(rdn);
141 boolean quoteOn = false;
142 // first pass, disable backslash escapes
143 for (int i=0; i < sb.length(); i++) {
144 if (sb.charAt(i) =='\\') {
145 sb.setCharAt(i, 'x');
146 if (i < sb.length()-1) {
147 sb.setCharAt(i+1, 'x');
148 }
149 else {
150 return null;
151 }
152 }
153 }
154 // second pass, disable quoted sequences
155 for (int i=0; i < sb.length(); i++) {
156 if (sb.charAt(i) == '"') {
157 quoteOn = !quoteOn;
158 continue;
159 }
160 if (quoteOn) {
161 sb.setCharAt(i, 'x');
162 }
163 }
164 return quoteOn ? null : sb.toString();
165 }
166
167 /**
168 * Type names can not contain any DN special characters
169 */
170 private boolean isValidType(String type) {
171 if (type == null || type.length() < 1) {
172 return false;
173 }
174 for (int i=0; i< type.length(); i++) {
175 for (int j=0; j < DN.ESCAPED_CHAR.length; j++) {
176 if (type.charAt(i) == DN.ESCAPED_CHAR[j]) {
177 return false;
178 }
179 }
180 }
181 return true;
182 }
183
184 /**
185 * Values can contain only single quote sequence, where quotes are
186 * at the beginning and the end of the sequence
187 */
188 private boolean isValidValue(String val) {
189 if (val == null || val.length() < 1) {
190 return false;
191 }
192 // count unescaped '"'
193 int cnt=0, i=0;
194 while (i >=0 && i < val.length()) {
195 i = val.indexOf('"', i);
196 if (i >= 0) {
197 if (i==0 || (val.charAt(i-1) != '\\')) {
198 cnt++;
199 }
200 i++;
201 }
202 }
203 if (cnt == 0) {
204 return true;
205 }
206 else if (cnt != 2) { // can have only two of them surrounding the value
207 return false;
208 }
209 else if (val.charAt(0) != '"' || val.charAt(val.length()-1) != '"') {
210 return false;
211 }
212
213 return true;
214 }
215
216 /**
217 * Returns the DN component as the first element in an
218 * array of strings.
219 * @param noType specify <code>true</code> to ignore the attribute type and
220 * equals sign (for example, "cn=") and return only the value
221 * @return an array of strings representing the DN component.
222 * @deprecated use <code>toString</code> or <code>getValues</code> instead.
223 */
224 public String[] explodeRDN(boolean noType) {
225 if (m_type == null)
226 return null;
227 String str[] = new String[1];
228 if (noType) {
229 str[0] = getValue();
230 } else {
231 str[0] = toString();
232 }
233 return str;
234 }
235
236 /**
237 * Returns the attribute type of the DN component.
238 * @return rdn the attribute type of the DN component.
239 * @deprecated use <code>getTypes()</code> instead.
240 */
241 public String getType() {
242 if (m_type != null && m_type.length > 0) {
243 return m_type[0];
244 }
245 return null;
246 }
247
248 /**
249 * Returns the attribute types of the DN component.
250 * @return rdn the attribute types of the DN component.
251 */
252 public String[] getTypes() {
253 return m_type;
254 }
255
256 /**
257 * Returns the value of the DN component.
258 * @return rdn the value of the DN component.
259 * @deprecated use <code>getValues()</code> instead.
260 */
261 public String getValue() {
262 if (m_value != null && m_value.length > 0) {
263 return m_value[0];
264 }
265 return null;
266 }
267
268 /**
269 * Returns the values of the DN component.
270 * @return rdn the values of the DN component.
271 */
272 public String[] getValues() {
273 return m_value;
274 }
275
276 /**
277 * Returns <code>true</code> if the RDN is multi-valued.
278 * @return <code>true</code> if the RDN is multi-valued.
279 */
280 public boolean isMultivalued() {
281 return m_ismultivalued;
282 }
283
284 /**
285 * Returns the string representation of the DN component.
286 * @return the string representation of the DN component.
287 */
288 public String toString() {
289 StringBuffer buf = new StringBuffer();
290
291 for ( int i = 0; m_type != null && i < m_type.length; i++ ) {
292 if ( i != 0) {
293 buf.append(" + ");
294 }
295 buf.append( m_type[i] + "=" + m_value[i]);
296 }
297
298 return buf.toString();
299 }
300
301 /**
302 * Determines if the specified string is a distinguished name component.
303 * @param dn the string to check
304 * @return <code>true</code> if the string is a distinguished name component.
305 */
306 public static boolean isRDN(String rdn) {
307 RDN newrdn = new RDN(rdn);
308 return ((newrdn.getTypes() != null) && (newrdn.getValues() != null));
309 }
310
311 /**
312 * Determines if the current DN component is equal to the specified
313 * DN component. Uses an internal table of ces (case exact string)
314 * attributes to determine how the attributes should be compared.
315 * @param rdn the DN component to compare against the
316 * current DN component.
317 * @return <code>true</code> if the two DN components are equal.
318 * @see org.ietf.ldap.util.RDN#registerAttributeSyntax
319 * @see org.ietf.ldap.util.RDN#getAttributeSyntax
320 */
321 public boolean equals(RDN rdn) {
322 String[] this_types = getTypes().clone();
323 String[] this_values = getValues().clone();
324 String[] rdn_types = rdn.getTypes().clone();
325 String[] rdn_values = rdn.getValues().clone();
326
327 if ( this_types.length != rdn_types.length ) {
328 return false;
329 }
330
331 sortTypesAndValues( this_types, this_values );
332 sortTypesAndValues( rdn_types, rdn_values );
333
334 for (int i = 0; i < this_types.length; i++ ) {
335
336 if ( !this_types[i].equalsIgnoreCase( rdn_types[i] ) ) {
337 return false;
338 }
339
340 if ( CES_SYNTAX.equals( getAttributeSyntax( this_types[i] ) ) ) {
341 if ( !this_values[i].equals( rdn_values[i] ) ) {
342 return false;
343 }
344 } else {
345 if ( !this_values[i].equalsIgnoreCase( rdn_values[i] ) ) {
346 return false;
347 }
348 }
349 }
350
351 return true;
352 }
353
354
355 /* sorts the rdn components by attribute to make comparison easier */
356 void sortTypesAndValues( String[] types, String[] values ) {
357 boolean no_changes;
358 do {
359 no_changes = true;
360 for ( int i = 0; i < types.length - 1; i++ ) {
361 if ( types[i].toLowerCase().compareTo( types[i + 1].toLowerCase() ) > 0 ) {
362 String tmp_type = types[i];
363 String tmp_value = values[i];
364 types[i] = types[i + 1];
365 values[i] = values[i + 1];
366 types[i + 1] = tmp_type;
367 values[i + 1] = tmp_value;
368 no_changes = false;
369 }
370 }
371 } while ( no_changes = false );
372 }
373
374 /**
375 * Registers the the given attribute for the given syntax in an
376 * internal table. This table is used for attribute comparison in the
377 * <code>equals()</code> method.
378 * @param attr the attribute to register.
379 * @param oid the syntax to register with the attribute.
380 * @see org.ietf.ldap.util.RDN#equals
381 * @see org.ietf.ldap.util.RDN#unregisterAttributeSyntax
382 * @see org.ietf.ldap.util.RDN#getAttributeSyntax
383 * @see org.ietf.ldap.util.RDN#getAttributesForSyntax
384 */
385 public static void registerAttributeSyntax( String attr, String oid ) {
386 m_attributehash.put( attr.toLowerCase(), oid );
387 }
388
389 /**
390 * Removes the the given attribute from the attribute syntax table.
391 * @param attr the attribute to remove.
392 * @see org.ietf.ldap.util.RDN#registerAttributeSyntax
393 * @see org.ietf.ldap.util.RDN#getAttributeSyntax
394 * @see org.ietf.ldap.util.RDN#getAttributesForSyntax
395 */
396 public static void unregisterAttributeSyntax( String attr ) {
397 m_attributehash.remove( attr.toLowerCase() );
398 }
399
400 /**
401 * Returns the syntax for the attribute if the given attribute is registered
402 * in the internal attribute table.
403 * @param attr the attribute to lookup in the table.
404 * @return the syntax of the attribute if found, null otherwise.
405 * @see org.ietf.ldap.util.RDN#unregisterAttributeSyntax
406 * @see org.ietf.ldap.util.RDN#registerAttributeSyntax
407 * @see org.ietf.ldap.util.RDN#getAttributesForSyntax
408 */
409 public static String getAttributeSyntax( String attr ) {
410 return m_attributehash.get( attr.toLowerCase() );
411 }
412
413 /**
414 * Returns all attributes registered for the given syntax as a
415 * <code>String</code> Array.
416 * @param oid the syntax to look up in the table.
417 * @return all attributes for the given syntax as a <code>String[]</code>
418 * @see org.ietf.ldap.util.RDN#unregisterAttributeSyntax
419 * @see org.ietf.ldap.util.RDN#registerAttributeSyntax
420 * @see org.ietf.ldap.util.RDN#getAttributeSyntax
421 */
422 public static String[] getAttributesForSyntax( String oid ) {
423 Enumeration<String> itr = m_attributehash.keys();
424 Vector<String> key_v = new Vector<>();
425 String tmp_str = null;
426
427 while ( itr.hasMoreElements() ) {
428 tmp_str = itr.nextElement();
429 if ( oid.equals( m_attributehash.get( tmp_str ) ) ) {
430 key_v.addElement( tmp_str );
431 }
432 }
433
434 String[] str = new String[key_v.size()];
435 for ( int i = 0; i < str.length; i++ ) {
436 str[i] = new String( key_v.elementAt( i ) );
437 }
438
439 return str;
440 }
441
442 public static final String[] _cesAttributes = {
443 "adminurl",
444 "altserver",
445 "automountinformation",
446 "bootfile",
447 "bootparameter",
448 "cirbindcredentials",
449 "generation",
450 "homedirectory",
451 "internationalisdnnumber",
452 "labeleduri",
453 "membercertificatedescription",
454 "membernisnetgroup",
455 "memberuid",
456 "memberurl",
457 "nismapentry",
458 "nisnetgrouptriple",
459 "nsaddressbooksyncurl",
460 "presentationaddress",
461 "ref",
462 "replicaentryfilter",
463 "searchguide",
464 "subtreeaci",
465 "vlvfilter",
466 "vlvname",
467 "x121address"
468 };
469
470 public static final String CES_SYNTAX = "1.3.6.1.4.1.1466.115.121.1.26";
471
472 static {
473 /* static initializer to fill the ces attribute hash
474 * this list was generated from the slapd.at.conf that
475 * ships with Netscape Directory Server 4.1
476 */
477 for ( int i = 0; i < _cesAttributes.length; i++ ) {
478 registerAttributeSyntax( _cesAttributes[i], CES_SYNTAX );
479 }
480 }
481 }
482
483
484
485
486
487
488
489
490
491
492
493