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_NEEDESCAPE(c) \
388 ( LDAP_DN_ESCAPE(c) || LDAP_DN_NE(c) )
389 #define LDAP_DN_NEEDESCAPE_LEAD(c) \
390 ( LDAP_DN_ASCII_SPACE(c) || LDAP_DN_OCTOTHORPE(c) || LDAP_DN_NE(c) )
391 #define LDAP_DN_NEEDESCAPE_TRAIL(c) \
392 ( LDAP_DN_ASCII_SPACE(c) || LDAP_DN_NEEDESCAPE(c) )
393 #define LDAP_DN_WILLESCAPE_CHAR(c) \
394 ( LDAP_DN_RDN_SEP(c) || LDAP_DN_AVA_SEP(c) || LDAP_DN_ESCAPE(c) )
395 #define LDAP_DN_IS_PRETTY(f) ( (f) & LDAP_DN_PRETTY )
396 #define LDAP_DN_WILLESCAPE_HEX(f, c) \
397 ( ( !LDAP_DN_IS_PRETTY( f ) ) && LDAP_DN_WILLESCAPE_CHAR(c) )
400 #define LDAP_DN_VALUE_END_V2(c) \
401 ( LDAP_DN_RDN_SEP_V2(c) || LDAP_DN_AVA_SEP(c) )
403 #define LDAP_DN_V2_SPECIAL(c) \
404 ( LDAP_DN_RDN_SEP_V2(c) || LDAP_DN_AVA_EQUALS(c) \
405 || LDAP_DN_AVA_SEP(c) || (c) == '<' || (c) == '>' \
406 || LDAP_DN_OCTOTHORPE(c) )
407 #define LDAP_DN_V2_PAIR(c) \
408 ( LDAP_DN_V2_SPECIAL(c) || LDAP_DN_ESCAPE(c) || LDAP_DN_QUOTES(c) )
411 * DCE (mostly from Luke Howard and IBM implementation for AIX)
413 * From: "Application Development Guide - Directory Services" (FIXME: add link?)
414 * Here escapes and valid chars for GDS are considered; as soon as more
415 * specific info is found, the macros will be updated.
417 * Chars: 'a'-'z', 'A'-'Z', '0'-'9',
418 * '.', ':', ',', ''', '+', '-', '=', '(', ')', '?', '/', ' '.
420 * Metachars: '/', ',', '=', '\'.
422 * the '\' is used to escape other metachars.
428 * Attribute types must start with alphabetic chars and can contain
429 * alphabetic chars and digits (FIXME: no '-'?). OIDs are allowed.
431 #define LDAP_DN_RDN_SEP_DCE(c) ( (c) == '/' )
432 #define LDAP_DN_AVA_SEP_DCE(c) ( (c) == ',' )
433 #define LDAP_DN_ESCAPE_DCE(c) ( LDAP_DN_ESCAPE(c) )
434 #define LDAP_DN_VALUE_END_DCE(c) \
435 ( LDAP_DN_RDN_SEP_DCE(c) || LDAP_DN_AVA_SEP_DCE(c) )
436 #define LDAP_DN_NEEDESCAPE_DCE(c) \
437 ( LDAP_DN_VALUE_END_DCE(c) || LDAP_DN_AVA_EQUALS(c) )
440 #define LDAP_DN_RDN_SEP_AD(c) ( (c) == '/' )
441 #define LDAP_DN_ESCAPE_AD(c) ( LDAP_DN_ESCAPE(c) )
442 #define LDAP_DN_AVA_SEP_AD(c) ( (c) == ',' ) /* assume same as DCE */
443 #define LDAP_DN_VALUE_END_AD(c) \
444 ( LDAP_DN_RDN_SEP_AD(c) || LDAP_DN_AVA_SEP_AD(c) )
445 #define LDAP_DN_NEEDESCAPE_AD(c) \
446 ( LDAP_DN_VALUE_END_AD(c) || LDAP_DN_AVA_EQUALS(c) )
449 #define LDAP_DN_HEXPAIR(s) \
450 ( LDAP_DN_ASCII_HEXDIGIT((s)[0]) && LDAP_DN_ASCII_HEXDIGIT((s)[1]) )
451 /* better look at the AttributeDescription? */
453 /* FIXME: no composite rdn or non-"dc" types, right?
454 * (what about "dc" in OID form?) */
455 /* FIXME: we do not allow binary values in domain, right? */
456 /* NOTE: use this macro only when ABSOLUTELY SURE rdn IS VALID! */
457 /* NOTE: don't use strcasecmp() as it is locale specific! */
458 #define LDAP_DC_ATTR "dc"
459 #define LDAP_DC_ATTRU "DC"
460 #define LDAP_DN_IS_RDN_DC( r ) \
461 ( (r) && (r)[0][0] && !(r)[0][1] \
462 && ((r)[0][0]->la_flags == LDAP_AVA_STRING) \
463 && ((r)[0][0]->la_attr.bv_len == 2) \
464 && (((r)[0][0]->la_attr.bv_val[0] == LDAP_DC_ATTR[0]) \
465 || ((r)[0][0]->la_attr.bv_val[0] == LDAP_DC_ATTRU[0])) \
466 && (((r)[0][0]->la_attr.bv_val[1] == LDAP_DC_ATTR[1]) \
467 || ((r)[0][0]->la_attr.bv_val[1] == LDAP_DC_ATTRU[1])))
469 /* Composite rules */
470 #define LDAP_DN_ALLOW_ONE_SPACE(f) \
471 ( LDAP_DN_LDAPV2(f) \
472 || !( (f) & LDAP_DN_P_NOSPACEAFTERRDN ) )
473 #define LDAP_DN_ALLOW_SPACES(f) \
474 ( LDAP_DN_LDAPV2(f) \
475 || !( (f) & ( LDAP_DN_P_NOLEADTRAILSPACES | LDAP_DN_P_NOSPACEAFTERRDN ) ) )
476 #define LDAP_DN_LDAP(f) \
477 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_LDAP )
478 #define LDAP_DN_LDAPV3(f) \
479 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_LDAPV3 )
480 #define LDAP_DN_LDAPV2(f) \
481 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_LDAPV2 )
482 #define LDAP_DN_DCE(f) \
483 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_DCE )
484 #define LDAP_DN_UFN(f) \
485 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_UFN )
486 #define LDAP_DN_ADC(f) \
487 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_AD_CANONICAL )
488 #define LDAP_DN_FORMAT(f) ( (f) & LDAP_DN_FORMAT_MASK )
491 * LDAPAVA helpers (will become part of the API for operations
492 * on structural representations of DNs).
495 ldapava_new( const struct berval *attr, const struct berval *val,
503 ava = LDAP_MALLOC( sizeof( LDAPAVA ) + attr->bv_len + 1 );
505 /* should we test it? */
510 ava->la_attr.bv_len = attr->bv_len;
511 ava->la_attr.bv_val = (char *)(ava+1);
512 AC_MEMCPY( ava->la_attr.bv_val, attr->bv_val, attr->bv_len );
513 ava->la_attr.bv_val[attr->bv_len] = '\0';
515 ava->la_value = *val;
516 ava->la_flags = flags;
518 ava->la_private = NULL;
524 ldap_avafree( LDAPAVA *ava )
529 /* ava's private must be freed by caller
530 * (at present let's skip this check because la_private
531 * basically holds static data) */
532 assert( ava->la_private == NULL );
536 /* la_attr is now contiguous with ava, not freed separately */
537 LDAP_FREE( ava->la_attr.bv_val );
539 LDAP_FREE( ava->la_value.bv_val );
545 ldap_rdnfree( LDAPRDN *rdn )
553 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
554 ldap_avafree( rdn[ 0 ][ iAVA ] );
561 ldap_dnfree( LDAPDN *dn )
569 for ( iRDN = 0; dn[ 0 ][ iRDN ]; iRDN++ ) {
570 ldap_rdnfree( dn[ 0 ][ iRDN ] );
577 * Converts a string representation of a DN (in LDAPv3, LDAPv2 or DCE)
578 * into a structural representation of the DN, by separating attribute
579 * types and values encoded in the more appropriate form, which is
580 * string or OID for attribute types and binary form of the BER encoded
581 * value or Unicode string. Formats different from LDAPv3 are parsed
582 * according to their own rules and turned into the more appropriate
583 * form according to LDAPv3.
585 * NOTE: I realize the code is getting spaghettish; it is rather
586 * experimental and will hopefully turn into something more simple
587 * and readable as soon as it works as expected.
590 #define TMP_SLOTS 1024
593 ldap_str2dn( LDAP_CONST char *str, LDAPDN **dn, unsigned flags )
596 int rc = LDAP_DECODING_ERROR;
599 LDAPDN *newDN = NULL;
600 LDAPRDN *newRDN = NULL, *tmpDN[TMP_SLOTS];
605 Debug( LDAP_DEBUG_TRACE, "=> ldap_str2dn(%s,%u)\n%s", str, flags, "" );
609 switch ( LDAP_DN_FORMAT( flags ) ) {
610 case LDAP_DN_FORMAT_LDAP:
611 case LDAP_DN_FORMAT_LDAPV3:
612 case LDAP_DN_FORMAT_LDAPV2:
613 case LDAP_DN_FORMAT_DCE:
616 /* unsupported in str2dn */
617 case LDAP_DN_FORMAT_UFN:
618 case LDAP_DN_FORMAT_AD_CANONICAL:
619 return LDAP_PARAM_ERROR;
621 case LDAP_DN_FORMAT_LBER:
623 return LDAP_PARAM_ERROR;
626 if ( str[ 0 ] == '\0' ) {
631 if ( LDAP_DN_DCE( flags ) ) {
634 * (from Luke Howard: thnx) A RDN separator is required
635 * at the beginning of an (absolute) DN.
637 if ( !LDAP_DN_RDN_SEP_DCE( p[ 0 ] ) ) {
642 } else if ( LDAP_DN_LDAP( flags ) ) {
644 * if dn starts with '/' let's make it a DCE dn
646 if ( LDAP_DN_RDN_SEP_DCE( p[ 0 ] ) ) {
647 flags |= LDAP_DN_FORMAT_DCE;
652 for ( ; p[ 0 ]; p++ ) {
655 err = ldap_str2rdn( p, &newRDN, (char **) &p, flags );
656 if ( err != LDAP_SUCCESS ) {
661 * We expect a rdn separator
664 switch ( LDAP_DN_FORMAT( flags ) ) {
665 case LDAP_DN_FORMAT_LDAPV3:
666 if ( !LDAP_DN_RDN_SEP( p[ 0 ] ) ) {
667 rc = LDAP_DECODING_ERROR;
672 case LDAP_DN_FORMAT_LDAP:
673 case LDAP_DN_FORMAT_LDAPV2:
674 if ( !LDAP_DN_RDN_SEP_V2( p[ 0 ] ) ) {
675 rc = LDAP_DECODING_ERROR;
680 case LDAP_DN_FORMAT_DCE:
681 if ( !LDAP_DN_RDN_SEP_DCE( p[ 0 ] ) ) {
682 rc = LDAP_DECODING_ERROR;
690 tmpDN[nrdns++] = newRDN;
697 assert (nrdns < TMP_SLOTS);
700 * make the static AVA array dynamically rescalable
702 if (nrdns >= TMP_SLOTS) {
703 rc = LDAP_DECODING_ERROR;
708 if ( p[ 0 ] == '\0' ) {
710 * the DN is over, phew
712 newDN = (LDAPDN *)LDAP_MALLOC( sizeof(LDAPDN) +
713 sizeof(LDAPRDN *) * (nrdns+1));
714 if ( newDN == NULL ) {
720 newDN[0] = (LDAPRDN **)(newDN+1);
722 if ( LDAP_DN_DCE( flags ) ) {
723 /* add in reversed order */
724 for ( i=0; i<nrdns; i++ )
725 newDN[0][i] = tmpDN[nrdns-1-i];
727 for ( i=0; i<nrdns; i++ )
728 newDN[0][i] = tmpDN[i];
730 newDN[0][nrdns] = NULL;
739 ldap_rdnfree( newRDN );
742 for (nrdns-- ;nrdns>=0; nrdns-- )
743 ldap_rdnfree( tmpDN[nrdns] );
746 Debug( LDAP_DEBUG_TRACE, "<= ldap_str2dn(%s,%u)=%d\n", str, flags, rc );
755 * Parses a relative DN according to flags up to a rdn separator
756 * or to the end of str.
757 * Returns the rdn and a pointer to the string continuation, which
758 * corresponds to the rdn separator or to '\0' in case the string is over.
761 ldap_str2rdn( LDAP_CONST char *str, LDAPRDN **rdn,
762 char **n_in, unsigned flags )
764 const char **n = (const char **) n_in;
768 int rc = LDAP_DECODING_ERROR;
769 int attrTypeEncoding = LDAP_AVA_STRING,
770 attrValueEncoding = LDAP_AVA_STRING;
772 struct berval attrType = { 0, NULL };
773 struct berval attrValue = { 0, NULL };
775 LDAPRDN *newRDN = NULL;
776 LDAPAVA *tmpRDN[TMP_SLOTS];
779 assert( rdn || flags & LDAP_DN_SKIP );
782 Debug( LDAP_DEBUG_TRACE, "=> ldap_str2rdn(%s,%u)\n%s", str, flags, "" );
789 switch ( LDAP_DN_FORMAT( flags ) ) {
790 case LDAP_DN_FORMAT_LDAP:
791 case LDAP_DN_FORMAT_LDAPV3:
792 case LDAP_DN_FORMAT_LDAPV2:
793 case LDAP_DN_FORMAT_DCE:
796 /* unsupported in str2dn */
797 case LDAP_DN_FORMAT_UFN:
798 case LDAP_DN_FORMAT_AD_CANONICAL:
799 return LDAP_PARAM_ERROR;
801 case LDAP_DN_FORMAT_LBER:
803 return LDAP_PARAM_ERROR;
806 if ( str[ 0 ] == '\0' ) {
811 for ( ; p[ 0 ] || state == GOTAVA; ) {
814 * The parser in principle advances one token a time,
815 * or toggles state if preferable.
820 * an AttributeType can be encoded as:
821 * - its string representation; in detail, implementations
822 * MUST recognize AttributeType string type names listed
823 * in section 2.3 of draft-ietf-ldapbis-dn-XX.txt, and
824 * MAY recognize other names.
825 * - its numeric OID (a dotted decimal string); in detail
826 * RFC 2253 asserts that ``Implementations MUST allow
827 * an oid in the attribute type to be prefixed by one
828 * of the character strings "oid." or "OID."''. As soon
829 * as draft-ietf-ldapbis-dn-XX.txt obsoletes RFC 2253
830 * I'm not sure whether this is required or not any
831 * longer; to be liberal, we still implement it.
834 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
835 if ( !LDAP_DN_ALLOW_ONE_SPACE( flags ) ) {
842 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
843 if ( !LDAP_DN_ALLOW_SPACES( flags ) ) {
848 /* whitespace is allowed (and trimmed) */
850 while ( p[ 0 ] && LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
855 /* error: we expected an AVA */
861 if ( LDAP_DN_OID_LEADCHAR( p[ 0 ] ) ) {
862 state = B4OIDATTRTYPE;
866 /* else must be alpha */
867 if ( !LDAP_DN_DESC_LEADCHAR( p[ 0 ] ) ) {
871 /* LDAPv2 "oid." prefix */
872 if ( LDAP_DN_LDAPV2( flags ) ) {
874 * to be overly pedantic, we only accept
877 if ( flags & LDAP_DN_PEDANTIC ) {
878 if ( !strncmp( p, "OID.", 4 )
879 || !strncmp( p, "oid.", 4 ) ) {
881 state = B4OIDATTRTYPE;
885 if ( !strncasecmp( p, "oid.", 4 ) ) {
887 state = B4OIDATTRTYPE;
893 state = B4STRINGATTRTYPE;
896 case B4OIDATTRTYPE: {
897 int err = LDAP_SUCCESS;
899 attrType.bv_val = ldap_int_parse_numericoid( &p, &err,
902 if ( err != LDAP_SUCCESS ) {
905 attrType.bv_len = p - attrType.bv_val;
907 attrTypeEncoding = LDAP_AVA_BINARY;
913 case B4STRINGATTRTYPE: {
914 const char *startPos, *endPos = NULL;
918 * the starting char has been found to be
919 * a LDAP_DN_DESC_LEADCHAR so we don't re-check it
920 * FIXME: DCE attr types seem to have a more
921 * restrictive syntax (no '-' ...)
923 for ( startPos = p++; p[ 0 ]; p++ ) {
924 if ( LDAP_DN_DESC_CHAR( p[ 0 ] ) ) {
928 if ( LDAP_DN_LANG_SEP( p[ 0 ] ) ) {
931 * RFC 2253 does not explicitly
932 * allow lang extensions to attribute
935 if ( flags & LDAP_DN_PEDANTIC ) {
940 * we trim ';' and following lang
941 * and so from attribute types
944 for ( ; LDAP_DN_ATTRDESC_CHAR( p[ 0 ] )
945 || LDAP_DN_LANG_SEP( p[ 0 ] ); p++ ) {
953 len = ( endPos ? endPos : p ) - startPos;
958 attrTypeEncoding = LDAP_AVA_STRING;
961 * here we need to decide whether to use it as is
962 * or turn it in OID form; as a consequence, we
963 * need to decide whether to binary encode the value
968 if ( flags & LDAP_DN_SKIP ) {
972 attrType.bv_val = (char *)startPos;
973 attrType.bv_len = len;
979 /* spaces may not be allowed */
980 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
981 if ( !LDAP_DN_ALLOW_SPACES( flags ) ) {
986 for ( p++; LDAP_DN_ASCII_SPACE( p[ 0 ] ); p++ ) {
991 /* need equal sign */
992 if ( !LDAP_DN_AVA_EQUALS( p[ 0 ] ) ) {
997 /* spaces may not be allowed */
998 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
999 if ( !LDAP_DN_ALLOW_SPACES( flags ) ) {
1004 for ( p++; LDAP_DN_ASCII_SPACE( p[ 0 ] ); p++ ) {
1010 * octothorpe means a BER encoded value will follow
1011 * FIXME: I don't think DCE will allow it
1013 if ( LDAP_DN_OCTOTHORPE( p[ 0 ] ) ) {
1015 attrValueEncoding = LDAP_AVA_BINARY;
1016 state = B4BINARYVALUE;
1020 /* STRING value expected */
1023 * if we're pedantic, an attribute type in OID form
1024 * SHOULD imply a BER encoded attribute value; we
1025 * should at least issue a warning
1027 if ( ( flags & LDAP_DN_PEDANTIC )
1028 && ( attrTypeEncoding == LDAP_AVA_BINARY ) ) {
1029 /* OID attrType SHOULD use binary encoding */
1033 attrValueEncoding = LDAP_AVA_STRING;
1036 * LDAPv2 allows the attribute value to be quoted;
1037 * also, IA5 values are expected, in principle
1039 if ( LDAP_DN_LDAPV2( flags ) || LDAP_DN_LDAP( flags ) ) {
1040 if ( LDAP_DN_QUOTES( p[ 0 ] ) ) {
1042 state = B4IA5VALUEQUOTED;
1046 if ( LDAP_DN_LDAPV2( flags ) ) {
1053 * here STRING means RFC 2253 string
1054 * FIXME: what about DCE strings?
1060 state = B4STRINGVALUE;
1065 if ( hexstr2binval( p, &attrValue, &p, flags ) ) {
1073 switch ( LDAP_DN_FORMAT( flags ) ) {
1074 case LDAP_DN_FORMAT_LDAP:
1075 case LDAP_DN_FORMAT_LDAPV3:
1076 if ( str2strval( p, &attrValue, &p, flags,
1077 &attrValueEncoding ) ) {
1082 case LDAP_DN_FORMAT_DCE:
1083 if ( DCE2strval( p, &attrValue, &p, flags ) ) {
1096 if ( IA52strval( p, &attrValue, &p, flags ) ) {
1103 case B4IA5VALUEQUOTED:
1105 /* lead quote already stripped */
1106 if ( quotedIA52strval( p, &attrValue,
1117 if ( !( flags & LDAP_DN_SKIP ) ) {
1121 * we accept empty values
1123 ava = ldapava_new( &attrType, &attrValue,
1124 attrValueEncoding );
1126 if ( ava == NULL ) {
1127 rc = LDAP_NO_MEMORY;
1130 tmpRDN[navas++] = ava;
1132 attrValue.bv_val = NULL;
1133 attrValue.bv_len = 0;
1135 assert(navas < TMP_SLOTS);
1139 * if we got an AVA separator ('+', or ',' for DCE )
1140 * we expect a new AVA for this RDN; otherwise
1141 * we add the RDN to the DN
1143 switch ( LDAP_DN_FORMAT( flags ) ) {
1144 case LDAP_DN_FORMAT_LDAP:
1145 case LDAP_DN_FORMAT_LDAPV3:
1146 case LDAP_DN_FORMAT_LDAPV2:
1147 if ( !LDAP_DN_AVA_SEP( p[ 0 ] ) ) {
1152 case LDAP_DN_FORMAT_DCE:
1153 if ( !LDAP_DN_AVA_SEP_DCE( p[ 0 ] ) ) {
1161 * the RDN is over, phew
1164 if ( !( flags & LDAP_DN_SKIP ) ) {
1165 newRDN = (LDAPRDN *)LDAP_MALLOC( sizeof(LDAPRDN)
1166 + sizeof(LDAPAVA *) * (navas+1) );
1167 if ( newRDN == NULL ) {
1168 rc = LDAP_NO_MEMORY;
1173 newRDN[0] = (LDAPAVA**)(newRDN+1);
1175 for (i=0; i<navas; i++)
1176 newRDN[0][i] = tmpRDN[i];
1177 newRDN[0][i] = NULL;
1185 /* they should have been used in an AVA */
1186 attrType.bv_val = NULL;
1187 attrValue.bv_val = NULL;
1202 /* They are set to NULL after they're used in an AVA */
1204 if ( attrValue.bv_val ) {
1205 free( attrValue.bv_val );
1208 for (navas-- ; navas>=0; navas-- )
1209 ldap_avafree( tmpRDN[navas] );
1213 Debug( LDAP_DEBUG_TRACE, "<= ldap_str2rdn(%*s)=%d\n",
1223 * reads in a UTF-8 string value, unescaping stuff:
1224 * '\' + LDAP_DN_NEEDESCAPE(c) -> 'c'
1225 * '\' + HEXPAIR(p) -> unhex(p)
1228 str2strval( const char *str, struct berval *val, const char **next, unsigned flags, unsigned *retFlags )
1230 const char *p, *startPos, *endPos = NULL;
1231 ber_len_t len, escapes;
1239 for ( startPos = p = str, escapes = 0; p[ 0 ]; p++ ) {
1240 if ( LDAP_DN_ESCAPE( p[ 0 ] ) ) {
1242 if ( p[ 0 ] == '\0' ) {
1245 if ( ( p == startPos + 1 && LDAP_DN_NEEDESCAPE_LEAD( p[ 0 ] ) )
1246 || ( LDAP_DN_VALUE_END( p[ 1 ] ) && LDAP_DN_NEEDESCAPE_TRAIL( p[ 0 ] ) )
1247 || LDAP_DN_NEEDESCAPE( 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 ( ( s == 0 && LDAP_DN_NEEDESCAPE_LEAD( startPos[ s ] ) )
1344 || ( s == len - 1 && LDAP_DN_NEEDESCAPE_TRAIL( startPos[ s ] ) )
1345 || LDAP_DN_NEEDESCAPE( startPos[ s ] ) ) {
1346 val->bv_val[ d++ ] =
1349 } else if ( LDAP_DN_HEXPAIR( &startPos[ s ] ) ) {
1352 hexstr2bin( &startPos[ s ], &c );
1353 val->bv_val[ d++ ] = c;
1357 /* we should never get here */
1362 val->bv_val[ d++ ] = startPos[ s++ ];
1366 val->bv_val[ d ] = '\0';
1367 assert( strlen( val->bv_val ) == len );
1374 DCE2strval( const char *str, struct berval *val, const char **next, unsigned flags )
1376 const char *p, *startPos, *endPos = NULL;
1377 ber_len_t len, escapes;
1385 for ( startPos = p = str, escapes = 0; p[ 0 ]; p++ ) {
1386 if ( LDAP_DN_ESCAPE_DCE( p[ 0 ] ) ) {
1388 if ( LDAP_DN_NEEDESCAPE_DCE( p[ 0 ] ) ) {
1395 } else if ( LDAP_DN_VALUE_END_DCE( p[ 0 ] ) ) {
1400 * FIXME: can we accept anything else? I guess we need
1401 * to stop if a value is not legal
1406 * (unescaped) trailing spaces are trimmed must be silently ignored;
1409 if ( p > startPos + 1 && LDAP_DN_ASCII_SPACE( p[ -1 ] ) &&
1410 !LDAP_DN_ESCAPE( p[ -2 ] ) ) {
1411 if ( flags & LDAP_DN_PEDANTIC ) {
1415 /* strip trailing (unescaped) spaces */
1416 for ( endPos = p - 1;
1417 endPos > startPos + 1 &&
1418 LDAP_DN_ASCII_SPACE( endPos[ -1 ] ) &&
1419 !LDAP_DN_ESCAPE( endPos[ -2 ] );
1426 if ( flags & LDAP_DN_SKIP ) {
1430 len = ( endPos ? endPos : p ) - startPos - escapes;
1432 if ( escapes == 0 ){
1433 val->bv_val = LDAP_STRNDUP( startPos, len );
1438 val->bv_val = LDAP_MALLOC( len + 1 );
1439 for ( s = 0, d = 0; d < len; ) {
1441 * This point is reached only if escapes
1442 * are properly used, so all we need to
1445 if ( LDAP_DN_ESCAPE_DCE( startPos[ s ] ) ) {
1449 val->bv_val[ d++ ] = startPos[ s++ ];
1451 val->bv_val[ d ] = '\0';
1452 assert( strlen( val->bv_val ) == len );
1459 IA52strval( const char *str, struct berval *val, const char **next, unsigned flags )
1461 const char *p, *startPos, *endPos = NULL;
1462 ber_len_t len, escapes;
1474 for ( startPos = p = str, escapes = 0; p[ 0 ]; p++ ) {
1475 if ( LDAP_DN_ESCAPE( p[ 0 ] ) ) {
1477 if ( p[ 0 ] == '\0' ) {
1481 if ( !LDAP_DN_NEEDESCAPE( p[ 0 ] )
1482 && ( LDAP_DN_PEDANTIC & flags ) ) {
1487 } else if ( LDAP_DN_VALUE_END_V2( p[ 0 ] ) ) {
1492 * FIXME: can we accept anything else? I guess we need
1493 * to stop if a value is not legal
1497 /* strip trailing (unescaped) spaces */
1499 endPos > startPos + 1 &&
1500 LDAP_DN_ASCII_SPACE( endPos[ -1 ] ) &&
1501 !LDAP_DN_ESCAPE( endPos[ -2 ] );
1507 if ( flags & LDAP_DN_SKIP ) {
1511 len = ( endPos ? endPos : p ) - startPos - escapes;
1513 if ( escapes == 0 ) {
1514 val->bv_val = LDAP_STRNDUP( startPos, len );
1519 val->bv_val = LDAP_MALLOC( len + 1 );
1520 for ( s = 0, d = 0; d < len; ) {
1521 if ( LDAP_DN_ESCAPE( startPos[ s ] ) ) {
1524 val->bv_val[ d++ ] = startPos[ s++ ];
1526 val->bv_val[ d ] = '\0';
1527 assert( strlen( val->bv_val ) == len );
1534 quotedIA52strval( const char *str, struct berval *val, const char **next, unsigned flags )
1536 const char *p, *startPos, *endPos = NULL;
1538 unsigned escapes = 0;
1546 /* initial quote already eaten */
1547 for ( startPos = p = str; p[ 0 ]; p++ ) {
1549 * According to RFC 1779, the quoted value can
1550 * contain escaped as well as unescaped special values;
1551 * as a consequence we tolerate escaped values
1552 * (e.g. '"\,"' -> '\,') and escape unescaped specials
1553 * (e.g. '","' -> '\,').
1555 if ( LDAP_DN_ESCAPE( p[ 0 ] ) ) {
1556 if ( p[ 1 ] == '\0' ) {
1561 if ( !LDAP_DN_V2_PAIR( p[ 0 ] )
1562 && ( LDAP_DN_PEDANTIC & flags ) ) {
1564 * do we allow to escape normal chars?
1565 * LDAPv2 does not allow any mechanism
1566 * for escaping chars with '\' and hex
1573 } else if ( LDAP_DN_QUOTES( p[ 0 ] ) ) {
1575 /* eat closing quotes */
1581 * FIXME: can we accept anything else? I guess we need
1582 * to stop if a value is not legal
1586 if ( endPos == NULL ) {
1590 /* Strip trailing (unescaped) spaces */
1591 for ( ; p[ 0 ] && LDAP_DN_ASCII_SPACE( p[ 0 ] ); p++ ) {
1596 if ( flags & LDAP_DN_SKIP ) {
1600 len = endPos - startPos - escapes;
1603 if ( escapes == 0 ) {
1604 val->bv_val = LDAP_STRNDUP( startPos, len );
1609 val->bv_val = LDAP_MALLOC( len + 1 );
1612 for ( s = d = 0; d < len; ) {
1613 if ( LDAP_DN_ESCAPE( str[ s ] ) ) {
1616 val->bv_val[ d++ ] = str[ s++ ];
1618 val->bv_val[ d ] = '\0';
1619 assert( strlen( val->bv_val ) == len );
1626 hexstr2bin( const char *str, char *c )
1636 if ( LDAP_DN_ASCII_DIGIT( c1 ) ) {
1640 if ( LDAP_DN_ASCII_UCASE_HEXALPHA( c1 ) ) {
1643 assert( LDAP_DN_ASCII_LCASE_HEXALPHA( c1 ) );
1650 if ( LDAP_DN_ASCII_DIGIT( c2 ) ) {
1654 if ( LDAP_DN_ASCII_UCASE_HEXALPHA( c2 ) ) {
1655 *c += c2 - 'A' + 10;
1657 assert( LDAP_DN_ASCII_LCASE_HEXALPHA( c2 ) );
1658 *c += c2 - 'a' + 10;
1666 hexstr2binval( const char *str, struct berval *val, const char **next, unsigned flags )
1668 const char *p, *startPos, *endPos = NULL;
1678 for ( startPos = p = str; p[ 0 ]; p += 2 ) {
1679 switch ( LDAP_DN_FORMAT( flags ) ) {
1680 case LDAP_DN_FORMAT_LDAPV3:
1681 if ( LDAP_DN_VALUE_END( p[ 0 ] ) ) {
1686 case LDAP_DN_FORMAT_LDAP:
1687 case LDAP_DN_FORMAT_LDAPV2:
1688 if ( LDAP_DN_VALUE_END_V2( p[ 0 ] ) ) {
1693 case LDAP_DN_FORMAT_DCE:
1694 if ( LDAP_DN_VALUE_END_DCE( p[ 0 ] ) ) {
1700 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1701 if ( flags & LDAP_DN_PEDANTIC ) {
1706 for ( ; p[ 0 ]; p++ ) {
1707 switch ( LDAP_DN_FORMAT( flags ) ) {
1708 case LDAP_DN_FORMAT_LDAPV3:
1709 if ( LDAP_DN_VALUE_END( p[ 0 ] ) ) {
1714 case LDAP_DN_FORMAT_LDAP:
1715 case LDAP_DN_FORMAT_LDAPV2:
1716 if ( LDAP_DN_VALUE_END_V2( p[ 0 ] ) ) {
1721 case LDAP_DN_FORMAT_DCE:
1722 if ( LDAP_DN_VALUE_END_DCE( p[ 0 ] ) ) {
1731 if ( !LDAP_DN_HEXPAIR( p ) ) {
1739 if ( flags & LDAP_DN_SKIP ) {
1743 len = ( ( endPos ? endPos : p ) - startPos ) / 2;
1745 assert( 2 * len == (ber_len_t) (( endPos ? endPos : p ) - startPos ));
1748 val->bv_val = LDAP_MALLOC( len + 1 );
1749 if ( val->bv_val == NULL ) {
1750 return( LDAP_NO_MEMORY );
1753 for ( s = 0, d = 0; d < len; s += 2, d++ ) {
1756 hexstr2bin( &startPos[ s ], &c );
1758 val->bv_val[ d ] = c;
1761 val->bv_val[ d ] = '\0';
1767 * convert a byte in a hexadecimal pair
1770 byte2hexpair( const char *val, char *pair )
1772 static const char hexdig[] = "0123456789ABCDEF";
1778 * we assume the string has enough room for the hex encoding
1782 pair[ 0 ] = hexdig[ 0x0f & ( val[ 0 ] >> 4 ) ];
1783 pair[ 1 ] = hexdig[ 0x0f & val[ 0 ] ];
1789 * convert a binary value in hexadecimal pairs
1792 binval2hexstr( struct berval *val, char *str )
1799 if ( val->bv_len == 0 ) {
1804 * we assume the string has enough room for the hex encoding
1808 for ( s = 0, d = 0; s < val->bv_len; s++, d += 2 ) {
1809 byte2hexpair( &val->bv_val[ s ], &str[ d ] );
1816 * Length of the string representation, accounting for escaped hex
1820 strval2strlen( struct berval *val, unsigned flags, ber_len_t *len )
1822 ber_len_t l, cl = 1;
1824 int escaped_byte_len = LDAP_DN_IS_PRETTY( flags ) ? 1 : 3;
1825 #ifdef PRETTY_ESCAPE
1826 int escaped_ascii_len = LDAP_DN_IS_PRETTY( flags ) ? 2 : 3;
1827 #endif /* PRETTY_ESCAPE */
1833 if ( val->bv_len == 0 ) {
1837 for ( l = 0, p = val->bv_val; p[ 0 ]; p += cl ) {
1838 cl = LDAP_UTF8_CHARLEN( p );
1840 /* illegal utf-8 char! */
1843 } else if ( cl > 1 ) {
1846 for ( cnt = 1; cnt < cl; cnt++ ) {
1847 if ( ( p[ cnt ] & 0x80 ) == 0x00 ) {
1851 l += escaped_byte_len * cl;
1853 } else if ( LDAP_DN_NEEDESCAPE( p[ 0 ] )
1854 || ( p == val->bv_val && LDAP_DN_NEEDESCAPE_LEAD( p[ 0 ] ) )
1855 || ( !p[ 1 ] && LDAP_DN_NEEDESCAPE_TRAIL( p[ 0 ] ) ) ) {
1856 #ifdef PRETTY_ESCAPE
1857 if ( LDAP_DN_WILLESCAPE_HEX( flags, p[ 0 ] ) ) {
1860 * there might be some chars we want
1861 * to escape in form of a couple
1862 * of hexdigits for optimization purposes
1867 l += escaped_ascii_len;
1869 #else /* ! PRETTY_ESCAPE */
1871 #endif /* ! PRETTY_ESCAPE */
1884 * convert to string representation, escaping with hex the UTF-8 stuff;
1885 * assume the destination has enough room for escaping
1888 strval2str( struct berval *val, char *str, unsigned flags, ber_len_t *len )
1890 ber_len_t s, d, end;
1896 if ( val->bv_len == 0 ) {
1902 * we assume the string has enough room for the hex encoding
1905 for ( s = 0, d = 0, end = val->bv_len - 1; s < val->bv_len; ) {
1906 ber_len_t cl = LDAP_UTF8_CHARLEN( &val->bv_val[ s ] );
1909 * there might be some chars we want to escape in form
1910 * of a couple of hexdigits for optimization purposes
1912 if ( ( cl > 1 && !LDAP_DN_IS_PRETTY( flags ) )
1913 #ifdef PRETTY_ESCAPE
1914 || LDAP_DN_WILLESCAPE_HEX( flags, val->bv_val[ s ] )
1915 #else /* ! PRETTY_ESCAPE */
1916 || LDAP_DN_NEEDESCAPE( val->bv_val[ s ] )
1917 || ( d == 0 && LDAP_DN_NEEDESCAPE_LEAD( val->bv_val[ s ] ) )
1918 || ( s == end && LDAP_DN_NEEDESCAPE_TRAIL( val->bv_val[ s ] ) )
1920 #endif /* ! PRETTY_ESCAPE */
1924 byte2hexpair( &val->bv_val[ s ], &str[ d ] );
1929 } else if ( cl > 1 ) {
1931 str[ d++ ] = val->bv_val[ s++ ];
1935 #ifdef PRETTY_ESCAPE
1936 if ( LDAP_DN_NEEDESCAPE( val->bv_val[ s ] )
1937 || ( d == 0 && LDAP_DN_NEEDESCAPE_LEAD( val->bv_val[ s ] ) )
1938 || ( s == end && LDAP_DN_NEEDESCAPE_TRAIL( val->bv_val[ s ] ) ) ) {
1940 if ( !LDAP_DN_IS_PRETTY( flags ) ) {
1941 byte2hexpair( &val->bv_val[ s ], &str[ d ] );
1947 #endif /* PRETTY_ESCAPE */
1948 str[ d++ ] = val->bv_val[ s++ ];
1958 * Length of the IA5 string representation (no UTF-8 allowed)
1961 strval2IA5strlen( struct berval *val, unsigned flags, ber_len_t *len )
1970 if ( val->bv_len == 0 ) {
1974 if ( flags & LDAP_AVA_NONPRINTABLE ) {
1976 * Turn value into a binary encoded BER
1981 for ( l = 0, p = val->bv_val; p[ 0 ]; p++ ) {
1982 if ( LDAP_DN_NEEDESCAPE( p[ 0 ] )
1983 || ( p == val->bv_val && LDAP_DN_NEEDESCAPE_LEAD( p[ 0 ] ) )
1984 || ( !p[ 1 ] && LDAP_DN_NEEDESCAPE_TRAIL( p[ 0 ] ) ) ) {
1999 * convert to string representation (np UTF-8)
2000 * assume the destination has enough room for escaping
2003 strval2IA5str( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2005 ber_len_t s, d, end;
2011 if ( val->bv_len == 0 ) {
2016 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2018 * Turn value into a binary encoded BER
2025 * we assume the string has enough room for the hex encoding
2029 for ( s = 0, d = 0, end = val->bv_len - 1; s < val->bv_len; ) {
2030 if ( LDAP_DN_NEEDESCAPE( val->bv_val[ s ] )
2031 || ( s == 0 && LDAP_DN_NEEDESCAPE_LEAD( val->bv_val[ s ] ) )
2032 || ( s == end && LDAP_DN_NEEDESCAPE_TRAIL( val->bv_val[ s ] ) ) ) {
2035 str[ d++ ] = val->bv_val[ s++ ];
2045 * Length of the (supposedly) DCE string representation,
2046 * accounting for escaped hex of UTF-8 chars
2049 strval2DCEstrlen( struct berval *val, unsigned flags, ber_len_t *len )
2058 if ( val->bv_len == 0 ) {
2062 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2064 * FIXME: Turn the value into a binary encoded BER?
2069 for ( l = 0, p = val->bv_val; p[ 0 ]; p++ ) {
2070 if ( LDAP_DN_NEEDESCAPE_DCE( p[ 0 ] ) ) {
2085 * convert to (supposedly) DCE string representation,
2086 * escaping with hex the UTF-8 stuff;
2087 * assume the destination has enough room for escaping
2090 strval2DCEstr( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2098 if ( val->bv_len == 0 ) {
2103 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2105 * FIXME: Turn the value into a binary encoded BER?
2113 * we assume the string has enough room for the hex encoding
2117 for ( s = 0, d = 0; s < val->bv_len; ) {
2118 if ( LDAP_DN_NEEDESCAPE_DCE( val->bv_val[ s ] ) ) {
2121 str[ d++ ] = val->bv_val[ s++ ];
2131 * Length of the (supposedly) AD canonical string representation,
2132 * accounting for escaped hex of UTF-8 chars
2135 strval2ADstrlen( struct berval *val, unsigned flags, ber_len_t *len )
2144 if ( val->bv_len == 0 ) {
2148 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2150 * FIXME: Turn the value into a binary encoded BER?
2155 for ( l = 0, p = val->bv_val; p[ 0 ]; p++ ) {
2156 if ( LDAP_DN_NEEDESCAPE_AD( p[ 0 ] ) ) {
2171 * convert to (supposedly) AD string representation,
2172 * escaping with hex the UTF-8 stuff;
2173 * assume the destination has enough room for escaping
2176 strval2ADstr( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2184 if ( val->bv_len == 0 ) {
2189 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2191 * FIXME: Turn the value into a binary encoded BER?
2199 * we assume the string has enough room for the hex encoding
2203 for ( s = 0, d = 0; s < val->bv_len; ) {
2204 if ( LDAP_DN_NEEDESCAPE_AD( val->bv_val[ s ] ) ) {
2207 str[ d++ ] = val->bv_val[ s++ ];
2217 * If the DN is terminated by single-AVA RDNs with attribute type of "dc",
2218 * the first part of the AD representation of the DN is written in DNS
2219 * form, i.e. dot separated domain name components (as suggested
2220 * by Luke Howard, http://www.padl.com/~lukeh)
2223 dn2domain( LDAPDN *dn, struct berval *bv, int pos, int *iRDN )
2226 int domain = 0, first = 1;
2227 ber_len_t l = 1; /* we move the null also */
2230 /* we are guaranteed there's enough memory in str */
2236 assert( *iRDN >= 0 );
2238 str = bv->bv_val + pos;
2240 for ( i = *iRDN; i >= 0; i-- ) {
2244 assert( dn[ 0 ][ i ] );
2247 assert( rdn[ 0 ][ 0 ] );
2248 ava = rdn[ 0 ][ 0 ];
2250 if ( !LDAP_DN_IS_RDN_DC( rdn ) ) {
2258 AC_MEMCPY( str, ava->la_value.bv_val,
2259 ava->la_value.bv_len + 1);
2260 l += ava->la_value.bv_len;
2263 AC_MEMCPY( str + ava->la_value.bv_len + 1, bv->bv_val + pos, l);
2264 AC_MEMCPY( str, ava->la_value.bv_val,
2265 ava->la_value.bv_len );
2266 str[ ava->la_value.bv_len ] = '.';
2267 l += ava->la_value.bv_len + 1;
2272 bv->bv_len = pos + l - 1;
2278 rdn2strlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len,
2279 int ( *s2l )( struct berval *v, unsigned f, ber_len_t *l ) )
2286 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
2287 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
2289 /* len(type) + '=' + '+' | ',' */
2290 l += ava->la_attr.bv_len + 2;
2292 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2293 /* octothorpe + twice the length */
2294 l += 1 + 2 * ava->la_value.bv_len;
2298 unsigned f = flags | ava->la_flags;
2300 if ( ( *s2l )( &ava->la_value, f, &vl ) ) {
2313 rdn2str( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len,
2314 int ( *s2s ) ( struct berval *v, char * s, unsigned f, ber_len_t *l ) )
2319 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
2320 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
2322 AC_MEMCPY( &str[ l ], ava->la_attr.bv_val,
2323 ava->la_attr.bv_len );
2324 l += ava->la_attr.bv_len;
2328 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2330 if ( binval2hexstr( &ava->la_value, &str[ l ] ) ) {
2333 l += 2 * ava->la_value.bv_len;
2337 unsigned f = flags | ava->la_flags;
2339 if ( ( *s2s )( &ava->la_value, &str[ l ], f, &vl ) ) {
2344 str[ l++ ] = ( rdn[ 0 ][ iAVA + 1 ] ? '+' : ',' );
2353 rdn2DCEstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len )
2360 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
2361 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
2363 /* len(type) + '=' + ',' | '/' */
2364 l += ava->la_attr.bv_len + 2;
2366 switch ( ava->la_flags ) {
2367 case LDAP_AVA_BINARY:
2368 /* octothorpe + twice the length */
2369 l += 1 + 2 * ava->la_value.bv_len;
2372 case LDAP_AVA_STRING: {
2374 unsigned f = flags | ava->la_flags;
2376 if ( strval2DCEstrlen( &ava->la_value, f, &vl ) ) {
2394 rdn2DCEstr( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len, int first )
2399 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
2400 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
2405 str[ l++ ] = ( iAVA ? ',' : '/' );
2408 AC_MEMCPY( &str[ l ], ava->la_attr.bv_val,
2409 ava->la_attr.bv_len );
2410 l += ava->la_attr.bv_len;
2414 switch ( ava->la_flags ) {
2415 case LDAP_AVA_BINARY:
2417 if ( binval2hexstr( &ava->la_value, &str[ l ] ) ) {
2420 l += 2 * ava->la_value.bv_len;
2423 case LDAP_AVA_STRING: {
2425 unsigned f = flags | ava->la_flags;
2427 if ( strval2DCEstr( &ava->la_value, &str[ l ], f, &vl ) ) {
2445 rdn2UFNstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len )
2455 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
2456 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
2459 l += ( rdn[ 0 ][ iAVA + 1 ] ? 3 : 2 );
2461 /* FIXME: are binary values allowed in UFN? */
2462 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2463 /* octothorpe + twice the value */
2464 l += 1 + 2 * ava->la_value.bv_len;
2468 unsigned f = flags | ava->la_flags;
2470 if ( strval2strlen( &ava->la_value, f, &vl ) ) {
2483 rdn2UFNstr( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len )
2488 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
2489 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
2491 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2493 if ( binval2hexstr( &ava->la_value, &str[ l ] ) ) {
2496 l += 2 * ava->la_value.bv_len;
2500 unsigned f = flags | ava->la_flags;
2502 if ( strval2str( &ava->la_value, &str[ l ], f, &vl ) ) {
2508 if ( rdn[ 0 ][ iAVA + 1 ]) {
2509 AC_MEMCPY( &str[ l ], " + ", 3 );
2513 AC_MEMCPY( &str[ l ], ", ", 2 );
2524 rdn2ADstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len )
2534 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
2535 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
2540 /* FIXME: are binary values allowed in UFN? */
2541 switch ( ava->la_flags ) {
2542 case LDAP_AVA_BINARY:
2543 /* octothorpe + twice the value */
2544 l += 1 + 2 * ava->la_value.bv_len;
2547 case LDAP_AVA_STRING: {
2549 unsigned f = flags | ava->la_flags;
2551 if ( strval2ADstrlen( &ava->la_value, f, &vl ) ) {
2569 rdn2ADstr( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len, int first )
2574 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
2575 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
2580 str[ l++ ] = ( iAVA ? ',' : '/' );
2583 switch ( ava->la_flags ) {
2584 case LDAP_AVA_BINARY:
2586 if ( binval2hexstr( &ava->la_value, &str[ l ] ) ) {
2589 l += 2 * ava->la_value.bv_len;
2592 case LDAP_AVA_STRING: {
2594 unsigned f = flags | ava->la_flags;
2596 if ( strval2ADstr( &ava->la_value, &str[ l ], f, &vl ) ) {
2616 * Returns in str a string representation of rdn based on flags.
2617 * There is some duplication of code between this and ldap_dn2str;
2618 * this is wanted to reduce the allocation of temporary buffers.
2621 ldap_rdn2str( LDAPRDN *rdn, char **str, unsigned flags )
2628 if((flags & LDAP_DN_FORMAT_MASK) == LDAP_DN_FORMAT_LBER) {
2629 return LDAP_PARAM_ERROR;
2632 rc = ldap_rdn2bv( rdn, &bv, flags );
2638 ldap_rdn2bv( LDAPRDN *rdn, struct berval *bv, unsigned flags )
2648 if ( rdn == NULL ) {
2649 bv->bv_val = LDAP_STRDUP( "" );
2650 return( LDAP_SUCCESS );
2654 * This routine wastes "back" bytes at the end of the string
2657 switch ( LDAP_DN_FORMAT( flags ) ) {
2658 case LDAP_DN_FORMAT_LDAPV3:
2659 if ( rdn2strlen( rdn, flags, &l, strval2strlen ) ) {
2660 return LDAP_DECODING_ERROR;
2664 case LDAP_DN_FORMAT_LDAPV2:
2665 if ( rdn2strlen( rdn, flags, &l, strval2IA5strlen ) ) {
2666 return LDAP_DECODING_ERROR;
2670 case LDAP_DN_FORMAT_UFN:
2671 if ( rdn2UFNstrlen( rdn, flags, &l ) ) {
2672 return LDAP_DECODING_ERROR;
2676 case LDAP_DN_FORMAT_DCE:
2677 if ( rdn2DCEstrlen( rdn, flags, &l ) ) {
2678 return LDAP_DECODING_ERROR;
2682 case LDAP_DN_FORMAT_AD_CANONICAL:
2683 if ( rdn2ADstrlen( rdn, flags, &l ) ) {
2684 return LDAP_DECODING_ERROR;
2689 return( LDAP_PARAM_ERROR );
2692 bv->bv_val = LDAP_MALLOC( l + 1 );
2694 switch ( LDAP_DN_FORMAT( flags ) ) {
2695 case LDAP_DN_FORMAT_LDAPV3:
2696 rc = rdn2str( rdn, bv->bv_val, flags, &l, strval2str );
2700 case LDAP_DN_FORMAT_LDAPV2:
2701 rc = rdn2str( rdn, bv->bv_val, flags, &l, strval2IA5str );
2705 case LDAP_DN_FORMAT_UFN:
2706 rc = rdn2UFNstr( rdn, bv->bv_val, flags, &l );
2710 case LDAP_DN_FORMAT_DCE:
2711 rc = rdn2DCEstr( rdn, bv->bv_val, flags, &l, 1 );
2715 case LDAP_DN_FORMAT_AD_CANONICAL:
2716 rc = rdn2ADstr( rdn, bv->bv_val, flags, &l, 1 );
2721 /* need at least one of the previous */
2722 return LDAP_PARAM_ERROR;
2726 ldap_memfree( bv->bv_val );
2730 bv->bv_len = l - back;
2731 bv->bv_val[ bv->bv_len ] = '\0';
2733 return LDAP_SUCCESS;
2737 * Very bulk implementation; many optimizations can be performed
2738 * - a NULL dn results in an empty string ""
2741 * a) what do we do if a UTF-8 string must be converted in LDAPv2?
2742 * we must encode it in binary form ('#' + HEXPAIRs)
2743 * b) does DCE/AD support UTF-8?
2744 * no clue; don't think so.
2745 * c) what do we do when binary values must be converted in UTF/DCE/AD?
2746 * use binary encoded BER
2748 int ldap_dn2str( LDAPDN *dn, char **str, unsigned flags )
2755 if((flags & LDAP_DN_FORMAT_MASK) == LDAP_DN_FORMAT_LBER) {
2756 return LDAP_PARAM_ERROR;
2759 rc = ldap_dn2bv( dn, &bv, flags );
2764 int ldap_dn2bv( LDAPDN *dn, struct berval *bv, unsigned flags )
2767 int rc = LDAP_ENCODING_ERROR;
2770 /* stringifying helpers for LDAPv3/LDAPv2 */
2771 int ( *sv2l ) ( struct berval *v, unsigned f, ber_len_t *l );
2772 int ( *sv2s ) ( struct berval *v, char *s, unsigned f, ber_len_t *l );
2779 Debug( LDAP_DEBUG_TRACE, "=> ldap_dn2bv(%u)\n%s%s", flags, "", "" );
2782 * a null dn means an empty dn string
2783 * FIXME: better raise an error?
2786 bv->bv_val = LDAP_STRDUP( "" );
2787 return( LDAP_SUCCESS );
2790 switch ( LDAP_DN_FORMAT( flags ) ) {
2791 case LDAP_DN_FORMAT_LDAPV3:
2792 sv2l = strval2strlen;
2796 case LDAP_DN_FORMAT_LDAPV2:
2797 sv2l = strval2IA5strlen;
2798 sv2s = strval2IA5str;
2801 for ( iRDN = 0, len = 0; dn[ 0 ][ iRDN ]; iRDN++ ) {
2803 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
2805 if ( rdn2strlen( rdn, flags, &rdnl, sv2l ) ) {
2806 goto return_results;
2812 if ( ( bv->bv_val = LDAP_MALLOC( len + 1 ) ) == NULL ) {
2813 rc = LDAP_NO_MEMORY;
2817 for ( l = 0, iRDN = 0; dn[ 0 ][ iRDN ]; iRDN++ ) {
2819 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
2821 if ( rdn2str( rdn, &bv->bv_val[ l ], flags,
2823 LDAP_FREE( bv->bv_val );
2825 goto return_results;
2833 * trim the last ',' (the allocated memory
2834 * is one byte longer than required)
2836 bv->bv_len = len - 1;
2837 bv->bv_val[ bv->bv_len ] = '\0';
2842 case LDAP_DN_FORMAT_UFN: {
2845 * FIXME: quoting from RFC 1781:
2847 To take a distinguished name, and generate a name of this format with
2848 attribute types omitted, the following steps are followed.
2850 1. If the first attribute is of type CommonName, the type may be
2853 2. If the last attribute is of type Country, the type may be
2856 3. If the last attribute is of type Country, the last
2857 Organisation attribute may have the type omitted.
2859 4. All attributes of type OrganisationalUnit may have the type
2860 omitted, unless they are after an Organisation attribute or
2861 the first attribute is of type OrganisationalUnit.
2863 * this should be the pedantic implementation.
2865 * Here the standard implementation reflects
2866 * the one historically provided by OpenLDAP
2867 * (and UMIch, I presume), with the variant
2868 * of spaces and plusses (' + ') separating
2871 * A non-standard but nice implementation could
2872 * be to turn the final "dc" attributes into a
2873 * dot-separated domain.
2875 * Other improvements could involve the use of
2876 * friendly country names and so.
2879 int leftmost_dc = -1;
2881 #endif /* DC_IN_UFN */
2883 for ( iRDN = 0, len = 0; dn[ 0 ][ iRDN ]; iRDN++ ) {
2885 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
2887 if ( rdn2UFNstrlen( rdn, flags, &rdnl ) ) {
2888 goto return_results;
2893 if ( LDAP_DN_IS_RDN_DC( rdn ) ) {
2894 if ( leftmost_dc == -1 ) {
2900 #endif /* DC_IN_UFN */
2903 if ( ( bv->bv_val = LDAP_MALLOC( len + 1 ) ) == NULL ) {
2904 rc = LDAP_NO_MEMORY;
2909 if ( leftmost_dc == -1 ) {
2910 #endif /* DC_IN_UFN */
2911 for ( l = 0, iRDN = 0; dn[ 0 ][ iRDN ]; iRDN++ ) {
2913 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
2915 if ( rdn2UFNstr( rdn, &bv->bv_val[ l ],
2917 LDAP_FREE( bv->bv_val );
2919 goto return_results;
2925 * trim the last ', ' (the allocated memory
2926 * is two bytes longer than required)
2928 bv->bv_len = len - 2;
2929 bv->bv_val[ bv->bv_len ] = '\0';
2932 last_iRDN = iRDN - 1;
2934 for ( l = 0, iRDN = 0; iRDN < leftmost_dc; iRDN++ ) {
2936 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
2938 if ( rdn2UFNstr( rdn, &bv->bv_val[ l ],
2940 LDAP_FREE( bv->bv_val );
2942 goto return_results;
2947 if ( !dn2domain( dn, bv, l, &last_iRDN ) ) {
2948 LDAP_FREE( bv->bv_val );
2950 goto return_results;
2953 /* the string is correctly terminated by dn2domain */
2955 #endif /* DC_IN_UFN */
2961 case LDAP_DN_FORMAT_DCE:
2963 for ( iRDN = 0, len = 0; dn[ 0 ][ iRDN ]; iRDN++ ) {
2965 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
2967 if ( rdn2DCEstrlen( rdn, flags, &rdnl ) ) {
2968 goto return_results;
2974 if ( ( bv->bv_val = LDAP_MALLOC( len + 1 ) ) == NULL ) {
2975 rc = LDAP_NO_MEMORY;
2979 for ( l = 0; iRDN--; ) {
2981 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
2983 if ( rdn2DCEstr( rdn, &bv->bv_val[ l ], flags,
2985 LDAP_FREE( bv->bv_val );
2987 goto return_results;
2995 bv->bv_val[ bv->bv_len ] = '\0';
3000 case LDAP_DN_FORMAT_AD_CANONICAL: {
3003 * Sort of UFN for DCE DNs: a slash ('/') separated
3004 * global->local DN with no types; strictly speaking,
3005 * the naming context should be a domain, which is
3006 * written in DNS-style, e.g. dot-deparated.
3010 * "givenName=Bill+sn=Gates,ou=People,dc=microsoft,dc=com"
3014 * "microsoft.com/People/Bill,Gates"
3016 for ( iRDN = 0, len = -1; dn[ 0 ][ iRDN ]; iRDN++ ) {
3018 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
3020 if ( rdn2ADstrlen( rdn, flags, &rdnl ) ) {
3021 goto return_results;
3027 if ( ( bv->bv_val = LDAP_MALLOC( len + 1 ) ) == NULL ) {
3028 rc = LDAP_NO_MEMORY;
3033 if ( iRDN && dn2domain( dn, bv, 0, &iRDN ) ) {
3034 for ( l = bv->bv_len; iRDN >= 0 ; iRDN-- ) {
3036 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
3038 if ( rdn2ADstr( rdn, &bv->bv_val[ l ],
3039 flags, &rdnl, 0 ) ) {
3040 LDAP_FREE( bv->bv_val );
3042 goto return_results;
3051 * Strictly speaking, AD canonical requires
3052 * a DN to be in the form "..., dc=smtg",
3053 * i.e. terminated by a domain component
3055 if ( flags & LDAP_DN_PEDANTIC ) {
3056 LDAP_FREE( bv->bv_val );
3058 rc = LDAP_ENCODING_ERROR;
3062 for ( l = 0; iRDN >= 0 ; iRDN-- ) {
3064 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
3066 if ( rdn2ADstr( rdn, &bv->bv_val[ l ],
3067 flags, &rdnl, first ) ) {
3068 LDAP_FREE( bv->bv_val );
3070 goto return_results;
3080 bv->bv_val[ bv->bv_len ] = '\0';
3087 return LDAP_PARAM_ERROR;
3090 Debug( LDAP_DEBUG_TRACE, "<= ldap_dn2bv(%s,%u)=%d\n",
3091 bv->bv_val, flags, rc );