3 * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
7 * Copyright (c) 1994 Regents of the University of Michigan.
17 #include <ac/stdlib.h>
18 #include <ac/socket.h>
19 #include <ac/string.h>
23 #include "ldap_schema.h"
25 /* extension to UFN that turns trailing "dc=value" rdns in DNS style,
26 * e.g. "ou=People,dc=openldap,dc=org" => "People, openldap.org" */
30 /* parsing/printing routines */
31 static int str2strval( const char *str, struct berval *val,
32 const char **next, unsigned flags, unsigned *retFlags );
33 static int DCE2strval( const char *str, struct berval *val,
34 const char **next, unsigned flags );
35 static int IA52strval( const char *str, struct berval *val,
36 const char **next, unsigned flags );
37 static int quotedIA52strval( const char *str, struct berval *val,
38 const char **next, unsigned flags );
39 static int hexstr2binval( const char *str, struct berval *val,
40 const char **next, unsigned flags );
41 static int hexstr2bin( const char *str, char *c );
42 static int byte2hexpair( const char *val, char *pair );
43 static int binval2hexstr( struct berval *val, char *str );
44 static int strval2strlen( struct berval *val, unsigned flags,
46 static int strval2str( struct berval *val, char *str, unsigned flags,
48 static int strval2IA5strlen( struct berval *val, unsigned flags,
50 static int strval2IA5str( struct berval *val, char *str, unsigned flags,
52 static int strval2DCEstrlen( struct berval *val, unsigned flags,
54 static int strval2DCEstr( struct berval *val, char *str, unsigned flags,
56 static int strval2ADstrlen( struct berval *val, unsigned flags,
58 static int strval2ADstr( struct berval *val, char *str, unsigned flags,
60 static int dn2domain( LDAPDN *dn, struct berval *bv, int pos, int *iRDN );
63 static LDAPAVA * ldapava_new(
64 const struct berval *attr, const struct berval *val, unsigned flags );
66 /* Higher level helpers */
67 static int rdn2strlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len,
68 int ( *s2l )( struct berval *, unsigned, ber_len_t * ) );
69 static int rdn2str( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len,
70 int ( *s2s )( struct berval *, char *, unsigned, ber_len_t * ));
71 static int rdn2UFNstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len );
72 static int rdn2UFNstr( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len );
73 static int rdn2DCEstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len );
74 static int rdn2DCEstr( LDAPRDN *rdn, char *str, unsigned flag, ber_len_t *len, int first );
75 static int rdn2ADstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len );
76 static int rdn2ADstr( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len, int first );
79 * RFC 1823 ldap_get_dn
82 ldap_get_dn( LDAP *ld, LDAPMessage *entry )
87 Debug( LDAP_DEBUG_TRACE, "ldap_get_dn\n", 0, 0, 0 );
89 if ( entry == NULL ) {
90 ld->ld_errno = LDAP_PARAM_ERROR;
94 tmp = *entry->lm_ber; /* struct copy */
95 if ( ber_scanf( &tmp, "{a" /*}*/, &dn ) == LBER_ERROR ) {
96 ld->ld_errno = LDAP_DECODING_ERROR;
104 * RFC 1823 ldap_dn2ufn
107 ldap_dn2ufn( LDAP_CONST char *dn )
111 Debug( LDAP_DEBUG_TRACE, "ldap_dn2ufn\n", 0, 0, 0 );
113 ( void )ldap_dn_normalize( dn, LDAP_DN_FORMAT_LDAP,
114 &out, LDAP_DN_FORMAT_UFN );
120 * RFC 1823 ldap_explode_dn
123 ldap_explode_dn( LDAP_CONST char *dn, int notypes )
126 char **values = NULL;
128 unsigned flag = notypes ? LDAP_DN_FORMAT_UFN : LDAP_DN_FORMAT_LDAPV3;
130 Debug( LDAP_DEBUG_TRACE, "ldap_explode_dn\n", 0, 0, 0 );
132 if ( ldap_str2dn( dn, &tmpDN, LDAP_DN_FORMAT_LDAP )
137 if( tmpDN == NULL ) {
138 values = LDAP_MALLOC( sizeof( char * ) );
139 if( values == NULL ) return NULL;
145 for ( iRDN = 0; tmpDN[ 0 ][ iRDN ]; iRDN++ );
147 values = LDAP_MALLOC( sizeof( char * ) * ( 1 + iRDN ) );
148 if ( values == NULL ) {
149 ldap_dnfree( tmpDN );
153 for ( iRDN = 0; tmpDN[ 0 ][ iRDN ]; iRDN++ ) {
154 ldap_rdn2str( tmpDN[ 0 ][ iRDN ], &values[ iRDN ], flag );
156 ldap_dnfree( tmpDN );
157 values[ iRDN ] = NULL;
163 ldap_explode_rdn( LDAP_CONST char *rdn, int notypes )
166 char **values = NULL;
170 Debug( LDAP_DEBUG_TRACE, "ldap_explode_rdn\n", 0, 0, 0 );
173 * we only parse the first rdn
174 * FIXME: we prefer efficiency over checking if the _ENTIRE_
177 if ( ldap_str2rdn( rdn, &tmpRDN, (char **) &p, LDAP_DN_FORMAT_LDAP )
182 for ( iAVA = 0; tmpRDN[ 0 ][ iAVA ]; iAVA++ ) ;
183 values = LDAP_MALLOC( sizeof( char * ) * ( 1 + iAVA ) );
184 if ( values == NULL ) {
185 ldap_rdnfree( tmpRDN );
189 for ( iAVA = 0; tmpRDN[ 0 ][ iAVA ]; iAVA++ ) {
190 ber_len_t l = 0, vl, al = 0;
192 LDAPAVA *ava = tmpRDN[ 0 ][ iAVA ];
194 if ( ava->la_flags == LDAP_AVA_BINARY ) {
195 vl = 1 + 2 * ava->la_value.bv_len;
198 if ( strval2strlen( &ava->la_value,
199 ava->la_flags, &vl ) ) {
205 al = ava->la_attr.bv_len;
206 l = vl + ava->la_attr.bv_len + 1;
208 str = LDAP_MALLOC( l + 1 );
209 AC_MEMCPY( str, ava->la_attr.bv_val,
210 ava->la_attr.bv_len );
215 str = LDAP_MALLOC( l + 1 );
218 if ( ava->la_flags == LDAP_AVA_BINARY ) {
220 if ( binval2hexstr( &ava->la_value, &str[ al ] ) ) {
225 if ( strval2str( &ava->la_value, &str[ al ],
226 ava->la_flags, &vl ) ) {
232 values[ iAVA ] = str;
234 values[ iAVA ] = NULL;
236 ldap_rdnfree( tmpRDN );
241 LBER_VFREE( values );
242 ldap_rdnfree( tmpRDN );
247 ldap_dn2dcedn( LDAP_CONST char *dn )
251 Debug( LDAP_DEBUG_TRACE, "ldap_dn2dcedn\n", 0, 0, 0 );
253 ( void )ldap_dn_normalize( dn, LDAP_DN_FORMAT_LDAP,
254 &out, LDAP_DN_FORMAT_DCE );
260 ldap_dcedn2dn( LDAP_CONST char *dce )
264 Debug( LDAP_DEBUG_TRACE, "ldap_dcedn2dn\n", 0, 0, 0 );
266 ( void )ldap_dn_normalize( dce, LDAP_DN_FORMAT_DCE, &out, LDAP_DN_FORMAT_LDAPV3 );
272 ldap_dn2ad_canonical( LDAP_CONST char *dn )
276 Debug( LDAP_DEBUG_TRACE, "ldap_dn2ad_canonical\n", 0, 0, 0 );
278 ( void )ldap_dn_normalize( dn, LDAP_DN_FORMAT_LDAP,
279 &out, LDAP_DN_FORMAT_AD_CANONICAL );
285 * function that changes the string representation of dnin
286 * from ( fin & LDAP_DN_FORMAT_MASK ) to ( fout & LDAP_DN_FORMAT_MASK )
289 * LDAP_DN_FORMAT_LDAP (rfc 2253 and ldapbis liberal,
290 * plus some rfc 1779)
291 * LDAP_DN_FORMAT_LDAPV3 (rfc 2253 and ldapbis)
292 * LDAP_DN_FORMAT_LDAPV2 (rfc 1779)
293 * LDAP_DN_FORMAT_DCE (?)
295 * fout can be any of the above except
296 * LDAP_DN_FORMAT_LDAP
298 * LDAP_DN_FORMAT_UFN (rfc 1781, partial and with extensions)
299 * LDAP_DN_FORMAT_AD_CANONICAL (?)
302 ldap_dn_normalize( LDAP_CONST char *dnin,
303 unsigned fin, char **dnout, unsigned fout )
306 LDAPDN *tmpDN = NULL;
308 Debug( LDAP_DEBUG_TRACE, "ldap_dn_normalize\n", 0, 0, 0 );
314 if ( dnin == NULL ) {
315 return( LDAP_SUCCESS );
318 rc = ldap_str2dn( dnin , &tmpDN, fin );
319 if ( rc != LDAP_SUCCESS ) {
323 rc = ldap_dn2str( tmpDN, dnout, fout );
325 ldap_dnfree( tmpDN );
333 /* #define B4ATTRTYPE 0x0001 */
334 #define B4OIDATTRTYPE 0x0002
335 #define B4STRINGATTRTYPE 0x0003
337 #define B4AVAEQUALS 0x0100
338 #define B4AVASEP 0x0200
339 #define B4RDNSEP 0x0300
340 #define GOTAVA 0x0400
342 #define B4ATTRVALUE 0x0010
343 #define B4STRINGVALUE 0x0020
344 #define B4IA5VALUEQUOTED 0x0030
345 #define B4IA5VALUE 0x0040
346 #define B4BINARYVALUE 0x0050
349 * Helpers (mostly from slap.h)
350 * c is assumed to Unicode in an ASCII compatible format (UTF-8)
351 * Macros assume "C" Locale (ASCII)
353 #define LDAP_DN_ASCII_SPACE(c) \
354 ( (c) == ' ' || (c) == '\t' || (c) == '\n' || (c) == '\r' )
355 #define LDAP_DN_ASCII_LOWER(c) LDAP_LOWER(c)
356 #define LDAP_DN_ASCII_UPPER(c) LDAP_UPPER(c)
357 #define LDAP_DN_ASCII_ALPHA(c) LDAP_ALPHA(c)
359 #define LDAP_DN_ASCII_DIGIT(c) LDAP_DIGIT(c)
360 #define LDAP_DN_ASCII_LCASE_HEXALPHA(c) LDAP_HEXLOWER(c)
361 #define LDAP_DN_ASCII_UCASE_HEXALPHA(c) LDAP_HEXUPPER(c)
362 #define LDAP_DN_ASCII_HEXDIGIT(c) LDAP_HEX(c)
363 #define LDAP_DN_ASCII_ALNUM(c) LDAP_ALNUM(c)
364 #define LDAP_DN_ASCII_PRINTABLE(c) ( (c) >= ' ' && (c) <= '~' )
367 #define LDAP_DN_OID_LEADCHAR(c) LDAP_DIGIT(c)
368 #define LDAP_DN_DESC_LEADCHAR(c) LDAP_ALPHA(c)
369 #define LDAP_DN_DESC_CHAR(c) LDAP_LDH(c)
370 #define LDAP_DN_LANG_SEP(c) ( (c) == ';' )
371 #define LDAP_DN_ATTRDESC_CHAR(c) \
372 ( LDAP_DN_DESC_CHAR(c) || LDAP_DN_LANG_SEP(c) )
374 /* special symbols */
375 #define LDAP_DN_AVA_EQUALS(c) ( (c) == '=' )
376 #define LDAP_DN_AVA_SEP(c) ( (c) == '+' )
377 #define LDAP_DN_RDN_SEP(c) ( (c) == ',' )
378 #define LDAP_DN_RDN_SEP_V2(c) ( LDAP_DN_RDN_SEP(c) || (c) == ';' )
379 #define LDAP_DN_OCTOTHORPE(c) ( (c) == '#' )
380 #define LDAP_DN_QUOTES(c) ( (c) == '\"' )
381 #define LDAP_DN_ESCAPE(c) ( (c) == '\\' )
382 #define LDAP_DN_VALUE_END(c) \
383 ( LDAP_DN_RDN_SEP(c) || LDAP_DN_AVA_SEP(c) )
384 #define LDAP_DN_NE(c) \
385 ( LDAP_DN_RDN_SEP_V2(c) || LDAP_DN_AVA_SEP(c) \
386 || LDAP_DN_QUOTES(c) || (c) == '<' || (c) == '>' )
387 #define LDAP_DN_MAYESCAPE(c) \
388 ( LDAP_DN_ESCAPE(c) || LDAP_DN_NE(c) \
389 || LDAP_DN_ASCII_SPACE(c) || LDAP_DN_OCTOTHORPE(c) )
390 #define LDAP_DN_NEEDESCAPE(c) \
391 ( LDAP_DN_ESCAPE(c) || LDAP_DN_NE(c) )
392 #define LDAP_DN_NEEDESCAPE_LEAD(c) LDAP_DN_MAYESCAPE(c)
393 #define LDAP_DN_NEEDESCAPE_TRAIL(c) \
394 ( LDAP_DN_ASCII_SPACE(c) || LDAP_DN_NEEDESCAPE(c) )
395 #define LDAP_DN_WILLESCAPE_CHAR(c) \
396 ( LDAP_DN_RDN_SEP(c) || LDAP_DN_AVA_SEP(c) || LDAP_DN_ESCAPE(c) )
397 #define LDAP_DN_IS_PRETTY(f) ( (f) & LDAP_DN_PRETTY )
398 #define LDAP_DN_WILLESCAPE_HEX(f, c) \
399 ( ( !LDAP_DN_IS_PRETTY( f ) ) && LDAP_DN_WILLESCAPE_CHAR(c) )
402 #define LDAP_DN_VALUE_END_V2(c) \
403 ( LDAP_DN_RDN_SEP_V2(c) || LDAP_DN_AVA_SEP(c) )
405 #define LDAP_DN_V2_SPECIAL(c) \
406 ( LDAP_DN_RDN_SEP_V2(c) || LDAP_DN_AVA_EQUALS(c) \
407 || LDAP_DN_AVA_SEP(c) || (c) == '<' || (c) == '>' \
408 || LDAP_DN_OCTOTHORPE(c) )
409 #define LDAP_DN_V2_PAIR(c) \
410 ( LDAP_DN_V2_SPECIAL(c) || LDAP_DN_ESCAPE(c) || LDAP_DN_QUOTES(c) )
413 * DCE (mostly from Luke Howard and IBM implementation for AIX)
415 * From: "Application Development Guide - Directory Services" (FIXME: add link?)
416 * Here escapes and valid chars for GDS are considered; as soon as more
417 * specific info is found, the macros will be updated.
419 * Chars: 'a'-'z', 'A'-'Z', '0'-'9',
420 * '.', ':', ',', ''', '+', '-', '=', '(', ')', '?', '/', ' '.
422 * Metachars: '/', ',', '=', '\'.
424 * the '\' is used to escape other metachars.
430 * Attribute types must start with alphabetic chars and can contain
431 * alphabetic chars and digits (FIXME: no '-'?). OIDs are allowed.
433 #define LDAP_DN_RDN_SEP_DCE(c) ( (c) == '/' )
434 #define LDAP_DN_AVA_SEP_DCE(c) ( (c) == ',' )
435 #define LDAP_DN_ESCAPE_DCE(c) ( LDAP_DN_ESCAPE(c) )
436 #define LDAP_DN_VALUE_END_DCE(c) \
437 ( LDAP_DN_RDN_SEP_DCE(c) || LDAP_DN_AVA_SEP_DCE(c) )
438 #define LDAP_DN_NEEDESCAPE_DCE(c) \
439 ( LDAP_DN_VALUE_END_DCE(c) || LDAP_DN_AVA_EQUALS(c) )
442 #define LDAP_DN_RDN_SEP_AD(c) ( (c) == '/' )
443 #define LDAP_DN_ESCAPE_AD(c) ( LDAP_DN_ESCAPE(c) )
444 #define LDAP_DN_AVA_SEP_AD(c) ( (c) == ',' ) /* assume same as DCE */
445 #define LDAP_DN_VALUE_END_AD(c) \
446 ( LDAP_DN_RDN_SEP_AD(c) || LDAP_DN_AVA_SEP_AD(c) )
447 #define LDAP_DN_NEEDESCAPE_AD(c) \
448 ( LDAP_DN_VALUE_END_AD(c) || LDAP_DN_AVA_EQUALS(c) )
451 #define LDAP_DN_HEXPAIR(s) \
452 ( LDAP_DN_ASCII_HEXDIGIT((s)[0]) && LDAP_DN_ASCII_HEXDIGIT((s)[1]) )
453 /* better look at the AttributeDescription? */
455 /* FIXME: no composite rdn or non-"dc" types, right?
456 * (what about "dc" in OID form?) */
457 /* FIXME: we do not allow binary values in domain, right? */
458 /* NOTE: use this macro only when ABSOLUTELY SURE rdn IS VALID! */
459 /* NOTE: don't use strcasecmp() as it is locale specific! */
460 #define LDAP_DC_ATTR "dc"
461 #define LDAP_DC_ATTRU "DC"
462 #define LDAP_DN_IS_RDN_DC( r ) \
463 ( (r) && (r)[0][0] && !(r)[0][1] \
464 && ((r)[0][0]->la_flags == LDAP_AVA_STRING) \
465 && ((r)[0][0]->la_attr.bv_len == 2) \
466 && (((r)[0][0]->la_attr.bv_val[0] == LDAP_DC_ATTR[0]) \
467 || ((r)[0][0]->la_attr.bv_val[0] == LDAP_DC_ATTRU[0])) \
468 && (((r)[0][0]->la_attr.bv_val[1] == LDAP_DC_ATTR[1]) \
469 || ((r)[0][0]->la_attr.bv_val[1] == LDAP_DC_ATTRU[1])))
471 /* Composite rules */
472 #define LDAP_DN_ALLOW_ONE_SPACE(f) \
473 ( LDAP_DN_LDAPV2(f) \
474 || !( (f) & LDAP_DN_P_NOSPACEAFTERRDN ) )
475 #define LDAP_DN_ALLOW_SPACES(f) \
476 ( LDAP_DN_LDAPV2(f) \
477 || !( (f) & ( LDAP_DN_P_NOLEADTRAILSPACES | LDAP_DN_P_NOSPACEAFTERRDN ) ) )
478 #define LDAP_DN_LDAP(f) \
479 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_LDAP )
480 #define LDAP_DN_LDAPV3(f) \
481 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_LDAPV3 )
482 #define LDAP_DN_LDAPV2(f) \
483 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_LDAPV2 )
484 #define LDAP_DN_DCE(f) \
485 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_DCE )
486 #define LDAP_DN_UFN(f) \
487 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_UFN )
488 #define LDAP_DN_ADC(f) \
489 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_AD_CANONICAL )
490 #define LDAP_DN_FORMAT(f) ( (f) & LDAP_DN_FORMAT_MASK )
493 * LDAPAVA helpers (will become part of the API for operations
494 * on structural representations of DNs).
497 ldapava_new( const struct berval *attr, const struct berval *val,
505 ava = LDAP_MALLOC( sizeof( LDAPAVA ) + attr->bv_len + 1 );
507 /* should we test it? */
512 ava->la_attr.bv_len = attr->bv_len;
513 ava->la_attr.bv_val = (char *)(ava+1);
514 AC_MEMCPY( ava->la_attr.bv_val, attr->bv_val, attr->bv_len );
515 ava->la_attr.bv_val[attr->bv_len] = '\0';
517 ava->la_value = *val;
518 ava->la_flags = flags;
520 ava->la_private = NULL;
526 ldap_avafree( LDAPAVA *ava )
531 /* ava's private must be freed by caller
532 * (at present let's skip this check because la_private
533 * basically holds static data) */
534 assert( ava->la_private == NULL );
538 /* la_attr is now contiguous with ava, not freed separately */
539 LDAP_FREE( ava->la_attr.bv_val );
541 LDAP_FREE( ava->la_value.bv_val );
547 ldap_rdnfree( LDAPRDN *rdn )
555 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
556 ldap_avafree( rdn[ 0 ][ iAVA ] );
563 ldap_dnfree( LDAPDN *dn )
571 for ( iRDN = 0; dn[ 0 ][ iRDN ]; iRDN++ ) {
572 ldap_rdnfree( dn[ 0 ][ iRDN ] );
579 * Converts a string representation of a DN (in LDAPv3, LDAPv2 or DCE)
580 * into a structural representation of the DN, by separating attribute
581 * types and values encoded in the more appropriate form, which is
582 * string or OID for attribute types and binary form of the BER encoded
583 * value or Unicode string. Formats different from LDAPv3 are parsed
584 * according to their own rules and turned into the more appropriate
585 * form according to LDAPv3.
587 * NOTE: I realize the code is getting spaghettish; it is rather
588 * experimental and will hopefully turn into something more simple
589 * and readable as soon as it works as expected.
593 * Default sizes of AVA and RDN static working arrays; if required
594 * the are dynamically resized. The values can be tuned in case
595 * of special requirements (e.g. very deep DN trees or high number
598 #define TMP_AVA_SLOTS 8
599 #define TMP_RDN_SLOTS 32
602 ldap_str2dn( LDAP_CONST char *str, LDAPDN **dn, unsigned flags )
605 int rc = LDAP_DECODING_ERROR;
608 LDAPDN *newDN = NULL;
609 LDAPRDN *newRDN = NULL, *tmpDN_[TMP_RDN_SLOTS], **tmpDN = tmpDN_;
610 int num_slots = TMP_RDN_SLOTS;
615 Debug( LDAP_DEBUG_TRACE, "=> ldap_str2dn(%s,%u)\n%s", str, flags, "" );
619 switch ( LDAP_DN_FORMAT( flags ) ) {
620 case LDAP_DN_FORMAT_LDAP:
621 case LDAP_DN_FORMAT_LDAPV3:
622 case LDAP_DN_FORMAT_LDAPV2:
623 case LDAP_DN_FORMAT_DCE:
626 /* unsupported in str2dn */
627 case LDAP_DN_FORMAT_UFN:
628 case LDAP_DN_FORMAT_AD_CANONICAL:
629 return LDAP_PARAM_ERROR;
631 case LDAP_DN_FORMAT_LBER:
633 return LDAP_PARAM_ERROR;
636 if ( str[ 0 ] == '\0' ) {
641 if ( LDAP_DN_DCE( flags ) ) {
644 * (from Luke Howard: thnx) A RDN separator is required
645 * at the beginning of an (absolute) DN.
647 if ( !LDAP_DN_RDN_SEP_DCE( p[ 0 ] ) ) {
653 * actually we do not want to accept by default the DCE form,
654 * we do not want to auto-detect it
657 } else if ( LDAP_DN_LDAP( flags ) ) {
659 * if dn starts with '/' let's make it a DCE dn
661 if ( LDAP_DN_RDN_SEP_DCE( p[ 0 ] ) ) {
662 flags |= LDAP_DN_FORMAT_DCE;
668 for ( ; p[ 0 ]; p++ ) {
671 err = ldap_str2rdn( p, &newRDN, (char **) &p, flags );
672 if ( err != LDAP_SUCCESS ) {
677 * We expect a rdn separator
680 switch ( LDAP_DN_FORMAT( flags ) ) {
681 case LDAP_DN_FORMAT_LDAPV3:
682 if ( !LDAP_DN_RDN_SEP( p[ 0 ] ) ) {
683 rc = LDAP_DECODING_ERROR;
688 case LDAP_DN_FORMAT_LDAP:
689 case LDAP_DN_FORMAT_LDAPV2:
690 if ( !LDAP_DN_RDN_SEP_V2( p[ 0 ] ) ) {
691 rc = LDAP_DECODING_ERROR;
696 case LDAP_DN_FORMAT_DCE:
697 if ( !LDAP_DN_RDN_SEP_DCE( p[ 0 ] ) ) {
698 rc = LDAP_DECODING_ERROR;
706 tmpDN[nrdns++] = newRDN;
710 * make the static RDN array dynamically rescalable
712 if ( nrdns == num_slots ) {
715 if ( tmpDN == tmpDN_ ) {
716 tmp = LDAP_MALLOC( num_slots * 2 * sizeof( LDAPRDN * ) );
721 AC_MEMCPY( tmp, tmpDN, num_slots * sizeof( LDAPRDN * ) );
724 tmp = LDAP_REALLOC( tmpDN, num_slots * 2 * sizeof( LDAPRDN * ) );
735 if ( p[ 0 ] == '\0' ) {
737 * the DN is over, phew
739 newDN = (LDAPDN *)LDAP_MALLOC( sizeof(LDAPDN) +
740 sizeof(LDAPRDN *) * (nrdns+1));
741 if ( newDN == NULL ) {
747 newDN[0] = (LDAPRDN **)(newDN+1);
749 if ( LDAP_DN_DCE( flags ) ) {
750 /* add in reversed order */
751 for ( i=0; i<nrdns; i++ )
752 newDN[0][i] = tmpDN[nrdns-1-i];
754 for ( i=0; i<nrdns; i++ )
755 newDN[0][i] = tmpDN[i];
757 newDN[0][nrdns] = NULL;
766 ldap_rdnfree( newRDN );
769 for ( nrdns-- ;nrdns >= 0; nrdns-- ) {
770 ldap_rdnfree( tmpDN[nrdns] );
775 if ( tmpDN != tmpDN_ ) {
779 Debug( LDAP_DEBUG_TRACE, "<= ldap_str2dn(%s,%u)=%d\n", str, flags, rc );
788 * Parses a relative DN according to flags up to a rdn separator
789 * or to the end of str.
790 * Returns the rdn and a pointer to the string continuation, which
791 * corresponds to the rdn separator or to '\0' in case the string is over.
794 ldap_str2rdn( LDAP_CONST char *str, LDAPRDN **rdn,
795 char **n_in, unsigned flags )
797 const char **n = (const char **) n_in;
801 int rc = LDAP_DECODING_ERROR;
802 int attrTypeEncoding = LDAP_AVA_STRING,
803 attrValueEncoding = LDAP_AVA_STRING;
805 struct berval attrType = { 0, NULL };
806 struct berval attrValue = { 0, NULL };
808 LDAPRDN *newRDN = NULL;
809 LDAPAVA *tmpRDN_[TMP_AVA_SLOTS], **tmpRDN = tmpRDN_;
810 int num_slots = TMP_AVA_SLOTS;
813 assert( rdn || flags & LDAP_DN_SKIP );
816 Debug( LDAP_DEBUG_TRACE, "=> ldap_str2rdn(%s,%u)\n%s", str, flags, "" );
823 switch ( LDAP_DN_FORMAT( flags ) ) {
824 case LDAP_DN_FORMAT_LDAP:
825 case LDAP_DN_FORMAT_LDAPV3:
826 case LDAP_DN_FORMAT_LDAPV2:
827 case LDAP_DN_FORMAT_DCE:
830 /* unsupported in str2dn */
831 case LDAP_DN_FORMAT_UFN:
832 case LDAP_DN_FORMAT_AD_CANONICAL:
833 return LDAP_PARAM_ERROR;
835 case LDAP_DN_FORMAT_LBER:
837 return LDAP_PARAM_ERROR;
840 if ( str[ 0 ] == '\0' ) {
845 for ( ; p[ 0 ] || state == GOTAVA; ) {
848 * The parser in principle advances one token a time,
849 * or toggles state if preferable.
854 * an AttributeType can be encoded as:
855 * - its string representation; in detail, implementations
856 * MUST recognize AttributeType string type names listed
857 * in section 2.3 of draft-ietf-ldapbis-dn-XX.txt, and
858 * MAY recognize other names.
859 * - its numeric OID (a dotted decimal string); in detail
860 * RFC 2253 asserts that ``Implementations MUST allow
861 * an oid in the attribute type to be prefixed by one
862 * of the character strings "oid." or "OID."''. As soon
863 * as draft-ietf-ldapbis-dn-XX.txt obsoletes RFC 2253
864 * I'm not sure whether this is required or not any
865 * longer; to be liberal, we still implement it.
868 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
869 if ( !LDAP_DN_ALLOW_ONE_SPACE( flags ) ) {
876 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
877 if ( !LDAP_DN_ALLOW_SPACES( flags ) ) {
882 /* whitespace is allowed (and trimmed) */
884 while ( p[ 0 ] && LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
889 /* error: we expected an AVA */
895 if ( LDAP_DN_OID_LEADCHAR( p[ 0 ] ) ) {
896 state = B4OIDATTRTYPE;
900 /* else must be alpha */
901 if ( !LDAP_DN_DESC_LEADCHAR( p[ 0 ] ) ) {
905 /* LDAPv2 "oid." prefix */
906 if ( LDAP_DN_LDAPV2( flags ) ) {
908 * to be overly pedantic, we only accept
911 if ( flags & LDAP_DN_PEDANTIC ) {
912 if ( !strncmp( p, "OID.", 4 )
913 || !strncmp( p, "oid.", 4 ) ) {
915 state = B4OIDATTRTYPE;
919 if ( !strncasecmp( p, "oid.", 4 ) ) {
921 state = B4OIDATTRTYPE;
927 state = B4STRINGATTRTYPE;
930 case B4OIDATTRTYPE: {
931 int err = LDAP_SUCCESS;
933 attrType.bv_val = ldap_int_parse_numericoid( &p, &err,
936 if ( err != LDAP_SUCCESS ) {
939 attrType.bv_len = p - attrType.bv_val;
941 attrTypeEncoding = LDAP_AVA_BINARY;
947 case B4STRINGATTRTYPE: {
948 const char *startPos, *endPos = NULL;
952 * the starting char has been found to be
953 * a LDAP_DN_DESC_LEADCHAR so we don't re-check it
954 * FIXME: DCE attr types seem to have a more
955 * restrictive syntax (no '-' ...)
957 for ( startPos = p++; p[ 0 ]; p++ ) {
958 if ( LDAP_DN_DESC_CHAR( p[ 0 ] ) ) {
962 if ( LDAP_DN_LANG_SEP( p[ 0 ] ) ) {
965 * RFC 2253 does not explicitly
966 * allow lang extensions to attribute
969 if ( flags & LDAP_DN_PEDANTIC ) {
974 * we trim ';' and following lang
975 * and so from attribute types
978 for ( ; LDAP_DN_ATTRDESC_CHAR( p[ 0 ] )
979 || LDAP_DN_LANG_SEP( p[ 0 ] ); p++ ) {
987 len = ( endPos ? endPos : p ) - startPos;
992 attrTypeEncoding = LDAP_AVA_STRING;
995 * here we need to decide whether to use it as is
996 * or turn it in OID form; as a consequence, we
997 * need to decide whether to binary encode the value
1000 state = B4AVAEQUALS;
1002 if ( flags & LDAP_DN_SKIP ) {
1006 attrType.bv_val = (char *)startPos;
1007 attrType.bv_len = len;
1013 /* spaces may not be allowed */
1014 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1015 if ( !LDAP_DN_ALLOW_SPACES( flags ) ) {
1020 for ( p++; LDAP_DN_ASCII_SPACE( p[ 0 ] ); p++ ) {
1025 /* need equal sign */
1026 if ( !LDAP_DN_AVA_EQUALS( p[ 0 ] ) ) {
1031 /* spaces may not be allowed */
1032 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1033 if ( !LDAP_DN_ALLOW_SPACES( flags ) ) {
1038 for ( p++; LDAP_DN_ASCII_SPACE( p[ 0 ] ); p++ ) {
1044 * octothorpe means a BER encoded value will follow
1045 * FIXME: I don't think DCE will allow it
1047 if ( LDAP_DN_OCTOTHORPE( p[ 0 ] ) ) {
1049 attrValueEncoding = LDAP_AVA_BINARY;
1050 state = B4BINARYVALUE;
1054 /* STRING value expected */
1057 * if we're pedantic, an attribute type in OID form
1058 * SHOULD imply a BER encoded attribute value; we
1059 * should at least issue a warning
1061 if ( ( flags & LDAP_DN_PEDANTIC )
1062 && ( attrTypeEncoding == LDAP_AVA_BINARY ) ) {
1063 /* OID attrType SHOULD use binary encoding */
1067 attrValueEncoding = LDAP_AVA_STRING;
1070 * LDAPv2 allows the attribute value to be quoted;
1071 * also, IA5 values are expected, in principle
1073 if ( LDAP_DN_LDAPV2( flags ) || LDAP_DN_LDAP( flags ) ) {
1074 if ( LDAP_DN_QUOTES( p[ 0 ] ) ) {
1076 state = B4IA5VALUEQUOTED;
1080 if ( LDAP_DN_LDAPV2( flags ) ) {
1087 * here STRING means RFC 2253 string
1088 * FIXME: what about DCE strings?
1094 state = B4STRINGVALUE;
1099 if ( hexstr2binval( p, &attrValue, &p, flags ) ) {
1107 switch ( LDAP_DN_FORMAT( flags ) ) {
1108 case LDAP_DN_FORMAT_LDAP:
1109 case LDAP_DN_FORMAT_LDAPV3:
1110 if ( str2strval( p, &attrValue, &p, flags,
1111 &attrValueEncoding ) ) {
1116 case LDAP_DN_FORMAT_DCE:
1117 if ( DCE2strval( p, &attrValue, &p, flags ) ) {
1130 if ( IA52strval( p, &attrValue, &p, flags ) ) {
1137 case B4IA5VALUEQUOTED:
1139 /* lead quote already stripped */
1140 if ( quotedIA52strval( p, &attrValue,
1151 if ( !( flags & LDAP_DN_SKIP ) ) {
1155 * we accept empty values
1157 ava = ldapava_new( &attrType, &attrValue,
1158 attrValueEncoding );
1160 if ( ava == NULL ) {
1161 rc = LDAP_NO_MEMORY;
1164 tmpRDN[navas++] = ava;
1166 attrValue.bv_val = NULL;
1167 attrValue.bv_len = 0;
1170 * prepare room for new AVAs if needed
1172 if (navas == num_slots) {
1175 if ( tmpRDN == tmpRDN_ ) {
1176 tmp = LDAP_MALLOC( num_slots * 2 * sizeof( LDAPAVA * ) );
1177 if ( tmp == NULL ) {
1178 rc = LDAP_NO_MEMORY;
1181 AC_MEMCPY( tmp, tmpRDN, num_slots * sizeof( LDAPAVA * ) );
1184 tmp = LDAP_REALLOC( tmpRDN, num_slots * 2 * sizeof( LDAPAVA * ) );
1185 if ( tmp == NULL ) {
1186 rc = LDAP_NO_MEMORY;
1197 * if we got an AVA separator ('+', or ',' for DCE )
1198 * we expect a new AVA for this RDN; otherwise
1199 * we add the RDN to the DN
1201 switch ( LDAP_DN_FORMAT( flags ) ) {
1202 case LDAP_DN_FORMAT_LDAP:
1203 case LDAP_DN_FORMAT_LDAPV3:
1204 case LDAP_DN_FORMAT_LDAPV2:
1205 if ( !LDAP_DN_AVA_SEP( p[ 0 ] ) ) {
1210 case LDAP_DN_FORMAT_DCE:
1211 if ( !LDAP_DN_AVA_SEP_DCE( p[ 0 ] ) ) {
1219 * the RDN is over, phew
1222 if ( !( flags & LDAP_DN_SKIP ) ) {
1223 newRDN = (LDAPRDN *)LDAP_MALLOC( sizeof(LDAPRDN)
1224 + sizeof(LDAPAVA *) * (navas+1) );
1225 if ( newRDN == NULL ) {
1226 rc = LDAP_NO_MEMORY;
1231 newRDN[0] = (LDAPAVA**)(newRDN+1);
1233 for (i=0; i<navas; i++)
1234 newRDN[0][i] = tmpRDN[i];
1235 newRDN[0][i] = NULL;
1243 /* they should have been used in an AVA */
1244 attrType.bv_val = NULL;
1245 attrValue.bv_val = NULL;
1260 /* They are set to NULL after they're used in an AVA */
1262 if ( attrValue.bv_val ) {
1263 free( attrValue.bv_val );
1266 for ( navas-- ; navas >= 0; navas-- ) {
1267 ldap_avafree( tmpRDN[navas] );
1272 if ( tmpRDN != tmpRDN_ ) {
1273 LDAP_FREE( tmpRDN );
1276 Debug( LDAP_DEBUG_TRACE, "<= ldap_str2rdn(%*s)=%d\n",
1287 * reads in a UTF-8 string value, unescaping stuff:
1288 * '\' + LDAP_DN_NEEDESCAPE(c) -> 'c'
1289 * '\' + HEXPAIR(p) -> unhex(p)
1292 str2strval( const char *str, struct berval *val, const char **next, unsigned flags, unsigned *retFlags )
1294 const char *p, *startPos, *endPos = NULL;
1295 ber_len_t len, escapes;
1303 for ( startPos = p = str, escapes = 0; p[ 0 ]; p++ ) {
1304 if ( LDAP_DN_ESCAPE( p[ 0 ] ) ) {
1306 if ( p[ 0 ] == '\0' ) {
1309 if ( LDAP_DN_MAYESCAPE( p[ 0 ] ) ) {
1314 if ( LDAP_DN_HEXPAIR( p ) ) {
1317 hexstr2bin( p, &c );
1320 if ( !LDAP_DN_ASCII_PRINTABLE( c ) ) {
1323 * we assume the string is UTF-8
1325 *retFlags = LDAP_AVA_NONPRINTABLE;
1332 if ( LDAP_DN_PEDANTIC & flags ) {
1336 * we do not allow escaping
1337 * of chars that don't need
1338 * to and do not belong to
1343 } else if (!LDAP_DN_ASCII_PRINTABLE( p[ 0 ] ) ) {
1344 *retFlags = LDAP_AVA_NONPRINTABLE;
1346 } else if ( ( LDAP_DN_LDAP( flags ) && LDAP_DN_VALUE_END_V2( p[ 0 ] ) )
1347 || ( LDAP_DN_LDAPV3( flags ) && LDAP_DN_VALUE_END( p[ 0 ] ) ) ) {
1350 } else if ( LDAP_DN_NEEDESCAPE( p[ 0 ] ) ) {
1352 * FIXME: maybe we can add
1353 * escapes if not pedantic?
1360 * we do allow unescaped spaces at the end
1361 * of the value only in non-pedantic mode
1363 if ( p > startPos + 1 && LDAP_DN_ASCII_SPACE( p[ -1 ] ) &&
1364 !LDAP_DN_ESCAPE( p[ -2 ] ) ) {
1365 if ( flags & LDAP_DN_PEDANTIC ) {
1369 /* strip trailing (unescaped) spaces */
1370 for ( endPos = p - 1;
1371 endPos > startPos + 1 &&
1372 LDAP_DN_ASCII_SPACE( endPos[ -1 ] ) &&
1373 !LDAP_DN_ESCAPE( endPos[ -2 ] );
1380 if ( flags & LDAP_DN_SKIP ) {
1385 * FIXME: test memory?
1387 len = ( endPos ? endPos : p ) - startPos - escapes;
1390 if ( escapes == 0 ) {
1391 val->bv_val = LDAP_STRNDUP( startPos, len );
1396 val->bv_val = LDAP_MALLOC( len + 1 );
1397 for ( s = 0, d = 0; d < len; ) {
1398 if ( LDAP_DN_ESCAPE( startPos[ s ] ) ) {
1400 if ( LDAP_DN_MAYESCAPE( startPos[ s ] ) ) {
1401 val->bv_val[ d++ ] =
1404 } else if ( LDAP_DN_HEXPAIR( &startPos[ s ] ) ) {
1407 hexstr2bin( &startPos[ s ], &c );
1408 val->bv_val[ d++ ] = c;
1412 /* we should never get here */
1417 val->bv_val[ d++ ] = startPos[ s++ ];
1421 val->bv_val[ d ] = '\0';
1429 DCE2strval( const char *str, struct berval *val, const char **next, unsigned flags )
1431 const char *p, *startPos, *endPos = NULL;
1432 ber_len_t len, escapes;
1440 for ( startPos = p = str, escapes = 0; p[ 0 ]; p++ ) {
1441 if ( LDAP_DN_ESCAPE_DCE( p[ 0 ] ) ) {
1443 if ( LDAP_DN_NEEDESCAPE_DCE( p[ 0 ] ) ) {
1450 } else if ( LDAP_DN_VALUE_END_DCE( p[ 0 ] ) ) {
1455 * FIXME: can we accept anything else? I guess we need
1456 * to stop if a value is not legal
1461 * (unescaped) trailing spaces are trimmed must be silently ignored;
1464 if ( p > startPos + 1 && LDAP_DN_ASCII_SPACE( p[ -1 ] ) &&
1465 !LDAP_DN_ESCAPE( p[ -2 ] ) ) {
1466 if ( flags & LDAP_DN_PEDANTIC ) {
1470 /* strip trailing (unescaped) spaces */
1471 for ( endPos = p - 1;
1472 endPos > startPos + 1 &&
1473 LDAP_DN_ASCII_SPACE( endPos[ -1 ] ) &&
1474 !LDAP_DN_ESCAPE( endPos[ -2 ] );
1481 if ( flags & LDAP_DN_SKIP ) {
1485 len = ( endPos ? endPos : p ) - startPos - escapes;
1487 if ( escapes == 0 ){
1488 val->bv_val = LDAP_STRNDUP( startPos, len );
1493 val->bv_val = LDAP_MALLOC( len + 1 );
1494 for ( s = 0, d = 0; d < len; ) {
1496 * This point is reached only if escapes
1497 * are properly used, so all we need to
1500 if ( LDAP_DN_ESCAPE_DCE( startPos[ s ] ) ) {
1504 val->bv_val[ d++ ] = startPos[ s++ ];
1506 val->bv_val[ d ] = '\0';
1507 assert( strlen( val->bv_val ) == len );
1514 IA52strval( const char *str, struct berval *val, const char **next, unsigned flags )
1516 const char *p, *startPos, *endPos = NULL;
1517 ber_len_t len, escapes;
1529 for ( startPos = p = str, escapes = 0; p[ 0 ]; p++ ) {
1530 if ( LDAP_DN_ESCAPE( p[ 0 ] ) ) {
1532 if ( p[ 0 ] == '\0' ) {
1536 if ( !LDAP_DN_NEEDESCAPE( p[ 0 ] )
1537 && ( LDAP_DN_PEDANTIC & flags ) ) {
1542 } else if ( LDAP_DN_VALUE_END_V2( p[ 0 ] ) ) {
1547 * FIXME: can we accept anything else? I guess we need
1548 * to stop if a value is not legal
1552 /* strip trailing (unescaped) spaces */
1554 endPos > startPos + 1 &&
1555 LDAP_DN_ASCII_SPACE( endPos[ -1 ] ) &&
1556 !LDAP_DN_ESCAPE( endPos[ -2 ] );
1562 if ( flags & LDAP_DN_SKIP ) {
1566 len = ( endPos ? endPos : p ) - startPos - escapes;
1568 if ( escapes == 0 ) {
1569 val->bv_val = LDAP_STRNDUP( startPos, len );
1574 val->bv_val = LDAP_MALLOC( len + 1 );
1575 for ( s = 0, d = 0; d < len; ) {
1576 if ( LDAP_DN_ESCAPE( startPos[ s ] ) ) {
1579 val->bv_val[ d++ ] = startPos[ s++ ];
1581 val->bv_val[ d ] = '\0';
1582 assert( strlen( val->bv_val ) == len );
1589 quotedIA52strval( const char *str, struct berval *val, const char **next, unsigned flags )
1591 const char *p, *startPos, *endPos = NULL;
1593 unsigned escapes = 0;
1601 /* initial quote already eaten */
1602 for ( startPos = p = str; p[ 0 ]; p++ ) {
1604 * According to RFC 1779, the quoted value can
1605 * contain escaped as well as unescaped special values;
1606 * as a consequence we tolerate escaped values
1607 * (e.g. '"\,"' -> '\,') and escape unescaped specials
1608 * (e.g. '","' -> '\,').
1610 if ( LDAP_DN_ESCAPE( p[ 0 ] ) ) {
1611 if ( p[ 1 ] == '\0' ) {
1616 if ( !LDAP_DN_V2_PAIR( p[ 0 ] )
1617 && ( LDAP_DN_PEDANTIC & flags ) ) {
1619 * do we allow to escape normal chars?
1620 * LDAPv2 does not allow any mechanism
1621 * for escaping chars with '\' and hex
1628 } else if ( LDAP_DN_QUOTES( p[ 0 ] ) ) {
1630 /* eat closing quotes */
1636 * FIXME: can we accept anything else? I guess we need
1637 * to stop if a value is not legal
1641 if ( endPos == NULL ) {
1645 /* Strip trailing (unescaped) spaces */
1646 for ( ; p[ 0 ] && LDAP_DN_ASCII_SPACE( p[ 0 ] ); p++ ) {
1651 if ( flags & LDAP_DN_SKIP ) {
1655 len = endPos - startPos - escapes;
1658 if ( escapes == 0 ) {
1659 val->bv_val = LDAP_STRNDUP( startPos, len );
1664 val->bv_val = LDAP_MALLOC( len + 1 );
1667 for ( s = d = 0; d < len; ) {
1668 if ( LDAP_DN_ESCAPE( str[ s ] ) ) {
1671 val->bv_val[ d++ ] = str[ s++ ];
1673 val->bv_val[ d ] = '\0';
1674 assert( strlen( val->bv_val ) == len );
1681 hexstr2bin( const char *str, char *c )
1691 if ( LDAP_DN_ASCII_DIGIT( c1 ) ) {
1695 if ( LDAP_DN_ASCII_UCASE_HEXALPHA( c1 ) ) {
1698 assert( LDAP_DN_ASCII_LCASE_HEXALPHA( c1 ) );
1705 if ( LDAP_DN_ASCII_DIGIT( c2 ) ) {
1709 if ( LDAP_DN_ASCII_UCASE_HEXALPHA( c2 ) ) {
1710 *c += c2 - 'A' + 10;
1712 assert( LDAP_DN_ASCII_LCASE_HEXALPHA( c2 ) );
1713 *c += c2 - 'a' + 10;
1721 hexstr2binval( const char *str, struct berval *val, const char **next, unsigned flags )
1723 const char *p, *startPos, *endPos = NULL;
1733 for ( startPos = p = str; p[ 0 ]; p += 2 ) {
1734 switch ( LDAP_DN_FORMAT( flags ) ) {
1735 case LDAP_DN_FORMAT_LDAPV3:
1736 if ( LDAP_DN_VALUE_END( p[ 0 ] ) ) {
1741 case LDAP_DN_FORMAT_LDAP:
1742 case LDAP_DN_FORMAT_LDAPV2:
1743 if ( LDAP_DN_VALUE_END_V2( p[ 0 ] ) ) {
1748 case LDAP_DN_FORMAT_DCE:
1749 if ( LDAP_DN_VALUE_END_DCE( p[ 0 ] ) ) {
1755 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1756 if ( flags & LDAP_DN_PEDANTIC ) {
1761 for ( ; p[ 0 ]; p++ ) {
1762 switch ( LDAP_DN_FORMAT( flags ) ) {
1763 case LDAP_DN_FORMAT_LDAPV3:
1764 if ( LDAP_DN_VALUE_END( p[ 0 ] ) ) {
1769 case LDAP_DN_FORMAT_LDAP:
1770 case LDAP_DN_FORMAT_LDAPV2:
1771 if ( LDAP_DN_VALUE_END_V2( p[ 0 ] ) ) {
1776 case LDAP_DN_FORMAT_DCE:
1777 if ( LDAP_DN_VALUE_END_DCE( p[ 0 ] ) ) {
1786 if ( !LDAP_DN_HEXPAIR( p ) ) {
1794 if ( flags & LDAP_DN_SKIP ) {
1798 len = ( ( endPos ? endPos : p ) - startPos ) / 2;
1800 assert( 2 * len == (ber_len_t) (( endPos ? endPos : p ) - startPos ));
1803 val->bv_val = LDAP_MALLOC( len + 1 );
1804 if ( val->bv_val == NULL ) {
1805 return( LDAP_NO_MEMORY );
1808 for ( s = 0, d = 0; d < len; s += 2, d++ ) {
1811 hexstr2bin( &startPos[ s ], &c );
1813 val->bv_val[ d ] = c;
1816 val->bv_val[ d ] = '\0';
1822 * convert a byte in a hexadecimal pair
1825 byte2hexpair( const char *val, char *pair )
1827 static const char hexdig[] = "0123456789ABCDEF";
1833 * we assume the string has enough room for the hex encoding
1837 pair[ 0 ] = hexdig[ 0x0f & ( val[ 0 ] >> 4 ) ];
1838 pair[ 1 ] = hexdig[ 0x0f & val[ 0 ] ];
1844 * convert a binary value in hexadecimal pairs
1847 binval2hexstr( struct berval *val, char *str )
1854 if ( val->bv_len == 0 ) {
1859 * we assume the string has enough room for the hex encoding
1863 for ( s = 0, d = 0; s < val->bv_len; s++, d += 2 ) {
1864 byte2hexpair( &val->bv_val[ s ], &str[ d ] );
1871 * Length of the string representation, accounting for escaped hex
1875 strval2strlen( struct berval *val, unsigned flags, ber_len_t *len )
1877 ber_len_t l, cl = 1;
1879 int escaped_byte_len = LDAP_DN_IS_PRETTY( flags ) ? 1 : 3;
1880 #ifdef PRETTY_ESCAPE
1881 int escaped_ascii_len = LDAP_DN_IS_PRETTY( flags ) ? 2 : 3;
1882 #endif /* PRETTY_ESCAPE */
1888 if ( val->bv_len == 0 ) {
1892 for ( l = 0, p = val->bv_val; p < val->bv_val + val->bv_len; p += cl ) {
1897 if ( p[ 0 ] == '\0' ) {
1903 cl = LDAP_UTF8_CHARLEN2( p, cl );
1905 /* illegal utf-8 char! */
1908 } else if ( cl > 1 ) {
1911 for ( cnt = 1; cnt < cl; cnt++ ) {
1912 if ( ( p[ cnt ] & 0xc0 ) != 0x80 ) {
1916 l += escaped_byte_len * cl;
1918 } else if ( LDAP_DN_NEEDESCAPE( p[ 0 ] )
1919 || ( p == val->bv_val && LDAP_DN_NEEDESCAPE_LEAD( p[ 0 ] ) )
1920 || ( !p[ 1 ] && LDAP_DN_NEEDESCAPE_TRAIL( p[ 0 ] ) ) ) {
1921 #ifdef PRETTY_ESCAPE
1923 if ( LDAP_DN_WILLESCAPE_HEX( flags, p[ 0 ] ) ) {
1925 if ( LDAP_DN_WILLESCAPE_CHAR( p[ 0 ] ) ) {
1929 * there might be some chars we want
1930 * to escape in form of a couple
1931 * of hexdigits for optimization purposes
1936 l += escaped_ascii_len;
1938 #else /* ! PRETTY_ESCAPE */
1940 #endif /* ! PRETTY_ESCAPE */
1953 * convert to string representation, escaping with hex the UTF-8 stuff;
1954 * assume the destination has enough room for escaping
1957 strval2str( struct berval *val, char *str, unsigned flags, ber_len_t *len )
1959 ber_len_t s, d, end;
1965 if ( val->bv_len == 0 ) {
1971 * we assume the string has enough room for the hex encoding
1974 for ( s = 0, d = 0, end = val->bv_len - 1; s < val->bv_len; ) {
1980 if ( val->bv_val[ s ] == '\0' ) {
1990 * The length was checked in strval2strlen();
1991 * LDAP_UTF8_CHARLEN() should suffice
1993 cl = LDAP_UTF8_CHARLEN2( &val->bv_val[ s ], cl );
1997 * there might be some chars we want to escape in form
1998 * of a couple of hexdigits for optimization purposes
2000 if ( ( cl > 1 && !LDAP_DN_IS_PRETTY( flags ) )
2001 #ifdef PRETTY_ESCAPE
2003 || LDAP_DN_WILLESCAPE_HEX( flags, val->bv_val[ s ] )
2005 || LDAP_DN_WILLESCAPE_CHAR( val->bv_val[ s ] )
2007 #else /* ! PRETTY_ESCAPE */
2008 || LDAP_DN_NEEDESCAPE( val->bv_val[ s ] )
2009 || ( d == 0 && LDAP_DN_NEEDESCAPE_LEAD( val->bv_val[ s ] ) )
2010 || ( s == end && LDAP_DN_NEEDESCAPE_TRAIL( val->bv_val[ s ] ) )
2012 #endif /* ! PRETTY_ESCAPE */
2016 byte2hexpair( &val->bv_val[ s ], &str[ d ] );
2021 } else if ( cl > 1 ) {
2023 str[ d++ ] = val->bv_val[ s++ ];
2027 #ifdef PRETTY_ESCAPE
2028 if ( LDAP_DN_NEEDESCAPE( val->bv_val[ s ] )
2029 || ( d == 0 && LDAP_DN_NEEDESCAPE_LEAD( val->bv_val[ s ] ) )
2030 || ( s == end && LDAP_DN_NEEDESCAPE_TRAIL( val->bv_val[ s ] ) ) ) {
2032 if ( !LDAP_DN_IS_PRETTY( flags ) ) {
2033 byte2hexpair( &val->bv_val[ s ], &str[ d ] );
2039 #endif /* PRETTY_ESCAPE */
2040 str[ d++ ] = val->bv_val[ s++ ];
2050 * Length of the IA5 string representation (no UTF-8 allowed)
2053 strval2IA5strlen( struct berval *val, unsigned flags, ber_len_t *len )
2062 if ( val->bv_len == 0 ) {
2066 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2068 * Turn value into a binary encoded BER
2073 for ( l = 0, p = val->bv_val; p[ 0 ]; p++ ) {
2074 if ( LDAP_DN_NEEDESCAPE( p[ 0 ] )
2075 || ( p == val->bv_val && LDAP_DN_NEEDESCAPE_LEAD( p[ 0 ] ) )
2076 || ( !p[ 1 ] && LDAP_DN_NEEDESCAPE_TRAIL( p[ 0 ] ) ) ) {
2091 * convert to string representation (np UTF-8)
2092 * assume the destination has enough room for escaping
2095 strval2IA5str( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2097 ber_len_t s, d, end;
2103 if ( val->bv_len == 0 ) {
2108 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2110 * Turn value into a binary encoded BER
2117 * we assume the string has enough room for the hex encoding
2121 for ( s = 0, d = 0, end = val->bv_len - 1; s < val->bv_len; ) {
2122 if ( LDAP_DN_NEEDESCAPE( val->bv_val[ s ] )
2123 || ( s == 0 && LDAP_DN_NEEDESCAPE_LEAD( val->bv_val[ s ] ) )
2124 || ( s == end && LDAP_DN_NEEDESCAPE_TRAIL( val->bv_val[ s ] ) ) ) {
2127 str[ d++ ] = val->bv_val[ s++ ];
2137 * Length of the (supposedly) DCE string representation,
2138 * accounting for escaped hex of UTF-8 chars
2141 strval2DCEstrlen( struct berval *val, unsigned flags, ber_len_t *len )
2150 if ( val->bv_len == 0 ) {
2154 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2156 * FIXME: Turn the value into a binary encoded BER?
2161 for ( l = 0, p = val->bv_val; p[ 0 ]; p++ ) {
2162 if ( LDAP_DN_NEEDESCAPE_DCE( p[ 0 ] ) ) {
2177 * convert to (supposedly) DCE string representation,
2178 * escaping with hex the UTF-8 stuff;
2179 * assume the destination has enough room for escaping
2182 strval2DCEstr( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2190 if ( val->bv_len == 0 ) {
2195 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2197 * FIXME: Turn the value into a binary encoded BER?
2205 * we assume the string has enough room for the hex encoding
2209 for ( s = 0, d = 0; s < val->bv_len; ) {
2210 if ( LDAP_DN_NEEDESCAPE_DCE( val->bv_val[ s ] ) ) {
2213 str[ d++ ] = val->bv_val[ s++ ];
2223 * Length of the (supposedly) AD canonical string representation,
2224 * accounting for escaped hex of UTF-8 chars
2227 strval2ADstrlen( struct berval *val, unsigned flags, ber_len_t *len )
2236 if ( val->bv_len == 0 ) {
2240 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2242 * FIXME: Turn the value into a binary encoded BER?
2247 for ( l = 0, p = val->bv_val; p[ 0 ]; p++ ) {
2248 if ( LDAP_DN_NEEDESCAPE_AD( p[ 0 ] ) ) {
2263 * convert to (supposedly) AD string representation,
2264 * escaping with hex the UTF-8 stuff;
2265 * assume the destination has enough room for escaping
2268 strval2ADstr( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2276 if ( val->bv_len == 0 ) {
2281 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2283 * FIXME: Turn the value into a binary encoded BER?
2291 * we assume the string has enough room for the hex encoding
2295 for ( s = 0, d = 0; s < val->bv_len; ) {
2296 if ( LDAP_DN_NEEDESCAPE_AD( val->bv_val[ s ] ) ) {
2299 str[ d++ ] = val->bv_val[ s++ ];
2309 * If the DN is terminated by single-AVA RDNs with attribute type of "dc",
2310 * the first part of the AD representation of the DN is written in DNS
2311 * form, i.e. dot separated domain name components (as suggested
2312 * by Luke Howard, http://www.padl.com/~lukeh)
2315 dn2domain( LDAPDN *dn, struct berval *bv, int pos, int *iRDN )
2318 int domain = 0, first = 1;
2319 ber_len_t l = 1; /* we move the null also */
2322 /* we are guaranteed there's enough memory in str */
2328 assert( *iRDN >= 0 );
2330 str = bv->bv_val + pos;
2332 for ( i = *iRDN; i >= 0; i-- ) {
2336 assert( dn[ 0 ][ i ] );
2339 assert( rdn[ 0 ][ 0 ] );
2340 ava = rdn[ 0 ][ 0 ];
2342 if ( !LDAP_DN_IS_RDN_DC( rdn ) ) {
2350 AC_MEMCPY( str, ava->la_value.bv_val,
2351 ava->la_value.bv_len + 1);
2352 l += ava->la_value.bv_len;
2355 AC_MEMCPY( str + ava->la_value.bv_len + 1, bv->bv_val + pos, l);
2356 AC_MEMCPY( str, ava->la_value.bv_val,
2357 ava->la_value.bv_len );
2358 str[ ava->la_value.bv_len ] = '.';
2359 l += ava->la_value.bv_len + 1;
2364 bv->bv_len = pos + l - 1;
2370 rdn2strlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len,
2371 int ( *s2l )( struct berval *v, unsigned f, ber_len_t *l ) )
2378 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
2379 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
2381 /* len(type) + '=' + '+' | ',' */
2382 l += ava->la_attr.bv_len + 2;
2384 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2385 /* octothorpe + twice the length */
2386 l += 1 + 2 * ava->la_value.bv_len;
2390 unsigned f = flags | ava->la_flags;
2392 if ( ( *s2l )( &ava->la_value, f, &vl ) ) {
2405 rdn2str( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len,
2406 int ( *s2s ) ( struct berval *v, char * s, unsigned f, ber_len_t *l ) )
2411 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
2412 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
2414 AC_MEMCPY( &str[ l ], ava->la_attr.bv_val,
2415 ava->la_attr.bv_len );
2416 l += ava->la_attr.bv_len;
2420 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2422 if ( binval2hexstr( &ava->la_value, &str[ l ] ) ) {
2425 l += 2 * ava->la_value.bv_len;
2429 unsigned f = flags | ava->la_flags;
2431 if ( ( *s2s )( &ava->la_value, &str[ l ], f, &vl ) ) {
2436 str[ l++ ] = ( rdn[ 0 ][ iAVA + 1 ] ? '+' : ',' );
2445 rdn2DCEstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len )
2452 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
2453 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
2455 /* len(type) + '=' + ',' | '/' */
2456 l += ava->la_attr.bv_len + 2;
2458 switch ( ava->la_flags ) {
2459 case LDAP_AVA_BINARY:
2460 /* octothorpe + twice the length */
2461 l += 1 + 2 * ava->la_value.bv_len;
2464 case LDAP_AVA_STRING: {
2466 unsigned f = flags | ava->la_flags;
2468 if ( strval2DCEstrlen( &ava->la_value, f, &vl ) ) {
2486 rdn2DCEstr( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len, int first )
2491 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
2492 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
2497 str[ l++ ] = ( iAVA ? ',' : '/' );
2500 AC_MEMCPY( &str[ l ], ava->la_attr.bv_val,
2501 ava->la_attr.bv_len );
2502 l += ava->la_attr.bv_len;
2506 switch ( ava->la_flags ) {
2507 case LDAP_AVA_BINARY:
2509 if ( binval2hexstr( &ava->la_value, &str[ l ] ) ) {
2512 l += 2 * ava->la_value.bv_len;
2515 case LDAP_AVA_STRING: {
2517 unsigned f = flags | ava->la_flags;
2519 if ( strval2DCEstr( &ava->la_value, &str[ l ], f, &vl ) ) {
2537 rdn2UFNstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len )
2547 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
2548 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
2551 l += ( rdn[ 0 ][ iAVA + 1 ] ? 3 : 2 );
2553 /* FIXME: are binary values allowed in UFN? */
2554 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2555 /* octothorpe + twice the value */
2556 l += 1 + 2 * ava->la_value.bv_len;
2560 unsigned f = flags | ava->la_flags;
2562 if ( strval2strlen( &ava->la_value, f, &vl ) ) {
2575 rdn2UFNstr( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len )
2580 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
2581 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
2583 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2585 if ( binval2hexstr( &ava->la_value, &str[ l ] ) ) {
2588 l += 2 * ava->la_value.bv_len;
2592 unsigned f = flags | ava->la_flags;
2594 if ( strval2str( &ava->la_value, &str[ l ], f, &vl ) ) {
2600 if ( rdn[ 0 ][ iAVA + 1 ]) {
2601 AC_MEMCPY( &str[ l ], " + ", 3 );
2605 AC_MEMCPY( &str[ l ], ", ", 2 );
2616 rdn2ADstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len )
2626 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
2627 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
2632 /* FIXME: are binary values allowed in UFN? */
2633 switch ( ava->la_flags ) {
2634 case LDAP_AVA_BINARY:
2635 /* octothorpe + twice the value */
2636 l += 1 + 2 * ava->la_value.bv_len;
2639 case LDAP_AVA_STRING: {
2641 unsigned f = flags | ava->la_flags;
2643 if ( strval2ADstrlen( &ava->la_value, f, &vl ) ) {
2661 rdn2ADstr( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len, int first )
2666 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
2667 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
2672 str[ l++ ] = ( iAVA ? ',' : '/' );
2675 switch ( ava->la_flags ) {
2676 case LDAP_AVA_BINARY:
2678 if ( binval2hexstr( &ava->la_value, &str[ l ] ) ) {
2681 l += 2 * ava->la_value.bv_len;
2684 case LDAP_AVA_STRING: {
2686 unsigned f = flags | ava->la_flags;
2688 if ( strval2ADstr( &ava->la_value, &str[ l ], f, &vl ) ) {
2708 * Returns in str a string representation of rdn based on flags.
2709 * There is some duplication of code between this and ldap_dn2str;
2710 * this is wanted to reduce the allocation of temporary buffers.
2713 ldap_rdn2str( LDAPRDN *rdn, char **str, unsigned flags )
2720 if((flags & LDAP_DN_FORMAT_MASK) == LDAP_DN_FORMAT_LBER) {
2721 return LDAP_PARAM_ERROR;
2724 rc = ldap_rdn2bv( rdn, &bv, flags );
2730 ldap_rdn2bv( LDAPRDN *rdn, struct berval *bv, unsigned flags )
2740 if ( rdn == NULL ) {
2741 bv->bv_val = LDAP_STRDUP( "" );
2742 return( LDAP_SUCCESS );
2746 * This routine wastes "back" bytes at the end of the string
2749 switch ( LDAP_DN_FORMAT( flags ) ) {
2750 case LDAP_DN_FORMAT_LDAPV3:
2751 if ( rdn2strlen( rdn, flags, &l, strval2strlen ) ) {
2752 return LDAP_DECODING_ERROR;
2756 case LDAP_DN_FORMAT_LDAPV2:
2757 if ( rdn2strlen( rdn, flags, &l, strval2IA5strlen ) ) {
2758 return LDAP_DECODING_ERROR;
2762 case LDAP_DN_FORMAT_UFN:
2763 if ( rdn2UFNstrlen( rdn, flags, &l ) ) {
2764 return LDAP_DECODING_ERROR;
2768 case LDAP_DN_FORMAT_DCE:
2769 if ( rdn2DCEstrlen( rdn, flags, &l ) ) {
2770 return LDAP_DECODING_ERROR;
2774 case LDAP_DN_FORMAT_AD_CANONICAL:
2775 if ( rdn2ADstrlen( rdn, flags, &l ) ) {
2776 return LDAP_DECODING_ERROR;
2781 return( LDAP_PARAM_ERROR );
2784 bv->bv_val = LDAP_MALLOC( l + 1 );
2786 switch ( LDAP_DN_FORMAT( flags ) ) {
2787 case LDAP_DN_FORMAT_LDAPV3:
2788 rc = rdn2str( rdn, bv->bv_val, flags, &l, strval2str );
2792 case LDAP_DN_FORMAT_LDAPV2:
2793 rc = rdn2str( rdn, bv->bv_val, flags, &l, strval2IA5str );
2797 case LDAP_DN_FORMAT_UFN:
2798 rc = rdn2UFNstr( rdn, bv->bv_val, flags, &l );
2802 case LDAP_DN_FORMAT_DCE:
2803 rc = rdn2DCEstr( rdn, bv->bv_val, flags, &l, 1 );
2807 case LDAP_DN_FORMAT_AD_CANONICAL:
2808 rc = rdn2ADstr( rdn, bv->bv_val, flags, &l, 1 );
2813 /* need at least one of the previous */
2814 return LDAP_PARAM_ERROR;
2818 ldap_memfree( bv->bv_val );
2822 bv->bv_len = l - back;
2823 bv->bv_val[ bv->bv_len ] = '\0';
2825 return LDAP_SUCCESS;
2829 * Very bulk implementation; many optimizations can be performed
2830 * - a NULL dn results in an empty string ""
2833 * a) what do we do if a UTF-8 string must be converted in LDAPv2?
2834 * we must encode it in binary form ('#' + HEXPAIRs)
2835 * b) does DCE/AD support UTF-8?
2836 * no clue; don't think so.
2837 * c) what do we do when binary values must be converted in UTF/DCE/AD?
2838 * use binary encoded BER
2840 int ldap_dn2str( LDAPDN *dn, char **str, unsigned flags )
2847 if((flags & LDAP_DN_FORMAT_MASK) == LDAP_DN_FORMAT_LBER) {
2848 return LDAP_PARAM_ERROR;
2851 rc = ldap_dn2bv( dn, &bv, flags );
2856 int ldap_dn2bv( LDAPDN *dn, struct berval *bv, unsigned flags )
2859 int rc = LDAP_ENCODING_ERROR;
2862 /* stringifying helpers for LDAPv3/LDAPv2 */
2863 int ( *sv2l ) ( struct berval *v, unsigned f, ber_len_t *l );
2864 int ( *sv2s ) ( struct berval *v, char *s, unsigned f, ber_len_t *l );
2871 Debug( LDAP_DEBUG_TRACE, "=> ldap_dn2bv(%u)\n%s%s", flags, "", "" );
2874 * a null dn means an empty dn string
2875 * FIXME: better raise an error?
2878 bv->bv_val = LDAP_STRDUP( "" );
2879 return( LDAP_SUCCESS );
2882 switch ( LDAP_DN_FORMAT( flags ) ) {
2883 case LDAP_DN_FORMAT_LDAPV3:
2884 sv2l = strval2strlen;
2888 case LDAP_DN_FORMAT_LDAPV2:
2889 sv2l = strval2IA5strlen;
2890 sv2s = strval2IA5str;
2893 for ( iRDN = 0, len = 0; dn[ 0 ][ iRDN ]; iRDN++ ) {
2895 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
2897 if ( rdn2strlen( rdn, flags, &rdnl, sv2l ) ) {
2898 goto return_results;
2904 if ( ( bv->bv_val = LDAP_MALLOC( len + 1 ) ) == NULL ) {
2905 rc = LDAP_NO_MEMORY;
2909 for ( l = 0, iRDN = 0; dn[ 0 ][ iRDN ]; iRDN++ ) {
2911 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
2913 if ( rdn2str( rdn, &bv->bv_val[ l ], flags,
2915 LDAP_FREE( bv->bv_val );
2917 goto return_results;
2925 * trim the last ',' (the allocated memory
2926 * is one byte longer than required)
2928 bv->bv_len = len - 1;
2929 bv->bv_val[ bv->bv_len ] = '\0';
2934 case LDAP_DN_FORMAT_UFN: {
2937 * FIXME: quoting from RFC 1781:
2939 To take a distinguished name, and generate a name of this format with
2940 attribute types omitted, the following steps are followed.
2942 1. If the first attribute is of type CommonName, the type may be
2945 2. If the last attribute is of type Country, the type may be
2948 3. If the last attribute is of type Country, the last
2949 Organisation attribute may have the type omitted.
2951 4. All attributes of type OrganisationalUnit may have the type
2952 omitted, unless they are after an Organisation attribute or
2953 the first attribute is of type OrganisationalUnit.
2955 * this should be the pedantic implementation.
2957 * Here the standard implementation reflects
2958 * the one historically provided by OpenLDAP
2959 * (and UMIch, I presume), with the variant
2960 * of spaces and plusses (' + ') separating
2963 * A non-standard but nice implementation could
2964 * be to turn the final "dc" attributes into a
2965 * dot-separated domain.
2967 * Other improvements could involve the use of
2968 * friendly country names and so.
2971 int leftmost_dc = -1;
2973 #endif /* DC_IN_UFN */
2975 for ( iRDN = 0, len = 0; dn[ 0 ][ iRDN ]; iRDN++ ) {
2977 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
2979 if ( rdn2UFNstrlen( rdn, flags, &rdnl ) ) {
2980 goto return_results;
2985 if ( LDAP_DN_IS_RDN_DC( rdn ) ) {
2986 if ( leftmost_dc == -1 ) {
2992 #endif /* DC_IN_UFN */
2995 if ( ( bv->bv_val = LDAP_MALLOC( len + 1 ) ) == NULL ) {
2996 rc = LDAP_NO_MEMORY;
3001 if ( leftmost_dc == -1 ) {
3002 #endif /* DC_IN_UFN */
3003 for ( l = 0, iRDN = 0; dn[ 0 ][ iRDN ]; iRDN++ ) {
3005 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
3007 if ( rdn2UFNstr( rdn, &bv->bv_val[ l ],
3009 LDAP_FREE( bv->bv_val );
3011 goto return_results;
3017 * trim the last ', ' (the allocated memory
3018 * is two bytes longer than required)
3020 bv->bv_len = len - 2;
3021 bv->bv_val[ bv->bv_len ] = '\0';
3024 last_iRDN = iRDN - 1;
3026 for ( l = 0, iRDN = 0; iRDN < leftmost_dc; iRDN++ ) {
3028 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
3030 if ( rdn2UFNstr( rdn, &bv->bv_val[ l ],
3032 LDAP_FREE( bv->bv_val );
3034 goto return_results;
3039 if ( !dn2domain( dn, bv, l, &last_iRDN ) ) {
3040 LDAP_FREE( bv->bv_val );
3042 goto return_results;
3045 /* the string is correctly terminated by dn2domain */
3047 #endif /* DC_IN_UFN */
3053 case LDAP_DN_FORMAT_DCE:
3055 for ( iRDN = 0, len = 0; dn[ 0 ][ iRDN ]; iRDN++ ) {
3057 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
3059 if ( rdn2DCEstrlen( rdn, flags, &rdnl ) ) {
3060 goto return_results;
3066 if ( ( bv->bv_val = LDAP_MALLOC( len + 1 ) ) == NULL ) {
3067 rc = LDAP_NO_MEMORY;
3071 for ( l = 0; iRDN--; ) {
3073 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
3075 if ( rdn2DCEstr( rdn, &bv->bv_val[ l ], flags,
3077 LDAP_FREE( bv->bv_val );
3079 goto return_results;
3087 bv->bv_val[ bv->bv_len ] = '\0';
3092 case LDAP_DN_FORMAT_AD_CANONICAL: {
3095 * Sort of UFN for DCE DNs: a slash ('/') separated
3096 * global->local DN with no types; strictly speaking,
3097 * the naming context should be a domain, which is
3098 * written in DNS-style, e.g. dot-deparated.
3102 * "givenName=Bill+sn=Gates,ou=People,dc=microsoft,dc=com"
3106 * "microsoft.com/People/Bill,Gates"
3108 for ( iRDN = 0, len = -1; dn[ 0 ][ iRDN ]; iRDN++ ) {
3110 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
3112 if ( rdn2ADstrlen( rdn, flags, &rdnl ) ) {
3113 goto return_results;
3119 if ( ( bv->bv_val = LDAP_MALLOC( len + 1 ) ) == NULL ) {
3120 rc = LDAP_NO_MEMORY;
3125 if ( iRDN && dn2domain( dn, bv, 0, &iRDN ) ) {
3126 for ( l = bv->bv_len; iRDN >= 0 ; iRDN-- ) {
3128 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
3130 if ( rdn2ADstr( rdn, &bv->bv_val[ l ],
3131 flags, &rdnl, 0 ) ) {
3132 LDAP_FREE( bv->bv_val );
3134 goto return_results;
3143 * Strictly speaking, AD canonical requires
3144 * a DN to be in the form "..., dc=smtg",
3145 * i.e. terminated by a domain component
3147 if ( flags & LDAP_DN_PEDANTIC ) {
3148 LDAP_FREE( bv->bv_val );
3150 rc = LDAP_ENCODING_ERROR;
3154 for ( l = 0; iRDN >= 0 ; iRDN-- ) {
3156 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
3158 if ( rdn2ADstr( rdn, &bv->bv_val[ l ],
3159 flags, &rdnl, first ) ) {
3160 LDAP_FREE( bv->bv_val );
3162 goto return_results;
3172 bv->bv_val[ bv->bv_len ] = '\0';
3179 return LDAP_PARAM_ERROR;
3182 Debug( LDAP_DEBUG_TRACE, "<= ldap_dn2bv(%s,%u)=%d\n",
3183 bv->bv_val, flags, rc );