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, &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( const char *dnin, unsigned fin, char **dnout, unsigned fout )
305 LDAPDN *tmpDN = NULL;
307 Debug( LDAP_DEBUG_TRACE, "ldap_dn_normalize\n", 0, 0, 0 );
313 if ( dnin == NULL ) {
314 return( LDAP_SUCCESS );
317 rc = ldap_str2dn( dnin , &tmpDN, fin );
318 if ( rc != LDAP_SUCCESS ) {
322 rc = ldap_dn2str( tmpDN, dnout, fout );
324 ldap_dnfree( tmpDN );
332 /* #define B4ATTRTYPE 0x0001 */
333 #define B4OIDATTRTYPE 0x0002
334 #define B4STRINGATTRTYPE 0x0003
336 #define B4AVAEQUALS 0x0100
337 #define B4AVASEP 0x0200
338 #define B4RDNSEP 0x0300
339 #define GOTAVA 0x0400
341 #define B4ATTRVALUE 0x0010
342 #define B4STRINGVALUE 0x0020
343 #define B4IA5VALUEQUOTED 0x0030
344 #define B4IA5VALUE 0x0040
345 #define B4BINARYVALUE 0x0050
348 * Helpers (mostly from slap.h)
349 * c is assumed to Unicode in an ASCII compatible format (UTF-8)
350 * Macros assume "C" Locale (ASCII)
352 #define LDAP_DN_ASCII_SPACE(c) \
353 ( (c) == ' ' || (c) == '\t' || (c) == '\n' || (c) == '\r' )
354 #define LDAP_DN_ASCII_LOWER(c) LDAP_LOWER(c)
355 #define LDAP_DN_ASCII_UPPER(c) LDAP_UPPER(c)
356 #define LDAP_DN_ASCII_ALPHA(c) LDAP_ALPHA(c)
358 #define LDAP_DN_ASCII_DIGIT(c) LDAP_DIGIT(c)
359 #define LDAP_DN_ASCII_LCASE_HEXALPHA(c) LDAP_HEXLOWER(c)
360 #define LDAP_DN_ASCII_UCASE_HEXALPHA(c) LDAP_HEXUPPER(c)
361 #define LDAP_DN_ASCII_HEXDIGIT(c) LDAP_HEX(c)
362 #define LDAP_DN_ASCII_ALNUM(c) LDAP_ALNUM(c)
363 #define LDAP_DN_ASCII_PRINTABLE(c) ( (c) >= ' ' && (c) <= '~' )
366 #define LDAP_DN_OID_LEADCHAR(c) LDAP_DIGIT(c)
367 #define LDAP_DN_DESC_LEADCHAR(c) LDAP_ALPHA(c)
368 #define LDAP_DN_DESC_CHAR(c) LDAP_LDH(c)
369 #define LDAP_DN_LANG_SEP(c) ( (c) == ';' )
370 #define LDAP_DN_ATTRDESC_CHAR(c) \
371 ( LDAP_DN_DESC_CHAR(c) || LDAP_DN_LANG_SEP(c) )
373 /* special symbols */
374 #define LDAP_DN_AVA_EQUALS(c) ( (c) == '=' )
375 #define LDAP_DN_AVA_SEP(c) ( (c) == '+' )
376 #define LDAP_DN_RDN_SEP(c) ( (c) == ',' )
377 #define LDAP_DN_RDN_SEP_V2(c) ( LDAP_DN_RDN_SEP(c) || (c) == ';' )
378 #define LDAP_DN_OCTOTHORPE(c) ( (c) == '#' )
379 #define LDAP_DN_QUOTES(c) ( (c) == '\"' )
380 #define LDAP_DN_ESCAPE(c) ( (c) == '\\' )
381 #define LDAP_DN_VALUE_END(c) \
382 ( LDAP_DN_RDN_SEP(c) || LDAP_DN_AVA_SEP(c) )
383 #define LDAP_DN_NE(c) \
384 ( LDAP_DN_RDN_SEP_V2(c) || LDAP_DN_AVA_SEP(c) \
385 || LDAP_DN_QUOTES(c) || (c) == '<' || (c) == '>' )
386 #define LDAP_DN_NEEDESCAPE(c) \
387 ( LDAP_DN_ESCAPE(c) || LDAP_DN_NE(c) )
388 #define LDAP_DN_NEEDESCAPE_LEAD(c) \
389 ( LDAP_DN_ASCII_SPACE(c) || LDAP_DN_OCTOTHORPE(c) || LDAP_DN_NE(c) )
390 #define LDAP_DN_NEEDESCAPE_TRAIL(c) \
391 ( LDAP_DN_ASCII_SPACE(c) || LDAP_DN_NEEDESCAPE(c) )
392 #define LDAP_DN_WILLESCAPE_CHAR(c) \
393 ( LDAP_DN_RDN_SEP(c) || LDAP_DN_AVA_SEP(c) || LDAP_DN_ESCAPE(c) )
394 #define LDAP_DN_IS_PRETTY(f) ( (f) & LDAP_DN_PRETTY )
395 #define LDAP_DN_WILLESCAPE_HEX(f, c) \
396 ( ( !LDAP_DN_IS_PRETTY( f ) ) && LDAP_DN_WILLESCAPE_CHAR(c) )
399 #define LDAP_DN_VALUE_END_V2(c) \
400 ( LDAP_DN_RDN_SEP_V2(c) || LDAP_DN_AVA_SEP(c) )
402 #define LDAP_DN_V2_SPECIAL(c) \
403 ( LDAP_DN_RDN_SEP_V2(c) || LDAP_DN_AVA_EQUALS(c) \
404 || LDAP_DN_AVA_SEP(c) || (c) == '<' || (c) == '>' \
405 || LDAP_DN_OCTOTHORPE(c) )
406 #define LDAP_DN_V2_PAIR(c) \
407 ( LDAP_DN_V2_SPECIAL(c) || LDAP_DN_ESCAPE(c) || LDAP_DN_QUOTES(c) )
410 * DCE (mostly from Luke Howard and IBM implementation for AIX)
412 * From: "Application Development Guide - Directory Services" (FIXME: add link?)
413 * Here escapes and valid chars for GDS are considered; as soon as more
414 * specific info is found, the macros will be updated.
416 * Chars: 'a'-'z', 'A'-'Z', '0'-'9',
417 * '.', ':', ',', ''', '+', '-', '=', '(', ')', '?', '/', ' '.
419 * Metachars: '/', ',', '=', '\'.
421 * the '\' is used to escape other metachars.
427 * Attribute types must start with alphabetic chars and can contain
428 * alphabetic chars and digits (FIXME: no '-'?). OIDs are allowed.
430 #define LDAP_DN_RDN_SEP_DCE(c) ( (c) == '/' )
431 #define LDAP_DN_AVA_SEP_DCE(c) ( (c) == ',' )
432 #define LDAP_DN_ESCAPE_DCE(c) ( LDAP_DN_ESCAPE(c) )
433 #define LDAP_DN_VALUE_END_DCE(c) \
434 ( LDAP_DN_RDN_SEP_DCE(c) || LDAP_DN_AVA_SEP_DCE(c) )
435 #define LDAP_DN_NEEDESCAPE_DCE(c) \
436 ( LDAP_DN_VALUE_END_DCE(c) || LDAP_DN_AVA_EQUALS(c) )
439 #define LDAP_DN_RDN_SEP_AD(c) ( (c) == '/' )
440 #define LDAP_DN_ESCAPE_AD(c) ( LDAP_DN_ESCAPE(c) )
441 #define LDAP_DN_AVA_SEP_AD(c) ( (c) == ',' ) /* assume same as DCE */
442 #define LDAP_DN_VALUE_END_AD(c) \
443 ( LDAP_DN_RDN_SEP_AD(c) || LDAP_DN_AVA_SEP_AD(c) )
444 #define LDAP_DN_NEEDESCAPE_AD(c) \
445 ( LDAP_DN_VALUE_END_AD(c) || LDAP_DN_AVA_EQUALS(c) )
448 #define LDAP_DN_HEXPAIR(s) \
449 ( LDAP_DN_ASCII_HEXDIGIT((s)[0]) && LDAP_DN_ASCII_HEXDIGIT((s)[1]) )
450 /* better look at the AttributeDescription? */
452 /* FIXME: no composite rdn or non-"dc" types, right?
453 * (what about "dc" in OID form?) */
454 /* FIXME: we do not allow binary values in domain, right? */
455 /* NOTE: use this macro only when ABSOLUTELY SURE rdn IS VALID! */
456 /* NOTE: don't use strcasecmp() as it is locale specific! */
457 #define LDAP_DC_ATTR "dc"
458 #define LDAP_DC_ATTRU "DC"
459 #define LDAP_DN_IS_RDN_DC( r ) \
460 ( (r) && (r)[0][0] && !(r)[0][1] \
461 && ((r)[0][0]->la_flags == LDAP_AVA_STRING) \
462 && ((r)[0][0]->la_attr.bv_len == 2) \
463 && (((r)[0][0]->la_attr.bv_val[0] == LDAP_DC_ATTR[0]) \
464 || ((r)[0][0]->la_attr.bv_val[0] == LDAP_DC_ATTRU[0])) \
465 && (((r)[0][0]->la_attr.bv_val[1] == LDAP_DC_ATTR[1]) \
466 || ((r)[0][0]->la_attr.bv_val[1] == LDAP_DC_ATTRU[1])))
468 /* Composite rules */
469 #define LDAP_DN_ALLOW_ONE_SPACE(f) \
470 ( LDAP_DN_LDAPV2(f) \
471 || !( (f) & LDAP_DN_P_NOSPACEAFTERRDN ) )
472 #define LDAP_DN_ALLOW_SPACES(f) \
473 ( LDAP_DN_LDAPV2(f) \
474 || !( (f) & ( LDAP_DN_P_NOLEADTRAILSPACES | LDAP_DN_P_NOSPACEAFTERRDN ) ) )
475 #define LDAP_DN_LDAP(f) \
476 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_LDAP )
477 #define LDAP_DN_LDAPV3(f) \
478 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_LDAPV3 )
479 #define LDAP_DN_LDAPV2(f) \
480 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_LDAPV2 )
481 #define LDAP_DN_DCE(f) \
482 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_DCE )
483 #define LDAP_DN_UFN(f) \
484 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_UFN )
485 #define LDAP_DN_ADC(f) \
486 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_AD_CANONICAL )
487 #define LDAP_DN_FORMAT(f) ( (f) & LDAP_DN_FORMAT_MASK )
490 * LDAPAVA helpers (will become part of the API for operations
491 * on structural representations of DNs).
494 ldapava_new( const struct berval *attr, const struct berval *val,
502 ava = LDAP_MALLOC( sizeof( LDAPAVA ) + attr->bv_len + 1 );
504 /* should we test it? */
509 ava->la_attr.bv_len = attr->bv_len;
510 ava->la_attr.bv_val = (char *)(ava+1);
511 AC_MEMCPY( ava->la_attr.bv_val, attr->bv_val, attr->bv_len );
512 ava->la_attr.bv_val[attr->bv_len] = '\0';
514 ava->la_value = *val;
515 ava->la_flags = flags;
517 ava->la_private = NULL;
523 ldap_avafree( LDAPAVA *ava )
528 /* ava's private must be freed by caller
529 * (at present let's skip this check because la_private
530 * basically holds static data) */
531 assert( ava->la_private == NULL );
535 /* la_attr is now contiguous with ava, not freed separately */
536 LDAP_FREE( ava->la_attr.bv_val );
538 LDAP_FREE( ava->la_value.bv_val );
544 ldap_rdnfree( LDAPRDN *rdn )
552 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
553 ldap_avafree( rdn[ 0 ][ iAVA ] );
560 ldap_dnfree( LDAPDN *dn )
568 for ( iRDN = 0; dn[ 0 ][ iRDN ]; iRDN++ ) {
569 ldap_rdnfree( dn[ 0 ][ iRDN ] );
576 * Converts a string representation of a DN (in LDAPv3, LDAPv2 or DCE)
577 * into a structural representation of the DN, by separating attribute
578 * types and values encoded in the more appropriate form, which is
579 * string or OID for attribute types and binary form of the BER encoded
580 * value or Unicode string. Formats different from LDAPv3 are parsed
581 * according to their own rules and turned into the more appropriate
582 * form according to LDAPv3.
584 * NOTE: I realize the code is getting spaghettish; it is rather
585 * experimental and will hopefully turn into something more simple
586 * and readable as soon as it works as expected.
589 #define TMP_SLOTS 1024
592 ldap_str2dn( const char *str, LDAPDN **dn, unsigned flags )
595 int rc = LDAP_DECODING_ERROR;
598 LDAPDN *newDN = NULL;
599 LDAPRDN *newRDN = NULL, *tmpDN[TMP_SLOTS];
604 Debug( LDAP_DEBUG_TRACE, "=> ldap_str2dn(%s,%u)\n%s", str, flags, "" );
608 switch ( LDAP_DN_FORMAT( flags ) ) {
609 case LDAP_DN_FORMAT_LDAP:
610 case LDAP_DN_FORMAT_LDAPV3:
611 case LDAP_DN_FORMAT_LDAPV2:
612 case LDAP_DN_FORMAT_DCE:
615 /* unsupported in str2dn */
616 case LDAP_DN_FORMAT_UFN:
617 case LDAP_DN_FORMAT_AD_CANONICAL:
618 return LDAP_PARAM_ERROR;
620 case LDAP_DN_FORMAT_LBER:
622 return LDAP_PARAM_ERROR;
625 if ( str[ 0 ] == '\0' ) {
630 if ( LDAP_DN_DCE( flags ) ) {
633 * (from Luke Howard: thnx) A RDN separator is required
634 * at the beginning of an (absolute) DN.
636 if ( !LDAP_DN_RDN_SEP_DCE( p[ 0 ] ) ) {
641 } else if ( LDAP_DN_LDAP( flags ) ) {
643 * if dn starts with '/' let's make it a DCE dn
645 if ( LDAP_DN_RDN_SEP_DCE( p[ 0 ] ) ) {
646 flags |= LDAP_DN_FORMAT_DCE;
651 for ( ; p[ 0 ]; p++ ) {
654 err = ldap_str2rdn( p, &newRDN, &p, flags );
655 if ( err != LDAP_SUCCESS ) {
660 * We expect a rdn separator
663 switch ( LDAP_DN_FORMAT( flags ) ) {
664 case LDAP_DN_FORMAT_LDAPV3:
665 if ( !LDAP_DN_RDN_SEP( p[ 0 ] ) ) {
666 rc = LDAP_DECODING_ERROR;
671 case LDAP_DN_FORMAT_LDAP:
672 case LDAP_DN_FORMAT_LDAPV2:
673 if ( !LDAP_DN_RDN_SEP_V2( p[ 0 ] ) ) {
674 rc = LDAP_DECODING_ERROR;
679 case LDAP_DN_FORMAT_DCE:
680 if ( !LDAP_DN_RDN_SEP_DCE( p[ 0 ] ) ) {
681 rc = LDAP_DECODING_ERROR;
689 tmpDN[nrdns++] = newRDN;
692 assert (nrdns < TMP_SLOTS);
694 if ( p[ 0 ] == '\0' ) {
696 * the DN is over, phew
698 newDN = (LDAPDN *)LDAP_MALLOC( sizeof(LDAPDN) +
699 sizeof(LDAPRDN *) * (nrdns+1));
700 if ( newDN == NULL ) {
706 newDN[0] = (LDAPRDN **)(newDN+1);
708 if ( LDAP_DN_DCE( flags ) ) {
709 /* add in reversed order */
710 for ( i=0; i<nrdns; i++ )
711 newDN[0][i] = tmpDN[nrdns-1-i];
713 for ( i=0; i<nrdns; i++ )
714 newDN[0][i] = tmpDN[i];
716 newDN[0][nrdns] = NULL;
725 ldap_rdnfree( newRDN );
728 for (nrdns-- ;nrdns>=0; nrdns-- )
729 ldap_rdnfree( tmpDN[nrdns] );
732 Debug( LDAP_DEBUG_TRACE, "<= ldap_str2dn(%s,%u)=%d\n", str, flags, rc );
741 * Parses a relative DN according to flags up to a rdn separator
742 * or to the end of str.
743 * Returns the rdn and a pointer to the string continuation, which
744 * corresponds to the rdn separator or to '\0' in case the string is over.
747 ldap_str2rdn( const char *str, LDAPRDN **rdn, const char **n, unsigned flags )
752 int rc = LDAP_DECODING_ERROR;
753 int attrTypeEncoding = LDAP_AVA_STRING,
754 attrValueEncoding = LDAP_AVA_STRING;
756 struct berval attrType = { 0, NULL };
757 struct berval attrValue = { 0, NULL };
759 LDAPRDN *newRDN = NULL;
760 LDAPAVA *tmpRDN[TMP_SLOTS];
763 assert( rdn || flags & LDAP_DN_SKIP );
766 Debug( LDAP_DEBUG_TRACE, "=> ldap_str2rdn(%s,%u)\n%s", str, flags, "" );
773 switch ( LDAP_DN_FORMAT( flags ) ) {
774 case LDAP_DN_FORMAT_LDAP:
775 case LDAP_DN_FORMAT_LDAPV3:
776 case LDAP_DN_FORMAT_LDAPV2:
777 case LDAP_DN_FORMAT_DCE:
780 /* unsupported in str2dn */
781 case LDAP_DN_FORMAT_UFN:
782 case LDAP_DN_FORMAT_AD_CANONICAL:
783 return LDAP_PARAM_ERROR;
785 case LDAP_DN_FORMAT_LBER:
787 return LDAP_PARAM_ERROR;
790 if ( str[ 0 ] == '\0' ) {
795 for ( ; p[ 0 ] || state == GOTAVA; ) {
798 * The parser in principle advances one token a time,
799 * or toggles state if preferable.
804 * an AttributeType can be encoded as:
805 * - its string representation; in detail, implementations
806 * MUST recognize AttributeType string type names listed
807 * in section 2.3 of draft-ietf-ldapbis-dn-XX.txt, and
808 * MAY recognize other names.
809 * - its numeric OID (a dotted decimal string); in detail
810 * RFC 2253 asserts that ``Implementations MUST allow
811 * an oid in the attribute type to be prefixed by one
812 * of the character strings "oid." or "OID."''. As soon
813 * as draft-ietf-ldapbis-dn-XX.txt obsoletes RFC 2253
814 * I'm not sure whether this is required or not any
815 * longer; to be liberal, we still implement it.
818 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
819 if ( !LDAP_DN_ALLOW_ONE_SPACE( flags ) ) {
826 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
827 if ( !LDAP_DN_ALLOW_SPACES( flags ) ) {
832 /* whitespace is allowed (and trimmed) */
834 while ( p[ 0 ] && LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
839 /* error: we expected an AVA */
845 if ( LDAP_DN_OID_LEADCHAR( p[ 0 ] ) ) {
846 state = B4OIDATTRTYPE;
850 /* else must be alpha */
851 if ( !LDAP_DN_DESC_LEADCHAR( p[ 0 ] ) ) {
855 /* LDAPv2 "oid." prefix */
856 if ( LDAP_DN_LDAPV2( flags ) ) {
858 * to be overly pedantic, we only accept
861 if ( flags & LDAP_DN_PEDANTIC ) {
862 if ( !strncmp( p, "OID.", 4 )
863 || !strncmp( p, "oid.", 4 ) ) {
865 state = B4OIDATTRTYPE;
869 if ( !strncasecmp( p, "oid.", 4 ) ) {
871 state = B4OIDATTRTYPE;
877 state = B4STRINGATTRTYPE;
880 case B4OIDATTRTYPE: {
881 int err = LDAP_SUCCESS;
883 attrType.bv_val = ldap_int_parse_numericoid( &p, &err,
886 if ( err != LDAP_SUCCESS ) {
889 attrType.bv_len = p - attrType.bv_val;
891 attrTypeEncoding = LDAP_AVA_BINARY;
897 case B4STRINGATTRTYPE: {
898 const char *startPos, *endPos = NULL;
902 * the starting char has been found to be
903 * a LDAP_DN_DESC_LEADCHAR so we don't re-check it
904 * FIXME: DCE attr types seem to have a more
905 * restrictive syntax (no '-' ...)
907 for ( startPos = p++; p[ 0 ]; p++ ) {
908 if ( LDAP_DN_DESC_CHAR( p[ 0 ] ) ) {
912 if ( LDAP_DN_LANG_SEP( p[ 0 ] ) ) {
915 * RFC 2253 does not explicitly
916 * allow lang extensions to attribute
919 if ( flags & LDAP_DN_PEDANTIC ) {
924 * we trim ';' and following lang
925 * and so from attribute types
928 for ( ; LDAP_DN_ATTRDESC_CHAR( p[ 0 ] )
929 || LDAP_DN_LANG_SEP( p[ 0 ] ); p++ ) {
937 len = ( endPos ? endPos : p ) - startPos;
942 attrTypeEncoding = LDAP_AVA_STRING;
945 * here we need to decide whether to use it as is
946 * or turn it in OID form; as a consequence, we
947 * need to decide whether to binary encode the value
952 if ( flags & LDAP_DN_SKIP ) {
956 attrType.bv_val = (char *)startPos;
957 attrType.bv_len = len;
963 /* spaces may not be allowed */
964 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
965 if ( !LDAP_DN_ALLOW_SPACES( flags ) ) {
970 for ( p++; LDAP_DN_ASCII_SPACE( p[ 0 ] ); p++ ) {
975 /* need equal sign */
976 if ( !LDAP_DN_AVA_EQUALS( p[ 0 ] ) ) {
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++ ) {
994 * octothorpe means a BER encoded value will follow
995 * FIXME: I don't think DCE will allow it
997 if ( LDAP_DN_OCTOTHORPE( p[ 0 ] ) ) {
999 attrValueEncoding = LDAP_AVA_BINARY;
1000 state = B4BINARYVALUE;
1004 /* STRING value expected */
1007 * if we're pedantic, an attribute type in OID form
1008 * SHOULD imply a BER encoded attribute value; we
1009 * should at least issue a warning
1011 if ( ( flags & LDAP_DN_PEDANTIC )
1012 && ( attrTypeEncoding == LDAP_AVA_BINARY ) ) {
1013 /* OID attrType SHOULD use binary encoding */
1017 attrValueEncoding = LDAP_AVA_STRING;
1020 * LDAPv2 allows the attribute value to be quoted;
1021 * also, IA5 values are expected, in principle
1023 if ( LDAP_DN_LDAPV2( flags ) || LDAP_DN_LDAP( flags ) ) {
1024 if ( LDAP_DN_QUOTES( p[ 0 ] ) ) {
1026 state = B4IA5VALUEQUOTED;
1030 if ( LDAP_DN_LDAPV2( flags ) ) {
1037 * here STRING means RFC 2253 string
1038 * FIXME: what about DCE strings?
1040 state = B4STRINGVALUE;
1044 if ( hexstr2binval( p, &attrValue, &p, flags ) ) {
1052 switch ( LDAP_DN_FORMAT( flags ) ) {
1053 case LDAP_DN_FORMAT_LDAP:
1054 case LDAP_DN_FORMAT_LDAPV3:
1055 if ( str2strval( p, &attrValue, &p, flags,
1056 &attrValueEncoding ) ) {
1061 case LDAP_DN_FORMAT_DCE:
1062 if ( DCE2strval( p, &attrValue, &p, flags ) ) {
1075 if ( IA52strval( p, &attrValue, &p, flags ) ) {
1082 case B4IA5VALUEQUOTED:
1084 /* lead quote already stripped */
1085 if ( quotedIA52strval( p, &attrValue,
1096 if ( !( flags & LDAP_DN_SKIP ) ) {
1100 * we accept empty values
1102 ava = ldapava_new( &attrType, &attrValue,
1103 attrValueEncoding );
1104 if ( ava == NULL ) {
1105 rc = LDAP_NO_MEMORY;
1108 tmpRDN[navas++] = ava;
1110 assert(navas < TMP_SLOTS);
1114 * if we got an AVA separator ('+', or ',' for DCE )
1115 * we expect a new AVA for this RDN; otherwise
1116 * we add the RDN to the DN
1118 switch ( LDAP_DN_FORMAT( flags ) ) {
1119 case LDAP_DN_FORMAT_LDAP:
1120 case LDAP_DN_FORMAT_LDAPV3:
1121 case LDAP_DN_FORMAT_LDAPV2:
1122 if ( !LDAP_DN_AVA_SEP( p[ 0 ] ) ) {
1127 case LDAP_DN_FORMAT_DCE:
1128 if ( !LDAP_DN_AVA_SEP_DCE( p[ 0 ] ) ) {
1136 * the RDN is over, phew
1139 if ( !( flags & LDAP_DN_SKIP ) ) {
1140 newRDN = (LDAPRDN *)LDAP_MALLOC( sizeof(LDAPRDN)
1141 + sizeof(LDAPAVA *) * (navas+1) );
1142 if ( newRDN == NULL ) {
1143 rc = LDAP_NO_MEMORY;
1148 newRDN[0] = (LDAPAVA**)(newRDN+1);
1150 for (i=0; i<navas; i++)
1151 newRDN[0][i] = tmpRDN[i];
1152 newRDN[0][i] = NULL;
1160 /* they should have been used in an AVA */
1161 attrType.bv_val = NULL;
1162 attrValue.bv_val = NULL;
1177 /* They are set to NULL after they're used in an AVA */
1179 if ( attrValue.bv_val ) {
1180 free( attrValue.bv_val );
1183 for (navas-- ; navas>=0; navas-- )
1184 ldap_avafree( tmpRDN[navas] );
1188 Debug( LDAP_DEBUG_TRACE, "<= ldap_str2rdn(%*s)=%d\n",
1198 * reads in a UTF-8 string value, unescaping stuff:
1199 * '\' + LDAP_DN_NEEDESCAPE(c) -> 'c'
1200 * '\' + HEXPAIR(p) -> unhex(p)
1203 str2strval( const char *str, struct berval *val, const char **next, unsigned flags, unsigned *retFlags )
1205 const char *p, *startPos, *endPos = NULL;
1206 ber_len_t len, escapes, unescapes;
1214 for ( startPos = p = str, escapes = 0, unescapes = 0; p[ 0 ]; p++ ) {
1215 if ( LDAP_DN_ESCAPE( p[ 0 ] ) ) {
1217 if ( p[ 0 ] == '\0' ) {
1220 if ( ( p == startPos + 1 && LDAP_DN_NEEDESCAPE_LEAD( p[ 0 ] ) )
1221 || ( LDAP_DN_VALUE_END( p[ 1 ] ) && LDAP_DN_NEEDESCAPE_TRAIL( p[ 0 ] ) )
1222 || LDAP_DN_NEEDESCAPE( p[ 0 ] ) ) {
1227 if ( LDAP_DN_HEXPAIR( p ) ) {
1230 hexstr2bin( p, &c );
1233 if ( !LDAP_DN_ASCII_PRINTABLE( c ) ) {
1236 * we assume the string is UTF-8
1238 *retFlags = LDAP_AVA_NONPRINTABLE;
1245 if ( LDAP_DN_PEDANTIC & flags ) {
1249 * FIXME: we allow escaping
1250 * of chars that don't need
1251 * to and do not belong to
1252 * HEXDIGITS (we also allow
1253 * single hexdigit; maybe we
1258 } else if (!LDAP_DN_ASCII_PRINTABLE( p[ 0 ] ) ) {
1259 *retFlags = LDAP_AVA_NONPRINTABLE;
1261 } else if ( ( LDAP_DN_LDAP( flags ) && LDAP_DN_VALUE_END_V2( p[ 0 ] ) )
1262 || ( LDAP_DN_LDAPV3( flags ) && LDAP_DN_VALUE_END( p[ 0 ] ) ) ) {
1265 } else if ( LDAP_DN_NEEDESCAPE( p[ 0 ] ) ) {
1267 * FIXME: maybe we can add
1268 * escapes if not pedantic?
1275 * we do allow unescaped spaces at the end
1276 * of the value only in non-pedantic mode
1278 if ( p > startPos + 1 && LDAP_DN_ASCII_SPACE( p[ -1 ] ) &&
1279 !LDAP_DN_ESCAPE( p[ -2 ] ) ) {
1280 if ( flags & LDAP_DN_PEDANTIC ) {
1284 /* strip trailing (unescaped) spaces */
1285 for ( endPos = p - 1;
1286 endPos > startPos + 1 &&
1287 LDAP_DN_ASCII_SPACE( endPos[ -1 ] ) &&
1288 !LDAP_DN_ESCAPE( endPos[ -2 ] );
1295 if ( flags & LDAP_DN_SKIP ) {
1300 * FIXME: test memory?
1302 len = ( endPos ? endPos : p ) - startPos - escapes - unescapes;
1305 if ( escapes == 0 && unescapes == 0 ) {
1306 val->bv_val = LDAP_STRNDUP( startPos, len );
1311 val->bv_val = LDAP_MALLOC( len + 1 );
1312 for ( s = 0, d = 0; d < len; ) {
1313 if ( LDAP_DN_ESCAPE( startPos[ s ] ) ) {
1315 if ( ( s == 0 && LDAP_DN_NEEDESCAPE_LEAD( startPos[ s ] ) )
1316 || ( s == len - 1 && LDAP_DN_NEEDESCAPE_TRAIL( startPos[ s ] ) )
1317 || LDAP_DN_NEEDESCAPE( startPos[ s ] ) ) {
1318 val->bv_val[ d++ ] =
1321 } else if ( LDAP_DN_HEXPAIR( &startPos[ s ] ) ) {
1324 hexstr2bin( &startPos[ s ], &c );
1325 val->bv_val[ d++ ] = c;
1330 * we allow escaping of chars
1331 * that do not need to
1333 val->bv_val[ d++ ] =
1338 val->bv_val[ d++ ] = startPos[ s++ ];
1342 val->bv_val[ d ] = '\0';
1343 assert( strlen( val->bv_val ) == len );
1350 DCE2strval( const char *str, struct berval *val, const char **next, unsigned flags )
1352 const char *p, *startPos, *endPos = NULL;
1353 ber_len_t len, escapes;
1361 for ( startPos = p = str, escapes = 0; p[ 0 ]; p++ ) {
1362 if ( LDAP_DN_ESCAPE_DCE( p[ 0 ] ) ) {
1364 if ( LDAP_DN_NEEDESCAPE_DCE( p[ 0 ] ) ) {
1371 } else if ( LDAP_DN_VALUE_END_DCE( p[ 0 ] ) ) {
1376 * FIXME: can we accept anything else? I guess we need
1377 * to stop if a value is not legal
1382 * (unescaped) trailing spaces are trimmed must be silently ignored;
1385 if ( p > startPos + 1 && LDAP_DN_ASCII_SPACE( p[ -1 ] ) &&
1386 !LDAP_DN_ESCAPE( p[ -2 ] ) ) {
1387 if ( flags & LDAP_DN_PEDANTIC ) {
1391 /* strip trailing (unescaped) spaces */
1392 for ( endPos = p - 1;
1393 endPos > startPos + 1 &&
1394 LDAP_DN_ASCII_SPACE( endPos[ -1 ] ) &&
1395 !LDAP_DN_ESCAPE( endPos[ -2 ] );
1402 if ( flags & LDAP_DN_SKIP ) {
1406 len = ( endPos ? endPos : p ) - startPos - escapes;
1408 if ( escapes == 0 ){
1409 val->bv_val = LDAP_STRNDUP( startPos, len );
1414 val->bv_val = LDAP_MALLOC( len + 1 );
1415 for ( s = 0, d = 0; d < len; ) {
1417 * This point is reached only if escapes
1418 * are properly used, so all we need to
1421 if ( LDAP_DN_ESCAPE_DCE( startPos[ s ] ) ) {
1425 val->bv_val[ d++ ] = startPos[ s++ ];
1427 val->bv_val[ d ] = '\0';
1428 assert( strlen( val->bv_val ) == len );
1435 IA52strval( const char *str, struct berval *val, const char **next, unsigned flags )
1437 const char *p, *startPos, *endPos = NULL;
1438 ber_len_t len, escapes;
1450 for ( startPos = p = str, escapes = 0; p[ 0 ]; p++ ) {
1451 if ( LDAP_DN_ESCAPE( p[ 0 ] ) ) {
1453 if ( p[ 0 ] == '\0' ) {
1457 if ( !LDAP_DN_NEEDESCAPE( p[ 0 ] )
1458 && ( LDAP_DN_PEDANTIC & flags ) ) {
1463 } else if ( LDAP_DN_VALUE_END_V2( p[ 0 ] ) ) {
1468 * FIXME: can we accept anything else? I guess we need
1469 * to stop if a value is not legal
1473 /* strip trailing (unescaped) spaces */
1475 endPos > startPos + 1 &&
1476 LDAP_DN_ASCII_SPACE( endPos[ -1 ] ) &&
1477 !LDAP_DN_ESCAPE( endPos[ -2 ] );
1483 if ( flags & LDAP_DN_SKIP ) {
1487 len = ( endPos ? endPos : p ) - startPos - escapes;
1489 if ( escapes == 0 ) {
1490 val->bv_val = LDAP_STRNDUP( startPos, len );
1495 val->bv_val = LDAP_MALLOC( len + 1 );
1496 for ( s = 0, d = 0; d < len; ) {
1497 if ( LDAP_DN_ESCAPE( startPos[ s ] ) ) {
1500 val->bv_val[ d++ ] = startPos[ s++ ];
1502 val->bv_val[ d ] = '\0';
1503 assert( strlen( val->bv_val ) == len );
1510 quotedIA52strval( const char *str, struct berval *val, const char **next, unsigned flags )
1512 const char *p, *startPos, *endPos = NULL;
1514 unsigned escapes = 0;
1522 /* initial quote already eaten */
1523 for ( startPos = p = str; p[ 0 ]; p++ ) {
1525 * According to RFC 1779, the quoted value can
1526 * contain escaped as well as unescaped special values;
1527 * as a consequence we tolerate escaped values
1528 * (e.g. '"\,"' -> '\,') and escape unescaped specials
1529 * (e.g. '","' -> '\,').
1531 if ( LDAP_DN_ESCAPE( p[ 0 ] ) ) {
1532 if ( p[ 1 ] == '\0' ) {
1537 if ( !LDAP_DN_V2_PAIR( p[ 0 ] )
1538 && ( LDAP_DN_PEDANTIC & flags ) ) {
1540 * do we allow to escape normal chars?
1541 * LDAPv2 does not allow any mechanism
1542 * for escaping chars with '\' and hex
1549 } else if ( LDAP_DN_QUOTES( p[ 0 ] ) ) {
1551 /* eat closing quotes */
1557 * FIXME: can we accept anything else? I guess we need
1558 * to stop if a value is not legal
1562 if ( endPos == NULL ) {
1566 /* Strip trailing (unescaped) spaces */
1567 for ( ; p[ 0 ] && LDAP_DN_ASCII_SPACE( p[ 0 ] ); p++ ) {
1572 if ( flags & LDAP_DN_SKIP ) {
1576 len = endPos - startPos - escapes;
1579 if ( escapes == 0 ) {
1580 val->bv_val = LDAP_STRNDUP( startPos, len );
1585 val->bv_val = LDAP_MALLOC( len + 1 );
1588 for ( s = d = 0; d < len; ) {
1589 if ( LDAP_DN_ESCAPE( str[ s ] ) ) {
1592 val->bv_val[ d++ ] = str[ s++ ];
1594 val->bv_val[ d ] = '\0';
1595 assert( strlen( val->bv_val ) == len );
1602 hexstr2bin( const char *str, char *c )
1612 if ( LDAP_DN_ASCII_DIGIT( c1 ) ) {
1616 if ( LDAP_DN_ASCII_UCASE_HEXALPHA( c1 ) ) {
1619 assert( LDAP_DN_ASCII_LCASE_HEXALPHA( c1 ) );
1626 if ( LDAP_DN_ASCII_DIGIT( c2 ) ) {
1630 if ( LDAP_DN_ASCII_UCASE_HEXALPHA( c2 ) ) {
1631 *c += c2 - 'A' + 10;
1633 assert( LDAP_DN_ASCII_LCASE_HEXALPHA( c2 ) );
1634 *c += c2 - 'a' + 10;
1642 hexstr2binval( const char *str, struct berval *val, const char **next, unsigned flags )
1644 const char *p, *startPos, *endPos = NULL;
1654 for ( startPos = p = str; p[ 0 ]; p += 2 ) {
1655 switch ( LDAP_DN_FORMAT( flags ) ) {
1656 case LDAP_DN_FORMAT_LDAPV3:
1657 if ( LDAP_DN_VALUE_END( p[ 0 ] ) ) {
1662 case LDAP_DN_FORMAT_LDAP:
1663 case LDAP_DN_FORMAT_LDAPV2:
1664 if ( LDAP_DN_VALUE_END_V2( p[ 0 ] ) ) {
1669 case LDAP_DN_FORMAT_DCE:
1670 if ( LDAP_DN_VALUE_END_DCE( p[ 0 ] ) ) {
1676 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1677 if ( flags & LDAP_DN_PEDANTIC ) {
1682 for ( ; p[ 0 ]; p++ ) {
1683 switch ( LDAP_DN_FORMAT( flags ) ) {
1684 case LDAP_DN_FORMAT_LDAPV3:
1685 if ( LDAP_DN_VALUE_END( p[ 0 ] ) ) {
1690 case LDAP_DN_FORMAT_LDAP:
1691 case LDAP_DN_FORMAT_LDAPV2:
1692 if ( LDAP_DN_VALUE_END_V2( p[ 0 ] ) ) {
1697 case LDAP_DN_FORMAT_DCE:
1698 if ( LDAP_DN_VALUE_END_DCE( p[ 0 ] ) ) {
1707 if ( !LDAP_DN_HEXPAIR( p ) ) {
1715 if ( flags & LDAP_DN_SKIP ) {
1719 len = ( ( endPos ? endPos : p ) - startPos ) / 2;
1721 assert( 2 * len == (ber_len_t) (( endPos ? endPos : p ) - startPos ));
1724 val->bv_val = LDAP_MALLOC( len + 1 );
1725 if ( val->bv_val == NULL ) {
1726 return( LDAP_NO_MEMORY );
1729 for ( s = 0, d = 0; d < len; s += 2, d++ ) {
1732 hexstr2bin( &startPos[ s ], &c );
1734 val->bv_val[ d ] = c;
1737 val->bv_val[ d ] = '\0';
1743 * convert a byte in a hexadecimal pair
1746 byte2hexpair( const char *val, char *pair )
1748 static const char hexdig[] = "0123456789ABCDEF";
1754 * we assume the string has enough room for the hex encoding
1758 pair[ 0 ] = hexdig[ 0x0f & ( val[ 0 ] >> 4 ) ];
1759 pair[ 1 ] = hexdig[ 0x0f & val[ 0 ] ];
1765 * convert a binary value in hexadecimal pairs
1768 binval2hexstr( struct berval *val, char *str )
1775 if ( val->bv_len == 0 ) {
1780 * we assume the string has enough room for the hex encoding
1784 for ( s = 0, d = 0; s < val->bv_len; s++, d += 2 ) {
1785 byte2hexpair( &val->bv_val[ s ], &str[ d ] );
1792 * Length of the string representation, accounting for escaped hex
1796 strval2strlen( struct berval *val, unsigned flags, ber_len_t *len )
1798 ber_len_t l, cl = 1;
1800 int escaped_byte_len = LDAP_DN_IS_PRETTY( flags ) ? 1 : 3;
1801 #ifdef PRETTY_ESCAPE
1802 int escaped_ascii_len = LDAP_DN_IS_PRETTY( flags ) ? 2 : 3;
1803 #endif /* PRETTY_ESCAPE */
1809 if ( val->bv_len == 0 ) {
1813 for ( l = 0, p = val->bv_val; p[ 0 ]; p += cl ) {
1814 cl = LDAP_UTF8_CHARLEN( p );
1816 /* illegal utf-8 char! */
1819 } else if ( cl > 1 ) {
1822 for ( cnt = 1; cnt < cl; cnt++ ) {
1823 if ( ( p[ cnt ] & 0x80 ) == 0x00 ) {
1827 l += escaped_byte_len * cl;
1829 } else if ( LDAP_DN_NEEDESCAPE( p[ 0 ] )
1830 || ( p == val->bv_val && LDAP_DN_NEEDESCAPE_LEAD( p[ 0 ] ) )
1831 || ( !p[ 1 ] && LDAP_DN_NEEDESCAPE_TRAIL( p[ 0 ] ) ) ) {
1832 #ifdef PRETTY_ESCAPE
1833 if ( LDAP_DN_WILLESCAPE_HEX( flags, p[ 0 ] ) ) {
1836 * there might be some chars we want
1837 * to escape in form of a couple
1838 * of hexdigits for optimization purposes
1843 l += escaped_ascii_len;
1845 #else /* ! PRETTY_ESCAPE */
1847 #endif /* ! PRETTY_ESCAPE */
1860 * convert to string representation, escaping with hex the UTF-8 stuff;
1861 * assume the destination has enough room for escaping
1864 strval2str( struct berval *val, char *str, unsigned flags, ber_len_t *len )
1866 ber_len_t s, d, end;
1872 if ( val->bv_len == 0 ) {
1878 * we assume the string has enough room for the hex encoding
1881 for ( s = 0, d = 0, end = val->bv_len - 1; s < val->bv_len; ) {
1882 ber_len_t cl = LDAP_UTF8_CHARLEN( &val->bv_val[ s ] );
1885 * there might be some chars we want to escape in form
1886 * of a couple of hexdigits for optimization purposes
1888 if ( ( cl > 1 && !LDAP_DN_IS_PRETTY( flags ) )
1889 #ifdef PRETTY_ESCAPE
1890 || LDAP_DN_WILLESCAPE_HEX( flags, val->bv_val[ s ] )
1891 #else /* ! PRETTY_ESCAPE */
1892 || LDAP_DN_NEEDESCAPE( val->bv_val[ s ] )
1893 || ( d == 0 && LDAP_DN_NEEDESCAPE_LEAD( val->bv_val[ s ] ) )
1894 || ( s == end && LDAP_DN_NEEDESCAPE_TRAIL( val->bv_val[ s ] ) )
1896 #endif /* ! PRETTY_ESCAPE */
1900 byte2hexpair( &val->bv_val[ s ], &str[ d ] );
1905 } else if ( cl > 1 ) {
1907 str[ d++ ] = val->bv_val[ s++ ];
1911 #ifdef PRETTY_ESCAPE
1912 if ( LDAP_DN_NEEDESCAPE( val->bv_val[ s ] )
1913 || ( d == 0 && LDAP_DN_NEEDESCAPE_LEAD( val->bv_val[ s ] ) )
1914 || ( s == end && LDAP_DN_NEEDESCAPE_TRAIL( val->bv_val[ s ] ) ) ) {
1916 if ( !LDAP_DN_IS_PRETTY( flags ) ) {
1917 byte2hexpair( &val->bv_val[ s ], &str[ d ] );
1923 #endif /* PRETTY_ESCAPE */
1924 str[ d++ ] = val->bv_val[ s++ ];
1934 * Length of the IA5 string representation (no UTF-8 allowed)
1937 strval2IA5strlen( struct berval *val, unsigned flags, ber_len_t *len )
1946 if ( val->bv_len == 0 ) {
1950 if ( flags & LDAP_AVA_NONPRINTABLE ) {
1952 * Turn value into a binary encoded BER
1957 for ( l = 0, p = val->bv_val; p[ 0 ]; p++ ) {
1958 if ( LDAP_DN_NEEDESCAPE( p[ 0 ] )
1959 || ( p == val->bv_val && LDAP_DN_NEEDESCAPE_LEAD( p[ 0 ] ) )
1960 || ( !p[ 1 ] && LDAP_DN_NEEDESCAPE_TRAIL( p[ 0 ] ) ) ) {
1975 * convert to string representation (np UTF-8)
1976 * assume the destination has enough room for escaping
1979 strval2IA5str( struct berval *val, char *str, unsigned flags, ber_len_t *len )
1981 ber_len_t s, d, end;
1987 if ( val->bv_len == 0 ) {
1992 if ( flags & LDAP_AVA_NONPRINTABLE ) {
1994 * Turn value into a binary encoded BER
2001 * we assume the string has enough room for the hex encoding
2005 for ( s = 0, d = 0, end = val->bv_len - 1; s < val->bv_len; ) {
2006 if ( LDAP_DN_NEEDESCAPE( val->bv_val[ s ] )
2007 || ( s == 0 && LDAP_DN_NEEDESCAPE_LEAD( val->bv_val[ s ] ) )
2008 || ( s == end && LDAP_DN_NEEDESCAPE_TRAIL( val->bv_val[ s ] ) ) ) {
2011 str[ d++ ] = val->bv_val[ s++ ];
2021 * Length of the (supposedly) DCE string representation,
2022 * accounting for escaped hex of UTF-8 chars
2025 strval2DCEstrlen( struct berval *val, unsigned flags, ber_len_t *len )
2034 if ( val->bv_len == 0 ) {
2038 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2040 * FIXME: Turn the value into a binary encoded BER?
2045 for ( l = 0, p = val->bv_val; p[ 0 ]; p++ ) {
2046 if ( LDAP_DN_NEEDESCAPE_DCE( p[ 0 ] ) ) {
2061 * convert to (supposedly) DCE string representation,
2062 * escaping with hex the UTF-8 stuff;
2063 * assume the destination has enough room for escaping
2066 strval2DCEstr( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2074 if ( val->bv_len == 0 ) {
2079 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2081 * FIXME: Turn the value into a binary encoded BER?
2089 * we assume the string has enough room for the hex encoding
2093 for ( s = 0, d = 0; s < val->bv_len; ) {
2094 if ( LDAP_DN_NEEDESCAPE_DCE( val->bv_val[ s ] ) ) {
2097 str[ d++ ] = val->bv_val[ s++ ];
2107 * Length of the (supposedly) AD canonical string representation,
2108 * accounting for escaped hex of UTF-8 chars
2111 strval2ADstrlen( struct berval *val, unsigned flags, ber_len_t *len )
2120 if ( val->bv_len == 0 ) {
2124 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2126 * FIXME: Turn the value into a binary encoded BER?
2131 for ( l = 0, p = val->bv_val; p[ 0 ]; p++ ) {
2132 if ( LDAP_DN_NEEDESCAPE_AD( p[ 0 ] ) ) {
2147 * convert to (supposedly) AD string representation,
2148 * escaping with hex the UTF-8 stuff;
2149 * assume the destination has enough room for escaping
2152 strval2ADstr( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2160 if ( val->bv_len == 0 ) {
2165 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2167 * FIXME: Turn the value into a binary encoded BER?
2175 * we assume the string has enough room for the hex encoding
2179 for ( s = 0, d = 0; s < val->bv_len; ) {
2180 if ( LDAP_DN_NEEDESCAPE_AD( val->bv_val[ s ] ) ) {
2183 str[ d++ ] = val->bv_val[ s++ ];
2193 * If the DN is terminated by single-AVA RDNs with attribute type of "dc",
2194 * the first part of the AD representation of the DN is written in DNS
2195 * form, i.e. dot separated domain name components (as suggested
2196 * by Luke Howard, http://www.padl.com/~lukeh)
2199 dn2domain( LDAPDN *dn, struct berval *bv, int pos, int *iRDN )
2202 int domain = 0, first = 1;
2203 ber_len_t l = 1; /* we move the null also */
2206 /* we are guaranteed there's enough memory in str */
2212 assert( *iRDN >= 0 );
2214 str = bv->bv_val + pos;
2216 for ( i = *iRDN; i >= 0; i-- ) {
2220 assert( dn[ 0 ][ i ] );
2223 assert( rdn[ 0 ][ 0 ] );
2224 ava = rdn[ 0 ][ 0 ];
2226 if ( !LDAP_DN_IS_RDN_DC( rdn ) ) {
2234 AC_MEMCPY( str, ava->la_value.bv_val,
2235 ava->la_value.bv_len + 1);
2236 l += ava->la_value.bv_len;
2239 AC_MEMCPY( str + ava->la_value.bv_len + 1, bv->bv_val + pos, l);
2240 AC_MEMCPY( str, ava->la_value.bv_val,
2241 ava->la_value.bv_len );
2242 str[ ava->la_value.bv_len ] = '.';
2243 l += ava->la_value.bv_len + 1;
2248 bv->bv_len = pos + l - 1;
2254 rdn2strlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len,
2255 int ( *s2l )( struct berval *v, unsigned f, ber_len_t *l ) )
2262 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
2263 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
2265 /* len(type) + '=' + '+' | ',' */
2266 l += ava->la_attr.bv_len + 2;
2268 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2269 /* octothorpe + twice the length */
2270 l += 1 + 2 * ava->la_value.bv_len;
2274 unsigned f = flags | ava->la_flags;
2276 if ( ( *s2l )( &ava->la_value, f, &vl ) ) {
2289 rdn2str( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len,
2290 int ( *s2s ) ( struct berval *v, char * s, unsigned f, ber_len_t *l ) )
2295 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
2296 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
2298 AC_MEMCPY( &str[ l ], ava->la_attr.bv_val,
2299 ava->la_attr.bv_len );
2300 l += ava->la_attr.bv_len;
2304 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2306 if ( binval2hexstr( &ava->la_value, &str[ l ] ) ) {
2309 l += 2 * ava->la_value.bv_len;
2313 unsigned f = flags | ava->la_flags;
2315 if ( ( *s2s )( &ava->la_value, &str[ l ], f, &vl ) ) {
2320 str[ l++ ] = ( rdn[ 0 ][ iAVA + 1 ] ? '+' : ',' );
2329 rdn2DCEstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len )
2336 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
2337 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
2339 /* len(type) + '=' + ',' | '/' */
2340 l += ava->la_attr.bv_len + 2;
2342 switch ( ava->la_flags ) {
2343 case LDAP_AVA_BINARY:
2344 /* octothorpe + twice the length */
2345 l += 1 + 2 * ava->la_value.bv_len;
2348 case LDAP_AVA_STRING: {
2350 unsigned f = flags | ava->la_flags;
2352 if ( strval2DCEstrlen( &ava->la_value, f, &vl ) ) {
2370 rdn2DCEstr( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len, int first )
2375 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
2376 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
2381 str[ l++ ] = ( iAVA ? ',' : '/' );
2384 AC_MEMCPY( &str[ l ], ava->la_attr.bv_val,
2385 ava->la_attr.bv_len );
2386 l += ava->la_attr.bv_len;
2390 switch ( ava->la_flags ) {
2391 case LDAP_AVA_BINARY:
2393 if ( binval2hexstr( &ava->la_value, &str[ l ] ) ) {
2396 l += 2 * ava->la_value.bv_len;
2399 case LDAP_AVA_STRING: {
2401 unsigned f = flags | ava->la_flags;
2403 if ( strval2DCEstr( &ava->la_value, &str[ l ], f, &vl ) ) {
2421 rdn2UFNstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len )
2431 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
2432 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
2435 l += ( rdn[ 0 ][ iAVA + 1 ] ? 3 : 2 );
2437 /* FIXME: are binary values allowed in UFN? */
2438 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2439 /* octothorpe + twice the value */
2440 l += 1 + 2 * ava->la_value.bv_len;
2444 unsigned f = flags | ava->la_flags;
2446 if ( strval2strlen( &ava->la_value, f, &vl ) ) {
2459 rdn2UFNstr( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len )
2464 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
2465 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
2467 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2469 if ( binval2hexstr( &ava->la_value, &str[ l ] ) ) {
2472 l += 2 * ava->la_value.bv_len;
2476 unsigned f = flags | ava->la_flags;
2478 if ( strval2str( &ava->la_value, &str[ l ], f, &vl ) ) {
2484 if ( rdn[ 0 ][ iAVA + 1 ]) {
2485 AC_MEMCPY( &str[ l ], " + ", 3 );
2489 AC_MEMCPY( &str[ l ], ", ", 2 );
2500 rdn2ADstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len )
2510 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
2511 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
2516 /* FIXME: are binary values allowed in UFN? */
2517 switch ( ava->la_flags ) {
2518 case LDAP_AVA_BINARY:
2519 /* octothorpe + twice the value */
2520 l += 1 + 2 * ava->la_value.bv_len;
2523 case LDAP_AVA_STRING: {
2525 unsigned f = flags | ava->la_flags;
2527 if ( strval2ADstrlen( &ava->la_value, f, &vl ) ) {
2545 rdn2ADstr( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len, int first )
2550 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
2551 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
2556 str[ l++ ] = ( iAVA ? ',' : '/' );
2559 switch ( ava->la_flags ) {
2560 case LDAP_AVA_BINARY:
2562 if ( binval2hexstr( &ava->la_value, &str[ l ] ) ) {
2565 l += 2 * ava->la_value.bv_len;
2568 case LDAP_AVA_STRING: {
2570 unsigned f = flags | ava->la_flags;
2572 if ( strval2ADstr( &ava->la_value, &str[ l ], f, &vl ) ) {
2592 * Returns in str a string representation of rdn based on flags.
2593 * There is some duplication of code between this and ldap_dn2str;
2594 * this is wanted to reduce the allocation of temporary buffers.
2597 ldap_rdn2str( LDAPRDN *rdn, char **str, unsigned flags )
2604 if((flags & LDAP_DN_FORMAT_MASK) == LDAP_DN_FORMAT_LBER) {
2605 return LDAP_PARAM_ERROR;
2608 rc = ldap_rdn2bv( rdn, &bv, flags );
2614 ldap_rdn2bv( LDAPRDN *rdn, struct berval *bv, unsigned flags )
2624 if ( rdn == NULL ) {
2625 bv->bv_val = LDAP_STRDUP( "" );
2626 return( LDAP_SUCCESS );
2630 * This routine wastes "back" bytes at the end of the string
2633 switch ( LDAP_DN_FORMAT( flags ) ) {
2634 case LDAP_DN_FORMAT_LDAPV3:
2635 if ( rdn2strlen( rdn, flags, &l, strval2strlen ) ) {
2636 return LDAP_DECODING_ERROR;
2640 case LDAP_DN_FORMAT_LDAPV2:
2641 if ( rdn2strlen( rdn, flags, &l, strval2IA5strlen ) ) {
2642 return LDAP_DECODING_ERROR;
2646 case LDAP_DN_FORMAT_UFN:
2647 if ( rdn2UFNstrlen( rdn, flags, &l ) ) {
2648 return LDAP_DECODING_ERROR;
2652 case LDAP_DN_FORMAT_DCE:
2653 if ( rdn2DCEstrlen( rdn, flags, &l ) ) {
2654 return LDAP_DECODING_ERROR;
2658 case LDAP_DN_FORMAT_AD_CANONICAL:
2659 if ( rdn2ADstrlen( rdn, flags, &l ) ) {
2660 return LDAP_DECODING_ERROR;
2665 return( LDAP_PARAM_ERROR );
2668 bv->bv_val = LDAP_MALLOC( l + 1 );
2670 switch ( LDAP_DN_FORMAT( flags ) ) {
2671 case LDAP_DN_FORMAT_LDAPV3:
2672 rc = rdn2str( rdn, bv->bv_val, flags, &l, strval2str );
2676 case LDAP_DN_FORMAT_LDAPV2:
2677 rc = rdn2str( rdn, bv->bv_val, flags, &l, strval2IA5str );
2681 case LDAP_DN_FORMAT_UFN:
2682 rc = rdn2UFNstr( rdn, bv->bv_val, flags, &l );
2686 case LDAP_DN_FORMAT_DCE:
2687 rc = rdn2DCEstr( rdn, bv->bv_val, flags, &l, 1 );
2691 case LDAP_DN_FORMAT_AD_CANONICAL:
2692 rc = rdn2ADstr( rdn, bv->bv_val, flags, &l, 1 );
2697 /* need at least one of the previous */
2698 return LDAP_PARAM_ERROR;
2702 ldap_memfree( bv->bv_val );
2706 bv->bv_len = l - back;
2707 bv->bv_val[ bv->bv_len ] = '\0';
2709 return LDAP_SUCCESS;
2713 * Very bulk implementation; many optimizations can be performed
2714 * - a NULL dn results in an empty string ""
2717 * a) what do we do if a UTF-8 string must be converted in LDAPv2?
2718 * we must encode it in binary form ('#' + HEXPAIRs)
2719 * b) does DCE/AD support UTF-8?
2720 * no clue; don't think so.
2721 * c) what do we do when binary values must be converted in UTF/DCE/AD?
2722 * use binary encoded BER
2724 int ldap_dn2str( LDAPDN *dn, char **str, unsigned flags )
2731 if((flags & LDAP_DN_FORMAT_MASK) == LDAP_DN_FORMAT_LBER) {
2732 return LDAP_PARAM_ERROR;
2735 rc = ldap_dn2bv( dn, &bv, flags );
2740 int ldap_dn2bv( LDAPDN *dn, struct berval *bv, unsigned flags )
2743 int rc = LDAP_ENCODING_ERROR;
2746 /* stringifying helpers for LDAPv3/LDAPv2 */
2747 int ( *sv2l ) ( struct berval *v, unsigned f, ber_len_t *l );
2748 int ( *sv2s ) ( struct berval *v, char *s, unsigned f, ber_len_t *l );
2755 Debug( LDAP_DEBUG_TRACE, "=> ldap_dn2bv(%u)\n%s%s", flags, "", "" );
2758 * a null dn means an empty dn string
2759 * FIXME: better raise an error?
2762 bv->bv_val = LDAP_STRDUP( "" );
2763 return( LDAP_SUCCESS );
2766 switch ( LDAP_DN_FORMAT( flags ) ) {
2767 case LDAP_DN_FORMAT_LDAPV3:
2768 sv2l = strval2strlen;
2772 case LDAP_DN_FORMAT_LDAPV2:
2773 sv2l = strval2IA5strlen;
2774 sv2s = strval2IA5str;
2777 for ( iRDN = 0, len = 0; dn[ 0 ][ iRDN ]; iRDN++ ) {
2779 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
2781 if ( rdn2strlen( rdn, flags, &rdnl, sv2l ) ) {
2782 goto return_results;
2788 if ( ( bv->bv_val = LDAP_MALLOC( len + 1 ) ) == NULL ) {
2789 rc = LDAP_NO_MEMORY;
2793 for ( l = 0, iRDN = 0; dn[ 0 ][ iRDN ]; iRDN++ ) {
2795 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
2797 if ( rdn2str( rdn, &bv->bv_val[ l ], flags,
2799 LDAP_FREE( bv->bv_val );
2801 goto return_results;
2809 * trim the last ',' (the allocated memory
2810 * is one byte longer than required)
2812 bv->bv_len = len - 1;
2813 bv->bv_val[ bv->bv_len ] = '\0';
2818 case LDAP_DN_FORMAT_UFN: {
2821 * FIXME: quoting from RFC 1781:
2823 To take a distinguished name, and generate a name of this format with
2824 attribute types omitted, the following steps are followed.
2826 1. If the first attribute is of type CommonName, the type may be
2829 2. If the last attribute is of type Country, the type may be
2832 3. If the last attribute is of type Country, the last
2833 Organisation attribute may have the type omitted.
2835 4. All attributes of type OrganisationalUnit may have the type
2836 omitted, unless they are after an Organisation attribute or
2837 the first attribute is of type OrganisationalUnit.
2839 * this should be the pedantic implementation.
2841 * Here the standard implementation reflects
2842 * the one historically provided by OpenLDAP
2843 * (and UMIch, I presume), with the variant
2844 * of spaces and plusses (' + ') separating
2847 * A non-standard but nice implementation could
2848 * be to turn the final "dc" attributes into a
2849 * dot-separated domain.
2851 * Other improvements could involve the use of
2852 * friendly country names and so.
2855 int leftmost_dc = -1;
2857 #endif /* DC_IN_UFN */
2859 for ( iRDN = 0, len = 0; dn[ 0 ][ iRDN ]; iRDN++ ) {
2861 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
2863 if ( rdn2UFNstrlen( rdn, flags, &rdnl ) ) {
2864 goto return_results;
2869 if ( LDAP_DN_IS_RDN_DC( rdn ) ) {
2870 if ( leftmost_dc == -1 ) {
2876 #endif /* DC_IN_UFN */
2879 if ( ( bv->bv_val = LDAP_MALLOC( len + 1 ) ) == NULL ) {
2880 rc = LDAP_NO_MEMORY;
2885 if ( leftmost_dc == -1 ) {
2886 #endif /* DC_IN_UFN */
2887 for ( l = 0, iRDN = 0; dn[ 0 ][ iRDN ]; iRDN++ ) {
2889 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
2891 if ( rdn2UFNstr( rdn, &bv->bv_val[ l ],
2893 LDAP_FREE( bv->bv_val );
2895 goto return_results;
2901 * trim the last ', ' (the allocated memory
2902 * is two bytes longer than required)
2904 bv->bv_len = len - 2;
2905 bv->bv_val[ bv->bv_len ] = '\0';
2908 last_iRDN = iRDN - 1;
2910 for ( l = 0, iRDN = 0; iRDN < leftmost_dc; iRDN++ ) {
2912 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
2914 if ( rdn2UFNstr( rdn, &bv->bv_val[ l ],
2916 LDAP_FREE( bv->bv_val );
2918 goto return_results;
2923 if ( !dn2domain( dn, bv, l, &last_iRDN ) ) {
2924 LDAP_FREE( bv->bv_val );
2926 goto return_results;
2929 /* the string is correctly terminated by dn2domain */
2931 #endif /* DC_IN_UFN */
2937 case LDAP_DN_FORMAT_DCE:
2939 for ( iRDN = 0, len = 0; dn[ 0 ][ iRDN ]; iRDN++ ) {
2941 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
2943 if ( rdn2DCEstrlen( rdn, flags, &rdnl ) ) {
2944 goto return_results;
2950 if ( ( bv->bv_val = LDAP_MALLOC( len + 1 ) ) == NULL ) {
2951 rc = LDAP_NO_MEMORY;
2955 for ( l = 0; iRDN--; ) {
2957 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
2959 if ( rdn2DCEstr( rdn, &bv->bv_val[ l ], flags,
2961 LDAP_FREE( bv->bv_val );
2963 goto return_results;
2971 bv->bv_val[ bv->bv_len ] = '\0';
2976 case LDAP_DN_FORMAT_AD_CANONICAL: {
2979 * Sort of UFN for DCE DNs: a slash ('/') separated
2980 * global->local DN with no types; strictly speaking,
2981 * the naming context should be a domain, which is
2982 * written in DNS-style, e.g. dot-deparated.
2986 * "givenName=Bill+sn=Gates,ou=People,dc=microsoft,dc=com"
2990 * "microsoft.com/People/Bill,Gates"
2992 for ( iRDN = 0, len = -1; dn[ 0 ][ iRDN ]; iRDN++ ) {
2994 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
2996 if ( rdn2ADstrlen( rdn, flags, &rdnl ) ) {
2997 goto return_results;
3003 if ( ( bv->bv_val = LDAP_MALLOC( len + 1 ) ) == NULL ) {
3004 rc = LDAP_NO_MEMORY;
3009 if ( iRDN && dn2domain( dn, bv, 0, &iRDN ) ) {
3010 for ( l = bv->bv_len; iRDN >= 0 ; iRDN-- ) {
3012 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
3014 if ( rdn2ADstr( rdn, &bv->bv_val[ l ],
3015 flags, &rdnl, 0 ) ) {
3016 LDAP_FREE( bv->bv_val );
3018 goto return_results;
3027 * Strictly speaking, AD canonical requires
3028 * a DN to be in the form "..., dc=smtg",
3029 * i.e. terminated by a domain component
3031 if ( flags & LDAP_DN_PEDANTIC ) {
3032 LDAP_FREE( bv->bv_val );
3034 rc = LDAP_ENCODING_ERROR;
3038 for ( l = 0; iRDN >= 0 ; iRDN-- ) {
3040 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
3042 if ( rdn2ADstr( rdn, &bv->bv_val[ l ],
3043 flags, &rdnl, first ) ) {
3044 LDAP_FREE( bv->bv_val );
3046 goto return_results;
3056 bv->bv_val[ bv->bv_len ] = '\0';
3063 return LDAP_PARAM_ERROR;
3066 Debug( LDAP_DEBUG_TRACE, "<= ldap_dn2bv(%s,%u)=%d\n",
3067 bv->bv_val, flags, rc );