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 struct berval *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 l = vl + ava->la_attr->bv_len + 1;
266 str = LDAP_MALLOC( l + 1 );
267 AC_MEMCPY( str, ava->la_attr->bv_val,
268 ava->la_attr->bv_len );
269 str[ ava->la_attr->bv_len + 1 ] = '=';
273 str = LDAP_MALLOC( l + 1 );
276 if ( ava->la_flags == LDAP_AVA_BINARY ) {
278 if ( binval2hexstr( ava->la_value, &str[ al ] ) ) {
283 if ( strval2str( ava->la_value, &str[ al ],
284 ava->la_flags, &vl ) ) {
290 values[ iAVA ] = str;
292 values[ iAVA ] = NULL;
294 ldapava_free_dn( tmpDN );
299 LBER_VFREE( values );
300 ldapava_free_dn( tmpDN );
302 #endif /* USE_LDAP_DN_PARSING */
306 ldap_dn2dcedn( LDAP_CONST char *dn )
308 #ifndef USE_LDAP_DN_PARSING /* deprecated */
309 char *dce, *q, **rdns, **p;
312 Debug( LDAP_DEBUG_TRACE, "ldap_dn2dcedn\n", 0, 0, 0 );
314 rdns = explode_name( dn, 0, NAME_TYPE_LDAP_DN );
315 if ( rdns == NULL ) {
319 for ( p = rdns; *p != NULL; p++ ) {
320 len += strlen( *p ) + 1;
323 q = dce = LDAP_MALLOC( len + 1 );
328 p--; /* get back past NULL */
330 for ( ; p != rdns; p-- ) {
342 #else /* USE_LDAP_DN_PARSING */
343 Debug( LDAP_DEBUG_TRACE, "ldap_dn2dcedn\n", 0, 0, 0 );
345 return dn2dn( dn, LDAP_DN_FORMAT_LDAPV3, LDAP_DN_FORMAT_DCE );
346 #endif /* USE_LDAP_DN_PARSING */
350 ldap_dcedn2dn( LDAP_CONST char *dce )
352 #ifndef USE_LDAP_DN_PARSING
353 char *dn, *q, **rdns, **p;
356 Debug( LDAP_DEBUG_TRACE, "ldap_dcedn2dn\n", 0, 0, 0 );
358 rdns = explode_name( dce, 0, NAME_TYPE_DCE_DN );
359 if ( rdns == NULL ) {
365 for ( p = rdns; *p != NULL; p++ ) {
366 len += strlen( *p ) + 1;
369 q = dn = LDAP_MALLOC( len );
376 for ( ; p != rdns; p-- ) {
384 /* the name was fully qualified, thus the most-significant
385 * RDN was empty. trash the last comma */
389 /* the name was relative. copy the most significant RDN */
394 #else /* USE_LDAP_DN_PARSING */
395 Debug( LDAP_DEBUG_TRACE, "ldap_dcedn2dn\n", 0, 0, 0 );
397 return dn2dn( dce, LDAP_DN_FORMAT_DCE, LDAP_DN_FORMAT_LDAPV3 );
398 #endif /* USE_LDAP_DN_PARSING */
402 ldap_dn2ad_canonical( LDAP_CONST char *dn )
404 Debug( LDAP_DEBUG_TRACE, "ldap_dn2ad_canonical\n", 0, 0, 0 );
406 return dn2dn( dn, LDAP_DN_FORMAT_LDAPV3, LDAP_DN_FORMAT_AD_CANONICAL );
409 #ifndef USE_LDAP_DN_PARSING /* deprecated */
414 explode_name( const char *name, int notypes, int is_type )
416 const char *p, *q, *rdn;
418 int offset, state, have_equals, count = 0, endquote, len;
421 if(name == NULL) name = "";
423 /* skip leading whitespace */
424 while( ldap_utf8_isspace( name )) {
425 LDAP_UTF8_INCR( name );
440 if ( p[1] != '\0' ) {
441 offset = LDAP_UTF8_OFFSET(++p);
445 if ( state == INQUOTE )
451 if( state == OUTQUOTE ) have_equals++;
454 if (is_type == NAME_TYPE_LDAP_RDN)
458 if (is_type == NAME_TYPE_DCE_DN)
463 if (is_type == NAME_TYPE_LDAP_DN)
468 if ( state == OUTQUOTE ) {
472 if ( parts == NULL ) {
473 if (( parts = (char **)LDAP_MALLOC( 8
474 * sizeof( char *))) == NULL )
476 } else if ( count >= 8 ) {
477 if (( parts = (char **)LDAP_REALLOC( parts,
478 (count+1) * sizeof( char *)))
483 parts[ count ] = NULL;
487 for ( q = rdn; q < p && *q != '='; ++q ) {
499 if ( p[-1] == '"' ) {
507 if (( parts[ count-1 ] = (char *)LDAP_CALLOC( 1,
510 AC_MEMCPY( parts[ count-1 ], rdn, len );
513 /* skip trailing spaces */
514 while( len > 0 && ldap_utf8_isspace(
515 &parts[count-1][len-1] ) )
521 parts[ count-1 ][ len ] = '\0';
525 * Don't forget to increment 'p' back to where
526 * it should be. If we don't, then we will
527 * never get past an "end quote."
532 rdn = *p ? &p[1] : p;
533 while ( ldap_utf8_isspace( rdn ) )
541 #endif /* !USE_LDAP_DN_PARSING */
544 * helper that changes the string representation of dnin
545 * from ( fin & LDAP_DN_FORMAT_MASK ) to ( fout & LDAP_DN_FORMAT_MASK )
548 * LDAP_DN_FORMAT_LDAPV3 (rfc 2253 and ldapbis)
549 * LDAP_DN_FORMAT_LDAPV2 (rfc 1779)
550 * LDAP_DN_FORMAT_DCE (?)
552 * fout can be any of the above plus:
553 * LDAP_DN_FORMAT_UFN (rfc 1781, partial and with extensions)
554 * LDAP_DN_FORMAT_AD_CANONICAL (?)
557 dn2dn( const char *dnin, unsigned fin, unsigned fout )
560 LDAPDN *tmpDN = NULL;
566 if ( ldap_str2dn( dnin , &tmpDN, fin ) != LDAP_SUCCESS ) {
570 /* don't care about the result ... */
571 ldap_dn2str( tmpDN, &dnout, fout );
573 ldapava_free_dn( tmpDN );
581 #define B4ATTRTYPE 0x0001
582 #define B4OIDATTRTYPE 0x0002
583 #define B4STRINGATTRTYPE 0x0003
585 #define B4AVAEQUALS 0x0100
586 #define B4AVASEP 0x0200
587 #define B4RDNSEP 0x0300
588 #define GOTAVA 0x0400
590 #define B4ATTRVALUE 0x0010
591 #define B4STRINGVALUE 0x0020
592 #define B4IA5VALUEQUOTED 0x0030
593 #define B4IA5VALUE 0x0040
594 #define B4BINARYVALUE 0x0050
596 /* Helpers (mostly from slapd.h; maybe it should be rewritten from this) */
597 #define LDAP_DN_ASCII_SPACE(c) \
598 ( (c) == ' ' || (c) == '\t' || (c) == '\n' || (c) == '\r' )
599 #define LDAP_DN_ASCII_LOWER(c) ( (c) >= 'a' && (c) <= 'z' )
600 #define LDAP_DN_ASCII_UPPER(c) ( (c) >= 'A' && (c) <= 'Z' )
601 #define LDAP_DN_ASCII_ALPHA(c) \
602 ( LDAP_DN_ASCII_LOWER(c) || LDAP_DN_ASCII_UPPER(c) )
603 #define LDAP_DN_ASCII_DIGIT(c) ( (c) >= '0' && (c) <= '9' )
604 #define LDAP_DN_ASCII_LCASE_HEXALPHA(c) ( (c) >= 'a' && (c) <= 'f' )
605 #define LDAP_DN_ASCII_UCASE_HEXALPHA(c) ( (c) >= 'A' && (c) <= 'F' )
606 #define LDAP_DN_ASCII_HEXDIGIT(c) \
607 ( LDAP_DN_ASCII_DIGIT(c) \
608 || LDAP_DN_ASCII_LCASE_HEXALPHA(c) \
609 || LDAP_DN_ASCII_UCASE_HEXALPHA(c) )
610 #define LDAP_DN_ASCII_ALNUM(c) \
611 ( LDAP_DN_ASCII_ALPHA(c) || LDAP_DN_ASCII_DIGIT(c) )
612 #define LDAP_DN_ASCII_PRINTABLE(c) ( (c) >= ' ' && (c) <= '~' )
615 #define LDAP_DN_OID_LEADCHAR(c) ( LDAP_DN_ASCII_DIGIT(c) )
616 #define LDAP_DN_DESC_LEADCHAR(c) ( LDAP_DN_ASCII_ALPHA(c) )
617 #define LDAP_DN_DESC_CHAR(c) ( LDAP_DN_ASCII_ALNUM(c) || (c) == '-' )
618 #define LDAP_DN_LANG_SEP(c) ( (c) == ';' )
619 #define LDAP_DN_ATTRDESC_CHAR(c) \
620 ( LDAP_DN_DESC_CHAR(c) || LDAP_DN_LANG_SEP(c) )
622 /* special symbols */
623 #define LDAP_DN_AVA_EQUALS(c) ( (c) == '=' )
624 #define LDAP_DN_AVA_SEP(c) ( (c) == '+' )
625 #define LDAP_DN_RDN_SEP(c) ( (c) == ',' )
626 #define LDAP_DN_RDN_SEP_V2(c) ( LDAP_DN_RDN_SEP(c) || (c) == ';' )
627 #define LDAP_DN_OCTOTHORPE(c) ( (c) == '#' )
628 #define LDAP_DN_QUOTES(c) ( (c) == '\"' )
629 #define LDAP_DN_ESCAPE(c) ( (c) == '\\' )
630 #define LDAP_DN_VALUE_END(c) \
631 ( LDAP_DN_RDN_SEP(c) || LDAP_DN_AVA_SEP(c) )
632 #define LDAP_DN_NE(c) \
633 ( LDAP_DN_RDN_SEP_V2(c) || LDAP_DN_AVA_SEP(c) \
634 || LDAP_DN_QUOTES(c) || (c) == '<' || (c) == '>' )
635 #define LDAP_DN_NEEDESCAPE(c) \
636 ( LDAP_DN_ESCAPE(c) || LDAP_DN_NE(c) )
637 #define LDAP_DN_NEEDESCAPE_LEAD(c) \
638 ( LDAP_DN_ASCII_SPACE(c) || LDAP_DN_OCTOTHORPE(c) || LDAP_DN_NE(c) )
639 #define LDAP_DN_NEEDESCAPE_TRAIL(c) \
640 ( LDAP_DN_ASCII_SPACE(c) || LDAP_DN_NEEDESCAPE(c) )
643 #define LDAP_DN_VALUE_END_V2(c) \
644 ( LDAP_DN_RDN_SEP_V2(c) || LDAP_DN_AVA_SEP(c) )
646 #define LDAP_DN_V2_SPECIAL(c) \
647 ( LDAP_DN_RDN_SEP_V2(c) || LDAP_DN_AVA_EQUALS(c) \
648 || LDAP_DN_AVA_SEP(c) || (c) == '<' || (c) == '>' \
649 || LDAP_DN_OCTOTHORPE(c) )
650 #define LDAP_DN_V2_PAIR(c) \
651 ( LDAP_DN_V2_SPECIAL(c) || LDAP_DN_ESCAPE(c) || LDAP_DN_QUOTES(c) )
654 * DCE (mostly from Luke Howard and IBM implementation for AIX)
656 * From: "Application Development Guide - Directory Services" (FIXME: add link?)
657 * Here escapes and valid chars for GDS are considered; as soon as more
658 * specific info is found, the macros will be updated.
660 * Chars: 'a'-'z', 'A'-'Z', '0'-'9',
661 * '.', ':', ',', ''', '+', '-', '=', '(', ')', '?', '/', ' '.
663 * Metachars: '/', ',', '=', '\'.
665 * the '\' is used to escape other metachars.
671 * Attribute types must start with alphabetic chars and can contain
672 * alphabetic chars and digits (FIXME: no '-'?). OIDs are allowed.
674 #define LDAP_DN_RDN_SEP_DCE(c) ( (c) == '/' )
675 #define LDAP_DN_AVA_SEP_DCE(c) ( (c) == ',' )
676 #define LDAP_DN_ESCAPE_DCE(c) ( LDAP_DN_ESCAPE(c) )
677 #define LDAP_DN_VALUE_END_DCE(c) \
678 ( LDAP_DN_RDN_SEP_DCE(c) || LDAP_DN_AVA_SEP_DCE(c) )
679 #define LDAP_DN_NEEDESCAPE_DCE(c) \
680 ( LDAP_DN_VALUE_END_DCE(c) || LDAP_DN_AVA_EQUALS(c) )
683 #define LDAP_DN_RDN_SEP_AD(c) ( (c) == '/' )
684 #define LDAP_DN_ESCAPE_AD(c) ( LDAP_DN_ESCAPE(c) )
685 #define LDAP_DN_AVA_SEP_AD(c) ( (c) == ',' ) /* assume same as DCE */
686 #define LDAP_DN_VALUE_END_AD(c) \
687 ( LDAP_DN_RDN_SEP_AD(c) || LDAP_DN_AVA_SEP_AD(c) )
688 #define LDAP_DN_NEEDESCAPE_AD(c) \
689 ( LDAP_DN_VALUE_END_AD(c) || LDAP_DN_AVA_EQUALS(c) )
692 #define LDAP_DN_HEXPAIR(s) \
693 ( LDAP_DN_ASCII_HEXDIGIT((s)[0]) && LDAP_DN_ASCII_HEXDIGIT((s)[1]) )
694 #define LDAP_DC_ATTR "dc"
695 /* better look at the AttributeDescription? */
697 /* FIXME: no composite rdn or non-"dc" types, right?
698 * (what about "dc" in OID form?) */
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->bv_val, 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 struct berval *attr, const struct berval *val,
737 ava = LDAP_MALLOC( sizeof( LDAPAVA ) );
739 /* should we test it? */
744 ava->la_attr = ( struct berval * )attr;
745 ava->la_value = ( struct berval * )val;
746 ava->la_flags = flags;
752 ldapava_free( LDAPAVA *ava )
756 ber_bvfree( ava->la_attr );
757 ber_bvfree( ava->la_value );
763 ldapava_append_to_rdn( LDAPRDN *rdn, LDAPAVA *ava )
771 for ( i = 0U; rdn[ i ]; i++ ) {
775 newRDN = LDAP_REALLOC( rdn, ( i + 2 ) * sizeof( LDAPAVA ** ) );
776 newRDN[ i ] = LDAP_MALLOC( sizeof( LDAPAVA * ) );
777 newRDN[ i ][ 0 ] = ava;
778 newRDN[ i + 1 ] = NULL;
784 ldapava_insert_into_rdn( LDAPRDN *rdn, LDAPAVA *ava, unsigned where )
792 for ( i = 0U; rdn[ i ]; i++ ) {
798 /* assume "at end", which corresponds to
799 * ldapava_append_to_rdn */
802 newRDN = LDAP_REALLOC( rdn, ( i + 2 ) * sizeof( LDAPAVA ** ) );
804 /* data after insert point */
805 AC_MEMCPY( &newRDN[ where + 1 ], &newRDN[ where ],
806 ( i - where ) * sizeof( LDAPRDN * ) );
808 newRDN[ where ] = LDAP_MALLOC( sizeof( LDAPAVA * ) );
809 newRDN[ where ][ 0 ] = ava;
810 newRDN[ i + 1 ] = NULL;
816 ldapava_free_rdn( LDAPRDN *rdn )
824 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
825 assert( rdn[ iAVA ][ 0 ] );
827 ldapava_free( rdn[ iAVA ][ 0 ] );
834 ldapava_append_to_dn( LDAPDN *dn, LDAPRDN *rdn )
842 for ( i = 0U; dn[ i ]; i++ ) {
846 newDN = LDAP_REALLOC( dn, ( i + 2 ) * sizeof( LDAPRDN ** ) );
847 newDN[ i ] = LDAP_MALLOC( sizeof( LDAPRDN * ) );
848 newDN[ i ][ 0 ] = rdn;
849 newDN[ i + 1 ] = NULL;
855 ldapava_insert_into_dn( LDAPDN *dn, LDAPRDN *rdn, unsigned where )
863 for ( i = 0U; dn[ i ]; i++ ) {
869 /* assume "at end", which corresponds to
870 * ldapava_append_to_dn */
873 newDN = LDAP_REALLOC( dn, ( i + 2 ) * sizeof( LDAPRDN ** ) );
875 /* data after insert point */
876 AC_MEMCPY( &newDN[ where + 1 ], &newDN[ where ],
877 ( i - where ) * sizeof( LDAPDN * ) );
879 newDN[ where ] = LDAP_MALLOC( sizeof( LDAPRDN * ) );
880 newDN[ where ][ 0 ] = rdn;
881 newDN[ i + 1 ] = NULL;
887 ldapava_free_dn( LDAPDN *dn )
895 for ( iRDN = 0; dn[ iRDN ]; iRDN++ ) {
896 assert( dn[ iRDN ][ 0 ] );
898 ldapava_free_rdn( dn[ iRDN ][ 0 ] );
905 * Converts a string representation of a DN (in LDAPv3, LDAPv2 or DCE)
906 * into a structured representation of the DN, by separating attribute
907 * types and values encoded in the more appropriate form, which is
908 * string or OID for attribute types and binary form of the BER encoded
909 * value or Unicode string. Formats different from LDAPv3 are parsed
910 * according to their own rules and turned into the more appropriate
911 * form according to LDAPv3.
913 * NOTE: I realize the code is getting spaghettish; it is rather
914 * experimental and will hopefully turn into something more simple
915 * and readable as soon as it works as expected.
919 ldap_str2dn( const char *str, LDAPDN **dn, unsigned flags )
922 int rc = LDAP_INVALID_DN_SYNTAX;
924 LDAPDN *newDN = NULL;
925 LDAPRDN *newRDN = NULL;
930 Debug( LDAP_DEBUG_TRACE, "=> ldap_str2dn(%s,%u)\n%s", str, flags, "" );
934 switch ( LDAP_DN_FORMAT( flags ) ) {
935 case LDAP_DN_FORMAT_LDAPV3:
936 case LDAP_DN_FORMAT_LDAPV2:
937 case LDAP_DN_FORMAT_DCE:
940 /* unsupported in str2dn */
941 case LDAP_DN_FORMAT_UFN:
942 case LDAP_DN_FORMAT_AD_CANONICAL:
943 return( LDAP_INVALID_DN_SYNTAX );
946 return( LDAP_OTHER );
949 if ( str[ 0 ] == '\0' ) {
950 return( LDAP_SUCCESS );
954 if ( LDAP_DN_DCE( flags ) ) {
957 * (from Luke Howard: thnx) A RDN separator is required
958 * at the beginning of an (absolute) DN.
960 if ( !LDAP_DN_RDN_SEP_DCE( p[ 0 ] ) ) {
966 for ( ; p[ 0 ]; p++ ) {
969 rc = ldap_str2rdn( p, &newRDN, &p, flags );
970 if ( rc != LDAP_SUCCESS ) {
975 * We expect a rdn separator
978 switch ( LDAP_DN_FORMAT( flags ) ) {
979 case LDAP_DN_FORMAT_LDAPV3:
980 case LDAP_DN_FORMAT_LDAPV2:
981 if ( !LDAP_DN_RDN_SEP( p[ 0 ] ) ) {
987 case LDAP_DN_FORMAT_DCE:
988 if ( !LDAP_DN_RDN_SEP_DCE( p[ 0 ] ) ) {
997 if ( LDAP_DN_DCE( flags ) ) {
998 /* add in reversed order */
999 dn = ldapava_insert_into_dn( newDN, newRDN, 0 );
1001 dn = ldapava_append_to_dn( newDN, newRDN );
1005 rc = LDAP_NO_MEMORY;
1012 if ( p[ 0 ] == '\0' ) {
1015 * the DN is over, phew
1024 ldapava_free_rdn( newRDN );
1028 ldapava_free_dn( newDN );
1034 Debug( LDAP_DEBUG_TRACE, "<= ldap_str2dn(%s,%u)=%d\n", str, flags, rc );
1043 * Parses a relative DN according to flags up to a rdn separator
1044 * or to the end of str.
1045 * Returns the rdn and a pointer to the string continuation, which
1046 * corresponds to the rdn separator or to '\0' in case the string is over.
1049 ldap_str2rdn( const char *str, LDAPRDN **rdn, const char **n, unsigned flags )
1053 int rc = LDAP_INVALID_DN_SYNTAX;
1054 int attrTypeEncoding = LDAP_AVA_STRING,
1055 attrValueEncoding = LDAP_AVA_STRING;
1057 struct berval *attrType = NULL;
1058 struct berval *attrValue = NULL;
1060 LDAPRDN *newRDN = NULL;
1066 Debug( LDAP_DEBUG_TRACE, "=> ldap_str2rdn(%s,%u)\n%s", str, flags, "" );
1071 switch ( LDAP_DN_FORMAT( flags ) ) {
1072 case LDAP_DN_FORMAT_LDAPV3:
1073 case LDAP_DN_FORMAT_LDAPV2:
1074 case LDAP_DN_FORMAT_DCE:
1077 /* unsupported in str2dn */
1078 case LDAP_DN_FORMAT_UFN:
1079 case LDAP_DN_FORMAT_AD_CANONICAL:
1080 return( LDAP_INVALID_DN_SYNTAX );
1083 return( LDAP_OTHER );
1086 if ( str[ 0 ] == '\0' ) {
1087 return( LDAP_SUCCESS );
1091 for ( ; p[ 0 ] || state == GOTAVA; ) {
1094 * The parser in principle advances one token a time,
1095 * or toggles state if preferable.
1100 * an AttributeType can be encoded as:
1101 * - its string representation; in detail, implementations
1102 * MUST recognize AttributeType string type names listed
1103 * in section 2.3 of draft-ietf-ldapbis-dn-XX.txt, and
1104 * MAY recognize other names.
1105 * - its numeric OID (a dotted decimal string); in detail
1106 * RFC 2253 asserts that ``Implementations MUST allow
1107 * an oid in the attribute type to be prefixed by one
1108 * of the character strings "oid." or "OID."''. As soon
1109 * as draft-ietf-ldapbis-dn-XX.txt obsoletes RFC 2253
1110 * I'm not sure whether this is required or not any
1111 * longer; to be liberal, we still implement it.
1114 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1115 if ( !LDAP_DN_ALLOW_ONE_SPACE( flags ) ) {
1122 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1123 if ( !LDAP_DN_ALLOW_SPACES( flags ) ) {
1128 /* whitespace is allowed (and trimmed) */
1130 while ( p[ 0 ] && LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1135 /* error: we expected an AVA */
1145 if ( LDAP_DN_OID_LEADCHAR( p[ 0 ] ) ) {
1146 state = B4OIDATTRTYPE;
1150 /* else must be alpha */
1151 if ( !LDAP_DN_DESC_LEADCHAR( p[ 0 ] ) ) {
1155 /* LDAPv2 "oid." prefix */
1156 if ( LDAP_DN_LDAPV2( flags ) ) {
1158 * to be overly pedantic, we only accept
1161 if ( flags & LDAP_DN_PEDANTIC ) {
1162 if ( !strncmp( p, "oid.", 4 )
1163 || !strncmp( p, "OID.", 4 ) ) {
1165 state = B4OIDATTRTYPE;
1169 if ( !strncasecmp( p, "oid.", 4 ) ) {
1171 state = B4OIDATTRTYPE;
1177 state = B4STRINGATTRTYPE;
1180 case B4OIDATTRTYPE: {
1181 int err = LDAP_SUCCESS;
1184 type = parse_numericoid( &p, &err, 0 );
1185 if ( type == NULL ) {
1188 attrType = LDAP_MALLOC( sizeof( struct berval ) );
1189 if ( attrType== NULL ) {
1190 rc = LDAP_NO_MEMORY;
1193 attrType->bv_val = type;
1194 attrType->bv_len = strlen( type );
1195 attrTypeEncoding = LDAP_AVA_BINARY;
1197 state = B4AVAEQUALS;
1201 case B4STRINGATTRTYPE: {
1202 const char *startPos, *endPos = NULL;
1206 * the starting char has been found to be
1207 * a LDAP_DN_DESC_LEADCHAR so we don't re-check it
1208 * FIXME: DCE attr types seem to have a more
1209 * restrictive syntax
1211 for ( startPos = p++; p[ 0 ]; p++ ) {
1212 if ( LDAP_DN_DESC_CHAR( p[ 0 ] ) ) {
1216 if ( LDAP_DN_LANG_SEP( p[ 0 ] ) ) {
1219 * RFC 2253 does not explicitly
1220 * allow lang extensions to attribute
1223 if ( flags & LDAP_DN_PEDANTIC ) {
1228 * we trim ';' and following lang
1229 * and so from attribute types
1232 for ( ; LDAP_DN_ATTRDESC_CHAR( p[ 0 ] )
1233 || LDAP_DN_LANG_SEP( p[ 0 ] ); p++ ) {
1241 len = ( endPos ? endPos : p ) - startPos;
1246 assert( attrType == NULL );
1247 attrType = LDAP_MALLOC( sizeof( struct berval ) );
1248 if ( attrType == NULL ) {
1249 rc = LDAP_NO_MEMORY;
1252 attrType->bv_val = LDAP_STRNDUP( startPos, len );
1253 if ( attrType->bv_val == NULL ) {
1254 rc = LDAP_NO_MEMORY;
1257 attrType->bv_len = len;
1258 attrTypeEncoding = LDAP_AVA_STRING;
1261 * here we need to decide whether to use it as is
1262 * or turn it in OID form; as a consequence, we
1263 * need to decide whether to binary encode the value
1266 state = B4AVAEQUALS;
1271 /* spaces may not be allowed */
1272 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1273 if ( !LDAP_DN_ALLOW_SPACES( flags ) ) {
1278 for ( p++; LDAP_DN_ASCII_SPACE( p[ 0 ] ); p++ ) {
1283 /* need equal sign */
1284 if ( !LDAP_DN_AVA_EQUALS( p[ 0 ] ) ) {
1289 /* spaces may not be allowed */
1290 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1291 if ( !LDAP_DN_ALLOW_SPACES( flags ) ) {
1296 for ( p++; LDAP_DN_ASCII_SPACE( p[ 0 ] ); p++ ) {
1302 * octothorpe means a BER encoded value will follow
1303 * FIXME: I don't think DCE will allow it
1305 if ( LDAP_DN_OCTOTHORPE( p[ 0 ] ) ) {
1307 attrValueEncoding = LDAP_AVA_BINARY;
1308 state = B4BINARYVALUE;
1312 /* STRING value expected */
1315 * if we're pedantic, an attribute type in OID form
1316 * SHOULD imply a BER encoded attribute value; we
1317 * should at least issue a warning
1319 if ( ( flags & LDAP_DN_PEDANTIC )
1320 && ( attrTypeEncoding == LDAP_AVA_BINARY ) ) {
1321 /* OID attrType SHOULD use binary encoding */
1325 attrValueEncoding = LDAP_AVA_STRING;
1328 * LDAPv2 allows the attribute value to be quoted;
1329 * also, IA5 values are expected, in principle
1331 if ( LDAP_DN_LDAPV2( flags ) ) {
1332 if ( LDAP_DN_QUOTES( p[ 0 ] ) ) {
1334 state = B4IA5VALUEQUOTED;
1343 * here STRING means RFC 2253 string
1344 * FIXME: what about DCE strings?
1346 state = B4STRINGVALUE;
1350 if ( hexstr2binval( p, &attrValue, &p, flags ) ) {
1358 switch ( LDAP_DN_FORMAT( flags ) ) {
1359 case LDAP_DN_FORMAT_LDAPV3:
1360 if ( str2strval( p, &attrValue,
1362 &attrValueEncoding ) ) {
1367 case LDAP_DN_FORMAT_DCE:
1368 /* FIXME: does DCE use UTF-8? */
1369 if ( DCE2strval( p, &attrValue,
1383 if ( IA52strval( p, &attrValue, &p, flags ) ) {
1390 case B4IA5VALUEQUOTED:
1392 /* lead quote already stripped */
1393 if ( quotedIA52strval( p, &attrValue,
1407 * we accept empty values
1409 ava = ldapava_new( attrType, attrValue,
1410 attrValueEncoding );
1411 if ( ava == NULL ) {
1412 rc = LDAP_NO_MEMORY;
1416 rdn = ldapava_append_to_rdn( newRDN, ava );
1417 if ( rdn == NULL ) {
1418 rc = LDAP_NO_MEMORY;
1424 * if we got an AVA separator ('+', | ',' * for DCE )
1425 * we expect a new AVA for this RDN; otherwise
1426 * we add the RDN to the DN
1428 switch ( LDAP_DN_FORMAT( flags ) ) {
1429 case LDAP_DN_FORMAT_LDAPV3:
1430 case LDAP_DN_FORMAT_LDAPV2:
1431 if ( !LDAP_DN_AVA_SEP( p[ 0 ] ) ) {
1436 case LDAP_DN_FORMAT_DCE:
1437 if ( !LDAP_DN_AVA_SEP_DCE( p[ 0 ] ) ) {
1445 * the RDN is over, phew
1452 /* they should have been used in an AVA */
1468 /* They are set to NULL after they're used in an AVA */
1470 ber_bvfree( attrType );
1474 ber_bvfree( attrValue );
1478 ldapava_free_rdn( newRDN );
1483 Debug( LDAP_DEBUG_TRACE, "<= ldap_str2rdn(%*s)=%d\n",
1491 * reads in a UTF-8 string value, unescaping stuff:
1492 * '\' + LDAP_DN_NEEDESCAPE(c) -> 'c'
1493 * '\' + HEXPAIR(p) -> unhex(p)
1496 str2strval( const char *str, struct berval **val, const char **next, unsigned flags, unsigned *retFlags )
1498 const char *p, *startPos, *endPos = NULL;
1499 ber_len_t len, escapes, unescapes;
1508 for ( startPos = p = str, escapes = 0, unescapes = 0; p[ 0 ]; p++ ) {
1509 if ( LDAP_DN_ESCAPE( p[ 0 ] ) ) {
1511 if ( p[ 0 ] == '\0' ) {
1514 if ( ( p == startPos + 1 && LDAP_DN_NEEDESCAPE_LEAD( p[ 0 ] ) )
1515 || ( LDAP_DN_VALUE_END( p[ 1 ] ) && LDAP_DN_NEEDESCAPE_TRAIL( p[ 0 ] ) )
1516 || LDAP_DN_NEEDESCAPE( p[ 0 ] ) ) {
1521 if ( LDAP_DN_HEXPAIR( p ) ) {
1524 hexstr2bin( p, &c );
1527 if ( !LDAP_DN_ASCII_PRINTABLE( c ) ) {
1530 * we assume the string is UTF-8
1532 *retFlags = LDAP_AVA_NONPRINTABLE;
1539 if ( LDAP_DN_PEDANTIC & flags ) {
1543 * FIXME: we allow escaping
1544 * of chars that don't need
1545 * to and do not belong to
1546 * HEXDIGITS (we also allow
1547 * single hexdigit; maybe we
1552 } else if ( LDAP_DN_VALUE_END( p[ 0 ] ) ) {
1555 } else if ( LDAP_DN_NEEDESCAPE( p[ 0 ] ) ) {
1557 * FIXME: maybe we can add
1558 * escapes if not pedantic?
1565 * we do allow unescaped spaces at the end
1566 * of the value only in non-pedantic mode
1568 if ( p > startPos + 1 && LDAP_DN_ASCII_SPACE( p[ -1 ] ) &&
1569 !LDAP_DN_ESCAPE( p[ -2 ] ) ) {
1570 if ( flags & LDAP_DN_PEDANTIC ) {
1574 /* strip trailing (unescaped) spaces */
1575 for ( endPos = p - 1;
1576 endPos > startPos + 1 &&
1577 LDAP_DN_ASCII_SPACE( endPos[ -1 ] ) &&
1578 !LDAP_DN_ESCAPE( endPos[ -2 ] );
1585 * FIXME: test memory?
1587 len = ( endPos ? endPos : p ) - startPos - escapes - unescapes;
1588 *val = LDAP_MALLOC( sizeof( struct berval ) );
1589 ( *val )->bv_len = len;
1591 if ( escapes == 0 && unescapes == 0 ) {
1592 ( *val )->bv_val = LDAP_STRNDUP( startPos, len );
1597 ( *val )->bv_val = LDAP_MALLOC( len + 1 );
1598 for ( s = 0, d = 0; d < len; ) {
1599 if ( LDAP_DN_ESCAPE( startPos[ s ] ) ) {
1601 if ( ( s == 0 && LDAP_DN_NEEDESCAPE_LEAD( startPos[ s ] ) )
1602 || ( s == len - 1 && LDAP_DN_NEEDESCAPE_TRAIL( startPos[ s ] ) )
1603 || LDAP_DN_NEEDESCAPE( startPos[ s ] ) ) {
1604 ( *val )->bv_val[ d++ ] =
1607 } else if ( LDAP_DN_HEXPAIR( &startPos[ s ] ) ) {
1610 hexstr2bin( &startPos[ s ], &c );
1611 ( *val )->bv_val[ d++ ] = c;
1616 * we allow escaping of chars
1617 * that do not need to
1619 ( *val )->bv_val[ d++ ] =
1624 ( *val )->bv_val[ d++ ] = startPos[ s++ ];
1628 ( *val )->bv_val[ d ] = '\0';
1629 assert( strlen( ( *val )->bv_val ) == len );
1639 DCE2strval( const char *str, struct berval **val, const char **next, unsigned flags )
1641 const char *p, *startPos, *endPos = NULL;
1642 ber_len_t len, escapes;
1651 for ( startPos = p = str, escapes = 0; p[ 0 ]; p++ ) {
1652 if ( LDAP_DN_ESCAPE_DCE( p[ 0 ] ) ) {
1654 if ( LDAP_DN_NEEDESCAPE_DCE( p[ 0 ] ) ) {
1661 } else if ( LDAP_DN_VALUE_END_DCE( p[ 0 ] ) ) {
1666 * FIXME: can we accept anything else? I guess we need
1667 * to stop if a value is not legal
1672 * (unescaped) trailing spaces are trimmed must be silently ignored;
1675 if ( p > startPos + 1 && LDAP_DN_ASCII_SPACE( p[ -1 ] ) &&
1676 !LDAP_DN_ESCAPE( p[ -2 ] ) ) {
1677 if ( flags & LDAP_DN_PEDANTIC ) {
1681 /* strip trailing (unescaped) spaces */
1682 for ( endPos = p - 1;
1683 endPos > startPos + 1 &&
1684 LDAP_DN_ASCII_SPACE( endPos[ -1 ] ) &&
1685 !LDAP_DN_ESCAPE( endPos[ -2 ] );
1692 len = ( endPos ? endPos : p ) - startPos - escapes;
1693 *val = LDAP_MALLOC( sizeof( struct berval ) );
1694 ( *val )->bv_len = len;
1695 if ( escapes == 0 ){
1696 ( *val )->bv_val = LDAP_STRNDUP( startPos, len );
1701 ( *val )->bv_val = LDAP_MALLOC( len + 1 );
1702 for ( s = 0, d = 0; d < len; ) {
1704 * This point is reached only if escapes
1705 * are properly used, so all we need to
1708 if ( LDAP_DN_ESCAPE_DCE( startPos[ s ] ) ) {
1712 ( *val )->bv_val[ d++ ] = startPos[ s++ ];
1714 ( *val )->bv_val[ d ] = '\0';
1715 assert( strlen( ( *val )->bv_val ) == len );
1724 IA52strval( const char *str, struct berval **val, const char **next, unsigned flags )
1726 const char *p, *startPos, *endPos = NULL;
1727 ber_len_t len, escapes;
1740 for ( startPos = p = str, escapes = 0; p[ 0 ]; p++ ) {
1741 if ( LDAP_DN_ESCAPE( p[ 0 ] ) ) {
1743 if ( p[ 0 ] == '\0' ) {
1747 if ( !LDAP_DN_NEEDESCAPE( p[ 0 ] )
1748 && ( LDAP_DN_PEDANTIC & flags ) ) {
1753 } else if ( LDAP_DN_VALUE_END_V2( p[ 0 ] ) ) {
1758 * FIXME: can we accept anything else? I guess we need
1759 * to stop if a value is not legal
1763 /* strip trailing (unescaped) spaces */
1765 endPos > startPos + 1 &&
1766 LDAP_DN_ASCII_SPACE( endPos[ -1 ] ) &&
1767 !LDAP_DN_ESCAPE( endPos[ -2 ] );
1772 *val = LDAP_MALLOC( sizeof( struct berval ) );
1773 len = ( endPos ? endPos : p ) - startPos - escapes;
1774 ( *val )->bv_len = len;
1775 if ( escapes == 0 ) {
1776 ( *val )->bv_val = LDAP_STRNDUP( startPos, len );
1781 ( *val )->bv_val = LDAP_MALLOC( len + 1 );
1782 for ( s = 0, d = 0; d < len; ) {
1783 if ( LDAP_DN_ESCAPE( startPos[ s ] ) ) {
1786 ( *val )->bv_val[ d++ ] = startPos[ s++ ];
1788 ( *val )->bv_val[ d ] = '\0';
1789 assert( strlen( ( *val )->bv_val ) == len );
1797 quotedIA52strval( const char *str, struct berval **val, const char **next, unsigned flags )
1799 const char *p, *startPos, *endPos = NULL;
1801 unsigned escapes = 0;
1810 /* initial quote already eaten */
1811 for ( startPos = p = str; p[ 0 ]; p++ ) {
1813 * According to RFC 1779, the quoted value can
1814 * contain escaped as well as unescaped special values;
1815 * as a consequence we tolerate escaped values
1816 * (e.g. '"\,"' -> '\,') and escape unescaped specials
1817 * (e.g. '","' -> '\,').
1819 if ( LDAP_DN_ESCAPE( p[ 0 ] ) ) {
1820 if ( p[ 1 ] == '\0' ) {
1825 if ( !LDAP_DN_V2_PAIR( p[ 0 ] )
1826 && ( LDAP_DN_PEDANTIC & flags ) ) {
1828 * do we allow to escape normal chars?
1829 * LDAPv2 does not allow any mechanism
1830 * for escaping chars with '\' and hex
1837 } else if ( LDAP_DN_QUOTES( p[ 0 ] ) ) {
1839 /* eat closing quotes */
1845 * FIXME: can we accept anything else? I guess we need
1846 * to stop if a value is not legal
1850 if ( endPos == NULL ) {
1854 /* Strip trailing (unescaped) spaces */
1855 for ( ; p[ 0 ] && LDAP_DN_ASCII_SPACE( p[ 0 ] ); p++ ) {
1859 len = endPos - startPos - escapes;
1861 *val = LDAP_MALLOC( sizeof( struct berval ) );
1862 ( *val )->bv_len = len;
1863 if ( escapes == 0 ) {
1864 ( *val )->bv_val = LDAP_STRNDUP( startPos, len );
1869 ( *val )->bv_val = LDAP_MALLOC( len + 1 );
1870 ( *val )->bv_len = len;
1872 for ( s = d = 0; d < len; ) {
1873 if ( LDAP_DN_ESCAPE( str[ s ] ) ) {
1876 ( *val )->bv_val[ d++ ] = str[ s++ ];
1878 ( *val )->bv_val[ d ] = '\0';
1879 assert( strlen( ( *val )->bv_val ) == len );
1888 hexstr2bin( const char *str, char *c )
1898 if ( LDAP_DN_ASCII_DIGIT( c1 ) ) {
1904 if ( LDAP_DN_ASCII_LCASE_HEXALPHA( c1 ) ) {
1911 if ( LDAP_DN_ASCII_DIGIT( c2 ) ) {
1917 if ( LDAP_DN_ASCII_LCASE_HEXALPHA( c2 ) ) {
1918 *c += c2 - 'a' + 10;
1926 hexstr2binval( const char *str, struct berval **val, const char **next, unsigned flags )
1928 const char *p, *startPos, *endPos = NULL;
1939 for ( startPos = p = str; p[ 0 ]; p += 2 ) {
1940 switch ( LDAP_DN_FORMAT( flags ) ) {
1941 case LDAP_DN_FORMAT_LDAPV3:
1942 case LDAP_DN_FORMAT_LDAPV2:
1943 if ( LDAP_DN_VALUE_END( p[ 0 ] ) ) {
1948 case LDAP_DN_FORMAT_DCE:
1949 if ( LDAP_DN_VALUE_END_DCE( p[ 0 ] ) ) {
1955 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1956 if ( flags & LDAP_DN_PEDANTIC ) {
1961 for ( ; p[ 0 ]; p++ ) {
1962 switch ( LDAP_DN_FORMAT( flags ) ) {
1963 case LDAP_DN_FORMAT_LDAPV3:
1964 case LDAP_DN_FORMAT_LDAPV2:
1965 if ( LDAP_DN_VALUE_END( p[ 0 ] ) ) {
1970 case LDAP_DN_FORMAT_DCE:
1971 if ( LDAP_DN_VALUE_END_DCE( p[ 0 ] ) ) {
1980 if ( !LDAP_DN_HEXPAIR( p ) ) {
1987 len = ( ( endPos ? endPos : p ) - startPos ) / 2;
1989 assert( 2 * len == ( endPos ? endPos : p ) - startPos );
1991 *val = LDAP_MALLOC( sizeof( struct berval ) );
1992 if ( *val == NULL ) {
1993 return( LDAP_NO_MEMORY );
1996 ( *val )->bv_len = len;
1997 ( *val )->bv_val = LDAP_MALLOC( len + 1 );
1998 if ( ( *val )->bv_val == NULL ) {
2000 return( LDAP_NO_MEMORY );
2003 for ( s = 0, d = 0; d < len; s += 2, d++ ) {
2006 hexstr2bin( &startPos[ s ], &c );
2008 ( *val )->bv_val[ d ] = c;
2011 ( *val )->bv_val[ d ] = '\0';
2018 * convert a byte in a hexadecimal pair
2021 byte2hexpair( const char *val, char *pair )
2023 static const char hexdig[] = "0123456789abcdef";
2029 * we assume the string has enough room for the hex encoding
2033 pair[ 0 ] = hexdig[ 0x0f & ( val[ 0 ] >> 4 ) ];
2034 pair[ 1 ] = hexdig[ 0x0f & val[ 0 ] ];
2040 * convert a binary value in hexadecimal pairs
2043 binval2hexstr( struct berval *val, char *str )
2050 if ( val->bv_len == 0 ) {
2055 * we assume the string has enough room for the hex encoding
2059 for ( s = 0, d = 0; s < val->bv_len; s++, d += 2 ) {
2060 byte2hexpair( &val->bv_val[ s ], &str[ d ] );
2067 * Length of the string representation, accounting for escaped hex
2071 strval2strlen( struct berval *val, unsigned flags, ber_len_t *len )
2073 ber_len_t l, cl = 1;
2080 if ( val->bv_len == 0 ) {
2084 for ( l = 0, p = val->bv_val; p[ 0 ]; p += cl ) {
2085 cl = ldap_utf8_charlen( p );
2087 /* illegal utf-8 char! */
2090 } else if ( cl > 1 ) {
2093 for ( cnt = 1; cnt < cl; cnt++ ) {
2094 if ( ( p[ cnt ] & 0x80 ) == 0x00 ) {
2098 /* need to escape it */
2101 } else if ( ( p == val->bv_val && LDAP_DN_NEEDESCAPE_LEAD( p[ 0 ] ) )
2102 || ( !p[ 1 ] && LDAP_DN_NEEDESCAPE_TRAIL( p[ 0 ] ) )
2103 || LDAP_DN_NEEDESCAPE( p[ 0 ] ) ) {
2117 * convert to string representation, escaping with hex the UTF-8 stuff;
2118 * assume the destination has enough room for escaping
2121 strval2str( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2123 ber_len_t s, d, end;
2129 if ( val->bv_len == 0 ) {
2135 * we assume the string has enough room for the hex encoding
2138 for ( s = 0, d = 0, end = val->bv_len - 1; s < val->bv_len; ) {
2139 ber_len_t cl = ldap_utf8_charlen( &val->bv_val[ s ] );
2144 byte2hexpair( &val->bv_val[ s ], &str[ d ] );
2150 if ( ( d == 0 && LDAP_DN_NEEDESCAPE_LEAD( val->bv_val[ s ] ) )
2151 || ( s == end && LDAP_DN_NEEDESCAPE_TRAIL( val->bv_val[ s ] ) )
2152 || LDAP_DN_NEEDESCAPE( val->bv_val[ s ] ) ) {
2155 str[ d++ ] = val->bv_val[ s++ ];
2165 * Length of the IA5 string representation (no UTF-8 allowed)
2168 strval2IA5strlen( struct berval *val, unsigned flags, ber_len_t *len )
2177 if ( val->bv_len == 0 ) {
2181 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2183 * Turn value into a binary encoded BER
2188 for ( l = 0, p = val->bv_val; p[ 0 ]; p++ ) {
2189 if ( ( p == val->bv_val && LDAP_DN_NEEDESCAPE_LEAD( p[ 0 ] ) )
2190 || ( !p[ 1 ] && LDAP_DN_NEEDESCAPE_TRAIL( p[ 0 ] ) )
2191 || LDAP_DN_NEEDESCAPE( p[ 0 ] ) ) {
2206 * convert to string representation (np UTF-8)
2207 * assume the destination has enough room for escaping
2210 strval2IA5str( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2212 ber_len_t s, d, end;
2218 if ( val->bv_len == 0 ) {
2223 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2225 * Turn value into a binary encoded BER
2232 * we assume the string has enough room for the hex encoding
2236 for ( s = 0, d = 0, end = val->bv_len - 1; s < val->bv_len; ) {
2237 if ( ( s == 0 && LDAP_DN_NEEDESCAPE_LEAD( val->bv_val[ s ] ) )
2238 || ( s == end && LDAP_DN_NEEDESCAPE_TRAIL( val->bv_val[ s ] ) )
2239 || LDAP_DN_NEEDESCAPE( val->bv_val[ s ] ) ) {
2242 str[ d++ ] = val->bv_val[ s++ ];
2252 * Length of the (supposedly) DCE string representation,
2253 * accounting for escaped hex of UTF-8 chars
2256 strval2DCEstrlen( struct berval *val, unsigned flags, ber_len_t *len )
2265 if ( val->bv_len == 0 ) {
2269 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2271 * FIXME: Turn the value into a binary encoded BER?
2276 for ( l = 0, p = val->bv_val; p[ 0 ]; p++ ) {
2277 if ( LDAP_DN_NEEDESCAPE_DCE( p[ 0 ] ) ) {
2292 * convert to (supposedly) DCE string representation,
2293 * escaping with hex the UTF-8 stuff;
2294 * assume the destination has enough room for escaping
2297 strval2DCEstr( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2305 if ( val->bv_len == 0 ) {
2310 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2312 * FIXME: Turn the value into a binary encoded BER?
2320 * we assume the string has enough room for the hex encoding
2324 for ( s = 0, d = 0; s < val->bv_len; ) {
2325 if ( LDAP_DN_NEEDESCAPE_DCE( val->bv_val[ s ] ) ) {
2328 str[ d++ ] = val->bv_val[ s++ ];
2338 * Length of the (supposedly) AD canonical string representation,
2339 * accounting for escaped hex of UTF-8 chars
2342 strval2ADstrlen( struct berval *val, unsigned flags, ber_len_t *len )
2351 if ( val->bv_len == 0 ) {
2355 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2357 * FIXME: Turn the value into a binary encoded BER?
2362 for ( l = 0, p = val->bv_val; p[ 0 ]; p++ ) {
2363 if ( LDAP_DN_NEEDESCAPE_AD( p[ 0 ] ) ) {
2378 * convert to (supposedly) AD string representation,
2379 * escaping with hex the UTF-8 stuff;
2380 * assume the destination has enough room for escaping
2383 strval2ADstr( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2391 if ( val->bv_len == 0 ) {
2396 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2398 * FIXME: Turn the value into a binary encoded BER?
2406 * we assume the string has enough room for the hex encoding
2410 for ( s = 0, d = 0; s < val->bv_len; ) {
2411 if ( LDAP_DN_NEEDESCAPE_AD( val->bv_val[ s ] ) ) {
2414 str[ d++ ] = val->bv_val[ s++ ];
2424 * If the DN is terminated by single-AVA RDNs with attribute type of "dc",
2425 * the forst part of the AD representation of the DN is written in DNS
2426 * form, i.e. dot separated domain name components (as suggested
2427 * by Luke Howard, http://www.padl.com/~lukeh)
2430 dn2domain( LDAPDN *dn, char *str, int *iRDN )
2433 int domain = 0, first = 1;
2434 ber_len_t l = 1; /* we move the null also */
2436 /* we are guaranteed there's enough memory in str */
2442 assert( *iRDN > 0 );
2444 for ( i = *iRDN; i >= 0; i-- ) {
2448 assert( dn[ i ][ 0 ] );
2451 assert( rdn[ 0 ][ 0 ] );
2452 ava = rdn[ 0 ][ 0 ];
2454 if ( !LDAP_DN_IS_RDN_DC( rdn ) ) {
2462 AC_MEMCPY( str, ava->la_value->bv_val,
2463 ava->la_value->bv_len + 1);
2464 l += ava->la_value->bv_len;
2467 AC_MEMCPY( str + ava->la_value->bv_len + 1, str, l);
2468 AC_MEMCPY( str, ava->la_value->bv_val,
2469 ava->la_value->bv_len );
2470 str[ ava->la_value->bv_len ] = '.';
2471 l += ava->la_value->bv_len + 1;
2481 rdn2strlen( LDAPRDN *rdn, ber_len_t *len,
2482 int ( *s2l )( struct berval *v, unsigned f, ber_len_t *l ) )
2489 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2490 LDAPAVA *ava = rdn[ iAVA ][ 0 ];
2492 /* len(type) + '=' + '+' | ',' */
2493 l += ava->la_attr->bv_len + 2;
2495 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2496 /* octothorpe + twice the length */
2497 l += 1 + 2 * ava->la_value->bv_len;
2502 if ( ( *s2l )( ava->la_value, ava->la_flags, &vl ) ) {
2515 rdn2str( LDAPRDN *rdn, char *str, ber_len_t *len,
2516 int ( *s2s ) ( struct berval *v, char * s, unsigned f, ber_len_t *l ) )
2521 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2522 LDAPAVA *ava = rdn[ iAVA ][ 0 ];
2524 AC_MEMCPY( &str[ l ], ava->la_attr->bv_val,
2525 ava->la_attr->bv_len );
2526 l += ava->la_attr->bv_len;
2530 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2532 if ( binval2hexstr( ava->la_value, &str[ l ] ) ) {
2535 l += 2 * ava->la_value->bv_len;
2540 if ( ( *s2s )( ava->la_value, &str[ l ],
2541 ava->la_flags, &vl ) ) {
2546 str[ l++ ] = ( rdn[ iAVA + 1 ] ? '+' : ',' );
2555 rdn2DCEstrlen( LDAPRDN *rdn, ber_len_t *len )
2562 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2563 LDAPAVA *ava = rdn[ iAVA ][ 0 ];
2565 /* len(type) + '=' + ',' | '/' */
2566 l += ava->la_attr->bv_len + 2;
2568 switch ( ava->la_flags ) {
2569 case LDAP_AVA_BINARY:
2570 /* octothorpe + twice the length */
2571 l += 1 + 2 * ava->la_value->bv_len;
2574 case LDAP_AVA_STRING: {
2577 if ( strval2DCEstrlen( ava->la_value,
2578 ava->la_flags, &vl ) ) {
2596 rdn2DCEstr( LDAPRDN *rdn, char *str, ber_len_t *len, int first )
2601 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2602 LDAPAVA *ava = rdn[ iAVA ][ 0 ];
2607 str[ l++ ] = ( iAVA ? ',' : '/' );
2610 AC_MEMCPY( &str[ l ], ava->la_attr->bv_val,
2611 ava->la_attr->bv_len );
2612 l += ava->la_attr->bv_len;
2616 switch ( ava->la_flags ) {
2617 case LDAP_AVA_BINARY:
2619 if ( binval2hexstr( ava->la_value, &str[ l ] ) ) {
2622 l += 2 * ava->la_value->bv_len;
2625 case LDAP_AVA_STRING: {
2628 if ( strval2DCEstr( ava->la_value, &str[ l ],
2629 ava->la_flags, &vl ) ) {
2647 rdn2UFNstrlen( LDAPRDN *rdn, ber_len_t *len )
2657 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2658 LDAPAVA *ava = rdn[ iAVA ][ 0 ];
2661 l += ( rdn[ iAVA + 1 ] ? 3 : 2 );
2663 /* FIXME: are binary values allowed in UFN? */
2664 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2665 /* octothorpe + twice the value */
2666 l += 1 + 2 * ava->la_value->bv_len;
2671 if ( strval2strlen( ava->la_value, ava->la_flags,
2685 rdn2UFNstr( LDAPRDN *rdn, char *str, ber_len_t *len )
2690 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2691 LDAPAVA *ava = rdn[ iAVA ][ 0 ];
2693 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2695 if ( binval2hexstr( ava->la_value, &str[ l ] ) ) {
2698 l += 2 * ava->la_value->bv_len;
2703 if ( strval2str( ava->la_value, &str[ l ],
2704 ava->la_flags, &vl ) ) {
2710 if ( rdn[ iAVA + 1 ]) {
2711 AC_MEMCPY( &str[ l ], " + ", 3 );
2715 AC_MEMCPY( &str[ l ], ", ", 2 );
2726 rdn2ADstrlen( LDAPRDN *rdn, ber_len_t *len )
2736 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2737 LDAPAVA *ava = rdn[ iAVA ][ 0 ];
2742 /* FIXME: are binary values allowed in UFN? */
2743 switch ( ava->la_flags ) {
2744 case LDAP_AVA_BINARY:
2745 /* octothorpe + twice the value */
2746 l += 1 + 2 * ava->la_value->bv_len;
2749 case LDAP_AVA_STRING: {
2752 if ( strval2ADstrlen( ava->la_value,
2753 ava->la_flags, &vl ) ) {
2771 rdn2ADstr( LDAPRDN *rdn, char *str, ber_len_t *len, int first )
2776 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2777 LDAPAVA *ava = rdn[ iAVA ][ 0 ];
2782 str[ l++ ] = ( iAVA ? ',' : '/' );
2785 switch ( ava->la_flags ) {
2786 case LDAP_AVA_BINARY:
2788 if ( binval2hexstr( ava->la_value, &str[ l ] ) ) {
2791 l += 2 * ava->la_value->bv_len;
2794 case LDAP_AVA_STRING: {
2797 if ( strval2ADstr( ava->la_value, &str[ l ],
2798 ava->la_flags, &vl ) ) {
2818 * Returns in str a string representation of rdn based on flags.
2819 * There is some duplication of code between this and ldap_dn2str;
2820 * this is wanted to reduce the allocation of temporary buffers.
2823 ldap_rdn2str( LDAPRDN *rdn, char **str, unsigned flags )
2830 if ( rdn == NULL ) {
2831 *str = LDAP_STRDUP( "" );
2832 return( LDAP_SUCCESS );
2836 * This routine wastes "back" bytes at the end of the string
2840 switch ( LDAP_DN_FORMAT( flags ) ) {
2841 case LDAP_DN_FORMAT_LDAPV3:
2842 if ( rdn2strlen( rdn, &l, strval2strlen ) ) {
2843 return( LDAP_OTHER );
2847 case LDAP_DN_FORMAT_LDAPV2:
2848 if ( rdn2strlen( rdn, &l, strval2IA5strlen ) ) {
2849 return( LDAP_OTHER );
2853 case LDAP_DN_FORMAT_UFN:
2854 if ( rdn2UFNstrlen( rdn, &l ) ) {
2855 return( LDAP_OTHER );
2859 case LDAP_DN_FORMAT_DCE:
2860 if ( rdn2DCEstrlen( rdn, &l ) ) {
2861 return( LDAP_OTHER );
2865 case LDAP_DN_FORMAT_AD_CANONICAL:
2866 if ( rdn2ADstrlen( rdn, &l ) ) {
2867 return( LDAP_OTHER );
2872 return( LDAP_OTHER );
2875 *str = LDAP_MALLOC( l + 1 );
2877 switch ( LDAP_DN_FORMAT( flags ) ) {
2878 case LDAP_DN_FORMAT_LDAPV3:
2879 rc = rdn2str( rdn, *str, &l, strval2str );
2883 case LDAP_DN_FORMAT_LDAPV2:
2884 rc = rdn2str( rdn, *str, &l, strval2IA5str );
2888 case LDAP_DN_FORMAT_UFN:
2889 rc = rdn2UFNstr( rdn, *str, &l );
2893 case LDAP_DN_FORMAT_DCE:
2894 rc = rdn2DCEstr( rdn, *str, &l, 1 );
2898 case LDAP_DN_FORMAT_AD_CANONICAL:
2899 rc = rdn2ADstr( rdn, *str, &l, 1 );
2904 /* need at least one of the previous */
2905 return( LDAP_OTHER );
2909 ldap_memfree( *str );
2910 return( LDAP_OTHER );
2913 ( *str )[ l - back ] = '\0';
2915 return( LDAP_SUCCESS );
2919 * Very bulk implementation; many optimizations can be performed
2920 * - a NULL dn results in an empty string ""
2923 * a) what do we do if a UTF-8 string must be converted in LDAPv2?
2924 * we must encode it in binary form ('#' + HEXPAIRs)
2925 * b) does DCE/AD support UTF-8?
2926 * no clue; don't think so.
2927 * c) what do we do when binary values must be converted in UTF/DCE/AD?
2928 * use binary encoded BER
2930 int ldap_dn2str( LDAPDN *dn, char **str, unsigned flags )
2933 int rc = LDAP_OTHER;
2936 /* stringifying helpers for LDAPv3/LDAPv2 */
2937 int ( *sv2l ) ( struct berval *v, unsigned f, ber_len_t *l );
2938 int ( *sv2s ) ( struct berval *v, char *s, unsigned f, ber_len_t *l );
2942 Debug( LDAP_DEBUG_TRACE, "=> ldap_dn2str(%u)\n%s%s", flags, "", "" );
2947 * a null dn means an empty dn string
2948 * FIXME: better raise an error?
2951 *str = LDAP_STRDUP( "" );
2952 return( LDAP_SUCCESS );
2955 switch ( LDAP_DN_FORMAT( flags ) ) {
2956 case LDAP_DN_FORMAT_LDAPV3:
2957 sv2l = strval2strlen;
2961 case LDAP_DN_FORMAT_LDAPV2:
2962 sv2l = strval2IA5strlen;
2963 sv2s = strval2IA5str;
2966 for ( iRDN = 0, len = 0; dn[ iRDN ]; iRDN++ ) {
2968 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
2970 if ( rdn2strlen( rdn, &rdnl, sv2l ) ) {
2971 goto return_results;
2977 if ( ( *str = LDAP_MALLOC( len + 1 ) ) == NULL ) {
2978 rc = LDAP_NO_MEMORY;
2982 for ( l = 0, iRDN = 0; dn[ iRDN ]; iRDN++ ) {
2984 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
2986 if ( rdn2str( rdn, &( *str )[ l ], &rdnl, sv2s ) ) {
2989 goto return_results;
2997 * trim the last ',' (the allocated memory
2998 * is one byte longer than required)
3000 ( *str )[ len - 1 ] = '\0';
3005 case LDAP_DN_FORMAT_UFN: {
3008 * FIXME: quoting from RFC 1781:
3010 To take a distinguished name, and generate a name of this format with
3011 attribute types omitted, the following steps are followed.
3013 1. If the first attribute is of type CommonName, the type may be
3016 2. If the last attribute is of type Country, the type may be
3019 3. If the last attribute is of type Country, the last
3020 Organisation attribute may have the type omitted.
3022 4. All attributes of type OrganisationalUnit may have the type
3023 omitted, unless they are after an Organisation attribute or
3024 the first attribute is of type OrganisationalUnit.
3026 * this should be the pedantic implementation.
3028 * Here the standard implementation reflects
3029 * the one historically provided by OpenLDAP
3030 * (and UMIch, I presume), with the variant
3031 * of spaces and plusses (' + ') separating
3034 * A non-standard but nice implementation could
3035 * be to turn the final "dc" attributes into a
3036 * dot-separated domain.
3038 * Other improvements could involve the use of
3039 * friendly country names and so.
3042 int leftmost_dc = -1;
3044 #endif /* DC_IN_UFN */
3046 for ( iRDN = 0, len = 0; dn[ iRDN ]; iRDN++ ) {
3048 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
3050 if ( rdn2UFNstrlen( rdn, &rdnl ) ) {
3051 goto return_results;
3056 if ( LDAP_DN_IS_RDN_DC( rdn ) ) {
3057 if ( leftmost_dc == -1 ) {
3063 #endif /* DC_IN_UFN */
3066 if ( ( *str = LDAP_MALLOC( len + 1 ) ) == NULL ) {
3067 rc = LDAP_NO_MEMORY;
3072 if ( leftmost_dc == -1 ) {
3073 #endif /* DC_IN_UFN */
3074 for ( l = 0, iRDN = 0; dn[ iRDN ]; iRDN++ ) {
3076 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
3078 if ( rdn2UFNstr( rdn, &( *str )[ l ], &vl ) ) {
3081 goto return_results;
3087 * trim the last ', ' (the allocated memory
3088 * is two bytes longer than required)
3090 ( *str )[ len - 2 ] = '\0';
3093 last_iRDN = iRDN - 1;
3095 for ( l = 0, iRDN = 0; iRDN < leftmost_dc; iRDN++ ) {
3097 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
3099 if ( rdn2UFNstr( rdn, &( *str )[ l ], &vl ) ) {
3102 goto return_results;
3107 if ( !dn2domain( dn, &( *str )[ l ], &last_iRDN ) ) {
3110 goto return_results;
3113 /* the string is correctly terminated by dn2domain */
3115 #endif /* DC_IN_UFN */
3121 case LDAP_DN_FORMAT_DCE:
3123 for ( iRDN = 0, len = 0; dn[ iRDN ]; iRDN++ ) {
3125 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
3127 if ( rdn2DCEstrlen( rdn, &rdnl ) ) {
3128 goto return_results;
3134 if ( ( *str = LDAP_MALLOC( len + 1 ) ) == NULL ) {
3135 rc = LDAP_NO_MEMORY;
3139 for ( l = 0; iRDN--; ) {
3141 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
3143 if ( rdn2DCEstr( rdn, &( *str )[ l ], &rdnl, 0 ) ) {
3146 goto return_results;
3153 ( *str )[ len ] = '\0';
3158 case LDAP_DN_FORMAT_AD_CANONICAL: {
3161 * Sort of UFN for DCE DNs: a slash ('/') separated
3162 * global->local DN with no types; strictly speaking,
3163 * the naming context should be a domain, which is
3164 * written in DNS-style, e.g. dot-deparated.
3168 * "cn=Bill+sn=Gates,ou=People,dc=microsoft,dc=com"
3172 * "microsoft.com/People/Bill,Gates"
3174 for ( iRDN = 0, len = -1; dn[ iRDN ]; iRDN++ ) {
3176 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
3178 if ( rdn2ADstrlen( rdn, &rdnl ) ) {
3179 goto return_results;
3185 if ( ( *str = LDAP_MALLOC( len + 1 ) ) == NULL ) {
3186 rc = LDAP_NO_MEMORY;
3191 if ( iRDN && dn2domain( dn, *str, &iRDN ) ) {
3192 for ( l = strlen( *str ); iRDN >= 0 ; iRDN-- ) {
3194 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
3196 if ( rdn2ADstr( rdn, &( *str )[ l ],
3200 goto return_results;
3209 * Strictly speaking, AD canonical requires
3210 * a DN to be in the form "..., dc=smtg",
3211 * i.e. terminated by a domain component
3213 if ( flags & LDAP_DN_PEDANTIC ) {
3216 rc = LDAP_INVALID_DN_SYNTAX;
3220 for ( l = 0; iRDN >= 0 ; iRDN-- ) {
3222 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
3224 if ( rdn2ADstr( rdn, &( *str )[ l ],
3228 goto return_results;
3237 ( *str )[ len ] = '\0';
3248 Debug( LDAP_DEBUG_TRACE, "<= ldap_dn2str(%s,%u)=%d\n", *str, flags, rc );