3 * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
7 * Copyright (c) 1994 Regents of the University of Michigan.
17 #include <ac/stdlib.h>
18 #include <ac/socket.h>
19 #include <ac/string.h>
23 #include "ldap_schema.h"
25 /* extension to UFN that turns trailing "dc=value" rdns in DNS style,
26 * e.g. "ou=People,dc=openldap,dc=org" => "People, openldap.org" */
28 /* #define PRETTY_ESCAPE */
30 /* parsing/printing routines */
31 static int str2strval( const char *str, struct berval *val,
32 const char **next, unsigned flags, unsigned *retFlags );
33 static int DCE2strval( const char *str, struct berval *val,
34 const char **next, unsigned flags );
35 static int IA52strval( const char *str, struct berval *val,
36 const char **next, unsigned flags );
37 static int quotedIA52strval( const char *str, struct berval *val,
38 const char **next, unsigned flags );
39 static int hexstr2binval( const char *str, struct berval *val,
40 const char **next, unsigned flags );
41 static int hexstr2bin( const char *str, char *c );
42 static int byte2hexpair( const char *val, char *pair );
43 static int binval2hexstr( struct berval *val, char *str );
44 static int strval2strlen( struct berval *val, unsigned flags,
46 static int strval2str( struct berval *val, char *str, unsigned flags,
48 static int strval2IA5strlen( struct berval *val, unsigned flags,
50 static int strval2IA5str( struct berval *val, char *str, unsigned flags,
52 static int strval2DCEstrlen( struct berval *val, unsigned flags,
54 static int strval2DCEstr( struct berval *val, char *str, unsigned flags,
56 static int strval2ADstrlen( struct berval *val, unsigned flags,
58 static int strval2ADstr( struct berval *val, char *str, unsigned flags,
60 static int dn2domain( LDAPDN *dn, struct berval *bv, int pos, int *iRDN );
63 static LDAPAVA * ldapava_new(
64 const struct berval *attr, const struct berval *val, unsigned flags );
66 /* Higher level helpers */
67 static int rdn2strlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len,
68 int ( *s2l )( struct berval *, unsigned, ber_len_t * ) );
69 static int rdn2str( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len,
70 int ( *s2s )( struct berval *, char *, unsigned, ber_len_t * ));
71 static int rdn2UFNstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len );
72 static int rdn2UFNstr( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len );
73 static int rdn2DCEstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len );
74 static int rdn2DCEstr( LDAPRDN *rdn, char *str, unsigned flag, ber_len_t *len, int first );
75 static int rdn2ADstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len );
76 static int rdn2ADstr( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len, int first );
79 * RFC 1823 ldap_get_dn
82 ldap_get_dn( LDAP *ld, LDAPMessage *entry )
87 Debug( LDAP_DEBUG_TRACE, "ldap_get_dn\n", 0, 0, 0 );
89 if ( entry == NULL ) {
90 ld->ld_errno = LDAP_PARAM_ERROR;
94 tmp = *entry->lm_ber; /* struct copy */
95 if ( ber_scanf( &tmp, "{a" /*}*/, &dn ) == LBER_ERROR ) {
96 ld->ld_errno = LDAP_DECODING_ERROR;
104 * RFC 1823 ldap_dn2ufn
107 ldap_dn2ufn( LDAP_CONST char *dn )
111 Debug( LDAP_DEBUG_TRACE, "ldap_dn2ufn\n", 0, 0, 0 );
113 ( void )ldap_dn_normalize( dn, LDAP_DN_FORMAT_LDAP,
114 &out, LDAP_DN_FORMAT_UFN );
120 * RFC 1823 ldap_explode_dn
123 ldap_explode_dn( LDAP_CONST char *dn, int notypes )
126 char **values = NULL;
128 unsigned flag = notypes ? LDAP_DN_FORMAT_UFN : LDAP_DN_FORMAT_LDAPV3;
130 Debug( LDAP_DEBUG_TRACE, "ldap_explode_dn\n", 0, 0, 0 );
132 if ( ldap_str2dn( dn, &tmpDN, LDAP_DN_FORMAT_LDAP )
137 if( tmpDN == NULL ) {
138 values = LDAP_MALLOC( sizeof( char * ) );
139 if( values == NULL ) return NULL;
145 for ( iRDN = 0; tmpDN[ 0 ][ iRDN ]; iRDN++ );
147 values = LDAP_MALLOC( sizeof( char * ) * ( 1 + iRDN ) );
148 if ( values == NULL ) {
149 ldap_dnfree( tmpDN );
153 for ( iRDN = 0; tmpDN[ 0 ][ iRDN ]; iRDN++ ) {
154 ldap_rdn2str( tmpDN[ 0 ][ iRDN ], &values[ iRDN ], flag );
156 ldap_dnfree( tmpDN );
157 values[ iRDN ] = NULL;
163 ldap_explode_rdn( LDAP_CONST char *rdn, int notypes )
166 char **values = NULL;
170 Debug( LDAP_DEBUG_TRACE, "ldap_explode_rdn\n", 0, 0, 0 );
173 * we only parse the first rdn
174 * FIXME: we prefer efficiency over checking if the _ENTIRE_
177 if ( ldap_str2rdn( rdn, &tmpRDN, (char **) &p, LDAP_DN_FORMAT_LDAP )
182 for ( iAVA = 0; tmpRDN[ 0 ][ iAVA ]; iAVA++ ) ;
183 values = LDAP_MALLOC( sizeof( char * ) * ( 1 + iAVA ) );
184 if ( values == NULL ) {
185 ldap_rdnfree( tmpRDN );
189 for ( iAVA = 0; tmpRDN[ 0 ][ iAVA ]; iAVA++ ) {
190 ber_len_t l = 0, vl, al = 0;
192 LDAPAVA *ava = tmpRDN[ 0 ][ iAVA ];
194 if ( ava->la_flags == LDAP_AVA_BINARY ) {
195 vl = 1 + 2 * ava->la_value.bv_len;
198 if ( strval2strlen( &ava->la_value,
199 ava->la_flags, &vl ) ) {
205 al = ava->la_attr.bv_len;
206 l = vl + ava->la_attr.bv_len + 1;
208 str = LDAP_MALLOC( l + 1 );
209 AC_MEMCPY( str, ava->la_attr.bv_val,
210 ava->la_attr.bv_len );
215 str = LDAP_MALLOC( l + 1 );
218 if ( ava->la_flags == LDAP_AVA_BINARY ) {
220 if ( binval2hexstr( &ava->la_value, &str[ al ] ) ) {
225 if ( strval2str( &ava->la_value, &str[ al ],
226 ava->la_flags, &vl ) ) {
232 values[ iAVA ] = str;
234 values[ iAVA ] = NULL;
236 ldap_rdnfree( tmpRDN );
241 LBER_VFREE( values );
242 ldap_rdnfree( tmpRDN );
247 ldap_dn2dcedn( LDAP_CONST char *dn )
251 Debug( LDAP_DEBUG_TRACE, "ldap_dn2dcedn\n", 0, 0, 0 );
253 ( void )ldap_dn_normalize( dn, LDAP_DN_FORMAT_LDAP,
254 &out, LDAP_DN_FORMAT_DCE );
260 ldap_dcedn2dn( LDAP_CONST char *dce )
264 Debug( LDAP_DEBUG_TRACE, "ldap_dcedn2dn\n", 0, 0, 0 );
266 ( void )ldap_dn_normalize( dce, LDAP_DN_FORMAT_DCE, &out, LDAP_DN_FORMAT_LDAPV3 );
272 ldap_dn2ad_canonical( LDAP_CONST char *dn )
276 Debug( LDAP_DEBUG_TRACE, "ldap_dn2ad_canonical\n", 0, 0, 0 );
278 ( void )ldap_dn_normalize( dn, LDAP_DN_FORMAT_LDAP,
279 &out, LDAP_DN_FORMAT_AD_CANONICAL );
285 * function that changes the string representation of dnin
286 * from ( fin & LDAP_DN_FORMAT_MASK ) to ( fout & LDAP_DN_FORMAT_MASK )
289 * LDAP_DN_FORMAT_LDAP (rfc 2253 and ldapbis liberal,
290 * plus some rfc 1779)
291 * LDAP_DN_FORMAT_LDAPV3 (rfc 2253 and ldapbis)
292 * LDAP_DN_FORMAT_LDAPV2 (rfc 1779)
293 * LDAP_DN_FORMAT_DCE (?)
295 * fout can be any of the above except
296 * LDAP_DN_FORMAT_LDAP
298 * LDAP_DN_FORMAT_UFN (rfc 1781, partial and with extensions)
299 * LDAP_DN_FORMAT_AD_CANONICAL (?)
302 ldap_dn_normalize( LDAP_CONST char *dnin,
303 unsigned fin, char **dnout, unsigned fout )
306 LDAPDN *tmpDN = NULL;
308 Debug( LDAP_DEBUG_TRACE, "ldap_dn_normalize\n", 0, 0, 0 );
314 if ( dnin == NULL ) {
315 return( LDAP_SUCCESS );
318 rc = ldap_str2dn( dnin , &tmpDN, fin );
319 if ( rc != LDAP_SUCCESS ) {
323 rc = ldap_dn2str( tmpDN, dnout, fout );
325 ldap_dnfree( tmpDN );
333 /* #define B4ATTRTYPE 0x0001 */
334 #define B4OIDATTRTYPE 0x0002
335 #define B4STRINGATTRTYPE 0x0003
337 #define B4AVAEQUALS 0x0100
338 #define B4AVASEP 0x0200
339 #define B4RDNSEP 0x0300
340 #define GOTAVA 0x0400
342 #define B4ATTRVALUE 0x0010
343 #define B4STRINGVALUE 0x0020
344 #define B4IA5VALUEQUOTED 0x0030
345 #define B4IA5VALUE 0x0040
346 #define B4BINARYVALUE 0x0050
349 * Helpers (mostly from slap.h)
350 * c is assumed to Unicode in an ASCII compatible format (UTF-8)
351 * Macros assume "C" Locale (ASCII)
353 #define LDAP_DN_ASCII_SPACE(c) \
354 ( (c) == ' ' || (c) == '\t' || (c) == '\n' || (c) == '\r' )
355 #define LDAP_DN_ASCII_LOWER(c) LDAP_LOWER(c)
356 #define LDAP_DN_ASCII_UPPER(c) LDAP_UPPER(c)
357 #define LDAP_DN_ASCII_ALPHA(c) LDAP_ALPHA(c)
359 #define LDAP_DN_ASCII_DIGIT(c) LDAP_DIGIT(c)
360 #define LDAP_DN_ASCII_LCASE_HEXALPHA(c) LDAP_HEXLOWER(c)
361 #define LDAP_DN_ASCII_UCASE_HEXALPHA(c) LDAP_HEXUPPER(c)
362 #define LDAP_DN_ASCII_HEXDIGIT(c) LDAP_HEX(c)
363 #define LDAP_DN_ASCII_ALNUM(c) LDAP_ALNUM(c)
364 #define LDAP_DN_ASCII_PRINTABLE(c) ( (c) >= ' ' && (c) <= '~' )
367 #define LDAP_DN_OID_LEADCHAR(c) LDAP_DIGIT(c)
368 #define LDAP_DN_DESC_LEADCHAR(c) LDAP_ALPHA(c)
369 #define LDAP_DN_DESC_CHAR(c) LDAP_LDH(c)
370 #define LDAP_DN_LANG_SEP(c) ( (c) == ';' )
371 #define LDAP_DN_ATTRDESC_CHAR(c) \
372 ( LDAP_DN_DESC_CHAR(c) || LDAP_DN_LANG_SEP(c) )
374 /* special symbols */
375 #define LDAP_DN_AVA_EQUALS(c) ( (c) == '=' )
376 #define LDAP_DN_AVA_SEP(c) ( (c) == '+' )
377 #define LDAP_DN_RDN_SEP(c) ( (c) == ',' )
378 #define LDAP_DN_RDN_SEP_V2(c) ( LDAP_DN_RDN_SEP(c) || (c) == ';' )
379 #define LDAP_DN_OCTOTHORPE(c) ( (c) == '#' )
380 #define LDAP_DN_QUOTES(c) ( (c) == '\"' )
381 #define LDAP_DN_ESCAPE(c) ( (c) == '\\' )
382 #define LDAP_DN_VALUE_END(c) \
383 ( LDAP_DN_RDN_SEP(c) || LDAP_DN_AVA_SEP(c) )
384 #define LDAP_DN_NE(c) \
385 ( LDAP_DN_RDN_SEP_V2(c) || LDAP_DN_AVA_SEP(c) \
386 || LDAP_DN_QUOTES(c) || (c) == '<' || (c) == '>' )
387 #define LDAP_DN_NEEDESCAPE(c) \
388 ( LDAP_DN_ESCAPE(c) || LDAP_DN_NE(c) )
389 #define LDAP_DN_NEEDESCAPE_LEAD(c) \
390 ( LDAP_DN_ASCII_SPACE(c) || LDAP_DN_OCTOTHORPE(c) || LDAP_DN_NE(c) )
391 #define LDAP_DN_NEEDESCAPE_TRAIL(c) \
392 ( LDAP_DN_ASCII_SPACE(c) || LDAP_DN_NEEDESCAPE(c) )
393 #define LDAP_DN_WILLESCAPE_CHAR(c) \
394 ( LDAP_DN_RDN_SEP(c) || LDAP_DN_AVA_SEP(c) || LDAP_DN_ESCAPE(c) )
395 #define LDAP_DN_IS_PRETTY(f) ( (f) & LDAP_DN_PRETTY )
396 #define LDAP_DN_WILLESCAPE_HEX(f, c) \
397 ( ( !LDAP_DN_IS_PRETTY( f ) ) && LDAP_DN_WILLESCAPE_CHAR(c) )
400 #define LDAP_DN_VALUE_END_V2(c) \
401 ( LDAP_DN_RDN_SEP_V2(c) || LDAP_DN_AVA_SEP(c) )
403 #define LDAP_DN_V2_SPECIAL(c) \
404 ( LDAP_DN_RDN_SEP_V2(c) || LDAP_DN_AVA_EQUALS(c) \
405 || LDAP_DN_AVA_SEP(c) || (c) == '<' || (c) == '>' \
406 || LDAP_DN_OCTOTHORPE(c) )
407 #define LDAP_DN_V2_PAIR(c) \
408 ( LDAP_DN_V2_SPECIAL(c) || LDAP_DN_ESCAPE(c) || LDAP_DN_QUOTES(c) )
411 * DCE (mostly from Luke Howard and IBM implementation for AIX)
413 * From: "Application Development Guide - Directory Services" (FIXME: add link?)
414 * Here escapes and valid chars for GDS are considered; as soon as more
415 * specific info is found, the macros will be updated.
417 * Chars: 'a'-'z', 'A'-'Z', '0'-'9',
418 * '.', ':', ',', ''', '+', '-', '=', '(', ')', '?', '/', ' '.
420 * Metachars: '/', ',', '=', '\'.
422 * the '\' is used to escape other metachars.
428 * Attribute types must start with alphabetic chars and can contain
429 * alphabetic chars and digits (FIXME: no '-'?). OIDs are allowed.
431 #define LDAP_DN_RDN_SEP_DCE(c) ( (c) == '/' )
432 #define LDAP_DN_AVA_SEP_DCE(c) ( (c) == ',' )
433 #define LDAP_DN_ESCAPE_DCE(c) ( LDAP_DN_ESCAPE(c) )
434 #define LDAP_DN_VALUE_END_DCE(c) \
435 ( LDAP_DN_RDN_SEP_DCE(c) || LDAP_DN_AVA_SEP_DCE(c) )
436 #define LDAP_DN_NEEDESCAPE_DCE(c) \
437 ( LDAP_DN_VALUE_END_DCE(c) || LDAP_DN_AVA_EQUALS(c) )
440 #define LDAP_DN_RDN_SEP_AD(c) ( (c) == '/' )
441 #define LDAP_DN_ESCAPE_AD(c) ( LDAP_DN_ESCAPE(c) )
442 #define LDAP_DN_AVA_SEP_AD(c) ( (c) == ',' ) /* assume same as DCE */
443 #define LDAP_DN_VALUE_END_AD(c) \
444 ( LDAP_DN_RDN_SEP_AD(c) || LDAP_DN_AVA_SEP_AD(c) )
445 #define LDAP_DN_NEEDESCAPE_AD(c) \
446 ( LDAP_DN_VALUE_END_AD(c) || LDAP_DN_AVA_EQUALS(c) )
449 #define LDAP_DN_HEXPAIR(s) \
450 ( LDAP_DN_ASCII_HEXDIGIT((s)[0]) && LDAP_DN_ASCII_HEXDIGIT((s)[1]) )
451 /* better look at the AttributeDescription? */
453 /* FIXME: no composite rdn or non-"dc" types, right?
454 * (what about "dc" in OID form?) */
455 /* FIXME: we do not allow binary values in domain, right? */
456 /* NOTE: use this macro only when ABSOLUTELY SURE rdn IS VALID! */
457 /* NOTE: don't use strcasecmp() as it is locale specific! */
458 #define LDAP_DC_ATTR "dc"
459 #define LDAP_DC_ATTRU "DC"
460 #define LDAP_DN_IS_RDN_DC( r ) \
461 ( (r) && (r)[0][0] && !(r)[0][1] \
462 && ((r)[0][0]->la_flags == LDAP_AVA_STRING) \
463 && ((r)[0][0]->la_attr.bv_len == 2) \
464 && (((r)[0][0]->la_attr.bv_val[0] == LDAP_DC_ATTR[0]) \
465 || ((r)[0][0]->la_attr.bv_val[0] == LDAP_DC_ATTRU[0])) \
466 && (((r)[0][0]->la_attr.bv_val[1] == LDAP_DC_ATTR[1]) \
467 || ((r)[0][0]->la_attr.bv_val[1] == LDAP_DC_ATTRU[1])))
469 /* Composite rules */
470 #define LDAP_DN_ALLOW_ONE_SPACE(f) \
471 ( LDAP_DN_LDAPV2(f) \
472 || !( (f) & LDAP_DN_P_NOSPACEAFTERRDN ) )
473 #define LDAP_DN_ALLOW_SPACES(f) \
474 ( LDAP_DN_LDAPV2(f) \
475 || !( (f) & ( LDAP_DN_P_NOLEADTRAILSPACES | LDAP_DN_P_NOSPACEAFTERRDN ) ) )
476 #define LDAP_DN_LDAP(f) \
477 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_LDAP )
478 #define LDAP_DN_LDAPV3(f) \
479 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_LDAPV3 )
480 #define LDAP_DN_LDAPV2(f) \
481 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_LDAPV2 )
482 #define LDAP_DN_DCE(f) \
483 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_DCE )
484 #define LDAP_DN_UFN(f) \
485 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_UFN )
486 #define LDAP_DN_ADC(f) \
487 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_AD_CANONICAL )
488 #define LDAP_DN_FORMAT(f) ( (f) & LDAP_DN_FORMAT_MASK )
491 * LDAPAVA helpers (will become part of the API for operations
492 * on structural representations of DNs).
495 ldapava_new( const struct berval *attr, const struct berval *val,
503 ava = LDAP_MALLOC( sizeof( LDAPAVA ) + attr->bv_len + 1 );
505 /* should we test it? */
510 ava->la_attr.bv_len = attr->bv_len;
511 ava->la_attr.bv_val = (char *)(ava+1);
512 AC_MEMCPY( ava->la_attr.bv_val, attr->bv_val, attr->bv_len );
513 ava->la_attr.bv_val[attr->bv_len] = '\0';
515 ava->la_value = *val;
516 ava->la_flags = flags;
518 ava->la_private = NULL;
524 ldap_avafree( LDAPAVA *ava )
529 /* ava's private must be freed by caller
530 * (at present let's skip this check because la_private
531 * basically holds static data) */
532 assert( ava->la_private == NULL );
536 /* la_attr is now contiguous with ava, not freed separately */
537 LDAP_FREE( ava->la_attr.bv_val );
539 LDAP_FREE( ava->la_value.bv_val );
545 ldap_rdnfree( LDAPRDN *rdn )
553 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
554 ldap_avafree( rdn[ 0 ][ iAVA ] );
561 ldap_dnfree( LDAPDN *dn )
569 for ( iRDN = 0; dn[ 0 ][ iRDN ]; iRDN++ ) {
570 ldap_rdnfree( dn[ 0 ][ iRDN ] );
577 * Converts a string representation of a DN (in LDAPv3, LDAPv2 or DCE)
578 * into a structural representation of the DN, by separating attribute
579 * types and values encoded in the more appropriate form, which is
580 * string or OID for attribute types and binary form of the BER encoded
581 * value or Unicode string. Formats different from LDAPv3 are parsed
582 * according to their own rules and turned into the more appropriate
583 * form according to LDAPv3.
585 * NOTE: I realize the code is getting spaghettish; it is rather
586 * experimental and will hopefully turn into something more simple
587 * and readable as soon as it works as expected.
590 #define TMP_SLOTS 1024
593 ldap_str2dn( LDAP_CONST char *str, LDAPDN **dn, unsigned flags )
596 int rc = LDAP_DECODING_ERROR;
599 LDAPDN *newDN = NULL;
600 LDAPRDN *newRDN = NULL, *tmpDN[TMP_SLOTS];
605 Debug( LDAP_DEBUG_TRACE, "=> ldap_str2dn(%s,%u)\n%s", str, flags, "" );
609 switch ( LDAP_DN_FORMAT( flags ) ) {
610 case LDAP_DN_FORMAT_LDAP:
611 case LDAP_DN_FORMAT_LDAPV3:
612 case LDAP_DN_FORMAT_LDAPV2:
613 case LDAP_DN_FORMAT_DCE:
616 /* unsupported in str2dn */
617 case LDAP_DN_FORMAT_UFN:
618 case LDAP_DN_FORMAT_AD_CANONICAL:
619 return LDAP_PARAM_ERROR;
621 case LDAP_DN_FORMAT_LBER:
623 return LDAP_PARAM_ERROR;
626 if ( str[ 0 ] == '\0' ) {
631 if ( LDAP_DN_DCE( flags ) ) {
634 * (from Luke Howard: thnx) A RDN separator is required
635 * at the beginning of an (absolute) DN.
637 if ( !LDAP_DN_RDN_SEP_DCE( p[ 0 ] ) ) {
642 } else if ( LDAP_DN_LDAP( flags ) ) {
644 * if dn starts with '/' let's make it a DCE dn
646 if ( LDAP_DN_RDN_SEP_DCE( p[ 0 ] ) ) {
647 flags |= LDAP_DN_FORMAT_DCE;
652 for ( ; p[ 0 ]; p++ ) {
655 err = ldap_str2rdn( p, &newRDN, (char **) &p, flags );
656 if ( err != LDAP_SUCCESS ) {
661 * We expect a rdn separator
664 switch ( LDAP_DN_FORMAT( flags ) ) {
665 case LDAP_DN_FORMAT_LDAPV3:
666 if ( !LDAP_DN_RDN_SEP( p[ 0 ] ) ) {
667 rc = LDAP_DECODING_ERROR;
672 case LDAP_DN_FORMAT_LDAP:
673 case LDAP_DN_FORMAT_LDAPV2:
674 if ( !LDAP_DN_RDN_SEP_V2( p[ 0 ] ) ) {
675 rc = LDAP_DECODING_ERROR;
680 case LDAP_DN_FORMAT_DCE:
681 if ( !LDAP_DN_RDN_SEP_DCE( p[ 0 ] ) ) {
682 rc = LDAP_DECODING_ERROR;
690 tmpDN[nrdns++] = newRDN;
697 assert (nrdns < TMP_SLOTS);
700 * make the static AVA array dynamically rescalable
702 if (nrdns >= TMP_SLOTS) {
703 rc = LDAP_DECODING_ERROR;
708 if ( p[ 0 ] == '\0' ) {
710 * the DN is over, phew
712 newDN = (LDAPDN *)LDAP_MALLOC( sizeof(LDAPDN) +
713 sizeof(LDAPRDN *) * (nrdns+1));
714 if ( newDN == NULL ) {
720 newDN[0] = (LDAPRDN **)(newDN+1);
722 if ( LDAP_DN_DCE( flags ) ) {
723 /* add in reversed order */
724 for ( i=0; i<nrdns; i++ )
725 newDN[0][i] = tmpDN[nrdns-1-i];
727 for ( i=0; i<nrdns; i++ )
728 newDN[0][i] = tmpDN[i];
730 newDN[0][nrdns] = NULL;
739 ldap_rdnfree( newRDN );
742 for (nrdns-- ;nrdns>=0; nrdns-- )
743 ldap_rdnfree( tmpDN[nrdns] );
746 Debug( LDAP_DEBUG_TRACE, "<= ldap_str2dn(%s,%u)=%d\n", str, flags, rc );
755 * Parses a relative DN according to flags up to a rdn separator
756 * or to the end of str.
757 * Returns the rdn and a pointer to the string continuation, which
758 * corresponds to the rdn separator or to '\0' in case the string is over.
761 ldap_str2rdn( LDAP_CONST char *str, LDAPRDN **rdn,
762 char **n_in, unsigned flags )
764 const char **n = (const char **) n_in;
768 int rc = LDAP_DECODING_ERROR;
769 int attrTypeEncoding = LDAP_AVA_STRING,
770 attrValueEncoding = LDAP_AVA_STRING;
772 struct berval attrType = { 0, NULL };
773 struct berval attrValue = { 0, NULL };
775 LDAPRDN *newRDN = NULL;
776 LDAPAVA *tmpRDN[TMP_SLOTS];
779 assert( rdn || flags & LDAP_DN_SKIP );
782 Debug( LDAP_DEBUG_TRACE, "=> ldap_str2rdn(%s,%u)\n%s", str, flags, "" );
789 switch ( LDAP_DN_FORMAT( flags ) ) {
790 case LDAP_DN_FORMAT_LDAP:
791 case LDAP_DN_FORMAT_LDAPV3:
792 case LDAP_DN_FORMAT_LDAPV2:
793 case LDAP_DN_FORMAT_DCE:
796 /* unsupported in str2dn */
797 case LDAP_DN_FORMAT_UFN:
798 case LDAP_DN_FORMAT_AD_CANONICAL:
799 return LDAP_PARAM_ERROR;
801 case LDAP_DN_FORMAT_LBER:
803 return LDAP_PARAM_ERROR;
806 if ( str[ 0 ] == '\0' ) {
811 for ( ; p[ 0 ] || state == GOTAVA; ) {
814 * The parser in principle advances one token a time,
815 * or toggles state if preferable.
820 * an AttributeType can be encoded as:
821 * - its string representation; in detail, implementations
822 * MUST recognize AttributeType string type names listed
823 * in section 2.3 of draft-ietf-ldapbis-dn-XX.txt, and
824 * MAY recognize other names.
825 * - its numeric OID (a dotted decimal string); in detail
826 * RFC 2253 asserts that ``Implementations MUST allow
827 * an oid in the attribute type to be prefixed by one
828 * of the character strings "oid." or "OID."''. As soon
829 * as draft-ietf-ldapbis-dn-XX.txt obsoletes RFC 2253
830 * I'm not sure whether this is required or not any
831 * longer; to be liberal, we still implement it.
834 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
835 if ( !LDAP_DN_ALLOW_ONE_SPACE( flags ) ) {
842 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
843 if ( !LDAP_DN_ALLOW_SPACES( flags ) ) {
848 /* whitespace is allowed (and trimmed) */
850 while ( p[ 0 ] && LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
855 /* error: we expected an AVA */
861 if ( LDAP_DN_OID_LEADCHAR( p[ 0 ] ) ) {
862 state = B4OIDATTRTYPE;
866 /* else must be alpha */
867 if ( !LDAP_DN_DESC_LEADCHAR( p[ 0 ] ) ) {
871 /* LDAPv2 "oid." prefix */
872 if ( LDAP_DN_LDAPV2( flags ) ) {
874 * to be overly pedantic, we only accept
877 if ( flags & LDAP_DN_PEDANTIC ) {
878 if ( !strncmp( p, "OID.", 4 )
879 || !strncmp( p, "oid.", 4 ) ) {
881 state = B4OIDATTRTYPE;
885 if ( !strncasecmp( p, "oid.", 4 ) ) {
887 state = B4OIDATTRTYPE;
893 state = B4STRINGATTRTYPE;
896 case B4OIDATTRTYPE: {
897 int err = LDAP_SUCCESS;
899 attrType.bv_val = ldap_int_parse_numericoid( &p, &err,
902 if ( err != LDAP_SUCCESS ) {
905 attrType.bv_len = p - attrType.bv_val;
907 attrTypeEncoding = LDAP_AVA_BINARY;
913 case B4STRINGATTRTYPE: {
914 const char *startPos, *endPos = NULL;
918 * the starting char has been found to be
919 * a LDAP_DN_DESC_LEADCHAR so we don't re-check it
920 * FIXME: DCE attr types seem to have a more
921 * restrictive syntax (no '-' ...)
923 for ( startPos = p++; p[ 0 ]; p++ ) {
924 if ( LDAP_DN_DESC_CHAR( p[ 0 ] ) ) {
928 if ( LDAP_DN_LANG_SEP( p[ 0 ] ) ) {
931 * RFC 2253 does not explicitly
932 * allow lang extensions to attribute
935 if ( flags & LDAP_DN_PEDANTIC ) {
940 * we trim ';' and following lang
941 * and so from attribute types
944 for ( ; LDAP_DN_ATTRDESC_CHAR( p[ 0 ] )
945 || LDAP_DN_LANG_SEP( p[ 0 ] ); p++ ) {
953 len = ( endPos ? endPos : p ) - startPos;
958 attrTypeEncoding = LDAP_AVA_STRING;
961 * here we need to decide whether to use it as is
962 * or turn it in OID form; as a consequence, we
963 * need to decide whether to binary encode the value
968 if ( flags & LDAP_DN_SKIP ) {
972 attrType.bv_val = (char *)startPos;
973 attrType.bv_len = len;
979 /* spaces may not be allowed */
980 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
981 if ( !LDAP_DN_ALLOW_SPACES( flags ) ) {
986 for ( p++; LDAP_DN_ASCII_SPACE( p[ 0 ] ); p++ ) {
991 /* need equal sign */
992 if ( !LDAP_DN_AVA_EQUALS( p[ 0 ] ) ) {
997 /* spaces may not be allowed */
998 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
999 if ( !LDAP_DN_ALLOW_SPACES( flags ) ) {
1004 for ( p++; LDAP_DN_ASCII_SPACE( p[ 0 ] ); p++ ) {
1010 * octothorpe means a BER encoded value will follow
1011 * FIXME: I don't think DCE will allow it
1013 if ( LDAP_DN_OCTOTHORPE( p[ 0 ] ) ) {
1015 attrValueEncoding = LDAP_AVA_BINARY;
1016 state = B4BINARYVALUE;
1020 /* STRING value expected */
1023 * if we're pedantic, an attribute type in OID form
1024 * SHOULD imply a BER encoded attribute value; we
1025 * should at least issue a warning
1027 if ( ( flags & LDAP_DN_PEDANTIC )
1028 && ( attrTypeEncoding == LDAP_AVA_BINARY ) ) {
1029 /* OID attrType SHOULD use binary encoding */
1033 attrValueEncoding = LDAP_AVA_STRING;
1036 * LDAPv2 allows the attribute value to be quoted;
1037 * also, IA5 values are expected, in principle
1039 if ( LDAP_DN_LDAPV2( flags ) || LDAP_DN_LDAP( flags ) ) {
1040 if ( LDAP_DN_QUOTES( p[ 0 ] ) ) {
1042 state = B4IA5VALUEQUOTED;
1046 if ( LDAP_DN_LDAPV2( flags ) ) {
1053 * here STRING means RFC 2253 string
1054 * FIXME: what about DCE strings?
1060 state = B4STRINGVALUE;
1065 if ( hexstr2binval( p, &attrValue, &p, flags ) ) {
1073 switch ( LDAP_DN_FORMAT( flags ) ) {
1074 case LDAP_DN_FORMAT_LDAP:
1075 case LDAP_DN_FORMAT_LDAPV3:
1076 if ( str2strval( p, &attrValue, &p, flags,
1077 &attrValueEncoding ) ) {
1082 case LDAP_DN_FORMAT_DCE:
1083 if ( DCE2strval( p, &attrValue, &p, flags ) ) {
1096 if ( IA52strval( p, &attrValue, &p, flags ) ) {
1103 case B4IA5VALUEQUOTED:
1105 /* lead quote already stripped */
1106 if ( quotedIA52strval( p, &attrValue,
1117 if ( !( flags & LDAP_DN_SKIP ) ) {
1121 * we accept empty values
1123 ava = ldapava_new( &attrType, &attrValue,
1124 attrValueEncoding );
1126 if ( ava == NULL ) {
1127 rc = LDAP_NO_MEMORY;
1130 tmpRDN[navas++] = ava;
1132 attrValue.bv_val = NULL;
1133 attrValue.bv_len = 0;
1135 assert(navas < TMP_SLOTS);
1139 * if we got an AVA separator ('+', or ',' for DCE )
1140 * we expect a new AVA for this RDN; otherwise
1141 * we add the RDN to the DN
1143 switch ( LDAP_DN_FORMAT( flags ) ) {
1144 case LDAP_DN_FORMAT_LDAP:
1145 case LDAP_DN_FORMAT_LDAPV3:
1146 case LDAP_DN_FORMAT_LDAPV2:
1147 if ( !LDAP_DN_AVA_SEP( p[ 0 ] ) ) {
1152 case LDAP_DN_FORMAT_DCE:
1153 if ( !LDAP_DN_AVA_SEP_DCE( p[ 0 ] ) ) {
1161 * the RDN is over, phew
1164 if ( !( flags & LDAP_DN_SKIP ) ) {
1165 newRDN = (LDAPRDN *)LDAP_MALLOC( sizeof(LDAPRDN)
1166 + sizeof(LDAPAVA *) * (navas+1) );
1167 if ( newRDN == NULL ) {
1168 rc = LDAP_NO_MEMORY;
1173 newRDN[0] = (LDAPAVA**)(newRDN+1);
1175 for (i=0; i<navas; i++)
1176 newRDN[0][i] = tmpRDN[i];
1177 newRDN[0][i] = NULL;
1185 /* they should have been used in an AVA */
1186 attrType.bv_val = NULL;
1187 attrValue.bv_val = NULL;
1202 /* They are set to NULL after they're used in an AVA */
1204 if ( attrValue.bv_val ) {
1205 free( attrValue.bv_val );
1208 for (navas-- ; navas>=0; navas-- )
1209 ldap_avafree( tmpRDN[navas] );
1213 Debug( LDAP_DEBUG_TRACE, "<= ldap_str2rdn(%*s)=%d\n",
1223 * reads in a UTF-8 string value, unescaping stuff:
1224 * '\' + LDAP_DN_NEEDESCAPE(c) -> 'c'
1225 * '\' + HEXPAIR(p) -> unhex(p)
1228 str2strval( const char *str, struct berval *val, const char **next, unsigned flags, unsigned *retFlags )
1230 const char *p, *startPos, *endPos = NULL;
1231 ber_len_t len, escapes, unescapes;
1239 for ( startPos = p = str, escapes = 0, unescapes = 0; p[ 0 ]; p++ ) {
1240 if ( LDAP_DN_ESCAPE( p[ 0 ] ) ) {
1242 if ( p[ 0 ] == '\0' ) {
1245 if ( ( p == startPos + 1 && LDAP_DN_NEEDESCAPE_LEAD( p[ 0 ] ) )
1246 || ( LDAP_DN_VALUE_END( p[ 1 ] ) && LDAP_DN_NEEDESCAPE_TRAIL( p[ 0 ] ) )
1247 || LDAP_DN_NEEDESCAPE( p[ 0 ] ) ) {
1252 if ( LDAP_DN_HEXPAIR( p ) ) {
1255 hexstr2bin( p, &c );
1258 if ( !LDAP_DN_ASCII_PRINTABLE( c ) ) {
1261 * we assume the string is UTF-8
1263 *retFlags = LDAP_AVA_NONPRINTABLE;
1270 if ( LDAP_DN_PEDANTIC & flags ) {
1274 * FIXME: we allow escaping
1275 * of chars that don't need
1276 * to and do not belong to
1277 * HEXDIGITS (we also allow
1278 * single hexdigit; maybe we
1283 } else if (!LDAP_DN_ASCII_PRINTABLE( p[ 0 ] ) ) {
1284 *retFlags = LDAP_AVA_NONPRINTABLE;
1286 } else if ( ( LDAP_DN_LDAP( flags ) && LDAP_DN_VALUE_END_V2( p[ 0 ] ) )
1287 || ( LDAP_DN_LDAPV3( flags ) && LDAP_DN_VALUE_END( p[ 0 ] ) ) ) {
1290 } else if ( LDAP_DN_NEEDESCAPE( p[ 0 ] ) ) {
1292 * FIXME: maybe we can add
1293 * escapes if not pedantic?
1300 * we do allow unescaped spaces at the end
1301 * of the value only in non-pedantic mode
1303 if ( p > startPos + 1 && LDAP_DN_ASCII_SPACE( p[ -1 ] ) &&
1304 !LDAP_DN_ESCAPE( p[ -2 ] ) ) {
1305 if ( flags & LDAP_DN_PEDANTIC ) {
1309 /* strip trailing (unescaped) spaces */
1310 for ( endPos = p - 1;
1311 endPos > startPos + 1 &&
1312 LDAP_DN_ASCII_SPACE( endPos[ -1 ] ) &&
1313 !LDAP_DN_ESCAPE( endPos[ -2 ] );
1320 if ( flags & LDAP_DN_SKIP ) {
1325 * FIXME: test memory?
1327 len = ( endPos ? endPos : p ) - startPos - escapes - unescapes;
1330 if ( escapes == 0 && unescapes == 0 ) {
1331 val->bv_val = LDAP_STRNDUP( startPos, len );
1336 val->bv_val = LDAP_MALLOC( len + 1 );
1337 for ( s = 0, d = 0; d < len; ) {
1338 if ( LDAP_DN_ESCAPE( startPos[ s ] ) ) {
1340 if ( ( s == 0 && LDAP_DN_NEEDESCAPE_LEAD( startPos[ s ] ) )
1341 || ( s == len - 1 && LDAP_DN_NEEDESCAPE_TRAIL( startPos[ s ] ) )
1342 || LDAP_DN_NEEDESCAPE( startPos[ s ] ) ) {
1343 val->bv_val[ d++ ] =
1346 } else if ( LDAP_DN_HEXPAIR( &startPos[ s ] ) ) {
1349 hexstr2bin( &startPos[ s ], &c );
1350 val->bv_val[ d++ ] = c;
1355 * we allow escaping of chars
1356 * that do not need to
1358 val->bv_val[ d++ ] =
1363 val->bv_val[ d++ ] = startPos[ s++ ];
1367 val->bv_val[ d ] = '\0';
1368 assert( strlen( val->bv_val ) == len );
1375 DCE2strval( const char *str, struct berval *val, const char **next, unsigned flags )
1377 const char *p, *startPos, *endPos = NULL;
1378 ber_len_t len, escapes;
1386 for ( startPos = p = str, escapes = 0; p[ 0 ]; p++ ) {
1387 if ( LDAP_DN_ESCAPE_DCE( p[ 0 ] ) ) {
1389 if ( LDAP_DN_NEEDESCAPE_DCE( p[ 0 ] ) ) {
1396 } else if ( LDAP_DN_VALUE_END_DCE( p[ 0 ] ) ) {
1401 * FIXME: can we accept anything else? I guess we need
1402 * to stop if a value is not legal
1407 * (unescaped) trailing spaces are trimmed must be silently ignored;
1410 if ( p > startPos + 1 && LDAP_DN_ASCII_SPACE( p[ -1 ] ) &&
1411 !LDAP_DN_ESCAPE( p[ -2 ] ) ) {
1412 if ( flags & LDAP_DN_PEDANTIC ) {
1416 /* strip trailing (unescaped) spaces */
1417 for ( endPos = p - 1;
1418 endPos > startPos + 1 &&
1419 LDAP_DN_ASCII_SPACE( endPos[ -1 ] ) &&
1420 !LDAP_DN_ESCAPE( endPos[ -2 ] );
1427 if ( flags & LDAP_DN_SKIP ) {
1431 len = ( endPos ? endPos : p ) - startPos - escapes;
1433 if ( escapes == 0 ){
1434 val->bv_val = LDAP_STRNDUP( startPos, len );
1439 val->bv_val = LDAP_MALLOC( len + 1 );
1440 for ( s = 0, d = 0; d < len; ) {
1442 * This point is reached only if escapes
1443 * are properly used, so all we need to
1446 if ( LDAP_DN_ESCAPE_DCE( startPos[ s ] ) ) {
1450 val->bv_val[ d++ ] = startPos[ s++ ];
1452 val->bv_val[ d ] = '\0';
1453 assert( strlen( val->bv_val ) == len );
1460 IA52strval( const char *str, struct berval *val, const char **next, unsigned flags )
1462 const char *p, *startPos, *endPos = NULL;
1463 ber_len_t len, escapes;
1475 for ( startPos = p = str, escapes = 0; p[ 0 ]; p++ ) {
1476 if ( LDAP_DN_ESCAPE( p[ 0 ] ) ) {
1478 if ( p[ 0 ] == '\0' ) {
1482 if ( !LDAP_DN_NEEDESCAPE( p[ 0 ] )
1483 && ( LDAP_DN_PEDANTIC & flags ) ) {
1488 } else if ( LDAP_DN_VALUE_END_V2( p[ 0 ] ) ) {
1493 * FIXME: can we accept anything else? I guess we need
1494 * to stop if a value is not legal
1498 /* strip trailing (unescaped) spaces */
1500 endPos > startPos + 1 &&
1501 LDAP_DN_ASCII_SPACE( endPos[ -1 ] ) &&
1502 !LDAP_DN_ESCAPE( endPos[ -2 ] );
1508 if ( flags & LDAP_DN_SKIP ) {
1512 len = ( endPos ? endPos : p ) - startPos - escapes;
1514 if ( escapes == 0 ) {
1515 val->bv_val = LDAP_STRNDUP( startPos, len );
1520 val->bv_val = LDAP_MALLOC( len + 1 );
1521 for ( s = 0, d = 0; d < len; ) {
1522 if ( LDAP_DN_ESCAPE( startPos[ s ] ) ) {
1525 val->bv_val[ d++ ] = startPos[ s++ ];
1527 val->bv_val[ d ] = '\0';
1528 assert( strlen( val->bv_val ) == len );
1535 quotedIA52strval( const char *str, struct berval *val, const char **next, unsigned flags )
1537 const char *p, *startPos, *endPos = NULL;
1539 unsigned escapes = 0;
1547 /* initial quote already eaten */
1548 for ( startPos = p = str; p[ 0 ]; p++ ) {
1550 * According to RFC 1779, the quoted value can
1551 * contain escaped as well as unescaped special values;
1552 * as a consequence we tolerate escaped values
1553 * (e.g. '"\,"' -> '\,') and escape unescaped specials
1554 * (e.g. '","' -> '\,').
1556 if ( LDAP_DN_ESCAPE( p[ 0 ] ) ) {
1557 if ( p[ 1 ] == '\0' ) {
1562 if ( !LDAP_DN_V2_PAIR( p[ 0 ] )
1563 && ( LDAP_DN_PEDANTIC & flags ) ) {
1565 * do we allow to escape normal chars?
1566 * LDAPv2 does not allow any mechanism
1567 * for escaping chars with '\' and hex
1574 } else if ( LDAP_DN_QUOTES( p[ 0 ] ) ) {
1576 /* eat closing quotes */
1582 * FIXME: can we accept anything else? I guess we need
1583 * to stop if a value is not legal
1587 if ( endPos == NULL ) {
1591 /* Strip trailing (unescaped) spaces */
1592 for ( ; p[ 0 ] && LDAP_DN_ASCII_SPACE( p[ 0 ] ); p++ ) {
1597 if ( flags & LDAP_DN_SKIP ) {
1601 len = endPos - startPos - escapes;
1604 if ( escapes == 0 ) {
1605 val->bv_val = LDAP_STRNDUP( startPos, len );
1610 val->bv_val = LDAP_MALLOC( len + 1 );
1613 for ( s = d = 0; d < len; ) {
1614 if ( LDAP_DN_ESCAPE( str[ s ] ) ) {
1617 val->bv_val[ d++ ] = str[ s++ ];
1619 val->bv_val[ d ] = '\0';
1620 assert( strlen( val->bv_val ) == len );
1627 hexstr2bin( const char *str, char *c )
1637 if ( LDAP_DN_ASCII_DIGIT( c1 ) ) {
1641 if ( LDAP_DN_ASCII_UCASE_HEXALPHA( c1 ) ) {
1644 assert( LDAP_DN_ASCII_LCASE_HEXALPHA( c1 ) );
1651 if ( LDAP_DN_ASCII_DIGIT( c2 ) ) {
1655 if ( LDAP_DN_ASCII_UCASE_HEXALPHA( c2 ) ) {
1656 *c += c2 - 'A' + 10;
1658 assert( LDAP_DN_ASCII_LCASE_HEXALPHA( c2 ) );
1659 *c += c2 - 'a' + 10;
1667 hexstr2binval( const char *str, struct berval *val, const char **next, unsigned flags )
1669 const char *p, *startPos, *endPos = NULL;
1679 for ( startPos = p = str; p[ 0 ]; p += 2 ) {
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 ] ) ) {
1701 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1702 if ( flags & LDAP_DN_PEDANTIC ) {
1707 for ( ; p[ 0 ]; p++ ) {
1708 switch ( LDAP_DN_FORMAT( flags ) ) {
1709 case LDAP_DN_FORMAT_LDAPV3:
1710 if ( LDAP_DN_VALUE_END( p[ 0 ] ) ) {
1715 case LDAP_DN_FORMAT_LDAP:
1716 case LDAP_DN_FORMAT_LDAPV2:
1717 if ( LDAP_DN_VALUE_END_V2( p[ 0 ] ) ) {
1722 case LDAP_DN_FORMAT_DCE:
1723 if ( LDAP_DN_VALUE_END_DCE( p[ 0 ] ) ) {
1732 if ( !LDAP_DN_HEXPAIR( p ) ) {
1740 if ( flags & LDAP_DN_SKIP ) {
1744 len = ( ( endPos ? endPos : p ) - startPos ) / 2;
1746 assert( 2 * len == (ber_len_t) (( endPos ? endPos : p ) - startPos ));
1749 val->bv_val = LDAP_MALLOC( len + 1 );
1750 if ( val->bv_val == NULL ) {
1751 return( LDAP_NO_MEMORY );
1754 for ( s = 0, d = 0; d < len; s += 2, d++ ) {
1757 hexstr2bin( &startPos[ s ], &c );
1759 val->bv_val[ d ] = c;
1762 val->bv_val[ d ] = '\0';
1768 * convert a byte in a hexadecimal pair
1771 byte2hexpair( const char *val, char *pair )
1773 static const char hexdig[] = "0123456789ABCDEF";
1779 * we assume the string has enough room for the hex encoding
1783 pair[ 0 ] = hexdig[ 0x0f & ( val[ 0 ] >> 4 ) ];
1784 pair[ 1 ] = hexdig[ 0x0f & val[ 0 ] ];
1790 * convert a binary value in hexadecimal pairs
1793 binval2hexstr( struct berval *val, char *str )
1800 if ( val->bv_len == 0 ) {
1805 * we assume the string has enough room for the hex encoding
1809 for ( s = 0, d = 0; s < val->bv_len; s++, d += 2 ) {
1810 byte2hexpair( &val->bv_val[ s ], &str[ d ] );
1817 * Length of the string representation, accounting for escaped hex
1821 strval2strlen( struct berval *val, unsigned flags, ber_len_t *len )
1823 ber_len_t l, cl = 1;
1825 int escaped_byte_len = LDAP_DN_IS_PRETTY( flags ) ? 1 : 3;
1826 #ifdef PRETTY_ESCAPE
1827 int escaped_ascii_len = LDAP_DN_IS_PRETTY( flags ) ? 2 : 3;
1828 #endif /* PRETTY_ESCAPE */
1834 if ( val->bv_len == 0 ) {
1838 for ( l = 0, p = val->bv_val; p[ 0 ]; p += cl ) {
1839 cl = LDAP_UTF8_CHARLEN( p );
1841 /* illegal utf-8 char! */
1844 } else if ( cl > 1 ) {
1847 for ( cnt = 1; cnt < cl; cnt++ ) {
1848 if ( ( p[ cnt ] & 0x80 ) == 0x00 ) {
1852 l += escaped_byte_len * cl;
1854 } else if ( LDAP_DN_NEEDESCAPE( p[ 0 ] )
1855 || ( p == val->bv_val && LDAP_DN_NEEDESCAPE_LEAD( p[ 0 ] ) )
1856 || ( !p[ 1 ] && LDAP_DN_NEEDESCAPE_TRAIL( p[ 0 ] ) ) ) {
1857 #ifdef PRETTY_ESCAPE
1858 if ( LDAP_DN_WILLESCAPE_HEX( flags, p[ 0 ] ) ) {
1861 * there might be some chars we want
1862 * to escape in form of a couple
1863 * of hexdigits for optimization purposes
1868 l += escaped_ascii_len;
1870 #else /* ! PRETTY_ESCAPE */
1872 #endif /* ! PRETTY_ESCAPE */
1885 * convert to string representation, escaping with hex the UTF-8 stuff;
1886 * assume the destination has enough room for escaping
1889 strval2str( struct berval *val, char *str, unsigned flags, ber_len_t *len )
1891 ber_len_t s, d, end;
1897 if ( val->bv_len == 0 ) {
1903 * we assume the string has enough room for the hex encoding
1906 for ( s = 0, d = 0, end = val->bv_len - 1; s < val->bv_len; ) {
1907 ber_len_t cl = LDAP_UTF8_CHARLEN( &val->bv_val[ s ] );
1910 * there might be some chars we want to escape in form
1911 * of a couple of hexdigits for optimization purposes
1913 if ( ( cl > 1 && !LDAP_DN_IS_PRETTY( flags ) )
1914 #ifdef PRETTY_ESCAPE
1915 || LDAP_DN_WILLESCAPE_HEX( flags, val->bv_val[ s ] )
1916 #else /* ! PRETTY_ESCAPE */
1917 || LDAP_DN_NEEDESCAPE( val->bv_val[ s ] )
1918 || ( d == 0 && LDAP_DN_NEEDESCAPE_LEAD( val->bv_val[ s ] ) )
1919 || ( s == end && LDAP_DN_NEEDESCAPE_TRAIL( val->bv_val[ s ] ) )
1921 #endif /* ! PRETTY_ESCAPE */
1925 byte2hexpair( &val->bv_val[ s ], &str[ d ] );
1930 } else if ( cl > 1 ) {
1932 str[ d++ ] = val->bv_val[ s++ ];
1936 #ifdef PRETTY_ESCAPE
1937 if ( LDAP_DN_NEEDESCAPE( val->bv_val[ s ] )
1938 || ( d == 0 && LDAP_DN_NEEDESCAPE_LEAD( val->bv_val[ s ] ) )
1939 || ( s == end && LDAP_DN_NEEDESCAPE_TRAIL( val->bv_val[ s ] ) ) ) {
1941 if ( !LDAP_DN_IS_PRETTY( flags ) ) {
1942 byte2hexpair( &val->bv_val[ s ], &str[ d ] );
1948 #endif /* PRETTY_ESCAPE */
1949 str[ d++ ] = val->bv_val[ s++ ];
1959 * Length of the IA5 string representation (no UTF-8 allowed)
1962 strval2IA5strlen( struct berval *val, unsigned flags, ber_len_t *len )
1971 if ( val->bv_len == 0 ) {
1975 if ( flags & LDAP_AVA_NONPRINTABLE ) {
1977 * Turn value into a binary encoded BER
1982 for ( l = 0, p = val->bv_val; p[ 0 ]; p++ ) {
1983 if ( LDAP_DN_NEEDESCAPE( p[ 0 ] )
1984 || ( p == val->bv_val && LDAP_DN_NEEDESCAPE_LEAD( p[ 0 ] ) )
1985 || ( !p[ 1 ] && LDAP_DN_NEEDESCAPE_TRAIL( p[ 0 ] ) ) ) {
2000 * convert to string representation (np UTF-8)
2001 * assume the destination has enough room for escaping
2004 strval2IA5str( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2006 ber_len_t s, d, end;
2012 if ( val->bv_len == 0 ) {
2017 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2019 * Turn value into a binary encoded BER
2026 * we assume the string has enough room for the hex encoding
2030 for ( s = 0, d = 0, end = val->bv_len - 1; s < val->bv_len; ) {
2031 if ( LDAP_DN_NEEDESCAPE( val->bv_val[ s ] )
2032 || ( s == 0 && LDAP_DN_NEEDESCAPE_LEAD( val->bv_val[ s ] ) )
2033 || ( s == end && LDAP_DN_NEEDESCAPE_TRAIL( val->bv_val[ s ] ) ) ) {
2036 str[ d++ ] = val->bv_val[ s++ ];
2046 * Length of the (supposedly) DCE string representation,
2047 * accounting for escaped hex of UTF-8 chars
2050 strval2DCEstrlen( struct berval *val, unsigned flags, ber_len_t *len )
2059 if ( val->bv_len == 0 ) {
2063 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2065 * FIXME: Turn the value into a binary encoded BER?
2070 for ( l = 0, p = val->bv_val; p[ 0 ]; p++ ) {
2071 if ( LDAP_DN_NEEDESCAPE_DCE( p[ 0 ] ) ) {
2086 * convert to (supposedly) DCE string representation,
2087 * escaping with hex the UTF-8 stuff;
2088 * assume the destination has enough room for escaping
2091 strval2DCEstr( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2099 if ( val->bv_len == 0 ) {
2104 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2106 * FIXME: Turn the value into a binary encoded BER?
2114 * we assume the string has enough room for the hex encoding
2118 for ( s = 0, d = 0; s < val->bv_len; ) {
2119 if ( LDAP_DN_NEEDESCAPE_DCE( val->bv_val[ s ] ) ) {
2122 str[ d++ ] = val->bv_val[ s++ ];
2132 * Length of the (supposedly) AD canonical string representation,
2133 * accounting for escaped hex of UTF-8 chars
2136 strval2ADstrlen( struct berval *val, unsigned flags, ber_len_t *len )
2145 if ( val->bv_len == 0 ) {
2149 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2151 * FIXME: Turn the value into a binary encoded BER?
2156 for ( l = 0, p = val->bv_val; p[ 0 ]; p++ ) {
2157 if ( LDAP_DN_NEEDESCAPE_AD( p[ 0 ] ) ) {
2172 * convert to (supposedly) AD string representation,
2173 * escaping with hex the UTF-8 stuff;
2174 * assume the destination has enough room for escaping
2177 strval2ADstr( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2185 if ( val->bv_len == 0 ) {
2190 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2192 * FIXME: Turn the value into a binary encoded BER?
2200 * we assume the string has enough room for the hex encoding
2204 for ( s = 0, d = 0; s < val->bv_len; ) {
2205 if ( LDAP_DN_NEEDESCAPE_AD( val->bv_val[ s ] ) ) {
2208 str[ d++ ] = val->bv_val[ s++ ];
2218 * If the DN is terminated by single-AVA RDNs with attribute type of "dc",
2219 * the first part of the AD representation of the DN is written in DNS
2220 * form, i.e. dot separated domain name components (as suggested
2221 * by Luke Howard, http://www.padl.com/~lukeh)
2224 dn2domain( LDAPDN *dn, struct berval *bv, int pos, int *iRDN )
2227 int domain = 0, first = 1;
2228 ber_len_t l = 1; /* we move the null also */
2231 /* we are guaranteed there's enough memory in str */
2237 assert( *iRDN >= 0 );
2239 str = bv->bv_val + pos;
2241 for ( i = *iRDN; i >= 0; i-- ) {
2245 assert( dn[ 0 ][ i ] );
2248 assert( rdn[ 0 ][ 0 ] );
2249 ava = rdn[ 0 ][ 0 ];
2251 if ( !LDAP_DN_IS_RDN_DC( rdn ) ) {
2259 AC_MEMCPY( str, ava->la_value.bv_val,
2260 ava->la_value.bv_len + 1);
2261 l += ava->la_value.bv_len;
2264 AC_MEMCPY( str + ava->la_value.bv_len + 1, bv->bv_val + pos, l);
2265 AC_MEMCPY( str, ava->la_value.bv_val,
2266 ava->la_value.bv_len );
2267 str[ ava->la_value.bv_len ] = '.';
2268 l += ava->la_value.bv_len + 1;
2273 bv->bv_len = pos + l - 1;
2279 rdn2strlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len,
2280 int ( *s2l )( struct berval *v, unsigned f, ber_len_t *l ) )
2287 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
2288 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
2290 /* len(type) + '=' + '+' | ',' */
2291 l += ava->la_attr.bv_len + 2;
2293 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2294 /* octothorpe + twice the length */
2295 l += 1 + 2 * ava->la_value.bv_len;
2299 unsigned f = flags | ava->la_flags;
2301 if ( ( *s2l )( &ava->la_value, f, &vl ) ) {
2314 rdn2str( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len,
2315 int ( *s2s ) ( struct berval *v, char * s, unsigned f, ber_len_t *l ) )
2320 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
2321 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
2323 AC_MEMCPY( &str[ l ], ava->la_attr.bv_val,
2324 ava->la_attr.bv_len );
2325 l += ava->la_attr.bv_len;
2329 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2331 if ( binval2hexstr( &ava->la_value, &str[ l ] ) ) {
2334 l += 2 * ava->la_value.bv_len;
2338 unsigned f = flags | ava->la_flags;
2340 if ( ( *s2s )( &ava->la_value, &str[ l ], f, &vl ) ) {
2345 str[ l++ ] = ( rdn[ 0 ][ iAVA + 1 ] ? '+' : ',' );
2354 rdn2DCEstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len )
2361 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
2362 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
2364 /* len(type) + '=' + ',' | '/' */
2365 l += ava->la_attr.bv_len + 2;
2367 switch ( ava->la_flags ) {
2368 case LDAP_AVA_BINARY:
2369 /* octothorpe + twice the length */
2370 l += 1 + 2 * ava->la_value.bv_len;
2373 case LDAP_AVA_STRING: {
2375 unsigned f = flags | ava->la_flags;
2377 if ( strval2DCEstrlen( &ava->la_value, f, &vl ) ) {
2395 rdn2DCEstr( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len, int first )
2400 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
2401 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
2406 str[ l++ ] = ( iAVA ? ',' : '/' );
2409 AC_MEMCPY( &str[ l ], ava->la_attr.bv_val,
2410 ava->la_attr.bv_len );
2411 l += ava->la_attr.bv_len;
2415 switch ( ava->la_flags ) {
2416 case LDAP_AVA_BINARY:
2418 if ( binval2hexstr( &ava->la_value, &str[ l ] ) ) {
2421 l += 2 * ava->la_value.bv_len;
2424 case LDAP_AVA_STRING: {
2426 unsigned f = flags | ava->la_flags;
2428 if ( strval2DCEstr( &ava->la_value, &str[ l ], f, &vl ) ) {
2446 rdn2UFNstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len )
2456 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
2457 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
2460 l += ( rdn[ 0 ][ iAVA + 1 ] ? 3 : 2 );
2462 /* FIXME: are binary values allowed in UFN? */
2463 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2464 /* octothorpe + twice the value */
2465 l += 1 + 2 * ava->la_value.bv_len;
2469 unsigned f = flags | ava->la_flags;
2471 if ( strval2strlen( &ava->la_value, f, &vl ) ) {
2484 rdn2UFNstr( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len )
2489 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
2490 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
2492 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2494 if ( binval2hexstr( &ava->la_value, &str[ l ] ) ) {
2497 l += 2 * ava->la_value.bv_len;
2501 unsigned f = flags | ava->la_flags;
2503 if ( strval2str( &ava->la_value, &str[ l ], f, &vl ) ) {
2509 if ( rdn[ 0 ][ iAVA + 1 ]) {
2510 AC_MEMCPY( &str[ l ], " + ", 3 );
2514 AC_MEMCPY( &str[ l ], ", ", 2 );
2525 rdn2ADstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len )
2535 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
2536 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
2541 /* FIXME: are binary values allowed in UFN? */
2542 switch ( ava->la_flags ) {
2543 case LDAP_AVA_BINARY:
2544 /* octothorpe + twice the value */
2545 l += 1 + 2 * ava->la_value.bv_len;
2548 case LDAP_AVA_STRING: {
2550 unsigned f = flags | ava->la_flags;
2552 if ( strval2ADstrlen( &ava->la_value, f, &vl ) ) {
2570 rdn2ADstr( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len, int first )
2575 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
2576 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
2581 str[ l++ ] = ( iAVA ? ',' : '/' );
2584 switch ( ava->la_flags ) {
2585 case LDAP_AVA_BINARY:
2587 if ( binval2hexstr( &ava->la_value, &str[ l ] ) ) {
2590 l += 2 * ava->la_value.bv_len;
2593 case LDAP_AVA_STRING: {
2595 unsigned f = flags | ava->la_flags;
2597 if ( strval2ADstr( &ava->la_value, &str[ l ], f, &vl ) ) {
2617 * Returns in str a string representation of rdn based on flags.
2618 * There is some duplication of code between this and ldap_dn2str;
2619 * this is wanted to reduce the allocation of temporary buffers.
2622 ldap_rdn2str( LDAPRDN *rdn, char **str, unsigned flags )
2629 if((flags & LDAP_DN_FORMAT_MASK) == LDAP_DN_FORMAT_LBER) {
2630 return LDAP_PARAM_ERROR;
2633 rc = ldap_rdn2bv( rdn, &bv, flags );
2639 ldap_rdn2bv( LDAPRDN *rdn, struct berval *bv, unsigned flags )
2649 if ( rdn == NULL ) {
2650 bv->bv_val = LDAP_STRDUP( "" );
2651 return( LDAP_SUCCESS );
2655 * This routine wastes "back" bytes at the end of the string
2658 switch ( LDAP_DN_FORMAT( flags ) ) {
2659 case LDAP_DN_FORMAT_LDAPV3:
2660 if ( rdn2strlen( rdn, flags, &l, strval2strlen ) ) {
2661 return LDAP_DECODING_ERROR;
2665 case LDAP_DN_FORMAT_LDAPV2:
2666 if ( rdn2strlen( rdn, flags, &l, strval2IA5strlen ) ) {
2667 return LDAP_DECODING_ERROR;
2671 case LDAP_DN_FORMAT_UFN:
2672 if ( rdn2UFNstrlen( rdn, flags, &l ) ) {
2673 return LDAP_DECODING_ERROR;
2677 case LDAP_DN_FORMAT_DCE:
2678 if ( rdn2DCEstrlen( rdn, flags, &l ) ) {
2679 return LDAP_DECODING_ERROR;
2683 case LDAP_DN_FORMAT_AD_CANONICAL:
2684 if ( rdn2ADstrlen( rdn, flags, &l ) ) {
2685 return LDAP_DECODING_ERROR;
2690 return( LDAP_PARAM_ERROR );
2693 bv->bv_val = LDAP_MALLOC( l + 1 );
2695 switch ( LDAP_DN_FORMAT( flags ) ) {
2696 case LDAP_DN_FORMAT_LDAPV3:
2697 rc = rdn2str( rdn, bv->bv_val, flags, &l, strval2str );
2701 case LDAP_DN_FORMAT_LDAPV2:
2702 rc = rdn2str( rdn, bv->bv_val, flags, &l, strval2IA5str );
2706 case LDAP_DN_FORMAT_UFN:
2707 rc = rdn2UFNstr( rdn, bv->bv_val, flags, &l );
2711 case LDAP_DN_FORMAT_DCE:
2712 rc = rdn2DCEstr( rdn, bv->bv_val, flags, &l, 1 );
2716 case LDAP_DN_FORMAT_AD_CANONICAL:
2717 rc = rdn2ADstr( rdn, bv->bv_val, flags, &l, 1 );
2722 /* need at least one of the previous */
2723 return LDAP_PARAM_ERROR;
2727 ldap_memfree( bv->bv_val );
2731 bv->bv_len = l - back;
2732 bv->bv_val[ bv->bv_len ] = '\0';
2734 return LDAP_SUCCESS;
2738 * Very bulk implementation; many optimizations can be performed
2739 * - a NULL dn results in an empty string ""
2742 * a) what do we do if a UTF-8 string must be converted in LDAPv2?
2743 * we must encode it in binary form ('#' + HEXPAIRs)
2744 * b) does DCE/AD support UTF-8?
2745 * no clue; don't think so.
2746 * c) what do we do when binary values must be converted in UTF/DCE/AD?
2747 * use binary encoded BER
2749 int ldap_dn2str( LDAPDN *dn, char **str, unsigned flags )
2756 if((flags & LDAP_DN_FORMAT_MASK) == LDAP_DN_FORMAT_LBER) {
2757 return LDAP_PARAM_ERROR;
2760 rc = ldap_dn2bv( dn, &bv, flags );
2765 int ldap_dn2bv( LDAPDN *dn, struct berval *bv, unsigned flags )
2768 int rc = LDAP_ENCODING_ERROR;
2771 /* stringifying helpers for LDAPv3/LDAPv2 */
2772 int ( *sv2l ) ( struct berval *v, unsigned f, ber_len_t *l );
2773 int ( *sv2s ) ( struct berval *v, char *s, unsigned f, ber_len_t *l );
2780 Debug( LDAP_DEBUG_TRACE, "=> ldap_dn2bv(%u)\n%s%s", flags, "", "" );
2783 * a null dn means an empty dn string
2784 * FIXME: better raise an error?
2787 bv->bv_val = LDAP_STRDUP( "" );
2788 return( LDAP_SUCCESS );
2791 switch ( LDAP_DN_FORMAT( flags ) ) {
2792 case LDAP_DN_FORMAT_LDAPV3:
2793 sv2l = strval2strlen;
2797 case LDAP_DN_FORMAT_LDAPV2:
2798 sv2l = strval2IA5strlen;
2799 sv2s = strval2IA5str;
2802 for ( iRDN = 0, len = 0; dn[ 0 ][ iRDN ]; iRDN++ ) {
2804 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
2806 if ( rdn2strlen( rdn, flags, &rdnl, sv2l ) ) {
2807 goto return_results;
2813 if ( ( bv->bv_val = LDAP_MALLOC( len + 1 ) ) == NULL ) {
2814 rc = LDAP_NO_MEMORY;
2818 for ( l = 0, iRDN = 0; dn[ 0 ][ iRDN ]; iRDN++ ) {
2820 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
2822 if ( rdn2str( rdn, &bv->bv_val[ l ], flags,
2824 LDAP_FREE( bv->bv_val );
2826 goto return_results;
2834 * trim the last ',' (the allocated memory
2835 * is one byte longer than required)
2837 bv->bv_len = len - 1;
2838 bv->bv_val[ bv->bv_len ] = '\0';
2843 case LDAP_DN_FORMAT_UFN: {
2846 * FIXME: quoting from RFC 1781:
2848 To take a distinguished name, and generate a name of this format with
2849 attribute types omitted, the following steps are followed.
2851 1. If the first attribute is of type CommonName, the type may be
2854 2. If the last attribute is of type Country, the type may be
2857 3. If the last attribute is of type Country, the last
2858 Organisation attribute may have the type omitted.
2860 4. All attributes of type OrganisationalUnit may have the type
2861 omitted, unless they are after an Organisation attribute or
2862 the first attribute is of type OrganisationalUnit.
2864 * this should be the pedantic implementation.
2866 * Here the standard implementation reflects
2867 * the one historically provided by OpenLDAP
2868 * (and UMIch, I presume), with the variant
2869 * of spaces and plusses (' + ') separating
2872 * A non-standard but nice implementation could
2873 * be to turn the final "dc" attributes into a
2874 * dot-separated domain.
2876 * Other improvements could involve the use of
2877 * friendly country names and so.
2880 int leftmost_dc = -1;
2882 #endif /* DC_IN_UFN */
2884 for ( iRDN = 0, len = 0; dn[ 0 ][ iRDN ]; iRDN++ ) {
2886 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
2888 if ( rdn2UFNstrlen( rdn, flags, &rdnl ) ) {
2889 goto return_results;
2894 if ( LDAP_DN_IS_RDN_DC( rdn ) ) {
2895 if ( leftmost_dc == -1 ) {
2901 #endif /* DC_IN_UFN */
2904 if ( ( bv->bv_val = LDAP_MALLOC( len + 1 ) ) == NULL ) {
2905 rc = LDAP_NO_MEMORY;
2910 if ( leftmost_dc == -1 ) {
2911 #endif /* DC_IN_UFN */
2912 for ( l = 0, iRDN = 0; dn[ 0 ][ iRDN ]; iRDN++ ) {
2914 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
2916 if ( rdn2UFNstr( rdn, &bv->bv_val[ l ],
2918 LDAP_FREE( bv->bv_val );
2920 goto return_results;
2926 * trim the last ', ' (the allocated memory
2927 * is two bytes longer than required)
2929 bv->bv_len = len - 2;
2930 bv->bv_val[ bv->bv_len ] = '\0';
2933 last_iRDN = iRDN - 1;
2935 for ( l = 0, iRDN = 0; iRDN < leftmost_dc; iRDN++ ) {
2937 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
2939 if ( rdn2UFNstr( rdn, &bv->bv_val[ l ],
2941 LDAP_FREE( bv->bv_val );
2943 goto return_results;
2948 if ( !dn2domain( dn, bv, l, &last_iRDN ) ) {
2949 LDAP_FREE( bv->bv_val );
2951 goto return_results;
2954 /* the string is correctly terminated by dn2domain */
2956 #endif /* DC_IN_UFN */
2962 case LDAP_DN_FORMAT_DCE:
2964 for ( iRDN = 0, len = 0; dn[ 0 ][ iRDN ]; iRDN++ ) {
2966 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
2968 if ( rdn2DCEstrlen( rdn, flags, &rdnl ) ) {
2969 goto return_results;
2975 if ( ( bv->bv_val = LDAP_MALLOC( len + 1 ) ) == NULL ) {
2976 rc = LDAP_NO_MEMORY;
2980 for ( l = 0; iRDN--; ) {
2982 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
2984 if ( rdn2DCEstr( rdn, &bv->bv_val[ l ], flags,
2986 LDAP_FREE( bv->bv_val );
2988 goto return_results;
2996 bv->bv_val[ bv->bv_len ] = '\0';
3001 case LDAP_DN_FORMAT_AD_CANONICAL: {
3004 * Sort of UFN for DCE DNs: a slash ('/') separated
3005 * global->local DN with no types; strictly speaking,
3006 * the naming context should be a domain, which is
3007 * written in DNS-style, e.g. dot-deparated.
3011 * "givenName=Bill+sn=Gates,ou=People,dc=microsoft,dc=com"
3015 * "microsoft.com/People/Bill,Gates"
3017 for ( iRDN = 0, len = -1; dn[ 0 ][ iRDN ]; iRDN++ ) {
3019 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
3021 if ( rdn2ADstrlen( rdn, flags, &rdnl ) ) {
3022 goto return_results;
3028 if ( ( bv->bv_val = LDAP_MALLOC( len + 1 ) ) == NULL ) {
3029 rc = LDAP_NO_MEMORY;
3034 if ( iRDN && dn2domain( dn, bv, 0, &iRDN ) ) {
3035 for ( l = bv->bv_len; iRDN >= 0 ; iRDN-- ) {
3037 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
3039 if ( rdn2ADstr( rdn, &bv->bv_val[ l ],
3040 flags, &rdnl, 0 ) ) {
3041 LDAP_FREE( bv->bv_val );
3043 goto return_results;
3052 * Strictly speaking, AD canonical requires
3053 * a DN to be in the form "..., dc=smtg",
3054 * i.e. terminated by a domain component
3056 if ( flags & LDAP_DN_PEDANTIC ) {
3057 LDAP_FREE( bv->bv_val );
3059 rc = LDAP_ENCODING_ERROR;
3063 for ( l = 0; iRDN >= 0 ; iRDN-- ) {
3065 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
3067 if ( rdn2ADstr( rdn, &bv->bv_val[ l ],
3068 flags, &rdnl, first ) ) {
3069 LDAP_FREE( bv->bv_val );
3071 goto return_results;
3081 bv->bv_val[ bv->bv_len ] = '\0';
3088 return LDAP_PARAM_ERROR;
3091 Debug( LDAP_DEBUG_TRACE, "<= ldap_dn2bv(%s,%u)=%d\n",
3092 bv->bv_val, flags, rc );