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" */
28 /* #define PRETTY_ESCAPE */
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.
592 #define TMP_SLOTS 1024
595 ldap_str2dn( LDAP_CONST char *str, LDAPDN **dn, unsigned flags )
598 int rc = LDAP_DECODING_ERROR;
601 LDAPDN *newDN = NULL;
602 LDAPRDN *newRDN = NULL, *tmpDN[TMP_SLOTS];
607 Debug( LDAP_DEBUG_TRACE, "=> ldap_str2dn(%s,%u)\n%s", str, flags, "" );
611 switch ( LDAP_DN_FORMAT( flags ) ) {
612 case LDAP_DN_FORMAT_LDAP:
613 case LDAP_DN_FORMAT_LDAPV3:
614 case LDAP_DN_FORMAT_LDAPV2:
615 case LDAP_DN_FORMAT_DCE:
618 /* unsupported in str2dn */
619 case LDAP_DN_FORMAT_UFN:
620 case LDAP_DN_FORMAT_AD_CANONICAL:
621 return LDAP_PARAM_ERROR;
623 case LDAP_DN_FORMAT_LBER:
625 return LDAP_PARAM_ERROR;
628 if ( str[ 0 ] == '\0' ) {
633 if ( LDAP_DN_DCE( flags ) ) {
636 * (from Luke Howard: thnx) A RDN separator is required
637 * at the beginning of an (absolute) DN.
639 if ( !LDAP_DN_RDN_SEP_DCE( p[ 0 ] ) ) {
644 } else if ( LDAP_DN_LDAP( flags ) ) {
646 * if dn starts with '/' let's make it a DCE dn
648 if ( LDAP_DN_RDN_SEP_DCE( p[ 0 ] ) ) {
649 flags |= LDAP_DN_FORMAT_DCE;
654 for ( ; p[ 0 ]; p++ ) {
657 err = ldap_str2rdn( p, &newRDN, (char **) &p, flags );
658 if ( err != LDAP_SUCCESS ) {
663 * We expect a rdn separator
666 switch ( LDAP_DN_FORMAT( flags ) ) {
667 case LDAP_DN_FORMAT_LDAPV3:
668 if ( !LDAP_DN_RDN_SEP( p[ 0 ] ) ) {
669 rc = LDAP_DECODING_ERROR;
674 case LDAP_DN_FORMAT_LDAP:
675 case LDAP_DN_FORMAT_LDAPV2:
676 if ( !LDAP_DN_RDN_SEP_V2( p[ 0 ] ) ) {
677 rc = LDAP_DECODING_ERROR;
682 case LDAP_DN_FORMAT_DCE:
683 if ( !LDAP_DN_RDN_SEP_DCE( p[ 0 ] ) ) {
684 rc = LDAP_DECODING_ERROR;
692 tmpDN[nrdns++] = newRDN;
699 assert (nrdns < TMP_SLOTS);
702 * make the static AVA array dynamically rescalable
704 if (nrdns >= TMP_SLOTS) {
705 rc = LDAP_DECODING_ERROR;
710 if ( p[ 0 ] == '\0' ) {
712 * the DN is over, phew
714 newDN = (LDAPDN *)LDAP_MALLOC( sizeof(LDAPDN) +
715 sizeof(LDAPRDN *) * (nrdns+1));
716 if ( newDN == NULL ) {
722 newDN[0] = (LDAPRDN **)(newDN+1);
724 if ( LDAP_DN_DCE( flags ) ) {
725 /* add in reversed order */
726 for ( i=0; i<nrdns; i++ )
727 newDN[0][i] = tmpDN[nrdns-1-i];
729 for ( i=0; i<nrdns; i++ )
730 newDN[0][i] = tmpDN[i];
732 newDN[0][nrdns] = NULL;
741 ldap_rdnfree( newRDN );
744 for (nrdns-- ;nrdns>=0; nrdns-- )
745 ldap_rdnfree( tmpDN[nrdns] );
748 Debug( LDAP_DEBUG_TRACE, "<= ldap_str2dn(%s,%u)=%d\n", str, flags, rc );
757 * Parses a relative DN according to flags up to a rdn separator
758 * or to the end of str.
759 * Returns the rdn and a pointer to the string continuation, which
760 * corresponds to the rdn separator or to '\0' in case the string is over.
763 ldap_str2rdn( LDAP_CONST char *str, LDAPRDN **rdn,
764 char **n_in, unsigned flags )
766 const char **n = (const char **) n_in;
770 int rc = LDAP_DECODING_ERROR;
771 int attrTypeEncoding = LDAP_AVA_STRING,
772 attrValueEncoding = LDAP_AVA_STRING;
774 struct berval attrType = { 0, NULL };
775 struct berval attrValue = { 0, NULL };
777 LDAPRDN *newRDN = NULL;
778 LDAPAVA *tmpRDN[TMP_SLOTS];
781 assert( rdn || flags & LDAP_DN_SKIP );
784 Debug( LDAP_DEBUG_TRACE, "=> ldap_str2rdn(%s,%u)\n%s", str, flags, "" );
791 switch ( LDAP_DN_FORMAT( flags ) ) {
792 case LDAP_DN_FORMAT_LDAP:
793 case LDAP_DN_FORMAT_LDAPV3:
794 case LDAP_DN_FORMAT_LDAPV2:
795 case LDAP_DN_FORMAT_DCE:
798 /* unsupported in str2dn */
799 case LDAP_DN_FORMAT_UFN:
800 case LDAP_DN_FORMAT_AD_CANONICAL:
801 return LDAP_PARAM_ERROR;
803 case LDAP_DN_FORMAT_LBER:
805 return LDAP_PARAM_ERROR;
808 if ( str[ 0 ] == '\0' ) {
813 for ( ; p[ 0 ] || state == GOTAVA; ) {
816 * The parser in principle advances one token a time,
817 * or toggles state if preferable.
822 * an AttributeType can be encoded as:
823 * - its string representation; in detail, implementations
824 * MUST recognize AttributeType string type names listed
825 * in section 2.3 of draft-ietf-ldapbis-dn-XX.txt, and
826 * MAY recognize other names.
827 * - its numeric OID (a dotted decimal string); in detail
828 * RFC 2253 asserts that ``Implementations MUST allow
829 * an oid in the attribute type to be prefixed by one
830 * of the character strings "oid." or "OID."''. As soon
831 * as draft-ietf-ldapbis-dn-XX.txt obsoletes RFC 2253
832 * I'm not sure whether this is required or not any
833 * longer; to be liberal, we still implement it.
836 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
837 if ( !LDAP_DN_ALLOW_ONE_SPACE( flags ) ) {
844 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
845 if ( !LDAP_DN_ALLOW_SPACES( flags ) ) {
850 /* whitespace is allowed (and trimmed) */
852 while ( p[ 0 ] && LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
857 /* error: we expected an AVA */
863 if ( LDAP_DN_OID_LEADCHAR( p[ 0 ] ) ) {
864 state = B4OIDATTRTYPE;
868 /* else must be alpha */
869 if ( !LDAP_DN_DESC_LEADCHAR( p[ 0 ] ) ) {
873 /* LDAPv2 "oid." prefix */
874 if ( LDAP_DN_LDAPV2( flags ) ) {
876 * to be overly pedantic, we only accept
879 if ( flags & LDAP_DN_PEDANTIC ) {
880 if ( !strncmp( p, "OID.", 4 )
881 || !strncmp( p, "oid.", 4 ) ) {
883 state = B4OIDATTRTYPE;
887 if ( !strncasecmp( p, "oid.", 4 ) ) {
889 state = B4OIDATTRTYPE;
895 state = B4STRINGATTRTYPE;
898 case B4OIDATTRTYPE: {
899 int err = LDAP_SUCCESS;
901 attrType.bv_val = ldap_int_parse_numericoid( &p, &err,
904 if ( err != LDAP_SUCCESS ) {
907 attrType.bv_len = p - attrType.bv_val;
909 attrTypeEncoding = LDAP_AVA_BINARY;
915 case B4STRINGATTRTYPE: {
916 const char *startPos, *endPos = NULL;
920 * the starting char has been found to be
921 * a LDAP_DN_DESC_LEADCHAR so we don't re-check it
922 * FIXME: DCE attr types seem to have a more
923 * restrictive syntax (no '-' ...)
925 for ( startPos = p++; p[ 0 ]; p++ ) {
926 if ( LDAP_DN_DESC_CHAR( p[ 0 ] ) ) {
930 if ( LDAP_DN_LANG_SEP( p[ 0 ] ) ) {
933 * RFC 2253 does not explicitly
934 * allow lang extensions to attribute
937 if ( flags & LDAP_DN_PEDANTIC ) {
942 * we trim ';' and following lang
943 * and so from attribute types
946 for ( ; LDAP_DN_ATTRDESC_CHAR( p[ 0 ] )
947 || LDAP_DN_LANG_SEP( p[ 0 ] ); p++ ) {
955 len = ( endPos ? endPos : p ) - startPos;
960 attrTypeEncoding = LDAP_AVA_STRING;
963 * here we need to decide whether to use it as is
964 * or turn it in OID form; as a consequence, we
965 * need to decide whether to binary encode the value
970 if ( flags & LDAP_DN_SKIP ) {
974 attrType.bv_val = (char *)startPos;
975 attrType.bv_len = len;
981 /* spaces may not be allowed */
982 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
983 if ( !LDAP_DN_ALLOW_SPACES( flags ) ) {
988 for ( p++; LDAP_DN_ASCII_SPACE( p[ 0 ] ); p++ ) {
993 /* need equal sign */
994 if ( !LDAP_DN_AVA_EQUALS( p[ 0 ] ) ) {
999 /* spaces may not be allowed */
1000 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1001 if ( !LDAP_DN_ALLOW_SPACES( flags ) ) {
1006 for ( p++; LDAP_DN_ASCII_SPACE( p[ 0 ] ); p++ ) {
1012 * octothorpe means a BER encoded value will follow
1013 * FIXME: I don't think DCE will allow it
1015 if ( LDAP_DN_OCTOTHORPE( p[ 0 ] ) ) {
1017 attrValueEncoding = LDAP_AVA_BINARY;
1018 state = B4BINARYVALUE;
1022 /* STRING value expected */
1025 * if we're pedantic, an attribute type in OID form
1026 * SHOULD imply a BER encoded attribute value; we
1027 * should at least issue a warning
1029 if ( ( flags & LDAP_DN_PEDANTIC )
1030 && ( attrTypeEncoding == LDAP_AVA_BINARY ) ) {
1031 /* OID attrType SHOULD use binary encoding */
1035 attrValueEncoding = LDAP_AVA_STRING;
1038 * LDAPv2 allows the attribute value to be quoted;
1039 * also, IA5 values are expected, in principle
1041 if ( LDAP_DN_LDAPV2( flags ) || LDAP_DN_LDAP( flags ) ) {
1042 if ( LDAP_DN_QUOTES( p[ 0 ] ) ) {
1044 state = B4IA5VALUEQUOTED;
1048 if ( LDAP_DN_LDAPV2( flags ) ) {
1055 * here STRING means RFC 2253 string
1056 * FIXME: what about DCE strings?
1062 state = B4STRINGVALUE;
1067 if ( hexstr2binval( p, &attrValue, &p, flags ) ) {
1075 switch ( LDAP_DN_FORMAT( flags ) ) {
1076 case LDAP_DN_FORMAT_LDAP:
1077 case LDAP_DN_FORMAT_LDAPV3:
1078 if ( str2strval( p, &attrValue, &p, flags,
1079 &attrValueEncoding ) ) {
1084 case LDAP_DN_FORMAT_DCE:
1085 if ( DCE2strval( p, &attrValue, &p, flags ) ) {
1098 if ( IA52strval( p, &attrValue, &p, flags ) ) {
1105 case B4IA5VALUEQUOTED:
1107 /* lead quote already stripped */
1108 if ( quotedIA52strval( p, &attrValue,
1119 if ( !( flags & LDAP_DN_SKIP ) ) {
1123 * we accept empty values
1125 ava = ldapava_new( &attrType, &attrValue,
1126 attrValueEncoding );
1128 if ( ava == NULL ) {
1129 rc = LDAP_NO_MEMORY;
1132 tmpRDN[navas++] = ava;
1134 attrValue.bv_val = NULL;
1135 attrValue.bv_len = 0;
1137 assert(navas < TMP_SLOTS);
1141 * if we got an AVA separator ('+', or ',' for DCE )
1142 * we expect a new AVA for this RDN; otherwise
1143 * we add the RDN to the DN
1145 switch ( LDAP_DN_FORMAT( flags ) ) {
1146 case LDAP_DN_FORMAT_LDAP:
1147 case LDAP_DN_FORMAT_LDAPV3:
1148 case LDAP_DN_FORMAT_LDAPV2:
1149 if ( !LDAP_DN_AVA_SEP( p[ 0 ] ) ) {
1154 case LDAP_DN_FORMAT_DCE:
1155 if ( !LDAP_DN_AVA_SEP_DCE( p[ 0 ] ) ) {
1163 * the RDN is over, phew
1166 if ( !( flags & LDAP_DN_SKIP ) ) {
1167 newRDN = (LDAPRDN *)LDAP_MALLOC( sizeof(LDAPRDN)
1168 + sizeof(LDAPAVA *) * (navas+1) );
1169 if ( newRDN == NULL ) {
1170 rc = LDAP_NO_MEMORY;
1175 newRDN[0] = (LDAPAVA**)(newRDN+1);
1177 for (i=0; i<navas; i++)
1178 newRDN[0][i] = tmpRDN[i];
1179 newRDN[0][i] = NULL;
1187 /* they should have been used in an AVA */
1188 attrType.bv_val = NULL;
1189 attrValue.bv_val = NULL;
1204 /* They are set to NULL after they're used in an AVA */
1206 if ( attrValue.bv_val ) {
1207 free( attrValue.bv_val );
1210 for (navas-- ; navas>=0; navas-- )
1211 ldap_avafree( tmpRDN[navas] );
1215 Debug( LDAP_DEBUG_TRACE, "<= ldap_str2rdn(%*s)=%d\n",
1225 * reads in a UTF-8 string value, unescaping stuff:
1226 * '\' + LDAP_DN_NEEDESCAPE(c) -> 'c'
1227 * '\' + HEXPAIR(p) -> unhex(p)
1230 str2strval( const char *str, struct berval *val, const char **next, unsigned flags, unsigned *retFlags )
1232 const char *p, *startPos, *endPos = NULL;
1233 ber_len_t len, escapes;
1241 for ( startPos = p = str, escapes = 0; p[ 0 ]; p++ ) {
1242 if ( LDAP_DN_ESCAPE( p[ 0 ] ) ) {
1244 if ( p[ 0 ] == '\0' ) {
1247 if ( LDAP_DN_MAYESCAPE( p[ 0 ] ) ) {
1252 if ( LDAP_DN_HEXPAIR( p ) ) {
1255 hexstr2bin( p, &c );
1259 /* do not accept zero, right? */
1263 if ( !LDAP_DN_ASCII_PRINTABLE( c ) ) {
1266 * we assume the string is UTF-8
1268 *retFlags = LDAP_AVA_NONPRINTABLE;
1275 if ( LDAP_DN_PEDANTIC & flags ) {
1279 * we do not allow escaping
1280 * of chars that don't need
1281 * to and do not belong to
1286 } else if (!LDAP_DN_ASCII_PRINTABLE( p[ 0 ] ) ) {
1287 *retFlags = LDAP_AVA_NONPRINTABLE;
1289 } else if ( ( LDAP_DN_LDAP( flags ) && LDAP_DN_VALUE_END_V2( p[ 0 ] ) )
1290 || ( LDAP_DN_LDAPV3( flags ) && LDAP_DN_VALUE_END( p[ 0 ] ) ) ) {
1293 } else if ( LDAP_DN_NEEDESCAPE( p[ 0 ] ) ) {
1295 * FIXME: maybe we can add
1296 * escapes if not pedantic?
1303 * we do allow unescaped spaces at the end
1304 * of the value only in non-pedantic mode
1306 if ( p > startPos + 1 && LDAP_DN_ASCII_SPACE( p[ -1 ] ) &&
1307 !LDAP_DN_ESCAPE( p[ -2 ] ) ) {
1308 if ( flags & LDAP_DN_PEDANTIC ) {
1312 /* strip trailing (unescaped) spaces */
1313 for ( endPos = p - 1;
1314 endPos > startPos + 1 &&
1315 LDAP_DN_ASCII_SPACE( endPos[ -1 ] ) &&
1316 !LDAP_DN_ESCAPE( endPos[ -2 ] );
1323 if ( flags & LDAP_DN_SKIP ) {
1328 * FIXME: test memory?
1330 len = ( endPos ? endPos : p ) - startPos - escapes;
1333 if ( escapes == 0 ) {
1334 val->bv_val = LDAP_STRNDUP( startPos, len );
1339 val->bv_val = LDAP_MALLOC( len + 1 );
1340 for ( s = 0, d = 0; d < len; ) {
1341 if ( LDAP_DN_ESCAPE( startPos[ s ] ) ) {
1343 if ( LDAP_DN_MAYESCAPE( startPos[ s ] ) ) {
1344 val->bv_val[ d++ ] =
1347 } else if ( LDAP_DN_HEXPAIR( &startPos[ s ] ) ) {
1350 hexstr2bin( &startPos[ s ], &c );
1351 val->bv_val[ d++ ] = c;
1355 /* we should never get here */
1360 val->bv_val[ d++ ] = startPos[ s++ ];
1364 val->bv_val[ d ] = '\0';
1365 assert( strlen( val->bv_val ) == len );
1372 DCE2strval( const char *str, struct berval *val, const char **next, unsigned flags )
1374 const char *p, *startPos, *endPos = NULL;
1375 ber_len_t len, escapes;
1383 for ( startPos = p = str, escapes = 0; p[ 0 ]; p++ ) {
1384 if ( LDAP_DN_ESCAPE_DCE( p[ 0 ] ) ) {
1386 if ( LDAP_DN_NEEDESCAPE_DCE( p[ 0 ] ) ) {
1393 } else if ( LDAP_DN_VALUE_END_DCE( p[ 0 ] ) ) {
1398 * FIXME: can we accept anything else? I guess we need
1399 * to stop if a value is not legal
1404 * (unescaped) trailing spaces are trimmed must be silently ignored;
1407 if ( p > startPos + 1 && LDAP_DN_ASCII_SPACE( p[ -1 ] ) &&
1408 !LDAP_DN_ESCAPE( p[ -2 ] ) ) {
1409 if ( flags & LDAP_DN_PEDANTIC ) {
1413 /* strip trailing (unescaped) spaces */
1414 for ( endPos = p - 1;
1415 endPos > startPos + 1 &&
1416 LDAP_DN_ASCII_SPACE( endPos[ -1 ] ) &&
1417 !LDAP_DN_ESCAPE( endPos[ -2 ] );
1424 if ( flags & LDAP_DN_SKIP ) {
1428 len = ( endPos ? endPos : p ) - startPos - escapes;
1430 if ( escapes == 0 ){
1431 val->bv_val = LDAP_STRNDUP( startPos, len );
1436 val->bv_val = LDAP_MALLOC( len + 1 );
1437 for ( s = 0, d = 0; d < len; ) {
1439 * This point is reached only if escapes
1440 * are properly used, so all we need to
1443 if ( LDAP_DN_ESCAPE_DCE( startPos[ s ] ) ) {
1447 val->bv_val[ d++ ] = startPos[ s++ ];
1449 val->bv_val[ d ] = '\0';
1450 assert( strlen( val->bv_val ) == len );
1457 IA52strval( const char *str, struct berval *val, const char **next, unsigned flags )
1459 const char *p, *startPos, *endPos = NULL;
1460 ber_len_t len, escapes;
1472 for ( startPos = p = str, escapes = 0; p[ 0 ]; p++ ) {
1473 if ( LDAP_DN_ESCAPE( p[ 0 ] ) ) {
1475 if ( p[ 0 ] == '\0' ) {
1479 if ( !LDAP_DN_NEEDESCAPE( p[ 0 ] )
1480 && ( LDAP_DN_PEDANTIC & flags ) ) {
1485 } else if ( LDAP_DN_VALUE_END_V2( p[ 0 ] ) ) {
1490 * FIXME: can we accept anything else? I guess we need
1491 * to stop if a value is not legal
1495 /* strip trailing (unescaped) spaces */
1497 endPos > startPos + 1 &&
1498 LDAP_DN_ASCII_SPACE( endPos[ -1 ] ) &&
1499 !LDAP_DN_ESCAPE( endPos[ -2 ] );
1505 if ( flags & LDAP_DN_SKIP ) {
1509 len = ( endPos ? endPos : p ) - startPos - escapes;
1511 if ( escapes == 0 ) {
1512 val->bv_val = LDAP_STRNDUP( startPos, len );
1517 val->bv_val = LDAP_MALLOC( len + 1 );
1518 for ( s = 0, d = 0; d < len; ) {
1519 if ( LDAP_DN_ESCAPE( startPos[ s ] ) ) {
1522 val->bv_val[ d++ ] = startPos[ s++ ];
1524 val->bv_val[ d ] = '\0';
1525 assert( strlen( val->bv_val ) == len );
1532 quotedIA52strval( const char *str, struct berval *val, const char **next, unsigned flags )
1534 const char *p, *startPos, *endPos = NULL;
1536 unsigned escapes = 0;
1544 /* initial quote already eaten */
1545 for ( startPos = p = str; p[ 0 ]; p++ ) {
1547 * According to RFC 1779, the quoted value can
1548 * contain escaped as well as unescaped special values;
1549 * as a consequence we tolerate escaped values
1550 * (e.g. '"\,"' -> '\,') and escape unescaped specials
1551 * (e.g. '","' -> '\,').
1553 if ( LDAP_DN_ESCAPE( p[ 0 ] ) ) {
1554 if ( p[ 1 ] == '\0' ) {
1559 if ( !LDAP_DN_V2_PAIR( p[ 0 ] )
1560 && ( LDAP_DN_PEDANTIC & flags ) ) {
1562 * do we allow to escape normal chars?
1563 * LDAPv2 does not allow any mechanism
1564 * for escaping chars with '\' and hex
1571 } else if ( LDAP_DN_QUOTES( p[ 0 ] ) ) {
1573 /* eat closing quotes */
1579 * FIXME: can we accept anything else? I guess we need
1580 * to stop if a value is not legal
1584 if ( endPos == NULL ) {
1588 /* Strip trailing (unescaped) spaces */
1589 for ( ; p[ 0 ] && LDAP_DN_ASCII_SPACE( p[ 0 ] ); p++ ) {
1594 if ( flags & LDAP_DN_SKIP ) {
1598 len = endPos - startPos - escapes;
1601 if ( escapes == 0 ) {
1602 val->bv_val = LDAP_STRNDUP( startPos, len );
1607 val->bv_val = LDAP_MALLOC( len + 1 );
1610 for ( s = d = 0; d < len; ) {
1611 if ( LDAP_DN_ESCAPE( str[ s ] ) ) {
1614 val->bv_val[ d++ ] = str[ s++ ];
1616 val->bv_val[ d ] = '\0';
1617 assert( strlen( val->bv_val ) == len );
1624 hexstr2bin( const char *str, char *c )
1634 if ( LDAP_DN_ASCII_DIGIT( c1 ) ) {
1638 if ( LDAP_DN_ASCII_UCASE_HEXALPHA( c1 ) ) {
1641 assert( LDAP_DN_ASCII_LCASE_HEXALPHA( c1 ) );
1648 if ( LDAP_DN_ASCII_DIGIT( c2 ) ) {
1652 if ( LDAP_DN_ASCII_UCASE_HEXALPHA( c2 ) ) {
1653 *c += c2 - 'A' + 10;
1655 assert( LDAP_DN_ASCII_LCASE_HEXALPHA( c2 ) );
1656 *c += c2 - 'a' + 10;
1664 hexstr2binval( const char *str, struct berval *val, const char **next, unsigned flags )
1666 const char *p, *startPos, *endPos = NULL;
1676 for ( startPos = p = str; p[ 0 ]; p += 2 ) {
1677 switch ( LDAP_DN_FORMAT( flags ) ) {
1678 case LDAP_DN_FORMAT_LDAPV3:
1679 if ( LDAP_DN_VALUE_END( p[ 0 ] ) ) {
1684 case LDAP_DN_FORMAT_LDAP:
1685 case LDAP_DN_FORMAT_LDAPV2:
1686 if ( LDAP_DN_VALUE_END_V2( p[ 0 ] ) ) {
1691 case LDAP_DN_FORMAT_DCE:
1692 if ( LDAP_DN_VALUE_END_DCE( p[ 0 ] ) ) {
1698 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1699 if ( flags & LDAP_DN_PEDANTIC ) {
1704 for ( ; p[ 0 ]; p++ ) {
1705 switch ( LDAP_DN_FORMAT( flags ) ) {
1706 case LDAP_DN_FORMAT_LDAPV3:
1707 if ( LDAP_DN_VALUE_END( p[ 0 ] ) ) {
1712 case LDAP_DN_FORMAT_LDAP:
1713 case LDAP_DN_FORMAT_LDAPV2:
1714 if ( LDAP_DN_VALUE_END_V2( p[ 0 ] ) ) {
1719 case LDAP_DN_FORMAT_DCE:
1720 if ( LDAP_DN_VALUE_END_DCE( p[ 0 ] ) ) {
1729 if ( !LDAP_DN_HEXPAIR( p ) ) {
1737 if ( flags & LDAP_DN_SKIP ) {
1741 len = ( ( endPos ? endPos : p ) - startPos ) / 2;
1743 assert( 2 * len == (ber_len_t) (( endPos ? endPos : p ) - startPos ));
1746 val->bv_val = LDAP_MALLOC( len + 1 );
1747 if ( val->bv_val == NULL ) {
1748 return( LDAP_NO_MEMORY );
1751 for ( s = 0, d = 0; d < len; s += 2, d++ ) {
1754 hexstr2bin( &startPos[ s ], &c );
1756 val->bv_val[ d ] = c;
1759 val->bv_val[ d ] = '\0';
1765 * convert a byte in a hexadecimal pair
1768 byte2hexpair( const char *val, char *pair )
1770 static const char hexdig[] = "0123456789ABCDEF";
1776 * we assume the string has enough room for the hex encoding
1780 pair[ 0 ] = hexdig[ 0x0f & ( val[ 0 ] >> 4 ) ];
1781 pair[ 1 ] = hexdig[ 0x0f & val[ 0 ] ];
1787 * convert a binary value in hexadecimal pairs
1790 binval2hexstr( struct berval *val, char *str )
1797 if ( val->bv_len == 0 ) {
1802 * we assume the string has enough room for the hex encoding
1806 for ( s = 0, d = 0; s < val->bv_len; s++, d += 2 ) {
1807 byte2hexpair( &val->bv_val[ s ], &str[ d ] );
1814 * Length of the string representation, accounting for escaped hex
1818 strval2strlen( struct berval *val, unsigned flags, ber_len_t *len )
1820 ber_len_t l, cl = 1;
1822 int escaped_byte_len = LDAP_DN_IS_PRETTY( flags ) ? 1 : 3;
1823 #ifdef PRETTY_ESCAPE
1824 int escaped_ascii_len = LDAP_DN_IS_PRETTY( flags ) ? 2 : 3;
1825 #endif /* PRETTY_ESCAPE */
1831 if ( val->bv_len == 0 ) {
1835 for ( l = 0, p = val->bv_val; p[ 0 ]; p += cl ) {
1836 cl = LDAP_UTF8_CHARLEN( p );
1838 /* illegal utf-8 char! */
1841 } else if ( cl > 1 ) {
1844 for ( cnt = 1; cnt < cl; cnt++ ) {
1845 if ( ( p[ cnt ] & 0x80 ) == 0x00 ) {
1849 l += escaped_byte_len * cl;
1851 } else if ( LDAP_DN_NEEDESCAPE( p[ 0 ] )
1852 || ( p == val->bv_val && LDAP_DN_NEEDESCAPE_LEAD( p[ 0 ] ) )
1853 || ( !p[ 1 ] && LDAP_DN_NEEDESCAPE_TRAIL( p[ 0 ] ) ) ) {
1854 #ifdef PRETTY_ESCAPE
1855 if ( LDAP_DN_WILLESCAPE_HEX( flags, p[ 0 ] ) ) {
1858 * there might be some chars we want
1859 * to escape in form of a couple
1860 * of hexdigits for optimization purposes
1865 l += escaped_ascii_len;
1867 #else /* ! PRETTY_ESCAPE */
1869 #endif /* ! PRETTY_ESCAPE */
1882 * convert to string representation, escaping with hex the UTF-8 stuff;
1883 * assume the destination has enough room for escaping
1886 strval2str( struct berval *val, char *str, unsigned flags, ber_len_t *len )
1888 ber_len_t s, d, end;
1894 if ( val->bv_len == 0 ) {
1900 * we assume the string has enough room for the hex encoding
1903 for ( s = 0, d = 0, end = val->bv_len - 1; s < val->bv_len; ) {
1904 ber_len_t cl = LDAP_UTF8_CHARLEN( &val->bv_val[ s ] );
1907 * there might be some chars we want to escape in form
1908 * of a couple of hexdigits for optimization purposes
1910 if ( ( cl > 1 && !LDAP_DN_IS_PRETTY( flags ) )
1911 #ifdef PRETTY_ESCAPE
1912 || LDAP_DN_WILLESCAPE_HEX( flags, val->bv_val[ s ] )
1913 #else /* ! PRETTY_ESCAPE */
1914 || LDAP_DN_NEEDESCAPE( val->bv_val[ s ] )
1915 || ( d == 0 && LDAP_DN_NEEDESCAPE_LEAD( val->bv_val[ s ] ) )
1916 || ( s == end && LDAP_DN_NEEDESCAPE_TRAIL( val->bv_val[ s ] ) )
1918 #endif /* ! PRETTY_ESCAPE */
1922 byte2hexpair( &val->bv_val[ s ], &str[ d ] );
1927 } else if ( cl > 1 ) {
1929 str[ d++ ] = val->bv_val[ s++ ];
1933 #ifdef PRETTY_ESCAPE
1934 if ( LDAP_DN_NEEDESCAPE( val->bv_val[ s ] )
1935 || ( d == 0 && LDAP_DN_NEEDESCAPE_LEAD( val->bv_val[ s ] ) )
1936 || ( s == end && LDAP_DN_NEEDESCAPE_TRAIL( val->bv_val[ s ] ) ) ) {
1938 if ( !LDAP_DN_IS_PRETTY( flags ) ) {
1939 byte2hexpair( &val->bv_val[ s ], &str[ d ] );
1945 #endif /* PRETTY_ESCAPE */
1946 str[ d++ ] = val->bv_val[ s++ ];
1956 * Length of the IA5 string representation (no UTF-8 allowed)
1959 strval2IA5strlen( struct berval *val, unsigned flags, ber_len_t *len )
1968 if ( val->bv_len == 0 ) {
1972 if ( flags & LDAP_AVA_NONPRINTABLE ) {
1974 * Turn value into a binary encoded BER
1979 for ( l = 0, p = val->bv_val; p[ 0 ]; p++ ) {
1980 if ( LDAP_DN_NEEDESCAPE( p[ 0 ] )
1981 || ( p == val->bv_val && LDAP_DN_NEEDESCAPE_LEAD( p[ 0 ] ) )
1982 || ( !p[ 1 ] && LDAP_DN_NEEDESCAPE_TRAIL( p[ 0 ] ) ) ) {
1997 * convert to string representation (np UTF-8)
1998 * assume the destination has enough room for escaping
2001 strval2IA5str( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2003 ber_len_t s, d, end;
2009 if ( val->bv_len == 0 ) {
2014 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2016 * Turn value into a binary encoded BER
2023 * we assume the string has enough room for the hex encoding
2027 for ( s = 0, d = 0, end = val->bv_len - 1; s < val->bv_len; ) {
2028 if ( LDAP_DN_NEEDESCAPE( val->bv_val[ s ] )
2029 || ( s == 0 && LDAP_DN_NEEDESCAPE_LEAD( val->bv_val[ s ] ) )
2030 || ( s == end && LDAP_DN_NEEDESCAPE_TRAIL( val->bv_val[ s ] ) ) ) {
2033 str[ d++ ] = val->bv_val[ s++ ];
2043 * Length of the (supposedly) DCE string representation,
2044 * accounting for escaped hex of UTF-8 chars
2047 strval2DCEstrlen( struct berval *val, unsigned flags, ber_len_t *len )
2056 if ( val->bv_len == 0 ) {
2060 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2062 * FIXME: Turn the value into a binary encoded BER?
2067 for ( l = 0, p = val->bv_val; p[ 0 ]; p++ ) {
2068 if ( LDAP_DN_NEEDESCAPE_DCE( p[ 0 ] ) ) {
2083 * convert to (supposedly) DCE string representation,
2084 * escaping with hex the UTF-8 stuff;
2085 * assume the destination has enough room for escaping
2088 strval2DCEstr( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2096 if ( val->bv_len == 0 ) {
2101 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2103 * FIXME: Turn the value into a binary encoded BER?
2111 * we assume the string has enough room for the hex encoding
2115 for ( s = 0, d = 0; s < val->bv_len; ) {
2116 if ( LDAP_DN_NEEDESCAPE_DCE( val->bv_val[ s ] ) ) {
2119 str[ d++ ] = val->bv_val[ s++ ];
2129 * Length of the (supposedly) AD canonical string representation,
2130 * accounting for escaped hex of UTF-8 chars
2133 strval2ADstrlen( struct berval *val, unsigned flags, ber_len_t *len )
2142 if ( val->bv_len == 0 ) {
2146 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2148 * FIXME: Turn the value into a binary encoded BER?
2153 for ( l = 0, p = val->bv_val; p[ 0 ]; p++ ) {
2154 if ( LDAP_DN_NEEDESCAPE_AD( p[ 0 ] ) ) {
2169 * convert to (supposedly) AD string representation,
2170 * escaping with hex the UTF-8 stuff;
2171 * assume the destination has enough room for escaping
2174 strval2ADstr( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2182 if ( val->bv_len == 0 ) {
2187 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2189 * FIXME: Turn the value into a binary encoded BER?
2197 * we assume the string has enough room for the hex encoding
2201 for ( s = 0, d = 0; s < val->bv_len; ) {
2202 if ( LDAP_DN_NEEDESCAPE_AD( val->bv_val[ s ] ) ) {
2205 str[ d++ ] = val->bv_val[ s++ ];
2215 * If the DN is terminated by single-AVA RDNs with attribute type of "dc",
2216 * the first part of the AD representation of the DN is written in DNS
2217 * form, i.e. dot separated domain name components (as suggested
2218 * by Luke Howard, http://www.padl.com/~lukeh)
2221 dn2domain( LDAPDN *dn, struct berval *bv, int pos, int *iRDN )
2224 int domain = 0, first = 1;
2225 ber_len_t l = 1; /* we move the null also */
2228 /* we are guaranteed there's enough memory in str */
2234 assert( *iRDN >= 0 );
2236 str = bv->bv_val + pos;
2238 for ( i = *iRDN; i >= 0; i-- ) {
2242 assert( dn[ 0 ][ i ] );
2245 assert( rdn[ 0 ][ 0 ] );
2246 ava = rdn[ 0 ][ 0 ];
2248 if ( !LDAP_DN_IS_RDN_DC( rdn ) ) {
2256 AC_MEMCPY( str, ava->la_value.bv_val,
2257 ava->la_value.bv_len + 1);
2258 l += ava->la_value.bv_len;
2261 AC_MEMCPY( str + ava->la_value.bv_len + 1, bv->bv_val + pos, l);
2262 AC_MEMCPY( str, ava->la_value.bv_val,
2263 ava->la_value.bv_len );
2264 str[ ava->la_value.bv_len ] = '.';
2265 l += ava->la_value.bv_len + 1;
2270 bv->bv_len = pos + l - 1;
2276 rdn2strlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len,
2277 int ( *s2l )( struct berval *v, unsigned f, ber_len_t *l ) )
2284 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
2285 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
2287 /* len(type) + '=' + '+' | ',' */
2288 l += ava->la_attr.bv_len + 2;
2290 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2291 /* octothorpe + twice the length */
2292 l += 1 + 2 * ava->la_value.bv_len;
2296 unsigned f = flags | ava->la_flags;
2298 if ( ( *s2l )( &ava->la_value, f, &vl ) ) {
2311 rdn2str( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len,
2312 int ( *s2s ) ( struct berval *v, char * s, unsigned f, ber_len_t *l ) )
2317 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
2318 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
2320 AC_MEMCPY( &str[ l ], ava->la_attr.bv_val,
2321 ava->la_attr.bv_len );
2322 l += ava->la_attr.bv_len;
2326 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2328 if ( binval2hexstr( &ava->la_value, &str[ l ] ) ) {
2331 l += 2 * ava->la_value.bv_len;
2335 unsigned f = flags | ava->la_flags;
2337 if ( ( *s2s )( &ava->la_value, &str[ l ], f, &vl ) ) {
2342 str[ l++ ] = ( rdn[ 0 ][ iAVA + 1 ] ? '+' : ',' );
2351 rdn2DCEstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len )
2358 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
2359 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
2361 /* len(type) + '=' + ',' | '/' */
2362 l += ava->la_attr.bv_len + 2;
2364 switch ( ava->la_flags ) {
2365 case LDAP_AVA_BINARY:
2366 /* octothorpe + twice the length */
2367 l += 1 + 2 * ava->la_value.bv_len;
2370 case LDAP_AVA_STRING: {
2372 unsigned f = flags | ava->la_flags;
2374 if ( strval2DCEstrlen( &ava->la_value, f, &vl ) ) {
2392 rdn2DCEstr( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len, int first )
2397 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
2398 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
2403 str[ l++ ] = ( iAVA ? ',' : '/' );
2406 AC_MEMCPY( &str[ l ], ava->la_attr.bv_val,
2407 ava->la_attr.bv_len );
2408 l += ava->la_attr.bv_len;
2412 switch ( ava->la_flags ) {
2413 case LDAP_AVA_BINARY:
2415 if ( binval2hexstr( &ava->la_value, &str[ l ] ) ) {
2418 l += 2 * ava->la_value.bv_len;
2421 case LDAP_AVA_STRING: {
2423 unsigned f = flags | ava->la_flags;
2425 if ( strval2DCEstr( &ava->la_value, &str[ l ], f, &vl ) ) {
2443 rdn2UFNstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len )
2453 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
2454 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
2457 l += ( rdn[ 0 ][ iAVA + 1 ] ? 3 : 2 );
2459 /* FIXME: are binary values allowed in UFN? */
2460 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2461 /* octothorpe + twice the value */
2462 l += 1 + 2 * ava->la_value.bv_len;
2466 unsigned f = flags | ava->la_flags;
2468 if ( strval2strlen( &ava->la_value, f, &vl ) ) {
2481 rdn2UFNstr( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len )
2486 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
2487 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
2489 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2491 if ( binval2hexstr( &ava->la_value, &str[ l ] ) ) {
2494 l += 2 * ava->la_value.bv_len;
2498 unsigned f = flags | ava->la_flags;
2500 if ( strval2str( &ava->la_value, &str[ l ], f, &vl ) ) {
2506 if ( rdn[ 0 ][ iAVA + 1 ]) {
2507 AC_MEMCPY( &str[ l ], " + ", 3 );
2511 AC_MEMCPY( &str[ l ], ", ", 2 );
2522 rdn2ADstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len )
2532 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
2533 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
2538 /* FIXME: are binary values allowed in UFN? */
2539 switch ( ava->la_flags ) {
2540 case LDAP_AVA_BINARY:
2541 /* octothorpe + twice the value */
2542 l += 1 + 2 * ava->la_value.bv_len;
2545 case LDAP_AVA_STRING: {
2547 unsigned f = flags | ava->la_flags;
2549 if ( strval2ADstrlen( &ava->la_value, f, &vl ) ) {
2567 rdn2ADstr( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len, int first )
2572 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
2573 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
2578 str[ l++ ] = ( iAVA ? ',' : '/' );
2581 switch ( ava->la_flags ) {
2582 case LDAP_AVA_BINARY:
2584 if ( binval2hexstr( &ava->la_value, &str[ l ] ) ) {
2587 l += 2 * ava->la_value.bv_len;
2590 case LDAP_AVA_STRING: {
2592 unsigned f = flags | ava->la_flags;
2594 if ( strval2ADstr( &ava->la_value, &str[ l ], f, &vl ) ) {
2614 * Returns in str a string representation of rdn based on flags.
2615 * There is some duplication of code between this and ldap_dn2str;
2616 * this is wanted to reduce the allocation of temporary buffers.
2619 ldap_rdn2str( LDAPRDN *rdn, char **str, unsigned flags )
2626 if((flags & LDAP_DN_FORMAT_MASK) == LDAP_DN_FORMAT_LBER) {
2627 return LDAP_PARAM_ERROR;
2630 rc = ldap_rdn2bv( rdn, &bv, flags );
2636 ldap_rdn2bv( LDAPRDN *rdn, struct berval *bv, unsigned flags )
2646 if ( rdn == NULL ) {
2647 bv->bv_val = LDAP_STRDUP( "" );
2648 return( LDAP_SUCCESS );
2652 * This routine wastes "back" bytes at the end of the string
2655 switch ( LDAP_DN_FORMAT( flags ) ) {
2656 case LDAP_DN_FORMAT_LDAPV3:
2657 if ( rdn2strlen( rdn, flags, &l, strval2strlen ) ) {
2658 return LDAP_DECODING_ERROR;
2662 case LDAP_DN_FORMAT_LDAPV2:
2663 if ( rdn2strlen( rdn, flags, &l, strval2IA5strlen ) ) {
2664 return LDAP_DECODING_ERROR;
2668 case LDAP_DN_FORMAT_UFN:
2669 if ( rdn2UFNstrlen( rdn, flags, &l ) ) {
2670 return LDAP_DECODING_ERROR;
2674 case LDAP_DN_FORMAT_DCE:
2675 if ( rdn2DCEstrlen( rdn, flags, &l ) ) {
2676 return LDAP_DECODING_ERROR;
2680 case LDAP_DN_FORMAT_AD_CANONICAL:
2681 if ( rdn2ADstrlen( rdn, flags, &l ) ) {
2682 return LDAP_DECODING_ERROR;
2687 return( LDAP_PARAM_ERROR );
2690 bv->bv_val = LDAP_MALLOC( l + 1 );
2692 switch ( LDAP_DN_FORMAT( flags ) ) {
2693 case LDAP_DN_FORMAT_LDAPV3:
2694 rc = rdn2str( rdn, bv->bv_val, flags, &l, strval2str );
2698 case LDAP_DN_FORMAT_LDAPV2:
2699 rc = rdn2str( rdn, bv->bv_val, flags, &l, strval2IA5str );
2703 case LDAP_DN_FORMAT_UFN:
2704 rc = rdn2UFNstr( rdn, bv->bv_val, flags, &l );
2708 case LDAP_DN_FORMAT_DCE:
2709 rc = rdn2DCEstr( rdn, bv->bv_val, flags, &l, 1 );
2713 case LDAP_DN_FORMAT_AD_CANONICAL:
2714 rc = rdn2ADstr( rdn, bv->bv_val, flags, &l, 1 );
2719 /* need at least one of the previous */
2720 return LDAP_PARAM_ERROR;
2724 ldap_memfree( bv->bv_val );
2728 bv->bv_len = l - back;
2729 bv->bv_val[ bv->bv_len ] = '\0';
2731 return LDAP_SUCCESS;
2735 * Very bulk implementation; many optimizations can be performed
2736 * - a NULL dn results in an empty string ""
2739 * a) what do we do if a UTF-8 string must be converted in LDAPv2?
2740 * we must encode it in binary form ('#' + HEXPAIRs)
2741 * b) does DCE/AD support UTF-8?
2742 * no clue; don't think so.
2743 * c) what do we do when binary values must be converted in UTF/DCE/AD?
2744 * use binary encoded BER
2746 int ldap_dn2str( LDAPDN *dn, char **str, unsigned flags )
2753 if((flags & LDAP_DN_FORMAT_MASK) == LDAP_DN_FORMAT_LBER) {
2754 return LDAP_PARAM_ERROR;
2757 rc = ldap_dn2bv( dn, &bv, flags );
2762 int ldap_dn2bv( LDAPDN *dn, struct berval *bv, unsigned flags )
2765 int rc = LDAP_ENCODING_ERROR;
2768 /* stringifying helpers for LDAPv3/LDAPv2 */
2769 int ( *sv2l ) ( struct berval *v, unsigned f, ber_len_t *l );
2770 int ( *sv2s ) ( struct berval *v, char *s, unsigned f, ber_len_t *l );
2777 Debug( LDAP_DEBUG_TRACE, "=> ldap_dn2bv(%u)\n%s%s", flags, "", "" );
2780 * a null dn means an empty dn string
2781 * FIXME: better raise an error?
2784 bv->bv_val = LDAP_STRDUP( "" );
2785 return( LDAP_SUCCESS );
2788 switch ( LDAP_DN_FORMAT( flags ) ) {
2789 case LDAP_DN_FORMAT_LDAPV3:
2790 sv2l = strval2strlen;
2794 case LDAP_DN_FORMAT_LDAPV2:
2795 sv2l = strval2IA5strlen;
2796 sv2s = strval2IA5str;
2799 for ( iRDN = 0, len = 0; dn[ 0 ][ iRDN ]; iRDN++ ) {
2801 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
2803 if ( rdn2strlen( rdn, flags, &rdnl, sv2l ) ) {
2804 goto return_results;
2810 if ( ( bv->bv_val = LDAP_MALLOC( len + 1 ) ) == NULL ) {
2811 rc = LDAP_NO_MEMORY;
2815 for ( l = 0, iRDN = 0; dn[ 0 ][ iRDN ]; iRDN++ ) {
2817 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
2819 if ( rdn2str( rdn, &bv->bv_val[ l ], flags,
2821 LDAP_FREE( bv->bv_val );
2823 goto return_results;
2831 * trim the last ',' (the allocated memory
2832 * is one byte longer than required)
2834 bv->bv_len = len - 1;
2835 bv->bv_val[ bv->bv_len ] = '\0';
2840 case LDAP_DN_FORMAT_UFN: {
2843 * FIXME: quoting from RFC 1781:
2845 To take a distinguished name, and generate a name of this format with
2846 attribute types omitted, the following steps are followed.
2848 1. If the first attribute is of type CommonName, the type may be
2851 2. If the last attribute is of type Country, the type may be
2854 3. If the last attribute is of type Country, the last
2855 Organisation attribute may have the type omitted.
2857 4. All attributes of type OrganisationalUnit may have the type
2858 omitted, unless they are after an Organisation attribute or
2859 the first attribute is of type OrganisationalUnit.
2861 * this should be the pedantic implementation.
2863 * Here the standard implementation reflects
2864 * the one historically provided by OpenLDAP
2865 * (and UMIch, I presume), with the variant
2866 * of spaces and plusses (' + ') separating
2869 * A non-standard but nice implementation could
2870 * be to turn the final "dc" attributes into a
2871 * dot-separated domain.
2873 * Other improvements could involve the use of
2874 * friendly country names and so.
2877 int leftmost_dc = -1;
2879 #endif /* DC_IN_UFN */
2881 for ( iRDN = 0, len = 0; dn[ 0 ][ iRDN ]; iRDN++ ) {
2883 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
2885 if ( rdn2UFNstrlen( rdn, flags, &rdnl ) ) {
2886 goto return_results;
2891 if ( LDAP_DN_IS_RDN_DC( rdn ) ) {
2892 if ( leftmost_dc == -1 ) {
2898 #endif /* DC_IN_UFN */
2901 if ( ( bv->bv_val = LDAP_MALLOC( len + 1 ) ) == NULL ) {
2902 rc = LDAP_NO_MEMORY;
2907 if ( leftmost_dc == -1 ) {
2908 #endif /* DC_IN_UFN */
2909 for ( l = 0, iRDN = 0; dn[ 0 ][ iRDN ]; iRDN++ ) {
2911 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
2913 if ( rdn2UFNstr( rdn, &bv->bv_val[ l ],
2915 LDAP_FREE( bv->bv_val );
2917 goto return_results;
2923 * trim the last ', ' (the allocated memory
2924 * is two bytes longer than required)
2926 bv->bv_len = len - 2;
2927 bv->bv_val[ bv->bv_len ] = '\0';
2930 last_iRDN = iRDN - 1;
2932 for ( l = 0, iRDN = 0; iRDN < leftmost_dc; iRDN++ ) {
2934 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
2936 if ( rdn2UFNstr( rdn, &bv->bv_val[ l ],
2938 LDAP_FREE( bv->bv_val );
2940 goto return_results;
2945 if ( !dn2domain( dn, bv, l, &last_iRDN ) ) {
2946 LDAP_FREE( bv->bv_val );
2948 goto return_results;
2951 /* the string is correctly terminated by dn2domain */
2953 #endif /* DC_IN_UFN */
2959 case LDAP_DN_FORMAT_DCE:
2961 for ( iRDN = 0, len = 0; dn[ 0 ][ iRDN ]; iRDN++ ) {
2963 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
2965 if ( rdn2DCEstrlen( rdn, flags, &rdnl ) ) {
2966 goto return_results;
2972 if ( ( bv->bv_val = LDAP_MALLOC( len + 1 ) ) == NULL ) {
2973 rc = LDAP_NO_MEMORY;
2977 for ( l = 0; iRDN--; ) {
2979 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
2981 if ( rdn2DCEstr( rdn, &bv->bv_val[ l ], flags,
2983 LDAP_FREE( bv->bv_val );
2985 goto return_results;
2993 bv->bv_val[ bv->bv_len ] = '\0';
2998 case LDAP_DN_FORMAT_AD_CANONICAL: {
3001 * Sort of UFN for DCE DNs: a slash ('/') separated
3002 * global->local DN with no types; strictly speaking,
3003 * the naming context should be a domain, which is
3004 * written in DNS-style, e.g. dot-deparated.
3008 * "givenName=Bill+sn=Gates,ou=People,dc=microsoft,dc=com"
3012 * "microsoft.com/People/Bill,Gates"
3014 for ( iRDN = 0, len = -1; dn[ 0 ][ iRDN ]; iRDN++ ) {
3016 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
3018 if ( rdn2ADstrlen( rdn, flags, &rdnl ) ) {
3019 goto return_results;
3025 if ( ( bv->bv_val = LDAP_MALLOC( len + 1 ) ) == NULL ) {
3026 rc = LDAP_NO_MEMORY;
3031 if ( iRDN && dn2domain( dn, bv, 0, &iRDN ) ) {
3032 for ( l = bv->bv_len; iRDN >= 0 ; iRDN-- ) {
3034 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
3036 if ( rdn2ADstr( rdn, &bv->bv_val[ l ],
3037 flags, &rdnl, 0 ) ) {
3038 LDAP_FREE( bv->bv_val );
3040 goto return_results;
3049 * Strictly speaking, AD canonical requires
3050 * a DN to be in the form "..., dc=smtg",
3051 * i.e. terminated by a domain component
3053 if ( flags & LDAP_DN_PEDANTIC ) {
3054 LDAP_FREE( bv->bv_val );
3056 rc = LDAP_ENCODING_ERROR;
3060 for ( l = 0; iRDN >= 0 ; iRDN-- ) {
3062 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
3064 if ( rdn2ADstr( rdn, &bv->bv_val[ l ],
3065 flags, &rdnl, first ) ) {
3066 LDAP_FREE( bv->bv_val );
3068 goto return_results;
3078 bv->bv_val[ bv->bv_len ] = '\0';
3085 return LDAP_PARAM_ERROR;
3088 Debug( LDAP_DEBUG_TRACE, "<= ldap_dn2bv(%s,%u)=%d\n",
3089 bv->bv_val, flags, rc );