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 /* parsing/printing routines */
31 static int str2strval( const char *str, struct berval *val,
32 const char **next, unsigned flags, unsigned *retFlags );
33 static int DCE2strval( const char *str, struct berval *val,
34 const char **next, unsigned flags );
35 static int IA52strval( const char *str, struct berval *val,
36 const char **next, unsigned flags );
37 static int quotedIA52strval( const char *str, struct berval *val,
38 const char **next, unsigned flags );
39 static int hexstr2binval( const char *str, struct berval *val,
40 const char **next, unsigned flags );
41 static int hexstr2bin( const char *str, char *c );
42 static int byte2hexpair( const char *val, char *pair );
43 static int binval2hexstr( struct berval *val, char *str );
44 static int strval2strlen( struct berval *val, unsigned flags,
46 static int strval2str( struct berval *val, char *str, unsigned flags,
48 static int strval2IA5strlen( struct berval *val, unsigned flags,
50 static int strval2IA5str( struct berval *val, char *str, unsigned flags,
52 static int strval2DCEstrlen( struct berval *val, unsigned flags,
54 static int strval2DCEstr( struct berval *val, char *str, unsigned flags,
56 static int strval2ADstrlen( struct berval *val, unsigned flags,
58 static int strval2ADstr( struct berval *val, char *str, unsigned flags,
60 static int dn2domain( LDAPDN *dn, struct berval *bv, int pos, int *iRDN );
63 static LDAPAVA * ldapava_new(
64 const struct berval *attr, const struct berval *val, unsigned flags );
66 /* Higher level helpers */
67 static int rdn2strlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len,
68 int ( *s2l )( struct berval *, unsigned, ber_len_t * ) );
69 static int rdn2str( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len,
70 int ( *s2s )( struct berval *, char *, unsigned, ber_len_t * ));
71 static int rdn2UFNstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len );
72 static int rdn2UFNstr( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len );
73 static int rdn2DCEstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len );
74 static int rdn2DCEstr( LDAPRDN *rdn, char *str, unsigned flag, ber_len_t *len, int first );
75 static int rdn2ADstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len );
76 static int rdn2ADstr( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len, int first );
79 * RFC 1823 ldap_get_dn
82 ldap_get_dn( LDAP *ld, LDAPMessage *entry )
87 Debug( LDAP_DEBUG_TRACE, "ldap_get_dn\n", 0, 0, 0 );
89 if ( entry == NULL ) {
90 ld->ld_errno = LDAP_PARAM_ERROR;
94 tmp = *entry->lm_ber; /* struct copy */
95 if ( ber_scanf( &tmp, "{a" /*}*/, &dn ) == LBER_ERROR ) {
96 ld->ld_errno = LDAP_DECODING_ERROR;
104 * RFC 1823 ldap_dn2ufn
107 ldap_dn2ufn( LDAP_CONST char *dn )
111 Debug( LDAP_DEBUG_TRACE, "ldap_dn2ufn\n", 0, 0, 0 );
113 ( void )ldap_dn_normalize( dn, LDAP_DN_FORMAT_LDAP,
114 &out, LDAP_DN_FORMAT_UFN );
120 * RFC 1823 ldap_explode_dn
123 ldap_explode_dn( LDAP_CONST char *dn, int notypes )
126 char **values = NULL;
128 unsigned flag = notypes ? LDAP_DN_FORMAT_UFN : LDAP_DN_FORMAT_LDAPV3;
130 Debug( LDAP_DEBUG_TRACE, "ldap_explode_dn\n", 0, 0, 0 );
132 if ( ldap_str2dn( dn, &tmpDN, LDAP_DN_FORMAT_LDAP )
137 if( tmpDN == NULL ) {
138 values = LDAP_MALLOC( sizeof( char * ) );
139 if( values == NULL ) return NULL;
145 for ( iRDN = 0; tmpDN[ 0 ][ iRDN ]; iRDN++ );
147 values = LDAP_MALLOC( sizeof( char * ) * ( 1 + iRDN ) );
148 if ( values == NULL ) {
149 ldap_dnfree( tmpDN );
153 for ( iRDN = 0; tmpDN[ 0 ][ iRDN ]; iRDN++ ) {
154 ldap_rdn2str( tmpDN[ 0 ][ iRDN ], &values[ iRDN ], flag );
156 ldap_dnfree( tmpDN );
157 values[ iRDN ] = NULL;
163 ldap_explode_rdn( LDAP_CONST char *rdn, int notypes )
166 char **values = NULL;
170 Debug( LDAP_DEBUG_TRACE, "ldap_explode_rdn\n", 0, 0, 0 );
173 * we only parse the first rdn
174 * FIXME: we prefer efficiency over checking if the _ENTIRE_
177 if ( ldap_str2rdn( rdn, &tmpRDN, &p, LDAP_DN_FORMAT_LDAP )
182 for ( iAVA = 0; tmpRDN[ 0 ][ iAVA ]; iAVA++ ) ;
183 values = LDAP_MALLOC( sizeof( char * ) * ( 1 + iAVA ) );
184 if ( values == NULL ) {
185 ldap_rdnfree( tmpRDN );
189 for ( iAVA = 0; tmpRDN[ 0 ][ iAVA ]; iAVA++ ) {
190 ber_len_t l = 0, vl, al = 0;
192 LDAPAVA *ava = tmpRDN[ 0 ][ iAVA ];
194 if ( ava->la_flags == LDAP_AVA_BINARY ) {
195 vl = 1 + 2 * ava->la_value.bv_len;
198 if ( strval2strlen( &ava->la_value,
199 ava->la_flags, &vl ) ) {
205 al = ava->la_attr.bv_len;
206 l = vl + ava->la_attr.bv_len + 1;
208 str = LDAP_MALLOC( l + 1 );
209 AC_MEMCPY( str, ava->la_attr.bv_val,
210 ava->la_attr.bv_len );
215 str = LDAP_MALLOC( l + 1 );
218 if ( ava->la_flags == LDAP_AVA_BINARY ) {
220 if ( binval2hexstr( &ava->la_value, &str[ al ] ) ) {
225 if ( strval2str( &ava->la_value, &str[ al ],
226 ava->la_flags, &vl ) ) {
232 values[ iAVA ] = str;
234 values[ iAVA ] = NULL;
236 ldap_rdnfree( tmpRDN );
241 LBER_VFREE( values );
242 ldap_rdnfree( tmpRDN );
247 ldap_dn2dcedn( LDAP_CONST char *dn )
251 Debug( LDAP_DEBUG_TRACE, "ldap_dn2dcedn\n", 0, 0, 0 );
253 ( void )ldap_dn_normalize( dn, LDAP_DN_FORMAT_LDAP,
254 &out, LDAP_DN_FORMAT_DCE );
260 ldap_dcedn2dn( LDAP_CONST char *dce )
264 Debug( LDAP_DEBUG_TRACE, "ldap_dcedn2dn\n", 0, 0, 0 );
266 ( void )ldap_dn_normalize( dce, LDAP_DN_FORMAT_DCE, &out, LDAP_DN_FORMAT_LDAPV3 );
272 ldap_dn2ad_canonical( LDAP_CONST char *dn )
276 Debug( LDAP_DEBUG_TRACE, "ldap_dn2ad_canonical\n", 0, 0, 0 );
278 ( void )ldap_dn_normalize( dn, LDAP_DN_FORMAT_LDAP,
279 &out, LDAP_DN_FORMAT_AD_CANONICAL );
285 * function that changes the string representation of dnin
286 * from ( fin & LDAP_DN_FORMAT_MASK ) to ( fout & LDAP_DN_FORMAT_MASK )
289 * LDAP_DN_FORMAT_LDAP (rfc 2253 and ldapbis liberal,
290 * plus some rfc 1779)
291 * LDAP_DN_FORMAT_LDAPV3 (rfc 2253 and ldapbis)
292 * LDAP_DN_FORMAT_LDAPV2 (rfc 1779)
293 * LDAP_DN_FORMAT_DCE (?)
295 * fout can be any of the above except
296 * LDAP_DN_FORMAT_LDAP
298 * LDAP_DN_FORMAT_UFN (rfc 1781, partial and with extensions)
299 * LDAP_DN_FORMAT_AD_CANONICAL (?)
302 ldap_dn_normalize( const char *dnin, unsigned fin, char **dnout, unsigned fout )
305 LDAPDN *tmpDN = NULL;
307 Debug( LDAP_DEBUG_TRACE, "ldap_dn_normalize\n", 0, 0, 0 );
313 if ( dnin == NULL ) {
314 return( LDAP_SUCCESS );
317 rc = ldap_str2dn( dnin , &tmpDN, fin );
318 if ( rc != LDAP_SUCCESS ) {
322 rc = ldap_dn2str( tmpDN, dnout, fout );
324 ldap_dnfree( tmpDN );
332 /* #define B4ATTRTYPE 0x0001 */
333 #define B4OIDATTRTYPE 0x0002
334 #define B4STRINGATTRTYPE 0x0003
336 #define B4AVAEQUALS 0x0100
337 #define B4AVASEP 0x0200
338 #define B4RDNSEP 0x0300
339 #define GOTAVA 0x0400
341 #define B4ATTRVALUE 0x0010
342 #define B4STRINGVALUE 0x0020
343 #define B4IA5VALUEQUOTED 0x0030
344 #define B4IA5VALUE 0x0040
345 #define B4BINARYVALUE 0x0050
348 * Helpers (mostly from slap.h)
349 * c is assumed to Unicode in an ASCII compatible format (UTF-8)
350 * Macros assume "C" Locale (ASCII)
352 #define LDAP_DN_ASCII_SPACE(c) \
353 ( (c) == ' ' || (c) == '\t' || (c) == '\n' || (c) == '\r' )
354 #define LDAP_DN_ASCII_LOWER(c) LDAP_LOWER(c)
355 #define LDAP_DN_ASCII_UPPER(c) LDAP_UPPER(c)
356 #define LDAP_DN_ASCII_ALPHA(c) LDAP_ALPHA(c)
358 #define LDAP_DN_ASCII_DIGIT(c) LDAP_DIGIT(c)
359 #define LDAP_DN_ASCII_LCASE_HEXALPHA(c) LDAP_HEXLOWER(c)
360 #define LDAP_DN_ASCII_UCASE_HEXALPHA(c) LDAP_HEXUPPER(c)
361 #define LDAP_DN_ASCII_HEXDIGIT(c) LDAP_HEX(c)
362 #define LDAP_DN_ASCII_ALNUM(c) LDAP_ALNUM(c)
363 #define LDAP_DN_ASCII_PRINTABLE(c) ( (c) >= ' ' && (c) <= '~' )
366 #define LDAP_DN_OID_LEADCHAR(c) LDAP_DIGIT(c)
367 #define LDAP_DN_DESC_LEADCHAR(c) LDAP_ALPHA(c)
368 #define LDAP_DN_DESC_CHAR(c) LDAP_LDH(c)
369 #define LDAP_DN_LANG_SEP(c) ( (c) == ';' )
370 #define LDAP_DN_ATTRDESC_CHAR(c) \
371 ( LDAP_DN_DESC_CHAR(c) || LDAP_DN_LANG_SEP(c) )
373 /* special symbols */
374 #define LDAP_DN_AVA_EQUALS(c) ( (c) == '=' )
375 #define LDAP_DN_AVA_SEP(c) ( (c) == '+' )
376 #define LDAP_DN_RDN_SEP(c) ( (c) == ',' )
377 #define LDAP_DN_RDN_SEP_V2(c) ( LDAP_DN_RDN_SEP(c) || (c) == ';' )
378 #define LDAP_DN_OCTOTHORPE(c) ( (c) == '#' )
379 #define LDAP_DN_QUOTES(c) ( (c) == '\"' )
380 #define LDAP_DN_ESCAPE(c) ( (c) == '\\' )
381 #define LDAP_DN_VALUE_END(c) \
382 ( LDAP_DN_RDN_SEP(c) || LDAP_DN_AVA_SEP(c) )
383 #define LDAP_DN_NE(c) \
384 ( LDAP_DN_RDN_SEP_V2(c) || LDAP_DN_AVA_SEP(c) \
385 || LDAP_DN_QUOTES(c) || (c) == '<' || (c) == '>' )
386 #define LDAP_DN_NEEDESCAPE(c) \
387 ( LDAP_DN_ESCAPE(c) || LDAP_DN_NE(c) )
388 #define LDAP_DN_NEEDESCAPE_LEAD(c) \
389 ( LDAP_DN_ASCII_SPACE(c) || LDAP_DN_OCTOTHORPE(c) || LDAP_DN_NE(c) )
390 #define LDAP_DN_NEEDESCAPE_TRAIL(c) \
391 ( LDAP_DN_ASCII_SPACE(c) || LDAP_DN_NEEDESCAPE(c) )
392 #define LDAP_DN_WILLESCAPE_CHAR(c) \
393 ( LDAP_DN_RDN_SEP(c) || LDAP_DN_AVA_SEP(c) || LDAP_DN_ESCAPE(c) )
394 #define LDAP_DN_IS_PRETTY(f) ( (f) & LDAP_DN_PRETTY )
395 #define LDAP_DN_WILLESCAPE_HEX(f, c) \
396 ( ( !LDAP_DN_IS_PRETTY( f ) ) && LDAP_DN_WILLESCAPE_CHAR(c) )
399 #define LDAP_DN_VALUE_END_V2(c) \
400 ( LDAP_DN_RDN_SEP_V2(c) || LDAP_DN_AVA_SEP(c) )
402 #define LDAP_DN_V2_SPECIAL(c) \
403 ( LDAP_DN_RDN_SEP_V2(c) || LDAP_DN_AVA_EQUALS(c) \
404 || LDAP_DN_AVA_SEP(c) || (c) == '<' || (c) == '>' \
405 || LDAP_DN_OCTOTHORPE(c) )
406 #define LDAP_DN_V2_PAIR(c) \
407 ( LDAP_DN_V2_SPECIAL(c) || LDAP_DN_ESCAPE(c) || LDAP_DN_QUOTES(c) )
410 * DCE (mostly from Luke Howard and IBM implementation for AIX)
412 * From: "Application Development Guide - Directory Services" (FIXME: add link?)
413 * Here escapes and valid chars for GDS are considered; as soon as more
414 * specific info is found, the macros will be updated.
416 * Chars: 'a'-'z', 'A'-'Z', '0'-'9',
417 * '.', ':', ',', ''', '+', '-', '=', '(', ')', '?', '/', ' '.
419 * Metachars: '/', ',', '=', '\'.
421 * the '\' is used to escape other metachars.
427 * Attribute types must start with alphabetic chars and can contain
428 * alphabetic chars and digits (FIXME: no '-'?). OIDs are allowed.
430 #define LDAP_DN_RDN_SEP_DCE(c) ( (c) == '/' )
431 #define LDAP_DN_AVA_SEP_DCE(c) ( (c) == ',' )
432 #define LDAP_DN_ESCAPE_DCE(c) ( LDAP_DN_ESCAPE(c) )
433 #define LDAP_DN_VALUE_END_DCE(c) \
434 ( LDAP_DN_RDN_SEP_DCE(c) || LDAP_DN_AVA_SEP_DCE(c) )
435 #define LDAP_DN_NEEDESCAPE_DCE(c) \
436 ( LDAP_DN_VALUE_END_DCE(c) || LDAP_DN_AVA_EQUALS(c) )
439 #define LDAP_DN_RDN_SEP_AD(c) ( (c) == '/' )
440 #define LDAP_DN_ESCAPE_AD(c) ( LDAP_DN_ESCAPE(c) )
441 #define LDAP_DN_AVA_SEP_AD(c) ( (c) == ',' ) /* assume same as DCE */
442 #define LDAP_DN_VALUE_END_AD(c) \
443 ( LDAP_DN_RDN_SEP_AD(c) || LDAP_DN_AVA_SEP_AD(c) )
444 #define LDAP_DN_NEEDESCAPE_AD(c) \
445 ( LDAP_DN_VALUE_END_AD(c) || LDAP_DN_AVA_EQUALS(c) )
448 #define LDAP_DN_HEXPAIR(s) \
449 ( LDAP_DN_ASCII_HEXDIGIT((s)[0]) && LDAP_DN_ASCII_HEXDIGIT((s)[1]) )
450 /* better look at the AttributeDescription? */
452 /* FIXME: no composite rdn or non-"dc" types, right?
453 * (what about "dc" in OID form?) */
454 /* FIXME: we do not allow binary values in domain, right? */
455 /* NOTE: use this macro only when ABSOLUTELY SURE rdn IS VALID! */
456 /* NOTE: don't use strcasecmp() as it is locale specific! */
457 #define LDAP_DC_ATTR "dc"
458 #define LDAP_DC_ATTRU "DC"
459 #define LDAP_DN_IS_RDN_DC( r ) \
460 ( (r) && (r)[0][0] && !(r)[0][1] \
461 && ((r)[0][0]->la_flags == LDAP_AVA_STRING) \
462 && ((r)[0][0]->la_attr.bv_len == 2) \
463 && (((r)[0][0]->la_attr.bv_val[0] == LDAP_DC_ATTR[0]) \
464 || ((r)[0][0]->la_attr.bv_val[0] == LDAP_DC_ATTRU[0])) \
465 && (((r)[0][0]->la_attr.bv_val[1] == LDAP_DC_ATTR[1]) \
466 || ((r)[0][0]->la_attr.bv_val[1] == LDAP_DC_ATTRU[1])))
468 /* Composite rules */
469 #define LDAP_DN_ALLOW_ONE_SPACE(f) \
470 ( LDAP_DN_LDAPV2(f) \
471 || !( (f) & LDAP_DN_P_NOSPACEAFTERRDN ) )
472 #define LDAP_DN_ALLOW_SPACES(f) \
473 ( LDAP_DN_LDAPV2(f) \
474 || !( (f) & ( LDAP_DN_P_NOLEADTRAILSPACES | LDAP_DN_P_NOSPACEAFTERRDN ) ) )
475 #define LDAP_DN_LDAP(f) \
476 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_LDAP )
477 #define LDAP_DN_LDAPV3(f) \
478 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_LDAPV3 )
479 #define LDAP_DN_LDAPV2(f) \
480 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_LDAPV2 )
481 #define LDAP_DN_DCE(f) \
482 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_DCE )
483 #define LDAP_DN_UFN(f) \
484 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_UFN )
485 #define LDAP_DN_ADC(f) \
486 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_AD_CANONICAL )
487 #define LDAP_DN_FORMAT(f) ( (f) & LDAP_DN_FORMAT_MASK )
490 * LDAPAVA helpers (will become part of the API for operations
491 * on structural representations of DNs).
494 ldapava_new( const struct berval *attr, const struct berval *val,
502 ava = LDAP_MALLOC( sizeof( LDAPAVA ) + attr->bv_len + 1 );
504 /* should we test it? */
509 ava->la_attr.bv_len = attr->bv_len;
510 ava->la_attr.bv_val = (char *)(ava+1);
511 AC_MEMCPY( ava->la_attr.bv_val, attr->bv_val, attr->bv_len );
512 ava->la_attr.bv_val[attr->bv_len] = '\0';
514 ava->la_value = *val;
515 ava->la_flags = flags;
517 ava->la_private = NULL;
523 ldap_avafree( LDAPAVA *ava )
528 /* ava's private must be freed by caller
529 * (at present let's skip this check because la_private
530 * basically holds static data) */
531 assert( ava->la_private == NULL );
535 /* la_attr is now contiguous with ava, not freed separately */
536 LDAP_FREE( ava->la_attr.bv_val );
538 LDAP_FREE( ava->la_value.bv_val );
544 ldap_rdnfree( LDAPRDN *rdn )
552 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
553 ldap_avafree( rdn[ 0 ][ iAVA ] );
560 ldap_dnfree( LDAPDN *dn )
568 for ( iRDN = 0; dn[ 0 ][ iRDN ]; iRDN++ ) {
569 ldap_rdnfree( dn[ 0 ][ iRDN ] );
576 * Converts a string representation of a DN (in LDAPv3, LDAPv2 or DCE)
577 * into a structural representation of the DN, by separating attribute
578 * types and values encoded in the more appropriate form, which is
579 * string or OID for attribute types and binary form of the BER encoded
580 * value or Unicode string. Formats different from LDAPv3 are parsed
581 * according to their own rules and turned into the more appropriate
582 * form according to LDAPv3.
584 * NOTE: I realize the code is getting spaghettish; it is rather
585 * experimental and will hopefully turn into something more simple
586 * and readable as soon as it works as expected.
589 #define TMP_SLOTS 1024
592 ldap_str2dn( const char *str, LDAPDN **dn, unsigned flags )
595 int rc = LDAP_DECODING_ERROR;
598 LDAPDN *newDN = NULL;
599 LDAPRDN *newRDN = NULL, *tmpDN[TMP_SLOTS];
604 Debug( LDAP_DEBUG_TRACE, "=> ldap_str2dn(%s,%u)\n%s", str, flags, "" );
608 switch ( LDAP_DN_FORMAT( flags ) ) {
609 case LDAP_DN_FORMAT_LDAP:
610 case LDAP_DN_FORMAT_LDAPV3:
611 case LDAP_DN_FORMAT_LDAPV2:
612 case LDAP_DN_FORMAT_DCE:
615 /* unsupported in str2dn */
616 case LDAP_DN_FORMAT_UFN:
617 case LDAP_DN_FORMAT_AD_CANONICAL:
618 return LDAP_PARAM_ERROR;
620 case LDAP_DN_FORMAT_LBER:
622 return LDAP_PARAM_ERROR;
625 if ( str[ 0 ] == '\0' ) {
630 if ( LDAP_DN_DCE( flags ) ) {
633 * (from Luke Howard: thnx) A RDN separator is required
634 * at the beginning of an (absolute) DN.
636 if ( !LDAP_DN_RDN_SEP_DCE( p[ 0 ] ) ) {
641 } else if ( LDAP_DN_LDAP( flags ) ) {
643 * if dn starts with '/' let's make it a DCE dn
645 if ( LDAP_DN_RDN_SEP_DCE( p[ 0 ] ) ) {
646 flags |= LDAP_DN_FORMAT_DCE;
651 for ( ; p[ 0 ]; p++ ) {
654 err = ldap_str2rdn( p, &newRDN, &p, flags );
655 if ( err != LDAP_SUCCESS ) {
660 * We expect a rdn separator
663 switch ( LDAP_DN_FORMAT( flags ) ) {
664 case LDAP_DN_FORMAT_LDAPV3:
665 if ( !LDAP_DN_RDN_SEP( p[ 0 ] ) ) {
666 rc = LDAP_DECODING_ERROR;
671 case LDAP_DN_FORMAT_LDAP:
672 case LDAP_DN_FORMAT_LDAPV2:
673 if ( !LDAP_DN_RDN_SEP_V2( p[ 0 ] ) ) {
674 rc = LDAP_DECODING_ERROR;
679 case LDAP_DN_FORMAT_DCE:
680 if ( !LDAP_DN_RDN_SEP_DCE( p[ 0 ] ) ) {
681 rc = LDAP_DECODING_ERROR;
689 tmpDN[nrdns++] = newRDN;
692 assert (nrdns < TMP_SLOTS);
694 if ( p[ 0 ] == '\0' ) {
696 * the DN is over, phew
698 newDN = (LDAPDN *)LDAP_MALLOC( sizeof(LDAPDN) +
699 sizeof(LDAPRDN *) * (nrdns+1));
700 if ( newDN == NULL ) {
706 newDN[0] = (LDAPRDN **)(newDN+1);
708 if ( LDAP_DN_DCE( flags ) ) {
709 /* add in reversed order */
710 for ( i=0; i<nrdns; i++ )
711 newDN[0][i] = tmpDN[nrdns-1-i];
713 for ( i=0; i<nrdns; i++ )
714 newDN[0][i] = tmpDN[i];
716 newDN[0][nrdns] = NULL;
725 ldap_rdnfree( newRDN );
728 for (nrdns-- ;nrdns>=0; nrdns-- )
729 ldap_rdnfree( tmpDN[nrdns] );
732 Debug( LDAP_DEBUG_TRACE, "<= ldap_str2dn(%s,%u)=%d\n", str, flags, rc );
741 * Parses a relative DN according to flags up to a rdn separator
742 * or to the end of str.
743 * Returns the rdn and a pointer to the string continuation, which
744 * corresponds to the rdn separator or to '\0' in case the string is over.
747 ldap_str2rdn( const char *str, LDAPRDN **rdn, const char **n, unsigned flags )
752 int rc = LDAP_DECODING_ERROR;
753 int attrTypeEncoding = LDAP_AVA_STRING,
754 attrValueEncoding = LDAP_AVA_STRING;
756 struct berval attrType = { 0, NULL };
757 struct berval attrValue = { 0, NULL };
759 LDAPRDN *newRDN = NULL;
760 LDAPAVA *tmpRDN[TMP_SLOTS];
763 assert( rdn || flags & LDAP_DN_SKIP );
766 Debug( LDAP_DEBUG_TRACE, "=> ldap_str2rdn(%s,%u)\n%s", str, flags, "" );
773 switch ( LDAP_DN_FORMAT( flags ) ) {
774 case LDAP_DN_FORMAT_LDAP:
775 case LDAP_DN_FORMAT_LDAPV3:
776 case LDAP_DN_FORMAT_LDAPV2:
777 case LDAP_DN_FORMAT_DCE:
780 /* unsupported in str2dn */
781 case LDAP_DN_FORMAT_UFN:
782 case LDAP_DN_FORMAT_AD_CANONICAL:
783 return LDAP_PARAM_ERROR;
785 case LDAP_DN_FORMAT_LBER:
787 return LDAP_PARAM_ERROR;
790 if ( str[ 0 ] == '\0' ) {
795 for ( ; p[ 0 ] || state == GOTAVA; ) {
798 * The parser in principle advances one token a time,
799 * or toggles state if preferable.
804 * an AttributeType can be encoded as:
805 * - its string representation; in detail, implementations
806 * MUST recognize AttributeType string type names listed
807 * in section 2.3 of draft-ietf-ldapbis-dn-XX.txt, and
808 * MAY recognize other names.
809 * - its numeric OID (a dotted decimal string); in detail
810 * RFC 2253 asserts that ``Implementations MUST allow
811 * an oid in the attribute type to be prefixed by one
812 * of the character strings "oid." or "OID."''. As soon
813 * as draft-ietf-ldapbis-dn-XX.txt obsoletes RFC 2253
814 * I'm not sure whether this is required or not any
815 * longer; to be liberal, we still implement it.
818 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
819 if ( !LDAP_DN_ALLOW_ONE_SPACE( flags ) ) {
826 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
827 if ( !LDAP_DN_ALLOW_SPACES( flags ) ) {
832 /* whitespace is allowed (and trimmed) */
834 while ( p[ 0 ] && LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
839 /* error: we expected an AVA */
845 if ( LDAP_DN_OID_LEADCHAR( p[ 0 ] ) ) {
846 state = B4OIDATTRTYPE;
850 /* else must be alpha */
851 if ( !LDAP_DN_DESC_LEADCHAR( p[ 0 ] ) ) {
855 /* LDAPv2 "oid." prefix */
856 if ( LDAP_DN_LDAPV2( flags ) ) {
858 * to be overly pedantic, we only accept
861 if ( flags & LDAP_DN_PEDANTIC ) {
862 if ( !strncmp( p, "OID.", 4 )
863 || !strncmp( p, "oid.", 4 ) ) {
865 state = B4OIDATTRTYPE;
869 if ( !strncasecmp( p, "oid.", 4 ) ) {
871 state = B4OIDATTRTYPE;
877 state = B4STRINGATTRTYPE;
880 case B4OIDATTRTYPE: {
881 int err = LDAP_SUCCESS;
883 attrType.bv_val = ldap_int_parse_numericoid( &p, &err,
886 if ( err != LDAP_SUCCESS ) {
889 attrType.bv_len = p - attrType.bv_val;
891 attrTypeEncoding = LDAP_AVA_BINARY;
897 case B4STRINGATTRTYPE: {
898 const char *startPos, *endPos = NULL;
902 * the starting char has been found to be
903 * a LDAP_DN_DESC_LEADCHAR so we don't re-check it
904 * FIXME: DCE attr types seem to have a more
905 * restrictive syntax (no '-' ...)
907 for ( startPos = p++; p[ 0 ]; p++ ) {
908 if ( LDAP_DN_DESC_CHAR( p[ 0 ] ) ) {
912 if ( LDAP_DN_LANG_SEP( p[ 0 ] ) ) {
915 * RFC 2253 does not explicitly
916 * allow lang extensions to attribute
919 if ( flags & LDAP_DN_PEDANTIC ) {
924 * we trim ';' and following lang
925 * and so from attribute types
928 for ( ; LDAP_DN_ATTRDESC_CHAR( p[ 0 ] )
929 || LDAP_DN_LANG_SEP( p[ 0 ] ); p++ ) {
937 len = ( endPos ? endPos : p ) - startPos;
942 attrTypeEncoding = LDAP_AVA_STRING;
945 * here we need to decide whether to use it as is
946 * or turn it in OID form; as a consequence, we
947 * need to decide whether to binary encode the value
952 if ( flags & LDAP_DN_SKIP ) {
956 attrType.bv_val = (char *)startPos;
957 attrType.bv_len = len;
963 /* spaces may not be allowed */
964 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
965 if ( !LDAP_DN_ALLOW_SPACES( flags ) ) {
970 for ( p++; LDAP_DN_ASCII_SPACE( p[ 0 ] ); p++ ) {
975 /* need equal sign */
976 if ( !LDAP_DN_AVA_EQUALS( p[ 0 ] ) ) {
981 /* spaces may not be allowed */
982 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
983 if ( !LDAP_DN_ALLOW_SPACES( flags ) ) {
988 for ( p++; LDAP_DN_ASCII_SPACE( p[ 0 ] ); p++ ) {
994 * octothorpe means a BER encoded value will follow
995 * FIXME: I don't think DCE will allow it
997 if ( LDAP_DN_OCTOTHORPE( p[ 0 ] ) ) {
999 attrValueEncoding = LDAP_AVA_BINARY;
1000 state = B4BINARYVALUE;
1004 /* STRING value expected */
1007 * if we're pedantic, an attribute type in OID form
1008 * SHOULD imply a BER encoded attribute value; we
1009 * should at least issue a warning
1011 if ( ( flags & LDAP_DN_PEDANTIC )
1012 && ( attrTypeEncoding == LDAP_AVA_BINARY ) ) {
1013 /* OID attrType SHOULD use binary encoding */
1017 attrValueEncoding = LDAP_AVA_STRING;
1020 * LDAPv2 allows the attribute value to be quoted;
1021 * also, IA5 values are expected, in principle
1023 if ( LDAP_DN_LDAPV2( flags ) || LDAP_DN_LDAP( flags ) ) {
1024 if ( LDAP_DN_QUOTES( p[ 0 ] ) ) {
1026 state = B4IA5VALUEQUOTED;
1030 if ( LDAP_DN_LDAPV2( flags ) ) {
1037 * here STRING means RFC 2253 string
1038 * FIXME: what about DCE strings?
1040 state = B4STRINGVALUE;
1044 if ( hexstr2binval( p, &attrValue, &p, flags ) ) {
1052 switch ( LDAP_DN_FORMAT( flags ) ) {
1053 case LDAP_DN_FORMAT_LDAP:
1054 case LDAP_DN_FORMAT_LDAPV3:
1055 if ( str2strval( p, &attrValue, &p, flags,
1056 &attrValueEncoding ) ) {
1061 case LDAP_DN_FORMAT_DCE:
1062 if ( DCE2strval( p, &attrValue, &p, flags ) ) {
1075 if ( IA52strval( p, &attrValue, &p, flags ) ) {
1082 case B4IA5VALUEQUOTED:
1084 /* lead quote already stripped */
1085 if ( quotedIA52strval( p, &attrValue,
1096 if ( !( flags & LDAP_DN_SKIP ) ) {
1100 * we accept empty values
1102 ava = ldapava_new( &attrType, &attrValue,
1103 attrValueEncoding );
1104 if ( ava == NULL ) {
1105 rc = LDAP_NO_MEMORY;
1108 tmpRDN[navas++] = ava;
1110 assert(navas < TMP_SLOTS);
1114 * if we got an AVA separator ('+', or ',' for DCE )
1115 * we expect a new AVA for this RDN; otherwise
1116 * we add the RDN to the DN
1118 switch ( LDAP_DN_FORMAT( flags ) ) {
1119 case LDAP_DN_FORMAT_LDAP:
1120 case LDAP_DN_FORMAT_LDAPV3:
1121 case LDAP_DN_FORMAT_LDAPV2:
1122 if ( !LDAP_DN_AVA_SEP( p[ 0 ] ) ) {
1127 case LDAP_DN_FORMAT_DCE:
1128 if ( !LDAP_DN_AVA_SEP_DCE( p[ 0 ] ) ) {
1136 * the RDN is over, phew
1139 if ( !( flags & LDAP_DN_SKIP ) ) {
1140 newRDN = (LDAPRDN *)LDAP_MALLOC( sizeof(LDAPRDN)
1141 + sizeof(LDAPAVA *) * (navas+1) );
1142 if ( newRDN == NULL ) {
1143 rc = LDAP_NO_MEMORY;
1148 newRDN[0] = (LDAPAVA**)(newRDN+1);
1150 for (i=0; i<navas; i++)
1151 newRDN[0][i] = tmpRDN[i];
1152 newRDN[0][i] = NULL;
1160 /* they should have been used in an AVA */
1161 attrType.bv_val = NULL;
1162 attrValue.bv_val = NULL;
1177 /* They are set to NULL after they're used in an AVA */
1179 if ( attrValue.bv_val ) {
1180 free( attrValue.bv_val );
1183 for (navas-- ; navas>=0; navas-- )
1184 ldap_avafree( tmpRDN[navas] );
1188 Debug( LDAP_DEBUG_TRACE, "<= ldap_str2rdn(%*s)=%d\n",
1198 * reads in a UTF-8 string value, unescaping stuff:
1199 * '\' + LDAP_DN_NEEDESCAPE(c) -> 'c'
1200 * '\' + HEXPAIR(p) -> unhex(p)
1203 str2strval( const char *str, struct berval *val, const char **next, unsigned flags, unsigned *retFlags )
1205 const char *p, *startPos, *endPos = NULL;
1206 ber_len_t len, escapes, unescapes;
1214 for ( startPos = p = str, escapes = 0, unescapes = 0; p[ 0 ]; p++ ) {
1215 if ( LDAP_DN_ESCAPE( p[ 0 ] ) ) {
1217 if ( p[ 0 ] == '\0' ) {
1220 if ( ( p == startPos + 1 && LDAP_DN_NEEDESCAPE_LEAD( p[ 0 ] ) )
1221 || ( LDAP_DN_VALUE_END( p[ 1 ] ) && LDAP_DN_NEEDESCAPE_TRAIL( p[ 0 ] ) )
1222 || LDAP_DN_NEEDESCAPE( p[ 0 ] ) ) {
1227 if ( LDAP_DN_HEXPAIR( p ) ) {
1230 hexstr2bin( p, &c );
1233 if ( !LDAP_DN_ASCII_PRINTABLE( c ) ) {
1236 * we assume the string is UTF-8
1238 *retFlags = LDAP_AVA_NONPRINTABLE;
1245 if ( LDAP_DN_PEDANTIC & flags ) {
1249 * FIXME: we allow escaping
1250 * of chars that don't need
1251 * to and do not belong to
1252 * HEXDIGITS (we also allow
1253 * single hexdigit; maybe we
1258 } else if ( ( LDAP_DN_LDAP( flags ) && LDAP_DN_VALUE_END_V2( p[ 0 ] ) )
1259 || ( LDAP_DN_LDAPV3( flags ) && LDAP_DN_VALUE_END( p[ 0 ] ) ) ) {
1262 } else if ( LDAP_DN_NEEDESCAPE( p[ 0 ] ) ) {
1264 * FIXME: maybe we can add
1265 * escapes if not pedantic?
1272 * we do allow unescaped spaces at the end
1273 * of the value only in non-pedantic mode
1275 if ( p > startPos + 1 && LDAP_DN_ASCII_SPACE( p[ -1 ] ) &&
1276 !LDAP_DN_ESCAPE( p[ -2 ] ) ) {
1277 if ( flags & LDAP_DN_PEDANTIC ) {
1281 /* strip trailing (unescaped) spaces */
1282 for ( endPos = p - 1;
1283 endPos > startPos + 1 &&
1284 LDAP_DN_ASCII_SPACE( endPos[ -1 ] ) &&
1285 !LDAP_DN_ESCAPE( endPos[ -2 ] );
1292 if ( flags & LDAP_DN_SKIP ) {
1297 * FIXME: test memory?
1299 len = ( endPos ? endPos : p ) - startPos - escapes - unescapes;
1302 if ( escapes == 0 && unescapes == 0 ) {
1303 val->bv_val = LDAP_STRNDUP( startPos, len );
1308 val->bv_val = LDAP_MALLOC( len + 1 );
1309 for ( s = 0, d = 0; d < len; ) {
1310 if ( LDAP_DN_ESCAPE( startPos[ s ] ) ) {
1312 if ( ( s == 0 && LDAP_DN_NEEDESCAPE_LEAD( startPos[ s ] ) )
1313 || ( s == len - 1 && LDAP_DN_NEEDESCAPE_TRAIL( startPos[ s ] ) )
1314 || LDAP_DN_NEEDESCAPE( startPos[ s ] ) ) {
1315 val->bv_val[ d++ ] =
1318 } else if ( LDAP_DN_HEXPAIR( &startPos[ s ] ) ) {
1321 hexstr2bin( &startPos[ s ], &c );
1322 val->bv_val[ d++ ] = c;
1327 * we allow escaping of chars
1328 * that do not need to
1330 val->bv_val[ d++ ] =
1335 val->bv_val[ d++ ] = startPos[ s++ ];
1339 val->bv_val[ d ] = '\0';
1340 assert( strlen( val->bv_val ) == len );
1347 DCE2strval( const char *str, struct berval *val, const char **next, unsigned flags )
1349 const char *p, *startPos, *endPos = NULL;
1350 ber_len_t len, escapes;
1358 for ( startPos = p = str, escapes = 0; p[ 0 ]; p++ ) {
1359 if ( LDAP_DN_ESCAPE_DCE( p[ 0 ] ) ) {
1361 if ( LDAP_DN_NEEDESCAPE_DCE( p[ 0 ] ) ) {
1368 } else if ( LDAP_DN_VALUE_END_DCE( p[ 0 ] ) ) {
1373 * FIXME: can we accept anything else? I guess we need
1374 * to stop if a value is not legal
1379 * (unescaped) trailing spaces are trimmed must be silently ignored;
1382 if ( p > startPos + 1 && LDAP_DN_ASCII_SPACE( p[ -1 ] ) &&
1383 !LDAP_DN_ESCAPE( p[ -2 ] ) ) {
1384 if ( flags & LDAP_DN_PEDANTIC ) {
1388 /* strip trailing (unescaped) spaces */
1389 for ( endPos = p - 1;
1390 endPos > startPos + 1 &&
1391 LDAP_DN_ASCII_SPACE( endPos[ -1 ] ) &&
1392 !LDAP_DN_ESCAPE( endPos[ -2 ] );
1399 if ( flags & LDAP_DN_SKIP ) {
1403 len = ( endPos ? endPos : p ) - startPos - escapes;
1405 if ( escapes == 0 ){
1406 val->bv_val = LDAP_STRNDUP( startPos, len );
1411 val->bv_val = LDAP_MALLOC( len + 1 );
1412 for ( s = 0, d = 0; d < len; ) {
1414 * This point is reached only if escapes
1415 * are properly used, so all we need to
1418 if ( LDAP_DN_ESCAPE_DCE( startPos[ s ] ) ) {
1422 val->bv_val[ d++ ] = startPos[ s++ ];
1424 val->bv_val[ d ] = '\0';
1425 assert( strlen( val->bv_val ) == len );
1432 IA52strval( const char *str, struct berval *val, const char **next, unsigned flags )
1434 const char *p, *startPos, *endPos = NULL;
1435 ber_len_t len, escapes;
1447 for ( startPos = p = str, escapes = 0; p[ 0 ]; p++ ) {
1448 if ( LDAP_DN_ESCAPE( p[ 0 ] ) ) {
1450 if ( p[ 0 ] == '\0' ) {
1454 if ( !LDAP_DN_NEEDESCAPE( p[ 0 ] )
1455 && ( LDAP_DN_PEDANTIC & flags ) ) {
1460 } else if ( LDAP_DN_VALUE_END_V2( p[ 0 ] ) ) {
1465 * FIXME: can we accept anything else? I guess we need
1466 * to stop if a value is not legal
1470 /* strip trailing (unescaped) spaces */
1472 endPos > startPos + 1 &&
1473 LDAP_DN_ASCII_SPACE( endPos[ -1 ] ) &&
1474 !LDAP_DN_ESCAPE( endPos[ -2 ] );
1480 if ( flags & LDAP_DN_SKIP ) {
1484 len = ( endPos ? endPos : p ) - startPos - escapes;
1486 if ( escapes == 0 ) {
1487 val->bv_val = LDAP_STRNDUP( startPos, len );
1492 val->bv_val = LDAP_MALLOC( len + 1 );
1493 for ( s = 0, d = 0; d < len; ) {
1494 if ( LDAP_DN_ESCAPE( startPos[ s ] ) ) {
1497 val->bv_val[ d++ ] = startPos[ s++ ];
1499 val->bv_val[ d ] = '\0';
1500 assert( strlen( val->bv_val ) == len );
1507 quotedIA52strval( const char *str, struct berval *val, const char **next, unsigned flags )
1509 const char *p, *startPos, *endPos = NULL;
1511 unsigned escapes = 0;
1519 /* initial quote already eaten */
1520 for ( startPos = p = str; p[ 0 ]; p++ ) {
1522 * According to RFC 1779, the quoted value can
1523 * contain escaped as well as unescaped special values;
1524 * as a consequence we tolerate escaped values
1525 * (e.g. '"\,"' -> '\,') and escape unescaped specials
1526 * (e.g. '","' -> '\,').
1528 if ( LDAP_DN_ESCAPE( p[ 0 ] ) ) {
1529 if ( p[ 1 ] == '\0' ) {
1534 if ( !LDAP_DN_V2_PAIR( p[ 0 ] )
1535 && ( LDAP_DN_PEDANTIC & flags ) ) {
1537 * do we allow to escape normal chars?
1538 * LDAPv2 does not allow any mechanism
1539 * for escaping chars with '\' and hex
1546 } else if ( LDAP_DN_QUOTES( p[ 0 ] ) ) {
1548 /* eat closing quotes */
1554 * FIXME: can we accept anything else? I guess we need
1555 * to stop if a value is not legal
1559 if ( endPos == NULL ) {
1563 /* Strip trailing (unescaped) spaces */
1564 for ( ; p[ 0 ] && LDAP_DN_ASCII_SPACE( p[ 0 ] ); p++ ) {
1569 if ( flags & LDAP_DN_SKIP ) {
1573 len = endPos - startPos - escapes;
1576 if ( escapes == 0 ) {
1577 val->bv_val = LDAP_STRNDUP( startPos, len );
1582 val->bv_val = LDAP_MALLOC( len + 1 );
1585 for ( s = d = 0; d < len; ) {
1586 if ( LDAP_DN_ESCAPE( str[ s ] ) ) {
1589 val->bv_val[ d++ ] = str[ s++ ];
1591 val->bv_val[ d ] = '\0';
1592 assert( strlen( val->bv_val ) == len );
1599 hexstr2bin( const char *str, char *c )
1609 if ( LDAP_DN_ASCII_DIGIT( c1 ) ) {
1613 if ( LDAP_DN_ASCII_UCASE_HEXALPHA( c1 ) ) {
1616 assert( LDAP_DN_ASCII_LCASE_HEXALPHA( c1 ) );
1623 if ( LDAP_DN_ASCII_DIGIT( c2 ) ) {
1627 if ( LDAP_DN_ASCII_UCASE_HEXALPHA( c2 ) ) {
1628 *c += c2 - 'A' + 10;
1630 assert( LDAP_DN_ASCII_LCASE_HEXALPHA( c2 ) );
1631 *c += c2 - 'a' + 10;
1639 hexstr2binval( const char *str, struct berval *val, const char **next, unsigned flags )
1641 const char *p, *startPos, *endPos = NULL;
1651 for ( startPos = p = str; p[ 0 ]; p += 2 ) {
1652 switch ( LDAP_DN_FORMAT( flags ) ) {
1653 case LDAP_DN_FORMAT_LDAPV3:
1654 if ( LDAP_DN_VALUE_END( p[ 0 ] ) ) {
1659 case LDAP_DN_FORMAT_LDAP:
1660 case LDAP_DN_FORMAT_LDAPV2:
1661 if ( LDAP_DN_VALUE_END_V2( p[ 0 ] ) ) {
1666 case LDAP_DN_FORMAT_DCE:
1667 if ( LDAP_DN_VALUE_END_DCE( p[ 0 ] ) ) {
1673 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1674 if ( flags & LDAP_DN_PEDANTIC ) {
1679 for ( ; p[ 0 ]; p++ ) {
1680 switch ( LDAP_DN_FORMAT( flags ) ) {
1681 case LDAP_DN_FORMAT_LDAPV3:
1682 if ( LDAP_DN_VALUE_END( p[ 0 ] ) ) {
1687 case LDAP_DN_FORMAT_LDAP:
1688 case LDAP_DN_FORMAT_LDAPV2:
1689 if ( LDAP_DN_VALUE_END_V2( p[ 0 ] ) ) {
1694 case LDAP_DN_FORMAT_DCE:
1695 if ( LDAP_DN_VALUE_END_DCE( p[ 0 ] ) ) {
1704 if ( !LDAP_DN_HEXPAIR( p ) ) {
1712 if ( flags & LDAP_DN_SKIP ) {
1716 len = ( ( endPos ? endPos : p ) - startPos ) / 2;
1718 assert( 2 * len == (ber_len_t) (( endPos ? endPos : p ) - startPos ));
1721 val->bv_val = LDAP_MALLOC( len + 1 );
1722 if ( val->bv_val == NULL ) {
1723 return( LDAP_NO_MEMORY );
1726 for ( s = 0, d = 0; d < len; s += 2, d++ ) {
1729 hexstr2bin( &startPos[ s ], &c );
1731 val->bv_val[ d ] = c;
1734 val->bv_val[ d ] = '\0';
1740 * convert a byte in a hexadecimal pair
1743 byte2hexpair( const char *val, char *pair )
1745 static const char hexdig[] = "0123456789ABCDEF";
1751 * we assume the string has enough room for the hex encoding
1755 pair[ 0 ] = hexdig[ 0x0f & ( val[ 0 ] >> 4 ) ];
1756 pair[ 1 ] = hexdig[ 0x0f & val[ 0 ] ];
1762 * convert a binary value in hexadecimal pairs
1765 binval2hexstr( struct berval *val, char *str )
1772 if ( val->bv_len == 0 ) {
1777 * we assume the string has enough room for the hex encoding
1781 for ( s = 0, d = 0; s < val->bv_len; s++, d += 2 ) {
1782 byte2hexpair( &val->bv_val[ s ], &str[ d ] );
1789 * Length of the string representation, accounting for escaped hex
1793 strval2strlen( struct berval *val, unsigned flags, ber_len_t *len )
1795 ber_len_t l, cl = 1;
1797 int escaped_byte_len = LDAP_DN_IS_PRETTY( flags ) ? 1 : 3;
1798 #ifdef PRETTY_ESCAPE
1799 int escaped_ascii_len = LDAP_DN_IS_PRETTY( flags ) ? 2 : 3;
1800 #endif /* PRETTY_ESCAPE */
1806 if ( val->bv_len == 0 ) {
1810 for ( l = 0, p = val->bv_val; p[ 0 ]; p += cl ) {
1811 cl = LDAP_UTF8_CHARLEN( p );
1813 /* illegal utf-8 char! */
1816 } else if ( cl > 1 ) {
1819 for ( cnt = 1; cnt < cl; cnt++ ) {
1820 if ( ( p[ cnt ] & 0x80 ) == 0x00 ) {
1824 l += escaped_byte_len * cl;
1826 } else if ( LDAP_DN_NEEDESCAPE( p[ 0 ] )
1827 || ( p == val->bv_val && LDAP_DN_NEEDESCAPE_LEAD( p[ 0 ] ) )
1828 || ( !p[ 1 ] && LDAP_DN_NEEDESCAPE_TRAIL( p[ 0 ] ) ) ) {
1829 #ifdef PRETTY_ESCAPE
1830 if ( LDAP_DN_WILLESCAPE_HEX( flags, p[ 0 ] ) ) {
1833 * there might be some chars we want
1834 * to escape in form of a couple
1835 * of hexdigits for optimization purposes
1840 l += escaped_ascii_len;
1842 #else /* ! PRETTY_ESCAPE */
1844 #endif /* ! PRETTY_ESCAPE */
1857 * convert to string representation, escaping with hex the UTF-8 stuff;
1858 * assume the destination has enough room for escaping
1861 strval2str( struct berval *val, char *str, unsigned flags, ber_len_t *len )
1863 ber_len_t s, d, end;
1869 if ( val->bv_len == 0 ) {
1875 * we assume the string has enough room for the hex encoding
1878 for ( s = 0, d = 0, end = val->bv_len - 1; s < val->bv_len; ) {
1879 ber_len_t cl = LDAP_UTF8_CHARLEN( &val->bv_val[ s ] );
1882 * there might be some chars we want to escape in form
1883 * of a couple of hexdigits for optimization purposes
1885 if ( ( cl > 1 && !LDAP_DN_IS_PRETTY( flags ) )
1886 #ifdef PRETTY_ESCAPE
1887 || LDAP_DN_WILLESCAPE_HEX( flags, val->bv_val[ s ] )
1888 #else /* ! PRETTY_ESCAPE */
1889 || LDAP_DN_NEEDESCAPE( val->bv_val[ s ] )
1890 || ( d == 0 && LDAP_DN_NEEDESCAPE_LEAD( val->bv_val[ s ] ) )
1891 || ( s == end && LDAP_DN_NEEDESCAPE_TRAIL( val->bv_val[ s ] ) )
1893 #endif /* ! PRETTY_ESCAPE */
1897 byte2hexpair( &val->bv_val[ s ], &str[ d ] );
1902 } else if ( cl > 1 ) {
1904 str[ d++ ] = val->bv_val[ s++ ];
1908 #ifdef PRETTY_ESCAPE
1909 if ( LDAP_DN_NEEDESCAPE( val->bv_val[ s ] )
1910 || ( d == 0 && LDAP_DN_NEEDESCAPE_LEAD( val->bv_val[ s ] ) )
1911 || ( s == end && LDAP_DN_NEEDESCAPE_TRAIL( val->bv_val[ s ] ) ) ) {
1913 if ( !LDAP_DN_IS_PRETTY( flags ) ) {
1914 byte2hexpair( &val->bv_val[ s ], &str[ d ] );
1920 #endif /* PRETTY_ESCAPE */
1921 str[ d++ ] = val->bv_val[ s++ ];
1931 * Length of the IA5 string representation (no UTF-8 allowed)
1934 strval2IA5strlen( struct berval *val, unsigned flags, ber_len_t *len )
1943 if ( val->bv_len == 0 ) {
1947 if ( flags & LDAP_AVA_NONPRINTABLE ) {
1949 * Turn value into a binary encoded BER
1954 for ( l = 0, p = val->bv_val; p[ 0 ]; p++ ) {
1955 if ( LDAP_DN_NEEDESCAPE( p[ 0 ] )
1956 || ( p == val->bv_val && LDAP_DN_NEEDESCAPE_LEAD( p[ 0 ] ) )
1957 || ( !p[ 1 ] && LDAP_DN_NEEDESCAPE_TRAIL( p[ 0 ] ) ) ) {
1972 * convert to string representation (np UTF-8)
1973 * assume the destination has enough room for escaping
1976 strval2IA5str( struct berval *val, char *str, unsigned flags, ber_len_t *len )
1978 ber_len_t s, d, end;
1984 if ( val->bv_len == 0 ) {
1989 if ( flags & LDAP_AVA_NONPRINTABLE ) {
1991 * Turn value into a binary encoded BER
1998 * we assume the string has enough room for the hex encoding
2002 for ( s = 0, d = 0, end = val->bv_len - 1; s < val->bv_len; ) {
2003 if ( LDAP_DN_NEEDESCAPE( val->bv_val[ s ] )
2004 || ( s == 0 && LDAP_DN_NEEDESCAPE_LEAD( val->bv_val[ s ] ) )
2005 || ( s == end && LDAP_DN_NEEDESCAPE_TRAIL( val->bv_val[ s ] ) ) ) {
2008 str[ d++ ] = val->bv_val[ s++ ];
2018 * Length of the (supposedly) DCE string representation,
2019 * accounting for escaped hex of UTF-8 chars
2022 strval2DCEstrlen( struct berval *val, unsigned flags, ber_len_t *len )
2031 if ( val->bv_len == 0 ) {
2035 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2037 * FIXME: Turn the value into a binary encoded BER?
2042 for ( l = 0, p = val->bv_val; p[ 0 ]; p++ ) {
2043 if ( LDAP_DN_NEEDESCAPE_DCE( p[ 0 ] ) ) {
2058 * convert to (supposedly) DCE string representation,
2059 * escaping with hex the UTF-8 stuff;
2060 * assume the destination has enough room for escaping
2063 strval2DCEstr( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2071 if ( val->bv_len == 0 ) {
2076 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2078 * FIXME: Turn the value into a binary encoded BER?
2086 * we assume the string has enough room for the hex encoding
2090 for ( s = 0, d = 0; s < val->bv_len; ) {
2091 if ( LDAP_DN_NEEDESCAPE_DCE( val->bv_val[ s ] ) ) {
2094 str[ d++ ] = val->bv_val[ s++ ];
2104 * Length of the (supposedly) AD canonical string representation,
2105 * accounting for escaped hex of UTF-8 chars
2108 strval2ADstrlen( struct berval *val, unsigned flags, ber_len_t *len )
2117 if ( val->bv_len == 0 ) {
2121 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2123 * FIXME: Turn the value into a binary encoded BER?
2128 for ( l = 0, p = val->bv_val; p[ 0 ]; p++ ) {
2129 if ( LDAP_DN_NEEDESCAPE_AD( p[ 0 ] ) ) {
2144 * convert to (supposedly) AD string representation,
2145 * escaping with hex the UTF-8 stuff;
2146 * assume the destination has enough room for escaping
2149 strval2ADstr( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2157 if ( val->bv_len == 0 ) {
2162 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2164 * FIXME: Turn the value into a binary encoded BER?
2172 * we assume the string has enough room for the hex encoding
2176 for ( s = 0, d = 0; s < val->bv_len; ) {
2177 if ( LDAP_DN_NEEDESCAPE_AD( val->bv_val[ s ] ) ) {
2180 str[ d++ ] = val->bv_val[ s++ ];
2190 * If the DN is terminated by single-AVA RDNs with attribute type of "dc",
2191 * the first part of the AD representation of the DN is written in DNS
2192 * form, i.e. dot separated domain name components (as suggested
2193 * by Luke Howard, http://www.padl.com/~lukeh)
2196 dn2domain( LDAPDN *dn, struct berval *bv, int pos, int *iRDN )
2199 int domain = 0, first = 1;
2200 ber_len_t l = 1; /* we move the null also */
2203 /* we are guaranteed there's enough memory in str */
2209 assert( *iRDN >= 0 );
2211 str = bv->bv_val + pos;
2213 for ( i = *iRDN; i >= 0; i-- ) {
2217 assert( dn[ 0 ][ i ] );
2220 assert( rdn[ 0 ][ 0 ] );
2221 ava = rdn[ 0 ][ 0 ];
2223 if ( !LDAP_DN_IS_RDN_DC( rdn ) ) {
2231 AC_MEMCPY( str, ava->la_value.bv_val,
2232 ava->la_value.bv_len + 1);
2233 l += ava->la_value.bv_len;
2236 AC_MEMCPY( str + ava->la_value.bv_len + 1, bv->bv_val, l);
2237 AC_MEMCPY( str, ava->la_value.bv_val,
2238 ava->la_value.bv_len );
2239 str[ ava->la_value.bv_len ] = '.';
2240 l += ava->la_value.bv_len + 1;
2245 bv->bv_len = pos + l - 1;
2251 rdn2strlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len,
2252 int ( *s2l )( struct berval *v, unsigned f, ber_len_t *l ) )
2259 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
2260 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
2262 /* len(type) + '=' + '+' | ',' */
2263 l += ava->la_attr.bv_len + 2;
2265 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2266 /* octothorpe + twice the length */
2267 l += 1 + 2 * ava->la_value.bv_len;
2271 unsigned f = flags | ava->la_flags;
2273 if ( ( *s2l )( &ava->la_value, f, &vl ) ) {
2286 rdn2str( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len,
2287 int ( *s2s ) ( struct berval *v, char * s, unsigned f, ber_len_t *l ) )
2292 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
2293 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
2295 AC_MEMCPY( &str[ l ], ava->la_attr.bv_val,
2296 ava->la_attr.bv_len );
2297 l += ava->la_attr.bv_len;
2301 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2303 if ( binval2hexstr( &ava->la_value, &str[ l ] ) ) {
2306 l += 2 * ava->la_value.bv_len;
2310 unsigned f = flags | ava->la_flags;
2312 if ( ( *s2s )( &ava->la_value, &str[ l ], f, &vl ) ) {
2317 str[ l++ ] = ( rdn[ 0 ][ iAVA + 1 ] ? '+' : ',' );
2326 rdn2DCEstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len )
2333 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
2334 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
2336 /* len(type) + '=' + ',' | '/' */
2337 l += ava->la_attr.bv_len + 2;
2339 switch ( ava->la_flags ) {
2340 case LDAP_AVA_BINARY:
2341 /* octothorpe + twice the length */
2342 l += 1 + 2 * ava->la_value.bv_len;
2345 case LDAP_AVA_STRING: {
2347 unsigned f = flags | ava->la_flags;
2349 if ( strval2DCEstrlen( &ava->la_value, f, &vl ) ) {
2367 rdn2DCEstr( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len, int first )
2372 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
2373 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
2378 str[ l++ ] = ( iAVA ? ',' : '/' );
2381 AC_MEMCPY( &str[ l ], ava->la_attr.bv_val,
2382 ava->la_attr.bv_len );
2383 l += ava->la_attr.bv_len;
2387 switch ( ava->la_flags ) {
2388 case LDAP_AVA_BINARY:
2390 if ( binval2hexstr( &ava->la_value, &str[ l ] ) ) {
2393 l += 2 * ava->la_value.bv_len;
2396 case LDAP_AVA_STRING: {
2398 unsigned f = flags | ava->la_flags;
2400 if ( strval2DCEstr( &ava->la_value, &str[ l ], f, &vl ) ) {
2418 rdn2UFNstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len )
2428 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
2429 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
2432 l += ( rdn[ 0 ][ iAVA + 1 ] ? 3 : 2 );
2434 /* FIXME: are binary values allowed in UFN? */
2435 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2436 /* octothorpe + twice the value */
2437 l += 1 + 2 * ava->la_value.bv_len;
2441 unsigned f = flags | ava->la_flags;
2443 if ( strval2strlen( &ava->la_value, f, &vl ) ) {
2456 rdn2UFNstr( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len )
2461 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
2462 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
2464 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2466 if ( binval2hexstr( &ava->la_value, &str[ l ] ) ) {
2469 l += 2 * ava->la_value.bv_len;
2473 unsigned f = flags | ava->la_flags;
2475 if ( strval2str( &ava->la_value, &str[ l ], f, &vl ) ) {
2481 if ( rdn[ 0 ][ iAVA + 1 ]) {
2482 AC_MEMCPY( &str[ l ], " + ", 3 );
2486 AC_MEMCPY( &str[ l ], ", ", 2 );
2497 rdn2ADstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len )
2507 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
2508 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
2513 /* FIXME: are binary values allowed in UFN? */
2514 switch ( ava->la_flags ) {
2515 case LDAP_AVA_BINARY:
2516 /* octothorpe + twice the value */
2517 l += 1 + 2 * ava->la_value.bv_len;
2520 case LDAP_AVA_STRING: {
2522 unsigned f = flags | ava->la_flags;
2524 if ( strval2ADstrlen( &ava->la_value, f, &vl ) ) {
2542 rdn2ADstr( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len, int first )
2547 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
2548 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
2553 str[ l++ ] = ( iAVA ? ',' : '/' );
2556 switch ( ava->la_flags ) {
2557 case LDAP_AVA_BINARY:
2559 if ( binval2hexstr( &ava->la_value, &str[ l ] ) ) {
2562 l += 2 * ava->la_value.bv_len;
2565 case LDAP_AVA_STRING: {
2567 unsigned f = flags | ava->la_flags;
2569 if ( strval2ADstr( &ava->la_value, &str[ l ], f, &vl ) ) {
2589 * Returns in str a string representation of rdn based on flags.
2590 * There is some duplication of code between this and ldap_dn2str;
2591 * this is wanted to reduce the allocation of temporary buffers.
2594 ldap_rdn2str( LDAPRDN *rdn, char **str, unsigned flags )
2601 if((flags & LDAP_DN_FORMAT_MASK) == LDAP_DN_FORMAT_LBER) {
2602 return LDAP_PARAM_ERROR;
2605 rc = ldap_rdn2bv( rdn, &bv, flags );
2611 ldap_rdn2bv( LDAPRDN *rdn, struct berval *bv, unsigned flags )
2621 if ( rdn == NULL ) {
2622 bv->bv_val = LDAP_STRDUP( "" );
2623 return( LDAP_SUCCESS );
2627 * This routine wastes "back" bytes at the end of the string
2630 switch ( LDAP_DN_FORMAT( flags ) ) {
2631 case LDAP_DN_FORMAT_LDAPV3:
2632 if ( rdn2strlen( rdn, flags, &l, strval2strlen ) ) {
2633 return LDAP_DECODING_ERROR;
2637 case LDAP_DN_FORMAT_LDAPV2:
2638 if ( rdn2strlen( rdn, flags, &l, strval2IA5strlen ) ) {
2639 return LDAP_DECODING_ERROR;
2643 case LDAP_DN_FORMAT_UFN:
2644 if ( rdn2UFNstrlen( rdn, flags, &l ) ) {
2645 return LDAP_DECODING_ERROR;
2649 case LDAP_DN_FORMAT_DCE:
2650 if ( rdn2DCEstrlen( rdn, flags, &l ) ) {
2651 return LDAP_DECODING_ERROR;
2655 case LDAP_DN_FORMAT_AD_CANONICAL:
2656 if ( rdn2ADstrlen( rdn, flags, &l ) ) {
2657 return LDAP_DECODING_ERROR;
2662 return( LDAP_PARAM_ERROR );
2665 bv->bv_val = LDAP_MALLOC( l + 1 );
2667 switch ( LDAP_DN_FORMAT( flags ) ) {
2668 case LDAP_DN_FORMAT_LDAPV3:
2669 rc = rdn2str( rdn, bv->bv_val, flags, &l, strval2str );
2673 case LDAP_DN_FORMAT_LDAPV2:
2674 rc = rdn2str( rdn, bv->bv_val, flags, &l, strval2IA5str );
2678 case LDAP_DN_FORMAT_UFN:
2679 rc = rdn2UFNstr( rdn, bv->bv_val, flags, &l );
2683 case LDAP_DN_FORMAT_DCE:
2684 rc = rdn2DCEstr( rdn, bv->bv_val, flags, &l, 1 );
2688 case LDAP_DN_FORMAT_AD_CANONICAL:
2689 rc = rdn2ADstr( rdn, bv->bv_val, flags, &l, 1 );
2694 /* need at least one of the previous */
2695 return LDAP_PARAM_ERROR;
2699 ldap_memfree( bv->bv_val );
2703 bv->bv_len = l - back;
2704 bv->bv_val[ bv->bv_len ] = '\0';
2706 return LDAP_SUCCESS;
2710 * Very bulk implementation; many optimizations can be performed
2711 * - a NULL dn results in an empty string ""
2714 * a) what do we do if a UTF-8 string must be converted in LDAPv2?
2715 * we must encode it in binary form ('#' + HEXPAIRs)
2716 * b) does DCE/AD support UTF-8?
2717 * no clue; don't think so.
2718 * c) what do we do when binary values must be converted in UTF/DCE/AD?
2719 * use binary encoded BER
2721 int ldap_dn2str( LDAPDN *dn, char **str, unsigned flags )
2728 if((flags & LDAP_DN_FORMAT_MASK) == LDAP_DN_FORMAT_LBER) {
2729 return LDAP_PARAM_ERROR;
2732 rc = ldap_dn2bv( dn, &bv, flags );
2737 int ldap_dn2bv( LDAPDN *dn, struct berval *bv, unsigned flags )
2740 int rc = LDAP_ENCODING_ERROR;
2743 /* stringifying helpers for LDAPv3/LDAPv2 */
2744 int ( *sv2l ) ( struct berval *v, unsigned f, ber_len_t *l );
2745 int ( *sv2s ) ( struct berval *v, char *s, unsigned f, ber_len_t *l );
2752 Debug( LDAP_DEBUG_TRACE, "=> ldap_dn2bv(%u)\n%s%s", flags, "", "" );
2755 * a null dn means an empty dn string
2756 * FIXME: better raise an error?
2759 bv->bv_val = LDAP_STRDUP( "" );
2760 return( LDAP_SUCCESS );
2763 switch ( LDAP_DN_FORMAT( flags ) ) {
2764 case LDAP_DN_FORMAT_LDAPV3:
2765 sv2l = strval2strlen;
2769 case LDAP_DN_FORMAT_LDAPV2:
2770 sv2l = strval2IA5strlen;
2771 sv2s = strval2IA5str;
2774 for ( iRDN = 0, len = 0; dn[ 0 ][ iRDN ]; iRDN++ ) {
2776 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
2778 if ( rdn2strlen( rdn, flags, &rdnl, sv2l ) ) {
2779 goto return_results;
2785 if ( ( bv->bv_val = LDAP_MALLOC( len + 1 ) ) == NULL ) {
2786 rc = LDAP_NO_MEMORY;
2790 for ( l = 0, iRDN = 0; dn[ 0 ][ iRDN ]; iRDN++ ) {
2792 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
2794 if ( rdn2str( rdn, &bv->bv_val[ l ], flags,
2796 LDAP_FREE( bv->bv_val );
2798 goto return_results;
2806 * trim the last ',' (the allocated memory
2807 * is one byte longer than required)
2809 bv->bv_len = len - 1;
2810 bv->bv_val[ bv->bv_len ] = '\0';
2815 case LDAP_DN_FORMAT_UFN: {
2818 * FIXME: quoting from RFC 1781:
2820 To take a distinguished name, and generate a name of this format with
2821 attribute types omitted, the following steps are followed.
2823 1. If the first attribute is of type CommonName, the type may be
2826 2. If the last attribute is of type Country, the type may be
2829 3. If the last attribute is of type Country, the last
2830 Organisation attribute may have the type omitted.
2832 4. All attributes of type OrganisationalUnit may have the type
2833 omitted, unless they are after an Organisation attribute or
2834 the first attribute is of type OrganisationalUnit.
2836 * this should be the pedantic implementation.
2838 * Here the standard implementation reflects
2839 * the one historically provided by OpenLDAP
2840 * (and UMIch, I presume), with the variant
2841 * of spaces and plusses (' + ') separating
2844 * A non-standard but nice implementation could
2845 * be to turn the final "dc" attributes into a
2846 * dot-separated domain.
2848 * Other improvements could involve the use of
2849 * friendly country names and so.
2852 int leftmost_dc = -1;
2854 #endif /* DC_IN_UFN */
2856 for ( iRDN = 0, len = 0; dn[ 0 ][ iRDN ]; iRDN++ ) {
2858 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
2860 if ( rdn2UFNstrlen( rdn, flags, &rdnl ) ) {
2861 goto return_results;
2866 if ( LDAP_DN_IS_RDN_DC( rdn ) ) {
2867 if ( leftmost_dc == -1 ) {
2873 #endif /* DC_IN_UFN */
2876 if ( ( bv->bv_val = LDAP_MALLOC( len + 1 ) ) == NULL ) {
2877 rc = LDAP_NO_MEMORY;
2882 if ( leftmost_dc == -1 ) {
2883 #endif /* DC_IN_UFN */
2884 for ( l = 0, iRDN = 0; dn[ 0 ][ iRDN ]; iRDN++ ) {
2886 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
2888 if ( rdn2UFNstr( rdn, &bv->bv_val[ l ],
2890 LDAP_FREE( bv->bv_val );
2892 goto return_results;
2898 * trim the last ', ' (the allocated memory
2899 * is two bytes longer than required)
2901 bv->bv_len = len - 2;
2902 bv->bv_val[ bv->bv_len ] = '\0';
2905 last_iRDN = iRDN - 1;
2907 for ( l = 0, iRDN = 0; iRDN < leftmost_dc; iRDN++ ) {
2909 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
2911 if ( rdn2UFNstr( rdn, &bv->bv_val[ l ],
2913 LDAP_FREE( bv->bv_val );
2915 goto return_results;
2920 if ( !dn2domain( dn, bv, l, &last_iRDN ) ) {
2921 LDAP_FREE( bv->bv_val );
2923 goto return_results;
2926 /* the string is correctly terminated by dn2domain */
2928 #endif /* DC_IN_UFN */
2934 case LDAP_DN_FORMAT_DCE:
2936 for ( iRDN = 0, len = 0; dn[ 0 ][ iRDN ]; iRDN++ ) {
2938 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
2940 if ( rdn2DCEstrlen( rdn, flags, &rdnl ) ) {
2941 goto return_results;
2947 if ( ( bv->bv_val = LDAP_MALLOC( len + 1 ) ) == NULL ) {
2948 rc = LDAP_NO_MEMORY;
2952 for ( l = 0; iRDN--; ) {
2954 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
2956 if ( rdn2DCEstr( rdn, &bv->bv_val[ l ], flags,
2958 LDAP_FREE( bv->bv_val );
2960 goto return_results;
2968 bv->bv_val[ bv->bv_len ] = '\0';
2973 case LDAP_DN_FORMAT_AD_CANONICAL: {
2976 * Sort of UFN for DCE DNs: a slash ('/') separated
2977 * global->local DN with no types; strictly speaking,
2978 * the naming context should be a domain, which is
2979 * written in DNS-style, e.g. dot-deparated.
2983 * "givenName=Bill+sn=Gates,ou=People,dc=microsoft,dc=com"
2987 * "microsoft.com/People/Bill,Gates"
2989 for ( iRDN = 0, len = -1; dn[ 0 ][ iRDN ]; iRDN++ ) {
2991 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
2993 if ( rdn2ADstrlen( rdn, flags, &rdnl ) ) {
2994 goto return_results;
3000 if ( ( bv->bv_val = LDAP_MALLOC( len + 1 ) ) == NULL ) {
3001 rc = LDAP_NO_MEMORY;
3006 if ( iRDN && dn2domain( dn, bv, 0, &iRDN ) ) {
3007 for ( l = bv->bv_len; iRDN >= 0 ; iRDN-- ) {
3009 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
3011 if ( rdn2ADstr( rdn, &bv->bv_val[ l ],
3012 flags, &rdnl, 0 ) ) {
3013 LDAP_FREE( bv->bv_val );
3015 goto return_results;
3024 * Strictly speaking, AD canonical requires
3025 * a DN to be in the form "..., dc=smtg",
3026 * i.e. terminated by a domain component
3028 if ( flags & LDAP_DN_PEDANTIC ) {
3029 LDAP_FREE( bv->bv_val );
3031 rc = LDAP_ENCODING_ERROR;
3035 for ( l = 0; iRDN >= 0 ; iRDN-- ) {
3037 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
3039 if ( rdn2ADstr( rdn, &bv->bv_val[ l ],
3040 flags, &rdnl, first ) ) {
3041 LDAP_FREE( bv->bv_val );
3043 goto return_results;
3053 bv->bv_val[ bv->bv_len ] = '\0';
3060 return LDAP_PARAM_ERROR;
3063 Debug( LDAP_DEBUG_TRACE, "<= ldap_dn2bv(%s,%u)=%d\n",
3064 bv->bv_val, flags, rc );