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>
20 #include <ac/socket.h>
21 #include <ac/string.h>
26 /* forces the use of new dn parsing routines */
27 /* #define USE_LDAP_DN_PARSING */
28 /* extension to UFN that turns trailing "dc=value" rdns in DNS style,
29 * e.g. "ou=People,dc=openldap,dc=org" => "People, openldap.org" */
32 static char *dn2dn( const char *dnin, unsigned fin, unsigned fout );
34 /* from libraries/libldap/schema.c */
35 extern char * parse_numericoid(const char **sp, int *code, const int flags);
37 /* parsing/printing routines */
38 static int str2strval( const char *str, struct berval **val,
39 const char **next, unsigned flags, unsigned *retFlags );
40 static int DCE2strval( const char *str, struct berval **val,
41 const char **next, unsigned flags );
42 static int IA52strval( const char *str, struct berval **val,
43 const char **next, unsigned flags );
44 static int quotedIA52strval( const char *str, struct berval **val,
45 const char **next, unsigned flags );
46 static int hexstr2binval( const char *str, struct berval **val,
47 const char **next, unsigned flags );
48 static int hexstr2bin( const char *str, char *c );
49 static int byte2hexpair( const char *val, char *pair );
50 static int binval2hexstr( struct berval *val, char *str );
51 static int strval2strlen( struct berval *val, unsigned flags,
53 static int strval2str( struct berval *val, char *str, unsigned flags,
55 static int strval2IA5strlen( struct berval *val, unsigned flags,
57 static int strval2IA5str( struct berval *val, char *str, unsigned flags,
59 static int strval2DCEstrlen( struct berval *val, unsigned flags,
61 static int strval2DCEstr( struct berval *val, char *str, unsigned flags,
63 static int strval2ADstrlen( struct berval *val, unsigned flags,
65 static int strval2ADstr( struct berval *val, char *str, unsigned flags,
67 static int dn2domain( LDAPDN *dn, char *str, int *iRDN );
70 LDAPAVA * ldapava_new( const char *attr, const struct berval *val,
72 void ldapava_free( LDAPAVA *ava );
73 LDAPRDN * ldapava_append_to_rdn( LDAPRDN *rdn, LDAPAVA *ava );
74 LDAPRDN * ldapava_insert_into_rdn( LDAPRDN *rdn, LDAPAVA *ava, unsigned where );
75 /* void ldapava_free_rdn( LDAPRDN *rdn ); in ldap.h */
76 LDAPDN * ldapava_append_to_dn( LDAPDN *dn, LDAPRDN *rdn );
77 LDAPDN * ldapava_insert_into_dn( LDAPDN *dn, LDAPRDN *rdn, unsigned where );
78 /* void ldapava_free_dn( LDAPDN *dn ); in ldap.h */
80 /* Higher level helpers */
81 static int rdn2strlen( LDAPRDN *rdn, ber_len_t *len,
82 int ( *s2l )( struct berval *, unsigned, ber_len_t * ) );
83 static int rdn2str( LDAPRDN *rdn, char *str, ber_len_t *len,
84 int ( *s2s )( struct berval *, char *, unsigned, ber_len_t * ));
85 static int rdn2UFNstrlen( LDAPRDN *rdn, ber_len_t *len );
86 static int rdn2UFNstr( LDAPRDN *rdn, char *str, ber_len_t *len );
87 static int rdn2DCEstrlen( LDAPRDN *rdn, ber_len_t *len );
88 static int rdn2DCEstr( LDAPRDN *rdn, char *str, ber_len_t *len, int first );
89 static int rdn2ADstrlen( LDAPRDN *rdn, ber_len_t *len );
90 static int rdn2ADstr( LDAPRDN *rdn, char *str, ber_len_t *len, int first );
92 #ifndef USE_LDAP_DN_PARSING /* deprecated */
93 #define NAME_TYPE_LDAP_RDN 0
94 #define NAME_TYPE_LDAP_DN 1
95 #define NAME_TYPE_DCE_DN 2
97 static char **explode_name( const char *name, int notypes, int is_type );
98 #endif /* !USE_LDAP_DN_PARSING */
101 * RFC 1823 ldap_get_dn
104 ldap_get_dn( LDAP *ld, LDAPMessage *entry )
109 Debug( LDAP_DEBUG_TRACE, "ldap_get_dn\n", 0, 0, 0 );
111 if ( entry == NULL ) {
112 ld->ld_errno = LDAP_PARAM_ERROR;
116 tmp = *entry->lm_ber; /* struct copy */
117 if ( ber_scanf( &tmp, "{a" /*}*/, &dn ) == LBER_ERROR ) {
118 ld->ld_errno = LDAP_DECODING_ERROR;
126 * RFC 1823 ldap_dn2ufn
129 ldap_dn2ufn( LDAP_CONST char *dn )
131 #ifndef USE_LDAP_DN_PARSING /* deprecated */
136 Debug( LDAP_DEBUG_TRACE, "ldap_dn2ufn\n", 0, 0, 0 );
138 /* produces completely untyped UFNs */
144 vals = ldap_explode_dn( dn , 0 );
149 for ( i = 0; vals[i]; i++ ) {
152 rvals = ldap_explode_rdn( vals[i] , 1 );
153 if ( rvals == NULL ) {
158 LDAP_FREE( vals[i] );
159 vals[i] = ldap_charray2str( rvals, " + " );
163 ufn = ldap_charray2str( vals, ", " );
167 #else /* USE_LDAP_DN_PARSING */
168 Debug( LDAP_DEBUG_TRACE, "ldap_dn2ufn\n", 0, 0, 0 );
170 return dn2dn( dn, LDAP_DN_FORMAT_LDAPV3, LDAP_DN_FORMAT_UFN );
171 #endif /* USE_LDAP_DN_PARSING */
175 * RFC 1823 ldap_explode_dn
178 ldap_explode_dn( LDAP_CONST char *dn, int notypes )
180 #ifndef USE_LDAP_DN_PARSING /* deprecated */
181 Debug( LDAP_DEBUG_TRACE, "ldap_explode_dn\n", 0, 0, 0 );
183 return explode_name( dn, notypes, NAME_TYPE_LDAP_DN );
184 #else /* USE_LDAP_DN_PARSING */
186 char **values = NULL;
188 unsigned flag = notypes ? LDAP_DN_FORMAT_UFN : LDAP_DN_FORMAT_LDAPV3;
190 Debug( LDAP_DEBUG_TRACE, "ldap_explode_dn\n", 0, 0, 0 );
192 if ( ldap_str2dn( dn, &tmpDN, LDAP_DN_FORMAT_LDAPV3 )
197 for ( iRDN = 0; tmpDN[ iRDN ]; iRDN++ ) {
198 char *str, **v = NULL;
200 ldap_rdn2str( tmpDN[ iRDN ][ 0 ], &str, flag );
202 v = LDAP_REALLOC( values, sizeof( char * ) * ( 2 + iRDN ) );
204 LBER_VFREE( values );
205 ldapava_free_dn( tmpDN );
209 values[ iRDN ] = str;
211 values[ iRDN ] = NULL;
214 #endif /* USE_LDAP_DN_PARSING */
218 ldap_explode_rdn( LDAP_CONST char *rdn, int notypes )
220 #ifndef USE_LDAP_DN_PARSING /* deprecated */
221 Debug( LDAP_DEBUG_TRACE, "ldap_explode_rdn\n", 0, 0, 0 );
223 return explode_name( rdn, notypes, NAME_TYPE_LDAP_RDN );
224 #else /* USE_LDAP_DN_PARSING */
226 char **values = NULL;
228 unsigned flag = notypes ? LDAP_DN_FORMAT_UFN : LDAP_DN_FORMAT_LDAPV3;
230 Debug( LDAP_DEBUG_TRACE, "ldap_explode_rdn\n", 0, 0, 0 );
233 * we assume this dn is made of one rdn only
235 if ( ldap_str2dn( rdn, &tmpDN, LDAP_DN_FORMAT_LDAPV3 )
240 for ( iAVA = 0; tmpDN[ 0 ][ 0 ][ iAVA ]; iAVA++ ) {
241 ber_len_t l = 0, vl, al = 0;
242 char *str, **v = NULL;
243 LDAPAVA *ava = tmpDN[ 0 ][ 0 ][ iAVA ][ 0 ];
245 v = LDAP_REALLOC( values, sizeof( char * ) * ( 2 + iAVA ) );
251 if ( ava->la_flags == LDAP_AVA_BINARY ) {
252 vl = 1 + 2 * ava->la_value->bv_len;
257 if ( strval2strlen( ava->la_value,
258 ava->la_flags, &vl ) ) {
264 al = strlen( ava->la_attr );
268 str = LDAP_MALLOC( l + 1 );
269 AC_MEMCPY( str, ava->la_attr, al );
274 str = LDAP_MALLOC( l + 1 );
277 if ( ava->la_flags == LDAP_AVA_BINARY ) {
279 if ( binval2hexstr( ava->la_value, &str[ al ] ) ) {
284 if ( strval2str( ava->la_value, &str[ al ],
285 ava->la_flags, &vl ) ) {
291 values[ iAVA ] = str;
293 values[ iAVA ] = NULL;
295 ldapava_free_dn( tmpDN );
300 LBER_VFREE( values );
301 ldapava_free_dn( tmpDN );
303 #endif /* USE_LDAP_DN_PARSING */
307 ldap_dn2dcedn( LDAP_CONST char *dn )
309 #ifndef USE_LDAP_DN_PARSING /* deprecated */
310 char *dce, *q, **rdns, **p;
313 Debug( LDAP_DEBUG_TRACE, "ldap_dn2dcedn\n", 0, 0, 0 );
315 rdns = explode_name( dn, 0, NAME_TYPE_LDAP_DN );
316 if ( rdns == NULL ) {
320 for ( p = rdns; *p != NULL; p++ ) {
321 len += strlen( *p ) + 1;
324 q = dce = LDAP_MALLOC( len + 1 );
329 p--; /* get back past NULL */
331 for ( ; p != rdns; p-- ) {
343 #else /* USE_LDAP_DN_PARSING */
344 Debug( LDAP_DEBUG_TRACE, "ldap_dn2dcedn\n", 0, 0, 0 );
346 return dn2dn( dn, LDAP_DN_FORMAT_LDAPV3, LDAP_DN_FORMAT_DCE );
347 #endif /* USE_LDAP_DN_PARSING */
351 ldap_dcedn2dn( LDAP_CONST char *dce )
353 #ifndef USE_LDAP_DN_PARSING
354 char *dn, *q, **rdns, **p;
357 Debug( LDAP_DEBUG_TRACE, "ldap_dcedn2dn\n", 0, 0, 0 );
359 rdns = explode_name( dce, 0, NAME_TYPE_DCE_DN );
360 if ( rdns == NULL ) {
366 for ( p = rdns; *p != NULL; p++ ) {
367 len += strlen( *p ) + 1;
370 q = dn = LDAP_MALLOC( len );
377 for ( ; p != rdns; p-- ) {
385 /* the name was fully qualified, thus the most-significant
386 * RDN was empty. trash the last comma */
390 /* the name was relative. copy the most significant RDN */
395 #else /* USE_LDAP_DN_PARSING */
396 Debug( LDAP_DEBUG_TRACE, "ldap_dcedn2dn\n", 0, 0, 0 );
398 return dn2dn( dce, LDAP_DN_FORMAT_DCE, LDAP_DN_FORMAT_LDAPV3 );
399 #endif /* USE_LDAP_DN_PARSING */
403 ldap_dn2ad_canonical( LDAP_CONST char *dn )
405 Debug( LDAP_DEBUG_TRACE, "ldap_dn2ad_canonical\n", 0, 0, 0 );
407 return dn2dn( dn, LDAP_DN_FORMAT_LDAPV3, LDAP_DN_FORMAT_AD_CANONICAL );
410 #ifndef USE_LDAP_DN_PARSING /* deprecated */
415 explode_name( const char *name, int notypes, int is_type )
417 const char *p, *q, *rdn;
419 int offset, state, have_equals, count = 0, endquote, len;
422 if(name == NULL) name = "";
424 /* skip leading whitespace */
425 while( ldap_utf8_isspace( name )) {
426 LDAP_UTF8_INCR( name );
441 if ( p[1] != '\0' ) {
442 offset = LDAP_UTF8_OFFSET(++p);
446 if ( state == INQUOTE )
452 if( state == OUTQUOTE ) have_equals++;
455 if (is_type == NAME_TYPE_LDAP_RDN)
459 if (is_type == NAME_TYPE_DCE_DN)
464 if (is_type == NAME_TYPE_LDAP_DN)
469 if ( state == OUTQUOTE ) {
473 if ( parts == NULL ) {
474 if (( parts = (char **)LDAP_MALLOC( 8
475 * sizeof( char *))) == NULL )
477 } else if ( count >= 8 ) {
478 if (( parts = (char **)LDAP_REALLOC( parts,
479 (count+1) * sizeof( char *)))
484 parts[ count ] = NULL;
488 for ( q = rdn; q < p && *q != '='; ++q ) {
500 if ( p[-1] == '"' ) {
508 if (( parts[ count-1 ] = (char *)LDAP_CALLOC( 1,
511 AC_MEMCPY( parts[ count-1 ], rdn, len );
514 /* skip trailing spaces */
515 while( len > 0 && ldap_utf8_isspace(
516 &parts[count-1][len-1] ) )
522 parts[ count-1 ][ len ] = '\0';
526 * Don't forget to increment 'p' back to where
527 * it should be. If we don't, then we will
528 * never get past an "end quote."
533 rdn = *p ? &p[1] : p;
534 while ( ldap_utf8_isspace( rdn ) )
542 #endif /* !USE_LDAP_DN_PARSING */
545 * helper that changes the string representation of dnin
546 * from ( fin & LDAP_DN_FORMAT_MASK ) to ( fout & LDAP_DN_FORMAT_MASK )
549 * LDAP_DN_FORMAT_LDAPV3 (rfc 2253 and ldapbis)
550 * LDAP_DN_FORMAT_LDAPV2 (rfc 1779)
551 * LDAP_DN_FORMAT_DCE (?)
553 * fout can be any of the above plus:
554 * LDAP_DN_FORMAT_UFN (rfc 1781, partial and with extensions)
555 * LDAP_DN_FORMAT_AD_CANONICAL (?)
558 dn2dn( const char *dnin, unsigned fin, unsigned fout )
561 LDAPDN *tmpDN = NULL;
567 if ( ldap_str2dn( dnin , &tmpDN, fin ) != LDAP_SUCCESS ) {
571 /* don't care about the result ... */
572 ldap_dn2str( tmpDN, &dnout, fout );
574 ldapava_free_dn( tmpDN );
582 #define B4ATTRTYPE 0x0001
583 #define B4OIDATTRTYPE 0x0002
584 #define B4STRINGATTRTYPE 0x0003
586 #define B4AVAEQUALS 0x0100
587 #define B4AVASEP 0x0200
588 #define B4RDNSEP 0x0300
589 #define GOTAVA 0x0400
591 #define B4ATTRVALUE 0x0010
592 #define B4STRINGVALUE 0x0020
593 #define B4IA5VALUEQUOTED 0x0030
594 #define B4IA5VALUE 0x0040
595 #define B4BINARYVALUE 0x0050
597 /* Helpers (mostly from slapd.h; maybe it should be rewritten from this) */
598 #define LDAP_DN_ASCII_SPACE(c) \
599 ( (c) == ' ' || (c) == '\t' || (c) == '\n' || (c) == '\r' )
600 #define LDAP_DN_ASCII_LOWER(c) ( (c) >= 'a' && (c) <= 'z' )
601 #define LDAP_DN_ASCII_UPPER(c) ( (c) >= 'A' && (c) <= 'Z' )
602 #define LDAP_DN_ASCII_ALPHA(c) \
603 ( LDAP_DN_ASCII_LOWER(c) || LDAP_DN_ASCII_UPPER(c) )
604 #define LDAP_DN_ASCII_DIGIT(c) ( (c) >= '0' && (c) <= '9' )
605 #define LDAP_DN_ASCII_LCASE_HEXALPHA(c) ( (c) >= 'a' && (c) <= 'f' )
606 #define LDAP_DN_ASCII_UCASE_HEXALPHA(c) ( (c) >= 'A' && (c) <= 'F' )
607 #define LDAP_DN_ASCII_HEXDIGIT(c) \
608 ( LDAP_DN_ASCII_DIGIT(c) \
609 || LDAP_DN_ASCII_LCASE_HEXALPHA(c) \
610 || LDAP_DN_ASCII_UCASE_HEXALPHA(c) )
611 #define LDAP_DN_ASCII_ALNUM(c) \
612 ( LDAP_DN_ASCII_ALPHA(c) || LDAP_DN_ASCII_DIGIT(c) )
613 #define LDAP_DN_ASCII_PRINTABLE(c) ( (c) >= ' ' && (c) <= '~' )
616 #define LDAP_DN_OID_LEADCHAR(c) ( LDAP_DN_ASCII_DIGIT(c) )
617 #define LDAP_DN_DESC_LEADCHAR(c) ( LDAP_DN_ASCII_ALPHA(c) )
618 #define LDAP_DN_DESC_CHAR(c) ( LDAP_DN_ASCII_ALNUM(c) || (c) == '-' )
619 #define LDAP_DN_LANG_SEP(c) ( (c) == ';' )
620 #define LDAP_DN_ATTRDESC_CHAR(c) \
621 ( LDAP_DN_DESC_CHAR(c) || LDAP_DN_LANG_SEP(c) )
623 /* special symbols */
624 #define LDAP_DN_AVA_EQUALS(c) ( (c) == '=' )
625 #define LDAP_DN_AVA_SEP(c) ( (c) == '+' )
626 #define LDAP_DN_RDN_SEP(c) ( (c) == ',' )
627 #define LDAP_DN_RDN_SEP_V2(c) ( LDAP_DN_RDN_SEP(c) || (c) == ';' )
628 #define LDAP_DN_OCTOTHORPE(c) ( (c) == '#' )
629 #define LDAP_DN_QUOTES(c) ( (c) == '\"' )
630 #define LDAP_DN_ESCAPE(c) ( (c) == '\\' )
631 #define LDAP_DN_VALUE_END(c) \
632 ( LDAP_DN_RDN_SEP(c) || LDAP_DN_AVA_SEP(c) )
633 #define LDAP_DN_NE(c) \
634 ( LDAP_DN_RDN_SEP_V2(c) || LDAP_DN_AVA_SEP(c) \
635 || LDAP_DN_QUOTES(c) || (c) == '<' || (c) == '>' )
636 #define LDAP_DN_NEEDESCAPE(c) \
637 ( LDAP_DN_ESCAPE(c) || LDAP_DN_NE(c) )
638 #define LDAP_DN_NEEDESCAPE_LEAD(c) \
639 ( LDAP_DN_ASCII_SPACE(c) || LDAP_DN_OCTOTHORPE(c) || LDAP_DN_NE(c) )
640 #define LDAP_DN_NEEDESCAPE_TRAIL(c) \
641 ( LDAP_DN_ASCII_SPACE(c) || LDAP_DN_NEEDESCAPE(c) )
644 #define LDAP_DN_VALUE_END_V2(c) \
645 ( LDAP_DN_RDN_SEP_V2(c) || LDAP_DN_AVA_SEP(c) )
647 #define LDAP_DN_V2_SPECIAL(c) \
648 ( LDAP_DN_RDN_SEP_V2(c) || LDAP_DN_AVA_EQUALS(c) \
649 || LDAP_DN_AVA_SEP(c) || (c) == '<' || (c) == '>' \
650 || LDAP_DN_OCTOTHORPE(c) )
651 #define LDAP_DN_V2_PAIR(c) \
652 ( LDAP_DN_V2_SPECIAL(c) || LDAP_DN_ESCAPE(c) || LDAP_DN_QUOTES(c) )
655 * DCE (mostly from Luke Howard and IBM implementation for AIX)
657 * From: "Application Development Guide - Directory Services" (FIXME: add link?)
658 * Here escapes and valid chars for GDS are considered; as soon as more
659 * specific info is found, the macros will be updated.
661 * Chars: 'a'-'z', 'A'-'Z', '0'-'9',
662 * '.', ':', ',', ''', '+', '-', '=', '(', ')', '?', '/', ' '.
664 * Metachars: '/', ',', '=', '\'.
666 * the '\' is used to escape other metachars.
672 * Attribute types must start with alphabetic chars and can contain
673 * alphabetic chars and digits (FIXME: no '-'?). OIDs are allowed.
675 #define LDAP_DN_RDN_SEP_DCE(c) ( (c) == '/' )
676 #define LDAP_DN_AVA_SEP_DCE(c) ( (c) == ',' )
677 #define LDAP_DN_ESCAPE_DCE(c) ( LDAP_DN_ESCAPE(c) )
678 #define LDAP_DN_VALUE_END_DCE(c) \
679 ( LDAP_DN_RDN_SEP_DCE(c) || LDAP_DN_AVA_SEP_DCE(c) )
680 #define LDAP_DN_NEEDESCAPE_DCE(c) \
681 ( LDAP_DN_VALUE_END_DCE(c) || LDAP_DN_AVA_EQUALS(c) )
684 #define LDAP_DN_RDN_SEP_AD(c) ( (c) == '/' )
685 #define LDAP_DN_ESCAPE_AD(c) ( LDAP_DN_ESCAPE(c) )
686 #define LDAP_DN_AVA_SEP_AD(c) ( (c) == ',' ) /* assume same as DCE */
687 #define LDAP_DN_VALUE_END_AD(c) \
688 ( LDAP_DN_RDN_SEP_AD(c) || LDAP_DN_AVA_SEP_AD(c) )
689 #define LDAP_DN_NEEDESCAPE_AD(c) \
690 ( LDAP_DN_VALUE_END_AD(c) || LDAP_DN_AVA_EQUALS(c) )
693 #define LDAP_DN_HEXPAIR(s) \
694 ( LDAP_DN_ASCII_HEXDIGIT((s)[0]) && LDAP_DN_ASCII_HEXDIGIT((s)[1]) )
695 #define LDAP_DC_ATTR "dc"
696 /* better look at the AttributeDescription? */
698 /* FIXME: no composite rdn or non-"dc" types, right? */
699 /* FIXME: we do not allow binary values in domain, right? */
700 #define LDAP_DN_IS_RDN_DC( rdn ) \
701 ( ( rdn ) && ( rdn )[ 0 ][ 0 ] && !( rdn )[ 1 ] \
702 && ( ( rdn )[ 0 ][ 0 ]->la_flags == LDAP_AVA_STRING ) \
703 && ! strcasecmp( ( rdn )[ 0 ][ 0 ]->la_attr, LDAP_DC_ATTR ) )
705 /* Composite rules */
706 #define LDAP_DN_ALLOW_ONE_SPACE(f) \
707 ( ( (f) & LDAP_DN_FORMAT_LDAPV2 ) \
708 || !( (f) & LDAP_DN_P_NOSPACEAFTERRDN ) )
709 #define LDAP_DN_ALLOW_SPACES(f) \
710 ( ( (f) & LDAP_DN_FORMAT_LDAPV2 ) \
711 || !( (f) & ( LDAP_DN_P_NOLEADTRAILSPACES | LDAP_DN_P_NOSPACEAFTERRDN ) ) )
712 #define LDAP_DN_LDAPV3(f) \
713 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_LDAPV3 )
714 #define LDAP_DN_LDAPV2(f) \
715 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_LDAPV2 )
716 #define LDAP_DN_DCE(f) \
717 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_DCE )
718 #define LDAP_DN_UFN(f) \
719 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_UFN )
720 #define LDAP_DN_ADC(f) \
721 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_AD_CANONICAL )
722 #define LDAP_DN_FORMAT(f) ( (f) & LDAP_DN_FORMAT_MASK )
725 * LDAPAVA helpers (will become part of the API for operations
726 * on structural representations of DNs).
729 ldapava_new( const char *attr, const struct berval *val, unsigned flags )
736 ava = LDAP_MALLOC( sizeof( LDAPAVA ) );
738 /* should we test it? */
743 ava->la_attr = ( char * )attr;
744 ava->la_value = ( struct berval * )val;
745 ava->la_flags = flags;
751 ldapava_free( LDAPAVA *ava )
755 LDAP_FREE( ava->la_attr );
756 ber_bvfree( ava->la_value );
762 ldapava_append_to_rdn( LDAPRDN *rdn, LDAPAVA *ava )
770 for ( i = 0U; rdn[ i ]; i++ ) {
774 newRDN = LDAP_REALLOC( rdn, ( i + 2 ) * sizeof( LDAPAVA ** ) );
775 newRDN[ i ] = LDAP_MALLOC( sizeof( LDAPAVA * ) );
776 newRDN[ i ][ 0 ] = ava;
777 newRDN[ i + 1 ] = NULL;
783 ldapava_insert_into_rdn( LDAPRDN *rdn, LDAPAVA *ava, unsigned where )
791 for ( i = 0U; rdn[ i ]; i++ ) {
797 /* assume "at end", which corresponds to
798 * ldapava_append_to_rdn */
801 newRDN = LDAP_REALLOC( rdn, ( i + 2 ) * sizeof( LDAPAVA ** ) );
803 /* data after insert point */
804 AC_MEMCPY( &newRDN[ where + 1 ], &newRDN[ where ],
805 ( i - where ) * sizeof( LDAPRDN * ) );
807 newRDN[ where ] = LDAP_MALLOC( sizeof( LDAPAVA * ) );
808 newRDN[ where ][ 0 ] = ava;
809 newRDN[ i + 1 ] = NULL;
815 ldapava_free_rdn( LDAPRDN *rdn )
823 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
824 assert( rdn[ iAVA ][ 0 ] );
826 ldapava_free( rdn[ iAVA ][ 0 ] );
833 ldapava_append_to_dn( LDAPDN *dn, LDAPRDN *rdn )
841 for ( i = 0U; dn[ i ]; i++ ) {
845 newDN = LDAP_REALLOC( dn, ( i + 2 ) * sizeof( LDAPRDN ** ) );
846 newDN[ i ] = LDAP_MALLOC( sizeof( LDAPRDN * ) );
847 newDN[ i ][ 0 ] = rdn;
848 newDN[ i + 1 ] = NULL;
854 ldapava_insert_into_dn( LDAPDN *dn, LDAPRDN *rdn, unsigned where )
862 for ( i = 0U; dn[ i ]; i++ ) {
868 /* assume "at end", which corresponds to
869 * ldapava_append_to_dn */
872 newDN = LDAP_REALLOC( dn, ( i + 2 ) * sizeof( LDAPRDN ** ) );
874 /* data after insert point */
875 AC_MEMCPY( &newDN[ where + 1 ], &newDN[ where ],
876 ( i - where ) * sizeof( LDAPDN * ) );
878 newDN[ where ] = LDAP_MALLOC( sizeof( LDAPRDN * ) );
879 newDN[ where ][ 0 ] = rdn;
880 newDN[ i + 1 ] = NULL;
886 ldapava_free_dn( LDAPDN *dn )
894 for ( iRDN = 0; dn[ iRDN ]; iRDN++ ) {
895 assert( dn[ iRDN ][ 0 ] );
897 ldapava_free_rdn( dn[ iRDN ][ 0 ] );
904 * Converts a string representation of a DN (in LDAPv3, LDAPv2 or DCE)
905 * into a structured representation of the DN, by separating attribute
906 * types and values encoded in the more appropriate form, which is
907 * string or OID for attribute types and binary form of the BER encoded
908 * value or Unicode string. Formats different from LDAPv3 are parsed
909 * according to their own rules and turned into the more appropriate
910 * form according to LDAPv3.
912 * NOTE: I realize the code is getting spaghettish; it is rather
913 * experimental and will hopefully turn into something more simple
914 * and readable as soon as it works as expected.
918 ldap_str2dn( const char *str, LDAPDN **dn, unsigned flags )
922 int rc = LDAP_INVALID_DN_SYNTAX;
923 int attrTypeEncoding, attrValueEncoding;
925 char *attrType = NULL;
926 struct berval *attrValue = NULL;
928 LDAPDN *newDN = NULL;
929 LDAPRDN *newRDN = NULL;
934 Debug( LDAP_DEBUG_TRACE, "=> ldap_str2dn(%s,%u)\n%s", str, flags, "" );
938 switch ( LDAP_DN_FORMAT( flags ) ) {
939 case LDAP_DN_FORMAT_LDAPV3:
940 case LDAP_DN_FORMAT_LDAPV2:
941 case LDAP_DN_FORMAT_DCE:
944 /* unsupported in str2dn */
945 case LDAP_DN_FORMAT_UFN:
946 case LDAP_DN_FORMAT_AD_CANONICAL:
947 return( LDAP_INVALID_DN_SYNTAX );
950 return( LDAP_OTHER );
953 if ( str[ 0 ] == '\0' ) {
954 return( LDAP_SUCCESS );
958 if ( LDAP_DN_DCE( flags ) ) {
961 * (from Luke Howard: thnx) A RDN separator is required
962 * at the beginning of an (absolute) DN.
964 if ( !LDAP_DN_RDN_SEP_DCE( p[ 0 ] ) ) {
970 for ( ; p[ 0 ]; p++ ) {
974 rc = ldap_str2rdn( p, &newRDN, &p, flags );
975 if ( rc != LDAP_SUCCESS ) {
980 * We expect a rdn separator
983 switch ( LDAP_DN_FORMAT( flags ) ) {
984 case LDAP_DN_FORMAT_LDAPV3:
985 case LDAP_DN_FORMAT_LDAPV2:
986 if ( !LDAP_DN_RDN_SEP( p[ 0 ] ) ) {
992 case LDAP_DN_FORMAT_DCE:
993 if ( !LDAP_DN_RDN_SEP_DCE( p[ 0 ] ) ) {
1002 if ( LDAP_DN_DCE( flags ) ) {
1003 /* add in reversed order */
1004 dn = ldapava_insert_into_dn( newDN, newRDN, 0 );
1006 dn = ldapava_append_to_dn( newDN, newRDN );
1010 rc = LDAP_NO_MEMORY;
1017 if ( p[ 0 ] == '\0' ) {
1020 * the DN is over, phew
1029 ldapava_free_rdn( newRDN );
1033 ldapava_free_dn( newDN );
1039 Debug( LDAP_DEBUG_TRACE, "<= ldap_str2dn(%s,%u)=%d\n", str, flags, rc );
1048 * Parses a relative DN according to flags up to a rdn separator
1049 * or to the end of str.
1050 * Returns the rdn and a pointer to the string continuation, which
1051 * corresponds to the rdn separator or to '\0' in case the string is over.
1054 ldap_str2rdn( const char *str, LDAPRDN **rdn, const char **n, unsigned flags )
1058 int rc = LDAP_INVALID_DN_SYNTAX;
1059 int attrTypeEncoding, attrValueEncoding;
1061 char *attrType = NULL;
1062 struct berval *attrValue = NULL;
1064 LDAPRDN *newRDN = NULL;
1070 Debug( LDAP_DEBUG_TRACE, "=> ldap_str2rdn(%s,%u)\n%s", str, flags, "" );
1075 switch ( LDAP_DN_FORMAT( flags ) ) {
1076 case LDAP_DN_FORMAT_LDAPV3:
1077 case LDAP_DN_FORMAT_LDAPV2:
1078 case LDAP_DN_FORMAT_DCE:
1081 /* unsupported in str2dn */
1082 case LDAP_DN_FORMAT_UFN:
1083 case LDAP_DN_FORMAT_AD_CANONICAL:
1084 return( LDAP_INVALID_DN_SYNTAX );
1087 return( LDAP_OTHER );
1090 if ( str[ 0 ] == '\0' ) {
1091 return( LDAP_SUCCESS );
1095 for ( ; p[ 0 ] || state == GOTAVA; ) {
1098 * The parser in principle advances one token a time,
1099 * or toggles state if preferable.
1104 * an AttributeType can be encoded as:
1105 * - its string representation; in detail, implementations
1106 * MUST recognize AttributeType string type names listed
1107 * in section 2.3 of draft-ietf-ldapbis-dn-XX.txt, and
1108 * MAY recognize other names.
1109 * - its numeric OID (a dotted decimal string); in detail
1110 * RFC 2253 asserts that ``Implementations MUST allow
1111 * an oid in the attribute type to be prefixed by one
1112 * of the character strings "oid." or "OID."''. As soon
1113 * as draft-ietf-ldapbis-dn-XX.txt obsoletes RFC 2253
1114 * I'm not sure whether this is required or not any
1115 * longer; to be liberal, we still implement it.
1118 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1119 if ( !LDAP_DN_ALLOW_ONE_SPACE( flags ) ) {
1126 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1127 if ( !LDAP_DN_ALLOW_SPACES( flags ) ) {
1132 /* whitespace is allowed (and trimmed) */
1134 while ( p[ 0 ] && LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1139 /* error: we expected an AVA */
1149 if ( LDAP_DN_OID_LEADCHAR( p[ 0 ] ) ) {
1150 state = B4OIDATTRTYPE;
1154 /* else must be alpha */
1155 if ( !LDAP_DN_DESC_LEADCHAR( p[ 0 ] ) ) {
1159 /* LDAPv2 "oid." prefix */
1160 if ( LDAP_DN_LDAPV2( flags ) ) {
1162 * to be overly pedantic, we only accept
1165 if ( flags & LDAP_DN_PEDANTIC ) {
1166 if ( !strncmp( p, "oid.", 4 )
1167 || !strncmp( p, "OID.", 4 ) ) {
1169 state = B4OIDATTRTYPE;
1173 if ( !strncasecmp( p, "oid.", 4 ) ) {
1175 state = B4OIDATTRTYPE;
1181 state = B4STRINGATTRTYPE;
1184 case B4OIDATTRTYPE: {
1185 int err = LDAP_SUCCESS;
1187 attrType = parse_numericoid( &p, &err, 0 );
1188 if ( attrType == NULL ) {
1191 attrTypeEncoding = LDAP_AVA_BINARY;
1193 state = B4AVAEQUALS;
1197 case B4STRINGATTRTYPE: {
1198 const char *startPos, *endPos = NULL;
1202 * the starting char has been found to be
1203 * a LDAP_DN_DESC_LEADCHAR so we don't re-check it
1204 * FIXME: DCE attr types seem to have a more
1205 * restrictive syntax
1207 for ( startPos = p++; p[ 0 ]; p++ ) {
1208 if ( LDAP_DN_DESC_CHAR( p[ 0 ] ) ) {
1212 if ( LDAP_DN_LANG_SEP( p[ 0 ] ) ) {
1215 * RFC 2253 does not explicitly
1216 * allow lang extensions to attribute
1219 if ( flags & LDAP_DN_PEDANTIC ) {
1224 * we trim ';' and following lang
1225 * and so from attribute types
1228 for ( ; LDAP_DN_ATTRDESC_CHAR( p[ 0 ] )
1229 || LDAP_DN_LANG_SEP( p[ 0 ] ); p++ ) {
1237 len = ( endPos ? endPos : p ) - startPos;
1242 assert( attrType == NULL );
1243 attrType = LDAP_STRNDUP( startPos, len );
1244 attrTypeEncoding = LDAP_AVA_STRING;
1247 * here we need to decide whether to use it as is
1248 * or turn it in OID form; as a consequence, we
1249 * need to decide whether to binary encode the value
1252 state = B4AVAEQUALS;
1257 /* spaces may not be allowed */
1258 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1259 if ( !LDAP_DN_ALLOW_SPACES( flags ) ) {
1264 for ( p++; LDAP_DN_ASCII_SPACE( p[ 0 ] ); p++ ) {
1269 /* need equal sign */
1270 if ( !LDAP_DN_AVA_EQUALS( p[ 0 ] ) ) {
1275 /* spaces may not be allowed */
1276 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1277 if ( !LDAP_DN_ALLOW_SPACES( flags ) ) {
1282 for ( p++; LDAP_DN_ASCII_SPACE( p[ 0 ] ); p++ ) {
1288 * octothorpe means a BER encoded value will follow
1289 * FIXME: I don't think DCE will allow it
1291 if ( LDAP_DN_OCTOTHORPE( p[ 0 ] ) ) {
1293 attrValueEncoding = LDAP_AVA_BINARY;
1294 state = B4BINARYVALUE;
1298 /* STRING value expected */
1301 * if we're pedantic, an attribute type in OID form
1302 * SHOULD imply a BER encoded attribute value; we
1303 * should at least issue a warning
1305 if ( ( flags & LDAP_DN_PEDANTIC )
1306 && ( attrTypeEncoding == LDAP_AVA_BINARY ) ) {
1307 /* OID attrType SHOULD use binary encoding */
1311 attrValueEncoding = LDAP_AVA_STRING;
1314 * LDAPv2 allows the attribute value to be quoted;
1315 * also, IA5 values are expected, in principle
1317 if ( LDAP_DN_LDAPV2( flags ) ) {
1318 if ( LDAP_DN_QUOTES( p[ 0 ] ) ) {
1320 state = B4IA5VALUEQUOTED;
1329 * here STRING means RFC 2253 string
1330 * FIXME: what about DCE strings?
1332 state = B4STRINGVALUE;
1336 if ( hexstr2binval( p, &attrValue, &p, flags ) ) {
1344 switch ( LDAP_DN_FORMAT( flags ) ) {
1345 case LDAP_DN_FORMAT_LDAPV3:
1346 if ( str2strval( p, &attrValue,
1348 &attrValueEncoding ) ) {
1353 case LDAP_DN_FORMAT_DCE:
1354 /* FIXME: does DCE use UTF-8? */
1355 if ( DCE2strval( p, &attrValue,
1369 if ( IA52strval( p, &attrValue, &p, flags ) ) {
1376 case B4IA5VALUEQUOTED:
1378 /* lead quote already stripped */
1379 if ( quotedIA52strval( p, &attrValue,
1393 * we accept empty values
1395 ava = ldapava_new( attrType, attrValue,
1396 attrValueEncoding );
1397 if ( ava == NULL ) {
1398 rc = LDAP_NO_MEMORY;
1402 rdn = ldapava_append_to_rdn( newRDN, ava );
1403 if ( rdn == NULL ) {
1404 rc = LDAP_NO_MEMORY;
1410 * if we got an AVA separator ('+', | ',' * for DCE )
1411 * we expect a new AVA for this RDN; otherwise
1412 * we add the RDN to the DN
1414 switch ( LDAP_DN_FORMAT( flags ) ) {
1415 case LDAP_DN_FORMAT_LDAPV3:
1416 case LDAP_DN_FORMAT_LDAPV2:
1417 if ( !LDAP_DN_AVA_SEP( p[ 0 ] ) ) {
1422 case LDAP_DN_FORMAT_DCE:
1423 if ( !LDAP_DN_AVA_SEP_DCE( p[ 0 ] ) ) {
1431 * the RDN is over, phew
1438 /* they should have been used in an AVA */
1454 /* They are set to NULL after they're used in an AVA */
1456 LDAP_FREE( attrType );
1460 ber_bvfree( attrValue );
1464 ldapava_free_rdn( newRDN );
1469 Debug( LDAP_DEBUG_TRACE, "<= ldap_str2rdn(%*s)=%d\n",
1477 * reads in a UTF-8 string value, unescaping stuff:
1478 * '\' + LDAP_DN_NEEDESCAPE(c) -> 'c'
1479 * '\' + HEXPAIR(p) -> unhex(p)
1482 str2strval( const char *str, struct berval **val, const char **next, unsigned flags, unsigned *retFlags )
1484 const char *p, *startPos, *endPos = NULL;
1485 ber_len_t len, escapes, unescapes;
1494 for ( startPos = p = str, escapes = 0, unescapes = 0; p[ 0 ]; p++ ) {
1495 if ( LDAP_DN_ESCAPE( p[ 0 ] ) ) {
1497 if ( p[ 0 ] == '\0' ) {
1500 if ( ( p == startPos + 1 && LDAP_DN_NEEDESCAPE_LEAD( p[ 0 ] ) )
1501 || ( LDAP_DN_VALUE_END( p[ 1 ] ) && LDAP_DN_NEEDESCAPE_TRAIL( p[ 0 ] ) )
1502 || LDAP_DN_NEEDESCAPE( p[ 0 ] ) ) {
1507 if ( LDAP_DN_HEXPAIR( p ) ) {
1510 hexstr2bin( p, &c );
1513 if ( !LDAP_DN_ASCII_PRINTABLE( c ) ) {
1516 * we assume the string is UTF-8
1518 *retFlags = LDAP_AVA_NONPRINTABLE;
1525 if ( LDAP_DN_PEDANTIC & flags ) {
1529 * FIXME: we allow escaping
1530 * of chars that don't need
1531 * to and do not belong to
1532 * HEXDIGITS (we also allow
1533 * single hexdigit; maybe we
1538 } else if ( LDAP_DN_VALUE_END( p[ 0 ] ) ) {
1541 } else if ( LDAP_DN_NEEDESCAPE( p[ 0 ] ) ) {
1543 * FIXME: maybe we can add
1544 * escapes if not pedantic?
1551 * we do allow unescaped spaces at the end
1552 * of the value only in non-pedantic mode
1554 if ( p > startPos + 1 && LDAP_DN_ASCII_SPACE( p[ -1 ] ) &&
1555 !LDAP_DN_ESCAPE( p[ -2 ] ) ) {
1556 if ( flags & LDAP_DN_PEDANTIC ) {
1560 /* strip trailing (unescaped) spaces */
1561 for ( endPos = p - 1;
1562 endPos > startPos + 1 &&
1563 LDAP_DN_ASCII_SPACE( endPos[ -1 ] ) &&
1564 !LDAP_DN_ESCAPE( endPos[ -2 ] );
1571 * FIXME: test memory?
1573 len = ( endPos ? endPos : p ) - startPos - escapes - unescapes;
1574 *val = LDAP_MALLOC( sizeof( struct berval ) );
1575 ( *val )->bv_len = len;
1577 if ( escapes == 0 && unescapes == 0 ) {
1578 ( *val )->bv_val = LDAP_STRNDUP( startPos, len );
1583 ( *val )->bv_val = LDAP_MALLOC( len + 1 );
1584 for ( s = 0, d = 0; d < len; ) {
1585 if ( LDAP_DN_ESCAPE( startPos[ s ] ) ) {
1587 if ( ( s == 0 && LDAP_DN_NEEDESCAPE_LEAD( startPos[ s ] ) )
1588 || ( s == len - 1 && LDAP_DN_NEEDESCAPE_TRAIL( startPos[ s ] ) )
1589 || LDAP_DN_NEEDESCAPE( startPos[ s ] ) ) {
1590 ( *val )->bv_val[ d++ ] =
1593 } else if ( LDAP_DN_HEXPAIR( &startPos[ s ] ) ) {
1596 hexstr2bin( &startPos[ s ], &c );
1597 ( *val )->bv_val[ d++ ] = c;
1602 * we allow escaping of chars
1603 * that do not need to
1605 ( *val )->bv_val[ d++ ] =
1610 ( *val )->bv_val[ d++ ] = startPos[ s++ ];
1614 ( *val )->bv_val[ d ] = '\0';
1615 assert( strlen( ( *val )->bv_val ) == len );
1625 DCE2strval( const char *str, struct berval **val, const char **next, unsigned flags )
1627 const char *p, *startPos, *endPos = NULL;
1628 ber_len_t len, escapes;
1637 for ( startPos = p = str, escapes = 0; p[ 0 ]; p++ ) {
1638 if ( LDAP_DN_ESCAPE_DCE( p[ 0 ] ) ) {
1640 if ( LDAP_DN_NEEDESCAPE_DCE( p[ 0 ] ) ) {
1647 } else if ( LDAP_DN_VALUE_END_DCE( p[ 0 ] ) ) {
1652 * FIXME: can we accept anything else? I guess we need
1653 * to stop if a value is not legal
1658 * (unescaped) trailing spaces are trimmed must be silently ignored;
1661 if ( p > startPos + 1 && LDAP_DN_ASCII_SPACE( p[ -1 ] ) &&
1662 !LDAP_DN_ESCAPE( p[ -2 ] ) ) {
1663 if ( flags & LDAP_DN_PEDANTIC ) {
1667 /* strip trailing (unescaped) spaces */
1668 for ( endPos = p - 1;
1669 endPos > startPos + 1 &&
1670 LDAP_DN_ASCII_SPACE( endPos[ -1 ] ) &&
1671 !LDAP_DN_ESCAPE( endPos[ -2 ] );
1678 len = ( endPos ? endPos : p ) - startPos - escapes;
1679 *val = LDAP_MALLOC( sizeof( struct berval ) );
1680 ( *val )->bv_len = len;
1681 if ( escapes == 0 ){
1682 ( *val )->bv_val = LDAP_STRNDUP( startPos, len );
1687 ( *val )->bv_val = LDAP_MALLOC( len + 1 );
1688 for ( s = 0, d = 0; d < len; ) {
1690 * This point is reached only if escapes
1691 * are properly used, so all we need to
1694 if ( LDAP_DN_ESCAPE_DCE( startPos[ s ] ) ) {
1698 ( *val )->bv_val[ d++ ] = startPos[ s++ ];
1700 ( *val )->bv_val[ d ] = '\0';
1701 assert( strlen( ( *val )->bv_val ) == len );
1710 IA52strval( const char *str, struct berval **val, const char **next, unsigned flags )
1712 const char *p, *startPos, *endPos = NULL;
1713 ber_len_t len, escapes;
1726 for ( startPos = p = str, escapes = 0; p[ 0 ]; p++ ) {
1727 if ( LDAP_DN_ESCAPE( p[ 0 ] ) ) {
1729 if ( p[ 0 ] == '\0' ) {
1733 if ( !LDAP_DN_NEEDESCAPE( p[ 0 ] )
1734 && ( LDAP_DN_PEDANTIC & flags ) ) {
1739 } else if ( LDAP_DN_VALUE_END_V2( p[ 0 ] ) ) {
1744 * FIXME: can we accept anything else? I guess we need
1745 * to stop if a value is not legal
1749 /* strip trailing (unescaped) spaces */
1751 endPos > startPos + 1 &&
1752 LDAP_DN_ASCII_SPACE( endPos[ -1 ] ) &&
1753 !LDAP_DN_ESCAPE( endPos[ -2 ] );
1758 *val = LDAP_MALLOC( sizeof( struct berval ) );
1759 len = ( endPos ? endPos : p ) - startPos - escapes;
1760 ( *val )->bv_len = len;
1761 if ( escapes == 0 ) {
1762 ( *val )->bv_val = LDAP_STRNDUP( startPos, len );
1767 ( *val )->bv_val = LDAP_MALLOC( len + 1 );
1768 for ( s = 0, d = 0; d < len; ) {
1769 if ( LDAP_DN_ESCAPE( startPos[ s ] ) ) {
1772 ( *val )->bv_val[ d++ ] = startPos[ s++ ];
1774 ( *val )->bv_val[ d ] = '\0';
1775 assert( strlen( ( *val )->bv_val ) == len );
1783 quotedIA52strval( const char *str, struct berval **val, const char **next, unsigned flags )
1785 const char *p, *startPos, *endPos = NULL;
1787 unsigned escapes = 0;
1796 /* initial quote already eaten */
1797 for ( startPos = p = str; p[ 0 ]; p++ ) {
1799 * According to RFC 1779, the quoted value can
1800 * contain escaped as well as unescaped special values;
1801 * as a consequence we tolerate escaped values
1802 * (e.g. '"\,"' -> '\,') and escape unescaped specials
1803 * (e.g. '","' -> '\,').
1805 if ( LDAP_DN_ESCAPE( p[ 0 ] ) ) {
1806 if ( p[ 1 ] == '\0' ) {
1811 if ( !LDAP_DN_V2_PAIR( p[ 0 ] )
1812 && ( LDAP_DN_PEDANTIC & flags ) ) {
1814 * do we allow to escape normal chars?
1815 * LDAPv2 does not allow any mechanism
1816 * for escaping chars with '\' and hex
1823 } else if ( LDAP_DN_QUOTES( p[ 0 ] ) ) {
1825 /* eat closing quotes */
1831 * FIXME: can we accept anything else? I guess we need
1832 * to stop if a value is not legal
1836 if ( endPos == NULL ) {
1840 /* Strip trailing (unescaped) spaces */
1841 for ( ; p[ 0 ] && LDAP_DN_ASCII_SPACE( p[ 0 ] ); p++ ) {
1845 len = endPos - startPos - escapes;
1847 *val = LDAP_MALLOC( sizeof( struct berval ) );
1848 ( *val )->bv_len = len;
1849 if ( escapes == 0 ) {
1850 ( *val )->bv_val = LDAP_STRNDUP( startPos, len );
1855 ( *val )->bv_val = LDAP_MALLOC( len + 1 );
1856 ( *val )->bv_len = len;
1858 for ( s = d = 0; d < len; ) {
1859 if ( LDAP_DN_ESCAPE( str[ s ] ) ) {
1862 ( *val )->bv_val[ d++ ] = str[ s++ ];
1864 ( *val )->bv_val[ d ] = '\0';
1865 assert( strlen( ( *val )->bv_val ) == len );
1874 hexstr2bin( const char *str, char *c )
1884 if ( LDAP_DN_ASCII_DIGIT( c1 ) ) {
1890 if ( LDAP_DN_ASCII_LCASE_HEXALPHA( c1 ) ) {
1897 if ( LDAP_DN_ASCII_DIGIT( c2 ) ) {
1903 if ( LDAP_DN_ASCII_LCASE_HEXALPHA( c2 ) ) {
1904 *c += c2 - 'a' + 10;
1912 hexstr2binval( const char *str, struct berval **val, const char **next, unsigned flags )
1914 const char *p, *startPos, *endPos = NULL;
1925 for ( startPos = p = str; p[ 0 ]; p += 2 ) {
1926 switch ( LDAP_DN_FORMAT( flags ) ) {
1927 case LDAP_DN_FORMAT_LDAPV3:
1928 case LDAP_DN_FORMAT_LDAPV2:
1929 if ( LDAP_DN_VALUE_END( p[ 0 ] ) ) {
1934 case LDAP_DN_FORMAT_DCE:
1935 if ( LDAP_DN_VALUE_END_DCE( p[ 0 ] ) ) {
1941 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1942 if ( flags & LDAP_DN_PEDANTIC ) {
1947 for ( ; p[ 0 ]; p++ ) {
1948 switch ( LDAP_DN_FORMAT( flags ) ) {
1949 case LDAP_DN_FORMAT_LDAPV3:
1950 case LDAP_DN_FORMAT_LDAPV2:
1951 if ( LDAP_DN_VALUE_END( p[ 0 ] ) ) {
1956 case LDAP_DN_FORMAT_DCE:
1957 if ( LDAP_DN_VALUE_END_DCE( p[ 0 ] ) ) {
1966 if ( !LDAP_DN_HEXPAIR( p ) ) {
1973 len = ( ( endPos ? endPos : p ) - startPos ) / 2;
1975 assert( 2 * len == ( endPos ? endPos : p ) - startPos );
1977 *val = LDAP_MALLOC( sizeof( struct berval ) );
1978 if ( *val == NULL ) {
1979 return( LDAP_NO_MEMORY );
1982 ( *val )->bv_len = len;
1983 ( *val )->bv_val = LDAP_MALLOC( len + 1 );
1984 if ( ( *val )->bv_val == NULL ) {
1986 return( LDAP_NO_MEMORY );
1989 for ( s = 0, d = 0; d < len; s += 2, d++ ) {
1992 hexstr2bin( &startPos[ s ], &c );
1994 ( *val )->bv_val[ d ] = c;
1997 ( *val )->bv_val[ d ] = '\0';
2004 * convert a byte in a hexadecimal pair
2007 byte2hexpair( const char *val, char *pair )
2009 static const char hexdig[] = "0123456789abcdef";
2015 * we assume the string has enough room for the hex encoding
2019 pair[ 0 ] = hexdig[ 0x0f & ( val[ 0 ] >> 4 ) ];
2020 pair[ 1 ] = hexdig[ 0x0f & val[ 0 ] ];
2026 * convert a binary value in hexadecimal pairs
2029 binval2hexstr( struct berval *val, char *str )
2036 if ( val->bv_len == 0 ) {
2041 * we assume the string has enough room for the hex encoding
2045 for ( s = 0, d = 0; s < val->bv_len; s++, d += 2 ) {
2046 byte2hexpair( &val->bv_val[ s ], &str[ d ] );
2053 * Length of the string representation, accounting for escaped hex
2057 strval2strlen( struct berval *val, unsigned flags, ber_len_t *len )
2059 ber_len_t l, cl = 1;
2066 if ( val->bv_len == 0 ) {
2070 for ( l = 0, p = val->bv_val; p[ 0 ]; p += cl ) {
2071 cl = ldap_utf8_charlen( p );
2073 /* illegal utf-8 char! */
2076 } else if ( cl > 1 ) {
2079 for ( cnt = 1; cnt < cl; cnt++ ) {
2080 if ( ( p[ cnt ] & 0x80 ) == 0x00 ) {
2084 /* need to escape it */
2087 } else if ( ( p == val->bv_val && LDAP_DN_NEEDESCAPE_LEAD( p[ 0 ] ) )
2088 || ( !p[ 1 ] && LDAP_DN_NEEDESCAPE_TRAIL( p[ 0 ] ) )
2089 || LDAP_DN_NEEDESCAPE( p[ 0 ] ) ) {
2103 * convert to string representation, escaping with hex the UTF-8 stuff;
2104 * assume the destination has enough room for escaping
2107 strval2str( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2109 ber_len_t s, d, end;
2115 if ( val->bv_len == 0 ) {
2121 * we assume the string has enough room for the hex encoding
2124 for ( s = 0, d = 0, end = val->bv_len - 1; s < val->bv_len; ) {
2125 ber_len_t cl = ldap_utf8_charlen( &val->bv_val[ s ] );
2130 byte2hexpair( &val->bv_val[ s ], &str[ d ] );
2136 if ( ( d == 0 && LDAP_DN_NEEDESCAPE_LEAD( val->bv_val[ s ] ) )
2137 || ( s == end && LDAP_DN_NEEDESCAPE_TRAIL( val->bv_val[ s ] ) )
2138 || LDAP_DN_NEEDESCAPE( val->bv_val[ s ] ) ) {
2141 str[ d++ ] = val->bv_val[ s++ ];
2151 * Length of the IA5 string representation (no UTF-8 allowed)
2154 strval2IA5strlen( struct berval *val, unsigned flags, ber_len_t *len )
2163 if ( val->bv_len == 0 ) {
2167 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2169 * Turn value into a binary encoded BER
2174 for ( l = 0, p = val->bv_val; p[ 0 ]; p++ ) {
2175 if ( ( p == val->bv_val && LDAP_DN_NEEDESCAPE_LEAD( p[ 0 ] ) )
2176 || ( !p[ 1 ] && LDAP_DN_NEEDESCAPE_TRAIL( p[ 0 ] ) )
2177 || LDAP_DN_NEEDESCAPE( p[ 0 ] ) ) {
2192 * convert to string representation (np UTF-8)
2193 * assume the destination has enough room for escaping
2196 strval2IA5str( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2198 ber_len_t s, d, end;
2204 if ( val->bv_len == 0 ) {
2209 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2211 * Turn value into a binary encoded BER
2218 * we assume the string has enough room for the hex encoding
2222 for ( s = 0, d = 0, end = val->bv_len - 1; s < val->bv_len; ) {
2223 if ( ( s == 0 && LDAP_DN_NEEDESCAPE_LEAD( val->bv_val[ s ] ) )
2224 || ( s == end && LDAP_DN_NEEDESCAPE_TRAIL( val->bv_val[ s ] ) )
2225 || LDAP_DN_NEEDESCAPE( val->bv_val[ s ] ) ) {
2228 str[ d++ ] = val->bv_val[ s++ ];
2238 * Length of the (supposedly) DCE string representation,
2239 * accounting for escaped hex of UTF-8 chars
2242 strval2DCEstrlen( struct berval *val, unsigned flags, ber_len_t *len )
2251 if ( val->bv_len == 0 ) {
2255 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2257 * FIXME: Turn the value into a binary encoded BER?
2262 for ( l = 0, p = val->bv_val; p[ 0 ]; p++ ) {
2263 if ( LDAP_DN_NEEDESCAPE_DCE( p[ 0 ] ) ) {
2278 * convert to (supposedly) DCE string representation,
2279 * escaping with hex the UTF-8 stuff;
2280 * assume the destination has enough room for escaping
2283 strval2DCEstr( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2291 if ( val->bv_len == 0 ) {
2296 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2298 * FIXME: Turn the value into a binary encoded BER?
2306 * we assume the string has enough room for the hex encoding
2310 for ( s = 0, d = 0; s < val->bv_len; ) {
2311 if ( LDAP_DN_NEEDESCAPE_DCE( val->bv_val[ s ] ) ) {
2314 str[ d++ ] = val->bv_val[ s++ ];
2324 * Length of the (supposedly) AD canonical string representation,
2325 * accounting for escaped hex of UTF-8 chars
2328 strval2ADstrlen( struct berval *val, unsigned flags, ber_len_t *len )
2337 if ( val->bv_len == 0 ) {
2341 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2343 * FIXME: Turn the value into a binary encoded BER?
2348 for ( l = 0, p = val->bv_val; p[ 0 ]; p++ ) {
2349 if ( LDAP_DN_NEEDESCAPE_AD( p[ 0 ] ) ) {
2364 * convert to (supposedly) AD string representation,
2365 * escaping with hex the UTF-8 stuff;
2366 * assume the destination has enough room for escaping
2369 strval2ADstr( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2377 if ( val->bv_len == 0 ) {
2382 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2384 * FIXME: Turn the value into a binary encoded BER?
2392 * we assume the string has enough room for the hex encoding
2396 for ( s = 0, d = 0; s < val->bv_len; ) {
2397 if ( LDAP_DN_NEEDESCAPE_AD( val->bv_val[ s ] ) ) {
2400 str[ d++ ] = val->bv_val[ s++ ];
2410 * If the DN is terminated by single-AVA RDNs with attribute type of "dc",
2411 * the forst part of the AD representation of the DN is written in DNS
2412 * form, i.e. dot separated domain name components (as suggested
2413 * by Luke Howard, http://www.padl.com/~lukeh)
2416 dn2domain( LDAPDN *dn, char *str, int *iRDN )
2419 int domain = 0, first = 1;
2420 ber_len_t l = 1; /* we move the null also */
2422 /* we are guaranteed there's enough memory in str */
2428 assert( *iRDN > 0 );
2430 for ( i = *iRDN; i >= 0; i-- ) {
2434 assert( dn[ i ][ 0 ] );
2437 assert( rdn[ 0 ][ 0 ] );
2438 ava = rdn[ 0 ][ 0 ];
2440 if ( !LDAP_DN_IS_RDN_DC( rdn ) ) {
2448 AC_MEMCPY( str, ava->la_value->bv_val,
2449 ava->la_value->bv_len + 1);
2450 l += ava->la_value->bv_len;
2453 AC_MEMCPY( str + ava->la_value->bv_len + 1, str, l);
2454 AC_MEMCPY( str, ava->la_value->bv_val,
2455 ava->la_value->bv_len );
2456 str[ ava->la_value->bv_len ] = '.';
2457 l += ava->la_value->bv_len + 1;
2467 rdn2strlen( LDAPRDN *rdn, ber_len_t *len,
2468 int ( *s2l )( struct berval *v, unsigned f, ber_len_t *l ) )
2475 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2476 LDAPAVA *ava = rdn[ iAVA ][ 0 ];
2478 /* len(type) + '=' + '+' | ',' */
2479 l += strlen( ava->la_attr ) + 2;
2481 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2482 /* octothorpe + twice the length */
2483 l += 1 + 2 * ava->la_value->bv_len;
2488 if ( ( *s2l )( ava->la_value, ava->la_flags, &vl ) ) {
2501 rdn2str( LDAPRDN *rdn, char *str, ber_len_t *len,
2502 int ( *s2s ) ( struct berval *v, char * s, unsigned f, ber_len_t *l ) )
2507 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2508 LDAPAVA *ava = rdn[ iAVA ][ 0 ];
2509 ber_len_t al = strlen( ava->la_attr );
2511 AC_MEMCPY( &str[ l ], ava->la_attr, al );
2516 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2518 if ( binval2hexstr( ava->la_value, &str[ l ] ) ) {
2521 l += 2 * ava->la_value->bv_len;
2526 if ( ( *s2s )( ava->la_value, &str[ l ],
2527 ava->la_flags, &vl ) ) {
2532 str[ l++ ] = ( rdn[ iAVA + 1 ] ? '+' : ',' );
2541 rdn2DCEstrlen( LDAPRDN *rdn, ber_len_t *len )
2548 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2549 LDAPAVA *ava = rdn[ iAVA ][ 0 ];
2551 /* len(type) + '=' + ',' | '/' */
2552 l += strlen( ava->la_attr ) + 2;
2554 switch ( ava->la_flags ) {
2555 case LDAP_AVA_BINARY:
2556 /* octothorpe + twice the length */
2557 l += 1 + 2 * ava->la_value->bv_len;
2560 case LDAP_AVA_STRING: {
2563 if ( strval2DCEstrlen( ava->la_value,
2564 ava->la_flags, &vl ) ) {
2582 rdn2DCEstr( LDAPRDN *rdn, char *str, ber_len_t *len, int first )
2587 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2588 LDAPAVA *ava = rdn[ iAVA ][ 0 ];
2589 ber_len_t al = strlen( ava->la_attr );
2594 str[ l++ ] = ( iAVA ? ',' : '/' );
2597 AC_MEMCPY( &str[ l ], ava->la_attr, al );
2602 switch ( ava->la_flags ) {
2603 case LDAP_AVA_BINARY:
2605 if ( binval2hexstr( ava->la_value, &str[ l ] ) ) {
2608 l += 2 * ava->la_value->bv_len;
2611 case LDAP_AVA_STRING: {
2614 if ( strval2DCEstr( ava->la_value, &str[ l ],
2615 ava->la_flags, &vl ) ) {
2633 rdn2UFNstrlen( LDAPRDN *rdn, ber_len_t *len )
2643 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2644 LDAPAVA *ava = rdn[ iAVA ][ 0 ];
2647 l += ( rdn[ iAVA + 1 ] ? 3 : 2 );
2649 /* FIXME: are binary values allowed in UFN? */
2650 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2651 /* octothorpe + twice the value */
2652 l += 1 + 2 * ava->la_value->bv_len;
2657 if ( strval2strlen( ava->la_value, ava->la_flags,
2671 rdn2UFNstr( LDAPRDN *rdn, char *str, ber_len_t *len )
2676 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2677 LDAPAVA *ava = rdn[ iAVA ][ 0 ];
2679 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2681 if ( binval2hexstr( ava->la_value, &str[ l ] ) ) {
2684 l += 2 * ava->la_value->bv_len;
2689 if ( strval2str( ava->la_value, &str[ l ],
2690 ava->la_flags, &vl ) ) {
2696 if ( rdn[ iAVA + 1 ]) {
2697 AC_MEMCPY( &str[ l ], " + ", 3 );
2701 AC_MEMCPY( &str[ l ], ", ", 2 );
2712 rdn2ADstrlen( LDAPRDN *rdn, ber_len_t *len )
2722 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2723 LDAPAVA *ava = rdn[ iAVA ][ 0 ];
2728 /* FIXME: are binary values allowed in UFN? */
2729 switch ( ava->la_flags ) {
2730 case LDAP_AVA_BINARY:
2731 /* octothorpe + twice the value */
2732 l += 1 + 2 * ava->la_value->bv_len;
2735 case LDAP_AVA_STRING: {
2738 if ( strval2ADstrlen( ava->la_value,
2739 ava->la_flags, &vl ) ) {
2757 rdn2ADstr( LDAPRDN *rdn, char *str, ber_len_t *len, int first )
2762 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2763 LDAPAVA *ava = rdn[ iAVA ][ 0 ];
2768 str[ l++ ] = ( iAVA ? ',' : '/' );
2771 switch ( ava->la_flags ) {
2772 case LDAP_AVA_BINARY:
2774 if ( binval2hexstr( ava->la_value, &str[ l ] ) ) {
2777 l += 2 * ava->la_value->bv_len;
2780 case LDAP_AVA_STRING: {
2783 if ( strval2str( ava->la_value, &str[ l ],
2784 ava->la_flags, &vl ) ) {
2804 * Returns in str a string representation of rdn based on flags.
2805 * There is some duplication of code between this and ldap_dn2str;
2806 * this is wanted to reduce the allocation of temporary buffers.
2809 ldap_rdn2str( LDAPRDN *rdn, char **str, unsigned flags )
2816 if ( rdn == NULL ) {
2817 *str = LDAP_STRDUP( "" );
2818 return( LDAP_SUCCESS );
2822 * This routine wastes "back" bytes at the end of the string
2826 switch ( LDAP_DN_FORMAT( flags ) ) {
2827 case LDAP_DN_FORMAT_LDAPV3:
2828 if ( rdn2strlen( rdn, &l, strval2strlen ) ) {
2829 return( LDAP_OTHER );
2833 case LDAP_DN_FORMAT_LDAPV2:
2834 if ( rdn2strlen( rdn, &l, strval2IA5strlen ) ) {
2835 return( LDAP_OTHER );
2839 case LDAP_DN_FORMAT_UFN:
2840 if ( rdn2UFNstrlen( rdn, &l ) ) {
2841 return( LDAP_OTHER );
2845 case LDAP_DN_FORMAT_DCE:
2846 if ( rdn2DCEstrlen( rdn, &l ) ) {
2847 return( LDAP_OTHER );
2851 case LDAP_DN_FORMAT_AD_CANONICAL:
2852 if ( rdn2ADstrlen( rdn, &l ) ) {
2853 return( LDAP_OTHER );
2858 return( LDAP_OTHER );
2861 *str = LDAP_MALLOC( l + 1 );
2863 switch ( LDAP_DN_FORMAT( flags ) ) {
2864 case LDAP_DN_FORMAT_LDAPV3:
2865 rc = rdn2str( rdn, *str, &l, strval2str );
2869 case LDAP_DN_FORMAT_LDAPV2:
2870 rc = rdn2str( rdn, *str, &l, strval2IA5str );
2874 case LDAP_DN_FORMAT_UFN:
2875 rc = rdn2UFNstr( rdn, *str, &l );
2879 case LDAP_DN_FORMAT_DCE:
2880 rc = rdn2DCEstr( rdn, *str, &l, 1 );
2884 case LDAP_DN_FORMAT_AD_CANONICAL:
2885 rc = rdn2ADstr( rdn, *str, &l, 1 );
2891 ldap_memfree( *str );
2892 return( LDAP_OTHER );
2895 ( *str )[ l - back ] = '\0';
2897 return( LDAP_SUCCESS );
2901 * Very bulk implementation; many optimizations can be performed
2902 * - a NULL dn results in an empty string ""
2905 * a) what do we do if a UTF-8 string must be converted in LDAPv2?
2906 * we must encode it in binary form ('#' + HEXPAIRs)
2907 * b) does DCE/AD support UTF-8?
2908 * no clue; don't think so.
2909 * c) what do we do when binary values must be converted in UTF/DCE/AD?
2910 * use binary encoded BER
2912 int ldap_dn2str( LDAPDN *dn, char **str, unsigned flags )
2915 int rc = LDAP_OTHER;
2918 /* stringifying helpers for LDAPv3/LDAPv2 */
2919 int ( *sv2l ) ( struct berval *v, unsigned f, ber_len_t *l );
2920 int ( *sv2s ) ( struct berval *v, char *s, unsigned f, ber_len_t *l );
2924 Debug( LDAP_DEBUG_TRACE, "=> ldap_dn2str(%u)\n%s%s", flags, "", "" );
2929 * a null dn means an empty dn string
2930 * FIXME: better raise an error?
2933 *str = LDAP_STRDUP( "" );
2934 return( LDAP_SUCCESS );
2937 switch ( LDAP_DN_FORMAT( flags ) ) {
2938 case LDAP_DN_FORMAT_LDAPV3:
2939 sv2l = strval2strlen;
2943 case LDAP_DN_FORMAT_LDAPV2:
2944 sv2l = strval2IA5strlen;
2945 sv2s = strval2IA5str;
2948 for ( iRDN = 0, len = 0; dn[ iRDN ]; iRDN++ ) {
2950 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
2952 if ( rdn2strlen( rdn, &rdnl, sv2l ) ) {
2953 goto return_results;
2959 if ( ( *str = LDAP_MALLOC( len + 1 ) ) == NULL ) {
2960 rc = LDAP_NO_MEMORY;
2964 for ( l = 0, iRDN = 0; dn[ iRDN ]; iRDN++ ) {
2966 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
2968 if ( rdn2str( rdn, &( *str )[ l ], &rdnl, sv2s ) ) {
2971 goto return_results;
2979 * trim the last ',' (the allocated memory
2980 * is one byte longer than required)
2982 ( *str )[ len - 1 ] = '\0';
2987 case LDAP_DN_FORMAT_UFN: {
2990 * FIXME: quoting from RFC 1781:
2992 To take a distinguished name, and generate a name of this format with
2993 attribute types omitted, the following steps are followed.
2995 1. If the first attribute is of type CommonName, the type may be
2998 2. If the last attribute is of type Country, the type may be
3001 3. If the last attribute is of type Country, the last
3002 Organisation attribute may have the type omitted.
3004 4. All attributes of type OrganisationalUnit may have the type
3005 omitted, unless they are after an Organisation attribute or
3006 the first attribute is of type OrganisationalUnit.
3008 * this should be the pedantic implementation.
3010 * Here the standard implementation reflects
3011 * the one historically provided by OpenLDAP
3012 * (and UMIch, I presume), with the variant
3013 * of spaces and plusses (' + ') separating
3016 * A non-standard but nice implementation could
3017 * be to turn the final "dc" attributes into a
3018 * dot-separated domain.
3020 * Other improvements could involve the use of
3021 * friendly country names and so.
3024 int leftmost_dc = -1;
3026 #endif /* DC_IN_UFN */
3028 for ( iRDN = 0, len = 0; dn[ iRDN ]; iRDN++ ) {
3030 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
3032 if ( rdn2UFNstrlen( rdn, &rdnl ) ) {
3033 goto return_results;
3038 if ( LDAP_DN_IS_RDN_DC( rdn ) ) {
3039 if ( leftmost_dc == -1 ) {
3045 #endif /* DC_IN_UFN */
3048 if ( ( *str = LDAP_MALLOC( len + 1 ) ) == NULL ) {
3049 rc = LDAP_NO_MEMORY;
3054 if ( leftmost_dc == -1 ) {
3055 #endif /* DC_IN_UFN */
3056 for ( l = 0, iRDN = 0; dn[ iRDN ]; iRDN++ ) {
3058 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
3060 if ( rdn2UFNstr( rdn, &( *str )[ l ], &vl ) ) {
3063 goto return_results;
3069 * trim the last ', ' (the allocated memory
3070 * is two bytes longer than required)
3072 ( *str )[ len - 2 ] = '\0';
3075 last_iRDN = iRDN - 1;
3077 for ( l = 0, iRDN = 0; iRDN < leftmost_dc; iRDN++ ) {
3079 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
3081 if ( rdn2UFNstr( rdn, &( *str )[ l ], &vl ) ) {
3084 goto return_results;
3089 if ( !dn2domain( dn, &( *str )[ l ], &last_iRDN ) ) {
3092 goto return_results;
3095 /* the string is correctly terminated by dn2domain */
3097 #endif /* DC_IN_UFN */
3103 case LDAP_DN_FORMAT_DCE:
3105 for ( iRDN = 0, len = 0; dn[ iRDN ]; iRDN++ ) {
3107 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
3109 if ( rdn2DCEstrlen( rdn, &rdnl ) ) {
3110 goto return_results;
3116 if ( ( *str = LDAP_MALLOC( len + 1 ) ) == NULL ) {
3117 rc = LDAP_NO_MEMORY;
3121 for ( l = 0; iRDN--; ) {
3123 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
3125 if ( rdn2DCEstr( rdn, &( *str )[ l ], &rdnl, 0 ) ) {
3128 goto return_results;
3135 ( *str )[ len ] = '\0';
3140 case LDAP_DN_FORMAT_AD_CANONICAL: {
3143 * Sort of UFN for DCE DNs: a slash ('/') separated
3144 * global->local DN with no types; strictly speaking,
3145 * the naming context should be a domain, which is
3146 * written in DNS-style, e.g. dot-deparated.
3150 * "cn=Bill+sn=Gates,ou=People,dc=microsoft,dc=com"
3154 * "microsoft.com/People/Bill,Gates"
3156 for ( iRDN = 0, len = -1; dn[ iRDN ]; iRDN++ ) {
3158 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
3160 if ( rdn2ADstrlen( rdn, &rdnl ) ) {
3161 goto return_results;
3167 if ( ( *str = LDAP_MALLOC( len + 1 ) ) == NULL ) {
3168 rc = LDAP_NO_MEMORY;
3173 if ( iRDN && dn2domain( dn, *str, &iRDN ) ) {
3174 for ( l = strlen( *str ); iRDN >= 0 ; iRDN-- ) {
3176 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
3178 if ( rdn2ADstr( rdn, &( *str )[ l ],
3182 goto return_results;
3191 * Strictly speaking, AD canonical requires
3192 * a DN to be in the form "..., dc=smtg",
3193 * i.e. terminated by a domain component
3195 if ( flags & LDAP_DN_PEDANTIC ) {
3198 rc = LDAP_INVALID_DN_SYNTAX;
3202 for ( l = 0; iRDN >= 0 ; iRDN-- ) {
3204 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
3206 if ( rdn2ADstr( rdn, &( *str )[ l ],
3210 goto return_results;
3219 ( *str )[ len ] = '\0';
3230 Debug( LDAP_DEBUG_TRACE, "<= ldap_dn2str(%s,%u)=%d\n", *str, flags, rc );