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 /* extension to UFN that turns trailing "dc=value" rdns in DNS style,
27 * e.g. "ou=People,dc=openldap,dc=org" => "People, openldap.org" */
30 static int dn2dn( const char *dnin, unsigned fin, char **dnout, unsigned fout );
32 /* from libraries/libldap/schema.c */
33 extern char * parse_numericoid(const char **sp, int *code, const int flags);
35 /* parsing/printing routines */
36 static int str2strval( const char *str, struct berval **val,
37 const char **next, unsigned flags, unsigned *retFlags );
38 static int DCE2strval( const char *str, struct berval **val,
39 const char **next, unsigned flags );
40 static int IA52strval( const char *str, struct berval **val,
41 const char **next, unsigned flags );
42 static int quotedIA52strval( const char *str, struct berval **val,
43 const char **next, unsigned flags );
44 static int hexstr2binval( const char *str, struct berval **val,
45 const char **next, unsigned flags );
46 static int hexstr2bin( const char *str, char *c );
47 static int byte2hexpair( const char *val, char *pair );
48 static int binval2hexstr( struct berval *val, char *str );
49 static int strval2strlen( struct berval *val, unsigned flags,
51 static int strval2str( struct berval *val, char *str, unsigned flags,
53 static int strval2IA5strlen( struct berval *val, unsigned flags,
55 static int strval2IA5str( struct berval *val, char *str, unsigned flags,
57 static int strval2DCEstrlen( struct berval *val, unsigned flags,
59 static int strval2DCEstr( struct berval *val, char *str, unsigned flags,
61 static int strval2ADstrlen( struct berval *val, unsigned flags,
63 static int strval2ADstr( struct berval *val, char *str, unsigned flags,
65 static int dn2domain( LDAPDN *dn, char *str, int *iRDN );
68 static LDAPAVA * ldapava_new(
69 const struct berval *attr, const struct berval *val, unsigned flags );
70 static LDAPRDN * ldapava_append_to_rdn( LDAPRDN *rdn, LDAPAVA *ava );
71 static LDAPRDN * ldapava_insert_into_rdn(
72 LDAPRDN *rdn, LDAPAVA *ava, unsigned where );
73 static LDAPDN * ldapava_append_to_dn( LDAPDN *dn, LDAPRDN *rdn );
74 static LDAPDN * ldapava_insert_into_dn(
75 LDAPDN *dn, LDAPRDN *rdn, unsigned where );
77 /* Higher level helpers */
78 static int rdn2strlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len,
79 int ( *s2l )( struct berval *, unsigned, ber_len_t * ) );
80 static int rdn2str( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len,
81 int ( *s2s )( struct berval *, char *, unsigned, ber_len_t * ));
82 static int rdn2UFNstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len );
83 static int rdn2UFNstr( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len );
84 static int rdn2DCEstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len );
85 static int rdn2DCEstr( LDAPRDN *rdn, char *str, unsigned flag, ber_len_t *len, int first );
86 static int rdn2ADstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len );
87 static int rdn2ADstr( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len, int first );
89 #ifndef USE_LDAP_DN_PARSING /* deprecated */
90 #define NAME_TYPE_LDAP_RDN 0
91 #define NAME_TYPE_LDAP_DN 1
92 #define NAME_TYPE_DCE_DN 2
94 static char **explode_name( const char *name, int notypes, int is_type );
95 #endif /* !USE_LDAP_DN_PARSING */
98 * RFC 1823 ldap_get_dn
101 ldap_get_dn( LDAP *ld, LDAPMessage *entry )
106 Debug( LDAP_DEBUG_TRACE, "ldap_get_dn\n", 0, 0, 0 );
108 if ( entry == NULL ) {
109 ld->ld_errno = LDAP_PARAM_ERROR;
113 tmp = *entry->lm_ber; /* struct copy */
114 if ( ber_scanf( &tmp, "{a" /*}*/, &dn ) == LBER_ERROR ) {
115 ld->ld_errno = LDAP_DECODING_ERROR;
123 * RFC 1823 ldap_dn2ufn
126 ldap_dn2ufn( LDAP_CONST char *dn )
128 #ifndef USE_LDAP_DN_PARSING /* deprecated */
133 Debug( LDAP_DEBUG_TRACE, "ldap_dn2ufn\n", 0, 0, 0 );
135 /* produces completely untyped UFNs */
141 vals = ldap_explode_dn( dn , 0 );
146 for ( i = 0; vals[i]; i++ ) {
149 rvals = ldap_explode_rdn( vals[i] , 1 );
150 if ( rvals == NULL ) {
155 LDAP_FREE( vals[i] );
156 vals[i] = ldap_charray2str( rvals, " + " );
160 ufn = ldap_charray2str( vals, ", " );
164 #else /* USE_LDAP_DN_PARSING */
167 Debug( LDAP_DEBUG_TRACE, "ldap_dn2ufn\n", 0, 0, 0 );
169 ( void )dn2dn( dn, LDAP_DN_FORMAT_LDAP, &out, LDAP_DN_FORMAT_UFN );
172 #endif /* USE_LDAP_DN_PARSING */
176 * RFC 1823 ldap_explode_dn
179 ldap_explode_dn( LDAP_CONST char *dn, int notypes )
181 #ifndef USE_LDAP_DN_PARSING /* deprecated */
182 Debug( LDAP_DEBUG_TRACE, "ldap_explode_dn\n", 0, 0, 0 );
184 return explode_name( dn, notypes, NAME_TYPE_LDAP_DN );
185 #else /* USE_LDAP_DN_PARSING */
187 char **values = NULL;
189 unsigned flag = notypes ? LDAP_DN_FORMAT_UFN : LDAP_DN_FORMAT_LDAPV3;
191 Debug( LDAP_DEBUG_TRACE, "ldap_explode_dn\n", 0, 0, 0 );
193 if ( ldap_str2dn( dn, &tmpDN, LDAP_DN_FORMAT_LDAP )
198 for ( iRDN = 0; tmpDN[ iRDN ]; iRDN++ ) {
199 char *str, **v = NULL;
201 ldap_rdn2str( tmpDN[ iRDN ][ 0 ], &str, flag );
203 v = LDAP_REALLOC( values, sizeof( char * ) * ( 2 + iRDN ) );
205 LBER_VFREE( values );
206 ldap_dnfree( tmpDN );
210 values[ iRDN ] = str;
212 values[ iRDN ] = NULL;
215 #endif /* USE_LDAP_DN_PARSING */
219 ldap_explode_rdn( LDAP_CONST char *rdn, int notypes )
221 #ifndef USE_LDAP_DN_PARSING /* deprecated */
222 Debug( LDAP_DEBUG_TRACE, "ldap_explode_rdn\n", 0, 0, 0 );
224 return explode_name( rdn, notypes, NAME_TYPE_LDAP_RDN );
225 #else /* USE_LDAP_DN_PARSING */
227 char **values = NULL;
229 unsigned flag = notypes ? LDAP_DN_FORMAT_UFN : LDAP_DN_FORMAT_LDAPV3;
231 Debug( LDAP_DEBUG_TRACE, "ldap_explode_rdn\n", 0, 0, 0 );
234 * we assume this dn is made of one rdn only
236 if ( ldap_str2dn( rdn, &tmpDN, LDAP_DN_FORMAT_LDAP )
241 for ( iAVA = 0; tmpDN[ 0 ][ 0 ][ iAVA ]; iAVA++ ) {
242 ber_len_t l = 0, vl, al = 0;
243 char *str, **v = NULL;
244 LDAPAVA *ava = tmpDN[ 0 ][ 0 ][ iAVA ][ 0 ];
246 v = LDAP_REALLOC( values, sizeof( char * ) * ( 2 + iAVA ) );
252 if ( ava->la_flags == LDAP_AVA_BINARY ) {
253 vl = 1 + 2 * ava->la_value->bv_len;
256 if ( strval2strlen( ava->la_value,
257 ava->la_flags, &vl ) ) {
263 al = ava->la_attr->bv_len;
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 );
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 ldap_dnfree( tmpDN );
299 LBER_VFREE( values );
300 ldap_dnfree( 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 */
345 Debug( LDAP_DEBUG_TRACE, "ldap_dn2dcedn\n", 0, 0, 0 );
347 ( void )dn2dn( dn, LDAP_DN_FORMAT_LDAP, &out, LDAP_DN_FORMAT_DCE );
350 #endif /* USE_LDAP_DN_PARSING */
354 ldap_dcedn2dn( LDAP_CONST char *dce )
356 #ifndef USE_LDAP_DN_PARSING
357 char *dn, *q, **rdns, **p;
360 Debug( LDAP_DEBUG_TRACE, "ldap_dcedn2dn\n", 0, 0, 0 );
362 rdns = explode_name( dce, 0, NAME_TYPE_DCE_DN );
363 if ( rdns == NULL ) {
369 for ( p = rdns; *p != NULL; p++ ) {
370 len += strlen( *p ) + 1;
373 q = dn = LDAP_MALLOC( len );
380 for ( ; p != rdns; p-- ) {
388 /* the name was fully qualified, thus the most-significant
389 * RDN was empty. trash the last comma */
393 /* the name was relative. copy the most significant RDN */
398 #else /* USE_LDAP_DN_PARSING */
401 Debug( LDAP_DEBUG_TRACE, "ldap_dcedn2dn\n", 0, 0, 0 );
403 ( void )dn2dn( dce, LDAP_DN_FORMAT_DCE, &out, LDAP_DN_FORMAT_LDAPV3 );
406 #endif /* USE_LDAP_DN_PARSING */
410 ldap_dn2ad_canonical( LDAP_CONST char *dn )
414 Debug( LDAP_DEBUG_TRACE, "ldap_dn2ad_canonical\n", 0, 0, 0 );
416 ( void )dn2dn( dn, LDAP_DN_FORMAT_LDAP,
417 &out, LDAP_DN_FORMAT_AD_CANONICAL );
422 #ifndef USE_LDAP_DN_PARSING /* deprecated */
427 explode_name( const char *name, int notypes, int is_type )
429 const char *p, *q, *rdn;
431 int offset, state, have_equals, count = 0, endquote, len;
434 if(name == NULL) name = "";
436 /* skip leading whitespace */
437 while( ldap_utf8_isspace( name )) {
438 LDAP_UTF8_INCR( name );
453 if ( p[1] != '\0' ) {
454 offset = LDAP_UTF8_OFFSET(++p);
458 if ( state == INQUOTE )
464 if( state == OUTQUOTE ) have_equals++;
467 if (is_type == NAME_TYPE_LDAP_RDN)
471 if (is_type == NAME_TYPE_DCE_DN)
476 if (is_type == NAME_TYPE_LDAP_DN)
481 if ( state == OUTQUOTE ) {
485 if ( parts == NULL ) {
486 if (( parts = (char **)LDAP_MALLOC( 8
487 * sizeof( char *))) == NULL )
489 } else if ( count >= 8 ) {
490 if (( parts = (char **)LDAP_REALLOC( parts,
491 (count+1) * sizeof( char *)))
496 parts[ count ] = NULL;
500 for ( q = rdn; q < p && *q != '='; ++q ) {
512 if ( p[-1] == '"' ) {
520 if (( parts[ count-1 ] = (char *)LDAP_CALLOC( 1,
523 AC_MEMCPY( parts[ count-1 ], rdn, len );
526 /* skip trailing spaces */
527 while( len > 0 && ldap_utf8_isspace(
528 &parts[count-1][len-1] ) )
534 parts[ count-1 ][ len ] = '\0';
538 * Don't forget to increment 'p' back to where
539 * it should be. If we don't, then we will
540 * never get past an "end quote."
545 rdn = *p ? &p[1] : p;
546 while ( ldap_utf8_isspace( rdn ) )
554 #endif /* !USE_LDAP_DN_PARSING */
557 ldap_dn_normalize( const char *in, unsigned iflags, char **out, unsigned oflags )
561 #ifdef USE_LDAP_DN_PARSING
562 Debug( LDAP_DEBUG_TRACE, "ldap_dn_normalize\n", 0, 0, 0 );
564 return dn2dn( in, iflags, out, oflags);
565 #else /* !USE_LDAP_DN_PARSING */
566 return( LDAP_OTHER );
567 #endif /* !USE_LDAP_DN_PARSING */
571 * helper that changes the string representation of dnin
572 * from ( fin & LDAP_DN_FORMAT_MASK ) to ( fout & LDAP_DN_FORMAT_MASK )
575 * LDAP_DN_FORMAT_LDAP (rfc 2253 and ldapbis liberal,
576 * plus some rfc 1779)
577 * LDAP_DN_FORMAT_LDAPV3 (rfc 2253 and ldapbis)
578 * LDAP_DN_FORMAT_LDAPV2 (rfc 1779)
579 * LDAP_DN_FORMAT_DCE (?)
581 * fout can be any of the above except
582 * LDAP_DN_FORMAT_LDAP
584 * LDAP_DN_FORMAT_UFN (rfc 1781, partial and with extensions)
585 * LDAP_DN_FORMAT_AD_CANONICAL (?)
588 dn2dn( const char *dnin, unsigned fin, char **dnout, unsigned fout )
591 LDAPDN *tmpDN = NULL;
597 if ( dnin == NULL ) {
598 return( LDAP_SUCCESS );
601 rc = ldap_str2dn( dnin , &tmpDN, fin );
602 if ( rc != LDAP_SUCCESS ) {
606 rc = ldap_dn2str( tmpDN, dnout, fout );
608 ldap_dnfree( tmpDN );
616 /* #define B4ATTRTYPE 0x0001 */
617 #define B4OIDATTRTYPE 0x0002
618 #define B4STRINGATTRTYPE 0x0003
620 #define B4AVAEQUALS 0x0100
621 #define B4AVASEP 0x0200
622 #define B4RDNSEP 0x0300
623 #define GOTAVA 0x0400
625 #define B4ATTRVALUE 0x0010
626 #define B4STRINGVALUE 0x0020
627 #define B4IA5VALUEQUOTED 0x0030
628 #define B4IA5VALUE 0x0040
629 #define B4BINARYVALUE 0x0050
631 /* Helpers (mostly from slapd.h; maybe it should be rewritten from this) */
632 #define LDAP_DN_ASCII_SPACE(c) \
633 ( (c) == ' ' || (c) == '\t' || (c) == '\n' || (c) == '\r' )
634 #define LDAP_DN_ASCII_LOWER(c) ( (c) >= 'a' && (c) <= 'z' )
635 #define LDAP_DN_ASCII_UPPER(c) ( (c) >= 'A' && (c) <= 'Z' )
636 #define LDAP_DN_ASCII_ALPHA(c) \
637 ( LDAP_DN_ASCII_LOWER(c) || LDAP_DN_ASCII_UPPER(c) )
638 #define LDAP_DN_ASCII_DIGIT(c) ( (c) >= '0' && (c) <= '9' )
639 #define LDAP_DN_ASCII_LCASE_HEXALPHA(c) ( (c) >= 'a' && (c) <= 'f' )
640 #define LDAP_DN_ASCII_UCASE_HEXALPHA(c) ( (c) >= 'A' && (c) <= 'F' )
641 #define LDAP_DN_ASCII_HEXDIGIT(c) \
642 ( LDAP_DN_ASCII_DIGIT(c) \
643 || LDAP_DN_ASCII_LCASE_HEXALPHA(c) \
644 || LDAP_DN_ASCII_UCASE_HEXALPHA(c) )
645 #define LDAP_DN_ASCII_ALNUM(c) \
646 ( LDAP_DN_ASCII_ALPHA(c) || LDAP_DN_ASCII_DIGIT(c) )
647 #define LDAP_DN_ASCII_PRINTABLE(c) ( (c) >= ' ' && (c) <= '~' )
650 #define LDAP_DN_OID_LEADCHAR(c) ( LDAP_DN_ASCII_DIGIT(c) )
651 #define LDAP_DN_DESC_LEADCHAR(c) ( LDAP_DN_ASCII_ALPHA(c) )
652 #define LDAP_DN_DESC_CHAR(c) ( LDAP_DN_ASCII_ALNUM(c) || (c) == '-' )
653 #define LDAP_DN_LANG_SEP(c) ( (c) == ';' )
654 #define LDAP_DN_ATTRDESC_CHAR(c) \
655 ( LDAP_DN_DESC_CHAR(c) || LDAP_DN_LANG_SEP(c) )
657 /* special symbols */
658 #define LDAP_DN_AVA_EQUALS(c) ( (c) == '=' )
659 #define LDAP_DN_AVA_SEP(c) ( (c) == '+' )
660 #define LDAP_DN_RDN_SEP(c) ( (c) == ',' )
661 #define LDAP_DN_RDN_SEP_V2(c) ( LDAP_DN_RDN_SEP(c) || (c) == ';' )
662 #define LDAP_DN_OCTOTHORPE(c) ( (c) == '#' )
663 #define LDAP_DN_QUOTES(c) ( (c) == '\"' )
664 #define LDAP_DN_ESCAPE(c) ( (c) == '\\' )
665 #define LDAP_DN_VALUE_END(c) \
666 ( LDAP_DN_RDN_SEP(c) || LDAP_DN_AVA_SEP(c) )
667 #define LDAP_DN_NE(c) \
668 ( LDAP_DN_RDN_SEP_V2(c) || LDAP_DN_AVA_SEP(c) \
669 || LDAP_DN_QUOTES(c) || (c) == '<' || (c) == '>' )
670 #define LDAP_DN_NEEDESCAPE(c) \
671 ( LDAP_DN_ESCAPE(c) || LDAP_DN_NE(c) )
672 #define LDAP_DN_NEEDESCAPE_LEAD(c) \
673 ( LDAP_DN_ASCII_SPACE(c) || LDAP_DN_OCTOTHORPE(c) || LDAP_DN_NE(c) )
674 #define LDAP_DN_NEEDESCAPE_TRAIL(c) \
675 ( LDAP_DN_ASCII_SPACE(c) || LDAP_DN_NEEDESCAPE(c) )
676 #define LDAP_DN_WILLESCAPE_CHAR( c) \
677 ( LDAP_DN_RDN_SEP(c) || LDAP_DN_AVA_SEP(c) )
678 #define LDAP_DN_IS_PRETTY( f ) ( (f) & LDAP_DN_PRETTY )
679 #define LDAP_DN_WILLESCAPE(f, c) \
680 ( ( !LDAP_DN_IS_PRETTY( f ) ) && LDAP_DN_WILLESCAPE_CHAR(c) )
683 #define LDAP_DN_VALUE_END_V2(c) \
684 ( LDAP_DN_RDN_SEP_V2(c) || LDAP_DN_AVA_SEP(c) )
686 #define LDAP_DN_V2_SPECIAL(c) \
687 ( LDAP_DN_RDN_SEP_V2(c) || LDAP_DN_AVA_EQUALS(c) \
688 || LDAP_DN_AVA_SEP(c) || (c) == '<' || (c) == '>' \
689 || LDAP_DN_OCTOTHORPE(c) )
690 #define LDAP_DN_V2_PAIR(c) \
691 ( LDAP_DN_V2_SPECIAL(c) || LDAP_DN_ESCAPE(c) || LDAP_DN_QUOTES(c) )
694 * DCE (mostly from Luke Howard and IBM implementation for AIX)
696 * From: "Application Development Guide - Directory Services" (FIXME: add link?)
697 * Here escapes and valid chars for GDS are considered; as soon as more
698 * specific info is found, the macros will be updated.
700 * Chars: 'a'-'z', 'A'-'Z', '0'-'9',
701 * '.', ':', ',', ''', '+', '-', '=', '(', ')', '?', '/', ' '.
703 * Metachars: '/', ',', '=', '\'.
705 * the '\' is used to escape other metachars.
711 * Attribute types must start with alphabetic chars and can contain
712 * alphabetic chars and digits (FIXME: no '-'?). OIDs are allowed.
714 #define LDAP_DN_RDN_SEP_DCE(c) ( (c) == '/' )
715 #define LDAP_DN_AVA_SEP_DCE(c) ( (c) == ',' )
716 #define LDAP_DN_ESCAPE_DCE(c) ( LDAP_DN_ESCAPE(c) )
717 #define LDAP_DN_VALUE_END_DCE(c) \
718 ( LDAP_DN_RDN_SEP_DCE(c) || LDAP_DN_AVA_SEP_DCE(c) )
719 #define LDAP_DN_NEEDESCAPE_DCE(c) \
720 ( LDAP_DN_VALUE_END_DCE(c) || LDAP_DN_AVA_EQUALS(c) )
723 #define LDAP_DN_RDN_SEP_AD(c) ( (c) == '/' )
724 #define LDAP_DN_ESCAPE_AD(c) ( LDAP_DN_ESCAPE(c) )
725 #define LDAP_DN_AVA_SEP_AD(c) ( (c) == ',' ) /* assume same as DCE */
726 #define LDAP_DN_VALUE_END_AD(c) \
727 ( LDAP_DN_RDN_SEP_AD(c) || LDAP_DN_AVA_SEP_AD(c) )
728 #define LDAP_DN_NEEDESCAPE_AD(c) \
729 ( LDAP_DN_VALUE_END_AD(c) || LDAP_DN_AVA_EQUALS(c) )
732 #define LDAP_DN_HEXPAIR(s) \
733 ( LDAP_DN_ASCII_HEXDIGIT((s)[0]) && LDAP_DN_ASCII_HEXDIGIT((s)[1]) )
734 #define LDAP_DC_ATTR "dc"
735 /* better look at the AttributeDescription? */
737 /* FIXME: no composite rdn or non-"dc" types, right?
738 * (what about "dc" in OID form?) */
739 /* FIXME: we do not allow binary values in domain, right? */
740 /* NOTE: use this macro only when ABSOLUTELY SURE rdn IS VALID! */
741 #define LDAP_DN_IS_RDN_DC( rdn ) \
742 ( ( rdn ) && ( rdn )[ 0 ][ 0 ] && !( rdn )[ 1 ] \
743 && ( ( rdn )[ 0 ][ 0 ]->la_flags == LDAP_AVA_STRING ) \
744 && ! strcasecmp( ( rdn )[ 0 ][ 0 ]->la_attr->bv_val, LDAP_DC_ATTR ) )
746 /* Composite rules */
747 #define LDAP_DN_ALLOW_ONE_SPACE(f) \
748 ( LDAP_DN_LDAPV2(f) \
749 || !( (f) & LDAP_DN_P_NOSPACEAFTERRDN ) )
750 #define LDAP_DN_ALLOW_SPACES(f) \
751 ( LDAP_DN_LDAPV2(f) \
752 || !( (f) & ( LDAP_DN_P_NOLEADTRAILSPACES | LDAP_DN_P_NOSPACEAFTERRDN ) ) )
753 #define LDAP_DN_LDAP(f) \
754 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_LDAP )
755 #define LDAP_DN_LDAPV3(f) \
756 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_LDAPV3 )
757 #define LDAP_DN_LDAPV2(f) \
758 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_LDAPV2 )
759 #define LDAP_DN_DCE(f) \
760 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_DCE )
761 #define LDAP_DN_UFN(f) \
762 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_UFN )
763 #define LDAP_DN_ADC(f) \
764 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_AD_CANONICAL )
765 #define LDAP_DN_FORMAT(f) ( (f) & LDAP_DN_FORMAT_MASK )
768 * LDAPAVA helpers (will become part of the API for operations
769 * on structural representations of DNs).
772 ldapava_new( const struct berval *attr, const struct berval *val,
780 ava = LDAP_MALLOC( sizeof( LDAPAVA ) );
782 /* should we test it? */
787 ava->la_attr = ( struct berval * )attr;
788 ava->la_value = ( struct berval * )val;
789 ava->la_flags = flags;
791 ava->la_private = NULL;
797 ldap_avafree( LDAPAVA *ava )
801 /* ava's private must be freed by caller */
802 assert( ava->la_private != NULL );
804 ber_bvfree( ava->la_attr );
805 ber_bvfree( ava->la_value );
811 ldapava_append_to_rdn( LDAPRDN *rdn, LDAPAVA *ava )
819 for ( i = 0U; rdn[ i ]; i++ ) {
823 newRDN = LDAP_REALLOC( rdn, ( i + 2 ) * sizeof( LDAPAVA ** ) );
824 newRDN[ i ] = LDAP_MALLOC( sizeof( LDAPAVA * ) );
825 newRDN[ i ][ 0 ] = ava;
826 newRDN[ i + 1 ] = NULL;
832 ldapava_insert_into_rdn( LDAPRDN *rdn, LDAPAVA *ava, unsigned where )
840 for ( i = 0U; rdn[ i ]; i++ ) {
846 /* assume "at end", which corresponds to
847 * ldapava_append_to_rdn */
850 newRDN = LDAP_REALLOC( rdn, ( i + 2 ) * sizeof( LDAPAVA ** ) );
852 /* data after insert point */
853 AC_MEMCPY( &newRDN[ where + 1 ], &newRDN[ where ],
854 ( i - where ) * sizeof( LDAPRDN * ) );
856 newRDN[ where ] = LDAP_MALLOC( sizeof( LDAPAVA * ) );
857 newRDN[ where ][ 0 ] = ava;
858 newRDN[ i + 1 ] = NULL;
864 ldap_rdnfree( LDAPRDN *rdn )
872 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
873 assert( rdn[ iAVA ][ 0 ] );
875 ldap_avafree( rdn[ iAVA ][ 0 ] );
882 ldapava_append_to_dn( LDAPDN *dn, LDAPRDN *rdn )
890 for ( i = 0U; dn[ i ]; i++ ) {
894 newDN = LDAP_REALLOC( dn, ( i + 2 ) * sizeof( LDAPRDN ** ) );
895 newDN[ i ] = LDAP_MALLOC( sizeof( LDAPRDN * ) );
896 newDN[ i ][ 0 ] = rdn;
897 newDN[ i + 1 ] = NULL;
903 ldapava_insert_into_dn( LDAPDN *dn, LDAPRDN *rdn, unsigned where )
911 for ( i = 0U; dn[ i ]; i++ ) {
917 /* assume "at end", which corresponds to
918 * ldapava_append_to_dn */
921 newDN = LDAP_REALLOC( dn, ( i + 2 ) * sizeof( LDAPRDN ** ) );
923 /* data after insert point */
924 AC_MEMCPY( &newDN[ where + 1 ], &newDN[ where ],
925 ( i - where ) * sizeof( LDAPDN * ) );
927 newDN[ where ] = LDAP_MALLOC( sizeof( LDAPRDN * ) );
928 newDN[ where ][ 0 ] = rdn;
929 newDN[ i + 1 ] = NULL;
935 ldap_dnfree( LDAPDN *dn )
943 for ( iRDN = 0; dn[ iRDN ]; iRDN++ ) {
944 assert( dn[ iRDN ][ 0 ] );
946 ldap_rdnfree( dn[ iRDN ][ 0 ] );
953 * Converts a string representation of a DN (in LDAPv3, LDAPv2 or DCE)
954 * into a structural representation of the DN, by separating attribute
955 * types and values encoded in the more appropriate form, which is
956 * string or OID for attribute types and binary form of the BER encoded
957 * value or Unicode string. Formats different from LDAPv3 are parsed
958 * according to their own rules and turned into the more appropriate
959 * form according to LDAPv3.
961 * NOTE: I realize the code is getting spaghettish; it is rather
962 * experimental and will hopefully turn into something more simple
963 * and readable as soon as it works as expected.
967 ldap_str2dn( const char *str, LDAPDN **dn, unsigned flags )
970 int rc = LDAP_INVALID_DN_SYNTAX;
972 LDAPDN *newDN = NULL;
973 LDAPRDN *newRDN = NULL;
978 Debug( LDAP_DEBUG_TRACE, "=> ldap_str2dn(%s,%u)\n%s", str, flags, "" );
982 switch ( LDAP_DN_FORMAT( flags ) ) {
983 case LDAP_DN_FORMAT_LDAP:
984 case LDAP_DN_FORMAT_LDAPV3:
985 case LDAP_DN_FORMAT_LDAPV2:
986 case LDAP_DN_FORMAT_DCE:
989 /* unsupported in str2dn */
990 case LDAP_DN_FORMAT_UFN:
991 case LDAP_DN_FORMAT_AD_CANONICAL:
992 return( LDAP_INVALID_DN_SYNTAX );
995 return( LDAP_OTHER );
998 if ( str[ 0 ] == '\0' ) {
999 return( LDAP_SUCCESS );
1003 if ( LDAP_DN_DCE( flags ) ) {
1006 * (from Luke Howard: thnx) A RDN separator is required
1007 * at the beginning of an (absolute) DN.
1009 if ( !LDAP_DN_RDN_SEP_DCE( p[ 0 ] ) ) {
1014 } else if ( LDAP_DN_LDAP( flags ) ) {
1016 * if dn starts with '/' let's make it a DCE dn
1018 if ( LDAP_DN_RDN_SEP_DCE( p[ 0 ] ) ) {
1019 flags |= LDAP_DN_FORMAT_DCE;
1024 for ( ; p[ 0 ]; p++ ) {
1028 err = ldap_str2rdn( p, &newRDN, &p, flags );
1029 if ( err != LDAP_SUCCESS ) {
1034 * We expect a rdn separator
1037 switch ( LDAP_DN_FORMAT( flags ) ) {
1038 case LDAP_DN_FORMAT_LDAPV3:
1039 if ( !LDAP_DN_RDN_SEP( p[ 0 ] ) ) {
1045 case LDAP_DN_FORMAT_LDAP:
1046 case LDAP_DN_FORMAT_LDAPV2:
1047 if ( !LDAP_DN_RDN_SEP_V2( p[ 0 ] ) ) {
1053 case LDAP_DN_FORMAT_DCE:
1054 if ( !LDAP_DN_RDN_SEP_DCE( p[ 0 ] ) ) {
1063 if ( LDAP_DN_DCE( flags ) ) {
1064 /* add in reversed order */
1065 dn = ldapava_insert_into_dn( newDN, newRDN, 0 );
1067 dn = ldapava_append_to_dn( newDN, newRDN );
1071 rc = LDAP_NO_MEMORY;
1078 if ( p[ 0 ] == '\0' ) {
1081 * the DN is over, phew
1090 ldap_rdnfree( newRDN );
1094 ldap_dnfree( newDN );
1100 Debug( LDAP_DEBUG_TRACE, "<= ldap_str2dn(%s,%u)=%d\n", str, flags, rc );
1109 * Parses a relative DN according to flags up to a rdn separator
1110 * or to the end of str.
1111 * Returns the rdn and a pointer to the string continuation, which
1112 * corresponds to the rdn separator or to '\0' in case the string is over.
1115 ldap_str2rdn( const char *str, LDAPRDN **rdn, const char **n, unsigned flags )
1119 int rc = LDAP_INVALID_DN_SYNTAX;
1120 int attrTypeEncoding = LDAP_AVA_STRING,
1121 attrValueEncoding = LDAP_AVA_STRING;
1123 struct berval *attrType = NULL;
1124 struct berval *attrValue = NULL;
1126 LDAPRDN *newRDN = NULL;
1132 Debug( LDAP_DEBUG_TRACE, "=> ldap_str2rdn(%s,%u)\n%s", str, flags, "" );
1137 switch ( LDAP_DN_FORMAT( flags ) ) {
1138 case LDAP_DN_FORMAT_LDAP:
1139 case LDAP_DN_FORMAT_LDAPV3:
1140 case LDAP_DN_FORMAT_LDAPV2:
1141 case LDAP_DN_FORMAT_DCE:
1144 /* unsupported in str2dn */
1145 case LDAP_DN_FORMAT_UFN:
1146 case LDAP_DN_FORMAT_AD_CANONICAL:
1147 return( LDAP_INVALID_DN_SYNTAX );
1150 return( LDAP_OTHER );
1153 if ( str[ 0 ] == '\0' ) {
1154 return( LDAP_SUCCESS );
1158 for ( ; p[ 0 ] || state == GOTAVA; ) {
1161 * The parser in principle advances one token a time,
1162 * or toggles state if preferable.
1167 * an AttributeType can be encoded as:
1168 * - its string representation; in detail, implementations
1169 * MUST recognize AttributeType string type names listed
1170 * in section 2.3 of draft-ietf-ldapbis-dn-XX.txt, and
1171 * MAY recognize other names.
1172 * - its numeric OID (a dotted decimal string); in detail
1173 * RFC 2253 asserts that ``Implementations MUST allow
1174 * an oid in the attribute type to be prefixed by one
1175 * of the character strings "oid." or "OID."''. As soon
1176 * as draft-ietf-ldapbis-dn-XX.txt obsoletes RFC 2253
1177 * I'm not sure whether this is required or not any
1178 * longer; to be liberal, we still implement it.
1181 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1182 if ( !LDAP_DN_ALLOW_ONE_SPACE( flags ) ) {
1189 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1190 if ( !LDAP_DN_ALLOW_SPACES( flags ) ) {
1195 /* whitespace is allowed (and trimmed) */
1197 while ( p[ 0 ] && LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1202 /* error: we expected an AVA */
1208 if ( LDAP_DN_OID_LEADCHAR( p[ 0 ] ) ) {
1209 state = B4OIDATTRTYPE;
1213 /* else must be alpha */
1214 if ( !LDAP_DN_DESC_LEADCHAR( p[ 0 ] ) ) {
1218 /* LDAPv2 "oid." prefix */
1219 if ( LDAP_DN_LDAPV2( flags ) ) {
1221 * to be overly pedantic, we only accept
1224 if ( flags & LDAP_DN_PEDANTIC ) {
1225 if ( !strncmp( p, "OID.", 4 )
1226 || !strncmp( p, "oid.", 4 ) ) {
1228 state = B4OIDATTRTYPE;
1232 if ( !strncasecmp( p, "oid.", 4 ) ) {
1234 state = B4OIDATTRTYPE;
1240 state = B4STRINGATTRTYPE;
1243 case B4OIDATTRTYPE: {
1244 int err = LDAP_SUCCESS;
1247 type = parse_numericoid( &p, &err, 0 );
1248 if ( type == NULL ) {
1251 attrType = LDAP_MALLOC( sizeof( struct berval ) );
1252 if ( attrType== NULL ) {
1253 rc = LDAP_NO_MEMORY;
1256 attrType->bv_val = type;
1257 attrType->bv_len = strlen( type );
1258 attrTypeEncoding = LDAP_AVA_BINARY;
1260 state = B4AVAEQUALS;
1264 case B4STRINGATTRTYPE: {
1265 const char *startPos, *endPos = NULL;
1269 * the starting char has been found to be
1270 * a LDAP_DN_DESC_LEADCHAR so we don't re-check it
1271 * FIXME: DCE attr types seem to have a more
1272 * restrictive syntax (no '-' ...)
1274 for ( startPos = p++; p[ 0 ]; p++ ) {
1275 if ( LDAP_DN_DESC_CHAR( p[ 0 ] ) ) {
1279 if ( LDAP_DN_LANG_SEP( p[ 0 ] ) ) {
1282 * RFC 2253 does not explicitly
1283 * allow lang extensions to attribute
1286 if ( flags & LDAP_DN_PEDANTIC ) {
1291 * we trim ';' and following lang
1292 * and so from attribute types
1295 for ( ; LDAP_DN_ATTRDESC_CHAR( p[ 0 ] )
1296 || LDAP_DN_LANG_SEP( p[ 0 ] ); p++ ) {
1304 len = ( endPos ? endPos : p ) - startPos;
1309 assert( attrType == NULL );
1310 attrType = LDAP_MALLOC( sizeof( struct berval ) );
1311 if ( attrType == NULL ) {
1312 rc = LDAP_NO_MEMORY;
1315 attrType->bv_val = LDAP_STRNDUP( startPos, len );
1316 if ( attrType->bv_val == NULL ) {
1317 rc = LDAP_NO_MEMORY;
1320 attrType->bv_len = len;
1321 attrTypeEncoding = LDAP_AVA_STRING;
1324 * here we need to decide whether to use it as is
1325 * or turn it in OID form; as a consequence, we
1326 * need to decide whether to binary encode the value
1329 state = B4AVAEQUALS;
1334 /* spaces may not be allowed */
1335 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1336 if ( !LDAP_DN_ALLOW_SPACES( flags ) ) {
1341 for ( p++; LDAP_DN_ASCII_SPACE( p[ 0 ] ); p++ ) {
1346 /* need equal sign */
1347 if ( !LDAP_DN_AVA_EQUALS( p[ 0 ] ) ) {
1352 /* spaces may not be allowed */
1353 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1354 if ( !LDAP_DN_ALLOW_SPACES( flags ) ) {
1359 for ( p++; LDAP_DN_ASCII_SPACE( p[ 0 ] ); p++ ) {
1365 * octothorpe means a BER encoded value will follow
1366 * FIXME: I don't think DCE will allow it
1368 if ( LDAP_DN_OCTOTHORPE( p[ 0 ] ) ) {
1370 attrValueEncoding = LDAP_AVA_BINARY;
1371 state = B4BINARYVALUE;
1375 /* STRING value expected */
1378 * if we're pedantic, an attribute type in OID form
1379 * SHOULD imply a BER encoded attribute value; we
1380 * should at least issue a warning
1382 if ( ( flags & LDAP_DN_PEDANTIC )
1383 && ( attrTypeEncoding == LDAP_AVA_BINARY ) ) {
1384 /* OID attrType SHOULD use binary encoding */
1388 attrValueEncoding = LDAP_AVA_STRING;
1391 * LDAPv2 allows the attribute value to be quoted;
1392 * also, IA5 values are expected, in principle
1394 if ( LDAP_DN_LDAPV2( flags ) || LDAP_DN_LDAP( flags ) ) {
1395 if ( LDAP_DN_QUOTES( p[ 0 ] ) ) {
1397 state = B4IA5VALUEQUOTED;
1401 if ( LDAP_DN_LDAPV2( flags ) ) {
1408 * here STRING means RFC 2253 string
1409 * FIXME: what about DCE strings?
1411 state = B4STRINGVALUE;
1415 if ( hexstr2binval( p, &attrValue, &p, flags ) ) {
1423 switch ( LDAP_DN_FORMAT( flags ) ) {
1424 case LDAP_DN_FORMAT_LDAP:
1425 case LDAP_DN_FORMAT_LDAPV3:
1426 if ( str2strval( p, &attrValue, &p, flags,
1427 &attrValueEncoding ) ) {
1432 case LDAP_DN_FORMAT_DCE:
1433 if ( DCE2strval( p, &attrValue, &p, flags ) ) {
1446 if ( IA52strval( p, &attrValue, &p, flags ) ) {
1453 case B4IA5VALUEQUOTED:
1455 /* lead quote already stripped */
1456 if ( quotedIA52strval( p, &attrValue,
1470 * we accept empty values
1472 ava = ldapava_new( attrType, attrValue,
1473 attrValueEncoding );
1474 if ( ava == NULL ) {
1475 rc = LDAP_NO_MEMORY;
1479 rdn = ldapava_append_to_rdn( newRDN, ava );
1480 if ( rdn == NULL ) {
1481 rc = LDAP_NO_MEMORY;
1487 * if we got an AVA separator ('+', or ',' for DCE )
1488 * we expect a new AVA for this RDN; otherwise
1489 * we add the RDN to the DN
1491 switch ( LDAP_DN_FORMAT( flags ) ) {
1492 case LDAP_DN_FORMAT_LDAP:
1493 case LDAP_DN_FORMAT_LDAPV3:
1494 case LDAP_DN_FORMAT_LDAPV2:
1495 if ( !LDAP_DN_AVA_SEP( p[ 0 ] ) ) {
1500 case LDAP_DN_FORMAT_DCE:
1501 if ( !LDAP_DN_AVA_SEP_DCE( p[ 0 ] ) ) {
1509 * the RDN is over, phew
1516 /* they should have been used in an AVA */
1532 /* They are set to NULL after they're used in an AVA */
1534 ber_bvfree( attrType );
1538 ber_bvfree( attrValue );
1542 ldap_rdnfree( newRDN );
1548 Debug( LDAP_DEBUG_TRACE, "<= ldap_str2rdn(%*s)=%d\n",
1556 * reads in a UTF-8 string value, unescaping stuff:
1557 * '\' + LDAP_DN_NEEDESCAPE(c) -> 'c'
1558 * '\' + HEXPAIR(p) -> unhex(p)
1561 str2strval( const char *str, struct berval **val, const char **next, unsigned flags, unsigned *retFlags )
1563 const char *p, *startPos, *endPos = NULL;
1564 ber_len_t len, escapes, unescapes;
1573 for ( startPos = p = str, escapes = 0, unescapes = 0; p[ 0 ]; p++ ) {
1574 if ( LDAP_DN_ESCAPE( p[ 0 ] ) ) {
1576 if ( p[ 0 ] == '\0' ) {
1579 if ( ( p == startPos + 1 && LDAP_DN_NEEDESCAPE_LEAD( p[ 0 ] ) )
1580 || ( LDAP_DN_VALUE_END( p[ 1 ] ) && LDAP_DN_NEEDESCAPE_TRAIL( p[ 0 ] ) )
1581 || LDAP_DN_NEEDESCAPE( p[ 0 ] ) ) {
1586 if ( LDAP_DN_HEXPAIR( p ) ) {
1589 hexstr2bin( p, &c );
1592 if ( !LDAP_DN_ASCII_PRINTABLE( c ) ) {
1595 * we assume the string is UTF-8
1597 *retFlags = LDAP_AVA_NONPRINTABLE;
1604 if ( LDAP_DN_PEDANTIC & flags ) {
1608 * FIXME: we allow escaping
1609 * of chars that don't need
1610 * to and do not belong to
1611 * HEXDIGITS (we also allow
1612 * single hexdigit; maybe we
1617 } else if ( ( LDAP_DN_LDAP( flags ) && LDAP_DN_VALUE_END_V2( p[ 0 ] ) )
1618 || ( LDAP_DN_LDAPV3( flags ) && LDAP_DN_VALUE_END( p[ 0 ] ) ) ) {
1621 } else if ( LDAP_DN_NEEDESCAPE( p[ 0 ] ) ) {
1623 * FIXME: maybe we can add
1624 * escapes if not pedantic?
1631 * we do allow unescaped spaces at the end
1632 * of the value only in non-pedantic mode
1634 if ( p > startPos + 1 && LDAP_DN_ASCII_SPACE( p[ -1 ] ) &&
1635 !LDAP_DN_ESCAPE( p[ -2 ] ) ) {
1636 if ( flags & LDAP_DN_PEDANTIC ) {
1640 /* strip trailing (unescaped) spaces */
1641 for ( endPos = p - 1;
1642 endPos > startPos + 1 &&
1643 LDAP_DN_ASCII_SPACE( endPos[ -1 ] ) &&
1644 !LDAP_DN_ESCAPE( endPos[ -2 ] );
1651 * FIXME: test memory?
1653 len = ( endPos ? endPos : p ) - startPos - escapes - unescapes;
1654 *val = LDAP_MALLOC( sizeof( struct berval ) );
1655 ( *val )->bv_len = len;
1657 if ( escapes == 0 && unescapes == 0 ) {
1658 ( *val )->bv_val = LDAP_STRNDUP( startPos, len );
1663 ( *val )->bv_val = LDAP_MALLOC( len + 1 );
1664 for ( s = 0, d = 0; d < len; ) {
1665 if ( LDAP_DN_ESCAPE( startPos[ s ] ) ) {
1667 if ( ( s == 0 && LDAP_DN_NEEDESCAPE_LEAD( startPos[ s ] ) )
1668 || ( s == len - 1 && LDAP_DN_NEEDESCAPE_TRAIL( startPos[ s ] ) )
1669 || LDAP_DN_NEEDESCAPE( startPos[ s ] ) ) {
1670 ( *val )->bv_val[ d++ ] =
1673 } else if ( LDAP_DN_HEXPAIR( &startPos[ s ] ) ) {
1676 hexstr2bin( &startPos[ s ], &c );
1677 ( *val )->bv_val[ d++ ] = c;
1682 * we allow escaping of chars
1683 * that do not need to
1685 ( *val )->bv_val[ d++ ] =
1690 ( *val )->bv_val[ d++ ] = startPos[ s++ ];
1694 ( *val )->bv_val[ d ] = '\0';
1695 assert( strlen( ( *val )->bv_val ) == len );
1705 DCE2strval( const char *str, struct berval **val, const char **next, unsigned flags )
1707 const char *p, *startPos, *endPos = NULL;
1708 ber_len_t len, escapes;
1717 for ( startPos = p = str, escapes = 0; p[ 0 ]; p++ ) {
1718 if ( LDAP_DN_ESCAPE_DCE( p[ 0 ] ) ) {
1720 if ( LDAP_DN_NEEDESCAPE_DCE( p[ 0 ] ) ) {
1727 } else if ( LDAP_DN_VALUE_END_DCE( p[ 0 ] ) ) {
1732 * FIXME: can we accept anything else? I guess we need
1733 * to stop if a value is not legal
1738 * (unescaped) trailing spaces are trimmed must be silently ignored;
1741 if ( p > startPos + 1 && LDAP_DN_ASCII_SPACE( p[ -1 ] ) &&
1742 !LDAP_DN_ESCAPE( p[ -2 ] ) ) {
1743 if ( flags & LDAP_DN_PEDANTIC ) {
1747 /* strip trailing (unescaped) spaces */
1748 for ( endPos = p - 1;
1749 endPos > startPos + 1 &&
1750 LDAP_DN_ASCII_SPACE( endPos[ -1 ] ) &&
1751 !LDAP_DN_ESCAPE( endPos[ -2 ] );
1758 len = ( endPos ? endPos : p ) - startPos - escapes;
1759 *val = LDAP_MALLOC( sizeof( struct berval ) );
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; ) {
1770 * This point is reached only if escapes
1771 * are properly used, so all we need to
1774 if ( LDAP_DN_ESCAPE_DCE( startPos[ s ] ) ) {
1778 ( *val )->bv_val[ d++ ] = startPos[ s++ ];
1780 ( *val )->bv_val[ d ] = '\0';
1781 assert( strlen( ( *val )->bv_val ) == len );
1790 IA52strval( const char *str, struct berval **val, const char **next, unsigned flags )
1792 const char *p, *startPos, *endPos = NULL;
1793 ber_len_t len, escapes;
1806 for ( startPos = p = str, escapes = 0; p[ 0 ]; p++ ) {
1807 if ( LDAP_DN_ESCAPE( p[ 0 ] ) ) {
1809 if ( p[ 0 ] == '\0' ) {
1813 if ( !LDAP_DN_NEEDESCAPE( p[ 0 ] )
1814 && ( LDAP_DN_PEDANTIC & flags ) ) {
1819 } else if ( LDAP_DN_VALUE_END_V2( p[ 0 ] ) ) {
1824 * FIXME: can we accept anything else? I guess we need
1825 * to stop if a value is not legal
1829 /* strip trailing (unescaped) spaces */
1831 endPos > startPos + 1 &&
1832 LDAP_DN_ASCII_SPACE( endPos[ -1 ] ) &&
1833 !LDAP_DN_ESCAPE( endPos[ -2 ] );
1838 *val = LDAP_MALLOC( sizeof( struct berval ) );
1839 len = ( endPos ? endPos : p ) - startPos - escapes;
1840 ( *val )->bv_len = len;
1841 if ( escapes == 0 ) {
1842 ( *val )->bv_val = LDAP_STRNDUP( startPos, len );
1847 ( *val )->bv_val = LDAP_MALLOC( len + 1 );
1848 for ( s = 0, d = 0; d < len; ) {
1849 if ( LDAP_DN_ESCAPE( startPos[ s ] ) ) {
1852 ( *val )->bv_val[ d++ ] = startPos[ s++ ];
1854 ( *val )->bv_val[ d ] = '\0';
1855 assert( strlen( ( *val )->bv_val ) == len );
1863 quotedIA52strval( const char *str, struct berval **val, const char **next, unsigned flags )
1865 const char *p, *startPos, *endPos = NULL;
1867 unsigned escapes = 0;
1876 /* initial quote already eaten */
1877 for ( startPos = p = str; p[ 0 ]; p++ ) {
1879 * According to RFC 1779, the quoted value can
1880 * contain escaped as well as unescaped special values;
1881 * as a consequence we tolerate escaped values
1882 * (e.g. '"\,"' -> '\,') and escape unescaped specials
1883 * (e.g. '","' -> '\,').
1885 if ( LDAP_DN_ESCAPE( p[ 0 ] ) ) {
1886 if ( p[ 1 ] == '\0' ) {
1891 if ( !LDAP_DN_V2_PAIR( p[ 0 ] )
1892 && ( LDAP_DN_PEDANTIC & flags ) ) {
1894 * do we allow to escape normal chars?
1895 * LDAPv2 does not allow any mechanism
1896 * for escaping chars with '\' and hex
1903 } else if ( LDAP_DN_QUOTES( p[ 0 ] ) ) {
1905 /* eat closing quotes */
1911 * FIXME: can we accept anything else? I guess we need
1912 * to stop if a value is not legal
1916 if ( endPos == NULL ) {
1920 /* Strip trailing (unescaped) spaces */
1921 for ( ; p[ 0 ] && LDAP_DN_ASCII_SPACE( p[ 0 ] ); p++ ) {
1925 len = endPos - startPos - escapes;
1927 *val = LDAP_MALLOC( sizeof( struct berval ) );
1928 ( *val )->bv_len = len;
1929 if ( escapes == 0 ) {
1930 ( *val )->bv_val = LDAP_STRNDUP( startPos, len );
1935 ( *val )->bv_val = LDAP_MALLOC( len + 1 );
1936 ( *val )->bv_len = len;
1938 for ( s = d = 0; d < len; ) {
1939 if ( LDAP_DN_ESCAPE( str[ s ] ) ) {
1942 ( *val )->bv_val[ d++ ] = str[ s++ ];
1944 ( *val )->bv_val[ d ] = '\0';
1945 assert( strlen( ( *val )->bv_val ) == len );
1954 hexstr2bin( const char *str, char *c )
1964 if ( LDAP_DN_ASCII_DIGIT( c1 ) ) {
1970 if ( LDAP_DN_ASCII_LCASE_HEXALPHA( c1 ) ) {
1977 if ( LDAP_DN_ASCII_DIGIT( c2 ) ) {
1983 if ( LDAP_DN_ASCII_LCASE_HEXALPHA( c2 ) ) {
1984 *c += c2 - 'a' + 10;
1992 hexstr2binval( const char *str, struct berval **val, const char **next, unsigned flags )
1994 const char *p, *startPos, *endPos = NULL;
2005 for ( startPos = p = str; p[ 0 ]; p += 2 ) {
2006 switch ( LDAP_DN_FORMAT( flags ) ) {
2007 case LDAP_DN_FORMAT_LDAPV3:
2008 if ( LDAP_DN_VALUE_END( p[ 0 ] ) ) {
2013 case LDAP_DN_FORMAT_LDAP:
2014 case LDAP_DN_FORMAT_LDAPV2:
2015 if ( LDAP_DN_VALUE_END_V2( p[ 0 ] ) ) {
2020 case LDAP_DN_FORMAT_DCE:
2021 if ( LDAP_DN_VALUE_END_DCE( p[ 0 ] ) ) {
2027 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
2028 if ( flags & LDAP_DN_PEDANTIC ) {
2033 for ( ; p[ 0 ]; p++ ) {
2034 switch ( LDAP_DN_FORMAT( flags ) ) {
2035 case LDAP_DN_FORMAT_LDAPV3:
2036 if ( LDAP_DN_VALUE_END( p[ 0 ] ) ) {
2041 case LDAP_DN_FORMAT_LDAP:
2042 case LDAP_DN_FORMAT_LDAPV2:
2043 if ( LDAP_DN_VALUE_END_V2( p[ 0 ] ) ) {
2048 case LDAP_DN_FORMAT_DCE:
2049 if ( LDAP_DN_VALUE_END_DCE( p[ 0 ] ) ) {
2058 if ( !LDAP_DN_HEXPAIR( p ) ) {
2065 len = ( ( endPos ? endPos : p ) - startPos ) / 2;
2067 assert( 2 * len == (ber_len_t) (( endPos ? endPos : p ) - startPos ));
2069 *val = LDAP_MALLOC( sizeof( struct berval ) );
2070 if ( *val == NULL ) {
2071 return( LDAP_NO_MEMORY );
2074 ( *val )->bv_len = len;
2075 ( *val )->bv_val = LDAP_MALLOC( len + 1 );
2076 if ( ( *val )->bv_val == NULL ) {
2078 return( LDAP_NO_MEMORY );
2081 for ( s = 0, d = 0; d < len; s += 2, d++ ) {
2084 hexstr2bin( &startPos[ s ], &c );
2086 ( *val )->bv_val[ d ] = c;
2089 ( *val )->bv_val[ d ] = '\0';
2096 * convert a byte in a hexadecimal pair
2099 byte2hexpair( const char *val, char *pair )
2101 static const char hexdig[] = "0123456789abcdef";
2107 * we assume the string has enough room for the hex encoding
2111 pair[ 0 ] = hexdig[ 0x0f & ( val[ 0 ] >> 4 ) ];
2112 pair[ 1 ] = hexdig[ 0x0f & val[ 0 ] ];
2118 * convert a binary value in hexadecimal pairs
2121 binval2hexstr( struct berval *val, char *str )
2128 if ( val->bv_len == 0 ) {
2133 * we assume the string has enough room for the hex encoding
2137 for ( s = 0, d = 0; s < val->bv_len; s++, d += 2 ) {
2138 byte2hexpair( &val->bv_val[ s ], &str[ d ] );
2145 * Length of the string representation, accounting for escaped hex
2149 strval2strlen( struct berval *val, unsigned flags, ber_len_t *len )
2151 ber_len_t l, cl = 1;
2153 int escaped_byte_len = LDAP_DN_IS_PRETTY( flags ) ? 1 : 3;
2159 if ( val->bv_len == 0 ) {
2163 for ( l = 0, p = val->bv_val; p[ 0 ]; p += cl ) {
2164 cl = ldap_utf8_charlen( p );
2166 /* illegal utf-8 char! */
2169 } else if ( cl > 1 ) {
2172 for ( cnt = 1; cnt < cl; cnt++ ) {
2173 if ( ( p[ cnt ] & 0x80 ) == 0x00 ) {
2177 l += escaped_byte_len * cl;
2180 * there might be some chars we want to escape in form
2181 * of a couple of hexdigits for optimization purposes
2183 } else if ( LDAP_DN_WILLESCAPE( flags, p[ 0 ] ) ) {
2186 } else if ( LDAP_DN_NEEDESCAPE( p[ 0 ] )
2187 || ( p == val->bv_val && LDAP_DN_NEEDESCAPE_LEAD( p[ 0 ] ) )
2188 || ( !p[ 1 ] && LDAP_DN_NEEDESCAPE_TRAIL( p[ 0 ] ) ) ) {
2202 * convert to string representation, escaping with hex the UTF-8 stuff;
2203 * assume the destination has enough room for escaping
2206 strval2str( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2208 ber_len_t s, d, end;
2214 if ( val->bv_len == 0 ) {
2220 * we assume the string has enough room for the hex encoding
2223 for ( s = 0, d = 0, end = val->bv_len - 1; s < val->bv_len; ) {
2224 ber_len_t cl = ldap_utf8_charlen( &val->bv_val[ s ] );
2227 * there might be some chars we want to escape in form
2228 * of a couple of hexdigits for optimization purposes
2230 if ( ( cl > 1 && !LDAP_DN_IS_PRETTY( flags ) )
2231 || LDAP_DN_WILLESCAPE( flags, val->bv_val[ s ] ) ) {
2234 byte2hexpair( &val->bv_val[ s ], &str[ d ] );
2239 } else if ( cl > 1 ) {
2241 str[ d++ ] = val->bv_val[ s++ ];
2245 if ( LDAP_DN_NEEDESCAPE( val->bv_val[ s ] )
2246 || ( d == 0 && LDAP_DN_NEEDESCAPE_LEAD( val->bv_val[ s ] ) )
2247 || ( s == end && LDAP_DN_NEEDESCAPE_TRAIL( val->bv_val[ s ] ) ) ) {
2250 str[ d++ ] = val->bv_val[ s++ ];
2260 * Length of the IA5 string representation (no UTF-8 allowed)
2263 strval2IA5strlen( struct berval *val, unsigned flags, ber_len_t *len )
2272 if ( val->bv_len == 0 ) {
2276 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2278 * Turn value into a binary encoded BER
2283 for ( l = 0, p = val->bv_val; p[ 0 ]; p++ ) {
2284 if ( LDAP_DN_NEEDESCAPE( p[ 0 ] )
2285 || ( p == val->bv_val && LDAP_DN_NEEDESCAPE_LEAD( p[ 0 ] ) )
2286 || ( !p[ 1 ] && LDAP_DN_NEEDESCAPE_TRAIL( p[ 0 ] ) ) ) {
2301 * convert to string representation (np UTF-8)
2302 * assume the destination has enough room for escaping
2305 strval2IA5str( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2307 ber_len_t s, d, end;
2313 if ( val->bv_len == 0 ) {
2318 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2320 * Turn value into a binary encoded BER
2327 * we assume the string has enough room for the hex encoding
2331 for ( s = 0, d = 0, end = val->bv_len - 1; s < val->bv_len; ) {
2332 if ( LDAP_DN_NEEDESCAPE( val->bv_val[ s ] )
2333 || ( s == 0 && LDAP_DN_NEEDESCAPE_LEAD( val->bv_val[ s ] ) )
2334 || ( s == end && LDAP_DN_NEEDESCAPE_TRAIL( val->bv_val[ s ] ) ) ) {
2337 str[ d++ ] = val->bv_val[ s++ ];
2347 * Length of the (supposedly) DCE string representation,
2348 * accounting for escaped hex of UTF-8 chars
2351 strval2DCEstrlen( struct berval *val, unsigned flags, ber_len_t *len )
2360 if ( val->bv_len == 0 ) {
2364 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2366 * FIXME: Turn the value into a binary encoded BER?
2371 for ( l = 0, p = val->bv_val; p[ 0 ]; p++ ) {
2372 if ( LDAP_DN_NEEDESCAPE_DCE( p[ 0 ] ) ) {
2387 * convert to (supposedly) DCE string representation,
2388 * escaping with hex the UTF-8 stuff;
2389 * assume the destination has enough room for escaping
2392 strval2DCEstr( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2400 if ( val->bv_len == 0 ) {
2405 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2407 * FIXME: Turn the value into a binary encoded BER?
2415 * we assume the string has enough room for the hex encoding
2419 for ( s = 0, d = 0; s < val->bv_len; ) {
2420 if ( LDAP_DN_NEEDESCAPE_DCE( val->bv_val[ s ] ) ) {
2423 str[ d++ ] = val->bv_val[ s++ ];
2433 * Length of the (supposedly) AD canonical string representation,
2434 * accounting for escaped hex of UTF-8 chars
2437 strval2ADstrlen( struct berval *val, unsigned flags, ber_len_t *len )
2446 if ( val->bv_len == 0 ) {
2450 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2452 * FIXME: Turn the value into a binary encoded BER?
2457 for ( l = 0, p = val->bv_val; p[ 0 ]; p++ ) {
2458 if ( LDAP_DN_NEEDESCAPE_AD( p[ 0 ] ) ) {
2473 * convert to (supposedly) AD string representation,
2474 * escaping with hex the UTF-8 stuff;
2475 * assume the destination has enough room for escaping
2478 strval2ADstr( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2486 if ( val->bv_len == 0 ) {
2491 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2493 * FIXME: Turn the value into a binary encoded BER?
2501 * we assume the string has enough room for the hex encoding
2505 for ( s = 0, d = 0; s < val->bv_len; ) {
2506 if ( LDAP_DN_NEEDESCAPE_AD( val->bv_val[ s ] ) ) {
2509 str[ d++ ] = val->bv_val[ s++ ];
2519 * If the DN is terminated by single-AVA RDNs with attribute type of "dc",
2520 * the forst part of the AD representation of the DN is written in DNS
2521 * form, i.e. dot separated domain name components (as suggested
2522 * by Luke Howard, http://www.padl.com/~lukeh)
2525 dn2domain( LDAPDN *dn, char *str, int *iRDN )
2528 int domain = 0, first = 1;
2529 ber_len_t l = 1; /* we move the null also */
2531 /* we are guaranteed there's enough memory in str */
2537 assert( *iRDN > 0 );
2539 for ( i = *iRDN; i >= 0; i-- ) {
2543 assert( dn[ i ][ 0 ] );
2546 assert( rdn[ 0 ][ 0 ] );
2547 ava = rdn[ 0 ][ 0 ];
2549 if ( !LDAP_DN_IS_RDN_DC( rdn ) ) {
2557 AC_MEMCPY( str, ava->la_value->bv_val,
2558 ava->la_value->bv_len + 1);
2559 l += ava->la_value->bv_len;
2562 AC_MEMCPY( str + ava->la_value->bv_len + 1, str, l);
2563 AC_MEMCPY( str, ava->la_value->bv_val,
2564 ava->la_value->bv_len );
2565 str[ ava->la_value->bv_len ] = '.';
2566 l += ava->la_value->bv_len + 1;
2576 rdn2strlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len,
2577 int ( *s2l )( struct berval *v, unsigned f, ber_len_t *l ) )
2584 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2585 LDAPAVA *ava = rdn[ iAVA ][ 0 ];
2587 /* len(type) + '=' + '+' | ',' */
2588 l += ava->la_attr->bv_len + 2;
2590 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2591 /* octothorpe + twice the length */
2592 l += 1 + 2 * ava->la_value->bv_len;
2596 unsigned f = flags | ava->la_flags;
2598 if ( ( *s2l )( ava->la_value, f, &vl ) ) {
2611 rdn2str( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len,
2612 int ( *s2s ) ( struct berval *v, char * s, unsigned f, ber_len_t *l ) )
2617 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2618 LDAPAVA *ava = rdn[ iAVA ][ 0 ];
2620 AC_MEMCPY( &str[ l ], ava->la_attr->bv_val,
2621 ava->la_attr->bv_len );
2622 l += ava->la_attr->bv_len;
2626 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2628 if ( binval2hexstr( ava->la_value, &str[ l ] ) ) {
2631 l += 2 * ava->la_value->bv_len;
2635 unsigned f = flags | ava->la_flags;
2637 if ( ( *s2s )( ava->la_value, &str[ l ], f, &vl ) ) {
2642 str[ l++ ] = ( rdn[ iAVA + 1 ] ? '+' : ',' );
2651 rdn2DCEstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len )
2658 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2659 LDAPAVA *ava = rdn[ iAVA ][ 0 ];
2661 /* len(type) + '=' + ',' | '/' */
2662 l += ava->la_attr->bv_len + 2;
2664 switch ( ava->la_flags ) {
2665 case LDAP_AVA_BINARY:
2666 /* octothorpe + twice the length */
2667 l += 1 + 2 * ava->la_value->bv_len;
2670 case LDAP_AVA_STRING: {
2672 unsigned f = flags | ava->la_flags;
2674 if ( strval2DCEstrlen( ava->la_value, f, &vl ) ) {
2692 rdn2DCEstr( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len, int first )
2697 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2698 LDAPAVA *ava = rdn[ iAVA ][ 0 ];
2703 str[ l++ ] = ( iAVA ? ',' : '/' );
2706 AC_MEMCPY( &str[ l ], ava->la_attr->bv_val,
2707 ava->la_attr->bv_len );
2708 l += ava->la_attr->bv_len;
2712 switch ( ava->la_flags ) {
2713 case LDAP_AVA_BINARY:
2715 if ( binval2hexstr( ava->la_value, &str[ l ] ) ) {
2718 l += 2 * ava->la_value->bv_len;
2721 case LDAP_AVA_STRING: {
2723 unsigned f = flags | ava->la_flags;
2725 if ( strval2DCEstr( ava->la_value, &str[ l ], f, &vl ) ) {
2743 rdn2UFNstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len )
2753 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2754 LDAPAVA *ava = rdn[ iAVA ][ 0 ];
2757 l += ( rdn[ iAVA + 1 ] ? 3 : 2 );
2759 /* FIXME: are binary values allowed in UFN? */
2760 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2761 /* octothorpe + twice the value */
2762 l += 1 + 2 * ava->la_value->bv_len;
2766 unsigned f = flags | ava->la_flags;
2768 if ( strval2strlen( ava->la_value, f, &vl ) ) {
2781 rdn2UFNstr( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len )
2786 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2787 LDAPAVA *ava = rdn[ iAVA ][ 0 ];
2789 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2791 if ( binval2hexstr( ava->la_value, &str[ l ] ) ) {
2794 l += 2 * ava->la_value->bv_len;
2798 unsigned f = flags | ava->la_flags;
2800 if ( strval2str( ava->la_value, &str[ l ], f, &vl ) ) {
2806 if ( rdn[ iAVA + 1 ]) {
2807 AC_MEMCPY( &str[ l ], " + ", 3 );
2811 AC_MEMCPY( &str[ l ], ", ", 2 );
2822 rdn2ADstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len )
2832 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2833 LDAPAVA *ava = rdn[ iAVA ][ 0 ];
2838 /* FIXME: are binary values allowed in UFN? */
2839 switch ( ava->la_flags ) {
2840 case LDAP_AVA_BINARY:
2841 /* octothorpe + twice the value */
2842 l += 1 + 2 * ava->la_value->bv_len;
2845 case LDAP_AVA_STRING: {
2847 unsigned f = flags | ava->la_flags;
2849 if ( strval2ADstrlen( ava->la_value, f, &vl ) ) {
2867 rdn2ADstr( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len, int first )
2872 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2873 LDAPAVA *ava = rdn[ iAVA ][ 0 ];
2878 str[ l++ ] = ( iAVA ? ',' : '/' );
2881 switch ( ava->la_flags ) {
2882 case LDAP_AVA_BINARY:
2884 if ( binval2hexstr( ava->la_value, &str[ l ] ) ) {
2887 l += 2 * ava->la_value->bv_len;
2890 case LDAP_AVA_STRING: {
2892 unsigned f = flags | ava->la_flags;
2894 if ( strval2ADstr( ava->la_value, &str[ l ], f, &vl ) ) {
2914 * Returns in str a string representation of rdn based on flags.
2915 * There is some duplication of code between this and ldap_dn2str;
2916 * this is wanted to reduce the allocation of temporary buffers.
2919 ldap_rdn2str( LDAPRDN *rdn, char **str, unsigned flags )
2926 if ( rdn == NULL ) {
2927 *str = LDAP_STRDUP( "" );
2928 return( LDAP_SUCCESS );
2932 * This routine wastes "back" bytes at the end of the string
2936 switch ( LDAP_DN_FORMAT( flags ) ) {
2937 case LDAP_DN_FORMAT_LDAPV3:
2938 if ( rdn2strlen( rdn, flags, &l, strval2strlen ) ) {
2939 return( LDAP_OTHER );
2943 case LDAP_DN_FORMAT_LDAPV2:
2944 if ( rdn2strlen( rdn, flags, &l, strval2IA5strlen ) ) {
2945 return( LDAP_OTHER );
2949 case LDAP_DN_FORMAT_UFN:
2950 if ( rdn2UFNstrlen( rdn, flags, &l ) ) {
2951 return( LDAP_OTHER );
2955 case LDAP_DN_FORMAT_DCE:
2956 if ( rdn2DCEstrlen( rdn, flags, &l ) ) {
2957 return( LDAP_OTHER );
2961 case LDAP_DN_FORMAT_AD_CANONICAL:
2962 if ( rdn2ADstrlen( rdn, flags, &l ) ) {
2963 return( LDAP_OTHER );
2968 return( LDAP_INVALID_DN_SYNTAX );
2971 *str = LDAP_MALLOC( l + 1 );
2973 switch ( LDAP_DN_FORMAT( flags ) ) {
2974 case LDAP_DN_FORMAT_LDAPV3:
2975 rc = rdn2str( rdn, *str, flags, &l, strval2str );
2979 case LDAP_DN_FORMAT_LDAPV2:
2980 rc = rdn2str( rdn, *str, flags, &l, strval2IA5str );
2984 case LDAP_DN_FORMAT_UFN:
2985 rc = rdn2UFNstr( rdn, *str, flags, &l );
2989 case LDAP_DN_FORMAT_DCE:
2990 rc = rdn2DCEstr( rdn, *str, flags, &l, 1 );
2994 case LDAP_DN_FORMAT_AD_CANONICAL:
2995 rc = rdn2ADstr( rdn, *str, flags, &l, 1 );
3000 /* need at least one of the previous */
3001 return( LDAP_OTHER );
3005 ldap_memfree( *str );
3006 return( LDAP_OTHER );
3009 ( *str )[ l - back ] = '\0';
3011 return( LDAP_SUCCESS );
3015 * Very bulk implementation; many optimizations can be performed
3016 * - a NULL dn results in an empty string ""
3019 * a) what do we do if a UTF-8 string must be converted in LDAPv2?
3020 * we must encode it in binary form ('#' + HEXPAIRs)
3021 * b) does DCE/AD support UTF-8?
3022 * no clue; don't think so.
3023 * c) what do we do when binary values must be converted in UTF/DCE/AD?
3024 * use binary encoded BER
3026 int ldap_dn2str( LDAPDN *dn, char **str, unsigned flags )
3029 int rc = LDAP_OTHER;
3032 /* stringifying helpers for LDAPv3/LDAPv2 */
3033 int ( *sv2l ) ( struct berval *v, unsigned f, ber_len_t *l );
3034 int ( *sv2s ) ( struct berval *v, char *s, unsigned f, ber_len_t *l );
3038 Debug( LDAP_DEBUG_TRACE, "=> ldap_dn2str(%u)\n%s%s", flags, "", "" );
3043 * a null dn means an empty dn string
3044 * FIXME: better raise an error?
3047 *str = LDAP_STRDUP( "" );
3048 return( LDAP_SUCCESS );
3051 switch ( LDAP_DN_FORMAT( flags ) ) {
3052 case LDAP_DN_FORMAT_LDAPV3:
3053 sv2l = strval2strlen;
3057 case LDAP_DN_FORMAT_LDAPV2:
3058 sv2l = strval2IA5strlen;
3059 sv2s = strval2IA5str;
3062 for ( iRDN = 0, len = 0; dn[ iRDN ]; iRDN++ ) {
3064 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
3066 if ( rdn2strlen( rdn, flags, &rdnl, sv2l ) ) {
3067 goto return_results;
3073 if ( ( *str = LDAP_MALLOC( len + 1 ) ) == NULL ) {
3074 rc = LDAP_NO_MEMORY;
3078 for ( l = 0, iRDN = 0; dn[ iRDN ]; iRDN++ ) {
3080 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
3082 if ( rdn2str( rdn, &( *str )[ l ], flags,
3086 goto return_results;
3094 * trim the last ',' (the allocated memory
3095 * is one byte longer than required)
3097 ( *str )[ len - 1 ] = '\0';
3102 case LDAP_DN_FORMAT_UFN: {
3105 * FIXME: quoting from RFC 1781:
3107 To take a distinguished name, and generate a name of this format with
3108 attribute types omitted, the following steps are followed.
3110 1. If the first attribute is of type CommonName, the type may be
3113 2. If the last attribute is of type Country, the type may be
3116 3. If the last attribute is of type Country, the last
3117 Organisation attribute may have the type omitted.
3119 4. All attributes of type OrganisationalUnit may have the type
3120 omitted, unless they are after an Organisation attribute or
3121 the first attribute is of type OrganisationalUnit.
3123 * this should be the pedantic implementation.
3125 * Here the standard implementation reflects
3126 * the one historically provided by OpenLDAP
3127 * (and UMIch, I presume), with the variant
3128 * of spaces and plusses (' + ') separating
3131 * A non-standard but nice implementation could
3132 * be to turn the final "dc" attributes into a
3133 * dot-separated domain.
3135 * Other improvements could involve the use of
3136 * friendly country names and so.
3139 int leftmost_dc = -1;
3141 #endif /* DC_IN_UFN */
3143 for ( iRDN = 0, len = 0; dn[ iRDN ]; iRDN++ ) {
3145 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
3147 if ( rdn2UFNstrlen( rdn, flags, &rdnl ) ) {
3148 goto return_results;
3153 if ( LDAP_DN_IS_RDN_DC( rdn ) ) {
3154 if ( leftmost_dc == -1 ) {
3160 #endif /* DC_IN_UFN */
3163 if ( ( *str = LDAP_MALLOC( len + 1 ) ) == NULL ) {
3164 rc = LDAP_NO_MEMORY;
3169 if ( leftmost_dc == -1 ) {
3170 #endif /* DC_IN_UFN */
3171 for ( l = 0, iRDN = 0; dn[ iRDN ]; iRDN++ ) {
3173 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
3175 if ( rdn2UFNstr( rdn, &( *str )[ l ],
3179 goto return_results;
3185 * trim the last ', ' (the allocated memory
3186 * is two bytes longer than required)
3188 ( *str )[ len - 2 ] = '\0';
3191 last_iRDN = iRDN - 1;
3193 for ( l = 0, iRDN = 0; iRDN < leftmost_dc; iRDN++ ) {
3195 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
3197 if ( rdn2UFNstr( rdn, &( *str )[ l ],
3201 goto return_results;
3206 if ( !dn2domain( dn, &( *str )[ l ], &last_iRDN ) ) {
3209 goto return_results;
3212 /* the string is correctly terminated by dn2domain */
3214 #endif /* DC_IN_UFN */
3220 case LDAP_DN_FORMAT_DCE:
3222 for ( iRDN = 0, len = 0; dn[ iRDN ]; iRDN++ ) {
3224 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
3226 if ( rdn2DCEstrlen( rdn, flags, &rdnl ) ) {
3227 goto return_results;
3233 if ( ( *str = LDAP_MALLOC( len + 1 ) ) == NULL ) {
3234 rc = LDAP_NO_MEMORY;
3238 for ( l = 0; iRDN--; ) {
3240 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
3242 if ( rdn2DCEstr( rdn, &( *str )[ l ], flags,
3246 goto return_results;
3253 ( *str )[ len ] = '\0';
3258 case LDAP_DN_FORMAT_AD_CANONICAL: {
3261 * Sort of UFN for DCE DNs: a slash ('/') separated
3262 * global->local DN with no types; strictly speaking,
3263 * the naming context should be a domain, which is
3264 * written in DNS-style, e.g. dot-deparated.
3268 * "givenName=Bill+sn=Gates,ou=People,dc=microsoft,dc=com"
3272 * "microsoft.com/People/Bill,Gates"
3274 for ( iRDN = 0, len = -1; dn[ iRDN ]; iRDN++ ) {
3276 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
3278 if ( rdn2ADstrlen( rdn, flags, &rdnl ) ) {
3279 goto return_results;
3285 if ( ( *str = LDAP_MALLOC( len + 1 ) ) == NULL ) {
3286 rc = LDAP_NO_MEMORY;
3291 if ( iRDN && dn2domain( dn, *str, &iRDN ) ) {
3292 for ( l = strlen( *str ); iRDN >= 0 ; iRDN-- ) {
3294 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
3296 if ( rdn2ADstr( rdn, &( *str )[ l ],
3297 flags, &rdnl, 0 ) ) {
3300 goto return_results;
3309 * Strictly speaking, AD canonical requires
3310 * a DN to be in the form "..., dc=smtg",
3311 * i.e. terminated by a domain component
3313 if ( flags & LDAP_DN_PEDANTIC ) {
3316 rc = LDAP_INVALID_DN_SYNTAX;
3320 for ( l = 0; iRDN >= 0 ; iRDN-- ) {
3322 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
3324 if ( rdn2ADstr( rdn, &( *str )[ l ],
3325 flags, &rdnl, first ) ) {
3328 goto return_results;
3337 ( *str )[ len ] = '\0';
3344 return( LDAP_INVALID_DN_SYNTAX );
3348 Debug( LDAP_DEBUG_TRACE, "<= ldap_dn2str(%s,%u)=%d\n", *str, flags, rc );