| 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 |
package org.ietf.ldap.util; |
| 39 |
|
| 40 |
import java.util.Enumeration; |
| 41 |
import java.util.Hashtable; |
| 42 |
import java.util.Vector; |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
public final class RDN implements java.io.Serializable { |
| 65 |
|
| 66 |
static final long serialVersionUID = 7895454691174650321L; |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
private String[] m_type = null; |
| 72 |
private String[] m_value = null; |
| 73 |
private boolean m_ismultivalued = false; |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
private static Hashtable<String, String> m_attributehash = new Hashtable<>(); |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
public RDN( String rdn ) { |
| 86 |
String neutralRDN = neutralizeEscapes(rdn); |
| 87 |
if (neutralRDN == null) { |
| 88 |
return; |
| 89 |
} |
| 90 |
int index = neutralRDN.indexOf( "=" ); |
| 91 |
int next_plus; |
| 92 |
|
| 93 |
|
| 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 |
|
| 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; |
| 123 |
} |
| 124 |
m_value[i] = values.elementAt( i ); |
| 125 |
if (!isValidValue(m_value[i])) { |
| 126 |
m_type = m_value = null; |
| 127 |
return; |
| 128 |
} |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 186 |
|
| 187 |
|
| 188 |
private boolean isValidValue(String val) { |
| 189 |
if (val == null || val.length() < 1) { |
| 190 |
return false; |
| 191 |
} |
| 192 |
|
| 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) { |
| 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 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 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 |
|
| 238 |
|
| 239 |
|
| 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 |
|
| 250 |
|
| 251 |
|
| 252 |
public String[] getTypes() { |
| 253 |
return m_type; |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 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 |
|
| 270 |
|
| 271 |
|
| 272 |
public String[] getValues() { |
| 273 |
return m_value; |
| 274 |
} |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
public boolean isMultivalued() { |
| 281 |
return m_ismultivalued; |
| 282 |
} |
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 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 |
|
| 303 |
|
| 304 |
|
| 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 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 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 |
|
| 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 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
public static void registerAttributeSyntax( String attr, String oid ) { |
| 386 |
m_attributehash.put( attr.toLowerCase(), oid ); |
| 387 |
} |
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
public static void unregisterAttributeSyntax( String attr ) { |
| 397 |
m_attributehash.remove( attr.toLowerCase() ); |
| 398 |
} |
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
public static String getAttributeSyntax( String attr ) { |
| 410 |
return m_attributehash.get( attr.toLowerCase() ); |
| 411 |
} |
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 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 |
|
| 474 |
|
| 475 |
|
| 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 |
|