3 * Copyright 1998-2000 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 /* from libraries/libldap/schema.c */
31 extern char * parse_numericoid(const char **sp, int *code, const int flags);
33 /* parsing/printing routines */
34 static int str2strval( const char *str, struct berval *val,
35 const char **next, unsigned flags, unsigned *retFlags );
36 static int DCE2strval( const char *str, struct berval *val,
37 const char **next, unsigned flags );
38 static int IA52strval( const char *str, struct berval *val,
39 const char **next, unsigned flags );
40 static int quotedIA52strval( const char *str, struct berval *val,
41 const char **next, unsigned flags );
42 static int hexstr2binval( const char *str, struct berval *val,
43 const char **next, unsigned flags );
44 static int hexstr2bin( const char *str, char *c );
45 static int byte2hexpair( const char *val, char *pair );
46 static int binval2hexstr( struct berval *val, char *str );
47 static int strval2strlen( struct berval *val, unsigned flags,
49 static int strval2str( struct berval *val, char *str, unsigned flags,
51 static int strval2IA5strlen( struct berval *val, unsigned flags,
53 static int strval2IA5str( struct berval *val, char *str, unsigned flags,
55 static int strval2DCEstrlen( struct berval *val, unsigned flags,
57 static int strval2DCEstr( struct berval *val, char *str, unsigned flags,
59 static int strval2ADstrlen( struct berval *val, unsigned flags,
61 static int strval2ADstr( struct berval *val, char *str, unsigned flags,
63 static int dn2domain( LDAPDN *dn, struct berval *bv, int pos, int *iRDN );
66 static LDAPAVA * ldapava_new(
67 const struct berval *attr, const struct berval *val, unsigned flags );
69 /* Higher level helpers */
70 static int rdn2strlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len,
71 int ( *s2l )( struct berval *, unsigned, ber_len_t * ) );
72 static int rdn2str( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len,
73 int ( *s2s )( struct berval *, char *, unsigned, ber_len_t * ));
74 static int rdn2UFNstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len );
75 static int rdn2UFNstr( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len );
76 static int rdn2DCEstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len );
77 static int rdn2DCEstr( LDAPRDN *rdn, char *str, unsigned flag, ber_len_t *len, int first );
78 static int rdn2ADstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len );
79 static int rdn2ADstr( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len, int first );
82 * RFC 1823 ldap_get_dn
85 ldap_get_dn( LDAP *ld, LDAPMessage *entry )
90 Debug( LDAP_DEBUG_TRACE, "ldap_get_dn\n", 0, 0, 0 );
92 if ( entry == NULL ) {
93 ld->ld_errno = LDAP_PARAM_ERROR;
97 tmp = *entry->lm_ber; /* struct copy */
98 if ( ber_scanf( &tmp, "{a" /*}*/, &dn ) == LBER_ERROR ) {
99 ld->ld_errno = LDAP_DECODING_ERROR;
107 * RFC 1823 ldap_dn2ufn
110 ldap_dn2ufn( LDAP_CONST char *dn )
114 Debug( LDAP_DEBUG_TRACE, "ldap_dn2ufn\n", 0, 0, 0 );
116 ( void )ldap_dn_normalize( dn, LDAP_DN_FORMAT_LDAP,
117 &out, LDAP_DN_FORMAT_UFN );
123 * RFC 1823 ldap_explode_dn
126 ldap_explode_dn( LDAP_CONST char *dn, int notypes )
129 char **values = NULL;
131 unsigned flag = notypes ? LDAP_DN_FORMAT_UFN : LDAP_DN_FORMAT_LDAPV3;
133 Debug( LDAP_DEBUG_TRACE, "ldap_explode_dn\n", 0, 0, 0 );
135 if ( ldap_str2dn( dn, &tmpDN, LDAP_DN_FORMAT_LDAP )
140 if( tmpDN == NULL ) {
141 values = LDAP_MALLOC( sizeof( char * ) );
142 if( values == NULL ) return NULL;
148 for ( iRDN = 0; tmpDN[ 0 ][ iRDN ]; iRDN++ );
150 values = LDAP_MALLOC( sizeof( char * ) * ( 1 + iRDN ) );
151 if ( values == NULL ) {
152 ldap_dnfree( tmpDN );
156 for ( iRDN = 0; tmpDN[ 0 ][ iRDN ]; iRDN++ ) {
157 ldap_rdn2str( tmpDN[ 0 ][ iRDN ], &values[ iRDN ], flag );
159 ldap_dnfree( tmpDN );
160 values[ iRDN ] = NULL;
166 ldap_explode_rdn( LDAP_CONST char *rdn, int notypes )
169 char **values = NULL;
173 Debug( LDAP_DEBUG_TRACE, "ldap_explode_rdn\n", 0, 0, 0 );
176 * we only parse the first rdn
177 * FIXME: we prefer efficiency over checking if the _ENTIRE_
180 if ( ldap_str2rdn( rdn, &tmpRDN, &p, LDAP_DN_FORMAT_LDAP )
185 for ( iAVA = 0; tmpRDN[ 0 ][ iAVA ]; iAVA++ ) ;
186 values = LDAP_MALLOC( sizeof( char * ) * ( 1 + iAVA ) );
187 if ( values == NULL ) {
188 ldap_rdnfree( tmpRDN );
192 for ( iAVA = 0; tmpRDN[ 0 ][ iAVA ]; iAVA++ ) {
193 ber_len_t l = 0, vl, al = 0;
195 LDAPAVA *ava = tmpRDN[ 0 ][ iAVA ];
197 if ( ava->la_flags == LDAP_AVA_BINARY ) {
198 vl = 1 + 2 * ava->la_value.bv_len;
201 if ( strval2strlen( &ava->la_value,
202 ava->la_flags, &vl ) ) {
208 al = ava->la_attr.bv_len;
209 l = vl + ava->la_attr.bv_len + 1;
211 str = LDAP_MALLOC( l + 1 );
212 AC_MEMCPY( str, ava->la_attr.bv_val,
213 ava->la_attr.bv_len );
218 str = LDAP_MALLOC( l + 1 );
221 if ( ava->la_flags == LDAP_AVA_BINARY ) {
223 if ( binval2hexstr( &ava->la_value, &str[ al ] ) ) {
228 if ( strval2str( &ava->la_value, &str[ al ],
229 ava->la_flags, &vl ) ) {
235 values[ iAVA ] = str;
237 values[ iAVA ] = NULL;
239 ldap_rdnfree( tmpRDN );
244 LBER_VFREE( values );
245 ldap_rdnfree( tmpRDN );
250 ldap_dn2dcedn( LDAP_CONST char *dn )
254 Debug( LDAP_DEBUG_TRACE, "ldap_dn2dcedn\n", 0, 0, 0 );
256 ( void )ldap_dn_normalize( dn, LDAP_DN_FORMAT_LDAP,
257 &out, LDAP_DN_FORMAT_DCE );
263 ldap_dcedn2dn( LDAP_CONST char *dce )
267 Debug( LDAP_DEBUG_TRACE, "ldap_dcedn2dn\n", 0, 0, 0 );
269 ( void )ldap_dn_normalize( dce, LDAP_DN_FORMAT_DCE, &out, LDAP_DN_FORMAT_LDAPV3 );
275 ldap_dn2ad_canonical( LDAP_CONST char *dn )
279 Debug( LDAP_DEBUG_TRACE, "ldap_dn2ad_canonical\n", 0, 0, 0 );
281 ( void )ldap_dn_normalize( dn, LDAP_DN_FORMAT_LDAP,
282 &out, LDAP_DN_FORMAT_AD_CANONICAL );
288 * function that changes the string representation of dnin
289 * from ( fin & LDAP_DN_FORMAT_MASK ) to ( fout & LDAP_DN_FORMAT_MASK )
292 * LDAP_DN_FORMAT_LDAP (rfc 2253 and ldapbis liberal,
293 * plus some rfc 1779)
294 * LDAP_DN_FORMAT_LDAPV3 (rfc 2253 and ldapbis)
295 * LDAP_DN_FORMAT_LDAPV2 (rfc 1779)
296 * LDAP_DN_FORMAT_DCE (?)
298 * fout can be any of the above except
299 * LDAP_DN_FORMAT_LDAP
301 * LDAP_DN_FORMAT_UFN (rfc 1781, partial and with extensions)
302 * LDAP_DN_FORMAT_AD_CANONICAL (?)
305 ldap_dn_normalize( const char *dnin, unsigned fin, char **dnout, unsigned fout )
308 LDAPDN *tmpDN = NULL;
310 Debug( LDAP_DEBUG_TRACE, "ldap_dn_normalize\n", 0, 0, 0 );
316 if ( dnin == NULL ) {
317 return( LDAP_SUCCESS );
320 rc = ldap_str2dn( dnin , &tmpDN, fin );
321 if ( rc != LDAP_SUCCESS ) {
325 rc = ldap_dn2str( tmpDN, dnout, fout );
327 ldap_dnfree( tmpDN );
335 /* #define B4ATTRTYPE 0x0001 */
336 #define B4OIDATTRTYPE 0x0002
337 #define B4STRINGATTRTYPE 0x0003
339 #define B4AVAEQUALS 0x0100
340 #define B4AVASEP 0x0200
341 #define B4RDNSEP 0x0300
342 #define GOTAVA 0x0400
344 #define B4ATTRVALUE 0x0010
345 #define B4STRINGVALUE 0x0020
346 #define B4IA5VALUEQUOTED 0x0030
347 #define B4IA5VALUE 0x0040
348 #define B4BINARYVALUE 0x0050
351 * Helpers (mostly from slap.h)
352 * c is assumed to Unicode in an ASCII compatible format (UTF-8)
353 * Macros assume "C" Locale (ASCII)
355 #define LDAP_DN_ASCII_SPACE(c) \
356 ( (c) == ' ' || (c) == '\t' || (c) == '\n' || (c) == '\r' )
357 #define LDAP_DN_ASCII_LOWER(c) LDAP_LOWER(c)
358 #define LDAP_DN_ASCII_UPPER(c) LDAP_UPPER(c)
359 #define LDAP_DN_ASCII_ALPHA(c) LDAP_ALPHA(c)
361 #define LDAP_DN_ASCII_DIGIT(c) LDAP_DIGIT(c)
362 #define LDAP_DN_ASCII_LCASE_HEXALPHA(c) LDAP_HEXLOWER(c)
363 #define LDAP_DN_ASCII_UCASE_HEXALPHA(c) LDAP_HEXUPPER(c)
364 #define LDAP_DN_ASCII_HEXDIGIT(c) LDAP_HEX(c)
365 #define LDAP_DN_ASCII_ALNUM(c) LDAP_ALNUM(c)
366 #define LDAP_DN_ASCII_PRINTABLE(c) ( (c) >= ' ' && (c) <= '~' )
369 #define LDAP_DN_OID_LEADCHAR(c) LDAP_DIGIT(c)
370 #define LDAP_DN_DESC_LEADCHAR(c) LDAP_ALPHA(c)
371 #define LDAP_DN_DESC_CHAR(c) LDAP_LDH(c)
372 #define LDAP_DN_LANG_SEP(c) ( (c) == ';' )
373 #define LDAP_DN_ATTRDESC_CHAR(c) \
374 ( LDAP_DN_DESC_CHAR(c) || LDAP_DN_LANG_SEP(c) )
376 /* special symbols */
377 #define LDAP_DN_AVA_EQUALS(c) ( (c) == '=' )
378 #define LDAP_DN_AVA_SEP(c) ( (c) == '+' )
379 #define LDAP_DN_RDN_SEP(c) ( (c) == ',' )
380 #define LDAP_DN_RDN_SEP_V2(c) ( LDAP_DN_RDN_SEP(c) || (c) == ';' )
381 #define LDAP_DN_OCTOTHORPE(c) ( (c) == '#' )
382 #define LDAP_DN_QUOTES(c) ( (c) == '\"' )
383 #define LDAP_DN_ESCAPE(c) ( (c) == '\\' )
384 #define LDAP_DN_VALUE_END(c) \
385 ( LDAP_DN_RDN_SEP(c) || LDAP_DN_AVA_SEP(c) )
386 #define LDAP_DN_NE(c) \
387 ( LDAP_DN_RDN_SEP_V2(c) || LDAP_DN_AVA_SEP(c) \
388 || LDAP_DN_QUOTES(c) || (c) == '<' || (c) == '>' )
389 #define LDAP_DN_NEEDESCAPE(c) \
390 ( LDAP_DN_ESCAPE(c) || LDAP_DN_NE(c) )
391 #define LDAP_DN_NEEDESCAPE_LEAD(c) \
392 ( LDAP_DN_ASCII_SPACE(c) || LDAP_DN_OCTOTHORPE(c) || LDAP_DN_NE(c) )
393 #define LDAP_DN_NEEDESCAPE_TRAIL(c) \
394 ( LDAP_DN_ASCII_SPACE(c) || LDAP_DN_NEEDESCAPE(c) )
395 #define LDAP_DN_WILLESCAPE_CHAR(c) \
396 ( LDAP_DN_RDN_SEP(c) || LDAP_DN_AVA_SEP(c) || LDAP_DN_ESCAPE(c) )
397 #define LDAP_DN_IS_PRETTY(f) ( (f) & LDAP_DN_PRETTY )
398 #define LDAP_DN_WILLESCAPE_HEX(f, c) \
399 ( ( !LDAP_DN_IS_PRETTY( f ) ) && LDAP_DN_WILLESCAPE_CHAR(c) )
402 #define LDAP_DN_VALUE_END_V2(c) \
403 ( LDAP_DN_RDN_SEP_V2(c) || LDAP_DN_AVA_SEP(c) )
405 #define LDAP_DN_V2_SPECIAL(c) \
406 ( LDAP_DN_RDN_SEP_V2(c) || LDAP_DN_AVA_EQUALS(c) \
407 || LDAP_DN_AVA_SEP(c) || (c) == '<' || (c) == '>' \
408 || LDAP_DN_OCTOTHORPE(c) )
409 #define LDAP_DN_V2_PAIR(c) \
410 ( LDAP_DN_V2_SPECIAL(c) || LDAP_DN_ESCAPE(c) || LDAP_DN_QUOTES(c) )
413 * DCE (mostly from Luke Howard and IBM implementation for AIX)
415 * From: "Application Development Guide - Directory Services" (FIXME: add link?)
416 * Here escapes and valid chars for GDS are considered; as soon as more
417 * specific info is found, the macros will be updated.
419 * Chars: 'a'-'z', 'A'-'Z', '0'-'9',
420 * '.', ':', ',', ''', '+', '-', '=', '(', ')', '?', '/', ' '.
422 * Metachars: '/', ',', '=', '\'.
424 * the '\' is used to escape other metachars.
430 * Attribute types must start with alphabetic chars and can contain
431 * alphabetic chars and digits (FIXME: no '-'?). OIDs are allowed.
433 #define LDAP_DN_RDN_SEP_DCE(c) ( (c) == '/' )
434 #define LDAP_DN_AVA_SEP_DCE(c) ( (c) == ',' )
435 #define LDAP_DN_ESCAPE_DCE(c) ( LDAP_DN_ESCAPE(c) )
436 #define LDAP_DN_VALUE_END_DCE(c) \
437 ( LDAP_DN_RDN_SEP_DCE(c) || LDAP_DN_AVA_SEP_DCE(c) )
438 #define LDAP_DN_NEEDESCAPE_DCE(c) \
439 ( LDAP_DN_VALUE_END_DCE(c) || LDAP_DN_AVA_EQUALS(c) )
442 #define LDAP_DN_RDN_SEP_AD(c) ( (c) == '/' )
443 #define LDAP_DN_ESCAPE_AD(c) ( LDAP_DN_ESCAPE(c) )
444 #define LDAP_DN_AVA_SEP_AD(c) ( (c) == ',' ) /* assume same as DCE */
445 #define LDAP_DN_VALUE_END_AD(c) \
446 ( LDAP_DN_RDN_SEP_AD(c) || LDAP_DN_AVA_SEP_AD(c) )
447 #define LDAP_DN_NEEDESCAPE_AD(c) \
448 ( LDAP_DN_VALUE_END_AD(c) || LDAP_DN_AVA_EQUALS(c) )
451 #define LDAP_DN_HEXPAIR(s) \
452 ( LDAP_DN_ASCII_HEXDIGIT((s)[0]) && LDAP_DN_ASCII_HEXDIGIT((s)[1]) )
453 /* better look at the AttributeDescription? */
455 /* FIXME: no composite rdn or non-"dc" types, right?
456 * (what about "dc" in OID form?) */
457 /* FIXME: we do not allow binary values in domain, right? */
458 /* NOTE: use this macro only when ABSOLUTELY SURE rdn IS VALID! */
459 /* NOTE: don't use strcasecmp() as it is locale specific! */
460 #define LDAP_DC_ATTR "dc"
461 #define LDAP_DC_ATTRU "DC"
462 #define LDAP_DN_IS_RDN_DC( r ) \
463 ( (r) && (r)[0][0] && !(r)[0][1] \
464 && ((r)[0][0]->la_flags == LDAP_AVA_STRING) \
465 && ((r)[0][0]->la_attr.bv_len == 2) \
466 && (((r)[0][0]->la_attr.bv_val[0] == LDAP_DC_ATTR[0]) \
467 || ((r)[0][0]->la_attr.bv_val[0] == LDAP_DC_ATTRU[0])) \
468 && (((r)[0][0]->la_attr.bv_val[1] == LDAP_DC_ATTR[1]) \
469 || ((r)[0][0]->la_attr.bv_val[1] == LDAP_DC_ATTRU[1])))
471 /* Composite rules */
472 #define LDAP_DN_ALLOW_ONE_SPACE(f) \
473 ( LDAP_DN_LDAPV2(f) \
474 || !( (f) & LDAP_DN_P_NOSPACEAFTERRDN ) )
475 #define LDAP_DN_ALLOW_SPACES(f) \
476 ( LDAP_DN_LDAPV2(f) \
477 || !( (f) & ( LDAP_DN_P_NOLEADTRAILSPACES | LDAP_DN_P_NOSPACEAFTERRDN ) ) )
478 #define LDAP_DN_LDAP(f) \
479 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_LDAP )
480 #define LDAP_DN_LDAPV3(f) \
481 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_LDAPV3 )
482 #define LDAP_DN_LDAPV2(f) \
483 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_LDAPV2 )
484 #define LDAP_DN_DCE(f) \
485 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_DCE )
486 #define LDAP_DN_UFN(f) \
487 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_UFN )
488 #define LDAP_DN_ADC(f) \
489 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_AD_CANONICAL )
490 #define LDAP_DN_FORMAT(f) ( (f) & LDAP_DN_FORMAT_MASK )
493 * LDAPAVA helpers (will become part of the API for operations
494 * on structural representations of DNs).
497 ldapava_new( const struct berval *attr, const struct berval *val,
505 ava = LDAP_MALLOC( sizeof( LDAPAVA ) + attr->bv_len + 1 );
507 /* should we test it? */
512 ava->la_attr.bv_len = attr->bv_len;
513 ava->la_attr.bv_val = (char *)(ava+1);
514 AC_MEMCPY( ava->la_attr.bv_val, attr->bv_val, attr->bv_len );
515 ava->la_attr.bv_val[attr->bv_len] = '\0';
517 ava->la_value = *val;
518 ava->la_flags = flags;
520 ava->la_private = NULL;
526 ldap_avafree( LDAPAVA *ava )
531 /* ava's private must be freed by caller
532 * (at present let's skip this check because la_private
533 * basically holds static data) */
534 assert( ava->la_private == NULL );
538 /* la_attr is now contiguous with ava, not freed separately */
539 free( ava->la_attr.bv_val );
541 free( ava->la_value.bv_val );
547 ldap_rdnfree( LDAPRDN *rdn )
555 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
556 ldap_avafree( rdn[ 0 ][ iAVA ] );
563 ldap_dnfree( LDAPDN *dn )
571 for ( iRDN = 0; dn[ 0 ][ iRDN ]; iRDN++ ) {
572 ldap_rdnfree( dn[ 0 ][ iRDN ] );
579 * Converts a string representation of a DN (in LDAPv3, LDAPv2 or DCE)
580 * into a structural representation of the DN, by separating attribute
581 * types and values encoded in the more appropriate form, which is
582 * string or OID for attribute types and binary form of the BER encoded
583 * value or Unicode string. Formats different from LDAPv3 are parsed
584 * according to their own rules and turned into the more appropriate
585 * form according to LDAPv3.
587 * NOTE: I realize the code is getting spaghettish; it is rather
588 * experimental and will hopefully turn into something more simple
589 * and readable as soon as it works as expected.
592 #define TMP_SLOTS 256
595 ldap_str2dn( const char *str, LDAPDN **dn, unsigned flags )
598 int rc = LDAP_INVALID_DN_SYNTAX;
601 LDAPDN *newDN = NULL;
602 LDAPRDN *newRDN = NULL, *tmpDN[TMP_SLOTS];
607 Debug( LDAP_DEBUG_TRACE, "=> ldap_str2dn(%s,%u)\n%s", str, flags, "" );
611 switch ( LDAP_DN_FORMAT( flags ) ) {
612 case LDAP_DN_FORMAT_LDAP:
613 case LDAP_DN_FORMAT_LDAPV3:
614 case LDAP_DN_FORMAT_LDAPV2:
615 case LDAP_DN_FORMAT_DCE:
618 /* unsupported in str2dn */
619 case LDAP_DN_FORMAT_UFN:
620 case LDAP_DN_FORMAT_AD_CANONICAL:
621 return( LDAP_INVALID_DN_SYNTAX );
624 return( LDAP_OTHER );
627 if ( str[ 0 ] == '\0' ) {
628 return( LDAP_SUCCESS );
632 if ( LDAP_DN_DCE( flags ) ) {
635 * (from Luke Howard: thnx) A RDN separator is required
636 * at the beginning of an (absolute) DN.
638 if ( !LDAP_DN_RDN_SEP_DCE( p[ 0 ] ) ) {
643 } else if ( LDAP_DN_LDAP( flags ) ) {
645 * if dn starts with '/' let's make it a DCE dn
647 if ( LDAP_DN_RDN_SEP_DCE( p[ 0 ] ) ) {
648 flags |= LDAP_DN_FORMAT_DCE;
653 for ( ; p[ 0 ]; p++ ) {
656 err = ldap_str2rdn( p, &newRDN, &p, flags );
657 if ( err != LDAP_SUCCESS ) {
662 * We expect a rdn separator
665 switch ( LDAP_DN_FORMAT( flags ) ) {
666 case LDAP_DN_FORMAT_LDAPV3:
667 if ( !LDAP_DN_RDN_SEP( p[ 0 ] ) ) {
673 case LDAP_DN_FORMAT_LDAP:
674 case LDAP_DN_FORMAT_LDAPV2:
675 if ( !LDAP_DN_RDN_SEP_V2( p[ 0 ] ) ) {
681 case LDAP_DN_FORMAT_DCE:
682 if ( !LDAP_DN_RDN_SEP_DCE( p[ 0 ] ) ) {
691 tmpDN[nrdns++] = newRDN;
694 assert (nrdns < TMP_SLOTS);
696 if ( p[ 0 ] == '\0' ) {
698 * the DN is over, phew
700 newDN = (LDAPDN *)LDAP_MALLOC( sizeof(LDAPDN) +
701 sizeof(LDAPRDN *) * (nrdns+1));
702 if ( newDN == NULL ) {
708 newDN[0] = (LDAPRDN **)(newDN+1);
710 if ( LDAP_DN_DCE( flags ) ) {
711 /* add in reversed order */
712 for ( i=0; i<nrdns; i++ )
713 newDN[0][i] = tmpDN[nrdns-1-i];
715 for ( i=0; i<nrdns; i++ )
716 newDN[0][i] = tmpDN[i];
718 newDN[0][nrdns] = NULL;
727 ldap_rdnfree( newRDN );
730 for (nrdns-- ;nrdns>=0; nrdns-- )
731 ldap_rdnfree( tmpDN[nrdns] );
735 Debug( LDAP_DEBUG_TRACE, "<= ldap_str2dn(%s,%u)=%d\n", str, flags, rc );
744 * Parses a relative DN according to flags up to a rdn separator
745 * or to the end of str.
746 * Returns the rdn and a pointer to the string continuation, which
747 * corresponds to the rdn separator or to '\0' in case the string is over.
750 ldap_str2rdn( const char *str, LDAPRDN **rdn, const char **n, unsigned flags )
755 int rc = LDAP_INVALID_DN_SYNTAX;
756 int attrTypeEncoding = LDAP_AVA_STRING,
757 attrValueEncoding = LDAP_AVA_STRING;
759 struct berval attrType = { 0, NULL };
760 struct berval attrValue = { 0, NULL };
762 LDAPRDN *newRDN = NULL;
763 LDAPAVA *tmpRDN[TMP_SLOTS];
766 assert( rdn || flags & LDAP_DN_SKIP );
769 Debug( LDAP_DEBUG_TRACE, "=> ldap_str2rdn(%s,%u)\n%s", str, flags, "" );
776 switch ( LDAP_DN_FORMAT( flags ) ) {
777 case LDAP_DN_FORMAT_LDAP:
778 case LDAP_DN_FORMAT_LDAPV3:
779 case LDAP_DN_FORMAT_LDAPV2:
780 case LDAP_DN_FORMAT_DCE:
783 /* unsupported in str2dn */
784 case LDAP_DN_FORMAT_UFN:
785 case LDAP_DN_FORMAT_AD_CANONICAL:
786 return( LDAP_INVALID_DN_SYNTAX );
789 return( LDAP_OTHER );
792 if ( str[ 0 ] == '\0' ) {
793 return( LDAP_SUCCESS );
797 for ( ; p[ 0 ] || state == GOTAVA; ) {
800 * The parser in principle advances one token a time,
801 * or toggles state if preferable.
806 * an AttributeType can be encoded as:
807 * - its string representation; in detail, implementations
808 * MUST recognize AttributeType string type names listed
809 * in section 2.3 of draft-ietf-ldapbis-dn-XX.txt, and
810 * MAY recognize other names.
811 * - its numeric OID (a dotted decimal string); in detail
812 * RFC 2253 asserts that ``Implementations MUST allow
813 * an oid in the attribute type to be prefixed by one
814 * of the character strings "oid." or "OID."''. As soon
815 * as draft-ietf-ldapbis-dn-XX.txt obsoletes RFC 2253
816 * I'm not sure whether this is required or not any
817 * longer; to be liberal, we still implement it.
820 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
821 if ( !LDAP_DN_ALLOW_ONE_SPACE( flags ) ) {
828 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
829 if ( !LDAP_DN_ALLOW_SPACES( flags ) ) {
834 /* whitespace is allowed (and trimmed) */
836 while ( p[ 0 ] && LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
841 /* error: we expected an AVA */
847 if ( LDAP_DN_OID_LEADCHAR( p[ 0 ] ) ) {
848 state = B4OIDATTRTYPE;
852 /* else must be alpha */
853 if ( !LDAP_DN_DESC_LEADCHAR( p[ 0 ] ) ) {
857 /* LDAPv2 "oid." prefix */
858 if ( LDAP_DN_LDAPV2( flags ) ) {
860 * to be overly pedantic, we only accept
863 if ( flags & LDAP_DN_PEDANTIC ) {
864 if ( !strncmp( p, "OID.", 4 )
865 || !strncmp( p, "oid.", 4 ) ) {
867 state = B4OIDATTRTYPE;
871 if ( !strncasecmp( p, "oid.", 4 ) ) {
873 state = B4OIDATTRTYPE;
879 state = B4STRINGATTRTYPE;
882 case B4OIDATTRTYPE: {
883 int err = LDAP_SUCCESS;
885 attrType.bv_val = parse_numericoid( &p, &err,
888 if ( err != LDAP_SUCCESS ) {
891 attrType.bv_len = p - attrType.bv_val;
893 attrTypeEncoding = LDAP_AVA_BINARY;
899 case B4STRINGATTRTYPE: {
900 const char *startPos, *endPos = NULL;
904 * the starting char has been found to be
905 * a LDAP_DN_DESC_LEADCHAR so we don't re-check it
906 * FIXME: DCE attr types seem to have a more
907 * restrictive syntax (no '-' ...)
909 for ( startPos = p++; p[ 0 ]; p++ ) {
910 if ( LDAP_DN_DESC_CHAR( p[ 0 ] ) ) {
914 if ( LDAP_DN_LANG_SEP( p[ 0 ] ) ) {
917 * RFC 2253 does not explicitly
918 * allow lang extensions to attribute
921 if ( flags & LDAP_DN_PEDANTIC ) {
926 * we trim ';' and following lang
927 * and so from attribute types
930 for ( ; LDAP_DN_ATTRDESC_CHAR( p[ 0 ] )
931 || LDAP_DN_LANG_SEP( p[ 0 ] ); p++ ) {
939 len = ( endPos ? endPos : p ) - startPos;
944 attrTypeEncoding = LDAP_AVA_STRING;
947 * here we need to decide whether to use it as is
948 * or turn it in OID form; as a consequence, we
949 * need to decide whether to binary encode the value
954 if ( flags & LDAP_DN_SKIP ) {
958 attrType.bv_val = (char *)startPos;
959 attrType.bv_len = len;
965 /* spaces may not be allowed */
966 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
967 if ( !LDAP_DN_ALLOW_SPACES( flags ) ) {
972 for ( p++; LDAP_DN_ASCII_SPACE( p[ 0 ] ); p++ ) {
977 /* need equal sign */
978 if ( !LDAP_DN_AVA_EQUALS( p[ 0 ] ) ) {
983 /* spaces may not be allowed */
984 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
985 if ( !LDAP_DN_ALLOW_SPACES( flags ) ) {
990 for ( p++; LDAP_DN_ASCII_SPACE( p[ 0 ] ); p++ ) {
996 * octothorpe means a BER encoded value will follow
997 * FIXME: I don't think DCE will allow it
999 if ( LDAP_DN_OCTOTHORPE( p[ 0 ] ) ) {
1001 attrValueEncoding = LDAP_AVA_BINARY;
1002 state = B4BINARYVALUE;
1006 /* STRING value expected */
1009 * if we're pedantic, an attribute type in OID form
1010 * SHOULD imply a BER encoded attribute value; we
1011 * should at least issue a warning
1013 if ( ( flags & LDAP_DN_PEDANTIC )
1014 && ( attrTypeEncoding == LDAP_AVA_BINARY ) ) {
1015 /* OID attrType SHOULD use binary encoding */
1019 attrValueEncoding = LDAP_AVA_STRING;
1022 * LDAPv2 allows the attribute value to be quoted;
1023 * also, IA5 values are expected, in principle
1025 if ( LDAP_DN_LDAPV2( flags ) || LDAP_DN_LDAP( flags ) ) {
1026 if ( LDAP_DN_QUOTES( p[ 0 ] ) ) {
1028 state = B4IA5VALUEQUOTED;
1032 if ( LDAP_DN_LDAPV2( flags ) ) {
1039 * here STRING means RFC 2253 string
1040 * FIXME: what about DCE strings?
1042 state = B4STRINGVALUE;
1046 if ( hexstr2binval( p, &attrValue, &p, flags ) ) {
1054 switch ( LDAP_DN_FORMAT( flags ) ) {
1055 case LDAP_DN_FORMAT_LDAP:
1056 case LDAP_DN_FORMAT_LDAPV3:
1057 if ( str2strval( p, &attrValue, &p, flags,
1058 &attrValueEncoding ) ) {
1063 case LDAP_DN_FORMAT_DCE:
1064 if ( DCE2strval( p, &attrValue, &p, flags ) ) {
1077 if ( IA52strval( p, &attrValue, &p, flags ) ) {
1084 case B4IA5VALUEQUOTED:
1086 /* lead quote already stripped */
1087 if ( quotedIA52strval( p, &attrValue,
1098 if ( !( flags & LDAP_DN_SKIP ) ) {
1102 * we accept empty values
1104 ava = ldapava_new( &attrType, &attrValue,
1105 attrValueEncoding );
1106 if ( ava == NULL ) {
1107 rc = LDAP_NO_MEMORY;
1110 tmpRDN[navas++] = ava;
1112 assert(navas < TMP_SLOTS);
1116 * if we got an AVA separator ('+', or ',' for DCE )
1117 * we expect a new AVA for this RDN; otherwise
1118 * we add the RDN to the DN
1120 switch ( LDAP_DN_FORMAT( flags ) ) {
1121 case LDAP_DN_FORMAT_LDAP:
1122 case LDAP_DN_FORMAT_LDAPV3:
1123 case LDAP_DN_FORMAT_LDAPV2:
1124 if ( !LDAP_DN_AVA_SEP( p[ 0 ] ) ) {
1129 case LDAP_DN_FORMAT_DCE:
1130 if ( !LDAP_DN_AVA_SEP_DCE( p[ 0 ] ) ) {
1138 * the RDN is over, phew
1141 if ( !( flags & LDAP_DN_SKIP ) ) {
1142 newRDN = (LDAPRDN *)LDAP_MALLOC( sizeof(LDAPRDN)
1143 + sizeof(LDAPAVA *) * (navas+1) );
1144 if ( newRDN == NULL ) {
1145 rc = LDAP_NO_MEMORY;
1150 newRDN[0] = (LDAPAVA**)(newRDN+1);
1152 for (i=0; i<navas; i++)
1153 newRDN[0][i] = tmpRDN[i];
1154 newRDN[0][i] = NULL;
1162 /* they should have been used in an AVA */
1163 attrType.bv_val = NULL;
1164 attrValue.bv_val = NULL;
1179 /* They are set to NULL after they're used in an AVA */
1181 if ( attrValue.bv_val ) {
1182 free( attrValue.bv_val );
1185 for (navas-- ; navas>=0; navas-- )
1186 ldap_avafree( tmpRDN[navas] );
1190 Debug( LDAP_DEBUG_TRACE, "<= ldap_str2rdn(%*s)=%d\n",
1200 * reads in a UTF-8 string value, unescaping stuff:
1201 * '\' + LDAP_DN_NEEDESCAPE(c) -> 'c'
1202 * '\' + HEXPAIR(p) -> unhex(p)
1205 str2strval( const char *str, struct berval *val, const char **next, unsigned flags, unsigned *retFlags )
1207 const char *p, *startPos, *endPos = NULL;
1208 ber_len_t len, escapes, unescapes;
1216 for ( startPos = p = str, escapes = 0, unescapes = 0; p[ 0 ]; p++ ) {
1217 if ( LDAP_DN_ESCAPE( p[ 0 ] ) ) {
1219 if ( p[ 0 ] == '\0' ) {
1222 if ( ( p == startPos + 1 && LDAP_DN_NEEDESCAPE_LEAD( p[ 0 ] ) )
1223 || ( LDAP_DN_VALUE_END( p[ 1 ] ) && LDAP_DN_NEEDESCAPE_TRAIL( p[ 0 ] ) )
1224 || LDAP_DN_NEEDESCAPE( p[ 0 ] ) ) {
1229 if ( LDAP_DN_HEXPAIR( p ) ) {
1232 hexstr2bin( p, &c );
1235 if ( !LDAP_DN_ASCII_PRINTABLE( c ) ) {
1238 * we assume the string is UTF-8
1240 *retFlags = LDAP_AVA_NONPRINTABLE;
1247 if ( LDAP_DN_PEDANTIC & flags ) {
1251 * FIXME: we allow escaping
1252 * of chars that don't need
1253 * to and do not belong to
1254 * HEXDIGITS (we also allow
1255 * single hexdigit; maybe we
1260 } else if ( ( LDAP_DN_LDAP( flags ) && LDAP_DN_VALUE_END_V2( p[ 0 ] ) )
1261 || ( LDAP_DN_LDAPV3( flags ) && LDAP_DN_VALUE_END( p[ 0 ] ) ) ) {
1264 } else if ( LDAP_DN_NEEDESCAPE( p[ 0 ] ) ) {
1266 * FIXME: maybe we can add
1267 * escapes if not pedantic?
1274 * we do allow unescaped spaces at the end
1275 * of the value only in non-pedantic mode
1277 if ( p > startPos + 1 && LDAP_DN_ASCII_SPACE( p[ -1 ] ) &&
1278 !LDAP_DN_ESCAPE( p[ -2 ] ) ) {
1279 if ( flags & LDAP_DN_PEDANTIC ) {
1283 /* strip trailing (unescaped) spaces */
1284 for ( endPos = p - 1;
1285 endPos > startPos + 1 &&
1286 LDAP_DN_ASCII_SPACE( endPos[ -1 ] ) &&
1287 !LDAP_DN_ESCAPE( endPos[ -2 ] );
1294 if ( flags & LDAP_DN_SKIP ) {
1299 * FIXME: test memory?
1301 len = ( endPos ? endPos : p ) - startPos - escapes - unescapes;
1304 if ( escapes == 0 && unescapes == 0 ) {
1305 val->bv_val = LDAP_STRNDUP( startPos, len );
1310 val->bv_val = LDAP_MALLOC( len + 1 );
1311 for ( s = 0, d = 0; d < len; ) {
1312 if ( LDAP_DN_ESCAPE( startPos[ s ] ) ) {
1314 if ( ( s == 0 && LDAP_DN_NEEDESCAPE_LEAD( startPos[ s ] ) )
1315 || ( s == len - 1 && LDAP_DN_NEEDESCAPE_TRAIL( startPos[ s ] ) )
1316 || LDAP_DN_NEEDESCAPE( startPos[ s ] ) ) {
1317 val->bv_val[ d++ ] =
1320 } else if ( LDAP_DN_HEXPAIR( &startPos[ s ] ) ) {
1323 hexstr2bin( &startPos[ s ], &c );
1324 val->bv_val[ d++ ] = c;
1329 * we allow escaping of chars
1330 * that do not need to
1332 val->bv_val[ d++ ] =
1337 val->bv_val[ d++ ] = startPos[ s++ ];
1341 val->bv_val[ d ] = '\0';
1342 assert( strlen( val->bv_val ) == len );
1349 DCE2strval( const char *str, struct berval *val, const char **next, unsigned flags )
1351 const char *p, *startPos, *endPos = NULL;
1352 ber_len_t len, escapes;
1360 for ( startPos = p = str, escapes = 0; p[ 0 ]; p++ ) {
1361 if ( LDAP_DN_ESCAPE_DCE( p[ 0 ] ) ) {
1363 if ( LDAP_DN_NEEDESCAPE_DCE( p[ 0 ] ) ) {
1370 } else if ( LDAP_DN_VALUE_END_DCE( p[ 0 ] ) ) {
1375 * FIXME: can we accept anything else? I guess we need
1376 * to stop if a value is not legal
1381 * (unescaped) trailing spaces are trimmed must be silently ignored;
1384 if ( p > startPos + 1 && LDAP_DN_ASCII_SPACE( p[ -1 ] ) &&
1385 !LDAP_DN_ESCAPE( p[ -2 ] ) ) {
1386 if ( flags & LDAP_DN_PEDANTIC ) {
1390 /* strip trailing (unescaped) spaces */
1391 for ( endPos = p - 1;
1392 endPos > startPos + 1 &&
1393 LDAP_DN_ASCII_SPACE( endPos[ -1 ] ) &&
1394 !LDAP_DN_ESCAPE( endPos[ -2 ] );
1401 if ( flags & LDAP_DN_SKIP ) {
1405 len = ( endPos ? endPos : p ) - startPos - escapes;
1407 if ( escapes == 0 ){
1408 val->bv_val = LDAP_STRNDUP( startPos, len );
1413 val->bv_val = LDAP_MALLOC( len + 1 );
1414 for ( s = 0, d = 0; d < len; ) {
1416 * This point is reached only if escapes
1417 * are properly used, so all we need to
1420 if ( LDAP_DN_ESCAPE_DCE( startPos[ s ] ) ) {
1424 val->bv_val[ d++ ] = startPos[ s++ ];
1426 val->bv_val[ d ] = '\0';
1427 assert( strlen( val->bv_val ) == len );
1434 IA52strval( const char *str, struct berval *val, const char **next, unsigned flags )
1436 const char *p, *startPos, *endPos = NULL;
1437 ber_len_t len, escapes;
1449 for ( startPos = p = str, escapes = 0; p[ 0 ]; p++ ) {
1450 if ( LDAP_DN_ESCAPE( p[ 0 ] ) ) {
1452 if ( p[ 0 ] == '\0' ) {
1456 if ( !LDAP_DN_NEEDESCAPE( p[ 0 ] )
1457 && ( LDAP_DN_PEDANTIC & flags ) ) {
1462 } else if ( LDAP_DN_VALUE_END_V2( p[ 0 ] ) ) {
1467 * FIXME: can we accept anything else? I guess we need
1468 * to stop if a value is not legal
1472 /* strip trailing (unescaped) spaces */
1474 endPos > startPos + 1 &&
1475 LDAP_DN_ASCII_SPACE( endPos[ -1 ] ) &&
1476 !LDAP_DN_ESCAPE( endPos[ -2 ] );
1482 if ( flags & LDAP_DN_SKIP ) {
1486 len = ( endPos ? endPos : p ) - startPos - escapes;
1488 if ( escapes == 0 ) {
1489 val->bv_val = LDAP_STRNDUP( startPos, len );
1494 val->bv_val = LDAP_MALLOC( len + 1 );
1495 for ( s = 0, d = 0; d < len; ) {
1496 if ( LDAP_DN_ESCAPE( startPos[ s ] ) ) {
1499 val->bv_val[ d++ ] = startPos[ s++ ];
1501 val->bv_val[ d ] = '\0';
1502 assert( strlen( val->bv_val ) == len );
1509 quotedIA52strval( const char *str, struct berval *val, const char **next, unsigned flags )
1511 const char *p, *startPos, *endPos = NULL;
1513 unsigned escapes = 0;
1521 /* initial quote already eaten */
1522 for ( startPos = p = str; p[ 0 ]; p++ ) {
1524 * According to RFC 1779, the quoted value can
1525 * contain escaped as well as unescaped special values;
1526 * as a consequence we tolerate escaped values
1527 * (e.g. '"\,"' -> '\,') and escape unescaped specials
1528 * (e.g. '","' -> '\,').
1530 if ( LDAP_DN_ESCAPE( p[ 0 ] ) ) {
1531 if ( p[ 1 ] == '\0' ) {
1536 if ( !LDAP_DN_V2_PAIR( p[ 0 ] )
1537 && ( LDAP_DN_PEDANTIC & flags ) ) {
1539 * do we allow to escape normal chars?
1540 * LDAPv2 does not allow any mechanism
1541 * for escaping chars with '\' and hex
1548 } else if ( LDAP_DN_QUOTES( p[ 0 ] ) ) {
1550 /* eat closing quotes */
1556 * FIXME: can we accept anything else? I guess we need
1557 * to stop if a value is not legal
1561 if ( endPos == NULL ) {
1565 /* Strip trailing (unescaped) spaces */
1566 for ( ; p[ 0 ] && LDAP_DN_ASCII_SPACE( p[ 0 ] ); p++ ) {
1571 if ( flags & LDAP_DN_SKIP ) {
1575 len = endPos - startPos - escapes;
1578 if ( escapes == 0 ) {
1579 val->bv_val = LDAP_STRNDUP( startPos, len );
1584 val->bv_val = LDAP_MALLOC( len + 1 );
1587 for ( s = d = 0; d < len; ) {
1588 if ( LDAP_DN_ESCAPE( str[ s ] ) ) {
1591 val->bv_val[ d++ ] = str[ s++ ];
1593 val->bv_val[ d ] = '\0';
1594 assert( strlen( val->bv_val ) == len );
1601 hexstr2bin( const char *str, char *c )
1611 if ( LDAP_DN_ASCII_DIGIT( c1 ) ) {
1615 if ( LDAP_DN_ASCII_UCASE_HEXALPHA( c1 ) ) {
1618 assert( LDAP_DN_ASCII_LCASE_HEXALPHA( c1 ) );
1625 if ( LDAP_DN_ASCII_DIGIT( c2 ) ) {
1629 if ( LDAP_DN_ASCII_UCASE_HEXALPHA( c2 ) ) {
1630 *c += c2 - 'A' + 10;
1632 assert( LDAP_DN_ASCII_LCASE_HEXALPHA( c2 ) );
1633 *c += c2 - 'a' + 10;
1641 hexstr2binval( const char *str, struct berval *val, const char **next, unsigned flags )
1643 const char *p, *startPos, *endPos = NULL;
1653 for ( startPos = p = str; p[ 0 ]; p += 2 ) {
1654 switch ( LDAP_DN_FORMAT( flags ) ) {
1655 case LDAP_DN_FORMAT_LDAPV3:
1656 if ( LDAP_DN_VALUE_END( p[ 0 ] ) ) {
1661 case LDAP_DN_FORMAT_LDAP:
1662 case LDAP_DN_FORMAT_LDAPV2:
1663 if ( LDAP_DN_VALUE_END_V2( p[ 0 ] ) ) {
1668 case LDAP_DN_FORMAT_DCE:
1669 if ( LDAP_DN_VALUE_END_DCE( p[ 0 ] ) ) {
1675 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1676 if ( flags & LDAP_DN_PEDANTIC ) {
1681 for ( ; p[ 0 ]; p++ ) {
1682 switch ( LDAP_DN_FORMAT( flags ) ) {
1683 case LDAP_DN_FORMAT_LDAPV3:
1684 if ( LDAP_DN_VALUE_END( p[ 0 ] ) ) {
1689 case LDAP_DN_FORMAT_LDAP:
1690 case LDAP_DN_FORMAT_LDAPV2:
1691 if ( LDAP_DN_VALUE_END_V2( p[ 0 ] ) ) {
1696 case LDAP_DN_FORMAT_DCE:
1697 if ( LDAP_DN_VALUE_END_DCE( p[ 0 ] ) ) {
1706 if ( !LDAP_DN_HEXPAIR( p ) ) {
1714 if ( flags & LDAP_DN_SKIP ) {
1718 len = ( ( endPos ? endPos : p ) - startPos ) / 2;
1720 assert( 2 * len == (ber_len_t) (( endPos ? endPos : p ) - startPos ));
1723 val->bv_val = LDAP_MALLOC( len + 1 );
1724 if ( val->bv_val == NULL ) {
1725 return( LDAP_NO_MEMORY );
1728 for ( s = 0, d = 0; d < len; s += 2, d++ ) {
1731 hexstr2bin( &startPos[ s ], &c );
1733 val->bv_val[ d ] = c;
1736 val->bv_val[ d ] = '\0';
1742 * convert a byte in a hexadecimal pair
1745 byte2hexpair( const char *val, char *pair )
1747 static const char hexdig[] = "0123456789ABCDEF";
1753 * we assume the string has enough room for the hex encoding
1757 pair[ 0 ] = hexdig[ 0x0f & ( val[ 0 ] >> 4 ) ];
1758 pair[ 1 ] = hexdig[ 0x0f & val[ 0 ] ];
1764 * convert a binary value in hexadecimal pairs
1767 binval2hexstr( struct berval *val, char *str )
1774 if ( val->bv_len == 0 ) {
1779 * we assume the string has enough room for the hex encoding
1783 for ( s = 0, d = 0; s < val->bv_len; s++, d += 2 ) {
1784 byte2hexpair( &val->bv_val[ s ], &str[ d ] );
1791 * Length of the string representation, accounting for escaped hex
1795 strval2strlen( struct berval *val, unsigned flags, ber_len_t *len )
1797 ber_len_t l, cl = 1;
1799 int escaped_byte_len = LDAP_DN_IS_PRETTY( flags ) ? 1 : 3;
1800 #ifdef PRETTY_ESCAPE
1801 int escaped_ascii_len = LDAP_DN_IS_PRETTY( flags ) ? 2 : 3;
1802 #endif /* PRETTY_ESCAPE */
1808 if ( val->bv_len == 0 ) {
1812 for ( l = 0, p = val->bv_val; p[ 0 ]; p += cl ) {
1813 cl = LDAP_UTF8_CHARLEN( p );
1815 /* illegal utf-8 char! */
1818 } else if ( cl > 1 ) {
1821 for ( cnt = 1; cnt < cl; cnt++ ) {
1822 if ( ( p[ cnt ] & 0x80 ) == 0x00 ) {
1826 l += escaped_byte_len * cl;
1828 } else if ( LDAP_DN_NEEDESCAPE( p[ 0 ] )
1829 || ( p == val->bv_val && LDAP_DN_NEEDESCAPE_LEAD( p[ 0 ] ) )
1830 || ( !p[ 1 ] && LDAP_DN_NEEDESCAPE_TRAIL( p[ 0 ] ) ) ) {
1831 #ifdef PRETTY_ESCAPE
1832 if ( LDAP_DN_WILLESCAPE_HEX( flags, p[ 0 ] ) ) {
1835 * there might be some chars we want
1836 * to escape in form of a couple
1837 * of hexdigits for optimization purposes
1842 l += escaped_ascii_len;
1844 #else /* ! PRETTY_ESCAPE */
1846 #endif /* ! PRETTY_ESCAPE */
1859 * convert to string representation, escaping with hex the UTF-8 stuff;
1860 * assume the destination has enough room for escaping
1863 strval2str( struct berval *val, char *str, unsigned flags, ber_len_t *len )
1865 ber_len_t s, d, end;
1871 if ( val->bv_len == 0 ) {
1877 * we assume the string has enough room for the hex encoding
1880 for ( s = 0, d = 0, end = val->bv_len - 1; s < val->bv_len; ) {
1881 ber_len_t cl = LDAP_UTF8_CHARLEN( &val->bv_val[ s ] );
1884 * there might be some chars we want to escape in form
1885 * of a couple of hexdigits for optimization purposes
1887 if ( ( cl > 1 && !LDAP_DN_IS_PRETTY( flags ) )
1888 #ifdef PRETTY_ESCAPE
1889 || LDAP_DN_WILLESCAPE_HEX( flags, val->bv_val[ s ] )
1890 #else /* ! PRETTY_ESCAPE */
1891 || LDAP_DN_NEEDESCAPE( val->bv_val[ s ] )
1892 || ( d == 0 && LDAP_DN_NEEDESCAPE_LEAD( val->bv_val[ s ] ) )
1893 || ( s == end && LDAP_DN_NEEDESCAPE_TRAIL( val->bv_val[ s ] ) )
1895 #endif /* ! PRETTY_ESCAPE */
1899 byte2hexpair( &val->bv_val[ s ], &str[ d ] );
1904 } else if ( cl > 1 ) {
1906 str[ d++ ] = val->bv_val[ s++ ];
1910 #ifdef PRETTY_ESCAPE
1911 if ( LDAP_DN_NEEDESCAPE( val->bv_val[ s ] )
1912 || ( d == 0 && LDAP_DN_NEEDESCAPE_LEAD( val->bv_val[ s ] ) )
1913 || ( s == end && LDAP_DN_NEEDESCAPE_TRAIL( val->bv_val[ s ] ) ) ) {
1915 if ( !LDAP_DN_IS_PRETTY( flags ) ) {
1916 byte2hexpair( &val->bv_val[ s ], &str[ d ] );
1922 #endif /* PRETTY_ESCAPE */
1923 str[ d++ ] = val->bv_val[ s++ ];
1933 * Length of the IA5 string representation (no UTF-8 allowed)
1936 strval2IA5strlen( struct berval *val, unsigned flags, ber_len_t *len )
1945 if ( val->bv_len == 0 ) {
1949 if ( flags & LDAP_AVA_NONPRINTABLE ) {
1951 * Turn value into a binary encoded BER
1956 for ( l = 0, p = val->bv_val; p[ 0 ]; p++ ) {
1957 if ( LDAP_DN_NEEDESCAPE( p[ 0 ] )
1958 || ( p == val->bv_val && LDAP_DN_NEEDESCAPE_LEAD( p[ 0 ] ) )
1959 || ( !p[ 1 ] && LDAP_DN_NEEDESCAPE_TRAIL( p[ 0 ] ) ) ) {
1974 * convert to string representation (np UTF-8)
1975 * assume the destination has enough room for escaping
1978 strval2IA5str( struct berval *val, char *str, unsigned flags, ber_len_t *len )
1980 ber_len_t s, d, end;
1986 if ( val->bv_len == 0 ) {
1991 if ( flags & LDAP_AVA_NONPRINTABLE ) {
1993 * Turn value into a binary encoded BER
2000 * we assume the string has enough room for the hex encoding
2004 for ( s = 0, d = 0, end = val->bv_len - 1; s < val->bv_len; ) {
2005 if ( LDAP_DN_NEEDESCAPE( val->bv_val[ s ] )
2006 || ( s == 0 && LDAP_DN_NEEDESCAPE_LEAD( val->bv_val[ s ] ) )
2007 || ( s == end && LDAP_DN_NEEDESCAPE_TRAIL( val->bv_val[ s ] ) ) ) {
2010 str[ d++ ] = val->bv_val[ s++ ];
2020 * Length of the (supposedly) DCE string representation,
2021 * accounting for escaped hex of UTF-8 chars
2024 strval2DCEstrlen( struct berval *val, unsigned flags, ber_len_t *len )
2033 if ( val->bv_len == 0 ) {
2037 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2039 * FIXME: Turn the value into a binary encoded BER?
2044 for ( l = 0, p = val->bv_val; p[ 0 ]; p++ ) {
2045 if ( LDAP_DN_NEEDESCAPE_DCE( p[ 0 ] ) ) {
2060 * convert to (supposedly) DCE string representation,
2061 * escaping with hex the UTF-8 stuff;
2062 * assume the destination has enough room for escaping
2065 strval2DCEstr( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2073 if ( val->bv_len == 0 ) {
2078 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2080 * FIXME: Turn the value into a binary encoded BER?
2088 * we assume the string has enough room for the hex encoding
2092 for ( s = 0, d = 0; s < val->bv_len; ) {
2093 if ( LDAP_DN_NEEDESCAPE_DCE( val->bv_val[ s ] ) ) {
2096 str[ d++ ] = val->bv_val[ s++ ];
2106 * Length of the (supposedly) AD canonical string representation,
2107 * accounting for escaped hex of UTF-8 chars
2110 strval2ADstrlen( struct berval *val, unsigned flags, ber_len_t *len )
2119 if ( val->bv_len == 0 ) {
2123 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2125 * FIXME: Turn the value into a binary encoded BER?
2130 for ( l = 0, p = val->bv_val; p[ 0 ]; p++ ) {
2131 if ( LDAP_DN_NEEDESCAPE_AD( p[ 0 ] ) ) {
2146 * convert to (supposedly) AD string representation,
2147 * escaping with hex the UTF-8 stuff;
2148 * assume the destination has enough room for escaping
2151 strval2ADstr( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2159 if ( val->bv_len == 0 ) {
2164 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2166 * FIXME: Turn the value into a binary encoded BER?
2174 * we assume the string has enough room for the hex encoding
2178 for ( s = 0, d = 0; s < val->bv_len; ) {
2179 if ( LDAP_DN_NEEDESCAPE_AD( val->bv_val[ s ] ) ) {
2182 str[ d++ ] = val->bv_val[ s++ ];
2192 * If the DN is terminated by single-AVA RDNs with attribute type of "dc",
2193 * the first part of the AD representation of the DN is written in DNS
2194 * form, i.e. dot separated domain name components (as suggested
2195 * by Luke Howard, http://www.padl.com/~lukeh)
2198 dn2domain( LDAPDN *dn, struct berval *bv, int pos, int *iRDN )
2201 int domain = 0, first = 1;
2202 ber_len_t l = 1; /* we move the null also */
2205 /* we are guaranteed there's enough memory in str */
2211 assert( *iRDN >= 0 );
2213 str = bv->bv_val + pos;
2215 for ( i = *iRDN; i >= 0; i-- ) {
2219 assert( dn[ 0 ][ i ] );
2222 assert( rdn[ 0 ][ 0 ] );
2223 ava = rdn[ 0 ][ 0 ];
2225 if ( !LDAP_DN_IS_RDN_DC( rdn ) ) {
2233 AC_MEMCPY( str, ava->la_value.bv_val,
2234 ava->la_value.bv_len + 1);
2235 l += ava->la_value.bv_len;
2238 AC_MEMCPY( str + ava->la_value.bv_len + 1, bv->bv_val, l);
2239 AC_MEMCPY( str, ava->la_value.bv_val,
2240 ava->la_value.bv_len );
2241 str[ ava->la_value.bv_len ] = '.';
2242 l += ava->la_value.bv_len + 1;
2247 bv->bv_len = pos + l - 1;
2253 rdn2strlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len,
2254 int ( *s2l )( struct berval *v, unsigned f, ber_len_t *l ) )
2261 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
2262 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
2264 /* len(type) + '=' + '+' | ',' */
2265 l += ava->la_attr.bv_len + 2;
2267 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2268 /* octothorpe + twice the length */
2269 l += 1 + 2 * ava->la_value.bv_len;
2273 unsigned f = flags | ava->la_flags;
2275 if ( ( *s2l )( &ava->la_value, f, &vl ) ) {
2288 rdn2str( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len,
2289 int ( *s2s ) ( struct berval *v, char * s, unsigned f, ber_len_t *l ) )
2294 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
2295 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
2297 AC_MEMCPY( &str[ l ], ava->la_attr.bv_val,
2298 ava->la_attr.bv_len );
2299 l += ava->la_attr.bv_len;
2303 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2305 if ( binval2hexstr( &ava->la_value, &str[ l ] ) ) {
2308 l += 2 * ava->la_value.bv_len;
2312 unsigned f = flags | ava->la_flags;
2314 if ( ( *s2s )( &ava->la_value, &str[ l ], f, &vl ) ) {
2319 str[ l++ ] = ( rdn[ 0 ][ iAVA + 1 ] ? '+' : ',' );
2328 rdn2DCEstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len )
2335 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
2336 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
2338 /* len(type) + '=' + ',' | '/' */
2339 l += ava->la_attr.bv_len + 2;
2341 switch ( ava->la_flags ) {
2342 case LDAP_AVA_BINARY:
2343 /* octothorpe + twice the length */
2344 l += 1 + 2 * ava->la_value.bv_len;
2347 case LDAP_AVA_STRING: {
2349 unsigned f = flags | ava->la_flags;
2351 if ( strval2DCEstrlen( &ava->la_value, f, &vl ) ) {
2369 rdn2DCEstr( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len, int first )
2374 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
2375 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
2380 str[ l++ ] = ( iAVA ? ',' : '/' );
2383 AC_MEMCPY( &str[ l ], ava->la_attr.bv_val,
2384 ava->la_attr.bv_len );
2385 l += ava->la_attr.bv_len;
2389 switch ( ava->la_flags ) {
2390 case LDAP_AVA_BINARY:
2392 if ( binval2hexstr( &ava->la_value, &str[ l ] ) ) {
2395 l += 2 * ava->la_value.bv_len;
2398 case LDAP_AVA_STRING: {
2400 unsigned f = flags | ava->la_flags;
2402 if ( strval2DCEstr( &ava->la_value, &str[ l ], f, &vl ) ) {
2420 rdn2UFNstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len )
2430 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
2431 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
2434 l += ( rdn[ 0 ][ iAVA + 1 ] ? 3 : 2 );
2436 /* FIXME: are binary values allowed in UFN? */
2437 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2438 /* octothorpe + twice the value */
2439 l += 1 + 2 * ava->la_value.bv_len;
2443 unsigned f = flags | ava->la_flags;
2445 if ( strval2strlen( &ava->la_value, f, &vl ) ) {
2458 rdn2UFNstr( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len )
2463 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
2464 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
2466 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2468 if ( binval2hexstr( &ava->la_value, &str[ l ] ) ) {
2471 l += 2 * ava->la_value.bv_len;
2475 unsigned f = flags | ava->la_flags;
2477 if ( strval2str( &ava->la_value, &str[ l ], f, &vl ) ) {
2483 if ( rdn[ 0 ][ iAVA + 1 ]) {
2484 AC_MEMCPY( &str[ l ], " + ", 3 );
2488 AC_MEMCPY( &str[ l ], ", ", 2 );
2499 rdn2ADstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len )
2509 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
2510 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
2515 /* FIXME: are binary values allowed in UFN? */
2516 switch ( ava->la_flags ) {
2517 case LDAP_AVA_BINARY:
2518 /* octothorpe + twice the value */
2519 l += 1 + 2 * ava->la_value.bv_len;
2522 case LDAP_AVA_STRING: {
2524 unsigned f = flags | ava->la_flags;
2526 if ( strval2ADstrlen( &ava->la_value, f, &vl ) ) {
2544 rdn2ADstr( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len, int first )
2549 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
2550 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
2555 str[ l++ ] = ( iAVA ? ',' : '/' );
2558 switch ( ava->la_flags ) {
2559 case LDAP_AVA_BINARY:
2561 if ( binval2hexstr( &ava->la_value, &str[ l ] ) ) {
2564 l += 2 * ava->la_value.bv_len;
2567 case LDAP_AVA_STRING: {
2569 unsigned f = flags | ava->la_flags;
2571 if ( strval2ADstr( &ava->la_value, &str[ l ], f, &vl ) ) {
2591 * Returns in str a string representation of rdn based on flags.
2592 * There is some duplication of code between this and ldap_dn2str;
2593 * this is wanted to reduce the allocation of temporary buffers.
2596 ldap_rdn2str( LDAPRDN *rdn, char **str, unsigned flags )
2603 rc = ldap_rdn2bv( rdn, &bv, flags );
2609 ldap_rdn2bv( LDAPRDN *rdn, struct berval *bv, unsigned flags )
2619 if ( rdn == NULL ) {
2620 bv->bv_val = LDAP_STRDUP( "" );
2621 return( LDAP_SUCCESS );
2625 * This routine wastes "back" bytes at the end of the string
2628 switch ( LDAP_DN_FORMAT( flags ) ) {
2629 case LDAP_DN_FORMAT_LDAPV3:
2630 if ( rdn2strlen( rdn, flags, &l, strval2strlen ) ) {
2631 return( LDAP_OTHER );
2635 case LDAP_DN_FORMAT_LDAPV2:
2636 if ( rdn2strlen( rdn, flags, &l, strval2IA5strlen ) ) {
2637 return( LDAP_OTHER );
2641 case LDAP_DN_FORMAT_UFN:
2642 if ( rdn2UFNstrlen( rdn, flags, &l ) ) {
2643 return( LDAP_OTHER );
2647 case LDAP_DN_FORMAT_DCE:
2648 if ( rdn2DCEstrlen( rdn, flags, &l ) ) {
2649 return( LDAP_OTHER );
2653 case LDAP_DN_FORMAT_AD_CANONICAL:
2654 if ( rdn2ADstrlen( rdn, flags, &l ) ) {
2655 return( LDAP_OTHER );
2660 return( LDAP_INVALID_DN_SYNTAX );
2663 bv->bv_val = LDAP_MALLOC( l + 1 );
2665 switch ( LDAP_DN_FORMAT( flags ) ) {
2666 case LDAP_DN_FORMAT_LDAPV3:
2667 rc = rdn2str( rdn, bv->bv_val, flags, &l, strval2str );
2671 case LDAP_DN_FORMAT_LDAPV2:
2672 rc = rdn2str( rdn, bv->bv_val, flags, &l, strval2IA5str );
2676 case LDAP_DN_FORMAT_UFN:
2677 rc = rdn2UFNstr( rdn, bv->bv_val, flags, &l );
2681 case LDAP_DN_FORMAT_DCE:
2682 rc = rdn2DCEstr( rdn, bv->bv_val, flags, &l, 1 );
2686 case LDAP_DN_FORMAT_AD_CANONICAL:
2687 rc = rdn2ADstr( rdn, bv->bv_val, flags, &l, 1 );
2692 /* need at least one of the previous */
2693 return( LDAP_OTHER );
2697 ldap_memfree( bv->bv_val );
2698 return( LDAP_OTHER );
2701 bv->bv_len = l - back;
2702 bv->bv_val[ bv->bv_len ] = '\0';
2704 return( LDAP_SUCCESS );
2708 * Very bulk implementation; many optimizations can be performed
2709 * - a NULL dn results in an empty string ""
2712 * a) what do we do if a UTF-8 string must be converted in LDAPv2?
2713 * we must encode it in binary form ('#' + HEXPAIRs)
2714 * b) does DCE/AD support UTF-8?
2715 * no clue; don't think so.
2716 * c) what do we do when binary values must be converted in UTF/DCE/AD?
2717 * use binary encoded BER
2719 int ldap_dn2str( LDAPDN *dn, char **str, unsigned flags )
2726 rc = ldap_dn2bv( dn, &bv, flags );
2731 int ldap_dn2bv( LDAPDN *dn, struct berval *bv, unsigned flags )
2734 int rc = LDAP_OTHER;
2737 /* stringifying helpers for LDAPv3/LDAPv2 */
2738 int ( *sv2l ) ( struct berval *v, unsigned f, ber_len_t *l );
2739 int ( *sv2s ) ( struct berval *v, char *s, unsigned f, ber_len_t *l );
2746 Debug( LDAP_DEBUG_TRACE, "=> ldap_dn2bv(%u)\n%s%s", flags, "", "" );
2749 * a null dn means an empty dn string
2750 * FIXME: better raise an error?
2753 bv->bv_val = LDAP_STRDUP( "" );
2754 return( LDAP_SUCCESS );
2757 switch ( LDAP_DN_FORMAT( flags ) ) {
2758 case LDAP_DN_FORMAT_LDAPV3:
2759 sv2l = strval2strlen;
2763 case LDAP_DN_FORMAT_LDAPV2:
2764 sv2l = strval2IA5strlen;
2765 sv2s = strval2IA5str;
2768 for ( iRDN = 0, len = 0; dn[ 0 ][ iRDN ]; iRDN++ ) {
2770 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
2772 if ( rdn2strlen( rdn, flags, &rdnl, sv2l ) ) {
2773 goto return_results;
2779 if ( ( bv->bv_val = LDAP_MALLOC( len + 1 ) ) == NULL ) {
2780 rc = LDAP_NO_MEMORY;
2784 for ( l = 0, iRDN = 0; dn[ 0 ][ iRDN ]; iRDN++ ) {
2786 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
2788 if ( rdn2str( rdn, &bv->bv_val[ l ], flags,
2790 LDAP_FREE( bv->bv_val );
2792 goto return_results;
2800 * trim the last ',' (the allocated memory
2801 * is one byte longer than required)
2803 bv->bv_len = len - 1;
2804 bv->bv_val[ bv->bv_len ] = '\0';
2809 case LDAP_DN_FORMAT_UFN: {
2812 * FIXME: quoting from RFC 1781:
2814 To take a distinguished name, and generate a name of this format with
2815 attribute types omitted, the following steps are followed.
2817 1. If the first attribute is of type CommonName, the type may be
2820 2. If the last attribute is of type Country, the type may be
2823 3. If the last attribute is of type Country, the last
2824 Organisation attribute may have the type omitted.
2826 4. All attributes of type OrganisationalUnit may have the type
2827 omitted, unless they are after an Organisation attribute or
2828 the first attribute is of type OrganisationalUnit.
2830 * this should be the pedantic implementation.
2832 * Here the standard implementation reflects
2833 * the one historically provided by OpenLDAP
2834 * (and UMIch, I presume), with the variant
2835 * of spaces and plusses (' + ') separating
2838 * A non-standard but nice implementation could
2839 * be to turn the final "dc" attributes into a
2840 * dot-separated domain.
2842 * Other improvements could involve the use of
2843 * friendly country names and so.
2846 int leftmost_dc = -1;
2848 #endif /* DC_IN_UFN */
2850 for ( iRDN = 0, len = 0; dn[ 0 ][ iRDN ]; iRDN++ ) {
2852 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
2854 if ( rdn2UFNstrlen( rdn, flags, &rdnl ) ) {
2855 goto return_results;
2860 if ( LDAP_DN_IS_RDN_DC( rdn ) ) {
2861 if ( leftmost_dc == -1 ) {
2867 #endif /* DC_IN_UFN */
2870 if ( ( bv->bv_val = LDAP_MALLOC( len + 1 ) ) == NULL ) {
2871 rc = LDAP_NO_MEMORY;
2876 if ( leftmost_dc == -1 ) {
2877 #endif /* DC_IN_UFN */
2878 for ( l = 0, iRDN = 0; dn[ 0 ][ iRDN ]; iRDN++ ) {
2880 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
2882 if ( rdn2UFNstr( rdn, &bv->bv_val[ l ],
2884 LDAP_FREE( bv->bv_val );
2886 goto return_results;
2892 * trim the last ', ' (the allocated memory
2893 * is two bytes longer than required)
2895 bv->bv_len = len - 2;
2896 bv->bv_val[ bv->bv_len ] = '\0';
2899 last_iRDN = iRDN - 1;
2901 for ( l = 0, iRDN = 0; iRDN < leftmost_dc; iRDN++ ) {
2903 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
2905 if ( rdn2UFNstr( rdn, &bv->bv_val[ l ],
2907 LDAP_FREE( bv->bv_val );
2909 goto return_results;
2914 if ( !dn2domain( dn, bv, l, &last_iRDN ) ) {
2915 LDAP_FREE( bv->bv_val );
2917 goto return_results;
2920 /* the string is correctly terminated by dn2domain */
2922 #endif /* DC_IN_UFN */
2928 case LDAP_DN_FORMAT_DCE:
2930 for ( iRDN = 0, len = 0; dn[ 0 ][ iRDN ]; iRDN++ ) {
2932 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
2934 if ( rdn2DCEstrlen( rdn, flags, &rdnl ) ) {
2935 goto return_results;
2941 if ( ( bv->bv_val = LDAP_MALLOC( len + 1 ) ) == NULL ) {
2942 rc = LDAP_NO_MEMORY;
2946 for ( l = 0; iRDN--; ) {
2948 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
2950 if ( rdn2DCEstr( rdn, &bv->bv_val[ l ], flags,
2952 LDAP_FREE( bv->bv_val );
2954 goto return_results;
2962 bv->bv_val[ bv->bv_len ] = '\0';
2967 case LDAP_DN_FORMAT_AD_CANONICAL: {
2970 * Sort of UFN for DCE DNs: a slash ('/') separated
2971 * global->local DN with no types; strictly speaking,
2972 * the naming context should be a domain, which is
2973 * written in DNS-style, e.g. dot-deparated.
2977 * "givenName=Bill+sn=Gates,ou=People,dc=microsoft,dc=com"
2981 * "microsoft.com/People/Bill,Gates"
2983 for ( iRDN = 0, len = -1; dn[ 0 ][ iRDN ]; iRDN++ ) {
2985 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
2987 if ( rdn2ADstrlen( rdn, flags, &rdnl ) ) {
2988 goto return_results;
2994 if ( ( bv->bv_val = LDAP_MALLOC( len + 1 ) ) == NULL ) {
2995 rc = LDAP_NO_MEMORY;
3000 if ( iRDN && dn2domain( dn, bv, 0, &iRDN ) ) {
3001 for ( l = bv->bv_len; iRDN >= 0 ; iRDN-- ) {
3003 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
3005 if ( rdn2ADstr( rdn, &bv->bv_val[ l ],
3006 flags, &rdnl, 0 ) ) {
3007 LDAP_FREE( bv->bv_val );
3009 goto return_results;
3018 * Strictly speaking, AD canonical requires
3019 * a DN to be in the form "..., dc=smtg",
3020 * i.e. terminated by a domain component
3022 if ( flags & LDAP_DN_PEDANTIC ) {
3023 LDAP_FREE( bv->bv_val );
3025 rc = LDAP_INVALID_DN_SYNTAX;
3029 for ( l = 0; iRDN >= 0 ; iRDN-- ) {
3031 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
3033 if ( rdn2ADstr( rdn, &bv->bv_val[ l ],
3034 flags, &rdnl, first ) ) {
3035 LDAP_FREE( bv->bv_val );
3037 goto return_results;
3047 bv->bv_val[ bv->bv_len ] = '\0';
3054 return( LDAP_INVALID_DN_SYNTAX );
3058 Debug( LDAP_DEBUG_TRACE, "<= ldap_dn2bv(%s,%u)=%d\n",
3059 bv->bv_val, flags, rc );