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>
24 /* extension to UFN that turns trailing "dc=value" rdns in DNS style,
25 * e.g. "ou=People,dc=openldap,dc=org" => "People, openldap.org" */
27 /* #define PRETTY_ESCAPE */
29 /* from libraries/libldap/schema.c */
30 extern char * parse_numericoid(const char **sp, int *code, const int flags);
32 /* parsing/printing routines */
33 static int str2strval( const char *str, struct berval *val,
34 const char **next, unsigned flags, unsigned *retFlags );
35 static int DCE2strval( const char *str, struct berval *val,
36 const char **next, unsigned flags );
37 static int IA52strval( const char *str, struct berval *val,
38 const char **next, unsigned flags );
39 static int quotedIA52strval( const char *str, struct berval *val,
40 const char **next, unsigned flags );
41 static int hexstr2binval( const char *str, struct berval *val,
42 const char **next, unsigned flags );
43 static int hexstr2bin( const char *str, char *c );
44 static int byte2hexpair( const char *val, char *pair );
45 static int binval2hexstr( struct berval *val, char *str );
46 static int strval2strlen( struct berval *val, unsigned flags,
48 static int strval2str( struct berval *val, char *str, unsigned flags,
50 static int strval2IA5strlen( struct berval *val, unsigned flags,
52 static int strval2IA5str( struct berval *val, char *str, unsigned flags,
54 static int strval2DCEstrlen( struct berval *val, unsigned flags,
56 static int strval2DCEstr( struct berval *val, char *str, unsigned flags,
58 static int strval2ADstrlen( struct berval *val, unsigned flags,
60 static int strval2ADstr( struct berval *val, char *str, unsigned flags,
62 static int dn2domain( LDAPDN *dn, struct berval *bv, int pos, int *iRDN );
65 static LDAPAVA * ldapava_new(
66 const struct berval *attr, const struct berval *val, unsigned flags );
67 static LDAPRDN * ldapava_append_to_rdn( LDAPRDN *rdn, LDAPAVA *ava );
68 static LDAPRDN * ldapava_insert_into_rdn(
69 LDAPRDN *rdn, LDAPAVA *ava, unsigned where );
70 static LDAPDN * ldapava_append_to_dn( LDAPDN *dn, LDAPRDN *rdn );
71 static LDAPDN * ldapava_insert_into_dn(
72 LDAPDN *dn, LDAPRDN *rdn, unsigned where );
74 /* Higher level helpers */
75 static int rdn2strlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len,
76 int ( *s2l )( struct berval *, unsigned, ber_len_t * ) );
77 static int rdn2str( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len,
78 int ( *s2s )( struct berval *, char *, unsigned, ber_len_t * ));
79 static int rdn2UFNstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len );
80 static int rdn2UFNstr( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len );
81 static int rdn2DCEstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len );
82 static int rdn2DCEstr( LDAPRDN *rdn, char *str, unsigned flag, ber_len_t *len, int first );
83 static int rdn2ADstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len );
84 static int rdn2ADstr( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len, int first );
87 * RFC 1823 ldap_get_dn
90 ldap_get_dn( LDAP *ld, LDAPMessage *entry )
95 Debug( LDAP_DEBUG_TRACE, "ldap_get_dn\n", 0, 0, 0 );
97 if ( entry == NULL ) {
98 ld->ld_errno = LDAP_PARAM_ERROR;
102 tmp = *entry->lm_ber; /* struct copy */
103 if ( ber_scanf( &tmp, "{a" /*}*/, &dn ) == LBER_ERROR ) {
104 ld->ld_errno = LDAP_DECODING_ERROR;
112 * RFC 1823 ldap_dn2ufn
115 ldap_dn2ufn( LDAP_CONST char *dn )
119 Debug( LDAP_DEBUG_TRACE, "ldap_dn2ufn\n", 0, 0, 0 );
121 ( void )ldap_dn_normalize( dn, LDAP_DN_FORMAT_LDAP,
122 &out, LDAP_DN_FORMAT_UFN );
128 * RFC 1823 ldap_explode_dn
131 ldap_explode_dn( LDAP_CONST char *dn, int notypes )
134 char **values = NULL;
136 unsigned flag = notypes ? LDAP_DN_FORMAT_UFN : LDAP_DN_FORMAT_LDAPV3;
138 Debug( LDAP_DEBUG_TRACE, "ldap_explode_dn\n", 0, 0, 0 );
140 if ( ldap_str2dn( dn, &tmpDN, LDAP_DN_FORMAT_LDAP )
145 if( tmpDN == NULL ) {
146 values = LDAP_MALLOC( sizeof( char * ) );
147 if( values == NULL ) return NULL;
153 for ( iRDN = 0; tmpDN[ iRDN ]; iRDN++ );
155 values = LDAP_MALLOC( sizeof( char * ) * ( 1 + iRDN ) );
156 if ( values == NULL ) {
157 ldap_dnfree( tmpDN );
161 for ( iRDN = 0; tmpDN[ iRDN ]; iRDN++ ) {
162 ldap_rdn2str( tmpDN[ iRDN ][ 0 ], &values[ iRDN ], flag );
164 ldap_dnfree( tmpDN );
165 values[ iRDN ] = NULL;
171 ldap_explode_rdn( LDAP_CONST char *rdn, int notypes )
174 char **values = NULL;
178 Debug( LDAP_DEBUG_TRACE, "ldap_explode_rdn\n", 0, 0, 0 );
181 * we only parse the first rdn
182 * FIXME: we prefer efficiency over checking if the _ENTIRE_
185 if ( ldap_str2rdn( rdn, &tmpRDN, &p, LDAP_DN_FORMAT_LDAP )
190 for ( iAVA = 0; tmpRDN[ iAVA ]; iAVA++ ) ;
191 values = LDAP_MALLOC( sizeof( char * ) * ( 1 + iAVA ) );
192 if ( values == NULL ) {
193 ldap_rdnfree( tmpRDN );
197 for ( iAVA = 0; tmpRDN[ iAVA ]; iAVA++ ) {
198 ber_len_t l = 0, vl, al = 0;
200 LDAPAVA *ava = tmpRDN[ iAVA ][ 0 ];
202 if ( ava->la_flags == LDAP_AVA_BINARY ) {
203 vl = 1 + 2 * ava->la_value.bv_len;
206 if ( strval2strlen( &ava->la_value,
207 ava->la_flags, &vl ) ) {
213 al = ava->la_attr.bv_len;
214 l = vl + ava->la_attr.bv_len + 1;
216 str = LDAP_MALLOC( l + 1 );
217 AC_MEMCPY( str, ava->la_attr.bv_val,
218 ava->la_attr.bv_len );
223 str = LDAP_MALLOC( l + 1 );
226 if ( ava->la_flags == LDAP_AVA_BINARY ) {
228 if ( binval2hexstr( &ava->la_value, &str[ al ] ) ) {
233 if ( strval2str( &ava->la_value, &str[ al ],
234 ava->la_flags, &vl ) ) {
240 values[ iAVA ] = str;
242 values[ iAVA ] = NULL;
244 ldap_rdnfree( tmpRDN );
249 LBER_VFREE( values );
250 ldap_rdnfree( tmpRDN );
255 ldap_dn2dcedn( LDAP_CONST char *dn )
259 Debug( LDAP_DEBUG_TRACE, "ldap_dn2dcedn\n", 0, 0, 0 );
261 ( void )ldap_dn_normalize( dn, LDAP_DN_FORMAT_LDAP,
262 &out, LDAP_DN_FORMAT_DCE );
268 ldap_dcedn2dn( LDAP_CONST char *dce )
272 Debug( LDAP_DEBUG_TRACE, "ldap_dcedn2dn\n", 0, 0, 0 );
274 ( void )ldap_dn_normalize( dce, LDAP_DN_FORMAT_DCE, &out, LDAP_DN_FORMAT_LDAPV3 );
280 ldap_dn2ad_canonical( LDAP_CONST char *dn )
284 Debug( LDAP_DEBUG_TRACE, "ldap_dn2ad_canonical\n", 0, 0, 0 );
286 ( void )ldap_dn_normalize( dn, LDAP_DN_FORMAT_LDAP,
287 &out, LDAP_DN_FORMAT_AD_CANONICAL );
293 * function that changes the string representation of dnin
294 * from ( fin & LDAP_DN_FORMAT_MASK ) to ( fout & LDAP_DN_FORMAT_MASK )
297 * LDAP_DN_FORMAT_LDAP (rfc 2253 and ldapbis liberal,
298 * plus some rfc 1779)
299 * LDAP_DN_FORMAT_LDAPV3 (rfc 2253 and ldapbis)
300 * LDAP_DN_FORMAT_LDAPV2 (rfc 1779)
301 * LDAP_DN_FORMAT_DCE (?)
303 * fout can be any of the above except
304 * LDAP_DN_FORMAT_LDAP
306 * LDAP_DN_FORMAT_UFN (rfc 1781, partial and with extensions)
307 * LDAP_DN_FORMAT_AD_CANONICAL (?)
310 ldap_dn_normalize( const char *dnin, unsigned fin, char **dnout, unsigned fout )
313 LDAPDN *tmpDN = NULL;
315 Debug( LDAP_DEBUG_TRACE, "ldap_dn_normalize\n", 0, 0, 0 );
321 if ( dnin == NULL ) {
322 return( LDAP_SUCCESS );
325 rc = ldap_str2dn( dnin , &tmpDN, fin );
326 if ( rc != LDAP_SUCCESS ) {
330 rc = ldap_dn2str( tmpDN, dnout, fout );
332 ldap_dnfree( tmpDN );
340 /* #define B4ATTRTYPE 0x0001 */
341 #define B4OIDATTRTYPE 0x0002
342 #define B4STRINGATTRTYPE 0x0003
344 #define B4AVAEQUALS 0x0100
345 #define B4AVASEP 0x0200
346 #define B4RDNSEP 0x0300
347 #define GOTAVA 0x0400
349 #define B4ATTRVALUE 0x0010
350 #define B4STRINGVALUE 0x0020
351 #define B4IA5VALUEQUOTED 0x0030
352 #define B4IA5VALUE 0x0040
353 #define B4BINARYVALUE 0x0050
356 * Helpers (mostly from slap.h)
357 * c is assumed to Unicode in an ASCII compatible format (UTF-8)
358 * Macros assume "C" Locale (ASCII)
360 #define LDAP_DN_ASCII_SPACE(c) \
361 ( (c) == ' ' || (c) == '\t' || (c) == '\n' || (c) == '\r' )
362 #define LDAP_DN_ASCII_LOWER(c) ( (c) >= 'a' && (c) <= 'z' )
363 #define LDAP_DN_ASCII_UPPER(c) ( (c) >= 'A' && (c) <= 'Z' )
364 #define LDAP_DN_ASCII_ALPHA(c) \
365 ( LDAP_DN_ASCII_LOWER(c) || LDAP_DN_ASCII_UPPER(c) )
366 #define LDAP_DN_ASCII_DIGIT(c) ( (c) >= '0' && (c) <= '9' )
367 #define LDAP_DN_ASCII_LCASE_HEXALPHA(c) ( (c) >= 'a' && (c) <= 'f' )
368 #define LDAP_DN_ASCII_UCASE_HEXALPHA(c) ( (c) >= 'A' && (c) <= 'F' )
369 #define LDAP_DN_ASCII_HEXDIGIT(c) \
370 ( LDAP_DN_ASCII_DIGIT(c) \
371 || LDAP_DN_ASCII_LCASE_HEXALPHA(c) \
372 || LDAP_DN_ASCII_UCASE_HEXALPHA(c) )
373 #define LDAP_DN_ASCII_ALNUM(c) \
374 ( LDAP_DN_ASCII_ALPHA(c) || LDAP_DN_ASCII_DIGIT(c) )
375 #define LDAP_DN_ASCII_PRINTABLE(c) ( (c) >= ' ' && (c) <= '~' )
378 #define LDAP_DN_OID_LEADCHAR(c) ( LDAP_DN_ASCII_DIGIT(c) )
379 #define LDAP_DN_DESC_LEADCHAR(c) ( LDAP_DN_ASCII_ALPHA(c) )
380 #define LDAP_DN_DESC_CHAR(c) ( LDAP_DN_ASCII_ALNUM(c) || (c) == '-' )
381 #define LDAP_DN_LANG_SEP(c) ( (c) == ';' )
382 #define LDAP_DN_ATTRDESC_CHAR(c) \
383 ( LDAP_DN_DESC_CHAR(c) || LDAP_DN_LANG_SEP(c) )
385 /* special symbols */
386 #define LDAP_DN_AVA_EQUALS(c) ( (c) == '=' )
387 #define LDAP_DN_AVA_SEP(c) ( (c) == '+' )
388 #define LDAP_DN_RDN_SEP(c) ( (c) == ',' )
389 #define LDAP_DN_RDN_SEP_V2(c) ( LDAP_DN_RDN_SEP(c) || (c) == ';' )
390 #define LDAP_DN_OCTOTHORPE(c) ( (c) == '#' )
391 #define LDAP_DN_QUOTES(c) ( (c) == '\"' )
392 #define LDAP_DN_ESCAPE(c) ( (c) == '\\' )
393 #define LDAP_DN_VALUE_END(c) \
394 ( LDAP_DN_RDN_SEP(c) || LDAP_DN_AVA_SEP(c) )
395 #define LDAP_DN_NE(c) \
396 ( LDAP_DN_RDN_SEP_V2(c) || LDAP_DN_AVA_SEP(c) \
397 || LDAP_DN_QUOTES(c) || (c) == '<' || (c) == '>' )
398 #define LDAP_DN_NEEDESCAPE(c) \
399 ( LDAP_DN_ESCAPE(c) || LDAP_DN_NE(c) )
400 #define LDAP_DN_NEEDESCAPE_LEAD(c) \
401 ( LDAP_DN_ASCII_SPACE(c) || LDAP_DN_OCTOTHORPE(c) || LDAP_DN_NE(c) )
402 #define LDAP_DN_NEEDESCAPE_TRAIL(c) \
403 ( LDAP_DN_ASCII_SPACE(c) || LDAP_DN_NEEDESCAPE(c) )
404 #define LDAP_DN_WILLESCAPE_CHAR(c) \
405 ( LDAP_DN_RDN_SEP(c) || LDAP_DN_AVA_SEP(c) || LDAP_DN_ESCAPE(c) )
406 #define LDAP_DN_IS_PRETTY(f) ( (f) & LDAP_DN_PRETTY )
407 #define LDAP_DN_WILLESCAPE_HEX(f, c) \
408 ( ( !LDAP_DN_IS_PRETTY( f ) ) && LDAP_DN_WILLESCAPE_CHAR(c) )
411 #define LDAP_DN_VALUE_END_V2(c) \
412 ( LDAP_DN_RDN_SEP_V2(c) || LDAP_DN_AVA_SEP(c) )
414 #define LDAP_DN_V2_SPECIAL(c) \
415 ( LDAP_DN_RDN_SEP_V2(c) || LDAP_DN_AVA_EQUALS(c) \
416 || LDAP_DN_AVA_SEP(c) || (c) == '<' || (c) == '>' \
417 || LDAP_DN_OCTOTHORPE(c) )
418 #define LDAP_DN_V2_PAIR(c) \
419 ( LDAP_DN_V2_SPECIAL(c) || LDAP_DN_ESCAPE(c) || LDAP_DN_QUOTES(c) )
422 * DCE (mostly from Luke Howard and IBM implementation for AIX)
424 * From: "Application Development Guide - Directory Services" (FIXME: add link?)
425 * Here escapes and valid chars for GDS are considered; as soon as more
426 * specific info is found, the macros will be updated.
428 * Chars: 'a'-'z', 'A'-'Z', '0'-'9',
429 * '.', ':', ',', ''', '+', '-', '=', '(', ')', '?', '/', ' '.
431 * Metachars: '/', ',', '=', '\'.
433 * the '\' is used to escape other metachars.
439 * Attribute types must start with alphabetic chars and can contain
440 * alphabetic chars and digits (FIXME: no '-'?). OIDs are allowed.
442 #define LDAP_DN_RDN_SEP_DCE(c) ( (c) == '/' )
443 #define LDAP_DN_AVA_SEP_DCE(c) ( (c) == ',' )
444 #define LDAP_DN_ESCAPE_DCE(c) ( LDAP_DN_ESCAPE(c) )
445 #define LDAP_DN_VALUE_END_DCE(c) \
446 ( LDAP_DN_RDN_SEP_DCE(c) || LDAP_DN_AVA_SEP_DCE(c) )
447 #define LDAP_DN_NEEDESCAPE_DCE(c) \
448 ( LDAP_DN_VALUE_END_DCE(c) || LDAP_DN_AVA_EQUALS(c) )
451 #define LDAP_DN_RDN_SEP_AD(c) ( (c) == '/' )
452 #define LDAP_DN_ESCAPE_AD(c) ( LDAP_DN_ESCAPE(c) )
453 #define LDAP_DN_AVA_SEP_AD(c) ( (c) == ',' ) /* assume same as DCE */
454 #define LDAP_DN_VALUE_END_AD(c) \
455 ( LDAP_DN_RDN_SEP_AD(c) || LDAP_DN_AVA_SEP_AD(c) )
456 #define LDAP_DN_NEEDESCAPE_AD(c) \
457 ( LDAP_DN_VALUE_END_AD(c) || LDAP_DN_AVA_EQUALS(c) )
460 #define LDAP_DN_HEXPAIR(s) \
461 ( LDAP_DN_ASCII_HEXDIGIT((s)[0]) && LDAP_DN_ASCII_HEXDIGIT((s)[1]) )
462 /* better look at the AttributeDescription? */
464 /* FIXME: no composite rdn or non-"dc" types, right?
465 * (what about "dc" in OID form?) */
466 /* FIXME: we do not allow binary values in domain, right? */
467 /* NOTE: use this macro only when ABSOLUTELY SURE rdn IS VALID! */
468 /* NOTE: don't use strcasecmp() as it is locale specific! */
469 #define LDAP_DC_ATTR "dc"
470 #define LDAP_DC_ATTRU "DC"
471 #define LDAP_DN_IS_RDN_DC( r ) \
472 ( (r) && (r)[0][0] && !(r)[1] \
473 && ((r)[0][0]->la_flags == LDAP_AVA_STRING) \
474 && ((r)[0][0]->la_attr.bv_len == 2) \
475 && (((r)[0][0]->la_attr.bv_val[0] == LDAP_DC_ATTR[0]) \
476 || ((r)[0][0]->la_attr.bv_val[0] == LDAP_DC_ATTRU[0])) \
477 && (((r)[0][0]->la_attr.bv_val[1] == LDAP_DC_ATTR[1]) \
478 || ((r)[0][0]->la_attr.bv_val[1] == LDAP_DC_ATTRU[1])))
480 /* Composite rules */
481 #define LDAP_DN_ALLOW_ONE_SPACE(f) \
482 ( LDAP_DN_LDAPV2(f) \
483 || !( (f) & LDAP_DN_P_NOSPACEAFTERRDN ) )
484 #define LDAP_DN_ALLOW_SPACES(f) \
485 ( LDAP_DN_LDAPV2(f) \
486 || !( (f) & ( LDAP_DN_P_NOLEADTRAILSPACES | LDAP_DN_P_NOSPACEAFTERRDN ) ) )
487 #define LDAP_DN_LDAP(f) \
488 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_LDAP )
489 #define LDAP_DN_LDAPV3(f) \
490 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_LDAPV3 )
491 #define LDAP_DN_LDAPV2(f) \
492 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_LDAPV2 )
493 #define LDAP_DN_DCE(f) \
494 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_DCE )
495 #define LDAP_DN_UFN(f) \
496 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_UFN )
497 #define LDAP_DN_ADC(f) \
498 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_AD_CANONICAL )
499 #define LDAP_DN_FORMAT(f) ( (f) & LDAP_DN_FORMAT_MASK )
502 * LDAPAVA helpers (will become part of the API for operations
503 * on structural representations of DNs).
506 ldapava_new( const struct berval *attr, const struct berval *val,
514 ava = LDAP_MALLOC( sizeof( LDAPAVA ) );
516 /* should we test it? */
521 ava->la_attr = *attr;
522 ava->la_value = *val;
523 ava->la_flags = flags;
525 ava->la_private = NULL;
531 ldap_avafree( LDAPAVA *ava )
536 /* ava's private must be freed by caller
537 * (at present let's skip this check because la_private
538 * basically holds static data) */
539 assert( ava->la_private == NULL );
542 free( ava->la_attr.bv_val );
543 free( ava->la_value.bv_val );
549 ldapava_append_to_rdn( LDAPRDN *rdn, LDAPAVA *ava )
557 for ( i = 0U; rdn[ i ]; i++ ) {
561 newRDN = LDAP_REALLOC( rdn, ( i + 2 ) * sizeof( LDAPAVA ** ) );
562 newRDN[ i ] = LDAP_MALLOC( sizeof( LDAPAVA * ) );
563 newRDN[ i ][ 0 ] = ava;
564 newRDN[ i + 1 ] = NULL;
570 ldapava_insert_into_rdn( LDAPRDN *rdn, LDAPAVA *ava, unsigned where )
578 for ( i = 0U; rdn[ i ]; i++ ) {
584 /* assume "at end", which corresponds to
585 * ldapava_append_to_rdn */
588 newRDN = LDAP_REALLOC( rdn, ( i + 2 ) * sizeof( LDAPAVA ** ) );
590 /* data after insert point */
591 AC_MEMCPY( &newRDN[ where + 1 ], &newRDN[ where ],
592 ( i - where ) * sizeof( LDAPRDN * ) );
594 newRDN[ where ] = LDAP_MALLOC( sizeof( LDAPAVA * ) );
595 newRDN[ where ][ 0 ] = ava;
596 newRDN[ i + 1 ] = NULL;
602 ldap_rdnfree( LDAPRDN *rdn )
610 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
611 assert( rdn[ iAVA ][ 0 ] );
613 ldap_avafree( rdn[ iAVA ][ 0 ] );
620 ldapava_append_to_dn( LDAPDN *dn, LDAPRDN *rdn )
628 for ( i = 0U; dn[ i ]; i++ ) {
632 newDN = LDAP_REALLOC( dn, ( i + 2 ) * sizeof( LDAPRDN ** ) );
633 newDN[ i ] = LDAP_MALLOC( sizeof( LDAPRDN * ) );
634 newDN[ i ][ 0 ] = rdn;
635 newDN[ i + 1 ] = NULL;
641 ldapava_insert_into_dn( LDAPDN *dn, LDAPRDN *rdn, unsigned where )
649 for ( i = 0U; dn[ i ]; i++ ) {
655 /* assume "at end", which corresponds to
656 * ldapava_append_to_dn */
659 newDN = LDAP_REALLOC( dn, ( i + 2 ) * sizeof( LDAPRDN ** ) );
661 /* data after insert point */
662 AC_MEMCPY( &newDN[ where + 1 ], &newDN[ where ],
663 ( i - where ) * sizeof( LDAPDN * ) );
665 newDN[ where ] = LDAP_MALLOC( sizeof( LDAPRDN * ) );
666 newDN[ where ][ 0 ] = rdn;
667 newDN[ i + 1 ] = NULL;
673 ldap_dnfree( LDAPDN *dn )
681 for ( iRDN = 0; dn[ iRDN ]; iRDN++ ) {
682 assert( dn[ iRDN ][ 0 ] );
684 ldap_rdnfree( dn[ iRDN ][ 0 ] );
691 * Converts a string representation of a DN (in LDAPv3, LDAPv2 or DCE)
692 * into a structural representation of the DN, by separating attribute
693 * types and values encoded in the more appropriate form, which is
694 * string or OID for attribute types and binary form of the BER encoded
695 * value or Unicode string. Formats different from LDAPv3 are parsed
696 * according to their own rules and turned into the more appropriate
697 * form according to LDAPv3.
699 * NOTE: I realize the code is getting spaghettish; it is rather
700 * experimental and will hopefully turn into something more simple
701 * and readable as soon as it works as expected.
705 ldap_str2dn( const char *str, LDAPDN **dn, unsigned flags )
708 int rc = LDAP_INVALID_DN_SYNTAX;
710 LDAPDN *newDN = NULL;
711 LDAPRDN *newRDN = NULL;
716 Debug( LDAP_DEBUG_TRACE, "=> ldap_str2dn(%s,%u)\n%s", str, flags, "" );
720 switch ( LDAP_DN_FORMAT( flags ) ) {
721 case LDAP_DN_FORMAT_LDAP:
722 case LDAP_DN_FORMAT_LDAPV3:
723 case LDAP_DN_FORMAT_LDAPV2:
724 case LDAP_DN_FORMAT_DCE:
727 /* unsupported in str2dn */
728 case LDAP_DN_FORMAT_UFN:
729 case LDAP_DN_FORMAT_AD_CANONICAL:
730 return( LDAP_INVALID_DN_SYNTAX );
733 return( LDAP_OTHER );
736 if ( str[ 0 ] == '\0' ) {
737 return( LDAP_SUCCESS );
741 if ( LDAP_DN_DCE( flags ) ) {
744 * (from Luke Howard: thnx) A RDN separator is required
745 * at the beginning of an (absolute) DN.
747 if ( !LDAP_DN_RDN_SEP_DCE( p[ 0 ] ) ) {
752 } else if ( LDAP_DN_LDAP( flags ) ) {
754 * if dn starts with '/' let's make it a DCE dn
756 if ( LDAP_DN_RDN_SEP_DCE( p[ 0 ] ) ) {
757 flags |= LDAP_DN_FORMAT_DCE;
762 for ( ; p[ 0 ]; p++ ) {
766 err = ldap_str2rdn( p, &newRDN, &p, flags );
767 if ( err != LDAP_SUCCESS ) {
772 * We expect a rdn separator
775 switch ( LDAP_DN_FORMAT( flags ) ) {
776 case LDAP_DN_FORMAT_LDAPV3:
777 if ( !LDAP_DN_RDN_SEP( p[ 0 ] ) ) {
783 case LDAP_DN_FORMAT_LDAP:
784 case LDAP_DN_FORMAT_LDAPV2:
785 if ( !LDAP_DN_RDN_SEP_V2( p[ 0 ] ) ) {
791 case LDAP_DN_FORMAT_DCE:
792 if ( !LDAP_DN_RDN_SEP_DCE( p[ 0 ] ) ) {
801 if ( LDAP_DN_DCE( flags ) ) {
802 /* add in reversed order */
803 dn = ldapava_insert_into_dn( newDN, newRDN, 0 );
805 dn = ldapava_append_to_dn( newDN, newRDN );
816 if ( p[ 0 ] == '\0' ) {
819 * the DN is over, phew
828 ldap_rdnfree( newRDN );
832 ldap_dnfree( newDN );
838 Debug( LDAP_DEBUG_TRACE, "<= ldap_str2dn(%s,%u)=%d\n", str, flags, rc );
847 * Parses a relative DN according to flags up to a rdn separator
848 * or to the end of str.
849 * Returns the rdn and a pointer to the string continuation, which
850 * corresponds to the rdn separator or to '\0' in case the string is over.
853 ldap_str2rdn( const char *str, LDAPRDN **rdn, const char **n, unsigned flags )
857 int rc = LDAP_INVALID_DN_SYNTAX;
858 int attrTypeEncoding = LDAP_AVA_STRING,
859 attrValueEncoding = LDAP_AVA_STRING;
861 struct berval attrType = { 0, NULL };
862 struct berval attrValue = { 0, NULL };
864 LDAPRDN *newRDN = NULL;
867 assert( rdn || flags & LDAP_DN_SKIP );
870 Debug( LDAP_DEBUG_TRACE, "=> ldap_str2rdn(%s,%u)\n%s", str, flags, "" );
877 switch ( LDAP_DN_FORMAT( flags ) ) {
878 case LDAP_DN_FORMAT_LDAP:
879 case LDAP_DN_FORMAT_LDAPV3:
880 case LDAP_DN_FORMAT_LDAPV2:
881 case LDAP_DN_FORMAT_DCE:
884 /* unsupported in str2dn */
885 case LDAP_DN_FORMAT_UFN:
886 case LDAP_DN_FORMAT_AD_CANONICAL:
887 return( LDAP_INVALID_DN_SYNTAX );
890 return( LDAP_OTHER );
893 if ( str[ 0 ] == '\0' ) {
894 return( LDAP_SUCCESS );
898 for ( ; p[ 0 ] || state == GOTAVA; ) {
901 * The parser in principle advances one token a time,
902 * or toggles state if preferable.
907 * an AttributeType can be encoded as:
908 * - its string representation; in detail, implementations
909 * MUST recognize AttributeType string type names listed
910 * in section 2.3 of draft-ietf-ldapbis-dn-XX.txt, and
911 * MAY recognize other names.
912 * - its numeric OID (a dotted decimal string); in detail
913 * RFC 2253 asserts that ``Implementations MUST allow
914 * an oid in the attribute type to be prefixed by one
915 * of the character strings "oid." or "OID."''. As soon
916 * as draft-ietf-ldapbis-dn-XX.txt obsoletes RFC 2253
917 * I'm not sure whether this is required or not any
918 * longer; to be liberal, we still implement it.
921 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
922 if ( !LDAP_DN_ALLOW_ONE_SPACE( flags ) ) {
929 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
930 if ( !LDAP_DN_ALLOW_SPACES( flags ) ) {
935 /* whitespace is allowed (and trimmed) */
937 while ( p[ 0 ] && LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
942 /* error: we expected an AVA */
948 if ( LDAP_DN_OID_LEADCHAR( p[ 0 ] ) ) {
949 state = B4OIDATTRTYPE;
953 /* else must be alpha */
954 if ( !LDAP_DN_DESC_LEADCHAR( p[ 0 ] ) ) {
958 /* LDAPv2 "oid." prefix */
959 if ( LDAP_DN_LDAPV2( flags ) ) {
961 * to be overly pedantic, we only accept
964 if ( flags & LDAP_DN_PEDANTIC ) {
965 if ( !strncmp( p, "OID.", 4 )
966 || !strncmp( p, "oid.", 4 ) ) {
968 state = B4OIDATTRTYPE;
972 if ( !strncasecmp( p, "oid.", 4 ) ) {
974 state = B4OIDATTRTYPE;
980 state = B4STRINGATTRTYPE;
983 case B4OIDATTRTYPE: {
984 int err = LDAP_SUCCESS;
987 type = parse_numericoid( &p, &err, 0 );
988 if ( type == NULL ) {
992 if ( flags & LDAP_DN_SKIP ) {
994 * FIXME: hack for skipping a rdn;
995 * need a cleaner solution
1000 ber_str2bv( type, 0, 0, &attrType );
1003 attrTypeEncoding = LDAP_AVA_BINARY;
1005 state = B4AVAEQUALS;
1009 case B4STRINGATTRTYPE: {
1010 const char *startPos, *endPos = NULL;
1014 * the starting char has been found to be
1015 * a LDAP_DN_DESC_LEADCHAR so we don't re-check it
1016 * FIXME: DCE attr types seem to have a more
1017 * restrictive syntax (no '-' ...)
1019 for ( startPos = p++; p[ 0 ]; p++ ) {
1020 if ( LDAP_DN_DESC_CHAR( p[ 0 ] ) ) {
1024 if ( LDAP_DN_LANG_SEP( p[ 0 ] ) ) {
1027 * RFC 2253 does not explicitly
1028 * allow lang extensions to attribute
1031 if ( flags & LDAP_DN_PEDANTIC ) {
1036 * we trim ';' and following lang
1037 * and so from attribute types
1040 for ( ; LDAP_DN_ATTRDESC_CHAR( p[ 0 ] )
1041 || LDAP_DN_LANG_SEP( p[ 0 ] ); p++ ) {
1049 len = ( endPos ? endPos : p ) - startPos;
1054 attrTypeEncoding = LDAP_AVA_STRING;
1057 * here we need to decide whether to use it as is
1058 * or turn it in OID form; as a consequence, we
1059 * need to decide whether to binary encode the value
1062 state = B4AVAEQUALS;
1064 if ( flags & LDAP_DN_SKIP ) {
1068 attrType.bv_val = LDAP_STRNDUP( startPos, len );
1069 if ( attrType.bv_val == NULL ) {
1070 rc = LDAP_NO_MEMORY;
1073 attrType.bv_len = len;
1079 /* spaces may not be allowed */
1080 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1081 if ( !LDAP_DN_ALLOW_SPACES( flags ) ) {
1086 for ( p++; LDAP_DN_ASCII_SPACE( p[ 0 ] ); p++ ) {
1091 /* need equal sign */
1092 if ( !LDAP_DN_AVA_EQUALS( p[ 0 ] ) ) {
1097 /* spaces may not be allowed */
1098 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1099 if ( !LDAP_DN_ALLOW_SPACES( flags ) ) {
1104 for ( p++; LDAP_DN_ASCII_SPACE( p[ 0 ] ); p++ ) {
1110 * octothorpe means a BER encoded value will follow
1111 * FIXME: I don't think DCE will allow it
1113 if ( LDAP_DN_OCTOTHORPE( p[ 0 ] ) ) {
1115 attrValueEncoding = LDAP_AVA_BINARY;
1116 state = B4BINARYVALUE;
1120 /* STRING value expected */
1123 * if we're pedantic, an attribute type in OID form
1124 * SHOULD imply a BER encoded attribute value; we
1125 * should at least issue a warning
1127 if ( ( flags & LDAP_DN_PEDANTIC )
1128 && ( attrTypeEncoding == LDAP_AVA_BINARY ) ) {
1129 /* OID attrType SHOULD use binary encoding */
1133 attrValueEncoding = LDAP_AVA_STRING;
1136 * LDAPv2 allows the attribute value to be quoted;
1137 * also, IA5 values are expected, in principle
1139 if ( LDAP_DN_LDAPV2( flags ) || LDAP_DN_LDAP( flags ) ) {
1140 if ( LDAP_DN_QUOTES( p[ 0 ] ) ) {
1142 state = B4IA5VALUEQUOTED;
1146 if ( LDAP_DN_LDAPV2( flags ) ) {
1153 * here STRING means RFC 2253 string
1154 * FIXME: what about DCE strings?
1156 state = B4STRINGVALUE;
1160 if ( hexstr2binval( p, &attrValue, &p, flags ) ) {
1168 switch ( LDAP_DN_FORMAT( flags ) ) {
1169 case LDAP_DN_FORMAT_LDAP:
1170 case LDAP_DN_FORMAT_LDAPV3:
1171 if ( str2strval( p, &attrValue, &p, flags,
1172 &attrValueEncoding ) ) {
1177 case LDAP_DN_FORMAT_DCE:
1178 if ( DCE2strval( p, &attrValue, &p, flags ) ) {
1191 if ( IA52strval( p, &attrValue, &p, flags ) ) {
1198 case B4IA5VALUEQUOTED:
1200 /* lead quote already stripped */
1201 if ( quotedIA52strval( p, &attrValue,
1212 if ( !( flags & LDAP_DN_SKIP ) ) {
1217 * we accept empty values
1219 ava = ldapava_new( &attrType, &attrValue,
1220 attrValueEncoding );
1221 if ( ava == NULL ) {
1222 rc = LDAP_NO_MEMORY;
1226 rdn = ldapava_append_to_rdn( newRDN, ava );
1227 if ( rdn == NULL ) {
1228 rc = LDAP_NO_MEMORY;
1235 * if we got an AVA separator ('+', or ',' for DCE )
1236 * we expect a new AVA for this RDN; otherwise
1237 * we add the RDN to the DN
1239 switch ( LDAP_DN_FORMAT( flags ) ) {
1240 case LDAP_DN_FORMAT_LDAP:
1241 case LDAP_DN_FORMAT_LDAPV3:
1242 case LDAP_DN_FORMAT_LDAPV2:
1243 if ( !LDAP_DN_AVA_SEP( p[ 0 ] ) ) {
1248 case LDAP_DN_FORMAT_DCE:
1249 if ( !LDAP_DN_AVA_SEP_DCE( p[ 0 ] ) ) {
1257 * the RDN is over, phew
1264 /* they should have been used in an AVA */
1265 attrType.bv_val = NULL;
1266 attrValue.bv_val = NULL;
1280 /* They are set to NULL after they're used in an AVA */
1281 if ( attrType.bv_val ) {
1282 free( attrType.bv_val );
1285 if ( attrValue.bv_val ) {
1286 free( attrValue.bv_val );
1290 ldap_rdnfree( newRDN );
1296 Debug( LDAP_DEBUG_TRACE, "<= ldap_str2rdn(%*s)=%d\n",
1306 * reads in a UTF-8 string value, unescaping stuff:
1307 * '\' + LDAP_DN_NEEDESCAPE(c) -> 'c'
1308 * '\' + HEXPAIR(p) -> unhex(p)
1311 str2strval( const char *str, struct berval *val, const char **next, unsigned flags, unsigned *retFlags )
1313 const char *p, *startPos, *endPos = NULL;
1314 ber_len_t len, escapes, unescapes;
1322 for ( startPos = p = str, escapes = 0, unescapes = 0; p[ 0 ]; p++ ) {
1323 if ( LDAP_DN_ESCAPE( p[ 0 ] ) ) {
1325 if ( p[ 0 ] == '\0' ) {
1328 if ( ( p == startPos + 1 && LDAP_DN_NEEDESCAPE_LEAD( p[ 0 ] ) )
1329 || ( LDAP_DN_VALUE_END( p[ 1 ] ) && LDAP_DN_NEEDESCAPE_TRAIL( p[ 0 ] ) )
1330 || LDAP_DN_NEEDESCAPE( p[ 0 ] ) ) {
1335 if ( LDAP_DN_HEXPAIR( p ) ) {
1338 hexstr2bin( p, &c );
1341 if ( !LDAP_DN_ASCII_PRINTABLE( c ) ) {
1344 * we assume the string is UTF-8
1346 *retFlags = LDAP_AVA_NONPRINTABLE;
1353 if ( LDAP_DN_PEDANTIC & flags ) {
1357 * FIXME: we allow escaping
1358 * of chars that don't need
1359 * to and do not belong to
1360 * HEXDIGITS (we also allow
1361 * single hexdigit; maybe we
1366 } else if ( ( LDAP_DN_LDAP( flags ) && LDAP_DN_VALUE_END_V2( p[ 0 ] ) )
1367 || ( LDAP_DN_LDAPV3( flags ) && LDAP_DN_VALUE_END( p[ 0 ] ) ) ) {
1370 } else if ( LDAP_DN_NEEDESCAPE( p[ 0 ] ) ) {
1372 * FIXME: maybe we can add
1373 * escapes if not pedantic?
1380 * we do allow unescaped spaces at the end
1381 * of the value only in non-pedantic mode
1383 if ( p > startPos + 1 && LDAP_DN_ASCII_SPACE( p[ -1 ] ) &&
1384 !LDAP_DN_ESCAPE( p[ -2 ] ) ) {
1385 if ( flags & LDAP_DN_PEDANTIC ) {
1389 /* strip trailing (unescaped) spaces */
1390 for ( endPos = p - 1;
1391 endPos > startPos + 1 &&
1392 LDAP_DN_ASCII_SPACE( endPos[ -1 ] ) &&
1393 !LDAP_DN_ESCAPE( endPos[ -2 ] );
1400 if ( flags & LDAP_DN_SKIP ) {
1405 * FIXME: test memory?
1407 len = ( endPos ? endPos : p ) - startPos - escapes - unescapes;
1410 if ( escapes == 0 && unescapes == 0 ) {
1411 val->bv_val = LDAP_STRNDUP( startPos, len );
1416 val->bv_val = LDAP_MALLOC( len + 1 );
1417 for ( s = 0, d = 0; d < len; ) {
1418 if ( LDAP_DN_ESCAPE( startPos[ s ] ) ) {
1420 if ( ( s == 0 && LDAP_DN_NEEDESCAPE_LEAD( startPos[ s ] ) )
1421 || ( s == len - 1 && LDAP_DN_NEEDESCAPE_TRAIL( startPos[ s ] ) )
1422 || LDAP_DN_NEEDESCAPE( startPos[ s ] ) ) {
1423 val->bv_val[ d++ ] =
1426 } else if ( LDAP_DN_HEXPAIR( &startPos[ s ] ) ) {
1429 hexstr2bin( &startPos[ s ], &c );
1430 val->bv_val[ d++ ] = c;
1435 * we allow escaping of chars
1436 * that do not need to
1438 val->bv_val[ d++ ] =
1443 val->bv_val[ d++ ] = startPos[ s++ ];
1447 val->bv_val[ d ] = '\0';
1448 assert( strlen( val->bv_val ) == len );
1455 DCE2strval( const char *str, struct berval *val, const char **next, unsigned flags )
1457 const char *p, *startPos, *endPos = NULL;
1458 ber_len_t len, escapes;
1466 for ( startPos = p = str, escapes = 0; p[ 0 ]; p++ ) {
1467 if ( LDAP_DN_ESCAPE_DCE( p[ 0 ] ) ) {
1469 if ( LDAP_DN_NEEDESCAPE_DCE( p[ 0 ] ) ) {
1476 } else if ( LDAP_DN_VALUE_END_DCE( p[ 0 ] ) ) {
1481 * FIXME: can we accept anything else? I guess we need
1482 * to stop if a value is not legal
1487 * (unescaped) trailing spaces are trimmed must be silently ignored;
1490 if ( p > startPos + 1 && LDAP_DN_ASCII_SPACE( p[ -1 ] ) &&
1491 !LDAP_DN_ESCAPE( p[ -2 ] ) ) {
1492 if ( flags & LDAP_DN_PEDANTIC ) {
1496 /* strip trailing (unescaped) spaces */
1497 for ( endPos = p - 1;
1498 endPos > startPos + 1 &&
1499 LDAP_DN_ASCII_SPACE( endPos[ -1 ] ) &&
1500 !LDAP_DN_ESCAPE( endPos[ -2 ] );
1507 if ( flags & LDAP_DN_SKIP ) {
1511 len = ( endPos ? endPos : p ) - startPos - escapes;
1513 if ( escapes == 0 ){
1514 val->bv_val = LDAP_STRNDUP( startPos, len );
1519 val->bv_val = LDAP_MALLOC( len + 1 );
1520 for ( s = 0, d = 0; d < len; ) {
1522 * This point is reached only if escapes
1523 * are properly used, so all we need to
1526 if ( LDAP_DN_ESCAPE_DCE( startPos[ s ] ) ) {
1530 val->bv_val[ d++ ] = startPos[ s++ ];
1532 val->bv_val[ d ] = '\0';
1533 assert( strlen( val->bv_val ) == len );
1540 IA52strval( const char *str, struct berval *val, const char **next, unsigned flags )
1542 const char *p, *startPos, *endPos = NULL;
1543 ber_len_t len, escapes;
1555 for ( startPos = p = str, escapes = 0; p[ 0 ]; p++ ) {
1556 if ( LDAP_DN_ESCAPE( p[ 0 ] ) ) {
1558 if ( p[ 0 ] == '\0' ) {
1562 if ( !LDAP_DN_NEEDESCAPE( p[ 0 ] )
1563 && ( LDAP_DN_PEDANTIC & flags ) ) {
1568 } else if ( LDAP_DN_VALUE_END_V2( p[ 0 ] ) ) {
1573 * FIXME: can we accept anything else? I guess we need
1574 * to stop if a value is not legal
1578 /* strip trailing (unescaped) spaces */
1580 endPos > startPos + 1 &&
1581 LDAP_DN_ASCII_SPACE( endPos[ -1 ] ) &&
1582 !LDAP_DN_ESCAPE( endPos[ -2 ] );
1588 if ( flags & LDAP_DN_SKIP ) {
1592 len = ( endPos ? endPos : p ) - startPos - escapes;
1594 if ( escapes == 0 ) {
1595 val->bv_val = LDAP_STRNDUP( startPos, len );
1600 val->bv_val = LDAP_MALLOC( len + 1 );
1601 for ( s = 0, d = 0; d < len; ) {
1602 if ( LDAP_DN_ESCAPE( startPos[ s ] ) ) {
1605 val->bv_val[ d++ ] = startPos[ s++ ];
1607 val->bv_val[ d ] = '\0';
1608 assert( strlen( val->bv_val ) == len );
1615 quotedIA52strval( const char *str, struct berval *val, const char **next, unsigned flags )
1617 const char *p, *startPos, *endPos = NULL;
1619 unsigned escapes = 0;
1627 /* initial quote already eaten */
1628 for ( startPos = p = str; p[ 0 ]; p++ ) {
1630 * According to RFC 1779, the quoted value can
1631 * contain escaped as well as unescaped special values;
1632 * as a consequence we tolerate escaped values
1633 * (e.g. '"\,"' -> '\,') and escape unescaped specials
1634 * (e.g. '","' -> '\,').
1636 if ( LDAP_DN_ESCAPE( p[ 0 ] ) ) {
1637 if ( p[ 1 ] == '\0' ) {
1642 if ( !LDAP_DN_V2_PAIR( p[ 0 ] )
1643 && ( LDAP_DN_PEDANTIC & flags ) ) {
1645 * do we allow to escape normal chars?
1646 * LDAPv2 does not allow any mechanism
1647 * for escaping chars with '\' and hex
1654 } else if ( LDAP_DN_QUOTES( p[ 0 ] ) ) {
1656 /* eat closing quotes */
1662 * FIXME: can we accept anything else? I guess we need
1663 * to stop if a value is not legal
1667 if ( endPos == NULL ) {
1671 /* Strip trailing (unescaped) spaces */
1672 for ( ; p[ 0 ] && LDAP_DN_ASCII_SPACE( p[ 0 ] ); p++ ) {
1677 if ( flags & LDAP_DN_SKIP ) {
1681 len = endPos - startPos - escapes;
1684 if ( escapes == 0 ) {
1685 val->bv_val = LDAP_STRNDUP( startPos, len );
1690 val->bv_val = LDAP_MALLOC( len + 1 );
1693 for ( s = d = 0; d < len; ) {
1694 if ( LDAP_DN_ESCAPE( str[ s ] ) ) {
1697 val->bv_val[ d++ ] = str[ s++ ];
1699 val->bv_val[ d ] = '\0';
1700 assert( strlen( val->bv_val ) == len );
1707 hexstr2bin( const char *str, char *c )
1717 if ( LDAP_DN_ASCII_DIGIT( c1 ) ) {
1721 if ( LDAP_DN_ASCII_UCASE_HEXALPHA( c1 ) ) {
1724 assert( LDAP_DN_ASCII_LCASE_HEXALPHA( c1 ) );
1731 if ( LDAP_DN_ASCII_DIGIT( c2 ) ) {
1735 if ( LDAP_DN_ASCII_UCASE_HEXALPHA( c2 ) ) {
1736 *c += c2 - 'A' + 10;
1738 assert( LDAP_DN_ASCII_LCASE_HEXALPHA( c2 ) );
1739 *c += c2 - 'a' + 10;
1747 hexstr2binval( const char *str, struct berval *val, const char **next, unsigned flags )
1749 const char *p, *startPos, *endPos = NULL;
1759 for ( startPos = p = str; p[ 0 ]; p += 2 ) {
1760 switch ( LDAP_DN_FORMAT( flags ) ) {
1761 case LDAP_DN_FORMAT_LDAPV3:
1762 if ( LDAP_DN_VALUE_END( p[ 0 ] ) ) {
1767 case LDAP_DN_FORMAT_LDAP:
1768 case LDAP_DN_FORMAT_LDAPV2:
1769 if ( LDAP_DN_VALUE_END_V2( p[ 0 ] ) ) {
1774 case LDAP_DN_FORMAT_DCE:
1775 if ( LDAP_DN_VALUE_END_DCE( p[ 0 ] ) ) {
1781 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1782 if ( flags & LDAP_DN_PEDANTIC ) {
1787 for ( ; p[ 0 ]; p++ ) {
1788 switch ( LDAP_DN_FORMAT( flags ) ) {
1789 case LDAP_DN_FORMAT_LDAPV3:
1790 if ( LDAP_DN_VALUE_END( p[ 0 ] ) ) {
1795 case LDAP_DN_FORMAT_LDAP:
1796 case LDAP_DN_FORMAT_LDAPV2:
1797 if ( LDAP_DN_VALUE_END_V2( p[ 0 ] ) ) {
1802 case LDAP_DN_FORMAT_DCE:
1803 if ( LDAP_DN_VALUE_END_DCE( p[ 0 ] ) ) {
1812 if ( !LDAP_DN_HEXPAIR( p ) ) {
1820 if ( flags & LDAP_DN_SKIP ) {
1824 len = ( ( endPos ? endPos : p ) - startPos ) / 2;
1826 assert( 2 * len == (ber_len_t) (( endPos ? endPos : p ) - startPos ));
1829 val->bv_val = LDAP_MALLOC( len + 1 );
1830 if ( val->bv_val == NULL ) {
1831 return( LDAP_NO_MEMORY );
1834 for ( s = 0, d = 0; d < len; s += 2, d++ ) {
1837 hexstr2bin( &startPos[ s ], &c );
1839 val->bv_val[ d ] = c;
1842 val->bv_val[ d ] = '\0';
1848 * convert a byte in a hexadecimal pair
1851 byte2hexpair( const char *val, char *pair )
1853 static const char hexdig[] = "0123456789ABCDEF";
1859 * we assume the string has enough room for the hex encoding
1863 pair[ 0 ] = hexdig[ 0x0f & ( val[ 0 ] >> 4 ) ];
1864 pair[ 1 ] = hexdig[ 0x0f & val[ 0 ] ];
1870 * convert a binary value in hexadecimal pairs
1873 binval2hexstr( struct berval *val, char *str )
1880 if ( val->bv_len == 0 ) {
1885 * we assume the string has enough room for the hex encoding
1889 for ( s = 0, d = 0; s < val->bv_len; s++, d += 2 ) {
1890 byte2hexpair( &val->bv_val[ s ], &str[ d ] );
1897 * Length of the string representation, accounting for escaped hex
1901 strval2strlen( struct berval *val, unsigned flags, ber_len_t *len )
1903 ber_len_t l, cl = 1;
1905 int escaped_byte_len = LDAP_DN_IS_PRETTY( flags ) ? 1 : 3;
1906 #ifdef PRETTY_ESCAPE
1907 int escaped_ascii_len = LDAP_DN_IS_PRETTY( flags ) ? 2 : 3;
1908 #endif /* PRETTY_ESCAPE */
1914 if ( val->bv_len == 0 ) {
1918 for ( l = 0, p = val->bv_val; p[ 0 ]; p += cl ) {
1919 cl = LDAP_UTF8_CHARLEN( p );
1921 /* illegal utf-8 char! */
1924 } else if ( cl > 1 ) {
1927 for ( cnt = 1; cnt < cl; cnt++ ) {
1928 if ( ( p[ cnt ] & 0x80 ) == 0x00 ) {
1932 l += escaped_byte_len * cl;
1934 } else if ( LDAP_DN_NEEDESCAPE( p[ 0 ] )
1935 || ( p == val->bv_val && LDAP_DN_NEEDESCAPE_LEAD( p[ 0 ] ) )
1936 || ( !p[ 1 ] && LDAP_DN_NEEDESCAPE_TRAIL( p[ 0 ] ) ) ) {
1937 #ifdef PRETTY_ESCAPE
1938 if ( LDAP_DN_WILLESCAPE_HEX( flags, p[ 0 ] ) ) {
1941 * there might be some chars we want
1942 * to escape in form of a couple
1943 * of hexdigits for optimization purposes
1948 l += escaped_ascii_len;
1950 #else /* ! PRETTY_ESCAPE */
1952 #endif /* ! PRETTY_ESCAPE */
1965 * convert to string representation, escaping with hex the UTF-8 stuff;
1966 * assume the destination has enough room for escaping
1969 strval2str( struct berval *val, char *str, unsigned flags, ber_len_t *len )
1971 ber_len_t s, d, end;
1977 if ( val->bv_len == 0 ) {
1983 * we assume the string has enough room for the hex encoding
1986 for ( s = 0, d = 0, end = val->bv_len - 1; s < val->bv_len; ) {
1987 ber_len_t cl = LDAP_UTF8_CHARLEN( &val->bv_val[ s ] );
1990 * there might be some chars we want to escape in form
1991 * of a couple of hexdigits for optimization purposes
1993 if ( ( cl > 1 && !LDAP_DN_IS_PRETTY( flags ) )
1994 #ifdef PRETTY_ESCAPE
1995 || LDAP_DN_WILLESCAPE_HEX( flags, val->bv_val[ s ] )
1996 #else /* ! PRETTY_ESCAPE */
1997 || LDAP_DN_NEEDESCAPE( val->bv_val[ s ] )
1998 || ( d == 0 && LDAP_DN_NEEDESCAPE_LEAD( val->bv_val[ s ] ) )
1999 || ( s == end && LDAP_DN_NEEDESCAPE_TRAIL( val->bv_val[ s ] ) )
2001 #endif /* ! PRETTY_ESCAPE */
2005 byte2hexpair( &val->bv_val[ s ], &str[ d ] );
2010 } else if ( cl > 1 ) {
2012 str[ d++ ] = val->bv_val[ s++ ];
2016 #ifdef PRETTY_ESCAPE
2017 if ( LDAP_DN_NEEDESCAPE( val->bv_val[ s ] )
2018 || ( d == 0 && LDAP_DN_NEEDESCAPE_LEAD( val->bv_val[ s ] ) )
2019 || ( s == end && LDAP_DN_NEEDESCAPE_TRAIL( val->bv_val[ s ] ) ) ) {
2021 if ( !LDAP_DN_IS_PRETTY( flags ) ) {
2022 byte2hexpair( &val->bv_val[ s ], &str[ d ] );
2028 #endif /* PRETTY_ESCAPE */
2029 str[ d++ ] = val->bv_val[ s++ ];
2039 * Length of the IA5 string representation (no UTF-8 allowed)
2042 strval2IA5strlen( struct berval *val, unsigned flags, ber_len_t *len )
2051 if ( val->bv_len == 0 ) {
2055 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2057 * Turn value into a binary encoded BER
2062 for ( l = 0, p = val->bv_val; p[ 0 ]; p++ ) {
2063 if ( LDAP_DN_NEEDESCAPE( p[ 0 ] )
2064 || ( p == val->bv_val && LDAP_DN_NEEDESCAPE_LEAD( p[ 0 ] ) )
2065 || ( !p[ 1 ] && LDAP_DN_NEEDESCAPE_TRAIL( p[ 0 ] ) ) ) {
2080 * convert to string representation (np UTF-8)
2081 * assume the destination has enough room for escaping
2084 strval2IA5str( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2086 ber_len_t s, d, end;
2092 if ( val->bv_len == 0 ) {
2097 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2099 * Turn value into a binary encoded BER
2106 * we assume the string has enough room for the hex encoding
2110 for ( s = 0, d = 0, end = val->bv_len - 1; s < val->bv_len; ) {
2111 if ( LDAP_DN_NEEDESCAPE( val->bv_val[ s ] )
2112 || ( s == 0 && LDAP_DN_NEEDESCAPE_LEAD( val->bv_val[ s ] ) )
2113 || ( s == end && LDAP_DN_NEEDESCAPE_TRAIL( val->bv_val[ s ] ) ) ) {
2116 str[ d++ ] = val->bv_val[ s++ ];
2126 * Length of the (supposedly) DCE string representation,
2127 * accounting for escaped hex of UTF-8 chars
2130 strval2DCEstrlen( struct berval *val, unsigned flags, ber_len_t *len )
2139 if ( val->bv_len == 0 ) {
2143 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2145 * FIXME: Turn the value into a binary encoded BER?
2150 for ( l = 0, p = val->bv_val; p[ 0 ]; p++ ) {
2151 if ( LDAP_DN_NEEDESCAPE_DCE( p[ 0 ] ) ) {
2166 * convert to (supposedly) DCE string representation,
2167 * escaping with hex the UTF-8 stuff;
2168 * assume the destination has enough room for escaping
2171 strval2DCEstr( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2179 if ( val->bv_len == 0 ) {
2184 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2186 * FIXME: Turn the value into a binary encoded BER?
2194 * we assume the string has enough room for the hex encoding
2198 for ( s = 0, d = 0; s < val->bv_len; ) {
2199 if ( LDAP_DN_NEEDESCAPE_DCE( val->bv_val[ s ] ) ) {
2202 str[ d++ ] = val->bv_val[ s++ ];
2212 * Length of the (supposedly) AD canonical string representation,
2213 * accounting for escaped hex of UTF-8 chars
2216 strval2ADstrlen( struct berval *val, unsigned flags, ber_len_t *len )
2225 if ( val->bv_len == 0 ) {
2229 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2231 * FIXME: Turn the value into a binary encoded BER?
2236 for ( l = 0, p = val->bv_val; p[ 0 ]; p++ ) {
2237 if ( LDAP_DN_NEEDESCAPE_AD( p[ 0 ] ) ) {
2252 * convert to (supposedly) AD string representation,
2253 * escaping with hex the UTF-8 stuff;
2254 * assume the destination has enough room for escaping
2257 strval2ADstr( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2265 if ( val->bv_len == 0 ) {
2270 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2272 * FIXME: Turn the value into a binary encoded BER?
2280 * we assume the string has enough room for the hex encoding
2284 for ( s = 0, d = 0; s < val->bv_len; ) {
2285 if ( LDAP_DN_NEEDESCAPE_AD( val->bv_val[ s ] ) ) {
2288 str[ d++ ] = val->bv_val[ s++ ];
2298 * If the DN is terminated by single-AVA RDNs with attribute type of "dc",
2299 * the first part of the AD representation of the DN is written in DNS
2300 * form, i.e. dot separated domain name components (as suggested
2301 * by Luke Howard, http://www.padl.com/~lukeh)
2304 dn2domain( LDAPDN *dn, struct berval *bv, int pos, int *iRDN )
2307 int domain = 0, first = 1;
2308 ber_len_t l = 1; /* we move the null also */
2311 /* we are guaranteed there's enough memory in str */
2317 assert( *iRDN >= 0 );
2319 str = bv->bv_val + pos;
2321 for ( i = *iRDN; i >= 0; i-- ) {
2325 assert( dn[ i ][ 0 ] );
2328 assert( rdn[ 0 ][ 0 ] );
2329 ava = rdn[ 0 ][ 0 ];
2331 if ( !LDAP_DN_IS_RDN_DC( rdn ) ) {
2339 AC_MEMCPY( str, ava->la_value.bv_val,
2340 ava->la_value.bv_len + 1);
2341 l += ava->la_value.bv_len;
2344 AC_MEMCPY( str + ava->la_value.bv_len + 1, bv->bv_val, l);
2345 AC_MEMCPY( str, ava->la_value.bv_val,
2346 ava->la_value.bv_len );
2347 str[ ava->la_value.bv_len ] = '.';
2348 l += ava->la_value.bv_len + 1;
2353 bv->bv_len = pos + l - 1;
2359 rdn2strlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len,
2360 int ( *s2l )( struct berval *v, unsigned f, ber_len_t *l ) )
2367 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2368 LDAPAVA *ava = rdn[ iAVA ][ 0 ];
2370 /* len(type) + '=' + '+' | ',' */
2371 l += ava->la_attr.bv_len + 2;
2373 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2374 /* octothorpe + twice the length */
2375 l += 1 + 2 * ava->la_value.bv_len;
2379 unsigned f = flags | ava->la_flags;
2381 if ( ( *s2l )( &ava->la_value, f, &vl ) ) {
2394 rdn2str( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len,
2395 int ( *s2s ) ( struct berval *v, char * s, unsigned f, ber_len_t *l ) )
2400 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2401 LDAPAVA *ava = rdn[ iAVA ][ 0 ];
2403 AC_MEMCPY( &str[ l ], ava->la_attr.bv_val,
2404 ava->la_attr.bv_len );
2405 l += ava->la_attr.bv_len;
2409 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2411 if ( binval2hexstr( &ava->la_value, &str[ l ] ) ) {
2414 l += 2 * ava->la_value.bv_len;
2418 unsigned f = flags | ava->la_flags;
2420 if ( ( *s2s )( &ava->la_value, &str[ l ], f, &vl ) ) {
2425 str[ l++ ] = ( rdn[ iAVA + 1 ] ? '+' : ',' );
2434 rdn2DCEstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len )
2441 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2442 LDAPAVA *ava = rdn[ iAVA ][ 0 ];
2444 /* len(type) + '=' + ',' | '/' */
2445 l += ava->la_attr.bv_len + 2;
2447 switch ( ava->la_flags ) {
2448 case LDAP_AVA_BINARY:
2449 /* octothorpe + twice the length */
2450 l += 1 + 2 * ava->la_value.bv_len;
2453 case LDAP_AVA_STRING: {
2455 unsigned f = flags | ava->la_flags;
2457 if ( strval2DCEstrlen( &ava->la_value, f, &vl ) ) {
2475 rdn2DCEstr( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len, int first )
2480 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2481 LDAPAVA *ava = rdn[ iAVA ][ 0 ];
2486 str[ l++ ] = ( iAVA ? ',' : '/' );
2489 AC_MEMCPY( &str[ l ], ava->la_attr.bv_val,
2490 ava->la_attr.bv_len );
2491 l += ava->la_attr.bv_len;
2495 switch ( ava->la_flags ) {
2496 case LDAP_AVA_BINARY:
2498 if ( binval2hexstr( &ava->la_value, &str[ l ] ) ) {
2501 l += 2 * ava->la_value.bv_len;
2504 case LDAP_AVA_STRING: {
2506 unsigned f = flags | ava->la_flags;
2508 if ( strval2DCEstr( &ava->la_value, &str[ l ], f, &vl ) ) {
2526 rdn2UFNstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len )
2536 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2537 LDAPAVA *ava = rdn[ iAVA ][ 0 ];
2540 l += ( rdn[ iAVA + 1 ] ? 3 : 2 );
2542 /* FIXME: are binary values allowed in UFN? */
2543 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2544 /* octothorpe + twice the value */
2545 l += 1 + 2 * ava->la_value.bv_len;
2549 unsigned f = flags | ava->la_flags;
2551 if ( strval2strlen( &ava->la_value, f, &vl ) ) {
2564 rdn2UFNstr( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len )
2569 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2570 LDAPAVA *ava = rdn[ iAVA ][ 0 ];
2572 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2574 if ( binval2hexstr( &ava->la_value, &str[ l ] ) ) {
2577 l += 2 * ava->la_value.bv_len;
2581 unsigned f = flags | ava->la_flags;
2583 if ( strval2str( &ava->la_value, &str[ l ], f, &vl ) ) {
2589 if ( rdn[ iAVA + 1 ]) {
2590 AC_MEMCPY( &str[ l ], " + ", 3 );
2594 AC_MEMCPY( &str[ l ], ", ", 2 );
2605 rdn2ADstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len )
2615 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2616 LDAPAVA *ava = rdn[ iAVA ][ 0 ];
2621 /* FIXME: are binary values allowed in UFN? */
2622 switch ( ava->la_flags ) {
2623 case LDAP_AVA_BINARY:
2624 /* octothorpe + twice the value */
2625 l += 1 + 2 * ava->la_value.bv_len;
2628 case LDAP_AVA_STRING: {
2630 unsigned f = flags | ava->la_flags;
2632 if ( strval2ADstrlen( &ava->la_value, f, &vl ) ) {
2650 rdn2ADstr( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len, int first )
2655 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2656 LDAPAVA *ava = rdn[ iAVA ][ 0 ];
2661 str[ l++ ] = ( iAVA ? ',' : '/' );
2664 switch ( ava->la_flags ) {
2665 case LDAP_AVA_BINARY:
2667 if ( binval2hexstr( &ava->la_value, &str[ l ] ) ) {
2670 l += 2 * ava->la_value.bv_len;
2673 case LDAP_AVA_STRING: {
2675 unsigned f = flags | ava->la_flags;
2677 if ( strval2ADstr( &ava->la_value, &str[ l ], f, &vl ) ) {
2697 * Returns in str a string representation of rdn based on flags.
2698 * There is some duplication of code between this and ldap_dn2str;
2699 * this is wanted to reduce the allocation of temporary buffers.
2702 ldap_rdn2str( LDAPRDN *rdn, char **str, unsigned flags )
2709 rc = ldap_rdn2bv( rdn, &bv, flags );
2715 ldap_rdn2bv( LDAPRDN *rdn, struct berval *bv, unsigned flags )
2725 if ( rdn == NULL ) {
2726 bv->bv_val = LDAP_STRDUP( "" );
2727 return( LDAP_SUCCESS );
2731 * This routine wastes "back" bytes at the end of the string
2734 switch ( LDAP_DN_FORMAT( flags ) ) {
2735 case LDAP_DN_FORMAT_LDAPV3:
2736 if ( rdn2strlen( rdn, flags, &l, strval2strlen ) ) {
2737 return( LDAP_OTHER );
2741 case LDAP_DN_FORMAT_LDAPV2:
2742 if ( rdn2strlen( rdn, flags, &l, strval2IA5strlen ) ) {
2743 return( LDAP_OTHER );
2747 case LDAP_DN_FORMAT_UFN:
2748 if ( rdn2UFNstrlen( rdn, flags, &l ) ) {
2749 return( LDAP_OTHER );
2753 case LDAP_DN_FORMAT_DCE:
2754 if ( rdn2DCEstrlen( rdn, flags, &l ) ) {
2755 return( LDAP_OTHER );
2759 case LDAP_DN_FORMAT_AD_CANONICAL:
2760 if ( rdn2ADstrlen( rdn, flags, &l ) ) {
2761 return( LDAP_OTHER );
2766 return( LDAP_INVALID_DN_SYNTAX );
2769 bv->bv_val = LDAP_MALLOC( l + 1 );
2771 switch ( LDAP_DN_FORMAT( flags ) ) {
2772 case LDAP_DN_FORMAT_LDAPV3:
2773 rc = rdn2str( rdn, bv->bv_val, flags, &l, strval2str );
2777 case LDAP_DN_FORMAT_LDAPV2:
2778 rc = rdn2str( rdn, bv->bv_val, flags, &l, strval2IA5str );
2782 case LDAP_DN_FORMAT_UFN:
2783 rc = rdn2UFNstr( rdn, bv->bv_val, flags, &l );
2787 case LDAP_DN_FORMAT_DCE:
2788 rc = rdn2DCEstr( rdn, bv->bv_val, flags, &l, 1 );
2792 case LDAP_DN_FORMAT_AD_CANONICAL:
2793 rc = rdn2ADstr( rdn, bv->bv_val, flags, &l, 1 );
2798 /* need at least one of the previous */
2799 return( LDAP_OTHER );
2803 ldap_memfree( bv->bv_val );
2804 return( LDAP_OTHER );
2807 bv->bv_len = l - back;
2808 bv->bv_val[ bv->bv_len ] = '\0';
2810 return( LDAP_SUCCESS );
2814 * Very bulk implementation; many optimizations can be performed
2815 * - a NULL dn results in an empty string ""
2818 * a) what do we do if a UTF-8 string must be converted in LDAPv2?
2819 * we must encode it in binary form ('#' + HEXPAIRs)
2820 * b) does DCE/AD support UTF-8?
2821 * no clue; don't think so.
2822 * c) what do we do when binary values must be converted in UTF/DCE/AD?
2823 * use binary encoded BER
2825 int ldap_dn2str( LDAPDN *dn, char **str, unsigned flags )
2832 rc = ldap_dn2bv( dn, &bv, flags );
2837 int ldap_dn2bv( LDAPDN *dn, struct berval *bv, unsigned flags )
2840 int rc = LDAP_OTHER;
2843 /* stringifying helpers for LDAPv3/LDAPv2 */
2844 int ( *sv2l ) ( struct berval *v, unsigned f, ber_len_t *l );
2845 int ( *sv2s ) ( struct berval *v, char *s, unsigned f, ber_len_t *l );
2852 Debug( LDAP_DEBUG_TRACE, "=> ldap_dn2bv(%u)\n%s%s", flags, "", "" );
2855 * a null dn means an empty dn string
2856 * FIXME: better raise an error?
2859 bv->bv_val = LDAP_STRDUP( "" );
2860 return( LDAP_SUCCESS );
2863 switch ( LDAP_DN_FORMAT( flags ) ) {
2864 case LDAP_DN_FORMAT_LDAPV3:
2865 sv2l = strval2strlen;
2869 case LDAP_DN_FORMAT_LDAPV2:
2870 sv2l = strval2IA5strlen;
2871 sv2s = strval2IA5str;
2874 for ( iRDN = 0, len = 0; dn[ iRDN ]; iRDN++ ) {
2876 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
2878 if ( rdn2strlen( rdn, flags, &rdnl, sv2l ) ) {
2879 goto return_results;
2885 if ( ( bv->bv_val = LDAP_MALLOC( len + 1 ) ) == NULL ) {
2886 rc = LDAP_NO_MEMORY;
2890 for ( l = 0, iRDN = 0; dn[ iRDN ]; iRDN++ ) {
2892 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
2894 if ( rdn2str( rdn, &bv->bv_val[ l ], flags,
2896 LDAP_FREE( bv->bv_val );
2898 goto return_results;
2906 * trim the last ',' (the allocated memory
2907 * is one byte longer than required)
2909 bv->bv_len = len - 1;
2910 bv->bv_val[ bv->bv_len ] = '\0';
2915 case LDAP_DN_FORMAT_UFN: {
2918 * FIXME: quoting from RFC 1781:
2920 To take a distinguished name, and generate a name of this format with
2921 attribute types omitted, the following steps are followed.
2923 1. If the first attribute is of type CommonName, the type may be
2926 2. If the last attribute is of type Country, the type may be
2929 3. If the last attribute is of type Country, the last
2930 Organisation attribute may have the type omitted.
2932 4. All attributes of type OrganisationalUnit may have the type
2933 omitted, unless they are after an Organisation attribute or
2934 the first attribute is of type OrganisationalUnit.
2936 * this should be the pedantic implementation.
2938 * Here the standard implementation reflects
2939 * the one historically provided by OpenLDAP
2940 * (and UMIch, I presume), with the variant
2941 * of spaces and plusses (' + ') separating
2944 * A non-standard but nice implementation could
2945 * be to turn the final "dc" attributes into a
2946 * dot-separated domain.
2948 * Other improvements could involve the use of
2949 * friendly country names and so.
2952 int leftmost_dc = -1;
2954 #endif /* DC_IN_UFN */
2956 for ( iRDN = 0, len = 0; dn[ iRDN ]; iRDN++ ) {
2958 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
2960 if ( rdn2UFNstrlen( rdn, flags, &rdnl ) ) {
2961 goto return_results;
2966 if ( LDAP_DN_IS_RDN_DC( rdn ) ) {
2967 if ( leftmost_dc == -1 ) {
2973 #endif /* DC_IN_UFN */
2976 if ( ( bv->bv_val = LDAP_MALLOC( len + 1 ) ) == NULL ) {
2977 rc = LDAP_NO_MEMORY;
2982 if ( leftmost_dc == -1 ) {
2983 #endif /* DC_IN_UFN */
2984 for ( l = 0, iRDN = 0; dn[ iRDN ]; iRDN++ ) {
2986 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
2988 if ( rdn2UFNstr( rdn, &bv->bv_val[ l ],
2990 LDAP_FREE( bv->bv_val );
2992 goto return_results;
2998 * trim the last ', ' (the allocated memory
2999 * is two bytes longer than required)
3001 bv->bv_len = len - 2;
3002 bv->bv_val[ bv->bv_len ] = '\0';
3005 last_iRDN = iRDN - 1;
3007 for ( l = 0, iRDN = 0; iRDN < leftmost_dc; iRDN++ ) {
3009 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
3011 if ( rdn2UFNstr( rdn, &bv->bv_val[ l ],
3013 LDAP_FREE( bv->bv_val );
3015 goto return_results;
3020 if ( !dn2domain( dn, bv, l, &last_iRDN ) ) {
3021 LDAP_FREE( bv->bv_val );
3023 goto return_results;
3026 /* the string is correctly terminated by dn2domain */
3028 #endif /* DC_IN_UFN */
3034 case LDAP_DN_FORMAT_DCE:
3036 for ( iRDN = 0, len = 0; dn[ iRDN ]; iRDN++ ) {
3038 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
3040 if ( rdn2DCEstrlen( rdn, flags, &rdnl ) ) {
3041 goto return_results;
3047 if ( ( bv->bv_val = LDAP_MALLOC( len + 1 ) ) == NULL ) {
3048 rc = LDAP_NO_MEMORY;
3052 for ( l = 0; iRDN--; ) {
3054 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
3056 if ( rdn2DCEstr( rdn, &bv->bv_val[ l ], flags,
3058 LDAP_FREE( bv->bv_val );
3060 goto return_results;
3068 bv->bv_val[ bv->bv_len ] = '\0';
3073 case LDAP_DN_FORMAT_AD_CANONICAL: {
3076 * Sort of UFN for DCE DNs: a slash ('/') separated
3077 * global->local DN with no types; strictly speaking,
3078 * the naming context should be a domain, which is
3079 * written in DNS-style, e.g. dot-deparated.
3083 * "givenName=Bill+sn=Gates,ou=People,dc=microsoft,dc=com"
3087 * "microsoft.com/People/Bill,Gates"
3089 for ( iRDN = 0, len = -1; dn[ iRDN ]; iRDN++ ) {
3091 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
3093 if ( rdn2ADstrlen( rdn, flags, &rdnl ) ) {
3094 goto return_results;
3100 if ( ( bv->bv_val = LDAP_MALLOC( len + 1 ) ) == NULL ) {
3101 rc = LDAP_NO_MEMORY;
3106 if ( iRDN && dn2domain( dn, bv, 0, &iRDN ) ) {
3107 for ( l = bv->bv_len; iRDN >= 0 ; iRDN-- ) {
3109 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
3111 if ( rdn2ADstr( rdn, &bv->bv_val[ l ],
3112 flags, &rdnl, 0 ) ) {
3113 LDAP_FREE( bv->bv_val );
3115 goto return_results;
3124 * Strictly speaking, AD canonical requires
3125 * a DN to be in the form "..., dc=smtg",
3126 * i.e. terminated by a domain component
3128 if ( flags & LDAP_DN_PEDANTIC ) {
3129 LDAP_FREE( bv->bv_val );
3131 rc = LDAP_INVALID_DN_SYNTAX;
3135 for ( l = 0; iRDN >= 0 ; iRDN-- ) {
3137 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
3139 if ( rdn2ADstr( rdn, &bv->bv_val[ l ],
3140 flags, &rdnl, first ) ) {
3141 LDAP_FREE( bv->bv_val );
3143 goto return_results;
3153 bv->bv_val[ bv->bv_len ] = '\0';
3160 return( LDAP_INVALID_DN_SYNTAX );
3164 Debug( LDAP_DEBUG_TRACE, "<= ldap_dn2bv(%s,%u)=%d\n", bv->bv_val, flags, rc );